2020-09-22 06:50:40 +02:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2020-12-02 00:23:43 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2019-03-15 04:37:54 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2018-02-05 00:08:20 +01:00
|
|
|
using System;
|
2020-12-02 00:23:43 +01:00
|
|
|
using System.Buffers.Binary;
|
|
|
|
using System.Collections.Generic;
|
2018-02-05 00:08:20 +01:00
|
|
|
using System.IO;
|
2020-12-02 00:23:43 +01:00
|
|
|
using System.Threading;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-09-22 06:50:40 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2020-09-22 06:50:40 +02:00
|
|
|
class ServerBase
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2020-12-02 00:23:43 +01:00
|
|
|
// Must be the maximum value used by services (highest one know is the one used by nvservices = 0x8000).
|
|
|
|
// Having a size that is too low will cause failures as data copy will fail if the receiving buffer is
|
|
|
|
// not large enough.
|
|
|
|
private const int PointerBufferSize = 0x8000;
|
|
|
|
|
|
|
|
private readonly static int[] DefaultCapabilities = new int[]
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2020-12-02 00:23:43 +01:00
|
|
|
0x030363F7,
|
|
|
|
0x1FFFFFCF,
|
|
|
|
0x207FFFEF,
|
|
|
|
0x47E0060F,
|
|
|
|
0x0048BFFF,
|
|
|
|
0x01007FFF
|
|
|
|
};
|
2020-09-22 06:50:40 +02:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
private readonly KernelContext _context;
|
|
|
|
private readonly KProcess _selfProcess;
|
|
|
|
|
|
|
|
private readonly List<int> _sessionHandles = new List<int>();
|
|
|
|
private readonly List<int> _portHandles = new List<int>();
|
|
|
|
private readonly Dictionary<int, IpcService> _sessions = new Dictionary<int, IpcService>();
|
|
|
|
private readonly Dictionary<int, IpcService> _ports = new Dictionary<int, IpcService>();
|
|
|
|
|
|
|
|
public ManualResetEvent InitDone { get; }
|
|
|
|
public IpcService SmObject { get; set; }
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
|
|
public ServerBase(KernelContext context, string name)
|
|
|
|
{
|
|
|
|
InitDone = new ManualResetEvent(false);
|
|
|
|
Name = name;
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
const ProcessCreationFlags flags =
|
|
|
|
ProcessCreationFlags.EnableAslr |
|
|
|
|
ProcessCreationFlags.AddressSpace64Bit |
|
|
|
|
ProcessCreationFlags.Is64Bit |
|
|
|
|
ProcessCreationFlags.PoolPartitionSystem;
|
|
|
|
|
|
|
|
ProcessCreationInfo creationInfo = new ProcessCreationInfo("Service", 1, 0, 0x8000000, 1, flags, 0, 0);
|
|
|
|
|
|
|
|
context.Syscall.CreateProcess(creationInfo, DefaultCapabilities, out int handle, null, ServerLoop);
|
|
|
|
|
|
|
|
_selfProcess = context.Scheduler.GetCurrentProcess().HandleTable.GetKProcess(handle);
|
|
|
|
|
|
|
|
context.Syscall.StartProcess(handle, 44, 3, 0x1000);
|
2020-09-22 06:50:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
private void AddPort(int serverPortHandle, IpcService obj)
|
|
|
|
{
|
|
|
|
_portHandles.Add(serverPortHandle);
|
|
|
|
_ports.Add(serverPortHandle, obj);
|
|
|
|
}
|
2020-09-22 06:50:40 +02:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
public void AddSessionObj(KServerSession serverSession, IpcService obj)
|
2020-09-22 06:50:40 +02:00
|
|
|
{
|
2020-12-02 00:23:43 +01:00
|
|
|
_selfProcess.HandleTable.GenerateHandle(serverSession, out int serverSessionHandle);
|
|
|
|
AddSessionObj(serverSessionHandle, obj);
|
2020-09-22 06:50:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
public void AddSessionObj(int serverSessionHandle, IpcService obj)
|
2020-09-22 06:50:40 +02:00
|
|
|
{
|
2020-12-02 00:23:43 +01:00
|
|
|
_sessionHandles.Add(serverSessionHandle);
|
|
|
|
_sessions.Add(serverSessionHandle, obj);
|
2020-09-22 06:50:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
private void ServerLoop()
|
2020-09-22 06:50:40 +02:00
|
|
|
{
|
2020-12-02 00:23:43 +01:00
|
|
|
if (SmObject != null)
|
|
|
|
{
|
|
|
|
_context.Syscall.ManageNamedPort("sm:", 50, out int serverPortHandle);
|
|
|
|
|
|
|
|
AddPort(serverPortHandle, SmObject);
|
2020-09-22 06:50:40 +02:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
InitDone.Set();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InitDone.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread thread = _context.Scheduler.GetCurrentThread();
|
|
|
|
ulong messagePtr = thread.TlsAddress;
|
|
|
|
_context.Syscall.SetHeapSize(0x200000, out ulong heapAddr);
|
|
|
|
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));
|
|
|
|
|
|
|
|
int replyTargetHandle = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int[] handles = _portHandles.ToArray();
|
|
|
|
|
|
|
|
for (int i = 0; i < handles.Length; i++)
|
|
|
|
{
|
|
|
|
if (_context.Syscall.AcceptSession(handles[i], out int serverSessionHandle) == KernelResult.Success)
|
|
|
|
{
|
|
|
|
AddSessionObj(serverSessionHandle, _ports[handles[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handles = _sessionHandles.ToArray();
|
2020-09-22 06:50:40 +02:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
var rc = _context.Syscall.ReplyAndReceive(handles, replyTargetHandle, 1000000L, out int signaledIndex);
|
|
|
|
|
|
|
|
thread.HandlePostSyscall();
|
|
|
|
|
|
|
|
if (!thread.Context.Running)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
replyTargetHandle = 0;
|
|
|
|
|
|
|
|
if (rc == KernelResult.Success && signaledIndex != -1)
|
|
|
|
{
|
|
|
|
int signaledHandle = handles[signaledIndex];
|
|
|
|
|
|
|
|
if (Process(signaledHandle, heapAddr))
|
|
|
|
{
|
|
|
|
replyTargetHandle = signaledHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
|
|
|
|
_selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool Process(int serverSessionHandle, ulong recvListAddr)
|
|
|
|
{
|
|
|
|
KProcess process = _context.Scheduler.GetCurrentProcess();
|
|
|
|
KThread thread = _context.Scheduler.GetCurrentThread();
|
|
|
|
ulong messagePtr = thread.TlsAddress;
|
|
|
|
ulong messageSize = 0x100;
|
|
|
|
|
|
|
|
byte[] reqData = new byte[messageSize];
|
|
|
|
|
|
|
|
process.CpuMemory.Read(messagePtr, reqData);
|
|
|
|
|
|
|
|
IpcMessage request = new IpcMessage(reqData, (long)messagePtr);
|
2018-12-06 12:16:24 +01:00
|
|
|
IpcMessage response = new IpcMessage();
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
ulong tempAddr = recvListAddr;
|
|
|
|
int sizesOffset = request.RawData.Length - ((request.RecvListBuff.Count * 2 + 3) & ~3);
|
|
|
|
|
|
|
|
bool noReceive = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < request.ReceiveBuff.Count; i++)
|
|
|
|
{
|
|
|
|
noReceive &= (request.ReceiveBuff[i].Position == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noReceive)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < request.RecvListBuff.Count; i++)
|
|
|
|
{
|
|
|
|
int size = BinaryPrimitives.ReadInt16LittleEndian(request.RawData.AsSpan().Slice(sizesOffset + i * 2, 2));
|
|
|
|
|
|
|
|
response.PtrBuff.Add(new IpcPtrBuffDesc((long)tempAddr, i, size));
|
|
|
|
|
|
|
|
request.RecvListBuff[i] = new IpcRecvListBuffDesc((long)tempAddr, size);
|
|
|
|
|
|
|
|
tempAddr += (ulong)size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldReply = true;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream raw = new MemoryStream(request.RawData))
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryReader reqReader = new BinaryReader(raw);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (request.Type == IpcMessageType.Request ||
|
|
|
|
request.Type == IpcMessageType.RequestWithContext)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
response.Type = IpcMessageType.Response;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream resMs = new MemoryStream())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryWriter resWriter = new BinaryWriter(resMs);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ServiceCtx context = new ServiceCtx(
|
2020-12-02 00:23:43 +01:00
|
|
|
_context.Device,
|
|
|
|
process,
|
|
|
|
process.CpuMemory,
|
|
|
|
thread,
|
2018-12-06 12:16:24 +01:00
|
|
|
request,
|
|
|
|
response,
|
|
|
|
reqReader,
|
|
|
|
resWriter);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
_sessions[serverSessionHandle].CallMethod(context);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
response.RawData = resMs.ToArray();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (request.Type == IpcMessageType.Control ||
|
|
|
|
request.Type == IpcMessageType.ControlWithContext)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2020-01-30 21:43:47 +01:00
|
|
|
uint magic = (uint)reqReader.ReadUInt64();
|
|
|
|
uint cmdId = (uint)reqReader.ReadUInt64();
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
switch (cmdId)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-12 05:04:52 +01:00
|
|
|
case 0:
|
2020-12-02 00:23:43 +01:00
|
|
|
request = FillResponse(response, 0, _sessions[serverSessionHandle].ConvertToDomain());
|
2018-03-12 05:04:52 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2020-12-02 00:23:43 +01:00
|
|
|
request = FillResponse(response, 0, PointerBufferSize);
|
2018-03-12 05:04:52 +01:00
|
|
|
break;
|
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
// TODO: Whats the difference between IpcDuplicateSession/Ex?
|
2018-04-05 02:01:36 +02:00
|
|
|
case 2:
|
2018-03-12 05:04:52 +01:00
|
|
|
case 4:
|
2018-12-06 12:16:24 +01:00
|
|
|
int unknown = reqReader.ReadInt32();
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
_context.Syscall.CreateSession(false, 0, out int dupServerSessionHandle, out int dupClientSessionHandle);
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
AddSessionObj(dupServerSessionHandle, _sessions[serverSessionHandle]);
|
|
|
|
|
|
|
|
response.HandleDesc = IpcHandleDesc.MakeMove(dupClientSessionHandle);
|
2018-03-12 05:04:52 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
request = FillResponse(response, 0);
|
2018-03-12 05:04:52 +01:00
|
|
|
|
|
|
|
break;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
default: throw new NotImplementedException(cmdId.ToString());
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (request.Type == IpcMessageType.CloseSession)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2020-12-02 00:23:43 +01:00
|
|
|
_context.Syscall.CloseHandle(serverSessionHandle);
|
|
|
|
_sessionHandles.Remove(serverSessionHandle);
|
|
|
|
IpcService service = _sessions[serverSessionHandle];
|
|
|
|
if (service is IDisposable disposableObj)
|
|
|
|
{
|
|
|
|
disposableObj.Dispose();
|
|
|
|
}
|
|
|
|
_sessions.Remove(serverSessionHandle);
|
|
|
|
shouldReply = false;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new NotImplementedException(request.Type.ToString());
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
process.CpuMemory.Write(messagePtr, response.GetBytes((long)messagePtr, recvListAddr | ((ulong)PointerBufferSize << 48)));
|
|
|
|
return shouldReply;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static IpcMessage FillResponse(IpcMessage response, long result, params int[] values)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
foreach (int value in values)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(value);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return FillResponse(response, result, ms.ToArray());
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static IpcMessage FillResponse(IpcMessage response, long result, byte[] data = null)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
response.Type = IpcMessageType.Response;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(IpcMagic.Sfco);
|
|
|
|
writer.Write(result);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (data != null)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(data);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
response.RawData = ms.ToArray();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return response;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 06:50:40 +02:00
|
|
|
}
|