From patchwork Tue Oct 18 11:04:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13010265 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 DA083C4332F for ; Tue, 18 Oct 2022 11:06:03 +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=ZHnd1YmavKbum+9w8sY7Yayl05m/Y66jQegf9O/YJUs=; b=gwFj4MIB4aaI8V cSBH2RVHrOn9UAoRQhnzh1zrxDLjrX/ECgA4JoU9NA4AOEVPgWr3YwI/0JymnzO5p4omhhUnpXiSl HJU3LN0aZFe/dULYwtKEDmAsm2nP6va6JCwFeDogafSjuvHAC64H3h/HjMxcXZnn1soMc72cdXZ7I wVYstkuAGyKThyi/jq+PBsTd19n5o/+aCSl62VJw1wHfGe4os7qCmmNRuHO+mOHhRUfuswaNUQJg1 oJEMPdEdGguUD3T4qGAn078wglZalO3zVyeBNLeuevV4loPfVXoMVnmyAFDuUibg6VJ4Th55uw4eh +zalJFz6unV8psjZ/A6A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPA-005yTH-DE; Tue, 18 Oct 2022 11:05:04 +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 1okkP0-005yD5-7E for linux-arm-kernel@lists.infradead.org; Tue, 18 Oct 2022 11:04:55 +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 969B361520; Tue, 18 Oct 2022 11:04:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 47ED4C43470; Tue, 18 Oct 2022 11:04:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666091093; bh=fOnIdv0Xyy55Ww2CKD9LOztyluMCPv+3nJyI3YpsVF0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HdFogkM/YvR0ICZqmNuKMnJRUxIg05fqAyNB8X53twaIVCGy0rpvrArb+RjqxP0qQ yWosl6raCPKzhdJYhFxPxn8upliX5U7PdmU4Hew+GXC9HkMziehh8/joi4MRjKyYyp 0rNAW3WUH9pGE/x8KwfI4B3yO9pUOW6msC921tl99n54CVnjD3SIVZrS7rytWeFfSf PBcwCFosvZ6XatU1M3EBVQcPHlyqKXDOxaraurQde+BnZLUw5CPaXNTWktS5qXEh7Q saPYDWvCN3NUpZ1AFSmBDxgE89e8ITDr9kbtcbcNdC8rL2PYWcb8VO7KJv7jA/3kW7 IC/XATeC4e+5w== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, keescook@chromium.org, Ard Biesheuvel , Will Deacon , Catalin Marinas , Marc Zyngier , Mark Rutland Subject: [PATCH v4 1/6] arm64: lds: reduce effective minimum image alignment to 64k Date: Tue, 18 Oct 2022 13:04:36 +0200 Message-Id: <20221018110441.3855148-2-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018110441.3855148-1-ardb@kernel.org> References: <20221018110441.3855148-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5081; i=ardb@kernel.org; h=from:subject; bh=fOnIdv0Xyy55Ww2CKD9LOztyluMCPv+3nJyI3YpsVF0=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTohAHq6+KfeoPUmam6DRiCZqZah9LWBjKGkmSMCj lX/kwg2JAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY06IQAAKCRDDTyI5ktmPJDZ9DA C/xJIV+LWKu7IwFMZnGt8Sb7I5n0+z0K/H30/w9+y9UNKq/u1NK41hFtPAUwW9LX8Ol3vTpKNVsZyn s/Rz/zbNWjA3awb81VPt9fr3J0FSI9lG1yQd8RzEUoU514h1Wh9jwPyM45rDbDo0ZhU2YbPDNFvvGK 68xPQoUT6ez1Q4L7Rf8tyYPUc9PlMJy7Eh2JwPOP+j8Garmmv78wx7EORedFadIGfaSySh6zIRDLpV uGrECP75CJojvdDU5sVl04CVF/h5kkLP9ZCd1fD8WwrhxdORWLW6RH54H9qzJ6Lds64FDWZo5PRn1I E/fpZB89cyAuPZe8Z6EGN5QdiM1g5X5zqqbpKdJMCdc362MuiJ/lq8knJLMzpNScQeR49HO6/K4rTH BG9gwuNAzwXUAqsy7m5DAfBv6Xg0x2jRSR6GUrjAI2tvV93aOXnMN0tnXVO8Tt93EhQlc73VYqN7Il vzsxEJh6oKy+XoeLoiVnMUG6hot29+cOyNcyv6BzQNFFA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221018_040454_368937_F98D809B X-CRM114-Status: GOOD ( 23.58 ) 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 Our segment alignment is 64k for all configurations, and coincidentally, this is the largest alignment supported by the PE/COFF executable format used by EFI. This means that generally, there is no need to move the image around in memory after it has been loaded by the firmware, which can be advantageous as it also permits us to rely on the memory attributes set by the firmware (R-X for [_text, __inittext_end] and RW- for [__initdata_begin, _end]. However, the minimum alignment of the image is actually 128k on 64k pages configurations with CONFIG_VMAP_STACK=y, due to the existence of a single 128k aligned object in the image, which is the stack of the init task. Let's work around this by adding some padding before the init stack allocation, so we can round down the stack pointer to a suitably aligned value if the image is not aligned to 128k in memory. Note that this does not affect the boot protocol, which still requires 2 MiB alignment for bare metal boot, but is only part of the internal contract between the EFI stub and the kernel proper. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/efi.h | 7 ------- arch/arm64/kernel/head.S | 3 +++ arch/arm64/kernel/vmlinux.lds.S | 11 ++++++++++- drivers/firmware/efi/libstub/arm64-stub.c | 2 +- include/linux/efi.h | 6 +----- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/arch/arm64/include/asm/efi.h b/arch/arm64/include/asm/efi.h index 439e2bc5d5d8..3177e76de708 100644 --- a/arch/arm64/include/asm/efi.h +++ b/arch/arm64/include/asm/efi.h @@ -54,13 +54,6 @@ efi_status_t __efi_rt_asm_wrapper(void *, const char *, ...); /* arch specific definitions used by the stub code */ -/* - * In some configurations (e.g. VMAP_STACK && 64K pages), stacks built into the - * kernel need greater alignment than we require the segments to be padded to. - */ -#define EFI_KIMG_ALIGN \ - (SEGMENT_ALIGN > THREAD_ALIGN ? SEGMENT_ALIGN : THREAD_ALIGN) - /* * On arm64, we have to ensure that the initrd ends up in the linear region, * which is a 1 GB aligned region of size '1UL << (VA_BITS_MIN - 1)' that is diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 2196aad7b55b..f168e3309704 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -398,6 +398,9 @@ SYM_FUNC_END(create_kernel_mapping) msr sp_el0, \tsk ldr \tmp1, [\tsk, #TSK_STACK] +#if THREAD_ALIGN > SEGMENT_ALIGN + bic \tmp1, \tmp1, #THREAD_ALIGN - 1 +#endif add sp, \tmp1, #THREAD_SIZE sub sp, sp, #PT_REGS_SIZE diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 45131e354e27..0efccdf52be2 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -274,7 +274,16 @@ SECTIONS _data = .; _sdata = .; - RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN) +#if THREAD_ALIGN > SEGMENT_ALIGN + /* + * Add some padding for the init stack so we can fix up any potential + * misalignment at runtime. In practice, this can only occur on 64k + * pages configurations with CONFIG_VMAP_STACK=y. + */ + . += THREAD_ALIGN - SEGMENT_ALIGN; + ASSERT(. == init_stack, "init_stack not at start of RW_DATA as expected") +#endif + RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, SEGMENT_ALIGN) /* * Data written with the MMU off but read with the MMU on requires diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index e767a5ac8c3d..6229f42c797f 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -97,7 +97,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, * 2M alignment if KASLR was explicitly disabled, even if it was not * going to be activated to begin with. */ - u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN; + u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : SEGMENT_ALIGN; if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID; diff --git a/include/linux/efi.h b/include/linux/efi.h index 256e70e42114..1a395c24fdc0 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -422,11 +422,7 @@ void efi_native_runtime_setup(void); /* * This GUID may be installed onto the kernel image's handle as a NULL protocol * to signal to the stub that the placement of the image should be respected, - * and moving the image in physical memory is undesirable. To ensure - * compatibility with 64k pages kernels with virtually mapped stacks, and to - * avoid defeating physical randomization, this protocol should only be - * installed if the image was placed at a randomized 128k aligned address in - * memory. + * and moving the image in physical memory is undesirable. */ #define LINUX_EFI_LOADED_IMAGE_FIXED_GUID EFI_GUID(0xf5a37b6d, 0x3344, 0x42a5, 0xb6, 0xbb, 0x97, 0x86, 0x48, 0xc1, 0x89, 0x0a) From patchwork Tue Oct 18 11:04:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13010266 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 CDA24C4332F for ; Tue, 18 Oct 2022 11:06:16 +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=6IwmkGBnsC9BORKEapfuI7xMQs5juXyhZrDpfAa8MJ8=; b=R82QN+Xdbod/S3 GYIVC1T3oIwa4UqWls43+3VNRGHLJumsw96tXyHIOdRtDf22cr7JFUAmkIQvJRiq1PDA5twoneI3I I7DvbAxnmpb43UoBVIFCPq3XbOXP8uR+nq3ZILHxHmS/AmGwuvNVoFw+tpsYj1YyUt2fR8Z1ifir1 65Vm5PbgMmDYLtKvJM+7as3BAMxj/TFQDLbd23aQh9mbBbQQXvY2FlgWrGXgBR91bNdeU55wrj6eJ P8i3yRd5Hfr0ojjjf7X9l6BVaZsyK5gHGWNTUEbTftb+Lat2sNRGv3YOFkmJlHBvSqoZMOEm9/GyJ jA/gFuaFTtbNgG0tr72w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPL-005ygf-C3; Tue, 18 Oct 2022 11:05:15 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkP3-005yMN-UD for linux-arm-kernel@lists.infradead.org; Tue, 18 Oct 2022 11:04:59 +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 ams.source.kernel.org (Postfix) with ESMTPS id 87ABAB81E8F; Tue, 18 Oct 2022 11:04:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 751DBC4347C; Tue, 18 Oct 2022 11:04:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666091095; bh=l+Ib5JlcWADbO+SzTyZ0TnTZ/81bpJ2Jk1D2qiqFglo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EQ/jw6CZYv+s+QnYSrhQoA0DNCqJ+AaP/d14+Sxt1WAvkjSRU5JyJkWG0XylcPDw4 Qk6chAYst49OVVV5kTHeuoWGi8gqjZUE15E/5gqeOPZGB54BWo71XffMxJSBUAo2bJ 1rW05umb8xy5VEM2GYEcrmOIbltThOgFWbJ0SUqU44VFzTIRpr1mhWGHi/pC638wN0 19UNMewgHfAMyR9ysROLAL1P+a7cKcqpN5RMYXFO034NP7AIQ/iAiOe3tgzUcLjpet 86D/qJS5IoR/U8N2WzxWJLrEhx2+ag1HWW5wQ9opAiRutLK2PCsHYbXJ6eOfE9lb56 1WlWpBYcEiKIQ== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, keescook@chromium.org, Ard Biesheuvel , Will Deacon , Catalin Marinas , Marc Zyngier , Mark Rutland Subject: [PATCH v4 2/6] arm64: kernel: move identity map out of .text mapping Date: Tue, 18 Oct 2022 13:04:37 +0200 Message-Id: <20221018110441.3855148-3-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018110441.3855148-1-ardb@kernel.org> References: <20221018110441.3855148-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3452; i=ardb@kernel.org; h=from:subject; bh=l+Ib5JlcWADbO+SzTyZ0TnTZ/81bpJ2Jk1D2qiqFglo=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTohCAxt1u0J8PSJyJC+4Lo0AnHH1KMjtFWBjsIF5 fS3LieCJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY06IQgAKCRDDTyI5ktmPJDF4DA CP/5Lod03+XvYrymRfDaR+IKb3ht94JXA3lDWb9aiVZYRaOd3RAyw22ezpOGsZxNKWcgqavJ0MRxb1 h8u+YuqRL3eIE+h6IVWf+lZpEOtH2GUIYC0uDw12P7EQKP/tp45oNFDPXhvd3ynAkeg3Z9lKGTKvxJ WhOqSKiTjE1PkHvA7vJmJOT292assjIZtJMLA2uMnQ48iW8Amunhaq14M24JXqwfpiyGTDTftmi7KQ x3dkPa4nMWbvA+c5jAyp1RI1CfFz5nL1PObg8mFuDCr9LzwgTRtRKYTpiBSqhlIjkyRLLNLWH4rtZf T5BiYvpTSR1hF9sID5FA+KtWocxK3PV42rkOiVRWLsNL93iYysp0BHD6Rfye07rwnTL54HYsGl3ilc NT8D6POJOxSVHCkE1PpDHa6unuJ1hr42KGBCZ+NtDlSS+CF8lYR6K+/YnFkrdlvjaHICeOUOSZmFKq Az2g02DRPKN8Lr1LpQFmGtBQ0OI/d3VK02mNkZTl+qpws= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221018_040458_294614_C656ADA0 X-CRM114-Status: GOOD ( 14.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 Reorganize the ID map slightly so that only code that is executed with the MMU off or via the 1:1 mapping remains. This allows us to move the identity map out of the .text segment, as it will no longer need executable permissions via the kernel mapping. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/head.S | 28 +++++++++++--------- arch/arm64/kernel/vmlinux.lds.S | 2 +- arch/arm64/mm/proc.S | 2 -- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index f168e3309704..25a84ce1700c 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -543,19 +543,6 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) eret SYM_FUNC_END(init_kernel_el) -/* - * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed - * in w0. See arch/arm64/include/asm/virt.h for more info. - */ -SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag) - adr_l x1, __boot_cpu_mode - cmp w0, #BOOT_CPU_MODE_EL2 - b.ne 1f - add x1, x1, #4 -1: str w0, [x1] // Save CPU boot mode - ret -SYM_FUNC_END(set_cpu_boot_mode_flag) - /* * This provides a "holding pen" for platforms to hold all secondary * cores are held until we're ready for them to initialise. @@ -600,6 +587,7 @@ SYM_FUNC_START_LOCAL(secondary_startup) br x8 SYM_FUNC_END(secondary_startup) + .text SYM_FUNC_START_LOCAL(__secondary_switched) mov x0, x20 bl set_cpu_boot_mode_flag @@ -628,6 +616,19 @@ SYM_FUNC_START_LOCAL(__secondary_too_slow) b __secondary_too_slow SYM_FUNC_END(__secondary_too_slow) +/* + * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed + * in w0. See arch/arm64/include/asm/virt.h for more info. + */ +SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag) + adr_l x1, __boot_cpu_mode + cmp w0, #BOOT_CPU_MODE_EL2 + b.ne 1f + add x1, x1, #4 +1: str w0, [x1] // Save CPU boot mode + ret +SYM_FUNC_END(set_cpu_boot_mode_flag) + /* * The booting CPU updates the failed status @__early_cpu_boot_status, * with MMU turned off. @@ -659,6 +660,7 @@ SYM_FUNC_END(__secondary_too_slow) * Checks if the selected granule size is supported by the CPU. * If it isn't, park the CPU */ + .section ".idmap.text","awx" SYM_FUNC_START(__enable_mmu) mrs x3, ID_AA64MMFR0_EL1 ubfx x3, x3, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4 diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 0efccdf52be2..5002d869fa7f 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -168,7 +168,6 @@ SECTIONS LOCK_TEXT KPROBES_TEXT HYPERVISOR_TEXT - IDMAP_TEXT *(.gnu.warning) . = ALIGN(16); *(.got) /* Global offset table */ @@ -195,6 +194,7 @@ SECTIONS TRAMP_TEXT HIBERNATE_TEXT KEXEC_TEXT + IDMAP_TEXT . = ALIGN(PAGE_SIZE); } diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S index b9ecbbae1e1a..d7ca6f23fb0d 100644 --- a/arch/arm64/mm/proc.S +++ b/arch/arm64/mm/proc.S @@ -110,7 +110,6 @@ SYM_FUNC_END(cpu_do_suspend) * * x0: Address of context pointer */ - .pushsection ".idmap.text", "awx" SYM_FUNC_START(cpu_do_resume) ldp x2, x3, [x0] ldp x4, x5, [x0, #16] @@ -166,7 +165,6 @@ alternative_else_nop_endif isb ret SYM_FUNC_END(cpu_do_resume) - .popsection #endif .pushsection ".idmap.text", "awx" From patchwork Tue Oct 18 11:04:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13010267 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 7ADD7C4332F for ; Tue, 18 Oct 2022 11:06:25 +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=QC1RLnXAOJZr6iFV6uXRoLme7pTPGb8fCfgM3VGF4Ss=; b=aW3qUBdk/d/Kqh 6Jaa8rplkLR7DsXdVH0spwSxMwxyHFFaDMuKe2da+yU4+zcbKrZ8uDRBwbo4/FTpnfPwkW4+cLyfG PevZNwcNx9mv4Eqj12mA9Y4b1YYmRHRC5fp29vhjo5UGy4e2PLvxoYRJytQ8JyJQ4XgJcc3qleTgs M328RU7JVTDDgpasSl6kbWwdjXjMqWinOrBYm2iaqWBN1HZ/DZtjy8ThEEtg8YlgWm3uBM0e2q2ZU UGQO3N9quWHvykYpGoS7UkZyWOgc1xiQjR8zE766zs2QiqQ4gMWp47dFDWO9Pn1faEtCVhDZDtmBp YDaNCH9jQDewAHSiYyYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPV-005yoX-Jl; Tue, 18 Oct 2022 11:05:25 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkP7-005yOs-6C for linux-arm-kernel@lists.infradead.org; Tue, 18 Oct 2022 11:05:02 +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 92DE261524; Tue, 18 Oct 2022 11:04:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A332CC43150; Tue, 18 Oct 2022 11:04:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666091097; bh=1QTkaT4iHMGgddft3nM2F14cfzLMiHsK/itOeD3mJxI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JF1jxCToj6+Ejp1PGlriyLvKJ6WX2APs4H1HQ+/CDc59zCq7KrVf90yxVWv9eEjmQ RDM6qnzcBWBXKSO5MIFYnVONwLvFQYKHrb/CpqALJh/8cCkypEujSf+A9rUpyDp8w5 3zpjYdeEmm1RKiyGjJQkegOO/UAXMXLiz4yRGhnQt8MiKM1Df26i8kQ42Njl149ZRR irt+LAH0hm1nPbniFX6H26g9l+vNx3OHtBexsLiRIYhR/IaV+1RvyDG9gsukZfVzC4 Fxr+F0kpj7/EOmESltIK2sA4ljyDn41yW/yqNf98FFsZO8MyeI6z03kO6pEju6fsBm HYRXufmmXfSLA== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, keescook@chromium.org, Ard Biesheuvel , Will Deacon , Catalin Marinas , Marc Zyngier , Mark Rutland Subject: [PATCH v4 3/6] arm64: head: record the MMU state at primary entry Date: Tue, 18 Oct 2022 13:04:38 +0200 Message-Id: <20221018110441.3855148-4-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018110441.3855148-1-ardb@kernel.org> References: <20221018110441.3855148-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2430; i=ardb@kernel.org; h=from:subject; bh=1QTkaT4iHMGgddft3nM2F14cfzLMiHsK/itOeD3mJxI=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTohDgqXMMD4mKkQSjAgLNYwoP57fkVrc7w4Ft/Gp ZphNEoyJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY06IQwAKCRDDTyI5ktmPJFKTC/ wOWqe/dRAf/kQER4xUM8QKLKEG0aM1mkAslFsVmDxCwJU6xuWIcEagW8STn/2auhyJDTFqxYZEE6QZ AW3WtSKTHqzCNt0+NMoKPEyFRWkbvjvp7almBkmLFWJ2M4KkhRdYZ+fIfIKjWBiOQJygKTc/xMz/FX Z63UcvhuiOQxw/jmd4ohEm+CAiohxDKUIyyOoIsdvbQYjJuQ13KAa0bMTjJOwqtqSRog1bfhnJFCHw ln3rQ6zhDqCnDRHpaairVWBet7/L/t6nU7z5+JyDZLfkIDIo6Rmhht1wBiCbQH4Cp4u28Ms590mBoC RngYas8/PEqwElIvpPrPtSc1kRJTKil+48S+nFv0B798nXUn0LhasEYKOzynuPfbJj+Hmgglplwk0g w/Ko9o7FcK7OOe19LIL/N42BrI9n6l+D0vqz/wek2JljrBOh2lZWdNdCE1P7U+0eti77MXIrikb2uE Alr2sJaZNO3wQPPFfvjm0nOq6D+VcPHUYhcXPxNxUl9qw= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221018_040501_327598_8F5802E8 X-CRM114-Status: GOOD ( 11.68 ) 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 Prepare for being able to deal with primary entry with the MMU and caches enabled, by recording whether or not we entered with the MMU on in register x19. While at it, add disable_mmu_workaround macro invocations to init_kernel_el, as its manipulation of SCTLR_ELx may amount to disabling of the MMU after subsequent patches. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/head.S | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 25a84ce1700c..643797b21a1c 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -77,6 +77,7 @@ * primary lowlevel boot path: * * Register Scope Purpose + * x19 primary_entry() .. start_kernel() whether we entered with the MMU on * x20 primary_entry() .. __primary_switch() CPU boot mode * x21 primary_entry() .. start_kernel() FDT pointer passed at boot in x0 * x22 create_idmap() .. start_kernel() ID map VA of the DT blob @@ -86,6 +87,7 @@ * x28 create_idmap() callee preserved temp register */ SYM_CODE_START(primary_entry) + bl record_mmu_state bl preserve_boot_args bl init_kernel_el // w0=cpu_boot_mode mov x20, x0 @@ -109,6 +111,19 @@ SYM_CODE_START(primary_entry) b __primary_switch SYM_CODE_END(primary_entry) +SYM_CODE_START_LOCAL(record_mmu_state) + mrs x19, CurrentEL + cmp x19, #CurrentEL_EL2 + mrs x19, sctlr_el1 + b.ne 0f + mrs x19, sctlr_el2 +0: tst x19, #SCTLR_ELx_C // Z := (C == 0) + and x19, x19, #SCTLR_ELx_M // isolate M bit + ccmp x19, xzr, #4, ne // Z |= (M == 0) + cset x19, ne // set x19 if !Z + ret +SYM_CODE_END(record_mmu_state) + /* * Preserve the arguments passed by the bootloader in x0 .. x3 */ @@ -497,6 +512,7 @@ SYM_FUNC_START(init_kernel_el) SYM_INNER_LABEL(init_el1, SYM_L_LOCAL) mov_q x0, INIT_SCTLR_EL1_MMU_OFF + pre_disable_mmu_workaround msr sctlr_el1, x0 isb mov_q x0, INIT_PSTATE_EL1 @@ -529,11 +545,13 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) cbz x0, 1f /* Set a sane SCTLR_EL1, the VHE way */ + pre_disable_mmu_workaround msr_s SYS_SCTLR_EL12, x1 mov x2, #BOOT_CPU_FLAG_E2H b 2f 1: + pre_disable_mmu_workaround msr sctlr_el1, x1 mov x2, xzr 2: From patchwork Tue Oct 18 11:04:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13010269 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 A112DC433FE for ; Tue, 18 Oct 2022 11:06:55 +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=t5vHaYcKvv6rh0cOds7siqlUh9eUsobxLU1TJr/POTw=; b=ZvfvTfkJwaiOWJ suhU0YEyJ9W/yMJkrs0J/8uVQD0PUKeZNcbwxFfxEkn6vUHKVIePJLHfM6YK3p1WHm2UoA1NG1oX9 DDA3NyYxGcOhzo55LLuO1sGPVKrWUixowntW+Q07A7lq6gIIxZunDPebk2nDKVAzmNoEENiT7LUNZ MllMWcDMaOORcbtq+0Ch4/bNUBiPwxNXJJqMuiL3s82JU6gk9HT5NGMBBBD7WNWYn0HbK/sUeCXHP dti+lOq1K9Mo2TKLRoQjYRCGLhQaogOCI/L65eWZ8bKIbpg8dmlfQfqjt8lTFwR7pPZkPhe7v9rUE zHxRdctlQUdfdiTGm0pA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPt-005zHW-Qm; Tue, 18 Oct 2022 11:05:50 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPA-005ySR-MJ for linux-arm-kernel@lists.infradead.org; Tue, 18 Oct 2022 11:05:06 +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 ams.source.kernel.org (Postfix) with ESMTPS id CEE0CB81E85; Tue, 18 Oct 2022 11:05:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D03C0C433D6; Tue, 18 Oct 2022 11:04:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666091099; bh=XyJL/8SEF32qYGNUVw1eQPMZ3OctMavcgBrjB2KO9Fw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=drHA4IKwrRO/yg42Rr+HJDZB5FhJHBzm4Yl/1inBn+YGl8WafkIB3yGPVcUX73y5H bY39w39IvRY4dkWTEU3zA68cWrId5E3wETGVMD14g15sYLezxUhRiQTy/1HaY02ALM OIsyu9K3UV/UFDeoVxezyBkWjhzwEahTGA3fUHNH0iZ2B6uihnXna5gGQxn7Yf+RQZ CTFf7u7kKfLsVppXSZYIAThV4NEqvF7fru2U9pFXABqLTI4MXoMMzjfEUIX2vGmd6G QblFIzyCv8x8CgiKkZi1VlkwqMxNs6GPmqDfH38lRLfdQ6IpRVOV4/anKjbZb5zf+5 8UuSPWflP4Dvg== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, keescook@chromium.org, Ard Biesheuvel , Will Deacon , Catalin Marinas , Marc Zyngier , Mark Rutland Subject: [PATCH v4 4/6] arm64: head: avoid cache invalidation when entering with the MMU on Date: Tue, 18 Oct 2022 13:04:39 +0200 Message-Id: <20221018110441.3855148-5-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018110441.3855148-1-ardb@kernel.org> References: <20221018110441.3855148-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1782; i=ardb@kernel.org; h=from:subject; bh=XyJL/8SEF32qYGNUVw1eQPMZ3OctMavcgBrjB2KO9Fw=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTohF5kWHwyv/QI5b27eYUOy19fcWWtdWHZdU2aUQ R7bmEyqJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY06IRQAKCRDDTyI5ktmPJAxOC/ 0cT2Sj7Y9pvPUeeCLm4X+r7iMyGxhGtcUTjlruFJR/cPWBaCIYM5DICNFPFzznQOTsjozpSG3jd7Bp c9lD/woQ724tptXFWBaxQlCBOllqrwE7ndgMxO5OgfkA9pK4xJFer0exBk3hgdGGULdegnQ1hnHztz ALmab4kiINJf8N/mjjm6PFNYdGCl1s52lMhk6N8oW+9OFhjHWN3SGwuXrNAA5sqwwEG2bac8NYHMSR lbqBabpx64PWZ8BrTX/UTp1sBenuVLNvdReHOwMq0HV4kGHY5m5LQgkHGW6NF0lBJDL6KhTReNhIic h7vy+xJYfp3vngb+VKp/zZeVW7lSTTKZTZrYfqTG11sANrBtLPtRRBhuBu0SnnsuGLqNNm5MRGw9KF Huyew7mEiq4iIKY+OD20z0DsOgnjR0bz2iMmmFcdj7HdeNWKRgusOZ/sGhdE7TUS/4AOm4bE3Ogklb S8VFjP+DXyXOWSkPBXh8P7KL1KaaVyaHfJMHempQ4SbcU= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221018_040505_044678_CD47EFC9 X-CRM114-Status: GOOD ( 12.85 ) 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 If we enter with the MMU on, there is no need for explicit cache invalidation for stores to memory, as they will be coherent with the caches. Let's take advantage of this, and create the ID map with the MMU still enabled if that is how we entered, and avoid any cache invalidation calls in that case. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/head.S | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 643797b21a1c..5de2ba3539a8 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -89,9 +89,9 @@ SYM_CODE_START(primary_entry) bl record_mmu_state bl preserve_boot_args + bl create_idmap bl init_kernel_el // w0=cpu_boot_mode mov x20, x0 - bl create_idmap /* * The following calls CPU setup code, see arch/arm64/mm/proc.S for @@ -134,11 +134,13 @@ SYM_CODE_START_LOCAL(preserve_boot_args) stp x21, x1, [x0] // x0 .. x3 at kernel entry stp x2, x3, [x0, #16] + cbnz x19, 0f // skip cache invalidation if MMU is on dmb sy // needed before dc ivac with // MMU off add x1, x0, #0x20 // 4 x 8 bytes b dcache_inval_poc // tail call +0: ret SYM_CODE_END(preserve_boot_args) SYM_FUNC_START_LOCAL(clear_page_tables) @@ -375,12 +377,13 @@ SYM_FUNC_START_LOCAL(create_idmap) * accesses (MMU disabled), invalidate those tables again to * remove any speculatively loaded cache lines. */ + cbnz x19, 0f // skip cache invalidation if MMU is on dmb sy adrp x0, init_idmap_pg_dir adrp x1, init_idmap_pg_end bl dcache_inval_poc - ret x28 +0: ret x28 SYM_FUNC_END(create_idmap) SYM_FUNC_START_LOCAL(create_kernel_mapping) From patchwork Tue Oct 18 11:04:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13010268 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 8F8A8C433FE for ; Tue, 18 Oct 2022 11:06:42 +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=DRRc/7QXegqrZJpL5mKFcvlvz1hPi2X50KlB3HpkBN4=; b=34QAp85AyFpJd1 T58pvljF8MAq4ozjx3Z8JZWanO/jFXeLJhuM+8No48OdJ4NkoZzuInBRIhvUgQgX+lcSfBhfvt3py yn5mPcwAg4ChzPPqUQyHN4vVTSDPDvgAB+EjC36pxprJCjh260Kkpyk5Jm6kV0hbw5f4gggDey3oq 5ui9TXHYWNPEKB/lc9egKLtoAuN0fGHdb+A7+jzMu5QUvSMHMuWpZqkvdJsTImqEDL/VlM9U3MVqk SrnIOSFtYSYdhyU6yGOM6QO7sRm3semSG1a+9kUaPkDvpoZKXORrneTZAilu5UElzW8aDAAJKvk9W sRqJQXuh1Cy6FPuoUL5A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPg-005yy8-0f; Tue, 18 Oct 2022 11:05:36 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPA-005yTB-9w for linux-arm-kernel@lists.infradead.org; Tue, 18 Oct 2022 11:05:05 +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 ams.source.kernel.org (Postfix) with ESMTPS id 06B1BB81E9F; Tue, 18 Oct 2022 11:05:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0957CC4347C; Tue, 18 Oct 2022 11:04:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666091101; bh=Y4hjTTjb2Eqy7jU72ay8krSE7XbZyj+VAYhAE2r2cFA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fmbA85qU8MKRkAoqWGnAAkHhJekpys4NZeX3ee36b6KcC424IMxb9dfUdajocgeri 2Dr4dVBs3T2yXfctf045k4L6Gwwn13lSeTgA+x17fy/DaV9Sm2lKI3OdVJWpX9fyYJ c399UQKMIknEwhLnFl6bwrf9HUjgLBhgcopXCxW2Xbpnuu1xyUeMZXNNUN4rViY9uq M8UqO1ii/x+zk+39dxuC9y8kQs3pHrLLtENbnR1PwKHCASmAZPCSFEpVnGu+TO98Dw OLRHJtEbcJ9Bz0ZNwQlVP3AiO5ncOOfE7UX8wyy5X47/ca5APkrgU+qCSDmKmKXYJH bUJ7P3As0oNEQ== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, keescook@chromium.org, Ard Biesheuvel , Will Deacon , Catalin Marinas , Marc Zyngier , Mark Rutland Subject: [PATCH v4 5/6] arm64: head: clean the ID map page to the PoC Date: Tue, 18 Oct 2022 13:04:40 +0200 Message-Id: <20221018110441.3855148-6-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018110441.3855148-1-ardb@kernel.org> References: <20221018110441.3855148-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1571; i=ardb@kernel.org; h=from:subject; bh=Y4hjTTjb2Eqy7jU72ay8krSE7XbZyj+VAYhAE2r2cFA=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTohHVkL7JW14y8qKMeV5U+XdPVHM6T5CMZ5c5gl4 Wc39K6qJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY06IRwAKCRDDTyI5ktmPJMNtC/ 9hg0J4BHdgyXhsetzPiK5Sh/Ccq1qIsTokdsq7EreoMAhoDNTCxhvaFnC/KBssLR+doXlzjs2Bof7a R6R0IjCzfAZN+DKpwKwGLaqsgX/q8k8rQdkzgSZVkGe84otGxMFbkvFnsQ+rBfwsHGNRT+IQ7GKSzZ DKzCZoJuj6L8xWhATue97hitmd+GCpjLNIuWXGeGt99BO8tqYtdElAFZrQvVq8LMvWRc02FOcGkVkS FfbB6iv1618jvXVkdfbvPIuKT7fn+INMfbm5VlaGiYt2MMZfbw/31H2423LO8suWFhD0bJz7ZQpkXX Vwx45Jyl/Ol6+NLT1R8Y0zYrvsV439WiugY/R4eX8aixrWW8AcuSt6mdbXRsdDHgflFfG+TblYbs45 xgrPaQ7GPOwloHluT4Bms2Bne2EcwJ5gWngbmmgOB/0USUsLWqi8a1oYrei0qfAvCX1L5FUDoRjl+O CUflkuUYiY8mX/svV3QDkbazOILSOjZR5Ks8/I3d0tAOQ= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221018_040504_663823_BD1F5517 X-CRM114-Status: GOOD ( 15.75 ) 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 If we enter with the MMU and caches enabled, the caller may not have performed any cache maintenance. So clean the ID mapped page to the PoC, to ensure that instruction and data accesses with the MMU off see the correct data. Note that this means primary_entry() itself needs to be moved into the ID map as well, as we will return from init_kernel_el() with the MMU and caches off. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/head.S | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 5de2ba3539a8..c8b8ed8477c1 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -70,7 +70,7 @@ __EFI_PE_HEADER - __INIT + .section ".idmap.text","awx" /* * The following callee saved general purpose registers are used on the @@ -90,6 +90,17 @@ SYM_CODE_START(primary_entry) bl record_mmu_state bl preserve_boot_args bl create_idmap + + /* + * If we entered with the MMU and caches on, clean the ID mapped part + * of the primary boot code to the PoC so we can safely execute it with + * the MMU off. + */ + cbz x19, 0f + adrp x0, __idmap_text_start + adr_l x1, __idmap_text_end + bl dcache_clean_poc +0: bl init_kernel_el // w0=cpu_boot_mode mov x20, x0 @@ -111,6 +122,7 @@ SYM_CODE_START(primary_entry) b __primary_switch SYM_CODE_END(primary_entry) + __INIT SYM_CODE_START_LOCAL(record_mmu_state) mrs x19, CurrentEL cmp x19, #CurrentEL_EL2 From patchwork Tue Oct 18 11:04:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13010270 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 6D969C4332F for ; Tue, 18 Oct 2022 11:07:14 +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=LkCBJb4nTCcPvstAI6QzcPE7BcFln6p3r1KwHaYpBZw=; b=V26+fc4aPkUomc hqQFpqvfEHqMCPD97ZFlR4VRrqMIhLVyCxZ53Ng/pXvKkbdEA9f+o1ovNIwsolPKWhQhRalJ0kZdy JVpsNgU4GwW0LwK7onxbKBz2Zk6kWwL2YVaFQiu0d/Y7g+4SOR88HOHH5NQqEsBeT+n5FecBLm9XK mWGQ8MD/QvdtYTa0jBkxDAKqPvfvQs0KqikHsr0ElzKLqEclU0CLItZ2BuoN5FteESwOszCIcyJKU 42XQsGh26EXRyoxREF7zwM8xSPC2pmUo7KEFtxqrMMLXx9NoWrTkFlpiUEMoYclVU/+TaYTiuO1wT gNtZN7pjYPAflE0MjeWA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkQ9-005zVa-JN; Tue, 18 Oct 2022 11:06:05 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1okkPC-005yU6-Fh for linux-arm-kernel@lists.infradead.org; Tue, 18 Oct 2022 11:05:08 +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 ams.source.kernel.org (Postfix) with ESMTPS id 36985B81E68; Tue, 18 Oct 2022 11:05:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3709FC43470; Tue, 18 Oct 2022 11:05:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666091104; bh=ajAZKUqLaOvnZYGz9N1ipr/BfWU81j4u/S3wIzL9ky0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tPXcrYiRpc+6OGEB9DCY5ERG2qV3gbhByKGda0loszkEvMKjEOBcGUUAU2GJ+usvw G5FIE8mfd1PswWH6imoDM3+aLwLC0CAT6TjiHGkmG+tSuLb0dcAiD9FGUrUAmczaEu wLiOXno/hv+LEY/d+7OR2NGw2iwQs7Npgan1WW0O3FUk5V7tVGzBXW34qEDsOYZJKG tHqaDvcYS9EJe1cXYbHsQTFGfsGsoBklfzzbZRmGeP/EfazRsDYCGKWwgu8hxwAHc6 rO77oSfzPNf3m7QY5Gw4p2j93gyM8HwBttx2AUrO4N4l/Of8qGtBug1LT75nu0tRjU gojJzD48bx5ZQ== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, keescook@chromium.org, Ard Biesheuvel , Will Deacon , Catalin Marinas , Marc Zyngier , Mark Rutland Subject: [PATCH v4 6/6] arm64: efi/libstub: enter with the MMU on Date: Tue, 18 Oct 2022 13:04:41 +0200 Message-Id: <20221018110441.3855148-7-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018110441.3855148-1-ardb@kernel.org> References: <20221018110441.3855148-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5695; i=ardb@kernel.org; h=from:subject; bh=ajAZKUqLaOvnZYGz9N1ipr/BfWU81j4u/S3wIzL9ky0=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTohIXzaJn/IlJ+kFr1EYdYg4nuTbXt53uRA+Daku FmMybzSJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY06ISAAKCRDDTyI5ktmPJI/BDA C2Wgvhwat6TNEwsKTKdXKsg0noF3KpdReIyTepvXE8UBG0XgnocFlw6jD1dNkY2lM8Pdmr3xHBjjJF IGJ6jwgtT6vUPlR2r1pQ7/UwD4QWqNdz6O6pR5H37vBRnWL+R2sVEr1XR1r9BLn3nkmaQYtcNHNCum ek1TOBMnDFNYXxMUW4pQYA395zUDv2GJt451/Y2kSTjMG0m2iK/SrSOpC+Ns2a7roDvBNA5h2+l3ot sPg5L+rlWBsWzqUb48Fs8giS9oh+A3df5EXaat7mOMKjc9cIp+UYMJva/VKerTyRHzLRfir+K8wvbH SswaTYcCX8gm8OVikTCAfD8SBJ+wbEKZ2SybzXw89b0nwA/a57v1Fmx7YpBXMqZM+6qHxZ4pkGnRgA pU68xLvz7tBO4tiA10RDplYzE+qcyzlIhBhwWK8ZXwJ5FKvByVEKWIWPYoeYGdvjNDxCL5p0nlcyhc Mm35VGTHNtZtJymkk+Vb+yLCdgBwH7j/HpAV/4E3048z4= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221018_040506_853037_A44C8B87 X-CRM114-Status: GOOD ( 19.83 ) 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 Instead of disabling the MMU and caches before jumping to the kernel's entry point, just call it directly, and keep the MMU and caches enabled. This removes the need for any unconditional cache invalidation to the PoC in the entry path (although cache maintenance of the code portion of the image is still necessary for I/D coherency if the image was moved around in memory). It also allows us to get rid of the asm routine, as doing the jump is easily done from C code. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/image-vars.h | 5 +- arch/arm64/mm/cache.S | 5 +- drivers/firmware/efi/libstub/arm64-entry.S | 57 -------------------- drivers/firmware/efi/libstub/arm64-stub.c | 17 +++++- 4 files changed, 22 insertions(+), 62 deletions(-) diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h index 74d20835cf91..13e082e946c5 100644 --- a/arch/arm64/kernel/image-vars.h +++ b/arch/arm64/kernel/image-vars.h @@ -10,7 +10,7 @@ #error This file should only be included in vmlinux.lds.S #endif -PROVIDE(__efistub_primary_entry_offset = primary_entry - _text); +PROVIDE(__efistub_primary_entry = primary_entry); /* * The EFI stub has its own symbol namespace prefixed by __efistub_, to @@ -28,10 +28,11 @@ PROVIDE(__efistub_strnlen = __pi_strnlen); PROVIDE(__efistub_strcmp = __pi_strcmp); PROVIDE(__efistub_strncmp = __pi_strncmp); PROVIDE(__efistub_strrchr = __pi_strrchr); -PROVIDE(__efistub_dcache_clean_poc = __pi_dcache_clean_poc); +PROVIDE(__efistub_caches_clean_inval_pou = __pi_caches_clean_inval_pou); PROVIDE(__efistub__text = _text); PROVIDE(__efistub__end = _end); +PROVIDE(__efistub___inittext_end = __inittext_end); PROVIDE(__efistub__edata = _edata); PROVIDE(__efistub_screen_info = screen_info); PROVIDE(__efistub__ctype = _ctype); diff --git a/arch/arm64/mm/cache.S b/arch/arm64/mm/cache.S index 081058d4e436..8c3b3ee9b1d7 100644 --- a/arch/arm64/mm/cache.S +++ b/arch/arm64/mm/cache.S @@ -52,10 +52,11 @@ alternative_else_nop_endif * - start - virtual start address of region * - end - virtual end address of region */ -SYM_FUNC_START(caches_clean_inval_pou) +SYM_FUNC_START(__pi_caches_clean_inval_pou) caches_clean_inval_pou_macro ret -SYM_FUNC_END(caches_clean_inval_pou) +SYM_FUNC_END(__pi_caches_clean_inval_pou) +SYM_FUNC_ALIAS(caches_clean_inval_pou, __pi_caches_clean_inval_pou) /* * caches_clean_inval_user_pou(start,end) diff --git a/drivers/firmware/efi/libstub/arm64-entry.S b/drivers/firmware/efi/libstub/arm64-entry.S deleted file mode 100644 index 4524525ab314..000000000000 --- a/drivers/firmware/efi/libstub/arm64-entry.S +++ /dev/null @@ -1,57 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * EFI entry point. - * - * Copyright (C) 2013, 2014 Red Hat, Inc. - * Author: Mark Salter - */ -#include -#include - -SYM_CODE_START(efi_enter_kernel) - /* - * efi_pe_entry() will have copied the kernel image if necessary and we - * end up here with device tree address in x1 and the kernel entry - * point stored in x0. Save those values in registers which are - * callee preserved. - */ - ldr w2, =primary_entry_offset - add x19, x0, x2 // relocated Image entrypoint - - mov x0, x1 // DTB address - mov x1, xzr - mov x2, xzr - mov x3, xzr - - /* - * Clean the remainder of this routine to the PoC - * so that we can safely disable the MMU and caches. - */ - adr x4, 1f - dc cvac, x4 - dsb sy - - /* Turn off Dcache and MMU */ - mrs x4, CurrentEL - cmp x4, #CurrentEL_EL2 - mrs x4, sctlr_el1 - b.ne 0f - mrs x4, sctlr_el2 -0: bic x4, x4, #SCTLR_ELx_M - bic x4, x4, #SCTLR_ELx_C - b.eq 1f - b 2f - - .balign 32 -1: pre_disable_mmu_workaround - msr sctlr_el2, x4 - isb - br x19 // jump to kernel entrypoint - -2: pre_disable_mmu_workaround - msr sctlr_el1, x4 - isb - br x19 // jump to kernel entrypoint - - .org 1b + 32 -SYM_CODE_END(efi_enter_kernel) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index 6229f42c797f..9c7e2c1aace2 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -86,7 +86,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, efi_handle_t image_handle) { efi_status_t status; - unsigned long kernel_size, kernel_memsize = 0; + unsigned long kernel_size, kernel_codesize, kernel_memsize; u32 phys_seed = 0; /* @@ -130,6 +130,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, SEGMENT_ALIGN >> 10); kernel_size = _edata - _text; + kernel_codesize = __inittext_end - _text; kernel_memsize = kernel_size + (_end - _edata); *reserve_size = kernel_memsize; @@ -173,6 +174,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, *image_addr = *reserve_addr; memcpy((void *)*image_addr, _text, kernel_size); + caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize); clean_image_to_poc: /* @@ -184,3 +186,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, return EFI_SUCCESS; } + +asmlinkage void primary_entry(void); + +void __noreturn efi_enter_kernel(unsigned long entrypoint, + unsigned long fdt_addr, + unsigned long fdt_size) +{ + void (* __noreturn enter_kernel)(u64, u64, u64, u64); + u64 offset = (char *)primary_entry - _text; + + enter_kernel = (void *)entrypoint + offset; + enter_kernel(fdt_addr, 0, 0, 0); +}