IpcService.cs 5.5 KB

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