The SBI Firmware Feature extension allows the S-mode to request some specific features (either hardware or software) to be enabled. This series uses this extension to request misaligned access exception delegation to S-mode in order to let the kernel handle it. It also adds support for the KVM FWFT SBI extension based on the misaligned access handling infrastructure. FWFT SBI extension is part of the SBI V3.0 specifications [1]. It can be tested using the qemu provided at [2] which contains the series from [3]. kvm-unit-tests [4] can be used inside kvm to tests the correct delegation of misaligned exceptions. Upstream OpenSBI can be used. The test can be run using a small utility[5] as well as using kvm-tools: $ qemu-system-riscv64 \ -cpu rv64,trap-misaligned-access=true,v=true \ -M virt \ -m 1024M \ -bios fw_dynamic.bin \ -kernel Image ... # ./unaligned Buf base address: 0x5555756ff0f0 Testing emulated misaligned accesses, size 2, offset 1 Testing non-emulated misaligned accesses, size 2, offset 1 Testing emulated misaligned accesses, size 4, offset 1 Testing non-emulated misaligned accesses, size 4, offset 1 Testing emulated misaligned accesses, size 4, offset 2 Testing non-emulated misaligned accesses, size 4, offset 2 Testing emulated misaligned accesses, size 4, offset 3 Testing non-emulated misaligned accesses, size 4, offset 3 Testing emulated misaligned accesses, size 8, offset 1 Testing non-emulated misaligned accesses, size 8, offset 1 Testing emulated misaligned accesses, size 8, offset 2 Testing non-emulated misaligned accesses, size 8, offset 2 Testing emulated misaligned accesses, size 8, offset 3 Testing non-emulated misaligned accesses, size 8, offset 3 Testing emulated misaligned accesses, size 8, offset 4 Testing non-emulated misaligned accesses, size 8, offset 4 Testing emulated misaligned accesses, size 8, offset 5 Testing non-emulated misaligned accesses, size 8, offset 5 Testing emulated misaligned accesses, size 8, offset 6 Testing non-emulated misaligned accesses, size 8, offset 6 Testing emulated misaligned accesses, size 8, offset 7 Testing non-emulated misaligned accesses, size 8, offset 7 # lkvm run -k sbi.flat -m 128 Info: # lkvm run -k sbi.flat -m 128 -c 1 --name guest-97 Info: Removed ghost socket file "/root/.lkvm//guest-97.sock". ########################################################################## # kvm-unit-tests ########################################################################## ... [test messages elided] PASS: sbi: fwft: FWFT extension probing no error PASS: sbi: fwft: get/set reserved feature 0x6 error == SBI_ERR_DENIED PASS: sbi: fwft: get/set reserved feature 0x3fffffff error == SBI_ERR_DENIED PASS: sbi: fwft: get/set reserved feature 0x80000000 error == SBI_ERR_DENIED PASS: sbi: fwft: get/set reserved feature 0xbfffffff error == SBI_ERR_DENIED PASS: sbi: fwft: misaligned_deleg: Get misaligned deleg feature no error PASS: sbi: fwft: misaligned_deleg: Set misaligned deleg feature invalid value error PASS: sbi: fwft: misaligned_deleg: Set misaligned deleg feature invalid value error PASS: sbi: fwft: misaligned_deleg: Set misaligned deleg feature value no error PASS: sbi: fwft: misaligned_deleg: Set misaligned deleg feature value 0 PASS: sbi: fwft: misaligned_deleg: Set misaligned deleg feature value no error PASS: sbi: fwft: misaligned_deleg: Set misaligned deleg feature value 1 PASS: sbi: fwft: misaligned_deleg: Verify misaligned load exception trap in supervisor SUMMARY: 50 tests, 2 unexpected failures, 12 skipped This series is available at [6]. Link: https://github.com/riscv-non-isa/riscv-sbi-doc/releases/download/vv3.0-rc2/riscv-sbi.pdf [1] Link: https://github.com/rivosinc/qemu/tree/dev/cleger/misaligned [2] Link: https://lore.kernel.org/all/20241211211933.198792-3-fkonrad@xxxxxxx/T/ [3] Link: https://github.com/clementleger/kvm-unit-tests/tree/dev/cleger/fwft_v1 [4] Link: https://github.com/clementleger/unaligned_test [5] Link: https://github.com/rivosinc/linux/tree/dev/cleger/fwft_v1 [6] --- Clément Léger (6): riscv: add Firmware Feature (FWFT) SBI extensions definitions riscv: request misaligned exception delegation from SBI RISC-V: KVM: add SBI extension init()/deinit() functions RISC-V: KVM: add support for FWFT SBI extension riscv: export unaligned_ctl_available() as a GPL symbol RISC-V: KVM: add support for SBI_FWFT_MISALIGNED_DELEG arch/riscv/include/asm/cpufeature.h | 1 + arch/riscv/include/asm/kvm_host.h | 4 + arch/riscv/include/asm/kvm_vcpu_sbi.h | 10 + arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h | 37 ++++ arch/riscv/include/asm/sbi.h | 28 +++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kernel/traps_misaligned.c | 61 ++++++ arch/riscv/kernel/unaligned_access_speed.c | 2 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 5 + arch/riscv/kvm/vcpu_sbi.c | 35 +++- arch/riscv/kvm/vcpu_sbi_fwft.c | 215 +++++++++++++++++++++ 12 files changed, 399 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h create mode 100644 arch/riscv/kvm/vcpu_sbi_fwft.c -- 2.47.1