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