blob: aaaead9a938e3f15347da5e1424e41847b39643f [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"
10#include "tfm_integ_test.h"
11#include "tfm_ns_svc.h"
12#include "tfm_ns_interface.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010013#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
14#include "test_framework_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"
Kevin Peng62a87112020-07-07 15:07:46 +080028
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 */
35#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION == 6110004))
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
41extern void * const osRtxUserSVC[1+USER_SVC_COUNT];
42 void * const osRtxUserSVC[1+USER_SVC_COUNT] = {
43 (void *)USER_SVC_COUNT,
44
45#define X(SVC_ENUM, SVC_HANDLER) (void*)SVC_HANDLER,
46
47 /* SVC API for Services */
48#ifdef TFM_NS_CLIENT_IDENTIFICATION
49 LIST_SVC_NSPM
50#endif
51
52#undef X
53
54/*
55 * (void *)user_function1,
56 * ...
57 */
58};
59
60/**
61 * \brief List of RTOS thread attributes
62 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010063#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
64 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080065static const osThreadAttr_t thread_attr = {
66 .name = "test_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010067 .stack_size = 4096U
Kevin Peng62a87112020-07-07 15:07:46 +080068};
69#endif
70
David Hu98adf322020-09-01 16:18:46 +080071#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
72static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080073static const osThreadAttr_t mailbox_thread_attr = {
74 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010075 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080076};
77#endif
78
Kevin Peng62a87112020-07-07 15:07:46 +080079/**
80 * \brief Static globals to hold RTOS related quantities,
81 * main thread
82 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010083#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
84 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080085static osThreadFunc_t thread_func;
86#endif
87
88#ifdef TFM_MULTI_CORE_TOPOLOGY
89static struct ns_mailbox_queue_t ns_mailbox_queue;
90
91static void tfm_ns_multi_core_boot(void)
92{
93 int32_t ret;
94
95 LOG_MSG("Non-secure code running on non-secure core.");
96
97 if (tfm_ns_wait_for_s_cpu_ready()) {
98 LOG_MSG("Error sync'ing with secure core.");
99
100 /* Avoid undefined behavior after multi-core sync-up failed */
101 for (;;) {
102 }
103 }
104
105 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
106 if (ret != MAILBOX_SUCCESS) {
107 LOG_MSG("Non-secure mailbox initialization failed.");
108
109 /* Avoid undefined behavior after NS mailbox initialization failed */
110 for (;;) {
111 }
112 }
113}
David Hucdc51fb2021-04-06 18:10:46 +0800114#else
115extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +0800116#endif
117
118/**
119 * \brief Platform peripherals and devices initialization.
120 * Can be overridden for platform specific initialization.
121 *
122 * \return ARM_DRIVER_OK if the initialization succeeds
123 */
124__WEAK int32_t tfm_ns_platform_init(void)
125{
126 stdio_init();
127
128 return ARM_DRIVER_OK;
129}
130
131/**
132 * \brief Platform peripherals and devices de-initialization.
133 * Can be overridden for platform specific initialization.
134 *
135 * \return ARM_DRIVER_OK if the de-initialization succeeds
136 */
137__WEAK int32_t tfm_ns_platform_uninit(void)
138{
139 stdio_uninit();
140
141 return ARM_DRIVER_OK;
142}
143
144/**
145 * \brief main() function
146 */
147#ifndef __GNUC__
148__attribute__((noreturn))
149#endif
150int main(void)
151{
Kevin Peng62a87112020-07-07 15:07:46 +0800152 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
153 /* Avoid undefined behavior if platform init failed */
154 while(1);
155 }
156
David Hu4ae00fe2021-01-27 17:48:07 +0800157 (void) osKernelInitialize();
158
Kevin Peng62a87112020-07-07 15:07:46 +0800159#ifdef TFM_MULTI_CORE_TOPOLOGY
160 tfm_ns_multi_core_boot();
David Hucdc51fb2021-04-06 18:10:46 +0800161#else
Kevin Peng62a87112020-07-07 15:07:46 +0800162 /* Initialize the TFM NS interface */
163 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800164#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800165
David Hu98adf322020-09-01 16:18:46 +0800166#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
167 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
168#endif
169
Raef Coles5ee45ed2020-09-24 11:25:44 +0100170#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800171 thread_func = test_app;
172#elif defined(PSA_API_TEST_NS)
173 thread_func = psa_api_test;
174#endif
175
Raef Coles5ee45ed2020-09-24 11:25:44 +0100176#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
177 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800178 (void) osThreadNew(thread_func, NULL, &thread_attr);
179#endif
180
181 LOG_MSG("Non-Secure system starting...\r\n");
182 (void) osKernelStart();
183
184 /* Reached only in case of error */
185 for (;;) {
186 }
187}