2020-09-22 00:50:40 -04:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2018-12-18 00:33:36 -05:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2019-03-14 23:37:54 -04:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2020-09-22 00:50:40 -04:00
|
|
|
namespace Ryujinx.HLE.HOS.Services
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2020-09-22 00:50:40 -04:00
|
|
|
class ServerBase
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2020-09-22 00:50:40 -04:00
|
|
|
private struct IpcRequest
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2020-09-22 00:50:40 -04:00
|
|
|
public Switch Device { get; }
|
|
|
|
public KProcess Process => Thread?.Owner;
|
|
|
|
public KThread Thread { get; }
|
|
|
|
public KClientSession Session { get; }
|
|
|
|
public ulong MessagePtr { get; }
|
|
|
|
public ulong MessageSize { get; }
|
|
|
|
|
|
|
|
public IpcRequest(Switch device, KThread thread, KClientSession session, ulong messagePtr, ulong messageSize)
|
|
|
|
{
|
|
|
|
Device = device;
|
|
|
|
Thread = thread;
|
|
|
|
Session = session;
|
|
|
|
MessagePtr = messagePtr;
|
|
|
|
MessageSize = messageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SignalDone(KernelResult result)
|
|
|
|
{
|
|
|
|
Thread.ObjSyncResult = result;
|
|
|
|
Thread.Reschedule(ThreadSchedState.Running);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly AsyncWorkQueue<IpcRequest> _ipcProcessor;
|
|
|
|
|
|
|
|
public ServerBase(string name)
|
|
|
|
{
|
|
|
|
_ipcProcessor = new AsyncWorkQueue<IpcRequest>(Process, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PushMessage(Switch device, KThread thread, KClientSession session, ulong messagePtr, ulong messageSize)
|
|
|
|
{
|
|
|
|
_ipcProcessor.Add(new IpcRequest(device, thread, session, messagePtr, messageSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Process(IpcRequest message)
|
|
|
|
{
|
|
|
|
byte[] reqData = new byte[message.MessageSize];
|
|
|
|
|
|
|
|
message.Process.CpuMemory.Read(message.MessagePtr, reqData);
|
|
|
|
|
|
|
|
IpcMessage request = new IpcMessage(reqData, (long)message.MessagePtr);
|
2018-12-06 06:16:24 -05:00
|
|
|
IpcMessage response = new IpcMessage();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
using (MemoryStream raw = new MemoryStream(request.RawData))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
BinaryReader reqReader = new BinaryReader(raw);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (request.Type == IpcMessageType.Request ||
|
|
|
|
request.Type == IpcMessageType.RequestWithContext)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
response.Type = IpcMessageType.Response;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
using (MemoryStream resMs = new MemoryStream())
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
BinaryWriter resWriter = new BinaryWriter(resMs);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ServiceCtx context = new ServiceCtx(
|
2020-09-22 00:50:40 -04:00
|
|
|
message.Device,
|
|
|
|
message.Process,
|
|
|
|
message.Process.CpuMemory,
|
|
|
|
message.Thread,
|
|
|
|
message.Session,
|
2018-12-06 06:16:24 -05:00
|
|
|
request,
|
|
|
|
response,
|
|
|
|
reqReader,
|
|
|
|
resWriter);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2020-09-22 00:50:40 -04:00
|
|
|
message.Session.Service.CallMethod(context);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
response.RawData = resMs.ToArray();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (request.Type == IpcMessageType.Control ||
|
|
|
|
request.Type == IpcMessageType.ControlWithContext)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2020-01-30 15:43:47 -05:00
|
|
|
uint magic = (uint)reqReader.ReadUInt64();
|
|
|
|
uint cmdId = (uint)reqReader.ReadUInt64();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (cmdId)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
case 0:
|
2020-09-22 00:50:40 -04:00
|
|
|
request = FillResponse(response, 0, message.Session.Service.ConvertToDomain());
|
2018-03-12 00:04:52 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2019-10-31 00:50:12 -04:00
|
|
|
request = FillResponse(response, 0, 0x1000);
|
2018-03-12 00:04:52 -04:00
|
|
|
break;
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// TODO: Whats the difference between IpcDuplicateSession/Ex?
|
2018-04-04 20:01:36 -04:00
|
|
|
case 2:
|
2018-03-12 00:04:52 -04:00
|
|
|
case 4:
|
2018-12-06 06:16:24 -05:00
|
|
|
int unknown = reqReader.ReadInt32();
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2020-09-22 00:50:40 -04:00
|
|
|
if (message.Process.HandleTable.GenerateHandle(message.Session, out int handle) != KernelResult.Success)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
response.HandleDesc = IpcHandleDesc.MakeMove(handle);
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
request = FillResponse(response, 0);
|
2018-03-12 00:04:52 -04:00
|
|
|
|
|
|
|
break;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
default: throw new NotImplementedException(cmdId.ToString());
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (request.Type == IpcMessageType.CloseSession)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2020-09-22 00:50:40 -04:00
|
|
|
message.SignalDone(KernelResult.PortRemoteClosed);
|
|
|
|
return;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new NotImplementedException(request.Type.ToString());
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2020-09-22 00:50:40 -04:00
|
|
|
message.Process.CpuMemory.Write(message.MessagePtr, response.GetBytes((long)message.MessagePtr));
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 14:53:23 -04:00
|
|
|
|
2020-09-22 00:50:40 -04:00
|
|
|
message.SignalDone(KernelResult.Success);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private static IpcMessage FillResponse(IpcMessage response, long result, params int[] values)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
foreach (int value in values)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
writer.Write(value);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return FillResponse(response, result, ms.ToArray());
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private static IpcMessage FillResponse(IpcMessage response, long result, byte[] data = null)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
response.Type = IpcMessageType.Response;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
writer.Write(IpcMagic.Sfco);
|
|
|
|
writer.Write(result);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (data != null)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
writer.Write(data);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
response.RawData = ms.ToArray();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return response;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 00:50:40 -04:00
|
|
|
}
|