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" |
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" |
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 | /** |
| 49 | * \brief List of RTOS thread attributes |
| 50 | */ |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 51 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 52 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 53 | static const osThreadAttr_t thread_attr = { |
| 54 | .name = "test_thread", |
Robert Rostohar | 26ebd14 | 2020-12-21 16:48:58 +0100 | [diff] [blame] | 55 | .stack_size = 4096U |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 56 | }; |
| 57 | #endif |
| 58 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 59 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 60 | static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner; |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 61 | static const osThreadAttr_t mailbox_thread_attr = { |
| 62 | .name = "mailbox_thread", |
Robert Rostohar | 26ebd14 | 2020-12-21 16:48:58 +0100 | [diff] [blame] | 63 | .stack_size = 1024U |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 64 | }; |
| 65 | #endif |
| 66 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 67 | /** |
| 68 | * \brief Static globals to hold RTOS related quantities, |
| 69 | * main thread |
| 70 | */ |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 71 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 72 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 73 | static osThreadFunc_t thread_func; |
| 74 | #endif |
| 75 | |
| 76 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 77 | static struct ns_mailbox_queue_t ns_mailbox_queue; |
| 78 | |
| 79 | static void tfm_ns_multi_core_boot(void) |
| 80 | { |
| 81 | int32_t ret; |
| 82 | |
| 83 | LOG_MSG("Non-secure code running on non-secure core."); |
| 84 | |
| 85 | if (tfm_ns_wait_for_s_cpu_ready()) { |
| 86 | LOG_MSG("Error sync'ing with secure core."); |
| 87 | |
| 88 | /* Avoid undefined behavior after multi-core sync-up failed */ |
| 89 | for (;;) { |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | ret = tfm_ns_mailbox_init(&ns_mailbox_queue); |
| 94 | if (ret != MAILBOX_SUCCESS) { |
| 95 | LOG_MSG("Non-secure mailbox initialization failed."); |
| 96 | |
| 97 | /* Avoid undefined behavior after NS mailbox initialization failed */ |
| 98 | for (;;) { |
| 99 | } |
| 100 | } |
| 101 | } |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 102 | #else |
| 103 | extern uint32_t tfm_ns_interface_init(void); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 104 | #endif |
| 105 | |
| 106 | /** |
| 107 | * \brief Platform peripherals and devices initialization. |
| 108 | * Can be overridden for platform specific initialization. |
| 109 | * |
| 110 | * \return ARM_DRIVER_OK if the initialization succeeds |
| 111 | */ |
| 112 | __WEAK int32_t tfm_ns_platform_init(void) |
| 113 | { |
| 114 | stdio_init(); |
| 115 | |
| 116 | return ARM_DRIVER_OK; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * \brief Platform peripherals and devices de-initialization. |
| 121 | * Can be overridden for platform specific initialization. |
| 122 | * |
| 123 | * \return ARM_DRIVER_OK if the de-initialization succeeds |
| 124 | */ |
| 125 | __WEAK int32_t tfm_ns_platform_uninit(void) |
| 126 | { |
| 127 | stdio_uninit(); |
| 128 | |
| 129 | return ARM_DRIVER_OK; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * \brief main() function |
| 134 | */ |
| 135 | #ifndef __GNUC__ |
| 136 | __attribute__((noreturn)) |
| 137 | #endif |
| 138 | int main(void) |
| 139 | { |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 140 | if (tfm_ns_platform_init() != ARM_DRIVER_OK) { |
| 141 | /* Avoid undefined behavior if platform init failed */ |
| 142 | while(1); |
| 143 | } |
| 144 | |
David Hu | 4ae00fe | 2021-01-27 17:48:07 +0800 | [diff] [blame] | 145 | (void) osKernelInitialize(); |
| 146 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 147 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 148 | tfm_ns_multi_core_boot(); |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 149 | #else |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 150 | /* Initialize the TFM NS interface */ |
| 151 | tfm_ns_interface_init(); |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 152 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 153 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 154 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 155 | (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr); |
| 156 | #endif |
| 157 | |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 158 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 159 | thread_func = test_app; |
| 160 | #elif defined(PSA_API_TEST_NS) |
| 161 | thread_func = psa_api_test; |
| 162 | #endif |
| 163 | |
Raef Coles | 5ee45ed | 2020-09-24 11:25:44 +0100 | [diff] [blame] | 164 | #if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \ |
| 165 | || defined(PSA_API_TEST_NS) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 166 | (void) osThreadNew(thread_func, NULL, &thread_attr); |
| 167 | #endif |
| 168 | |
| 169 | LOG_MSG("Non-Secure system starting...\r\n"); |
| 170 | (void) osKernelStart(); |
| 171 | |
| 172 | /* Reached only in case of error */ |
| 173 | for (;;) { |
| 174 | } |
| 175 | } |