xref: /openbmc/linux/kernel/crash_core.c (revision 6f991cc3)
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  <ebiederm@xmission.com>
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>
13*6f991cc3SEric DeVolder #include <linux/kexec.h>
14692f66f2SHari Bathini 
15692f66f2SHari Bathini #include <asm/page.h>
16692f66f2SHari Bathini #include <asm/sections.h>
17692f66f2SHari Bathini 
18a24d22b2SEric Biggers #include <crypto/sha1.h>
190935288cSVijay Balakrishna 
205fd8fea9SStephen Brennan #include "kallsyms_internal.h"
215fd8fea9SStephen Brennan 
22*6f991cc3SEric DeVolder /* Per cpu memory for storing cpu states in case of system crash. */
23*6f991cc3SEric DeVolder note_buf_t __percpu *crash_notes;
24*6f991cc3SEric DeVolder 
25692f66f2SHari Bathini /* vmcoreinfo stuff */
2623c85094SOmar Sandoval unsigned char *vmcoreinfo_data;
2723c85094SOmar Sandoval size_t vmcoreinfo_size;
28203e9e41SXunlei Pang u32 *vmcoreinfo_note;
29692f66f2SHari Bathini 
301229384fSXunlei Pang /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
311229384fSXunlei Pang static unsigned char *vmcoreinfo_data_safecopy;
321229384fSXunlei Pang 
33692f66f2SHari Bathini /*
34692f66f2SHari Bathini  * parsing the "crashkernel" commandline
35692f66f2SHari Bathini  *
36692f66f2SHari Bathini  * this code is intended to be called from architecture specific code
37692f66f2SHari Bathini  */
38692f66f2SHari Bathini 
39692f66f2SHari Bathini 
40692f66f2SHari Bathini /*
41692f66f2SHari Bathini  * This function parses command lines in the format
42692f66f2SHari Bathini  *
43692f66f2SHari Bathini  *   crashkernel=ramsize-range:size[,...][@offset]
44692f66f2SHari Bathini  *
45692f66f2SHari Bathini  * The function returns 0 on success and -EINVAL on failure.
46692f66f2SHari Bathini  */
47692f66f2SHari Bathini static int __init parse_crashkernel_mem(char *cmdline,
48692f66f2SHari Bathini 					unsigned long long system_ram,
49692f66f2SHari Bathini 					unsigned long long *crash_size,
50692f66f2SHari Bathini 					unsigned long long *crash_base)
51692f66f2SHari Bathini {
52692f66f2SHari Bathini 	char *cur = cmdline, *tmp;
5346d36b1bSTao Liu 	unsigned long long total_mem = system_ram;
5446d36b1bSTao Liu 
5546d36b1bSTao Liu 	/*
5646d36b1bSTao Liu 	 * Firmware sometimes reserves some memory regions for its own use,
5746d36b1bSTao Liu 	 * so the system memory size is less than the actual physical memory
5846d36b1bSTao Liu 	 * size. Work around this by rounding up the total size to 128M,
5946d36b1bSTao Liu 	 * which is enough for most test cases.
6046d36b1bSTao Liu 	 */
6146d36b1bSTao Liu 	total_mem = roundup(total_mem, SZ_128M);
62692f66f2SHari Bathini 
63692f66f2SHari Bathini 	/* for each entry of the comma-separated list */
64692f66f2SHari Bathini 	do {
65692f66f2SHari Bathini 		unsigned long long start, end = ULLONG_MAX, size;
66692f66f2SHari Bathini 
67692f66f2SHari Bathini 		/* get the start of the range */
68692f66f2SHari Bathini 		start = memparse(cur, &tmp);
69692f66f2SHari Bathini 		if (cur == tmp) {
70692f66f2SHari Bathini 			pr_warn("crashkernel: Memory value expected\n");
71692f66f2SHari Bathini 			return -EINVAL;
72692f66f2SHari Bathini 		}
73692f66f2SHari Bathini 		cur = tmp;
74692f66f2SHari Bathini 		if (*cur != '-') {
75692f66f2SHari Bathini 			pr_warn("crashkernel: '-' expected\n");
76692f66f2SHari Bathini 			return -EINVAL;
77692f66f2SHari Bathini 		}
78692f66f2SHari Bathini 		cur++;
79692f66f2SHari Bathini 
80692f66f2SHari Bathini 		/* if no ':' is here, than we read the end */
81692f66f2SHari Bathini 		if (*cur != ':') {
82692f66f2SHari Bathini 			end = memparse(cur, &tmp);
83692f66f2SHari Bathini 			if (cur == tmp) {
84692f66f2SHari Bathini 				pr_warn("crashkernel: Memory value expected\n");
85692f66f2SHari Bathini 				return -EINVAL;
86692f66f2SHari Bathini 			}
87692f66f2SHari Bathini 			cur = tmp;
88692f66f2SHari Bathini 			if (end <= start) {
89692f66f2SHari Bathini 				pr_warn("crashkernel: end <= start\n");
90692f66f2SHari Bathini 				return -EINVAL;
91692f66f2SHari Bathini 			}
92692f66f2SHari Bathini 		}
93692f66f2SHari Bathini 
94692f66f2SHari Bathini 		if (*cur != ':') {
95692f66f2SHari Bathini 			pr_warn("crashkernel: ':' expected\n");
96692f66f2SHari Bathini 			return -EINVAL;
97692f66f2SHari Bathini 		}
98692f66f2SHari Bathini 		cur++;
99692f66f2SHari Bathini 
100692f66f2SHari Bathini 		size = memparse(cur, &tmp);
101692f66f2SHari Bathini 		if (cur == tmp) {
102692f66f2SHari Bathini 			pr_warn("Memory value expected\n");
103692f66f2SHari Bathini 			return -EINVAL;
104692f66f2SHari Bathini 		}
105692f66f2SHari Bathini 		cur = tmp;
10646d36b1bSTao Liu 		if (size >= total_mem) {
107692f66f2SHari Bathini 			pr_warn("crashkernel: invalid size\n");
108692f66f2SHari Bathini 			return -EINVAL;
109692f66f2SHari Bathini 		}
110692f66f2SHari Bathini 
111692f66f2SHari Bathini 		/* match ? */
11246d36b1bSTao Liu 		if (total_mem >= start && total_mem < end) {
113692f66f2SHari Bathini 			*crash_size = size;
114692f66f2SHari Bathini 			break;
115692f66f2SHari Bathini 		}
116692f66f2SHari Bathini 	} while (*cur++ == ',');
117692f66f2SHari Bathini 
118692f66f2SHari Bathini 	if (*crash_size > 0) {
119692f66f2SHari Bathini 		while (*cur && *cur != ' ' && *cur != '@')
120692f66f2SHari Bathini 			cur++;
121692f66f2SHari Bathini 		if (*cur == '@') {
122692f66f2SHari Bathini 			cur++;
123692f66f2SHari Bathini 			*crash_base = memparse(cur, &tmp);
124692f66f2SHari Bathini 			if (cur == tmp) {
125692f66f2SHari Bathini 				pr_warn("Memory value expected after '@'\n");
126692f66f2SHari Bathini 				return -EINVAL;
127692f66f2SHari Bathini 			}
128692f66f2SHari Bathini 		}
129de40ccefSDave Young 	} else
130de40ccefSDave Young 		pr_info("crashkernel size resulted in zero bytes\n");
131692f66f2SHari Bathini 
132692f66f2SHari Bathini 	return 0;
133692f66f2SHari Bathini }
134692f66f2SHari Bathini 
135692f66f2SHari Bathini /*
136692f66f2SHari Bathini  * That function parses "simple" (old) crashkernel command lines like
137692f66f2SHari Bathini  *
138692f66f2SHari Bathini  *	crashkernel=size[@offset]
139692f66f2SHari Bathini  *
140692f66f2SHari Bathini  * It returns 0 on success and -EINVAL on failure.
141692f66f2SHari Bathini  */
142692f66f2SHari Bathini static int __init parse_crashkernel_simple(char *cmdline,
143692f66f2SHari Bathini 					   unsigned long long *crash_size,
144692f66f2SHari Bathini 					   unsigned long long *crash_base)
145692f66f2SHari Bathini {
146692f66f2SHari Bathini 	char *cur = cmdline;
147692f66f2SHari Bathini 
148692f66f2SHari Bathini 	*crash_size = memparse(cmdline, &cur);
149692f66f2SHari Bathini 	if (cmdline == cur) {
150692f66f2SHari Bathini 		pr_warn("crashkernel: memory value expected\n");
151692f66f2SHari Bathini 		return -EINVAL;
152692f66f2SHari Bathini 	}
153692f66f2SHari Bathini 
154692f66f2SHari Bathini 	if (*cur == '@')
155692f66f2SHari Bathini 		*crash_base = memparse(cur+1, &cur);
156692f66f2SHari Bathini 	else if (*cur != ' ' && *cur != '\0') {
157692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
158692f66f2SHari Bathini 		return -EINVAL;
159692f66f2SHari Bathini 	}
160692f66f2SHari Bathini 
161692f66f2SHari Bathini 	return 0;
162692f66f2SHari Bathini }
163692f66f2SHari Bathini 
164692f66f2SHari Bathini #define SUFFIX_HIGH 0
165692f66f2SHari Bathini #define SUFFIX_LOW  1
166692f66f2SHari Bathini #define SUFFIX_NULL 2
167692f66f2SHari Bathini static __initdata char *suffix_tbl[] = {
168692f66f2SHari Bathini 	[SUFFIX_HIGH] = ",high",
169692f66f2SHari Bathini 	[SUFFIX_LOW]  = ",low",
170692f66f2SHari Bathini 	[SUFFIX_NULL] = NULL,
171692f66f2SHari Bathini };
172692f66f2SHari Bathini 
173692f66f2SHari Bathini /*
174692f66f2SHari Bathini  * That function parses "suffix"  crashkernel command lines like
175692f66f2SHari Bathini  *
176692f66f2SHari Bathini  *	crashkernel=size,[high|low]
177692f66f2SHari Bathini  *
178692f66f2SHari Bathini  * It returns 0 on success and -EINVAL on failure.
179692f66f2SHari Bathini  */
180692f66f2SHari Bathini static int __init parse_crashkernel_suffix(char *cmdline,
181692f66f2SHari Bathini 					   unsigned long long	*crash_size,
182692f66f2SHari Bathini 					   const char *suffix)
183692f66f2SHari Bathini {
184692f66f2SHari Bathini 	char *cur = cmdline;
185692f66f2SHari Bathini 
186692f66f2SHari Bathini 	*crash_size = memparse(cmdline, &cur);
187692f66f2SHari Bathini 	if (cmdline == cur) {
188692f66f2SHari Bathini 		pr_warn("crashkernel: memory value expected\n");
189692f66f2SHari Bathini 		return -EINVAL;
190692f66f2SHari Bathini 	}
191692f66f2SHari Bathini 
192692f66f2SHari Bathini 	/* check with suffix */
193692f66f2SHari Bathini 	if (strncmp(cur, suffix, strlen(suffix))) {
194692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
195692f66f2SHari Bathini 		return -EINVAL;
196692f66f2SHari Bathini 	}
197692f66f2SHari Bathini 	cur += strlen(suffix);
198692f66f2SHari Bathini 	if (*cur != ' ' && *cur != '\0') {
199692f66f2SHari Bathini 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
200692f66f2SHari Bathini 		return -EINVAL;
201692f66f2SHari Bathini 	}
202692f66f2SHari Bathini 
203692f66f2SHari Bathini 	return 0;
204692f66f2SHari Bathini }
205692f66f2SHari Bathini 
206692f66f2SHari Bathini static __init char *get_last_crashkernel(char *cmdline,
207692f66f2SHari Bathini 			     const char *name,
208692f66f2SHari Bathini 			     const char *suffix)
209692f66f2SHari Bathini {
210692f66f2SHari Bathini 	char *p = cmdline, *ck_cmdline = NULL;
211692f66f2SHari Bathini 
212692f66f2SHari Bathini 	/* find crashkernel and use the last one if there are more */
213692f66f2SHari Bathini 	p = strstr(p, name);
214692f66f2SHari Bathini 	while (p) {
215692f66f2SHari Bathini 		char *end_p = strchr(p, ' ');
216692f66f2SHari Bathini 		char *q;
217692f66f2SHari Bathini 
218692f66f2SHari Bathini 		if (!end_p)
219692f66f2SHari Bathini 			end_p = p + strlen(p);
220692f66f2SHari Bathini 
221692f66f2SHari Bathini 		if (!suffix) {
222692f66f2SHari Bathini 			int i;
223692f66f2SHari Bathini 
224692f66f2SHari Bathini 			/* skip the one with any known suffix */
225692f66f2SHari Bathini 			for (i = 0; suffix_tbl[i]; i++) {
226692f66f2SHari Bathini 				q = end_p - strlen(suffix_tbl[i]);
227692f66f2SHari Bathini 				if (!strncmp(q, suffix_tbl[i],
228692f66f2SHari Bathini 					     strlen(suffix_tbl[i])))
229692f66f2SHari Bathini 					goto next;
230692f66f2SHari Bathini 			}
231692f66f2SHari Bathini 			ck_cmdline = p;
232692f66f2SHari Bathini 		} else {
233692f66f2SHari Bathini 			q = end_p - strlen(suffix);
234692f66f2SHari Bathini 			if (!strncmp(q, suffix, strlen(suffix)))
235692f66f2SHari Bathini 				ck_cmdline = p;
236692f66f2SHari Bathini 		}
237692f66f2SHari Bathini next:
238692f66f2SHari Bathini 		p = strstr(p+1, name);
239692f66f2SHari Bathini 	}
240692f66f2SHari Bathini 
241692f66f2SHari Bathini 	return ck_cmdline;
242692f66f2SHari Bathini }
243692f66f2SHari Bathini 
244692f66f2SHari Bathini static int __init __parse_crashkernel(char *cmdline,
245692f66f2SHari Bathini 			     unsigned long long system_ram,
246692f66f2SHari Bathini 			     unsigned long long *crash_size,
247692f66f2SHari Bathini 			     unsigned long long *crash_base,
248692f66f2SHari Bathini 			     const char *name,
249692f66f2SHari Bathini 			     const char *suffix)
250692f66f2SHari Bathini {
251692f66f2SHari Bathini 	char	*first_colon, *first_space;
252692f66f2SHari Bathini 	char	*ck_cmdline;
253692f66f2SHari Bathini 
254692f66f2SHari Bathini 	BUG_ON(!crash_size || !crash_base);
255692f66f2SHari Bathini 	*crash_size = 0;
256692f66f2SHari Bathini 	*crash_base = 0;
257692f66f2SHari Bathini 
258692f66f2SHari Bathini 	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
259692f66f2SHari Bathini 	if (!ck_cmdline)
2602e5920bbSZhen Lei 		return -ENOENT;
261692f66f2SHari Bathini 
262692f66f2SHari Bathini 	ck_cmdline += strlen(name);
263692f66f2SHari Bathini 
264692f66f2SHari Bathini 	if (suffix)
265692f66f2SHari Bathini 		return parse_crashkernel_suffix(ck_cmdline, crash_size,
266692f66f2SHari Bathini 				suffix);
267692f66f2SHari Bathini 	/*
268692f66f2SHari Bathini 	 * if the commandline contains a ':', then that's the extended
269692f66f2SHari Bathini 	 * syntax -- if not, it must be the classic syntax
270692f66f2SHari Bathini 	 */
271692f66f2SHari Bathini 	first_colon = strchr(ck_cmdline, ':');
272692f66f2SHari Bathini 	first_space = strchr(ck_cmdline, ' ');
273692f66f2SHari Bathini 	if (first_colon && (!first_space || first_colon < first_space))
274692f66f2SHari Bathini 		return parse_crashkernel_mem(ck_cmdline, system_ram,
275692f66f2SHari Bathini 				crash_size, crash_base);
276692f66f2SHari Bathini 
277692f66f2SHari Bathini 	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
278692f66f2SHari Bathini }
279692f66f2SHari Bathini 
280692f66f2SHari Bathini /*
281692f66f2SHari Bathini  * That function is the entry point for command line parsing and should be
282692f66f2SHari Bathini  * called from the arch-specific code.
283692f66f2SHari Bathini  */
284692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline,
285692f66f2SHari Bathini 			     unsigned long long system_ram,
286692f66f2SHari Bathini 			     unsigned long long *crash_size,
287692f66f2SHari Bathini 			     unsigned long long *crash_base)
288692f66f2SHari Bathini {
289692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
290692f66f2SHari Bathini 					"crashkernel=", NULL);
291692f66f2SHari Bathini }
292692f66f2SHari Bathini 
293692f66f2SHari Bathini int __init parse_crashkernel_high(char *cmdline,
294692f66f2SHari Bathini 			     unsigned long long system_ram,
295692f66f2SHari Bathini 			     unsigned long long *crash_size,
296692f66f2SHari Bathini 			     unsigned long long *crash_base)
297692f66f2SHari Bathini {
298692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
299692f66f2SHari Bathini 				"crashkernel=", suffix_tbl[SUFFIX_HIGH]);
300692f66f2SHari Bathini }
301692f66f2SHari Bathini 
302692f66f2SHari Bathini int __init parse_crashkernel_low(char *cmdline,
303692f66f2SHari Bathini 			     unsigned long long system_ram,
304692f66f2SHari Bathini 			     unsigned long long *crash_size,
305692f66f2SHari Bathini 			     unsigned long long *crash_base)
306692f66f2SHari Bathini {
307692f66f2SHari Bathini 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
308692f66f2SHari Bathini 				"crashkernel=", suffix_tbl[SUFFIX_LOW]);
309692f66f2SHari Bathini }
310692f66f2SHari Bathini 
31171d2bcecSPhilipp Rudo /*
31271d2bcecSPhilipp Rudo  * Add a dummy early_param handler to mark crashkernel= as a known command line
31371d2bcecSPhilipp Rudo  * parameter and suppress incorrect warnings in init/main.c.
31471d2bcecSPhilipp Rudo  */
31571d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg)
31671d2bcecSPhilipp Rudo {
31771d2bcecSPhilipp Rudo 	return 0;
31871d2bcecSPhilipp Rudo }
31971d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy);
32071d2bcecSPhilipp Rudo 
321*6f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
322*6f991cc3SEric DeVolder 			  void **addr, unsigned long *sz)
323*6f991cc3SEric DeVolder {
324*6f991cc3SEric DeVolder 	Elf64_Ehdr *ehdr;
325*6f991cc3SEric DeVolder 	Elf64_Phdr *phdr;
326*6f991cc3SEric DeVolder 	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
327*6f991cc3SEric DeVolder 	unsigned char *buf;
328*6f991cc3SEric DeVolder 	unsigned int cpu, i;
329*6f991cc3SEric DeVolder 	unsigned long long notes_addr;
330*6f991cc3SEric DeVolder 	unsigned long mstart, mend;
331*6f991cc3SEric DeVolder 
332*6f991cc3SEric DeVolder 	/* extra phdr for vmcoreinfo ELF note */
333*6f991cc3SEric DeVolder 	nr_phdr = nr_cpus + 1;
334*6f991cc3SEric DeVolder 	nr_phdr += mem->nr_ranges;
335*6f991cc3SEric DeVolder 
336*6f991cc3SEric DeVolder 	/*
337*6f991cc3SEric DeVolder 	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
338*6f991cc3SEric DeVolder 	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
339*6f991cc3SEric DeVolder 	 * I think this is required by tools like gdb. So same physical
340*6f991cc3SEric DeVolder 	 * memory will be mapped in two ELF headers. One will contain kernel
341*6f991cc3SEric DeVolder 	 * text virtual addresses and other will have __va(physical) addresses.
342*6f991cc3SEric DeVolder 	 */
343*6f991cc3SEric DeVolder 
344*6f991cc3SEric DeVolder 	nr_phdr++;
345*6f991cc3SEric DeVolder 	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
346*6f991cc3SEric DeVolder 	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
347*6f991cc3SEric DeVolder 
348*6f991cc3SEric DeVolder 	buf = vzalloc(elf_sz);
349*6f991cc3SEric DeVolder 	if (!buf)
350*6f991cc3SEric DeVolder 		return -ENOMEM;
351*6f991cc3SEric DeVolder 
352*6f991cc3SEric DeVolder 	ehdr = (Elf64_Ehdr *)buf;
353*6f991cc3SEric DeVolder 	phdr = (Elf64_Phdr *)(ehdr + 1);
354*6f991cc3SEric DeVolder 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
355*6f991cc3SEric DeVolder 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
356*6f991cc3SEric DeVolder 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
357*6f991cc3SEric DeVolder 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
358*6f991cc3SEric DeVolder 	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
359*6f991cc3SEric DeVolder 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
360*6f991cc3SEric DeVolder 	ehdr->e_type = ET_CORE;
361*6f991cc3SEric DeVolder 	ehdr->e_machine = ELF_ARCH;
362*6f991cc3SEric DeVolder 	ehdr->e_version = EV_CURRENT;
363*6f991cc3SEric DeVolder 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
364*6f991cc3SEric DeVolder 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
365*6f991cc3SEric DeVolder 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
366*6f991cc3SEric DeVolder 
367*6f991cc3SEric DeVolder 	/* Prepare one phdr of type PT_NOTE for each present CPU */
368*6f991cc3SEric DeVolder 	for_each_present_cpu(cpu) {
369*6f991cc3SEric DeVolder 		phdr->p_type = PT_NOTE;
370*6f991cc3SEric DeVolder 		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
371*6f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = notes_addr;
372*6f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
373*6f991cc3SEric DeVolder 		(ehdr->e_phnum)++;
374*6f991cc3SEric DeVolder 		phdr++;
375*6f991cc3SEric DeVolder 	}
376*6f991cc3SEric DeVolder 
377*6f991cc3SEric DeVolder 	/* Prepare one PT_NOTE header for vmcoreinfo */
378*6f991cc3SEric DeVolder 	phdr->p_type = PT_NOTE;
379*6f991cc3SEric DeVolder 	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
380*6f991cc3SEric DeVolder 	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
381*6f991cc3SEric DeVolder 	(ehdr->e_phnum)++;
382*6f991cc3SEric DeVolder 	phdr++;
383*6f991cc3SEric DeVolder 
384*6f991cc3SEric DeVolder 	/* Prepare PT_LOAD type program header for kernel text region */
385*6f991cc3SEric DeVolder 	if (need_kernel_map) {
386*6f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
387*6f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
388*6f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) _text;
389*6f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = _end - _text;
390*6f991cc3SEric DeVolder 		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
391*6f991cc3SEric DeVolder 		ehdr->e_phnum++;
392*6f991cc3SEric DeVolder 		phdr++;
393*6f991cc3SEric DeVolder 	}
394*6f991cc3SEric DeVolder 
395*6f991cc3SEric DeVolder 	/* Go through all the ranges in mem->ranges[] and prepare phdr */
396*6f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
397*6f991cc3SEric DeVolder 		mstart = mem->ranges[i].start;
398*6f991cc3SEric DeVolder 		mend = mem->ranges[i].end;
399*6f991cc3SEric DeVolder 
400*6f991cc3SEric DeVolder 		phdr->p_type = PT_LOAD;
401*6f991cc3SEric DeVolder 		phdr->p_flags = PF_R|PF_W|PF_X;
402*6f991cc3SEric DeVolder 		phdr->p_offset  = mstart;
403*6f991cc3SEric DeVolder 
404*6f991cc3SEric DeVolder 		phdr->p_paddr = mstart;
405*6f991cc3SEric DeVolder 		phdr->p_vaddr = (unsigned long) __va(mstart);
406*6f991cc3SEric DeVolder 		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
407*6f991cc3SEric DeVolder 		phdr->p_align = 0;
408*6f991cc3SEric DeVolder 		ehdr->e_phnum++;
409*6f991cc3SEric 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",
410*6f991cc3SEric DeVolder 			phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
411*6f991cc3SEric DeVolder 			ehdr->e_phnum, phdr->p_offset);
412*6f991cc3SEric DeVolder 		phdr++;
413*6f991cc3SEric DeVolder 	}
414*6f991cc3SEric DeVolder 
415*6f991cc3SEric DeVolder 	*addr = buf;
416*6f991cc3SEric DeVolder 	*sz = elf_sz;
417*6f991cc3SEric DeVolder 	return 0;
418*6f991cc3SEric DeVolder }
419*6f991cc3SEric DeVolder 
420*6f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem,
421*6f991cc3SEric DeVolder 			    unsigned long long mstart, unsigned long long mend)
422*6f991cc3SEric DeVolder {
423*6f991cc3SEric DeVolder 	int i, j;
424*6f991cc3SEric DeVolder 	unsigned long long start, end, p_start, p_end;
425*6f991cc3SEric DeVolder 	struct range temp_range = {0, 0};
426*6f991cc3SEric DeVolder 
427*6f991cc3SEric DeVolder 	for (i = 0; i < mem->nr_ranges; i++) {
428*6f991cc3SEric DeVolder 		start = mem->ranges[i].start;
429*6f991cc3SEric DeVolder 		end = mem->ranges[i].end;
430*6f991cc3SEric DeVolder 		p_start = mstart;
431*6f991cc3SEric DeVolder 		p_end = mend;
432*6f991cc3SEric DeVolder 
433*6f991cc3SEric DeVolder 		if (mstart > end || mend < start)
434*6f991cc3SEric DeVolder 			continue;
435*6f991cc3SEric DeVolder 
436*6f991cc3SEric DeVolder 		/* Truncate any area outside of range */
437*6f991cc3SEric DeVolder 		if (mstart < start)
438*6f991cc3SEric DeVolder 			p_start = start;
439*6f991cc3SEric DeVolder 		if (mend > end)
440*6f991cc3SEric DeVolder 			p_end = end;
441*6f991cc3SEric DeVolder 
442*6f991cc3SEric DeVolder 		/* Found completely overlapping range */
443*6f991cc3SEric DeVolder 		if (p_start == start && p_end == end) {
444*6f991cc3SEric DeVolder 			mem->ranges[i].start = 0;
445*6f991cc3SEric DeVolder 			mem->ranges[i].end = 0;
446*6f991cc3SEric DeVolder 			if (i < mem->nr_ranges - 1) {
447*6f991cc3SEric DeVolder 				/* Shift rest of the ranges to left */
448*6f991cc3SEric DeVolder 				for (j = i; j < mem->nr_ranges - 1; j++) {
449*6f991cc3SEric DeVolder 					mem->ranges[j].start =
450*6f991cc3SEric DeVolder 						mem->ranges[j+1].start;
451*6f991cc3SEric DeVolder 					mem->ranges[j].end =
452*6f991cc3SEric DeVolder 							mem->ranges[j+1].end;
453*6f991cc3SEric DeVolder 				}
454*6f991cc3SEric DeVolder 
455*6f991cc3SEric DeVolder 				/*
456*6f991cc3SEric DeVolder 				 * Continue to check if there are another overlapping ranges
457*6f991cc3SEric DeVolder 				 * from the current position because of shifting the above
458*6f991cc3SEric DeVolder 				 * mem ranges.
459*6f991cc3SEric DeVolder 				 */
460*6f991cc3SEric DeVolder 				i--;
461*6f991cc3SEric DeVolder 				mem->nr_ranges--;
462*6f991cc3SEric DeVolder 				continue;
463*6f991cc3SEric DeVolder 			}
464*6f991cc3SEric DeVolder 			mem->nr_ranges--;
465*6f991cc3SEric DeVolder 			return 0;
466*6f991cc3SEric DeVolder 		}
467*6f991cc3SEric DeVolder 
468*6f991cc3SEric DeVolder 		if (p_start > start && p_end < end) {
469*6f991cc3SEric DeVolder 			/* Split original range */
470*6f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
471*6f991cc3SEric DeVolder 			temp_range.start = p_end + 1;
472*6f991cc3SEric DeVolder 			temp_range.end = end;
473*6f991cc3SEric DeVolder 		} else if (p_start != start)
474*6f991cc3SEric DeVolder 			mem->ranges[i].end = p_start - 1;
475*6f991cc3SEric DeVolder 		else
476*6f991cc3SEric DeVolder 			mem->ranges[i].start = p_end + 1;
477*6f991cc3SEric DeVolder 		break;
478*6f991cc3SEric DeVolder 	}
479*6f991cc3SEric DeVolder 
480*6f991cc3SEric DeVolder 	/* If a split happened, add the split to array */
481*6f991cc3SEric DeVolder 	if (!temp_range.end)
482*6f991cc3SEric DeVolder 		return 0;
483*6f991cc3SEric DeVolder 
484*6f991cc3SEric DeVolder 	/* Split happened */
485*6f991cc3SEric DeVolder 	if (i == mem->max_nr_ranges - 1)
486*6f991cc3SEric DeVolder 		return -ENOMEM;
487*6f991cc3SEric DeVolder 
488*6f991cc3SEric DeVolder 	/* Location where new range should go */
489*6f991cc3SEric DeVolder 	j = i + 1;
490*6f991cc3SEric DeVolder 	if (j < mem->nr_ranges) {
491*6f991cc3SEric DeVolder 		/* Move over all ranges one slot towards the end */
492*6f991cc3SEric DeVolder 		for (i = mem->nr_ranges - 1; i >= j; i--)
493*6f991cc3SEric DeVolder 			mem->ranges[i + 1] = mem->ranges[i];
494*6f991cc3SEric DeVolder 	}
495*6f991cc3SEric DeVolder 
496*6f991cc3SEric DeVolder 	mem->ranges[j].start = temp_range.start;
497*6f991cc3SEric DeVolder 	mem->ranges[j].end = temp_range.end;
498*6f991cc3SEric DeVolder 	mem->nr_ranges++;
499*6f991cc3SEric DeVolder 	return 0;
500*6f991cc3SEric DeVolder }
501*6f991cc3SEric DeVolder 
50251dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
503692f66f2SHari Bathini 			  void *data, size_t data_len)
504692f66f2SHari Bathini {
50551dbd925SHari Bathini 	struct elf_note *note = (struct elf_note *)buf;
506692f66f2SHari Bathini 
50751dbd925SHari Bathini 	note->n_namesz = strlen(name) + 1;
50851dbd925SHari Bathini 	note->n_descsz = data_len;
50951dbd925SHari Bathini 	note->n_type   = type;
51051dbd925SHari Bathini 	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
51151dbd925SHari Bathini 	memcpy(buf, name, note->n_namesz);
51251dbd925SHari Bathini 	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
51351dbd925SHari Bathini 	memcpy(buf, data, data_len);
51451dbd925SHari Bathini 	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
515692f66f2SHari Bathini 
516692f66f2SHari Bathini 	return buf;
517692f66f2SHari Bathini }
518692f66f2SHari Bathini 
51951dbd925SHari Bathini void final_note(Elf_Word *buf)
520692f66f2SHari Bathini {
52151dbd925SHari Bathini 	memset(buf, 0, sizeof(struct elf_note));
522692f66f2SHari Bathini }
523692f66f2SHari Bathini 
524692f66f2SHari Bathini static void update_vmcoreinfo_note(void)
525692f66f2SHari Bathini {
526692f66f2SHari Bathini 	u32 *buf = vmcoreinfo_note;
527692f66f2SHari Bathini 
528692f66f2SHari Bathini 	if (!vmcoreinfo_size)
529692f66f2SHari Bathini 		return;
530692f66f2SHari Bathini 	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
531692f66f2SHari Bathini 			      vmcoreinfo_size);
532692f66f2SHari Bathini 	final_note(buf);
533692f66f2SHari Bathini }
534692f66f2SHari Bathini 
5351229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr)
5361229384fSXunlei Pang {
5371229384fSXunlei Pang 	if (ptr)
5381229384fSXunlei Pang 		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
5391229384fSXunlei Pang 
5401229384fSXunlei Pang 	vmcoreinfo_data_safecopy = ptr;
5411229384fSXunlei Pang }
5421229384fSXunlei Pang 
543692f66f2SHari Bathini void crash_save_vmcoreinfo(void)
544692f66f2SHari Bathini {
545203e9e41SXunlei Pang 	if (!vmcoreinfo_note)
546203e9e41SXunlei Pang 		return;
547203e9e41SXunlei Pang 
5481229384fSXunlei Pang 	/* Use the safe copy to generate vmcoreinfo note if have */
5491229384fSXunlei Pang 	if (vmcoreinfo_data_safecopy)
5501229384fSXunlei Pang 		vmcoreinfo_data = vmcoreinfo_data_safecopy;
5511229384fSXunlei Pang 
55291bc9aafSArnd Bergmann 	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
553692f66f2SHari Bathini 	update_vmcoreinfo_note();
554692f66f2SHari Bathini }
555692f66f2SHari Bathini 
556692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...)
557692f66f2SHari Bathini {
558692f66f2SHari Bathini 	va_list args;
559692f66f2SHari Bathini 	char buf[0x50];
560692f66f2SHari Bathini 	size_t r;
561692f66f2SHari Bathini 
562692f66f2SHari Bathini 	va_start(args, fmt);
563692f66f2SHari Bathini 	r = vscnprintf(buf, sizeof(buf), fmt, args);
564692f66f2SHari Bathini 	va_end(args);
565692f66f2SHari Bathini 
5665203f499SXunlei Pang 	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
567692f66f2SHari Bathini 
568692f66f2SHari Bathini 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
569692f66f2SHari Bathini 
570692f66f2SHari Bathini 	vmcoreinfo_size += r;
57108fc35f3SStephen Brennan 
57208fc35f3SStephen Brennan 	WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES,
57308fc35f3SStephen Brennan 		  "vmcoreinfo data exceeds allocated size, truncating");
574692f66f2SHari Bathini }
575692f66f2SHari Bathini 
576692f66f2SHari Bathini /*
577692f66f2SHari Bathini  * provide an empty default implementation here -- architecture
578692f66f2SHari Bathini  * code may override this
579692f66f2SHari Bathini  */
580692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void)
581692f66f2SHari Bathini {}
582692f66f2SHari Bathini 
583692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void)
584692f66f2SHari Bathini {
585203e9e41SXunlei Pang 	return __pa(vmcoreinfo_note);
586692f66f2SHari Bathini }
58743d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note);
588692f66f2SHari Bathini 
589692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void)
590692f66f2SHari Bathini {
591203e9e41SXunlei Pang 	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
592203e9e41SXunlei Pang 	if (!vmcoreinfo_data) {
593203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
594203e9e41SXunlei Pang 		return -ENOMEM;
595203e9e41SXunlei Pang 	}
596203e9e41SXunlei Pang 
597203e9e41SXunlei Pang 	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
598203e9e41SXunlei Pang 						GFP_KERNEL | __GFP_ZERO);
599203e9e41SXunlei Pang 	if (!vmcoreinfo_note) {
600203e9e41SXunlei Pang 		free_page((unsigned long)vmcoreinfo_data);
601203e9e41SXunlei Pang 		vmcoreinfo_data = NULL;
602203e9e41SXunlei Pang 		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
603203e9e41SXunlei Pang 		return -ENOMEM;
604203e9e41SXunlei Pang 	}
605203e9e41SXunlei Pang 
606692f66f2SHari Bathini 	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
60744e8a5e9SStephen Boyd 	VMCOREINFO_BUILD_ID();
608692f66f2SHari Bathini 	VMCOREINFO_PAGESIZE(PAGE_SIZE);
609692f66f2SHari Bathini 
610692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(init_uts_ns);
611ca4a9241SAlexander Egorenkov 	VMCOREINFO_OFFSET(uts_namespace, name);
612692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(node_online_map);
613692f66f2SHari Bathini #ifdef CONFIG_MMU
614eff4345eSOmar Sandoval 	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
615692f66f2SHari Bathini #endif
616692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(_stext);
617692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(vmap_area_list);
618692f66f2SHari Bathini 
619a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA
620692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(mem_map);
621692f66f2SHari Bathini 	VMCOREINFO_SYMBOL(contig_page_data);
622692f66f2SHari Bathini #endif
623692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM
624a0b12803SKirill A. Shutemov 	VMCOREINFO_SYMBOL_ARRAY(mem_section);
625692f66f2SHari Bathini 	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
626692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(mem_section);
627692f66f2SHari Bathini 	VMCOREINFO_OFFSET(mem_section, section_mem_map);
6284f5aecdfSPingfan Liu 	VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
6291d50e5d0SBhupesh Sharma 	VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
630692f66f2SHari Bathini #endif
631692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(page);
632692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(pglist_data);
633692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(zone);
634692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(free_area);
635692f66f2SHari Bathini 	VMCOREINFO_STRUCT_SIZE(list_head);
636692f66f2SHari Bathini 	VMCOREINFO_SIZE(nodemask_t);
637692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, flags);
638692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _refcount);
639692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, mapping);
640692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, lru);
641692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, _mapcount);
642692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, private);
6431c5509beSMatthew Wilcox (Oracle) 	VMCOREINFO_OFFSET(folio, _folio_dtor);
6441c5509beSMatthew Wilcox (Oracle) 	VMCOREINFO_OFFSET(folio, _folio_order);
645692f66f2SHari Bathini 	VMCOREINFO_OFFSET(page, compound_head);
646692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_zones);
647692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, nr_zones);
64843b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM
649692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
650692f66f2SHari Bathini #endif
651692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
652692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
653692f66f2SHari Bathini 	VMCOREINFO_OFFSET(pglist_data, node_id);
654692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, free_area);
655692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, vm_stat);
656692f66f2SHari Bathini 	VMCOREINFO_OFFSET(zone, spanned_pages);
657692f66f2SHari Bathini 	VMCOREINFO_OFFSET(free_area, free_list);
658692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, next);
659692f66f2SHari Bathini 	VMCOREINFO_OFFSET(list_head, prev);
660692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, va_start);
661692f66f2SHari Bathini 	VMCOREINFO_OFFSET(vmap_area, list);
66223baf831SKirill A. Shutemov 	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER + 1);
663692f66f2SHari Bathini 	log_buf_vmcoreinfo_setup();
664692f66f2SHari Bathini 	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
665692f66f2SHari Bathini 	VMCOREINFO_NUMBER(NR_FREE_PAGES);
666692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_lru);
667692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_private);
668692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_swapcache);
6691cbf29daSPetr Tesarik 	VMCOREINFO_NUMBER(PG_swapbacked);
670692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_slab);
671692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE
672692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_hwpoison);
673692f66f2SHari Bathini #endif
674692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PG_head_mask);
6756e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE	(~PG_buddy)
676692f66f2SHari Bathini 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
677692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE
678692f66f2SHari Bathini 	VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
679e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE	(~PG_offline)
680e04b742fSDavid Hildenbrand 	VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
681692f66f2SHari Bathini #endif
682692f66f2SHari Bathini 
6835fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS
6845fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_names);
685f09bddbdSStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_num_syms);
6865fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_table);
6875fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_token_index);
6885fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE
6895fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_offsets);
6905fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_relative_base);
6915fd8fea9SStephen Brennan #else
6925fd8fea9SStephen Brennan 	VMCOREINFO_SYMBOL(kallsyms_addresses);
6935fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */
6945fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */
6955fd8fea9SStephen Brennan 
696692f66f2SHari Bathini 	arch_crash_save_vmcoreinfo();
697692f66f2SHari Bathini 	update_vmcoreinfo_note();
698692f66f2SHari Bathini 
699692f66f2SHari Bathini 	return 0;
700692f66f2SHari Bathini }
701692f66f2SHari Bathini 
702692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init);
703*6f991cc3SEric DeVolder 
704*6f991cc3SEric DeVolder static int __init crash_notes_memory_init(void)
705*6f991cc3SEric DeVolder {
706*6f991cc3SEric DeVolder 	/* Allocate memory for saving cpu registers. */
707*6f991cc3SEric DeVolder 	size_t size, align;
708*6f991cc3SEric DeVolder 
709*6f991cc3SEric DeVolder 	/*
710*6f991cc3SEric DeVolder 	 * crash_notes could be allocated across 2 vmalloc pages when percpu
711*6f991cc3SEric DeVolder 	 * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
712*6f991cc3SEric DeVolder 	 * pages are also on 2 continuous physical pages. In this case the
713*6f991cc3SEric DeVolder 	 * 2nd part of crash_notes in 2nd page could be lost since only the
714*6f991cc3SEric DeVolder 	 * starting address and size of crash_notes are exported through sysfs.
715*6f991cc3SEric DeVolder 	 * Here round up the size of crash_notes to the nearest power of two
716*6f991cc3SEric DeVolder 	 * and pass it to __alloc_percpu as align value. This can make sure
717*6f991cc3SEric DeVolder 	 * crash_notes is allocated inside one physical page.
718*6f991cc3SEric DeVolder 	 */
719*6f991cc3SEric DeVolder 	size = sizeof(note_buf_t);
720*6f991cc3SEric DeVolder 	align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
721*6f991cc3SEric DeVolder 
722*6f991cc3SEric DeVolder 	/*
723*6f991cc3SEric DeVolder 	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
724*6f991cc3SEric DeVolder 	 * definitely will be in 2 pages with that.
725*6f991cc3SEric DeVolder 	 */
726*6f991cc3SEric DeVolder 	BUILD_BUG_ON(size > PAGE_SIZE);
727*6f991cc3SEric DeVolder 
728*6f991cc3SEric DeVolder 	crash_notes = __alloc_percpu(size, align);
729*6f991cc3SEric DeVolder 	if (!crash_notes) {
730*6f991cc3SEric DeVolder 		pr_warn("Memory allocation for saving cpu register states failed\n");
731*6f991cc3SEric DeVolder 		return -ENOMEM;
732*6f991cc3SEric DeVolder 	}
733*6f991cc3SEric DeVolder 	return 0;
734*6f991cc3SEric DeVolder }
735*6f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init);
736