Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 1 | /* |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame^] | 2 | * Copyright (c) 2017-2022, Arm Limited. All rights reserved. |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "tfm_api.h" |
| 9 | #include "cmsis_os2.h" |
Xinyu Zhang | 4c640e8 | 2021-09-22 15:25:09 +0800 | [diff] [blame] | 10 | #include "cmsis_compiler.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 11 | #include "tfm_ns_interface.h" |
Xinyu Zhang | eebbea3 | 2021-09-01 15:26:39 +0800 | [diff] [blame] | 12 | #include "tfm_nsid_manager.h" |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 13 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) |
David Hu | acba69e | 2021-09-10 15:36:48 +0800 | [diff] [blame] | 14 | #include "tfm_integ_test.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 15 | #endif |
| 16 | #ifdef PSA_API_TEST_NS |
| 17 | #include "psa_api_test.h" |
| 18 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 19 | #include "tfm_plat_ns.h" |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 20 | #include "driver/Driver_USART.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 21 | #include "device_cfg.h" |
| 22 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 23 | #include "tfm_multi_core_api.h" |
| 24 | #include "tfm_ns_mailbox.h" |
| 25 | #endif |
Summer Qin | 7789423 | 2020-08-28 11:24:15 +0800 | [diff] [blame] | 26 | #include "tfm_log.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 27 | #include "uart_stdout.h" |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame^] | 28 | #if (CONFIG_TFM_FP >= 1) |
| 29 | #include "cmsis.h" |
| 30 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * \brief Modified table template for user defined SVC functions |
| 34 | * |
| 35 | * \details RTX has a weak definition of osRtxUserSVC, which |
| 36 | * is overridden here |
| 37 | */ |
David Hu | acba69e | 2021-09-10 15:36:48 +0800 | [diff] [blame] | 38 | #if defined(__ARMCC_VERSION) |
| 39 | #if (__ARMCC_VERSION == 6110004) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 40 | /* Workaround needed for a bug in Armclang 6.11, more details at: |
| 41 | * http://www.keil.com/support/docs/4089.htm |
| 42 | */ |
| 43 | __attribute__((section(".gnu.linkonce"))) |
| 44 | #endif |
David Hu | acba69e | 2021-09-10 15:36:48 +0800 | [diff] [blame] | 45 | |
| 46 | /* Avoids the semihosting issue */ |
| 47 | #if (__ARMCC_VERSION >= 6010050) |
| 48 | __asm(" .global __ARM_use_no_argv\n"); |
| 49 | #endif |
| 50 | #endif |
| 51 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 52 | /** |
| 53 | * \brief List of RTOS thread attributes |
| 54 | */ |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 55 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 56 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 57 | static const osThreadAttr_t thread_attr = { |
| 58 | .name = "test_thread", |
Xinyu Zhang | eebbea3 | 2021-09-01 15:26:39 +0800 | [diff] [blame] | 59 | .stack_size = 4096U, |
| 60 | .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 61 | }; |
| 62 | #endif |
| 63 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 64 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 65 | static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner; |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 66 | static const osThreadAttr_t mailbox_thread_attr = { |
| 67 | .name = "mailbox_thread", |
Robert Rostohar | 26ebd14 | 2020-12-21 16:48:58 +0100 | [diff] [blame] | 68 | .stack_size = 1024U |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 69 | }; |
| 70 | #endif |
| 71 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 72 | /** |
| 73 | * \brief Static globals to hold RTOS related quantities, |
| 74 | * main thread |
| 75 | */ |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 76 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 77 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 78 | static osThreadFunc_t thread_func; |
| 79 | #endif |
| 80 | |
| 81 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 82 | static struct ns_mailbox_queue_t ns_mailbox_queue; |
| 83 | |
| 84 | static void tfm_ns_multi_core_boot(void) |
| 85 | { |
| 86 | int32_t ret; |
| 87 | |
Chris Brand | a149929 | 2021-10-28 12:02:05 -0700 | [diff] [blame] | 88 | LOG_MSG("Non-secure code running on non-secure core.\r\n"); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 89 | |
| 90 | if (tfm_ns_wait_for_s_cpu_ready()) { |
Chris Brand | a149929 | 2021-10-28 12:02:05 -0700 | [diff] [blame] | 91 | LOG_MSG("Error sync'ing with secure core.\r\n"); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 92 | |
| 93 | /* Avoid undefined behavior after multi-core sync-up failed */ |
| 94 | for (;;) { |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | ret = tfm_ns_mailbox_init(&ns_mailbox_queue); |
| 99 | if (ret != MAILBOX_SUCCESS) { |
Chris Brand | a149929 | 2021-10-28 12:02:05 -0700 | [diff] [blame] | 100 | LOG_MSG("Non-secure mailbox initialization failed.\r\n"); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 101 | |
| 102 | /* Avoid undefined behavior after NS mailbox initialization failed */ |
| 103 | for (;;) { |
| 104 | } |
| 105 | } |
| 106 | } |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 107 | #else |
| 108 | extern uint32_t tfm_ns_interface_init(void); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 109 | #endif |
| 110 | |
| 111 | /** |
| 112 | * \brief Platform peripherals and devices initialization. |
| 113 | * Can be overridden for platform specific initialization. |
| 114 | * |
| 115 | * \return ARM_DRIVER_OK if the initialization succeeds |
| 116 | */ |
| 117 | __WEAK int32_t tfm_ns_platform_init(void) |
| 118 | { |
| 119 | stdio_init(); |
| 120 | |
| 121 | return ARM_DRIVER_OK; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * \brief Platform peripherals and devices de-initialization. |
| 126 | * Can be overridden for platform specific initialization. |
| 127 | * |
| 128 | * \return ARM_DRIVER_OK if the de-initialization succeeds |
| 129 | */ |
| 130 | __WEAK int32_t tfm_ns_platform_uninit(void) |
| 131 | { |
| 132 | stdio_uninit(); |
| 133 | |
| 134 | return ARM_DRIVER_OK; |
| 135 | } |
| 136 | |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame^] | 137 | |
| 138 | __WEAK int32_t tfm_ns_cp_init(void) |
| 139 | { |
| 140 | #if (CONFIG_TFM_FP >= 1) |
| 141 | #ifdef __GNUC__ |
| 142 | /* Enable NSPE privileged and unprivilged access to the FP Extension */ |
| 143 | SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */ |
| 144 | | (3U << 11U*2U); /* enable CP11 full access */ |
| 145 | #endif |
| 146 | #endif |
| 147 | return ARM_DRIVER_OK; |
| 148 | } |
| 149 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 150 | /** |
| 151 | * \brief main() function |
| 152 | */ |
| 153 | #ifndef __GNUC__ |
| 154 | __attribute__((noreturn)) |
| 155 | #endif |
| 156 | int main(void) |
| 157 | { |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 158 | if (tfm_ns_platform_init() != ARM_DRIVER_OK) { |
| 159 | /* Avoid undefined behavior if platform init failed */ |
| 160 | while(1); |
| 161 | } |
| 162 | |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame^] | 163 | if (tfm_ns_cp_init() != ARM_DRIVER_OK) { |
| 164 | /* Avoid undefined behavior if co-porcessor init failed */ |
| 165 | while(1); |
| 166 | } |
| 167 | |
David Hu | 4ae00fe | 2021-01-27 17:48:07 +0800 | [diff] [blame] | 168 | (void) osKernelInitialize(); |
| 169 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 170 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 171 | tfm_ns_multi_core_boot(); |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 172 | #else |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 173 | /* Initialize the TFM NS interface */ |
| 174 | tfm_ns_interface_init(); |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 175 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 176 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 177 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 178 | (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr); |
| 179 | #endif |
| 180 | |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 181 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 182 | thread_func = test_app; |
| 183 | #elif defined(PSA_API_TEST_NS) |
| 184 | thread_func = psa_api_test; |
| 185 | #endif |
| 186 | |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 187 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 188 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 189 | (void) osThreadNew(thread_func, NULL, &thread_attr); |
| 190 | #endif |
| 191 | |
| 192 | LOG_MSG("Non-Secure system starting...\r\n"); |
| 193 | (void) osKernelStart(); |
| 194 | |
| 195 | /* Reached only in case of error */ |
| 196 | for (;;) { |
| 197 | } |
| 198 | } |