1 /*
2 * Crasher module for kdump testing
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright © IBM Corporation 2007
19 *
20 */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/proc_fs.h>
26 #include <linux/spinlock.h>
27 #include <asm/uaccess.h>
28 #include <linux/version.h>
29 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
30 # include <asm/system.h>
31 #endif
32
33 MODULE_LICENSE("GPL");
34
35 int crasher_init(void);
36 void crasher_exit(void);
37 module_init(crasher_init);
38 module_exit(crasher_exit);
39
40 #define CRASH "crasher" /* name of /proc entry file */
41
crasher_read(char * buf,char ** start,off_t offset,int len,int * eof,void * data)42 static int crasher_read(char *buf, char **start, off_t offset, int len,
43 int *eof, void *data)
44 {
45 return (sprintf(buf, "\n"));
46 }
47
crasher_write(struct file * file,const char * buffer,unsigned long count,void * data)48 static int crasher_write(struct file *file, const char *buffer,
49 unsigned long count, void *data)
50 {
51 char value, *a;
52 DEFINE_SPINLOCK(mylock);
53
54 /* grab the first byte the user gave us, ignore the rest */
55 if (copy_from_user(&value, buffer, 1))
56 return -EFAULT;
57
58 switch (value) {
59 case '0': /* panic the system */
60 panic("KDUMP test panic\n");
61 break;
62
63 case '1': /* BUG() test */
64 BUG();
65 break;
66
67 case '2': /* panic_on_oops test */
68 a = 0;
69 a[1] = 'A';
70 break;
71
72 case '3': /* hang w/double spinlock */
73 spin_lock_irq(&mylock);
74 spin_lock_irq(&mylock);
75 break;
76
77 default:
78 printk("crasher: Bad command\n");
79 }
80
81 return count; /* tell the user we read all his data,
82 somtimes white lies are ok */
83 }
84
85 /* create a directory in /proc and a debug file in the new directory */
86
crasher_init(void)87 int crasher_init(void)
88 {
89 struct proc_dir_entry *crasher_proc;
90
91 printk("loaded crasher module\n");
92
93 /* build a crasher file that can be set */
94 if ((crasher_proc = create_proc_entry(CRASH, 0, NULL)) == NULL) {
95 return -ENOMEM;
96 }
97 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
98 crasher_proc->owner = THIS_MODULE
99 #endif
100 crasher_proc->read_proc = crasher_read;
101 crasher_proc->write_proc = crasher_write;
102 return 0;
103 }
104
crasher_exit(void)105 void crasher_exit(void)
106 {
107 remove_proc_entry(CRASH, NULL);
108 printk("removed crasher module\n");
109 }
110