kgdbts.c 30 KB

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