From nobody Mon Feb 9 22:41:14 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1689729833; cv=none; d=zohomail.com; s=zohoarc; b=kMKXUYCGdRpyID40UzRAvdkLglDCzwYllkUmbxRJqgjgtrIJtD8Qq6awPV/ui8t7XIZu3P1Ck3XKVsuxISKiripg+QU7cZh4cCF1uuEDI5/277JAhW2aeaad6kX79HZTBPow1/EZwHQcMz+C96oOEc3UfBQ8cCC75QCnNaL9NwA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1689729833; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/5KRXSS3DUc7aMWMTPL8fRz80+vQ74aVXKA6ZvPBjS4=; b=fL0OfOCpTazFmwuwBdd4Y+1hdXvLoAduz+4lMC+tFdXo5DOrHDcqTRmfUo9zkUzlKR3SOQy/OgqjlfS7kUi2o0IldvGjrDRwR7uKyw1bDAEKIAFxYrEv75hyScw2LfOlRP8skh5DVvsxFF5e6c/mLHDsXDsGoM7R4hjlwB1nJVY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1689729833795182.112252604084; Tue, 18 Jul 2023 18:23:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qLvuS-0004S0-7F; Tue, 18 Jul 2023 21:23:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qLtV7-0003R9-R4 for qemu-devel@nongnu.org; Tue, 18 Jul 2023 18:49:01 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qLtUs-0004yz-DW for qemu-devel@nongnu.org; Tue, 18 Jul 2023 18:49:01 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-666ecf9a081so6258644b3a.2 for ; Tue, 18 Jul 2023 15:48:46 -0700 (PDT) Received: from kaiwenx.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id i14-20020aa787ce000000b00682669dc19bsm1983312pfo.201.2023.07.18.15.48.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 15:48:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1689720524; x=1692312524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/5KRXSS3DUc7aMWMTPL8fRz80+vQ74aVXKA6ZvPBjS4=; b=fALrsQcNnnBKxMa4sbcYmfQd5zTE1NbL+z+UqQ5IJAo4KzrPcimSOuz/tP94VpOOY5 hsoUqZCTHIzad8j4PcnAneX7EyXuJfxdrGoO+cA3n/rFLVgGdcyToHJwtDpRgcopARaR 3fWmu/YpMJuF2lXaxe1zQvXIJWqzTlqKjwDVCds8K+N7JD8onveGD2B7rk6jGEkKBVW2 otVQxZG/nFhp3BdFVNcvQn8B+u7TwJ1GhM2jBr/NjlCxIb6+Ys6p0jr/P1bs5yoGXeGE 5/TN3eqXJ49P4X+Lw0jtfZEtA4hyWmpdXdP2dnKh2J/hhAfEfJGFrfCgIIHcco3qkko0 u5iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689720524; x=1692312524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/5KRXSS3DUc7aMWMTPL8fRz80+vQ74aVXKA6ZvPBjS4=; b=VoPkK0jJ3cSaCPpzYAgOM1FqnpWXnjlYciVH2z1lIXojz2EJCXaFhWPb3EsL6dgTB+ hKbn/fNQ+HPFnyV0UQPVS6kBD0UeviWTDc3zuNffrBOLWlZx92om297HokJdLawJTdRy 1gGv8d5GnRmFkYuiBSsNKLjEEHpyXiERLXamgAIm+OZe3c1rxut13Qa3bQxe8wA7msFZ 1OzFbRaF1tDT5DLsmorX2VO2rCKmk4hzeA7xYK2VlPSAQgof1IDDZz9+E0fxKW8E3Y8Y oTUCNFRxutq2n6g4L/Tw5foHchPOeUF3A8WEW2V2EGseZpU4BE0VTjy2uBtyWd1ohJaq 8Pxg== X-Gm-Message-State: ABy/qLZAmgMLzMzMuIlDadCh9hgSG9Aa9xOJ+XO5ji0MvSLDr+pOCz1I JnXBIfiaCjBDMe5G82IExBadAg== X-Google-Smtp-Source: APBJJlFI85vVQT7ZBtXatmEU3BItJ8q4D64KA1l423YRA3QoGbXKSlzUAHK7rlYUtaMs9k1xnHtZig== X-Received: by 2002:a05:6a21:33a6:b0:12d:10b6:98f9 with SMTP id yy38-20020a056a2133a600b0012d10b698f9mr20088339pzb.56.1689720524305; Tue, 18 Jul 2023 15:48:44 -0700 (PDT) From: Kaiwen Xue To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Kaiwen Xue , Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Atish Kumar Patra , Kaiwen Xue Subject: [PATCH 3/3] target/riscv: Add cycle & instret privilege mode filtering support Date: Tue, 18 Jul 2023 15:47:45 -0700 Message-ID: <3ed71e9812a806b98dd519594af52b9525ec459c.1689631639.git.kaiwenx@rivosinc.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=kaiwenx@rivosinc.com; helo=mail-pf1-x42b.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 18 Jul 2023 21:23:14 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1689729834275100005 Content-Type: text/plain; charset="utf-8" QEMU only calculates dummy cycles and instructions, so there is no actual means to stop the icount in QEMU. Hence this patch merely adds the functionality of accessing the cfg registers, and cause no actual effects on the counting of cycle and instret counters. Signed-off-by: Kaiwen Xue Signed-off-by: Kaiwen Xue --- target/riscv/csr.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index ea7585329e..b1d5e85a79 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -218,6 +218,17 @@ static RISCVException sscofpmf(CPURISCVState *env, int= csrno) return RISCV_EXCP_NONE; } =20 +static RISCVException smcntrpmf(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu =3D env_archcpu(env); + + if (!cpu->cfg.ext_smcntrpmf) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return RISCV_EXCP_NONE; +} + static RISCVException any(CPURISCVState *env, int csrno) { return RISCV_EXCP_NONE; @@ -800,6 +811,54 @@ static int read_hpmcounterh(CPURISCVState *env, int cs= rno, target_ulong *val) =20 #else /* CONFIG_USER_ONLY */ =20 +static int read_mcyclecfg(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val =3D env->mcyclecfg; + return RISCV_EXCP_NONE; +} + +static int write_mcyclecfg(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mcyclecfg =3D val; + return RISCV_EXCP_NONE; +} + +static int read_mcyclecfgh(CPURISCVState *env, int csrno, target_ulong *va= l) +{ + *val =3D env->mcyclecfgh; + return RISCV_EXCP_NONE; +} + +static int write_mcyclecfgh(CPURISCVState *env, int csrno, target_ulong va= l) +{ + env->mcyclecfgh =3D val; + return RISCV_EXCP_NONE; +} + +static int read_minstretcfg(CPURISCVState *env, int csrno, target_ulong *v= al) +{ + *val =3D env->minstretcfg; + return RISCV_EXCP_NONE; +} + +static int write_minstretcfg(CPURISCVState *env, int csrno, target_ulong v= al) +{ + env->minstretcfg =3D val; + return RISCV_EXCP_NONE; +} + +static int read_minstretcfgh(CPURISCVState *env, int csrno, target_ulong *= val) +{ + *val =3D env->minstretcfgh; + return RISCV_EXCP_NONE; +} + +static int write_minstretcfgh(CPURISCVState *env, int csrno, target_ulong = val) +{ + env->minstretcfgh =3D val; + return RISCV_EXCP_NONE; +} + static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong *val) { int evt_index =3D csrno - CSR_MCOUNTINHIBIT; @@ -4506,6 +4565,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { write_mcountinhibit, .min_priv_ver =3D PRIV_VERSION_1_11_0 }, =20 + [CSR_MCYCLECFG] =3D { "mcyclecfg", smcntrpmf, read_mcyclecfg, + write_mcyclecfg, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MINSTRETCFG] =3D { "minstretcfg", smcntrpmf, read_minstretcfg, + write_minstretcfg, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MHPMEVENT3] =3D { "mhpmevent3", any, read_mhpmevent, write_mhpmevent }, [CSR_MHPMEVENT4] =3D { "mhpmevent4", any, read_mhpmevent, @@ -4565,6 +4631,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_MHPMEVENT31] =3D { "mhpmevent31", any, read_mhpmevent, write_mhpmevent }, =20 + [CSR_MCYCLECFGH] =3D { "mcyclecfgh", smcntrpmf, read_mcyclecfgh, + write_mcyclecfgh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_MINSTRETCFGH] =3D { "minstretcfgh", smcntrpmf, read_minstretcfg= h, + write_minstretcfgh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_MHPMEVENT3H] =3D { "mhpmevent3h", sscofpmf, read_mhpmevent= h, write_mhpmeventh, .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, --=20 2.34.1