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