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> |
| 9 | #include <attestation_token.h> |
| 10 | #include <buffer.h> |
| 11 | #include <esr.h> |
| 12 | #include <exit.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 13 | #include <gic.h> |
| 14 | #include <granule.h> |
| 15 | #include <inject_exp.h> |
| 16 | #include <memory_alloc.h> |
| 17 | #include <psci.h> |
| 18 | #include <realm.h> |
| 19 | #include <realm_attest.h> |
| 20 | #include <rec.h> |
| 21 | #include <rsi-config.h> |
| 22 | #include <rsi-handler.h> |
| 23 | #include <rsi-host-call.h> |
| 24 | #include <rsi-logger.h> |
| 25 | #include <rsi-memory.h> |
| 26 | #include <rsi-walk.h> |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame^] | 27 | #include <run.h> |
| 28 | #include <simd.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 29 | #include <smc-rmi.h> |
| 30 | #include <smc-rsi.h> |
| 31 | #include <status.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 32 | #include <sysreg_traps.h> |
| 33 | #include <table.h> |
| 34 | |
| 35 | void save_fpu_state(struct fpu_state *fpu); |
| 36 | void restore_fpu_state(struct fpu_state *fpu); |
| 37 | |
| 38 | static void system_abort(void) |
| 39 | { |
| 40 | /* |
| 41 | * TODO: report the abort to the EL3. |
| 42 | * We need to establish the exact EL3 API first. |
| 43 | */ |
| 44 | assert(false); |
| 45 | } |
| 46 | |
| 47 | static bool fixup_aarch32_data_abort(struct rec *rec, unsigned long *esr) |
| 48 | { |
| 49 | unsigned long spsr = read_spsr_el2(); |
| 50 | |
| 51 | if ((spsr & SPSR_EL2_nRW_AARCH32) != 0UL) { |
| 52 | /* |
| 53 | * mmio emulation of AArch32 reads/writes is not supported. |
| 54 | */ |
| 55 | *esr &= ~ESR_EL2_ABORT_ISV_BIT; |
| 56 | return true; |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | static unsigned long get_dabt_write_value(struct rec *rec, unsigned long esr) |
| 62 | { |
| 63 | unsigned int rt = esr_srt(esr); |
| 64 | |
| 65 | /* Handle xzr */ |
| 66 | if (rt == 31U) { |
| 67 | return 0UL; |
| 68 | } |
| 69 | return rec->regs[rt] & access_mask(esr); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Returns 'true' if access from @rec to @addr is within the Protected IPA space. |
| 74 | */ |
| 75 | static bool access_in_rec_par(struct rec *rec, unsigned long addr) |
| 76 | { |
| 77 | /* |
| 78 | * It is OK to check only the base address of the access because: |
| 79 | * - The Protected IPA space starts at address zero. |
| 80 | * - The IPA width is below 64 bits, therefore the access cannot |
| 81 | * wrap around. |
| 82 | */ |
| 83 | return addr_in_rec_par(rec, addr); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Returns 'true' if the @ipa is in PAR and its RIPAS is 'empty'. |
| 88 | * |
| 89 | * @ipa must be aligned to the granule size. |
| 90 | */ |
| 91 | static bool ipa_is_empty(unsigned long ipa, struct rec *rec) |
| 92 | { |
| 93 | unsigned long s2tte, *ll_table; |
| 94 | struct rtt_walk wi; |
| 95 | enum ripas ripas; |
| 96 | bool ret; |
| 97 | |
| 98 | assert(GRANULE_ALIGNED(ipa)); |
| 99 | |
| 100 | if (!addr_in_rec_par(rec, ipa)) { |
| 101 | return false; |
| 102 | } |
| 103 | granule_lock(rec->realm_info.g_rtt, GRANULE_STATE_RTT); |
| 104 | |
| 105 | rtt_walk_lock_unlock(rec->realm_info.g_rtt, |
| 106 | rec->realm_info.s2_starting_level, |
| 107 | rec->realm_info.ipa_bits, |
| 108 | ipa, RTT_PAGE_LEVEL, &wi); |
| 109 | |
| 110 | ll_table = granule_map(wi.g_llt, SLOT_RTT); |
| 111 | s2tte = s2tte_read(&ll_table[wi.index]); |
| 112 | |
| 113 | if (s2tte_is_destroyed(s2tte)) { |
| 114 | ret = false; |
| 115 | goto out_unmap_ll_table; |
| 116 | } |
| 117 | ripas = s2tte_get_ripas(s2tte); |
Yousuf A | 6280815 | 2022-10-31 10:35:42 +0000 | [diff] [blame] | 118 | ret = (ripas == RIPAS_EMPTY); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 119 | |
| 120 | out_unmap_ll_table: |
| 121 | buffer_unmap(ll_table); |
| 122 | granule_unlock(wi.g_llt); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static bool fsc_is_external_abort(unsigned long fsc) |
| 127 | { |
| 128 | if (fsc == ESR_EL2_ABORT_FSC_SEA) { |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | if ((fsc >= ESR_EL2_ABORT_FSC_SEA_TTW_START) && |
| 133 | (fsc <= ESR_EL2_ABORT_FSC_SEA_TTW_END)) { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Handles Data/Instruction Aborts at a lower EL with External Abort fault |
| 142 | * status code (D/IFSC). |
| 143 | * Returns 'true' if the exception is the external abort and the `rec_exit` |
| 144 | * structure is populated, 'false' otherwise. |
| 145 | */ |
| 146 | static bool handle_sync_external_abort(struct rec *rec, |
| 147 | struct rmi_rec_exit *rec_exit, |
| 148 | unsigned long esr) |
| 149 | { |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 150 | unsigned long fsc = esr & MASK(ESR_EL2_ABORT_FSC); |
| 151 | unsigned long set = esr & MASK(ESR_EL2_ABORT_SET); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 152 | |
| 153 | if (!fsc_is_external_abort(fsc)) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | switch (set) { |
| 158 | case ESR_EL2_ABORT_SET_UER: |
| 159 | /* |
| 160 | * The recoverable SEA. |
| 161 | * Inject the sync. abort into the Realm. |
| 162 | * Report the exception to the host. |
| 163 | */ |
| 164 | inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA); |
| 165 | /* |
| 166 | * Fall through. |
| 167 | */ |
| 168 | case ESR_EL2_ABORT_SET_UEO: |
| 169 | /* |
| 170 | * The restartable SEA. |
| 171 | * Report the exception to the host. |
| 172 | * The REC restarts the same instruction. |
| 173 | */ |
| 174 | rec_exit->esr = esr & ESR_NONEMULATED_ABORT_MASK; |
| 175 | |
| 176 | /* |
| 177 | * The value of the HPFAR_EL2 is not provided to the host as |
| 178 | * it is undefined for external aborts. |
| 179 | * |
| 180 | * We also don't provide the content of FAR_EL2 because it |
| 181 | * has no practical value to the host without the HPFAR_EL2. |
| 182 | */ |
| 183 | break; |
| 184 | case ESR_EL2_ABORT_SET_UC: |
| 185 | /* |
| 186 | * The uncontainable SEA. |
| 187 | * Fatal to the system. |
| 188 | */ |
| 189 | system_abort(); |
| 190 | break; |
| 191 | default: |
| 192 | assert(false); |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | void emulate_stage2_data_abort(struct rec *rec, |
| 199 | struct rmi_rec_exit *rec_exit, |
| 200 | unsigned long rtt_level) |
| 201 | { |
| 202 | unsigned long fipa = rec->regs[1]; |
| 203 | |
| 204 | assert(rtt_level <= RTT_PAGE_LEVEL); |
| 205 | |
| 206 | /* |
| 207 | * Setup Exception Syndrom Register to emulate a real data abort |
| 208 | * and return to NS host to handle it. |
| 209 | */ |
| 210 | rec_exit->esr = (ESR_EL2_EC_DATA_ABORT | |
| 211 | (ESR_EL2_ABORT_FSC_TRANSLATION_FAULT_L0 + rtt_level)); |
| 212 | rec_exit->far = 0UL; |
| 213 | rec_exit->hpfar = fipa >> HPFAR_EL2_FIPA_OFFSET; |
| 214 | rec_exit->exit_reason = RMI_EXIT_SYNC; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Returns 'true' if the abort is handled and the RMM should return to the Realm, |
| 219 | * and returns 'false' if the exception should be reported to the HS host. |
| 220 | */ |
| 221 | static bool handle_data_abort(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 222 | unsigned long esr) |
| 223 | { |
| 224 | unsigned long far = 0UL; |
| 225 | unsigned long hpfar = read_hpfar_el2(); |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 226 | unsigned long fipa = (hpfar & MASK(HPFAR_EL2_FIPA)) << HPFAR_EL2_FIPA_OFFSET; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 227 | unsigned long write_val = 0UL; |
| 228 | |
| 229 | if (handle_sync_external_abort(rec, rec_exit, esr)) { |
| 230 | /* |
| 231 | * All external aborts are immediately reported to the host. |
| 232 | */ |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * The memory access that crosses a page boundary may cause two aborts |
| 238 | * with `hpfar_el2` values referring to two consecutive pages. |
| 239 | * |
| 240 | * Insert the SEA and return to the Realm if the granule's RIPAS is EMPTY. |
| 241 | */ |
| 242 | if (ipa_is_empty(fipa, rec)) { |
| 243 | inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA); |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | if (fixup_aarch32_data_abort(rec, &esr) || |
| 248 | access_in_rec_par(rec, fipa)) { |
| 249 | esr &= ESR_NONEMULATED_ABORT_MASK; |
| 250 | goto end; |
| 251 | } |
| 252 | |
| 253 | if (esr_is_write(esr)) { |
| 254 | write_val = get_dabt_write_value(rec, esr); |
| 255 | } |
| 256 | |
| 257 | far = read_far_el2() & ~GRANULE_MASK; |
| 258 | esr &= ESR_EMULATED_ABORT_MASK; |
| 259 | |
| 260 | end: |
| 261 | rec_exit->esr = esr; |
| 262 | rec_exit->far = far; |
| 263 | rec_exit->hpfar = hpfar; |
| 264 | rec_exit->gprs[0] = write_val; |
| 265 | |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Returns 'true' if the abort is handled and the RMM should return to the Realm, |
| 271 | * and returns 'false' if the exception should be reported to the NS host. |
| 272 | */ |
| 273 | static bool handle_instruction_abort(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 274 | unsigned long esr) |
| 275 | { |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 276 | unsigned long fsc = esr & MASK(ESR_EL2_ABORT_FSC); |
| 277 | unsigned long fsc_type = fsc & ~MASK(ESR_EL2_ABORT_FSC_LEVEL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 278 | unsigned long hpfar = read_hpfar_el2(); |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 279 | unsigned long fipa = (hpfar & MASK(HPFAR_EL2_FIPA)) << HPFAR_EL2_FIPA_OFFSET; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 280 | |
| 281 | if (handle_sync_external_abort(rec, rec_exit, esr)) { |
| 282 | /* |
| 283 | * All external aborts are immediately reported to the host. |
| 284 | */ |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Insert the SEA and return to the Realm if: |
| 290 | * - The instruction abort is at an Unprotected IPA, or |
| 291 | * - The granule's RIPAS is EMPTY |
| 292 | */ |
| 293 | if (!access_in_rec_par(rec, fipa) || ipa_is_empty(fipa, rec)) { |
| 294 | inject_sync_idabort(ESR_EL2_ABORT_FSC_SEA); |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | if (fsc_type != ESR_EL2_ABORT_FSC_TRANSLATION_FAULT) { |
| 299 | unsigned long far = read_far_el2(); |
| 300 | |
| 301 | /* |
| 302 | * TODO: Should this ever happen, or is it an indication of an |
| 303 | * internal consistency failure in the RMM which should lead |
| 304 | * to a panic instead? |
| 305 | */ |
| 306 | |
| 307 | ERROR("Unhandled instruction abort:\n"); |
| 308 | ERROR(" FSC: %12s0x%02lx\n", " ", fsc); |
| 309 | ERROR(" FAR: %16lx\n", far); |
| 310 | ERROR(" HPFAR: %16lx\n", hpfar); |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | rec_exit->hpfar = hpfar; |
| 315 | rec_exit->esr = esr & ESR_NONEMULATED_ABORT_MASK; |
| 316 | |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | /* |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame^] | 321 | * Handle FPU or SVE exceptions. |
| 322 | * Returns: true if the exception is handled. |
| 323 | */ |
| 324 | static bool |
| 325 | handle_simd_exception(simd_t exp_type, struct rec *rec) |
| 326 | { |
| 327 | /* |
| 328 | * If the REC wants to use SVE and if SVE is not enabled for this REC |
| 329 | * then inject undefined abort. This can happen when CPU implements |
| 330 | * FEAT_SVE but the Realm didn't request this feature during creation. |
| 331 | */ |
| 332 | if (exp_type == SIMD_SVE && rec_simd_type(rec) != SIMD_SVE) { |
| 333 | realm_inject_undef_abort(); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | /* FPU or SVE exception can happen only when REC hasn't used SIMD */ |
| 338 | assert(rec_is_simd_allowed(rec) == false); |
| 339 | |
| 340 | /* |
| 341 | * Allow the REC to use SIMD. Save NS SIMD state and restore REC SIMD |
| 342 | * state from memory to registers. |
| 343 | */ |
| 344 | simd_save_ns_state(); |
| 345 | rec_simd_enable_restore(rec); |
| 346 | |
| 347 | /* |
| 348 | * Return 'true' indicating that this exception has been handled and |
| 349 | * execution can continue. |
| 350 | */ |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 355 | * Return 'false' if no IRQ is pending, |
| 356 | * return 'true' if there is an IRQ pending, and need to return to host. |
| 357 | */ |
| 358 | static bool check_pending_irq(void) |
| 359 | { |
| 360 | unsigned long pending_irq; |
| 361 | |
| 362 | pending_irq = read_isr_el1(); |
| 363 | |
| 364 | return (pending_irq != 0UL); |
| 365 | } |
| 366 | |
| 367 | static void advance_pc(void) |
| 368 | { |
| 369 | unsigned long pc = read_elr_el2(); |
| 370 | |
| 371 | write_elr_el2(pc + 4UL); |
| 372 | } |
| 373 | |
| 374 | static void return_result_to_realm(struct rec *rec, struct smc_result result) |
| 375 | { |
| 376 | rec->regs[0] = result.x[0]; |
| 377 | rec->regs[1] = result.x[1]; |
| 378 | rec->regs[2] = result.x[2]; |
| 379 | rec->regs[3] = result.x[3]; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Return 'true' if execution should continue in the REC, otherwise return |
| 384 | * 'false' to go back to the NS caller of REC.Enter. |
| 385 | */ |
| 386 | static bool handle_realm_rsi(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 387 | { |
| 388 | bool ret_to_rec = true; /* Return to Realm */ |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 389 | unsigned int function_id = (unsigned int)rec->regs[0]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 390 | |
| 391 | RSI_LOG_SET(rec->regs[1], rec->regs[2], |
| 392 | rec->regs[3], rec->regs[4], rec->regs[5]); |
| 393 | |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 394 | /* cppcheck-suppress unsignedPositive */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 395 | if (!IS_SMC32_PSCI_FID(function_id) && !IS_SMC64_PSCI_FID(function_id) |
Arunachalam Ganapathy | 5c4411b | 2023-03-06 13:48:14 +0000 | [diff] [blame] | 396 | && !IS_SMC64_RSI_FID(function_id) |
| 397 | && !(function_id == SMCCC_VERSION)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 398 | |
| 399 | ERROR("Invalid RSI function_id = %x\n", function_id); |
| 400 | rec->regs[0] = SMC_UNKNOWN; |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | switch (function_id) { |
| 405 | case SMCCC_VERSION: |
| 406 | rec->regs[0] = SMCCC_VERSION_NUMBER; |
| 407 | break; |
| 408 | case SMC_RSI_ABI_VERSION: |
| 409 | rec->regs[0] = system_rsi_abi_version(); |
| 410 | break; |
| 411 | case SMC32_PSCI_FID_MIN ... SMC32_PSCI_FID_MAX: |
| 412 | case SMC64_PSCI_FID_MIN ... SMC64_PSCI_FID_MAX: { |
| 413 | struct psci_result res; |
| 414 | |
| 415 | res = psci_rsi(rec, |
| 416 | function_id, |
| 417 | rec->regs[1], |
| 418 | rec->regs[2], |
| 419 | rec->regs[3]); |
| 420 | |
| 421 | if (!rec->psci_info.pending) { |
| 422 | rec->regs[0] = res.smc_res.x[0]; |
| 423 | rec->regs[1] = res.smc_res.x[1]; |
| 424 | rec->regs[2] = res.smc_res.x[2]; |
| 425 | rec->regs[3] = res.smc_res.x[3]; |
| 426 | } |
| 427 | |
| 428 | if (res.hvc_forward.forward_psci_call) { |
| 429 | unsigned int i; |
| 430 | |
| 431 | rec_exit->exit_reason = RMI_EXIT_PSCI; |
| 432 | rec_exit->gprs[0] = function_id; |
| 433 | rec_exit->gprs[1] = res.hvc_forward.x1; |
| 434 | rec_exit->gprs[2] = res.hvc_forward.x2; |
| 435 | rec_exit->gprs[3] = res.hvc_forward.x3; |
| 436 | |
| 437 | for (i = 4U; i < REC_EXIT_NR_GPRS; i++) { |
| 438 | rec_exit->gprs[i] = 0UL; |
| 439 | } |
| 440 | |
| 441 | advance_pc(); |
| 442 | ret_to_rec = false; |
| 443 | } |
| 444 | break; |
| 445 | } |
| 446 | case SMC_RSI_ATTEST_TOKEN_INIT: |
| 447 | rec->regs[0] = handle_rsi_attest_token_init(rec); |
| 448 | break; |
| 449 | case SMC_RSI_ATTEST_TOKEN_CONTINUE: { |
| 450 | struct attest_result res; |
| 451 | attest_realm_token_sign_continue_start(); |
| 452 | while (true) { |
| 453 | /* |
| 454 | * Possible outcomes: |
| 455 | * if res.incomplete is true |
| 456 | * if IRQ pending |
| 457 | * check for pending IRQ and return to host |
| 458 | * else try a new iteration |
| 459 | * else |
| 460 | * if RTT table walk has failed, |
| 461 | * emulate data abort back to host |
| 462 | * otherwise |
| 463 | * return to realm because the token |
| 464 | * creation is complete or input parameter |
| 465 | * validation failed. |
| 466 | */ |
| 467 | handle_rsi_attest_token_continue(rec, &res); |
| 468 | |
| 469 | if (res.incomplete) { |
| 470 | if (check_pending_irq()) { |
| 471 | rec_exit->exit_reason = RMI_EXIT_IRQ; |
| 472 | /* Return to NS host to handle IRQ. */ |
| 473 | ret_to_rec = false; |
| 474 | break; |
| 475 | } |
| 476 | } else { |
| 477 | if (res.walk_result.abort) { |
| 478 | emulate_stage2_data_abort( |
| 479 | rec, rec_exit, |
| 480 | res.walk_result.rtt_level); |
| 481 | ret_to_rec = false; /* Exit to Host */ |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | /* Return to Realm */ |
| 486 | return_result_to_realm(rec, res.smc_res); |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | attest_realm_token_sign_continue_finish(); |
| 491 | break; |
| 492 | } |
| 493 | case SMC_RSI_MEASUREMENT_READ: |
| 494 | rec->regs[0] = handle_rsi_read_measurement(rec); |
| 495 | break; |
| 496 | case SMC_RSI_MEASUREMENT_EXTEND: |
| 497 | rec->regs[0] = handle_rsi_extend_measurement(rec); |
| 498 | break; |
| 499 | case SMC_RSI_REALM_CONFIG: { |
Arunachalam Ganapathy | dbaa886 | 2022-11-03 13:56:18 +0000 | [diff] [blame] | 500 | struct rsi_walk_smc_result res; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 501 | |
| 502 | res = handle_rsi_realm_config(rec); |
| 503 | if (res.walk_result.abort) { |
| 504 | emulate_stage2_data_abort(rec, rec_exit, |
| 505 | res.walk_result.rtt_level); |
| 506 | ret_to_rec = false; /* Exit to Host */ |
| 507 | } else { |
| 508 | /* Return to Realm */ |
| 509 | return_result_to_realm(rec, res.smc_res); |
| 510 | } |
| 511 | break; |
| 512 | } |
| 513 | case SMC_RSI_IPA_STATE_SET: |
| 514 | if (handle_rsi_ipa_state_set(rec, rec_exit)) { |
| 515 | rec->regs[0] = RSI_ERROR_INPUT; |
| 516 | } else { |
| 517 | advance_pc(); |
| 518 | ret_to_rec = false; /* Return to Host */ |
| 519 | } |
| 520 | break; |
| 521 | case SMC_RSI_IPA_STATE_GET: { |
Arunachalam Ganapathy | dbaa886 | 2022-11-03 13:56:18 +0000 | [diff] [blame] | 522 | struct rsi_walk_smc_result res; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 523 | |
Arunachalam Ganapathy | dbaa886 | 2022-11-03 13:56:18 +0000 | [diff] [blame] | 524 | res = handle_rsi_ipa_state_get(rec); |
| 525 | if (res.walk_result.abort) { |
| 526 | emulate_stage2_data_abort(rec, rec_exit, |
| 527 | res.walk_result.rtt_level); |
| 528 | /* Exit to Host */ |
| 529 | ret_to_rec = false; |
| 530 | } else { |
| 531 | /* Exit to Realm */ |
| 532 | return_result_to_realm(rec, res.smc_res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 533 | } |
| 534 | break; |
| 535 | } |
| 536 | case SMC_RSI_HOST_CALL: { |
| 537 | struct rsi_host_call_result res; |
| 538 | |
| 539 | res = handle_rsi_host_call(rec, rec_exit); |
| 540 | |
| 541 | if (res.walk_result.abort) { |
| 542 | emulate_stage2_data_abort(rec, rec_exit, |
| 543 | res.walk_result.rtt_level); |
AlexeiFedorov | 591967c | 2022-11-16 17:47:34 +0000 | [diff] [blame] | 544 | /* Exit to Host */ |
| 545 | ret_to_rec = false; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 546 | } else { |
| 547 | rec->regs[0] = res.smc_result; |
| 548 | |
| 549 | /* |
| 550 | * Return to Realm in case of error, |
| 551 | * parent function calls advance_pc() |
| 552 | */ |
| 553 | if (rec->regs[0] == RSI_SUCCESS) { |
| 554 | advance_pc(); |
| 555 | |
| 556 | /* Exit to Host */ |
| 557 | rec->host_call = true; |
| 558 | rec_exit->exit_reason = RMI_EXIT_HOST_CALL; |
| 559 | ret_to_rec = false; |
| 560 | } |
| 561 | } |
| 562 | break; |
| 563 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 564 | default: |
| 565 | rec->regs[0] = SMC_UNKNOWN; |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | /* Log RSI call */ |
| 570 | RSI_LOG_EXIT(function_id, rec->regs[0], ret_to_rec); |
| 571 | return ret_to_rec; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Return 'true' if the RMM handled the exception, |
| 576 | * 'false' to return to the Non-secure host. |
| 577 | */ |
| 578 | static bool handle_exception_sync(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 579 | { |
| 580 | const unsigned long esr = read_esr_el2(); |
| 581 | |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 582 | switch (esr & MASK(ESR_EL2_EC)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 583 | case ESR_EL2_EC_WFX: |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 584 | 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] | 585 | advance_pc(); |
| 586 | return false; |
| 587 | case ESR_EL2_EC_HVC: |
| 588 | realm_inject_undef_abort(); |
| 589 | return true; |
| 590 | case ESR_EL2_EC_SMC: |
| 591 | if (!handle_realm_rsi(rec, rec_exit)) { |
| 592 | return false; |
| 593 | } |
| 594 | /* |
| 595 | * Advance PC. |
| 596 | * HCR_EL2.TSC traps execution of the SMC instruction. |
| 597 | * It is not a routing control for the SMC exception. |
| 598 | * Trap exceptions and SMC exceptions have different |
| 599 | * preferred return addresses. |
| 600 | */ |
| 601 | advance_pc(); |
| 602 | return true; |
| 603 | case ESR_EL2_EC_SYSREG: { |
| 604 | bool ret = handle_sysreg_access_trap(rec, rec_exit, esr); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 605 | advance_pc(); |
| 606 | return ret; |
| 607 | } |
| 608 | case ESR_EL2_EC_INST_ABORT: |
| 609 | return handle_instruction_abort(rec, rec_exit, esr); |
| 610 | case ESR_EL2_EC_DATA_ABORT: |
| 611 | return handle_data_abort(rec, rec_exit, esr); |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame^] | 612 | case ESR_EL2_EC_FPU: |
| 613 | return handle_simd_exception(SIMD_FPU, rec); |
| 614 | case ESR_EL2_EC_SVE: |
| 615 | return handle_simd_exception(SIMD_SVE, rec); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 616 | default: |
| 617 | /* |
| 618 | * TODO: Check if there are other exit reasons we could |
| 619 | * encounter here and handle them appropriately |
| 620 | */ |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | VERBOSE("Unhandled sync exit ESR: %08lx (EC: %lx ISS: %lx)\n", |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 625 | esr, EXTRACT(ESR_EL2_EC, esr), EXTRACT(ESR_EL2_ISS, esr)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * Zero values in esr, far & hpfar of 'rec_exit' structure |
| 629 | * will be returned to the NS host. |
| 630 | * The only information that may leak is when there was |
| 631 | * some unhandled/unknown reason for the exception. |
| 632 | */ |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Return 'true' if the RMM handled the exception, 'false' to return to the |
| 638 | * Non-secure host. |
| 639 | */ |
| 640 | static bool handle_exception_serror_lel(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 641 | { |
| 642 | const unsigned long esr = read_esr_el2(); |
| 643 | |
| 644 | if (esr & ESR_EL2_SERROR_IDS_BIT) { |
| 645 | /* |
| 646 | * Implementation defined content of the esr. |
| 647 | */ |
| 648 | system_abort(); |
| 649 | } |
| 650 | |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 651 | if ((esr & MASK(ESR_EL2_SERROR_DFSC)) != ESR_EL2_SERROR_DFSC_ASYNC) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 652 | /* |
| 653 | * Either Uncategorized or Reserved fault status code. |
| 654 | */ |
| 655 | system_abort(); |
| 656 | } |
| 657 | |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 658 | switch (esr & MASK(ESR_EL2_SERROR_AET)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 659 | case ESR_EL2_SERROR_AET_UEU: /* Unrecoverable RAS Error */ |
| 660 | case ESR_EL2_SERROR_AET_UER: /* Recoverable RAS Error */ |
| 661 | /* |
| 662 | * The abort is fatal to the current S/W. Inject the SError into |
| 663 | * the Realm so it can e.g. shut down gracefully or localize the |
| 664 | * problem at the specific EL0 application. |
| 665 | * |
| 666 | * Note: Consider shutting down the Realm here to avoid |
| 667 | * the host's attack on unstable Realms. |
| 668 | */ |
| 669 | inject_serror(rec, esr); |
| 670 | /* |
| 671 | * Fall through. |
| 672 | */ |
| 673 | case ESR_EL2_SERROR_AET_CE: /* Corrected RAS Error */ |
| 674 | case ESR_EL2_SERROR_AET_UEO: /* Restartable RAS Error */ |
| 675 | /* |
| 676 | * Report the exception to the host. |
| 677 | */ |
| 678 | rec_exit->esr = esr & ESR_SERROR_MASK; |
| 679 | break; |
| 680 | case ESR_EL2_SERROR_AET_UC: /* Uncontainable RAS Error */ |
| 681 | system_abort(); |
| 682 | break; |
| 683 | default: |
| 684 | /* |
| 685 | * Unrecognized Asynchronous Error Type |
| 686 | */ |
| 687 | assert(false); |
| 688 | } |
| 689 | |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | static bool handle_exception_irq_lel(struct rec *rec, struct rmi_rec_exit *rec_exit) |
| 694 | { |
| 695 | (void)rec; |
| 696 | |
| 697 | rec_exit->exit_reason = RMI_EXIT_IRQ; |
| 698 | |
| 699 | /* |
| 700 | * With GIC all virtual interrupt programming |
| 701 | * must go via the NS hypervisor. |
| 702 | */ |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | /* Returns 'true' when returning to Realm (S) and false when to NS */ |
| 707 | bool handle_realm_exit(struct rec *rec, struct rmi_rec_exit *rec_exit, int exception) |
| 708 | { |
| 709 | switch (exception) { |
| 710 | case ARM_EXCEPTION_SYNC_LEL: { |
| 711 | bool ret; |
| 712 | |
| 713 | /* |
| 714 | * TODO: Sanitize ESR to ensure it doesn't leak sensitive |
| 715 | * information. |
| 716 | */ |
| 717 | rec_exit->exit_reason = RMI_EXIT_SYNC; |
| 718 | ret = handle_exception_sync(rec, rec_exit); |
| 719 | if (!ret) { |
| 720 | rec->last_run_info.esr = read_esr_el2(); |
| 721 | rec->last_run_info.far = read_far_el2(); |
| 722 | rec->last_run_info.hpfar = read_hpfar_el2(); |
| 723 | } |
| 724 | return ret; |
| 725 | |
| 726 | /* |
| 727 | * TODO: Much more detailed handling of exit reasons. |
| 728 | */ |
| 729 | } |
| 730 | case ARM_EXCEPTION_IRQ_LEL: |
| 731 | return handle_exception_irq_lel(rec, rec_exit); |
| 732 | case ARM_EXCEPTION_FIQ_LEL: |
| 733 | rec_exit->exit_reason = RMI_EXIT_FIQ; |
| 734 | break; |
| 735 | case ARM_EXCEPTION_SERROR_LEL: { |
| 736 | const unsigned long esr = read_esr_el2(); |
| 737 | bool ret; |
| 738 | |
| 739 | /* |
| 740 | * TODO: Sanitize ESR to ensure it doesn't leak sensitive |
| 741 | * information. |
| 742 | */ |
| 743 | rec_exit->exit_reason = RMI_EXIT_SERROR; |
| 744 | ret = handle_exception_serror_lel(rec, rec_exit); |
| 745 | if (!ret) { |
| 746 | rec->last_run_info.esr = esr; |
| 747 | rec->last_run_info.far = read_far_el2(); |
| 748 | rec->last_run_info.hpfar = read_hpfar_el2(); |
| 749 | } |
| 750 | return ret; |
| 751 | } |
| 752 | default: |
| 753 | INFO("Unrecognized exit reason: %d\n", exception); |
| 754 | break; |
| 755 | }; |
| 756 | |
| 757 | return false; |
| 758 | } |