IpcService.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. namespace Ryujinx.HLE.HOS.Services
  10. {
  11. abstract class IpcService
  12. {
  13. public IReadOnlyDictionary<int, MethodInfo> CmifCommands { get; }
  14. public IReadOnlyDictionary<int, MethodInfo> TipcCommands { get; }
  15. public ServerBase Server { get; private set; }
  16. private IpcService _parent;
  17. private IdDictionary _domainObjects;
  18. private int _selfId;
  19. private bool _isDomain;
  20. public IpcService(ServerBase server = null)
  21. {
  22. CmifCommands = Assembly.GetExecutingAssembly().GetTypes()
  23. .Where(type => type == GetType())
  24. .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
  25. .SelectMany(methodInfo => methodInfo.GetCustomAttributes(typeof(CommandCmifAttribute))
  26. .Select(command => (((CommandCmifAttribute)command).Id, methodInfo)))
  27. .ToDictionary(command => command.Id, command => command.methodInfo);
  28. TipcCommands = Assembly.GetExecutingAssembly().GetTypes()
  29. .Where(type => type == GetType())
  30. .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
  31. .SelectMany(methodInfo => methodInfo.GetCustomAttributes(typeof(CommandTipcAttribute))
  32. .Select(command => (((CommandTipcAttribute)command).Id, methodInfo)))
  33. .ToDictionary(command => command.Id, command => command.methodInfo);
  34. Server = server;
  35. _parent = this;
  36. _domainObjects = new IdDictionary();
  37. _selfId = -1;
  38. }
  39. public int ConvertToDomain()
  40. {
  41. if (_selfId == -1)
  42. {
  43. _selfId = _domainObjects.Add(this);
  44. }
  45. _isDomain = true;
  46. return _selfId;
  47. }
  48. public void ConvertToSession()
  49. {
  50. _isDomain = false;
  51. }
  52. public void CallCmifMethod(ServiceCtx context)
  53. {
  54. IpcService service = this;
  55. if (_isDomain)
  56. {
  57. int domainWord0 = context.RequestData.ReadInt32();
  58. int domainObjId = context.RequestData.ReadInt32();
  59. int domainCmd = (domainWord0 >> 0) & 0xff;
  60. int inputObjCount = (domainWord0 >> 8) & 0xff;
  61. int dataPayloadSize = (domainWord0 >> 16) & 0xffff;
  62. context.RequestData.BaseStream.Seek(0x10 + dataPayloadSize, SeekOrigin.Begin);
  63. context.Request.ObjectIds.EnsureCapacity(inputObjCount);
  64. for (int index = 0; index < inputObjCount; index++)
  65. {
  66. context.Request.ObjectIds.Add(context.RequestData.ReadInt32());
  67. }
  68. context.RequestData.BaseStream.Seek(0x10, SeekOrigin.Begin);
  69. if (domainCmd == 1)
  70. {
  71. service = GetObject(domainObjId);
  72. context.ResponseData.Write(0L);
  73. context.ResponseData.Write(0L);
  74. }
  75. else if (domainCmd == 2)
  76. {
  77. Delete(domainObjId);
  78. context.ResponseData.Write(0L);
  79. return;
  80. }
  81. else
  82. {
  83. throw new NotImplementedException($"Domain command: {domainCmd}");
  84. }
  85. }
  86. long sfciMagic = context.RequestData.ReadInt64();
  87. int commandId = (int)context.RequestData.ReadInt64();
  88. bool serviceExists = service.CmifCommands.TryGetValue(commandId, out MethodInfo processRequest);
  89. if (context.Device.Configuration.IgnoreMissingServices || serviceExists)
  90. {
  91. ResultCode result = ResultCode.Success;
  92. context.ResponseData.BaseStream.Seek(_isDomain ? 0x20 : 0x10, SeekOrigin.Begin);
  93. if (serviceExists)
  94. {
  95. Logger.Trace?.Print(LogClass.KernelIpc, $"{service.GetType().Name}: {processRequest.Name}");
  96. result = (ResultCode)processRequest.Invoke(service, new object[] { context });
  97. }
  98. else
  99. {
  100. string serviceName;
  101. DummyService dummyService = service as DummyService;
  102. serviceName = (dummyService == null) ? service.GetType().FullName : dummyService.ServiceName;
  103. Logger.Warning?.Print(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
  104. }
  105. if (_isDomain)
  106. {
  107. foreach (int id in context.Response.ObjectIds)
  108. {
  109. context.ResponseData.Write(id);
  110. }
  111. context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
  112. context.ResponseData.Write(context.Response.ObjectIds.Count);
  113. }
  114. context.ResponseData.BaseStream.Seek(_isDomain ? 0x10 : 0, SeekOrigin.Begin);
  115. context.ResponseData.Write(IpcMagic.Sfco);
  116. context.ResponseData.Write((long)result);
  117. }
  118. else
  119. {
  120. string dbgMessage = $"{service.GetType().FullName}: {commandId}";
  121. throw new ServiceNotImplementedException(service, context, dbgMessage);
  122. }
  123. }
  124. public void CallTipcMethod(ServiceCtx context)
  125. {
  126. int commandId = (int)context.Request.Type - 0x10;
  127. bool serviceExists = TipcCommands.TryGetValue(commandId, out MethodInfo processRequest);
  128. if (context.Device.Configuration.IgnoreMissingServices || serviceExists)
  129. {
  130. ResultCode result = ResultCode.Success;
  131. context.ResponseData.BaseStream.Seek(0x4, SeekOrigin.Begin);
  132. if (serviceExists)
  133. {
  134. Logger.Debug?.Print(LogClass.KernelIpc, $"{GetType().Name}: {processRequest.Name}");
  135. result = (ResultCode)processRequest.Invoke(this, new object[] { context });
  136. }
  137. else
  138. {
  139. string serviceName;
  140. DummyService dummyService = this as DummyService;
  141. serviceName = (dummyService == null) ? GetType().FullName : dummyService.ServiceName;
  142. Logger.Warning?.Print(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
  143. }
  144. context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
  145. context.ResponseData.Write((uint)result);
  146. }
  147. else
  148. {
  149. string dbgMessage = $"{GetType().FullName}: {commandId}";
  150. throw new ServiceNotImplementedException(this, context, dbgMessage);
  151. }
  152. }
  153. protected void MakeObject(ServiceCtx context, IpcService obj)
  154. {
  155. obj.TrySetServer(_parent.Server);
  156. if (_parent._isDomain)
  157. {
  158. obj._parent = _parent;
  159. context.Response.ObjectIds.Add(_parent.Add(obj));
  160. }
  161. else
  162. {
  163. context.Device.System.KernelContext.Syscall.CreateSession(out int serverSessionHandle, out int clientSessionHandle, false, 0);
  164. obj.Server.AddSessionObj(serverSessionHandle, obj);
  165. context.Response.HandleDesc = IpcHandleDesc.MakeMove(clientSessionHandle);
  166. }
  167. }
  168. protected T GetObject<T>(ServiceCtx context, int index) where T : IpcService
  169. {
  170. int objId = context.Request.ObjectIds[index];
  171. IpcService obj = _parent.GetObject(objId);
  172. return obj is T t ? t : null;
  173. }
  174. public bool TrySetServer(ServerBase newServer)
  175. {
  176. if (Server == null)
  177. {
  178. Server = newServer;
  179. return true;
  180. }
  181. return false;
  182. }
  183. private int Add(IpcService obj)
  184. {
  185. return _domainObjects.Add(obj);
  186. }
  187. private bool Delete(int id)
  188. {
  189. object obj = _domainObjects.Delete(id);
  190. if (obj is IDisposable disposableObj)
  191. {
  192. disposableObj.Dispose();
  193. }
  194. return obj != null;
  195. }
  196. private IpcService GetObject(int id)
  197. {
  198. return _domainObjects.GetData<IpcService>(id);
  199. }
  200. public void SetParent(IpcService parent)
  201. {
  202. _parent = parent._parent;
  203. }
  204. public virtual void DestroyAtExit()
  205. {
  206. foreach (object domainObject in _domainObjects.Values)
  207. {
  208. if (domainObject != this && domainObject is IDisposable disposableObj)
  209. {
  210. disposableObj.Dispose();
  211. }
  212. }
  213. _domainObjects.Clear();
  214. }
  215. }
  216. }