2 * Standard kernel function entry points for Linux crash dumps.
4 * Copyright (C) 1999 - 2005 Silicon Graphics, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2000 - 2002 TurboLinux, Inc. All rights reserved.
7 * Copyright (C) 2001 - 2002 Matt D. Robinson. All rights reserved.
8 * Copyright (C) 2002 Free Software Foundation, Inc. All rights reserved.
10 * This code is released under version 2 of the GNU GPL.
13 #include <linux/kernel.h>
14 #include <linux/dump.h>
15 #include <linux/sysrq.h>
16 #include <linux/reboot.h>
18 #include "dump_methods.h"
21 #define LKCD_VERSION "7.0.1"
22 #define LKCD_DATE "2005-06-01"
23 #define DUMP_MAJOR 0 /* dynamic major by default */
25 MODULE_AUTHOR("LKCD development team <lkcd-devel@lists.sourceforge.net>");
26 MODULE_DESCRIPTION("Linux Kernel Crash Dump (LKCD) driver");
27 MODULE_LICENSE("GPL");
28 MODULE_VERSION(LKCD_VERSION);
31 * -----------------------------------------------------------------------
33 * -----------------------------------------------------------------------
37 struct dump_config dump_config = {
48 /* forward declorations */
49 static int dump_target_init(int);
50 static int dumper_setup(const char *);
51 static int dump_compress_init(int compression_type);
53 /* sysfs input protection semaphore */
54 static DECLARE_MUTEX(dump_sysfs_mutex);
56 /* degree of system freeze when dumping */
57 enum dump_silence_levels dump_silence_level = DUMP_HARD_SPIN_CPUS;
59 /* Other global fields */
60 extern struct __dump_header dump_header;
61 struct dump_dev *dump_dev = NULL; /* Active dump device */
62 struct dump_dev_driver *dump_dev_driver = NULL; /* acutall driver device */
63 static int dump_compress = 0;
64 int dump_compress_level = 1;
66 static u32 dump_compress_none(const u8 *old, u32 oldsize, u8 *new, u32 newsize,
68 struct __dump_compress dump_none_compression = {
69 .compress_type = DUMP_COMPRESS_NONE,
70 .compress_func = dump_compress_none,
71 .compress_name = "none",
74 /* static variables */
75 static int dump_okay = 0;
76 static spinlock_t dump_lock = SPIN_LOCK_UNLOCKED;
78 /* used for dump compressors */
79 static struct list_head dump_compress_list = LIST_HEAD_INIT(
82 /* list of registered dump targets */
83 static struct list_head dump_target_list = LIST_HEAD_INIT(dump_target_list);
85 /* lkcd info structure -- this is used by lcrash for basic system data */
86 struct __lkcdinfo lkcdinfo = {
87 .ptrsz = (sizeof(void *) * 8),
88 #if defined(__LITTLE_ENDIAN)
89 .byte_order = __LITTLE_ENDIAN,
91 .byte_order = __BIG_ENDIAN,
93 .page_shift = PAGE_SHIFT,
94 .page_size = PAGE_SIZE,
95 .page_mask = PAGE_MASK,
96 .page_offset = PAGE_OFFSET,
99 struct dump_attribute {
100 struct attribute attr;
101 ssize_t (*show) (struct dump_dev_driver *ddev, char *buf);
102 ssize_t (*store)(struct dump_dev_driver *ddev, const char *buf,
106 #define DUMPVAR_ATTR(_name, _mode, _show, _store) \
107 struct dump_attribute dump_attr_##_name = { \
108 .attr = {.name = __stringify(_name), \
110 .owner = THIS_MODULE}, \
115 #define to_dump_attr(_attr) container_of(_attr,struct dump_attribute,attr)
116 #define to_dump_dev_driver(obj) container_of(obj,struct dump_dev_driver,kobj)
118 ssize_t dump_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
120 struct dump_dev_driver *dev = to_dump_dev_driver(kobj);
121 struct dump_attribute *dump_attr = to_dump_attr(attr);
124 if (!dump_config.dumper)
125 /* dump device must be configured first */
128 down_interruptible(&dump_sysfs_mutex);
130 ret = dump_attr->show(dev, buf);
131 up(&dump_sysfs_mutex);
136 ssize_t dump_attr_store(struct kobject *kobj, struct attribute *attr,
137 const char *buf, size_t count)
139 struct dump_dev_driver *dev = to_dump_dev_driver(kobj);
140 struct dump_attribute *dump_attr = to_dump_attr(attr);
143 if (!dump_config.dumper)
144 /* dump device must be configured first */
147 if (dump_attr->store)
148 ret = dump_attr->store(dev, buf, count);
152 static struct sysfs_ops dump_attr_ops = {
153 .show = dump_attr_show,
154 .store = dump_attr_store,
157 ssize_t show_version(struct dump_dev_driver *ddev, char *buf)
159 return sprintf(buf, "%s\n%s\n", LKCD_VERSION, LKCD_DATE);
162 ssize_t show_polling(struct dump_dev_driver *ddev, char *buf)
164 return sprintf(buf, "%d\n", dump_config.polling);
167 ssize_t store_polling(struct dump_dev_driver *ddev, const char *buf,
175 if ((sscanf(buf, "%lx", &tmp)) != 1)
178 if ((tmp < 0) || (tmp > 1))
181 dump_config.polling = tmp;
183 /* If dump_device has already been initalized and we
184 * want to change the polling status we need to
185 * re-init dumpdev with the new polling value.
187 if (dump_config.dump_device)
188 dumper_setup(dump_config.dump_device);
193 ssize_t show_dumpdev(struct dump_dev_driver *ddev, char *buf)
195 return sprintf(buf, "%s\n", dump_config.dump_device);
198 ssize_t store_dumpdev(struct dump_dev_driver *ddev, const char *buf,
202 printk("LKCD: Configuring dump device\n");
207 if ((strncmp(buf, "eth", 3) == 0) |
208 (strncmp(buf, "ath", 3) == 0) |
209 (strncmp(buf, "wlan", 4) == 0)){
211 type = DUMP_TYPE_NETDEV;
213 type = DUMP_TYPE_BLOCKDEV;
215 if (dump_target_init(type) < 0)
220 err = dumper_setup(buf);
224 /* do we have a compress value that was set
225 * before we had a dump dump_dev that needs
228 if (dump_config.comp_flag) {
229 dump_compress_init((int)dump_config.comp_val);
230 dump_config.comp_flag = 0;
236 ssize_t show_level(struct dump_dev_driver *ddev, char *buf)
238 return sprintf(buf, "0x%lx\n", dump_config.level);
241 ssize_t store_level(struct dump_dev_driver *ddev, const char *buf,
249 if ( ( sscanf(buf, "%lx", &tmp)) != 1)
255 dump_config.level = 0;
257 /* FIXME this is terrible and make it impossible for
258 * the user to see what they set. I'm leaving it only for
259 * the first rev and will fix this soon! -- troyh
263 case DUMP_LEVEL_ALL_RAM:
264 dump_config.level |= DUMP_MASK_UNUSED;
265 case DUMP_LEVEL_USED:
266 dump_config.level |= DUMP_MASK_USED;
267 case DUMP_LEVEL_KERN:
268 dump_config.level |= DUMP_MASK_KERN;
269 case DUMP_LEVEL_HEADER:
270 dump_config.level |= DUMP_MASK_HEADER;
271 case DUMP_LEVEL_NONE:
276 printk("LKCD: Dump Level 0x%lx\n", dump_config.level);
282 ssize_t show_compress(struct dump_dev_driver *ddev, char *buf)
285 if (dump_config.comp_flag)
286 return sprintf(buf, "%d\n", dump_config.comp_val);
288 return sprintf(buf, "%d\n", dump_compress);
291 ssize_t store_compress(struct dump_dev_driver *ddev, const char *buf,
299 if ((sscanf(buf, "%lx", &tmp)) != 1)
302 if ((tmp < 0) | (tmp > 2))
305 /* dump_config.dump_device must valid first to establish
306 * the compression type. Will take the parameter now and
307 * delay the compress_init until we have a dump_device.
309 if (dump_config.dump_device == 0){
310 dump_config.comp_flag = 1;
311 dump_config.comp_val = tmp;
313 dump_compress_init((int)tmp);
319 ssize_t show_compress_level(struct dump_dev_driver *ddev, char *buf)
321 return sprintf(buf, "%d\n", dump_compress_level);
324 ssize_t store_compress_level(struct dump_dev_driver *ddev, const char *buf, size_t count)
331 if (sscanf(buf, "%d", &tmp) != 1)
334 /* Small kludge: We allow levels 1-9 here because that's what
335 * makes sense for gzip, which is currently the only compression
336 * type to make use of multiple levels.
338 if (tmp < 1 || tmp > 9)
341 dump_compress_level = tmp;
346 ssize_t show_reboot(struct dump_dev_driver *ddev, char *buf)
348 return sprintf(buf, "%d\n", dump_config.reboot);
351 ssize_t store_reboot(struct dump_dev_driver *ddev, const char *buf,
359 if ((sscanf(buf, "%lx", &tmp)) != 1)
362 if ((tmp < 0) | (tmp > 1))
365 dump_config.reboot = tmp;
372 static DUMPVAR_ATTR(polling, 0664, show_polling, store_polling);
373 static DUMPVAR_ATTR(dumpdev, 0664, show_dumpdev, store_dumpdev);
374 static DUMPVAR_ATTR(level, 0664, show_level, store_level);
375 static DUMPVAR_ATTR(compress, 0664, show_compress, store_compress);
376 static DUMPVAR_ATTR(compress_level, 0664, show_compress_level, store_compress_level);
377 static DUMPVAR_ATTR(reboot, 0664, show_reboot, store_reboot);
378 static DUMPVAR_ATTR(version, 0444, show_version, NULL);
380 /* These are default attributes for the dump device
383 static struct attribute *def_attrs[] = {
387 static struct dump_attribute *dump_attrs[] = {
392 &dump_attr_compress_level,
399 * dump_release - free dump structure
400 * @kobj: kobject of dump structure
402 * This is called when the refcount of the dump structure
403 * reaches 0. This should happen right after we unregister,
404 * but just in case, we use the release callback anyway.
407 static void dump_release(struct kobject *kobj)
409 struct dump_dev_driver *dev = to_dump_dev_driver(kobj);
413 static struct kobj_type ktype_dump = {
414 .release = dump_release,
415 .sysfs_ops = &dump_attr_ops,
416 .default_attrs = def_attrs,
419 static decl_subsys(dump, &ktype_dump, NULL);
423 * -----------------------------------------------------------------------
424 * C O M P R E S S I O N F U N C T I O N S
425 * -----------------------------------------------------------------------
429 * Name: dump_compress_none()
430 * Func: Don't do any compression, period.
433 dump_compress_none(const u8 *old, u32 oldsize, u8 *new, u32 newsize,
436 /* just return the old size */
441 * Name: dump_execute()
442 * Func: Execute the dumping process. This makes sure all the appropriate
443 * fields are updated correctly, and calls dump_execute_memdump(),
444 * which does the real work.
446 void dump_execute(const char *panic_str, const struct pt_regs *regs)
450 int loglevel_save = console_loglevel;
452 if (console_loglevel < 6) /* KERN_INFO */
453 console_loglevel = 6;
455 /* make sure we can dump */
457 pr_info("LKCD: not yet configured, can't take dump now\n");
461 /* Exclude multiple dumps at the same time,
462 * and disable interrupts, some drivers may re-enable
463 * interrupts in with silence()
465 * Try and acquire spin lock. If successful, leave preempt
466 * and interrupts disabled. See spin_lock_irqsave in spinlock.h
468 local_irq_save(flags);
469 if (!spin_trylock(&dump_lock)) {
470 local_irq_restore(flags);
471 pr_info("LKCD: dump already in progress\n");
475 /* What state are interrupts really in? */
476 if (in_interrupt()) {
478 printk("LKCD: Dumping from interrupt handler!\n");
480 printk("LKCD: Dumping from bottom half!\n");
483 * If we are not doing polling I/O then we should attempt
484 * to clean up the irq state.
486 * If polling I/O falls back to interrupt-driven mode then
487 * it will need to clean the IRQ state
489 if (!(dump_config.polling))
490 __dump_clean_irq_state();
493 /* Bring system into the strictest level of quiescing for min drift
494 * dump drivers can soften this as required in dev->ops->silence()
496 dump_oncpu = smp_processor_id() + 1;
497 dump_silence_level = DUMP_HARD_SPIN_CPUS;
499 state = dump_generic_execute(panic_str, regs);
502 spin_unlock_irqrestore(&dump_lock, flags);
505 printk("LKCD: Dump Incomplete or failed!\n");
507 printk("LKCD: Dump Complete; %d dump pages saved.\n",
508 dump_header.dh_num_dump_pages);
511 console_loglevel = loglevel_save;
513 if (dump_config.reboot)
519 * Name: dump_register_compression()
520 * Func: Register a dump compression mechanism.
522 void dump_register_compression(struct __dump_compress *item)
525 list_add(&(item->list), &dump_compress_list);
529 * Name: dump_unregister_compression()
530 * Func: Remove a dump compression mechanism, and re-assign the dump
531 * compression pointer if necessary.
533 void dump_unregister_compression(int compression_type)
535 struct list_head *tmp;
536 struct __dump_compress *dc;
538 /* let's make sure our list is valid */
539 if (compression_type == DUMP_COMPRESS_NONE)
541 list_for_each(tmp, &dump_compress_list) {
542 dc = list_entry(tmp, struct __dump_compress, list);
543 if (dc->compress_type == compression_type) {
545 * If we're currently configured to dump using
546 * the compression mechanism we're removing,
547 * unconfigure ourselves.
549 if (dump_config.dumper &&
550 dump_config.dumper->compress == dc) {
554 dump_config.dumper = NULL;
556 list_del(&(dc->list));
563 * Name: dump_compress_init()
564 * Func: Initialize (or re-initialize) compression scheme.
566 static int dump_compress_init(int compression_type)
568 struct list_head *tmp;
569 struct __dump_compress *dc;
571 list_for_each(tmp, &dump_compress_list) {
572 dc = list_entry(tmp, struct __dump_compress, list);
573 if (dc->compress_type == compression_type) {
574 dump_config.dumper->compress = dc;
575 dump_compress = compression_type;
576 printk("LKCD: %s compression initalized\n",
582 printk("LKCD: compression_type:%d not found\n", compression_type);
586 static int dumper_setup(const char *devid)
590 /* unconfigure old dumper if it exists */
592 if (dump_config.dumper) {
593 printk("LKCD: Unconfiguring current dumper\n");
596 /* set up new dumper */
597 dump_config.dumper = &dumper_singlestage;
599 dump_config.dumper->dev = dump_dev;
601 if (dump_config.dump_device != devid) {
602 kfree(dump_config.dump_device);
603 if (!(dump_config.dump_device = kstrdup(devid, GFP_KERNEL)))
607 ret = dump_configure(devid);
610 printk("LKCD: %s dumper set up for dev %s\n",
611 dump_config.dumper->name,
612 dump_config.dump_device);
614 printk("LKCD: %s dumper set up failed for dev %s\n",
615 dump_config.dumper->name,
616 dump_config.dump_device);
617 dump_config.dumper = NULL;
618 dump_config.dump_device = NULL;
623 static int dump_target_init(int type)
625 struct list_head *tmp;
626 struct dump_dev *dev;
628 list_for_each(tmp, &dump_target_list) {
629 dev = list_entry(tmp, struct dump_dev, list);
630 if (type == dev->type) {
642 * -----------------------------------------------------------------------
643 * I N I T F U N C T I O N S
644 * -----------------------------------------------------------------------
647 static void dump_populate_dir(struct dump_dev_driver * ddev)
649 struct dump_attribute *attr;
652 for (i = 0; (attr = dump_attrs[i]) && !err; i++) {
654 err = sysfs_create_file(&dump_subsys.kset.kobj,
660 * These register and unregister routines are exported for modules
661 * to register their dump drivers (like block, net etc)
663 int dump_register_device(struct dump_dev *ddev)
665 struct list_head *tmp;
666 struct dump_dev *dev;
668 list_for_each(tmp, &dump_target_list) {
669 dev = list_entry(tmp, struct dump_dev, list);
670 if (ddev->type == dev->type) {
671 printk("LKCD: Target type %d already registered\n",
673 return -1; /* return proper error */
676 list_add(&(ddev->list), &dump_target_list);
681 void dump_unregister_device(struct dump_dev *ddev)
683 list_del(&(ddev->list));
684 if (ddev != dump_dev)
689 if (dump_config.dumper)
694 dump_config.dumper = NULL;
697 static int panic_event(struct notifier_block *this, unsigned long event,
702 get_current_regs(®s);
703 dump_execute((const char *)ptr, ®s);
707 extern struct notifier_block *panic_notifier_list;
708 static int panic_event(struct notifier_block *, unsigned long, void *);
709 static struct notifier_block panic_block = {
710 .notifier_call = panic_event,
713 #ifdef CONFIG_MAGIC_SYSRQ
715 static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
716 struct tty_struct *tty) {
719 get_current_regs(®s);
720 dump_execute("sysrq", ®s);
723 dump_execute("sysrq", pt_regs);
727 static struct sysrq_key_op sysrq_crashdump_op = {
728 .handler = sysrq_handle_crashdump,
730 .action_msg = "Starting crash dump",
734 static inline void dump_sysrq_register(void)
736 #ifdef CONFIG_MAGIC_SYSRQ
737 register_sysrq_key(DUMP_SYSRQ_KEY, &sysrq_crashdump_op);
741 static inline void dump_sysrq_unregister(void)
743 #ifdef CONFIG_MAGIC_SYSRQ
744 unregister_sysrq_key(DUMP_SYSRQ_KEY, &sysrq_crashdump_op);
750 * Func: Initialize the dump process. This will set up any architecture
753 static int __init dump_init(void)
758 err = subsystem_register(&dump_subsys);
763 dump_dev_driver = kmalloc(sizeof(struct dump_dev_driver), GFP_KERNEL);
764 if (!dump_dev_driver) {
765 subsystem_unregister(&dump_subsys);
769 memset(dump_dev_driver, 0, sizeof(struct dump_dev_driver));
771 kobject_set_name(&dump_dev_driver->kobj, "dump_dev_driver");
772 kobj_set_kset_s(dump_dev_driver, dump_subsys);
774 /* initalize but do not register the kobject
775 * that represents the dump device, we only want
776 * if for refcount, the important attributes are
777 * assigned to the dump_subsys kobject anyway.
779 kobject_init(&dump_dev_driver->kobj);
781 dump_populate_dir(dump_dev_driver);
783 __dump_init((u64) PAGE_OFFSET);
785 /* set the dump_compression_list structure up */
786 dump_register_compression(&dump_none_compression);
788 /* grab the total memory size now (not if/when we crash) */
791 /* set the memory size */
792 dump_header.dh_memory_size = (u64) info.totalram;
794 dump_sysrq_register();
796 notifier_chain_register(&panic_notifier_list, &panic_block);
797 dump_function_ptr = dump_execute;
799 pr_info("LKCD: Crash dump driver initialized.\n");
803 static inline void dump_device_driver_unregister(struct dump_dev_driver *dev)
805 kobject_unregister(&dev->kobj);
808 static void __exit dump_cleanup(void)
810 subsystem_unregister(&dump_subsys);
812 if (dump_dev_driver) {
813 dump_device_driver_unregister(dump_dev_driver);
818 if (dump_config.dumper)
821 /* arch-specific cleanup routine */
824 /* ignore errors while unregistering -- since can't do anything */
825 dump_sysrq_unregister();
826 notifier_chain_unregister(&panic_notifier_list, &panic_block);
827 dump_function_ptr = NULL;
829 pr_info("LKCD: Crash dump driver unloaded.\n");
832 EXPORT_SYMBOL_GPL(dump_register_compression);
833 EXPORT_SYMBOL_GPL(dump_unregister_compression);
834 EXPORT_SYMBOL_GPL(dump_register_device);
835 EXPORT_SYMBOL_GPL(dump_unregister_device);
836 EXPORT_SYMBOL_GPL(dump_config);
837 EXPORT_SYMBOL_GPL(dump_compress_level);
838 EXPORT_SYMBOL_GPL(dump_silence_level);
839 EXPORT_SYMBOL_GPL(__dump_irq_enable);
840 EXPORT_SYMBOL_GPL(__dump_irq_restore);
842 module_init(dump_init);
843 module_exit(dump_cleanup);