PtcJumpTable.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. namespace ARMeilleure.Translation.PTC
  9. {
  10. class PtcJumpTable
  11. {
  12. public struct TableEntry<TAddress>
  13. {
  14. public int EntryIndex;
  15. public long GuestAddress;
  16. public TAddress HostAddress;
  17. public TableEntry(int entryIndex, long guestAddress, TAddress hostAddress)
  18. {
  19. EntryIndex = entryIndex;
  20. GuestAddress = guestAddress;
  21. HostAddress = hostAddress;
  22. }
  23. }
  24. public enum DirectHostAddress
  25. {
  26. CallStub = 0,
  27. TailCallStub = 1,
  28. Host = 2
  29. }
  30. public enum IndirectHostAddress
  31. {
  32. CallStub = 0,
  33. TailCallStub = 1
  34. }
  35. private readonly List<TableEntry<DirectHostAddress>> _jumpTable;
  36. private readonly List<TableEntry<IndirectHostAddress>> _dynamicTable;
  37. public List<ulong> Targets { get; }
  38. public Dictionary<ulong, List<int>> Dependants { get; }
  39. public Dictionary<ulong, List<int>> Owners { get; }
  40. public PtcJumpTable()
  41. {
  42. _jumpTable = new List<TableEntry<DirectHostAddress>>();
  43. _dynamicTable = new List<TableEntry<IndirectHostAddress>>();
  44. Targets = new List<ulong>();
  45. Dependants = new Dictionary<ulong, List<int>>();
  46. Owners = new Dictionary<ulong, List<int>>();
  47. }
  48. public PtcJumpTable(
  49. List<TableEntry<DirectHostAddress>> jumpTable, List<TableEntry<IndirectHostAddress>> dynamicTable,
  50. List<ulong> targets, Dictionary<ulong, List<int>> dependants, Dictionary<ulong, List<int>> owners)
  51. {
  52. _jumpTable = jumpTable;
  53. _dynamicTable = dynamicTable;
  54. Targets = targets;
  55. Dependants = dependants;
  56. Owners = owners;
  57. }
  58. public static PtcJumpTable Deserialize(MemoryStream stream)
  59. {
  60. using (BinaryReader reader = new BinaryReader(stream, EncodingCache.UTF8NoBOM, true))
  61. {
  62. var jumpTable = new List<TableEntry<DirectHostAddress>>();
  63. int jumpTableCount = reader.ReadInt32();
  64. for (int i = 0; i < jumpTableCount; i++)
  65. {
  66. int entryIndex = reader.ReadInt32();
  67. long guestAddress = reader.ReadInt64();
  68. DirectHostAddress hostAddress = (DirectHostAddress)reader.ReadInt32();
  69. jumpTable.Add(new TableEntry<DirectHostAddress>(entryIndex, guestAddress, hostAddress));
  70. }
  71. var dynamicTable = new List<TableEntry<IndirectHostAddress>>();
  72. int dynamicTableCount = reader.ReadInt32();
  73. for (int i = 0; i < dynamicTableCount; i++)
  74. {
  75. int entryIndex = reader.ReadInt32();
  76. long guestAddress = reader.ReadInt64();
  77. IndirectHostAddress hostAddress = (IndirectHostAddress)reader.ReadInt32();
  78. dynamicTable.Add(new TableEntry<IndirectHostAddress>(entryIndex, guestAddress, hostAddress));
  79. }
  80. var targets = new List<ulong>();
  81. int targetsCount = reader.ReadInt32();
  82. for (int i = 0; i < targetsCount; i++)
  83. {
  84. ulong address = reader.ReadUInt64();
  85. targets.Add(address);
  86. }
  87. var dependants = new Dictionary<ulong, List<int>>();
  88. int dependantsCount = reader.ReadInt32();
  89. for (int i = 0; i < dependantsCount; i++)
  90. {
  91. ulong address = reader.ReadUInt64();
  92. var entries = new List<int>();
  93. int entriesCount = reader.ReadInt32();
  94. for (int j = 0; j < entriesCount; j++)
  95. {
  96. int entry = reader.ReadInt32();
  97. entries.Add(entry);
  98. }
  99. dependants.Add(address, entries);
  100. }
  101. var owners = new Dictionary<ulong, List<int>>();
  102. int ownersCount = reader.ReadInt32();
  103. for (int i = 0; i < ownersCount; i++)
  104. {
  105. ulong address = reader.ReadUInt64();
  106. var entries = new List<int>();
  107. int entriesCount = reader.ReadInt32();
  108. for (int j = 0; j < entriesCount; j++)
  109. {
  110. int entry = reader.ReadInt32();
  111. entries.Add(entry);
  112. }
  113. owners.Add(address, entries);
  114. }
  115. return new PtcJumpTable(jumpTable, dynamicTable, targets, dependants, owners);
  116. }
  117. }
  118. public static void Serialize(MemoryStream stream, PtcJumpTable ptcJumpTable)
  119. {
  120. using (BinaryWriter writer = new BinaryWriter(stream, EncodingCache.UTF8NoBOM, true))
  121. {
  122. writer.Write((int)ptcJumpTable._jumpTable.Count);
  123. foreach (var tableEntry in ptcJumpTable._jumpTable)
  124. {
  125. writer.Write((int)tableEntry.EntryIndex);
  126. writer.Write((long)tableEntry.GuestAddress);
  127. writer.Write((int)tableEntry.HostAddress);
  128. }
  129. writer.Write((int)ptcJumpTable._dynamicTable.Count);
  130. foreach (var tableEntry in ptcJumpTable._dynamicTable)
  131. {
  132. writer.Write((int)tableEntry.EntryIndex);
  133. writer.Write((long)tableEntry.GuestAddress);
  134. writer.Write((int)tableEntry.HostAddress);
  135. }
  136. writer.Write((int)ptcJumpTable.Targets.Count);
  137. foreach (ulong address in ptcJumpTable.Targets)
  138. {
  139. writer.Write((ulong)address);
  140. }
  141. writer.Write((int)ptcJumpTable.Dependants.Count);
  142. foreach (var kv in ptcJumpTable.Dependants)
  143. {
  144. writer.Write((ulong)kv.Key); // address
  145. writer.Write((int)kv.Value.Count);
  146. foreach (int entry in kv.Value)
  147. {
  148. writer.Write((int)entry);
  149. }
  150. }
  151. writer.Write((int)ptcJumpTable.Owners.Count);
  152. foreach (var kv in ptcJumpTable.Owners)
  153. {
  154. writer.Write((ulong)kv.Key); // address
  155. writer.Write((int)kv.Value.Count);
  156. foreach (int entry in kv.Value)
  157. {
  158. writer.Write((int)entry);
  159. }
  160. }
  161. }
  162. }
  163. public void Initialize(JumpTable jumpTable)
  164. {
  165. Targets.Clear();
  166. foreach (ulong guestAddress in jumpTable.Targets.Keys)
  167. {
  168. Targets.Add(guestAddress);
  169. }
  170. Dependants.Clear();
  171. foreach (var kv in jumpTable.Dependants)
  172. {
  173. Dependants.Add(kv.Key, new List<int>(kv.Value));
  174. }
  175. Owners.Clear();
  176. foreach (var kv in jumpTable.Owners)
  177. {
  178. Owners.Add(kv.Key, new List<int>(kv.Value));
  179. }
  180. }
  181. // For future use.
  182. public void Clean(ulong guestAddress)
  183. {
  184. if (Owners.TryGetValue(guestAddress, out List<int> entries))
  185. {
  186. foreach (int entry in entries)
  187. {
  188. if ((entry & JumpTable.DynamicEntryTag) == 0)
  189. {
  190. int removed = _jumpTable.RemoveAll(tableEntry => tableEntry.EntryIndex == entry);
  191. Debug.Assert(removed == 1);
  192. }
  193. else
  194. {
  195. if (JumpTable.DynamicTableElems > 1)
  196. {
  197. throw new NotSupportedException();
  198. }
  199. int removed = _dynamicTable.RemoveAll(tableEntry => tableEntry.EntryIndex == (entry & ~JumpTable.DynamicEntryTag));
  200. Debug.Assert(removed == 1);
  201. }
  202. }
  203. }
  204. Targets.Remove(guestAddress);
  205. Dependants.Remove(guestAddress);
  206. Owners.Remove(guestAddress);
  207. }
  208. public void Clear()
  209. {
  210. _jumpTable.Clear();
  211. _dynamicTable.Clear();
  212. Targets.Clear();
  213. Dependants.Clear();
  214. Owners.Clear();
  215. }
  216. public void WriteJumpTable(JumpTable jumpTable, ConcurrentDictionary<ulong, TranslatedFunction> funcs)
  217. {
  218. // Writes internal state to jump table in-memory, after PtcJumpTable was deserialized.
  219. foreach (var tableEntry in _jumpTable)
  220. {
  221. long guestAddress = tableEntry.GuestAddress;
  222. DirectHostAddress directHostAddress = tableEntry.HostAddress;
  223. long hostAddress;
  224. if (directHostAddress == DirectHostAddress.CallStub)
  225. {
  226. hostAddress = DirectCallStubs.DirectCallStub(false).ToInt64();
  227. }
  228. else if (directHostAddress == DirectHostAddress.TailCallStub)
  229. {
  230. hostAddress = DirectCallStubs.DirectCallStub(true).ToInt64();
  231. }
  232. else if (directHostAddress == DirectHostAddress.Host)
  233. {
  234. if (funcs.TryGetValue((ulong)guestAddress, out TranslatedFunction func))
  235. {
  236. hostAddress = func.FuncPtr.ToInt64();
  237. }
  238. else
  239. {
  240. if (!PtcProfiler.ProfiledFuncs.TryGetValue((ulong)guestAddress, out var value) || !value.highCq)
  241. {
  242. throw new KeyNotFoundException($"({nameof(guestAddress)} = 0x{(ulong)guestAddress:X16})");
  243. }
  244. hostAddress = 0L;
  245. }
  246. }
  247. else
  248. {
  249. throw new InvalidOperationException(nameof(directHostAddress));
  250. }
  251. int entry = tableEntry.EntryIndex;
  252. jumpTable.Table.SetEntry(entry);
  253. jumpTable.ExpandIfNeededJumpTable(entry);
  254. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  255. Marshal.WriteInt64(addr, 0, guestAddress);
  256. Marshal.WriteInt64(addr, 8, hostAddress);
  257. }
  258. }
  259. public void WriteDynamicTable(JumpTable jumpTable)
  260. {
  261. // Writes internal state to jump table in-memory, after PtcJumpTable was deserialized.
  262. if (JumpTable.DynamicTableElems > 1)
  263. {
  264. throw new NotSupportedException();
  265. }
  266. foreach (var tableEntry in _dynamicTable)
  267. {
  268. long guestAddress = tableEntry.GuestAddress;
  269. IndirectHostAddress indirectHostAddress = tableEntry.HostAddress;
  270. long hostAddress;
  271. if (indirectHostAddress == IndirectHostAddress.CallStub)
  272. {
  273. hostAddress = DirectCallStubs.IndirectCallStub(false).ToInt64();
  274. }
  275. else if (indirectHostAddress == IndirectHostAddress.TailCallStub)
  276. {
  277. hostAddress = DirectCallStubs.IndirectCallStub(true).ToInt64();
  278. }
  279. else
  280. {
  281. throw new InvalidOperationException(nameof(indirectHostAddress));
  282. }
  283. int entry = tableEntry.EntryIndex;
  284. jumpTable.DynTable.SetEntry(entry);
  285. jumpTable.ExpandIfNeededDynamicTable(entry);
  286. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  287. Marshal.WriteInt64(addr, 0, guestAddress);
  288. Marshal.WriteInt64(addr, 8, hostAddress);
  289. }
  290. }
  291. public void ReadJumpTable(JumpTable jumpTable)
  292. {
  293. // Reads in-memory jump table state and store internally for PtcJumpTable serialization.
  294. _jumpTable.Clear();
  295. IEnumerable<int> entries = jumpTable.Table.GetEntries();
  296. foreach (int entry in entries)
  297. {
  298. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  299. long guestAddress = Marshal.ReadInt64(addr, 0);
  300. long hostAddress = Marshal.ReadInt64(addr, 8);
  301. DirectHostAddress directHostAddress;
  302. if (hostAddress == DirectCallStubs.DirectCallStub(false).ToInt64())
  303. {
  304. directHostAddress = DirectHostAddress.CallStub;
  305. }
  306. else if (hostAddress == DirectCallStubs.DirectCallStub(true).ToInt64())
  307. {
  308. directHostAddress = DirectHostAddress.TailCallStub;
  309. }
  310. else
  311. {
  312. directHostAddress = DirectHostAddress.Host;
  313. }
  314. _jumpTable.Add(new TableEntry<DirectHostAddress>(entry, guestAddress, directHostAddress));
  315. }
  316. }
  317. public void ReadDynamicTable(JumpTable jumpTable)
  318. {
  319. // Reads in-memory jump table state and store internally for PtcJumpTable serialization.
  320. if (JumpTable.DynamicTableElems > 1)
  321. {
  322. throw new NotSupportedException();
  323. }
  324. _dynamicTable.Clear();
  325. IEnumerable<int> entries = jumpTable.DynTable.GetEntries();
  326. foreach (int entry in entries)
  327. {
  328. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  329. long guestAddress = Marshal.ReadInt64(addr, 0);
  330. long hostAddress = Marshal.ReadInt64(addr, 8);
  331. IndirectHostAddress indirectHostAddress;
  332. if (hostAddress == DirectCallStubs.IndirectCallStub(false).ToInt64())
  333. {
  334. indirectHostAddress = IndirectHostAddress.CallStub;
  335. }
  336. else if (hostAddress == DirectCallStubs.IndirectCallStub(true).ToInt64())
  337. {
  338. indirectHostAddress = IndirectHostAddress.TailCallStub;
  339. }
  340. else
  341. {
  342. throw new InvalidOperationException($"({nameof(hostAddress)} = 0x{hostAddress:X16})");
  343. }
  344. _dynamicTable.Add(new TableEntry<IndirectHostAddress>(entry, guestAddress, indirectHostAddress));
  345. }
  346. }
  347. }
  348. }