crash_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. static unsigned char *vmcoreinfo_data;
  15. static 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. }
  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=%ld\n", get_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. static int __init crash_save_vmcoreinfo_init(void)
  320. {
  321. vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  322. if (!vmcoreinfo_data) {
  323. pr_warn("Memory allocation for vmcoreinfo_data failed\n");
  324. return -ENOMEM;
  325. }
  326. vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
  327. GFP_KERNEL | __GFP_ZERO);
  328. if (!vmcoreinfo_note) {
  329. free_page((unsigned long)vmcoreinfo_data);
  330. vmcoreinfo_data = NULL;
  331. pr_warn("Memory allocation for vmcoreinfo_note failed\n");
  332. return -ENOMEM;
  333. }
  334. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  335. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  336. VMCOREINFO_SYMBOL(init_uts_ns);
  337. VMCOREINFO_SYMBOL(node_online_map);
  338. #ifdef CONFIG_MMU
  339. VMCOREINFO_SYMBOL(swapper_pg_dir);
  340. #endif
  341. VMCOREINFO_SYMBOL(_stext);
  342. VMCOREINFO_SYMBOL(vmap_area_list);
  343. #ifndef CONFIG_NEED_MULTIPLE_NODES
  344. VMCOREINFO_SYMBOL(mem_map);
  345. VMCOREINFO_SYMBOL(contig_page_data);
  346. #endif
  347. #ifdef CONFIG_SPARSEMEM
  348. VMCOREINFO_SYMBOL_ARRAY(mem_section);
  349. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  350. VMCOREINFO_STRUCT_SIZE(mem_section);
  351. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  352. #endif
  353. VMCOREINFO_STRUCT_SIZE(page);
  354. VMCOREINFO_STRUCT_SIZE(pglist_data);
  355. VMCOREINFO_STRUCT_SIZE(zone);
  356. VMCOREINFO_STRUCT_SIZE(free_area);
  357. VMCOREINFO_STRUCT_SIZE(list_head);
  358. VMCOREINFO_SIZE(nodemask_t);
  359. VMCOREINFO_OFFSET(page, flags);
  360. VMCOREINFO_OFFSET(page, _refcount);
  361. VMCOREINFO_OFFSET(page, mapping);
  362. VMCOREINFO_OFFSET(page, lru);
  363. VMCOREINFO_OFFSET(page, _mapcount);
  364. VMCOREINFO_OFFSET(page, private);
  365. VMCOREINFO_OFFSET(page, compound_dtor);
  366. VMCOREINFO_OFFSET(page, compound_order);
  367. VMCOREINFO_OFFSET(page, compound_head);
  368. VMCOREINFO_OFFSET(pglist_data, node_zones);
  369. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  370. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  371. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  372. #endif
  373. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  374. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  375. VMCOREINFO_OFFSET(pglist_data, node_id);
  376. VMCOREINFO_OFFSET(zone, free_area);
  377. VMCOREINFO_OFFSET(zone, vm_stat);
  378. VMCOREINFO_OFFSET(zone, spanned_pages);
  379. VMCOREINFO_OFFSET(free_area, free_list);
  380. VMCOREINFO_OFFSET(list_head, next);
  381. VMCOREINFO_OFFSET(list_head, prev);
  382. VMCOREINFO_OFFSET(vmap_area, va_start);
  383. VMCOREINFO_OFFSET(vmap_area, list);
  384. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  385. log_buf_vmcoreinfo_setup();
  386. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  387. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  388. VMCOREINFO_NUMBER(PG_lru);
  389. VMCOREINFO_NUMBER(PG_private);
  390. VMCOREINFO_NUMBER(PG_swapcache);
  391. VMCOREINFO_NUMBER(PG_slab);
  392. #ifdef CONFIG_MEMORY_FAILURE
  393. VMCOREINFO_NUMBER(PG_hwpoison);
  394. #endif
  395. VMCOREINFO_NUMBER(PG_head_mask);
  396. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  397. #ifdef CONFIG_HUGETLB_PAGE
  398. VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
  399. #endif
  400. arch_crash_save_vmcoreinfo();
  401. update_vmcoreinfo_note();
  402. return 0;
  403. }
  404. subsys_initcall(crash_save_vmcoreinfo_init);