1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Kernel relocation at boot time
5 * Copyright (C) 2023 Loongson Technology Corporation Limited
9 #include <linux/kernel.h>
10 #include <linux/printk.h>
11 #include <linux/panic_notifier.h>
12 #include <linux/start_kernel.h>
13 #include <asm/bootinfo.h>
14 #include <asm/early_ioremap.h>
16 #include <asm/sections.h>
17 #include <asm/setup.h>
19 #define RELOCATED(x) ((void *)((long)x + reloc_offset))
20 #define RELOCATED_KASLR(x) ((void *)((long)x + random_offset))
22 static unsigned long reloc_offset
;
24 static inline void __init
relocate_relative(void)
26 Elf64_Rela
*rela
, *rela_end
;
27 rela
= (Elf64_Rela
*)&__rela_dyn_begin
;
28 rela_end
= (Elf64_Rela
*)&__rela_dyn_end
;
30 for ( ; rela
< rela_end
; rela
++) {
31 Elf64_Addr addr
= rela
->r_offset
;
32 Elf64_Addr relocated_addr
= rela
->r_addend
;
34 if (rela
->r_info
!= R_LARCH_RELATIVE
)
37 if (relocated_addr
>= VMLINUX_LOAD_ADDRESS
)
38 relocated_addr
= (Elf64_Addr
)RELOCATED(relocated_addr
);
40 *(Elf64_Addr
*)RELOCATED(addr
) = relocated_addr
;
44 static inline void __init
relocate_absolute(long random_offset
)
47 struct rela_la_abs
*p
;
49 begin
= RELOCATED_KASLR(&__la_abs_begin
);
50 end
= RELOCATED_KASLR(&__la_abs_end
);
52 for (p
= begin
; (void *)p
< end
; p
++) {
54 uint32_t lu12iw
, ori
, lu32id
, lu52id
;
55 union loongarch_instruction
*insn
= (void *)p
- p
->offset
;
57 lu12iw
= (v
>> 12) & 0xfffff;
59 lu32id
= (v
>> 32) & 0xfffff;
62 insn
[0].reg1i20_format
.immediate
= lu12iw
;
63 insn
[1].reg2i12_format
.immediate
= ori
;
64 insn
[2].reg1i20_format
.immediate
= lu32id
;
65 insn
[3].reg2i12_format
.immediate
= lu52id
;
69 #ifdef CONFIG_RANDOMIZE_BASE
70 static inline __init
unsigned long rotate_xor(unsigned long hash
,
71 const void *area
, size_t size
)
74 const typeof(hash
) *ptr
= PTR_ALIGN(area
, sizeof(hash
));
76 diff
= (void *)ptr
- area
;
77 if (size
< diff
+ sizeof(hash
))
80 size
= ALIGN_DOWN(size
- diff
, sizeof(hash
));
82 for (i
= 0; i
< size
/ sizeof(hash
); i
++) {
83 /* Rotate by odd number of bits and XOR. */
84 hash
= (hash
<< ((sizeof(hash
) * 8) - 7)) | (hash
>> 7);
91 static inline __init
unsigned long get_random_boot(void)
93 unsigned long hash
= 0;
94 unsigned long entropy
= random_get_entropy();
96 /* Attempt to create a simple but unpredictable starting entropy. */
97 hash
= rotate_xor(hash
, linux_banner
, strlen(linux_banner
));
99 /* Add in any runtime entropy we can get */
100 hash
= rotate_xor(hash
, &entropy
, sizeof(entropy
));
105 static inline __init
bool kaslr_disabled(void)
108 const char *builtin_cmdline
= CONFIG_CMDLINE
;
110 str
= strstr(builtin_cmdline
, "nokaslr");
111 if (str
== builtin_cmdline
|| (str
> builtin_cmdline
&& *(str
- 1) == ' '))
114 str
= strstr(boot_command_line
, "nokaslr");
115 if (str
== boot_command_line
|| (str
> boot_command_line
&& *(str
- 1) == ' '))
121 /* Choose a new address for the kernel */
122 static inline void __init
*determine_relocation_address(void)
124 unsigned long kernel_length
;
125 unsigned long random_offset
;
126 void *destination
= _text
;
128 if (kaslr_disabled())
131 kernel_length
= (long)_end
- (long)_text
;
133 random_offset
= get_random_boot() << 16;
134 random_offset
&= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET
- 1);
135 if (random_offset
< kernel_length
)
136 random_offset
+= ALIGN(kernel_length
, 0xffff);
138 return RELOCATED_KASLR(destination
);
141 static inline int __init
relocation_addr_valid(void *location_new
)
143 if ((unsigned long)location_new
& 0x00000ffff)
144 return 0; /* Inappropriately aligned new location */
146 if ((unsigned long)location_new
< (unsigned long)_end
)
147 return 0; /* New location overlaps original kernel */
153 static inline void __init
update_reloc_offset(unsigned long *addr
, long random_offset
)
155 unsigned long *new_addr
= (unsigned long *)RELOCATED_KASLR(addr
);
157 *new_addr
= (unsigned long)reloc_offset
;
160 unsigned long __init
relocate_kernel(void)
162 unsigned long kernel_length
;
163 unsigned long random_offset
= 0;
164 void *location_new
= _text
; /* Default to original kernel start */
165 char *cmdline
= early_ioremap(fw_arg1
, COMMAND_LINE_SIZE
); /* Boot command line is passed in fw_arg1 */
167 strscpy(boot_command_line
, cmdline
, COMMAND_LINE_SIZE
);
169 #ifdef CONFIG_RANDOMIZE_BASE
170 location_new
= determine_relocation_address();
172 /* Sanity check relocation address */
173 if (relocation_addr_valid(location_new
))
174 random_offset
= (unsigned long)location_new
- (unsigned long)(_text
);
176 reloc_offset
= (unsigned long)_text
- VMLINUX_LOAD_ADDRESS
;
179 kernel_length
= (long)(_end
) - (long)(_text
);
181 /* Copy the kernel to it's new location */
182 memcpy(location_new
, _text
, kernel_length
);
184 /* Sync the caches ready for execution of new kernel */
185 __asm__
__volatile__ (
190 reloc_offset
+= random_offset
;
192 /* The current thread is now within the relocated kernel */
193 __current_thread_info
= RELOCATED_KASLR(__current_thread_info
);
195 update_reloc_offset(&reloc_offset
, random_offset
);
201 relocate_absolute(random_offset
);
203 return random_offset
;
207 * Show relocation information on panic.
209 static void show_kernel_relocation(const char *level
)
211 if (reloc_offset
> 0) {
213 pr_cont("Kernel relocated by 0x%lx\n", reloc_offset
);
214 pr_cont(" .text @ 0x%px\n", _text
);
215 pr_cont(" .data @ 0x%px\n", _sdata
);
216 pr_cont(" .bss @ 0x%px\n", __bss_start
);
220 static int kernel_location_notifier_fn(struct notifier_block
*self
,
221 unsigned long v
, void *p
)
223 show_kernel_relocation(KERN_EMERG
);
227 static struct notifier_block kernel_location_notifier
= {
228 .notifier_call
= kernel_location_notifier_fn
231 static int __init
register_kernel_offset_dumper(void)
233 atomic_notifier_chain_register(&panic_notifier_list
,
234 &kernel_location_notifier
);
238 arch_initcall(register_kernel_offset_dumper
);