All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] arm64: relocate: convert relocate_code to C
@ 2026-11-25  7:41 Ilias Apalodimas
  2026-11-25  7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
                   ` (2 more replies)
  0 siblings, 3 replies; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-25  7:41 UTC (permalink / raw)
  To: trini, richard.henderson; +Cc: casey.connolly, Ilias Apalodimas, u-boot

relocate_code() on arm64 is called from _main with sp and gd already
pointing at the relocated area and lr pointing to the relocated copy of
relocation_return. By that time pie_fixup has already run, if
POSITION_INDEPENDENT is enabled, the image is fixed up and plain C works
fine, like the rest of board_init_f(). So let's convert it to C which is
much easier to read and maintain.

While at it change the behaviour and order of cache maintenance.
Invalidate the I-cache after the D-cache clean instead of before
and add a 'dsb' between 'ic iallu' and the 'isb', which is the
architecturally required sequence.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 arch/arm/lib/relocate_64.S | 96 --------------------------------------
 arch/arm/lib/relocate_64.c | 82 ++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 96 deletions(-)
 delete mode 100644 arch/arm/lib/relocate_64.S
 create mode 100644 arch/arm/lib/relocate_64.c

diff --git a/arch/arm/lib/relocate_64.S b/arch/arm/lib/relocate_64.S
deleted file mode 100644
index 495a5f03ba76..000000000000
--- a/arch/arm/lib/relocate_64.S
+++ /dev/null
@@ -1,96 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * relocate - common relocation function for AArch64 U-Boot
- *
- * (C) Copyright 2013
- * Albert ARIBAUD <albert.u.boot@aribaud.net>
- * David Feng <fenghua@phytium.com.cn>
- */
-
-#include <asm-offsets.h>
-#include <config.h>
-#include <elf.h>
-#include <linux/linkage.h>
-#include <asm/macro.h>
-
-/*
- * void relocate_code(addr_moni)
- *
- * This function relocates the monitor code.
- * x0 holds the destination address.
- */
-ENTRY(relocate_code)
-	stp	x29, x30, [sp, #-32]!	/* create a stack frame */
-	mov	x29, sp
-	str	x0, [sp, #16]
-	/*
-	 * Copy u-boot from flash to RAM
-	 */
-	adrp	x1, __image_copy_start		/* x1 <- address bits [31:12] */
-	add	x1, x1, :lo12:__image_copy_start/* x1 <- address bits [11:00] */
-	subs	x9, x0, x1			/* x9 <- Run to copy offset */
-	b.eq	relocate_done			/* skip relocation */
-	/*
-	 * Don't ldr x1, __image_copy_start here, since if the code is already
-	 * running at an address other than it was linked to, that instruction
-	 * will load the relocated value of __image_copy_start. To
-	 * correctly apply relocations, we need to know the linked value.
-	 *
-	 * Linked &__image_copy_start, which we know was at
-	 * CONFIG_TEXT_BASE, which is stored in _TEXT_BASE, as a non-
-	 * relocated value, since it isn't a symbol reference.
-	 */
-	ldr	x1, _TEXT_BASE		/* x1 <- Linked &__image_copy_start */
-	subs	x9, x0, x1		/* x9 <- Link to copy offset */
-
-	adrp	x1, __image_copy_start		/* x1 <- address bits [31:12] */
-	add	x1, x1, :lo12:__image_copy_start/* x1 <- address bits [11:00] */
-	adrp	x2, __image_copy_end		/* x2 <- address bits [31:12] */
-	add	x2, x2, :lo12:__image_copy_end	/* x2 <- address bits [11:00] */
-copy_loop:
-	ldp	x10, x11, [x1], #16	/* copy from source address [x1] */
-	stp	x10, x11, [x0], #16	/* copy to   target address [x0] */
-	cmp	x1, x2			/* until source end address [x2] */
-	b.lo	copy_loop
-	str	x0, [sp, #24]
-
-	/*
-	 * Fix .rela.dyn relocations
-	 */
-	adrp	x2, __rel_dyn_start		/* x2 <- address bits [31:12] */
-	add	x2, x2, :lo12:__rel_dyn_start	/* x2 <- address bits [11:00] */
-	adrp	x3, __rel_dyn_end		/* x3 <- address bits [31:12] */
-	add	x3, x3, :lo12:__rel_dyn_end	/* x3 <- address bits [11:00] */
-fixloop:
-	ldp	x0, x1, [x2], #16	/* (x0,x1) <- (SRC location, fixup) */
-	ldr	x4, [x2], #8		/* x4 <- addend */
-	and	x1, x1, #0xffffffff
-	cmp	x1, #R_AARCH64_RELATIVE
-	bne	fixnext
-
-	/* relative fix: store addend plus offset at dest location */
-	add	x0, x0, x9
-	add	x4, x4, x9
-	str	x4, [x0]
-fixnext:
-	cmp	x2, x3
-	b.lo	fixloop
-
-relocate_done:
-	switch_el x1, 3f, 2f, 1f
-	bl	hang
-3:	mrs	x0, sctlr_el3
-	b	0f
-2:	mrs	x0, sctlr_el2
-	b	0f
-1:	mrs	x0, sctlr_el1
-0:	tbz	w0, #2, 5f	/* skip flushing cache if disabled */
-	tbz	w0, #12, 4f	/* skip invalidating i-cache if disabled */
-	ic	iallu		/* i-cache invalidate all */
-	isb	sy
-4:	ldp	x0, x1, [sp, #16]
-	bl	__asm_flush_dcache_range
-	bl     __asm_flush_l3_dcache
-5:	ldp	x29, x30, [sp],#32
-	ret
-ENDPROC(relocate_code)
diff --git a/arch/arm/lib/relocate_64.c b/arch/arm/lib/relocate_64.c
new file mode 100644
index 000000000000..6f7a50f2264e
--- /dev/null
+++ b/arch/arm/lib/relocate_64.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * relocate - common relocation function for AArch64
+ *
+ * Adapted from the original asm from:
+ * Albert ARIBAUD <albert.u.boot@aribaud.net>
+ * David Feng <fenghua@phytium.com.cn>
+ *
+ * Copyright (c) 2026, Ilias Apalodimas <ilias.apalodimas@linaro.org>
+ */
+
+#include <elf.h>
+#include <init.h>
+#include <linux/compiler.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <asm/sections.h>
+#include <asm/system.h>
+
+/**
+ * relocate_code() - copy U-Boot to its final location and fix it up
+ *
+ * @dest: destination address, gd->relocaddr
+ *
+ * This is entered from _main in crt0_64.S with sp and gd already
+ * pointing to the relocated area and with lr pointing to the relocated
+ * copy of relocation_return, so returning from here continues execution
+ * in the new image.
+ *
+ * Constraints for this function, since it runs from the old image and
+ * before BSS is usable:
+ * - It must not touch BSS. On arm64 .bss overlays .rela.dyn
+ * - It must not rely on anything that needs relocating in the destination.
+ *   Symbol addresses are generated PC-relative (adrp/add, -fno-pic), so they
+ *   resolve to the runtime location of the currently executing image, which
+ *   is already correct, either because we run at CONFIG_TEXT_BASE or because
+ *   pie_fixup has fixed up the running image in place.
+ * - It must not be instrumented.
+ */
+void notrace relocate_code(ulong dest)
+{
+	u64 src = (u64)__image_copy_start;
+	u64 size = (u64)__image_copy_end - src;
+	const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
+	const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
+	u64 sctlr;
+	u64 offset;
+
+	if (src == dest)
+		return;
+	memcpy((void *)dest, (void *)src, size);
+	/*
+	 * r_offset and r_addend are link-time values, so the offset to apply
+	 * is link-to-copy
+	 */
+	offset = dest - CONFIG_TEXT_BASE;
+
+	/*
+	 * We have a makefile check that aborts if reloc entries are not
+	 * R_AARCH64_RELATIVE.
+	 */
+	for (; rela < rela_end; rela++) {
+		if (ELF64_R_TYPE(rela->r_info) != R_AARCH64_RELATIVE)
+			continue;
+		*(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
+	}
+
+	sctlr = get_sctlr();
+	if (sctlr & CR_C) {
+		__asm_flush_dcache_range(dest, dest + size);
+		__asm_flush_l3_dcache();
+	} else {
+		/* Non-cacheable stores need to complete before we fetch new code */
+		asm volatile("dsb sy" : : : "memory");
+	}
+
+	if (sctlr & CR_I)
+		asm volatile("ic iallu\n\tdsb nsh\n\tisb" : : : "memory");
+
+	/* Always discard anything already fetched/prefetched */
+	asm volatile("isb" : : : "memory");
+}
--
2.53.0


^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2026-12-04 16:14 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  7:41 [PATCH 1/2] arm64: relocate: convert relocate_code to C Ilias Apalodimas
2026-11-25  7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
2026-11-25 17:20   ` Quentin Schulz
2026-11-26 12:48     ` Peter Robinson
2026-11-26 15:14       ` Ilias Apalodimas
2026-11-27 18:46         ` Tom Rini
2026-12-03 22:27           ` Ilias Apalodimas
2026-12-03 22:42             ` Tom Rini
2026-12-03 22:53               ` Ilias Apalodimas
2026-12-03 23:02                 ` Tom Rini
2026-12-04  6:44                   ` Ilias Apalodimas
2026-12-04 15:12                     ` Tom Rini
2026-12-04 15:50                       ` Ilias Apalodimas
2026-12-04 16:13                         ` Ilias Apalodimas
2026-11-26 16:35   ` Richard Henderson
2026-11-26 16:58     ` Ilias Apalodimas
2026-11-26 20:51       ` Ilias Apalodimas
2026-12-03 22:14         ` Richard Henderson
2026-11-25 17:07 ` [PATCH 1/2] arm64: relocate: convert relocate_code to C Quentin Schulz
2026-11-25 17:08   ` Casey Connolly
2026-11-25 17:11   ` Ilias Apalodimas
2026-11-27 18:35     ` Tom Rini
2026-11-26 16:11 ` Richard Henderson
2026-11-26 16:18   ` Ilias Apalodimas
2026-11-26 17:03     ` Ilias Apalodimas

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.