relocate.c 11 KB

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