[RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot

Eric Curtin ericcurtin17 at gmail.com
Mon Nov 30 17:52:04 PST 2026


From: Ryan Roberts <ryan.roberts at 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 at arm.com>
Signed-off-by: Eric Curtin <eric.curtin at 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;



More information about the linux-arm-kernel mailing list