using Ryujinx.Memory.Range;
using System.Collections.Generic;
namespace Ryujinx.Memory.Tracking
{
///
/// Manages memory tracking for a given virutal/physical memory block.
///
public class MemoryTracking
{
private readonly IVirtualMemoryManager _memoryManager;
private readonly MemoryBlock _block;
// Only use these from within the lock.
private readonly NonOverlappingRangeList _virtualRegions;
private readonly NonOverlappingRangeList _physicalRegions;
// Only use these from within the lock.
private readonly VirtualRegion[] _virtualResults = new VirtualRegion[10];
private readonly PhysicalRegion[] _physicalResults = new PhysicalRegion[10];
private readonly int _pageSize;
///
/// This lock must be obtained when traversing or updating the region-handle hierarchy.
/// It is not required when reading dirty flags.
///
internal object TrackingLock = new object();
public bool EnablePhysicalProtection { get; set; }
///
/// Create a new tracking structure for the given "physical" memory block,
/// with a given "virtual" memory manager that will provide mappings and virtual memory protection.
///
/// Virtual memory manager
/// Physical memory block
/// Page size of the virtual memory space
public MemoryTracking(IVirtualMemoryManager memoryManager, MemoryBlock block, int pageSize)
{
_memoryManager = memoryManager;
_block = block;
_pageSize = pageSize;
_virtualRegions = new NonOverlappingRangeList();
_physicalRegions = new NonOverlappingRangeList();
}
private (ulong address, ulong size) PageAlign(ulong address, ulong size)
{
ulong pageMask = (ulong)_pageSize - 1;
ulong rA = address & ~pageMask;
ulong rS = ((address + size + pageMask) & ~pageMask) - rA;
return (rA, rS);
}
///
/// Indicate that a virtual region has been mapped, and which physical region it has been mapped to.
/// Should be called after the mapping is complete.
///
/// Virtual memory address
/// Physical memory address
/// Size to be mapped
public void Map(ulong va, ulong pa, ulong size)
{
// A mapping may mean we need to re-evaluate each VirtualRegion's affected area.
// Find all handles that overlap with the range, we need to recalculate their physical regions
lock (TrackingLock)
{
var results = _virtualResults;
int count = _virtualRegions.FindOverlapsNonOverlapping(va, size, ref results);
for (int i = 0; i < count; i++)
{
VirtualRegion region = results[i];
// If the region has been fully remapped, signal that it has been mapped again.
bool remapped = _memoryManager.IsRangeMapped(region.Address, region.Size);
if (remapped)
{
region.SignalMappingChanged(true);
}
region.RecalculatePhysicalChildren();
region.UpdateProtection();
}
}
}
///
/// Indicate that a virtual region has been unmapped.
/// Should be called after the unmapping is complete.
///
/// Virtual memory address
/// Size to be unmapped
public void Unmap(ulong va, ulong size)
{
// An unmapping may mean we need to re-evaluate each VirtualRegion's affected area.
// Find all handles that overlap with the range, we need to recalculate their physical regions
lock (TrackingLock)
{
var results = _virtualResults;
int count = _virtualRegions.FindOverlapsNonOverlapping(va, size, ref results);
for (int i = 0; i < count; i++)
{
VirtualRegion region = results[i];
region.SignalMappingChanged(false);
region.RecalculatePhysicalChildren();
}
}
}
///
/// Get a list of virtual regions that a handle covers.
///
/// Starting virtual memory address of the handle
/// Size of the handle's memory region
/// A list of virtual regions within the given range
internal List GetVirtualRegionsForHandle(ulong va, ulong size)
{
List result = new List();
_virtualRegions.GetOrAddRegions(result, va, size, (va, size) => new VirtualRegion(this, va, size));
return result;
}
///
/// Get a list of physical regions that a virtual region covers.
/// Note that this becomes outdated if the virtual or physical regions are unmapped or remapped.
///
/// Virtual memory address
/// Size of the virtual region
/// A list of physical regions the virtual region covers
internal List GetPhysicalRegionsForVirtual(ulong va, ulong size)
{
List result = new List();
// Get a list of physical regions for this virtual region, from our injected virtual mapping function.
(ulong Address, ulong Size)[] physicalRegions = _memoryManager.GetPhysicalRegions(va, size);
if (physicalRegions != null)
{
foreach (var region in physicalRegions)
{
_physicalRegions.GetOrAddRegions(result, region.Address, region.Size, (pa, size) => new PhysicalRegion(this, pa, size));
}
}
return result;
}
///
/// Remove a virtual region from the range list. This assumes that the lock has been acquired.
///
/// Region to remove
internal void RemoveVirtual(VirtualRegion region)
{
_virtualRegions.Remove(region);
}
///
/// Remove a physical region from the range list. This assumes that the lock has been acquired.
///
/// Region to remove
internal void RemovePhysical(PhysicalRegion region)
{
_physicalRegions.Remove(region);
}
///
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
///
/// CPU virtual address of the region
/// Size of the region
/// Desired granularity of write tracking
/// The memory tracking handle
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, ulong granularity)
{
(address, size) = PageAlign(address, size);
return new MultiRegionHandle(this, address, size, granularity);
}
///
/// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
///
/// CPU virtual address of the region
/// Size of the region
/// Desired granularity of write tracking
/// The memory tracking handle
public SmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity)
{
(address, size) = PageAlign(address, size);
return new SmartMultiRegionHandle(this, address, size, granularity);
}
///
/// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
///
/// CPU virtual address of the region
/// Size of the region
/// The memory tracking handle
public RegionHandle BeginTracking(ulong address, ulong size)
{
(address, size) = PageAlign(address, size);
lock (TrackingLock)
{
RegionHandle handle = new RegionHandle(this, address, size, _memoryManager.IsRangeMapped(address, size));
return handle;
}
}
///
/// Signal that a physical memory event happened at the given location.
///
/// Physical address accessed
/// Whether the region was written to or read
/// True if the event triggered any tracking regions, false otherwise
public bool PhysicalMemoryEvent(ulong address, bool write)
{
// Look up the physical region using the region list.
// Signal up the chain to relevant handles.
lock (TrackingLock)
{
var results = _physicalResults;
int count = _physicalRegions.FindOverlapsNonOverlapping(address, 1, ref results); // TODO: get/use the actual access size?
if (count == 0)
{
_block.Reprotect(address & ~(ulong)(_pageSize - 1), (ulong)_pageSize, MemoryPermission.ReadAndWrite);
return false; // We can't handle this - unprotect and return.
}
for (int i = 0; i < count; i++)
{
PhysicalRegion region = results[i];
region.Signal(address, 1, write);
}
}
return true;
}
///
/// Signal that a virtual memory event happened at the given location (one byte).
///
/// Virtual address accessed
/// Whether the address was written to or read
/// True if the event triggered any tracking regions, false otherwise
public bool VirtualMemoryEventTracking(ulong address, bool write)
{
return VirtualMemoryEvent(address, 1, write);
}
///
/// Signal that a virtual memory event happened at the given location.
///
/// Virtual address accessed
/// Size of the region affected in bytes
/// Whether the region was written to or read
/// True if the event triggered any tracking regions, false otherwise
public bool VirtualMemoryEvent(ulong address, ulong size, bool write)
{
// Look up the virtual region using the region list.
// Signal up the chain to relevant handles.
lock (TrackingLock)
{
var results = _virtualResults;
int count = _virtualRegions.FindOverlapsNonOverlapping(address, size, ref results);
if (count == 0)
{
_memoryManager.TrackingReprotect(address & ~(ulong)(_pageSize - 1), (ulong)_pageSize, MemoryPermission.ReadAndWrite);
return false; // We can't handle this - it's probably a real invalid access.
}
for (int i = 0; i < count; i++)
{
VirtualRegion region = results[i];
region.Signal(address, size, write);
}
}
return true;
}
///
/// Reprotect a given physical region, if enabled. This is protected on the memory block provided during initialization.
///
/// Region to reprotect
/// Memory permission to protect with
internal void ProtectPhysicalRegion(PhysicalRegion region, MemoryPermission permission)
{
if (EnablePhysicalProtection)
{
_block.Reprotect(region.Address, region.Size, permission);
}
}
///
/// Reprotect a given virtual region. The virtual memory manager will handle this.
///
/// Region to reprotect
/// Memory permission to protect with
internal void ProtectVirtualRegion(VirtualRegion region, MemoryPermission permission)
{
_memoryManager.TrackingReprotect(region.Address, region.Size, permission);
}
///
/// Returns the number of virtual and physical regions currently being tracked.
/// Useful for tests and metrics.
///
/// The number of virtual regions, and the number of physical regions
public (int VirtualCount, int PhysicalCount) GetRegionCounts()
{
lock (TrackingLock)
{
return (_virtualRegions.Count, _physicalRegions.Count);
}
}
}
}