blob: 70e54ed221a25ba33fa8e712b925314d7657e618 [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"
28#include "region.h"
29
30/**
31 * \brief Modified table template for user defined SVC functions
32 *
33 * \details RTX has a weak definition of osRtxUserSVC, which
34 * is overridden here
35 */
36#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION == 6110004))
37/* Workaround needed for a bug in Armclang 6.11, more details at:
38 * http://www.keil.com/support/docs/4089.htm
39 */
40__attribute__((section(".gnu.linkonce")))
41#endif
42extern void * const osRtxUserSVC[1+USER_SVC_COUNT];
43 void * const osRtxUserSVC[1+USER_SVC_COUNT] = {
44 (void *)USER_SVC_COUNT,
45
46#define X(SVC_ENUM, SVC_HANDLER) (void*)SVC_HANDLER,
47
48 /* SVC API for Services */
49#ifdef TFM_NS_CLIENT_IDENTIFICATION
50 LIST_SVC_NSPM
51#endif
52
53#undef X
54
55/*
56 * (void *)user_function1,
57 * ...
58 */
59};
60
61/**
62 * \brief List of RTOS thread attributes
63 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010064#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
65 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080066static uint64_t test_app_stack[(4u * 1024u) / (sizeof(uint64_t))]; /* 4KB */
67static const osThreadAttr_t thread_attr = {
68 .name = "test_thread",
69 .stack_mem = test_app_stack,
70 .stack_size = sizeof(test_app_stack),
71};
72#endif
73
David Hu98adf322020-09-01 16:18:46 +080074#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
75static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
76/* 1KB stack */
77#define MAILBOX_THREAD_STACK_SIZE (1u * 1024u)
78static uint64_t mailbox_thread_stack[MAILBOX_THREAD_STACK_SIZE /
79 sizeof(uint64_t)];
80static const osThreadAttr_t mailbox_thread_attr = {
81 .name = "mailbox_thread",
82 .stack_mem = mailbox_thread_stack,
83 .stack_size = sizeof(mailbox_thread_stack),
84};
85#endif
86
Kevin Peng62a87112020-07-07 15:07:46 +080087/**
88 * \brief Static globals to hold RTOS related quantities,
89 * main thread
90 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010091#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
92 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080093static osThreadFunc_t thread_func;
94#endif
95
96#ifdef TFM_MULTI_CORE_TOPOLOGY
97static struct ns_mailbox_queue_t ns_mailbox_queue;
98
99static void tfm_ns_multi_core_boot(void)
100{
101 int32_t ret;
102
103 LOG_MSG("Non-secure code running on non-secure core.");
104
105 if (tfm_ns_wait_for_s_cpu_ready()) {
106 LOG_MSG("Error sync'ing with secure core.");
107
108 /* Avoid undefined behavior after multi-core sync-up failed */
109 for (;;) {
110 }
111 }
112
113 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
114 if (ret != MAILBOX_SUCCESS) {
115 LOG_MSG("Non-secure mailbox initialization failed.");
116
117 /* Avoid undefined behavior after NS mailbox initialization failed */
118 for (;;) {
119 }
120 }
121}
122#endif
123
124/**
125 * \brief Platform peripherals and devices initialization.
126 * Can be overridden for platform specific initialization.
127 *
128 * \return ARM_DRIVER_OK if the initialization succeeds
129 */
130__WEAK int32_t tfm_ns_platform_init(void)
131{
132 stdio_init();
133
134 return ARM_DRIVER_OK;
135}
136
137/**
138 * \brief Platform peripherals and devices de-initialization.
139 * Can be overridden for platform specific initialization.
140 *
141 * \return ARM_DRIVER_OK if the de-initialization succeeds
142 */
143__WEAK int32_t tfm_ns_platform_uninit(void)
144{
145 stdio_uninit();
146
147 return ARM_DRIVER_OK;
148}
149
150/**
151 * \brief main() function
152 */
153#ifndef __GNUC__
154__attribute__((noreturn))
155#endif
156int main(void)
157{
158#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
159 /* Set Main Stack Pointer limit */
160 REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
161 __set_MSPLIM((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
162 $$ZI$$Base));
163#endif
164
165 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
166 /* Avoid undefined behavior if platform init failed */
167 while(1);
168 }
169
David Hu4ae00fe2021-01-27 17:48:07 +0800170 (void) osKernelInitialize();
171
Kevin Peng62a87112020-07-07 15:07:46 +0800172#ifdef TFM_MULTI_CORE_TOPOLOGY
173 tfm_ns_multi_core_boot();
174#endif
175
Kevin Peng62a87112020-07-07 15:07:46 +0800176 /* Initialize the TFM NS interface */
177 tfm_ns_interface_init();
178
David Hu98adf322020-09-01 16:18:46 +0800179#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
180 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
181#endif
182
Raef Coles5ee45ed2020-09-24 11:25:44 +0100183#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800184 thread_func = test_app;
185#elif defined(PSA_API_TEST_NS)
186 thread_func = psa_api_test;
187#endif
188
Raef Coles5ee45ed2020-09-24 11:25:44 +0100189#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
190 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800191 (void) osThreadNew(thread_func, NULL, &thread_attr);
192#endif
193
194 LOG_MSG("Non-Secure system starting...\r\n");
195 (void) osKernelStart();
196
197 /* Reached only in case of error */
198 for (;;) {
199 }
200}