IUserInterface.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Ipc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. namespace Ryujinx.HLE.HOS.Services.Sm
  12. {
  13. class IUserInterface : IpcService
  14. {
  15. private static Dictionary<string, Type> _services;
  16. private readonly SmRegistry _registry;
  17. private readonly ServerBase _commonServer;
  18. private bool _isInitialized;
  19. public IUserInterface(KernelContext context, SmRegistry registry)
  20. {
  21. _commonServer = new ServerBase(context, "CommonServer");
  22. _registry = registry;
  23. }
  24. static IUserInterface()
  25. {
  26. _services = Assembly.GetExecutingAssembly().GetTypes()
  27. .SelectMany(type => type.GetCustomAttributes(typeof(ServiceAttribute), true)
  28. .Select(service => (((ServiceAttribute)service).Name, type)))
  29. .ToDictionary(service => service.Name, service => service.type);
  30. }
  31. [CommandHipc(0)]
  32. [CommandTipc(0)] // 12.0.0+
  33. // Initialize(pid, u64 reserved)
  34. public ResultCode Initialize(ServiceCtx context)
  35. {
  36. _isInitialized = true;
  37. return ResultCode.Success;
  38. }
  39. [CommandTipc(1)] // 12.0.0+
  40. // GetService(ServiceName name) -> handle<move, session>
  41. public ResultCode GetServiceTipc(ServiceCtx context)
  42. {
  43. context.Response.HandleDesc = IpcHandleDesc.MakeMove(0);
  44. return GetService(context);
  45. }
  46. [CommandHipc(1)]
  47. public ResultCode GetService(ServiceCtx context)
  48. {
  49. if (!_isInitialized)
  50. {
  51. return ResultCode.NotInitialized;
  52. }
  53. string name = ReadName(context);
  54. if (name == string.Empty)
  55. {
  56. return ResultCode.InvalidName;
  57. }
  58. KSession session = new KSession(context.Device.System.KernelContext);
  59. if (_registry.TryGetService(name, out KPort port))
  60. {
  61. KernelResult result = port.EnqueueIncomingSession(session.ServerSession);
  62. if (result != KernelResult.Success)
  63. {
  64. throw new InvalidOperationException($"Session enqueue on port returned error \"{result}\".");
  65. }
  66. if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
  67. {
  68. throw new InvalidOperationException("Out of handles!");
  69. }
  70. session.ClientSession.DecrementReferenceCount();
  71. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  72. }
  73. else
  74. {
  75. if (_services.TryGetValue(name, out Type type))
  76. {
  77. ServiceAttribute serviceAttribute = (ServiceAttribute)type.GetCustomAttributes(typeof(ServiceAttribute)).First(service => ((ServiceAttribute)service).Name == name);
  78. IpcService service = serviceAttribute.Parameter != null
  79. ? (IpcService)Activator.CreateInstance(type, context, serviceAttribute.Parameter)
  80. : (IpcService)Activator.CreateInstance(type, context);
  81. service.TrySetServer(_commonServer);
  82. service.Server.AddSessionObj(session.ServerSession, service);
  83. }
  84. else
  85. {
  86. if (context.Device.Configuration.IgnoreMissingServices)
  87. {
  88. Logger.Warning?.Print(LogClass.Service, $"Missing service {name} ignored");
  89. }
  90. else
  91. {
  92. throw new NotImplementedException(name);
  93. }
  94. }
  95. if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
  96. {
  97. throw new InvalidOperationException("Out of handles!");
  98. }
  99. session.ServerSession.DecrementReferenceCount();
  100. session.ClientSession.DecrementReferenceCount();
  101. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  102. }
  103. return ResultCode.Success;
  104. }
  105. [CommandHipc(2)]
  106. // RegisterService(ServiceName name, u8 isLight, u32 maxHandles) -> handle<move, port>
  107. public ResultCode RegisterServiceHipc(ServiceCtx context)
  108. {
  109. if (!_isInitialized)
  110. {
  111. return ResultCode.NotInitialized;
  112. }
  113. long namePosition = context.RequestData.BaseStream.Position;
  114. string name = ReadName(context);
  115. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  116. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  117. int maxSessions = context.RequestData.ReadInt32();
  118. return RegisterService(context, name, isLight, maxSessions);
  119. }
  120. [CommandTipc(2)] // 12.0.0+
  121. // RegisterService(ServiceName name, u32 maxHandles, u8 isLight) -> handle<move, port>
  122. public ResultCode RegisterServiceTipc(ServiceCtx context)
  123. {
  124. if (!_isInitialized)
  125. {
  126. context.Response.HandleDesc = IpcHandleDesc.MakeMove(0);
  127. return ResultCode.NotInitialized;
  128. }
  129. long namePosition = context.RequestData.BaseStream.Position;
  130. string name = ReadName(context);
  131. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  132. int maxSessions = context.RequestData.ReadInt32();
  133. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  134. return RegisterService(context, name, isLight, maxSessions);
  135. }
  136. private ResultCode RegisterService(ServiceCtx context, string name, bool isLight, int maxSessions)
  137. {
  138. if (string.IsNullOrEmpty(name))
  139. {
  140. return ResultCode.InvalidName;
  141. }
  142. Logger.Info?.Print(LogClass.ServiceSm, $"Register \"{name}\".");
  143. KPort port = new KPort(context.Device.System.KernelContext, maxSessions, isLight, 0);
  144. if (!_registry.TryRegister(name, port))
  145. {
  146. return ResultCode.AlreadyRegistered;
  147. }
  148. if (context.Process.HandleTable.GenerateHandle(port.ServerPort, out int handle) != KernelResult.Success)
  149. {
  150. throw new InvalidOperationException("Out of handles!");
  151. }
  152. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  153. return ResultCode.Success;
  154. }
  155. [CommandHipc(3)]
  156. [CommandTipc(3)] // 12.0.0+
  157. // UnregisterService(ServiceName name)
  158. public ResultCode UnregisterService(ServiceCtx context)
  159. {
  160. if (!_isInitialized)
  161. {
  162. return ResultCode.NotInitialized;
  163. }
  164. long namePosition = context.RequestData.BaseStream.Position;
  165. string name = ReadName(context);
  166. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  167. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  168. int maxSessions = context.RequestData.ReadInt32();
  169. if (string.IsNullOrEmpty(name))
  170. {
  171. return ResultCode.InvalidName;
  172. }
  173. if (!_registry.Unregister(name))
  174. {
  175. return ResultCode.NotRegistered;
  176. }
  177. return ResultCode.Success;
  178. }
  179. private static string ReadName(ServiceCtx context)
  180. {
  181. string name = string.Empty;
  182. for (int index = 0; index < 8 &&
  183. context.RequestData.BaseStream.Position <
  184. context.RequestData.BaseStream.Length; index++)
  185. {
  186. byte chr = context.RequestData.ReadByte();
  187. if (chr >= 0x20 && chr < 0x7f)
  188. {
  189. name += (char)chr;
  190. }
  191. }
  192. return name;
  193. }
  194. public override void DestroyAtExit()
  195. {
  196. _commonServer.Dispose();
  197. base.DestroyAtExit();
  198. }
  199. }
  200. }