crash_core.c 11 KB

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