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 #include <hyp/sysreg-sr.h> 9 10 #include <linux/arm-smccc.h> 11 #include <linux/kvm_host.h> 12 #include <linux/types.h> 13 #include <linux/jump_label.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 29 #include <nvhe/mem_protect.h> 30 31 /* Non-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 struct fgt_masks hfgrtr_masks; 37 struct fgt_masks hfgwtr_masks; 38 struct fgt_masks hfgitr_masks; 39 struct fgt_masks hdfgrtr_masks; 40 struct fgt_masks hdfgwtr_masks; 41 struct fgt_masks hafgrtr_masks; 42 struct fgt_masks hfgrtr2_masks; 43 struct fgt_masks hfgwtr2_masks; 44 struct fgt_masks hfgitr2_masks; 45 struct fgt_masks hdfgrtr2_masks; 46 struct fgt_masks hdfgwtr2_masks; 47 48 extern void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc); 49 50 static void __activate_cptr_traps(struct kvm_vcpu *vcpu) 51 { 52 u64 val = CPTR_EL2_TAM; /* Same bit irrespective of E2H */ 53 54 if (!guest_owns_fp_regs()) 55 __activate_traps_fpsimd32(vcpu); 56 57 if (has_hvhe()) { 58 val |= CPACR_EL1_TTA; 59 60 if (guest_owns_fp_regs()) { 61 val |= CPACR_EL1_FPEN; 62 if (vcpu_has_sve(vcpu)) 63 val |= CPACR_EL1_ZEN; 64 } 65 66 write_sysreg(val, cpacr_el1); 67 } else { 68 val |= CPTR_EL2_TTA | CPTR_NVHE_EL2_RES1; 69 70 /* 71 * Always trap SME since it's not supported in KVM. 72 * TSM is RES1 if SME isn't implemented. 73 */ 74 val |= CPTR_EL2_TSM; 75 76 if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs()) 77 val |= CPTR_EL2_TZ; 78 79 if (!guest_owns_fp_regs()) 80 val |= CPTR_EL2_TFP; 81 82 write_sysreg(val, cptr_el2); 83 } 84 } 85 86 static void __deactivate_cptr_traps(struct kvm_vcpu *vcpu) 87 { 88 if (has_hvhe()) { 89 u64 val = CPACR_EL1_FPEN; 90 91 if (cpus_have_final_cap(ARM64_SVE)) 92 val |= CPACR_EL1_ZEN; 93 if (cpus_have_final_cap(ARM64_SME)) 94 val |= CPACR_EL1_SMEN; 95 96 write_sysreg(val, cpacr_el1); 97 } else { 98 u64 val = CPTR_NVHE_EL2_RES1; 99 100 if (!cpus_have_final_cap(ARM64_SVE)) 101 val |= CPTR_EL2_TZ; 102 if (!cpus_have_final_cap(ARM64_SME)) 103 val |= CPTR_EL2_TSM; 104 105 write_sysreg(val, cptr_el2); 106 } 107 } 108 109 static void __activate_traps(struct kvm_vcpu *vcpu) 110 { 111 ___activate_traps(vcpu, vcpu->arch.hcr_el2); 112 __activate_traps_common(vcpu); 113 __activate_cptr_traps(vcpu); 114 115 write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2); 116 117 if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { 118 struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; 119 120 isb(); 121 /* 122 * At this stage, and thanks to the above isb(), S2 is 123 * configured and enabled. We can now restore the guest's S1 124 * configuration: SCTLR, and only then TCR. 125 */ 126 write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR); 127 isb(); 128 write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1), SYS_TCR); 129 } 130 } 131 132 static void __deactivate_traps(struct kvm_vcpu *vcpu) 133 { 134 extern char __kvm_hyp_host_vector[]; 135 136 ___deactivate_traps(vcpu); 137 138 if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { 139 u64 val; 140 141 /* 142 * Set the TCR and SCTLR registers in the exact opposite 143 * sequence as __activate_traps (first prevent walks, 144 * then force the MMU on). A generous sprinkling of isb() 145 * ensure that things happen in this exact order. 146 */ 147 val = read_sysreg_el1(SYS_TCR); 148 write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR); 149 isb(); 150 val = read_sysreg_el1(SYS_SCTLR); 151 write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR); 152 isb(); 153 } 154 155 __deactivate_traps_common(vcpu); 156 157 write_sysreg_hcr(this_cpu_ptr(&kvm_init_params)->hcr_el2); 158 159 __deactivate_cptr_traps(vcpu); 160 write_sysreg(__kvm_hyp_host_vector, vbar_el2); 161 } 162 163 /* Save VGICv3 state on non-VHE systems */ 164 static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu) 165 { 166 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) { 167 __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3); 168 __vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3); 169 } 170 } 171 172 /* Restore VGICv3 state on non-VHE systems */ 173 static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu) 174 { 175 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) { 176 __vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3); 177 __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3); 178 } 179 } 180 181 /* 182 * Disable host events, enable guest events 183 */ 184 #ifdef CONFIG_HW_PERF_EVENTS 185 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu) 186 { 187 struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events; 188 189 if (pmu->events_host) 190 write_sysreg(pmu->events_host, pmcntenclr_el0); 191 192 if (pmu->events_guest) 193 write_sysreg(pmu->events_guest, pmcntenset_el0); 194 195 return (pmu->events_host || pmu->events_guest); 196 } 197 198 /* 199 * Disable guest events, enable host events 200 */ 201 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu) 202 { 203 struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events; 204 205 if (pmu->events_guest) 206 write_sysreg(pmu->events_guest, pmcntenclr_el0); 207 208 if (pmu->events_host) 209 write_sysreg(pmu->events_host, pmcntenset_el0); 210 } 211 #else 212 #define __pmu_switch_to_guest(v) ({ false; }) 213 #define __pmu_switch_to_host(v) do {} while (0) 214 #endif 215 216 /* 217 * Handler for protected VM MSR, MRS or System instruction execution in AArch64. 218 * 219 * Returns true if the hypervisor has handled the exit, and control should go 220 * back to the guest, or false if it hasn't. 221 */ 222 static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code) 223 { 224 /* 225 * Make sure we handle the exit for workarounds before the pKVM 226 * handling, as the latter could decide to UNDEF. 227 */ 228 return (kvm_hyp_handle_sysreg(vcpu, exit_code) || 229 kvm_handle_pvm_sysreg(vcpu, exit_code)); 230 } 231 232 static const exit_handler_fn hyp_exit_handlers[] = { 233 [0 ... ESR_ELx_EC_MAX] = NULL, 234 [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32, 235 [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg, 236 [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd, 237 [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, 238 [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, 239 [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low, 240 [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low, 241 [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, 242 }; 243 244 static const exit_handler_fn pvm_exit_handlers[] = { 245 [0 ... ESR_ELx_EC_MAX] = NULL, 246 [ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64, 247 [ESR_ELx_EC_SVE] = kvm_handle_pvm_restricted, 248 [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, 249 [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, 250 [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low, 251 [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low, 252 [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, 253 }; 254 255 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu) 256 { 257 if (unlikely(vcpu_is_protected(vcpu))) 258 return pvm_exit_handlers; 259 260 return hyp_exit_handlers; 261 } 262 263 static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code) 264 { 265 const exit_handler_fn *handlers = kvm_get_exit_handler_array(vcpu); 266 267 synchronize_vcpu_pstate(vcpu, exit_code); 268 269 /* 270 * Some guests (e.g., protected VMs) are not be allowed to run in 271 * AArch32. The ARMv8 architecture does not give the hypervisor a 272 * mechanism to prevent a guest from dropping to AArch32 EL0 if 273 * implemented by the CPU. If the hypervisor spots a guest in such a 274 * state ensure it is handled, and don't trust the host to spot or fix 275 * it. The check below is based on the one in 276 * kvm_arch_vcpu_ioctl_run(). 277 */ 278 if (unlikely(vcpu_is_protected(vcpu) && vcpu_mode_is_32bit(vcpu))) { 279 /* 280 * As we have caught the guest red-handed, decide that it isn't 281 * fit for purpose anymore by making the vcpu invalid. The VMM 282 * can try and fix it by re-initializing the vcpu with 283 * KVM_ARM_VCPU_INIT, however, this is likely not possible for 284 * protected VMs. 285 */ 286 vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 287 *exit_code &= BIT(ARM_EXIT_WITH_SERROR_BIT); 288 *exit_code |= ARM_EXCEPTION_IL; 289 } 290 291 return __fixup_guest_exit(vcpu, exit_code, handlers); 292 } 293 294 /* Switch to the guest for legacy non-VHE systems */ 295 int __kvm_vcpu_run(struct kvm_vcpu *vcpu) 296 { 297 struct kvm_cpu_context *host_ctxt; 298 struct kvm_cpu_context *guest_ctxt; 299 struct kvm_s2_mmu *mmu; 300 bool pmu_switch_needed; 301 u64 exit_code; 302 303 /* 304 * Having IRQs masked via PMR when entering the guest means the GIC 305 * will not signal the CPU of interrupts of lower priority, and the 306 * only way to get out will be via guest exceptions. 307 * Naturally, we want to avoid this. 308 */ 309 if (system_uses_irq_prio_masking()) { 310 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); 311 pmr_sync(); 312 } 313 314 host_ctxt = host_data_ptr(host_ctxt); 315 host_ctxt->__hyp_running_vcpu = vcpu; 316 guest_ctxt = &vcpu->arch.ctxt; 317 318 pmu_switch_needed = __pmu_switch_to_guest(vcpu); 319 320 __sysreg_save_state_nvhe(host_ctxt); 321 /* 322 * We must flush and disable the SPE buffer for nVHE, as 323 * the translation regime(EL1&0) is going to be loaded with 324 * that of the guest. And we must do this before we change the 325 * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and 326 * before we load guest Stage1. 327 */ 328 __debug_save_host_buffers_nvhe(vcpu); 329 330 /* 331 * We're about to restore some new MMU state. Make sure 332 * ongoing page-table walks that have started before we 333 * trapped to EL2 have completed. This also synchronises the 334 * above disabling of SPE and TRBE. 335 * 336 * See DDI0487I.a D8.1.5 "Out-of-context translation regimes", 337 * rule R_LFHQG and subsequent information statements. 338 */ 339 dsb(nsh); 340 341 __kvm_adjust_pc(vcpu); 342 343 /* 344 * We must restore the 32-bit state before the sysregs, thanks 345 * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72). 346 * 347 * Also, and in order to be able to deal with erratum #1319537 (A57) 348 * and #1319367 (A72), we must ensure that all VM-related sysreg are 349 * restored before we enable S2 translation. 350 */ 351 __sysreg32_restore_state(vcpu); 352 __sysreg_restore_state_nvhe(guest_ctxt); 353 354 mmu = kern_hyp_va(vcpu->arch.hw_mmu); 355 __load_stage2(mmu, kern_hyp_va(mmu->arch)); 356 __activate_traps(vcpu); 357 358 __hyp_vgic_restore_state(vcpu); 359 __timer_enable_traps(vcpu); 360 361 __debug_switch_to_guest(vcpu); 362 363 do { 364 /* Jump in the fire! */ 365 exit_code = __guest_enter(vcpu); 366 367 /* And we're baaack! */ 368 } while (fixup_guest_exit(vcpu, &exit_code)); 369 370 __sysreg_save_state_nvhe(guest_ctxt); 371 __sysreg32_save_state(vcpu); 372 __timer_disable_traps(vcpu); 373 __hyp_vgic_save_state(vcpu); 374 375 /* 376 * Same thing as before the guest run: we're about to switch 377 * the MMU context, so let's make sure we don't have any 378 * ongoing EL1&0 translations. 379 */ 380 dsb(nsh); 381 382 __deactivate_traps(vcpu); 383 __load_host_stage2(); 384 385 __sysreg_restore_state_nvhe(host_ctxt); 386 387 if (guest_owns_fp_regs()) 388 __fpsimd_save_fpexc32(vcpu); 389 390 __debug_switch_to_host(vcpu); 391 /* 392 * This must come after restoring the host sysregs, since a non-VHE 393 * system may enable SPE here and make use of the TTBRs. 394 */ 395 __debug_restore_host_buffers_nvhe(vcpu); 396 397 if (pmu_switch_needed) 398 __pmu_switch_to_host(vcpu); 399 400 /* Returning to host will clear PSR.I, remask PMR if needed */ 401 if (system_uses_irq_prio_masking()) 402 gic_write_pmr(GIC_PRIO_IRQOFF); 403 404 host_ctxt->__hyp_running_vcpu = NULL; 405 406 return exit_code; 407 } 408 409 asmlinkage void __noreturn hyp_panic(void) 410 { 411 u64 spsr = read_sysreg_el2(SYS_SPSR); 412 u64 elr = read_sysreg_el2(SYS_ELR); 413 u64 par = read_sysreg_par(); 414 struct kvm_cpu_context *host_ctxt; 415 struct kvm_vcpu *vcpu; 416 417 host_ctxt = host_data_ptr(host_ctxt); 418 vcpu = host_ctxt->__hyp_running_vcpu; 419 420 if (vcpu) { 421 __timer_disable_traps(vcpu); 422 __deactivate_traps(vcpu); 423 __load_host_stage2(); 424 __sysreg_restore_state_nvhe(host_ctxt); 425 } 426 427 /* Prepare to dump kvm nvhe hyp stacktrace */ 428 kvm_nvhe_prepare_backtrace((unsigned long)__builtin_frame_address(0), 429 _THIS_IP_); 430 431 __hyp_do_panic(host_ctxt, spsr, elr, par); 432 unreachable(); 433 } 434 435 asmlinkage void __noreturn hyp_panic_bad_stack(void) 436 { 437 hyp_panic(); 438 } 439 440 asmlinkage void kvm_unexpected_el2_exception(void) 441 { 442 __kvm_unexpected_el2_exception(); 443 } 444