relocate.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for Kernel relocation at boot time
  4. *
  5. * Copyright (C) 2023 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/elf.h>
  8. #include <linux/kernel.h>
  9. #include <linux/printk.h>
  10. #include <linux/panic_notifier.h>
  11. #include <linux/start_kernel.h>
  12. #include <asm/bootinfo.h>
  13. #include <asm/early_ioremap.h>
  14. #include <asm/inst.h>
  15. #include <asm/io.h>
  16. #include <asm/sections.h>
  17. #include <asm/setup.h>
  18. #define RELOCATED(x) ((void *)((long)x + reloc_offset))
  19. #define RELOCATED_KASLR(x) ((void *)((long)x + random_offset))
  20. static unsigned long reloc_offset;
  21. static inline void __init relocate_relative(void)
  22. {
  23. Elf64_Rela *rela, *rela_end;
  24. rela = (Elf64_Rela *)&__rela_dyn_begin;
  25. rela_end = (Elf64_Rela *)&__rela_dyn_end;
  26. for ( ; rela < rela_end; rela++) {
  27. Elf64_Addr addr = rela->r_offset;
  28. Elf64_Addr relocated_addr = rela->r_addend;
  29. if (rela->r_info != R_LARCH_RELATIVE)
  30. continue;
  31. relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr);
  32. *(Elf64_Addr *)RELOCATED(addr) = relocated_addr;
  33. }
  34. #ifdef CONFIG_RELR
  35. u64 *addr = NULL;
  36. u64 *relr = (u64 *)&__relr_dyn_begin;
  37. u64 *relr_end = (u64 *)&__relr_dyn_end;
  38. for ( ; relr < relr_end; relr++) {
  39. if ((*relr & 1) == 0) {
  40. addr = (u64 *)(*relr + reloc_offset);
  41. *addr++ += reloc_offset;
  42. } else {
  43. for (u64 *p = addr, r = *relr >> 1; r; p++, r >>= 1)
  44. if (r & 1)
  45. *p += reloc_offset;
  46. addr += 63;
  47. }
  48. }
  49. #endif
  50. }
  51. static inline void __init relocate_absolute(long random_offset)
  52. {
  53. void *begin, *end;
  54. struct rela_la_abs *p;
  55. begin = RELOCATED_KASLR(&__la_abs_begin);
  56. end = RELOCATED_KASLR(&__la_abs_end);
  57. for (p = begin; (void *)p < end; p++) {
  58. long v = p->symvalue;
  59. uint32_t lu12iw, ori, lu32id, lu52id;
  60. union loongarch_instruction *insn = (void *)p->pc;
  61. lu12iw = (v >> 12) & 0xfffff;
  62. ori = v & 0xfff;
  63. lu32id = (v >> 32) & 0xfffff;
  64. lu52id = v >> 52;
  65. insn[0].reg1i20_format.immediate = lu12iw;
  66. insn[1].reg2i12_format.immediate = ori;
  67. insn[2].reg1i20_format.immediate = lu32id;
  68. insn[3].reg2i12_format.immediate = lu52id;
  69. }
  70. }
  71. #ifdef CONFIG_RANDOMIZE_BASE
  72. static inline __init unsigned long rotate_xor(unsigned long hash,
  73. const void *area, size_t size)
  74. {
  75. size_t i, diff;
  76. const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
  77. diff = (void *)ptr - area;
  78. if (size < diff + sizeof(hash))
  79. return hash;
  80. size = ALIGN_DOWN(size - diff, sizeof(hash));
  81. for (i = 0; i < size / sizeof(hash); i++) {
  82. /* Rotate by odd number of bits and XOR. */
  83. hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
  84. hash ^= ptr[i];
  85. }
  86. return hash;
  87. }
  88. static inline __init unsigned long get_random_boot(void)
  89. {
  90. unsigned long hash = 0;
  91. unsigned long entropy = random_get_entropy();
  92. /* Attempt to create a simple but unpredictable starting entropy. */
  93. hash = rotate_xor(hash, linux_banner, strlen(linux_banner));
  94. /* Add in any runtime entropy we can get */
  95. hash = rotate_xor(hash, &entropy, sizeof(entropy));
  96. return hash;
  97. }
  98. static int __init nokaslr(char *p)
  99. {
  100. pr_info("KASLR is disabled.\n");
  101. return 0; /* Print a notice and silence the boot warning */
  102. }
  103. early_param("nokaslr", nokaslr);
  104. static inline __init bool kaslr_disabled(void)
  105. {
  106. char *str;
  107. const char *builtin_cmdline = CONFIG_CMDLINE;
  108. str = strstr(builtin_cmdline, "nokaslr");
  109. if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
  110. return true;
  111. str = strstr(boot_command_line, "nokaslr");
  112. if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
  113. return true;
  114. #ifdef CONFIG_HIBERNATION
  115. str = strstr(builtin_cmdline, "nohibernate");
  116. if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
  117. return false;
  118. str = strstr(boot_command_line, "nohibernate");
  119. if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
  120. return false;
  121. str = strstr(builtin_cmdline, "noresume");
  122. if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
  123. return false;
  124. str = strstr(boot_command_line, "noresume");
  125. if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
  126. return false;
  127. str = strstr(builtin_cmdline, "resume=");
  128. if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' '))
  129. return true;
  130. str = strstr(boot_command_line, "resume=");
  131. if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' '))
  132. return true;
  133. #endif
  134. return false;
  135. }
  136. /* Choose a new address for the kernel */
  137. static inline void __init *determine_relocation_address(void)
  138. {
  139. unsigned long kernel_length;
  140. unsigned long random_offset;
  141. void *destination = _text;
  142. if (kaslr_disabled())
  143. return destination;
  144. kernel_length = (long)_end - (long)_text;
  145. random_offset = get_random_boot() << 16;
  146. random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1);
  147. if (random_offset < kernel_length)
  148. random_offset += ALIGN(kernel_length, 0xffff);
  149. return RELOCATED_KASLR(destination);
  150. }
  151. static inline int __init relocation_addr_valid(void *location_new)
  152. {
  153. if ((unsigned long)location_new & 0x00000ffff)
  154. return 0; /* Inappropriately aligned new location */
  155. if ((unsigned long)location_new < (unsigned long)_end)
  156. return 0; /* New location overlaps original kernel */
  157. return 1;
  158. }
  159. #endif
  160. static inline void __init update_reloc_offset(unsigned long *addr, long random_offset)
  161. {
  162. unsigned long *new_addr = (unsigned long *)RELOCATED_KASLR(addr);
  163. *new_addr = (unsigned long)reloc_offset;
  164. }
  165. unsigned long __init relocate_kernel(void)
  166. {
  167. unsigned long kernel_length;
  168. unsigned long random_offset = 0;
  169. void *location_new = _text; /* Default to original kernel start */
  170. char *cmdline = early_memremap_ro(fw_arg1, COMMAND_LINE_SIZE); /* Boot command line is passed in fw_arg1 */
  171. strscpy(boot_command_line, cmdline, COMMAND_LINE_SIZE);
  172. #ifdef CONFIG_RANDOMIZE_BASE
  173. location_new = determine_relocation_address();
  174. /* Sanity check relocation address */
  175. if (relocation_addr_valid(location_new))
  176. random_offset = (unsigned long)location_new - (unsigned long)(_text);
  177. #endif
  178. reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS;
  179. early_memunmap(cmdline, COMMAND_LINE_SIZE);
  180. if (random_offset) {
  181. kernel_length = (long)(_end) - (long)(_text);
  182. /* Copy the kernel to it's new location */
  183. memcpy(location_new, _text, kernel_length);
  184. /* Sync the caches ready for execution of new kernel */
  185. __asm__ __volatile__ (
  186. "ibar 0 \t\n"
  187. "dbar 0 \t\n"
  188. ::: "memory");
  189. reloc_offset += random_offset;
  190. /* The current thread is now within the relocated kernel */
  191. __current_thread_info = RELOCATED_KASLR(__current_thread_info);
  192. update_reloc_offset(&reloc_offset, random_offset);
  193. }
  194. if (reloc_offset)
  195. relocate_relative();
  196. relocate_absolute(random_offset);
  197. return random_offset;
  198. }
  199. /*
  200. * Show relocation information on panic.
  201. */
  202. static void show_kernel_relocation(const char *level)
  203. {
  204. if (reloc_offset > 0) {
  205. printk(level);
  206. pr_cont("Kernel relocated by 0x%lx\n", reloc_offset);
  207. pr_cont(" .text @ 0x%px\n", _text);
  208. pr_cont(" .data @ 0x%px\n", _sdata);
  209. pr_cont(" .bss @ 0x%px\n", __bss_start);
  210. }
  211. }
  212. static int kernel_location_notifier_fn(struct notifier_block *self,
  213. unsigned long v, void *p)
  214. {
  215. show_kernel_relocation(KERN_EMERG);
  216. return NOTIFY_DONE;
  217. }
  218. static struct notifier_block kernel_location_notifier = {
  219. .notifier_call = kernel_location_notifier_fn
  220. };
  221. static int __init register_kernel_offset_dumper(void)
  222. {
  223. atomic_notifier_chain_register(&panic_notifier_list,
  224. &kernel_location_notifier);
  225. return 0;
  226. }
  227. arch_initcall(register_kernel_offset_dumper);