Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 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 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 9 | #include <zephyr/kernel.h> |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 10 | #include <kernel_internal.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 11 | #include <zephyr/linker/linker-defs.h> |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 12 | |
| 13 | #ifdef CONFIG_STACK_CANARIES |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 14 | #ifdef CONFIG_STACK_CANARIES_TLS |
| 15 | extern __thread volatile uintptr_t __stack_chk_guard; |
| 16 | #else |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 17 | extern volatile uintptr_t __stack_chk_guard; |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 18 | #endif /* CONFIG_STACK_CANARIES_TLS */ |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 19 | #endif /* CONFIG_STACK_CANARIES */ |
| 20 | |
| 21 | /** |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 22 | * @brief Copy the data section from ROM to RAM |
| 23 | * |
| 24 | * This routine copies the data section from ROM to RAM. |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 25 | */ |
| 26 | void z_data_copy(void) |
| 27 | { |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 28 | z_early_memcpy(&__data_region_start, &__data_region_load_start, |
| 29 | __data_region_end - __data_region_start); |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 30 | #ifdef CONFIG_ARCH_HAS_RAMFUNC_SUPPORT |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 31 | z_early_memcpy(&__ramfunc_start, &__ramfunc_load_start, |
| 32 | (uintptr_t) &__ramfunc_size); |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 33 | #endif /* CONFIG_ARCH_HAS_RAMFUNC_SUPPORT */ |
| 34 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 35 | z_early_memcpy(&__ccm_data_start, &__ccm_data_rom_start, |
| 36 | __ccm_data_end - __ccm_data_start); |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 37 | #endif |
Maureen Helm | f633852 | 2021-01-06 08:52:28 -0600 | [diff] [blame] | 38 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 39 | z_early_memcpy(&__itcm_start, &__itcm_load_start, |
| 40 | (uintptr_t) &__itcm_size); |
Maureen Helm | f633852 | 2021-01-06 08:52:28 -0600 | [diff] [blame] | 41 | #endif |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 42 | #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay) |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 43 | z_early_memcpy(&__dtcm_data_start, &__dtcm_data_load_start, |
| 44 | __dtcm_data_end - __dtcm_data_start); |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 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 |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 71 | z_early_memcpy(&_app_smem_start, &_app_smem_rom_start, |
| 72 | _app_smem_end - _app_smem_start); |
Anas Nashif | de69edb | 2020-08-27 08:37:10 -0400 | [diff] [blame] | 73 | #endif /* CONFIG_STACK_CANARIES */ |
| 74 | #endif /* CONFIG_USERSPACE */ |
| 75 | } |