kgdbts.c 31 KB

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