From eb37ac4142d21010f1bbc8807336b1588adc703c Mon Sep 17 00:00:00 2001 From: TuxSH Date: Sun, 26 Apr 2020 01:50:47 +0100 Subject: [PATCH] redefine exit methods for sysmodules. Partially fixes the shutdown issue --- sysmodules/loader/source/main.c | 54 ++++++------------- sysmodules/pm/source/main.c | 82 ++++++++++++---------------- sysmodules/pxi/source/main.c | 74 +++++++++++-------------- sysmodules/rosalina/source/main.c | 90 +++++++++++++++---------------- sysmodules/sm/source/main.c | 45 +++++++--------- 5 files changed, 147 insertions(+), 198 deletions(-) diff --git a/sysmodules/loader/source/main.c b/sysmodules/loader/source/main.c index af1c10c..92f2e02 100644 --- a/sysmodules/loader/source/main.c +++ b/sysmodules/loader/source/main.c @@ -47,9 +47,23 @@ static inline void loadCFWInfo(void) if(needToInitSd) fileOpen(&file, ARCHIVE_SDMC, "/", FS_OPEN_READ); //Init SD card if SAFE_MODE is being booted } -// this is called before main -void __appInit() +void __ctru_exit(int rc) { (void)rc; } // needed to avoid linking error + +// this is called after main exits +void exit(int rc) { + (void)rc; + // Not supposed to terminate... kernel will clean up the handles if it does happen anyway + svcExitProcess(); +} + +void __sync_init(); +void __libc_init_array(void); + +// called before main +void initSystem(void) +{ + __sync_init(); loadCFWInfo(); Result res; @@ -71,42 +85,8 @@ void __appInit() assertSuccess(FSUSER_SetPriority(0)); assertSuccess(pxiPmInit()); -} -// this is called after main exits -void __appExit() -{ - pxiPmExit(); - //fsldrExit(); - svcCloseHandle(*fsGetSessionHandle()); - fsRegExit(); - srvExit(); -} - -// stubs for non-needed pre-main functions -void __sync_init(); -void __sync_fini(); -void __system_initSyscalls(); - -void __ctru_exit() -{ - void __libc_fini_array(void); - - __libc_fini_array(); - __appExit(); - __sync_fini(); - svcExitProcess(); -} - -void initSystem() -{ - void __libc_init_array(void); - - __sync_init(); - __system_initSyscalls(); - __appInit(); - - __libc_init_array(); + //__libc_init_array(); } static const ServiceManagerServiceEntry services[] = { diff --git a/sysmodules/pm/source/main.c b/sysmodules/pm/source/main.c index 5ba8515..07fbf38 100644 --- a/sysmodules/pm/source/main.c +++ b/sysmodules/pm/source/main.c @@ -14,10 +14,27 @@ #include "service_manager.h" static MyThread processMonitorThread, taskRunnerThread; +static u8 ALIGN(8) processDataBuffer[0x40 * sizeof(ProcessData)] = {0}; +static u8 ALIGN(8) exheaderInfoBuffer[6 * sizeof(ExHeader_Info)] = {0}; +static u8 ALIGN(8) threadStacks[2][THREAD_STACK_SIZE] = {0}; -// this is called before main -void __appInit() +// this is called after main exits +void exit(int rc) { + (void)rc; + // Not supposed to terminate... kernel will clean up the handles if it does happen anyway + svcExitProcess(); +} + +Result __sync_init(void); +//void __libc_init_array(void); + +// Called before main +void initSystem() +{ + __sync_init(); + //__libc_init_array(); + // Wait for sm for(Result res = 0xD88007FA; res == (Result)0xD88007FA; svcSleepThread(500 * 1000LL)) { res = srvPmInit(); @@ -28,56 +45,10 @@ void __appInit() loaderInit(); fsRegInit(); - static u8 ALIGN(8) processDataBuffer[0x40 * sizeof(ProcessData)] = {0}; - static u8 ALIGN(8) exheaderInfoBuffer[6 * sizeof(ExHeader_Info)] = {0}; - static u8 ALIGN(8) threadStacks[2][THREAD_STACK_SIZE] = {0}; - // Init objects Manager_Init(processDataBuffer, 0x40); ExHeaderInfoHeap_Init(exheaderInfoBuffer, 6); TaskRunner_Init(); - - // Init the reslimits, register the KIPs and map the firmlaunch parameters - initializeReslimits(); - Manager_RegisterKips(); - mapFirmlaunchParameters(); - - // Create the threads - assertSuccess(MyThread_Create(&processMonitorThread, processMonitor, NULL, threadStacks[0], THREAD_STACK_SIZE, 0x17, -2)); - assertSuccess(MyThread_Create(&taskRunnerThread, TaskRunner_HandleTasks, NULL, threadStacks[1], THREAD_STACK_SIZE, 0x17, -2)); - - // Launch NS, etc. - autolaunchSysmodules(); -} - -// this is called after main exits -void __appExit() -{ - // We don't clean up g_manager's handles because it could hang the process monitor thread, etc. - fsRegExit(); - loaderExit(); - srvPmExit(); -} - - -Result __sync_init(void); -Result __sync_fini(void); -void __libc_init_array(void); -void __libc_fini_array(void); - -void __ctru_exit() -{ - __libc_fini_array(); - __appExit(); - __sync_fini(); - svcExitProcess(); -} - -void initSystem() -{ - __sync_init(); - __appInit(); - __libc_init_array(); } static const ServiceManagerServiceEntry services[] = { @@ -90,9 +61,24 @@ static const ServiceManagerNotificationEntry notifications[] = { { 0x000, NULL }, }; +void __ctru_exit(int rc) { (void)rc; } // needed to avoid linking error + int main(void) { Result res = 0; + + // Init the reslimits, register the KIPs and map the firmlaunch parameters + initializeReslimits(); + Manager_RegisterKips(); + mapFirmlaunchParameters(); + + // Create the threads + assertSuccess(MyThread_Create(&processMonitorThread, processMonitor, NULL, threadStacks[0], THREAD_STACK_SIZE, 0x17, -2)); + assertSuccess(MyThread_Create(&taskRunnerThread, TaskRunner_HandleTasks, NULL, threadStacks[1], THREAD_STACK_SIZE, 0x17, -2)); + + // Launch NS, etc. + autolaunchSysmodules(); + if (R_FAILED(res = ServiceManager_Run(services, notifications, NULL))) { panic(res); } diff --git a/sysmodules/pxi/source/main.c b/sysmodules/pxi/source/main.c index 451488a..8c9194e 100644 --- a/sysmodules/pxi/source/main.c +++ b/sysmodules/pxi/source/main.c @@ -7,7 +7,6 @@ This is part of 3ds_pxi, which is licensed under the MIT license (see LICENSE fo */ #include - #include "PXI.h" #include "common.h" #include "MyThread.h" @@ -100,9 +99,38 @@ static u8 ALIGN(8) receiverStack[THREAD_STACK_SIZE]; static u8 ALIGN(8) senderStack[THREAD_STACK_SIZE]; static u8 ALIGN(8) PXISRV11HandlerStack[THREAD_STACK_SIZE]; -// this is called before main -void __appInit() +Result __sync_init(void); +Result __sync_fini(void); +void __libc_fini_array(void); +void __libc_init_array(void); + +void __ctru_exit(int rc) { (void)rc; } // needed to avoid linking error + +// this is called after main exits +void exit(int rc) { + (void)rc; + + srvExit(); + exitPXI(); + + svcCloseHandle(terminationRequestedEvent); + svcCloseHandle(sessionManager.sendAllBuffersToArm9Event); + svcCloseHandle(sessionManager.replySemaphore); + svcCloseHandle(sessionManager.PXISRV11CommandReceivedEvent); + svcCloseHandle(sessionManager.PXISRV11ReplySentEvent); + + //__libc_fini_array(); + __sync_fini(); + svcExitProcess(); +} + +// this is called before main + +void initSystem(void) +{ + __sync_init(); + assertSuccess(svcCreateEvent(&terminationRequestedEvent, RESET_STICKY)); assertSuccess(svcCreateEvent(&sessionManager.sendAllBuffersToArm9Event, RESET_ONESHOT)); @@ -117,46 +145,8 @@ void __appInit() if(R_FAILED(res) && res != (Result)0xD88007FA) svcBreak(USERBREAK_PANIC); } -} -// this is called after main exits -void __appExit() -{ - srvExit(); - exitPXI(); - - svcCloseHandle(terminationRequestedEvent); - svcCloseHandle(sessionManager.sendAllBuffersToArm9Event); - svcCloseHandle(sessionManager.replySemaphore); - svcCloseHandle(sessionManager.PXISRV11CommandReceivedEvent); - svcCloseHandle(sessionManager.PXISRV11ReplySentEvent); -} - -// stubs for non-needed pre-main functions -void __system_initSyscalls(){} - - -Result __sync_init(void); -Result __sync_fini(void); - -void __ctru_exit() -{ - void __libc_fini_array(void); - - __libc_fini_array(); - __appExit(); - __sync_fini(); - svcExitProcess(); -} - -void initSystem() -{ - void __libc_init_array(void); - - __sync_init(); - __system_initSyscalls(); - __appInit(); - __libc_init_array(); + //__libc_init_array(); } int main(void) diff --git a/sysmodules/rosalina/source/main.c b/sysmodules/rosalina/source/main.c index 54e04bc..b4b4b5a 100644 --- a/sysmodules/rosalina/source/main.c +++ b/sysmodules/rosalina/source/main.c @@ -40,6 +40,8 @@ #include "task_runner.h" +bool isN3DS; + static Result stealFsReg(void) { Result ret = 0; @@ -73,11 +75,50 @@ static Result fsRegSetupPermissions(void) return res; } -// this is called before main -bool isN3DS; -void __appInit() +Result __sync_init(void); +Result __sync_fini(void); +void __libc_init_array(void); +void __libc_fini_array(void); + +void __ctru_exit(int rc) { (void)rc; } // needed to avoid linking error + +// this is called after main exits +void exit(int rc) { + (void)rc; + // TODO: make pm terminate rosalina + __libc_fini_array(); + + acExit(); + pmDbgExit(); + fsExit(); + svcCloseHandle(*fsRegGetSessionHandle()); + srvExit(); + + __sync_fini(); + svcExitProcess(); +} + +// this is called before main +void initSystem(void) +{ + s64 out; Result res; + __sync_init(); + + isN3DS = svcGetSystemInfo(&out, 0x10001, 0) == 0; + + svcGetSystemInfo(&out, 0x10000, 0x100); + HBLDR_3DSX_TID = out == 0 ? HBLDR_DEFAULT_3DSX_TID : (u64)out; + + svcGetSystemInfo(&out, 0x10000, 0x101); + menuCombo = out == 0 ? DEFAULT_MENU_COMBO : (u32)out; + + miscellaneousMenu.items[0].title = HBLDR_3DSX_TID == HBLDR_DEFAULT_3DSX_TID ? "Switch the hb. title to the current app." : + "Switch the hb. title to hblauncher_loader"; + + ProcessPatchesMenu_PatchUnpatchFSDirectly(); + for(res = 0xD88007FA; res == (Result)0xD88007FA; svcSleepThread(500 * 1000LL)) { res = srvInit(); @@ -93,50 +134,7 @@ void __appInit() if (R_FAILED(acInit())) svcBreak(USERBREAK_PANIC); -} -// this is called after main exits -void __appExit() -{ - acExit(); - pmDbgExit(); - fsExit(); - svcCloseHandle(*fsRegGetSessionHandle()); - srvExit(); -} - - -Result __sync_init(void); -Result __sync_fini(void); -void __libc_init_array(void); -void __libc_fini_array(void); - -void __ctru_exit() -{ - __libc_fini_array(); - __appExit(); - __sync_fini(); - svcExitProcess(); -} - - -void initSystem() -{ - s64 out; - isN3DS = svcGetSystemInfo(&out, 0x10001, 0) == 0; - - svcGetSystemInfo(&out, 0x10000, 0x100); - HBLDR_3DSX_TID = out == 0 ? HBLDR_DEFAULT_3DSX_TID : (u64)out; - - svcGetSystemInfo(&out, 0x10000, 0x101); - menuCombo = out == 0 ? DEFAULT_MENU_COMBO : (u32)out; - - miscellaneousMenu.items[0].title = HBLDR_3DSX_TID == HBLDR_DEFAULT_3DSX_TID ? "Switch the hb. title to the current app." : - "Switch the hb. title to hblauncher_loader"; - - ProcessPatchesMenu_PatchUnpatchFSDirectly(); - __sync_init(); - __appInit(); __libc_init_array(); // ROSALINA HACKJOB BEGIN diff --git a/sysmodules/sm/source/main.c b/sysmodules/sm/source/main.c index 2a727c2..c37f5aa 100644 --- a/sysmodules/sm/source/main.c +++ b/sysmodules/sm/source/main.c @@ -24,10 +24,27 @@ static ProcessData processDataPool[64]; static u8 ALIGN(4) serviceAccessListStaticBuffer[0x110]; -void __appInit(void) -{ - s64 out; +void __ctru_exit(int rc) { (void)rc; } // needed to avoid linking error +// this is called after main exits +void exit(int rc) +{ + (void)rc; + // Not supposed to terminate... kernel will clean up the handles if it does happen anyway + svcExitProcess(); +} + +void __sync_init(); +// void __libc_init_array(void); + +// Called before main +void initSystem(void) +{ + __sync_init(); + + //__libc_init_array(); + + s64 out; u32 *staticBuffers = getThreadStaticBuffers(); staticBuffers[0] = IPC_Desc_StaticBuffer(0x110, 0); staticBuffers[1] = (u32)serviceAccessListStaticBuffer; @@ -40,28 +57,6 @@ void __appInit(void) buildList(&freeProcessDataList, processDataPool, sizeof(processDataPool) / sizeof(ProcessData), sizeof(ProcessData)); } - -// this is called after main exits -void __appExit(void){} - -void __system_allocateHeaps(void){} - -void __system_initSyscalls(void){} - -Result __sync_init(void); -Result __sync_fini(void); - -void __ctru_exit(void){} - -void initSystem(void) -{ - void __libc_init_array(void); - __sync_init(); - __system_allocateHeaps(); - __appInit(); - __libc_init_array(); -} - int main(void) { Result res;