From 93c24a5631ba5281046d2cd1b141cc2f0ce1d29f Mon Sep 17 00:00:00 2001 From: Justin Weiss Date: Sat, 5 Oct 2019 21:35:06 -0700 Subject: [PATCH] 3DS: Switch from svc* to the thread* API svcCreateThread doesn't fully set up thread vars, which causes svcBreaks / crashes when calling certain functions -- reentrant ones, for example. threadCreate, etc. are higher-level functions that do all the correct setup and cleanup. Since we're treating the thread structure as opaque, calling it an int_32t seems OK. --- frontend/3ds/3ds_utils.h | 8 +++++--- frontend/3ds/pthread.h | 20 +++++--------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/frontend/3ds/3ds_utils.h b/frontend/3ds/3ds_utils.h index 3d50a668..1f12b840 100644 --- a/frontend/3ds/3ds_utils.h +++ b/frontend/3ds/3ds_utils.h @@ -2,6 +2,7 @@ #define _3DS_UTILS_H #include +#include #define MEMOP_PROT 6 #define MEMOP_MAP 4 @@ -15,9 +16,10 @@ int32_t svcCloseHandle(uint32_t handle); int32_t svcControlMemory(void* addr_out, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); int32_t svcControlProcessMemory(uint32_t process, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); -int32_t svcCreateThread(int32_t* thread, void *(*entrypoint)(void*), void* arg, void* stack_top, int32_t thread_priority, int32_t processor_id); -int32_t svcWaitSynchronization(int32_t handle, int64_t nanoseconds); -void svcExitThread(void) __attribute__((noreturn)); +int32_t threadCreate(void *(*entrypoint)(void*), void* arg, size_t stack_size, int32_t prio, int32_t affinity, bool detached); +int32_t threadJoin(int32_t thread, int64_t timeout_ns); +void threadFree(int32_t thread); +void threadExit(int32_t rc) __attribute__((noreturn)); int32_t svcBackdoor(int32_t (*callback)(void)); diff --git a/frontend/3ds/pthread.h b/frontend/3ds/pthread.h index 42de161f..9f43707e 100644 --- a/frontend/3ds/pthread.h +++ b/frontend/3ds/pthread.h @@ -10,23 +10,13 @@ #define CTR_PTHREAD_STACK_SIZE 0x10000 -typedef struct -{ - int32_t handle; - uint32_t* stack; -}pthread_t; +typedef int32_t pthread_t; typedef int pthread_attr_t; static inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) { - - thread->stack = linearMemAlign(CTR_PTHREAD_STACK_SIZE, 8); - - svcCreateThread(&thread->handle, start_routine, arg, - (uint32_t*)((uint32_t)thread->stack + CTR_PTHREAD_STACK_SIZE), - 0x25, -2); - + thread = threadCreate(start_routine, arg, CTR_PTHREAD_STACK_SIZE, 0x25, -2, FALSE); return 1; } @@ -35,10 +25,10 @@ static inline int pthread_join(pthread_t thread, void **retval) { (void)retval; - if(svcWaitSynchronization(thread.handle, INT64_MAX)) + if(threadJoin(thread, INT64_MAX)) return -1; - linearFree(thread.stack); + threadFree(thread); return 0; } @@ -48,7 +38,7 @@ static inline void pthread_exit(void *retval) { (void)retval; - svcExitThread(); + threadExit(0); } -- 2.39.5