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