MemoryManagementUnix.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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)
  14. {
  15. return AllocateInternal(size, MmapProts.PROT_READ | MmapProts.PROT_WRITE);
  16. }
  17. public static IntPtr Reserve(ulong size)
  18. {
  19. return AllocateInternal(size, MmapProts.PROT_NONE);
  20. }
  21. private static IntPtr AllocateInternal(ulong size, MmapProts prot, 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. IntPtr ptr = mmap(IntPtr.Zero, size, prot, flags, -1, 0);
  37. if (ptr == new IntPtr(-1L))
  38. {
  39. throw new OutOfMemoryException();
  40. }
  41. if (!_allocations.TryAdd(ptr, size))
  42. {
  43. // This should be impossible, kernel shouldn't return an already mapped address.
  44. throw new InvalidOperationException();
  45. }
  46. return ptr;
  47. }
  48. public static bool Commit(IntPtr address, ulong size)
  49. {
  50. return mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0;
  51. }
  52. public static bool Decommit(IntPtr address, ulong size)
  53. {
  54. // Must be writable for madvise to work properly.
  55. mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE);
  56. madvise(address, size, MADV_REMOVE);
  57. return mprotect(address, size, MmapProts.PROT_NONE) == 0;
  58. }
  59. public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission)
  60. {
  61. return mprotect(address, size, GetProtection(permission)) == 0;
  62. }
  63. private static MmapProts GetProtection(MemoryPermission permission)
  64. {
  65. return permission switch
  66. {
  67. MemoryPermission.None => MmapProts.PROT_NONE,
  68. MemoryPermission.Read => MmapProts.PROT_READ,
  69. MemoryPermission.ReadAndWrite => MmapProts.PROT_READ | MmapProts.PROT_WRITE,
  70. MemoryPermission.ReadAndExecute => MmapProts.PROT_READ | MmapProts.PROT_EXEC,
  71. MemoryPermission.ReadWriteExecute => MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC,
  72. MemoryPermission.Execute => MmapProts.PROT_EXEC,
  73. _ => throw new MemoryProtectionException(permission)
  74. };
  75. }
  76. public static bool Free(IntPtr address)
  77. {
  78. if (_allocations.TryRemove(address, out ulong size))
  79. {
  80. return munmap(address, size) == 0;
  81. }
  82. return false;
  83. }
  84. public static bool Unmap(IntPtr address, ulong size)
  85. {
  86. return munmap(address, size) == 0;
  87. }
  88. public unsafe static IntPtr CreateSharedMemory(ulong size, bool reserve)
  89. {
  90. int fd;
  91. if (OperatingSystem.IsMacOS())
  92. {
  93. byte[] memName = Encoding.ASCII.GetBytes("Ryujinx-XXXXXX");
  94. fixed (byte* pMemName = memName)
  95. {
  96. fd = shm_open((IntPtr)pMemName, 0x2 | 0x200 | 0x800 | 0x400, 384); // O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600
  97. if (fd == -1)
  98. {
  99. throw new OutOfMemoryException();
  100. }
  101. if (shm_unlink((IntPtr)pMemName) != 0)
  102. {
  103. throw new OutOfMemoryException();
  104. }
  105. }
  106. }
  107. else
  108. {
  109. byte[] fileName = Encoding.ASCII.GetBytes("/dev/shm/Ryujinx-XXXXXX");
  110. fixed (byte* pFileName = fileName)
  111. {
  112. fd = mkstemp((IntPtr)pFileName);
  113. if (fd == -1)
  114. {
  115. throw new OutOfMemoryException();
  116. }
  117. if (unlink((IntPtr)pFileName) != 0)
  118. {
  119. throw new OutOfMemoryException();
  120. }
  121. }
  122. }
  123. if (ftruncate(fd, (IntPtr)size) != 0)
  124. {
  125. throw new OutOfMemoryException();
  126. }
  127. return (IntPtr)fd;
  128. }
  129. public static void DestroySharedMemory(IntPtr handle)
  130. {
  131. close((int)handle);
  132. }
  133. public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
  134. {
  135. return mmap(IntPtr.Zero, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_SHARED, (int)handle, 0);
  136. }
  137. public static void UnmapSharedMemory(IntPtr address, ulong size)
  138. {
  139. munmap(address, size);
  140. }
  141. public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, ulong size)
  142. {
  143. mmap(location, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_FIXED | MmapFlags.MAP_SHARED, (int)sharedMemory, (long)srcOffset);
  144. }
  145. public static void UnmapView(IntPtr location, ulong size)
  146. {
  147. mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED | MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_NORESERVE, -1, 0);
  148. }
  149. }
  150. }