1 /*
2  * Copyright (c) 2010-2014 Wind River Systems, Inc.
3  * Copyright (c) 2020 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 
9 #include <zephyr/kernel.h>
10 #include <kernel_internal.h>
11 #include <zephyr/linker/linker-defs.h>
12 
13 #ifdef CONFIG_STACK_CANARIES
14 #ifdef CONFIG_STACK_CANARIES_TLS
15 extern __thread volatile uintptr_t __stack_chk_guard;
16 #else
17 extern volatile uintptr_t __stack_chk_guard;
18 #endif /* CONFIG_STACK_CANARIES_TLS */
19 #endif /* CONFIG_STACK_CANARIES */
20 
21 /**
22  * @brief Copy the data section from ROM to RAM
23  *
24  * This routine copies the data section from ROM to RAM.
25  */
z_data_copy(void)26 void z_data_copy(void)
27 {
28 	z_early_memcpy(&__data_region_start, &__data_region_load_start,
29 		       __data_region_end - __data_region_start);
30 #ifdef CONFIG_ARCH_HAS_RAMFUNC_SUPPORT
31 	z_early_memcpy(&__ramfunc_start, &__ramfunc_load_start,
32 		       (uintptr_t) &__ramfunc_size);
33 #endif /* CONFIG_ARCH_HAS_RAMFUNC_SUPPORT */
34 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay)
35 	z_early_memcpy(&__ccm_data_start, &__ccm_data_rom_start,
36 		       __ccm_data_end - __ccm_data_start);
37 #endif
38 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
39 	z_early_memcpy(&__itcm_start, &__itcm_load_start,
40 		       (uintptr_t) &__itcm_size);
41 #endif
42 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay)
43 	z_early_memcpy(&__dtcm_data_start, &__dtcm_data_load_start,
44 		       __dtcm_data_end - __dtcm_data_start);
45 #endif
46 #ifdef CONFIG_CODE_DATA_RELOCATION
47 	extern void data_copy_xip_relocation(void);
48 
49 	data_copy_xip_relocation();
50 #endif	/* CONFIG_CODE_DATA_RELOCATION */
51 #ifdef CONFIG_USERSPACE
52 #ifdef CONFIG_STACK_CANARIES
53 	/* stack canary checking is active for all C functions.
54 	 * __stack_chk_guard is some uninitialized value living in the
55 	 * app shared memory sections. Preserve it, and don't make any
56 	 * function calls to perform the memory copy. The true canary
57 	 * value gets set later in z_cstart().
58 	 */
59 	uintptr_t guard_copy = __stack_chk_guard;
60 	uint8_t *src = (uint8_t *)&_app_smem_rom_start;
61 	uint8_t *dst = (uint8_t *)&_app_smem_start;
62 	uint32_t count = _app_smem_end - _app_smem_start;
63 
64 	guard_copy = __stack_chk_guard;
65 	while (count > 0) {
66 		*(dst++) = *(src++);
67 		count--;
68 	}
69 	__stack_chk_guard = guard_copy;
70 #else
71 	z_early_memcpy(&_app_smem_start, &_app_smem_rom_start,
72 		       _app_smem_end - _app_smem_start);
73 #endif /* CONFIG_STACK_CANARIES */
74 #endif /* CONFIG_USERSPACE */
75 }
76