Better process implementation (#491)

* Initial implementation of KProcess

* Some improvements to the memory manager, implement back guest stack trace printing

* Better GetInfo implementation, improve checking in some places with information from process capabilities

* Allow the cpu to read/write from the correct memory locations for accesses crossing a page boundary

* Change long -> ulong for address/size on memory related methods to avoid unnecessary casts

* Attempt at implementing ldr:ro with new KProcess

* Allow BSS with size 0 on ldr:ro

* Add checking for memory block slab heap usage, return errors if full, exit gracefully

* Use KMemoryBlockSize const from KMemoryManager

* Allow all methods to read from non-contiguous locations

* Fix for TransactParcelAuto

* Address PR feedback, additionally fix some small issues related to the KIP loader and implement SVCs GetProcessId, GetProcessList, GetSystemInfo, CreatePort and ManageNamedPort

* Fix wrong check for source pages count from page list on MapPhysicalMemory

* Fix some issues with UnloadNro on ldr:ro
This commit is contained in:
gdkchan 2018-11-28 20:18:09 -02:00 committed by GitHub
parent e7fe7d7247
commit 00579927e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
119 changed files with 7998 additions and 3232 deletions

View file

@ -1,9 +0,0 @@
namespace Ryujinx.HLE.Loaders.Npdm
{
enum ApplicationType
{
SystemModule,
Application,
Applet
}
}

View file

@ -1,33 +0,0 @@
namespace Ryujinx.HLE.Loaders.Npdm
{
enum FsPermissionBool : ulong
{
BisCache = 0x8000000000000080,
EraseMmc = 0x8000000000000080,
GameCardCertificate = 0x8000000000000010,
GameCardIdSet = 0x8000000000000010,
GameCardDriver = 0x8000000000000200,
GameCardAsic = 0x8000000000000200,
SaveDataCreate = 0x8000000000002020,
SaveDataDelete0 = 0x8000000000000060,
SystemSaveDataCreate0 = 0x8000000000000028,
SystemSaveDataCreate1 = 0x8000000000000020,
SaveDataDelete1 = 0x8000000000004028,
SaveDataIterators0 = 0x8000000000000060,
SaveDataIterators1 = 0x8000000000004020,
SaveThumbnails = 0x8000000000020000,
PosixTime = 0x8000000000000400,
SaveDataExtraData = 0x8000000000004060,
GlobalMode = 0x8000000000080000,
SpeedEmulation = 0x8000000000080000,
NULL = 0,
PaddingFiles = 0xC000000000800000,
SaveData_Debug = 0xC000000001000000,
SaveData_SystemManagement = 0xC000000002000000,
Unknown0x16 = 0x8000000004000000,
Unknown0x17 = 0x8000000008000000,
Unknown0x18 = 0x8000000010000000,
Unknown0x19 = 0x8000000000000800,
Unknown0x1A = 0x8000000000004020
}
}

View file

@ -1,45 +0,0 @@
namespace Ryujinx.HLE.Loaders.Npdm
{
enum FsPermissionRw : ulong
{
MountContentType2 = 0x8000000000000801,
MountContentType5 = 0x8000000000000801,
MountContentType3 = 0x8000000000000801,
MountContentType4 = 0x8000000000000801,
MountContentType6 = 0x8000000000000801,
MountContentType7 = 0x8000000000000801,
Unknown0x6 = 0x8000000000000000,
ContentStorageAccess = 0x8000000000000800,
ImageDirectoryAccess = 0x8000000000001000,
MountBisType28 = 0x8000000000000084,
MountBisType29 = 0x8000000000000080,
MountBisType30 = 0x8000000000008080,
MountBisType31 = 0x8000000000008080,
Unknown0xD = 0x8000000000000080,
SdCardAccess = 0xC000000000200000,
GameCardUser = 0x8000000000000010,
SaveDataAccess0 = 0x8000000000040020,
SystemSaveDataAccess0 = 0x8000000000000028,
SaveDataAccess1 = 0x8000000000000020,
SystemSaveDataAccess1 = 0x8000000000000020,
BisPartition0 = 0x8000000000010082,
BisPartition10 = 0x8000000000010080,
BisPartition20 = 0x8000000000010080,
BisPartition21 = 0x8000000000010080,
BisPartition22 = 0x8000000000010080,
BisPartition23 = 0x8000000000010080,
BisPartition24 = 0x8000000000010080,
BisPartition25 = 0x8000000000010080,
BisPartition26 = 0x8000000000000080,
BisPartition27 = 0x8000000000000084,
BisPartition28 = 0x8000000000000084,
BisPartition29 = 0x8000000000000080,
BisPartition30 = 0x8000000000000080,
BisPartition31 = 0x8000000000000080,
BisPartition32 = 0x8000000000000080,
Unknown0x23 = 0xC000000000200000,
GameCard_System = 0x8000000000000100,
MountContent_System = 0x8000000000100008,
HostAccess = 0xC000000000400000
}
}

View file

@ -1,173 +1,23 @@
using Ryujinx.HLE.Exceptions;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.IO;
namespace Ryujinx.HLE.Loaders.Npdm
{
class KernelAccessControl
{
public ReadOnlyCollection<KernelAccessControlItem> Items;
public int[] Capabilities { get; private set; }
public KernelAccessControl(Stream Stream, int Offset, int Size)
{
Stream.Seek(Offset, SeekOrigin.Begin);
Capabilities = new int[Size / 4];
BinaryReader Reader = new BinaryReader(Stream);
KernelAccessControlItem[] Items = new KernelAccessControlItem[Size / 4];
for (int Index = 0; Index < Size / 4; Index++)
for (int Index = 0; Index < Capabilities.Length; Index++)
{
uint Descriptor = Reader.ReadUInt32();
//Ignore the descriptor.
if (Descriptor == 0xffffffff)
{
continue;
}
Items[Index] = new KernelAccessControlItem();
int LowBits = 0;
while ((Descriptor & 1) != 0)
{
Descriptor >>= 1;
LowBits++;
}
Descriptor >>= 1;
switch (LowBits)
{
//Kernel flags.
case 3:
{
Items[Index].HasKernelFlags = true;
Items[Index].HighestThreadPriority = (Descriptor >> 0) & 0x3f;
Items[Index].LowestThreadPriority = (Descriptor >> 6) & 0x3f;
Items[Index].LowestCpuId = (Descriptor >> 12) & 0xff;
Items[Index].HighestCpuId = (Descriptor >> 20) & 0xff;
break;
}
//Syscall mask.
case 4:
{
Items[Index].HasSvcFlags = true;
Items[Index].AllowedSvcs = new bool[0x80];
int SysCallBase = (int)(Descriptor >> 24) * 0x18;
for (int SysCall = 0; SysCall < 0x18 && SysCallBase + SysCall < 0x80; SysCall++)
{
Items[Index].AllowedSvcs[SysCallBase + SysCall] = (Descriptor & 1) != 0;
Descriptor >>= 1;
}
break;
}
//Map IO/Normal.
case 6:
{
ulong Address = (Descriptor & 0xffffff) << 12;
bool IsRo = (Descriptor >> 24) != 0;
if (Index == Size / 4 - 1)
{
throw new InvalidNpdmException("Invalid Kernel Access Control Descriptors!");
}
Descriptor = Reader.ReadUInt32();
if ((Descriptor & 0x7f) != 0x3f)
{
throw new InvalidNpdmException("Invalid Kernel Access Control Descriptors!");
}
Descriptor >>= 7;
ulong MmioSize = (Descriptor & 0xffffff) << 12;
bool IsNormal = (Descriptor >> 24) != 0;
Items[Index].NormalMmio.Add(new KernelAccessControlMmio(Address, MmioSize, IsRo, IsNormal));
Index++;
break;
}
//Map Normal Page.
case 7:
{
ulong Address = Descriptor << 12;
Items[Index].PageMmio.Add(new KernelAccessControlMmio(Address, 0x1000, false, false));
break;
}
//IRQ Pair.
case 11:
{
Items[Index].Irq.Add(new KernelAccessControlIrq(
(Descriptor >> 0) & 0x3ff,
(Descriptor >> 10) & 0x3ff));
break;
}
//Application Type.
case 13:
{
Items[Index].HasApplicationType = true;
Items[Index].ApplicationType = (int)Descriptor & 7;
break;
}
//Kernel Release Version.
case 14:
{
Items[Index].HasKernelVersion = true;
Items[Index].KernelVersionRelease = (int)Descriptor;
break;
}
//Handle Table Size.
case 15:
{
Items[Index].HasHandleTableSize = true;
Items[Index].HandleTableSize = (int)Descriptor;
break;
}
//Debug Flags.
case 16:
{
Items[Index].HasDebugFlags = true;
Items[Index].AllowDebug = ((Descriptor >> 0) & 1) != 0;
Items[Index].ForceDebug = ((Descriptor >> 1) & 1) != 0;
break;
}
}
Capabilities[Index] = Reader.ReadInt32();
}
this.Items = Array.AsReadOnly(Items);
}
}
}

View file

@ -1,14 +0,0 @@
namespace Ryujinx.HLE.Loaders.Npdm
{
struct KernelAccessControlIrq
{
public uint Irq0 { get; private set; }
public uint Irq1 { get; private set; }
public KernelAccessControlIrq(uint Irq0, uint Irq1)
{
this.Irq0 = Irq0;
this.Irq1 = Irq1;
}
}
}

View file

@ -1,22 +0,0 @@
namespace Ryujinx.HLE.Loaders.Npdm
{
struct KernelAccessControlMmio
{
public ulong Address { get; private set; }
public ulong Size { get; private set; }
public bool IsRo { get; private set; }
public bool IsNormal { get; private set; }
public KernelAccessControlMmio(
ulong Address,
ulong Size,
bool IsRo,
bool IsNormal)
{
this.Address = Address;
this.Size = Size;
this.IsRo = IsRo;
this.IsNormal = IsNormal;
}
}
}

View file

@ -1,33 +0,0 @@
using System.Collections.Generic;
namespace Ryujinx.HLE.Loaders.Npdm
{
struct KernelAccessControlItem
{
public bool HasKernelFlags { get; set; }
public uint LowestThreadPriority { get; set; }
public uint HighestThreadPriority { get; set; }
public uint LowestCpuId { get; set; }
public uint HighestCpuId { get; set; }
public bool HasSvcFlags { get; set; }
public bool[] AllowedSvcs { get; set; }
public List<KernelAccessControlMmio> NormalMmio { get; set; }
public List<KernelAccessControlMmio> PageMmio { get; set; }
public List<KernelAccessControlIrq> Irq { get; set; }
public bool HasApplicationType { get; set; }
public int ApplicationType { get; set; }
public bool HasKernelVersion { get; set; }
public int KernelVersionRelease { get; set; }
public bool HasHandleTableSize { get; set; }
public int HandleTableSize { get; set; }
public bool HasDebugFlags { get; set; }
public bool AllowDebug { get; set; }
public bool ForceDebug { get; set; }
}
}

View file

@ -1,5 +1,4 @@
using Ryujinx.HLE.Exceptions;
using Ryujinx.HLE.Utilities;
using System.IO;
using System.Text;
@ -12,15 +11,15 @@ namespace Ryujinx.HLE.Loaders.Npdm
{
private const int MetaMagic = 'M' << 0 | 'E' << 8 | 'T' << 16 | 'A' << 24;
public bool Is64Bits { get; private set; }
public int AddressSpaceWidth { get; private set; }
public byte MainThreadPriority { get; private set; }
public byte DefaultCpuId { get; private set; }
public int SystemResourceSize { get; private set; }
public int ProcessCategory { get; private set; }
public int MainEntrypointStackSize { get; private set; }
public string TitleName { get; private set; }
public byte[] ProductCode { get; private set; }
public byte MmuFlags { get; private set; }
public bool Is64Bits { get; private set; }
public byte MainThreadPriority { get; private set; }
public byte DefaultCpuId { get; private set; }
public int PersonalMmHeapSize { get; private set; }
public int ProcessCategory { get; private set; }
public int MainThreadStackSize { get; private set; }
public string TitleName { get; private set; }
public byte[] ProductCode { get; private set; }
public ACI0 ACI0 { get; private set; }
public ACID ACID { get; private set; }
@ -36,27 +35,22 @@ namespace Ryujinx.HLE.Loaders.Npdm
Reader.ReadInt64();
//MmuFlags, bit0: 64-bit instructions, bits1-3: address space width (1=64-bit, 2=32-bit). Needs to be <= 0xF.
byte MmuFlags = Reader.ReadByte();
MmuFlags = Reader.ReadByte();
Is64Bits = (MmuFlags & 1) != 0;
AddressSpaceWidth = (MmuFlags >> 1) & 7;
Is64Bits = (MmuFlags & 1) != 0;
Reader.ReadByte();
MainThreadPriority = Reader.ReadByte(); //(0-63).
MainThreadPriority = Reader.ReadByte();
DefaultCpuId = Reader.ReadByte();
Reader.ReadInt32();
//System resource size (max size as of 5.x: 534773760).
SystemResourceSize = EndianSwap.Swap32(Reader.ReadInt32());
PersonalMmHeapSize = Reader.ReadInt32();
//ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here.
ProcessCategory = EndianSwap.Swap32(Reader.ReadInt32());
ProcessCategory = Reader.ReadInt32();
//Main entrypoint stack size.
MainEntrypointStackSize = Reader.ReadInt32();
MainThreadStackSize = Reader.ReadInt32();
byte[] TempTitleName = Reader.ReadBytes(0x10);

View file

@ -28,8 +28,8 @@ namespace Ryujinx.HLE.Loaders.Npdm
break;
}
int Length = ((ControlByte & 0x07)) + 1;
bool RegisterAllowed = ((ControlByte & 0x80) != 0);
int Length = (ControlByte & 0x07) + 1;
bool RegisterAllowed = (ControlByte & 0x80) != 0;
Services.Add(Encoding.ASCII.GetString(Reader.ReadBytes(Length), 0, Length), RegisterAllowed);

View file

@ -1,134 +0,0 @@
namespace Ryujinx.HLE.Loaders.Npdm
{
enum SvcName
{
Reserved0,
SetHeapSize,
SetMemoryPermission,
SetMemoryAttribute,
MapMemory,
UnmapMemory,
QueryMemory,
ExitProcess,
CreateThread,
StartThread,
ExitThread,
SleepThread,
GetThreadPriority,
SetThreadPriority,
GetThreadCoreMask,
SetThreadCoreMask,
GetCurrentProcessorNumber,
SignalEvent,
ClearEvent,
MapSharedMemory,
UnmapSharedMemory,
CreateTransferMemory,
CloseHandle,
ResetSignal,
WaitSynchronization,
CancelSynchronization,
ArbitrateLock,
ArbitrateUnlock,
WaitProcessWideKeyAtomic,
SignalProcessWideKey,
GetSystemTick,
ConnectToNamedPort,
SendSyncRequestLight,
SendSyncRequest,
SendSyncRequestWithUserBuffer,
SendAsyncRequestWithUserBuffer,
GetProcessId,
GetThreadId,
Break,
OutputDebugString,
ReturnFromException,
GetInfo,
FlushEntireDataCache,
FlushDataCache,
MapPhysicalMemory,
UnmapPhysicalMemory,
GetFutureThreadInfo,
GetLastThreadInfo,
GetResourceLimitLimitValue,
GetResourceLimitCurrentValue,
SetThreadActivity,
GetThreadContext3,
WaitForAddress,
SignalToAddress,
Reserved1,
Reserved2,
Reserved3,
Reserved4,
Reserved5,
Reserved6,
DumpInfo,
DumpInfoNew,
Reserved7,
Reserved8,
CreateSession,
AcceptSession,
ReplyAndReceiveLight,
ReplyAndReceive,
ReplyAndReceiveWithUserBuffer,
CreateEvent,
Reserved9,
Reserved10,
MapPhysicalMemoryUnsafe,
UnmapPhysicalMemoryUnsafe,
SetUnsafeLimit,
CreateCodeMemory,
ControlCodeMemory,
SleepSystem,
ReadWriteRegister,
SetProcessActivity,
CreateSharedMemory,
MapTransferMemory,
UnmapTransferMemory,
CreateInterruptEvent,
QueryPhysicalAddress,
QueryIoMapping,
CreateDeviceAddressSpace,
AttachDeviceAddressSpace,
DetachDeviceAddressSpace,
MapDeviceAddressSpaceByForce,
MapDeviceAddressSpaceAligned,
MapDeviceAddressSpace,
UnmapDeviceAddressSpace,
InvalidateProcessDataCache,
StoreProcessDataCache,
FlushProcessDataCache,
DebugActiveProcess,
BreakDebugProcess,
TerminateDebugProcess,
GetDebugEvent,
ContinueDebugEvent,
GetProcessList,
GetThreadList,
GetDebugThreadContext,
SetDebugThreadContext,
QueryDebugProcessMemory,
ReadDebugProcessMemory,
WriteDebugProcessMemory,
SetHardwareBreakPoint,
GetDebugThreadParam,
Reserved11,
GetSystemInfo,
CreatePort,
ManageNamedPort,
ConnectToPort,
SetProcessMemoryPermission,
MapProcessMemory,
UnmapProcessMemory,
QueryProcessMemory,
MapProcessCodeMemory,
UnmapProcessCodeMemory,
CreateProcess,
StartProcess,
TerminateProcess,
GetProcessInfo,
CreateResourceLimit,
SetResourceLimitLimitValue,
CallSecureMonitor
}
}