aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Memory
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2023-01-17 01:13:24 -0300
committerGitHub <noreply@github.com>2023-01-17 05:13:24 +0100
commit86fd0643c26433362a25acceb4fa1fcee07dd0b2 (patch)
tree8d12fb6b0629c195a0a3c1014f46cfe8f22cd3e6 /Ryujinx.Memory
parent43a83a401ea8101bf6d001fe6fe188e1c106245e (diff)
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 <rhy3756547@hotmail.com>
Diffstat (limited to 'Ryujinx.Memory')
-rw-r--r--Ryujinx.Memory/AddressSpaceManager.cs137
-rw-r--r--Ryujinx.Memory/IVirtualMemoryManager.cs30
-rw-r--r--Ryujinx.Memory/MemoryBlock.cs2
-rw-r--r--Ryujinx.Memory/MemoryMapFlags.cs23
-rw-r--r--Ryujinx.Memory/Range/HostMemoryRange.cs71
-rw-r--r--Ryujinx.Memory/Tracking/MemoryTracking.cs10
-rw-r--r--Ryujinx.Memory/Tracking/MultiRegionHandle.cs21
-rw-r--r--Ryujinx.Memory/Tracking/RegionHandle.cs27
8 files changed, 259 insertions, 62 deletions
diff --git a/Ryujinx.Memory/AddressSpaceManager.cs b/Ryujinx.Memory/AddressSpaceManager.cs
index ffe880bf..b532ce5e 100644
--- a/Ryujinx.Memory/AddressSpaceManager.cs
+++ b/Ryujinx.Memory/AddressSpaceManager.cs
@@ -13,9 +13,12 @@ namespace Ryujinx.Memory
/// </summary>
public sealed class AddressSpaceManager : IVirtualMemoryManager, IWritableBlock
{
- public const int PageBits = PageTable<ulong>.PageBits;
- public const int PageSize = PageTable<ulong>.PageSize;
- public const int PageMask = PageTable<ulong>.PageMask;
+ public const int PageBits = PageTable<nuint>.PageBits;
+ public const int PageSize = PageTable<nuint>.PageSize;
+ public const int PageMask = PageTable<nuint>.PageMask;
+
+ /// <inheritdoc/>
+ public bool Supports4KBPages => true;
/// <summary>
/// Address space width in bits.
@@ -25,7 +28,7 @@ namespace Ryujinx.Memory
private readonly ulong _addressSpaceSize;
private readonly MemoryBlock _backingMemory;
- private readonly PageTable<ulong> _pageTable;
+ private readonly PageTable<nuint> _pageTable;
/// <summary>
/// Creates a new instance of the memory manager.
@@ -46,17 +49,17 @@ namespace Ryujinx.Memory
AddressSpaceBits = asBits;
_addressSpaceSize = asSize;
_backingMemory = backingMemory;
- _pageTable = new PageTable<ulong>();
+ _pageTable = new PageTable<nuint>();
}
/// <inheritdoc/>
- public void Map(ulong va, ulong pa, ulong size)
+ public void Map(ulong va, ulong pa, ulong size, MemoryMapFlags flags)
{
AssertValidAddressAndSize(va, size);
while (size != 0)
{
- _pageTable.Map(va, pa);
+ _pageTable.Map(va, (nuint)(ulong)_backingMemory.GetPointer(pa, PageSize));
va += PageSize;
pa += PageSize;
@@ -65,6 +68,21 @@ namespace Ryujinx.Memory
}
/// <inheritdoc/>
+ public void MapForeign(ulong va, nuint hostPointer, ulong size)
+ {
+ AssertValidAddressAndSize(va, size);
+
+ while (size != 0)
+ {
+ _pageTable.Map(va, hostPointer);
+
+ va += PageSize;
+ hostPointer += PageSize;
+ size -= PageSize;
+ }
+ }
+
+ /// <inheritdoc/>
public void Unmap(ulong va, ulong size)
{
AssertValidAddressAndSize(va, size);
@@ -108,7 +126,7 @@ namespace Ryujinx.Memory
if (IsContiguousAndMapped(va, data.Length))
{
- data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length));
+ data.CopyTo(GetHostSpanContiguous(va, data.Length));
}
else
{
@@ -116,22 +134,18 @@ namespace Ryujinx.Memory
if ((va & PageMask) != 0)
{
- ulong pa = GetPhysicalAddressInternal(va);
-
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
- data.Slice(0, size).CopyTo(_backingMemory.GetSpan(pa, size));
+ data.Slice(0, size).CopyTo(GetHostSpanContiguous(va, size));
offset += size;
}
for (; offset < data.Length; offset += size)
{
- ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
-
size = Math.Min(data.Length - offset, PageSize);
- data.Slice(offset, size).CopyTo(_backingMemory.GetSpan(pa, size));
+ data.Slice(offset, size).CopyTo(GetHostSpanContiguous(va + (ulong)offset, size));
}
}
}
@@ -154,7 +168,7 @@ namespace Ryujinx.Memory
if (IsContiguousAndMapped(va, size))
{
- return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size);
+ return GetHostSpanContiguous(va, size);
}
else
{
@@ -176,7 +190,7 @@ namespace Ryujinx.Memory
if (IsContiguousAndMapped(va, size))
{
- return new WritableRegion(null, va, _backingMemory.GetMemory(GetPhysicalAddressInternal(va), size));
+ return new WritableRegion(null, va, new NativeMemoryManager<byte>((byte*)GetHostAddress(va), size).Memory);
}
else
{
@@ -189,14 +203,14 @@ namespace Ryujinx.Memory
}
/// <inheritdoc/>
- public ref T GetRef<T>(ulong va) where T : unmanaged
+ public unsafe ref T GetRef<T>(ulong va) where T : unmanaged
{
if (!IsContiguous(va, Unsafe.SizeOf<T>()))
{
ThrowMemoryNotContiguous();
}
- return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
+ return ref *(T*)GetHostAddress(va);
}
/// <inheritdoc/>
@@ -210,7 +224,7 @@ namespace Ryujinx.Memory
return (int)(vaSpan / PageSize);
}
- private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
+ private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
@@ -232,7 +246,7 @@ namespace Ryujinx.Memory
return false;
}
- if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize))
+ if (GetHostAddress(va) + PageSize != GetHostAddress(va + PageSize))
{
return false;
}
@@ -244,6 +258,17 @@ namespace Ryujinx.Memory
}
/// <inheritdoc/>
+ public IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size)
+ {
+ if (size == 0)
+ {
+ return Enumerable.Empty<HostMemoryRange>();
+ }
+
+ return GetHostRegionsImpl(va, size);
+ }
+
+ /// <inheritdoc/>
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
{
if (size == 0)
@@ -251,6 +276,39 @@ namespace Ryujinx.Memory
return Enumerable.Empty<MemoryRange>();
}
+ var hostRegions = GetHostRegionsImpl(va, size);
+ if (hostRegions == null)
+ {
+ return null;
+ }
+
+ var regions = new MemoryRange[hostRegions.Count];
+
+ ulong backingStart = (ulong)_backingMemory.Pointer;
+ ulong backingEnd = backingStart + _backingMemory.Size;
+
+ int count = 0;
+
+ for (int i = 0; i < regions.Length; i++)
+ {
+ var hostRegion = hostRegions[i];
+
+ if ((ulong)hostRegion.Address >= backingStart && (ulong)hostRegion.Address < backingEnd)
+ {
+ regions[count++] = new MemoryRange((ulong)hostRegion.Address - backingStart, hostRegion.Size);
+ }
+ }
+
+ if (count != regions.Length)
+ {
+ return new ArraySegment<MemoryRange>(regions, 0, count);
+ }
+
+ return regions;
+ }
+
+ private List<HostMemoryRange> GetHostRegionsImpl(ulong va, ulong size)
+ {
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
{
return null;
@@ -258,9 +316,9 @@ namespace Ryujinx.Memory
int pages = GetPagesCount(va, (uint)size, out va);
- var regions = new List<MemoryRange>();
+ var regions = new List<HostMemoryRange>();
- ulong regionStart = GetPhysicalAddressInternal(va);
+ nuint regionStart = GetHostAddress(va);
ulong regionSize = PageSize;
for (int page = 0; page < pages - 1; page++)
@@ -270,12 +328,12 @@ namespace Ryujinx.Memory
return null;
}
- ulong newPa = GetPhysicalAddressInternal(va + PageSize);
+ nuint newHostAddress = GetHostAddress(va + PageSize);
- if (GetPhysicalAddressInternal(va) + PageSize != newPa)
+ if (GetHostAddress(va) + PageSize != newHostAddress)
{
- regions.Add(new MemoryRange(regionStart, regionSize));
- regionStart = newPa;
+ regions.Add(new HostMemoryRange(regionStart, regionSize));
+ regionStart = newHostAddress;
regionSize = 0;
}
@@ -283,7 +341,7 @@ namespace Ryujinx.Memory
regionSize += PageSize;
}
- regions.Add(new MemoryRange(regionStart, regionSize));
+ regions.Add(new HostMemoryRange(regionStart, regionSize));
return regions;
}
@@ -301,22 +359,18 @@ namespace Ryujinx.Memory
if ((va & PageMask) != 0)
{
- ulong pa = GetPhysicalAddressInternal(va);
-
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
- _backingMemory.GetSpan(pa, size).CopyTo(data.Slice(0, size));
+ GetHostSpanContiguous(va, size).CopyTo(data.Slice(0, size));
offset += size;
}
for (; offset < data.Length; offset += size)
{
- ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
-
size = Math.Min(data.Length - offset, PageSize);
- _backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
+ GetHostSpanContiguous(va + (ulong)offset, size).CopyTo(data.Slice(offset, size));
}
}
@@ -391,22 +445,23 @@ namespace Ryujinx.Memory
}
}
- private ulong GetPhysicalAddressInternal(ulong va)
+ private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
{
- return _pageTable.Read(va) + (va & PageMask);
+ return new Span<byte>((void*)GetHostAddress(va), size);
}
- /// <summary>
- /// Reprotect a region of virtual memory for tracking. Sets software protection bits.
- /// </summary>
- /// <param name="va">Virtual address base</param>
- /// <param name="size">Size of the region to protect</param>
- /// <param name="protection">Memory protection to set</param>
+ private nuint GetHostAddress(ulong va)
+ {
+ return _pageTable.Read(va) + (nuint)(va & PageMask);
+ }
+
+ /// <inheritdoc/>
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
{
throw new NotImplementedException();
}
+ /// <inheritdoc/>
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false)
{
// Only the ARM Memory Manager has tracking for now.
diff --git a/Ryujinx.Memory/IVirtualMemoryManager.cs b/Ryujinx.Memory/IVirtualMemoryManager.cs
index c8a74f66..390371ad 100644
--- a/Ryujinx.Memory/IVirtualMemoryManager.cs
+++ b/Ryujinx.Memory/IVirtualMemoryManager.cs
@@ -7,6 +7,12 @@ namespace Ryujinx.Memory
public interface IVirtualMemoryManager
{
/// <summary>
+ /// Indicates whenever the memory manager supports aliasing pages at 4KB granularity.
+ /// </summary>
+ /// <returns>True if 4KB pages are supported by the memory manager, false otherwise</returns>
+ bool Supports4KBPages { get; }
+
+ /// <summary>
/// Maps a virtual memory range into a physical memory range.
/// </summary>
/// <remarks>
@@ -15,7 +21,20 @@ namespace Ryujinx.Memory
/// <param name="va">Virtual memory address</param>
/// <param name="pa">Physical memory address where the region should be mapped to</param>
/// <param name="size">Size to be mapped</param>
- void Map(ulong va, ulong pa, ulong size);
+ /// <param name="flags">Flags controlling memory mapping</param>
+ void Map(ulong va, ulong pa, ulong size, MemoryMapFlags flags);
+
+ /// <summary>
+ /// Maps a virtual memory range into an arbitrary host memory range.
+ /// </summary>
+ /// <remarks>
+ /// Addresses and size must be page aligned.
+ /// Not all memory managers supports this feature.
+ /// </remarks>
+ /// <param name="va">Virtual memory address</param>
+ /// <param name="hostPointer">Host pointer where the virtual region should be mapped</param>
+ /// <param name="size">Size to be mapped</param>
+ void MapForeign(ulong va, nuint hostPointer, ulong size);
/// <summary>
/// Unmaps a previously mapped range of virtual memory.
@@ -116,6 +135,15 @@ namespace Ryujinx.Memory
ref T GetRef<T>(ulong va) where T : unmanaged;
/// <summary>
+ /// Gets the host regions that make up the given virtual address region.
+ /// If any part of the virtual region is unmapped, null is returned.
+ /// </summary>
+ /// <param name="va">Virtual address of the range</param>
+ /// <param name="size">Size of the range</param>
+ /// <returns>Array of host regions</returns>
+ IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size);
+
+ /// <summary>
/// Gets the physical regions that make up the given virtual address region.
/// If any part of the virtual region is unmapped, null is returned.
/// </summary>
diff --git a/Ryujinx.Memory/MemoryBlock.cs b/Ryujinx.Memory/MemoryBlock.cs
index 2df7ea9b..885ef456 100644
--- a/Ryujinx.Memory/MemoryBlock.cs
+++ b/Ryujinx.Memory/MemoryBlock.cs
@@ -440,4 +440,4 @@ namespace Ryujinx.Memory
private static void ThrowInvalidMemoryRegionException() => throw new InvalidMemoryRegionException();
}
-}
+} \ No newline at end of file
diff --git a/Ryujinx.Memory/MemoryMapFlags.cs b/Ryujinx.Memory/MemoryMapFlags.cs
new file mode 100644
index 00000000..b4c74c8c
--- /dev/null
+++ b/Ryujinx.Memory/MemoryMapFlags.cs
@@ -0,0 +1,23 @@
+using System;
+
+namespace Ryujinx.Memory
+{
+ /// <summary>
+ /// Flags that indicate how the host memory should be mapped.
+ /// </summary>
+ [Flags]
+ public enum MemoryMapFlags
+ {
+ /// <summary>
+ /// No mapping flags.
+ /// </summary>
+ None = 0,
+
+ /// <summary>
+ /// Indicates that the implementation is free to ignore the specified backing memory offset
+ /// and allocate its own private storage for the mapping.
+ /// This allows some mappings that would otherwise fail due to host platform restrictions to succeed.
+ /// </summary>
+ Private = 1 << 0
+ }
+}
diff --git a/Ryujinx.Memory/Range/HostMemoryRange.cs b/Ryujinx.Memory/Range/HostMemoryRange.cs
new file mode 100644
index 00000000..79c649d8
--- /dev/null
+++ b/Ryujinx.Memory/Range/HostMemoryRange.cs
@@ -0,0 +1,71 @@
+using System;
+
+namespace Ryujinx.Memory.Range
+{
+ /// <summary>
+ /// Range of memory composed of an address and size.
+ /// </summary>
+ public struct HostMemoryRange : IEquatable<HostMemoryRange>
+ {
+ /// <summary>
+ /// An empty memory range, with a null address and zero size.
+ /// </summary>
+ public static HostMemoryRange Empty => new HostMemoryRange(0, 0);
+
+ /// <summary>
+ /// Start address of the range.
+ /// </summary>
+ public nuint Address { get; }
+
+ /// <summary>
+ /// Size of the range in bytes.
+ /// </summary>
+ public ulong Size { get; }
+
+ /// <summary>
+ /// Address where the range ends (exclusive).
+ /// </summary>
+ public nuint EndAddress => Address + (nuint)Size;
+
+ /// <summary>
+ /// Creates a new memory range with the specified address and size.
+ /// </summary>
+ /// <param name="address">Start address</param>
+ /// <param name="size">Size in bytes</param>
+ public HostMemoryRange(nuint address, ulong size)
+ {
+ Address = address;
+ Size = size;
+ }
+
+ /// <summary>
+ /// Checks if the range overlaps with another.
+ /// </summary>
+ /// <param name="other">The other range to check for overlap</param>
+ /// <returns>True if the ranges overlap, false otherwise</returns>
+ public bool OverlapsWith(HostMemoryRange other)
+ {
+ nuint thisAddress = Address;
+ nuint thisEndAddress = EndAddress;
+ nuint otherAddress = other.Address;
+ nuint otherEndAddress = other.EndAddress;
+
+ return thisAddress < otherEndAddress && otherAddress < thisEndAddress;
+ }
+
+ public override bool Equals(object obj)
+ {
+ return obj is HostMemoryRange other && Equals(other);
+ }
+
+ public bool Equals(HostMemoryRange other)
+ {
+ return Address == other.Address && Size == other.Size;
+ }
+
+ public override int GetHashCode()
+ {
+ return HashCode.Combine(Address, Size);
+ }
+ }
+}
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
/// <returns>The memory tracking handle</returns>
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity)
{
- (address, size) = PageAlign(address, size);
-
return new MultiRegionHandle(this, address, size, handles, granularity);
}
@@ -166,11 +164,11 @@ namespace Ryujinx.Memory.Tracking
/// <returns>The memory tracking handle</returns>
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
/// <returns>The memory tracking handle</returns>
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<IRegionHandle> 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
/// <param name="tracking">Tracking object for the target memory block</param>
/// <param name="address">Virtual address of the region to track</param>
/// <param name="size">Size of the region to track</param>
+ /// <param name="realAddress">The real, unaligned address of the handle</param>
+ /// <param name="realSize">The real, unaligned size of the handle</param>
/// <param name="bitmap">The bitmap the dirty flag for this handle is stored in</param>
/// <param name="bit">The bit index representing the dirty flag for this handle</param>
/// <param name="mapped">True if the region handle starts mapped</param>
- 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
/// <param name="tracking">Tracking object for the target memory block</param>
/// <param name="address">Virtual address of the region to track</param>
/// <param name="size">Size of the region to track</param>
+ /// <param name="realAddress">The real, unaligned address of the handle</param>
+ /// <param name="realSize">The real, unaligned size of the handle</param>
/// <param name="mapped">True if the region handle starts mapped</param>
- 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<RegionHandle>)
{
@@ -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.