mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-12-12 20:42:02 +00: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>
235 lines
No EOL
9.3 KiB
C#
235 lines
No EOL
9.3 KiB
C#
using Ryujinx.Cpu;
|
|
using Ryujinx.Cpu.Tracking;
|
|
using Ryujinx.Memory;
|
|
using Ryujinx.Memory.Range;
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
{
|
|
/// <summary>
|
|
/// Represents physical memory, accessible from the GPU.
|
|
/// This is actually working CPU virtual addresses, of memory mapped on the application process.
|
|
/// </summary>
|
|
class PhysicalMemory : IDisposable
|
|
{
|
|
public const int PageSize = 0x1000;
|
|
|
|
private IVirtualMemoryManagerTracked _cpuMemory;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the physical memory.
|
|
/// </summary>
|
|
/// <param name="cpuMemory">CPU memory manager of the application process</param>
|
|
public PhysicalMemory(IVirtualMemoryManagerTracked cpuMemory)
|
|
{
|
|
_cpuMemory = cpuMemory;
|
|
|
|
if (_cpuMemory is IRefCounted rc)
|
|
{
|
|
rc.IncrementReferenceCount();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a span of data from the application process.
|
|
/// </summary>
|
|
/// <param name="address">Start address of the range</param>
|
|
/// <param name="size">Size in bytes to be range</param>
|
|
/// <param name="tracked">True if read tracking is triggered on the span</param>
|
|
/// <returns>A read only span of the data at the specified memory location</returns>
|
|
public ReadOnlySpan<byte> GetSpan(ulong address, int size, bool tracked = false)
|
|
{
|
|
return _cpuMemory.GetSpan(address, size, tracked);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a span of data from the application process.
|
|
/// </summary>
|
|
/// <param name="range">Ranges of physical memory where the data is located</param>
|
|
/// <param name="tracked">True if read tracking is triggered on the span</param>
|
|
/// <returns>A read only span of the data at the specified memory location</returns>
|
|
public ReadOnlySpan<byte> GetSpan(MultiRange range, bool tracked = false)
|
|
{
|
|
if (range.Count == 1)
|
|
{
|
|
var singleRange = range.GetSubRange(0);
|
|
return _cpuMemory.GetSpan(singleRange.Address, (int)singleRange.Size, tracked);
|
|
}
|
|
else
|
|
{
|
|
Span<byte> data = new byte[range.GetSize()];
|
|
|
|
int offset = 0;
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
|
{
|
|
var currentRange = range.GetSubRange(i);
|
|
int size = (int)currentRange.Size;
|
|
_cpuMemory.GetSpan(currentRange.Address, size, tracked).CopyTo(data.Slice(offset, size));
|
|
offset += size;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a writable region from the application process.
|
|
/// </summary>
|
|
/// <param name="address">Start address of the range</param>
|
|
/// <param name="size">Size in bytes to be range</param>
|
|
/// <returns>A writable region with the data at the specified memory location</returns>
|
|
public WritableRegion GetWritableRegion(ulong address, int size)
|
|
{
|
|
return _cpuMemory.GetWritableRegion(address, size);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads data from the application process.
|
|
/// </summary>
|
|
/// <typeparam name="T">Type of the structure</typeparam>
|
|
/// <param name="gpuVa">Address to read from</param>
|
|
/// <returns>The data at the specified memory location</returns>
|
|
public T Read<T>(ulong address) where T : unmanaged
|
|
{
|
|
return MemoryMarshal.Cast<byte, T>(GetSpan(address, Unsafe.SizeOf<T>()))[0];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes data to the application process.
|
|
/// </summary>
|
|
/// <param name="address">Address to write into</param>
|
|
/// <param name="data">Data to be written</param>
|
|
public void Write(ulong address, ReadOnlySpan<byte> data)
|
|
{
|
|
_cpuMemory.Write(address, data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes data to the application process.
|
|
/// </summary>
|
|
/// <param name="range">Ranges of physical memory where the data is located</param>
|
|
/// <param name="data">Data to be written</param>
|
|
public void Write(MultiRange range, ReadOnlySpan<byte> data)
|
|
{
|
|
WriteImpl(range, data, _cpuMemory.Write);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes data to the application process, without any tracking.
|
|
/// </summary>
|
|
/// <param name="address">Address to write into</param>
|
|
/// <param name="data">Data to be written</param>
|
|
public void WriteUntracked(ulong address, ReadOnlySpan<byte> data)
|
|
{
|
|
_cpuMemory.WriteUntracked(address, data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes data to the application process, without any tracking.
|
|
/// </summary>
|
|
/// <param name="range">Ranges of physical memory where the data is located</param>
|
|
/// <param name="data">Data to be written</param>
|
|
public void WriteUntracked(MultiRange range, ReadOnlySpan<byte> data)
|
|
{
|
|
WriteImpl(range, data, _cpuMemory.WriteUntracked);
|
|
}
|
|
|
|
private delegate void WriteCallback(ulong address, ReadOnlySpan<byte> data);
|
|
|
|
/// <summary>
|
|
/// Writes data to the application process, using the supplied callback method.
|
|
/// </summary>
|
|
/// <param name="range">Ranges of physical memory where the data is located</param>
|
|
/// <param name="data">Data to be written</param>
|
|
/// <param name="writeCallback">Callback method that will perform the write</param>
|
|
private void WriteImpl(MultiRange range, ReadOnlySpan<byte> data, WriteCallback writeCallback)
|
|
{
|
|
if (range.Count == 1)
|
|
{
|
|
var singleRange = range.GetSubRange(0);
|
|
writeCallback(singleRange.Address, data);
|
|
}
|
|
else
|
|
{
|
|
int offset = 0;
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
|
{
|
|
var currentRange = range.GetSubRange(i);
|
|
int size = (int)currentRange.Size;
|
|
writeCallback(currentRange.Address, data.Slice(offset, size));
|
|
offset += size;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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 CpuRegionHandle BeginTracking(ulong address, ulong size)
|
|
{
|
|
return _cpuMemory.BeginTracking(address, size);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
|
|
/// </summary>
|
|
/// <param name="range">Ranges of physical memory where the data is located</param>
|
|
/// <returns>The memory tracking handle</returns>
|
|
public GpuRegionHandle BeginTracking(MultiRange range)
|
|
{
|
|
var cpuRegionHandles = new CpuRegionHandle[range.Count];
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
|
{
|
|
var currentRange = range.GetSubRange(i);
|
|
cpuRegionHandles[i] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size);
|
|
}
|
|
|
|
return new GpuRegionHandle(cpuRegionHandles);
|
|
}
|
|
|
|
/// <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 CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, ulong granularity = 4096)
|
|
{
|
|
return _cpuMemory.BeginGranularTracking(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 CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity = 4096)
|
|
{
|
|
return _cpuMemory.BeginSmartGranularTracking(address, size, granularity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Release our reference to the CPU memory manager.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if (_cpuMemory is IRefCounted rc)
|
|
{
|
|
rc.DecrementReferenceCount();
|
|
|
|
_cpuMemory = null;
|
|
}
|
|
}
|
|
}
|
|
} |