KVM: arm64: Fix and test MMIO sign-extending loads
Fuad Tabba
reviewerv2 cover KVM: arm64: Fix and test MMIO sign-extending loads
Hi folks,
Changes since v1 [1]:
- Patch 1: rewrote the commit msg in the Arm ARM's terms, with the Mem accessor performing the access keyed on the access size and SignExtend handling the register width. No code change. (Marc)
- Patch 2: added a big-endian pass to the test. The big-endian loads run at EL0 with SCTLR_EL1.E0E set, so the access is big-endian while the stage-1 walk stays little-endian. (Marc)
Oliver's Reviewed-by is on patch 1 only: the code there is unchanged, while the test in patch 2 gained the big-endian coverage above.
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.
Based on Linux 7.1 (8cd9520d35a6c).
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 | 259 ++++++++++++++++++ 3 files changed, 264 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/kvm/arm64/mmio_sign_ext.c
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH v2 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.
--- 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
Fuad Tabba
reviewerPATCH v2 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.
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 | 259 ++++++++++++++++++ 2 files changed, 260 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 +259 -0
259 lines not on this page · raw message
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH v2 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] Guest EL1 will crash with an Instruction Abort because the `el0_be_loads` assembly block shares…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Guest EL1 will crash with an Instruction Abort because the el0_be_loads assembly block shares a page with EL1 test code and is marked as EL0-accessible. --
commit 60c24b3648e8f92661ec2d484d27ac4580cad60a
Author: Fuad Tabba <[email protected]>
KVM: arm64: selftests: Add MMIO sign-extending load test
Adds 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 5 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +0
-0
[ ... ]
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c line 84
Quoted 5 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +5
-0+extern char el0_be_loads[], el0_be_loads_end[]; +asm( +" .pushsection .text, \"ax\"\n" +" .global el0_be_loads\n" +"el0_be_loads:\n"
[Severity: Medium]
Will this crash guest EL1 with an Instruction Abort?
The el0_be_loads assembly block lacks page alignment directives, placing it adjacent to EL1 executable code in the same page of the .text section.
When make_el0_accessible() is called in main():
make_el0_accessible(vm, (uint64_t)el0_be_loads,
el0_be_loads_end - el0_be_loads);It makes the shared page EL0-accessible by setting PTE_USER. This inadvertently makes the EL1 guest_code on the same page EL0-accessible. In the ARMv8 EL1&0 translation regime, an instruction fetch by EL1 from an EL0-accessible memory page unconditionally generates a Permission Fault.
Quoted 3 lines
+" ldrsb x19, [x0]\n"
+" ldrsb x20, [x0]\n"
+" ldrsb w21, [x0]\n"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Fuad Tabba
reviewerRe: [PATCH v2 2/2] KVM: arm64: selftests: Add MMIO sign-extending load test
Quoted 36 lines · On Thu, 25 Jun 2026 at 15:57, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Guest EL1 will crash with an Instruction Abort because theel0_be_loadsassembly block shares a page with EL1 test code and is marked as EL0-accessible. --commit
60c24b3648e8f92661ec2d484d27ac4580cad60a
Author: Fuad Tabba <[email protected]>KVM: arm64: selftests: Add MMIO sign-extending load test
Adds 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 5 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +0
-0[ ... ]
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c
Quoted 5 lines
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c new file +5
-0+extern char el0_be_loads[], el0_be_loads_end[]; +asm( +" .pushsection .text, \"ax\"\n" +" .global el0_be_loads\n" +"el0_be_loads:\n"[Severity: Medium]
Will this crash guest EL1 with an Instruction Abort?The
el0_be_loadsassembly block lacks page alignment directives, placing it adjacent to EL1 executable code in the same page of the .text section.When
make_el0_accessible()is called inmain():
Quoted 7 lines
make_el0_accessible(vm, (uint64_t)el0_be_loads, el0_be_loads_end - el0_be_loads);It makes the shared page EL0-accessible by setting
PTE_USER. This inadvertently makes the EL1guest_codeon the same page EL0-accessible. In the ARMv8 EL1&0 translation regime, an instruction fetch by EL1 from an EL0-accessible memory page unconditionally generates a Permission Fault.
Nice catch! You're right: AP[2:1]=01 grants UnprivWrite, and the architecture removes PrivExecute whenever UnprivWrite is present, regardless of PXN (ARM ARM D8.4.1.2). The call is also unnecessary:
EL0 instruction fetch is controlled by UXN (already 0), not AP bits. The EL0 stub only needs data access to the MMIO page, not to its own code.
The fix is to drop the second make_el0_accessible call and the now-unused el0_be_loads_end label. Re-tested with and without the previous patch (patch 1/2), all tests pass with the fix and the first sign-bit case fails without it.
Marc, can you fold this in or would you like me to respin?
tools/testing/selftests/kvm/arm64/mmio_sign_ext.c +2 -7
@@ -77,7 +77,7 @@ static void guest_loads_le(void)* fault on the little-endian tables. x0 holds MMIO_ADDR; results return in * x19..x28 (tests[] order) via a single SVC. */ -extern char el0_be_loads[], el0_be_loads_end[]; +extern char el0_be_loads[]; asm(" .pushsection .text, \"ax\"\n" " .global el0_be_loads\n"
@@ -93,8 +93,6 @@ asm(" ldrsw x27, [x0]\n" " ldrsw x28, [x0]\n" " svc #0\n" -" .global el0_be_loads_end\n" -"el0_be_loads_end:\n" " .popsection\n" );
@@ -213,11 +211,8 @@ int main(void) el0_svc_handler); be =vcpu_mixed_endian_el0 (vcpu); -if (be) { +if (be)make_el0_accessible (vm,MMIO_ADDR , vm->page_size); -make_el0_accessible (vm, (uint64_t )el0_be_loads, - el0_be_loads_end - el0_be_loads); - }ksft_print_header ();ksft_set_plan (ARRAY_SIZE (tests) * (be ?2 :1 ));
Cheers,
/fuad
Quoted 8 lines
Quoted 4 lines
+" ldrsb x19, [x0]\n" +" ldrsb x20, [x0]\n" +" ldrsb w21, [x0]\n"--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2