2018-11-28 23:18:09 +01:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
static class KernelInit
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
public static void InitializeResourceLimit(KResourceLimit ResourceLimit)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
void EnsureSuccess(KernelResult Result)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
throw new InvalidOperationException($"Unexpected result \"{Result}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int KernelMemoryCfg = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
long RamSize = GetRamSize(KernelMemoryCfg);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01: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-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!ResourceLimit.Reserve(LimitableResource.Memory, 0) ||
|
|
|
|
!ResourceLimit.Reserve(LimitableResource.Memory, 0x60000))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure reserving memory on resource limit.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static KMemoryRegionManager[] GetMemoryRegions()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
KMemoryArrange Arrange = GetMemoryArrange();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return new KMemoryRegionManager[]
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
GetMemoryRegion(Arrange.Application),
|
|
|
|
GetMemoryRegion(Arrange.Applet),
|
|
|
|
GetMemoryRegion(Arrange.Service),
|
|
|
|
GetMemoryRegion(Arrange.NvServices)
|
2018-11-28 23:18:09 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private static KMemoryRegionManager GetMemoryRegion(KMemoryArrangeRegion Region)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return new KMemoryRegionManager(Region.Address, Region.Size, Region.EndAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static KMemoryArrange GetMemoryArrange()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int McEmemCfg = 0x1000;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong EmemApertureSize = (ulong)(McEmemCfg & 0x3fff) << 20;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int KernelMemoryCfg = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong RamSize = (ulong)GetRamSize(KernelMemoryCfg);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong RamPart0;
|
|
|
|
ulong RamPart1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (RamSize * 2 > EmemApertureSize)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
RamPart0 = EmemApertureSize / 2;
|
|
|
|
RamPart1 = EmemApertureSize / 2;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
RamPart0 = EmemApertureSize;
|
|
|
|
RamPart1 = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int MemoryArrange = 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong ApplicationRgSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
switch (MemoryArrange)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
case 2: ApplicationRgSize = 0x80000000; break;
|
2018-11-28 23:18:09 +01:00
|
|
|
case 0x11:
|
2018-12-05 01:52:39 +01:00
|
|
|
case 0x21: ApplicationRgSize = 0x133400000; break;
|
|
|
|
default: ApplicationRgSize = 0xcd500000; break;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong AppletRgSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
switch (MemoryArrange)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
case 2: AppletRgSize = 0x61200000; break;
|
|
|
|
case 3: AppletRgSize = 0x1c000000; break;
|
|
|
|
case 0x11: AppletRgSize = 0x23200000; break;
|
2018-11-28 23:18:09 +01:00
|
|
|
case 0x12:
|
2018-12-05 01:52:39 +01:00
|
|
|
case 0x21: AppletRgSize = 0x89100000; break;
|
|
|
|
default: AppletRgSize = 0x1fb00000; break;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KMemoryArrangeRegion ServiceRg;
|
|
|
|
KMemoryArrangeRegion NvServicesRg;
|
|
|
|
KMemoryArrangeRegion AppletRg;
|
|
|
|
KMemoryArrangeRegion ApplicationRg;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
const ulong NvServicesRgSize = 0x29ba000;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong ApplicationRgEnd = DramMemoryMap.DramEnd; //- RamPart0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ApplicationRg = new KMemoryArrangeRegion(ApplicationRgEnd - ApplicationRgSize, ApplicationRgSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong NvServicesRgEnd = ApplicationRg.Address - AppletRgSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
NvServicesRg = new KMemoryArrangeRegion(NvServicesRgEnd - NvServicesRgSize, NvServicesRgSize);
|
|
|
|
AppletRg = new KMemoryArrangeRegion(NvServicesRgEnd, AppletRgSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//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-05 01:52:39 +01:00
|
|
|
ulong ServiceRgSize = NvServicesRg.Address - DramMemoryMap.SlabHeapEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ServiceRg = new KMemoryArrangeRegion(DramMemoryMap.SlabHeapEnd, ServiceRgSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return new KMemoryArrange(ServiceRg, NvServicesRg, AppletRg, ApplicationRg);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private static long GetRamSize(int KernelMemoryCfg)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
switch ((KernelMemoryCfg >> 16) & 3)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
case 1: return 0x180000000;
|
|
|
|
case 2: return 0x200000000;
|
|
|
|
default: return 0x100000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|