POWER - Performance Optimizations With Extensive Ramifications (#2286)
* 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>
This commit is contained in:
parent
fb65f392d1
commit
54ea2285f0
107 changed files with 8309 additions and 4183 deletions
|
@ -47,7 +47,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return KernelResult.OutOfMemory;
|
||||
}
|
||||
|
||||
public KernelResult CopyBuffersToClient(KMemoryManager memoryManager)
|
||||
public KernelResult CopyBuffersToClient(KPageTableBase memoryManager)
|
||||
{
|
||||
KernelResult result = CopyToClient(memoryManager, _receiveBufferDescriptors);
|
||||
|
||||
|
@ -59,7 +59,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return CopyToClient(memoryManager, _exchangeBufferDescriptors);
|
||||
}
|
||||
|
||||
private KernelResult CopyToClient(KMemoryManager memoryManager, List<KBufferDescriptor> list)
|
||||
private KernelResult CopyToClient(KPageTableBase memoryManager, List<KBufferDescriptor> list)
|
||||
{
|
||||
foreach (KBufferDescriptor desc in list)
|
||||
{
|
||||
|
@ -81,8 +81,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
attributeMask |= MemoryAttribute.DeviceMapped;
|
||||
}
|
||||
|
||||
ulong clientAddrTruncated = BitUtils.AlignDown(desc.ClientAddress, KMemoryManager.PageSize);
|
||||
ulong clientAddrRounded = BitUtils.AlignUp (desc.ClientAddress, KMemoryManager.PageSize);
|
||||
ulong clientAddrTruncated = BitUtils.AlignDown(desc.ClientAddress, KPageTableBase.PageSize);
|
||||
ulong clientAddrRounded = BitUtils.AlignUp (desc.ClientAddress, KPageTableBase.PageSize);
|
||||
|
||||
// Check if address is not aligned, in this case we need to perform 2 copies.
|
||||
if (clientAddrTruncated != clientAddrRounded)
|
||||
|
@ -113,9 +113,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
ulong clientEndAddr = desc.ClientAddress + desc.Size;
|
||||
ulong serverEndAddr = desc.ServerAddress + desc.Size;
|
||||
|
||||
ulong clientEndAddrTruncated = BitUtils.AlignDown(clientEndAddr, KMemoryManager.PageSize);
|
||||
ulong clientEndAddrRounded = BitUtils.AlignUp (clientEndAddr, KMemoryManager.PageSize);
|
||||
ulong serverEndAddrTruncated = BitUtils.AlignDown(serverEndAddr, KMemoryManager.PageSize);
|
||||
ulong clientEndAddrTruncated = BitUtils.AlignDown(clientEndAddr, KPageTableBase.PageSize);
|
||||
ulong clientEndAddrRounded = BitUtils.AlignUp (clientEndAddr, KPageTableBase.PageSize);
|
||||
ulong serverEndAddrTruncated = BitUtils.AlignDown(serverEndAddr, KPageTableBase.PageSize);
|
||||
|
||||
if (clientEndAddrTruncated < clientEndAddrRounded &&
|
||||
(clientAddrTruncated == clientAddrRounded || clientAddrTruncated < clientEndAddrTruncated))
|
||||
|
@ -140,7 +140,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult UnmapServerBuffers(KMemoryManager memoryManager)
|
||||
public KernelResult UnmapServerBuffers(KPageTableBase memoryManager)
|
||||
{
|
||||
KernelResult result = UnmapServer(memoryManager, _sendBufferDescriptors);
|
||||
|
||||
|
@ -159,7 +159,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return UnmapServer(memoryManager, _exchangeBufferDescriptors);
|
||||
}
|
||||
|
||||
private KernelResult UnmapServer(KMemoryManager memoryManager, List<KBufferDescriptor> list)
|
||||
private KernelResult UnmapServer(KPageTableBase memoryManager, List<KBufferDescriptor> list)
|
||||
{
|
||||
foreach (KBufferDescriptor descriptor in list)
|
||||
{
|
||||
|
@ -177,7 +177,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult RestoreClientBuffers(KMemoryManager memoryManager)
|
||||
public KernelResult RestoreClientBuffers(KPageTableBase memoryManager)
|
||||
{
|
||||
KernelResult result = RestoreClient(memoryManager, _sendBufferDescriptors);
|
||||
|
||||
|
@ -196,7 +196,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return RestoreClient(memoryManager, _exchangeBufferDescriptors);
|
||||
}
|
||||
|
||||
private KernelResult RestoreClient(KMemoryManager memoryManager, List<KBufferDescriptor> list)
|
||||
private KernelResult RestoreClient(KPageTableBase memoryManager, List<KBufferDescriptor> list)
|
||||
{
|
||||
foreach (KBufferDescriptor descriptor in list)
|
||||
{
|
||||
|
|
|
@ -19,10 +19,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
private struct Message
|
||||
{
|
||||
public ulong Address { get; }
|
||||
public ulong DramAddress { get; }
|
||||
public ulong Size { get; }
|
||||
public bool IsCustom { get; }
|
||||
public ulong Address { get; }
|
||||
public ulong Size { get; }
|
||||
public bool IsCustom { get; }
|
||||
|
||||
public Message(KThread thread, ulong customCmdBuffAddress, ulong customCmdBuffSize)
|
||||
{
|
||||
|
@ -32,16 +31,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
{
|
||||
Address = customCmdBuffAddress;
|
||||
Size = customCmdBuffSize;
|
||||
|
||||
KProcess process = thread.Owner;
|
||||
|
||||
DramAddress = process.MemoryManager.GetDramAddressFromVa(Address);
|
||||
}
|
||||
else
|
||||
{
|
||||
Address = thread.TlsAddress;
|
||||
DramAddress = thread.TlsDramAddress;
|
||||
Size = 0x100;
|
||||
Address = thread.TlsAddress;
|
||||
Size = 0x100;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -252,7 +246,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
Message clientMsg = new Message(request);
|
||||
Message serverMsg = new Message(serverThread, customCmdBuffAddr, customCmdBuffSize);
|
||||
|
||||
MessageHeader clientHeader = GetClientMessageHeader(clientMsg);
|
||||
MessageHeader clientHeader = GetClientMessageHeader(clientProcess, clientMsg);
|
||||
MessageHeader serverHeader = GetServerMessageHeader(serverMsg);
|
||||
|
||||
KernelResult serverResult = KernelResult.NotFound;
|
||||
|
@ -318,6 +312,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
|
||||
ulong[] receiveList = GetReceiveList(
|
||||
serverProcess,
|
||||
serverMsg,
|
||||
serverHeader.ReceiveListType,
|
||||
serverHeader.ReceiveListOffset);
|
||||
|
@ -351,7 +346,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
for (int index = 0; index < clientHeader.CopyHandlesCount; index++)
|
||||
{
|
||||
int newHandle = 0;
|
||||
int handle = KernelContext.Memory.Read<int>(clientMsg.DramAddress + offset * 4);
|
||||
int handle = clientProcess.CpuMemory.Read<int>(clientMsg.Address + offset * 4);
|
||||
|
||||
if (clientResult == KernelResult.Success && handle != 0)
|
||||
{
|
||||
|
@ -366,7 +361,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
for (int index = 0; index < clientHeader.MoveHandlesCount; index++)
|
||||
{
|
||||
int newHandle = 0;
|
||||
int handle = KernelContext.Memory.Read<int>(clientMsg.DramAddress + offset * 4);
|
||||
int handle = clientProcess.CpuMemory.Read<int>(clientMsg.Address + offset * 4);
|
||||
|
||||
if (handle != 0)
|
||||
{
|
||||
|
@ -402,7 +397,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
for (int index = 0; index < clientHeader.PointerBuffersCount; index++)
|
||||
{
|
||||
ulong pointerDesc = KernelContext.Memory.Read<ulong>(clientMsg.DramAddress + offset * 4);
|
||||
ulong pointerDesc = clientProcess.CpuMemory.Read<ulong>(clientMsg.Address + offset * 4);
|
||||
|
||||
PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc);
|
||||
|
||||
|
@ -461,11 +456,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
for (int index = 0; index < totalBuffersCount; index++)
|
||||
{
|
||||
ulong clientDescAddress = clientMsg.DramAddress + offset * 4;
|
||||
ulong clientDescAddress = clientMsg.Address + offset * 4;
|
||||
|
||||
uint descWord0 = KernelContext.Memory.Read<uint>(clientDescAddress + 0);
|
||||
uint descWord1 = KernelContext.Memory.Read<uint>(clientDescAddress + 4);
|
||||
uint descWord2 = KernelContext.Memory.Read<uint>(clientDescAddress + 8);
|
||||
uint descWord0 = clientProcess.CpuMemory.Read<uint>(clientDescAddress + 0);
|
||||
uint descWord1 = clientProcess.CpuMemory.Read<uint>(clientDescAddress + 4);
|
||||
uint descWord2 = clientProcess.CpuMemory.Read<uint>(clientDescAddress + 8);
|
||||
|
||||
bool isSendDesc = index < clientHeader.SendBuffersCount;
|
||||
bool isExchangeDesc = index >= clientHeader.SendBuffersCount + clientHeader.ReceiveBuffersCount;
|
||||
|
@ -575,10 +570,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
else
|
||||
{
|
||||
copySrc = clientProcess.MemoryManager.GetDramAddressFromVa(copySrc);
|
||||
copyDst = serverProcess.MemoryManager.GetDramAddressFromVa(copyDst);
|
||||
|
||||
KernelContext.Memory.Copy(copyDst, copySrc, copySize);
|
||||
serverProcess.CpuMemory.Write(copyDst, clientProcess.CpuMemory.GetSpan(copySrc, (int)copySize));
|
||||
}
|
||||
|
||||
if (clientResult != KernelResult.Success)
|
||||
|
@ -623,7 +615,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
Message clientMsg = new Message(request);
|
||||
Message serverMsg = new Message(serverThread, customCmdBuffAddr, customCmdBuffSize);
|
||||
|
||||
MessageHeader clientHeader = GetClientMessageHeader(clientMsg);
|
||||
MessageHeader clientHeader = GetClientMessageHeader(clientProcess, clientMsg);
|
||||
MessageHeader serverHeader = GetServerMessageHeader(serverMsg);
|
||||
|
||||
KernelResult clientResult = KernelResult.Success;
|
||||
|
@ -683,6 +675,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
// Read receive list.
|
||||
ulong[] receiveList = GetReceiveList(
|
||||
clientProcess,
|
||||
clientMsg,
|
||||
clientHeader.ReceiveListType,
|
||||
clientHeader.ReceiveListOffset);
|
||||
|
@ -698,8 +691,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
|
||||
// Copy header.
|
||||
KernelContext.Memory.Write(clientMsg.DramAddress + 0, serverHeader.Word0);
|
||||
KernelContext.Memory.Write(clientMsg.DramAddress + 4, serverHeader.Word1);
|
||||
clientProcess.CpuMemory.Write(clientMsg.Address + 0, serverHeader.Word0);
|
||||
clientProcess.CpuMemory.Write(clientMsg.Address + 4, serverHeader.Word1);
|
||||
|
||||
// Copy handles.
|
||||
uint offset;
|
||||
|
@ -708,11 +701,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
{
|
||||
offset = 3;
|
||||
|
||||
KernelContext.Memory.Write(clientMsg.DramAddress + 8, serverHeader.Word2);
|
||||
clientProcess.CpuMemory.Write(clientMsg.Address + 8, serverHeader.Word2);
|
||||
|
||||
if (serverHeader.HasPid)
|
||||
{
|
||||
KernelContext.Memory.Write(clientMsg.DramAddress + offset * 4, serverProcess.Pid);
|
||||
clientProcess.CpuMemory.Write(clientMsg.Address + offset * 4, serverProcess.Pid);
|
||||
|
||||
offset += 2;
|
||||
}
|
||||
|
@ -728,7 +721,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
GetCopyObjectHandle(serverThread, clientProcess, handle, out newHandle);
|
||||
}
|
||||
|
||||
KernelContext.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle);
|
||||
clientProcess.CpuMemory.Write(clientMsg.Address + offset * 4, newHandle);
|
||||
|
||||
offset++;
|
||||
}
|
||||
|
@ -751,7 +744,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
}
|
||||
|
||||
KernelContext.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle);
|
||||
clientProcess.CpuMemory.Write(clientMsg.Address + offset * 4, newHandle);
|
||||
|
||||
offset++;
|
||||
}
|
||||
|
@ -808,7 +801,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
}
|
||||
|
||||
ulong dstDescAddress = clientMsg.DramAddress + offset * 4;
|
||||
ulong dstDescAddress = clientMsg.Address + offset * 4;
|
||||
|
||||
ulong clientPointerDesc =
|
||||
(recvListBufferAddress << 32) |
|
||||
|
@ -817,7 +810,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
clientPointerDesc |= pointerDesc & 0xffff000f;
|
||||
|
||||
KernelContext.Memory.Write(dstDescAddress + 0, clientPointerDesc);
|
||||
clientProcess.CpuMemory.Write(dstDescAddress + 0, clientPointerDesc);
|
||||
|
||||
offset += 2;
|
||||
}
|
||||
|
@ -830,11 +823,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
for (int index = 0; index < totalBuffersCount; index++)
|
||||
{
|
||||
ulong dstDescAddress = clientMsg.DramAddress + offset * 4;
|
||||
ulong dstDescAddress = clientMsg.Address + offset * 4;
|
||||
|
||||
KernelContext.Memory.Write(dstDescAddress + 0, 0);
|
||||
KernelContext.Memory.Write(dstDescAddress + 4, 0);
|
||||
KernelContext.Memory.Write(dstDescAddress + 8, 0);
|
||||
clientProcess.CpuMemory.Write(dstDescAddress + 0, 0);
|
||||
clientProcess.CpuMemory.Write(dstDescAddress + 4, 0);
|
||||
clientProcess.CpuMemory.Write(dstDescAddress + 8, 0);
|
||||
|
||||
offset += 3;
|
||||
}
|
||||
|
@ -865,10 +858,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
else
|
||||
{
|
||||
copyDst = clientProcess.MemoryManager.GetDramAddressFromVa(copyDst);
|
||||
copySrc = serverProcess.MemoryManager.GetDramAddressFromVa(copySrc);
|
||||
|
||||
KernelContext.Memory.Copy(copyDst, copySrc, copySize);
|
||||
clientProcess.CpuMemory.Write(copyDst, serverProcess.CpuMemory.GetSpan(copySrc, (int)copySize));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -878,11 +868,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
return serverResult;
|
||||
}
|
||||
|
||||
private MessageHeader GetClientMessageHeader(Message clientMsg)
|
||||
private MessageHeader GetClientMessageHeader(KProcess clientProcess, Message clientMsg)
|
||||
{
|
||||
uint word0 = KernelContext.Memory.Read<uint>(clientMsg.DramAddress + 0);
|
||||
uint word1 = KernelContext.Memory.Read<uint>(clientMsg.DramAddress + 4);
|
||||
uint word2 = KernelContext.Memory.Read<uint>(clientMsg.DramAddress + 8);
|
||||
uint word0 = clientProcess.CpuMemory.Read<uint>(clientMsg.Address + 0);
|
||||
uint word1 = clientProcess.CpuMemory.Read<uint>(clientMsg.Address + 4);
|
||||
uint word2 = clientProcess.CpuMemory.Read<uint>(clientMsg.Address + 8);
|
||||
|
||||
return new MessageHeader(word0, word1, word2);
|
||||
}
|
||||
|
@ -949,7 +939,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
}
|
||||
}
|
||||
|
||||
private ulong[] GetReceiveList(Message message, uint recvListType, uint recvListOffset)
|
||||
private ulong[] GetReceiveList(KProcess ownerProcess, Message message, uint recvListType, uint recvListOffset)
|
||||
{
|
||||
int recvListSize = 0;
|
||||
|
||||
|
@ -964,11 +954,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
ulong[] receiveList = new ulong[recvListSize];
|
||||
|
||||
ulong recvListAddress = message.DramAddress + recvListOffset;
|
||||
ulong recvListAddress = message.Address + recvListOffset;
|
||||
|
||||
for (int index = 0; index < recvListSize; index++)
|
||||
{
|
||||
receiveList[index] = KernelContext.Memory.Read<ulong>(recvListAddress + (ulong)index * 8);
|
||||
receiveList[index] = ownerProcess.CpuMemory.Read<ulong>(recvListAddress + (ulong)index * 8);
|
||||
}
|
||||
|
||||
return receiveList;
|
||||
|
@ -1219,10 +1209,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
ulong address = clientProcess.MemoryManager.GetDramAddressFromVa(request.CustomCmdBuffAddr);
|
||||
ulong address = request.CustomCmdBuffAddr;
|
||||
|
||||
KernelContext.Memory.Write<ulong>(address, 0);
|
||||
KernelContext.Memory.Write(address + 8, (int)result);
|
||||
clientProcess.CpuMemory.Write<ulong>(address, 0);
|
||||
clientProcess.CpuMemory.Write(address + 8, (int)result);
|
||||
}
|
||||
|
||||
clientProcess.MemoryManager.UnborrowIpcBuffer(request.CustomCmdBuffAddr, request.CustomCmdBuffSize);
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Ryujinx.HLE.HOS.Kernel
|
|||
public const int MemoryBlockAllocatorSize = 0x2710;
|
||||
|
||||
public const ulong UserSlabHeapBase = DramMemoryMap.SlabHeapBase;
|
||||
public const ulong UserSlabHeapItemSize = KMemoryManager.PageSize;
|
||||
public const ulong UserSlabHeapItemSize = KPageTableBase.PageSize;
|
||||
public const ulong UserSlabHeapSize = 0x3de000;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,10 +28,10 @@ namespace Ryujinx.HLE.HOS.Kernel
|
|||
|
||||
public KResourceLimit ResourceLimit { get; }
|
||||
|
||||
public KMemoryRegionManager[] MemoryRegions { get; }
|
||||
public KMemoryManager MemoryManager { get; }
|
||||
|
||||
public KMemoryBlockAllocator LargeMemoryBlockAllocator { get; }
|
||||
public KMemoryBlockAllocator SmallMemoryBlockAllocator { get; }
|
||||
public KMemoryBlockSlabManager LargeMemoryBlockSlabManager { get; }
|
||||
public KMemoryBlockSlabManager SmallMemoryBlockSlabManager { get; }
|
||||
|
||||
public KSlabHeap UserSlabHeapPages { get; }
|
||||
|
||||
|
@ -70,16 +70,18 @@ namespace Ryujinx.HLE.HOS.Kernel
|
|||
|
||||
KernelInit.InitializeResourceLimit(ResourceLimit, memorySize);
|
||||
|
||||
MemoryRegions = KernelInit.GetMemoryRegions(memorySize, memoryArrange);
|
||||
MemoryManager = new KMemoryManager(memorySize, memoryArrange);
|
||||
|
||||
LargeMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize * 2);
|
||||
SmallMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize);
|
||||
LargeMemoryBlockSlabManager = new KMemoryBlockSlabManager(KernelConstants.MemoryBlockAllocatorSize * 2);
|
||||
SmallMemoryBlockSlabManager = new KMemoryBlockSlabManager(KernelConstants.MemoryBlockAllocatorSize);
|
||||
|
||||
UserSlabHeapPages = new KSlabHeap(
|
||||
KernelConstants.UserSlabHeapBase,
|
||||
KernelConstants.UserSlabHeapItemSize,
|
||||
KernelConstants.UserSlabHeapSize);
|
||||
|
||||
memory.Commit(KernelConstants.UserSlabHeapBase - DramMemoryMap.DramBase, KernelConstants.UserSlabHeapSize);
|
||||
|
||||
CriticalSection = new KCriticalSection(this);
|
||||
Schedulers = new KScheduler[KScheduler.CpuCoresCount];
|
||||
PriorityQueue = new KPriorityQueue();
|
||||
|
|
|
@ -9,5 +9,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
public const ulong SlabHeapBase = KernelReserveBase + 0x85000;
|
||||
public const ulong SlapHeapSize = 0xa21000;
|
||||
public const ulong SlabHeapEnd = SlabHeapBase + SlapHeapSize;
|
||||
|
||||
public static bool IsHeapPhysicalAddress(ulong address)
|
||||
{
|
||||
return address >= SlabHeapEnd;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -84,7 +84,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
ulong leftAddress = BaseAddress;
|
||||
|
||||
ulong leftPagesCount = (address - leftAddress) / KMemoryManager.PageSize;
|
||||
ulong leftPagesCount = (address - leftAddress) / KPageTableBase.PageSize;
|
||||
|
||||
BaseAddress = address;
|
||||
|
||||
|
@ -107,7 +107,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
public KMemoryInfo GetInfo()
|
||||
{
|
||||
ulong size = PagesCount * KMemoryManager.PageSize;
|
||||
ulong size = PagesCount * KPageTableBase.PageSize;
|
||||
|
||||
return new KMemoryInfo(
|
||||
BaseAddress,
|
||||
|
|
329
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockManager.cs
Normal file
329
Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockManager.cs
Normal file
|
@ -0,0 +1,329 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryBlockManager
|
||||
{
|
||||
private const int PageSize = KPageTableBase.PageSize;
|
||||
|
||||
private readonly LinkedList<KMemoryBlock> _blocks;
|
||||
|
||||
public int BlocksCount => _blocks.Count;
|
||||
|
||||
private KMemoryBlockSlabManager _slabManager;
|
||||
|
||||
private ulong _addrSpaceEnd;
|
||||
|
||||
public KMemoryBlockManager()
|
||||
{
|
||||
_blocks = new LinkedList<KMemoryBlock>();
|
||||
}
|
||||
|
||||
public KernelResult Initialize(ulong addrSpaceStart, ulong addrSpaceEnd, KMemoryBlockSlabManager slabManager)
|
||||
{
|
||||
_slabManager = slabManager;
|
||||
_addrSpaceEnd = addrSpaceEnd;
|
||||
|
||||
// First insertion will always need only a single block,
|
||||
// because there's nothing else to split.
|
||||
if (!slabManager.CanAllocate(1))
|
||||
{
|
||||
return KernelResult.OutOfResource;
|
||||
}
|
||||
|
||||
ulong addrSpacePagesCount = (addrSpaceEnd - addrSpaceStart) / PageSize;
|
||||
|
||||
_blocks.AddFirst(new KMemoryBlock(
|
||||
addrSpaceStart,
|
||||
addrSpacePagesCount,
|
||||
MemoryState.Unmapped,
|
||||
KMemoryPermission.None,
|
||||
MemoryAttribute.None));
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public void InsertBlock(
|
||||
ulong baseAddress,
|
||||
ulong pagesCount,
|
||||
MemoryState oldState,
|
||||
KMemoryPermission oldPermission,
|
||||
MemoryAttribute oldAttribute,
|
||||
MemoryState newState,
|
||||
KMemoryPermission newPermission,
|
||||
MemoryAttribute newAttribute)
|
||||
{
|
||||
// Insert new block on the list only on areas where the state
|
||||
// of the block matches the state specified on the old* state
|
||||
// arguments, otherwise leave it as is.
|
||||
int oldCount = _blocks.Count;
|
||||
|
||||
oldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
|
||||
|
||||
ulong endAddr = baseAddress + pagesCount * PageSize;
|
||||
|
||||
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
LinkedListNode<KMemoryBlock> newNode = node;
|
||||
|
||||
KMemoryBlock currBlock = node.Value;
|
||||
|
||||
ulong currBaseAddr = currBlock.BaseAddress;
|
||||
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
||||
|
||||
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
||||
{
|
||||
MemoryAttribute currBlockAttr = currBlock.Attribute | MemoryAttribute.IpcAndDeviceMapped;
|
||||
|
||||
if (currBlock.State != oldState ||
|
||||
currBlock.Permission != oldPermission ||
|
||||
currBlockAttr != oldAttribute)
|
||||
{
|
||||
node = node.Next;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (baseAddress > currBaseAddr)
|
||||
{
|
||||
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
||||
}
|
||||
|
||||
if (endAddr < currEndAddr)
|
||||
{
|
||||
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
||||
}
|
||||
|
||||
newNode.Value.SetState(newPermission, newState, newAttribute);
|
||||
|
||||
newNode = MergeEqualStateNeighbors(newNode);
|
||||
}
|
||||
|
||||
if (currEndAddr - 1 >= endAddr - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
node = newNode.Next;
|
||||
}
|
||||
|
||||
_slabManager.Count += _blocks.Count - oldCount;
|
||||
|
||||
ValidateInternalState();
|
||||
}
|
||||
|
||||
public void InsertBlock(
|
||||
ulong baseAddress,
|
||||
ulong pagesCount,
|
||||
MemoryState state,
|
||||
KMemoryPermission permission = KMemoryPermission.None,
|
||||
MemoryAttribute attribute = MemoryAttribute.None)
|
||||
{
|
||||
// Inserts new block at the list, replacing and splitting
|
||||
// existing blocks as needed.
|
||||
int oldCount = _blocks.Count;
|
||||
|
||||
ulong endAddr = baseAddress + pagesCount * PageSize;
|
||||
|
||||
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
LinkedListNode<KMemoryBlock> newNode = node;
|
||||
|
||||
KMemoryBlock currBlock = node.Value;
|
||||
|
||||
ulong currBaseAddr = currBlock.BaseAddress;
|
||||
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
||||
|
||||
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
||||
{
|
||||
if (baseAddress > currBaseAddr)
|
||||
{
|
||||
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
||||
}
|
||||
|
||||
if (endAddr < currEndAddr)
|
||||
{
|
||||
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
||||
}
|
||||
|
||||
newNode.Value.SetState(permission, state, attribute);
|
||||
|
||||
newNode = MergeEqualStateNeighbors(newNode);
|
||||
}
|
||||
|
||||
if (currEndAddr - 1 >= endAddr - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
node = newNode.Next;
|
||||
}
|
||||
|
||||
_slabManager.Count += _blocks.Count - oldCount;
|
||||
|
||||
ValidateInternalState();
|
||||
}
|
||||
|
||||
public delegate void BlockMutator(KMemoryBlock block, KMemoryPermission newPerm);
|
||||
|
||||
public void InsertBlock(
|
||||
ulong baseAddress,
|
||||
ulong pagesCount,
|
||||
BlockMutator blockMutate,
|
||||
KMemoryPermission permission = KMemoryPermission.None)
|
||||
{
|
||||
// Inserts new block at the list, replacing and splitting
|
||||
// existing blocks as needed, then calling the callback
|
||||
// function on the new block.
|
||||
int oldCount = _blocks.Count;
|
||||
|
||||
ulong endAddr = baseAddress + pagesCount * PageSize;
|
||||
|
||||
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
LinkedListNode<KMemoryBlock> newNode = node;
|
||||
|
||||
KMemoryBlock currBlock = node.Value;
|
||||
|
||||
ulong currBaseAddr = currBlock.BaseAddress;
|
||||
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
||||
|
||||
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
||||
{
|
||||
if (baseAddress > currBaseAddr)
|
||||
{
|
||||
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
||||
}
|
||||
|
||||
if (endAddr < currEndAddr)
|
||||
{
|
||||
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
||||
}
|
||||
|
||||
KMemoryBlock newBlock = newNode.Value;
|
||||
|
||||
blockMutate(newBlock, permission);
|
||||
|
||||
newNode = MergeEqualStateNeighbors(newNode);
|
||||
}
|
||||
|
||||
if (currEndAddr - 1 >= endAddr - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
node = newNode.Next;
|
||||
}
|
||||
|
||||
_slabManager.Count += _blocks.Count - oldCount;
|
||||
|
||||
ValidateInternalState();
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
private void ValidateInternalState()
|
||||
{
|
||||
ulong expectedAddress = 0;
|
||||
|
||||
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
LinkedListNode<KMemoryBlock> newNode = node;
|
||||
|
||||
KMemoryBlock currBlock = node.Value;
|
||||
|
||||
Debug.Assert(currBlock.BaseAddress == expectedAddress);
|
||||
|
||||
expectedAddress = currBlock.BaseAddress + currBlock.PagesCount * PageSize;
|
||||
|
||||
node = newNode.Next;
|
||||
}
|
||||
|
||||
Debug.Assert(expectedAddress == _addrSpaceEnd);
|
||||
}
|
||||
|
||||
private LinkedListNode<KMemoryBlock> MergeEqualStateNeighbors(LinkedListNode<KMemoryBlock> node)
|
||||
{
|
||||
KMemoryBlock block = node.Value;
|
||||
|
||||
if (node.Previous != null)
|
||||
{
|
||||
KMemoryBlock previousBlock = node.Previous.Value;
|
||||
|
||||
if (BlockStateEquals(block, previousBlock))
|
||||
{
|
||||
LinkedListNode<KMemoryBlock> previousNode = node.Previous;
|
||||
|
||||
_blocks.Remove(node);
|
||||
|
||||
previousBlock.AddPages(block.PagesCount);
|
||||
|
||||
node = previousNode;
|
||||
block = previousBlock;
|
||||
}
|
||||
}
|
||||
|
||||
if (node.Next != null)
|
||||
{
|
||||
KMemoryBlock nextBlock = node.Next.Value;
|
||||
|
||||
if (BlockStateEquals(block, nextBlock))
|
||||
{
|
||||
_blocks.Remove(node.Next);
|
||||
|
||||
block.AddPages(nextBlock.PagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static bool BlockStateEquals(KMemoryBlock lhs, KMemoryBlock rhs)
|
||||
{
|
||||
return lhs.State == rhs.State &&
|
||||
lhs.Permission == rhs.Permission &&
|
||||
lhs.Attribute == rhs.Attribute &&
|
||||
lhs.SourcePermission == rhs.SourcePermission &&
|
||||
lhs.DeviceRefCount == rhs.DeviceRefCount &&
|
||||
lhs.IpcRefCount == rhs.IpcRefCount;
|
||||
}
|
||||
|
||||
public KMemoryBlock FindBlock(ulong address)
|
||||
{
|
||||
return FindBlockNode(address)?.Value;
|
||||
}
|
||||
|
||||
public LinkedListNode<KMemoryBlock> FindBlockNode(ulong address)
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
KMemoryBlock block = node.Value;
|
||||
|
||||
ulong currEndAddr = block.PagesCount * PageSize + block.BaseAddress;
|
||||
|
||||
if (block.BaseAddress <= address && currEndAddr - 1 >= address)
|
||||
{
|
||||
return node;
|
||||
}
|
||||
|
||||
node = node.Next;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KMemoryBlockAllocator
|
||||
class KMemoryBlockSlabManager
|
||||
{
|
||||
private ulong _capacityElements;
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public KMemoryBlockAllocator(ulong capacityElements)
|
||||
public KMemoryBlockSlabManager(ulong capacityElements)
|
||||
{
|
||||
_capacityElements = capacityElements;
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,6 @@
|
|||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
|
@ -13,7 +14,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
private int _blockOrdersCount;
|
||||
|
||||
private KMemoryRegionBlock[] _blocks;
|
||||
private readonly KMemoryRegionBlock[] _blocks;
|
||||
|
||||
private readonly ushort[] _pageReferenceCounts;
|
||||
|
||||
public KMemoryRegionManager(ulong address, ulong size, ulong endAddr)
|
||||
{
|
||||
|
@ -80,9 +83,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
}
|
||||
}
|
||||
|
||||
_pageReferenceCounts = new ushort[size / KPageTableBase.PageSize];
|
||||
|
||||
if (size != 0)
|
||||
{
|
||||
FreePages(address, size / KMemoryManager.PageSize);
|
||||
FreePages(address, size / KPageTableBase.PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,15 +95,33 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
return AllocatePagesImpl(pagesCount, backwards, out pageList);
|
||||
KernelResult result = AllocatePagesImpl(pagesCount, backwards, out pageList);
|
||||
|
||||
if (result == KernelResult.Success)
|
||||
{
|
||||
foreach (var node in pageList)
|
||||
{
|
||||
IncrementPagesReferenceCount(node.Address, node.PagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong AllocatePagesContiguous(ulong pagesCount, bool backwards)
|
||||
public ulong AllocatePagesContiguous(KernelContext context, ulong pagesCount, bool backwards)
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
return AllocatePagesContiguousImpl(pagesCount, backwards);
|
||||
ulong address = AllocatePagesContiguousImpl(pagesCount, backwards);
|
||||
|
||||
if (address != 0)
|
||||
{
|
||||
IncrementPagesReferenceCount(address, pagesCount);
|
||||
context.Memory.Commit(address - DramMemoryMap.DramBase, pagesCount * KPageTableBase.PageSize);
|
||||
}
|
||||
|
||||
return address;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -124,7 +147,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
ulong bestFitBlockSize = 1UL << block.Order;
|
||||
|
||||
ulong blockPagesCount = bestFitBlockSize / KMemoryManager.PageSize;
|
||||
ulong blockPagesCount = bestFitBlockSize / KPageTableBase.PageSize;
|
||||
|
||||
// Check if this is the best fit for this page size.
|
||||
// If so, try allocating as much requested pages as possible.
|
||||
|
@ -185,7 +208,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
int blockIndex = 0;
|
||||
|
||||
while ((1UL << _blocks[blockIndex].Order) / KMemoryManager.PageSize < pagesCount)
|
||||
while ((1UL << _blocks[blockIndex].Order) / KPageTableBase.PageSize < pagesCount)
|
||||
{
|
||||
if (++blockIndex >= _blocks.Length)
|
||||
{
|
||||
|
@ -197,11 +220,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
ulong address = AllocatePagesForOrder(blockIndex, backwards, tightestFitBlockSize);
|
||||
|
||||
ulong requiredSize = pagesCount * KMemoryManager.PageSize;
|
||||
ulong requiredSize = pagesCount * KPageTableBase.PageSize;
|
||||
|
||||
if (address != 0 && tightestFitBlockSize > requiredSize)
|
||||
{
|
||||
FreePages(address + requiredSize, (tightestFitBlockSize - requiredSize) / KMemoryManager.PageSize);
|
||||
FreePages(address + requiredSize, (tightestFitBlockSize - requiredSize) / KPageTableBase.PageSize);
|
||||
}
|
||||
|
||||
return address;
|
||||
|
@ -327,137 +350,121 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
if (firstFreeBlockSize > bestFitBlockSize)
|
||||
{
|
||||
FreePages(address + bestFitBlockSize, (firstFreeBlockSize - bestFitBlockSize) / KMemoryManager.PageSize);
|
||||
FreePages(address + bestFitBlockSize, (firstFreeBlockSize - bestFitBlockSize) / KPageTableBase.PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
public void FreePage(ulong address)
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
FreePages(address, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void FreePages(KPageList pageList)
|
||||
{
|
||||
lock (_blocks)
|
||||
{
|
||||
foreach (KPageNode pageNode in pageList)
|
||||
{
|
||||
FreePages(pageNode.Address, pageNode.PagesCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FreePages(ulong address, ulong pagesCount)
|
||||
{
|
||||
ulong endAddr = address + pagesCount * KMemoryManager.PageSize;
|
||||
|
||||
int blockIndex = _blockOrdersCount - 1;
|
||||
|
||||
ulong addressRounded = 0;
|
||||
ulong endAddrTruncated = 0;
|
||||
|
||||
for (; blockIndex >= 0; blockIndex--)
|
||||
lock (_blocks)
|
||||
{
|
||||
KMemoryRegionBlock allocInfo = _blocks[blockIndex];
|
||||
ulong endAddr = address + pagesCount * KPageTableBase.PageSize;
|
||||
|
||||
int blockSize = 1 << allocInfo.Order;
|
||||
int blockIndex = _blockOrdersCount - 1;
|
||||
|
||||
addressRounded = BitUtils.AlignUp (address, blockSize);
|
||||
endAddrTruncated = BitUtils.AlignDown(endAddr, blockSize);
|
||||
ulong addressRounded = 0;
|
||||
ulong endAddrTruncated = 0;
|
||||
|
||||
if (addressRounded < endAddrTruncated)
|
||||
for (; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
KMemoryRegionBlock allocInfo = _blocks[blockIndex];
|
||||
|
||||
void FreeRegion(ulong currAddress)
|
||||
{
|
||||
for (int currBlockIndex = blockIndex;
|
||||
currBlockIndex < _blockOrdersCount && currAddress != 0;
|
||||
currBlockIndex++)
|
||||
{
|
||||
KMemoryRegionBlock block = _blocks[currBlockIndex];
|
||||
int blockSize = 1 << allocInfo.Order;
|
||||
|
||||
block.FreeCount++;
|
||||
addressRounded = BitUtils.AlignUp (address, blockSize);
|
||||
endAddrTruncated = BitUtils.AlignDown(endAddr, blockSize);
|
||||
|
||||
ulong freedBlocks = (currAddress - block.StartAligned) >> block.Order;
|
||||
|
||||
int index = (int)freedBlocks;
|
||||
|
||||
for (int level = block.MaxLevel - 1; level >= 0; level--, index /= 64)
|
||||
{
|
||||
long mask = block.Masks[level][index / 64];
|
||||
|
||||
block.Masks[level][index / 64] = mask | (1L << (index & 63));
|
||||
|
||||
if (mask != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int blockSizeDelta = 1 << (block.NextOrder - block.Order);
|
||||
|
||||
int freedBlocksTruncated = BitUtils.AlignDown((int)freedBlocks, blockSizeDelta);
|
||||
|
||||
if (!block.TryCoalesce(freedBlocksTruncated, blockSizeDelta))
|
||||
if (addressRounded < endAddrTruncated)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
currAddress = block.StartAligned + ((ulong)freedBlocksTruncated << block.Order);
|
||||
}
|
||||
}
|
||||
|
||||
// Free inside aligned region.
|
||||
ulong baseAddress = addressRounded;
|
||||
|
||||
while (baseAddress < endAddrTruncated)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
FreeRegion(baseAddress);
|
||||
|
||||
baseAddress += blockSize;
|
||||
}
|
||||
|
||||
int nextBlockIndex = blockIndex - 1;
|
||||
|
||||
// Free region between Address and aligned region start.
|
||||
baseAddress = addressRounded;
|
||||
|
||||
for (blockIndex = nextBlockIndex; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
while (baseAddress - blockSize >= address)
|
||||
void FreeRegion(ulong currAddress)
|
||||
{
|
||||
baseAddress -= blockSize;
|
||||
for (int currBlockIndex = blockIndex;
|
||||
currBlockIndex < _blockOrdersCount && currAddress != 0;
|
||||
currBlockIndex++)
|
||||
{
|
||||
KMemoryRegionBlock block = _blocks[currBlockIndex];
|
||||
|
||||
FreeRegion(baseAddress);
|
||||
block.FreeCount++;
|
||||
|
||||
ulong freedBlocks = (currAddress - block.StartAligned) >> block.Order;
|
||||
|
||||
int index = (int)freedBlocks;
|
||||
|
||||
for (int level = block.MaxLevel - 1; level >= 0; level--, index /= 64)
|
||||
{
|
||||
long mask = block.Masks[level][index / 64];
|
||||
|
||||
block.Masks[level][index / 64] = mask | (1L << (index & 63));
|
||||
|
||||
if (mask != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int blockSizeDelta = 1 << (block.NextOrder - block.Order);
|
||||
|
||||
int freedBlocksTruncated = BitUtils.AlignDown((int)freedBlocks, blockSizeDelta);
|
||||
|
||||
if (!block.TryCoalesce(freedBlocksTruncated, blockSizeDelta))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
currAddress = block.StartAligned + ((ulong)freedBlocksTruncated << block.Order);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Free region between aligned region end and End Address.
|
||||
baseAddress = endAddrTruncated;
|
||||
// Free inside aligned region.
|
||||
ulong baseAddress = addressRounded;
|
||||
|
||||
for (blockIndex = nextBlockIndex; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
while (baseAddress + blockSize <= endAddr)
|
||||
while (baseAddress < endAddrTruncated)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
FreeRegion(baseAddress);
|
||||
|
||||
baseAddress += blockSize;
|
||||
}
|
||||
|
||||
int nextBlockIndex = blockIndex - 1;
|
||||
|
||||
// Free region between Address and aligned region start.
|
||||
baseAddress = addressRounded;
|
||||
|
||||
for (blockIndex = nextBlockIndex; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
while (baseAddress - blockSize >= address)
|
||||
{
|
||||
baseAddress -= blockSize;
|
||||
|
||||
FreeRegion(baseAddress);
|
||||
}
|
||||
}
|
||||
|
||||
// Free region between aligned region end and End Address.
|
||||
baseAddress = endAddrTruncated;
|
||||
|
||||
for (blockIndex = nextBlockIndex; blockIndex >= 0; blockIndex--)
|
||||
{
|
||||
ulong blockSize = 1UL << _blocks[blockIndex].Order;
|
||||
|
||||
while (baseAddress + blockSize <= endAddr)
|
||||
{
|
||||
FreeRegion(baseAddress);
|
||||
|
||||
baseAddress += blockSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -477,12 +484,76 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
KMemoryRegionBlock block = _blocks[blockIndex];
|
||||
|
||||
ulong blockPagesCount = (1UL << block.Order) / KMemoryManager.PageSize;
|
||||
ulong blockPagesCount = (1UL << block.Order) / KPageTableBase.PageSize;
|
||||
|
||||
availablePages += blockPagesCount * block.FreeCount;
|
||||
}
|
||||
|
||||
return availablePages;
|
||||
}
|
||||
|
||||
public void IncrementPagesReferenceCount(ulong address, ulong pagesCount)
|
||||
{
|
||||
ulong index = GetPageOffset(address);
|
||||
ulong endIndex = index + pagesCount;
|
||||
|
||||
while (index < endIndex)
|
||||
{
|
||||
ushort referenceCount = ++_pageReferenceCounts[index];
|
||||
Debug.Assert(referenceCount >= 1);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
public void DecrementPagesReferenceCount(ulong address, ulong pagesCount)
|
||||
{
|
||||
ulong index = GetPageOffset(address);
|
||||
ulong endIndex = index + pagesCount;
|
||||
|
||||
ulong freeBaseIndex = 0;
|
||||
ulong freePagesCount = 0;
|
||||
|
||||
while (index < endIndex)
|
||||
{
|
||||
Debug.Assert(_pageReferenceCounts[index] > 0);
|
||||
ushort referenceCount = --_pageReferenceCounts[index];
|
||||
|
||||
if (referenceCount == 0)
|
||||
{
|
||||
if (freePagesCount != 0)
|
||||
{
|
||||
freePagesCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
freeBaseIndex = index;
|
||||
freePagesCount = 1;
|
||||
}
|
||||
}
|
||||
else if (freePagesCount != 0)
|
||||
{
|
||||
FreePages(Address + freeBaseIndex * KPageTableBase.PageSize, freePagesCount);
|
||||
freePagesCount = 0;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (freePagesCount != 0)
|
||||
{
|
||||
FreePages(Address + freeBaseIndex * KPageTableBase.PageSize, freePagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
public ulong GetPageOffset(ulong address)
|
||||
{
|
||||
return (address - Address) / KPageTableBase.PageSize;
|
||||
}
|
||||
|
||||
public ulong GetPageOffsetFromEnd(ulong address)
|
||||
{
|
||||
return (EndAddr - address) / KPageTableBase.PageSize;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
class KPageList : IEnumerable<KPageNode>
|
||||
{
|
||||
public LinkedList<KPageNode> Nodes { get; private set; }
|
||||
public LinkedList<KPageNode> Nodes { get; }
|
||||
|
||||
public KPageList()
|
||||
{
|
||||
|
@ -21,7 +21,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
KPageNode lastNode = Nodes.Last.Value;
|
||||
|
||||
if (lastNode.Address + lastNode.PagesCount * KMemoryManager.PageSize == address)
|
||||
if (lastNode.Address + lastNode.PagesCount * KPageTableBase.PageSize == address)
|
||||
{
|
||||
address = lastNode.Address;
|
||||
pagesCount += lastNode.PagesCount;
|
||||
|
@ -68,6 +68,22 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
return thisNode == null && otherNode == null;
|
||||
}
|
||||
|
||||
public void IncrementPagesReferenceCount(KMemoryManager manager)
|
||||
{
|
||||
foreach (var node in this)
|
||||
{
|
||||
manager.IncrementPagesReferenceCount(node.Address, node.PagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
public void DecrementPagesReferenceCount(KMemoryManager manager)
|
||||
{
|
||||
foreach (var node in this)
|
||||
{
|
||||
manager.DecrementPagesReferenceCount(node.Address, node.PagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KPageNode> GetEnumerator()
|
||||
{
|
||||
return Nodes.GetEnumerator();
|
||||
|
|
221
Ryujinx.HLE/HOS/Kernel/Memory/KPageTable.cs
Normal file
221
Ryujinx.HLE/HOS/Kernel/Memory/KPageTable.cs
Normal file
|
@ -0,0 +1,221 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.Memory;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KPageTable : KPageTableBase
|
||||
{
|
||||
private readonly IVirtualMemoryManager _cpuMemory;
|
||||
|
||||
public override bool SupportsMemoryAliasing => true;
|
||||
|
||||
public KPageTable(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context)
|
||||
{
|
||||
_cpuMemory = cpuMemory;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
return _cpuMemory.GetPhysicalRegions(va, size);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var srcRanges = GetPhysicalRegions(src, pagesCount * PageSize);
|
||||
|
||||
KernelResult result = Reprotect(src, pagesCount, KMemoryPermission.None);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
result = MapPages(dst, srcRanges, newDstPermission);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
KernelResult reprotectResult = Reprotect(src, pagesCount, oldSrcPermission);
|
||||
Debug.Assert(reprotectResult == KernelResult.Success);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult UnmapMemory(ulong dst, ulong src, ulong pagesCount, KMemoryPermission oldDstPermission, KMemoryPermission newSrcPermission)
|
||||
{
|
||||
ulong size = pagesCount * PageSize;
|
||||
|
||||
var srcRanges = GetPhysicalRegions(src, size);
|
||||
var dstRanges = GetPhysicalRegions(dst, size);
|
||||
|
||||
if (!dstRanges.SequenceEqual(srcRanges))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
KernelResult result = Unmap(dst, pagesCount);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
result = Reprotect(src, pagesCount, newSrcPermission);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
KernelResult mapResult = MapPages(dst, dstRanges, oldDstPermission);
|
||||
Debug.Assert(mapResult == KernelResult.Success);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong dstVa, ulong pagesCount, ulong srcPa, KMemoryPermission permission)
|
||||
{
|
||||
ulong size = pagesCount * PageSize;
|
||||
|
||||
Context.Memory.Commit(srcPa - DramMemoryMap.DramBase, size);
|
||||
|
||||
_cpuMemory.Map(dstVa, Context.Memory.GetPointer(srcPa - DramMemoryMap.DramBase, size), size);
|
||||
|
||||
if (DramMemoryMap.IsHeapPhysicalAddress(srcPa))
|
||||
{
|
||||
Context.MemoryManager.IncrementPagesReferenceCount(srcPa, pagesCount);
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, KPageList pageList, KMemoryPermission permission)
|
||||
{
|
||||
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, Context.Memory.GetPointer(addr, size), size);
|
||||
|
||||
currentVa += size;
|
||||
}
|
||||
|
||||
scopedPageList.SignalSuccess();
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, IEnumerable<HostMemoryRange> ranges, KMemoryPermission permission)
|
||||
{
|
||||
ulong currentVa = address;
|
||||
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
ulong size = range.Size;
|
||||
|
||||
ulong pa = GetDramAddressFromHostAddress(range.Address);
|
||||
if (pa != ulong.MaxValue)
|
||||
{
|
||||
pa += DramMemoryMap.DramBase;
|
||||
if (DramMemoryMap.IsHeapPhysicalAddress(pa))
|
||||
{
|
||||
Context.MemoryManager.IncrementPagesReferenceCount(pa, size / PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
_cpuMemory.Map(currentVa, range.Address, size);
|
||||
|
||||
currentVa += size;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult Unmap(ulong address, ulong pagesCount)
|
||||
{
|
||||
KPageList pagesToClose = new KPageList();
|
||||
|
||||
var regions = _cpuMemory.GetPhysicalRegions(address, pagesCount * PageSize);
|
||||
|
||||
foreach (var region in regions)
|
||||
{
|
||||
ulong pa = GetDramAddressFromHostAddress(region.Address);
|
||||
if (pa == ulong.MaxValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
pa += DramMemoryMap.DramBase;
|
||||
if (DramMemoryMap.IsHeapPhysicalAddress(pa))
|
||||
{
|
||||
pagesToClose.AddRange(pa, region.Size / PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
_cpuMemory.Unmap(address, pagesCount * PageSize);
|
||||
|
||||
pagesToClose.DecrementPagesReferenceCount(Context.MemoryManager);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private ulong GetDramAddressFromHostAddress(nuint hostAddress)
|
||||
{
|
||||
if (hostAddress < (nuint)(ulong)Context.Memory.Pointer || hostAddress >= (nuint)((ulong)Context.Memory.Pointer + Context.Memory.Size))
|
||||
{
|
||||
return ulong.MaxValue;
|
||||
}
|
||||
|
||||
return hostAddress - (ulong)Context.Memory.Pointer;
|
||||
}
|
||||
}
|
||||
}
|
2797
Ryujinx.HLE/HOS/Kernel/Memory/KPageTableBase.cs
Normal file
2797
Ryujinx.HLE/HOS/Kernel/Memory/KPageTableBase.cs
Normal file
File diff suppressed because it is too large
Load diff
125
Ryujinx.HLE/HOS/Kernel/Memory/KPageTableHostMapped.cs
Normal file
125
Ryujinx.HLE/HOS/Kernel/Memory/KPageTableHostMapped.cs
Normal file
|
@ -0,0 +1,125 @@
|
|||
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 IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
return _cpuMemory.GetPhysicalRegions(va, size);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
_cpuMemory.Map(dstVa, 0, pagesCount * PageSize);
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, KPageList pageList, KMemoryPermission permission)
|
||||
{
|
||||
_cpuMemory.Map(address, 0, pageList.GetPagesCount() * PageSize);
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, IEnumerable<HostMemoryRange> ranges, KMemoryPermission permission)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
}
|
27
Ryujinx.HLE/HOS/Kernel/Memory/KScopedPageList.cs
Normal file
27
Ryujinx.HLE/HOS/Kernel/Memory/KScopedPageList.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
struct KScopedPageList : IDisposable
|
||||
{
|
||||
private readonly KMemoryManager _manager;
|
||||
private KPageList _pageList;
|
||||
|
||||
public KScopedPageList(KMemoryManager manager, KPageList pageList)
|
||||
{
|
||||
_manager = manager;
|
||||
_pageList = pageList;
|
||||
pageList.IncrementPagesReferenceCount(manager);
|
||||
}
|
||||
|
||||
public void SignalSuccess()
|
||||
{
|
||||
_pageList = null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_pageList?.DecrementPagesReferenceCount(_manager);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
class KSharedMemory : KAutoObject
|
||||
{
|
||||
private readonly KPageList _pageList;
|
||||
private readonly SharedMemoryStorage _storage;
|
||||
|
||||
private readonly long _ownerPid;
|
||||
|
||||
|
@ -14,28 +14,29 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
private readonly KMemoryPermission _userPermission;
|
||||
|
||||
public KSharedMemory(
|
||||
KernelContext context,
|
||||
KPageList pageList,
|
||||
long ownerPid,
|
||||
KernelContext context,
|
||||
SharedMemoryStorage storage,
|
||||
long ownerPid,
|
||||
KMemoryPermission ownerPermission,
|
||||
KMemoryPermission userPermission) : base(context)
|
||||
{
|
||||
_pageList = pageList;
|
||||
_ownerPid = ownerPid;
|
||||
_storage = storage;
|
||||
_ownerPid = ownerPid;
|
||||
_ownerPermission = ownerPermission;
|
||||
_userPermission = userPermission;
|
||||
_userPermission = userPermission;
|
||||
}
|
||||
|
||||
public KernelResult MapIntoProcess(
|
||||
KMemoryManager memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process,
|
||||
KPageTableBase memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process,
|
||||
KMemoryPermission permission)
|
||||
{
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize);
|
||||
|
||||
if (_pageList.GetPagesCount() != pagesCountRounded)
|
||||
var pageList = _storage.GetPageList();
|
||||
if (pageList.GetPagesCount() != pagesCountRounded)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
@ -49,23 +50,35 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
return memoryManager.MapPages(address, _pageList, MemoryState.SharedMemory, permission);
|
||||
KernelResult result = memoryManager.MapPages(address, pageList, MemoryState.SharedMemory, permission);
|
||||
|
||||
if (result == KernelResult.Success && !memoryManager.SupportsMemoryAliasing)
|
||||
{
|
||||
_storage.Borrow(process, address);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public KernelResult UnmapFromProcess(
|
||||
KMemoryManager memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process)
|
||||
KPageTableBase memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process)
|
||||
{
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize);
|
||||
|
||||
if (_pageList.GetPagesCount() != pagesCountRounded)
|
||||
var pageList = _storage.GetPageList();
|
||||
ulong pagesCount = pageList.GetPagesCount();
|
||||
|
||||
if (pagesCount != pagesCountRounded)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
return memoryManager.UnmapPages(address, _pageList, MemoryState.SharedMemory);
|
||||
var ranges = _storage.GetRanges();
|
||||
|
||||
return memoryManager.UnmapPages(address, pagesCount, ranges, MemoryState.SharedMemory);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
|
@ -11,10 +13,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
// TODO: Remove when we no longer need to read it from the owner directly.
|
||||
public KProcess Creator => _creator;
|
||||
|
||||
private readonly KPageList _pageList;
|
||||
private readonly List<HostMemoryRange> _ranges;
|
||||
|
||||
public ulong Address { get; private set; }
|
||||
public ulong Size => _pageList.GetPagesCount() * KMemoryManager.PageSize;
|
||||
public ulong Size { get; private set; }
|
||||
|
||||
public KMemoryPermission Permission { get; private set; }
|
||||
|
||||
|
@ -23,7 +25,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
public KTransferMemory(KernelContext context) : base(context)
|
||||
{
|
||||
_pageList = new KPageList();
|
||||
_ranges = new List<HostMemoryRange>();
|
||||
}
|
||||
|
||||
public KernelResult Initialize(ulong address, ulong size, KMemoryPermission permission)
|
||||
|
@ -32,7 +34,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
_creator = creator;
|
||||
|
||||
KernelResult result = creator.MemoryManager.BorrowTransferMemory(_pageList, address, size, permission);
|
||||
KernelResult result = creator.MemoryManager.BorrowTransferMemory(_ranges, address, size, permission);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
|
@ -43,6 +45,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
|
||||
Permission = permission;
|
||||
Address = address;
|
||||
Size = size;
|
||||
_hasBeenInitialized = true;
|
||||
_isMapped = false;
|
||||
|
||||
|
@ -53,7 +56,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|||
{
|
||||
if (_hasBeenInitialized)
|
||||
{
|
||||
if (!_isMapped && _creator.MemoryManager.UnborrowTransferMemory(Address, Size, _pageList) != KernelResult.Success)
|
||||
if (!_isMapped && _creator.MemoryManager.UnborrowTransferMemory(Address, Size, _ranges) != KernelResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
|
||||
}
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
enum MemoryOperation
|
||||
{
|
||||
MapPa,
|
||||
MapVa,
|
||||
Allocate,
|
||||
Unmap,
|
||||
ChangePermRw,
|
||||
ChangePermsAndAttributes
|
||||
}
|
||||
}
|
103
Ryujinx.HLE/HOS/Kernel/Memory/SharedMemoryStorage.cs
Normal file
103
Ryujinx.HLE/HOS/Kernel/Memory/SharedMemoryStorage.cs
Normal file
|
@ -0,0 +1,103 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.Memory;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class SharedMemoryStorage
|
||||
{
|
||||
private readonly KernelContext _context;
|
||||
private readonly KPageList _pageList;
|
||||
private readonly ulong _size;
|
||||
|
||||
private IVirtualMemoryManager _borrowerMemory;
|
||||
private ulong _borrowerVa;
|
||||
|
||||
public SharedMemoryStorage(KernelContext context, KPageList pageList)
|
||||
{
|
||||
_context = context;
|
||||
_pageList = pageList;
|
||||
_size = pageList.GetPagesCount() * KPageTableBase.PageSize;
|
||||
|
||||
foreach (KPageNode pageNode in pageList)
|
||||
{
|
||||
ulong address = pageNode.Address - DramMemoryMap.DramBase;
|
||||
ulong size = pageNode.PagesCount * KPageTableBase.PageSize;
|
||||
context.Memory.Commit(address, size);
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
for (ulong offset = 0; offset < _size; offset += sizeof(ulong))
|
||||
{
|
||||
GetRef<ulong>(offset) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public ref T GetRef<T>(ulong offset) where T : unmanaged
|
||||
{
|
||||
if (_borrowerMemory == null)
|
||||
{
|
||||
if (_pageList.Nodes.Count == 1)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
else
|
||||
{
|
||||
return ref _borrowerMemory.GetRef<T>(_borrowerVa + offset);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<HostMemoryRange> GetRanges()
|
||||
{
|
||||
if (_borrowerMemory == null)
|
||||
{
|
||||
var ranges = new List<HostMemoryRange>();
|
||||
|
||||
foreach (KPageNode pageNode in _pageList)
|
||||
{
|
||||
ulong address = pageNode.Address - DramMemoryMap.DramBase;
|
||||
ulong size = pageNode.PagesCount * KPageTableBase.PageSize;
|
||||
|
||||
ranges.Add(new HostMemoryRange(_context.Memory.GetPointer(address, size), size));
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
else
|
||||
{
|
||||
return _borrowerMemory.GetPhysicalRegions(_borrowerVa, _size);
|
||||
}
|
||||
}
|
||||
|
||||
public KPageList GetPageList()
|
||||
{
|
||||
return _pageList;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
{
|
||||
interface IProcessContextFactory
|
||||
{
|
||||
IProcessContext Create(MemoryBlock backingMemory, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler);
|
||||
IProcessContext Create(KernelContext context, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
(KernelVersionMinor << 15) |
|
||||
(KernelVersionRevision << 0);
|
||||
|
||||
public KMemoryManager MemoryManager { get; private set; }
|
||||
public KPageTableBase MemoryManager { get; private set; }
|
||||
|
||||
private SortedDictionary<ulong, KTlsPageInfo> _fullTlsPages;
|
||||
private SortedDictionary<ulong, KTlsPageInfo> _freeTlsPages;
|
||||
|
@ -132,11 +132,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
ulong codeAddress = creationInfo.CodeAddress;
|
||||
|
||||
ulong codeSize = (ulong)creationInfo.CodePagesCount * KMemoryManager.PageSize;
|
||||
ulong codeSize = (ulong)creationInfo.CodePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
KMemoryBlockAllocator memoryBlockAllocator = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication)
|
||||
? KernelContext.LargeMemoryBlockAllocator
|
||||
: KernelContext.SmallMemoryBlockAllocator;
|
||||
KMemoryBlockSlabManager slabManager = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication)
|
||||
? KernelContext.LargeMemoryBlockSlabManager
|
||||
: KernelContext.SmallMemoryBlockSlabManager;
|
||||
|
||||
KernelResult result = MemoryManager.InitializeForProcess(
|
||||
addrSpaceType,
|
||||
|
@ -145,7 +145,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
memRegion,
|
||||
codeAddress,
|
||||
codeSize,
|
||||
memoryBlockAllocator);
|
||||
slabManager);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
|
@ -157,11 +157,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
result = MemoryManager.MapPages(
|
||||
codeAddress,
|
||||
pageList,
|
||||
MemoryState.CodeStatic,
|
||||
KMemoryPermission.None);
|
||||
result = MemoryManager.MapPages(codeAddress, pageList, MemoryState.CodeStatic, KMemoryPermission.None);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
|
@ -202,7 +198,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
ulong codePagesCount = (ulong)creationInfo.CodePagesCount;
|
||||
|
||||
ulong neededSizeForProcess = personalMmHeapSize + codePagesCount * KMemoryManager.PageSize;
|
||||
ulong neededSizeForProcess = personalMmHeapSize + codePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
if (neededSizeForProcess != 0 && resourceLimit != null)
|
||||
{
|
||||
|
@ -222,17 +218,17 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
PersonalMmHeapPagesCount = (ulong)creationInfo.SystemResourcePagesCount;
|
||||
|
||||
KMemoryBlockAllocator memoryBlockAllocator;
|
||||
KMemoryBlockSlabManager slabManager;
|
||||
|
||||
if (PersonalMmHeapPagesCount != 0)
|
||||
{
|
||||
memoryBlockAllocator = new KMemoryBlockAllocator(PersonalMmHeapPagesCount * KMemoryManager.PageSize);
|
||||
slabManager = new KMemoryBlockSlabManager(PersonalMmHeapPagesCount * KPageTableBase.PageSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
memoryBlockAllocator = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication)
|
||||
? KernelContext.LargeMemoryBlockAllocator
|
||||
: KernelContext.SmallMemoryBlockAllocator;
|
||||
slabManager = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication)
|
||||
? KernelContext.LargeMemoryBlockSlabManager
|
||||
: KernelContext.SmallMemoryBlockSlabManager;
|
||||
}
|
||||
|
||||
AddressSpaceType addrSpaceType = (AddressSpaceType)((int)(creationInfo.Flags & ProcessCreationFlags.AddressSpaceMask) >> (int)ProcessCreationFlags.AddressSpaceShift);
|
||||
|
@ -243,7 +239,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
ulong codeAddress = creationInfo.CodeAddress;
|
||||
|
||||
ulong codeSize = codePagesCount * KMemoryManager.PageSize;
|
||||
ulong codeSize = codePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
KernelResult result = MemoryManager.InitializeForProcess(
|
||||
addrSpaceType,
|
||||
|
@ -252,7 +248,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
memRegion,
|
||||
codeAddress,
|
||||
codeSize,
|
||||
memoryBlockAllocator);
|
||||
slabManager);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
|
@ -268,7 +264,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
result = MemoryManager.MapNewProcessCode(
|
||||
result = MemoryManager.MapPages(
|
||||
codeAddress,
|
||||
codePagesCount,
|
||||
MemoryState.CodeStatic,
|
||||
|
@ -352,7 +348,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
_version = creationInfo.Version;
|
||||
TitleId = creationInfo.TitleId;
|
||||
_entrypoint = creationInfo.CodeAddress;
|
||||
_imageSize = (ulong)creationInfo.CodePagesCount * KMemoryManager.PageSize;
|
||||
_imageSize = (ulong)creationInfo.CodePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
switch (Flags & ProcessCreationFlags.AddressSpaceMask)
|
||||
{
|
||||
|
@ -396,9 +392,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
if (pageInfo.IsFull())
|
||||
{
|
||||
_freeTlsPages.Remove(pageInfo.PageAddr);
|
||||
_freeTlsPages.Remove(pageInfo.PageVirtualAddress);
|
||||
|
||||
_fullTlsPages.Add(pageInfo.PageAddr, pageInfo);
|
||||
_fullTlsPages.Add(pageInfo.PageVirtualAddress, pageInfo);
|
||||
}
|
||||
|
||||
result = KernelResult.Success;
|
||||
|
@ -415,7 +411,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
throw new InvalidOperationException("Unexpected failure getting free TLS page!");
|
||||
}
|
||||
|
||||
_freeTlsPages.Add(pageInfo.PageAddr, pageInfo);
|
||||
_freeTlsPages.Add(pageInfo.PageVirtualAddress, pageInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -440,11 +436,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
ulong regionStart = MemoryManager.TlsIoRegionStart;
|
||||
ulong regionSize = MemoryManager.TlsIoRegionEnd - regionStart;
|
||||
|
||||
ulong regionPagesCount = regionSize / KMemoryManager.PageSize;
|
||||
ulong regionPagesCount = regionSize / KPageTableBase.PageSize;
|
||||
|
||||
KernelResult result = MemoryManager.AllocateOrMapPa(
|
||||
KernelResult result = MemoryManager.MapPages(
|
||||
1,
|
||||
KMemoryManager.PageSize,
|
||||
KPageTableBase.PageSize,
|
||||
tlsPagePa,
|
||||
true,
|
||||
regionStart,
|
||||
|
@ -459,9 +455,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
}
|
||||
else
|
||||
{
|
||||
pageInfo = new KTlsPageInfo(tlsPageVa);
|
||||
pageInfo = new KTlsPageInfo(tlsPageVa, tlsPagePa);
|
||||
|
||||
MemoryHelper.FillWithZeros(CpuMemory, tlsPageVa, KMemoryManager.PageSize);
|
||||
MemoryHelper.FillWithZeros(CpuMemory, tlsPageVa, KPageTableBase.PageSize);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -469,7 +465,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
public KernelResult FreeThreadLocalStorage(ulong tlsSlotAddr)
|
||||
{
|
||||
ulong tlsPageAddr = BitUtils.AlignDown(tlsSlotAddr, KMemoryManager.PageSize);
|
||||
ulong tlsPageAddr = BitUtils.AlignDown(tlsSlotAddr, KPageTableBase.PageSize);
|
||||
|
||||
KernelContext.CriticalSection.Enter();
|
||||
|
||||
|
@ -514,16 +510,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
private KernelResult FreeTlsPage(KTlsPageInfo pageInfo)
|
||||
{
|
||||
if (!MemoryManager.TryConvertVaToPa(pageInfo.PageAddr, out ulong tlsPagePa))
|
||||
{
|
||||
throw new InvalidOperationException("Unexpected failure translating virtual address to physical.");
|
||||
}
|
||||
|
||||
KernelResult result = MemoryManager.UnmapForKernel(pageInfo.PageAddr, 1, MemoryState.ThreadLocal);
|
||||
KernelResult result = MemoryManager.UnmapForKernel(pageInfo.PageVirtualAddress, 1, MemoryState.ThreadLocal);
|
||||
|
||||
if (result == KernelResult.Success)
|
||||
{
|
||||
KernelContext.UserSlabHeapPages.Free(tlsPagePa);
|
||||
KernelContext.UserSlabHeapPages.Free(pageInfo.PagePhysicalAddress);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -556,7 +547,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
throw new InvalidOperationException("Trying to start a process with a invalid state!");
|
||||
}
|
||||
|
||||
ulong stackSizeRounded = BitUtils.AlignUp(stackSize, KMemoryManager.PageSize);
|
||||
ulong stackSizeRounded = BitUtils.AlignUp(stackSize, KPageTableBase.PageSize);
|
||||
|
||||
ulong neededSize = stackSizeRounded + _imageSize;
|
||||
|
||||
|
@ -598,7 +589,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
{
|
||||
ulong stackBottom = stackTop - _mainThreadStackSize;
|
||||
|
||||
ulong stackPagesCount = _mainThreadStackSize / KMemoryManager.PageSize;
|
||||
ulong stackPagesCount = _mainThreadStackSize / KPageTableBase.PageSize;
|
||||
|
||||
MemoryManager.UnmapForKernel(stackBottom, stackPagesCount, MemoryState.Stack);
|
||||
|
||||
|
@ -611,16 +602,16 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
if (stackSizeRounded != 0)
|
||||
{
|
||||
ulong stackPagesCount = stackSizeRounded / KMemoryManager.PageSize;
|
||||
ulong stackPagesCount = stackSizeRounded / KPageTableBase.PageSize;
|
||||
|
||||
ulong regionStart = MemoryManager.StackRegionStart;
|
||||
ulong regionSize = MemoryManager.StackRegionEnd - regionStart;
|
||||
|
||||
ulong regionPagesCount = regionSize / KMemoryManager.PageSize;
|
||||
ulong regionPagesCount = regionSize / KPageTableBase.PageSize;
|
||||
|
||||
result = MemoryManager.AllocateOrMapPa(
|
||||
result = MemoryManager.MapPages(
|
||||
stackPagesCount,
|
||||
KMemoryManager.PageSize,
|
||||
KPageTableBase.PageSize,
|
||||
0,
|
||||
false,
|
||||
regionStart,
|
||||
|
@ -834,7 +825,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
return 0;
|
||||
}
|
||||
|
||||
return personalMmHeapPagesCount * KMemoryManager.PageSize;
|
||||
return personalMmHeapPagesCount * KPageTableBase.PageSize;
|
||||
}
|
||||
|
||||
public void AddCpuTime(long ticks)
|
||||
|
@ -1058,16 +1049,23 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
_ => 39
|
||||
};
|
||||
|
||||
Context = _contextFactory.Create(KernelContext.Memory, 1UL << addrSpaceBits, InvalidAccessHandler);
|
||||
Context = _contextFactory.Create(KernelContext, 1UL << addrSpaceBits, InvalidAccessHandler);
|
||||
|
||||
// TODO: This should eventually be removed.
|
||||
// The GPU shouldn't depend on the CPU memory manager at all.
|
||||
if (flags.HasFlag(ProcessCreationFlags.IsApplication))
|
||||
{
|
||||
KernelContext.Device.Gpu.SetVmm((MemoryManager)CpuMemory);
|
||||
KernelContext.Device.Gpu.SetVmm((IVirtualMemoryManagerTracked)CpuMemory);
|
||||
}
|
||||
|
||||
MemoryManager = new KMemoryManager(KernelContext, CpuMemory);
|
||||
if (Context.AddressSpace is MemoryManagerHostMapped)
|
||||
{
|
||||
MemoryManager = new KPageTableHostMapped(KernelContext, CpuMemory);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryManager = new KPageTable(KernelContext, CpuMemory);
|
||||
}
|
||||
}
|
||||
|
||||
private bool InvalidAccessHandler(ulong va)
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
IrqAccessMask = new byte[0x80];
|
||||
}
|
||||
|
||||
public KernelResult InitializeForKernel(ReadOnlySpan<int> capabilities, KMemoryManager memoryManager)
|
||||
public KernelResult InitializeForKernel(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
|
||||
{
|
||||
AllowedCpuCoresMask = 0xf;
|
||||
AllowedThreadPriosMask = -1;
|
||||
|
@ -35,12 +35,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
return Parse(capabilities, memoryManager);
|
||||
}
|
||||
|
||||
public KernelResult InitializeForUser(ReadOnlySpan<int> capabilities, KMemoryManager memoryManager)
|
||||
public KernelResult InitializeForUser(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
|
||||
{
|
||||
return Parse(capabilities, memoryManager);
|
||||
}
|
||||
|
||||
private KernelResult Parse(ReadOnlySpan<int> capabilities, KMemoryManager memoryManager)
|
||||
private KernelResult Parse(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
|
||||
{
|
||||
int mask0 = 0;
|
||||
int mask1 = 0;
|
||||
|
@ -117,7 +117,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
private KernelResult ParseCapability(int cap, ref int mask0, ref int mask1, KMemoryManager memoryManager)
|
||||
private KernelResult ParseCapability(int cap, ref int mask0, ref int mask1, KPageTableBase memoryManager)
|
||||
{
|
||||
int code = (cap + 1) & ~cap;
|
||||
|
||||
|
@ -217,7 +217,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
{
|
||||
long address = ((long)(uint)cap << 4) & 0xffffff000;
|
||||
|
||||
memoryManager.MapIoMemory(address, KMemoryManager.PageSize, KMemoryPermission.ReadAndWrite);
|
||||
memoryManager.MapIoMemory(address, KPageTableBase.PageSize, KMemoryPermission.ReadAndWrite);
|
||||
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -6,15 +6,17 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
{
|
||||
public const int TlsEntrySize = 0x200;
|
||||
|
||||
public ulong PageAddr { get; private set; }
|
||||
public ulong PageVirtualAddress { get; }
|
||||
public ulong PagePhysicalAddress { get; }
|
||||
|
||||
private bool[] _isSlotFree;
|
||||
private readonly bool[] _isSlotFree;
|
||||
|
||||
public KTlsPageInfo(ulong pageAddress)
|
||||
public KTlsPageInfo(ulong pageVirtualAddress, ulong pagePhysicalAddress)
|
||||
{
|
||||
PageAddr = pageAddress;
|
||||
PageVirtualAddress = pageVirtualAddress;
|
||||
PagePhysicalAddress = pagePhysicalAddress;
|
||||
|
||||
_isSlotFree = new bool[KMemoryManager.PageSize / TlsEntrySize];
|
||||
_isSlotFree = new bool[KPageTableBase.PageSize / TlsEntrySize];
|
||||
|
||||
for (int index = 0; index < _isSlotFree.Length; index++)
|
||||
{
|
||||
|
@ -24,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
public bool TryGetFreePage(out ulong address)
|
||||
{
|
||||
address = PageAddr;
|
||||
address = PageVirtualAddress;
|
||||
|
||||
for (int index = 0; index < _isSlotFree.Length; index++)
|
||||
{
|
||||
|
@ -69,7 +71,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
|
||||
public void FreeTlsSlot(ulong address)
|
||||
{
|
||||
_isSlotFree[(address - PageAddr) / TlsEntrySize] = true;
|
||||
_isSlotFree[(address - PageVirtualAddress) / TlsEntrySize] = true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,7 +20,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
{
|
||||
_pagePosition = pagePosition;
|
||||
|
||||
_slots = new bool[KMemoryManager.PageSize / TlsEntrySize];
|
||||
_slots = new bool[KPageTableBase.PageSize / TlsEntrySize];
|
||||
}
|
||||
|
||||
public bool TryGetFreeTlsAddr(out long position)
|
||||
|
|
|
@ -5,9 +5,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
|||
{
|
||||
class ProcessContextFactory : IProcessContextFactory
|
||||
{
|
||||
public IProcessContext Create(MemoryBlock backingMemory, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler)
|
||||
public IProcessContext Create(KernelContext context, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler)
|
||||
{
|
||||
return new ProcessContext(new AddressSpaceManager(backingMemory, addressSpaceSize));
|
||||
return new ProcessContext(new AddressSpaceManager(addressSpaceSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1278,7 +1278,7 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
|||
|
||||
private static bool PageAligned(ulong position)
|
||||
{
|
||||
return (position & (KMemoryManager.PageSize - 1)) == 0;
|
||||
return (position & (KPageTableBase.PageSize - 1)) == 0;
|
||||
}
|
||||
|
||||
// System
|
||||
|
@ -1504,12 +1504,12 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
|||
value = (long)(process.MemoryManager.StackRegionEnd -
|
||||
process.MemoryManager.StackRegionStart); break;
|
||||
|
||||
case 16: value = (long)process.PersonalMmHeapPagesCount * KMemoryManager.PageSize; break;
|
||||
case 16: value = (long)process.PersonalMmHeapPagesCount * KPageTableBase.PageSize; break;
|
||||
|
||||
case 17:
|
||||
if (process.PersonalMmHeapPagesCount != 0)
|
||||
{
|
||||
value = process.MemoryManager.GetMmUsedPages() * KMemoryManager.PageSize;
|
||||
value = process.MemoryManager.GetMmUsedPages() * KPageTableBase.PageSize;
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -1760,7 +1760,7 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
|||
return KernelResult.InvalidCombination;
|
||||
}
|
||||
|
||||
KMemoryRegionManager region = _context.MemoryRegions[subId];
|
||||
KMemoryRegionManager region = _context.MemoryManager.MemoryRegions[subId];
|
||||
|
||||
switch (id)
|
||||
{
|
||||
|
@ -1772,7 +1772,7 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
|||
{
|
||||
ulong freePagesCount = region.GetFreePages();
|
||||
|
||||
value = (long)(freePagesCount * KMemoryManager.PageSize);
|
||||
value = (long)(freePagesCount * KPageTableBase.PageSize);
|
||||
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
|
|||
private ulong _tlsAddress;
|
||||
|
||||
public ulong TlsAddress => _tlsAddress;
|
||||
public ulong TlsDramAddress { get; private set; }
|
||||
|
||||
public KSynchronizationObject[] WaitSyncObjects { get; }
|
||||
public int[] WaitSyncHandles { get; }
|
||||
|
@ -159,8 +158,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
|
|||
return KernelResult.OutOfMemory;
|
||||
}
|
||||
|
||||
TlsDramAddress = owner.MemoryManager.GetDramAddressFromVa(_tlsAddress);
|
||||
|
||||
MemoryHelper.FillWithZeros(owner.CpuMemory, _tlsAddress, KTlsPageInfo.TlsEntrySize);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue