18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * kgdbts is a test suite for kgdb for the sole purpose of validating
48c2ecf20Sopenharmony_ci * that key pieces of the kgdb internals are working properly such as
58c2ecf20Sopenharmony_ci * HW/SW breakpoints, single stepping, and NMI.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Created by: Jason Wessel <jason.wessel@windriver.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (c) 2008 Wind River Systems, Inc.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci/* Information about the kgdb test suite.
128c2ecf20Sopenharmony_ci * -------------------------------------
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The kgdb test suite is designed as a KGDB I/O module which
158c2ecf20Sopenharmony_ci * simulates the communications that a debugger would have with kgdb.
168c2ecf20Sopenharmony_ci * The tests are broken up in to a line by line and referenced here as
178c2ecf20Sopenharmony_ci * a "get" which is kgdb requesting input and "put" which is kgdb
188c2ecf20Sopenharmony_ci * sending a response.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * The kgdb suite can be invoked from the kernel command line
218c2ecf20Sopenharmony_ci * arguments system or executed dynamically at run time.  The test
228c2ecf20Sopenharmony_ci * suite uses the variable "kgdbts" to obtain the information about
238c2ecf20Sopenharmony_ci * which tests to run and to configure the verbosity level.  The
248c2ecf20Sopenharmony_ci * following are the various characters you can use with the kgdbts=
258c2ecf20Sopenharmony_ci * line:
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * When using the "kgdbts=" you only choose one of the following core
288c2ecf20Sopenharmony_ci * test types:
298c2ecf20Sopenharmony_ci * A = Run all the core tests silently
308c2ecf20Sopenharmony_ci * V1 = Run all the core tests with minimal output
318c2ecf20Sopenharmony_ci * V2 = Run all the core tests in debug mode
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * You can also specify optional tests:
348c2ecf20Sopenharmony_ci * N## = Go to sleep with interrupts of for ## seconds
358c2ecf20Sopenharmony_ci *       to test the HW NMI watchdog
368c2ecf20Sopenharmony_ci * F## = Break at kernel_clone for ## iterations
378c2ecf20Sopenharmony_ci * S## = Break at sys_open for ## iterations
388c2ecf20Sopenharmony_ci * I## = Run the single step test ## iterations
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * NOTE: that the kernel_clone and sys_open tests are mutually exclusive.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * To invoke the kgdb test suite from boot you use a kernel start
438c2ecf20Sopenharmony_ci * argument as follows:
448c2ecf20Sopenharmony_ci * 	kgdbts=V1 kgdbwait
458c2ecf20Sopenharmony_ci * Or if you wanted to perform the NMI test for 6 seconds and kernel_clone
468c2ecf20Sopenharmony_ci * test for 100 forks, you could use:
478c2ecf20Sopenharmony_ci * 	kgdbts=V1N6F100 kgdbwait
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * The test suite can also be invoked at run time with:
508c2ecf20Sopenharmony_ci *	echo kgdbts=V1N6F100 > /sys/module/kgdbts/parameters/kgdbts
518c2ecf20Sopenharmony_ci * Or as another example:
528c2ecf20Sopenharmony_ci *	echo kgdbts=V2 > /sys/module/kgdbts/parameters/kgdbts
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * When developing a new kgdb arch specific implementation or
558c2ecf20Sopenharmony_ci * using these tests for the purpose of regression testing,
568c2ecf20Sopenharmony_ci * several invocations are required.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * 1) Boot with the test suite enabled by using the kernel arguments
598c2ecf20Sopenharmony_ci *       "kgdbts=V1F100 kgdbwait"
608c2ecf20Sopenharmony_ci *    ## If kgdb arch specific implementation has NMI use
618c2ecf20Sopenharmony_ci *       "kgdbts=V1N6F100
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * 2) After the system boot run the basic test.
648c2ecf20Sopenharmony_ci * echo kgdbts=V1 > /sys/module/kgdbts/parameters/kgdbts
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * 3) Run the concurrency tests.  It is best to use n+1
678c2ecf20Sopenharmony_ci *    while loops where n is the number of cpus you have
688c2ecf20Sopenharmony_ci *    in your system.  The example below uses only two
698c2ecf20Sopenharmony_ci *    loops.
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * ## This tests break points on sys_open
728c2ecf20Sopenharmony_ci * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
738c2ecf20Sopenharmony_ci * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
748c2ecf20Sopenharmony_ci * echo kgdbts=V1S10000 > /sys/module/kgdbts/parameters/kgdbts
758c2ecf20Sopenharmony_ci * fg # and hit control-c
768c2ecf20Sopenharmony_ci * fg # and hit control-c
778c2ecf20Sopenharmony_ci * ## This tests break points on kernel_clone
788c2ecf20Sopenharmony_ci * while [ 1 ] ; do date > /dev/null ; done &
798c2ecf20Sopenharmony_ci * while [ 1 ] ; do date > /dev/null ; done &
808c2ecf20Sopenharmony_ci * echo kgdbts=V1F1000 > /sys/module/kgdbts/parameters/kgdbts
818c2ecf20Sopenharmony_ci * fg # and hit control-c
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#include <linux/kernel.h>
868c2ecf20Sopenharmony_ci#include <linux/kgdb.h>
878c2ecf20Sopenharmony_ci#include <linux/ctype.h>
888c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
898c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
908c2ecf20Sopenharmony_ci#include <linux/nmi.h>
918c2ecf20Sopenharmony_ci#include <linux/delay.h>
928c2ecf20Sopenharmony_ci#include <linux/kthread.h>
938c2ecf20Sopenharmony_ci#include <linux/module.h>
948c2ecf20Sopenharmony_ci#include <linux/sched/task.h>
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#include <asm/sections.h>
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define v1printk(a...) do {		\
998c2ecf20Sopenharmony_ci	if (verbose)			\
1008c2ecf20Sopenharmony_ci		printk(KERN_INFO a);	\
1018c2ecf20Sopenharmony_ci} while (0)
1028c2ecf20Sopenharmony_ci#define v2printk(a...) do {		\
1038c2ecf20Sopenharmony_ci	if (verbose > 1) {		\
1048c2ecf20Sopenharmony_ci		printk(KERN_INFO a);	\
1058c2ecf20Sopenharmony_ci	}				\
1068c2ecf20Sopenharmony_ci	touch_nmi_watchdog();		\
1078c2ecf20Sopenharmony_ci} while (0)
1088c2ecf20Sopenharmony_ci#define eprintk(a...) do {		\
1098c2ecf20Sopenharmony_ci	printk(KERN_ERR a);		\
1108c2ecf20Sopenharmony_ci	WARN_ON(1);			\
1118c2ecf20Sopenharmony_ci} while (0)
1128c2ecf20Sopenharmony_ci#define MAX_CONFIG_LEN		40
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct kgdb_io kgdbts_io_ops;
1158c2ecf20Sopenharmony_cistatic char get_buf[BUFMAX];
1168c2ecf20Sopenharmony_cistatic int get_buf_cnt;
1178c2ecf20Sopenharmony_cistatic char put_buf[BUFMAX];
1188c2ecf20Sopenharmony_cistatic int put_buf_cnt;
1198c2ecf20Sopenharmony_cistatic char scratch_buf[BUFMAX];
1208c2ecf20Sopenharmony_cistatic int verbose;
1218c2ecf20Sopenharmony_cistatic int repeat_test;
1228c2ecf20Sopenharmony_cistatic int test_complete;
1238c2ecf20Sopenharmony_cistatic int send_ack;
1248c2ecf20Sopenharmony_cistatic int final_ack;
1258c2ecf20Sopenharmony_cistatic int force_hwbrks;
1268c2ecf20Sopenharmony_cistatic int hwbreaks_ok;
1278c2ecf20Sopenharmony_cistatic int hw_break_val;
1288c2ecf20Sopenharmony_cistatic int hw_break_val2;
1298c2ecf20Sopenharmony_cistatic int cont_instead_of_sstep;
1308c2ecf20Sopenharmony_cistatic unsigned long cont_thread_id;
1318c2ecf20Sopenharmony_cistatic unsigned long sstep_thread_id;
1328c2ecf20Sopenharmony_ci#if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_SPARC)
1338c2ecf20Sopenharmony_cistatic int arch_needs_sstep_emulation = 1;
1348c2ecf20Sopenharmony_ci#else
1358c2ecf20Sopenharmony_cistatic int arch_needs_sstep_emulation;
1368c2ecf20Sopenharmony_ci#endif
1378c2ecf20Sopenharmony_cistatic unsigned long cont_addr;
1388c2ecf20Sopenharmony_cistatic unsigned long sstep_addr;
1398c2ecf20Sopenharmony_cistatic int restart_from_top_after_write;
1408c2ecf20Sopenharmony_cistatic int sstep_state;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/* Storage for the registers, in GDB format. */
1438c2ecf20Sopenharmony_cistatic unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
1448c2ecf20Sopenharmony_ci					sizeof(unsigned long) - 1) /
1458c2ecf20Sopenharmony_ci					sizeof(unsigned long)];
1468c2ecf20Sopenharmony_cistatic struct pt_regs kgdbts_regs;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
1498c2ecf20Sopenharmony_cistatic int configured		= -1;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#ifdef CONFIG_KGDB_TESTS_BOOT_STRING
1528c2ecf20Sopenharmony_cistatic char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
1538c2ecf20Sopenharmony_ci#else
1548c2ecf20Sopenharmony_cistatic char config[MAX_CONFIG_LEN];
1558c2ecf20Sopenharmony_ci#endif
1568c2ecf20Sopenharmony_cistatic struct kparam_string kps = {
1578c2ecf20Sopenharmony_ci	.string			= config,
1588c2ecf20Sopenharmony_ci	.maxlen			= MAX_CONFIG_LEN,
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void fill_get_buf(char *buf);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistruct test_struct {
1648c2ecf20Sopenharmony_ci	char *get;
1658c2ecf20Sopenharmony_ci	char *put;
1668c2ecf20Sopenharmony_ci	void (*get_handler)(char *);
1678c2ecf20Sopenharmony_ci	int (*put_handler)(char *, char *);
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistruct test_state {
1718c2ecf20Sopenharmony_ci	char *name;
1728c2ecf20Sopenharmony_ci	struct test_struct *tst;
1738c2ecf20Sopenharmony_ci	int idx;
1748c2ecf20Sopenharmony_ci	int (*run_test) (int, int);
1758c2ecf20Sopenharmony_ci	int (*validate_put) (char *);
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic struct test_state ts;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic int kgdbts_unreg_thread(void *ptr)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	/* Wait until the tests are complete and then ungresiter the I/O
1838c2ecf20Sopenharmony_ci	 * driver.
1848c2ecf20Sopenharmony_ci	 */
1858c2ecf20Sopenharmony_ci	while (!final_ack)
1868c2ecf20Sopenharmony_ci		msleep_interruptible(1500);
1878c2ecf20Sopenharmony_ci	/* Pause for any other threads to exit after final ack. */
1888c2ecf20Sopenharmony_ci	msleep_interruptible(1000);
1898c2ecf20Sopenharmony_ci	if (configured)
1908c2ecf20Sopenharmony_ci		kgdb_unregister_io_module(&kgdbts_io_ops);
1918c2ecf20Sopenharmony_ci	configured = 0;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/* This is noinline such that it can be used for a single location to
1978c2ecf20Sopenharmony_ci * place a breakpoint
1988c2ecf20Sopenharmony_ci */
1998c2ecf20Sopenharmony_cistatic noinline void kgdbts_break_test(void)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	v2printk("kgdbts: breakpoint complete\n");
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* Lookup symbol info in the kernel */
2058c2ecf20Sopenharmony_cistatic unsigned long lookup_addr(char *arg)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	unsigned long addr = 0;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (!strcmp(arg, "kgdbts_break_test"))
2108c2ecf20Sopenharmony_ci		addr = (unsigned long)kgdbts_break_test;
2118c2ecf20Sopenharmony_ci	else if (!strcmp(arg, "sys_open"))
2128c2ecf20Sopenharmony_ci		addr = (unsigned long)do_sys_open;
2138c2ecf20Sopenharmony_ci	else if (!strcmp(arg, "kernel_clone"))
2148c2ecf20Sopenharmony_ci		addr = (unsigned long)kernel_clone;
2158c2ecf20Sopenharmony_ci	else if (!strcmp(arg, "hw_break_val"))
2168c2ecf20Sopenharmony_ci		addr = (unsigned long)&hw_break_val;
2178c2ecf20Sopenharmony_ci	addr = (unsigned long) dereference_function_descriptor((void *)addr);
2188c2ecf20Sopenharmony_ci	return addr;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic void break_helper(char *bp_type, char *arg, unsigned long vaddr)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	unsigned long addr;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	if (arg)
2268c2ecf20Sopenharmony_ci		addr = lookup_addr(arg);
2278c2ecf20Sopenharmony_ci	else
2288c2ecf20Sopenharmony_ci		addr = vaddr;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	sprintf(scratch_buf, "%s,%lx,%i", bp_type, addr,
2318c2ecf20Sopenharmony_ci		BREAK_INSTR_SIZE);
2328c2ecf20Sopenharmony_ci	fill_get_buf(scratch_buf);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic void sw_break(char *arg)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	break_helper(force_hwbrks ? "Z1" : "Z0", arg, 0);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic void sw_rem_break(char *arg)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	break_helper(force_hwbrks ? "z1" : "z0", arg, 0);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void hw_break(char *arg)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	break_helper("Z1", arg, 0);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void hw_rem_break(char *arg)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	break_helper("z1", arg, 0);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic void hw_write_break(char *arg)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	break_helper("Z2", arg, 0);
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic void hw_rem_write_break(char *arg)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	break_helper("z2", arg, 0);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic void hw_access_break(char *arg)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	break_helper("Z4", arg, 0);
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic void hw_rem_access_break(char *arg)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	break_helper("z4", arg, 0);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic void hw_break_val_access(void)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	hw_break_val2 = hw_break_val;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic void hw_break_val_write(void)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	hw_break_val++;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic int get_thread_id_continue(char *put_str, char *arg)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	char *ptr = &put_str[11];
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (put_str[1] != 'T' || put_str[2] != '0')
2908c2ecf20Sopenharmony_ci		return 1;
2918c2ecf20Sopenharmony_ci	kgdb_hex2long(&ptr, &cont_thread_id);
2928c2ecf20Sopenharmony_ci	return 0;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic int check_and_rewind_pc(char *put_str, char *arg)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	unsigned long addr = lookup_addr(arg);
2988c2ecf20Sopenharmony_ci	unsigned long ip;
2998c2ecf20Sopenharmony_ci	int offset = 0;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
3028c2ecf20Sopenharmony_ci		 NUMREGBYTES);
3038c2ecf20Sopenharmony_ci	gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
3048c2ecf20Sopenharmony_ci	ip = instruction_pointer(&kgdbts_regs);
3058c2ecf20Sopenharmony_ci	v2printk("Stopped at IP: %lx\n", ip);
3068c2ecf20Sopenharmony_ci#ifdef GDB_ADJUSTS_BREAK_OFFSET
3078c2ecf20Sopenharmony_ci	/* On some arches, a breakpoint stop requires it to be decremented */
3088c2ecf20Sopenharmony_ci	if (addr + BREAK_INSTR_SIZE == ip)
3098c2ecf20Sopenharmony_ci		offset = -BREAK_INSTR_SIZE;
3108c2ecf20Sopenharmony_ci#endif
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (arch_needs_sstep_emulation && sstep_addr &&
3138c2ecf20Sopenharmony_ci	    ip + offset == sstep_addr &&
3148c2ecf20Sopenharmony_ci	    ((!strcmp(arg, "sys_open") || !strcmp(arg, "kernel_clone")))) {
3158c2ecf20Sopenharmony_ci		/* This is special case for emulated single step */
3168c2ecf20Sopenharmony_ci		v2printk("Emul: rewind hit single step bp\n");
3178c2ecf20Sopenharmony_ci		restart_from_top_after_write = 1;
3188c2ecf20Sopenharmony_ci	} else if (strcmp(arg, "silent") && ip + offset != addr) {
3198c2ecf20Sopenharmony_ci		eprintk("kgdbts: BP mismatch %lx expected %lx\n",
3208c2ecf20Sopenharmony_ci			   ip + offset, addr);
3218c2ecf20Sopenharmony_ci		return 1;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci	/* Readjust the instruction pointer if needed */
3248c2ecf20Sopenharmony_ci	ip += offset;
3258c2ecf20Sopenharmony_ci	cont_addr = ip;
3268c2ecf20Sopenharmony_ci#ifdef GDB_ADJUSTS_BREAK_OFFSET
3278c2ecf20Sopenharmony_ci	instruction_pointer_set(&kgdbts_regs, ip);
3288c2ecf20Sopenharmony_ci#endif
3298c2ecf20Sopenharmony_ci	return 0;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic int check_single_step(char *put_str, char *arg)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	unsigned long addr = lookup_addr(arg);
3358c2ecf20Sopenharmony_ci	static int matched_id;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/*
3388c2ecf20Sopenharmony_ci	 * From an arch indepent point of view the instruction pointer
3398c2ecf20Sopenharmony_ci	 * should be on a different instruction
3408c2ecf20Sopenharmony_ci	 */
3418c2ecf20Sopenharmony_ci	kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
3428c2ecf20Sopenharmony_ci		 NUMREGBYTES);
3438c2ecf20Sopenharmony_ci	gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
3448c2ecf20Sopenharmony_ci	v2printk("Singlestep stopped at IP: %lx\n",
3458c2ecf20Sopenharmony_ci		   instruction_pointer(&kgdbts_regs));
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (sstep_thread_id != cont_thread_id) {
3488c2ecf20Sopenharmony_ci		/*
3498c2ecf20Sopenharmony_ci		 * Ensure we stopped in the same thread id as before, else the
3508c2ecf20Sopenharmony_ci		 * debugger should continue until the original thread that was
3518c2ecf20Sopenharmony_ci		 * single stepped is scheduled again, emulating gdb's behavior.
3528c2ecf20Sopenharmony_ci		 */
3538c2ecf20Sopenharmony_ci		v2printk("ThrID does not match: %lx\n", cont_thread_id);
3548c2ecf20Sopenharmony_ci		if (arch_needs_sstep_emulation) {
3558c2ecf20Sopenharmony_ci			if (matched_id &&
3568c2ecf20Sopenharmony_ci			    instruction_pointer(&kgdbts_regs) != addr)
3578c2ecf20Sopenharmony_ci				goto continue_test;
3588c2ecf20Sopenharmony_ci			matched_id++;
3598c2ecf20Sopenharmony_ci			ts.idx -= 2;
3608c2ecf20Sopenharmony_ci			sstep_state = 0;
3618c2ecf20Sopenharmony_ci			return 0;
3628c2ecf20Sopenharmony_ci		}
3638c2ecf20Sopenharmony_ci		cont_instead_of_sstep = 1;
3648c2ecf20Sopenharmony_ci		ts.idx -= 4;
3658c2ecf20Sopenharmony_ci		return 0;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_cicontinue_test:
3688c2ecf20Sopenharmony_ci	matched_id = 0;
3698c2ecf20Sopenharmony_ci	if (instruction_pointer(&kgdbts_regs) == addr) {
3708c2ecf20Sopenharmony_ci		eprintk("kgdbts: SingleStep failed at %lx\n",
3718c2ecf20Sopenharmony_ci			   instruction_pointer(&kgdbts_regs));
3728c2ecf20Sopenharmony_ci		return 1;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return 0;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic void write_regs(char *arg)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	memset(scratch_buf, 0, sizeof(scratch_buf));
3818c2ecf20Sopenharmony_ci	scratch_buf[0] = 'G';
3828c2ecf20Sopenharmony_ci	pt_regs_to_gdb_regs(kgdbts_gdb_regs, &kgdbts_regs);
3838c2ecf20Sopenharmony_ci	kgdb_mem2hex((char *)kgdbts_gdb_regs, &scratch_buf[1], NUMREGBYTES);
3848c2ecf20Sopenharmony_ci	fill_get_buf(scratch_buf);
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic void skip_back_repeat_test(char *arg)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	int go_back = simple_strtol(arg, NULL, 10);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	repeat_test--;
3928c2ecf20Sopenharmony_ci	if (repeat_test <= 0) {
3938c2ecf20Sopenharmony_ci		ts.idx++;
3948c2ecf20Sopenharmony_ci	} else {
3958c2ecf20Sopenharmony_ci		if (repeat_test % 100 == 0)
3968c2ecf20Sopenharmony_ci			v1printk("kgdbts:RUN ... %d remaining\n", repeat_test);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci		ts.idx -= go_back;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci	fill_get_buf(ts.tst[ts.idx].get);
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic int got_break(char *put_str, char *arg)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	test_complete = 1;
4068c2ecf20Sopenharmony_ci	if (!strncmp(put_str+1, arg, 2)) {
4078c2ecf20Sopenharmony_ci		if (!strncmp(arg, "T0", 2))
4088c2ecf20Sopenharmony_ci			test_complete = 2;
4098c2ecf20Sopenharmony_ci		return 0;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci	return 1;
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic void get_cont_catch(char *arg)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	/* Always send detach because the test is completed at this point */
4178c2ecf20Sopenharmony_ci	fill_get_buf("D");
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic int put_cont_catch(char *put_str, char *arg)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	/* This is at the end of the test and we catch any and all input */
4238c2ecf20Sopenharmony_ci	v2printk("kgdbts: cleanup task: %lx\n", sstep_thread_id);
4248c2ecf20Sopenharmony_ci	ts.idx--;
4258c2ecf20Sopenharmony_ci	return 0;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic int emul_reset(char *put_str, char *arg)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	if (strncmp(put_str, "$OK", 3))
4318c2ecf20Sopenharmony_ci		return 1;
4328c2ecf20Sopenharmony_ci	if (restart_from_top_after_write) {
4338c2ecf20Sopenharmony_ci		restart_from_top_after_write = 0;
4348c2ecf20Sopenharmony_ci		ts.idx = -1;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci	return 0;
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cistatic void emul_sstep_get(char *arg)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	if (!arch_needs_sstep_emulation) {
4428c2ecf20Sopenharmony_ci		if (cont_instead_of_sstep) {
4438c2ecf20Sopenharmony_ci			cont_instead_of_sstep = 0;
4448c2ecf20Sopenharmony_ci			fill_get_buf("c");
4458c2ecf20Sopenharmony_ci		} else {
4468c2ecf20Sopenharmony_ci			fill_get_buf(arg);
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci		return;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci	switch (sstep_state) {
4518c2ecf20Sopenharmony_ci	case 0:
4528c2ecf20Sopenharmony_ci		v2printk("Emulate single step\n");
4538c2ecf20Sopenharmony_ci		/* Start by looking at the current PC */
4548c2ecf20Sopenharmony_ci		fill_get_buf("g");
4558c2ecf20Sopenharmony_ci		break;
4568c2ecf20Sopenharmony_ci	case 1:
4578c2ecf20Sopenharmony_ci		/* set breakpoint */
4588c2ecf20Sopenharmony_ci		break_helper("Z0", NULL, sstep_addr);
4598c2ecf20Sopenharmony_ci		break;
4608c2ecf20Sopenharmony_ci	case 2:
4618c2ecf20Sopenharmony_ci		/* Continue */
4628c2ecf20Sopenharmony_ci		fill_get_buf("c");
4638c2ecf20Sopenharmony_ci		break;
4648c2ecf20Sopenharmony_ci	case 3:
4658c2ecf20Sopenharmony_ci		/* Clear breakpoint */
4668c2ecf20Sopenharmony_ci		break_helper("z0", NULL, sstep_addr);
4678c2ecf20Sopenharmony_ci		break;
4688c2ecf20Sopenharmony_ci	default:
4698c2ecf20Sopenharmony_ci		eprintk("kgdbts: ERROR failed sstep get emulation\n");
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci	sstep_state++;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic int emul_sstep_put(char *put_str, char *arg)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	if (!arch_needs_sstep_emulation) {
4778c2ecf20Sopenharmony_ci		char *ptr = &put_str[11];
4788c2ecf20Sopenharmony_ci		if (put_str[1] != 'T' || put_str[2] != '0')
4798c2ecf20Sopenharmony_ci			return 1;
4808c2ecf20Sopenharmony_ci		kgdb_hex2long(&ptr, &sstep_thread_id);
4818c2ecf20Sopenharmony_ci		return 0;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci	switch (sstep_state) {
4848c2ecf20Sopenharmony_ci	case 1:
4858c2ecf20Sopenharmony_ci		/* validate the "g" packet to get the IP */
4868c2ecf20Sopenharmony_ci		kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
4878c2ecf20Sopenharmony_ci			 NUMREGBYTES);
4888c2ecf20Sopenharmony_ci		gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
4898c2ecf20Sopenharmony_ci		v2printk("Stopped at IP: %lx\n",
4908c2ecf20Sopenharmony_ci			 instruction_pointer(&kgdbts_regs));
4918c2ecf20Sopenharmony_ci		/* Want to stop at IP + break instruction size by default */
4928c2ecf20Sopenharmony_ci		sstep_addr = cont_addr + BREAK_INSTR_SIZE;
4938c2ecf20Sopenharmony_ci		break;
4948c2ecf20Sopenharmony_ci	case 2:
4958c2ecf20Sopenharmony_ci		if (strncmp(put_str, "$OK", 3)) {
4968c2ecf20Sopenharmony_ci			eprintk("kgdbts: failed sstep break set\n");
4978c2ecf20Sopenharmony_ci			return 1;
4988c2ecf20Sopenharmony_ci		}
4998c2ecf20Sopenharmony_ci		break;
5008c2ecf20Sopenharmony_ci	case 3:
5018c2ecf20Sopenharmony_ci		if (strncmp(put_str, "$T0", 3)) {
5028c2ecf20Sopenharmony_ci			eprintk("kgdbts: failed continue sstep\n");
5038c2ecf20Sopenharmony_ci			return 1;
5048c2ecf20Sopenharmony_ci		} else {
5058c2ecf20Sopenharmony_ci			char *ptr = &put_str[11];
5068c2ecf20Sopenharmony_ci			kgdb_hex2long(&ptr, &sstep_thread_id);
5078c2ecf20Sopenharmony_ci		}
5088c2ecf20Sopenharmony_ci		break;
5098c2ecf20Sopenharmony_ci	case 4:
5108c2ecf20Sopenharmony_ci		if (strncmp(put_str, "$OK", 3)) {
5118c2ecf20Sopenharmony_ci			eprintk("kgdbts: failed sstep break unset\n");
5128c2ecf20Sopenharmony_ci			return 1;
5138c2ecf20Sopenharmony_ci		}
5148c2ecf20Sopenharmony_ci		/* Single step is complete so continue on! */
5158c2ecf20Sopenharmony_ci		sstep_state = 0;
5168c2ecf20Sopenharmony_ci		return 0;
5178c2ecf20Sopenharmony_ci	default:
5188c2ecf20Sopenharmony_ci		eprintk("kgdbts: ERROR failed sstep put emulation\n");
5198c2ecf20Sopenharmony_ci	}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* Continue on the same test line until emulation is complete */
5228c2ecf20Sopenharmony_ci	ts.idx--;
5238c2ecf20Sopenharmony_ci	return 0;
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic int final_ack_set(char *put_str, char *arg)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	if (strncmp(put_str+1, arg, 2))
5298c2ecf20Sopenharmony_ci		return 1;
5308c2ecf20Sopenharmony_ci	final_ack = 1;
5318c2ecf20Sopenharmony_ci	return 0;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci/*
5348c2ecf20Sopenharmony_ci * Test to plant a breakpoint and detach, which should clear out the
5358c2ecf20Sopenharmony_ci * breakpoint and restore the original instruction.
5368c2ecf20Sopenharmony_ci */
5378c2ecf20Sopenharmony_cistatic struct test_struct plant_and_detach_test[] = {
5388c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
5398c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
5408c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach */
5418c2ecf20Sopenharmony_ci	{ "", "" },
5428c2ecf20Sopenharmony_ci};
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci/*
5458c2ecf20Sopenharmony_ci * Simple test to write in a software breakpoint, check for the
5468c2ecf20Sopenharmony_ci * correct stop location and detach.
5478c2ecf20Sopenharmony_ci */
5488c2ecf20Sopenharmony_cistatic struct test_struct sw_breakpoint_test[] = {
5498c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
5508c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
5518c2ecf20Sopenharmony_ci	{ "c", "T0*", }, /* Continue */
5528c2ecf20Sopenharmony_ci	{ "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
5538c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs },
5548c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
5558c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach */
5568c2ecf20Sopenharmony_ci	{ "D", "OK", NULL,  got_break }, /* On success we made it here */
5578c2ecf20Sopenharmony_ci	{ "", "" },
5588c2ecf20Sopenharmony_ci};
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci/*
5618c2ecf20Sopenharmony_ci * Test a known bad memory read location to test the fault handler and
5628c2ecf20Sopenharmony_ci * read bytes 1-8 at the bad address
5638c2ecf20Sopenharmony_ci */
5648c2ecf20Sopenharmony_cistatic struct test_struct bad_read_test[] = {
5658c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
5668c2ecf20Sopenharmony_ci	{ "m0,1", "E*" }, /* read 1 byte at address 1 */
5678c2ecf20Sopenharmony_ci	{ "m0,2", "E*" }, /* read 1 byte at address 2 */
5688c2ecf20Sopenharmony_ci	{ "m0,3", "E*" }, /* read 1 byte at address 3 */
5698c2ecf20Sopenharmony_ci	{ "m0,4", "E*" }, /* read 1 byte at address 4 */
5708c2ecf20Sopenharmony_ci	{ "m0,5", "E*" }, /* read 1 byte at address 5 */
5718c2ecf20Sopenharmony_ci	{ "m0,6", "E*" }, /* read 1 byte at address 6 */
5728c2ecf20Sopenharmony_ci	{ "m0,7", "E*" }, /* read 1 byte at address 7 */
5738c2ecf20Sopenharmony_ci	{ "m0,8", "E*" }, /* read 1 byte at address 8 */
5748c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach which removes all breakpoints and continues */
5758c2ecf20Sopenharmony_ci	{ "", "" },
5768c2ecf20Sopenharmony_ci};
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci/*
5798c2ecf20Sopenharmony_ci * Test for hitting a breakpoint, remove it, single step, plant it
5808c2ecf20Sopenharmony_ci * again and detach.
5818c2ecf20Sopenharmony_ci */
5828c2ecf20Sopenharmony_cistatic struct test_struct singlestep_break_test[] = {
5838c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
5848c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
5858c2ecf20Sopenharmony_ci	{ "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
5868c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
5878c2ecf20Sopenharmony_ci	{ "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
5888c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs }, /* Write registers */
5898c2ecf20Sopenharmony_ci	{ "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
5908c2ecf20Sopenharmony_ci	{ "g", "kgdbts_break_test", NULL, check_single_step },
5918c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
5928c2ecf20Sopenharmony_ci	{ "c", "T0*", }, /* Continue */
5938c2ecf20Sopenharmony_ci	{ "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
5948c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs }, /* Write registers */
5958c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Remove all breakpoints and continues */
5968c2ecf20Sopenharmony_ci	{ "", "" },
5978c2ecf20Sopenharmony_ci};
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci/*
6008c2ecf20Sopenharmony_ci * Test for hitting a breakpoint at kernel_clone for what ever the number
6018c2ecf20Sopenharmony_ci * of iterations required by the variable repeat_test.
6028c2ecf20Sopenharmony_ci */
6038c2ecf20Sopenharmony_cistatic struct test_struct do_kernel_clone_test[] = {
6048c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
6058c2ecf20Sopenharmony_ci	{ "kernel_clone", "OK", sw_break, }, /* set sw breakpoint */
6068c2ecf20Sopenharmony_ci	{ "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
6078c2ecf20Sopenharmony_ci	{ "kernel_clone", "OK", sw_rem_break }, /*remove breakpoint */
6088c2ecf20Sopenharmony_ci	{ "g", "kernel_clone", NULL, check_and_rewind_pc }, /* check location */
6098c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs, emul_reset }, /* Write registers */
6108c2ecf20Sopenharmony_ci	{ "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
6118c2ecf20Sopenharmony_ci	{ "g", "kernel_clone", NULL, check_single_step },
6128c2ecf20Sopenharmony_ci	{ "kernel_clone", "OK", sw_break, }, /* set sw breakpoint */
6138c2ecf20Sopenharmony_ci	{ "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
6148c2ecf20Sopenharmony_ci	{ "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
6158c2ecf20Sopenharmony_ci	{ "", "", get_cont_catch, put_cont_catch },
6168c2ecf20Sopenharmony_ci};
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci/* Test for hitting a breakpoint at sys_open for what ever the number
6198c2ecf20Sopenharmony_ci * of iterations required by the variable repeat_test.
6208c2ecf20Sopenharmony_ci */
6218c2ecf20Sopenharmony_cistatic struct test_struct sys_open_test[] = {
6228c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
6238c2ecf20Sopenharmony_ci	{ "sys_open", "OK", sw_break, }, /* set sw breakpoint */
6248c2ecf20Sopenharmony_ci	{ "c", "T0*", NULL, get_thread_id_continue }, /* Continue */
6258c2ecf20Sopenharmony_ci	{ "sys_open", "OK", sw_rem_break }, /*remove breakpoint */
6268c2ecf20Sopenharmony_ci	{ "g", "sys_open", NULL, check_and_rewind_pc }, /* check location */
6278c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs, emul_reset }, /* Write registers */
6288c2ecf20Sopenharmony_ci	{ "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
6298c2ecf20Sopenharmony_ci	{ "g", "sys_open", NULL, check_single_step },
6308c2ecf20Sopenharmony_ci	{ "sys_open", "OK", sw_break, }, /* set sw breakpoint */
6318c2ecf20Sopenharmony_ci	{ "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
6328c2ecf20Sopenharmony_ci	{ "D", "OK", NULL, final_ack_set }, /* detach and unregister I/O */
6338c2ecf20Sopenharmony_ci	{ "", "", get_cont_catch, put_cont_catch },
6348c2ecf20Sopenharmony_ci};
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci/*
6378c2ecf20Sopenharmony_ci * Test for hitting a simple hw breakpoint
6388c2ecf20Sopenharmony_ci */
6398c2ecf20Sopenharmony_cistatic struct test_struct hw_breakpoint_test[] = {
6408c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
6418c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", hw_break, }, /* set hw breakpoint */
6428c2ecf20Sopenharmony_ci	{ "c", "T0*", }, /* Continue */
6438c2ecf20Sopenharmony_ci	{ "g", "kgdbts_break_test", NULL, check_and_rewind_pc },
6448c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs },
6458c2ecf20Sopenharmony_ci	{ "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
6468c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach */
6478c2ecf20Sopenharmony_ci	{ "D", "OK", NULL,  got_break }, /* On success we made it here */
6488c2ecf20Sopenharmony_ci	{ "", "" },
6498c2ecf20Sopenharmony_ci};
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/*
6528c2ecf20Sopenharmony_ci * Test for hitting a hw write breakpoint
6538c2ecf20Sopenharmony_ci */
6548c2ecf20Sopenharmony_cistatic struct test_struct hw_write_break_test[] = {
6558c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
6568c2ecf20Sopenharmony_ci	{ "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
6578c2ecf20Sopenharmony_ci	{ "c", "T0*", NULL, got_break }, /* Continue */
6588c2ecf20Sopenharmony_ci	{ "g", "silent", NULL, check_and_rewind_pc },
6598c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs },
6608c2ecf20Sopenharmony_ci	{ "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
6618c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach */
6628c2ecf20Sopenharmony_ci	{ "D", "OK", NULL,  got_break }, /* On success we made it here */
6638c2ecf20Sopenharmony_ci	{ "", "" },
6648c2ecf20Sopenharmony_ci};
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci/*
6678c2ecf20Sopenharmony_ci * Test for hitting a hw access breakpoint
6688c2ecf20Sopenharmony_ci */
6698c2ecf20Sopenharmony_cistatic struct test_struct hw_access_break_test[] = {
6708c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
6718c2ecf20Sopenharmony_ci	{ "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
6728c2ecf20Sopenharmony_ci	{ "c", "T0*", NULL, got_break }, /* Continue */
6738c2ecf20Sopenharmony_ci	{ "g", "silent", NULL, check_and_rewind_pc },
6748c2ecf20Sopenharmony_ci	{ "write", "OK", write_regs },
6758c2ecf20Sopenharmony_ci	{ "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
6768c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach */
6778c2ecf20Sopenharmony_ci	{ "D", "OK", NULL,  got_break }, /* On success we made it here */
6788c2ecf20Sopenharmony_ci	{ "", "" },
6798c2ecf20Sopenharmony_ci};
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci/*
6828c2ecf20Sopenharmony_ci * Test for hitting a hw access breakpoint
6838c2ecf20Sopenharmony_ci */
6848c2ecf20Sopenharmony_cistatic struct test_struct nmi_sleep_test[] = {
6858c2ecf20Sopenharmony_ci	{ "?", "S0*" }, /* Clear break points */
6868c2ecf20Sopenharmony_ci	{ "c", "T0*", NULL, got_break }, /* Continue */
6878c2ecf20Sopenharmony_ci	{ "D", "OK" }, /* Detach */
6888c2ecf20Sopenharmony_ci	{ "D", "OK", NULL,  got_break }, /* On success we made it here */
6898c2ecf20Sopenharmony_ci	{ "", "" },
6908c2ecf20Sopenharmony_ci};
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistatic void fill_get_buf(char *buf)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	unsigned char checksum = 0;
6958c2ecf20Sopenharmony_ci	int count = 0;
6968c2ecf20Sopenharmony_ci	char ch;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	strcpy(get_buf, "$");
6998c2ecf20Sopenharmony_ci	strcat(get_buf, buf);
7008c2ecf20Sopenharmony_ci	while ((ch = buf[count])) {
7018c2ecf20Sopenharmony_ci		checksum += ch;
7028c2ecf20Sopenharmony_ci		count++;
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci	strcat(get_buf, "#");
7058c2ecf20Sopenharmony_ci	get_buf[count + 2] = hex_asc_hi(checksum);
7068c2ecf20Sopenharmony_ci	get_buf[count + 3] = hex_asc_lo(checksum);
7078c2ecf20Sopenharmony_ci	get_buf[count + 4] = '\0';
7088c2ecf20Sopenharmony_ci	v2printk("get%i: %s\n", ts.idx, get_buf);
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_cistatic int validate_simple_test(char *put_str)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	char *chk_str;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	if (ts.tst[ts.idx].put_handler)
7168c2ecf20Sopenharmony_ci		return ts.tst[ts.idx].put_handler(put_str,
7178c2ecf20Sopenharmony_ci			ts.tst[ts.idx].put);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	chk_str = ts.tst[ts.idx].put;
7208c2ecf20Sopenharmony_ci	if (*put_str == '$')
7218c2ecf20Sopenharmony_ci		put_str++;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	while (*chk_str != '\0' && *put_str != '\0') {
7248c2ecf20Sopenharmony_ci		/* If someone does a * to match the rest of the string, allow
7258c2ecf20Sopenharmony_ci		 * it, or stop if the received string is complete.
7268c2ecf20Sopenharmony_ci		 */
7278c2ecf20Sopenharmony_ci		if (*put_str == '#' || *chk_str == '*')
7288c2ecf20Sopenharmony_ci			return 0;
7298c2ecf20Sopenharmony_ci		if (*put_str != *chk_str)
7308c2ecf20Sopenharmony_ci			return 1;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci		chk_str++;
7338c2ecf20Sopenharmony_ci		put_str++;
7348c2ecf20Sopenharmony_ci	}
7358c2ecf20Sopenharmony_ci	if (*chk_str == '\0' && (*put_str == '\0' || *put_str == '#'))
7368c2ecf20Sopenharmony_ci		return 0;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	return 1;
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic int run_simple_test(int is_get_char, int chr)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	int ret = 0;
7448c2ecf20Sopenharmony_ci	if (is_get_char) {
7458c2ecf20Sopenharmony_ci		/* Send an ACK on the get if a prior put completed and set the
7468c2ecf20Sopenharmony_ci		 * send ack variable
7478c2ecf20Sopenharmony_ci		 */
7488c2ecf20Sopenharmony_ci		if (send_ack) {
7498c2ecf20Sopenharmony_ci			send_ack = 0;
7508c2ecf20Sopenharmony_ci			return '+';
7518c2ecf20Sopenharmony_ci		}
7528c2ecf20Sopenharmony_ci		/* On the first get char, fill the transmit buffer and then
7538c2ecf20Sopenharmony_ci		 * take from the get_string.
7548c2ecf20Sopenharmony_ci		 */
7558c2ecf20Sopenharmony_ci		if (get_buf_cnt == 0) {
7568c2ecf20Sopenharmony_ci			if (ts.tst[ts.idx].get_handler)
7578c2ecf20Sopenharmony_ci				ts.tst[ts.idx].get_handler(ts.tst[ts.idx].get);
7588c2ecf20Sopenharmony_ci			else
7598c2ecf20Sopenharmony_ci				fill_get_buf(ts.tst[ts.idx].get);
7608c2ecf20Sopenharmony_ci		}
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci		if (get_buf[get_buf_cnt] == '\0') {
7638c2ecf20Sopenharmony_ci			eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
7648c2ecf20Sopenharmony_ci			   ts.name, ts.idx);
7658c2ecf20Sopenharmony_ci			get_buf_cnt = 0;
7668c2ecf20Sopenharmony_ci			fill_get_buf("D");
7678c2ecf20Sopenharmony_ci		}
7688c2ecf20Sopenharmony_ci		ret = get_buf[get_buf_cnt];
7698c2ecf20Sopenharmony_ci		get_buf_cnt++;
7708c2ecf20Sopenharmony_ci		return ret;
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	/* This callback is a put char which is when kgdb sends data to
7748c2ecf20Sopenharmony_ci	 * this I/O module.
7758c2ecf20Sopenharmony_ci	 */
7768c2ecf20Sopenharmony_ci	if (ts.tst[ts.idx].get[0] == '\0' && ts.tst[ts.idx].put[0] == '\0' &&
7778c2ecf20Sopenharmony_ci	    !ts.tst[ts.idx].get_handler) {
7788c2ecf20Sopenharmony_ci		eprintk("kgdbts: ERROR: beyond end of test on"
7798c2ecf20Sopenharmony_ci			   " '%s' line %i\n", ts.name, ts.idx);
7808c2ecf20Sopenharmony_ci		return 0;
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (put_buf_cnt >= BUFMAX) {
7848c2ecf20Sopenharmony_ci		eprintk("kgdbts: ERROR: put buffer overflow on"
7858c2ecf20Sopenharmony_ci			   " '%s' line %i\n", ts.name, ts.idx);
7868c2ecf20Sopenharmony_ci		put_buf_cnt = 0;
7878c2ecf20Sopenharmony_ci		return 0;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci	/* Ignore everything until the first valid packet start '$' */
7908c2ecf20Sopenharmony_ci	if (put_buf_cnt == 0 && chr != '$')
7918c2ecf20Sopenharmony_ci		return 0;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	put_buf[put_buf_cnt] = chr;
7948c2ecf20Sopenharmony_ci	put_buf_cnt++;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* End of packet == #XX so look for the '#' */
7978c2ecf20Sopenharmony_ci	if (put_buf_cnt > 3 && put_buf[put_buf_cnt - 3] == '#') {
7988c2ecf20Sopenharmony_ci		if (put_buf_cnt >= BUFMAX) {
7998c2ecf20Sopenharmony_ci			eprintk("kgdbts: ERROR: put buffer overflow on"
8008c2ecf20Sopenharmony_ci				" '%s' line %i\n", ts.name, ts.idx);
8018c2ecf20Sopenharmony_ci			put_buf_cnt = 0;
8028c2ecf20Sopenharmony_ci			return 0;
8038c2ecf20Sopenharmony_ci		}
8048c2ecf20Sopenharmony_ci		put_buf[put_buf_cnt] = '\0';
8058c2ecf20Sopenharmony_ci		v2printk("put%i: %s\n", ts.idx, put_buf);
8068c2ecf20Sopenharmony_ci		/* Trigger check here */
8078c2ecf20Sopenharmony_ci		if (ts.validate_put && ts.validate_put(put_buf)) {
8088c2ecf20Sopenharmony_ci			eprintk("kgdbts: ERROR PUT: end of test "
8098c2ecf20Sopenharmony_ci			   "buffer on '%s' line %i expected %s got %s\n",
8108c2ecf20Sopenharmony_ci			   ts.name, ts.idx, ts.tst[ts.idx].put, put_buf);
8118c2ecf20Sopenharmony_ci		}
8128c2ecf20Sopenharmony_ci		ts.idx++;
8138c2ecf20Sopenharmony_ci		put_buf_cnt = 0;
8148c2ecf20Sopenharmony_ci		get_buf_cnt = 0;
8158c2ecf20Sopenharmony_ci		send_ack = 1;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci	return 0;
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cistatic void init_simple_test(void)
8218c2ecf20Sopenharmony_ci{
8228c2ecf20Sopenharmony_ci	memset(&ts, 0, sizeof(ts));
8238c2ecf20Sopenharmony_ci	ts.run_test = run_simple_test;
8248c2ecf20Sopenharmony_ci	ts.validate_put = validate_simple_test;
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cistatic void run_plant_and_detach_test(int is_early)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	char before[BREAK_INSTR_SIZE];
8308c2ecf20Sopenharmony_ci	char after[BREAK_INSTR_SIZE];
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	copy_from_kernel_nofault(before, (char *)kgdbts_break_test,
8338c2ecf20Sopenharmony_ci	  BREAK_INSTR_SIZE);
8348c2ecf20Sopenharmony_ci	init_simple_test();
8358c2ecf20Sopenharmony_ci	ts.tst = plant_and_detach_test;
8368c2ecf20Sopenharmony_ci	ts.name = "plant_and_detach_test";
8378c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
8388c2ecf20Sopenharmony_ci	if (!is_early)
8398c2ecf20Sopenharmony_ci		kgdb_breakpoint();
8408c2ecf20Sopenharmony_ci	copy_from_kernel_nofault(after, (char *)kgdbts_break_test,
8418c2ecf20Sopenharmony_ci			BREAK_INSTR_SIZE);
8428c2ecf20Sopenharmony_ci	if (memcmp(before, after, BREAK_INSTR_SIZE)) {
8438c2ecf20Sopenharmony_ci		printk(KERN_CRIT "kgdbts: ERROR kgdb corrupted memory\n");
8448c2ecf20Sopenharmony_ci		panic("kgdb memory corruption");
8458c2ecf20Sopenharmony_ci	}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	/* complete the detach test */
8488c2ecf20Sopenharmony_ci	if (!is_early)
8498c2ecf20Sopenharmony_ci		kgdbts_break_test();
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic void run_breakpoint_test(int is_hw_breakpoint)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	test_complete = 0;
8558c2ecf20Sopenharmony_ci	init_simple_test();
8568c2ecf20Sopenharmony_ci	if (is_hw_breakpoint) {
8578c2ecf20Sopenharmony_ci		ts.tst = hw_breakpoint_test;
8588c2ecf20Sopenharmony_ci		ts.name = "hw_breakpoint_test";
8598c2ecf20Sopenharmony_ci	} else {
8608c2ecf20Sopenharmony_ci		ts.tst = sw_breakpoint_test;
8618c2ecf20Sopenharmony_ci		ts.name = "sw_breakpoint_test";
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
8648c2ecf20Sopenharmony_ci	kgdb_breakpoint();
8658c2ecf20Sopenharmony_ci	/* run code with the break point in it */
8668c2ecf20Sopenharmony_ci	kgdbts_break_test();
8678c2ecf20Sopenharmony_ci	kgdb_breakpoint();
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	if (test_complete)
8708c2ecf20Sopenharmony_ci		return;
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	eprintk("kgdbts: ERROR %s test failed\n", ts.name);
8738c2ecf20Sopenharmony_ci	if (is_hw_breakpoint)
8748c2ecf20Sopenharmony_ci		hwbreaks_ok = 0;
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_cistatic void run_hw_break_test(int is_write_test)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	test_complete = 0;
8808c2ecf20Sopenharmony_ci	init_simple_test();
8818c2ecf20Sopenharmony_ci	if (is_write_test) {
8828c2ecf20Sopenharmony_ci		ts.tst = hw_write_break_test;
8838c2ecf20Sopenharmony_ci		ts.name = "hw_write_break_test";
8848c2ecf20Sopenharmony_ci	} else {
8858c2ecf20Sopenharmony_ci		ts.tst = hw_access_break_test;
8868c2ecf20Sopenharmony_ci		ts.name = "hw_access_break_test";
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
8898c2ecf20Sopenharmony_ci	kgdb_breakpoint();
8908c2ecf20Sopenharmony_ci	hw_break_val_access();
8918c2ecf20Sopenharmony_ci	if (is_write_test) {
8928c2ecf20Sopenharmony_ci		if (test_complete == 2) {
8938c2ecf20Sopenharmony_ci			eprintk("kgdbts: ERROR %s broke on access\n",
8948c2ecf20Sopenharmony_ci				ts.name);
8958c2ecf20Sopenharmony_ci			hwbreaks_ok = 0;
8968c2ecf20Sopenharmony_ci		}
8978c2ecf20Sopenharmony_ci		hw_break_val_write();
8988c2ecf20Sopenharmony_ci	}
8998c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	if (test_complete == 1)
9028c2ecf20Sopenharmony_ci		return;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	eprintk("kgdbts: ERROR %s test failed\n", ts.name);
9058c2ecf20Sopenharmony_ci	hwbreaks_ok = 0;
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic void run_nmi_sleep_test(int nmi_sleep)
9098c2ecf20Sopenharmony_ci{
9108c2ecf20Sopenharmony_ci	unsigned long flags;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	init_simple_test();
9138c2ecf20Sopenharmony_ci	ts.tst = nmi_sleep_test;
9148c2ecf20Sopenharmony_ci	ts.name = "nmi_sleep_test";
9158c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
9168c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9178c2ecf20Sopenharmony_ci	local_irq_save(flags);
9188c2ecf20Sopenharmony_ci	mdelay(nmi_sleep*1000);
9198c2ecf20Sopenharmony_ci	touch_nmi_watchdog();
9208c2ecf20Sopenharmony_ci	local_irq_restore(flags);
9218c2ecf20Sopenharmony_ci	if (test_complete != 2)
9228c2ecf20Sopenharmony_ci		eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
9238c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9248c2ecf20Sopenharmony_ci	if (test_complete == 1)
9258c2ecf20Sopenharmony_ci		return;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	eprintk("kgdbts: ERROR %s test failed\n", ts.name);
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_cistatic void run_bad_read_test(void)
9318c2ecf20Sopenharmony_ci{
9328c2ecf20Sopenharmony_ci	init_simple_test();
9338c2ecf20Sopenharmony_ci	ts.tst = bad_read_test;
9348c2ecf20Sopenharmony_ci	ts.name = "bad_read_test";
9358c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
9368c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic void run_kernel_clone_test(void)
9408c2ecf20Sopenharmony_ci{
9418c2ecf20Sopenharmony_ci	init_simple_test();
9428c2ecf20Sopenharmony_ci	ts.tst = do_kernel_clone_test;
9438c2ecf20Sopenharmony_ci	ts.name = "do_kernel_clone_test";
9448c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
9458c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9468c2ecf20Sopenharmony_ci}
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_cistatic void run_sys_open_test(void)
9498c2ecf20Sopenharmony_ci{
9508c2ecf20Sopenharmony_ci	init_simple_test();
9518c2ecf20Sopenharmony_ci	ts.tst = sys_open_test;
9528c2ecf20Sopenharmony_ci	ts.name = "sys_open_test";
9538c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
9548c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_cistatic void run_singlestep_break_test(void)
9588c2ecf20Sopenharmony_ci{
9598c2ecf20Sopenharmony_ci	init_simple_test();
9608c2ecf20Sopenharmony_ci	ts.tst = singlestep_break_test;
9618c2ecf20Sopenharmony_ci	ts.name = "singlestep_breakpoint_test";
9628c2ecf20Sopenharmony_ci	/* Activate test with initial breakpoint */
9638c2ecf20Sopenharmony_ci	kgdb_breakpoint();
9648c2ecf20Sopenharmony_ci	kgdbts_break_test();
9658c2ecf20Sopenharmony_ci	kgdbts_break_test();
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cistatic void kgdbts_run_tests(void)
9698c2ecf20Sopenharmony_ci{
9708c2ecf20Sopenharmony_ci	char *ptr;
9718c2ecf20Sopenharmony_ci	int clone_test = 0;
9728c2ecf20Sopenharmony_ci	int do_sys_open_test = 0;
9738c2ecf20Sopenharmony_ci	int sstep_test = 1000;
9748c2ecf20Sopenharmony_ci	int nmi_sleep = 0;
9758c2ecf20Sopenharmony_ci	int i;
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	verbose = 0;
9788c2ecf20Sopenharmony_ci	if (strstr(config, "V1"))
9798c2ecf20Sopenharmony_ci		verbose = 1;
9808c2ecf20Sopenharmony_ci	if (strstr(config, "V2"))
9818c2ecf20Sopenharmony_ci		verbose = 2;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	ptr = strchr(config, 'F');
9848c2ecf20Sopenharmony_ci	if (ptr)
9858c2ecf20Sopenharmony_ci		clone_test = simple_strtol(ptr + 1, NULL, 10);
9868c2ecf20Sopenharmony_ci	ptr = strchr(config, 'S');
9878c2ecf20Sopenharmony_ci	if (ptr)
9888c2ecf20Sopenharmony_ci		do_sys_open_test = simple_strtol(ptr + 1, NULL, 10);
9898c2ecf20Sopenharmony_ci	ptr = strchr(config, 'N');
9908c2ecf20Sopenharmony_ci	if (ptr)
9918c2ecf20Sopenharmony_ci		nmi_sleep = simple_strtol(ptr+1, NULL, 10);
9928c2ecf20Sopenharmony_ci	ptr = strchr(config, 'I');
9938c2ecf20Sopenharmony_ci	if (ptr)
9948c2ecf20Sopenharmony_ci		sstep_test = simple_strtol(ptr+1, NULL, 10);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	/* All HW break point tests */
9978c2ecf20Sopenharmony_ci	if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) {
9988c2ecf20Sopenharmony_ci		hwbreaks_ok = 1;
9998c2ecf20Sopenharmony_ci		v1printk("kgdbts:RUN hw breakpoint test\n");
10008c2ecf20Sopenharmony_ci		run_breakpoint_test(1);
10018c2ecf20Sopenharmony_ci		v1printk("kgdbts:RUN hw write breakpoint test\n");
10028c2ecf20Sopenharmony_ci		run_hw_break_test(1);
10038c2ecf20Sopenharmony_ci		v1printk("kgdbts:RUN access write breakpoint test\n");
10048c2ecf20Sopenharmony_ci		run_hw_break_test(0);
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	/* required internal KGDB tests */
10088c2ecf20Sopenharmony_ci	v1printk("kgdbts:RUN plant and detach test\n");
10098c2ecf20Sopenharmony_ci	run_plant_and_detach_test(0);
10108c2ecf20Sopenharmony_ci	v1printk("kgdbts:RUN sw breakpoint test\n");
10118c2ecf20Sopenharmony_ci	run_breakpoint_test(0);
10128c2ecf20Sopenharmony_ci	v1printk("kgdbts:RUN bad memory access test\n");
10138c2ecf20Sopenharmony_ci	run_bad_read_test();
10148c2ecf20Sopenharmony_ci	v1printk("kgdbts:RUN singlestep test %i iterations\n", sstep_test);
10158c2ecf20Sopenharmony_ci	for (i = 0; i < sstep_test; i++) {
10168c2ecf20Sopenharmony_ci		run_singlestep_break_test();
10178c2ecf20Sopenharmony_ci		if (i % 100 == 0)
10188c2ecf20Sopenharmony_ci			v1printk("kgdbts:RUN singlestep [%i/%i]\n",
10198c2ecf20Sopenharmony_ci				 i, sstep_test);
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	/* ===Optional tests=== */
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	if (nmi_sleep) {
10258c2ecf20Sopenharmony_ci		v1printk("kgdbts:RUN NMI sleep %i seconds test\n", nmi_sleep);
10268c2ecf20Sopenharmony_ci		run_nmi_sleep_test(nmi_sleep);
10278c2ecf20Sopenharmony_ci	}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	/* If the kernel_clone test is run it will be the last test that is
10308c2ecf20Sopenharmony_ci	 * executed because a kernel thread will be spawned at the very
10318c2ecf20Sopenharmony_ci	 * end to unregister the debug hooks.
10328c2ecf20Sopenharmony_ci	 */
10338c2ecf20Sopenharmony_ci	if (clone_test) {
10348c2ecf20Sopenharmony_ci		repeat_test = clone_test;
10358c2ecf20Sopenharmony_ci		printk(KERN_INFO "kgdbts:RUN kernel_clone for %i breakpoints\n",
10368c2ecf20Sopenharmony_ci			repeat_test);
10378c2ecf20Sopenharmony_ci		kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
10388c2ecf20Sopenharmony_ci		run_kernel_clone_test();
10398c2ecf20Sopenharmony_ci		return;
10408c2ecf20Sopenharmony_ci	}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	/* If the sys_open test is run it will be the last test that is
10438c2ecf20Sopenharmony_ci	 * executed because a kernel thread will be spawned at the very
10448c2ecf20Sopenharmony_ci	 * end to unregister the debug hooks.
10458c2ecf20Sopenharmony_ci	 */
10468c2ecf20Sopenharmony_ci	if (do_sys_open_test) {
10478c2ecf20Sopenharmony_ci		repeat_test = do_sys_open_test;
10488c2ecf20Sopenharmony_ci		printk(KERN_INFO "kgdbts:RUN sys_open for %i breakpoints\n",
10498c2ecf20Sopenharmony_ci			repeat_test);
10508c2ecf20Sopenharmony_ci		kthread_run(kgdbts_unreg_thread, NULL, "kgdbts_unreg");
10518c2ecf20Sopenharmony_ci		run_sys_open_test();
10528c2ecf20Sopenharmony_ci		return;
10538c2ecf20Sopenharmony_ci	}
10548c2ecf20Sopenharmony_ci	/* Shutdown and unregister */
10558c2ecf20Sopenharmony_ci	kgdb_unregister_io_module(&kgdbts_io_ops);
10568c2ecf20Sopenharmony_ci	configured = 0;
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_cistatic int kgdbts_option_setup(char *opt)
10608c2ecf20Sopenharmony_ci{
10618c2ecf20Sopenharmony_ci	if (strlen(opt) >= MAX_CONFIG_LEN) {
10628c2ecf20Sopenharmony_ci		printk(KERN_ERR "kgdbts: config string too long\n");
10638c2ecf20Sopenharmony_ci		return 1;
10648c2ecf20Sopenharmony_ci	}
10658c2ecf20Sopenharmony_ci	strcpy(config, opt);
10668c2ecf20Sopenharmony_ci	return 1;
10678c2ecf20Sopenharmony_ci}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci__setup("kgdbts=", kgdbts_option_setup);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_cistatic int configure_kgdbts(void)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	int err = 0;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	if (!strlen(config) || isspace(config[0]))
10768c2ecf20Sopenharmony_ci		goto noconfig;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	final_ack = 0;
10798c2ecf20Sopenharmony_ci	run_plant_and_detach_test(1);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	err = kgdb_register_io_module(&kgdbts_io_ops);
10828c2ecf20Sopenharmony_ci	if (err) {
10838c2ecf20Sopenharmony_ci		configured = 0;
10848c2ecf20Sopenharmony_ci		return err;
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ci	configured = 1;
10878c2ecf20Sopenharmony_ci	kgdbts_run_tests();
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	return err;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_cinoconfig:
10928c2ecf20Sopenharmony_ci	config[0] = 0;
10938c2ecf20Sopenharmony_ci	configured = 0;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	return err;
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic int __init init_kgdbts(void)
10998c2ecf20Sopenharmony_ci{
11008c2ecf20Sopenharmony_ci	/* Already configured? */
11018c2ecf20Sopenharmony_ci	if (configured == 1)
11028c2ecf20Sopenharmony_ci		return 0;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	return configure_kgdbts();
11058c2ecf20Sopenharmony_ci}
11068c2ecf20Sopenharmony_cidevice_initcall(init_kgdbts);
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_cistatic int kgdbts_get_char(void)
11098c2ecf20Sopenharmony_ci{
11108c2ecf20Sopenharmony_ci	int val = 0;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	if (ts.run_test)
11138c2ecf20Sopenharmony_ci		val = ts.run_test(1, 0);
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	return val;
11168c2ecf20Sopenharmony_ci}
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_cistatic void kgdbts_put_char(u8 chr)
11198c2ecf20Sopenharmony_ci{
11208c2ecf20Sopenharmony_ci	if (ts.run_test)
11218c2ecf20Sopenharmony_ci		ts.run_test(0, chr);
11228c2ecf20Sopenharmony_ci}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistatic int param_set_kgdbts_var(const char *kmessage,
11258c2ecf20Sopenharmony_ci				const struct kernel_param *kp)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	size_t len = strlen(kmessage);
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (len >= MAX_CONFIG_LEN) {
11308c2ecf20Sopenharmony_ci		printk(KERN_ERR "kgdbts: config string too long\n");
11318c2ecf20Sopenharmony_ci		return -ENOSPC;
11328c2ecf20Sopenharmony_ci	}
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	/* Only copy in the string if the init function has not run yet */
11358c2ecf20Sopenharmony_ci	if (configured < 0) {
11368c2ecf20Sopenharmony_ci		strcpy(config, kmessage);
11378c2ecf20Sopenharmony_ci		return 0;
11388c2ecf20Sopenharmony_ci	}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	if (configured == 1) {
11418c2ecf20Sopenharmony_ci		printk(KERN_ERR "kgdbts: ERROR: Already configured and running.\n");
11428c2ecf20Sopenharmony_ci		return -EBUSY;
11438c2ecf20Sopenharmony_ci	}
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	strcpy(config, kmessage);
11468c2ecf20Sopenharmony_ci	/* Chop out \n char as a result of echo */
11478c2ecf20Sopenharmony_ci	if (len && config[len - 1] == '\n')
11488c2ecf20Sopenharmony_ci		config[len - 1] = '\0';
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	/* Go and configure with the new params. */
11518c2ecf20Sopenharmony_ci	return configure_kgdbts();
11528c2ecf20Sopenharmony_ci}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_cistatic void kgdbts_pre_exp_handler(void)
11558c2ecf20Sopenharmony_ci{
11568c2ecf20Sopenharmony_ci	/* Increment the module count when the debugger is active */
11578c2ecf20Sopenharmony_ci	if (!kgdb_connected)
11588c2ecf20Sopenharmony_ci		try_module_get(THIS_MODULE);
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_cistatic void kgdbts_post_exp_handler(void)
11628c2ecf20Sopenharmony_ci{
11638c2ecf20Sopenharmony_ci	/* decrement the module count when the debugger detaches */
11648c2ecf20Sopenharmony_ci	if (!kgdb_connected)
11658c2ecf20Sopenharmony_ci		module_put(THIS_MODULE);
11668c2ecf20Sopenharmony_ci}
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_cistatic struct kgdb_io kgdbts_io_ops = {
11698c2ecf20Sopenharmony_ci	.name			= "kgdbts",
11708c2ecf20Sopenharmony_ci	.read_char		= kgdbts_get_char,
11718c2ecf20Sopenharmony_ci	.write_char		= kgdbts_put_char,
11728c2ecf20Sopenharmony_ci	.pre_exception		= kgdbts_pre_exp_handler,
11738c2ecf20Sopenharmony_ci	.post_exception		= kgdbts_post_exp_handler,
11748c2ecf20Sopenharmony_ci};
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci/*
11778c2ecf20Sopenharmony_ci * not really modular, but the easiest way to keep compat with existing
11788c2ecf20Sopenharmony_ci * bootargs behaviour is to continue using module_param here.
11798c2ecf20Sopenharmony_ci */
11808c2ecf20Sopenharmony_cimodule_param_call(kgdbts, param_set_kgdbts_var, param_get_string, &kps, 0644);
11818c2ecf20Sopenharmony_ciMODULE_PARM_DESC(kgdbts, "<A|V1|V2>[F#|S#][N#]");
1182