IUserInterface.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. namespace Ryujinx.HLE.HOS.Services.Sm
  10. {
  11. class IUserInterface : IpcService
  12. {
  13. private Dictionary<int, ServiceProcessRequest> _commands;
  14. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  15. private ConcurrentDictionary<string, KPort> _registeredServices;
  16. private bool _isInitialized;
  17. public IUserInterface()
  18. {
  19. _commands = new Dictionary<int, ServiceProcessRequest>
  20. {
  21. { 0, Initialize },
  22. { 1, GetService },
  23. { 2, RegisterService },
  24. { 3, UnregisterService }
  25. };
  26. _registeredServices = new ConcurrentDictionary<string, KPort>();
  27. }
  28. public static void InitializePort(Horizon system)
  29. {
  30. KPort port = new KPort(system, 256, false, 0);
  31. port.ClientPort.SetName("sm:");
  32. port.ClientPort.Service = new IUserInterface();
  33. }
  34. public long Initialize(ServiceCtx context)
  35. {
  36. _isInitialized = true;
  37. return 0;
  38. }
  39. public long GetService(ServiceCtx context)
  40. {
  41. if (!_isInitialized)
  42. {
  43. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.NotInitialized);
  44. }
  45. string name = ReadName(context);
  46. if (name == string.Empty)
  47. {
  48. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.InvalidName);
  49. }
  50. KSession session = new KSession(context.Device.System);
  51. if (_registeredServices.TryGetValue(name, out KPort port))
  52. {
  53. KernelResult result = port.EnqueueIncomingSession(session.ServerSession);
  54. if (result != KernelResult.Success)
  55. {
  56. throw new InvalidOperationException($"Session enqueue on port returned error \"{result}\".");
  57. }
  58. }
  59. else
  60. {
  61. session.ClientSession.Service = ServiceFactory.MakeService(context.Device.System, name);
  62. }
  63. if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
  64. {
  65. throw new InvalidOperationException("Out of handles!");
  66. }
  67. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  68. return 0;
  69. }
  70. public long RegisterService(ServiceCtx context)
  71. {
  72. if (!_isInitialized)
  73. {
  74. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.NotInitialized);
  75. }
  76. long namePosition = context.RequestData.BaseStream.Position;
  77. string name = ReadName(context);
  78. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  79. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  80. int maxSessions = context.RequestData.ReadInt32();
  81. if (name == string.Empty)
  82. {
  83. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.InvalidName);
  84. }
  85. Logger.PrintInfo(LogClass.ServiceSm, $"Register \"{name}\".");
  86. KPort port = new KPort(context.Device.System, maxSessions, isLight, 0);
  87. if (!_registeredServices.TryAdd(name, port))
  88. {
  89. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.AlreadyRegistered);
  90. }
  91. if (context.Process.HandleTable.GenerateHandle(port.ServerPort, out int handle) != KernelResult.Success)
  92. {
  93. throw new InvalidOperationException("Out of handles!");
  94. }
  95. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  96. return 0;
  97. }
  98. public long UnregisterService(ServiceCtx context)
  99. {
  100. if (!_isInitialized)
  101. {
  102. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.NotInitialized);
  103. }
  104. long namePosition = context.RequestData.BaseStream.Position;
  105. string name = ReadName(context);
  106. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  107. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  108. int maxSessions = context.RequestData.ReadInt32();
  109. if (name == string.Empty)
  110. {
  111. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.InvalidName);
  112. }
  113. if (!_registeredServices.TryRemove(name, out _))
  114. {
  115. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.NotRegistered);
  116. }
  117. return 0;
  118. }
  119. private static string ReadName(ServiceCtx context)
  120. {
  121. string name = string.Empty;
  122. for (int index = 0; index < 8 &&
  123. context.RequestData.BaseStream.Position <
  124. context.RequestData.BaseStream.Length; index++)
  125. {
  126. byte chr = context.RequestData.ReadByte();
  127. if (chr >= 0x20 && chr < 0x7f)
  128. {
  129. name += (char)chr;
  130. }
  131. }
  132. return name;
  133. }
  134. }
  135. }