kgdbts.c 31 KB

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