Merge branch 'i2c/for-current-fixed' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / kernel / crash_core.c
blobfcbd568f1e95069e6f42a46cad5f5c739f273def
1 /*
2 * crash.c - kernel crash support code.
3 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
7 */
9 #include <linux/crash_core.h>
10 #include <linux/utsname.h>
11 #include <linux/vmalloc.h>
13 #include <asm/page.h>
14 #include <asm/sections.h>
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);
23 * parsing the "crashkernel" commandline
25 * this code is intended to be called from architecture specific code
30 * This function parses command lines in the format
32 * crashkernel=ramsize-range:size[,...][@offset]
34 * The function returns 0 on success and -EINVAL on failure.
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)
41 char *cur = cmdline, *tmp;
43 /* for each entry of the comma-separated list */
44 do {
45 unsigned long long start, end = ULLONG_MAX, size;
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;
53 cur = tmp;
54 if (*cur != '-') {
55 pr_warn("crashkernel: '-' expected\n");
56 return -EINVAL;
58 cur++;
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;
67 cur = tmp;
68 if (end <= start) {
69 pr_warn("crashkernel: end <= start\n");
70 return -EINVAL;
74 if (*cur != ':') {
75 pr_warn("crashkernel: ':' expected\n");
76 return -EINVAL;
78 cur++;
80 size = memparse(cur, &tmp);
81 if (cur == tmp) {
82 pr_warn("Memory value expected\n");
83 return -EINVAL;
85 cur = tmp;
86 if (size >= system_ram) {
87 pr_warn("crashkernel: invalid size\n");
88 return -EINVAL;
91 /* match ? */
92 if (system_ram >= start && system_ram < end) {
93 *crash_size = size;
94 break;
96 } while (*cur++ == ',');
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;
111 return 0;
115 * That function parses "simple" (old) crashkernel command lines like
117 * crashkernel=size[@offset]
119 * It returns 0 on success and -EINVAL on failure.
121 static int __init parse_crashkernel_simple(char *cmdline,
122 unsigned long long *crash_size,
123 unsigned long long *crash_base)
125 char *cur = cmdline;
127 *crash_size = memparse(cmdline, &cur);
128 if (cmdline == cur) {
129 pr_warn("crashkernel: memory value expected\n");
130 return -EINVAL;
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;
140 return 0;
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,
153 * That function parses "suffix" crashkernel command lines like
155 * crashkernel=size,[high|low]
157 * It returns 0 on success and -EINVAL on failure.
159 static int __init parse_crashkernel_suffix(char *cmdline,
160 unsigned long long *crash_size,
161 const char *suffix)
163 char *cur = cmdline;
165 *crash_size = memparse(cmdline, &cur);
166 if (cmdline == cur) {
167 pr_warn("crashkernel: memory value expected\n");
168 return -EINVAL;
171 /* check with suffix */
172 if (strncmp(cur, suffix, strlen(suffix))) {
173 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
174 return -EINVAL;
176 cur += strlen(suffix);
177 if (*cur != ' ' && *cur != '\0') {
178 pr_warn("crashkernel: unrecognized char: %c\n", *cur);
179 return -EINVAL;
182 return 0;
185 static __init char *get_last_crashkernel(char *cmdline,
186 const char *name,
187 const char *suffix)
189 char *p = cmdline, *ck_cmdline = NULL;
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;
197 if (!end_p)
198 end_p = p + strlen(p);
200 if (!suffix) {
201 int i;
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;
210 ck_cmdline = p;
211 } else {
212 q = end_p - strlen(suffix);
213 if (!strncmp(q, suffix, strlen(suffix)))
214 ck_cmdline = p;
216 next:
217 p = strstr(p+1, name);
220 if (!ck_cmdline)
221 return NULL;
223 return ck_cmdline;
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)
233 char *first_colon, *first_space;
234 char *ck_cmdline;
236 BUG_ON(!crash_size || !crash_base);
237 *crash_size = 0;
238 *crash_base = 0;
240 ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
242 if (!ck_cmdline)
243 return -EINVAL;
245 ck_cmdline += strlen(name);
247 if (suffix)
248 return parse_crashkernel_suffix(ck_cmdline, crash_size,
249 suffix);
251 * if the commandline contains a ':', then that's the extended
252 * syntax -- if not, it must be the classic syntax
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);
260 return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
264 * That function is the entry point for command line parsing and should be
265 * called from the arch-specific code.
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)
272 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
273 "crashkernel=", NULL);
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)
281 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
282 "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
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)
290 return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
291 "crashkernel=", suffix_tbl[SUFFIX_LOW]);
294 Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
295 void *data, size_t data_len)
297 struct elf_note *note = (struct elf_note *)buf;
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));
308 return buf;
311 void final_note(Elf_Word *buf)
313 memset(buf, 0, sizeof(struct elf_note));
316 static void update_vmcoreinfo_note(void)
318 u32 *buf = vmcoreinfo_note;
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);
327 void crash_save_vmcoreinfo(void)
329 vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
330 update_vmcoreinfo_note();
333 void vmcoreinfo_append_str(const char *fmt, ...)
335 va_list args;
336 char buf[0x50];
337 size_t r;
339 va_start(args, fmt);
340 r = vscnprintf(buf, sizeof(buf), fmt, args);
341 va_end(args);
343 r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
345 memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
347 vmcoreinfo_size += r;
351 * provide an empty default implementation here -- architecture
352 * code may override this
354 void __weak arch_crash_save_vmcoreinfo(void)
357 phys_addr_t __weak paddr_vmcoreinfo_note(void)
359 return __pa_symbol((unsigned long)(char *)&vmcoreinfo_note);
362 static int __init crash_save_vmcoreinfo_init(void)
364 VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
365 VMCOREINFO_PAGESIZE(PAGE_SIZE);
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);
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
433 arch_crash_save_vmcoreinfo();
434 update_vmcoreinfo_note();
436 return 0;
439 subsys_initcall(crash_save_vmcoreinfo_init);