Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 1 | /* |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 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" |
| 10 | #include "tfm_integ_test.h" |
| 11 | #include "tfm_ns_svc.h" |
| 12 | #include "tfm_ns_interface.h" |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 13 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) |
| 14 | #include "test_framework_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" |
| 28 | #include "region.h" |
| 29 | |
| 30 | /** |
| 31 | * \brief Modified table template for user defined SVC functions |
| 32 | * |
| 33 | * \details RTX has a weak definition of osRtxUserSVC, which |
| 34 | * is overridden here |
| 35 | */ |
| 36 | #if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION == 6110004)) |
| 37 | /* Workaround needed for a bug in Armclang 6.11, more details at: |
| 38 | * http://www.keil.com/support/docs/4089.htm |
| 39 | */ |
| 40 | __attribute__((section(".gnu.linkonce"))) |
| 41 | #endif |
| 42 | extern void * const osRtxUserSVC[1+USER_SVC_COUNT]; |
| 43 | void * const osRtxUserSVC[1+USER_SVC_COUNT] = { |
| 44 | (void *)USER_SVC_COUNT, |
| 45 | |
| 46 | #define X(SVC_ENUM, SVC_HANDLER) (void*)SVC_HANDLER, |
| 47 | |
| 48 | /* SVC API for Services */ |
| 49 | #ifdef TFM_NS_CLIENT_IDENTIFICATION |
| 50 | LIST_SVC_NSPM |
| 51 | #endif |
| 52 | |
| 53 | #undef X |
| 54 | |
| 55 | /* |
| 56 | * (void *)user_function1, |
| 57 | * ... |
| 58 | */ |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * \brief List of RTOS thread attributes |
| 63 | */ |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 64 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 65 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 66 | static uint64_t test_app_stack[(4u * 1024u) / (sizeof(uint64_t))]; /* 4KB */ |
| 67 | static const osThreadAttr_t thread_attr = { |
| 68 | .name = "test_thread", |
| 69 | .stack_mem = test_app_stack, |
| 70 | .stack_size = sizeof(test_app_stack), |
| 71 | }; |
| 72 | #endif |
| 73 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 74 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 75 | static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner; |
| 76 | /* 1KB stack */ |
| 77 | #define MAILBOX_THREAD_STACK_SIZE (1u * 1024u) |
| 78 | static uint64_t mailbox_thread_stack[MAILBOX_THREAD_STACK_SIZE / |
| 79 | sizeof(uint64_t)]; |
| 80 | static const osThreadAttr_t mailbox_thread_attr = { |
| 81 | .name = "mailbox_thread", |
| 82 | .stack_mem = mailbox_thread_stack, |
| 83 | .stack_size = sizeof(mailbox_thread_stack), |
| 84 | }; |
| 85 | #endif |
| 86 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 87 | /** |
| 88 | * \brief Static globals to hold RTOS related quantities, |
| 89 | * main thread |
| 90 | */ |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 91 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 92 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 93 | static osThreadFunc_t thread_func; |
| 94 | #endif |
| 95 | |
| 96 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 97 | static struct ns_mailbox_queue_t ns_mailbox_queue; |
| 98 | |
| 99 | static void tfm_ns_multi_core_boot(void) |
| 100 | { |
| 101 | int32_t ret; |
| 102 | |
| 103 | LOG_MSG("Non-secure code running on non-secure core."); |
| 104 | |
| 105 | if (tfm_ns_wait_for_s_cpu_ready()) { |
| 106 | LOG_MSG("Error sync'ing with secure core."); |
| 107 | |
| 108 | /* Avoid undefined behavior after multi-core sync-up failed */ |
| 109 | for (;;) { |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | ret = tfm_ns_mailbox_init(&ns_mailbox_queue); |
| 114 | if (ret != MAILBOX_SUCCESS) { |
| 115 | LOG_MSG("Non-secure mailbox initialization failed."); |
| 116 | |
| 117 | /* Avoid undefined behavior after NS mailbox initialization failed */ |
| 118 | for (;;) { |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | /** |
| 125 | * \brief Platform peripherals and devices initialization. |
| 126 | * Can be overridden for platform specific initialization. |
| 127 | * |
| 128 | * \return ARM_DRIVER_OK if the initialization succeeds |
| 129 | */ |
| 130 | __WEAK int32_t tfm_ns_platform_init(void) |
| 131 | { |
| 132 | stdio_init(); |
| 133 | |
| 134 | return ARM_DRIVER_OK; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * \brief Platform peripherals and devices de-initialization. |
| 139 | * Can be overridden for platform specific initialization. |
| 140 | * |
| 141 | * \return ARM_DRIVER_OK if the de-initialization succeeds |
| 142 | */ |
| 143 | __WEAK int32_t tfm_ns_platform_uninit(void) |
| 144 | { |
| 145 | stdio_uninit(); |
| 146 | |
| 147 | return ARM_DRIVER_OK; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * \brief main() function |
| 152 | */ |
| 153 | #ifndef __GNUC__ |
| 154 | __attribute__((noreturn)) |
| 155 | #endif |
| 156 | int main(void) |
| 157 | { |
| 158 | #if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__) |
| 159 | /* Set Main Stack Pointer limit */ |
| 160 | REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base); |
| 161 | __set_MSPLIM((uint32_t)®ION_NAME(Image$$, ARM_LIB_STACK_MSP, |
| 162 | $$ZI$$Base)); |
| 163 | #endif |
| 164 | |
| 165 | if (tfm_ns_platform_init() != ARM_DRIVER_OK) { |
| 166 | /* Avoid undefined behavior if platform init failed */ |
| 167 | while(1); |
| 168 | } |
| 169 | |
David Hu | 4ae00fe | 2021-01-27 17:48:07 +0800 | [diff] [blame^] | 170 | (void) osKernelInitialize(); |
| 171 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 172 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 173 | tfm_ns_multi_core_boot(); |
| 174 | #endif |
| 175 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 176 | /* Initialize the TFM NS interface */ |
| 177 | tfm_ns_interface_init(); |
| 178 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 179 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 180 | (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr); |
| 181 | #endif |
| 182 | |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 183 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 184 | thread_func = test_app; |
| 185 | #elif defined(PSA_API_TEST_NS) |
| 186 | thread_func = psa_api_test; |
| 187 | #endif |
| 188 | |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 189 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 190 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 191 | (void) osThreadNew(thread_func, NULL, &thread_attr); |
| 192 | #endif |
| 193 | |
| 194 | LOG_MSG("Non-Secure system starting...\r\n"); |
| 195 | (void) osKernelStart(); |
| 196 | |
| 197 | /* Reached only in case of error */ |
| 198 | for (;;) { |
| 199 | } |
| 200 | } |