JitCache.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using ARMeilleure.CodeGen;
  2. using ARMeilleure.CodeGen.Unwinding;
  3. using ARMeilleure.Memory;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8. namespace ARMeilleure.Translation.Cache
  9. {
  10. static class JitCache
  11. {
  12. private const int PageSize = 4 * 1024;
  13. private const int PageMask = PageSize - 1;
  14. private const int CodeAlignment = 4; // Bytes.
  15. private const int CacheSize = 2047 * 1024 * 1024;
  16. private static ReservedRegion _jitRegion;
  17. private static CacheMemoryAllocator _cacheAllocator;
  18. private static readonly List<CacheEntry> _cacheEntries = new List<CacheEntry>();
  19. private static readonly object _lock = new object();
  20. private static bool _initialized;
  21. public static void Initialize(IJitMemoryAllocator allocator)
  22. {
  23. if (_initialized) return;
  24. lock (_lock)
  25. {
  26. if (_initialized) return;
  27. _jitRegion = new ReservedRegion(allocator, CacheSize);
  28. _cacheAllocator = new CacheMemoryAllocator(CacheSize);
  29. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  30. {
  31. JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize, _jitRegion.Pointer + Allocate(PageSize));
  32. }
  33. _initialized = true;
  34. }
  35. }
  36. public static IntPtr Map(CompiledFunction func)
  37. {
  38. byte[] code = func.Code;
  39. lock (_lock)
  40. {
  41. Debug.Assert(_initialized);
  42. int funcOffset = Allocate(code.Length);
  43. IntPtr funcPtr = _jitRegion.Pointer + funcOffset;
  44. ReprotectAsWritable(funcOffset, code.Length);
  45. Marshal.Copy(code, 0, funcPtr, code.Length);
  46. ReprotectAsExecutable(funcOffset, code.Length);
  47. Add(funcOffset, code.Length, func.UnwindInfo);
  48. return funcPtr;
  49. }
  50. }
  51. public static void Unmap(IntPtr pointer)
  52. {
  53. lock (_lock)
  54. {
  55. Debug.Assert(_initialized);
  56. int funcOffset = (int)(pointer.ToInt64() - _jitRegion.Pointer.ToInt64());
  57. bool result = TryFind(funcOffset, out CacheEntry entry);
  58. Debug.Assert(result);
  59. _cacheAllocator.Free(funcOffset, AlignCodeSize(entry.Size));
  60. Remove(funcOffset);
  61. }
  62. }
  63. private static void ReprotectAsWritable(int offset, int size)
  64. {
  65. int endOffs = offset + size;
  66. int regionStart = offset & ~PageMask;
  67. int regionEnd = (endOffs + PageMask) & ~PageMask;
  68. _jitRegion.Block.MapAsRwx((ulong)regionStart, (ulong)(regionEnd - regionStart));
  69. }
  70. private static void ReprotectAsExecutable(int offset, int size)
  71. {
  72. int endOffs = offset + size;
  73. int regionStart = offset & ~PageMask;
  74. int regionEnd = (endOffs + PageMask) & ~PageMask;
  75. _jitRegion.Block.MapAsRx((ulong)regionStart, (ulong)(regionEnd - regionStart));
  76. }
  77. private static int Allocate(int codeSize)
  78. {
  79. codeSize = AlignCodeSize(codeSize);
  80. int allocOffset = _cacheAllocator.Allocate(codeSize);
  81. if (allocOffset < 0)
  82. {
  83. throw new OutOfMemoryException("JIT Cache exhausted.");
  84. }
  85. _jitRegion.ExpandIfNeeded((ulong)allocOffset + (ulong)codeSize);
  86. return allocOffset;
  87. }
  88. private static int AlignCodeSize(int codeSize)
  89. {
  90. return checked(codeSize + (CodeAlignment - 1)) & ~(CodeAlignment - 1);
  91. }
  92. private static void Add(int offset, int size, UnwindInfo unwindInfo)
  93. {
  94. CacheEntry entry = new CacheEntry(offset, size, unwindInfo);
  95. int index = _cacheEntries.BinarySearch(entry);
  96. if (index < 0)
  97. {
  98. index = ~index;
  99. }
  100. _cacheEntries.Insert(index, entry);
  101. }
  102. private static void Remove(int offset)
  103. {
  104. int index = _cacheEntries.BinarySearch(new CacheEntry(offset, 0, default));
  105. if (index < 0)
  106. {
  107. index = ~index - 1;
  108. }
  109. if (index >= 0)
  110. {
  111. _cacheEntries.RemoveAt(index);
  112. }
  113. }
  114. public static bool TryFind(int offset, out CacheEntry entry)
  115. {
  116. lock (_lock)
  117. {
  118. int index = _cacheEntries.BinarySearch(new CacheEntry(offset, 0, default));
  119. if (index < 0)
  120. {
  121. index = ~index - 1;
  122. }
  123. if (index >= 0)
  124. {
  125. entry = _cacheEntries[index];
  126. return true;
  127. }
  128. }
  129. entry = default;
  130. return false;
  131. }
  132. }
  133. }