kgdbts.c 31 KB

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