2018-12-18 00:33:36 -05:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Memory;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2023-01-04 17:15:45 -05:00
|
|
|
using Ryujinx.Horizon.Common;
|
2020-12-01 18:23:43 -05:00
|
|
|
using System;
|
2022-02-17 20:35:23 -05:00
|
|
|
using System.Numerics;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Process
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
class KProcessCapabilities
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
public byte[] SvcAccessMask { get; private set; }
|
|
|
|
public byte[] IrqAccessMask { get; private set; }
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2022-01-29 16:18:03 -05:00
|
|
|
public ulong AllowedCpuCoresMask { get; private set; }
|
|
|
|
public ulong AllowedThreadPriosMask { get; private set; }
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
public int DebuggingFlags { get; private set; }
|
|
|
|
public int HandleTableSize { get; private set; }
|
|
|
|
public int KernelReleaseVersion { get; private set; }
|
|
|
|
public int ApplicationType { get; private set; }
|
|
|
|
|
|
|
|
public KProcessCapabilities()
|
|
|
|
{
|
|
|
|
SvcAccessMask = new byte[0x10];
|
|
|
|
IrqAccessMask = new byte[0x80];
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
public Result InitializeForKernel(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
AllowedCpuCoresMask = 0xf;
|
2022-01-29 16:18:03 -05:00
|
|
|
AllowedThreadPriosMask = ulong.MaxValue;
|
2018-11-28 17:18:09 -05:00
|
|
|
DebuggingFlags &= ~3;
|
|
|
|
KernelReleaseVersion = KProcess.KernelVersionPacked;
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
return Parse(capabilities, memoryManager);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
public Result InitializeForUser(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
return Parse(capabilities, memoryManager);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
private Result Parse(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int mask0 = 0;
|
|
|
|
int mask1 = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
for (int index = 0; index < capabilities.Length; index++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
int cap = capabilities[index];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (((cap + 1) & ~cap) != 0x40)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2023-01-04 17:15:45 -05:00
|
|
|
Result result = ParseCapability(cap, ref mask0, ref mask1, memoryManager);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
if (result != Result.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
if ((uint)index + 1 >= capabilities.Length)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int prevCap = cap;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
cap = capabilities[++index];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (((cap + 1) & ~cap) != 0x40)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((cap & 0x78000000) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((cap & 0x7ffff80) == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
long address = ((long)(uint)prevCap << 5) & 0xffffff000;
|
|
|
|
long size = ((long)(uint)cap << 5) & 0xfffff000;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (((ulong)(address + size - 1) >> 36) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission perm = (prevCap >> 31) != 0
|
|
|
|
? KMemoryPermission.Read
|
|
|
|
: KMemoryPermission.ReadAndWrite;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
Result result;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((cap >> 31) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = memoryManager.MapNormalMemory(address, size, perm);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = memoryManager.MapIoMemory(address, size, perm);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
if (result != Result.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
private Result ParseCapability(int cap, ref int mask0, ref int mask1, KPageTableBase memoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int code = (cap + 1) & ~cap;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (code == 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCapability;
|
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (code == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 20:35:23 -05:00
|
|
|
int codeMask = 1 << (32 - BitOperations.LeadingZeroCount((uint)code + 1));
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Check if the property was already set.
|
2018-12-06 06:16:24 -05:00
|
|
|
if (((mask0 & codeMask) & 0x1e008) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
mask0 |= codeMask;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (code)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
if (AllowedCpuCoresMask != 0 || AllowedThreadPriosMask != 0)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidCapability;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int lowestCpuCore = (cap >> 16) & 0xff;
|
|
|
|
int highestCpuCore = (cap >> 24) & 0xff;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (lowestCpuCore > highestCpuCore)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int highestThreadPrio = (cap >> 4) & 0x3f;
|
|
|
|
int lowestThreadPrio = (cap >> 10) & 0x3f;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (lowestThreadPrio > highestThreadPrio)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (highestCpuCore >= KScheduler.CpuCoresCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCpuCore;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AllowedCpuCoresMask = GetMaskFromMinMax(lowestCpuCore, highestCpuCore);
|
|
|
|
AllowedThreadPriosMask = GetMaskFromMinMax(lowestThreadPrio, highestThreadPrio);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x10:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int slot = (cap >> 29) & 7;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int svcSlotMask = 1 << slot;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((mask1 & svcSlotMask) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
mask1 |= svcSlotMask;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int svcMask = (cap >> 5) & 0xffffff;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int baseSvc = slot * 24;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < 24; index++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (((svcMask >> index) & 1) == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int svcId = baseSvc + index;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (svcId > 0x7f)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
SvcAccessMask[svcId / 8] |= (byte)(1 << (svcId & 7));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x80:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
long address = ((long)(uint)cap << 4) & 0xffffff000;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
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>
2021-05-24 16:52:44 -04:00
|
|
|
memoryManager.MapIoMemory(address, KPageTableBase.PageSize, KMemoryPermission.ReadAndWrite);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x800:
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// TODO: GIC distributor check.
|
2018-12-06 06:16:24 -05:00
|
|
|
int irq0 = (cap >> 12) & 0x3ff;
|
|
|
|
int irq1 = (cap >> 22) & 0x3ff;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (irq0 != 0x3ff)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
IrqAccessMask[irq0 / 8] |= (byte)(1 << (irq0 & 7));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (irq1 != 0x3ff)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
IrqAccessMask[irq1 / 8] |= (byte)(1 << (irq1 & 7));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x2000:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int applicationType = cap >> 14;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((uint)applicationType > 7)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ApplicationType = applicationType;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x4000:
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Note: This check is bugged on kernel too, we are just replicating the bug here.
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((KernelReleaseVersion >> 17) != 0 || cap < 0x80000)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelReleaseVersion = cap;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x8000:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int handleTableSize = cap >> 26;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((uint)handleTableSize > 0x3ff)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
HandleTableSize = handleTableSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x10000:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int debuggingFlags = cap >> 19;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((uint)debuggingFlags > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
DebuggingFlags &= ~3;
|
|
|
|
DebuggingFlags |= debuggingFlags;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: return KernelResult.InvalidCapability;
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2022-01-29 16:18:03 -05:00
|
|
|
private static ulong GetMaskFromMinMax(int min, int max)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int range = max - min + 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (range == 64)
|
|
|
|
{
|
2022-01-29 16:18:03 -05:00
|
|
|
return ulong.MaxValue;
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-29 16:18:03 -05:00
|
|
|
ulong mask = (1UL << range) - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return mask << min;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|