xref: /linux-6.15/kernel/crash_core.c (revision 4e87ff59)
140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2692f66f2SHari Bathini /*
3692f66f2SHari Bathini  * crash.c - kernel crash support code.
4692f66f2SHari Bathini  * Copyright (C) 2002-2004 Eric Biederman  <[email protected]>
5692f66f2SHari Bathini  */
6692f66f2SHari Bathini 
744e8a5e9SStephen Boyd #include <linux/buildid.h>
871d2bcecSPhilipp Rudo #include <linux/init.h>
9692f66f2SHari Bathini #include <linux/utsname.h>
10692f66f2SHari Bathini #include <linux/vmalloc.h>
1146d36b1bSTao Liu #include <linux/sizes.h>
126f991cc3SEric DeVolder #include <linux/kexec.h>
1324726275SEric DeVolder #include <linux/memory.h>
1424726275SEric DeVolder #include <linux/cpuhotplug.h>
150ab97169SBaoquan He #include <linux/memblock.h>
160ab97169SBaoquan He #include <linux/kmemleak.h>
17692f66f2SHari Bathini 
18692f66f2SHari Bathini #include <asm/page.h>
19692f66f2SHari Bathini #include <asm/sections.h>
20692f66f2SHari Bathini 
21a24d22b2SEric Biggers #include <crypto/sha1.h>
220935288cSVijay Balakrishna 
235fd8fea9SStephen Brennan #include "kallsyms_internal.h"
2424726275SEric DeVolder #include "kexec_internal.h"
255fd8fea9SStephen Brennan 
266f991cc3SEric DeVolder /* Per cpu memory for storing cpu states in case of system crash. */
276f991cc3SEric DeVolder note_buf_t __percpu *crash_notes;
28692f66f2SHari Bathini 
29692f66f2SHari Bathini /* vmcoreinfo stuff */
3023c85094SOmar Sandoval unsigned char *vmcoreinfo_data;
3123c85094SOmar Sandoval size_t vmcoreinfo_size;
32203e9e41SXunlei Pang u32 *vmcoreinfo_note;
33692f66f2SHari Bathini 
341229384fSXunlei Pang /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
351229384fSXunlei Pang static unsigned char *vmcoreinfo_data_safecopy;
361229384fSXunlei Pang 
37b631b95dSBaoquan He /* Location of the reserved area for the crash kernel */
38b631b95dSBaoquan He struct resource crashk_res = {
39b631b95dSBaoquan He 	.name  = "Crash kernel",
40b631b95dSBaoquan He 	.start = 0,
41b631b95dSBaoquan He 	.end   = 0,
42b631b95dSBaoquan He 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
43b631b95dSBaoquan He 	.desc  = IORES_DESC_CRASH_KERNEL
44b631b95dSBaoquan He };
45b631b95dSBaoquan He struct resource crashk_low_res = {
46b631b95dSBaoquan He 	.name  = "Crash kernel",
47b631b95dSBaoquan He 	.start = 0,
48b631b95dSBaoquan He 	.end   = 0,
49b631b95dSBaoquan He 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
50b631b95dSBaoquan He 	.desc  = IORES_DESC_CRASH_KERNEL
51b631b95dSBaoquan He };
52b631b95dSBaoquan He 
53692f66f2SHari Bathini /*
54692f66f2SHari Bathini  * parsing the "crashkernel" commandline
55692f66f2SHari Bathini  *
56692f66f2SHari Bathini  * this code is intended to be called from architecture specific code
57692f66f2SHari Bathini  */
58692f66f2SHari Bathini 
59692f66f2SHari Bathini 
60692f66f2SHari Bathini /*
61692f66f2SHari Bathini  * This function parses command lines in the format
62692f66f2SHari Bathini  *
63692f66f2SHari Bathini  *   crashkernel=ramsize-range:size[,...][@offset]
64692f66f2SHari Bathini  *
65692f66f2SHari Bathini  * The function returns 0 on success and -EINVAL on failure.
66692f66f2SHari Bathini  */
67692f66f2SHari Bathini static int __init parse_crashkernel_mem(char *cmdline,
68692f66f2SHari Bathini 					unsigned long long system_ram,
69692f66f2SHari Bathini 					unsigned long long *crash_size,
70692f66f2SHari Bathini 					unsigned long long *crash_base)
71692f66f2SHari Bathini {
72692f66f2SHari Bathini 	char *cur = cmdline, *tmp;
7346d36b1bSTao Liu 	unsigned long long total_mem = system_ram;
7446d36b1bSTao Liu 
7546d36b1bSTao Liu 	/*
7646d36b1bSTao Liu 	 * Firmware sometimes reserves some memory regions for its own use,
7746d36b1bSTao Liu 	 * so the system memory size is less than the actual physical memory
7846d36b1bSTao Liu 	 * size. Work around this by rounding up the total size to 128M,
7946d36b1bSTao Liu 	 * which is enough for most test cases.
8046d36b1bSTao Liu 	 */
8146d36b1bSTao Liu 	total_mem = roundup(total_mem, SZ_128M);
82692f66f2SHari Bathini 
83692f66f2SHari Bathini 	/* for each entry of the comma-separated list */
84692f66f2SHari Bathini 	do {
85692f66f2SHari Bathini 		unsigned long long start, end = ULLONG_MAX, size;
86692f66f2SHari Bathini 
87692f66f2SHari Bathini 		/* get the start of the range */
88692f66f2SHari Bathini 		start = memparse(cur, &tmp);
89692f66f2SHari Bathini 		if (cur == tmp) {
90692f66f2SHari Bathini 			pr_warn("crashkernel: Memory value expected\n");
91692f66f2SHari Bathini 			return -EINVAL;
92692f66f2SHari Bathini 		}
93692f66f2SHari Bathini 		cur = tmp;
94692f66f2SHari Bathini 		if (*cur != '-') {
95692f66f2SHari Bathini 			pr_warn("crashkernel: '-' expected\n");
96692f66f2SHari Bathini 			return -EINVAL;
97692f66f2SHari Bathini 		}
98692f66f2SHari Bathini 		cur++;
99692f66f2SHari Bathini 
100692f66f2SHari Bathini 		/* if no ':' is here, than we read the end */
101692f66f2SHari Bathini 		if (*cur != ':') {
102692f66f2SHari Bathini 			end = memparse(cur, &tmp);
103692f66f2SHari Bathini 			if (cur == tmp) {
104692f66f2SHari Bathini 				pr_warn("crashkernel: Memory value expected\n");
105692f66f2SHari Bathini 				return -EINVAL;
106692f66f2SHari Bathini 			}
107692f66f2SHari Bathini 			cur = tmp;
108692f66f2SHari Bathini 			if (end <= start) {
109692f66f2SHari Bathini 				pr_warn("crashkernel: end <= start\n");
110692f66f2SHari Bathini 				return -EINVAL;
111692f66f2SHari Bathini 			}
112692f66f2SHari Bathini 		}
113692f66f2SHari Bathini 
114692f66f2SHari Bathini 		if (*cur != ':') {
115692f66f2SHari Bathini 			pr_warn("crashkernel: ':' expected\n");
116692f66f2SHari Bathini 			return -EINVAL;
117692f66f2SHari Bathini 		}
118692f66f2SHari Bathini 		cur++;
119692f66f2SHari Bathini 
120692f66f2SHari Bathini 		size = memparse(cur, &tmp);
121692f66f2SHari Bathini 		if (cur == tmp) {
122692f66f2SHari Bathini 			pr_warn("Memory value expected\n");
123692f66f2SHari Bathini 			return -EINVAL;
124692f66f2SHari Bathini 		}
125692f66f2SHari Bathini 		cur = tmp;
12646d36b1bSTao Liu 		if (size >= total_mem) {
127692f66f2SHari Bathini 			pr_warn("crashkernel: invalid size\n");
128692f66f2SHari Bathini 			return -EINVAL;
129692f66f2SHari Bathini 		}
130692f66f2SHari Bathini 
131692f66f2SHari Bathini 		/* match ? */
13246d36b1bSTao Liu 		if (total_mem >= start && total_mem < end) {
133692f66f2SHari Bathini 			*crash_size = size;
134692f66f2SHari Bathini 			break;
135692f66f2SHari Bathini 		}
136692f66f2SHari Bathini 	} while (*cur++ == ',');
137692f66f2SHari Bathini 
138692f66f2SHari Bathini 	if (*crash_size > 0) {
139692f66f2SHari Bathini 		while (*cur && *cur != ' ' && *cur != '@')
140692f66f2SHari Bathini 			cur++;
141692f66f2SHari Bathini 		if (*cur == '@') {
142692f66f2SHari Bathini 			cur++;
143692f66f2SHari Bathini 			*crash_base = memparse(cur, &tmp);
144692f66f2SHari Bathini 			if (cur == tmp) {
145692f66f2SHari Bathini 				pr_warn("Memory value expected after '@'\n");
146692f66f2SHari Bathini 				return -EINVAL;
147692f66f2SHari Bathini 			}
148692f66f2SHari Bathini 		}
149de40ccefSDave Young 	} else
150de40ccefSDave Young 		pr_info("crashkernel size resulted in zero bytes\n");
151692f66f2SHari Bathini 
152692f66f2SHari Bathini 	return 0;
153692f66f2SHari Bathini }
154692f66f2SHari Bathini 
155692f66f2SHari Bathini /*
156692f66f2SHari Bathini  * That function parses "simple" (old) crashkernel command lines like
157692f66f2SHari Bathini  *
158692f66f2SHari Bathini  *	crashkernel=size[@offset]
159692f66f2SHari Bathini  *
160692f66f2SHari Bathini  * It returns 0 on success and -EINVAL on failure.
161692f66f2SHari Bathini  */
162692f66f2SHari Bathini static int __init parse_crashkernel_simple(char *cmdline,
163692f66f2SHari Bathini 					   unsigned long long *crash_size,
164692f66f2SHari Bathini 					   unsigned long long *crash_base)
165692f66f2SHari Bathini {
166692f66f2SHari Bathini 	char *cur = cmdline;
167692f66f2SHari Bathini 
168692f66f2SHari Bathini 	*crash_size = memparse(cmdline, &cur);
169692f66f2SHari Bathini 	if (cmdline == cur) {
170692f66f2SHari Bathini 		pr_warn("crashkernel: memory value expected\n");
171692f66f2SHari Bathini 		return -EINVAL;
172692f66f2SHari Bathini 	}
173692f66f2SHari Bathini 
174692f66f2SHari Bathini 	if (*cur == '@')
175692f66f2SHari Bathini 		*crash_base = memparse(cur+1, &cur);
176692f66f2SHari Bathini 	else if (*cur != ' ' && *cur != '\0') {
177692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
178692f66f2SHari Bathini 		return -EINVAL;
179692f66f2SHari Bathini 	}
180692f66f2SHari Bathini 
181692f66f2SHari Bathini 	return 0;
182692f66f2SHari Bathini }
183692f66f2SHari Bathini 
184692f66f2SHari Bathini #define SUFFIX_HIGH 0
185692f66f2SHari Bathini #define SUFFIX_LOW  1
186692f66f2SHari Bathini #define SUFFIX_NULL 2
187692f66f2SHari Bathini static __initdata char *suffix_tbl[] = {
188692f66f2SHari Bathini 	[SUFFIX_HIGH] = ",high",
189692f66f2SHari Bathini 	[SUFFIX_LOW]  = ",low",
190692f66f2SHari Bathini 	[SUFFIX_NULL] = NULL,
191692f66f2SHari Bathini };
192692f66f2SHari Bathini 
193692f66f2SHari Bathini /*
194692f66f2SHari Bathini  * That function parses "suffix"  crashkernel command lines like
195692f66f2SHari Bathini  *
196692f66f2SHari Bathini  *	crashkernel=size,[high|low]
197692f66f2SHari Bathini  *
198692f66f2SHari Bathini  * It returns 0 on success and -EINVAL on failure.
199692f66f2SHari Bathini  */
200692f66f2SHari Bathini static int __init parse_crashkernel_suffix(char *cmdline,
201692f66f2SHari Bathini 					   unsigned long long *crash_size,
202692f66f2SHari Bathini 					   const char *suffix)
203692f66f2SHari Bathini {
204692f66f2SHari Bathini 	char *cur = cmdline;
205692f66f2SHari Bathini 
206692f66f2SHari Bathini 	*crash_size = memparse(cmdline, &cur);
207692f66f2SHari Bathini 	if (cmdline == cur) {
208692f66f2SHari Bathini 		pr_warn("crashkernel: memory value expected\n");
209692f66f2SHari Bathini 		return -EINVAL;
210692f66f2SHari Bathini 	}
211692f66f2SHari Bathini 
212692f66f2SHari Bathini 	/* check with suffix */
213692f66f2SHari Bathini 	if (strncmp(cur, suffix, strlen(suffix))) {
214692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
215692f66f2SHari Bathini 		return -EINVAL;
216692f66f2SHari Bathini 	}
217692f66f2SHari Bathini 	cur += strlen(suffix);
218692f66f2SHari Bathini 	if (*cur != ' ' && *cur != '\0') {
219692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
220692f66f2SHari Bathini 		return -EINVAL;
221692f66f2SHari Bathini 	}
222692f66f2SHari Bathini 
223692f66f2SHari Bathini 	return 0;
224692f66f2SHari Bathini }
225692f66f2SHari Bathini 
226692f66f2SHari Bathini static __init char *get_last_crashkernel(char *cmdline,
227692f66f2SHari Bathini 			     const char *name,
228692f66f2SHari Bathini 			     const char *suffix)
229692f66f2SHari Bathini {
230692f66f2SHari Bathini 	char *p = cmdline, *ck_cmdline = NULL;
231692f66f2SHari Bathini 
232692f66f2SHari Bathini 	/* find crashkernel and use the last one if there are more */
233692f66f2SHari Bathini 	p = strstr(p, name);
234692f66f2SHari Bathini 	while (p) {
235692f66f2SHari Bathini 		char *end_p = strchr(p, ' ');
236692f66f2SHari Bathini 		char *q;
237692f66f2SHari Bathini 
238692f66f2SHari Bathini 		if (!end_p)
239692f66f2SHari Bathini 			end_p = p + strlen(p);
240692f66f2SHari Bathini 
241692f66f2SHari Bathini 		if (!suffix) {
242692f66f2SHari Bathini 			int i;
243692f66f2SHari Bathini 
244692f66f2SHari Bathini 			/* skip the one with any known suffix */
245692f66f2SHari Bathini 			for (i = 0; suffix_tbl[i]; i++) {
246692f66f2SHari Bathini 				q = end_p - strlen(suffix_tbl[i]);
247692f66f2SHari Bathini 				if (!strncmp(q, suffix_tbl[i],
248692f66f2SHari Bathini 					     strlen(suffix_tbl[i])))
249692f66f2SHari Bathini 					goto next;
250692f66f2SHari Bathini 			}
251692f66f2SHari Bathini 			ck_cmdline = p;
252692f66f2SHari Bathini 		} else {
253692f66f2SHari Bathini 			q = end_p - strlen(suffix);
254692f66f2SHari Bathini 			if (!strncmp(q, suffix, strlen(suffix)))
255692f66f2SHari Bathini 				ck_cmdline = p;
256692f66f2SHari Bathini 		}
257692f66f2SHari Bathini next:
258692f66f2SHari Bathini 		p = strstr(p+1, name);
259692f66f2SHari Bathini 	}
260692f66f2SHari Bathini 
261692f66f2SHari Bathini 	return ck_cmdline;
262692f66f2SHari Bathini }
263692f66f2SHari Bathini 
264692f66f2SHari Bathini static int __init __parse_crashkernel(char *cmdline,
265692f66f2SHari Bathini 			     unsigned long long system_ram,
266692f66f2SHari Bathini 			     unsigned long long *crash_size,
267692f66f2SHari Bathini 			     unsigned long long *crash_base,
268692f66f2SHari Bathini 			     const char *suffix)
269692f66f2SHari Bathini {
270692f66f2SHari Bathini 	char *first_colon, *first_space;
271692f66f2SHari Bathini 	char *ck_cmdline;
272a6304272SBaoquan He 	char *name = "crashkernel=";
273692f66f2SHari Bathini 
274692f66f2SHari Bathini 	BUG_ON(!crash_size || !crash_base);
275692f66f2SHari Bathini 	*crash_size = 0;
276692f66f2SHari Bathini 	*crash_base = 0;
277692f66f2SHari Bathini 
278692f66f2SHari Bathini 	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
279692f66f2SHari Bathini 	if (!ck_cmdline)
2802e5920bbSZhen Lei 		return -ENOENT;
281692f66f2SHari Bathini 
282692f66f2SHari Bathini 	ck_cmdline += strlen(name);
283692f66f2SHari Bathini 
284692f66f2SHari Bathini 	if (suffix)
285692f66f2SHari Bathini 		return parse_crashkernel_suffix(ck_cmdline, crash_size,
286692f66f2SHari Bathini 				suffix);
287692f66f2SHari Bathini 	/*
288692f66f2SHari Bathini 	 * if the commandline contains a ':', then that's the extended
289692f66f2SHari Bathini 	 * syntax -- if not, it must be the classic syntax
290692f66f2SHari Bathini 	 */
291692f66f2SHari Bathini 	first_colon = strchr(ck_cmdline, ':');
292692f66f2SHari Bathini 	first_space = strchr(ck_cmdline, ' ');
293692f66f2SHari Bathini 	if (first_colon && (!first_space || first_colon < first_space))
294692f66f2SHari Bathini 		return parse_crashkernel_mem(ck_cmdline, system_ram,
295692f66f2SHari Bathini 				crash_size, crash_base);
296692f66f2SHari Bathini 
297692f66f2SHari Bathini 	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
298692f66f2SHari Bathini }
299692f66f2SHari Bathini 
300692f66f2SHari Bathini /*
301692f66f2SHari Bathini  * That function is the entry point for command line parsing and should be
302692f66f2SHari Bathini  * called from the arch-specific code.
30370916e9cSBaoquan He  *
30470916e9cSBaoquan He  * If crashkernel=,high|low is supported on architecture, non-NULL values
30570916e9cSBaoquan He  * should be passed to parameters 'low_size' and 'high'.
306692f66f2SHari Bathini  */
307692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline,
308692f66f2SHari Bathini 			     unsigned long long system_ram,
309692f66f2SHari Bathini 			     unsigned long long *crash_size,
310a9e1a3d8SBaoquan He 			     unsigned long long *crash_base,
311a9e1a3d8SBaoquan He 			     unsigned long long *low_size,
312a9e1a3d8SBaoquan He 			     bool *high)
313692f66f2SHari Bathini {
314a9e1a3d8SBaoquan He 	int ret;
315a9e1a3d8SBaoquan He 
316a9e1a3d8SBaoquan He 	/* crashkernel=X[@offset] */
317a9e1a3d8SBaoquan He 	ret = __parse_crashkernel(cmdline, system_ram, crash_size,
318a9e1a3d8SBaoquan He 				crash_base, NULL);
31970916e9cSBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
32070916e9cSBaoquan He 	/*
32170916e9cSBaoquan He 	 * If non-NULL 'high' passed in and no normal crashkernel
32270916e9cSBaoquan He 	 * setting detected, try parsing crashkernel=,high|low.
32370916e9cSBaoquan He 	 */
32470916e9cSBaoquan He 	if (high && ret == -ENOENT) {
32570916e9cSBaoquan He 		ret = __parse_crashkernel(cmdline, 0, crash_size,
32670916e9cSBaoquan He 				crash_base, suffix_tbl[SUFFIX_HIGH]);
32770916e9cSBaoquan He 		if (ret || !*crash_size)
32870916e9cSBaoquan He 			return -EINVAL;
329a9e1a3d8SBaoquan He 
33070916e9cSBaoquan He 		/*
33170916e9cSBaoquan He 		 * crashkernel=Y,low can be specified or not, but invalid value
33270916e9cSBaoquan He 		 * is not allowed.
33370916e9cSBaoquan He 		 */
33470916e9cSBaoquan He 		ret = __parse_crashkernel(cmdline, 0, low_size,
33570916e9cSBaoquan He 				crash_base, suffix_tbl[SUFFIX_LOW]);
33670916e9cSBaoquan He 		if (ret == -ENOENT) {
33770916e9cSBaoquan He 			*low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
33870916e9cSBaoquan He 			ret = 0;
33970916e9cSBaoquan He 		} else if (ret) {
34070916e9cSBaoquan He 			return ret;
34170916e9cSBaoquan He 		}
34270916e9cSBaoquan He 
34370916e9cSBaoquan He 		*high = true;
34470916e9cSBaoquan He 	}
34570916e9cSBaoquan He #endif
34670916e9cSBaoquan He 	if (!*crash_size)
34770916e9cSBaoquan He 		ret = -EINVAL;
34870916e9cSBaoquan He 
34970916e9cSBaoquan He 	return ret;
350692f66f2SHari Bathini }
351692f66f2SHari Bathini 
35271d2bcecSPhilipp Rudo /*
35371d2bcecSPhilipp Rudo  * Add a dummy early_param handler to mark crashkernel= as a known command line
35471d2bcecSPhilipp Rudo  * parameter and suppress incorrect warnings in init/main.c.
35571d2bcecSPhilipp Rudo  */
35671d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg)
35771d2bcecSPhilipp Rudo {
35871d2bcecSPhilipp Rudo 	return 0;
35971d2bcecSPhilipp Rudo }
36071d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy);
36171d2bcecSPhilipp Rudo 
3620ab97169SBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
3630ab97169SBaoquan He static int __init reserve_crashkernel_low(unsigned long long low_size)
3640ab97169SBaoquan He {
3650ab97169SBaoquan He #ifdef CONFIG_64BIT
3660ab97169SBaoquan He 	unsigned long long low_base;
3670ab97169SBaoquan He 
3680ab97169SBaoquan He 	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
3690ab97169SBaoquan He 	if (!low_base) {
3700ab97169SBaoquan He 		pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
3710ab97169SBaoquan He 		return -ENOMEM;
3720ab97169SBaoquan He 	}
3730ab97169SBaoquan He 
3740ab97169SBaoquan He 	pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
3750ab97169SBaoquan He 		low_base, low_base + low_size, low_size >> 20);
3760ab97169SBaoquan He 
3770ab97169SBaoquan He 	crashk_low_res.start = low_base;
3780ab97169SBaoquan He 	crashk_low_res.end   = low_base + low_size - 1;
3790ab97169SBaoquan He #endif
3800ab97169SBaoquan He 	return 0;
3810ab97169SBaoquan He }
3820ab97169SBaoquan He 
3830ab97169SBaoquan He void __init reserve_crashkernel_generic(char *cmdline,
3840ab97169SBaoquan He 			     unsigned long long crash_size,
3850ab97169SBaoquan He 			     unsigned long long crash_base,
3860ab97169SBaoquan He 			     unsigned long long crash_low_size,
3870ab97169SBaoquan He 			     bool high)
3880ab97169SBaoquan He {
3890ab97169SBaoquan He 	unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0;
3900ab97169SBaoquan He 	bool fixed_base = false;
3910ab97169SBaoquan He 
3920ab97169SBaoquan He 	/* User specifies base address explicitly. */
3930ab97169SBaoquan He 	if (crash_base) {
3940ab97169SBaoquan He 		fixed_base = true;
3950ab97169SBaoquan He 		search_base = crash_base;
3960ab97169SBaoquan He 		search_end = crash_base + crash_size;
3970ab97169SBaoquan He 	} else if (high) {
3980ab97169SBaoquan He 		search_base = CRASH_ADDR_LOW_MAX;
3990ab97169SBaoquan He 		search_end = CRASH_ADDR_HIGH_MAX;
4000ab97169SBaoquan He 	}
4010ab97169SBaoquan He 
4020ab97169SBaoquan He retry:
4030ab97169SBaoquan He 	crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
4040ab97169SBaoquan He 					       search_base, search_end);
4050ab97169SBaoquan He 	if (!crash_base) {
4060ab97169SBaoquan He 		/*
4070ab97169SBaoquan He 		 * For crashkernel=size[KMG]@offset[KMG], print out failure
4080ab97169SBaoquan He 		 * message if can't reserve the specified region.
4090ab97169SBaoquan He 		 */
4100ab97169SBaoquan He 		if (fixed_base) {
4110ab97169SBaoquan He 			pr_warn("crashkernel reservation failed - memory is in use.\n");
4120ab97169SBaoquan He 			return;
4130ab97169SBaoquan He 		}
4140ab97169SBaoquan He 
4150ab97169SBaoquan He 		/*
4160ab97169SBaoquan He 		 * For crashkernel=size[KMG], if the first attempt was for
4170ab97169SBaoquan He 		 * low memory, fall back to high memory, the minimum required
4180ab97169SBaoquan He 		 * low memory will be reserved later.
4190ab97169SBaoquan He 		 */
4200ab97169SBaoquan He 		if (!high && search_end == CRASH_ADDR_LOW_MAX) {
4210ab97169SBaoquan He 			search_end = CRASH_ADDR_HIGH_MAX;
4220ab97169SBaoquan He 			search_base = CRASH_ADDR_LOW_MAX;
4230ab97169SBaoquan He 			crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
4240ab97169SBaoquan He 			goto retry;
4250ab97169SBaoquan He 		}
4260ab97169SBaoquan He 
4270ab97169SBaoquan He 		/*
4280ab97169SBaoquan He 		 * For crashkernel=size[KMG],high, if the first attempt was
4290ab97169SBaoquan He 		 * for high memory, fall back to low memory.
4300ab97169SBaoquan He 		 */
4310ab97169SBaoquan He 		if (high && search_end == CRASH_ADDR_HIGH_MAX) {
4320ab97169SBaoquan He 			search_end = CRASH_ADDR_LOW_MAX;
4330ab97169SBaoquan He 			search_base = 0;
4340ab97169SBaoquan He 			goto retry;
4350ab97169SBaoquan He 		}
4360ab97169SBaoquan He 		pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
4370ab97169SBaoquan He 			crash_size);
4380ab97169SBaoquan He 		return;
4390ab97169SBaoquan He 	}
4400ab97169SBaoquan He 
4411dd11e97SYuntao Wang 	if ((crash_base >= CRASH_ADDR_LOW_MAX) &&
4420ab97169SBaoquan He 	     crash_low_size && reserve_crashkernel_low(crash_low_size)) {
4430ab97169SBaoquan He 		memblock_phys_free(crash_base, crash_size);
4440ab97169SBaoquan He 		return;
4450ab97169SBaoquan He 	}
4460ab97169SBaoquan He 
4470ab97169SBaoquan He 	pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
4480ab97169SBaoquan He 		crash_base, crash_base + crash_size, crash_size >> 20);
4490ab97169SBaoquan He 
4500ab97169SBaoquan He 	/*
4510ab97169SBaoquan He 	 * The crashkernel memory will be removed from the kernel linear
4520ab97169SBaoquan He 	 * map. Inform kmemleak so that it won't try to access it.
4530ab97169SBaoquan He 	 */
4540ab97169SBaoquan He 	kmemleak_ignore_phys(crash_base);
4550ab97169SBaoquan He 	if (crashk_low_res.end)
4560ab97169SBaoquan He 		kmemleak_ignore_phys(crashk_low_res.start);
4570ab97169SBaoquan He 
4580ab97169SBaoquan He 	crashk_res.start = crash_base;
4590ab97169SBaoquan He 	crashk_res.end = crash_base + crash_size - 1;
4600ab97169SBaoquan He }
4614a693ce6SHuacai Chen 
4624a693ce6SHuacai Chen static __init int insert_crashkernel_resources(void)
4634a693ce6SHuacai Chen {
4644a693ce6SHuacai Chen 	if (crashk_res.start < crashk_res.end)
4654a693ce6SHuacai Chen 		insert_resource(&iomem_resource, &crashk_res);
4664a693ce6SHuacai Chen 
4674a693ce6SHuacai Chen 	if (crashk_low_res.start < crashk_low_res.end)
4684a693ce6SHuacai Chen 		insert_resource(&iomem_resource, &crashk_low_res);
4694a693ce6SHuacai Chen 
4704a693ce6SHuacai Chen 	return 0;
4714a693ce6SHuacai Chen }
4724a693ce6SHuacai Chen early_initcall(insert_crashkernel_resources);
4730ab97169SBaoquan He #endif
4740ab97169SBaoquan He 
4756f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
4766f991cc3SEric DeVolder 			  void **addr, unsigned long *sz)
4776f991cc3SEric DeVolder {
4786f991cc3SEric DeVolder 	Elf64_Ehdr *ehdr;
4796f991cc3SEric DeVolder 	Elf64_Phdr *phdr;
4806f991cc3SEric DeVolder 	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
4816f991cc3SEric DeVolder 	unsigned char *buf;
4826f991cc3SEric DeVolder 	unsigned int cpu, i;
4836f991cc3SEric DeVolder 	unsigned long long notes_addr;
4846f991cc3SEric DeVolder 	unsigned long mstart, mend;
4856f991cc3SEric DeVolder 
4866f991cc3SEric DeVolder 	/* extra phdr for vmcoreinfo ELF note */
4876f991cc3SEric DeVolder 	nr_phdr = nr_cpus + 1;
4886f991cc3SEric DeVolder 	nr_phdr += mem->nr_ranges;
4896f991cc3SEric DeVolder 
4906f991cc3SEric DeVolder 	/*
4916f991cc3SEric DeVolder 	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
4926f991cc3SEric DeVolder 	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
4936f991cc3SEric DeVolder 	 * I think this is required by tools like gdb. So same physical
4946f991cc3SEric DeVolder 	 * memory will be mapped in two ELF headers. One will contain kernel
4956f991cc3SEric DeVolder 	 * text virtual addresses and other will have __va(physical) addresses.
4966f991cc3SEric DeVolder 	 */
4976f991cc3SEric DeVolder 
4986f991cc3SEric DeVolder 	nr_phdr++;
4996f991cc3SEric DeVolder 	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
5006f991cc3SEric DeVolder 	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
5016f991cc3SEric DeVolder 
5026f991cc3SEric DeVolder 	buf = vzalloc(elf_sz);
5036f991cc3SEric DeVolder 	if (!buf)
5046f991cc3SEric DeVolder 		return -ENOMEM;
5056f991cc3SEric DeVolder 
5066f991cc3SEric DeVolder 	ehdr = (Elf64_Ehdr *)buf;
5076f991cc3SEric DeVolder 	phdr = (Elf64_Phdr *)(ehdr + 1);
5086f991cc3SEric DeVolder 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
5096f991cc3SEric DeVolder 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
5106f991cc3SEric DeVolder 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
5116f991cc3SEric DeVolder 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
5126f991cc3SEric DeVolder 	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
5136f991cc3SEric DeVolder 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
5146f991cc3SEric DeVolder 	ehdr->e_type = ET_CORE;
5156f991cc3SEric DeVolder 	ehdr->e_machine = ELF_ARCH;
5166f991cc3SEric DeVolder 	ehdr->e_version = EV_CURRENT;
5176f991cc3SEric DeVolder 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
5186f991cc3SEric DeVolder 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
5196f991cc3SEric DeVolder 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
5206f991cc3SEric DeVolder 
521a396d0f8SEric DeVolder 	/* Prepare one phdr of type PT_NOTE for each possible CPU */
522a396d0f8SEric DeVolder 	for_each_possible_cpu(cpu) {
5236f991cc3SEric DeVolder 		phdr->p_type = PT_NOTE;
5246f991cc3SEric DeVolder 		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
5256f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = notes_addr;
5266f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
5276f991cc3SEric DeVolder 		(ehdr->e_phnum)++;
5286f991cc3SEric DeVolder 		phdr++;
5296f991cc3SEric DeVolder 	}
5306f991cc3SEric DeVolder 
5316f991cc3SEric DeVolder 	/* Prepare one PT_NOTE header for vmcoreinfo */
5326f991cc3SEric DeVolder 	phdr->p_type = PT_NOTE;
5336f991cc3SEric DeVolder 	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
5346f991cc3SEric DeVolder 	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
5356f991cc3SEric DeVolder 	(ehdr->e_phnum)++;
5366f991cc3SEric DeVolder 	phdr++;
5376f991cc3SEric DeVolder 
5386f991cc3SEric DeVolder 	/* Prepare PT_LOAD type program header for kernel text region */
5396f991cc3SEric DeVolder 	if (need_kernel_map) {
5406f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
5416f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
5426f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) _text;
5436f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = _end - _text;
5446f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
5456f991cc3SEric DeVolder 		ehdr->e_phnum++;
5466f991cc3SEric DeVolder 		phdr++;
5476f991cc3SEric DeVolder 	}
5486f991cc3SEric DeVolder 
5496f991cc3SEric DeVolder 	/* Go through all the ranges in mem->ranges[] and prepare phdr */
5506f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
5516f991cc3SEric DeVolder 		mstart = mem->ranges[i].start;
5526f991cc3SEric DeVolder 		mend = mem->ranges[i].end;
5536f991cc3SEric DeVolder 
5546f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
5556f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
5566f991cc3SEric DeVolder 		phdr->p_offset  = mstart;
5576f991cc3SEric DeVolder 
5586f991cc3SEric DeVolder 		phdr->p_paddr = mstart;
5596f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) __va(mstart);
5606f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
5616f991cc3SEric DeVolder 		phdr->p_align = 0;
5626f991cc3SEric DeVolder 		ehdr->e_phnum++;
563a85ee18cSBaoquan He #ifdef CONFIG_KEXEC_FILE
564a85ee18cSBaoquan He 		kexec_dprintk("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
5656f991cc3SEric DeVolder 			      phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
5666f991cc3SEric DeVolder 			      ehdr->e_phnum, phdr->p_offset);
567a85ee18cSBaoquan He #endif
5686f991cc3SEric DeVolder 		phdr++;
5696f991cc3SEric DeVolder 	}
5706f991cc3SEric DeVolder 
5716f991cc3SEric DeVolder 	*addr = buf;
5726f991cc3SEric DeVolder 	*sz = elf_sz;
5736f991cc3SEric DeVolder 	return 0;
5746f991cc3SEric DeVolder }
5756f991cc3SEric DeVolder 
5766f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem,
5776f991cc3SEric DeVolder 			    unsigned long long mstart, unsigned long long mend)
5786f991cc3SEric DeVolder {
5796dff3159SYuntao Wang 	int i;
5806f991cc3SEric DeVolder 	unsigned long long start, end, p_start, p_end;
5816f991cc3SEric DeVolder 
5826f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
5836f991cc3SEric DeVolder 		start = mem->ranges[i].start;
5846f991cc3SEric DeVolder 		end = mem->ranges[i].end;
5856f991cc3SEric DeVolder 		p_start = mstart;
5866f991cc3SEric DeVolder 		p_end = mend;
5876f991cc3SEric DeVolder 
5886dff3159SYuntao Wang 		if (p_start > end)
5896f991cc3SEric DeVolder 			continue;
5906f991cc3SEric DeVolder 
5916dff3159SYuntao Wang 		/*
5926dff3159SYuntao Wang 		 * Because the memory ranges in mem->ranges are stored in
5936dff3159SYuntao Wang 		 * ascending order, when we detect `p_end < start`, we can
5946dff3159SYuntao Wang 		 * immediately exit the for loop, as the subsequent memory
5956dff3159SYuntao Wang 		 * ranges will definitely be outside the range we are looking
5966dff3159SYuntao Wang 		 * for.
5976dff3159SYuntao Wang 		 */
5986dff3159SYuntao Wang 		if (p_end < start)
5996dff3159SYuntao Wang 			break;
6006dff3159SYuntao Wang 
6016f991cc3SEric DeVolder 		/* Truncate any area outside of range */
6026dff3159SYuntao Wang 		if (p_start < start)
6036f991cc3SEric DeVolder 			p_start = start;
6046dff3159SYuntao Wang 		if (p_end > end)
6056f991cc3SEric DeVolder 			p_end = end;
6066f991cc3SEric DeVolder 
6076f991cc3SEric DeVolder 		/* Found completely overlapping range */
6086f991cc3SEric DeVolder 		if (p_start == start && p_end == end) {
6096dff3159SYuntao Wang 			memmove(&mem->ranges[i], &mem->ranges[i + 1],
6106dff3159SYuntao Wang 				(mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i]));
6116f991cc3SEric DeVolder 			i--;
6126f991cc3SEric DeVolder 			mem->nr_ranges--;
6136dff3159SYuntao Wang 		} else if (p_start > start && p_end < end) {
6146f991cc3SEric DeVolder 			/* Split original range */
6156dff3159SYuntao Wang 			if (mem->nr_ranges >= mem->max_nr_ranges)
6166dff3159SYuntao Wang 				return -ENOMEM;
6176dff3159SYuntao Wang 
6186dff3159SYuntao Wang 			memmove(&mem->ranges[i + 2], &mem->ranges[i + 1],
6196dff3159SYuntao Wang 				(mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i]));
6206dff3159SYuntao Wang 
6216f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
6226dff3159SYuntao Wang 			mem->ranges[i + 1].start = p_end + 1;
6236dff3159SYuntao Wang 			mem->ranges[i + 1].end = end;
6246dff3159SYuntao Wang 
6256dff3159SYuntao Wang 			i++;
6266dff3159SYuntao Wang 			mem->nr_ranges++;
6276f991cc3SEric DeVolder 		} else if (p_start != start)
6286f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
6296f991cc3SEric DeVolder 		else
6306f991cc3SEric DeVolder 			mem->ranges[i].start = p_end + 1;
6316f991cc3SEric DeVolder 	}
6326f991cc3SEric DeVolder 
6336f991cc3SEric DeVolder 	return 0;
6346f991cc3SEric DeVolder }
6356f991cc3SEric DeVolder 
63651dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
637692f66f2SHari Bathini 			  void *data, size_t data_len)
638692f66f2SHari Bathini {
63951dbd925SHari Bathini 	struct elf_note *note = (struct elf_note *)buf;
640692f66f2SHari Bathini 
64151dbd925SHari Bathini 	note->n_namesz = strlen(name) + 1;
64251dbd925SHari Bathini 	note->n_descsz = data_len;
64351dbd925SHari Bathini 	note->n_type   = type;
64451dbd925SHari Bathini 	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
64551dbd925SHari Bathini 	memcpy(buf, name, note->n_namesz);
64651dbd925SHari Bathini 	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
64751dbd925SHari Bathini 	memcpy(buf, data, data_len);
64851dbd925SHari Bathini 	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
649692f66f2SHari Bathini 
650692f66f2SHari Bathini 	return buf;
651692f66f2SHari Bathini }
652692f66f2SHari Bathini 
65351dbd925SHari Bathini void final_note(Elf_Word *buf)
654692f66f2SHari Bathini {
65551dbd925SHari Bathini 	memset(buf, 0, sizeof(struct elf_note));
656692f66f2SHari Bathini }
657692f66f2SHari Bathini 
658692f66f2SHari Bathini static void update_vmcoreinfo_note(void)
659692f66f2SHari Bathini {
660692f66f2SHari Bathini 	u32 *buf = vmcoreinfo_note;
661692f66f2SHari Bathini 
662692f66f2SHari Bathini 	if (!vmcoreinfo_size)
663692f66f2SHari Bathini 		return;
664692f66f2SHari Bathini 	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
665692f66f2SHari Bathini 			      vmcoreinfo_size);
666692f66f2SHari Bathini 	final_note(buf);
667692f66f2SHari Bathini }
668692f66f2SHari Bathini 
6691229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr)
6701229384fSXunlei Pang {
6711229384fSXunlei Pang 	if (ptr)
6721229384fSXunlei Pang 		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
6731229384fSXunlei Pang 
6741229384fSXunlei Pang 	vmcoreinfo_data_safecopy = ptr;
6751229384fSXunlei Pang }
6761229384fSXunlei Pang 
677692f66f2SHari Bathini void crash_save_vmcoreinfo(void)
678692f66f2SHari Bathini {
679203e9e41SXunlei Pang 	if (!vmcoreinfo_note)
680203e9e41SXunlei Pang 		return;
681203e9e41SXunlei Pang 
6821229384fSXunlei Pang 	/* Use the safe copy to generate vmcoreinfo note if have */
6831229384fSXunlei Pang 	if (vmcoreinfo_data_safecopy)
6841229384fSXunlei Pang 		vmcoreinfo_data = vmcoreinfo_data_safecopy;
6851229384fSXunlei Pang 
68691bc9aafSArnd Bergmann 	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
687692f66f2SHari Bathini 	update_vmcoreinfo_note();
688692f66f2SHari Bathini }
689692f66f2SHari Bathini 
690692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...)
691692f66f2SHari Bathini {
692692f66f2SHari Bathini 	va_list args;
693692f66f2SHari Bathini 	char buf[0x50];
694692f66f2SHari Bathini 	size_t r;
695692f66f2SHari Bathini 
696692f66f2SHari Bathini 	va_start(args, fmt);
697692f66f2SHari Bathini 	r = vscnprintf(buf, sizeof(buf), fmt, args);
698692f66f2SHari Bathini 	va_end(args);
699692f66f2SHari Bathini 
7005203f499SXunlei Pang 	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
701692f66f2SHari Bathini 
702692f66f2SHari Bathini 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
703692f66f2SHari Bathini 
704692f66f2SHari Bathini 	vmcoreinfo_size += r;
70508fc35f3SStephen Brennan 
70608fc35f3SStephen Brennan 	WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES,
70708fc35f3SStephen Brennan 		  "vmcoreinfo data exceeds allocated size, truncating");
708692f66f2SHari Bathini }
709692f66f2SHari Bathini 
710692f66f2SHari Bathini /*
711692f66f2SHari Bathini  * provide an empty default implementation here -- architecture
712692f66f2SHari Bathini  * code may override this
713692f66f2SHari Bathini  */
714692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void)
715692f66f2SHari Bathini {}
716692f66f2SHari Bathini 
717692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void)
718692f66f2SHari Bathini {
719203e9e41SXunlei Pang 	return __pa(vmcoreinfo_note);
720692f66f2SHari Bathini }
72143d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note);
722692f66f2SHari Bathini 
723692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void)
724692f66f2SHari Bathini {
725203e9e41SXunlei Pang 	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
726203e9e41SXunlei Pang 	if (!vmcoreinfo_data) {
727203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
728203e9e41SXunlei Pang 		return -ENOMEM;
729203e9e41SXunlei Pang 	}
730203e9e41SXunlei Pang 
731203e9e41SXunlei Pang 	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
732203e9e41SXunlei Pang 						GFP_KERNEL | __GFP_ZERO);
733203e9e41SXunlei Pang 	if (!vmcoreinfo_note) {
734203e9e41SXunlei Pang 		free_page((unsigned long)vmcoreinfo_data);
735203e9e41SXunlei Pang 		vmcoreinfo_data = NULL;
736203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
737203e9e41SXunlei Pang 		return -ENOMEM;
738203e9e41SXunlei Pang 	}
739203e9e41SXunlei Pang 
740692f66f2SHari Bathini 	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
74144e8a5e9SStephen Boyd 	VMCOREINFO_BUILD_ID();
742692f66f2SHari Bathini 	VMCOREINFO_PAGESIZE(PAGE_SIZE);
743692f66f2SHari Bathini 
744692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(init_uts_ns);
745ca4a9241SAlexander Egorenkov 	VMCOREINFO_OFFSET(uts_namespace, name);
746692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(node_online_map);
747692f66f2SHari Bathini #ifdef CONFIG_MMU
748eff4345eSOmar Sandoval 	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
749692f66f2SHari Bathini #endif
750692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(_stext);
751692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(vmap_area_list);
752692f66f2SHari Bathini 
753a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA
754692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(mem_map);
755692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(contig_page_data);
756692f66f2SHari Bathini #endif
757692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM
758a0b12803SKirill A. Shutemov 	VMCOREINFO_SYMBOL_ARRAY(mem_section);
759692f66f2SHari Bathini 	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
760692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(mem_section);
761692f66f2SHari Bathini 	VMCOREINFO_OFFSET(mem_section, section_mem_map);
7624f5aecdfSPingfan Liu 	VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
7631d50e5d0SBhupesh Sharma 	VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
764692f66f2SHari Bathini #endif
765692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(page);
766692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(pglist_data);
767692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(zone);
768692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(free_area);
769692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(list_head);
770692f66f2SHari Bathini 	VMCOREINFO_SIZE(nodemask_t);
771692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, flags);
772692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _refcount);
773692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, mapping);
774692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, lru);
775692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _mapcount);
776692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, private);
777692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, compound_head);
778692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_zones);
779692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, nr_zones);
78043b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM
781692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
782692f66f2SHari Bathini #endif
783692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
784692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
785692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_id);
786692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, free_area);
787692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, vm_stat);
788692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, spanned_pages);
789692f66f2SHari Bathini 	VMCOREINFO_OFFSET(free_area, free_list);
790692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, next);
791692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, prev);
792692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, va_start);
793692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, list);
794fd377218SKirill A. Shutemov 	VMCOREINFO_LENGTH(zone.free_area, NR_PAGE_ORDERS);
795692f66f2SHari Bathini 	log_buf_vmcoreinfo_setup();
796692f66f2SHari Bathini 	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
797692f66f2SHari Bathini 	VMCOREINFO_NUMBER(NR_FREE_PAGES);
798692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_lru);
799692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_private);
800692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_swapcache);
8011cbf29daSPetr Tesarik 	VMCOREINFO_NUMBER(PG_swapbacked);
802692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_slab);
803692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE
804692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_hwpoison);
805692f66f2SHari Bathini #endif
806692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_head_mask);
8076e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE	(~PG_buddy)
808692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
809692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
8109c5ccf2dSMatthew Wilcox (Oracle) 	VMCOREINFO_NUMBER(PG_hugetlb);
811e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE	(~PG_offline)
812e04b742fSDavid Hildenbrand 	VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
813692f66f2SHari Bathini #endif
814692f66f2SHari Bathini 
8155fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS
8165fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_names);
817f09bddbdSStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_num_syms);
8185fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_table);
8195fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_index);
8205fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE
8215fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_offsets);
8225fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_relative_base);
8235fd8fea9SStephen Brennan #else
8245fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_addresses);
8255fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */
8265fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */
8275fd8fea9SStephen Brennan 
828692f66f2SHari Bathini 	arch_crash_save_vmcoreinfo();
829692f66f2SHari Bathini 	update_vmcoreinfo_note();
830692f66f2SHari Bathini 
831692f66f2SHari Bathini 	return 0;
832692f66f2SHari Bathini }
833692f66f2SHari Bathini 
834692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init);
8356f991cc3SEric DeVolder 
8366f991cc3SEric DeVolder static int __init crash_notes_memory_init(void)
8376f991cc3SEric DeVolder {
8386f991cc3SEric DeVolder 	/* Allocate memory for saving cpu registers. */
8396f991cc3SEric DeVolder 	size_t size, align;
8406f991cc3SEric DeVolder 
8416f991cc3SEric DeVolder 	/*
8426f991cc3SEric DeVolder 	 * crash_notes could be allocated across 2 vmalloc pages when percpu
8436f991cc3SEric DeVolder 	 * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
8446f991cc3SEric DeVolder 	 * pages are also on 2 continuous physical pages. In this case the
8456f991cc3SEric DeVolder 	 * 2nd part of crash_notes in 2nd page could be lost since only the
8466f991cc3SEric DeVolder 	 * starting address and size of crash_notes are exported through sysfs.
8476f991cc3SEric DeVolder 	 * Here round up the size of crash_notes to the nearest power of two
8486f991cc3SEric DeVolder 	 * and pass it to __alloc_percpu as align value. This can make sure
8496f991cc3SEric DeVolder 	 * crash_notes is allocated inside one physical page.
8506f991cc3SEric DeVolder 	 */
8516f991cc3SEric DeVolder 	size = sizeof(note_buf_t);
8526f991cc3SEric DeVolder 	align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
8536f991cc3SEric DeVolder 
8546f991cc3SEric DeVolder 	/*
8556f991cc3SEric DeVolder 	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
8566f991cc3SEric DeVolder 	 * definitely will be in 2 pages with that.
8576f991cc3SEric DeVolder 	 */
8586f991cc3SEric DeVolder 	BUILD_BUG_ON(size > PAGE_SIZE);
8596f991cc3SEric DeVolder 
8606f991cc3SEric DeVolder 	crash_notes = __alloc_percpu(size, align);
8616f991cc3SEric DeVolder 	if (!crash_notes) {
8626f991cc3SEric DeVolder 		pr_warn("Memory allocation for saving cpu register states failed\n");
8636f991cc3SEric DeVolder 		return -ENOMEM;
8646f991cc3SEric DeVolder 	}
8656f991cc3SEric DeVolder 	return 0;
8666f991cc3SEric DeVolder }
8676f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init);
86824726275SEric DeVolder 
86924726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
87024726275SEric DeVolder #undef pr_fmt
87124726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt
872a72bbec7SEric DeVolder 
873a72bbec7SEric DeVolder /*
874e2a8f20dSBaoquan He  * Different than kexec/kdump loading/unloading/jumping/shrinking which
875e2a8f20dSBaoquan He  * usually rarely happen, there will be many crash hotplug events notified
876e2a8f20dSBaoquan He  * during one short period, e.g one memory board is hot added and memory
877e2a8f20dSBaoquan He  * regions are online. So mutex lock  __crash_hotplug_lock is used to
878e2a8f20dSBaoquan He  * serialize the crash hotplug handling specifically.
879e2a8f20dSBaoquan He  */
880*4e87ff59SAndrew Morton static DEFINE_MUTEX(__crash_hotplug_lock);
881e2a8f20dSBaoquan He #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock)
882e2a8f20dSBaoquan He #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock)
883e2a8f20dSBaoquan He 
884e2a8f20dSBaoquan He /*
885a72bbec7SEric DeVolder  * This routine utilized when the crash_hotplug sysfs node is read.
886a72bbec7SEric DeVolder  * It reflects the kernel's ability/permission to update the crash
887a72bbec7SEric DeVolder  * elfcorehdr directly.
888a72bbec7SEric DeVolder  */
889a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void)
890a72bbec7SEric DeVolder {
891a72bbec7SEric DeVolder 	int rc = 0;
892a72bbec7SEric DeVolder 
893e2a8f20dSBaoquan He 	crash_hotplug_lock();
894a72bbec7SEric DeVolder 	/* Obtain lock while reading crash information */
895a72bbec7SEric DeVolder 	if (!kexec_trylock()) {
896a72bbec7SEric DeVolder 		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
897e2a8f20dSBaoquan He 		crash_hotplug_unlock();
898a72bbec7SEric DeVolder 		return 0;
899a72bbec7SEric DeVolder 	}
900a72bbec7SEric DeVolder 	if (kexec_crash_image) {
901a72bbec7SEric DeVolder 		if (kexec_crash_image->file_mode)
902a72bbec7SEric DeVolder 			rc = 1;
903a72bbec7SEric DeVolder 		else
904a72bbec7SEric DeVolder 			rc = kexec_crash_image->update_elfcorehdr;
905a72bbec7SEric DeVolder 	}
906a72bbec7SEric DeVolder 	/* Release lock now that update complete */
907a72bbec7SEric DeVolder 	kexec_unlock();
908e2a8f20dSBaoquan He 	crash_hotplug_unlock();
909a72bbec7SEric DeVolder 
910a72bbec7SEric DeVolder 	return rc;
911a72bbec7SEric DeVolder }
912a72bbec7SEric DeVolder 
91324726275SEric DeVolder /*
91424726275SEric DeVolder  * To accurately reflect hot un/plug changes of cpu and memory resources
91524726275SEric DeVolder  * (including onling and offlining of those resources), the elfcorehdr
91624726275SEric DeVolder  * (which is passed to the crash kernel via the elfcorehdr= parameter)
91724726275SEric DeVolder  * must be updated with the new list of CPUs and memories.
91824726275SEric DeVolder  *
91924726275SEric DeVolder  * In order to make changes to elfcorehdr, two conditions are needed:
92024726275SEric DeVolder  * First, the segment containing the elfcorehdr must be large enough
92124726275SEric DeVolder  * to permit a growing number of resources; the elfcorehdr memory size
92224726275SEric DeVolder  * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES.
92324726275SEric DeVolder  * Second, purgatory must explicitly exclude the elfcorehdr from the
92424726275SEric DeVolder  * list of segments it checks (since the elfcorehdr changes and thus
92524726275SEric DeVolder  * would require an update to purgatory itself to update the digest).
92624726275SEric DeVolder  */
92724726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu)
92824726275SEric DeVolder {
92924726275SEric DeVolder 	struct kimage *image;
93024726275SEric DeVolder 
931e2a8f20dSBaoquan He 	crash_hotplug_lock();
93224726275SEric DeVolder 	/* Obtain lock while changing crash information */
93324726275SEric DeVolder 	if (!kexec_trylock()) {
93424726275SEric DeVolder 		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
935e2a8f20dSBaoquan He 		crash_hotplug_unlock();
93624726275SEric DeVolder 		return;
93724726275SEric DeVolder 	}
93824726275SEric DeVolder 
93924726275SEric DeVolder 	/* Check kdump is not loaded */
94024726275SEric DeVolder 	if (!kexec_crash_image)
94124726275SEric DeVolder 		goto out;
94224726275SEric DeVolder 
94324726275SEric DeVolder 	image = kexec_crash_image;
94424726275SEric DeVolder 
945a72bbec7SEric DeVolder 	/* Check that updating elfcorehdr is permitted */
946a72bbec7SEric DeVolder 	if (!(image->file_mode || image->update_elfcorehdr))
947a72bbec7SEric DeVolder 		goto out;
948a72bbec7SEric DeVolder 
94924726275SEric DeVolder 	if (hp_action == KEXEC_CRASH_HP_ADD_CPU ||
95024726275SEric DeVolder 		hp_action == KEXEC_CRASH_HP_REMOVE_CPU)
95124726275SEric DeVolder 		pr_debug("hp_action %u, cpu %u\n", hp_action, cpu);
95224726275SEric DeVolder 	else
95324726275SEric DeVolder 		pr_debug("hp_action %u\n", hp_action);
95424726275SEric DeVolder 
95524726275SEric DeVolder 	/*
95624726275SEric DeVolder 	 * The elfcorehdr_index is set to -1 when the struct kimage
95724726275SEric DeVolder 	 * is allocated. Find the segment containing the elfcorehdr,
95824726275SEric DeVolder 	 * if not already found.
95924726275SEric DeVolder 	 */
96024726275SEric DeVolder 	if (image->elfcorehdr_index < 0) {
96124726275SEric DeVolder 		unsigned long mem;
96224726275SEric DeVolder 		unsigned char *ptr;
96324726275SEric DeVolder 		unsigned int n;
96424726275SEric DeVolder 
96524726275SEric DeVolder 		for (n = 0; n < image->nr_segments; n++) {
96624726275SEric DeVolder 			mem = image->segment[n].mem;
96724726275SEric DeVolder 			ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
96824726275SEric DeVolder 			if (ptr) {
96924726275SEric DeVolder 				/* The segment containing elfcorehdr */
97024726275SEric DeVolder 				if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
97124726275SEric DeVolder 					image->elfcorehdr_index = (int)n;
97224726275SEric DeVolder 				kunmap_local(ptr);
97324726275SEric DeVolder 			}
97424726275SEric DeVolder 		}
97524726275SEric DeVolder 	}
97624726275SEric DeVolder 
97724726275SEric DeVolder 	if (image->elfcorehdr_index < 0) {
97824726275SEric DeVolder 		pr_err("unable to locate elfcorehdr segment");
97924726275SEric DeVolder 		goto out;
98024726275SEric DeVolder 	}
98124726275SEric DeVolder 
98224726275SEric DeVolder 	/* Needed in order for the segments to be updated */
98324726275SEric DeVolder 	arch_kexec_unprotect_crashkres();
98424726275SEric DeVolder 
98524726275SEric DeVolder 	/* Differentiate between normal load and hotplug update */
98624726275SEric DeVolder 	image->hp_action = hp_action;
98724726275SEric DeVolder 
98824726275SEric DeVolder 	/* Now invoke arch-specific update handler */
98924726275SEric DeVolder 	arch_crash_handle_hotplug_event(image);
99024726275SEric DeVolder 
99124726275SEric DeVolder 	/* No longer handling a hotplug event */
99224726275SEric DeVolder 	image->hp_action = KEXEC_CRASH_HP_NONE;
99324726275SEric DeVolder 	image->elfcorehdr_updated = true;
99424726275SEric DeVolder 
99524726275SEric DeVolder 	/* Change back to read-only */
99624726275SEric DeVolder 	arch_kexec_protect_crashkres();
99724726275SEric DeVolder 
99824726275SEric DeVolder 	/* Errors in the callback is not a reason to rollback state */
99924726275SEric DeVolder out:
100024726275SEric DeVolder 	/* Release lock now that update complete */
100124726275SEric DeVolder 	kexec_unlock();
1002e2a8f20dSBaoquan He 	crash_hotplug_unlock();
100324726275SEric DeVolder }
100424726275SEric DeVolder 
100524726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v)
100624726275SEric DeVolder {
100724726275SEric DeVolder 	switch (val) {
100824726275SEric DeVolder 	case MEM_ONLINE:
100924726275SEric DeVolder 		crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY,
101024726275SEric DeVolder 			KEXEC_CRASH_HP_INVALID_CPU);
101124726275SEric DeVolder 		break;
101224726275SEric DeVolder 
101324726275SEric DeVolder 	case MEM_OFFLINE:
101424726275SEric DeVolder 		crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY,
101524726275SEric DeVolder 			KEXEC_CRASH_HP_INVALID_CPU);
101624726275SEric DeVolder 		break;
101724726275SEric DeVolder 	}
101824726275SEric DeVolder 	return NOTIFY_OK;
101924726275SEric DeVolder }
102024726275SEric DeVolder 
102124726275SEric DeVolder static struct notifier_block crash_memhp_nb = {
102224726275SEric DeVolder 	.notifier_call = crash_memhp_notifier,
102324726275SEric DeVolder 	.priority = 0
102424726275SEric DeVolder };
102524726275SEric DeVolder 
102624726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu)
102724726275SEric DeVolder {
102824726275SEric DeVolder 	crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu);
102924726275SEric DeVolder 	return 0;
103024726275SEric DeVolder }
103124726275SEric DeVolder 
103224726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu)
103324726275SEric DeVolder {
103424726275SEric DeVolder 	crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu);
103524726275SEric DeVolder 	return 0;
103624726275SEric DeVolder }
103724726275SEric DeVolder 
103824726275SEric DeVolder static int __init crash_hotplug_init(void)
103924726275SEric DeVolder {
104024726275SEric DeVolder 	int result = 0;
104124726275SEric DeVolder 
104224726275SEric DeVolder 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
104324726275SEric DeVolder 		register_memory_notifier(&crash_memhp_nb);
104424726275SEric DeVolder 
104524726275SEric DeVolder 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
104624726275SEric DeVolder 		result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
104724726275SEric DeVolder 			"crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline);
104824726275SEric DeVolder 	}
104924726275SEric DeVolder 
105024726275SEric DeVolder 	return result;
105124726275SEric DeVolder }
105224726275SEric DeVolder 
105324726275SEric DeVolder subsys_initcall(crash_hotplug_init);
105424726275SEric DeVolder #endif
1055