blob: 2df4ffd6e3a2da2cc35f84104435276804adc3b6 [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
David Hu98adf322020-09-01 16:18:46 +08002 * Copyright (c) 2017-2021, 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"
Raef Coles5ee45ed2020-09-24 11:25:44 +010012#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
David Huacba69e2021-09-10 15:36:48 +080013#include "tfm_integ_test.h"
Kevin Peng62a87112020-07-07 15:07:46 +080014#endif
15#ifdef PSA_API_TEST_NS
16#include "psa_api_test.h"
17#endif
Kevin Peng62a87112020-07-07 15:07:46 +080018#include "tfm_plat_ns.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010019#include "driver/Driver_USART.h"
Kevin Peng62a87112020-07-07 15:07:46 +080020#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 Qin77894232020-08-28 11:24:15 +080025#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080026#include "uart_stdout.h"
Kevin Peng62a87112020-07-07 15:07:46 +080027
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 Huacba69e2021-09-10 15:36:48 +080034#if defined(__ARMCC_VERSION)
35#if (__ARMCC_VERSION == 6110004)
Kevin Peng62a87112020-07-07 15:07:46 +080036/* 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 Huacba69e2021-09-10 15:36:48 +080041
42/* Avoids the semihosting issue */
43#if (__ARMCC_VERSION >= 6010050)
44__asm(" .global __ARM_use_no_argv\n");
45#endif
46#endif
47
Kevin Peng62a87112020-07-07 15:07:46 +080048/**
49 * \brief List of RTOS thread attributes
50 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010051#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
52 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080053static const osThreadAttr_t thread_attr = {
54 .name = "test_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010055 .stack_size = 4096U
Kevin Peng62a87112020-07-07 15:07:46 +080056};
57#endif
58
David Hu98adf322020-09-01 16:18:46 +080059#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
60static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080061static const osThreadAttr_t mailbox_thread_attr = {
62 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010063 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080064};
65#endif
66
Kevin Peng62a87112020-07-07 15:07:46 +080067/**
68 * \brief Static globals to hold RTOS related quantities,
69 * main thread
70 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010071#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
72 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080073static osThreadFunc_t thread_func;
74#endif
75
76#ifdef TFM_MULTI_CORE_TOPOLOGY
77static struct ns_mailbox_queue_t ns_mailbox_queue;
78
79static 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 Hucdc51fb2021-04-06 18:10:46 +0800102#else
103extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +0800104#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
138int main(void)
139{
Kevin Peng62a87112020-07-07 15:07:46 +0800140 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
141 /* Avoid undefined behavior if platform init failed */
142 while(1);
143 }
144
David Hu4ae00fe2021-01-27 17:48:07 +0800145 (void) osKernelInitialize();
146
Kevin Peng62a87112020-07-07 15:07:46 +0800147#ifdef TFM_MULTI_CORE_TOPOLOGY
148 tfm_ns_multi_core_boot();
David Hucdc51fb2021-04-06 18:10:46 +0800149#else
Kevin Peng62a87112020-07-07 15:07:46 +0800150 /* Initialize the TFM NS interface */
151 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800152#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800153
David Hu98adf322020-09-01 16:18:46 +0800154#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
155 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
156#endif
157
Raef Coles5ee45ed2020-09-24 11:25:44 +0100158#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800159 thread_func = test_app;
160#elif defined(PSA_API_TEST_NS)
161 thread_func = psa_api_test;
162#endif
163
Raef Coles5ee45ed2020-09-24 11:25:44 +0100164#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
165 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800166 (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}