From patchwork Tue Sep 12 14:16:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13381865 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 930E1CA0EEB for ; Tue, 12 Sep 2023 15:30:33 +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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=sQ5KFxDoeLSPICqaOJgGyzNgVvXYCEQuGcedzk716dw=; b=v8AJaL7UPXAUhy6gF3wiH8I+hI nTBqLJV6xXuRqSLa+XIgjsF+L/0nw+u7MxV4ZmqvpBKVLZgB18fHHtQwW8Q/Q/93qLM1R0iw/Fu8s KrOaUxxxlhd9PviPWPAClGwZsDbTZL75OhSF114rRA1/xp8vMGpuzHf+t4uvzlnAcyXBgGdm6e9tA GvwI2TfITYc5KcQpk0g/7HrEgCdKZ+NW9fiV/pleQdTqjYSZXC9TQ8PQw7HcQZXiun7qLzzYifMM7 vMBlKVnP/XdQD4RrB2AhR3+gE8bkAi+j9JmcylODR3GOTdNgu10JRc+6wCIjItbxuWXwaoBBfsrK3 e87zGdHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg5LP-003igH-1j; Tue, 12 Sep 2023 15:30:27 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg4DR-003WOt-1n for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:18:19 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59222a14ee1so67432837b3.1 for ; Tue, 12 Sep 2023 07:18:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528288; x=1695133088; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7zFAMdEnBh2nxO6Ud4c95SHScST5+qYd/avAx/j2osg=; b=oVKYmtPS5MWzOEkpIo1bHxVdg1DFPFneHXguNnL2hQPs723OQPBcOFyvKgLqY0Lsll Pe7R8aCD3pw+zlSWLOeCC0BDp8Cleih8yWME6VorgvevyliOGu2DF6BPySb9dTFsha+5 zC6qmeVXxHBXHHDTOJ6YDrcc7zRAmcc0GiAV9UEyh2LW7EiuCI+8CgiZMiLGKzC+eBMB 7dtZFPL7qgBQA1oz7uSiXKF1um2XSgYVW/44iMktLj0P/Gozwe3z4nrolWQp1MwnEOav wRbmrvTrUGZ7GB9vEAmeVgvL5AJyqPg2MGIMNOcXZYr/7NLGoKzGO/zseu+XqNdBPaIg Vpag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528288; x=1695133088; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7zFAMdEnBh2nxO6Ud4c95SHScST5+qYd/avAx/j2osg=; b=VdHcoF10N5l+m/76Tj5GQ/qb3w8tpb7aTboiyVoLEq8pXW6HwwPXUsivhm/YQOh3tA G/XSPulZyfsd1Lv6J9G3c0eF8xKgYRAxo16OzQBaZvP25dS8OyFW0lrNn23fwEKqqWwf O9hX5Z9JrRX/g24J/2B6+lMizdfb09DT+slv6TUSIlbZuTN+ZfV4CYtvcRtIqhsvkl3C J4u/zCe0KYX/HV8NvuU6SbtePXgLZN/XBAkMVT/8a6Fm9Z4E8g0E87Jtgl7SefLuG83u ex3okVQz/cmFIMTcdcXoV8YzLd4VoG94jIqd8ubzvlyApnPtBHmSj5GwZaShPPiYIQum rU6g== X-Gm-Message-State: AOJu0YwG1zuBKF1pfTY5rHChZwDm64sz9erxFOlSCrw62QolFp6DLRF6 viggxFImdtEjdwseRqKlNsWutOFuqM+VjQcC9w/X1OrTlobOE9cwOpHwZLCR1scPd2W5+vrWmw4 fFt42NKcbNoUH4sBkFwS1jSfKoq6v5AV4s+E9IOyAdo6ySTkZY0hWaERdl5dJ0zX8r0m7dZyBeS 8= X-Google-Smtp-Source: AGHT+IEJ1RparPTsqiyFdzyxH9A0rL3Ams+gFuAfWW7e8BaoLYZJKNK9N6KCsbIowMZuPk0juaqSCp4F X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a81:4327:0:b0:589:a5c6:4a8e with SMTP id q39-20020a814327000000b00589a5c64a8emr309653ywa.1.1694528287760; Tue, 12 Sep 2023 07:18:07 -0700 (PDT) Date: Tue, 12 Sep 2023 14:16:12 +0000 In-Reply-To: <20230912141549.278777-63-ardb@google.com> Mime-Version: 1.0 References: <20230912141549.278777-63-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3389; i=ardb@kernel.org; h=from:subject; bh=6Jap1NIlQWA20VBoBeawuJQlFyCJAj1ChLFh6mmy7So=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWhaG3vmtOSlzUXrNj/r2avquSHnowPCgdvPT/eOcNp7 sG/C//97ShlYRDjYJAVU2QRmP333c7TE6VqnWfJwsxhZQIZwsDFKQATYRBhZJi97oTyf6Op3L2/ 3fY9ffXykzeXy7ZJ+6enhOVNmRbwdmMAI8MCzZ3KUzgnbF8u8Fhv79X/XQ8iD8TNKGpJ+FEU4mH 4cA0/AA== X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-85-ardb@google.com> Subject: [PATCH v4 22/61] arm64: head: Clear BSS and the kernel page tables in one go From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook , Joey Gouly X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230912_071809_613530_5259F28B X-CRM114-Status: GOOD ( 16.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 From: Ard Biesheuvel We will move the CPU feature overrides into BSS in a subsequent patch, and this requires that BSS is zeroed before the feature override detection code runs. So let's map BSS read-write in the ID map, and zero it via this mapping. Since the kernel page tables are right next to it, and also zeroed via the ID map, let's drop the separate clear_page_tables() function, and just zero everything in one go. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/head.S | 33 +++++++------------- arch/arm64/kernel/vmlinux.lds.S | 3 ++ 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index ca5e5fbefcd3..2af518161f3a 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -177,17 +177,6 @@ SYM_CODE_START_LOCAL(preserve_boot_args) ret SYM_CODE_END(preserve_boot_args) -SYM_FUNC_START_LOCAL(clear_page_tables) - /* - * Clear the init page tables. - */ - adrp x0, init_pg_dir - adrp x1, init_pg_end - sub x2, x1, x0 - mov x1, xzr - b __pi_memset // tail call -SYM_FUNC_END(clear_page_tables) - /* * Macro to populate page table entries, these entries can be pointers to the next level * or last level entries pointing to physical memory. @@ -386,9 +375,9 @@ SYM_FUNC_START_LOCAL(create_idmap) map_memory x0, x1, x3, x6, x7, x3, IDMAP_PGD_ORDER, x10, x11, x12, x13, x14, EXTRA_SHIFT - /* Remap the kernel page tables r/w in the ID map */ + /* Remap BSS and the kernel page tables r/w in the ID map */ adrp x1, _text - adrp x2, init_pg_dir + adrp x2, __bss_start adrp x3, _end bic x4, x2, #SWAPPER_BLOCK_SIZE - 1 mov_q x5, SWAPPER_RW_MMUFLAGS @@ -489,14 +478,6 @@ SYM_FUNC_START_LOCAL(__primary_switched) mov x0, x20 bl set_cpu_boot_mode_flag - // Clear BSS - adr_l x0, __bss_start - mov x1, xzr - adr_l x2, __bss_stop - sub x2, x2, x0 - bl __pi_memset - dsb ishst // Make zero page visible to PTW - #if VA_BITS > 48 adr_l x8, vabits_actual // Set this early so KASAN early init str x25, [x8] // ... observes the correct value @@ -782,6 +763,15 @@ SYM_FUNC_START_LOCAL(__primary_switch) adrp x1, reserved_pg_dir adrp x2, init_idmap_pg_dir bl __enable_mmu + + // Clear BSS + adrp x0, __bss_start + mov x1, xzr + adrp x2, init_pg_end + sub x2, x2, x0 + bl __pi_memset + dsb ishst // Make zero page visible to PTW + #ifdef CONFIG_RELOCATABLE adrp x23, KERNEL_START and x23, x23, MIN_KIMG_ALIGN - 1 @@ -796,7 +786,6 @@ SYM_FUNC_START_LOCAL(__primary_switch) orr x23, x23, x0 // record kernel offset #endif #endif - bl clear_page_tables bl create_kernel_mapping adrp x1, init_pg_dir diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 8dd5dda66f7c..8a3c6aacc355 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -311,12 +311,15 @@ SECTIONS __pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin); _edata = .; + /* start of zero-init region */ BSS_SECTION(SBSS_ALIGN, 0, 0) . = ALIGN(PAGE_SIZE); init_pg_dir = .; . += INIT_DIR_SIZE; init_pg_end = .; + /* end of zero-init region */ + #ifdef CONFIG_RELOCATABLE . += SZ_4K; /* stack for the early relocation code */ early_init_stack = .;