From nobody Thu Dec 18 02:26:45 2025 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB6DC2144A2 for ; Wed, 30 Apr 2025 08:16:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746000999; cv=none; b=LVGMmpWqGWXELZE/bA8xWF7SqV1iITyQLNM0sjOKF77wrWxr92jXvpxg3B42qByed40ps6YPyywvPpO3EsRx/df2NF6U6WiqX0kdLwNyguGeXjGajTmyA0D99v0Y8nPcDo+UidHRcFcCAou2sAWgmXkttZvdEqEDgCsGroQL4xI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746000999; c=relaxed/simple; bh=bGusNtEUlaX23wJD2XfALVYCzkrJMuPAiNTlbGcMBJM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZQ9Q0wUSX9coFXb8H5+LXpBZDbyjkw6m9h+2ItsffPyLiuPeaaObowmW4wX0nc5RiPWtiLAYXAOWhQqeQXjPYPM2Pe1ipVBy6h64kRaY/xyz3PH4PPul439Rf0K9+PmJcwu//SAN1AQz9nXeiMEl4a588gvCifiT/VnU0mPqkII= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=RBP7+1fs; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="RBP7+1fs" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-73bf1cef6ceso7013020b3a.0 for ; Wed, 30 Apr 2025 01:16:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746000996; x=1746605796; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=aRFZ5FKZBztRGxrY6vVfDYbRzHdpxr6eQNzNURiD5s8=; b=RBP7+1fsgaaEszj/50cmRkgRo9X444NsS4dDL+X4apzaNhk3/dIGOmRe8Ij0JgEfGi ikOjlkUBSMjcVILPd4Qh+RxSzNbRD4s5sppoBXNnFZegd9oVurUCFsKgq0J4dKsUZsZa rqoYYdvO9J30T9sjOQD13DWdwHs3f4D/Sk/jdSd23RFJ+yD2uRfz5pHyAizzRfay9EVg 39njVFsQPMt4dgmEPIHyBZUzBiGJpzSngtKjUiLrQZu8Zqtfb3PARx9KRwvp4mgTG9lf ge3BNpwPzGWyhbg72aPeRwY1E46DsnYDs+Z7t3+pTHfxlVBemFFSVOJsJLipbUlaFbSx gt4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746000996; x=1746605796; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aRFZ5FKZBztRGxrY6vVfDYbRzHdpxr6eQNzNURiD5s8=; b=CdrZ8gYPHatJEjwOcahJ2du7oiQm+JDakUjmQcTHOTGkDVqXnUP2FgIXJgcTRRApwK lNtxzsmPtQB62+IFyEceO8gNl7b4QQ8WikxroNhO9/q1EjkNE4I0Jqcv6Oyx6q4k69gj i9Gd7grS9I781Wd6a2OaJhRyV/q28IuNerYXoYgWKQJAWgwDbAtBhLjpY5+uXX7wUEOP Cfl5SdXY8ngL4B3NqQSYtX0WnV0qRlyYUGMiCFoT94+vVPDMGZ76J890cro1spUg55Lz v7S+87muj4Vg5u3dV87y+XvBtJhZGEXMi234Ri/Q/l7o+JnrAmhB93IHaJmH1dA9nGGV fmtg== X-Forwarded-Encrypted: i=1; AJvYcCWA8FR9GiNfUkasqDAAKHHSuxSBYe54LT09Ps9e6bqTVDSWCTvECh64OFArWcaHAGdMK4v48kRScBklDsA=@vger.kernel.org X-Gm-Message-State: AOJu0Yxh3nSfHJsD4xDBmppsYD6vZBBKOalng9g58uOeWiOmot9e3qmT L2QKhol6oSz78gg0x6VUeRakxO2zexzpN6o4nnYUog9W4JC8ClTE/u31l+hFzMg= X-Gm-Gg: ASbGncuvvppKGg070YhM6lnS3eRNH17RUf/XA2JIlpQM5ZPRl7vFctGZDkJG7A7anyz Pd+UFcRS7oryDSS65ALHle4O5o2bNKCumG7eVZyXb/uvqs8aAMkiJhLLmWqECV4MbOOKCYRmIRa 1RPoK5mgI1MGBzEIk9p9BV7RMC7e0V22aHe3J0AAvI+/roPqZz5AnyfqzIaI3nkA9C44nMAA+0b O+OMSp65pCLZtjiy16BtSZvbLBXNvpfk4gCLeTefyvlXzBf1X6+sxPMZlbKV3ZywtEYqUDa/gBo D76aJHlVtxi+pHCMJ3tB0FCKIvf4dXXjpfZpRrM3gKvgy/uOLq3GcNr9UOmy5aLN X-Google-Smtp-Source: AGHT+IGbD1Zl91/g/+st4aTTgt5Bkror5XgxZ0mOSUBOJaMyTd1yJdlz0KXAlGwPjFYFs62/1RdnzA== X-Received: by 2002:a05:6a00:2e05:b0:732:2484:e0ce with SMTP id d2e1a72fcca58-7403a811cadmr2252321b3a.17.1746000996180; Wed, 30 Apr 2025 01:16:36 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74039a309edsm1073084b3a.91.2025.04.30.01.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Apr 2025 01:16:35 -0700 (PDT) From: Atish Patra Date: Wed, 30 Apr 2025 01:16:29 -0700 Subject: [PATCH v3 2/3] KVM: riscv: selftests: Decode stval to identify exact exception type Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250430-kvm_selftest_improve-v3-2-eea270ff080b@rivosinc.com> References: <20250430-kvm_selftest_improve-v3-0-eea270ff080b@rivosinc.com> In-Reply-To: <20250430-kvm_selftest_improve-v3-0-eea270ff080b@rivosinc.com> To: Anup Patel , Atish Patra , Paolo Bonzini , Shuah Khan , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , Andrew Jones Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-42535 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 Reviewed-by: Andrew Jones Signed-off-by: Atish Patra --- .../testing/selftests/kvm/include/riscv/processor.h | 13 +++++++++++++ tools/testing/selftests/kvm/riscv/sbi_pmu_test.c | 20 ++++++++++++++++= +++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/riscv/processor.h b/tools/= testing/selftests/kvm/include/riscv/processor.h index 1b5aef87de0f..162f303d9daa 100644 --- a/tools/testing/selftests/kvm/include/riscv/processor.h +++ b/tools/testing/selftests/kvm/include/riscv/processor.h @@ -11,6 +11,19 @@ #include #include "kvm_util.h" =20 +#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_SHIF= T) + static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype, uint64_t idx, uint64_t size) { diff --git a/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c b/tools/testi= ng/selftests/kvm/riscv/sbi_pmu_test.c index 6e66833e5941..924a335d2262 100644 --- a/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +++ b/tools/testing/selftests/kvm/riscv/sbi_pmu_test.c @@ -73,7 +73,6 @@ unsigned long pmu_csr_read_num(int csr_num) =20 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, unsign= ed long stop_flags) =20 static void guest_illegal_exception_handler(struct pt_regs *regs) { + unsigned long insn; + int opcode, csr_num, funct3; + __GUEST_ASSERT(regs->cause =3D=3D EXC_INST_ILLEGAL, "Unexpected exception handler %lx\n", regs->cause); =20 + insn =3D regs->badaddr; + opcode =3D (insn & INSN_OPCODE_MASK) >> INSN_OPCODE_SHIFT; + __GUEST_ASSERT(opcode =3D=3D INSN_OPCODE_SYSTEM, + "Unexpected instruction with opcode 0x%x insn 0x%lx\n", opcode, i= nsn); + + csr_num =3D GET_CSR_NUM(insn); + funct3 =3D GET_RM(insn); + /* Validate if it is a CSR read/write operation */ + __GUEST_ASSERT(funct3 <=3D 7 && (funct3 !=3D 0 && funct3 !=3D 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 >=3D CSR_CYCLE && csr_num <=3D CSR_HPMCOUNTER31), + "Unexpected csr_num 0x%x\n", csr_num); + illegal_handler_invoked =3D true; /* skip the trapping instruction */ regs->epc +=3D 4; --=20 2.43.0