From adda19ecb2e2d8de05ad5a8149ede5a23b23113d Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Mon, 18 May 2020 01:15:44 +0100 Subject: [PATCH] so bascially rosalina's image... was in BASE (hardcoded by kernel) while its heaps were in SYSTEM. Fix this; also put the kext where BASE was. --- arm9/source/patches.c | 7 +++++-- k11_extension/include/globals.h | 1 + k11_extension/include/kernel.h | 6 +++--- k11_extension/source/globals.c | 1 + k11_extension/source/main.c | 12 +++++++++--- k11_extension/source/svc/GetSystemInfo.c | 4 ++++ sysmodules/pm/source/luma.c | 17 +++++++++++++++-- sysmodules/pm/source/luma.h | 4 +++- sysmodules/pm/source/reslimit.c | 5 ++++- sysmodules/rosalina/rosalina.rsf | 2 +- sysmodules/rosalina/source/menus.c | 2 +- 11 files changed, 47 insertions(+), 14 deletions(-) diff --git a/arm9/source/patches.c b/arm9/source/patches.c index fbd8e56..f441eb3 100644 --- a/arm9/source/patches.c +++ b/arm9/source/patches.c @@ -110,6 +110,7 @@ u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32 struct KExtParameters { u32 basePA; + u32 stolenSystemMemRegionSize; void *originalHandlers[4]; u32 L1MMUTableAddrs[4]; @@ -136,8 +137,9 @@ u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32 static const u8 patternHook3_4[] = {0x00, 0x00, 0xA0, 0xE1, 0x03, 0xF0, 0x20, 0xE3, 0xFD, 0xFF, 0xFF, 0xEA}; //SGI0 setup code, etc. //Our kernel11 extension is initially loaded in VRAM - u32 kextTotalSize = *(u32 *)0x18000020 - K11EXT_VA; - u32 dstKextPA = (ISN3DS ? 0x2E000000 : 0x26C00000) - kextTotalSize; + //u32 kextTotalSize = *(u32 *)0x18000020 - K11EXT_VA; + u32 stolenSystemMemRegionSize = ISN3DS ? 0 : 0 << 10; // no need to steal any mem on N3DS. Currently, everything fits in BASE on O3DS too (?) + u32 dstKextPA = (ISN3DS ? 0x2E000000 : 0x26C00000) - stolenSystemMemRegionSize; // start of BASE memregion (note: linear heap ---> <--- the rest) u32 *hookVeneers = (u32 *)*freeK11Space; u32 relocBase = 0xFFFF0000 + (*freeK11Space - (u8 *)arm11ExceptionsPage); @@ -179,6 +181,7 @@ u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32 struct KExtParameters *p = (struct KExtParameters *)(*(u32 *)0x18000024 - K11EXT_VA + 0x18000000); p->basePA = dstKextPA; + p->stolenSystemMemRegionSize = stolenSystemMemRegionSize; for(u32 i = 0; i < 4; i++) { diff --git a/k11_extension/include/globals.h b/k11_extension/include/globals.h index c8cb4f2..2a320bf 100644 --- a/k11_extension/include/globals.h +++ b/k11_extension/include/globals.h @@ -133,6 +133,7 @@ typedef struct CfwInfo extern CfwInfo cfwInfo; extern u32 kextBasePa; +extern u32 stolenSystemMemRegionSize; extern vu32 rosalinaState; extern bool hasStartedRosalinaNetworkFuncsOnce; diff --git a/k11_extension/include/kernel.h b/k11_extension/include/kernel.h index 30da727..b1f58ca 100644 --- a/k11_extension/include/kernel.h +++ b/k11_extension/include/kernel.h @@ -1126,11 +1126,11 @@ typedef union KCacheMaintenanceInterruptEvent typedef struct FcramLayout { - void *applicationAddr; + u32 applicationAddr; u32 applicationSize; - void *systemAddr; + u32 systemAddr; u32 systemSize; - void *baseAddr; + u32 baseAddr; u32 baseSize; } FcramLayout; diff --git a/k11_extension/source/globals.c b/k11_extension/source/globals.c index 1d8f7fb..030e41e 100644 --- a/k11_extension/source/globals.c +++ b/k11_extension/source/globals.c @@ -109,6 +109,7 @@ void (*coreBarrier)(void); CfwInfo cfwInfo; u32 kextBasePa; +u32 stolenSystemMemRegionSize; vu32 rosalinaState; bool hasStartedRosalinaNetworkFuncsOnce; diff --git a/k11_extension/source/main.c b/k11_extension/source/main.c index c069188..4250f3f 100644 --- a/k11_extension/source/main.c +++ b/k11_extension/source/main.c @@ -37,6 +37,7 @@ struct KExtParameters { u32 basePA; + u32 stolenSystemMemRegionSize; void *originalHandlers[4]; u32 L1MMUTableAddrs[4]; @@ -261,7 +262,14 @@ void main(FcramLayout *layout, KCoreContext *ctxs) u32 TTBCR_; s64 nb; - layout->systemSize -= __end__ - __start__; + cfwInfo = p->cfwInfo; + kextBasePa = p->basePA; + stolenSystemMemRegionSize = p->stolenSystemMemRegionSize; + + u32 kextSize = (u32)(__end__ - __start__); + layout->systemSize -= stolenSystemMemRegionSize; + layout->baseAddr = layout->baseAddr - stolenSystemMemRegionSize + kextSize; + layout->baseSize = layout->baseSize + stolenSystemMemRegionSize - kextSize; fcramLayout = *layout; coreCtxs = ctxs; @@ -270,8 +278,6 @@ void main(FcramLayout *layout, KCoreContext *ctxs) isN3DS = getNumberOfCores() == 4; memcpy(L1MMUTableAddrs, (const void *)p->L1MMUTableAddrs, 16); exceptionStackTop = (u32 *)0xFFFF2000 + (1 << (32 - TTBCR - 20)); - cfwInfo = p->cfwInfo; - kextBasePa = p->basePA; memcpy(originalHandlers + 1, p->originalHandlers, 16); void **arm11SvcTable = (void**)originalHandlers[2]; diff --git a/k11_extension/source/svc/GetSystemInfo.c b/k11_extension/source/svc/GetSystemInfo.c index 4a5f88f..f06b662 100644 --- a/k11_extension/source/svc/GetSystemInfo.c +++ b/k11_extension/source/svc/GetSystemInfo.c @@ -82,6 +82,10 @@ Result GetSystemInfoHook(s64 *out, s32 type, s32 param) *out = (s64)(((u64)kextBasePa << 32) | (u64)(__end__ - __start__)); break; + case 0x301: // stolen SYSTEM memory size + *out = stolenSystemMemRegionSize; + break; + default: *out = 0; res = 0xF8C007F4; // not implemented diff --git a/sysmodules/pm/source/luma.c b/sysmodules/pm/source/luma.c index cd3f489..eb14d26 100644 --- a/sysmodules/pm/source/luma.c +++ b/sysmodules/pm/source/luma.c @@ -3,11 +3,24 @@ #include "luma.h" #include "util.h" +bool hasKExt(void) +{ + s64 val; + return svcGetSystemInfo(&val, 0x20000, 0) == 1; +} + u32 getKExtSize(void) { s64 val; - Result res = svcGetSystemInfo(&val, 0x10000, 0x300); - return R_FAILED(res) ? 0 : (u32)val; + svcGetSystemInfo(&val, 0x10000, 0x300); + return (u32)val; +} + +u32 getStolenSystemMemRegionSize(void) +{ + s64 val; + svcGetSystemInfo(&val, 0x10000, 0x301); + return (u32)val; } bool isTitleLaunchPrevented(u64 titleId) diff --git a/sysmodules/pm/source/luma.h b/sysmodules/pm/source/luma.h index db486d6..80b80ef 100644 --- a/sysmodules/pm/source/luma.h +++ b/sysmodules/pm/source/luma.h @@ -2,5 +2,7 @@ #include <3ds/types.h> +bool hasKExt(void); u32 getKExtSize(void); -bool isTitleLaunchPrevented(u64 titleId); \ No newline at end of file +u32 getStolenSystemMemRegionSize(void); +bool isTitleLaunchPrevented(u64 titleId); diff --git a/sysmodules/pm/source/reslimit.c b/sysmodules/pm/source/reslimit.c index b3bc0d9..2611f72 100644 --- a/sysmodules/pm/source/reslimit.c +++ b/sysmodules/pm/source/reslimit.c @@ -243,7 +243,10 @@ static ReslimitValues *fixupReslimitValues(void) { // In order: APPLICATION, SYS_APPLET, LIB_APPLET, OTHER // Fixup "commit" reslimit - u32 sysmemalloc = SYSMEMALLOC + getKExtSize(); + + // Note: we lie in the reslimit and make as if neither KExt nor Roslina existed, to avoid breakage + + u32 sysmemalloc = SYSMEMALLOC + (hasKExt() ? getStolenSystemMemRegionSize() : 0); ReslimitValues *values = !IS_N3DS ? g_o3dsReslimitValues : g_n3dsReslimitValues; static const u32 minAppletMemAmount = 0x1200000; diff --git a/sysmodules/rosalina/rosalina.rsf b/sysmodules/rosalina/rosalina.rsf index 53eb796..a6c826e 100644 --- a/sysmodules/rosalina/rosalina.rsf +++ b/sysmodules/rosalina/rosalina.rsf @@ -37,7 +37,7 @@ AccessControlInfo: CoreVersion : 2 DescVersion : 2 - MemoryType : System # Application / System / Base + MemoryType : Base # Application / System / Base HandleTableSize: 150 MemoryMapping: diff --git a/sysmodules/rosalina/source/menus.c b/sysmodules/rosalina/source/menus.c index 1103e3b..fa54ae4 100644 --- a/sysmodules/rosalina/source/menus.c +++ b/sysmodules/rosalina/source/menus.c @@ -62,7 +62,7 @@ Menu rosalinaMenu = { bool rosalinaMenuShouldShowDebugInfo(void) { - return false; + return true; } void RosalinaMenu_ShowDebugInfo(void)