arm64: vgic-v2: Fix proxying of cpuif access
PATCH 1/1 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. I'd rather kill BE support, but hey, just in case...
--- arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c | 33 ++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c +27 -6
@@ -23,6 +23,30 @@#include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> +static bool __hyp_text __is_be(struct kvm_vcpu *vcpu) +{ +if (vcpu_mode_is_32bit (vcpu)) +return !!(read_sysreg_el2 (spsr) &COMPAT_PSR_E_BIT ); + +return !!(read_sysreg (SCTLR_EL1 ) & SCTLR_ELx_EE); +} + +static u32 __hyp_text __host_to_guest_u32(struct kvm_vcpu *vcpu, u32 data) +{ +if (__is_be (vcpu)) +return cpu_to_be32 (data); + +return cpu_to_le32 (data); +} + +static u32 __hyp_text __guest_to_host_u32(struct kvm_vcpu *vcpu, u32 data) +{ +if (__is_be (vcpu)) +return be32_to_cpu (data); + +return le32_to_cpu (data); +} +/* * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the * guest.
@@ -64,14 +88,11 @@ int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) addr += fault_ipa - vgic->vgic_cpu_base;if (kvm_vcpu_dabt_iswrite (vcpu)) { -u32 data =vcpu_data_guest_to_host (vcpu, -vcpu_get_reg (vcpu, rd), -sizeof (u32)); +u32 data =__guest_to_host_u32 (vcpu,vcpu_get_reg (vcpu, rd));writel_relaxed (data, addr); }else { -u32 data =readl_relaxed (addr); -vcpu_set_reg (vcpu, rd,vcpu_data_host_to_guest (vcpu, data, -sizeof (u32))); +u32 data =__host_to_guest_u32 (vcpu,readl_relaxed (addr)); +vcpu_set_reg (vcpu, rd, data); }return 1 ;
Signature · 1 line
2.14.2
Quoted 4 lines · Marc Zyngier
Proxying the cpuif accesses at EL2 makes use of
vcpu_data_guest_to_hostand co, which check the endianness, which call intovcpu_read_sys_reg... which isn't mapped at EL2 (it was inlined before, and got moved OoL with the VHE optimizations).
I thought we relied on static inline functions to always be inlined, but apparently not? Does this mean we have potential other bugs looming depending on the mood of the compiler, or was there something special that went wrong here?
Quoted 9 lines
The result is of course a nice panic. Let's add some specialized cruft to keep the broken platforms that require this hack alive. I'd rather kill BE support, but hey, just in case...
Fixes d47533dab9f5 ("KVM: arm64: Introduce framework for accessing deferred sysregs")Reported-by Suzuki K Poulose <[email protected]>Tested-by Suzuki K Poulose <[email protected]>Signed-off-by Marc Zyngier <[email protected]>
Otherwise:
Quoted 60 lines
--- arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c | 33 ++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-)arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c +27
-6@@ -23,6 +23,30 @@#include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> +static bool __hyp_text __is_be(struct kvm_vcpu *vcpu) +{ +if (vcpu_mode_is_32bit (vcpu)) +return !!(read_sysreg_el2 (spsr) &COMPAT_PSR_E_BIT ); + +return !!(read_sysreg (SCTLR_EL1 ) & SCTLR_ELx_EE); +} + +static u32 __hyp_text __host_to_guest_u32(struct kvm_vcpu *vcpu, u32 data) +{ +if (__is_be (vcpu)) +return cpu_to_be32 (data); + +return cpu_to_le32 (data); +} + +static u32 __hyp_text __guest_to_host_u32(struct kvm_vcpu *vcpu, u32 data) +{ +if (__is_be (vcpu)) +return be32_to_cpu (data); + +return le32_to_cpu (data); +} +/* * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the * guest. @@ -64,14 +88,11 @@ int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) addr += fault_ipa - vgic->vgic_cpu_base;if (kvm_vcpu_dabt_iswrite (vcpu)) { -u32 data =vcpu_data_guest_to_host (vcpu, -vcpu_get_reg (vcpu, rd), -sizeof (u32)); +u32 data =__guest_to_host_u32 (vcpu,vcpu_get_reg (vcpu, rd));writel_relaxed (data, addr); }else { -u32 data =readl_relaxed (addr); -vcpu_set_reg (vcpu, rd,vcpu_data_host_to_guest (vcpu, data, -sizeof (u32))); +u32 data =__host_to_guest_u32 (vcpu,readl_relaxed (addr)); +vcpu_set_reg (vcpu, rd, data); }return 1 ;Signature · 1 line
2.14.2
Quoted 10 lines · On Sun, 29 Apr 2018 14:34:32 +0200 Christoffer Dall <[email protected]>…
Quoted 4 lines · Marc Zyngier
Proxying the cpuif accesses at EL2 makes use of
vcpu_data_guest_to_hostand co, which check the endianness, which call intovcpu_read_sys_reg... which isn't mapped at EL2 (it was inlined before, and got moved OoL with the VHE optimizations).I thought we relied on static inline functions to always be inlined, but apparently not? Does this mean we have potential other bugs looming depending on the mood of the compiler, or was there something special that went wrong here?
We do rely on that behaviour. And that was the case until you moved vcpu_read_sys_reg() to be entirely out of line (see d47533dab9f5).
At that point, kvm_vcpu_is_be() becomes a death trap.
We missed it for two reasons:
- It was only indirectly called, making it quite hard to notice the potential breakage
- Nobody gives a damn about 64k pages, specially on something like Juno
What we'd need is a way to find cross-section calls (text -> hyp-text should allowed, but not the reverse). We already have similar things in the kernel, it is probably only a matter of reusing the infrastructure for our own purpose.
Quoted 4 lines
Quoted 4 lines
The result is of course a nice panic. Let's add some specialized cruft to keep the broken platforms that require this hack alive. I'd rather kill BE support, but hey, just in case...
Quoted 8 lines
Quoted 4 lines
Fixes d47533dab9f5 ("KVM: arm64: Introduce framework for accessing deferred sysregs")Reported-by Suzuki K Poulose <[email protected]>Tested-by Suzuki K Poulose <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Otherwise:
Reviewed-by Christoffer Dall <[email protected]>
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 9 lines · Marc Zyngier
Quoted 6 lines · On Sun, 29 Apr 2018 14:34:32 +0200 Christoffer Dall <[email protected]>…
Quoted 4 lines · Marc Zyngier
Proxying the cpuif accesses at EL2 makes use of
vcpu_data_guest_to_hostand co, which check the endianness, which call intovcpu_read_sys_reg... which isn't mapped at EL2 (it was inlined before, and got moved OoL with the VHE optimizations).
Quoted 7 lines
Quoted 4 lines
I thought we relied on static inline functions to always be inlined, but apparently not? Does this mean we have potential other bugs looming depending on the mood of the compiler, or was there something special that went wrong here?
We do rely on that behaviour. And that was the case until you moved
vcpu_read_sys_reg()to be entirely out of line (seed47533dab9f5).
ah, now I see, kvm_vcpu_is_be() calls vcpu_read_sys_reg(), I somehow missed this even though you spelled it out in the commit message. Sorry.
Quoted 8 lines
At that point,
kvm_vcpu_is_be()becomes a death trap.We missed it for two reasons:
- It was only indirectly called, making it quite hard to notice the potential breakage
- Nobody gives a damn about 64k pages, specially on something like Juno
Quoted 5 lines
What we'd need is a way to find cross-section calls (text -> hyp-text should allowed, but not the reverse). We already have similar things in the kernel, it is probably only a matter of reusing the infrastructure for our own purpose.
That would be good indeed...
Thanks for the further explanation.
-Christoffer
Hi Marc,
Quoted 8 lines · Marc Zyngier
Proxying the cpuif accesses at EL2 makes use of
vcpu_data_guest_to_hostand co, which check the endianness, which call intovcpu_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. I'd rather kill BE support, but hey, just in case...
I have this a spin on Juno with a big-endian host and 64K pages:
Trying to boot a BE guest hangs.
Trying to boot a LE guest hangs.
Quoted 6 lines
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c +0
-2@@ -64,14 +88,11 @@ int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) addr += fault_ipa - vgic->vgic_cpu_base;if (kvm_vcpu_dabt_iswrite (vcpu)) { - u32 data = vcpu_data_guest_to_host(vcpu, - vcpu_get_reg(vcpu, rd),
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c int __hyp_text __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu) · line 96
Quoted 10 lines
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c +3
-4- sizeof(u32)); +u32 data =__guest_to_host_u32 (vcpu,vcpu_get_reg (vcpu, rd));writel_relaxed (data, addr); }else { -u32 data =readl_relaxed (addr); -vcpu_set_reg (vcpu, rd,vcpu_data_host_to_guest (vcpu, data, -sizeof (u32))); +u32 data =__host_to_guest_u32 (vcpu,readl_relaxed (addr)); +vcpu_set_reg (vcpu, rd, data); }
This happens because readl()/writel() are doing their own swabbing on big-endian, even if the guest had already done this.
As I've trampled all over this patch, I'll post a v2...
Thanks,
James
Quoted 24 lines · Marc Zyngier
Quoted 10 lines · On Sun, 29 Apr 2018 14:34:32 +0200 Christoffer Dall <[email protected]>…
Quoted 4 lines · Marc Zyngier
Proxying the cpuif accesses at EL2 makes use of
vcpu_data_guest_to_hostand co, which check the endianness, which call intovcpu_read_sys_reg... which isn't mapped at EL2 (it was inlined before, and got moved OoL with the VHE optimizations).I thought we relied on static inline functions to always be inlined, but apparently not? Does this mean we have potential other bugs looming depending on the mood of the compiler, or was there something special that went wrong here?
We do rely on that behaviour. And that was the case until you moved
vcpu_read_sys_reg()to be entirely out of line (seed47533dab9f5).At that point,
kvm_vcpu_is_be()becomes a death trap.We missed it for two reasons:
- It was only indirectly called, making it quite hard to notice the potential breakage
- Nobody gives a damn about 64k pages, specially on something like Juno
Quoted 5 lines
What we'd need is a way to find cross-section calls (text -> hyp-text should allowed, but not the reverse). We already have similar things in the kernel, it is probably only a matter of reusing the infrastructure for our own purpose.
Hi all,
While debugging a series backported to the RHEL kernel, I was suspicious of the problem being an issue like this (a hyp-text to non-hyp-mapped reference), so I went ahead an implemented the cross-section reference checking as Marc suggested. I've attached the patch. I don't really like it though because of all the special casing necessary to kill false alarms - making it too hacky. Also, the need to inline some functions just to match their symbol names is pretty lame. Anyway, maybe it can be helpful, at least as inspiration.
Running it on latest master doesn't produce anything. Running it without b220244d4179 ("arm64: vgic-v2: Fix proxying of cpuif access") gives
.hyp.text:__vgic_v2_perform_cpuif_access references .text:vcpu_read_sys_reg
so I guess it works.
Unfortunately it didn't help me with my downstream debug...
drew
arch/arm64/kvm/hyp/hyp-entry.S +1 -0
@@ -200,6 +200,7 @@ ENDPROC(\label) invalid_vector el2h_fiq_invalid invalid_vector el1_fiq_invalid +hyp_entry_literal_pool: .ltorg .align 11
arch/arm64/kvm/hyp/switch.c +4 -4
@@ -86,7 +86,7 @@ static void __hyp_text __deactivate_traps_common(void)write_sysreg (0 , pmuserenr_el0); } -static void activate_traps_vhe (struct kvm_vcpu *vcpu) +static noinline void activate_traps_vhe(struct kvm_vcpu *vcpu) {u64 val;
@@ -125,7 +125,7 @@ static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)__activate_traps_nvhe (vcpu); } -static void deactivate_traps_vhe (void ) +static noinline void deactivate_traps_vhe(void) {extern char vectors[];/* kernel exception vectors */ write_sysreg (HCR_HOST_VHE_FLAGS , hcr_el2);
@@ -529,8 +529,8 @@ static void __hyp_text __hyp_call_panic_nvhe(u64 spsr, u64 elr, u64 par, read_sysreg(hpfar_el2), par, vcpu); } -static void __hyp_call_panic_vhe (u64 spsr,u64 elr,u64 par, -struct kvm_cpu_context *host_ctxt) +static noinline void __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par, +struct kvm_cpu_context *host_ctxt) {struct kvm_vcpu *vcpu; vcpu = host_ctxt->__hyp_running_vcpu;
scripts/mod/modpost.c +59 -1
90 lines not on this page · raw message
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm