diff --git a/arch/arc/core/reset.S b/arch/arc/core/reset.S index 8bc582dca46..7fe025e191a 100644 --- a/arch/arc/core/reset.S +++ b/arch/arc/core/reset.S @@ -17,7 +17,7 @@ #include GDATA(_interrupt_stack) -GDATA(_main_stack) +GDATA(z_main_stack) GDATA(_VectorTable) /* use one of the available interrupt stacks during init */ @@ -154,7 +154,7 @@ _master_core_startup: * FIRQ stack when CONFIG_INIT_STACKS is enabled before switching to * one of them for the rest of the early boot */ - mov_s sp, _main_stack + mov_s sp, z_main_stack add sp, sp, CONFIG_MAIN_STACK_SIZE mov_s r0, _interrupt_stack diff --git a/arch/arm/core/cortex_m/vector_table.S b/arch/arm/core/cortex_m/vector_table.S index 0be3b3b2a8d..137f35ef59d 100644 --- a/arch/arm/core/cortex_m/vector_table.S +++ b/arch/arm/core/cortex_m/vector_table.S @@ -22,7 +22,7 @@ _ASM_FILE_PROLOGUE -GDATA(_main_stack) +GDATA(z_main_stack) SECTION_SUBSEC_FUNC(exc_vector_table,_vector_table_section,_vector_table) @@ -31,7 +31,7 @@ SECTION_SUBSEC_FUNC(exc_vector_table,_vector_table_section,_vector_table) * on the interrupt stack when CONFIG_INIT_STACKS is enabled before * switching to the interrupt stack for the rest of the early boot */ - .word _main_stack + CONFIG_MAIN_STACK_SIZE + .word z_main_stack + CONFIG_MAIN_STACK_SIZE .word __reset .word __nmi diff --git a/arch/arm/core/irq_relay.S b/arch/arm/core/irq_relay.S index a5d030c93b0..04233f24c4a 100644 --- a/arch/arm/core/irq_relay.S +++ b/arch/arm/core/irq_relay.S @@ -29,7 +29,7 @@ _ASM_FILE_PROLOGUE #if defined(CONFIG_SW_VECTOR_RELAY) GDATA(_vector_table_pointer) -GDATA(_main_stack) +GDATA(z_main_stack) SECTION_FUNC(vector_relay_handler, __vector_relay_handler) mrs r0, ipsr; @@ -52,7 +52,7 @@ SECTION_FUNC(vector_relay_handler, __vector_relay_handler) GTEXT(__vector_relay_handler) SECTION_FUNC(vector_relay_table, __vector_relay_table) - .word _main_stack + CONFIG_MAIN_STACK_SIZE + .word z_main_stack + CONFIG_MAIN_STACK_SIZE .word __reset diff --git a/kernel/include/kernel_internal.h b/kernel/include/kernel_internal.h index 13fcb7dd88a..c748b65a3cd 100644 --- a/kernel/include/kernel_internal.h +++ b/kernel/include/kernel_internal.h @@ -300,6 +300,11 @@ extern u32_t z_timestamp_main; /* timestamp when main task starts */ extern u32_t z_timestamp_idle; /* timestamp when CPU goes idle */ #endif +extern struct k_thread z_main_thread; +extern struct k_thread z_idle_thread; +extern K_THREAD_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE); +extern K_THREAD_STACK_DEFINE(z_idle_stack, CONFIG_IDLE_STACK_SIZE); + #ifdef __cplusplus } #endif diff --git a/kernel/init.c b/kernel/init.c index 8ecc7d0efd1..556bf35d5d7 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -71,18 +71,11 @@ u32_t __noinit z_timestamp_idle; /* timestamp when CPU goes idle */ #endif /* init/main and idle threads */ +K_THREAD_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE); +K_THREAD_STACK_DEFINE(z_idle_stack, CONFIG_IDLE_STACK_SIZE); -#define IDLE_STACK_SIZE CONFIG_IDLE_STACK_SIZE -#define MAIN_STACK_SIZE CONFIG_MAIN_STACK_SIZE - -K_THREAD_STACK_DEFINE(_main_stack, MAIN_STACK_SIZE); -K_THREAD_STACK_DEFINE(_idle_stack, IDLE_STACK_SIZE); - -static struct k_thread _main_thread_s; -static struct k_thread _idle_thread_s; - -k_tid_t const _main_thread = (k_tid_t)&_main_thread_s; -k_tid_t const _idle_thread = (k_tid_t)&_idle_thread_s; +struct k_thread z_main_thread; +struct k_thread z_idle_thread; /* * storage space for the interrupt stack @@ -101,21 +94,21 @@ K_THREAD_STACK_DEFINE(_interrupt_stack, CONFIG_ISR_STACK_SIZE); * clean this up in the future. */ #if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 1 -K_THREAD_STACK_DEFINE(_idle_stack1, IDLE_STACK_SIZE); +K_THREAD_STACK_DEFINE(_idle_stack1, CONFIG_IDLE_STACK_SIZE); static struct k_thread _idle_thread1_s; k_tid_t const _idle_thread1 = (k_tid_t)&_idle_thread1_s; K_THREAD_STACK_DEFINE(_interrupt_stack1, CONFIG_ISR_STACK_SIZE); #endif #if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 2 -K_THREAD_STACK_DEFINE(_idle_stack2, IDLE_STACK_SIZE); +K_THREAD_STACK_DEFINE(_idle_stack2, CONFIG_IDLE_STACK_SIZE); static struct k_thread _idle_thread2_s; k_tid_t const _idle_thread2 = (k_tid_t)&_idle_thread2_s; K_THREAD_STACK_DEFINE(_interrupt_stack2, CONFIG_ISR_STACK_SIZE); #endif #if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 3 -K_THREAD_STACK_DEFINE(_idle_stack3, IDLE_STACK_SIZE); +K_THREAD_STACK_DEFINE(_idle_stack3, CONFIG_IDLE_STACK_SIZE); static struct k_thread _idle_thread3_s; k_tid_t const _idle_thread3 = (k_tid_t)&_idle_thread3_s; K_THREAD_STACK_DEFINE(_interrupt_stack3, CONFIG_ISR_STACK_SIZE); @@ -291,7 +284,7 @@ static void bg_thread_main(void *unused1, void *unused2, void *unused3) main(); /* Mark nonessenrial since main() has no more work to do */ - _main_thread->base.user_options &= ~K_ESSENTIAL; + z_main_thread.base.user_options &= ~K_ESSENTIAL; /* Dump coverage data once the main() has exited. */ gcov_coverage_dump(); @@ -311,7 +304,7 @@ void __weak main(void) static void init_idle_thread(struct k_thread *thr, k_thread_stack_t *stack) { z_setup_new_thread(thr, stack, - IDLE_STACK_SIZE, idle, NULL, NULL, NULL, + CONFIG_IDLE_STACK_SIZE, idle, NULL, NULL, NULL, K_LOWEST_THREAD_PRIO, K_ESSENTIAL, IDLE_THREAD_NAME); z_mark_thread_as_started(thr); @@ -371,21 +364,21 @@ static void prepare_multithreading(struct k_thread *dummy_thread) * contain garbage, which would prevent the cache loading algorithm * to work as intended */ - _kernel.ready_q.cache = _main_thread; + _kernel.ready_q.cache = &z_main_thread; #endif - z_setup_new_thread(_main_thread, _main_stack, - MAIN_STACK_SIZE, bg_thread_main, - NULL, NULL, NULL, - CONFIG_MAIN_THREAD_PRIORITY, K_ESSENTIAL, "main"); - sys_trace_thread_create(_main_thread); + z_setup_new_thread(&z_main_thread, z_main_stack, + CONFIG_MAIN_STACK_SIZE, bg_thread_main, + NULL, NULL, NULL, + CONFIG_MAIN_THREAD_PRIORITY, K_ESSENTIAL, "main"); + sys_trace_thread_create(&z_main_thread); - z_mark_thread_as_started(_main_thread); - z_ready_thread(_main_thread); + z_mark_thread_as_started(&z_main_thread); + z_ready_thread(&z_main_thread); - init_idle_thread(_idle_thread, _idle_stack); - _kernel.cpus[0].idle_thread = _idle_thread; - sys_trace_thread_create(_idle_thread); + init_idle_thread(&z_idle_thread, z_idle_stack); + _kernel.cpus[0].idle_thread = &z_idle_thread; + sys_trace_thread_create(&z_idle_thread); #if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 1 init_idle_thread(_idle_thread1, _idle_stack1); @@ -418,9 +411,9 @@ static void prepare_multithreading(struct k_thread *dummy_thread) static FUNC_NORETURN void switch_to_main_thread(void) { #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN - z_arch_switch_to_main_thread(_main_thread, _main_stack, - K_THREAD_STACK_SIZEOF(_main_stack), - bg_thread_main); + z_arch_switch_to_main_thread(&z_main_thread, z_main_stack, + K_THREAD_STACK_SIZEOF(z_main_stack), + bg_thread_main); #else /* * Context switch to main task (entry function is _main()): the diff --git a/kernel/sched.c b/kernel/sched.c index 0cd583137a1..7daced12ae5 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -13,6 +13,7 @@ #include #include #include +#include #if defined(CONFIG_SCHED_DUMB) #define _priq_run_add z_priq_dumb_add @@ -84,9 +85,7 @@ static inline bool is_idle(struct k_thread *thread) #ifdef CONFIG_SMP return thread->base.is_idle; #else - extern k_tid_t const _idle_thread; - - return thread == _idle_thread; + return thread == &z_idle_thread; #endif } diff --git a/lib/cmsis_rtos_v1/cmsis_kernel.c b/lib/cmsis_rtos_v1/cmsis_kernel.c index 4b9ae265a83..71c049bb029 100644 --- a/lib/cmsis_rtos_v1/cmsis_kernel.c +++ b/lib/cmsis_rtos_v1/cmsis_kernel.c @@ -7,8 +7,7 @@ #include #include #include - -extern const k_tid_t _main_thread; +#include /** * @brief Get the RTOS kernel system timer counter @@ -42,5 +41,5 @@ osStatus osKernelStart(void) */ int32_t osKernelRunning(void) { - return z_has_thread_started(_main_thread); + return z_has_thread_started(&z_main_thread); } diff --git a/subsys/debug/tracing/cpu_stats.c b/subsys/debug/tracing/cpu_stats.c index e66d0fde56e..bea81459010 100644 --- a/subsys/debug/tracing/cpu_stats.c +++ b/subsys/debug/tracing/cpu_stats.c @@ -6,6 +6,7 @@ #include #include +#include enum cpu_state { CPU_STATE_IDLE, @@ -21,16 +22,12 @@ static struct cpu_stats stats_hw_tick; static int nested_interrupts; static struct k_thread *current_thread; -#ifndef CONFIG_SMP -extern k_tid_t const _idle_thread; -#endif - static int is_idle_thread(struct k_thread *thread) { #ifdef CONFIG_SMP return thread->base.is_idle; #else - return thread == _idle_thread; + return thread == &z_idle_thread; #endif } diff --git a/subsys/debug/tracing/ctf/ctf_top.c b/subsys/debug/tracing/ctf/ctf_top.c index 9bd46ec1e19..7eeec66d0a4 100644 --- a/subsys/debug/tracing/ctf/ctf_top.c +++ b/subsys/debug/tracing/ctf/ctf_top.c @@ -7,19 +7,15 @@ #include #include #include +#include #include "ctf_top.h" - -#ifndef CONFIG_SMP -extern k_tid_t const _idle_thread; -#endif - static inline int is_idle_thread(struct k_thread *thread) { #ifdef CONFIG_SMP return thread->base.is_idle; #else - return thread == _idle_thread; + return thread == &z_idle_thread; #endif } diff --git a/subsys/debug/tracing/include/tracing_sysview.h b/subsys/debug/tracing/include/tracing_sysview.h index acd890305e1..98061da72d2 100644 --- a/subsys/debug/tracing/include/tracing_sysview.h +++ b/subsys/debug/tracing/include/tracing_sysview.h @@ -7,21 +7,18 @@ #define _TRACE_SYSVIEW_H #include #include +#include #include #include #include "SEGGER_SYSVIEW_Zephyr.h" -#ifndef CONFIG_SMP -extern k_tid_t const _idle_thread; -#endif - static inline int is_idle_thread(struct k_thread *thread) { #ifdef CONFIG_SMP return thread->base.is_idle; #else - return thread == _idle_thread; + return thread == &z_idle_thread; #endif } diff --git a/subsys/net/ip/net_shell.c b/subsys/net/ip/net_shell.c index eb7f2196f28..f377c79a1ce 100644 --- a/subsys/net/ip/net_shell.c +++ b/subsys/net/ip/net_shell.c @@ -14,6 +14,7 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG); #include +#include #include #include #include @@ -3248,7 +3249,6 @@ static int cmd_net_route(const struct shell *shell, size_t argc, char *argv[]) } #if defined(CONFIG_INIT_STACKS) -extern K_THREAD_STACK_DEFINE(_main_stack, CONFIG_MAIN_STACK_SIZE); extern K_THREAD_STACK_DEFINE(_interrupt_stack, CONFIG_ISR_STACK_SIZE); extern K_THREAD_STACK_DEFINE(sys_work_q_stack, CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE); @@ -3294,11 +3294,11 @@ static int cmd_net_stacks(const struct shell *shell, size_t argc, } #if defined(CONFIG_INIT_STACKS) - net_analyze_stack_get_values(Z_THREAD_STACK_BUFFER(_main_stack), - K_THREAD_STACK_SIZEOF(_main_stack), + net_analyze_stack_get_values(Z_THREAD_STACK_BUFFER(z_main_stack), + K_THREAD_STACK_SIZEOF(z_main_stack), &pcnt, &unused); PR("%s [%s] stack size %d/%d bytes unused %u usage %d/%d (%u %%)\n", - "main", "_main_stack", CONFIG_MAIN_STACK_SIZE, + "main", "z_main_stack", CONFIG_MAIN_STACK_SIZE, CONFIG_MAIN_STACK_SIZE, unused, CONFIG_MAIN_STACK_SIZE - unused, CONFIG_MAIN_STACK_SIZE, pcnt); diff --git a/tests/kernel/mem_protect/obj_validation/src/main.c b/tests/kernel/mem_protect/obj_validation/src/main.c index b06144e7437..ced9ef80b16 100644 --- a/tests/kernel/mem_protect/obj_validation/src/main.c +++ b/tests/kernel/mem_protect/obj_validation/src/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #define SEM_ARRAY_SIZE 16 @@ -65,8 +66,6 @@ void object_permission_checks(struct k_sem *sem, bool skip_init) "object should have had sufficient permissions"); } -extern const k_tid_t _main_thread; - /** * @brief Tests to verify object permission * @@ -93,7 +92,7 @@ void test_generic_object(void) /* Give an extra reference to another thread so the object * doesn't disappear if we revoke our own */ - k_object_access_grant(dyn_sem[i], _main_thread); + k_object_access_grant(dyn_sem[i], &z_main_thread); } /* dynamic object table well-populated with semaphores at this point */ diff --git a/tests/kernel/threads/thread_apis/src/main.c b/tests/kernel/threads/thread_apis/src/main.c index d6f722614ce..940c30f0699 100644 --- a/tests/kernel/threads/thread_apis/src/main.c +++ b/tests/kernel/threads/thread_apis/src/main.c @@ -146,7 +146,6 @@ void test_thread_name_get_set(void) #ifdef CONFIG_USERSPACE static char unreadable_string[64]; static char not_my_buffer[CONFIG_THREAD_MAX_NAME_LEN]; -ZTEST_BMEM struct k_thread *main_thread_ptr; struct k_sem sem; #endif /* CONFIG_USERSPACE */ @@ -169,7 +168,7 @@ void test_thread_name_user_get_set(void) zassert_equal(ret, -EFAULT, "accepted unreadable string"); ret = k_thread_name_set((struct k_thread *)&sem, "some name"); zassert_equal(ret, -EINVAL, "accepted non-thread object"); - ret = k_thread_name_set(main_thread_ptr, "some name"); + ret = k_thread_name_set(&z_main_thread, "some name"); zassert_equal(ret, -EINVAL, "no permission on thread object"); /* Set and get current thread's name */ @@ -191,7 +190,7 @@ void test_thread_name_user_get_set(void) ret = k_thread_name_copy((struct k_thread *)&sem, thread_name, sizeof(thread_name)); zassert_equal(ret, -EINVAL, "not a thread object"); - ret = k_thread_name_copy(main_thread_ptr, thread_name, + ret = k_thread_name_copy(&z_main_thread, thread_name, sizeof(thread_name)); zassert_equal(ret, 0, "couldn't get main thread name"); printk("Main thread name is '%s'\n", thread_name); @@ -270,8 +269,6 @@ void test_user_mode(void) } #endif -extern const k_tid_t _main_thread; - void test_main(void) { k_thread_access_grant(k_current_get(), &tdata, tstack); @@ -281,8 +278,6 @@ void test_main(void) #ifdef CONFIG_USERSPACE strncpy(unreadable_string, "unreadable string", sizeof(unreadable_string)); - /* Copy so user mode can get at it */ - main_thread_ptr = _main_thread; #endif ztest_test_suite(threads_lifecycle,