MemoryManagementUnix.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Runtime.Versioning;
  4. using System.Text;
  5. using static Ryujinx.Memory.MemoryManagerUnixHelper;
  6. namespace Ryujinx.Memory
  7. {
  8. [SupportedOSPlatform("linux")]
  9. [SupportedOSPlatform("macos")]
  10. static class MemoryManagementUnix
  11. {
  12. private static readonly ConcurrentDictionary<IntPtr, ulong> _allocations = new ConcurrentDictionary<IntPtr, ulong>();
  13. public static IntPtr Allocate(ulong size, bool forJit)
  14. {
  15. return AllocateInternal(size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, forJit);
  16. }
  17. public static IntPtr Reserve(ulong size, bool forJit)
  18. {
  19. return AllocateInternal(size, MmapProts.PROT_NONE, forJit);
  20. }
  21. private static IntPtr AllocateInternal(ulong size, MmapProts prot, bool forJit, bool shared = false)
  22. {
  23. MmapFlags flags = MmapFlags.MAP_ANONYMOUS;
  24. if (shared)
  25. {
  26. flags |= MmapFlags.MAP_SHARED | MmapFlags.MAP_UNLOCKED;
  27. }
  28. else
  29. {
  30. flags |= MmapFlags.MAP_PRIVATE;
  31. }
  32. if (prot == MmapProts.PROT_NONE)
  33. {
  34. flags |= MmapFlags.MAP_NORESERVE;
  35. }
  36. if (OperatingSystem.IsMacOSVersionAtLeast(10, 14) && forJit)
  37. {
  38. flags |= MmapFlags.MAP_JIT_DARWIN;
  39. if (prot == (MmapProts.PROT_READ | MmapProts.PROT_WRITE))
  40. {
  41. prot |= MmapProts.PROT_EXEC;
  42. }
  43. }
  44. IntPtr ptr = mmap(IntPtr.Zero, size, prot, flags, -1, 0);
  45. if (ptr == new IntPtr(-1L))
  46. {
  47. throw new OutOfMemoryException();
  48. }
  49. if (!_allocations.TryAdd(ptr, size))
  50. {
  51. // This should be impossible, kernel shouldn't return an already mapped address.
  52. throw new InvalidOperationException();
  53. }
  54. return ptr;
  55. }
  56. public static bool Commit(IntPtr address, ulong size, bool forJit)
  57. {
  58. MmapProts prot = MmapProts.PROT_READ | MmapProts.PROT_WRITE;
  59. if (OperatingSystem.IsMacOSVersionAtLeast(10, 14) && forJit)
  60. {
  61. prot |= MmapProts.PROT_EXEC;
  62. }
  63. return mprotect(address, size, prot) == 0;
  64. }
  65. public static bool Decommit(IntPtr address, ulong size)
  66. {
  67. // Must be writable for madvise to work properly.
  68. mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE);
  69. madvise(address, size, MADV_REMOVE);
  70. return mprotect(address, size, MmapProts.PROT_NONE) == 0;
  71. }
  72. public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission)
  73. {
  74. return mprotect(address, size, GetProtection(permission)) == 0;
  75. }
  76. private static MmapProts GetProtection(MemoryPermission permission)
  77. {
  78. return permission switch
  79. {
  80. MemoryPermission.None => MmapProts.PROT_NONE,
  81. MemoryPermission.Read => MmapProts.PROT_READ,
  82. MemoryPermission.ReadAndWrite => MmapProts.PROT_READ | MmapProts.PROT_WRITE,
  83. MemoryPermission.ReadAndExecute => MmapProts.PROT_READ | MmapProts.PROT_EXEC,
  84. MemoryPermission.ReadWriteExecute => MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC,
  85. MemoryPermission.Execute => MmapProts.PROT_EXEC,
  86. _ => throw new MemoryProtectionException(permission)
  87. };
  88. }
  89. public static bool Free(IntPtr address)
  90. {
  91. if (_allocations.TryRemove(address, out ulong size))
  92. {
  93. return munmap(address, size) == 0;
  94. }
  95. return false;
  96. }
  97. public static bool Unmap(IntPtr address, ulong size)
  98. {
  99. return munmap(address, size) == 0;
  100. }
  101. public unsafe static IntPtr CreateSharedMemory(ulong size, bool reserve)
  102. {
  103. int fd;
  104. if (OperatingSystem.IsMacOS())
  105. {
  106. byte[] memName = Encoding.ASCII.GetBytes("Ryujinx-XXXXXX");
  107. fixed (byte* pMemName = memName)
  108. {
  109. fd = shm_open((IntPtr)pMemName, 0x2 | 0x200 | 0x800 | 0x400, 384); // O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600
  110. if (fd == -1)
  111. {
  112. throw new OutOfMemoryException();
  113. }
  114. if (shm_unlink((IntPtr)pMemName) != 0)
  115. {
  116. throw new OutOfMemoryException();
  117. }
  118. }
  119. }
  120. else
  121. {
  122. byte[] fileName = Encoding.ASCII.GetBytes("/dev/shm/Ryujinx-XXXXXX");
  123. fixed (byte* pFileName = fileName)
  124. {
  125. fd = mkstemp((IntPtr)pFileName);
  126. if (fd == -1)
  127. {
  128. throw new OutOfMemoryException();
  129. }
  130. if (unlink((IntPtr)pFileName) != 0)
  131. {
  132. throw new OutOfMemoryException();
  133. }
  134. }
  135. }
  136. if (ftruncate(fd, (IntPtr)size) != 0)
  137. {
  138. throw new OutOfMemoryException();
  139. }
  140. return (IntPtr)fd;
  141. }
  142. public static void DestroySharedMemory(IntPtr handle)
  143. {
  144. close((int)handle);
  145. }
  146. public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
  147. {
  148. return mmap(IntPtr.Zero, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_SHARED, (int)handle, 0);
  149. }
  150. public static void UnmapSharedMemory(IntPtr address, ulong size)
  151. {
  152. munmap(address, size);
  153. }
  154. public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, ulong size)
  155. {
  156. mmap(location, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_FIXED | MmapFlags.MAP_SHARED, (int)sharedMemory, (long)srcOffset);
  157. }
  158. public static void UnmapView(IntPtr location, ulong size)
  159. {
  160. mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED | MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_NORESERVE, -1, 0);
  161. }
  162. }
  163. }