From nobody Mon Nov 25 08:52:44 2024 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=1716999117; cv=none; d=zohomail.com; s=zohoarc; b=fx+ynNfuPHcewRP+xfQdtzv4DibduzctFCEZcQJqcMbTJiBlYri4kO7vCafNQgBQ9vAwc3vPMj/Iut6GC7lQlvEEQ0CjB0uvu0b1KZIwfG5yFINzU/zAcD+/CheA3AE5CbA1LmE5b/I+McnU2yMoE9XQz9GGkwXj3lfT4YdsZl0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999117; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=StS3+7nlVIf5oE8cBUohIBpboOuZOuETS8f794xWjaU=; b=bPnM/J9zFi5rVBisxqE/kH0e1PhZN3k6vAzn4YeWMI5HfbCE8r4CWad7RpTqD1eDneeieWB/DOLovZOOFXE83PDQp0dCnSxJ2M6ZaC/sq2JzKv5EOlyT+eYYFj6jZDHpYxEyfxsajk5sdP2mAV8Tf0/Egq5OGig6+O8jUEVB5/g= 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 1716999117130599.4954000508425; Wed, 29 May 2024 09:11:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsV-0000w6-00; Wed, 29 May 2024 12:10:16 -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 1sCLsR-0000sr-H9 for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:11 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsE-0003MR-1t for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:10 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-42121d28664so11275515e9.2 for ; Wed, 29 May 2024 09:09:57 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998996; x=1717603796; darn=nongnu.org; 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=StS3+7nlVIf5oE8cBUohIBpboOuZOuETS8f794xWjaU=; b=NBgFyaiYJ0W823dafgsxAAgnFZaXKxe7T9kp7huvafDYpiY9t9YKdOMLhv+Woo9omj jmYKDW7zAk4TLzT7FbBCmc9IjQNdDN2si/Kkfl56aaIF7gihMT7SgDcSbX/JhxToIIiC yuhggB1Cv0Va9IqqBTMZav2j7ZJQRV8h+f84WpbYvR9TNqS7eXjkIBVohgieQvC1WWIC n/mUjm3IsTErqNRGs53nNgJuE4STbeuhmPthcTBSlanQWkfTJKSurGWf/Gu6FJQUQOne EodYkTznGiMXA6Gt6XlcUkITCNdnRV2xUctVJXOhKlFQ70hGeqG+WwTa/ucdqCggznWV Q83Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998996; x=1717603796; 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=StS3+7nlVIf5oE8cBUohIBpboOuZOuETS8f794xWjaU=; b=l1qHQXscat8v0/UlhYnFYy0/thlMucnQENXvYY/6xe+6q/V7bALsbxu6v54RAXjt9C eF2R/LujM1EaoKVWKEsiPNWvsNUxrq8PvrMu9SyTCCT8vPmTMsBy+ERqar5UxNY+9Azf j4E5unweo6oPjlbrl+H7bjlvz9UMox6rlpjPlrWi2QqZMsLo2SmACqEv8kpmyFqQFWRM IlTs5ILJlm9se9aN9vBW4SHWoI9fyCy5DYPZJJW33bnK8OshuW3DWbOCRDKTBYRk5vCq VNSRkB1EULahIu/ML8WgT7kRRDElYTKi8RefWxveJ7twQIwN+e4QPdvCT/aXItfMVIQC 1j9Q== X-Forwarded-Encrypted: i=1; AJvYcCUIY5+3pXf+ZoxOGgD3jcGU2Um5ZCmyDgpewDm95yHPdghYRvsPb+JKtn/uG0mlVngZCiLwbvcRTnJ1AgtPKnS6MCmmXdE= X-Gm-Message-State: AOJu0YzW0aWnbYN0IpLtjZ5DcFGxszsJA9sqCDsp8VYLiw6G6b1+7sap 42H5K+1TPxuV0D026gSoF4EkAkN7YgrgkMo0QJ0EnXhX4eGaM9Nnci6kav1GmI4= X-Google-Smtp-Source: AGHT+IGCJ/NYOxMvS1YDuREfbFCQfN3xOX+rh0kdeKF2fE/hy7YqfCJIFYcwrThYPSPNJql3gkGoRw== X-Received: by 2002:a05:600c:364f:b0:420:1094:65d with SMTP id 5b1f17b1804b1-421089f4dd2mr120738055e9.12.1716998996240; Wed, 29 May 2024 09:09:56 -0700 (PDT) From: Rajnesh Kanwal To: 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, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 3/6] target/riscv: Add support for Control Transfer Records extension CSRs. Date: Wed, 29 May 2024 17:09:47 +0100 Message-Id: <20240529160950.132754-4-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.com> 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=2a00:1450:4864:20::335; envelope-from=rkanwal@rivosinc.com; helo=mail-wm1-x335.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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-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.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999118655100029 Content-Type: text/plain; charset="utf-8" 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 */ =20 + 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 *e= nv, int csrno) return RISCV_EXCP_ILLEGAL_INST; } =20 +/* + * 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 =3D=3D PRV_M && riscv_cpu_cfg(env)->ext_smctr) || + (env->priv =3D=3D PRV_S && !env->virt_enabled && + riscv_cpu_cfg(env)->ext_ssctr)) { + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); + } + + if (env->priv =3D=3D PRV_S && env->virt_enabled && + riscv_cpu_cfg(env)->ext_ssctr) { + if (csrno =3D=3D CSR_SCTRSTATUS) { + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); + } + + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; + } + + return RISCV_EXCP_ILLEGAL_INST; +} + +static RISCVException ctr_vsmode(CPURISCVState *env, int csrno) +{ + if (env->priv =3D=3D PRV_S && env->virt_enabled && + riscv_cpu_cfg(env)->ext_ssctr) { + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); + } + + 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; } =20 +static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_= mask) +{ + uint64_t mask =3D wr_mask & SCTRDEPTH_MASK; + + if (ret_val) { + *ret_val =3D env->sctrdepth & SCTRDEPTH_MASK; + } + + env->sctrdepth =3D (env->sctrdepth & ~mask) | (new_val & mask); + + /* Correct depth. */ + if (wr_mask & SCTRDEPTH_MASK) { + uint64_t depth =3D get_field(env->sctrdepth, SCTRDEPTH_MASK); + + if (depth > SCTRDEPTH_MAX) { + env->sctrdepth =3D + set_field(env->sctrdepth, SCTRDEPTH_MASK, SCTRDEPTH_MAX); + } + + /* Update sctrstatus.WRPTR with a legal value */ + depth =3D 16 << depth; + env->sctrstatus =3D + 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 =3D wr_mask & MCTRCTL_MASK; + + if (ret_val) { + *ret_val =3D env->mctrctl & MCTRCTL_MASK; + } + + env->mctrctl =3D (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 =3D wr_mask & SCTRCTL_MASK; + RISCVException ret; + + ret =3D rmw_mctrctl(env, csrno, ret_val, new_val, mask); + if (ret_val) { + *ret_val &=3D 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 =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); + uint32_t mask =3D wr_mask & SCTRSTATUS_MASK; + + if (ret_val) { + *ret_val =3D env->sctrstatus & SCTRSTATUS_MASK; + } + + env->sctrstatus =3D (env->sctrstatus & ~mask) | (new_val & mask); + + /* Update sctrstatus.WRPTR with a legal value */ + env->sctrstatus =3D 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 =3D wr_mask & VSCTRCTL_MASK; + + if (ret_val) { + *ret_val =3D env->vsctrctl & VSCTRCTL_MASK; + } + + env->vsctrctl =3D (env->vsctrctl & ~mask) | (new_val & mask); + + return RISCV_EXCP_NONE; +} + static RISCVException read_vstopi(CPURISCVState *env, int csrno, target_ulong *val) { @@ -5771,6 +5920,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_SPMBASE] =3D { "spmbase", pointer_masking, read_spmbase, write_spmbase = }, =20 + [CSR_MCTRCTL] =3D { "mctrctl", ctr_mmode, NULL, NULL, + rmw_mctrctl }, + [CSR_SCTRCTL] =3D { "sctrctl", ctr_smode, NULL, NULL, + rmw_sctrctl }, + [CSR_SCTRDEPTH] =3D { "sctrdepth", ctr_smode, NULL, NULL, + rmw_sctrdepth }, + [CSR_SCTRSTATUS] =3D { "sctrstatus", ctr_smode, NULL, NULL, + rmw_sctrstatus }, + [CSR_VSCTRCTL] =3D { "vsctrctl", ctr_vsmode, NULL, NULL, + rmw_vsctrctl }, /* Performance Counters */ [CSR_HPMCOUNTER3] =3D { "hpmcounter3", ctr, read_hpmcounter }, [CSR_HPMCOUNTER4] =3D { "hpmcounter4", ctr, read_hpmcounter }, --=20 2.34.1