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/crash_core.h>
8 #include <linux/utsname.h>
9 #include <linux/vmalloc.h>
12 #include <asm/sections.h>
14 #include <crypto/sha1.h>
16 /* vmcoreinfo stuff */
17 unsigned char *vmcoreinfo_data;
18 size_t vmcoreinfo_size;
21 /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
22 static unsigned char *vmcoreinfo_data_safecopy;
25 * parsing the "crashkernel" commandline
27 * this code is intended to be called from architecture specific code
32 * This function parses command lines in the format
34 * crashkernel=ramsize-range:size[,...][@offset]
36 * The function returns 0 on success and -EINVAL on failure.
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)
43 char *cur = cmdline, *tmp;
45 /* for each entry of the comma-separated list */
47 unsigned long long start, end = ULLONG_MAX, size;
49 /* get the start of the range */
50 start = memparse(cur, &tmp);
52 pr_warn("crashkernel: Memory value expected\n");
57 pr_warn("crashkernel: '-' expected\n");
62 /* if no ':' is here, than we read the end */
64 end = memparse(cur, &tmp);
66 pr_warn("crashkernel: Memory value expected\n");
71 pr_warn("crashkernel: end <= start\n");
77 pr_warn("crashkernel: ':' expected\n");
82 size = memparse(cur, &tmp);
84 pr_warn("Memory value expected\n");
88 if (size >= system_ram) {
89 pr_warn("crashkernel: invalid size\n");
94 if (system_ram >= start && system_ram < end) {
98 } while (*cur++ == ',');
100 if (*crash_size > 0) {
101 while (*cur && *cur != ' ' && *cur != '@')
105 *crash_base = memparse(cur, &tmp);
107 pr_warn("Memory value expected after '@'\n");
112 pr_info("crashkernel size resulted in zero bytes\n");
118 * That function parses "simple" (old) crashkernel command lines like
120 * crashkernel=size[@offset]
122 * It returns 0 on success and -EINVAL on failure.
124 static int __init parse_crashkernel_simple(char *cmdline,
125 unsigned long long *crash_size,
126 unsigned long long *crash_base)
130 *crash_size = memparse(cmdline, &cur);
131 if (cmdline == cur) {
132 pr_warn("crashkernel: memory value expected\n");
137 *crash_base = memparse(cur+1, &cur);
138 else if (*cur != ' ' && *cur != '\0') {
139 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
146 #define SUFFIX_HIGH 0
148 #define SUFFIX_NULL 2
149 static __initdata char *suffix_tbl[] = {
150 [SUFFIX_HIGH] = ",high",
151 [SUFFIX_LOW] = ",low",
152 [SUFFIX_NULL] = NULL,
156 * That function parses "suffix" crashkernel command lines like
158 * crashkernel=size,[high|low]
160 * It returns 0 on success and -EINVAL on failure.
162 static int __init parse_crashkernel_suffix(char *cmdline,
163 unsigned long long *crash_size,
168 *crash_size = memparse(cmdline, &cur);
169 if (cmdline == cur) {
170 pr_warn("crashkernel: memory value expected\n");
174 /* check with suffix */
175 if (strncmp(cur, suffix, strlen(suffix))) {
176 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
179 cur += strlen(suffix);
180 if (*cur != ' ' && *cur != '\0') {
181 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
188 static __init char *get_last_crashkernel(char *cmdline,
192 char *p = cmdline, *ck_cmdline = NULL;
194 /* find crashkernel and use the last one if there are more */
197 char *end_p = strchr(p, ' ');
201 end_p = p + strlen(p);
206 /* skip the one with any known suffix */
207 for (i = 0; suffix_tbl[i]; i++) {
208 q = end_p - strlen(suffix_tbl[i]);
209 if (!strncmp(q, suffix_tbl[i],
210 strlen(suffix_tbl[i])))
215 q = end_p - strlen(suffix);
216 if (!strncmp(q, suffix, strlen(suffix)))
220 p = strstr(p+1, name);
229 static int __init __parse_crashkernel(char *cmdline,
230 unsigned long long system_ram,
231 unsigned long long *crash_size,
232 unsigned long long *crash_base,
236 char *first_colon, *first_space;
239 BUG_ON(!crash_size || !crash_base);
243 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
248 ck_cmdline += strlen(name);
251 return parse_crashkernel_suffix(ck_cmdline, crash_size,
254 * if the commandline contains a ':', then that's the extended
255 * syntax -- if not, it must be the classic syntax
257 first_colon = strchr(ck_cmdline, ':');
258 first_space = strchr(ck_cmdline, ' ');
259 if (first_colon && (!first_space || first_colon < first_space))
260 return parse_crashkernel_mem(ck_cmdline, system_ram,
261 crash_size, crash_base);
263 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
267 * That function is the entry point for command line parsing and should be
268 * called from the arch-specific code.
270 int __init parse_crashkernel(char *cmdline,
271 unsigned long long system_ram,
272 unsigned long long *crash_size,
273 unsigned long long *crash_base)
275 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
276 "crashkernel=", NULL);
279 int __init parse_crashkernel_high(char *cmdline,
280 unsigned long long system_ram,
281 unsigned long long *crash_size,
282 unsigned long long *crash_base)
284 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
285 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
288 int __init parse_crashkernel_low(char *cmdline,
289 unsigned long long system_ram,
290 unsigned long long *crash_size,
291 unsigned long long *crash_base)
293 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
294 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
297 Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
298 void *data, size_t data_len)
300 struct elf_note *note = (struct elf_note *)buf;
302 note->n_namesz = strlen(name) + 1;
303 note->n_descsz = data_len;
305 buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
306 memcpy(buf, name, note->n_namesz);
307 buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
308 memcpy(buf, data, data_len);
309 buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
314 void final_note(Elf_Word *buf)
316 memset(buf, 0, sizeof(struct elf_note));
319 static void update_vmcoreinfo_note(void)
321 u32 *buf = vmcoreinfo_note;
323 if (!vmcoreinfo_size)
325 buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
330 void crash_update_vmcoreinfo_safecopy(void *ptr)
333 memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
335 vmcoreinfo_data_safecopy = ptr;
338 void crash_save_vmcoreinfo(void)
340 if (!vmcoreinfo_note)
343 /* Use the safe copy to generate vmcoreinfo note if have */
344 if (vmcoreinfo_data_safecopy)
345 vmcoreinfo_data = vmcoreinfo_data_safecopy;
347 vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
348 update_vmcoreinfo_note();
351 void vmcoreinfo_append_str(const char *fmt, ...)
358 r = vscnprintf(buf, sizeof(buf), fmt, args);
361 r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
363 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
365 vmcoreinfo_size += r;
369 * provide an empty default implementation here -- architecture
370 * code may override this
372 void __weak arch_crash_save_vmcoreinfo(void)
375 phys_addr_t __weak paddr_vmcoreinfo_note(void)
377 return __pa(vmcoreinfo_note);
379 EXPORT_SYMBOL(paddr_vmcoreinfo_note);
381 #define NOTES_SIZE (&__stop_notes - &__start_notes)
382 #define BUILD_ID_MAX SHA1_DIGEST_SIZE
383 #define NT_GNU_BUILD_ID 3
385 struct elf_note_section {
386 struct elf_note n_hdr;
391 * Add build ID from .notes section as generated by the GNU ld(1)
392 * or LLVM lld(1) --build-id option.
394 static void add_build_id_vmcoreinfo(void)
396 char build_id[BUILD_ID_MAX * 2 + 1];
397 int n_remain = NOTES_SIZE;
399 while (n_remain >= sizeof(struct elf_note)) {
400 const struct elf_note_section *note_sec =
401 &__start_notes + NOTES_SIZE - n_remain;
402 const u32 n_namesz = note_sec->n_hdr.n_namesz;
404 if (note_sec->n_hdr.n_type == NT_GNU_BUILD_ID &&
406 !strcmp((char *)¬e_sec->n_data[0], "GNU")) {
407 if (note_sec->n_hdr.n_descsz <= BUILD_ID_MAX) {
408 const u32 n_descsz = note_sec->n_hdr.n_descsz;
409 const u8 *s = ¬e_sec->n_data[n_namesz];
412 bin2hex(build_id, s, n_descsz);
413 build_id[2 * n_descsz] = '\0';
414 VMCOREINFO_BUILD_ID(build_id);
417 pr_warn("Build ID is too large to include in vmcoreinfo: %u > %u\n",
418 note_sec->n_hdr.n_descsz,
422 n_remain -= sizeof(struct elf_note) +
423 ALIGN(note_sec->n_hdr.n_namesz, 4) +
424 ALIGN(note_sec->n_hdr.n_descsz, 4);
428 static int __init crash_save_vmcoreinfo_init(void)
430 vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
431 if (!vmcoreinfo_data) {
432 pr_warn("Memory allocation for vmcoreinfo_data failed\n");
436 vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
437 GFP_KERNEL | __GFP_ZERO);
438 if (!vmcoreinfo_note) {
439 free_page((unsigned long)vmcoreinfo_data);
440 vmcoreinfo_data = NULL;
441 pr_warn("Memory allocation for vmcoreinfo_note failed\n");
445 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
446 add_build_id_vmcoreinfo();
447 VMCOREINFO_PAGESIZE(PAGE_SIZE);
449 VMCOREINFO_SYMBOL(init_uts_ns);
450 VMCOREINFO_OFFSET(uts_namespace, name);
451 VMCOREINFO_SYMBOL(node_online_map);
453 VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
455 VMCOREINFO_SYMBOL(_stext);
456 VMCOREINFO_SYMBOL(vmap_area_list);
459 VMCOREINFO_SYMBOL(mem_map);
460 VMCOREINFO_SYMBOL(contig_page_data);
462 #ifdef CONFIG_SPARSEMEM
463 VMCOREINFO_SYMBOL_ARRAY(mem_section);
464 VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
465 VMCOREINFO_STRUCT_SIZE(mem_section);
466 VMCOREINFO_OFFSET(mem_section, section_mem_map);
467 VMCOREINFO_NUMBER(SECTION_SIZE_BITS);
468 VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS);
470 VMCOREINFO_STRUCT_SIZE(page);
471 VMCOREINFO_STRUCT_SIZE(pglist_data);
472 VMCOREINFO_STRUCT_SIZE(zone);
473 VMCOREINFO_STRUCT_SIZE(free_area);
474 VMCOREINFO_STRUCT_SIZE(list_head);
475 VMCOREINFO_SIZE(nodemask_t);
476 VMCOREINFO_OFFSET(page, flags);
477 VMCOREINFO_OFFSET(page, _refcount);
478 VMCOREINFO_OFFSET(page, mapping);
479 VMCOREINFO_OFFSET(page, lru);
480 VMCOREINFO_OFFSET(page, _mapcount);
481 VMCOREINFO_OFFSET(page, private);
482 VMCOREINFO_OFFSET(page, compound_dtor);
483 VMCOREINFO_OFFSET(page, compound_order);
484 VMCOREINFO_OFFSET(page, compound_head);
485 VMCOREINFO_OFFSET(pglist_data, node_zones);
486 VMCOREINFO_OFFSET(pglist_data, nr_zones);
487 #ifdef CONFIG_FLATMEM
488 VMCOREINFO_OFFSET(pglist_data, node_mem_map);
490 VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
491 VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
492 VMCOREINFO_OFFSET(pglist_data, node_id);
493 VMCOREINFO_OFFSET(zone, free_area);
494 VMCOREINFO_OFFSET(zone, vm_stat);
495 VMCOREINFO_OFFSET(zone, spanned_pages);
496 VMCOREINFO_OFFSET(free_area, free_list);
497 VMCOREINFO_OFFSET(list_head, next);
498 VMCOREINFO_OFFSET(list_head, prev);
499 VMCOREINFO_OFFSET(vmap_area, va_start);
500 VMCOREINFO_OFFSET(vmap_area, list);
501 VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
502 log_buf_vmcoreinfo_setup();
503 VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
504 VMCOREINFO_NUMBER(NR_FREE_PAGES);
505 VMCOREINFO_NUMBER(PG_lru);
506 VMCOREINFO_NUMBER(PG_private);
507 VMCOREINFO_NUMBER(PG_swapcache);
508 VMCOREINFO_NUMBER(PG_swapbacked);
509 VMCOREINFO_NUMBER(PG_slab);
510 #ifdef CONFIG_MEMORY_FAILURE
511 VMCOREINFO_NUMBER(PG_hwpoison);
513 VMCOREINFO_NUMBER(PG_head_mask);
514 #define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy)
515 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
516 #ifdef CONFIG_HUGETLB_PAGE
517 VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
518 #define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline)
519 VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
522 arch_crash_save_vmcoreinfo();
523 update_vmcoreinfo_note();
528 subsys_initcall(crash_save_vmcoreinfo_init);