mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 5/8] ARM64: asm: rewrite ENTRY_FUNCTION(_WITHSTACK) fully in assembly
Date: Mon, 24 Oct 2022 08:57:13 +0200	[thread overview]
Message-ID: <20221024065716.1215046-6-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20221024065716.1215046-1-a.fatoum@pengutronix.de>

Recent episode with pointer authentication showed again that for
platforms without __attribute__((naked)), we are better off writing
the early header in assembly. We still want to keep the board specific
entry points in C for ease of use, so we have ENTRY_FUNCTION_WITHSTACK
generate two symbols:

  - A 32-bit stack top value that's placed in .rodata

  - An entry point with the normal C code, including stack-using
    prologues

The new common assembly head code will access the stack pointer in a
position-independent manner and set it up, before continuing with the
C code. The barebox header is part of the common assembly head code
ensuring it's not moved around due to compiler code generation.

The common code will need access to board-specific entry point and stack
top. The former is readily available as the alias __pbl_board_entry.
The latter is a bit more complicated, as the symbol may not exist for
boards not using the common header in a multi-image build.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 Makefile                           |  3 +--
 arch/arm/cpu/Makefile              |  2 ++
 arch/arm/cpu/head_64.S             | 36 ++++++++++++++++++++++++++++++
 arch/arm/include/asm/barebox-arm.h | 28 +++++++----------------
 arch/arm/lib/pbl.lds.S             |  9 ++++++++
 5 files changed, 56 insertions(+), 22 deletions(-)
 create mode 100644 arch/arm/cpu/head_64.S

diff --git a/Makefile b/Makefile
index 81ef44122367..a4b9f3c021d7 100644
--- a/Makefile
+++ b/Makefile
@@ -660,8 +660,7 @@ KBUILD_CFLAGS  += $(call cc-option,-fno-stack-check)
 KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
 
 # We don't have the necessary infrastructure to benefit from ARMv8.3+ pointer
-# authentication. On older CPUs, they are interpreted as NOPs and blot the
-# code and break less portable code that expects a very specific code layout
+# authentication. On older CPUs, they are interpreted as NOPs bloating the code
 KBUILD_CFLAGS += $(call cc-option,-mbranch-protection=none)
 
 KBUILD_CFLAGS   += $(call cc-disable-warning, address-of-packed-member)
diff --git a/arch/arm/cpu/Makefile b/arch/arm/cpu/Makefile
index c0993c1abe5d..7674c1464c1f 100644
--- a/arch/arm/cpu/Makefile
+++ b/arch/arm/cpu/Makefile
@@ -13,6 +13,8 @@ AFLAGS_hyp.pbl.o :=-Wa,-march=armv7-a -Wa,-mcpu=all
 obj-y += start.o entry.o entry_ll$(S64).o
 KASAN_SANITIZE_start.o := n
 
+pbl-$(CONFIG_CPU_64) += head_64.o
+
 pbl-$(CONFIG_BOARD_ARM_GENERIC_DT) += board-dt-2nd.o
 pbl-$(CONFIG_BOARD_ARM_GENERIC_DT_AARCH64) += board-dt-2nd-aarch64.o
 
diff --git a/arch/arm/cpu/head_64.S b/arch/arm/cpu/head_64.S
new file mode 100644
index 000000000000..4ed4ffb05250
--- /dev/null
+++ b/arch/arm/cpu/head_64.S
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <linux/linkage.h>
+#include <asm/barebox-arm64.h>
+#include <asm/image.h>
+
+/* Linker will point these at board-specific symbols */
+.globl __pbl_board_stack_top
+.globl __pbl_board_entry
+
+.section .text_head_prologue_common, "x"
+ENTRY(__barebox_arm64_head)
+	nop
+	nop
+	nop
+	adr x9, __pbl_board_stack_top
+	ldr w9, [x9]
+	cbz x9, 1f
+	mov sp, x9
+	b 1f
+	.org 0x20
+	.asciz "barebox"
+	.word 0xffffffff
+	.word _barebox_image_size	/* image size to copy */
+	.rept 8
+	.word 0x55555555
+	.endr
+1:
+#ifdef CONFIG_PBL_BREAK
+	brk #17
+	nop
+#else
+	nop
+	nop
+#endif
+	b __pbl_board_entry
+END(__barebox_arm64_head)
diff --git a/arch/arm/include/asm/barebox-arm.h b/arch/arm/include/asm/barebox-arm.h
index 68a9610398cc..89b4a89755a6 100644
--- a/arch/arm/include/asm/barebox-arm.h
+++ b/arch/arm/include/asm/barebox-arm.h
@@ -136,24 +136,7 @@ static inline unsigned long arm_mem_barebox_image(unsigned long membase,
 	}
 }
 
-#ifdef CONFIG_CPU_64
-
-#define ____emit_entry_prologue(name, instr, ...) do { \
-	static __attribute__ ((unused,section(".text_head_prologue_" __stringify(name)))) \
-		const u32 __entry_prologue[] = {(instr), ##__VA_ARGS__}; \
-	__keep_symbolref(__entry_prologue); \
-} while(0)
-
-#define __emit_entry_prologue(name, instr1, instr2, instr3, instr4, instr5) \
-	____emit_entry_prologue(name, instr1, instr2, instr3, instr4, instr5)
-
-#define __ARM_SETUP_STACK(name, stack_top) \
-	__emit_entry_prologue(name, 0x14000002	/* b pc+0x8 */,		\
-			      stack_top		/* 32-bit literal */,	\
-			      0x18ffffe9	/* ldr w9, top */,	\
-			      0xb4000049	/* cbz x9, pc+0x8 */,	\
-			      0x9100013f	/* mov sp, x9 */)
-#else
+#ifndef CONFIG_CPU_64
 #define __ARM_SETUP_STACK(name, stack_top) if (stack_top) arm_setup_stack(stack_top)
 #endif
 
@@ -166,6 +149,9 @@ static inline unsigned long arm_mem_barebox_image(unsigned long membase,
  * code block will not be inlined and may spill to stack right away.
  */
 #ifdef CONFIG_CPU_64
+
+void __barebox_arm64_head(ulong x0, ulong x1, ulong x2);
+
 #define ENTRY_FUNCTION_WITHSTACK(name, stack_top, arg0, arg1, arg2)	\
 	void name(ulong r0, ulong r1, ulong r2);			\
 									\
@@ -174,8 +160,10 @@ static inline unsigned long arm_mem_barebox_image(unsigned long membase,
 	void __section(.text_head_entry_##name)	name			\
 				(ulong r0, ulong r1, ulong r2)		\
 		{							\
-			__barebox_arm_head();				\
-			__ARM_SETUP_STACK(name, stack_top);		\
+			static __section(.pbl_board_stack_top_##name)	\
+				const u32 __stack_top = (stack_top);	\
+			__keep_symbolref(__barebox_arm64_head);		\
+			__keep_symbolref(__stack_top);			\
 			__##name(r0, r1, r2);				\
 		}							\
 		static void noinline __##name				\
diff --git a/arch/arm/lib/pbl.lds.S b/arch/arm/lib/pbl.lds.S
index ae1babdcfd27..114ec7bc8195 100644
--- a/arch/arm/lib/pbl.lds.S
+++ b/arch/arm/lib/pbl.lds.S
@@ -44,6 +44,15 @@ SECTIONS
 	. = ALIGN(4);
 	.rodata : { *(.rodata*) }
 
+	. = ALIGN(4);
+	__pbl_board_stack_top = .;
+	.rodata.pbl_board_stack_top : {
+		*(.pbl_board_stack_top_*)
+		/* Dummy for when BootROM sets up usable stack */
+		LONG(0x00000000);
+	}
+	ASSERT(. - __pbl_board_stack_top <= 8, "Only One PBL per Image allowed")
+
 	.barebox_imd : { BAREBOX_IMD }
 
 	_etext = .;			/* End of text and rodata section */
-- 
2.30.2




  parent reply	other threads:[~2022-10-24  6:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-24  6:57 [PATCH 0/8] ARM64: rewrite ENTRY_FUNCTION_WITHSTACK " Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 1/8] ARM64: cpu: select HAVE_PBL_MULTI_IMAGES globally Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 2/8] ARM64: asm: define ENTRY_FUNCTION in terms of ENTRY_FUNCTION_WITHSTACK Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 3/8] pbl: have linker define __pbl_board_entry alias Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 4/8] asm-generic: memory_layout: define __keep_symbolref() Ahmad Fatoum
2022-10-24  6:57 ` Ahmad Fatoum [this message]
2022-10-24  8:55   ` [PATCH 5/8] ARM64: asm: rewrite ENTRY_FUNCTION(_WITHSTACK) fully in assembly Sascha Hauer
2022-10-24 10:02     ` Ahmad Fatoum
2022-10-24 12:11   ` [PATCH] fixup! " Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 6/8] ARM64: asm: drop __barebox_arm_head Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 7/8] ARM: asm: cleanup 32-bit entry points Ahmad Fatoum
2022-10-24  6:57 ` [PATCH 8/8] Documentation: devel: porting: bring it up-to-date Ahmad Fatoum

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=20221024065716.1215046-6-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox