From 86fd0643c26433362a25acceb4fa1fcee07dd0b2 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Tue, 17 Jan 2023 01:13:24 -0300 Subject: Implement support for page sizes > 4KB (#4252) * Implement support for page sizes > 4KB * Check and work around more alignment issues * Was not meant to change this * Use MemoryBlock.GetPageSize() value for signal handler code * Do not take the path for private allocations if host supports 4KB pages * Add Flags attribute on MemoryMapFlags * Fix dirty region size with 16kb pages Would accidentally report a size that was too high (generally 16k instead of 4k, uploading 4x as much data) Co-authored-by: riperiperi --- Ryujinx.Memory/Tracking/MemoryTracking.cs | 10 ++++------ Ryujinx.Memory/Tracking/MultiRegionHandle.cs | 21 +++++++++++---------- Ryujinx.Memory/Tracking/RegionHandle.cs | 27 ++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 19 deletions(-) (limited to 'Ryujinx.Memory/Tracking') diff --git a/Ryujinx.Memory/Tracking/MemoryTracking.cs b/Ryujinx.Memory/Tracking/MemoryTracking.cs index 9aa7c7ff..9a35cfb6 100644 --- a/Ryujinx.Memory/Tracking/MemoryTracking.cs +++ b/Ryujinx.Memory/Tracking/MemoryTracking.cs @@ -139,8 +139,6 @@ namespace Ryujinx.Memory.Tracking /// The memory tracking handle public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable handles, ulong granularity) { - (address, size) = PageAlign(address, size); - return new MultiRegionHandle(this, address, size, handles, granularity); } @@ -166,11 +164,11 @@ namespace Ryujinx.Memory.Tracking /// The memory tracking handle public RegionHandle BeginTracking(ulong address, ulong size) { - (address, size) = PageAlign(address, size); + var (paAddress, paSize) = PageAlign(address, size); lock (TrackingLock) { - RegionHandle handle = new RegionHandle(this, address, size, _memoryManager.IsRangeMapped(address, size)); + RegionHandle handle = new RegionHandle(this, paAddress, paSize, address, size, _memoryManager.IsRangeMapped(address, size)); return handle; } @@ -186,11 +184,11 @@ namespace Ryujinx.Memory.Tracking /// The memory tracking handle internal RegionHandle BeginTrackingBitmap(ulong address, ulong size, ConcurrentBitmap bitmap, int bit) { - (address, size) = PageAlign(address, size); + var (paAddress, paSize) = PageAlign(address, size); lock (TrackingLock) { - RegionHandle handle = new RegionHandle(this, address, size, bitmap, bit, _memoryManager.IsRangeMapped(address, size)); + RegionHandle handle = new RegionHandle(this, paAddress, paSize, address, size, bitmap, bit, _memoryManager.IsRangeMapped(address, size)); return handle; } diff --git a/Ryujinx.Memory/Tracking/MultiRegionHandle.cs b/Ryujinx.Memory/Tracking/MultiRegionHandle.cs index 6cbea7f3..6ea2b784 100644 --- a/Ryujinx.Memory/Tracking/MultiRegionHandle.cs +++ b/Ryujinx.Memory/Tracking/MultiRegionHandle.cs @@ -32,7 +32,7 @@ namespace Ryujinx.Memory.Tracking internal MultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, IEnumerable handles, ulong granularity) { - _handles = new RegionHandle[size / granularity]; + _handles = new RegionHandle[(size + granularity - 1) / granularity]; Granularity = granularity; _dirtyBitmap = new ConcurrentBitmap(_handles.Length, true); @@ -50,7 +50,7 @@ namespace Ryujinx.Memory.Tracking foreach (RegionHandle handle in handles) { - int startIndex = (int)((handle.Address - address) / granularity); + int startIndex = (int)((handle.RealAddress - address) / granularity); // Fill any gap left before this handle. while (i < startIndex) @@ -72,7 +72,7 @@ namespace Ryujinx.Memory.Tracking } else { - int endIndex = (int)((handle.EndAddress - address) / granularity); + int endIndex = (int)((handle.RealEndAddress - address) / granularity); while (i < endIndex) { @@ -171,12 +171,13 @@ namespace Ryujinx.Memory.Tracking modifiedAction(rgStart, rgSize); rgSize = 0; } - rgStart = handle.Address; + + rgStart = handle.RealAddress; } if (handle.Dirty) { - rgSize += handle.Size; + rgSize += handle.RealSize; handle.Reprotect(); } @@ -191,7 +192,7 @@ namespace Ryujinx.Memory.Tracking int startHandle = (int)((address - Address) / Granularity); int lastHandle = (int)((address + (size - 1) - Address) / Granularity); - ulong rgStart = _handles[startHandle].Address; + ulong rgStart = Address + (ulong)startHandle * Granularity; if (startHandle == lastHandle) { @@ -200,7 +201,7 @@ namespace Ryujinx.Memory.Tracking if (handle.Dirty) { handle.Reprotect(); - modifiedAction(rgStart, handle.Size); + modifiedAction(rgStart, handle.RealSize); } return; @@ -273,10 +274,10 @@ namespace Ryujinx.Memory.Tracking modifiedAction(rgStart, rgSize); rgSize = 0; } - rgStart = handle.Address; + rgStart = handle.RealAddress; } - rgSize += handle.Size; + rgSize += handle.RealSize; handle.Reprotect(false, (checkMasks[index] & bitValue) == 0); checkMasks[index] &= ~bitValue; @@ -320,7 +321,7 @@ namespace Ryujinx.Memory.Tracking { handle.Reprotect(); - modifiedAction(rgStart, handle.Size); + modifiedAction(rgStart, handle.RealSize); } } diff --git a/Ryujinx.Memory/Tracking/RegionHandle.cs b/Ryujinx.Memory/Tracking/RegionHandle.cs index 86c77abc..580f94a5 100644 --- a/Ryujinx.Memory/Tracking/RegionHandle.cs +++ b/Ryujinx.Memory/Tracking/RegionHandle.cs @@ -42,6 +42,10 @@ namespace Ryujinx.Memory.Tracking public ulong Size { get; } public ulong EndAddress { get; } + public ulong RealAddress { get; } + public ulong RealSize { get; } + public ulong RealEndAddress { get; } + internal IMultiRegionHandle Parent { get; set; } private event Action _onDirty; @@ -89,10 +93,12 @@ namespace Ryujinx.Memory.Tracking /// Tracking object for the target memory block /// Virtual address of the region to track /// Size of the region to track + /// The real, unaligned address of the handle + /// The real, unaligned size of the handle /// The bitmap the dirty flag for this handle is stored in /// The bit index representing the dirty flag for this handle /// True if the region handle starts mapped - internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ConcurrentBitmap bitmap, int bit, bool mapped = true) + internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, ConcurrentBitmap bitmap, int bit, bool mapped = true) { Bitmap = bitmap; DirtyBit = bit; @@ -104,6 +110,10 @@ namespace Ryujinx.Memory.Tracking Size = size; EndAddress = address + size; + RealAddress = realAddress; + RealSize = realSize; + RealEndAddress = realAddress + realSize; + _tracking = tracking; _regions = tracking.GetVirtualRegionsForHandle(address, size); foreach (var region in _regions) @@ -119,16 +129,23 @@ namespace Ryujinx.Memory.Tracking /// Tracking object for the target memory block /// Virtual address of the region to track /// Size of the region to track + /// The real, unaligned address of the handle + /// The real, unaligned size of the handle /// True if the region handle starts mapped - internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, bool mapped = true) + internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, bool mapped = true) { Bitmap = new ConcurrentBitmap(1, mapped); Unmapped = !mapped; + Address = address; Size = size; EndAddress = address + size; + RealAddress = realAddress; + RealSize = realSize; + RealEndAddress = realAddress + realSize; + _tracking = tracking; _regions = tracking.GetVirtualRegionsForHandle(address, size); foreach (var region in _regions) @@ -199,6 +216,10 @@ namespace Ryujinx.Memory.Tracking if (_preAction != null) { + // Limit the range to within this handle. + ulong maxAddress = Math.Max(address, RealAddress); + ulong minEndAddress = Math.Min(address + size, RealAddress + RealSize); + // Copy the handles list in case it changes when we're out of the lock. if (handleIterable is List) { @@ -212,7 +233,7 @@ namespace Ryujinx.Memory.Tracking { lock (_preActionLock) { - _preAction?.Invoke(address, size); + _preAction?.Invoke(maxAddress, minEndAddress - maxAddress); // The action is removed after it returns, to ensure that the null check above succeeds when // it's still in progress rather than continuing and possibly missing a required data flush. -- cgit v1.2.3