[PATCH 08/16] riscv: kvm: Fix MMIO emulation for sign-extended insns
From: Charlie Jenkins via B4 Relay
Date: Wed Apr 08 2026 - 00:48:18 EST
From: Charlie Jenkins <thecharlesjenkins@xxxxxxxxx>
KVM MMIO emulation failed to sign extend any signed reads and at the
same time also unsuccessfully attempted to sign extend reads using lbu.
Remove the shifting for lbu to avoid sign extension for that
instruction and cast the data to a signed long instead of an unsigned
long to allow for sign extension.
Signed-off-by: Charlie Jenkins <thecharlesjenkins@xxxxxxxxx>
---
arch/riscv/kvm/vcpu_insn.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 62c4510a40af..311e2530f888 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -416,7 +416,6 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
shift = 8 * (sizeof(ulong) - len);
} else if ((insn & INSN_MASK_LBU) == INSN_MATCH_LBU) {
len = 1;
- shift = 8 * (sizeof(ulong) - len);
#ifdef CONFIG_64BIT
} else if ((insn & INSN_MASK_LD) == INSN_MATCH_LD) {
len = 8;
@@ -650,22 +649,22 @@ int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
case 1:
data8 = *((u8 *)run->mmio.data);
SET_RD(insn, &vcpu->arch.guest_context,
- (ulong)data8 << shift >> shift);
+ (long)data8 << shift >> shift);
break;
case 2:
data16 = *((u16 *)run->mmio.data);
SET_RD(insn, &vcpu->arch.guest_context,
- (ulong)data16 << shift >> shift);
+ (long)data16 << shift >> shift);
break;
case 4:
data32 = *((u32 *)run->mmio.data);
SET_RD(insn, &vcpu->arch.guest_context,
- (ulong)data32 << shift >> shift);
+ (long)data32 << shift >> shift);
break;
case 8:
data64 = *((u64 *)run->mmio.data);
SET_RD(insn, &vcpu->arch.guest_context,
- (ulong)data64 << shift >> shift);
+ (long)data64 << shift >> shift);
break;
default:
return -EOPNOTSUPP;
--
2.52.0