i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / misc / kgdbts.c
blob3aa9a969b373a4ffd102e35e2f5c804274675dab
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.
6 * Created by: Jason Wessel <jason.wessel@windriver.com>
8 * Copyright (c) 2008 Wind River Systems, Inc.
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.
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.
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
23 /* Information about the kgdb test suite.
24 * -------------------------------------
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.
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:
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
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
52 * NOTE: that the do_fork and sys_open tests are mutually exclusive.
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
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
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.
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
75 * 2) After the system boot run the basic test.
76 * echo kgdbts=V1 > /sys/module/kgdbts/parameters/kgdbts
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.
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
97 #include <linux/kernel.h>
98 #include <linux/kgdb.h>
99 #include <linux/ctype.h>
100 #include <linux/uaccess.h>
101 #include <linux/syscalls.h>
102 #include <linux/nmi.h>
103 #include <linux/delay.h>
104 #include <linux/kthread.h>
105 #include <linux/module.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
122 static struct kgdb_io kgdbts_io_ops;
123 static char get_buf[BUFMAX];
124 static int get_buf_cnt;
125 static char put_buf[BUFMAX];
126 static int put_buf_cnt;
127 static char scratch_buf[BUFMAX];
128 static int verbose;
129 static int repeat_test;
130 static int test_complete;
131 static int send_ack;
132 static int final_ack;
133 static int force_hwbrks;
134 static int hwbreaks_ok;
135 static int hw_break_val;
136 static int hw_break_val2;
137 static int cont_instead_of_sstep;
138 static unsigned long cont_thread_id;
139 static unsigned long sstep_thread_id;
140 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_SPARC)
141 static int arch_needs_sstep_emulation = 1;
142 #else
143 static int arch_needs_sstep_emulation;
144 #endif
145 static unsigned long cont_addr;
146 static unsigned long sstep_addr;
147 static int restart_from_top_after_write;
148 static int sstep_state;
150 /* Storage for the registers, in GDB format. */
151 static unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
152 sizeof(unsigned long) - 1) /
153 sizeof(unsigned long)];
154 static struct pt_regs kgdbts_regs;
156 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
157 static int configured = -1;
159 #ifdef CONFIG_KGDB_TESTS_BOOT_STRING
160 static char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
161 #else
162 static char config[MAX_CONFIG_LEN];
163 #endif
164 static struct kparam_string kps = {
165 .string = config,
166 .maxlen = MAX_CONFIG_LEN,
169 static void fill_get_buf(char *buf);
171 struct test_struct {
172 char *get;
173 char *put;
174 void (*get_handler)(char *);
175 int (*put_handler)(char *, char *);
178 struct test_state {
179 char *name;
180 struct test_struct *tst;
181 int idx;
182 int (*run_test) (int, int);
183 int (*validate_put) (char *);
186 static struct test_state ts;
188 static int kgdbts_unreg_thread(void *ptr)
190 /* Wait until the tests are complete and then ungresiter the I/O
191 * driver.
193 while (!final_ack)
194 msleep_interruptible(1500);
195 /* Pause for any other threads to exit after final ack. */
196 msleep_interruptible(1000);
197 if (configured)
198 kgdb_unregister_io_module(&kgdbts_io_ops);
199 configured = 0;
201 return 0;
204 /* This is noinline such that it can be used for a single location to
205 * place a breakpoint
207 static noinline void kgdbts_break_test(void)
209 v2printk("kgdbts: breakpoint complete\n");
212 /* Lookup symbol info in the kernel */
213 static unsigned long lookup_addr(char *arg)
215 unsigned long addr = 0;
217 if (!strcmp(arg, "kgdbts_break_test"))
218 addr = (unsigned long)kgdbts_break_test;
219 else if (!strcmp(arg, "sys_open"))
220 addr = (unsigned long)do_sys_open;
221 else if (!strcmp(arg, "do_fork"))
222 addr = (unsigned long)do_fork;
223 else if (!strcmp(arg, "hw_break_val"))
224 addr = (unsigned long)&hw_break_val;
225 return addr;
228 static void break_helper(char *bp_type, char *arg, unsigned long vaddr)
230 unsigned long addr;
232 if (arg)
233 addr = lookup_addr(arg);
234 else
235 addr = vaddr;
237 sprintf(scratch_buf, "%s,%lx,%i", bp_type, addr,
238 BREAK_INSTR_SIZE);
239 fill_get_buf(scratch_buf);
242 static void sw_break(char *arg)
244 break_helper(force_hwbrks ? "Z1" : "Z0", arg, 0);
247 static void sw_rem_break(char *arg)
249 break_helper(force_hwbrks ? "z1" : "z0", arg, 0);
252 static void hw_break(char *arg)
254 break_helper("Z1", arg, 0);
257 static void hw_rem_break(char *arg)
259 break_helper("z1", arg, 0);
262 static void hw_write_break(char *arg)
264 break_helper("Z2", arg, 0);
267 static void hw_rem_write_break(char *arg)
269 break_helper("z2", arg, 0);
272 static void hw_access_break(char *arg)
274 break_helper("Z4", arg, 0);
277 static void hw_rem_access_break(char *arg)
279 break_helper("z4", arg, 0);
282 static void hw_break_val_access(void)
284 hw_break_val2 = hw_break_val;
287 static void hw_break_val_write(void)
289 hw_break_val++;
292 static int get_thread_id_continue(char *put_str, char *arg)
294 char *ptr = &put_str[11];
296 if (put_str[1] != 'T' || put_str[2] != '0')
297 return 1;
298 kgdb_hex2long(&ptr, &cont_thread_id);
299 return 0;
302 static int check_and_rewind_pc(char *put_str, char *arg)
304 unsigned long addr = lookup_addr(arg);
305 unsigned long ip;
306 int offset = 0;
308 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
309 NUMREGBYTES);
310 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
311 ip = instruction_pointer(&kgdbts_regs);
312 v2printk("Stopped at IP: %lx\n", ip);
313 #ifdef GDB_ADJUSTS_BREAK_OFFSET
314 /* On some arches, a breakpoint stop requires it to be decremented */
315 if (addr + BREAK_INSTR_SIZE == ip)
316 offset = -BREAK_INSTR_SIZE;
317 #endif
319 if (arch_needs_sstep_emulation && sstep_addr &&
320 ip + offset == sstep_addr &&
321 ((!strcmp(arg, "sys_open") || !strcmp(arg, "do_fork")))) {
322 /* This is special case for emulated single step */
323 v2printk("Emul: rewind hit single step bp\n");
324 restart_from_top_after_write = 1;
325 } else if (strcmp(arg, "silent") && ip + offset != addr) {
326 eprintk("kgdbts: BP mismatch %lx expected %lx\n",
327 ip + offset, addr);
328 return 1;
330 /* Readjust the instruction pointer if needed */
331 ip += offset;
332 cont_addr = ip;
333 #ifdef GDB_ADJUSTS_BREAK_OFFSET
334 instruction_pointer_set(&kgdbts_regs, ip);
335 #endif
336 return 0;
339 static int check_single_step(char *put_str, char *arg)
341 unsigned long addr = lookup_addr(arg);
342 static int matched_id;
345 * From an arch indepent point of view the instruction pointer
346 * should be on a different instruction
348 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
349 NUMREGBYTES);
350 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
351 v2printk("Singlestep stopped at IP: %lx\n",
352 instruction_pointer(&kgdbts_regs));
354 if (sstep_thread_id != cont_thread_id) {
356 * Ensure we stopped in the same thread id as before, else the
357 * debugger should continue until the original thread that was
358 * single stepped is scheduled again, emulating gdb's behavior.
360 v2printk("ThrID does not match: %lx\n", cont_thread_id);
361 if (arch_needs_sstep_emulation) {
362 if (matched_id &&
363 instruction_pointer(&kgdbts_regs) != addr)
364 goto continue_test;
365 matched_id++;
366 ts.idx -= 2;
367 sstep_state = 0;
368 return 0;
370 cont_instead_of_sstep = 1;
371 ts.idx -= 4;
372 return 0;
374 continue_test:
375 matched_id = 0;
376 if (instruction_pointer(&kgdbts_regs) == addr) {
377 eprintk("kgdbts: SingleStep failed at %lx\n",
378 instruction_pointer(&kgdbts_regs));
379 return 1;
382 return 0;
385 static void write_regs(char *arg)
387 memset(scratch_buf, 0, sizeof(scratch_buf));
388 scratch_buf[0] = 'G';
389 pt_regs_to_gdb_regs(kgdbts_gdb_regs, &kgdbts_regs);
390 kgdb_mem2hex((char *)kgdbts_gdb_regs, &scratch_buf[1], NUMREGBYTES);
391 fill_get_buf(scratch_buf);
394 static void skip_back_repeat_test(char *arg)
396 int go_back = simple_strtol(arg, NULL, 10);
398 repeat_test--;
399 if (repeat_test <= 0)
400 ts.idx++;
401 else
402 ts.idx -= go_back;
403 fill_get_buf(ts.tst[ts.idx].get);
406 static int got_break(char *put_str, char *arg)
408 test_complete = 1;
409 if (!strncmp(put_str+1, arg, 2)) {
410 if (!strncmp(arg, "T0", 2))
411 test_complete = 2;
412 return 0;
414 return 1;
417 static void get_cont_catch(char *arg)
419 /* Always send detach because the test is completed at this point */
420 fill_get_buf("D");
423 static int put_cont_catch(char *put_str, char *arg)
425 /* This is at the end of the test and we catch any and all input */
426 v2printk("kgdbts: cleanup task: %lx\n", sstep_thread_id);
427 ts.idx--;
428 return 0;
431 static int emul_reset(char *put_str, char *arg)
433 if (strncmp(put_str, "$OK", 3))
434 return 1;
435 if (restart_from_top_after_write) {
436 restart_from_top_after_write = 0;
437 ts.idx = -1;
439 return 0;
442 static void emul_sstep_get(char *arg)
444 if (!arch_needs_sstep_emulation) {
445 if (cont_instead_of_sstep) {
446 cont_instead_of_sstep = 0;
447 fill_get_buf("c");
448 } else {
449 fill_get_buf(arg);
451 return;
453 switch (sstep_state) {
454 case 0:
455 v2printk("Emulate single step\n");
456 /* Start by looking at the current PC */
457 fill_get_buf("g");
458 break;
459 case 1:
460 /* set breakpoint */
461 break_helper("Z0", NULL, sstep_addr);
462 break;
463 case 2:
464 /* Continue */
465 fill_get_buf("c");
466 break;
467 case 3:
468 /* Clear breakpoint */
469 break_helper("z0", NULL, sstep_addr);
470 break;
471 default:
472 eprintk("kgdbts: ERROR failed sstep get emulation\n");
474 sstep_state++;
477 static int emul_sstep_put(char *put_str, char *arg)
479 if (!arch_needs_sstep_emulation) {
480 char *ptr = &put_str[11];
481 if (put_str[1] != 'T' || put_str[2] != '0')
482 return 1;
483 kgdb_hex2long(&ptr, &sstep_thread_id);
484 return 0;
486 switch (sstep_state) {
487 case 1:
488 /* validate the "g" packet to get the IP */
489 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
490 NUMREGBYTES);
491 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
492 v2printk("Stopped at IP: %lx\n",
493 instruction_pointer(&kgdbts_regs));
494 /* Want to stop at IP + break instruction size by default */
495 sstep_addr = cont_addr + BREAK_INSTR_SIZE;
496 break;
497 case 2:
498 if (strncmp(put_str, "$OK", 3)) {
499 eprintk("kgdbts: failed sstep break set\n");
500 return 1;
502 break;
503 case 3:
504 if (strncmp(put_str, "$T0", 3)) {
505 eprintk("kgdbts: failed continue sstep\n");
506 return 1;
507 } else {
508 char *ptr = &put_str[11];
509 kgdb_hex2long(&ptr, &sstep_thread_id);
511 break;
512 case 4:
513 if (strncmp(put_str, "$OK", 3)) {
514 eprintk("kgdbts: failed sstep break unset\n");
515 return 1;
517 /* Single step is complete so continue on! */
518 sstep_state = 0;
519 return 0;
520 default:
521 eprintk("kgdbts: ERROR failed sstep put emulation\n");
524 /* Continue on the same test line until emulation is complete */
525 ts.idx--;
526 return 0;
529 static int final_ack_set(char *put_str, char *arg)
531 if (strncmp(put_str+1, arg, 2))
532 return 1;
533 final_ack = 1;
534 return 0;
537 * Test to plant a breakpoint and detach, which should clear out the
538 * breakpoint and restore the original instruction.
540 static struct test_struct plant_and_detach_test[] = {
541 { "?", "S0*" }, /* Clear break points */
542 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
543 { "D", "OK" }, /* Detach */
544 { "", "" },
548 * Simple test to write in a software breakpoint, check for the
549 * correct stop location and detach.
551 static struct test_struct sw_breakpoint_test[] = {
552 { "?", "S0*" }, /* Clear break points */
553 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
554 { "c", "T0*", }, /* Continue */
555 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
556 { "write", "OK", write_regs },
557 { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
558 { "D", "OK" }, /* Detach */
559 { "D", "OK", NULL, got_break }, /* On success we made it here */
560 { "", "" },
564 * Test a known bad memory read location to test the fault handler and
565 * read bytes 1-8 at the bad address
567 static struct test_struct bad_read_test[] = {
568 { "?", "S0*" }, /* Clear break points */
569 { "m0,1", "E*" }, /* read 1 byte at address 1 */
570 { "m0,2", "E*" }, /* read 1 byte at address 2 */
571 { "m0,3", "E*" }, /* read 1 byte at address 3 */
572 { "m0,4", "E*" }, /* read 1 byte at address 4 */
573 { "m0,5", "E*" }, /* read 1 byte at address 5 */
574 { "m0,6", "E*" }, /* read 1 byte at address 6 */
575 { "m0,7", "E*" }, /* read 1 byte at address 7 */
576 { "m0,8", "E*" }, /* read 1 byte at address 8 */
577 { "D", "OK" }, /* Detach which removes all breakpoints and continues */
578 { "", "" },
582 * Test for hitting a breakpoint, remove it, single step, plant it
583 * again and detach.
585 static struct test_struct singlestep_break_test[] = {
586 { "?", "S0*" }, /* Clear break points */
587 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
588 { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
589 { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
590 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
591 { "write", "OK", write_regs }, /* Write registers */
592 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
593 { "g", "kgdbts_break_test", NULL, check_single_step },
594 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
595 { "c", "T0*", }, /* Continue */
596 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
597 { "write", "OK", write_regs }, /* Write registers */
598 { "D", "OK" }, /* Remove all breakpoints and continues */
599 { "", "" },
603 * Test for hitting a breakpoint at do_fork for what ever the number
604 * of iterations required by the variable repeat_test.
606 static struct test_struct do_fork_test[] = {
607 { "?", "S0*" }, /* Clear break points */
608 { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
609 { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
610 { "do_fork", "OK", sw_rem_break }, /*remove breakpoint */
611 { "g", "do_fork", NULL, check_and_rewind_pc }, /* check location */
612 { "write", "OK", write_regs, emul_reset }, /* Write registers */
613 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
614 { "g", "do_fork", NULL, check_single_step },
615 { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
616 { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
617 { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
618 { "", "", get_cont_catch, put_cont_catch },
621 /* Test for hitting a breakpoint at sys_open for what ever the number
622 * of iterations required by the variable repeat_test.
624 static struct test_struct sys_open_test[] = {
625 { "?", "S0*" }, /* Clear break points */
626 { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
627 { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
628 { "sys_open", "OK", sw_rem_break }, /*remove breakpoint */
629 { "g", "sys_open", NULL, check_and_rewind_pc }, /* check location */
630 { "write", "OK", write_regs, emul_reset }, /* Write registers */
631 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
632 { "g", "sys_open", NULL, check_single_step },
633 { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
634 { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
635 { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
636 { "", "", get_cont_catch, put_cont_catch },
640 * Test for hitting a simple hw breakpoint
642 static struct test_struct hw_breakpoint_test[] = {
643 { "?", "S0*" }, /* Clear break points */
644 { "kgdbts_break_test", "OK", hw_break, }, /* set hw breakpoint */
645 { "c", "T0*", }, /* Continue */
646 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
647 { "write", "OK", write_regs },
648 { "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
649 { "D", "OK" }, /* Detach */
650 { "D", "OK", NULL, got_break }, /* On success we made it here */
651 { "", "" },
655 * Test for hitting a hw write breakpoint
657 static struct test_struct hw_write_break_test[] = {
658 { "?", "S0*" }, /* Clear break points */
659 { "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
660 { "c", "T0*", NULL, got_break }, /* Continue */
661 { "g", "silent", NULL, check_and_rewind_pc },
662 { "write", "OK", write_regs },
663 { "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
664 { "D", "OK" }, /* Detach */
665 { "D", "OK", NULL, got_break }, /* On success we made it here */
666 { "", "" },
670 * Test for hitting a hw access breakpoint
672 static struct test_struct hw_access_break_test[] = {
673 { "?", "S0*" }, /* Clear break points */
674 { "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
675 { "c", "T0*", NULL, got_break }, /* Continue */
676 { "g", "silent", NULL, check_and_rewind_pc },
677 { "write", "OK", write_regs },
678 { "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
679 { "D", "OK" }, /* Detach */
680 { "D", "OK", NULL, got_break }, /* On success we made it here */
681 { "", "" },
685 * Test for hitting a hw access breakpoint
687 static struct test_struct nmi_sleep_test[] = {
688 { "?", "S0*" }, /* Clear break points */
689 { "c", "T0*", NULL, got_break }, /* Continue */
690 { "D", "OK" }, /* Detach */
691 { "D", "OK", NULL, got_break }, /* On success we made it here */
692 { "", "" },
695 static void fill_get_buf(char *buf)
697 unsigned char checksum = 0;
698 int count = 0;
699 char ch;
701 strcpy(get_buf, "$");
702 strcat(get_buf, buf);
703 while ((ch = buf[count])) {
704 checksum += ch;
705 count++;
707 strcat(get_buf, "#");
708 get_buf[count + 2] = hex_asc_hi(checksum);
709 get_buf[count + 3] = hex_asc_lo(checksum);
710 get_buf[count + 4] = '\0';
711 v2printk("get%i: %s\n", ts.idx, get_buf);
714 static int validate_simple_test(char *put_str)
716 char *chk_str;
718 if (ts.tst[ts.idx].put_handler)
719 return ts.tst[ts.idx].put_handler(put_str,
720 ts.tst[ts.idx].put);
722 chk_str = ts.tst[ts.idx].put;
723 if (*put_str == '$')
724 put_str++;
726 while (*chk_str != '\0' && *put_str != '\0') {
727 /* If someone does a * to match the rest of the string, allow
728 * it, or stop if the received string is complete.
730 if (*put_str == '#' || *chk_str == '*')
731 return 0;
732 if (*put_str != *chk_str)
733 return 1;
735 chk_str++;
736 put_str++;
738 if (*chk_str == '\0' && (*put_str == '\0' || *put_str == '#'))
739 return 0;
741 return 1;
744 static int run_simple_test(int is_get_char, int chr)
746 int ret = 0;
747 if (is_get_char) {
748 /* Send an ACK on the get if a prior put completed and set the
749 * send ack variable
751 if (send_ack) {
752 send_ack = 0;
753 return '+';
755 /* On the first get char, fill the transmit buffer and then
756 * take from the get_string.
758 if (get_buf_cnt == 0) {
759 if (ts.tst[ts.idx].get_handler)
760 ts.tst[ts.idx].get_handler(ts.tst[ts.idx].get);
761 else
762 fill_get_buf(ts.tst[ts.idx].get);
765 if (get_buf[get_buf_cnt] == '\0') {
766 eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
767 ts.name, ts.idx);
768 get_buf_cnt = 0;
769 fill_get_buf("D");
771 ret = get_buf[get_buf_cnt];
772 get_buf_cnt++;
773 return ret;
776 /* This callback is a put char which is when kgdb sends data to
777 * this I/O module.
779 if (ts.tst[ts.idx].get[0] == '\0' && ts.tst[ts.idx].put[0] == '\0' &&
780 !ts.tst[ts.idx].get_handler) {
781 eprintk("kgdbts: ERROR: beyond end of test on"
782 " '%s' line %i\n", ts.name, ts.idx);
783 return 0;
786 if (put_buf_cnt >= BUFMAX) {
787 eprintk("kgdbts: ERROR: put buffer overflow on"
788 " '%s' line %i\n", ts.name, ts.idx);
789 put_buf_cnt = 0;
790 return 0;
792 /* Ignore everything until the first valid packet start '$' */
793 if (put_buf_cnt == 0 && chr != '$')
794 return 0;
796 put_buf[put_buf_cnt] = chr;
797 put_buf_cnt++;
799 /* End of packet == #XX so look for the '#' */
800 if (put_buf_cnt > 3 && put_buf[put_buf_cnt - 3] == '#') {
801 if (put_buf_cnt >= BUFMAX) {
802 eprintk("kgdbts: ERROR: put buffer overflow on"
803 " '%s' line %i\n", ts.name, ts.idx);
804 put_buf_cnt = 0;
805 return 0;
807 put_buf[put_buf_cnt] = '\0';
808 v2printk("put%i: %s\n", ts.idx, put_buf);
809 /* Trigger check here */
810 if (ts.validate_put && ts.validate_put(put_buf)) {
811 eprintk("kgdbts: ERROR PUT: end of test "
812 "buffer on '%s' line %i expected %s got %s\n",
813 ts.name, ts.idx, ts.tst[ts.idx].put, put_buf);
815 ts.idx++;
816 put_buf_cnt = 0;
817 get_buf_cnt = 0;
818 send_ack = 1;
820 return 0;
823 static void init_simple_test(void)
825 memset(&ts, 0, sizeof(ts));
826 ts.run_test = run_simple_test;
827 ts.validate_put = validate_simple_test;
830 static void run_plant_and_detach_test(int is_early)
832 char before[BREAK_INSTR_SIZE];
833 char after[BREAK_INSTR_SIZE];
835 probe_kernel_read(before, (char *)kgdbts_break_test,
836 BREAK_INSTR_SIZE);
837 init_simple_test();
838 ts.tst = plant_and_detach_test;
839 ts.name = "plant_and_detach_test";
840 /* Activate test with initial breakpoint */
841 if (!is_early)
842 kgdb_breakpoint();
843 probe_kernel_read(after, (char *)kgdbts_break_test,
844 BREAK_INSTR_SIZE);
845 if (memcmp(before, after, BREAK_INSTR_SIZE)) {
846 printk(KERN_CRIT "kgdbts: ERROR kgdb corrupted memory\n");
847 panic("kgdb memory corruption");
850 /* complete the detach test */
851 if (!is_early)
852 kgdbts_break_test();
855 static void run_breakpoint_test(int is_hw_breakpoint)
857 test_complete = 0;
858 init_simple_test();
859 if (is_hw_breakpoint) {
860 ts.tst = hw_breakpoint_test;
861 ts.name = "hw_breakpoint_test";
862 } else {
863 ts.tst = sw_breakpoint_test;
864 ts.name = "sw_breakpoint_test";
866 /* Activate test with initial breakpoint */
867 kgdb_breakpoint();
868 /* run code with the break point in it */
869 kgdbts_break_test();
870 kgdb_breakpoint();
872 if (test_complete)
873 return;
875 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
876 if (is_hw_breakpoint)
877 hwbreaks_ok = 0;
880 static void run_hw_break_test(int is_write_test)
882 test_complete = 0;
883 init_simple_test();
884 if (is_write_test) {
885 ts.tst = hw_write_break_test;
886 ts.name = "hw_write_break_test";
887 } else {
888 ts.tst = hw_access_break_test;
889 ts.name = "hw_access_break_test";
891 /* Activate test with initial breakpoint */
892 kgdb_breakpoint();
893 hw_break_val_access();
894 if (is_write_test) {
895 if (test_complete == 2) {
896 eprintk("kgdbts: ERROR %s broke on access\n",
897 ts.name);
898 hwbreaks_ok = 0;
900 hw_break_val_write();
902 kgdb_breakpoint();
904 if (test_complete == 1)
905 return;
907 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
908 hwbreaks_ok = 0;
911 static void run_nmi_sleep_test(int nmi_sleep)
913 unsigned long flags;
915 init_simple_test();
916 ts.tst = nmi_sleep_test;
917 ts.name = "nmi_sleep_test";
918 /* Activate test with initial breakpoint */
919 kgdb_breakpoint();
920 local_irq_save(flags);
921 mdelay(nmi_sleep*1000);
922 touch_nmi_watchdog();
923 local_irq_restore(flags);
924 if (test_complete != 2)
925 eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
926 kgdb_breakpoint();
927 if (test_complete == 1)
928 return;
930 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
933 static void run_bad_read_test(void)
935 init_simple_test();
936 ts.tst = bad_read_test;
937 ts.name = "bad_read_test";
938 /* Activate test with initial breakpoint */
939 kgdb_breakpoint();
942 static void run_do_fork_test(void)
944 init_simple_test();
945 ts.tst = do_fork_test;
946 ts.name = "do_fork_test";
947 /* Activate test with initial breakpoint */
948 kgdb_breakpoint();
951 static void run_sys_open_test(void)
953 init_simple_test();
954 ts.tst = sys_open_test;
955 ts.name = "sys_open_test";
956 /* Activate test with initial breakpoint */
957 kgdb_breakpoint();
960 static void run_singlestep_break_test(void)
962 init_simple_test();
963 ts.tst = singlestep_break_test;
964 ts.name = "singlestep_breakpoint_test";
965 /* Activate test with initial breakpoint */
966 kgdb_breakpoint();
967 kgdbts_break_test();
968 kgdbts_break_test();
971 static void kgdbts_run_tests(void)
973 char *ptr;
974 int fork_test = 0;
975 int do_sys_open_test = 0;
976 int sstep_test = 1000;
977 int nmi_sleep = 0;
978 int i;
980 ptr = strchr(config, 'F');
981 if (ptr)
982 fork_test = simple_strtol(ptr + 1, NULL, 10);
983 ptr = strchr(config, 'S');
984 if (ptr)
985 do_sys_open_test = simple_strtol(ptr + 1, NULL, 10);
986 ptr = strchr(config, 'N');
987 if (ptr)
988 nmi_sleep = simple_strtol(ptr+1, NULL, 10);
989 ptr = strchr(config, 'I');
990 if (ptr)
991 sstep_test = simple_strtol(ptr+1, NULL, 10);
993 /* All HW break point tests */
994 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) {
995 hwbreaks_ok = 1;
996 v1printk("kgdbts:RUN hw breakpoint test\n");
997 run_breakpoint_test(1);
998 v1printk("kgdbts:RUN hw write breakpoint test\n");
999 run_hw_break_test(1);
1000 v1printk("kgdbts:RUN access write breakpoint test\n");
1001 run_hw_break_test(0);
1004 /* required internal KGDB tests */
1005 v1printk("kgdbts:RUN plant and detach test\n");
1006 run_plant_and_detach_test(0);
1007 v1printk("kgdbts:RUN sw breakpoint test\n");
1008 run_breakpoint_test(0);
1009 v1printk("kgdbts:RUN bad memory access test\n");
1010 run_bad_read_test();
1011 v1printk("kgdbts:RUN singlestep test %i iterations\n", sstep_test);
1012 for (i = 0; i < sstep_test; i++) {
1013 run_singlestep_break_test();
1014 if (i % 100 == 0)
1015 v1printk("kgdbts:RUN singlestep [%i/%i]\n",
1016 i, sstep_test);
1019 /* ===Optional tests=== */
1021 if (nmi_sleep) {
1022 v1printk("kgdbts:RUN NMI sleep %i seconds test\n", nmi_sleep);
1023 run_nmi_sleep_test(nmi_sleep);
1026 /* If the do_fork test is run it will be the last test that is
1027 * executed because a kernel thread will be spawned at the very
1028 * end to unregister the debug hooks.
1030 if (fork_test) {
1031 repeat_test = fork_test;
1032 printk(KERN_INFO "kgdbts:RUN do_fork for %i breakpoints\n",
1033 repeat_test);
1034 kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
1035 run_do_fork_test();
1036 return;
1039 /* If the sys_open test is run it will be the last test that is
1040 * executed because a kernel thread will be spawned at the very
1041 * end to unregister the debug hooks.
1043 if (do_sys_open_test) {
1044 repeat_test = do_sys_open_test;
1045 printk(KERN_INFO "kgdbts:RUN sys_open for %i breakpoints\n",
1046 repeat_test);
1047 kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
1048 run_sys_open_test();
1049 return;
1051 /* Shutdown and unregister */
1052 kgdb_unregister_io_module(&kgdbts_io_ops);
1053 configured = 0;
1056 static int kgdbts_option_setup(char *opt)
1058 if (strlen(opt) >= MAX_CONFIG_LEN) {
1059 printk(KERN_ERR "kgdbts: config string too long\n");
1060 return -ENOSPC;
1062 strcpy(config, opt);
1064 verbose = 0;
1065 if (strstr(config, "V1"))
1066 verbose = 1;
1067 if (strstr(config, "V2"))
1068 verbose = 2;
1070 return 0;
1073 __setup("kgdbts=", kgdbts_option_setup);
1075 static int configure_kgdbts(void)
1077 int err = 0;
1079 if (!strlen(config) || isspace(config[0]))
1080 goto noconfig;
1081 err = kgdbts_option_setup(config);
1082 if (err)
1083 goto noconfig;
1085 final_ack = 0;
1086 run_plant_and_detach_test(1);
1088 err = kgdb_register_io_module(&kgdbts_io_ops);
1089 if (err) {
1090 configured = 0;
1091 return err;
1093 configured = 1;
1094 kgdbts_run_tests();
1096 return err;
1098 noconfig:
1099 config[0] = 0;
1100 configured = 0;
1102 return err;
1105 static int __init init_kgdbts(void)
1107 /* Already configured? */
1108 if (configured == 1)
1109 return 0;
1111 return configure_kgdbts();
1114 static int kgdbts_get_char(void)
1116 int val = 0;
1118 if (ts.run_test)
1119 val = ts.run_test(1, 0);
1121 return val;
1124 static void kgdbts_put_char(u8 chr)
1126 if (ts.run_test)
1127 ts.run_test(0, chr);
1130 static int param_set_kgdbts_var(const char *kmessage, struct kernel_param *kp)
1132 int len = strlen(kmessage);
1134 if (len >= MAX_CONFIG_LEN) {
1135 printk(KERN_ERR "kgdbts: config string too long\n");
1136 return -ENOSPC;
1139 /* Only copy in the string if the init function has not run yet */
1140 if (configured < 0) {
1141 strcpy(config, kmessage);
1142 return 0;
1145 if (configured == 1) {
1146 printk(KERN_ERR "kgdbts: ERROR: Already configured and running.\n");
1147 return -EBUSY;
1150 strcpy(config, kmessage);
1151 /* Chop out \n char as a result of echo */
1152 if (config[len - 1] == '\n')
1153 config[len - 1] = '\0';
1155 /* Go and configure with the new params. */
1156 return configure_kgdbts();
1159 static void kgdbts_pre_exp_handler(void)
1161 /* Increment the module count when the debugger is active */
1162 if (!kgdb_connected)
1163 try_module_get(THIS_MODULE);
1166 static void kgdbts_post_exp_handler(void)
1168 /* decrement the module count when the debugger detaches */
1169 if (!kgdb_connected)
1170 module_put(THIS_MODULE);
1173 static struct kgdb_io kgdbts_io_ops = {
1174 .name = "kgdbts",
1175 .read_char = kgdbts_get_char,
1176 .write_char = kgdbts_put_char,
1177 .pre_exception = kgdbts_pre_exp_handler,
1178 .post_exception = kgdbts_post_exp_handler,
1181 module_init(init_kgdbts);
1182 module_param_call(kgdbts, param_set_kgdbts_var, param_get_string, &kps, 0644);
1183 MODULE_PARM_DESC(kgdbts, "<A|V1|V2>[F#|S#][N#]");
1184 MODULE_DESCRIPTION("KGDB Test Suite");
1185 MODULE_LICENSE("GPL");
1186 MODULE_AUTHOR("Wind River Systems, Inc.");