crash_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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[VMCOREINFO_BYTES];
  15. u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
  16. size_t vmcoreinfo_size;
  17. size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
  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. }
  97. return 0;
  98. }
  99. /*
  100. * That function parses "simple" (old) crashkernel command lines like
  101. *
  102. * crashkernel=size[@offset]
  103. *
  104. * It returns 0 on success and -EINVAL on failure.
  105. */
  106. static int __init parse_crashkernel_simple(char *cmdline,
  107. unsigned long long *crash_size,
  108. unsigned long long *crash_base)
  109. {
  110. char *cur = cmdline;
  111. *crash_size = memparse(cmdline, &cur);
  112. if (cmdline == cur) {
  113. pr_warn("crashkernel: memory value expected\n");
  114. return -EINVAL;
  115. }
  116. if (*cur == '@')
  117. *crash_base = memparse(cur+1, &cur);
  118. else if (*cur != ' ' && *cur != '\0') {
  119. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  120. return -EINVAL;
  121. }
  122. return 0;
  123. }
  124. #define SUFFIX_HIGH 0
  125. #define SUFFIX_LOW 1
  126. #define SUFFIX_NULL 2
  127. static __initdata char *suffix_tbl[] = {
  128. [SUFFIX_HIGH] = ",high",
  129. [SUFFIX_LOW] = ",low",
  130. [SUFFIX_NULL] = NULL,
  131. };
  132. /*
  133. * That function parses "suffix" crashkernel command lines like
  134. *
  135. * crashkernel=size,[high|low]
  136. *
  137. * It returns 0 on success and -EINVAL on failure.
  138. */
  139. static int __init parse_crashkernel_suffix(char *cmdline,
  140. unsigned long long *crash_size,
  141. const char *suffix)
  142. {
  143. char *cur = cmdline;
  144. *crash_size = memparse(cmdline, &cur);
  145. if (cmdline == cur) {
  146. pr_warn("crashkernel: memory value expected\n");
  147. return -EINVAL;
  148. }
  149. /* check with suffix */
  150. if (strncmp(cur, suffix, strlen(suffix))) {
  151. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  152. return -EINVAL;
  153. }
  154. cur += strlen(suffix);
  155. if (*cur != ' ' && *cur != '\0') {
  156. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  157. return -EINVAL;
  158. }
  159. return 0;
  160. }
  161. static __init char *get_last_crashkernel(char *cmdline,
  162. const char *name,
  163. const char *suffix)
  164. {
  165. char *p = cmdline, *ck_cmdline = NULL;
  166. /* find crashkernel and use the last one if there are more */
  167. p = strstr(p, name);
  168. while (p) {
  169. char *end_p = strchr(p, ' ');
  170. char *q;
  171. if (!end_p)
  172. end_p = p + strlen(p);
  173. if (!suffix) {
  174. int i;
  175. /* skip the one with any known suffix */
  176. for (i = 0; suffix_tbl[i]; i++) {
  177. q = end_p - strlen(suffix_tbl[i]);
  178. if (!strncmp(q, suffix_tbl[i],
  179. strlen(suffix_tbl[i])))
  180. goto next;
  181. }
  182. ck_cmdline = p;
  183. } else {
  184. q = end_p - strlen(suffix);
  185. if (!strncmp(q, suffix, strlen(suffix)))
  186. ck_cmdline = p;
  187. }
  188. next:
  189. p = strstr(p+1, name);
  190. }
  191. if (!ck_cmdline)
  192. return NULL;
  193. return ck_cmdline;
  194. }
  195. static int __init __parse_crashkernel(char *cmdline,
  196. unsigned long long system_ram,
  197. unsigned long long *crash_size,
  198. unsigned long long *crash_base,
  199. const char *name,
  200. const char *suffix)
  201. {
  202. char *first_colon, *first_space;
  203. char *ck_cmdline;
  204. BUG_ON(!crash_size || !crash_base);
  205. *crash_size = 0;
  206. *crash_base = 0;
  207. ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
  208. if (!ck_cmdline)
  209. return -EINVAL;
  210. ck_cmdline += strlen(name);
  211. if (suffix)
  212. return parse_crashkernel_suffix(ck_cmdline, crash_size,
  213. suffix);
  214. /*
  215. * if the commandline contains a ':', then that's the extended
  216. * syntax -- if not, it must be the classic syntax
  217. */
  218. first_colon = strchr(ck_cmdline, ':');
  219. first_space = strchr(ck_cmdline, ' ');
  220. if (first_colon && (!first_space || first_colon < first_space))
  221. return parse_crashkernel_mem(ck_cmdline, system_ram,
  222. crash_size, crash_base);
  223. return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
  224. }
  225. /*
  226. * That function is the entry point for command line parsing and should be
  227. * called from the arch-specific code.
  228. */
  229. int __init parse_crashkernel(char *cmdline,
  230. unsigned long long system_ram,
  231. unsigned long long *crash_size,
  232. unsigned long long *crash_base)
  233. {
  234. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  235. "crashkernel=", NULL);
  236. }
  237. int __init parse_crashkernel_high(char *cmdline,
  238. unsigned long long system_ram,
  239. unsigned long long *crash_size,
  240. unsigned long long *crash_base)
  241. {
  242. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  243. "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
  244. }
  245. int __init parse_crashkernel_low(char *cmdline,
  246. unsigned long long system_ram,
  247. unsigned long long *crash_size,
  248. unsigned long long *crash_base)
  249. {
  250. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  251. "crashkernel=", suffix_tbl[SUFFIX_LOW]);
  252. }
  253. Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
  254. void *data, size_t data_len)
  255. {
  256. struct elf_note *note = (struct elf_note *)buf;
  257. note->n_namesz = strlen(name) + 1;
  258. note->n_descsz = data_len;
  259. note->n_type = type;
  260. buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
  261. memcpy(buf, name, note->n_namesz);
  262. buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
  263. memcpy(buf, data, data_len);
  264. buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
  265. return buf;
  266. }
  267. void final_note(Elf_Word *buf)
  268. {
  269. memset(buf, 0, sizeof(struct elf_note));
  270. }
  271. static void update_vmcoreinfo_note(void)
  272. {
  273. u32 *buf = vmcoreinfo_note;
  274. if (!vmcoreinfo_size)
  275. return;
  276. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  277. vmcoreinfo_size);
  278. final_note(buf);
  279. }
  280. void crash_save_vmcoreinfo(void)
  281. {
  282. vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
  283. update_vmcoreinfo_note();
  284. }
  285. void vmcoreinfo_append_str(const char *fmt, ...)
  286. {
  287. va_list args;
  288. char buf[0x50];
  289. size_t r;
  290. va_start(args, fmt);
  291. r = vscnprintf(buf, sizeof(buf), fmt, args);
  292. va_end(args);
  293. r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
  294. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  295. vmcoreinfo_size += r;
  296. }
  297. /*
  298. * provide an empty default implementation here -- architecture
  299. * code may override this
  300. */
  301. void __weak arch_crash_save_vmcoreinfo(void)
  302. {}
  303. phys_addr_t __weak paddr_vmcoreinfo_note(void)
  304. {
  305. return __pa_symbol((unsigned long)(char *)&vmcoreinfo_note);
  306. }
  307. static int __init crash_save_vmcoreinfo_init(void)
  308. {
  309. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  310. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  311. VMCOREINFO_SYMBOL(init_uts_ns);
  312. VMCOREINFO_SYMBOL(node_online_map);
  313. #ifdef CONFIG_MMU
  314. VMCOREINFO_SYMBOL(swapper_pg_dir);
  315. #endif
  316. VMCOREINFO_SYMBOL(_stext);
  317. VMCOREINFO_SYMBOL(vmap_area_list);
  318. #ifndef CONFIG_NEED_MULTIPLE_NODES
  319. VMCOREINFO_SYMBOL(mem_map);
  320. VMCOREINFO_SYMBOL(contig_page_data);
  321. #endif
  322. #ifdef CONFIG_SPARSEMEM
  323. VMCOREINFO_SYMBOL(mem_section);
  324. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  325. VMCOREINFO_STRUCT_SIZE(mem_section);
  326. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  327. #endif
  328. VMCOREINFO_STRUCT_SIZE(page);
  329. VMCOREINFO_STRUCT_SIZE(pglist_data);
  330. VMCOREINFO_STRUCT_SIZE(zone);
  331. VMCOREINFO_STRUCT_SIZE(free_area);
  332. VMCOREINFO_STRUCT_SIZE(list_head);
  333. VMCOREINFO_SIZE(nodemask_t);
  334. VMCOREINFO_OFFSET(page, flags);
  335. VMCOREINFO_OFFSET(page, _refcount);
  336. VMCOREINFO_OFFSET(page, mapping);
  337. VMCOREINFO_OFFSET(page, lru);
  338. VMCOREINFO_OFFSET(page, _mapcount);
  339. VMCOREINFO_OFFSET(page, private);
  340. VMCOREINFO_OFFSET(page, compound_dtor);
  341. VMCOREINFO_OFFSET(page, compound_order);
  342. VMCOREINFO_OFFSET(page, compound_head);
  343. VMCOREINFO_OFFSET(pglist_data, node_zones);
  344. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  345. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  346. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  347. #endif
  348. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  349. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  350. VMCOREINFO_OFFSET(pglist_data, node_id);
  351. VMCOREINFO_OFFSET(zone, free_area);
  352. VMCOREINFO_OFFSET(zone, vm_stat);
  353. VMCOREINFO_OFFSET(zone, spanned_pages);
  354. VMCOREINFO_OFFSET(free_area, free_list);
  355. VMCOREINFO_OFFSET(list_head, next);
  356. VMCOREINFO_OFFSET(list_head, prev);
  357. VMCOREINFO_OFFSET(vmap_area, va_start);
  358. VMCOREINFO_OFFSET(vmap_area, list);
  359. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  360. log_buf_vmcoreinfo_setup();
  361. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  362. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  363. VMCOREINFO_NUMBER(PG_lru);
  364. VMCOREINFO_NUMBER(PG_private);
  365. VMCOREINFO_NUMBER(PG_swapcache);
  366. VMCOREINFO_NUMBER(PG_slab);
  367. #ifdef CONFIG_MEMORY_FAILURE
  368. VMCOREINFO_NUMBER(PG_hwpoison);
  369. #endif
  370. VMCOREINFO_NUMBER(PG_head_mask);
  371. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  372. #ifdef CONFIG_HUGETLB_PAGE
  373. VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
  374. #endif
  375. arch_crash_save_vmcoreinfo();
  376. update_vmcoreinfo_note();
  377. return 0;
  378. }
  379. subsys_initcall(crash_save_vmcoreinfo_init);