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