From: kosmirror <kos...@us...> - 2025-08-16 17:02:50
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "A pseudo Operating System for the Dreamcast.". The branch, master has been updated via 011c420e0b5c843591fb8258c4784eab4459fdac (commit) via 4e79be987997f5e1250da90d0cf89b443da3d439 (commit) from a6cf282777a0aa3477f725695c38eb072f97e599 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 011c420e0b5c843591fb8258c4784eab4459fdac Author: darc <da...@pr...> Date: Wed Aug 6 10:22:31 2025 -0500 Add arch_stk_setup function for setting up a new stack during thread creation Some architectures, like PowerPC, need the stack to be adjusted before thread creation. This is unnecessary on Dreamcast, so the implementation added to Dreamcast is empty. commit 4e79be987997f5e1250da90d0cf89b443da3d439 Author: darc <da...@pr...> Date: Wed Aug 6 10:10:44 2025 -0500 Move stack functions from arch.h to stack.h ----------------------------------------------------------------------- Summary of changes: addons/libpthread/pthread_attr_init.c | 2 +- .../basic/stackprotector/stackprotector.c | 2 +- kernel/arch/dreamcast/hardware/pvr/pvr_mem.c | 2 + kernel/arch/dreamcast/include/arch/arch.h | 71 +----------------- kernel/arch/dreamcast/include/arch/stack.h | 87 ++++++++++++++++++++-- kernel/arch/dreamcast/kernel/mm.c | 4 +- kernel/arch/dreamcast/kernel/stack.c | 10 ++- kernel/thread/thread.c | 17 ++++- 8 files changed, 109 insertions(+), 86 deletions(-) diff --git a/addons/libpthread/pthread_attr_init.c b/addons/libpthread/pthread_attr_init.c index 6eab4ace..43732e16 100644 --- a/addons/libpthread/pthread_attr_init.c +++ b/addons/libpthread/pthread_attr_init.c @@ -11,7 +11,7 @@ #include <string.h> #include <kos/thread.h> -#include <arch/arch.h> +#include <arch/stack.h> int pthread_attr_init(pthread_attr_t *attr) { if(!attr) diff --git a/examples/dreamcast/basic/stackprotector/stackprotector.c b/examples/dreamcast/basic/stackprotector/stackprotector.c index 235840fc..710360f1 100644 --- a/examples/dreamcast/basic/stackprotector/stackprotector.c +++ b/examples/dreamcast/basic/stackprotector/stackprotector.c @@ -26,7 +26,7 @@ #include <string.h> #include <stdio.h> -#include <arch/arch.h> +#include <arch/stack.h> #include <stdlib.h> /* This function will override the default stack protector handler that is diff --git a/kernel/arch/dreamcast/hardware/pvr/pvr_mem.c b/kernel/arch/dreamcast/hardware/pvr/pvr_mem.c index b9cff2bf..fc6ac904 100644 --- a/kernel/arch/dreamcast/hardware/pvr/pvr_mem.c +++ b/kernel/arch/dreamcast/hardware/pvr/pvr_mem.c @@ -15,6 +15,8 @@ #include <kos/opts.h> #include <kos/dbglog.h> +#include <arch/stack.h> + /* This module basically serves as a KOS-friendly front end and support routines diff --git a/kernel/arch/dreamcast/include/arch/arch.h b/kernel/arch/dreamcast/include/arch/arch.h index 1f9d227c..2414f919 100644 --- a/kernel/arch/dreamcast/include/arch/arch.h +++ b/kernel/arch/dreamcast/include/arch/arch.h @@ -10,8 +10,7 @@ \brief Dreamcast architecture specific options. \ingroup arch - This file has various architecture specific options defined in it. Also, any - functions that start with arch_ are in here. + This file has various architecture specific options defined in it. \author Megan Potter */ @@ -80,21 +79,6 @@ extern char _etext; static const unsigned HZ __depr("Please use the new THD_SCHED_HZ macro.") = THD_SCHED_HZ; -#ifndef THD_STACK_ALIGNMENT -/** \brief Required alignment for stack. */ -#define THD_STACK_ALIGNMENT 8 -#endif - -#ifndef THD_STACK_SIZE -/** \brief Default thread stack size. */ -#define THD_STACK_SIZE 32768 -#endif - -#ifndef THD_KERNEL_STACK_SIZE -/** \brief Main/kernel thread's stack size. */ -#define THD_KERNEL_STACK_SIZE (64 * 1024) -#endif - /** \brief Default video mode. */ #define DEFAULT_VID_MODE DM_640x480 @@ -376,59 +360,6 @@ static inline void arch_sleep(void) { __asm__ __volatile__("sleep\n"); } -/** \brief DC specific "function" to get the return address from the current - function. - \ingroup arch - - \return The return address of the current function. -*/ -static __always_inline uintptr_t arch_get_ret_addr(void) { - uintptr_t pr; - - __asm__ __volatile__("sts pr,%0\n" : "=r"(pr)); - - return pr; -} - -/* Please note that all of the following frame pointer macros are ONLY - valid if you have compiled your code WITHOUT -fomit-frame-pointer. These - are mainly useful for getting a stack trace from an error. */ - -/** \brief DC specific "function" to get the frame pointer from the current - function. - \ingroup arch - - \return The frame pointer from the current function. - \note This only works if you don't disable frame pointers. -*/ -static __always_inline uintptr_t arch_get_fptr(void) { - register uintptr_t fp __asm__("r14"); - - return fp; -} - -/** \brief Pass in a frame pointer value to get the return address for the - given frame. - \ingroup arch - - \param fptr The frame pointer to look at. - \return The return address of the pointer. -*/ -static inline uintptr_t arch_fptr_ret_addr(uintptr_t fptr) { - return *(uintptr_t *)fptr; -} - -/** \brief Pass in a frame pointer value to get the previous frame pointer for - the given frame. - \ingroup arch - - \param fptr The frame pointer to look at. - \return The previous frame pointer. -*/ -static inline uintptr_t arch_fptr_next(uintptr_t fptr) { - return arch_fptr_ret_addr(fptr + 4); -} - /** \brief Returns true if the passed address is likely to be valid. Doesn't have to be exact, just a sort of general idea. \ingroup arch diff --git a/kernel/arch/dreamcast/include/arch/stack.h b/kernel/arch/dreamcast/include/arch/stack.h index d587aa25..6612bf4f 100644 --- a/kernel/arch/dreamcast/include/arch/stack.h +++ b/kernel/arch/dreamcast/include/arch/stack.h @@ -2,19 +2,22 @@ arch/dreamcast/include/arch/stack.h (c)2002 Megan Potter + (c)2025 Eric Fradella */ /** \file arch/stack.h - \brief Stack tracing. + \brief Stack functions \ingroup debugging_stacktrace - The functions in this file deal with doing stack traces. These functions - will do a stack trace, as specified, printing it out to stdout (usually a - dcload terminal). These functions only work if frame pointers have been - enabled at compile time (-DFRAME_POINTERS and no -fomit-frame-pointer flag). + This file contains arch-specific stack implementations, including defining + stack sizes and alignments, as well as functions for stack tracing and + debugging. On Dreamcast, the stack tracing functions only work if frame + pointers have been enabled at compile time (-DFRAME_POINTERS and no + -fomit-frame-pointer flag). \author Megan Potter + \author Eric Fradella */ #ifndef __ARCH_STACK_H @@ -24,6 +27,7 @@ __BEGIN_DECLS #include <stdint.h> +#include <kos/thread.h> /** \defgroup debugging_stacktrace Stack Traces \brief API for managing stack backtracing @@ -32,6 +36,79 @@ __BEGIN_DECLS @{ */ +#ifndef THD_STACK_ALIGNMENT +/** \brief Required alignment for stack. */ +#define THD_STACK_ALIGNMENT 8 +#endif + +#ifndef THD_STACK_SIZE +/** \brief Default thread stack size. */ +#define THD_STACK_SIZE 32768 +#endif + +#ifndef THD_KERNEL_STACK_SIZE +/** \brief Main/kernel thread's stack size. */ +#define THD_KERNEL_STACK_SIZE (64 * 1024) +#endif + +/** \brief DC specific "function" to get the return address from the current + function. + + \return The return address of the current function. +*/ +static __always_inline uintptr_t arch_get_ret_addr(void) { + uintptr_t pr; + + __asm__ __volatile__("sts pr,%0\n" : "=r"(pr)); + + return pr; +} + +/* Please note that all of the following frame pointer macros are ONLY + valid if you have compiled your code WITHOUT -fomit-frame-pointer. These + are mainly useful for getting a stack trace from an error. */ + +/** \brief DC specific "function" to get the frame pointer from the current + function. + + \return The frame pointer from the current function. + \note This only works if you don't disable frame pointers. +*/ +static __always_inline uintptr_t arch_get_fptr(void) { + register uintptr_t fp __asm__("r14"); + + return fp; +} + +/** \brief Pass in a frame pointer value to get the return address for the + given frame. + + \param fptr The frame pointer to look at. + \return The return address of the pointer. +*/ +static inline uintptr_t arch_fptr_ret_addr(uintptr_t fptr) { + return *(uintptr_t *)fptr; +} + +/** \brief Pass in a frame pointer value to get the previous frame pointer for + the given frame. + + \param fptr The frame pointer to look at. + \return The previous frame pointer. +*/ +static inline uintptr_t arch_fptr_next(uintptr_t fptr) { + return arch_fptr_ret_addr(fptr + 4); +} + +/** \brief Set up new stack before running. + + This function does nothing as it is unnecessary on Dreamcast. + + \param nt A pointer to the new thread for which a stack + is to be set up. +*/ +void arch_stk_setup(kthread_t *nt); + /** \brief Do a stack trace from the current function. This function does a stack trace from the current function, printing the diff --git a/kernel/arch/dreamcast/kernel/mm.c b/kernel/arch/dreamcast/kernel/mm.c index 37641fe0..03cfe32c 100644 --- a/kernel/arch/dreamcast/kernel/mm.c +++ b/kernel/arch/dreamcast/kernel/mm.c @@ -13,10 +13,10 @@ /* Note: right now we only support system RAM */ - -#include <arch/types.h> #include <arch/arch.h> +#include <arch/types.h> #include <arch/irq.h> +#include <arch/stack.h> #include <kos/dbglog.h> #include <errno.h> #include <stdio.h> diff --git a/kernel/arch/dreamcast/kernel/stack.c b/kernel/arch/dreamcast/kernel/stack.c index 31dffb16..32404b92 100644 --- a/kernel/arch/dreamcast/kernel/stack.c +++ b/kernel/arch/dreamcast/kernel/stack.c @@ -7,10 +7,7 @@ /* Functions to tinker with the stack, including obtaining a stack trace when frame pointers are enabled. If frame pointers are enabled, then you'll need to also define FRAME_POINTERS to get support for stack - traces. - - We could probably technically move this into arch indep with a bit more - work... */ + traces. */ #include <kos/dbgio.h> #include <arch/arch.h> @@ -23,6 +20,11 @@ static uintptr_t arch_stack_32m_dft = 0x8e000000; extern uintptr_t arch_stack_16m __attribute__((weak,alias("arch_stack_16m_dft"))); extern uintptr_t arch_stack_32m __attribute__((weak,alias("arch_stack_32m_dft"))); +/* This function is unnecessary and does nothing on Dreamcast */ +void arch_stk_setup(kthread_t *nt) { + (void)nt; +} + /* Do a stack trace from the current function; leave off the first n frames (i.e., in assert()). */ __noinline void arch_stk_trace(int n) { diff --git a/kernel/thread/thread.c b/kernel/thread/thread.c index 36e57aa1..66d9a5af 100644 --- a/kernel/thread/thread.c +++ b/kernel/thread/thread.c @@ -26,9 +26,10 @@ #include <kos/cond.h> #include <kos/genwait.h> +#include <arch/arch.h> #include <arch/irq.h> +#include <arch/stack.h> #include <arch/timer.h> -#include <arch/arch.h> #include <arch/tls_static.h> /* @@ -361,8 +362,11 @@ int thd_remove_from_runnable(kthread_t *thd) { } /* New thread function; given a routine address, it will create a - new kernel thread with the given attributes. When the routine - returns, the thread will exit. Returns the new thread struct. */ + new thread with the given attributes. When the routine returns, + the thread will exit. Returns the new thread struct. + Note that this function is also used in thd_init() to add the + already running kernel thread to the thread scheduler. This is + the only circumstance in which routine should be NULL. */ kthread_t *thd_create_ex(const kthread_attr_t *restrict attr, void *(*routine)(void *param), void *param) { kthread_t *nt = NULL; @@ -430,6 +434,13 @@ kthread_t *thd_create_ex(const kthread_attr_t *restrict attr, ((uint32_t)nt->stack) + nt->stack_size, (uint32_t)thd_birth, params, 0); + /* Some architectures require setting up a new stack before use. + We won't do this if routine is NULL, however, as this means + we are creating the kernel thread, which is already running. */ + if(routine) { + arch_stk_setup(nt); + } + /* Create static TLS data if the thread hasn't disabled it. */ if(real_attr.disable_tls) { nt->flags |= THD_DISABLE_TLS; hooks/post-receive -- A pseudo Operating System for the Dreamcast. |