]> git.ipfire.org Git - thirdparty/ipxe.git/commit
[riscv] Relocate to a safe physical address on startup
authorMichael Brown <mcb30@ipxe.org>
Mon, 12 May 2025 10:58:23 +0000 (11:58 +0100)
committerMichael Brown <mcb30@ipxe.org>
Mon, 12 May 2025 12:59:42 +0000 (13:59 +0100)
commit17fd67ce03179cb3ed0e16c309d2a7f78e29c919
treee464ae08e5632e9629e954fd065949ca1c4d74eb
parent3dfc88158c3b35c0a089b0b0ff49198cf16141df
[riscv] Relocate to a safe physical address on startup

On startup, we may be running from read-only memory.  We need to parse
the devicetree to obtain the system memory map, and identify a safe
location to which we can copy our own binary image along with a
stashed copy of the devicetree, and then transfer execution to this
new location.

Parsing the system memory map realistically requires running C code.
This in turn requires a small temporary stack, and some way to ensure
that symbol references are valid.

We first attempt to enable paging, to make the runtime virtual
addresses equal to the link-time virtual addresses.  If this fails,
then we attempt to apply the compressed relocation records.

Assuming that one of these has worked (i.e. that either the CPU
supports paging or that our image started execution in writable
memory), then we call fdtmem_relocate() to parse the system memory map
to find a suitable relocation target address.

After the copy we disable paging, jump to the relocated copy,
re-enable paging, and reapply relocation records (if needed).  At this
point, we have a full runtime environment, and can transfer control to
normal C code.

Provide this functionality as part of libprefix.S, since it is likely
to be shared by multiple prefixes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/arch/riscv/prefix/libprefix.S
src/arch/riscv/prefix/sbiprefix.S
src/arch/riscv/scripts/sbi.lds
src/core/fdtmem.c