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