blob: b65d5cd160de73ad45e9bb035500e62443a31c14 [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"
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
19#include "target_cfg.h"
20#include "tfm_plat_ns.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010021#include "driver/Driver_USART.h"
Kevin Peng62a87112020-07-07 15:07:46 +080022#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"
Summer Qin77894232020-08-28 11:24:15 +080028#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080029#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 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010066#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
67 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080068static uint64_t test_app_stack[(4u * 1024u) / (sizeof(uint64_t))]; /* 4KB */
69static const osThreadAttr_t thread_attr = {
70 .name = "test_thread",
71 .stack_mem = test_app_stack,
72 .stack_size = sizeof(test_app_stack),
73};
74#endif
75
76/**
77 * \brief Static globals to hold RTOS related quantities,
78 * main thread
79 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010080#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
81 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080082static osThreadFunc_t thread_func;
83#endif
84
85#ifdef TFM_MULTI_CORE_TOPOLOGY
86static struct ns_mailbox_queue_t ns_mailbox_queue;
87
88static void tfm_ns_multi_core_boot(void)
89{
90 int32_t ret;
91
92 LOG_MSG("Non-secure code running on non-secure core.");
93
94 if (tfm_ns_wait_for_s_cpu_ready()) {
95 LOG_MSG("Error sync'ing with secure core.");
96
97 /* Avoid undefined behavior after multi-core sync-up failed */
98 for (;;) {
99 }
100 }
101
102 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
103 if (ret != MAILBOX_SUCCESS) {
104 LOG_MSG("Non-secure mailbox initialization failed.");
105
106 /* Avoid undefined behavior after NS mailbox initialization failed */
107 for (;;) {
108 }
109 }
110}
111#endif
112
113/**
114 * \brief Platform peripherals and devices initialization.
115 * Can be overridden for platform specific initialization.
116 *
117 * \return ARM_DRIVER_OK if the initialization succeeds
118 */
119__WEAK int32_t tfm_ns_platform_init(void)
120{
121 stdio_init();
122
123 return ARM_DRIVER_OK;
124}
125
126/**
127 * \brief Platform peripherals and devices de-initialization.
128 * Can be overridden for platform specific initialization.
129 *
130 * \return ARM_DRIVER_OK if the de-initialization succeeds
131 */
132__WEAK int32_t tfm_ns_platform_uninit(void)
133{
134 stdio_uninit();
135
136 return ARM_DRIVER_OK;
137}
138
139/**
140 * \brief main() function
141 */
142#ifndef __GNUC__
143__attribute__((noreturn))
144#endif
145int main(void)
146{
147#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
148 /* Set Main Stack Pointer limit */
149 REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
150 __set_MSPLIM((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
151 $$ZI$$Base));
152#endif
153
154 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
155 /* Avoid undefined behavior if platform init failed */
156 while(1);
157 }
158
159#ifdef TFM_MULTI_CORE_TOPOLOGY
160 tfm_ns_multi_core_boot();
161#endif
162
163 (void) osKernelInitialize();
164
165 /* Initialize the TFM NS interface */
166 tfm_ns_interface_init();
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}