IpcHandler.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.OsHle.Handles;
  3. using Ryujinx.Core.OsHle.IpcServices;
  4. using System;
  5. using System.IO;
  6. namespace Ryujinx.Core.OsHle.Ipc
  7. {
  8. static class IpcHandler
  9. {
  10. private const long SfciMagic = 'S' << 0 | 'F' << 8 | 'C' << 16 | 'I' << 24;
  11. private const long SfcoMagic = 'S' << 0 | 'F' << 8 | 'C' << 16 | 'O' << 24;
  12. public static void IpcCall(
  13. Switch Ns,
  14. AMemory Memory,
  15. HSession Session,
  16. IpcMessage Request,
  17. int ThreadId,
  18. long CmdPtr,
  19. int HndId)
  20. {
  21. IpcMessage Response = new IpcMessage(Request.IsDomain && Request.Type == IpcMessageType.Request);
  22. using (MemoryStream Raw = new MemoryStream(Request.RawData))
  23. {
  24. BinaryReader ReqReader = new BinaryReader(Raw);
  25. if (Request.Type == IpcMessageType.Request)
  26. {
  27. string ServiceName = Session.Service.GetType().Name;
  28. ServiceProcessRequest ProcReq = null;
  29. bool IgnoreNullPR = false;
  30. string DbgServiceName = string.Empty;
  31. if (Session is HDomain Dom)
  32. {
  33. if (Request.DomCmd == IpcDomCmd.SendMsg)
  34. {
  35. long Magic = ReqReader.ReadInt64();
  36. int CmdId = (int)ReqReader.ReadInt64();
  37. object Obj = Dom.GetObject(Request.DomObjId);
  38. if (Obj is HDomain)
  39. {
  40. Session.Service.Commands.TryGetValue(CmdId, out ProcReq);
  41. DbgServiceName = $"{ProcReq?.Method.Name ?? CmdId.ToString()}";
  42. }
  43. else if (Obj != null)
  44. {
  45. ((IIpcService)Obj).Commands.TryGetValue(CmdId, out ProcReq);
  46. DbgServiceName = $"{Obj.GetType().Name} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  47. }
  48. }
  49. else if (Request.DomCmd == IpcDomCmd.DeleteObj)
  50. {
  51. Dom.DeleteObject(Request.DomObjId);
  52. Response = FillResponse(Response, 0);
  53. IgnoreNullPR = true;
  54. }
  55. }
  56. else
  57. {
  58. long Magic = ReqReader.ReadInt64();
  59. int CmdId = (int)ReqReader.ReadInt64();
  60. if (Session is HSessionObj)
  61. {
  62. object Obj = ((HSessionObj)Session).Obj;
  63. ((IIpcService)Obj).Commands.TryGetValue(CmdId, out ProcReq);
  64. DbgServiceName = $"{Obj.GetType().Name} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  65. }
  66. else
  67. {
  68. Session.Service.Commands.TryGetValue(CmdId, out ProcReq);
  69. DbgServiceName = $"{ProcReq?.Method.Name ?? CmdId.ToString()}";
  70. }
  71. }
  72. DbgServiceName = $"Tid {ThreadId} {ServiceName} {DbgServiceName}";
  73. Logging.Debug($"IpcMessage: {DbgServiceName}");
  74. if (ProcReq != null)
  75. {
  76. using (MemoryStream ResMS = new MemoryStream())
  77. {
  78. BinaryWriter ResWriter = new BinaryWriter(ResMS);
  79. ServiceCtx Context = new ServiceCtx(
  80. Ns,
  81. Memory,
  82. Session,
  83. Request,
  84. Response,
  85. ReqReader,
  86. ResWriter);
  87. long Result = ProcReq(Context);
  88. Response = FillResponse(Response, Result, ResMS.ToArray());
  89. }
  90. }
  91. else if (!IgnoreNullPR)
  92. {
  93. throw new NotImplementedException(DbgServiceName);
  94. }
  95. }
  96. else if (Request.Type == IpcMessageType.Control)
  97. {
  98. long Magic = ReqReader.ReadInt64();
  99. long CmdId = ReqReader.ReadInt64();
  100. switch (CmdId)
  101. {
  102. case 0: Request = IpcConvertSessionToDomain(Ns, Session, Response, HndId); break;
  103. case 3: Request = IpcQueryBufferPointerSize(Response); break;
  104. case 2: //IpcDuplicateSession, differences is unknown.
  105. case 4: Request = IpcDuplicateSessionEx(Ns, Session, Response, ReqReader); break;
  106. default: throw new NotImplementedException(CmdId.ToString());
  107. }
  108. }
  109. else if (Request.Type == IpcMessageType.Unknown2)
  110. {
  111. //TODO
  112. }
  113. else
  114. {
  115. throw new NotImplementedException(Request.Type.ToString());
  116. }
  117. AMemoryHelper.WriteBytes(Memory, CmdPtr, Response.GetBytes(CmdPtr));
  118. }
  119. }
  120. private static IpcMessage IpcConvertSessionToDomain(
  121. Switch Ns,
  122. HSession Session,
  123. IpcMessage Response,
  124. int HndId)
  125. {
  126. HDomain Dom = new HDomain(Session);
  127. Ns.Os.Handles.ReplaceData(HndId, Dom);
  128. return FillResponse(Response, 0, Dom.GenerateObjectId(Dom));
  129. }
  130. private static IpcMessage IpcDuplicateSessionEx(
  131. Switch Ns,
  132. HSession Session,
  133. IpcMessage Response,
  134. BinaryReader ReqReader)
  135. {
  136. int Unknown = ReqReader.ReadInt32();
  137. int Handle = Ns.Os.Handles.GenerateId(Session);
  138. Response.HandleDesc = IpcHandleDesc.MakeMove(Handle);
  139. return FillResponse(Response, 0);
  140. }
  141. private static IpcMessage IpcQueryBufferPointerSize(IpcMessage Response)
  142. {
  143. return FillResponse(Response, 0, 0x500);
  144. }
  145. private static IpcMessage FillResponse(IpcMessage Response, long Result, params int[] Values)
  146. {
  147. using (MemoryStream MS = new MemoryStream())
  148. {
  149. BinaryWriter Writer = new BinaryWriter(MS);
  150. foreach (int Value in Values)
  151. {
  152. Writer.Write(Value);
  153. }
  154. return FillResponse(Response, Result, MS.ToArray());
  155. }
  156. }
  157. private static IpcMessage FillResponse(IpcMessage Response, long Result, byte[] Data = null)
  158. {
  159. Response.Type = IpcMessageType.Response;
  160. using (MemoryStream MS = new MemoryStream())
  161. {
  162. BinaryWriter Writer = new BinaryWriter(MS);
  163. Writer.Write(SfcoMagic);
  164. Writer.Write(Result);
  165. if (Data != null)
  166. {
  167. Writer.Write(Data);
  168. }
  169. Response.RawData = MS.ToArray();
  170. }
  171. return Response;
  172. }
  173. }
  174. }