1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015 - ARM Ltd 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 */ 6 7 #include <hyp/switch.h> 8 9 #include <linux/arm-smccc.h> 10 #include <linux/kvm_host.h> 11 #include <linux/types.h> 12 #include <linux/jump_label.h> 13 #include <linux/percpu.h> 14 #include <uapi/linux/psci.h> 15 16 #include <kvm/arm_psci.h> 17 18 #include <asm/barrier.h> 19 #include <asm/cpufeature.h> 20 #include <asm/kprobes.h> 21 #include <asm/kvm_asm.h> 22 #include <asm/kvm_emulate.h> 23 #include <asm/kvm_hyp.h> 24 #include <asm/kvm_mmu.h> 25 #include <asm/fpsimd.h> 26 #include <asm/debug-monitors.h> 27 #include <asm/processor.h> 28 #include <asm/thread_info.h> 29 #include <asm/vectors.h> 30 31 /* VHE specific context */ 32 DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); 33 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 34 DEFINE_PER_CPU(unsigned long, kvm_hyp_vector); 35 36 /* 37 * HCR_EL2 bits that the NV guest can freely change (no RES0/RES1 38 * semantics, irrespective of the configuration), but that cannot be 39 * applied to the actual HW as things would otherwise break badly. 40 * 41 * - TGE: we want the guest to use EL1, which is incompatible with 42 * this bit being set 43 * 44 * - API/APK: they are already accounted for by vcpu_load(), and can 45 * only take effect across a load/put cycle (such as ERET) 46 * 47 * - FIEN: no way we let a guest have access to the RAS "Common Fault 48 * Injection" thing, whatever that does 49 */ 50 #define NV_HCR_GUEST_EXCLUDE (HCR_TGE | HCR_API | HCR_APK | HCR_FIEN) 51 52 static u64 __compute_hcr(struct kvm_vcpu *vcpu) 53 { 54 u64 guest_hcr, hcr = vcpu->arch.hcr_el2; 55 56 if (!vcpu_has_nv(vcpu)) 57 return hcr; 58 59 /* 60 * We rely on the invariant that a vcpu entered from HYP 61 * context must also exit in the same context, as only an ERET 62 * instruction can kick us out of it, and we obviously trap 63 * that sucker. PSTATE.M will get fixed-up on exit. 64 */ 65 if (is_hyp_ctxt(vcpu)) { 66 host_data_set_flag(VCPU_IN_HYP_CONTEXT); 67 68 hcr |= HCR_NV | HCR_NV2 | HCR_AT | HCR_TTLB; 69 70 if (!vcpu_el2_e2h_is_set(vcpu)) 71 hcr |= HCR_NV1; 72 73 /* Publish the guest's view of HCR_EL2 to the HW */ 74 if (cpus_have_final_cap(ARM64_HAS_NV3) && vcpu_el2_e2h_is_set(vcpu)) 75 write_sysreg_s(__vcpu_sys_reg(vcpu, HCR_EL2), SYS_NVHCR_EL2); 76 else 77 __vcpu_assign_sys_reg(vcpu, NVHCR_EL2, __vcpu_sys_reg(vcpu, HCR_EL2)); 78 79 /* 80 * Nothing in HCR_EL2 should impact running in hypervisor 81 * context, apart from bits we have defined as RESx (E2H, 82 * HCD and co), or that cannot be set directly (the EXCLUDE 83 * bits). Given that we OR the guest's view with the host's, 84 * we can use the 0 value as the starting point, and only 85 * use the config-driven RES1 bits. 86 */ 87 guest_hcr = kvm_vcpu_apply_reg_masks(vcpu, HCR_EL2, 0); 88 89 write_sysreg_s(vcpu->arch.ctxt.vncr_array, SYS_VNCR_EL2); 90 } else { 91 host_data_clear_flag(VCPU_IN_HYP_CONTEXT); 92 93 guest_hcr = __vcpu_sys_reg(vcpu, HCR_EL2); 94 if (guest_hcr & HCR_NV) { 95 u64 va = __fix_to_virt(vncr_fixmap(smp_processor_id())); 96 97 /* Inherit the low bits from the actual register */ 98 va |= __vcpu_sys_reg(vcpu, VNCR_EL2) & GENMASK(PAGE_SHIFT - 1, 0); 99 write_sysreg_s(va, SYS_VNCR_EL2); 100 101 /* Force NV2 in case the guest is forgetful... */ 102 guest_hcr |= HCR_NV2; 103 } 104 105 /* 106 * Exclude the guest's TWED configuration if it hasn't set TWE 107 * to avoid potentially delaying traps for the host. 108 */ 109 if (!(guest_hcr & HCR_TWE)) 110 guest_hcr &= ~(HCR_EL2_TWEDEn | HCR_EL2_TWEDEL); 111 } 112 113 BUG_ON(host_data_test_flag(VCPU_IN_HYP_CONTEXT) && 114 host_data_test_flag(L1_VNCR_MAPPED)); 115 116 return hcr | (guest_hcr & ~NV_HCR_GUEST_EXCLUDE); 117 } 118 119 static void __activate_traps(struct kvm_vcpu *vcpu) 120 { 121 u64 val; 122 123 ___activate_traps(vcpu, __compute_hcr(vcpu)); 124 125 if (has_cntpoff()) { 126 struct timer_map map; 127 128 get_timer_map(vcpu, &map); 129 130 /* 131 * We're entrering the guest. Reload the correct 132 * values from memory now that TGE is clear. 133 */ 134 if (map.direct_ptimer == vcpu_ptimer(vcpu)) 135 val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0); 136 if (map.direct_ptimer == vcpu_hptimer(vcpu)) 137 val = __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2); 138 139 if (map.direct_ptimer) { 140 write_sysreg_el0(val, SYS_CNTP_CVAL); 141 isb(); 142 } 143 } 144 145 __activate_cptr_traps(vcpu); 146 147 write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1); 148 } 149 NOKPROBE_SYMBOL(__activate_traps); 150 151 static void __deactivate_traps(struct kvm_vcpu *vcpu) 152 { 153 const char *host_vectors = vectors; 154 155 ___deactivate_traps(vcpu); 156 157 write_sysreg_hcr(HCR_HOST_VHE_FLAGS); 158 159 if (has_cntpoff()) { 160 struct timer_map map; 161 u64 val, offset; 162 163 get_timer_map(vcpu, &map); 164 165 /* 166 * We're exiting the guest. Save the latest CVAL value 167 * to memory and apply the offset now that TGE is set. 168 */ 169 val = read_sysreg_el0(SYS_CNTP_CVAL); 170 if (map.direct_ptimer == vcpu_ptimer(vcpu)) 171 __vcpu_assign_sys_reg(vcpu, CNTP_CVAL_EL0, val); 172 if (map.direct_ptimer == vcpu_hptimer(vcpu)) 173 __vcpu_assign_sys_reg(vcpu, CNTHP_CVAL_EL2, val); 174 175 offset = read_sysreg_s(SYS_CNTPOFF_EL2); 176 177 if (map.direct_ptimer && offset) { 178 write_sysreg_el0(val + offset, SYS_CNTP_CVAL); 179 isb(); 180 } 181 } 182 183 /* 184 * ARM errata 1165522 and 1530923 require the actual execution of the 185 * above before we can switch to the EL2/EL0 translation regime used by 186 * the host. 187 */ 188 asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); 189 190 __deactivate_cptr_traps(vcpu); 191 192 if (!arm64_kernel_unmapped_at_el0()) 193 host_vectors = __this_cpu_read(this_cpu_vector); 194 write_sysreg(host_vectors, vbar_el1); 195 } 196 NOKPROBE_SYMBOL(__deactivate_traps); 197 198 /* 199 * Disable IRQs in __vcpu_{load,put}_{activate,deactivate}_traps() to 200 * prevent a race condition between context switching of PMUSERENR_EL0 201 * in __{activate,deactivate}_traps_common() and IPIs that attempts to 202 * update PMUSERENR_EL0. See also kvm_set_pmuserenr(). 203 */ 204 static void __vcpu_load_activate_traps(struct kvm_vcpu *vcpu) 205 { 206 unsigned long flags; 207 208 local_irq_save(flags); 209 __activate_traps_common(vcpu); 210 local_irq_restore(flags); 211 } 212 213 static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) 214 { 215 unsigned long flags; 216 217 local_irq_save(flags); 218 __deactivate_traps_common(vcpu); 219 local_irq_restore(flags); 220 } 221 222 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu) 223 { 224 host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu; 225 226 __vcpu_load_switch_sysregs(vcpu); 227 __vcpu_load_activate_traps(vcpu); 228 __load_stage2(vcpu->arch.hw_mmu); 229 } 230 231 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu) 232 { 233 __vcpu_put_deactivate_traps(vcpu); 234 __vcpu_put_switch_sysregs(vcpu); 235 236 host_data_ptr(host_ctxt)->__hyp_running_vcpu = NULL; 237 } 238 239 static u64 compute_emulated_cntx_ctl_el0(struct kvm_vcpu *vcpu, 240 enum vcpu_sysreg reg) 241 { 242 unsigned long ctl; 243 u64 cval, cnt; 244 bool stat; 245 246 switch (reg) { 247 case CNTP_CTL_EL0: 248 cval = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0); 249 ctl = __vcpu_sys_reg(vcpu, CNTP_CTL_EL0); 250 cnt = compute_counter_value(vcpu_ptimer(vcpu)); 251 break; 252 case CNTV_CTL_EL0: 253 cval = __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0); 254 ctl = __vcpu_sys_reg(vcpu, CNTV_CTL_EL0); 255 cnt = compute_counter_value(vcpu_vtimer(vcpu)); 256 break; 257 default: 258 BUG(); 259 } 260 261 stat = cval <= cnt; 262 __assign_bit(__ffs(ARCH_TIMER_CTRL_IT_STAT), &ctl, stat); 263 264 return ctl; 265 } 266 267 static bool kvm_hyp_handle_timer(struct kvm_vcpu *vcpu, u64 *exit_code) 268 { 269 u64 esr, val; 270 271 /* 272 * Having FEAT_ECV allows for a better quality of timer emulation. 273 * However, this comes at a huge cost in terms of traps. Try and 274 * satisfy the reads from guest's hypervisor context without 275 * returning to the kernel if we can. 276 */ 277 if (!is_hyp_ctxt(vcpu)) 278 return false; 279 280 esr = kvm_vcpu_get_esr(vcpu); 281 if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) != ESR_ELx_SYS64_ISS_DIR_READ) 282 return false; 283 284 switch (esr_sys64_to_sysreg(esr)) { 285 case SYS_CNTP_CTL_EL02: 286 val = compute_emulated_cntx_ctl_el0(vcpu, CNTP_CTL_EL0); 287 break; 288 case SYS_CNTP_CTL_EL0: 289 if (vcpu_el2_e2h_is_set(vcpu)) 290 val = read_sysreg_el0(SYS_CNTP_CTL); 291 else 292 val = compute_emulated_cntx_ctl_el0(vcpu, CNTP_CTL_EL0); 293 break; 294 case SYS_CNTP_CVAL_EL02: 295 val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0); 296 break; 297 case SYS_CNTP_CVAL_EL0: 298 if (vcpu_el2_e2h_is_set(vcpu)) { 299 val = read_sysreg_el0(SYS_CNTP_CVAL); 300 301 if (!has_cntpoff()) 302 val -= timer_get_offset(vcpu_hptimer(vcpu)); 303 } else { 304 val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0); 305 } 306 break; 307 case SYS_CNTPCT_EL0: 308 case SYS_CNTPCTSS_EL0: 309 val = compute_counter_value(vcpu_hptimer(vcpu)); 310 break; 311 case SYS_CNTV_CTL_EL02: 312 val = compute_emulated_cntx_ctl_el0(vcpu, CNTV_CTL_EL0); 313 break; 314 case SYS_CNTV_CTL_EL0: 315 if (vcpu_el2_e2h_is_set(vcpu)) 316 val = read_sysreg_el0(SYS_CNTV_CTL); 317 else 318 val = compute_emulated_cntx_ctl_el0(vcpu, CNTV_CTL_EL0); 319 break; 320 case SYS_CNTV_CVAL_EL02: 321 val = __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0); 322 break; 323 case SYS_CNTV_CVAL_EL0: 324 if (vcpu_el2_e2h_is_set(vcpu)) 325 val = read_sysreg_el0(SYS_CNTV_CVAL); 326 else 327 val = __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0); 328 break; 329 case SYS_CNTVCT_EL0: 330 case SYS_CNTVCTSS_EL0: 331 val = compute_counter_value(vcpu_hvtimer(vcpu)); 332 break; 333 default: 334 return false; 335 } 336 337 vcpu_set_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu), val); 338 __kvm_skip_instr(vcpu); 339 340 return true; 341 } 342 343 static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code) 344 { 345 u64 esr = kvm_vcpu_get_esr(vcpu); 346 u64 spsr, elr, mode; 347 348 /* With NV3, the fast path is handled in HW */ 349 if (cpus_have_final_cap(ARM64_HAS_NV3) && vcpu_el2_e2h_is_set(vcpu)) 350 return false; 351 352 /* 353 * Going through the whole put/load motions is a waste of time 354 * if this is a VHE guest hypervisor returning to its own 355 * userspace, or the hypervisor performing a local exception 356 * return. No need to save/restore registers, no need to 357 * switch S2 MMU. Just do the canonical ERET unless we are in 358 * nested context. 359 * 360 * Note that this is made possible because KVM itself never traps 361 * ERET when running an L2. The consequence is that any ERET trap is 362 * the result of HCR_EL2 or HFGITR_EL2 programming by L1 for its own 363 * guest, and the exception must be forwarded to L1. 364 */ 365 if (is_nested_ctxt(vcpu)) 366 return false; 367 368 spsr = read_sysreg_el1(SYS_SPSR); 369 mode = spsr & (PSR_MODE_MASK | PSR_MODE32_BIT); 370 371 switch (mode) { 372 case PSR_MODE_EL0t: 373 if (!(vcpu_el2_e2h_is_set(vcpu) && vcpu_el2_tge_is_set(vcpu))) 374 return false; 375 break; 376 case PSR_MODE_EL2t: 377 mode = PSR_MODE_EL1t; 378 break; 379 case PSR_MODE_EL2h: 380 mode = PSR_MODE_EL1h; 381 break; 382 default: 383 return false; 384 } 385 386 /* If ERETAx fails, take the slow path */ 387 if (esr_iss_is_eretax(esr)) { 388 if (!(vcpu_has_ptrauth(vcpu) && kvm_auth_eretax(vcpu, &elr))) 389 return false; 390 } else { 391 elr = read_sysreg_el1(SYS_ELR); 392 } 393 394 spsr = (spsr & ~(PSR_MODE_MASK | PSR_MODE32_BIT)) | mode; 395 396 write_sysreg_el2(spsr, SYS_SPSR); 397 write_sysreg_el2(elr, SYS_ELR); 398 399 return true; 400 } 401 402 static bool kvm_hyp_handle_tlbi_el2(struct kvm_vcpu *vcpu, u64 *exit_code) 403 { 404 int ret = -EINVAL; 405 u32 instr; 406 u64 val; 407 408 /* 409 * Ideally, we would never trap on EL2 S1 TLB invalidations using 410 * the EL1 instructions when the guest's HCR_EL2.{E2H,TGE}=={1,1}. 411 * But "thanks" to FEAT_NV2, we don't trap writes to HCR_EL2, 412 * meaning that we can't track changes to the virtual TGE bit. So we 413 * have to leave HCR_EL2.TTLB set on the host. Oopsie... 414 * 415 * Try and handle these invalidation as quickly as possible, without 416 * fully exiting. Note that we don't need to consider any forwarding 417 * here, as having E2H+TGE set is the very definition of being 418 * InHost. 419 * 420 * For the lesser hypervisors out there that have failed to get on 421 * with the VHE program, we can also handle the nVHE style of EL2 422 * invalidation. 423 */ 424 if (!(is_hyp_ctxt(vcpu))) 425 return false; 426 427 instr = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu)); 428 val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu)); 429 430 if ((kvm_supported_tlbi_s1e1_op(vcpu, instr) && 431 vcpu_el2_e2h_is_set(vcpu) && vcpu_el2_tge_is_set(vcpu)) || 432 kvm_supported_tlbi_s1e2_op (vcpu, instr)) 433 ret = __kvm_tlbi_s1e2(NULL, val, instr); 434 435 if (ret) 436 return false; 437 438 /* 439 * If we have to check for any VNCR TLB being invalidated, go back 440 * to the slow path for further processing. 441 * 442 * The synchronisation betweem TLBI and walk is provided by the 443 * speculative increment of the TLB counter on walk, and the 444 * invalidation counter. Yes, this is fiddly. 445 */ 446 if (vcpu_el2_e2h_is_set(vcpu) && vcpu_el2_tge_is_set(vcpu) && 447 atomic_read(&vcpu->kvm->arch.vncr_tlb_count)) 448 return false; 449 450 __kvm_skip_instr(vcpu); 451 452 return true; 453 } 454 455 static bool kvm_hyp_handle_cpacr_el1(struct kvm_vcpu *vcpu, u64 *exit_code) 456 { 457 u64 esr = kvm_vcpu_get_esr(vcpu); 458 int rt; 459 460 if (cpus_have_final_cap(ARM64_HAS_NV2P1)) 461 return false; 462 463 if (!is_hyp_ctxt(vcpu) || esr_sys64_to_sysreg(esr) != SYS_CPACR_EL1) 464 return false; 465 466 rt = kvm_vcpu_sys_get_rt(vcpu); 467 468 if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ) { 469 vcpu_set_reg(vcpu, rt, __vcpu_sys_reg(vcpu, CPTR_EL2)); 470 } else { 471 vcpu_write_sys_reg(vcpu, vcpu_get_reg(vcpu, rt), CPTR_EL2); 472 __activate_cptr_traps(vcpu); 473 } 474 475 __kvm_skip_instr(vcpu); 476 477 return true; 478 } 479 480 static bool kvm_hyp_handle_zcr_el2(struct kvm_vcpu *vcpu, u64 *exit_code) 481 { 482 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu)); 483 484 if (!vcpu_has_nv(vcpu)) 485 return false; 486 487 if (sysreg != SYS_ZCR_EL2) 488 return false; 489 490 if (guest_owns_fp_regs()) 491 return false; 492 493 /* 494 * ZCR_EL2 traps are handled in the slow path, with the expectation 495 * that the guest's FP context has already been loaded onto the CPU. 496 * 497 * Load the guest's FP context and unconditionally forward to the 498 * slow path for handling (i.e. return false). 499 */ 500 kvm_hyp_handle_fpsimd(vcpu, exit_code); 501 return false; 502 } 503 504 static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu *vcpu, u64 *exit_code) 505 { 506 if (kvm_hyp_handle_tlbi_el2(vcpu, exit_code)) 507 return true; 508 509 if (kvm_hyp_handle_timer(vcpu, exit_code)) 510 return true; 511 512 if (kvm_hyp_handle_cpacr_el1(vcpu, exit_code)) 513 return true; 514 515 if (kvm_hyp_handle_zcr_el2(vcpu, exit_code)) 516 return true; 517 518 return kvm_hyp_handle_sysreg(vcpu, exit_code); 519 } 520 521 static bool kvm_hyp_handle_impdef(struct kvm_vcpu *vcpu, u64 *exit_code) 522 { 523 u64 iss; 524 525 if (!cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS)) 526 return false; 527 528 /* 529 * Compute a synthetic ESR for a sysreg trap. Conveniently, AFSR1_EL2 530 * is populated with a correct ISS for a sysreg trap. These fruity 531 * parts are 64bit only, so unconditionally set IL. 532 */ 533 iss = ESR_ELx_ISS(read_sysreg_s(SYS_AFSR1_EL2)); 534 vcpu->arch.fault.esr_el2 = FIELD_PREP(ESR_ELx_EC_MASK, ESR_ELx_EC_SYS64) | 535 FIELD_PREP(ESR_ELx_ISS_MASK, iss) | 536 ESR_ELx_IL; 537 return false; 538 } 539 540 static const exit_handler_fn hyp_exit_handlers[] = { 541 [0 ... ESR_ELx_EC_MAX] = NULL, 542 [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32, 543 [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg_vhe, 544 [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd, 545 [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, 546 [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, 547 [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low, 548 [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low, 549 [ESR_ELx_EC_ERET] = kvm_hyp_handle_eret, 550 [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, 551 552 /* Apple shenanigans */ 553 [0x3F] = kvm_hyp_handle_impdef, 554 }; 555 556 static void fixup_nv_guest_exit(struct kvm_vcpu *vcpu) 557 { 558 /* 559 * If we were in HYP context on entry, adjust the PSTATE view 560 * so that the usual helpers work correctly. This enforces our 561 * invariant that the guest's HYP context status is preserved 562 * across a run. 563 */ 564 if (unlikely(host_data_test_flag(VCPU_IN_HYP_CONTEXT))) { 565 u64 mode = *vcpu_cpsr(vcpu) & (PSR_MODE_MASK | PSR_MODE32_BIT); 566 u64 hcr; 567 568 switch (mode) { 569 case PSR_MODE_EL1t: 570 mode = PSR_MODE_EL2t; 571 break; 572 case PSR_MODE_EL1h: 573 mode = PSR_MODE_EL2h; 574 break; 575 } 576 577 *vcpu_cpsr(vcpu) &= ~(PSR_MODE_MASK | PSR_MODE32_BIT); 578 *vcpu_cpsr(vcpu) |= mode; 579 580 /* Publish the latest HCR_EL2 to the emulation */ 581 hcr = (cpus_have_final_cap(ARM64_HAS_NV3) && 582 vcpu_el2_e2h_is_set(vcpu)) ? 583 read_sysreg_s(SYS_NVHCR_EL2) : 584 __vcpu_sys_reg(vcpu, NVHCR_EL2); 585 586 __vcpu_assign_sys_reg(vcpu, HCR_EL2, hcr); 587 } 588 589 /* Apply extreme paranoia! */ 590 BUG_ON(!!host_data_test_flag(VCPU_IN_HYP_CONTEXT) != is_hyp_ctxt(vcpu)); 591 } 592 593 static bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code) 594 { 595 synchronize_vcpu_pstate(vcpu); 596 597 if (vcpu_has_nv(vcpu)) 598 fixup_nv_guest_exit(vcpu); 599 600 return __fixup_guest_exit(vcpu, exit_code, hyp_exit_handlers); 601 } 602 603 /* Switch to the guest for VHE systems running in EL2 */ 604 static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu) 605 { 606 struct kvm_cpu_context *host_ctxt; 607 struct kvm_cpu_context *guest_ctxt; 608 u64 exit_code; 609 610 host_ctxt = host_data_ptr(host_ctxt); 611 guest_ctxt = &vcpu->arch.ctxt; 612 613 fpsimd_lazy_switch_to_guest(vcpu); 614 615 sysreg_save_host_state_vhe(host_ctxt); 616 617 /* 618 * Note that ARM erratum 1165522 requires us to configure both stage 1 619 * and stage 2 translation for the guest context before we clear 620 * HCR_EL2.TGE. The stage 1 and stage 2 guest context has already been 621 * loaded on the CPU in kvm_vcpu_load_vhe(). 622 */ 623 __activate_traps(vcpu); 624 625 __kvm_adjust_pc(vcpu); 626 627 sysreg_restore_guest_state_vhe(guest_ctxt); 628 __debug_switch_to_guest(vcpu); 629 630 do { 631 /* Jump in the fire! */ 632 exit_code = __guest_enter(vcpu); 633 634 /* And we're baaack! */ 635 } while (fixup_guest_exit(vcpu, &exit_code)); 636 637 sysreg_save_guest_state_vhe(guest_ctxt); 638 639 __deactivate_traps(vcpu); 640 641 sysreg_restore_host_state_vhe(host_ctxt); 642 643 __debug_switch_to_host(vcpu); 644 645 /* 646 * Ensure that all system register writes above have taken effect 647 * before returning to the host. In VHE mode, CPTR traps for 648 * FPSIMD/SVE/SME also apply to EL2, so FPSIMD/SVE/SME state must be 649 * manipulated after the ISB. 650 */ 651 isb(); 652 653 fpsimd_lazy_switch_to_host(vcpu); 654 655 if (guest_owns_fp_regs()) 656 __fpsimd_save_fpexc32(vcpu); 657 658 return exit_code; 659 } 660 NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe); 661 662 int __kvm_vcpu_run(struct kvm_vcpu *vcpu) 663 { 664 int ret; 665 666 local_daif_mask(); 667 668 /* 669 * Having IRQs masked via PMR when entering the guest means the GIC 670 * will not signal the CPU of interrupts of lower priority, and the 671 * only way to get out will be via guest exceptions. 672 * Naturally, we want to avoid this. 673 * 674 * local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a 675 * dsb to ensure the redistributor is forwards EL2 IRQs to the CPU. 676 */ 677 pmr_sync(); 678 679 ret = __kvm_vcpu_run_vhe(vcpu); 680 681 /* 682 * local_daif_restore() takes care to properly restore PSTATE.DAIF 683 * and the GIC PMR if the host is using IRQ priorities. 684 */ 685 local_daif_restore(DAIF_PROCCTX_NOIRQ); 686 687 return ret; 688 } 689 690 static void __noreturn __hyp_call_panic(u64 spsr, u64 elr, u64 par) 691 { 692 struct kvm_cpu_context *host_ctxt; 693 struct kvm_vcpu *vcpu; 694 695 host_ctxt = host_data_ptr(host_ctxt); 696 vcpu = host_ctxt->__hyp_running_vcpu; 697 698 if (vcpu) 699 __deactivate_traps(vcpu); 700 sysreg_restore_host_state_vhe(host_ctxt); 701 702 panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n", 703 spsr, elr, 704 read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR), 705 read_sysreg(hpfar_el2), par, vcpu); 706 } 707 NOKPROBE_SYMBOL(__hyp_call_panic); 708 709 void __noreturn hyp_panic(void) 710 { 711 u64 spsr = read_sysreg_el2(SYS_SPSR); 712 u64 elr = read_sysreg_el2(SYS_ELR); 713 u64 par = read_sysreg_par(); 714 715 __hyp_call_panic(spsr, elr, par); 716 } 717 718 asmlinkage void kvm_unexpected_el2_exception(void) 719 { 720 __kvm_unexpected_el2_exception(); 721 } 722