kgdbts.c 31 KB

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