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_REQUIRES_STACK_CANARIES
14 #ifdef CONFIG_STACK_CANARIES_TLS
15 extern Z_THREAD_LOCAL 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_REQUIRES_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_region_start, &__ramfunc_load_start,
32 		       __ramfunc_end - __ramfunc_region_start);
33 #endif /* CONFIG_ARCH_HAS_RAMFUNC_SUPPORT */
34 #ifdef CONFIG_ARCH_HAS_NOCACHE_MEMORY_SUPPORT
35 #if CONFIG_NOCACHE_MEMORY
36 	z_early_memcpy(&_nocache_ram_start, &_nocache_load_start,
37 		       (uintptr_t) &_nocache_ram_size);
38 #endif /* CONFIG_NOCACHE_MEMORY */
39 #endif /* CONFIG_ARCH_HAS_NOCACHE_MEMORY_SUPPORT */
40 #if DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_ccm))
41 	z_early_memcpy(&__ccm_data_start, &__ccm_data_load_start,
42 		       __ccm_data_end - __ccm_data_start);
43 #endif
44 #if DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_itcm))
45 	z_early_memcpy(&__itcm_start, &__itcm_load_start,
46 		       (uintptr_t) &__itcm_size);
47 #endif
48 #if DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_dtcm))
49 	z_early_memcpy(&__dtcm_data_start, &__dtcm_data_load_start,
50 		       __dtcm_data_end - __dtcm_data_start);
51 #endif
52 #ifdef CONFIG_CODE_DATA_RELOCATION
53 	extern void data_copy_xip_relocation(void);
54 
55 	data_copy_xip_relocation();
56 #endif	/* CONFIG_CODE_DATA_RELOCATION */
57 #ifdef CONFIG_USERSPACE
58 #ifdef CONFIG_REQUIRES_STACK_CANARIES
59 	/* stack canary checking is active for all C functions.
60 	 * __stack_chk_guard is some uninitialized value living in the
61 	 * app shared memory sections. Preserve it, and don't make any
62 	 * function calls to perform the memory copy. The true canary
63 	 * value gets set later in z_cstart().
64 	 */
65 	uintptr_t guard_copy = __stack_chk_guard;
66 	uint8_t *src = (uint8_t *)&_app_smem_rom_start;
67 	uint8_t *dst = (uint8_t *)&_app_smem_start;
68 	uint32_t count = _app_smem_end - _app_smem_start;
69 
70 	guard_copy = __stack_chk_guard;
71 	while (count > 0) {
72 		*(dst++) = *(src++);
73 		count--;
74 	}
75 	__stack_chk_guard = guard_copy;
76 #else
77 	z_early_memcpy(&_app_smem_start, &_app_smem_rom_start,
78 		       _app_smem_end - _app_smem_start);
79 #endif /* CONFIG_REQUIRES_STACK_CANARIES */
80 #endif /* CONFIG_USERSPACE */
81 }
82