blob: f4fe3dd7d7606b6a4aeaf888a6f79ca541c057dd [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"
Kevin Peng62a87112020-07-07 15:07:46 +080010#include "tfm_ns_svc.h"
11#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 +080048extern 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 Coles5ee45ed2020-09-24 11:25:44 +010070#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
71 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080072static const osThreadAttr_t thread_attr = {
73 .name = "test_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010074 .stack_size = 4096U
Kevin Peng62a87112020-07-07 15:07:46 +080075};
76#endif
77
David Hu98adf322020-09-01 16:18:46 +080078#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
79static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080080static const osThreadAttr_t mailbox_thread_attr = {
81 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010082 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080083};
84#endif
85
Kevin Peng62a87112020-07-07 15:07:46 +080086/**
87 * \brief Static globals to hold RTOS related quantities,
88 * main thread
89 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010090#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
91 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080092static osThreadFunc_t thread_func;
93#endif
94
95#ifdef TFM_MULTI_CORE_TOPOLOGY
96static struct ns_mailbox_queue_t ns_mailbox_queue;
97
98static 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 Hucdc51fb2021-04-06 18:10:46 +0800121#else
122extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +0800123#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
157int main(void)
158{
Kevin Peng62a87112020-07-07 15:07:46 +0800159 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
160 /* Avoid undefined behavior if platform init failed */
161 while(1);
162 }
163
David Hu4ae00fe2021-01-27 17:48:07 +0800164 (void) osKernelInitialize();
165
Kevin Peng62a87112020-07-07 15:07:46 +0800166#ifdef TFM_MULTI_CORE_TOPOLOGY
167 tfm_ns_multi_core_boot();
David Hucdc51fb2021-04-06 18:10:46 +0800168#else
Kevin Peng62a87112020-07-07 15:07:46 +0800169 /* Initialize the TFM NS interface */
170 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800171#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800172
David Hu98adf322020-09-01 16:18:46 +0800173#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
174 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
175#endif
176
Raef Coles5ee45ed2020-09-24 11:25:44 +0100177#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800178 thread_func = test_app;
179#elif defined(PSA_API_TEST_NS)
180 thread_func = psa_api_test;
181#endif
182
Raef Coles5ee45ed2020-09-24 11:25:44 +0100183#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
184 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800185 (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}