JumpTable.cs 6.0 KB

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