Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2010-2014 Wind River Systems, Inc.
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * @file
9 : : * @brief Kernel initialization module
10 : : *
11 : : * This module contains routines that are used to initialize the kernel.
12 : : */
13 : :
14 : : #include <ctype.h>
15 : : #include <stdbool.h>
16 : : #include <string.h>
17 : : #include <offsets_short.h>
18 : : #include <zephyr/kernel.h>
19 : : #include <zephyr/sys/minmax.h>
20 : : #include <zephyr/sys/printk.h>
21 : : #include <zephyr/debug/stack.h>
22 : : #include <zephyr/random/random.h>
23 : : #include <zephyr/linker/sections.h>
24 : : #include <zephyr/toolchain.h>
25 : : #include <zephyr/device.h>
26 : : #include <zephyr/init.h>
27 : : #include <zephyr/linker/linker-defs.h>
28 : : #include <zephyr/platform/hooks.h>
29 : : #include <ksched.h>
30 : : #include <kthread.h>
31 : : #include <ipi.h>
32 : : #include <ksmp.h>
33 : : #include <zephyr/sys/dlist.h>
34 : : #include <kernel_internal.h>
35 : : #include <zephyr/drivers/entropy.h>
36 : : #include <zephyr/logging/log_ctrl.h>
37 : : #include <zephyr/tracing/tracing.h>
38 : : #include <zephyr/debug/gcov.h>
39 : : #include <kswap.h>
40 : : #include <zephyr/timing/timing.h>
41 : : #include <zephyr/logging/log.h>
42 : : #include <zephyr/internal/syscall_handler.h>
43 : : #include <zephyr/arch/common/init.h>
44 : : #include <scheduler.h>
45 : :
46 : : LOG_MODULE_REGISTER(os, CONFIG_KERNEL_LOG_LEVEL);
47 : :
48 : : /* the only struct z_kernel instance */
49 : : struct z_kernel _kernel;
50 : :
51 : : #ifdef CONFIG_PM
52 : : atomic_t _cpus_active;
53 : : #endif
54 : :
55 : : /* init/main and idle threads */
56 : : K_THREAD_STACK_DEFINE(z_main_stack, CONFIG_MAIN_STACK_SIZE);
57 : :
58 : : #ifdef CONFIG_MULTITHREADING
59 : : struct k_thread z_main_thread;
60 : : struct k_thread z_idle_threads[CONFIG_MP_MAX_NUM_CPUS];
61 : :
62 : : static K_KERNEL_STACK_ARRAY_DEFINE(z_idle_stacks,
63 : : CONFIG_MP_MAX_NUM_CPUS,
64 : : CONFIG_IDLE_STACK_SIZE);
65 : :
66 : 1 : static void z_init_static_threads(void)
67 : : {
68 [ - + - + ]: 1 : STRUCT_SECTION_FOREACH(_static_thread_data, thread_data) {
69 : 0 : z_setup_new_thread(
70 : : thread_data->init_thread,
71 : : thread_data->init_stack,
72 : : thread_data->init_stack_size,
73 : : thread_data->init_entry,
74 : : thread_data->init_p1,
75 : : thread_data->init_p2,
76 : : thread_data->init_p3,
77 : : thread_data->init_prio,
78 : : thread_data->init_options,
79 : : thread_data->init_name);
80 : :
81 : 0 : thread_data->init_thread->init_data = thread_data;
82 : : }
83 : :
84 : : #ifdef CONFIG_USERSPACE
85 : : STRUCT_SECTION_FOREACH(k_object_assignment, pos) {
86 : : for (int i = 0; pos->objects[i] != NULL; i++) {
87 : : k_object_access_grant(pos->objects[i],
88 : : pos->thread);
89 : : }
90 : : }
91 : : #endif /* CONFIG_USERSPACE */
92 : :
93 : : /*
94 : : * Static threads may be started immediately or
95 : : * after a previously specified delay. Even though the
96 : : * scheduler is locked, ticks can still be delivered and
97 : : * processed. Take a sched lock to prevent them from running
98 : : * until they are all started.
99 : : */
100 : 1 : k_sched_lock();
101 [ - + - + ]: 1 : STRUCT_SECTION_FOREACH(_static_thread_data, thread_data) {
102 : 0 : k_timeout_t init_delay = Z_THREAD_INIT_DELAY(thread_data);
103 : :
104 : 0 : thread_schedule_new(thread_data->init_thread, init_delay);
105 : : }
106 : 1 : k_sched_unlock();
107 : 1 : }
108 : : #else
109 : : #define z_init_static_threads() do { } while (false)
110 : : #endif /* CONFIG_MULTITHREADING */
111 : :
112 : : extern const struct init_entry __init_start[];
113 : : extern const struct init_entry __init_EARLY_start[];
114 : : extern const struct init_entry __init_PRE_KERNEL_1_start[];
115 : : extern const struct init_entry __init_PRE_KERNEL_2_start[];
116 : : extern const struct init_entry __init_POST_KERNEL_start[];
117 : : extern const struct init_entry __init_APPLICATION_start[];
118 : : extern const struct init_entry __init_end[];
119 : :
120 : : enum init_level {
121 : : INIT_LEVEL_EARLY = 0,
122 : : INIT_LEVEL_PRE_KERNEL_1,
123 : : INIT_LEVEL_PRE_KERNEL_2,
124 : : INIT_LEVEL_POST_KERNEL,
125 : : INIT_LEVEL_APPLICATION,
126 : : #ifdef CONFIG_SMP
127 : : INIT_LEVEL_SMP,
128 : : #endif /* CONFIG_SMP */
129 : : };
130 : :
131 : : #ifdef CONFIG_SMP
132 : : extern const struct init_entry __init_SMP_start[];
133 : : #endif /* CONFIG_SMP */
134 : :
135 : : /*
136 : : * storage space for the interrupt stack
137 : : *
138 : : * Note: This area is used as the system stack during kernel initialization,
139 : : * since the kernel hasn't yet set up its own stack areas. The dual purposing
140 : : * of this area is safe since interrupts are disabled until the kernel context
141 : : * switches to the init thread.
142 : : */
143 : : K_KERNEL_STACK_ARRAY_DEFINE(z_interrupt_stacks,
144 : : CONFIG_MP_MAX_NUM_CPUS,
145 : : CONFIG_ISR_STACK_SIZE);
146 : :
147 : : extern void idle(void *unused1, void *unused2, void *unused3);
148 : :
149 : : #ifdef CONFIG_OBJ_CORE_SYSTEM
150 : : #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
151 : : static struct k_obj_core_stats_desc cpu_stats_desc = {
152 : : .raw_size = sizeof(struct k_cycle_stats),
153 : : .query_size = sizeof(struct k_thread_runtime_stats),
154 : : .raw = z_cpu_stats_raw,
155 : : .query = z_cpu_stats_query,
156 : : .reset = NULL,
157 : : .disable = NULL,
158 : : .enable = NULL,
159 : : };
160 : :
161 : : /* CPU object cores have no static instances to walk; each CPU links its own
162 : : * object core in z_init_cpu(). Register the type only.
163 : : */
164 : : K_OBJ_TYPE_DEFINE_TYPE_ONLY(obj_type_cpu, _cpu, K_OBJ_TYPE_CPU_ID,
165 : : &cpu_stats_desc);
166 : : #else
167 : : K_OBJ_TYPE_DEFINE_TYPE_ONLY(obj_type_cpu, _cpu, K_OBJ_TYPE_CPU_ID, NULL);
168 : : #endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
169 : : #endif /* CONFIG_OBJ_CORE_SYSTEM */
170 : :
171 : : #ifdef CONFIG_REQUIRES_STACK_CANARIES
172 : : #ifdef CONFIG_STACK_CANARIES_TLS
173 : : extern Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
174 : : #else
175 : : extern volatile uintptr_t __stack_chk_guard;
176 : : #endif /* CONFIG_STACK_CANARIES_TLS */
177 : : #endif /* CONFIG_REQUIRES_STACK_CANARIES */
178 : :
179 : : bool z_sys_post_kernel;
180 : :
181 : : /* defined in device.c */
182 : : extern int do_device_init(const struct device *dev);
183 : :
184 : : /**
185 : : * @brief Initialize state for all static devices.
186 : : *
187 : : * The state object is always zero-initialized, but this may not be
188 : : * sufficient.
189 : : */
190 : 1 : static void z_device_state_init(void)
191 : : {
192 [ - + - + ]: 1 : STRUCT_SECTION_FOREACH(device, dev) {
193 : 0 : k_object_init(dev);
194 : : }
195 : 1 : }
196 : :
197 : : /**
198 : : * @brief Execute all the init entry initialization functions at a given level
199 : : *
200 : : * @details Invokes the initialization routine for each init entry object
201 : : * created by the INIT_ENTRY_DEFINE() macro using the specified level.
202 : : * The linker script places the init entry objects in memory in the order
203 : : * they need to be invoked, with symbols indicating where one level leaves
204 : : * off and the next one begins.
205 : : *
206 : : * @param level init level to run.
207 : : */
208 : 5 : static void z_sys_init_run_level(enum init_level level)
209 : : {
210 : 5 : static const struct init_entry *levels[] = {
211 : : __init_EARLY_start,
212 : : __init_PRE_KERNEL_1_start,
213 : : __init_PRE_KERNEL_2_start,
214 : : __init_POST_KERNEL_start,
215 : : __init_APPLICATION_start,
216 : : #ifdef CONFIG_SMP
217 : : __init_SMP_start,
218 : : #endif /* CONFIG_SMP */
219 : : /* End marker */
220 : : __init_end,
221 : : };
222 : 5 : const struct init_entry *entry;
223 : :
224 [ + + ]: 9 : for (entry = levels[level]; entry < levels[level+1]; entry++) {
225 : 4 : const struct device *dev = entry->dev;
226 : 4 : int result = 0;
227 : :
228 : 4 : sys_trace_sys_init_enter(entry, level);
229 [ - + ]: 4 : if (dev != NULL) {
230 [ # # ]: 0 : if ((dev->flags & DEVICE_FLAG_INIT_DEFERRED) == 0U) {
231 : 0 : result = do_device_init(dev);
232 : : }
233 : : } else {
234 : 4 : result = entry->init_fn();
235 : : }
236 : 4 : sys_trace_sys_init_exit(entry, level, result);
237 : : }
238 : 5 : }
239 : :
240 : : #ifdef CONFIG_STATIC_INIT_GNU
241 : :
242 : : extern void (*__zephyr_init_array_start[])();
243 : : extern void (*__zephyr_init_array_end[])();
244 : :
245 : 1 : static void z_static_init_gnu(void)
246 : : {
247 : 1 : void (**fn)();
248 : :
249 [ + + ]: 200 : for (fn = __zephyr_init_array_start; fn != __zephyr_init_array_end; fn++) {
250 : : /* MWDT toolchain sticks a NULL at the end of the array */
251 [ + - ]: 199 : if (*fn == NULL) {
252 : : break;
253 : : }
254 : 199 : (**fn)();
255 : : }
256 : 1 : }
257 : :
258 : : #endif
259 : :
260 : : /**
261 : : * @brief Mainline for kernel's background thread
262 : : *
263 : : * This routine completes kernel initialization by invoking the remaining
264 : : * init functions, then invokes application's main() routine.
265 : : */
266 : : __boot_func
267 : 1 : static void bg_thread_main(void *unused1, void *unused2, void *unused3)
268 : : {
269 : 1 : ARG_UNUSED(unused1);
270 : 1 : ARG_UNUSED(unused2);
271 : 1 : ARG_UNUSED(unused3);
272 : :
273 : : #ifdef CONFIG_MMU
274 : : /* Invoked here such that backing store or eviction algorithms may
275 : : * initialize kernel objects, and that all POST_KERNEL and later tasks
276 : : * may perform memory management tasks (except for
277 : : * k_mem_map_phys_bare() which is allowed at any time)
278 : : */
279 : : z_mem_manage_init();
280 : : #endif /* CONFIG_MMU */
281 : 1 : z_sys_post_kernel = true;
282 : :
283 : : #if CONFIG_IRQ_OFFLOAD
284 : : arch_irq_offload_init();
285 : : #endif
286 : :
287 : : /* Run kernel post-init hooks before POST_KERNEL device init, so that
288 : : * drivers initialized there may use them (e.g. submit to the system
289 : : * work queue). Entries are present only for subsystems that are linked
290 : : * in (pay-per-use); the walk references only the section bounds.
291 : : */
292 [ - + - + ]: 1 : STRUCT_SECTION_FOREACH(k_kernel_init_post_entry, entry) {
293 : 0 : entry->init_fn();
294 : : }
295 : :
296 : 1 : z_sys_init_run_level(INIT_LEVEL_POST_KERNEL);
297 : :
298 : 1 : soc_late_init_hook();
299 : :
300 : 1 : board_late_init_hook();
301 : :
302 : : #if defined(CONFIG_STACK_POINTER_RANDOM) && (CONFIG_STACK_POINTER_RANDOM != 0)
303 : : z_stack_adjust_initialized = 1;
304 : : #endif /* CONFIG_STACK_POINTER_RANDOM */
305 : :
306 : : #ifdef CONFIG_STATIC_INIT_GNU
307 : 1 : z_static_init_gnu();
308 : : #endif /* CONFIG_STATIC_INIT_GNU */
309 : :
310 : : /* Final init level before app starts */
311 : 1 : z_sys_init_run_level(INIT_LEVEL_APPLICATION);
312 : :
313 : 1 : z_init_static_threads();
314 : :
315 : : #ifdef CONFIG_KERNEL_COHERENCE
316 : : __ASSERT_NO_MSG(sys_cache_is_mem_coherent(&_kernel));
317 : : #endif /* CONFIG_KERNEL_COHERENCE */
318 : :
319 : : #ifdef CONFIG_SMP
320 : : /* Start the secondary CPUs; CPUs whose devicetree node carries
321 : : * zephyr,deferred-start are skipped and left for a run-time
322 : : * k_smp_cpu_start()/k_smp_cpu_resume().
323 : : */
324 : : z_smp_init();
325 : : z_sys_init_run_level(INIT_LEVEL_SMP);
326 : : #endif /* CONFIG_SMP */
327 : :
328 : : #ifdef CONFIG_MMU
329 : : z_mem_manage_boot_finish();
330 : : #endif /* CONFIG_MMU */
331 : :
332 : : #ifdef CONFIG_BOOTARGS
333 : : extern int main(int argc, char **argv);
334 : : extern char **sys_boot_prepare_main_args(int *argc);
335 : :
336 : : int argc = 0;
337 : : char **argv = sys_boot_prepare_main_args(&argc);
338 : : (void)main(argc, argv);
339 : : #else
340 : 1 : extern int main(void);
341 : :
342 : 1 : (void)main();
343 : : #endif /* CONFIG_BOOTARGS */
344 : :
345 : : #ifdef CONFIG_MULTITHREADING
346 : : /* Mark non-essential since main() has no more work to do */
347 : 0 : z_thread_essential_clear(&z_main_thread);
348 : : #endif
349 : :
350 : : #ifdef CONFIG_COVERAGE_DUMP
351 : : /* Dump coverage data once the main() has exited. */
352 : : gcov_coverage_dump();
353 : : #elif defined(CONFIG_COVERAGE_SEMIHOST)
354 : : gcov_coverage_semihost();
355 : : #endif /* CONFIG_COVERAGE_DUMP */
356 : : } /* LCOV_EXCL_LINE ... because we just dumped final coverage data */
357 : :
358 : : #if defined(CONFIG_MULTITHREADING)
359 : : __boot_func
360 : 1 : static void init_idle_thread(int i)
361 : : {
362 : 1 : struct k_thread *thread = &z_idle_threads[i];
363 : 1 : k_thread_stack_t *stack = z_idle_stacks[i];
364 : 1 : size_t stack_size = K_KERNEL_STACK_SIZEOF(z_idle_stacks[i]);
365 : :
366 : : #ifdef CONFIG_THREAD_NAME
367 : :
368 : : #if CONFIG_MP_MAX_NUM_CPUS > 1
369 : : char tname[8];
370 : : snprintk(tname, 8, "idle %02d", i);
371 : : #else
372 : 1 : char *tname = "idle";
373 : : #endif /* CONFIG_MP_MAX_NUM_CPUS */
374 : :
375 : : #else
376 : : char *tname = NULL;
377 : : #endif /* CONFIG_THREAD_NAME */
378 : :
379 : 1 : z_setup_new_thread(thread, stack,
380 : 1 : stack_size, idle, &_kernel.cpus[i],
381 : : NULL, NULL, K_IDLE_PRIO, K_ESSENTIAL,
382 : : tname);
383 : 1 : z_mark_thread_as_not_sleeping(thread);
384 : 1 : z_smp_thread_mark_idle(thread);
385 : 1 : }
386 : :
387 : 1 : void z_init_cpu(int id)
388 : : {
389 : 1 : init_idle_thread(id);
390 : 1 : _kernel.cpus[id].idle_thread = &z_idle_threads[id];
391 : 1 : _kernel.cpus[id].id = id;
392 : 1 : _kernel.cpus[id].irq_stack =
393 : 1 : (K_KERNEL_STACK_BUFFER(z_interrupt_stacks[id]) +
394 : : K_KERNEL_STACK_SIZEOF(z_interrupt_stacks[id]));
395 : : #ifdef CONFIG_SCHED_THREAD_USAGE_ALL
396 : : _kernel.cpus[id].usage = &_kernel.usage[id];
397 : : _kernel.cpus[id].usage->track_usage =
398 : : CONFIG_SCHED_THREAD_USAGE_AUTO_ENABLE;
399 : : #endif
400 : :
401 : : #ifdef CONFIG_PM
402 : : /*
403 : : * Increment number of CPUs active. The pm subsystem
404 : : * will keep track of this from here.
405 : : */
406 : : atomic_inc(&_cpus_active);
407 : : #endif
408 : :
409 : : #ifdef CONFIG_OBJ_CORE_SYSTEM
410 : : k_obj_core_init_and_link(K_OBJ_CORE(&_kernel.cpus[id]), &obj_type_cpu);
411 : : #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
412 : : k_obj_core_stats_register(K_OBJ_CORE(&_kernel.cpus[id]),
413 : : _kernel.cpus[id].usage,
414 : : sizeof(struct k_cycle_stats));
415 : : #endif
416 : : #endif
417 : :
418 : 1 : z_smp_cpu_init(&_kernel.cpus[id]);
419 : 1 : }
420 : :
421 : : /**
422 : : *
423 : : * @brief Initializes kernel data structures
424 : : *
425 : : * This routine initializes various kernel data structures, including
426 : : * the init and idle threads and any architecture-specific initialization.
427 : : *
428 : : * Note that all fields of "_kernel" are set to zero on entry, which may
429 : : * be all the initialization many of them require.
430 : : *
431 : : * @return initial stack pointer for the main thread
432 : : */
433 : : __boot_func
434 : 1 : static char *prepare_multithreading(void)
435 : : {
436 : 1 : char *stack_ptr;
437 : :
438 : : /* _kernel.ready_q is all zeroes */
439 : 1 : z_sched_init();
440 : :
441 : : #ifndef CONFIG_SMP
442 : : /*
443 : : * prime the cache with the main thread since:
444 : : *
445 : : * - the cache can never be NULL
446 : : * - the main thread will be the one to run first
447 : : * - no other thread is initialized yet and thus their priority fields
448 : : * contain garbage, which would prevent the cache loading algorithm
449 : : * to work as intended
450 : : */
451 : 1 : _kernel.ready_q.cache = &z_main_thread;
452 : : #endif /* CONFIG_SMP */
453 : 1 : stack_ptr = z_setup_new_thread(&z_main_thread, z_main_stack,
454 : : K_THREAD_STACK_SIZEOF(z_main_stack),
455 : : bg_thread_main,
456 : : NULL, NULL, NULL,
457 : : CONFIG_MAIN_THREAD_PRIORITY,
458 : : K_ESSENTIAL, "main");
459 : 1 : z_mark_thread_as_not_sleeping(&z_main_thread);
460 : 1 : z_ready_thread(&z_main_thread);
461 : :
462 : 1 : z_init_cpu(0);
463 : :
464 : 1 : return stack_ptr;
465 : : }
466 : :
467 : : __boot_func
468 : 1 : static FUNC_NORETURN void switch_to_main_thread(char *stack_ptr)
469 : : {
470 : : #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN
471 : 1 : arch_switch_to_main_thread(&z_main_thread, stack_ptr, bg_thread_main);
472 : : #else
473 : : ARG_UNUSED(stack_ptr);
474 : : /*
475 : : * Context switch to main task (entry function is _main()): the
476 : : * current fake thread is not on a wait queue or ready queue, so it
477 : : * will never be rescheduled in.
478 : : */
479 : : z_swap_unlocked();
480 : : #endif /* CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN */
481 : : CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
482 : : }
483 : : #endif /* CONFIG_MULTITHREADING */
484 : :
485 : : __boot_func
486 : : FUNC_NO_STACK_PROTECTOR
487 : 0 : void __weak z_early_rand_get(uint8_t *buf, size_t length)
488 : : {
489 : 0 : static uint64_t state = (uint64_t)CONFIG_TIMER_RANDOM_INITIAL_STATE;
490 : 0 : int rc;
491 : :
492 : : #ifdef CONFIG_ENTROPY_HAS_DRIVER
493 : : const struct device *const entropy = entropy_get_default_device();
494 : :
495 : : if ((entropy != NULL) && device_is_ready(entropy)) {
496 : : /* Try to see if driver provides an ISR-specific API */
497 : : rc = entropy_get_entropy_isr(entropy, buf, length, ENTROPY_BUSYWAIT);
498 : : if (rc > 0) {
499 : : length -= rc;
500 : : buf += rc;
501 : : }
502 : : }
503 : : #endif /* CONFIG_ENTROPY_HAS_DRIVER */
504 : :
505 [ # # ]: 0 : while (length > 0) {
506 : 0 : uint32_t val;
507 : :
508 : 0 : state = state + k_cycle_get_32();
509 : 0 : state = state * 2862933555777941757ULL + 3037000493ULL;
510 : 0 : val = (uint32_t)(state >> 32);
511 : 0 : rc = min(length, sizeof(val));
512 : 0 : arch_early_memcpy((void *)buf, &val, rc);
513 : :
514 : 0 : length -= rc;
515 : 0 : buf += rc;
516 : : }
517 : 0 : }
518 : :
519 : : /**
520 : : *
521 : : * @brief Initialize kernel
522 : : *
523 : : * This routine is invoked when the system is ready to run C code. The
524 : : * processor must be running in 32-bit mode, and the BSS must have been
525 : : * cleared/zeroed.
526 : : *
527 : : * @return Does not return
528 : : */
529 : : __boot_func
530 : : FUNC_NO_STACK_PROTECTOR
531 : 1 : FUNC_NORETURN void z_cstart(void)
532 : : {
533 : : /* gcov hook needed to get the coverage report.*/
534 : 1 : gcov_static_init();
535 : :
536 : : /* initialize early init calls */
537 : 1 : z_sys_init_run_level(INIT_LEVEL_EARLY);
538 : :
539 : : /* perform any architecture-specific initialization */
540 : 1 : arch_kernel_init();
541 : :
542 : 1 : LOG_CORE_INIT();
543 : :
544 : : #if defined(CONFIG_MULTITHREADING)
545 : 1 : z_dummy_thread_init(&_thread_dummy);
546 : : #endif /* CONFIG_MULTITHREADING */
547 : : /* do any necessary initialization of static devices */
548 : 1 : z_device_state_init();
549 : :
550 : 1 : soc_early_init_hook();
551 : :
552 : 1 : board_early_init_hook();
553 : :
554 : : /* Run kernel pre-init hooks before PRE_KERNEL device init, so those
555 : : * drivers may use the kernel objects initialized here. Entries are
556 : : * present only for subsystems that are linked in (pay-per-use); the
557 : : * walk references only the section bounds.
558 : : */
559 [ - + - + ]: 1 : STRUCT_SECTION_FOREACH(k_kernel_init_pre_entry, entry) {
560 : 0 : entry->init_fn();
561 : : }
562 : :
563 : : /* perform basic hardware initialization */
564 : 1 : z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_1);
565 : 1 : z_smp_arch_init();
566 : 1 : z_sys_init_run_level(INIT_LEVEL_PRE_KERNEL_2);
567 : :
568 : : #ifdef CONFIG_REQUIRES_STACK_CANARIES
569 : : uintptr_t stack_guard;
570 : :
571 : : z_early_rand_get((uint8_t *)&stack_guard, sizeof(stack_guard));
572 : : __stack_chk_guard = stack_guard;
573 : : __stack_chk_guard <<= 8;
574 : : #endif /* CONFIG_REQUIRES_STACK_CANARIES */
575 : :
576 : : #ifdef CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT
577 : : timing_init();
578 : : timing_start();
579 : : #endif /* CONFIG_TIMING_FUNCTIONS_NEED_AT_BOOT */
580 : :
581 : : #ifdef CONFIG_MULTITHREADING
582 : 1 : switch_to_main_thread(prepare_multithreading());
583 : : #else
584 : : #ifdef ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING
585 : : /* Custom ARCH-specific routine to switch to main()
586 : : * in the case of no multi-threading.
587 : : */
588 : : ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING(bg_thread_main,
589 : : NULL, NULL, NULL);
590 : : #else
591 : : bg_thread_main(NULL, NULL, NULL);
592 : :
593 : : /* LCOV_EXCL_START
594 : : * We've already dumped coverage data at this point.
595 : : */
596 : : irq_lock();
597 : : while (true) {
598 : : }
599 : : /* LCOV_EXCL_STOP */
600 : : #endif /* ARCH_SWITCH_TO_MAIN_NO_MULTITHREADING */
601 : : #endif /* CONFIG_MULTITHREADING */
602 : :
603 : : /*
604 : : * Compiler can't tell that the above routines won't return and issues
605 : : * a warning unless we explicitly tell it that control never gets this
606 : : * far.
607 : : */
608 : :
609 : : CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
610 : : }
|