IpcService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.Generic;
  7. using System.IO;
  8. namespace Ryujinx.HLE.HOS.Services
  9. {
  10. abstract class IpcService : IIpcService
  11. {
  12. public abstract IReadOnlyDictionary<int, ServiceProcessRequest> Commands { get; }
  13. private IdDictionary _domainObjects;
  14. private int _selfId;
  15. private bool _isDomain;
  16. public IpcService()
  17. {
  18. _domainObjects = new IdDictionary();
  19. _selfId = -1;
  20. }
  21. public int ConvertToDomain()
  22. {
  23. if (_selfId == -1)
  24. {
  25. _selfId = _domainObjects.Add(this);
  26. }
  27. _isDomain = true;
  28. return _selfId;
  29. }
  30. public void ConvertToSession()
  31. {
  32. _isDomain = false;
  33. }
  34. public void CallMethod(ServiceCtx context)
  35. {
  36. IIpcService service = this;
  37. if (_isDomain)
  38. {
  39. int domainWord0 = context.RequestData.ReadInt32();
  40. int domainObjId = context.RequestData.ReadInt32();
  41. int domainCmd = (domainWord0 >> 0) & 0xff;
  42. int inputObjCount = (domainWord0 >> 8) & 0xff;
  43. int dataPayloadSize = (domainWord0 >> 16) & 0xffff;
  44. context.RequestData.BaseStream.Seek(0x10 + dataPayloadSize, SeekOrigin.Begin);
  45. for (int index = 0; index < inputObjCount; index++)
  46. {
  47. context.Request.ObjectIds.Add(context.RequestData.ReadInt32());
  48. }
  49. context.RequestData.BaseStream.Seek(0x10, SeekOrigin.Begin);
  50. if (domainCmd == 1)
  51. {
  52. service = GetObject(domainObjId);
  53. context.ResponseData.Write(0L);
  54. context.ResponseData.Write(0L);
  55. }
  56. else if (domainCmd == 2)
  57. {
  58. Delete(domainObjId);
  59. context.ResponseData.Write(0L);
  60. return;
  61. }
  62. else
  63. {
  64. throw new NotImplementedException($"Domain command: {domainCmd}");
  65. }
  66. }
  67. long sfciMagic = context.RequestData.ReadInt64();
  68. int commandId = (int)context.RequestData.ReadInt64();
  69. if (service.Commands.TryGetValue(commandId, out ServiceProcessRequest processRequest))
  70. {
  71. context.ResponseData.BaseStream.Seek(_isDomain ? 0x20 : 0x10, SeekOrigin.Begin);
  72. Logger.PrintDebug(LogClass.KernelIpc, $"{service.GetType().Name}: {processRequest.Method.Name}");
  73. long result = processRequest(context);
  74. if (_isDomain)
  75. {
  76. foreach (int id in context.Response.ObjectIds)
  77. {
  78. context.ResponseData.Write(id);
  79. }
  80. context.ResponseData.BaseStream.Seek(0, SeekOrigin.Begin);
  81. context.ResponseData.Write(context.Response.ObjectIds.Count);
  82. }
  83. context.ResponseData.BaseStream.Seek(_isDomain ? 0x10 : 0, SeekOrigin.Begin);
  84. context.ResponseData.Write(IpcMagic.Sfco);
  85. context.ResponseData.Write(result);
  86. }
  87. else
  88. {
  89. string dbgMessage = $"{context.Session.ServiceName} {service.GetType().Name}: {commandId}";
  90. throw new NotImplementedException(dbgMessage);
  91. }
  92. }
  93. protected static void MakeObject(ServiceCtx context, IpcService obj)
  94. {
  95. IpcService service = context.Session.Service;
  96. if (service._isDomain)
  97. {
  98. context.Response.ObjectIds.Add(service.Add(obj));
  99. }
  100. else
  101. {
  102. KSession session = new KSession(obj, context.Session.ServiceName);
  103. if (context.Process.HandleTable.GenerateHandle(session, out int handle) != KernelResult.Success)
  104. {
  105. throw new InvalidOperationException("Out of handles!");
  106. }
  107. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  108. }
  109. }
  110. protected static T GetObject<T>(ServiceCtx context, int index) where T : IpcService
  111. {
  112. IpcService service = context.Session.Service;
  113. if (!service._isDomain)
  114. {
  115. int handle = context.Request.HandleDesc.ToMove[index];
  116. KSession session = context.Process.HandleTable.GetObject<KSession>(handle);
  117. return session?.Service is T ? (T)session.Service : null;
  118. }
  119. int objId = context.Request.ObjectIds[index];
  120. IIpcService obj = service.GetObject(objId);
  121. return obj is T ? (T)obj : null;
  122. }
  123. private int Add(IIpcService obj)
  124. {
  125. return _domainObjects.Add(obj);
  126. }
  127. private bool Delete(int id)
  128. {
  129. object obj = _domainObjects.Delete(id);
  130. if (obj is IDisposable disposableObj)
  131. {
  132. disposableObj.Dispose();
  133. }
  134. return obj != null;
  135. }
  136. private IIpcService GetObject(int id)
  137. {
  138. return _domainObjects.GetData<IIpcService>(id);
  139. }
  140. }
  141. }