blob: 33a70dbe3d6412e52811fb33d3dd60eced5cb3d3 [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"
Xinyu Zhang4c640e82021-09-22 15:25:09 +080010#include "cmsis_compiler.h"
Kevin Peng62a87112020-07-07 15:07:46 +080011#include "tfm_ns_interface.h"
Xinyu Zhangeebbea32021-09-01 15:26:39 +080012#include "tfm_nsid_manager.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010013#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
David Huacba69e2021-09-10 15:36:48 +080014#include "tfm_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"
Kevin Peng62a87112020-07-07 15:07:46 +080028
29/**
30 * \brief Modified table template for user defined SVC functions
31 *
32 * \details RTX has a weak definition of osRtxUserSVC, which
33 * is overridden here
34 */
David Huacba69e2021-09-10 15:36:48 +080035#if defined(__ARMCC_VERSION)
36#if (__ARMCC_VERSION == 6110004)
Kevin Peng62a87112020-07-07 15:07:46 +080037/* 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
David Huacba69e2021-09-10 15:36:48 +080042
43/* Avoids the semihosting issue */
44#if (__ARMCC_VERSION >= 6010050)
45__asm(" .global __ARM_use_no_argv\n");
46#endif
47#endif
48
Kevin Peng62a87112020-07-07 15:07:46 +080049/**
50 * \brief List of RTOS thread attributes
51 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010052#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
53 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080054static const osThreadAttr_t thread_attr = {
55 .name = "test_thread",
Xinyu Zhangeebbea32021-09-01 15:26:39 +080056 .stack_size = 4096U,
57 .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID)
Kevin Peng62a87112020-07-07 15:07:46 +080058};
59#endif
60
David Hu98adf322020-09-01 16:18:46 +080061#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
62static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080063static const osThreadAttr_t mailbox_thread_attr = {
64 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010065 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080066};
67#endif
68
Kevin Peng62a87112020-07-07 15:07:46 +080069/**
70 * \brief Static globals to hold RTOS related quantities,
71 * main thread
72 */
Raef Coles5ee45ed2020-09-24 11:25:44 +010073#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
74 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +080075static osThreadFunc_t thread_func;
76#endif
77
78#ifdef TFM_MULTI_CORE_TOPOLOGY
79static struct ns_mailbox_queue_t ns_mailbox_queue;
80
81static void tfm_ns_multi_core_boot(void)
82{
83 int32_t ret;
84
85 LOG_MSG("Non-secure code running on non-secure core.");
86
87 if (tfm_ns_wait_for_s_cpu_ready()) {
88 LOG_MSG("Error sync'ing with secure core.");
89
90 /* Avoid undefined behavior after multi-core sync-up failed */
91 for (;;) {
92 }
93 }
94
95 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
96 if (ret != MAILBOX_SUCCESS) {
97 LOG_MSG("Non-secure mailbox initialization failed.");
98
99 /* Avoid undefined behavior after NS mailbox initialization failed */
100 for (;;) {
101 }
102 }
103}
David Hucdc51fb2021-04-06 18:10:46 +0800104#else
105extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +0800106#endif
107
108/**
109 * \brief Platform peripherals and devices initialization.
110 * Can be overridden for platform specific initialization.
111 *
112 * \return ARM_DRIVER_OK if the initialization succeeds
113 */
114__WEAK int32_t tfm_ns_platform_init(void)
115{
116 stdio_init();
117
118 return ARM_DRIVER_OK;
119}
120
121/**
122 * \brief Platform peripherals and devices de-initialization.
123 * Can be overridden for platform specific initialization.
124 *
125 * \return ARM_DRIVER_OK if the de-initialization succeeds
126 */
127__WEAK int32_t tfm_ns_platform_uninit(void)
128{
129 stdio_uninit();
130
131 return ARM_DRIVER_OK;
132}
133
134/**
135 * \brief main() function
136 */
137#ifndef __GNUC__
138__attribute__((noreturn))
139#endif
140int main(void)
141{
Kevin Peng62a87112020-07-07 15:07:46 +0800142 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
143 /* Avoid undefined behavior if platform init failed */
144 while(1);
145 }
146
David Hu4ae00fe2021-01-27 17:48:07 +0800147 (void) osKernelInitialize();
148
Kevin Peng62a87112020-07-07 15:07:46 +0800149#ifdef TFM_MULTI_CORE_TOPOLOGY
150 tfm_ns_multi_core_boot();
David Hucdc51fb2021-04-06 18:10:46 +0800151#else
Kevin Peng62a87112020-07-07 15:07:46 +0800152 /* Initialize the TFM NS interface */
153 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800154#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800155
David Hu98adf322020-09-01 16:18:46 +0800156#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
157 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
158#endif
159
Raef Coles5ee45ed2020-09-24 11:25:44 +0100160#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S)
Kevin Peng62a87112020-07-07 15:07:46 +0800161 thread_func = test_app;
162#elif defined(PSA_API_TEST_NS)
163 thread_func = psa_api_test;
164#endif
165
Raef Coles5ee45ed2020-09-24 11:25:44 +0100166#if defined(TEST_FRAMEWORK_NS) || defined(TEST_FRAMEWORK_S) \
167 || defined(PSA_API_TEST_NS)
Kevin Peng62a87112020-07-07 15:07:46 +0800168 (void) osThreadNew(thread_func, NULL, &thread_attr);
169#endif
170
171 LOG_MSG("Non-Secure system starting...\r\n");
172 (void) osKernelStart();
173
174 /* Reached only in case of error */
175 for (;;) {
176 }
177}