1 // SPDX-License-Identifier: GPL-2.0-only
3 * crash.c - kernel crash support code.
4 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
7 #include <linux/buildid.h>
8 #include <linux/crash_core.h>
9 #include <linux/init.h>
10 #include <linux/utsname.h>
11 #include <linux/vmalloc.h>
14 #include <asm/sections.h>
16 #include <crypto/sha1.h>
18 /* vmcoreinfo stuff */
19 unsigned char *vmcoreinfo_data;
20 size_t vmcoreinfo_size;
23 /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
24 static unsigned char *vmcoreinfo_data_safecopy;
27 * parsing the "crashkernel" commandline
29 * this code is intended to be called from architecture specific code
34 * This function parses command lines in the format
36 * crashkernel=ramsize-range:size[,...][@offset]
38 * The function returns 0 on success and -EINVAL on failure.
40 static int __init parse_crashkernel_mem(char *cmdline,
41 unsigned long long system_ram,
42 unsigned long long *crash_size,
43 unsigned long long *crash_base)
45 char *cur = cmdline, *tmp;
47 /* for each entry of the comma-separated list */
49 unsigned long long start, end = ULLONG_MAX, size;
51 /* get the start of the range */
52 start = memparse(cur, &tmp);
54 pr_warn("crashkernel: Memory value expected\n");
59 pr_warn("crashkernel: '-' expected\n");
64 /* if no ':' is here, than we read the end */
66 end = memparse(cur, &tmp);
68 pr_warn("crashkernel: Memory value expected\n");
73 pr_warn("crashkernel: end <= start\n");
79 pr_warn("crashkernel: ':' expected\n");
84 size = memparse(cur, &tmp);
86 pr_warn("Memory value expected\n");
90 if (size >= system_ram) {
91 pr_warn("crashkernel: invalid size\n");
96 if (system_ram >= start && system_ram < end) {
100 } while (*cur++ == ',');
102 if (*crash_size > 0) {
103 while (*cur && *cur != ' ' && *cur != '@')
107 *crash_base = memparse(cur, &tmp);
109 pr_warn("Memory value expected after '@'\n");
114 pr_info("crashkernel size resulted in zero bytes\n");
120 * That function parses "simple" (old) crashkernel command lines like
122 * crashkernel=size[@offset]
124 * It returns 0 on success and -EINVAL on failure.
126 static int __init parse_crashkernel_simple(char *cmdline,
127 unsigned long long *crash_size,
128 unsigned long long *crash_base)
132 *crash_size = memparse(cmdline, &cur);
133 if (cmdline == cur) {
134 pr_warn("crashkernel: memory value expected\n");
139 *crash_base = memparse(cur+1, &cur);
140 else if (*cur != ' ' && *cur != '\0') {
141 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
148 #define SUFFIX_HIGH 0
150 #define SUFFIX_NULL 2
151 static __initdata char *suffix_tbl[] = {
152 [SUFFIX_HIGH] = ",high",
153 [SUFFIX_LOW] = ",low",
154 [SUFFIX_NULL] = NULL,
158 * That function parses "suffix" crashkernel command lines like
160 * crashkernel=size,[high|low]
162 * It returns 0 on success and -EINVAL on failure.
164 static int __init parse_crashkernel_suffix(char *cmdline,
165 unsigned long long *crash_size,
170 *crash_size = memparse(cmdline, &cur);
171 if (cmdline == cur) {
172 pr_warn("crashkernel: memory value expected\n");
176 /* check with suffix */
177 if (strncmp(cur, suffix, strlen(suffix))) {
178 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
181 cur += strlen(suffix);
182 if (*cur != ' ' && *cur != '\0') {
183 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
190 static __init char *get_last_crashkernel(char *cmdline,
194 char *p = cmdline, *ck_cmdline = NULL;
196 /* find crashkernel and use the last one if there are more */
199 char *end_p = strchr(p, ' ');
203 end_p = p + strlen(p);
208 /* skip the one with any known suffix */
209 for (i = 0; suffix_tbl[i]; i++) {
210 q = end_p - strlen(suffix_tbl[i]);
211 if (!strncmp(q, suffix_tbl[i],
212 strlen(suffix_tbl[i])))
217 q = end_p - strlen(suffix);
218 if (!strncmp(q, suffix, strlen(suffix)))
222 p = strstr(p+1, name);
231 static int __init __parse_crashkernel(char *cmdline,
232 unsigned long long system_ram,
233 unsigned long long *crash_size,
234 unsigned long long *crash_base,
238 char *first_colon, *first_space;
241 BUG_ON(!crash_size || !crash_base);
245 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
249 ck_cmdline += strlen(name);
252 return parse_crashkernel_suffix(ck_cmdline, crash_size,
255 * if the commandline contains a ':', then that's the extended
256 * syntax -- if not, it must be the classic syntax
258 first_colon = strchr(ck_cmdline, ':');
259 first_space = strchr(ck_cmdline, ' ');
260 if (first_colon && (!first_space || first_colon < first_space))
261 return parse_crashkernel_mem(ck_cmdline, system_ram,
262 crash_size, crash_base);
264 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
268 * That function is the entry point for command line parsing and should be
269 * called from the arch-specific code.
271 int __init parse_crashkernel(char *cmdline,
272 unsigned long long system_ram,
273 unsigned long long *crash_size,
274 unsigned long long *crash_base)
276 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
277 "crashkernel=", NULL);
280 int __init parse_crashkernel_high(char *cmdline,
281 unsigned long long system_ram,
282 unsigned long long *crash_size,
283 unsigned long long *crash_base)
285 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
286 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
289 int __init parse_crashkernel_low(char *cmdline,
290 unsigned long long system_ram,
291 unsigned long long *crash_size,
292 unsigned long long *crash_base)
294 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
295 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
299 * Add a dummy early_param handler to mark crashkernel= as a known command line
300 * parameter and suppress incorrect warnings in init/main.c.
302 static int __init parse_crashkernel_dummy(char *arg)
306 early_param("crashkernel", parse_crashkernel_dummy);
308 Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
309 void *data, size_t data_len)
311 struct elf_note *note = (struct elf_note *)buf;
313 note->n_namesz = strlen(name) + 1;
314 note->n_descsz = data_len;
316 buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
317 memcpy(buf, name, note->n_namesz);
318 buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
319 memcpy(buf, data, data_len);
320 buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
325 void final_note(Elf_Word *buf)
327 memset(buf, 0, sizeof(struct elf_note));
330 static void update_vmcoreinfo_note(void)
332 u32 *buf = vmcoreinfo_note;
334 if (!vmcoreinfo_size)
336 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
341 void crash_update_vmcoreinfo_safecopy(void *ptr)
344 memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
346 vmcoreinfo_data_safecopy = ptr;
349 void crash_save_vmcoreinfo(void)
351 if (!vmcoreinfo_note)
354 /* Use the safe copy to generate vmcoreinfo note if have */
355 if (vmcoreinfo_data_safecopy)
356 vmcoreinfo_data = vmcoreinfo_data_safecopy;
358 vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
359 update_vmcoreinfo_note();
362 void vmcoreinfo_append_str(const char *fmt, ...)
369 r = vscnprintf(buf, sizeof(buf), fmt, args);
372 r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
374 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
376 vmcoreinfo_size += r;
380 * provide an empty default implementation here -- architecture
381 * code may override this
383 void __weak arch_crash_save_vmcoreinfo(void)
386 phys_addr_t __weak paddr_vmcoreinfo_note(void)
388 return __pa(vmcoreinfo_note);
390 EXPORT_SYMBOL(paddr_vmcoreinfo_note);
392 static int __init crash_save_vmcoreinfo_init(void)
394 vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
395 if (!vmcoreinfo_data) {
396 pr_warn("Memory allocation for vmcoreinfo_data failed\n");
400 vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
401 GFP_KERNEL | __GFP_ZERO);
402 if (!vmcoreinfo_note) {
403 free_page((unsigned long)vmcoreinfo_data);
404 vmcoreinfo_data = NULL;
405 pr_warn("Memory allocation for vmcoreinfo_note failed\n");
409 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
410 VMCOREINFO_BUILD_ID();
411 VMCOREINFO_PAGESIZE(PAGE_SIZE);
413 VMCOREINFO_SYMBOL(init_uts_ns);
414 VMCOREINFO_OFFSET(uts_namespace, name);
415 VMCOREINFO_SYMBOL(node_online_map);
417 VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
419 VMCOREINFO_SYMBOL(_stext);
420 VMCOREINFO_SYMBOL(vmap_area_list);
423 VMCOREINFO_SYMBOL(mem_map);
424 VMCOREINFO_SYMBOL(contig_page_data);
426 #ifdef CONFIG_SPARSEMEM
427 VMCOREINFO_SYMBOL_ARRAY(mem_section);
428 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
429 VMCOREINFO_STRUCT_SIZE(mem_section);
430 VMCOREINFO_OFFSET(mem_section, section_mem_map);
431 VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
432 VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
434 VMCOREINFO_STRUCT_SIZE(page);
435 VMCOREINFO_STRUCT_SIZE(pglist_data);
436 VMCOREINFO_STRUCT_SIZE(zone);
437 VMCOREINFO_STRUCT_SIZE(free_area);
438 VMCOREINFO_STRUCT_SIZE(list_head);
439 VMCOREINFO_SIZE(nodemask_t);
440 VMCOREINFO_OFFSET(page, flags);
441 VMCOREINFO_OFFSET(page, _refcount);
442 VMCOREINFO_OFFSET(page, mapping);
443 VMCOREINFO_OFFSET(page, lru);
444 VMCOREINFO_OFFSET(page, _mapcount);
445 VMCOREINFO_OFFSET(page, private);
446 VMCOREINFO_OFFSET(page, compound_dtor);
447 VMCOREINFO_OFFSET(page, compound_order);
448 VMCOREINFO_OFFSET(page, compound_head);
449 VMCOREINFO_OFFSET(pglist_data, node_zones);
450 VMCOREINFO_OFFSET(pglist_data, nr_zones);
451 #ifdef CONFIG_FLATMEM
452 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
454 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
455 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
456 VMCOREINFO_OFFSET(pglist_data, node_id);
457 VMCOREINFO_OFFSET(zone, free_area);
458 VMCOREINFO_OFFSET(zone, vm_stat);
459 VMCOREINFO_OFFSET(zone, spanned_pages);
460 VMCOREINFO_OFFSET(free_area, free_list);
461 VMCOREINFO_OFFSET(list_head, next);
462 VMCOREINFO_OFFSET(list_head, prev);
463 VMCOREINFO_OFFSET(vmap_area, va_start);
464 VMCOREINFO_OFFSET(vmap_area, list);
465 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
466 log_buf_vmcoreinfo_setup();
467 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
468 VMCOREINFO_NUMBER(NR_FREE_PAGES);
469 VMCOREINFO_NUMBER(PG_lru);
470 VMCOREINFO_NUMBER(PG_private);
471 VMCOREINFO_NUMBER(PG_swapcache);
472 VMCOREINFO_NUMBER(PG_swapbacked);
473 VMCOREINFO_NUMBER(PG_slab);
474 #ifdef CONFIG_MEMORY_FAILURE
475 VMCOREINFO_NUMBER(PG_hwpoison);
477 VMCOREINFO_NUMBER(PG_head_mask);
478 #define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy)
479 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
480 #ifdef CONFIG_HUGETLB_PAGE
481 VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
482 #define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline)
483 VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
486 arch_crash_save_vmcoreinfo();
487 update_vmcoreinfo_note();
492 subsys_initcall(crash_save_vmcoreinfo_init);