David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 1 | /* |
Ken Liu | 5385ad1 | 2023-01-04 12:37:28 +0800 | [diff] [blame^] | 2 | * Copyright (c) 2018-2023, Arm Limited. All rights reserved. |
Chris Brand | be5bec1 | 2022-10-18 11:41:59 -0700 | [diff] [blame] | 3 | * Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon |
| 4 | * company) or an affiliate of Cypress Semiconductor Corporation. All rights |
| 5 | * reserved. |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 6 | * |
| 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 Liu | 1d96c13 | 2019-12-31 15:51:30 +0800 | [diff] [blame] | 15 | #include <stddef.h> |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 16 | #include <inttypes.h> |
Michel Jaouen | af0e98d | 2022-11-01 10:08:20 +0100 | [diff] [blame] | 17 | #include "fih.h" |
Kevin Peng | bc5e5aa | 2019-10-16 10:55:17 +0800 | [diff] [blame] | 18 | #include "tfm_hal_device_header.h" |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 19 | #include "cmsis_compiler.h" |
| 20 | |
Ronald Cron | 312be68 | 2019-09-23 09:27:33 +0200 | [diff] [blame] | 21 | #if defined(__ARM_ARCH_8_1M_MAIN__) || \ |
| 22 | defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 23 | #include "tfm_arch_v8m.h" |
David Hu | 40455c9 | 2019-07-02 14:31:34 +0800 | [diff] [blame] | 24 | #elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \ |
| 25 | defined(__ARM_ARCH_7EM__) |
| 26 | #include "tfm_arch_v6m_v7m.h" |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 27 | #else |
| 28 | #error "Unsupported ARM Architecture." |
| 29 | #endif |
| 30 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 31 | #define SCHEDULER_LOCKED 1 |
| 32 | #define SCHEDULER_UNLOCKED 0 |
| 33 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 34 | #define XPSR_T32 0x01000000 |
| 35 | |
Michel Jaouen | af0e98d | 2022-11-01 10:08:20 +0100 | [diff] [blame] | 36 | /* Define IRQ level */ |
| 37 | #if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__) |
| 38 | #define SecureFault_IRQnLVL (0) |
| 39 | #define MemoryManagement_IRQnLVL (0) |
| 40 | #define BusFault_IRQnLVL (0) |
| 41 | #define SVCall_IRQnLVL (0) |
| 42 | #elif defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) |
| 43 | #define MemoryManagement_IRQnLVL (0) |
| 44 | #define BusFault_IRQnLVL (0) |
| 45 | #define SVCall_IRQnLVL (0) |
| 46 | #elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_8M_BASE__) |
| 47 | #define SVCall_IRQnLVL (0) |
| 48 | #else |
| 49 | #error "Unsupported ARM Architecture." |
| 50 | #endif |
| 51 | |
| 52 | |
Chris Brand | be5bec1 | 2022-10-18 11:41:59 -0700 | [diff] [blame] | 53 | /* The lowest secure interrupt priority */ |
| 54 | #ifdef CONFIG_TFM_USE_TRUSTZONE |
| 55 | /* IMPORTANT NOTE: |
| 56 | * |
| 57 | * Although the priority of the secure PendSV must be the lowest possible |
| 58 | * among other interrupts in the Secure state, it must be ensured that |
| 59 | * PendSV is not preempted nor masked by Non-Secure interrupts to ensure |
| 60 | * the integrity of the Secure operation. |
| 61 | * When AIRCR.PRIS is set, the Non-Secure execution can act on |
| 62 | * FAULTMASK_NS, PRIMASK_NS or BASEPRI_NS register to boost its priority |
| 63 | * number up to the value 0x80. |
| 64 | * For this reason, set the priority of the PendSV interrupt to the next |
| 65 | * priority level configurable on the platform, just below 0x80. |
| 66 | */ |
| 67 | #define PENDSV_PRIO_FOR_SCHED ((1 << (__NVIC_PRIO_BITS - 1)) - 1) |
| 68 | #else |
| 69 | /* If TZ is not in use, we have the full priority range available */ |
| 70 | #define PENDSV_PRIO_FOR_SCHED ((1 << __NVIC_PRIO_BITS) - 1) |
| 71 | #endif |
| 72 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 73 | /* State context defined by architecture */ |
Ken Liu | 5a2b905 | 2019-08-15 19:03:29 +0800 | [diff] [blame] | 74 | struct tfm_state_context_t { |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 75 | uint32_t r0; |
| 76 | uint32_t r1; |
| 77 | uint32_t r2; |
| 78 | uint32_t r3; |
| 79 | uint32_t r12; |
Ken Liu | 5a2b905 | 2019-08-15 19:03:29 +0800 | [diff] [blame] | 80 | uint32_t lr; |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 81 | uint32_t ra; |
| 82 | uint32_t xpsr; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 83 | }; |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 84 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 85 | /* Context addition to state context */ |
| 86 | struct tfm_additional_context_t { |
Ken Liu | 5385ad1 | 2023-01-04 12:37:28 +0800 | [diff] [blame^] | 87 | uint32_t integ_sign; /* Integrity signature */ |
| 88 | uint32_t reserved; /* Reserved */ |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 89 | uint32_t callee[8]; /* R4-R11. NOT ORDERED!! */ |
| 90 | }; |
| 91 | |
| 92 | /* Full thread context */ |
| 93 | struct full_context_t { |
| 94 | struct tfm_additional_context_t addi_ctx; |
| 95 | struct tfm_state_context_t stat_ctx; |
| 96 | }; |
| 97 | |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame] | 98 | /* |
| 99 | * Under cross call ABI, SPM can be preempted by interrupts, the interrupt |
| 100 | * handling can set SPM API return value and makes the initial SPM API |
| 101 | * return code invalid. Use one flag to indicate if the return code has been |
| 102 | * force updated by interrupts, then SPM return code can be discarded as it |
| 103 | * is out of date. |
| 104 | */ |
| 105 | #define CROSS_RETCODE_EMPTY 0xEEEEEEED |
| 106 | #define CROSS_RETCODE_UPDATED 0xEEEEEEEE |
| 107 | |
Sherry Zhang | b24f54d | 2022-07-04 14:26:07 +0800 | [diff] [blame] | 108 | /* Context control. |
| 109 | * CAUTION: Assembly references this structure. DO CHECK the below functions |
| 110 | * before changing the structure: |
| 111 | 'PendSV_Handler' |
| 112 | */ |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 113 | struct context_ctrl_t { |
Sherry Zhang | b24f54d | 2022-07-04 14:26:07 +0800 | [diff] [blame] | 114 | uint32_t sp; /* Stack pointer (higher address). |
| 115 | * THIS MUST BE THE FIRST MEMBER OF |
| 116 | * THE STRUCT. |
| 117 | */ |
| 118 | uint32_t exc_ret; /* EXC_RETURN pattern. |
| 119 | * THIS MUST BE THE SECOND MEMBER OF |
| 120 | * THE STRUCT. |
| 121 | */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame] | 122 | uint32_t sp_limit; /* Stack limit (lower address) */ |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 123 | uint32_t sp_base; /* Stack usage start (higher addr) */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame] | 124 | uint32_t cross_frame; /* Cross call frame position. */ |
| 125 | uint32_t retcode_status; /* Cross call retcode status. */ |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * The context on MSP when de-privileged FLIH Function calls SVC to return. |
| 130 | * It is the same when de-privileged FLIH Function is ready to run. |
| 131 | */ |
| 132 | struct context_flih_ret_t { |
| 133 | uint64_t stack_seal; /* Two words stack seal */ |
| 134 | struct tfm_additional_context_t addi_ctx; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 135 | uint32_t psp; /* PSP when interrupt exception ocurrs */ |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 136 | uint32_t psplim; /* PSPLIM when interrupt exception ocurrs when */ |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 137 | struct tfm_state_context_t state_ctx; /* ctx on SVC_PREPARE_DEPRIV_FLIH */ |
| 138 | }; |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 139 | |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame] | 140 | /* A customized ABI format. */ |
| 141 | struct cross_call_abi_frame_t { |
| 142 | uint32_t a0; |
| 143 | uint32_t a1; |
| 144 | uint32_t a2; |
| 145 | uint32_t a3; |
| 146 | uint32_t unused0; |
| 147 | uint32_t unused1; |
| 148 | }; |
| 149 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 150 | /* Assign stack and stack limit to the context control instance. */ |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 151 | #define ARCH_CTXCTRL_INIT(x, buf, sz) do { \ |
| 152 | (x)->sp = ((uint32_t)(buf) + (uint32_t)(sz)) & ~0x7; \ |
| 153 | (x)->sp_limit = ((uint32_t)(buf) + 7) & ~0x7; \ |
| 154 | (x)->sp_base = (x)->sp; \ |
| 155 | (x)->exc_ret = 0; \ |
| 156 | (x)->cross_frame = 0; \ |
| 157 | (x)->retcode_status = CROSS_RETCODE_EMPTY; \ |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 158 | } while (0) |
| 159 | |
| 160 | /* Allocate 'size' bytes in stack. */ |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 161 | #define ARCH_CTXCTRL_ALLOCATE_STACK(x, size) \ |
| 162 | ((x)->sp -= ((size) + 7) & ~0x7) |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 163 | |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 164 | /* The last allocated pointer. */ |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 165 | #define ARCH_CTXCTRL_ALLOCATED_PTR(x) ((x)->sp) |
| 166 | |
| 167 | /* Prepare a exception return pattern on the stack. */ |
| 168 | #define ARCH_CTXCTRL_EXCRET_PATTERN(x, param, pfn, pfnlr) do { \ |
| 169 | (x)->r0 = (uint32_t)(param); \ |
| 170 | (x)->ra = (uint32_t)(pfn); \ |
| 171 | (x)->lr = (uint32_t)(pfnlr); \ |
| 172 | (x)->xpsr = XPSR_T32; \ |
| 173 | } while (0) |
| 174 | |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 175 | /* |
| 176 | * Claim a statically initialized context control instance. |
| 177 | * Make the start stack pointer at 'stack_buf[stack_size]' because |
| 178 | * the hardware acts in a 'Decrease-then-store' behaviour. |
| 179 | */ |
| 180 | #define ARCH_CLAIM_CTXCTRL_INSTANCE(name, stack_buf, stack_size) \ |
| 181 | struct context_ctrl_t name = { \ |
| 182 | .sp = (uint32_t)&stack_buf[stack_size], \ |
| 183 | .sp_base = (uint32_t)&stack_buf[stack_size], \ |
| 184 | .sp_limit = (uint32_t)stack_buf, \ |
| 185 | .exc_ret = 0, \ |
| 186 | } |
| 187 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 188 | /** |
| 189 | * \brief Get Link Register |
| 190 | * \details Returns the value of the Link Register (LR) |
| 191 | * \return LR value |
| 192 | */ |
TTornblom | dd233d1 | 2020-11-05 11:44:28 +0100 | [diff] [blame] | 193 | #if !defined ( __ICCARM__ ) |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 194 | __attribute__ ((always_inline)) __STATIC_INLINE uint32_t __get_LR(void) |
| 195 | { |
| 196 | register uint32_t result; |
| 197 | |
| 198 | __ASM volatile ("MOV %0, LR\n" : "=r" (result)); |
| 199 | return result; |
| 200 | } |
TTornblom | dd233d1 | 2020-11-05 11:44:28 +0100 | [diff] [blame] | 201 | #endif |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 202 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 203 | __STATIC_INLINE uint32_t __save_disable_irq(void) |
| 204 | { |
| 205 | uint32_t result; |
| 206 | |
| 207 | __ASM volatile ("mrs %0, primask \n cpsid i" : "=r" (result) :: "memory"); |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | __STATIC_INLINE void __restore_irq(uint32_t status) |
| 212 | { |
| 213 | __ASM volatile ("msr primask, %0" :: "r" (status) : "memory"); |
| 214 | } |
| 215 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 216 | __attribute__ ((always_inline)) |
| 217 | __STATIC_INLINE uint32_t __get_active_exc_num(void) |
| 218 | { |
| 219 | IPSR_Type IPSR; |
| 220 | |
| 221 | /* if non-zero, exception is active. NOT banked S/NS */ |
| 222 | IPSR.w = __get_IPSR(); |
| 223 | return IPSR.b.ISR; |
| 224 | } |
| 225 | |
| 226 | __attribute__ ((always_inline)) |
| 227 | __STATIC_INLINE void __set_CONTROL_SPSEL(uint32_t SPSEL) |
| 228 | { |
| 229 | CONTROL_Type ctrl; |
| 230 | |
| 231 | ctrl.w = __get_CONTROL(); |
| 232 | ctrl.b.SPSEL = SPSEL; |
| 233 | __set_CONTROL(ctrl.w); |
| 234 | __ISB(); |
| 235 | } |
| 236 | |
Antonio de Angelis | 995e4a6 | 2022-10-19 15:46:42 +0100 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * \brief Whether in privileged level |
| 240 | * |
| 241 | * \retval true If current execution runs in privileged level. |
| 242 | * \retval false If current execution runs in unprivileged level. |
| 243 | */ |
| 244 | __STATIC_INLINE bool tfm_arch_is_priv(void) |
| 245 | { |
| 246 | CONTROL_Type ctrl; |
| 247 | |
| 248 | /* If in Handler mode */ |
| 249 | if (__get_IPSR()) { |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | /* If in privileged Thread mode */ |
| 254 | ctrl.w = __get_CONTROL(); |
| 255 | if (!ctrl.b.nPRIV) { |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
Gabor Toth | 4d41411 | 2021-11-10 17:44:50 +0100 | [diff] [blame] | 262 | #if (CONFIG_TFM_FLOAT_ABI >= 1) && CONFIG_TFM_LAZY_STACKING |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 263 | #define ARCH_FLUSH_FP_CONTEXT() __asm volatile("vmov s0, s0 \n":::"memory") |
| 264 | #else |
| 265 | #define ARCH_FLUSH_FP_CONTEXT() |
| 266 | #endif |
| 267 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 268 | /* Set secure exceptions priority. */ |
Ken Liu | 50e2109 | 2020-10-14 16:42:15 +0800 | [diff] [blame] | 269 | void tfm_arch_set_secure_exception_priorities(void); |
Jamie Fox | 3ede971 | 2020-09-28 23:14:54 +0100 | [diff] [blame] | 270 | |
Michel Jaouen | af0e98d | 2022-11-01 10:08:20 +0100 | [diff] [blame] | 271 | #ifdef TFM_FIH_PROFILE_ON |
| 272 | /* Check secure exception priority */ |
| 273 | FIH_RET_TYPE(int32_t) tfm_arch_verify_secure_exception_priorities(void); |
| 274 | #endif |
| 275 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 276 | /* Configure various extensions. */ |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 277 | void tfm_arch_config_extensions(void); |
Jamie Fox | 4558767 | 2020-08-17 18:31:14 +0100 | [diff] [blame] | 278 | |
Gabor Toth | 4d41411 | 2021-11-10 17:44:50 +0100 | [diff] [blame] | 279 | #if (CONFIG_TFM_FLOAT_ABI > 0) |
Ken Liu | 182fb40 | 2022-06-20 16:05:47 +0800 | [diff] [blame] | 280 | /* Clear float point data. */ |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 281 | void tfm_arch_clear_fp_data(void); |
| 282 | #endif |
| 283 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 284 | /* |
| 285 | * This function is called after SPM has initialized. |
| 286 | * It frees the stack used by SPM initialization and do Exception Return. |
| 287 | * It does not return. |
| 288 | */ |
Ken Liu | dedbf4b | 2021-11-02 09:07:25 +0800 | [diff] [blame] | 289 | void tfm_arch_free_msp_and_exc_ret(uint32_t msp_base, uint32_t exc_return); |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 290 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 291 | /* |
| 292 | * This function sets return value on APIs that cause scheduling, for example |
| 293 | * psa_wait(), by manipulating the control context - this is usaully setting the |
| 294 | * R0 register of the thread context. |
| 295 | */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame] | 296 | void tfm_arch_set_context_ret_code(void *p_ctx_ctrl, uint32_t ret_code); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 297 | |
| 298 | /* Init a thread context on thread stack and update the control context. */ |
| 299 | void tfm_arch_init_context(void *p_ctx_ctrl, |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 300 | uintptr_t pfn, void *param, uintptr_t pfnlr); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Refresh the HW (sp, splimit) according to the given control context and |
| 304 | * returns the EXC_RETURN payload (caller might need it for following codes). |
| 305 | * |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 306 | * The p_ctx_ctrl must have been initialized by 'tfm_arch_init_context'. |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 307 | */ |
| 308 | uint32_t tfm_arch_refresh_hardware_context(void *p_ctx_ctrl); |
| 309 | |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 310 | /* |
| 311 | * Triggers scheduler. A return type is assigned in case |
| 312 | * SPM returns values by the context. |
| 313 | */ |
| 314 | uint32_t tfm_arch_trigger_pendsv(void); |
| 315 | |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 316 | /* |
| 317 | * Switch to a new stack area, lock scheduler and call function. |
| 318 | * If 'stk_base' is ZERO, stack won't be switched and re-use caller stack. |
| 319 | */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame] | 320 | void arch_non_preempt_call(uintptr_t fn_addr, uintptr_t frame_addr, |
| 321 | uint32_t stk_base, uint32_t stk_limit); |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 322 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 323 | #endif |