Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Charlie Jenkins" <thecharlesjenkins@gmail.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v3 14/17] KVM: riscv: selftests: Add csr emulation test
Date: Wed, 08 Jul 2026 03:58:53 +0000	[thread overview]
Message-ID: <20260708035854.485EB1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707-riscv_insn_table-v3-14-3f8e5f75ba97@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Unaligned trap vector in `check_test_csr_guest` leads to an infinite exception loop.
- [Medium] Missing check for the RISC-V F (Floating-Point) extension causes spurious test crashes on non-F systems.
--

--- Patch [14]: [PATCH v3 14/17] KVM: riscv: selftests: Add csr emulation test ---
commit de141f953203428fe244492005f127de5ea224f3
Author: Charlie Jenkins <thecharlesjenkins@gmail.com>

KVM: riscv: selftests: Add csr emulation test

Introduce a kvm test that uses the emulated test csr to validate that
all emulated reads/writes to csrs function as expected.

> diff --git a/tools/testing/selftests/kvm/riscv/csr_test.c b/tools/testing/selftests/kvm/riscv/csr_test.c
> new file mode 100644
> index 0000000000000..bdedf7c84001c
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/riscv/csr_test.c
> @@ -0,0 +1,133 @@
[ ... ]
> +/*
> + * Use the fcsr as a U-mode accesible csr and compare against the custom 'test'
> + * hypervisor csr (currently using vsscratch)
> + */
> +#define test_csr(write, initial, mode)								\
> +static void test_##write(void)									\
> +{												\
> +	unsigned long hypervisor_result, reference_result, old_hypervisor;			\
> +	asm volatile (										\
> +		"csrs	sstatus, %[enable_fp]\n"						\
> +		"csrw	fcsr, %[init]\n"							\
> +		#write"	zero, fcsr, %[mask]\n"							\
> +		"csrr	%[ref_res], fcsr\n"							\

[Severity: Medium]
Will this test crash on systems lacking the RISC-V F (Floating-Point)
extension?

The test unconditionally writes to and reads from fcsr in test_csr(). On
hosts without the F extension, accessing fcsr will generate an illegal
instruction trap. Since this trap isn't caught, the selftest exception
handler will terminate the test rather than skipping gracefully.

Should main() verify KVM_RISCV_ISA_EXT_F is available before executing
the tests that depend on it?

[ ... ]
> +static void check_test_csr_guest(void)
> +{
> +	unsigned long scause, stvec;
> +
> +	asm volatile(
> +		".option push\n"
> +		".option norvc\n"
> +		"la	%[stvec], 1f\n"
> +		"csrw	stvec, %[stvec]\n"
> +		"csrwi	%[test_csr], 0x0\n"
> +		"1:\n"

[Severity: High]
Could this lead to an infinite exception loop due to an unaligned trap
vector?

The label 1: is used as the exception vector in check_test_csr_guest(),
but it lacks a .align 2 or .balign 4 directive.

The stvec register requires 4-byte alignment. If the compiler aligns the
inline assembly block such that 1: lands on a 2-byte boundary, writing
to stvec will mask the lower bits. When the csrwi instruction traps, the
CPU will jump to the masked address, which could land in the middle of
the instruction.

This immediately triggers another illegal instruction trap, leading to a
soft lockup and hanging the test suite.

Can a .balign 4 be added before the label to ensure proper alignment?

> +		"csrr	%[scause], scause\n"
> +		".option pop\n"
> +		: [scause] "=&r" (scause), [stvec] "=&r" (stvec)
> +		: [test_csr] "i" (CSR_TEST)
> +	);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-riscv_insn_table-v3-0-3f8e5f75ba97@gmail.com?part=14

  reply	other threads:[~2026-07-08  3:58 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08  3:34 [PATCH v3 00/17] riscv: Generate riscv instruction functions Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 01/17] riscv: Introduce instruction table generation Charlie Jenkins
2026-07-08  3:50   ` sashiko-bot
2026-07-09  6:23     ` Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 02/17] riscv: alternatives: Use generated instruction headers for patching code Charlie Jenkins
2026-07-08  3:45   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 03/17] riscv: kgdb: Use generated instruction headers Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 04/17] riscv: Add kprobes instruction simulation KUnit Charlie Jenkins
2026-07-08  3:51   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 05/17] riscv: kprobes: Use generated instruction headers Charlie Jenkins
2026-07-08  3:52   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 06/17] riscv: cfi: " Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 07/17] riscv: Maintain epc on misaligned emulation error Charlie Jenkins
2026-07-08  3:55   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 08/17] riscv: Use generated instruction headers for misaligned loads/stores Charlie Jenkins
2026-07-08  3:49   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 09/17] riscv: kvm: Use generated instruction headers for csr code Charlie Jenkins
2026-07-08  3:48   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 10/17] KVM: device: Add test device Charlie Jenkins
2026-07-08  3:48   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 11/17] KVM: riscv: selftests: Add mmio test Charlie Jenkins
2026-07-08  3:59   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 12/17] riscv: kvm: Use generated instruction headers for mmio emulation Charlie Jenkins
2026-07-08  4:01   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 13/17] riscv: kvm: Add emulated test csr Charlie Jenkins
2026-07-08  4:00   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 14/17] KVM: riscv: selftests: Add csr emulation test Charlie Jenkins
2026-07-08  3:58   ` sashiko-bot [this message]
2026-07-08  3:34 ` [PATCH v3 15/17] riscv: kvm: Use generated instruction headers for csr emulation Charlie Jenkins
2026-07-08  4:04   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 16/17] riscv: kexec: Use generated instruction headers for kexec relocations Charlie Jenkins
2026-07-08  4:01   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 17/17] riscv: Remove unused instruction headers Charlie Jenkins
2026-07-08  4:09   ` sashiko-bot
2026-07-09  6:26 ` [syzbot ci] Re: riscv: Generate riscv instruction functions syzbot ci

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=20260708035854.485EB1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thecharlesjenkins@gmail.com \
    /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