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 #include <asm/sections.h> 25 #include <asm/trans_pgd.h> 26 27 #include "cpu-reset.h" 28 29 /** 30 * kexec_image_info - For debugging output. 31 */ 32 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i) 33 static void _kexec_image_info(const char *func, int line, 34 const struct kimage *kimage) 35 { 36 unsigned long i; 37 38 pr_debug("%s:%d:\n", func, line); 39 pr_debug(" kexec kimage info:\n"); 40 pr_debug(" type: %d\n", kimage->type); 41 pr_debug(" start: %lx\n", kimage->start); 42 pr_debug(" head: %lx\n", kimage->head); 43 pr_debug(" nr_segments: %lu\n", kimage->nr_segments); 44 pr_debug(" dtb_mem: %pa\n", &kimage->arch.dtb_mem); 45 pr_debug(" kern_reloc: %pa\n", &kimage->arch.kern_reloc); 46 pr_debug(" el2_vectors: %pa\n", &kimage->arch.el2_vectors); 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 /* Allocates pages for kexec page table */ 147 static void *kexec_page_alloc(void *arg) 148 { 149 struct kimage *kimage = (struct kimage *)arg; 150 struct page *page = kimage_alloc_control_pages(kimage, 0); 151 152 if (!page) 153 return NULL; 154 155 memset(page_address(page), 0, PAGE_SIZE); 156 157 return page_address(page); 158 } 159 160 int machine_kexec_post_load(struct kimage *kimage) 161 { 162 void *reloc_code = page_to_virt(kimage->control_code_page); 163 long reloc_size; 164 struct trans_pgd_info info = { 165 .trans_alloc_page = kexec_page_alloc, 166 .trans_alloc_arg = kimage, 167 }; 168 169 /* If in place, relocation is not used, only flush next kernel */ 170 if (kimage->head & IND_DONE) { 171 kexec_segment_flush(kimage); 172 kexec_image_info(kimage); 173 return 0; 174 } 175 176 kimage->arch.el2_vectors = 0; 177 if (is_hyp_nvhe()) { 178 int rc = trans_pgd_copy_el2_vectors(&info, 179 &kimage->arch.el2_vectors); 180 if (rc) 181 return rc; 182 } 183 184 reloc_size = __relocate_new_kernel_end - __relocate_new_kernel_start; 185 memcpy(reloc_code, __relocate_new_kernel_start, reloc_size); 186 kimage->arch.kern_reloc = __pa(reloc_code); 187 188 /* Flush the reloc_code in preparation for its execution. */ 189 dcache_clean_inval_poc((unsigned long)reloc_code, 190 (unsigned long)reloc_code + reloc_size); 191 icache_inval_pou((uintptr_t)reloc_code, 192 (uintptr_t)reloc_code + reloc_size); 193 kexec_list_flush(kimage); 194 kexec_image_info(kimage); 195 196 return 0; 197 } 198 199 /** 200 * machine_kexec - Do the kexec reboot. 201 * 202 * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC. 203 */ 204 void machine_kexec(struct kimage *kimage) 205 { 206 bool in_kexec_crash = (kimage == kexec_crash_image); 207 bool stuck_cpus = cpus_are_stuck_in_kernel(); 208 209 /* 210 * New cpus may have become stuck_in_kernel after we loaded the image. 211 */ 212 BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1))); 213 WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()), 214 "Some CPUs may be stale, kdump will be unreliable.\n"); 215 216 pr_info("Bye!\n"); 217 218 local_daif_mask(); 219 220 /* 221 * Both restart and cpu_soft_restart will shutdown the MMU, disable data 222 * caches. However, restart will start new kernel or purgatory directly, 223 * cpu_soft_restart will transfer control to arm64_relocate_new_kernel 224 * In kexec case, kimage->start points to purgatory assuming that 225 * kernel entry and dtb address are embedded in purgatory by 226 * userspace (kexec-tools). 227 * In kexec_file case, the kernel starts directly without purgatory. 228 */ 229 if (kimage->head & IND_DONE) { 230 typeof(__cpu_soft_restart) *restart; 231 232 cpu_install_idmap(); 233 restart = (void *)__pa_symbol(function_nocfi(__cpu_soft_restart)); 234 restart(is_hyp_nvhe(), kimage->start, kimage->arch.dtb_mem, 235 0, 0); 236 } else { 237 if (is_hyp_nvhe()) 238 __hyp_set_vectors(kimage->arch.el2_vectors); 239 cpu_soft_restart(kimage->arch.kern_reloc, 240 virt_to_phys(kimage), 0, 0); 241 } 242 243 BUG(); /* Should never get here. */ 244 } 245 246 static void machine_kexec_mask_interrupts(void) 247 { 248 unsigned int i; 249 struct irq_desc *desc; 250 251 for_each_irq_desc(i, desc) { 252 struct irq_chip *chip; 253 int ret; 254 255 chip = irq_desc_get_chip(desc); 256 if (!chip) 257 continue; 258 259 /* 260 * First try to remove the active state. If this 261 * fails, try to EOI the interrupt. 262 */ 263 ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false); 264 265 if (ret && irqd_irq_inprogress(&desc->irq_data) && 266 chip->irq_eoi) 267 chip->irq_eoi(&desc->irq_data); 268 269 if (chip->irq_mask) 270 chip->irq_mask(&desc->irq_data); 271 272 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data)) 273 chip->irq_disable(&desc->irq_data); 274 } 275 } 276 277 /** 278 * machine_crash_shutdown - shutdown non-crashing cpus and save registers 279 */ 280 void machine_crash_shutdown(struct pt_regs *regs) 281 { 282 local_irq_disable(); 283 284 /* shutdown non-crashing cpus */ 285 crash_smp_send_stop(); 286 287 /* for crashing cpu */ 288 crash_save_cpu(regs, smp_processor_id()); 289 machine_kexec_mask_interrupts(); 290 291 pr_info("Starting crashdump kernel...\n"); 292 } 293 294 void arch_kexec_protect_crashkres(void) 295 { 296 int i; 297 298 for (i = 0; i < kexec_crash_image->nr_segments; i++) 299 set_memory_valid( 300 __phys_to_virt(kexec_crash_image->segment[i].mem), 301 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0); 302 } 303 304 void arch_kexec_unprotect_crashkres(void) 305 { 306 int i; 307 308 for (i = 0; i < kexec_crash_image->nr_segments; i++) 309 set_memory_valid( 310 __phys_to_virt(kexec_crash_image->segment[i].mem), 311 kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1); 312 } 313 314 #ifdef CONFIG_HIBERNATION 315 /* 316 * To preserve the crash dump kernel image, the relevant memory segments 317 * should be mapped again around the hibernation. 318 */ 319 void crash_prepare_suspend(void) 320 { 321 if (kexec_crash_image) 322 arch_kexec_unprotect_crashkres(); 323 } 324 325 void crash_post_resume(void) 326 { 327 if (kexec_crash_image) 328 arch_kexec_protect_crashkres(); 329 } 330 331 /* 332 * crash_is_nosave 333 * 334 * Return true only if a page is part of reserved memory for crash dump kernel, 335 * but does not hold any data of loaded kernel image. 336 * 337 * Note that all the pages in crash dump kernel memory have been initially 338 * marked as Reserved as memory was allocated via memblock_reserve(). 339 * 340 * In hibernation, the pages which are Reserved and yet "nosave" are excluded 341 * from the hibernation iamge. crash_is_nosave() does thich check for crash 342 * dump kernel and will reduce the total size of hibernation image. 343 */ 344 345 bool crash_is_nosave(unsigned long pfn) 346 { 347 int i; 348 phys_addr_t addr; 349 350 if (!crashk_res.end) 351 return false; 352 353 /* in reserved memory? */ 354 addr = __pfn_to_phys(pfn); 355 if ((addr < crashk_res.start) || (crashk_res.end < addr)) 356 return false; 357 358 if (!kexec_crash_image) 359 return true; 360 361 /* not part of loaded kernel image? */ 362 for (i = 0; i < kexec_crash_image->nr_segments; i++) 363 if (addr >= kexec_crash_image->segment[i].mem && 364 addr < (kexec_crash_image->segment[i].mem + 365 kexec_crash_image->segment[i].memsz)) 366 return false; 367 368 return true; 369 } 370 371 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end) 372 { 373 unsigned long addr; 374 struct page *page; 375 376 for (addr = begin; addr < end; addr += PAGE_SIZE) { 377 page = phys_to_page(addr); 378 free_reserved_page(page); 379 } 380 } 381 #endif /* CONFIG_HIBERNATION */ 382