From fcd08d661463614d7a150c5357cabcba05268fcc Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Sat, 17 Dec 2022 23:33:15 +0100 Subject: [PATCH] Patch kernel11 to remove checks on core2/3 thread creation This stubs checks in SvcCreateThread and SvcSetProcessIdealProcessor that applied when creating threads on core2 and core3. This allows non-sysmodules to create threads on core3. Please note, core2 access was already being automatically granted to 3dsx apps for a long time (this is controlled through a kernel flag), and other apps that needed it had that flag too. This commit thus changes nothing for all these applications. Do not create threads on core3 unless you know exactly what you're doing. On N3DS, gsp (GPU sysmodule) depends on qtm (head-tracking sysmodule) which runs many threads at very high priority on core3. Running code that needs the GPU (including printf) on core3 can thus result in thread starvation / deadlock. If you just need an extra application core, just use core2 as it is intended for that exact purpose and is by default completely idle. Closes #1668 --- arm11/linker.specs | 2 +- arm9/linker.specs | 2 +- arm9/source/patches.c | 19 ++++++++++++++++++- k11_extension/linker.specs | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/arm11/linker.specs b/arm11/linker.specs index 3009904..9dbe4ff 100644 --- a/arm11/linker.specs +++ b/arm11/linker.specs @@ -1,7 +1,7 @@ %rename link old_link *link: -%(old_link) -T %:getenv(TOPDIR /linker.ld) --nmagic --gc-sections +%(old_link) -T %:getenv(TOPDIR /linker.ld) --nmagic --gc-sections --no-warn-rwx-segments *startfile: crti%O%s crtbegin%O%s diff --git a/arm9/linker.specs b/arm9/linker.specs index 3009904..9dbe4ff 100644 --- a/arm9/linker.specs +++ b/arm9/linker.specs @@ -1,7 +1,7 @@ %rename link old_link *link: -%(old_link) -T %:getenv(TOPDIR /linker.ld) --nmagic --gc-sections +%(old_link) -T %:getenv(TOPDIR /linker.ld) --nmagic --gc-sections --no-warn-rwx-segments *startfile: crti%O%s crtbegin%O%s diff --git a/arm9/source/patches.c b/arm9/source/patches.c index 9a0fb75..e4c3799 100644 --- a/arm9/source/patches.c +++ b/arm9/source/patches.c @@ -271,6 +271,23 @@ u32 patchKernel11(u8 *pos, u32 size, u32 baseK11VA, u32 *arm11SvcTable, u32 *arm off[-5] = 0xE51FF004; off[-4] = K11EXT_VA + 0x2C; + if (ISN3DS) + { + // Patch SvcSetProcessIdealProcessor and SvcCreate thread to always allow + // for core2 and core3 to be used. Normally, processes with the 0x2000 kernel flag + // have access to core2, and BASE processes have access to both core2 and core3. + // We're patching the if (memory region == BASE) check to be always true. + off = (u32 *)pos; + for (u32 i = 0; i < 2 && (u8 *)off < pos + size; i++) + { + // cmp r2, #0x300; beq... + for (; (off[0] != 0xE3520C03 || off[1] != 0x0A000003) && (u8 *)off < pos + size; off++); + if ((u8 *)off > pos + size) + return 1; + off[1] = 0xEA000003; + } + } + return 0; } @@ -581,7 +598,7 @@ u32 patchP9AMTicketWrapperZeroKeyIV(u8 *pos, u32 size, u32 firmVersion) //Beyond limit if(opjumpdistance < -0x1fffff || opjumpdistance > 0x1fffff) return 1; - //r0 and r1 for old call are already correct for this one + //r0 and r1 for old call are already correct for this one //BLX __rt_memclr u32 op = (0xE800F000U | (((u32)opjumpdistance & 0x7FF) << 16) | (((u32)opjumpdistance >> 11) & 0x3FF) | (((u32)opjumpdistance >> 21) & 0x400)) & ~(1<<16); diff --git a/k11_extension/linker.specs b/k11_extension/linker.specs index 3009904..9dbe4ff 100644 --- a/k11_extension/linker.specs +++ b/k11_extension/linker.specs @@ -1,7 +1,7 @@ %rename link old_link *link: -%(old_link) -T %:getenv(TOPDIR /linker.ld) --nmagic --gc-sections +%(old_link) -T %:getenv(TOPDIR /linker.ld) --nmagic --gc-sections --no-warn-rwx-segments *startfile: crti%O%s crtbegin%O%s