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