From nobody Fri Nov 1 07:24:14 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=1708128181; cv=none; d=zohomail.com; s=zohoarc; b=akoi32BIAWNF7nr1vhPdw7k0fQ88Z4dLSw7onfF1k1DvegTt3wu9dAb2uOUNreLCwAPdQo+EEhRifc1D31lJnQ0cGAX9Z4f+z0UFxSGxd8s87lGW2c4ZzGa2vKkblx3Yks1Hw+ocGMP4EH6xDmaVInTEr5rhYcdWMiKIkDy7Rf0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708128181; 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=Znixe6dXR2BFlfMVD0apwVpdd9139jR5123rhJJMt7E=; b=EjdmGhih0Di/ParwVlxuOpYyu7Ywr7iU3qN5tmH4M02tIufit8DyuyCCTSdMga1CW7X4f31aZO2C1Py5BOPimtA6XFXivA8A1hl32yJGDnV6Clem0zw8yUBKv7lsv+kiOk6W24bI3+Z4wRIMWGkUvhTkAqPTX4YPYxTrPkxAZZ8= 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 1708128181090184.13532832459032; Fri, 16 Feb 2024 16:03:01 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rb89g-0003PQ-Mu; Fri, 16 Feb 2024 19:02:10 -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 1rb89b-0003OZ-E9 for qemu-devel@nongnu.org; Fri, 16 Feb 2024 19:02:04 -0500 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rb89N-0007vN-EG for qemu-devel@nongnu.org; Fri, 16 Feb 2024 19:02:03 -0500 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6e09143c7bdso1709998b3a.3 for ; Fri, 16 Feb 2024 16:01:49 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id p13-20020a635b0d000000b005d66caee3d0sm464015pgb.22.2024.02.16.16.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 16:01:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1708128107; x=1708732907; 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=Znixe6dXR2BFlfMVD0apwVpdd9139jR5123rhJJMt7E=; b=Bcy/jO7q1nd58Me9c6bxsS0TT5ZJ5DtKPL3g1Qc3/BuH/BNO447exLP1Qc94w2srMH IwlC+hDFShMHfR/hj0aKrktYvWjS7iHZtp5Z9Cjq3Sz0fBB/DcGPp8K+mn5mZ1MpoAGp ZmpytkAC5gYzbCnfFIQRN3KPJOzvYJfScDr68urqNxeKnAzRZ1dOT5jq8W9W9Xu0Yiq4 /JpkWID1qWJ38Q23Eg7A1yG8OUT6NUoNt6+4YLHNue987kGiCCZOATG4cyXAMuZALeiI ytyvfKOLRXrdLQnjLMp9yZpvebAU7uU8sczubXC8zp5t6QFe85w6ZBTmorChKi6WnSu6 64ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708128107; x=1708732907; 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=Znixe6dXR2BFlfMVD0apwVpdd9139jR5123rhJJMt7E=; b=oEv9cf5J61XItBknzT3wx/bl2GmvxDvThR7nw73joPKNKs3ryL+t+FiHBzkxwBQdfG 8QmZHvfg8WKQnrwdyc2UovceSXMfvkWt+izkGSV1UVxvQpMKkjOVjMMAoCIwTRoIIo0g 5Rj5U12trDUYpoancGNNIRB/+JLSQ3I2cmcrdlp3exgtxEc0TDJ+kx4o/+oEOM3QHXxw XPLAFlsJlm2xWq6cU8ExMrNdCIesfP971341AUhJavjOst3j1X5VLXIS3gDGQ/EIIXOL wIHtAtivP7I6Mv9CuVb4steGh2Up33r1/CEUWpXG9Ystrm7j/HK8DeFemgkhJpH3liMe i96A== X-Gm-Message-State: AOJu0Yw2IGfK/HElf/4luddD3fjeWM+ValPLD106CKtJZnfeqZAVVFCl 8ciDGyaiqNS1LrANAmm6k7iNJT9nyfkxUHlqsQEADyDk28S0/cScWoYJXWta/vDIIFuW8tHwIBn D X-Google-Smtp-Source: AGHT+IEeJQfda2ruAtsclJSP7u8kSHO7tyK5NQ4gK/G2JGrJPMgO+9ew0VRjLHR5vKhRCDunB/So4Q== X-Received: by 2002:a05:6a00:6c9f:b0:6e0:a30c:8c84 with SMTP id jc31-20020a056a006c9f00b006e0a30c8c84mr7837245pfb.1.1708128107107; Fri, 16 Feb 2024 16:01:47 -0800 (PST) From: Atish Patra To: qemu-devel@nongnu.org Cc: Atish Patra , Alistair Francis , Bin Meng , Daniel Henrique Barboza , Liu Zhiwei , Palmer Dabbelt , qemu-riscv@nongnu.org, Weiwei Li , kaiwenxue1@gmail.com Subject: [PATCH RFC 2/8] target/riscv: Decouple AIA processing from xiselect and xireg Date: Fri, 16 Feb 2024 16:01:28 -0800 Message-Id: <20240217000134.3634191-3-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240217000134.3634191-1-atishp@rivosinc.com> References: <20240217000134.3634191-1-atishp@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=2607:f8b0:4864:20::430; envelope-from=atishp@rivosinc.com; helo=mail-pf1-x430.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, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_TEMPERROR=0.01 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: 1708128182554100005 Content-Type: text/plain; charset="utf-8" From: Kaiwen Xue Since xiselect and xireg also will be of use in sxcsrind, AIA should have its own separated interface when those CSRs are accessed. Signed-off-by: Atish Patra Signed-off-by: Kaiwen Xue --- target/riscv/csr.c | 147 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 122 insertions(+), 25 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 8829dee7bc75..1af0c8890a2b 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -287,6 +287,15 @@ static int aia_any32(CPURISCVState *env, int csrno) return any32(env, csrno); } =20 +static int sxcsrind_or_aia_any(CPURISCVState *env, int csrno) +{ + if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrin= d) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return any(env, csrno); +} + static RISCVException smode(CPURISCVState *env, int csrno) { if (riscv_has_ext(env, RVS)) { @@ -323,6 +332,15 @@ static int aia_smode32(CPURISCVState *env, int csrno) return smode32(env, csrno); } =20 +static int sxcsrind_or_aia_smode(CPURISCVState *env, int csrno) +{ + if (!riscv_cpu_cfg(env)->ext_ssaia && !riscv_cpu_cfg(env)->ext_sscsrin= d) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return smode(env, csrno); +} + static RISCVException hmode(CPURISCVState *env, int csrno) { if (riscv_has_ext(env, RVH)) { @@ -342,6 +360,15 @@ static RISCVException hmode32(CPURISCVState *env, int = csrno) =20 } =20 +static int sxcsrind_or_aia_hmode(CPURISCVState *env, int csrno) +{ + if (!riscv_cpu_cfg(env)->ext_ssaia && !riscv_cpu_cfg(env)->ext_sscsrin= d) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return hmode(env, csrno); +} + static RISCVException umode(CPURISCVState *env, int csrno) { if (riscv_has_ext(env, RVU)) { @@ -1804,13 +1831,29 @@ static int aia_xlate_vs_csrno(CPURISCVState *env, i= nt csrno) }; } =20 +static int sxcsrind_xlate_vs_csrno(CPURISCVState *env, int csrno) +{ + if (!env->virt_enabled) { + return csrno; + } + + switch (csrno) { + case CSR_SISELECT: + return CSR_VSISELECT; + case CSR_SIREG: + return CSR_VSIREG; + default: + return csrno; + }; +} + static int rmw_xiselect(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { target_ulong *iselect; =20 /* Translate CSR number for VS-mode */ - csrno =3D aia_xlate_vs_csrno(env, csrno); + csrno =3D sxcsrind_xlate_vs_csrno(env, csrno); =20 /* Find the iselect CSR based on CSR number */ switch (csrno) { @@ -1839,6 +1882,12 @@ static int rmw_xiselect(CPURISCVState *env, int csrn= o, target_ulong *val, return RISCV_EXCP_NONE; } =20 +static bool xiselect_aia_range(target_ulong isel) +{ + return (ISELECT_IPRIO0 <=3D isel && isel <=3D ISELECT_IPRIO15) || + (ISELECT_IMSIC_FIRST <=3D isel && isel <=3D ISELECT_IMSIC_LAST); +} + static int rmw_iprio(target_ulong xlen, target_ulong iselect, uint8_t *iprio, target_ulong *val, target_ulong new_val, @@ -1884,44 +1933,44 @@ static int rmw_iprio(target_ulong xlen, return 0; } =20 -static int rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val, - target_ulong new_val, target_ulong wr_mask) +static int rmw_xireg_aia(CPURISCVState *env, int csrno, + target_ulong isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) { - bool virt, isel_reserved; - uint8_t *iprio; + bool virt =3D false, isel_reserved =3D false; int ret =3D -EINVAL; - target_ulong priv, isel, vgein; - - /* Translate CSR number for VS-mode */ - csrno =3D aia_xlate_vs_csrno(env, csrno); + uint8_t *iprio; + target_ulong priv, vgein; =20 - /* Decode register details from CSR number */ - virt =3D false; - isel_reserved =3D false; + /* VS-mode CSR number passed in has already been translated */ switch (csrno) { case CSR_MIREG: + if (!riscv_cpu_cfg(env)->ext_smaia) { + goto done; + } iprio =3D env->miprio; - isel =3D env->miselect; priv =3D PRV_M; break; case CSR_SIREG: - if (env->priv =3D=3D PRV_S && env->mvien & MIP_SEIP && + if (!riscv_cpu_cfg(env)->ext_ssaia || + (env->priv =3D=3D PRV_S && env->mvien & MIP_SEIP && env->siselect >=3D ISELECT_IMSIC_EIDELIVERY && - env->siselect <=3D ISELECT_IMSIC_EIE63) { + env->siselect <=3D ISELECT_IMSIC_EIE63)) { goto done; } iprio =3D env->siprio; - isel =3D env->siselect; priv =3D PRV_S; break; case CSR_VSIREG: + if (!riscv_cpu_cfg(env)->ext_ssaia) { + goto done; + } iprio =3D env->hviprio; - isel =3D env->vsiselect; priv =3D PRV_S; virt =3D true; break; default: - goto done; + goto done; }; =20 /* Find the selected guest interrupt file */ @@ -1952,10 +2001,53 @@ static int rmw_xireg(CPURISCVState *env, int csrno,= target_ulong *val, } =20 done: + /* + * If AIA is not enabled, illegal instruction exception is always + * returned regardless of whether we are in VS-mode or not + */ if (ret) { return (env->virt_enabled && virt && !isel_reserved) ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; } + + return RISCV_EXCP_NONE; +} + +static int rmw_xireg(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; + + /* Translate CSR number for VS-mode */ + csrno =3D sxcsrind_xlate_vs_csrno(env, csrno); + + /* Decode register details from CSR number */ + switch (csrno) { + case CSR_MIREG: + isel =3D env->miselect; + break; + case CSR_SIREG: + isel =3D env->siselect; + break; + case CSR_VSIREG: + isel =3D env->vsiselect; + virt =3D true; + break; + default: + goto done; + }; + + if (xiselect_aia_range(isel)) { + return rmw_xireg_aia(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; } =20 @@ -4682,8 +4774,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_MIP] =3D { "mip", any, NULL, NULL, rmw_mip }, =20 /* Machine-Level Window to Indirectly Accessed Registers (AIA) */ - [CSR_MISELECT] =3D { "miselect", aia_any, NULL, NULL, rmw_xiselec= t }, - [CSR_MIREG] =3D { "mireg", aia_any, NULL, NULL, rmw_xireg }, + [CSR_MISELECT] =3D { "miselect", sxcsrind_or_aia_any, NULL, NULL, + rmw_xiselect }, + [CSR_MIREG] =3D { "mireg", sxcsrind_or_aia_any, NULL, NULL, + rmw_xireg }, =20 /* Machine-Level Interrupts (AIA) */ [CSR_MTOPEI] =3D { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, @@ -4801,8 +4895,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_SATP] =3D { "satp", satp, read_satp, write_satp }, =20 /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */ - [CSR_SISELECT] =3D { "siselect", aia_smode, NULL, NULL, rmw_xisele= ct }, - [CSR_SIREG] =3D { "sireg", aia_smode, NULL, NULL, rmw_xireg = }, + [CSR_SISELECT] =3D { "siselect", sxcsrind_or_aia_smode, NULL, NULL, + rmw_xiselect = }, + [CSR_SIREG] =3D { "sireg", sxcsrind_or_aia_smode, NULL, NULL, + rmw_xireg = }, =20 /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPEI] =3D { "stopei", aia_smode, NULL, NULL, rmw_xtopei= }, @@ -4881,9 +4977,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { /* * VS-Level Window to Indirectly Accessed Registers (H-extension with = AIA) */ - [CSR_VSISELECT] =3D { "vsiselect", aia_hmode, NULL, NULL, - rmw_xiselect = }, - [CSR_VSIREG] =3D { "vsireg", aia_hmode, NULL, NULL, rmw_xire= g }, + [CSR_VSISELECT] =3D { "vsiselect", sxcsrind_or_aia_hmode, NULL, NU= LL, + rmw_xiselect = }, + [CSR_VSIREG] =3D { "vsireg", sxcsrind_or_aia_hmode, NULL, NU= LL, + rmw_xireg = }, =20 /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPEI] =3D { "vstopei", aia_hmode, NULL, NULL, rmw_xtop= ei }, --=20 2.34.1