Ryujinx/Ryujinx.HLE/HOS/Ipc/IpcHandler.cs

150 lines
4.7 KiB
C#
Raw Normal View History

using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Ipc;
using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.HOS.Kernel.Threading;
2018-02-05 00:08:20 +01:00
using System;
using System.IO;
namespace Ryujinx.HLE.HOS.Ipc
2018-02-05 00:08:20 +01:00
{
static class IpcHandler
{
public static KernelResult IpcCall(
Switch device,
KProcess process,
MemoryManager memory,
KThread thread,
KClientSession session,
IpcMessage request,
long cmdPtr)
2018-02-05 00:08:20 +01:00
{
IpcMessage response = new IpcMessage();
2018-02-05 00:08:20 +01:00
using (MemoryStream raw = new MemoryStream(request.RawData))
2018-02-05 00:08:20 +01:00
{
BinaryReader reqReader = new BinaryReader(raw);
2018-02-05 00:08:20 +01:00
if (request.Type == IpcMessageType.Request ||
request.Type == IpcMessageType.RequestWithContext)
2018-02-05 00:08:20 +01:00
{
response.Type = IpcMessageType.Response;
2018-02-05 00:08:20 +01:00
using (MemoryStream resMs = new MemoryStream())
2018-02-05 00:08:20 +01:00
{
BinaryWriter resWriter = new BinaryWriter(resMs);
2018-02-05 00:08:20 +01:00
ServiceCtx context = new ServiceCtx(
device,
process,
memory,
thread,
session,
request,
response,
reqReader,
resWriter);
2018-02-05 00:08:20 +01:00
session.Service.CallMethod(context);
2018-02-05 00:08:20 +01:00
response.RawData = resMs.ToArray();
2018-02-05 00:08:20 +01:00
}
}
else if (request.Type == IpcMessageType.Control ||
request.Type == IpcMessageType.ControlWithContext)
2018-02-05 00:08:20 +01:00
{
uint magic = (uint)reqReader.ReadUInt64();
uint cmdId = (uint)reqReader.ReadUInt64();
2018-02-05 00:08:20 +01:00
switch (cmdId)
2018-02-05 00:08:20 +01:00
{
case 0:
{
request = FillResponse(response, 0, session.Service.ConvertToDomain());
break;
}
case 3:
{
request = FillResponse(response, 0, 0x1000);
break;
}
// TODO: Whats the difference between IpcDuplicateSession/Ex?
case 2:
case 4:
{
int unknown = reqReader.ReadInt32();
if (process.HandleTable.GenerateHandle(session, out int handle) != KernelResult.Success)
{
throw new InvalidOperationException("Out of handles!");
}
response.HandleDesc = IpcHandleDesc.MakeMove(handle);
request = FillResponse(response, 0);
break;
}
2018-02-05 00:08:20 +01:00
default: throw new NotImplementedException(cmdId.ToString());
2018-02-05 00:08:20 +01:00
}
}
else if (request.Type == IpcMessageType.CloseSession)
2018-02-05 00:08:20 +01:00
{
// TODO
return KernelResult.PortRemoteClosed;
2018-02-05 00:08:20 +01:00
}
else
{
throw new NotImplementedException(request.Type.ToString());
2018-02-05 00:08:20 +01:00
}
memory.Write((ulong)cmdPtr, response.GetBytes(cmdPtr));
2018-02-05 00:08:20 +01:00
}
return KernelResult.Success;
2018-02-05 00:08:20 +01:00
}
private static IpcMessage FillResponse(IpcMessage response, long result, params int[] values)
2018-02-05 00:08:20 +01:00
{
using (MemoryStream ms = new MemoryStream())
2018-02-05 00:08:20 +01:00
{
BinaryWriter writer = new BinaryWriter(ms);
2018-02-05 00:08:20 +01:00
foreach (int value in values)
2018-02-05 00:08:20 +01:00
{
writer.Write(value);
2018-02-05 00:08:20 +01:00
}
return FillResponse(response, result, ms.ToArray());
2018-02-05 00:08:20 +01:00
}
}
private static IpcMessage FillResponse(IpcMessage response, long result, byte[] data = null)
2018-02-05 00:08:20 +01:00
{
response.Type = IpcMessageType.Response;
2018-02-05 00:08:20 +01:00
using (MemoryStream ms = new MemoryStream())
2018-02-05 00:08:20 +01:00
{
BinaryWriter writer = new BinaryWriter(ms);
2018-02-05 00:08:20 +01:00
writer.Write(IpcMagic.Sfco);
writer.Write(result);
2018-02-05 00:08:20 +01:00
if (data != null)
2018-02-05 00:08:20 +01:00
{
writer.Write(data);
2018-02-05 00:08:20 +01:00
}
response.RawData = ms.ToArray();
2018-02-05 00:08:20 +01:00
}
return response;
2018-02-05 00:08:20 +01:00
}
}
}