Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * kgdbts is a test suite for kgdb for the sole purpose of validating
0004  * that key pieces of the kgdb internals are working properly such as
0005  * HW/SW breakpoints, single stepping, and NMI.
0006  *
0007  * Created by: Jason Wessel <jason.wessel@windriver.com>
0008  *
0009  * Copyright (c) 2008 Wind River Systems, Inc.
0010  */
0011 /* Information about the kgdb test suite.
0012  * -------------------------------------
0013  *
0014  * The kgdb test suite is designed as a KGDB I/O module which
0015  * simulates the communications that a debugger would have with kgdb.
0016  * The tests are broken up in to a line by line and referenced here as
0017  * a "get" which is kgdb requesting input and "put" which is kgdb
0018  * sending a response.
0019  *
0020  * The kgdb suite can be invoked from the kernel command line
0021  * arguments system or executed dynamically at run time.  The test
0022  * suite uses the variable "kgdbts" to obtain the information about
0023  * which tests to run and to configure the verbosity level.  The
0024  * following are the various characters you can use with the kgdbts=
0025  * line:
0026  *
0027  * When using the "kgdbts=" you only choose one of the following core
0028  * test types:
0029  * A = Run all the core tests silently
0030  * V1 = Run all the core tests with minimal output
0031  * V2 = Run all the core tests in debug mode
0032  *
0033  * You can also specify optional tests:
0034  * N## = Go to sleep with interrupts of for ## seconds
0035  *       to test the HW NMI watchdog
0036  * F## = Break at kernel_clone for ## iterations
0037  * S## = Break at sys_open for ## iterations
0038  * I## = Run the single step test ## iterations
0039  *
0040  * NOTE: that the kernel_clone and sys_open tests are mutually exclusive.
0041  *
0042  * To invoke the kgdb test suite from boot you use a kernel start
0043  * argument as follows:
0044  *  kgdbts=V1 kgdbwait
0045  * Or if you wanted to perform the NMI test for 6 seconds and kernel_clone
0046  * test for 100 forks, you could use:
0047  *  kgdbts=V1N6F100 kgdbwait
0048  *
0049  * The test suite can also be invoked at run time with:
0050  *  echo kgdbts=V1N6F100 > /sys/module/kgdbts/parameters/kgdbts
0051  * Or as another example:
0052  *  echo kgdbts=V2 > /sys/module/kgdbts/parameters/kgdbts
0053  *
0054  * When developing a new kgdb arch specific implementation or
0055  * using these tests for the purpose of regression testing,
0056  * several invocations are required.
0057  *
0058  * 1) Boot with the test suite enabled by using the kernel arguments
0059  *       "kgdbts=V1F100 kgdbwait"
0060  *    ## If kgdb arch specific implementation has NMI use
0061  *       "kgdbts=V1N6F100
0062  *
0063  * 2) After the system boot run the basic test.
0064  * echo kgdbts=V1 > /sys/module/kgdbts/parameters/kgdbts
0065  *
0066  * 3) Run the concurrency tests.  It is best to use n+1
0067  *    while loops where n is the number of cpus you have
0068  *    in your system.  The example below uses only two
0069  *    loops.
0070  *
0071  * ## This tests break points on sys_open
0072  * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
0073  * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
0074  * echo kgdbts=V1S10000 > /sys/module/kgdbts/parameters/kgdbts
0075  * fg # and hit control-c
0076  * fg # and hit control-c
0077  * ## This tests break points on kernel_clone
0078  * while [ 1 ] ; do date > /dev/null ; done &
0079  * while [ 1 ] ; do date > /dev/null ; done &
0080  * echo kgdbts=V1F1000 > /sys/module/kgdbts/parameters/kgdbts
0081  * fg # and hit control-c
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 /* Storage for the registers, in GDB format. */
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 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
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     /* Wait until the tests are complete and then ungresiter the I/O
0184      * driver.
0185      */
0186     while (!final_ack)
0187         msleep_interruptible(1500);
0188     /* Pause for any other threads to exit after final ack. */
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 /* This is noinline such that it can be used for a single location to
0198  * place a breakpoint
0199  */
0200 static noinline void kgdbts_break_test(void)
0201 {
0202     v2printk("kgdbts: breakpoint complete\n");
0203 }
0204 
0205 /*
0206  * This is a cached wrapper for kallsyms_lookup_name().
0207  *
0208  * The cache is a big win for several tests. For example it more the doubles
0209  * the cycles per second during the sys_open test. This is not theoretic,
0210  * the performance improvement shows up at human scale, especially when
0211  * testing using emulators.
0212  *
0213  * Obviously neither re-entrant nor thread-safe but that is OK since it
0214  * can only be called from the debug trap (and therefore all other CPUs
0215  * are halted).
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     /* On some arches, a breakpoint stop requires it to be decremented */
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         /* This is special case for emulated single step */
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     /* Readjust the instruction pointer if needed */
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      * From an arch indepent point of view the instruction pointer
0349      * should be on a different instruction
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          * Ensure we stopped in the same thread id as before, else the
0360          * debugger should continue until the original thread that was
0361          * single stepped is scheduled again, emulating gdb's behavior.
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     /* Always send detach because the test is completed at this point */
0427     fill_get_buf("D");
0428 }
0429 
0430 static int put_cont_catch(char *put_str, char *arg)
0431 {
0432     /* This is at the end of the test and we catch any and all input */
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         /* Start by looking at the current PC */
0464         fill_get_buf("g");
0465         break;
0466     case 1:
0467         /* set breakpoint */
0468         break_helper("Z0", NULL, sstep_addr);
0469         break;
0470     case 2:
0471         /* Continue */
0472         fill_get_buf("c");
0473         break;
0474     case 3:
0475         /* Clear breakpoint */
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         /* validate the "g" packet to get the IP */
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         /* Want to stop at IP + break instruction size by default */
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         /* Single step is complete so continue on! */
0525         sstep_state = 0;
0526         return 0;
0527     default:
0528         eprintk("kgdbts: ERROR failed sstep put emulation\n");
0529     }
0530 
0531     /* Continue on the same test line until emulation is complete */
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  * Test to plant a breakpoint and detach, which should clear out the
0545  * breakpoint and restore the original instruction.
0546  */
0547 static struct test_struct plant_and_detach_test[] = {
0548     { "?", "S0*" }, /* Clear break points */
0549     { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
0550     { "D", "OK" }, /* Detach */
0551     { "", "" },
0552 };
0553 
0554 /*
0555  * Simple test to write in a software breakpoint, check for the
0556  * correct stop location and detach.
0557  */
0558 static struct test_struct sw_breakpoint_test[] = {
0559     { "?", "S0*" }, /* Clear break points */
0560     { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
0561     { "c", "T0*", }, /* Continue */
0562     { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0563     { "write", "OK", write_regs },
0564     { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
0565     { "D", "OK" }, /* Detach */
0566     { "D", "OK", NULL,  got_break }, /* On success we made it here */
0567     { "", "" },
0568 };
0569 
0570 /*
0571  * Test a known bad memory read location to test the fault handler and
0572  * read bytes 1-8 at the bad address
0573  */
0574 static struct test_struct bad_read_test[] = {
0575     { "?", "S0*" }, /* Clear break points */
0576     { "m0,1", "E*" }, /* read 1 byte at address 1 */
0577     { "m0,2", "E*" }, /* read 1 byte at address 2 */
0578     { "m0,3", "E*" }, /* read 1 byte at address 3 */
0579     { "m0,4", "E*" }, /* read 1 byte at address 4 */
0580     { "m0,5", "E*" }, /* read 1 byte at address 5 */
0581     { "m0,6", "E*" }, /* read 1 byte at address 6 */
0582     { "m0,7", "E*" }, /* read 1 byte at address 7 */
0583     { "m0,8", "E*" }, /* read 1 byte at address 8 */
0584     { "D", "OK" }, /* Detach which removes all breakpoints and continues */
0585     { "", "" },
0586 };
0587 
0588 /*
0589  * Test for hitting a breakpoint, remove it, single step, plant it
0590  * again and detach.
0591  */
0592 static struct test_struct singlestep_break_test[] = {
0593     { "?", "S0*" }, /* Clear break points */
0594     { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
0595     { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
0596     { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
0597     { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0598     { "write", "OK", write_regs }, /* Write registers */
0599     { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
0600     { "g", "kgdbts_break_test", NULL, check_single_step },
0601     { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
0602     { "c", "T0*", }, /* Continue */
0603     { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0604     { "write", "OK", write_regs }, /* Write registers */
0605     { "D", "OK" }, /* Remove all breakpoints and continues */
0606     { "", "" },
0607 };
0608 
0609 /*
0610  * Test for hitting a breakpoint at kernel_clone for what ever the number
0611  * of iterations required by the variable repeat_test.
0612  */
0613 static struct test_struct do_kernel_clone_test[] = {
0614     { "?", "S0*" }, /* Clear break points */
0615     { "kernel_clone", "OK", sw_break, }, /* set sw breakpoint */
0616     { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
0617     { "kernel_clone", "OK", sw_rem_break }, /*remove breakpoint */
0618     { "g", "kernel_clone", NULL, check_and_rewind_pc }, /* check location */
0619     { "write", "OK", write_regs, emul_reset }, /* Write registers */
0620     { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
0621     { "g", "kernel_clone", NULL, check_single_step },
0622     { "kernel_clone", "OK", sw_break, }, /* set sw breakpoint */
0623     { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
0624     { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
0625     { "", "", get_cont_catch, put_cont_catch },
0626 };
0627 
0628 /* Test for hitting a breakpoint at sys_open for what ever the number
0629  * of iterations required by the variable repeat_test.
0630  */
0631 static struct test_struct sys_open_test[] = {
0632     { "?", "S0*" }, /* Clear break points */
0633     { "do_sys_openat2", "OK", sw_break, }, /* set sw breakpoint */
0634     { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
0635     { "do_sys_openat2", "OK", sw_rem_break }, /*remove breakpoint */
0636     { "g", "do_sys_openat2", NULL, check_and_rewind_pc }, /* check location */
0637     { "write", "OK", write_regs, emul_reset }, /* Write registers */
0638     { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
0639     { "g", "do_sys_openat2", NULL, check_single_step },
0640     { "do_sys_openat2", "OK", sw_break, }, /* set sw breakpoint */
0641     { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
0642     { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
0643     { "", "", get_cont_catch, put_cont_catch },
0644 };
0645 
0646 /*
0647  * Test for hitting a simple hw breakpoint
0648  */
0649 static struct test_struct hw_breakpoint_test[] = {
0650     { "?", "S0*" }, /* Clear break points */
0651     { "kgdbts_break_test", "OK", hw_break, }, /* set hw breakpoint */
0652     { "c", "T0*", }, /* Continue */
0653     { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
0654     { "write", "OK", write_regs },
0655     { "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
0656     { "D", "OK" }, /* Detach */
0657     { "D", "OK", NULL,  got_break }, /* On success we made it here */
0658     { "", "" },
0659 };
0660 
0661 /*
0662  * Test for hitting a hw write breakpoint
0663  */
0664 static struct test_struct hw_write_break_test[] = {
0665     { "?", "S0*" }, /* Clear break points */
0666     { "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
0667     { "c", "T0*", NULL, got_break }, /* Continue */
0668     { "g", "silent", NULL, check_and_rewind_pc },
0669     { "write", "OK", write_regs },
0670     { "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
0671     { "D", "OK" }, /* Detach */
0672     { "D", "OK", NULL,  got_break }, /* On success we made it here */
0673     { "", "" },
0674 };
0675 
0676 /*
0677  * Test for hitting a hw access breakpoint
0678  */
0679 static struct test_struct hw_access_break_test[] = {
0680     { "?", "S0*" }, /* Clear break points */
0681     { "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
0682     { "c", "T0*", NULL, got_break }, /* Continue */
0683     { "g", "silent", NULL, check_and_rewind_pc },
0684     { "write", "OK", write_regs },
0685     { "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
0686     { "D", "OK" }, /* Detach */
0687     { "D", "OK", NULL,  got_break }, /* On success we made it here */
0688     { "", "" },
0689 };
0690 
0691 /*
0692  * Test for hitting a hw access breakpoint
0693  */
0694 static struct test_struct nmi_sleep_test[] = {
0695     { "?", "S0*" }, /* Clear break points */
0696     { "c", "T0*", NULL, got_break }, /* Continue */
0697     { "D", "OK" }, /* Detach */
0698     { "D", "OK", NULL,  got_break }, /* On success we made it here */
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         /* If someone does a * to match the rest of the string, allow
0735          * it, or stop if the received string is complete.
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         /* Send an ACK on the get if a prior put completed and set the
0756          * send ack variable
0757          */
0758         if (send_ack) {
0759             send_ack = 0;
0760             return '+';
0761         }
0762         /* On the first get char, fill the transmit buffer and then
0763          * take from the get_string.
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     /* This callback is a put char which is when kgdb sends data to
0784      * this I/O module.
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     /* Ignore everything until the first valid packet start '$' */
0800     if (put_buf_cnt == 0 && chr != '$')
0801         return 0;
0802 
0803     put_buf[put_buf_cnt] = chr;
0804     put_buf_cnt++;
0805 
0806     /* End of packet == #XX so look for the '#' */
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         /* Trigger check here */
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     /* Activate test with initial breakpoint */
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     /* complete the detach test */
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     /* Activate test with initial breakpoint */
0874     kgdb_breakpoint();
0875     /* run code with the break point in it */
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     /* Activate test with initial breakpoint */
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     /* Activate test with initial breakpoint */
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     /* Activate test with initial breakpoint */
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     /* Activate test with initial breakpoint */
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     /* Activate test with initial breakpoint */
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     /* Activate test with initial breakpoint */
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     /* All HW break point tests */
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     /* required internal KGDB tests */
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     /* ===Optional tests=== */
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     /* If the kernel_clone 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.
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     /* If the sys_open test is run it will be the last test that is
1053      * executed because a kernel thread will be spawned at the very
1054      * end to unregister the debug hooks.
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     /* Shutdown and unregister */
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     /* Already configured? */
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     /* Only copy in the string if the init function has not run yet */
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     /* Chop out \n char as a result of echo */
1157     if (len && config[len - 1] == '\n')
1158         config[len - 1] = '\0';
1159 
1160     /* Go and configure with the new params. */
1161     return configure_kgdbts();
1162 }
1163 
1164 static void kgdbts_pre_exp_handler(void)
1165 {
1166     /* Increment the module count when the debugger is active */
1167     if (!kgdb_connected)
1168         try_module_get(THIS_MODULE);
1169 }
1170 
1171 static void kgdbts_post_exp_handler(void)
1172 {
1173     /* decrement the module count when the debugger detaches */
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  * not really modular, but the easiest way to keep compat with existing
1188  * bootargs behaviour is to continue using module_param here.
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#]");