xref: /linux-6.15/kernel/crash_core.c (revision a9e1a3d8)
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.
286692f66f2SHari Bathini  */
287692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline,
288692f66f2SHari Bathini 			     unsigned long long system_ram,
289692f66f2SHari Bathini 			     unsigned long long *crash_size,
290*a9e1a3d8SBaoquan He 			     unsigned long long *crash_base,
291*a9e1a3d8SBaoquan He 			     unsigned long long *low_size,
292*a9e1a3d8SBaoquan He 			     bool *high)
293692f66f2SHari Bathini {
294*a9e1a3d8SBaoquan He 	int ret;
295*a9e1a3d8SBaoquan He 
296*a9e1a3d8SBaoquan He 	/* crashkernel=X[@offset] */
297*a9e1a3d8SBaoquan He 	ret = __parse_crashkernel(cmdline, system_ram, crash_size,
298*a9e1a3d8SBaoquan He 				crash_base, NULL);
299*a9e1a3d8SBaoquan He 	if (!high)
300*a9e1a3d8SBaoquan He 		return ret;
301*a9e1a3d8SBaoquan He 
302*a9e1a3d8SBaoquan He 	return 0;
303692f66f2SHari Bathini }
304692f66f2SHari Bathini 
305692f66f2SHari Bathini int __init parse_crashkernel_high(char *cmdline,
306692f66f2SHari Bathini 			     unsigned long long system_ram,
307692f66f2SHari Bathini 			     unsigned long long *crash_size,
308692f66f2SHari Bathini 			     unsigned long long *crash_base)
309692f66f2SHari Bathini {
310692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
311a6304272SBaoquan He 				suffix_tbl[SUFFIX_HIGH]);
312692f66f2SHari Bathini }
313692f66f2SHari Bathini 
314692f66f2SHari Bathini int __init parse_crashkernel_low(char *cmdline,
315692f66f2SHari Bathini 			     unsigned long long system_ram,
316692f66f2SHari Bathini 			     unsigned long long *crash_size,
317692f66f2SHari Bathini 			     unsigned long long *crash_base)
318692f66f2SHari Bathini {
319692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
320a6304272SBaoquan He 				suffix_tbl[SUFFIX_LOW]);
321692f66f2SHari Bathini }
322692f66f2SHari Bathini 
32371d2bcecSPhilipp Rudo /*
32471d2bcecSPhilipp Rudo  * Add a dummy early_param handler to mark crashkernel= as a known command line
32571d2bcecSPhilipp Rudo  * parameter and suppress incorrect warnings in init/main.c.
32671d2bcecSPhilipp Rudo  */
32771d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg)
32871d2bcecSPhilipp Rudo {
32971d2bcecSPhilipp Rudo 	return 0;
33071d2bcecSPhilipp Rudo }
33171d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy);
33271d2bcecSPhilipp Rudo 
3336f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
3346f991cc3SEric DeVolder 			  void **addr, unsigned long *sz)
3356f991cc3SEric DeVolder {
3366f991cc3SEric DeVolder 	Elf64_Ehdr *ehdr;
3376f991cc3SEric DeVolder 	Elf64_Phdr *phdr;
3386f991cc3SEric DeVolder 	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
3396f991cc3SEric DeVolder 	unsigned char *buf;
3406f991cc3SEric DeVolder 	unsigned int cpu, i;
3416f991cc3SEric DeVolder 	unsigned long long notes_addr;
3426f991cc3SEric DeVolder 	unsigned long mstart, mend;
3436f991cc3SEric DeVolder 
3446f991cc3SEric DeVolder 	/* extra phdr for vmcoreinfo ELF note */
3456f991cc3SEric DeVolder 	nr_phdr = nr_cpus + 1;
3466f991cc3SEric DeVolder 	nr_phdr += mem->nr_ranges;
3476f991cc3SEric DeVolder 
3486f991cc3SEric DeVolder 	/*
3496f991cc3SEric DeVolder 	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
3506f991cc3SEric DeVolder 	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
3516f991cc3SEric DeVolder 	 * I think this is required by tools like gdb. So same physical
3526f991cc3SEric DeVolder 	 * memory will be mapped in two ELF headers. One will contain kernel
3536f991cc3SEric DeVolder 	 * text virtual addresses and other will have __va(physical) addresses.
3546f991cc3SEric DeVolder 	 */
3556f991cc3SEric DeVolder 
3566f991cc3SEric DeVolder 	nr_phdr++;
3576f991cc3SEric DeVolder 	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
3586f991cc3SEric DeVolder 	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
3596f991cc3SEric DeVolder 
3606f991cc3SEric DeVolder 	buf = vzalloc(elf_sz);
3616f991cc3SEric DeVolder 	if (!buf)
3626f991cc3SEric DeVolder 		return -ENOMEM;
3636f991cc3SEric DeVolder 
3646f991cc3SEric DeVolder 	ehdr = (Elf64_Ehdr *)buf;
3656f991cc3SEric DeVolder 	phdr = (Elf64_Phdr *)(ehdr + 1);
3666f991cc3SEric DeVolder 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
3676f991cc3SEric DeVolder 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
3686f991cc3SEric DeVolder 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
3696f991cc3SEric DeVolder 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
3706f991cc3SEric DeVolder 	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
3716f991cc3SEric DeVolder 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
3726f991cc3SEric DeVolder 	ehdr->e_type = ET_CORE;
3736f991cc3SEric DeVolder 	ehdr->e_machine = ELF_ARCH;
3746f991cc3SEric DeVolder 	ehdr->e_version = EV_CURRENT;
3756f991cc3SEric DeVolder 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
3766f991cc3SEric DeVolder 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
3776f991cc3SEric DeVolder 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
3786f991cc3SEric DeVolder 
379a396d0f8SEric DeVolder 	/* Prepare one phdr of type PT_NOTE for each possible CPU */
380a396d0f8SEric DeVolder 	for_each_possible_cpu(cpu) {
3816f991cc3SEric DeVolder 		phdr->p_type = PT_NOTE;
3826f991cc3SEric DeVolder 		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
3836f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = notes_addr;
3846f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
3856f991cc3SEric DeVolder 		(ehdr->e_phnum)++;
3866f991cc3SEric DeVolder 		phdr++;
3876f991cc3SEric DeVolder 	}
3886f991cc3SEric DeVolder 
3896f991cc3SEric DeVolder 	/* Prepare one PT_NOTE header for vmcoreinfo */
3906f991cc3SEric DeVolder 	phdr->p_type = PT_NOTE;
3916f991cc3SEric DeVolder 	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
3926f991cc3SEric DeVolder 	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
3936f991cc3SEric DeVolder 	(ehdr->e_phnum)++;
3946f991cc3SEric DeVolder 	phdr++;
3956f991cc3SEric DeVolder 
3966f991cc3SEric DeVolder 	/* Prepare PT_LOAD type program header for kernel text region */
3976f991cc3SEric DeVolder 	if (need_kernel_map) {
3986f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
3996f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
4006f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) _text;
4016f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = _end - _text;
4026f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
4036f991cc3SEric DeVolder 		ehdr->e_phnum++;
4046f991cc3SEric DeVolder 		phdr++;
4056f991cc3SEric DeVolder 	}
4066f991cc3SEric DeVolder 
4076f991cc3SEric DeVolder 	/* Go through all the ranges in mem->ranges[] and prepare phdr */
4086f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
4096f991cc3SEric DeVolder 		mstart = mem->ranges[i].start;
4106f991cc3SEric DeVolder 		mend = mem->ranges[i].end;
4116f991cc3SEric DeVolder 
4126f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
4136f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
4146f991cc3SEric DeVolder 		phdr->p_offset  = mstart;
4156f991cc3SEric DeVolder 
4166f991cc3SEric DeVolder 		phdr->p_paddr = mstart;
4176f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) __va(mstart);
4186f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
4196f991cc3SEric DeVolder 		phdr->p_align = 0;
4206f991cc3SEric DeVolder 		ehdr->e_phnum++;
4216f991cc3SEric 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",
4226f991cc3SEric DeVolder 			phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
4236f991cc3SEric DeVolder 			ehdr->e_phnum, phdr->p_offset);
4246f991cc3SEric DeVolder 		phdr++;
4256f991cc3SEric DeVolder 	}
4266f991cc3SEric DeVolder 
4276f991cc3SEric DeVolder 	*addr = buf;
4286f991cc3SEric DeVolder 	*sz = elf_sz;
4296f991cc3SEric DeVolder 	return 0;
4306f991cc3SEric DeVolder }
4316f991cc3SEric DeVolder 
4326f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem,
4336f991cc3SEric DeVolder 			    unsigned long long mstart, unsigned long long mend)
4346f991cc3SEric DeVolder {
4356f991cc3SEric DeVolder 	int i, j;
4366f991cc3SEric DeVolder 	unsigned long long start, end, p_start, p_end;
4376f991cc3SEric DeVolder 	struct range temp_range = {0, 0};
4386f991cc3SEric DeVolder 
4396f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
4406f991cc3SEric DeVolder 		start = mem->ranges[i].start;
4416f991cc3SEric DeVolder 		end = mem->ranges[i].end;
4426f991cc3SEric DeVolder 		p_start = mstart;
4436f991cc3SEric DeVolder 		p_end = mend;
4446f991cc3SEric DeVolder 
4456f991cc3SEric DeVolder 		if (mstart > end || mend < start)
4466f991cc3SEric DeVolder 			continue;
4476f991cc3SEric DeVolder 
4486f991cc3SEric DeVolder 		/* Truncate any area outside of range */
4496f991cc3SEric DeVolder 		if (mstart < start)
4506f991cc3SEric DeVolder 			p_start = start;
4516f991cc3SEric DeVolder 		if (mend > end)
4526f991cc3SEric DeVolder 			p_end = end;
4536f991cc3SEric DeVolder 
4546f991cc3SEric DeVolder 		/* Found completely overlapping range */
4556f991cc3SEric DeVolder 		if (p_start == start && p_end == end) {
4566f991cc3SEric DeVolder 			mem->ranges[i].start = 0;
4576f991cc3SEric DeVolder 			mem->ranges[i].end = 0;
4586f991cc3SEric DeVolder 			if (i < mem->nr_ranges - 1) {
4596f991cc3SEric DeVolder 				/* Shift rest of the ranges to left */
4606f991cc3SEric DeVolder 				for (j = i; j < mem->nr_ranges - 1; j++) {
4616f991cc3SEric DeVolder 					mem->ranges[j].start =
4626f991cc3SEric DeVolder 						mem->ranges[j+1].start;
4636f991cc3SEric DeVolder 					mem->ranges[j].end =
4646f991cc3SEric DeVolder 							mem->ranges[j+1].end;
4656f991cc3SEric DeVolder 				}
4666f991cc3SEric DeVolder 
4676f991cc3SEric DeVolder 				/*
4686f991cc3SEric DeVolder 				 * Continue to check if there are another overlapping ranges
4696f991cc3SEric DeVolder 				 * from the current position because of shifting the above
4706f991cc3SEric DeVolder 				 * mem ranges.
4716f991cc3SEric DeVolder 				 */
4726f991cc3SEric DeVolder 				i--;
4736f991cc3SEric DeVolder 				mem->nr_ranges--;
4746f991cc3SEric DeVolder 				continue;
4756f991cc3SEric DeVolder 			}
4766f991cc3SEric DeVolder 			mem->nr_ranges--;
4776f991cc3SEric DeVolder 			return 0;
4786f991cc3SEric DeVolder 		}
4796f991cc3SEric DeVolder 
4806f991cc3SEric DeVolder 		if (p_start > start && p_end < end) {
4816f991cc3SEric DeVolder 			/* Split original range */
4826f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
4836f991cc3SEric DeVolder 			temp_range.start = p_end + 1;
4846f991cc3SEric DeVolder 			temp_range.end = end;
4856f991cc3SEric DeVolder 		} else if (p_start != start)
4866f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
4876f991cc3SEric DeVolder 		else
4886f991cc3SEric DeVolder 			mem->ranges[i].start = p_end + 1;
4896f991cc3SEric DeVolder 		break;
4906f991cc3SEric DeVolder 	}
4916f991cc3SEric DeVolder 
4926f991cc3SEric DeVolder 	/* If a split happened, add the split to array */
4936f991cc3SEric DeVolder 	if (!temp_range.end)
4946f991cc3SEric DeVolder 		return 0;
4956f991cc3SEric DeVolder 
4966f991cc3SEric DeVolder 	/* Split happened */
4976f991cc3SEric DeVolder 	if (i == mem->max_nr_ranges - 1)
4986f991cc3SEric DeVolder 		return -ENOMEM;
4996f991cc3SEric DeVolder 
5006f991cc3SEric DeVolder 	/* Location where new range should go */
5016f991cc3SEric DeVolder 	j = i + 1;
5026f991cc3SEric DeVolder 	if (j < mem->nr_ranges) {
5036f991cc3SEric DeVolder 		/* Move over all ranges one slot towards the end */
5046f991cc3SEric DeVolder 		for (i = mem->nr_ranges - 1; i >= j; i--)
5056f991cc3SEric DeVolder 			mem->ranges[i + 1] = mem->ranges[i];
5066f991cc3SEric DeVolder 	}
5076f991cc3SEric DeVolder 
5086f991cc3SEric DeVolder 	mem->ranges[j].start = temp_range.start;
5096f991cc3SEric DeVolder 	mem->ranges[j].end = temp_range.end;
5106f991cc3SEric DeVolder 	mem->nr_ranges++;
5116f991cc3SEric DeVolder 	return 0;
5126f991cc3SEric DeVolder }
5136f991cc3SEric DeVolder 
51451dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
515692f66f2SHari Bathini 			  void *data, size_t data_len)
516692f66f2SHari Bathini {
51751dbd925SHari Bathini 	struct elf_note *note = (struct elf_note *)buf;
518692f66f2SHari Bathini 
51951dbd925SHari Bathini 	note->n_namesz = strlen(name) + 1;
52051dbd925SHari Bathini 	note->n_descsz = data_len;
52151dbd925SHari Bathini 	note->n_type   = type;
52251dbd925SHari Bathini 	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
52351dbd925SHari Bathini 	memcpy(buf, name, note->n_namesz);
52451dbd925SHari Bathini 	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
52551dbd925SHari Bathini 	memcpy(buf, data, data_len);
52651dbd925SHari Bathini 	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
527692f66f2SHari Bathini 
528692f66f2SHari Bathini 	return buf;
529692f66f2SHari Bathini }
530692f66f2SHari Bathini 
53151dbd925SHari Bathini void final_note(Elf_Word *buf)
532692f66f2SHari Bathini {
53351dbd925SHari Bathini 	memset(buf, 0, sizeof(struct elf_note));
534692f66f2SHari Bathini }
535692f66f2SHari Bathini 
536692f66f2SHari Bathini static void update_vmcoreinfo_note(void)
537692f66f2SHari Bathini {
538692f66f2SHari Bathini 	u32 *buf = vmcoreinfo_note;
539692f66f2SHari Bathini 
540692f66f2SHari Bathini 	if (!vmcoreinfo_size)
541692f66f2SHari Bathini 		return;
542692f66f2SHari Bathini 	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
543692f66f2SHari Bathini 			      vmcoreinfo_size);
544692f66f2SHari Bathini 	final_note(buf);
545692f66f2SHari Bathini }
546692f66f2SHari Bathini 
5471229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr)
5481229384fSXunlei Pang {
5491229384fSXunlei Pang 	if (ptr)
5501229384fSXunlei Pang 		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
5511229384fSXunlei Pang 
5521229384fSXunlei Pang 	vmcoreinfo_data_safecopy = ptr;
5531229384fSXunlei Pang }
5541229384fSXunlei Pang 
555692f66f2SHari Bathini void crash_save_vmcoreinfo(void)
556692f66f2SHari Bathini {
557203e9e41SXunlei Pang 	if (!vmcoreinfo_note)
558203e9e41SXunlei Pang 		return;
559203e9e41SXunlei Pang 
5601229384fSXunlei Pang 	/* Use the safe copy to generate vmcoreinfo note if have */
5611229384fSXunlei Pang 	if (vmcoreinfo_data_safecopy)
5621229384fSXunlei Pang 		vmcoreinfo_data = vmcoreinfo_data_safecopy;
5631229384fSXunlei Pang 
56491bc9aafSArnd Bergmann 	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
565692f66f2SHari Bathini 	update_vmcoreinfo_note();
566692f66f2SHari Bathini }
567692f66f2SHari Bathini 
568692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...)
569692f66f2SHari Bathini {
570692f66f2SHari Bathini 	va_list args;
571692f66f2SHari Bathini 	char buf[0x50];
572692f66f2SHari Bathini 	size_t r;
573692f66f2SHari Bathini 
574692f66f2SHari Bathini 	va_start(args, fmt);
575692f66f2SHari Bathini 	r = vscnprintf(buf, sizeof(buf), fmt, args);
576692f66f2SHari Bathini 	va_end(args);
577692f66f2SHari Bathini 
5785203f499SXunlei Pang 	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
579692f66f2SHari Bathini 
580692f66f2SHari Bathini 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
581692f66f2SHari Bathini 
582692f66f2SHari Bathini 	vmcoreinfo_size += r;
58308fc35f3SStephen Brennan 
58408fc35f3SStephen Brennan 	WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES,
58508fc35f3SStephen Brennan 		  "vmcoreinfo data exceeds allocated size, truncating");
586692f66f2SHari Bathini }
587692f66f2SHari Bathini 
588692f66f2SHari Bathini /*
589692f66f2SHari Bathini  * provide an empty default implementation here -- architecture
590692f66f2SHari Bathini  * code may override this
591692f66f2SHari Bathini  */
592692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void)
593692f66f2SHari Bathini {}
594692f66f2SHari Bathini 
595692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void)
596692f66f2SHari Bathini {
597203e9e41SXunlei Pang 	return __pa(vmcoreinfo_note);
598692f66f2SHari Bathini }
59943d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note);
600692f66f2SHari Bathini 
601692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void)
602692f66f2SHari Bathini {
603203e9e41SXunlei Pang 	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
604203e9e41SXunlei Pang 	if (!vmcoreinfo_data) {
605203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
606203e9e41SXunlei Pang 		return -ENOMEM;
607203e9e41SXunlei Pang 	}
608203e9e41SXunlei Pang 
609203e9e41SXunlei Pang 	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
610203e9e41SXunlei Pang 						GFP_KERNEL | __GFP_ZERO);
611203e9e41SXunlei Pang 	if (!vmcoreinfo_note) {
612203e9e41SXunlei Pang 		free_page((unsigned long)vmcoreinfo_data);
613203e9e41SXunlei Pang 		vmcoreinfo_data = NULL;
614203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
615203e9e41SXunlei Pang 		return -ENOMEM;
616203e9e41SXunlei Pang 	}
617203e9e41SXunlei Pang 
618692f66f2SHari Bathini 	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
61944e8a5e9SStephen Boyd 	VMCOREINFO_BUILD_ID();
620692f66f2SHari Bathini 	VMCOREINFO_PAGESIZE(PAGE_SIZE);
621692f66f2SHari Bathini 
622692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(init_uts_ns);
623ca4a9241SAlexander Egorenkov 	VMCOREINFO_OFFSET(uts_namespace, name);
624692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(node_online_map);
625692f66f2SHari Bathini #ifdef CONFIG_MMU
626eff4345eSOmar Sandoval 	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
627692f66f2SHari Bathini #endif
628692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(_stext);
629692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(vmap_area_list);
630692f66f2SHari Bathini 
631a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA
632692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(mem_map);
633692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(contig_page_data);
634692f66f2SHari Bathini #endif
635692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM
636a0b12803SKirill A. Shutemov 	VMCOREINFO_SYMBOL_ARRAY(mem_section);
637692f66f2SHari Bathini 	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
638692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(mem_section);
639692f66f2SHari Bathini 	VMCOREINFO_OFFSET(mem_section, section_mem_map);
6404f5aecdfSPingfan Liu 	VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
6411d50e5d0SBhupesh Sharma 	VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
642692f66f2SHari Bathini #endif
643692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(page);
644692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(pglist_data);
645692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(zone);
646692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(free_area);
647692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(list_head);
648692f66f2SHari Bathini 	VMCOREINFO_SIZE(nodemask_t);
649692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, flags);
650692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _refcount);
651692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, mapping);
652692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, lru);
653692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _mapcount);
654692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, private);
655692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, compound_head);
656692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_zones);
657692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, nr_zones);
65843b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM
659692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
660692f66f2SHari Bathini #endif
661692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
662692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
663692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_id);
664692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, free_area);
665692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, vm_stat);
666692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, spanned_pages);
667692f66f2SHari Bathini 	VMCOREINFO_OFFSET(free_area, free_list);
668692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, next);
669692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, prev);
670692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, va_start);
671692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, list);
67223baf831SKirill A. Shutemov 	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER + 1);
673692f66f2SHari Bathini 	log_buf_vmcoreinfo_setup();
674692f66f2SHari Bathini 	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
675692f66f2SHari Bathini 	VMCOREINFO_NUMBER(NR_FREE_PAGES);
676692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_lru);
677692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_private);
678692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_swapcache);
6791cbf29daSPetr Tesarik 	VMCOREINFO_NUMBER(PG_swapbacked);
680692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_slab);
681692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE
682692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_hwpoison);
683692f66f2SHari Bathini #endif
684692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_head_mask);
6856e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE	(~PG_buddy)
686692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
687692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
6889c5ccf2dSMatthew Wilcox (Oracle) 	VMCOREINFO_NUMBER(PG_hugetlb);
689e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE	(~PG_offline)
690e04b742fSDavid Hildenbrand 	VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
691692f66f2SHari Bathini #endif
692692f66f2SHari Bathini 
6935fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS
6945fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_names);
695f09bddbdSStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_num_syms);
6965fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_table);
6975fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_index);
6985fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE
6995fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_offsets);
7005fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_relative_base);
7015fd8fea9SStephen Brennan #else
7025fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_addresses);
7035fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */
7045fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */
7055fd8fea9SStephen Brennan 
706692f66f2SHari Bathini 	arch_crash_save_vmcoreinfo();
707692f66f2SHari Bathini 	update_vmcoreinfo_note();
708692f66f2SHari Bathini 
709692f66f2SHari Bathini 	return 0;
710692f66f2SHari Bathini }
711692f66f2SHari Bathini 
712692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init);
7136f991cc3SEric DeVolder 
7146f991cc3SEric DeVolder static int __init crash_notes_memory_init(void)
7156f991cc3SEric DeVolder {
7166f991cc3SEric DeVolder 	/* Allocate memory for saving cpu registers. */
7176f991cc3SEric DeVolder 	size_t size, align;
7186f991cc3SEric DeVolder 
7196f991cc3SEric DeVolder 	/*
7206f991cc3SEric DeVolder 	 * crash_notes could be allocated across 2 vmalloc pages when percpu
7216f991cc3SEric DeVolder 	 * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
7226f991cc3SEric DeVolder 	 * pages are also on 2 continuous physical pages. In this case the
7236f991cc3SEric DeVolder 	 * 2nd part of crash_notes in 2nd page could be lost since only the
7246f991cc3SEric DeVolder 	 * starting address and size of crash_notes are exported through sysfs.
7256f991cc3SEric DeVolder 	 * Here round up the size of crash_notes to the nearest power of two
7266f991cc3SEric DeVolder 	 * and pass it to __alloc_percpu as align value. This can make sure
7276f991cc3SEric DeVolder 	 * crash_notes is allocated inside one physical page.
7286f991cc3SEric DeVolder 	 */
7296f991cc3SEric DeVolder 	size = sizeof(note_buf_t);
7306f991cc3SEric DeVolder 	align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
7316f991cc3SEric DeVolder 
7326f991cc3SEric DeVolder 	/*
7336f991cc3SEric DeVolder 	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
7346f991cc3SEric DeVolder 	 * definitely will be in 2 pages with that.
7356f991cc3SEric DeVolder 	 */
7366f991cc3SEric DeVolder 	BUILD_BUG_ON(size > PAGE_SIZE);
7376f991cc3SEric DeVolder 
7386f991cc3SEric DeVolder 	crash_notes = __alloc_percpu(size, align);
7396f991cc3SEric DeVolder 	if (!crash_notes) {
7406f991cc3SEric DeVolder 		pr_warn("Memory allocation for saving cpu register states failed\n");
7416f991cc3SEric DeVolder 		return -ENOMEM;
7426f991cc3SEric DeVolder 	}
7436f991cc3SEric DeVolder 	return 0;
7446f991cc3SEric DeVolder }
7456f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init);
74624726275SEric DeVolder 
74724726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
74824726275SEric DeVolder #undef pr_fmt
74924726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt
750a72bbec7SEric DeVolder 
751a72bbec7SEric DeVolder /*
752e2a8f20dSBaoquan He  * Different than kexec/kdump loading/unloading/jumping/shrinking which
753e2a8f20dSBaoquan He  * usually rarely happen, there will be many crash hotplug events notified
754e2a8f20dSBaoquan He  * during one short period, e.g one memory board is hot added and memory
755e2a8f20dSBaoquan He  * regions are online. So mutex lock  __crash_hotplug_lock is used to
756e2a8f20dSBaoquan He  * serialize the crash hotplug handling specifically.
757e2a8f20dSBaoquan He  */
758e2a8f20dSBaoquan He DEFINE_MUTEX(__crash_hotplug_lock);
759e2a8f20dSBaoquan He #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock)
760e2a8f20dSBaoquan He #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock)
761e2a8f20dSBaoquan He 
762e2a8f20dSBaoquan He /*
763a72bbec7SEric DeVolder  * This routine utilized when the crash_hotplug sysfs node is read.
764a72bbec7SEric DeVolder  * It reflects the kernel's ability/permission to update the crash
765a72bbec7SEric DeVolder  * elfcorehdr directly.
766a72bbec7SEric DeVolder  */
767a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void)
768a72bbec7SEric DeVolder {
769a72bbec7SEric DeVolder 	int rc = 0;
770a72bbec7SEric DeVolder 
771e2a8f20dSBaoquan He 	crash_hotplug_lock();
772a72bbec7SEric DeVolder 	/* Obtain lock while reading crash information */
773a72bbec7SEric DeVolder 	if (!kexec_trylock()) {
774a72bbec7SEric DeVolder 		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
775e2a8f20dSBaoquan He 		crash_hotplug_unlock();
776a72bbec7SEric DeVolder 		return 0;
777a72bbec7SEric DeVolder 	}
778a72bbec7SEric DeVolder 	if (kexec_crash_image) {
779a72bbec7SEric DeVolder 		if (kexec_crash_image->file_mode)
780a72bbec7SEric DeVolder 			rc = 1;
781a72bbec7SEric DeVolder 		else
782a72bbec7SEric DeVolder 			rc = kexec_crash_image->update_elfcorehdr;
783a72bbec7SEric DeVolder 	}
784a72bbec7SEric DeVolder 	/* Release lock now that update complete */
785a72bbec7SEric DeVolder 	kexec_unlock();
786e2a8f20dSBaoquan He 	crash_hotplug_unlock();
787a72bbec7SEric DeVolder 
788a72bbec7SEric DeVolder 	return rc;
789a72bbec7SEric DeVolder }
790a72bbec7SEric DeVolder 
79124726275SEric DeVolder /*
79224726275SEric DeVolder  * To accurately reflect hot un/plug changes of cpu and memory resources
79324726275SEric DeVolder  * (including onling and offlining of those resources), the elfcorehdr
79424726275SEric DeVolder  * (which is passed to the crash kernel via the elfcorehdr= parameter)
79524726275SEric DeVolder  * must be updated with the new list of CPUs and memories.
79624726275SEric DeVolder  *
79724726275SEric DeVolder  * In order to make changes to elfcorehdr, two conditions are needed:
79824726275SEric DeVolder  * First, the segment containing the elfcorehdr must be large enough
79924726275SEric DeVolder  * to permit a growing number of resources; the elfcorehdr memory size
80024726275SEric DeVolder  * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES.
80124726275SEric DeVolder  * Second, purgatory must explicitly exclude the elfcorehdr from the
80224726275SEric DeVolder  * list of segments it checks (since the elfcorehdr changes and thus
80324726275SEric DeVolder  * would require an update to purgatory itself to update the digest).
80424726275SEric DeVolder  */
80524726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu)
80624726275SEric DeVolder {
80724726275SEric DeVolder 	struct kimage *image;
80824726275SEric DeVolder 
809e2a8f20dSBaoquan He 	crash_hotplug_lock();
81024726275SEric DeVolder 	/* Obtain lock while changing crash information */
81124726275SEric DeVolder 	if (!kexec_trylock()) {
81224726275SEric DeVolder 		pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n");
813e2a8f20dSBaoquan He 		crash_hotplug_unlock();
81424726275SEric DeVolder 		return;
81524726275SEric DeVolder 	}
81624726275SEric DeVolder 
81724726275SEric DeVolder 	/* Check kdump is not loaded */
81824726275SEric DeVolder 	if (!kexec_crash_image)
81924726275SEric DeVolder 		goto out;
82024726275SEric DeVolder 
82124726275SEric DeVolder 	image = kexec_crash_image;
82224726275SEric DeVolder 
823a72bbec7SEric DeVolder 	/* Check that updating elfcorehdr is permitted */
824a72bbec7SEric DeVolder 	if (!(image->file_mode || image->update_elfcorehdr))
825a72bbec7SEric DeVolder 		goto out;
826a72bbec7SEric DeVolder 
82724726275SEric DeVolder 	if (hp_action == KEXEC_CRASH_HP_ADD_CPU ||
82824726275SEric DeVolder 		hp_action == KEXEC_CRASH_HP_REMOVE_CPU)
82924726275SEric DeVolder 		pr_debug("hp_action %u, cpu %u\n", hp_action, cpu);
83024726275SEric DeVolder 	else
83124726275SEric DeVolder 		pr_debug("hp_action %u\n", hp_action);
83224726275SEric DeVolder 
83324726275SEric DeVolder 	/*
83424726275SEric DeVolder 	 * The elfcorehdr_index is set to -1 when the struct kimage
83524726275SEric DeVolder 	 * is allocated. Find the segment containing the elfcorehdr,
83624726275SEric DeVolder 	 * if not already found.
83724726275SEric DeVolder 	 */
83824726275SEric DeVolder 	if (image->elfcorehdr_index < 0) {
83924726275SEric DeVolder 		unsigned long mem;
84024726275SEric DeVolder 		unsigned char *ptr;
84124726275SEric DeVolder 		unsigned int n;
84224726275SEric DeVolder 
84324726275SEric DeVolder 		for (n = 0; n < image->nr_segments; n++) {
84424726275SEric DeVolder 			mem = image->segment[n].mem;
84524726275SEric DeVolder 			ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
84624726275SEric DeVolder 			if (ptr) {
84724726275SEric DeVolder 				/* The segment containing elfcorehdr */
84824726275SEric DeVolder 				if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
84924726275SEric DeVolder 					image->elfcorehdr_index = (int)n;
85024726275SEric DeVolder 				kunmap_local(ptr);
85124726275SEric DeVolder 			}
85224726275SEric DeVolder 		}
85324726275SEric DeVolder 	}
85424726275SEric DeVolder 
85524726275SEric DeVolder 	if (image->elfcorehdr_index < 0) {
85624726275SEric DeVolder 		pr_err("unable to locate elfcorehdr segment");
85724726275SEric DeVolder 		goto out;
85824726275SEric DeVolder 	}
85924726275SEric DeVolder 
86024726275SEric DeVolder 	/* Needed in order for the segments to be updated */
86124726275SEric DeVolder 	arch_kexec_unprotect_crashkres();
86224726275SEric DeVolder 
86324726275SEric DeVolder 	/* Differentiate between normal load and hotplug update */
86424726275SEric DeVolder 	image->hp_action = hp_action;
86524726275SEric DeVolder 
86624726275SEric DeVolder 	/* Now invoke arch-specific update handler */
86724726275SEric DeVolder 	arch_crash_handle_hotplug_event(image);
86824726275SEric DeVolder 
86924726275SEric DeVolder 	/* No longer handling a hotplug event */
87024726275SEric DeVolder 	image->hp_action = KEXEC_CRASH_HP_NONE;
87124726275SEric DeVolder 	image->elfcorehdr_updated = true;
87224726275SEric DeVolder 
87324726275SEric DeVolder 	/* Change back to read-only */
87424726275SEric DeVolder 	arch_kexec_protect_crashkres();
87524726275SEric DeVolder 
87624726275SEric DeVolder 	/* Errors in the callback is not a reason to rollback state */
87724726275SEric DeVolder out:
87824726275SEric DeVolder 	/* Release lock now that update complete */
87924726275SEric DeVolder 	kexec_unlock();
880e2a8f20dSBaoquan He 	crash_hotplug_unlock();
88124726275SEric DeVolder }
88224726275SEric DeVolder 
88324726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v)
88424726275SEric DeVolder {
88524726275SEric DeVolder 	switch (val) {
88624726275SEric DeVolder 	case MEM_ONLINE:
88724726275SEric DeVolder 		crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY,
88824726275SEric DeVolder 			KEXEC_CRASH_HP_INVALID_CPU);
88924726275SEric DeVolder 		break;
89024726275SEric DeVolder 
89124726275SEric DeVolder 	case MEM_OFFLINE:
89224726275SEric DeVolder 		crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY,
89324726275SEric DeVolder 			KEXEC_CRASH_HP_INVALID_CPU);
89424726275SEric DeVolder 		break;
89524726275SEric DeVolder 	}
89624726275SEric DeVolder 	return NOTIFY_OK;
89724726275SEric DeVolder }
89824726275SEric DeVolder 
89924726275SEric DeVolder static struct notifier_block crash_memhp_nb = {
90024726275SEric DeVolder 	.notifier_call = crash_memhp_notifier,
90124726275SEric DeVolder 	.priority = 0
90224726275SEric DeVolder };
90324726275SEric DeVolder 
90424726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu)
90524726275SEric DeVolder {
90624726275SEric DeVolder 	crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu);
90724726275SEric DeVolder 	return 0;
90824726275SEric DeVolder }
90924726275SEric DeVolder 
91024726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu)
91124726275SEric DeVolder {
91224726275SEric DeVolder 	crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu);
91324726275SEric DeVolder 	return 0;
91424726275SEric DeVolder }
91524726275SEric DeVolder 
91624726275SEric DeVolder static int __init crash_hotplug_init(void)
91724726275SEric DeVolder {
91824726275SEric DeVolder 	int result = 0;
91924726275SEric DeVolder 
92024726275SEric DeVolder 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
92124726275SEric DeVolder 		register_memory_notifier(&crash_memhp_nb);
92224726275SEric DeVolder 
92324726275SEric DeVolder 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
92424726275SEric DeVolder 		result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
92524726275SEric DeVolder 			"crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline);
92624726275SEric DeVolder 	}
92724726275SEric DeVolder 
92824726275SEric DeVolder 	return result;
92924726275SEric DeVolder }
93024726275SEric DeVolder 
93124726275SEric DeVolder subsys_initcall(crash_hotplug_init);
93224726275SEric DeVolder #endif
933