mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-11-15 02:36:40 +00:00
08831eecf7
* IPC refactor part 3 + 4: New server HIPC message processor with source generator based serialization * Make types match on calls to AlignUp/AlignDown * Formatting * Address some PR feedback * Move BitfieldExtensions to Ryujinx.Common.Utilities and consolidate implementations * Rename Reader/Writer to SpanReader/SpanWriter and move to Ryujinx.Common.Memory * Implement EventType * Address more PR feedback * Log request processing errors since they are not normal * Rename waitable to multiwait and add missing lock * PR feedback * Ac_K PR feedback
191 lines
5.9 KiB
C#
191 lines
5.9 KiB
C#
using Ryujinx.Horizon.Common;
|
|
using Ryujinx.Memory;
|
|
using System;
|
|
using System.Diagnostics;
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|
{
|
|
class KPageTable : KPageTableBase
|
|
{
|
|
private readonly IVirtualMemoryManager _cpuMemory;
|
|
|
|
public KPageTable(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context)
|
|
{
|
|
_cpuMemory = cpuMemory;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override void GetPhysicalRegions(ulong va, ulong size, KPageList pageList)
|
|
{
|
|
var ranges = _cpuMemory.GetPhysicalRegions(va, size);
|
|
foreach (var range in ranges)
|
|
{
|
|
pageList.AddRange(range.Address + DramMemoryMap.DramBase, range.Size / PageSize);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override ReadOnlySpan<byte> GetSpan(ulong va, int size)
|
|
{
|
|
return _cpuMemory.GetSpan(va, size);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result MapMemory(ulong src, ulong dst, ulong pagesCount, KMemoryPermission oldSrcPermission, KMemoryPermission newDstPermission)
|
|
{
|
|
KPageList pageList = new KPageList();
|
|
GetPhysicalRegions(src, pagesCount * PageSize, pageList);
|
|
|
|
Result result = Reprotect(src, pagesCount, KMemoryPermission.None);
|
|
|
|
if (result != Result.Success)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = MapPages(dst, pageList, newDstPermission, false, 0);
|
|
|
|
if (result != Result.Success)
|
|
{
|
|
Result reprotectResult = Reprotect(src, pagesCount, oldSrcPermission);
|
|
Debug.Assert(reprotectResult == Result.Success);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result UnmapMemory(ulong dst, ulong src, ulong pagesCount, KMemoryPermission oldDstPermission, KMemoryPermission newSrcPermission)
|
|
{
|
|
ulong size = pagesCount * PageSize;
|
|
|
|
KPageList srcPageList = new KPageList();
|
|
KPageList dstPageList = new KPageList();
|
|
|
|
GetPhysicalRegions(src, size, srcPageList);
|
|
GetPhysicalRegions(dst, size, dstPageList);
|
|
|
|
if (!dstPageList.IsEqual(srcPageList))
|
|
{
|
|
return KernelResult.InvalidMemRange;
|
|
}
|
|
|
|
Result result = Unmap(dst, pagesCount);
|
|
|
|
if (result != Result.Success)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = Reprotect(src, pagesCount, newSrcPermission);
|
|
|
|
if (result != Result.Success)
|
|
{
|
|
Result mapResult = MapPages(dst, dstPageList, oldDstPermission, false, 0);
|
|
Debug.Assert(mapResult == Result.Success);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result MapPages(ulong dstVa, ulong pagesCount, ulong srcPa, KMemoryPermission permission, bool shouldFillPages, byte fillValue)
|
|
{
|
|
ulong size = pagesCount * PageSize;
|
|
|
|
Context.Memory.Commit(srcPa - DramMemoryMap.DramBase, size);
|
|
|
|
_cpuMemory.Map(dstVa, srcPa - DramMemoryMap.DramBase, size);
|
|
|
|
if (DramMemoryMap.IsHeapPhysicalAddress(srcPa))
|
|
{
|
|
Context.MemoryManager.IncrementPagesReferenceCount(srcPa, pagesCount);
|
|
}
|
|
|
|
if (shouldFillPages)
|
|
{
|
|
_cpuMemory.Fill(dstVa, size, fillValue);
|
|
}
|
|
|
|
return Result.Success;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result MapPages(ulong address, KPageList pageList, KMemoryPermission permission, bool shouldFillPages, byte fillValue)
|
|
{
|
|
using var scopedPageList = new KScopedPageList(Context.MemoryManager, pageList);
|
|
|
|
ulong currentVa = address;
|
|
|
|
foreach (var pageNode in pageList)
|
|
{
|
|
ulong addr = pageNode.Address - DramMemoryMap.DramBase;
|
|
ulong size = pageNode.PagesCount * PageSize;
|
|
|
|
Context.Memory.Commit(addr, size);
|
|
|
|
_cpuMemory.Map(currentVa, addr, size);
|
|
|
|
if (shouldFillPages)
|
|
{
|
|
_cpuMemory.Fill(currentVa, size, fillValue);
|
|
}
|
|
|
|
currentVa += size;
|
|
}
|
|
|
|
scopedPageList.SignalSuccess();
|
|
|
|
return Result.Success;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result Unmap(ulong address, ulong pagesCount)
|
|
{
|
|
KPageList pagesToClose = new KPageList();
|
|
|
|
var regions = _cpuMemory.GetPhysicalRegions(address, pagesCount * PageSize);
|
|
|
|
foreach (var region in regions)
|
|
{
|
|
ulong pa = region.Address + DramMemoryMap.DramBase;
|
|
if (DramMemoryMap.IsHeapPhysicalAddress(pa))
|
|
{
|
|
pagesToClose.AddRange(pa, region.Size / PageSize);
|
|
}
|
|
}
|
|
|
|
_cpuMemory.Unmap(address, pagesCount * PageSize);
|
|
|
|
pagesToClose.DecrementPagesReferenceCount(Context.MemoryManager);
|
|
|
|
return Result.Success;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result Reprotect(ulong address, ulong pagesCount, KMemoryPermission permission)
|
|
{
|
|
// TODO.
|
|
return Result.Success;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override Result ReprotectWithAttributes(ulong address, ulong pagesCount, KMemoryPermission permission)
|
|
{
|
|
// TODO.
|
|
return Result.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);
|
|
}
|
|
}
|
|
}
|