KVM: arm64: Fix and test MMIO sign-extending loads
Fuad Tabba
reviewercover 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
Fuad Tabba
reviewerPATCH 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.
--- arch/arm64/kvm/mmio.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmio.c +4 -3
@@ -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
Marc Zyngier
maintainerRe: [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")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/mmio.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/arm64/kvm/mmio.c +0
-0
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.
Fuad Tabba
reviewerRe: [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")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/mmio.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/arm64/kvm/mmio.c +0
-0arch/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.
Marc Zyngier
maintainerRe: [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 as0x0180host-native,cpu_to_be32turns it into0x80010000instead of the0x8001cpu_to_be16gives, and it never sign-extends to0xffff8001.
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.
Fuad Tabba
reviewerRe: [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 as0x0180host-native,cpu_to_be32turns it into0x80010000instead of the0x8001cpu_to_be16gives, and it never sign-extends to0xffff8001.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.
Fuad Tabba
reviewerPATCH 2/2 KVM: arm64: selftests: Add MMIO sign-extending load test
In reply to Fuad TabbaAdd 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
@@ -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
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,
/fuadFuad 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