IUserInterface.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Ipc;
  5. using System;
  6. using System.Collections.Concurrent;
  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. [Service("sm:")]
  14. class IUserInterface : IpcService
  15. {
  16. private Dictionary<string, Type> _services;
  17. private ConcurrentDictionary<string, KPort> _registeredServices;
  18. private bool _isInitialized;
  19. public IUserInterface(ServiceCtx context = null)
  20. {
  21. _registeredServices = new ConcurrentDictionary<string, KPort>();
  22. _services = Assembly.GetExecutingAssembly().GetTypes()
  23. .SelectMany(type => type.GetCustomAttributes(typeof(ServiceAttribute), true)
  24. .Select(service => (((ServiceAttribute)service).Name, type)))
  25. .ToDictionary(service => service.Name, service => service.type);
  26. }
  27. public static void InitializePort(Horizon system)
  28. {
  29. KPort port = new KPort(system, 256, false, 0);
  30. port.ClientPort.SetName("sm:");
  31. port.ClientPort.Service = new IUserInterface();
  32. }
  33. [Command(0)]
  34. // Initialize(pid, u64 reserved)
  35. public ResultCode Initialize(ServiceCtx context)
  36. {
  37. _isInitialized = true;
  38. return ResultCode.Success;
  39. }
  40. [Command(1)]
  41. // GetService(ServiceName name) -> handle<move, session>
  42. public ResultCode GetService(ServiceCtx context)
  43. {
  44. if (!_isInitialized)
  45. {
  46. return ResultCode.NotInitialized;
  47. }
  48. string name = ReadName(context);
  49. if (name == string.Empty)
  50. {
  51. return ResultCode.InvalidName;
  52. }
  53. KSession session = new KSession(context.Device.System);
  54. if (_registeredServices.TryGetValue(name, out KPort port))
  55. {
  56. KernelResult result = port.EnqueueIncomingSession(session.ServerSession);
  57. if (result != KernelResult.Success)
  58. {
  59. throw new InvalidOperationException($"Session enqueue on port returned error \"{result}\".");
  60. }
  61. }
  62. else
  63. {
  64. if (_services.TryGetValue(name, out Type type))
  65. {
  66. ServiceAttribute serviceAttribute = (ServiceAttribute)type.GetCustomAttributes(typeof(ServiceAttribute)).First(service => ((ServiceAttribute)service).Name == name);
  67. session.ClientSession.Service = serviceAttribute.Parameter != null ? (IpcService)Activator.CreateInstance(type, context, serviceAttribute.Parameter)
  68. : (IpcService)Activator.CreateInstance(type, context);
  69. }
  70. else
  71. {
  72. if (ServiceConfiguration.IgnoreMissingServices)
  73. {
  74. Logger.PrintWarning(LogClass.Service, $"Missing service {name} ignored");
  75. session.ClientSession.Service = new DummyService(name);
  76. }
  77. else
  78. {
  79. throw new NotImplementedException(name);
  80. }
  81. }
  82. }
  83. if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
  84. {
  85. throw new InvalidOperationException("Out of handles!");
  86. }
  87. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  88. return ResultCode.Success;
  89. }
  90. [Command(2)]
  91. // RegisterService(ServiceName name, u8, u32 maxHandles) -> handle<move, port>
  92. public ResultCode RegisterService(ServiceCtx context)
  93. {
  94. if (!_isInitialized)
  95. {
  96. return ResultCode.NotInitialized;
  97. }
  98. long namePosition = context.RequestData.BaseStream.Position;
  99. string name = ReadName(context);
  100. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  101. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  102. int maxSessions = context.RequestData.ReadInt32();
  103. if (name == string.Empty)
  104. {
  105. return ResultCode.InvalidName;
  106. }
  107. Logger.PrintInfo(LogClass.ServiceSm, $"Register \"{name}\".");
  108. KPort port = new KPort(context.Device.System, maxSessions, isLight, 0);
  109. if (!_registeredServices.TryAdd(name, port))
  110. {
  111. return ResultCode.AlreadyRegistered;
  112. }
  113. if (context.Process.HandleTable.GenerateHandle(port.ServerPort, out int handle) != KernelResult.Success)
  114. {
  115. throw new InvalidOperationException("Out of handles!");
  116. }
  117. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  118. return ResultCode.Success;
  119. }
  120. [Command(3)]
  121. // UnregisterService(ServiceName name)
  122. public ResultCode UnregisterService(ServiceCtx context)
  123. {
  124. if (!_isInitialized)
  125. {
  126. return ResultCode.NotInitialized;
  127. }
  128. long namePosition = context.RequestData.BaseStream.Position;
  129. string name = ReadName(context);
  130. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  131. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  132. int maxSessions = context.RequestData.ReadInt32();
  133. if (name == string.Empty)
  134. {
  135. return ResultCode.InvalidName;
  136. }
  137. if (!_registeredServices.TryRemove(name, out _))
  138. {
  139. return ResultCode.NotRegistered;
  140. }
  141. return ResultCode.Success;
  142. }
  143. private static string ReadName(ServiceCtx context)
  144. {
  145. string name = string.Empty;
  146. for (int index = 0; index < 8 &&
  147. context.RequestData.BaseStream.Position <
  148. context.RequestData.BaseStream.Length; index++)
  149. {
  150. byte chr = context.RequestData.ReadByte();
  151. if (chr >= 0x20 && chr < 0x7f)
  152. {
  153. name += (char)chr;
  154. }
  155. }
  156. return name;
  157. }
  158. }
  159. }