From patchwork Tue Sep 12 14:16:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13381809 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 2538FCA0EEB for ; Tue, 12 Sep 2023 14:21:09 +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=Ds+WE+f3pm319hiZlXLLmMZrVcR2lACzPbKIbUkKchU=; b=FmnBul6nvyrONJBqMV+yTLKJpa gEasm8EHCawmDkkyDNvO/Kms7kkz326+pQcWQgT5TfJi4PAZAD/V5bMlEzYwAHlIxAdMKe6ZkzJdK WQ5SZKtXIfDseo6GfbGYtk8ci3oWdchr69ysCYVqmWHXJa39f5dAphVpuWpztzeYfbDqt6S/9XwTW w1UpvB7gw9W/bIN5KrNmuWLwuRNcMSgnolGfRWXNVbZxYcRAlcvzl2SYZcVifWmxNgmkoOJ/4a7mr 28re9WD85TUr8vbAnqRDQvvOBbT7K2ROpYQcf/IzrH8KNaprVUcCsMvRkV2OZkhby1u6fM0nBfr+k UV2xO7bA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Fl-003YMn-1x; Tue, 12 Sep 2023 14:20:33 +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 1qg4EW-003XJw-19 for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:19:25 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58e49935630so104552307b3.0 for ; Tue, 12 Sep 2023 07:19:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528354; x=1695133154; 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=7BFmaSziYEEiu5DiUfOMF9ycyq5S9ydLnoCOi72SCs4=; b=g8crgThcKeqTr1Ozb3XpPnhAcU3AjbcmDTnjzCNSbd7gRJfAt+SYGM3sjGLcPOa5BK ralQbeKKqcsDOq5wMJXORvZ/LNhpIaG1vCZPhFOUCRwm5/n6y7Ap2m+X7Cr0Jy3QJs6s sOQmDGSg2eYPm/MYJK9mu1jnnhwxNBUAu7a/JegnSRM9mG9C4nwKfhu4u9aXeOCkpTfx fepiey3O/jfRZK+12iEhnyNBqvgFweqRD3lQB9o7Ajr118XH00K2670oZx07RuWelSOz 4BTSEmVaji81ih5ZUg0NyFNy4pqts5G/V/YbSvqnVb34GezRsb9vyQSJjVLhG8eqIdS9 h5yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528354; x=1695133154; 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=7BFmaSziYEEiu5DiUfOMF9ycyq5S9ydLnoCOi72SCs4=; b=tO81Toz2R8rmrWHsZcsL7MURntZV0FRiLN04hX59G2+0znZF2bUpLtH4vlrUNTH/ZJ vohvUMS8qAiJE9zjBFExkBAauT8eMiiyJCQJZ02LVLe9hIPtq8qAYOATIUcWDAcTUrXJ EwsM7hzFu+Ny3QLEoKm8PcSQ+FWC8L3qw3cVMJ7lcZhltFjjf9qOzXYQAMKQV/VcOgSK YUt7Vft7F/YhBTHL4eCNF+jY+fgxYgE+9j5HOGMlY7sDQDC4ifXe2ZYoksEMJpHHy20P WXvwiiMCgSHlvdcauDNPhyuve3slC/1EN1kWC8QfM76ofOqBHhbQCHOfxhOnSRJEO1GX F/rw== X-Gm-Message-State: AOJu0YyA3DFQWXb5T+YWQrVIMXQH2eIyB381E8hZ/tlAIl84tKnKA4Pw IHCmj10lxQnGnyAu343qUx1Il8iP5wMY0n6QUwjAI9VZ4Ad+SNL3mSuwmLtStyLjVM0+KXBRIxr +nxxhjjgRmoezqS0QMCCrdVgpeCweCIP0PV0k6bgVCD94hrp9OASd5TD4sXvA6ZFuAiukM+KTle U= X-Google-Smtp-Source: AGHT+IFDXWvktD9Qvfuk4AAck/vdyq3TLq/UH3KTSPcR6uU3mesB/qhDLqanJ3ZGfEEBbxYe2FEkRoVW X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:690c:2d13:b0:589:a533:405b with SMTP id eq19-20020a05690c2d1300b00589a533405bmr63505ywb.3.1694528354354; Tue, 12 Sep 2023 07:19:14 -0700 (PDT) Date: Tue, 12 Sep 2023 14:16:39 +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=6547; i=ardb@kernel.org; h=from:subject; bh=qXSALkyQaGwy5loMod8vAXTsYLFRxnid2WwT8yST+cU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWh6ORZjZqMHYamfFNyn949+Kjnl+jrT73BEtsuGHD1P lY13WzVUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACZy7Tojw8zqtK072694/fCT 73W9ZBH0IcftxqRP4VzZKafW9l239WX4ny0kPfv0ug0TMjYFvHJsuJN08/EKjU2mz051L3as3CO nygUA X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-112-ardb@google.com> Subject: [PATCH v4 49/61] arm64: mm: add LPA2 and 5 level paging support to G-to-nG conversion 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_071916_439638_EB8A019A X-CRM114-Status: GOOD ( 21.28 ) 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 Add support for 5 level paging in the G-to-nG routine that creates its own temporary page tables to traverse the swapper page tables. Also add support for running the 5 level configuration with the top level folded at runtime, to support CPUs that do not implement the LPA2 extension. While at it, wire up the level skipping logic so it will also trigger on 4 level configurations with LPA2 enabled at build time but not active at runtime, as we'll fall back to 3 level paging in that case. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/cpufeature.c | 9 ++- arch/arm64/mm/proc.S | 70 +++++++++++++++++--- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index a2c320ed36b3..f4c81300cb13 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1753,6 +1753,9 @@ kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused) if (arm64_use_ng_mappings) return; + if (levels == 5 && !pgtable_l5_enabled()) + levels = 4; + remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings); if (!cpu) { @@ -1766,9 +1769,9 @@ kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused) // // The physical pages are laid out as follows: // - // +--------+-/-------+-/------ +-\\--------+ - // : PTE[] : | PMD[] : | PUD[] : || PGD[] : - // +--------+-\-------+-\------ +-//--------+ + // +--------+-/-------+-/------ +-/------ +-\\\--------+ + // : PTE[] : | PMD[] : | PUD[] : | P4D[] : ||| PGD[] : + // +--------+-\-------+-\------ +-\------ +-///--------+ // ^ // The first page is mapped into this hierarchy at a PMD_SHIFT // aligned virtual address, so that we can manipulate the PTE diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S index 31be0d16241d..d60366144031 100644 --- a/arch/arm64/mm/proc.S +++ b/arch/arm64/mm/proc.S @@ -216,16 +216,15 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1) .macro kpti_mk_tbl_ng, type, num_entries add end_\type\()p, cur_\type\()p, #\num_entries * 8 .Ldo_\type: - ldr \type, [cur_\type\()p] // Load the entry + ldr \type, [cur_\type\()p], #8 // Load the entry and advance tbz \type, #0, .Lnext_\type // Skip invalid and tbnz \type, #11, .Lnext_\type // non-global entries orr \type, \type, #PTE_NG // Same bit for blocks and pages - str \type, [cur_\type\()p] // Update the entry + str \type, [cur_\type\()p, #-8] // Update the entry .ifnc \type, pte tbnz \type, #1, .Lderef_\type .endif .Lnext_\type: - add cur_\type\()p, cur_\type\()p, #8 cmp cur_\type\()p, end_\type\()p b.ne .Ldo_\type .endm @@ -235,18 +234,18 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1) * fixmap slot associated with the current level. */ .macro kpti_map_pgtbl, type, level - str xzr, [temp_pte, #8 * (\level + 1)] // break before make + str xzr, [temp_pte, #8 * (\level + 2)] // break before make dsb nshst - add pte, temp_pte, #PAGE_SIZE * (\level + 1) + add pte, temp_pte, #PAGE_SIZE * (\level + 2) lsr pte, pte, #12 tlbi vaae1, pte dsb nsh isb phys_to_pte pte, cur_\type\()p - add cur_\type\()p, temp_pte, #PAGE_SIZE * (\level + 1) + add cur_\type\()p, temp_pte, #PAGE_SIZE * (\level + 2) orr pte, pte, pte_flags - str pte, [temp_pte, #8 * (\level + 1)] + str pte, [temp_pte, #8 * (\level + 2)] dsb nshst .endm @@ -279,6 +278,8 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) end_ptep .req x15 pte .req x16 valid .req x17 + cur_p4dp .req x19 + end_p4dp .req x20 mov x5, x3 // preserve temp_pte arg mrs swapper_ttb, ttbr1_el1 @@ -286,6 +287,12 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) cbnz cpu, __idmap_kpti_secondary +#if CONFIG_PGTABLE_LEVELS > 4 + stp x29, x30, [sp, #-32]! + mov x29, sp + stp x19, x20, [sp, #16] +#endif + /* We're the boot CPU. Wait for the others to catch up */ sevl 1: wfe @@ -303,9 +310,32 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) mov_q pte_flags, KPTI_NG_PTE_FLAGS /* Everybody is enjoying the idmap, so we can rewrite swapper. */ + +#ifdef CONFIG_ARM64_LPA2 + /* + * If LPA2 support is configured, but 52-bit virtual addressing is not + * enabled at runtime, we will fall back to one level of paging less, + * and so we have to walk swapper_pg_dir as if we dereferenced its + * address from a PGD level entry, and terminate the PGD level loop + * right after. + */ + adrp pgd, swapper_pg_dir // walk &swapper_pg_dir at the next level + mov cur_pgdp, end_pgdp // must be equal to terminate the PGD loop +alternative_if_not ARM64_HAS_VA52 + b .Lderef_pgd // skip to the next level +alternative_else_nop_endif + /* + * LPA2 based 52-bit virtual addressing requires 52-bit physical + * addressing to be enabled as well. In this case, the shareability + * bits are repurposed as physical address bits, and should not be + * set in pte_flags. + */ + bic pte_flags, pte_flags, #PTE_SHARED +#endif + /* PGD */ adrp cur_pgdp, swapper_pg_dir - kpti_map_pgtbl pgd, 0 + kpti_map_pgtbl pgd, -1 kpti_mk_tbl_ng pgd, PTRS_PER_PGD /* Ensure all the updated entries are visible to secondary CPUs */ @@ -318,16 +348,33 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) /* Set the flag to zero to indicate that we're all done */ str wzr, [flag_ptr] +#if CONFIG_PGTABLE_LEVELS > 4 + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #32 +#endif ret .Lderef_pgd: + /* P4D */ + .if CONFIG_PGTABLE_LEVELS > 4 + p4d .req x30 + pte_to_phys cur_p4dp, pgd + kpti_map_pgtbl p4d, 0 + kpti_mk_tbl_ng p4d, PTRS_PER_P4D + b .Lnext_pgd + .else /* CONFIG_PGTABLE_LEVELS <= 4 */ + p4d .req pgd + .set .Lnext_p4d, .Lnext_pgd + .endif + +.Lderef_p4d: /* PUD */ .if CONFIG_PGTABLE_LEVELS > 3 pud .req x10 - pte_to_phys cur_pudp, pgd + pte_to_phys cur_pudp, p4d kpti_map_pgtbl pud, 1 kpti_mk_tbl_ng pud, PTRS_PER_PUD - b .Lnext_pgd + b .Lnext_p4d .else /* CONFIG_PGTABLE_LEVELS <= 3 */ pud .req pgd .set .Lnext_pud, .Lnext_pgd @@ -371,6 +418,9 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings) .unreq end_ptep .unreq pte .unreq valid + .unreq cur_p4dp + .unreq end_p4dp + .unreq p4d /* Secondary CPUs end up here */ __idmap_kpti_secondary: