ARM: sunxi: dt: Convert to the new SID compatibles
[deliverable/linux.git] / drivers / misc / lkdtm.c
CommitLineData
8bb31b9d
AG
1/*
2 * Kprobe module for testing crash dumps
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 (C) IBM Corporation, 2006
19 *
20 * Author: Ankita Garg <ankita@in.ibm.com>
21 *
22 * This module induces system failures at predefined crashpoints to
23 * evaluate the reliability of crash dumps obtained using different dumping
24 * solutions.
25 *
26 * It is adapted from the Linux Kernel Dump Test Tool by
27 * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
28 *
0347af4e 29 * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
8bb31b9d 30 *
0347af4e 31 * See Documentation/fault-injection/provoke-crashes.txt for instructions
8bb31b9d 32 */
feac6e21 33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8bb31b9d
AG
34
35#include <linux/kernel.h>
5d861d92 36#include <linux/fs.h>
8bb31b9d 37#include <linux/module.h>
5d861d92 38#include <linux/buffer_head.h>
8bb31b9d 39#include <linux/kprobes.h>
5d861d92 40#include <linux/list.h>
8bb31b9d 41#include <linux/init.h>
8bb31b9d 42#include <linux/interrupt.h>
5d861d92 43#include <linux/hrtimer.h>
5a0e3ad6 44#include <linux/slab.h>
8bb31b9d 45#include <scsi/scsi_cmnd.h>
0347af4e 46#include <linux/debugfs.h>
cc33c537 47#include <linux/vmalloc.h>
9ae113ce 48#include <linux/mman.h>
8bb31b9d
AG
49
50#ifdef CONFIG_IDE
51#include <linux/ide.h>
52#endif
53
7d196ac3
KC
54/*
55 * Make sure our attempts to over run the kernel stack doesn't trigger
56 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
57 * recurse past the end of THREAD_SIZE by default.
58 */
59#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
60#define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
61#else
62#define REC_STACK_SIZE (THREAD_SIZE / 8)
63#endif
64#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
65
8bb31b9d 66#define DEFAULT_COUNT 10
cc33c537 67#define EXEC_SIZE 64
8bb31b9d
AG
68
69enum cname {
93e2f585
NK
70 CN_INVALID,
71 CN_INT_HARDWARE_ENTRY,
72 CN_INT_HW_IRQ_EN,
73 CN_INT_TASKLET_ENTRY,
74 CN_FS_DEVRW,
75 CN_MEM_SWAPOUT,
76 CN_TIMERADD,
77 CN_SCSI_DISPATCH_CMD,
78 CN_IDE_CORE_CP,
79 CN_DIRECT,
8bb31b9d
AG
80};
81
82enum ctype {
93e2f585
NK
83 CT_NONE,
84 CT_PANIC,
85 CT_BUG,
65892723 86 CT_WARNING,
93e2f585
NK
87 CT_EXCEPTION,
88 CT_LOOP,
89 CT_OVERFLOW,
90 CT_CORRUPT_STACK,
91 CT_UNALIGNED_LOAD_STORE_WRITE,
92 CT_OVERWRITE_ALLOCATION,
93 CT_WRITE_AFTER_FREE,
94 CT_SOFTLOCKUP,
95 CT_HARDLOCKUP,
274a5855 96 CT_SPINLOCKUP,
93e2f585 97 CT_HUNG_TASK,
cc33c537
KC
98 CT_EXEC_DATA,
99 CT_EXEC_STACK,
100 CT_EXEC_KMALLOC,
101 CT_EXEC_VMALLOC,
9ae113ce
KC
102 CT_EXEC_USERSPACE,
103 CT_ACCESS_USERSPACE,
104 CT_WRITE_RO,
dc2b9e90 105 CT_WRITE_KERN,
8bb31b9d
AG
106};
107
108static char* cp_name[] = {
109 "INT_HARDWARE_ENTRY",
110 "INT_HW_IRQ_EN",
111 "INT_TASKLET_ENTRY",
112 "FS_DEVRW",
113 "MEM_SWAPOUT",
114 "TIMERADD",
115 "SCSI_DISPATCH_CMD",
0347af4e
SK
116 "IDE_CORE_CP",
117 "DIRECT",
8bb31b9d
AG
118};
119
120static char* cp_type[] = {
121 "PANIC",
122 "BUG",
65892723 123 "WARNING",
8bb31b9d
AG
124 "EXCEPTION",
125 "LOOP",
0347af4e
SK
126 "OVERFLOW",
127 "CORRUPT_STACK",
128 "UNALIGNED_LOAD_STORE_WRITE",
129 "OVERWRITE_ALLOCATION",
130 "WRITE_AFTER_FREE",
a48223f9
FW
131 "SOFTLOCKUP",
132 "HARDLOCKUP",
274a5855 133 "SPINLOCKUP",
a48223f9 134 "HUNG_TASK",
cc33c537
KC
135 "EXEC_DATA",
136 "EXEC_STACK",
137 "EXEC_KMALLOC",
138 "EXEC_VMALLOC",
9ae113ce
KC
139 "EXEC_USERSPACE",
140 "ACCESS_USERSPACE",
141 "WRITE_RO",
dc2b9e90 142 "WRITE_KERN",
8bb31b9d
AG
143};
144
145static struct jprobe lkdtm;
146
147static int lkdtm_parse_commandline(void);
148static void lkdtm_handler(void);
149
ec1c620b
AV
150static char* cpoint_name;
151static char* cpoint_type;
8bb31b9d
AG
152static int cpoint_count = DEFAULT_COUNT;
153static int recur_count = REC_NUM_DEFAULT;
154
93e2f585
NK
155static enum cname cpoint = CN_INVALID;
156static enum ctype cptype = CT_NONE;
8bb31b9d 157static int count = DEFAULT_COUNT;
aa2c96d6 158static DEFINE_SPINLOCK(count_lock);
274a5855 159static DEFINE_SPINLOCK(lock_me_up);
8bb31b9d 160
cc33c537
KC
161static u8 data_area[EXEC_SIZE];
162
9ae113ce
KC
163static const unsigned long rodata = 0xAA55AA55;
164
8bb31b9d 165module_param(recur_count, int, 0644);
7d196ac3 166MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
dca41306 167module_param(cpoint_name, charp, 0444);
5d861d92 168MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
dca41306 169module_param(cpoint_type, charp, 0444);
5d861d92
RD
170MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
171 "hitting the crash point");
172module_param(cpoint_count, int, 0644);
173MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
174 "crash point is to be hit to trigger action");
8bb31b9d 175
2118116e 176static unsigned int jp_do_irq(unsigned int irq)
8bb31b9d
AG
177{
178 lkdtm_handler();
179 jprobe_return();
180 return 0;
181}
182
2118116e
AB
183static irqreturn_t jp_handle_irq_event(unsigned int irq,
184 struct irqaction *action)
8bb31b9d
AG
185{
186 lkdtm_handler();
187 jprobe_return();
188 return 0;
189}
190
2118116e 191static void jp_tasklet_action(struct softirq_action *a)
8bb31b9d
AG
192{
193 lkdtm_handler();
194 jprobe_return();
195}
196
2118116e 197static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
8bb31b9d
AG
198{
199 lkdtm_handler();
200 jprobe_return();
201}
202
203struct scan_control;
204
2118116e
AB
205static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
206 struct zone *zone,
207 struct scan_control *sc)
8bb31b9d
AG
208{
209 lkdtm_handler();
210 jprobe_return();
211 return 0;
212}
213
2118116e
AB
214static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
215 const enum hrtimer_mode mode)
8bb31b9d
AG
216{
217 lkdtm_handler();
218 jprobe_return();
219 return 0;
220}
221
2118116e 222static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
8bb31b9d
AG
223{
224 lkdtm_handler();
225 jprobe_return();
226 return 0;
227}
228
229#ifdef CONFIG_IDE
44629432 230static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
8bb31b9d
AG
231 struct block_device *bdev, unsigned int cmd,
232 unsigned long arg)
233{
234 lkdtm_handler();
235 jprobe_return();
236 return 0;
237}
238#endif
239
0347af4e
SK
240/* Return the crashpoint number or NONE if the name is invalid */
241static enum ctype parse_cp_type(const char *what, size_t count)
242{
243 int i;
244
245 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
246 if (!strcmp(what, cp_type[i]))
247 return i + 1;
248 }
249
93e2f585 250 return CT_NONE;
0347af4e
SK
251}
252
253static const char *cp_type_to_str(enum ctype type)
254{
93e2f585 255 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
0347af4e
SK
256 return "None";
257
258 return cp_type[type - 1];
259}
260
261static const char *cp_name_to_str(enum cname name)
262{
93e2f585 263 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
0347af4e
SK
264 return "INVALID";
265
266 return cp_name[name - 1];
267}
268
269
8bb31b9d
AG
270static int lkdtm_parse_commandline(void)
271{
272 int i;
aa2c96d6 273 unsigned long flags;
8bb31b9d 274
0347af4e 275 if (cpoint_count < 1 || recur_count < 1)
8bb31b9d
AG
276 return -EINVAL;
277
aa2c96d6 278 spin_lock_irqsave(&count_lock, flags);
0347af4e 279 count = cpoint_count;
aa2c96d6 280 spin_unlock_irqrestore(&count_lock, flags);
0347af4e
SK
281
282 /* No special parameters */
283 if (!cpoint_type && !cpoint_name)
284 return 0;
285
286 /* Neither or both of these need to be set */
287 if (!cpoint_type || !cpoint_name)
288 return -EINVAL;
289
290 cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
93e2f585 291 if (cptype == CT_NONE)
0347af4e
SK
292 return -EINVAL;
293
294 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
8bb31b9d
AG
295 if (!strcmp(cpoint_name, cp_name[i])) {
296 cpoint = i + 1;
0347af4e 297 return 0;
8bb31b9d
AG
298 }
299 }
300
0347af4e
SK
301 /* Could not find a valid crash point */
302 return -EINVAL;
8bb31b9d
AG
303}
304
7d196ac3 305static int recursive_loop(int remaining)
8bb31b9d 306{
7d196ac3 307 char buf[REC_STACK_SIZE];
8bb31b9d 308
7d196ac3
KC
309 /* Make sure compiler does not optimize this away. */
310 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
311 if (!remaining)
8bb31b9d
AG
312 return 0;
313 else
7d196ac3 314 return recursive_loop(remaining - 1);
8bb31b9d
AG
315}
316
cc33c537
KC
317static void do_nothing(void)
318{
319 return;
320}
321
dc2b9e90
KC
322/* Must immediately follow do_nothing for size calculuations to work out. */
323static void do_overwritten(void)
324{
325 pr_info("do_overwritten wasn't overwritten!\n");
326 return;
327}
328
629c66a2
KC
329static noinline void corrupt_stack(void)
330{
331 /* Use default char array length that triggers stack protection. */
332 char data[8];
333
334 memset((void *)data, 0, 64);
335}
336
cc33c537
KC
337static void execute_location(void *dst)
338{
339 void (*func)(void) = dst;
340
aac416fc
KC
341 pr_info("attempting ok execution at %p\n", do_nothing);
342 do_nothing();
343
cc33c537 344 memcpy(dst, do_nothing, EXEC_SIZE);
aac416fc
KC
345 flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
346 pr_info("attempting bad execution at %p\n", func);
cc33c537
KC
347 func();
348}
349
9ae113ce
KC
350static void execute_user_location(void *dst)
351{
5123662a 352 /* Intentionally crossing kernel/user memory boundary. */
9ae113ce
KC
353 void (*func)(void) = dst;
354
aac416fc
KC
355 pr_info("attempting ok execution at %p\n", do_nothing);
356 do_nothing();
357
5123662a 358 if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
9ae113ce 359 return;
aac416fc
KC
360 flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
361 pr_info("attempting bad execution at %p\n", func);
9ae113ce
KC
362 func();
363}
364
0347af4e 365static void lkdtm_do_action(enum ctype which)
8bb31b9d 366{
0347af4e 367 switch (which) {
93e2f585 368 case CT_PANIC:
0347af4e
SK
369 panic("dumptest");
370 break;
93e2f585 371 case CT_BUG:
0347af4e
SK
372 BUG();
373 break;
65892723
KC
374 case CT_WARNING:
375 WARN_ON(1);
376 break;
93e2f585 377 case CT_EXCEPTION:
0347af4e
SK
378 *((int *) 0) = 0;
379 break;
93e2f585 380 case CT_LOOP:
0347af4e
SK
381 for (;;)
382 ;
383 break;
93e2f585 384 case CT_OVERFLOW:
7d196ac3 385 (void) recursive_loop(recur_count);
0347af4e 386 break;
629c66a2
KC
387 case CT_CORRUPT_STACK:
388 corrupt_stack();
0347af4e 389 break;
93e2f585 390 case CT_UNALIGNED_LOAD_STORE_WRITE: {
0347af4e
SK
391 static u8 data[5] __attribute__((aligned(4))) = {1, 2,
392 3, 4, 5};
393 u32 *p;
394 u32 val = 0x12345678;
395
396 p = (u32 *)(data + 1);
397 if (*p == 0)
398 val = 0x87654321;
399 *p = val;
400 break;
401 }
93e2f585 402 case CT_OVERWRITE_ALLOCATION: {
0347af4e
SK
403 size_t len = 1020;
404 u32 *data = kmalloc(len, GFP_KERNEL);
405
406 data[1024 / sizeof(u32)] = 0x12345678;
407 kfree(data);
408 break;
409 }
93e2f585 410 case CT_WRITE_AFTER_FREE: {
0347af4e
SK
411 size_t len = 1024;
412 u32 *data = kmalloc(len, GFP_KERNEL);
413
414 kfree(data);
415 schedule();
416 memset(data, 0x78, len);
417 break;
418 }
93e2f585 419 case CT_SOFTLOCKUP:
a48223f9
FW
420 preempt_disable();
421 for (;;)
422 cpu_relax();
423 break;
93e2f585 424 case CT_HARDLOCKUP:
a48223f9
FW
425 local_irq_disable();
426 for (;;)
427 cpu_relax();
428 break;
274a5855
KC
429 case CT_SPINLOCKUP:
430 /* Must be called twice to trigger. */
431 spin_lock(&lock_me_up);
5123662a
KC
432 /* Let sparse know we intended to exit holding the lock. */
433 __release(&lock_me_up);
274a5855 434 break;
93e2f585 435 case CT_HUNG_TASK:
a48223f9
FW
436 set_current_state(TASK_UNINTERRUPTIBLE);
437 schedule();
438 break;
cc33c537
KC
439 case CT_EXEC_DATA:
440 execute_location(data_area);
441 break;
442 case CT_EXEC_STACK: {
443 u8 stack_area[EXEC_SIZE];
444 execute_location(stack_area);
445 break;
446 }
447 case CT_EXEC_KMALLOC: {
448 u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
449 execute_location(kmalloc_area);
450 kfree(kmalloc_area);
451 break;
452 }
453 case CT_EXEC_VMALLOC: {
454 u32 *vmalloc_area = vmalloc(EXEC_SIZE);
455 execute_location(vmalloc_area);
456 vfree(vmalloc_area);
457 break;
458 }
9ae113ce
KC
459 case CT_EXEC_USERSPACE: {
460 unsigned long user_addr;
461
462 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
463 PROT_READ | PROT_WRITE | PROT_EXEC,
464 MAP_ANONYMOUS | MAP_PRIVATE, 0);
465 if (user_addr >= TASK_SIZE) {
466 pr_warn("Failed to allocate user memory\n");
467 return;
468 }
469 execute_user_location((void *)user_addr);
470 vm_munmap(user_addr, PAGE_SIZE);
471 break;
472 }
473 case CT_ACCESS_USERSPACE: {
474 unsigned long user_addr, tmp;
475 unsigned long *ptr;
476
477 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
478 PROT_READ | PROT_WRITE | PROT_EXEC,
479 MAP_ANONYMOUS | MAP_PRIVATE, 0);
480 if (user_addr >= TASK_SIZE) {
481 pr_warn("Failed to allocate user memory\n");
482 return;
483 }
484
485 ptr = (unsigned long *)user_addr;
aac416fc
KC
486
487 pr_info("attempting bad read at %p\n", ptr);
9ae113ce
KC
488 tmp = *ptr;
489 tmp += 0xc0dec0de;
aac416fc
KC
490
491 pr_info("attempting bad write at %p\n", ptr);
9ae113ce
KC
492 *ptr = tmp;
493
494 vm_munmap(user_addr, PAGE_SIZE);
495
496 break;
497 }
498 case CT_WRITE_RO: {
499 unsigned long *ptr;
500
501 ptr = (unsigned long *)&rodata;
aac416fc
KC
502
503 pr_info("attempting bad write at %p\n", ptr);
9ae113ce
KC
504 *ptr ^= 0xabcd1234;
505
506 break;
507 }
dc2b9e90
KC
508 case CT_WRITE_KERN: {
509 size_t size;
510 unsigned char *ptr;
511
512 size = (unsigned long)do_overwritten -
513 (unsigned long)do_nothing;
514 ptr = (unsigned char *)do_overwritten;
515
516 pr_info("attempting bad %zu byte write at %p\n", size, ptr);
517 memcpy(ptr, (unsigned char *)do_nothing, size);
518 flush_icache_range((unsigned long)ptr,
519 (unsigned long)(ptr + size));
520
521 do_overwritten();
522 break;
523 }
93e2f585 524 case CT_NONE:
0347af4e
SK
525 default:
526 break;
527 }
528
529}
530
531static void lkdtm_handler(void)
532{
aa2c96d6 533 unsigned long flags;
92618184 534 bool do_it = false;
aa2c96d6
JH
535
536 spin_lock_irqsave(&count_lock, flags);
0347af4e 537 count--;
feac6e21
KC
538 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
539 cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
8bb31b9d
AG
540
541 if (count == 0) {
92618184 542 do_it = true;
8bb31b9d
AG
543 count = cpoint_count;
544 }
aa2c96d6 545 spin_unlock_irqrestore(&count_lock, flags);
92618184
CW
546
547 if (do_it)
548 lkdtm_do_action(cptype);
8bb31b9d
AG
549}
550
0347af4e 551static int lkdtm_register_cpoint(enum cname which)
8bb31b9d
AG
552{
553 int ret;
554
93e2f585 555 cpoint = CN_INVALID;
0347af4e
SK
556 if (lkdtm.entry != NULL)
557 unregister_jprobe(&lkdtm);
8bb31b9d 558
0347af4e 559 switch (which) {
93e2f585 560 case CN_DIRECT:
0347af4e
SK
561 lkdtm_do_action(cptype);
562 return 0;
93e2f585 563 case CN_INT_HARDWARE_ENTRY:
f58f2fa9 564 lkdtm.kp.symbol_name = "do_IRQ";
8bb31b9d
AG
565 lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
566 break;
93e2f585 567 case CN_INT_HW_IRQ_EN:
8bb31b9d
AG
568 lkdtm.kp.symbol_name = "handle_IRQ_event";
569 lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
570 break;
93e2f585 571 case CN_INT_TASKLET_ENTRY:
8bb31b9d
AG
572 lkdtm.kp.symbol_name = "tasklet_action";
573 lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
574 break;
93e2f585 575 case CN_FS_DEVRW:
8bb31b9d
AG
576 lkdtm.kp.symbol_name = "ll_rw_block";
577 lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
578 break;
93e2f585 579 case CN_MEM_SWAPOUT:
18a61e4a
AG
580 lkdtm.kp.symbol_name = "shrink_inactive_list";
581 lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
8bb31b9d 582 break;
93e2f585 583 case CN_TIMERADD:
8bb31b9d
AG
584 lkdtm.kp.symbol_name = "hrtimer_start";
585 lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
586 break;
93e2f585 587 case CN_SCSI_DISPATCH_CMD:
8bb31b9d
AG
588 lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
589 lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
590 break;
93e2f585 591 case CN_IDE_CORE_CP:
8bb31b9d
AG
592#ifdef CONFIG_IDE
593 lkdtm.kp.symbol_name = "generic_ide_ioctl";
594 lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
595#else
feac6e21 596 pr_info("Crash point not available\n");
0347af4e 597 return -EINVAL;
8bb31b9d
AG
598#endif
599 break;
600 default:
feac6e21 601 pr_info("Invalid Crash Point\n");
0347af4e 602 return -EINVAL;
8bb31b9d
AG
603 }
604
0347af4e 605 cpoint = which;
8bb31b9d 606 if ((ret = register_jprobe(&lkdtm)) < 0) {
feac6e21 607 pr_info("Couldn't register jprobe\n");
93e2f585 608 cpoint = CN_INVALID;
0347af4e
SK
609 }
610
611 return ret;
612}
613
614static ssize_t do_register_entry(enum cname which, struct file *f,
615 const char __user *user_buf, size_t count, loff_t *off)
616{
617 char *buf;
618 int err;
619
620 if (count >= PAGE_SIZE)
621 return -EINVAL;
622
623 buf = (char *)__get_free_page(GFP_KERNEL);
624 if (!buf)
625 return -ENOMEM;
626 if (copy_from_user(buf, user_buf, count)) {
627 free_page((unsigned long) buf);
628 return -EFAULT;
629 }
630 /* NULL-terminate and remove enter */
631 buf[count] = '\0';
632 strim(buf);
633
634 cptype = parse_cp_type(buf, count);
635 free_page((unsigned long) buf);
636
93e2f585 637 if (cptype == CT_NONE)
0347af4e
SK
638 return -EINVAL;
639
640 err = lkdtm_register_cpoint(which);
641 if (err < 0)
642 return err;
643
644 *off += count;
645
646 return count;
647}
648
649/* Generic read callback that just prints out the available crash types */
650static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
651 size_t count, loff_t *off)
652{
653 char *buf;
654 int i, n, out;
655
656 buf = (char *)__get_free_page(GFP_KERNEL);
086ff4b3
AC
657 if (buf == NULL)
658 return -ENOMEM;
0347af4e
SK
659
660 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
661 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
662 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
663 buf[n] = '\0';
664
665 out = simple_read_from_buffer(user_buf, count, off,
666 buf, n);
667 free_page((unsigned long) buf);
668
669 return out;
670}
671
672static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
673{
674 return 0;
675}
676
677
678static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
679 size_t count, loff_t *off)
680{
93e2f585 681 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
0347af4e
SK
682}
683
684static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
685 size_t count, loff_t *off)
686{
93e2f585 687 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
0347af4e
SK
688}
689
690static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
691 size_t count, loff_t *off)
692{
93e2f585 693 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
0347af4e
SK
694}
695
696static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
697 size_t count, loff_t *off)
698{
93e2f585 699 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
0347af4e
SK
700}
701
702static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
703 size_t count, loff_t *off)
704{
93e2f585 705 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
0347af4e
SK
706}
707
708static ssize_t timeradd_entry(struct file *f, const char __user *buf,
709 size_t count, loff_t *off)
710{
93e2f585 711 return do_register_entry(CN_TIMERADD, f, buf, count, off);
0347af4e
SK
712}
713
714static ssize_t scsi_dispatch_cmd_entry(struct file *f,
715 const char __user *buf, size_t count, loff_t *off)
716{
93e2f585 717 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
0347af4e
SK
718}
719
720static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
721 size_t count, loff_t *off)
722{
93e2f585 723 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
0347af4e
SK
724}
725
726/* Special entry to just crash directly. Available without KPROBEs */
727static ssize_t direct_entry(struct file *f, const char __user *user_buf,
728 size_t count, loff_t *off)
729{
730 enum ctype type;
731 char *buf;
732
733 if (count >= PAGE_SIZE)
734 return -EINVAL;
735 if (count < 1)
736 return -EINVAL;
737
738 buf = (char *)__get_free_page(GFP_KERNEL);
739 if (!buf)
740 return -ENOMEM;
741 if (copy_from_user(buf, user_buf, count)) {
742 free_page((unsigned long) buf);
743 return -EFAULT;
744 }
745 /* NULL-terminate and remove enter */
746 buf[count] = '\0';
747 strim(buf);
748
749 type = parse_cp_type(buf, count);
750 free_page((unsigned long) buf);
93e2f585 751 if (type == CT_NONE)
0347af4e
SK
752 return -EINVAL;
753
feac6e21 754 pr_info("Performing direct entry %s\n", cp_type_to_str(type));
0347af4e
SK
755 lkdtm_do_action(type);
756 *off += count;
757
758 return count;
759}
760
761struct crash_entry {
762 const char *name;
763 const struct file_operations fops;
764};
765
766static const struct crash_entry crash_entries[] = {
767 {"DIRECT", {.read = lkdtm_debugfs_read,
05271ec4 768 .llseek = generic_file_llseek,
0347af4e
SK
769 .open = lkdtm_debugfs_open,
770 .write = direct_entry} },
771 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
05271ec4 772 .llseek = generic_file_llseek,
0347af4e
SK
773 .open = lkdtm_debugfs_open,
774 .write = int_hardware_entry} },
775 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
05271ec4 776 .llseek = generic_file_llseek,
0347af4e
SK
777 .open = lkdtm_debugfs_open,
778 .write = int_hw_irq_en} },
779 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
05271ec4 780 .llseek = generic_file_llseek,
0347af4e
SK
781 .open = lkdtm_debugfs_open,
782 .write = int_tasklet_entry} },
783 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
05271ec4 784 .llseek = generic_file_llseek,
0347af4e
SK
785 .open = lkdtm_debugfs_open,
786 .write = fs_devrw_entry} },
787 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
05271ec4 788 .llseek = generic_file_llseek,
0347af4e
SK
789 .open = lkdtm_debugfs_open,
790 .write = mem_swapout_entry} },
791 {"TIMERADD", {.read = lkdtm_debugfs_read,
05271ec4 792 .llseek = generic_file_llseek,
0347af4e
SK
793 .open = lkdtm_debugfs_open,
794 .write = timeradd_entry} },
795 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
05271ec4 796 .llseek = generic_file_llseek,
0347af4e
SK
797 .open = lkdtm_debugfs_open,
798 .write = scsi_dispatch_cmd_entry} },
799 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
05271ec4 800 .llseek = generic_file_llseek,
0347af4e
SK
801 .open = lkdtm_debugfs_open,
802 .write = ide_core_cp_entry} },
803};
804
805static struct dentry *lkdtm_debugfs_root;
806
807static int __init lkdtm_module_init(void)
808{
809 int ret = -EINVAL;
810 int n_debugfs_entries = 1; /* Assume only the direct entry */
811 int i;
812
813 /* Register debugfs interface */
814 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
815 if (!lkdtm_debugfs_root) {
feac6e21 816 pr_err("creating root dir failed\n");
0347af4e
SK
817 return -ENODEV;
818 }
819
820#ifdef CONFIG_KPROBES
821 n_debugfs_entries = ARRAY_SIZE(crash_entries);
822#endif
823
824 for (i = 0; i < n_debugfs_entries; i++) {
825 const struct crash_entry *cur = &crash_entries[i];
826 struct dentry *de;
827
828 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
829 NULL, &cur->fops);
830 if (de == NULL) {
feac6e21 831 pr_err("could not create %s\n", cur->name);
0347af4e
SK
832 goto out_err;
833 }
834 }
835
836 if (lkdtm_parse_commandline() == -EINVAL) {
feac6e21 837 pr_info("Invalid command\n");
0347af4e
SK
838 goto out_err;
839 }
840
93e2f585 841 if (cpoint != CN_INVALID && cptype != CT_NONE) {
0347af4e
SK
842 ret = lkdtm_register_cpoint(cpoint);
843 if (ret < 0) {
feac6e21 844 pr_info("Invalid crash point %d\n", cpoint);
0347af4e
SK
845 goto out_err;
846 }
feac6e21
KC
847 pr_info("Crash point %s of type %s registered\n",
848 cpoint_name, cpoint_type);
0347af4e 849 } else {
feac6e21 850 pr_info("No crash points registered, enable through debugfs\n");
8bb31b9d
AG
851 }
852
8bb31b9d 853 return 0;
0347af4e
SK
854
855out_err:
856 debugfs_remove_recursive(lkdtm_debugfs_root);
857 return ret;
8bb31b9d
AG
858}
859
2118116e 860static void __exit lkdtm_module_exit(void)
8bb31b9d 861{
0347af4e
SK
862 debugfs_remove_recursive(lkdtm_debugfs_root);
863
864 unregister_jprobe(&lkdtm);
feac6e21 865 pr_info("Crash point unregistered\n");
8bb31b9d
AG
866}
867
868module_init(lkdtm_module_init);
869module_exit(lkdtm_module_exit);
870
871MODULE_LICENSE("GPL");
This page took 0.668803 seconds and 5 git commands to generate.