IpcService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Ipc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using Ryujinx.Profiler;
  10. using System.Reflection;
  11. using System.Linq;
  12. namespace Ryujinx.HLE.HOS.Services
  13. {
  14. abstract class IpcService : IIpcService
  15. {
  16. public IReadOnlyDictionary<int, MethodInfo> Commands { get; }
  17. private IdDictionary _domainObjects;
  18. private int _selfId;
  19. private bool _isDomain;
  20. public IpcService()
  21. {
  22. Commands = Assembly.GetExecutingAssembly().GetTypes()
  23. .Where(type => type == GetType())
  24. .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
  25. .SelectMany(methodInfo => methodInfo.GetCustomAttributes(typeof(CommandAttribute))
  26. .Select(command => (((CommandAttribute)command).Id, methodInfo)))
  27. .ToDictionary(command => command.Id, command => command.methodInfo);
  28. _domainObjects = new IdDictionary();
  29. _selfId = -1;
  30. }
  31. public int ConvertToDomain()
  32. {
  33. if (_selfId == -1)
  34. {
  35. _selfId = _domainObjects.Add(this);
  36. }
  37. _isDomain = true;
  38. return _selfId;
  39. }
  40. public void ConvertToSession()
  41. {
  42. _isDomain = false;
  43. }
  44. public void CallMethod(ServiceCtx context)
  45. {
  46. IIpcService service = this;
  47. if (_isDomain)
  48. {
  49. int domainWord0 = context.RequestData.ReadInt32();
  50. int domainObjId = context.RequestData.ReadInt32();
  51. int domainCmd = (domainWord0 >> 0) & 0xff;
  52. int inputObjCount = (domainWord0 >> 8) & 0xff;
  53. int dataPayloadSize = (domainWord0 >> 16) & 0xffff;
  54. context.RequestData.BaseStream.Seek(0x10 + dataPayloadSize, SeekOrigin.Begin);
  55. for (int index = 0; index < inputObjCount; index++)
  56. {
  57. context.Request.ObjectIds.Add(context.RequestData.ReadInt32());
  58. }
  59. context.RequestData.BaseStream.Seek(0x10, SeekOrigin.Begin);
  60. if (domainCmd == 1)
  61. {
  62. service = GetObject(domainObjId);
  63. context.ResponseData.Write(0L);
  64. context.ResponseData.Write(0L);
  65. }
  66. else if (domainCmd == 2)
  67. {
  68. Delete(domainObjId);
  69. context.ResponseData.Write(0L);
  70. return;
  71. }
  72. else
  73. {
  74. throw new NotImplementedException($"Domain command: {domainCmd}");
  75. }
  76. }
  77. long sfciMagic = context.RequestData.ReadInt64();
  78. int commandId = (int)context.RequestData.ReadInt64();
  79. bool serviceExists = service.Commands.TryGetValue(commandId, out MethodInfo processRequest);
  80. if (ServiceConfiguration.IgnoreMissingServices || serviceExists)
  81. {
  82. ResultCode result = ResultCode.Success;
  83. context.ResponseData.BaseStream.Seek(_isDomain ? 0x20 : 0x10, SeekOrigin.Begin);
  84. if (serviceExists)
  85. {
  86. Logger.PrintDebug(LogClass.KernelIpc, $"{service.GetType().Name}: {processRequest.Name}");
  87. ProfileConfig profile = Profiles.ServiceCall;
  88. profile.SessionGroup = service.GetType().Name;
  89. profile.SessionItem = processRequest.Name;
  90. Profile.Begin(profile);
  91. result = (ResultCode)processRequest.Invoke(service, new object[] { context });
  92. Profile.End(profile);
  93. }
  94. else
  95. {
  96. string serviceName;
  97. DummyService dummyService = service as DummyService;
  98. serviceName = (dummyService == null) ? service.GetType().FullName : dummyService.ServiceName;
  99. Logger.PrintWarning(LogClass.KernelIpc, $"Missing service {serviceName}: {commandId} ignored");
  100. }
  101. if (_isDomain)
  102. {
  103. foreach (int id in context.Response.ObjectIds)
  104. {
  105. context.ResponseData.Write(id);
  106. }
  107. context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
  108. context.ResponseData.Write(context.Response.ObjectIds.Count);
  109. }
  110. context.ResponseData.BaseStream.Seek(_isDomain ? 0x10 : 0, SeekOrigin.Begin);
  111. context.ResponseData.Write(IpcMagic.Sfco);
  112. context.ResponseData.Write((long)result);
  113. }
  114. else
  115. {
  116. string dbgMessage = $"{service.GetType().FullName}: {commandId}";
  117. throw new ServiceNotImplementedException(context, dbgMessage);
  118. }
  119. }
  120. protected static void MakeObject(ServiceCtx context, IpcService obj)
  121. {
  122. IpcService service = context.Session.Service;
  123. if (service._isDomain)
  124. {
  125. context.Response.ObjectIds.Add(service.Add(obj));
  126. }
  127. else
  128. {
  129. KSession session = new KSession(context.Device.System);
  130. session.ClientSession.Service = obj;
  131. if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
  132. {
  133. throw new InvalidOperationException("Out of handles!");
  134. }
  135. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  136. }
  137. }
  138. protected static T GetObject<T>(ServiceCtx context, int index) where T : IpcService
  139. {
  140. IpcService service = context.Session.Service;
  141. if (!service._isDomain)
  142. {
  143. int handle = context.Request.HandleDesc.ToMove[index];
  144. KClientSession session = context.Process.HandleTable.GetObject<KClientSession>(handle);
  145. return session?.Service is T ? (T)session.Service : null;
  146. }
  147. int objId = context.Request.ObjectIds[index];
  148. IIpcService obj = service.GetObject(objId);
  149. return obj is T ? (T)obj : null;
  150. }
  151. private int Add(IIpcService obj)
  152. {
  153. return _domainObjects.Add(obj);
  154. }
  155. private bool Delete(int id)
  156. {
  157. object obj = _domainObjects.Delete(id);
  158. if (obj is IDisposable disposableObj)
  159. {
  160. disposableObj.Dispose();
  161. }
  162. return obj != null;
  163. }
  164. private IIpcService GetObject(int id)
  165. {
  166. return _domainObjects.GetData<IIpcService>(id);
  167. }
  168. }
  169. }