Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Support for Kernel relocation at boot time
0007  *
0008  * Copyright (C) 2015, Imagination Technologies Ltd.
0009  * Authors: Matt Redfearn (matt.redfearn@mips.com)
0010  */
0011 #include <asm/bootinfo.h>
0012 #include <asm/cacheflush.h>
0013 #include <asm/fw/fw.h>
0014 #include <asm/sections.h>
0015 #include <asm/setup.h>
0016 #include <asm/timex.h>
0017 #include <linux/elf.h>
0018 #include <linux/kernel.h>
0019 #include <linux/libfdt.h>
0020 #include <linux/of_fdt.h>
0021 #include <linux/panic_notifier.h>
0022 #include <linux/sched/task.h>
0023 #include <linux/start_kernel.h>
0024 #include <linux/string.h>
0025 #include <linux/printk.h>
0026 
0027 #define RELOCATED(x) ((void *)((long)x + offset))
0028 
0029 extern u32 _relocation_start[]; /* End kernel image / start relocation table */
0030 extern u32 _relocation_end[];   /* End relocation table */
0031 
0032 extern long __start___ex_table; /* Start exception table */
0033 extern long __stop___ex_table;  /* End exception table */
0034 
0035 extern void __weak plat_fdt_relocated(void *new_location);
0036 
0037 /*
0038  * This function may be defined for a platform to perform any post-relocation
0039  * fixup necessary.
0040  * Return non-zero to abort relocation
0041  */
0042 int __weak plat_post_relocation(long offset)
0043 {
0044     return 0;
0045 }
0046 
0047 static inline u32 __init get_synci_step(void)
0048 {
0049     u32 res;
0050 
0051     __asm__("rdhwr  %0, $1" : "=r" (res));
0052 
0053     return res;
0054 }
0055 
0056 static void __init sync_icache(void *kbase, unsigned long kernel_length)
0057 {
0058     void *kend = kbase + kernel_length;
0059     u32 step = get_synci_step();
0060 
0061     do {
0062         __asm__ __volatile__(
0063             "synci  0(%0)"
0064             : /* no output */
0065             : "r" (kbase));
0066 
0067         kbase += step;
0068     } while (step && kbase < kend);
0069 
0070     /* Completion barrier */
0071     __sync();
0072 }
0073 
0074 static void __init apply_r_mips_64_rel(u32 *loc_new, long offset)
0075 {
0076     *(u64 *)loc_new += offset;
0077 }
0078 
0079 static void __init apply_r_mips_32_rel(u32 *loc_new, long offset)
0080 {
0081     *loc_new += offset;
0082 }
0083 
0084 static int __init apply_r_mips_26_rel(u32 *loc_orig, u32 *loc_new, long offset)
0085 {
0086     unsigned long target_addr = (*loc_orig) & 0x03ffffff;
0087 
0088     if (offset % 4) {
0089         pr_err("Dangerous R_MIPS_26 REL relocation\n");
0090         return -ENOEXEC;
0091     }
0092 
0093     /* Original target address */
0094     target_addr <<= 2;
0095     target_addr += (unsigned long)loc_orig & 0xf0000000;
0096 
0097     /* Get the new target address */
0098     target_addr += offset;
0099 
0100     if ((target_addr & 0xf0000000) != ((unsigned long)loc_new & 0xf0000000)) {
0101         pr_err("R_MIPS_26 REL relocation overflow\n");
0102         return -ENOEXEC;
0103     }
0104 
0105     target_addr -= (unsigned long)loc_new & 0xf0000000;
0106     target_addr >>= 2;
0107 
0108     *loc_new = (*loc_new & ~0x03ffffff) | (target_addr & 0x03ffffff);
0109 
0110     return 0;
0111 }
0112 
0113 
0114 static void __init apply_r_mips_hi16_rel(u32 *loc_orig, u32 *loc_new,
0115                      long offset)
0116 {
0117     unsigned long insn = *loc_orig;
0118     unsigned long target = (insn & 0xffff) << 16; /* high 16bits of target */
0119 
0120     target += offset;
0121 
0122     *loc_new = (insn & ~0xffff) | ((target >> 16) & 0xffff);
0123 }
0124 
0125 static int __init reloc_handler(u32 type, u32 *loc_orig, u32 *loc_new,
0126                 long offset)
0127 {
0128     switch (type) {
0129     case R_MIPS_64:
0130         apply_r_mips_64_rel(loc_new, offset);
0131         break;
0132     case R_MIPS_32:
0133         apply_r_mips_32_rel(loc_new, offset);
0134         break;
0135     case R_MIPS_26:
0136         return apply_r_mips_26_rel(loc_orig, loc_new, offset);
0137     case R_MIPS_HI16:
0138         apply_r_mips_hi16_rel(loc_orig, loc_new, offset);
0139         break;
0140     default:
0141         pr_err("Unhandled relocation type %d at 0x%pK\n", type,
0142                loc_orig);
0143         return -ENOEXEC;
0144     }
0145 
0146     return 0;
0147 }
0148 
0149 static int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
0150 {
0151     u32 *r;
0152     u32 *loc_orig;
0153     u32 *loc_new;
0154     int type;
0155     int res;
0156 
0157     for (r = _relocation_start; r < _relocation_end; r++) {
0158         /* Sentinel for last relocation */
0159         if (*r == 0)
0160             break;
0161 
0162         type = (*r >> 24) & 0xff;
0163         loc_orig = kbase_old + ((*r & 0x00ffffff) << 2);
0164         loc_new = RELOCATED(loc_orig);
0165 
0166         res = reloc_handler(type, loc_orig, loc_new, offset);
0167         if (res)
0168             return res;
0169     }
0170 
0171     return 0;
0172 }
0173 
0174 /*
0175  * The exception table is filled in by the relocs tool after vmlinux is linked.
0176  * It must be relocated separately since there will not be any relocation
0177  * information for it filled in by the linker.
0178  */
0179 static int __init relocate_exception_table(long offset)
0180 {
0181     unsigned long *etable_start, *etable_end, *e;
0182 
0183     etable_start = RELOCATED(&__start___ex_table);
0184     etable_end = RELOCATED(&__stop___ex_table);
0185 
0186     for (e = etable_start; e < etable_end; e++)
0187         *e += offset;
0188 
0189     return 0;
0190 }
0191 
0192 #ifdef CONFIG_RANDOMIZE_BASE
0193 
0194 static inline __init unsigned long rotate_xor(unsigned long hash,
0195                           const void *area, size_t size)
0196 {
0197     const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
0198     size_t diff, i;
0199 
0200     diff = (void *)ptr - area;
0201     if (unlikely(size < diff + sizeof(hash)))
0202         return hash;
0203 
0204     size = ALIGN_DOWN(size - diff, sizeof(hash));
0205 
0206     for (i = 0; i < size / sizeof(hash); i++) {
0207         /* Rotate by odd number of bits and XOR. */
0208         hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
0209         hash ^= ptr[i];
0210     }
0211 
0212     return hash;
0213 }
0214 
0215 static inline __init unsigned long get_random_boot(void)
0216 {
0217     unsigned long entropy = random_get_entropy();
0218     unsigned long hash = 0;
0219 
0220     /* Attempt to create a simple but unpredictable starting entropy. */
0221     hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
0222 
0223     /* Add in any runtime entropy we can get */
0224     hash = rotate_xor(hash, &entropy, sizeof(entropy));
0225 
0226 #if defined(CONFIG_USE_OF)
0227     /* Get any additional entropy passed in device tree */
0228     if (initial_boot_params) {
0229         int node, len;
0230         u64 *prop;
0231 
0232         node = fdt_path_offset(initial_boot_params, "/chosen");
0233         if (node >= 0) {
0234             prop = fdt_getprop_w(initial_boot_params, node,
0235                          "kaslr-seed", &len);
0236             if (prop && (len == sizeof(u64)))
0237                 hash = rotate_xor(hash, prop, sizeof(*prop));
0238         }
0239     }
0240 #endif /* CONFIG_USE_OF */
0241 
0242     return hash;
0243 }
0244 
0245 static inline __init bool kaslr_disabled(void)
0246 {
0247     char *str;
0248 
0249 #if defined(CONFIG_CMDLINE_BOOL)
0250     const char *builtin_cmdline = CONFIG_CMDLINE;
0251 
0252     str = strstr(builtin_cmdline, "nokaslr");
0253     if (str == builtin_cmdline ||
0254         (str > builtin_cmdline && *(str - 1) == ' '))
0255         return true;
0256 #endif
0257     str = strstr(arcs_cmdline, "nokaslr");
0258     if (str == arcs_cmdline || (str > arcs_cmdline && *(str - 1) == ' '))
0259         return true;
0260 
0261     return false;
0262 }
0263 
0264 static inline void __init *determine_relocation_address(void)
0265 {
0266     /* Choose a new address for the kernel */
0267     unsigned long kernel_length;
0268     void *dest = &_text;
0269     unsigned long offset;
0270 
0271     if (kaslr_disabled())
0272         return dest;
0273 
0274     kernel_length = (long)_end - (long)(&_text);
0275 
0276     offset = get_random_boot() << 16;
0277     offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1);
0278     if (offset < kernel_length)
0279         offset += ALIGN(kernel_length, 0xffff);
0280 
0281     return RELOCATED(dest);
0282 }
0283 
0284 #else
0285 
0286 static inline void __init *determine_relocation_address(void)
0287 {
0288     /*
0289      * Choose a new address for the kernel
0290      * For now we'll hard code the destination
0291      */
0292     return (void *)0xffffffff81000000;
0293 }
0294 
0295 #endif
0296 
0297 static inline int __init relocation_addr_valid(void *loc_new)
0298 {
0299     if ((unsigned long)loc_new & 0x0000ffff) {
0300         /* Inappropriately aligned new location */
0301         return 0;
0302     }
0303     if ((unsigned long)loc_new < (unsigned long)&_end) {
0304         /* New location overlaps original kernel */
0305         return 0;
0306     }
0307     return 1;
0308 }
0309 
0310 static inline void __init update_kaslr_offset(unsigned long *addr, long offset)
0311 {
0312     unsigned long *new_addr = (unsigned long *)RELOCATED(addr);
0313 
0314     *new_addr = (unsigned long)offset;
0315 }
0316 
0317 #if defined(CONFIG_USE_OF)
0318 void __weak *plat_get_fdt(void)
0319 {
0320     return NULL;
0321 }
0322 #endif
0323 
0324 void *__init relocate_kernel(void)
0325 {
0326     void *loc_new;
0327     unsigned long kernel_length;
0328     unsigned long bss_length;
0329     long offset = 0;
0330     int res = 1;
0331     /* Default to original kernel entry point */
0332     void *kernel_entry = start_kernel;
0333     void *fdt = NULL;
0334 
0335     /* Get the command line */
0336     fw_init_cmdline();
0337 #if defined(CONFIG_USE_OF)
0338     /* Deal with the device tree */
0339     fdt = plat_get_fdt();
0340     early_init_dt_scan(fdt);
0341     if (boot_command_line[0]) {
0342         /* Boot command line was passed in device tree */
0343         strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE);
0344     }
0345 #endif /* CONFIG_USE_OF */
0346 
0347     kernel_length = (long)(&_relocation_start) - (long)(&_text);
0348     bss_length = (long)&__bss_stop - (long)&__bss_start;
0349 
0350     loc_new = determine_relocation_address();
0351 
0352     /* Sanity check relocation address */
0353     if (relocation_addr_valid(loc_new))
0354         offset = (unsigned long)loc_new - (unsigned long)(&_text);
0355 
0356     /* Reset the command line now so we don't end up with a duplicate */
0357     arcs_cmdline[0] = '\0';
0358 
0359     if (offset) {
0360         void (*fdt_relocated_)(void *) = NULL;
0361 #if defined(CONFIG_USE_OF)
0362         unsigned long fdt_phys = virt_to_phys(fdt);
0363 
0364         /*
0365          * If built-in dtb is used then it will have been relocated
0366          * during kernel _text relocation. If appended DTB is used
0367          * then it will not be relocated, but it should remain
0368          * intact in the original location. If dtb is loaded by
0369          * the bootloader then it may need to be moved if it crosses
0370          * the target memory area
0371          */
0372 
0373         if (fdt_phys >= virt_to_phys(RELOCATED(&_text)) &&
0374             fdt_phys <= virt_to_phys(RELOCATED(&_end))) {
0375             void *fdt_relocated =
0376                 RELOCATED(ALIGN((long)&_end, PAGE_SIZE));
0377             memcpy(fdt_relocated, fdt, fdt_totalsize(fdt));
0378             fdt = fdt_relocated;
0379             fdt_relocated_ = RELOCATED(&plat_fdt_relocated);
0380         }
0381 #endif /* CONFIG_USE_OF */
0382 
0383         /* Copy the kernel to it's new location */
0384         memcpy(loc_new, &_text, kernel_length);
0385 
0386         /* Perform relocations on the new kernel */
0387         res = do_relocations(&_text, loc_new, offset);
0388         if (res < 0)
0389             goto out;
0390 
0391         /* Sync the caches ready for execution of new kernel */
0392         sync_icache(loc_new, kernel_length);
0393 
0394         res = relocate_exception_table(offset);
0395         if (res < 0)
0396             goto out;
0397 
0398         /*
0399          * The original .bss has already been cleared, and
0400          * some variables such as command line parameters
0401          * stored to it so make a copy in the new location.
0402          */
0403         memcpy(RELOCATED(&__bss_start), &__bss_start, bss_length);
0404 
0405         /*
0406          * If fdt was stored outside of the kernel image and
0407          * had to be moved then update platform's state data
0408          * with the new fdt location
0409          */
0410         if (fdt_relocated_)
0411             fdt_relocated_(fdt);
0412 
0413         /*
0414          * Last chance for the platform to abort relocation.
0415          * This may also be used by the platform to perform any
0416          * initialisation required now that the new kernel is
0417          * resident in memory and ready to be executed.
0418          */
0419         if (plat_post_relocation(offset))
0420             goto out;
0421 
0422         /* The current thread is now within the relocated image */
0423         __current_thread_info = RELOCATED(&init_thread_union);
0424 
0425         /* Return the new kernel's entry point */
0426         kernel_entry = RELOCATED(start_kernel);
0427 
0428         /* Error may occur before, so keep it at last */
0429         update_kaslr_offset(&__kaslr_offset, offset);
0430     }
0431 out:
0432     return kernel_entry;
0433 }
0434 
0435 /*
0436  * Show relocation information on panic.
0437  */
0438 static void show_kernel_relocation(const char *level)
0439 {
0440     if (__kaslr_offset > 0) {
0441         printk(level);
0442         pr_cont("Kernel relocated by 0x%pK\n", (void *)__kaslr_offset);
0443         pr_cont(" .text @ 0x%pK\n", _text);
0444         pr_cont(" .data @ 0x%pK\n", _sdata);
0445         pr_cont(" .bss  @ 0x%pK\n", __bss_start);
0446     }
0447 }
0448 
0449 static int kernel_location_notifier_fn(struct notifier_block *self,
0450                        unsigned long v, void *p)
0451 {
0452     show_kernel_relocation(KERN_EMERG);
0453     return NOTIFY_DONE;
0454 }
0455 
0456 static struct notifier_block kernel_location_notifier = {
0457     .notifier_call = kernel_location_notifier_fn
0458 };
0459 
0460 static int __init register_kernel_offset_dumper(void)
0461 {
0462     atomic_notifier_chain_register(&panic_notifier_list,
0463                        &kernel_location_notifier);
0464     return 0;
0465 }
0466 __initcall(register_kernel_offset_dumper);