kgdbts.c 30 KB

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