aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs
diff options
context:
space:
mode:
authorriperiperi <rhy3756547@hotmail.com>2021-05-24 21:52:44 +0100
committerGitHub <noreply@github.com>2021-05-24 22:52:44 +0200
commit54ea2285f05ef6f59a6f1c63df4a7bdd77d7b883 (patch)
tree244f601856dc96f2bf8ce15aecd0b5623ae7b234 /Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs
parentfb65f392d1c4b0e01f22b6ddebcc8317ba9769c3 (diff)
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>
Diffstat (limited to 'Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs')
-rw-r--r--Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs92
1 files changed, 45 insertions, 47 deletions
diff --git a/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs b/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs
index 516b53f4..f2ba675f 100644
--- a/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs
+++ b/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs
@@ -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)