blob: 7ad6680e39a1fead9f8d86ee43a9148fc8997f72 [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
2 * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
3 *
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"
13#ifdef TEST_FRAMEWORK_NS
14#include "test/framework/test_framework_integ_test.h"
15#endif
16#ifdef PSA_API_TEST_NS
17#include "psa_api_test.h"
18#endif
19#include "target_cfg.h"
20#include "tfm_plat_ns.h"
21#include "Driver_USART.h"
22#include "device_cfg.h"
23#ifdef TFM_MULTI_CORE_TOPOLOGY
24#include "tfm_multi_core_api.h"
25#include "tfm_ns_mailbox.h"
26#endif
Summer Qin77894232020-08-28 11:24:15 +080027#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080028#include "uart_stdout.h"
29#include "region.h"
30
31/**
32 * \brief Modified table template for user defined SVC functions
33 *
34 * \details RTX has a weak definition of osRtxUserSVC, which
35 * is overridden here
36 */
37#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION == 6110004))
38/* Workaround needed for a bug in Armclang 6.11, more details at:
39 * http://www.keil.com/support/docs/4089.htm
40 */
41__attribute__((section(".gnu.linkonce")))
42#endif
43extern void * const osRtxUserSVC[1+USER_SVC_COUNT];
44 void * const osRtxUserSVC[1+USER_SVC_COUNT] = {
45 (void *)USER_SVC_COUNT,
46
47#define X(SVC_ENUM, SVC_HANDLER) (void*)SVC_HANDLER,
48
49 /* SVC API for Services */
50#ifdef TFM_NS_CLIENT_IDENTIFICATION
51 LIST_SVC_NSPM
52#endif
53
54#undef X
55
56/*
57 * (void *)user_function1,
58 * ...
59 */
60};
61
62/**
63 * \brief List of RTOS thread attributes
64 */
65#if defined(TEST_FRAMEWORK_NS) || defined(PSA_API_TEST_NS)
66static 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
74/**
75 * \brief Static globals to hold RTOS related quantities,
76 * main thread
77 */
78#if defined(TEST_FRAMEWORK_NS) || defined(PSA_API_TEST_NS)
79static osThreadFunc_t thread_func;
80#endif
81
82#ifdef TFM_MULTI_CORE_TOPOLOGY
83static struct ns_mailbox_queue_t ns_mailbox_queue;
84
85static void tfm_ns_multi_core_boot(void)
86{
87 int32_t ret;
88
89 LOG_MSG("Non-secure code running on non-secure core.");
90
91 if (tfm_ns_wait_for_s_cpu_ready()) {
92 LOG_MSG("Error sync'ing with secure core.");
93
94 /* Avoid undefined behavior after multi-core sync-up failed */
95 for (;;) {
96 }
97 }
98
99 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
100 if (ret != MAILBOX_SUCCESS) {
101 LOG_MSG("Non-secure mailbox initialization failed.");
102
103 /* Avoid undefined behavior after NS mailbox initialization failed */
104 for (;;) {
105 }
106 }
107}
108#endif
109
110/**
111 * \brief Platform peripherals and devices initialization.
112 * Can be overridden for platform specific initialization.
113 *
114 * \return ARM_DRIVER_OK if the initialization succeeds
115 */
116__WEAK int32_t tfm_ns_platform_init(void)
117{
118 stdio_init();
119
120 return ARM_DRIVER_OK;
121}
122
123/**
124 * \brief Platform peripherals and devices de-initialization.
125 * Can be overridden for platform specific initialization.
126 *
127 * \return ARM_DRIVER_OK if the de-initialization succeeds
128 */
129__WEAK int32_t tfm_ns_platform_uninit(void)
130{
131 stdio_uninit();
132
133 return ARM_DRIVER_OK;
134}
135
136/**
137 * \brief main() function
138 */
139#ifndef __GNUC__
140__attribute__((noreturn))
141#endif
142int main(void)
143{
144#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
145 /* Set Main Stack Pointer limit */
146 REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
147 __set_MSPLIM((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
148 $$ZI$$Base));
149#endif
150
151 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
152 /* Avoid undefined behavior if platform init failed */
153 while(1);
154 }
155
156#ifdef TFM_MULTI_CORE_TOPOLOGY
157 tfm_ns_multi_core_boot();
158#endif
159
160 (void) osKernelInitialize();
161
162 /* Initialize the TFM NS interface */
163 tfm_ns_interface_init();
164
165#if defined(TEST_FRAMEWORK_NS)
166 thread_func = test_app;
167#elif defined(PSA_API_TEST_NS)
168 thread_func = psa_api_test;
169#endif
170
171#if defined(TEST_FRAMEWORK_NS) || defined(PSA_API_TEST_NS)
172 (void) osThreadNew(thread_func, NULL, &thread_attr);
173#endif
174
175 LOG_MSG("Non-Secure system starting...\r\n");
176 (void) osKernelStart();
177
178 /* Reached only in case of error */
179 for (;;) {
180 }
181}