2018-10-17 19:15:50 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-17 01:47:36 +02:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-03-19 19:58:46 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services
|
2018-03-19 19:58:46 +01:00
|
|
|
{
|
|
|
|
abstract class IpcService : IIpcService
|
|
|
|
{
|
|
|
|
public abstract IReadOnlyDictionary<int, ServiceProcessRequest> Commands { get; }
|
|
|
|
|
|
|
|
private IdDictionary DomainObjects;
|
|
|
|
|
|
|
|
private int SelfId;
|
|
|
|
|
2018-04-05 00:29:34 +02:00
|
|
|
private bool IsDomain;
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
public IpcService()
|
|
|
|
{
|
|
|
|
DomainObjects = new IdDictionary();
|
|
|
|
|
|
|
|
SelfId = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int ConvertToDomain()
|
|
|
|
{
|
|
|
|
if (SelfId == -1)
|
|
|
|
{
|
|
|
|
SelfId = DomainObjects.Add(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
IsDomain = true;
|
|
|
|
|
|
|
|
return SelfId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ConvertToSession()
|
|
|
|
{
|
|
|
|
IsDomain = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CallMethod(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
IIpcService Service = this;
|
|
|
|
|
|
|
|
if (IsDomain)
|
|
|
|
{
|
|
|
|
int DomainWord0 = Context.RequestData.ReadInt32();
|
|
|
|
int DomainObjId = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-07-29 06:36:29 +02:00
|
|
|
int DomainCmd = (DomainWord0 >> 0) & 0xff;
|
|
|
|
int InputObjCount = (DomainWord0 >> 8) & 0xff;
|
|
|
|
int DataPayloadSize = (DomainWord0 >> 16) & 0xffff;
|
2018-03-19 19:58:46 +01:00
|
|
|
|
2018-07-29 06:36:29 +02:00
|
|
|
Context.RequestData.BaseStream.Seek(0x10 + DataPayloadSize, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
for (int Index = 0; Index < InputObjCount; Index++)
|
|
|
|
{
|
|
|
|
Context.Request.ObjectIds.Add(Context.RequestData.ReadInt32());
|
|
|
|
}
|
|
|
|
|
|
|
|
Context.RequestData.BaseStream.Seek(0x10, SeekOrigin.Begin);
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
if (DomainCmd == 1)
|
|
|
|
{
|
|
|
|
Service = GetObject(DomainObjId);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0L);
|
|
|
|
Context.ResponseData.Write(0L);
|
|
|
|
}
|
|
|
|
else if (DomainCmd == 2)
|
|
|
|
{
|
|
|
|
Delete(DomainObjId);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0L);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new NotImplementedException($"Domain command: {DomainCmd}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long SfciMagic = Context.RequestData.ReadInt64();
|
|
|
|
int CommandId = (int)Context.RequestData.ReadInt64();
|
|
|
|
|
|
|
|
if (Service.Commands.TryGetValue(CommandId, out ServiceProcessRequest ProcessRequest))
|
|
|
|
{
|
|
|
|
Context.ResponseData.BaseStream.Seek(IsDomain ? 0x20 : 0x10, SeekOrigin.Begin);
|
|
|
|
|
2018-10-17 19:15:50 +02:00
|
|
|
Logger.PrintDebug(LogClass.KernelIpc, $"{Service.GetType().Name}: {ProcessRequest.Method.Name}");
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
long Result = ProcessRequest(Context);
|
|
|
|
|
|
|
|
if (IsDomain)
|
|
|
|
{
|
2018-07-29 06:36:29 +02:00
|
|
|
foreach (int Id in Context.Response.ObjectIds)
|
2018-03-19 19:58:46 +01:00
|
|
|
{
|
|
|
|
Context.ResponseData.Write(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
2018-07-29 06:36:29 +02:00
|
|
|
Context.ResponseData.Write(Context.Response.ObjectIds.Count);
|
2018-03-19 19:58:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Context.ResponseData.BaseStream.Seek(IsDomain ? 0x10 : 0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(IpcMagic.Sfco);
|
|
|
|
Context.ResponseData.Write(Result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-06 07:38:59 +02:00
|
|
|
string DbgMessage = $"{Context.Session.ServiceName} {Service.GetType().Name}: {CommandId}";
|
|
|
|
|
|
|
|
throw new NotImplementedException(DbgMessage);
|
2018-03-19 19:58:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static void MakeObject(ServiceCtx Context, IpcService Obj)
|
|
|
|
{
|
|
|
|
IpcService Service = Context.Session.Service;
|
|
|
|
|
|
|
|
if (Service.IsDomain)
|
|
|
|
{
|
2018-07-29 06:36:29 +02:00
|
|
|
Context.Response.ObjectIds.Add(Service.Add(Obj));
|
2018-03-19 19:58:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-06 07:38:59 +02:00
|
|
|
KSession Session = new KSession(Obj, Context.Session.ServiceName);
|
2018-03-19 19:58:46 +01:00
|
|
|
|
2018-09-23 20:11:46 +02:00
|
|
|
if (Context.Process.HandleTable.GenerateHandle(Session, out int Handle) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
Context.Response.HandleDesc = IpcHandleDesc.MakeMove(Handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 06:36:29 +02:00
|
|
|
protected static T GetObject<T>(ServiceCtx Context, int Index) where T : IpcService
|
|
|
|
{
|
|
|
|
IpcService Service = Context.Session.Service;
|
|
|
|
|
|
|
|
if (!Service.IsDomain)
|
|
|
|
{
|
|
|
|
int Handle = Context.Request.HandleDesc.ToMove[Index];
|
|
|
|
|
2018-09-23 20:11:46 +02:00
|
|
|
KSession Session = Context.Process.HandleTable.GetObject<KSession>(Handle);
|
2018-07-29 06:36:29 +02:00
|
|
|
|
|
|
|
return Session?.Service is T ? (T)Session.Service : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ObjId = Context.Request.ObjectIds[Index];
|
|
|
|
|
|
|
|
IIpcService Obj = Service.GetObject(ObjId);
|
|
|
|
|
|
|
|
return Obj is T ? (T)Obj : null;
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
private int Add(IIpcService Obj)
|
|
|
|
{
|
|
|
|
return DomainObjects.Add(Obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool Delete(int Id)
|
|
|
|
{
|
|
|
|
object Obj = DomainObjects.Delete(Id);
|
|
|
|
|
|
|
|
if (Obj is IDisposable DisposableObj)
|
|
|
|
{
|
|
|
|
DisposableObj.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Obj != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IIpcService GetObject(int Id)
|
|
|
|
{
|
|
|
|
return DomainObjects.GetData<IIpcService>(Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|