Skip to content

Commit a2e102e

Browse files
AndrewScullMarc Zyngier
authored andcommitted
KVM: arm64: nVHE: Handle hyp panics
Restore the host context when panicking from hyp to give the best chance of the panic being clean. The host requires that registers be preserved such as x18 for the shadow callstack. If the panic is caused by an exception from EL1, the host context is still valid so the panic can return straight back to the host. If the panic comes from EL2 then it's most likely that the hyp context is active and the host context needs to be restored. There are windows before and after the host context is saved and restored that restoration is attempted incorrectly and the panic won't be clean. Signed-off-by: Andrew Scull <ascull@google.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20200915104643.2543892-14-ascull@google.com
1 parent 4e3393a commit a2e102e

3 files changed

Lines changed: 64 additions & 34 deletions

File tree

‎arch/arm64/include/asm/kvm_hyp.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ u64 __guest_enter(struct kvm_vcpu *vcpu);
9494

9595
void __noreturn hyp_panic(void);
9696
#ifdef __KVM_NVHE_HYPERVISOR__
97-
void __noreturn __hyp_do_panic(unsigned long, ...);
97+
void __noreturn __hyp_do_panic(bool restore_host, u64 spsr, u64 elr, u64 par);
9898
#endif
9999

100100
#endif /* __ARM64_KVM_HYP_H__ */

‎arch/arm64/kvm/hyp/nvhe/host.S‎

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ SYM_FUNC_START(__host_exit)
4747
ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)]
4848
ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)]
4949
ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)]
50+
51+
/* x0-7 are use for panic arguments */
52+
__host_enter_for_panic:
5053
ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)]
5154
ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)]
5255
ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)]
@@ -57,18 +60,38 @@ SYM_FUNC_START(__host_exit)
5760
restore_callee_saved_regs x29
5861

5962
/* Do not touch any register after this! */
63+
__host_enter_without_restoring:
6064
eret
6165
sb
6266
SYM_FUNC_END(__host_exit)
6367

68+
/*
69+
* void __noreturn __hyp_do_panic(bool restore_host, u64 spsr, u64 elr, u64 par);
70+
*/
6471
SYM_FUNC_START(__hyp_do_panic)
72+
/* Load the format arguments into x1-7 */
73+
mov x6, x3
74+
get_vcpu_ptr x7, x3
75+
76+
mrs x3, esr_el2
77+
mrs x4, far_el2
78+
mrs x5, hpfar_el2
79+
80+
/* Prepare and exit to the host's panic funciton. */
6581
mov lr, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
6682
PSR_MODE_EL1h)
6783
msr spsr_el2, lr
6884
ldr lr, =panic
6985
msr elr_el2, lr
70-
eret
71-
sb
86+
87+
/*
88+
* Set the panic format string and enter the host, conditionally
89+
* restoring the host context.
90+
*/
91+
cmp x0, xzr
92+
ldr x0, =__hyp_panic_string
93+
b.eq __host_enter_without_restoring
94+
b __host_enter_for_panic
7295
SYM_FUNC_END(__hyp_do_panic)
7396

7497
.macro host_el1_sync_vect
@@ -79,7 +102,7 @@ SYM_FUNC_END(__hyp_do_panic)
79102
lsr x0, x0, #ESR_ELx_EC_SHIFT
80103
cmp x0, #ESR_ELx_EC_HVC64
81104
ldp x0, x1, [sp], #16
82-
b.ne hyp_panic
105+
b.ne __host_exit
83106

84107
/* Check for a stub HVC call */
85108
cmp x0, #HVC_STUB_HCALL_NR
@@ -105,16 +128,31 @@ SYM_FUNC_END(__hyp_do_panic)
105128
.endif
106129
.endm
107130

108-
.macro invalid_host_vect
131+
.macro invalid_host_el2_vect
109132
.align 7
110133
/* If a guest is loaded, panic out of it. */
111134
stp x0, x1, [sp, #-16]!
112135
get_loaded_vcpu x0, x1
113136
cbnz x0, __guest_exit_panic
114137
add sp, sp, #16
138+
139+
/*
140+
* The panic may not be clean if the exception is taken before the host
141+
* context has been saved by __host_exit or after the hyp context has
142+
* been partially clobbered by __host_enter.
143+
*/
115144
b hyp_panic
116145
.endm
117146

147+
.macro invalid_host_el1_vect
148+
.align 7
149+
mov x0, xzr /* restore_host = false */
150+
mrs x1, spsr_el2
151+
mrs x2, elr_el2
152+
mrs x3, par_el1
153+
b __hyp_do_panic
154+
.endm
155+
118156
/*
119157
* The host vector does not use an ESB instruction in order to avoid consuming
120158
* SErrors that should only be consumed by the host. Guest entry is deferred by
@@ -127,23 +165,23 @@ SYM_FUNC_END(__hyp_do_panic)
127165
*/
128166
.align 11
129167
SYM_CODE_START(__kvm_hyp_host_vector)
130-
invalid_host_vect // Synchronous EL2t
131-
invalid_host_vect // IRQ EL2t
132-
invalid_host_vect // FIQ EL2t
133-
invalid_host_vect // Error EL2t
168+
invalid_host_el2_vect // Synchronous EL2t
169+
invalid_host_el2_vect // IRQ EL2t
170+
invalid_host_el2_vect // FIQ EL2t
171+
invalid_host_el2_vect // Error EL2t
134172

135-
invalid_host_vect // Synchronous EL2h
136-
invalid_host_vect // IRQ EL2h
137-
invalid_host_vect // FIQ EL2h
138-
invalid_host_vect // Error EL2h
173+
invalid_host_el2_vect // Synchronous EL2h
174+
invalid_host_el2_vect // IRQ EL2h
175+
invalid_host_el2_vect // FIQ EL2h
176+
invalid_host_el2_vect // Error EL2h
139177

140178
host_el1_sync_vect // Synchronous 64-bit EL1
141-
invalid_host_vect // IRQ 64-bit EL1
142-
invalid_host_vect // FIQ 64-bit EL1
143-
invalid_host_vect // Error 64-bit EL1
144-
145-
invalid_host_vect // Synchronous 32-bit EL1
146-
invalid_host_vect // IRQ 32-bit EL1
147-
invalid_host_vect // FIQ 32-bit EL1
148-
invalid_host_vect // Error 32-bit EL1
179+
invalid_host_el1_vect // IRQ 64-bit EL1
180+
invalid_host_el1_vect // FIQ 64-bit EL1
181+
invalid_host_el1_vect // Error 64-bit EL1
182+
183+
invalid_host_el1_vect // Synchronous 32-bit EL1
184+
invalid_host_el1_vect // IRQ 32-bit EL1
185+
invalid_host_el1_vect // FIQ 32-bit EL1
186+
invalid_host_el1_vect // Error 32-bit EL1
149187
SYM_CODE_END(__kvm_hyp_host_vector)

‎arch/arm64/kvm/hyp/nvhe/switch.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
242242
if (system_uses_irq_prio_masking())
243243
gic_write_pmr(GIC_PRIO_IRQOFF);
244244

245+
host_ctxt->__hyp_running_vcpu = NULL;
246+
245247
return exit_code;
246248
}
247249

@@ -250,31 +252,21 @@ void __noreturn hyp_panic(void)
250252
u64 spsr = read_sysreg_el2(SYS_SPSR);
251253
u64 elr = read_sysreg_el2(SYS_ELR);
252254
u64 par = read_sysreg(par_el1);
255+
bool restore_host = true;
253256
struct kvm_cpu_context *host_ctxt;
254257
struct kvm_vcpu *vcpu;
255-
unsigned long str_va;
256258

257259
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
258260
vcpu = host_ctxt->__hyp_running_vcpu;
259261

260-
if (read_sysreg(vttbr_el2)) {
262+
if (vcpu) {
261263
__timer_disable_traps(vcpu);
262264
__deactivate_traps(vcpu);
263265
__load_host_stage2();
264266
__sysreg_restore_state_nvhe(host_ctxt);
265267
}
266268

267-
/*
268-
* Force the panic string to be loaded from the literal pool,
269-
* making sure it is a kernel address and not a PC-relative
270-
* reference.
271-
*/
272-
asm volatile("ldr %0, =%1" : "=r" (str_va) : "S" (__hyp_panic_string));
273-
274-
__hyp_do_panic(str_va,
275-
spsr, elr,
276-
read_sysreg(esr_el2), read_sysreg_el2(SYS_FAR),
277-
read_sysreg(hpfar_el2), par, vcpu);
269+
__hyp_do_panic(restore_host, spsr, elr, par);
278270
unreachable();
279271
}
280272

0 commit comments

Comments
 (0)