2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
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-04 19:52:39 -05:00
|
|
|
public KernelResult InitializeForKernel(int[] Caps, KMemoryManager MemoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
AllowedCpuCoresMask = 0xf;
|
|
|
|
AllowedThreadPriosMask = -1;
|
|
|
|
DebuggingFlags &= ~3;
|
|
|
|
KernelReleaseVersion = KProcess.KernelVersionPacked;
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Parse(Caps, MemoryManager);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public KernelResult InitializeForUser(int[] Caps, KMemoryManager MemoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Parse(Caps, MemoryManager);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult Parse(int[] Caps, KMemoryManager MemoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Mask0 = 0;
|
|
|
|
int Mask1 = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (int Index = 0; Index < Caps.Length; Index++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Cap = Caps[Index];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (((Cap + 1) & ~Cap) != 0x40)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result = ParseCapability(Cap, ref Mask0, ref Mask1, MemoryManager);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((uint)Index + 1 >= Caps.Length)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int PrevCap = Cap;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Cap = Caps[++Index];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (((Cap + 1) & ~Cap) != 0x40)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((Cap & 0x78000000) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((Cap & 0x7ffff80) == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -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-04 19:52:39 -05:00
|
|
|
if (((ulong)(Address + Size - 1) >> 36) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidAddress;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
MemoryPermission Perm = (PrevCap >> 31) != 0
|
2018-11-28 17:18:09 -05:00
|
|
|
? MemoryPermission.Read
|
|
|
|
: MemoryPermission.ReadAndWrite;
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelResult Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((Cap >> 31) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = MemoryManager.MapNormalMemory(Address, Size, Perm);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Result = MemoryManager.MapIoMemory(Address, Size, Perm);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private KernelResult ParseCapability(int Cap, ref int Mask0, ref int Mask1, KMemoryManager MemoryManager)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Code = (Cap + 1) & ~Cap;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Code == 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCapability;
|
|
|
|
}
|
2018-12-04 19:52:39 -05:00
|
|
|
else if (Code == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int CodeMask = 1 << (32 - BitUtils.CountLeadingZeros32(Code + 1));
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
//Check if the property was already set.
|
2018-12-04 19:52:39 -05:00
|
|
|
if (((Mask0 & CodeMask) & 0x1e008) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Mask0 |= CodeMask;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
switch (Code)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
if (AllowedCpuCoresMask != 0 || AllowedThreadPriosMask != 0)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidCapability;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int LowestCpuCore = (Cap >> 16) & 0xff;
|
|
|
|
int HighestCpuCore = (Cap >> 24) & 0xff;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (LowestCpuCore > HighestCpuCore)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int HighestThreadPrio = (Cap >> 4) & 0x3f;
|
|
|
|
int LowestThreadPrio = (Cap >> 10) & 0x3f;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (LowestThreadPrio > HighestThreadPrio)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (HighestCpuCore >= KScheduler.CpuCoresCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCpuCore;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
AllowedCpuCoresMask = GetMaskFromMinMax(LowestCpuCore, HighestCpuCore);
|
|
|
|
AllowedThreadPriosMask = GetMaskFromMinMax(LowestThreadPrio, HighestThreadPrio);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x10:
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Slot = (Cap >> 29) & 7;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int SvcSlotMask = 1 << Slot;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((Mask1 & SvcSlotMask) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Mask1 |= SvcSlotMask;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int SvcMask = (Cap >> 5) & 0xffffff;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int BaseSvc = Slot * 24;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (int Index = 0; Index < 24; Index++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (((SvcMask >> Index) & 1) == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int SvcId = BaseSvc + Index;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (SvcId > 0x7f)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.MaximumExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
SvcAccessMask[SvcId / 8] |= (byte)(1 << (SvcId & 7));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x80:
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long Address = ((long)(uint)Cap << 4) & 0xffffff000;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
MemoryManager.MapIoMemory(Address, KMemoryManager.PageSize, MemoryPermission.ReadAndWrite);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x800:
|
|
|
|
{
|
|
|
|
//TODO: GIC distributor check.
|
2018-12-04 19:52:39 -05:00
|
|
|
int Irq0 = (Cap >> 12) & 0x3ff;
|
|
|
|
int Irq1 = (Cap >> 22) & 0x3ff;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Irq0 != 0x3ff)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
IrqAccessMask[Irq0 / 8] |= (byte)(1 << (Irq0 & 7));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Irq1 != 0x3ff)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
IrqAccessMask[Irq1 / 8] |= (byte)(1 << (Irq1 & 7));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x2000:
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int ApplicationType = Cap >> 14;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((uint)ApplicationType > 7)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
this.ApplicationType = ApplicationType;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x4000:
|
|
|
|
{
|
|
|
|
//Note: This check is bugged on kernel too, we are just replicating the bug here.
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((KernelReleaseVersion >> 17) != 0 || Cap < 0x80000)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
KernelReleaseVersion = Cap;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x8000:
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int HandleTableSize = Cap >> 26;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((uint)HandleTableSize > 0x3ff)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
this.HandleTableSize = HandleTableSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x10000:
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int DebuggingFlags = Cap >> 19;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((uint)DebuggingFlags > 3)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ReservedValue;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
this.DebuggingFlags &= ~3;
|
|
|
|
this.DebuggingFlags |= DebuggingFlags;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: return KernelResult.InvalidCapability;
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private static long GetMaskFromMinMax(int Min, int Max)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int Range = Max - Min + 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
long Mask = (1L << Range) - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Mask << Min;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|