PtcJumpTable.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. namespace ARMeilleure.Translation.PTC
  6. {
  7. [Serializable]
  8. class PtcJumpTable
  9. {
  10. private readonly List<KeyValuePair<long, DirectHostAddress>> _jumpTable;
  11. private readonly List<KeyValuePair<long, IndirectHostAddress>> _dynamicTable;
  12. private readonly List<ulong> _targets;
  13. private readonly Dictionary<ulong, LinkedList<int>> _dependants;
  14. public int TableEnd => _jumpTable.Count;
  15. public int DynTableEnd => _dynamicTable.Count;
  16. public List<ulong> Targets => _targets;
  17. public Dictionary<ulong, LinkedList<int>> Dependants => _dependants;
  18. public PtcJumpTable()
  19. {
  20. _jumpTable = new List<KeyValuePair<long, DirectHostAddress>>();
  21. _dynamicTable = new List<KeyValuePair<long, IndirectHostAddress>>();
  22. _targets = new List<ulong>();
  23. _dependants = new Dictionary<ulong, LinkedList<int>>();
  24. }
  25. public void Initialize(JumpTable jumpTable)
  26. {
  27. _targets.Clear();
  28. foreach (ulong guestAddress in jumpTable.Targets.Keys)
  29. {
  30. _targets.Add(guestAddress);
  31. }
  32. _dependants.Clear();
  33. foreach (var item in jumpTable.Dependants)
  34. {
  35. _dependants.Add(item.Key, new LinkedList<int>(item.Value));
  36. }
  37. }
  38. public void Clear()
  39. {
  40. _jumpTable.Clear();
  41. _dynamicTable.Clear();
  42. _targets.Clear();
  43. _dependants.Clear();
  44. }
  45. public void WriteJumpTable(JumpTable jumpTable, ConcurrentDictionary<ulong, TranslatedFunction> funcs)
  46. {
  47. jumpTable.ExpandIfNeededJumpTable(TableEnd);
  48. int entry = 0;
  49. foreach (var item in _jumpTable)
  50. {
  51. entry += 1;
  52. long guestAddress = item.Key;
  53. DirectHostAddress directHostAddress = item.Value;
  54. long hostAddress;
  55. if (directHostAddress == DirectHostAddress.CallStub)
  56. {
  57. hostAddress = DirectCallStubs.DirectCallStub(false).ToInt64();
  58. }
  59. else if (directHostAddress == DirectHostAddress.TailCallStub)
  60. {
  61. hostAddress = DirectCallStubs.DirectCallStub(true).ToInt64();
  62. }
  63. else if (directHostAddress == DirectHostAddress.Host)
  64. {
  65. if (funcs.TryGetValue((ulong)guestAddress, out TranslatedFunction func))
  66. {
  67. hostAddress = func.FuncPtr.ToInt64();
  68. }
  69. else
  70. {
  71. throw new KeyNotFoundException($"({nameof(guestAddress)} = 0x{(ulong)guestAddress:X16})");
  72. }
  73. }
  74. else
  75. {
  76. throw new InvalidOperationException(nameof(directHostAddress));
  77. }
  78. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  79. Marshal.WriteInt64(addr, 0, guestAddress);
  80. Marshal.WriteInt64(addr, 8, hostAddress);
  81. }
  82. }
  83. public void WriteDynamicTable(JumpTable jumpTable)
  84. {
  85. if (JumpTable.DynamicTableElems > 1)
  86. {
  87. throw new NotSupportedException();
  88. }
  89. jumpTable.ExpandIfNeededDynamicTable(DynTableEnd);
  90. int entry = 0;
  91. foreach (var item in _dynamicTable)
  92. {
  93. entry += 1;
  94. long guestAddress = item.Key;
  95. IndirectHostAddress indirectHostAddress = item.Value;
  96. long hostAddress;
  97. if (indirectHostAddress == IndirectHostAddress.CallStub)
  98. {
  99. hostAddress = DirectCallStubs.IndirectCallStub(false).ToInt64();
  100. }
  101. else if (indirectHostAddress == IndirectHostAddress.TailCallStub)
  102. {
  103. hostAddress = DirectCallStubs.IndirectCallStub(true).ToInt64();
  104. }
  105. else
  106. {
  107. throw new InvalidOperationException(nameof(indirectHostAddress));
  108. }
  109. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  110. Marshal.WriteInt64(addr, 0, guestAddress);
  111. Marshal.WriteInt64(addr, 8, hostAddress);
  112. }
  113. }
  114. public void ReadJumpTable(JumpTable jumpTable)
  115. {
  116. _jumpTable.Clear();
  117. for (int entry = 1; entry <= jumpTable.TableEnd; entry++)
  118. {
  119. IntPtr addr = jumpTable.GetEntryAddressJumpTable(entry);
  120. long guestAddress = Marshal.ReadInt64(addr, 0);
  121. long hostAddress = Marshal.ReadInt64(addr, 8);
  122. DirectHostAddress directHostAddress;
  123. if (hostAddress == DirectCallStubs.DirectCallStub(false).ToInt64())
  124. {
  125. directHostAddress = DirectHostAddress.CallStub;
  126. }
  127. else if (hostAddress == DirectCallStubs.DirectCallStub(true).ToInt64())
  128. {
  129. directHostAddress = DirectHostAddress.TailCallStub;
  130. }
  131. else
  132. {
  133. directHostAddress = DirectHostAddress.Host;
  134. }
  135. _jumpTable.Add(new KeyValuePair<long, DirectHostAddress>(guestAddress, directHostAddress));
  136. }
  137. }
  138. public void ReadDynamicTable(JumpTable jumpTable)
  139. {
  140. if (JumpTable.DynamicTableElems > 1)
  141. {
  142. throw new NotSupportedException();
  143. }
  144. _dynamicTable.Clear();
  145. for (int entry = 1; entry <= jumpTable.DynTableEnd; entry++)
  146. {
  147. IntPtr addr = jumpTable.GetEntryAddressDynamicTable(entry);
  148. long guestAddress = Marshal.ReadInt64(addr, 0);
  149. long hostAddress = Marshal.ReadInt64(addr, 8);
  150. IndirectHostAddress indirectHostAddress;
  151. if (hostAddress == DirectCallStubs.IndirectCallStub(false).ToInt64())
  152. {
  153. indirectHostAddress = IndirectHostAddress.CallStub;
  154. }
  155. else if (hostAddress == DirectCallStubs.IndirectCallStub(true).ToInt64())
  156. {
  157. indirectHostAddress = IndirectHostAddress.TailCallStub;
  158. }
  159. else
  160. {
  161. throw new InvalidOperationException($"({nameof(hostAddress)} = 0x{hostAddress:X16})");
  162. }
  163. _dynamicTable.Add(new KeyValuePair<long, IndirectHostAddress>(guestAddress, indirectHostAddress));
  164. }
  165. }
  166. private enum DirectHostAddress
  167. {
  168. CallStub,
  169. TailCallStub,
  170. Host
  171. }
  172. private enum IndirectHostAddress
  173. {
  174. CallStub,
  175. TailCallStub
  176. }
  177. }
  178. }