kgdbts.c 28 KB

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