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