123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- #include <zephyr.h>
- #include <offsets_short.h>
- #include <kernel.h>
- #include <sys/printk.h>
- #include <debug/stack.h>
- #include <random/rand32.h>
- #include <linker/sections.h>
- #include <toolchain.h>
- #include <kernel_structs.h>
- #include <device.h>
- #include <init.h>
- #include <linker/linker-defs.h>
- #include <ksched.h>
- #include <string.h>
- #include <sys/dlist.h>
- #include <kernel_internal.h>
- #include <drivers/entropy.h>
- #include <logging/log_ctrl.h>
- #include <tracing/tracing.h>
- #include <stdbool.h>
- #include <debug/gcov.h>
- #include <kswap.h>
- #include <timing/timing.h>
- #include <logging/log.h>
- LOG_MODULE_REGISTER(os, CONFIG_KERNEL_LOG_LEVEL);
- struct z_kernel _kernel;
- #if 0
- K_THREAD_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE);
- #else
- __in_section_unique(main.noinit.stack) struct z_thread_stack_element __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \
- z_main_stack[CONFIG_MAIN_STACK_SIZE];
- #endif
- struct k_thread z_main_thread;
- #ifdef CONFIG_MULTITHREADING
- __pinned_bss
- struct k_thread z_idle_threads[CONFIG_MP_NUM_CPUS];
- static K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_idle_stacks,
- CONFIG_MP_NUM_CPUS,
- CONFIG_IDLE_STACK_SIZE);
- #endif
- #if 0
- K_KERNEL_STACK_ARRAY_DEFINE(z_interrupt_stacks, CONFIG_MP_NUM_CPUS,
- CONFIG_ISR_STACK_SIZE);
- #else
- __in_section_unique(interrupt.noinit.stack) struct z_thread_stack_element __aligned(Z_KERNEL_STACK_OBJ_ALIGN) \
- z_interrupt_stacks[CONFIG_MP_NUM_CPUS][CONFIG_ISR_STACK_SIZE];
- #endif
- #ifdef CONFIG_SYS_CLOCK_EXISTS
- #define initialize_timeouts() do { \
- sys_dlist_init(&_timeout_q); \
- } while (false)
- #else
- #define initialize_timeouts() do { } while ((0))
- #endif
- extern void idle(void *unused1, void *unused2, void *unused3);
- __boot_func
- void z_bss_zero(void)
- {
- #ifndef CONFIG_SOC_NO_PSRAM
- if ((uint32_t)&__psram_bss_end - (uint32_t)&__psram_bss_start > 0) {
- (void)memset(&__psram_bss_start, 0, (uint32_t)&__psram_bss_end - (uint32_t)&__psram_bss_start);
- }
- if ((uint32_t)&__sram_bss_end - (uint32_t)&__sram_bss_start > 0) {
- (void)memset(&__sram_bss_start, 0, (uint32_t)&__sram_bss_end - (uint32_t)&__sram_bss_start);
- }
- #else
- if ((uint32_t)&__bss_end - (uint32_t)&__bss_start > 0) {
- (void)memset(&__bss_start, 0, (uint32_t)&__bss_end - (uint32_t)&__bss_start);
- }
- #endif
- #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay)
- (void)memset(&__ccm_bss_start, 0,
- ((uint32_t) &__ccm_bss_end - (uint32_t) &__ccm_bss_start));
- #endif
- #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay)
- (void)memset(&__dtcm_bss_start, 0,
- ((uint32_t) &__dtcm_bss_end - (uint32_t) &__dtcm_bss_start));
- #endif
- #ifdef CONFIG_CODE_DATA_RELOCATION
- extern void bss_zeroing_relocation(void);
- bss_zeroing_relocation();
- #endif
- #ifdef CONFIG_COVERAGE_GCOV
- (void)memset(&__gcov_bss_start, 0,
- ((uintptr_t) &__gcov_bss_end - (uintptr_t) &__gcov_bss_start));
- #endif
- }
- #ifdef CONFIG_LINKER_USE_BOOT_SECTION
- __boot_func
- void z_bss_zero_boot(void)
- {
- (void)memset(&lnkr_boot_bss_start, 0,
- (uintptr_t)&lnkr_boot_bss_end
- - (uintptr_t)&lnkr_boot_bss_start);
- }
- #endif
- #ifdef CONFIG_LINKER_USE_PINNED_SECTION
- #ifdef CONFIG_LINKER_USE_BOOT_SECTION
- __boot_func
- #else
- __pinned_func
- #endif
- void z_bss_zero_pinned(void)
- {
- (void)memset(&lnkr_pinned_bss_start, 0,
- (uintptr_t)&lnkr_pinned_bss_end
- - (uintptr_t)&lnkr_pinned_bss_start);
- }
- #endif
- #ifdef CONFIG_STACK_CANARIES
- extern volatile uintptr_t __stack_chk_guard;
- #endif
- __pinned_bss
- bool z_sys_post_kernel;
- extern void boot_banner(void);
- __boot_func
- static void bg_thread_main(void *unused1, void *unused2, void *unused3)
- {
- ARG_UNUSED(unused1);
- ARG_UNUSED(unused2);
- ARG_UNUSED(unused3);
- #ifdef CONFIG_MMU
-
- z_mem_manage_init();
- #endif
- z_sys_post_kernel = true;
- z_sys_init_run_level(_SYS_INIT_LEVEL_POST_KERNEL);
- #if CONFIG_STACK_POINTER_RANDOM
- z_stack_adjust_initialized = 1;
- #endif
- boot_banner();
- #if defined(CONFIG_CPLUSPLUS) && !defined(CONFIG_ARCH_POSIX)
- void z_cpp_init_static(void);
- z_cpp_init_static();
- #endif
-
- z_sys_init_run_level(_SYS_INIT_LEVEL_APPLICATION);
- z_init_static_threads();
- #ifdef CONFIG_KERNEL_COHERENCE
- __ASSERT_NO_MSG(arch_mem_coherent(&_kernel));
- #endif
- #ifdef CONFIG_SMP
- z_smp_init();
- z_sys_init_run_level(_SYS_INIT_LEVEL_SMP);
- #endif
- #ifdef CONFIG_MMU
- z_mem_manage_boot_finish();
- #endif
- extern int main(void);
- main();
-
- z_main_thread.base.user_options &= ~K_ESSENTIAL;
- #ifdef CONFIG_COVERAGE_DUMP
-
- gcov_coverage_dump();
- #endif
- }
- #if defined(CONFIG_MULTITHREADING)
- __boot_func
- static void init_idle_thread(int i)
- {
- struct k_thread *thread = &z_idle_threads[i];
- k_thread_stack_t *stack = z_idle_stacks[i];
- #ifdef CONFIG_THREAD_NAME
- char tname[8];
- snprintk(tname, 8, "idle %02d", i);
- #else
- char *tname = NULL;
- #endif
- z_setup_new_thread(thread, stack,
- CONFIG_IDLE_STACK_SIZE, idle, &_kernel.cpus[i],
- NULL, NULL, K_IDLE_PRIO, K_ESSENTIAL,
- tname);
- z_mark_thread_as_started(thread);
- #ifdef CONFIG_SMP
- thread->base.is_idle = 1U;
- #endif
- }
- void z_reinit_idle_thread(int i)
- {
- init_idle_thread(i);
- }
- __boot_func
- static char *prepare_multithreading(void)
- {
- char *stack_ptr;
-
- z_sched_init();
- #ifndef CONFIG_SMP
-
- _kernel.ready_q.cache = &z_main_thread;
- #endif
- stack_ptr = 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");
- z_mark_thread_as_started(&z_main_thread);
- z_ready_thread(&z_main_thread);
- for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) {
- init_idle_thread(i);
- _kernel.cpus[i].idle_thread = &z_idle_threads[i];
- _kernel.cpus[i].id = i;
- _kernel.cpus[i].irq_stack =
- (Z_KERNEL_STACK_BUFFER(z_interrupt_stacks[i]) +
- K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[i]));
- }
- return stack_ptr;
- }
- __boot_func
- static FUNC_NORETURN void switch_to_main_thread(char *stack_ptr)
- {
- #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN
- arch_switch_to_main_thread(&z_main_thread, stack_ptr, bg_thread_main);
- #else
- ARG_UNUSED(stack_ptr);
-
- z_swap_unlocked();
- #endif
- CODE_UNREACHABLE;
- }
- #endif
- #if defined(CONFIG_ENTROPY_HAS_DRIVER) || defined(CONFIG_TEST_RANDOM_GENERATOR)
- __boot_func
- void z_early_boot_rand_get(uint8_t *buf, size_t length)
- {
- int n = sizeof(uint32_t);
- #ifdef CONFIG_ENTROPY_HAS_DRIVER
- const struct device *entropy = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
- int rc;
- if (entropy == NULL) {
- goto sys_rand_fallback;
- }
-
- rc = entropy_get_entropy_isr(entropy, buf, length, ENTROPY_BUSYWAIT);
- if (rc == -ENOTSUP) {
-
- rc = entropy_get_entropy(entropy, buf, length);
- }
- if (rc >= 0) {
- return;
- }
-
- sys_rand_fallback:
- #endif
-
- while (length > 0U) {
- uint32_t rndbits;
- uint8_t *p_rndbits = (uint8_t *)&rndbits;
- rndbits = sys_rand32_get();
- if (length < sizeof(uint32_t)) {
- n = length;
- }
- for (int i = 0; i < n; i++) {
- *buf = *p_rndbits;
- buf++;
- p_rndbits++;
- }
- length -= n;
- }
- }
- #endif
- __boot_func
- FUNC_NORETURN void z_cstart(void)
- {
-
- gcov_static_init();
- LOG_CORE_INIT();
-
- arch_kernel_init();
- #if defined(CONFIG_MULTITHREADING)
-
- struct k_thread dummy_thread;
- z_dummy_thread_init(&dummy_thread);
- #endif
-
- z_device_state_init();
-
- z_sys_init_run_level(_SYS_INIT_LEVEL_PRE_KERNEL_1);
- z_sys_init_run_level(_SYS_INIT_LEVEL_PRE_KERNEL_2);
- #ifdef CONFIG_STACK_CANARIES
- uintptr_t stack_guard;
- z_early_boot_rand_get((uint8_t *)&stack_guard, sizeof(stack_guard));
- __stack_chk_guard = stack_guard;
- __stack_chk_guard <<= 8;
- #endif
- #ifdef CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT
- timing_init();
- timing_start();
- #endif
- #ifdef CONFIG_MULTITHREADING
- switch_to_main_thread(prepare_multithreading());
- #else
- #ifdef ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING
-
- ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING(bg_thread_main,
- NULL, NULL, NULL);
- #else
- bg_thread_main(NULL, NULL, NULL);
-
- irq_lock();
- while (true) {
- }
-
- #endif
- #endif
-
- CODE_UNREACHABLE;
- }
|