JitCache.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using ARMeilleure.CodeGen;
  2. using ARMeilleure.Memory;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. namespace ARMeilleure.Translation
  8. {
  9. static class JitCache
  10. {
  11. private const int PageSize = 4 * 1024;
  12. private const int PageMask = PageSize - 1;
  13. private const int CodeAlignment = 4; // Bytes.
  14. private const int CacheSize = 2047 * 1024 * 1024;
  15. private static ReservedRegion _jitRegion;
  16. private static int _offset;
  17. private static readonly List<JitCacheEntry> _cacheEntries = new List<JitCacheEntry>();
  18. private static readonly object _lock = new object();
  19. private static bool _initialized;
  20. public static void Initialize(IJitMemoryAllocator allocator)
  21. {
  22. if (_initialized) return;
  23. lock (_lock)
  24. {
  25. if (_initialized) return;
  26. _jitRegion = new ReservedRegion(allocator, CacheSize);
  27. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  28. {
  29. _jitRegion.ExpandIfNeeded((ulong)PageSize);
  30. JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize);
  31. // The first page is used for the table based SEH structs.
  32. _offset = PageSize;
  33. }
  34. _initialized = true;
  35. }
  36. }
  37. public static IntPtr Map(CompiledFunction func)
  38. {
  39. byte[] code = func.Code;
  40. lock (_lock)
  41. {
  42. Debug.Assert(_initialized);
  43. int funcOffset = Allocate(code.Length);
  44. IntPtr funcPtr = _jitRegion.Pointer + funcOffset;
  45. Marshal.Copy(code, 0, funcPtr, code.Length);
  46. ReprotectRange(funcOffset, code.Length);
  47. Add(new JitCacheEntry(funcOffset, code.Length, func.UnwindInfo));
  48. return funcPtr;
  49. }
  50. }
  51. private static void ReprotectRange(int offset, int size)
  52. {
  53. // Map pages that are already full as RX.
  54. // Map pages that are not full yet as RWX.
  55. // On unix, the address must be page aligned.
  56. int endOffs = offset + size;
  57. int pageStart = offset & ~PageMask;
  58. int pageEnd = endOffs & ~PageMask;
  59. int fullPagesSize = pageEnd - pageStart;
  60. if (fullPagesSize != 0)
  61. {
  62. _jitRegion.Block.MapAsRx((ulong)pageStart, (ulong)fullPagesSize);
  63. }
  64. int remaining = endOffs - pageEnd;
  65. if (remaining != 0)
  66. {
  67. _jitRegion.Block.MapAsRwx((ulong)pageEnd, (ulong)remaining);
  68. }
  69. }
  70. private static int Allocate(int codeSize)
  71. {
  72. codeSize = checked(codeSize + (CodeAlignment - 1)) & ~(CodeAlignment - 1);
  73. int allocOffset = _offset;
  74. _offset += codeSize;
  75. if (_offset > CacheSize)
  76. {
  77. throw new OutOfMemoryException("JIT Cache exhausted.");
  78. }
  79. _jitRegion.ExpandIfNeeded((ulong)_offset);
  80. return allocOffset;
  81. }
  82. private static void Add(JitCacheEntry entry)
  83. {
  84. _cacheEntries.Add(entry);
  85. }
  86. public static bool TryFind(int offset, out JitCacheEntry entry)
  87. {
  88. lock (_lock)
  89. {
  90. foreach (JitCacheEntry cacheEntry in _cacheEntries)
  91. {
  92. int endOffset = cacheEntry.Offset + cacheEntry.Size;
  93. if (offset >= cacheEntry.Offset && offset < endOffset)
  94. {
  95. entry = cacheEntry;
  96. return true;
  97. }
  98. }
  99. }
  100. entry = default;
  101. return false;
  102. }
  103. }
  104. }