crash_core.c 13 KB

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