From patchwork Tue Feb 18 02:54:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13978845 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 CFAF4C021AA for ; Tue, 18 Feb 2025 02:56:14 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tkDlK-00048D-VO; Mon, 17 Feb 2025 21:55:08 -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 1tkDlD-00045o-Qw for qemu-devel@nongnu.org; Mon, 17 Feb 2025 21:55:01 -0500 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tkDl8-0000Uy-UI for qemu-devel@nongnu.org; Mon, 17 Feb 2025 21:54:58 -0500 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-220d28c215eso69925495ad.1 for ; Mon, 17 Feb 2025 18:54:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739847292; x=1740452092; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=jMUW/GnUYvHdgMBPniAILJ6d8IoxDISMulVgOjY7AJs=; b=2cNRhxML4+5/kBihZKinNo2+/u8MRWnltw7H6eQYRX5AiTrTW3nzwCDBJO59FJG5e6 DI2uO9Q6ohg0aKzwdH8L7H3hnrF23yhkdI0PI0CCwmxvVktt4AjllTSZRhl5QPHw6lZx dl5tbqQ//uI86vMBYxMC2Y/4ltAknakioGmQCIt4j2bS/L52VBmVQTqa1ZZNjYIaHFmk zDUjsTHo6AOVGyDuedVLif3E0nbIlbWUNR0gxkKMrHIA6P1Y/A5inEZwNQJj10R0Mbcv +q9lYFktJ0vY5QbOYWRHF2uiHbatqLG2brjognhg/nCQDnUFSSzr2LdqvG189xO/UznU GplA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739847292; x=1740452092; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jMUW/GnUYvHdgMBPniAILJ6d8IoxDISMulVgOjY7AJs=; b=dWQd8adv3Xn5o2NISntQPIZNEFeA9iWNkoDiA71M6DJ+tBrqu4Jts65uMsHxSY7+nq r8l0yT16iHUhXZZUPPePtXhifyxRlIOneZkXPQSaZN+poTe4ME2FRQS7glLvZnat5dcK xmu/7rSyC9zlQm0HzVppLwRrgVLWUAiaKd5vorroeAOiFJwfGqc3k6K46pw81Im9qTbI AkWT7snk3G/bpyrlpMVSm5/P+70QDSdvmJsEVqhO7Ek0fDTX0lPv+nifsjqugrjM0L/7 g/Lvt3AOhrIMOKqVPrU4yxli/yjEpeR7YY976MehvUjJyFtmrggemJlmhdmCDOTr9MqW d70w== X-Forwarded-Encrypted: i=1; AJvYcCXhERkYfUWUX966wYjMf5OSMR4ApbBotljR90HF7JqAItkaBmjkSB1mFqbFrSfHv+NLIiKIVJA56VPK@nongnu.org X-Gm-Message-State: AOJu0Ywq1aBccDS/aMaEmXiOzELX2kgJBr7h1tpUzZW4U1TucsZhSuwl 436/zgsXLtWo81whwauiqZyZXs0GoJ7LKGpHdeWWfB18YBgUzo9FMIzbcBvNbP0= X-Gm-Gg: ASbGncsGWb2gGtcFYLXk8UVgFoxB4lTwmbFDqOj8udSIOak10tW/0Pj1NKXchUZDdbf HcmnRQzC+CioTJvd2j488ZBIP4vssSV87EDKs34kcPRg1vxPtfC5Vx4n6n7iU8bXk2VFM17u3Jf Nl0ioJNpxP2OHLLrpVpW16kpsKzDSo/IWGdWDU8jMfxGMvnhBSPW8bb5riWQ/eO1f15crzx5zv5 54O03x0vGNmszkrj7YF5fDFkfQdF0K1OlYGuUhlwZA2B2AEhEG5goyRmsSvP1MuSn0cm4fMvJtJ 7aug/T9/rP5q1TL5Ay1+gqykzg== X-Google-Smtp-Source: AGHT+IHcTqgc4/lvSBqE2D6EQZhGZRrp93z7bvsvT3PjZbtKYZAv/aJrdATgWP36QjF8aIHEJJqcqA== X-Received: by 2002:a17:902:d58b:b0:216:2426:767f with SMTP id d9443c01a7336-221040cec77mr201836485ad.49.1739847292053; Mon, 17 Feb 2025 18:54:52 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556d473sm78293595ad.166.2025.02.17.18.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 18:54:51 -0800 (PST) From: Deepak Gupta To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, bmeng.cn@gmail.com, liwei1518@gmail.com, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, Deepak Gupta , Adam Zabrocki Subject: [PATCH 1/2] target/riscv: fix access permission checks for CSR_SSP Date: Mon, 17 Feb 2025 18:54:45 -0800 Message-Id: <20250218025446.2452254-1-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=debug@rivosinc.com; helo=mail-pl1-x635.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=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via `xSSE`. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer" of `zicfiss` specification. Thanks to Adam Zabrocki for bringing this to attention. Fixes: 8205bc127a83 ("target/riscv: introduce ssp and enabling controls for zicfiss" Reported-by: Adam Zabrocki Signed-off-by: Deepak Gupta --- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index afb7544f07..75c661d2a1 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno) return RISCV_EXCP_ILLEGAL_INST; } + /* If ext implemented, M-mode always have access to SSP CSR */ + if (env->priv == PRV_M) { + return RISCV_EXCP_NONE; + } + /* if bcfi not active for current env, access to csr is illegal */ if (!cpu_get_bcfien(env)) { #if !defined(CONFIG_USER_ONLY) From patchwork Tue Feb 18 02:54:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13978846 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 CC62CC021A9 for ; Tue, 18 Feb 2025 02:56:14 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tkDlU-0004HQ-DP; Mon, 17 Feb 2025 21:55:16 -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 1tkDlF-000462-9L for qemu-devel@nongnu.org; Mon, 17 Feb 2025 21:55:01 -0500 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tkDl8-0000VB-UO for qemu-devel@nongnu.org; Mon, 17 Feb 2025 21:54:59 -0500 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-22100006bc8so47095905ad.0 for ; Mon, 17 Feb 2025 18:54:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739847293; x=1740452093; 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=7ansaoQj2VIkNZSxiwgYKiw4k0MHlhOwmkCCrsFy9sk=; b=hkFr5jsSKhCFa5HkzXquPxsboxAOqItVgnnpV+qtl28XttV/+RC46iCB5vvUjlJad6 p1JW6dNQ/oWe3zk4HLE6M7mX9S/nqvTltgpbRACQ77q29EYSoNq47I4zf4VP+uMUd9fg IFce5aHS95xWFb3DGXkyR7Wu6w1pde2fVmgXSSkzkjb5n7AQoxyhMhcZRfjHX+X5+ysr LCMkXanOwTUbnLIbfkTYpDt5kPvpjOrkeCcfMq5rw5WYSy0UXrXRzMwulsVqQwUQPx6T 0KP1lh5tM5SgE5kx82CyvYHLrPnHGwYP89OcKRcWOkE4qgmrYS5lhlI0TK04X1SFhTZ7 Q+WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739847293; x=1740452093; 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=7ansaoQj2VIkNZSxiwgYKiw4k0MHlhOwmkCCrsFy9sk=; b=mLXlhSENlmMAtChR3v1fIi1kzyPFD8HgbD2+u66FjukC8cSx6UEj1FE6rbs2SWifi3 D+ZF7h9lDWoeYD1ZXUJqFsrZYkAjriw5v8V0NuEMAj988N2FA2I4t+9eXrIcQOzP6nlB teLHjLPaVWt3Ot6ZftxG79+7jFstaEpBgw88qmYdj1V8rNQqHJSRhdAoCseD5v7vwii1 nE9QZtLVvGs2h7X6Lj+TT2RSGMW3nI+Uxu+mohuOErEhdZJRVT3fTudWQgqG9cSemjuj 8oARS2jWKIdJ9QrMuqE+CElYKcQ0cPaSbD0E7GBFemsdrLnrBDjySu6Y9vziD/Q19DD6 ODnQ== X-Forwarded-Encrypted: i=1; AJvYcCVXKth9o/0y9WvBZPs433DYYv7pYj7e1Dl9DUipxJzt5Vsuygcudh+IyPyolAKJazQIiFaBbj7OgYfb@nongnu.org X-Gm-Message-State: AOJu0Yy/nARF8blUrWevC7VZJrpfjXSzvbFzaDft2v4QM9u9iNZBEaO/ f7FK+Weee4qfgREXB/JpckAwhWJ3GFIolS1CyLjRDce5qy5/Lx24ittGZo8GcIc= X-Gm-Gg: ASbGncuZtgdWy/NgWJqDLIjv58chFRZl0bhP0u5oOYkP8vlcaTf0rTk0EWikUy/avqc Pgy8lNCYP29rDJiaSLanO10CpchQI647tHjqVvkG+D/G5tEyKeKwxWEAc6At/ilt6ODEuDnRH3A SAFM9jyN1lhjvO0X/wTAvWCz12QU7UUqyWmfSyTBp6CKKsGzkqrQldZqqDaonfNoL4kzR9lPFLD 8JnqQjP2P29JlLutVvMPVAeP990/k6kDOEr+zyBRzZqIgtKT9TOf/fP4ocrIcK/RBRsg9BuBM0T 1DTOBP7vQgfgP95Cz5uAp1DXfw== X-Google-Smtp-Source: AGHT+IHx5PshsvLEAlI4zKHqHquOxs/xHzGaDPJb6I+3G83+tZ08pz6of8sx0EYU4p2zNg+4LzfUwA== X-Received: by 2002:a17:903:41c3:b0:21f:7821:55b6 with SMTP id d9443c01a7336-22104030832mr188581915ad.13.1739847293079; Mon, 17 Feb 2025 18:54:53 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556d473sm78293595ad.166.2025.02.17.18.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 18:54:52 -0800 (PST) From: Deepak Gupta To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, alistair.francis@wdc.com, bmeng.cn@gmail.com, liwei1518@gmail.com, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, Deepak Gupta , Ved Shanbhogue Subject: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode Date: Mon, 17 Feb 2025 18:54:46 -0800 Message-Id: <20250218025446.2452254-2-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250218025446.2452254-1-debug@rivosinc.com> References: <20250218025446.2452254-1-debug@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=debug@rivosinc.com; helo=mail-pl1-x630.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds `ssamoswap` instruction. `ssamoswap` takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then `ssamoswap` must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode. Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, `ssamoswap` can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for `ssamoswap`. Fixes: f06bfe3dc38c ("target/riscv: implement zicfiss instructions") Reported-by: Ved Shanbhogue Signed-off-by: Deepak Gupta --- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/target/riscv/insn_trans/trans_rvzicfiss.c.inc b/target/riscv/insn_trans/trans_rvzicfiss.c.inc index e3ebc4977c..ec016cd70f 100644 --- a/target/riscv/insn_trans/trans_rvzicfiss.c.inc +++ b/target/riscv/insn_trans/trans_rvzicfiss.c.inc @@ -15,6 +15,13 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ + + #define REQUIRE_ZICFISS(ctx) do { \ + if (!ctx->cfg_ptr->ext_zicfiss) { \ + return false; \ + } \ +} while (0) + static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) { if (!ctx->bcfi_enabled) { @@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a) static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; } @@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) { REQUIRE_64BIT(ctx); REQUIRE_A_OR_ZAAMO(ctx); - if (!ctx->bcfi_enabled) { + REQUIRE_ZICFISS(ctx); + if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) { return false; }