Skip to content

Commit b220244

Browse files
James MorseMarc Zyngier
authored andcommitted
arm64: vgic-v2: Fix proxying of cpuif access
Proxying the cpuif accesses at EL2 makes use of vcpu_data_guest_to_host and co, which check the endianness, which call into vcpu_read_sys_reg... which isn't mapped at EL2 (it was inlined before, and got moved OoL with the VHE optimizations). The result is of course a nice panic. Let's add some specialized cruft to keep the broken platforms that require this hack alive. But, this code used vcpu_data_guest_to_host(), which expected us to write the value to host memory, instead we have trapped the guest's read or write to an mmio-device, and are about to replay it using the host's readl()/writel() which also perform swabbing based on the host endianness. This goes wrong when both host and guest are big-endian, as readl()/writel() will undo the guest's swabbing, causing the big-endian value to be written to device-memory. What needs doing? A big-endian guest will have pre-swabbed data before storing, undo this. If its necessary for the host, writel() will re-swab it. For a read a big-endian guest expects to swab the data after the load. The hosts's readl() will correct for host endianness, giving us the device-memory's value in the register. For a big-endian guest, swab it as if we'd only done the load. For a little-endian guest, nothing needs doing as readl()/writel() leave the correct device-memory value in registers. Tested on Juno with that rarest of things: a big-endian 64K host. Based on a patch from Marc Zyngier. Reported-by: Suzuki K Poulose <[email protected]> Fixes: bf8feb3 ("arm64: KVM: vgic-v2: Add GICV access from HYP") Signed-off-by: James Morse <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent c3616a0 commit b220244

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@
1818
#include <linux/compiler.h>
1919
#include <linux/irqchip/arm-gic.h>
2020
#include <linux/kvm_host.h>
21+
#include <linux/swab.h>
2122

2223
#include <asm/kvm_emulate.h>
2324
#include <asm/kvm_hyp.h>
2425
#include <asm/kvm_mmu.h>
2526

27+
static bool __hyp_text __is_be(struct kvm_vcpu *vcpu)
28+
{
29+
if (vcpu_mode_is_32bit(vcpu))
30+
return !!(read_sysreg_el2(spsr) & COMPAT_PSR_E_BIT);
31+
32+
return !!(read_sysreg(SCTLR_EL1) & SCTLR_ELx_EE);
33+
}
34+
2635
/*
2736
* __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the
2837
* guest.
@@ -64,14 +73,19 @@ int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu)
6473
addr += fault_ipa - vgic->vgic_cpu_base;
6574

6675
if (kvm_vcpu_dabt_iswrite(vcpu)) {
67-
u32 data = vcpu_data_guest_to_host(vcpu,
68-
vcpu_get_reg(vcpu, rd),
69-
sizeof(u32));
76+
u32 data = vcpu_get_reg(vcpu, rd);
77+
if (__is_be(vcpu)) {
78+
/* guest pre-swabbed data, undo this for writel() */
79+
data = swab32(data);
80+
}
7081
writel_relaxed(data, addr);
7182
} else {
7283
u32 data = readl_relaxed(addr);
73-
vcpu_set_reg(vcpu, rd, vcpu_data_host_to_guest(vcpu, data,
74-
sizeof(u32)));
84+
if (__is_be(vcpu)) {
85+
/* guest expects swabbed data */
86+
data = swab32(data);
87+
}
88+
vcpu_set_reg(vcpu, rd, data);
7589
}
7690

7791
return 1;

0 commit comments

Comments
 (0)