[PW_SID:1094650] RISC-V: KVM: Fix sign extension for MMIO loads#1931
[PW_SID:1094650] RISC-V: KVM: Fix sign extension for MMIO loads#1931linux-riscv-bot wants to merge 1 commit into
Conversation
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: b91f0e4 ("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
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
|
Patch 1: "RISC-V: KVM: Fix sign extension for MMIO loads" |
PR for series 1094650 applied to workflow__riscv__fixes
Name: RISC-V: KVM: Fix sign extension for MMIO loads
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=1094650
Version: 1