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 if (relocated_addr >= VMLINUX_LOAD_ADDRESS) 39 relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr); 40 41 *(Elf64_Addr *)RELOCATED(addr) = relocated_addr; 42 } 43 } 44 45 static inline void __init relocate_absolute(long random_offset) 46 { 47 void *begin, *end; 48 struct rela_la_abs *p; 49 50 begin = RELOCATED_KASLR(&__la_abs_begin); 51 end = RELOCATED_KASLR(&__la_abs_end); 52 53 for (p = begin; (void *)p < end; p++) { 54 long v = p->symvalue; 55 uint32_t lu12iw, ori, lu32id, lu52id; 56 union loongarch_instruction *insn = (void *)p->pc; 57 58 lu12iw = (v >> 12) & 0xfffff; 59 ori = v & 0xfff; 60 lu32id = (v >> 32) & 0xfffff; 61 lu52id = v >> 52; 62 63 insn[0].reg1i20_format.immediate = lu12iw; 64 insn[1].reg2i12_format.immediate = ori; 65 insn[2].reg1i20_format.immediate = lu32id; 66 insn[3].reg2i12_format.immediate = lu52id; 67 } 68 } 69 70 #ifdef CONFIG_RANDOMIZE_BASE 71 static inline __init unsigned long rotate_xor(unsigned long hash, 72 const void *area, size_t size) 73 { 74 size_t i, diff; 75 const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash)); 76 77 diff = (void *)ptr - area; 78 if (size < diff + sizeof(hash)) 79 return hash; 80 81 size = ALIGN_DOWN(size - diff, sizeof(hash)); 82 83 for (i = 0; i < size / sizeof(hash); i++) { 84 /* Rotate by odd number of bits and XOR. */ 85 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7); 86 hash ^= ptr[i]; 87 } 88 89 return hash; 90 } 91 92 static inline __init unsigned long get_random_boot(void) 93 { 94 unsigned long hash = 0; 95 unsigned long entropy = random_get_entropy(); 96 97 /* Attempt to create a simple but unpredictable starting entropy. */ 98 hash = rotate_xor(hash, linux_banner, strlen(linux_banner)); 99 100 /* Add in any runtime entropy we can get */ 101 hash = rotate_xor(hash, &entropy, sizeof(entropy)); 102 103 return hash; 104 } 105 106 static int __init nokaslr(char *p) 107 { 108 pr_info("KASLR is disabled.\n"); 109 110 return 0; /* Print a notice and silence the boot warning */ 111 } 112 early_param("nokaslr", nokaslr); 113 114 static inline __init bool kaslr_disabled(void) 115 { 116 char *str; 117 const char *builtin_cmdline = CONFIG_CMDLINE; 118 119 str = strstr(builtin_cmdline, "nokaslr"); 120 if (str == builtin_cmdline || (str > builtin_cmdline && *(str - 1) == ' ')) 121 return true; 122 123 str = strstr(boot_command_line, "nokaslr"); 124 if (str == boot_command_line || (str > boot_command_line && *(str - 1) == ' ')) 125 return true; 126 127 return false; 128 } 129 130 /* Choose a new address for the kernel */ 131 static inline void __init *determine_relocation_address(void) 132 { 133 unsigned long kernel_length; 134 unsigned long random_offset; 135 void *destination = _text; 136 137 if (kaslr_disabled()) 138 return destination; 139 140 kernel_length = (long)_end - (long)_text; 141 142 random_offset = get_random_boot() << 16; 143 random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1); 144 if (random_offset < kernel_length) 145 random_offset += ALIGN(kernel_length, 0xffff); 146 147 return RELOCATED_KASLR(destination); 148 } 149 150 static inline int __init relocation_addr_valid(void *location_new) 151 { 152 if ((unsigned long)location_new & 0x00000ffff) 153 return 0; /* Inappropriately aligned new location */ 154 155 if ((unsigned long)location_new < (unsigned long)_end) 156 return 0; /* New location overlaps original kernel */ 157 158 return 1; 159 } 160 #endif 161 162 static inline void __init update_reloc_offset(unsigned long *addr, long random_offset) 163 { 164 unsigned long *new_addr = (unsigned long *)RELOCATED_KASLR(addr); 165 166 *new_addr = (unsigned long)reloc_offset; 167 } 168 169 unsigned long __init relocate_kernel(void) 170 { 171 unsigned long kernel_length; 172 unsigned long random_offset = 0; 173 void *location_new = _text; /* Default to original kernel start */ 174 char *cmdline = early_memremap_ro(fw_arg1, COMMAND_LINE_SIZE); /* Boot command line is passed in fw_arg1 */ 175 176 strscpy(boot_command_line, cmdline, COMMAND_LINE_SIZE); 177 178 #ifdef CONFIG_RANDOMIZE_BASE 179 location_new = determine_relocation_address(); 180 181 /* Sanity check relocation address */ 182 if (relocation_addr_valid(location_new)) 183 random_offset = (unsigned long)location_new - (unsigned long)(_text); 184 #endif 185 reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS; 186 early_memunmap(cmdline, COMMAND_LINE_SIZE); 187 188 if (random_offset) { 189 kernel_length = (long)(_end) - (long)(_text); 190 191 /* Copy the kernel to it's new location */ 192 memcpy(location_new, _text, kernel_length); 193 194 /* Sync the caches ready for execution of new kernel */ 195 __asm__ __volatile__ ( 196 "ibar 0 \t\n" 197 "dbar 0 \t\n" 198 ::: "memory"); 199 200 reloc_offset += random_offset; 201 202 /* The current thread is now within the relocated kernel */ 203 __current_thread_info = RELOCATED_KASLR(__current_thread_info); 204 205 update_reloc_offset(&reloc_offset, random_offset); 206 } 207 208 if (reloc_offset) 209 relocate_relative(); 210 211 relocate_absolute(random_offset); 212 213 return random_offset; 214 } 215 216 /* 217 * Show relocation information on panic. 218 */ 219 static void show_kernel_relocation(const char *level) 220 { 221 if (reloc_offset > 0) { 222 printk(level); 223 pr_cont("Kernel relocated by 0x%lx\n", reloc_offset); 224 pr_cont(" .text @ 0x%px\n", _text); 225 pr_cont(" .data @ 0x%px\n", _sdata); 226 pr_cont(" .bss @ 0x%px\n", __bss_start); 227 } 228 } 229 230 static int kernel_location_notifier_fn(struct notifier_block *self, 231 unsigned long v, void *p) 232 { 233 show_kernel_relocation(KERN_EMERG); 234 return NOTIFY_DONE; 235 } 236 237 static struct notifier_block kernel_location_notifier = { 238 .notifier_call = kernel_location_notifier_fn 239 }; 240 241 static int __init register_kernel_offset_dumper(void) 242 { 243 atomic_notifier_chain_register(&panic_notifier_list, 244 &kernel_location_notifier); 245 return 0; 246 } 247 248 arch_initcall(register_kernel_offset_dumper); 249