kernel: rename main/idle thread/stacks

The main and idle threads, and their associated stacks,
were being referenced in various parts of the kernel
with no central definition. Expose these in kernel_internal.h
and namespace with z_ appropriately.

The main and idle threads were being defined statically,
with another variable exposed to contain their pointer
value. This wastes a bit of memory and isn't accessible
to user threads anyway, just expose the actual thread
objects.

Redundance MAIN_STACK_SIZE and IDLE_STACK_SIZE defines
in init.c removed, just use the Kconfigs they derive
from.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-09-21 17:54:37 -07:00 committed by Anas Nashif
commit fe031611fd
13 changed files with 52 additions and 72 deletions

View file

@ -17,7 +17,7 @@
#include <swap_macros.h> #include <swap_macros.h>
GDATA(_interrupt_stack) GDATA(_interrupt_stack)
GDATA(_main_stack) GDATA(z_main_stack)
GDATA(_VectorTable) GDATA(_VectorTable)
/* use one of the available interrupt stacks during init */ /* 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 * FIRQ stack when CONFIG_INIT_STACKS is enabled before switching to
* one of them for the rest of the early boot * 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 add sp, sp, CONFIG_MAIN_STACK_SIZE
mov_s r0, _interrupt_stack mov_s r0, _interrupt_stack

View file

@ -22,7 +22,7 @@
_ASM_FILE_PROLOGUE _ASM_FILE_PROLOGUE
GDATA(_main_stack) GDATA(z_main_stack)
SECTION_SUBSEC_FUNC(exc_vector_table,_vector_table_section,_vector_table) 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 * on the interrupt stack when CONFIG_INIT_STACKS is enabled before
* switching to the interrupt stack for the rest of the early boot * 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 __reset
.word __nmi .word __nmi

View file

@ -29,7 +29,7 @@ _ASM_FILE_PROLOGUE
#if defined(CONFIG_SW_VECTOR_RELAY) #if defined(CONFIG_SW_VECTOR_RELAY)
GDATA(_vector_table_pointer) GDATA(_vector_table_pointer)
GDATA(_main_stack) GDATA(z_main_stack)
SECTION_FUNC(vector_relay_handler, __vector_relay_handler) SECTION_FUNC(vector_relay_handler, __vector_relay_handler)
mrs r0, ipsr; mrs r0, ipsr;
@ -52,7 +52,7 @@ SECTION_FUNC(vector_relay_handler, __vector_relay_handler)
GTEXT(__vector_relay_handler) GTEXT(__vector_relay_handler)
SECTION_FUNC(vector_relay_table, __vector_relay_table) 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 .word __reset

View file

@ -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 */ extern u32_t z_timestamp_idle; /* timestamp when CPU goes idle */
#endif #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 #ifdef __cplusplus
} }
#endif #endif

View file

@ -71,18 +71,11 @@ u32_t __noinit z_timestamp_idle; /* timestamp when CPU goes idle */
#endif #endif
/* init/main and idle threads */ /* 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 struct k_thread z_main_thread;
#define MAIN_STACK_SIZE CONFIG_MAIN_STACK_SIZE struct k_thread z_idle_thread;
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;
/* /*
* storage space for the interrupt stack * 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. * clean this up in the future.
*/ */
#if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 1 #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; static struct k_thread _idle_thread1_s;
k_tid_t const _idle_thread1 = (k_tid_t)&_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); K_THREAD_STACK_DEFINE(_interrupt_stack1, CONFIG_ISR_STACK_SIZE);
#endif #endif
#if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 2 #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; static struct k_thread _idle_thread2_s;
k_tid_t const _idle_thread2 = (k_tid_t)&_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); K_THREAD_STACK_DEFINE(_interrupt_stack2, CONFIG_ISR_STACK_SIZE);
#endif #endif
#if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 3 #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; static struct k_thread _idle_thread3_s;
k_tid_t const _idle_thread3 = (k_tid_t)&_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); 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(); main();
/* Mark nonessenrial since main() has no more work to do */ /* 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. */ /* Dump coverage data once the main() has exited. */
gcov_coverage_dump(); 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) static void init_idle_thread(struct k_thread *thr, k_thread_stack_t *stack)
{ {
z_setup_new_thread(thr, 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); K_LOWEST_THREAD_PRIO, K_ESSENTIAL, IDLE_THREAD_NAME);
z_mark_thread_as_started(thr); 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 * contain garbage, which would prevent the cache loading algorithm
* to work as intended * to work as intended
*/ */
_kernel.ready_q.cache = _main_thread; _kernel.ready_q.cache = &z_main_thread;
#endif #endif
z_setup_new_thread(_main_thread, _main_stack, z_setup_new_thread(&z_main_thread, z_main_stack,
MAIN_STACK_SIZE, bg_thread_main, CONFIG_MAIN_STACK_SIZE, bg_thread_main,
NULL, NULL, NULL, NULL, NULL, NULL,
CONFIG_MAIN_THREAD_PRIORITY, K_ESSENTIAL, "main"); CONFIG_MAIN_THREAD_PRIORITY, K_ESSENTIAL, "main");
sys_trace_thread_create(_main_thread); sys_trace_thread_create(&z_main_thread);
z_mark_thread_as_started(_main_thread); z_mark_thread_as_started(&z_main_thread);
z_ready_thread(_main_thread); z_ready_thread(&z_main_thread);
init_idle_thread(_idle_thread, _idle_stack); init_idle_thread(&z_idle_thread, z_idle_stack);
_kernel.cpus[0].idle_thread = _idle_thread; _kernel.cpus[0].idle_thread = &z_idle_thread;
sys_trace_thread_create(_idle_thread); sys_trace_thread_create(&z_idle_thread);
#if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 1 #if defined(CONFIG_SMP) && CONFIG_MP_NUM_CPUS > 1
init_idle_thread(_idle_thread1, _idle_stack1); 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) static FUNC_NORETURN void switch_to_main_thread(void)
{ {
#ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN
z_arch_switch_to_main_thread(_main_thread, _main_stack, z_arch_switch_to_main_thread(&z_main_thread, z_main_stack,
K_THREAD_STACK_SIZEOF(_main_stack), K_THREAD_STACK_SIZEOF(z_main_stack),
bg_thread_main); bg_thread_main);
#else #else
/* /*
* Context switch to main task (entry function is _main()): the * Context switch to main task (entry function is _main()): the

View file

@ -13,6 +13,7 @@
#include <syscall_handler.h> #include <syscall_handler.h>
#include <drivers/timer/system_timer.h> #include <drivers/timer/system_timer.h>
#include <stdbool.h> #include <stdbool.h>
#include <kernel_internal.h>
#if defined(CONFIG_SCHED_DUMB) #if defined(CONFIG_SCHED_DUMB)
#define _priq_run_add z_priq_dumb_add #define _priq_run_add z_priq_dumb_add
@ -84,9 +85,7 @@ static inline bool is_idle(struct k_thread *thread)
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
return thread->base.is_idle; return thread->base.is_idle;
#else #else
extern k_tid_t const _idle_thread; return thread == &z_idle_thread;
return thread == _idle_thread;
#endif #endif
} }

View file

@ -7,8 +7,7 @@
#include <kernel_structs.h> #include <kernel_structs.h>
#include <cmsis_os.h> #include <cmsis_os.h>
#include <ksched.h> #include <ksched.h>
#include <kernel_internal.h>
extern const k_tid_t _main_thread;
/** /**
* @brief Get the RTOS kernel system timer counter * @brief Get the RTOS kernel system timer counter
@ -42,5 +41,5 @@ osStatus osKernelStart(void)
*/ */
int32_t osKernelRunning(void) int32_t osKernelRunning(void)
{ {
return z_has_thread_started(_main_thread); return z_has_thread_started(&z_main_thread);
} }

View file

@ -6,6 +6,7 @@
#include <tracing_cpu_stats.h> #include <tracing_cpu_stats.h>
#include <sys/printk.h> #include <sys/printk.h>
#include <kernel_internal.h>
enum cpu_state { enum cpu_state {
CPU_STATE_IDLE, CPU_STATE_IDLE,
@ -21,16 +22,12 @@ static struct cpu_stats stats_hw_tick;
static int nested_interrupts; static int nested_interrupts;
static struct k_thread *current_thread; 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) static int is_idle_thread(struct k_thread *thread)
{ {
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
return thread->base.is_idle; return thread->base.is_idle;
#else #else
return thread == _idle_thread; return thread == &z_idle_thread;
#endif #endif
} }

View file

@ -7,19 +7,15 @@
#include <zephyr.h> #include <zephyr.h>
#include <kernel_structs.h> #include <kernel_structs.h>
#include <init.h> #include <init.h>
#include <kernel_internal.h>
#include "ctf_top.h" #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) static inline int is_idle_thread(struct k_thread *thread)
{ {
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
return thread->base.is_idle; return thread->base.is_idle;
#else #else
return thread == _idle_thread; return thread == &z_idle_thread;
#endif #endif
} }

View file

@ -7,21 +7,18 @@
#define _TRACE_SYSVIEW_H #define _TRACE_SYSVIEW_H
#include <kernel.h> #include <kernel.h>
#include <init.h> #include <init.h>
#include <kernel_internal.h>
#include <SEGGER_SYSVIEW.h> #include <SEGGER_SYSVIEW.h>
#include <Global.h> #include <Global.h>
#include "SEGGER_SYSVIEW_Zephyr.h" #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) static inline int is_idle_thread(struct k_thread *thread)
{ {
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
return thread->base.is_idle; return thread->base.is_idle;
#else #else
return thread == _idle_thread; return thread == &z_idle_thread;
#endif #endif
} }

View file

@ -14,6 +14,7 @@
LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG); LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
#include <zephyr.h> #include <zephyr.h>
#include <kernel_internal.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <shell/shell.h> #include <shell/shell.h>
@ -3248,7 +3249,6 @@ static int cmd_net_route(const struct shell *shell, size_t argc, char *argv[])
} }
#if defined(CONFIG_INIT_STACKS) #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(_interrupt_stack, CONFIG_ISR_STACK_SIZE);
extern K_THREAD_STACK_DEFINE(sys_work_q_stack, extern K_THREAD_STACK_DEFINE(sys_work_q_stack,
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE); 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) #if defined(CONFIG_INIT_STACKS)
net_analyze_stack_get_values(Z_THREAD_STACK_BUFFER(_main_stack), net_analyze_stack_get_values(Z_THREAD_STACK_BUFFER(z_main_stack),
K_THREAD_STACK_SIZEOF(_main_stack), K_THREAD_STACK_SIZEOF(z_main_stack),
&pcnt, &unused); &pcnt, &unused);
PR("%s [%s] stack size %d/%d bytes unused %u usage %d/%d (%u %%)\n", 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 - unused, CONFIG_MAIN_STACK_SIZE, pcnt); CONFIG_MAIN_STACK_SIZE - unused, CONFIG_MAIN_STACK_SIZE, pcnt);

View file

@ -7,6 +7,7 @@
#include <zephyr.h> #include <zephyr.h>
#include <syscall_handler.h> #include <syscall_handler.h>
#include <ztest.h> #include <ztest.h>
#include <kernel_internal.h>
#define SEM_ARRAY_SIZE 16 #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"); "object should have had sufficient permissions");
} }
extern const k_tid_t _main_thread;
/** /**
* @brief Tests to verify object permission * @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 /* Give an extra reference to another thread so the object
* doesn't disappear if we revoke our own * 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 */ /* dynamic object table well-populated with semaphores at this point */

View file

@ -146,7 +146,6 @@ void test_thread_name_get_set(void)
#ifdef CONFIG_USERSPACE #ifdef CONFIG_USERSPACE
static char unreadable_string[64]; static char unreadable_string[64];
static char not_my_buffer[CONFIG_THREAD_MAX_NAME_LEN]; static char not_my_buffer[CONFIG_THREAD_MAX_NAME_LEN];
ZTEST_BMEM struct k_thread *main_thread_ptr;
struct k_sem sem; struct k_sem sem;
#endif /* CONFIG_USERSPACE */ #endif /* CONFIG_USERSPACE */
@ -169,7 +168,7 @@ void test_thread_name_user_get_set(void)
zassert_equal(ret, -EFAULT, "accepted unreadable string"); zassert_equal(ret, -EFAULT, "accepted unreadable string");
ret = k_thread_name_set((struct k_thread *)&sem, "some name"); ret = k_thread_name_set((struct k_thread *)&sem, "some name");
zassert_equal(ret, -EINVAL, "accepted non-thread object"); 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"); zassert_equal(ret, -EINVAL, "no permission on thread object");
/* Set and get current thread's name */ /* 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, ret = k_thread_name_copy((struct k_thread *)&sem, thread_name,
sizeof(thread_name)); sizeof(thread_name));
zassert_equal(ret, -EINVAL, "not a thread object"); 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)); sizeof(thread_name));
zassert_equal(ret, 0, "couldn't get main thread name"); zassert_equal(ret, 0, "couldn't get main thread name");
printk("Main thread name is '%s'\n", thread_name); printk("Main thread name is '%s'\n", thread_name);
@ -270,8 +269,6 @@ void test_user_mode(void)
} }
#endif #endif
extern const k_tid_t _main_thread;
void test_main(void) void test_main(void)
{ {
k_thread_access_grant(k_current_get(), &tdata, tstack); k_thread_access_grant(k_current_get(), &tdata, tstack);
@ -281,8 +278,6 @@ void test_main(void)
#ifdef CONFIG_USERSPACE #ifdef CONFIG_USERSPACE
strncpy(unreadable_string, "unreadable string", strncpy(unreadable_string, "unreadable string",
sizeof(unreadable_string)); sizeof(unreadable_string));
/* Copy so user mode can get at it */
main_thread_ptr = _main_thread;
#endif #endif
ztest_test_suite(threads_lifecycle, ztest_test_suite(threads_lifecycle,