97 #include <linux/kernel.h>
99 #include <linux/ctype.h>
105 #include <linux/module.h>
107 #define v1printk(a...) do { \
109 printk(KERN_INFO a); \
111 #define v2printk(a...) do { \
113 printk(KERN_INFO a); \
114 touch_nmi_watchdog(); \
116 #define eprintk(a...) do { \
117 printk(KERN_ERR a); \
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];
129 static int repeat_test;
130 static int test_complete;
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;
143 static int arch_needs_sstep_emulation;
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;
151 static unsigned long kgdbts_gdb_regs[(
NUMREGBYTES +
152 sizeof(
unsigned long) - 1) /
153 sizeof(
unsigned long)];
154 static struct pt_regs kgdbts_regs;
159 #ifdef CONFIG_KGDB_TESTS_BOOT_STRING
169 static void fill_get_buf(
char *
buf);
188 static int kgdbts_unreg_thread(
void *
ptr)
207 static noinline void kgdbts_break_test(
void)
209 v2printk(
"kgdbts: breakpoint complete\n");
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"))
221 else if (!
strcmp(arg,
"do_fork"))
223 else if (!
strcmp(arg,
"hw_break_val"))
224 addr = (
unsigned long)&hw_break_val;
228 static void break_helper(
char *bp_type,
char *arg,
unsigned long vaddr)
233 addr = lookup_addr(arg);
237 sprintf(scratch_buf,
"%s,%lx,%i", bp_type, addr,
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)
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')
302 static int check_and_rewind_pc(
char *put_str,
char *arg)
304 unsigned long addr = lookup_addr(arg);
312 v2printk(
"Stopped at IP: %lx\n", ip);
313 #ifdef GDB_ADJUSTS_BREAK_OFFSET
319 if (arch_needs_sstep_emulation && sstep_addr &&
320 ip + offset == sstep_addr &&
321 ((!
strcmp(arg,
"sys_open") || !
strcmp(arg,
"do_fork")))) {
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",
333 #ifdef GDB_ADJUSTS_BREAK_OFFSET
334 instruction_pointer_set(&kgdbts_regs, ip);
339 static int check_single_step(
char *put_str,
char *arg)
341 unsigned long addr = lookup_addr(arg);
342 static int matched_id;
351 v2printk(
"Singlestep stopped at IP: %lx\n",
354 if (sstep_thread_id != cont_thread_id) {
360 v2printk(
"ThrID does not match: %lx\n", cont_thread_id);
361 if (arch_needs_sstep_emulation) {
370 cont_instead_of_sstep = 1;
377 eprintk(
"kgdbts: SingleStep failed at %lx\n",
385 static void write_regs(
char *arg)
387 memset(scratch_buf, 0,
sizeof(scratch_buf));
388 scratch_buf[0] =
'G';
391 fill_get_buf(scratch_buf);
394 static void skip_back_repeat_test(
char *arg)
399 if (repeat_test <= 0)
403 fill_get_buf(
ts.tst[
ts.idx].get);
406 static int got_break(
char *put_str,
char *arg)
409 if (!
strncmp(put_str+1, arg, 2)) {
417 static void get_cont_catch(
char *arg)
423 static int put_cont_catch(
char *put_str,
char *arg)
426 v2printk(
"kgdbts: cleanup task: %lx\n", sstep_thread_id);
431 static int emul_reset(
char *put_str,
char *arg)
433 if (
strncmp(put_str,
"$OK", 3))
435 if (restart_from_top_after_write) {
436 restart_from_top_after_write = 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;
453 switch (sstep_state) {
461 break_helper(
"Z0",
NULL, sstep_addr);
469 break_helper(
"z0",
NULL, sstep_addr);
472 eprintk(
"kgdbts: ERROR failed sstep get emulation\n");
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')
486 switch (sstep_state) {
498 if (
strncmp(put_str,
"$OK", 3)) {
499 eprintk(
"kgdbts: failed sstep break set\n");
504 if (
strncmp(put_str,
"$T0", 3)) {
505 eprintk(
"kgdbts: failed continue sstep\n");
508 char *ptr = &put_str[11];
513 if (
strncmp(put_str,
"$OK", 3)) {
514 eprintk(
"kgdbts: failed sstep break unset\n");
521 eprintk(
"kgdbts: ERROR failed sstep put emulation\n");
529 static int final_ack_set(
char *put_str,
char *arg)
531 if (
strncmp(put_str+1, arg, 2))
540 static struct test_struct plant_and_detach_test[] = {
542 {
"kgdbts_break_test",
"OK", sw_break, },
553 {
"kgdbts_break_test",
"OK", sw_break, },
555 {
"g",
"kgdbts_break_test",
NULL, check_and_rewind_pc },
556 {
"write",
"OK", write_regs },
557 {
"kgdbts_break_test",
"OK", sw_rem_break },
559 {
"D",
"OK",
NULL, got_break },
585 static struct test_struct singlestep_break_test[] = {
587 {
"kgdbts_break_test",
"OK", sw_break, },
588 {
"c",
"T0*",
NULL, get_thread_id_continue },
589 {
"kgdbts_break_test",
"OK", sw_rem_break },
590 {
"g",
"kgdbts_break_test",
NULL, check_and_rewind_pc },
591 {
"write",
"OK", write_regs },
592 {
"s",
"T0*", emul_sstep_get, emul_sstep_put },
593 {
"g",
"kgdbts_break_test",
NULL, check_single_step },
594 {
"kgdbts_break_test",
"OK", sw_break, },
596 {
"g",
"kgdbts_break_test",
NULL, check_and_rewind_pc },
597 {
"write",
"OK", write_regs },
608 {
"do_fork",
"OK", sw_break, },
609 {
"c",
"T0*",
NULL, get_thread_id_continue },
610 {
"do_fork",
"OK", sw_rem_break },
611 {
"g",
"do_fork",
NULL, check_and_rewind_pc },
612 {
"write",
"OK", write_regs, emul_reset },
613 {
"s",
"T0*", emul_sstep_get, emul_sstep_put },
614 {
"g",
"do_fork",
NULL, check_single_step },
615 {
"do_fork",
"OK", sw_break, },
616 {
"7",
"T0*", skip_back_repeat_test },
617 {
"D",
"OK",
NULL, final_ack_set },
618 {
"",
"", get_cont_catch, put_cont_catch },
626 {
"sys_open",
"OK", sw_break, },
627 {
"c",
"T0*",
NULL, get_thread_id_continue },
628 {
"sys_open",
"OK", sw_rem_break },
629 {
"g",
"sys_open",
NULL, check_and_rewind_pc },
630 {
"write",
"OK", write_regs, emul_reset },
631 {
"s",
"T0*", emul_sstep_get, emul_sstep_put },
632 {
"g",
"sys_open",
NULL, check_single_step },
633 {
"sys_open",
"OK", sw_break, },
634 {
"7",
"T0*", skip_back_repeat_test },
635 {
"D",
"OK",
NULL, final_ack_set },
636 {
"",
"", get_cont_catch, put_cont_catch },
644 {
"kgdbts_break_test",
"OK", hw_break, },
646 {
"g",
"kgdbts_break_test",
NULL, check_and_rewind_pc },
647 {
"write",
"OK", write_regs },
648 {
"kgdbts_break_test",
"OK", hw_rem_break },
650 {
"D",
"OK",
NULL, got_break },
657 static struct test_struct hw_write_break_test[] = {
659 {
"hw_break_val",
"OK", hw_write_break, },
660 {
"c",
"T0*",
NULL, got_break },
661 {
"g",
"silent",
NULL, check_and_rewind_pc },
662 {
"write",
"OK", write_regs },
663 {
"hw_break_val",
"OK", hw_rem_write_break },
665 {
"D",
"OK",
NULL, got_break },
672 static struct test_struct hw_access_break_test[] = {
674 {
"hw_break_val",
"OK", hw_access_break, },
675 {
"c",
"T0*",
NULL, got_break },
676 {
"g",
"silent",
NULL, check_and_rewind_pc },
677 {
"write",
"OK", write_regs },
678 {
"hw_break_val",
"OK", hw_rem_access_break },
680 {
"D",
"OK",
NULL, got_break },
689 {
"c",
"T0*",
NULL, got_break },
691 {
"D",
"OK",
NULL, got_break },
695 static void fill_get_buf(
char *
buf)
703 while ((ch = buf[count])) {
710 get_buf[count + 4] =
'\0';
714 static int validate_simple_test(
char *put_str)
718 if (
ts.tst[
ts.idx].put_handler)
719 return ts.tst[
ts.idx].put_handler(put_str,
722 chk_str =
ts.tst[
ts.idx].put;
726 while (*chk_str !=
'\0' && *put_str !=
'\0') {
730 if (*put_str ==
'#' || *chk_str ==
'*')
732 if (*put_str != *chk_str)
738 if (*chk_str ==
'\0' && (*put_str ==
'\0' || *put_str ==
'#'))
744 static int run_simple_test(
int is_get_char,
int chr)
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);
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",
771 ret = get_buf[get_buf_cnt];
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);
786 if (put_buf_cnt >=
BUFMAX) {
787 eprintk(
"kgdbts: ERROR: put buffer overflow on"
788 " '%s' line %i\n",
ts.name,
ts.idx);
793 if (put_buf_cnt == 0 && chr !=
'$')
796 put_buf[put_buf_cnt] = chr;
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);
807 put_buf[put_buf_cnt] =
'\0';
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);
823 static void init_simple_test(
void)
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)
838 ts.tst = plant_and_detach_test;
839 ts.name =
"plant_and_detach_test";
845 if (
memcmp(before, after, BREAK_INSTR_SIZE)) {
847 panic(
"kgdb memory corruption");
855 static void run_breakpoint_test(
int is_hw_breakpoint)
859 if (is_hw_breakpoint) {
860 ts.tst = hw_breakpoint_test;
861 ts.name =
"hw_breakpoint_test";
863 ts.tst = sw_breakpoint_test;
864 ts.name =
"sw_breakpoint_test";
875 eprintk(
"kgdbts: ERROR %s test failed\n",
ts.name);
876 if (is_hw_breakpoint)
880 static void run_hw_break_test(
int is_write_test)
885 ts.tst = hw_write_break_test;
886 ts.name =
"hw_write_break_test";
888 ts.tst = hw_access_break_test;
889 ts.name =
"hw_access_break_test";
893 hw_break_val_access();
895 if (test_complete == 2) {
896 eprintk(
"kgdbts: ERROR %s broke on access\n",
900 hw_break_val_write();
904 if (test_complete == 1)
907 eprintk(
"kgdbts: ERROR %s test failed\n",
ts.name);
911 static void run_nmi_sleep_test(
int nmi_sleep)
916 ts.tst = nmi_sleep_test;
917 ts.name =
"nmi_sleep_test";
924 if (test_complete != 2)
925 eprintk(
"kgdbts: ERROR nmi_test did not hit nmi\n");
927 if (test_complete == 1)
930 eprintk(
"kgdbts: ERROR %s test failed\n",
ts.name);
933 static void run_bad_read_test(
void)
936 ts.tst = bad_read_test;
937 ts.name =
"bad_read_test";
942 static void run_do_fork_test(
void)
945 ts.tst = do_fork_test;
946 ts.name =
"do_fork_test";
951 static void run_sys_open_test(
void)
954 ts.tst = sys_open_test;
955 ts.name =
"sys_open_test";
960 static void run_singlestep_break_test(
void)
963 ts.tst = singlestep_break_test;
964 ts.name =
"singlestep_breakpoint_test";
971 static void kgdbts_run_tests(
void)
975 int do_sys_open_test = 0;
976 int sstep_test = 1000;
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);
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();
1015 v1printk(
"kgdbts:RUN singlestep [%i/%i]\n",
1022 v1printk(
"kgdbts:RUN NMI sleep %i seconds test\n", nmi_sleep);
1023 run_nmi_sleep_test(nmi_sleep);
1031 repeat_test = fork_test;
1043 if (do_sys_open_test) {
1044 repeat_test = do_sys_open_test;
1048 run_sys_open_test();
1056 static int kgdbts_option_setup(
char *
opt)
1073 __setup(
"kgdbts=", kgdbts_option_setup);
1075 static int configure_kgdbts(
void)
1081 err = kgdbts_option_setup(
config);
1086 run_plant_and_detach_test(1);
1105 static int __init init_kgdbts(
void)
1111 return configure_kgdbts();
1114 static int kgdbts_get_char(
void)
1119 val =
ts.run_test(1, 0);
1124 static void kgdbts_put_char(
u8 chr)
1127 ts.run_test(0, chr);
1130 static int param_set_kgdbts_var(
const char *kmessage,
struct kernel_param *kp)
1146 printk(
KERN_ERR "kgdbts: ERROR: Already configured and running.\n");
1152 if (
config[len - 1] ==
'\n')
1156 return configure_kgdbts();
1159 static void kgdbts_pre_exp_handler(
void)
1166 static void kgdbts_post_exp_handler(
void)
1173 static struct kgdb_io kgdbts_io_ops = {
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,