Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 - ARM Ltd |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 7 | #include <linux/arm-smccc.h> |
James Morse | b7c50fa | 2019-05-22 18:47:04 +0100 | [diff] [blame] | 8 | #include <linux/kvm_host.h> |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 9 | #include <linux/types.h> |
Vladimir Murzin | 5a7a842 | 2016-09-12 15:49:15 +0100 | [diff] [blame] | 10 | #include <linux/jump_label.h> |
Marc Zyngier | 9034868 | 2018-01-03 16:38:37 +0000 | [diff] [blame] | 11 | #include <uapi/linux/psci.h> |
Vladimir Murzin | 5a7a842 | 2016-09-12 15:49:15 +0100 | [diff] [blame] | 12 | |
Marc Zyngier | a4097b3 | 2018-02-06 17:56:13 +0000 | [diff] [blame] | 13 | #include <kvm/arm_psci.h> |
| 14 | |
Julien Thierry | 85738e0 | 2019-01-31 14:58:48 +0000 | [diff] [blame] | 15 | #include <asm/arch_gicv3.h> |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 16 | #include <asm/cpufeature.h> |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 17 | #include <asm/kprobes.h> |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 18 | #include <asm/kvm_asm.h> |
Marc Zyngier | fb5ee36 | 2016-09-06 09:28:45 +0100 | [diff] [blame] | 19 | #include <asm/kvm_emulate.h> |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 20 | #include <asm/kvm_host.h> |
Marc Zyngier | 13720a5 | 2016-01-28 13:44:07 +0000 | [diff] [blame] | 21 | #include <asm/kvm_hyp.h> |
Marc Zyngier | d681198 | 2017-10-23 17:11:14 +0100 | [diff] [blame] | 22 | #include <asm/kvm_mmu.h> |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 23 | #include <asm/fpsimd.h> |
Alex Bennée | e3feebf | 2017-11-23 12:11:34 +0000 | [diff] [blame] | 24 | #include <asm/debug-monitors.h> |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 25 | #include <asm/processor.h> |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 26 | #include <asm/thread_info.h> |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 27 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 28 | /* Check whether the FP regs were dirtied while in the host-side run loop: */ |
| 29 | static bool __hyp_text update_fp_enabled(struct kvm_vcpu *vcpu) |
Marc Zyngier | 3287622 | 2015-10-28 14:15:45 +0000 | [diff] [blame] | 30 | { |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 31 | if (vcpu->arch.host_thread_info->flags & _TIF_FOREIGN_FPSTATE) |
| 32 | vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED | |
| 33 | KVM_ARM64_FP_HOST); |
Marc Zyngier | 3287622 | 2015-10-28 14:15:45 +0000 | [diff] [blame] | 34 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 35 | return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED); |
Marc Zyngier | 3287622 | 2015-10-28 14:15:45 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Christoffer Dall | b9f8ca4 | 2017-12-27 22:12:12 +0100 | [diff] [blame] | 38 | /* Save the 32-bit only FPSIMD system register state */ |
| 39 | static void __hyp_text __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu) |
| 40 | { |
| 41 | if (!vcpu_el1_is_32bit(vcpu)) |
| 42 | return; |
| 43 | |
| 44 | vcpu->arch.ctxt.sys_regs[FPEXC32_EL2] = read_sysreg(fpexc32_el2); |
| 45 | } |
| 46 | |
Christoffer Dall | d5a21bc | 2017-08-04 08:50:25 +0200 | [diff] [blame] | 47 | static void __hyp_text __activate_traps_fpsimd32(struct kvm_vcpu *vcpu) |
| 48 | { |
| 49 | /* |
| 50 | * We are about to set CPTR_EL2.TFP to trap all floating point |
| 51 | * register accesses to EL2, however, the ARM ARM clearly states that |
| 52 | * traps are only taken to EL2 if the operation would not otherwise |
| 53 | * trap to EL1. Therefore, always make sure that for 32-bit guests, |
| 54 | * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit. |
| 55 | * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to |
| 56 | * it will cause an exception. |
| 57 | */ |
| 58 | if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) { |
| 59 | write_sysreg(1 << 30, fpexc32_el2); |
| 60 | isb(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void __hyp_text __activate_traps_common(struct kvm_vcpu *vcpu) |
| 65 | { |
| 66 | /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */ |
| 67 | write_sysreg(1 << 15, hstr_el2); |
| 68 | |
| 69 | /* |
| 70 | * Make sure we trap PMU access from EL0 to EL2. Also sanitize |
| 71 | * PMSELR_EL0 to make sure it never contains the cycle |
| 72 | * counter, which could make a PMXEVCNTR_EL0 access UNDEF at |
| 73 | * EL1 instead of being trapped to EL2. |
| 74 | */ |
| 75 | write_sysreg(0, pmselr_el0); |
| 76 | write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0); |
| 77 | write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2); |
| 78 | } |
| 79 | |
| 80 | static void __hyp_text __deactivate_traps_common(void) |
| 81 | { |
| 82 | write_sysreg(0, hstr_el2); |
| 83 | write_sysreg(0, pmuserenr_el0); |
| 84 | } |
| 85 | |
Christoffer Dall | b7787e6 | 2017-10-03 17:06:15 +0200 | [diff] [blame] | 86 | static void activate_traps_vhe(struct kvm_vcpu *vcpu) |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 87 | { |
| 88 | u64 val; |
| 89 | |
| 90 | val = read_sysreg(cpacr_el1); |
| 91 | val |= CPACR_EL1_TTA; |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 92 | val &= ~CPACR_EL1_ZEN; |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 93 | if (update_fp_enabled(vcpu)) { |
| 94 | if (vcpu_has_sve(vcpu)) |
| 95 | val |= CPACR_EL1_ZEN; |
| 96 | } else { |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 97 | val &= ~CPACR_EL1_FPEN; |
Marc Zyngier | 7d14919 | 2018-08-23 11:51:43 +0100 | [diff] [blame] | 98 | __activate_traps_fpsimd32(vcpu); |
| 99 | } |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 100 | |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 101 | write_sysreg(val, cpacr_el1); |
| 102 | |
Marc Zyngier | 6840bdd | 2018-01-03 16:38:35 +0000 | [diff] [blame] | 103 | write_sysreg(kvm_get_hyp_vector(), vbar_el1); |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 104 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 105 | NOKPROBE_SYMBOL(activate_traps_vhe); |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 106 | |
Christoffer Dall | d5a21bc | 2017-08-04 08:50:25 +0200 | [diff] [blame] | 107 | static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu) |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 108 | { |
| 109 | u64 val; |
| 110 | |
Christoffer Dall | a246562 | 2017-08-04 13:47:18 +0200 | [diff] [blame] | 111 | __activate_traps_common(vcpu); |
| 112 | |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 113 | val = CPTR_EL2_DEFAULT; |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 114 | val |= CPTR_EL2_TTA | CPTR_EL2_TZ; |
Marc Zyngier | 7d14919 | 2018-08-23 11:51:43 +0100 | [diff] [blame] | 115 | if (!update_fp_enabled(vcpu)) { |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 116 | val |= CPTR_EL2_TFP; |
Marc Zyngier | 7d14919 | 2018-08-23 11:51:43 +0100 | [diff] [blame] | 117 | __activate_traps_fpsimd32(vcpu); |
| 118 | } |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 119 | |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 120 | write_sysreg(val, cptr_el2); |
| 121 | } |
| 122 | |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 123 | static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu) |
| 124 | { |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 125 | u64 hcr = vcpu->arch.hcr_el2; |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 126 | |
Christoffer Dall | d5a21bc | 2017-08-04 08:50:25 +0200 | [diff] [blame] | 127 | write_sysreg(hcr, hcr_el2); |
Dave Martin | 93390c0 | 2017-10-31 15:50:56 +0000 | [diff] [blame] | 128 | |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 129 | if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE)) |
James Morse | 4715c14 | 2018-01-15 19:39:01 +0000 | [diff] [blame] | 130 | write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2); |
| 131 | |
Christoffer Dall | b7787e6 | 2017-10-03 17:06:15 +0200 | [diff] [blame] | 132 | if (has_vhe()) |
| 133 | activate_traps_vhe(vcpu); |
| 134 | else |
| 135 | __activate_traps_nvhe(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Christoffer Dall | b7787e6 | 2017-10-03 17:06:15 +0200 | [diff] [blame] | 138 | static void deactivate_traps_vhe(void) |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 139 | { |
| 140 | extern char vectors[]; /* kernel exception vectors */ |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 141 | write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2); |
Marc Zyngier | 1e4448c | 2018-12-06 17:31:24 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * ARM erratum 1165522 requires the actual execution of the above |
| 145 | * before we can switch to the EL2/EL0 translation regime used by |
| 146 | * the host. |
| 147 | */ |
| 148 | asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_1165522)); |
| 149 | |
Dave Martin | 17eed27 | 2017-10-31 15:51:16 +0000 | [diff] [blame] | 150 | write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1); |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 151 | write_sysreg(vectors, vbar_el1); |
| 152 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 153 | NOKPROBE_SYMBOL(deactivate_traps_vhe); |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 154 | |
| 155 | static void __hyp_text __deactivate_traps_nvhe(void) |
| 156 | { |
Will Deacon | f85279b | 2016-09-22 11:35:43 +0100 | [diff] [blame] | 157 | u64 mdcr_el2 = read_sysreg(mdcr_el2); |
| 158 | |
Christoffer Dall | a246562 | 2017-08-04 13:47:18 +0200 | [diff] [blame] | 159 | __deactivate_traps_common(); |
| 160 | |
Will Deacon | f85279b | 2016-09-22 11:35:43 +0100 | [diff] [blame] | 161 | mdcr_el2 &= MDCR_EL2_HPMN_MASK; |
| 162 | mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT; |
| 163 | |
| 164 | write_sysreg(mdcr_el2, mdcr_el2); |
Mark Rutland | 4eaed6a | 2018-12-07 18:39:21 +0000 | [diff] [blame] | 165 | write_sysreg(HCR_HOST_NVHE_FLAGS, hcr_el2); |
Marc Zyngier | 68908bf | 2015-01-29 15:47:55 +0000 | [diff] [blame] | 166 | write_sysreg(CPTR_EL2_DEFAULT, cptr_el2); |
| 167 | } |
| 168 | |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 169 | static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu) |
| 170 | { |
Marc Zyngier | 44636f9 | 2016-09-06 14:02:00 +0100 | [diff] [blame] | 171 | /* |
| 172 | * If we pended a virtual abort, preserve it until it gets |
| 173 | * cleared. See D1.14.3 (Virtual Interrupts) for details, but |
| 174 | * the crucial bit is "On taking a vSError interrupt, |
| 175 | * HCR_EL2.VSE is cleared to 0." |
| 176 | */ |
| 177 | if (vcpu->arch.hcr_el2 & HCR_VSE) |
| 178 | vcpu->arch.hcr_el2 = read_sysreg(hcr_el2); |
| 179 | |
Christoffer Dall | b7787e6 | 2017-10-03 17:06:15 +0200 | [diff] [blame] | 180 | if (has_vhe()) |
| 181 | deactivate_traps_vhe(); |
| 182 | else |
| 183 | __deactivate_traps_nvhe(); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Christoffer Dall | a246562 | 2017-08-04 13:47:18 +0200 | [diff] [blame] | 186 | void activate_traps_vhe_load(struct kvm_vcpu *vcpu) |
| 187 | { |
| 188 | __activate_traps_common(vcpu); |
| 189 | } |
| 190 | |
| 191 | void deactivate_traps_vhe_put(void) |
| 192 | { |
| 193 | u64 mdcr_el2 = read_sysreg(mdcr_el2); |
| 194 | |
| 195 | mdcr_el2 &= MDCR_EL2_HPMN_MASK | |
| 196 | MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT | |
| 197 | MDCR_EL2_TPMS; |
| 198 | |
| 199 | write_sysreg(mdcr_el2, mdcr_el2); |
| 200 | |
| 201 | __deactivate_traps_common(); |
| 202 | } |
| 203 | |
Christoffer Dall | 34f8cdf | 2017-10-10 13:25:21 +0200 | [diff] [blame] | 204 | static void __hyp_text __activate_vm(struct kvm *kvm) |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 205 | { |
Suzuki K Poulose | 9f98ddd | 2018-09-26 17:32:39 +0100 | [diff] [blame] | 206 | __load_guest_stage2(kvm); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu) |
| 210 | { |
| 211 | write_sysreg(0, vttbr_el2); |
| 212 | } |
| 213 | |
Christoffer Dall | 771621b | 2017-10-04 23:42:32 +0200 | [diff] [blame] | 214 | /* Save VGICv3 state on non-VHE systems */ |
| 215 | static void __hyp_text __hyp_vgic_save_state(struct kvm_vcpu *vcpu) |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 216 | { |
Christoffer Dall | 2d0e63e | 2017-10-05 17:19:19 +0200 | [diff] [blame] | 217 | if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) { |
Vladimir Murzin | 5a7a842 | 2016-09-12 15:49:15 +0100 | [diff] [blame] | 218 | __vgic_v3_save_state(vcpu); |
Christoffer Dall | 2d0e63e | 2017-10-05 17:19:19 +0200 | [diff] [blame] | 219 | __vgic_v3_deactivate_traps(vcpu); |
| 220 | } |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Christoffer Dall | 771621b | 2017-10-04 23:42:32 +0200 | [diff] [blame] | 223 | /* Restore VGICv3 state on non_VEH systems */ |
| 224 | static void __hyp_text __hyp_vgic_restore_state(struct kvm_vcpu *vcpu) |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 225 | { |
Christoffer Dall | 2d0e63e | 2017-10-05 17:19:19 +0200 | [diff] [blame] | 226 | if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) { |
| 227 | __vgic_v3_activate_traps(vcpu); |
Vladimir Murzin | 5a7a842 | 2016-09-12 15:49:15 +0100 | [diff] [blame] | 228 | __vgic_v3_restore_state(vcpu); |
Christoffer Dall | 2d0e63e | 2017-10-05 17:19:19 +0200 | [diff] [blame] | 229 | } |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 232 | static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar) |
| 233 | { |
| 234 | u64 par, tmp; |
| 235 | |
| 236 | /* |
| 237 | * Resolve the IPA the hard way using the guest VA. |
| 238 | * |
| 239 | * Stage-1 translation already validated the memory access |
| 240 | * rights. As such, we can use the EL1 translation regime, and |
| 241 | * don't have to distinguish between EL0 and EL1 access. |
| 242 | * |
| 243 | * We do need to save/restore PAR_EL1 though, as we haven't |
| 244 | * saved the guest context yet, and we may return early... |
| 245 | */ |
| 246 | par = read_sysreg(par_el1); |
| 247 | asm volatile("at s1e1r, %0" : : "r" (far)); |
| 248 | isb(); |
| 249 | |
| 250 | tmp = read_sysreg(par_el1); |
| 251 | write_sysreg(par, par_el1); |
| 252 | |
Will Deacon | 5c062ef | 2019-08-22 17:21:21 +0100 | [diff] [blame] | 253 | if (unlikely(tmp & SYS_PAR_EL1_F)) |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 254 | return false; /* Translation failed, back to guest */ |
| 255 | |
| 256 | /* Convert PAR to HPFAR format */ |
Suzuki K Poulose | bc1d7de | 2018-09-26 17:32:51 +0100 | [diff] [blame] | 257 | *hpfar = PAR_TO_HPFAR(tmp); |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
| 261 | static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu) |
| 262 | { |
James Morse | c60590b | 2018-01-15 19:39:03 +0000 | [diff] [blame] | 263 | u8 ec; |
| 264 | u64 esr; |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 265 | u64 hpfar, far; |
| 266 | |
James Morse | c60590b | 2018-01-15 19:39:03 +0000 | [diff] [blame] | 267 | esr = vcpu->arch.fault.esr_el2; |
| 268 | ec = ESR_ELx_EC(esr); |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 269 | |
| 270 | if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW) |
| 271 | return true; |
| 272 | |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 273 | far = read_sysreg_el2(SYS_FAR); |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * The HPFAR can be invalid if the stage 2 fault did not |
| 277 | * happen during a stage 1 page table walk (the ESR_EL2.S1PTW |
| 278 | * bit is clear) and one of the two following cases are true: |
| 279 | * 1. The fault was due to a permission fault |
| 280 | * 2. The processor carries errata 834220 |
| 281 | * |
| 282 | * Therefore, for all non S1PTW faults where we either have a |
| 283 | * permission fault or the errata workaround is enabled, we |
| 284 | * resolve the IPA using the AT instruction. |
| 285 | */ |
| 286 | if (!(esr & ESR_ELx_S1PTW) && |
Marc Zyngier | b6749e2 | 2019-09-01 22:12:35 +0100 | [diff] [blame] | 287 | (cpus_have_const_cap(ARM64_WORKAROUND_834220) || |
| 288 | (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) { |
Marc Zyngier | 5f05a72a | 2015-10-28 15:06:47 +0000 | [diff] [blame] | 289 | if (!__translate_far_to_hpfar(far, &hpfar)) |
| 290 | return false; |
| 291 | } else { |
| 292 | hpfar = read_sysreg(hpfar_el2); |
| 293 | } |
| 294 | |
| 295 | vcpu->arch.fault.far_el2 = far; |
| 296 | vcpu->arch.fault.hpfar_el2 = hpfar; |
| 297 | return true; |
| 298 | } |
| 299 | |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 300 | /* Check for an FPSIMD/SVE trap and handle as appropriate */ |
| 301 | static bool __hyp_text __hyp_handle_fpsimd(struct kvm_vcpu *vcpu) |
Dave Martin | ceda9ff | 2018-02-16 16:35:32 +0000 | [diff] [blame] | 302 | { |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 303 | bool vhe, sve_guest, sve_host; |
| 304 | u8 hsr_ec; |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 305 | |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 306 | if (!system_supports_fpsimd()) |
| 307 | return false; |
| 308 | |
| 309 | if (system_supports_sve()) { |
| 310 | sve_guest = vcpu_has_sve(vcpu); |
| 311 | sve_host = vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE; |
| 312 | vhe = true; |
| 313 | } else { |
| 314 | sve_guest = false; |
| 315 | sve_host = false; |
| 316 | vhe = has_vhe(); |
| 317 | } |
| 318 | |
| 319 | hsr_ec = kvm_vcpu_trap_get_class(vcpu); |
| 320 | if (hsr_ec != ESR_ELx_EC_FP_ASIMD && |
| 321 | hsr_ec != ESR_ELx_EC_SVE) |
| 322 | return false; |
| 323 | |
| 324 | /* Don't handle SVE traps for non-SVE vcpus here: */ |
| 325 | if (!sve_guest) |
| 326 | if (hsr_ec != ESR_ELx_EC_FP_ASIMD) |
| 327 | return false; |
| 328 | |
| 329 | /* Valid trap. Switch the context: */ |
| 330 | |
| 331 | if (vhe) { |
| 332 | u64 reg = read_sysreg(cpacr_el1) | CPACR_EL1_FPEN; |
| 333 | |
| 334 | if (sve_guest) |
| 335 | reg |= CPACR_EL1_ZEN; |
| 336 | |
| 337 | write_sysreg(reg, cpacr_el1); |
| 338 | } else { |
Dave Martin | ceda9ff | 2018-02-16 16:35:32 +0000 | [diff] [blame] | 339 | write_sysreg(read_sysreg(cptr_el2) & ~(u64)CPTR_EL2_TFP, |
| 340 | cptr_el2); |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 341 | } |
Dave Martin | ceda9ff | 2018-02-16 16:35:32 +0000 | [diff] [blame] | 342 | |
| 343 | isb(); |
| 344 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 345 | if (vcpu->arch.flags & KVM_ARM64_FP_HOST) { |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 346 | /* |
| 347 | * In the SVE case, VHE is assumed: it is enforced by |
| 348 | * Kconfig and kvm_arch_init(). |
| 349 | */ |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 350 | if (sve_host) { |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 351 | struct thread_struct *thread = container_of( |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 352 | vcpu->arch.host_fpsimd_state, |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 353 | struct thread_struct, uw.fpsimd_state); |
| 354 | |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 355 | sve_save_state(sve_pffr(thread), |
| 356 | &vcpu->arch.host_fpsimd_state->fpsr); |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 357 | } else { |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 358 | __fpsimd_save_state(vcpu->arch.host_fpsimd_state); |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 361 | vcpu->arch.flags &= ~KVM_ARM64_FP_HOST; |
| 362 | } |
| 363 | |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 364 | if (sve_guest) { |
| 365 | sve_load_state(vcpu_sve_pffr(vcpu), |
| 366 | &vcpu->arch.ctxt.gp_regs.fp_regs.fpsr, |
| 367 | sve_vq_from_vl(vcpu->arch.sve_max_vl) - 1); |
Dave Martin | 7343376 | 2018-09-28 14:39:16 +0100 | [diff] [blame] | 368 | write_sysreg_s(vcpu->arch.ctxt.sys_regs[ZCR_EL1], SYS_ZCR_EL12); |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 369 | } else { |
| 370 | __fpsimd_restore_state(&vcpu->arch.ctxt.gp_regs.fp_regs); |
| 371 | } |
Dave Martin | 7343376 | 2018-09-28 14:39:16 +0100 | [diff] [blame] | 372 | |
Dave Martin | ceda9ff | 2018-02-16 16:35:32 +0000 | [diff] [blame] | 373 | /* Skip restoring fpexc32 for AArch64 guests */ |
| 374 | if (!(read_sysreg(hcr_el2) & HCR_RW)) |
| 375 | write_sysreg(vcpu->arch.ctxt.sys_regs[FPEXC32_EL2], |
| 376 | fpexc32_el2); |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 377 | |
| 378 | vcpu->arch.flags |= KVM_ARM64_FP_ENABLED; |
Dave Martin | cf412b0 | 2018-05-02 14:18:02 +0100 | [diff] [blame] | 379 | |
| 380 | return true; |
Dave Martin | ceda9ff | 2018-02-16 16:35:32 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 383 | /* |
| 384 | * Return true when we were able to fixup the guest exit and should return to |
| 385 | * the guest, false when we should restore the host state and return to the |
| 386 | * main run loop. |
| 387 | */ |
| 388 | static bool __hyp_text fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code) |
| 389 | { |
| 390 | if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ) |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 391 | vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR); |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * We're using the raw exception code in order to only process |
| 395 | * the trap if no SError is pending. We will come back to the |
| 396 | * same PC once the SError has been injected, and replay the |
| 397 | * trapping instruction. |
| 398 | */ |
Dave Martin | 7846b31 | 2018-05-02 13:36:48 +0100 | [diff] [blame] | 399 | if (*exit_code != ARM_EXCEPTION_TRAP) |
| 400 | goto exit; |
| 401 | |
Dave Martin | cf412b0 | 2018-05-02 14:18:02 +0100 | [diff] [blame] | 402 | /* |
| 403 | * We trap the first access to the FP/SIMD to save the host context |
| 404 | * and restore the guest context lazily. |
| 405 | * If FP/SIMD is not implemented, handle the trap and inject an |
| 406 | * undefined instruction exception to the guest. |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 407 | * Similarly for trapped SVE accesses. |
Dave Martin | cf412b0 | 2018-05-02 14:18:02 +0100 | [diff] [blame] | 408 | */ |
Dave Martin | b43b5dd | 2018-09-28 14:39:17 +0100 | [diff] [blame] | 409 | if (__hyp_handle_fpsimd(vcpu)) |
| 410 | return true; |
Dave Martin | cf412b0 | 2018-05-02 14:18:02 +0100 | [diff] [blame] | 411 | |
Dave Martin | 7846b31 | 2018-05-02 13:36:48 +0100 | [diff] [blame] | 412 | if (!__populate_fault_info(vcpu)) |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 413 | return true; |
| 414 | |
Dave Martin | 7846b31 | 2018-05-02 13:36:48 +0100 | [diff] [blame] | 415 | if (static_branch_unlikely(&vgic_v2_cpuif_trap)) { |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 416 | bool valid; |
| 417 | |
| 418 | valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW && |
| 419 | kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT && |
| 420 | kvm_vcpu_dabt_isvalid(vcpu) && |
| 421 | !kvm_vcpu_dabt_isextabt(vcpu) && |
| 422 | !kvm_vcpu_dabt_iss1tw(vcpu); |
| 423 | |
| 424 | if (valid) { |
| 425 | int ret = __vgic_v2_perform_cpuif_access(vcpu); |
| 426 | |
Mark Rutland | bd7d95c | 2018-11-09 15:07:11 +0000 | [diff] [blame] | 427 | if (ret == 1) |
Dave Martin | ba4f4cb | 2018-05-02 13:23:07 +0100 | [diff] [blame] | 428 | return true; |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 429 | |
Mark Rutland | bd7d95c | 2018-11-09 15:07:11 +0000 | [diff] [blame] | 430 | /* Promote an illegal access to an SError.*/ |
| 431 | if (ret == -1) |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 432 | *exit_code = ARM_EXCEPTION_EL1_SERROR; |
Dave Martin | 7846b31 | 2018-05-02 13:36:48 +0100 | [diff] [blame] | 433 | |
| 434 | goto exit; |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | if (static_branch_unlikely(&vgic_v3_cpuif_trap) && |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 439 | (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 || |
| 440 | kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) { |
| 441 | int ret = __vgic_v3_perform_cpuif_access(vcpu); |
| 442 | |
Mark Rutland | bd7d95c | 2018-11-09 15:07:11 +0000 | [diff] [blame] | 443 | if (ret == 1) |
Dave Martin | ba4f4cb | 2018-05-02 13:23:07 +0100 | [diff] [blame] | 444 | return true; |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Dave Martin | 7846b31 | 2018-05-02 13:36:48 +0100 | [diff] [blame] | 447 | exit: |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 448 | /* Return to the host kernel and handle the exit */ |
| 449 | return false; |
| 450 | } |
| 451 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 452 | static inline bool __hyp_text __needs_ssbd_off(struct kvm_vcpu *vcpu) |
| 453 | { |
| 454 | if (!cpus_have_const_cap(ARM64_SSBD)) |
| 455 | return false; |
| 456 | |
| 457 | return !(vcpu->arch.workaround_flags & VCPU_WORKAROUND_2_FLAG); |
| 458 | } |
| 459 | |
| 460 | static void __hyp_text __set_guest_arch_workaround_state(struct kvm_vcpu *vcpu) |
| 461 | { |
| 462 | #ifdef CONFIG_ARM64_SSBD |
| 463 | /* |
| 464 | * The host runs with the workaround always present. If the |
| 465 | * guest wants it disabled, so be it... |
| 466 | */ |
| 467 | if (__needs_ssbd_off(vcpu) && |
| 468 | __hyp_this_cpu_read(arm64_ssbd_callback_required)) |
| 469 | arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 0, NULL); |
| 470 | #endif |
| 471 | } |
| 472 | |
| 473 | static void __hyp_text __set_host_arch_workaround_state(struct kvm_vcpu *vcpu) |
| 474 | { |
| 475 | #ifdef CONFIG_ARM64_SSBD |
| 476 | /* |
| 477 | * If the guest has disabled the workaround, bring it back on. |
| 478 | */ |
| 479 | if (__needs_ssbd_off(vcpu) && |
| 480 | __hyp_this_cpu_read(arm64_ssbd_callback_required)) |
| 481 | arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, 1, NULL); |
| 482 | #endif |
| 483 | } |
| 484 | |
James Morse | b7c50fa | 2019-05-22 18:47:04 +0100 | [diff] [blame] | 485 | /** |
| 486 | * Disable host events, enable guest events |
| 487 | */ |
| 488 | static bool __hyp_text __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt) |
| 489 | { |
| 490 | struct kvm_host_data *host; |
| 491 | struct kvm_pmu_events *pmu; |
| 492 | |
| 493 | host = container_of(host_ctxt, struct kvm_host_data, host_ctxt); |
| 494 | pmu = &host->pmu_events; |
| 495 | |
| 496 | if (pmu->events_host) |
| 497 | write_sysreg(pmu->events_host, pmcntenclr_el0); |
| 498 | |
| 499 | if (pmu->events_guest) |
| 500 | write_sysreg(pmu->events_guest, pmcntenset_el0); |
| 501 | |
| 502 | return (pmu->events_host || pmu->events_guest); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Disable guest events, enable host events |
| 507 | */ |
| 508 | static void __hyp_text __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt) |
| 509 | { |
| 510 | struct kvm_host_data *host; |
| 511 | struct kvm_pmu_events *pmu; |
| 512 | |
| 513 | host = container_of(host_ctxt, struct kvm_host_data, host_ctxt); |
| 514 | pmu = &host->pmu_events; |
| 515 | |
| 516 | if (pmu->events_guest) |
| 517 | write_sysreg(pmu->events_guest, pmcntenclr_el0); |
| 518 | |
| 519 | if (pmu->events_host) |
| 520 | write_sysreg(pmu->events_host, pmcntenset_el0); |
| 521 | } |
| 522 | |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 523 | /* Switch to the guest for VHE systems running in EL2 */ |
| 524 | int kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu) |
| 525 | { |
| 526 | struct kvm_cpu_context *host_ctxt; |
| 527 | struct kvm_cpu_context *guest_ctxt; |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 528 | u64 exit_code; |
| 529 | |
Christoffer Dall | 86d0568 | 2016-12-23 00:20:38 +0100 | [diff] [blame] | 530 | host_ctxt = vcpu->arch.host_cpu_context; |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 531 | host_ctxt->__hyp_running_vcpu = vcpu; |
| 532 | guest_ctxt = &vcpu->arch.ctxt; |
| 533 | |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 534 | sysreg_save_host_state_vhe(host_ctxt); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 535 | |
Marc Zyngier | 1e4448c | 2018-12-06 17:31:24 +0000 | [diff] [blame] | 536 | /* |
| 537 | * ARM erratum 1165522 requires us to configure both stage 1 and |
| 538 | * stage 2 translation for the guest context before we clear |
| 539 | * HCR_EL2.TGE. |
| 540 | * |
| 541 | * We have already configured the guest's stage 1 translation in |
| 542 | * kvm_vcpu_load_sysregs above. We must now call __activate_vm |
| 543 | * before __activate_traps, because __activate_vm configures |
| 544 | * stage 2 translation, and __activate_traps clear HCR_EL2.TGE |
| 545 | * (among other things). |
| 546 | */ |
Christoffer Dall | 34f8cdf | 2017-10-10 13:25:21 +0200 | [diff] [blame] | 547 | __activate_vm(vcpu->kvm); |
Marc Zyngier | bfae1b9 | 2018-12-06 17:31:21 +0000 | [diff] [blame] | 548 | __activate_traps(vcpu); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 549 | |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 550 | sysreg_restore_guest_state_vhe(guest_ctxt); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 551 | __debug_switch_to_guest(vcpu); |
| 552 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 553 | __set_guest_arch_workaround_state(vcpu); |
| 554 | |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 555 | do { |
| 556 | /* Jump in the fire! */ |
| 557 | exit_code = __guest_enter(vcpu, host_ctxt); |
| 558 | |
| 559 | /* And we're baaack! */ |
| 560 | } while (fixup_guest_exit(vcpu, &exit_code)); |
| 561 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 562 | __set_host_arch_workaround_state(vcpu); |
| 563 | |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 564 | sysreg_save_guest_state_vhe(guest_ctxt); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 565 | |
| 566 | __deactivate_traps(vcpu); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 567 | |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 568 | sysreg_restore_host_state_vhe(host_ctxt); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 569 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 570 | if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) |
Christoffer Dall | b9f8ca4 | 2017-12-27 22:12:12 +0100 | [diff] [blame] | 571 | __fpsimd_save_fpexc32(vcpu); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 572 | |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 573 | __debug_switch_to_host(vcpu); |
| 574 | |
| 575 | return exit_code; |
| 576 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 577 | NOKPROBE_SYMBOL(kvm_vcpu_run_vhe); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 578 | |
| 579 | /* Switch to the guest for legacy non-VHE systems */ |
| 580 | int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu) |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 581 | { |
| 582 | struct kvm_cpu_context *host_ctxt; |
| 583 | struct kvm_cpu_context *guest_ctxt; |
Andrew Murray | 3d91bef | 2019-04-09 20:22:14 +0100 | [diff] [blame] | 584 | bool pmu_switch_needed; |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 585 | u64 exit_code; |
| 586 | |
Julien Thierry | 85738e0 | 2019-01-31 14:58:48 +0000 | [diff] [blame] | 587 | /* |
| 588 | * Having IRQs masked via PMR when entering the guest means the GIC |
| 589 | * will not signal the CPU of interrupts of lower priority, and the |
| 590 | * only way to get out will be via guest exceptions. |
| 591 | * Naturally, we want to avoid this. |
| 592 | */ |
| 593 | if (system_uses_irq_prio_masking()) { |
Julien Thierry | bd82d4b | 2019-06-11 10:38:10 +0100 | [diff] [blame] | 594 | gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); |
Julien Thierry | 85738e0 | 2019-01-31 14:58:48 +0000 | [diff] [blame] | 595 | dsb(sy); |
| 596 | } |
| 597 | |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 598 | vcpu = kern_hyp_va(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 599 | |
| 600 | host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context); |
James Morse | c97e166 | 2018-01-08 15:38:05 +0000 | [diff] [blame] | 601 | host_ctxt->__hyp_running_vcpu = vcpu; |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 602 | guest_ctxt = &vcpu->arch.ctxt; |
| 603 | |
Andrew Murray | 3d91bef | 2019-04-09 20:22:14 +0100 | [diff] [blame] | 604 | pmu_switch_needed = __pmu_switch_to_guest(host_ctxt); |
| 605 | |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 606 | __sysreg_save_state_nvhe(host_ctxt); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 607 | |
Christoffer Dall | 34f8cdf | 2017-10-10 13:25:21 +0200 | [diff] [blame] | 608 | __activate_vm(kern_hyp_va(vcpu->kvm)); |
Marc Zyngier | bfae1b9 | 2018-12-06 17:31:21 +0000 | [diff] [blame] | 609 | __activate_traps(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 610 | |
Christoffer Dall | 771621b | 2017-10-04 23:42:32 +0200 | [diff] [blame] | 611 | __hyp_vgic_restore_state(vcpu); |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 612 | __timer_enable_traps(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * We must restore the 32-bit state before the sysregs, thanks |
Marc Zyngier | 674e701 | 2016-08-16 15:03:01 +0100 | [diff] [blame] | 616 | * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72). |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 617 | */ |
| 618 | __sysreg32_restore_state(vcpu); |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 619 | __sysreg_restore_state_nvhe(guest_ctxt); |
Christoffer Dall | 014c4c7 | 2017-10-10 20:10:08 +0200 | [diff] [blame] | 620 | __debug_switch_to_guest(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 621 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 622 | __set_guest_arch_workaround_state(vcpu); |
| 623 | |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 624 | do { |
| 625 | /* Jump in the fire! */ |
| 626 | exit_code = __guest_enter(vcpu, host_ctxt); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 627 | |
Christoffer Dall | dc25140 | 2017-10-03 13:16:04 +0200 | [diff] [blame] | 628 | /* And we're baaack! */ |
| 629 | } while (fixup_guest_exit(vcpu, &exit_code)); |
Marc Zyngier | 59da1cb | 2017-06-09 12:49:33 +0100 | [diff] [blame] | 630 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 631 | __set_host_arch_workaround_state(vcpu); |
| 632 | |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 633 | __sysreg_save_state_nvhe(guest_ctxt); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 634 | __sysreg32_save_state(vcpu); |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 635 | __timer_disable_traps(vcpu); |
Christoffer Dall | 771621b | 2017-10-04 23:42:32 +0200 | [diff] [blame] | 636 | __hyp_vgic_save_state(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 637 | |
| 638 | __deactivate_traps(vcpu); |
| 639 | __deactivate_vm(vcpu); |
| 640 | |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 641 | __sysreg_restore_state_nvhe(host_ctxt); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 642 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 643 | if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) |
Christoffer Dall | b9f8ca4 | 2017-12-27 22:12:12 +0100 | [diff] [blame] | 644 | __fpsimd_save_fpexc32(vcpu); |
Marc Zyngier | c13d168 | 2015-10-26 08:34:09 +0000 | [diff] [blame] | 645 | |
Will Deacon | f85279b | 2016-09-22 11:35:43 +0100 | [diff] [blame] | 646 | /* |
| 647 | * This must come after restoring the host sysregs, since a non-VHE |
| 648 | * system may enable SPE here and make use of the TTBRs. |
| 649 | */ |
Christoffer Dall | 014c4c7 | 2017-10-10 20:10:08 +0200 | [diff] [blame] | 650 | __debug_switch_to_host(vcpu); |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 651 | |
Andrew Murray | 3d91bef | 2019-04-09 20:22:14 +0100 | [diff] [blame] | 652 | if (pmu_switch_needed) |
| 653 | __pmu_switch_to_host(host_ctxt); |
| 654 | |
Julien Thierry | 85738e0 | 2019-01-31 14:58:48 +0000 | [diff] [blame] | 655 | /* Returning to host will clear PSR.I, remask PMR if needed */ |
| 656 | if (system_uses_irq_prio_masking()) |
| 657 | gic_write_pmr(GIC_PRIO_IRQOFF); |
| 658 | |
Marc Zyngier | be901e9 | 2015-10-21 09:57:10 +0100 | [diff] [blame] | 659 | return exit_code; |
| 660 | } |
Marc Zyngier | 53fd5b6 | 2015-10-25 15:21:52 +0000 | [diff] [blame] | 661 | |
| 662 | static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n"; |
| 663 | |
James Morse | c97e166 | 2018-01-08 15:38:05 +0000 | [diff] [blame] | 664 | static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par, |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 665 | struct kvm_cpu_context *__host_ctxt) |
Marc Zyngier | 53fd5b6 | 2015-10-25 15:21:52 +0000 | [diff] [blame] | 666 | { |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 667 | struct kvm_vcpu *vcpu; |
Marc Zyngier | cf7df13 | 2016-06-30 18:40:35 +0100 | [diff] [blame] | 668 | unsigned long str_va; |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 669 | |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 670 | vcpu = __host_ctxt->__hyp_running_vcpu; |
| 671 | |
| 672 | if (read_sysreg(vttbr_el2)) { |
| 673 | __timer_disable_traps(vcpu); |
| 674 | __deactivate_traps(vcpu); |
| 675 | __deactivate_vm(vcpu); |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 676 | __sysreg_restore_state_nvhe(__host_ctxt); |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Marc Zyngier | cf7df13 | 2016-06-30 18:40:35 +0100 | [diff] [blame] | 679 | /* |
| 680 | * Force the panic string to be loaded from the literal pool, |
| 681 | * making sure it is a kernel address and not a PC-relative |
| 682 | * reference. |
| 683 | */ |
| 684 | asm volatile("ldr %0, =__hyp_panic_string" : "=r" (str_va)); |
| 685 | |
| 686 | __hyp_do_panic(str_va, |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 687 | spsr, elr, |
| 688 | read_sysreg(esr_el2), read_sysreg_el2(SYS_FAR), |
James Morse | c97e166 | 2018-01-08 15:38:05 +0000 | [diff] [blame] | 689 | read_sysreg(hpfar_el2), par, vcpu); |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 692 | static void __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par, |
| 693 | struct kvm_cpu_context *host_ctxt) |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 694 | { |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 695 | struct kvm_vcpu *vcpu; |
| 696 | vcpu = host_ctxt->__hyp_running_vcpu; |
| 697 | |
| 698 | __deactivate_traps(vcpu); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 699 | sysreg_restore_host_state_vhe(host_ctxt); |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 700 | |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 701 | panic(__hyp_panic_string, |
| 702 | spsr, elr, |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 703 | read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR), |
James Morse | c97e166 | 2018-01-08 15:38:05 +0000 | [diff] [blame] | 704 | read_sysreg(hpfar_el2), par, vcpu); |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 705 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 706 | NOKPROBE_SYMBOL(__hyp_call_panic_vhe); |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 707 | |
Christoffer Dall | 4464e21 | 2017-10-08 17:01:56 +0200 | [diff] [blame] | 708 | void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *host_ctxt) |
Marc Zyngier | 253dcbd | 2015-11-17 14:07:45 +0000 | [diff] [blame] | 709 | { |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 710 | u64 spsr = read_sysreg_el2(SYS_SPSR); |
| 711 | u64 elr = read_sysreg_el2(SYS_ELR); |
Marc Zyngier | 53fd5b6 | 2015-10-25 15:21:52 +0000 | [diff] [blame] | 712 | u64 par = read_sysreg(par_el1); |
| 713 | |
Christoffer Dall | 8f17f5e | 2017-10-09 21:43:50 +0200 | [diff] [blame] | 714 | if (!has_vhe()) |
| 715 | __hyp_call_panic_nvhe(spsr, elr, par, host_ctxt); |
| 716 | else |
| 717 | __hyp_call_panic_vhe(spsr, elr, par, host_ctxt); |
Marc Zyngier | 53fd5b6 | 2015-10-25 15:21:52 +0000 | [diff] [blame] | 718 | |
| 719 | unreachable(); |
| 720 | } |