Skip to content

Commit 69c9176

Browse files
soccertackoupton
authored andcommitted
KVM: arm64: nv: Respect virtual HCR_EL2.TWx setting
Forward exceptions due to WFI or WFE instructions to the virtual EL2 if they are not coming from the virtual EL2 and virtual HCR_EL2.TWx is set. Signed-off-by: Jintack Lim <jintack.lim@linaro.org> Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20250225172930.1850838-12-maz@kernel.org Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 201c8d4 commit 69c9176

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

arch/arm64/include/asm/kvm_emulate.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
275275
return vcpu->arch.fault.esr_el2;
276276
}
277277

278+
static inline bool guest_hyp_wfx_traps_enabled(const struct kvm_vcpu *vcpu)
279+
{
280+
u64 esr = kvm_vcpu_get_esr(vcpu);
281+
bool is_wfe = !!(esr & ESR_ELx_WFx_ISS_WFE);
282+
u64 hcr_el2 = __vcpu_sys_reg(vcpu, HCR_EL2);
283+
284+
if (!vcpu_has_nv(vcpu) || vcpu_is_el2(vcpu))
285+
return false;
286+
287+
return ((is_wfe && (hcr_el2 & HCR_TWE)) ||
288+
(!is_wfe && (hcr_el2 & HCR_TWI)));
289+
}
290+
278291
static __always_inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
279292
{
280293
u64 esr = kvm_vcpu_get_esr(vcpu);

arch/arm64/kvm/handle_exit.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ static int kvm_handle_fpasimd(struct kvm_vcpu *vcpu)
129129
static int kvm_handle_wfx(struct kvm_vcpu *vcpu)
130130
{
131131
u64 esr = kvm_vcpu_get_esr(vcpu);
132+
bool is_wfe = !!(esr & ESR_ELx_WFx_ISS_WFE);
132133

133-
if (esr & ESR_ELx_WFx_ISS_WFE) {
134+
if (guest_hyp_wfx_traps_enabled(vcpu))
135+
return kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
136+
137+
if (is_wfe) {
134138
trace_kvm_wfx_arm64(*vcpu_pc(vcpu), true);
135139
vcpu->stat.wfe_exit_stat++;
136140
} else {

0 commit comments

Comments
 (0)