From patchwork Tue Sep 12 14:15:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13381758 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 00EB4CA0EEB for ; Tue, 12 Sep 2023 14:17:48 +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: Mime-Version:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=QH2aRcxxXjI4dprfNnOIFPg6Ns3J33OQaRMxKvB5MO4=; b=r/h UYdf1ei8mOcHysDe6R7OgsNrdolEF1zY9kj+vYi5pvBYbOAOBD4cert7iKKoyPcRIup7XePn0f3DQ tx6iGxjJyg16WAzWJyO9YOdYMFfDZyPet7YAwHo0TVlde4/X7HZUhNGz41FMB1gO2QfIqQSlZ+7Pw bx67IsWt9e9efMM29/+g2MWv75t4ctCI9+QIPXkmQBkneD64LXO99pDZZT98b0+MNtxNiZGXv4ywY d+r9WsjycImazXMYGEDlqi+0mmI8BACFsuQWgF5nw+3JncibGcTfIzp0KEvccZl6L5kZVQdqMU9K3 /W8e0cVfKV/UQVyKsdlqA7okfl+LybA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Cd-003VoL-04; Tue, 12 Sep 2023 14:17:19 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg4CY-003Vn4-2B for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:17:16 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-3fe182913c5so44265345e9.0 for ; Tue, 12 Sep 2023 07:17:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528232; x=1695133032; darn=lists.infradead.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=moK3T+j8uNcaPTtq4a6y4cvOed5MacHrAxTW6E4xksk=; b=34EONMSD2m3I0N3WVGm1ZytrVwKHNz605n3e8pQO8679x9v0aUFKeC6Wq1cke2QEA/ 4k/6BpvJyaQwOO21G1Gm5xIpDC3cMzRn8RxCzZoq+aX66IOTjrGtvWHlQCw3pzBBU/n0 AgyFqS3OlghyLUjd1fnjzldGlGGHxvcJ/cQ+tDecwsKuysvhivE+gnQO2Ov2mtgV3VEF JR5WDhGAJzCPa5C1KsurKl9HgshqeQU2XySMurovOK3fgY7xWjHEehpEqE4Q1FeUCdZY aeOndEWlwvRllNliH9vm/iO8QX2XgT6BYyfinbmLuUAA5V2EkYJlauLVszdBCCgN4v7h xA0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528232; x=1695133032; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=moK3T+j8uNcaPTtq4a6y4cvOed5MacHrAxTW6E4xksk=; b=H2wVCPtm80pgvYSqxf3lDwuokjUrX+ezdYzhqxOHCByZoSPSOwuVqtYxaOIZBeBnsr I98yr+u03+GUOHmfc7IUCzgHHnc3OQMlz5cY5I4iyXn3VUP+vpcgAeitDDSDnONgtJFK IExnCPR4/tarewBBWCqbC5HyhV6dUXCUe66ZGzzf+glDqyVCrHEnIHr6VunbY121OQzy qNG0WO4aT5F6Jlh1CzimcPIpanxfof1y4vkTdblLJu/NiPdQe+XnBZZ10ZLHQUDDfAnT LF2gCiZwmMR9mL+M2Q5KG4VtVLKK7yN07CrfAsJbS1o3Fm6fM9qMrKeAEqaxucT0A9Zg oc8A== X-Gm-Message-State: AOJu0YwsD+IUspRScskIMR4Z2WxRMGO59y36oAkIuP/yrLFNWa/V2dGm Sd7CzvpC2RTUZ7WE7vckZElEvGKdJa31eyZiG/aIZxt1UFZvTJ7IUTZdniP4WvUQJNgZOrE7rxS VqS9ooxOWQOR8q5qbIwyNIIeWfmg7p0RNq7If2HcJTW7qP+2FHIsjX99PHtd4mbyXSn7MvibSMS A= X-Google-Smtp-Source: AGHT+IElnmUArrwRtw8PXBadA7pO7PNlkAO5OhN71FcA1ynqOF6NgU63f/3p2b9CCS3ny34D2cGiwWnp X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:adf:e542:0:b0:31d:c574:a5dc with SMTP id z2-20020adfe542000000b0031dc574a5dcmr167938wrm.14.1694528231486; Tue, 12 Sep 2023 07:17:11 -0700 (PDT) Date: Tue, 12 Sep 2023 14:15:50 +0000 Mime-Version: 1.0 X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=12097; i=ardb@kernel.org; h=from:subject; bh=Xg9fdOPHHI9ZsIosap6qrrFhaLojuEc3HzMtIu2FMdA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWhaGr534TthpbHHu/+uKMzeOOMtzxq5gVNKau59l+UO X7to8icjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjAR7QiGP3zfLKSrs/jEj/9n PvnugL2qd14w/3rXJdZuGiozJjHsmcTwh/+O+woGHpGb0z6UelRFeds8Xes747L0OrbdLs/WztR oYgYA X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-63-ardb@google.com> Subject: [PATCH v4 00/61] arm64: Add support for LPA2 at stage1 and WXN 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_071714_719811_7AE8B1BA X-CRM114-Status: GOOD ( 37.34 ) 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 This is a followup to [0], which was sent out more than 6 months ago. Thanks to Ryan and Mark for feedback and review. This series is independent from Ryan's work on adding support for LPA2 to KVM - the only potential source of conflict should be the patch "arm64: kvm: Limit HYP VA and host S2 range to 48 bits when LPA2 is in effect", which could simply be dropped in favour of the KVM changes to make it support LPA2. Changes since v3: - add some acks and incorporate some minor suggested tweaks, mostly related to coding style and comments - rebase onto v6.6-rc1 - add patch to deal with references to PTE_MAYBE_NG from asm code - add patch to move dummy 'nokaslr' parsing routine out of idreg-override.c - rework ptdump address marker array population NOTE: this series still does not address the TLBI changes needed for LPA2 and 5 level paging. Ryan seems to have a good handle on those, and this work is complementary with his KVM work to a fair extent anyway. -%- The first ~15 patches of this series rework how the kernel VA space is organized, so that the vmemmap region does not take up more space than necessary, and so that most of it can be reclaimed when running a build capable of 52-bit virtual addressing on hardware that is not. This is needed because the vmemmap region will take up a substantial part of the upper VA region that it shares with the kernel, modules and vmalloc/vmap mappings once we enable LPA2 with 4k pages. The next ~30 patches rework the early init code, reimplementing most of the page table and relocation handling in C code. There are several reasons why this is beneficial: - we generally prefer C code over asm for these things, and the macros that currently exist in head.S for creating the kernel pages tables are a good example why; - we no longer need to create the kernel mapping in two passes, which means we can remove the logic that copies parts of the fixmap and the KAsan shadow from one set of page tables to the other; this is especially advantageous for KAsan with LPA2, which needs more elaborate shadow handling across multiple levels, since the KAsan region cannot be placed on exact pgd_t boundaries in that case; - we can read the ID registers and parse command line overrides before creating the page tables, which simplifies the LPA2 case, as flicking the global TCR_EL1.DS bit at a later stage would require elaborate repainting of all page table descriptors, some of which with the MMU disabled; - we can use more elaborate logic to create the mappings, which means we can use more precise mappings for code and data sections even when using 2 MiB granularity, and this is a prerequisite for running with WXN. As part of the ID map changes, we decouple the ID map size from the kernel VA size, and switch to a 48-bit VA map for all configurations. The next 18 patches rework the existing LVA support as a CPU feature, which simplifies some code and gets rid of the vabits_actual variable. Then, LPA2 support is implemented in the same vein. This requires adding support for 5 level paging as well, given that LPA2 introduces a new paging level '-1' when using 4k pages. Combined with the vmemmap changes at the start of the series, the resulting LPA2/4k pages configuration will have the exact same VA space layout as the ordinary 4k/4 levels configuration, and so LPA2 support can reasonably be enabled by default, as the fallback is seamless on non-LPA2 hardware. In the 16k/LPA2 case, the fallback also reduces the number of paging levels, resulting in a 47-bit VA space. This is based on the assumption that hybrid LPA2/non-LPA2 16k pages kernels in production use would prefer not to take the performance hit of 4 level paging to gain only a single additional bit of VA space. (Note that generic Android kernels use only 3 levels of paging today.) Bespoke 16k configurations can still configure 48-bit virtual addressing as before. Finally, enable support for running with the WXN control enabled. This was previously part of a separate series [1], but given that the delta is tiny, it is included here as well. [0] https://lore.kernel.org/all/20230307140522.2311461-1-ardb@kernel.org/ [1] https://lore.kernel.org/all/20221111171201.2088501-1-ardb@kernel.org/ Cc: Catalin Marinas Cc: Will Deacon Cc: Marc Zyngier Cc: Mark Rutland Cc: Ryan Roberts Cc: Anshuman Khandual Cc: Kees Cook Cc: Joey Gouly Anshuman Khandual (2): arm64/mm: Add FEAT_LPA2 specific TCR_EL1.DS field arm64/mm: Add FEAT_LPA2 specific ID_AA64MMFR0.TGRAN[2] Ard Biesheuvel (59): arm64: kernel: Disable latent_entropy GCC plugin in early C runtime arm64: mm: Take potential load offset into account when KASLR is off arm64: mm: get rid of kimage_vaddr global variable arm64: mm: Move PCI I/O emulation region above the vmemmap region arm64: mm: Move fixmap region above vmemmap region arm64: ptdump: Allow all region boundaries to be defined at boot time arm64: ptdump: Discover start of vmemmap region at runtime arm64: vmemmap: Avoid base2 order of struct page size to dimension region arm64: mm: Reclaim unused vmemmap region for vmalloc use arm64: kaslr: Adjust randomization range dynamically arm64: kernel: Manage absolute relocations in code built under pi/ arm64: kernel: Don't rely on objcopy to make code under pi/ __init arm64: head: move relocation handling to C code arm64: idreg-override: Omit non-NULL checks for override pointer arm64: idreg-override: Prepare for place relative reloc patching arm64: idreg-override: Avoid parameq() and parameqn() arm64: idreg-override: avoid strlen() to check for empty strings arm64: idreg-override: Avoid sprintf() for simple string concatenation arm64: idreg-override: Avoid kstrtou64() to parse a single hex digit arm64: idreg-override: Move to early mini C runtime arm64: kernel: Remove early fdt remap code arm64: head: Clear BSS and the kernel page tables in one go arm64: Move feature overrides into the BSS section arm64: head: Run feature override detection before mapping the kernel arm64: head: move dynamic shadow call stack patching into early C runtime arm64: kaslr: Use feature override instead of parsing the cmdline again arm64/kernel: Move 'nokaslr' parsing out of early idreg code arm64: idreg-override: Create a pseudo feature for rodata=off arm64: Add helpers to probe local CPU for PAC and BTI support arm64: head: allocate more pages for the kernel mapping arm64: head: move memstart_offset_seed handling to C code arm64: mm: Make kaslr_requires_kpti() a static inline arm64: head: Move early kernel mapping routines into C code arm64: mm: Use 48-bit virtual addressing for the permanent ID map arm64: pgtable: Decouple PGDIR size macros from PGD/PUD/PMD levels arm64: kernel: Create initial ID map from C code arm64: mm: avoid fixmap for early swapper_pg_dir updates arm64: mm: omit redundant remap of kernel image arm64: Revert "mm: provide idmap pointer to cpu_replace_ttbr1()" arm64: mmu: Make cpu_replace_ttbr1() out of line arm64: mm: Handle LVA support as a CPU feature arm64: mm: Add feature override support for LVA arm64/mm: Avoid #define'ing PTE_MAYBE_NG to 0x0 for asm use arm64: mm: Wire up TCR.DS bit to PTE shareability fields arm64: mm: Add LPA2 support to phys<->pte conversion routines arm64: mm: Add definitions to support 5 levels of paging arm64: mm: add LPA2 and 5 level paging support to G-to-nG conversion arm64: Enable LPA2 at boot if supported by the system arm64: mm: Add 5 level paging support to fixmap and swapper handling arm64: kasan: Reduce minimum shadow alignment and enable 5 level paging arm64: mm: Add support for folding PUDs at runtime arm64: ptdump: Disregard unaddressable VA space arm64: ptdump: Deal with translation levels folded at runtime arm64: kvm: avoid CONFIG_PGTABLE_LEVELS for runtime levels arm64: kvm: Limit HYP VA and host S2 range to 48 bits when LPA2 is in effect arm64: Enable 52-bit virtual addressing for 4k and 16k granule configs arm64: defconfig: Enable LPA2 support mm: add arch hook to validate mmap() prot flags arm64: mm: add support for WXN memory translation attribute arch/arm64/Kconfig | 34 +- arch/arm64/configs/defconfig | 2 +- arch/arm64/include/asm/archrandom.h | 2 - arch/arm64/include/asm/assembler.h | 55 +-- arch/arm64/include/asm/cpufeature.h | 94 ++++ arch/arm64/include/asm/fixmap.h | 2 +- arch/arm64/include/asm/kasan.h | 2 - arch/arm64/include/asm/kernel-pgtable.h | 128 ++---- arch/arm64/include/asm/memory.h | 37 +- arch/arm64/include/asm/mman.h | 36 ++ arch/arm64/include/asm/mmu.h | 40 +- arch/arm64/include/asm/mmu_context.h | 86 ++-- arch/arm64/include/asm/pgalloc.h | 53 ++- arch/arm64/include/asm/pgtable-hwdef.h | 33 +- arch/arm64/include/asm/pgtable-prot.h | 22 +- arch/arm64/include/asm/pgtable-types.h | 6 + arch/arm64/include/asm/pgtable.h | 229 +++++++++- arch/arm64/include/asm/scs.h | 36 +- arch/arm64/include/asm/setup.h | 3 - arch/arm64/include/asm/sysreg.h | 2 + arch/arm64/include/asm/tlb.h | 3 +- arch/arm64/kernel/Makefile | 7 +- arch/arm64/kernel/cpufeature.c | 104 +++-- arch/arm64/kernel/head.S | 465 ++------------------ arch/arm64/kernel/image-vars.h | 35 ++ arch/arm64/kernel/kaslr.c | 11 +- arch/arm64/kernel/module.c | 2 +- arch/arm64/kernel/pi/Makefile | 28 +- arch/arm64/kernel/{ => pi}/idreg-override.c | 221 ++++++---- arch/arm64/kernel/pi/kaslr_early.c | 78 +--- arch/arm64/kernel/pi/map_kernel.c | 278 ++++++++++++ arch/arm64/kernel/pi/map_range.c | 105 +++++ arch/arm64/kernel/{ => pi}/patch-scs.c | 36 +- arch/arm64/kernel/pi/pi.h | 30 ++ arch/arm64/kernel/pi/relacheck.c | 130 ++++++ arch/arm64/kernel/pi/relocate.c | 64 +++ arch/arm64/kernel/setup.c | 22 - arch/arm64/kernel/sleep.S | 3 - arch/arm64/kernel/suspend.c | 2 +- arch/arm64/kernel/vmlinux.lds.S | 17 +- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 + arch/arm64/kvm/mmu.c | 22 +- arch/arm64/kvm/va_layout.c | 9 +- arch/arm64/mm/fixmap.c | 36 +- arch/arm64/mm/init.c | 2 +- arch/arm64/mm/kasan_init.c | 154 +++++-- arch/arm64/mm/mmap.c | 4 + arch/arm64/mm/mmu.c | 267 ++++++----- arch/arm64/mm/pgd.c | 17 +- arch/arm64/mm/proc.S | 122 ++++- arch/arm64/mm/ptdump.c | 77 ++-- arch/arm64/tools/cpucaps | 1 + include/linux/mman.h | 15 + mm/mmap.c | 3 + 54 files changed, 2078 insertions(+), 1196 deletions(-) rename arch/arm64/kernel/{ => pi}/idreg-override.c (53%) create mode 100644 arch/arm64/kernel/pi/map_kernel.c create mode 100644 arch/arm64/kernel/pi/map_range.c rename arch/arm64/kernel/{ => pi}/patch-scs.c (89%) create mode 100644 arch/arm64/kernel/pi/pi.h create mode 100644 arch/arm64/kernel/pi/relacheck.c create mode 100644 arch/arm64/kernel/pi/relocate.c