Merge tag 'usb-serial-5.15-rc1' of https://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / misc / kgdbts.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kgdbts is a test suite for kgdb for the sole purpose of validating
4  * that key pieces of the kgdb internals are working properly such as
5  * HW/SW breakpoints, single stepping, and NMI.
6  *
7  * Created by: Jason Wessel <jason.wessel@windriver.com>
8  *
9  * Copyright (c) 2008 Wind River Systems, Inc.
10  */
11 /* Information about the kgdb test suite.
12  * -------------------------------------
13  *
14  * The kgdb test suite is designed as a KGDB I/O module which
15  * simulates the communications that a debugger would have with kgdb.
16  * The tests are broken up in to a line by line and referenced here as
17  * a "get" which is kgdb requesting input and "put" which is kgdb
18  * sending a response.
19  *
20  * The kgdb suite can be invoked from the kernel command line
21  * arguments system or executed dynamically at run time.  The test
22  * suite uses the variable "kgdbts" to obtain the information about
23  * which tests to run and to configure the verbosity level.  The
24  * following are the various characters you can use with the kgdbts=
25  * line:
26  *
27  * When using the "kgdbts=" you only choose one of the following core
28  * test types:
29  * A = Run all the core tests silently
30  * V1 = Run all the core tests with minimal output
31  * V2 = Run all the core tests in debug mode
32  *
33  * You can also specify optional tests:
34  * N## = Go to sleep with interrupts of for ## seconds
35  *       to test the HW NMI watchdog
36  * F## = Break at kernel_clone for ## iterations
37  * S## = Break at sys_open for ## iterations
38  * I## = Run the single step test ## iterations
39  *
40  * NOTE: that the kernel_clone and sys_open tests are mutually exclusive.
41  *
42  * To invoke the kgdb test suite from boot you use a kernel start
43  * argument as follows:
44  *      kgdbts=V1 kgdbwait
45  * Or if you wanted to perform the NMI test for 6 seconds and kernel_clone
46  * test for 100 forks, you could use:
47  *      kgdbts=V1N6F100 kgdbwait
48  *
49  * The test suite can also be invoked at run time with:
50  *      echo kgdbts=V1N6F100 > /sys/module/kgdbts/parameters/kgdbts
51  * Or as another example:
52  *      echo kgdbts=V2 > /sys/module/kgdbts/parameters/kgdbts
53  *
54  * When developing a new kgdb arch specific implementation or
55  * using these tests for the purpose of regression testing,
56  * several invocations are required.
57  *
58  * 1) Boot with the test suite enabled by using the kernel arguments
59  *       "kgdbts=V1F100 kgdbwait"
60  *    ## If kgdb arch specific implementation has NMI use
61  *       "kgdbts=V1N6F100
62  *
63  * 2) After the system boot run the basic test.
64  * echo kgdbts=V1 > /sys/module/kgdbts/parameters/kgdbts
65  *
66  * 3) Run the concurrency tests.  It is best to use n+1
67  *    while loops where n is the number of cpus you have
68  *    in your system.  The example below uses only two
69  *    loops.
70  *
71  * ## This tests break points on sys_open
72  * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
73  * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
74  * echo kgdbts=V1S10000 > /sys/module/kgdbts/parameters/kgdbts
75  * fg # and hit control-c
76  * fg # and hit control-c
77  * ## This tests break points on kernel_clone
78  * while [ 1 ] ; do date > /dev/null ; done &
79  * while [ 1 ] ; do date > /dev/null ; done &
80  * echo kgdbts=V1F1000 > /sys/module/kgdbts/parameters/kgdbts
81  * fg # and hit control-c
82  *
83  */
84
85 #include <linux/kernel.h>
86 #include <linux/kgdb.h>
87 #include <linux/ctype.h>
88 #include <linux/uaccess.h>
89 #include <linux/syscalls.h>
90 #include <linux/nmi.h>
91 #include <linux/delay.h>
92 #include <linux/kthread.h>
93 #include <linux/module.h>
94 #include <linux/sched/task.h>
95 #include <linux/kallsyms.h>
96
97 #include <asm/sections.h>
98
99 #define v1printk(a...) do {             \
100         if (verbose)                    \
101                 printk(KERN_INFO a);    \
102 } while (0)
103 #define v2printk(a...) do {             \
104         if (verbose > 1) {              \
105                 printk(KERN_INFO a);    \
106         }                               \
107         touch_nmi_watchdog();           \
108 } while (0)
109 #define eprintk(a...) do {              \
110         printk(KERN_ERR a);             \
111         WARN_ON(1);                     \
112 } while (0)
113 #define MAX_CONFIG_LEN          40
114
115 static struct kgdb_io kgdbts_io_ops;
116 static char get_buf[BUFMAX];
117 static int get_buf_cnt;
118 static char put_buf[BUFMAX];
119 static int put_buf_cnt;
120 static char scratch_buf[BUFMAX];
121 static int verbose;
122 static int repeat_test;
123 static int test_complete;
124 static int send_ack;
125 static int final_ack;
126 static int force_hwbrks;
127 static int hwbreaks_ok;
128 static int hw_break_val;
129 static int hw_break_val2;
130 static int cont_instead_of_sstep;
131 static unsigned long cont_thread_id;
132 static unsigned long sstep_thread_id;
133 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_SPARC)
134 static int arch_needs_sstep_emulation = 1;
135 #else
136 static int arch_needs_sstep_emulation;
137 #endif
138 static unsigned long cont_addr;
139 static unsigned long sstep_addr;
140 static int restart_from_top_after_write;
141 static int sstep_state;
142
143 /* Storage for the registers, in GDB format. */
144 static unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
145                                         sizeof(unsigned long) - 1) /
146                                         sizeof(unsigned long)];
147 static struct pt_regs kgdbts_regs;
148
149 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
150 static int configured           = -1;
151
152 #ifdef CONFIG_KGDB_TESTS_BOOT_STRING
153 static char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
154 #else
155 static char config[MAX_CONFIG_LEN];
156 #endif
157 static struct kparam_string kps = {
158         .string                 = config,
159         .maxlen                 = MAX_CONFIG_LEN,
160 };
161
162 static void fill_get_buf(char *buf);
163
164 struct test_struct {
165         char *get;
166         char *put;
167         void (*get_handler)(char *);
168         int (*put_handler)(char *, char *);
169 };
170
171 struct test_state {
172         char *name;
173         struct test_struct *tst;
174         int idx;
175         int (*run_test) (int, int);
176         int (*validate_put) (char *);
177 };
178
179 static struct test_state ts;
180
181 static int kgdbts_unreg_thread(void *ptr)
182 {
183         /* Wait until the tests are complete and then ungresiter the I/O
184          * driver.
185          */
186         while (!final_ack)
187                 msleep_interruptible(1500);
188         /* Pause for any other threads to exit after final ack. */
189         msleep_interruptible(1000);
190         if (configured)
191                 kgdb_unregister_io_module(&kgdbts_io_ops);
192         configured = 0;
193
194         return 0;
195 }
196
197 /* This is noinline such that it can be used for a single location to
198  * place a breakpoint
199  */
200 static noinline void kgdbts_break_test(void)
201 {
202         v2printk("kgdbts: breakpoint complete\n");
203 }
204
205 /*
206  * This is a cached wrapper for kallsyms_lookup_name().
207  *
208  * The cache is a big win for several tests. For example it more the doubles
209  * the cycles per second during the sys_open test. This is not theoretic,
210  * the performance improvement shows up at human scale, especially when
211  * testing using emulators.
212  *
213  * Obviously neither re-entrant nor thread-safe but that is OK since it
214  * can only be called from the debug trap (and therefore all other CPUs
215  * are halted).
216  */
217 static unsigned long lookup_addr(char *arg)
218 {
219         static char cached_arg[KSYM_NAME_LEN];
220         static unsigned long cached_addr;
221
222         if (strcmp(arg, cached_arg)) {
223                 strscpy(cached_arg, arg, KSYM_NAME_LEN);
224                 cached_addr = kallsyms_lookup_name(arg);
225         }
226
227         return (unsigned long)dereference_function_descriptor(
228                         (void *)cached_addr);
229 }
230
231 static void break_helper(char *bp_type, char *arg, unsigned long vaddr)
232 {
233         unsigned long addr;
234
235         if (arg)
236                 addr = lookup_addr(arg);
237         else
238                 addr = vaddr;
239
240         sprintf(scratch_buf, "%s,%lx,%i", bp_type, addr,
241                 BREAK_INSTR_SIZE);
242         fill_get_buf(scratch_buf);
243 }
244
245 static void sw_break(char *arg)
246 {
247         break_helper(force_hwbrks ? "Z1" : "Z0", arg, 0);
248 }
249
250 static void sw_rem_break(char *arg)
251 {
252         break_helper(force_hwbrks ? "z1" : "z0", arg, 0);
253 }
254
255 static void hw_break(char *arg)
256 {
257         break_helper("Z1", arg, 0);
258 }
259
260 static void hw_rem_break(char *arg)
261 {
262         break_helper("z1", arg, 0);
263 }
264
265 static void hw_write_break(char *arg)
266 {
267         break_helper("Z2", arg, 0);
268 }
269
270 static void hw_rem_write_break(char *arg)
271 {
272         break_helper("z2", arg, 0);
273 }
274
275 static void hw_access_break(char *arg)
276 {
277         break_helper("Z4", arg, 0);
278 }
279
280 static void hw_rem_access_break(char *arg)
281 {
282         break_helper("z4", arg, 0);
283 }
284
285 static void hw_break_val_access(void)
286 {
287         hw_break_val2 = hw_break_val;
288 }
289
290 static void hw_break_val_write(void)
291 {
292         hw_break_val++;
293 }
294
295 static int get_thread_id_continue(char *put_str, char *arg)
296 {
297         char *ptr = &put_str[11];
298
299         if (put_str[1] != 'T' || put_str[2] != '0')
300                 return 1;
301         kgdb_hex2long(&ptr, &cont_thread_id);
302         return 0;
303 }
304
305 static int check_and_rewind_pc(char *put_str, char *arg)
306 {
307         unsigned long addr = lookup_addr(arg);
308         unsigned long ip;
309         int offset = 0;
310
311         kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
312                  NUMREGBYTES);
313         gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
314         ip = instruction_pointer(&kgdbts_regs);
315         v2printk("Stopped at IP: %lx\n", ip);
316 #ifdef GDB_ADJUSTS_BREAK_OFFSET
317         /* On some arches, a breakpoint stop requires it to be decremented */
318         if (addr + BREAK_INSTR_SIZE == ip)
319                 offset = -BREAK_INSTR_SIZE;
320 #endif
321
322         if (arch_needs_sstep_emulation && sstep_addr &&
323             ip + offset == sstep_addr &&
324             ((!strcmp(arg, "do_sys_openat2") || !strcmp(arg, "kernel_clone")))) {
325                 /* This is special case for emulated single step */
326                 v2printk("Emul: rewind hit single step bp\n");
327                 restart_from_top_after_write = 1;
328         } else if (strcmp(arg, "silent") && ip + offset != addr) {
329                 eprintk("kgdbts: BP mismatch %lx expected %lx\n",
330                            ip + offset, addr);
331                 return 1;
332         }
333         /* Readjust the instruction pointer if needed */
334         ip += offset;
335         cont_addr = ip;
336 #ifdef GDB_ADJUSTS_BREAK_OFFSET
337         instruction_pointer_set(&kgdbts_regs, ip);
338 #endif
339         return 0;
340 }
341
342 static int check_single_step(char *put_str, char *arg)
343 {
344         unsigned long addr = lookup_addr(arg);
345         static int matched_id;
346
347         /*
348          * From an arch indepent point of view the instruction pointer
349          * should be on a different instruction
350          */
351         kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
352                  NUMREGBYTES);
353         gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
354         v2printk("Singlestep stopped at IP: %lx\n",
355                    instruction_pointer(&kgdbts_regs));
356
357         if (sstep_thread_id != cont_thread_id) {
358                 /*
359                  * Ensure we stopped in the same thread id as before, else the
360                  * debugger should continue until the original thread that was
361                  * single stepped is scheduled again, emulating gdb's behavior.
362                  */
363                 v2printk("ThrID does not match: %lx\n", cont_thread_id);
364                 if (arch_needs_sstep_emulation) {
365                         if (matched_id &&
366                             instruction_pointer(&kgdbts_regs) != addr)
367                                 goto continue_test;
368                         matched_id++;
369                         ts.idx -= 2;
370                         sstep_state = 0;
371                         return 0;
372                 }
373                 cont_instead_of_sstep = 1;
374                 ts.idx -= 4;
375                 return 0;
376         }
377 continue_test:
378         matched_id = 0;
379         if (instruction_pointer(&kgdbts_regs) == addr) {
380                 eprintk("kgdbts: SingleStep failed at %lx\n",
381                            instruction_pointer(&kgdbts_regs));
382                 return 1;
383         }
384
385         return 0;
386 }
387
388 static void write_regs(char *arg)
389 {
390         memset(scratch_buf, 0, sizeof(scratch_buf));
391         scratch_buf[0] = 'G';
392         pt_regs_to_gdb_regs(kgdbts_gdb_regs, &kgdbts_regs);
393         kgdb_mem2hex((char *)kgdbts_gdb_regs, &scratch_buf[1], NUMREGBYTES);
394         fill_get_buf(scratch_buf);
395 }
396
397 static void skip_back_repeat_test(char *arg)
398 {
399         int go_back = simple_strtol(arg, NULL, 10);
400
401         repeat_test--;
402         if (repeat_test <= 0) {
403                 ts.idx++;
404         } else {
405                 if (repeat_test % 100 == 0)
406                         v1printk("kgdbts:RUN ... %d remaining\n", repeat_test);
407
408                 ts.idx -= go_back;
409         }
410         fill_get_buf(ts.tst[ts.idx].get);
411 }
412
413 static int got_break(char *put_str, char *arg)
414 {
415         test_complete = 1;
416         if (!strncmp(put_str+1, arg, 2)) {
417                 if (!strncmp(arg, "T0", 2))
418                         test_complete = 2;
419                 return 0;
420         }
421         return 1;
422 }
423
424 static void get_cont_catch(char *arg)
425 {
426         /* Always send detach because the test is completed at this point */
427         fill_get_buf("D");
428 }
429
430 static int put_cont_catch(char *put_str, char *arg)
431 {
432         /* This is at the end of the test and we catch any and all input */
433         v2printk("kgdbts: cleanup task: %lx\n", sstep_thread_id);
434         ts.idx--;
435         return 0;
436 }
437
438 static int emul_reset(char *put_str, char *arg)
439 {
440         if (strncmp(put_str, "$OK", 3))
441                 return 1;
442         if (restart_from_top_after_write) {
443                 restart_from_top_after_write = 0;
444                 ts.idx = -1;
445         }
446         return 0;
447 }
448
449 static void emul_sstep_get(char *arg)
450 {
451         if (!arch_needs_sstep_emulation) {
452                 if (cont_instead_of_sstep) {
453                         cont_instead_of_sstep = 0;
454                         fill_get_buf("c");
455                 } else {
456                         fill_get_buf(arg);
457                 }
458                 return;
459         }
460         switch (sstep_state) {
461         case 0:
462                 v2printk("Emulate single step\n");
463                 /* Start by looking at the current PC */
464                 fill_get_buf("g");
465                 break;
466         case 1:
467                 /* set breakpoint */
468                 break_helper("Z0", NULL, sstep_addr);
469                 break;
470         case 2:
471                 /* Continue */
472                 fill_get_buf("c");
473                 break;
474         case 3:
475                 /* Clear breakpoint */
476                 break_helper("z0", NULL, sstep_addr);
477                 break;
478         default:
479                 eprintk("kgdbts: ERROR failed sstep get emulation\n");
480         }
481         sstep_state++;
482 }
483
484 static int emul_sstep_put(char *put_str, char *arg)
485 {
486         if (!arch_needs_sstep_emulation) {
487                 char *ptr = &put_str[11];
488                 if (put_str[1] != 'T' || put_str[2] != '0')
489                         return 1;
490                 kgdb_hex2long(&ptr, &sstep_thread_id);
491                 return 0;
492         }
493         switch (sstep_state) {
494         case 1:
495                 /* validate the "g" packet to get the IP */
496                 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
497                          NUMREGBYTES);
498                 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
499                 v2printk("Stopped at IP: %lx\n",
500                          instruction_pointer(&kgdbts_regs));
501                 /* Want to stop at IP + break instruction size by default */
502                 sstep_addr = cont_addr + BREAK_INSTR_SIZE;
503                 break;
504         case 2:
505                 if (strncmp(put_str, "$OK", 3)) {
506                         eprintk("kgdbts: failed sstep break set\n");
507                         return 1;
508                 }
509                 break;
510         case 3:
511                 if (strncmp(put_str, "$T0", 3)) {
512                         eprintk("kgdbts: failed continue sstep\n");
513                         return 1;
514                 } else {
515                         char *ptr = &put_str[11];
516                         kgdb_hex2long(&ptr, &sstep_thread_id);
517                 }
518                 break;
519         case 4:
520                 if (strncmp(put_str, "$OK", 3)) {
521                         eprintk("kgdbts: failed sstep break unset\n");
522                         return 1;
523                 }
524                 /* Single step is complete so continue on! */
525                 sstep_state = 0;
526                 return 0;
527         default:
528                 eprintk("kgdbts: ERROR failed sstep put emulation\n");
529         }
530
531         /* Continue on the same test line until emulation is complete */
532         ts.idx--;
533         return 0;
534 }
535
536 static int final_ack_set(char *put_str, char *arg)
537 {
538         if (strncmp(put_str+1, arg, 2))
539                 return 1;
540         final_ack = 1;
541         return 0;
542 }
543 /*
544  * Test to plant a breakpoint and detach, which should clear out the
545  * breakpoint and restore the original instruction.
546  */
547 static struct test_struct plant_and_detach_test[] = {
548         { "?", "S0*" }, /* Clear break points */
549         { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
550         { "D", "OK" }, /* Detach */
551         { "", "" },
552 };
553
554 /*
555  * Simple test to write in a software breakpoint, check for the
556  * correct stop location and detach.
557  */
558 static struct test_struct sw_breakpoint_test[] = {
559         { "?", "S0*" }, /* Clear break points */
560         { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
561         { "c", "T0*", }, /* Continue */
562         { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
563         { "write", "OK", write_regs },
564         { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
565         { "D", "OK" }, /* Detach */
566         { "D", "OK", NULL,  got_break }, /* On success we made it here */
567         { "", "" },
568 };
569
570 /*
571  * Test a known bad memory read location to test the fault handler and
572  * read bytes 1-8 at the bad address
573  */
574 static struct test_struct bad_read_test[] = {
575         { "?", "S0*" }, /* Clear break points */
576         { "m0,1", "E*" }, /* read 1 byte at address 1 */
577         { "m0,2", "E*" }, /* read 1 byte at address 2 */
578         { "m0,3", "E*" }, /* read 1 byte at address 3 */
579         { "m0,4", "E*" }, /* read 1 byte at address 4 */
580         { "m0,5", "E*" }, /* read 1 byte at address 5 */
581         { "m0,6", "E*" }, /* read 1 byte at address 6 */
582         { "m0,7", "E*" }, /* read 1 byte at address 7 */
583         { "m0,8", "E*" }, /* read 1 byte at address 8 */
584         { "D", "OK" }, /* Detach which removes all breakpoints and continues */
585         { "", "" },
586 };
587
588 /*
589  * Test for hitting a breakpoint, remove it, single step, plant it
590  * again and detach.
591  */
592 static struct test_struct singlestep_break_test[] = {
593         { "?", "S0*" }, /* Clear break points */
594         { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
595         { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
596         { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
597         { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
598         { "write", "OK", write_regs }, /* Write registers */
599         { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
600         { "g", "kgdbts_break_test", NULL, check_single_step },
601         { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
602         { "c", "T0*", }, /* Continue */
603         { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
604         { "write", "OK", write_regs }, /* Write registers */
605         { "D", "OK" }, /* Remove all breakpoints and continues */
606         { "", "" },
607 };
608
609 /*
610  * Test for hitting a breakpoint at kernel_clone for what ever the number
611  * of iterations required by the variable repeat_test.
612  */
613 static struct test_struct do_kernel_clone_test[] = {
614         { "?", "S0*" }, /* Clear break points */
615         { "kernel_clone", "OK", sw_break, }, /* set sw breakpoint */
616         { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
617         { "kernel_clone", "OK", sw_rem_break }, /*remove breakpoint */
618         { "g", "kernel_clone", NULL, check_and_rewind_pc }, /* check location */
619         { "write", "OK", write_regs, emul_reset }, /* Write registers */
620         { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
621         { "g", "kernel_clone", NULL, check_single_step },
622         { "kernel_clone", "OK", sw_break, }, /* set sw breakpoint */
623         { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
624         { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
625         { "", "", get_cont_catch, put_cont_catch },
626 };
627
628 /* Test for hitting a breakpoint at sys_open for what ever the number
629  * of iterations required by the variable repeat_test.
630  */
631 static struct test_struct sys_open_test[] = {
632         { "?", "S0*" }, /* Clear break points */
633         { "do_sys_openat2", "OK", sw_break, }, /* set sw breakpoint */
634         { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
635         { "do_sys_openat2", "OK", sw_rem_break }, /*remove breakpoint */
636         { "g", "do_sys_openat2", NULL, check_and_rewind_pc }, /* check location */
637         { "write", "OK", write_regs, emul_reset }, /* Write registers */
638         { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
639         { "g", "do_sys_openat2", NULL, check_single_step },
640         { "do_sys_openat2", "OK", sw_break, }, /* set sw breakpoint */
641         { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
642         { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
643         { "", "", get_cont_catch, put_cont_catch },
644 };
645
646 /*
647  * Test for hitting a simple hw breakpoint
648  */
649 static struct test_struct hw_breakpoint_test[] = {
650         { "?", "S0*" }, /* Clear break points */
651         { "kgdbts_break_test", "OK", hw_break, }, /* set hw breakpoint */
652         { "c", "T0*", }, /* Continue */
653         { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
654         { "write", "OK", write_regs },
655         { "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
656         { "D", "OK" }, /* Detach */
657         { "D", "OK", NULL,  got_break }, /* On success we made it here */
658         { "", "" },
659 };
660
661 /*
662  * Test for hitting a hw write breakpoint
663  */
664 static struct test_struct hw_write_break_test[] = {
665         { "?", "S0*" }, /* Clear break points */
666         { "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
667         { "c", "T0*", NULL, got_break }, /* Continue */
668         { "g", "silent", NULL, check_and_rewind_pc },
669         { "write", "OK", write_regs },
670         { "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
671         { "D", "OK" }, /* Detach */
672         { "D", "OK", NULL,  got_break }, /* On success we made it here */
673         { "", "" },
674 };
675
676 /*
677  * Test for hitting a hw access breakpoint
678  */
679 static struct test_struct hw_access_break_test[] = {
680         { "?", "S0*" }, /* Clear break points */
681         { "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
682         { "c", "T0*", NULL, got_break }, /* Continue */
683         { "g", "silent", NULL, check_and_rewind_pc },
684         { "write", "OK", write_regs },
685         { "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
686         { "D", "OK" }, /* Detach */
687         { "D", "OK", NULL,  got_break }, /* On success we made it here */
688         { "", "" },
689 };
690
691 /*
692  * Test for hitting a hw access breakpoint
693  */
694 static struct test_struct nmi_sleep_test[] = {
695         { "?", "S0*" }, /* Clear break points */
696         { "c", "T0*", NULL, got_break }, /* Continue */
697         { "D", "OK" }, /* Detach */
698         { "D", "OK", NULL,  got_break }, /* On success we made it here */
699         { "", "" },
700 };
701
702 static void fill_get_buf(char *buf)
703 {
704         unsigned char checksum = 0;
705         int count = 0;
706         char ch;
707
708         strcpy(get_buf, "$");
709         strcat(get_buf, buf);
710         while ((ch = buf[count])) {
711                 checksum += ch;
712                 count++;
713         }
714         strcat(get_buf, "#");
715         get_buf[count + 2] = hex_asc_hi(checksum);
716         get_buf[count + 3] = hex_asc_lo(checksum);
717         get_buf[count + 4] = '\0';
718         v2printk("get%i: %s\n", ts.idx, get_buf);
719 }
720
721 static int validate_simple_test(char *put_str)
722 {
723         char *chk_str;
724
725         if (ts.tst[ts.idx].put_handler)
726                 return ts.tst[ts.idx].put_handler(put_str,
727                         ts.tst[ts.idx].put);
728
729         chk_str = ts.tst[ts.idx].put;
730         if (*put_str == '$')
731                 put_str++;
732
733         while (*chk_str != '\0' && *put_str != '\0') {
734                 /* If someone does a * to match the rest of the string, allow
735                  * it, or stop if the received string is complete.
736                  */
737                 if (*put_str == '#' || *chk_str == '*')
738                         return 0;
739                 if (*put_str != *chk_str)
740                         return 1;
741
742                 chk_str++;
743                 put_str++;
744         }
745         if (*chk_str == '\0' && (*put_str == '\0' || *put_str == '#'))
746                 return 0;
747
748         return 1;
749 }
750
751 static int run_simple_test(int is_get_char, int chr)
752 {
753         int ret = 0;
754         if (is_get_char) {
755                 /* Send an ACK on the get if a prior put completed and set the
756                  * send ack variable
757                  */
758                 if (send_ack) {
759                         send_ack = 0;
760                         return '+';
761                 }
762                 /* On the first get char, fill the transmit buffer and then
763                  * take from the get_string.
764                  */
765                 if (get_buf_cnt == 0) {
766                         if (ts.tst[ts.idx].get_handler)
767                                 ts.tst[ts.idx].get_handler(ts.tst[ts.idx].get);
768                         else
769                                 fill_get_buf(ts.tst[ts.idx].get);
770                 }
771
772                 if (get_buf[get_buf_cnt] == '\0') {
773                         eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
774                            ts.name, ts.idx);
775                         get_buf_cnt = 0;
776                         fill_get_buf("D");
777                 }
778                 ret = get_buf[get_buf_cnt];
779                 get_buf_cnt++;
780                 return ret;
781         }
782
783         /* This callback is a put char which is when kgdb sends data to
784          * this I/O module.
785          */
786         if (ts.tst[ts.idx].get[0] == '\0' && ts.tst[ts.idx].put[0] == '\0' &&
787             !ts.tst[ts.idx].get_handler) {
788                 eprintk("kgdbts: ERROR: beyond end of test on"
789                            " '%s' line %i\n", ts.name, ts.idx);
790                 return 0;
791         }
792
793         if (put_buf_cnt >= BUFMAX) {
794                 eprintk("kgdbts: ERROR: put buffer overflow on"
795                            " '%s' line %i\n", ts.name, ts.idx);
796                 put_buf_cnt = 0;
797                 return 0;
798         }
799         /* Ignore everything until the first valid packet start '$' */
800         if (put_buf_cnt == 0 && chr != '$')
801                 return 0;
802
803         put_buf[put_buf_cnt] = chr;
804         put_buf_cnt++;
805
806         /* End of packet == #XX so look for the '#' */
807         if (put_buf_cnt > 3 && put_buf[put_buf_cnt - 3] == '#') {
808                 if (put_buf_cnt >= BUFMAX) {
809                         eprintk("kgdbts: ERROR: put buffer overflow on"
810                                 " '%s' line %i\n", ts.name, ts.idx);
811                         put_buf_cnt = 0;
812                         return 0;
813                 }
814                 put_buf[put_buf_cnt] = '\0';
815                 v2printk("put%i: %s\n", ts.idx, put_buf);
816                 /* Trigger check here */
817                 if (ts.validate_put && ts.validate_put(put_buf)) {
818                         eprintk("kgdbts: ERROR PUT: end of test "
819                            "buffer on '%s' line %i expected %s got %s\n",
820                            ts.name, ts.idx, ts.tst[ts.idx].put, put_buf);
821                 }
822                 ts.idx++;
823                 put_buf_cnt = 0;
824                 get_buf_cnt = 0;
825                 send_ack = 1;
826         }
827         return 0;
828 }
829
830 static void init_simple_test(void)
831 {
832         memset(&ts, 0, sizeof(ts));
833         ts.run_test = run_simple_test;
834         ts.validate_put = validate_simple_test;
835 }
836
837 static void run_plant_and_detach_test(int is_early)
838 {
839         char before[BREAK_INSTR_SIZE];
840         char after[BREAK_INSTR_SIZE];
841
842         copy_from_kernel_nofault(before, (char *)kgdbts_break_test,
843           BREAK_INSTR_SIZE);
844         init_simple_test();
845         ts.tst = plant_and_detach_test;
846         ts.name = "plant_and_detach_test";
847         /* Activate test with initial breakpoint */
848         if (!is_early)
849                 kgdb_breakpoint();
850         copy_from_kernel_nofault(after, (char *)kgdbts_break_test,
851                         BREAK_INSTR_SIZE);
852         if (memcmp(before, after, BREAK_INSTR_SIZE)) {
853                 printk(KERN_CRIT "kgdbts: ERROR kgdb corrupted memory\n");
854                 panic("kgdb memory corruption");
855         }
856
857         /* complete the detach test */
858         if (!is_early)
859                 kgdbts_break_test();
860 }
861
862 static void run_breakpoint_test(int is_hw_breakpoint)
863 {
864         test_complete = 0;
865         init_simple_test();
866         if (is_hw_breakpoint) {
867                 ts.tst = hw_breakpoint_test;
868                 ts.name = "hw_breakpoint_test";
869         } else {
870                 ts.tst = sw_breakpoint_test;
871                 ts.name = "sw_breakpoint_test";
872         }
873         /* Activate test with initial breakpoint */
874         kgdb_breakpoint();
875         /* run code with the break point in it */
876         kgdbts_break_test();
877         kgdb_breakpoint();
878
879         if (test_complete)
880                 return;
881
882         eprintk("kgdbts: ERROR %s test failed\n", ts.name);
883         if (is_hw_breakpoint)
884                 hwbreaks_ok = 0;
885 }
886
887 static void run_hw_break_test(int is_write_test)
888 {
889         test_complete = 0;
890         init_simple_test();
891         if (is_write_test) {
892                 ts.tst = hw_write_break_test;
893                 ts.name = "hw_write_break_test";
894         } else {
895                 ts.tst = hw_access_break_test;
896                 ts.name = "hw_access_break_test";
897         }
898         /* Activate test with initial breakpoint */
899         kgdb_breakpoint();
900         hw_break_val_access();
901         if (is_write_test) {
902                 if (test_complete == 2) {
903                         eprintk("kgdbts: ERROR %s broke on access\n",
904                                 ts.name);
905                         hwbreaks_ok = 0;
906                 }
907                 hw_break_val_write();
908         }
909         kgdb_breakpoint();
910
911         if (test_complete == 1)
912                 return;
913
914         eprintk("kgdbts: ERROR %s test failed\n", ts.name);
915         hwbreaks_ok = 0;
916 }
917
918 static void run_nmi_sleep_test(int nmi_sleep)
919 {
920         unsigned long flags;
921
922         init_simple_test();
923         ts.tst = nmi_sleep_test;
924         ts.name = "nmi_sleep_test";
925         /* Activate test with initial breakpoint */
926         kgdb_breakpoint();
927         local_irq_save(flags);
928         mdelay(nmi_sleep*1000);
929         touch_nmi_watchdog();
930         local_irq_restore(flags);
931         if (test_complete != 2)
932                 eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
933         kgdb_breakpoint();
934         if (test_complete == 1)
935                 return;
936
937         eprintk("kgdbts: ERROR %s test failed\n", ts.name);
938 }
939
940 static void run_bad_read_test(void)
941 {
942         init_simple_test();
943         ts.tst = bad_read_test;
944         ts.name = "bad_read_test";
945         /* Activate test with initial breakpoint */
946         kgdb_breakpoint();
947 }
948
949 static void run_kernel_clone_test(void)
950 {
951         init_simple_test();
952         ts.tst = do_kernel_clone_test;
953         ts.name = "do_kernel_clone_test";
954         /* Activate test with initial breakpoint */
955         kgdb_breakpoint();
956 }
957
958 static void run_sys_open_test(void)
959 {
960         init_simple_test();
961         ts.tst = sys_open_test;
962         ts.name = "sys_open_test";
963         /* Activate test with initial breakpoint */
964         kgdb_breakpoint();
965 }
966
967 static void run_singlestep_break_test(void)
968 {
969         init_simple_test();
970         ts.tst = singlestep_break_test;
971         ts.name = "singlestep_breakpoint_test";
972         /* Activate test with initial breakpoint */
973         kgdb_breakpoint();
974         kgdbts_break_test();
975         kgdbts_break_test();
976 }
977
978 static void kgdbts_run_tests(void)
979 {
980         char *ptr;
981         int clone_test = 0;
982         int do_sys_open_test = 0;
983         int sstep_test = 1000;
984         int nmi_sleep = 0;
985         int i;
986
987         verbose = 0;
988         if (strstr(config, "V1"))
989                 verbose = 1;
990         if (strstr(config, "V2"))
991                 verbose = 2;
992
993         ptr = strchr(config, 'F');
994         if (ptr)
995                 clone_test = simple_strtol(ptr + 1, NULL, 10);
996         ptr = strchr(config, 'S');
997         if (ptr)
998                 do_sys_open_test = simple_strtol(ptr + 1, NULL, 10);
999         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 -ENOSPC;
1074         }
1075         strcpy(config, opt);
1076         return 0;
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#]");