blob: 5c424c339fcdeeae836c47e5110e52e74d8e5794 [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 uint64_t test_app_stack[(4u * 1024u) / (sizeof(uint64_t))]; /* 4KB */
66static const osThreadAttr_t thread_attr = {
67 .name = "test_thread",
68 .stack_mem = test_app_stack,
69 .stack_size = sizeof(test_app_stack),
70};
71#endif
72
David Hu98adf322020-09-01 16:18:46 +080073#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
74static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
75/* 1KB stack */
76#define MAILBOX_THREAD_STACK_SIZE (1u * 1024u)
77static uint64_t mailbox_thread_stack[MAILBOX_THREAD_STACK_SIZE /
78 sizeof(uint64_t)];
79static const osThreadAttr_t mailbox_thread_attr = {
80 .name = "mailbox_thread",
81 .stack_mem = mailbox_thread_stack,
82 .stack_size = sizeof(mailbox_thread_stack),
83};
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}
121#endif
122
123/**
124 * \brief Platform peripherals and devices initialization.
125 * Can be overridden for platform specific initialization.
126 *
127 * \return ARM_DRIVER_OK if the initialization succeeds
128 */
129__WEAK int32_t tfm_ns_platform_init(void)
130{
131 stdio_init();
132
133 return ARM_DRIVER_OK;
134}
135
136/**
137 * \brief Platform peripherals and devices de-initialization.
138 * Can be overridden for platform specific initialization.
139 *
140 * \return ARM_DRIVER_OK if the de-initialization succeeds
141 */
142__WEAK int32_t tfm_ns_platform_uninit(void)
143{
144 stdio_uninit();
145
146 return ARM_DRIVER_OK;
147}
148
149/**
150 * \brief main() function
151 */
152#ifndef __GNUC__
153__attribute__((noreturn))
154#endif
155int main(void)
156{
Kevin Peng62a87112020-07-07 15:07:46 +0800157 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
158 /* Avoid undefined behavior if platform init failed */
159 while(1);
160 }
161
David Hu4ae00fe2021-01-27 17:48:07 +0800162 (void) osKernelInitialize();
163
Kevin Peng62a87112020-07-07 15:07:46 +0800164#ifdef TFM_MULTI_CORE_TOPOLOGY
165 tfm_ns_multi_core_boot();
166#endif
167
Kevin Peng62a87112020-07-07 15:07:46 +0800168 /* Initialize the TFM NS interface */
169 tfm_ns_interface_init();
170
David Hu98adf322020-09-01 16:18:46 +0800171#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
172 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
173#endif
174
Raef Coles5ee45ed2020-09-24 11:25:44 +0100175#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800176 thread_func = test_app;
177#elif defined(PSA_API_TEST_NS)
178 thread_func = psa_api_test;
179#endif
180
Raef Coles5ee45ed2020-09-24 11:25:44 +0100181#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
182 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800183 (void) osThreadNew(thread_func, NULL, &thread_attr);
184#endif
185
186 LOG_MSG("Non-Secure system starting...\r\n");
187 (void) osKernelStart();
188
189 /* Reached only in case of error */
190 for (;;) {
191 }
192}