Skip to content

riscv: support running U-Boot proper at any address

Romain Caritey u-boot 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4riscv: start.S: use lla instead of laarch/riscv/cpu/start.S+20 -200 replies
2/4riscv: check that only R_RISCV_RELATIVE relocations are generatedMakefile arch/riscv/config.mk+15 -00 replies
3/4riscv: start.S: move .rela.dyn processing to a subroutinearch/riscv/cpu/start.S+31 -460 replies
4/4riscv: add support for POSITION_INDEPENDENTarch/riscv/Kconfig arch/riscv/cpu/start.S common/board_f.c+90 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/4] 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