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/panic_notifier.h>
22 #include <linux/sched/task.h>
23 #include <linux/start_kernel.h>
24 #include <linux/string.h>
25 #include <linux/printk.h>
27 #define RELOCATED(x) ((void *)((long)x + offset))
29 extern u32 _relocation_start
[]; /* End kernel image / start relocation table */
30 extern u32 _relocation_end
[]; /* End relocation table */
32 extern long __start___ex_table
; /* Start exception table */
33 extern long __stop___ex_table
; /* End exception table */
35 extern void __weak
plat_fdt_relocated(void *new_location
);
38 * This function may be defined for a platform to perform any post-relocation
40 * Return non-zero to abort relocation
42 int __weak
plat_post_relocation(long offset
)
47 static inline u32 __init
get_synci_step(void)
51 __asm__("rdhwr %0, $1" : "=r" (res
));
56 static void __init
sync_icache(void *kbase
, unsigned long kernel_length
)
58 void *kend
= kbase
+ kernel_length
;
59 u32 step
= get_synci_step();
68 } while (step
&& kbase
< kend
);
70 /* Completion barrier */
74 static void __init
apply_r_mips_64_rel(u32
*loc_new
, long offset
)
76 *(u64
*)loc_new
+= offset
;
79 static void __init
apply_r_mips_32_rel(u32
*loc_new
, long offset
)
84 static int __init
apply_r_mips_26_rel(u32
*loc_orig
, u32
*loc_new
, long offset
)
86 unsigned long target_addr
= (*loc_orig
) & 0x03ffffff;
89 pr_err("Dangerous R_MIPS_26 REL relocation\n");
93 /* Original target address */
95 target_addr
+= (unsigned long)loc_orig
& 0xf0000000;
97 /* Get the new target address */
98 target_addr
+= offset
;
100 if ((target_addr
& 0xf0000000) != ((unsigned long)loc_new
& 0xf0000000)) {
101 pr_err("R_MIPS_26 REL relocation overflow\n");
105 target_addr
-= (unsigned long)loc_new
& 0xf0000000;
108 *loc_new
= (*loc_new
& ~0x03ffffff) | (target_addr
& 0x03ffffff);
114 static void __init
apply_r_mips_hi16_rel(u32
*loc_orig
, u32
*loc_new
,
117 unsigned long insn
= *loc_orig
;
118 unsigned long target
= (insn
& 0xffff) << 16; /* high 16bits of target */
122 *loc_new
= (insn
& ~0xffff) | ((target
>> 16) & 0xffff);
125 static int __init
reloc_handler(u32 type
, u32
*loc_orig
, u32
*loc_new
,
130 apply_r_mips_64_rel(loc_new
, offset
);
133 apply_r_mips_32_rel(loc_new
, offset
);
136 return apply_r_mips_26_rel(loc_orig
, loc_new
, offset
);
138 apply_r_mips_hi16_rel(loc_orig
, loc_new
, offset
);
141 pr_err("Unhandled relocation type %d at 0x%pK\n", type
,
149 static int __init
do_relocations(void *kbase_old
, void *kbase_new
, long offset
)
157 for (r
= _relocation_start
; r
< _relocation_end
; r
++) {
158 /* Sentinel for last relocation */
162 type
= (*r
>> 24) & 0xff;
163 loc_orig
= kbase_old
+ ((*r
& 0x00ffffff) << 2);
164 loc_new
= RELOCATED(loc_orig
);
166 res
= reloc_handler(type
, loc_orig
, loc_new
, offset
);
175 * The exception table is filled in by the relocs tool after vmlinux is linked.
176 * It must be relocated separately since there will not be any relocation
177 * information for it filled in by the linker.
179 static int __init
relocate_exception_table(long offset
)
181 unsigned long *etable_start
, *etable_end
, *e
;
183 etable_start
= RELOCATED(&__start___ex_table
);
184 etable_end
= RELOCATED(&__stop___ex_table
);
186 for (e
= etable_start
; e
< etable_end
; e
++)
192 #ifdef CONFIG_RANDOMIZE_BASE
194 static inline __init
unsigned long rotate_xor(unsigned long hash
,
195 const void *area
, size_t size
)
197 const typeof(hash
) *ptr
= PTR_ALIGN(area
, sizeof(hash
));
200 diff
= (void *)ptr
- area
;
201 if (unlikely(size
< diff
+ sizeof(hash
)))
204 size
= ALIGN_DOWN(size
- diff
, sizeof(hash
));
206 for (i
= 0; i
< size
/ sizeof(hash
); i
++) {
207 /* Rotate by odd number of bits and XOR. */
208 hash
= (hash
<< ((sizeof(hash
) * 8) - 7)) | (hash
>> 7);
215 static inline __init
unsigned long get_random_boot(void)
217 unsigned long entropy
= random_get_entropy();
218 unsigned long hash
= 0;
220 /* Attempt to create a simple but unpredictable starting entropy. */
221 hash
= rotate_xor(hash
, linux_banner
, strlen(linux_banner
));
223 /* Add in any runtime entropy we can get */
224 hash
= rotate_xor(hash
, &entropy
, sizeof(entropy
));
226 #if defined(CONFIG_USE_OF)
227 /* Get any additional entropy passed in device tree */
228 if (initial_boot_params
) {
232 node
= fdt_path_offset(initial_boot_params
, "/chosen");
234 prop
= fdt_getprop_w(initial_boot_params
, node
,
236 if (prop
&& (len
== sizeof(u64
)))
237 hash
= rotate_xor(hash
, prop
, sizeof(*prop
));
240 #endif /* CONFIG_USE_OF */
245 static inline __init
bool kaslr_disabled(void)
249 #if defined(CONFIG_CMDLINE_BOOL)
250 const char *builtin_cmdline
= CONFIG_CMDLINE
;
252 str
= strstr(builtin_cmdline
, "nokaslr");
253 if (str
== builtin_cmdline
||
254 (str
> builtin_cmdline
&& *(str
- 1) == ' '))
257 str
= strstr(arcs_cmdline
, "nokaslr");
258 if (str
== arcs_cmdline
|| (str
> arcs_cmdline
&& *(str
- 1) == ' '))
264 static inline void __init
*determine_relocation_address(void)
266 /* Choose a new address for the kernel */
267 unsigned long kernel_length
;
269 unsigned long offset
;
271 if (kaslr_disabled())
274 kernel_length
= (long)_end
- (long)(&_text
);
276 offset
= get_random_boot() << 16;
277 offset
&= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET
- 1);
278 if (offset
< kernel_length
)
279 offset
+= ALIGN(kernel_length
, 0xffff);
281 return RELOCATED(dest
);
286 static inline void __init
*determine_relocation_address(void)
289 * Choose a new address for the kernel
290 * For now we'll hard code the destination
292 return (void *)0xffffffff81000000;
297 static inline int __init
relocation_addr_valid(void *loc_new
)
299 if ((unsigned long)loc_new
& 0x0000ffff) {
300 /* Inappropriately aligned new location */
303 if ((unsigned long)loc_new
< (unsigned long)&_end
) {
304 /* New location overlaps original kernel */
310 static inline void __init
update_kaslr_offset(unsigned long *addr
, long offset
)
312 unsigned long *new_addr
= (unsigned long *)RELOCATED(addr
);
314 *new_addr
= (unsigned long)offset
;
317 #if defined(CONFIG_USE_OF)
318 void __weak
*plat_get_fdt(void)
324 void *__init
relocate_kernel(void)
327 unsigned long kernel_length
;
328 unsigned long bss_length
;
331 /* Default to original kernel entry point */
332 void *kernel_entry
= start_kernel
;
335 /* Get the command line */
337 #if defined(CONFIG_USE_OF)
338 /* Deal with the device tree */
339 fdt
= plat_get_fdt();
340 early_init_dt_scan(fdt
, __pa(fdt
));
341 if (boot_command_line
[0]) {
342 /* Boot command line was passed in device tree */
343 strscpy(arcs_cmdline
, boot_command_line
, COMMAND_LINE_SIZE
);
345 #endif /* CONFIG_USE_OF */
347 kernel_length
= (long)(&_relocation_start
) - (long)(&_text
);
348 bss_length
= (long)&__bss_stop
- (long)&__bss_start
;
350 loc_new
= determine_relocation_address();
352 /* Sanity check relocation address */
353 if (relocation_addr_valid(loc_new
))
354 offset
= (unsigned long)loc_new
- (unsigned long)(&_text
);
356 /* Reset the command line now so we don't end up with a duplicate */
357 arcs_cmdline
[0] = '\0';
360 void (*fdt_relocated_
)(void *) = NULL
;
361 #if defined(CONFIG_USE_OF)
362 unsigned long fdt_phys
= virt_to_phys(fdt
);
365 * If built-in dtb is used then it will have been relocated
366 * during kernel _text relocation. If appended DTB is used
367 * then it will not be relocated, but it should remain
368 * intact in the original location. If dtb is loaded by
369 * the bootloader then it may need to be moved if it crosses
370 * the target memory area
373 if (fdt_phys
>= virt_to_phys(RELOCATED(&_text
)) &&
374 fdt_phys
<= virt_to_phys(RELOCATED(&_end
))) {
375 void *fdt_relocated
=
376 RELOCATED(ALIGN((long)&_end
, PAGE_SIZE
));
377 memcpy(fdt_relocated
, fdt
, fdt_totalsize(fdt
));
379 fdt_relocated_
= RELOCATED(&plat_fdt_relocated
);
381 #endif /* CONFIG_USE_OF */
383 /* Copy the kernel to its new location */
384 memcpy(loc_new
, &_text
, kernel_length
);
386 /* Perform relocations on the new kernel */
387 res
= do_relocations(&_text
, loc_new
, offset
);
391 /* Sync the caches ready for execution of new kernel */
392 sync_icache(loc_new
, kernel_length
);
394 res
= relocate_exception_table(offset
);
399 * The original .bss has already been cleared, and
400 * some variables such as command line parameters
401 * stored to it so make a copy in the new location.
403 memcpy(RELOCATED(&__bss_start
), &__bss_start
, bss_length
);
406 * If fdt was stored outside of the kernel image and
407 * had to be moved then update platform's state data
408 * with the new fdt location
414 * Last chance for the platform to abort relocation.
415 * This may also be used by the platform to perform any
416 * initialisation required now that the new kernel is
417 * resident in memory and ready to be executed.
419 if (plat_post_relocation(offset
))
422 /* The current thread is now within the relocated image */
423 __current_thread_info
= RELOCATED(&init_thread_union
);
425 /* Return the new kernel's entry point */
426 kernel_entry
= RELOCATED(start_kernel
);
428 /* Error may occur before, so keep it at last */
429 update_kaslr_offset(&__kaslr_offset
, offset
);
436 * Show relocation information on panic.
438 static void show_kernel_relocation(const char *level
)
440 if (__kaslr_offset
> 0) {
442 pr_cont("Kernel relocated by 0x%pK\n", (void *)__kaslr_offset
);
443 pr_cont(" .text @ 0x%pK\n", _text
);
444 pr_cont(" .data @ 0x%pK\n", _sdata
);
445 pr_cont(" .bss @ 0x%pK\n", __bss_start
);
449 static int kernel_location_notifier_fn(struct notifier_block
*self
,
450 unsigned long v
, void *p
)
452 show_kernel_relocation(KERN_EMERG
);
456 static struct notifier_block kernel_location_notifier
= {
457 .notifier_call
= kernel_location_notifier_fn
460 static int __init
register_kernel_offset_dumper(void)
462 atomic_notifier_chain_register(&panic_notifier_list
,
463 &kernel_location_notifier
);
466 __initcall(register_kernel_offset_dumper
);