blob: b3a027e316926980f5ff0b95aad2dc4b10edcda8 [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}
114#endif
115
116/**
117 * \brief Platform peripherals and devices initialization.
118 * Can be overridden for platform specific initialization.
119 *
120 * \return ARM_DRIVER_OK if the initialization succeeds
121 */
122__WEAK int32_t tfm_ns_platform_init(void)
123{
124 stdio_init();
125
126 return ARM_DRIVER_OK;
127}
128
129/**
130 * \brief Platform peripherals and devices de-initialization.
131 * Can be overridden for platform specific initialization.
132 *
133 * \return ARM_DRIVER_OK if the de-initialization succeeds
134 */
135__WEAK int32_t tfm_ns_platform_uninit(void)
136{
137 stdio_uninit();
138
139 return ARM_DRIVER_OK;
140}
141
142/**
143 * \brief main() function
144 */
145#ifndef __GNUC__
146__attribute__((noreturn))
147#endif
148int main(void)
149{
Kevin Peng62a87112020-07-07 15:07:46 +0800150 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
151 /* Avoid undefined behavior if platform init failed */
152 while(1);
153 }
154
David Hu4ae00fe2021-01-27 17:48:07 +0800155 (void) osKernelInitialize();
156
Kevin Peng62a87112020-07-07 15:07:46 +0800157#ifdef TFM_MULTI_CORE_TOPOLOGY
158 tfm_ns_multi_core_boot();
159#endif
160
Kevin Peng62a87112020-07-07 15:07:46 +0800161 /* Initialize the TFM NS interface */
162 tfm_ns_interface_init();
163
David Hu98adf322020-09-01 16:18:46 +0800164#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
165 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
166#endif
167
Raef Coles5ee45ed2020-09-24 11:25:44 +0100168#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800169 thread_func = test_app;
170#elif defined(PSA_API_TEST_NS)
171 thread_func = psa_api_test;
172#endif
173
Raef Coles5ee45ed2020-09-24 11:25:44 +0100174#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
175 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800176 (void) osThreadNew(thread_func, NULL, &thread_attr);
177#endif
178
179 LOG_MSG("Non-Secure system starting...\r\n");
180 (void) osKernelStart();
181
182 /* Reached only in case of error */
183 for (;;) {
184 }
185}