From patchwork Wed Aug 5 03:29:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hongzheng-Li X-Patchwork-Id: 1341070 X-Patchwork-Delegate: atishp@atishpatra.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=h7C2oAbo; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=p/rwMgFA; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BLxwN1W3bz9s1x for ; Wed, 5 Aug 2020 13:30:38 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=h2k5tsFxCHPRk0901CmBIOXQraIY2IRiwg6tozpc8uY=; b=h7C2oAbogpiAwHw/z7zSuP/muP Dyan7r4eQJElIwB9F4rx2+Jdx+zptvWWGb12Flfb8LxO5ZiGySvDSg2qptHfMis+4SnNXTd+pgCtQ 57f6Pz2j4ji6McqHKUAJiUd6PbCwF8/3ADVBOu/jwLEzbLLxg1HAJxZ08JsWJ7SiV6PlltCnFkhRF k8bx0pJkah56WQOLH07ibyvQzQ3tTmcEEHNdUh8EwbpgnBDWPOV/U2YVb53s+yRCHgyxeRDKaqIGG WcHejmO0LJYcMbSl1IiAxG1WeKvS9054nWwc1fRnmwVLGE6KXcBQoQSoUPxNVpcgVSxGBEcIdFr9N 3s5T61UA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k3A8J-000341-A5; Wed, 05 Aug 2020 03:30:27 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k3A8H-00033T-Am for opensbi@lists.infradead.org; Wed, 05 Aug 2020 03:30:26 +0000 Received: by mail-pf1-x441.google.com with SMTP id r11so13985208pfl.11 for ; Tue, 04 Aug 2020 20:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=quuFZMuiV1np1d+smgl0Ulv9jrOvjeG8zOsZPr0plDo=; b=p/rwMgFAy9PN/8KKiNVZDPBbId0hLUVi17J0OBJDp0knCO2OGmvGO0uskpL9sV5aGc YyOjImMcGPjSzv37OA4mOuRmN0DR2HJy7/JN16+ST5GyJkOQdV18dTfqhz1LO8uUt8oH L1Labd2TSqsw/nfkYkwlXRjAQNwM61y6sZ8Sf8jNWozzzuLSpt9I+NfXOGM0lEqcLzJy PHTkSHTKo1fOXlcUQLc6fURxd7YR61KA3ZSF+oU7We1SUwaZu0RPA63ZN9q8qQD+j4Xk GvV+iaHacff5izs6gqD4DEQHAzNXuWwolUyhBIZFgWuzde6jVrUfdGcvcfll568F/qrD ihDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=quuFZMuiV1np1d+smgl0Ulv9jrOvjeG8zOsZPr0plDo=; b=BroU3PaHB01acmsoKt/+0w2/WVT6LmwUddE/a8BMbkoYSP8QlFa32wiafsIWF2Qyde HT0Wm/BU8cW/oCxX8Tt9ZyjZlJjgg8J7s39T1Z1EHwm0MaopRk4BxeI8lvKbxnPWB11G /4N79gAtBy6H2aPh7T2d9e89hNy0QE3BEuUPMkOs11VEe7nsWTQRfpFOTG4f/IT6YexI 8rgxBQXXP+thJ1wlLLhu2DPNhfQlIaR4U5/cuE9/7C0Ogxq7ZRpMxqUxMyFuQCxvXCkH 7LyWGEnLkUwVJ1mZWExDlBGb2MlK1E4LY1YyBtWMRMAHgnlTiNGNqeehhztoA7PMGnFN 0mfA== X-Gm-Message-State: AOAM530PrvxS9JFXBRktfleT5JVWCrcJp910zZxuhjUe20g9VQ6Ow3E+ 7hnN5/CB2Nqmm9qJ217LAyWiWK7WUgo= X-Google-Smtp-Source: ABdhPJyCkz21kj17vnoYD/zHDZ6KcnbL5gK2t5kilV/U6k25Masz2XIMYihN4+EsbilYIbBmhgwm+g== X-Received: by 2002:a63:1962:: with SMTP id 34mr1223878pgz.411.1596598221064; Tue, 04 Aug 2020 20:30:21 -0700 (PDT) Received: from localhost.localdomain (183178047145.ctinets.com. [183.178.47.145]) by smtp.gmail.com with ESMTPSA id t1sm863906pgq.66.2020.08.04.20.30.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Aug 2020 20:30:20 -0700 (PDT) From: Hongzheng-Li X-Google-Original-From: Hongzheng-Li To: opensbi@lists.infradead.org Subject: [PATCH] lib: add Enhanced PMP support Date: Wed, 5 Aug 2020 11:29:44 +0800 Message-Id: <20200805032944.936-1-Ethan.Lee.QNL@gmail.com> X-Mailer: git-send-email 2.24.1.windows.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200804_233025_442686_BB8C4EAC X-CRM114-Status: GOOD ( 18.80 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) 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:441 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [ethan.lee.qnl[at]gmail.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -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 -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hou Weiying , Hongzheng-Li Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Hongzheng-Li The Enhanced PMP(ePMP) is used for memory access and execution prevention on Machine mode. The latest spec can be found here: https://docs.google.com/document/d/1Mh_aiHYxemL0umN3GTTw8vsbmzHZ_nxZXgjgOUzbvc8/edit?pli=1#heading=h.ab3kl2ch725u Signed-off-by: Hongzheng-Li Signed-off-by: Hou Weiying --- include/sbi/riscv_asm.h | 4 ++++ include/sbi/riscv_encoding.h | 1 + include/sbi/sbi_hart.h | 9 ++++++--- lib/sbi/riscv_asm.c | 30 ++++++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 27 +++++++++++++++++++++++++++ lib/sbi/sbi_init.c | 1 + 6 files changed, 69 insertions(+), 3 deletions(-) diff --git a/include/sbi/riscv_asm.h b/include/sbi/riscv_asm.h index 6e093ca..badd8af 100644 --- a/include/sbi/riscv_asm.h +++ b/include/sbi/riscv_asm.h @@ -182,6 +182,10 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr, int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out, unsigned long *size); +int epmp_set(int rlb, int mmwp, int mml); + +int epmp_get(int *rlb, int *mmwp, int *mml); + #endif /* !__ASSEMBLY__ */ #endif diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h index 827c86c..2052925 100644 --- a/include/sbi/riscv_encoding.h +++ b/include/sbi/riscv_encoding.h @@ -279,6 +279,7 @@ #define CSR_PMPADDR13 0x3bd #define CSR_PMPADDR14 0x3be #define CSR_PMPADDR15 0x3bf +#define CSR_MSECCFG 0x390 #define CSR_TSELECT 0x7a0 #define CSR_TDATA1 0x7a1 #define CSR_TDATA2 0x7a2 diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h index 51c2c35..4ed882f 100644 --- a/include/sbi/sbi_hart.h +++ b/include/sbi/sbi_hart.h @@ -16,12 +16,14 @@ enum sbi_hart_features { /** Hart has PMP support */ SBI_HART_HAS_PMP = (1 << 0), + /** enhanced PMP */ + SBI_HART_HAS_EPMP = (1 << 1), /** Hart has S-mode counter enable */ - SBI_HART_HAS_SCOUNTEREN = (1 << 1), + SBI_HART_HAS_SCOUNTEREN = (1 << 2), /** Hart has M-mode counter enable */ - SBI_HART_HAS_MCOUNTEREN = (1 << 2), + SBI_HART_HAS_MCOUNTEREN = (1 << 3), /** HART has timer csr implementation in hardware */ - SBI_HART_HAS_TIME = (1 << 3), + SBI_HART_HAS_TIME = (1 << 4), /** Last index of Hart features*/ SBI_HART_HAS_LAST_FEATURE = SBI_HART_HAS_TIME, @@ -43,6 +45,7 @@ int sbi_hart_pmp_get(struct sbi_scratch *scratch, unsigned int n, unsigned long *prot_out, unsigned long *addr_out, unsigned long *size); void sbi_hart_pmp_dump(struct sbi_scratch *scratch); +void sbi_hart_epmp_dump(struct sbi_scratch *scratch); int sbi_hart_pmp_check_addr(struct sbi_scratch *scratch, unsigned long daddr, unsigned long attr); bool sbi_hart_has_feature(struct sbi_scratch *scratch, unsigned long feature); diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c index 6dfebd9..4621f70 100644 --- a/lib/sbi/riscv_asm.c +++ b/lib/sbi/riscv_asm.c @@ -349,3 +349,33 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out, return 0; } + +int epmp_set(int rlb, int mmwp, int mml) +{ + unsigned long mseccfg; + + if ((rlb | mmwp | mml) & ~1) + return SBI_EINVAL; + +#if __riscv_xlen == 32 || __riscv_xlen == 64 + mseccfg = rlb << 2 | mmwp << 1 | mml; +#else + mseccfg = -1; +#endif + if (mseccfg < 0) + return SBI_ENOTSUPP; + + csr_write(CSR_MSECCFG, mseccfg); + + return 0; +} + +int epmp_get(int *rlb, int *mmwp, int *mml) +{ + unsigned long mseccfg = csr_read(CSR_MSECCFG); + *rlb = (mseccfg >> 2) & 1; + *mmwp = (mseccfg >> 1) & 1; + *mml = mseccfg & 1; + + return 0; +} diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index fa20bd2..c1c6957 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -184,6 +184,18 @@ void sbi_hart_pmp_dump(struct sbi_scratch *scratch) } } +void sbi_hart_epmp_dump(struct sbi_scratch *scratch) +{ + if (!sbi_hart_has_feature(scratch, SBI_HART_HAS_EPMP)) + return; + + int rlb, mmwp, mml; + + epmp_get(&rlb, &mmwp, &mml); + sbi_printf("ePMP: RLB = %d, MMWP = %d, MML = %d\n", + rlb, mmwp, mml); +} + int sbi_hart_pmp_check_addr(struct sbi_scratch *scratch, unsigned long addr, unsigned long attr) { @@ -239,6 +251,9 @@ static int pmp_init(struct sbi_scratch *scratch, u32 hartid) */ pmp_set(pmp_idx++, PMP_R | PMP_W | PMP_X, 0, __riscv_xlen); + if (sbi_hart_has_feature(scratch, SBI_HART_HAS_EPMP)) + epmp_set(0, 0, 0); + return 0; } @@ -288,6 +303,9 @@ static inline char *sbi_hart_feature_id2string(unsigned long feature) case SBI_HART_HAS_TIME: fstr = "time"; break; + case SBI_HART_HAS_EPMP: + fstr = "epmp"; + break; default: break; } @@ -397,6 +415,15 @@ static void hart_detect_features(struct sbi_scratch *scratch) hfeatures->features |= SBI_HART_HAS_MCOUNTEREN; } + /* Detect if hart supports EPMP(enhanced PMP) feature */ + trap.cause = 0; + val = csr_read_allowed(CSR_MSECCFG, (unsigned long)&trap); + if (!trap.cause) { + csr_write_allowed(CSR_MSECCFG, (unsigned long)&trap, val); + if (!trap.cause) + hfeatures->features |= SBI_HART_HAS_EPMP; + } + /* Detect if hart supports time CSR */ trap.cause = 0; csr_read_allowed(CSR_TIME, (unsigned long)&trap); diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c index a7fb848..1ced76b 100644 --- a/lib/sbi/sbi_init.c +++ b/lib/sbi/sbi_init.c @@ -82,6 +82,7 @@ static void sbi_boot_prints(struct sbi_scratch *scratch, u32 hartid) sbi_hart_delegation_dump(scratch); sbi_hart_pmp_dump(scratch); + sbi_hart_epmp_dump(scratch); } static spinlock_t coldboot_lock = SPIN_LOCK_INITIALIZER;