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