From 28fddb7045da6da402c1399e7df6d26b98239a7b Mon Sep 17 00:00:00 2001 From: Ioannis Glaropoulos Date: Fri, 24 May 2019 15:25:30 +0200 Subject: [PATCH 1/2] kernel: init: use K_THREAD_STACK_SIZEOF when switching to main thread For architectures with custom swap to main, currently: - arm - posix we are now using K_THREAD_STACK_SIZEOF macro to pass the main thread stack size to z_arch_switch_to_main_thread(). This does not introduce any behavioral changes for posix; the K_THREAD_STACK_SIZEOF() simply returns the sizeof() the stack object. For Arm, this allows us to clean-up one more occurence of CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT in kernel_arch_func.h. Signed-off-by: Ioannis Glaropoulos --- arch/arm/include/kernel_arch_func.h | 8 +------- kernel/init.c | 3 ++- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/arch/arm/include/kernel_arch_func.h b/arch/arm/include/kernel_arch_func.h index 980ae388db4f5..0a21dff9c0f2c 100644 --- a/arch/arm/include/kernel_arch_func.h +++ b/arch/arm/include/kernel_arch_func.h @@ -74,15 +74,9 @@ z_arch_switch_to_main_thread(struct k_thread *main_thread, /* get high address of the stack, i.e. its start (stack grows down) */ char *start_of_main_stack; -#if defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) && \ - defined(CONFIG_USERSPACE) - start_of_main_stack = - Z_THREAD_STACK_BUFFER(main_stack) + main_stack_size - - MPU_GUARD_ALIGN_AND_SIZE; -#else start_of_main_stack = Z_THREAD_STACK_BUFFER(main_stack) + main_stack_size; -#endif + start_of_main_stack = (char *)STACK_ROUND_DOWN(start_of_main_stack); #ifdef CONFIG_TRACING diff --git a/kernel/init.c b/kernel/init.c index 276912f649695..c26f84bb7dbf0 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -411,7 +411,8 @@ static void prepare_multithreading(struct k_thread *dummy_thread) static void switch_to_main_thread(void) { #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN - z_arch_switch_to_main_thread(_main_thread, _main_stack, MAIN_STACK_SIZE, + z_arch_switch_to_main_thread(_main_thread, _main_stack, + K_THREAD_STACK_SIZEOF(_main_stack), bg_thread_main); #else /* From bf28d4119042c5c6835990124cad35c1c485d659 Mon Sep 17 00:00:00 2001 From: Ioannis Glaropoulos Date: Fri, 24 May 2019 15:31:45 +0200 Subject: [PATCH 2/2] kernel: remove redundant #ifdef CONFIG_MULTITHREADING Remove a redundant #ifdef CONFIG_MULTITHREADING guard for a code block already inside CONFIG_MULTITHREADING. Add some inline #endif comments for ease of reading. Signed-off-by: Ioannis Glaropoulos --- kernel/init.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/init.c b/kernel/init.c index c26f84bb7dbf0..647ba242dfc0d 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -303,7 +303,7 @@ static void init_idle_thread(struct k_thread *thr, k_thread_stack_t *stack) K_LOWEST_THREAD_PRIO, K_ESSENTIAL, IDLE_THREAD_NAME); z_mark_thread_as_started(thr); } -#endif +#endif /* CONFIG_MULTITHREADING */ /** * @@ -347,7 +347,7 @@ static void prepare_multithreading(struct k_thread *dummy_thread) #ifdef CONFIG_USERSPACE dummy_thread->mem_domain_info.mem_domain = 0; #endif -#endif +#endif /* CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN */ /* _kernel.ready_q is all zeroes */ z_sched_init(); @@ -374,11 +374,9 @@ static void prepare_multithreading(struct k_thread *dummy_thread) z_mark_thread_as_started(_main_thread); z_ready_thread(_main_thread); -#ifdef CONFIG_MULTITHREADING init_idle_thread(_idle_thread, _idle_stack); _kernel.cpus[0].idle_thread = _idle_thread; sys_trace_thread_create(_idle_thread); -#endif #if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 1 init_idle_thread(_idle_thread1, _idle_stack1);