From 0872cce98b758553a1a284107e23d0bef67c3031 Mon Sep 17 00:00:00 2001 From: gijs Date: Sun, 22 Oct 2023 00:07:08 +0200 Subject: [PATCH] Fix proper stack sizes Add `DECLARE_STACK` and `STACK_START` macros Fix addresses so `nuStackIdle` and `obStackMain` are 0x2000 in size --- include/common.h | 4 ++++ include/nu/nusys.h | 4 ++-- linker_scripts/symbols/addr.txt | 6 +++--- src/boot.c | 11 ++++++----- src/nu/nusimgr.c | 4 ++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/common.h b/include/common.h index 6fbd858..ca7edcb 100644 --- a/include/common.h +++ b/include/common.h @@ -75,6 +75,10 @@ typedef u8 bool; #define SCREEN_WIDTH_MAX 640 #define SCREEN_HEIGHT_MAX 480 +#define DECLARE_STACK(name, size) u64 name[size / sizeof(u64)] +// Stacks grow backwards (towards smaller addresses) +#define STACK_START(stack) ((u8*)(&stack) + sizeof(stack)) + #include "character.h" typedef struct { diff --git a/include/nu/nusys.h b/include/nu/nusys.h index 6ff862b..94637f8 100644 --- a/include/nu/nusys.h +++ b/include/nu/nusys.h @@ -60,9 +60,9 @@ extern "C" { /*--------------------------------------*/ /* NUSYS STACK SIZE */ /*--------------------------------------*/ -//#define NU_IDLE_STACK_SIZE 0x2000 /* Idle thread */ +#define NU_IDLE_STACK_SIZE 0x2000 /* Idle thread */ #define NU_RMON_STACK_SIZE 0x2000 /* Rmon thread */ -//#define NU_MAIN_STACK_SIZE NU_SPEC_BOOT_STACK_SIZE /* Main thread */ +#define NU_MAIN_STACK_SIZE NU_SPEC_BOOT_STACK_SIZE /* Main thread */ diff --git a/linker_scripts/symbols/addr.txt b/linker_scripts/symbols/addr.txt index 8852421..81ee3a9 100644 --- a/linker_scripts/symbols/addr.txt +++ b/linker_scripts/symbols/addr.txt @@ -27,7 +27,7 @@ nuSiMesgQ = 0x800E9B88; nuSiMesgBuf = 0x800BBE60; siMgrThread = 0x800BBE80; // size:0x1B0 nuThreadIdle = 0x800AF440; -nuStackIdle = 0x800B0010; +nuStackIdle = 0x800AF7A0; // size:0x2000 nuPiCartHandle = 0x800E7A20; nuPiMgrMesgQ = 0x800BE030; nuPiMesgBuf = 0x800BE048; @@ -1029,8 +1029,8 @@ __VIRTUAL_MEMORY_EXHAUSTED = 0x800AE35C; __virtual_mem_alloc = 0x80070F30; obMain = 0x80071EB0; -obThreadMain = 0x800AF5F0; -obStackMain = 0x800C4CF0; +obThreadMain = 0x800AF5F0; // size:0x1B0 +obStackMain = 0x800C4D60; // size:0x2000 entry = 0x80070C00; diff --git a/src/boot.c b/src/boot.c index 8615ef3..5262ed8 100644 --- a/src/boot.c +++ b/src/boot.c @@ -2,20 +2,21 @@ #include "nu/nusys.h" -#define NU_IDLE_STACK_SIZE 0x1790 #define OB_MAIN_STACK_SIZE 0x2070 extern OSThread nuThreadIdle; -extern u64 nuStackIdle[NU_IDLE_STACK_SIZE / sizeof(u64)]; +extern DECLARE_STACK(nuStackIdle, NU_IDLE_STACK_SIZE); extern OSThread obThreadMain; -extern u64 obStackMain[OB_MAIN_STACK_SIZE / sizeof(u64)]; +extern DECLARE_STACK(obStackMain, NU_MAIN_STACK_SIZE); + +const int test = sizeof(obThreadMain); void nuIdle(void* arg); void nuBoot(void) { osInitialize(); - osCreateThread(&nuThreadIdle, NU_IDLE_THREAD_ID, nuIdle, NULL, nuStackIdle + NU_IDLE_STACK_SIZE / sizeof(u64), NU_MAIN_THREAD_PRI); + osCreateThread(&nuThreadIdle, NU_IDLE_THREAD_ID, nuIdle, NULL, STACK_START(nuStackIdle), NU_MAIN_THREAD_PRI); osStartThread(&nuThreadIdle); } @@ -27,7 +28,7 @@ void nuIdle(void* arg) { osViSetSpecialFeatures(OS_VI_DIVOT_ON | OS_VI_DITHER_FILTER_ON | OS_VI_GAMMA_OFF | OS_VI_GAMMA_DITHER_OFF); - osCreateThread(&obThreadMain, NU_MAIN_THREAD_ID, obMain, NULL, obStackMain + OB_MAIN_STACK_SIZE / sizeof(u64), NU_MAIN_THREAD_PRI); + osCreateThread(&obThreadMain, NU_MAIN_THREAD_ID, obMain, NULL, STACK_START(obStackMain), NU_MAIN_THREAD_PRI); osStartThread(&obThreadMain); osSetThreadPri(&nuThreadIdle, NU_IDLE_THREAD_PRI); diff --git a/src/nu/nusimgr.c b/src/nu/nusimgr.c index 6894771..dc705ad 100644 --- a/src/nu/nusimgr.c +++ b/src/nu/nusimgr.c @@ -5,7 +5,7 @@ void nuSiMgrThread(void* arg); -extern u64 nuStackSiMgrThread[0x2000 / sizeof(u64)]; +extern DECLARE_STACK(nuStackSiMgrThread, 0x2000); /* 1A060 80089C60 */ u8 nuSiMgrInit(void) { @@ -17,7 +17,7 @@ u8 nuSiMgrInit(void) { osContInit(&nuSiMesgQ, &pattern, status); - osCreateThread(&siMgrThread, 5, nuSiMgrThread, NULL, nuStackSiMgrThread + 0x2000 / sizeof(u64), NU_SI_THREAD_PRI); + osCreateThread(&siMgrThread, 5, nuSiMgrThread, NULL, STACK_START(nuStackSiMgrThread), NU_SI_THREAD_PRI); osStartThread(&siMgrThread); return pattern;