From patchwork Tue Jan 23 14:53:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13527716 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 E8105C47258 for ; Tue, 23 Jan 2024 16:00:38 +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=/omRSc5jnA21AiUvqAqWUDQ2oBFE/zt2piXhvNbno0Q=; b=e6hZozFEhJ1jTdLdhGZ2sxZbpQ /+7AVl1WVWIuxYfIaYf1iyIMECtpcVSxGB806PWggwJERxcX34oJPGb1QrzVPrCn8euXK4be9HaAo rnSf+SGBTuc2gEa2ElO8uBhP7smioVPcTeaJiyS5yH6hcj1q5HQNudAJH//nxW0OEAK7RxK/TIUMA ZF8ZZXEOyIDl8gO7NZMsg6bPuH4zWN1u0XUwYfX7myOCOpueZYPSKPnE3eS6p9zjf/S0m1SlOCm52 NwdvS8WvV2U0JV/y+auPXoV2s277bzGlvUxA2wuN8HgCAAQueWC4D4VHWAg3ia994Hp9CtQ9Lg8e1 vaG+Ccww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSJCW-00HAuu-0q; Tue, 23 Jan 2024 16:00:36 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSIBy-00GuQf-0F for linux-arm-kernel@bombadil.infradead.org; Tue, 23 Jan 2024 14:55:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=AviN0IFXbzaVb7eLl7lq4OId7ilOzOEDH0/Mv3Ok3Ak=; b=YVwAMisXCHPVcOL4UGcCvaYdDR eN6uVh0AMjfzl0oDFfMBpS+YLC5NyxZiPGAGnF1quUhm+Tv5MJ776iKZZ3AWy2UZpuiKL3YE7/FOW MXxeNEQP0etVawHqryBzfiAeGMm0NGscRR/CqhmmuZZKn7NY7VBNNGAsa9oM0zr6avU6yGYJxzTXW XZ0kBcZQNOgq9Ea2zW6LXIvIVVDQaCaS6mqZw7ZETh2CmeneMxMgQByBivznIHBvjJXci5iNXccFo SVDQsf1py+ldkIGNOJcBOAgKSsAK54jZeERJy8cS4SwidHws1wlZN1jj8W4mR+yhgryPeoK/YGvTR pSTJF0DQ==; Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rSIBu-00000003vZ5-2j6r for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 14:55:56 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dc374283d67so1098074276.3 for ; Tue, 23 Jan 2024 06:55:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706021753; x=1706626553; 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=AviN0IFXbzaVb7eLl7lq4OId7ilOzOEDH0/Mv3Ok3Ak=; b=UGE6k4shy9dCq3yhSVlCkpkMu1Zp/AKkJXVmAf5b2lPqmjev7A/DUKdjuS4BFTzV3S JIHpq6s4mV2vDQuAtm4AQS7BgkGU+AacFm40p9qA+Qw33/7G99OMrnySCXGu1aCaBeZD UdlXQYuoyJfxX8xjVnpV2pcgaAkcC4hIcDf6ewlvs93L3XJPN2lazCy2gqi5Y2Ps27UF c8LQeupQ84E9U9dJBY2vcmWttZwEdjnluN2O0IaK0X1nE4ysBOXcOXSjpKBuu7cihP+y gMJnpWdHKjwcyryffd5V65v8TJbe/QNB9xob8ujvNF39TGEcGseLFwIyeYpTNqEQHdnL LPSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706021753; x=1706626553; 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=AviN0IFXbzaVb7eLl7lq4OId7ilOzOEDH0/Mv3Ok3Ak=; b=Yyq1HpYM/vsC/XMvzgTF3zYZ5dBJNfqR3AZUqRFWQd8EM84spi/kllhRPbUmTv/kvQ +opFl0pa1YcbZZvpxtkiCJkiHyxgyWaglTrE0nrthTH2m+6xmDUrVF6kS6E+BfUnyVN2 /UDe29+98+H4ih3hmnGLAVq1s0lmPSZr8xdR2Yxmg+PHYkmCSvKIGAk4l8G+ToNMLAY1 WNtEXu1B2w2vXQ45l16rosfwAMp9E4nTDemMfblW+rokErr0nBdSnYLv3QTBUzRZN8LT gX9G83MTkV/A6ryndZqCtHa9DSNhLMyAi4j7MN1i/2ugaNE09Gkqqpb3WVzY6X4G2LFs Lb/A== X-Gm-Message-State: AOJu0Yybmd6nVYFIPDZ0rqaCobDDJXGM9zkwoAcXWx5pXmNfIjSyYvqM PJug3tKfSWKjZzlmr0+ahR+FGXkgWcYcaLpLl+9Y0JKXoAIW/oGqp9oFMSa0POXsV/wW+D7+Tfz 0hihnlypiKLVBw7kH7odMqR9KEnyIsyXyGHqT8mcrkkhpSRsCyF5IcJfOZ2Mw9tDGPi9k70R27x VqH7qU+2q/oWyHDQBqEqMhycOnnS1ebar5kzprMnSk X-Google-Smtp-Source: AGHT+IHOQJwFpfqdkpWfUOkI2kHhnBNX4/vMO4PgBOKo6ZBBF8eEpT+GMVWAEbeoYrxPiuB1Ji/FGqp2 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a25:4f55:0:b0:dc2:4fff:75e9 with SMTP id d82-20020a254f55000000b00dc24fff75e9mr2809681ybb.8.1706021752733; Tue, 23 Jan 2024 06:55:52 -0800 (PST) Date: Tue, 23 Jan 2024 15:53:38 +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=6529; i=ardb@kernel.org; h=from:subject; bh=XYJCQgbo4x5GPyU84XxVjSPhiwpdx2vqDizAij74NQA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX9pa8Nfl+6uJOdozYaqP3Q+vGBafOeNa69rrwyb/qP/ jqfYpndUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACZyV5zhn0XPqZiF+hcPb+qw 3sL2Uzf4ss88691bLyi231z/aPPDDRcZ/uktzea/uNmpJIO5dWLzpyq1ZaujdxaoSV6wyjX1K93 RxAwA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123145258.1462979-91-ardb+git@google.com> Subject: [PATCH v7 39/50] 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240123_145555_069560_A237DE90 X-CRM114-Status: GOOD ( 20.79 ) 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 ed9670d8360c..bc5e4e569864 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1765,6 +1765,9 @@ static int __init __kpti_install_ng_mappings(void *__unused) pgd_t *kpti_ng_temp_pgd; u64 alloc = 0; + if (levels == 5 && !pgtable_l5_enabled()) + levels = 4; + remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings); if (!cpu) { @@ -1778,9 +1781,9 @@ static int __init __kpti_install_ng_mappings(void *__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 d03434b7bca5..fa0d7c63f8d2 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: