From patchwork Thu Nov 24 12:39:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13054936 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 30D92C4321E for ; Thu, 24 Nov 2022 12:45:31 +0000 (UTC) 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=+3kMOzDj3bJULWfxOsqkqhYhQ8YZH6fcMRgnf8HTPew=; b=IQdl5MbBmYVsKY zaTfxOicUwZzvRYX3YFFl2kulKp+ALMf0WedVZKPLLwz39wDMsZ1cmQD3VLl7IiRN0dmONaThJpeP fMbdkOEQsgxHcOGcqybthwRqqCkDNFoY1eQMq5zaVYt/gxsetaF79rlAbUhqfsifvi20EXAdfZqMi Q63Af4RouvDisFq1BUjs2WNMvMKe39GLGx/I9AAYtM2uVeOT5O3Bggo1v6tuZgQveRxVvB0fuGkHr U5R481MgQAixv6x3VeOpDxYGfsKC94UhxwZObAi+27y+zg24ZoF+ULP4DaFzbFWecX5v7UxhiW3rT g44K4EH3zCETuJaXDhnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oyBaW-008QlL-Aa; Thu, 24 Nov 2022 12:44:20 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oyBWg-008OXK-Qi for linux-arm-kernel@lists.infradead.org; Thu, 24 Nov 2022 12:40:24 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 689816210B; Thu, 24 Nov 2022 12:40:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EE2DC433D6; Thu, 24 Nov 2022 12:40:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669293621; bh=pF90lXgtccxdoLzmn1+lmWSi1OrSbQLggAaRT4/wiFg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dMaBx9UUWz/AW7Q+vBLPDVtT/EcnitGgq4HVadszdmuBc9kfJufdth7GR5usENhKT dnMW64QzMzhxEb3ow7IlMiZYkMDOjglrTNAQ36oa1eYwmyG6fFtsXaK3YjAMZZ4YVh +e0EhpFupX/dWDiftpdB4Vie5TR9Elf0gRByAbWRdWpvWGWNWOYulI/G7CB8K82BbY U0Z6LURrIcAnNcF2r9m02mXHQhKoHJrX3hNqzAnsuMuDf+jlAV0Y9k7W3Eb+B49msC vtaxZLnyHj38STLly6IuHNNEZwMpLwFZ41OLQWrXEUcrkC2Zfi5akU6lipJI09Evws /4jX43Q695Omw== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Marc Zyngier , Will Deacon , Mark Rutland , Kees Cook , Catalin Marinas , Mark Brown , Anshuman Khandual , Richard Henderson , Ryan Roberts Subject: [PATCH v2 10/19] arm64: mm: Wire up TCR.DS bit to PTE shareability fields Date: Thu, 24 Nov 2022 13:39:23 +0100 Message-Id: <20221124123932.2648991-11-ardb@kernel.org> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog In-Reply-To: <20221124123932.2648991-1-ardb@kernel.org> References: <20221124123932.2648991-1-ardb@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221124_044022_987026_542E3810 X-CRM114-Status: GOOD ( 18.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When LPA2 is enabled, bits 8 and 9 of page and block descriptors become part of the output address instead of carrying shareability attributes for the region in question. So avoid setting these bits if TCR.DS == 1, which means LPA2 is enabled. Signed-off-by: Ard Biesheuvel --- arch/arm64/Kconfig | 4 ++++ arch/arm64/include/asm/pgtable-prot.h | 18 ++++++++++++++++-- arch/arm64/mm/mmap.c | 4 ++++ arch/arm64/mm/proc.S | 8 ++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 170832f31eff..6d299c6c0a56 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -1264,6 +1264,10 @@ config ARM64_PA_BITS default 48 if ARM64_PA_BITS_48 default 52 if ARM64_PA_BITS_52 +config ARM64_LPA2 + def_bool y + depends on ARM64_PA_BITS_52 && !ARM64_64K_PAGES + choice prompt "Endianness" default CPU_LITTLE_ENDIAN diff --git a/arch/arm64/include/asm/pgtable-prot.h b/arch/arm64/include/asm/pgtable-prot.h index 9b165117a454..269584d5a2c0 100644 --- a/arch/arm64/include/asm/pgtable-prot.h +++ b/arch/arm64/include/asm/pgtable-prot.h @@ -40,6 +40,20 @@ extern bool arm64_use_ng_mappings; #define PTE_MAYBE_NG (arm64_use_ng_mappings ? PTE_NG : 0) #define PMD_MAYBE_NG (arm64_use_ng_mappings ? PMD_SECT_NG : 0) +#ifndef CONFIG_ARM64_LPA2 +#define lpa2_is_enabled() false +#define PTE_MAYBE_SHARED PTE_SHARED +#define PMD_MAYBE_SHARED PMD_SECT_S +#else +static inline bool __pure lpa2_is_enabled(void) +{ + return read_tcr() & TCR_DS; +} + +#define PTE_MAYBE_SHARED (lpa2_is_enabled() ? 0 : PTE_SHARED) +#define PMD_MAYBE_SHARED (lpa2_is_enabled() ? 0 : PMD_SECT_S) +#endif + /* * If we have userspace only BTI we don't want to mark kernel pages * guarded even if the system does support BTI. @@ -50,8 +64,8 @@ extern bool arm64_use_ng_mappings; #define PTE_MAYBE_GP 0 #endif -#define PROT_DEFAULT (_PROT_DEFAULT | PTE_MAYBE_NG) -#define PROT_SECT_DEFAULT (_PROT_SECT_DEFAULT | PMD_MAYBE_NG) +#define PROT_DEFAULT (PTE_TYPE_PAGE | PTE_MAYBE_NG | PTE_MAYBE_SHARED | PTE_AF) +#define PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_MAYBE_NG | PMD_MAYBE_SHARED | PMD_SECT_AF) #define PROT_DEVICE_nGnRnE (PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_DEVICE_nGnRnE)) #define PROT_DEVICE_nGnRE (PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_DEVICE_nGnRE)) diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index 8f5b7ce857ed..adcf547f74eb 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -73,6 +73,10 @@ static int __init adjust_protection_map(void) protection_map[VM_EXEC | VM_SHARED] = PAGE_EXECONLY; } + if (lpa2_is_enabled()) + for (int i = 0; i < ARRAY_SIZE(protection_map); i++) + pgprot_val(protection_map[i]) &= ~PTE_SHARED; + return 0; } arch_initcall(adjust_protection_map); diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S index 873adaccb12f..0f576d72b847 100644 --- a/arch/arm64/mm/proc.S +++ b/arch/arm64/mm/proc.S @@ -297,6 +297,14 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) mov temp_pte, x5 mov pte_flags, #KPTI_NG_PTE_FLAGS +#ifdef CONFIG_ARM64_LPA2 + /* Omit PTE_SHARED from pte_flags if LPA2 is enabled */ + mrs x5, tcr_el1 + tst x5, #TCR_DS + mov x5, #KPTI_NG_PTE_FLAGS & ~PTE_SHARED + csel pte_flags, x5, pte_flags, ne +#endif + /* Everybody is enjoying the idmap, so we can rewrite swapper. */ /* PGD */ adrp cur_pgdp, swapper_pg_dir