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-12-18 05:33:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
2018-03-19 18:58:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2019-04-26 04:53:10 +00:00
|
|
|
using Ryujinx.Profiler;
|
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
|
|
|
{
|
|
|
|
abstract class IpcService : IIpcService
|
|
|
|
{
|
|
|
|
public abstract IReadOnlyDictionary<int, ServiceProcessRequest> Commands { get; }
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private IdDictionary _domainObjects;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private int _selfId;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private bool _isDomain;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
|
|
|
public IpcService()
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_domainObjects = new IdDictionary();
|
2018-03-19 18:58:46 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void CallMethod(ServiceCtx context)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
IIpcService 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
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int domainCmd = (domainWord0 >> 0) & 0xff;
|
|
|
|
int inputObjCount = (domainWord0 >> 8) & 0xff;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
long sfciMagic = context.RequestData.ReadInt64();
|
|
|
|
int commandId = (int)context.RequestData.ReadInt64();
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2019-04-15 23:22:55 +00:00
|
|
|
bool serviceExists = service.Commands.TryGetValue(commandId, out ServiceProcessRequest processRequest);
|
|
|
|
|
|
|
|
if (ServiceConfiguration.IgnoreMissingServices || serviceExists)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2019-04-15 23:22:55 +00:00
|
|
|
long result = 0;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
Logger.PrintDebug(LogClass.KernelIpc, $"{service.GetType().Name}: {processRequest.Method.Name}");
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2019-04-26 04:53:10 +00:00
|
|
|
ProfileConfig profile = Profiles.ServiceCall;
|
|
|
|
profile.SessionGroup = service.GetType().Name;
|
|
|
|
profile.SessionItem = processRequest.Method.Name;
|
|
|
|
|
|
|
|
Profile.Begin(profile);
|
2019-04-15 23:22:55 +00:00
|
|
|
result = processRequest(context);
|
2019-04-26 04:53:10 +00:00
|
|
|
Profile.End(profile);
|
2019-04-15 23:22:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string serviceName;
|
|
|
|
DummyService dummyService = service as DummyService;
|
|
|
|
|
|
|
|
serviceName = (dummyService == null) ? service.GetType().FullName : dummyService.ServiceName;
|
|
|
|
|
|
|
|
Logger.PrintWarning(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
|
|
|
|
}
|
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);
|
|
|
|
context.ResponseData.Write(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
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
throw new ServiceNotImplementedException(context, dbgMessage);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
protected static void MakeObject(ServiceCtx context, IpcService obj)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
IpcService service = context.Session.Service;
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (service._isDomain)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Response.ObjectIds.Add(service.Add(obj));
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-18 22:26:39 +00:00
|
|
|
KSession session = new KSession(context.Device.System);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2019-01-18 22:26:39 +00:00
|
|
|
session.ClientSession.Service = obj;
|
|
|
|
|
|
|
|
if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
|
2018-09-23 18:11:46 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
protected static 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
|
|
|
IpcService service = context.Session.Service;
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (!service._isDomain)
|
2018-07-29 04:36:29 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int handle = context.Request.HandleDesc.ToMove[index];
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2019-01-18 22:26:39 +00:00
|
|
|
KClientSession session = context.Process.HandleTable.GetObject<KClientSession>(handle);
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return session?.Service is T ? (T)session.Service : null;
|
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
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
IIpcService obj = service.GetObject(objId);
|
2018-07-29 04:36:29 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return obj is T ? (T)obj : null;
|
2018-07-29 04:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private int Add(IIpcService 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
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private IIpcService GetObject(int id)
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return _domainObjects.GetData<IIpcService>(id);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 04:53:10 +00:00
|
|
|
}
|