1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Architecture specific (i386/x86_64) functions for kexec based crash dumps. 4 * 5 * Created by: Hariprasad Nellitheertha (hari@in.ibm.com) 6 * 7 * Copyright (C) IBM Corporation, 2004. All rights reserved. 8 * Copyright (C) Red Hat Inc., 2014. All rights reserved. 9 * Authors: 10 * Vivek Goyal <vgoyal@redhat.com> 11 * 12 */ 13 14 #define pr_fmt(fmt) "kexec: " fmt 15 16 #include <linux/types.h> 17 #include <linux/kernel.h> 18 #include <linux/smp.h> 19 #include <linux/reboot.h> 20 #include <linux/kexec.h> 21 #include <linux/delay.h> 22 #include <linux/elf.h> 23 #include <linux/elfcore.h> 24 #include <linux/export.h> 25 #include <linux/slab.h> 26 #include <linux/vmalloc.h> 27 #include <linux/memblock.h> 28 29 #include <asm/bootparam.h> 30 #include <asm/processor.h> 31 #include <asm/hardirq.h> 32 #include <asm/nmi.h> 33 #include <asm/hw_irq.h> 34 #include <asm/apic.h> 35 #include <asm/e820/types.h> 36 #include <asm/io_apic.h> 37 #include <asm/hpet.h> 38 #include <linux/kdebug.h> 39 #include <asm/cpu.h> 40 #include <asm/reboot.h> 41 #include <asm/tdx.h> 42 #include <asm/intel_pt.h> 43 #include <asm/crash.h> 44 #include <asm/cmdline.h> 45 #include <asm/sev.h> 46 #include <asm/virt.h> 47 48 /* Used while preparing memory map entries for second kernel */ 49 struct crash_memmap_data { 50 struct boot_params *params; 51 /* Type of memory */ 52 unsigned int type; 53 }; 54 55 #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC) 56 57 static void kdump_nmi_callback(int cpu, struct pt_regs *regs) 58 { 59 crash_save_cpu(regs, cpu); 60 61 /* 62 * Disable Intel PT to stop its logging 63 */ 64 cpu_emergency_stop_pt(); 65 66 kdump_sev_callback(); 67 68 disable_local_APIC(); 69 } 70 71 void kdump_nmi_shootdown_cpus(void) 72 { 73 nmi_shootdown_cpus(kdump_nmi_callback); 74 75 disable_local_APIC(); 76 } 77 78 /* Override the weak function in kernel/panic.c */ 79 void crash_smp_send_stop(void) 80 { 81 static int cpus_stopped; 82 83 if (cpus_stopped) 84 return; 85 86 if (smp_ops.crash_stop_other_cpus) 87 smp_ops.crash_stop_other_cpus(); 88 else 89 smp_send_stop(); 90 91 cpus_stopped = 1; 92 } 93 94 #else 95 void crash_smp_send_stop(void) 96 { 97 /* There are no cpus to shootdown */ 98 } 99 #endif 100 101 void native_machine_crash_shutdown(struct pt_regs *regs) 102 { 103 /* This function is only called after the system 104 * has panicked or is otherwise in a critical state. 105 * The minimum amount of code to allow a kexec'd kernel 106 * to run successfully needs to happen here. 107 * 108 * In practice this means shooting down the other cpus in 109 * an SMP system. 110 */ 111 /* The kernel is broken so disable interrupts */ 112 local_irq_disable(); 113 114 crash_smp_send_stop(); 115 116 tdx_sys_disable(); 117 x86_virt_emergency_disable_virtualization_cpu(); 118 119 /* 120 * Disable Intel PT to stop its logging 121 */ 122 cpu_emergency_stop_pt(); 123 124 #ifdef CONFIG_X86_IO_APIC 125 /* Prevent crash_kexec() from deadlocking on ioapic_lock. */ 126 ioapic_zap_locks(); 127 clear_IO_APIC(); 128 #endif 129 lapic_shutdown(); 130 restore_boot_irq_mode(); 131 #ifdef CONFIG_HPET_TIMER 132 hpet_disable(); 133 #endif 134 135 /* 136 * Non-crash kexec calls enc_kexec_begin() while scheduling is still 137 * active. This allows the callback to wait until all in-flight 138 * shared<->private conversions are complete. In a crash scenario, 139 * enc_kexec_begin() gets called after all but one CPU have been shut 140 * down and interrupts have been disabled. This allows the callback to 141 * detect a race with the conversion and report it. 142 */ 143 x86_platform.guest.enc_kexec_begin(); 144 x86_platform.guest.enc_kexec_finish(); 145 146 crash_save_cpu(regs, smp_processor_id()); 147 } 148 149 #if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_HOTPLUG) 150 static int get_nr_ram_ranges_callback(struct resource *res, void *arg) 151 { 152 unsigned int *nr_ranges = arg; 153 154 (*nr_ranges)++; 155 return 0; 156 } 157 158 unsigned int arch_get_system_nr_ranges(void) 159 { 160 /* 161 * Exclusion of crash region, crashk_low_res and/or crashk_cma_ranges 162 * may cause range splits. So add extra slots here. 163 * 164 * Exclusion of low 1M may not cause another range split, because the 165 * range of exclude is [0, 1M] and the condition for splitting a new 166 * region is that the start, end parameters are both in a certain 167 * existing region in cmem and cannot be equal to existing region's 168 * start or end. Obviously, the start of [0, 1M] cannot meet this 169 * condition. 170 * 171 * But in order to lest the low 1M could be changed in the future, 172 * (e.g. [start, 1M]), add a extra slot. 173 */ 174 unsigned int nr_ranges = 3 + crashk_cma_cnt; 175 176 walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback); 177 return nr_ranges; 178 } 179 180 int arch_crash_exclude_ranges(struct crash_mem *cmem) 181 { 182 /* Exclude the low 1M because it is always reserved */ 183 return crash_exclude_mem_range(cmem, 0, SZ_1M - 1); 184 } 185 186 static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) 187 { 188 struct crash_mem *cmem = arg; 189 190 cmem->ranges[cmem->nr_ranges].start = res->start; 191 cmem->ranges[cmem->nr_ranges].end = res->end; 192 cmem->nr_ranges++; 193 194 return 0; 195 } 196 197 int arch_crash_populate_cmem(struct crash_mem *cmem) 198 { 199 return walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback); 200 } 201 #endif 202 203 #ifdef CONFIG_KEXEC_FILE 204 static int add_e820_entry(struct boot_params *params, struct e820_entry *entry) 205 { 206 unsigned int nr_e820_entries; 207 208 nr_e820_entries = params->e820_entries; 209 if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE) 210 return 1; 211 212 memcpy(¶ms->e820_table[nr_e820_entries], entry, sizeof(struct e820_entry)); 213 params->e820_entries++; 214 return 0; 215 } 216 217 static int memmap_entry_callback(struct resource *res, void *arg) 218 { 219 struct crash_memmap_data *cmd = arg; 220 struct boot_params *params = cmd->params; 221 struct e820_entry ei; 222 223 ei.addr = res->start; 224 ei.size = resource_size(res); 225 ei.type = cmd->type; 226 add_e820_entry(params, &ei); 227 228 return 0; 229 } 230 231 static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem, 232 unsigned long long mstart, 233 unsigned long long mend) 234 { 235 unsigned long start, end; 236 int ret; 237 238 cmem->ranges[0].start = mstart; 239 cmem->ranges[0].end = mend; 240 cmem->nr_ranges = 1; 241 242 /* Exclude elf header region */ 243 start = image->elf_load_addr; 244 end = start + image->elf_headers_sz - 1; 245 ret = crash_exclude_mem_range(cmem, start, end); 246 247 if (ret) 248 return ret; 249 250 /* Exclude dm crypt keys region */ 251 if (image->dm_crypt_keys_addr) { 252 start = image->dm_crypt_keys_addr; 253 end = start + image->dm_crypt_keys_sz - 1; 254 return crash_exclude_mem_range(cmem, start, end); 255 } 256 257 return ret; 258 } 259 260 /* Prepare memory map for crash dump kernel */ 261 int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params) 262 { 263 unsigned int nr_ranges = 0; 264 int i, ret = 0; 265 unsigned long flags; 266 struct e820_entry ei; 267 struct crash_memmap_data cmd; 268 struct crash_mem *cmem; 269 270 /* 271 * In the current x86 architecture code, the elfheader is always 272 * allocated at crashk_res.start. But it depends on the allocation 273 * position of elfheader in crashk_res. To avoid potential out of 274 * bounds in future, add an extra slot. 275 * 276 * And using random kexec_buf for passing dm crypt keys may cause a 277 * range split too, add another extra slot here. 278 */ 279 nr_ranges = 3; 280 cmem = vzalloc(struct_size(cmem, ranges, nr_ranges)); 281 if (!cmem) 282 return -ENOMEM; 283 284 cmem->max_nr_ranges = nr_ranges; 285 286 memset(&cmd, 0, sizeof(struct crash_memmap_data)); 287 cmd.params = params; 288 289 /* Add the low 1M */ 290 cmd.type = E820_TYPE_RAM; 291 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 292 walk_iomem_res_desc(IORES_DESC_NONE, flags, 0, (1<<20)-1, &cmd, 293 memmap_entry_callback); 294 295 /* Add ACPI tables */ 296 cmd.type = E820_TYPE_ACPI; 297 flags = IORESOURCE_MEM | IORESOURCE_BUSY; 298 walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd, 299 memmap_entry_callback); 300 301 /* Add ACPI Non-volatile Storage */ 302 cmd.type = E820_TYPE_NVS; 303 walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd, 304 memmap_entry_callback); 305 306 /* Add e820 reserved ranges */ 307 cmd.type = E820_TYPE_RESERVED; 308 flags = IORESOURCE_MEM; 309 walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd, 310 memmap_entry_callback); 311 312 /* Add crashk_low_res region */ 313 if (crashk_low_res.end) { 314 ei.addr = crashk_low_res.start; 315 ei.size = resource_size(&crashk_low_res); 316 ei.type = E820_TYPE_RAM; 317 add_e820_entry(params, &ei); 318 } 319 320 /* Exclude some ranges from crashk_res and add rest to memmap */ 321 ret = memmap_exclude_ranges(image, cmem, crashk_res.start, crashk_res.end); 322 if (ret) 323 goto out; 324 325 for (i = 0; i < cmem->nr_ranges; i++) { 326 ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1; 327 328 /* If entry is less than a page, skip it */ 329 if (ei.size < PAGE_SIZE) 330 continue; 331 ei.addr = cmem->ranges[i].start; 332 ei.type = E820_TYPE_RAM; 333 add_e820_entry(params, &ei); 334 } 335 336 for (i = 0; i < crashk_cma_cnt; ++i) { 337 ei.addr = crashk_cma_ranges[i].start; 338 ei.size = crashk_cma_ranges[i].end - 339 crashk_cma_ranges[i].start + 1; 340 ei.type = E820_TYPE_RAM; 341 add_e820_entry(params, &ei); 342 } 343 344 out: 345 vfree(cmem); 346 return ret; 347 } 348 349 int crash_load_segments(struct kimage *image) 350 { 351 int ret; 352 unsigned long pnum = 0; 353 struct kexec_buf kbuf = { .image = image, .buf_min = 0, 354 .buf_max = ULONG_MAX, .top_down = false }; 355 356 /* Prepare elf headers and add a segment */ 357 ret = crash_prepare_headers(IS_ENABLED(CONFIG_X86_64), &kbuf.buffer, 358 &kbuf.bufsz, &pnum); 359 if (ret) 360 return ret; 361 362 image->elf_headers = kbuf.buffer; 363 image->elf_headers_sz = kbuf.bufsz; 364 kbuf.memsz = kbuf.bufsz; 365 366 #ifdef CONFIG_CRASH_HOTPLUG 367 /* 368 * The elfcorehdr segment size accounts for VMCOREINFO, kernel_map, 369 * maximum CPUs and maximum memory ranges. 370 */ 371 if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) 372 pnum = 2 + CONFIG_NR_CPUS_DEFAULT + CONFIG_CRASH_MAX_MEMORY_RANGES; 373 else 374 pnum += 2 + CONFIG_NR_CPUS_DEFAULT; 375 376 if (pnum < (unsigned long)PN_XNUM) { 377 kbuf.memsz = pnum * sizeof(Elf64_Phdr); 378 kbuf.memsz += sizeof(Elf64_Ehdr); 379 380 image->elfcorehdr_index = image->nr_segments; 381 382 /* Mark as usable to crash kernel, else crash kernel fails on boot */ 383 image->elf_headers_sz = kbuf.memsz; 384 } else { 385 pr_err("number of Phdrs %lu exceeds max\n", pnum); 386 } 387 #endif 388 389 kbuf.buf_align = ELF_CORE_HEADER_ALIGN; 390 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 391 ret = kexec_add_buffer(&kbuf); 392 if (ret) 393 return ret; 394 image->elf_load_addr = kbuf.mem; 395 kexec_dprintk("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 396 image->elf_load_addr, kbuf.bufsz, kbuf.memsz); 397 398 return ret; 399 } 400 #endif /* CONFIG_KEXEC_FILE */ 401 402 #ifdef CONFIG_CRASH_HOTPLUG 403 404 #undef pr_fmt 405 #define pr_fmt(fmt) "crash hp: " fmt 406 407 int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_flags) 408 { 409 410 #ifdef CONFIG_KEXEC_FILE 411 if (image->file_mode) 412 return 1; 413 #endif 414 /* 415 * Initially, crash hotplug support for kexec_load was added 416 * with the KEXEC_UPDATE_ELFCOREHDR flag. Later, this 417 * functionality was expanded to accommodate multiple kexec 418 * segment updates, leading to the introduction of the 419 * KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag bit. Consequently, 420 * when the kexec tool sends either of these flags, it indicates 421 * that the required kexec segment (elfcorehdr) is excluded from 422 * the SHA calculation. 423 */ 424 return (kexec_flags & KEXEC_UPDATE_ELFCOREHDR || 425 kexec_flags & KEXEC_CRASH_HOTPLUG_SUPPORT); 426 } 427 428 unsigned int arch_crash_get_elfcorehdr_size(void) 429 { 430 unsigned int sz; 431 432 /* kernel_map, VMCOREINFO and maximum CPUs */ 433 sz = 2 + CONFIG_NR_CPUS_DEFAULT; 434 if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) 435 sz += CONFIG_CRASH_MAX_MEMORY_RANGES; 436 sz *= sizeof(Elf64_Phdr); 437 return sz; 438 } 439 440 /** 441 * arch_crash_handle_hotplug_event() - Handle hotplug elfcorehdr changes 442 * @image: a pointer to kexec_crash_image 443 * @arg: struct memory_notify handler for memory hotplug case and 444 * NULL for CPU hotplug case. 445 * 446 * Prepare the new elfcorehdr and replace the existing elfcorehdr. 447 */ 448 void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) 449 { 450 void *elfbuf = NULL, *old_elfcorehdr; 451 unsigned long mem, memsz; 452 unsigned long elfsz = 0; 453 454 /* 455 * As crash_prepare_elf64_headers() has already described all 456 * possible CPUs, there is no need to update the elfcorehdr 457 * for additional CPU changes. 458 */ 459 if ((image->file_mode || image->elfcorehdr_updated) && 460 ((image->hp_action == KEXEC_CRASH_HP_ADD_CPU) || 461 (image->hp_action == KEXEC_CRASH_HP_REMOVE_CPU))) 462 return; 463 464 /* 465 * Create the new elfcorehdr reflecting the changes to CPU and/or 466 * memory resources. 467 */ 468 if (crash_prepare_headers(IS_ENABLED(CONFIG_X86_64), &elfbuf, &elfsz, NULL)) { 469 pr_err("unable to create new elfcorehdr"); 470 goto out; 471 } 472 473 /* 474 * Obtain address and size of the elfcorehdr segment, and 475 * check it against the new elfcorehdr buffer. 476 */ 477 mem = image->segment[image->elfcorehdr_index].mem; 478 memsz = image->segment[image->elfcorehdr_index].memsz; 479 if (elfsz > memsz) { 480 pr_err("update elfcorehdr elfsz %lu > memsz %lu", 481 elfsz, memsz); 482 goto out; 483 } 484 485 /* 486 * Copy new elfcorehdr over the old elfcorehdr at destination. 487 */ 488 old_elfcorehdr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); 489 if (!old_elfcorehdr) { 490 pr_err("mapping elfcorehdr segment failed\n"); 491 goto out; 492 } 493 494 /* 495 * Temporarily invalidate the crash image while the 496 * elfcorehdr is updated. 497 */ 498 xchg(&kexec_crash_image, NULL); 499 memcpy_flushcache(old_elfcorehdr, elfbuf, elfsz); 500 xchg(&kexec_crash_image, image); 501 kunmap_local(old_elfcorehdr); 502 pr_debug("updated elfcorehdr\n"); 503 504 out: 505 vfree(elfbuf); 506 } 507 #endif 508