1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-02 21:42:02 +00:00
Ryujinx/Ryujinx.HLE/HOS/Kernel/KProcessCapabilities.cs

311 lines
9.2 KiB
C#
Raw Normal View History

using Ryujinx.Common;
namespace Ryujinx.HLE.HOS.Kernel
{
class KProcessCapabilities
{
public byte[] SvcAccessMask { get; private set; }
public byte[] IrqAccessMask { get; private set; }
public long AllowedCpuCoresMask { get; private set; }
public long AllowedThreadPriosMask { get; private set; }
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];
}
2018-12-01 20:01:59 +00:00
public KernelResult InitializeForKernel(int[] caps, KMemoryManager memoryManager)
{
AllowedCpuCoresMask = 0xf;
AllowedThreadPriosMask = -1;
DebuggingFlags &= ~3;
KernelReleaseVersion = KProcess.KernelVersionPacked;
2018-12-01 20:01:59 +00:00
return Parse(caps, memoryManager);
}
2018-12-01 20:01:59 +00:00
public KernelResult InitializeForUser(int[] caps, KMemoryManager memoryManager)
{
2018-12-01 20:01:59 +00:00
return Parse(caps, memoryManager);
}
2018-12-01 20:01:59 +00:00
private KernelResult Parse(int[] caps, KMemoryManager memoryManager)
{
2018-12-01 20:01:59 +00:00
int mask0 = 0;
int mask1 = 0;
2018-12-01 20:01:59 +00:00
for (int index = 0; index < caps.Length; index++)
{
2018-12-01 20:01:59 +00:00
int cap = caps[index];
2018-12-01 20:01:59 +00:00
if (((cap + 1) & ~cap) != 0x40)
{
2018-12-01 20:01:59 +00:00
KernelResult result = ParseCapability(cap, ref mask0, ref mask1, memoryManager);
2018-12-01 20:01:59 +00:00
if (result != KernelResult.Success)
{
2018-12-01 20:01:59 +00:00
return result;
}
}
else
{
2018-12-01 20:01:59 +00:00
if ((uint)index + 1 >= caps.Length)
{
return KernelResult.InvalidCombination;
}
2018-12-01 20:01:59 +00:00
int prevCap = cap;
2018-12-01 20:01:59 +00:00
cap = caps[++index];
2018-12-01 20:01:59 +00:00
if (((cap + 1) & ~cap) != 0x40)
{
return KernelResult.InvalidCombination;
}
2018-12-01 20:01:59 +00:00
if ((cap & 0x78000000) != 0)
{
return KernelResult.MaximumExceeded;
}
2018-12-01 20:01:59 +00:00
if ((cap & 0x7ffff80) == 0)
{
return KernelResult.InvalidSize;
}
2018-12-01 20:01:59 +00:00
long address = ((long)(uint)prevCap << 5) & 0xffffff000;
long size = ((long)(uint)cap << 5) & 0xfffff000;
2018-12-01 20:01:59 +00:00
if (((ulong)(address + size - 1) >> 36) != 0)
{
return KernelResult.InvalidAddress;
}
2018-12-01 20:01:59 +00:00
MemoryPermission perm = (prevCap >> 31) != 0
? MemoryPermission.Read
: MemoryPermission.ReadAndWrite;
2018-12-01 20:01:59 +00:00
KernelResult result;
2018-12-01 20:01:59 +00:00
if ((cap >> 31) != 0)
{
2018-12-01 20:01:59 +00:00
result = memoryManager.MapNormalMemory(address, size, perm);
}
else
{
2018-12-01 20:01:59 +00:00
result = memoryManager.MapIoMemory(address, size, perm);
}
2018-12-01 20:01:59 +00:00
if (result != KernelResult.Success)
{
2018-12-01 20:01:59 +00:00
return result;
}
}
}
return KernelResult.Success;
}
2018-12-01 20:01:59 +00:00
private KernelResult ParseCapability(int cap, ref int mask0, ref int mask1, KMemoryManager memoryManager)
{
2018-12-01 20:01:59 +00:00
int code = (cap + 1) & ~cap;
2018-12-01 20:01:59 +00:00
if (code == 1)
{
return KernelResult.InvalidCapability;
}
2018-12-01 20:01:59 +00:00
else if (code == 0)
{
return KernelResult.Success;
}
2018-12-01 20:01:59 +00:00
int codeMask = 1 << (32 - BitUtils.CountLeadingZeros32(code + 1));
//Check if the property was already set.
2018-12-01 20:01:59 +00:00
if (((mask0 & codeMask) & 0x1e008) != 0)
{
return KernelResult.InvalidCombination;
}
2018-12-01 20:01:59 +00:00
mask0 |= codeMask;
2018-12-01 20:01:59 +00:00
switch (code)
{
case 8:
{
if (AllowedCpuCoresMask != 0 || AllowedThreadPriosMask != 0)
{
return KernelResult.InvalidCapability;
}
2018-12-01 20:01:59 +00:00
int lowestCpuCore = (cap >> 16) & 0xff;
int highestCpuCore = (cap >> 24) & 0xff;
2018-12-01 20:01:59 +00:00
if (lowestCpuCore > highestCpuCore)
{
return KernelResult.InvalidCombination;
}
2018-12-01 20:01:59 +00:00
int highestThreadPrio = (cap >> 4) & 0x3f;
int lowestThreadPrio = (cap >> 10) & 0x3f;
2018-12-01 20:01:59 +00:00
if (lowestThreadPrio > highestThreadPrio)
{
return KernelResult.InvalidCombination;
}
2018-12-01 20:01:59 +00:00
if (highestCpuCore >= KScheduler.CpuCoresCount)
{
return KernelResult.InvalidCpuCore;
}
2018-12-01 20:01:59 +00:00
AllowedCpuCoresMask = GetMaskFromMinMax(lowestCpuCore, highestCpuCore);
AllowedThreadPriosMask = GetMaskFromMinMax(lowestThreadPrio, highestThreadPrio);
break;
}
case 0x10:
{
2018-12-01 20:01:59 +00:00
int slot = (cap >> 29) & 7;
2018-12-01 20:01:59 +00:00
int svcSlotMask = 1 << slot;
2018-12-01 20:01:59 +00:00
if ((mask1 & svcSlotMask) != 0)
{
return KernelResult.InvalidCombination;
}
2018-12-01 20:01:59 +00:00
mask1 |= svcSlotMask;
2018-12-01 20:01:59 +00:00
int svcMask = (cap >> 5) & 0xffffff;
2018-12-01 20:01:59 +00:00
int baseSvc = slot * 24;
2018-12-01 20:01:59 +00:00
for (int index = 0; index < 24; index++)
{
2018-12-01 20:01:59 +00:00
if (((svcMask >> index) & 1) == 0)
{
continue;
}
2018-12-01 20:01:59 +00:00
int svcId = baseSvc + index;
2018-12-01 20:01:59 +00:00
if (svcId > 0x7f)
{
return KernelResult.MaximumExceeded;
}
2018-12-01 20:01:59 +00:00
SvcAccessMask[svcId / 8] |= (byte)(1 << (svcId & 7));
}
break;
}
case 0x80:
{
2018-12-01 20:01:59 +00:00
long address = ((long)(uint)cap << 4) & 0xffffff000;
2018-12-01 20:01:59 +00:00
memoryManager.MapIoMemory(address, KMemoryManager.PageSize, MemoryPermission.ReadAndWrite);
break;
}
case 0x800:
{
//TODO: GIC distributor check.
2018-12-01 20:01:59 +00:00
int irq0 = (cap >> 12) & 0x3ff;
int irq1 = (cap >> 22) & 0x3ff;
2018-12-01 20:01:59 +00:00
if (irq0 != 0x3ff)
{
2018-12-01 20:01:59 +00:00
IrqAccessMask[irq0 / 8] |= (byte)(1 << (irq0 & 7));
}
2018-12-01 20:01:59 +00:00
if (irq1 != 0x3ff)
{
2018-12-01 20:01:59 +00:00
IrqAccessMask[irq1 / 8] |= (byte)(1 << (irq1 & 7));
}
break;
}
case 0x2000:
{
2018-12-01 20:01:59 +00:00
int applicationType = cap >> 14;
2018-12-01 20:01:59 +00:00
if ((uint)applicationType > 7)
{
return KernelResult.ReservedValue;
}
2018-12-01 20:24:37 +00:00
ApplicationType = applicationType;
break;
}
case 0x4000:
{
//Note: This check is bugged on kernel too, we are just replicating the bug here.
2018-12-01 20:01:59 +00:00
if ((KernelReleaseVersion >> 17) != 0 || cap < 0x80000)
{
return KernelResult.ReservedValue;
}
2018-12-01 20:01:59 +00:00
KernelReleaseVersion = cap;
break;
}
case 0x8000:
{
2018-12-01 20:01:59 +00:00
int handleTableSize = cap >> 26;
2018-12-01 20:01:59 +00:00
if ((uint)handleTableSize > 0x3ff)
{
return KernelResult.ReservedValue;
}
2018-12-01 20:24:37 +00:00
HandleTableSize = handleTableSize;
break;
}
case 0x10000:
{
2018-12-01 20:01:59 +00:00
int debuggingFlags = cap >> 19;
2018-12-01 20:01:59 +00:00
if ((uint)debuggingFlags > 3)
{
return KernelResult.ReservedValue;
}
2018-12-01 20:24:37 +00:00
DebuggingFlags &= ~3;
DebuggingFlags |= debuggingFlags;
break;
}
default: return KernelResult.InvalidCapability;
}
return KernelResult.Success;
}
2018-12-01 20:01:59 +00:00
private static long GetMaskFromMinMax(int min, int max)
{
2018-12-01 20:01:59 +00:00
int range = max - min + 1;
2018-12-01 20:01:59 +00:00
long mask = (1L << range) - 1;
2018-12-01 20:01:59 +00:00
return mask << min;
}
}
}