mirror of
https://github.com/GreemDev/Ryujinx
synced 2024-11-23 02:06:55 +01:00
54ea2285f0
* Refactoring of KMemoryManager class * Replace some trivial uses of DRAM address with VA * Get rid of GetDramAddressFromVa * Abstracting more operations on derived page table class * Run auto-format on KPageTableBase * Managed to make TryConvertVaToPa private, few uses remains now * Implement guest physical pages ref counting, remove manual freeing * Make DoMmuOperation private and call new abstract methods only from the base class * Pass pages count rather than size on Map/UnmapMemory * Change memory managers to take host pointers * Fix a guest memory leak and simplify KPageTable * Expose new methods for host range query and mapping * Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists * Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking) * Add a SharedMemoryStorage class, will be useful for host mapping * Sayonara AddVaRangeToPageList, you served us well * Start to implement host memory mapping (WIP) * Support memory tracking through host exception handling * Fix some access violations from HLE service guest memory access and CPU * Fix memory tracking * Fix mapping list bugs, including a race and a error adding mapping ranges * Simple page table for memory tracking * Simple "volatile" region handle mode * Update UBOs directly (experimental, rough) * Fix the overlap check * Only set non-modified buffers as volatile * Fix some memory tracking issues * Fix possible race in MapBufferFromClientProcess (block list updates were not locked) * Write uniform update to memory immediately, only defer the buffer set. * Fix some memory tracking issues * Pass correct pages count on shared memory unmap * Armeilleure Signal Handler v1 + Unix changes Unix currently behaves like windows, rather than remapping physical * Actually check if the host platform is unix * Fix decommit on linux. * Implement windows 10 placeholder shared memory, fix a buffer issue. * Make PTC version something that will never match with master * Remove testing variable for block count * Add reference count for memory manager, fix dispose Can still deadlock with OpenAL * Add address validation, use page table for mapped check, add docs Might clean up the page table traversing routines. * Implement batched mapping/tracking. * Move documentation, fix tests. * Cleanup uniform buffer update stuff. * Remove unnecessary assignment. * Add unsafe host mapped memory switch On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work. * Remove C# exception handlers They have issues due to current .NET limitations, so the meilleure one fully replaces them for now. * Fix MapPhysicalMemory on the software MemoryManager. * Null check for GetHostAddress, docs * Add configuration for setting memory manager mode (not in UI yet) * Add config to UI * Fix type mismatch on Unix signal handler code emit * Fix 6GB DRAM mode. The size can be greater than `uint.MaxValue` when the DRAM is >4GB. * Address some feedback. * More detailed error if backing memory cannot be mapped. * SetLastError on all OS functions for consistency * Force pages dirty with UBO update instead of setting them directly. Seems to be much faster across a few games. Need retesting. * Rebase, configuration rework, fix mem tracking regression * Fix race in FreePages * Set memory managers null after decrementing ref count * Remove readonly keyword, as this is now modified. * Use a local variable for the signal handler rather than a register. * Fix bug with buffer resize, and index/uniform buffer binding. Should fix flickering in games. * Add InvalidAccessHandler to MemoryTracking Doesn't do anything yet * Call invalid access handler on unmapped read/write. Same rules as the regular memory manager. * Make unsafe mapped memory its own MemoryManagerType * Move FlushUboDirty into UpdateState. * Buffer dirty cache, rather than ubo cache Much cleaner, may be reusable for Inline2Memory updates. * This doesn't return anything anymore. * Add sigaction remove methods, correct a few function signatures. * Return empty list of physical regions for size 0. * Also on AddressSpaceManager Co-authored-by: gdkchan <gab.dark.100@gmail.com>
254 lines
10 KiB
C#
254 lines
10 KiB
C#
using Ryujinx.Memory.Range;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Ryujinx.Memory.Tracking
|
|
{
|
|
/// <summary>
|
|
/// Manages memory tracking for a given virutal/physical memory block.
|
|
/// </summary>
|
|
public class MemoryTracking
|
|
{
|
|
private readonly IVirtualMemoryManager _memoryManager;
|
|
private readonly InvalidAccessHandler _invalidAccessHandler;
|
|
|
|
// Only use these from within the lock.
|
|
private readonly NonOverlappingRangeList<VirtualRegion> _virtualRegions;
|
|
|
|
// Only use these from within the lock.
|
|
private readonly VirtualRegion[] _virtualResults = new VirtualRegion[10];
|
|
|
|
private readonly int _pageSize;
|
|
|
|
/// <summary>
|
|
/// This lock must be obtained when traversing or updating the region-handle hierarchy.
|
|
/// It is not required when reading dirty flags.
|
|
/// </summary>
|
|
internal object TrackingLock = new object();
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="memoryManager">Virtual memory manager</param>
|
|
/// <param name="block">Physical memory block</param>
|
|
/// <param name="pageSize">Page size of the virtual memory space</param>
|
|
public MemoryTracking(IVirtualMemoryManager memoryManager, int pageSize, InvalidAccessHandler invalidAccessHandler = null)
|
|
{
|
|
_memoryManager = memoryManager;
|
|
_pageSize = pageSize;
|
|
_invalidAccessHandler = invalidAccessHandler;
|
|
|
|
_virtualRegions = new NonOverlappingRangeList<VirtualRegion>();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="va">Virtual memory address</param>
|
|
/// <param name="size">Size to be mapped</param>
|
|
public void Map(ulong va, 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.UpdateProtection();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Indicate that a virtual region has been unmapped.
|
|
/// Should be called before the unmapping is complete.
|
|
/// </summary>
|
|
/// <param name="va">Virtual memory address</param>
|
|
/// <param name="size">Size to be unmapped</param>
|
|
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 notify them that the region was unmapped.
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of virtual regions that a handle covers.
|
|
/// </summary>
|
|
/// <param name="va">Starting virtual memory address of the handle</param>
|
|
/// <param name="size">Size of the handle's memory region</param>
|
|
/// <returns>A list of virtual regions within the given range</returns>
|
|
internal List<VirtualRegion> GetVirtualRegionsForHandle(ulong va, ulong size)
|
|
{
|
|
List<VirtualRegion> result = new List<VirtualRegion>();
|
|
_virtualRegions.GetOrAddRegions(result, va, size, (va, size) => new VirtualRegion(this, va, size));
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a virtual region from the range list. This assumes that the lock has been acquired.
|
|
/// </summary>
|
|
/// <param name="region">Region to remove</param>
|
|
internal void RemoveVirtual(VirtualRegion region)
|
|
{
|
|
_virtualRegions.Remove(region);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
|
/// </summary>
|
|
/// <param name="address">CPU virtual address of the region</param>
|
|
/// <param name="size">Size of the region</param>
|
|
/// <param name="granularity">Desired granularity of write tracking</param>
|
|
/// <returns>The memory tracking handle</returns>
|
|
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, ulong granularity)
|
|
{
|
|
(address, size) = PageAlign(address, size);
|
|
|
|
return new MultiRegionHandle(this, address, size, granularity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
|
/// </summary>
|
|
/// <param name="address">CPU virtual address of the region</param>
|
|
/// <param name="size">Size of the region</param>
|
|
/// <param name="granularity">Desired granularity of write tracking</param>
|
|
/// <returns>The memory tracking handle</returns>
|
|
public SmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity)
|
|
{
|
|
(address, size) = PageAlign(address, size);
|
|
|
|
return new SmartMultiRegionHandle(this, address, size, granularity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
|
|
/// </summary>
|
|
/// <param name="address">CPU virtual address of the region</param>
|
|
/// <param name="size">Size of the region</param>
|
|
/// <returns>The memory tracking handle</returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signal that a virtual memory event happened at the given location (one byte).
|
|
/// </summary>
|
|
/// <param name="address">Virtual address accessed</param>
|
|
/// <param name="write">Whether the address was written to or read</param>
|
|
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
|
public bool VirtualMemoryEventTracking(ulong address, bool write)
|
|
{
|
|
return VirtualMemoryEvent(address, 1, write);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signal that a virtual memory event happened at the given location.
|
|
/// </summary>
|
|
/// <param name="address">Virtual address accessed</param>
|
|
/// <param name="size">Size of the region affected in bytes</param>
|
|
/// <param name="write">Whether the region was written to or read</param>
|
|
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
|
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)
|
|
{
|
|
if (!_memoryManager.IsMapped(address))
|
|
{
|
|
_invalidAccessHandler?.Invoke(address);
|
|
|
|
// We can't continue - it's impossible to remove protection from the page.
|
|
// Even if the access handler wants us to continue, we wouldn't be able to.
|
|
throw new InvalidMemoryRegionException();
|
|
}
|
|
|
|
_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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reprotect a given virtual region. The virtual memory manager will handle this.
|
|
/// </summary>
|
|
/// <param name="region">Region to reprotect</param>
|
|
/// <param name="permission">Memory permission to protect with</param>
|
|
internal void ProtectVirtualRegion(VirtualRegion region, MemoryPermission permission)
|
|
{
|
|
_memoryManager.TrackingReprotect(region.Address, region.Size, permission);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the number of virtual regions currently being tracked.
|
|
/// Useful for tests and metrics.
|
|
/// </summary>
|
|
/// <returns>The number of virtual regions</returns>
|
|
public int GetRegionCount()
|
|
{
|
|
lock (TrackingLock)
|
|
{
|
|
return _virtualRegions.Count;
|
|
}
|
|
}
|
|
}
|
|
}
|