IpcMessage.cs 8.0 KB

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