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