mirror-linux/arch/riscv/kvm
Jiakai Xu c7832534a8 RISC-V: KVM: Fix sign extension for MMIO loads
The kvm_riscv_vcpu_mmio_return() function handles MMIO read results
by writing the data back to the guest register. For signed load
instructions (LB, LH, LW on RV64), the value needs sign-extension
from a smaller integer to unsigned long.

The current code uses:
    (ulong)data << shift >> shift
but (ulong) makes the right shift a logical shift (zero-extend)
rather than an arithmetic shift (sign-extend), causing incorrect
results when the MMIO device returns a negative value. For example,
LB reading 0x80 would return 128 instead of -128.

Fix this by casting to (long) after the left shift so that the
subsequent right shift is arithmetic and correctly propagates
the sign bit:
    (long)((ulong)data << shift) >> shift

Additionally, remove the unnecessary shift assignment for LBU
(unsigned byte load) since it does not need sign extension.
This makes LBU consistent with LHU and LWU which already keep
shift = 0.

Fixes: b91f0e4cb8 ("RISC-V: KVM: Factor-out instruction emulation into separate sources")
Signed-off-by: Jiakai Xu <jiakaiPeanut@gmail.com>
Signed-off-by: Jiakai Xu <xujiakai2025@iscas.ac.cn>
Assisted-by: OpenClaw:DeepSeek-V3.2
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260514081752.472987-1-xujiakai2025@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-05-18 10:19:18 +05:30
..
Kconfig KVM: remove CONFIG_KVM_GENERIC_MMU_NOTIFIER 2026-02-28 15:31:35 +01:00
Makefile RISC-V: KVM: Factor-out VCPU config into separate sources 2026-04-03 17:48:40 +05:30
aia.c RISC-V: KVM: Fix error code returned for Ssaia ONE_REG 2026-03-06 11:20:30 +05:30
aia_aplic.c KVM: riscv: Fix Spectre-v1 in APLIC interrupt handling 2026-03-06 11:20:30 +05:30
aia_device.c RISC-V: KVM: Factor-out ISA checks into separate sources 2026-04-03 17:35:01 +05:30
aia_imsic.c RISC-V: KVM: Fix null pointer dereference in kvm_riscv_vcpu_aia_rmw_topei() 2026-03-06 11:20:30 +05:30
gstage.c RISC-V: KVM: Cache gstage pgd_levels in struct kvm_gstage 2026-04-04 13:46:46 +05:30
isa.c RISC-V: KVM: Factor-out ISA checks into separate sources 2026-04-03 17:35:01 +05:30
main.c RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode 2026-04-04 13:21:21 +05:30
mmu.c RISC-V: KVM: Cache gstage pgd_levels in struct kvm_gstage 2026-04-04 13:46:46 +05:30
nacl.c RISC-V: KVM: Add common nested acceleration support 2024-10-28 16:43:57 +05:30
tlb.c RISC-V updates for v7.1 2026-04-24 10:00:37 -07:00
trace.h RISCV: KVM: add tracepoints for entry and exit events 2024-06-26 18:37:36 +05:30
vcpu.c RISC-V: KVM: Don't check hstateen0 when updating sstateen0 CSR 2026-04-03 17:48:45 +05:30
vcpu_config.c RISC-V: KVM: Factor-out VCPU config into separate sources 2026-04-03 17:48:40 +05:30
vcpu_exit.c RISC-V: KVM: Factor-out g-stage page table management 2025-07-28 22:27:30 +05:30
vcpu_fp.c RISC-V: KVM: Factor-out ISA checks into separate sources 2026-04-03 17:35:01 +05:30
vcpu_insn.c RISC-V: KVM: Fix sign extension for MMIO loads 2026-05-18 10:19:18 +05:30
vcpu_onereg.c RISC-V: KVM: Factor-out ISA checks into separate sources 2026-04-03 17:35:01 +05:30
vcpu_pmu.c riscv: kvm: return SBI_ERR_FAILURE for pmu_event_info() when OOM 2026-05-18 10:19:18 +05:30
vcpu_sbi.c RISC-V: KVM: Add SBI MPXY extension support for Guest 2025-11-24 09:55:36 +05:30
vcpu_sbi_base.c RISC-V: KVM: Add separate source for forwarded SBI extensions 2025-11-24 09:55:36 +05:30
vcpu_sbi_forward.c RISC-V: KVM: Add SBI MPXY extension support for Guest 2025-11-24 09:55:36 +05:30
vcpu_sbi_fwft.c Convert remaining multi-line kmalloc_obj/flex GFP_KERNEL uses 2026-02-22 08:26:33 -08:00
vcpu_sbi_hsm.c KVM: RISC-V: refactor sbi reset request 2025-05-21 09:34:49 +05:30
vcpu_sbi_pmu.c RISC-V: KVM: Implement get event info function 2025-09-16 11:49:31 +05:30
vcpu_sbi_replace.c RISC-V: KVM: Add separate source for forwarded SBI extensions 2025-11-24 09:55:36 +05:30
vcpu_sbi_sta.c RISC-V: KVM: Fix invalid HVA warning in steal-time recording 2026-05-18 10:19:18 +05:30
vcpu_sbi_system.c RISC-V: KVM: Convert kvm_riscv_vcpu_sbi_forward() into extension handler 2025-11-24 09:55:36 +05:30
vcpu_sbi_v01.c RISC-V: KVM: Fix NULL pointer dereference in SBI v0.1 SEND_IPI handler 2026-05-18 10:19:18 +05:30
vcpu_switch.S RISC-V: KVM: Use SBI sync SRET call when available 2024-10-28 16:44:03 +05:30
vcpu_timer.c RISC-V: KVM: Factor-out ISA checks into separate sources 2026-04-03 17:35:01 +05:30
vcpu_vector.c RISC-V: KVM: Factor-out ISA checks into separate sources 2026-04-03 17:35:01 +05:30
vm.c RISC-V: KVM: Reuse KVM_CAP_VM_GPA_BITS to select HGATP.MODE 2026-04-04 13:48:21 +05:30
vmid.c RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode 2026-04-04 13:21:21 +05:30