IpcService.cs 5.4 KB

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