Skip to contentSkip to conversation

KVM: arm64: Fix and test MMIO sign-extending loads

PATCH Superseded Fuad Tabba 3 participants 8 messages Jun 22 – Jun 25

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: arm64: Fix and test MMIO sign-extending loads

Hi folks,

A sign-extending load (LDRSB/LDRSH/LDRSW) from emulated MMIO returns a zero-extended value rather than the sign-extended one the architecture requires; vcpu_data_host_to_guest() strips the sign bits when it masks the data to the access width.

If my git archeology is right, the masking dates to 2014 (b30070862edbd, big-endian support) and has been wrong ever since, but sign-extending loads from device memory are rare enough that nobody hit it. Patch 1 fixes it; patch 2 adds a selftest so it doesn't regress.

Cheers,
/fuad

Fuad Tabba (2):
KVM: arm64: Fix sign-extension of MMIO loads
KVM: arm64: selftests: Add MMIO sign-extending load test

 arch/arm64/kvm/mmio.c                         |   7 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/mmio_sign_ext.c       | 133 ++++++++++++++++++
 3 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/mmio_sign_ext.c

base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6

Signature · 1 line
2.39.5
author reviewer
#1 lore raw

PATCH 1/2 KVM: arm64: Fix sign-extension of MMIO loads

A sign-extending load from MMIO (LDRSB, LDRSH, LDRSW) delivers a zero-extended value: in kvm_handle_mmio_return(), vcpu_data_host_to_guest() masks the data to the access width after sign-extension, stripping the sign bits.

Move vcpu_data_host_to_guest() ahead of sign-extension so the width mask runs first. trace_kvm_mmio() moves with it and keeps reporting the raw access-width data.

Fixes b30070862edbd ("ARM64: KVM: MMIO support BE host running LE code")
---
 arch/arm64/kvm/mmio.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmio.c +4 -3
index e2285ed8c91de..d1c3a352d5a22 100644
@@ -126,6 +126,10 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		len = kvm_vcpu_dabt_get_as(vcpu);
 		data = kvm_mmio_read_buf(run->mmio.data, len);
 
+		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
+			       &data);
+		data = vcpu_data_host_to_guest(vcpu, data, len);
+
 		if (kvm_vcpu_dabt_issext(vcpu) &&
 		    len < sizeof(unsigned long)) {
 			mask = 1U << ((len * 8) - 1);
@@ -135,9 +139,6 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		if (!kvm_vcpu_dabt_issf(vcpu))
 			data = data & 0xffffffff;
 
-		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
-			       &data);
-		data = vcpu_data_host_to_guest(vcpu, data, len);
 		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
 	}
 
Signature · 1 line
2.39.5
maintainer
#2 lore raw

Re: [PATCH 1/2] KVM: arm64: Fix sign-extension of MMIO loads

Quoted 21 lines · Fuad Tabba

A sign-extending load from MMIO (LDRSB, LDRSH, LDRSW) delivers a zero-extended value: in kvm_handle_mmio_return(), vcpu_data_host_to_guest() masks the data to the access width after sign-extension, stripping the sign bits.

Move vcpu_data_host_to_guest() ahead of sign-extension so the width mask runs first. trace_kvm_mmio() moves with it and keeps reporting the raw access-width data.

Fixes b30070862edbd ("ARM64: KVM: MMIO support BE host running LE code")
---
 arch/arm64/kvm/mmio.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmio.c +0 -0
index e2285ed8c91de..d1c3a352d5a22 100644

arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 131

Quoted 6 lines
arch/arm64/kvm/mmio.c +3 -0
@@ -126,6 +126,10 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		len = kvm_vcpu_dabt_get_as(vcpu);
 		data = kvm_mmio_read_buf(run->mmio.data, len);
 
+		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
+			       &data);
+		data = vcpu_data_host_to_guest(vcpu, data, len);

This helper is in charge of the endianness of the read from the guest's PoV. How can the sign-extension (which happens from the host's perspective) correctly work if it takes place after the byte swapping?

To me, the real issue appears to be in the that helper, which swaps the data based on the size of the access instead of the width of the register.

Or am I once more completely confused with the endianness crap?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#3 lore raw

Re: [PATCH 1/2] KVM: arm64: Fix sign-extension of MMIO loads

Quoted 35 lines · Marc Zyngier
Quoted 21 lines · Fuad Tabba

A sign-extending load from MMIO (LDRSB, LDRSH, LDRSW) delivers a zero-extended value: in kvm_handle_mmio_return(), vcpu_data_host_to_guest() masks the data to the access width after sign-extension, stripping the sign bits.

Move vcpu_data_host_to_guest() ahead of sign-extension so the width mask runs first. trace_kvm_mmio() moves with it and keeps reporting the raw access-width data.

Fixes b30070862edbd ("ARM64: KVM: MMIO support BE host running LE code")
---
 arch/arm64/kvm/mmio.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmio.c +0 -0
index e2285ed8c91de..d1c3a352d5a22 100644

arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 131

Quoted 6 lines
arch/arm64/kvm/mmio.c +3 -0
@@ -126,6 +126,10 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
              len = kvm_vcpu_dabt_get_as(vcpu);
              data = kvm_mmio_read_buf(run->mmio.data, len);
 
+             trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
+                            &data);
+             data = vcpu_data_host_to_guest(vcpu, data, len);

This helper is in charge of the endianness of the read from the guest's PoV. How can the sign-extension (which happens from the host's perspective) correctly work if it takes place after the byte swapping?

Quoted 5 lines

To me, the real issue appears to be in the that helper, which swaps the data based on the size of the access instead of the width of the register.

Or am I once more completely confused with the endianness crap?

Endianess always confuses me too :D

My reading of the ARM ARM is that the byte reversal is keyed on the access size there too. It lives in Mem{size}, with the register width handled separately by SignExtend(regsize):

data = Mem[address, 2]; // byte-reversed by the access size, BE
X[t] = SignExtend(data, regsize);

So vcpu_data_host_to_guest(..., len) swapping by len matches the Mem-side reversal. Swapping by the register width would reorder bytes that were never loaded. An LDRSH into Wt reads 2 bytes but would bswap 4: the halfword reaches the helper as 0x0180 host-native, cpu_to_be32 turns it into 0x80010000 instead of the 0x8001 cpu_to_be16 gives, and it never sign-extends to 0xffff8001.

If that reading holds, none of the helper's ops are individually wrong, and the only bug was the order, with the sign-extend running before the swap and the width mask then dropping it. But I've gone round in circles on endianness before (to say the least), so please say if I've done it again.

Cheers,
/fuad

Quoted 5 lines
        M.

--
Without deviation from the norm, progress is not possible.

maintainer
#4 lore raw

Re: [PATCH 1/2] KVM: arm64: Fix sign-extension of MMIO loads

Quoted 15 lines · Fuad Tabba

My reading of the ARM ARM is that the byte reversal is keyed on the access size there too. It lives in Mem{size}, with the register width handled separately by SignExtend(regsize):

data = Mem[address, 2]; // byte-reversed by the access size, BE
X[t] = SignExtend(data, regsize);

So vcpu_data_host_to_guest(..., len) swapping by len matches the Mem-side reversal. Swapping by the register width would reorder bytes that were never loaded. An LDRSH into Wt reads 2 bytes but would bswap 4: the halfword reaches the helper as 0x0180 host-native, cpu_to_be32 turns it into 0x80010000 instead of the 0x8001 cpu_to_be16 gives, and it never sign-extends to 0xffff8001.

Quoted 4 lines

If that reading holds, none of the helper's ops are individually wrong, and the only bug was the order, with the sign-extend running before the swap and the width mask then dropping it. But I've gone round in circles on endianness before (to say the least), so please say if I've done it again.

That's quite convincing.

And the quoted pseudocode is much easier to reason about than the current blurb in the commit message. For reference, J1.2.3.111 Mem{}() is the relevant bit of the M.b spec and clearly shows that the access is done LE, and only then byteswapped. Can you please repaint the commit log to describe things in those terms?

Also, can you augment your test to cover for BE accesses from the guest if the HW supports it?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#5 lore raw

Re: [PATCH 1/2] KVM: arm64: Fix sign-extension of MMIO loads

Quoted 23 lines · Marc Zyngier
Quoted 15 lines · Fuad Tabba

My reading of the ARM ARM is that the byte reversal is keyed on the access size there too. It lives in Mem{size}, with the register width handled separately by SignExtend(regsize):

data = Mem[address, 2]; // byte-reversed by the access size, BE
X[t] = SignExtend(data, regsize);

So vcpu_data_host_to_guest(..., len) swapping by len matches the Mem-side reversal. Swapping by the register width would reorder bytes that were never loaded. An LDRSH into Wt reads 2 bytes but would bswap 4: the halfword reaches the helper as 0x0180 host-native, cpu_to_be32 turns it into 0x80010000 instead of the 0x8001 cpu_to_be16 gives, and it never sign-extends to 0xffff8001.

Quoted 4 lines

If that reading holds, none of the helper's ops are individually wrong, and the only bug was the order, with the sign-extend running before the swap and the width mask then dropping it. But I've gone round in circles on endianness before (to say the least), so please say if I've done it again.

Quoted 7 lines

That's quite convincing.

And the quoted pseudocode is much easier to reason about than the current blurb in the commit message. For reference, J1.2.3.111 Mem{}() is the relevant bit of the M.b spec and clearly shows that the access is done LE, and only then byteswapped. Can you please repaint the commit log to describe things in those terms?

Will do.

Quoted 3 lines

Also, can you augment your test to cover for BE accesses from the guest if the HW supports it?

I'll see what I can come up with...

Cheers,
/fuad

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

author reviewer
#6 lore raw

PATCH 2/2 KVM: arm64: selftests: Add MMIO sign-extending load test

In reply to Fuad Tabba

Add a test for sign-extending MMIO loads (LDRSB, LDRSH, LDRSW) into Xt and Wt destinations, with and without the sign bit set. The host supplies the MMIO data and checks the guest register holds the sign-extended value.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/mmio_sign_ext.c       | 133 ++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/mmio_sign_ext.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 9118a5a51b89f..0f5803a1092e1 100644
@@ -171,6 +171,7 @@ TEST_GEN_PROGS_arm64 += arm64/hello_el2
 TEST_GEN_PROGS_arm64 += arm64/host_sve
 TEST_GEN_PROGS_arm64 += arm64/hypercalls
 TEST_GEN_PROGS_arm64 += arm64/external_aborts
+TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer Reviewed-by on the v1 series
#7 lore raw
In reply to Fuad Tabba
Quoted 12 lines · Fuad Tabba

Hi folks,

A sign-extending load (LDRSB/LDRSH/LDRSW) from emulated MMIO returns a zero-extended value rather than the sign-extended one the architecture requires; vcpu_data_host_to_guest() strips the sign bits when it masks the data to the access width.

If my git archeology is right, the masking dates to 2014 (b30070862edbd, big-endian support) and has been wrong ever since, but sign-extending loads from device memory are rare enough that nobody hit it. Patch 1 fixes it; patch 2 adds a selftest so it doesn't regress.

Quoted 6 lines

Cheers,
/fuad

Fuad Tabba (2):
KVM: arm64: Fix sign-extension of MMIO loads
KVM: arm64: selftests: Add MMIO sign-extending load test

For the oddball guest :)

Thanks,
Oliver