0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 #include <linux/kernel.h>
0086 #include <linux/kgdb.h>
0087 #include <linux/ctype.h>
0088 #include <linux/uaccess.h>
0089 #include <linux/syscalls.h>
0090 #include <linux/nmi.h>
0091 #include <linux/delay.h>
0092 #include <linux/kthread.h>
0093 #include <linux/module.h>
0094 #include <linux/sched/task.h>
0095 #include <linux/kallsyms.h>
0096
0097 #include <asm/sections.h>
0098
0099 #define v1printk(a...) do { \
0100 if (verbose) \
0101 printk(KERN_INFO a); \
0102 } while (0)
0103 #define v2printk(a...) do { \
0104 if (verbose > 1) { \
0105 printk(KERN_INFO a); \
0106 } \
0107 touch_nmi_watchdog(); \
0108 } while (0)
0109 #define eprintk(a...) do { \
0110 printk(KERN_ERR a); \
0111 WARN_ON(1); \
0112 } while (0)
0113 #define MAX_CONFIG_LEN 40
0114
0115 static struct kgdb_io kgdbts_io_ops;
0116 static char get_buf[BUFMAX];
0117 static int get_buf_cnt;
0118 static char put_buf[BUFMAX];
0119 static int put_buf_cnt;
0120 static char scratch_buf[BUFMAX];
0121 static int verbose;
0122 static int repeat_test;
0123 static int test_complete;
0124 static int send_ack;
0125 static int final_ack;
0126 static int force_hwbrks;
0127 static int hwbreaks_ok;
0128 static int hw_break_val;
0129 static int hw_break_val2;
0130 static int cont_instead_of_sstep;
0131 static unsigned long cont_thread_id;
0132 static unsigned long sstep_thread_id;
0133 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_SPARC)
0134 static int arch_needs_sstep_emulation = 1;
0135 #else
0136 static int arch_needs_sstep_emulation;
0137 #endif
0138 static unsigned long cont_addr;
0139 static unsigned long sstep_addr;
0140 static int restart_from_top_after_write;
0141 static int sstep_state;
0142
0143
0144 static unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
0145 sizeof(unsigned long) - 1) /
0146 sizeof(unsigned long)];
0147 static struct pt_regs kgdbts_regs;
0148
0149
0150 static int configured = -1;
0151
0152 #ifdef CONFIG_KGDB_TESTS_BOOT_STRING
0153 static char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
0154 #else
0155 static char config[MAX_CONFIG_LEN];
0156 #endif
0157 static struct kparam_string kps = {
0158 .string = config,
0159 .maxlen = MAX_CONFIG_LEN,
0160 };
0161
0162 static void fill_get_buf(char *buf);
0163
0164 struct test_struct {
0165 char *get;
0166 char *put;
0167 void (*get_handler)(char *);
0168 int (*put_handler)(char *, char *);
0169 };
0170
0171 struct test_state {
0172 char *name;
0173 struct test_struct *tst;
0174 int idx;
0175 int (*run_test) (int, int);
0176 int (*validate_put) (char *);
0177 };
0178
0179 static struct test_state ts;
0180
0181 static int kgdbts_unreg_thread(void *ptr)
0182 {
0183
0184
0185
0186 while (!final_ack)
0187 msleep_interruptible(1500);
0188
0189 msleep_interruptible(1000);
0190 if (configured)
0191 kgdb_unregister_io_module(&kgdbts_io_ops);
0192 configured = 0;
0193
0194 return 0;
0195 }
0196
0197
0198
0199
0200 static noinline void kgdbts_break_test(void)
0201 {
0202 v2printk("kgdbts: breakpoint complete\n");
0203 }
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 static unsigned long lookup_addr(char *arg)
0218 {
0219 static char cached_arg[KSYM_NAME_LEN];
0220 static unsigned long cached_addr;
0221
0222 if (strcmp(arg, cached_arg)) {
0223 strscpy(cached_arg, arg, KSYM_NAME_LEN);
0224 cached_addr = kallsyms_lookup_name(arg);
0225 }
0226
0227 return (unsigned long)dereference_function_descriptor(
0228 (void *)cached_addr);
0229 }
0230
0231 static void break_helper(char *bp_type, char *arg, unsigned long vaddr)
0232 {
0233 unsigned long addr;
0234
0235 if (arg)
0236 addr = lookup_addr(arg);
0237 else
0238 addr = vaddr;
0239
0240 sprintf(scratch_buf, "%s,%lx,%i", bp_type, addr,
0241 BREAK_INSTR_SIZE);
0242 fill_get_buf(scratch_buf);
0243 }
0244
0245 static void sw_break(char *arg)
0246 {
0247 break_helper(force_hwbrks ? "Z1" : "Z0", arg, 0);
0248 }
0249
0250 static void sw_rem_break(char *arg)
0251 {
0252 break_helper(force_hwbrks ? "z1" : "z0", arg, 0);
0253 }
0254
0255 static void hw_break(char *arg)
0256 {
0257 break_helper("Z1", arg, 0);
0258 }
0259
0260 static void hw_rem_break(char *arg)
0261 {
0262 break_helper("z1", arg, 0);
0263 }
0264
0265 static void hw_write_break(char *arg)
0266 {
0267 break_helper("Z2", arg, 0);
0268 }
0269
0270 static void hw_rem_write_break(char *arg)
0271 {
0272 break_helper("z2", arg, 0);
0273 }
0274
0275 static void hw_access_break(char *arg)
0276 {
0277 break_helper("Z4", arg, 0);
0278 }
0279
0280 static void hw_rem_access_break(char *arg)
0281 {
0282 break_helper("z4", arg, 0);
0283 }
0284
0285 static void hw_break_val_access(void)
0286 {
0287 hw_break_val2 = hw_break_val;
0288 }
0289
0290 static void hw_break_val_write(void)
0291 {
0292 hw_break_val++;
0293 }
0294
0295 static int get_thread_id_continue(char *put_str, char *arg)
0296 {
0297 char *ptr = &put_str[11];
0298
0299 if (put_str[1] != 'T' || put_str[2] != '0')
0300 return 1;
0301 kgdb_hex2long(&ptr, &cont_thread_id);
0302 return 0;
0303 }
0304
0305 static int check_and_rewind_pc(char *put_str, char *arg)
0306 {
0307 unsigned long addr = lookup_addr(arg);
0308 unsigned long ip;
0309 int offset = 0;
0310
0311 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
0312 NUMREGBYTES);
0313 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
0314 ip = instruction_pointer(&kgdbts_regs);
0315 v2printk("Stopped at IP: %lx\n", ip);
0316 #ifdef GDB_ADJUSTS_BREAK_OFFSET
0317
0318 if (addr + BREAK_INSTR_SIZE == ip)
0319 offset = -BREAK_INSTR_SIZE;
0320 #endif
0321
0322 if (arch_needs_sstep_emulation && sstep_addr &&
0323 ip + offset == sstep_addr &&
0324 ((!strcmp(arg, "do_sys_openat2") || !strcmp(arg, "kernel_clone")))) {
0325
0326 v2printk("Emul: rewind hit single step bp\n");
0327 restart_from_top_after_write = 1;
0328 } else if (strcmp(arg, "silent") && ip + offset != addr) {
0329 eprintk("kgdbts: BP mismatch %lx expected %lx\n",
0330 ip + offset, addr);
0331 return 1;
0332 }
0333
0334 ip += offset;
0335 cont_addr = ip;
0336 #ifdef GDB_ADJUSTS_BREAK_OFFSET
0337 instruction_pointer_set(&kgdbts_regs, ip);
0338 #endif
0339 return 0;
0340 }
0341
0342 static int check_single_step(char *put_str, char *arg)
0343 {
0344 unsigned long addr = lookup_addr(arg);
0345 static int matched_id;
0346
0347
0348
0349
0350
0351 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
0352 NUMREGBYTES);
0353 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
0354 v2printk("Singlestep stopped at IP: %lx\n",
0355 instruction_pointer(&kgdbts_regs));
0356
0357 if (sstep_thread_id != cont_thread_id) {
0358
0359
0360
0361
0362
0363 v2printk("ThrID does not match: %lx\n", cont_thread_id);
0364 if (arch_needs_sstep_emulation) {
0365 if (matched_id &&
0366 instruction_pointer(&kgdbts_regs) != addr)
0367 goto continue_test;
0368 matched_id++;
0369 ts.idx -= 2;
0370 sstep_state = 0;
0371 return 0;
0372 }
0373 cont_instead_of_sstep = 1;
0374 ts.idx -= 4;
0375 return 0;
0376 }
0377 continue_test:
0378 matched_id = 0;
0379 if (instruction_pointer(&kgdbts_regs) == addr) {
0380 eprintk("kgdbts: SingleStep failed at %lx\n",
0381 instruction_pointer(&kgdbts_regs));
0382 return 1;
0383 }
0384
0385 return 0;
0386 }
0387
0388 static void write_regs(char *arg)
0389 {
0390 memset(scratch_buf, 0, sizeof(scratch_buf));
0391 scratch_buf[0] = 'G';
0392 pt_regs_to_gdb_regs(kgdbts_gdb_regs, &kgdbts_regs);
0393 kgdb_mem2hex((char *)kgdbts_gdb_regs, &scratch_buf[1], NUMREGBYTES);
0394 fill_get_buf(scratch_buf);
0395 }
0396
0397 static void skip_back_repeat_test(char *arg)
0398 {
0399 int go_back = simple_strtol(arg, NULL, 10);
0400
0401 repeat_test--;
0402 if (repeat_test <= 0) {
0403 ts.idx++;
0404 } else {
0405 if (repeat_test % 100 == 0)
0406 v1printk("kgdbts:RUN ... %d remaining\n", repeat_test);
0407
0408 ts.idx -= go_back;
0409 }
0410 fill_get_buf(ts.tst[ts.idx].get);
0411 }
0412
0413 static int got_break(char *put_str, char *arg)
0414 {
0415 test_complete = 1;
0416 if (!strncmp(put_str+1, arg, 2)) {
0417 if (!strncmp(arg, "T0", 2))
0418 test_complete = 2;
0419 return 0;
0420 }
0421 return 1;
0422 }
0423
0424 static void get_cont_catch(char *arg)
0425 {
0426
0427 fill_get_buf("D");
0428 }
0429
0430 static int put_cont_catch(char *put_str, char *arg)
0431 {
0432
0433 v2printk("kgdbts: cleanup task: %lx\n", sstep_thread_id);
0434 ts.idx--;
0435 return 0;
0436 }
0437
0438 static int emul_reset(char *put_str, char *arg)
0439 {
0440 if (strncmp(put_str, "$OK", 3))
0441 return 1;
0442 if (restart_from_top_after_write) {
0443 restart_from_top_after_write = 0;
0444 ts.idx = -1;
0445 }
0446 return 0;
0447 }
0448
0449 static void emul_sstep_get(char *arg)
0450 {
0451 if (!arch_needs_sstep_emulation) {
0452 if (cont_instead_of_sstep) {
0453 cont_instead_of_sstep = 0;
0454 fill_get_buf("c");
0455 } else {
0456 fill_get_buf(arg);
0457 }
0458 return;
0459 }
0460 switch (sstep_state) {
0461 case 0:
0462 v2printk("Emulate single step\n");
0463
0464 fill_get_buf("g");
0465 break;
0466 case 1:
0467
0468 break_helper("Z0", NULL, sstep_addr);
0469 break;
0470 case 2:
0471
0472 fill_get_buf("c");
0473 break;
0474 case 3:
0475
0476 break_helper("z0", NULL, sstep_addr);
0477 break;
0478 default:
0479 eprintk("kgdbts: ERROR failed sstep get emulation\n");
0480 }
0481 sstep_state++;
0482 }
0483
0484 static int emul_sstep_put(char *put_str, char *arg)
0485 {
0486 if (!arch_needs_sstep_emulation) {
0487 char *ptr = &put_str[11];
0488 if (put_str[1] != 'T' || put_str[2] != '0')
0489 return 1;
0490 kgdb_hex2long(&ptr, &sstep_thread_id);
0491 return 0;
0492 }
0493 switch (sstep_state) {
0494 case 1:
0495
0496 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
0497 NUMREGBYTES);
0498 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
0499 v2printk("Stopped at IP: %lx\n",
0500 instruction_pointer(&kgdbts_regs));
0501
0502 sstep_addr = cont_addr + BREAK_INSTR_SIZE;
0503 break;
0504 case 2:
0505 if (strncmp(put_str, "$OK", 3)) {
0506 eprintk("kgdbts: failed sstep break set\n");
0507 return 1;
0508 }
0509 break;
0510 case 3:
0511 if (strncmp(put_str, "$T0", 3)) {
0512 eprintk("kgdbts: failed continue sstep\n");
0513 return 1;
0514 } else {
0515 char *ptr = &put_str[11];
0516 kgdb_hex2long(&ptr, &sstep_thread_id);
0517 }
0518 break;
0519 case 4:
0520 if (strncmp(put_str, "$OK", 3)) {
0521 eprintk("kgdbts: failed sstep break unset\n");
0522 return 1;
0523 }
0524
0525 sstep_state = 0;
0526 return 0;
0527 default:
0528 eprintk("kgdbts: ERROR failed sstep put emulation\n");
0529 }
0530
0531
0532 ts.idx--;
0533 return 0;
0534 }
0535
0536 static int final_ack_set(char *put_str, char *arg)
0537 {
0538 if (strncmp(put_str+1, arg, 2))
0539 return 1;
0540 final_ack = 1;
0541 return 0;
0542 }
0543
0544
0545
0546
0547 static struct test_struct plant_and_detach_test[] = {
0548 { "?", "S0*" },
0549 { "kgdbts_break_test", "OK", sw_break, },
0550 { "D", "OK" },
0551 { "", "" },
0552 };
0553
0554
0555
0556
0557
0558 static struct test_struct sw_breakpoint_test[] = {
0559 { "?", "S0*" },
0560 { "kgdbts_break_test", "OK", sw_break, },
0561 { "c", "T0*", },
0562 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0563 { "write", "OK", write_regs },
0564 { "kgdbts_break_test", "OK", sw_rem_break },
0565 { "D", "OK" },
0566 { "D", "OK", NULL, got_break },
0567 { "", "" },
0568 };
0569
0570
0571
0572
0573
0574 static struct test_struct bad_read_test[] = {
0575 { "?", "S0*" },
0576 { "m0,1", "E*" },
0577 { "m0,2", "E*" },
0578 { "m0,3", "E*" },
0579 { "m0,4", "E*" },
0580 { "m0,5", "E*" },
0581 { "m0,6", "E*" },
0582 { "m0,7", "E*" },
0583 { "m0,8", "E*" },
0584 { "D", "OK" },
0585 { "", "" },
0586 };
0587
0588
0589
0590
0591
0592 static struct test_struct singlestep_break_test[] = {
0593 { "?", "S0*" },
0594 { "kgdbts_break_test", "OK", sw_break, },
0595 { "c", "T0*", NULL, get_thread_id_continue },
0596 { "kgdbts_break_test", "OK", sw_rem_break },
0597 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0598 { "write", "OK", write_regs },
0599 { "s", "T0*", emul_sstep_get, emul_sstep_put },
0600 { "g", "kgdbts_break_test", NULL, check_single_step },
0601 { "kgdbts_break_test", "OK", sw_break, },
0602 { "c", "T0*", },
0603 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0604 { "write", "OK", write_regs },
0605 { "D", "OK" },
0606 { "", "" },
0607 };
0608
0609
0610
0611
0612
0613 static struct test_struct do_kernel_clone_test[] = {
0614 { "?", "S0*" },
0615 { "kernel_clone", "OK", sw_break, },
0616 { "c", "T0*", NULL, get_thread_id_continue },
0617 { "kernel_clone", "OK", sw_rem_break },
0618 { "g", "kernel_clone", NULL, check_and_rewind_pc },
0619 { "write", "OK", write_regs, emul_reset },
0620 { "s", "T0*", emul_sstep_get, emul_sstep_put },
0621 { "g", "kernel_clone", NULL, check_single_step },
0622 { "kernel_clone", "OK", sw_break, },
0623 { "7", "T0*", skip_back_repeat_test },
0624 { "D", "OK", NULL, final_ack_set },
0625 { "", "", get_cont_catch, put_cont_catch },
0626 };
0627
0628
0629
0630
0631 static struct test_struct sys_open_test[] = {
0632 { "?", "S0*" },
0633 { "do_sys_openat2", "OK", sw_break, },
0634 { "c", "T0*", NULL, get_thread_id_continue },
0635 { "do_sys_openat2", "OK", sw_rem_break },
0636 { "g", "do_sys_openat2", NULL, check_and_rewind_pc },
0637 { "write", "OK", write_regs, emul_reset },
0638 { "s", "T0*", emul_sstep_get, emul_sstep_put },
0639 { "g", "do_sys_openat2", NULL, check_single_step },
0640 { "do_sys_openat2", "OK", sw_break, },
0641 { "7", "T0*", skip_back_repeat_test },
0642 { "D", "OK", NULL, final_ack_set },
0643 { "", "", get_cont_catch, put_cont_catch },
0644 };
0645
0646
0647
0648
0649 static struct test_struct hw_breakpoint_test[] = {
0650 { "?", "S0*" },
0651 { "kgdbts_break_test", "OK", hw_break, },
0652 { "c", "T0*", },
0653 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0654 { "write", "OK", write_regs },
0655 { "kgdbts_break_test", "OK", hw_rem_break },
0656 { "D", "OK" },
0657 { "D", "OK", NULL, got_break },
0658 { "", "" },
0659 };
0660
0661
0662
0663
0664 static struct test_struct hw_write_break_test[] = {
0665 { "?", "S0*" },
0666 { "hw_break_val", "OK", hw_write_break, },
0667 { "c", "T0*", NULL, got_break },
0668 { "g", "silent", NULL, check_and_rewind_pc },
0669 { "write", "OK", write_regs },
0670 { "hw_break_val", "OK", hw_rem_write_break },
0671 { "D", "OK" },
0672 { "D", "OK", NULL, got_break },
0673 { "", "" },
0674 };
0675
0676
0677
0678
0679 static struct test_struct hw_access_break_test[] = {
0680 { "?", "S0*" },
0681 { "hw_break_val", "OK", hw_access_break, },
0682 { "c", "T0*", NULL, got_break },
0683 { "g", "silent", NULL, check_and_rewind_pc },
0684 { "write", "OK", write_regs },
0685 { "hw_break_val", "OK", hw_rem_access_break },
0686 { "D", "OK" },
0687 { "D", "OK", NULL, got_break },
0688 { "", "" },
0689 };
0690
0691
0692
0693
0694 static struct test_struct nmi_sleep_test[] = {
0695 { "?", "S0*" },
0696 { "c", "T0*", NULL, got_break },
0697 { "D", "OK" },
0698 { "D", "OK", NULL, got_break },
0699 { "", "" },
0700 };
0701
0702 static void fill_get_buf(char *buf)
0703 {
0704 unsigned char checksum = 0;
0705 int count = 0;
0706 char ch;
0707
0708 strcpy(get_buf, "$");
0709 strcat(get_buf, buf);
0710 while ((ch = buf[count])) {
0711 checksum += ch;
0712 count++;
0713 }
0714 strcat(get_buf, "#");
0715 get_buf[count + 2] = hex_asc_hi(checksum);
0716 get_buf[count + 3] = hex_asc_lo(checksum);
0717 get_buf[count + 4] = '\0';
0718 v2printk("get%i: %s\n", ts.idx, get_buf);
0719 }
0720
0721 static int validate_simple_test(char *put_str)
0722 {
0723 char *chk_str;
0724
0725 if (ts.tst[ts.idx].put_handler)
0726 return ts.tst[ts.idx].put_handler(put_str,
0727 ts.tst[ts.idx].put);
0728
0729 chk_str = ts.tst[ts.idx].put;
0730 if (*put_str == '$')
0731 put_str++;
0732
0733 while (*chk_str != '\0' && *put_str != '\0') {
0734
0735
0736
0737 if (*put_str == '#' || *chk_str == '*')
0738 return 0;
0739 if (*put_str != *chk_str)
0740 return 1;
0741
0742 chk_str++;
0743 put_str++;
0744 }
0745 if (*chk_str == '\0' && (*put_str == '\0' || *put_str == '#'))
0746 return 0;
0747
0748 return 1;
0749 }
0750
0751 static int run_simple_test(int is_get_char, int chr)
0752 {
0753 int ret = 0;
0754 if (is_get_char) {
0755
0756
0757
0758 if (send_ack) {
0759 send_ack = 0;
0760 return '+';
0761 }
0762
0763
0764
0765 if (get_buf_cnt == 0) {
0766 if (ts.tst[ts.idx].get_handler)
0767 ts.tst[ts.idx].get_handler(ts.tst[ts.idx].get);
0768 else
0769 fill_get_buf(ts.tst[ts.idx].get);
0770 }
0771
0772 if (get_buf[get_buf_cnt] == '\0') {
0773 eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
0774 ts.name, ts.idx);
0775 get_buf_cnt = 0;
0776 fill_get_buf("D");
0777 }
0778 ret = get_buf[get_buf_cnt];
0779 get_buf_cnt++;
0780 return ret;
0781 }
0782
0783
0784
0785
0786 if (ts.tst[ts.idx].get[0] == '\0' && ts.tst[ts.idx].put[0] == '\0' &&
0787 !ts.tst[ts.idx].get_handler) {
0788 eprintk("kgdbts: ERROR: beyond end of test on"
0789 " '%s' line %i\n", ts.name, ts.idx);
0790 return 0;
0791 }
0792
0793 if (put_buf_cnt >= BUFMAX) {
0794 eprintk("kgdbts: ERROR: put buffer overflow on"
0795 " '%s' line %i\n", ts.name, ts.idx);
0796 put_buf_cnt = 0;
0797 return 0;
0798 }
0799
0800 if (put_buf_cnt == 0 && chr != '$')
0801 return 0;
0802
0803 put_buf[put_buf_cnt] = chr;
0804 put_buf_cnt++;
0805
0806
0807 if (put_buf_cnt > 3 && put_buf[put_buf_cnt - 3] == '#') {
0808 if (put_buf_cnt >= BUFMAX) {
0809 eprintk("kgdbts: ERROR: put buffer overflow on"
0810 " '%s' line %i\n", ts.name, ts.idx);
0811 put_buf_cnt = 0;
0812 return 0;
0813 }
0814 put_buf[put_buf_cnt] = '\0';
0815 v2printk("put%i: %s\n", ts.idx, put_buf);
0816
0817 if (ts.validate_put && ts.validate_put(put_buf)) {
0818 eprintk("kgdbts: ERROR PUT: end of test "
0819 "buffer on '%s' line %i expected %s got %s\n",
0820 ts.name, ts.idx, ts.tst[ts.idx].put, put_buf);
0821 }
0822 ts.idx++;
0823 put_buf_cnt = 0;
0824 get_buf_cnt = 0;
0825 send_ack = 1;
0826 }
0827 return 0;
0828 }
0829
0830 static void init_simple_test(void)
0831 {
0832 memset(&ts, 0, sizeof(ts));
0833 ts.run_test = run_simple_test;
0834 ts.validate_put = validate_simple_test;
0835 }
0836
0837 static void run_plant_and_detach_test(int is_early)
0838 {
0839 char before[BREAK_INSTR_SIZE];
0840 char after[BREAK_INSTR_SIZE];
0841
0842 copy_from_kernel_nofault(before, (char *)kgdbts_break_test,
0843 BREAK_INSTR_SIZE);
0844 init_simple_test();
0845 ts.tst = plant_and_detach_test;
0846 ts.name = "plant_and_detach_test";
0847
0848 if (!is_early)
0849 kgdb_breakpoint();
0850 copy_from_kernel_nofault(after, (char *)kgdbts_break_test,
0851 BREAK_INSTR_SIZE);
0852 if (memcmp(before, after, BREAK_INSTR_SIZE)) {
0853 printk(KERN_CRIT "kgdbts: ERROR kgdb corrupted memory\n");
0854 panic("kgdb memory corruption");
0855 }
0856
0857
0858 if (!is_early)
0859 kgdbts_break_test();
0860 }
0861
0862 static void run_breakpoint_test(int is_hw_breakpoint)
0863 {
0864 test_complete = 0;
0865 init_simple_test();
0866 if (is_hw_breakpoint) {
0867 ts.tst = hw_breakpoint_test;
0868 ts.name = "hw_breakpoint_test";
0869 } else {
0870 ts.tst = sw_breakpoint_test;
0871 ts.name = "sw_breakpoint_test";
0872 }
0873
0874 kgdb_breakpoint();
0875
0876 kgdbts_break_test();
0877 kgdb_breakpoint();
0878
0879 if (test_complete)
0880 return;
0881
0882 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
0883 if (is_hw_breakpoint)
0884 hwbreaks_ok = 0;
0885 }
0886
0887 static void run_hw_break_test(int is_write_test)
0888 {
0889 test_complete = 0;
0890 init_simple_test();
0891 if (is_write_test) {
0892 ts.tst = hw_write_break_test;
0893 ts.name = "hw_write_break_test";
0894 } else {
0895 ts.tst = hw_access_break_test;
0896 ts.name = "hw_access_break_test";
0897 }
0898
0899 kgdb_breakpoint();
0900 hw_break_val_access();
0901 if (is_write_test) {
0902 if (test_complete == 2) {
0903 eprintk("kgdbts: ERROR %s broke on access\n",
0904 ts.name);
0905 hwbreaks_ok = 0;
0906 }
0907 hw_break_val_write();
0908 }
0909 kgdb_breakpoint();
0910
0911 if (test_complete == 1)
0912 return;
0913
0914 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
0915 hwbreaks_ok = 0;
0916 }
0917
0918 static void run_nmi_sleep_test(int nmi_sleep)
0919 {
0920 unsigned long flags;
0921
0922 init_simple_test();
0923 ts.tst = nmi_sleep_test;
0924 ts.name = "nmi_sleep_test";
0925
0926 kgdb_breakpoint();
0927 local_irq_save(flags);
0928 mdelay(nmi_sleep*1000);
0929 touch_nmi_watchdog();
0930 local_irq_restore(flags);
0931 if (test_complete != 2)
0932 eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
0933 kgdb_breakpoint();
0934 if (test_complete == 1)
0935 return;
0936
0937 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
0938 }
0939
0940 static void run_bad_read_test(void)
0941 {
0942 init_simple_test();
0943 ts.tst = bad_read_test;
0944 ts.name = "bad_read_test";
0945
0946 kgdb_breakpoint();
0947 }
0948
0949 static void run_kernel_clone_test(void)
0950 {
0951 init_simple_test();
0952 ts.tst = do_kernel_clone_test;
0953 ts.name = "do_kernel_clone_test";
0954
0955 kgdb_breakpoint();
0956 }
0957
0958 static void run_sys_open_test(void)
0959 {
0960 init_simple_test();
0961 ts.tst = sys_open_test;
0962 ts.name = "sys_open_test";
0963
0964 kgdb_breakpoint();
0965 }
0966
0967 static void run_singlestep_break_test(void)
0968 {
0969 init_simple_test();
0970 ts.tst = singlestep_break_test;
0971 ts.name = "singlestep_breakpoint_test";
0972
0973 kgdb_breakpoint();
0974 kgdbts_break_test();
0975 kgdbts_break_test();
0976 }
0977
0978 static void kgdbts_run_tests(void)
0979 {
0980 char *ptr;
0981 int clone_test = 0;
0982 int do_sys_open_test = 0;
0983 int sstep_test = 1000;
0984 int nmi_sleep = 0;
0985 int i;
0986
0987 verbose = 0;
0988 if (strstr(config, "V1"))
0989 verbose = 1;
0990 if (strstr(config, "V2"))
0991 verbose = 2;
0992
0993 ptr = strchr(config, 'F');
0994 if (ptr)
0995 clone_test = simple_strtol(ptr + 1, NULL, 10);
0996 ptr = strchr(config, 'S');
0997 if (ptr)
0998 do_sys_open_test = simple_strtol(ptr + 1, NULL, 10);
0999 ptr = strchr(config, 'N');
1000 if (ptr)
1001 nmi_sleep = simple_strtol(ptr+1, NULL, 10);
1002 ptr = strchr(config, 'I');
1003 if (ptr)
1004 sstep_test = simple_strtol(ptr+1, NULL, 10);
1005
1006
1007 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) {
1008 hwbreaks_ok = 1;
1009 v1printk("kgdbts:RUN hw breakpoint test\n");
1010 run_breakpoint_test(1);
1011 v1printk("kgdbts:RUN hw write breakpoint test\n");
1012 run_hw_break_test(1);
1013 v1printk("kgdbts:RUN access write breakpoint test\n");
1014 run_hw_break_test(0);
1015 }
1016
1017
1018 v1printk("kgdbts:RUN plant and detach test\n");
1019 run_plant_and_detach_test(0);
1020 v1printk("kgdbts:RUN sw breakpoint test\n");
1021 run_breakpoint_test(0);
1022 v1printk("kgdbts:RUN bad memory access test\n");
1023 run_bad_read_test();
1024 v1printk("kgdbts:RUN singlestep test %i iterations\n", sstep_test);
1025 for (i = 0; i < sstep_test; i++) {
1026 run_singlestep_break_test();
1027 if (i % 100 == 0)
1028 v1printk("kgdbts:RUN singlestep [%i/%i]\n",
1029 i, sstep_test);
1030 }
1031
1032
1033
1034 if (nmi_sleep) {
1035 v1printk("kgdbts:RUN NMI sleep %i seconds test\n", nmi_sleep);
1036 run_nmi_sleep_test(nmi_sleep);
1037 }
1038
1039
1040
1041
1042
1043 if (clone_test) {
1044 repeat_test = clone_test;
1045 printk(KERN_INFO "kgdbts:RUN kernel_clone for %i breakpoints\n",
1046 repeat_test);
1047 kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
1048 run_kernel_clone_test();
1049 return;
1050 }
1051
1052
1053
1054
1055
1056 if (do_sys_open_test) {
1057 repeat_test = do_sys_open_test;
1058 printk(KERN_INFO "kgdbts:RUN sys_open for %i breakpoints\n",
1059 repeat_test);
1060 kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
1061 run_sys_open_test();
1062 return;
1063 }
1064
1065 kgdb_unregister_io_module(&kgdbts_io_ops);
1066 configured = 0;
1067 }
1068
1069 static int kgdbts_option_setup(char *opt)
1070 {
1071 if (strlen(opt) >= MAX_CONFIG_LEN) {
1072 printk(KERN_ERR "kgdbts: config string too long\n");
1073 return 1;
1074 }
1075 strcpy(config, opt);
1076 return 1;
1077 }
1078
1079 __setup("kgdbts=", kgdbts_option_setup);
1080
1081 static int configure_kgdbts(void)
1082 {
1083 int err = 0;
1084
1085 if (!strlen(config) || isspace(config[0]))
1086 goto noconfig;
1087
1088 final_ack = 0;
1089 run_plant_and_detach_test(1);
1090
1091 err = kgdb_register_io_module(&kgdbts_io_ops);
1092 if (err) {
1093 configured = 0;
1094 return err;
1095 }
1096 configured = 1;
1097 kgdbts_run_tests();
1098
1099 return err;
1100
1101 noconfig:
1102 config[0] = 0;
1103 configured = 0;
1104
1105 return err;
1106 }
1107
1108 static int __init init_kgdbts(void)
1109 {
1110
1111 if (configured == 1)
1112 return 0;
1113
1114 return configure_kgdbts();
1115 }
1116 device_initcall(init_kgdbts);
1117
1118 static int kgdbts_get_char(void)
1119 {
1120 int val = 0;
1121
1122 if (ts.run_test)
1123 val = ts.run_test(1, 0);
1124
1125 return val;
1126 }
1127
1128 static void kgdbts_put_char(u8 chr)
1129 {
1130 if (ts.run_test)
1131 ts.run_test(0, chr);
1132 }
1133
1134 static int param_set_kgdbts_var(const char *kmessage,
1135 const struct kernel_param *kp)
1136 {
1137 size_t len = strlen(kmessage);
1138
1139 if (len >= MAX_CONFIG_LEN) {
1140 printk(KERN_ERR "kgdbts: config string too long\n");
1141 return -ENOSPC;
1142 }
1143
1144
1145 if (configured < 0) {
1146 strcpy(config, kmessage);
1147 return 0;
1148 }
1149
1150 if (configured == 1) {
1151 printk(KERN_ERR "kgdbts: ERROR: Already configured and running.\n");
1152 return -EBUSY;
1153 }
1154
1155 strcpy(config, kmessage);
1156
1157 if (len && config[len - 1] == '\n')
1158 config[len - 1] = '\0';
1159
1160
1161 return configure_kgdbts();
1162 }
1163
1164 static void kgdbts_pre_exp_handler(void)
1165 {
1166
1167 if (!kgdb_connected)
1168 try_module_get(THIS_MODULE);
1169 }
1170
1171 static void kgdbts_post_exp_handler(void)
1172 {
1173
1174 if (!kgdb_connected)
1175 module_put(THIS_MODULE);
1176 }
1177
1178 static struct kgdb_io kgdbts_io_ops = {
1179 .name = "kgdbts",
1180 .read_char = kgdbts_get_char,
1181 .write_char = kgdbts_put_char,
1182 .pre_exception = kgdbts_pre_exp_handler,
1183 .post_exception = kgdbts_post_exp_handler,
1184 };
1185
1186
1187
1188
1189
1190 module_param_call(kgdbts, param_set_kgdbts_var, param_get_string, &kps, 0644);
1191 MODULE_PARM_DESC(kgdbts, "<A|V1|V2>[F#|S#][N#]");