blob: e9ab48d8dd21f9da35333fb300d4a9d05206daef [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
Feder Liang7abe9a42021-12-03 17:54:58 +08002 * Copyright (c) 2017-2022, Arm Limited. All rights reserved.
Kevin Peng62a87112020-07-07 15:07:46 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "tfm_api.h"
9#include "cmsis_os2.h"
Xinyu Zhang4c640e82021-09-22 15:25:09 +080010#include "cmsis_compiler.h"
Kevin Peng62a87112020-07-07 15:07:46 +080011#include "tfm_ns_interface.h"
Xinyu Zhangeebbea32021-09-01 15:26:39 +080012#include "tfm_nsid_manager.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010013#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
David Huacba69e2021-09-10 15:36:48 +080014#include "tfm_integ_test.h"
Kevin Peng62a87112020-07-07 15:07:46 +080015#endif
16#ifdef PSA_API_TEST_NS
17#include "psa_api_test.h"
18#endif
Kevin Peng62a87112020-07-07 15:07:46 +080019#include "tfm_plat_ns.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010020#include "driver/Driver_USART.h"
Kevin Peng62a87112020-07-07 15:07:46 +080021#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 Qin77894232020-08-28 11:24:15 +080026#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080027#include "uart_stdout.h"
Feder Liang7abe9a42021-12-03 17:54:58 +080028#if (CONFIG_TFM_FP >= 1)
29#include "cmsis.h"
30#endif
Kevin Peng62a87112020-07-07 15:07:46 +080031
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 Huacba69e2021-09-10 15:36:48 +080038#if defined(__ARMCC_VERSION)
39#if (__ARMCC_VERSION == 6110004)
Kevin Peng62a87112020-07-07 15:07:46 +080040/* 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 Huacba69e2021-09-10 15:36:48 +080045
46/* Avoids the semihosting issue */
47#if (__ARMCC_VERSION >= 6010050)
48__asm(" .global __ARM_use_no_argv\n");
49#endif
50#endif
51
Kevin Peng62a87112020-07-07 15:07:46 +080052/**
53 * \brief List of RTOS thread attributes
54 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010055#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
56 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080057static const osThreadAttr_t thread_attr = {
58 .name = "test_thread",
Xinyu Zhangeebbea32021-09-01 15:26:39 +080059 .stack_size = 4096U,
60 .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID)
Kevin Peng62a87112020-07-07 15:07:46 +080061};
62#endif
63
David Hu98adf322020-09-01 16:18:46 +080064#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
65static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080066static const osThreadAttr_t mailbox_thread_attr = {
67 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010068 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080069};
70#endif
71
Kevin Peng62a87112020-07-07 15:07:46 +080072/**
73 * \brief Static globals to hold RTOS related quantities,
74 * main thread
75 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010076#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
77 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080078static osThreadFunc_t thread_func;
79#endif
80
81#ifdef TFM_MULTI_CORE_TOPOLOGY
82static struct ns_mailbox_queue_t ns_mailbox_queue;
83
84static void tfm_ns_multi_core_boot(void)
85{
86 int32_t ret;
87
Chris Branda1499292021-10-28 12:02:05 -070088 LOG_MSG("Non-secure code running on non-secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080089
90 if (tfm_ns_wait_for_s_cpu_ready()) {
Chris Branda1499292021-10-28 12:02:05 -070091 LOG_MSG("Error sync'ing with secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080092
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 Branda1499292021-10-28 12:02:05 -0700100 LOG_MSG("Non-secure mailbox initialization failed.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +0800101
102 /* Avoid undefined behavior after NS mailbox initialization failed */
103 for (;;) {
104 }
105 }
106}
David Hucdc51fb2021-04-06 18:10:46 +0800107#else
108extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +0800109#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 Liang7abe9a42021-12-03 17:54:58 +0800137
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 Peng62a87112020-07-07 15:07:46 +0800150/**
151 * \brief main() function
152 */
153#ifndef __GNUC__
154__attribute__((noreturn))
155#endif
156int main(void)
157{
Kevin Peng62a87112020-07-07 15:07:46 +0800158 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
159 /* Avoid undefined behavior if platform init failed */
160 while(1);
161 }
162
Feder Liang7abe9a42021-12-03 17:54:58 +0800163 if (tfm_ns_cp_init() != ARM_DRIVER_OK) {
164 /* Avoid undefined behavior if co-porcessor init failed */
165 while(1);
166 }
167
David Hu4ae00fe2021-01-27 17:48:07 +0800168 (void) osKernelInitialize();
169
Kevin Peng62a87112020-07-07 15:07:46 +0800170#ifdef TFM_MULTI_CORE_TOPOLOGY
171 tfm_ns_multi_core_boot();
David Hucdc51fb2021-04-06 18:10:46 +0800172#else
Kevin Peng62a87112020-07-07 15:07:46 +0800173 /* Initialize the TFM NS interface */
174 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800175#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800176
David Hu98adf322020-09-01 16:18:46 +0800177#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
178 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
179#endif
180
Raef Coles5ee45ed2020-09-24 11:25:44 +0100181#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800182 thread_func = test_app;
183#elif defined(PSA_API_TEST_NS)
184 thread_func = psa_api_test;
185#endif
186
Raef Coles5ee45ed2020-09-24 11:25:44 +0100187#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
188 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800189 (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}