PtcJumpTable.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using ARMeilleure.Translation.Cache;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace ARMeilleure.Translation.PTC
  7. {
  8. [Serializable]
  9. class PtcJumpTable
  10. {
  11. [Serializable]
  12. private 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. private enum DirectHostAddress
  25. {
  26. CallStub,
  27. TailCallStub,
  28. Host
  29. }
  30. private enum IndirectHostAddress
  31. {
  32. CallStub,
  33. TailCallStub
  34. }
  35. private readonly List<TableEntry<DirectHostAddress>> _jumpTable;
  36. private readonly List<TableEntry<IndirectHostAddress>> _dynamicTable;
  37. private readonly List<ulong> _targets;
  38. private readonly Dictionary<ulong, List<int>> _dependants;
  39. private readonly Dictionary<ulong, List<int>> _owners;
  40. public List<ulong> Targets => _targets;
  41. public Dictionary<ulong, List<int>> Dependants => _dependants;
  42. public Dictionary<ulong, List<int>> Owners => _owners;
  43. public PtcJumpTable()
  44. {
  45. _jumpTable = new List<TableEntry<DirectHostAddress>>();
  46. _dynamicTable = new List<TableEntry<IndirectHostAddress>>();
  47. _targets = new List<ulong>();
  48. _dependants = new Dictionary<ulong, List<int>>();
  49. _owners = new Dictionary<ulong, List<int>>();
  50. }
  51. public void Initialize(JumpTable jumpTable)
  52. {
  53. _targets.Clear();
  54. foreach (ulong guestAddress in jumpTable.Targets.Keys)
  55. {
  56. _targets.Add(guestAddress);
  57. }
  58. _dependants.Clear();
  59. foreach (var item in jumpTable.Dependants)
  60. {
  61. _dependants.Add(item.Key, new List<int>(item.Value));
  62. }
  63. _owners.Clear();
  64. foreach (var item in jumpTable.Owners)
  65. {
  66. _owners.Add(item.Key, new List<int>(item.Value));
  67. }
  68. }
  69. public void Clear()
  70. {
  71. _jumpTable.Clear();
  72. _dynamicTable.Clear();
  73. _targets.Clear();
  74. _dependants.Clear();
  75. _owners.Clear();
  76. }
  77. public void WriteJumpTable(JumpTable jumpTable, ConcurrentDictionary<ulong, TranslatedFunction> funcs)
  78. {
  79. // Writes internal state to jump table in-memory, after PTC was loaded.
  80. foreach (var item in _jumpTable)
  81. {
  82. long guestAddress = item.GuestAddress;
  83. DirectHostAddress directHostAddress = item.HostAddress;
  84. long hostAddress;
  85. if (directHostAddress == DirectHostAddress.CallStub)
  86. {
  87. hostAddress = DirectCallStubs.DirectCallStub(false).ToInt64();
  88. }
  89. else if (directHostAddress == DirectHostAddress.TailCallStub)
  90. {
  91. hostAddress = DirectCallStubs.DirectCallStub(true).ToInt64();
  92. }
  93. else if (directHostAddress == DirectHostAddress.Host)
  94. {
  95. if (funcs.TryGetValue((ulong)guestAddress, out TranslatedFunction func))
  96. {
  97. hostAddress = func.FuncPtr.ToInt64();
  98. }
  99. else
  100. {
  101. throw new KeyNotFoundException($"({nameof(guestAddress)} = 0x{(ulong)guestAddress:X16})");
  102. }
  103. }
  104. else
  105. {
  106. throw new InvalidOperationException(nameof(directHostAddress));
  107. }
  108. int entry = item.EntryIndex;
  109. jumpTable.Table.SetEntry(entry);
  110. jumpTable.ExpandIfNeededJumpTable(entry);
  111. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  112. Marshal.WriteInt64(addr, 0, guestAddress);
  113. Marshal.WriteInt64(addr, 8, hostAddress);
  114. }
  115. }
  116. public void WriteDynamicTable(JumpTable jumpTable)
  117. {
  118. // Writes internal state to jump table in-memory, after PTC was loaded.
  119. if (JumpTable.DynamicTableElems > 1)
  120. {
  121. throw new NotSupportedException();
  122. }
  123. foreach (var item in _dynamicTable)
  124. {
  125. long guestAddress = item.GuestAddress;
  126. IndirectHostAddress indirectHostAddress = item.HostAddress;
  127. long hostAddress;
  128. if (indirectHostAddress == IndirectHostAddress.CallStub)
  129. {
  130. hostAddress = DirectCallStubs.IndirectCallStub(false).ToInt64();
  131. }
  132. else if (indirectHostAddress == IndirectHostAddress.TailCallStub)
  133. {
  134. hostAddress = DirectCallStubs.IndirectCallStub(true).ToInt64();
  135. }
  136. else
  137. {
  138. throw new InvalidOperationException(nameof(indirectHostAddress));
  139. }
  140. int entry = item.EntryIndex;
  141. jumpTable.DynTable.SetEntry(entry);
  142. jumpTable.ExpandIfNeededDynamicTable(entry);
  143. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  144. Marshal.WriteInt64(addr, 0, guestAddress);
  145. Marshal.WriteInt64(addr, 8, hostAddress);
  146. }
  147. }
  148. public void ReadJumpTable(JumpTable jumpTable)
  149. {
  150. // Reads in-memory jump table state and store internally for PTC serialization.
  151. _jumpTable.Clear();
  152. IEnumerable<int> entries = jumpTable.Table.GetEntries();
  153. foreach (int entry in entries)
  154. {
  155. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  156. long guestAddress = Marshal.ReadInt64(addr, 0);
  157. long hostAddress = Marshal.ReadInt64(addr, 8);
  158. DirectHostAddress directHostAddress;
  159. if (hostAddress == DirectCallStubs.DirectCallStub(false).ToInt64())
  160. {
  161. directHostAddress = DirectHostAddress.CallStub;
  162. }
  163. else if (hostAddress == DirectCallStubs.DirectCallStub(true).ToInt64())
  164. {
  165. directHostAddress = DirectHostAddress.TailCallStub;
  166. }
  167. else
  168. {
  169. directHostAddress = DirectHostAddress.Host;
  170. }
  171. _jumpTable.Add(new TableEntry<DirectHostAddress>(entry, guestAddress, directHostAddress));
  172. }
  173. }
  174. public void ReadDynamicTable(JumpTable jumpTable)
  175. {
  176. // Reads in-memory jump table state and store internally for PTC serialization.
  177. if (JumpTable.DynamicTableElems > 1)
  178. {
  179. throw new NotSupportedException();
  180. }
  181. _dynamicTable.Clear();
  182. IEnumerable<int> entries = jumpTable.DynTable.GetEntries();
  183. foreach (int entry in entries)
  184. {
  185. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  186. long guestAddress = Marshal.ReadInt64(addr, 0);
  187. long hostAddress = Marshal.ReadInt64(addr, 8);
  188. IndirectHostAddress indirectHostAddress;
  189. if (hostAddress == DirectCallStubs.IndirectCallStub(false).ToInt64())
  190. {
  191. indirectHostAddress = IndirectHostAddress.CallStub;
  192. }
  193. else if (hostAddress == DirectCallStubs.IndirectCallStub(true).ToInt64())
  194. {
  195. indirectHostAddress = IndirectHostAddress.TailCallStub;
  196. }
  197. else
  198. {
  199. throw new InvalidOperationException($"({nameof(hostAddress)} = 0x{hostAddress:X16})");
  200. }
  201. _dynamicTable.Add(new TableEntry<IndirectHostAddress>(entry, guestAddress, indirectHostAddress));
  202. }
  203. }
  204. }
  205. }