David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 1 | /* |
Feder Liang | 5519438 | 2021-11-22 16:45:33 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 3 | * |
| 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 Liu | 1d96c13 | 2019-12-31 15:51:30 +0800 | [diff] [blame] | 12 | #include <stddef.h> |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 13 | #include <inttypes.h> |
Kevin Peng | bc5e5aa | 2019-10-16 10:55:17 +0800 | [diff] [blame] | 14 | #include "tfm_hal_device_header.h" |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 15 | #include "cmsis_compiler.h" |
| 16 | |
Ronald Cron | 312be68 | 2019-09-23 09:27:33 +0200 | [diff] [blame] | 17 | #if defined(__ARM_ARCH_8_1M_MAIN__) || \ |
| 18 | defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 19 | #include "tfm_arch_v8m.h" |
David Hu | 40455c9 | 2019-07-02 14:31:34 +0800 | [diff] [blame] | 20 | #elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \ |
| 21 | defined(__ARM_ARCH_7EM__) |
| 22 | #include "tfm_arch_v6m_v7m.h" |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 23 | #else |
| 24 | #error "Unsupported ARM Architecture." |
| 25 | #endif |
| 26 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 27 | #define SCHEDULER_LOCKED 1 |
| 28 | #define SCHEDULER_UNLOCKED 0 |
| 29 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 30 | #define XPSR_T32 0x01000000 |
| 31 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 32 | /* State context defined by architecture */ |
Ken Liu | 5a2b905 | 2019-08-15 19:03:29 +0800 | [diff] [blame] | 33 | struct tfm_state_context_t { |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 34 | uint32_t r0; |
| 35 | uint32_t r1; |
| 36 | uint32_t r2; |
| 37 | uint32_t r3; |
| 38 | uint32_t r12; |
Ken Liu | 5a2b905 | 2019-08-15 19:03:29 +0800 | [diff] [blame] | 39 | uint32_t lr; |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 40 | uint32_t ra; |
| 41 | uint32_t xpsr; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 42 | }; |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 43 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 44 | /* Context addition to state context */ |
| 45 | struct tfm_additional_context_t { |
| 46 | uint32_t callee[8]; /* R4-R11. NOT ORDERED!! */ |
| 47 | }; |
| 48 | |
| 49 | /* Full thread context */ |
| 50 | struct full_context_t { |
| 51 | struct tfm_additional_context_t addi_ctx; |
| 52 | struct tfm_state_context_t stat_ctx; |
| 53 | }; |
| 54 | |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame^] | 55 | /* |
| 56 | * Under cross call ABI, SPM can be preempted by interrupts, the interrupt |
| 57 | * handling can set SPM API return value and makes the initial SPM API |
| 58 | * return code invalid. Use one flag to indicate if the return code has been |
| 59 | * force updated by interrupts, then SPM return code can be discarded as it |
| 60 | * is out of date. |
| 61 | */ |
| 62 | #define CROSS_RETCODE_EMPTY 0xEEEEEEED |
| 63 | #define CROSS_RETCODE_UPDATED 0xEEEEEEEE |
| 64 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 65 | /* Context control */ |
| 66 | struct context_ctrl_t { |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame^] | 67 | uint32_t sp; /* Stack pointer (higher address) */ |
| 68 | uint32_t sp_limit; /* Stack limit (lower address) */ |
| 69 | uint32_t allocated; /* Stack alloced bytes (8-aligned) */ |
| 70 | uint32_t exc_ret; /* EXC_RETURN pattern. */ |
| 71 | uint32_t cross_frame; /* Cross call frame position. */ |
| 72 | uint32_t retcode_status; /* Cross call retcode status. */ |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * The context on MSP when de-privileged FLIH Function calls SVC to return. |
| 77 | * It is the same when de-privileged FLIH Function is ready to run. |
| 78 | */ |
| 79 | struct context_flih_ret_t { |
| 80 | uint64_t stack_seal; /* Two words stack seal */ |
| 81 | struct tfm_additional_context_t addi_ctx; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 82 | uint32_t psp; /* PSP when interrupt exception ocurrs */ |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 83 | uint32_t psplim; /* PSPLIM when interrupt exception ocurrs when */ |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 84 | struct tfm_state_context_t state_ctx; /* ctx on SVC_PREPARE_DEPRIV_FLIH */ |
| 85 | }; |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 86 | |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame^] | 87 | /* A customized ABI format. */ |
| 88 | struct cross_call_abi_frame_t { |
| 89 | uint32_t a0; |
| 90 | uint32_t a1; |
| 91 | uint32_t a2; |
| 92 | uint32_t a3; |
| 93 | uint32_t unused0; |
| 94 | uint32_t unused1; |
| 95 | }; |
| 96 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 97 | /* Assign stack and stack limit to the context control instance. */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame^] | 98 | #define ARCH_CTXCTRL_INIT(x, buf, size) do { \ |
| 99 | (x)->sp = ((uint32_t)(buf) + (uint32_t)(size)) & ~0x7; \ |
| 100 | (x)->sp_limit = ((uint32_t)(buf) + 7) & ~0x7; \ |
| 101 | (x)->allocated = 0; \ |
| 102 | (x)->exc_ret = 0; \ |
| 103 | (x)->cross_frame = 0; \ |
| 104 | (x)->retcode_status = CROSS_RETCODE_EMPTY; \ |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 105 | } while (0) |
| 106 | |
| 107 | /* Allocate 'size' bytes in stack. */ |
| 108 | #define ARCH_CTXCTRL_ALLOCATE_STACK(x, size) do { \ |
| 109 | (x)->allocated += ((size) + 7) & ~0x7; \ |
| 110 | (x)->sp -= (x)->allocated; \ |
| 111 | } while (0) |
| 112 | |
| 113 | /* The latest allocated pointer. */ |
| 114 | #define ARCH_CTXCTRL_ALLOCATED_PTR(x) ((x)->sp) |
| 115 | |
| 116 | /* Prepare a exception return pattern on the stack. */ |
| 117 | #define ARCH_CTXCTRL_EXCRET_PATTERN(x, param, pfn, pfnlr) do { \ |
| 118 | (x)->r0 = (uint32_t)(param); \ |
| 119 | (x)->ra = (uint32_t)(pfn); \ |
| 120 | (x)->lr = (uint32_t)(pfnlr); \ |
| 121 | (x)->xpsr = XPSR_T32; \ |
| 122 | } while (0) |
| 123 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 124 | /** |
| 125 | * \brief Get Link Register |
| 126 | * \details Returns the value of the Link Register (LR) |
| 127 | * \return LR value |
| 128 | */ |
TTornblom | dd233d1 | 2020-11-05 11:44:28 +0100 | [diff] [blame] | 129 | #if !defined ( __ICCARM__ ) |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 130 | __attribute__ ((always_inline)) __STATIC_INLINE uint32_t __get_LR(void) |
| 131 | { |
| 132 | register uint32_t result; |
| 133 | |
| 134 | __ASM volatile ("MOV %0, LR\n" : "=r" (result)); |
| 135 | return result; |
| 136 | } |
TTornblom | dd233d1 | 2020-11-05 11:44:28 +0100 | [diff] [blame] | 137 | #endif |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 138 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 139 | __STATIC_INLINE uint32_t __save_disable_irq(void) |
| 140 | { |
| 141 | uint32_t result; |
| 142 | |
| 143 | __ASM volatile ("mrs %0, primask \n cpsid i" : "=r" (result) :: "memory"); |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | __STATIC_INLINE void __restore_irq(uint32_t status) |
| 148 | { |
| 149 | __ASM volatile ("msr primask, %0" :: "r" (status) : "memory"); |
| 150 | } |
| 151 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 152 | __attribute__ ((always_inline)) |
| 153 | __STATIC_INLINE uint32_t __get_active_exc_num(void) |
| 154 | { |
| 155 | IPSR_Type IPSR; |
| 156 | |
| 157 | /* if non-zero, exception is active. NOT banked S/NS */ |
| 158 | IPSR.w = __get_IPSR(); |
| 159 | return IPSR.b.ISR; |
| 160 | } |
| 161 | |
| 162 | __attribute__ ((always_inline)) |
| 163 | __STATIC_INLINE void __set_CONTROL_SPSEL(uint32_t SPSEL) |
| 164 | { |
| 165 | CONTROL_Type ctrl; |
| 166 | |
| 167 | ctrl.w = __get_CONTROL(); |
| 168 | ctrl.b.SPSEL = SPSEL; |
| 169 | __set_CONTROL(ctrl.w); |
| 170 | __ISB(); |
| 171 | } |
| 172 | |
Feder Liang | 5519438 | 2021-11-22 16:45:33 +0800 | [diff] [blame] | 173 | #if (CONFIG_TFM_FP >= 1) && CONFIG_TFM_LAZY_STACKING |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 174 | #define ARCH_FLUSH_FP_CONTEXT() __asm volatile("vmov s0, s0 \n":::"memory") |
| 175 | #else |
| 176 | #define ARCH_FLUSH_FP_CONTEXT() |
| 177 | #endif |
| 178 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 179 | /* Set secure exceptions priority. */ |
Ken Liu | 50e2109 | 2020-10-14 16:42:15 +0800 | [diff] [blame] | 180 | void tfm_arch_set_secure_exception_priorities(void); |
Jamie Fox | 3ede971 | 2020-09-28 23:14:54 +0100 | [diff] [blame] | 181 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 182 | /* Configure various extensions. */ |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 183 | void tfm_arch_config_extensions(void); |
Jamie Fox | 4558767 | 2020-08-17 18:31:14 +0100 | [diff] [blame] | 184 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 185 | /* Clear float point status. */ |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 186 | void tfm_arch_clear_fp_status(void); |
| 187 | |
Feder Liang | 5519438 | 2021-11-22 16:45:33 +0800 | [diff] [blame] | 188 | #if (CONFIG_TFM_FP >= 1) |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 189 | /* |
| 190 | * Clear float point data. |
| 191 | */ |
| 192 | void tfm_arch_clear_fp_data(void); |
| 193 | #endif |
| 194 | |
Kevin Peng | 300c68d | 2021-08-12 17:40:17 +0800 | [diff] [blame] | 195 | /* |
| 196 | * This function is called after SPM has initialized. |
| 197 | * It frees the stack used by SPM initialization and do Exception Return. |
| 198 | * It does not return. |
| 199 | */ |
Ken Liu | dedbf4b | 2021-11-02 09:07:25 +0800 | [diff] [blame] | 200 | 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] | 201 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 202 | /* |
| 203 | * This function sets return value on APIs that cause scheduling, for example |
| 204 | * psa_wait(), by manipulating the control context - this is usaully setting the |
| 205 | * R0 register of the thread context. |
| 206 | */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame^] | 207 | 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] | 208 | |
| 209 | /* Init a thread context on thread stack and update the control context. */ |
| 210 | void tfm_arch_init_context(void *p_ctx_ctrl, |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 211 | uintptr_t pfn, void *param, uintptr_t pfnlr); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Refresh the HW (sp, splimit) according to the given control context and |
| 215 | * returns the EXC_RETURN payload (caller might need it for following codes). |
| 216 | * |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 217 | * 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] | 218 | */ |
| 219 | uint32_t tfm_arch_refresh_hardware_context(void *p_ctx_ctrl); |
| 220 | |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 221 | /* |
| 222 | * Triggers scheduler. A return type is assigned in case |
| 223 | * SPM returns values by the context. |
| 224 | */ |
| 225 | uint32_t tfm_arch_trigger_pendsv(void); |
| 226 | |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 227 | /* |
| 228 | * Switch to a new stack area, lock scheduler and call function. |
| 229 | * If 'stk_base' is ZERO, stack won't be switched and re-use caller stack. |
| 230 | */ |
Ken Liu | ca4580f | 2022-03-09 21:27:43 +0800 | [diff] [blame^] | 231 | void arch_non_preempt_call(uintptr_t fn_addr, uintptr_t frame_addr, |
| 232 | uint32_t stk_base, uint32_t stk_limit); |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 233 | |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 234 | #endif |