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