Direct mapping with alias support on Windows

This commit is contained in:
gdkchan 2021-06-21 21:31:16 -03:00 committed by gdk
parent b4b7d4723e
commit 335d91a367
21 changed files with 478 additions and 482 deletions

View file

@ -127,8 +127,21 @@ namespace Ryujinx.Cpu
/// <inheritdoc/> /// <inheritdoc/>
public T ReadTracked<T>(ulong va) where T : unmanaged public T ReadTracked<T>(ulong va) where T : unmanaged
{ {
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false); try
return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0]; {
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false);
return Read<T>(va);
}
catch (InvalidMemoryRegionException)
{
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
{
throw;
}
return default;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -301,7 +314,7 @@ namespace Ryujinx.Cpu
return (int)(vaSpan / PageSize); return (int)(vaSpan / PageSize);
} }
private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException(); private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va); private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);

View file

@ -7,6 +7,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
namespace Ryujinx.Cpu namespace Ryujinx.Cpu
@ -14,7 +15,7 @@ namespace Ryujinx.Cpu
/// <summary> /// <summary>
/// Represents a CPU memory manager which maps guest virtual memory directly onto a host virtual region. /// Represents a CPU memory manager which maps guest virtual memory directly onto a host virtual region.
/// </summary> /// </summary>
public class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked public class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
{ {
public const int PageBits = 12; public const int PageBits = 12;
public const int PageSize = 1 << PageBits; public const int PageSize = 1 << PageBits;
@ -39,12 +40,14 @@ namespace Ryujinx.Cpu
private readonly bool _unsafeMode; private readonly bool _unsafeMode;
private readonly MemoryBlock _addressSpace; private readonly MemoryBlock _addressSpace;
private readonly MemoryBlock _addressSpaceMirror;
private readonly ulong _addressSpaceSize; private readonly ulong _addressSpaceSize;
private readonly MemoryBlock _backingMemory;
private readonly PageTable<ulong> _pageTable;
private readonly MemoryEhMeilleure _memoryEh; private readonly MemoryEhMeilleure _memoryEh;
private ulong[] _pageTable; private readonly ulong[] _pageBitmap;
public int AddressSpaceBits { get; } public int AddressSpaceBits { get; }
@ -59,11 +62,14 @@ namespace Ryujinx.Cpu
/// <summary> /// <summary>
/// Creates a new instance of the host mapped memory manager. /// Creates a new instance of the host mapped memory manager.
/// </summary> /// </summary>
/// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
/// <param name="addressSpaceSize">Size of the address space</param> /// <param name="addressSpaceSize">Size of the address space</param>
/// <param name="unsafeMode">True if unmanaged access should not be masked (unsafe), false otherwise.</param> /// <param name="unsafeMode">True if unmanaged access should not be masked (unsafe), false otherwise.</param>
/// <param name="invalidAccessHandler">Optional function to handle invalid memory accesses</param> /// <param name="invalidAccessHandler">Optional function to handle invalid memory accesses</param>
public MemoryManagerHostMapped(ulong addressSpaceSize, bool unsafeMode, InvalidAccessHandler invalidAccessHandler = null) public MemoryManagerHostMapped(MemoryBlock backingMemory, ulong addressSpaceSize, bool unsafeMode, InvalidAccessHandler invalidAccessHandler = null)
{ {
_backingMemory = backingMemory;
_pageTable = new PageTable<ulong>();
_invalidAccessHandler = invalidAccessHandler; _invalidAccessHandler = invalidAccessHandler;
_unsafeMode = unsafeMode; _unsafeMode = unsafeMode;
_addressSpaceSize = addressSpaceSize; _addressSpaceSize = addressSpaceSize;
@ -79,9 +85,8 @@ namespace Ryujinx.Cpu
AddressSpaceBits = asBits; AddressSpaceBits = asBits;
_pageTable = new ulong[1 << (AddressSpaceBits - (PageBits + PageToPteShift))]; _pageBitmap = new ulong[1 << (AddressSpaceBits - (PageBits + PageToPteShift))];
_addressSpace = new MemoryBlock(asSize, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable); _addressSpace = new MemoryBlock(asSize, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
_addressSpaceMirror = _addressSpace.CreateMirror();
Tracking = new MemoryTracking(this, PageSize, invalidAccessHandler); Tracking = new MemoryTracking(this, PageSize, invalidAccessHandler);
_memoryEh = new MemoryEhMeilleure(_addressSpace, Tracking); _memoryEh = new MemoryEhMeilleure(_addressSpace, Tracking);
} }
@ -140,12 +145,25 @@ namespace Ryujinx.Cpu
{ {
AssertValidAddressAndSize(va, size); AssertValidAddressAndSize(va, size);
_addressSpace.Commit(va, size); PtMap(va, pa, size);
_addressSpace.MapView(_backingMemory, pa, va, size);
AddMapping(va, size); AddMapping(va, size);
Tracking.Map(va, size); Tracking.Map(va, size);
} }
private void PtMap(ulong va, ulong pa, ulong size)
{
while (size != 0)
{
_pageTable.Map(va, pa);
va += PageSize;
pa += PageSize;
size -= PageSize;
}
}
/// <inheritdoc/> /// <inheritdoc/>
public void Unmap(ulong va, ulong size) public void Unmap(ulong va, ulong size)
{ {
@ -155,27 +173,25 @@ namespace Ryujinx.Cpu
Tracking.Unmap(va, size); Tracking.Unmap(va, size);
RemoveMapping(va, size); RemoveMapping(va, size);
_addressSpace.Decommit(va, size); _addressSpace.UnmapView(va, size);
PtUnmap(va, size);
}
private void PtUnmap(ulong va, ulong size)
{
while (size != 0)
{
_pageTable.Unmap(va);
va += PageSize;
size -= PageSize;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
public T Read<T>(ulong va) where T : unmanaged public T Read<T>(ulong va) where T : unmanaged
{ {
try return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
{
AssertMapped(va, (ulong)Unsafe.SizeOf<T>());
return _addressSpaceMirror.Read<T>(va);
}
catch (InvalidMemoryRegionException)
{
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
{
throw;
}
return default;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -201,46 +217,73 @@ namespace Ryujinx.Cpu
/// <inheritdoc/> /// <inheritdoc/>
public void Read(ulong va, Span<byte> data) public void Read(ulong va, Span<byte> data)
{ {
try ReadImpl(va, data);
{
AssertMapped(va, (ulong)data.Length);
_addressSpaceMirror.Read(va, data);
}
catch (InvalidMemoryRegionException)
{
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
{
throw;
}
}
} }
/// <inheritdoc/> /// <inheritdoc/>
public void Write<T>(ulong va, T value) where T : unmanaged public void Write<T>(ulong va, T value) where T : unmanaged
{ {
try Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
{
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), write: true);
_addressSpaceMirror.Write(va, value);
}
catch (InvalidMemoryRegionException)
{
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
{
throw;
}
}
} }
/// <inheritdoc/> /// <inheritdoc/>
public void Write(ulong va, ReadOnlySpan<byte> data) public void Write(ulong va, ReadOnlySpan<byte> data)
{ {
try { if (data.Length == 0)
SignalMemoryTracking(va, (ulong)data.Length, write: true); {
return;
}
_addressSpaceMirror.Write(va, data); SignalMemoryTracking(va, (ulong)data.Length, true);
WriteImpl(va, data);
}
/// <inheritdoc/>
public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
{
if (data.Length == 0)
{
return;
}
WriteImpl(va, data);
}
private void WriteImpl(ulong va, ReadOnlySpan<byte> data)
{
try
{
AssertValidAddressAndSize(va, (ulong)data.Length);
if (IsContiguousAndMapped(va, data.Length))
{
data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length));
}
else
{
int offset = 0, size;
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));
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));
}
}
} }
catch (InvalidMemoryRegionException) catch (InvalidMemoryRegionException)
{ {
@ -251,37 +294,31 @@ namespace Ryujinx.Cpu
} }
} }
/// <inheritdoc/>
public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
{
try
{
AssertMapped(va, (ulong)data.Length);
_addressSpaceMirror.Write(va, data);
}
catch (InvalidMemoryRegionException)
{
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
{
throw;
}
}
}
/// <inheritdoc/> /// <inheritdoc/>
public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false) public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
{ {
if (size == 0)
{
return ReadOnlySpan<byte>.Empty;
}
if (tracked) if (tracked)
{ {
SignalMemoryTracking(va, (ulong)size, write: false); SignalMemoryTracking(va, (ulong)size, false);
}
if (IsContiguousAndMapped(va, size))
{
return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size);
} }
else else
{ {
AssertMapped(va, (ulong)size); Span<byte> data = new byte[size];
}
return _addressSpaceMirror.GetSpan(va, size); ReadImpl(va, data);
return data;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -291,20 +328,35 @@ namespace Ryujinx.Cpu
{ {
SignalMemoryTracking(va, (ulong)size, true); SignalMemoryTracking(va, (ulong)size, true);
} }
else
if (size == 0)
{ {
AssertMapped(va, (ulong)size); return new WritableRegion(null, va, Memory<byte>.Empty);
} }
return _addressSpaceMirror.GetWritableRegion(va, size); if (IsContiguousAndMapped(va, size))
{
return new WritableRegion(null, va, _backingMemory.GetMemory(GetPhysicalAddressInternal(va), size));
}
else
{
Memory<byte> memory = new byte[size];
GetSpan(va, size).CopyTo(memory.Span);
return new WritableRegion(this, va, memory);
}
} }
/// <inheritdoc/> /// <inheritdoc/>
public ref T GetRef<T>(ulong va) where T : unmanaged public ref T GetRef<T>(ulong va) where T : unmanaged
{ {
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), true); if (!IsContiguous(va, Unsafe.SizeOf<T>()))
{
ThrowMemoryNotContiguous();
}
return ref _addressSpaceMirror.GetRef<T>(va); return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -322,7 +374,7 @@ namespace Ryujinx.Cpu
int bit = (int)((page & 31) << 1); int bit = (int)((page & 31) << 1);
int pageIndex = (int)(page >> PageToPteShift); int pageIndex = (int)(page >> PageToPteShift);
ref ulong pageRef = ref _pageTable[pageIndex]; ref ulong pageRef = ref _pageBitmap[pageIndex];
ulong pte = Volatile.Read(ref pageRef); ulong pte = Volatile.Read(ref pageRef);
@ -373,7 +425,7 @@ namespace Ryujinx.Cpu
mask &= endMask; mask &= endMask;
} }
ref ulong pageRef = ref _pageTable[pageIndex++]; ref ulong pageRef = ref _pageBitmap[pageIndex++];
ulong pte = Volatile.Read(ref pageRef); ulong pte = Volatile.Read(ref pageRef);
pte |= pte >> 1; pte |= pte >> 1;
@ -388,10 +440,124 @@ namespace Ryujinx.Cpu
return true; return true;
} }
private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguous(ulong va, int size)
{
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, (ulong)size))
{
return false;
}
int pages = GetPagesCount(va, (uint)size, out va);
for (int page = 0; page < pages - 1; page++)
{
if (!ValidateAddress(va + PageSize))
{
return false;
}
if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize))
{
return false;
}
va += PageSize;
}
return true;
}
/// <inheritdoc/> /// <inheritdoc/>
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size) public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
{ {
throw new NotImplementedException(); if (size == 0)
{
return Enumerable.Empty<MemoryRange>();
}
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
{
return null;
}
int pages = GetPagesCount(va, (uint)size, out va);
var regions = new List<MemoryRange>();
ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize;
for (int page = 0; page < pages - 1; page++)
{
if (!ValidateAddress(va + PageSize))
{
return null;
}
ulong newPa = GetPhysicalAddressInternal(va + PageSize);
if (GetPhysicalAddressInternal(va) + PageSize != newPa)
{
regions.Add(new MemoryRange(regionStart, regionSize));
regionStart = newPa;
regionSize = 0;
}
va += PageSize;
regionSize += PageSize;
}
regions.Add(new MemoryRange(regionStart, regionSize));
return regions;
}
private void ReadImpl(ulong va, Span<byte> data)
{
if (data.Length == 0)
{
return;
}
try
{
AssertValidAddressAndSize(va, (ulong)data.Length);
int offset = 0, size;
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));
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));
}
}
catch (InvalidMemoryRegionException)
{
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
{
throw;
}
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -420,7 +586,7 @@ namespace Ryujinx.Cpu
int bit = (int)((pageStart & 31) << 1); int bit = (int)((pageStart & 31) << 1);
int pageIndex = (int)(pageStart >> PageToPteShift); int pageIndex = (int)(pageStart >> PageToPteShift);
ref ulong pageRef = ref _pageTable[pageIndex]; ref ulong pageRef = ref _pageBitmap[pageIndex];
ulong pte = Volatile.Read(ref pageRef); ulong pte = Volatile.Read(ref pageRef);
ulong state = ((pte >> bit) & 3); ulong state = ((pte >> bit) & 3);
@ -452,7 +618,7 @@ namespace Ryujinx.Cpu
mask &= endMask; mask &= endMask;
} }
ref ulong pageRef = ref _pageTable[pageIndex++]; ref ulong pageRef = ref _pageBitmap[pageIndex++];
ulong pte = Volatile.Read(ref pageRef); ulong pte = Volatile.Read(ref pageRef);
ulong mappedMask = mask & BlockMappedMask; ulong mappedMask = mask & BlockMappedMask;
@ -523,7 +689,7 @@ namespace Ryujinx.Cpu
ulong tag = protTag << bit; ulong tag = protTag << bit;
int pageIndex = (int)(pageStart >> PageToPteShift); int pageIndex = (int)(pageStart >> PageToPteShift);
ref ulong pageRef = ref _pageTable[pageIndex]; ref ulong pageRef = ref _pageBitmap[pageIndex];
ulong pte; ulong pte;
@ -555,7 +721,7 @@ namespace Ryujinx.Cpu
mask &= endMask; mask &= endMask;
} }
ref ulong pageRef = ref _pageTable[pageIndex++]; ref ulong pageRef = ref _pageBitmap[pageIndex++];
ulong pte; ulong pte;
ulong mappedMask; ulong mappedMask;
@ -625,7 +791,7 @@ namespace Ryujinx.Cpu
mask &= endMask; mask &= endMask;
} }
ref ulong pageRef = ref _pageTable[pageIndex++]; ref ulong pageRef = ref _pageBitmap[pageIndex++];
ulong pte; ulong pte;
ulong mappedMask; ulong mappedMask;
@ -670,7 +836,7 @@ namespace Ryujinx.Cpu
mask |= endMask; mask |= endMask;
} }
ref ulong pageRef = ref _pageTable[pageIndex++]; ref ulong pageRef = ref _pageBitmap[pageIndex++];
ulong pte; ulong pte;
do do
@ -683,12 +849,27 @@ namespace Ryujinx.Cpu
} }
} }
private ulong GetPhysicalAddress(ulong va)
{
// We return -1L if the virtual address is invalid or unmapped.
if (!ValidateAddress(va) || !IsMapped(va))
{
return ulong.MaxValue;
}
return GetPhysicalAddressInternal(va);
}
private ulong GetPhysicalAddressInternal(ulong va)
{
return _pageTable.Read(va) + (va & PageMask);
}
/// <summary> /// <summary>
/// Disposes of resources used by the memory manager. /// Disposes of resources used by the memory manager.
/// </summary> /// </summary>
protected override void Destroy() protected override void Destroy()
{ {
_addressSpaceMirror.Dispose();
_addressSpace.Dispose(); _addressSpace.Dispose();
_memoryEh.Dispose(); _memoryEh.Dispose();
} }

View file

@ -29,7 +29,7 @@ namespace Ryujinx.HLE.HOS
case MemoryManagerMode.HostMapped: case MemoryManagerMode.HostMapped:
case MemoryManagerMode.HostMappedUnsafe: case MemoryManagerMode.HostMappedUnsafe:
bool unsafeMode = mode == MemoryManagerMode.HostMappedUnsafe; bool unsafeMode = mode == MemoryManagerMode.HostMappedUnsafe;
return new ArmProcessContext<MemoryManagerHostMapped>(pid, _gpu, new MemoryManagerHostMapped(addressSpaceSize, unsafeMode, invalidAccessHandler), for64Bit); return new ArmProcessContext<MemoryManagerHostMapped>(pid, _gpu, new MemoryManagerHostMapped(context.Memory, addressSpaceSize, unsafeMode, invalidAccessHandler), for64Bit);
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();

View file

@ -9,8 +9,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
private readonly IVirtualMemoryManager _cpuMemory; private readonly IVirtualMemoryManager _cpuMemory;
public override bool SupportsMemoryAliasing => true;
public KPageTable(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context) public KPageTable(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context)
{ {
_cpuMemory = cpuMemory; _cpuMemory = cpuMemory;

View file

@ -1,11 +1,9 @@
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.Memory.Range;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
namespace Ryujinx.HLE.HOS.Kernel.Memory namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
@ -73,8 +71,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
private MersenneTwister _randomNumberGenerator; private MersenneTwister _randomNumberGenerator;
public abstract bool SupportsMemoryAliasing { get; }
private MemoryFillValue _heapFillValue; private MemoryFillValue _heapFillValue;
private MemoryFillValue _ipcFillValue; private MemoryFillValue _ipcFillValue;
@ -305,7 +301,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
TlsIoRegionStart = tlsIoRegion.Start; TlsIoRegionStart = tlsIoRegion.Start;
TlsIoRegionEnd = tlsIoRegion.End; TlsIoRegionEnd = tlsIoRegion.End;
// TODO: Check kernel configuration via secure monitor call when implemented to set memory fill values. // TODO: Check kernel configuration via secure monitor call when implemented to set memory fill values.
_currentHeapAddr = HeapRegionStart; _currentHeapAddr = HeapRegionStart;
_heapCapacity = 0; _heapCapacity = 0;
@ -1692,11 +1688,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
bool send, bool send,
out ulong dst) out ulong dst)
{ {
if (!SupportsMemoryAliasing)
{
throw new NotSupportedException("Memory aliasing not supported, can't map IPC buffers.");
}
dst = 0; dst = 0;
if (!_slabManager.CanAllocate(MaxBlocksNeededForInsertion)) if (!_slabManager.CanAllocate(MaxBlocksNeededForInsertion))

View file

@ -1,133 +0,0 @@
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.Memory;
using Ryujinx.Memory.Range;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ryujinx.HLE.HOS.Kernel.Memory
{
class KPageTableHostMapped : KPageTableBase
{
private const int CopyChunckSize = 0x100000;
private readonly IVirtualMemoryManager _cpuMemory;
public override bool SupportsMemoryAliasing => false;
public KPageTableHostMapped(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context)
{
_cpuMemory = cpuMemory;
}
/// <inheritdoc/>
protected override void GetPhysicalRegions(ulong va, ulong size, KPageList pageList)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
protected override ReadOnlySpan<byte> GetSpan(ulong va, int size)
{
return _cpuMemory.GetSpan(va, size);
}
/// <inheritdoc/>
protected override KernelResult MapMemory(ulong src, ulong dst, ulong pagesCount, KMemoryPermission oldSrcPermission, KMemoryPermission newDstPermission)
{
ulong size = pagesCount * PageSize;
_cpuMemory.Map(dst, 0, size);
ulong currentSize = size;
while (currentSize > 0)
{
ulong copySize = Math.Min(currentSize, CopyChunckSize);
_cpuMemory.Write(dst, _cpuMemory.GetSpan(src, (int)copySize));
currentSize -= copySize;
}
return KernelResult.Success;
}
/// <inheritdoc/>
protected override KernelResult UnmapMemory(ulong dst, ulong src, ulong pagesCount, KMemoryPermission oldDstPermission, KMemoryPermission newSrcPermission)
{
ulong size = pagesCount * PageSize;
// TODO: Validation.
ulong currentSize = size;
while (currentSize > 0)
{
ulong copySize = Math.Min(currentSize, CopyChunckSize);
_cpuMemory.Write(src, _cpuMemory.GetSpan(dst, (int)copySize));
currentSize -= copySize;
}
_cpuMemory.Unmap(dst, size);
return KernelResult.Success;
}
/// <inheritdoc/>
protected override KernelResult MapPages(ulong dstVa, ulong pagesCount, ulong srcPa, KMemoryPermission permission, bool shouldFillPages, byte fillValue)
{
_cpuMemory.Map(dstVa, 0, pagesCount * PageSize);
if (shouldFillPages)
{
_cpuMemory.Fill(dstVa, pagesCount * PageSize, fillValue);
}
return KernelResult.Success;
}
/// <inheritdoc/>
protected override KernelResult MapPages(ulong address, KPageList pageList, KMemoryPermission permission, bool shouldFillPages, byte fillValue)
{
ulong pagesCount = pageList.GetPagesCount();
_cpuMemory.Map(address, 0, pagesCount * PageSize);
if (shouldFillPages)
{
_cpuMemory.Fill(address, pagesCount * PageSize, fillValue);
}
return KernelResult.Success;
}
/// <inheritdoc/>
protected override KernelResult Unmap(ulong address, ulong pagesCount)
{
_cpuMemory.Unmap(address, pagesCount * PageSize);
return KernelResult.Success;
}
/// <inheritdoc/>
protected override KernelResult Reprotect(ulong address, ulong pagesCount, KMemoryPermission permission)
{
// TODO.
return KernelResult.Success;
}
/// <inheritdoc/>
protected override KernelResult ReprotectWithAttributes(ulong address, ulong pagesCount, KMemoryPermission permission)
{
// TODO.
return KernelResult.Success;
}
/// <inheritdoc/>
protected override void SignalMemoryTracking(ulong va, ulong size, bool write)
{
_cpuMemory.SignalMemoryTracking(va, size, write);
}
/// <inheritdoc/>
protected override void Write(ulong va, ReadOnlySpan<byte> data)
{
_cpuMemory.Write(va, data);
}
}
}

View file

@ -50,14 +50,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
return KernelResult.InvalidPermission; return KernelResult.InvalidPermission;
} }
KernelResult result = memoryManager.MapPages(address, pageList, MemoryState.SharedMemory, permission); return memoryManager.MapPages(address, pageList, MemoryState.SharedMemory, permission);
if (result == KernelResult.Success && !memoryManager.SupportsMemoryAliasing)
{
_storage.Borrow(process, address);
}
return result;
} }
public KernelResult UnmapFromProcess(KPageTableBase memoryManager, ulong address, ulong size, KProcess process) public KernelResult UnmapFromProcess(KPageTableBase memoryManager, ulong address, ulong size, KProcess process)

View file

@ -94,11 +94,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
if (result == KernelResult.Success) if (result == KernelResult.Success)
{ {
_isMapped = true; _isMapped = true;
if (!memoryManager.SupportsMemoryAliasing)
{
_storage.Borrow(process, address);
}
} }
return result; return result;

View file

@ -1,8 +1,4 @@
using Ryujinx.HLE.HOS.Kernel.Process; using System;
using Ryujinx.Memory;
using Ryujinx.Memory.Range;
using System;
using System.Collections.Generic;
namespace Ryujinx.HLE.HOS.Kernel.Memory namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
@ -12,9 +8,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
private readonly KPageList _pageList; private readonly KPageList _pageList;
private readonly ulong _size; private readonly ulong _size;
private IVirtualMemoryManager _borrowerMemory;
private ulong _borrowerVa;
public SharedMemoryStorage(KernelContext context, KPageList pageList) public SharedMemoryStorage(KernelContext context, KPageList pageList)
{ {
_context = context; _context = context;
@ -29,24 +22,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
} }
} }
public void Borrow(KProcess dstProcess, ulong va)
{
ulong currentOffset = 0;
foreach (KPageNode pageNode in _pageList)
{
ulong address = pageNode.Address - DramMemoryMap.DramBase;
ulong size = pageNode.PagesCount * KPageTableBase.PageSize;
dstProcess.CpuMemory.Write(va + currentOffset, _context.Memory.GetSpan(address + currentOffset, (int)size));
currentOffset += size;
}
_borrowerMemory = dstProcess.CpuMemory;
_borrowerVa = va;
}
public void ZeroFill() public void ZeroFill()
{ {
for (ulong offset = 0; offset < _size; offset += sizeof(ulong)) for (ulong offset = 0; offset < _size; offset += sizeof(ulong))
@ -57,20 +32,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
public ref T GetRef<T>(ulong offset) where T : unmanaged public ref T GetRef<T>(ulong offset) where T : unmanaged
{ {
if (_borrowerMemory == null) if (_pageList.Nodes.Count == 1)
{ {
if (_pageList.Nodes.Count == 1) ulong address = _pageList.Nodes.First.Value.Address - DramMemoryMap.DramBase;
{ return ref _context.Memory.GetRef<T>(address + offset);
ulong address = _pageList.Nodes.First.Value.Address - DramMemoryMap.DramBase; }
return ref _context.Memory.GetRef<T>(address + offset);
}
throw new NotImplementedException("Non-contiguous shared memory is not yet supported."); throw new NotImplementedException("Non-contiguous shared memory is not yet supported.");
}
else
{
return ref _borrowerMemory.GetRef<T>(_borrowerVa + offset);
}
} }
public KPageList GetPageList() public KPageList GetPageList()

View file

@ -1075,14 +1075,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
Context = _contextFactory.Create(KernelContext, Pid, 1UL << addrSpaceBits, InvalidAccessHandler, for64Bit); Context = _contextFactory.Create(KernelContext, Pid, 1UL << addrSpaceBits, InvalidAccessHandler, for64Bit);
if (Context.AddressSpace is MemoryManagerHostMapped) MemoryManager = new KPageTable(KernelContext, CpuMemory);
{
MemoryManager = new KPageTableHostMapped(KernelContext, CpuMemory);
}
else
{
MemoryManager = new KPageTable(KernelContext, CpuMemory);
}
} }
private bool InvalidAccessHandler(ulong va) private bool InvalidAccessHandler(ulong va)

View file

@ -60,7 +60,7 @@ namespace Ryujinx.HLE
AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(configuration.AudioDeviceDriver); AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(configuration.AudioDeviceDriver);
Memory = new MemoryBlock(configuration.MemoryConfiguration.ToDramSize(), MemoryAllocationFlags.Reserve); Memory = new MemoryBlock(configuration.MemoryConfiguration.ToDramSize(), MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable);
Gpu = new GpuContext(configuration.GpuRenderer); Gpu = new GpuContext(configuration.GpuRenderer);

View file

@ -14,7 +14,7 @@ namespace Ryujinx.Memory.Tests
{ {
} }
public void Map(ulong va, nuint hostAddress, ulong size) public void Map(ulong va, ulong pa, ulong size)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -59,9 +59,9 @@ namespace Ryujinx.Memory.Tests
throw new NotImplementedException(); throw new NotImplementedException();
} }
IEnumerable<HostMemoryRange> IVirtualMemoryManager.GetPhysicalRegions(ulong va, ulong size) IEnumerable<MemoryRange> IVirtualMemoryManager.GetPhysicalRegions(ulong va, ulong size)
{ {
return NoMappings ? new HostMemoryRange[0] : new HostMemoryRange[] { new HostMemoryRange((nuint)va, size) }; return NoMappings ? new MemoryRange[0] : new MemoryRange[] { new MemoryRange(va, size) };
} }
public bool IsMapped(ulong va) public bool IsMapped(ulong va)

View file

@ -202,7 +202,7 @@ namespace Ryujinx.Memory
return (int)(vaSpan / PageSize); return (int)(vaSpan / PageSize);
} }
private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException(); private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va); private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);

View file

@ -29,6 +29,12 @@ namespace Ryujinx.Memory
/// Enables mirroring of the memory block through aliasing of memory pages. /// Enables mirroring of the memory block through aliasing of memory pages.
/// When enabled, this allows creating more memory blocks sharing the same backing storage. /// When enabled, this allows creating more memory blocks sharing the same backing storage.
/// </summary> /// </summary>
Mirrorable = 1 << 2 Mirrorable = 1 << 2,
/// <summary>
/// Indicates that the memory block should support mapping views of a mirrorable memory block.
/// The block that is to have their views mapped should be created with the <see cref="Mirrorable"/> flag.
/// </summary>
ViewCompatible = 1 << 3
} }
} }

View file

@ -41,7 +41,7 @@ namespace Ryujinx.Memory
} }
else if (flags.HasFlag(MemoryAllocationFlags.Reserve)) else if (flags.HasFlag(MemoryAllocationFlags.Reserve))
{ {
_pointer = MemoryManagement.Reserve(size); _pointer = MemoryManagement.Reserve(size, flags.HasFlag(MemoryAllocationFlags.ViewCompatible));
} }
else else
{ {
@ -112,6 +112,36 @@ namespace Ryujinx.Memory
return MemoryManagement.Decommit(GetPointerInternal(offset, size), size); return MemoryManagement.Decommit(GetPointerInternal(offset, size), size);
} }
/// <summary>
/// Maps a view of memory from another memory block.
/// </summary>
/// <param name="srcBlock">Memory block from where the backing memory will be taken</param>
/// <param name="srcOffset">Offset on <paramref name="srcBlock"/> of the region that should be mapped</param>
/// <param name="dstOffset">Offset to map the view into on this block</param>
/// <param name="size">Size of the range to be mapped</param>
/// <exception cref="NotSupportedException">Throw when the source memory block does not support mirroring</exception>
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
/// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
public void MapView(MemoryBlock srcBlock, ulong srcOffset, ulong dstOffset, ulong size)
{
if (srcBlock._sharedMemory == IntPtr.Zero)
{
throw new ArgumentException("The source memory block is not mirrorable, and thus cannot be mapped on the current block.");
}
MemoryManagement.MapView(srcBlock._sharedMemory, srcOffset, GetPointerInternal(dstOffset, size), size);
}
/// <summary>
/// Unmaps a view of memory from another memory block.
/// </summary>
/// <param name="offset">Offset of the view previously mapped with <see cref="MapView"/></param>
/// <param name="size">Size of the range to be unmapped</param>
public void UnmapView(ulong offset, ulong size)
{
MemoryManagement.UnmapView(GetPointerInternal(offset, size), size);
}
/// <summary> /// <summary>
/// Reprotects a region of memory. /// Reprotects a region of memory.
/// </summary> /// </summary>

View file

@ -23,13 +23,13 @@ namespace Ryujinx.Memory
} }
} }
public static IntPtr Reserve(ulong size) public static IntPtr Reserve(ulong size, bool viewCompatible)
{ {
if (OperatingSystem.IsWindows()) if (OperatingSystem.IsWindows())
{ {
IntPtr sizeNint = new IntPtr((long)size); IntPtr sizeNint = new IntPtr((long)size);
return MemoryManagementWindows.Reserve(sizeNint); return MemoryManagementWindows.Reserve(sizeNint, viewCompatible);
} }
else if (OperatingSystem.IsLinux() || else if (OperatingSystem.IsLinux() ||
OperatingSystem.IsMacOS()) OperatingSystem.IsMacOS())
@ -80,6 +80,34 @@ namespace Ryujinx.Memory
} }
} }
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr address, ulong size)
{
if (OperatingSystem.IsWindows())
{
IntPtr sizeNint = new IntPtr((long)size);
MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, sizeNint);
}
else
{
throw new PlatformNotSupportedException();
}
}
public static void UnmapView(IntPtr address, ulong size)
{
if (OperatingSystem.IsWindows())
{
IntPtr sizeNint = new IntPtr((long)size);
MemoryManagementWindows.UnmapView(address, sizeNint);
}
else
{
throw new PlatformNotSupportedException();
}
}
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool throwOnFail) public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool throwOnFail)
{ {
bool result; bool result;

View file

@ -9,7 +9,10 @@ namespace Ryujinx.Memory
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
static class MemoryManagementWindows static class MemoryManagementWindows
{ {
private const int PageSize = 0x1000;
private static readonly IntPtr InvalidHandleValue = new IntPtr(-1); private static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
private static readonly IntPtr CurrentProcessHandle = new IntPtr(-1);
private static bool UseWin10Placeholders; private static bool UseWin10Placeholders;
private static object _emulatedHandleLock = new object(); private static object _emulatedHandleLock = new object();
@ -23,6 +26,16 @@ namespace Ryujinx.Memory
AllocationType flAllocationType, AllocationType flAllocationType,
MemoryProtection flProtect); MemoryProtection flProtect);
[DllImport("KernelBase.dll", SetLastError = true)]
private static extern IntPtr VirtualAlloc2(
IntPtr process,
IntPtr lpAddress,
IntPtr dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect,
IntPtr extendedParameters,
ulong parameterCount);
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
private static extern bool VirtualProtect( private static extern bool VirtualProtect(
IntPtr lpAddress, IntPtr lpAddress,
@ -53,10 +66,25 @@ namespace Ryujinx.Memory
uint dwFileOffsetLow, uint dwFileOffsetLow,
IntPtr dwNumberOfBytesToMap); IntPtr dwNumberOfBytesToMap);
[DllImport("KernelBase.dll", SetLastError = true)]
private static extern IntPtr MapViewOfFile3(
IntPtr hFileMappingObject,
IntPtr process,
IntPtr baseAddress,
ulong offset,
IntPtr dwNumberOfBytesToMap,
ulong allocationType,
MemoryProtection dwDesiredAccess,
IntPtr extendedParameters,
ulong parameterCount);
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
private static extern bool UnmapViewOfFile(IntPtr lpBaseAddress); private static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("KernelBase.dll", SetLastError = true)]
private static extern bool UnmapViewOfFile2(IntPtr process, IntPtr lpBaseAddress, ulong unmapFlags);
[DllImport("kernel32.dll")]
private static extern uint GetLastError(); private static extern uint GetLastError();
static MemoryManagementWindows() static MemoryManagementWindows()
@ -69,8 +97,13 @@ namespace Ryujinx.Memory
return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit); return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit);
} }
public static IntPtr Reserve(IntPtr size) public static IntPtr Reserve(IntPtr size, bool viewCompatible)
{ {
if (viewCompatible)
{
return AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);
}
return AllocateInternal(size, AllocationType.Reserve); return AllocateInternal(size, AllocationType.Reserve);
} }
@ -86,44 +119,73 @@ namespace Ryujinx.Memory
return ptr; return ptr;
} }
public static bool Commit(IntPtr location, IntPtr size) private static IntPtr AllocateInternal2(IntPtr size, AllocationType flags = 0)
{ {
if (UseWin10Placeholders) IntPtr ptr = VirtualAlloc2(CurrentProcessHandle, IntPtr.Zero, size, flags, MemoryProtection.NoAccess, IntPtr.Zero, 0);
if (ptr == IntPtr.Zero)
{ {
lock (_emulatedSharedList) throw new OutOfMemoryException();
{
foreach (var shared in _emulatedSharedList)
{
if (shared.CommitMap(location, size))
{
return true;
}
}
}
} }
return ptr;
}
public static bool Commit(IntPtr location, IntPtr size)
{
return VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero; return VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero;
} }
public static bool Decommit(IntPtr location, IntPtr size) public static bool Decommit(IntPtr location, IntPtr size)
{ {
if (UseWin10Placeholders)
{
lock (_emulatedSharedList)
{
foreach (var shared in _emulatedSharedList)
{
if (shared.DecommitMap(location, size))
{
return true;
}
}
}
}
return VirtualFree(location, size, AllocationType.Decommit); return VirtualFree(location, size, AllocationType.Decommit);
} }
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
{
IntPtr endLocation = (nint)location + (nint)size;
while (location != endLocation)
{
VirtualFree(location, (IntPtr)PageSize, AllocationType.Release | AllocationType.PreservePlaceholder);
var ptr = MapViewOfFile3(
sharedMemory,
CurrentProcessHandle,
location,
srcOffset,
(IntPtr)PageSize,
0x4000,
MemoryProtection.ReadWrite,
IntPtr.Zero,
0);
if (ptr == IntPtr.Zero)
{
throw new Exception($"MapViewOfFile3 failed with error code 0x{GetLastError():X}.");
}
location += PageSize;
srcOffset += PageSize;
}
}
public static void UnmapView(IntPtr location, IntPtr size)
{
IntPtr endLocation = (nint)location + (int)size;
while (location != endLocation)
{
bool result = UnmapViewOfFile2(CurrentProcessHandle, location, 2);
if (!result)
{
throw new Exception($"UnmapViewOfFile2 failed with error code 0x{GetLastError():X}.");
}
location += PageSize;
}
}
public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission) public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission)
{ {
if (UseWin10Placeholders) if (UseWin10Placeholders)
@ -194,57 +256,26 @@ namespace Ryujinx.Memory
public static IntPtr CreateSharedMemory(IntPtr size, bool reserve) public static IntPtr CreateSharedMemory(IntPtr size, bool reserve)
{ {
if (UseWin10Placeholders && reserve) var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
IntPtr handle = CreateFileMapping(
InvalidHandleValue,
IntPtr.Zero,
FileMapProtection.PageReadWrite | prot,
(uint)(size.ToInt64() >> 32),
(uint)size.ToInt64(),
null);
if (handle == IntPtr.Zero)
{ {
lock (_emulatedHandleLock) throw new OutOfMemoryException();
{
int handle = GetEmulatedHandle();
_emulatedShared[handle - 1] = new EmulatedSharedMemoryWindows((ulong)size);
_emulatedSharedList.Add(_emulatedShared[handle - 1]);
return (IntPtr)handle;
}
} }
else
{
var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
IntPtr handle = CreateFileMapping( return handle;
InvalidHandleValue,
IntPtr.Zero,
FileMapProtection.PageReadWrite | prot,
(uint)(size.ToInt64() >> 32),
(uint)size.ToInt64(),
null);
if (handle == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return handle;
}
} }
public static void DestroySharedMemory(IntPtr handle) public static void DestroySharedMemory(IntPtr handle)
{ {
if (UseWin10Placeholders)
{
lock (_emulatedHandleLock)
{
int iHandle = (int)(ulong)handle;
if (EmulatedHandleValid(ref iHandle))
{
_emulatedSharedList.Remove(_emulatedShared[iHandle]);
_emulatedShared[iHandle].Dispose();
_emulatedShared[iHandle] = null;
return;
}
}
}
if (!CloseHandle(handle)) if (!CloseHandle(handle))
{ {
throw new ArgumentException("Invalid handle.", nameof(handle)); throw new ArgumentException("Invalid handle.", nameof(handle));
@ -253,19 +284,6 @@ namespace Ryujinx.Memory
public static IntPtr MapSharedMemory(IntPtr handle) public static IntPtr MapSharedMemory(IntPtr handle)
{ {
if (UseWin10Placeholders)
{
lock (_emulatedHandleLock)
{
int iHandle = (int)(ulong)handle;
if (EmulatedHandleValid(ref iHandle))
{
return _emulatedShared[iHandle].Map();
}
}
}
IntPtr ptr = MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero); IntPtr ptr = MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);
if (ptr == IntPtr.Zero) if (ptr == IntPtr.Zero)
@ -278,20 +296,6 @@ namespace Ryujinx.Memory
public static void UnmapSharedMemory(IntPtr address) public static void UnmapSharedMemory(IntPtr address)
{ {
if (UseWin10Placeholders)
{
lock (_emulatedHandleLock)
{
foreach (EmulatedSharedMemoryWindows shared in _emulatedSharedList)
{
if (shared.Unmap((ulong)address))
{
return;
}
}
}
}
if (!UnmapViewOfFile(address)) if (!UnmapViewOfFile(address))
{ {
throw new ArgumentException("Invalid address.", nameof(address)); throw new ArgumentException("Invalid address.", nameof(address));

View file

@ -1,6 +1,6 @@
namespace Ryujinx.Memory namespace Ryujinx.Memory
{ {
class PageTable<T> where T : unmanaged public class PageTable<T> where T : unmanaged
{ {
public const int PageBits = 12; public const int PageBits = 12;
public const int PageSize = 1 << PageBits; public const int PageSize = 1 << PageBits;

View file

@ -1,71 +0,0 @@
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);
}
}
}

View file

@ -54,9 +54,9 @@ namespace Ryujinx.Tests.Cpu
_currAddress = CodeBaseAddress; _currAddress = CodeBaseAddress;
_ram = new MemoryBlock(Size * 2); _ram = new MemoryBlock(Size * 2);
_memory = new MemoryManager(1ul << 16); _memory = new MemoryManager(_ram, 1ul << 16);
_memory.IncrementReferenceCount(); _memory.IncrementReferenceCount();
_memory.Map(CodeBaseAddress, _ram.GetPointer(0, Size * 2), Size * 2); _memory.Map(CodeBaseAddress, 0, Size * 2);
_context = CpuContext.CreateExecutionContext(); _context = CpuContext.CreateExecutionContext();
Translator.IsReadyForTranslation.Set(); Translator.IsReadyForTranslation.Set();

View file

@ -49,9 +49,9 @@ namespace Ryujinx.Tests.Cpu
_currAddress = CodeBaseAddress; _currAddress = CodeBaseAddress;
_ram = new MemoryBlock(Size * 2); _ram = new MemoryBlock(Size * 2);
_memory = new MemoryManager(1ul << 16); _memory = new MemoryManager(_ram, 1ul << 16);
_memory.IncrementReferenceCount(); _memory.IncrementReferenceCount();
_memory.Map(CodeBaseAddress, _ram.GetPointer(0, Size * 2), Size * 2); _memory.Map(CodeBaseAddress, 0, Size * 2);
_context = CpuContext.CreateExecutionContext(); _context = CpuContext.CreateExecutionContext();
_context.IsAarch32 = true; _context.IsAarch32 = true;