2018-11-28 23:18:09 +01:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Logging;
|
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.Process;
|
2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
|
|
using Ryujinx.HLE.Loaders.Npdm;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS
|
|
|
|
{
|
|
|
|
class ProgramLoader
|
|
|
|
{
|
|
|
|
private const bool AslrEnabled = true;
|
|
|
|
|
|
|
|
private const int ArgsHeaderSize = 8;
|
|
|
|
private const int ArgsDataSize = 0x9000;
|
|
|
|
private const int ArgsTotalSize = ArgsHeaderSize + ArgsDataSize;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public static bool LoadKernelInitalProcess(Horizon system, KernelInitialProcess kip)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int endOffset = kip.DataOffset + kip.Data.Length;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (kip.BssSize != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
endOffset = kip.BssOffset + kip.BssSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int codeSize = BitUtils.AlignUp(kip.TextOffset + endOffset, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int codePagesCount = codeSize / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong codeBaseAddress = kip.Addr39Bits ? 0x8000000UL : 0x200000UL;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong codeAddress = codeBaseAddress + (ulong)kip.TextOffset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int mmuFlags = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
if (AslrEnabled)
|
|
|
|
{
|
|
|
|
//TODO: Randomization.
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
mmuFlags |= 0x20;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (kip.Addr39Bits)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
mmuFlags |= (int)AddressSpaceType.Addr39Bits << 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (kip.Is64Bits)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
mmuFlags |= 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
|
|
|
|
kip.Name,
|
|
|
|
kip.ProcessCategory,
|
|
|
|
kip.TitleId,
|
|
|
|
codeAddress,
|
|
|
|
codePagesCount,
|
|
|
|
mmuFlags,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
|
|
|
0);
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryRegion memoryRegion = kip.IsService
|
2018-11-28 23:18:09 +01:00
|
|
|
? MemoryRegion.Service
|
|
|
|
: MemoryRegion.Application;
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
KMemoryRegionManager region = system.MemoryRegions[(int)memoryRegion];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = region.AllocatePages((ulong)codePagesCount, false, out KPageList pageList);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KProcess process = new KProcess(system);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = process.InitializeKip(
|
|
|
|
creationInfo,
|
|
|
|
kip.Capabilities,
|
|
|
|
pageList,
|
|
|
|
system.ResourceLimit,
|
2019-01-18 23:26:39 +01:00
|
|
|
memoryRegion);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = LoadIntoMemory(process, kip, codeBaseAddress);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
process.DefaultCpuCore = kip.DefaultProcessorId;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = process.Start(kip.MainThreadPriority, (ulong)kip.MainThreadStackSize);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
system.Processes.Add(process.Pid, process);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool LoadStaticObjects(
|
2018-12-06 12:16:24 +01:00
|
|
|
Horizon system,
|
|
|
|
Npdm metaData,
|
|
|
|
IExecutable[] staticObjects,
|
|
|
|
byte[] arguments = null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-25 02:59:53 +01:00
|
|
|
if (!metaData.Is64Bits)
|
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.Loader, "32-bits application detected!");
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong argsStart = 0;
|
|
|
|
int argsSize = 0;
|
2019-01-25 02:59:53 +01:00
|
|
|
ulong codeStart = metaData.Is64Bits ? 0x8000000UL : 0x200000UL;
|
2018-12-06 12:16:24 +01:00
|
|
|
int codeSize = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong[] nsoBase = new ulong[staticObjects.Length];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < staticObjects.Length; index++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
IExecutable staticObject = staticObjects[index];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int textEnd = staticObject.TextOffset + staticObject.Text.Length;
|
|
|
|
int roEnd = staticObject.RoOffset + staticObject.Ro.Length;
|
|
|
|
int dataEnd = staticObject.DataOffset + staticObject.Data.Length + staticObject.BssSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int nsoSize = textEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((uint)nsoSize < (uint)roEnd)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
nsoSize = roEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((uint)nsoSize < (uint)dataEnd)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
nsoSize = dataEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
nsoSize = BitUtils.AlignUp(nsoSize, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
nsoBase[index] = codeStart + (ulong)codeSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
codeSize += nsoSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (arguments != null && argsSize == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
argsStart = (ulong)codeSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
argsSize = BitUtils.AlignDown(arguments.Length * 2 + ArgsTotalSize - 1, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
codeSize += argsSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int codePagesCount = codeSize / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int personalMmHeapPagesCount = metaData.PersonalMmHeapSize / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
|
|
|
|
metaData.TitleName,
|
|
|
|
metaData.ProcessCategory,
|
|
|
|
metaData.Aci0.TitleId,
|
|
|
|
codeStart,
|
|
|
|
codePagesCount,
|
|
|
|
metaData.MmuFlags,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
2018-12-06 12:16:24 +01:00
|
|
|
personalMmHeapPagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
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
|
|
|
KResourceLimit resourceLimit = new KResourceLimit(system);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long applicationRgSize = (long)system.MemoryRegions[(int)MemoryRegion.Application].Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = resourceLimit.SetLimitValue(LimitableResource.Memory, applicationRgSize);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.Thread, 608);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.Event, 700);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 128);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.Session, 894);
|
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
|
|
|
{
|
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization failed setting resource limit values.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KProcess process = new KProcess(system);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryRegion memoryRegion = (MemoryRegion)((metaData.Acid.Flags >> 2) & 0xf);
|
|
|
|
|
|
|
|
if (memoryRegion > MemoryRegion.NvServices)
|
|
|
|
{
|
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization failed due to invalid ACID flags.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = process.Initialize(
|
|
|
|
creationInfo,
|
|
|
|
metaData.Aci0.KernelAccessControl.Capabilities,
|
|
|
|
resourceLimit,
|
2019-01-18 23:26:39 +01:00
|
|
|
memoryRegion);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < staticObjects.Length; index++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Logger.PrintInfo(LogClass.Loader, $"Loading image {index} at 0x{nsoBase[index]:x16}...");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = LoadIntoMemory(process, staticObjects[index], nsoBase[index]);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
process.DefaultCpuCore = metaData.DefaultCpuId;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = process.Start(metaData.MainThreadPriority, (ulong)metaData.MainThreadStackSize);
|
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
|
|
|
Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
system.Processes.Add(process.Pid, process);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static KernelResult LoadIntoMemory(KProcess process, IExecutable image, ulong baseAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong textStart = baseAddress + (ulong)image.TextOffset;
|
|
|
|
ulong roStart = baseAddress + (ulong)image.RoOffset;
|
|
|
|
ulong dataStart = baseAddress + (ulong)image.DataOffset;
|
|
|
|
ulong bssStart = baseAddress + (ulong)image.BssOffset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong end = dataStart + (ulong)image.Data.Length;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (image.BssSize != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
end = bssStart + (ulong)image.BssSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
process.CpuMemory.WriteBytes((long)textStart, image.Text);
|
|
|
|
process.CpuMemory.WriteBytes((long)roStart, image.Ro);
|
|
|
|
process.CpuMemory.WriteBytes((long)dataStart, image.Data);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, image.BssSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult SetProcessMemoryPermission(ulong address, ulong size, MemoryPermission permission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (size == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
size = BitUtils.AlignUp(size, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return process.MemoryManager.SetProcessMemoryPermission(address, size, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = SetProcessMemoryPermission(textStart, (ulong)image.Text.Length, MemoryPermission.ReadAndExecute);
|
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
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = SetProcessMemoryPermission(roStart, (ulong)image.Ro.Length, MemoryPermission.Read);
|
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
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return SetProcessMemoryPermission(dataStart, end - dataStart, MemoryPermission.ReadAndWrite);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|