From patchwork Wed Oct 21 00:57:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 1385266 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=wdc.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=y264mgxl; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.a=rsa-sha256 header.s=dkim.wdc.com header.b=KMb+6dwT; 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 4CGBtp6N5Vz9sSG for ; Wed, 21 Oct 2020 11:58:10 +1100 (AEDT) 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:References:In-Reply-To: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:List-Owner; bh=GESolum5+7amxUS8sYGvUf2X9+EjuCsgZmEeroNYSK8=; b=y264mgxlnY7Uaxue1LgbzAUeV QvfpiQik/0tZUBT2wwcHGZHfRfe5WlBx+CnIZI/nNVmgYGirbKWqINN2xh0W4U7Sq1HM1pNl1Tc8P O6bTIlcYMzYflZVN2MQ7h6ZmFomzQDSbor7BPG7URDa+trO/khzZzhplS4OgpCKmupp9BPfkNPoxi FK6xj64fDl7UkdAbpGTp9A6jyUWVhyvv/Lmlw1EwIwWdfVuYXl7LcXVnNxCrqrWRExDVZXFrDqNF+ wEjRymTfcqignjKiQIckd44qWh095pZMqub4KSVzHZj92I3mheMJBxcMO3Z6Q2cq4AWHFmerTHUvi w34Uv31+w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kV2S3-0007dJ-6f; Wed, 21 Oct 2020 00:58:03 +0000 Received: from esa1.hgst.iphmx.com ([68.232.141.245]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kV2S1-0007bh-0J for opensbi@lists.infradead.org; Wed, 21 Oct 2020 00:58:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1603241880; x=1634777880; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fY1F84Uc0tibfywy3EEwkLPXJjuJgcQmdfytITBiNTc=; b=KMb+6dwTv1TODTg0ACuLM39MJFdSO+Ltyo0gBblekcSntdkuof7MTAgC NqjRmjVz8wMHxUsGK9YBfx/thKZ9GLuZVO7DHStB/9Jd74eUnXaMnVn9x zInSvgZcZuUnECjwzfLackCu3BXBI//LkXlNr08QdTzmi7jEuHCMR/E/e 5f0/6xOgw82lq+Vv8d42Y94LH2Q31AaSaCJi4E/LFzWvpHl+7OindrIQN l4NXCRo1hOeR3qcDUDkcR+/mLKDtdUzLDfflv+0hW5uP0M7q8DaZSHqVZ 0GEYa3hdqjtJW6rxkZAYESdxsn6GxLVcwvNjHexcZq7J+ZwYVesn1teRq Q==; IronPort-SDR: nZI4JYThRlvHNcXFKFiJksSyuJff8EDjhRSSXDy07xvHMuF1vKpXlzV2fdx99TzzgRrP29hzyN xs2XmhQ7yRxVKtZFp/JFXSSSLHH64f0CnR9pUyMdCggvIvZLgYRRcZ56U3FJHrDPPzd66wlu5C 5T9DNGHo+so+FddQMTJjmKP3BkXdO8BTlyoHXhFHFnDRjX8XhGcHYd85wosMMQ/q0DUNJuMxRM w8uXJn3ud+Gme0fMNr2/9Un0OyJGJKyYBrrd6jweWgPiu7AO3av253LyQPD6SEz8eLTOMrvLDh EA8= X-IronPort-AV: E=Sophos;i="5.77,399,1596470400"; d="scan'208";a="260357388" Received: from uls-op-cesaip01.wdc.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 21 Oct 2020 08:57:56 +0800 IronPort-SDR: tpdYCQeh9EK8UyaI4z3qbO34tP5PbLjarcvNjWiII10vza1cDAdTUiJvs4dDgEJkxpYw+3crOY R5XbDRvf69Cjzm2vORdcY368LQWb1OdHjTSxgDRgwzS2BYN6axIMiEbtyPEesmj8sSV5vwwDoi Nbq/etgYBJw/18G73+t+CXN3nycOlhoPzNyn6dlSo4XyXjWqSAJZ38m6gMRyLA76boaScT87Kt RmtYRlV/FtsLb/4NU25FjFJFCQw4bxtHOEwPgN/KKoe3vVLc+HDrdyf1bINfc6vT+TMP5LzI01 CgEKDj/WvWQnMy/mjzMyooK3 Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2020 17:44:22 -0700 IronPort-SDR: CQK9wXyb7RrP0o1pdltCcj7uuF7ORch6KUpRB0cXGkCQAqC/HBwmww+XOQygjRy59auON5YsdK 8S0BTs+VkJWjfbZJ/lV3IXYvcYLZ0LZlkYBRD6n3X/YcAvp7L7xi+T+Ah5tnxG0+dC9cSbQn+U K/aOgNdMegArHefEE+H5E9VoAJMXtIw4fwJeEE3HTvrTXbk6vxCr+QotS+wGi2AyIG4lvRJwaV FnixzkWlazR8Q/A+i/jC9lNQlIMXFbbugM8hQoq/BpYlJL/wD8Abu60FDF4LHFNDM4/W+OGaLG mtI= WDCIronportException: Internal Received: from usa003000.ad.shared (HELO jedi-01.hgst.com) ([10.86.60.38]) by uls-op-cesaip02.wdc.com with ESMTP; 20 Oct 2020 17:57:57 -0700 From: Atish Patra To: opensbi@lists.infradead.org Subject: [PATCH v2 1/2] lib: sbi: Check pmp range granularity and size before setting pmp Date: Tue, 20 Oct 2020 17:57:53 -0700 Message-Id: <20201021005754.2386272-2-atish.patra@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201021005754.2386272-1-atish.patra@wdc.com> References: <20201021005754.2386272-1-atish.patra@wdc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201020_205801_180608_288CFB6D X-CRM114-Status: GOOD ( 16.08 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [68.232.141.245 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from 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_AU Message has a valid DKIM or DK signature from author's 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: Atish Patra , anup.patel@wdc.com Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org As per RISC-V privilege specification, a platform may choose to implement a coarser granularity scheme for PMP addresses. In that case, we shouldn't allow any pmp region size smaller than the platform supports. A platform may not also implement all the bits for a PMP address specified in the priv specification. The pmp range granularity and size is dynamically detected while detecting pmp support. Any pmp modification request beyond these values will not succeed. Signed-off-by: Atish Patra --- include/sbi/sbi_hart.h | 2 ++ lib/sbi/sbi_hart.c | 56 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h index 3ac150036c79..c1baae57d898 100644 --- a/include/sbi/sbi_hart.h +++ b/include/sbi/sbi_hart.h @@ -39,6 +39,8 @@ unsigned int sbi_hart_mhpm_count(struct sbi_scratch *scratch); void sbi_hart_delegation_dump(struct sbi_scratch *scratch, const char *prefix, const char *suffix); unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch); +unsigned long sbi_hart_pmp_gran(struct sbi_scratch *scratch); +unsigned int sbi_hart_pmp_sz_bits(struct sbi_scratch *scratch); int sbi_hart_pmp_configure(struct sbi_scratch *scratch); bool sbi_hart_has_feature(struct sbi_scratch *scratch, unsigned long feature); void sbi_hart_get_features_str(struct sbi_scratch *scratch, diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 8ded82841d82..5acb02aecc11 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -30,6 +30,8 @@ void (*sbi_hart_expected_trap)(void) = &__sbi_expected_trap; struct hart_features { unsigned long features; unsigned int pmp_count; + unsigned int pmp_sz_bits; + unsigned long pmp_gran; unsigned int mhpm_count; }; static unsigned long hart_features_offset; @@ -159,16 +161,36 @@ unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch) return hfeatures->pmp_count; } +unsigned long sbi_hart_pmp_gran(struct sbi_scratch *scratch) +{ + struct hart_features *hfeatures = + sbi_scratch_offset_ptr(scratch, hart_features_offset); + + return hfeatures->pmp_gran; +} + +unsigned int sbi_hart_pmp_sz_bits(struct sbi_scratch *scratch) +{ + struct hart_features *hfeatures = + sbi_scratch_offset_ptr(scratch, hart_features_offset); + + return hfeatures->pmp_sz_bits; +} + int sbi_hart_pmp_configure(struct sbi_scratch *scratch) { struct sbi_domain_memregion *reg; struct sbi_domain *dom = sbi_domain_thishart_ptr(); - unsigned int pmp_idx = 0, pmp_flags; + unsigned int pmp_idx = 0, pmp_flags, pmp_bits; unsigned int pmp_count = sbi_hart_pmp_count(scratch); + unsigned long pmp_addr, pmp_gran_log2; if (!pmp_count) return 0; + pmp_gran_log2 = log2roundup(sbi_hart_pmp_gran(scratch)); + pmp_bits = sbi_hart_pmp_sz_bits(scratch); + sbi_domain_for_each_memregion(dom, reg) { if (pmp_count <= pmp_idx) break; @@ -183,7 +205,9 @@ int sbi_hart_pmp_configure(struct sbi_scratch *scratch) if (reg->flags & SBI_DOMAIN_MEMREGION_MMODE) pmp_flags |= PMP_L; - pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order); + pmp_addr = reg->base >> PMP_SHIFT; + if (pmp_gran_log2 <= reg->order && pmp_addr < (1UL << pmp_bits)) + pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order); } return 0; @@ -282,11 +306,26 @@ done: sbi_strncpy(features_str, "none", nfstr); } +static unsigned long hart_pmp_get_allowed_addr(void) +{ + unsigned long val = 0; + struct sbi_trap_info trap = {0}; + + csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK); \ + if (!trap.cause) { + val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap); + if (trap.cause) + val = 0; + } + + return val; +} + static void hart_detect_features(struct sbi_scratch *scratch) { struct sbi_trap_info trap = {0}; struct hart_features *hfeatures; - unsigned long val; + unsigned long val, msb, lsb; /* Reset hart features */ hfeatures = sbi_scratch_offset_ptr(scratch, hart_features_offset); @@ -332,8 +371,15 @@ static void hart_detect_features(struct sbi_scratch *scratch) __check_csr_32(__csr + 0, __rdonly, __wrval, __field, __skip) \ __check_csr_32(__csr + 32, __rdonly, __wrval, __field, __skip) - /* Detect number of PMP regions */ - __check_csr_64(CSR_PMPADDR0, 0, 1UL, pmp_count, __pmp_skip); + /* Detect number of PMP regions. At least PMPADDR0 should be implemented*/ + val = hart_pmp_get_allowed_addr(); + if (val) { + lsb = __ffs(val); + msb = __fls(val); + hfeatures->pmp_gran = 1 << (lsb + 2); + hfeatures->pmp_sz_bits = msb + 1; + __check_csr_64(CSR_PMPADDR0, 0, val, pmp_count, __pmp_skip); + } __pmp_skip: /* Detect number of MHPM counters */