xref: /openbmc/linux/kernel/crash_core.c (revision dea54fba)
1 /*
2  * crash.c - kernel crash support code.
3  * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com>
4  *
5  * This source code is licensed under the GNU General Public License,
6  * Version 2.  See the file COPYING for more details.
7  */
8 
9 #include <linux/crash_core.h>
10 #include <linux/utsname.h>
11 #include <linux/vmalloc.h>
12 
13 #include <asm/page.h>
14 #include <asm/sections.h>
15 
16 /* vmcoreinfo stuff */
17 static unsigned char *vmcoreinfo_data;
18 static size_t vmcoreinfo_size;
19 u32 *vmcoreinfo_note;
20 
21 /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
22 static unsigned char *vmcoreinfo_data_safecopy;
23 
24 /*
25  * parsing the "crashkernel" commandline
26  *
27  * this code is intended to be called from architecture specific code
28  */
29 
30 
31 /*
32  * This function parses command lines in the format
33  *
34  *   crashkernel=ramsize-range:size[,...][@offset]
35  *
36  * The function returns 0 on success and -EINVAL on failure.
37  */
38 static int __init parse_crashkernel_mem(char *cmdline,
39 					unsigned long long system_ram,
40 					unsigned long long *crash_size,
41 					unsigned long long *crash_base)
42 {
43 	char *cur = cmdline, *tmp;
44 
45 	/* for each entry of the comma-separated list */
46 	do {
47 		unsigned long long start, end = ULLONG_MAX, size;
48 
49 		/* get the start of the range */
50 		start = memparse(cur, &tmp);
51 		if (cur == tmp) {
52 			pr_warn("crashkernel: Memory value expected\n");
53 			return -EINVAL;
54 		}
55 		cur = tmp;
56 		if (*cur != '-') {
57 			pr_warn("crashkernel: '-' expected\n");
58 			return -EINVAL;
59 		}
60 		cur++;
61 
62 		/* if no ':' is here, than we read the end */
63 		if (*cur != ':') {
64 			end = memparse(cur, &tmp);
65 			if (cur == tmp) {
66 				pr_warn("crashkernel: Memory value expected\n");
67 				return -EINVAL;
68 			}
69 			cur = tmp;
70 			if (end <= start) {
71 				pr_warn("crashkernel: end <= start\n");
72 				return -EINVAL;
73 			}
74 		}
75 
76 		if (*cur != ':') {
77 			pr_warn("crashkernel: ':' expected\n");
78 			return -EINVAL;
79 		}
80 		cur++;
81 
82 		size = memparse(cur, &tmp);
83 		if (cur == tmp) {
84 			pr_warn("Memory value expected\n");
85 			return -EINVAL;
86 		}
87 		cur = tmp;
88 		if (size >= system_ram) {
89 			pr_warn("crashkernel: invalid size\n");
90 			return -EINVAL;
91 		}
92 
93 		/* match ? */
94 		if (system_ram >= start && system_ram < end) {
95 			*crash_size = size;
96 			break;
97 		}
98 	} while (*cur++ == ',');
99 
100 	if (*crash_size > 0) {
101 		while (*cur && *cur != ' ' && *cur != '@')
102 			cur++;
103 		if (*cur == '@') {
104 			cur++;
105 			*crash_base = memparse(cur, &tmp);
106 			if (cur == tmp) {
107 				pr_warn("Memory value expected after '@'\n");
108 				return -EINVAL;
109 			}
110 		}
111 	}
112 
113 	return 0;
114 }
115 
116 /*
117  * That function parses "simple" (old) crashkernel command lines like
118  *
119  *	crashkernel=size[@offset]
120  *
121  * It returns 0 on success and -EINVAL on failure.
122  */
123 static int __init parse_crashkernel_simple(char *cmdline,
124 					   unsigned long long *crash_size,
125 					   unsigned long long *crash_base)
126 {
127 	char *cur = cmdline;
128 
129 	*crash_size = memparse(cmdline, &cur);
130 	if (cmdline == cur) {
131 		pr_warn("crashkernel: memory value expected\n");
132 		return -EINVAL;
133 	}
134 
135 	if (*cur == '@')
136 		*crash_base = memparse(cur+1, &cur);
137 	else if (*cur != ' ' && *cur != '\0') {
138 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
139 		return -EINVAL;
140 	}
141 
142 	return 0;
143 }
144 
145 #define SUFFIX_HIGH 0
146 #define SUFFIX_LOW  1
147 #define SUFFIX_NULL 2
148 static __initdata char *suffix_tbl[] = {
149 	[SUFFIX_HIGH] = ",high",
150 	[SUFFIX_LOW]  = ",low",
151 	[SUFFIX_NULL] = NULL,
152 };
153 
154 /*
155  * That function parses "suffix"  crashkernel command lines like
156  *
157  *	crashkernel=size,[high|low]
158  *
159  * It returns 0 on success and -EINVAL on failure.
160  */
161 static int __init parse_crashkernel_suffix(char *cmdline,
162 					   unsigned long long	*crash_size,
163 					   const char *suffix)
164 {
165 	char *cur = cmdline;
166 
167 	*crash_size = memparse(cmdline, &cur);
168 	if (cmdline == cur) {
169 		pr_warn("crashkernel: memory value expected\n");
170 		return -EINVAL;
171 	}
172 
173 	/* check with suffix */
174 	if (strncmp(cur, suffix, strlen(suffix))) {
175 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
176 		return -EINVAL;
177 	}
178 	cur += strlen(suffix);
179 	if (*cur != ' ' && *cur != '\0') {
180 		pr_warn("crashkernel: unrecognized char: %c\n", *cur);
181 		return -EINVAL;
182 	}
183 
184 	return 0;
185 }
186 
187 static __init char *get_last_crashkernel(char *cmdline,
188 			     const char *name,
189 			     const char *suffix)
190 {
191 	char *p = cmdline, *ck_cmdline = NULL;
192 
193 	/* find crashkernel and use the last one if there are more */
194 	p = strstr(p, name);
195 	while (p) {
196 		char *end_p = strchr(p, ' ');
197 		char *q;
198 
199 		if (!end_p)
200 			end_p = p + strlen(p);
201 
202 		if (!suffix) {
203 			int i;
204 
205 			/* skip the one with any known suffix */
206 			for (i = 0; suffix_tbl[i]; i++) {
207 				q = end_p - strlen(suffix_tbl[i]);
208 				if (!strncmp(q, suffix_tbl[i],
209 					     strlen(suffix_tbl[i])))
210 					goto next;
211 			}
212 			ck_cmdline = p;
213 		} else {
214 			q = end_p - strlen(suffix);
215 			if (!strncmp(q, suffix, strlen(suffix)))
216 				ck_cmdline = p;
217 		}
218 next:
219 		p = strstr(p+1, name);
220 	}
221 
222 	if (!ck_cmdline)
223 		return NULL;
224 
225 	return ck_cmdline;
226 }
227 
228 static int __init __parse_crashkernel(char *cmdline,
229 			     unsigned long long system_ram,
230 			     unsigned long long *crash_size,
231 			     unsigned long long *crash_base,
232 			     const char *name,
233 			     const char *suffix)
234 {
235 	char	*first_colon, *first_space;
236 	char	*ck_cmdline;
237 
238 	BUG_ON(!crash_size || !crash_base);
239 	*crash_size = 0;
240 	*crash_base = 0;
241 
242 	ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
243 
244 	if (!ck_cmdline)
245 		return -EINVAL;
246 
247 	ck_cmdline += strlen(name);
248 
249 	if (suffix)
250 		return parse_crashkernel_suffix(ck_cmdline, crash_size,
251 				suffix);
252 	/*
253 	 * if the commandline contains a ':', then that's the extended
254 	 * syntax -- if not, it must be the classic syntax
255 	 */
256 	first_colon = strchr(ck_cmdline, ':');
257 	first_space = strchr(ck_cmdline, ' ');
258 	if (first_colon && (!first_space || first_colon < first_space))
259 		return parse_crashkernel_mem(ck_cmdline, system_ram,
260 				crash_size, crash_base);
261 
262 	return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
263 }
264 
265 /*
266  * That function is the entry point for command line parsing and should be
267  * called from the arch-specific code.
268  */
269 int __init parse_crashkernel(char *cmdline,
270 			     unsigned long long system_ram,
271 			     unsigned long long *crash_size,
272 			     unsigned long long *crash_base)
273 {
274 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
275 					"crashkernel=", NULL);
276 }
277 
278 int __init parse_crashkernel_high(char *cmdline,
279 			     unsigned long long system_ram,
280 			     unsigned long long *crash_size,
281 			     unsigned long long *crash_base)
282 {
283 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
284 				"crashkernel=", suffix_tbl[SUFFIX_HIGH]);
285 }
286 
287 int __init parse_crashkernel_low(char *cmdline,
288 			     unsigned long long system_ram,
289 			     unsigned long long *crash_size,
290 			     unsigned long long *crash_base)
291 {
292 	return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
293 				"crashkernel=", suffix_tbl[SUFFIX_LOW]);
294 }
295 
296 Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
297 			  void *data, size_t data_len)
298 {
299 	struct elf_note *note = (struct elf_note *)buf;
300 
301 	note->n_namesz = strlen(name) + 1;
302 	note->n_descsz = data_len;
303 	note->n_type   = type;
304 	buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
305 	memcpy(buf, name, note->n_namesz);
306 	buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
307 	memcpy(buf, data, data_len);
308 	buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
309 
310 	return buf;
311 }
312 
313 void final_note(Elf_Word *buf)
314 {
315 	memset(buf, 0, sizeof(struct elf_note));
316 }
317 
318 static void update_vmcoreinfo_note(void)
319 {
320 	u32 *buf = vmcoreinfo_note;
321 
322 	if (!vmcoreinfo_size)
323 		return;
324 	buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
325 			      vmcoreinfo_size);
326 	final_note(buf);
327 }
328 
329 void crash_update_vmcoreinfo_safecopy(void *ptr)
330 {
331 	if (ptr)
332 		memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
333 
334 	vmcoreinfo_data_safecopy = ptr;
335 }
336 
337 void crash_save_vmcoreinfo(void)
338 {
339 	if (!vmcoreinfo_note)
340 		return;
341 
342 	/* Use the safe copy to generate vmcoreinfo note if have */
343 	if (vmcoreinfo_data_safecopy)
344 		vmcoreinfo_data = vmcoreinfo_data_safecopy;
345 
346 	vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
347 	update_vmcoreinfo_note();
348 }
349 
350 void vmcoreinfo_append_str(const char *fmt, ...)
351 {
352 	va_list args;
353 	char buf[0x50];
354 	size_t r;
355 
356 	va_start(args, fmt);
357 	r = vscnprintf(buf, sizeof(buf), fmt, args);
358 	va_end(args);
359 
360 	r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
361 
362 	memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
363 
364 	vmcoreinfo_size += r;
365 }
366 
367 /*
368  * provide an empty default implementation here -- architecture
369  * code may override this
370  */
371 void __weak arch_crash_save_vmcoreinfo(void)
372 {}
373 
374 phys_addr_t __weak paddr_vmcoreinfo_note(void)
375 {
376 	return __pa(vmcoreinfo_note);
377 }
378 
379 static int __init crash_save_vmcoreinfo_init(void)
380 {
381 	vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
382 	if (!vmcoreinfo_data) {
383 		pr_warn("Memory allocation for vmcoreinfo_data failed\n");
384 		return -ENOMEM;
385 	}
386 
387 	vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
388 						GFP_KERNEL | __GFP_ZERO);
389 	if (!vmcoreinfo_note) {
390 		free_page((unsigned long)vmcoreinfo_data);
391 		vmcoreinfo_data = NULL;
392 		pr_warn("Memory allocation for vmcoreinfo_note failed\n");
393 		return -ENOMEM;
394 	}
395 
396 	VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
397 	VMCOREINFO_PAGESIZE(PAGE_SIZE);
398 
399 	VMCOREINFO_SYMBOL(init_uts_ns);
400 	VMCOREINFO_SYMBOL(node_online_map);
401 #ifdef CONFIG_MMU
402 	VMCOREINFO_SYMBOL(swapper_pg_dir);
403 #endif
404 	VMCOREINFO_SYMBOL(_stext);
405 	VMCOREINFO_SYMBOL(vmap_area_list);
406 
407 #ifndef CONFIG_NEED_MULTIPLE_NODES
408 	VMCOREINFO_SYMBOL(mem_map);
409 	VMCOREINFO_SYMBOL(contig_page_data);
410 #endif
411 #ifdef CONFIG_SPARSEMEM
412 	VMCOREINFO_SYMBOL(mem_section);
413 	VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
414 	VMCOREINFO_STRUCT_SIZE(mem_section);
415 	VMCOREINFO_OFFSET(mem_section, section_mem_map);
416 #endif
417 	VMCOREINFO_STRUCT_SIZE(page);
418 	VMCOREINFO_STRUCT_SIZE(pglist_data);
419 	VMCOREINFO_STRUCT_SIZE(zone);
420 	VMCOREINFO_STRUCT_SIZE(free_area);
421 	VMCOREINFO_STRUCT_SIZE(list_head);
422 	VMCOREINFO_SIZE(nodemask_t);
423 	VMCOREINFO_OFFSET(page, flags);
424 	VMCOREINFO_OFFSET(page, _refcount);
425 	VMCOREINFO_OFFSET(page, mapping);
426 	VMCOREINFO_OFFSET(page, lru);
427 	VMCOREINFO_OFFSET(page, _mapcount);
428 	VMCOREINFO_OFFSET(page, private);
429 	VMCOREINFO_OFFSET(page, compound_dtor);
430 	VMCOREINFO_OFFSET(page, compound_order);
431 	VMCOREINFO_OFFSET(page, compound_head);
432 	VMCOREINFO_OFFSET(pglist_data, node_zones);
433 	VMCOREINFO_OFFSET(pglist_data, nr_zones);
434 #ifdef CONFIG_FLAT_NODE_MEM_MAP
435 	VMCOREINFO_OFFSET(pglist_data, node_mem_map);
436 #endif
437 	VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
438 	VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
439 	VMCOREINFO_OFFSET(pglist_data, node_id);
440 	VMCOREINFO_OFFSET(zone, free_area);
441 	VMCOREINFO_OFFSET(zone, vm_stat);
442 	VMCOREINFO_OFFSET(zone, spanned_pages);
443 	VMCOREINFO_OFFSET(free_area, free_list);
444 	VMCOREINFO_OFFSET(list_head, next);
445 	VMCOREINFO_OFFSET(list_head, prev);
446 	VMCOREINFO_OFFSET(vmap_area, va_start);
447 	VMCOREINFO_OFFSET(vmap_area, list);
448 	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
449 	log_buf_vmcoreinfo_setup();
450 	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
451 	VMCOREINFO_NUMBER(NR_FREE_PAGES);
452 	VMCOREINFO_NUMBER(PG_lru);
453 	VMCOREINFO_NUMBER(PG_private);
454 	VMCOREINFO_NUMBER(PG_swapcache);
455 	VMCOREINFO_NUMBER(PG_slab);
456 #ifdef CONFIG_MEMORY_FAILURE
457 	VMCOREINFO_NUMBER(PG_hwpoison);
458 #endif
459 	VMCOREINFO_NUMBER(PG_head_mask);
460 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
461 #ifdef CONFIG_HUGETLB_PAGE
462 	VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
463 #endif
464 
465 	arch_crash_save_vmcoreinfo();
466 	update_vmcoreinfo_note();
467 
468 	return 0;
469 }
470 
471 subsys_initcall(crash_save_vmcoreinfo_init);
472