Ryujinx/Ryujinx.Memory/MemoryManagementWindows.cs
gdkchan 95017b8c66
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>
2022-05-02 20:30:02 -03:00

212 lines
No EOL
6.7 KiB
C#

using Ryujinx.Memory.WindowsShared;
using System;
using System.Runtime.Versioning;
namespace Ryujinx.Memory
{
[SupportedOSPlatform("windows")]
static class MemoryManagementWindows
{
private const int PageSize = 0x1000;
private static readonly PlaceholderManager _placeholders = new PlaceholderManager();
public static IntPtr Allocate(IntPtr size)
{
return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit);
}
public static IntPtr Reserve(IntPtr size, bool viewCompatible)
{
if (viewCompatible)
{
IntPtr baseAddress = AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);
_placeholders.ReserveRange((ulong)baseAddress, (ulong)size);
return baseAddress;
}
return AllocateInternal(size, AllocationType.Reserve);
}
private static IntPtr AllocateInternal(IntPtr size, AllocationType flags = 0)
{
IntPtr ptr = WindowsApi.VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
private static IntPtr AllocateInternal2(IntPtr size, AllocationType flags = 0)
{
IntPtr ptr = WindowsApi.VirtualAlloc2(WindowsApi.CurrentProcessHandle, IntPtr.Zero, size, flags, MemoryProtection.NoAccess, IntPtr.Zero, 0);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
public static bool Commit(IntPtr location, IntPtr size)
{
return WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero;
}
public static bool Decommit(IntPtr location, IntPtr size)
{
return WindowsApi.VirtualFree(location, size, AllocationType.Decommit);
}
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
{
_placeholders.MapView(sharedMemory, srcOffset, location, size);
}
public static void MapView4KB(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
{
ulong uaddress = (ulong)location;
ulong usize = (ulong)size;
IntPtr endLocation = (IntPtr)(uaddress + usize);
while (location != endLocation)
{
WindowsApi.VirtualFree(location, (IntPtr)PageSize, AllocationType.Release | AllocationType.PreservePlaceholder);
var ptr = WindowsApi.MapViewOfFile3(
sharedMemory,
WindowsApi.CurrentProcessHandle,
location,
srcOffset,
(IntPtr)PageSize,
0x4000,
MemoryProtection.ReadWrite,
IntPtr.Zero,
0);
if (ptr == IntPtr.Zero)
{
throw new WindowsApiException("MapViewOfFile3");
}
location += PageSize;
srcOffset += PageSize;
}
}
public static void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size)
{
_placeholders.UnmapView(sharedMemory, location, size);
}
public static void UnmapView4KB(IntPtr location, IntPtr size)
{
ulong uaddress = (ulong)location;
ulong usize = (ulong)size;
IntPtr endLocation = (IntPtr)(uaddress + usize);
while (location != endLocation)
{
bool result = WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, location, 2);
if (!result)
{
throw new WindowsApiException("UnmapViewOfFile2");
}
location += PageSize;
}
}
public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
{
if (forView)
{
return _placeholders.ReprotectView(address, size, permission);
}
else
{
return WindowsApi.VirtualProtect(address, size, WindowsApi.GetProtection(permission), out _);
}
}
public static bool Reprotect4KB(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
{
ulong uaddress = (ulong)address;
ulong usize = (ulong)size;
while (usize > 0)
{
if (!WindowsApi.VirtualProtect((IntPtr)uaddress, (IntPtr)PageSize, WindowsApi.GetProtection(permission), out _))
{
return false;
}
uaddress += PageSize;
usize -= PageSize;
}
return true;
}
public static bool Free(IntPtr address)
{
return WindowsApi.VirtualFree(address, IntPtr.Zero, AllocationType.Release);
}
public static IntPtr CreateSharedMemory(IntPtr size, bool reserve)
{
var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
IntPtr handle = WindowsApi.CreateFileMapping(
WindowsApi.InvalidHandleValue,
IntPtr.Zero,
FileMapProtection.PageReadWrite | prot,
(uint)(size.ToInt64() >> 32),
(uint)size.ToInt64(),
null);
if (handle == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return handle;
}
public static void DestroySharedMemory(IntPtr handle)
{
if (!WindowsApi.CloseHandle(handle))
{
throw new ArgumentException("Invalid handle.", nameof(handle));
}
}
public static IntPtr MapSharedMemory(IntPtr handle)
{
IntPtr ptr = WindowsApi.MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
public static void UnmapSharedMemory(IntPtr address)
{
if (!WindowsApi.UnmapViewOfFile(address))
{
throw new ArgumentException("Invalid address.", nameof(address));
}
}
public static bool RetryFromAccessViolation()
{
return _placeholders.RetryFromAccessViolation();
}
}
}