Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2014 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
Anas Nashif | cb888e6 | 2016-12-18 09:42:55 -0500 | [diff] [blame] | 9 | * @brief Kernel initialization module |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 10 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 11 | * This module contains routines that are used to initialize the kernel. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 12 | */ |
| 13 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 14 | #include <offsets_short.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 15 | #include <zephyr/kernel.h> |
| 16 | #include <zephyr/sys/printk.h> |
| 17 | #include <zephyr/debug/stack.h> |
| 18 | #include <zephyr/random/rand32.h> |
| 19 | #include <zephyr/linker/sections.h> |
| 20 | #include <zephyr/toolchain.h> |
| 21 | #include <zephyr/kernel_structs.h> |
| 22 | #include <zephyr/device.h> |
| 23 | #include <zephyr/init.h> |
| 24 | #include <zephyr/linker/linker-defs.h> |
Benjamin Walsh | b4b108d | 2016-10-13 10:31:48 -0400 | [diff] [blame] | 25 | #include <ksched.h> |
Mahavir Jain | acea241 | 2016-12-02 21:48:39 +0530 | [diff] [blame] | 26 | #include <string.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 27 | #include <zephyr/sys/dlist.h> |
Andy Ross | 245b54e | 2018-02-08 09:10:46 -0800 | [diff] [blame] | 28 | #include <kernel_internal.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 29 | #include <zephyr/drivers/entropy.h> |
| 30 | #include <zephyr/logging/log_ctrl.h> |
| 31 | #include <zephyr/tracing/tracing.h> |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 32 | #include <stdbool.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 33 | #include <zephyr/debug/gcov.h> |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 34 | #include <kswap.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 35 | #include <zephyr/timing/timing.h> |
| 36 | #include <zephyr/logging/log.h> |
Jordan Yates | db3d51b | 2022-03-12 21:10:42 +1000 | [diff] [blame] | 37 | #include <zephyr/pm/device_runtime.h> |
Krzysztof Chruscinski | 3ed8083 | 2020-11-26 19:32:34 +0100 | [diff] [blame] | 38 | LOG_MODULE_REGISTER(os, CONFIG_KERNEL_LOG_LEVEL); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 39 | |
Kumar Gala | 0722b6f | 2022-10-21 09:34:08 -0500 | [diff] [blame] | 40 | |
| 41 | BUILD_ASSERT(CONFIG_MP_NUM_CPUS == CONFIG_MP_MAX_NUM_CPUS, |
| 42 | "CONFIG_MP_NUM_CPUS and CONFIG_MP_MAX_NUM_CPUS need to be set the same"); |
| 43 | |
Krzysztof Chruscinski | 7dcff6e | 2021-04-16 15:16:00 +0200 | [diff] [blame] | 44 | /* the only struct z_kernel instance */ |
Qipeng Zha | fa973d1 | 2023-05-15 13:29:18 +0800 | [diff] [blame] | 45 | __pinned_bss |
Krzysztof Chruscinski | 7dcff6e | 2021-04-16 15:16:00 +0200 | [diff] [blame] | 46 | struct z_kernel _kernel; |
| 47 | |
Flavio Ceolin | 4f29930 | 2023-05-30 11:49:45 -0700 | [diff] [blame] | 48 | __pinned_bss |
| 49 | atomic_t _cpus_active; |
| 50 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 51 | /* init/main and idle threads */ |
Daniel Leung | ebbfde9 | 2021-07-12 13:47:48 -0700 | [diff] [blame] | 52 | K_THREAD_PINNED_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE); |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 53 | struct k_thread z_main_thread; |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 54 | |
| 55 | #ifdef CONFIG_MULTITHREADING |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 56 | __pinned_bss |
Kumar Gala | c778eb2 | 2022-10-12 10:55:36 -0500 | [diff] [blame] | 57 | struct k_thread z_idle_threads[CONFIG_MP_MAX_NUM_CPUS]; |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 58 | |
| 59 | static K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_idle_stacks, |
Kumar Gala | c778eb2 | 2022-10-12 10:55:36 -0500 | [diff] [blame] | 60 | CONFIG_MP_MAX_NUM_CPUS, |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 61 | CONFIG_IDLE_STACK_SIZE); |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 62 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 63 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 64 | extern const struct init_entry __init_start[]; |
| 65 | extern const struct init_entry __init_EARLY_start[]; |
| 66 | extern const struct init_entry __init_PRE_KERNEL_1_start[]; |
| 67 | extern const struct init_entry __init_PRE_KERNEL_2_start[]; |
| 68 | extern const struct init_entry __init_POST_KERNEL_start[]; |
| 69 | extern const struct init_entry __init_APPLICATION_start[]; |
| 70 | extern const struct init_entry __init_end[]; |
| 71 | |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 72 | enum init_level { |
| 73 | INIT_LEVEL_EARLY = 0, |
| 74 | INIT_LEVEL_PRE_KERNEL_1, |
| 75 | INIT_LEVEL_PRE_KERNEL_2, |
| 76 | INIT_LEVEL_POST_KERNEL, |
| 77 | INIT_LEVEL_APPLICATION, |
| 78 | #ifdef CONFIG_SMP |
| 79 | INIT_LEVEL_SMP, |
| 80 | #endif |
| 81 | }; |
| 82 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 83 | #ifdef CONFIG_SMP |
| 84 | extern const struct init_entry __init_SMP_start[]; |
| 85 | #endif |
| 86 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 87 | /* |
| 88 | * storage space for the interrupt stack |
| 89 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 90 | * Note: This area is used as the system stack during kernel initialization, |
| 91 | * since the kernel hasn't yet set up its own stack areas. The dual purposing |
| 92 | * of this area is safe since interrupts are disabled until the kernel context |
| 93 | * switches to the init thread. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 94 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 95 | K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_interrupt_stacks, |
Kumar Gala | c778eb2 | 2022-10-12 10:55:36 -0500 | [diff] [blame] | 96 | CONFIG_MP_MAX_NUM_CPUS, |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 97 | CONFIG_ISR_STACK_SIZE); |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 98 | |
Peter Mitsis | 96cb05c | 2016-09-15 12:37:58 -0400 | [diff] [blame] | 99 | extern void idle(void *unused1, void *unused2, void *unused3); |
| 100 | |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 101 | |
Andrew Boie | fe228a8 | 2019-06-11 12:49:32 -0700 | [diff] [blame] | 102 | /* LCOV_EXCL_START |
| 103 | * |
| 104 | * This code is called so early in the boot process that code coverage |
| 105 | * doesn't work properly. In addition, not all arches call this code, |
| 106 | * some like x86 do this with optimized assembly |
| 107 | */ |
| 108 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 109 | /** |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 110 | * @brief equivalent of memset() for early boot usage |
| 111 | * |
| 112 | * Architectures that can't safely use the regular (optimized) memset very |
| 113 | * early during boot because e.g. hardware isn't yet sufficiently initialized |
| 114 | * may override this with their own safe implementation. |
| 115 | */ |
| 116 | __boot_func |
| 117 | void __weak z_early_memset(void *dst, int c, size_t n) |
| 118 | { |
| 119 | (void) memset(dst, c, n); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @brief equivalent of memcpy() for early boot usage |
| 124 | * |
| 125 | * Architectures that can't safely use the regular (optimized) memcpy very |
| 126 | * early during boot because e.g. hardware isn't yet sufficiently initialized |
| 127 | * may override this with their own safe implementation. |
| 128 | */ |
| 129 | __boot_func |
| 130 | void __weak z_early_memcpy(void *dst, const void *src, size_t n) |
| 131 | { |
| 132 | (void) memcpy(dst, src, n); |
| 133 | } |
| 134 | |
| 135 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 136 | * @brief Clear BSS |
| 137 | * |
| 138 | * This routine clears the BSS region, so all bytes are 0. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 139 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 140 | __boot_func |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 141 | void z_bss_zero(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 142 | { |
Andy Ross | 5722da7 | 2022-08-09 17:55:14 -0700 | [diff] [blame] | 143 | if (IS_ENABLED(CONFIG_ARCH_POSIX)) { |
| 144 | /* native_posix gets its memory cleared on entry by |
| 145 | * the host OS, and in any case the host clang/lld |
| 146 | * doesn't emit the __bss_end symbol this code expects |
| 147 | * to see |
| 148 | */ |
| 149 | return; |
| 150 | } |
| 151 | |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 152 | z_early_memset(__bss_start, 0, __bss_end - __bss_start); |
Martà BolÃvar | 6e8775f | 2020-05-11 11:56:08 -0700 | [diff] [blame] | 153 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 154 | z_early_memset(&__ccm_bss_start, 0, |
| 155 | (uintptr_t) &__ccm_bss_end |
| 156 | - (uintptr_t) &__ccm_bss_start); |
Erwin Rol | 1dc41d1 | 2017-10-05 01:22:32 +0200 | [diff] [blame] | 157 | #endif |
Martà BolÃvar | 6e8775f | 2020-05-11 11:56:08 -0700 | [diff] [blame] | 158 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 159 | z_early_memset(&__dtcm_bss_start, 0, |
| 160 | (uintptr_t) &__dtcm_bss_end |
| 161 | - (uintptr_t) &__dtcm_bss_start); |
Alexander Wachter | b4c5f4b | 2019-07-03 14:19:29 +0200 | [diff] [blame] | 162 | #endif |
Immo Birnbaum | da28829 | 2022-01-21 12:38:30 +0100 | [diff] [blame] | 163 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ocm), okay) |
Nicolas Pitre | 3617398 | 2022-02-22 00:00:52 -0500 | [diff] [blame] | 164 | z_early_memset(&__ocm_bss_start, 0, |
| 165 | (uintptr_t) &__ocm_bss_end |
| 166 | - (uintptr_t) &__ocm_bss_start); |
Immo Birnbaum | da28829 | 2022-01-21 12:38:30 +0100 | [diff] [blame] | 167 | #endif |
Adithya Baglody | 91c5b84 | 2018-11-13 16:57:45 +0530 | [diff] [blame] | 168 | #ifdef CONFIG_CODE_DATA_RELOCATION |
| 169 | extern void bss_zeroing_relocation(void); |
| 170 | |
| 171 | bss_zeroing_relocation(); |
| 172 | #endif /* CONFIG_CODE_DATA_RELOCATION */ |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 173 | #ifdef CONFIG_COVERAGE_GCOV |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 174 | z_early_memset(&__gcov_bss_start, 0, |
| 175 | ((uintptr_t) &__gcov_bss_end - (uintptr_t) &__gcov_bss_start)); |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 176 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Daniel Leung | d812728 | 2021-02-24 10:18:34 -0800 | [diff] [blame] | 179 | #ifdef CONFIG_LINKER_USE_BOOT_SECTION |
| 180 | /** |
| 181 | * @brief Clear BSS within the bot region |
| 182 | * |
| 183 | * This routine clears the BSS within the boot region. |
| 184 | * This is separate from z_bss_zero() as boot region may |
| 185 | * contain symbols required for the boot process before |
| 186 | * paging is initialized. |
| 187 | */ |
| 188 | __boot_func |
| 189 | void z_bss_zero_boot(void) |
| 190 | { |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 191 | z_early_memset(&lnkr_boot_bss_start, 0, |
| 192 | (uintptr_t)&lnkr_boot_bss_end |
| 193 | - (uintptr_t)&lnkr_boot_bss_start); |
Daniel Leung | d812728 | 2021-02-24 10:18:34 -0800 | [diff] [blame] | 194 | } |
| 195 | #endif /* CONFIG_LINKER_USE_BOOT_SECTION */ |
| 196 | |
Daniel Leung | 1310ad6 | 2021-02-23 13:33:38 -0800 | [diff] [blame] | 197 | #ifdef CONFIG_LINKER_USE_PINNED_SECTION |
| 198 | /** |
| 199 | * @brief Clear BSS within the pinned region |
| 200 | * |
| 201 | * This routine clears the BSS within the pinned region. |
| 202 | * This is separate from z_bss_zero() as pinned region may |
| 203 | * contain symbols required for the boot process before |
| 204 | * paging is initialized. |
| 205 | */ |
| 206 | #ifdef CONFIG_LINKER_USE_BOOT_SECTION |
| 207 | __boot_func |
| 208 | #else |
| 209 | __pinned_func |
| 210 | #endif |
| 211 | void z_bss_zero_pinned(void) |
| 212 | { |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 213 | z_early_memset(&lnkr_pinned_bss_start, 0, |
| 214 | (uintptr_t)&lnkr_pinned_bss_end |
| 215 | - (uintptr_t)&lnkr_pinned_bss_start); |
Daniel Leung | 1310ad6 | 2021-02-23 13:33:38 -0800 | [diff] [blame] | 216 | } |
| 217 | #endif /* CONFIG_LINKER_USE_PINNED_SECTION */ |
| 218 | |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 219 | #ifdef CONFIG_STACK_CANARIES |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 220 | #ifdef CONFIG_STACK_CANARIES_TLS |
| 221 | extern __thread volatile uintptr_t __stack_chk_guard; |
| 222 | #else |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 223 | extern volatile uintptr_t __stack_chk_guard; |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 224 | #endif |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 225 | #endif /* CONFIG_STACK_CANARIES */ |
| 226 | |
Andrew Boie | fe228a8 | 2019-06-11 12:49:32 -0700 | [diff] [blame] | 227 | /* LCOV_EXCL_STOP */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 228 | |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 229 | __pinned_bss |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 230 | bool z_sys_post_kernel; |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 231 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 232 | /** |
| 233 | * @brief Execute all the init entry initialization functions at a given level |
| 234 | * |
| 235 | * @details Invokes the initialization routine for each init entry object |
| 236 | * created by the INIT_ENTRY_DEFINE() macro using the specified level. |
| 237 | * The linker script places the init entry objects in memory in the order |
| 238 | * they need to be invoked, with symbols indicating where one level leaves |
| 239 | * off and the next one begins. |
| 240 | * |
| 241 | * @param level init level to run. |
| 242 | */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 243 | static void z_sys_init_run_level(enum init_level level) |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 244 | { |
| 245 | static const struct init_entry *levels[] = { |
| 246 | __init_EARLY_start, |
| 247 | __init_PRE_KERNEL_1_start, |
| 248 | __init_PRE_KERNEL_2_start, |
| 249 | __init_POST_KERNEL_start, |
| 250 | __init_APPLICATION_start, |
| 251 | #ifdef CONFIG_SMP |
| 252 | __init_SMP_start, |
| 253 | #endif |
| 254 | /* End marker */ |
| 255 | __init_end, |
| 256 | }; |
| 257 | const struct init_entry *entry; |
| 258 | |
| 259 | for (entry = levels[level]; entry < levels[level+1]; entry++) { |
| 260 | const struct device *dev = entry->dev; |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 261 | |
| 262 | if (dev != NULL) { |
Gerard Marull-Paretas | 4d06166 | 2023-04-18 10:56:15 +0200 | [diff] [blame] | 263 | int rc = 0; |
| 264 | |
| 265 | if (entry->init_fn.dev != NULL) { |
| 266 | rc = entry->init_fn.dev(dev); |
| 267 | /* Mark device initialized. If initialization |
| 268 | * failed, record the error condition. |
| 269 | */ |
| 270 | if (rc != 0) { |
| 271 | if (rc < 0) { |
| 272 | rc = -rc; |
| 273 | } |
| 274 | if (rc > UINT8_MAX) { |
| 275 | rc = UINT8_MAX; |
| 276 | } |
| 277 | dev->state->init_res = rc; |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 278 | } |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 279 | } |
Gerard Marull-Paretas | 4d06166 | 2023-04-18 10:56:15 +0200 | [diff] [blame] | 280 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 281 | dev->state->initialized = true; |
Gerard Marull-Paretas | 4d06166 | 2023-04-18 10:56:15 +0200 | [diff] [blame] | 282 | |
Jordan Yates | db3d51b | 2022-03-12 21:10:42 +1000 | [diff] [blame] | 283 | if (rc == 0) { |
| 284 | /* Run automatic device runtime enablement */ |
| 285 | (void)pm_device_runtime_auto_enable(dev); |
| 286 | } |
Gerard Marull-Paretas | a5fd0d1 | 2022-10-19 09:33:44 +0200 | [diff] [blame] | 287 | } else { |
| 288 | (void)entry->init_fn.sys(); |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Anas Nashif | 4b59312 | 2020-08-27 09:08:40 -0400 | [diff] [blame] | 293 | extern void boot_banner(void); |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 294 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 295 | /** |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 296 | * @brief Mainline for kernel's background thread |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 297 | * |
| 298 | * This routine completes kernel initialization by invoking the remaining |
| 299 | * init functions, then invokes application's main() routine. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 300 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 301 | __boot_func |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 302 | static void bg_thread_main(void *unused1, void *unused2, void *unused3) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 303 | { |
| 304 | ARG_UNUSED(unused1); |
| 305 | ARG_UNUSED(unused2); |
| 306 | ARG_UNUSED(unused3); |
| 307 | |
Andrew Boie | e35f179 | 2020-12-09 12:18:40 -0800 | [diff] [blame] | 308 | #ifdef CONFIG_MMU |
| 309 | /* Invoked here such that backing store or eviction algorithms may |
| 310 | * initialize kernel objects, and that all POST_KERNEL and later tasks |
| 311 | * may perform memory management tasks (except for z_phys_map() which |
| 312 | * is allowed at any time) |
| 313 | */ |
| 314 | z_mem_manage_init(); |
| 315 | #endif /* CONFIG_MMU */ |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 316 | z_sys_post_kernel = true; |
| 317 | |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 318 | z_sys_init_run_level(INIT_LEVEL_POST_KERNEL); |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 319 | #if CONFIG_STACK_POINTER_RANDOM |
| 320 | z_stack_adjust_initialized = 1; |
| 321 | #endif |
Anas Nashif | 4b59312 | 2020-08-27 09:08:40 -0400 | [diff] [blame] | 322 | boot_banner(); |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 323 | |
Stephanos Ioannidis | 4a64bfe | 2022-12-09 06:16:44 +0900 | [diff] [blame] | 324 | #if defined(CONFIG_CPP) |
Evgeniy Paltsev | 497cb2e | 2021-04-26 16:15:49 +0300 | [diff] [blame] | 325 | void z_cpp_init_static(void); |
| 326 | z_cpp_init_static(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 327 | #endif |
| 328 | |
Peter Bigot | c308f80 | 2020-05-08 09:28:44 -0500 | [diff] [blame] | 329 | /* Final init level before app starts */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 330 | z_sys_init_run_level(INIT_LEVEL_APPLICATION); |
Peter Bigot | c308f80 | 2020-05-08 09:28:44 -0500 | [diff] [blame] | 331 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 332 | z_init_static_threads(); |
Anas Nashif | 83088a2 | 2017-08-24 04:27:51 -0400 | [diff] [blame] | 333 | |
Anas Nashif | 39f632e | 2020-12-07 13:15:42 -0500 | [diff] [blame] | 334 | #ifdef CONFIG_KERNEL_COHERENCE |
Daniel Leung | 079bc64 | 2021-02-02 16:12:15 -0800 | [diff] [blame] | 335 | __ASSERT_NO_MSG(arch_mem_coherent(&_kernel)); |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 336 | #endif |
| 337 | |
Andy Ross | eb25870 | 2018-04-12 12:10:10 -0700 | [diff] [blame] | 338 | #ifdef CONFIG_SMP |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 339 | if (!IS_ENABLED(CONFIG_SMP_BOOT_DELAY)) { |
| 340 | z_smp_init(); |
| 341 | } |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 342 | z_sys_init_run_level(INIT_LEVEL_SMP); |
Andy Ross | eb25870 | 2018-04-12 12:10:10 -0700 | [diff] [blame] | 343 | #endif |
Inaky Perez-Gonzalez | c51f73f | 2017-06-20 17:01:09 -0700 | [diff] [blame] | 344 | |
Daniel Leung | e88afd2 | 2021-07-15 13:15:29 -0700 | [diff] [blame] | 345 | #ifdef CONFIG_MMU |
| 346 | z_mem_manage_boot_finish(); |
| 347 | #endif /* CONFIG_MMU */ |
| 348 | |
Stephanos Ioannidis | fa5fd41 | 2022-11-05 00:22:25 +0900 | [diff] [blame] | 349 | extern int main(void); |
Andrew Boie | f1c373c | 2016-10-28 12:45:05 -0700 | [diff] [blame] | 350 | |
Stephanos Ioannidis | fa5fd41 | 2022-11-05 00:22:25 +0900 | [diff] [blame] | 351 | (void)main(); |
Allan Stephens | 073442e | 2016-11-09 07:46:56 -0600 | [diff] [blame] | 352 | |
Nazar Kazakov | 9713f0d | 2022-02-24 12:00:55 +0000 | [diff] [blame] | 353 | /* Mark nonessential since main() has no more work to do */ |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 354 | z_main_thread.base.user_options &= ~K_ESSENTIAL; |
Andrew Boie | 8e05333 | 2019-06-11 12:58:16 -0700 | [diff] [blame] | 355 | |
Anas Nashif | 471ffbe | 2020-01-30 08:44:10 -0500 | [diff] [blame] | 356 | #ifdef CONFIG_COVERAGE_DUMP |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 357 | /* Dump coverage data once the main() has exited. */ |
| 358 | gcov_coverage_dump(); |
Anas Nashif | 471ffbe | 2020-01-30 08:44:10 -0500 | [diff] [blame] | 359 | #endif |
Andrew Boie | 8e05333 | 2019-06-11 12:58:16 -0700 | [diff] [blame] | 360 | } /* LCOV_EXCL_LINE ... because we just dumped final coverage data */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 361 | |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 362 | #if defined(CONFIG_MULTITHREADING) |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 363 | __boot_func |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 364 | static void init_idle_thread(int i) |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 365 | { |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 366 | struct k_thread *thread = &z_idle_threads[i]; |
| 367 | k_thread_stack_t *stack = z_idle_stacks[i]; |
| 368 | |
| 369 | #ifdef CONFIG_THREAD_NAME |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 370 | |
Kumar Gala | 4f458ba | 2022-10-18 11:11:46 -0500 | [diff] [blame] | 371 | #if CONFIG_MP_MAX_NUM_CPUS > 1 |
Trond Einar Snekvik | 6224ecb | 2022-03-25 12:46:32 +0100 | [diff] [blame] | 372 | char tname[8]; |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 373 | snprintk(tname, 8, "idle %02d", i); |
| 374 | #else |
Trond Einar Snekvik | 6224ecb | 2022-03-25 12:46:32 +0100 | [diff] [blame] | 375 | char *tname = "idle"; |
| 376 | #endif |
| 377 | |
| 378 | #else |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 379 | char *tname = NULL; |
| 380 | #endif /* CONFIG_THREAD_NAME */ |
| 381 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 382 | z_setup_new_thread(thread, stack, |
Andrew Boie | f5a7e1a | 2020-09-02 09:20:38 -0700 | [diff] [blame] | 383 | CONFIG_IDLE_STACK_SIZE, idle, &_kernel.cpus[i], |
Andy Ross | 851d14a | 2021-05-13 15:46:43 -0700 | [diff] [blame] | 384 | NULL, NULL, K_IDLE_PRIO, K_ESSENTIAL, |
Andrew Boie | f5a7e1a | 2020-09-02 09:20:38 -0700 | [diff] [blame] | 385 | tname); |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 386 | z_mark_thread_as_started(thread); |
Andy Ross | 6c283ca | 2019-08-16 22:09:30 -0700 | [diff] [blame] | 387 | |
| 388 | #ifdef CONFIG_SMP |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 389 | thread->base.is_idle = 1U; |
Andy Ross | 6c283ca | 2019-08-16 22:09:30 -0700 | [diff] [blame] | 390 | #endif |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 391 | } |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 392 | |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 393 | void z_init_cpu(int id) |
Andy Ross | c6d077e | 2021-08-18 06:28:11 -0700 | [diff] [blame] | 394 | { |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 395 | init_idle_thread(id); |
| 396 | _kernel.cpus[id].idle_thread = &z_idle_threads[id]; |
| 397 | _kernel.cpus[id].id = id; |
| 398 | _kernel.cpus[id].irq_stack = |
| 399 | (Z_KERNEL_STACK_BUFFER(z_interrupt_stacks[id]) + |
| 400 | K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[id])); |
| 401 | #ifdef CONFIG_SCHED_THREAD_USAGE_ALL |
| 402 | _kernel.cpus[id].usage.track_usage = |
| 403 | CONFIG_SCHED_THREAD_USAGE_AUTO_ENABLE; |
| 404 | #endif |
Flavio Ceolin | 4f29930 | 2023-05-30 11:49:45 -0700 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Increment number of CPUs active. The pm subsystem |
| 408 | * will keep track of this from here. |
| 409 | */ |
| 410 | atomic_inc(&_cpus_active); |
Andy Ross | c6d077e | 2021-08-18 06:28:11 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 413 | /** |
| 414 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 415 | * @brief Initializes kernel data structures |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 416 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 417 | * This routine initializes various kernel data structures, including |
| 418 | * the init and idle threads and any architecture-specific initialization. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 419 | * |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 420 | * Note that all fields of "_kernel" are set to zero on entry, which may |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 421 | * be all the initialization many of them require. |
| 422 | * |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 423 | * @return initial stack pointer for the main thread |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 424 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 425 | __boot_func |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 426 | static char *prepare_multithreading(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 427 | { |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 428 | char *stack_ptr; |
| 429 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 430 | /* _kernel.ready_q is all zeroes */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 431 | z_sched_init(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 432 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 433 | #ifndef CONFIG_SMP |
Benjamin Walsh | 88b3691 | 2016-12-02 10:37:27 -0500 | [diff] [blame] | 434 | /* |
| 435 | * prime the cache with the main thread since: |
| 436 | * |
| 437 | * - the cache can never be NULL |
| 438 | * - the main thread will be the one to run first |
| 439 | * - no other thread is initialized yet and thus their priority fields |
| 440 | * contain garbage, which would prevent the cache loading algorithm |
| 441 | * to work as intended |
| 442 | */ |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 443 | _kernel.ready_q.cache = &z_main_thread; |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 444 | #endif |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 445 | stack_ptr = z_setup_new_thread(&z_main_thread, z_main_stack, |
| 446 | CONFIG_MAIN_STACK_SIZE, bg_thread_main, |
| 447 | NULL, NULL, NULL, |
| 448 | CONFIG_MAIN_THREAD_PRIORITY, |
Ioannis Glaropoulos | 40aab32 | 2021-01-28 21:46:28 +0100 | [diff] [blame] | 449 | K_ESSENTIAL, "main"); |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 450 | z_mark_thread_as_started(&z_main_thread); |
| 451 | z_ready_thread(&z_main_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 452 | |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 453 | z_init_cpu(0); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 454 | |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 455 | return stack_ptr; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 458 | __boot_func |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 459 | static FUNC_NORETURN void switch_to_main_thread(char *stack_ptr) |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 460 | { |
Benjamin Walsh | 296a234 | 2016-11-20 11:04:31 -0500 | [diff] [blame] | 461 | #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 462 | arch_switch_to_main_thread(&z_main_thread, stack_ptr, bg_thread_main); |
Benjamin Walsh | 296a234 | 2016-11-20 11:04:31 -0500 | [diff] [blame] | 463 | #else |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 464 | ARG_UNUSED(stack_ptr); |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 465 | /* |
| 466 | * Context switch to main task (entry function is _main()): the |
| 467 | * current fake thread is not on a wait queue or ready queue, so it |
| 468 | * will never be rescheduled in. |
| 469 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 470 | z_swap_unlocked(); |
Benjamin Walsh | 296a234 | 2016-11-20 11:04:31 -0500 | [diff] [blame] | 471 | #endif |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 472 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 473 | } |
Anas Nashif | c0ea505 | 2019-01-30 09:58:41 -0500 | [diff] [blame] | 474 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 475 | |
Flavio Ceolin | 8ae822c | 2020-02-14 16:35:54 -0800 | [diff] [blame] | 476 | #if defined(CONFIG_ENTROPY_HAS_DRIVER) || defined(CONFIG_TEST_RANDOM_GENERATOR) |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 477 | __boot_func |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 478 | void z_early_boot_rand_get(uint8_t *buf, size_t length) |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 479 | { |
| 480 | #ifdef CONFIG_ENTROPY_HAS_DRIVER |
Gerard Marull-Paretas | a202341 | 2022-08-22 10:36:10 +0200 | [diff] [blame] | 481 | const struct device *const entropy = DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_entropy)); |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 482 | int rc; |
| 483 | |
Gerard Marull-Paretas | 5573d8d | 2022-02-25 18:31:13 +0100 | [diff] [blame] | 484 | if (!device_is_ready(entropy)) { |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 485 | goto sys_rand_fallback; |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Carles Cufi | b546449 | 2018-05-24 20:12:23 +0200 | [diff] [blame] | 488 | /* Try to see if driver provides an ISR-specific API */ |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 489 | rc = entropy_get_entropy_isr(entropy, buf, length, ENTROPY_BUSYWAIT); |
Carles Cufi | b546449 | 2018-05-24 20:12:23 +0200 | [diff] [blame] | 490 | if (rc == -ENOTSUP) { |
| 491 | /* Driver does not provide an ISR-specific API, assume it can |
| 492 | * be called from ISR context |
| 493 | */ |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 494 | rc = entropy_get_entropy(entropy, buf, length); |
Carles Cufi | b546449 | 2018-05-24 20:12:23 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (rc >= 0) { |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 498 | return; |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Carles Cufi | b546449 | 2018-05-24 20:12:23 +0200 | [diff] [blame] | 501 | /* Fall through to fallback */ |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 502 | |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 503 | sys_rand_fallback: |
Mazen NEIFER | e2bbad9 | 2017-02-07 10:01:12 +0100 | [diff] [blame] | 504 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 505 | |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 506 | /* FIXME: this assumes sys_rand32_get() won't use any synchronization |
| 507 | * primitive, like semaphores or mutexes. It's too early in the boot |
| 508 | * process to use any of them. Ideally, only the path where entropy |
| 509 | * devices are available should be built, this is only a fallback for |
| 510 | * those devices without a HWRNG entropy driver. |
| 511 | */ |
Flavio Ceolin | ac079fc | 2022-03-09 12:07:57 -0800 | [diff] [blame] | 512 | sys_rand_get(buf, length); |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 513 | } |
Flavio Ceolin | 8ae822c | 2020-02-14 16:35:54 -0800 | [diff] [blame] | 514 | /* defined(CONFIG_ENTROPY_HAS_DRIVER) || defined(CONFIG_TEST_RANDOM_GENERATOR) */ |
| 515 | #endif |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 516 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 517 | /** |
| 518 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 519 | * @brief Initialize kernel |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 520 | * |
| 521 | * This routine is invoked when the system is ready to run C code. The |
| 522 | * processor must be running in 32-bit mode, and the BSS must have been |
| 523 | * cleared/zeroed. |
| 524 | * |
| 525 | * @return Does not return |
| 526 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 527 | __boot_func |
Daniel Leung | 256db60 | 2022-12-15 15:54:56 -0800 | [diff] [blame] | 528 | FUNC_NO_STACK_PROTECTOR |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 529 | FUNC_NORETURN void z_cstart(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 530 | { |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 531 | /* gcov hook needed to get the coverage report.*/ |
| 532 | gcov_static_init(); |
| 533 | |
Gerard Marull-Paretas | e42f58e | 2022-10-11 17:17:18 +0200 | [diff] [blame] | 534 | /* initialize early init calls */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 535 | z_sys_init_run_level(INIT_LEVEL_EARLY); |
Gerard Marull-Paretas | e42f58e | 2022-10-11 17:17:18 +0200 | [diff] [blame] | 536 | |
Andrew Boie | 982d5c8 | 2018-05-23 13:30:34 -0700 | [diff] [blame] | 537 | /* perform any architecture-specific initialization */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 538 | arch_kernel_init(); |
Andrew Boie | 982d5c8 | 2018-05-23 13:30:34 -0700 | [diff] [blame] | 539 | |
Ederson de Souza | 75fd452 | 2022-01-25 10:47:05 -0800 | [diff] [blame] | 540 | LOG_CORE_INIT(); |
| 541 | |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 542 | #if defined(CONFIG_MULTITHREADING) |
| 543 | /* Note: The z_ready_thread() call in prepare_multithreading() requires |
| 544 | * a dummy thread even if CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN=y |
| 545 | */ |
| 546 | struct k_thread dummy_thread; |
Andy Ross | 6d9106f | 2019-02-01 14:42:28 -0800 | [diff] [blame] | 547 | |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 548 | z_dummy_thread_init(&dummy_thread); |
Andy Ross | 6d9106f | 2019-02-01 14:42:28 -0800 | [diff] [blame] | 549 | #endif |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 550 | /* do any necessary initialization of static devices */ |
| 551 | z_device_state_init(); |
| 552 | |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 553 | /* perform basic hardware initialization */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 554 | z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_1); |
| 555 | z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_2); |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 556 | |
Mazen NEIFER | e2bbad9 | 2017-02-07 10:01:12 +0100 | [diff] [blame] | 557 | #ifdef CONFIG_STACK_CANARIES |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 558 | uintptr_t stack_guard; |
| 559 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 560 | z_early_boot_rand_get((uint8_t *)&stack_guard, sizeof(stack_guard)); |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 561 | __stack_chk_guard = stack_guard; |
| 562 | __stack_chk_guard <<= 8; |
| 563 | #endif /* CONFIG_STACK_CANARIES */ |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 564 | |
Daniel Leung | 1559712 | 2021-03-31 13:40:01 -0700 | [diff] [blame] | 565 | #ifdef CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT |
Daniel Leung | fd7a68d | 2020-10-14 12:17:12 -0700 | [diff] [blame] | 566 | timing_init(); |
| 567 | timing_start(); |
| 568 | #endif |
| 569 | |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 570 | #ifdef CONFIG_MULTITHREADING |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 571 | switch_to_main_thread(prepare_multithreading()); |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 572 | #else |
Ioannis Glaropoulos | 60bd51a | 2020-08-03 11:11:19 +0200 | [diff] [blame] | 573 | #ifdef ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING |
| 574 | /* Custom ARCH-specific routine to switch to main() |
| 575 | * in the case of no multi-threading. |
| 576 | */ |
| 577 | ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING(bg_thread_main, |
| 578 | NULL, NULL, NULL); |
| 579 | #else |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 580 | bg_thread_main(NULL, NULL, NULL); |
| 581 | |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 582 | /* LCOV_EXCL_START |
| 583 | * We've already dumped coverage data at this point. |
| 584 | */ |
Andy Ross | 8daafd4 | 2018-08-30 09:45:12 -0700 | [diff] [blame] | 585 | irq_lock(); |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 586 | while (true) { |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 587 | } |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 588 | /* LCOV_EXCL_STOP */ |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 589 | #endif |
Ioannis Glaropoulos | 60bd51a | 2020-08-03 11:11:19 +0200 | [diff] [blame] | 590 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * Compiler can't tell that the above routines won't return and issues |
| 594 | * a warning unless we explicitly tell it that control never gets this |
| 595 | * far. |
| 596 | */ |
| 597 | |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 598 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 599 | } |