Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Charlie Jenkins via B4 Relay"
	<devnull+thecharlesjenkins.gmail.com@kernel.org>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2 11/16] riscv: kvm: Use generated instruction headers for mmio emulation
Date: Mon, 22 Jun 2026 04:27:08 +0000	[thread overview]
Message-ID: <20260622042709.59C011F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260621-riscv_insn_table-v2-11-7f0810ee2274@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The migration to generated instruction headers missed the required '+8' offset for compressed instructions that use 3-bit register fields, causing KVM to access the wrong architectural registers.
- [High] Removal of `#ifdef CONFIG_64BIT` guards exposes 32-bit KVM hosts to Undefined Behavior (negative bit shift) when a guest triggers 64-bit instruction emulation.
--

commit 5795a15cde75d07e7edad3d6b52a93e9aa4d9d86
Author: Charlie Jenkins <thecharlesjenkins@gmail.com>

riscv: kvm: Use generated instruction headers for mmio emulation
    
Migrate the mmio emulation code to use the generated instruction headers
instead of the hand-written instruction composition functions.

> diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
> index 8ccf6ec722f03..1d8741d022425 100644
> --- a/arch/riscv/kvm/vcpu_insn.c
> +++ b/arch/riscv/kvm/vcpu_insn.c
[ ... ]
> @@ -408,44 +408,47 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
[ ... ]
> -#ifdef CONFIG_64BIT
> -	} else if ((insn & INSN_MASK_LD) == INSN_MATCH_LD) {
> +	} else if (riscv_insn_is_ld(insn)) {
>  		len = 8;
>  		shift = 8 * (sizeof(ulong) - len);
> -	} else if ((insn & INSN_MASK_LWU) == INSN_MATCH_LWU) {

[Severity: High]
By removing these CONFIG_64BIT guards, does this expose 32-bit KVM hosts
to undefined behavior?

If a guest triggers emulation of a 64-bit load, len becomes 8, and shift
is calculated as 8 * (sizeof(ulong) - len). On a 32-bit host where
sizeof(ulong) is 4, shift becomes -32.

This would result in a negative bit shift when kvm_riscv_vcpu_mmio_return()
later evaluates (long)data << shift >> shift.

> +		rd = riscv_insn_ld_extract_xd(insn);
> +	} else if (riscv_insn_is_lwu(insn)) {
>  		len = 4;
> -#endif
[ ... ]
> -	} else if ((insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW) {
> +	} else if (riscv_insn_is_c_lw(insn)) {
>  		len = 4;
>  		shift = 8 * (sizeof(ulong) - len);
> -		insn = RVC_RS2S(insn) << SH_RD;
> -	} else if ((insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP &&
> -		   ((insn >> SH_RD) & 0x1f)) {
> +		rd = riscv_insn_c_lw_extract_xd(insn);

[Severity: High]
Does this code read or write to the wrong guest register?

Compressed instructions like c.lw and c.sw use 3-bit register fields
corresponding to general purpose registers x8 through x15. The generated
extraction macros (like riscv_insn_c_lw_extract_xd) return the raw 0-7
value.

Without an explicit +8 offset mapping, won't KVM incorrectly use registers
x0-x7 as the index into the guest_context array?

> +	} else if (riscv_insn_is_c_lwsp(insn)) {
>  		len = 4;
>  		shift = 8 * (sizeof(ulong) - len);
> +		rd = riscv_insn_c_lwsp_extract_xd(insn);
>  	} else {
>  		return -EOPNOTSUPP;
>  	}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260621-riscv_insn_table-v2-0-7f0810ee2274@gmail.com?part=11

  reply	other threads:[~2026-06-22  4:27 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-22  4:01 [PATCH v2 00/16] riscv: Generate riscv instruction functions Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 01/16] riscv: Introduce instruction table generation Charlie Jenkins via B4 Relay
2026-06-22  4:13   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 02/16] riscv: alternatives: Use generated instruction headers for patching code Charlie Jenkins via B4 Relay
2026-06-22  4:28   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 03/16] riscv: kgdb: Use generated instruction headers Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 04/16] riscv: Add kprobes instruction simulation KUnit Charlie Jenkins via B4 Relay
2026-06-22  4:19   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 05/16] riscv: kprobes: Use generated instruction headers Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 06/16] riscv: cfi: " Charlie Jenkins via B4 Relay
2026-06-22  4:35   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 07/16] riscv: Use generated instruction headers for misaligned loads/stores Charlie Jenkins via B4 Relay
2026-06-22  4:18   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 08/16] riscv: kvm: Use generated instruction headers for csr code Charlie Jenkins via B4 Relay
2026-06-22  4:18   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 09/16] KVM: device: Add test device Charlie Jenkins via B4 Relay
2026-06-22  4:13   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 10/16] KVM: riscv: selftests: Add mmio test Charlie Jenkins via B4 Relay
2026-06-22  4:18   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 11/16] riscv: kvm: Use generated instruction headers for mmio emulation Charlie Jenkins via B4 Relay
2026-06-22  4:27   ` sashiko-bot [this message]
2026-06-22  4:01 ` [PATCH v2 12/16] riscv: kvm: Add emulated test csr Charlie Jenkins via B4 Relay
2026-06-22  4:23   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 13/16] KVM: riscv: selftests: Add csr emulation test Charlie Jenkins via B4 Relay
2026-06-22  4:24   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 14/16] riscv: kvm: Use generated instruction headers for csr emulation Charlie Jenkins via B4 Relay
2026-06-22  4:30   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 15/16] riscv: kexec: Use generated instruction headers for kexec relocations Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 16/16] riscv: Remove unused instruction headers Charlie Jenkins via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260622042709.59C011F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=devnull+thecharlesjenkins.gmail.com@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox