JumpTable.cs 5.8 KB

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