1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-11 07:42:01 +00:00
Ryujinx/Ryujinx.HLE/HOS/Kernel/KernelInit.cs

136 lines
4.7 KiB
C#
Raw Normal View History

using System;
namespace Ryujinx.HLE.HOS.Kernel
{
static class KernelInit
{
2018-12-01 20:01:59 +00:00
public static void InitializeResourceLimit(KResourceLimit resourceLimit)
{
2018-12-01 20:01:59 +00:00
void EnsureSuccess(KernelResult result)
{
2018-12-01 20:01:59 +00:00
if (result != KernelResult.Success)
{
2018-12-01 20:01:59 +00:00
throw new InvalidOperationException($"Unexpected result \"{result}\".");
}
}
2018-12-01 20:01:59 +00:00
int kernelMemoryCfg = 0;
2018-12-01 20:01:59 +00:00
long ramSize = GetRamSize(kernelMemoryCfg);
2018-12-01 20:01:59 +00:00
EnsureSuccess(resourceLimit.SetLimitValue(LimitableResource.Memory, ramSize));
EnsureSuccess(resourceLimit.SetLimitValue(LimitableResource.Thread, 800));
EnsureSuccess(resourceLimit.SetLimitValue(LimitableResource.Event, 700));
EnsureSuccess(resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 200));
EnsureSuccess(resourceLimit.SetLimitValue(LimitableResource.Session, 900));
2018-12-01 20:01:59 +00:00
if (!resourceLimit.Reserve(LimitableResource.Memory, 0) ||
!resourceLimit.Reserve(LimitableResource.Memory, 0x60000))
{
throw new InvalidOperationException("Unexpected failure reserving memory on resource limit.");
}
}
public static KMemoryRegionManager[] GetMemoryRegions()
{
2018-12-01 20:01:59 +00:00
KMemoryArrange arrange = GetMemoryArrange();
return new KMemoryRegionManager[]
{
2018-12-01 20:01:59 +00:00
GetMemoryRegion(arrange.Application),
GetMemoryRegion(arrange.Applet),
GetMemoryRegion(arrange.Service),
GetMemoryRegion(arrange.NvServices)
};
}
2018-12-01 20:01:59 +00:00
private static KMemoryRegionManager GetMemoryRegion(KMemoryArrangeRegion region)
{
2018-12-01 20:01:59 +00:00
return new KMemoryRegionManager(region.Address, region.Size, region.EndAddr);
}
private static KMemoryArrange GetMemoryArrange()
{
2018-12-01 20:01:59 +00:00
int mcEmemCfg = 0x1000;
2018-12-01 20:01:59 +00:00
ulong ememApertureSize = (ulong)(mcEmemCfg & 0x3fff) << 20;
2018-12-01 20:01:59 +00:00
int kernelMemoryCfg = 0;
2018-12-01 20:01:59 +00:00
ulong ramSize = (ulong)GetRamSize(kernelMemoryCfg);
2018-12-01 20:01:59 +00:00
ulong ramPart0;
ulong ramPart1;
2018-12-01 20:01:59 +00:00
if (ramSize * 2 > ememApertureSize)
{
2018-12-01 20:01:59 +00:00
ramPart0 = ememApertureSize / 2;
ramPart1 = ememApertureSize / 2;
}
else
{
2018-12-01 20:01:59 +00:00
ramPart0 = ememApertureSize;
ramPart1 = 0;
}
2018-12-01 20:01:59 +00:00
int memoryArrange = 1;
2018-12-01 20:01:59 +00:00
ulong applicationRgSize;
2018-12-01 20:01:59 +00:00
switch (memoryArrange)
{
2018-12-01 20:01:59 +00:00
case 2: applicationRgSize = 0x80000000; break;
case 0x11:
2018-12-01 20:01:59 +00:00
case 0x21: applicationRgSize = 0x133400000; break;
default: applicationRgSize = 0xcd500000; break;
}
2018-12-01 20:01:59 +00:00
ulong appletRgSize;
2018-12-01 20:01:59 +00:00
switch (memoryArrange)
{
2018-12-01 20:01:59 +00:00
case 2: appletRgSize = 0x61200000; break;
case 3: appletRgSize = 0x1c000000; break;
case 0x11: appletRgSize = 0x23200000; break;
case 0x12:
2018-12-01 20:01:59 +00:00
case 0x21: appletRgSize = 0x89100000; break;
default: appletRgSize = 0x1fb00000; break;
}
2018-12-01 20:01:59 +00:00
KMemoryArrangeRegion serviceRg;
KMemoryArrangeRegion nvServicesRg;
KMemoryArrangeRegion appletRg;
KMemoryArrangeRegion applicationRg;
2018-12-01 20:01:59 +00:00
const ulong nvServicesRgSize = 0x29ba000;
2018-12-01 20:01:59 +00:00
ulong applicationRgEnd = DramMemoryMap.DramEnd; //- RamPart0;
2018-12-01 20:01:59 +00:00
applicationRg = new KMemoryArrangeRegion(applicationRgEnd - applicationRgSize, applicationRgSize);
2018-12-01 20:01:59 +00:00
ulong nvServicesRgEnd = applicationRg.Address - appletRgSize;
2018-12-01 20:01:59 +00:00
nvServicesRg = new KMemoryArrangeRegion(nvServicesRgEnd - nvServicesRgSize, nvServicesRgSize);
appletRg = new KMemoryArrangeRegion(nvServicesRgEnd, appletRgSize);
//Note: There is an extra region used by the kernel, however
//since we are doing HLE we are not going to use that memory, so give all
//the remaining memory space to services.
2018-12-01 20:01:59 +00:00
ulong serviceRgSize = nvServicesRg.Address - DramMemoryMap.SlabHeapEnd;
2018-12-01 20:01:59 +00:00
serviceRg = new KMemoryArrangeRegion(DramMemoryMap.SlabHeapEnd, serviceRgSize);
2018-12-01 20:01:59 +00:00
return new KMemoryArrange(serviceRg, nvServicesRg, appletRg, applicationRg);
}
2018-12-01 20:01:59 +00:00
private static long GetRamSize(int kernelMemoryCfg)
{
2018-12-01 20:01:59 +00:00
switch ((kernelMemoryCfg >> 16) & 3)
{
case 1: return 0x180000000;
case 2: return 0x200000000;
default: return 0x100000000;
}
}
}
}