From: Andrew Jones <ajones@ventanamicro.com>
To: Atish Patra <atishp@rivosinc.com>
Cc: Anup Patel <anup@brainfault.org>,
Atish Patra <atishp@atishpatra.org>,
Paolo Bonzini <pbonzini@redhat.com>,
Shuah Khan <shuah@kernel.org>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Alexandre Ghiti <alex@ghiti.fr>,
kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/3] KVM: riscv: selftests: Decode stval to identify exact exception type
Date: Fri, 25 Apr 2025 15:33:54 +0200 [thread overview]
Message-ID: <20250425-f458b9801a83cc35bda55c74@orel> (raw)
In-Reply-To: <20250324-kvm_selftest_improve-v1-2-583620219d4f@rivosinc.com>
On Mon, Mar 24, 2025 at 05:40:30PM -0700, Atish Patra wrote:
> Currently, the sbi_pmu_test continues if the exception type is illegal
> instruction because access to hpmcounter will generate that. However, we
> may get illegal for other reasons as well which should result in test
> assertion.
>
> Use the stval to decode the exact type of instructions and which csrs are
> being accessed if it is csr access instructions. Assert in all cases
> except if it is a csr access instructions that access valid PMU related
> registers.
>
> Signed-off-by: Atish Patra <atishp@rivosinc.com>
> ---
> tools/testing/selftests/kvm/riscv/sbi_pmu_test.c | 32 ++++++++++++++++++++++++
> 1 file changed, 32 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c b/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c
> index 03406de4989d..11bde69b5238 100644
> --- a/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c
> +++ b/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c
> @@ -128,11 +128,43 @@ static void stop_counter(unsigned long counter, unsigned long stop_flags)
> "Unable to stop counter %ld error %ld\n", counter, ret.error);
> }
>
> +#define INSN_OPCODE_MASK 0x007c
> +#define INSN_OPCODE_SHIFT 2
> +#define INSN_OPCODE_SYSTEM 28
> +
> +#define INSN_MASK_FUNCT3 0x7000
> +#define INSN_SHIFT_FUNCT3 12
> +
> +#define INSN_CSR_MASK 0xfff00000
> +#define INSN_CSR_SHIFT 20
> +
> +#define GET_RM(insn) (((insn) & INSN_MASK_FUNCT3) >> INSN_SHIFT_FUNCT3)
> +#define GET_CSR_NUM(insn) (((insn) & INSN_CSR_MASK) >> INSN_CSR_SHIFT)
It'd be good to put these macros in include/riscv/processor.h or some new
include/riscv/ header to be shared with other tests that may want to
decode stval.
Thanks,
drew
> +
> static void guest_illegal_exception_handler(struct ex_regs *regs)
> {
> + unsigned long insn;
> + int opcode, csr_num, funct3;
> +
> __GUEST_ASSERT(regs->cause == EXC_INST_ILLEGAL,
> "Unexpected exception handler %lx\n", regs->cause);
>
> + insn = regs->stval;
> + opcode = (insn & INSN_OPCODE_MASK) >> INSN_OPCODE_SHIFT;
> + __GUEST_ASSERT(opcode == INSN_OPCODE_SYSTEM,
> + "Unexpected instruction with opcode 0x%x insn 0x%lx\n", opcode, insn);
> +
> + csr_num = GET_CSR_NUM(insn);
> + funct3 = GET_RM(insn);
> + /* Validate if it is a CSR read/write operation */
> + __GUEST_ASSERT(funct3 <= 7 && (funct3 != 0 || funct3 != 4),
> + "Unexpected system opcode with funct3 0x%x csr_num 0x%x\n",
> + funct3, csr_num);
> +
> + /* Validate if it is a HPMCOUNTER CSR operation */
> + __GUEST_ASSERT(csr_num == CSR_CYCLE || csr_num <= CSR_HPMCOUNTER31,
> + "Unexpected csr_num 0x%x\n", csr_num);
> +
> illegal_handler_invoked = true;
> /* skip the trapping instruction */
> regs->epc += 4;
>
> --
> 2.43.0
>
>
> --
> kvm-riscv mailing list
> kvm-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/kvm-riscv
next prev parent reply other threads:[~2025-04-25 13:33 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-25 0:40 [PATCH 0/3] RISC-V KVM selftests improvements Atish Patra
2025-03-25 0:40 ` [PATCH 1/3] KVM: riscv: selftests: Add stval to exception handling Atish Patra
2025-04-25 12:09 ` Anup Patel
2025-04-25 13:50 ` Andrew Jones
2025-04-28 22:47 ` Atish Patra
2025-04-29 9:05 ` Andrew Jones
2025-03-25 0:40 ` [PATCH 2/3] KVM: riscv: selftests: Decode stval to identify exact exception type Atish Patra
2025-04-25 12:12 ` Anup Patel
2025-04-25 13:33 ` Andrew Jones [this message]
2025-04-28 22:48 ` Atish Patra
2025-03-25 0:40 ` [PATCH 3/3] KVM: riscv: selftests: Add vector extension tests Atish Patra
2025-04-25 12:16 ` Anup Patel
2025-04-25 14:20 ` Andrew Jones
2025-04-29 0:32 ` Atish Patra
2025-04-29 9:15 ` Andrew Jones
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=20250425-f458b9801a83cc35bda55c74@orel \
--to=ajones@ventanamicro.com \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=atishp@atishpatra.org \
--cc=atishp@rivosinc.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
/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