IpcMessage.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Microsoft.IO;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Memory;
  4. using System;
  5. using System.Buffers;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. namespace Ryujinx.HLE.HOS.Ipc
  10. {
  11. class IpcMessage
  12. {
  13. public IpcMessageType Type { get; set; }
  14. public IpcHandleDesc HandleDesc { get; set; }
  15. public List<IpcPtrBuffDesc> PtrBuff { get; private set; }
  16. public List<IpcBuffDesc> SendBuff { get; private set; }
  17. public List<IpcBuffDesc> ReceiveBuff { get; private set; }
  18. public List<IpcBuffDesc> ExchangeBuff { get; private set; }
  19. public List<IpcRecvListBuffDesc> RecvListBuff { get; private set; }
  20. public List<int> ObjectIds { get; private set; }
  21. public byte[] RawData { get; set; }
  22. public IpcMessage()
  23. {
  24. PtrBuff = new List<IpcPtrBuffDesc>(0);
  25. SendBuff = new List<IpcBuffDesc>(0);
  26. ReceiveBuff = new List<IpcBuffDesc>(0);
  27. ExchangeBuff = new List<IpcBuffDesc>(0);
  28. RecvListBuff = new List<IpcRecvListBuffDesc>(0);
  29. ObjectIds = new List<int>(0);
  30. }
  31. public IpcMessage(ReadOnlySpan<byte> data, long cmdPtr)
  32. {
  33. using (RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream(data))
  34. {
  35. BinaryReader reader = new BinaryReader(ms);
  36. int word0 = reader.ReadInt32();
  37. int word1 = reader.ReadInt32();
  38. Type = (IpcMessageType)(word0 & 0xffff);
  39. int ptrBuffCount = (word0 >> 16) & 0xf;
  40. int sendBuffCount = (word0 >> 20) & 0xf;
  41. int recvBuffCount = (word0 >> 24) & 0xf;
  42. int xchgBuffCount = (word0 >> 28) & 0xf;
  43. int rawDataSize = (word1 >> 0) & 0x3ff;
  44. int recvListFlags = (word1 >> 10) & 0xf;
  45. bool hndDescEnable = ((word1 >> 31) & 0x1) != 0;
  46. if (hndDescEnable)
  47. {
  48. HandleDesc = new IpcHandleDesc(reader);
  49. }
  50. PtrBuff = new List<IpcPtrBuffDesc>(ptrBuffCount);
  51. for (int index = 0; index < ptrBuffCount; index++)
  52. {
  53. PtrBuff.Add(new IpcPtrBuffDesc(reader));
  54. }
  55. static List<IpcBuffDesc> ReadBuff(BinaryReader reader, int count)
  56. {
  57. List<IpcBuffDesc> buff = new List<IpcBuffDesc>(count);
  58. for (int index = 0; index < count; index++)
  59. {
  60. buff.Add(new IpcBuffDesc(reader));
  61. }
  62. return buff;
  63. }
  64. SendBuff = ReadBuff(reader, sendBuffCount);
  65. ReceiveBuff = ReadBuff(reader, recvBuffCount);
  66. ExchangeBuff = ReadBuff(reader, xchgBuffCount);
  67. rawDataSize *= 4;
  68. long recvListPos = reader.BaseStream.Position + rawDataSize;
  69. // Only CMIF has the padding requirements.
  70. if (Type < IpcMessageType.TipcCloseSession)
  71. {
  72. long pad0 = GetPadSize16(reader.BaseStream.Position + cmdPtr);
  73. if (rawDataSize != 0)
  74. {
  75. rawDataSize -= (int)pad0;
  76. }
  77. reader.BaseStream.Seek(pad0, SeekOrigin.Current);
  78. }
  79. int recvListCount = recvListFlags - 2;
  80. if (recvListCount == 0)
  81. {
  82. recvListCount = 1;
  83. }
  84. else if (recvListCount < 0)
  85. {
  86. recvListCount = 0;
  87. }
  88. RawData = reader.ReadBytes(rawDataSize);
  89. reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin);
  90. RecvListBuff = new List<IpcRecvListBuffDesc>(recvListCount);
  91. for (int index = 0; index < recvListCount; index++)
  92. {
  93. RecvListBuff.Add(new IpcRecvListBuffDesc(reader.ReadUInt64()));
  94. }
  95. ObjectIds = new List<int>(0);
  96. }
  97. }
  98. public RecyclableMemoryStream GetStream(long cmdPtr, ulong recvListAddr)
  99. {
  100. RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream();
  101. int word0;
  102. int word1;
  103. word0 = (int)Type;
  104. word0 |= (PtrBuff.Count & 0xf) << 16;
  105. word0 |= (SendBuff.Count & 0xf) << 20;
  106. word0 |= (ReceiveBuff.Count & 0xf) << 24;
  107. word0 |= (ExchangeBuff.Count & 0xf) << 28;
  108. using RecyclableMemoryStream handleDataStream = HandleDesc?.GetStream();
  109. int dataLength = RawData?.Length ?? 0;
  110. dataLength = (dataLength + 3) & ~3;
  111. int rawLength = dataLength;
  112. int pad0 = (int)GetPadSize16(cmdPtr + 8 + (handleDataStream?.Length ?? 0) + PtrBuff.Count * 8);
  113. // Apparently, padding after Raw Data is 16 bytes, however when there is
  114. // padding before Raw Data too, we need to subtract the size of this padding.
  115. // This is the weirdest padding I've seen so far...
  116. int pad1 = 0x10 - pad0;
  117. dataLength = (dataLength + pad0 + pad1) / 4;
  118. word1 = (dataLength & 0x3ff) | (2 << 10);
  119. if (HandleDesc != null)
  120. {
  121. word1 |= 1 << 31;
  122. }
  123. ms.Write(word0);
  124. ms.Write(word1);
  125. if (handleDataStream != null)
  126. {
  127. ms.Write(handleDataStream);
  128. }
  129. foreach (IpcPtrBuffDesc ptrBuffDesc in PtrBuff)
  130. {
  131. ms.Write(ptrBuffDesc.GetWord0());
  132. ms.Write(ptrBuffDesc.GetWord1());
  133. }
  134. ms.WriteByte(0, pad0);
  135. if (RawData != null)
  136. {
  137. ms.Write(RawData);
  138. ms.WriteByte(0, rawLength - RawData.Length);
  139. }
  140. ms.WriteByte(0, pad1);
  141. ms.Write(recvListAddr);
  142. ms.Position = 0;
  143. return ms;
  144. }
  145. public RecyclableMemoryStream GetStreamTipc()
  146. {
  147. Debug.Assert(PtrBuff.Count == 0);
  148. RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream();
  149. int word0;
  150. int word1;
  151. word0 = (int)Type;
  152. word0 |= (SendBuff.Count & 0xf) << 20;
  153. word0 |= (ReceiveBuff.Count & 0xf) << 24;
  154. word0 |= (ExchangeBuff.Count & 0xf) << 28;
  155. using RecyclableMemoryStream handleDataStream = HandleDesc?.GetStream();
  156. int dataLength = RawData?.Length ?? 0;
  157. dataLength = ((dataLength + 3) & ~3) / 4;
  158. word1 = (dataLength & 0x3ff);
  159. if (HandleDesc != null)
  160. {
  161. word1 |= 1 << 31;
  162. }
  163. ms.Write(word0);
  164. ms.Write(word1);
  165. if (handleDataStream != null)
  166. {
  167. ms.Write(handleDataStream);
  168. }
  169. if (RawData != null)
  170. {
  171. ms.Write(RawData);
  172. }
  173. return ms;
  174. }
  175. private long GetPadSize16(long position)
  176. {
  177. if ((position & 0xf) != 0)
  178. {
  179. return 0x10 - (position & 0xf);
  180. }
  181. return 0;
  182. }
  183. // ReSharper disable once InconsistentNaming
  184. public (ulong Position, ulong Size) GetBufferType0x21(int index = 0)
  185. {
  186. if (PtrBuff.Count > index && PtrBuff[index].Position != 0)
  187. {
  188. return (PtrBuff[index].Position, PtrBuff[index].Size);
  189. }
  190. if (SendBuff.Count > index)
  191. {
  192. return (SendBuff[index].Position, SendBuff[index].Size);
  193. }
  194. return (0, 0);
  195. }
  196. // ReSharper disable once InconsistentNaming
  197. public (ulong Position, ulong Size) GetBufferType0x22(int index = 0)
  198. {
  199. if (RecvListBuff.Count > index && RecvListBuff[index].Position != 0)
  200. {
  201. return (RecvListBuff[index].Position, RecvListBuff[index].Size);
  202. }
  203. if (ReceiveBuff.Count > index)
  204. {
  205. return (ReceiveBuff[index].Position, ReceiveBuff[index].Size);
  206. }
  207. return (0, 0);
  208. }
  209. }
  210. }