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