Skip to contentSkip to conversation

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

PATCH Applied Fuad Tabba 3 participants 6 messages Jul 6

Subsystems (4)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

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

Hi folks,

Changes since v2 [1]:
- Patch 2: dropped the second make_el0_accessible() call and the el0_be_loads_end symbol; the EL0 code page does not need its user-access bit set because it is fetched, not loaded. (sashiko)

Oliver's Reviewed-by is on patch 1 only: the code there is unchanged since v1.

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.

Both passes run under QEMU, which reports mixed-endian support.

Based on Linux 7.2-rc2 (8cdeaa50eae8d).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/

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       | 255 ++++++++++++++++++
 3 files changed, 260 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/mmio_sign_ext.c
Signature · 1 line
2.39.5
author reviewer
#1 lore raw

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

A sign-extending load (LDRSB, LDRSH, LDRSW) from MMIO returns a zero-extended value to the guest. The architecture performs such a load as a memory read of the access size, then a sign-extension to the register width. For LDRSH (DDI 0487 M.b C6.2.225, with the Mem accessor at J1.2.3.111):

    data = Mem{16}(address, accdesc);
    X{regsize}(t) = SignExtend{regsize}(data);

The byte order is handled inside the Mem accessor, keyed on the access size; the register width is separate, applied afterwards by SignExtend().

kvm_handle_mmio_return() runs these in the wrong order: it sign-extends the access-width data, then calls vcpu_data_host_to_guest(), which masks the value back to the access width (the size-keyed byte-order step). The mask drops the sign bits that sign-extension produced.

Reorder so vcpu_data_host_to_guest() runs first, with the sign-extension to register width after it. trace_kvm_mmio() moves with it and now logs the access-width data before sign-extension.

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
author reviewer
#2 lore raw

PATCH v3 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.

Repeat the loads big-endian on a mixed-endian implementation. Issue those at EL0: SCTLR_EL1.EE would make an EL1 load big-endian but also walk the little-endian page tables big-endian, whereas SCTLR_EL1.E0E selects only EL0 data endianness and leaves the walk little-endian.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/mmio_sign_ext.c       | 255 ++++++++++++++++++
 2 files changed, 256 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/mmio_sign_ext.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index d28a057fa6c2d..6fc34e9bf8e1b 100644
@@ -174,6 +174,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 +255 -0

255 lines not on this page · raw message

Signature · 1 line
2.39.5
#3 lore raw

Re: [PATCH v3 2/2] KVM: arm64: selftests: Add MMIO sign-extending load test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The test executes guest code at EL0 but fails to map the executable text segment with…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test executes guest code at EL0 but fails to map the executable text segment with unprivileged access permissions (PTE_USER), causing an immediate Instruction Abort. --

commit 8d8bc26c675634520d0cf6c2769677a38145f1a9
Author: Fuad Tabba <[email protected]>

KVM: arm64: selftests: Add MMIO sign-extending load test

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.

Quoted 4 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 14 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +14 -0
+static void guest_code(void)
+{
+	guest_loads_le();
+
+	if (guest_mixed_endian_el0()) {
+		write_sysreg(read_sysreg(sctlr_el1) | SCTLR_EL1_E0E, sctlr_el1);
+		isb();
+
+		asm volatile(
+		"	msr	elr_el1, %[pc]\n"
+		"	msr	spsr_el1, %[spsr]\n"
+		"	mov	x0, %[mmio]\n"
+		"	isb\n"
+		"	eret\n"

[ ... ]

Quoted 9 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +9 -0
+int main(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	unsigned int i;
+	bool be;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);

tools/testing/selftests/kvm/arm64/mmio_sign_ext.c line 216

Quoted 10 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +10 -0
+	virt_map(vm, MMIO_ADDR, MMIO_ADDR, 1);
+
+	vm_init_descriptor_tables(vm);
+	vcpu_init_descriptor_tables(vcpu);
+	vm_install_sync_handler(vm, VECTOR_SYNC_LOWER_64, ESR_ELx_EC_SVC64,
+				el0_svc_handler);
+
+	be = vcpu_mixed_endian_el0(vcpu);
+	if (be)
+		make_el0_accessible(vm, MMIO_ADDR, vm->page_size);

[Severity: Medium]
Will this cause an immediate Instruction Abort on hardware supporting mixed endianness at EL0?

The MMIO address is made EL0-accessible here, but the code segment containing el0_be_loads is not. KVM selftests map the test payload (including the .text segment) as EL1-only by default (AP[1]=0).

When vcpu_mixed_endian_el0() is true, guest_code() drops to EL0 using eret to execute el0_be_loads. Since the page containing el0_be_loads lacks unprivileged access permissions (PTE_USER), the instruction fetch at EL0 will trigger a Permission Fault.

Should the page containing el0_be_loads also be made EL0-accessible here?

Quoted 3 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +3 -0
+
+	ksft_print_header();
+	ksft_set_plan(ARRAY_SIZE(tests) * (be ? 2 : 1));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#4 lore raw

Re: [PATCH v3 2/2] KVM: arm64: selftests: Add MMIO sign-extending load test

Quoted 66 lines · On Mon, 6 Jul 2026 at 13:08, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test executes guest code at EL0 but fails to map the executable text segment with unprivileged access permissions (PTE_USER), causing an immediate Instruction Abort. --

commit 8d8bc26c675634520d0cf6c2769677a38145f1a9
Author: Fuad Tabba <[email protected]>

KVM: arm64: selftests: Add MMIO sign-extending load test

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.

Quoted 4 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 14 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +14 -0
+static void guest_code(void)
+{
+     guest_loads_le();
+
+     if (guest_mixed_endian_el0()) {
+             write_sysreg(read_sysreg(sctlr_el1) | SCTLR_EL1_E0E, sctlr_el1);
+             isb();
+
+             asm volatile(
+             "       msr     elr_el1, %[pc]\n"
+             "       msr     spsr_el1, %[spsr]\n"
+             "       mov     x0, %[mmio]\n"
+             "       isb\n"
+             "       eret\n"

[ ... ]

Quoted 9 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +9 -0
+int main(void)
+{
+     struct kvm_vcpu *vcpu;
+     struct kvm_vm *vm;
+     struct ucall uc;
+     unsigned int i;
+     bool be;
+
+     vm = vm_create_with_one_vcpu(&vcpu, guest_code);

tools/testing/selftests/kvm/arm64/mmio_sign_ext.c line 216

Quoted 10 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +10 -0
+     virt_map(vm, MMIO_ADDR, MMIO_ADDR, 1);
+
+     vm_init_descriptor_tables(vm);
+     vcpu_init_descriptor_tables(vcpu);
+     vm_install_sync_handler(vm, VECTOR_SYNC_LOWER_64, ESR_ELx_EC_SVC64,
+                             el0_svc_handler);
+
+     be = vcpu_mixed_endian_el0(vcpu);
+     if (be)
+             make_el0_accessible(vm, MMIO_ADDR, vm->page_size);

[Severity: Medium]
Will this cause an immediate Instruction Abort on hardware supporting mixed endianness at EL0?

The MMIO address is made EL0-accessible here, but the code segment containing el0_be_loads is not. KVM selftests map the test payload (including the .text segment) as EL1-only by default (AP[1]=0).

Quoted 6 lines

When vcpu_mixed_endian_el0() is true, guest_code() drops to EL0 using eret to execute el0_be_loads. Since the page containing el0_be_loads lacks unprivileged access permissions (PTE_USER), the instruction fetch at EL0 will trigger a Permission Fault.

Should the page containing el0_be_loads also be made EL0-accessible here?

Not an issue. PTE_USER (AP[1]) controls data access at EL0, not instruction fetch, which is governed by UXN. The .text pages have UXN clear, so el0_be_loads is fetchable at EL0 without PTE_USER.

This is actually what your earlier review caught in v2: the old code set PTE_USER on the code page unnecessarily, confusing data access with instruction fetch.

Cheers,
/fuad

Quoted 7 lines

tools/testing/selftests/kvm/arm64/mmio_sign_ext.c line 219

Quoted 4 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +3 -0
+
+     ksft_print_header();
+     ksft_set_plan(ARRAY_SIZE(tests) * (be ? 2 : 1));

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

maintainer Applied
#5 lore raw
In reply to Fuad Tabba
Quoted 9 lines · Fuad Tabba

Changes since v2 [1]:
- Patch 2: dropped the second make_el0_accessible() call and the el0_be_loads_end symbol; the EL0 code page does not need its user-access bit set because it is fetched, not loaded. (sashiko)

Oliver's Reviewed-by is on patch 1 only: the code there is unchanged since v1.

[...]

Applied to fixes, thanks!

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

      commit: 85f56708a443ec02a290878f00d79a1ff5110e41
[2/2] KVM: arm64: selftests: Add MMIO sign-extending load test
      commit: 65a38ddeaeed1a962bc1b4ca03c98201c877fea5

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.