blob: c0b5276f81ad60b47cfa485331ef9eafb9d95f5c [file] [log] [blame]
David Hu50711e32019-06-12 18:32:30 +08001/*
Summer Qindea1f2c2021-01-11 14:46:34 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
David Hu50711e32019-06-12 18:32:30 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7#ifndef __TFM_ARCH_H__
8#define __TFM_ARCH_H__
9
10/* This header file collects the architecture related operations. */
11
Ken Liu1d96c132019-12-31 15:51:30 +080012#include <stddef.h>
David Hu50711e32019-06-12 18:32:30 +080013#include <inttypes.h>
Kevin Pengbc5e5aa2019-10-16 10:55:17 +080014#include "tfm_hal_device_header.h"
David Hu50711e32019-06-12 18:32:30 +080015#include "cmsis_compiler.h"
16
Ronald Cron312be682019-09-23 09:27:33 +020017#if defined(__ARM_ARCH_8_1M_MAIN__) || \
18 defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Hu50711e32019-06-12 18:32:30 +080019#include "tfm_arch_v8m.h"
David Hu40455c92019-07-02 14:31:34 +080020#elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
21 defined(__ARM_ARCH_7EM__)
22#include "tfm_arch_v6m_v7m.h"
David Hu50711e32019-06-12 18:32:30 +080023#else
24#error "Unsupported ARM Architecture."
25#endif
26
Mingyang Sun620c8562021-11-10 11:44:58 +080027#define SCHEDULER_LOCKED 1
28#define SCHEDULER_UNLOCKED 0
29
David Hu50711e32019-06-12 18:32:30 +080030#define XPSR_T32 0x01000000
31
Ken Liu5d73c872021-08-19 19:23:17 +080032/* State context defined by architecture */
Ken Liu5a2b9052019-08-15 19:03:29 +080033struct tfm_state_context_t {
David Hu50711e32019-06-12 18:32:30 +080034 uint32_t r0;
35 uint32_t r1;
36 uint32_t r2;
37 uint32_t r3;
38 uint32_t r12;
Ken Liu5a2b9052019-08-15 19:03:29 +080039 uint32_t lr;
David Hu50711e32019-06-12 18:32:30 +080040 uint32_t ra;
41 uint32_t xpsr;
Ken Liu5d73c872021-08-19 19:23:17 +080042};
David Hu50711e32019-06-12 18:32:30 +080043
Ken Liu5d73c872021-08-19 19:23:17 +080044/* Context addition to state context */
45struct tfm_additional_context_t {
46 uint32_t callee[8]; /* R4-R11. NOT ORDERED!! */
47};
48
49/* Full thread context */
50struct full_context_t {
51 struct tfm_additional_context_t addi_ctx;
52 struct tfm_state_context_t stat_ctx;
53};
54
55/* Context control */
56struct context_ctrl_t {
57 uint32_t sp; /* Stack pointer (higher address) */
58 uint32_t sp_limit; /* Stack limit (lower address) */
59 uint32_t reserved; /* Reserved */
60 uint32_t exc_ret; /* EXC_RETURN pattern. */
61};
62
63/*
64 * The context on MSP when de-privileged FLIH Function calls SVC to return.
65 * It is the same when de-privileged FLIH Function is ready to run.
66 */
67struct context_flih_ret_t {
68 uint64_t stack_seal; /* Two words stack seal */
69 struct tfm_additional_context_t addi_ctx;
Ken Liu5d73c872021-08-19 19:23:17 +080070 uint32_t psp; /* PSP when interrupt exception ocurrs */
Kevin Pengca59ec02021-12-09 14:35:50 +080071 uint32_t psplim; /* PSPLIM when interrupt exception ocurrs when */
Ken Liu5d73c872021-08-19 19:23:17 +080072 struct tfm_state_context_t state_ctx; /* ctx on SVC_PREPARE_DEPRIV_FLIH */
73};
David Hu50711e32019-06-12 18:32:30 +080074
David Hu50711e32019-06-12 18:32:30 +080075/**
76 * \brief Get Link Register
77 * \details Returns the value of the Link Register (LR)
78 * \return LR value
79 */
TTornblomdd233d12020-11-05 11:44:28 +010080#if !defined ( __ICCARM__ )
David Hu50711e32019-06-12 18:32:30 +080081__attribute__ ((always_inline)) __STATIC_INLINE uint32_t __get_LR(void)
82{
83 register uint32_t result;
84
85 __ASM volatile ("MOV %0, LR\n" : "=r" (result));
86 return result;
87}
TTornblomdd233d12020-11-05 11:44:28 +010088#endif
David Hu50711e32019-06-12 18:32:30 +080089
Ken Liu92ede9f2021-10-20 09:35:00 +080090__STATIC_INLINE uint32_t __save_disable_irq(void)
91{
92 uint32_t result;
93
94 __ASM volatile ("mrs %0, primask \n cpsid i" : "=r" (result) :: "memory");
95 return result;
96}
97
98__STATIC_INLINE void __restore_irq(uint32_t status)
99{
100 __ASM volatile ("msr primask, %0" :: "r" (status) : "memory");
101}
102
David Hu50711e32019-06-12 18:32:30 +0800103__attribute__ ((always_inline))
104__STATIC_INLINE uint32_t __get_active_exc_num(void)
105{
106 IPSR_Type IPSR;
107
108 /* if non-zero, exception is active. NOT banked S/NS */
109 IPSR.w = __get_IPSR();
110 return IPSR.b.ISR;
111}
112
113__attribute__ ((always_inline))
114__STATIC_INLINE void __set_CONTROL_SPSEL(uint32_t SPSEL)
115{
116 CONTROL_Type ctrl;
117
118 ctrl.w = __get_CONTROL();
119 ctrl.b.SPSEL = SPSEL;
120 __set_CONTROL(ctrl.w);
121 __ISB();
122}
123
Feder Liang42f5b562021-09-10 17:38:36 +0800124#if (CONFIG_TFM_SPE_FP >= 1) && CONFIG_TFM_LAZY_STACKING_SPE
125#define ARCH_FLUSH_FP_CONTEXT() __asm volatile("vmov s0, s0 \n":::"memory")
126#else
127#define ARCH_FLUSH_FP_CONTEXT()
128#endif
129
Ken Liu5d73c872021-08-19 19:23:17 +0800130/* Set secure exceptions priority. */
Ken Liu50e21092020-10-14 16:42:15 +0800131void tfm_arch_set_secure_exception_priorities(void);
Jamie Fox3ede9712020-09-28 23:14:54 +0100132
Ken Liu5d73c872021-08-19 19:23:17 +0800133/* Configure various extensions. */
Summer Qindea1f2c2021-01-11 14:46:34 +0800134void tfm_arch_config_extensions(void);
Jamie Fox45587672020-08-17 18:31:14 +0100135
Ken Liu5d73c872021-08-19 19:23:17 +0800136/* Clear float point status. */
Ken Liuce2692d2020-02-11 12:39:36 +0800137void tfm_arch_clear_fp_status(void);
138
Feder Liang42f5b562021-09-10 17:38:36 +0800139#if (CONFIG_TFM_SPE_FP >= 1)
140/*
141 * Clear float point data.
142 */
143void tfm_arch_clear_fp_data(void);
144#endif
145
Kevin Peng300c68d2021-08-12 17:40:17 +0800146/*
147 * This function is called after SPM has initialized.
148 * It frees the stack used by SPM initialization and do Exception Return.
149 * It does not return.
150 */
Ken Liudedbf4b2021-11-02 09:07:25 +0800151void tfm_arch_free_msp_and_exc_ret(uint32_t msp_base, uint32_t exc_return);
Kevin Peng300c68d2021-08-12 17:40:17 +0800152
Ken Liu5d73c872021-08-19 19:23:17 +0800153/*
154 * This function sets return value on APIs that cause scheduling, for example
155 * psa_wait(), by manipulating the control context - this is usaully setting the
156 * R0 register of the thread context.
157 */
158void tfm_arch_set_context_ret_code(void *p_ctx_ctrl, uintptr_t ret_code);
159
160/* Init a thread context on thread stack and update the control context. */
161void tfm_arch_init_context(void *p_ctx_ctrl,
162 uintptr_t pfn, void *param, uintptr_t pfnlr,
163 uintptr_t sp_limit, uintptr_t sp);
164
165/*
166 * Refresh the HW (sp, splimit) according to the given control context and
167 * returns the EXC_RETURN payload (caller might need it for following codes).
168 *
169 * The p_ctx_ctrl must have been initialized by tfm_arch_init_context
170 */
171uint32_t tfm_arch_refresh_hardware_context(void *p_ctx_ctrl);
172
Ken Liue07c3b72021-10-14 16:19:13 +0800173/*
174 * Triggers scheduler. A return type is assigned in case
175 * SPM returns values by the context.
176 */
177uint32_t tfm_arch_trigger_pendsv(void);
178
179
180/*
181 * Switch to a new stack area, lock scheduler and call function.
182 * If 'stk_base' is ZERO, stack won't be switched and re-use caller stack.
183 */
184uint32_t arch_non_preempt_call(uintptr_t fn_addr, uintptr_t frame_addr,
185 uint32_t stk_base, uint32_t stk_limit);
186
David Hu50711e32019-06-12 18:32:30 +0800187#endif