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 hcr = vcpu->arch.hcr_el2; 52 53 if (!vcpu_has_nv(vcpu)) 54 return hcr; 55 56 if (is_hyp_ctxt(vcpu)) { 57 hcr |= HCR_NV | HCR_NV2 | HCR_AT | HCR_TTLB; 58 59 if (!vcpu_el2_e2h_is_set(vcpu)) 60 hcr |= HCR_NV1; 61 62 write_sysreg_s(vcpu->arch.ctxt.vncr_array, SYS_VNCR_EL2); 63 } 64 65 return hcr | (__vcpu_sys_reg(vcpu, HCR_EL2) & ~NV_HCR_GUEST_EXCLUDE); 66 } 67 68 static void __activate_cptr_traps(struct kvm_vcpu *vcpu) 69 { 70 u64 cptr; 71 72 /* 73 * With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to 74 * CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2, 75 * except for some missing controls, such as TAM. 76 * In this case, CPTR_EL2.TAM has the same position with or without 77 * VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM 78 * shift value for trapping the AMU accesses. 79 */ 80 u64 val = CPACR_ELx_TTA | CPTR_EL2_TAM; 81 82 if (guest_owns_fp_regs()) { 83 val |= CPACR_ELx_FPEN; 84 if (vcpu_has_sve(vcpu)) 85 val |= CPACR_ELx_ZEN; 86 } else { 87 __activate_traps_fpsimd32(vcpu); 88 } 89 90 /* 91 * Layer the guest hypervisor's trap configuration on top of our own if 92 * we're in a nested context. 93 */ 94 if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu)) 95 goto write; 96 97 cptr = vcpu_sanitised_cptr_el2(vcpu); 98 99 /* 100 * Pay attention, there's some interesting detail here. 101 * 102 * The CPTR_EL2.xEN fields are 2 bits wide, although there are only two 103 * meaningful trap states when HCR_EL2.TGE = 0 (running a nested guest): 104 * 105 * - CPTR_EL2.xEN = x0, traps are enabled 106 * - CPTR_EL2.xEN = x1, traps are disabled 107 * 108 * In other words, bit[0] determines if guest accesses trap or not. In 109 * the interest of simplicity, clear the entire field if the guest 110 * hypervisor has traps enabled to dispel any illusion of something more 111 * complicated taking place. 112 */ 113 if (!(SYS_FIELD_GET(CPACR_ELx, FPEN, cptr) & BIT(0))) 114 val &= ~CPACR_ELx_FPEN; 115 if (!(SYS_FIELD_GET(CPACR_ELx, ZEN, cptr) & BIT(0))) 116 val &= ~CPACR_ELx_ZEN; 117 118 write: 119 write_sysreg(val, cpacr_el1); 120 } 121 122 static void __activate_traps(struct kvm_vcpu *vcpu) 123 { 124 u64 val; 125 126 ___activate_traps(vcpu, __compute_hcr(vcpu)); 127 128 if (has_cntpoff()) { 129 struct timer_map map; 130 131 get_timer_map(vcpu, &map); 132 133 /* 134 * We're entrering the guest. Reload the correct 135 * values from memory now that TGE is clear. 136 */ 137 if (map.direct_ptimer == vcpu_ptimer(vcpu)) 138 val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0); 139 if (map.direct_ptimer == vcpu_hptimer(vcpu)) 140 val = __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2); 141 142 if (map.direct_ptimer) { 143 write_sysreg_el0(val, SYS_CNTP_CVAL); 144 isb(); 145 } 146 } 147 148 __activate_cptr_traps(vcpu); 149 150 write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1); 151 } 152 NOKPROBE_SYMBOL(__activate_traps); 153 154 static void __deactivate_traps(struct kvm_vcpu *vcpu) 155 { 156 const char *host_vectors = vectors; 157 158 ___deactivate_traps(vcpu); 159 160 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2); 161 162 if (has_cntpoff()) { 163 struct timer_map map; 164 u64 val, offset; 165 166 get_timer_map(vcpu, &map); 167 168 /* 169 * We're exiting the guest. Save the latest CVAL value 170 * to memory and apply the offset now that TGE is set. 171 */ 172 val = read_sysreg_el0(SYS_CNTP_CVAL); 173 if (map.direct_ptimer == vcpu_ptimer(vcpu)) 174 __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = val; 175 if (map.direct_ptimer == vcpu_hptimer(vcpu)) 176 __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2) = val; 177 178 offset = read_sysreg_s(SYS_CNTPOFF_EL2); 179 180 if (map.direct_ptimer && offset) { 181 write_sysreg_el0(val + offset, SYS_CNTP_CVAL); 182 isb(); 183 } 184 } 185 186 /* 187 * ARM errata 1165522 and 1530923 require the actual execution of the 188 * above before we can switch to the EL2/EL0 translation regime used by 189 * the host. 190 */ 191 asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); 192 193 kvm_reset_cptr_el2(vcpu); 194 195 if (!arm64_kernel_unmapped_at_el0()) 196 host_vectors = __this_cpu_read(this_cpu_vector); 197 write_sysreg(host_vectors, vbar_el1); 198 } 199 NOKPROBE_SYMBOL(__deactivate_traps); 200 201 /* 202 * Disable IRQs in __vcpu_{load,put}_{activate,deactivate}_traps() to 203 * prevent a race condition between context switching of PMUSERENR_EL0 204 * in __{activate,deactivate}_traps_common() and IPIs that attempts to 205 * update PMUSERENR_EL0. See also kvm_set_pmuserenr(). 206 */ 207 static void __vcpu_load_activate_traps(struct kvm_vcpu *vcpu) 208 { 209 unsigned long flags; 210 211 local_irq_save(flags); 212 __activate_traps_common(vcpu); 213 local_irq_restore(flags); 214 } 215 216 static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) 217 { 218 unsigned long flags; 219 220 local_irq_save(flags); 221 __deactivate_traps_common(vcpu); 222 local_irq_restore(flags); 223 } 224 225 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu) 226 { 227 host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu; 228 229 __vcpu_load_switch_sysregs(vcpu); 230 __vcpu_load_activate_traps(vcpu); 231 __load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch); 232 } 233 234 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu) 235 { 236 __vcpu_put_deactivate_traps(vcpu); 237 __vcpu_put_switch_sysregs(vcpu); 238 239 host_data_ptr(host_ctxt)->__hyp_running_vcpu = NULL; 240 } 241 242 static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code) 243 { 244 u64 esr = kvm_vcpu_get_esr(vcpu); 245 u64 spsr, elr, mode; 246 247 /* 248 * Going through the whole put/load motions is a waste of time 249 * if this is a VHE guest hypervisor returning to its own 250 * userspace, or the hypervisor performing a local exception 251 * return. No need to save/restore registers, no need to 252 * switch S2 MMU. Just do the canonical ERET. 253 * 254 * Unless the trap has to be forwarded further down the line, 255 * of course... 256 */ 257 if ((__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_NV) || 258 (__vcpu_sys_reg(vcpu, HFGITR_EL2) & HFGITR_EL2_ERET)) 259 return false; 260 261 spsr = read_sysreg_el1(SYS_SPSR); 262 mode = spsr & (PSR_MODE_MASK | PSR_MODE32_BIT); 263 264 switch (mode) { 265 case PSR_MODE_EL0t: 266 if (!(vcpu_el2_e2h_is_set(vcpu) && vcpu_el2_tge_is_set(vcpu))) 267 return false; 268 break; 269 case PSR_MODE_EL2t: 270 mode = PSR_MODE_EL1t; 271 break; 272 case PSR_MODE_EL2h: 273 mode = PSR_MODE_EL1h; 274 break; 275 default: 276 return false; 277 } 278 279 /* If ERETAx fails, take the slow path */ 280 if (esr_iss_is_eretax(esr)) { 281 if (!(vcpu_has_ptrauth(vcpu) && kvm_auth_eretax(vcpu, &elr))) 282 return false; 283 } else { 284 elr = read_sysreg_el1(SYS_ELR); 285 } 286 287 spsr = (spsr & ~(PSR_MODE_MASK | PSR_MODE32_BIT)) | mode; 288 289 write_sysreg_el2(spsr, SYS_SPSR); 290 write_sysreg_el2(elr, SYS_ELR); 291 292 return true; 293 } 294 295 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu) 296 { 297 __fpsimd_save_state(*host_data_ptr(fpsimd_state)); 298 } 299 300 static bool kvm_hyp_handle_cpacr_el1(struct kvm_vcpu *vcpu, u64 *exit_code) 301 { 302 u64 esr = kvm_vcpu_get_esr(vcpu); 303 int rt; 304 305 if (!is_hyp_ctxt(vcpu) || esr_sys64_to_sysreg(esr) != SYS_CPACR_EL1) 306 return false; 307 308 rt = kvm_vcpu_sys_get_rt(vcpu); 309 310 if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ) { 311 vcpu_set_reg(vcpu, rt, __vcpu_sys_reg(vcpu, CPTR_EL2)); 312 } else { 313 vcpu_write_sys_reg(vcpu, vcpu_get_reg(vcpu, rt), CPTR_EL2); 314 __activate_cptr_traps(vcpu); 315 } 316 317 __kvm_skip_instr(vcpu); 318 319 return true; 320 } 321 322 static bool kvm_hyp_handle_zcr_el2(struct kvm_vcpu *vcpu, u64 *exit_code) 323 { 324 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu)); 325 326 if (!vcpu_has_nv(vcpu)) 327 return false; 328 329 if (sysreg != SYS_ZCR_EL2) 330 return false; 331 332 if (guest_owns_fp_regs()) 333 return false; 334 335 /* 336 * ZCR_EL2 traps are handled in the slow path, with the expectation 337 * that the guest's FP context has already been loaded onto the CPU. 338 * 339 * Load the guest's FP context and unconditionally forward to the 340 * slow path for handling (i.e. return false). 341 */ 342 kvm_hyp_handle_fpsimd(vcpu, exit_code); 343 return false; 344 } 345 346 static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu *vcpu, u64 *exit_code) 347 { 348 if (kvm_hyp_handle_cpacr_el1(vcpu, exit_code)) 349 return true; 350 351 if (kvm_hyp_handle_zcr_el2(vcpu, exit_code)) 352 return true; 353 354 return kvm_hyp_handle_sysreg(vcpu, exit_code); 355 } 356 357 static const exit_handler_fn hyp_exit_handlers[] = { 358 [0 ... ESR_ELx_EC_MAX] = NULL, 359 [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32, 360 [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg_vhe, 361 [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd, 362 [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, 363 [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, 364 [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low, 365 [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low, 366 [ESR_ELx_EC_ERET] = kvm_hyp_handle_eret, 367 [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, 368 }; 369 370 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu) 371 { 372 return hyp_exit_handlers; 373 } 374 375 static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code) 376 { 377 /* 378 * If we were in HYP context on entry, adjust the PSTATE view 379 * so that the usual helpers work correctly. 380 */ 381 if (vcpu_has_nv(vcpu) && (read_sysreg(hcr_el2) & HCR_NV)) { 382 u64 mode = *vcpu_cpsr(vcpu) & (PSR_MODE_MASK | PSR_MODE32_BIT); 383 384 switch (mode) { 385 case PSR_MODE_EL1t: 386 mode = PSR_MODE_EL2t; 387 break; 388 case PSR_MODE_EL1h: 389 mode = PSR_MODE_EL2h; 390 break; 391 } 392 393 *vcpu_cpsr(vcpu) &= ~(PSR_MODE_MASK | PSR_MODE32_BIT); 394 *vcpu_cpsr(vcpu) |= mode; 395 } 396 } 397 398 /* Switch to the guest for VHE systems running in EL2 */ 399 static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu) 400 { 401 struct kvm_cpu_context *host_ctxt; 402 struct kvm_cpu_context *guest_ctxt; 403 u64 exit_code; 404 405 host_ctxt = host_data_ptr(host_ctxt); 406 guest_ctxt = &vcpu->arch.ctxt; 407 408 sysreg_save_host_state_vhe(host_ctxt); 409 410 /* 411 * Note that ARM erratum 1165522 requires us to configure both stage 1 412 * and stage 2 translation for the guest context before we clear 413 * HCR_EL2.TGE. The stage 1 and stage 2 guest context has already been 414 * loaded on the CPU in kvm_vcpu_load_vhe(). 415 */ 416 __activate_traps(vcpu); 417 418 __kvm_adjust_pc(vcpu); 419 420 sysreg_restore_guest_state_vhe(guest_ctxt); 421 __debug_switch_to_guest(vcpu); 422 423 do { 424 /* Jump in the fire! */ 425 exit_code = __guest_enter(vcpu); 426 427 /* And we're baaack! */ 428 } while (fixup_guest_exit(vcpu, &exit_code)); 429 430 sysreg_save_guest_state_vhe(guest_ctxt); 431 432 __deactivate_traps(vcpu); 433 434 sysreg_restore_host_state_vhe(host_ctxt); 435 436 if (guest_owns_fp_regs()) 437 __fpsimd_save_fpexc32(vcpu); 438 439 __debug_switch_to_host(vcpu); 440 441 return exit_code; 442 } 443 NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe); 444 445 int __kvm_vcpu_run(struct kvm_vcpu *vcpu) 446 { 447 int ret; 448 449 local_daif_mask(); 450 451 /* 452 * Having IRQs masked via PMR when entering the guest means the GIC 453 * will not signal the CPU of interrupts of lower priority, and the 454 * only way to get out will be via guest exceptions. 455 * Naturally, we want to avoid this. 456 * 457 * local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a 458 * dsb to ensure the redistributor is forwards EL2 IRQs to the CPU. 459 */ 460 pmr_sync(); 461 462 ret = __kvm_vcpu_run_vhe(vcpu); 463 464 /* 465 * local_daif_restore() takes care to properly restore PSTATE.DAIF 466 * and the GIC PMR if the host is using IRQ priorities. 467 */ 468 local_daif_restore(DAIF_PROCCTX_NOIRQ); 469 470 /* 471 * When we exit from the guest we change a number of CPU configuration 472 * parameters, such as traps. We rely on the isb() in kvm_call_hyp*() 473 * to make sure these changes take effect before running the host or 474 * additional guests. 475 */ 476 return ret; 477 } 478 479 static void __hyp_call_panic(u64 spsr, u64 elr, u64 par) 480 { 481 struct kvm_cpu_context *host_ctxt; 482 struct kvm_vcpu *vcpu; 483 484 host_ctxt = host_data_ptr(host_ctxt); 485 vcpu = host_ctxt->__hyp_running_vcpu; 486 487 __deactivate_traps(vcpu); 488 sysreg_restore_host_state_vhe(host_ctxt); 489 490 panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n", 491 spsr, elr, 492 read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR), 493 read_sysreg(hpfar_el2), par, vcpu); 494 } 495 NOKPROBE_SYMBOL(__hyp_call_panic); 496 497 void __noreturn hyp_panic(void) 498 { 499 u64 spsr = read_sysreg_el2(SYS_SPSR); 500 u64 elr = read_sysreg_el2(SYS_ELR); 501 u64 par = read_sysreg_par(); 502 503 __hyp_call_panic(spsr, elr, par); 504 unreachable(); 505 } 506 507 asmlinkage void kvm_unexpected_el2_exception(void) 508 { 509 __kvm_unexpected_el2_exception(); 510 } 511