From patchwork Thu Feb 9 04:26:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 1739786 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Az8AahAk; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=rivosinc-com.20210112.gappssmtp.com header.i=@rivosinc-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=BZQ0NB8d; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PC3k53qp9z23yG for ; Thu, 9 Feb 2023 15:27:23 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ft6N0d91S9fiROVIkMjvqvjOLfl2hWUcNCfz0uW5AgE=; b=Az8AahAk/RvrcF bVN2oHKx+IEY/IzwLCeTGLE2YSSu5g9593HChTBe42tFaWsQC1LQxR7BdTqlpuRZiHsrkF3SWo4Ja lvHtlitYh5p/rx/dlSle2/fUH2kNUFQ+HghcyA64sZw+otG2+Al5UMeyRtSTRmIbiAr+FH31ZMM8+ f6lE8jMbImjs5xH1y8Ul+pzT0u1OC0TMwj8XltI9H3kZaNRYRZrcAUk1ipdxFw59hsaU6AQvEvaeb gqczDdCjJS1DqjVAmDFQJV7Szte6uFVGOOJQ4pswtNLwLw2wIfFRBsTAI81KGgnTa9aCsCt7GJ3PF jR9tsvU0qWA3ZC22LbIQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPyWc-000874-8j; Thu, 09 Feb 2023 04:27:10 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPyWa-00084y-7X for opensbi@lists.infradead.org; Thu, 09 Feb 2023 04:27:09 +0000 Received: by mail-pg1-x52c.google.com with SMTP id u75so794089pgc.10 for ; Wed, 08 Feb 2023 20:27:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; 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=Gcolcqr/+KFA8Z6F1fcmSmXex5OlsD4AwBbjpSvSA6g=; b=BZQ0NB8djA8DIkEHgl6x9cJp2wdyCXfewrJT8Nc+a6KdiEwljNXlYv2eBWwZkEonXO 8pRlDI7BrT2aBJ95LFfmqPQgs2C1iA/C35EJ6sH5bGl0awU/MyHvfgspMU6uq076k/mQ OE8I/o9c04wGIpCjh6bOXsbOSpxzYhJn1VI2rymZDjc8YTiCfSGpALSCWaQfE5FfEAx7 q2tiW2wgkxiSmbPizqa8T6pmId+L8OFKQB6NkjvkFXlaCP5I0YkX+0KyDz9czAwTddWB NvuKLR/LyHfFedrwWKhVmjiMD6Ul8OxoQETnWI5lyzEOg8odEUUj0fHxQUIfEXYBt+N5 BhBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Gcolcqr/+KFA8Z6F1fcmSmXex5OlsD4AwBbjpSvSA6g=; b=b5kkJ7v7bLEaEnN1S5kleHqVGnMfO7BdKaM5BnqAePK58BPPBmsRy1bWLWo88bVHDW ipTKqtbF8VGRPE1pHA1fb/ZocseNZKG1sC50gW2MgWarOwCGxB/gTLJMgL8GqPev0q96 LBXjzjXsyTkwj6yLJRzqbVRc4rwJzq1pOQOlmLPsXrIjhI6xqRnO/fds1887vfu3c9cA iIjiyuo3Y6AulE7c+tovAoY2fSq1JnQOn/+9fss1nHiUOvlg1cQZkQoh7jAGxg5y6BNr mBrEoKi+jydKV1+JMwOQzK0RMgoCc/6kjGomW+PiBYAErfvAtnlgL5XPMdbt6+MculR9 //aA== X-Gm-Message-State: AO0yUKUNIWfroeyYJWFACwz0Pk+2CvdaS9r8Dimc3v0OdgWxom0sdrTU P79YUWguZpPXkhpowe+wOZhMn3OgBO96+yBE X-Google-Smtp-Source: AK7set/MJOi7zosqGkyKzww7s3w47kIMnwqHq3eYvxdOLZ4Rcw/wDN+21L+DLpBo8MML5yy0xwQsZA== X-Received: by 2002:a05:6a00:db:b0:5a8:4e4e:df6d with SMTP id e27-20020a056a0000db00b005a84e4edf6dmr2000650pfj.6.1675916822386; Wed, 08 Feb 2023 20:27:02 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id 26-20020aa7921a000000b00593eb3a5e44sm283853pfo.37.2023.02.08.20.27.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 20:27:01 -0800 (PST) From: Deepak Gupta To: opensbi@lists.infradead.org Cc: Deepak Gupta Subject: [PATCH v3 Zisslpcfi 2/2] lib: sbi: Zisslpcfi detection and elp cfi state reflect back in status Date: Wed, 8 Feb 2023 20:26:54 -0800 Message-Id: <20230209042654.3568990-3-debug@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230209042654.3568990-1-debug@rivosinc.com> References: <20230209042654.3568990-1-debug@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230208_202708_289032_88FEB26B X-CRM114-Status: GOOD ( 13.15 ) X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This patch adds support for zisslpcfi detection in sbi_hart.c If Zisslpcfi is detected, this turns on menvcfg.CFI Zisslpcfi records status of cfi state in xstatus csr. Missing landing pad sets MPELP in mstatus. When SBI is redirecting back to S/VS/HS, SPELP is set in sstatus/vsstatus. Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:52c listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This patch adds support for zisslpcfi detection in sbi_hart.c If Zisslpcfi is detected, this turns on menvcfg.CFI Zisslpcfi records status of cfi state in xstatus csr. Missing landing pad sets MPELP in mstatus. When SBI is redirecting back to S/VS/HS, SPELP is set in sstatus/vsstatus. Signed-off-by: Deepak Gupta --- changelog v1 --> v3: - instead of using "int" for lplr_exist/ssp_exist use "bool" - use smaller case true/false - updates to use correct extension name "zisslpcfi" --- lib/sbi/sbi_hart.c | 23 +++++++++++++++++++++++ lib/sbi/sbi_trap.c | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 5447c52..f7fb524 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -157,6 +157,16 @@ static void mstatus_init(struct sbi_scratch *scratch) #endif } + if (sbi_hart_has_extension(scratch, SBI_HART_EXT_ZISSLPCFI)) { +#if __riscv_xlen == 32 + unsigned long menvcfgh_val; + menvcfgh_val = csr_read(CSR_MENVCFGH); + menvcfgh_val |= ENVCFGH_CFI; + csr_write(CSR_MENVCFGH, menvcfgh_val); +#else + menvcfg_val |= ENVCFG_CFI; +#endif + } csr_write(CSR_MENVCFG, menvcfg_val); } @@ -446,6 +456,8 @@ static inline char *sbi_hart_extension_id2string(int ext) case SBI_HART_EXT_SMSTATEEN: estr = "smstateen"; break; + case SBI_HART_EXT_ZISSLPCFI: + estr = "zisslpcfi"; default: break; } @@ -555,6 +567,7 @@ static int hart_detect_features(struct sbi_scratch *scratch) sbi_scratch_offset_ptr(scratch, hart_features_offset); unsigned long val, oldval; int rc; + bool ssp_exist, lplr_exist; /* If hart features already detected then do nothing */ if (hfeatures->detected) @@ -693,6 +706,16 @@ __mhpm_skip: SBI_HART_EXT_SMSTATEEN, true); } + if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) { + val = csr_read_allowed(CSR_SSP, (unsigned long)&trap); + ssp_exist = trap.cause == 0; + val = csr_read_allowed(CSR_LPLR, (unsigned long)&trap); + lplr_exist = trap.cause == 0; + if (lplr_exist & ssp_exist) + __sbi_hart_update_extension(hfeatures, + SBI_HART_EXT_ZISSLPCFI, true); + } + /* Let platform populate extensions */ rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(), hfeatures); diff --git a/lib/sbi/sbi_trap.c b/lib/sbi/sbi_trap.c index c875c90..80d9e65 100644 --- a/lib/sbi/sbi_trap.c +++ b/lib/sbi/sbi_trap.c @@ -87,6 +87,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs, struct sbi_trap_info *trap) { ulong hstatus, vsstatus, prev_mode; + bool elp = false; #if __riscv_xlen == 32 bool prev_virt = (regs->mstatusH & MSTATUSH_MPV) ? TRUE : FALSE; #else @@ -100,6 +101,13 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs, if (prev_mode != PRV_S && prev_mode != PRV_U) return SBI_ENOTSUPP; + /* If extension has support for CFI, clear MPELP because redirecting to VS or (H)S */ + if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(), SBI_HART_EXT_ZISSLPCFI)) { + elp = (regs->mstatus & MSTATUS_MPELP)? true: false; + /* Since redirecting, clear mpelp unconditionally */ + regs->mstatus &= ~MSTATUS_MPELP; + } + /* If exceptions came from VS/VU-mode, redirect to VS-mode if * delegated in hedeleg */ @@ -153,6 +161,10 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs, /* Get VS-mode SSTATUS CSR */ vsstatus = csr_read(CSR_VSSTATUS); + /*if elp was set, set it back in vsstatus */ + if (elp) + vsstatus |= MSTATUS_SPELP; + /* Set SPP for VS-mode */ vsstatus &= ~SSTATUS_SPP; if (prev_mode == PRV_S) @@ -193,6 +205,10 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs, /* Clear SIE for S-mode */ regs->mstatus &= ~MSTATUS_SIE; + + /* if elp was set, set it back in mstatus */ + if (elp) + regs->mstatus |= MSTATUS_SPELP; } return 0;