Linux Audio

Check our new training course

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