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> |
Flavio Ceolin | e7bd10a | 2023-10-06 22:38:53 +0000 | [diff] [blame] | 18 | #include <zephyr/random/random.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 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> |
Anas Nashif | 3ca50f5 | 2024-02-23 10:53:01 -0500 | [diff] [blame] | 26 | #include <kthread.h> |
Mahavir Jain | acea241 | 2016-12-02 21:48:39 +0530 | [diff] [blame] | 27 | #include <string.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 28 | #include <zephyr/sys/dlist.h> |
Andy Ross | 245b54e | 2018-02-08 09:10:46 -0800 | [diff] [blame] | 29 | #include <kernel_internal.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 30 | #include <zephyr/drivers/entropy.h> |
| 31 | #include <zephyr/logging/log_ctrl.h> |
| 32 | #include <zephyr/tracing/tracing.h> |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 33 | #include <stdbool.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 34 | #include <zephyr/debug/gcov.h> |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 35 | #include <kswap.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 36 | #include <zephyr/timing/timing.h> |
| 37 | #include <zephyr/logging/log.h> |
Jordan Yates | db3d51b | 2022-03-12 21:10:42 +1000 | [diff] [blame] | 38 | #include <zephyr/pm/device_runtime.h> |
Ederson de Souza | eeebb4d | 2024-01-05 13:29:20 -0800 | [diff] [blame] | 39 | #include <zephyr/internal/syscall_handler.h> |
Krzysztof Chruscinski | 3ed8083 | 2020-11-26 19:32:34 +0100 | [diff] [blame] | 40 | LOG_MODULE_REGISTER(os, CONFIG_KERNEL_LOG_LEVEL); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 41 | |
Kumar Gala | 0722b6f | 2022-10-21 09:34:08 -0500 | [diff] [blame] | 42 | BUILD_ASSERT(CONFIG_MP_NUM_CPUS == CONFIG_MP_MAX_NUM_CPUS, |
| 43 | "CONFIG_MP_NUM_CPUS and CONFIG_MP_MAX_NUM_CPUS need to be set the same"); |
| 44 | |
Krzysztof Chruscinski | 7dcff6e | 2021-04-16 15:16:00 +0200 | [diff] [blame] | 45 | /* the only struct z_kernel instance */ |
Qipeng Zha | fa973d1 | 2023-05-15 13:29:18 +0800 | [diff] [blame] | 46 | __pinned_bss |
Krzysztof Chruscinski | 7dcff6e | 2021-04-16 15:16:00 +0200 | [diff] [blame] | 47 | struct z_kernel _kernel; |
| 48 | |
Flavio Ceolin | 4f29930 | 2023-05-30 11:49:45 -0700 | [diff] [blame] | 49 | __pinned_bss |
| 50 | atomic_t _cpus_active; |
| 51 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 52 | /* init/main and idle threads */ |
Daniel Leung | ebbfde9 | 2021-07-12 13:47:48 -0700 | [diff] [blame] | 53 | K_THREAD_PINNED_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE); |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 54 | struct k_thread z_main_thread; |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 55 | |
| 56 | #ifdef CONFIG_MULTITHREADING |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 57 | __pinned_bss |
Kumar Gala | c778eb2 | 2022-10-12 10:55:36 -0500 | [diff] [blame] | 58 | struct k_thread z_idle_threads[CONFIG_MP_MAX_NUM_CPUS]; |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 59 | |
| 60 | static K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_idle_stacks, |
Kumar Gala | c778eb2 | 2022-10-12 10:55:36 -0500 | [diff] [blame] | 61 | CONFIG_MP_MAX_NUM_CPUS, |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 62 | CONFIG_IDLE_STACK_SIZE); |
Anas Nashif | 3ca50f5 | 2024-02-23 10:53:01 -0500 | [diff] [blame] | 63 | |
| 64 | static void z_init_static_threads(void) |
| 65 | { |
| 66 | STRUCT_SECTION_FOREACH(_static_thread_data, thread_data) { |
| 67 | z_setup_new_thread( |
| 68 | thread_data->init_thread, |
| 69 | thread_data->init_stack, |
| 70 | thread_data->init_stack_size, |
| 71 | thread_data->init_entry, |
| 72 | thread_data->init_p1, |
| 73 | thread_data->init_p2, |
| 74 | thread_data->init_p3, |
| 75 | thread_data->init_prio, |
| 76 | thread_data->init_options, |
| 77 | thread_data->init_name); |
| 78 | |
| 79 | thread_data->init_thread->init_data = thread_data; |
| 80 | } |
| 81 | |
| 82 | #ifdef CONFIG_USERSPACE |
| 83 | STRUCT_SECTION_FOREACH(k_object_assignment, pos) { |
| 84 | for (int i = 0; pos->objects[i] != NULL; i++) { |
| 85 | k_object_access_grant(pos->objects[i], |
| 86 | pos->thread); |
| 87 | } |
| 88 | } |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 89 | #endif /* CONFIG_USERSPACE */ |
Anas Nashif | 3ca50f5 | 2024-02-23 10:53:01 -0500 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Non-legacy static threads may be started immediately or |
| 93 | * after a previously specified delay. Even though the |
| 94 | * scheduler is locked, ticks can still be delivered and |
| 95 | * processed. Take a sched lock to prevent them from running |
| 96 | * until they are all started. |
| 97 | * |
| 98 | * Note that static threads defined using the legacy API have a |
| 99 | * delay of K_FOREVER. |
| 100 | */ |
| 101 | k_sched_lock(); |
| 102 | STRUCT_SECTION_FOREACH(_static_thread_data, thread_data) { |
| 103 | k_timeout_t init_delay = Z_THREAD_INIT_DELAY(thread_data); |
| 104 | |
| 105 | if (!K_TIMEOUT_EQ(init_delay, K_FOREVER)) { |
| 106 | thread_schedule_new(thread_data->init_thread, |
| 107 | init_delay); |
| 108 | } |
| 109 | } |
| 110 | k_sched_unlock(); |
| 111 | } |
| 112 | #else |
| 113 | #define z_init_static_threads() do { } while (false) |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 114 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 115 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 116 | extern const struct init_entry __init_start[]; |
| 117 | extern const struct init_entry __init_EARLY_start[]; |
| 118 | extern const struct init_entry __init_PRE_KERNEL_1_start[]; |
| 119 | extern const struct init_entry __init_PRE_KERNEL_2_start[]; |
| 120 | extern const struct init_entry __init_POST_KERNEL_start[]; |
| 121 | extern const struct init_entry __init_APPLICATION_start[]; |
| 122 | extern const struct init_entry __init_end[]; |
| 123 | |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 124 | enum init_level { |
| 125 | INIT_LEVEL_EARLY = 0, |
| 126 | INIT_LEVEL_PRE_KERNEL_1, |
| 127 | INIT_LEVEL_PRE_KERNEL_2, |
| 128 | INIT_LEVEL_POST_KERNEL, |
| 129 | INIT_LEVEL_APPLICATION, |
| 130 | #ifdef CONFIG_SMP |
| 131 | INIT_LEVEL_SMP, |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 132 | #endif /* CONFIG_SMP */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_SMP |
| 136 | extern const struct init_entry __init_SMP_start[]; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 137 | #endif /* CONFIG_SMP */ |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 138 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 139 | /* |
| 140 | * storage space for the interrupt stack |
| 141 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 142 | * Note: This area is used as the system stack during kernel initialization, |
| 143 | * since the kernel hasn't yet set up its own stack areas. The dual purposing |
| 144 | * of this area is safe since interrupts are disabled until the kernel context |
| 145 | * switches to the init thread. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 146 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 147 | K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_interrupt_stacks, |
Kumar Gala | c778eb2 | 2022-10-12 10:55:36 -0500 | [diff] [blame] | 148 | CONFIG_MP_MAX_NUM_CPUS, |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 149 | CONFIG_ISR_STACK_SIZE); |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 150 | |
Peter Mitsis | 96cb05c | 2016-09-15 12:37:58 -0400 | [diff] [blame] | 151 | extern void idle(void *unused1, void *unused2, void *unused3); |
| 152 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 153 | #ifdef CONFIG_OBJ_CORE_SYSTEM |
| 154 | static struct k_obj_type obj_type_cpu; |
| 155 | static struct k_obj_type obj_type_kernel; |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 156 | |
| 157 | #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM |
| 158 | static struct k_obj_core_stats_desc cpu_stats_desc = { |
| 159 | .raw_size = sizeof(struct k_cycle_stats), |
| 160 | .query_size = sizeof(struct k_thread_runtime_stats), |
| 161 | .raw = z_cpu_stats_raw, |
| 162 | .query = z_cpu_stats_query, |
| 163 | .reset = NULL, |
| 164 | .disable = NULL, |
| 165 | .enable = NULL, |
| 166 | }; |
| 167 | |
| 168 | static struct k_obj_core_stats_desc kernel_stats_desc = { |
| 169 | .raw_size = sizeof(struct k_cycle_stats) * CONFIG_MP_MAX_NUM_CPUS, |
| 170 | .query_size = sizeof(struct k_thread_runtime_stats), |
| 171 | .raw = z_kernel_stats_raw, |
| 172 | .query = z_kernel_stats_query, |
| 173 | .reset = NULL, |
| 174 | .disable = NULL, |
| 175 | .enable = NULL, |
| 176 | }; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 177 | #endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */ |
| 178 | #endif /* CONFIG_OBJ_CORE_SYSTEM */ |
Carles Cufi | cb0cf9f | 2017-01-10 10:57:38 +0100 | [diff] [blame] | 179 | |
Andrew Boie | fe228a8 | 2019-06-11 12:49:32 -0700 | [diff] [blame] | 180 | /* LCOV_EXCL_START |
| 181 | * |
| 182 | * This code is called so early in the boot process that code coverage |
| 183 | * doesn't work properly. In addition, not all arches call this code, |
| 184 | * some like x86 do this with optimized assembly |
| 185 | */ |
| 186 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 187 | /** |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 188 | * @brief equivalent of memset() for early boot usage |
| 189 | * |
| 190 | * Architectures that can't safely use the regular (optimized) memset very |
| 191 | * early during boot because e.g. hardware isn't yet sufficiently initialized |
| 192 | * may override this with their own safe implementation. |
| 193 | */ |
| 194 | __boot_func |
| 195 | void __weak z_early_memset(void *dst, int c, size_t n) |
| 196 | { |
| 197 | (void) memset(dst, c, n); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @brief equivalent of memcpy() for early boot usage |
| 202 | * |
| 203 | * Architectures that can't safely use the regular (optimized) memcpy very |
| 204 | * early during boot because e.g. hardware isn't yet sufficiently initialized |
| 205 | * may override this with their own safe implementation. |
| 206 | */ |
| 207 | __boot_func |
| 208 | void __weak z_early_memcpy(void *dst, const void *src, size_t n) |
| 209 | { |
| 210 | (void) memcpy(dst, src, n); |
| 211 | } |
| 212 | |
| 213 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 214 | * @brief Clear BSS |
| 215 | * |
| 216 | * This routine clears the BSS region, so all bytes are 0. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 217 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 218 | __boot_func |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 219 | void z_bss_zero(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 220 | { |
Alexander Razinkov | d2c101d | 2023-10-27 18:33:15 +0300 | [diff] [blame] | 221 | if (IS_ENABLED(CONFIG_SKIP_BSS_CLEAR)) { |
Andy Ross | 5722da7 | 2022-08-09 17:55:14 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 225 | z_early_memset(__bss_start, 0, __bss_end - __bss_start); |
Martà BolÃvar | 6e8775f | 2020-05-11 11:56:08 -0700 | [diff] [blame] | 226 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 227 | z_early_memset(&__ccm_bss_start, 0, |
| 228 | (uintptr_t) &__ccm_bss_end |
| 229 | - (uintptr_t) &__ccm_bss_start); |
Erwin Rol | 1dc41d1 | 2017-10-05 01:22:32 +0200 | [diff] [blame] | 230 | #endif |
Martà BolÃvar | 6e8775f | 2020-05-11 11:56:08 -0700 | [diff] [blame] | 231 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 232 | z_early_memset(&__dtcm_bss_start, 0, |
| 233 | (uintptr_t) &__dtcm_bss_end |
| 234 | - (uintptr_t) &__dtcm_bss_start); |
Alexander Wachter | b4c5f4b | 2019-07-03 14:19:29 +0200 | [diff] [blame] | 235 | #endif |
Immo Birnbaum | da28829 | 2022-01-21 12:38:30 +0100 | [diff] [blame] | 236 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ocm), okay) |
Nicolas Pitre | 3617398 | 2022-02-22 00:00:52 -0500 | [diff] [blame] | 237 | z_early_memset(&__ocm_bss_start, 0, |
| 238 | (uintptr_t) &__ocm_bss_end |
| 239 | - (uintptr_t) &__ocm_bss_start); |
Immo Birnbaum | da28829 | 2022-01-21 12:38:30 +0100 | [diff] [blame] | 240 | #endif |
Adithya Baglody | 91c5b84 | 2018-11-13 16:57:45 +0530 | [diff] [blame] | 241 | #ifdef CONFIG_CODE_DATA_RELOCATION |
| 242 | extern void bss_zeroing_relocation(void); |
| 243 | |
| 244 | bss_zeroing_relocation(); |
| 245 | #endif /* CONFIG_CODE_DATA_RELOCATION */ |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 246 | #ifdef CONFIG_COVERAGE_GCOV |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 247 | z_early_memset(&__gcov_bss_start, 0, |
| 248 | ((uintptr_t) &__gcov_bss_end - (uintptr_t) &__gcov_bss_start)); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 249 | #endif /* CONFIG_COVERAGE_GCOV */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Daniel Leung | d812728 | 2021-02-24 10:18:34 -0800 | [diff] [blame] | 252 | #ifdef CONFIG_LINKER_USE_BOOT_SECTION |
| 253 | /** |
| 254 | * @brief Clear BSS within the bot region |
| 255 | * |
| 256 | * This routine clears the BSS within the boot region. |
| 257 | * This is separate from z_bss_zero() as boot region may |
| 258 | * contain symbols required for the boot process before |
| 259 | * paging is initialized. |
| 260 | */ |
| 261 | __boot_func |
| 262 | void z_bss_zero_boot(void) |
| 263 | { |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 264 | z_early_memset(&lnkr_boot_bss_start, 0, |
| 265 | (uintptr_t)&lnkr_boot_bss_end |
| 266 | - (uintptr_t)&lnkr_boot_bss_start); |
Daniel Leung | d812728 | 2021-02-24 10:18:34 -0800 | [diff] [blame] | 267 | } |
| 268 | #endif /* CONFIG_LINKER_USE_BOOT_SECTION */ |
| 269 | |
Daniel Leung | 1310ad6 | 2021-02-23 13:33:38 -0800 | [diff] [blame] | 270 | #ifdef CONFIG_LINKER_USE_PINNED_SECTION |
| 271 | /** |
| 272 | * @brief Clear BSS within the pinned region |
| 273 | * |
| 274 | * This routine clears the BSS within the pinned region. |
| 275 | * This is separate from z_bss_zero() as pinned region may |
| 276 | * contain symbols required for the boot process before |
| 277 | * paging is initialized. |
| 278 | */ |
| 279 | #ifdef CONFIG_LINKER_USE_BOOT_SECTION |
| 280 | __boot_func |
| 281 | #else |
| 282 | __pinned_func |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 283 | #endif /* CONFIG_LINKER_USE_BOOT_SECTION */ |
Daniel Leung | 1310ad6 | 2021-02-23 13:33:38 -0800 | [diff] [blame] | 284 | void z_bss_zero_pinned(void) |
| 285 | { |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 286 | z_early_memset(&lnkr_pinned_bss_start, 0, |
| 287 | (uintptr_t)&lnkr_pinned_bss_end |
| 288 | - (uintptr_t)&lnkr_pinned_bss_start); |
Daniel Leung | 1310ad6 | 2021-02-23 13:33:38 -0800 | [diff] [blame] | 289 | } |
| 290 | #endif /* CONFIG_LINKER_USE_PINNED_SECTION */ |
| 291 | |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 292 | #ifdef CONFIG_STACK_CANARIES |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 293 | #ifdef CONFIG_STACK_CANARIES_TLS |
| 294 | extern __thread volatile uintptr_t __stack_chk_guard; |
| 295 | #else |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 296 | extern volatile uintptr_t __stack_chk_guard; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 297 | #endif /* CONFIG_STACK_CANARIES_TLS */ |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 298 | #endif /* CONFIG_STACK_CANARIES */ |
| 299 | |
Andrew Boie | fe228a8 | 2019-06-11 12:49:32 -0700 | [diff] [blame] | 300 | /* LCOV_EXCL_STOP */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 301 | |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 302 | __pinned_bss |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 303 | bool z_sys_post_kernel; |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 304 | |
Ederson de Souza | eeebb4d | 2024-01-05 13:29:20 -0800 | [diff] [blame] | 305 | static int do_device_init(const struct init_entry *entry) |
| 306 | { |
| 307 | const struct device *dev = entry->dev; |
| 308 | int rc = 0; |
| 309 | |
| 310 | if (entry->init_fn.dev != NULL) { |
| 311 | rc = entry->init_fn.dev(dev); |
| 312 | /* Mark device initialized. If initialization |
| 313 | * failed, record the error condition. |
| 314 | */ |
| 315 | if (rc != 0) { |
| 316 | if (rc < 0) { |
| 317 | rc = -rc; |
| 318 | } |
| 319 | if (rc > UINT8_MAX) { |
| 320 | rc = UINT8_MAX; |
| 321 | } |
| 322 | dev->state->init_res = rc; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | dev->state->initialized = true; |
| 327 | |
| 328 | if (rc == 0) { |
| 329 | /* Run automatic device runtime enablement */ |
| 330 | (void)pm_device_runtime_auto_enable(dev); |
| 331 | } |
| 332 | |
| 333 | return rc; |
| 334 | } |
| 335 | |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 336 | /** |
| 337 | * @brief Execute all the init entry initialization functions at a given level |
| 338 | * |
| 339 | * @details Invokes the initialization routine for each init entry object |
| 340 | * created by the INIT_ENTRY_DEFINE() macro using the specified level. |
| 341 | * The linker script places the init entry objects in memory in the order |
| 342 | * they need to be invoked, with symbols indicating where one level leaves |
| 343 | * off and the next one begins. |
| 344 | * |
| 345 | * @param level init level to run. |
| 346 | */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 347 | static void z_sys_init_run_level(enum init_level level) |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 348 | { |
| 349 | static const struct init_entry *levels[] = { |
| 350 | __init_EARLY_start, |
| 351 | __init_PRE_KERNEL_1_start, |
| 352 | __init_PRE_KERNEL_2_start, |
| 353 | __init_POST_KERNEL_start, |
| 354 | __init_APPLICATION_start, |
| 355 | #ifdef CONFIG_SMP |
| 356 | __init_SMP_start, |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 357 | #endif /* CONFIG_SMP */ |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 358 | /* End marker */ |
| 359 | __init_end, |
| 360 | }; |
| 361 | const struct init_entry *entry; |
| 362 | |
| 363 | for (entry = levels[level]; entry < levels[level+1]; entry++) { |
| 364 | const struct device *dev = entry->dev; |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 365 | |
| 366 | if (dev != NULL) { |
Ederson de Souza | eeebb4d | 2024-01-05 13:29:20 -0800 | [diff] [blame] | 367 | do_device_init(entry); |
Gerard Marull-Paretas | a5fd0d1 | 2022-10-19 09:33:44 +0200 | [diff] [blame] | 368 | } else { |
| 369 | (void)entry->init_fn.sys(); |
Gerard Marull-Paretas | 8312393 | 2022-10-04 11:25:37 +0200 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Ederson de Souza | eeebb4d | 2024-01-05 13:29:20 -0800 | [diff] [blame] | 374 | |
| 375 | int z_impl_device_init(const struct device *dev) |
| 376 | { |
| 377 | if (dev == NULL) { |
| 378 | return -ENOENT; |
| 379 | } |
| 380 | |
| 381 | STRUCT_SECTION_FOREACH_ALTERNATE(_deferred_init, init_entry, entry) { |
| 382 | if (entry->dev == dev) { |
| 383 | return do_device_init(entry); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return -ENOENT; |
| 388 | } |
| 389 | |
| 390 | #ifdef CONFIG_USERSPACE |
| 391 | static inline int z_vrfy_device_init(const struct device *dev) |
| 392 | { |
| 393 | K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY)); |
| 394 | |
| 395 | return z_impl_device_init(dev); |
| 396 | } |
| 397 | #include <syscalls/device_init_mrsh.c> |
| 398 | #endif |
| 399 | |
Anas Nashif | 4b59312 | 2020-08-27 09:08:40 -0400 | [diff] [blame] | 400 | extern void boot_banner(void); |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 401 | |
Anas Nashif | 3ca50f5 | 2024-02-23 10:53:01 -0500 | [diff] [blame] | 402 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 403 | /** |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 404 | * @brief Mainline for kernel's background thread |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 405 | * |
| 406 | * This routine completes kernel initialization by invoking the remaining |
| 407 | * init functions, then invokes application's main() routine. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 408 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 409 | __boot_func |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 410 | static void bg_thread_main(void *unused1, void *unused2, void *unused3) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 411 | { |
| 412 | ARG_UNUSED(unused1); |
| 413 | ARG_UNUSED(unused2); |
| 414 | ARG_UNUSED(unused3); |
| 415 | |
Andrew Boie | e35f179 | 2020-12-09 12:18:40 -0800 | [diff] [blame] | 416 | #ifdef CONFIG_MMU |
| 417 | /* Invoked here such that backing store or eviction algorithms may |
| 418 | * initialize kernel objects, and that all POST_KERNEL and later tasks |
| 419 | * may perform memory management tasks (except for z_phys_map() which |
| 420 | * is allowed at any time) |
| 421 | */ |
| 422 | z_mem_manage_init(); |
| 423 | #endif /* CONFIG_MMU */ |
Peter Bigot | 74ef395 | 2019-12-23 11:48:43 -0600 | [diff] [blame] | 424 | z_sys_post_kernel = true; |
| 425 | |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 426 | z_sys_init_run_level(INIT_LEVEL_POST_KERNEL); |
Hess Nathan | 527e712 | 2024-04-29 14:08:04 +0200 | [diff] [blame] | 427 | #if defined(CONFIG_STACK_POINTER_RANDOM) && (CONFIG_STACK_POINTER_RANDOM != 0) |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 428 | z_stack_adjust_initialized = 1; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 429 | #endif /* CONFIG_STACK_POINTER_RANDOM */ |
Anas Nashif | 4b59312 | 2020-08-27 09:08:40 -0400 | [diff] [blame] | 430 | boot_banner(); |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 431 | |
Stephanos Ioannidis | 4a64bfe | 2022-12-09 06:16:44 +0900 | [diff] [blame] | 432 | #if defined(CONFIG_CPP) |
Evgeniy Paltsev | 497cb2e | 2021-04-26 16:15:49 +0300 | [diff] [blame] | 433 | void z_cpp_init_static(void); |
| 434 | z_cpp_init_static(); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 435 | #endif /* CONFIG_CPP */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 436 | |
Peter Bigot | c308f80 | 2020-05-08 09:28:44 -0500 | [diff] [blame] | 437 | /* Final init level before app starts */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 438 | z_sys_init_run_level(INIT_LEVEL_APPLICATION); |
Peter Bigot | c308f80 | 2020-05-08 09:28:44 -0500 | [diff] [blame] | 439 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 440 | z_init_static_threads(); |
Anas Nashif | 83088a2 | 2017-08-24 04:27:51 -0400 | [diff] [blame] | 441 | |
Anas Nashif | 39f632e | 2020-12-07 13:15:42 -0500 | [diff] [blame] | 442 | #ifdef CONFIG_KERNEL_COHERENCE |
Daniel Leung | 079bc64 | 2021-02-02 16:12:15 -0800 | [diff] [blame] | 443 | __ASSERT_NO_MSG(arch_mem_coherent(&_kernel)); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 444 | #endif /* CONFIG_KERNEL_COHERENCE */ |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 445 | |
Andy Ross | eb25870 | 2018-04-12 12:10:10 -0700 | [diff] [blame] | 446 | #ifdef CONFIG_SMP |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 447 | if (!IS_ENABLED(CONFIG_SMP_BOOT_DELAY)) { |
| 448 | z_smp_init(); |
| 449 | } |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 450 | z_sys_init_run_level(INIT_LEVEL_SMP); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 451 | #endif /* CONFIG_SMP */ |
Inaky Perez-Gonzalez | c51f73f | 2017-06-20 17:01:09 -0700 | [diff] [blame] | 452 | |
Daniel Leung | e88afd2 | 2021-07-15 13:15:29 -0700 | [diff] [blame] | 453 | #ifdef CONFIG_MMU |
| 454 | z_mem_manage_boot_finish(); |
| 455 | #endif /* CONFIG_MMU */ |
| 456 | |
Stephanos Ioannidis | fa5fd41 | 2022-11-05 00:22:25 +0900 | [diff] [blame] | 457 | extern int main(void); |
Andrew Boie | f1c373c | 2016-10-28 12:45:05 -0700 | [diff] [blame] | 458 | |
Stephanos Ioannidis | fa5fd41 | 2022-11-05 00:22:25 +0900 | [diff] [blame] | 459 | (void)main(); |
Allan Stephens | 073442e | 2016-11-09 07:46:56 -0600 | [diff] [blame] | 460 | |
Anas Nashif | 8791012 | 2024-02-22 22:24:36 -0500 | [diff] [blame] | 461 | /* Mark non-essential since main() has no more work to do */ |
| 462 | z_thread_essential_clear(&z_main_thread); |
Andrew Boie | 8e05333 | 2019-06-11 12:58:16 -0700 | [diff] [blame] | 463 | |
Anas Nashif | 471ffbe | 2020-01-30 08:44:10 -0500 | [diff] [blame] | 464 | #ifdef CONFIG_COVERAGE_DUMP |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 465 | /* Dump coverage data once the main() has exited. */ |
| 466 | gcov_coverage_dump(); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 467 | #endif /* CONFIG_COVERAGE_DUMP */ |
Andrew Boie | 8e05333 | 2019-06-11 12:58:16 -0700 | [diff] [blame] | 468 | } /* LCOV_EXCL_LINE ... because we just dumped final coverage data */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 469 | |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 470 | #if defined(CONFIG_MULTITHREADING) |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 471 | __boot_func |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 472 | static void init_idle_thread(int i) |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 473 | { |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 474 | struct k_thread *thread = &z_idle_threads[i]; |
| 475 | k_thread_stack_t *stack = z_idle_stacks[i]; |
| 476 | |
| 477 | #ifdef CONFIG_THREAD_NAME |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 478 | |
Kumar Gala | 4f458ba | 2022-10-18 11:11:46 -0500 | [diff] [blame] | 479 | #if CONFIG_MP_MAX_NUM_CPUS > 1 |
Trond Einar Snekvik | 6224ecb | 2022-03-25 12:46:32 +0100 | [diff] [blame] | 480 | char tname[8]; |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 481 | snprintk(tname, 8, "idle %02d", i); |
| 482 | #else |
Trond Einar Snekvik | 6224ecb | 2022-03-25 12:46:32 +0100 | [diff] [blame] | 483 | char *tname = "idle"; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 484 | #endif /* CONFIG_MP_MAX_NUM_CPUS */ |
Trond Einar Snekvik | 6224ecb | 2022-03-25 12:46:32 +0100 | [diff] [blame] | 485 | |
| 486 | #else |
Andrew Boie | c24673e | 2020-03-16 09:44:28 -0700 | [diff] [blame] | 487 | char *tname = NULL; |
| 488 | #endif /* CONFIG_THREAD_NAME */ |
| 489 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 490 | z_setup_new_thread(thread, stack, |
Andrew Boie | f5a7e1a | 2020-09-02 09:20:38 -0700 | [diff] [blame] | 491 | CONFIG_IDLE_STACK_SIZE, idle, &_kernel.cpus[i], |
Andy Ross | 851d14a | 2021-05-13 15:46:43 -0700 | [diff] [blame] | 492 | NULL, NULL, K_IDLE_PRIO, K_ESSENTIAL, |
Andrew Boie | f5a7e1a | 2020-09-02 09:20:38 -0700 | [diff] [blame] | 493 | tname); |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 494 | z_mark_thread_as_started(thread); |
Andy Ross | 6c283ca | 2019-08-16 22:09:30 -0700 | [diff] [blame] | 495 | |
| 496 | #ifdef CONFIG_SMP |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 497 | thread->base.is_idle = 1U; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 498 | #endif /* CONFIG_SMP */ |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 499 | } |
Andy Ross | 780ba23 | 2018-01-29 09:20:18 -0800 | [diff] [blame] | 500 | |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 501 | void z_init_cpu(int id) |
Andy Ross | c6d077e | 2021-08-18 06:28:11 -0700 | [diff] [blame] | 502 | { |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 503 | init_idle_thread(id); |
| 504 | _kernel.cpus[id].idle_thread = &z_idle_threads[id]; |
| 505 | _kernel.cpus[id].id = id; |
| 506 | _kernel.cpus[id].irq_stack = |
Daniel Leung | b69d248 | 2024-03-22 12:56:12 -0700 | [diff] [blame] | 507 | (K_KERNEL_STACK_BUFFER(z_interrupt_stacks[id]) + |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 508 | K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[id])); |
| 509 | #ifdef CONFIG_SCHED_THREAD_USAGE_ALL |
Peter Mitsis | 9bedfd8 | 2023-05-23 18:36:04 -0400 | [diff] [blame] | 510 | _kernel.cpus[id].usage = &_kernel.usage[id]; |
| 511 | _kernel.cpus[id].usage->track_usage = |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 512 | CONFIG_SCHED_THREAD_USAGE_AUTO_ENABLE; |
| 513 | #endif |
Flavio Ceolin | 4f29930 | 2023-05-30 11:49:45 -0700 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * Increment number of CPUs active. The pm subsystem |
| 517 | * will keep track of this from here. |
| 518 | */ |
| 519 | atomic_inc(&_cpus_active); |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 520 | |
| 521 | #ifdef CONFIG_OBJ_CORE_SYSTEM |
| 522 | k_obj_core_init_and_link(K_OBJ_CORE(&_kernel.cpus[id]), &obj_type_cpu); |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 523 | #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM |
| 524 | k_obj_core_stats_register(K_OBJ_CORE(&_kernel.cpus[id]), |
| 525 | _kernel.cpus[id].usage, |
| 526 | sizeof(struct k_cycle_stats)); |
| 527 | #endif |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 528 | #endif |
Andy Ross | c6d077e | 2021-08-18 06:28:11 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 531 | /** |
| 532 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 533 | * @brief Initializes kernel data structures |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 534 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 535 | * This routine initializes various kernel data structures, including |
| 536 | * the init and idle threads and any architecture-specific initialization. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 537 | * |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 538 | * 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] | 539 | * be all the initialization many of them require. |
| 540 | * |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 541 | * @return initial stack pointer for the main thread |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 542 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 543 | __boot_func |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 544 | static char *prepare_multithreading(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 545 | { |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 546 | char *stack_ptr; |
| 547 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 548 | /* _kernel.ready_q is all zeroes */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 549 | z_sched_init(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 550 | |
Andy Ross | 2724fd1 | 2018-01-29 14:55:20 -0800 | [diff] [blame] | 551 | #ifndef CONFIG_SMP |
Benjamin Walsh | 88b3691 | 2016-12-02 10:37:27 -0500 | [diff] [blame] | 552 | /* |
| 553 | * prime the cache with the main thread since: |
| 554 | * |
| 555 | * - the cache can never be NULL |
| 556 | * - the main thread will be the one to run first |
| 557 | * - no other thread is initialized yet and thus their priority fields |
| 558 | * contain garbage, which would prevent the cache loading algorithm |
| 559 | * to work as intended |
| 560 | */ |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 561 | _kernel.ready_q.cache = &z_main_thread; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 562 | #endif /* CONFIG_SMP */ |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 563 | stack_ptr = z_setup_new_thread(&z_main_thread, z_main_stack, |
| 564 | CONFIG_MAIN_STACK_SIZE, bg_thread_main, |
| 565 | NULL, NULL, NULL, |
| 566 | CONFIG_MAIN_THREAD_PRIORITY, |
Ioannis Glaropoulos | 40aab32 | 2021-01-28 21:46:28 +0100 | [diff] [blame] | 567 | K_ESSENTIAL, "main"); |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 568 | z_mark_thread_as_started(&z_main_thread); |
| 569 | z_ready_thread(&z_main_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 570 | |
Andy Ross | 2b210cb | 2022-01-17 11:56:54 -0800 | [diff] [blame] | 571 | z_init_cpu(0); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 572 | |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 573 | return stack_ptr; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 576 | __boot_func |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 577 | static FUNC_NORETURN void switch_to_main_thread(char *stack_ptr) |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 578 | { |
Benjamin Walsh | 296a234 | 2016-11-20 11:04:31 -0500 | [diff] [blame] | 579 | #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 580 | 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] | 581 | #else |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 582 | ARG_UNUSED(stack_ptr); |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 583 | /* |
| 584 | * Context switch to main task (entry function is _main()): the |
| 585 | * current fake thread is not on a wait queue or ready queue, so it |
| 586 | * will never be rescheduled in. |
| 587 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 588 | z_swap_unlocked(); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 589 | #endif /* CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN */ |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 590 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 591 | } |
Anas Nashif | c0ea505 | 2019-01-30 09:58:41 -0500 | [diff] [blame] | 592 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | c742d7e | 2016-10-05 17:50:54 -0400 | [diff] [blame] | 593 | |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 594 | __boot_func |
Flavio Ceolin | 974e336 | 2023-10-09 15:25:42 -0700 | [diff] [blame] | 595 | void __weak z_early_rand_get(uint8_t *buf, size_t length) |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 596 | { |
Flavio Ceolin | 991ff6f | 2023-10-10 10:31:35 -0700 | [diff] [blame] | 597 | static uint64_t state = (uint64_t)CONFIG_TIMER_RANDOM_INITIAL_STATE; |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 598 | int rc; |
| 599 | |
Flavio Ceolin | 6c0fad2 | 2023-10-09 20:52:07 -0700 | [diff] [blame] | 600 | #ifdef CONFIG_ENTROPY_HAS_DRIVER |
| 601 | const struct device *const entropy = DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_entropy)); |
| 602 | |
| 603 | if ((entropy != NULL) && device_is_ready(entropy)) { |
| 604 | /* Try to see if driver provides an ISR-specific API */ |
| 605 | rc = entropy_get_entropy_isr(entropy, buf, length, ENTROPY_BUSYWAIT); |
| 606 | if (rc > 0) { |
| 607 | length -= rc; |
| 608 | buf += rc; |
| 609 | } |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 610 | } |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 611 | #endif /* CONFIG_ENTROPY_HAS_DRIVER */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 612 | |
Flavio Ceolin | 6c0fad2 | 2023-10-09 20:52:07 -0700 | [diff] [blame] | 613 | while (length > 0) { |
| 614 | uint32_t val; |
| 615 | |
| 616 | state = state + k_cycle_get_32(); |
| 617 | state = state * 2862933555777941757ULL + 3037000493ULL; |
| 618 | val = (uint32_t)(state >> 32); |
| 619 | rc = MIN(length, sizeof(val)); |
| 620 | z_early_memcpy((void *)buf, &val, rc); |
| 621 | |
| 622 | length -= rc; |
| 623 | buf += rc; |
| 624 | } |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 627 | /** |
| 628 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 629 | * @brief Initialize kernel |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 630 | * |
| 631 | * This routine is invoked when the system is ready to run C code. The |
| 632 | * processor must be running in 32-bit mode, and the BSS must have been |
| 633 | * cleared/zeroed. |
| 634 | * |
| 635 | * @return Does not return |
| 636 | */ |
Daniel Leung | 660d147 | 2021-03-25 16:05:15 -0700 | [diff] [blame] | 637 | __boot_func |
Daniel Leung | 256db60 | 2022-12-15 15:54:56 -0800 | [diff] [blame] | 638 | FUNC_NO_STACK_PROTECTOR |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 639 | FUNC_NORETURN void z_cstart(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 640 | { |
Adithya Baglody | 71e90f9 | 2018-08-29 16:44:16 +0530 | [diff] [blame] | 641 | /* gcov hook needed to get the coverage report.*/ |
| 642 | gcov_static_init(); |
| 643 | |
Gerard Marull-Paretas | e42f58e | 2022-10-11 17:17:18 +0200 | [diff] [blame] | 644 | /* initialize early init calls */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 645 | z_sys_init_run_level(INIT_LEVEL_EARLY); |
Gerard Marull-Paretas | e42f58e | 2022-10-11 17:17:18 +0200 | [diff] [blame] | 646 | |
Andrew Boie | 982d5c8 | 2018-05-23 13:30:34 -0700 | [diff] [blame] | 647 | /* perform any architecture-specific initialization */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 648 | arch_kernel_init(); |
Andrew Boie | 982d5c8 | 2018-05-23 13:30:34 -0700 | [diff] [blame] | 649 | |
Ederson de Souza | 75fd452 | 2022-01-25 10:47:05 -0800 | [diff] [blame] | 650 | LOG_CORE_INIT(); |
| 651 | |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 652 | #if defined(CONFIG_MULTITHREADING) |
Andy Ross | fd340eb | 2024-04-19 15:03:09 -0700 | [diff] [blame] | 653 | z_dummy_thread_init(&_thread_dummy); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 654 | #endif /* CONFIG_MULTITHREADING */ |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 655 | /* do any necessary initialization of static devices */ |
| 656 | z_device_state_init(); |
| 657 | |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 658 | /* perform basic hardware initialization */ |
Gerard Marull-Paretas | 495245a | 2022-10-04 11:52:18 +0200 | [diff] [blame] | 659 | z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_1); |
| 660 | z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_2); |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 661 | |
Mazen NEIFER | e2bbad9 | 2017-02-07 10:01:12 +0100 | [diff] [blame] | 662 | #ifdef CONFIG_STACK_CANARIES |
Andrew Boie | 468efad | 2020-05-12 16:20:14 -0700 | [diff] [blame] | 663 | uintptr_t stack_guard; |
| 664 | |
Flavio Ceolin | f9c7a5e | 2023-10-09 15:22:18 -0700 | [diff] [blame] | 665 | z_early_rand_get((uint8_t *)&stack_guard, sizeof(stack_guard)); |
Flavio Ceolin | 394f66b | 2019-08-09 16:31:33 -0700 | [diff] [blame] | 666 | __stack_chk_guard = stack_guard; |
| 667 | __stack_chk_guard <<= 8; |
| 668 | #endif /* CONFIG_STACK_CANARIES */ |
Leandro Pereira | 389c364 | 2018-05-23 13:38:52 -0700 | [diff] [blame] | 669 | |
Daniel Leung | 1559712 | 2021-03-31 13:40:01 -0700 | [diff] [blame] | 670 | #ifdef CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT |
Daniel Leung | fd7a68d | 2020-10-14 12:17:12 -0700 | [diff] [blame] | 671 | timing_init(); |
| 672 | timing_start(); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 673 | #endif /* CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT */ |
Daniel Leung | fd7a68d | 2020-10-14 12:17:12 -0700 | [diff] [blame] | 674 | |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 675 | #ifdef CONFIG_MULTITHREADING |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 676 | switch_to_main_thread(prepare_multithreading()); |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 677 | #else |
Ioannis Glaropoulos | 60bd51a | 2020-08-03 11:11:19 +0200 | [diff] [blame] | 678 | #ifdef ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING |
| 679 | /* Custom ARCH-specific routine to switch to main() |
| 680 | * in the case of no multi-threading. |
| 681 | */ |
| 682 | ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING(bg_thread_main, |
| 683 | NULL, NULL, NULL); |
| 684 | #else |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 685 | bg_thread_main(NULL, NULL, NULL); |
| 686 | |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 687 | /* LCOV_EXCL_START |
| 688 | * We've already dumped coverage data at this point. |
| 689 | */ |
Andy Ross | 8daafd4 | 2018-08-30 09:45:12 -0700 | [diff] [blame] | 690 | irq_lock(); |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 691 | while (true) { |
Andy Ross | 3d14615 | 2018-06-13 10:51:42 -0700 | [diff] [blame] | 692 | } |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 693 | /* LCOV_EXCL_STOP */ |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 694 | #endif /* ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING */ |
Ioannis Glaropoulos | 60bd51a | 2020-08-03 11:11:19 +0200 | [diff] [blame] | 695 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * Compiler can't tell that the above routines won't return and issues |
| 699 | * a warning unless we explicitly tell it that control never gets this |
| 700 | * far. |
| 701 | */ |
| 702 | |
Andrew Boie | c5164f3 | 2019-06-11 13:33:32 -0700 | [diff] [blame] | 703 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 704 | } |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 705 | |
| 706 | #ifdef CONFIG_OBJ_CORE_SYSTEM |
| 707 | static int init_cpu_obj_core_list(void) |
| 708 | { |
| 709 | /* Initialize CPU object type */ |
| 710 | |
| 711 | z_obj_type_init(&obj_type_cpu, K_OBJ_TYPE_CPU_ID, |
| 712 | offsetof(struct _cpu, obj_core)); |
| 713 | |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 714 | #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM |
| 715 | k_obj_type_stats_init(&obj_type_cpu, &cpu_stats_desc); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 716 | #endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */ |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 717 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int init_kernel_obj_core_list(void) |
| 722 | { |
| 723 | /* Initialize kernel object type */ |
| 724 | |
| 725 | z_obj_type_init(&obj_type_kernel, K_OBJ_TYPE_KERNEL_ID, |
| 726 | offsetof(struct z_kernel, obj_core)); |
| 727 | |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 728 | #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM |
| 729 | k_obj_type_stats_init(&obj_type_kernel, &kernel_stats_desc); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 730 | #endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */ |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 731 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 732 | k_obj_core_init_and_link(K_OBJ_CORE(&_kernel), &obj_type_kernel); |
Peter Mitsis | e6f1090 | 2023-06-01 12:16:40 -0400 | [diff] [blame] | 733 | #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM |
| 734 | k_obj_core_stats_register(K_OBJ_CORE(&_kernel), _kernel.usage, |
| 735 | sizeof(_kernel.usage)); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 736 | #endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | SYS_INIT(init_cpu_obj_core_list, PRE_KERNEL_1, |
| 742 | CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
| 743 | |
| 744 | SYS_INIT(init_kernel_obj_core_list, PRE_KERNEL_1, |
| 745 | CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 746 | #endif /* CONFIG_OBJ_CORE_SYSTEM */ |