blob: e153572a0cd4ae76ee7ec3018fc0fa6a00ac2d5b [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
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
74/**
75 * \brief Static globals to hold RTOS related quantities,
76 * main thread
77 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010078#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
79 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080080static 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
Raef Coles5ee45ed2020-09-24 11:25:44 +0100166#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800167 thread_func = test_app;
168#elif defined(PSA_API_TEST_NS)
169 thread_func = psa_api_test;
170#endif
171
Raef Coles5ee45ed2020-09-24 11:25:44 +0100172#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
173 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800174 (void) osThreadNew(thread_func, NULL, &thread_attr);
175#endif
176
177 LOG_MSG("Non-Secure system starting...\r\n");
178 (void) osKernelStart();
179
180 /* Reached only in case of error */
181 for (;;) {
182 }
183}