kgdbts.c 31 KB

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