All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Curtin <ericcurtin17@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: Ryan Roberts <ryan.roberts@arm.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>
Subject: [RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot
Date: Tue,  1 Dec 2026 01:52:04 +0000	[thread overview]
Message-ID: <20261201015240.1017259-33-ericcurtin17@gmail.com> (raw)
In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com>

From: Ryan Roberts <ryan.roberts@arm.com>

TRAMP_VALIAS is the fixmap address of the entry trampoline, and with
boot-time page size selection it depends on PAGE_SIZE and so is not a
compile-time constant. The tramp_alias macro in entry.S and the Falkor
erratum 1003 TLB invalidation in the trampoline both encode it as
immediates in movz/movk instructions.

With CONFIG_ARM64_BOOT_TIME_PAGE_SIZE, wrap these instruction sequences
in alternative_cb and patch in the final value once the page size is
known. tramp_alias initially encodes the offset of the symbol within the
trampoline text; kpti_patch_tramp_alias() recovers that offset, adds
TRAMP_VALIAS and rewrites the two instructions as a movn/movk pair, as
the fixmap lies in the top 2GiB. qcom_erratum_1003_patch_tramp_tlbi()
fills in the three movk instructions that hold the address bits for the
TLBI. TRAMP_VALIAS is no longer exported through asm-offsets in this
configuration.

Builds without boot-time page size are unchanged.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 30e1b94283a4ebe3ef362fd5a7befcc1cfe4ee9a)
---
 arch/arm64/kernel/asm-offsets.c |  2 +-
 arch/arm64/kernel/cpu_errata.c  | 53 +++++++++++++++++++++++++++++++++
 arch/arm64/kernel/entry.S       | 34 +++++++++++++++++++++
 arch/arm64/mm/mmu.c             | 50 +++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
index 9d603317eeb9..c7490746d760 100644
--- a/arch/arm64/kernel/asm-offsets.c
+++ b/arch/arm64/kernel/asm-offsets.c
@@ -153,7 +153,7 @@ int main(void)
   DEFINE(HIBERN_PBE_NEXT,	offsetof(struct pbe, next));
   DEFINE(ARM64_FTR_SYSVAL,	offsetof(struct arm64_ftr_reg, sys_val));
   BLANK();
-#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+#if defined(CONFIG_UNMAP_KERNEL_AT_EL0) && !defined(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE)
   DEFINE(TRAMP_VALIAS,		TRAMP_VALIAS);
 #endif
 #ifdef CONFIG_ARM_SDE_INTERFACE
diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c
index 0ded97d7cbec..6a7ae41c7147 100644
--- a/arch/arm64/kernel/cpu_errata.c
+++ b/arch/arm64/kernel/cpu_errata.c
@@ -507,6 +507,59 @@ static const struct arm64_cpu_capabilities qcom_erratum_1003_list[] = {
 	},
 	{},
 };
+
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+/* Only called from assembly, silence sparse */
+void __init qcom_erratum_1003_patch_tramp_tlbi(struct alt_instr *alt,
+					       __le32 *origptr, __le32 *updptr,
+					       int nr_inst);
+
+void __init qcom_erratum_1003_patch_tramp_tlbi(struct alt_instr *alt,
+					       __le32 *origptr, __le32 *updptr,
+					       int nr_inst)
+{
+	u32 insn, rd;
+	u64 val;
+
+	/*
+	 * ASID already in rd[63:48]. Bottom 48 bits, vaddr for TRAMP_VALIAS,
+	 * are different per page size, so are patched in at boot. Need 3 movk
+	 * instructions to do that.
+	 */
+	BUG_ON(nr_inst != 3);
+
+	/* 2MB boundary containing the vectors, so we nobble the walk cache */
+	val = (TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12;
+
+	/* Compute target register */
+	insn = le32_to_cpu(*origptr);
+	rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
+
+	/* movk rd, #(val & 0xffff) */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)val,
+					 0,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+
+	/* movk rd, #((val >> 16) & 0xffff), lsl #16 */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)(val >> 16),
+					 16,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+
+	/* movk rd, #((val >> 32) & 0xffff), lsl #32 */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)(val >> 32),
+					 32,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
 #endif
 
 #ifdef CONFIG_ARM64_WORKAROUND_CLEAN_CACHE
diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
index 8d17f61d1b5b..6c03468ae0dd 100644
--- a/arch/arm64/kernel/entry.S
+++ b/arch/arm64/kernel/entry.S
@@ -101,10 +101,25 @@
 	.endm
 
 	.macro	tramp_alias, dst, sym
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+	/*
+	 * TRAMP_VALIAS depends on page size, so let's initially stuff these
+	 * instructions with the offset of the symbol within the tramp text
+	 * section. Then at boot time, kpti_patch_tramp_alias() recovers the
+	 * offset, adds TRAMP_VALIAS and patches the final value back into the
+	 * instructions.
+	 */
+	.set	.Lsymoff\@, \sym - .entry.tramp.text
+alternative_cb ARM64_ALWAYS_BOOT, kpti_patch_tramp_alias
+	movz	\dst, :abs_g0_nc:.Lsymoff\@
+	movk	\dst, :abs_g1_nc:.Lsymoff\@
+alternative_cb_end
+#else
 	.set	.Lalias\@, TRAMP_VALIAS + \sym - .entry.tramp.text
 	movz	\dst, :abs_g2_s:.Lalias\@
 	movk	\dst, :abs_g1_nc:.Lalias\@
 	movk	\dst, :abs_g0_nc:.Lalias\@
+#endif
 	.endm
 
 	/*
@@ -635,6 +650,24 @@ SYM_CODE_END(ret_to_user)
 	bic	\tmp, \tmp, #USER_ASID_FLAG
 	msr	ttbr1_el1, \tmp
 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+alternative_if_not ARM64_WORKAROUND_QCOM_FALKOR_E1003
+	b	.Lskip_falkor_e1003\@
+alternative_else_nop_endif
+	/*
+	 * ASID already in \tmp[63:48]. Bottom 48 bits, vaddr for TRAMP_VALIAS,
+	 * are different per page size, so are patched in at boot.
+	 */
+alternative_cb ARM64_ALWAYS_BOOT, qcom_erratum_1003_patch_tramp_tlbi
+	movk	\tmp, #0
+	movk	\tmp, #0, lsl #16
+	movk	\tmp, #0, lsl #32
+alternative_cb_end
+	isb
+	tlbi	vae1, \tmp
+	dsb	nsh
+.Lskip_falkor_e1003\@:
+#else
 alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
 	/* ASID already in \tmp[63:48] */
 	movk	\tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
@@ -645,6 +678,7 @@ alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
 	tlbi	vae1, \tmp
 	dsb	nsh
 alternative_else_nop_endif
+#endif
 #endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
 	.endm
 
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index e1f06f622f32..0cc8c8f27e0a 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -34,6 +34,7 @@
 #include <asm/barrier.h>
 #include <asm/cputype.h>
 #include <asm/fixmap.h>
+#include <asm/insn.h>
 #include <asm/kasan.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/sections.h>
@@ -1406,6 +1407,55 @@ void __init kpti_install_ng_mappings(void)
 	stop_machine(__kpti_install_ng_mappings, NULL, cpu_online_mask);
 }
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+/* Only called from assembly, silence sparse */
+void __init kpti_patch_tramp_alias(struct alt_instr *alt, __le32 *origptr,
+				   __le32 *updptr, int nr_inst);
+
+void __init kpti_patch_tramp_alias(struct alt_instr *alt, __le32 *origptr,
+				   __le32 *updptr, int nr_inst)
+{
+	u64 imm, val = 0;
+	u32 insn, rd;
+	int i;
+
+	BUG_ON(nr_inst != 2);
+
+	/* The fixmap is in the top 2GiB, so VA bits [63:32] are all ones */
+	BUILD_BUG_ON(__FIXADDR_TOT_START(PAGE_SHIFT_MAX) < -UL(SZ_2G));
+
+	/* Compute target register */
+	insn = le32_to_cpu(*origptr);
+	rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
+
+	/* Recover symbol offset; 16 bits per instruction, stored low to high */
+	for (i = 0; i < 2; i++, origptr++) {
+		insn = le32_to_cpu(*origptr);
+		imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn);
+		val |= imm << (i * 16);
+	}
+
+	/* Calculate final address in fixmap */
+	val += TRAMP_VALIAS;
+
+	/* movn rd, #(~(val >> 16) & 0xffff), lsl #16 */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)~(val >> 16),
+					 16,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_INVERSE);
+	*updptr++ = cpu_to_le32(insn);
+
+	/* movk rd, #(val & 0xffff) */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)val,
+					 0,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 static pgprot_t __init kernel_exec_prot(void)
 {
 	return rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;


  parent reply	other threads:[~2026-12-01  1:54 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
2026-12-01  1:51 ` Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 03/43] mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 04/43] mm: page_frag: Add _MIN/_MAX variants of PAGE_FRAG_CACHE_MAX_SIZE Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 08/43] cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 09/43] bpf: Do not embed page-aligned members in struct bpf_ringbuf Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 10/43] PM: hibernate: Do not size swap structures to exactly PAGE_SIZE Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 11/43] perf/core: Handle a boot-time PAGE_SIZE in format attrs and mlock Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 12/43] KVM: Check struct kvm_run against PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 13/43] crash: Check crash_notes size " Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 14/43] arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 15/43] arm64: pi: Pass the early pgtable allocation limit to map_range() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 17/43] arm64: Parameterise early pgtable size macros by page shift Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 19/43] arm64: pi: Select the page size at runtime during early boot Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 20/43] arm64: pi: Clear the FDT ptes on every remap in map_fdt() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 22/43] arm64: Align image sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 23/43] arm64: Map the trampoline rodata at its aligned offset Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 24/43] arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 25/43] arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:51 ` [RFC PATCH v2 27/43] arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 28/43] arm64: cpufeature: Add capabilities for the page size in use Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 29/43] arm64: Look up the page size at runtime in assembly code Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:52 ` [RFC PATCH v2 31/43] arm64: Support runtime folding in idmap_kpti_install_ng_mappings Eric Curtin
2026-12-01  1:52 ` Eric Curtin [this message]
2026-12-01  1:52 ` [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 35/43] mm/zsmalloc: Size static limits for the worst-case page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01  1:52 ` [RFC PATCH v2 37/43] vdso: Lay out the data pages in VDSO_PAGE_SIZE units Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 38/43] mm: Use fixed sizes for shmem str_dup and swap cache PFN bits Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 39/43] crash: Size the vmcoreinfo page array for the smallest page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 40/43] arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 42/43] tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page Eric Curtin
2026-12-01 13:15   ` Masami Hiramatsu (Google)
2026-12-01  1:52 ` [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection Eric Curtin
2026-12-02  2:34   ` sashiko-bot
2026-12-01 10:41 ` [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Lorenzo Stoakes (ARM)
2026-12-01 10:41   ` Lorenzo Stoakes (ARM)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201015240.1017259-33-ericcurtin17@gmail.com \
    --to=ericcurtin17@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=ryan.roberts@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.