1 /*
2  * Copyright (c) 2019 Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Full C support initialization
10  *
11  * Initialization of full C support: zero the .bss and call z_cstart().
12  *
13  * Stack is available in this module, but not the global data/bss until their
14  * initialization is performed.
15  */
16 
17 #include <kernel_internal.h>
18 #include <zephyr/linker/linker-defs.h>
19 
z_arm64_mm_init(bool is_primary_core)20 __weak void z_arm64_mm_init(bool is_primary_core) { }
21 
22 extern void z_arm64_mm_init(bool is_primary_core);
23 
24 /*
25  * These simple memset/memcpy alternatives are necessary as the optimized
26  * ones depend on the MMU to be active (see commit c5b898743a20).
27  */
z_early_memset(void * dst,int c,size_t n)28 void z_early_memset(void *dst, int c, size_t n)
29 {
30 	uint8_t *d = dst;
31 
32 	while (n--) {
33 		*d++ = c;
34 	}
35 }
36 
z_early_memcpy(void * dst,const void * src,size_t n)37 void z_early_memcpy(void *dst, const void *src, size_t n)
38 {
39 	uint8_t *d = dst;
40 	const uint8_t *s = src;
41 
42 	while (n--) {
43 		*d++ = *s++;
44 	}
45 }
46 
47 /**
48  *
49  * @brief Prepare to and run C code
50  *
51  * This routine prepares for the execution of and runs C code.
52  *
53  */
z_arm64_prep_c(void)54 void z_arm64_prep_c(void)
55 {
56 	/* Initialize tpidrro_el0 with our struct _cpu instance address */
57 	write_tpidrro_el0((uintptr_t)&_kernel.cpus[0]);
58 
59 	z_bss_zero();
60 	z_data_copy();
61 #ifdef CONFIG_ARM64_SAFE_EXCEPTION_STACK
62 	/* After bss clean, _kernel.cpus is in bss section */
63 	z_arm64_safe_exception_stack_init();
64 #endif
65 	z_arm64_mm_init(true);
66 	z_arm64_interrupt_init();
67 	z_cstart();
68 
69 	CODE_UNREACHABLE;
70 }
71 
72 #if CONFIG_MP_MAX_NUM_CPUS > 1
73 extern FUNC_NORETURN void z_arm64_secondary_start(void);
z_arm64_secondary_prep_c(void)74 void z_arm64_secondary_prep_c(void)
75 {
76 	z_arm64_secondary_start();
77 
78 	CODE_UNREACHABLE;
79 }
80 #endif
81