blob: a5752560c853d0a6c9648243f4d67c904cabcfb2 [file] [log] [blame]
Anas Nashifde69edb2020-08-27 08:37:10 -04001/*
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
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02009#include <zephyr/kernel.h>
Nicolas Pitre678b76e2022-02-10 13:54:49 -050010#include <kernel_internal.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020011#include <zephyr/linker/linker-defs.h>
Anas Nashifde69edb2020-08-27 08:37:10 -040012
13#ifdef CONFIG_STACK_CANARIES
Flavio Ceolind16c5b92023-08-01 15:07:57 -070014#ifdef CONFIG_STACK_CANARIES_TLS
15extern __thread volatile uintptr_t __stack_chk_guard;
16#else
Anas Nashifde69edb2020-08-27 08:37:10 -040017extern volatile uintptr_t __stack_chk_guard;
Flavio Ceolind16c5b92023-08-01 15:07:57 -070018#endif /* CONFIG_STACK_CANARIES_TLS */
Anas Nashifde69edb2020-08-27 08:37:10 -040019#endif /* CONFIG_STACK_CANARIES */
20
21/**
Anas Nashifde69edb2020-08-27 08:37:10 -040022 * @brief Copy the data section from ROM to RAM
23 *
24 * This routine copies the data section from ROM to RAM.
Anas Nashifde69edb2020-08-27 08:37:10 -040025 */
26void z_data_copy(void)
27{
Nicolas Pitre678b76e2022-02-10 13:54:49 -050028 z_early_memcpy(&__data_region_start, &__data_region_load_start,
29 __data_region_end - __data_region_start);
Anas Nashifde69edb2020-08-27 08:37:10 -040030#ifdef CONFIG_ARCH_HAS_RAMFUNC_SUPPORT
Nicolas Pitre678b76e2022-02-10 13:54:49 -050031 z_early_memcpy(&__ramfunc_start, &__ramfunc_load_start,
32 (uintptr_t) &__ramfunc_size);
Anas Nashifde69edb2020-08-27 08:37:10 -040033#endif /* CONFIG_ARCH_HAS_RAMFUNC_SUPPORT */
34#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay)
Nicolas Pitre678b76e2022-02-10 13:54:49 -050035 z_early_memcpy(&__ccm_data_start, &__ccm_data_rom_start,
36 __ccm_data_end - __ccm_data_start);
Anas Nashifde69edb2020-08-27 08:37:10 -040037#endif
Maureen Helmf6338522021-01-06 08:52:28 -060038#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
Nicolas Pitre678b76e2022-02-10 13:54:49 -050039 z_early_memcpy(&__itcm_start, &__itcm_load_start,
40 (uintptr_t) &__itcm_size);
Maureen Helmf6338522021-01-06 08:52:28 -060041#endif
Anas Nashifde69edb2020-08-27 08:37:10 -040042#if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay)
Nicolas Pitre678b76e2022-02-10 13:54:49 -050043 z_early_memcpy(&__dtcm_data_start, &__dtcm_data_load_start,
44 __dtcm_data_end - __dtcm_data_start);
Anas Nashifde69edb2020-08-27 08:37:10 -040045#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
Nicolas Pitre678b76e2022-02-10 13:54:49 -050071 z_early_memcpy(&_app_smem_start, &_app_smem_rom_start,
72 _app_smem_end - _app_smem_start);
Anas Nashifde69edb2020-08-27 08:37:10 -040073#endif /* CONFIG_STACK_CANARIES */
74#endif /* CONFIG_USERSPACE */
75}