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