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>
17 #include <asm/sections.h>
18 #include <asm/setup.h>
20 #define RELOCATED(x) ((void *)((long)x + reloc_offset))
21 #define RELOCATED_KASLR(x) ((void *)((long)x + random_offset))
23 static unsigned long reloc_offset;
25 static inline void __init relocate_relative(void)
27 Elf64_Rela *rela, *rela_end;
28 rela = (Elf64_Rela *)&__rela_dyn_begin;
29 rela_end = (Elf64_Rela *)&__rela_dyn_end;
31 for ( ; rela < rela_end; rela++) {
32 Elf64_Addr addr = rela->r_offset;
33 Elf64_Addr relocated_addr = rela->r_addend;
35 if (rela->r_info != R_LARCH_RELATIVE)
38 relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr);
39 *(Elf64_Addr *)RELOCATED(addr) = relocated_addr;
44 u64 *relr = (u64 *)&__relr_dyn_begin;
45 u64 *relr_end = (u64 *)&__relr_dyn_end;
47 for ( ; relr < relr_end; relr++) {
48 if ((*relr & 1) == 0) {
49 addr = (u64 *)(*relr + reloc_offset);
50 *addr++ += reloc_offset;
52 for (u64 *p = addr, r = *relr >> 1; r; p++, r >>= 1)
61 static inline void __init relocate_absolute(long random_offset)
64 struct rela_la_abs *p;
66 begin = RELOCATED_KASLR(&__la_abs_begin);
67 end = RELOCATED_KASLR(&__la_abs_end);
69 for (p = begin; (void *)p < end; p++) {
71 uint32_t lu12iw, ori, lu32id, lu52id;
72 union loongarch_instruction *insn = (void *)p->pc;
74 lu12iw = (v >> 12) & 0xfffff;
76 lu32id = (v >> 32) & 0xfffff;
79 insn[0].reg1i20_format.immediate = lu12iw;
80 insn[1].reg2i12_format.immediate = ori;
81 insn[2].reg1i20_format.immediate = lu32id;
82 insn[3].reg2i12_format.immediate = lu52id;
86 #ifdef CONFIG_RANDOMIZE_BASE
87 static inline __init unsigned long rotate_xor(unsigned long hash,
88 const void *area, size_t size)
91 const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
93 diff = (void *)ptr - area;
94 if (size < diff + sizeof(hash))
97 size = ALIGN_DOWN(size - diff, sizeof(hash));
99 for (i = 0; i < size / sizeof(hash); i++) {
100 /* Rotate by odd number of bits and XOR. */
101 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
108 static inline __init unsigned long get_random_boot(void)
110 unsigned long hash = 0;
111 unsigned long entropy = random_get_entropy();
113 /* Attempt to create a simple but unpredictable starting entropy. */
114 hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
116 /* Add in any runtime entropy we can get */
117 hash = rotate_xor(hash, &entropy, sizeof(entropy));
122 static int __init nokaslr(char *p)
124 pr_info("KASLR is disabled.\n");
126 return 0; /* Print a notice and silence the boot warning */
128 early_param("nokaslr", nokaslr);
130 static inline __init bool kaslr_disabled(void)
133 const char *builtin_cmdline = CONFIG_CMDLINE;
135 str = strstr(builtin_cmdline, "nokaslr");
136 if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
139 str = strstr(boot_command_line, "nokaslr");
140 if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
143 #ifdef CONFIG_HIBERNATION
144 str = strstr(builtin_cmdline, "nohibernate");
145 if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
148 str = strstr(boot_command_line, "nohibernate");
149 if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
152 str = strstr(builtin_cmdline, "noresume");
153 if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
156 str = strstr(boot_command_line, "noresume");
157 if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
160 str = strstr(builtin_cmdline, "resume=");
161 if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
164 str = strstr(boot_command_line, "resume=");
165 if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
172 /* Choose a new address for the kernel */
173 static inline void __init *determine_relocation_address(void)
175 unsigned long kernel_length;
176 unsigned long random_offset;
177 void *destination = _text;
179 if (kaslr_disabled())
182 kernel_length = (long)_end - (long)_text;
184 random_offset = get_random_boot() << 16;
185 random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1);
186 if (random_offset < kernel_length)
187 random_offset += ALIGN(kernel_length, 0xffff);
189 return RELOCATED_KASLR(destination);
192 static inline int __init relocation_addr_valid(void *location_new)
194 if ((unsigned long)location_new & 0x00000ffff)
195 return 0; /* Inappropriately aligned new location */
197 if ((unsigned long)location_new < (unsigned long)_end)
198 return 0; /* New location overlaps original kernel */
204 static inline void __init update_reloc_offset(unsigned long *addr, long random_offset)
206 unsigned long *new_addr = (unsigned long *)RELOCATED_KASLR(addr);
208 *new_addr = (unsigned long)reloc_offset;
211 unsigned long __init relocate_kernel(void)
213 unsigned long kernel_length;
214 unsigned long random_offset = 0;
215 void *location_new = _text; /* Default to original kernel start */
216 char *cmdline = early_memremap_ro(fw_arg1, COMMAND_LINE_SIZE); /* Boot command line is passed in fw_arg1 */
218 strscpy(boot_command_line, cmdline, COMMAND_LINE_SIZE);
220 #ifdef CONFIG_RANDOMIZE_BASE
221 location_new = determine_relocation_address();
223 /* Sanity check relocation address */
224 if (relocation_addr_valid(location_new))
225 random_offset = (unsigned long)location_new - (unsigned long)(_text);
227 reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS;
228 early_memunmap(cmdline, COMMAND_LINE_SIZE);
231 kernel_length = (long)(_end) - (long)(_text);
233 /* Copy the kernel to it's new location */
234 memcpy(location_new, _text, kernel_length);
236 /* Sync the caches ready for execution of new kernel */
237 __asm__ __volatile__ (
242 reloc_offset += random_offset;
244 /* The current thread is now within the relocated kernel */
245 __current_thread_info = RELOCATED_KASLR(__current_thread_info);
247 update_reloc_offset(&reloc_offset, random_offset);
253 relocate_absolute(random_offset);
255 return random_offset;
259 * Show relocation information on panic.
261 static void show_kernel_relocation(const char *level)
263 if (reloc_offset > 0) {
265 pr_cont("Kernel relocated by 0x%lx\n", reloc_offset);
266 pr_cont(" .text @ 0x%px\n", _text);
267 pr_cont(" .data @ 0x%px\n", _sdata);
268 pr_cont(" .bss @ 0x%px\n", __bss_start);
272 static int kernel_location_notifier_fn(struct notifier_block *self,
273 unsigned long v, void *p)
275 show_kernel_relocation(KERN_EMERG);
279 static struct notifier_block kernel_location_notifier = {
280 .notifier_call = kernel_location_notifier_fn
283 static int __init register_kernel_offset_dumper(void)
285 atomic_notifier_chain_register(&panic_notifier_list,
286 &kernel_location_notifier);
290 arch_initcall(register_kernel_offset_dumper);