Skip to contentSkip to conversation

KVM: arm64: Preserve GPRs for AArch32 CP64 reads generating an UNDEF

PATCH Applied Karl Mehltretter 3 participants 4 messages Aug 2 – Aug 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: arm64: Preserve GPRs for AArch32 CP64 reads generating an UNDEF

kvm_handle_cp_64() only seeds params.regval for writes. If a CP64 read is decoded but UNDEFs, emulate_cp() still returns handled and the caller writes params.regval back to Rt/Rt2.

This can happen for PMU counter read accesses generating an UNDEF. KVM injects the exception into the guest, so the MRRC GPRs must remain unchanged.

Instead, the uninitialised regval is copied into the guest GPRs. With stack auto-initialisation this is a deterministic zero or pattern value. With CONFIG_INIT_STACK_NONE it may be stale host stack data.

Match kvm_handle_cp_32() and kvm_handle_sys_reg() by seeding regval from the GPRs before emulation.

Fixes 62a89c44954f0 ("arm64: KVM: 32bit handling of coprocessor traps")
Assisted-by Claude:claude-opus-5

---
Runtime tested on a Raspberry Pi 400 with a minimal KVM harness running an AArch32 guest. On the unpatched 6.1.21-v8+ vendor kernel, the PMCCNTR MRRC test took UNDEF with r0=0x00000001/r1=0x00000000 instead of the guest's sentinel values. With this patch on v7.2-rc3-278-g38436106b2f5, the same test preserved r0=0x12345678/r1=0x9abcdef0.

Changes in v2:
- Rework the commit message to use correct terminology, omit the unreachable GIC example, fix the Fixes tag and drop the added comment in the source code (Marc Zyngier).

Link to v1: https://lore.kernel.org/r/[email protected]

arch/arm64/kvm/sys_regs.c +2 -4
@@ -4860,11 +4860,9 @@
 	/*
 	 * Make a 64-bit value out of Rt and Rt2. As we use the same trap
 	 * backends between AArch32 and AArch64, we get away with it.
 	 */
-	if (params.is_write) {
-		params.regval = vcpu_get_reg(vcpu, Rt) & 0xffffffff;
-		params.regval |= vcpu_get_reg(vcpu, Rt2) << 32;
-	}
+	params.regval = vcpu_get_reg(vcpu, Rt) & 0xffffffff;
+	params.regval |= vcpu_get_reg(vcpu, Rt2) << 32;
 
 	/*
 	 * If the table contains a handler, handle the
Signature · 1 line
2.51.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 13 lines · Karl Mehltretter

kvm_handle_cp_64() only seeds params.regval for writes. If a CP64 read is decoded but UNDEFs, emulate_cp() still returns handled and the caller writes params.regval back to Rt/Rt2.

This can happen for PMU counter read accesses generating an UNDEF. KVM injects the exception into the guest, so the MRRC GPRs must remain unchanged.

Instead, the uninitialised regval is copied into the guest GPRs. With stack auto-initialisation this is a deterministic zero or pattern value. With CONFIG_INIT_STACK_NONE it may be stale host stack data.

Quoted 6 lines

Match kvm_handle_cp_32() and kvm_handle_sys_reg() by seeding regval from the GPRs before emulation.

Fixes 62a89c44954f0 ("arm64: KVM: 32bit handling of coprocessor traps")
Assisted-by Claude:claude-opus-5
	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Karl Mehltretter
Quoted 13 lines · Karl Mehltretter

kvm_handle_cp_64() only seeds params.regval for writes. If a CP64 read is decoded but UNDEFs, emulate_cp() still returns handled and the caller writes params.regval back to Rt/Rt2.

This can happen for PMU counter read accesses generating an UNDEF. KVM injects the exception into the guest, so the MRRC GPRs must remain unchanged.

Instead, the uninitialised regval is copied into the guest GPRs. With stack auto-initialisation this is a deterministic zero or pattern value. With CONFIG_INIT_STACK_NONE it may be stale host stack data.

Quoted 6 lines

Match kvm_handle_cp_32() and kvm_handle_sys_reg() by seeding regval from the GPRs before emulation.

Fixes 62a89c44954f0 ("arm64: KVM: 32bit handling of coprocessor traps")
Assisted-by Claude:claude-opus-5
	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer Applied
#3 lore raw
In reply to Karl Mehltretter
Quoted 9 lines · Karl Mehltretter

kvm_handle_cp_64() only seeds params.regval for writes. If a CP64 read is decoded but UNDEFs, emulate_cp() still returns handled and the caller writes params.regval back to Rt/Rt2.

This can happen for PMU counter read accesses generating an UNDEF. KVM injects the exception into the guest, so the MRRC GPRs must remain unchanged.

[...]

Applied to next, thanks!

[1/1] KVM: arm64: Preserve GPRs for AArch32 CP64 reads generating an UNDEF

      https://git.kernel.org/kvmarm/kvmarm/c/47746d4b7436

--
Best,
Oliver