From 6cd7936f575e511f23bf75288f0dae25bad2fa51 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Fri, 22 Mar 2024 14:03:37 -0700 Subject: [PATCH] kernel: align kernel stack size declaration When kernel stack is defined as an array, K_KERNEL_STACK_LEN() is used to calculate the size for each stack in the array. However, standalone kernel stack has its size calculated by Z_KERNEL_STACK_SIZE_ADJUST() instead. Depending on the arch alignment requirement, they may not be the same... which could cause some confusions. So align them both to use K_KERNEL_STACK_LEN(). Signed-off-by: Daniel Leung --- arch/x86/core/intel64/cpu.c | 4 ++-- include/zephyr/kernel/thread_stack.h | 4 ++-- include/zephyr/posix/pthread.h | 2 +- kernel/dynamic.c | 2 +- kernel/thread.c | 2 +- .../source/COMPONENT_ZEPHYR/cyabs_rtos_zephyr.c | 2 +- tests/kernel/threads/dynamic_thread_stack/src/main.c | 2 +- tests/kernel/threads/thread_stack/src/main.c | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/x86/core/intel64/cpu.c b/arch/x86/core/intel64/cpu.c index 3c17e2de593..d1aedcd4dec 100644 --- a/arch/x86/core/intel64/cpu.c +++ b/arch/x86/core/intel64/cpu.c @@ -107,9 +107,9 @@ struct x86_cpuboot x86_cpuboot[] = { .tr = X86_KERNEL_CPU0_TR, .gs_base = &tss0, .sp = (uint64_t) z_interrupt_stacks[0] + - Z_KERNEL_STACK_SIZE_ADJUST(CONFIG_ISR_STACK_SIZE), + K_KERNEL_STACK_LEN(CONFIG_ISR_STACK_SIZE), .stack_size = - Z_KERNEL_STACK_SIZE_ADJUST(CONFIG_ISR_STACK_SIZE), + K_KERNEL_STACK_LEN(CONFIG_ISR_STACK_SIZE), .fn = z_prep_c, .arg = &x86_cpu_boot_arg, }, diff --git a/include/zephyr/kernel/thread_stack.h b/include/zephyr/kernel/thread_stack.h index a4bad55b9da..3598742a249 100644 --- a/include/zephyr/kernel/thread_stack.h +++ b/include/zephyr/kernel/thread_stack.h @@ -123,7 +123,7 @@ static inline char *z_stack_ptr_align(char *ptr) */ #define K_KERNEL_STACK_DECLARE(sym, size) \ extern struct z_thread_stack_element \ - sym[Z_KERNEL_STACK_SIZE_ADJUST(size)] + sym[K_KERNEL_STACK_LEN(size)] /** * @brief Declare a reference to a thread stack array @@ -175,7 +175,7 @@ static inline char *z_stack_ptr_align(char *ptr) #define Z_KERNEL_STACK_DEFINE_IN(sym, size, lsect) \ struct z_thread_stack_element lsect \ __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \ - sym[Z_KERNEL_STACK_SIZE_ADJUST(size)] + sym[K_KERNEL_STACK_LEN(size)] /** * @brief Define a toplevel array of kernel stack memory regions in specified section diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index eb786f80e19..d950c9ea7fd 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -49,7 +49,7 @@ extern "C" { #define PTHREAD_ONCE_INIT {0} /* The minimum allowable stack size */ -#define PTHREAD_STACK_MIN Z_KERNEL_STACK_SIZE_ADJUST(0) +#define PTHREAD_STACK_MIN K_KERNEL_STACK_LEN(0) /** * @brief Declare a condition variable as initialized diff --git a/kernel/dynamic.c b/kernel/dynamic.c index 66cd99853c0..d03e3669346 100644 --- a/kernel/dynamic.c +++ b/kernel/dynamic.c @@ -75,7 +75,7 @@ static k_thread_stack_t *stack_alloc_dyn(size_t size, int flags) } return z_thread_stack_alloc_dyn(Z_KERNEL_STACK_OBJ_ALIGN, - Z_KERNEL_STACK_SIZE_ADJUST(size)); + K_KERNEL_STACK_LEN(size)); } k_thread_stack_t *z_impl_k_thread_stack_alloc(size_t size, int flags) diff --git a/kernel/thread.c b/kernel/thread.c index 8e3739e25c0..5d705e114b1 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -391,7 +391,7 @@ static char *setup_thread_stack(struct k_thread *new_thread, #endif /* CONFIG_USERSPACE */ { /* Object cannot host a user mode thread */ - stack_obj_size = Z_KERNEL_STACK_SIZE_ADJUST(stack_size); + stack_obj_size = K_KERNEL_STACK_LEN(stack_size); stack_buf_start = K_KERNEL_STACK_BUFFER(stack); stack_buf_size = stack_obj_size - K_KERNEL_STACK_RESERVED; diff --git a/modules/hal_infineon/abstraction-rtos/source/COMPONENT_ZEPHYR/cyabs_rtos_zephyr.c b/modules/hal_infineon/abstraction-rtos/source/COMPONENT_ZEPHYR/cyabs_rtos_zephyr.c index 7c7197fafbd..3e1acbbc17b 100644 --- a/modules/hal_infineon/abstraction-rtos/source/COMPONENT_ZEPHYR/cyabs_rtos_zephyr.c +++ b/modules/hal_infineon/abstraction-rtos/source/COMPONENT_ZEPHYR/cyabs_rtos_zephyr.c @@ -122,7 +122,7 @@ cy_rslt_t cy_rtos_create_thread(cy_thread_t *thread, cy_thread_entry_fn_t entry_ /* Allocate stack if NULL was passed */ if ((uint32_t *)stack == NULL) { stack_alloc = k_aligned_alloc(Z_KERNEL_STACK_OBJ_ALIGN, - Z_KERNEL_STACK_SIZE_ADJUST(stack_size)); + K_KERNEL_STACK_LEN(stack_size)); /* Store pointer to allocated stack, * NULL if not allocated by cy_rtos_thread_create (passed by application). diff --git a/tests/kernel/threads/dynamic_thread_stack/src/main.c b/tests/kernel/threads/dynamic_thread_stack/src/main.c index 1e93beb3c66..c2446e04d68 100644 --- a/tests/kernel/threads/dynamic_thread_stack/src/main.c +++ b/tests/kernel/threads/dynamic_thread_stack/src/main.c @@ -14,7 +14,7 @@ #ifdef CONFIG_USERSPACE #define STACK_OBJ_SIZE Z_THREAD_STACK_SIZE_ADJUST(CONFIG_DYNAMIC_THREAD_STACK_SIZE) #else -#define STACK_OBJ_SIZE Z_KERNEL_STACK_SIZE_ADJUST(CONFIG_DYNAMIC_THREAD_STACK_SIZE) +#define STACK_OBJ_SIZE K_KERNEL_STACK_LEN(CONFIG_DYNAMIC_THREAD_STACK_SIZE) #endif #define MAX_HEAP_STACKS (POOL_SIZE / STACK_OBJ_SIZE) diff --git a/tests/kernel/threads/thread_stack/src/main.c b/tests/kernel/threads/thread_stack/src/main.c index 64651dacee7..df68c060323 100644 --- a/tests/kernel/threads/thread_stack/src/main.c +++ b/tests/kernel/threads/thread_stack/src/main.c @@ -235,7 +235,7 @@ void stack_buffer_scenarios(void) * K_THREAD_STACK_SIZEOF(my_stack) * * K_KERNEL_STACK_DEFINE(my_kern_stack, Y): - * Z_KERNEL_STACK_SIZE_ADJUST(Y) - K_KERNEL_STACK_RESERVED == + * K_KERNEL_STACK_LEN(Y) - K_KERNEL_STACK_RESERVED == * K_KERNEL_STACK_SIZEOF(my_stack) */ #ifdef CONFIG_USERSPACE @@ -245,7 +245,7 @@ void stack_buffer_scenarios(void) } else #endif { - adjusted = Z_KERNEL_STACK_SIZE_ADJUST(scenario_data.declared_size); + adjusted = K_KERNEL_STACK_LEN(scenario_data.declared_size); } adjusted -= reserved;