1
2
3
4
5
6
7
8
9
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[];
29extern u32 _relocation_end[];
30
31extern long __start___ex_table;
32extern long __stop___ex_table;
33
34extern void __weak plat_fdt_relocated(void *new_location);
35
36
37
38
39
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 :
64 : "r" (kbase));
65
66 kbase += step;
67 } while (step && kbase < kend);
68
69
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
97 target_addr <<= 2;
98 target_addr += (unsigned long)loc_orig & 0xf0000000;
99
100
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 & 0xf0000000;
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;
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
145 if (*r == 0)
146 break;
147
148 type = (*r >> 24) & 0xff;
149 loc_orig = kbase_old + ((*r & 0x00ffffff) << 2);
150 loc_new = RELOCATED(loc_orig);
151
152 if (reloc_handlers_rel[type] == NULL) {
153
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
169
170
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 const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
191 size_t diff, i;
192
193 diff = (void *)ptr - area;
194 if (unlikely(size < diff + sizeof(hash)))
195 return hash;
196
197 size = ALIGN_DOWN(size - diff, sizeof(hash));
198
199 for (i = 0; i < size / sizeof(hash); i++) {
200
201 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
202 hash ^= ptr[i];
203 }
204
205 return hash;
206}
207
208static inline __init unsigned long get_random_boot(void)
209{
210 unsigned long entropy = random_get_entropy();
211 unsigned long hash = 0;
212
213
214 hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
215
216
217 hash = rotate_xor(hash, &entropy, sizeof(entropy));
218
219#if defined(CONFIG_USE_OF)
220
221 if (initial_boot_params) {
222 int node, len;
223 u64 *prop;
224
225 node = fdt_path_offset(initial_boot_params, "/chosen");
226 if (node >= 0) {
227 prop = fdt_getprop_w(initial_boot_params, node,
228 "kaslr-seed", &len);
229 if (prop && (len == sizeof(u64)))
230 hash = rotate_xor(hash, prop, sizeof(*prop));
231 }
232 }
233#endif
234
235 return hash;
236}
237
238static inline __init bool kaslr_disabled(void)
239{
240 char *str;
241
242#if defined(CONFIG_CMDLINE_BOOL)
243 const char *builtin_cmdline = CONFIG_CMDLINE;
244
245 str = strstr(builtin_cmdline, "nokaslr");
246 if (str == builtin_cmdline ||
247 (str > builtin_cmdline && *(str - 1) == ' '))
248 return true;
249#endif
250 str = strstr(arcs_cmdline, "nokaslr");
251 if (str == arcs_cmdline || (str > arcs_cmdline && *(str - 1) == ' '))
252 return true;
253
254 return false;
255}
256
257static inline void __init *determine_relocation_address(void)
258{
259
260 unsigned long kernel_length;
261 void *dest = &_text;
262 unsigned long offset;
263
264 if (kaslr_disabled())
265 return dest;
266
267 kernel_length = (long)_end - (long)(&_text);
268
269 offset = get_random_boot() << 16;
270 offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1);
271 if (offset < kernel_length)
272 offset += ALIGN(kernel_length, 0xffff);
273
274 return RELOCATED(dest);
275}
276
277#else
278
279static inline void __init *determine_relocation_address(void)
280{
281
282
283
284
285 return (void *)0xffffffff81000000;
286}
287
288#endif
289
290static inline int __init relocation_addr_valid(void *loc_new)
291{
292 if ((unsigned long)loc_new & 0x0000ffff) {
293
294 return 0;
295 }
296 if ((unsigned long)loc_new < (unsigned long)&_end) {
297
298 return 0;
299 }
300 return 1;
301}
302
303#if defined(CONFIG_USE_OF)
304void __weak *plat_get_fdt(void)
305{
306 return NULL;
307}
308#endif
309
310void *__init relocate_kernel(void)
311{
312 void *loc_new;
313 unsigned long kernel_length;
314 unsigned long bss_length;
315 long offset = 0;
316 int res = 1;
317
318 void *kernel_entry = start_kernel;
319 void *fdt = NULL;
320
321
322 fw_init_cmdline();
323#if defined(CONFIG_USE_OF)
324
325 fdt = plat_get_fdt();
326 early_init_dt_scan(fdt);
327 if (boot_command_line[0]) {
328
329 strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE);
330 }
331#endif
332
333 kernel_length = (long)(&_relocation_start) - (long)(&_text);
334 bss_length = (long)&__bss_stop - (long)&__bss_start;
335
336 loc_new = determine_relocation_address();
337
338
339 if (relocation_addr_valid(loc_new))
340 offset = (unsigned long)loc_new - (unsigned long)(&_text);
341
342
343 arcs_cmdline[0] = '\0';
344
345 if (offset) {
346 void (*fdt_relocated_)(void *) = NULL;
347#if defined(CONFIG_USE_OF)
348 unsigned long fdt_phys = virt_to_phys(fdt);
349
350
351
352
353
354
355
356
357
358
359 if (fdt_phys >= virt_to_phys(RELOCATED(&_text)) &&
360 fdt_phys <= virt_to_phys(RELOCATED(&_end))) {
361 void *fdt_relocated =
362 RELOCATED(ALIGN((long)&_end, PAGE_SIZE));
363 memcpy(fdt_relocated, fdt, fdt_totalsize(fdt));
364 fdt = fdt_relocated;
365 fdt_relocated_ = RELOCATED(&plat_fdt_relocated);
366 }
367#endif
368
369
370 memcpy(loc_new, &_text, kernel_length);
371
372
373 res = do_relocations(&_text, loc_new, offset);
374 if (res < 0)
375 goto out;
376
377
378 sync_icache(loc_new, kernel_length);
379
380 res = relocate_exception_table(offset);
381 if (res < 0)
382 goto out;
383
384
385
386
387
388
389 memcpy(RELOCATED(&__bss_start), &__bss_start, bss_length);
390
391
392
393
394
395
396 if (fdt_relocated_)
397 fdt_relocated_(fdt);
398
399
400
401
402
403
404
405 if (plat_post_relocation(offset))
406 goto out;
407
408
409 __current_thread_info = RELOCATED(&init_thread_union);
410
411
412 kernel_entry = RELOCATED(start_kernel);
413 }
414out:
415 return kernel_entry;
416}
417
418
419
420
421void show_kernel_relocation(const char *level)
422{
423 unsigned long offset;
424
425 offset = __pa_symbol(_text) - __pa_symbol(VMLINUX_LOAD_ADDRESS);
426
427 if (IS_ENABLED(CONFIG_RELOCATABLE) && offset > 0) {
428 printk(level);
429 pr_cont("Kernel relocated by 0x%pK\n", (void *)offset);
430 pr_cont(" .text @ 0x%pK\n", _text);
431 pr_cont(" .data @ 0x%pK\n", _sdata);
432 pr_cont(" .bss @ 0x%pK\n", __bss_start);
433 }
434}
435
436static int kernel_location_notifier_fn(struct notifier_block *self,
437 unsigned long v, void *p)
438{
439 show_kernel_relocation(KERN_EMERG);
440 return NOTIFY_DONE;
441}
442
443static struct notifier_block kernel_location_notifier = {
444 .notifier_call = kernel_location_notifier_fn
445};
446
447static int __init register_kernel_offset_dumper(void)
448{
449 atomic_notifier_chain_register(&panic_notifier_list,
450 &kernel_location_notifier);
451 return 0;
452}
453__initcall(register_kernel_offset_dumper);
454