diff options
Diffstat (limited to 'Ryujinx.Memory')
-rw-r--r-- | Ryujinx.Memory/MemoryManagementUnix.cs | 34 | ||||
-rw-r--r-- | Ryujinx.Memory/MemoryManagerUnixHelper.cs | 128 | ||||
-rw-r--r-- | Ryujinx.Memory/Ryujinx.Memory.csproj | 4 |
3 files changed, 141 insertions, 25 deletions
diff --git a/Ryujinx.Memory/MemoryManagementUnix.cs b/Ryujinx.Memory/MemoryManagementUnix.cs index edfc98dd..9e3ec48a 100644 --- a/Ryujinx.Memory/MemoryManagementUnix.cs +++ b/Ryujinx.Memory/MemoryManagementUnix.cs @@ -1,10 +1,11 @@ -using Mono.Unix.Native; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.Versioning; +using static Ryujinx.Memory.MemoryManagerUnixHelper; + namespace Ryujinx.Memory { [SupportedOSPlatform("linux")] @@ -18,15 +19,6 @@ namespace Ryujinx.Memory public IntPtr SourcePointer; } - [DllImport("libc", SetLastError = true)] - public static extern IntPtr mremap(IntPtr old_address, ulong old_size, ulong new_size, MremapFlags flags, IntPtr new_address); - - [DllImport("libc", SetLastError = true)] - public static extern int madvise(IntPtr address, ulong size, int advice); - - private const int MADV_DONTNEED = 4; - private const int MADV_REMOVE = 9; - private static readonly List<UnixSharedMemory> _sharedMemory = new List<UnixSharedMemory>(); private static readonly ConcurrentDictionary<IntPtr, ulong> _sharedMemorySource = new ConcurrentDictionary<IntPtr, ulong>(); private static readonly ConcurrentDictionary<IntPtr, ulong> _allocations = new ConcurrentDictionary<IntPtr, ulong>(); @@ -47,7 +39,7 @@ namespace Ryujinx.Memory if (shared) { - flags |= MmapFlags.MAP_SHARED | (MmapFlags)0x80000; + flags |= MmapFlags.MAP_SHARED | MmapFlags.MAP_UNLOCKED; } else { @@ -59,7 +51,7 @@ namespace Ryujinx.Memory flags |= MmapFlags.MAP_NORESERVE; } - IntPtr ptr = Syscall.mmap(IntPtr.Zero, size, prot, flags, -1, 0); + IntPtr ptr = mmap(IntPtr.Zero, size, prot, flags, -1, 0); if (ptr == new IntPtr(-1L)) { @@ -77,7 +69,7 @@ namespace Ryujinx.Memory public static bool Commit(IntPtr address, ulong size) { - bool success = Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0; + bool success = mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0; if (success) { @@ -87,7 +79,7 @@ namespace Ryujinx.Memory { ulong sharedAddress = ((ulong)address - (ulong)shared.SourcePointer) + (ulong)shared.Pointer; - if (Syscall.mprotect((IntPtr)sharedAddress, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0) + if (mprotect((IntPtr)sharedAddress, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0) { return false; } @@ -108,16 +100,16 @@ namespace Ryujinx.Memory } // Must be writable for madvise to work properly. - Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE); + mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE); madvise(address, size, isShared ? MADV_REMOVE : MADV_DONTNEED); - return Syscall.mprotect(address, size, MmapProts.PROT_NONE) == 0; + return mprotect(address, size, MmapProts.PROT_NONE) == 0; } public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission) { - return Syscall.mprotect(address, size, GetProtection(permission)) == 0; + return mprotect(address, size, GetProtection(permission)) == 0; } private static MmapProts GetProtection(MemoryPermission permission) @@ -138,7 +130,7 @@ namespace Ryujinx.Memory { if (_allocations.TryRemove(address, out ulong size)) { - return Syscall.munmap(address, size) == 0; + return munmap(address, size) == 0; } return false; @@ -146,14 +138,14 @@ namespace Ryujinx.Memory public static IntPtr Remap(IntPtr target, IntPtr source, ulong size) { - int flags = (int)MremapFlags.MREMAP_MAYMOVE; + int flags = 1; if (target != IntPtr.Zero) { flags |= 2; } - IntPtr result = mremap(source, 0, size, (MremapFlags)(flags), target); + IntPtr result = mremap(source, 0, size, flags, target); if (result == IntPtr.Zero) { diff --git a/Ryujinx.Memory/MemoryManagerUnixHelper.cs b/Ryujinx.Memory/MemoryManagerUnixHelper.cs new file mode 100644 index 00000000..4409ccee --- /dev/null +++ b/Ryujinx.Memory/MemoryManagerUnixHelper.cs @@ -0,0 +1,128 @@ +using System; +using System.Runtime.InteropServices; + +namespace Ryujinx.Memory +{ + public static class MemoryManagerUnixHelper + { + [Flags] + public enum MmapProts : uint + { + PROT_NONE = 0, + PROT_READ = 1, + PROT_WRITE = 2, + PROT_EXEC = 4 + } + + [Flags] + public enum MmapFlags : uint + { + MAP_SHARED = 1, + MAP_PRIVATE = 2, + MAP_ANONYMOUS = 4, + MAP_NORESERVE = 8, + MAP_UNLOCKED = 16 + } + + private const int MAP_ANONYMOUS_LINUX_GENERIC = 0x20; + private const int MAP_NORESERVE_LINUX_GENERIC = 0x4000; + private const int MAP_UNLOCKED_LINUX_GENERIC = 0x80000; + + private const int MAP_NORESERVE_DARWIN = 0x40; + private const int MAP_JIT_DARWIN = 0x800; + private const int MAP_ANONYMOUS_DARWIN = 0x1000; + + public const int MADV_DONTNEED = 4; + public const int MADV_REMOVE = 9; + + [DllImport("libc", EntryPoint = "mmap", SetLastError = true)] + private static extern IntPtr Internal_mmap(IntPtr address, ulong length, MmapProts prot, int flags, int fd, long offset); + + [DllImport("libc", SetLastError = true)] + public static extern int mprotect(IntPtr address, ulong length, MmapProts prot); + + [DllImport("libc", SetLastError = true)] + public static extern int munmap(IntPtr address, ulong length); + + [DllImport("libc", SetLastError = true)] + public static extern IntPtr mremap(IntPtr old_address, ulong old_size, ulong new_size, int flags, IntPtr new_address); + + [DllImport("libc", SetLastError = true)] + public static extern int madvise(IntPtr address, ulong size, int advice); + + private static int MmapFlagsToSystemFlags(MmapFlags flags) + { + int result = 0; + + if (flags.HasFlag(MmapFlags.MAP_SHARED)) + { + result |= (int)MmapFlags.MAP_SHARED; + } + + if (flags.HasFlag(MmapFlags.MAP_PRIVATE)) + { + result |= (int)MmapFlags.MAP_PRIVATE; + } + + if (flags.HasFlag(MmapFlags.MAP_ANONYMOUS)) + { + if (OperatingSystem.IsLinux()) + { + result |= MAP_ANONYMOUS_LINUX_GENERIC; + } + else if (OperatingSystem.IsMacOS()) + { + result |= MAP_ANONYMOUS_DARWIN; + } + else + { + throw new NotImplementedException(); + } + } + + if (flags.HasFlag(MmapFlags.MAP_NORESERVE)) + { + if (OperatingSystem.IsLinux()) + { + result |= MAP_NORESERVE_LINUX_GENERIC; + } + else if (OperatingSystem.IsMacOS()) + { + result |= MAP_NORESERVE_DARWIN; + } + else + { + throw new NotImplementedException(); + } + } + + if (flags.HasFlag(MmapFlags.MAP_UNLOCKED)) + { + if (OperatingSystem.IsLinux()) + { + result |= MAP_UNLOCKED_LINUX_GENERIC; + } + else if (OperatingSystem.IsMacOS()) + { + // FIXME: Doesn't exist on Darwin + } + else + { + throw new NotImplementedException(); + } + } + + if (OperatingSystem.IsMacOSVersionAtLeast(10, 14)) + { + result |= MAP_JIT_DARWIN; + } + + return result; + } + + public static IntPtr mmap(IntPtr address, ulong length, MmapProts prot, MmapFlags flags, int fd, long offset) + { + return Internal_mmap(address, length, prot, MmapFlagsToSystemFlags(flags), fd, offset); + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Memory/Ryujinx.Memory.csproj b/Ryujinx.Memory/Ryujinx.Memory.csproj index 5ece9322..0b2ed706 100644 --- a/Ryujinx.Memory/Ryujinx.Memory.csproj +++ b/Ryujinx.Memory/Ryujinx.Memory.csproj @@ -6,10 +6,6 @@ </PropertyGroup> <ItemGroup> - <PackageReference Include="Mono.Posix.NETStandard" Version="5.20.1-preview" /> - </ItemGroup> - - <ItemGroup> <ProjectReference Include="..\Ryujinx.Common\Ryujinx.Common.csproj" /> </ItemGroup> |