relocate.c 11 KB

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