From patchwork Thu Feb 9 04:26:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 1739788 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=frCPxTx8; 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=JhMlqEjm; 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 4PC3k53nHCz23yF 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=/aEJ2voSO246KAjHCp9KyNX8KfEZ0Tr/M0ruHrIqyus=; b=frCPxTx8sqMgTn /AgQAufFBBKj4aoqk3XxzcI3peTq9s4OwayznBUl29LHg4GSo6c2ySaCDNMC18PnakuIVhWfCcpxo BFzNvXywIDwbkJMftnw7o+dP+G1NnoIiXtkcjFLa2CbUDgXaTXdri3VkEorm+Ik6bMZ0xLAtg7pr8 1BX8EaHO8Xy5c9aanPCcpWin+YHVGtuswB6STR6yJMqQDLdXsSpA4pQ8g46lrfjqSdIkaGafzRBZi +CF9p2cYjOkkeOp/doWUAaKjnDa68NPnMLgqHqHjkvV6LsVUFpUyibc4fjAsJLtw/1kn1a4jUezhu EdENRuloc3coosAXZaAg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPyWb-00086g-IM; Thu, 09 Feb 2023 04:27:09 +0000 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPyWZ-00084a-3T for opensbi@lists.infradead.org; Thu, 09 Feb 2023 04:27:08 +0000 Received: by mail-pj1-x1031.google.com with SMTP id ki19-20020a17090ae91300b00232cba666bbso1357966pjb.2 for ; Wed, 08 Feb 2023 20:27:01 -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=UWmPhqFWyPLBLWoZ5i9Ml65b5XHRgcJnDsM2qndZ8a4=; b=JhMlqEjmbAaTasVbss0Qpxj6GdhulhH2IQiZccJVP1SBG4g2dKjZb3xKLDJWDAevuF ugS1F59dSYjtOaDDhNYyHKKGX9XP6elixWLh/7g4DtvYw6SivgdxBDMW+0RWA4Oub860 Sa/QjL7fl4yzPLInCukntx8d6t1ylCfrwtHmffwc2MuWXDCTC9HRyF2ObANls0wPULSR IrNxJQ7q7ZPEEFhmCf9dBmYgvIHu+r58ItVe9YtGg4QuOPQ27MiKdr6oFYzJcNSl2A2a 8hkNen/EQyHIKbfKjCUcHyaC3mCYwgyX7l3GJ+arWRvUjD6QG6KOSQDc9ilEAg7hdFv/ WVHw== 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=UWmPhqFWyPLBLWoZ5i9Ml65b5XHRgcJnDsM2qndZ8a4=; b=tmUtpeJOSX8jPfujqxjZ5hKtQ8fC6b6fJMYyOjhPWk71F6lUNuh6baA/oabRLgXqHY cwz9JjFzAnsZthySM6FJizXyIXXgzWlaWLo4lfCwFhUz2AdUrajpJJeRHjJktZ0wNG/q 6NLSwU/QqQaqNoC1es1+ODbS3+oPUui9e2UjUyeKWGmCjBZByS+nO4OkLE7ENG2LOAuj aZ+yGQ7NmRh2Bo43K4xcTk35AysIX5LHubhgkrw0KGSvjITDFzubLsQ5IsrXFrrd8mF1 bLNVqoG/tEmjxxQNFve8zE4cQ611jWUwxJfNywSOpE7Azgp0o5sCrnzVtjZKg8PqXCwF 98mA== X-Gm-Message-State: AO0yUKV3rltsT4IKoeta40j8oH/yuyZ5B53W4EA2RccpsNpUBkrBDnUQ kMxjSiIyM1Cz433Gypjdl9fHs1bDfPdlIwKh X-Google-Smtp-Source: AK7set9379ct7v27GphhF2TAGui0lqN/vLoXze65IB0ZSYIKzfUXVgVpmpJeH/9elw+JvyeLeWtUrA== X-Received: by 2002:a05:6a20:394f:b0:be:df22:96ca with SMTP id r15-20020a056a20394f00b000bedf2296camr12308690pzg.25.1675916820431; Wed, 08 Feb 2023 20:27:00 -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.26.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 20:26:59 -0800 (PST) From: Deepak Gupta To: opensbi@lists.infradead.org Cc: Deepak Gupta Subject: [PATCH v3 Zisslpcfi 1/2] include: adding support for Zisslpcfi encodings Date: Wed, 8 Feb 2023 20:26:53 -0800 Message-Id: <20230209042654.3568990-2-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_202707_161213_B31CE09D X-CRM114-Status: UNSURE ( 8.59 ) X-CRM114-Notice: Please train this message. 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: Zisslpcfi extension (see link) introduces b60 (CFI) in menvcfg CSR to enable shadow stack and landing pad for machine and lesser privileged modes. Additionally extension introduces new bits in xstatus [...] 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:1031 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 Zisslpcfi extension (see link) introduces b60 (CFI) in menvcfg CSR to enable shadow stack and landing pad for machine and lesser privileged modes. Additionally extension introduces new bits in xstatus for cfi state. Link: https://github.com/riscv/riscv-cfi Signed-off-by: Deepak Gupta --- changelog v1 --> v3: - updated with correct name for extension "zisslpcfi" - updated encodings.h with additional csr bit definitions. --- include/sbi/riscv_encoding.h | 10 ++++++++++ include/sbi/sbi_hart.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h index b0f08c8..9c45724 100644 --- a/include/sbi/riscv_encoding.h +++ b/include/sbi/riscv_encoding.h @@ -32,6 +32,10 @@ #define MSTATUS_TVM _UL(0x00100000) #define MSTATUS_TW _UL(0x00200000) #define MSTATUS_TSR _UL(0x00400000) +#define MSTATUS_UFCFIEN _UL(0x00800000) +#define MSTATUS_UBCFIEN _UL(0x01000000) +#define MSTATUS_SPELP _UL(0x02000000) +#define MSTATUS_MPELP _UL(0x04000000) #define MSTATUS32_SD _UL(0x80000000) #if __riscv_xlen == 64 #define MSTATUS_UXL _ULL(0x0000000300000000) @@ -210,9 +214,13 @@ #if __riscv_xlen > 32 #define ENVCFG_STCE (_ULL(1) << 63) #define ENVCFG_PBMTE (_ULL(1) << 62) +#define ENVCFG_CFI (_ULL(1) << 60) +#define ENVCFG_SFCFIEN (_ULL(1) << 59) #else #define ENVCFGH_STCE (_UL(1) << 31) #define ENVCFGH_PBMTE (_UL(1) << 30) +#define ENVCFGH_CFI (_UL(1) << 28) +#define ENVCFGH_SFCFIEN (_UL(1) << 27) #endif #define ENVCFG_CBZE (_UL(1) << 7) #define ENVCFG_CBCFE (_UL(1) << 6) @@ -229,6 +237,8 @@ #define CSR_USTATUS 0x000 #define CSR_UIE 0x004 #define CSR_UTVEC 0x005 +#define CSR_LPLR 0x006 +#define CSR_SSP 0x020 /* User Trap Handling (N-extension) */ #define CSR_USCRATCH 0x040 diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h index 95b40e7..d153220 100644 --- a/include/sbi/sbi_hart.h +++ b/include/sbi/sbi_hart.h @@ -36,6 +36,8 @@ enum sbi_hart_extensions { SBI_HART_EXT_SMSTATEEN, /** HART has Sstc extension */ SBI_HART_EXT_SSTC, + /** HART has zisslpcfi extension */ + SBI_HART_EXT_ZISSLPCFI, /** Maximum index of Hart extension */ SBI_HART_EXT_MAX, 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;