1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kexec for arm64 4 * 5 * Copyright (C) Linaro. 6 * Copyright (C) Huawei Futurewei Technologies. 7 */ 8 9 #include <linux/interrupt.h> 10 #include <linux/irq.h> 11 #include <linux/kernel.h> 12 #include <linux/kexec.h> 13 #include <linux/page-flags.h> 14 #include <linux/set_memory.h> 15 #include <linux/smp.h> 16 17 #include <asm/cacheflush.h> 18 #include <asm/cpu_ops.h> 19 #include <asm/daifflags.h> 20 #include <asm/memory.h> 21 #include <asm/mmu.h> 22 #include <asm/mmu_context.h> 23 #include <asm/page.h> 24 25 #include "cpu-reset.h" 26 27 /* Global variables for the arm64_relocate_new_kernel routine. */ 28 extern const unsigned char arm64_relocate_new_kernel[]; 29 extern const unsigned long arm64_relocate_new_kernel_size; 30 31 /** 32 * kexec_image_info - For debugging output. 33 */ 34 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i) 35 static void _kexec_image_info(const char *func, int line, 36 const struct kimage *kimage) 37 { 38 unsigned long i; 39 40 pr_debug("%s:%d:\n", func, line); 41 pr_debug(" kexec kimage info:\n"); 42 pr_debug(" type: %d\n", kimage->type); 43 pr_debug(" start: %lx\n", kimage->start); 44 pr_debug(" head: %lx\n", kimage->head); 45 pr_debug(" nr_segments: %lu\n", kimage->nr_segments); 46 pr_debug(" kern_reloc: %pa\n", &kimage->arch.kern_reloc); 47 48 for (i = 0; i < kimage->nr_segments; i++) { 49 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n", 50 i, 51 kimage->segment[i].mem, 52 kimage->segment[i].mem + kimage->segment[i].memsz, 53 kimage->segment[i].memsz, 54 kimage->segment[i].memsz / PAGE_SIZE); 55 } 56 } 57 58 void machine_kexec_cleanup(struct kimage *kimage) 59 { 60 /* Empty routine needed to avoid build errors. */ 61 } 62 63 /** 64 * machine_kexec_prepare - Prepare for a kexec reboot. 65 * 66 * Called from the core kexec code when a kernel image is loaded. 67 * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus 68 * are stuck in the kernel. This avoids a panic once we hit machine_kexec(). 69 */ 70 int machine_kexec_prepare(struct kimage *kimage) 71 { 72 if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) { 73 pr_err("Can't kexec: CPUs are stuck in the kernel.\n"); 74 return -EBUSY; 75 } 76 77 return 0; 78 } 79 80 /** 81 * kexec_list_flush - Helper to flush the kimage list and source pages to PoC. 82 */ 83 static void kexec_list_flush(struct kimage *kimage) 84 { 85 kimage_entry_t *entry; 86 87 dcache_clean_inval_poc((unsigned long)kimage, 88 (unsigned long)kimage + sizeof(*kimage)); 89 90 for (entry = &kimage->head; ; entry++) { 91 unsigned int flag; 92 unsigned long addr; 93 94 /* flush the list entries. */ 95 dcache_clean_inval_poc((unsigned long)entry, 96 (unsigned long)entry + 97 sizeof(kimage_entry_t)); 98 99 flag = *entry & IND_FLAGS; 100 if (flag == IND_DONE) 101 break; 102 103 addr = (unsigned long)phys_to_virt(*entry & PAGE_MASK); 104 105 switch (flag) { 106 case IND_INDIRECTION: 107 /* Set entry point just before the new list page. */ 108 entry = (kimage_entry_t *)addr - 1; 109 break; 110 case IND_SOURCE: 111 /* flush the source pages. */ 112 dcache_clean_inval_poc(addr, addr + PAGE_SIZE); 113 break; 114 case IND_DESTINATION: 115 break; 116 default: 117 BUG(); 118 } 119 } 120 } 121 122 /** 123 * kexec_segment_flush - Helper to flush the kimage segments to PoC. 124 */ 125 static void kexec_segment_flush(const struct kimage *kimage) 126 { 127 unsigned long i; 128 129 pr_debug("%s:\n", __func__); 130 131 for (i = 0; i < kimage->nr_segments; i++) { 132 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n", 133 i, 134 kimage->segment[i].mem, 135 kimage->segment[i].mem + kimage->segment[i].memsz, 136 kimage->segment[i].memsz, 137 kimage->segment[i].memsz / PAGE_SIZE); 138 139 dcache_clean_inval_poc( 140 (unsigned long)phys_to_virt(kimage->segment[i].mem), 141 (unsigned long)phys_to_virt(kimage->segment[i].mem) + 142 kimage->segment[i].memsz); 143 } 144 } 145 146 int machine_kexec_post_load(struct kimage *kimage) 147 { 148 void *reloc_code = page_to_virt(kimage->control_code_page); 149 150 /* If in place, relocation is not used, only flush next kernel */ 151 if (kimage->head & IND_DONE) { 152 kexec_segment_flush(kimage); 153 kexec_image_info(kimage); 154 return 0; 155 } 156 157 memcpy(reloc_code, arm64_relocate_new_kernel, 158 arm64_relocate_new_kernel_size); 159 kimage->arch.kern_reloc = __pa(reloc_code); 160 161 /* Flush the reloc_code in preparation for its execution. */ 162 dcache_clean_inval_poc((unsigned long)reloc_code, 163 (unsigned long)reloc_code + 164 arm64_relocate_new_kernel_size); 165 icache_inval_pou((uintptr_t)reloc_code, 166 (uintptr_t)reloc_code + 167 arm64_relocate_new_kernel_size); 168 kexec_list_flush(kimage); 169 kexec_image_info(kimage); 170 171 return 0; 172 } 173 174 /** 175 * machine_kexec - Do the kexec reboot. 176 * 177 * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC. 178 */ 179 void machine_kexec(struct kimage *kimage) 180 { 181 bool in_kexec_crash = (kimage == kexec_crash_image); 182 bool stuck_cpus = cpus_are_stuck_in_kernel(); 183 184 /* 185 * New cpus may have become stuck_in_kernel after we loaded the image. 186 */ 187 BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1))); 188 WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()), 189 "Some CPUs may be stale, kdump will be unreliable.\n"); 190 191 pr_info("Bye!\n"); 192 193 local_daif_mask(); 194 195 /* 196 * Both restart and cpu_soft_restart will shutdown the MMU, disable data 197 * caches. However, restart will start new kernel or purgatory directly, 198 * cpu_soft_restart will transfer control to arm64_relocate_new_kernel 199 * In kexec case, kimage->start points to purgatory assuming that 200 * kernel entry and dtb address are embedded in purgatory by 201 * userspace (kexec-tools). 202 * In kexec_file case, the kernel starts directly without purgatory. 203 */ 204 if (kimage->head & IND_DONE) { 205 typeof(__cpu_soft_restart) *restart; 206 207 cpu_install_idmap(); 208 restart = (void *)__pa_symbol(function_nocfi(__cpu_soft_restart)); 209 restart(is_hyp_nvhe(), kimage->start, kimage->arch.dtb_mem, 210 0, 0); 211 } else { 212 cpu_soft_restart(kimage->arch.kern_reloc, virt_to_phys(kimage), 213 0, 0); 214 } 215 216 BUG(); /* Should never get here. */ 217 } 218 219 static void machine_kexec_mask_interrupts(void) 220 { 221 unsigned int i; 222 struct irq_desc *desc; 223 224 for_each_irq_desc(i, desc) { 225 struct irq_chip *chip; 226 int ret; 227 228 chip = irq_desc_get_chip(desc); 229 if (!chip) 230 continue; 231 232 /* 233 * First try to remove the active state. If this 234 * fails, try to EOI the interrupt. 235 */ 236 ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false); 237 238 if (ret && irqd_irq_inprogress(&desc->irq_data) && 239 chip->irq_eoi) 240 chip->irq_eoi(&desc->irq_data); 241 242 if (chip->irq_mask) 243 chip->irq_mask(&desc->irq_data); 244 245 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data)) 246 chip->irq_disable(&desc->irq_data); 247 } 248 } 249 250 /** 251 * machine_crash_shutdown - shutdown non-crashing cpus and save registers 252 */ 253 void machine_crash_shutdown(struct pt_regs *regs) 254 { 255 local_irq_disable(); 256 257 /* shutdown non-crashing cpus */ 258 crash_smp_send_stop(); 259 260 /* for crashing cpu */ 261 crash_save_cpu(regs, smp_processor_id()); 262 machine_kexec_mask_interrupts(); 263 264 pr_info("Starting crashdump kernel...\n"); 265 } 266 267 void arch_kexec_protect_crashkres(void) 268 { 269 int i; 270 271 for (i = 0; i < kexec_crash_image->nr_segments; i++) 272 set_memory_valid( 273 __phys_to_virt(kexec_crash_image->segment[i].mem), 274 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0); 275 } 276 277 void arch_kexec_unprotect_crashkres(void) 278 { 279 int i; 280 281 for (i = 0; i < kexec_crash_image->nr_segments; i++) 282 set_memory_valid( 283 __phys_to_virt(kexec_crash_image->segment[i].mem), 284 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1); 285 } 286 287 #ifdef CONFIG_HIBERNATION 288 /* 289 * To preserve the crash dump kernel image, the relevant memory segments 290 * should be mapped again around the hibernation. 291 */ 292 void crash_prepare_suspend(void) 293 { 294 if (kexec_crash_image) 295 arch_kexec_unprotect_crashkres(); 296 } 297 298 void crash_post_resume(void) 299 { 300 if (kexec_crash_image) 301 arch_kexec_protect_crashkres(); 302 } 303 304 /* 305 * crash_is_nosave 306 * 307 * Return true only if a page is part of reserved memory for crash dump kernel, 308 * but does not hold any data of loaded kernel image. 309 * 310 * Note that all the pages in crash dump kernel memory have been initially 311 * marked as Reserved as memory was allocated via memblock_reserve(). 312 * 313 * In hibernation, the pages which are Reserved and yet "nosave" are excluded 314 * from the hibernation iamge. crash_is_nosave() does thich check for crash 315 * dump kernel and will reduce the total size of hibernation image. 316 */ 317 318 bool crash_is_nosave(unsigned long pfn) 319 { 320 int i; 321 phys_addr_t addr; 322 323 if (!crashk_res.end) 324 return false; 325 326 /* in reserved memory? */ 327 addr = __pfn_to_phys(pfn); 328 if ((addr < crashk_res.start) || (crashk_res.end < addr)) 329 return false; 330 331 if (!kexec_crash_image) 332 return true; 333 334 /* not part of loaded kernel image? */ 335 for (i = 0; i < kexec_crash_image->nr_segments; i++) 336 if (addr >= kexec_crash_image->segment[i].mem && 337 addr < (kexec_crash_image->segment[i].mem + 338 kexec_crash_image->segment[i].memsz)) 339 return false; 340 341 return true; 342 } 343 344 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end) 345 { 346 unsigned long addr; 347 struct page *page; 348 349 for (addr = begin; addr < end; addr += PAGE_SIZE) { 350 page = phys_to_page(addr); 351 free_reserved_page(page); 352 } 353 } 354 #endif /* CONFIG_HIBERNATION */ 355