blob: d6c4fb3c70cca48e96b8348ffc4512894093d17c [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2010-2014 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
Anas Nashifcb888e62016-12-18 09:42:55 -05009 * @brief Kernel initialization module
Benjamin Walsh456c6da2016-09-02 18:55:39 -040010 *
Anas Nashifdc3d73b2016-12-19 20:25:56 -050011 * This module contains routines that are used to initialize the kernel.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040012 */
13
Jakub Michalski49fc1062024-07-11 15:10:43 +020014#include <ctype.h>
15#include <stdbool.h>
16#include <string.h>
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050017#include <offsets_short.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020018#include <zephyr/kernel.h>
19#include <zephyr/sys/printk.h>
20#include <zephyr/debug/stack.h>
Flavio Ceoline7bd10a2023-10-06 22:38:53 +000021#include <zephyr/random/random.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020022#include <zephyr/linker/sections.h>
23#include <zephyr/toolchain.h>
24#include <zephyr/kernel_structs.h>
25#include <zephyr/device.h>
26#include <zephyr/init.h>
27#include <zephyr/linker/linker-defs.h>
Anas Nashife260d032023-09-13 12:03:10 +000028#include <zephyr/platform/hooks.h>
Benjamin Walshb4b108d2016-10-13 10:31:48 -040029#include <ksched.h>
Anas Nashif3ca50f52024-02-23 10:53:01 -050030#include <kthread.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020031#include <zephyr/sys/dlist.h>
Andy Ross245b54e2018-02-08 09:10:46 -080032#include <kernel_internal.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020033#include <zephyr/drivers/entropy.h>
34#include <zephyr/logging/log_ctrl.h>
35#include <zephyr/tracing/tracing.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020036#include <zephyr/debug/gcov.h>
Andrew Boie468efad2020-05-12 16:20:14 -070037#include <kswap.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020038#include <zephyr/timing/timing.h>
39#include <zephyr/logging/log.h>
Jordan Yatesdb3d51b2022-03-12 21:10:42 +100040#include <zephyr/pm/device_runtime.h>
Ederson de Souzaeeebb4d2024-01-05 13:29:20 -080041#include <zephyr/internal/syscall_handler.h>
Krzysztof Chruscinski3ed80832020-11-26 19:32:34 +010042LOG_MODULE_REGISTER(os, CONFIG_KERNEL_LOG_LEVEL);
Anas Nashif57554052018-03-03 02:31:05 -060043
Krzysztof Chruscinski7dcff6e2021-04-16 15:16:00 +020044/* the only struct z_kernel instance */
Qipeng Zhafa973d12023-05-15 13:29:18 +080045__pinned_bss
Krzysztof Chruscinski7dcff6e2021-04-16 15:16:00 +020046struct z_kernel _kernel;
47
Andy Ross17a5beb2023-08-05 08:54:31 -070048#ifdef CONFIG_PM
49__pinned_bss atomic_t _cpus_active;
50#endif
Flavio Ceolin4f299302023-05-30 11:49:45 -070051
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052/* init/main and idle threads */
Daniel Leungebbfde92021-07-12 13:47:48 -070053K_THREAD_PINNED_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE);
Andrew Boiefe031612019-09-21 17:54:37 -070054struct k_thread z_main_thread;
Andrew Boie80a0d9d2020-03-12 15:37:29 -070055
56#ifdef CONFIG_MULTITHREADING
Daniel Leung660d1472021-03-25 16:05:15 -070057__pinned_bss
Kumar Galac778eb22022-10-12 10:55:36 -050058struct k_thread z_idle_threads[CONFIG_MP_MAX_NUM_CPUS];
Daniel Leung660d1472021-03-25 16:05:15 -070059
60static K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_idle_stacks,
Kumar Galac778eb22022-10-12 10:55:36 -050061 CONFIG_MP_MAX_NUM_CPUS,
Daniel Leung660d1472021-03-25 16:05:15 -070062 CONFIG_IDLE_STACK_SIZE);
Anas Nashif3ca50f52024-02-23 10:53:01 -050063
64static 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 Heinbcd1d192024-03-08 12:00:10 +010089#endif /* CONFIG_USERSPACE */
Anas Nashif3ca50f52024-02-23 10:53:01 -050090
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 Boie80a0d9d2020-03-12 15:37:29 -0700114#endif /* CONFIG_MULTITHREADING */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200116extern const struct init_entry __init_start[];
117extern const struct init_entry __init_EARLY_start[];
118extern const struct init_entry __init_PRE_KERNEL_1_start[];
119extern const struct init_entry __init_PRE_KERNEL_2_start[];
120extern const struct init_entry __init_POST_KERNEL_start[];
121extern const struct init_entry __init_APPLICATION_start[];
122extern const struct init_entry __init_end[];
123
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200124enum 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 Heinbcd1d192024-03-08 12:00:10 +0100132#endif /* CONFIG_SMP */
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200133};
134
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200135#ifdef CONFIG_SMP
136extern const struct init_entry __init_SMP_start[];
Simon Heinbcd1d192024-03-08 12:00:10 +0100137#endif /* CONFIG_SMP */
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200138
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400139/*
140 * storage space for the interrupt stack
141 *
Anas Nashifdc3d73b2016-12-19 20:25:56 -0500142 * 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 Walsh456c6da2016-09-02 18:55:39 -0400146 */
Daniel Leung660d1472021-03-25 16:05:15 -0700147K_KERNEL_PINNED_STACK_ARRAY_DEFINE(z_interrupt_stacks,
Kumar Galac778eb22022-10-12 10:55:36 -0500148 CONFIG_MP_MAX_NUM_CPUS,
Daniel Leung660d1472021-03-25 16:05:15 -0700149 CONFIG_ISR_STACK_SIZE);
Andy Ross780ba232018-01-29 09:20:18 -0800150
Peter Mitsis96cb05c2016-09-15 12:37:58 -0400151extern void idle(void *unused1, void *unused2, void *unused3);
152
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400153#ifdef CONFIG_OBJ_CORE_SYSTEM
154static struct k_obj_type obj_type_cpu;
155static struct k_obj_type obj_type_kernel;
Peter Mitsise6f10902023-06-01 12:16:40 -0400156
157#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
158static 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
168static 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 Heinbcd1d192024-03-08 12:00:10 +0100177#endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
178#endif /* CONFIG_OBJ_CORE_SYSTEM */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100179
Andrew Boiefe228a82019-06-11 12:49:32 -0700180/* 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 Walsh456c6da2016-09-02 18:55:39 -0400187/**
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500188 * @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
195void __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
208void __weak z_early_memcpy(void *dst, const void *src, size_t n)
209{
210 (void) memcpy(dst, src, n);
211}
212
213/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400214 * @brief Clear BSS
215 *
216 * This routine clears the BSS region, so all bytes are 0.
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400217 */
Daniel Leung660d1472021-03-25 16:05:15 -0700218__boot_func
Patrik Flykt4344e272019-03-08 14:19:05 -0700219void z_bss_zero(void)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400220{
Alexander Razinkovd2c101d2023-10-27 18:33:15 +0300221 if (IS_ENABLED(CONFIG_SKIP_BSS_CLEAR)) {
Andy Ross5722da72022-08-09 17:55:14 -0700222 return;
223 }
224
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
Yong Cong Sin52a20232024-09-20 12:47:40 +0800226#if DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_ccm))
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500227 z_early_memset(&__ccm_bss_start, 0,
228 (uintptr_t) &__ccm_bss_end
229 - (uintptr_t) &__ccm_bss_start);
Erwin Rol1dc41d12017-10-05 01:22:32 +0200230#endif
Yong Cong Sin52a20232024-09-20 12:47:40 +0800231#if DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_dtcm))
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500232 z_early_memset(&__dtcm_bss_start, 0,
233 (uintptr_t) &__dtcm_bss_end
234 - (uintptr_t) &__dtcm_bss_start);
Alexander Wachterb4c5f4b2019-07-03 14:19:29 +0200235#endif
Yong Cong Sin52a20232024-09-20 12:47:40 +0800236#if DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_ocm))
Nicolas Pitre36173982022-02-22 00:00:52 -0500237 z_early_memset(&__ocm_bss_start, 0,
238 (uintptr_t) &__ocm_bss_end
239 - (uintptr_t) &__ocm_bss_start);
Immo Birnbaumda288292022-01-21 12:38:30 +0100240#endif
Adithya Baglody91c5b842018-11-13 16:57:45 +0530241#ifdef CONFIG_CODE_DATA_RELOCATION
242 extern void bss_zeroing_relocation(void);
243
244 bss_zeroing_relocation();
245#endif /* CONFIG_CODE_DATA_RELOCATION */
Adithya Baglody71e90f92018-08-29 16:44:16 +0530246#ifdef CONFIG_COVERAGE_GCOV
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500247 z_early_memset(&__gcov_bss_start, 0,
248 ((uintptr_t) &__gcov_bss_end - (uintptr_t) &__gcov_bss_start));
Simon Heinbcd1d192024-03-08 12:00:10 +0100249#endif /* CONFIG_COVERAGE_GCOV */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400250}
251
Daniel Leungd8127282021-02-24 10:18:34 -0800252#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
262void z_bss_zero_boot(void)
263{
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500264 z_early_memset(&lnkr_boot_bss_start, 0,
265 (uintptr_t)&lnkr_boot_bss_end
266 - (uintptr_t)&lnkr_boot_bss_start);
Daniel Leungd8127282021-02-24 10:18:34 -0800267}
268#endif /* CONFIG_LINKER_USE_BOOT_SECTION */
269
Daniel Leung1310ad62021-02-23 13:33:38 -0800270#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 Heinbcd1d192024-03-08 12:00:10 +0100283#endif /* CONFIG_LINKER_USE_BOOT_SECTION */
Daniel Leung1310ad62021-02-23 13:33:38 -0800284void z_bss_zero_pinned(void)
285{
Nicolas Pitre678b76e2022-02-10 13:54:49 -0500286 z_early_memset(&lnkr_pinned_bss_start, 0,
287 (uintptr_t)&lnkr_pinned_bss_end
288 - (uintptr_t)&lnkr_pinned_bss_start);
Daniel Leung1310ad62021-02-23 13:33:38 -0800289}
290#endif /* CONFIG_LINKER_USE_PINNED_SECTION */
291
Flavio Ceolin82ace412024-11-25 13:58:40 -0800292#ifdef CONFIG_REQUIRES_STACK_CANARIES
Flavio Ceolind16c5b92023-08-01 15:07:57 -0700293#ifdef CONFIG_STACK_CANARIES_TLS
Daniel Flodin746c59c2024-09-18 14:07:42 +0200294extern Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
Flavio Ceolind16c5b92023-08-01 15:07:57 -0700295#else
Andrew Boie01100ea2019-02-21 15:02:22 -0800296extern volatile uintptr_t __stack_chk_guard;
Simon Heinbcd1d192024-03-08 12:00:10 +0100297#endif /* CONFIG_STACK_CANARIES_TLS */
Flavio Ceolin82ace412024-11-25 13:58:40 -0800298#endif /* CONFIG_REQUIRES_STACK_CANARIES */
Andrew Boie01100ea2019-02-21 15:02:22 -0800299
Andrew Boiefe228a82019-06-11 12:49:32 -0700300/* LCOV_EXCL_STOP */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400301
Daniel Leung660d1472021-03-25 16:05:15 -0700302__pinned_bss
Peter Bigot74ef3952019-12-23 11:48:43 -0600303bool z_sys_post_kernel;
Daniel Leung660d1472021-03-25 16:05:15 -0700304
Ederson de Souzaeeebb4d2024-01-05 13:29:20 -0800305static 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-Paretas83123932022-10-04 11:25:37 +0200336/**
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-Paretas495245a2022-10-04 11:52:18 +0200347static void z_sys_init_run_level(enum init_level level)
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200348{
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 Heinbcd1d192024-03-08 12:00:10 +0100357#endif /* CONFIG_SMP */
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200358 /* 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;
Yong Cong Sin15dc87d2024-06-14 20:40:49 +0800365 int result;
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200366
Yong Cong Sin15dc87d2024-06-14 20:40:49 +0800367 sys_trace_sys_init_enter(entry, level);
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200368 if (dev != NULL) {
Yong Cong Sin15dc87d2024-06-14 20:40:49 +0800369 result = do_device_init(entry);
Gerard Marull-Paretasa5fd0d12022-10-19 09:33:44 +0200370 } else {
Yong Cong Sin15dc87d2024-06-14 20:40:49 +0800371 result = entry->init_fn.sys();
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200372 }
Yong Cong Sin15dc87d2024-06-14 20:40:49 +0800373 sys_trace_sys_init_exit(entry, level, result);
Gerard Marull-Paretas83123932022-10-04 11:25:37 +0200374 }
375}
376
Ederson de Souzaeeebb4d2024-01-05 13:29:20 -0800377
378int z_impl_device_init(const struct device *dev)
379{
380 if (dev == NULL) {
381 return -ENOENT;
382 }
383
384 STRUCT_SECTION_FOREACH_ALTERNATE(_deferred_init, init_entry, entry) {
385 if (entry->dev == dev) {
386 return do_device_init(entry);
387 }
388 }
389
390 return -ENOENT;
391}
392
393#ifdef CONFIG_USERSPACE
394static inline int z_vrfy_device_init(const struct device *dev)
395{
396 K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
397
398 return z_impl_device_init(dev);
399}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800400#include <zephyr/syscalls/device_init_mrsh.c>
Ederson de Souzaeeebb4d2024-01-05 13:29:20 -0800401#endif
402
Anas Nashif4b593122020-08-27 09:08:40 -0400403extern void boot_banner(void);
Peter Bigot74ef3952019-12-23 11:48:43 -0600404
Jakub Michalski49fc1062024-07-11 15:10:43 +0200405#ifdef CONFIG_BOOTARGS
406extern const char *get_bootargs(void);
407static char **prepare_main_args(int *argc)
408{
409#ifdef CONFIG_DYNAMIC_BOOTARGS
410 const char *bootargs = get_bootargs();
411#else
412 const char bootargs[] = CONFIG_BOOTARGS_STRING;
413#endif
414
415 /* beginning of the buffer contains argument's strings, end of it contains argvs */
416 static char args_buf[CONFIG_BOOTARGS_ARGS_BUFFER_SIZE];
417 char *strings_end = (char *)args_buf;
418 char **argv_begin = (char **)WB_DN(
419 args_buf + CONFIG_BOOTARGS_ARGS_BUFFER_SIZE - sizeof(char *));
420 int i = 0;
421
422 *argc = 0;
423 *argv_begin = NULL;
424
425#ifdef CONFIG_DYNAMIC_BOOTARGS
426 if (!bootargs) {
427 return argv_begin;
428 }
429#endif
430
431 while (1) {
432 while (isspace(bootargs[i])) {
433 i++;
434 }
435
436 if (bootargs[i] == '\0') {
437 return argv_begin;
438 }
439
440 if (strings_end + sizeof(char *) >= (char *)argv_begin) {
441 LOG_WRN("not enough space in args buffer to accommodate all bootargs"
442 " - bootargs truncated");
443 return argv_begin;
444 }
445
446 argv_begin--;
447 memmove(argv_begin, argv_begin + 1, *argc * sizeof(char *));
448 argv_begin[*argc] = strings_end;
449
450 bool quoted = false;
451
452 if (bootargs[i] == '\"' || bootargs[i] == '\'') {
453 char delimiter = bootargs[i];
454
455 for (int j = i + 1; bootargs[j] != '\0'; j++) {
456 if (bootargs[j] == delimiter) {
457 quoted = true;
458 break;
459 }
460 }
461 }
462
463 if (quoted) {
464 char delimiter = bootargs[i];
465
466 i++; /* strip quotes */
467 while (bootargs[i] != delimiter
468 && strings_end < (char *)argv_begin) {
469 *strings_end++ = bootargs[i++];
470 }
471 i++; /* strip quotes */
472 } else {
473 while (!isspace(bootargs[i])
474 && bootargs[i] != '\0'
475 && strings_end < (char *)argv_begin) {
476 *strings_end++ = bootargs[i++];
477 }
478 }
479
480 if (strings_end < (char *)argv_begin) {
481 *strings_end++ = '\0';
482 } else {
483 LOG_WRN("not enough space in args buffer to accommodate all bootargs"
484 " - bootargs truncated");
485 argv_begin[*argc] = NULL;
486 return argv_begin;
487 }
488 (*argc)++;
489 }
490}
491#endif
Anas Nashif3ca50f52024-02-23 10:53:01 -0500492
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400493/**
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800494 * @brief Mainline for kernel's background thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400495 *
496 * This routine completes kernel initialization by invoking the remaining
497 * init functions, then invokes application's main() routine.
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400498 */
Daniel Leung660d1472021-03-25 16:05:15 -0700499__boot_func
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800500static void bg_thread_main(void *unused1, void *unused2, void *unused3)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400501{
502 ARG_UNUSED(unused1);
503 ARG_UNUSED(unused2);
504 ARG_UNUSED(unused3);
505
Andrew Boiee35f1792020-12-09 12:18:40 -0800506#ifdef CONFIG_MMU
507 /* Invoked here such that backing store or eviction algorithms may
508 * initialize kernel objects, and that all POST_KERNEL and later tasks
Daniel Leung552e2972024-06-06 09:26:10 -0700509 * may perform memory management tasks (except for
510 * k_mem_map_phys_bare() which is allowed at any time)
Andrew Boiee35f1792020-12-09 12:18:40 -0800511 */
512 z_mem_manage_init();
513#endif /* CONFIG_MMU */
Peter Bigot74ef3952019-12-23 11:48:43 -0600514 z_sys_post_kernel = true;
515
Anas Nashif7e225ef2024-07-08 17:08:30 -0400516#if CONFIG_IRQ_OFFLOAD
517 arch_irq_offload_init();
518#endif
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200519 z_sys_init_run_level(INIT_LEVEL_POST_KERNEL);
Anas Nashife260d032023-09-13 12:03:10 +0000520#if CONFIG_SOC_LATE_INIT_HOOK
521 soc_late_init_hook();
522#endif
523#if CONFIG_BOARD_LATE_INIT_HOOK
524 board_late_init_hook();
525#endif
526
Hess Nathan527e7122024-04-29 14:08:04 +0200527#if defined(CONFIG_STACK_POINTER_RANDOM) && (CONFIG_STACK_POINTER_RANDOM != 0)
Andrew Boie538754c2018-05-23 15:25:23 -0700528 z_stack_adjust_initialized = 1;
Simon Heinbcd1d192024-03-08 12:00:10 +0100529#endif /* CONFIG_STACK_POINTER_RANDOM */
Anas Nashif4b593122020-08-27 09:08:40 -0400530 boot_banner();
Andrew Boie0b474ee2016-11-08 11:06:55 -0800531
Alberto Escolar Piedras6e977ae2024-06-21 13:18:55 +0200532 void z_init_static(void);
533 z_init_static();
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400534
Peter Bigotc308f802020-05-08 09:28:44 -0500535 /* Final init level before app starts */
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200536 z_sys_init_run_level(INIT_LEVEL_APPLICATION);
Peter Bigotc308f802020-05-08 09:28:44 -0500537
Patrik Flykt4344e272019-03-08 14:19:05 -0700538 z_init_static_threads();
Anas Nashif83088a22017-08-24 04:27:51 -0400539
Anas Nashif39f632e2020-12-07 13:15:42 -0500540#ifdef CONFIG_KERNEL_COHERENCE
Daniel Leung079bc642021-02-02 16:12:15 -0800541 __ASSERT_NO_MSG(arch_mem_coherent(&_kernel));
Simon Heinbcd1d192024-03-08 12:00:10 +0100542#endif /* CONFIG_KERNEL_COHERENCE */
Andy Rossf6d32ab2020-05-13 15:34:04 +0000543
Andy Rosseb258702018-04-12 12:10:10 -0700544#ifdef CONFIG_SMP
Andy Ross2b210cb2022-01-17 11:56:54 -0800545 if (!IS_ENABLED(CONFIG_SMP_BOOT_DELAY)) {
546 z_smp_init();
547 }
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200548 z_sys_init_run_level(INIT_LEVEL_SMP);
Simon Heinbcd1d192024-03-08 12:00:10 +0100549#endif /* CONFIG_SMP */
Inaky Perez-Gonzalezc51f73f2017-06-20 17:01:09 -0700550
Daniel Leunge88afd22021-07-15 13:15:29 -0700551#ifdef CONFIG_MMU
552 z_mem_manage_boot_finish();
553#endif /* CONFIG_MMU */
554
Jakub Michalski49fc1062024-07-11 15:10:43 +0200555#ifdef CONFIG_BOOTARGS
556 extern int main(int, char **);
557
558 int argc = 0;
559 char **argv = prepare_main_args(&argc);
560 (void)main(argc, argv);
561#else
Stephanos Ioannidisfa5fd412022-11-05 00:22:25 +0900562 extern int main(void);
Andrew Boief1c373c2016-10-28 12:45:05 -0700563
Stephanos Ioannidisfa5fd412022-11-05 00:22:25 +0900564 (void)main();
Jakub Michalski49fc1062024-07-11 15:10:43 +0200565#endif /* CONFIG_BOOTARGS */
Allan Stephens073442e2016-11-09 07:46:56 -0600566
Anas Nashif87910122024-02-22 22:24:36 -0500567 /* Mark non-essential since main() has no more work to do */
568 z_thread_essential_clear(&z_main_thread);
Andrew Boie8e053332019-06-11 12:58:16 -0700569
Anas Nashif471ffbe2020-01-30 08:44:10 -0500570#ifdef CONFIG_COVERAGE_DUMP
Adithya Baglody71e90f92018-08-29 16:44:16 +0530571 /* Dump coverage data once the main() has exited. */
572 gcov_coverage_dump();
Simon Heinbcd1d192024-03-08 12:00:10 +0100573#endif /* CONFIG_COVERAGE_DUMP */
Andrew Boie8e053332019-06-11 12:58:16 -0700574} /* LCOV_EXCL_LINE ... because we just dumped final coverage data */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400575
Andy Ross780ba232018-01-29 09:20:18 -0800576#if defined(CONFIG_MULTITHREADING)
Daniel Leung660d1472021-03-25 16:05:15 -0700577__boot_func
Andrew Boiec24673e2020-03-16 09:44:28 -0700578static void init_idle_thread(int i)
Andy Ross780ba232018-01-29 09:20:18 -0800579{
Andrew Boiec24673e2020-03-16 09:44:28 -0700580 struct k_thread *thread = &z_idle_threads[i];
581 k_thread_stack_t *stack = z_idle_stacks[i];
Dong Wang749566a2024-05-24 09:52:13 +0800582 size_t stack_size = K_KERNEL_STACK_SIZEOF(z_idle_stacks[i]);
Andrew Boiec24673e2020-03-16 09:44:28 -0700583
584#ifdef CONFIG_THREAD_NAME
Andrew Boiec24673e2020-03-16 09:44:28 -0700585
Kumar Gala4f458ba2022-10-18 11:11:46 -0500586#if CONFIG_MP_MAX_NUM_CPUS > 1
Trond Einar Snekvik6224ecb2022-03-25 12:46:32 +0100587 char tname[8];
Andrew Boiec24673e2020-03-16 09:44:28 -0700588 snprintk(tname, 8, "idle %02d", i);
589#else
Trond Einar Snekvik6224ecb2022-03-25 12:46:32 +0100590 char *tname = "idle";
Simon Heinbcd1d192024-03-08 12:00:10 +0100591#endif /* CONFIG_MP_MAX_NUM_CPUS */
Trond Einar Snekvik6224ecb2022-03-25 12:46:32 +0100592
593#else
Andrew Boiec24673e2020-03-16 09:44:28 -0700594 char *tname = NULL;
595#endif /* CONFIG_THREAD_NAME */
596
Anas Nashif9e3e7f62019-12-19 08:19:45 -0500597 z_setup_new_thread(thread, stack,
Dong Wang749566a2024-05-24 09:52:13 +0800598 stack_size, idle, &_kernel.cpus[i],
Andy Ross851d14a2021-05-13 15:46:43 -0700599 NULL, NULL, K_IDLE_PRIO, K_ESSENTIAL,
Andrew Boief5a7e1a2020-09-02 09:20:38 -0700600 tname);
Peter Mitsis35435922024-11-18 09:46:24 -0800601 z_mark_thread_as_not_sleeping(thread);
Andy Ross6c283ca2019-08-16 22:09:30 -0700602
603#ifdef CONFIG_SMP
Anas Nashif9e3e7f62019-12-19 08:19:45 -0500604 thread->base.is_idle = 1U;
Simon Heinbcd1d192024-03-08 12:00:10 +0100605#endif /* CONFIG_SMP */
Andy Ross780ba232018-01-29 09:20:18 -0800606}
Andy Ross780ba232018-01-29 09:20:18 -0800607
Andy Ross2b210cb2022-01-17 11:56:54 -0800608void z_init_cpu(int id)
Andy Rossc6d077e2021-08-18 06:28:11 -0700609{
Andy Ross2b210cb2022-01-17 11:56:54 -0800610 init_idle_thread(id);
611 _kernel.cpus[id].idle_thread = &z_idle_threads[id];
612 _kernel.cpus[id].id = id;
613 _kernel.cpus[id].irq_stack =
Daniel Leungb69d2482024-03-22 12:56:12 -0700614 (K_KERNEL_STACK_BUFFER(z_interrupt_stacks[id]) +
Andy Ross2b210cb2022-01-17 11:56:54 -0800615 K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[id]));
616#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
Peter Mitsis9bedfd82023-05-23 18:36:04 -0400617 _kernel.cpus[id].usage = &_kernel.usage[id];
618 _kernel.cpus[id].usage->track_usage =
Andy Ross2b210cb2022-01-17 11:56:54 -0800619 CONFIG_SCHED_THREAD_USAGE_AUTO_ENABLE;
620#endif
Flavio Ceolin4f299302023-05-30 11:49:45 -0700621
Andy Ross17a5beb2023-08-05 08:54:31 -0700622#ifdef CONFIG_PM
Flavio Ceolin4f299302023-05-30 11:49:45 -0700623 /*
624 * Increment number of CPUs active. The pm subsystem
625 * will keep track of this from here.
626 */
627 atomic_inc(&_cpus_active);
Andy Ross17a5beb2023-08-05 08:54:31 -0700628#endif
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400629
630#ifdef CONFIG_OBJ_CORE_SYSTEM
631 k_obj_core_init_and_link(K_OBJ_CORE(&_kernel.cpus[id]), &obj_type_cpu);
Peter Mitsise6f10902023-06-01 12:16:40 -0400632#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
633 k_obj_core_stats_register(K_OBJ_CORE(&_kernel.cpus[id]),
634 _kernel.cpus[id].usage,
635 sizeof(struct k_cycle_stats));
636#endif
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400637#endif
Andy Rossc6d077e2021-08-18 06:28:11 -0700638}
639
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400640/**
641 *
Anas Nashifdc3d73b2016-12-19 20:25:56 -0500642 * @brief Initializes kernel data structures
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400643 *
Anas Nashifdc3d73b2016-12-19 20:25:56 -0500644 * This routine initializes various kernel data structures, including
645 * the init and idle threads and any architecture-specific initialization.
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400646 *
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500647 * Note that all fields of "_kernel" are set to zero on entry, which may
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400648 * be all the initialization many of them require.
649 *
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700650 * @return initial stack pointer for the main thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400651 */
Daniel Leung660d1472021-03-25 16:05:15 -0700652__boot_func
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700653static char *prepare_multithreading(void)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400654{
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700655 char *stack_ptr;
656
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500657 /* _kernel.ready_q is all zeroes */
Patrik Flykt4344e272019-03-08 14:19:05 -0700658 z_sched_init();
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400659
Andy Ross2724fd12018-01-29 14:55:20 -0800660#ifndef CONFIG_SMP
Benjamin Walsh88b36912016-12-02 10:37:27 -0500661 /*
662 * prime the cache with the main thread since:
663 *
664 * - the cache can never be NULL
665 * - the main thread will be the one to run first
666 * - no other thread is initialized yet and thus their priority fields
667 * contain garbage, which would prevent the cache loading algorithm
668 * to work as intended
669 */
Andrew Boiefe031612019-09-21 17:54:37 -0700670 _kernel.ready_q.cache = &z_main_thread;
Simon Heinbcd1d192024-03-08 12:00:10 +0100671#endif /* CONFIG_SMP */
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700672 stack_ptr = z_setup_new_thread(&z_main_thread, z_main_stack,
Dong Wang749566a2024-05-24 09:52:13 +0800673 K_THREAD_STACK_SIZEOF(z_main_stack),
674 bg_thread_main,
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700675 NULL, NULL, NULL,
676 CONFIG_MAIN_THREAD_PRIORITY,
Ioannis Glaropoulos40aab322021-01-28 21:46:28 +0100677 K_ESSENTIAL, "main");
Peter Mitsis35435922024-11-18 09:46:24 -0800678 z_mark_thread_as_not_sleeping(&z_main_thread);
Andrew Boiefe031612019-09-21 17:54:37 -0700679 z_ready_thread(&z_main_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400680
Andy Ross2b210cb2022-01-17 11:56:54 -0800681 z_init_cpu(0);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400682
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700683 return stack_ptr;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400684}
685
Daniel Leung660d1472021-03-25 16:05:15 -0700686__boot_func
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700687static FUNC_NORETURN void switch_to_main_thread(char *stack_ptr)
Benjamin Walshc742d7e2016-10-05 17:50:54 -0400688{
Benjamin Walsh296a2342016-11-20 11:04:31 -0500689#ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700690 arch_switch_to_main_thread(&z_main_thread, stack_ptr, bg_thread_main);
Benjamin Walsh296a2342016-11-20 11:04:31 -0500691#else
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700692 ARG_UNUSED(stack_ptr);
Benjamin Walshc742d7e2016-10-05 17:50:54 -0400693 /*
694 * Context switch to main task (entry function is _main()): the
695 * current fake thread is not on a wait queue or ready queue, so it
696 * will never be rescheduled in.
697 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700698 z_swap_unlocked();
Simon Heinbcd1d192024-03-08 12:00:10 +0100699#endif /* CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN */
Andrew Boiec5164f32019-06-11 13:33:32 -0700700 CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
Benjamin Walshc742d7e2016-10-05 17:50:54 -0400701}
Anas Nashifc0ea5052019-01-30 09:58:41 -0500702#endif /* CONFIG_MULTITHREADING */
Benjamin Walshc742d7e2016-10-05 17:50:54 -0400703
Daniel Leung660d1472021-03-25 16:05:15 -0700704__boot_func
Flavio Ceolin974e3362023-10-09 15:25:42 -0700705void __weak z_early_rand_get(uint8_t *buf, size_t length)
Leandro Pereira389c3642018-05-23 13:38:52 -0700706{
Flavio Ceolin991ff6f2023-10-10 10:31:35 -0700707 static uint64_t state = (uint64_t)CONFIG_TIMER_RANDOM_INITIAL_STATE;
Leandro Pereira389c3642018-05-23 13:38:52 -0700708 int rc;
709
Flavio Ceolin6c0fad22023-10-09 20:52:07 -0700710#ifdef CONFIG_ENTROPY_HAS_DRIVER
711 const struct device *const entropy = DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_entropy));
712
713 if ((entropy != NULL) && device_is_ready(entropy)) {
714 /* Try to see if driver provides an ISR-specific API */
715 rc = entropy_get_entropy_isr(entropy, buf, length, ENTROPY_BUSYWAIT);
716 if (rc > 0) {
717 length -= rc;
718 buf += rc;
719 }
Leandro Pereira389c3642018-05-23 13:38:52 -0700720 }
Simon Heinbcd1d192024-03-08 12:00:10 +0100721#endif /* CONFIG_ENTROPY_HAS_DRIVER */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722
Flavio Ceolin6c0fad22023-10-09 20:52:07 -0700723 while (length > 0) {
724 uint32_t val;
725
726 state = state + k_cycle_get_32();
727 state = state * 2862933555777941757ULL + 3037000493ULL;
728 val = (uint32_t)(state >> 32);
729 rc = MIN(length, sizeof(val));
730 z_early_memcpy((void *)buf, &val, rc);
731
732 length -= rc;
733 buf += rc;
734 }
Flavio Ceolin394f66b2019-08-09 16:31:33 -0700735}
736
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400737/**
738 *
Anas Nashifdc3d73b2016-12-19 20:25:56 -0500739 * @brief Initialize kernel
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400740 *
741 * This routine is invoked when the system is ready to run C code. The
742 * processor must be running in 32-bit mode, and the BSS must have been
743 * cleared/zeroed.
744 *
745 * @return Does not return
746 */
Daniel Leung660d1472021-03-25 16:05:15 -0700747__boot_func
Daniel Leung256db602022-12-15 15:54:56 -0800748FUNC_NO_STACK_PROTECTOR
Patrik Flykt4344e272019-03-08 14:19:05 -0700749FUNC_NORETURN void z_cstart(void)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400750{
Adithya Baglody71e90f92018-08-29 16:44:16 +0530751 /* gcov hook needed to get the coverage report.*/
752 gcov_static_init();
753
Gerard Marull-Paretase42f58e2022-10-11 17:17:18 +0200754 /* initialize early init calls */
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200755 z_sys_init_run_level(INIT_LEVEL_EARLY);
Gerard Marull-Paretase42f58e2022-10-11 17:17:18 +0200756
Andrew Boie982d5c82018-05-23 13:30:34 -0700757 /* perform any architecture-specific initialization */
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800758 arch_kernel_init();
Andrew Boie982d5c82018-05-23 13:30:34 -0700759
Ederson de Souza75fd4522022-01-25 10:47:05 -0800760 LOG_CORE_INIT();
761
Andrew Boie468efad2020-05-12 16:20:14 -0700762#if defined(CONFIG_MULTITHREADING)
Andy Rossfd340eb2024-04-19 15:03:09 -0700763 z_dummy_thread_init(&_thread_dummy);
Simon Heinbcd1d192024-03-08 12:00:10 +0100764#endif /* CONFIG_MULTITHREADING */
Peter Bigot1cadd8b2021-02-02 10:07:18 -0600765 /* do any necessary initialization of static devices */
766 z_device_state_init();
767
Anas Nashife260d032023-09-13 12:03:10 +0000768#if CONFIG_SOC_EARLY_INIT_HOOK
769 soc_early_init_hook();
770#endif
771#if CONFIG_BOARD_EARLY_INIT_HOOK
772 board_early_init_hook();
773#endif
Andrew Boie0b474ee2016-11-08 11:06:55 -0800774 /* perform basic hardware initialization */
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200775 z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_1);
Anas Nashifc20e7982024-06-06 18:34:22 -0400776#if defined(CONFIG_SMP)
777 arch_smp_init();
778#endif
Gerard Marull-Paretas495245a2022-10-04 11:52:18 +0200779 z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_2);
Andrew Boie0b474ee2016-11-08 11:06:55 -0800780
Flavio Ceolin82ace412024-11-25 13:58:40 -0800781#ifdef CONFIG_REQUIRES_STACK_CANARIES
Andrew Boie468efad2020-05-12 16:20:14 -0700782 uintptr_t stack_guard;
783
Flavio Ceolinf9c7a5e2023-10-09 15:22:18 -0700784 z_early_rand_get((uint8_t *)&stack_guard, sizeof(stack_guard));
Flavio Ceolin394f66b2019-08-09 16:31:33 -0700785 __stack_chk_guard = stack_guard;
786 __stack_chk_guard <<= 8;
Flavio Ceolin82ace412024-11-25 13:58:40 -0800787#endif /* CONFIG_REQUIRES_STACK_CANARIES */
Leandro Pereira389c3642018-05-23 13:38:52 -0700788
Daniel Leung15597122021-03-31 13:40:01 -0700789#ifdef CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT
Daniel Leungfd7a68d2020-10-14 12:17:12 -0700790 timing_init();
791 timing_start();
Simon Heinbcd1d192024-03-08 12:00:10 +0100792#endif /* CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT */
Daniel Leungfd7a68d2020-10-14 12:17:12 -0700793
Andy Ross3d146152018-06-13 10:51:42 -0700794#ifdef CONFIG_MULTITHREADING
Andrew Boiee4cc84a2020-04-24 11:29:47 -0700795 switch_to_main_thread(prepare_multithreading());
Andy Ross3d146152018-06-13 10:51:42 -0700796#else
Ioannis Glaropoulos60bd51a2020-08-03 11:11:19 +0200797#ifdef ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING
798 /* Custom ARCH-specific routine to switch to main()
799 * in the case of no multi-threading.
800 */
801 ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING(bg_thread_main,
802 NULL, NULL, NULL);
803#else
Andy Ross3d146152018-06-13 10:51:42 -0700804 bg_thread_main(NULL, NULL, NULL);
805
Andrew Boiec5164f32019-06-11 13:33:32 -0700806 /* LCOV_EXCL_START
807 * We've already dumped coverage data at this point.
808 */
Andy Ross8daafd42018-08-30 09:45:12 -0700809 irq_lock();
Flavio Ceolinb3d92022018-09-17 15:56:06 -0700810 while (true) {
Andy Ross3d146152018-06-13 10:51:42 -0700811 }
Andrew Boiec5164f32019-06-11 13:33:32 -0700812 /* LCOV_EXCL_STOP */
Simon Heinbcd1d192024-03-08 12:00:10 +0100813#endif /* ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING */
Ioannis Glaropoulos60bd51a2020-08-03 11:11:19 +0200814#endif /* CONFIG_MULTITHREADING */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400815
816 /*
817 * Compiler can't tell that the above routines won't return and issues
818 * a warning unless we explicitly tell it that control never gets this
819 * far.
820 */
821
Andrew Boiec5164f32019-06-11 13:33:32 -0700822 CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400823}
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400824
825#ifdef CONFIG_OBJ_CORE_SYSTEM
826static int init_cpu_obj_core_list(void)
827{
828 /* Initialize CPU object type */
829
830 z_obj_type_init(&obj_type_cpu, K_OBJ_TYPE_CPU_ID,
831 offsetof(struct _cpu, obj_core));
832
Peter Mitsise6f10902023-06-01 12:16:40 -0400833#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
834 k_obj_type_stats_init(&obj_type_cpu, &cpu_stats_desc);
Simon Heinbcd1d192024-03-08 12:00:10 +0100835#endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
Peter Mitsise6f10902023-06-01 12:16:40 -0400836
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400837 return 0;
838}
839
840static int init_kernel_obj_core_list(void)
841{
842 /* Initialize kernel object type */
843
844 z_obj_type_init(&obj_type_kernel, K_OBJ_TYPE_KERNEL_ID,
845 offsetof(struct z_kernel, obj_core));
846
Peter Mitsise6f10902023-06-01 12:16:40 -0400847#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
848 k_obj_type_stats_init(&obj_type_kernel, &kernel_stats_desc);
Simon Heinbcd1d192024-03-08 12:00:10 +0100849#endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
Peter Mitsise6f10902023-06-01 12:16:40 -0400850
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400851 k_obj_core_init_and_link(K_OBJ_CORE(&_kernel), &obj_type_kernel);
Peter Mitsise6f10902023-06-01 12:16:40 -0400852#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
853 k_obj_core_stats_register(K_OBJ_CORE(&_kernel), _kernel.usage,
854 sizeof(_kernel.usage));
Simon Heinbcd1d192024-03-08 12:00:10 +0100855#endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
Peter Mitsis6df8efe2023-05-11 14:06:46 -0400856
857 return 0;
858}
859
860SYS_INIT(init_cpu_obj_core_list, PRE_KERNEL_1,
861 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
862
863SYS_INIT(init_kernel_obj_core_list, PRE_KERNEL_1,
864 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
Simon Heinbcd1d192024-03-08 12:00:10 +0100865#endif /* CONFIG_OBJ_CORE_SYSTEM */