PtcJumpTable.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using ARMeilleure.Translation.Cache;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using static ARMeilleure.Translation.PTC.PtcFormatter;
  9. namespace ARMeilleure.Translation.PTC
  10. {
  11. class PtcJumpTable
  12. {
  13. [StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 16*/)]
  14. public struct TableEntry<TAddress>
  15. {
  16. public int EntryIndex;
  17. public long GuestAddress;
  18. public TAddress HostAddress;
  19. public TableEntry(int entryIndex, long guestAddress, TAddress hostAddress)
  20. {
  21. EntryIndex = entryIndex;
  22. GuestAddress = guestAddress;
  23. HostAddress = hostAddress;
  24. }
  25. }
  26. public enum DirectHostAddress : int
  27. {
  28. CallStub = 0,
  29. TailCallStub = 1,
  30. Host = 2
  31. }
  32. public enum IndirectHostAddress : int
  33. {
  34. CallStub = 0,
  35. TailCallStub = 1
  36. }
  37. private readonly List<TableEntry<DirectHostAddress>> _jumpTable;
  38. private readonly List<TableEntry<IndirectHostAddress>> _dynamicTable;
  39. public List<ulong> Targets { get; }
  40. public Dictionary<ulong, List<int>> Dependants { get; }
  41. public Dictionary<ulong, List<int>> Owners { get; }
  42. public PtcJumpTable()
  43. {
  44. _jumpTable = new List<TableEntry<DirectHostAddress>>();
  45. _dynamicTable = new List<TableEntry<IndirectHostAddress>>();
  46. Targets = new List<ulong>();
  47. Dependants = new Dictionary<ulong, List<int>>();
  48. Owners = new Dictionary<ulong, List<int>>();
  49. }
  50. public PtcJumpTable(
  51. List<TableEntry<DirectHostAddress>> jumpTable, List<TableEntry<IndirectHostAddress>> dynamicTable,
  52. List<ulong> targets, Dictionary<ulong, List<int>> dependants, Dictionary<ulong, List<int>> owners)
  53. {
  54. _jumpTable = jumpTable;
  55. _dynamicTable = dynamicTable;
  56. Targets = targets;
  57. Dependants = dependants;
  58. Owners = owners;
  59. }
  60. public static PtcJumpTable Deserialize(Stream stream)
  61. {
  62. var jumpTable = DeserializeList<TableEntry<DirectHostAddress>>(stream);
  63. var dynamicTable = DeserializeList<TableEntry<IndirectHostAddress>>(stream);
  64. var targets = DeserializeList<ulong>(stream);
  65. var dependants = DeserializeDictionary<ulong, List<int>>(stream, (stream) => DeserializeList<int>(stream));
  66. var owners = DeserializeDictionary<ulong, List<int>>(stream, (stream) => DeserializeList<int>(stream));
  67. return new PtcJumpTable(jumpTable, dynamicTable, targets, dependants, owners);
  68. }
  69. public static int GetSerializeSize(PtcJumpTable ptcJumpTable)
  70. {
  71. int size = 0;
  72. size += GetSerializeSizeList(ptcJumpTable._jumpTable);
  73. size += GetSerializeSizeList(ptcJumpTable._dynamicTable);
  74. size += GetSerializeSizeList(ptcJumpTable.Targets);
  75. size += GetSerializeSizeDictionary(ptcJumpTable.Dependants, (list) => GetSerializeSizeList(list));
  76. size += GetSerializeSizeDictionary(ptcJumpTable.Owners, (list) => GetSerializeSizeList(list));
  77. return size;
  78. }
  79. public static void Serialize(Stream stream, PtcJumpTable ptcJumpTable)
  80. {
  81. SerializeList(stream, ptcJumpTable._jumpTable);
  82. SerializeList(stream, ptcJumpTable._dynamicTable);
  83. SerializeList(stream, ptcJumpTable.Targets);
  84. SerializeDictionary(stream, ptcJumpTable.Dependants, (stream, list) => SerializeList(stream, list));
  85. SerializeDictionary(stream, ptcJumpTable.Owners, (stream, list) => SerializeList(stream, list));
  86. }
  87. public void Initialize(JumpTable jumpTable)
  88. {
  89. Targets.Clear();
  90. foreach (ulong guestAddress in jumpTable.Targets.Keys)
  91. {
  92. Targets.Add(guestAddress);
  93. }
  94. Dependants.Clear();
  95. foreach (var kv in jumpTable.Dependants)
  96. {
  97. Dependants.Add(kv.Key, new List<int>(kv.Value));
  98. }
  99. Owners.Clear();
  100. foreach (var kv in jumpTable.Owners)
  101. {
  102. Owners.Add(kv.Key, new List<int>(kv.Value));
  103. }
  104. }
  105. public void Clean(ulong guestAddress)
  106. {
  107. if (Owners.TryGetValue(guestAddress, out List<int> entries))
  108. {
  109. foreach (int entry in entries)
  110. {
  111. if ((entry & JumpTable.DynamicEntryTag) == 0)
  112. {
  113. int removed = _jumpTable.RemoveAll(tableEntry => tableEntry.EntryIndex == entry);
  114. Debug.Assert(removed == 1);
  115. }
  116. else
  117. {
  118. if (JumpTable.DynamicTableElems > 1)
  119. {
  120. throw new NotSupportedException();
  121. }
  122. int removed = _dynamicTable.RemoveAll(tableEntry => tableEntry.EntryIndex == (entry & ~JumpTable.DynamicEntryTag));
  123. Debug.Assert(removed == 1);
  124. }
  125. }
  126. }
  127. Targets.Remove(guestAddress);
  128. Dependants.Remove(guestAddress);
  129. Owners.Remove(guestAddress);
  130. }
  131. public void ClearIfNeeded()
  132. {
  133. if (_jumpTable.Count == 0 && _dynamicTable.Count == 0 &&
  134. Targets.Count == 0 && Dependants.Count == 0 && Owners.Count == 0)
  135. {
  136. return;
  137. }
  138. _jumpTable.Clear();
  139. _jumpTable.TrimExcess();
  140. _dynamicTable.Clear();
  141. _dynamicTable.TrimExcess();
  142. Targets.Clear();
  143. Targets.TrimExcess();
  144. Dependants.Clear();
  145. Dependants.TrimExcess();
  146. Owners.Clear();
  147. Owners.TrimExcess();
  148. }
  149. public void WriteJumpTable(JumpTable jumpTable, ConcurrentDictionary<ulong, TranslatedFunction> funcs)
  150. {
  151. // Writes internal state to jump table in-memory, after PtcJumpTable was deserialized.
  152. foreach (var tableEntry in _jumpTable)
  153. {
  154. long guestAddress = tableEntry.GuestAddress;
  155. DirectHostAddress directHostAddress = tableEntry.HostAddress;
  156. long hostAddress;
  157. if (directHostAddress == DirectHostAddress.CallStub)
  158. {
  159. hostAddress = DirectCallStubs.DirectCallStub(false).ToInt64();
  160. }
  161. else if (directHostAddress == DirectHostAddress.TailCallStub)
  162. {
  163. hostAddress = DirectCallStubs.DirectCallStub(true).ToInt64();
  164. }
  165. else if (directHostAddress == DirectHostAddress.Host)
  166. {
  167. if (funcs.TryGetValue((ulong)guestAddress, out TranslatedFunction func))
  168. {
  169. hostAddress = func.FuncPtr.ToInt64();
  170. }
  171. else
  172. {
  173. if (!PtcProfiler.ProfiledFuncs.TryGetValue((ulong)guestAddress, out var value) || !value.HighCq)
  174. {
  175. throw new KeyNotFoundException($"({nameof(guestAddress)} = 0x{(ulong)guestAddress:X16})");
  176. }
  177. hostAddress = 0L;
  178. }
  179. }
  180. else
  181. {
  182. throw new InvalidOperationException(nameof(directHostAddress));
  183. }
  184. int entry = tableEntry.EntryIndex;
  185. jumpTable.Table.SetEntry(entry);
  186. jumpTable.ExpandIfNeededJumpTable(entry);
  187. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  188. Marshal.WriteInt64(addr, 0, guestAddress);
  189. Marshal.WriteInt64(addr, 8, hostAddress);
  190. }
  191. }
  192. public void WriteDynamicTable(JumpTable jumpTable)
  193. {
  194. // Writes internal state to jump table in-memory, after PtcJumpTable was deserialized.
  195. if (JumpTable.DynamicTableElems > 1)
  196. {
  197. throw new NotSupportedException();
  198. }
  199. foreach (var tableEntry in _dynamicTable)
  200. {
  201. long guestAddress = tableEntry.GuestAddress;
  202. IndirectHostAddress indirectHostAddress = tableEntry.HostAddress;
  203. long hostAddress;
  204. if (indirectHostAddress == IndirectHostAddress.CallStub)
  205. {
  206. hostAddress = DirectCallStubs.IndirectCallStub(false).ToInt64();
  207. }
  208. else if (indirectHostAddress == IndirectHostAddress.TailCallStub)
  209. {
  210. hostAddress = DirectCallStubs.IndirectCallStub(true).ToInt64();
  211. }
  212. else
  213. {
  214. throw new InvalidOperationException(nameof(indirectHostAddress));
  215. }
  216. int entry = tableEntry.EntryIndex;
  217. jumpTable.DynTable.SetEntry(entry);
  218. jumpTable.ExpandIfNeededDynamicTable(entry);
  219. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  220. Marshal.WriteInt64(addr, 0, guestAddress);
  221. Marshal.WriteInt64(addr, 8, hostAddress);
  222. }
  223. }
  224. public void ReadJumpTable(JumpTable jumpTable)
  225. {
  226. // Reads in-memory jump table state and store internally for PtcJumpTable serialization.
  227. _jumpTable.Clear();
  228. IEnumerable<int> entries = jumpTable.Table.GetEntries();
  229. foreach (int entry in entries)
  230. {
  231. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  232. long guestAddress = Marshal.ReadInt64(addr, 0);
  233. long hostAddress = Marshal.ReadInt64(addr, 8);
  234. DirectHostAddress directHostAddress;
  235. if (hostAddress == DirectCallStubs.DirectCallStub(false).ToInt64())
  236. {
  237. directHostAddress = DirectHostAddress.CallStub;
  238. }
  239. else if (hostAddress == DirectCallStubs.DirectCallStub(true).ToInt64())
  240. {
  241. directHostAddress = DirectHostAddress.TailCallStub;
  242. }
  243. else
  244. {
  245. directHostAddress = DirectHostAddress.Host;
  246. }
  247. _jumpTable.Add(new TableEntry<DirectHostAddress>(entry, guestAddress, directHostAddress));
  248. }
  249. }
  250. public void ReadDynamicTable(JumpTable jumpTable)
  251. {
  252. // Reads in-memory jump table state and store internally for PtcJumpTable serialization.
  253. if (JumpTable.DynamicTableElems > 1)
  254. {
  255. throw new NotSupportedException();
  256. }
  257. _dynamicTable.Clear();
  258. IEnumerable<int> entries = jumpTable.DynTable.GetEntries();
  259. foreach (int entry in entries)
  260. {
  261. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  262. long guestAddress = Marshal.ReadInt64(addr, 0);
  263. long hostAddress = Marshal.ReadInt64(addr, 8);
  264. IndirectHostAddress indirectHostAddress;
  265. if (hostAddress == DirectCallStubs.IndirectCallStub(false).ToInt64())
  266. {
  267. indirectHostAddress = IndirectHostAddress.CallStub;
  268. }
  269. else if (hostAddress == DirectCallStubs.IndirectCallStub(true).ToInt64())
  270. {
  271. indirectHostAddress = IndirectHostAddress.TailCallStub;
  272. }
  273. else
  274. {
  275. throw new InvalidOperationException($"({nameof(hostAddress)} = 0x{hostAddress:X16})");
  276. }
  277. _dynamicTable.Add(new TableEntry<IndirectHostAddress>(entry, guestAddress, indirectHostAddress));
  278. }
  279. }
  280. }
  281. }