IHOSBinderDriver.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Threading;
  4. using Ryujinx.Horizon.Common;
  5. using System;
  6. using System.Buffers;
  7. namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
  8. {
  9. abstract class IHOSBinderDriver : IpcService
  10. {
  11. public IHOSBinderDriver() { }
  12. [CommandCmif(0)]
  13. // TransactParcel(s32, u32, u32, buffer<unknown, 5, 0>) -> buffer<unknown, 6, 0>
  14. public ResultCode TransactParcel(ServiceCtx context)
  15. {
  16. int binderId = context.RequestData.ReadInt32();
  17. uint code = context.RequestData.ReadUInt32();
  18. uint flags = context.RequestData.ReadUInt32();
  19. ulong dataPos = context.Request.SendBuff[0].Position;
  20. ulong dataSize = context.Request.SendBuff[0].Size;
  21. ulong replyPos = context.Request.ReceiveBuff[0].Position;
  22. ulong replySize = context.Request.ReceiveBuff[0].Size;
  23. ReadOnlySpan<byte> inputParcel = context.Memory.GetSpan(dataPos, (int)dataSize);
  24. Span<byte> outputParcel = new Span<byte>(new byte[replySize]);
  25. ResultCode result = OnTransact(binderId, code, flags, inputParcel, outputParcel);
  26. if (result == ResultCode.Success)
  27. {
  28. context.Memory.Write(replyPos, outputParcel);
  29. }
  30. return result;
  31. }
  32. [CommandCmif(1)]
  33. // AdjustRefcount(s32, s32, s32)
  34. public ResultCode AdjustRefcount(ServiceCtx context)
  35. {
  36. int binderId = context.RequestData.ReadInt32();
  37. int addVal = context.RequestData.ReadInt32();
  38. int type = context.RequestData.ReadInt32();
  39. return AdjustRefcount(binderId, addVal, type);
  40. }
  41. [CommandCmif(2)]
  42. // GetNativeHandle(s32, s32) -> handle<copy>
  43. public ResultCode GetNativeHandle(ServiceCtx context)
  44. {
  45. int binderId = context.RequestData.ReadInt32();
  46. uint typeId = context.RequestData.ReadUInt32();
  47. GetNativeHandle(binderId, typeId, out KReadableEvent readableEvent);
  48. if (context.Process.HandleTable.GenerateHandle(readableEvent, out int handle) != Result.Success)
  49. {
  50. throw new InvalidOperationException("Out of handles!");
  51. }
  52. context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
  53. return ResultCode.Success;
  54. }
  55. [CommandCmif(3)] // 3.0.0+
  56. // TransactParcelAuto(s32, u32, u32, buffer<unknown, 21, 0>) -> buffer<unknown, 22, 0>
  57. public ResultCode TransactParcelAuto(ServiceCtx context)
  58. {
  59. int binderId = context.RequestData.ReadInt32();
  60. uint code = context.RequestData.ReadUInt32();
  61. uint flags = context.RequestData.ReadUInt32();
  62. (ulong dataPos, ulong dataSize) = context.Request.GetBufferType0x21();
  63. (ulong replyPos, ulong replySize) = context.Request.GetBufferType0x22();
  64. ReadOnlySpan<byte> inputParcel = context.Memory.GetSpan(dataPos, (int)dataSize);
  65. using (IMemoryOwner<byte> outputParcelOwner = ByteMemoryPool.Shared.RentCleared(replySize))
  66. {
  67. Span<byte> outputParcel = outputParcelOwner.Memory.Span;
  68. ResultCode result = OnTransact(binderId, code, flags, inputParcel, outputParcel);
  69. if (result == ResultCode.Success)
  70. {
  71. context.Memory.Write(replyPos, outputParcel);
  72. }
  73. return result;
  74. }
  75. }
  76. protected abstract ResultCode AdjustRefcount(int binderId, int addVal, int type);
  77. protected abstract void GetNativeHandle(int binderId, uint typeId, out KReadableEvent readableEvent);
  78. protected abstract ResultCode OnTransact(int binderId, uint code, uint flags, ReadOnlySpan<byte> inputParcel, Span<byte> outputParcel);
  79. }
  80. }