xref: /openbmc/linux/arch/x86/kernel/crash.c (revision 543cd4c5)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
262a31a03SHiroshi Shimamoto /*
362a31a03SHiroshi Shimamoto  * Architecture specific (i386/x86_64) functions for kexec based crash dumps.
462a31a03SHiroshi Shimamoto  *
562a31a03SHiroshi Shimamoto  * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
662a31a03SHiroshi Shimamoto  *
762a31a03SHiroshi Shimamoto  * Copyright (C) IBM Corporation, 2004. All rights reserved.
8dd5f7260SVivek Goyal  * Copyright (C) Red Hat Inc., 2014. All rights reserved.
9dd5f7260SVivek Goyal  * Authors:
10dd5f7260SVivek Goyal  *      Vivek Goyal <vgoyal@redhat.com>
1162a31a03SHiroshi Shimamoto  *
1262a31a03SHiroshi Shimamoto  */
1362a31a03SHiroshi Shimamoto 
14dd5f7260SVivek Goyal #define pr_fmt(fmt)	"kexec: " fmt
15dd5f7260SVivek Goyal 
1662a31a03SHiroshi Shimamoto #include <linux/types.h>
1762a31a03SHiroshi Shimamoto #include <linux/kernel.h>
1862a31a03SHiroshi Shimamoto #include <linux/smp.h>
1962a31a03SHiroshi Shimamoto #include <linux/reboot.h>
2062a31a03SHiroshi Shimamoto #include <linux/kexec.h>
2162a31a03SHiroshi Shimamoto #include <linux/delay.h>
2262a31a03SHiroshi Shimamoto #include <linux/elf.h>
2362a31a03SHiroshi Shimamoto #include <linux/elfcore.h>
24186f4360SPaul Gortmaker #include <linux/export.h>
25dd5f7260SVivek Goyal #include <linux/slab.h>
26d6472302SStephen Rothwell #include <linux/vmalloc.h>
276f599d84SLianbo Jiang #include <linux/memblock.h>
2862a31a03SHiroshi Shimamoto 
2962a31a03SHiroshi Shimamoto #include <asm/processor.h>
3062a31a03SHiroshi Shimamoto #include <asm/hardirq.h>
3162a31a03SHiroshi Shimamoto #include <asm/nmi.h>
3262a31a03SHiroshi Shimamoto #include <asm/hw_irq.h>
3362a31a03SHiroshi Shimamoto #include <asm/apic.h>
345520b7e7SIngo Molnar #include <asm/e820/types.h>
358643e28dSJiang Liu #include <asm/io_apic.h>
360c1b2724SOGAWA Hirofumi #include <asm/hpet.h>
3762a31a03SHiroshi Shimamoto #include <linux/kdebug.h>
3896b89dc6SJaswinder Singh Rajput #include <asm/cpu.h>
39ed23dc6fSGlauber Costa #include <asm/reboot.h>
40da06a43dSTakao Indoh #include <asm/intel_pt.h>
4189f579ceSYi Wang #include <asm/crash.h>
426f599d84SLianbo Jiang #include <asm/cmdline.h>
438e294786SEduardo Habkost 
44dd5f7260SVivek Goyal /* Used while preparing memory map entries for second kernel */
45dd5f7260SVivek Goyal struct crash_memmap_data {
46dd5f7260SVivek Goyal 	struct boot_params *params;
47dd5f7260SVivek Goyal 	/* Type of memory */
48dd5f7260SVivek Goyal 	unsigned int type;
49dd5f7260SVivek Goyal };
50dd5f7260SVivek Goyal 
51f23d1f4aSZhang Yanfei #if defined(CONFIG_SMP) && defined(CONFIG_X86_LOCAL_APIC)
52f23d1f4aSZhang Yanfei 
kdump_nmi_callback(int cpu,struct pt_regs * regs)53f23d1f4aSZhang Yanfei static void kdump_nmi_callback(int cpu, struct pt_regs *regs)
54f23d1f4aSZhang Yanfei {
55f23d1f4aSZhang Yanfei 	crash_save_cpu(regs, cpu);
56f23d1f4aSZhang Yanfei 
57f23d1f4aSZhang Yanfei 	/*
580ca0d818SZhang Yanfei 	 * Disable Intel PT to stop its logging
59f23d1f4aSZhang Yanfei 	 */
60f23d1f4aSZhang Yanfei 	cpu_emergency_stop_pt();
61f23d1f4aSZhang Yanfei 
62f23d1f4aSZhang Yanfei 	disable_local_APIC();
630ca0d818SZhang Yanfei }
64f23d1f4aSZhang Yanfei 
kdump_nmi_shootdown_cpus(void)65f23d1f4aSZhang Yanfei void kdump_nmi_shootdown_cpus(void)
66f23d1f4aSZhang Yanfei {
67f23d1f4aSZhang Yanfei 	nmi_shootdown_cpus(kdump_nmi_callback);
68f23d1f4aSZhang Yanfei 
69f23d1f4aSZhang Yanfei 	disable_local_APIC();
70f23d1f4aSZhang Yanfei }
71f23d1f4aSZhang Yanfei 
72b2bbe71bSEduardo Habkost /* Override the weak function in kernel/panic.c */
crash_smp_send_stop(void)73b2bbe71bSEduardo Habkost void crash_smp_send_stop(void)
749c48f1c6SDon Zickus {
7562a31a03SHiroshi Shimamoto 	static int cpus_stopped;
76a7d41820SEduardo Habkost 
77a7d41820SEduardo Habkost 	if (cpus_stopped)
78f23d1f4aSZhang Yanfei 		return;
79f23d1f4aSZhang Yanfei 
80f23d1f4aSZhang Yanfei 	if (smp_ops.crash_stop_other_cpus)
81f23d1f4aSZhang Yanfei 		smp_ops.crash_stop_other_cpus();
82f23d1f4aSZhang Yanfei 	else
83da06a43dSTakao Indoh 		smp_send_stop();
84da06a43dSTakao Indoh 
85da06a43dSTakao Indoh 	cpus_stopped = 1;
86da06a43dSTakao Indoh }
87da06a43dSTakao Indoh 
88a7d41820SEduardo Habkost #else
crash_smp_send_stop(void)89a7d41820SEduardo Habkost void crash_smp_send_stop(void)
90a7d41820SEduardo Habkost {
910ee59413SHidehiro Kawai 	/* There are no cpus to shootdown */
92d1e7b91cSEduardo Habkost }
938e294786SEduardo Habkost #endif
94d1e7b91cSEduardo Habkost 
native_machine_crash_shutdown(struct pt_regs * regs)9562a31a03SHiroshi Shimamoto void native_machine_crash_shutdown(struct pt_regs *regs)
9662a31a03SHiroshi Shimamoto {
97d1e7b91cSEduardo Habkost 	/* This function is only called after the system
980ee59413SHidehiro Kawai 	 * has panicked or is otherwise in a critical state.
990ee59413SHidehiro Kawai 	 * The minimum amount of code to allow a kexec'd kernel
1000ee59413SHidehiro Kawai 	 * to run successfully needs to happen here.
1010ee59413SHidehiro Kawai 	 *
1020ee59413SHidehiro Kawai 	 * In practice this means shooting down the other cpus in
1030ee59413SHidehiro Kawai 	 * an SMP system.
1040ee59413SHidehiro Kawai 	 */
1050ee59413SHidehiro Kawai 	/* The kernel is broken so disable interrupts */
1060ee59413SHidehiro Kawai 	local_irq_disable();
1070ee59413SHidehiro Kawai 
1080ee59413SHidehiro Kawai 	crash_smp_send_stop();
1090ee59413SHidehiro Kawai 
1100ee59413SHidehiro Kawai 	cpu_emergency_disable_virtualization();
1110ee59413SHidehiro Kawai 
1120ee59413SHidehiro Kawai 	/*
1130ee59413SHidehiro Kawai 	 * Disable Intel PT to stop its logging
11462a31a03SHiroshi Shimamoto 	 */
1150ee59413SHidehiro Kawai 	cpu_emergency_stop_pt();
11662a31a03SHiroshi Shimamoto 
11762a31a03SHiroshi Shimamoto #ifdef CONFIG_X86_IO_APIC
11862a31a03SHiroshi Shimamoto 	/* Prevent crash_kexec() from deadlocking on ioapic_lock. */
11962a31a03SHiroshi Shimamoto 	ioapic_zap_locks();
12062a31a03SHiroshi Shimamoto 	clear_IO_APIC();
121ed23dc6fSGlauber Costa #endif
12262a31a03SHiroshi Shimamoto 	lapic_shutdown();
12362a31a03SHiroshi Shimamoto 	restore_boot_irq_mode();
12462a31a03SHiroshi Shimamoto #ifdef CONFIG_HPET_TIMER
12562a31a03SHiroshi Shimamoto 	hpet_disable();
12662a31a03SHiroshi Shimamoto #endif
12762a31a03SHiroshi Shimamoto 	crash_save_cpu(regs, safe_smp_processor_id());
12862a31a03SHiroshi Shimamoto }
12962a31a03SHiroshi Shimamoto 
13062a31a03SHiroshi Shimamoto #if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_HOTPLUG)
get_nr_ram_ranges_callback(struct resource * res,void * arg)13162a31a03SHiroshi Shimamoto static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
13262a31a03SHiroshi Shimamoto {
13362a31a03SHiroshi Shimamoto 	unsigned int *nr_ranges = arg;
1340ee59413SHidehiro Kawai 
1352340b62fSEduardo Habkost 	(*nr_ranges)++;
136f23d1f4aSZhang Yanfei 	return 0;
137f23d1f4aSZhang Yanfei }
138f23d1f4aSZhang Yanfei 
139f23d1f4aSZhang Yanfei /* Gather all the required information to prepare elf headers for ram regions */
fill_up_crash_elf_data(void)140f23d1f4aSZhang Yanfei static struct crash_mem *fill_up_crash_elf_data(void)
14126044affSSean Christopherson {
1422340b62fSEduardo Habkost 	unsigned int nr_ranges = 0;
143da06a43dSTakao Indoh 	struct crash_mem *cmem;
144da06a43dSTakao Indoh 
145da06a43dSTakao Indoh 	walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
146da06a43dSTakao Indoh 	if (!nr_ranges)
147da06a43dSTakao Indoh 		return NULL;
14817405453SYoshihiro YUNOMAE 
14917405453SYoshihiro YUNOMAE 	/*
15017405453SYoshihiro YUNOMAE 	 * Exclusion of crash region and/or crashk_low_res may cause
151339b2ae0SBaoquan He 	 * another range split. So add extra two slots here.
15262a31a03SHiroshi Shimamoto 	 */
153522e6646SFenghua Yu 	nr_ranges += 2;
154339b2ae0SBaoquan He 	cmem = vzalloc(struct_size(cmem, ranges, nr_ranges));
1550c1b2724SOGAWA Hirofumi 	if (!cmem)
1560c1b2724SOGAWA Hirofumi 		return NULL;
1570c1b2724SOGAWA Hirofumi 
15862a31a03SHiroshi Shimamoto 	cmem->max_nr_ranges = nr_ranges;
15962a31a03SHiroshi Shimamoto 	cmem->nr_ranges = 0;
160dd5f7260SVivek Goyal 
161ea53ad9cSEric DeVolder 	return cmem;
1621d2e733bSTom Lendacky }
163dd5f7260SVivek Goyal 
164e3c41e37SLee, Chun-Yi /*
165dd5f7260SVivek Goyal  * Look for any unwanted ranges between mstart, mend and remove them. This
166dd5f7260SVivek Goyal  * might lead to split and split ranges are put in cmem->ranges[] array
167dd5f7260SVivek Goyal  */
elf_header_exclude_ranges(struct crash_mem * cmem)168dd5f7260SVivek Goyal static int elf_header_exclude_ranges(struct crash_mem *cmem)
169dd5f7260SVivek Goyal {
170dd5f7260SVivek Goyal 	int ret = 0;
1718d5f894aSAKASHI Takahiro 
172dd5f7260SVivek Goyal 	/* Exclude the low 1M because it is always reserved */
173dd5f7260SVivek Goyal 	ret = crash_exclude_mem_range(cmem, 0, (1<<20)-1);
1748d5f894aSAKASHI Takahiro 	if (ret)
175dd5f7260SVivek Goyal 		return ret;
1769eff3037SBorislav Petkov 
1778d5f894aSAKASHI Takahiro 	/* Exclude crashkernel region */
1788d5f894aSAKASHI Takahiro 	ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
179dd5f7260SVivek Goyal 	if (ret)
1808d5f894aSAKASHI Takahiro 		return ret;
1818d5f894aSAKASHI Takahiro 
1828d5f894aSAKASHI Takahiro 	if (crashk_low_res.end)
1838d5f894aSAKASHI Takahiro 		ret = crash_exclude_mem_range(cmem, crashk_low_res.start,
1848d5f894aSAKASHI Takahiro 					      crashk_low_res.end);
1854df43095SGustavo A. R. Silva 
1868d5f894aSAKASHI Takahiro 	return ret;
1878d5f894aSAKASHI Takahiro }
188dd5f7260SVivek Goyal 
prepare_elf64_ram_headers_callback(struct resource * res,void * arg)1898d5f894aSAKASHI Takahiro static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
1908d5f894aSAKASHI Takahiro {
191dd5f7260SVivek Goyal 	struct crash_mem *cmem = arg;
1928d5f894aSAKASHI Takahiro 
193dd5f7260SVivek Goyal 	cmem->ranges[cmem->nr_ranges].start = res->start;
194dd5f7260SVivek Goyal 	cmem->ranges[cmem->nr_ranges].end = res->end;
195dd5f7260SVivek Goyal 	cmem->nr_ranges++;
196dd5f7260SVivek Goyal 
1978d5f894aSAKASHI Takahiro 	return 0;
198dd5f7260SVivek Goyal }
1998d5f894aSAKASHI Takahiro 
200dd5f7260SVivek Goyal /* Prepare elf headers. Return addr and size */
prepare_elf_headers(struct kimage * image,void ** addr,unsigned long * sz,unsigned long * nr_mem_ranges)201dd5f7260SVivek Goyal static int prepare_elf_headers(struct kimage *image, void **addr,
202dd5f7260SVivek Goyal 					unsigned long *sz, unsigned long *nr_mem_ranges)
2037c321eb2SLianbo Jiang {
204a3e1c3bbSLianbo Jiang 	struct crash_mem *cmem;
2057c321eb2SLianbo Jiang 	int ret;
2067c321eb2SLianbo Jiang 
2077c321eb2SLianbo Jiang 	cmem = fill_up_crash_elf_data();
208dd5f7260SVivek Goyal 	if (!cmem)
209babac4a8SAKASHI Takahiro 		return -ENOMEM;
210dd5f7260SVivek Goyal 
211dd5f7260SVivek Goyal 	ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
212dd5f7260SVivek Goyal 	if (ret)
2139eff3037SBorislav Petkov 		goto out;
214babac4a8SAKASHI Takahiro 
215babac4a8SAKASHI Takahiro 	/* Exclude unwanted mem ranges */
216dd5f7260SVivek Goyal 	ret = elf_header_exclude_ranges(cmem);
217dd5f7260SVivek Goyal 	if (ret)
218dd5f7260SVivek Goyal 		goto out;
219dd5f7260SVivek Goyal 
2201d2e733bSTom Lendacky 	/* Return the computed number of memory ranges, for hotplug usage */
221dd5f7260SVivek Goyal 	*nr_mem_ranges = cmem->nr_ranges;
2228d5f894aSAKASHI Takahiro 
223dd5f7260SVivek Goyal 	/* By default prepare 64bit headers */
224cbe66016SAKASHI Takahiro 	ret =  crash_prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz);
225cbe66016SAKASHI Takahiro 
226cbe66016SAKASHI Takahiro out:
227dd5f7260SVivek Goyal 	vfree(cmem);
228cbe66016SAKASHI Takahiro 	return ret;
229dd5f7260SVivek Goyal }
230dd5f7260SVivek Goyal #endif
231dd5f7260SVivek Goyal 
232dd5f7260SVivek Goyal #ifdef CONFIG_KEXEC_FILE
add_e820_entry(struct boot_params * params,struct e820_entry * entry)233ea53ad9cSEric DeVolder static int add_e820_entry(struct boot_params *params, struct e820_entry *entry)
234dd5f7260SVivek Goyal {
2358d5f894aSAKASHI Takahiro 	unsigned int nr_e820_entries;
2367c321eb2SLianbo Jiang 
237dd5f7260SVivek Goyal 	nr_e820_entries = params->e820_entries;
2388d5f894aSAKASHI Takahiro 	if (nr_e820_entries >= E820_MAX_ENTRIES_ZEROPAGE)
2398d5f894aSAKASHI Takahiro 		return 1;
240dd5f7260SVivek Goyal 
241dd5f7260SVivek Goyal 	memcpy(&params->e820_table[nr_e820_entries], entry, sizeof(struct e820_entry));
2429eff3037SBorislav Petkov 	params->e820_entries++;
243cbe66016SAKASHI Takahiro 	return 0;
244cbe66016SAKASHI Takahiro }
245cbe66016SAKASHI Takahiro 
memmap_entry_callback(struct resource * res,void * arg)246cbe66016SAKASHI Takahiro static int memmap_entry_callback(struct resource *res, void *arg)
2478d5f894aSAKASHI Takahiro {
248cbe66016SAKASHI Takahiro 	struct crash_memmap_data *cmd = arg;
249cbe66016SAKASHI Takahiro 	struct boot_params *params = cmd->params;
250cbe66016SAKASHI Takahiro 	struct e820_entry ei;
251ea53ad9cSEric DeVolder 
252ea53ad9cSEric DeVolder 	ei.addr = res->start;
253ea53ad9cSEric DeVolder 	ei.size = resource_size(res);
254dd5f7260SVivek Goyal 	ei.type = cmd->type;
2559eff3037SBorislav Petkov 	add_e820_entry(params, &ei);
256cbe66016SAKASHI Takahiro 
257cbe66016SAKASHI Takahiro 	return 0;
2588d5f894aSAKASHI Takahiro }
259dd5f7260SVivek Goyal 
memmap_exclude_ranges(struct kimage * image,struct crash_mem * cmem,unsigned long long mstart,unsigned long long mend)260dd5f7260SVivek Goyal static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem,
261ea53ad9cSEric DeVolder 				 unsigned long long mstart,
262dd5f7260SVivek Goyal 				 unsigned long long mend)
263ea53ad9cSEric DeVolder {
2648ec67d97SIngo Molnar 	unsigned long start, end;
265dd5f7260SVivek Goyal 
266dd5f7260SVivek Goyal 	cmem->ranges[0].start = mstart;
267dd5f7260SVivek Goyal 	cmem->ranges[0].end = mend;
268dd5f7260SVivek Goyal 	cmem->nr_ranges = 1;
26908b46d5dSIngo Molnar 
270dd5f7260SVivek Goyal 	/* Exclude elf header region */
271dd5f7260SVivek Goyal 	start = image->elf_load_addr;
2729eff3037SBorislav Petkov 	end = start + image->elf_headers_sz - 1;
273dd5f7260SVivek Goyal 	return crash_exclude_mem_range(cmem, start, end);
274dd5f7260SVivek Goyal }
275dd5f7260SVivek Goyal 
276dd5f7260SVivek Goyal /* Prepare memory map for crash dump kernel */
crash_setup_memmap_entries(struct kimage * image,struct boot_params * params)2771d2e733bSTom Lendacky int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
278dd5f7260SVivek Goyal {
279dd5f7260SVivek Goyal 	int i, ret = 0;
280dd5f7260SVivek Goyal 	unsigned long flags;
2818ec67d97SIngo Molnar 	struct e820_entry ei;
282dd5f7260SVivek Goyal 	struct crash_memmap_data cmd;
2831d2e733bSTom Lendacky 	struct crash_mem *cmem;
2849275b933Skbuild test robot 
285dd5f7260SVivek Goyal 	cmem = vzalloc(struct_size(cmem, ranges, 1));
286dd5f7260SVivek Goyal 	if (!cmem)
287dd5f7260SVivek Goyal 		return -ENOMEM;
288dd5f7260SVivek Goyal 
289dd5f7260SVivek Goyal 	memset(&cmd, 0, sizeof(struct crash_memmap_data));
290dd5f7260SVivek Goyal 	cmd.params = params;
291dd5f7260SVivek Goyal 
292dd5f7260SVivek Goyal 	/* Add the low 1M */
293dd5f7260SVivek Goyal 	cmd.type = E820_TYPE_RAM;
294dd5f7260SVivek Goyal 	flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
295dd5f7260SVivek Goyal 	walk_iomem_res_desc(IORES_DESC_NONE, flags, 0, (1<<20)-1, &cmd,
296dd5f7260SVivek Goyal 			    memmap_entry_callback);
297dd5f7260SVivek Goyal 
298dd5f7260SVivek Goyal 	/* Add ACPI tables */
299dd5f7260SVivek Goyal 	cmd.type = E820_TYPE_ACPI;
300dd5f7260SVivek Goyal 	flags = IORESOURCE_MEM | IORESOURCE_BUSY;
301dd5f7260SVivek Goyal 	walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1, &cmd,
302179350f0SLakshmi Ramasubramanian 			    memmap_entry_callback);
303179350f0SLakshmi Ramasubramanian 
304babac4a8SAKASHI Takahiro 	/* Add ACPI Non-volatile Storage */
305dd5f7260SVivek Goyal 	cmd.type = E820_TYPE_NVS;
306dd5f7260SVivek Goyal 	walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
307dd5f7260SVivek Goyal 			    memmap_entry_callback);
308dd5f7260SVivek Goyal 
309dd5f7260SVivek Goyal 	/* Add e820 reserved ranges */
310dd5f7260SVivek Goyal 	cmd.type = E820_TYPE_RESERVED;
311dd5f7260SVivek Goyal 	flags = IORESOURCE_MEM;
3128ec67d97SIngo Molnar 	walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd,
313dd5f7260SVivek Goyal 			    memmap_entry_callback);
314dd5f7260SVivek Goyal 
315dd5f7260SVivek Goyal 	/* Add crashk_low_res region */
3165849cdf8SMike Galbraith 	if (crashk_low_res.end) {
317dd5f7260SVivek Goyal 		ei.addr = crashk_low_res.start;
318dd5f7260SVivek Goyal 		ei.size = resource_size(&crashk_low_res);
319dd5f7260SVivek Goyal 		ei.type = E820_TYPE_RAM;
320dd5f7260SVivek Goyal 		add_e820_entry(params, &ei);
321dd5f7260SVivek Goyal 	}
322dd5f7260SVivek Goyal 
3237c321eb2SLianbo Jiang 	/* Exclude some ranges from crashk_res and add rest to memmap */
3247c321eb2SLianbo Jiang 	ret = memmap_exclude_ranges(image, cmem, crashk_res.start, crashk_res.end);
3257c321eb2SLianbo Jiang 	if (ret)
3267c321eb2SLianbo Jiang 		goto out;
3277c321eb2SLianbo Jiang 
328dd5f7260SVivek Goyal 	for (i = 0; i < cmem->nr_ranges; i++) {
329dd5f7260SVivek Goyal 		ei.size = cmem->ranges[i].end - cmem->ranges[i].start + 1;
33009821ff1SIngo Molnar 
331dd5f7260SVivek Goyal 		/* If entry is less than a page, skip it */
332f0f4711aSToshi Kani 		if (ei.size < PAGE_SIZE)
333dd5f7260SVivek Goyal 			continue;
334dd5f7260SVivek Goyal 		ei.addr = cmem->ranges[i].start;
335dd5f7260SVivek Goyal 		ei.type = E820_TYPE_RAM;
33609821ff1SIngo Molnar 		add_e820_entry(params, &ei);
337f0f4711aSToshi Kani 	}
338dd5f7260SVivek Goyal 
339dd5f7260SVivek Goyal out:
340980621daSLianbo Jiang 	vfree(cmem);
341980621daSLianbo Jiang 	return ret;
342980621daSLianbo Jiang }
343980621daSLianbo Jiang 
crash_load_segments(struct kimage * image)344980621daSLianbo Jiang int crash_load_segments(struct kimage *image)
345980621daSLianbo Jiang {
346dd5f7260SVivek Goyal 	int ret;
347dd5f7260SVivek Goyal 	unsigned long pnum = 0;
348dd5f7260SVivek Goyal 	struct kexec_buf kbuf = { .image = image, .buf_min = 0,
3491429b568SJulia Lawall 				  .buf_max = ULONG_MAX, .top_down = false };
35009821ff1SIngo Molnar 
351dd5f7260SVivek Goyal 	/* Prepare elf headers and add a segment */
352dd5f7260SVivek Goyal 	ret = prepare_elf_headers(image, &kbuf.buffer, &kbuf.bufsz, &pnum);
353dd5f7260SVivek Goyal 	if (ret)
354dd5f7260SVivek Goyal 		return ret;
3559eff3037SBorislav Petkov 
356dd5f7260SVivek Goyal 	image->elf_headers	= kbuf.buffer;
357dd5f7260SVivek Goyal 	image->elf_headers_sz	= kbuf.bufsz;
358dd5f7260SVivek Goyal 	kbuf.memsz		= kbuf.bufsz;
359dd5f7260SVivek Goyal 
360dd5f7260SVivek Goyal #ifdef CONFIG_CRASH_HOTPLUG
361dd5f7260SVivek Goyal 	/*
362dd5f7260SVivek Goyal 	 * The elfcorehdr segment size accounts for VMCOREINFO, kernel_map,
363dd5f7260SVivek Goyal 	 * maximum CPUs and maximum memory ranges.
364dd5f7260SVivek Goyal 	 */
365dd5f7260SVivek Goyal 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
36609821ff1SIngo Molnar 		pnum = 2 + CONFIG_NR_CPUS_DEFAULT + CONFIG_CRASH_MAX_MEMORY_RANGES;
367dd5f7260SVivek Goyal 	else
368dd5f7260SVivek Goyal 		pnum += 2 + CONFIG_NR_CPUS_DEFAULT;
369dd5f7260SVivek Goyal 
370dd5f7260SVivek Goyal 	if (pnum < (unsigned long)PN_XNUM) {
371dd5f7260SVivek Goyal 		kbuf.memsz = pnum * sizeof(Elf64_Phdr);
372dd5f7260SVivek Goyal 		kbuf.memsz += sizeof(Elf64_Ehdr);
373dd5f7260SVivek Goyal 
374dd5f7260SVivek Goyal 		image->elfcorehdr_index = image->nr_segments;
375dd5f7260SVivek Goyal 
376dd5f7260SVivek Goyal 		/* Mark as usable to crash kernel, else crash kernel fails on boot */
377dd5f7260SVivek Goyal 		image->elf_headers_sz = kbuf.memsz;
378ea53ad9cSEric DeVolder 	} else {
379ec2b9bfaSThiago Jung Bauermann 		pr_err("number of Phdrs %lu exceeds max\n", pnum);
380ec2b9bfaSThiago Jung Bauermann 	}
381dd5f7260SVivek Goyal #endif
382dd5f7260SVivek Goyal 
383ea53ad9cSEric DeVolder 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
384dd5f7260SVivek Goyal 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
385dd5f7260SVivek Goyal 	ret = kexec_add_buffer(&kbuf);
386dd5f7260SVivek Goyal 	if (ret)
387179350f0SLakshmi Ramasubramanian 		return ret;
388179350f0SLakshmi Ramasubramanian 	image->elf_load_addr = kbuf.mem;
389ec2b9bfaSThiago Jung Bauermann 	pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
390ea53ad9cSEric DeVolder 		 image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
391ea53ad9cSEric DeVolder 
392ea53ad9cSEric DeVolder 	return ret;
393ea53ad9cSEric DeVolder }
394ea53ad9cSEric DeVolder #endif /* CONFIG_KEXEC_FILE */
395ea53ad9cSEric DeVolder 
396ea53ad9cSEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
397ea53ad9cSEric DeVolder 
398ea53ad9cSEric DeVolder #undef pr_fmt
399ea53ad9cSEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt
400ea53ad9cSEric DeVolder 
401ea53ad9cSEric DeVolder /* These functions provide the value for the sysfs crash_hotplug nodes */
402ea53ad9cSEric DeVolder #ifdef CONFIG_HOTPLUG_CPU
arch_crash_hotplug_cpu_support(void)403ea53ad9cSEric DeVolder int arch_crash_hotplug_cpu_support(void)
404ea53ad9cSEric DeVolder {
405ea53ad9cSEric DeVolder 	return crash_check_update_elfcorehdr();
406ea53ad9cSEric DeVolder }
407ea53ad9cSEric DeVolder #endif
408ea53ad9cSEric DeVolder 
409ea53ad9cSEric DeVolder #ifdef CONFIG_MEMORY_HOTPLUG
arch_crash_hotplug_memory_support(void)410ea53ad9cSEric DeVolder int arch_crash_hotplug_memory_support(void)
411ea53ad9cSEric DeVolder {
412ea53ad9cSEric DeVolder 	return crash_check_update_elfcorehdr();
413ea53ad9cSEric DeVolder }
414ec2b9bfaSThiago Jung Bauermann #endif
415993a1103SDave Young 
arch_crash_get_elfcorehdr_size(void)416ec2b9bfaSThiago Jung Bauermann unsigned int arch_crash_get_elfcorehdr_size(void)
417d00dd2f2STakashi Iwai {
418dd5f7260SVivek Goyal 	unsigned int sz;
419179350f0SLakshmi Ramasubramanian 
420dd5f7260SVivek Goyal 	/* kernel_map, VMCOREINFO and maximum CPUs */
421b57a7c9dSEric DeVolder 	sz = 2 + CONFIG_NR_CPUS_DEFAULT;
422dd5f7260SVivek Goyal 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
423dd5f7260SVivek Goyal 		sz += CONFIG_CRASH_MAX_MEMORY_RANGES;
424dd5f7260SVivek Goyal 	sz *= sizeof(Elf64_Phdr);
42574ca317cSVivek Goyal 	return sz;
426ea53ad9cSEric DeVolder }
427ea53ad9cSEric DeVolder 
428ea53ad9cSEric DeVolder /**
429ea53ad9cSEric DeVolder  * arch_crash_handle_hotplug_event() - Handle hotplug elfcorehdr changes
430ea53ad9cSEric DeVolder  * @image: a pointer to kexec_crash_image
431ea53ad9cSEric DeVolder  *
432a72bbec7SEric DeVolder  * Prepare the new elfcorehdr and replace the existing elfcorehdr.
433a72bbec7SEric DeVolder  */
arch_crash_handle_hotplug_event(struct kimage * image)434a72bbec7SEric DeVolder void arch_crash_handle_hotplug_event(struct kimage *image)
435a72bbec7SEric DeVolder {
436a72bbec7SEric DeVolder 	void *elfbuf = NULL, *old_elfcorehdr;
437a72bbec7SEric DeVolder 	unsigned long nr_mem_ranges;
438a72bbec7SEric DeVolder 	unsigned long mem, memsz;
439a72bbec7SEric DeVolder 	unsigned long elfsz = 0;
440a72bbec7SEric DeVolder 
441a72bbec7SEric DeVolder 	/*
442a72bbec7SEric DeVolder 	 * As crash_prepare_elf64_headers() has already described all
443a72bbec7SEric DeVolder 	 * possible CPUs, there is no need to update the elfcorehdr
444a72bbec7SEric DeVolder 	 * for additional CPU changes.
445a72bbec7SEric DeVolder 	 */
446a72bbec7SEric DeVolder 	if ((image->file_mode || image->elfcorehdr_updated) &&
447a72bbec7SEric DeVolder 		((image->hp_action == KEXEC_CRASH_HP_ADD_CPU) ||
448a72bbec7SEric DeVolder 		(image->hp_action == KEXEC_CRASH_HP_REMOVE_CPU)))
449a72bbec7SEric DeVolder 		return;
450a72bbec7SEric DeVolder 
451a72bbec7SEric DeVolder 	/*
452a72bbec7SEric DeVolder 	 * Create the new elfcorehdr reflecting the changes to CPU and/or
453a72bbec7SEric DeVolder 	 * memory resources.
454a72bbec7SEric DeVolder 	 */
455a72bbec7SEric DeVolder 	if (prepare_elf_headers(image, &elfbuf, &elfsz, &nr_mem_ranges)) {
456a72bbec7SEric DeVolder 		pr_err("unable to create new elfcorehdr");
457a72bbec7SEric DeVolder 		goto out;
458a72bbec7SEric DeVolder 	}
459ea53ad9cSEric DeVolder 
460ea53ad9cSEric DeVolder 	/*
461ea53ad9cSEric DeVolder 	 * Obtain address and size of the elfcorehdr segment, and
462ea53ad9cSEric DeVolder 	 * check it against the new elfcorehdr buffer.
463ea53ad9cSEric DeVolder 	 */
464ea53ad9cSEric DeVolder 	mem = image->segment[image->elfcorehdr_index].mem;
465ea53ad9cSEric DeVolder 	memsz = image->segment[image->elfcorehdr_index].memsz;
466ea53ad9cSEric DeVolder 	if (elfsz > memsz) {
467ea53ad9cSEric DeVolder 		pr_err("update elfcorehdr elfsz %lu > memsz %lu",
468ea53ad9cSEric DeVolder 			elfsz, memsz);
469ea53ad9cSEric DeVolder 		goto out;
470ea53ad9cSEric DeVolder 	}
471ea53ad9cSEric DeVolder 
472ea53ad9cSEric DeVolder 	/*
473*543cd4c5SEric DeVolder 	 * Copy new elfcorehdr over the old elfcorehdr at destination.
474*543cd4c5SEric DeVolder 	 */
475*543cd4c5SEric DeVolder 	old_elfcorehdr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
476*543cd4c5SEric DeVolder 	if (!old_elfcorehdr) {
477*543cd4c5SEric DeVolder 		pr_err("mapping elfcorehdr segment failed\n");
478*543cd4c5SEric DeVolder 		goto out;
479*543cd4c5SEric DeVolder 	}
480*543cd4c5SEric DeVolder 
481*543cd4c5SEric DeVolder 	/*
482*543cd4c5SEric DeVolder 	 * Temporarily invalidate the crash image while the
483ea53ad9cSEric DeVolder 	 * elfcorehdr is updated.
484ea53ad9cSEric DeVolder 	 */
485ea53ad9cSEric DeVolder 	xchg(&kexec_crash_image, NULL);
486ea53ad9cSEric DeVolder 	memcpy_flushcache(old_elfcorehdr, elfbuf, elfsz);
487ea53ad9cSEric DeVolder 	xchg(&kexec_crash_image, image);
488ea53ad9cSEric DeVolder 	kunmap_local(old_elfcorehdr);
489ea53ad9cSEric DeVolder 	pr_debug("updated elfcorehdr\n");
490ea53ad9cSEric DeVolder 
491ea53ad9cSEric DeVolder out:
492ea53ad9cSEric DeVolder 	vfree(elfbuf);
493ea53ad9cSEric DeVolder }
494ea53ad9cSEric DeVolder #endif
495ea53ad9cSEric DeVolder