JumpTable.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ARMeilleure.Diagnostics;
  2. using ARMeilleure.Memory;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. namespace ARMeilleure.Translation
  10. {
  11. using PTC;
  12. class JumpTable
  13. {
  14. // The jump table is a block of (guestAddress, hostAddress) function mappings.
  15. // Each entry corresponds to one branch in a JIT compiled function. The entries are
  16. // reserved specifically for each call.
  17. // The _dependants dictionary can be used to update the hostAddress for any functions that change.
  18. public const int JumpTableStride = 16; // 8 byte guest address, 8 byte host address.
  19. private const int JumpTableSize = 1048576;
  20. private const int JumpTableByteSize = JumpTableSize * JumpTableStride;
  21. // The dynamic table is also a block of (guestAddress, hostAddress) function mappings.
  22. // The main difference is that indirect calls and jumps reserve _multiple_ entries on the table.
  23. // These start out as all 0. When an indirect call is made, it tries to find the guest address on the table.
  24. // If we get to an empty address, the guestAddress is set to the call that we want.
  25. // If we get to a guestAddress that matches our own (or we just claimed it), the hostAddress is read.
  26. // If it is non-zero, we immediately branch or call the host function.
  27. // If it is 0, NativeInterface is called to find the rejited address of the call.
  28. // If none is found, the hostAddress entry stays at 0. Otherwise, the new address is placed in the entry.
  29. // If the table size is exhausted and we didn't find our desired address, we fall back to requesting
  30. // the function from the JIT.
  31. public const int DynamicTableElems = 1;
  32. public const int DynamicTableStride = DynamicTableElems * JumpTableStride;
  33. private const int DynamicTableSize = 1048576;
  34. private const int DynamicTableByteSize = DynamicTableSize * DynamicTableStride;
  35. private readonly ReservedRegion _jumpRegion;
  36. private readonly ReservedRegion _dynamicRegion;
  37. private int _tableEnd = 0;
  38. private int _dynTableEnd = 0;
  39. public IntPtr JumpPointer => _jumpRegion.Pointer;
  40. public IntPtr DynamicPointer => _dynamicRegion.Pointer;
  41. public int TableEnd => _tableEnd;
  42. public int DynTableEnd => _dynTableEnd;
  43. public ConcurrentDictionary<ulong, TranslatedFunction> Targets { get; }
  44. public ConcurrentDictionary<ulong, LinkedList<int>> Dependants { get; } // TODO: Attach to TranslatedFunction or a wrapper class.
  45. public JumpTable(IJitMemoryAllocator allocator)
  46. {
  47. _jumpRegion = new ReservedRegion(allocator, JumpTableByteSize);
  48. _dynamicRegion = new ReservedRegion(allocator, DynamicTableByteSize);
  49. Targets = new ConcurrentDictionary<ulong, TranslatedFunction>();
  50. Dependants = new ConcurrentDictionary<ulong, LinkedList<int>>();
  51. Symbols.Add((ulong)_jumpRegion.Pointer.ToInt64(), JumpTableByteSize, JumpTableStride, "JMP_TABLE");
  52. Symbols.Add((ulong)_dynamicRegion.Pointer.ToInt64(), DynamicTableByteSize, DynamicTableStride, "DYN_TABLE");
  53. }
  54. public void Initialize(PtcJumpTable ptcJumpTable, ConcurrentDictionary<ulong, TranslatedFunction> funcs)
  55. {
  56. _tableEnd = ptcJumpTable.TableEnd;
  57. _dynTableEnd = ptcJumpTable.DynTableEnd;
  58. foreach (ulong guestAddress in ptcJumpTable.Targets)
  59. {
  60. if (funcs.TryGetValue(guestAddress, out TranslatedFunction func))
  61. {
  62. Targets.TryAdd(guestAddress, func);
  63. }
  64. else
  65. {
  66. throw new KeyNotFoundException($"({nameof(guestAddress)} = 0x{guestAddress:X16})");
  67. }
  68. }
  69. foreach (var item in ptcJumpTable.Dependants)
  70. {
  71. Dependants.TryAdd(item.Key, new LinkedList<int>(item.Value));
  72. }
  73. }
  74. public void RegisterFunction(ulong address, TranslatedFunction func)
  75. {
  76. address &= ~3UL;
  77. Targets.AddOrUpdate(address, func, (key, oldFunc) => func);
  78. long funcPtr = func.FuncPtr.ToInt64();
  79. // Update all jump table entries that target this address.
  80. if (Dependants.TryGetValue(address, out LinkedList<int> myDependants))
  81. {
  82. lock (myDependants)
  83. {
  84. foreach (int entry in myDependants)
  85. {
  86. IntPtr addr = GetEntryAddressJumpTable(entry);
  87. Marshal.WriteInt64(addr, 8, funcPtr);
  88. }
  89. }
  90. }
  91. }
  92. public int ReserveTableEntry(long ownerAddress, long address, bool isJump)
  93. {
  94. int entry = Interlocked.Increment(ref _tableEnd);
  95. ExpandIfNeededJumpTable(entry);
  96. // Is the address we have already registered? If so, put the function address in the jump table.
  97. // If not, it will point to the direct call stub.
  98. long value = DirectCallStubs.DirectCallStub(isJump).ToInt64();
  99. if (Targets.TryGetValue((ulong)address, out TranslatedFunction func))
  100. {
  101. value = func.FuncPtr.ToInt64();
  102. }
  103. // Make sure changes to the function at the target address update this jump table entry.
  104. LinkedList<int> targetDependants = Dependants.GetOrAdd((ulong)address, (addr) => new LinkedList<int>());
  105. lock (targetDependants)
  106. {
  107. targetDependants.AddLast(entry);
  108. }
  109. IntPtr addr = GetEntryAddressJumpTable(entry);
  110. Marshal.WriteInt64(addr, 0, address);
  111. Marshal.WriteInt64(addr, 8, value);
  112. return entry;
  113. }
  114. public int ReserveDynamicEntry(bool isJump)
  115. {
  116. int entry = Interlocked.Increment(ref _dynTableEnd);
  117. ExpandIfNeededDynamicTable(entry);
  118. // Initialize all host function pointers to the indirect call stub.
  119. IntPtr addr = GetEntryAddressDynamicTable(entry);
  120. long stubPtr = DirectCallStubs.IndirectCallStub(isJump).ToInt64();
  121. for (int i = 0; i < DynamicTableElems; i++)
  122. {
  123. Marshal.WriteInt64(addr, i * JumpTableStride + 8, stubPtr);
  124. }
  125. return entry;
  126. }
  127. public void ExpandIfNeededJumpTable(int entries)
  128. {
  129. Debug.Assert(entries > 0);
  130. if (entries < JumpTableSize)
  131. {
  132. _jumpRegion.ExpandIfNeeded((ulong)((entries + 1) * JumpTableStride));
  133. }
  134. else
  135. {
  136. throw new OutOfMemoryException("JIT Direct Jump Table exhausted.");
  137. }
  138. }
  139. public void ExpandIfNeededDynamicTable(int entries)
  140. {
  141. Debug.Assert(entries > 0);
  142. if (entries < DynamicTableSize)
  143. {
  144. _dynamicRegion.ExpandIfNeeded((ulong)((entries + 1) * DynamicTableStride));
  145. }
  146. else
  147. {
  148. throw new OutOfMemoryException("JIT Dynamic Jump Table exhausted.");
  149. }
  150. }
  151. public IntPtr GetEntryAddressJumpTable(int entry)
  152. {
  153. Debug.Assert(entry >= 1 && entry <= _tableEnd);
  154. return _jumpRegion.Pointer + entry * JumpTableStride;
  155. }
  156. public IntPtr GetEntryAddressDynamicTable(int entry)
  157. {
  158. Debug.Assert(entry >= 1 && entry <= _dynTableEnd);
  159. return _dynamicRegion.Pointer + entry * DynamicTableStride;
  160. }
  161. }
  162. }