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