From 3a136b304f6a37991b6772976d1e8c13520c7921 Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Fri, 11 Oct 2024 15:14:34 +0200 Subject: [PATCH] syscalls: enable getting functions from stack --- hal/armv7a/arch/cpu.h | 6 +++--- hal/armv7m/arch/cpu.h | 6 +++--- hal/armv8m/arch/cpu.h | 6 +++--- hal/armv8r/arch/cpu.h | 6 +++--- hal/ia32/arch/cpu.h | 5 ++--- hal/riscv64/arch/cpu.h | 6 +++--- hal/sparcv8leon3/arch/cpu.h | 8 ++++---- proc/process.h | 2 +- proc/threads.c | 4 ++-- syscalls.c | 10 +++++----- 10 files changed, 29 insertions(+), 30 deletions(-) diff --git a/hal/armv7a/arch/cpu.h b/hal/armv7a/arch/cpu.h index 8ad745a20..ca53eb31c 100644 --- a/hal/armv7a/arch/cpu.h +++ b/hal/armv7a/arch/cpu.h @@ -55,9 +55,9 @@ #define GETFROMSTACK(ustack, t, v, n) \ do { \ - ustack = (void *)(((addr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \ - (v) = *(t *)ustack; \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + ustack = (void *)(((addr_t)ustack + sizeof(typeof(v)) - 1) & ~(sizeof(typeof(v)) - 1)); \ + (v) = *(typeof(v) *)ustack; \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) typedef struct _cpu_context_t { diff --git a/hal/armv7m/arch/cpu.h b/hal/armv7m/arch/cpu.h index cf724fcf1..1a6d946d0 100644 --- a/hal/armv7m/arch/cpu.h +++ b/hal/armv7m/arch/cpu.h @@ -64,9 +64,9 @@ #define GETFROMSTACK(ustack, t, v, n) \ do { \ - ustack = (void *)(((ptr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \ - (v) = *(t *)ustack; \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + ustack = (void *)(((ptr_t)ustack + sizeof(typeof(v)) - 1) & ~(sizeof(typeof(v)) - 1)); \ + (v) = *(typeof(v) *)ustack; \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) diff --git a/hal/armv8m/arch/cpu.h b/hal/armv8m/arch/cpu.h index 3ab49e0d1..cf045102a 100644 --- a/hal/armv8m/arch/cpu.h +++ b/hal/armv8m/arch/cpu.h @@ -48,9 +48,9 @@ #define GETFROMSTACK(ustack, t, v, n) \ do { \ - ustack = (void *)(((ptr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \ - (v) = *(t *)ustack; \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + ustack = (void *)(((ptr_t)ustack + sizeof(typeof(v)) - 1) & ~(sizeof(typeof(v)) - 1)); \ + (v) = *(typeof(v) *)ustack; \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) diff --git a/hal/armv8r/arch/cpu.h b/hal/armv8r/arch/cpu.h index 8b53b4873..509502058 100644 --- a/hal/armv8r/arch/cpu.h +++ b/hal/armv8r/arch/cpu.h @@ -61,9 +61,9 @@ #define GETFROMSTACK(ustack, t, v, n) \ do { \ - ustack = (void *)(((addr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \ - (v) = *(t *)ustack; \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + ustack = (void *)(((addr_t)ustack + sizeof(typeof(v)) - 1) & ~(sizeof(typeof(v)) - 1)); \ + (v) = *(typeof(v) *)ustack; \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) typedef struct _cpu_context_t { diff --git a/hal/ia32/arch/cpu.h b/hal/ia32/arch/cpu.h index 76d894e1e..d894551b9 100644 --- a/hal/ia32/arch/cpu.h +++ b/hal/ia32/arch/cpu.h @@ -187,12 +187,11 @@ do { \ if (n == 0) \ ustack += 4; \ - v = *(t *)ustack; \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + v = *(typeof(v) *)ustack; \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) - #pragma pack(push, 1) typedef struct { diff --git a/hal/riscv64/arch/cpu.h b/hal/riscv64/arch/cpu.h index b58964f06..3ba79bcd8 100644 --- a/hal/riscv64/arch/cpu.h +++ b/hal/riscv64/arch/cpu.h @@ -72,9 +72,9 @@ #define GETFROMSTACK(ustack, t, v, n) \ do { \ - ustack = (void *)(((addr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \ - (v) = *(t *)ustack; \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + ustack = (void *)(((addr_t)ustack + sizeof(typeof(v)) - 1) & ~(sizeof(typeof(v)) - 1)); \ + (v) = *(typeof(v) *)ustack; \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) diff --git a/hal/sparcv8leon3/arch/cpu.h b/hal/sparcv8leon3/arch/cpu.h index 1e4a01eb9..b5e9b0613 100644 --- a/hal/sparcv8leon3/arch/cpu.h +++ b/hal/sparcv8leon3/arch/cpu.h @@ -150,10 +150,10 @@ do { \ /* 8-byte values might have been put on stack unaligned */ \ /* clang-format off */ \ - if (sizeof(t) == 8 && ((ptr_t)(ustack) & 0x7) != 0) { \ + if (sizeof(typeof(v)) == 8 && ((ptr_t)(ustack) & 0x7) != 0) { \ /* clang-format on */ \ union { \ - t val; \ + typeof(v) val; \ struct { \ u32 lo; \ u32 hi; \ @@ -164,9 +164,9 @@ v = data##n.val; \ } \ else { \ - (v) = *(t *)ustack; \ + (v) = *(typeof(v) *)ustack; \ } \ - ustack += SIZE_STACK_ARG(sizeof(t)); \ + ustack += SIZE_STACK_ARG(sizeof(typeof(v))); \ } while (0) diff --git a/proc/process.h b/proc/process.h index c0c881ae3..254d17c6b 100644 --- a/proc/process.h +++ b/proc/process.h @@ -66,7 +66,7 @@ typedef struct _process_t { unsigned sigpend; unsigned sigmask; - void *sighandler; + void (*sighandler)(void); void *got; hal_tls_t tls; diff --git a/proc/threads.c b/proc/threads.c index 1b9280c9d..71060c1d3 100644 --- a/proc/threads.c +++ b/proc/threads.c @@ -1486,7 +1486,7 @@ void threads_setupUserReturn(void *retval) { spinlock_ctx_t sc; cpu_context_t *ctx, *signalCtx; - void *f; + void (*f)(void); void *kstackTop; thread_t *thread; @@ -1501,7 +1501,7 @@ void threads_setupUserReturn(void *retval) if (_threads_checkSignal(thread, thread->process, signalCtx, SIG_SRC_SCALL) == 0) { f = thread->process->sighandler; hal_spinlockClear(&threads_common.spinlock, &sc); - hal_jmp(f, kstackTop, hal_cpuGetUserSP(signalCtx), 0, NULL); + hal_jmp((void *)f, kstackTop, hal_cpuGetUserSP(signalCtx), 0, NULL); /* no return */ } diff --git a/syscalls.c b/syscalls.c index fc991c893..e13a2d473 100644 --- a/syscalls.c +++ b/syscalls.c @@ -294,7 +294,7 @@ int syscalls_beginthreadex(void *ustack) int *id; int err; - GETFROMSTACK(ustack, void *, start, 0); + GETFROMSTACK(ustack, void (*)(void *), start, 0); GETFROMSTACK(ustack, unsigned int, priority, 1); GETFROMSTACK(ustack, void *, stack, 2); GETFROMSTACK(ustack, unsigned int, stacksz, 3); @@ -659,14 +659,14 @@ int syscalls_interrupt(void *ustack) { process_t *proc = proc_current()->process; unsigned int n; - void *f; + int (*f)(unsigned int, void *); void *data; handle_t cond; handle_t *handle; int res; GETFROMSTACK(ustack, unsigned int, n, 0); - GETFROMSTACK(ustack, void *, f, 1); + GETFROMSTACK(ustack, int (*)(unsigned int, void *), f, 1); GETFROMSTACK(ustack, void *, data, 2); GETFROMSTACK(ustack, handle_t, cond, 3); GETFROMSTACK(ustack, handle_t *, handle, 4); @@ -941,11 +941,11 @@ addr_t syscalls_va2pa(void *ustack) int syscalls_signalHandle(void *ustack) { - void *handler; + void (*handler)(void); unsigned mask, mmask; thread_t *thread; - GETFROMSTACK(ustack, void *, handler, 0); + GETFROMSTACK(ustack, void (*)(void), handler, 0); GETFROMSTACK(ustack, unsigned, mask, 1); GETFROMSTACK(ustack, unsigned, mmask, 2);