Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * |
| 4 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <arch_helpers.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 9 | #include <buffer.h> |
| 10 | #include <esr.h> |
| 11 | #include <exit.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 12 | #include <gic.h> |
| 13 | #include <granule.h> |
| 14 | #include <inject_exp.h> |
| 15 | #include <memory_alloc.h> |
| 16 | #include <psci.h> |
| 17 | #include <realm.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 18 | #include <rec.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 19 | #include <rsi-handler.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 20 | #include <rsi-logger.h> |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 21 | #include <run.h> |
| 22 | #include <simd.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 23 | #include <smc-rmi.h> |
| 24 | #include <smc-rsi.h> |
| 25 | #include <status.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 26 | #include <sysreg_traps.h> |
| 27 | #include <table.h> |
| 28 | |
| 29 | void save_fpu_state(struct fpu_state *fpu); |
| 30 | void restore_fpu_state(struct fpu_state *fpu); |
| 31 | |
| 32 | static void system_abort(void) |
| 33 | { |
| 34 | /* |
| 35 | * TODO: report the abort to the EL3. |
| 36 | * We need to establish the exact EL3 API first. |
| 37 | */ |
| 38 | assert(false); |
| 39 | } |
| 40 | |
| 41 | static bool fixup_aarch32_data_abort(struct rec *rec, unsigned long *esr) |
| 42 | { |
| 43 | unsigned long spsr = read_spsr_el2(); |
| 44 | |
| 45 | if ((spsr & SPSR_EL2_nRW_AARCH32) != 0UL) { |
| 46 | /* |
| 47 | * mmio emulation of AArch32 reads/writes is not supported. |
| 48 | */ |
| 49 | *esr &= ~ESR_EL2_ABORT_ISV_BIT; |
| 50 | return true; |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | static unsigned long get_dabt_write_value(struct rec *rec, unsigned long esr) |
| 56 | { |
| 57 | unsigned int rt = esr_srt(esr); |
| 58 | |
| 59 | /* Handle xzr */ |
| 60 | if (rt == 31U) { |
| 61 | return 0UL; |
| 62 | } |
| 63 | return rec->regs[rt] & access_mask(esr); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Returns 'true' if access from @rec to @addr is within the Protected IPA space. |
| 68 | */ |
| 69 | static bool access_in_rec_par(struct rec *rec, unsigned long addr) |
| 70 | { |
| 71 | /* |
| 72 | * It is OK to check only the base address of the access because: |
| 73 | * - The Protected IPA space starts at address zero. |
| 74 | * - The IPA width is below 64 bits, therefore the access cannot |
| 75 | * wrap around. |
| 76 | */ |
| 77 | return addr_in_rec_par(rec, addr); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Returns 'true' if the @ipa is in PAR and its RIPAS is 'empty'. |
| 82 | * |
| 83 | * @ipa must be aligned to the granule size. |
| 84 | */ |
| 85 | static bool ipa_is_empty(unsigned long ipa, struct rec *rec) |
| 86 | { |
AlexeiFedorov | 8b11710 | 2023-04-18 15:32:07 +0100 | [diff] [blame] | 87 | struct s2_walk_result s2_walk; |
| 88 | enum s2_walk_status walk_status; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 89 | |
| 90 | assert(GRANULE_ALIGNED(ipa)); |
| 91 | |
AlexeiFedorov | 8b11710 | 2023-04-18 15:32:07 +0100 | [diff] [blame] | 92 | walk_status = realm_ipa_to_pa(rec, ipa, &s2_walk); |
| 93 | |
| 94 | if ((walk_status != WALK_INVALID_PARAMS) && |
AlexeiFedorov | 7ddb27c | 2023-04-18 16:16:31 +0100 | [diff] [blame] | 95 | (s2_walk.ripas_val == RIPAS_EMPTY)) { |
AlexeiFedorov | 8b11710 | 2023-04-18 15:32:07 +0100 | [diff] [blame] | 96 | return true; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 97 | } |
AlexeiFedorov | 8b11710 | 2023-04-18 15:32:07 +0100 | [diff] [blame] | 98 | return false; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static bool fsc_is_external_abort(unsigned long fsc) |
| 102 | { |
| 103 | if (fsc == ESR_EL2_ABORT_FSC_SEA) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | if ((fsc >= ESR_EL2_ABORT_FSC_SEA_TTW_START) && |
| 108 | (fsc <= ESR_EL2_ABORT_FSC_SEA_TTW_END)) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Handles Data/Instruction Aborts at a lower EL with External Abort fault |
| 117 | * status code (D/IFSC). |
| 118 | * Returns 'true' if the exception is the external abort and the `rec_exit` |
| 119 | * structure is populated, 'false' otherwise. |
| 120 | */ |
| 121 | static bool handle_sync_external_abort(struct rec *rec, |
| 122 | struct rmi_rec_exit *rec_exit, |
| 123 | unsigned long esr) |
| 124 | { |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 125 | unsigned long fsc = esr & MASK(ESR_EL2_ABORT_FSC); |
| 126 | unsigned long set = esr & MASK(ESR_EL2_ABORT_SET); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 127 | |
| 128 | if (!fsc_is_external_abort(fsc)) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | switch (set) { |
| 133 | case ESR_EL2_ABORT_SET_UER: |
| 134 | /* |
| 135 | * The recoverable SEA. |
| 136 | * Inject the sync. abort into the Realm. |
| 137 | * Report the exception to the host. |
| 138 | */ |
| 139 | inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA); |
| 140 | /* |
| 141 | * Fall through. |
| 142 | */ |
| 143 | case ESR_EL2_ABORT_SET_UEO: |
| 144 | /* |
| 145 | * The restartable SEA. |
| 146 | * Report the exception to the host. |
| 147 | * The REC restarts the same instruction. |
| 148 | */ |
| 149 | rec_exit->esr = esr & ESR_NONEMULATED_ABORT_MASK; |
| 150 | |
| 151 | /* |
| 152 | * The value of the HPFAR_EL2 is not provided to the host as |
| 153 | * it is undefined for external aborts. |
| 154 | * |
| 155 | * We also don't provide the content of FAR_EL2 because it |
| 156 | * has no practical value to the host without the HPFAR_EL2. |
| 157 | */ |
| 158 | break; |
| 159 | case ESR_EL2_ABORT_SET_UC: |
| 160 | /* |
| 161 | * The uncontainable SEA. |
| 162 | * Fatal to the system. |
| 163 | */ |
| 164 | system_abort(); |
| 165 | break; |
| 166 | default: |
| 167 | assert(false); |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | void emulate_stage2_data_abort(struct rec *rec, |
| 174 | struct rmi_rec_exit *rec_exit, |
| 175 | unsigned long rtt_level) |
| 176 | { |
| 177 | unsigned long fipa = rec->regs[1]; |
| 178 | |
| 179 | assert(rtt_level <= RTT_PAGE_LEVEL); |
| 180 | |
| 181 | /* |
| 182 | * Setup Exception Syndrom Register to emulate a real data abort |
| 183 | * and return to NS host to handle it. |
| 184 | */ |
| 185 | rec_exit->esr = (ESR_EL2_EC_DATA_ABORT | |
| 186 | (ESR_EL2_ABORT_FSC_TRANSLATION_FAULT_L0 + rtt_level)); |
| 187 | rec_exit->far = 0UL; |
| 188 | rec_exit->hpfar = fipa >> HPFAR_EL2_FIPA_OFFSET; |
| 189 | rec_exit->exit_reason = RMI_EXIT_SYNC; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Returns 'true' if the abort is handled and the RMM should return to the Realm, |
| 194 | * and returns 'false' if the exception should be reported to the HS host. |
| 195 | */ |
| 196 | static bool handle_data_abort(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 197 | unsigned long esr) |
| 198 | { |
| 199 | unsigned long far = 0UL; |
| 200 | unsigned long hpfar = read_hpfar_el2(); |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 201 | unsigned long fipa = (hpfar & MASK(HPFAR_EL2_FIPA)) << HPFAR_EL2_FIPA_OFFSET; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 202 | unsigned long write_val = 0UL; |
| 203 | |
| 204 | if (handle_sync_external_abort(rec, rec_exit, esr)) { |
| 205 | /* |
| 206 | * All external aborts are immediately reported to the host. |
| 207 | */ |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * The memory access that crosses a page boundary may cause two aborts |
| 213 | * with `hpfar_el2` values referring to two consecutive pages. |
| 214 | * |
| 215 | * Insert the SEA and return to the Realm if the granule's RIPAS is EMPTY. |
| 216 | */ |
| 217 | if (ipa_is_empty(fipa, rec)) { |
| 218 | inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | if (fixup_aarch32_data_abort(rec, &esr) || |
| 223 | access_in_rec_par(rec, fipa)) { |
| 224 | esr &= ESR_NONEMULATED_ABORT_MASK; |
| 225 | goto end; |
| 226 | } |
| 227 | |
| 228 | if (esr_is_write(esr)) { |
| 229 | write_val = get_dabt_write_value(rec, esr); |
| 230 | } |
| 231 | |
| 232 | far = read_far_el2() & ~GRANULE_MASK; |
| 233 | esr &= ESR_EMULATED_ABORT_MASK; |
| 234 | |
| 235 | end: |
| 236 | rec_exit->esr = esr; |
| 237 | rec_exit->far = far; |
| 238 | rec_exit->hpfar = hpfar; |
| 239 | rec_exit->gprs[0] = write_val; |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Returns 'true' if the abort is handled and the RMM should return to the Realm, |
| 246 | * and returns 'false' if the exception should be reported to the NS host. |
| 247 | */ |
| 248 | static bool handle_instruction_abort(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 249 | unsigned long esr) |
| 250 | { |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 251 | unsigned long fsc = esr & MASK(ESR_EL2_ABORT_FSC); |
| 252 | unsigned long fsc_type = fsc & ~MASK(ESR_EL2_ABORT_FSC_LEVEL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 253 | unsigned long hpfar = read_hpfar_el2(); |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 254 | unsigned long fipa = (hpfar & MASK(HPFAR_EL2_FIPA)) << HPFAR_EL2_FIPA_OFFSET; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 255 | |
| 256 | if (handle_sync_external_abort(rec, rec_exit, esr)) { |
| 257 | /* |
| 258 | * All external aborts are immediately reported to the host. |
| 259 | */ |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Insert the SEA and return to the Realm if: |
| 265 | * - The instruction abort is at an Unprotected IPA, or |
| 266 | * - The granule's RIPAS is EMPTY |
| 267 | */ |
| 268 | if (!access_in_rec_par(rec, fipa) || ipa_is_empty(fipa, rec)) { |
| 269 | inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | if (fsc_type != ESR_EL2_ABORT_FSC_TRANSLATION_FAULT) { |
| 274 | unsigned long far = read_far_el2(); |
| 275 | |
| 276 | /* |
| 277 | * TODO: Should this ever happen, or is it an indication of an |
| 278 | * internal consistency failure in the RMM which should lead |
| 279 | * to a panic instead? |
| 280 | */ |
| 281 | |
| 282 | ERROR("Unhandled instruction abort:\n"); |
| 283 | ERROR(" FSC: %12s0x%02lx\n", " ", fsc); |
| 284 | ERROR(" FAR: %16lx\n", far); |
| 285 | ERROR(" HPFAR: %16lx\n", hpfar); |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | rec_exit->hpfar = hpfar; |
| 290 | rec_exit->esr = esr & ESR_NONEMULATED_ABORT_MASK; |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | /* |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 296 | * Handle FPU or SVE exceptions. |
| 297 | * Returns: true if the exception is handled. |
| 298 | */ |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 299 | static bool handle_simd_exception(simd_t exp_type, struct rec *rec) |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 300 | { |
| 301 | /* |
| 302 | * If the REC wants to use SVE and if SVE is not enabled for this REC |
| 303 | * then inject undefined abort. This can happen when CPU implements |
| 304 | * FEAT_SVE but the Realm didn't request this feature during creation. |
| 305 | */ |
| 306 | if (exp_type == SIMD_SVE && rec_simd_type(rec) != SIMD_SVE) { |
| 307 | realm_inject_undef_abort(); |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | /* FPU or SVE exception can happen only when REC hasn't used SIMD */ |
| 312 | assert(rec_is_simd_allowed(rec) == false); |
| 313 | |
| 314 | /* |
| 315 | * Allow the REC to use SIMD. Save NS SIMD state and restore REC SIMD |
| 316 | * state from memory to registers. |
| 317 | */ |
| 318 | simd_save_ns_state(); |
| 319 | rec_simd_enable_restore(rec); |
| 320 | |
| 321 | /* |
| 322 | * Return 'true' indicating that this exception has been handled and |
| 323 | * execution can continue. |
| 324 | */ |
| 325 | return true; |
| 326 | } |
| 327 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 328 | static void advance_pc(void) |
| 329 | { |
| 330 | unsigned long pc = read_elr_el2(); |
| 331 | |
| 332 | write_elr_el2(pc + 4UL); |
| 333 | } |
| 334 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 335 | static inline bool rsi_handler_needs_fpu(unsigned int id) |
| 336 | { |
| 337 | #ifdef RMM_FPU_USE_AT_REL2 |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 338 | if ((id == SMC_RSI_ATTEST_TOKEN_CONTINUE) || |
| 339 | (id == SMC_RSI_MEASUREMENT_EXTEND)) { |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | #endif |
| 343 | return false; |
| 344 | } |
| 345 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 346 | /* |
| 347 | * Return 'true' if execution should continue in the REC, otherwise return |
| 348 | * 'false' to go back to the NS caller of REC.Enter. |
| 349 | */ |
| 350 | static bool handle_realm_rsi(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 351 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 352 | struct rsi_result res = { 0 }; |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 353 | unsigned int function_id = (unsigned int)rec->regs[0]; |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 354 | bool restore_rec_simd_state = false; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 355 | bool needs_fpu, ret_to_rec; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 356 | |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 357 | RSI_LOG_SET(rec->regs); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 358 | |
Arunachalam Ganapathy | 937b549 | 2023-02-28 11:17:52 +0000 | [diff] [blame] | 359 | /* Ignore SVE hint bit, until RMM supports SVE hint bit */ |
| 360 | function_id &= ~MASK(SMC_SVE_HINT); |
| 361 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 362 | needs_fpu = rsi_handler_needs_fpu(function_id); |
| 363 | if (needs_fpu) { |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 364 | /* |
| 365 | * RSI handler uses FPU at REL2, so actively save REC SIMD state |
| 366 | * if REC is using SIMD or NS SIMD state. Restore the same before |
| 367 | * return from this function. |
| 368 | */ |
| 369 | if (rec_is_simd_allowed(rec)) { |
| 370 | rec_simd_save_disable(rec); |
| 371 | restore_rec_simd_state = true; |
| 372 | } else { |
| 373 | simd_save_ns_state(); |
| 374 | } |
| 375 | } else if (rec_is_simd_allowed(rec)) { |
| 376 | /* |
| 377 | * If the REC is allowed to access SIMD, then we will enter RMM |
| 378 | * with SIMD traps disabled. So enable SIMD traps as RMM by |
| 379 | * default runs with SIMD traps enabled |
| 380 | */ |
Arunachalam Ganapathy | 43c2c6b | 2023-03-24 15:06:34 +0000 | [diff] [blame] | 381 | simd_disable(); |
| 382 | } |
| 383 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 384 | switch (function_id) { |
| 385 | case SMCCC_VERSION: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 386 | res.action = UPDATE_REC_RETURN_TO_REALM; |
| 387 | res.smc_res.x[0] = SMCCC_VERSION_NUMBER; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 388 | break; |
| 389 | case SMC_RSI_ABI_VERSION: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 390 | handle_rsi_version(&res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 391 | break; |
| 392 | case SMC32_PSCI_FID_MIN ... SMC32_PSCI_FID_MAX: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 393 | case SMC64_PSCI_FID_MIN ... SMC64_PSCI_FID_MAX: |
| 394 | handle_psci(rec, rec_exit, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 395 | break; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 396 | case SMC_RSI_ATTEST_TOKEN_INIT: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 397 | handle_rsi_attest_token_init(rec, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 398 | break; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 399 | case SMC_RSI_ATTEST_TOKEN_CONTINUE: |
| 400 | handle_rsi_attest_token_continue(rec, rec_exit, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 401 | break; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 402 | case SMC_RSI_MEASUREMENT_READ: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 403 | handle_rsi_measurement_read(rec, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 404 | break; |
| 405 | case SMC_RSI_MEASUREMENT_EXTEND: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 406 | handle_rsi_measurement_extend(rec, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 407 | break; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 408 | case SMC_RSI_REALM_CONFIG: |
| 409 | handle_rsi_realm_config(rec, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 410 | break; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 411 | case SMC_RSI_IPA_STATE_SET: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 412 | handle_rsi_ipa_state_set(rec, rec_exit, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 413 | break; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 414 | case SMC_RSI_IPA_STATE_GET: |
| 415 | handle_rsi_ipa_state_get(rec, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 416 | break; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 417 | case SMC_RSI_HOST_CALL: |
| 418 | handle_rsi_host_call(rec, rec_exit, &res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 419 | break; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 420 | default: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 421 | res.action = UPDATE_REC_RETURN_TO_REALM; |
| 422 | res.smc_res.x[0] = SMC_UNKNOWN; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 426 | if (needs_fpu) { |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 427 | if (restore_rec_simd_state == true) { |
| 428 | rec_simd_enable_restore(rec); |
| 429 | } else { |
| 430 | simd_restore_ns_state(); |
| 431 | } |
| 432 | } else if (rec_is_simd_allowed(rec)) { |
Arunachalam Ganapathy | 43c2c6b | 2023-03-24 15:06:34 +0000 | [diff] [blame] | 433 | simd_enable(rec_simd_type(rec)); |
| 434 | } |
| 435 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 436 | if ((res.action & FLAG_UPDATE_REC) != 0) { |
| 437 | for (unsigned int i = 0U; i < SMC_RESULT_REGS; ++i) { |
| 438 | rec->regs[i] = res.smc_res.x[i]; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if ((res.action & FLAG_STAGE_2_ABORT) != 0) { |
| 443 | emulate_stage2_data_abort(rec, rec_exit, res.rtt_level); |
| 444 | } else { |
| 445 | advance_pc(); |
| 446 | } |
| 447 | |
| 448 | ret_to_rec = ((res.action & FLAG_EXIT_TO_HOST) == 0); |
| 449 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 450 | /* Log RSI call */ |
AlexeiFedorov | 6c11969 | 2023-04-21 12:31:15 +0100 | [diff] [blame] | 451 | RSI_LOG_EXIT(function_id, rec->regs, ret_to_rec); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 452 | return ret_to_rec; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Return 'true' if the RMM handled the exception, |
| 457 | * 'false' to return to the Non-secure host. |
| 458 | */ |
| 459 | static bool handle_exception_sync(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 460 | { |
| 461 | const unsigned long esr = read_esr_el2(); |
| 462 | |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 463 | switch (esr & MASK(ESR_EL2_EC)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 464 | case ESR_EL2_EC_WFX: |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 465 | rec_exit->esr = esr & (MASK(ESR_EL2_EC) | ESR_EL2_WFx_TI_BIT); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 466 | advance_pc(); |
| 467 | return false; |
| 468 | case ESR_EL2_EC_HVC: |
| 469 | realm_inject_undef_abort(); |
| 470 | return true; |
| 471 | case ESR_EL2_EC_SMC: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 472 | return handle_realm_rsi(rec, rec_exit); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 473 | case ESR_EL2_EC_SYSREG: { |
| 474 | bool ret = handle_sysreg_access_trap(rec, rec_exit, esr); |
AlexeiFedorov | 5b186ad | 2023-04-26 14:43:18 +0100 | [diff] [blame] | 475 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 476 | advance_pc(); |
| 477 | return ret; |
| 478 | } |
| 479 | case ESR_EL2_EC_INST_ABORT: |
| 480 | return handle_instruction_abort(rec, rec_exit, esr); |
| 481 | case ESR_EL2_EC_DATA_ABORT: |
| 482 | return handle_data_abort(rec, rec_exit, esr); |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 483 | case ESR_EL2_EC_FPU: |
| 484 | return handle_simd_exception(SIMD_FPU, rec); |
| 485 | case ESR_EL2_EC_SVE: |
| 486 | return handle_simd_exception(SIMD_SVE, rec); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 487 | default: |
| 488 | /* |
| 489 | * TODO: Check if there are other exit reasons we could |
| 490 | * encounter here and handle them appropriately |
| 491 | */ |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | VERBOSE("Unhandled sync exit ESR: %08lx (EC: %lx ISS: %lx)\n", |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 496 | esr, EXTRACT(ESR_EL2_EC, esr), EXTRACT(ESR_EL2_ISS, esr)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | * Zero values in esr, far & hpfar of 'rec_exit' structure |
| 500 | * will be returned to the NS host. |
| 501 | * The only information that may leak is when there was |
| 502 | * some unhandled/unknown reason for the exception. |
| 503 | */ |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Return 'true' if the RMM handled the exception, 'false' to return to the |
| 509 | * Non-secure host. |
| 510 | */ |
| 511 | static bool handle_exception_serror_lel(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 512 | { |
| 513 | const unsigned long esr = read_esr_el2(); |
| 514 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 515 | if ((esr & ESR_EL2_SERROR_IDS_BIT) != 0UL) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 516 | /* |
| 517 | * Implementation defined content of the esr. |
| 518 | */ |
| 519 | system_abort(); |
| 520 | } |
| 521 | |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 522 | if ((esr & MASK(ESR_EL2_SERROR_DFSC)) != ESR_EL2_SERROR_DFSC_ASYNC) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 523 | /* |
| 524 | * Either Uncategorized or Reserved fault status code. |
| 525 | */ |
| 526 | system_abort(); |
| 527 | } |
| 528 | |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 529 | switch (esr & MASK(ESR_EL2_SERROR_AET)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 530 | case ESR_EL2_SERROR_AET_UEU: /* Unrecoverable RAS Error */ |
| 531 | case ESR_EL2_SERROR_AET_UER: /* Recoverable RAS Error */ |
| 532 | /* |
| 533 | * The abort is fatal to the current S/W. Inject the SError into |
| 534 | * the Realm so it can e.g. shut down gracefully or localize the |
| 535 | * problem at the specific EL0 application. |
| 536 | * |
| 537 | * Note: Consider shutting down the Realm here to avoid |
| 538 | * the host's attack on unstable Realms. |
| 539 | */ |
| 540 | inject_serror(rec, esr); |
| 541 | /* |
| 542 | * Fall through. |
| 543 | */ |
| 544 | case ESR_EL2_SERROR_AET_CE: /* Corrected RAS Error */ |
| 545 | case ESR_EL2_SERROR_AET_UEO: /* Restartable RAS Error */ |
| 546 | /* |
| 547 | * Report the exception to the host. |
| 548 | */ |
| 549 | rec_exit->esr = esr & ESR_SERROR_MASK; |
| 550 | break; |
| 551 | case ESR_EL2_SERROR_AET_UC: /* Uncontainable RAS Error */ |
| 552 | system_abort(); |
| 553 | break; |
| 554 | default: |
| 555 | /* |
| 556 | * Unrecognized Asynchronous Error Type |
| 557 | */ |
| 558 | assert(false); |
| 559 | } |
| 560 | |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | static bool handle_exception_irq_lel(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 565 | { |
| 566 | (void)rec; |
| 567 | |
| 568 | rec_exit->exit_reason = RMI_EXIT_IRQ; |
| 569 | |
| 570 | /* |
| 571 | * With GIC all virtual interrupt programming |
| 572 | * must go via the NS hypervisor. |
| 573 | */ |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | /* Returns 'true' when returning to Realm (S) and false when to NS */ |
| 578 | bool handle_realm_exit(struct rec *rec, struct rmi_rec_exit *rec_exit, int exception) |
| 579 | { |
| 580 | switch (exception) { |
| 581 | case ARM_EXCEPTION_SYNC_LEL: { |
| 582 | bool ret; |
| 583 | |
| 584 | /* |
| 585 | * TODO: Sanitize ESR to ensure it doesn't leak sensitive |
| 586 | * information. |
| 587 | */ |
| 588 | rec_exit->exit_reason = RMI_EXIT_SYNC; |
| 589 | ret = handle_exception_sync(rec, rec_exit); |
| 590 | if (!ret) { |
| 591 | rec->last_run_info.esr = read_esr_el2(); |
| 592 | rec->last_run_info.far = read_far_el2(); |
| 593 | rec->last_run_info.hpfar = read_hpfar_el2(); |
| 594 | } |
| 595 | return ret; |
| 596 | |
| 597 | /* |
| 598 | * TODO: Much more detailed handling of exit reasons. |
| 599 | */ |
| 600 | } |
| 601 | case ARM_EXCEPTION_IRQ_LEL: |
| 602 | return handle_exception_irq_lel(rec, rec_exit); |
| 603 | case ARM_EXCEPTION_FIQ_LEL: |
| 604 | rec_exit->exit_reason = RMI_EXIT_FIQ; |
| 605 | break; |
| 606 | case ARM_EXCEPTION_SERROR_LEL: { |
| 607 | const unsigned long esr = read_esr_el2(); |
| 608 | bool ret; |
| 609 | |
| 610 | /* |
| 611 | * TODO: Sanitize ESR to ensure it doesn't leak sensitive |
| 612 | * information. |
| 613 | */ |
| 614 | rec_exit->exit_reason = RMI_EXIT_SERROR; |
| 615 | ret = handle_exception_serror_lel(rec, rec_exit); |
| 616 | if (!ret) { |
| 617 | rec->last_run_info.esr = esr; |
| 618 | rec->last_run_info.far = read_far_el2(); |
| 619 | rec->last_run_info.hpfar = read_hpfar_el2(); |
| 620 | } |
| 621 | return ret; |
| 622 | } |
| 623 | default: |
| 624 | INFO("Unrecognized exit reason: %d\n", exception); |
| 625 | break; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 626 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 627 | |
| 628 | return false; |
| 629 | } |