kgdbts.c 29 KB

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