IpcService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.Reflection;
  8. using System.Linq;
  9. namespace Ryujinx.HLE.HOS.Services
  10. {
  11. abstract class IpcService
  12. {
  13. public IReadOnlyDictionary<int, MethodInfo> HipcCommands { 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. HipcCommands = 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(CommandHipcAttribute))
  26. .Select(command => (((CommandHipcAttribute)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 CallHipcMethod(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. for (int index = 0; index < inputObjCount; index++)
  64. {
  65. context.Request.ObjectIds.Add(context.RequestData.ReadInt32());
  66. }
  67. context.RequestData.BaseStream.Seek(0x10, SeekOrigin.Begin);
  68. if (domainCmd == 1)
  69. {
  70. service = GetObject(domainObjId);
  71. context.ResponseData.Write(0L);
  72. context.ResponseData.Write(0L);
  73. }
  74. else if (domainCmd == 2)
  75. {
  76. Delete(domainObjId);
  77. context.ResponseData.Write(0L);
  78. return;
  79. }
  80. else
  81. {
  82. throw new NotImplementedException($"Domain command: {domainCmd}");
  83. }
  84. }
  85. long sfciMagic = context.RequestData.ReadInt64();
  86. int commandId = (int)context.RequestData.ReadInt64();
  87. bool serviceExists = service.HipcCommands.TryGetValue(commandId, out MethodInfo processRequest);
  88. if (context.Device.Configuration.IgnoreMissingServices || serviceExists)
  89. {
  90. ResultCode result = ResultCode.Success;
  91. context.ResponseData.BaseStream.Seek(_isDomain ? 0x20 : 0x10, SeekOrigin.Begin);
  92. if (serviceExists)
  93. {
  94. Logger.Debug?.Print(LogClass.KernelIpc, $"{service.GetType().Name}: {processRequest.Name}");
  95. result = (ResultCode)processRequest.Invoke(service, new object[] { context });
  96. }
  97. else
  98. {
  99. string serviceName;
  100. DummyService dummyService = service as DummyService;
  101. serviceName = (dummyService == null) ? service.GetType().FullName : dummyService.ServiceName;
  102. Logger.Warning?.Print(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
  103. }
  104. if (_isDomain)
  105. {
  106. foreach (int id in context.Response.ObjectIds)
  107. {
  108. context.ResponseData.Write(id);
  109. }
  110. context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
  111. context.ResponseData.Write(context.Response.ObjectIds.Count);
  112. }
  113. context.ResponseData.BaseStream.Seek(_isDomain ? 0x10 : 0, SeekOrigin.Begin);
  114. context.ResponseData.Write(IpcMagic.Sfco);
  115. context.ResponseData.Write((long)result);
  116. }
  117. else
  118. {
  119. string dbgMessage = $"{service.GetType().FullName}: {commandId}";
  120. throw new ServiceNotImplementedException(service, context, dbgMessage, false);
  121. }
  122. }
  123. public void CallTipcMethod(ServiceCtx context)
  124. {
  125. int commandId = (int)context.Request.Type - 0x10;
  126. bool serviceExists = TipcCommands.TryGetValue(commandId, out MethodInfo processRequest);
  127. if (context.Device.Configuration.IgnoreMissingServices || serviceExists)
  128. {
  129. ResultCode result = ResultCode.Success;
  130. context.ResponseData.BaseStream.Seek(0x4, SeekOrigin.Begin);
  131. if (serviceExists)
  132. {
  133. Logger.Debug?.Print(LogClass.KernelIpc, $"{GetType().Name}: {processRequest.Name}");
  134. result = (ResultCode)processRequest.Invoke(this, new object[] { context });
  135. }
  136. else
  137. {
  138. string serviceName;
  139. DummyService dummyService = this as DummyService;
  140. serviceName = (dummyService == null) ? GetType().FullName : dummyService.ServiceName;
  141. Logger.Warning?.Print(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
  142. }
  143. context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
  144. context.ResponseData.Write((uint)result);
  145. }
  146. else
  147. {
  148. string dbgMessage = $"{GetType().FullName}: {commandId}";
  149. throw new ServiceNotImplementedException(this, context, dbgMessage, true);
  150. }
  151. }
  152. protected void MakeObject(ServiceCtx context, IpcService obj)
  153. {
  154. obj.TrySetServer(_parent.Server);
  155. if (_parent._isDomain)
  156. {
  157. obj._parent = _parent;
  158. context.Response.ObjectIds.Add(_parent.Add(obj));
  159. }
  160. else
  161. {
  162. context.Device.System.KernelContext.Syscall.CreateSession(false, 0, out int serverSessionHandle, out int clientSessionHandle);
  163. obj.Server.AddSessionObj(serverSessionHandle, obj);
  164. context.Response.HandleDesc = IpcHandleDesc.MakeMove(clientSessionHandle);
  165. }
  166. }
  167. protected T GetObject<T>(ServiceCtx context, int index) where T : IpcService
  168. {
  169. int objId = context.Request.ObjectIds[index];
  170. IpcService obj = _parent.GetObject(objId);
  171. return obj is T t ? t : null;
  172. }
  173. public bool TrySetServer(ServerBase newServer)
  174. {
  175. if (Server == null)
  176. {
  177. Server = newServer;
  178. return true;
  179. }
  180. return false;
  181. }
  182. private int Add(IpcService obj)
  183. {
  184. return _domainObjects.Add(obj);
  185. }
  186. private bool Delete(int id)
  187. {
  188. object obj = _domainObjects.Delete(id);
  189. if (obj is IDisposable disposableObj)
  190. {
  191. disposableObj.Dispose();
  192. }
  193. return obj != null;
  194. }
  195. private IpcService GetObject(int id)
  196. {
  197. return _domainObjects.GetData<IpcService>(id);
  198. }
  199. public void SetParent(IpcService parent)
  200. {
  201. _parent = parent._parent;
  202. }
  203. public virtual void DestroyAtExit()
  204. {
  205. foreach (object domainObject in _domainObjects.Values)
  206. {
  207. if (domainObject != this && domainObject is IDisposable disposableObj)
  208. {
  209. disposableObj.Dispose();
  210. }
  211. }
  212. _domainObjects.Clear();
  213. }
  214. }
  215. }