Skip to content

Commit

Permalink
loader: add relocation logging and move magic
Browse files Browse the repository at this point in the history
Signed-off-by: Alwin Joshy <[email protected]>
  • Loading branch information
alwin-joshy committed Feb 4, 2025
1 parent 87138c5 commit c6c2dd7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
15 changes: 11 additions & 4 deletions loader/src/aarch64/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ fixup_image_base:
cmp x0, x1
beq image_ok

/* Log that we are being relocated */
bl log_relocation

/* Restore x0 and x1 */
ldr x0, =LINK_ADDRESS
adr x1, _start

/* Sanity check: We don't want to overwrite ourselves! We assume that
* everything between _start (src_start) and _bss_end (src_end) is important (i.e.
* something that might be run while relocating) but allow overlap for
Expand All @@ -52,11 +59,11 @@ fixup_image_base:
add x2, x2, #:lo12:_bss_end

/* The loader_data is directly after _bss_end, with the first
* value being the loader_data struct. The first field of this
* struct is the size of the loader_data region, so we add
* this to _bss_end to get the real end of the image
* value being the loader_data struct. The second field of
* this struct is the size of the loader_data region, so we
* add this to _bss_end to get the real end of the image
*/
ldr x3, [x2]
ldr x3, [x2, #+8]
add x2, x2, x3
sub x2, x2, x1

Expand Down
10 changes: 9 additions & 1 deletion loader/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ struct region {
};

struct loader_data {
uintptr_t size;
uintptr_t magic;
uintptr_t size;
uintptr_t flags;
uintptr_t kernel_entry;
uintptr_t ui_p_reg_start;
Expand Down Expand Up @@ -735,6 +735,14 @@ static inline void enable_mmu(void)
}
#endif

void log_relocation(uint64_t reloc_addr, uint64_t curr_addr) {
puts("LDR|INFO: loader is being relocated. Currently at: ");
puthex64(curr_addr);
puts(". Moving to: ");
puthex64(reloc_addr);
puts(".\n");
}

int main(void)
{
#if defined(BOARD_zcu102)
Expand Down
11 changes: 9 additions & 2 deletions loader/src/riscv/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ fixup_image_base:
la a1, _start
beq a0, a1, image_ok

/* Log that we are being relocated */
jal log_relocation

/* Restore a0 and a1 */
li a0, LINK_ADDRESS
la a1, _start

/* Sanity check: We don't want to overwrite ourselves! We assume that
* everything between _start (src_start) and _bss_end (src_end) is important (i.e.
* something that might be run while relocating) but allow overlap for
Expand All @@ -121,11 +128,11 @@ fixup_image_base:
la a2, _bss_end

/* The loader_data is directly after _bss_end, with the first
* value being the loader_data struct. The first field of this
* value being the loader_data struct. The second field of this
* struct is the size of the loader_data region, so we add
* this to _bss_end to get the real end of the image
*/
lw a3, (a2)
lw a3, 8(a2)
add a2, a2, a3
sub a2, a2, a1

Expand Down
4 changes: 2 additions & 2 deletions tool/microkit/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ struct LoaderRegion64 {

#[repr(C)]
struct LoaderHeader64 {
size: u64,
magic: u64,
size: u64,
flags: u64,
kernel_entry: u64,
ui_p_reg_start: u64,
Expand Down Expand Up @@ -298,8 +298,8 @@ impl<'a> Loader<'a> {
});

let header = LoaderHeader64 {
size,
magic,
size,
flags,
kernel_entry,
ui_p_reg_start,
Expand Down

0 comments on commit c6c2dd7

Please sign in to comment.