From 52f3510caced2008d18ca1090a33cc03910ad6ef Mon Sep 17 00:00:00 2001 From: Kent McLeod Date: Tue, 1 Feb 2022 22:27:12 +1100 Subject: [PATCH] elfloader, RISC-V: Move the elfloader during boot The SBI implementation usually loads the elfloader where the kernel would prefer to also be loaded. Allow the elfloader to move itself to a higher address before loading the kernel and user images. Signed-off-by: Kent McLeod --- cmake-tool/helpers/application_settings.cmake | 3 --- elfloader-tool/src/arch-riscv/crt0.S | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmake-tool/helpers/application_settings.cmake b/cmake-tool/helpers/application_settings.cmake index eaac50ccc..4706537d4 100644 --- a/cmake-tool/helpers/application_settings.cmake +++ b/cmake-tool/helpers/application_settings.cmake @@ -49,9 +49,6 @@ function(ApplyData61ElfLoaderSettings kernel_platform kernel_sel4_arch) if(KernelPlatformZynqmp AND KernelSel4ArchAarch32) set(IMAGE_START_ADDR 0x8000000 CACHE INTERNAL "" FORCE) endif() - if(KernelPlatformSpike AND KernelSel4ArchRiscV32) - set(IMAGE_START_ADDR 0x80400000 CACHE INTERNAL "" FORCE) - endif() endfunction() function(ApplyCommonSimulationSettings kernel_sel4_arch) diff --git a/elfloader-tool/src/arch-riscv/crt0.S b/elfloader-tool/src/arch-riscv/crt0.S index 54c4c106b..4e1f42aba 100644 --- a/elfloader-tool/src/arch-riscv/crt0.S +++ b/elfloader-tool/src/arch-riscv/crt0.S @@ -54,10 +54,25 @@ _start: mv s0, a0 /* preserve a0 (hart id) in s0 */ mv s2, a1 /* preserve a1 (dtb) in s2 */ -#ifdef CONFIG_IMAGE_BINARY + /* Set a stack so that we can call into C functions */ + la sp, (elfloader_stack_alloc + BIT(12)) + /* + * Binary images may not be loaded in the correct location. + * Try and move ourselves so we're in the right place. + */ + jal fixup_image_base + /* fixup_image_base returns 0 if no need to move */ + beqz a0, 1f + + /* otherwise, restore args and jump to the start of the new elfloader */ + mv a2, a0 + mv a0, s0 + mv a1, s2 + jr a2 + /* Clear the BSS before we get to do anything more specific */ +1: jal clear_bss -#endif /* Check if the Heart State Management (HSM) extension exists, so it can be * used to switch harts if we are not running on hart CONFIG_FIRST_HART_ID.