blob: 324f4202cdd50a35573d3a9aa12ac28df8a97327 [file] [log] [blame]
Marc Zyngierbe901e92015-10-21 09:57:10 +01001/*
2 * Copyright (C) 2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Marc Zyngier5f05a72a2015-10-28 15:06:47 +000018#include <linux/types.h>
Vladimir Murzin5a7a8422016-09-12 15:49:15 +010019#include <linux/jump_label.h>
Marc Zyngier90348682018-01-03 16:38:37 +000020#include <uapi/linux/psci.h>
Vladimir Murzin5a7a8422016-09-12 15:49:15 +010021
Marc Zyngier68908bf2015-01-29 15:47:55 +000022#include <asm/kvm_asm.h>
Marc Zyngierfb5ee362016-09-06 09:28:45 +010023#include <asm/kvm_emulate.h>
Marc Zyngier13720a52016-01-28 13:44:07 +000024#include <asm/kvm_hyp.h>
Suzuki K Poulose82e01912016-11-08 13:56:21 +000025#include <asm/fpsimd.h>
Alex Bennéee3feebf2017-11-23 12:11:34 +000026#include <asm/debug-monitors.h>
Marc Zyngierbe901e92015-10-21 09:57:10 +010027
Marc Zyngier32876222015-10-28 14:15:45 +000028static bool __hyp_text __fpsimd_enabled_nvhe(void)
29{
30 return !(read_sysreg(cptr_el2) & CPTR_EL2_TFP);
31}
32
33static bool __hyp_text __fpsimd_enabled_vhe(void)
34{
35 return !!(read_sysreg(cpacr_el1) & CPACR_EL1_FPEN);
36}
37
38static hyp_alternate_select(__fpsimd_is_enabled,
39 __fpsimd_enabled_nvhe, __fpsimd_enabled_vhe,
40 ARM64_HAS_VIRT_HOST_EXTN);
41
42bool __hyp_text __fpsimd_enabled(void)
43{
44 return __fpsimd_is_enabled()();
45}
46
Marc Zyngier68908bf2015-01-29 15:47:55 +000047static void __hyp_text __activate_traps_vhe(void)
48{
49 u64 val;
50
51 val = read_sysreg(cpacr_el1);
52 val |= CPACR_EL1_TTA;
Dave Martin17eed272017-10-31 15:51:16 +000053 val &= ~(CPACR_EL1_FPEN | CPACR_EL1_ZEN);
Marc Zyngier68908bf2015-01-29 15:47:55 +000054 write_sysreg(val, cpacr_el1);
55
Marc Zyngier6840bdd2018-01-03 16:38:35 +000056 write_sysreg(kvm_get_hyp_vector(), vbar_el1);
Marc Zyngier68908bf2015-01-29 15:47:55 +000057}
58
59static void __hyp_text __activate_traps_nvhe(void)
60{
61 u64 val;
62
63 val = CPTR_EL2_DEFAULT;
Dave Martin17eed272017-10-31 15:51:16 +000064 val |= CPTR_EL2_TTA | CPTR_EL2_TFP | CPTR_EL2_TZ;
Marc Zyngier68908bf2015-01-29 15:47:55 +000065 write_sysreg(val, cptr_el2);
66}
67
68static hyp_alternate_select(__activate_traps_arch,
69 __activate_traps_nvhe, __activate_traps_vhe,
70 ARM64_HAS_VIRT_HOST_EXTN);
71
Marc Zyngierbe901e92015-10-21 09:57:10 +010072static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
73{
74 u64 val;
75
76 /*
77 * We are about to set CPTR_EL2.TFP to trap all floating point
78 * register accesses to EL2, however, the ARM ARM clearly states that
79 * traps are only taken to EL2 if the operation would not otherwise
80 * trap to EL1. Therefore, always make sure that for 32-bit guests,
81 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
Suzuki K Poulose82e01912016-11-08 13:56:21 +000082 * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
83 * it will cause an exception.
Marc Zyngierbe901e92015-10-21 09:57:10 +010084 */
85 val = vcpu->arch.hcr_el2;
Dave Martin93390c02017-10-31 15:50:56 +000086
Suzuki K Poulose82e01912016-11-08 13:56:21 +000087 if (!(val & HCR_RW) && system_supports_fpsimd()) {
Marc Zyngierbe901e92015-10-21 09:57:10 +010088 write_sysreg(1 << 30, fpexc32_el2);
89 isb();
90 }
Dave Martin93390c02017-10-31 15:50:56 +000091
92 if (val & HCR_RW) /* for AArch64 only: */
93 val |= HCR_TID3; /* TID3: trap feature register accesses */
94
Marc Zyngierbe901e92015-10-21 09:57:10 +010095 write_sysreg(val, hcr_el2);
Dave Martin93390c02017-10-31 15:50:56 +000096
Marc Zyngierbe901e92015-10-21 09:57:10 +010097 /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
98 write_sysreg(1 << 15, hstr_el2);
Marc Zyngier21cbe3c2016-12-06 14:34:22 +000099 /*
100 * Make sure we trap PMU access from EL0 to EL2. Also sanitize
101 * PMSELR_EL0 to make sure it never contains the cycle
102 * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
103 * EL1 instead of being trapped to EL2.
104 */
105 write_sysreg(0, pmselr_el0);
Shannon Zhaod692b8a2015-09-08 15:15:56 +0800106 write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100107 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
Marc Zyngier68908bf2015-01-29 15:47:55 +0000108 __activate_traps_arch()();
Marc Zyngierbe901e92015-10-21 09:57:10 +0100109}
110
Marc Zyngier68908bf2015-01-29 15:47:55 +0000111static void __hyp_text __deactivate_traps_vhe(void)
112{
113 extern char vectors[]; /* kernel exception vectors */
Will Deaconf85279b2016-09-22 11:35:43 +0100114 u64 mdcr_el2 = read_sysreg(mdcr_el2);
Marc Zyngier68908bf2015-01-29 15:47:55 +0000115
Will Deaconf85279b2016-09-22 11:35:43 +0100116 mdcr_el2 &= MDCR_EL2_HPMN_MASK |
117 MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
118 MDCR_EL2_TPMS;
119
120 write_sysreg(mdcr_el2, mdcr_el2);
Marc Zyngier68908bf2015-01-29 15:47:55 +0000121 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
Dave Martin17eed272017-10-31 15:51:16 +0000122 write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
Marc Zyngier68908bf2015-01-29 15:47:55 +0000123 write_sysreg(vectors, vbar_el1);
124}
125
126static void __hyp_text __deactivate_traps_nvhe(void)
127{
Will Deaconf85279b2016-09-22 11:35:43 +0100128 u64 mdcr_el2 = read_sysreg(mdcr_el2);
129
130 mdcr_el2 &= MDCR_EL2_HPMN_MASK;
131 mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
132
133 write_sysreg(mdcr_el2, mdcr_el2);
Marc Zyngier68908bf2015-01-29 15:47:55 +0000134 write_sysreg(HCR_RW, hcr_el2);
135 write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
136}
137
138static hyp_alternate_select(__deactivate_traps_arch,
139 __deactivate_traps_nvhe, __deactivate_traps_vhe,
140 ARM64_HAS_VIRT_HOST_EXTN);
141
Marc Zyngierbe901e92015-10-21 09:57:10 +0100142static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
143{
Marc Zyngier44636f92016-09-06 14:02:00 +0100144 /*
145 * If we pended a virtual abort, preserve it until it gets
146 * cleared. See D1.14.3 (Virtual Interrupts) for details, but
147 * the crucial bit is "On taking a vSError interrupt,
148 * HCR_EL2.VSE is cleared to 0."
149 */
150 if (vcpu->arch.hcr_el2 & HCR_VSE)
151 vcpu->arch.hcr_el2 = read_sysreg(hcr_el2);
152
Marc Zyngier68908bf2015-01-29 15:47:55 +0000153 __deactivate_traps_arch()();
Marc Zyngierbe901e92015-10-21 09:57:10 +0100154 write_sysreg(0, hstr_el2);
Shannon Zhaod692b8a2015-09-08 15:15:56 +0800155 write_sysreg(0, pmuserenr_el0);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100156}
157
158static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
159{
160 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
161 write_sysreg(kvm->arch.vttbr, vttbr_el2);
162}
163
164static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
165{
166 write_sysreg(0, vttbr_el2);
167}
168
Marc Zyngierbe901e92015-10-21 09:57:10 +0100169static void __hyp_text __vgic_save_state(struct kvm_vcpu *vcpu)
170{
Vladimir Murzin5a7a8422016-09-12 15:49:15 +0100171 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
172 __vgic_v3_save_state(vcpu);
173 else
174 __vgic_v2_save_state(vcpu);
175
Marc Zyngierbe901e92015-10-21 09:57:10 +0100176 write_sysreg(read_sysreg(hcr_el2) & ~HCR_INT_OVERRIDE, hcr_el2);
177}
178
179static void __hyp_text __vgic_restore_state(struct kvm_vcpu *vcpu)
180{
181 u64 val;
182
183 val = read_sysreg(hcr_el2);
184 val |= HCR_INT_OVERRIDE;
185 val |= vcpu->arch.irq_lines;
186 write_sysreg(val, hcr_el2);
187
Vladimir Murzin5a7a8422016-09-12 15:49:15 +0100188 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
189 __vgic_v3_restore_state(vcpu);
190 else
191 __vgic_v2_restore_state(vcpu);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100192}
193
Marc Zyngier5f05a72a2015-10-28 15:06:47 +0000194static bool __hyp_text __true_value(void)
195{
196 return true;
197}
198
199static bool __hyp_text __false_value(void)
200{
201 return false;
202}
203
204static hyp_alternate_select(__check_arm_834220,
205 __false_value, __true_value,
206 ARM64_WORKAROUND_834220);
207
208static bool __hyp_text __translate_far_to_hpfar(u64 far, u64 *hpfar)
209{
210 u64 par, tmp;
211
212 /*
213 * Resolve the IPA the hard way using the guest VA.
214 *
215 * Stage-1 translation already validated the memory access
216 * rights. As such, we can use the EL1 translation regime, and
217 * don't have to distinguish between EL0 and EL1 access.
218 *
219 * We do need to save/restore PAR_EL1 though, as we haven't
220 * saved the guest context yet, and we may return early...
221 */
222 par = read_sysreg(par_el1);
223 asm volatile("at s1e1r, %0" : : "r" (far));
224 isb();
225
226 tmp = read_sysreg(par_el1);
227 write_sysreg(par, par_el1);
228
229 if (unlikely(tmp & 1))
230 return false; /* Translation failed, back to guest */
231
232 /* Convert PAR to HPFAR format */
233 *hpfar = ((tmp >> 12) & ((1UL << 36) - 1)) << 4;
234 return true;
235}
236
237static bool __hyp_text __populate_fault_info(struct kvm_vcpu *vcpu)
238{
239 u64 esr = read_sysreg_el2(esr);
Mark Rutland561454e2016-05-31 12:33:02 +0100240 u8 ec = ESR_ELx_EC(esr);
Marc Zyngier5f05a72a2015-10-28 15:06:47 +0000241 u64 hpfar, far;
242
243 vcpu->arch.fault.esr_el2 = esr;
244
245 if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
246 return true;
247
248 far = read_sysreg_el2(far);
249
250 /*
251 * The HPFAR can be invalid if the stage 2 fault did not
252 * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
253 * bit is clear) and one of the two following cases are true:
254 * 1. The fault was due to a permission fault
255 * 2. The processor carries errata 834220
256 *
257 * Therefore, for all non S1PTW faults where we either have a
258 * permission fault or the errata workaround is enabled, we
259 * resolve the IPA using the AT instruction.
260 */
261 if (!(esr & ESR_ELx_S1PTW) &&
262 (__check_arm_834220()() || (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
263 if (!__translate_far_to_hpfar(far, &hpfar))
264 return false;
265 } else {
266 hpfar = read_sysreg(hpfar_el2);
267 }
268
269 vcpu->arch.fault.far_el2 = far;
270 vcpu->arch.fault.hpfar_el2 = hpfar;
271 return true;
272}
273
Alex Bennéee3feebf2017-11-23 12:11:34 +0000274/* Skip an instruction which has been emulated. Returns true if
275 * execution can continue or false if we need to exit hyp mode because
276 * single-step was in effect.
277 */
278static bool __hyp_text __skip_instr(struct kvm_vcpu *vcpu)
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100279{
280 *vcpu_pc(vcpu) = read_sysreg_el2(elr);
281
282 if (vcpu_mode_is_32bit(vcpu)) {
283 vcpu->arch.ctxt.gp_regs.regs.pstate = read_sysreg_el2(spsr);
284 kvm_skip_instr32(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
285 write_sysreg_el2(vcpu->arch.ctxt.gp_regs.regs.pstate, spsr);
286 } else {
287 *vcpu_pc(vcpu) += 4;
288 }
289
290 write_sysreg_el2(*vcpu_pc(vcpu), elr);
Alex Bennéee3feebf2017-11-23 12:11:34 +0000291
292 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
293 vcpu->arch.fault.esr_el2 =
294 (ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT) | 0x22;
295 return false;
296 } else {
297 return true;
298 }
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100299}
300
Christoffer Dallcf0ba182016-09-01 13:16:03 +0200301int __hyp_text __kvm_vcpu_run(struct kvm_vcpu *vcpu)
Marc Zyngierbe901e92015-10-21 09:57:10 +0100302{
303 struct kvm_cpu_context *host_ctxt;
304 struct kvm_cpu_context *guest_ctxt;
Marc Zyngierc13d1682015-10-26 08:34:09 +0000305 bool fp_enabled;
Marc Zyngierbe901e92015-10-21 09:57:10 +0100306 u64 exit_code;
307
308 vcpu = kern_hyp_va(vcpu);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100309
310 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
James Morsec97e1662018-01-08 15:38:05 +0000311 host_ctxt->__hyp_running_vcpu = vcpu;
Marc Zyngierbe901e92015-10-21 09:57:10 +0100312 guest_ctxt = &vcpu->arch.ctxt;
313
Marc Zyngieredef5282015-10-28 12:17:35 +0000314 __sysreg_save_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100315 __debug_cond_save_host_state(vcpu);
316
317 __activate_traps(vcpu);
318 __activate_vm(vcpu);
319
320 __vgic_restore_state(vcpu);
Christoffer Dall688c50a2017-01-04 16:10:28 +0100321 __timer_enable_traps(vcpu);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100322
323 /*
324 * We must restore the 32-bit state before the sysregs, thanks
Marc Zyngier674e7012016-08-16 15:03:01 +0100325 * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
Marc Zyngierbe901e92015-10-21 09:57:10 +0100326 */
327 __sysreg32_restore_state(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000328 __sysreg_restore_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100329 __debug_restore_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
330
331 /* Jump in the fire! */
Marc Zyngier5f05a72a2015-10-28 15:06:47 +0000332again:
Marc Zyngierbe901e92015-10-21 09:57:10 +0100333 exit_code = __guest_enter(vcpu, host_ctxt);
334 /* And we're baaack! */
335
Marc Zyngier395ea792016-09-06 14:02:07 +0100336 /*
337 * We're using the raw exception code in order to only process
338 * the trap if no SError is pending. We will come back to the
339 * same PC once the SError has been injected, and replay the
340 * trapping instruction.
341 */
Marc Zyngier5f05a72a2015-10-28 15:06:47 +0000342 if (exit_code == ARM_EXCEPTION_TRAP && !__populate_fault_info(vcpu))
343 goto again;
344
Marc Zyngier90348682018-01-03 16:38:37 +0000345 if (exit_code == ARM_EXCEPTION_TRAP &&
346 (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_HVC64 ||
347 kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_HVC32) &&
348 vcpu_get_reg(vcpu, 0) == PSCI_0_2_FN_PSCI_VERSION) {
349 u64 val = PSCI_RET_NOT_SUPPORTED;
350 if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
351 val = 2;
352
353 vcpu_set_reg(vcpu, 0, val);
354 goto again;
355 }
356
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100357 if (static_branch_unlikely(&vgic_v2_cpuif_trap) &&
358 exit_code == ARM_EXCEPTION_TRAP) {
359 bool valid;
360
361 valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
362 kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
363 kvm_vcpu_dabt_isvalid(vcpu) &&
364 !kvm_vcpu_dabt_isextabt(vcpu) &&
365 !kvm_vcpu_dabt_iss1tw(vcpu);
366
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100367 if (valid) {
368 int ret = __vgic_v2_perform_cpuif_access(vcpu);
369
370 if (ret == 1) {
Alex Bennéee3feebf2017-11-23 12:11:34 +0000371 if (__skip_instr(vcpu))
372 goto again;
373 else
374 exit_code = ARM_EXCEPTION_TRAP;
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100375 }
376
377 if (ret == -1) {
Alex Bennéee3feebf2017-11-23 12:11:34 +0000378 /* Promote an illegal access to an
379 * SError. If we would be returning
380 * due to single-step clear the SS
381 * bit so handle_exit knows what to
382 * do after dealing with the error.
383 */
384 if (!__skip_instr(vcpu))
385 *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100386 exit_code = ARM_EXCEPTION_EL1_SERROR;
387 }
388
389 /* 0 falls through to be handler out of EL2 */
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100390 }
391 }
392
Marc Zyngier59da1cb2017-06-09 12:49:33 +0100393 if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
394 exit_code == ARM_EXCEPTION_TRAP &&
395 (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
396 kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
397 int ret = __vgic_v3_perform_cpuif_access(vcpu);
398
399 if (ret == 1) {
Alex Bennéee3feebf2017-11-23 12:11:34 +0000400 if (__skip_instr(vcpu))
401 goto again;
402 else
403 exit_code = ARM_EXCEPTION_TRAP;
Marc Zyngier59da1cb2017-06-09 12:49:33 +0100404 }
405
406 /* 0 falls through to be handled out of EL2 */
407 }
408
Shanker Donthineniec82b562018-01-05 14:28:59 -0600409 if (cpus_have_const_cap(ARM64_HARDEN_BP_POST_GUEST_EXIT)) {
410 u32 midr = read_cpuid_id();
411
412 /* Apply BTAC predictors mitigation to all Falkor chips */
413 if ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1)
414 __qcom_hyp_sanitize_btac_predictors();
415 }
416
Marc Zyngierc13d1682015-10-26 08:34:09 +0000417 fp_enabled = __fpsimd_enabled();
418
Marc Zyngieredef5282015-10-28 12:17:35 +0000419 __sysreg_save_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100420 __sysreg32_save_state(vcpu);
Christoffer Dall688c50a2017-01-04 16:10:28 +0100421 __timer_disable_traps(vcpu);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100422 __vgic_save_state(vcpu);
423
424 __deactivate_traps(vcpu);
425 __deactivate_vm(vcpu);
426
Marc Zyngieredef5282015-10-28 12:17:35 +0000427 __sysreg_restore_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100428
Marc Zyngierc13d1682015-10-26 08:34:09 +0000429 if (fp_enabled) {
430 __fpsimd_save_state(&guest_ctxt->gp_regs.fp_regs);
431 __fpsimd_restore_state(&host_ctxt->gp_regs.fp_regs);
432 }
433
Marc Zyngierbe901e92015-10-21 09:57:10 +0100434 __debug_save_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
Will Deaconf85279b2016-09-22 11:35:43 +0100435 /*
436 * This must come after restoring the host sysregs, since a non-VHE
437 * system may enable SPE here and make use of the TTBRs.
438 */
Marc Zyngierbe901e92015-10-21 09:57:10 +0100439 __debug_cond_restore_host_state(vcpu);
440
441 return exit_code;
442}
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000443
444static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
445
James Morsec97e1662018-01-08 15:38:05 +0000446static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par,
447 struct kvm_vcpu *vcpu)
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000448{
Marc Zyngiercf7df132016-06-30 18:40:35 +0100449 unsigned long str_va;
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000450
Marc Zyngiercf7df132016-06-30 18:40:35 +0100451 /*
452 * Force the panic string to be loaded from the literal pool,
453 * making sure it is a kernel address and not a PC-relative
454 * reference.
455 */
456 asm volatile("ldr %0, =__hyp_panic_string" : "=r" (str_va));
457
458 __hyp_do_panic(str_va,
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000459 spsr, elr,
460 read_sysreg(esr_el2), read_sysreg_el2(far),
James Morsec97e1662018-01-08 15:38:05 +0000461 read_sysreg(hpfar_el2), par, vcpu);
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000462}
463
James Morsec97e1662018-01-08 15:38:05 +0000464static void __hyp_text __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par,
465 struct kvm_vcpu *vcpu)
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000466{
467 panic(__hyp_panic_string,
468 spsr, elr,
469 read_sysreg_el2(esr), read_sysreg_el2(far),
James Morsec97e1662018-01-08 15:38:05 +0000470 read_sysreg(hpfar_el2), par, vcpu);
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000471}
472
473static hyp_alternate_select(__hyp_call_panic,
474 __hyp_call_panic_nvhe, __hyp_call_panic_vhe,
475 ARM64_HAS_VIRT_HOST_EXTN);
476
James Morsec97e1662018-01-08 15:38:05 +0000477void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *__host_ctxt)
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000478{
James Morsec97e1662018-01-08 15:38:05 +0000479 struct kvm_vcpu *vcpu = NULL;
480
Marc Zyngier253dcbd2015-11-17 14:07:45 +0000481 u64 spsr = read_sysreg_el2(spsr);
482 u64 elr = read_sysreg_el2(elr);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000483 u64 par = read_sysreg(par_el1);
484
485 if (read_sysreg(vttbr_el2)) {
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000486 struct kvm_cpu_context *host_ctxt;
487
James Morsec97e1662018-01-08 15:38:05 +0000488 host_ctxt = kern_hyp_va(__host_ctxt);
489 vcpu = host_ctxt->__hyp_running_vcpu;
Christoffer Dall688c50a2017-01-04 16:10:28 +0100490 __timer_disable_traps(vcpu);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000491 __deactivate_traps(vcpu);
492 __deactivate_vm(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000493 __sysreg_restore_host_state(host_ctxt);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000494 }
495
496 /* Call panic for real */
James Morsec97e1662018-01-08 15:38:05 +0000497 __hyp_call_panic()(spsr, elr, par, vcpu);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000498
499 unreachable();
500}