JumpTable.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using ARMeilleure.Memory;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. namespace ARMeilleure.Translation
  8. {
  9. class JumpTable
  10. {
  11. // The jump table is a block of (guestAddress, hostAddress) function mappings.
  12. // Each entry corresponds to one branch in a JIT compiled function. The entries are
  13. // reserved specifically for each call.
  14. // The _dependants dictionary can be used to update the hostAddress for any functions that change.
  15. public const int JumpTableStride = 16; // 8 byte guest address, 8 byte host address
  16. private const int JumpTableSize = 1048576;
  17. private const int JumpTableByteSize = JumpTableSize * JumpTableStride;
  18. // The dynamic table is also a block of (guestAddress, hostAddress) function mappings.
  19. // The main difference is that indirect calls and jumps reserve _multiple_ entries on the table.
  20. // These start out as all 0. When an indirect call is made, it tries to find the guest address on the table.
  21. // If we get to an empty address, the guestAddress is set to the call that we want.
  22. // If we get to a guestAddress that matches our own (or we just claimed it), the hostAddress is read.
  23. // If it is non-zero, we immediately branch or call the host function.
  24. // If it is 0, NativeInterface is called to find the rejited address of the call.
  25. // If none is found, the hostAddress entry stays at 0. Otherwise, the new address is placed in the entry.
  26. // If the table size is exhausted and we didn't find our desired address, we fall back to requesting
  27. // the function from the JIT.
  28. private const int DynamicTableSize = 1048576;
  29. public const int DynamicTableElems = 1;
  30. public const int DynamicTableStride = DynamicTableElems * JumpTableStride;
  31. private const int DynamicTableByteSize = DynamicTableSize * JumpTableStride * DynamicTableElems;
  32. private int _tableEnd = 0;
  33. private int _dynTableEnd = 0;
  34. private ConcurrentDictionary<ulong, TranslatedFunction> _targets;
  35. private ConcurrentDictionary<ulong, LinkedList<int>> _dependants; // TODO: Attach to TranslatedFunction or a wrapper class.
  36. private ReservedRegion _jumpRegion;
  37. private ReservedRegion _dynamicRegion;
  38. public IntPtr JumpPointer => _jumpRegion.Pointer;
  39. public IntPtr DynamicPointer => _dynamicRegion.Pointer;
  40. public JumpTable(IJitMemoryAllocator allocator)
  41. {
  42. _jumpRegion = new ReservedRegion(allocator, JumpTableByteSize);
  43. _dynamicRegion = new ReservedRegion(allocator, DynamicTableByteSize);
  44. _targets = new ConcurrentDictionary<ulong, TranslatedFunction>();
  45. _dependants = new ConcurrentDictionary<ulong, LinkedList<int>>();
  46. }
  47. public void RegisterFunction(ulong address, TranslatedFunction func)
  48. {
  49. address &= ~3UL;
  50. _targets.AddOrUpdate(address, func, (key, oldFunc) => func);
  51. long funcPtr = func.GetPointer().ToInt64();
  52. // Update all jump table entries that target this address.
  53. if (_dependants.TryGetValue(address, out LinkedList<int> myDependants))
  54. {
  55. lock (myDependants)
  56. {
  57. foreach (var entry in myDependants)
  58. {
  59. IntPtr addr = _jumpRegion.Pointer + entry * JumpTableStride;
  60. Marshal.WriteInt64(addr, 8, funcPtr);
  61. }
  62. }
  63. }
  64. }
  65. public int ReserveDynamicEntry(bool isJump)
  66. {
  67. int entry = Interlocked.Increment(ref _dynTableEnd);
  68. if (entry >= DynamicTableSize)
  69. {
  70. throw new OutOfMemoryException("JIT Dynamic Jump Table exhausted.");
  71. }
  72. _dynamicRegion.ExpandIfNeeded((ulong)((entry + 1) * DynamicTableStride));
  73. // Initialize all host function pointers to the indirect call stub.
  74. IntPtr addr = _dynamicRegion.Pointer + entry * DynamicTableStride;
  75. long stubPtr = (long)DirectCallStubs.IndirectCallStub(isJump);
  76. for (int i = 0; i < DynamicTableElems; i++)
  77. {
  78. Marshal.WriteInt64(addr, i * JumpTableStride + 8, stubPtr);
  79. }
  80. return entry;
  81. }
  82. public int ReserveTableEntry(long ownerAddress, long address, bool isJump)
  83. {
  84. int entry = Interlocked.Increment(ref _tableEnd);
  85. if (entry >= JumpTableSize)
  86. {
  87. throw new OutOfMemoryException("JIT Direct Jump Table exhausted.");
  88. }
  89. _jumpRegion.ExpandIfNeeded((ulong)((entry + 1) * JumpTableStride));
  90. // Is the address we have already registered? If so, put the function address in the jump table.
  91. // If not, it will point to the direct call stub.
  92. long value = (long)DirectCallStubs.DirectCallStub(isJump);
  93. if (_targets.TryGetValue((ulong)address, out TranslatedFunction func))
  94. {
  95. value = func.GetPointer().ToInt64();
  96. }
  97. // Make sure changes to the function at the target address update this jump table entry.
  98. LinkedList<int> targetDependants = _dependants.GetOrAdd((ulong)address, (addr) => new LinkedList<int>());
  99. lock (targetDependants)
  100. {
  101. targetDependants.AddLast(entry);
  102. }
  103. IntPtr addr = _jumpRegion.Pointer + entry * JumpTableStride;
  104. Marshal.WriteInt64(addr, 0, address);
  105. Marshal.WriteInt64(addr, 8, value);
  106. return entry;
  107. }
  108. }
  109. }