/drivers/misc/kgdbts.c

http://github.com/mirrors/linux · C · 1180 lines · 859 code · 124 blank · 197 comment · 151 complexity · edace9ab63f149589e1ca3b4af8e670d MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kgdbts is a test suite for kgdb for the sole purpose of validating
  4. * that key pieces of the kgdb internals are working properly such as
  5. * HW/SW breakpoints, single stepping, and NMI.
  6. *
  7. * Created by: Jason Wessel <jason.wessel@windriver.com>
  8. *
  9. * Copyright (c) 2008 Wind River Systems, Inc.
  10. */
  11. /* Information about the kgdb test suite.
  12. * -------------------------------------
  13. *
  14. * The kgdb test suite is designed as a KGDB I/O module which
  15. * simulates the communications that a debugger would have with kgdb.
  16. * The tests are broken up in to a line by line and referenced here as
  17. * a "get" which is kgdb requesting input and "put" which is kgdb
  18. * sending a response.
  19. *
  20. * The kgdb suite can be invoked from the kernel command line
  21. * arguments system or executed dynamically at run time. The test
  22. * suite uses the variable "kgdbts" to obtain the information about
  23. * which tests to run and to configure the verbosity level. The
  24. * following are the various characters you can use with the kgdbts=
  25. * line:
  26. *
  27. * When using the "kgdbts=" you only choose one of the following core
  28. * test types:
  29. * A = Run all the core tests silently
  30. * V1 = Run all the core tests with minimal output
  31. * V2 = Run all the core tests in debug mode
  32. *
  33. * You can also specify optional tests:
  34. * N## = Go to sleep with interrupts of for ## seconds
  35. * to test the HW NMI watchdog
  36. * F## = Break at do_fork for ## iterations
  37. * S## = Break at sys_open for ## iterations
  38. * I## = Run the single step test ## iterations
  39. *
  40. * NOTE: that the do_fork and sys_open tests are mutually exclusive.
  41. *
  42. * To invoke the kgdb test suite from boot you use a kernel start
  43. * argument as follows:
  44. * kgdbts=V1 kgdbwait
  45. * Or if you wanted to perform the NMI test for 6 seconds and do_fork
  46. * test for 100 forks, you could use:
  47. * kgdbts=V1N6F100 kgdbwait
  48. *
  49. * The test suite can also be invoked at run time with:
  50. * echo kgdbts=V1N6F100 > /sys/module/kgdbts/parameters/kgdbts
  51. * Or as another example:
  52. * echo kgdbts=V2 > /sys/module/kgdbts/parameters/kgdbts
  53. *
  54. * When developing a new kgdb arch specific implementation or
  55. * using these tests for the purpose of regression testing,
  56. * several invocations are required.
  57. *
  58. * 1) Boot with the test suite enabled by using the kernel arguments
  59. * "kgdbts=V1F100 kgdbwait"
  60. * ## If kgdb arch specific implementation has NMI use
  61. * "kgdbts=V1N6F100
  62. *
  63. * 2) After the system boot run the basic test.
  64. * echo kgdbts=V1 > /sys/module/kgdbts/parameters/kgdbts
  65. *
  66. * 3) Run the concurrency tests. It is best to use n+1
  67. * while loops where n is the number of cpus you have
  68. * in your system. The example below uses only two
  69. * loops.
  70. *
  71. * ## This tests break points on sys_open
  72. * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
  73. * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
  74. * echo kgdbts=V1S10000 > /sys/module/kgdbts/parameters/kgdbts
  75. * fg # and hit control-c
  76. * fg # and hit control-c
  77. * ## This tests break points on do_fork
  78. * while [ 1 ] ; do date > /dev/null ; done &
  79. * while [ 1 ] ; do date > /dev/null ; done &
  80. * echo kgdbts=V1F1000 > /sys/module/kgdbts/parameters/kgdbts
  81. * fg # and hit control-c
  82. *
  83. */
  84. #include <linux/kernel.h>
  85. #include <linux/kgdb.h>
  86. #include <linux/ctype.h>
  87. #include <linux/uaccess.h>
  88. #include <linux/syscalls.h>
  89. #include <linux/nmi.h>
  90. #include <linux/delay.h>
  91. #include <linux/kthread.h>
  92. #include <linux/module.h>
  93. #include <linux/sched/task.h>
  94. #include <asm/sections.h>
  95. #define v1printk(a...) do { \
  96. if (verbose) \
  97. printk(KERN_INFO a); \
  98. } while (0)
  99. #define v2printk(a...) do { \
  100. if (verbose > 1) \
  101. printk(KERN_INFO a); \
  102. touch_nmi_watchdog(); \
  103. } while (0)
  104. #define eprintk(a...) do { \
  105. printk(KERN_ERR a); \
  106. WARN_ON(1); \
  107. } while (0)
  108. #define MAX_CONFIG_LEN 40
  109. static struct kgdb_io kgdbts_io_ops;
  110. static char get_buf[BUFMAX];
  111. static int get_buf_cnt;
  112. static char put_buf[BUFMAX];
  113. static int put_buf_cnt;
  114. static char scratch_buf[BUFMAX];
  115. static int verbose;
  116. static int repeat_test;
  117. static int test_complete;
  118. static int send_ack;
  119. static int final_ack;
  120. static int force_hwbrks;
  121. static int hwbreaks_ok;
  122. static int hw_break_val;
  123. static int hw_break_val2;
  124. static int cont_instead_of_sstep;
  125. static unsigned long cont_thread_id;
  126. static unsigned long sstep_thread_id;
  127. #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_SPARC)
  128. static int arch_needs_sstep_emulation = 1;
  129. #else
  130. static int arch_needs_sstep_emulation;
  131. #endif
  132. static unsigned long cont_addr;
  133. static unsigned long sstep_addr;
  134. static int restart_from_top_after_write;
  135. static int sstep_state;
  136. /* Storage for the registers, in GDB format. */
  137. static unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
  138. sizeof(unsigned long) - 1) /
  139. sizeof(unsigned long)];
  140. static struct pt_regs kgdbts_regs;
  141. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  142. static int configured = -1;
  143. #ifdef CONFIG_KGDB_TESTS_BOOT_STRING
  144. static char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
  145. #else
  146. static char config[MAX_CONFIG_LEN];
  147. #endif
  148. static struct kparam_string kps = {
  149. .string = config,
  150. .maxlen = MAX_CONFIG_LEN,
  151. };
  152. static void fill_get_buf(char *buf);
  153. struct test_struct {
  154. char *get;
  155. char *put;
  156. void (*get_handler)(char *);
  157. int (*put_handler)(char *, char *);
  158. };
  159. struct test_state {
  160. char *name;
  161. struct test_struct *tst;
  162. int idx;
  163. int (*run_test) (int, int);
  164. int (*validate_put) (char *);
  165. };
  166. static struct test_state ts;
  167. static int kgdbts_unreg_thread(void *ptr)
  168. {
  169. /* Wait until the tests are complete and then ungresiter the I/O
  170. * driver.
  171. */
  172. while (!final_ack)
  173. msleep_interruptible(1500);
  174. /* Pause for any other threads to exit after final ack. */
  175. msleep_interruptible(1000);
  176. if (configured)
  177. kgdb_unregister_io_module(&kgdbts_io_ops);
  178. configured = 0;
  179. return 0;
  180. }
  181. /* This is noinline such that it can be used for a single location to
  182. * place a breakpoint
  183. */
  184. static noinline void kgdbts_break_test(void)
  185. {
  186. v2printk("kgdbts: breakpoint complete\n");
  187. }
  188. /* Lookup symbol info in the kernel */
  189. static unsigned long lookup_addr(char *arg)
  190. {
  191. unsigned long addr = 0;
  192. if (!strcmp(arg, "kgdbts_break_test"))
  193. addr = (unsigned long)kgdbts_break_test;
  194. else if (!strcmp(arg, "sys_open"))
  195. addr = (unsigned long)do_sys_open;
  196. else if (!strcmp(arg, "do_fork"))
  197. addr = (unsigned long)_do_fork;
  198. else if (!strcmp(arg, "hw_break_val"))
  199. addr = (unsigned long)&hw_break_val;
  200. addr = (unsigned long) dereference_function_descriptor((void *)addr);
  201. return addr;
  202. }
  203. static void break_helper(char *bp_type, char *arg, unsigned long vaddr)
  204. {
  205. unsigned long addr;
  206. if (arg)
  207. addr = lookup_addr(arg);
  208. else
  209. addr = vaddr;
  210. sprintf(scratch_buf, "%s,%lx,%i", bp_type, addr,
  211. BREAK_INSTR_SIZE);
  212. fill_get_buf(scratch_buf);
  213. }
  214. static void sw_break(char *arg)
  215. {
  216. break_helper(force_hwbrks ? "Z1" : "Z0", arg, 0);
  217. }
  218. static void sw_rem_break(char *arg)
  219. {
  220. break_helper(force_hwbrks ? "z1" : "z0", arg, 0);
  221. }
  222. static void hw_break(char *arg)
  223. {
  224. break_helper("Z1", arg, 0);
  225. }
  226. static void hw_rem_break(char *arg)
  227. {
  228. break_helper("z1", arg, 0);
  229. }
  230. static void hw_write_break(char *arg)
  231. {
  232. break_helper("Z2", arg, 0);
  233. }
  234. static void hw_rem_write_break(char *arg)
  235. {
  236. break_helper("z2", arg, 0);
  237. }
  238. static void hw_access_break(char *arg)
  239. {
  240. break_helper("Z4", arg, 0);
  241. }
  242. static void hw_rem_access_break(char *arg)
  243. {
  244. break_helper("z4", arg, 0);
  245. }
  246. static void hw_break_val_access(void)
  247. {
  248. hw_break_val2 = hw_break_val;
  249. }
  250. static void hw_break_val_write(void)
  251. {
  252. hw_break_val++;
  253. }
  254. static int get_thread_id_continue(char *put_str, char *arg)
  255. {
  256. char *ptr = &put_str[11];
  257. if (put_str[1] != 'T' || put_str[2] != '0')
  258. return 1;
  259. kgdb_hex2long(&ptr, &cont_thread_id);
  260. return 0;
  261. }
  262. static int check_and_rewind_pc(char *put_str, char *arg)
  263. {
  264. unsigned long addr = lookup_addr(arg);
  265. unsigned long ip;
  266. int offset = 0;
  267. kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
  268. NUMREGBYTES);
  269. gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
  270. ip = instruction_pointer(&kgdbts_regs);
  271. v2printk("Stopped at IP: %lx\n", ip);
  272. #ifdef GDB_ADJUSTS_BREAK_OFFSET
  273. /* On some arches, a breakpoint stop requires it to be decremented */
  274. if (addr + BREAK_INSTR_SIZE == ip)
  275. offset = -BREAK_INSTR_SIZE;
  276. #endif
  277. if (arch_needs_sstep_emulation && sstep_addr &&
  278. ip + offset == sstep_addr &&
  279. ((!strcmp(arg, "sys_open") || !strcmp(arg, "do_fork")))) {
  280. /* This is special case for emulated single step */
  281. v2printk("Emul: rewind hit single step bp\n");
  282. restart_from_top_after_write = 1;
  283. } else if (strcmp(arg, "silent") && ip + offset != addr) {
  284. eprintk("kgdbts: BP mismatch %lx expected %lx\n",
  285. ip + offset, addr);
  286. return 1;
  287. }
  288. /* Readjust the instruction pointer if needed */
  289. ip += offset;
  290. cont_addr = ip;
  291. #ifdef GDB_ADJUSTS_BREAK_OFFSET
  292. instruction_pointer_set(&kgdbts_regs, ip);
  293. #endif
  294. return 0;
  295. }
  296. static int check_single_step(char *put_str, char *arg)
  297. {
  298. unsigned long addr = lookup_addr(arg);
  299. static int matched_id;
  300. /*
  301. * From an arch indepent point of view the instruction pointer
  302. * should be on a different instruction
  303. */
  304. kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
  305. NUMREGBYTES);
  306. gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
  307. v2printk("Singlestep stopped at IP: %lx\n",
  308. instruction_pointer(&kgdbts_regs));
  309. if (sstep_thread_id != cont_thread_id) {
  310. /*
  311. * Ensure we stopped in the same thread id as before, else the
  312. * debugger should continue until the original thread that was
  313. * single stepped is scheduled again, emulating gdb's behavior.
  314. */
  315. v2printk("ThrID does not match: %lx\n", cont_thread_id);
  316. if (arch_needs_sstep_emulation) {
  317. if (matched_id &&
  318. instruction_pointer(&kgdbts_regs) != addr)
  319. goto continue_test;
  320. matched_id++;
  321. ts.idx -= 2;
  322. sstep_state = 0;
  323. return 0;
  324. }
  325. cont_instead_of_sstep = 1;
  326. ts.idx -= 4;
  327. return 0;
  328. }
  329. continue_test:
  330. matched_id = 0;
  331. if (instruction_pointer(&kgdbts_regs) == addr) {
  332. eprintk("kgdbts: SingleStep failed at %lx\n",
  333. instruction_pointer(&kgdbts_regs));
  334. return 1;
  335. }
  336. return 0;
  337. }
  338. static void write_regs(char *arg)
  339. {
  340. memset(scratch_buf, 0, sizeof(scratch_buf));
  341. scratch_buf[0] = 'G';
  342. pt_regs_to_gdb_regs(kgdbts_gdb_regs, &kgdbts_regs);
  343. kgdb_mem2hex((char *)kgdbts_gdb_regs, &scratch_buf[1], NUMREGBYTES);
  344. fill_get_buf(scratch_buf);
  345. }
  346. static void skip_back_repeat_test(char *arg)
  347. {
  348. int go_back = simple_strtol(arg, NULL, 10);
  349. repeat_test--;
  350. if (repeat_test <= 0) {
  351. ts.idx++;
  352. } else {
  353. if (repeat_test % 100 == 0)
  354. v1printk("kgdbts:RUN ... %d remaining\n", repeat_test);
  355. ts.idx -= go_back;
  356. }
  357. fill_get_buf(ts.tst[ts.idx].get);
  358. }
  359. static int got_break(char *put_str, char *arg)
  360. {
  361. test_complete = 1;
  362. if (!strncmp(put_str+1, arg, 2)) {
  363. if (!strncmp(arg, "T0", 2))
  364. test_complete = 2;
  365. return 0;
  366. }
  367. return 1;
  368. }
  369. static void get_cont_catch(char *arg)
  370. {
  371. /* Always send detach because the test is completed at this point */
  372. fill_get_buf("D");
  373. }
  374. static int put_cont_catch(char *put_str, char *arg)
  375. {
  376. /* This is at the end of the test and we catch any and all input */
  377. v2printk("kgdbts: cleanup task: %lx\n", sstep_thread_id);
  378. ts.idx--;
  379. return 0;
  380. }
  381. static int emul_reset(char *put_str, char *arg)
  382. {
  383. if (strncmp(put_str, "$OK", 3))
  384. return 1;
  385. if (restart_from_top_after_write) {
  386. restart_from_top_after_write = 0;
  387. ts.idx = -1;
  388. }
  389. return 0;
  390. }
  391. static void emul_sstep_get(char *arg)
  392. {
  393. if (!arch_needs_sstep_emulation) {
  394. if (cont_instead_of_sstep) {
  395. cont_instead_of_sstep = 0;
  396. fill_get_buf("c");
  397. } else {
  398. fill_get_buf(arg);
  399. }
  400. return;
  401. }
  402. switch (sstep_state) {
  403. case 0:
  404. v2printk("Emulate single step\n");
  405. /* Start by looking at the current PC */
  406. fill_get_buf("g");
  407. break;
  408. case 1:
  409. /* set breakpoint */
  410. break_helper("Z0", NULL, sstep_addr);
  411. break;
  412. case 2:
  413. /* Continue */
  414. fill_get_buf("c");
  415. break;
  416. case 3:
  417. /* Clear breakpoint */
  418. break_helper("z0", NULL, sstep_addr);
  419. break;
  420. default:
  421. eprintk("kgdbts: ERROR failed sstep get emulation\n");
  422. }
  423. sstep_state++;
  424. }
  425. static int emul_sstep_put(char *put_str, char *arg)
  426. {
  427. if (!arch_needs_sstep_emulation) {
  428. char *ptr = &put_str[11];
  429. if (put_str[1] != 'T' || put_str[2] != '0')
  430. return 1;
  431. kgdb_hex2long(&ptr, &sstep_thread_id);
  432. return 0;
  433. }
  434. switch (sstep_state) {
  435. case 1:
  436. /* validate the "g" packet to get the IP */
  437. kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
  438. NUMREGBYTES);
  439. gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
  440. v2printk("Stopped at IP: %lx\n",
  441. instruction_pointer(&kgdbts_regs));
  442. /* Want to stop at IP + break instruction size by default */
  443. sstep_addr = cont_addr + BREAK_INSTR_SIZE;
  444. break;
  445. case 2:
  446. if (strncmp(put_str, "$OK", 3)) {
  447. eprintk("kgdbts: failed sstep break set\n");
  448. return 1;
  449. }
  450. break;
  451. case 3:
  452. if (strncmp(put_str, "$T0", 3)) {
  453. eprintk("kgdbts: failed continue sstep\n");
  454. return 1;
  455. } else {
  456. char *ptr = &put_str[11];
  457. kgdb_hex2long(&ptr, &sstep_thread_id);
  458. }
  459. break;
  460. case 4:
  461. if (strncmp(put_str, "$OK", 3)) {
  462. eprintk("kgdbts: failed sstep break unset\n");
  463. return 1;
  464. }
  465. /* Single step is complete so continue on! */
  466. sstep_state = 0;
  467. return 0;
  468. default:
  469. eprintk("kgdbts: ERROR failed sstep put emulation\n");
  470. }
  471. /* Continue on the same test line until emulation is complete */
  472. ts.idx--;
  473. return 0;
  474. }
  475. static int final_ack_set(char *put_str, char *arg)
  476. {
  477. if (strncmp(put_str+1, arg, 2))
  478. return 1;
  479. final_ack = 1;
  480. return 0;
  481. }
  482. /*
  483. * Test to plant a breakpoint and detach, which should clear out the
  484. * breakpoint and restore the original instruction.
  485. */
  486. static struct test_struct plant_and_detach_test[] = {
  487. { "?", "S0*" }, /* Clear break points */
  488. { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
  489. { "D", "OK" }, /* Detach */
  490. { "", "" },
  491. };
  492. /*
  493. * Simple test to write in a software breakpoint, check for the
  494. * correct stop location and detach.
  495. */
  496. static struct test_struct sw_breakpoint_test[] = {
  497. { "?", "S0*" }, /* Clear break points */
  498. { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
  499. { "c", "T0*", }, /* Continue */
  500. { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
  501. { "write", "OK", write_regs },
  502. { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
  503. { "D", "OK" }, /* Detach */
  504. { "D", "OK", NULL, got_break }, /* On success we made it here */
  505. { "", "" },
  506. };
  507. /*
  508. * Test a known bad memory read location to test the fault handler and
  509. * read bytes 1-8 at the bad address
  510. */
  511. static struct test_struct bad_read_test[] = {
  512. { "?", "S0*" }, /* Clear break points */
  513. { "m0,1", "E*" }, /* read 1 byte at address 1 */
  514. { "m0,2", "E*" }, /* read 1 byte at address 2 */
  515. { "m0,3", "E*" }, /* read 1 byte at address 3 */
  516. { "m0,4", "E*" }, /* read 1 byte at address 4 */
  517. { "m0,5", "E*" }, /* read 1 byte at address 5 */
  518. { "m0,6", "E*" }, /* read 1 byte at address 6 */
  519. { "m0,7", "E*" }, /* read 1 byte at address 7 */
  520. { "m0,8", "E*" }, /* read 1 byte at address 8 */
  521. { "D", "OK" }, /* Detach which removes all breakpoints and continues */
  522. { "", "" },
  523. };
  524. /*
  525. * Test for hitting a breakpoint, remove it, single step, plant it
  526. * again and detach.
  527. */
  528. static struct test_struct singlestep_break_test[] = {
  529. { "?", "S0*" }, /* Clear break points */
  530. { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
  531. { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
  532. { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
  533. { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
  534. { "write", "OK", write_regs }, /* Write registers */
  535. { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
  536. { "g", "kgdbts_break_test", NULL, check_single_step },
  537. { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
  538. { "c", "T0*", }, /* Continue */
  539. { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
  540. { "write", "OK", write_regs }, /* Write registers */
  541. { "D", "OK" }, /* Remove all breakpoints and continues */
  542. { "", "" },
  543. };
  544. /*
  545. * Test for hitting a breakpoint at do_fork for what ever the number
  546. * of iterations required by the variable repeat_test.
  547. */
  548. static struct test_struct do_fork_test[] = {
  549. { "?", "S0*" }, /* Clear break points */
  550. { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
  551. { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
  552. { "do_fork", "OK", sw_rem_break }, /*remove breakpoint */
  553. { "g", "do_fork", NULL, check_and_rewind_pc }, /* check location */
  554. { "write", "OK", write_regs, emul_reset }, /* Write registers */
  555. { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
  556. { "g", "do_fork", NULL, check_single_step },
  557. { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
  558. { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
  559. { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
  560. { "", "", get_cont_catch, put_cont_catch },
  561. };
  562. /* Test for hitting a breakpoint at sys_open for what ever the number
  563. * of iterations required by the variable repeat_test.
  564. */
  565. static struct test_struct sys_open_test[] = {
  566. { "?", "S0*" }, /* Clear break points */
  567. { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
  568. { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
  569. { "sys_open", "OK", sw_rem_break }, /*remove breakpoint */
  570. { "g", "sys_open", NULL, check_and_rewind_pc }, /* check location */
  571. { "write", "OK", write_regs, emul_reset }, /* Write registers */
  572. { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
  573. { "g", "sys_open", NULL, check_single_step },
  574. { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
  575. { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
  576. { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
  577. { "", "", get_cont_catch, put_cont_catch },
  578. };
  579. /*
  580. * Test for hitting a simple hw breakpoint
  581. */
  582. static struct test_struct hw_breakpoint_test[] = {
  583. { "?", "S0*" }, /* Clear break points */
  584. { "kgdbts_break_test", "OK", hw_break, }, /* set hw breakpoint */
  585. { "c", "T0*", }, /* Continue */
  586. { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
  587. { "write", "OK", write_regs },
  588. { "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
  589. { "D", "OK" }, /* Detach */
  590. { "D", "OK", NULL, got_break }, /* On success we made it here */
  591. { "", "" },
  592. };
  593. /*
  594. * Test for hitting a hw write breakpoint
  595. */
  596. static struct test_struct hw_write_break_test[] = {
  597. { "?", "S0*" }, /* Clear break points */
  598. { "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
  599. { "c", "T0*", NULL, got_break }, /* Continue */
  600. { "g", "silent", NULL, check_and_rewind_pc },
  601. { "write", "OK", write_regs },
  602. { "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
  603. { "D", "OK" }, /* Detach */
  604. { "D", "OK", NULL, got_break }, /* On success we made it here */
  605. { "", "" },
  606. };
  607. /*
  608. * Test for hitting a hw access breakpoint
  609. */
  610. static struct test_struct hw_access_break_test[] = {
  611. { "?", "S0*" }, /* Clear break points */
  612. { "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
  613. { "c", "T0*", NULL, got_break }, /* Continue */
  614. { "g", "silent", NULL, check_and_rewind_pc },
  615. { "write", "OK", write_regs },
  616. { "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
  617. { "D", "OK" }, /* Detach */
  618. { "D", "OK", NULL, got_break }, /* On success we made it here */
  619. { "", "" },
  620. };
  621. /*
  622. * Test for hitting a hw access breakpoint
  623. */
  624. static struct test_struct nmi_sleep_test[] = {
  625. { "?", "S0*" }, /* Clear break points */
  626. { "c", "T0*", NULL, got_break }, /* Continue */
  627. { "D", "OK" }, /* Detach */
  628. { "D", "OK", NULL, got_break }, /* On success we made it here */
  629. { "", "" },
  630. };
  631. static void fill_get_buf(char *buf)
  632. {
  633. unsigned char checksum = 0;
  634. int count = 0;
  635. char ch;
  636. strcpy(get_buf, "$");
  637. strcat(get_buf, buf);
  638. while ((ch = buf[count])) {
  639. checksum += ch;
  640. count++;
  641. }
  642. strcat(get_buf, "#");
  643. get_buf[count + 2] = hex_asc_hi(checksum);
  644. get_buf[count + 3] = hex_asc_lo(checksum);
  645. get_buf[count + 4] = '\0';
  646. v2printk("get%i: %s\n", ts.idx, get_buf);
  647. }
  648. static int validate_simple_test(char *put_str)
  649. {
  650. char *chk_str;
  651. if (ts.tst[ts.idx].put_handler)
  652. return ts.tst[ts.idx].put_handler(put_str,
  653. ts.tst[ts.idx].put);
  654. chk_str = ts.tst[ts.idx].put;
  655. if (*put_str == '$')
  656. put_str++;
  657. while (*chk_str != '\0' && *put_str != '\0') {
  658. /* If someone does a * to match the rest of the string, allow
  659. * it, or stop if the received string is complete.
  660. */
  661. if (*put_str == '#' || *chk_str == '*')
  662. return 0;
  663. if (*put_str != *chk_str)
  664. return 1;
  665. chk_str++;
  666. put_str++;
  667. }
  668. if (*chk_str == '\0' && (*put_str == '\0' || *put_str == '#'))
  669. return 0;
  670. return 1;
  671. }
  672. static int run_simple_test(int is_get_char, int chr)
  673. {
  674. int ret = 0;
  675. if (is_get_char) {
  676. /* Send an ACK on the get if a prior put completed and set the
  677. * send ack variable
  678. */
  679. if (send_ack) {
  680. send_ack = 0;
  681. return '+';
  682. }
  683. /* On the first get char, fill the transmit buffer and then
  684. * take from the get_string.
  685. */
  686. if (get_buf_cnt == 0) {
  687. if (ts.tst[ts.idx].get_handler)
  688. ts.tst[ts.idx].get_handler(ts.tst[ts.idx].get);
  689. else
  690. fill_get_buf(ts.tst[ts.idx].get);
  691. }
  692. if (get_buf[get_buf_cnt] == '\0') {
  693. eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
  694. ts.name, ts.idx);
  695. get_buf_cnt = 0;
  696. fill_get_buf("D");
  697. }
  698. ret = get_buf[get_buf_cnt];
  699. get_buf_cnt++;
  700. return ret;
  701. }
  702. /* This callback is a put char which is when kgdb sends data to
  703. * this I/O module.
  704. */
  705. if (ts.tst[ts.idx].get[0] == '\0' && ts.tst[ts.idx].put[0] == '\0' &&
  706. !ts.tst[ts.idx].get_handler) {
  707. eprintk("kgdbts: ERROR: beyond end of test on"
  708. " '%s' line %i\n", ts.name, ts.idx);
  709. return 0;
  710. }
  711. if (put_buf_cnt >= BUFMAX) {
  712. eprintk("kgdbts: ERROR: put buffer overflow on"
  713. " '%s' line %i\n", ts.name, ts.idx);
  714. put_buf_cnt = 0;
  715. return 0;
  716. }
  717. /* Ignore everything until the first valid packet start '$' */
  718. if (put_buf_cnt == 0 && chr != '$')
  719. return 0;
  720. put_buf[put_buf_cnt] = chr;
  721. put_buf_cnt++;
  722. /* End of packet == #XX so look for the '#' */
  723. if (put_buf_cnt > 3 && put_buf[put_buf_cnt - 3] == '#') {
  724. if (put_buf_cnt >= BUFMAX) {
  725. eprintk("kgdbts: ERROR: put buffer overflow on"
  726. " '%s' line %i\n", ts.name, ts.idx);
  727. put_buf_cnt = 0;
  728. return 0;
  729. }
  730. put_buf[put_buf_cnt] = '\0';
  731. v2printk("put%i: %s\n", ts.idx, put_buf);
  732. /* Trigger check here */
  733. if (ts.validate_put && ts.validate_put(put_buf)) {
  734. eprintk("kgdbts: ERROR PUT: end of test "
  735. "buffer on '%s' line %i expected %s got %s\n",
  736. ts.name, ts.idx, ts.tst[ts.idx].put, put_buf);
  737. }
  738. ts.idx++;
  739. put_buf_cnt = 0;
  740. get_buf_cnt = 0;
  741. send_ack = 1;
  742. }
  743. return 0;
  744. }
  745. static void init_simple_test(void)
  746. {
  747. memset(&ts, 0, sizeof(ts));
  748. ts.run_test = run_simple_test;
  749. ts.validate_put = validate_simple_test;
  750. }
  751. static void run_plant_and_detach_test(int is_early)
  752. {
  753. char before[BREAK_INSTR_SIZE];
  754. char after[BREAK_INSTR_SIZE];
  755. probe_kernel_read(before, (char *)kgdbts_break_test,
  756. BREAK_INSTR_SIZE);
  757. init_simple_test();
  758. ts.tst = plant_and_detach_test;
  759. ts.name = "plant_and_detach_test";
  760. /* Activate test with initial breakpoint */
  761. if (!is_early)
  762. kgdb_breakpoint();
  763. probe_kernel_read(after, (char *)kgdbts_break_test,
  764. BREAK_INSTR_SIZE);
  765. if (memcmp(before, after, BREAK_INSTR_SIZE)) {
  766. printk(KERN_CRIT "kgdbts: ERROR kgdb corrupted memory\n");
  767. panic("kgdb memory corruption");
  768. }
  769. /* complete the detach test */
  770. if (!is_early)
  771. kgdbts_break_test();
  772. }
  773. static void run_breakpoint_test(int is_hw_breakpoint)
  774. {
  775. test_complete = 0;
  776. init_simple_test();
  777. if (is_hw_breakpoint) {
  778. ts.tst = hw_breakpoint_test;
  779. ts.name = "hw_breakpoint_test";
  780. } else {
  781. ts.tst = sw_breakpoint_test;
  782. ts.name = "sw_breakpoint_test";
  783. }
  784. /* Activate test with initial breakpoint */
  785. kgdb_breakpoint();
  786. /* run code with the break point in it */
  787. kgdbts_break_test();
  788. kgdb_breakpoint();
  789. if (test_complete)
  790. return;
  791. eprintk("kgdbts: ERROR %s test failed\n", ts.name);
  792. if (is_hw_breakpoint)
  793. hwbreaks_ok = 0;
  794. }
  795. static void run_hw_break_test(int is_write_test)
  796. {
  797. test_complete = 0;
  798. init_simple_test();
  799. if (is_write_test) {
  800. ts.tst = hw_write_break_test;
  801. ts.name = "hw_write_break_test";
  802. } else {
  803. ts.tst = hw_access_break_test;
  804. ts.name = "hw_access_break_test";
  805. }
  806. /* Activate test with initial breakpoint */
  807. kgdb_breakpoint();
  808. hw_break_val_access();
  809. if (is_write_test) {
  810. if (test_complete == 2) {
  811. eprintk("kgdbts: ERROR %s broke on access\n",
  812. ts.name);
  813. hwbreaks_ok = 0;
  814. }
  815. hw_break_val_write();
  816. }
  817. kgdb_breakpoint();
  818. if (test_complete == 1)
  819. return;
  820. eprintk("kgdbts: ERROR %s test failed\n", ts.name);
  821. hwbreaks_ok = 0;
  822. }
  823. static void run_nmi_sleep_test(int nmi_sleep)
  824. {
  825. unsigned long flags;
  826. init_simple_test();
  827. ts.tst = nmi_sleep_test;
  828. ts.name = "nmi_sleep_test";
  829. /* Activate test with initial breakpoint */
  830. kgdb_breakpoint();
  831. local_irq_save(flags);
  832. mdelay(nmi_sleep*1000);
  833. touch_nmi_watchdog();
  834. local_irq_restore(flags);
  835. if (test_complete != 2)
  836. eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
  837. kgdb_breakpoint();
  838. if (test_complete == 1)
  839. return;
  840. eprintk("kgdbts: ERROR %s test failed\n", ts.name);
  841. }
  842. static void run_bad_read_test(void)
  843. {
  844. init_simple_test();
  845. ts.tst = bad_read_test;
  846. ts.name = "bad_read_test";
  847. /* Activate test with initial breakpoint */
  848. kgdb_breakpoint();
  849. }
  850. static void run_do_fork_test(void)
  851. {
  852. init_simple_test();
  853. ts.tst = do_fork_test;
  854. ts.name = "do_fork_test";
  855. /* Activate test with initial breakpoint */
  856. kgdb_breakpoint();
  857. }
  858. static void run_sys_open_test(void)
  859. {
  860. init_simple_test();
  861. ts.tst = sys_open_test;
  862. ts.name = "sys_open_test";
  863. /* Activate test with initial breakpoint */
  864. kgdb_breakpoint();
  865. }
  866. static void run_singlestep_break_test(void)
  867. {
  868. init_simple_test();
  869. ts.tst = singlestep_break_test;
  870. ts.name = "singlestep_breakpoint_test";
  871. /* Activate test with initial breakpoint */
  872. kgdb_breakpoint();
  873. kgdbts_break_test();
  874. kgdbts_break_test();
  875. }
  876. static void kgdbts_run_tests(void)
  877. {
  878. char *ptr;
  879. int fork_test = 0;
  880. int do_sys_open_test = 0;
  881. int sstep_test = 1000;
  882. int nmi_sleep = 0;
  883. int i;
  884. verbose = 0;
  885. if (strstr(config, "V1"))
  886. verbose = 1;
  887. if (strstr(config, "V2"))
  888. verbose = 2;
  889. ptr = strchr(config, 'F');
  890. if (ptr)
  891. fork_test = simple_strtol(ptr + 1, NULL, 10);
  892. ptr = strchr(config, 'S');
  893. if (ptr)
  894. do_sys_open_test = simple_strtol(ptr + 1, NULL, 10);
  895. ptr = strchr(config, 'N');
  896. if (ptr)
  897. nmi_sleep = simple_strtol(ptr+1, NULL, 10);
  898. ptr = strchr(config, 'I');
  899. if (ptr)
  900. sstep_test = simple_strtol(ptr+1, NULL, 10);
  901. /* All HW break point tests */
  902. if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) {
  903. hwbreaks_ok = 1;
  904. v1printk("kgdbts:RUN hw breakpoint test\n");
  905. run_breakpoint_test(1);
  906. v1printk("kgdbts:RUN hw write breakpoint test\n");
  907. run_hw_break_test(1);
  908. v1printk("kgdbts:RUN access write breakpoint test\n");
  909. run_hw_break_test(0);
  910. }
  911. /* required internal KGDB tests */
  912. v1printk("kgdbts:RUN plant and detach test\n");
  913. run_plant_and_detach_test(0);
  914. v1printk("kgdbts:RUN sw breakpoint test\n");
  915. run_breakpoint_test(0);
  916. v1printk("kgdbts:RUN bad memory access test\n");
  917. run_bad_read_test();
  918. v1printk("kgdbts:RUN singlestep test %i iterations\n", sstep_test);
  919. for (i = 0; i < sstep_test; i++) {
  920. run_singlestep_break_test();
  921. if (i % 100 == 0)
  922. v1printk("kgdbts:RUN singlestep [%i/%i]\n",
  923. i, sstep_test);
  924. }
  925. /* ===Optional tests=== */
  926. if (nmi_sleep) {
  927. v1printk("kgdbts:RUN NMI sleep %i seconds test\n", nmi_sleep);
  928. run_nmi_sleep_test(nmi_sleep);
  929. }
  930. /* If the do_fork test is run it will be the last test that is
  931. * executed because a kernel thread will be spawned at the very
  932. * end to unregister the debug hooks.
  933. */
  934. if (fork_test) {
  935. repeat_test = fork_test;
  936. printk(KERN_INFO "kgdbts:RUN do_fork for %i breakpoints\n",
  937. repeat_test);
  938. kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
  939. run_do_fork_test();
  940. return;
  941. }
  942. /* If the sys_open test is run it will be the last test that is
  943. * executed because a kernel thread will be spawned at the very
  944. * end to unregister the debug hooks.
  945. */
  946. if (do_sys_open_test) {
  947. repeat_test = do_sys_open_test;
  948. printk(KERN_INFO "kgdbts:RUN sys_open for %i breakpoints\n",
  949. repeat_test);
  950. kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
  951. run_sys_open_test();
  952. return;
  953. }
  954. /* Shutdown and unregister */
  955. kgdb_unregister_io_module(&kgdbts_io_ops);
  956. configured = 0;
  957. }
  958. static int kgdbts_option_setup(char *opt)
  959. {
  960. if (strlen(opt) >= MAX_CONFIG_LEN) {
  961. printk(KERN_ERR "kgdbts: config string too long\n");
  962. return -ENOSPC;
  963. }
  964. strcpy(config, opt);
  965. return 0;
  966. }
  967. __setup("kgdbts=", kgdbts_option_setup);
  968. static int configure_kgdbts(void)
  969. {
  970. int err = 0;
  971. if (!strlen(config) || isspace(config[0]))
  972. goto noconfig;
  973. final_ack = 0;
  974. run_plant_and_detach_test(1);
  975. err = kgdb_register_io_module(&kgdbts_io_ops);
  976. if (err) {
  977. configured = 0;
  978. return err;
  979. }
  980. configured = 1;
  981. kgdbts_run_tests();
  982. return err;
  983. noconfig:
  984. config[0] = 0;
  985. configured = 0;
  986. return err;
  987. }
  988. static int __init init_kgdbts(void)
  989. {
  990. /* Already configured? */
  991. if (configured == 1)
  992. return 0;
  993. return configure_kgdbts();
  994. }
  995. device_initcall(init_kgdbts);
  996. static int kgdbts_get_char(void)
  997. {
  998. int val = 0;
  999. if (ts.run_test)
  1000. val = ts.run_test(1, 0);
  1001. return val;
  1002. }
  1003. static void kgdbts_put_char(u8 chr)
  1004. {
  1005. if (ts.run_test)
  1006. ts.run_test(0, chr);
  1007. }
  1008. static int param_set_kgdbts_var(const char *kmessage,
  1009. const struct kernel_param *kp)
  1010. {
  1011. size_t len = strlen(kmessage);
  1012. if (len >= MAX_CONFIG_LEN) {
  1013. printk(KERN_ERR "kgdbts: config string too long\n");
  1014. return -ENOSPC;
  1015. }
  1016. /* Only copy in the string if the init function has not run yet */
  1017. if (configured < 0) {
  1018. strcpy(config, kmessage);
  1019. return 0;
  1020. }
  1021. if (configured == 1) {
  1022. printk(KERN_ERR "kgdbts: ERROR: Already configured and running.\n");
  1023. return -EBUSY;
  1024. }
  1025. strcpy(config, kmessage);
  1026. /* Chop out \n char as a result of echo */
  1027. if (len && config[len - 1] == '\n')
  1028. config[len - 1] = '\0';
  1029. /* Go and configure with the new params. */
  1030. return configure_kgdbts();
  1031. }
  1032. static void kgdbts_pre_exp_handler(void)
  1033. {
  1034. /* Increment the module count when the debugger is active */
  1035. if (!kgdb_connected)
  1036. try_module_get(THIS_MODULE);
  1037. }
  1038. static void kgdbts_post_exp_handler(void)
  1039. {
  1040. /* decrement the module count when the debugger detaches */
  1041. if (!kgdb_connected)
  1042. module_put(THIS_MODULE);
  1043. }
  1044. static struct kgdb_io kgdbts_io_ops = {
  1045. .name = "kgdbts",
  1046. .read_char = kgdbts_get_char,
  1047. .write_char = kgdbts_put_char,
  1048. .pre_exception = kgdbts_pre_exp_handler,
  1049. .post_exception = kgdbts_post_exp_handler,
  1050. };
  1051. /*
  1052. * not really modular, but the easiest way to keep compat with existing
  1053. * bootargs behaviour is to continue using module_param here.
  1054. */
  1055. module_param_call(kgdbts, param_set_kgdbts_var, param_get_string, &kps, 0644);
  1056. MODULE_PARM_DESC(kgdbts, "<A|V1|V2>[F#|S#][N#]");