JitCache.cs 3.8 KB

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