Support memory aliasing (#2954)

* Back to the origins: Make memory manager take guest PA rather than host address once again

* Direct mapping with alias support on Windows

* Fixes and remove more of the emulated shared memory

* Linux support

* Make shared and transfer memory not depend on SharedMemoryStorage

* More efficient view mapping on Windows (no more restricted to 4KB pages at a time)

* Handle potential access violations caused by partial unmap

* Implement host mapping using shared memory on Linux

* Add new GetPhysicalAddressChecked method, used to ensure the virtual address is mapped before address translation

Also align GetRef behaviour with software memory manager

* We don't need a mirrorable memory block for software memory manager mode

* Disable memory aliasing tests while we don't have shared memory support on Mac

* Shared memory & SIGBUS handler for macOS

* Fix typo + nits + re-enable memory tests

* Set MAP_JIT_DARWIN on x86 Mac too

* Add back the address space mirror

* Only set MAP_JIT_DARWIN if we are mapping as executable

* Disable aliasing tests again (still fails on Mac)

* Fix UnmapView4KB (by not casting size to int)

* Use ref counting on memory blocks to delay closing the shared memory handle until all blocks using it are disposed

* Address PR feedback

* Make RO hold a reference to the guest process memory manager to avoid early disposal

Co-authored-by: nastys <nastys@users.noreply.github.com>
This commit is contained in:
gdkchan 2022-05-02 20:30:02 -03:00 committed by GitHub
parent 4a892fbdc9
commit 95017b8c66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 2373 additions and 2155 deletions

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using static Ryujinx.Memory.MemoryManagerUnixHelper;
@ -12,15 +11,6 @@ namespace Ryujinx.Memory
[SupportedOSPlatform("macos")]
static class MemoryManagementUnix
{
private struct UnixSharedMemory
{
public IntPtr Pointer;
public ulong Size;
public IntPtr SourcePointer;
}
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>();
public static IntPtr Allocate(ulong size)
@ -69,40 +59,15 @@ namespace Ryujinx.Memory
public static bool Commit(IntPtr address, ulong size)
{
bool success = mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0;
if (success)
{
foreach (var shared in _sharedMemory)
{
if ((ulong)address + size > (ulong)shared.SourcePointer && (ulong)address < (ulong)shared.SourcePointer + shared.Size)
{
ulong sharedAddress = ((ulong)address - (ulong)shared.SourcePointer) + (ulong)shared.Pointer;
if (mprotect((IntPtr)sharedAddress, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0)
{
return false;
}
}
}
}
return success;
return mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0;
}
public static bool Decommit(IntPtr address, ulong size)
{
bool isShared;
lock (_sharedMemory)
{
isShared = _sharedMemory.Exists(x => (ulong)address >= (ulong)x.Pointer && (ulong)address + size <= (ulong)x.Pointer + x.Size);
}
// Must be writable for madvise to work properly.
mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE);
madvise(address, size, isShared ? MADV_REMOVE : MADV_DONTNEED);
madvise(address, size, MADV_REMOVE);
return mprotect(address, size, MmapProts.PROT_NONE) == 0;
}
@ -136,139 +101,83 @@ namespace Ryujinx.Memory
return false;
}
public static IntPtr Remap(IntPtr target, IntPtr source, ulong size)
public static bool Unmap(IntPtr address, ulong size)
{
int flags = 1;
if (target != IntPtr.Zero)
{
flags |= 2;
}
IntPtr result = mremap(source, 0, size, flags, target);
if (result == IntPtr.Zero)
{
throw new InvalidOperationException();
}
return result;
return munmap(address, size) == 0;
}
public static IntPtr CreateSharedMemory(ulong size, bool reserve)
public unsafe static IntPtr CreateSharedMemory(ulong size, bool reserve)
{
IntPtr result = AllocateInternal(
size,
reserve ? MmapProts.PROT_NONE : MmapProts.PROT_READ | MmapProts.PROT_WRITE,
true);
int fd;
if (result == IntPtr.Zero)
if (OperatingSystem.IsMacOS())
{
byte[] memName = Encoding.ASCII.GetBytes("Ryujinx-XXXXXX");
fixed (byte* pMemName = memName)
{
fd = shm_open((IntPtr)pMemName, 0x2 | 0x200 | 0x800 | 0x400, 384); // O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600
if (fd == -1)
{
throw new OutOfMemoryException();
}
if (shm_unlink((IntPtr)pMemName) != 0)
{
throw new OutOfMemoryException();
}
}
}
else
{
byte[] fileName = Encoding.ASCII.GetBytes("/dev/shm/Ryujinx-XXXXXX");
fixed (byte* pFileName = fileName)
{
fd = mkstemp((IntPtr)pFileName);
if (fd == -1)
{
throw new OutOfMemoryException();
}
if (unlink((IntPtr)pFileName) != 0)
{
throw new OutOfMemoryException();
}
}
}
if (ftruncate(fd, (IntPtr)size) != 0)
{
throw new OutOfMemoryException();
}
_sharedMemorySource[result] = (ulong)size;
return result;
return (IntPtr)fd;
}
public static void DestroySharedMemory(IntPtr handle)
{
lock (_sharedMemory)
{
foreach (var memory in _sharedMemory)
{
if (memory.SourcePointer == handle)
{
throw new InvalidOperationException("Shared memory cannot be destroyed unless fully unmapped.");
}
}
}
_sharedMemorySource.Remove(handle, out ulong _);
close((int)handle);
}
public static IntPtr MapSharedMemory(IntPtr handle)
public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
{
// Try find the handle for this shared memory. If it is mapped, then we want to map
// it a second time in another location.
// If it is not mapped, then its handle is the mapping.
ulong size = _sharedMemorySource[handle];
if (size == 0)
{
throw new InvalidOperationException("Shared memory cannot be mapped after its source is unmapped.");
}
lock (_sharedMemory)
{
foreach (var memory in _sharedMemory)
{
if (memory.Pointer == handle)
{
IntPtr result = AllocateInternal(
memory.Size,
MmapProts.PROT_NONE
);
if (result == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
Remap(result, handle, memory.Size);
_sharedMemory.Add(new UnixSharedMemory
{
Pointer = result,
Size = memory.Size,
SourcePointer = handle
});
return result;
}
}
_sharedMemory.Add(new UnixSharedMemory
{
Pointer = handle,
Size = size,
SourcePointer = handle
});
}
return handle;
return mmap(IntPtr.Zero, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_SHARED, (int)handle, 0);
}
public static void UnmapSharedMemory(IntPtr address)
public static void UnmapSharedMemory(IntPtr address, ulong size)
{
lock (_sharedMemory)
{
int removed = _sharedMemory.RemoveAll(memory =>
{
if (memory.Pointer == address)
{
if (memory.Pointer == memory.SourcePointer)
{
// After removing the original mapping, it cannot be mapped again.
_sharedMemorySource[memory.SourcePointer] = 0;
}
munmap(address, size);
}
Free(address);
return true;
}
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, ulong size)
{
mmap(location, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_FIXED | MmapFlags.MAP_SHARED, (int)sharedMemory, (long)srcOffset);
}
return false;
});
if (removed == 0)
{
throw new InvalidOperationException("Shared memory mapping could not be found.");
}
}
public static void UnmapView(IntPtr location, ulong size)
{
mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED, -1, 0);
}
}
}