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