Skip to contentSkip to conversation

riscv: support running U-Boot proper at any address

PATCH In review Romain Caritey 2 participants 6 messages Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover riscv: support running U-Boot proper at any address

U-Boot proper on RISC-V must currently be loaded and run at CONFIG_TEXT_BASE. This series adds a POSITION_INDEPENDENT option, as on ARM, so that it can be run from another address, for instance when the previous stage does not load it at a fixed place.

This is needed for the Microchip PIC64 SoC [1], which has two clusters running U-Boot from different addresses. With this series, a single U-Boot build can run on both clusters, instead of one build per cluster with a different CONFIG_TEXT_BASE.

Patch 1 replaces la by lla in start.S. With -fpic, la loads the address from the GOT, which only holds the right value once relocations have been applied.

Patch 2 adds a checkriscvreloc build check, modelled after checkarmreloc, to make sure only R_RISCV_RELATIVE relocations are generated. This is the case for every configuration with -static -pie.

Patch 3 moves the .rela.dyn processing to a subroutine and drops the unused symbol-based branch, so that it can be called twice.

Patch 4 adds POSITION_INDEPENDENT, INIT_SP_RELATIVE and
SYS_INIT_SP_BSS_OFFSET: start.S fixes the relocations for the run-time address before any C code runs, including board_init_f_alloc_reserve() and harts_early_init(), and computes the early stack pointer from __bss_start. The first hart to enter _start applies the relocations, the other harts wait for it to be done.

Note on Kconfig: POSITION_INDEPENDENT, INIT_SP_RELATIVE and SYS_INIT_SP_BSS_OFFSET are now defined in both arch/arm/Kconfig and arch/riscv/Kconfig, with the same meaning but different dependencies. I kept them per architecture to keep this series contained to RISC-V. They could be unified in a shared place (for instance a common Kconfig with per-architecture dependencies) as a follow-up, if preferred.

Tested on QEMU virt with OpenSBI fw_jump (S-mode) and in M-mode, with 1, 4 and 8 harts, using qemu-riscv64_smode_defconfig and qemu-riscv64_defconfig + POSITION_INDEPENDENT:
- loaded at CONFIG_TEXT_BASE (no fixup needed),
- linked at 0x80400000 and loaded at 0x80200000,
- linked at 0x0 and loaded at 0x80200000,
- M-mode linked at 0x80100000 and loaded at 0x80000000.
All reach the prompt; bdinfo, env and bootefi hello behave as with a non-PIE build. A harts_early_init() calling through a function pointer in .data, which crashed with the fixup placed after it, now boots. Builds without POSITION_INDEPENDENT are unchanged.

Also tested on hardware, on a Microchip PIC64 HPSC [1] board in unified mode with 8 harts.

Romain Caritey:
riscv: start.S: use lla instead of la
riscv: check that only R_RISCV_RELATIVE relocations are generated
riscv: start.S: move .rela.dyn processing to a subroutine
riscv: add support for POSITION_INDEPENDENT

 Makefile               |  10 +++
 arch/riscv/Kconfig     |  35 +++++++++
 arch/riscv/config.mk   |   5 ++
 arch/riscv/cpu/start.S | 163 +++++++++++++++++++++++++----------------
 common/board_f.c       |   3 +
 5 files changed, 152 insertions(+), 64 deletions(-)

[1] https://www.microchip.com/en-us/products/microprocessors/64-bit-mpus/

base-commit: 6201222134cd704b5717211fc917e2ca6263577a

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/4 riscv: start.S: use lla instead of la

With -fpic, which is always used for RISC-V, the la pseudo-instruction is expanded into a load from the GOT. The GOT entries hold link-time addresses until the .rela.dyn relocations have been applied, so la only returns the right address when U-Boot runs at CONFIG_TEXT_BASE.

The symbols referenced in start.S are all local to U-Boot, and the code needs their run-time address. Use lla, which always generates a PC-relative auipc/addi pair and does not access memory.

No functional change when U-Boot runs at CONFIG_TEXT_BASE, which is currently the only supported case. This is a prerequisite for running U-Boot at an address different from the one it was linked at.

---
 arch/riscv/cpu/start.S | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
arch/riscv/cpu/start.S +20 -20
index 7bafdfd390a..72032b77ef9 100644
@@ -61,7 +61,7 @@ _start:
 	 * Set the trap handler. This must happen after initializing gp because
 	 * the handler may use it.
 	 */
-	la	t0, trap_entry
+	lla	t0, trap_entry
 	csrw	MODE_PREFIX(tvec), t0
 
 	/*
@@ -149,7 +149,7 @@ call_harts_early_init:
 	 * Pick hart to initialize global data and run U-Boot. The other harts
 	 * wait for initialization to complete.
 	 */
-	la	t0, hart_lottery
+	lla	t0, hart_lottery
 	li	t1, 1
 	amoswap.w s2, t1, 0(t0)
 	bnez	s2, wait_for_gd_init
@@ -176,7 +176,7 @@ call_harts_early_init:
 
 #if !CONFIG_IS_ENABLED(XIP)
 #ifdef CONFIG_AVAILABLE_HARTS
-	la	t0, available_harts_lock
+	lla	t0, available_harts_lock
 	amoswap.w.rl zero, zero, 0(t0)
 #endif
 
@@ -188,7 +188,7 @@ wait_for_gd_init:
 	 */
 	mv	gp, s0
 #ifdef CONFIG_AVAILABLE_HARTS
-	la	t0, available_harts_lock
+	lla	t0, available_harts_lock
 	li	t1, 1
 1:	amoswap.w.aq t1, t1, 0(t0)
 	bnez	t1, 1b
@@ -215,13 +215,13 @@ wait_for_gd_init:
 #endif
 
 	mv	a0, zero		/* a0 <-- boot_flags = 0 */
-	la	t5, board_init_f
+	lla	t5, board_init_f
 	jalr	t5			/* jump to board_init_f() */
 
 #ifdef CONFIG_XPL_BUILD
 spl_clear_bss:
-	la	t0, __bss_start
-	la	t1, __bss_end
+	lla	t0, __bss_start
+	lla	t1, __bss_end
 	beq	t0, t1, spl_stack_gd_setup
 
 spl_clear_bss_loop:
@@ -248,7 +248,7 @@ spl_stack_gd_setup:
 #if CONFIG_IS_ENABLED(SMP)
 	/* set new stack and global data pointer on secondary harts */
 spl_secondary_hart_stack_gd_setup:
-	la	a0, secondary_hart_relocate
+	lla	a0, secondary_hart_relocate
 	mv	a1, s0
 	mv	a2, s0
 	mv	a3, zero
@@ -257,7 +257,7 @@ spl_secondary_hart_stack_gd_setup:
 	/* hang if relocation of secondary harts has failed */
 	beqz	a0, 1f
 	mv	a1, a0
-	la	a0, secondary_harts_relocation_error
+	lla	a0, secondary_harts_relocation_error
 	jal	printf
 	jal	hang
 #endif
@@ -297,12 +297,12 @@ stack_setup:
 	mv	sp, s2
 #endif
 
-	la	t0, _start
+	lla	t0, _start
 	sub	t6, s4, t0		/* t6 <- relocation offset */
 	beq	t0, s4, clear_bss	/* skip relocation */
 
 	mv	t1, s4			/* t1 <- scratch for copy_loop */
-	la	t2, __bss_start		/* t2 <- source end address */
+	lla	t2, __bss_start		/* t2 <- source end address */
 
 copy_loop:
 	LREG	t5, 0(t0)
@@ -315,8 +315,8 @@ copy_loop:
  * Update dynamic relocations after board_init_f
  */
 fix_rela_dyn:
-	la	t1, __rel_dyn_start
-	la	t2, __rel_dyn_end
+	lla	t1, __rel_dyn_start
+	lla	t2, __rel_dyn_end
 	beq	t1, t2, clear_bss
 	add	t1, t1, t6		/* t1 <- rela_dyn_start in RAM */
 	add	t2, t2, t6		/* t2 <- rela_dyn_end in RAM */
@@ -333,7 +333,7 @@ fix_rela_dyn:
 	j	10f
 
 8:
-	la	t4, __dyn_sym_start
+	lla	t4, __dyn_sym_start
 	add	t4, t4, t6
 
 9:
@@ -359,14 +359,14 @@ fix_rela_dyn:
 /*
  * trap update
 */
-	la	t0, trap_entry
+	lla	t0, trap_entry
 	add	t0, t0, t6
 	csrw	MODE_PREFIX(tvec), t0
 
 clear_bss:
-	la	t0, __bss_start		/* t0 <- rel __bss_start in FLASH */
+	lla	t0, __bss_start		/* t0 <- rel __bss_start in FLASH */
 	add	t0, t0, t6		/* t0 <- rel __bss_start in RAM */
-	la	t1, __bss_end		/* t1 <- rel __bss_end in FLASH */
+	lla	t1, __bss_end		/* t1 <- rel __bss_end in FLASH */
 	add	t1, t1, t6		/* t1 <- rel __bss_end in RAM */
 	beq	t0, t1, relocate_secondary_harts
 
@@ -378,7 +378,7 @@ clbss_l:
 relocate_secondary_harts:
 #if CONFIG_IS_ENABLED(SMP)
 	/* send relocation IPI */
-	la	t0, secondary_hart_relocate
+	lla	t0, secondary_hart_relocate
 	add	a0, t0, t6
 
 	/* store relocation offset */
@@ -392,7 +392,7 @@ relocate_secondary_harts:
 	/* hang if relocation of secondary harts has failed */
 	beqz	a0, 1f
 	mv	a1, a0
-	la	a0, secondary_harts_relocation_error
+	lla	a0, secondary_harts_relocation_error
 	jal	printf
 	jal	hang
 
@@ -407,7 +407,7 @@ relocate_secondary_harts:
 call_board_init_r:
 	jal	invalidate_icache_all
 	jal	flush_dcache_all
-	la	t0, board_init_r        /* offset of board_init_r() */
+	lla	t0, board_init_r        /* offset of board_init_r() */
 	add	t4, t0, t6		/* real address of board_init_r() */
 /*
  * setup parameters for board_init_r
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/4 riscv: check that only R_RISCV_RELATIVE relocations are generated

In reply to Romain Caritey

U-Boot proper is linked with -static -pie on RISC-V, so the linker resolves every symbol and only emits R_RISCV_RELATIVE dynamic relocations. Any other relocation type in .rela.dyn would mean that the image can not be relocated correctly.

Add a checkriscvreloc target, modelled after checkarmreloc, and run it for U-Boot proper so that such a binary fails to build instead of failing at run-time.

---
 Makefile             | 10 ++++++++++
 arch/riscv/config.mk |  5 +++++
 2 files changed, 15 insertions(+)
Makefile +10 -0
index a9cc6ec2bd3..09d6bf7fe11 100644
@@ -2516,6 +2516,16 @@ checkarmreloc: u-boot
 		false; \
 	fi
 
+# RISC-V relocations should all be R_RISCV_RELATIVE, as
+# apply_relocation in arch/riscv/cpu/start.S only handles that type.
+checkriscvreloc: u-boot
+	@RELOC="`$(READELF) -r -W $< | cut -d ' ' -f 4 | \
+		grep R_RISCV | sort -u`"; \
+	if test "$$RELOC" != "R_RISCV_RELATIVE"; then \
+		echo "$< contains unexpected relocations: $$RELOC"; \
+		false; \
+	fi
+
 tools/version.h: include/version.h FORCE
 	$(Q)mkdir -p $(dir $@)
 	$(call if_changed,copy)
arch/riscv/config.mk +5 -0
index eddd6a3b9a2..dc7bd603add 100644
@@ -41,3 +41,8 @@ LDFLAGS_u-boot		+= --gc-sections -static -pie
 
 EFI_CRT0		:= crt0_riscv_efi.o
 EFI_RELOC		:= reloc_riscv_efi.o
+
+ifneq ($(CONFIG_XPL_BUILD),y)
+# Check that only R_RISCV_RELATIVE relocations are generated.
+INPUTS-y += checkriscvreloc
+endif
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/4 riscv: start.S: move .rela.dyn processing to a subroutine

In reply to Romain Caritey

U-Boot proper is linked with -static -pie, so the linker only emits R_RISCV_RELATIVE dynamic relocations, which checkriscvreloc now enforces. The symbol-based branch of the fixup loop, handling R_RISCV_32/R_RISCV_64 through .dynsym, is therefore dead code. It is also incorrect for undefined weak symbols, which would be turned into the relocation offset instead of staying NULL.

Drop it, as done on ARM where only R_ARM_RELATIVE/R_AARCH64_RELATIVE are handled, and move the remaining loop to an apply_relocation subroutine taking the offset to apply in a3. This allows the same code to be reused to relocate U-Boot before board_init_f().

The relocation table is now read from the running image instead of the copy, which holds the same data, and the offset is computed from CONFIG_TEXT_BASE, which is equal to the run-time address of _start for all current users. No functional change.

---
 arch/riscv/cpu/start.S | 77 +++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 46 deletions(-)
arch/riscv/cpu/start.S +31 -46
index 72032b77ef9..7cf6384b647 100644
@@ -20,16 +20,10 @@
 #define LREG			lw
 #define SREG			sw
 #define REGBYTES		4
-#define RELOC_TYPE		R_RISCV_32
-#define SYM_INDEX		0x8
-#define SYM_SIZE		0x10
 #else
 #define LREG			ld
 #define SREG			sd
 #define REGBYTES		8
-#define RELOC_TYPE		R_RISCV_64
-#define SYM_INDEX		0x20
-#define SYM_SIZE		0x18
 #endif
 
 .section .data
@@ -315,46 +309,9 @@ copy_loop:
  * Update dynamic relocations after board_init_f
  */
 fix_rela_dyn:
-	lla	t1, __rel_dyn_start
-	lla	t2, __rel_dyn_end
-	beq	t1, t2, clear_bss
-	add	t1, t1, t6		/* t1 <- rela_dyn_start in RAM */
-	add	t2, t2, t6		/* t2 <- rela_dyn_end in RAM */
-
-6:
-	LREG	t5, REGBYTES(t1)	/* t5 <-- relocation info:type */
-	li	t3, R_RISCV_RELATIVE	/* reloc type R_RISCV_RELATIVE */
-	bne	t5, t3, 8f		/* skip non-RISCV_RELOC entries */
-	LREG	t3, 0(t1)
-	LREG	t5, (REGBYTES * 2)(t1)	/* t5 <-- addend */
-	add	t5, t5, t6		/* t5 <-- location to fix up in RAM */
-	add	t3, t3, t6		/* t3 <-- location to fix up in RAM */
-	SREG	t5, 0(t3)
-	j	10f
-
-8:
-	lla	t4, __dyn_sym_start
-	add	t4, t4, t6
-
-9:
-	srli	t0, t5, SYM_INDEX	/* t0 <--- sym table index */
-	andi	t5, t5, 0xFF		/* t5 <--- relocation type */
-	li	t3, RELOC_TYPE
-	bne	t5, t3, 10f		/* skip non-addned entries */
-
-	LREG	t3, 0(t1)
-	li	t5, SYM_SIZE
-	mul	t0, t0, t5
-	add	s5, t4, t0
-	LREG	t0, (REGBYTES * 2)(t1)	/* t0 <-- addend */
-	LREG	t5, REGBYTES(s5)
-	add	t5, t5, t0
-	add	t5, t5, t6		/* t5 <-- location to fix up in RAM */
-	add	t3, t3, t6		/* t3 <-- location to fix up in RAM */
-	SREG	t5, 0(t3)
-10:
-	addi	t1, t1, (REGBYTES * 3)
-	blt	t1, t2, 6b
+	li	a3, CONFIG_TEXT_BASE	/* a3 <- linked address of _start */
+	sub	a3, s4, a3		/* a3 <- link-vs-destination offset */
+	jal	apply_relocation
 
 /*
  * trap update
@@ -420,6 +377,34 @@ call_board_init_r:
  * jump to it ...
  */
 	jr	t4			/* jump to board_init_r() */
+
+/*
+ * Apply the .rela.dyn relocations of the running image. a3 is added
+ * to the link-time location and value of each entry. As on ARM, only
+ * R_RISCV_RELATIVE relocations are handled, other entries are skipped;
+ * checkriscvreloc fails the build if any other type is generated.
+ *
+ * a3 = the offset to add (how far the code moved from its link address)
+ */
+apply_relocation:
+	lla	a0, __rel_dyn_start	/* a0 <- first relocation entry */
+	lla	a1, __rel_dyn_end	/* a1 <- one past the last entry */
+	beq	a0, a1, 3f
+
+1:
+	LREG	t5, REGBYTES(a0)	/* t5 <-- relocation info:type */
+	li	t3, R_RISCV_RELATIVE	/* reloc type R_RISCV_RELATIVE */
+	bne	t5, t3, 2f		/* skip non-RISCV_RELOC entries */
+	LREG	t3, 0(a0)
+	LREG	t5, (REGBYTES * 2)(a0)	/* t5 <-- addend */
+	add	t5, t5, a3		/* t5 <-- relocated value */
+	add	t3, t3, a3		/* t3 <-- location to fix up */
+	SREG	t5, 0(t3)
+2:
+	addi	a0, a0, (REGBYTES * 3)
+	blt	a0, a1, 1b
+3:
+	ret
 #endif /* !defined(CONFIG_XPL_BUILD) */
 
 #if CONFIG_IS_ENABLED(SMP)
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/4 riscv: add support for POSITION_INDEPENDENT

In reply to Romain Caritey

U-Boot proper on RISC-V has to be loaded at CONFIG_TEXT_BASE. The previous stage, OpenSBI for instance, does not always load it there, and the load address can depend on the platform or on the memory layout.

Add a POSITION_INDEPENDENT option, as on ARM. When enabled:

- start.S applies the .rela.dyn relocations for the run-time address using apply_relocation, before any C code runs: compiled code loads addresses from the GOT or from initialized data, so even board_init_f_alloc_reserve() and harts_early_init(), which may be overridden by a CPU or board, must only run after the fixup. The first hart to enter _start applies the relocations, the other harts wait for it to be done. The fixup is skipped when U-Boot runs at CONFIG_TEXT_BASE.

- INIT_SP_RELATIVE, selected by default, sets the early stack pointer to __bss_start + SYS_INIT_SP_BSS_OFFSET instead of using a fixed address.

- gd->reloc_off is computed from the run-time address of _start, as board_init_f() adds it to run-time addresses to jump to the relocated board_init_r().

The early stack, malloc area and global data are located above .bss, so the memory from the load address up to __bss_start + SYS_INIT_SP_BSS_OFFSET must be usable.

POSITION_INDEPENDENT, INIT_SP_RELATIVE and SYS_INIT_SP_BSS_OFFSET duplicate the options of the same name in arch/arm/Kconfig, with the same meaning but different dependencies. They are kept per architecture for now to keep this change contained to RISC-V; they could be unified in a shared Kconfig file with per-architecture dependencies as a follow-up.

---
 arch/riscv/Kconfig     | 35 +++++++++++++++++++++++++++
 arch/riscv/cpu/start.S | 54 ++++++++++++++++++++++++++++++++++++++++--
 common/board_f.c       |  3 +++
 3 files changed, 90 insertions(+), 2 deletions(-)
arch/riscv/Kconfig +35 -0
index 386241efd4a..1903a19c527 100644
@@ -501,6 +501,41 @@ config SPL_XIP
 	  rely on lock variables (for example hart_lottery and available_harts_lock),
 	  this affects only SPL, other stages should proceed as non-XIP.
 
+config POSITION_INDEPENDENT
+	bool "Generate position-independent pre-relocation code"
+	depends on !XIP
+	help
+	  U-Boot expects to be linked to a specific hard-coded address, and to
+	  be loaded to and run from that address. This option lifts that
+	  restriction, thus allowing the code to be loaded to and executed from
+	  almost any 4K aligned address. This logic relies on the relocation
+	  information that is embedded in the binary to support U-Boot
+	  relocating itself to the top-of-RAM later during execution.
+
+config INIT_SP_RELATIVE
+	bool "Specify the early stack pointer relative to the .bss section"
+	default y if POSITION_INDEPENDENT
+	help
+	  U-Boot typically uses a hard-coded value for the stack pointer
+	  before relocation. Enable this option to instead calculate the
+	  initial SP at run-time. This is useful to avoid hard-coding addresses
+	  into U-Boot, so that it can be loaded and executed at arbitrary
+	  addresses and thus avoid using arbitrary addresses at runtime.
+
+	  If this option is enabled, the early stack pointer is set to
+	  &_bss_start with a offset value added. The offset is specified by
+	  SYS_INIT_SP_BSS_OFFSET.
+
+config SYS_INIT_SP_BSS_OFFSET
+	hex "Early stack offset from the .bss base address"
+	depends on INIT_SP_RELATIVE
+	default 0x80000
+	help
+	  This option's value is the offset added to &_bss_start in order to
+	  calculate the stack pointer. This offset should be large enough so
+	  that the early malloc region, global data (gd), and early stack usage
+	  do not overlap any appended DTB.
+
 config AVAILABLE_HARTS
 	bool "Send IPI by available harts"
 	depends on !XIP
arch/riscv/cpu/start.S +52 -2

78 lines not on this page · raw message

common/board_f.c +3 -0
index 2f26b4644ca..4f928a9f33d 100644
@@ -763,6 +763,9 @@ static int setup_reloc(void)
 		gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
 #elif defined(CONFIG_MICROBLAZE)
 		gd->reloc_off = gd->relocaddr - (u32)_start;
+#elif defined(CONFIG_RISCV) && defined(CONFIG_POSITION_INDEPENDENT)
+		/* U-Boot may not run at CONFIG_TEXT_BASE, use where it runs */
+		gd->reloc_off = gd->relocaddr - (ulong)_start;
 #elif defined(CONFIG_M68K)
 		/*
 		 * On all ColdFire arch cpu, monitor code starts always
Signature · 1 line
2.47.3
#5 lore raw
In reply to Romain Caritey
Quoted 28 lines · Romain Caritey

U-Boot proper on RISC-V must currently be loaded and run at CONFIG_TEXT_BASE. This series adds a POSITION_INDEPENDENT option, as on ARM, so that it can be run from another address, for instance when the previous stage does not load it at a fixed place.

This is needed for the Microchip PIC64 SoC [1], which has two clusters running U-Boot from different addresses. With this series, a single U-Boot build can run on both clusters, instead of one build per cluster with a different CONFIG_TEXT_BASE.

Patch 1 replaces la by lla in start.S. With -fpic, la loads the address from the GOT, which only holds the right value once relocations have been applied.

Patch 2 adds a checkriscvreloc build check, modelled after checkarmreloc, to make sure only R_RISCV_RELATIVE relocations are generated. This is the case for every configuration with -static -pie.

Patch 3 moves the .rela.dyn processing to a subroutine and drops the unused symbol-based branch, so that it can be called twice.

Patch 4 adds POSITION_INDEPENDENT, INIT_SP_RELATIVE and
SYS_INIT_SP_BSS_OFFSET: start.S fixes the relocations for the run-time address before any C code runs, including board_init_f_alloc_reserve() and harts_early_init(), and computes the early stack pointer from __bss_start. The first hart to enter _start applies the relocations, the other harts wait for it to be done.

Quoted 6 lines

Note on Kconfig: POSITION_INDEPENDENT, INIT_SP_RELATIVE and SYS_INIT_SP_BSS_OFFSET are now defined in both arch/arm/Kconfig and arch/riscv/Kconfig, with the same meaning but different dependencies. I kept them per architecture to keep this series contained to RISC-V. They could be unified in a shared place (for instance a common Kconfig with per-architecture dependencies) as a follow-up, if preferred.

This is a good thing to see. Yes, in a follow-up please move the options to arch/Kconfig (and a short menu for them, similar to "Skipping low level initialization functions") with correct per-arch dependencies. Thanks!

Signature · 1 line
Tom