From patchwork Tue Jan 23 14:53:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13527726 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 86E8BC48285 for ; Tue, 23 Jan 2024 16:01:11 +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=2L32XDF0TXcaXAzChe0UqYGqfMDc6frzH5p09MFa7FI=; b=hJdDR9iX4LAChQYzRj+zauvVR2 C9W3yUqFqdQh804RqICffwnjUtMgHZ/IKlMQOuk3IS7tjMqdgaWixBLa1Tr4PJfipKIsAme5WLxmh VfrHDy+lB7aehO0KxV1uPhXq+udTsymwkHfwb6tTcOh4atFBwYrU8HAmYkIg0l0sDIoDtBLrEkd2I xVXzRbfehq1NZTAYF7j3QoDXJCFSTG7pQUrtcerS6QaQ3LZ9pvXl9sh9bMo3XSwdzwcPKEPv2gDj1 63Sk7UTjDSiG2HO9VilcDsUpSBaIBAiIGsXs+TiXLFhOyMiSEnxKMaIHYDnGJ8CrmvsGin+zPb6Ta 2/42qXvQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSJCS-00HArq-14; Tue, 23 Jan 2024 16:00:32 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSIB3-00Gtq4-0g for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 14:55:10 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ffa2bb4810so34490947b3.0 for ; Tue, 23 Jan 2024 06:54:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706021697; x=1706626497; 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=ODYIdDM+nqe1w4y1lwTl7vo5ShPL7ZusEVAukESBcmA=; b=kuPHehNLMElNELpyFaE0czZLAM7eqAgM/f8HcOkTLFctEzWz01mT1Ocvee+uK/pJqS KmsPPLCecbAOVDlBjD7q2IZnQBO7oNF8lFHTLpE70m/zzchIMOe9u7u4rd1PtSu6VWBq NOSygg0DuMBIYqGPB+Kj5VAYThOeNCDauWf8w8PmpeHnhZK4nWAjqCwDoN1d4/fJcu8g LoXo41AeBU39GZFVcdFr8CcEMvV+ocNMja2ggn4gNuDwhrN4XmhEprW0a1CQcMC1Ochv VTGDtY2ptr2wxXiMDAJWv79O7IrsOr4jKvfIwAYhl9l9Wk75RTjkVzv3W+Obw6SmJrW2 lVXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706021697; x=1706626497; 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=ODYIdDM+nqe1w4y1lwTl7vo5ShPL7ZusEVAukESBcmA=; b=So2wQpKAD8wfPfkH7f8E/yPqPds4PiIWC+Rq+fqC1woFyQfiNcaSymERYtn0S4+8Bj lwQlmGTBq9TZrhcl8a1Iem6iWAAwjqiuRuWM8Zm14c5toSDVe67fH/z9XXrFkXofw9zE w5taMtZ5mJmziq6kkaLFbuNS3vhcY+NUxObwjVfM9t9Fc/0pwh9B5lSCPj28zxOllnBm IfVQUPUuR4vAhvjDwyLjD7JCTrwVYJfPJzr9tgXk008M1PBdhd0fiA+vjTJybjKvVk/+ 4xP3vCptLiG303Q+jhw5VOQdVx7TnaSInx3ctt5lDqjIApUcClxvuMmzPkNxNpLkr/BP A5ow== X-Gm-Message-State: AOJu0YzDszWsEBjHYYum8DrNIBHIkft8+G3Y27WpSV5FXbZIrCXOI+av xUWnwWEIxY/lNY8E37gK4kcQmqyhhSd0RsMXxthg4aZ158I8OVMqvc3QJiIsuogtBum6I4MYbR4 4/qw56ic8l3SF1k/uNM7njneqa/gX8cNL58vvtI6l21W3SZtUY9nX1u02TdVPf1V6Aw5vFo9qVo casHC6ARsVJ3R/yonW8M01oIMJDu3RuGPaoOdIpXsm X-Google-Smtp-Source: AGHT+IG4BFKauWXrRjQFf+n3/FITwowBhf9WAUIL4UiM8G8m0V2+3TFjLue7igRROnHRqjyEZFgYF7pF X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a81:4cc6:0:b0:5ff:5389:526c with SMTP id z189-20020a814cc6000000b005ff5389526cmr2111331ywa.2.1706021696914; Tue, 23 Jan 2024 06:54:56 -0800 (PST) Date: Tue, 23 Jan 2024 15:53:14 +0100 In-Reply-To: <20240123145258.1462979-52-ardb+git@google.com> Mime-Version: 1.0 References: <20240123145258.1462979-52-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2825; i=ardb@kernel.org; h=from:subject; bh=ytqy22hIensXZScckAkMf5yP4E4Kr8RYIQMTUB1HVx4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX9pdt8fqVGeSz8XYLLWyfGa4cs335GKFZeRVDKZ2m3j f2hGouOUhYGMQ4GWTFFFoHZf9/tPD1RqtZ5lizMHFYmkCEMXJwCMJHXuxgZHojypmg+n3Re/nJG zTxp7h6+2nNXb7MVZdt1xpvwLT2hyPA/ht1Q60zpXGMf4+3J5pmT9sq1qF+dseXfO4GggBdPdl7 gBAA= X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123145258.1462979-67-ardb+git@google.com> Subject: [PATCH v7 15/50] arm64: head: Run feature override detection before mapping the kernel 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240123_065501_258504_6AE1BB99 X-CRM114-Status: GOOD ( 14.16 ) 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 To permit the feature overrides to be taken into account before the KASLR init code runs and the kernel mapping is created, move the detection code to an earlier stage in the boot. In a subsequent patch, this will be taken advantage of by merging the preliminary and permanent mappings of the kernel text and data into a single one that gets created and relocated before start_kernel() is called. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/head.S | 17 +++++++++-------- arch/arm64/kernel/vmlinux.lds.S | 4 +--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 2af518161f3a..865ecc1f8255 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -375,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 BSS and the kernel page tables r/w in the ID map */ + /* Remap [.init].data, BSS and the kernel page tables r/w in the ID map */ adrp x1, _text - adrp x2, __bss_start + adrp x2, __initdata_begin adrp x3, _end bic x4, x2, #SWAPPER_BLOCK_SIZE - 1 mov_q x5, SWAPPER_RW_MMUFLAGS @@ -491,9 +491,6 @@ SYM_FUNC_START_LOCAL(__primary_switched) #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) bl kasan_early_init #endif - mov x0, x20 // pass the full boot status - mov x1, x22 // pass the low FDT mapping - bl __pi_init_feature_override // Parse cpu feature overrides #ifdef CONFIG_UNWIND_PATCH_PAC_INTO_SCS bl scs_patch_vmlinux #endif @@ -772,12 +769,16 @@ SYM_FUNC_START_LOCAL(__primary_switch) 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 adrp x1, early_init_stack mov sp, x1 mov x29, xzr + mov x0, x20 // pass the full boot status + mov x1, x22 // pass the low FDT mapping + bl __pi_init_feature_override // Parse cpu feature overrides + +#ifdef CONFIG_RELOCATABLE + adrp x23, KERNEL_START + and x23, x23, MIN_KIMG_ALIGN - 1 #ifdef CONFIG_RANDOMIZE_BASE mov x0, x22 bl __pi_kaslr_early_init diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 8a3c6aacc355..3afb4223a5e8 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -320,10 +320,8 @@ SECTIONS init_pg_end = .; /* end of zero-init region */ -#ifdef CONFIG_RELOCATABLE - . += SZ_4K; /* stack for the early relocation code */ + . += SZ_4K; /* stack for the early C runtime */ early_init_stack = .; -#endif . = ALIGN(SEGMENT_ALIGN); __pecoff_data_size = ABSOLUTE(. - __initdata_begin);