2018-10-17 17:15:50 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-01-11 00:11:46 +00:00
|
|
|
using Ryujinx.HLE.Exceptions;
|
2018-08-16 23:47:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2018-03-19 18:58:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2019-07-12 01:13:43 +00:00
|
|
|
using System.Linq;
|
2023-03-04 13:43:08 +00:00
|
|
|
using System.Reflection;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2021-03-18 23:31:08 +00:00
|
|
|
abstract class IpcService
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2021-04-13 22:01:24 +00:00
|
|
|
public IReadOnlyDictionary<int, MethodInfo> HipcCommands { get; }
|
|
|
|
public IReadOnlyDictionary<int, MethodInfo> TipcCommands { get; }
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-09-22 04:50:40 +00:00
|
|
|
public ServerBase Server { get; private set; }
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
private IpcService _parent;
|
2020-09-22 04:50:40 +00:00
|
|
|
private IdDictionary _domainObjects;
|
2018-12-06 11:16:24 +00:00
|
|
|
private int _selfId;
|
|
|
|
private bool _isDomain;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-09-22 04:50:40 +00:00
|
|
|
public IpcService(ServerBase server = null)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2021-04-13 22:01:24 +00:00
|
|
|
HipcCommands = Assembly.GetExecutingAssembly().GetTypes()
|
2019-07-12 01:13:43 +00:00
|
|
|
.Where(type => type == GetType())
|
2019-07-14 21:30:47 +00:00
|
|
|
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
|
2021-04-13 22:01:24 +00:00
|
|
|
.SelectMany(methodInfo => methodInfo.GetCustomAttributes(typeof(CommandHipcAttribute))
|
|
|
|
.Select(command => (((CommandHipcAttribute)command).Id, methodInfo)))
|
|
|
|
.ToDictionary(command => command.Id, command => command.methodInfo);
|
|
|
|
|
|
|
|
TipcCommands = Assembly.GetExecutingAssembly().GetTypes()
|
|
|
|
.Where(type => type == GetType())
|
|
|
|
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
|
|
|
|
.SelectMany(methodInfo => methodInfo.GetCustomAttributes(typeof(CommandTipcAttribute))
|
|
|
|
.Select(command => (((CommandTipcAttribute)command).Id, methodInfo)))
|
2019-07-12 01:13:43 +00:00
|
|
|
.ToDictionary(command => command.Id, command => command.methodInfo);
|
|
|
|
|
2020-09-22 04:50:40 +00:00
|
|
|
Server = server;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
_parent = this;
|
2020-09-22 04:50:40 +00:00
|
|
|
_domainObjects = new IdDictionary();
|
2018-12-06 11:16:24 +00:00
|
|
|
_selfId = -1;
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int ConvertToDomain()
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (_selfId == -1)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_selfId = _domainObjects.Add(this);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_isDomain = true;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return _selfId;
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ConvertToSession()
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_isDomain = false;
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
public void CallHipcMethod(ServiceCtx context)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2021-03-18 23:31:08 +00:00
|
|
|
IpcService service = this;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (_isDomain)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int domainWord0 = context.RequestData.ReadInt32();
|
|
|
|
int domainObjId = context.RequestData.ReadInt32();
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
int domainCmd = (domainWord0 >> 0) & 0xff;
|
|
|
|
int inputObjCount = (domainWord0 >> 8) & 0xff;
|
2018-12-06 11:16:24 +00:00
|
|
|
int dataPayloadSize = (domainWord0 >> 16) & 0xffff;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.RequestData.BaseStream.Seek(0x10 + dataPayloadSize, SeekOrigin.Begin);
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int index = 0; index < inputObjCount; index++)
|
2018-07-29 04:36:29 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Request.ObjectIds.Add(context.RequestData.ReadInt32());
|
2018-07-29 04:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.RequestData.BaseStream.Seek(0x10, SeekOrigin.Begin);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (domainCmd == 1)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
service = GetObject(domainObjId);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(0L);
|
|
|
|
context.ResponseData.Write(0L);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
else if (domainCmd == 2)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Delete(domainObjId);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(0L);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
throw new NotImplementedException($"Domain command: {domainCmd}");
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
long sfciMagic = context.RequestData.ReadInt64();
|
|
|
|
int commandId = (int)context.RequestData.ReadInt64();
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
bool serviceExists = service.HipcCommands.TryGetValue(commandId, out MethodInfo processRequest);
|
2019-04-15 23:22:55 +00:00
|
|
|
|
2021-05-16 15:12:14 +00:00
|
|
|
if (context.Device.Configuration.IgnoreMissingServices || serviceExists)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2019-07-14 19:04:38 +00:00
|
|
|
ResultCode result = ResultCode.Success;
|
2019-04-15 23:22:55 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.BaseStream.Seek(_isDomain ? 0x20 : 0x10, SeekOrigin.Begin);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2019-04-15 23:22:55 +00:00
|
|
|
if (serviceExists)
|
|
|
|
{
|
2022-02-18 00:08:07 +00:00
|
|
|
Logger.Trace?.Print(LogClass.KernelIpc, $"{service.GetType().Name}: {processRequest.Name}");
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
result = (ResultCode)processRequest.Invoke(service, new object[] { context });
|
2019-04-15 23:22:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string serviceName;
|
2019-07-12 01:13:43 +00:00
|
|
|
|
2019-04-15 23:22:55 +00:00
|
|
|
DummyService dummyService = service as DummyService;
|
|
|
|
|
|
|
|
serviceName = (dummyService == null) ? service.GetType().FullName : dummyService.ServiceName;
|
|
|
|
|
2020-08-03 23:32:53 +00:00
|
|
|
Logger.Warning?.Print(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
|
2019-04-15 23:22:55 +00:00
|
|
|
}
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (_isDomain)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
foreach (int id in context.Response.ObjectIds)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(id);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(context.Response.ObjectIds.Count);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.BaseStream.Seek(_isDomain ? 0x10 : 0, SeekOrigin.Begin);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(IpcMagic.Sfco);
|
2019-07-14 19:04:38 +00:00
|
|
|
context.ResponseData.Write((long)result);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-18 22:26:39 +00:00
|
|
|
string dbgMessage = $"{service.GetType().FullName}: {commandId}";
|
2018-04-06 05:38:59 +00:00
|
|
|
|
2021-08-11 19:16:42 +00:00
|
|
|
throw new ServiceNotImplementedException(service, context, dbgMessage);
|
2021-04-13 22:01:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CallTipcMethod(ServiceCtx context)
|
|
|
|
{
|
|
|
|
int commandId = (int)context.Request.Type - 0x10;
|
|
|
|
|
|
|
|
bool serviceExists = TipcCommands.TryGetValue(commandId, out MethodInfo processRequest);
|
|
|
|
|
2021-05-16 15:12:14 +00:00
|
|
|
if (context.Device.Configuration.IgnoreMissingServices || serviceExists)
|
2021-04-13 22:01:24 +00:00
|
|
|
{
|
|
|
|
ResultCode result = ResultCode.Success;
|
|
|
|
|
|
|
|
context.ResponseData.BaseStream.Seek(0x4, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
if (serviceExists)
|
|
|
|
{
|
|
|
|
Logger.Debug?.Print(LogClass.KernelIpc, $"{GetType().Name}: {processRequest.Name}");
|
|
|
|
|
|
|
|
result = (ResultCode)processRequest.Invoke(this, new object[] { context });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string serviceName;
|
|
|
|
|
|
|
|
DummyService dummyService = this as DummyService;
|
|
|
|
|
|
|
|
serviceName = (dummyService == null) ? GetType().FullName : dummyService.ServiceName;
|
|
|
|
|
|
|
|
Logger.Warning?.Print(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
context.ResponseData.Write((uint)result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string dbgMessage = $"{GetType().FullName}: {commandId}";
|
|
|
|
|
2021-08-11 19:16:42 +00:00
|
|
|
throw new ServiceNotImplementedException(this, context, dbgMessage);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
protected void MakeObject(ServiceCtx context, IpcService obj)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2020-12-01 23:23:43 +00:00
|
|
|
obj.TrySetServer(_parent.Server);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
if (_parent._isDomain)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2020-12-01 23:23:43 +00:00
|
|
|
obj._parent = _parent;
|
|
|
|
|
|
|
|
context.Response.ObjectIds.Add(_parent.Add(obj));
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-29 21:18:03 +00:00
|
|
|
context.Device.System.KernelContext.Syscall.CreateSession(out int serverSessionHandle, out int clientSessionHandle, false, 0);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
obj.Server.AddSessionObj(serverSessionHandle, obj);
|
2020-07-20 02:23:26 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeMove(clientSessionHandle);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
protected T GetObject<T>(ServiceCtx context, int index) where T : IpcService
|
2018-07-29 04:36:29 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int objId = context.Request.ObjectIds[index];
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2021-03-18 23:31:08 +00:00
|
|
|
IpcService obj = _parent.GetObject(objId);
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
return obj is T t ? t : null;
|
2018-07-29 04:36:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 04:50:40 +00:00
|
|
|
public bool TrySetServer(ServerBase newServer)
|
|
|
|
{
|
|
|
|
if (Server == null)
|
|
|
|
{
|
|
|
|
Server = newServer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-18 23:31:08 +00:00
|
|
|
private int Add(IpcService obj)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return _domainObjects.Add(obj);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private bool Delete(int id)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
object obj = _domainObjects.Delete(id);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (obj is IDisposable disposableObj)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
disposableObj.Dispose();
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return obj != null;
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 23:31:08 +00:00
|
|
|
private IpcService GetObject(int id)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2021-03-18 23:31:08 +00:00
|
|
|
return _domainObjects.GetData<IpcService>(id);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
2020-12-01 23:23:43 +00:00
|
|
|
|
|
|
|
public void SetParent(IpcService parent)
|
|
|
|
{
|
|
|
|
_parent = parent._parent;
|
|
|
|
}
|
2021-06-29 17:37:13 +00:00
|
|
|
|
|
|
|
public virtual void DestroyAtExit()
|
|
|
|
{
|
|
|
|
foreach (object domainObject in _domainObjects.Values)
|
|
|
|
{
|
|
|
|
if (domainObject != this && domainObject is IDisposable disposableObj)
|
|
|
|
{
|
|
|
|
disposableObj.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_domainObjects.Clear();
|
|
|
|
}
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
2019-04-26 04:53:10 +00:00
|
|
|
}
|