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.h>
10 #include <kernel.h>
11 #include <string.h>
12 #include <linker/linker-defs.h>
13 
14 #ifdef CONFIG_STACK_CANARIES
15 extern volatile uintptr_t __stack_chk_guard;
16 #endif /* CONFIG_STACK_CANARIES */
17 
18 /**
19  *
20  * @brief Copy the data section from ROM to RAM
21  *
22  * This routine copies the data section from ROM to RAM.
23  *
24  * @return N/A
25  */
z_data_copy(void)26 void z_data_copy(void)
27 {
28 	(void)memcpy(&__data_region_start, &__data_region_load_start,
29 		 __data_region_end - __data_region_start);
30 #ifdef CONFIG_ARCH_HAS_RAMFUNC_SUPPORT
31 	(void)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 	(void)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 	(void)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 	(void)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 	(void)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