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.
9 #include <linux/crash_core.h>
10 #include <linux/utsname.h>
11 #include <linux/vmalloc.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 */
45 unsigned long long start
, end
= ULLONG_MAX
, size
;
47 /* get the start of the range */
48 start
= memparse(cur
, &tmp
);
50 pr_warn("crashkernel: Memory value expected\n");
55 pr_warn("crashkernel: '-' expected\n");
60 /* if no ':' is here, than we read the end */
62 end
= memparse(cur
, &tmp
);
64 pr_warn("crashkernel: Memory value expected\n");
69 pr_warn("crashkernel: end <= start\n");
75 pr_warn("crashkernel: ':' expected\n");
80 size
= memparse(cur
, &tmp
);
82 pr_warn("Memory value expected\n");
86 if (size
>= system_ram
) {
87 pr_warn("crashkernel: invalid size\n");
92 if (system_ram
>= start
&& system_ram
< end
) {
96 } while (*cur
++ == ',');
98 if (*crash_size
> 0) {
99 while (*cur
&& *cur
!= ' ' && *cur
!= '@')
103 *crash_base
= memparse(cur
, &tmp
);
105 pr_warn("Memory value expected after '@'\n");
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
)
127 *crash_size
= memparse(cmdline
, &cur
);
128 if (cmdline
== cur
) {
129 pr_warn("crashkernel: memory value expected\n");
134 *crash_base
= memparse(cur
+1, &cur
);
135 else if (*cur
!= ' ' && *cur
!= '\0') {
136 pr_warn("crashkernel: unrecognized char: %c\n", *cur
);
143 #define SUFFIX_HIGH 0
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
,
165 *crash_size
= memparse(cmdline
, &cur
);
166 if (cmdline
== cur
) {
167 pr_warn("crashkernel: memory value expected\n");
171 /* check with suffix */
172 if (strncmp(cur
, suffix
, strlen(suffix
))) {
173 pr_warn("crashkernel: unrecognized char: %c\n", *cur
);
176 cur
+= strlen(suffix
);
177 if (*cur
!= ' ' && *cur
!= '\0') {
178 pr_warn("crashkernel: unrecognized char: %c\n", *cur
);
185 static __init
char *get_last_crashkernel(char *cmdline
,
189 char *p
= cmdline
, *ck_cmdline
= NULL
;
191 /* find crashkernel and use the last one if there are more */
194 char *end_p
= strchr(p
, ' ');
198 end_p
= p
+ strlen(p
);
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
])))
212 q
= end_p
- strlen(suffix
);
213 if (!strncmp(q
, suffix
, strlen(suffix
)))
217 p
= strstr(p
+1, name
);
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
,
233 char *first_colon
, *first_space
;
236 BUG_ON(!crash_size
|| !crash_base
);
240 ck_cmdline
= get_last_crashkernel(cmdline
, name
, suffix
);
245 ck_cmdline
+= strlen(name
);
248 return parse_crashkernel_suffix(ck_cmdline
, crash_size
,
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
;
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
));
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
)
322 buf
= append_elf_note(buf
, VMCOREINFO_NOTE_NAME
, 0, vmcoreinfo_data
,
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
, ...)
340 r
= vscnprintf(buf
, sizeof(buf
), fmt
, 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
);
370 VMCOREINFO_SYMBOL(swapper_pg_dir
);
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
);
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
);
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
);
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
);
427 VMCOREINFO_NUMBER(PG_head_mask
);
428 VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE
);
429 #ifdef CONFIG_HUGETLB_PAGE
430 VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR
);
433 arch_crash_save_vmcoreinfo();
434 update_vmcoreinfo_note();
439 subsys_initcall(crash_save_vmcoreinfo_init
);