2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Support for Kernel relocation at boot time
8 * Copyright (C) 2015, Imagination Technologies Ltd.
9 * Authors: Matt Redfearn (matt.redfearn@mips.com)
11 #include <asm/bootinfo.h>
12 #include <asm/cacheflush.h>
13 #include <asm/fw/fw.h>
14 #include <asm/sections.h>
15 #include <asm/setup.h>
16 #include <asm/timex.h>
17 #include <linux/elf.h>
18 #include <linux/kernel.h>
19 #include <linux/libfdt.h>
20 #include <linux/of_fdt.h>
21 #include <linux/sched/task.h>
22 #include <linux/start_kernel.h>
23 #include <linux/string.h>
24 #include <linux/printk.h>
26 #define RELOCATED(x) ((void *)((long)x + offset))
28 extern u32 _relocation_start[]; /* End kernel image / start relocation table */
29 extern u32 _relocation_end[]; /* End relocation table */
31 extern long __start___ex_table; /* Start exception table */
32 extern long __stop___ex_table; /* End exception table */
34 extern void __weak plat_fdt_relocated(void *new_location);
37 * This function may be defined for a platform to perform any post-relocation
39 * Return non-zero to abort relocation
41 int __weak plat_post_relocation(long offset)
46 static inline u32 __init get_synci_step(void)
50 __asm__("rdhwr %0, $1" : "=r" (res));
55 static void __init sync_icache(void *kbase, unsigned long kernel_length)
57 void *kend = kbase + kernel_length;
58 u32 step = get_synci_step();
67 } while (step && kbase < kend);
69 /* Completion barrier */
73 static void __init apply_r_mips_64_rel(u32 *loc_new, long offset)
75 *(u64 *)loc_new += offset;
78 static void __init apply_r_mips_32_rel(u32 *loc_new, long offset)
83 static int __init apply_r_mips_26_rel(u32 *loc_orig, u32 *loc_new, long offset)
85 unsigned long target_addr = (*loc_orig) & 0x03ffffff;
88 pr_err("Dangerous R_MIPS_26 REL relocation\n");
92 /* Original target address */
94 target_addr += (unsigned long)loc_orig & 0xf0000000;
96 /* Get the new target address */
97 target_addr += offset;
99 if ((target_addr & 0xf0000000) != ((unsigned long)loc_new & 0xf0000000)) {
100 pr_err("R_MIPS_26 REL relocation overflow\n");
104 target_addr -= (unsigned long)loc_new & 0xf0000000;
107 *loc_new = (*loc_new & ~0x03ffffff) | (target_addr & 0x03ffffff);
113 static void __init apply_r_mips_hi16_rel(u32 *loc_orig, u32 *loc_new,
116 unsigned long insn = *loc_orig;
117 unsigned long target = (insn & 0xffff) << 16; /* high 16bits of target */
121 *loc_new = (insn & ~0xffff) | ((target >> 16) & 0xffff);
124 static int __init reloc_handler(u32 type, u32 *loc_orig, u32 *loc_new,
129 apply_r_mips_64_rel(loc_new, offset);
132 apply_r_mips_32_rel(loc_new, offset);
135 return apply_r_mips_26_rel(loc_orig, loc_new, offset);
137 apply_r_mips_hi16_rel(loc_orig, loc_new, offset);
140 pr_err("Unhandled relocation type %d at 0x%pK\n", type,
148 static int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
156 for (r = _relocation_start; r < _relocation_end; r++) {
157 /* Sentinel for last relocation */
161 type = (*r >> 24) & 0xff;
162 loc_orig = kbase_old + ((*r & 0x00ffffff) << 2);
163 loc_new = RELOCATED(loc_orig);
165 res = reloc_handler(type, loc_orig, loc_new, offset);
174 * The exception table is filled in by the relocs tool after vmlinux is linked.
175 * It must be relocated separately since there will not be any relocation
176 * information for it filled in by the linker.
178 static int __init relocate_exception_table(long offset)
180 unsigned long *etable_start, *etable_end, *e;
182 etable_start = RELOCATED(&__start___ex_table);
183 etable_end = RELOCATED(&__stop___ex_table);
185 for (e = etable_start; e < etable_end; e++)
191 #ifdef CONFIG_RANDOMIZE_BASE
193 static inline __init unsigned long rotate_xor(unsigned long hash,
194 const void *area, size_t size)
196 const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
199 diff = (void *)ptr - area;
200 if (unlikely(size < diff + sizeof(hash)))
203 size = ALIGN_DOWN(size - diff, sizeof(hash));
205 for (i = 0; i < size / sizeof(hash); i++) {
206 /* Rotate by odd number of bits and XOR. */
207 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
214 static inline __init unsigned long get_random_boot(void)
216 unsigned long entropy = random_get_entropy();
217 unsigned long hash = 0;
219 /* Attempt to create a simple but unpredictable starting entropy. */
220 hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
222 /* Add in any runtime entropy we can get */
223 hash = rotate_xor(hash, &entropy, sizeof(entropy));
225 #if defined(CONFIG_USE_OF)
226 /* Get any additional entropy passed in device tree */
227 if (initial_boot_params) {
231 node = fdt_path_offset(initial_boot_params, "/chosen");
233 prop = fdt_getprop_w(initial_boot_params, node,
235 if (prop && (len == sizeof(u64)))
236 hash = rotate_xor(hash, prop, sizeof(*prop));
239 #endif /* CONFIG_USE_OF */
244 static inline __init bool kaslr_disabled(void)
248 #if defined(CONFIG_CMDLINE_BOOL)
249 const char *builtin_cmdline = CONFIG_CMDLINE;
251 str = strstr(builtin_cmdline, "nokaslr");
252 if (str == builtin_cmdline ||
253 (str > builtin_cmdline && *(str - 1) == ' '))
256 str = strstr(arcs_cmdline, "nokaslr");
257 if (str == arcs_cmdline || (str > arcs_cmdline && *(str - 1) == ' '))
263 static inline void __init *determine_relocation_address(void)
265 /* Choose a new address for the kernel */
266 unsigned long kernel_length;
268 unsigned long offset;
270 if (kaslr_disabled())
273 kernel_length = (long)_end - (long)(&_text);
275 offset = get_random_boot() << 16;
276 offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1);
277 if (offset < kernel_length)
278 offset += ALIGN(kernel_length, 0xffff);
280 return RELOCATED(dest);
285 static inline void __init *determine_relocation_address(void)
288 * Choose a new address for the kernel
289 * For now we'll hard code the destination
291 return (void *)0xffffffff81000000;
296 static inline int __init relocation_addr_valid(void *loc_new)
298 if ((unsigned long)loc_new & 0x0000ffff) {
299 /* Inappropriately aligned new location */
302 if ((unsigned long)loc_new < (unsigned long)&_end) {
303 /* New location overlaps original kernel */
309 static inline void __init update_kaslr_offset(unsigned long *addr, long offset)
311 unsigned long *new_addr = (unsigned long *)RELOCATED(addr);
313 *new_addr = (unsigned long)offset;
316 #if defined(CONFIG_USE_OF)
317 void __weak *plat_get_fdt(void)
323 void *__init relocate_kernel(void)
326 unsigned long kernel_length;
327 unsigned long bss_length;
330 /* Default to original kernel entry point */
331 void *kernel_entry = start_kernel;
334 /* Get the command line */
336 #if defined(CONFIG_USE_OF)
337 /* Deal with the device tree */
338 fdt = plat_get_fdt();
339 early_init_dt_scan(fdt);
340 if (boot_command_line[0]) {
341 /* Boot command line was passed in device tree */
342 strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE);
344 #endif /* CONFIG_USE_OF */
346 kernel_length = (long)(&_relocation_start) - (long)(&_text);
347 bss_length = (long)&__bss_stop - (long)&__bss_start;
349 loc_new = determine_relocation_address();
351 /* Sanity check relocation address */
352 if (relocation_addr_valid(loc_new))
353 offset = (unsigned long)loc_new - (unsigned long)(&_text);
355 /* Reset the command line now so we don't end up with a duplicate */
356 arcs_cmdline[0] = '\0';
359 void (*fdt_relocated_)(void *) = NULL;
360 #if defined(CONFIG_USE_OF)
361 unsigned long fdt_phys = virt_to_phys(fdt);
364 * If built-in dtb is used then it will have been relocated
365 * during kernel _text relocation. If appended DTB is used
366 * then it will not be relocated, but it should remain
367 * intact in the original location. If dtb is loaded by
368 * the bootloader then it may need to be moved if it crosses
369 * the target memory area
372 if (fdt_phys >= virt_to_phys(RELOCATED(&_text)) &&
373 fdt_phys <= virt_to_phys(RELOCATED(&_end))) {
374 void *fdt_relocated =
375 RELOCATED(ALIGN((long)&_end, PAGE_SIZE));
376 memcpy(fdt_relocated, fdt, fdt_totalsize(fdt));
378 fdt_relocated_ = RELOCATED(&plat_fdt_relocated);
380 #endif /* CONFIG_USE_OF */
382 /* Copy the kernel to it's new location */
383 memcpy(loc_new, &_text, kernel_length);
385 /* Perform relocations on the new kernel */
386 res = do_relocations(&_text, loc_new, offset);
390 /* Sync the caches ready for execution of new kernel */
391 sync_icache(loc_new, kernel_length);
393 res = relocate_exception_table(offset);
398 * The original .bss has already been cleared, and
399 * some variables such as command line parameters
400 * stored to it so make a copy in the new location.
402 memcpy(RELOCATED(&__bss_start), &__bss_start, bss_length);
405 * If fdt was stored outside of the kernel image and
406 * had to be moved then update platform's state data
407 * with the new fdt location
413 * Last chance for the platform to abort relocation.
414 * This may also be used by the platform to perform any
415 * initialisation required now that the new kernel is
416 * resident in memory and ready to be executed.
418 if (plat_post_relocation(offset))
421 /* The current thread is now within the relocated image */
422 __current_thread_info = RELOCATED(&init_thread_union);
424 /* Return the new kernel's entry point */
425 kernel_entry = RELOCATED(start_kernel);
427 /* Error may occur before, so keep it at last */
428 update_kaslr_offset(&__kaslr_offset, offset);
435 * Show relocation information on panic.
437 static void show_kernel_relocation(const char *level)
439 if (__kaslr_offset > 0) {
441 pr_cont("Kernel relocated by 0x%pK\n", (void *)__kaslr_offset);
442 pr_cont(" .text @ 0x%pK\n", _text);
443 pr_cont(" .data @ 0x%pK\n", _sdata);
444 pr_cont(" .bss @ 0x%pK\n", __bss_start);
448 static int kernel_location_notifier_fn(struct notifier_block *self,
449 unsigned long v, void *p)
451 show_kernel_relocation(KERN_EMERG);
455 static struct notifier_block kernel_location_notifier = {
456 .notifier_call = kernel_location_notifier_fn
459 static int __init register_kernel_offset_dumper(void)
461 atomic_notifier_chain_register(&panic_notifier_list,
462 &kernel_location_notifier);
465 __initcall(register_kernel_offset_dumper);