IUserInterface.cs 6.8 KB

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