2 * Standard kernel function entry points for Linux crash dumps.
4 * Created by: Matt Robinson (yakker@sourceforge.net)
5 * Contributions from SGI, IBM, HP, MCL, and others.
7 * Copyright (C) 1999 - 2002 Silicon Graphics, Inc. All rights reserved.
8 * Copyright (C) 2000 - 2002 TurboLinux, Inc. All rights reserved.
9 * Copyright (C) 2001 - 2002 Matt D. Robinson. All rights reserved.
10 * Copyright (C) 2002 Free Software Foundation, Inc. All rights reserved.
12 * This code is released under version 2 of the GNU GPL.
16 * -----------------------------------------------------------------------
20 * This dump code goes back to SGI's first attempts at dumping system
21 * memory on SGI systems running IRIX. A few developers at SGI needed
22 * a way to take this system dump and analyze it, and created 'icrash',
23 * or IRIX Crash. The mechanism (the dumps and 'icrash') were used
24 * by support people to generate crash reports when a system failure
25 * occurred. This was vital for large system configurations that
26 * couldn't apply patch after patch after fix just to hope that the
27 * problems would go away. So the system memory, along with the crash
28 * dump analyzer, allowed support people to quickly figure out what the
29 * problem was on the system with the crash dump.
31 * In comes Linux. SGI started moving towards the open source community,
32 * and upon doing so, SGI wanted to take its support utilities into Linux
33 * with the hopes that they would end up the in kernel and user space to
34 * be used by SGI's customers buying SGI Linux systems. One of the first
35 * few products to be open sourced by SGI was LKCD, or Linux Kernel Crash
36 * Dumps. LKCD comprises of a patch to the kernel to enable system
37 * dumping, along with 'lcrash', or Linux Crash, to analyze the system
38 * memory dump. A few additional system scripts and kernel modifications
39 * are also included to make the dump mechanism and dump data easier to
42 * As soon as LKCD was released into the open source community, a number
43 * of larger companies started to take advantage of it. Today, there are
44 * many community members that contribute to LKCD, and it continues to
45 * flourish and grow as an open source project.
51 * This is the list of system tunables (via /proc) that are available
52 * for Linux systems. All the read, write, etc., functions are listed
53 * here. Currently, there are a few different tunables for dumps:
55 * dump_device (used to be dumpdev):
56 * The device for dumping the memory pages out to. This
57 * may be set to the primary swap partition for disruptive dumps,
58 * and must be an unused partition for non-disruptive dumps.
59 * Todo: In the case of network dumps, this may be interpreted
60 * as the IP address of the netdump server to connect to.
62 * dump_compress (used to be dump_compress_pages):
63 * This is the flag which indicates which compression mechanism
64 * to use. This is a BITMASK, not an index (0,1,2,4,8,16,etc.).
65 * This is the current set of values:
67 * 0: DUMP_COMPRESS_NONE -- Don't compress any pages.
68 * 1: DUMP_COMPRESS_RLE -- This uses RLE compression.
69 * 2: DUMP_COMPRESS_GZIP -- This uses GZIP compression.
72 * The amount of effort the dump module should make to save
73 * information for post crash analysis. This value is now
74 * a BITMASK value, not an index:
76 * 0: Do nothing, no dumping. (DUMP_LEVEL_NONE)
78 * 1: Print out the dump information to the dump header, and
79 * write it out to the dump_device. (DUMP_LEVEL_HEADER)
81 * 2: Write out the dump header and all kernel memory pages.
84 * 4: Write out the dump header and all kernel and user
85 * memory pages. (DUMP_LEVEL_USED)
87 * 8: Write out the dump header and all conventional/cached
88 * memory (RAM) pages in the system (kernel, user, free).
89 * (DUMP_LEVEL_ALL_RAM)
91 * 16: Write out everything, including non-conventional memory
92 * like firmware, proms, I/O registers, uncached memory.
95 * The dump_level will default to 1.
98 * These are the flags to use when talking about dumps. There
99 * are lots of possibilities. This is a BITMASK value, not an index.
101 * -----------------------------------------------------------------------
104 #include <linux/kernel.h>
105 #include <linux/delay.h>
106 #include <linux/reboot.h>
107 #include <linux/fs.h>
108 #include <linux/dump.h>
109 #include "dump_methods.h"
110 #include <linux/proc_fs.h>
111 #include <linux/module.h>
112 #include <linux/utsname.h>
113 #include <linux/highmem.h>
114 #include <linux/miscdevice.h>
115 #include <linux/sysrq.h>
116 #include <linux/sysctl.h>
117 #include <linux/nmi.h>
118 #include <linux/init.h>
120 #include <asm/hardirq.h>
121 #include <asm/uaccess.h>
124 * -----------------------------------------------------------------------
126 * -----------------------------------------------------------------------
130 struct dump_config dump_config = {
138 static _dump_regs_t all_regs;
141 /* Global variables used in dump.h */
142 /* degree of system freeze when dumping */
143 enum dump_silence_levels dump_silence_level = DUMP_HARD_SPIN_CPUS;
145 /* Other global fields */
146 extern struct __dump_header dump_header;
147 struct dump_dev *dump_dev = NULL; /* Active dump device */
148 static int dump_compress = 0;
150 static u16 dump_compress_none(const u8 *old, u16 oldsize, u8 *new, u16 newsize);
151 struct __dump_compress dump_none_compression = {
152 .compress_type = DUMP_COMPRESS_NONE,
153 .compress_func = dump_compress_none,
154 .compress_name = "none",
157 /* our device operations and functions */
158 static int dump_ioctl(struct inode *i, struct file *f,
159 unsigned int cmd, unsigned long arg);
161 static struct file_operations dump_fops = {
162 .owner = THIS_MODULE,
166 static struct miscdevice dump_miscdev = {
167 .minor = CRASH_DUMP_MINOR,
171 MODULE_ALIAS_MISCDEV(CRASH_DUMP_MINOR);
173 /* static variables */
174 static int dump_okay = 0; /* can we dump out to disk? */
175 static spinlock_t dump_lock = SPIN_LOCK_UNLOCKED;
177 /* used for dump compressors */
178 static struct list_head dump_compress_list = LIST_HEAD_INIT(dump_compress_list);
180 /* list of registered dump targets */
181 static struct list_head dump_target_list = LIST_HEAD_INIT(dump_target_list);
183 /* lkcd info structure -- this is used by lcrash for basic system data */
184 struct __lkcdinfo lkcdinfo = {
185 .ptrsz = (sizeof(void *) * 8),
186 #if defined(__LITTLE_ENDIAN)
187 .byte_order = __LITTLE_ENDIAN,
189 .byte_order = __BIG_ENDIAN,
191 .page_shift = PAGE_SHIFT,
192 .page_size = PAGE_SIZE,
193 .page_mask = PAGE_MASK,
194 .page_offset = PAGE_OFFSET,
198 * -----------------------------------------------------------------------
199 * / P R O C T U N A B L E F U N C T I O N S
200 * -----------------------------------------------------------------------
203 static int proc_dump_device(ctl_table *ctl, int write, struct file *f,
204 void *buffer, size_t *lenp);
206 static int proc_doulonghex(ctl_table *ctl, int write, struct file *f,
207 void *buffer, size_t *lenp);
209 * sysctl-tuning infrastructure.
211 static ctl_table dump_table[] = {
212 { .ctl_name = CTL_DUMP_LEVEL,
213 .procname = DUMP_LEVEL_NAME,
214 .data = &dump_config.level,
215 .maxlen = sizeof(int),
217 .proc_handler = proc_doulonghex, },
219 { .ctl_name = CTL_DUMP_FLAGS,
220 .procname = DUMP_FLAGS_NAME,
221 .data = &dump_config.flags,
222 .maxlen = sizeof(int),
224 .proc_handler = proc_doulonghex, },
226 { .ctl_name = CTL_DUMP_COMPRESS,
227 .procname = DUMP_COMPRESS_NAME,
228 .data = &dump_compress, /* FIXME */
229 .maxlen = sizeof(int),
231 .proc_handler = proc_dointvec, },
233 { .ctl_name = CTL_DUMP_DEVICE,
234 .procname = DUMP_DEVICE_NAME,
236 .data = &dump_config.dump_device, /* FIXME */
237 .maxlen = sizeof(int),
238 .proc_handler = proc_dump_device },
240 #ifdef CONFIG_CRASH_DUMP_MEMDEV
241 { .ctl_name = CTL_DUMP_ADDR,
242 .procname = DUMP_ADDR_NAME,
244 .data = &dump_config.dump_addr,
245 .maxlen = sizeof(unsigned long),
246 .proc_handler = proc_doulonghex },
252 static ctl_table dump_root[] = {
253 { .ctl_name = KERN_DUMP,
256 .child = dump_table },
260 static ctl_table kernel_root[] = {
261 { .ctl_name = CTL_KERN,
262 .procname = "kernel",
264 .child = dump_root, },
268 static struct ctl_table_header *sysctl_header;
271 * -----------------------------------------------------------------------
272 * C O M P R E S S I O N F U N C T I O N S
273 * -----------------------------------------------------------------------
277 * Name: dump_compress_none()
278 * Func: Don't do any compression, period.
281 dump_compress_none(const u8 *old, u16 oldsize, u8 *new, u16 newsize)
283 /* just return the old size */
289 * Name: dump_execute()
290 * Func: Execute the dumping process. This makes sure all the appropriate
291 * fields are updated correctly, and calls dump_execute_memdump(),
292 * which does the real work.
295 dump_execute(const char *panic_str, const struct pt_regs *regs)
300 /* make sure we can dump */
302 pr_info("LKCD not yet configured, can't take dump now\n");
306 /* Exclude multiple dumps at the same time,
307 * and disable interrupts, some drivers may re-enable
308 * interrupts in with silence()
310 * Try and acquire spin lock. If successful, leave preempt
311 * and interrupts disabled. See spin_lock_irqsave in spinlock.h
313 local_irq_save(flags);
314 if (!spin_trylock(&dump_lock)) {
315 local_irq_restore(flags);
316 pr_info("LKCD dump already in progress\n");
320 /* Bring system into the strictest level of quiescing for min drift
321 * dump drivers can soften this as required in dev->ops->silence()
323 dump_oncpu = smp_processor_id() + 1;
324 dump_silence_level = DUMP_HARD_SPIN_CPUS;
326 state = dump_generic_execute(panic_str, regs);
329 spin_unlock_irqrestore(&dump_lock, flags);
332 printk("Dump Incomplete or failed!\n");
334 printk("Dump Complete; %d dump pages saved.\n",
335 dump_header.dh_num_dump_pages);
340 * Name: dump_register_compression()
341 * Func: Register a dump compression mechanism.
344 dump_register_compression(struct __dump_compress *item)
347 list_add(&(item->list), &dump_compress_list);
351 * Name: dump_unregister_compression()
352 * Func: Remove a dump compression mechanism, and re-assign the dump
353 * compression pointer if necessary.
356 dump_unregister_compression(int compression_type)
358 struct list_head *tmp;
359 struct __dump_compress *dc;
361 /* let's make sure our list is valid */
362 if (compression_type != DUMP_COMPRESS_NONE) {
363 list_for_each(tmp, &dump_compress_list) {
364 dc = list_entry(tmp, struct __dump_compress, list);
365 if (dc->compress_type == compression_type) {
366 list_del(&(dc->list));
374 * Name: dump_compress_init()
375 * Func: Initialize (or re-initialize) compression scheme.
378 dump_compress_init(int compression_type)
380 struct list_head *tmp;
381 struct __dump_compress *dc;
383 /* try to remove the compression item */
384 list_for_each(tmp, &dump_compress_list) {
385 dc = list_entry(tmp, struct __dump_compress, list);
386 if (dc->compress_type == compression_type) {
387 dump_config.dumper->compress = dc;
388 dump_compress = compression_type;
389 pr_debug("Dump Compress %s\n", dc->compress_name);
395 * nothing on the list -- return ENODATA to indicate an error
398 * EAGAIN: reports "Resource temporarily unavailable" which
399 * isn't very enlightening.
401 printk("compression_type:%d not found\n", compression_type);
407 dumper_setup(unsigned long flags, unsigned long devid)
411 /* unconfigure old dumper if it exists */
413 if (dump_config.dumper) {
414 pr_debug("Unconfiguring current dumper\n");
417 /* set up new dumper */
418 if (dump_config.flags & DUMP_FLAGS_SOFTBOOT) {
419 printk("Configuring softboot based dump \n");
420 #ifdef CONFIG_CRASH_DUMP_MEMDEV
421 dump_config.dumper = &dumper_stage1;
423 printk("Requires CONFIG_CRASHDUMP_MEMDEV. Can't proceed.\n");
427 dump_config.dumper = &dumper_singlestage;
429 dump_config.dumper->dev = dump_dev;
431 ret = dump_configure(devid);
434 pr_debug("%s dumper set up for dev 0x%lx\n",
435 dump_config.dumper->name, devid);
436 dump_config.dump_device = devid;
438 printk("%s dumper set up failed for dev 0x%lx\n",
439 dump_config.dumper->name, devid);
440 dump_config.dumper = NULL;
446 dump_target_init(int target)
449 struct list_head *tmp;
450 struct dump_dev *dev;
453 case DUMP_FLAGS_DISKDUMP:
454 strcpy(type, "blockdev"); break;
455 case DUMP_FLAGS_NETDUMP:
456 strcpy(type, "networkdev"); break;
462 * This is a bit stupid, generating strings from flag
463 * and doing strcmp. This is done because 'struct dump_dev'
464 * has string 'type_name' and not interger 'type'.
466 list_for_each(tmp, &dump_target_list) {
467 dev = list_entry(tmp, struct dump_dev, list);
468 if (strcmp(type, dev->type_name) == 0) {
478 * Func: Allow all dump tunables through a standard ioctl() mechanism.
479 * This is far better than before, where we'd go through /proc,
480 * because now this will work for multiple OS and architectures.
483 dump_ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
485 /* check capabilities */
486 if (!capable(CAP_SYS_ADMIN))
489 if (!dump_config.dumper && cmd == DIOSDUMPCOMPRESS)
490 /* dump device must be configured first */
494 * This is the main mechanism for controlling get/set data
495 * for various dump device parameters. The real trick here
496 * is setting the dump device (DIOSDUMPDEV). That's what
497 * triggers everything else.
500 case DIOSDUMPDEV: /* set dump_device */
501 pr_debug("Configuring dump device\n");
502 if (!(f->f_flags & O_RDWR))
506 return dumper_setup(dump_config.flags, arg);
509 case DIOGDUMPDEV: /* get dump_device */
510 return put_user((long)dump_config.dump_device, (long *)arg);
512 case DIOSDUMPLEVEL: /* set dump_level */
513 if (!(f->f_flags & O_RDWR))
516 /* make sure we have a positive value */
520 /* Fixme: clean this up */
521 dump_config.level = 0;
524 case DUMP_LEVEL_ALL_RAM:
525 dump_config.level |= DUMP_MASK_UNUSED;
526 case DUMP_LEVEL_USED:
527 dump_config.level |= DUMP_MASK_USED;
528 case DUMP_LEVEL_KERN:
529 dump_config.level |= DUMP_MASK_KERN;
530 case DUMP_LEVEL_HEADER:
531 dump_config.level |= DUMP_MASK_HEADER;
532 case DUMP_LEVEL_NONE:
537 pr_debug("Dump Level 0x%lx\n", dump_config.level);
540 case DIOGDUMPLEVEL: /* get dump_level */
541 /* fixme: handle conversion */
542 return put_user((long)dump_config.level, (long *)arg);
545 case DIOSDUMPFLAGS: /* set dump_flags */
547 if (!(f->f_flags & O_RDWR))
550 /* make sure we have a positive value */
554 if (dump_target_init(arg & DUMP_FLAGS_TARGETMASK) < 0)
555 return -EINVAL; /* return proper error */
557 dump_config.flags = arg;
559 pr_debug("Dump Flags 0x%lx\n", dump_config.flags);
562 case DIOGDUMPFLAGS: /* get dump_flags */
563 return put_user((long)dump_config.flags, (long *)arg);
565 case DIOSDUMPCOMPRESS: /* set the dump_compress status */
566 if (!(f->f_flags & O_RDWR))
569 return dump_compress_init((int)arg);
571 case DIOGDUMPCOMPRESS: /* get the dump_compress status */
572 return put_user((long)(dump_config.dumper ?
573 dump_config.dumper->compress->compress_type : 0),
575 case DIOGDUMPOKAY: /* check if dump is configured */
576 return put_user((long)dump_okay, (long *)arg);
578 case DIOSDUMPTAKE: /* Trigger a manual dump */
579 /* Do not proceed if lkcd not yet configured */
581 printk("LKCD not yet configured. Cannot take manual dump\n");
586 return manual_handle_crashdump();
590 * these are network dump specific ioctls, let the
591 * module handle them.
593 return dump_dev_ioctl(cmd, arg);
599 * Handle special cases for dump_device
600 * changing dump device requires doing an opening the device
603 proc_dump_device(ctl_table *ctl, int write, struct file *f,
604 void *buffer, size_t *lenp)
606 int *valp = ctl->data;
610 /* same permission checks as ioctl */
611 if (capable(CAP_SYS_ADMIN)) {
612 ret = proc_doulonghex(ctl, write, f, buffer, lenp);
613 if (ret == 0 && write && *valp != oval) {
614 /* need to restore old value to close properly */
615 dump_config.dump_device = (dev_t) oval;
617 ret = dumper_setup(dump_config.flags, (dev_t) *valp);
624 /* All for the want of a proc_do_xxx routine which prints values in hex */
626 proc_doulonghex(ctl_table *ctl, int write, struct file *f,
627 void *buffer, size_t *lenp)
634 if (!ctl->data || !ctl->maxlen || !*lenp || (f->f_pos)) {
639 i = (unsigned long *) ctl->data;
642 sprintf(buf, "0x%lx\n", (*i));
646 if(copy_to_user(buffer, buf, len))
656 * -----------------------------------------------------------------------
657 * I N I T F U N C T I O N S
658 * -----------------------------------------------------------------------
662 * These register and unregister routines are exported for modules
663 * to register their dump drivers (like block, net etc)
666 dump_register_device(struct dump_dev *ddev)
668 struct list_head *tmp;
669 struct dump_dev *dev;
671 list_for_each(tmp, &dump_target_list) {
672 dev = list_entry(tmp, struct dump_dev, list);
673 if (strcmp(ddev->type_name, dev->type_name) == 0) {
674 printk("Target type %s already registered\n",
676 return -1; /* return proper error */
679 list_add(&(ddev->list), &dump_target_list);
685 dump_unregister_device(struct dump_dev *ddev)
687 list_del(&(ddev->list));
688 if (ddev != dump_dev)
693 if (dump_config.dumper)
696 dump_config.flags &= ~DUMP_FLAGS_TARGETMASK;
699 dump_config.dumper = NULL;
702 static int panic_event(struct notifier_block *this, unsigned long event,
706 get_current_general_regs(&all_regs);
707 get_current_cp14_regs(&all_regs);
708 get_current_cp15_regs(&all_regs);
709 dump_execute((const char *)ptr, &all_regs);
713 get_current_regs(®s);
714 dump_execute((const char *)ptr, ®s);
719 extern struct notifier_block *panic_notifier_list;
720 static int panic_event(struct notifier_block *, unsigned long, void *);
721 static struct notifier_block panic_block = {
722 .notifier_call = panic_event,
725 #ifdef CONFIG_MAGIC_SYSRQ
727 static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
728 struct tty_struct *tty) {
729 dump_execute("sysrq", pt_regs);
732 static struct sysrq_key_op sysrq_crashdump_op = {
733 .handler = sysrq_handle_crashdump,
735 .action_msg = "Starting crash dump",
740 dump_sysrq_register(void)
742 #ifdef CONFIG_MAGIC_SYSRQ
743 __sysrq_lock_table();
744 __sysrq_put_key_op(DUMP_SYSRQ_KEY, &sysrq_crashdump_op);
745 __sysrq_unlock_table();
750 dump_sysrq_unregister(void)
752 #ifdef CONFIG_MAGIC_SYSRQ
753 __sysrq_lock_table();
754 if (__sysrq_get_key_op(DUMP_SYSRQ_KEY) == &sysrq_crashdump_op)
755 __sysrq_put_key_op(DUMP_SYSRQ_KEY, NULL);
756 __sysrq_unlock_table();
762 * Func: Initialize the dump process. This will set up any architecture
763 * dependent code. The big key is we need the memory offsets before
764 * the page table is initialized, because the base memory offset
765 * is changed after paging_init() is called.
773 /* try to create our dump device */
774 err = misc_register(&dump_miscdev);
776 printk("cannot register dump character device!\n");
780 __dump_init((u64)PAGE_OFFSET);
782 /* set the dump_compression_list structure up */
783 dump_register_compression(&dump_none_compression);
785 /* grab the total memory size now (not if/when we crash) */
788 /* set the memory size */
789 dump_header.dh_memory_size = (u64)info.totalram;
791 sysctl_header = register_sysctl_table(kernel_root, 0);
792 dump_sysrq_register();
794 notifier_chain_register(&panic_notifier_list, &panic_block);
795 dump_function_ptr = dump_execute;
797 pr_info("Crash dump driver initialized.\n");
806 if (dump_config.dumper)
809 /* arch-specific cleanup routine */
812 /* ignore errors while unregistering -- since can't do anything */
813 unregister_sysctl_table(sysctl_header);
814 misc_deregister(&dump_miscdev);
815 dump_sysrq_unregister();
816 notifier_chain_unregister(&panic_notifier_list, &panic_block);
817 dump_function_ptr = NULL;
820 EXPORT_SYMBOL(dump_register_compression);
821 EXPORT_SYMBOL(dump_unregister_compression);
822 EXPORT_SYMBOL(dump_register_device);
823 EXPORT_SYMBOL(dump_unregister_device);
824 EXPORT_SYMBOL(dump_config);
825 EXPORT_SYMBOL(dump_silence_level);
827 EXPORT_SYMBOL(__dump_irq_enable);
828 EXPORT_SYMBOL(__dump_irq_restore);
830 MODULE_AUTHOR("Matt D. Robinson <yakker@sourceforge.net>");
831 MODULE_DESCRIPTION("Linux Kernel Crash Dump (LKCD) driver");
832 MODULE_LICENSE("GPL");
834 module_init(dump_init);
835 module_exit(dump_cleanup);