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