From nobody Sat Nov 23 21:08:17 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=1731892730; cv=none; d=zohomail.com; s=zohoarc; b=bnMZJWzDQArLca7x6zxCH0Hu0cbMBMIega9WPly/U87mJFXi2cyu/hYJUv865Vu/dmjI5RLWM3SoJiTFFhmvON7v/YsJlW2VBo3jqvNbtyNt2GACdRK0p12ZBasdVv2v81T7sKY/HzYZh620Qu/jQ/aG4PNzWsTxVKakTGbb8Vk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731892730; h=Content-Type: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=UjRl8vMgww0LP/o14M23ZRkREhAdHlsDIuecT3qmbiE=; b=XyzrQ6t2rI/KgtvwZlfHKOBEufSyyjixREM9NsYX8TwlKEJ1olyer69akkRLFbT1baBzGdevoGiPzPENB4Qnn5WcTk0LjfO6QCwMk47fenIYa0ZdbwmO6g1A7GXMYJyNjR9N27cbagq8c0E4f6VMySbKlFOgViEVFGT9MTRjvv8= 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 1731892730647796.1241485813542; Sun, 17 Nov 2024 17:18:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tCqO5-00016L-4J; Sun, 17 Nov 2024 20:17:09 -0500 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 1tCqO0-00012E-2E for qemu-devel@nongnu.org; Sun, 17 Nov 2024 20:17:04 -0500 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tCqNw-0006Pe-0Z for qemu-devel@nongnu.org; Sun, 17 Nov 2024 20:17:03 -0500 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-71e5130832aso952696b3a.0 for ; Sun, 17 Nov 2024 17:16:57 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-724771e8b75sm5134024b3a.147.2024.11.17.17.16.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Nov 2024 17:16:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731892617; x=1732497417; darn=nongnu.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=UjRl8vMgww0LP/o14M23ZRkREhAdHlsDIuecT3qmbiE=; b=D3dVuB0JpwSvCXNXP6a43ZjmUyJVdJWKk5iOcOiTFKYGpVGhjE+8hCzGmVHnjo5FBR UGcB9F3rV8S/BOIMPx92VhI8IdQysqGY34/n47+xPfwnIpgqSkv8BdRHpgT+Usl4dmOe zyQRfV4WeF6Zc6sPtvEHvwhHITUQm8Z6yKJ1Lhs8G5DOSCA20PE/JZRptM2V6DPvIoTl YWIl+rmmXFt0i0uoYn25zS9pEx6Zl7RBYI340wBJ37zVnMqPd8ufASfnIhevBxaB69CM 0/FYoET72cSCNcSPNMpXUpdthkP4cHKGcUDVU2wFJ2EQaE3j0laEwTDjbJmdvxvlCkV8 yMPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731892617; x=1732497417; 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=UjRl8vMgww0LP/o14M23ZRkREhAdHlsDIuecT3qmbiE=; b=D3odQOXf+aZFuTiKIxFxOMMdOxIPerDPTTaGj+wmd8zK8G86Gg+6bIOp5CDfDxfWtS w9dfctKsLWBT4mxBxE2bvIcmL2ikU4u6KliljqqCBTj7P2FaytHI5k/cz8NbzKlcGQQ/ OL0P9o/JDAbYwltL+JF5/Nlr96DA6lgPqKh56X/XwAot0Pvly9C20xTR6apqXmPCmFd7 tFFjZqwfCTDC+gWVPQJjT+XHtLhFhCdmlMwmEwXvgmtngEFTaIaOGuvQo+If+M/UHPu1 L2MWZDtZcyM5sql7bAuXtrlGCeBfbSMKZI0XZj0uSsyojWJDdfH7qpUP7VLWeCAbFmFl d1aw== X-Forwarded-Encrypted: i=1; AJvYcCWchedc/LS3zgWrpeDevONgksIjnC9YrAXH5RZOMsQN1X1k6HQYLSfybbwghhWN91qc3Jee4ZXrZ9Oi@nongnu.org X-Gm-Message-State: AOJu0YxKOVszR0XlYLDw1RUZdw8cjkh0Bads0w4IdQMp3iW1DhwZ4Mla 4nYtFXYPWiYD27T9CNuTFdfakDwa0MxdlrHseKA5IS+P9I6+e/3lqkcGIKPl2T0gKAjMXWPNQrA X X-Google-Smtp-Source: AGHT+IFbXO3+m9bLJARakqrBHU69Sa8vBXXBznlw63idcWos+87SAdEnZltM4Nz8QzBwN1/TYxkhFA== X-Received: by 2002:a05:6a00:188e:b0:71e:f14:869c with SMTP id d2e1a72fcca58-72476b96f7emr15002896b3a.6.1731892616563; Sun, 17 Nov 2024 17:16:56 -0800 (PST) From: Atish Patra Date: Sun, 17 Nov 2024 17:15:52 -0800 Subject: [PATCH v3 04/11] target/riscv: Support generic CSR indirect access MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241117-counter_delegation-v3-4-476d6f36e3c8@rivosinc.com> References: <20241117-counter_delegation-v3-0-476d6f36e3c8@rivosinc.com> In-Reply-To: <20241117-counter_delegation-v3-0-476d6f36e3c8@rivosinc.com> To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: kaiwenxue1@gmail.com, Atish Patra , palmer@dabbelt.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, bin.meng@windriver.com, dbarboza@ventanamicro.com, alistair.francis@wdc.com, Kaiwen Xue X-Mailer: b4 0.15-dev-13183 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::434; envelope-from=atishp@rivosinc.com; helo=mail-pf1-x434.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 autolearn=unavailable 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: 1731892732025116600 From: Kaiwen Xue This adds the indirect access registers required by sscsrind/smcsrind and the operations on them. Note that xiselect and xireg are used for both AIA and sxcsrind, and the behavior of accessing them depends on whether each extension is enabled and the value stored in xiselect. Co-developed-by: Atish Patra Signed-off-by: Atish Patra Signed-off-by: Kaiwen Xue --- target/riscv/cpu_bits.h | 28 ++++++++- target/riscv/csr.c | 149 ++++++++++++++++++++++++++++++++++++++++++++= ++-- 2 files changed, 171 insertions(+), 6 deletions(-) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 385a2c67c24b..e13c5420a251 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -173,6 +173,13 @@ #define CSR_MISELECT 0x350 #define CSR_MIREG 0x351 =20 +/* Machine Indirect Register Alias */ +#define CSR_MIREG2 0x352 +#define CSR_MIREG3 0x353 +#define CSR_MIREG4 0x355 +#define CSR_MIREG5 0x356 +#define CSR_MIREG6 0x357 + /* Machine-Level Interrupts (AIA) */ #define CSR_MTOPEI 0x35c #define CSR_MTOPI 0xfb0 @@ -222,6 +229,13 @@ #define CSR_SISELECT 0x150 #define CSR_SIREG 0x151 =20 +/* Supervisor Indirect Register Alias */ +#define CSR_SIREG2 0x152 +#define CSR_SIREG3 0x153 +#define CSR_SIREG4 0x155 +#define CSR_SIREG5 0x156 +#define CSR_SIREG6 0x157 + /* Supervisor-Level Interrupts (AIA) */ #define CSR_STOPEI 0x15c #define CSR_STOPI 0xdb0 @@ -288,6 +302,13 @@ #define CSR_VSISELECT 0x250 #define CSR_VSIREG 0x251 =20 +/* Virtual Supervisor Indirect Alias */ +#define CSR_VSIREG2 0x252 +#define CSR_VSIREG3 0x253 +#define CSR_VSIREG4 0x255 +#define CSR_VSIREG5 0x256 +#define CSR_VSIREG6 0x257 + /* VS-Level Interrupts (H-extension with AIA) */ #define CSR_VSTOPEI 0x25c #define CSR_VSTOPI 0xeb0 @@ -863,10 +884,13 @@ typedef enum RISCVException { #define ISELECT_IMSIC_EIE63 0xff #define ISELECT_IMSIC_FIRST ISELECT_IMSIC_EIDELIVERY #define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63 -#define ISELECT_MASK 0x1ff +#define ISELECT_MASK_AIA 0x1ff + +/* MISELECT, SISELECT, and VSISELECT bits (AIA) */ +#define ISELECT_MASK_SXCSRIND 0xfff =20 /* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */ -#define ISELECT_IMSIC_TOPEI (ISELECT_MASK + 1) +#define ISELECT_IMSIC_TOPEI (ISELECT_MASK_AIA + 1) =20 /* IMSIC bits (AIA) */ #define IMSIC_TOPEI_IID_SHIFT 16 diff --git a/target/riscv/csr.c b/target/riscv/csr.c index c91a26a52ef6..57e9c9e25f02 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -306,6 +306,17 @@ static RISCVException aia_any32(CPURISCVState *env, in= t csrno) return any32(env, csrno); } =20 +static RISCVException csrind_any(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu =3D env_archcpu(env); + + if (!cpu->cfg.ext_smcsrind) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return RISCV_EXCP_NONE; +} + static RISCVException csrind_or_aia_any(CPURISCVState *env, int csrno) { if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrin= d) { @@ -389,6 +400,15 @@ static bool csrind_or_aia_extensions_present(CPURISCVS= tate *env) return csrind_extensions_present(env) || aia_extensions_present(env); } =20 +static RISCVException csrind_smode(CPURISCVState *env, int csrno) +{ + if (!csrind_extensions_present(env)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return smode(env, csrno); +} + static RISCVException csrind_or_aia_smode(CPURISCVState *env, int csrno) { if (!csrind_or_aia_extensions_present(env)) { @@ -417,6 +437,15 @@ static RISCVException hmode32(CPURISCVState *env, int = csrno) =20 } =20 +static RISCVException csrind_hmode(CPURISCVState *env, int csrno) +{ + if (!csrind_extensions_present(env)) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return hmode(env, csrno); +} + static RISCVException csrind_or_aia_hmode(CPURISCVState *env, int csrno) { if (!csrind_or_aia_extensions_present(env)) { @@ -2065,7 +2094,12 @@ static int csrind_xlate_vs_csrno(CPURISCVState *env,= int csrno) case CSR_SISELECT: return CSR_VSISELECT; case CSR_SIREG: - return CSR_VSIREG; + case CSR_SIREG2: + case CSR_SIREG3: + case CSR_SIREG4: + case CSR_SIREG5: + case CSR_SIREG6: + return CSR_VSIREG + (csrno - CSR_SIREG); default: return csrno; }; @@ -2105,7 +2139,12 @@ static RISCVException rmw_xiselect(CPURISCVState *en= v, int csrno, *val =3D *iselect; } =20 - wr_mask &=3D ISELECT_MASK; + if (riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsri= nd) { + wr_mask &=3D ISELECT_MASK_SXCSRIND; + } else { + wr_mask &=3D ISELECT_MASK_AIA; + } + if (wr_mask) { *iselect =3D (*iselect & ~wr_mask) | (new_val & wr_mask); } @@ -2244,6 +2283,59 @@ done: return RISCV_EXCP_NONE; } =20 +/* + * rmw_xireg_csrind: Perform indirect access to xireg and xireg2-xireg6 + * + * Perform indirect access to xireg and xireg2-xireg6. + * This is a generic interface for all xireg CSRs. Apart from AIA, all oth= er + * extension using csrind should be implemented here. + */ +static int rmw_xireg_csrind(CPURISCVState *env, int csrno, + target_ulong isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + return -EINVAL; +} + +static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + bool virt =3D false; + int ret =3D -EINVAL; + target_ulong isel; + + ret =3D smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + + /* Translate CSR number for VS-mode */ + csrno =3D csrind_xlate_vs_csrno(env, csrno); + + if (CSR_MIREG <=3D csrno && csrno <=3D CSR_MIREG6 && + csrno !=3D CSR_MIREG4 - 1) { + isel =3D env->miselect; + } else if (CSR_SIREG <=3D csrno && csrno <=3D CSR_SIREG6 && + csrno !=3D CSR_SIREG4 - 1) { + isel =3D env->siselect; + } else if (CSR_VSIREG <=3D csrno && csrno <=3D CSR_VSIREG6 && + csrno !=3D CSR_VSIREG4 - 1) { + isel =3D env->vsiselect; + virt =3D true; + } else { + goto done; + } + + return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); + +done: + if (ret) { + return (env->virt_enabled && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; + } + return RISCV_EXCP_NONE; +} + static RISCVException rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) @@ -2276,8 +2368,21 @@ static RISCVException rmw_xireg(CPURISCVState *env, = int csrno, goto done; }; =20 + /* + * Use the xiselect range to determine actual op on xireg. + * + * Since we only checked the existence of AIA or Indirect Access in the + * predicate, we should check the existence of the exact extension when + * we get to a specific range and return illegal instruction exception= even + * in VS-mode. + */ if (xiselect_aia_range(isel)) { return rmw_xireg_aia(env, csrno, isel, val, new_val, wr_mask); + } else if (riscv_cpu_cfg(env)->ext_smcsrind || + riscv_cpu_cfg(env)->ext_sscsrind) { + return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); + } else { + return RISCV_EXCP_ILLEGAL_INST; } =20 done: @@ -2735,7 +2840,7 @@ static RISCVException write_mstateen0(CPURISCVState *= env, int csrno, wr_mask |=3D SMSTATEEN0_P1P13; } =20 - if (riscv_cpu_cfg(env)->ext_smaia) { + if (riscv_cpu_cfg(env)->ext_smaia || riscv_cpu_cfg(env)->ext_smcsrind)= { wr_mask |=3D SMSTATEEN0_SVSLCT; } =20 @@ -2828,7 +2933,7 @@ static RISCVException write_hstateen0(CPURISCVState *= env, int csrno, wr_mask |=3D SMSTATEEN0_FCSR; } =20 - if (riscv_cpu_cfg(env)->ext_ssaia) { + if (riscv_cpu_cfg(env)->ext_ssaia || riscv_cpu_cfg(env)->ext_sscsrind)= { wr_mask |=3D SMSTATEEN0_SVSLCT; } =20 @@ -5261,6 +5366,18 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_MIREG] =3D { "mireg", csrind_or_aia_any, NULL, NULL, rmw_xireg }, =20 + /* Machine Indirect Register Alias */ + [CSR_MIREG2] =3D { "mireg2", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MIREG3] =3D { "mireg3", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MIREG4] =3D { "mireg4", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MIREG5] =3D { "mireg5", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MIREG6] =3D { "mireg6", csrind_any, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + /* Machine-Level Interrupts (AIA) */ [CSR_MTOPEI] =3D { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, [CSR_MTOPI] =3D { "mtopi", aia_any, read_mtopi }, @@ -5382,6 +5499,18 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_SIREG] =3D { "sireg", csrind_or_aia_smode, NULL, NULL, rmw_xireg = }, =20 + /* Supervisor Indirect Register Alias */ + [CSR_SIREG2] =3D { "sireg2", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_SIREG3] =3D { "sireg3", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_SIREG4] =3D { "sireg4", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_SIREG5] =3D { "sireg5", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_SIREG6] =3D { "sireg6", csrind_smode, NULL, NULL, rmw_xiregi, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPEI] =3D { "stopei", aia_smode, NULL, NULL, rmw_xtopei= }, [CSR_STOPI] =3D { "stopi", aia_smode, read_stopi }, @@ -5464,6 +5593,18 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_VSIREG] =3D { "vsireg", csrind_or_aia_hmode, NULL, NULL, rmw_xireg = }, =20 + /* Virtual Supervisor Indirect Alias */ + [CSR_VSIREG2] =3D { "vsireg2", csrind_hmode, NULL, NULL, rmw_xireg= i, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_VSIREG3] =3D { "vsireg3", csrind_hmode, NULL, NULL, rmw_xireg= i, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_VSIREG4] =3D { "vsireg4", csrind_hmode, NULL, NULL, rmw_xireg= i, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_VSIREG5] =3D { "vsireg5", csrind_hmode, NULL, NULL, rmw_xireg= i, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_VSIREG6] =3D { "vsireg6", csrind_hmode, NULL, NULL, rmw_xireg= i, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPEI] =3D { "vstopei", aia_hmode, NULL, NULL, rmw_xtop= ei }, [CSR_VSTOPI] =3D { "vstopi", aia_hmode, read_vstopi }, --=20 2.34.1