xref: /linux-6.15/kernel/crash_core.c (revision 70916e9c)
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>
8692f66f2SHari Bathini #include <linux/crash_core.h>
971d2bcecSPhilipp Rudo #include <linux/init.h>
10692f66f2SHari Bathini #include <linux/utsname.h>
11692f66f2SHari Bathini #include <linux/vmalloc.h>
1246d36b1bSTao Liu #include <linux/sizes.h>
136f991cc3SEric DeVolder #include <linux/kexec.h>
1424726275SEric DeVolder #include <linux/memory.h>
1524726275SEric DeVolder #include <linux/cpuhotplug.h>
16692f66f2SHari Bathini 
17692f66f2SHari Bathini #include <asm/page.h>
18692f66f2SHari Bathini #include <asm/sections.h>
19692f66f2SHari Bathini 
20a24d22b2SEric Biggers #include <crypto/sha1.h>
210935288cSVijay Balakrishna 
225fd8fea9SStephen Brennan #include "kallsyms_internal.h"
2324726275SEric DeVolder #include "kexec_internal.h"
245fd8fea9SStephen Brennan 
256f991cc3SEric DeVolder /* Per cpu memory for storing cpu states in case of system crash. */
266f991cc3SEric DeVolder note_buf_t __percpu *crash_notes;
27692f66f2SHari Bathini 
28692f66f2SHari Bathini /* vmcoreinfo stuff */
2923c85094SOmar Sandoval unsigned char *vmcoreinfo_data;
3023c85094SOmar Sandoval size_t vmcoreinfo_size;
31203e9e41SXunlei Pang u32 *vmcoreinfo_note;
32692f66f2SHari Bathini 
331229384fSXunlei Pang /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
341229384fSXunlei Pang static unsigned char *vmcoreinfo_data_safecopy;
351229384fSXunlei Pang 
36692f66f2SHari Bathini /*
37692f66f2SHari Bathini  * parsing the "crashkernel" commandline
38692f66f2SHari Bathini  *
39692f66f2SHari Bathini  * this code is intended to be called from architecture specific code
40692f66f2SHari Bathini  */
41692f66f2SHari Bathini 
42692f66f2SHari Bathini 
43692f66f2SHari Bathini /*
44692f66f2SHari Bathini  * This function parses command lines in the format
45692f66f2SHari Bathini  *
46692f66f2SHari Bathini  *   crashkernel=ramsize-range:size[,...][@offset]
47692f66f2SHari Bathini  *
48692f66f2SHari Bathini  * The function returns 0 on success and -EINVAL on failure.
49692f66f2SHari Bathini  */
50692f66f2SHari Bathini static int __init parse_crashkernel_mem(char *cmdline,
51692f66f2SHari Bathini 					unsigned long long system_ram,
52692f66f2SHari Bathini 					unsigned long long *crash_size,
53692f66f2SHari Bathini 					unsigned long long *crash_base)
54692f66f2SHari Bathini {
55692f66f2SHari Bathini 	char *cur = cmdline, *tmp;
5646d36b1bSTao Liu 	unsigned long long total_mem = system_ram;
5746d36b1bSTao Liu 
5846d36b1bSTao Liu 	/*
5946d36b1bSTao Liu 	 * Firmware sometimes reserves some memory regions for its own use,
6046d36b1bSTao Liu 	 * so the system memory size is less than the actual physical memory
6146d36b1bSTao Liu 	 * size. Work around this by rounding up the total size to 128M,
6246d36b1bSTao Liu 	 * which is enough for most test cases.
6346d36b1bSTao Liu 	 */
6446d36b1bSTao Liu 	total_mem = roundup(total_mem, SZ_128M);
65692f66f2SHari Bathini 
66692f66f2SHari Bathini 	/* for each entry of the comma-separated list */
67692f66f2SHari Bathini 	do {
68692f66f2SHari Bathini 		unsigned long long start, end = ULLONG_MAX, size;
69692f66f2SHari Bathini 
70692f66f2SHari Bathini 		/* get the start of the range */
71692f66f2SHari Bathini 		start = memparse(cur, &tmp);
72692f66f2SHari Bathini 		if (cur == tmp) {
73692f66f2SHari Bathini 			pr_warn("crashkernel: Memory value expected\n");
74692f66f2SHari Bathini 			return -EINVAL;
75692f66f2SHari Bathini 		}
76692f66f2SHari Bathini 		cur = tmp;
77692f66f2SHari Bathini 		if (*cur != '-') {
78692f66f2SHari Bathini 			pr_warn("crashkernel: '-' expected\n");
79692f66f2SHari Bathini 			return -EINVAL;
80692f66f2SHari Bathini 		}
81692f66f2SHari Bathini 		cur++;
82692f66f2SHari Bathini 
83692f66f2SHari Bathini 		/* if no ':' is here, than we read the end */
84692f66f2SHari Bathini 		if (*cur != ':') {
85692f66f2SHari Bathini 			end = memparse(cur, &tmp);
86692f66f2SHari Bathini 			if (cur == tmp) {
87692f66f2SHari Bathini 				pr_warn("crashkernel: Memory value expected\n");
88692f66f2SHari Bathini 				return -EINVAL;
89692f66f2SHari Bathini 			}
90692f66f2SHari Bathini 			cur = tmp;
91692f66f2SHari Bathini 			if (end <= start) {
92692f66f2SHari Bathini 				pr_warn("crashkernel: end <= start\n");
93692f66f2SHari Bathini 				return -EINVAL;
94692f66f2SHari Bathini 			}
95692f66f2SHari Bathini 		}
96692f66f2SHari Bathini 
97692f66f2SHari Bathini 		if (*cur != ':') {
98692f66f2SHari Bathini 			pr_warn("crashkernel: ':' expected\n");
99692f66f2SHari Bathini 			return -EINVAL;
100692f66f2SHari Bathini 		}
101692f66f2SHari Bathini 		cur++;
102692f66f2SHari Bathini 
103692f66f2SHari Bathini 		size = memparse(cur, &tmp);
104692f66f2SHari Bathini 		if (cur == tmp) {
105692f66f2SHari Bathini 			pr_warn("Memory value expected\n");
106692f66f2SHari Bathini 			return -EINVAL;
107692f66f2SHari Bathini 		}
108692f66f2SHari Bathini 		cur = tmp;
10946d36b1bSTao Liu 		if (size >= total_mem) {
110692f66f2SHari Bathini 			pr_warn("crashkernel: invalid size\n");
111692f66f2SHari Bathini 			return -EINVAL;
112692f66f2SHari Bathini 		}
113692f66f2SHari Bathini 
114692f66f2SHari Bathini 		/* match ? */
11546d36b1bSTao Liu 		if (total_mem >= start && total_mem < end) {
116692f66f2SHari Bathini 			*crash_size = size;
117692f66f2SHari Bathini 			break;
118692f66f2SHari Bathini 		}
119692f66f2SHari Bathini 	} while (*cur++ == ',');
120692f66f2SHari Bathini 
121692f66f2SHari Bathini 	if (*crash_size > 0) {
122692f66f2SHari Bathini 		while (*cur && *cur != ' ' && *cur != '@')
123692f66f2SHari Bathini 			cur++;
124692f66f2SHari Bathini 		if (*cur == '@') {
125692f66f2SHari Bathini 			cur++;
126692f66f2SHari Bathini 			*crash_base = memparse(cur, &tmp);
127692f66f2SHari Bathini 			if (cur == tmp) {
128692f66f2SHari Bathini 				pr_warn("Memory value expected after '@'\n");
129692f66f2SHari Bathini 				return -EINVAL;
130692f66f2SHari Bathini 			}
131692f66f2SHari Bathini 		}
132de40ccefSDave Young 	} else
133de40ccefSDave Young 		pr_info("crashkernel size resulted in zero bytes\n");
134692f66f2SHari Bathini 
135692f66f2SHari Bathini 	return 0;
136692f66f2SHari Bathini }
137692f66f2SHari Bathini 
138692f66f2SHari Bathini /*
139692f66f2SHari Bathini  * That function parses "simple" (old) crashkernel command lines like
140692f66f2SHari Bathini  *
141692f66f2SHari Bathini  *	crashkernel=size[@offset]
142692f66f2SHari Bathini  *
143692f66f2SHari Bathini  * It returns 0 on success and -EINVAL on failure.
144692f66f2SHari Bathini  */
145692f66f2SHari Bathini static int __init parse_crashkernel_simple(char *cmdline,
146692f66f2SHari Bathini 					   unsigned long long *crash_size,
147692f66f2SHari Bathini 					   unsigned long long *crash_base)
148692f66f2SHari Bathini {
149692f66f2SHari Bathini 	char *cur = cmdline;
150692f66f2SHari Bathini 
151692f66f2SHari Bathini 	*crash_size = memparse(cmdline, &cur);
152692f66f2SHari Bathini 	if (cmdline == cur) {
153692f66f2SHari Bathini 		pr_warn("crashkernel: memory value expected\n");
154692f66f2SHari Bathini 		return -EINVAL;
155692f66f2SHari Bathini 	}
156692f66f2SHari Bathini 
157692f66f2SHari Bathini 	if (*cur == '@')
158692f66f2SHari Bathini 		*crash_base = memparse(cur+1, &cur);
159692f66f2SHari Bathini 	else if (*cur != ' ' && *cur != '\0') {
160692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
161692f66f2SHari Bathini 		return -EINVAL;
162692f66f2SHari Bathini 	}
163692f66f2SHari Bathini 
164692f66f2SHari Bathini 	return 0;
165692f66f2SHari Bathini }
166692f66f2SHari Bathini 
167692f66f2SHari Bathini #define SUFFIX_HIGH 0
168692f66f2SHari Bathini #define SUFFIX_LOW  1
169692f66f2SHari Bathini #define SUFFIX_NULL 2
170692f66f2SHari Bathini static __initdata char *suffix_tbl[] = {
171692f66f2SHari Bathini 	[SUFFIX_HIGH] = ",high",
172692f66f2SHari Bathini 	[SUFFIX_LOW]  = ",low",
173692f66f2SHari Bathini 	[SUFFIX_NULL] = NULL,
174692f66f2SHari Bathini };
175692f66f2SHari Bathini 
176692f66f2SHari Bathini /*
177692f66f2SHari Bathini  * That function parses "suffix"  crashkernel command lines like
178692f66f2SHari Bathini  *
179692f66f2SHari Bathini  *	crashkernel=size,[high|low]
180692f66f2SHari Bathini  *
181692f66f2SHari Bathini  * It returns 0 on success and -EINVAL on failure.
182692f66f2SHari Bathini  */
183692f66f2SHari Bathini static int __init parse_crashkernel_suffix(char *cmdline,
184692f66f2SHari Bathini 					   unsigned long long	*crash_size,
185692f66f2SHari Bathini 					   const char *suffix)
186692f66f2SHari Bathini {
187692f66f2SHari Bathini 	char *cur = cmdline;
188692f66f2SHari Bathini 
189692f66f2SHari Bathini 	*crash_size = memparse(cmdline, &cur);
190692f66f2SHari Bathini 	if (cmdline == cur) {
191692f66f2SHari Bathini 		pr_warn("crashkernel: memory value expected\n");
192692f66f2SHari Bathini 		return -EINVAL;
193692f66f2SHari Bathini 	}
194692f66f2SHari Bathini 
195692f66f2SHari Bathini 	/* check with suffix */
196692f66f2SHari Bathini 	if (strncmp(cur, suffix, strlen(suffix))) {
197692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
198692f66f2SHari Bathini 		return -EINVAL;
199692f66f2SHari Bathini 	}
200692f66f2SHari Bathini 	cur += strlen(suffix);
201692f66f2SHari Bathini 	if (*cur != ' ' && *cur != '\0') {
202692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
203692f66f2SHari Bathini 		return -EINVAL;
204692f66f2SHari Bathini 	}
205692f66f2SHari Bathini 
206692f66f2SHari Bathini 	return 0;
207692f66f2SHari Bathini }
208692f66f2SHari Bathini 
209692f66f2SHari Bathini static __init char *get_last_crashkernel(char *cmdline,
210692f66f2SHari Bathini 			     const char *name,
211692f66f2SHari Bathini 			     const char *suffix)
212692f66f2SHari Bathini {
213692f66f2SHari Bathini 	char *p = cmdline, *ck_cmdline = NULL;
214692f66f2SHari Bathini 
215692f66f2SHari Bathini 	/* find crashkernel and use the last one if there are more */
216692f66f2SHari Bathini 	p = strstr(p, name);
217692f66f2SHari Bathini 	while (p) {
218692f66f2SHari Bathini 		char *end_p = strchr(p, ' ');
219692f66f2SHari Bathini 		char *q;
220692f66f2SHari Bathini 
221692f66f2SHari Bathini 		if (!end_p)
222692f66f2SHari Bathini 			end_p = p + strlen(p);
223692f66f2SHari Bathini 
224692f66f2SHari Bathini 		if (!suffix) {
225692f66f2SHari Bathini 			int i;
226692f66f2SHari Bathini 
227692f66f2SHari Bathini 			/* skip the one with any known suffix */
228692f66f2SHari Bathini 			for (i = 0; suffix_tbl[i]; i++) {
229692f66f2SHari Bathini 				q = end_p - strlen(suffix_tbl[i]);
230692f66f2SHari Bathini 				if (!strncmp(q, suffix_tbl[i],
231692f66f2SHari Bathini 					     strlen(suffix_tbl[i])))
232692f66f2SHari Bathini 					goto next;
233692f66f2SHari Bathini 			}
234692f66f2SHari Bathini 			ck_cmdline = p;
235692f66f2SHari Bathini 		} else {
236692f66f2SHari Bathini 			q = end_p - strlen(suffix);
237692f66f2SHari Bathini 			if (!strncmp(q, suffix, strlen(suffix)))
238692f66f2SHari Bathini 				ck_cmdline = p;
239692f66f2SHari Bathini 		}
240692f66f2SHari Bathini next:
241692f66f2SHari Bathini 		p = strstr(p+1, name);
242692f66f2SHari Bathini 	}
243692f66f2SHari Bathini 
244692f66f2SHari Bathini 	return ck_cmdline;
245692f66f2SHari Bathini }
246692f66f2SHari Bathini 
247692f66f2SHari Bathini static int __init __parse_crashkernel(char *cmdline,
248692f66f2SHari Bathini 			     unsigned long long system_ram,
249692f66f2SHari Bathini 			     unsigned long long *crash_size,
250692f66f2SHari Bathini 			     unsigned long long *crash_base,
251692f66f2SHari Bathini 			     const char *suffix)
252692f66f2SHari Bathini {
253692f66f2SHari Bathini 	char	*first_colon, *first_space;
254692f66f2SHari Bathini 	char	*ck_cmdline;
255a6304272SBaoquan He 	char	*name = "crashkernel=";
256692f66f2SHari Bathini 
257692f66f2SHari Bathini 	BUG_ON(!crash_size || !crash_base);
258692f66f2SHari Bathini 	*crash_size = 0;
259692f66f2SHari Bathini 	*crash_base = 0;
260692f66f2SHari Bathini 
261692f66f2SHari Bathini 	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
262692f66f2SHari Bathini 	if (!ck_cmdline)
2632e5920bbSZhen Lei 		return -ENOENT;
264692f66f2SHari Bathini 
265692f66f2SHari Bathini 	ck_cmdline += strlen(name);
266692f66f2SHari Bathini 
267692f66f2SHari Bathini 	if (suffix)
268692f66f2SHari Bathini 		return parse_crashkernel_suffix(ck_cmdline, crash_size,
269692f66f2SHari Bathini 				suffix);
270692f66f2SHari Bathini 	/*
271692f66f2SHari Bathini 	 * if the commandline contains a ':', then that's the extended
272692f66f2SHari Bathini 	 * syntax -- if not, it must be the classic syntax
273692f66f2SHari Bathini 	 */
274692f66f2SHari Bathini 	first_colon = strchr(ck_cmdline, ':');
275692f66f2SHari Bathini 	first_space = strchr(ck_cmdline, ' ');
276692f66f2SHari Bathini 	if (first_colon && (!first_space || first_colon < first_space))
277692f66f2SHari Bathini 		return parse_crashkernel_mem(ck_cmdline, system_ram,
278692f66f2SHari Bathini 				crash_size, crash_base);
279692f66f2SHari Bathini 
280692f66f2SHari Bathini 	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
281692f66f2SHari Bathini }
282692f66f2SHari Bathini 
283692f66f2SHari Bathini /*
284692f66f2SHari Bathini  * That function is the entry point for command line parsing and should be
285692f66f2SHari Bathini  * called from the arch-specific code.
286*70916e9cSBaoquan He  *
287*70916e9cSBaoquan He  * If crashkernel=,high|low is supported on architecture, non-NULL values
288*70916e9cSBaoquan He  * should be passed to parameters 'low_size' and 'high'.
289692f66f2SHari Bathini  */
290692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline,
291692f66f2SHari Bathini 			     unsigned long long system_ram,
292692f66f2SHari Bathini 			     unsigned long long *crash_size,
293a9e1a3d8SBaoquan He 			     unsigned long long *crash_base,
294a9e1a3d8SBaoquan He 			     unsigned long long *low_size,
295a9e1a3d8SBaoquan He 			     bool *high)
296692f66f2SHari Bathini {
297a9e1a3d8SBaoquan He 	int ret;
298a9e1a3d8SBaoquan He 
299a9e1a3d8SBaoquan He 	/* crashkernel=X[@offset] */
300a9e1a3d8SBaoquan He 	ret = __parse_crashkernel(cmdline, system_ram, crash_size,
301a9e1a3d8SBaoquan He 				crash_base, NULL);
302*70916e9cSBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
303*70916e9cSBaoquan He 	/*
304*70916e9cSBaoquan He 	 * If non-NULL 'high' passed in and no normal crashkernel
305*70916e9cSBaoquan He 	 * setting detected, try parsing crashkernel=,high|low.
306*70916e9cSBaoquan He 	 */
307*70916e9cSBaoquan He 	if (high && ret == -ENOENT) {
308*70916e9cSBaoquan He 		ret = __parse_crashkernel(cmdline, 0, crash_size,
309*70916e9cSBaoquan He 				crash_base, suffix_tbl[SUFFIX_HIGH]);
310*70916e9cSBaoquan He 		if (ret || !*crash_size)
311*70916e9cSBaoquan He 			return -EINVAL;
312a9e1a3d8SBaoquan He 
313*70916e9cSBaoquan He 		/*
314*70916e9cSBaoquan He 		 * crashkernel=Y,low can be specified or not, but invalid value
315*70916e9cSBaoquan He 		 * is not allowed.
316*70916e9cSBaoquan He 		 */
317*70916e9cSBaoquan He 		ret = __parse_crashkernel(cmdline, 0, low_size,
318*70916e9cSBaoquan He 				crash_base, suffix_tbl[SUFFIX_LOW]);
319*70916e9cSBaoquan He 		if (ret == -ENOENT) {
320*70916e9cSBaoquan He 			*low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
321*70916e9cSBaoquan He 			ret = 0;
322*70916e9cSBaoquan He 		} else if (ret) {
323*70916e9cSBaoquan He 			return ret;
324*70916e9cSBaoquan He 		}
325*70916e9cSBaoquan He 
326*70916e9cSBaoquan He 		*high = true;
327*70916e9cSBaoquan He 	}
328*70916e9cSBaoquan He #endif
329*70916e9cSBaoquan He 	if (!*crash_size)
330*70916e9cSBaoquan He 		ret = -EINVAL;
331*70916e9cSBaoquan He 
332*70916e9cSBaoquan He 	return ret;
333692f66f2SHari Bathini }
334692f66f2SHari Bathini 
335692f66f2SHari Bathini int __init parse_crashkernel_high(char *cmdline,
336692f66f2SHari Bathini 			     unsigned long long system_ram,
337692f66f2SHari Bathini 			     unsigned long long *crash_size,
338692f66f2SHari Bathini 			     unsigned long long *crash_base)
339692f66f2SHari Bathini {
340692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
341a6304272SBaoquan He 				suffix_tbl[SUFFIX_HIGH]);
342692f66f2SHari Bathini }
343692f66f2SHari Bathini 
344692f66f2SHari Bathini int __init parse_crashkernel_low(char *cmdline,
345692f66f2SHari Bathini 			     unsigned long long system_ram,
346692f66f2SHari Bathini 			     unsigned long long *crash_size,
347692f66f2SHari Bathini 			     unsigned long long *crash_base)
348692f66f2SHari Bathini {
349692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
350a6304272SBaoquan He 				suffix_tbl[SUFFIX_LOW]);
351692f66f2SHari Bathini }
352692f66f2SHari Bathini 
35371d2bcecSPhilipp Rudo /*
35471d2bcecSPhilipp Rudo  * Add a dummy early_param handler to mark crashkernel= as a known command line
35571d2bcecSPhilipp Rudo  * parameter and suppress incorrect warnings in init/main.c.
35671d2bcecSPhilipp Rudo  */
35771d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg)
35871d2bcecSPhilipp Rudo {
35971d2bcecSPhilipp Rudo 	return 0;
36071d2bcecSPhilipp Rudo }
36171d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy);
36271d2bcecSPhilipp Rudo 
3636f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
3646f991cc3SEric DeVolder 			  void **addr, unsigned long *sz)
3656f991cc3SEric DeVolder {
3666f991cc3SEric DeVolder 	Elf64_Ehdr *ehdr;
3676f991cc3SEric DeVolder 	Elf64_Phdr *phdr;
3686f991cc3SEric DeVolder 	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
3696f991cc3SEric DeVolder 	unsigned char *buf;
3706f991cc3SEric DeVolder 	unsigned int cpu, i;
3716f991cc3SEric DeVolder 	unsigned long long notes_addr;
3726f991cc3SEric DeVolder 	unsigned long mstart, mend;
3736f991cc3SEric DeVolder 
3746f991cc3SEric DeVolder 	/* extra phdr for vmcoreinfo ELF note */
3756f991cc3SEric DeVolder 	nr_phdr = nr_cpus + 1;
3766f991cc3SEric DeVolder 	nr_phdr += mem->nr_ranges;
3776f991cc3SEric DeVolder 
3786f991cc3SEric DeVolder 	/*
3796f991cc3SEric DeVolder 	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
3806f991cc3SEric DeVolder 	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
3816f991cc3SEric DeVolder 	 * I think this is required by tools like gdb. So same physical
3826f991cc3SEric DeVolder 	 * memory will be mapped in two ELF headers. One will contain kernel
3836f991cc3SEric DeVolder 	 * text virtual addresses and other will have __va(physical) addresses.
3846f991cc3SEric DeVolder 	 */
3856f991cc3SEric DeVolder 
3866f991cc3SEric DeVolder 	nr_phdr++;
3876f991cc3SEric DeVolder 	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
3886f991cc3SEric DeVolder 	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
3896f991cc3SEric DeVolder 
3906f991cc3SEric DeVolder 	buf = vzalloc(elf_sz);
3916f991cc3SEric DeVolder 	if (!buf)
3926f991cc3SEric DeVolder 		return -ENOMEM;
3936f991cc3SEric DeVolder 
3946f991cc3SEric DeVolder 	ehdr = (Elf64_Ehdr *)buf;
3956f991cc3SEric DeVolder 	phdr = (Elf64_Phdr *)(ehdr + 1);
3966f991cc3SEric DeVolder 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
3976f991cc3SEric DeVolder 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
3986f991cc3SEric DeVolder 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
3996f991cc3SEric DeVolder 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
4006f991cc3SEric DeVolder 	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
4016f991cc3SEric DeVolder 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
4026f991cc3SEric DeVolder 	ehdr->e_type = ET_CORE;
4036f991cc3SEric DeVolder 	ehdr->e_machine = ELF_ARCH;
4046f991cc3SEric DeVolder 	ehdr->e_version = EV_CURRENT;
4056f991cc3SEric DeVolder 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
4066f991cc3SEric DeVolder 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
4076f991cc3SEric DeVolder 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
4086f991cc3SEric DeVolder 
409a396d0f8SEric DeVolder 	/* Prepare one phdr of type PT_NOTE for each possible CPU */
410a396d0f8SEric DeVolder 	for_each_possible_cpu(cpu) {
4116f991cc3SEric DeVolder 		phdr->p_type = PT_NOTE;
4126f991cc3SEric DeVolder 		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
4136f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = notes_addr;
4146f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
4156f991cc3SEric DeVolder 		(ehdr->e_phnum)++;
4166f991cc3SEric DeVolder 		phdr++;
4176f991cc3SEric DeVolder 	}
4186f991cc3SEric DeVolder 
4196f991cc3SEric DeVolder 	/* Prepare one PT_NOTE header for vmcoreinfo */
4206f991cc3SEric DeVolder 	phdr->p_type = PT_NOTE;
4216f991cc3SEric DeVolder 	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
4226f991cc3SEric DeVolder 	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
4236f991cc3SEric DeVolder 	(ehdr->e_phnum)++;
4246f991cc3SEric DeVolder 	phdr++;
4256f991cc3SEric DeVolder 
4266f991cc3SEric DeVolder 	/* Prepare PT_LOAD type program header for kernel text region */
4276f991cc3SEric DeVolder 	if (need_kernel_map) {
4286f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
4296f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
4306f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) _text;
4316f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = _end - _text;
4326f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
4336f991cc3SEric DeVolder 		ehdr->e_phnum++;
4346f991cc3SEric DeVolder 		phdr++;
4356f991cc3SEric DeVolder 	}
4366f991cc3SEric DeVolder 
4376f991cc3SEric DeVolder 	/* Go through all the ranges in mem->ranges[] and prepare phdr */
4386f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
4396f991cc3SEric DeVolder 		mstart = mem->ranges[i].start;
4406f991cc3SEric DeVolder 		mend = mem->ranges[i].end;
4416f991cc3SEric DeVolder 
4426f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
4436f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
4446f991cc3SEric DeVolder 		phdr->p_offset  = mstart;
4456f991cc3SEric DeVolder 
4466f991cc3SEric DeVolder 		phdr->p_paddr = mstart;
4476f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) __va(mstart);
4486f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
4496f991cc3SEric DeVolder 		phdr->p_align = 0;
4506f991cc3SEric DeVolder 		ehdr->e_phnum++;
4516f991cc3SEric DeVolder 		pr_debug("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
4526f991cc3SEric DeVolder 			phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
4536f991cc3SEric DeVolder 			ehdr->e_phnum, phdr->p_offset);
4546f991cc3SEric DeVolder 		phdr++;
4556f991cc3SEric DeVolder 	}
4566f991cc3SEric DeVolder 
4576f991cc3SEric DeVolder 	*addr = buf;
4586f991cc3SEric DeVolder 	*sz = elf_sz;
4596f991cc3SEric DeVolder 	return 0;
4606f991cc3SEric DeVolder }
4616f991cc3SEric DeVolder 
4626f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem,
4636f991cc3SEric DeVolder 			    unsigned long long mstart, unsigned long long mend)
4646f991cc3SEric DeVolder {
4656f991cc3SEric DeVolder 	int i, j;
4666f991cc3SEric DeVolder 	unsigned long long start, end, p_start, p_end;
4676f991cc3SEric DeVolder 	struct range temp_range = {0, 0};
4686f991cc3SEric DeVolder 
4696f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
4706f991cc3SEric DeVolder 		start = mem->ranges[i].start;
4716f991cc3SEric DeVolder 		end = mem->ranges[i].end;
4726f991cc3SEric DeVolder 		p_start = mstart;
4736f991cc3SEric DeVolder 		p_end = mend;
4746f991cc3SEric DeVolder 
4756f991cc3SEric DeVolder 		if (mstart > end || mend < start)
4766f991cc3SEric DeVolder 			continue;
4776f991cc3SEric DeVolder 
4786f991cc3SEric DeVolder 		/* Truncate any area outside of range */
4796f991cc3SEric DeVolder 		if (mstart < start)
4806f991cc3SEric DeVolder 			p_start = start;
4816f991cc3SEric DeVolder 		if (mend > end)
4826f991cc3SEric DeVolder 			p_end = end;
4836f991cc3SEric DeVolder 
4846f991cc3SEric DeVolder 		/* Found completely overlapping range */
4856f991cc3SEric DeVolder 		if (p_start == start && p_end == end) {
4866f991cc3SEric DeVolder 			mem->ranges[i].start = 0;
4876f991cc3SEric DeVolder 			mem->ranges[i].end = 0;
4886f991cc3SEric DeVolder 			if (i < mem->nr_ranges - 1) {
4896f991cc3SEric DeVolder 				/* Shift rest of the ranges to left */
4906f991cc3SEric DeVolder 				for (j = i; j < mem->nr_ranges - 1; j++) {
4916f991cc3SEric DeVolder 					mem->ranges[j].start =
4926f991cc3SEric DeVolder 						mem->ranges[j+1].start;
4936f991cc3SEric DeVolder 					mem->ranges[j].end =
4946f991cc3SEric DeVolder 							mem->ranges[j+1].end;
4956f991cc3SEric DeVolder 				}
4966f991cc3SEric DeVolder 
4976f991cc3SEric DeVolder 				/*
4986f991cc3SEric DeVolder 				 * Continue to check if there are another overlapping ranges
4996f991cc3SEric DeVolder 				 * from the current position because of shifting the above
5006f991cc3SEric DeVolder 				 * mem ranges.
5016f991cc3SEric DeVolder 				 */
5026f991cc3SEric DeVolder 				i--;
5036f991cc3SEric DeVolder 				mem->nr_ranges--;
5046f991cc3SEric DeVolder 				continue;
5056f991cc3SEric DeVolder 			}
5066f991cc3SEric DeVolder 			mem->nr_ranges--;
5076f991cc3SEric DeVolder 			return 0;
5086f991cc3SEric DeVolder 		}
5096f991cc3SEric DeVolder 
5106f991cc3SEric DeVolder 		if (p_start > start && p_end < end) {
5116f991cc3SEric DeVolder 			/* Split original range */
5126f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
5136f991cc3SEric DeVolder 			temp_range.start = p_end + 1;
5146f991cc3SEric DeVolder 			temp_range.end = end;
5156f991cc3SEric DeVolder 		} else if (p_start != start)
5166f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
5176f991cc3SEric DeVolder 		else
5186f991cc3SEric DeVolder 			mem->ranges[i].start = p_end + 1;
5196f991cc3SEric DeVolder 		break;
5206f991cc3SEric DeVolder 	}
5216f991cc3SEric DeVolder 
5226f991cc3SEric DeVolder 	/* If a split happened, add the split to array */
5236f991cc3SEric DeVolder 	if (!temp_range.end)
5246f991cc3SEric DeVolder 		return 0;
5256f991cc3SEric DeVolder 
5266f991cc3SEric DeVolder 	/* Split happened */
5276f991cc3SEric DeVolder 	if (i == mem->max_nr_ranges - 1)
5286f991cc3SEric DeVolder 		return -ENOMEM;
5296f991cc3SEric DeVolder 
5306f991cc3SEric DeVolder 	/* Location where new range should go */
5316f991cc3SEric DeVolder 	j = i + 1;
5326f991cc3SEric DeVolder 	if (j < mem->nr_ranges) {
5336f991cc3SEric DeVolder 		/* Move over all ranges one slot towards the end */
5346f991cc3SEric DeVolder 		for (i = mem->nr_ranges - 1; i >= j; i--)
5356f991cc3SEric DeVolder 			mem->ranges[i + 1] = mem->ranges[i];
5366f991cc3SEric DeVolder 	}
5376f991cc3SEric DeVolder 
5386f991cc3SEric DeVolder 	mem->ranges[j].start = temp_range.start;
5396f991cc3SEric DeVolder 	mem->ranges[j].end = temp_range.end;
5406f991cc3SEric DeVolder 	mem->nr_ranges++;
5416f991cc3SEric DeVolder 	return 0;
5426f991cc3SEric DeVolder }
5436f991cc3SEric DeVolder 
54451dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
545692f66f2SHari Bathini 			  void *data, size_t data_len)
546692f66f2SHari Bathini {
54751dbd925SHari Bathini 	struct elf_note *note = (struct elf_note *)buf;
548692f66f2SHari Bathini 
54951dbd925SHari Bathini 	note->n_namesz = strlen(name) + 1;
55051dbd925SHari Bathini 	note->n_descsz = data_len;
55151dbd925SHari Bathini 	note->n_type   = type;
55251dbd925SHari Bathini 	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
55351dbd925SHari Bathini 	memcpy(buf, name, note->n_namesz);
55451dbd925SHari Bathini 	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
55551dbd925SHari Bathini 	memcpy(buf, data, data_len);
55651dbd925SHari Bathini 	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
557692f66f2SHari Bathini 
558692f66f2SHari Bathini 	return buf;
559692f66f2SHari Bathini }
560692f66f2SHari Bathini 
56151dbd925SHari Bathini void final_note(Elf_Word *buf)
562692f66f2SHari Bathini {
56351dbd925SHari Bathini 	memset(buf, 0, sizeof(struct elf_note));
564692f66f2SHari Bathini }
565692f66f2SHari Bathini 
566692f66f2SHari Bathini static void update_vmcoreinfo_note(void)
567692f66f2SHari Bathini {
568692f66f2SHari Bathini 	u32 *buf = vmcoreinfo_note;
569692f66f2SHari Bathini 
570692f66f2SHari Bathini 	if (!vmcoreinfo_size)
571692f66f2SHari Bathini 		return;
572692f66f2SHari Bathini 	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
573692f66f2SHari Bathini 			      vmcoreinfo_size);
574692f66f2SHari Bathini 	final_note(buf);
575692f66f2SHari Bathini }
576692f66f2SHari Bathini 
5771229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr)
5781229384fSXunlei Pang {
5791229384fSXunlei Pang 	if (ptr)
5801229384fSXunlei Pang 		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
5811229384fSXunlei Pang 
5821229384fSXunlei Pang 	vmcoreinfo_data_safecopy = ptr;
5831229384fSXunlei Pang }
5841229384fSXunlei Pang 
585692f66f2SHari Bathini void crash_save_vmcoreinfo(void)
586692f66f2SHari Bathini {
587203e9e41SXunlei Pang 	if (!vmcoreinfo_note)
588203e9e41SXunlei Pang 		return;
589203e9e41SXunlei Pang 
5901229384fSXunlei Pang 	/* Use the safe copy to generate vmcoreinfo note if have */
5911229384fSXunlei Pang 	if (vmcoreinfo_data_safecopy)
5921229384fSXunlei Pang 		vmcoreinfo_data = vmcoreinfo_data_safecopy;
5931229384fSXunlei Pang 
59491bc9aafSArnd Bergmann 	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
595692f66f2SHari Bathini 	update_vmcoreinfo_note();
596692f66f2SHari Bathini }
597692f66f2SHari Bathini 
598692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...)
599692f66f2SHari Bathini {
600692f66f2SHari Bathini 	va_list args;
601692f66f2SHari Bathini 	char buf[0x50];
602692f66f2SHari Bathini 	size_t r;
603692f66f2SHari Bathini 
604692f66f2SHari Bathini 	va_start(args, fmt);
605692f66f2SHari Bathini 	r = vscnprintf(buf, sizeof(buf), fmt, args);
606692f66f2SHari Bathini 	va_end(args);
607692f66f2SHari Bathini 
6085203f499SXunlei Pang 	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
609692f66f2SHari Bathini 
610692f66f2SHari Bathini 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
611692f66f2SHari Bathini 
612692f66f2SHari Bathini 	vmcoreinfo_size += r;
61308fc35f3SStephen Brennan 
61408fc35f3SStephen Brennan 	WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES,
61508fc35f3SStephen Brennan 		  "vmcoreinfo data exceeds allocated size, truncating");
616692f66f2SHari Bathini }
617692f66f2SHari Bathini 
618692f66f2SHari Bathini /*
619692f66f2SHari Bathini  * provide an empty default implementation here -- architecture
620692f66f2SHari Bathini  * code may override this
621692f66f2SHari Bathini  */
622692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void)
623692f66f2SHari Bathini {}
624692f66f2SHari Bathini 
625692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void)
626692f66f2SHari Bathini {
627203e9e41SXunlei Pang 	return __pa(vmcoreinfo_note);
628692f66f2SHari Bathini }
62943d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note);
630692f66f2SHari Bathini 
631692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void)
632692f66f2SHari Bathini {
633203e9e41SXunlei Pang 	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
634203e9e41SXunlei Pang 	if (!vmcoreinfo_data) {
635203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
636203e9e41SXunlei Pang 		return -ENOMEM;
637203e9e41SXunlei Pang 	}
638203e9e41SXunlei Pang 
639203e9e41SXunlei Pang 	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
640203e9e41SXunlei Pang 						GFP_KERNEL | __GFP_ZERO);
641203e9e41SXunlei Pang 	if (!vmcoreinfo_note) {
642203e9e41SXunlei Pang 		free_page((unsigned long)vmcoreinfo_data);
643203e9e41SXunlei Pang 		vmcoreinfo_data = NULL;
644203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
645203e9e41SXunlei Pang 		return -ENOMEM;
646203e9e41SXunlei Pang 	}
647203e9e41SXunlei Pang 
648692f66f2SHari Bathini 	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
64944e8a5e9SStephen Boyd 	VMCOREINFO_BUILD_ID();
650692f66f2SHari Bathini 	VMCOREINFO_PAGESIZE(PAGE_SIZE);
651692f66f2SHari Bathini 
652692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(init_uts_ns);
653ca4a9241SAlexander Egorenkov 	VMCOREINFO_OFFSET(uts_namespace, name);
654692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(node_online_map);
655692f66f2SHari Bathini #ifdef CONFIG_MMU
656eff4345eSOmar Sandoval 	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
657692f66f2SHari Bathini #endif
658692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(_stext);
659692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(vmap_area_list);
660692f66f2SHari Bathini 
661a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA
662692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(mem_map);
663692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(contig_page_data);
664692f66f2SHari Bathini #endif
665692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM
666a0b12803SKirill A. Shutemov 	VMCOREINFO_SYMBOL_ARRAY(mem_section);
667692f66f2SHari Bathini 	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
668692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(mem_section);
669692f66f2SHari Bathini 	VMCOREINFO_OFFSET(mem_section, section_mem_map);
6704f5aecdfSPingfan Liu 	VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
6711d50e5d0SBhupesh Sharma 	VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
672692f66f2SHari Bathini #endif
673692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(page);
674692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(pglist_data);
675692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(zone);
676692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(free_area);
677692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(list_head);
678692f66f2SHari Bathini 	VMCOREINFO_SIZE(nodemask_t);
679692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, flags);
680692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _refcount);
681692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, mapping);
682692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, lru);
683692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _mapcount);
684692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, private);
685692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, compound_head);
686692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_zones);
687692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, nr_zones);
68843b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM
689692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
690692f66f2SHari Bathini #endif
691692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
692692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
693692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_id);
694692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, free_area);
695692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, vm_stat);
696692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, spanned_pages);
697692f66f2SHari Bathini 	VMCOREINFO_OFFSET(free_area, free_list);
698692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, next);
699692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, prev);
700692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, va_start);
701692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, list);
70223baf831SKirill A. Shutemov 	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER + 1);
703692f66f2SHari Bathini 	log_buf_vmcoreinfo_setup();
704692f66f2SHari Bathini 	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
705692f66f2SHari Bathini 	VMCOREINFO_NUMBER(NR_FREE_PAGES);
706692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_lru);
707692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_private);
708692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_swapcache);
7091cbf29daSPetr Tesarik 	VMCOREINFO_NUMBER(PG_swapbacked);
710692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_slab);
711692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE
712692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_hwpoison);
713692f66f2SHari Bathini #endif
714692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_head_mask);
7156e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE	(~PG_buddy)
716692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
717692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
7189c5ccf2dSMatthew Wilcox (Oracle) 	VMCOREINFO_NUMBER(PG_hugetlb);
719e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE	(~PG_offline)
720e04b742fSDavid Hildenbrand 	VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
721692f66f2SHari Bathini #endif
722692f66f2SHari Bathini 
7235fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS
7245fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_names);
725f09bddbdSStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_num_syms);
7265fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_table);
7275fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_index);
7285fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE
7295fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_offsets);
7305fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_relative_base);
7315fd8fea9SStephen Brennan #else
7325fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_addresses);
7335fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */
7345fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */
7355fd8fea9SStephen Brennan 
736692f66f2SHari Bathini 	arch_crash_save_vmcoreinfo();
737692f66f2SHari Bathini 	update_vmcoreinfo_note();
738692f66f2SHari Bathini 
739692f66f2SHari Bathini 	return 0;
740692f66f2SHari Bathini }
741692f66f2SHari Bathini 
742692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init);
7436f991cc3SEric DeVolder 
7446f991cc3SEric DeVolder static int __init crash_notes_memory_init(void)
7456f991cc3SEric DeVolder {
7466f991cc3SEric DeVolder 	/* Allocate memory for saving cpu registers. */
7476f991cc3SEric DeVolder 	size_t size, align;
7486f991cc3SEric DeVolder 
7496f991cc3SEric DeVolder 	/*
7506f991cc3SEric DeVolder 	 * crash_notes could be allocated across 2 vmalloc pages when percpu
7516f991cc3SEric DeVolder 	 * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
7526f991cc3SEric DeVolder 	 * pages are also on 2 continuous physical pages. In this case the
7536f991cc3SEric DeVolder 	 * 2nd part of crash_notes in 2nd page could be lost since only the
7546f991cc3SEric DeVolder 	 * starting address and size of crash_notes are exported through sysfs.
7556f991cc3SEric DeVolder 	 * Here round up the size of crash_notes to the nearest power of two
7566f991cc3SEric DeVolder 	 * and pass it to __alloc_percpu as align value. This can make sure
7576f991cc3SEric DeVolder 	 * crash_notes is allocated inside one physical page.
7586f991cc3SEric DeVolder 	 */
7596f991cc3SEric DeVolder 	size = sizeof(note_buf_t);
7606f991cc3SEric DeVolder 	align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
7616f991cc3SEric DeVolder 
7626f991cc3SEric DeVolder 	/*
7636f991cc3SEric DeVolder 	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
7646f991cc3SEric DeVolder 	 * definitely will be in 2 pages with that.
7656f991cc3SEric DeVolder 	 */
7666f991cc3SEric DeVolder 	BUILD_BUG_ON(size > PAGE_SIZE);
7676f991cc3SEric DeVolder 
7686f991cc3SEric DeVolder 	crash_notes = __alloc_percpu(size, align);
7696f991cc3SEric DeVolder 	if (!crash_notes) {
7706f991cc3SEric DeVolder 		pr_warn("Memory allocation for saving cpu register states failed\n");
7716f991cc3SEric DeVolder 		return -ENOMEM;
7726f991cc3SEric DeVolder 	}
7736f991cc3SEric DeVolder 	return 0;
7746f991cc3SEric DeVolder }
7756f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init);
77624726275SEric DeVolder 
77724726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
77824726275SEric DeVolder #undef pr_fmt
77924726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt
780a72bbec7SEric DeVolder 
781a72bbec7SEric DeVolder /*
782e2a8f20dSBaoquan He  * Different than kexec/kdump loading/unloading/jumping/shrinking which
783e2a8f20dSBaoquan He  * usually rarely happen, there will be many crash hotplug events notified
784e2a8f20dSBaoquan He  * during one short period, e.g one memory board is hot added and memory
785e2a8f20dSBaoquan He  * regions are online. So mutex lock  __crash_hotplug_lock is used to
786e2a8f20dSBaoquan He  * serialize the crash hotplug handling specifically.
787e2a8f20dSBaoquan He  */
788e2a8f20dSBaoquan He DEFINE_MUTEX(__crash_hotplug_lock);
789e2a8f20dSBaoquan He #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock)
790e2a8f20dSBaoquan He #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock)
791e2a8f20dSBaoquan He 
792e2a8f20dSBaoquan He /*
793a72bbec7SEric DeVolder  * This routine utilized when the crash_hotplug sysfs node is read.
794a72bbec7SEric DeVolder  * It reflects the kernel's ability/permission to update the crash
795a72bbec7SEric DeVolder  * elfcorehdr directly.
796a72bbec7SEric DeVolder  */
797a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void)
798a72bbec7SEric DeVolder {
799a72bbec7SEric DeVolder 	int rc = 0;
800a72bbec7SEric DeVolder 
801e2a8f20dSBaoquan He 	crash_hotplug_lock();
802a72bbec7SEric DeVolder 	/* Obtain lock while reading crash information */
803a72bbec7SEric DeVolder 	if (!kexec_trylock()) {
804a72bbec7SEric DeVolder 		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
805e2a8f20dSBaoquan He 		crash_hotplug_unlock();
806a72bbec7SEric DeVolder 		return 0;
807a72bbec7SEric DeVolder 	}
808a72bbec7SEric DeVolder 	if (kexec_crash_image) {
809a72bbec7SEric DeVolder 		if (kexec_crash_image->file_mode)
810a72bbec7SEric DeVolder 			rc = 1;
811a72bbec7SEric DeVolder 		else
812a72bbec7SEric DeVolder 			rc = kexec_crash_image->update_elfcorehdr;
813a72bbec7SEric DeVolder 	}
814a72bbec7SEric DeVolder 	/* Release lock now that update complete */
815a72bbec7SEric DeVolder 	kexec_unlock();
816e2a8f20dSBaoquan He 	crash_hotplug_unlock();
817a72bbec7SEric DeVolder 
818a72bbec7SEric DeVolder 	return rc;
819a72bbec7SEric DeVolder }
820a72bbec7SEric DeVolder 
82124726275SEric DeVolder /*
82224726275SEric DeVolder  * To accurately reflect hot un/plug changes of cpu and memory resources
82324726275SEric DeVolder  * (including onling and offlining of those resources), the elfcorehdr
82424726275SEric DeVolder  * (which is passed to the crash kernel via the elfcorehdr= parameter)
82524726275SEric DeVolder  * must be updated with the new list of CPUs and memories.
82624726275SEric DeVolder  *
82724726275SEric DeVolder  * In order to make changes to elfcorehdr, two conditions are needed:
82824726275SEric DeVolder  * First, the segment containing the elfcorehdr must be large enough
82924726275SEric DeVolder  * to permit a growing number of resources; the elfcorehdr memory size
83024726275SEric DeVolder  * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES.
83124726275SEric DeVolder  * Second, purgatory must explicitly exclude the elfcorehdr from the
83224726275SEric DeVolder  * list of segments it checks (since the elfcorehdr changes and thus
83324726275SEric DeVolder  * would require an update to purgatory itself to update the digest).
83424726275SEric DeVolder  */
83524726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu)
83624726275SEric DeVolder {
83724726275SEric DeVolder 	struct kimage *image;
83824726275SEric DeVolder 
839e2a8f20dSBaoquan He 	crash_hotplug_lock();
84024726275SEric DeVolder 	/* Obtain lock while changing crash information */
84124726275SEric DeVolder 	if (!kexec_trylock()) {
84224726275SEric DeVolder 		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
843e2a8f20dSBaoquan He 		crash_hotplug_unlock();
84424726275SEric DeVolder 		return;
84524726275SEric DeVolder 	}
84624726275SEric DeVolder 
84724726275SEric DeVolder 	/* Check kdump is not loaded */
84824726275SEric DeVolder 	if (!kexec_crash_image)
84924726275SEric DeVolder 		goto out;
85024726275SEric DeVolder 
85124726275SEric DeVolder 	image = kexec_crash_image;
85224726275SEric DeVolder 
853a72bbec7SEric DeVolder 	/* Check that updating elfcorehdr is permitted */
854a72bbec7SEric DeVolder 	if (!(image->file_mode || image->update_elfcorehdr))
855a72bbec7SEric DeVolder 		goto out;
856a72bbec7SEric DeVolder 
85724726275SEric DeVolder 	if (hp_action == KEXEC_CRASH_HP_ADD_CPU ||
85824726275SEric DeVolder 		hp_action == KEXEC_CRASH_HP_REMOVE_CPU)
85924726275SEric DeVolder 		pr_debug("hp_action %u, cpu %u\n", hp_action, cpu);
86024726275SEric DeVolder 	else
86124726275SEric DeVolder 		pr_debug("hp_action %u\n", hp_action);
86224726275SEric DeVolder 
86324726275SEric DeVolder 	/*
86424726275SEric DeVolder 	 * The elfcorehdr_index is set to -1 when the struct kimage
86524726275SEric DeVolder 	 * is allocated. Find the segment containing the elfcorehdr,
86624726275SEric DeVolder 	 * if not already found.
86724726275SEric DeVolder 	 */
86824726275SEric DeVolder 	if (image->elfcorehdr_index < 0) {
86924726275SEric DeVolder 		unsigned long mem;
87024726275SEric DeVolder 		unsigned char *ptr;
87124726275SEric DeVolder 		unsigned int n;
87224726275SEric DeVolder 
87324726275SEric DeVolder 		for (n = 0; n < image->nr_segments; n++) {
87424726275SEric DeVolder 			mem = image->segment[n].mem;
87524726275SEric DeVolder 			ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
87624726275SEric DeVolder 			if (ptr) {
87724726275SEric DeVolder 				/* The segment containing elfcorehdr */
87824726275SEric DeVolder 				if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
87924726275SEric DeVolder 					image->elfcorehdr_index = (int)n;
88024726275SEric DeVolder 				kunmap_local(ptr);
88124726275SEric DeVolder 			}
88224726275SEric DeVolder 		}
88324726275SEric DeVolder 	}
88424726275SEric DeVolder 
88524726275SEric DeVolder 	if (image->elfcorehdr_index < 0) {
88624726275SEric DeVolder 		pr_err("unable to locate elfcorehdr segment");
88724726275SEric DeVolder 		goto out;
88824726275SEric DeVolder 	}
88924726275SEric DeVolder 
89024726275SEric DeVolder 	/* Needed in order for the segments to be updated */
89124726275SEric DeVolder 	arch_kexec_unprotect_crashkres();
89224726275SEric DeVolder 
89324726275SEric DeVolder 	/* Differentiate between normal load and hotplug update */
89424726275SEric DeVolder 	image->hp_action = hp_action;
89524726275SEric DeVolder 
89624726275SEric DeVolder 	/* Now invoke arch-specific update handler */
89724726275SEric DeVolder 	arch_crash_handle_hotplug_event(image);
89824726275SEric DeVolder 
89924726275SEric DeVolder 	/* No longer handling a hotplug event */
90024726275SEric DeVolder 	image->hp_action = KEXEC_CRASH_HP_NONE;
90124726275SEric DeVolder 	image->elfcorehdr_updated = true;
90224726275SEric DeVolder 
90324726275SEric DeVolder 	/* Change back to read-only */
90424726275SEric DeVolder 	arch_kexec_protect_crashkres();
90524726275SEric DeVolder 
90624726275SEric DeVolder 	/* Errors in the callback is not a reason to rollback state */
90724726275SEric DeVolder out:
90824726275SEric DeVolder 	/* Release lock now that update complete */
90924726275SEric DeVolder 	kexec_unlock();
910e2a8f20dSBaoquan He 	crash_hotplug_unlock();
91124726275SEric DeVolder }
91224726275SEric DeVolder 
91324726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v)
91424726275SEric DeVolder {
91524726275SEric DeVolder 	switch (val) {
91624726275SEric DeVolder 	case MEM_ONLINE:
91724726275SEric DeVolder 		crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY,
91824726275SEric DeVolder 			KEXEC_CRASH_HP_INVALID_CPU);
91924726275SEric DeVolder 		break;
92024726275SEric DeVolder 
92124726275SEric DeVolder 	case MEM_OFFLINE:
92224726275SEric DeVolder 		crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY,
92324726275SEric DeVolder 			KEXEC_CRASH_HP_INVALID_CPU);
92424726275SEric DeVolder 		break;
92524726275SEric DeVolder 	}
92624726275SEric DeVolder 	return NOTIFY_OK;
92724726275SEric DeVolder }
92824726275SEric DeVolder 
92924726275SEric DeVolder static struct notifier_block crash_memhp_nb = {
93024726275SEric DeVolder 	.notifier_call = crash_memhp_notifier,
93124726275SEric DeVolder 	.priority = 0
93224726275SEric DeVolder };
93324726275SEric DeVolder 
93424726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu)
93524726275SEric DeVolder {
93624726275SEric DeVolder 	crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu);
93724726275SEric DeVolder 	return 0;
93824726275SEric DeVolder }
93924726275SEric DeVolder 
94024726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu)
94124726275SEric DeVolder {
94224726275SEric DeVolder 	crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu);
94324726275SEric DeVolder 	return 0;
94424726275SEric DeVolder }
94524726275SEric DeVolder 
94624726275SEric DeVolder static int __init crash_hotplug_init(void)
94724726275SEric DeVolder {
94824726275SEric DeVolder 	int result = 0;
94924726275SEric DeVolder 
95024726275SEric DeVolder 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
95124726275SEric DeVolder 		register_memory_notifier(&crash_memhp_nb);
95224726275SEric DeVolder 
95324726275SEric DeVolder 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
95424726275SEric DeVolder 		result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
95524726275SEric DeVolder 			"crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline);
95624726275SEric DeVolder 	}
95724726275SEric DeVolder 
95824726275SEric DeVolder 	return result;
95924726275SEric DeVolder }
96024726275SEric DeVolder 
96124726275SEric DeVolder subsys_initcall(crash_hotplug_init);
96224726275SEric DeVolder #endif
963