IUserInterface.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. };
  25. _registeredServices = new ConcurrentDictionary<string, KPort>();
  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. public long Initialize(ServiceCtx context)
  34. {
  35. _isInitialized = true;
  36. return 0;
  37. }
  38. public long GetService(ServiceCtx context)
  39. {
  40. if (!_isInitialized)
  41. {
  42. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.NotInitialized);
  43. }
  44. string name = ReadName(context);
  45. if (name == string.Empty)
  46. {
  47. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.InvalidName);
  48. }
  49. KSession session = new KSession(context.Device.System);
  50. if (_registeredServices.TryGetValue(name, out KPort port))
  51. {
  52. KernelResult result = port.EnqueueIncomingSession(session.ServerSession);
  53. if (result != KernelResult.Success)
  54. {
  55. throw new InvalidOperationException($"Session enqueue on port returned error \"{result}\".");
  56. }
  57. }
  58. else
  59. {
  60. session.ClientSession.Service = ServiceFactory.MakeService(context.Device.System, name);
  61. }
  62. if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
  63. {
  64. throw new InvalidOperationException("Out of handles!");
  65. }
  66. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  67. return 0;
  68. }
  69. public long RegisterService(ServiceCtx context)
  70. {
  71. if (!_isInitialized)
  72. {
  73. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.NotInitialized);
  74. }
  75. long namePosition = context.RequestData.BaseStream.Position;
  76. string name = ReadName(context);
  77. context.RequestData.BaseStream.Seek(namePosition + 8, SeekOrigin.Begin);
  78. bool isLight = (context.RequestData.ReadInt32() & 1) != 0;
  79. int maxSessions = context.RequestData.ReadInt32();
  80. if (name == string.Empty)
  81. {
  82. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.InvalidName);
  83. }
  84. Logger.PrintInfo(LogClass.ServiceSm, $"Register \"{name}\".");
  85. KPort port = new KPort(context.Device.System, maxSessions, isLight, 0);
  86. if (!_registeredServices.TryAdd(name, port))
  87. {
  88. return ErrorCode.MakeError(ErrorModule.Sm, SmErr.AlreadyRegistered);
  89. }
  90. if (context.Process.HandleTable.GenerateHandle(port.ServerPort, out int handle) != KernelResult.Success)
  91. {
  92. throw new InvalidOperationException("Out of handles!");
  93. }
  94. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  95. return 0;
  96. }
  97. private static string ReadName(ServiceCtx context)
  98. {
  99. string name = string.Empty;
  100. for (int index = 0; index < 8 &&
  101. context.RequestData.BaseStream.Position <
  102. context.RequestData.BaseStream.Length; index++)
  103. {
  104. byte chr = context.RequestData.ReadByte();
  105. if (chr >= 0x20 && chr < 0x7f)
  106. {
  107. name += (char)chr;
  108. }
  109. }
  110. return name;
  111. }
  112. }
  113. }