Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7D431C25B7E for ; Tue, 4 Jun 2024 10:15:19 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sERBb-0006fA-C2; Tue, 04 Jun 2024 06:14:35 -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 1sERBZ-0006dB-0b for qemu-riscv@nongnu.org; Tue, 04 Jun 2024 06:14:33 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sERBW-0000ko-9R for qemu-riscv@nongnu.org; Tue, 04 Jun 2024 06:14:32 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1f48b825d8cso7514975ad.2 for ; Tue, 04 Jun 2024 03:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1717496068; x=1718100868; darn=nongnu.org; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ohH2CE4Aa+EFMZj4nIK/fCdlHTNN4SZO7xcBNi8AmYw=; b=go0VDM3U3hkgpmwvQY/fa9T2epmMX+IpwvQTREkavQdBFeKkkYKoxN5G/rCrs1bHWO MpTkAUeePTsPj/215W7I/U70JM26b/A9qvXiSEwhGaAoRqNTBVnZh2Et543Lq5t+Kl4L +KPoNZG7OKIudl29kRcV1I8tyrB/FejpMmx2t2XFSgn5m/0Aptxrz6alRuATP0K1eOwN ZcLUyi7+sfq4MXRZUkhMXo+t1m3gNspKhL5QYqRT1Emk5RQPD6hI++BFyPvgSyCSDQXi tNeEwqim60LE7/KLNbo0JLsvv0kX+9sPBuBsQXToHIXgRexk/1SMr48PL3x4NdiuxpQU OilA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717496068; x=1718100868; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ohH2CE4Aa+EFMZj4nIK/fCdlHTNN4SZO7xcBNi8AmYw=; b=RctrK4d+X0DSNS0r0u9FPsqbw58xHi3P6GeXUmTqf+ILzz1YMvPaO1jFss6MbSS+jf CZvizlJ4zohfZJXgoCaWYRJQJXWLUmBBgDSpUddpHjtUgODh52HNzHcvgj4mt8vEqSVN R4+g7HXMcU2s4+GYa3k+8Lae8Nb7ujFAbU9H4iZ5HkTl1bzkc/lpJOZAi0Qza/QBGMCT tFiYKfGPSvHlRo5n1ZqT23oilAzbGuIklq8608rIln87s2JLG5jpgSkIrO/9FRp2qZ85 GRVQKKmm+qXP0DxPcfbGLBEMRYXa+2LExyx8D1j2ZcDljWH/bAq5S0czNblFhu/4KdxZ q2lw== X-Forwarded-Encrypted: i=1; AJvYcCU8U+VLugJPojotAZ6/k2+rNtoVVAXnsqEjZgO4imVafMBeVyVFP2XVk0qOR8L80IUQt/hbFdrY7GXjAb7LkcSEi1wrzVs= X-Gm-Message-State: AOJu0YzLSRw+B3SV3MlDOu46wFNDYLKcBuiQ8I+WQh8Qp0tL8DVm/q8S bGfyo1CF1TfeqtyziFXpdEpHY3Lf6MYnvgvH70oHd710b2xzkzxj4KqrQL42RN8= X-Google-Smtp-Source: AGHT+IHkMwqbboo7QRnJVps70A/GXpg/wteA6CDX26+Fl/ALXcedb5KUXm7FDELvuPBmRBhbe4sUew== X-Received: by 2002:a17:902:c94c:b0:1f6:5a50:93b3 with SMTP id d9443c01a7336-1f65a509712mr97772515ad.43.1717496068239; Tue, 04 Jun 2024 03:14:28 -0700 (PDT) Received: from [192.168.100.252] (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f63241c392sm79119095ad.291.2024.06.04.03.14.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Jun 2024 03:14:27 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------HdwHUJx5fdBvZT0cld0x6xoB" Message-ID: <3f3cc0f1-0a56-452a-a934-b3f770056570@sifive.com> Date: Tue, 4 Jun 2024 18:14:24 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 3/6] target/riscv: Add support for Control Transfer Records extension CSRs. To: Rajnesh Kanwal , qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org References: <20240529160950.132754-1-rkanwal@rivosinc.com> <20240529160950.132754-4-rkanwal@rivosinc.com> Content-Language: en-US From: Jason Chien In-Reply-To: <20240529160950.132754-4-rkanwal@rivosinc.com> Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=jason.chien@sifive.com; helo=mail-pl1-x62e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-riscv@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-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org Sender: qemu-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org This is a multi-part message in MIME format. --------------HdwHUJx5fdBvZT0cld0x6xoB Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Rajnesh Kanwal 於 2024/5/30 上午 12:09 寫道: > This commit adds support for [m|s|vs]ctrcontrol, sctrstatus and > sctrdepth CSRs handling. > > Signed-off-by: Rajnesh Kanwal > --- > target/riscv/cpu.h | 5 ++ > target/riscv/cpu_cfg.h | 2 + > target/riscv/csr.c | 159 +++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 166 insertions(+) > > diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h > index a185e2d494..3d4d5172b8 100644 > --- a/target/riscv/cpu.h > +++ b/target/riscv/cpu.h > @@ -263,6 +263,11 @@ struct CPUArchState { > target_ulong mcause; > target_ulong mtval; /* since: priv-1.10.0 */ > > + uint64_t mctrctl; > + uint32_t sctrdepth; > + uint32_t sctrstatus; > + uint64_t vsctrctl; > + > /* Machine and Supervisor interrupt priorities */ > uint8_t miprio[64]; > uint8_t siprio[64]; > diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h > index d9354dc80a..d329a65811 100644 > --- a/target/riscv/cpu_cfg.h > +++ b/target/riscv/cpu_cfg.h > @@ -123,6 +123,8 @@ struct RISCVCPUConfig { > bool ext_zvfhmin; > bool ext_smaia; > bool ext_ssaia; > + bool ext_smctr; > + bool ext_ssctr; > bool ext_sscofpmf; > bool ext_smepmp; > bool rvv_ta_all_1s; > diff --git a/target/riscv/csr.c b/target/riscv/csr.c > index 2f92e4b717..888084d8e5 100644 > --- a/target/riscv/csr.c > +++ b/target/riscv/csr.c > @@ -621,6 +621,61 @@ static RISCVException pointer_masking(CPURISCVState *env, int csrno) > return RISCV_EXCP_ILLEGAL_INST; > } > > +/* > + * M-mode: > + * Without ext_smctr raise illegal inst excep. > + * Otherwise everything is accessible to m-mode. > + * > + * S-mode: > + * Without ext_ssctr or mstateen.ctr raise illegal inst excep. > + * Otherwise everything other than mctrctl is accessible. > + * > + * VS-mode: > + * Without ext_ssctr or mstateen.ctr raise illegal inst excep. > + * Without hstateen.ctr raise virtual illegal inst excep. > + * Otherwise allow vsctrctl, sctrstatus, 0x200-0x2ff entry range. > + * Always raise illegal instruction exception for sctrdepth. > + */ > +static RISCVException ctr_mmode(CPURISCVState *env, int csrno) > +{ > + /* Check if smctr-ext is present */ > + if (riscv_cpu_cfg(env)->ext_smctr) { > + return RISCV_EXCP_NONE; > + } > + > + return RISCV_EXCP_ILLEGAL_INST; > +} > + > +static RISCVException ctr_smode(CPURISCVState *env, int csrno) > +{ > + if ((env->priv == PRV_M && riscv_cpu_cfg(env)->ext_smctr) || > + (env->priv == PRV_S && !env->virt_enabled && > + riscv_cpu_cfg(env)->ext_ssctr)) { > + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); > + } > + > + if (env->priv == PRV_S && env->virt_enabled && > + riscv_cpu_cfg(env)->ext_ssctr) { > + if (csrno == CSR_SCTRSTATUS) { missing sctrctl? > + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); > + } > + > + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; > + } > + > + return RISCV_EXCP_ILLEGAL_INST; > +} I think there is no need to bind M-mode with ext_smctr, S-mode with ext_ssctr and VS-mode with ext_ssctr, since this predicate function is for S-mode CSRs, which are defined in both smctr and ssctr, we just need to check at least one of ext_ssctr or ext_smctr is true. The spec states that: Attempts to access sctrdepth from VS-mode or VU-mode raise a virtual-instruction exception, unless CTR state enable access restrictions apply. In my understanding, we should check the presence of smstateen extension first, and if smstateen is implemented: * for sctrctl and sctrstatus, call smstateen_acc_ok() * for sctrdepth, call smstateen_acc_ok(), and if there is any exception returned, always report virtual-instruction exception. If smstateen is not implemented: * for sctrctl and sctrstatus, there is no check. * for sctrdepth, I think the spec is ambiguous. What does "CTR state enable access restrictions apply" mean when smstateen is not implemented? Here is the code to better understand my description. static RISCVException ctr_smode(CPURISCVState *env, int csrno) {     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);     if (!cfg->ext_ssctr && !cfg->ext_smctr) {         return RISCV_EXCP_ILLEGAL_INST;     }     if (riscv_cpu_cfg(env)->ext_smstateen) {         RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);         if (ret != RISCV_EXCP_NONE) {             if (csrno == CSR_SCTRDEPTH && env->virt_enabled) {                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;             }             return ret;         }     } else {         /* The spec is ambiguous. */         if (csrno == CSR_SCTRDEPTH && env->virt_enabled) {             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;         }     }     return RISCV_EXCP_NONE; } > + > +static RISCVException ctr_vsmode(CPURISCVState *env, int csrno) > +{ > + if (env->priv == PRV_S && env->virt_enabled && > + riscv_cpu_cfg(env)->ext_ssctr) { > + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); In riscv_csrrw_check(), an virtual-instruction exception is always reported no matter what. Do we need this check? > + } > + > + return ctr_smode(env, csrno); > +} > + > static RISCVException aia_hmode(CPURISCVState *env, int csrno) > { > int ret; > @@ -3835,6 +3890,100 @@ static RISCVException write_satp(CPURISCVState *env, int csrno, > return RISCV_EXCP_NONE; > } > > +static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, > + target_ulong *ret_val, > + target_ulong new_val, target_ulong wr_mask) > +{ > + uint64_t mask = wr_mask & SCTRDEPTH_MASK; > + > + if (ret_val) { > + *ret_val = env->sctrdepth & SCTRDEPTH_MASK; We don't need to do bitwise and with SCTRDEPTH_MASK on read accesses when we always do bitwise and with SCTRDEPTH_MASK on write accesses. > + } > + > + env->sctrdepth = (env->sctrdepth & ~mask) | (new_val & mask); > + > + /* Correct depth. */ > + if (wr_mask & SCTRDEPTH_MASK) { > + uint64_t depth = get_field(env->sctrdepth, SCTRDEPTH_MASK); > + > + if (depth > SCTRDEPTH_MAX) { > + env->sctrdepth = > + set_field(env->sctrdepth, SCTRDEPTH_MASK, SCTRDEPTH_MAX); > + } > + > + /* Update sctrstatus.WRPTR with a legal value */ > + depth = 16 << depth; The "depth" on the right side may exceed SCTRDEPTH_MAX. > + env->sctrstatus = > + env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1)); > + } > + > + return RISCV_EXCP_NONE; > +} > + > +static RISCVException rmw_mctrctl(CPURISCVState *env, int csrno, > + target_ulong *ret_val, > + target_ulong new_val, target_ulong wr_mask) > +{ > + uint64_t mask = wr_mask & MCTRCTL_MASK; > + > + if (ret_val) { > + *ret_val = env->mctrctl & MCTRCTL_MASK; There is no need to do bitwise and with the mask on read accesses when we always do bitwise and with the mask on write accesses. > + } > + > + env->mctrctl = (env->mctrctl & ~mask) | (new_val & mask); > + > + return RISCV_EXCP_NONE; > +} > + > +static RISCVException rmw_sctrctl(CPURISCVState *env, int csrno, > + target_ulong *ret_val, > + target_ulong new_val, target_ulong wr_mask) > +{ > + uint64_t mask = wr_mask & SCTRCTL_MASK; > + RISCVException ret; > + > + ret = rmw_mctrctl(env, csrno, ret_val, new_val, mask); When V=1, vsctrctl substitutes for sctrctl. > + if (ret_val) { > + *ret_val &= SCTRCTL_MASK; > + } > + > + return ret; > +} > + > +static RISCVException rmw_sctrstatus(CPURISCVState *env, int csrno, > + target_ulong *ret_val, > + target_ulong new_val, target_ulong wr_mask) > +{ > + uint32_t depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); > + uint32_t mask = wr_mask & SCTRSTATUS_MASK; > + > + if (ret_val) { > + *ret_val = env->sctrstatus & SCTRSTATUS_MASK; There is no need to do bitwise and with the mask on read accesses when we always do bitwise and with the mask on write accesses. > + } > + > + env->sctrstatus = (env->sctrstatus & ~mask) | (new_val & mask); > + > + /* Update sctrstatus.WRPTR with a legal value */ > + env->sctrstatus = env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1)); > + > + return RISCV_EXCP_NONE; > +} > + > +static RISCVException rmw_vsctrctl(CPURISCVState *env, int csrno, > + target_ulong *ret_val, > + target_ulong new_val, target_ulong wr_mask) > +{ > + uint64_t mask = wr_mask & VSCTRCTL_MASK; > + > + if (ret_val) { > + *ret_val = env->vsctrctl & VSCTRCTL_MASK; There is no need to do bitwise and with the mask on read accesses when we always do bitwise and with the mask on write accesses. > + } > + > + env->vsctrctl = (env->vsctrctl & ~mask) | (new_val & mask); > + > + return RISCV_EXCP_NONE; > +} Is it possible to define rmw_xctrctl() instead of three individual rmw functions and use a switch case to select the mask and the CSR for the purpose of reducing code size? > + > static RISCVException read_vstopi(CPURISCVState *env, int csrno, > target_ulong *val) > { > @@ -5771,6 +5920,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { > [CSR_SPMBASE] = { "spmbase", pointer_masking, read_spmbase, > write_spmbase }, > > + [CSR_MCTRCTL] = { "mctrctl", ctr_mmode, NULL, NULL, > + rmw_mctrctl }, I think this can be one line. > + [CSR_SCTRCTL] = { "sctrctl", ctr_smode, NULL, NULL, > + rmw_sctrctl }, same here > + [CSR_SCTRDEPTH] = { "sctrdepth", ctr_smode, NULL, NULL, > + rmw_sctrdepth }, same here > + [CSR_SCTRSTATUS] = { "sctrstatus", ctr_smode, NULL, NULL, > + rmw_sctrstatus }, same here > + [CSR_VSCTRCTL] = { "vsctrctl", ctr_vsmode, NULL, NULL, > + rmw_vsctrctl }, same here > /* Performance Counters */ > [CSR_HPMCOUNTER3] = { "hpmcounter3", ctr, read_hpmcounter }, > [CSR_HPMCOUNTER4] = { "hpmcounter4", ctr, read_hpmcounter }, --------------HdwHUJx5fdBvZT0cld0x6xoB Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


Rajnesh Kanwal 於 2024/5/30 上午 12:09 寫道:
This commit adds support for [m|s|vs]ctrcontrol, sctrstatus and
sctrdepth CSRs handling.

Signed-off-by: Rajnesh Kanwal <rkanwal@rivosinc.com>
---
 target/riscv/cpu.h     |   5 ++
 target/riscv/cpu_cfg.h |   2 +
 target/riscv/csr.c     | 159 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+)

diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
index a185e2d494..3d4d5172b8 100644
--- a/target/riscv/cpu.h
+++ b/target/riscv/cpu.h
@@ -263,6 +263,11 @@ struct CPUArchState {
     target_ulong mcause;
     target_ulong mtval;  /* since: priv-1.10.0 */
 
+    uint64_t mctrctl;
+    uint32_t sctrdepth;
+    uint32_t sctrstatus;
+    uint64_t vsctrctl;
+
     /* Machine and Supervisor interrupt priorities */
     uint8_t miprio[64];
     uint8_t siprio[64];
diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h
index d9354dc80a..d329a65811 100644
--- a/target/riscv/cpu_cfg.h
+++ b/target/riscv/cpu_cfg.h
@@ -123,6 +123,8 @@ struct RISCVCPUConfig {
     bool ext_zvfhmin;
     bool ext_smaia;
     bool ext_ssaia;
+    bool ext_smctr;
+    bool ext_ssctr;
     bool ext_sscofpmf;
     bool ext_smepmp;
     bool rvv_ta_all_1s;
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index 2f92e4b717..888084d8e5 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -621,6 +621,61 @@ static RISCVException pointer_masking(CPURISCVState *env, int csrno)
     return RISCV_EXCP_ILLEGAL_INST;
 }
 
+/*
+ * M-mode:
+ * Without ext_smctr raise illegal inst excep.
+ * Otherwise everything is accessible to m-mode.
+ *
+ * S-mode:
+ * Without ext_ssctr or mstateen.ctr raise illegal inst excep.
+ * Otherwise everything other than mctrctl is accessible.
+ *
+ * VS-mode:
+ * Without ext_ssctr or mstateen.ctr raise illegal inst excep.
+ * Without hstateen.ctr raise virtual illegal inst excep.
+ * Otherwise allow vsctrctl, sctrstatus, 0x200-0x2ff entry range.
+ * Always raise illegal instruction exception for sctrdepth.
+ */
+static RISCVException ctr_mmode(CPURISCVState *env, int csrno)
+{
+    /* Check if smctr-ext is present */
+    if (riscv_cpu_cfg(env)->ext_smctr) {
+        return RISCV_EXCP_NONE;
+    }
+
+    return RISCV_EXCP_ILLEGAL_INST;
+}
+
+static RISCVException ctr_smode(CPURISCVState *env, int csrno)
+{
+    if ((env->priv == PRV_M && riscv_cpu_cfg(env)->ext_smctr) ||
+        (env->priv == PRV_S && !env->virt_enabled &&
+         riscv_cpu_cfg(env)->ext_ssctr)) {
+        return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);
+    }
+
+    if (env->priv == PRV_S && env->virt_enabled &&
+        riscv_cpu_cfg(env)->ext_ssctr) {
+        if (csrno == CSR_SCTRSTATUS) {
missing sctrctl?
+            return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);
+        }
+
+        return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
+    }
+
+    return RISCV_EXCP_ILLEGAL_INST;
+}

I think there is no need to bind M-mode with ext_smctr, S-mode with ext_ssctr and VS-mode with ext_ssctr, since this predicate function is for S-mode CSRs, which are defined in both smctr and ssctr, we just need to check at least one of ext_ssctr or ext_smctr is true.

The spec states that:
Attempts to access sctrdepth from VS-mode or VU-mode raise a virtual-instruction exception, unless CTR state enable access restrictions apply.

In my understanding, we should check the presence of smstateen extension first, and

if smstateen is implemented:

  • for sctrctl and sctrstatus, call smstateen_acc_ok()
  • for sctrdepth, call smstateen_acc_ok(), and if there is any exception returned, always report virtual-instruction exception.

If smstateen is not implemented:

  • for sctrctl and sctrstatus, there is no check.
  • for sctrdepth, I think the spec is ambiguous. What does "CTR state enable access restrictions apply" mean when smstateen is not implemented?

Here is the code to better understand my description.

static RISCVException ctr_smode(CPURISCVState *env, int csrno)
{
    const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);

    if (!cfg->ext_ssctr && !cfg->ext_smctr) {
        return RISCV_EXCP_ILLEGAL_INST;
    }

    if (riscv_cpu_cfg(env)->ext_smstateen) {
        RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);
        if (ret != RISCV_EXCP_NONE) {
            if (csrno == CSR_SCTRDEPTH && env->virt_enabled) {
                return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
            }

            return ret;
        }
    } else {
        /* The spec is ambiguous. */
        if (csrno == CSR_SCTRDEPTH && env->virt_enabled) {
            return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
        }
    }

    return RISCV_EXCP_NONE;
}

+
+static RISCVException ctr_vsmode(CPURISCVState *env, int csrno)
+{
+    if (env->priv == PRV_S && env->virt_enabled &&
+        riscv_cpu_cfg(env)->ext_ssctr) {
+        return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);
In riscv_csrrw_check(), an virtual-instruction exception is always reported no matter what. Do we need this check?
+    }
+
+    return ctr_smode(env, csrno);
+}
+
 static RISCVException aia_hmode(CPURISCVState *env, int csrno)
 {
     int ret;
@@ -3835,6 +3890,100 @@ static RISCVException write_satp(CPURISCVState *env, int csrno,
     return RISCV_EXCP_NONE;
 }
 
+static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno,
+                                    target_ulong *ret_val,
+                                    target_ulong new_val, target_ulong wr_mask)
+{
+    uint64_t mask = wr_mask & SCTRDEPTH_MASK;
+
+    if (ret_val) {
+        *ret_val = env->sctrdepth & SCTRDEPTH_MASK;
We don't need to do bitwise and with SCTRDEPTH_MASK on read accesses when we always do bitwise and with SCTRDEPTH_MASK on write accesses.
+    }
+
+    env->sctrdepth = (env->sctrdepth & ~mask) | (new_val & mask);
+
+    /* Correct depth. */
+    if (wr_mask & SCTRDEPTH_MASK) {
+        uint64_t depth = get_field(env->sctrdepth, SCTRDEPTH_MASK);
+
+        if (depth > SCTRDEPTH_MAX) {
+            env->sctrdepth =
+                set_field(env->sctrdepth, SCTRDEPTH_MASK, SCTRDEPTH_MAX);
+        }
+
+        /* Update sctrstatus.WRPTR with a legal value */
+        depth = 16 << depth;
The "depth" on the right side may exceed SCTRDEPTH_MAX.
+        env->sctrstatus =
+            env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
+    }
+
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException rmw_mctrctl(CPURISCVState *env, int csrno,
+                                    target_ulong *ret_val,
+                                    target_ulong new_val, target_ulong wr_mask)
+{
+    uint64_t mask = wr_mask & MCTRCTL_MASK;
+
+    if (ret_val) {
+        *ret_val = env->mctrctl & MCTRCTL_MASK;
There is no need to do bitwise and with the mask on read accesses when we always do bitwise and with the mask on write accesses.
+    }
+
+    env->mctrctl = (env->mctrctl & ~mask) | (new_val & mask);
+
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException rmw_sctrctl(CPURISCVState *env, int csrno,
+                                    target_ulong *ret_val,
+                                    target_ulong new_val, target_ulong wr_mask)
+{
+    uint64_t mask = wr_mask & SCTRCTL_MASK;
+    RISCVException ret;
+
+    ret = rmw_mctrctl(env, csrno, ret_val, new_val, mask);
When V=1, vsctrctl substitutes for sctrctl.
+    if (ret_val) {
+        *ret_val &= SCTRCTL_MASK;
+    }
+
+    return ret;
+}
+
+static RISCVException rmw_sctrstatus(CPURISCVState *env, int csrno,
+                                     target_ulong *ret_val,
+                                     target_ulong new_val, target_ulong wr_mask)
+{
+    uint32_t depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK);
+    uint32_t mask = wr_mask & SCTRSTATUS_MASK;
+
+    if (ret_val) {
+        *ret_val = env->sctrstatus & SCTRSTATUS_MASK;
There is no need to do bitwise and with the mask on read accesses when we always do bitwise and with the mask on write accesses.
+    }
+
+    env->sctrstatus = (env->sctrstatus & ~mask) | (new_val & mask);
+
+    /* Update sctrstatus.WRPTR with a legal value */
+    env->sctrstatus = env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1));
+
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException rmw_vsctrctl(CPURISCVState *env, int csrno,
+                                    target_ulong *ret_val,
+                                    target_ulong new_val, target_ulong wr_mask)
+{
+    uint64_t mask = wr_mask & VSCTRCTL_MASK;
+
+    if (ret_val) {
+        *ret_val = env->vsctrctl & VSCTRCTL_MASK;
There is no need to do bitwise and with the mask on read accesses when we always do bitwise and with the mask on write accesses.
+    }
+
+    env->vsctrctl = (env->vsctrctl & ~mask) | (new_val & mask);
+
+    return RISCV_EXCP_NONE;
+}
Is it possible to define rmw_xctrctl() instead of three individual rmw functions and use a switch case to select the mask and the CSR for the purpose of reducing code size?
+
 static RISCVException read_vstopi(CPURISCVState *env, int csrno,
                                   target_ulong *val)
 {
@@ -5771,6 +5920,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
     [CSR_SPMBASE] =    { "spmbase", pointer_masking, read_spmbase,
                          write_spmbase                                      },
 
+    [CSR_MCTRCTL]       = { "mctrctl",       ctr_mmode, NULL, NULL,
+                                rmw_mctrctl },
I think this can be one line.
+    [CSR_SCTRCTL]       = { "sctrctl",       ctr_smode, NULL, NULL,
+                                rmw_sctrctl },
same here
+    [CSR_SCTRDEPTH]       = { "sctrdepth",       ctr_smode, NULL, NULL,
+                                rmw_sctrdepth },
same here
+    [CSR_SCTRSTATUS]       = { "sctrstatus",       ctr_smode, NULL, NULL,
+                                rmw_sctrstatus },
same here
+    [CSR_VSCTRCTL]      = { "vsctrctl",      ctr_vsmode, NULL, NULL,
+                                rmw_vsctrctl },
same here
     /* Performance Counters */
     [CSR_HPMCOUNTER3]    = { "hpmcounter3",    ctr,    read_hpmcounter },
     [CSR_HPMCOUNTER4]    = { "hpmcounter4",    ctr,    read_hpmcounter },
--------------HdwHUJx5fdBvZT0cld0x6xoB--