Loading...
1/*
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
4 * for more details.
5 *
6 * Support for Kernel relocation at boot time
7 *
8 * Copyright (C) 2015, Imagination Technologies Ltd.
9 * Authors: Matt Redfearn (matt.redfearn@mips.com)
10 */
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>
26
27#define RELOCATED(x) ((void *)((long)x + offset))
28
29extern u32 _relocation_start[]; /* End kernel image / start relocation table */
30extern u32 _relocation_end[]; /* End relocation table */
31
32extern long __start___ex_table; /* Start exception table */
33extern long __stop___ex_table; /* End exception table */
34
35extern void __weak plat_fdt_relocated(void *new_location);
36
37/*
38 * This function may be defined for a platform to perform any post-relocation
39 * fixup necessary.
40 * Return non-zero to abort relocation
41 */
42int __weak plat_post_relocation(long offset)
43{
44 return 0;
45}
46
47static inline u32 __init get_synci_step(void)
48{
49 u32 res;
50
51 __asm__("rdhwr %0, $1" : "=r" (res));
52
53 return res;
54}
55
56static void __init sync_icache(void *kbase, unsigned long kernel_length)
57{
58 void *kend = kbase + kernel_length;
59 u32 step = get_synci_step();
60
61 do {
62 __asm__ __volatile__(
63 "synci 0(%0)"
64 : /* no output */
65 : "r" (kbase));
66
67 kbase += step;
68 } while (step && kbase < kend);
69
70 /* Completion barrier */
71 __sync();
72}
73
74static void __init apply_r_mips_64_rel(u32 *loc_new, long offset)
75{
76 *(u64 *)loc_new += offset;
77}
78
79static void __init apply_r_mips_32_rel(u32 *loc_new, long offset)
80{
81 *loc_new += offset;
82}
83
84static int __init apply_r_mips_26_rel(u32 *loc_orig, u32 *loc_new, long offset)
85{
86 unsigned long target_addr = (*loc_orig) & 0x03ffffff;
87
88 if (offset % 4) {
89 pr_err("Dangerous R_MIPS_26 REL relocation\n");
90 return -ENOEXEC;
91 }
92
93 /* Original target address */
94 target_addr <<= 2;
95 target_addr += (unsigned long)loc_orig & 0xf0000000;
96
97 /* Get the new target address */
98 target_addr += offset;
99
100 if ((target_addr & 0xf0000000) != ((unsigned long)loc_new & 0xf0000000)) {
101 pr_err("R_MIPS_26 REL relocation overflow\n");
102 return -ENOEXEC;
103 }
104
105 target_addr -= (unsigned long)loc_new & 0xf0000000;
106 target_addr >>= 2;
107
108 *loc_new = (*loc_new & ~0x03ffffff) | (target_addr & 0x03ffffff);
109
110 return 0;
111}
112
113
114static void __init apply_r_mips_hi16_rel(u32 *loc_orig, u32 *loc_new,
115 long offset)
116{
117 unsigned long insn = *loc_orig;
118 unsigned long target = (insn & 0xffff) << 16; /* high 16bits of target */
119
120 target += offset;
121
122 *loc_new = (insn & ~0xffff) | ((target >> 16) & 0xffff);
123}
124
125static int __init reloc_handler(u32 type, u32 *loc_orig, u32 *loc_new,
126 long offset)
127{
128 switch (type) {
129 case R_MIPS_64:
130 apply_r_mips_64_rel(loc_new, offset);
131 break;
132 case R_MIPS_32:
133 apply_r_mips_32_rel(loc_new, offset);
134 break;
135 case R_MIPS_26:
136 return apply_r_mips_26_rel(loc_orig, loc_new, offset);
137 case R_MIPS_HI16:
138 apply_r_mips_hi16_rel(loc_orig, loc_new, offset);
139 break;
140 default:
141 pr_err("Unhandled relocation type %d at 0x%pK\n", type,
142 loc_orig);
143 return -ENOEXEC;
144 }
145
146 return 0;
147}
148
149static int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
150{
151 u32 *r;
152 u32 *loc_orig;
153 u32 *loc_new;
154 int type;
155 int res;
156
157 for (r = _relocation_start; r < _relocation_end; r++) {
158 /* Sentinel for last relocation */
159 if (*r == 0)
160 break;
161
162 type = (*r >> 24) & 0xff;
163 loc_orig = kbase_old + ((*r & 0x00ffffff) << 2);
164 loc_new = RELOCATED(loc_orig);
165
166 res = reloc_handler(type, loc_orig, loc_new, offset);
167 if (res)
168 return res;
169 }
170
171 return 0;
172}
173
174/*
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.
178 */
179static int __init relocate_exception_table(long offset)
180{
181 unsigned long *etable_start, *etable_end, *e;
182
183 etable_start = RELOCATED(&__start___ex_table);
184 etable_end = RELOCATED(&__stop___ex_table);
185
186 for (e = etable_start; e < etable_end; e++)
187 *e += offset;
188
189 return 0;
190}
191
192#ifdef CONFIG_RANDOMIZE_BASE
193
194static inline __init unsigned long rotate_xor(unsigned long hash,
195 const void *area, size_t size)
196{
197 const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
198 size_t diff, i;
199
200 diff = (void *)ptr - area;
201 if (unlikely(size < diff + sizeof(hash)))
202 return hash;
203
204 size = ALIGN_DOWN(size - diff, sizeof(hash));
205
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);
209 hash ^= ptr[i];
210 }
211
212 return hash;
213}
214
215static inline __init unsigned long get_random_boot(void)
216{
217 unsigned long entropy = random_get_entropy();
218 unsigned long hash = 0;
219
220 /* Attempt to create a simple but unpredictable starting entropy. */
221 hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
222
223 /* Add in any runtime entropy we can get */
224 hash = rotate_xor(hash, &entropy, sizeof(entropy));
225
226#if defined(CONFIG_USE_OF)
227 /* Get any additional entropy passed in device tree */
228 if (initial_boot_params) {
229 int node, len;
230 u64 *prop;
231
232 node = fdt_path_offset(initial_boot_params, "/chosen");
233 if (node >= 0) {
234 prop = fdt_getprop_w(initial_boot_params, node,
235 "kaslr-seed", &len);
236 if (prop && (len == sizeof(u64)))
237 hash = rotate_xor(hash, prop, sizeof(*prop));
238 }
239 }
240#endif /* CONFIG_USE_OF */
241
242 return hash;
243}
244
245static inline __init bool kaslr_disabled(void)
246{
247 char *str;
248
249#if defined(CONFIG_CMDLINE_BOOL)
250 const char *builtin_cmdline = CONFIG_CMDLINE;
251
252 str = strstr(builtin_cmdline, "nokaslr");
253 if (str == builtin_cmdline ||
254 (str > builtin_cmdline && *(str - 1) == ' '))
255 return true;
256#endif
257 str = strstr(arcs_cmdline, "nokaslr");
258 if (str == arcs_cmdline || (str > arcs_cmdline && *(str - 1) == ' '))
259 return true;
260
261 return false;
262}
263
264static inline void __init *determine_relocation_address(void)
265{
266 /* Choose a new address for the kernel */
267 unsigned long kernel_length;
268 void *dest = &_text;
269 unsigned long offset;
270
271 if (kaslr_disabled())
272 return dest;
273
274 kernel_length = (long)_end - (long)(&_text);
275
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);
280
281 return RELOCATED(dest);
282}
283
284#else
285
286static inline void __init *determine_relocation_address(void)
287{
288 /*
289 * Choose a new address for the kernel
290 * For now we'll hard code the destination
291 */
292 return (void *)0xffffffff81000000;
293}
294
295#endif
296
297static inline int __init relocation_addr_valid(void *loc_new)
298{
299 if ((unsigned long)loc_new & 0x0000ffff) {
300 /* Inappropriately aligned new location */
301 return 0;
302 }
303 if ((unsigned long)loc_new < (unsigned long)&_end) {
304 /* New location overlaps original kernel */
305 return 0;
306 }
307 return 1;
308}
309
310static inline void __init update_kaslr_offset(unsigned long *addr, long offset)
311{
312 unsigned long *new_addr = (unsigned long *)RELOCATED(addr);
313
314 *new_addr = (unsigned long)offset;
315}
316
317#if defined(CONFIG_USE_OF)
318void __weak *plat_get_fdt(void)
319{
320 return NULL;
321}
322#endif
323
324void *__init relocate_kernel(void)
325{
326 void *loc_new;
327 unsigned long kernel_length;
328 unsigned long bss_length;
329 long offset = 0;
330 int res = 1;
331 /* Default to original kernel entry point */
332 void *kernel_entry = start_kernel;
333 void *fdt = NULL;
334
335 /* Get the command line */
336 fw_init_cmdline();
337#if defined(CONFIG_USE_OF)
338 /* Deal with the device tree */
339 fdt = plat_get_fdt();
340 early_init_dt_scan(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);
344 }
345#endif /* CONFIG_USE_OF */
346
347 kernel_length = (long)(&_relocation_start) - (long)(&_text);
348 bss_length = (long)&__bss_stop - (long)&__bss_start;
349
350 loc_new = determine_relocation_address();
351
352 /* Sanity check relocation address */
353 if (relocation_addr_valid(loc_new))
354 offset = (unsigned long)loc_new - (unsigned long)(&_text);
355
356 /* Reset the command line now so we don't end up with a duplicate */
357 arcs_cmdline[0] = '\0';
358
359 if (offset) {
360 void (*fdt_relocated_)(void *) = NULL;
361#if defined(CONFIG_USE_OF)
362 unsigned long fdt_phys = virt_to_phys(fdt);
363
364 /*
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
371 */
372
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));
378 fdt = fdt_relocated;
379 fdt_relocated_ = RELOCATED(&plat_fdt_relocated);
380 }
381#endif /* CONFIG_USE_OF */
382
383 /* Copy the kernel to it's new location */
384 memcpy(loc_new, &_text, kernel_length);
385
386 /* Perform relocations on the new kernel */
387 res = do_relocations(&_text, loc_new, offset);
388 if (res < 0)
389 goto out;
390
391 /* Sync the caches ready for execution of new kernel */
392 sync_icache(loc_new, kernel_length);
393
394 res = relocate_exception_table(offset);
395 if (res < 0)
396 goto out;
397
398 /*
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.
402 */
403 memcpy(RELOCATED(&__bss_start), &__bss_start, bss_length);
404
405 /*
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
409 */
410 if (fdt_relocated_)
411 fdt_relocated_(fdt);
412
413 /*
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.
418 */
419 if (plat_post_relocation(offset))
420 goto out;
421
422 /* The current thread is now within the relocated image */
423 __current_thread_info = RELOCATED(&init_thread_union);
424
425 /* Return the new kernel's entry point */
426 kernel_entry = RELOCATED(start_kernel);
427
428 /* Error may occur before, so keep it at last */
429 update_kaslr_offset(&__kaslr_offset, offset);
430 }
431out:
432 return kernel_entry;
433}
434
435/*
436 * Show relocation information on panic.
437 */
438static void show_kernel_relocation(const char *level)
439{
440 if (__kaslr_offset > 0) {
441 printk(level);
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);
446 }
447}
448
449static int kernel_location_notifier_fn(struct notifier_block *self,
450 unsigned long v, void *p)
451{
452 show_kernel_relocation(KERN_EMERG);
453 return NOTIFY_DONE;
454}
455
456static struct notifier_block kernel_location_notifier = {
457 .notifier_call = kernel_location_notifier_fn
458};
459
460static int __init register_kernel_offset_dumper(void)
461{
462 atomic_notifier_chain_register(&panic_notifier_list,
463 &kernel_location_notifier);
464 return 0;
465}
466__initcall(register_kernel_offset_dumper);
1/*
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
4 * for more details.
5 *
6 * Support for Kernel relocation at boot time
7 *
8 * Copyright (C) 2015, Imagination Technologies Ltd.
9 * Authors: Matt Redfearn (matt.redfearn@mips.com)
10 */
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>
25
26#define RELOCATED(x) ((void *)((long)x + offset))
27
28extern u32 _relocation_start[]; /* End kernel image / start relocation table */
29extern u32 _relocation_end[]; /* End relocation table */
30
31extern long __start___ex_table; /* Start exception table */
32extern long __stop___ex_table; /* End exception table */
33
34extern void __weak plat_fdt_relocated(void *new_location);
35
36/*
37 * This function may be defined for a platform to perform any post-relocation
38 * fixup necessary.
39 * Return non-zero to abort relocation
40 */
41int __weak plat_post_relocation(long offset)
42{
43 return 0;
44}
45
46static inline u32 __init get_synci_step(void)
47{
48 u32 res;
49
50 __asm__("rdhwr %0, $1" : "=r" (res));
51
52 return res;
53}
54
55static void __init sync_icache(void *kbase, unsigned long kernel_length)
56{
57 void *kend = kbase + kernel_length;
58 u32 step = get_synci_step();
59
60 do {
61 __asm__ __volatile__(
62 "synci 0(%0)"
63 : /* no output */
64 : "r" (kbase));
65
66 kbase += step;
67 } while (kbase < kend);
68
69 /* Completion barrier */
70 __sync();
71}
72
73static int __init apply_r_mips_64_rel(u32 *loc_orig, u32 *loc_new, long offset)
74{
75 *(u64 *)loc_new += offset;
76
77 return 0;
78}
79
80static int __init apply_r_mips_32_rel(u32 *loc_orig, u32 *loc_new, long offset)
81{
82 *loc_new += offset;
83
84 return 0;
85}
86
87static int __init apply_r_mips_26_rel(u32 *loc_orig, u32 *loc_new, long offset)
88{
89 unsigned long target_addr = (*loc_orig) & 0x03ffffff;
90
91 if (offset % 4) {
92 pr_err("Dangerous R_MIPS_26 REL relocation\n");
93 return -ENOEXEC;
94 }
95
96 /* Original target address */
97 target_addr <<= 2;
98 target_addr += (unsigned long)loc_orig & ~0x03ffffff;
99
100 /* Get the new target address */
101 target_addr += offset;
102
103 if ((target_addr & 0xf0000000) != ((unsigned long)loc_new & 0xf0000000)) {
104 pr_err("R_MIPS_26 REL relocation overflow\n");
105 return -ENOEXEC;
106 }
107
108 target_addr -= (unsigned long)loc_new & ~0x03ffffff;
109 target_addr >>= 2;
110
111 *loc_new = (*loc_new & ~0x03ffffff) | (target_addr & 0x03ffffff);
112
113 return 0;
114}
115
116
117static int __init apply_r_mips_hi16_rel(u32 *loc_orig, u32 *loc_new, long offset)
118{
119 unsigned long insn = *loc_orig;
120 unsigned long target = (insn & 0xffff) << 16; /* high 16bits of target */
121
122 target += offset;
123
124 *loc_new = (insn & ~0xffff) | ((target >> 16) & 0xffff);
125 return 0;
126}
127
128static int (*reloc_handlers_rel[]) (u32 *, u32 *, long) __initdata = {
129 [R_MIPS_64] = apply_r_mips_64_rel,
130 [R_MIPS_32] = apply_r_mips_32_rel,
131 [R_MIPS_26] = apply_r_mips_26_rel,
132 [R_MIPS_HI16] = apply_r_mips_hi16_rel,
133};
134
135int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
136{
137 u32 *r;
138 u32 *loc_orig;
139 u32 *loc_new;
140 int type;
141 int res;
142
143 for (r = _relocation_start; r < _relocation_end; r++) {
144 /* Sentinel for last relocation */
145 if (*r == 0)
146 break;
147
148 type = (*r >> 24) & 0xff;
149 loc_orig = (void *)(kbase_old + ((*r & 0x00ffffff) << 2));
150 loc_new = RELOCATED(loc_orig);
151
152 if (reloc_handlers_rel[type] == NULL) {
153 /* Unsupported relocation */
154 pr_err("Unhandled relocation type %d at 0x%pK\n",
155 type, loc_orig);
156 return -ENOEXEC;
157 }
158
159 res = reloc_handlers_rel[type](loc_orig, loc_new, offset);
160 if (res)
161 return res;
162 }
163
164 return 0;
165}
166
167/*
168 * The exception table is filled in by the relocs tool after vmlinux is linked.
169 * It must be relocated separately since there will not be any relocation
170 * information for it filled in by the linker.
171 */
172static int __init relocate_exception_table(long offset)
173{
174 unsigned long *etable_start, *etable_end, *e;
175
176 etable_start = RELOCATED(&__start___ex_table);
177 etable_end = RELOCATED(&__stop___ex_table);
178
179 for (e = etable_start; e < etable_end; e++)
180 *e += offset;
181
182 return 0;
183}
184
185#ifdef CONFIG_RANDOMIZE_BASE
186
187static inline __init unsigned long rotate_xor(unsigned long hash,
188 const void *area, size_t size)
189{
190 size_t i;
191 unsigned long *ptr = (unsigned long *)area;
192
193 for (i = 0; i < size / sizeof(hash); i++) {
194 /* Rotate by odd number of bits and XOR. */
195 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
196 hash ^= ptr[i];
197 }
198
199 return hash;
200}
201
202static inline __init unsigned long get_random_boot(void)
203{
204 unsigned long entropy = random_get_entropy();
205 unsigned long hash = 0;
206
207 /* Attempt to create a simple but unpredictable starting entropy. */
208 hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
209
210 /* Add in any runtime entropy we can get */
211 hash = rotate_xor(hash, &entropy, sizeof(entropy));
212
213#if defined(CONFIG_USE_OF)
214 /* Get any additional entropy passed in device tree */
215 if (initial_boot_params) {
216 int node, len;
217 u64 *prop;
218
219 node = fdt_path_offset(initial_boot_params, "/chosen");
220 if (node >= 0) {
221 prop = fdt_getprop_w(initial_boot_params, node,
222 "kaslr-seed", &len);
223 if (prop && (len == sizeof(u64)))
224 hash = rotate_xor(hash, prop, sizeof(*prop));
225 }
226 }
227#endif /* CONFIG_USE_OF */
228
229 return hash;
230}
231
232static inline __init bool kaslr_disabled(void)
233{
234 char *str;
235
236#if defined(CONFIG_CMDLINE_BOOL)
237 const char *builtin_cmdline = CONFIG_CMDLINE;
238
239 str = strstr(builtin_cmdline, "nokaslr");
240 if (str == builtin_cmdline ||
241 (str > builtin_cmdline && *(str - 1) == ' '))
242 return true;
243#endif
244 str = strstr(arcs_cmdline, "nokaslr");
245 if (str == arcs_cmdline || (str > arcs_cmdline && *(str - 1) == ' '))
246 return true;
247
248 return false;
249}
250
251static inline void __init *determine_relocation_address(void)
252{
253 /* Choose a new address for the kernel */
254 unsigned long kernel_length;
255 void *dest = &_text;
256 unsigned long offset;
257
258 if (kaslr_disabled())
259 return dest;
260
261 kernel_length = (long)_end - (long)(&_text);
262
263 offset = get_random_boot() << 16;
264 offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1);
265 if (offset < kernel_length)
266 offset += ALIGN(kernel_length, 0xffff);
267
268 return RELOCATED(dest);
269}
270
271#else
272
273static inline void __init *determine_relocation_address(void)
274{
275 /*
276 * Choose a new address for the kernel
277 * For now we'll hard code the destination
278 */
279 return (void *)0xffffffff81000000;
280}
281
282#endif
283
284static inline int __init relocation_addr_valid(void *loc_new)
285{
286 if ((unsigned long)loc_new & 0x0000ffff) {
287 /* Inappropriately aligned new location */
288 return 0;
289 }
290 if ((unsigned long)loc_new < (unsigned long)&_end) {
291 /* New location overlaps original kernel */
292 return 0;
293 }
294 return 1;
295}
296
297void *__init relocate_kernel(void)
298{
299 void *loc_new;
300 unsigned long kernel_length;
301 unsigned long bss_length;
302 long offset = 0;
303 int res = 1;
304 /* Default to original kernel entry point */
305 void *kernel_entry = start_kernel;
306 void *fdt = NULL;
307
308 /* Get the command line */
309 fw_init_cmdline();
310#if defined(CONFIG_USE_OF)
311 /* Deal with the device tree */
312 fdt = plat_get_fdt();
313 early_init_dt_scan(fdt);
314 if (boot_command_line[0]) {
315 /* Boot command line was passed in device tree */
316 strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE);
317 }
318#endif /* CONFIG_USE_OF */
319
320 kernel_length = (long)(&_relocation_start) - (long)(&_text);
321 bss_length = (long)&__bss_stop - (long)&__bss_start;
322
323 loc_new = determine_relocation_address();
324
325 /* Sanity check relocation address */
326 if (relocation_addr_valid(loc_new))
327 offset = (unsigned long)loc_new - (unsigned long)(&_text);
328
329 /* Reset the command line now so we don't end up with a duplicate */
330 arcs_cmdline[0] = '\0';
331
332 if (offset) {
333 void (*fdt_relocated_)(void *) = NULL;
334#if defined(CONFIG_USE_OF)
335 unsigned long fdt_phys = virt_to_phys(fdt);
336
337 /*
338 * If built-in dtb is used then it will have been relocated
339 * during kernel _text relocation. If appended DTB is used
340 * then it will not be relocated, but it should remain
341 * intact in the original location. If dtb is loaded by
342 * the bootloader then it may need to be moved if it crosses
343 * the target memory area
344 */
345
346 if (fdt_phys >= virt_to_phys(RELOCATED(&_text)) &&
347 fdt_phys <= virt_to_phys(RELOCATED(&_end))) {
348 void *fdt_relocated =
349 RELOCATED(ALIGN((long)&_end, PAGE_SIZE));
350 memcpy(fdt_relocated, fdt, fdt_totalsize(fdt));
351 fdt = fdt_relocated;
352 fdt_relocated_ = RELOCATED(&plat_fdt_relocated);
353 }
354#endif /* CONFIG_USE_OF */
355
356 /* Copy the kernel to it's new location */
357 memcpy(loc_new, &_text, kernel_length);
358
359 /* Perform relocations on the new kernel */
360 res = do_relocations(&_text, loc_new, offset);
361 if (res < 0)
362 goto out;
363
364 /* Sync the caches ready for execution of new kernel */
365 sync_icache(loc_new, kernel_length);
366
367 res = relocate_exception_table(offset);
368 if (res < 0)
369 goto out;
370
371 /*
372 * The original .bss has already been cleared, and
373 * some variables such as command line parameters
374 * stored to it so make a copy in the new location.
375 */
376 memcpy(RELOCATED(&__bss_start), &__bss_start, bss_length);
377
378 /*
379 * If fdt was stored outside of the kernel image and
380 * had to be moved then update platform's state data
381 * with the new fdt location
382 */
383 if (fdt_relocated_)
384 fdt_relocated_(fdt);
385
386 /*
387 * Last chance for the platform to abort relocation.
388 * This may also be used by the platform to perform any
389 * initialisation required now that the new kernel is
390 * resident in memory and ready to be executed.
391 */
392 if (plat_post_relocation(offset))
393 goto out;
394
395 /* The current thread is now within the relocated image */
396 __current_thread_info = RELOCATED(&init_thread_union);
397
398 /* Return the new kernel's entry point */
399 kernel_entry = RELOCATED(start_kernel);
400 }
401out:
402 return kernel_entry;
403}
404
405/*
406 * Show relocation information on panic.
407 */
408void show_kernel_relocation(const char *level)
409{
410 unsigned long offset;
411
412 offset = __pa_symbol(_text) - __pa_symbol(VMLINUX_LOAD_ADDRESS);
413
414 if (IS_ENABLED(CONFIG_RELOCATABLE) && offset > 0) {
415 printk(level);
416 pr_cont("Kernel relocated by 0x%pK\n", (void *)offset);
417 pr_cont(" .text @ 0x%pK\n", _text);
418 pr_cont(" .data @ 0x%pK\n", _sdata);
419 pr_cont(" .bss @ 0x%pK\n", __bss_start);
420 }
421}
422
423static int kernel_location_notifier_fn(struct notifier_block *self,
424 unsigned long v, void *p)
425{
426 show_kernel_relocation(KERN_EMERG);
427 return NOTIFY_DONE;
428}
429
430static struct notifier_block kernel_location_notifier = {
431 .notifier_call = kernel_location_notifier_fn
432};
433
434static int __init register_kernel_offset_dumper(void)
435{
436 atomic_notifier_chain_register(&panic_notifier_list,
437 &kernel_location_notifier);
438 return 0;
439}
440__initcall(register_kernel_offset_dumper);