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