blob: ff7e13b7a934cc02f9869dfe5c337095a32c6e53 [file] [log] [blame]
David Hu50711e32019-06-12 18:32:30 +08001/*
Feder Liang55194382021-11-22 16:45:33 +08002 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Chris Brandbe5bec12022-10-18 11:41:59 -07003 * Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon
4 * company) or an affiliate of Cypress Semiconductor Corporation. All rights
5 * reserved.
David Hu50711e32019-06-12 18:32:30 +08006 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 */
10#ifndef __TFM_ARCH_H__
11#define __TFM_ARCH_H__
12
13/* This header file collects the architecture related operations. */
14
Ken Liu1d96c132019-12-31 15:51:30 +080015#include <stddef.h>
David Hu50711e32019-06-12 18:32:30 +080016#include <inttypes.h>
Kevin Pengbc5e5aa2019-10-16 10:55:17 +080017#include "tfm_hal_device_header.h"
David Hu50711e32019-06-12 18:32:30 +080018#include "cmsis_compiler.h"
19
Ronald Cron312be682019-09-23 09:27:33 +020020#if defined(__ARM_ARCH_8_1M_MAIN__) || \
21 defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Hu50711e32019-06-12 18:32:30 +080022#include "tfm_arch_v8m.h"
David Hu40455c92019-07-02 14:31:34 +080023#elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
24 defined(__ARM_ARCH_7EM__)
25#include "tfm_arch_v6m_v7m.h"
David Hu50711e32019-06-12 18:32:30 +080026#else
27#error "Unsupported ARM Architecture."
28#endif
29
Mingyang Sun620c8562021-11-10 11:44:58 +080030#define SCHEDULER_LOCKED 1
31#define SCHEDULER_UNLOCKED 0
32
David Hu50711e32019-06-12 18:32:30 +080033#define XPSR_T32 0x01000000
34
Chris Brandbe5bec12022-10-18 11:41:59 -070035/* The lowest secure interrupt priority */
36#ifdef CONFIG_TFM_USE_TRUSTZONE
37/* IMPORTANT NOTE:
38 *
39 * Although the priority of the secure PendSV must be the lowest possible
40 * among other interrupts in the Secure state, it must be ensured that
41 * PendSV is not preempted nor masked by Non-Secure interrupts to ensure
42 * the integrity of the Secure operation.
43 * When AIRCR.PRIS is set, the Non-Secure execution can act on
44 * FAULTMASK_NS, PRIMASK_NS or BASEPRI_NS register to boost its priority
45 * number up to the value 0x80.
46 * For this reason, set the priority of the PendSV interrupt to the next
47 * priority level configurable on the platform, just below 0x80.
48 */
49#define PENDSV_PRIO_FOR_SCHED ((1 << (__NVIC_PRIO_BITS - 1)) - 1)
50#else
51/* If TZ is not in use, we have the full priority range available */
52#define PENDSV_PRIO_FOR_SCHED ((1 << __NVIC_PRIO_BITS) - 1)
53#endif
54
Ken Liu5d73c872021-08-19 19:23:17 +080055/* State context defined by architecture */
Ken Liu5a2b9052019-08-15 19:03:29 +080056struct tfm_state_context_t {
David Hu50711e32019-06-12 18:32:30 +080057 uint32_t r0;
58 uint32_t r1;
59 uint32_t r2;
60 uint32_t r3;
61 uint32_t r12;
Ken Liu5a2b9052019-08-15 19:03:29 +080062 uint32_t lr;
David Hu50711e32019-06-12 18:32:30 +080063 uint32_t ra;
64 uint32_t xpsr;
Ken Liu5d73c872021-08-19 19:23:17 +080065};
David Hu50711e32019-06-12 18:32:30 +080066
Ken Liu5d73c872021-08-19 19:23:17 +080067/* Context addition to state context */
68struct tfm_additional_context_t {
69 uint32_t callee[8]; /* R4-R11. NOT ORDERED!! */
70};
71
72/* Full thread context */
73struct full_context_t {
74 struct tfm_additional_context_t addi_ctx;
75 struct tfm_state_context_t stat_ctx;
76};
77
Ken Liuca4580f2022-03-09 21:27:43 +080078/*
79 * Under cross call ABI, SPM can be preempted by interrupts, the interrupt
80 * handling can set SPM API return value and makes the initial SPM API
81 * return code invalid. Use one flag to indicate if the return code has been
82 * force updated by interrupts, then SPM return code can be discarded as it
83 * is out of date.
84 */
85#define CROSS_RETCODE_EMPTY 0xEEEEEEED
86#define CROSS_RETCODE_UPDATED 0xEEEEEEEE
87
Sherry Zhangb24f54d2022-07-04 14:26:07 +080088/* Context control.
89 * CAUTION: Assembly references this structure. DO CHECK the below functions
90 * before changing the structure:
91 'PendSV_Handler'
92 */
Ken Liu5d73c872021-08-19 19:23:17 +080093struct context_ctrl_t {
Sherry Zhangb24f54d2022-07-04 14:26:07 +080094 uint32_t sp; /* Stack pointer (higher address).
95 * THIS MUST BE THE FIRST MEMBER OF
96 * THE STRUCT.
97 */
98 uint32_t exc_ret; /* EXC_RETURN pattern.
99 * THIS MUST BE THE SECOND MEMBER OF
100 * THE STRUCT.
101 */
Ken Liuca4580f2022-03-09 21:27:43 +0800102 uint32_t sp_limit; /* Stack limit (lower address) */
Ken Liu63a176b2022-06-09 22:36:56 +0800103 uint32_t sp_base; /* Stack usage start (higher addr) */
Ken Liuca4580f2022-03-09 21:27:43 +0800104 uint32_t cross_frame; /* Cross call frame position. */
105 uint32_t retcode_status; /* Cross call retcode status. */
Ken Liu5d73c872021-08-19 19:23:17 +0800106};
107
108/*
109 * The context on MSP when de-privileged FLIH Function calls SVC to return.
110 * It is the same when de-privileged FLIH Function is ready to run.
111 */
112struct context_flih_ret_t {
113 uint64_t stack_seal; /* Two words stack seal */
114 struct tfm_additional_context_t addi_ctx;
Ken Liu5d73c872021-08-19 19:23:17 +0800115 uint32_t psp; /* PSP when interrupt exception ocurrs */
Kevin Pengca59ec02021-12-09 14:35:50 +0800116 uint32_t psplim; /* PSPLIM when interrupt exception ocurrs when */
Ken Liu5d73c872021-08-19 19:23:17 +0800117 struct tfm_state_context_t state_ctx; /* ctx on SVC_PREPARE_DEPRIV_FLIH */
118};
David Hu50711e32019-06-12 18:32:30 +0800119
Ken Liuca4580f2022-03-09 21:27:43 +0800120/* A customized ABI format. */
121struct cross_call_abi_frame_t {
122 uint32_t a0;
123 uint32_t a1;
124 uint32_t a2;
125 uint32_t a3;
126 uint32_t unused0;
127 uint32_t unused1;
128};
129
Ken Liubf4681f2022-02-11 11:15:03 +0800130/* Assign stack and stack limit to the context control instance. */
Ken Liu63a176b2022-06-09 22:36:56 +0800131#define ARCH_CTXCTRL_INIT(x, buf, sz) do { \
132 (x)->sp = ((uint32_t)(buf) + (uint32_t)(sz)) & ~0x7; \
133 (x)->sp_limit = ((uint32_t)(buf) + 7) & ~0x7; \
134 (x)->sp_base = (x)->sp; \
135 (x)->exc_ret = 0; \
136 (x)->cross_frame = 0; \
137 (x)->retcode_status = CROSS_RETCODE_EMPTY; \
Ken Liubf4681f2022-02-11 11:15:03 +0800138 } while (0)
139
140/* Allocate 'size' bytes in stack. */
Ken Liu63a176b2022-06-09 22:36:56 +0800141#define ARCH_CTXCTRL_ALLOCATE_STACK(x, size) \
142 ((x)->sp -= ((size) + 7) & ~0x7)
Ken Liubf4681f2022-02-11 11:15:03 +0800143
Ken Liu63a176b2022-06-09 22:36:56 +0800144/* The last allocated pointer. */
Ken Liubf4681f2022-02-11 11:15:03 +0800145#define ARCH_CTXCTRL_ALLOCATED_PTR(x) ((x)->sp)
146
147/* Prepare a exception return pattern on the stack. */
148#define ARCH_CTXCTRL_EXCRET_PATTERN(x, param, pfn, pfnlr) do { \
149 (x)->r0 = (uint32_t)(param); \
150 (x)->ra = (uint32_t)(pfn); \
151 (x)->lr = (uint32_t)(pfnlr); \
152 (x)->xpsr = XPSR_T32; \
153 } while (0)
154
Ken Liu63a176b2022-06-09 22:36:56 +0800155/*
156 * Claim a statically initialized context control instance.
157 * Make the start stack pointer at 'stack_buf[stack_size]' because
158 * the hardware acts in a 'Decrease-then-store' behaviour.
159 */
160#define ARCH_CLAIM_CTXCTRL_INSTANCE(name, stack_buf, stack_size) \
161 struct context_ctrl_t name = { \
162 .sp = (uint32_t)&stack_buf[stack_size], \
163 .sp_base = (uint32_t)&stack_buf[stack_size], \
164 .sp_limit = (uint32_t)stack_buf, \
165 .exc_ret = 0, \
166 }
167
David Hu50711e32019-06-12 18:32:30 +0800168/**
169 * \brief Get Link Register
170 * \details Returns the value of the Link Register (LR)
171 * \return LR value
172 */
TTornblomdd233d12020-11-05 11:44:28 +0100173#if !defined ( __ICCARM__ )
David Hu50711e32019-06-12 18:32:30 +0800174__attribute__ ((always_inline)) __STATIC_INLINE uint32_t __get_LR(void)
175{
176 register uint32_t result;
177
178 __ASM volatile ("MOV %0, LR\n" : "=r" (result));
179 return result;
180}
TTornblomdd233d12020-11-05 11:44:28 +0100181#endif
David Hu50711e32019-06-12 18:32:30 +0800182
Ken Liu92ede9f2021-10-20 09:35:00 +0800183__STATIC_INLINE uint32_t __save_disable_irq(void)
184{
185 uint32_t result;
186
187 __ASM volatile ("mrs %0, primask \n cpsid i" : "=r" (result) :: "memory");
188 return result;
189}
190
191__STATIC_INLINE void __restore_irq(uint32_t status)
192{
193 __ASM volatile ("msr primask, %0" :: "r" (status) : "memory");
194}
195
David Hu50711e32019-06-12 18:32:30 +0800196__attribute__ ((always_inline))
197__STATIC_INLINE uint32_t __get_active_exc_num(void)
198{
199 IPSR_Type IPSR;
200
201 /* if non-zero, exception is active. NOT banked S/NS */
202 IPSR.w = __get_IPSR();
203 return IPSR.b.ISR;
204}
205
206__attribute__ ((always_inline))
207__STATIC_INLINE void __set_CONTROL_SPSEL(uint32_t SPSEL)
208{
209 CONTROL_Type ctrl;
210
211 ctrl.w = __get_CONTROL();
212 ctrl.b.SPSEL = SPSEL;
213 __set_CONTROL(ctrl.w);
214 __ISB();
215}
216
Antonio de Angelis995e4a62022-10-19 15:46:42 +0100217
218/**
219 * \brief Whether in privileged level
220 *
221 * \retval true If current execution runs in privileged level.
222 * \retval false If current execution runs in unprivileged level.
223 */
224__STATIC_INLINE bool tfm_arch_is_priv(void)
225{
226 CONTROL_Type ctrl;
227
228 /* If in Handler mode */
229 if (__get_IPSR()) {
230 return true;
231 }
232
233 /* If in privileged Thread mode */
234 ctrl.w = __get_CONTROL();
235 if (!ctrl.b.nPRIV) {
236 return true;
237 }
238
239 return false;
240}
241
Gabor Toth4d414112021-11-10 17:44:50 +0100242#if (CONFIG_TFM_FLOAT_ABI >= 1) && CONFIG_TFM_LAZY_STACKING
Feder Liang42f5b562021-09-10 17:38:36 +0800243#define ARCH_FLUSH_FP_CONTEXT() __asm volatile("vmov s0, s0 \n":::"memory")
244#else
245#define ARCH_FLUSH_FP_CONTEXT()
246#endif
247
Ken Liu5d73c872021-08-19 19:23:17 +0800248/* Set secure exceptions priority. */
Ken Liu50e21092020-10-14 16:42:15 +0800249void tfm_arch_set_secure_exception_priorities(void);
Jamie Fox3ede9712020-09-28 23:14:54 +0100250
Ken Liu5d73c872021-08-19 19:23:17 +0800251/* Configure various extensions. */
Summer Qindea1f2c2021-01-11 14:46:34 +0800252void tfm_arch_config_extensions(void);
Jamie Fox45587672020-08-17 18:31:14 +0100253
Gabor Toth4d414112021-11-10 17:44:50 +0100254#if (CONFIG_TFM_FLOAT_ABI > 0)
Ken Liu182fb402022-06-20 16:05:47 +0800255/* Clear float point data. */
Feder Liang42f5b562021-09-10 17:38:36 +0800256void tfm_arch_clear_fp_data(void);
257#endif
258
Kevin Peng300c68d2021-08-12 17:40:17 +0800259/*
260 * This function is called after SPM has initialized.
261 * It frees the stack used by SPM initialization and do Exception Return.
262 * It does not return.
263 */
Ken Liudedbf4b2021-11-02 09:07:25 +0800264void tfm_arch_free_msp_and_exc_ret(uint32_t msp_base, uint32_t exc_return);
Kevin Peng300c68d2021-08-12 17:40:17 +0800265
Ken Liu5d73c872021-08-19 19:23:17 +0800266/*
267 * This function sets return value on APIs that cause scheduling, for example
268 * psa_wait(), by manipulating the control context - this is usaully setting the
269 * R0 register of the thread context.
270 */
Ken Liuca4580f2022-03-09 21:27:43 +0800271void tfm_arch_set_context_ret_code(void *p_ctx_ctrl, uint32_t ret_code);
Ken Liu5d73c872021-08-19 19:23:17 +0800272
273/* Init a thread context on thread stack and update the control context. */
274void tfm_arch_init_context(void *p_ctx_ctrl,
Ken Liubf4681f2022-02-11 11:15:03 +0800275 uintptr_t pfn, void *param, uintptr_t pfnlr);
Ken Liu5d73c872021-08-19 19:23:17 +0800276
277/*
278 * Refresh the HW (sp, splimit) according to the given control context and
279 * returns the EXC_RETURN payload (caller might need it for following codes).
280 *
Ken Liubf4681f2022-02-11 11:15:03 +0800281 * The p_ctx_ctrl must have been initialized by 'tfm_arch_init_context'.
Ken Liu5d73c872021-08-19 19:23:17 +0800282 */
283uint32_t tfm_arch_refresh_hardware_context(void *p_ctx_ctrl);
284
Ken Liue07c3b72021-10-14 16:19:13 +0800285/*
286 * Triggers scheduler. A return type is assigned in case
287 * SPM returns values by the context.
288 */
289uint32_t tfm_arch_trigger_pendsv(void);
290
Ken Liue07c3b72021-10-14 16:19:13 +0800291/*
292 * Switch to a new stack area, lock scheduler and call function.
293 * If 'stk_base' is ZERO, stack won't be switched and re-use caller stack.
294 */
Ken Liuca4580f2022-03-09 21:27:43 +0800295void arch_non_preempt_call(uintptr_t fn_addr, uintptr_t frame_addr,
296 uint32_t stk_base, uint32_t stk_limit);
Ken Liue07c3b72021-10-14 16:19:13 +0800297
David Hu50711e32019-06-12 18:32:30 +0800298#endif