1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015 - ARM Ltd 4 * Author: Marc Zyngier <[email protected]> 5 */ 6 7 #ifndef __ARM64_KVM_HYP_SWITCH_H__ 8 #define __ARM64_KVM_HYP_SWITCH_H__ 9 10 #include <hyp/adjust_pc.h> 11 #include <hyp/fault.h> 12 13 #include <linux/arm-smccc.h> 14 #include <linux/kvm_host.h> 15 #include <linux/types.h> 16 #include <linux/jump_label.h> 17 #include <uapi/linux/psci.h> 18 19 #include <kvm/arm_psci.h> 20 21 #include <asm/barrier.h> 22 #include <asm/cpufeature.h> 23 #include <asm/extable.h> 24 #include <asm/kprobes.h> 25 #include <asm/kvm_asm.h> 26 #include <asm/kvm_emulate.h> 27 #include <asm/kvm_hyp.h> 28 #include <asm/kvm_mmu.h> 29 #include <asm/kvm_nested.h> 30 #include <asm/kvm_ptrauth.h> 31 #include <asm/fpsimd.h> 32 #include <asm/debug-monitors.h> 33 #include <asm/processor.h> 34 #include <asm/traps.h> 35 36 struct kvm_exception_table_entry { 37 int insn, fixup; 38 }; 39 40 extern struct kvm_exception_table_entry __start___kvm_ex_table; 41 extern struct kvm_exception_table_entry __stop___kvm_ex_table; 42 43 /* Save the 32-bit only FPSIMD system register state */ 44 static inline void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu) 45 { 46 if (!vcpu_el1_is_32bit(vcpu)) 47 return; 48 49 __vcpu_sys_reg(vcpu, FPEXC32_EL2) = read_sysreg(fpexc32_el2); 50 } 51 52 static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu) 53 { 54 /* 55 * We are about to set CPTR_EL2.TFP to trap all floating point 56 * register accesses to EL2, however, the ARM ARM clearly states that 57 * traps are only taken to EL2 if the operation would not otherwise 58 * trap to EL1. Therefore, always make sure that for 32-bit guests, 59 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit. 60 * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to 61 * it will cause an exception. 62 */ 63 if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) { 64 write_sysreg(1 << 30, fpexc32_el2); 65 isb(); 66 } 67 } 68 69 #define compute_clr_set(vcpu, reg, clr, set) \ 70 do { \ 71 u64 hfg; \ 72 hfg = __vcpu_sys_reg(vcpu, reg) & ~__ ## reg ## _RES0; \ 73 set |= hfg & __ ## reg ## _MASK; \ 74 clr |= ~hfg & __ ## reg ## _nMASK; \ 75 } while(0) 76 77 #define reg_to_fgt_group_id(reg) \ 78 ({ \ 79 enum fgt_group_id id; \ 80 switch(reg) { \ 81 case HFGRTR_EL2: \ 82 case HFGWTR_EL2: \ 83 id = HFGxTR_GROUP; \ 84 break; \ 85 case HFGITR_EL2: \ 86 id = HFGITR_GROUP; \ 87 break; \ 88 case HDFGRTR_EL2: \ 89 case HDFGWTR_EL2: \ 90 id = HDFGRTR_GROUP; \ 91 break; \ 92 case HAFGRTR_EL2: \ 93 id = HAFGRTR_GROUP; \ 94 break; \ 95 default: \ 96 BUILD_BUG_ON(1); \ 97 } \ 98 \ 99 id; \ 100 }) 101 102 #define compute_undef_clr_set(vcpu, kvm, reg, clr, set) \ 103 do { \ 104 u64 hfg = kvm->arch.fgu[reg_to_fgt_group_id(reg)]; \ 105 set |= hfg & __ ## reg ## _MASK; \ 106 clr |= hfg & __ ## reg ## _nMASK; \ 107 } while(0) 108 109 #define update_fgt_traps_cs(hctxt, vcpu, kvm, reg, clr, set) \ 110 do { \ 111 u64 c = 0, s = 0; \ 112 \ 113 ctxt_sys_reg(hctxt, reg) = read_sysreg_s(SYS_ ## reg); \ 114 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) \ 115 compute_clr_set(vcpu, reg, c, s); \ 116 \ 117 compute_undef_clr_set(vcpu, kvm, reg, c, s); \ 118 \ 119 s |= set; \ 120 c |= clr; \ 121 if (c || s) { \ 122 u64 val = __ ## reg ## _nMASK; \ 123 val |= s; \ 124 val &= ~c; \ 125 write_sysreg_s(val, SYS_ ## reg); \ 126 } \ 127 } while(0) 128 129 #define update_fgt_traps(hctxt, vcpu, kvm, reg) \ 130 update_fgt_traps_cs(hctxt, vcpu, kvm, reg, 0, 0) 131 132 /* 133 * Validate the fine grain trap masks. 134 * Check that the masks do not overlap and that all bits are accounted for. 135 */ 136 #define CHECK_FGT_MASKS(reg) \ 137 do { \ 138 BUILD_BUG_ON((__ ## reg ## _MASK) & (__ ## reg ## _nMASK)); \ 139 BUILD_BUG_ON(~((__ ## reg ## _RES0) ^ (__ ## reg ## _MASK) ^ \ 140 (__ ## reg ## _nMASK))); \ 141 } while(0) 142 143 static inline bool cpu_has_amu(void) 144 { 145 u64 pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1); 146 147 return cpuid_feature_extract_unsigned_field(pfr0, 148 ID_AA64PFR0_EL1_AMU_SHIFT); 149 } 150 151 static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu) 152 { 153 struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt); 154 struct kvm *kvm = kern_hyp_va(vcpu->kvm); 155 156 CHECK_FGT_MASKS(HFGRTR_EL2); 157 CHECK_FGT_MASKS(HFGWTR_EL2); 158 CHECK_FGT_MASKS(HFGITR_EL2); 159 CHECK_FGT_MASKS(HDFGRTR_EL2); 160 CHECK_FGT_MASKS(HDFGWTR_EL2); 161 CHECK_FGT_MASKS(HAFGRTR_EL2); 162 CHECK_FGT_MASKS(HCRX_EL2); 163 164 if (!cpus_have_final_cap(ARM64_HAS_FGT)) 165 return; 166 167 update_fgt_traps(hctxt, vcpu, kvm, HFGRTR_EL2); 168 update_fgt_traps_cs(hctxt, vcpu, kvm, HFGWTR_EL2, 0, 169 cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) ? 170 HFGxTR_EL2_TCR_EL1_MASK : 0); 171 update_fgt_traps(hctxt, vcpu, kvm, HFGITR_EL2); 172 update_fgt_traps(hctxt, vcpu, kvm, HDFGRTR_EL2); 173 update_fgt_traps(hctxt, vcpu, kvm, HDFGWTR_EL2); 174 175 if (cpu_has_amu()) 176 update_fgt_traps(hctxt, vcpu, kvm, HAFGRTR_EL2); 177 } 178 179 #define __deactivate_fgt(htcxt, vcpu, kvm, reg) \ 180 do { \ 181 if ((vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) || \ 182 kvm->arch.fgu[reg_to_fgt_group_id(reg)]) \ 183 write_sysreg_s(ctxt_sys_reg(hctxt, reg), \ 184 SYS_ ## reg); \ 185 } while(0) 186 187 static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu) 188 { 189 struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt); 190 struct kvm *kvm = kern_hyp_va(vcpu->kvm); 191 192 if (!cpus_have_final_cap(ARM64_HAS_FGT)) 193 return; 194 195 __deactivate_fgt(hctxt, vcpu, kvm, HFGRTR_EL2); 196 if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38)) 197 write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2); 198 else 199 __deactivate_fgt(hctxt, vcpu, kvm, HFGWTR_EL2); 200 __deactivate_fgt(hctxt, vcpu, kvm, HFGITR_EL2); 201 __deactivate_fgt(hctxt, vcpu, kvm, HDFGRTR_EL2); 202 __deactivate_fgt(hctxt, vcpu, kvm, HDFGWTR_EL2); 203 204 if (cpu_has_amu()) 205 __deactivate_fgt(hctxt, vcpu, kvm, HAFGRTR_EL2); 206 } 207 208 static inline void __activate_traps_common(struct kvm_vcpu *vcpu) 209 { 210 /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */ 211 write_sysreg(1 << 15, hstr_el2); 212 213 /* 214 * Make sure we trap PMU access from EL0 to EL2. Also sanitize 215 * PMSELR_EL0 to make sure it never contains the cycle 216 * counter, which could make a PMXEVCNTR_EL0 access UNDEF at 217 * EL1 instead of being trapped to EL2. 218 */ 219 if (kvm_arm_support_pmu_v3()) { 220 struct kvm_cpu_context *hctxt; 221 222 write_sysreg(0, pmselr_el0); 223 224 hctxt = host_data_ptr(host_ctxt); 225 ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0); 226 write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0); 227 vcpu_set_flag(vcpu, PMUSERENR_ON_CPU); 228 } 229 230 *host_data_ptr(host_debug_state.mdcr_el2) = read_sysreg(mdcr_el2); 231 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2); 232 233 if (cpus_have_final_cap(ARM64_HAS_HCX)) { 234 u64 hcrx = vcpu->arch.hcrx_el2; 235 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) { 236 u64 clr = 0, set = 0; 237 238 compute_clr_set(vcpu, HCRX_EL2, clr, set); 239 240 hcrx |= set; 241 hcrx &= ~clr; 242 } 243 244 write_sysreg_s(hcrx, SYS_HCRX_EL2); 245 } 246 247 __activate_traps_hfgxtr(vcpu); 248 } 249 250 static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu) 251 { 252 write_sysreg(*host_data_ptr(host_debug_state.mdcr_el2), mdcr_el2); 253 254 write_sysreg(0, hstr_el2); 255 if (kvm_arm_support_pmu_v3()) { 256 struct kvm_cpu_context *hctxt; 257 258 hctxt = host_data_ptr(host_ctxt); 259 write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0); 260 vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU); 261 } 262 263 if (cpus_have_final_cap(ARM64_HAS_HCX)) 264 write_sysreg_s(HCRX_HOST_FLAGS, SYS_HCRX_EL2); 265 266 __deactivate_traps_hfgxtr(vcpu); 267 } 268 269 static inline void ___activate_traps(struct kvm_vcpu *vcpu, u64 hcr) 270 { 271 if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM)) 272 hcr |= HCR_TVM; 273 274 write_sysreg(hcr, hcr_el2); 275 276 if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE)) 277 write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2); 278 } 279 280 static inline void ___deactivate_traps(struct kvm_vcpu *vcpu) 281 { 282 /* 283 * If we pended a virtual abort, preserve it until it gets 284 * cleared. See D1.14.3 (Virtual Interrupts) for details, but 285 * the crucial bit is "On taking a vSError interrupt, 286 * HCR_EL2.VSE is cleared to 0." 287 */ 288 if (vcpu->arch.hcr_el2 & HCR_VSE) { 289 vcpu->arch.hcr_el2 &= ~HCR_VSE; 290 vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE; 291 } 292 } 293 294 static inline bool __populate_fault_info(struct kvm_vcpu *vcpu) 295 { 296 return __get_fault_info(vcpu->arch.fault.esr_el2, &vcpu->arch.fault); 297 } 298 299 static bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code) 300 { 301 *vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR); 302 arm64_mops_reset_regs(vcpu_gp_regs(vcpu), vcpu->arch.fault.esr_el2); 303 write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR); 304 305 /* 306 * Finish potential single step before executing the prologue 307 * instruction. 308 */ 309 *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS; 310 write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR); 311 312 return true; 313 } 314 315 static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu) 316 { 317 /* 318 * The vCPU's saved SVE state layout always matches the max VL of the 319 * vCPU. Start off with the max VL so we can load the SVE state. 320 */ 321 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2); 322 __sve_restore_state(vcpu_sve_pffr(vcpu), 323 &vcpu->arch.ctxt.fp_regs.fpsr, 324 true); 325 326 /* 327 * The effective VL for a VM could differ from the max VL when running a 328 * nested guest, as the guest hypervisor could select a smaller VL. Slap 329 * that into hardware before wrapping up. 330 */ 331 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) 332 sve_cond_update_zcr_vq(__vcpu_sys_reg(vcpu, ZCR_EL2), SYS_ZCR_EL2); 333 334 write_sysreg_el1(__vcpu_sys_reg(vcpu, vcpu_sve_zcr_elx(vcpu)), SYS_ZCR); 335 } 336 337 static inline void __hyp_sve_save_host(void) 338 { 339 struct cpu_sve_state *sve_state = *host_data_ptr(sve_state); 340 341 sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR); 342 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2); 343 __sve_save_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl), 344 &sve_state->fpsr, 345 true); 346 } 347 348 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu); 349 350 /* 351 * We trap the first access to the FP/SIMD to save the host context and 352 * restore the guest context lazily. 353 * If FP/SIMD is not implemented, handle the trap and inject an undefined 354 * instruction exception to the guest. Similarly for trapped SVE accesses. 355 */ 356 static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code) 357 { 358 bool sve_guest; 359 u8 esr_ec; 360 361 if (!system_supports_fpsimd()) 362 return false; 363 364 sve_guest = vcpu_has_sve(vcpu); 365 esr_ec = kvm_vcpu_trap_get_class(vcpu); 366 367 /* Only handle traps the vCPU can support here: */ 368 switch (esr_ec) { 369 case ESR_ELx_EC_FP_ASIMD: 370 /* Forward traps to the guest hypervisor as required */ 371 if (guest_hyp_fpsimd_traps_enabled(vcpu)) 372 return false; 373 break; 374 case ESR_ELx_EC_SYS64: 375 if (WARN_ON_ONCE(!is_hyp_ctxt(vcpu))) 376 return false; 377 fallthrough; 378 case ESR_ELx_EC_SVE: 379 if (!sve_guest) 380 return false; 381 if (guest_hyp_sve_traps_enabled(vcpu)) 382 return false; 383 break; 384 default: 385 return false; 386 } 387 388 /* Valid trap. Switch the context: */ 389 390 /* First disable enough traps to allow us to update the registers */ 391 if (sve_guest || (is_protected_kvm_enabled() && system_supports_sve())) 392 cpacr_clear_set(0, CPACR_ELx_FPEN | CPACR_ELx_ZEN); 393 else 394 cpacr_clear_set(0, CPACR_ELx_FPEN); 395 isb(); 396 397 /* Write out the host state if it's in the registers */ 398 if (host_owns_fp_regs()) 399 kvm_hyp_save_fpsimd_host(vcpu); 400 401 /* Restore the guest state */ 402 if (sve_guest) 403 __hyp_sve_restore_guest(vcpu); 404 else 405 __fpsimd_restore_state(&vcpu->arch.ctxt.fp_regs); 406 407 /* Skip restoring fpexc32 for AArch64 guests */ 408 if (!(read_sysreg(hcr_el2) & HCR_RW)) 409 write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2); 410 411 *host_data_ptr(fp_owner) = FP_STATE_GUEST_OWNED; 412 413 return true; 414 } 415 416 static inline bool handle_tx2_tvm(struct kvm_vcpu *vcpu) 417 { 418 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu)); 419 int rt = kvm_vcpu_sys_get_rt(vcpu); 420 u64 val = vcpu_get_reg(vcpu, rt); 421 422 /* 423 * The normal sysreg handling code expects to see the traps, 424 * let's not do anything here. 425 */ 426 if (vcpu->arch.hcr_el2 & HCR_TVM) 427 return false; 428 429 switch (sysreg) { 430 case SYS_SCTLR_EL1: 431 write_sysreg_el1(val, SYS_SCTLR); 432 break; 433 case SYS_TTBR0_EL1: 434 write_sysreg_el1(val, SYS_TTBR0); 435 break; 436 case SYS_TTBR1_EL1: 437 write_sysreg_el1(val, SYS_TTBR1); 438 break; 439 case SYS_TCR_EL1: 440 write_sysreg_el1(val, SYS_TCR); 441 break; 442 case SYS_ESR_EL1: 443 write_sysreg_el1(val, SYS_ESR); 444 break; 445 case SYS_FAR_EL1: 446 write_sysreg_el1(val, SYS_FAR); 447 break; 448 case SYS_AFSR0_EL1: 449 write_sysreg_el1(val, SYS_AFSR0); 450 break; 451 case SYS_AFSR1_EL1: 452 write_sysreg_el1(val, SYS_AFSR1); 453 break; 454 case SYS_MAIR_EL1: 455 write_sysreg_el1(val, SYS_MAIR); 456 break; 457 case SYS_AMAIR_EL1: 458 write_sysreg_el1(val, SYS_AMAIR); 459 break; 460 case SYS_CONTEXTIDR_EL1: 461 write_sysreg_el1(val, SYS_CONTEXTIDR); 462 break; 463 default: 464 return false; 465 } 466 467 __kvm_skip_instr(vcpu); 468 return true; 469 } 470 471 static bool kvm_hyp_handle_cntpct(struct kvm_vcpu *vcpu) 472 { 473 struct arch_timer_context *ctxt; 474 u32 sysreg; 475 u64 val; 476 477 /* 478 * We only get here for 64bit guests, 32bit guests will hit 479 * the long and winding road all the way to the standard 480 * handling. Yes, it sucks to be irrelevant. 481 */ 482 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu)); 483 484 switch (sysreg) { 485 case SYS_CNTPCT_EL0: 486 case SYS_CNTPCTSS_EL0: 487 if (vcpu_has_nv(vcpu)) { 488 if (is_hyp_ctxt(vcpu)) { 489 ctxt = vcpu_hptimer(vcpu); 490 break; 491 } 492 493 /* Check for guest hypervisor trapping */ 494 val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2); 495 if (!vcpu_el2_e2h_is_set(vcpu)) 496 val = (val & CNTHCTL_EL1PCTEN) << 10; 497 498 if (!(val & (CNTHCTL_EL1PCTEN << 10))) 499 return false; 500 } 501 502 ctxt = vcpu_ptimer(vcpu); 503 break; 504 default: 505 return false; 506 } 507 508 val = arch_timer_read_cntpct_el0(); 509 510 if (ctxt->offset.vm_offset) 511 val -= *kern_hyp_va(ctxt->offset.vm_offset); 512 if (ctxt->offset.vcpu_offset) 513 val -= *kern_hyp_va(ctxt->offset.vcpu_offset); 514 515 vcpu_set_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu), val); 516 __kvm_skip_instr(vcpu); 517 return true; 518 } 519 520 static bool handle_ampere1_tcr(struct kvm_vcpu *vcpu) 521 { 522 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu)); 523 int rt = kvm_vcpu_sys_get_rt(vcpu); 524 u64 val = vcpu_get_reg(vcpu, rt); 525 526 if (sysreg != SYS_TCR_EL1) 527 return false; 528 529 /* 530 * Affected parts do not advertise support for hardware Access Flag / 531 * Dirty state management in ID_AA64MMFR1_EL1.HAFDBS, but the underlying 532 * control bits are still functional. The architecture requires these be 533 * RES0 on systems that do not implement FEAT_HAFDBS. 534 * 535 * Uphold the requirements of the architecture by masking guest writes 536 * to TCR_EL1.{HA,HD} here. 537 */ 538 val &= ~(TCR_HD | TCR_HA); 539 write_sysreg_el1(val, SYS_TCR); 540 __kvm_skip_instr(vcpu); 541 return true; 542 } 543 544 static bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code) 545 { 546 if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) && 547 handle_tx2_tvm(vcpu)) 548 return true; 549 550 if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) && 551 handle_ampere1_tcr(vcpu)) 552 return true; 553 554 if (static_branch_unlikely(&vgic_v3_cpuif_trap) && 555 __vgic_v3_perform_cpuif_access(vcpu) == 1) 556 return true; 557 558 if (kvm_hyp_handle_cntpct(vcpu)) 559 return true; 560 561 return false; 562 } 563 564 static bool kvm_hyp_handle_cp15_32(struct kvm_vcpu *vcpu, u64 *exit_code) 565 { 566 if (static_branch_unlikely(&vgic_v3_cpuif_trap) && 567 __vgic_v3_perform_cpuif_access(vcpu) == 1) 568 return true; 569 570 return false; 571 } 572 573 static bool kvm_hyp_handle_memory_fault(struct kvm_vcpu *vcpu, u64 *exit_code) 574 { 575 if (!__populate_fault_info(vcpu)) 576 return true; 577 578 return false; 579 } 580 static bool kvm_hyp_handle_iabt_low(struct kvm_vcpu *vcpu, u64 *exit_code) 581 __alias(kvm_hyp_handle_memory_fault); 582 static bool kvm_hyp_handle_watchpt_low(struct kvm_vcpu *vcpu, u64 *exit_code) 583 __alias(kvm_hyp_handle_memory_fault); 584 585 static bool kvm_hyp_handle_dabt_low(struct kvm_vcpu *vcpu, u64 *exit_code) 586 { 587 if (kvm_hyp_handle_memory_fault(vcpu, exit_code)) 588 return true; 589 590 if (static_branch_unlikely(&vgic_v2_cpuif_trap)) { 591 bool valid; 592 593 valid = kvm_vcpu_trap_is_translation_fault(vcpu) && 594 kvm_vcpu_dabt_isvalid(vcpu) && 595 !kvm_vcpu_abt_issea(vcpu) && 596 !kvm_vcpu_abt_iss1tw(vcpu); 597 598 if (valid) { 599 int ret = __vgic_v2_perform_cpuif_access(vcpu); 600 601 if (ret == 1) 602 return true; 603 604 /* Promote an illegal access to an SError.*/ 605 if (ret == -1) 606 *exit_code = ARM_EXCEPTION_EL1_SERROR; 607 } 608 } 609 610 return false; 611 } 612 613 typedef bool (*exit_handler_fn)(struct kvm_vcpu *, u64 *); 614 615 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu); 616 617 static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code); 618 619 /* 620 * Allow the hypervisor to handle the exit with an exit handler if it has one. 621 * 622 * Returns true if the hypervisor handled the exit, and control should go back 623 * to the guest, or false if it hasn't. 624 */ 625 static inline bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu, u64 *exit_code) 626 { 627 const exit_handler_fn *handlers = kvm_get_exit_handler_array(vcpu); 628 exit_handler_fn fn; 629 630 fn = handlers[kvm_vcpu_trap_get_class(vcpu)]; 631 632 if (fn) 633 return fn(vcpu, exit_code); 634 635 return false; 636 } 637 638 static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu, u64 *exit_code) 639 { 640 /* 641 * Check for the conditions of Cortex-A510's #2077057. When these occur 642 * SPSR_EL2 can't be trusted, but isn't needed either as it is 643 * unchanged from the value in vcpu_gp_regs(vcpu)->pstate. 644 * Are we single-stepping the guest, and took a PAC exception from the 645 * active-not-pending state? 646 */ 647 if (cpus_have_final_cap(ARM64_WORKAROUND_2077057) && 648 vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && 649 *vcpu_cpsr(vcpu) & DBG_SPSR_SS && 650 ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) == ESR_ELx_EC_PAC) 651 write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR); 652 653 vcpu->arch.ctxt.regs.pstate = read_sysreg_el2(SYS_SPSR); 654 } 655 656 /* 657 * Return true when we were able to fixup the guest exit and should return to 658 * the guest, false when we should restore the host state and return to the 659 * main run loop. 660 */ 661 static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code) 662 { 663 /* 664 * Save PSTATE early so that we can evaluate the vcpu mode 665 * early on. 666 */ 667 synchronize_vcpu_pstate(vcpu, exit_code); 668 669 /* 670 * Check whether we want to repaint the state one way or 671 * another. 672 */ 673 early_exit_filter(vcpu, exit_code); 674 675 if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ) 676 vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR); 677 678 if (ARM_SERROR_PENDING(*exit_code) && 679 ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ) { 680 u8 esr_ec = kvm_vcpu_trap_get_class(vcpu); 681 682 /* 683 * HVC already have an adjusted PC, which we need to 684 * correct in order to return to after having injected 685 * the SError. 686 * 687 * SMC, on the other hand, is *trapped*, meaning its 688 * preferred return address is the SMC itself. 689 */ 690 if (esr_ec == ESR_ELx_EC_HVC32 || esr_ec == ESR_ELx_EC_HVC64) 691 write_sysreg_el2(read_sysreg_el2(SYS_ELR) - 4, SYS_ELR); 692 } 693 694 /* 695 * We're using the raw exception code in order to only process 696 * the trap if no SError is pending. We will come back to the 697 * same PC once the SError has been injected, and replay the 698 * trapping instruction. 699 */ 700 if (*exit_code != ARM_EXCEPTION_TRAP) 701 goto exit; 702 703 /* Check if there's an exit handler and allow it to handle the exit. */ 704 if (kvm_hyp_handle_exit(vcpu, exit_code)) 705 goto guest; 706 exit: 707 /* Return to the host kernel and handle the exit */ 708 return false; 709 710 guest: 711 /* Re-enter the guest */ 712 asm(ALTERNATIVE("nop", "dmb sy", ARM64_WORKAROUND_1508412)); 713 return true; 714 } 715 716 static inline void __kvm_unexpected_el2_exception(void) 717 { 718 extern char __guest_exit_restore_elr_and_panic[]; 719 unsigned long addr, fixup; 720 struct kvm_exception_table_entry *entry, *end; 721 unsigned long elr_el2 = read_sysreg(elr_el2); 722 723 entry = &__start___kvm_ex_table; 724 end = &__stop___kvm_ex_table; 725 726 while (entry < end) { 727 addr = (unsigned long)&entry->insn + entry->insn; 728 fixup = (unsigned long)&entry->fixup + entry->fixup; 729 730 if (addr != elr_el2) { 731 entry++; 732 continue; 733 } 734 735 write_sysreg(fixup, elr_el2); 736 return; 737 } 738 739 /* Trigger a panic after restoring the hyp context. */ 740 this_cpu_ptr(&kvm_hyp_ctxt)->sys_regs[ELR_EL2] = elr_el2; 741 write_sysreg(__guest_exit_restore_elr_and_panic, elr_el2); 742 } 743 744 #endif /* __ARM64_KVM_HYP_SWITCH_H__ */ 745