relocate.c 9.1 KB

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