kgdbts: (1 of 2) fix single step awareness to work correctly with SMP
[deliverable/linux.git] / drivers / misc / kgdbts.c
CommitLineData
e8d31c20
JW
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
7cfcd985 50 * I## = Run the single step test ## iterations
e8d31c20
JW
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>
eb12a679 105#include <linux/module.h>
e8d31c20
JW
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)
974460c5
JW
116#define eprintk(a...) do { \
117 printk(KERN_ERR a); \
118 WARN_ON(1); \
119 } while (0)
e8d31c20
JW
120#define MAX_CONFIG_LEN 40
121
e8d31c20
JW
122static struct kgdb_io kgdbts_io_ops;
123static char get_buf[BUFMAX];
124static int get_buf_cnt;
125static char put_buf[BUFMAX];
126static int put_buf_cnt;
127static char scratch_buf[BUFMAX];
128static int verbose;
129static int repeat_test;
130static int test_complete;
131static int send_ack;
132static int final_ack;
b33cb815
JW
133static int force_hwbrks;
134static int hwbreaks_ok;
e8d31c20
JW
135static int hw_break_val;
136static int hw_break_val2;
486c5987
JW
137static int cont_instead_of_sstep;
138static unsigned long cont_thread_id;
139static unsigned long sstep_thread_id;
4d7ffa49 140#if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_SPARC)
e8d31c20
JW
141static int arch_needs_sstep_emulation = 1;
142#else
143static int arch_needs_sstep_emulation;
144#endif
145static unsigned long sstep_addr;
146static int sstep_state;
147
148/* Storage for the registers, in GDB format. */
149static unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
150 sizeof(unsigned long) - 1) /
151 sizeof(unsigned long)];
152static struct pt_regs kgdbts_regs;
153
154/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
155static int configured = -1;
156
974460c5
JW
157#ifdef CONFIG_KGDB_TESTS_BOOT_STRING
158static char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
159#else
e8d31c20 160static char config[MAX_CONFIG_LEN];
974460c5 161#endif
e8d31c20
JW
162static struct kparam_string kps = {
163 .string = config,
164 .maxlen = MAX_CONFIG_LEN,
165};
166
167static void fill_get_buf(char *buf);
168
169struct test_struct {
170 char *get;
171 char *put;
172 void (*get_handler)(char *);
173 int (*put_handler)(char *, char *);
174};
175
176struct 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
184static struct test_state ts;
185
186static 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 */
204static noinline void kgdbts_break_test(void)
205{
206 v2printk("kgdbts: breakpoint complete\n");
207}
208
209/* Lookup symbol info in the kernel */
210static 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"))
486c5987 217 addr = (unsigned long)do_sys_open;
e8d31c20
JW
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
225static 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
239static void sw_break(char *arg)
240{
b33cb815 241 break_helper(force_hwbrks ? "Z1" : "Z0", arg, 0);
e8d31c20
JW
242}
243
244static void sw_rem_break(char *arg)
245{
b33cb815 246 break_helper(force_hwbrks ? "z1" : "z0", arg, 0);
e8d31c20
JW
247}
248
249static void hw_break(char *arg)
250{
251 break_helper("Z1", arg, 0);
252}
253
254static void hw_rem_break(char *arg)
255{
256 break_helper("z1", arg, 0);
257}
258
259static void hw_write_break(char *arg)
260{
261 break_helper("Z2", arg, 0);
262}
263
264static void hw_rem_write_break(char *arg)
265{
266 break_helper("z2", arg, 0);
267}
268
269static void hw_access_break(char *arg)
270{
271 break_helper("Z4", arg, 0);
272}
273
274static void hw_rem_access_break(char *arg)
275{
276 break_helper("z4", arg, 0);
277}
278
279static void hw_break_val_access(void)
280{
281 hw_break_val2 = hw_break_val;
282}
283
284static void hw_break_val_write(void)
285{
286 hw_break_val++;
287}
288
486c5987
JW
289static 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
e8d31c20
JW
299static int check_and_rewind_pc(char *put_str, char *arg)
300{
301 unsigned long addr = lookup_addr(arg);
63ab25eb 302 unsigned long ip;
e8d31c20
JW
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);
63ab25eb
MF
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;
e8d31c20 314#endif
63ab25eb 315 if (strcmp(arg, "silent") && ip + offset != addr) {
974460c5 316 eprintk("kgdbts: BP mismatch %lx expected %lx\n",
63ab25eb 317 ip + offset, addr);
e8d31c20
JW
318 return 1;
319 }
63ab25eb 320 /* Readjust the instruction pointer if needed */
603d04b2
MF
321 ip += offset;
322#ifdef GDB_ADJUSTS_BREAK_OFFSET
323 instruction_pointer_set(&kgdbts_regs, ip);
324#endif
e8d31c20
JW
325 return 0;
326}
327
328static 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));
486c5987
JW
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 }
e8d31c20 352 if (instruction_pointer(&kgdbts_regs) == addr) {
974460c5 353 eprintk("kgdbts: SingleStep failed at %lx\n",
e8d31c20
JW
354 instruction_pointer(&kgdbts_regs));
355 return 1;
356 }
357
358 return 0;
359}
360
361static 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
370static 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
382static 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
393static void emul_sstep_get(char *arg)
394{
395 if (!arch_needs_sstep_emulation) {
486c5987
JW
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 }
e8d31c20
JW
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 */
001fddf5 412 break_helper("Z0", NULL, sstep_addr);
e8d31c20
JW
413 break;
414 case 2:
415 /* Continue */
416 fill_get_buf("c");
417 break;
418 case 3:
419 /* Clear breakpoint */
001fddf5 420 break_helper("z0", NULL, sstep_addr);
e8d31c20
JW
421 break;
422 default:
974460c5 423 eprintk("kgdbts: ERROR failed sstep get emulation\n");
e8d31c20
JW
424 }
425 sstep_state++;
426}
427
428static int emul_sstep_put(char *put_str, char *arg)
429{
430 if (!arch_needs_sstep_emulation) {
486c5987
JW
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;
e8d31c20
JW
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)) {
974460c5 451 eprintk("kgdbts: failed sstep break set\n");
e8d31c20
JW
452 return 1;
453 }
454 break;
455 case 3:
456 if (strncmp(put_str, "$T0", 3)) {
974460c5 457 eprintk("kgdbts: failed continue sstep\n");
e8d31c20
JW
458 return 1;
459 }
460 break;
461 case 4:
462 if (strncmp(put_str, "$OK", 3)) {
974460c5 463 eprintk("kgdbts: failed sstep break unset\n");
e8d31c20
JW
464 return 1;
465 }
466 /* Single step is complete so continue on! */
467 sstep_state = 0;
468 return 0;
469 default:
974460c5 470 eprintk("kgdbts: ERROR failed sstep put emulation\n");
e8d31c20
JW
471 }
472
473 /* Continue on the same test line until emulation is complete */
474 ts.idx--;
475 return 0;
476}
477
478static 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 */
489static 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 */
500static 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 */
001fddf5 504 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
e8d31c20
JW
505 { "write", "OK", write_regs },
506 { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
507 { "D", "OK" }, /* Detach */
001fddf5 508 { "D", "OK", NULL, got_break }, /* On success we made it here */
e8d31c20
JW
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 */
516static 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 */
534static struct test_struct singlestep_break_test[] = {
535 { "?", "S0*" }, /* Clear break points */
536 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
486c5987
JW
537 { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
538 { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
001fddf5 539 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
e8d31c20 540 { "write", "OK", write_regs }, /* Write registers */
e8d31c20 541 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
001fddf5 542 { "g", "kgdbts_break_test", NULL, check_single_step },
e8d31c20
JW
543 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
544 { "c", "T0*", }, /* Continue */
001fddf5 545 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
e8d31c20
JW
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 */
555static struct test_struct do_fork_test[] = {
556 { "?", "S0*" }, /* Clear break points */
557 { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
486c5987
JW
558 { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
559 { "do_fork", "OK", sw_rem_break }, /*remove breakpoint */
001fddf5 560 { "g", "do_fork", NULL, check_and_rewind_pc }, /* check location */
e8d31c20 561 { "write", "OK", write_regs }, /* Write registers */
e8d31c20 562 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
001fddf5 563 { "g", "do_fork", NULL, check_single_step },
e8d31c20
JW
564 { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
565 { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
001fddf5 566 { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
e8d31c20
JW
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 */
573static struct test_struct sys_open_test[] = {
574 { "?", "S0*" }, /* Clear break points */
575 { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
486c5987
JW
576 { "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
577 { "sys_open", "OK", sw_rem_break }, /*remove breakpoint */
001fddf5 578 { "g", "sys_open", NULL, check_and_rewind_pc }, /* check location */
e8d31c20 579 { "write", "OK", write_regs }, /* Write registers */
e8d31c20 580 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
001fddf5 581 { "g", "sys_open", NULL, check_single_step },
e8d31c20
JW
582 { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
583 { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
001fddf5 584 { "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
e8d31c20
JW
585 { "", "" },
586};
587
588/*
589 * Test for hitting a simple hw breakpoint
590 */
591static 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 */
001fddf5 595 { "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
e8d31c20
JW
596 { "write", "OK", write_regs },
597 { "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
598 { "D", "OK" }, /* Detach */
001fddf5 599 { "D", "OK", NULL, got_break }, /* On success we made it here */
e8d31c20
JW
600 { "", "" },
601};
602
603/*
604 * Test for hitting a hw write breakpoint
605 */
606static struct test_struct hw_write_break_test[] = {
607 { "?", "S0*" }, /* Clear break points */
608 { "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
001fddf5
HH
609 { "c", "T0*", NULL, got_break }, /* Continue */
610 { "g", "silent", NULL, check_and_rewind_pc },
e8d31c20
JW
611 { "write", "OK", write_regs },
612 { "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
613 { "D", "OK" }, /* Detach */
001fddf5 614 { "D", "OK", NULL, got_break }, /* On success we made it here */
e8d31c20
JW
615 { "", "" },
616};
617
618/*
619 * Test for hitting a hw access breakpoint
620 */
621static struct test_struct hw_access_break_test[] = {
622 { "?", "S0*" }, /* Clear break points */
623 { "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
001fddf5
HH
624 { "c", "T0*", NULL, got_break }, /* Continue */
625 { "g", "silent", NULL, check_and_rewind_pc },
e8d31c20
JW
626 { "write", "OK", write_regs },
627 { "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
628 { "D", "OK" }, /* Detach */
001fddf5 629 { "D", "OK", NULL, got_break }, /* On success we made it here */
e8d31c20
JW
630 { "", "" },
631};
632
633/*
634 * Test for hitting a hw access breakpoint
635 */
636static struct test_struct nmi_sleep_test[] = {
637 { "?", "S0*" }, /* Clear break points */
001fddf5 638 { "c", "T0*", NULL, got_break }, /* Continue */
e8d31c20 639 { "D", "OK" }, /* Detach */
001fddf5 640 { "D", "OK", NULL, got_break }, /* On success we made it here */
e8d31c20
JW
641 { "", "" },
642};
643
644static 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, "#");
827e609b
HH
657 get_buf[count + 2] = hex_asc_hi(checksum);
658 get_buf[count + 3] = hex_asc_lo(checksum);
e8d31c20
JW
659 get_buf[count + 4] = '\0';
660 v2printk("get%i: %s\n", ts.idx, get_buf);
661}
662
663static 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
25985edc 677 * it, or stop if the received string is complete.
e8d31c20
JW
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
693static 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') {
974460c5 715 eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
e8d31c20
JW
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') {
974460c5 730 eprintk("kgdbts: ERROR: beyond end of test on"
e8d31c20
JW
731 " '%s' line %i\n", ts.name, ts.idx);
732 return 0;
733 }
734
735 if (put_buf_cnt >= BUFMAX) {
974460c5 736 eprintk("kgdbts: ERROR: put buffer overflow on"
e8d31c20
JW
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] == '#') {
b4f1b67b
RK
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 }
e8d31c20
JW
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)) {
974460c5 760 eprintk("kgdbts: ERROR PUT: end of test "
e8d31c20
JW
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
772static 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
779static 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
804static 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
974460c5 824 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
b33cb815
JW
825 if (is_hw_breakpoint)
826 hwbreaks_ok = 0;
e8d31c20
JW
827}
828
829static 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) {
b33cb815 844 if (test_complete == 2) {
974460c5 845 eprintk("kgdbts: ERROR %s broke on access\n",
e8d31c20 846 ts.name);
b33cb815
JW
847 hwbreaks_ok = 0;
848 }
e8d31c20
JW
849 hw_break_val_write();
850 }
851 kgdb_breakpoint();
852
853 if (test_complete == 1)
854 return;
855
974460c5 856 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
b33cb815 857 hwbreaks_ok = 0;
e8d31c20
JW
858}
859
860static 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)
974460c5 874 eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
e8d31c20
JW
875 kgdb_breakpoint();
876 if (test_complete == 1)
877 return;
878
974460c5 879 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
e8d31c20
JW
880}
881
882static 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
891static 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
900static 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
909static 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
456ca7ff
JW
920static 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
e8d31c20
JW
936static void kgdbts_run_tests(void)
937{
938 char *ptr;
939 int fork_test = 0;
001fddf5 940 int do_sys_open_test = 0;
7cfcd985 941 int sstep_test = 1000;
e8d31c20 942 int nmi_sleep = 0;
7cfcd985 943 int i;
e8d31c20 944
59d309f9 945 ptr = strchr(config, 'F');
e8d31c20 946 if (ptr)
001fddf5 947 fork_test = simple_strtol(ptr + 1, NULL, 10);
59d309f9 948 ptr = strchr(config, 'S');
e8d31c20 949 if (ptr)
001fddf5 950 do_sys_open_test = simple_strtol(ptr + 1, NULL, 10);
59d309f9 951 ptr = strchr(config, 'N');
e8d31c20
JW
952 if (ptr)
953 nmi_sleep = simple_strtol(ptr+1, NULL, 10);
59d309f9 954 ptr = strchr(config, 'I');
7cfcd985
JW
955 if (ptr)
956 sstep_test = simple_strtol(ptr+1, NULL, 10);
e8d31c20 957
456ca7ff
JW
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
e8d31c20
JW
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();
7cfcd985
JW
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 }
e8d31c20
JW
984
985 /* ===Optional tests=== */
986
e8d31c20
JW
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);
001fddf5 1000 kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
e8d31c20
JW
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 */
001fddf5
HH
1009 if (do_sys_open_test) {
1010 repeat_test = do_sys_open_test;
e8d31c20
JW
1011 printk(KERN_INFO "kgdbts:RUN sys_open for %i breakpoints\n",
1012 repeat_test);
001fddf5 1013 kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
e8d31c20
JW
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
1022static int kgdbts_option_setup(char *opt)
1023{
adb4b83c 1024 if (strlen(opt) >= MAX_CONFIG_LEN) {
e8d31c20
JW
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
1041static 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
1064noconfig:
1065 config[0] = 0;
1066 configured = 0;
1067
1068 return err;
1069}
1070
1071static int __init init_kgdbts(void)
1072{
1073 /* Already configured? */
1074 if (configured == 1)
1075 return 0;
1076
1077 return configure_kgdbts();
1078}
1079
e8d31c20
JW
1080static 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
1090static void kgdbts_put_char(u8 chr)
1091{
1092 if (ts.run_test)
1093 ts.run_test(0, chr);
1094}
1095
1096static 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
4dacd5c0
DD
1111 if (configured == 1) {
1112 printk(KERN_ERR "kgdbts: ERROR: Already configured and running.\n");
e8d31c20
JW
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
e8d31c20
JW
1121 /* Go and configure with the new params. */
1122 return configure_kgdbts();
1123}
1124
1125static 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
1132static 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
1139static 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
1147module_init(init_kgdbts);
e8d31c20
JW
1148module_param_call(kgdbts, param_set_kgdbts_var, param_get_string, &kps, 0644);
1149MODULE_PARM_DESC(kgdbts, "<A|V1|V2>[F#|S#][N#]");
1150MODULE_DESCRIPTION("KGDB Test Suite");
1151MODULE_LICENSE("GPL");
1152MODULE_AUTHOR("Wind River Systems, Inc.");
1153
This page took 0.346834 seconds and 5 git commands to generate.