IUserInterface.cs 8.0 KB

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