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