KVM: riscv: selftests: Decode stval to identify exact exception type
authorAtish Patra <atishp@rivosinc.com>
Wed, 30 Apr 2025 08:16:29 +0000 (01:16 -0700)
committerAnup Patel <anup@brainfault.org>
Wed, 21 May 2025 04:04:37 +0000 (09:34 +0530)
Currently, the sbi_pmu_test continues if the exception type is illegal
instruction because access to hpmcounter will generate that. However
illegal instruction exception may occur due to the other reasons
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.

Take this opportunity to remove the CSR_CYCLEH reference as the test is
compiled for RV64 only.

Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20250430-kvm_selftest_improve-v3-2-eea270ff080b@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
tools/testing/selftests/kvm/include/riscv/processor.h
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c

index 1b5aef8..162f303 100644 (file)
 #include <asm/csr.h>
 #include "kvm_util.h"
 
+#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)
+
 static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,
                                    uint64_t idx, uint64_t size)
 {
index 6e66833..924a335 100644 (file)
@@ -73,7 +73,6 @@ unsigned long pmu_csr_read_num(int csr_num)
 
        switch (csr_num) {
        switchcase_csr_read_32(CSR_CYCLE, ret)
-       switchcase_csr_read_32(CSR_CYCLEH, ret)
        default :
                break;
        }
@@ -130,9 +129,28 @@ static void stop_counter(unsigned long counter, unsigned long stop_flags)
 
 static void guest_illegal_exception_handler(struct pt_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->badaddr;
+       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;