lkdtm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. *
  29. * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
  30. *
  31. * See Documentation/fault-injection/provoke-crashes.txt for instructions
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/fs.h>
  35. #include <linux/module.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/kprobes.h>
  38. #include <linux/list.h>
  39. #include <linux/init.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/hrtimer.h>
  42. #include <linux/slab.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/mman.h>
  47. #ifdef CONFIG_IDE
  48. #include <linux/ide.h>
  49. #endif
  50. /*
  51. * Make sure our attempts to over run the kernel stack doesn't trigger
  52. * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
  53. * recurse past the end of THREAD_SIZE by default.
  54. */
  55. #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
  56. #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
  57. #else
  58. #define REC_STACK_SIZE (THREAD_SIZE / 8)
  59. #endif
  60. #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
  61. #define DEFAULT_COUNT 10
  62. #define EXEC_SIZE 64
  63. enum cname {
  64. CN_INVALID,
  65. CN_INT_HARDWARE_ENTRY,
  66. CN_INT_HW_IRQ_EN,
  67. CN_INT_TASKLET_ENTRY,
  68. CN_FS_DEVRW,
  69. CN_MEM_SWAPOUT,
  70. CN_TIMERADD,
  71. CN_SCSI_DISPATCH_CMD,
  72. CN_IDE_CORE_CP,
  73. CN_DIRECT,
  74. };
  75. enum ctype {
  76. CT_NONE,
  77. CT_PANIC,
  78. CT_BUG,
  79. CT_WARNING,
  80. CT_EXCEPTION,
  81. CT_LOOP,
  82. CT_OVERFLOW,
  83. CT_CORRUPT_STACK,
  84. CT_UNALIGNED_LOAD_STORE_WRITE,
  85. CT_OVERWRITE_ALLOCATION,
  86. CT_WRITE_AFTER_FREE,
  87. CT_SOFTLOCKUP,
  88. CT_HARDLOCKUP,
  89. CT_SPINLOCKUP,
  90. CT_HUNG_TASK,
  91. CT_EXEC_DATA,
  92. CT_EXEC_STACK,
  93. CT_EXEC_KMALLOC,
  94. CT_EXEC_VMALLOC,
  95. CT_EXEC_USERSPACE,
  96. CT_ACCESS_USERSPACE,
  97. CT_WRITE_RO,
  98. };
  99. static char* cp_name[] = {
  100. "INT_HARDWARE_ENTRY",
  101. "INT_HW_IRQ_EN",
  102. "INT_TASKLET_ENTRY",
  103. "FS_DEVRW",
  104. "MEM_SWAPOUT",
  105. "TIMERADD",
  106. "SCSI_DISPATCH_CMD",
  107. "IDE_CORE_CP",
  108. "DIRECT",
  109. };
  110. static char* cp_type[] = {
  111. "PANIC",
  112. "BUG",
  113. "WARNING",
  114. "EXCEPTION",
  115. "LOOP",
  116. "OVERFLOW",
  117. "CORRUPT_STACK",
  118. "UNALIGNED_LOAD_STORE_WRITE",
  119. "OVERWRITE_ALLOCATION",
  120. "WRITE_AFTER_FREE",
  121. "SOFTLOCKUP",
  122. "HARDLOCKUP",
  123. "SPINLOCKUP",
  124. "HUNG_TASK",
  125. "EXEC_DATA",
  126. "EXEC_STACK",
  127. "EXEC_KMALLOC",
  128. "EXEC_VMALLOC",
  129. "EXEC_USERSPACE",
  130. "ACCESS_USERSPACE",
  131. "WRITE_RO",
  132. };
  133. static struct jprobe lkdtm;
  134. static int lkdtm_parse_commandline(void);
  135. static void lkdtm_handler(void);
  136. static char* cpoint_name;
  137. static char* cpoint_type;
  138. static int cpoint_count = DEFAULT_COUNT;
  139. static int recur_count = REC_NUM_DEFAULT;
  140. static enum cname cpoint = CN_INVALID;
  141. static enum ctype cptype = CT_NONE;
  142. static int count = DEFAULT_COUNT;
  143. static DEFINE_SPINLOCK(count_lock);
  144. static DEFINE_SPINLOCK(lock_me_up);
  145. static u8 data_area[EXEC_SIZE];
  146. static const unsigned long rodata = 0xAA55AA55;
  147. module_param(recur_count, int, 0644);
  148. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  149. module_param(cpoint_name, charp, 0444);
  150. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  151. module_param(cpoint_type, charp, 0444);
  152. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  153. "hitting the crash point");
  154. module_param(cpoint_count, int, 0644);
  155. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  156. "crash point is to be hit to trigger action");
  157. static unsigned int jp_do_irq(unsigned int irq)
  158. {
  159. lkdtm_handler();
  160. jprobe_return();
  161. return 0;
  162. }
  163. static irqreturn_t jp_handle_irq_event(unsigned int irq,
  164. struct irqaction *action)
  165. {
  166. lkdtm_handler();
  167. jprobe_return();
  168. return 0;
  169. }
  170. static void jp_tasklet_action(struct softirq_action *a)
  171. {
  172. lkdtm_handler();
  173. jprobe_return();
  174. }
  175. static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  176. {
  177. lkdtm_handler();
  178. jprobe_return();
  179. }
  180. struct scan_control;
  181. static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
  182. struct zone *zone,
  183. struct scan_control *sc)
  184. {
  185. lkdtm_handler();
  186. jprobe_return();
  187. return 0;
  188. }
  189. static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
  190. const enum hrtimer_mode mode)
  191. {
  192. lkdtm_handler();
  193. jprobe_return();
  194. return 0;
  195. }
  196. static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  197. {
  198. lkdtm_handler();
  199. jprobe_return();
  200. return 0;
  201. }
  202. #ifdef CONFIG_IDE
  203. int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  204. struct block_device *bdev, unsigned int cmd,
  205. unsigned long arg)
  206. {
  207. lkdtm_handler();
  208. jprobe_return();
  209. return 0;
  210. }
  211. #endif
  212. /* Return the crashpoint number or NONE if the name is invalid */
  213. static enum ctype parse_cp_type(const char *what, size_t count)
  214. {
  215. int i;
  216. for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
  217. if (!strcmp(what, cp_type[i]))
  218. return i + 1;
  219. }
  220. return CT_NONE;
  221. }
  222. static const char *cp_type_to_str(enum ctype type)
  223. {
  224. if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
  225. return "None";
  226. return cp_type[type - 1];
  227. }
  228. static const char *cp_name_to_str(enum cname name)
  229. {
  230. if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
  231. return "INVALID";
  232. return cp_name[name - 1];
  233. }
  234. static int lkdtm_parse_commandline(void)
  235. {
  236. int i;
  237. unsigned long flags;
  238. if (cpoint_count < 1 || recur_count < 1)
  239. return -EINVAL;
  240. spin_lock_irqsave(&count_lock, flags);
  241. count = cpoint_count;
  242. spin_unlock_irqrestore(&count_lock, flags);
  243. /* No special parameters */
  244. if (!cpoint_type && !cpoint_name)
  245. return 0;
  246. /* Neither or both of these need to be set */
  247. if (!cpoint_type || !cpoint_name)
  248. return -EINVAL;
  249. cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
  250. if (cptype == CT_NONE)
  251. return -EINVAL;
  252. for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
  253. if (!strcmp(cpoint_name, cp_name[i])) {
  254. cpoint = i + 1;
  255. return 0;
  256. }
  257. }
  258. /* Could not find a valid crash point */
  259. return -EINVAL;
  260. }
  261. static int recursive_loop(int remaining)
  262. {
  263. char buf[REC_STACK_SIZE];
  264. /* Make sure compiler does not optimize this away. */
  265. memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
  266. if (!remaining)
  267. return 0;
  268. else
  269. return recursive_loop(remaining - 1);
  270. }
  271. static void do_nothing(void)
  272. {
  273. return;
  274. }
  275. static noinline void corrupt_stack(void)
  276. {
  277. /* Use default char array length that triggers stack protection. */
  278. char data[8];
  279. memset((void *)data, 0, 64);
  280. }
  281. static void execute_location(void *dst)
  282. {
  283. void (*func)(void) = dst;
  284. memcpy(dst, do_nothing, EXEC_SIZE);
  285. func();
  286. }
  287. static void execute_user_location(void *dst)
  288. {
  289. void (*func)(void) = dst;
  290. if (copy_to_user(dst, do_nothing, EXEC_SIZE))
  291. return;
  292. func();
  293. }
  294. static void lkdtm_do_action(enum ctype which)
  295. {
  296. switch (which) {
  297. case CT_PANIC:
  298. panic("dumptest");
  299. break;
  300. case CT_BUG:
  301. BUG();
  302. break;
  303. case CT_WARNING:
  304. WARN_ON(1);
  305. break;
  306. case CT_EXCEPTION:
  307. *((int *) 0) = 0;
  308. break;
  309. case CT_LOOP:
  310. for (;;)
  311. ;
  312. break;
  313. case CT_OVERFLOW:
  314. (void) recursive_loop(recur_count);
  315. break;
  316. case CT_CORRUPT_STACK:
  317. corrupt_stack();
  318. break;
  319. case CT_UNALIGNED_LOAD_STORE_WRITE: {
  320. static u8 data[5] __attribute__((aligned(4))) = {1, 2,
  321. 3, 4, 5};
  322. u32 *p;
  323. u32 val = 0x12345678;
  324. p = (u32 *)(data + 1);
  325. if (*p == 0)
  326. val = 0x87654321;
  327. *p = val;
  328. break;
  329. }
  330. case CT_OVERWRITE_ALLOCATION: {
  331. size_t len = 1020;
  332. u32 *data = kmalloc(len, GFP_KERNEL);
  333. data[1024 / sizeof(u32)] = 0x12345678;
  334. kfree(data);
  335. break;
  336. }
  337. case CT_WRITE_AFTER_FREE: {
  338. size_t len = 1024;
  339. u32 *data = kmalloc(len, GFP_KERNEL);
  340. kfree(data);
  341. schedule();
  342. memset(data, 0x78, len);
  343. break;
  344. }
  345. case CT_SOFTLOCKUP:
  346. preempt_disable();
  347. for (;;)
  348. cpu_relax();
  349. break;
  350. case CT_HARDLOCKUP:
  351. local_irq_disable();
  352. for (;;)
  353. cpu_relax();
  354. break;
  355. case CT_SPINLOCKUP:
  356. /* Must be called twice to trigger. */
  357. spin_lock(&lock_me_up);
  358. break;
  359. case CT_HUNG_TASK:
  360. set_current_state(TASK_UNINTERRUPTIBLE);
  361. schedule();
  362. break;
  363. case CT_EXEC_DATA:
  364. execute_location(data_area);
  365. break;
  366. case CT_EXEC_STACK: {
  367. u8 stack_area[EXEC_SIZE];
  368. execute_location(stack_area);
  369. break;
  370. }
  371. case CT_EXEC_KMALLOC: {
  372. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  373. execute_location(kmalloc_area);
  374. kfree(kmalloc_area);
  375. break;
  376. }
  377. case CT_EXEC_VMALLOC: {
  378. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  379. execute_location(vmalloc_area);
  380. vfree(vmalloc_area);
  381. break;
  382. }
  383. case CT_EXEC_USERSPACE: {
  384. unsigned long user_addr;
  385. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  386. PROT_READ | PROT_WRITE | PROT_EXEC,
  387. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  388. if (user_addr >= TASK_SIZE) {
  389. pr_warn("Failed to allocate user memory\n");
  390. return;
  391. }
  392. execute_user_location((void *)user_addr);
  393. vm_munmap(user_addr, PAGE_SIZE);
  394. break;
  395. }
  396. case CT_ACCESS_USERSPACE: {
  397. unsigned long user_addr, tmp;
  398. unsigned long *ptr;
  399. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  400. PROT_READ | PROT_WRITE | PROT_EXEC,
  401. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  402. if (user_addr >= TASK_SIZE) {
  403. pr_warn("Failed to allocate user memory\n");
  404. return;
  405. }
  406. ptr = (unsigned long *)user_addr;
  407. tmp = *ptr;
  408. tmp += 0xc0dec0de;
  409. *ptr = tmp;
  410. vm_munmap(user_addr, PAGE_SIZE);
  411. break;
  412. }
  413. case CT_WRITE_RO: {
  414. unsigned long *ptr;
  415. ptr = (unsigned long *)&rodata;
  416. *ptr ^= 0xabcd1234;
  417. break;
  418. }
  419. case CT_NONE:
  420. default:
  421. break;
  422. }
  423. }
  424. static void lkdtm_handler(void)
  425. {
  426. unsigned long flags;
  427. bool do_it = false;
  428. spin_lock_irqsave(&count_lock, flags);
  429. count--;
  430. printk(KERN_INFO "lkdtm: Crash point %s of type %s hit, trigger in %d rounds\n",
  431. cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
  432. if (count == 0) {
  433. do_it = true;
  434. count = cpoint_count;
  435. }
  436. spin_unlock_irqrestore(&count_lock, flags);
  437. if (do_it)
  438. lkdtm_do_action(cptype);
  439. }
  440. static int lkdtm_register_cpoint(enum cname which)
  441. {
  442. int ret;
  443. cpoint = CN_INVALID;
  444. if (lkdtm.entry != NULL)
  445. unregister_jprobe(&lkdtm);
  446. switch (which) {
  447. case CN_DIRECT:
  448. lkdtm_do_action(cptype);
  449. return 0;
  450. case CN_INT_HARDWARE_ENTRY:
  451. lkdtm.kp.symbol_name = "do_IRQ";
  452. lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
  453. break;
  454. case CN_INT_HW_IRQ_EN:
  455. lkdtm.kp.symbol_name = "handle_IRQ_event";
  456. lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
  457. break;
  458. case CN_INT_TASKLET_ENTRY:
  459. lkdtm.kp.symbol_name = "tasklet_action";
  460. lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
  461. break;
  462. case CN_FS_DEVRW:
  463. lkdtm.kp.symbol_name = "ll_rw_block";
  464. lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
  465. break;
  466. case CN_MEM_SWAPOUT:
  467. lkdtm.kp.symbol_name = "shrink_inactive_list";
  468. lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
  469. break;
  470. case CN_TIMERADD:
  471. lkdtm.kp.symbol_name = "hrtimer_start";
  472. lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
  473. break;
  474. case CN_SCSI_DISPATCH_CMD:
  475. lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
  476. lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
  477. break;
  478. case CN_IDE_CORE_CP:
  479. #ifdef CONFIG_IDE
  480. lkdtm.kp.symbol_name = "generic_ide_ioctl";
  481. lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
  482. #else
  483. printk(KERN_INFO "lkdtm: Crash point not available\n");
  484. return -EINVAL;
  485. #endif
  486. break;
  487. default:
  488. printk(KERN_INFO "lkdtm: Invalid Crash Point\n");
  489. return -EINVAL;
  490. }
  491. cpoint = which;
  492. if ((ret = register_jprobe(&lkdtm)) < 0) {
  493. printk(KERN_INFO "lkdtm: Couldn't register jprobe\n");
  494. cpoint = CN_INVALID;
  495. }
  496. return ret;
  497. }
  498. static ssize_t do_register_entry(enum cname which, struct file *f,
  499. const char __user *user_buf, size_t count, loff_t *off)
  500. {
  501. char *buf;
  502. int err;
  503. if (count >= PAGE_SIZE)
  504. return -EINVAL;
  505. buf = (char *)__get_free_page(GFP_KERNEL);
  506. if (!buf)
  507. return -ENOMEM;
  508. if (copy_from_user(buf, user_buf, count)) {
  509. free_page((unsigned long) buf);
  510. return -EFAULT;
  511. }
  512. /* NULL-terminate and remove enter */
  513. buf[count] = '\0';
  514. strim(buf);
  515. cptype = parse_cp_type(buf, count);
  516. free_page((unsigned long) buf);
  517. if (cptype == CT_NONE)
  518. return -EINVAL;
  519. err = lkdtm_register_cpoint(which);
  520. if (err < 0)
  521. return err;
  522. *off += count;
  523. return count;
  524. }
  525. /* Generic read callback that just prints out the available crash types */
  526. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  527. size_t count, loff_t *off)
  528. {
  529. char *buf;
  530. int i, n, out;
  531. buf = (char *)__get_free_page(GFP_KERNEL);
  532. if (buf == NULL)
  533. return -ENOMEM;
  534. n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
  535. for (i = 0; i < ARRAY_SIZE(cp_type); i++)
  536. n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
  537. buf[n] = '\0';
  538. out = simple_read_from_buffer(user_buf, count, off,
  539. buf, n);
  540. free_page((unsigned long) buf);
  541. return out;
  542. }
  543. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  544. {
  545. return 0;
  546. }
  547. static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
  548. size_t count, loff_t *off)
  549. {
  550. return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
  551. }
  552. static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
  553. size_t count, loff_t *off)
  554. {
  555. return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
  556. }
  557. static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
  558. size_t count, loff_t *off)
  559. {
  560. return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
  561. }
  562. static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
  563. size_t count, loff_t *off)
  564. {
  565. return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
  566. }
  567. static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
  568. size_t count, loff_t *off)
  569. {
  570. return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
  571. }
  572. static ssize_t timeradd_entry(struct file *f, const char __user *buf,
  573. size_t count, loff_t *off)
  574. {
  575. return do_register_entry(CN_TIMERADD, f, buf, count, off);
  576. }
  577. static ssize_t scsi_dispatch_cmd_entry(struct file *f,
  578. const char __user *buf, size_t count, loff_t *off)
  579. {
  580. return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
  581. }
  582. static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
  583. size_t count, loff_t *off)
  584. {
  585. return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
  586. }
  587. /* Special entry to just crash directly. Available without KPROBEs */
  588. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  589. size_t count, loff_t *off)
  590. {
  591. enum ctype type;
  592. char *buf;
  593. if (count >= PAGE_SIZE)
  594. return -EINVAL;
  595. if (count < 1)
  596. return -EINVAL;
  597. buf = (char *)__get_free_page(GFP_KERNEL);
  598. if (!buf)
  599. return -ENOMEM;
  600. if (copy_from_user(buf, user_buf, count)) {
  601. free_page((unsigned long) buf);
  602. return -EFAULT;
  603. }
  604. /* NULL-terminate and remove enter */
  605. buf[count] = '\0';
  606. strim(buf);
  607. type = parse_cp_type(buf, count);
  608. free_page((unsigned long) buf);
  609. if (type == CT_NONE)
  610. return -EINVAL;
  611. printk(KERN_INFO "lkdtm: Performing direct entry %s\n",
  612. cp_type_to_str(type));
  613. lkdtm_do_action(type);
  614. *off += count;
  615. return count;
  616. }
  617. struct crash_entry {
  618. const char *name;
  619. const struct file_operations fops;
  620. };
  621. static const struct crash_entry crash_entries[] = {
  622. {"DIRECT", {.read = lkdtm_debugfs_read,
  623. .llseek = generic_file_llseek,
  624. .open = lkdtm_debugfs_open,
  625. .write = direct_entry} },
  626. {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
  627. .llseek = generic_file_llseek,
  628. .open = lkdtm_debugfs_open,
  629. .write = int_hardware_entry} },
  630. {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
  631. .llseek = generic_file_llseek,
  632. .open = lkdtm_debugfs_open,
  633. .write = int_hw_irq_en} },
  634. {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
  635. .llseek = generic_file_llseek,
  636. .open = lkdtm_debugfs_open,
  637. .write = int_tasklet_entry} },
  638. {"FS_DEVRW", {.read = lkdtm_debugfs_read,
  639. .llseek = generic_file_llseek,
  640. .open = lkdtm_debugfs_open,
  641. .write = fs_devrw_entry} },
  642. {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
  643. .llseek = generic_file_llseek,
  644. .open = lkdtm_debugfs_open,
  645. .write = mem_swapout_entry} },
  646. {"TIMERADD", {.read = lkdtm_debugfs_read,
  647. .llseek = generic_file_llseek,
  648. .open = lkdtm_debugfs_open,
  649. .write = timeradd_entry} },
  650. {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
  651. .llseek = generic_file_llseek,
  652. .open = lkdtm_debugfs_open,
  653. .write = scsi_dispatch_cmd_entry} },
  654. {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
  655. .llseek = generic_file_llseek,
  656. .open = lkdtm_debugfs_open,
  657. .write = ide_core_cp_entry} },
  658. };
  659. static struct dentry *lkdtm_debugfs_root;
  660. static int __init lkdtm_module_init(void)
  661. {
  662. int ret = -EINVAL;
  663. int n_debugfs_entries = 1; /* Assume only the direct entry */
  664. int i;
  665. /* Register debugfs interface */
  666. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  667. if (!lkdtm_debugfs_root) {
  668. printk(KERN_ERR "lkdtm: creating root dir failed\n");
  669. return -ENODEV;
  670. }
  671. #ifdef CONFIG_KPROBES
  672. n_debugfs_entries = ARRAY_SIZE(crash_entries);
  673. #endif
  674. for (i = 0; i < n_debugfs_entries; i++) {
  675. const struct crash_entry *cur = &crash_entries[i];
  676. struct dentry *de;
  677. de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
  678. NULL, &cur->fops);
  679. if (de == NULL) {
  680. printk(KERN_ERR "lkdtm: could not create %s\n",
  681. cur->name);
  682. goto out_err;
  683. }
  684. }
  685. if (lkdtm_parse_commandline() == -EINVAL) {
  686. printk(KERN_INFO "lkdtm: Invalid command\n");
  687. goto out_err;
  688. }
  689. if (cpoint != CN_INVALID && cptype != CT_NONE) {
  690. ret = lkdtm_register_cpoint(cpoint);
  691. if (ret < 0) {
  692. printk(KERN_INFO "lkdtm: Invalid crash point %d\n",
  693. cpoint);
  694. goto out_err;
  695. }
  696. printk(KERN_INFO "lkdtm: Crash point %s of type %s registered\n",
  697. cpoint_name, cpoint_type);
  698. } else {
  699. printk(KERN_INFO "lkdtm: No crash points registered, enable through debugfs\n");
  700. }
  701. return 0;
  702. out_err:
  703. debugfs_remove_recursive(lkdtm_debugfs_root);
  704. return ret;
  705. }
  706. static void __exit lkdtm_module_exit(void)
  707. {
  708. debugfs_remove_recursive(lkdtm_debugfs_root);
  709. unregister_jprobe(&lkdtm);
  710. printk(KERN_INFO "lkdtm: Crash point unregistered\n");
  711. }
  712. module_init(lkdtm_module_init);
  713. module_exit(lkdtm_module_exit);
  714. MODULE_LICENSE("GPL");