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