Skip to content

Commit

Permalink
LoongArch: Simplify the processing of jumping new kernel for KASLR
Browse files Browse the repository at this point in the history
Modified relocate_kernel() doesn't return new kernel's entry point but
the random_offset. In this way we share the start_kernel() processing
with the normal kernel, which avoids calling 'jr a0' directly and allows
some other operations (e.g, kasan_early_init) before start_kernel() when
KASLR (CONFIG_RANDOMIZE_BASE) is turned on.

Signed-off-by: Qing Zhang <zhangqing@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
  • Loading branch information
Qing Zhang authored and Huacai Chen committed Sep 6, 2023
1 parent fb6d5c1 commit 9fbcc07
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion arch/loongarch/include/asm/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern long __la_abs_end;
extern long __rela_dyn_begin;
extern long __rela_dyn_end;

extern void * __init relocate_kernel(void);
extern unsigned long __init relocate_kernel(void);

#endif

Expand Down
11 changes: 6 additions & 5 deletions arch/loongarch/kernel/head.S
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ SYM_CODE_START(kernel_entry) # kernel entry point
PTR_LI sp, (_THREAD_SIZE - PT_SIZE)
PTR_ADD sp, sp, tp
set_saved_sp sp, t0, t1
#endif

/* relocate_kernel() returns the new kernel entry point */
jr a0
ASM_BUG()
/* Jump to the new kernel: new_pc = current_pc + random_offset */
pcaddi t0, 0
add.d t0, t0, a0
jirl zero, t0, 0xc
#endif /* CONFIG_RANDOMIZE_BASE */

#endif
#endif /* CONFIG_RELOCATABLE */

bl start_kernel
ASM_BUG()
Expand Down
8 changes: 2 additions & 6 deletions arch/loongarch/kernel/relocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@ static inline void __init update_reloc_offset(unsigned long *addr, long random_o
*new_addr = (unsigned long)reloc_offset;
}

void * __init relocate_kernel(void)
unsigned long __init relocate_kernel(void)
{
unsigned long kernel_length;
unsigned long random_offset = 0;
void *location_new = _text; /* Default to original kernel start */
void *kernel_entry = start_kernel; /* Default to original kernel entry point */
char *cmdline = early_ioremap(fw_arg1, COMMAND_LINE_SIZE); /* Boot command line is passed in fw_arg1 */

strscpy(boot_command_line, cmdline, COMMAND_LINE_SIZE);
Expand Down Expand Up @@ -190,9 +189,6 @@ void * __init relocate_kernel(void)

reloc_offset += random_offset;

/* Return the new kernel's entry point */
kernel_entry = RELOCATED_KASLR(start_kernel);

/* The current thread is now within the relocated kernel */
__current_thread_info = RELOCATED_KASLR(__current_thread_info);

Expand All @@ -204,7 +200,7 @@ void * __init relocate_kernel(void)

relocate_absolute(random_offset);

return kernel_entry;
return random_offset;
}

/*
Expand Down

0 comments on commit 9fbcc07

Please sign in to comment.