From patchwork Thu Jul 6 10:49:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Himanshu Chauhan X-Patchwork-Id: 1804281 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=Ri0RE5Si; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=LXHlW/eN; 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 4QxYFv6wlgz20bQ for ; Thu, 6 Jul 2023 20:50:11 +1000 (AEST) 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=A4ERhyb7oZRLbhiC6vxSdPzmgVkZ3/7I55BNyjetYgY=; b=Ri0RE5SiIPG388 ewL2WRuTmK41VVphcqE3Rh4YmExvGQwq3bcYUnFKttwgFBG2PrXYCDS2/cSL0cMw5gnxNQm3KpCBd NQFfWOdKYgibT7RhCil7AN2/vg0xPCK78GtcHXeZiBHT8i2RbMDqjp9B49fKxbsRoNtLQtpYo2CQf OIhPkiXWmHBkrwICPUJY5Z2qKFeU/z8eUNtQSBd4Y+fEqrCJH1CuJR18DshirZVSN9KKhjKwu83Sg 9ycdM6nHsAgSFsR2Ck4otGI8cH/pB5rg6LF9dejJebjFh094MJmjVDetvHcWOXt7n38pvkmqK9CV3 PnHeOvAUJiMZCmMW7eAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHMYb-001Gma-0h; Thu, 06 Jul 2023 10:49:53 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qHMYW-001GhY-38 for opensbi@lists.infradead.org; Thu, 06 Jul 2023 10:49:50 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-262d096149cso138978a91.1 for ; Thu, 06 Jul 2023 03:49:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1688640588; x=1691232588; 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=WPEDVKYV+QDkfxQjy7At7F7MyYyXDRqEUMbJj4BSYRk=; b=LXHlW/eNZYsIQ8sKlsZcvPzPhR2ET3zHS6+A99BsbBJfhf0twRnNY5o7n5AemYYJqM Kmck1O4urack2Awc7zDlg+F2BTPddhwfc05G3bM1kZfDaQDS4XQ0HSOtkDdvJ6GpXE3o bbBMj3oImhcofnNgwOlOIcrPcPLoaWLdSpTG056Qx5VWcZ6iNbSyBUeiU8bITlXCBDxe bkULfdKJtpXCVFhvk8yNYR8yDSQ39znZ9ABRvLAEz3IASamGMKHBhG/1U4RepXaAoRcI /GBnZ6PC821x8myzUt52BCcquaG2blwA4Z2XmqlDx16dFEKhZ7p/1uc5wgiKbJ2l+8Uj 6Iyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688640588; x=1691232588; 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=WPEDVKYV+QDkfxQjy7At7F7MyYyXDRqEUMbJj4BSYRk=; b=KfJhlRkLpOjPpaWgr3Cku/E6mmu0uVO689zs3k5NWjRcUXSd1qkZNQeitI8CNhE6c5 W0QOdez57IEJgsP/pZ2D1W//Skq9ihqDn3le38++VGZKGEV3J7kSlRHB826JWjnmDWx0 8psdYtolL0G2YjfXCiBqVIrPH2RU6GgpJ83M8mr14+76qZ46xLaTrT9OOrveaBuiXgQX UbVEu+Oc6Cc5USj9qtdpzIG8YBxoc+3STz3O/OHERnObJf36hER8qfcU9yCjTVrhvIAP jpoxjmm3VHxB5LPWmh3oHNsKnkmDHNwvINy32YczAtrQs1Wql+qgEBNVC2YX9xrUj2i+ //VQ== X-Gm-Message-State: ABy/qLaBc6ae6w17Y8JLr9/qT0EgJc1xP73Q+tP/lEHI7lbOP/ecsLhX mDQMNrEPLpA7FQyWKidW4lLpPVQrvtrnoyyMWzbov/r4 X-Google-Smtp-Source: APBJJlEehVxFsr+cy7sCmSxqzPcDm+rj0ZTVMP7sD7eID4r6uiarQz66sr03SDoNztVGv41FT2yf4g== X-Received: by 2002:a17:90a:fb49:b0:263:a5b5:c538 with SMTP id iq9-20020a17090afb4900b00263a5b5c538mr1376270pjb.4.1688640588001; Thu, 06 Jul 2023 03:49:48 -0700 (PDT) Received: from saptrishi.. ([2405:201:d019:cbd3:9126:16c2:7e:c7e5]) by smtp.gmail.com with ESMTPSA id gn20-20020a17090ac79400b002635341a7e8sm3670746pjb.3.2023.07.06.03.49.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jul 2023 03:49:47 -0700 (PDT) From: Himanshu Chauhan To: opensbi@lists.infradead.org Cc: Himanshu Chauhan Subject: [PATCH v2 7/8] lib: sbi: Add functions to map/unmap shared memory Date: Thu, 6 Jul 2023 16:19:27 +0530 Message-Id: <20230706104928.3391947-8-hchauhan@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230706104928.3391947-1-hchauhan@ventanamicro.com> References: <20230620143212.2701598-1-hchauhan@ventanamicro.com> <20230706104928.3391947-1-hchauhan@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230706_034949_029794_306ABA2B X-CRM114-Status: GOOD ( 13.92 ) X-Spam-Score: -0.2 (/) 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: When Smepmp is enabled, M-mode will need to map/unmap the shared memory before it can read/write to it. This patch adds functions to create dynamic short-lived mappings. Signed-off-by: Himanshu Chauhan --- include/sbi/sbi_hart.h | 2 ++ lib/sbi/sbi_hart.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) Content analysis details: (-0.2 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:1029 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_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.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 When Smepmp is enabled, M-mode will need to map/unmap the shared memory before it can read/write to it. This patch adds functions to create dynamic short-lived mappings. Signed-off-by: Himanshu Chauhan Reviewed-by: Anup Patel --- include/sbi/sbi_hart.h | 2 ++ lib/sbi/sbi_hart.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h index fb5bb59..975966d 100644 --- a/include/sbi/sbi_hart.h +++ b/include/sbi/sbi_hart.h @@ -88,6 +88,8 @@ unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch); unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch); unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch); int sbi_hart_pmp_configure(struct sbi_scratch *scratch); +int sbi_hart_map_saddr(unsigned long base, unsigned long size); +int sbi_hart_unmap_saddr(void); int sbi_hart_priv_version(struct sbi_scratch *scratch); void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch, char *version_str, int nvstr); diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 5d91718..936dbda 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -347,6 +347,48 @@ unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch, return pmp_flags; } +int sbi_hart_map_saddr(unsigned long addr, unsigned long size) +{ + /* shared R/W access for M and S/U mode */ + unsigned int pmp_flags = (PMP_W | PMP_X); + unsigned long order, base = 0; + struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); + + /* If Smepmp is not supported no special mapping is required */ + if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP)) + return SBI_OK; + + if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY)) + return SBI_ENOSPC; + + for (order = log2roundup(size) ; order <= __riscv_xlen; order++) { + if (order < __riscv_xlen) { + base = addr & ~((1UL << order) - 1UL); + if ((base <= addr) && + (addr < (base + (1UL << order))) && + (base <= (addr + size - 1UL)) && + ((addr + size - 1UL) < (base + (1UL << order)))) + break; + } else { + return SBI_EFAIL; + } + } + + pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order); + + return SBI_OK; +} + +int sbi_hart_unmap_saddr(void) +{ + struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); + + if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP)) + return SBI_OK; + + return pmp_disable(SBI_SMEPMP_RESV_ENTRY); +} + int sbi_hart_pmp_configure(struct sbi_scratch *scratch) { struct sbi_domain_memregion *reg;