xref: /openbmc/linux/kernel/debug/gdbstub.c (revision f5316b4aea024da9266d740322a5481657f6ce59)
153197fc4SJason Wessel /*
253197fc4SJason Wessel  * Kernel Debug Core
353197fc4SJason Wessel  *
453197fc4SJason Wessel  * Maintainer: Jason Wessel <jason.wessel@windriver.com>
553197fc4SJason Wessel  *
653197fc4SJason Wessel  * Copyright (C) 2000-2001 VERITAS Software Corporation.
753197fc4SJason Wessel  * Copyright (C) 2002-2004 Timesys Corporation
853197fc4SJason Wessel  * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
953197fc4SJason Wessel  * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
1053197fc4SJason Wessel  * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
1153197fc4SJason Wessel  * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
1253197fc4SJason Wessel  * Copyright (C) 2005-2009 Wind River Systems, Inc.
1353197fc4SJason Wessel  * Copyright (C) 2007 MontaVista Software, Inc.
1453197fc4SJason Wessel  * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
1553197fc4SJason Wessel  *
1653197fc4SJason Wessel  * Contributors at various stages not listed above:
1753197fc4SJason Wessel  *  Jason Wessel ( jason.wessel@windriver.com )
1853197fc4SJason Wessel  *  George Anzinger <george@mvista.com>
1953197fc4SJason Wessel  *  Anurekh Saxena (anurekh.saxena@timesys.com)
2053197fc4SJason Wessel  *  Lake Stevens Instrument Division (Glenn Engel)
2153197fc4SJason Wessel  *  Jim Kingdon, Cygnus Support.
2253197fc4SJason Wessel  *
2353197fc4SJason Wessel  * Original KGDB stub: David Grothe <dave@gcom.com>,
2453197fc4SJason Wessel  * Tigran Aivazian <tigran@sco.com>
2553197fc4SJason Wessel  *
2653197fc4SJason Wessel  * This file is licensed under the terms of the GNU General Public License
2753197fc4SJason Wessel  * version 2. This program is licensed "as is" without any warranty of any
2853197fc4SJason Wessel  * kind, whether express or implied.
2953197fc4SJason Wessel  */
3053197fc4SJason Wessel 
3153197fc4SJason Wessel #include <linux/kernel.h>
3253197fc4SJason Wessel #include <linux/kgdb.h>
33*f5316b4aSJason Wessel #include <linux/kdb.h>
3453197fc4SJason Wessel #include <linux/reboot.h>
3553197fc4SJason Wessel #include <linux/uaccess.h>
3653197fc4SJason Wessel #include <asm/cacheflush.h>
3753197fc4SJason Wessel #include <asm/unaligned.h>
3853197fc4SJason Wessel #include "debug_core.h"
3953197fc4SJason Wessel 
4053197fc4SJason Wessel #define KGDB_MAX_THREAD_QUERY 17
4153197fc4SJason Wessel 
4253197fc4SJason Wessel /* Our I/O buffers. */
4353197fc4SJason Wessel static char			remcom_in_buffer[BUFMAX];
4453197fc4SJason Wessel static char			remcom_out_buffer[BUFMAX];
4553197fc4SJason Wessel 
4653197fc4SJason Wessel /* Storage for the registers, in GDB format. */
4753197fc4SJason Wessel static unsigned long		gdb_regs[(NUMREGBYTES +
4853197fc4SJason Wessel 					sizeof(unsigned long) - 1) /
4953197fc4SJason Wessel 					sizeof(unsigned long)];
5053197fc4SJason Wessel 
5153197fc4SJason Wessel /*
5253197fc4SJason Wessel  * GDB remote protocol parser:
5353197fc4SJason Wessel  */
5453197fc4SJason Wessel 
5553197fc4SJason Wessel static int hex(char ch)
5653197fc4SJason Wessel {
5753197fc4SJason Wessel 	if ((ch >= 'a') && (ch <= 'f'))
5853197fc4SJason Wessel 		return ch - 'a' + 10;
5953197fc4SJason Wessel 	if ((ch >= '0') && (ch <= '9'))
6053197fc4SJason Wessel 		return ch - '0';
6153197fc4SJason Wessel 	if ((ch >= 'A') && (ch <= 'F'))
6253197fc4SJason Wessel 		return ch - 'A' + 10;
6353197fc4SJason Wessel 	return -1;
6453197fc4SJason Wessel }
6553197fc4SJason Wessel 
66*f5316b4aSJason Wessel #ifdef CONFIG_KGDB_KDB
67*f5316b4aSJason Wessel static int gdbstub_read_wait(void)
68*f5316b4aSJason Wessel {
69*f5316b4aSJason Wessel 	int ret = -1;
70*f5316b4aSJason Wessel 	int i;
71*f5316b4aSJason Wessel 
72*f5316b4aSJason Wessel 	/* poll any additional I/O interfaces that are defined */
73*f5316b4aSJason Wessel 	while (ret < 0)
74*f5316b4aSJason Wessel 		for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
75*f5316b4aSJason Wessel 			ret = kdb_poll_funcs[i]();
76*f5316b4aSJason Wessel 			if (ret > 0)
77*f5316b4aSJason Wessel 				break;
78*f5316b4aSJason Wessel 		}
79*f5316b4aSJason Wessel 	return ret;
80*f5316b4aSJason Wessel }
81*f5316b4aSJason Wessel #else
82*f5316b4aSJason Wessel static int gdbstub_read_wait(void)
83*f5316b4aSJason Wessel {
84*f5316b4aSJason Wessel 	int ret = dbg_io_ops->read_char();
85*f5316b4aSJason Wessel 	while (ret == NO_POLL_CHAR)
86*f5316b4aSJason Wessel 		ret = dbg_io_ops->read_char();
87*f5316b4aSJason Wessel 	return ret;
88*f5316b4aSJason Wessel }
89*f5316b4aSJason Wessel #endif
9053197fc4SJason Wessel /* scan for the sequence $<data>#<checksum> */
9153197fc4SJason Wessel static void get_packet(char *buffer)
9253197fc4SJason Wessel {
9353197fc4SJason Wessel 	unsigned char checksum;
9453197fc4SJason Wessel 	unsigned char xmitcsum;
9553197fc4SJason Wessel 	int count;
9653197fc4SJason Wessel 	char ch;
9753197fc4SJason Wessel 
9853197fc4SJason Wessel 	do {
9953197fc4SJason Wessel 		/*
10053197fc4SJason Wessel 		 * Spin and wait around for the start character, ignore all
10153197fc4SJason Wessel 		 * other characters:
10253197fc4SJason Wessel 		 */
103*f5316b4aSJason Wessel 		while ((ch = (gdbstub_read_wait())) != '$')
10453197fc4SJason Wessel 			/* nothing */;
10553197fc4SJason Wessel 
10653197fc4SJason Wessel 		kgdb_connected = 1;
10753197fc4SJason Wessel 		checksum = 0;
10853197fc4SJason Wessel 		xmitcsum = -1;
10953197fc4SJason Wessel 
11053197fc4SJason Wessel 		count = 0;
11153197fc4SJason Wessel 
11253197fc4SJason Wessel 		/*
11353197fc4SJason Wessel 		 * now, read until a # or end of buffer is found:
11453197fc4SJason Wessel 		 */
11553197fc4SJason Wessel 		while (count < (BUFMAX - 1)) {
116*f5316b4aSJason Wessel 			ch = gdbstub_read_wait();
11753197fc4SJason Wessel 			if (ch == '#')
11853197fc4SJason Wessel 				break;
11953197fc4SJason Wessel 			checksum = checksum + ch;
12053197fc4SJason Wessel 			buffer[count] = ch;
12153197fc4SJason Wessel 			count = count + 1;
12253197fc4SJason Wessel 		}
12353197fc4SJason Wessel 		buffer[count] = 0;
12453197fc4SJason Wessel 
12553197fc4SJason Wessel 		if (ch == '#') {
126*f5316b4aSJason Wessel 			xmitcsum = hex(gdbstub_read_wait()) << 4;
127*f5316b4aSJason Wessel 			xmitcsum += hex(gdbstub_read_wait());
12853197fc4SJason Wessel 
12953197fc4SJason Wessel 			if (checksum != xmitcsum)
13053197fc4SJason Wessel 				/* failed checksum */
13153197fc4SJason Wessel 				dbg_io_ops->write_char('-');
13253197fc4SJason Wessel 			else
13353197fc4SJason Wessel 				/* successful transfer */
13453197fc4SJason Wessel 				dbg_io_ops->write_char('+');
13553197fc4SJason Wessel 			if (dbg_io_ops->flush)
13653197fc4SJason Wessel 				dbg_io_ops->flush();
13753197fc4SJason Wessel 		}
13853197fc4SJason Wessel 	} while (checksum != xmitcsum);
13953197fc4SJason Wessel }
14053197fc4SJason Wessel 
14153197fc4SJason Wessel /*
14253197fc4SJason Wessel  * Send the packet in buffer.
14353197fc4SJason Wessel  * Check for gdb connection if asked for.
14453197fc4SJason Wessel  */
14553197fc4SJason Wessel static void put_packet(char *buffer)
14653197fc4SJason Wessel {
14753197fc4SJason Wessel 	unsigned char checksum;
14853197fc4SJason Wessel 	int count;
14953197fc4SJason Wessel 	char ch;
15053197fc4SJason Wessel 
15153197fc4SJason Wessel 	/*
15253197fc4SJason Wessel 	 * $<packet info>#<checksum>.
15353197fc4SJason Wessel 	 */
15453197fc4SJason Wessel 	while (1) {
15553197fc4SJason Wessel 		dbg_io_ops->write_char('$');
15653197fc4SJason Wessel 		checksum = 0;
15753197fc4SJason Wessel 		count = 0;
15853197fc4SJason Wessel 
15953197fc4SJason Wessel 		while ((ch = buffer[count])) {
16053197fc4SJason Wessel 			dbg_io_ops->write_char(ch);
16153197fc4SJason Wessel 			checksum += ch;
16253197fc4SJason Wessel 			count++;
16353197fc4SJason Wessel 		}
16453197fc4SJason Wessel 
16553197fc4SJason Wessel 		dbg_io_ops->write_char('#');
16653197fc4SJason Wessel 		dbg_io_ops->write_char(hex_asc_hi(checksum));
16753197fc4SJason Wessel 		dbg_io_ops->write_char(hex_asc_lo(checksum));
16853197fc4SJason Wessel 		if (dbg_io_ops->flush)
16953197fc4SJason Wessel 			dbg_io_ops->flush();
17053197fc4SJason Wessel 
17153197fc4SJason Wessel 		/* Now see what we get in reply. */
172*f5316b4aSJason Wessel 		ch = gdbstub_read_wait();
17353197fc4SJason Wessel 
17453197fc4SJason Wessel 		if (ch == 3)
175*f5316b4aSJason Wessel 			ch = gdbstub_read_wait();
17653197fc4SJason Wessel 
17753197fc4SJason Wessel 		/* If we get an ACK, we are done. */
17853197fc4SJason Wessel 		if (ch == '+')
17953197fc4SJason Wessel 			return;
18053197fc4SJason Wessel 
18153197fc4SJason Wessel 		/*
18253197fc4SJason Wessel 		 * If we get the start of another packet, this means
18353197fc4SJason Wessel 		 * that GDB is attempting to reconnect.  We will NAK
18453197fc4SJason Wessel 		 * the packet being sent, and stop trying to send this
18553197fc4SJason Wessel 		 * packet.
18653197fc4SJason Wessel 		 */
18753197fc4SJason Wessel 		if (ch == '$') {
18853197fc4SJason Wessel 			dbg_io_ops->write_char('-');
18953197fc4SJason Wessel 			if (dbg_io_ops->flush)
19053197fc4SJason Wessel 				dbg_io_ops->flush();
19153197fc4SJason Wessel 			return;
19253197fc4SJason Wessel 		}
19353197fc4SJason Wessel 	}
19453197fc4SJason Wessel }
19553197fc4SJason Wessel 
19653197fc4SJason Wessel static char gdbmsgbuf[BUFMAX + 1];
19753197fc4SJason Wessel 
19853197fc4SJason Wessel void gdbstub_msg_write(const char *s, int len)
19953197fc4SJason Wessel {
20053197fc4SJason Wessel 	char *bufptr;
20153197fc4SJason Wessel 	int wcount;
20253197fc4SJason Wessel 	int i;
20353197fc4SJason Wessel 
20453197fc4SJason Wessel 	/* 'O'utput */
20553197fc4SJason Wessel 	gdbmsgbuf[0] = 'O';
20653197fc4SJason Wessel 
20753197fc4SJason Wessel 	/* Fill and send buffers... */
20853197fc4SJason Wessel 	while (len > 0) {
20953197fc4SJason Wessel 		bufptr = gdbmsgbuf + 1;
21053197fc4SJason Wessel 
21153197fc4SJason Wessel 		/* Calculate how many this time */
21253197fc4SJason Wessel 		if ((len << 1) > (BUFMAX - 2))
21353197fc4SJason Wessel 			wcount = (BUFMAX - 2) >> 1;
21453197fc4SJason Wessel 		else
21553197fc4SJason Wessel 			wcount = len;
21653197fc4SJason Wessel 
21753197fc4SJason Wessel 		/* Pack in hex chars */
21853197fc4SJason Wessel 		for (i = 0; i < wcount; i++)
21953197fc4SJason Wessel 			bufptr = pack_hex_byte(bufptr, s[i]);
22053197fc4SJason Wessel 		*bufptr = '\0';
22153197fc4SJason Wessel 
22253197fc4SJason Wessel 		/* Move up */
22353197fc4SJason Wessel 		s += wcount;
22453197fc4SJason Wessel 		len -= wcount;
22553197fc4SJason Wessel 
22653197fc4SJason Wessel 		/* Write packet */
22753197fc4SJason Wessel 		put_packet(gdbmsgbuf);
22853197fc4SJason Wessel 	}
22953197fc4SJason Wessel }
23053197fc4SJason Wessel 
23153197fc4SJason Wessel /*
23253197fc4SJason Wessel  * Convert the memory pointed to by mem into hex, placing result in
23353197fc4SJason Wessel  * buf.  Return a pointer to the last char put in buf (null). May
23453197fc4SJason Wessel  * return an error.
23553197fc4SJason Wessel  */
23653197fc4SJason Wessel int kgdb_mem2hex(char *mem, char *buf, int count)
23753197fc4SJason Wessel {
23853197fc4SJason Wessel 	char *tmp;
23953197fc4SJason Wessel 	int err;
24053197fc4SJason Wessel 
24153197fc4SJason Wessel 	/*
24253197fc4SJason Wessel 	 * We use the upper half of buf as an intermediate buffer for the
24353197fc4SJason Wessel 	 * raw memory copy.  Hex conversion will work against this one.
24453197fc4SJason Wessel 	 */
24553197fc4SJason Wessel 	tmp = buf + count;
24653197fc4SJason Wessel 
24753197fc4SJason Wessel 	err = probe_kernel_read(tmp, mem, count);
24853197fc4SJason Wessel 	if (!err) {
24953197fc4SJason Wessel 		while (count > 0) {
25053197fc4SJason Wessel 			buf = pack_hex_byte(buf, *tmp);
25153197fc4SJason Wessel 			tmp++;
25253197fc4SJason Wessel 			count--;
25353197fc4SJason Wessel 		}
25453197fc4SJason Wessel 
25553197fc4SJason Wessel 		*buf = 0;
25653197fc4SJason Wessel 	}
25753197fc4SJason Wessel 
25853197fc4SJason Wessel 	return err;
25953197fc4SJason Wessel }
26053197fc4SJason Wessel 
26153197fc4SJason Wessel /*
26253197fc4SJason Wessel  * Convert the hex array pointed to by buf into binary to be placed in
26353197fc4SJason Wessel  * mem.  Return a pointer to the character AFTER the last byte
26453197fc4SJason Wessel  * written.  May return an error.
26553197fc4SJason Wessel  */
26653197fc4SJason Wessel int kgdb_hex2mem(char *buf, char *mem, int count)
26753197fc4SJason Wessel {
26853197fc4SJason Wessel 	char *tmp_raw;
26953197fc4SJason Wessel 	char *tmp_hex;
27053197fc4SJason Wessel 
27153197fc4SJason Wessel 	/*
27253197fc4SJason Wessel 	 * We use the upper half of buf as an intermediate buffer for the
27353197fc4SJason Wessel 	 * raw memory that is converted from hex.
27453197fc4SJason Wessel 	 */
27553197fc4SJason Wessel 	tmp_raw = buf + count * 2;
27653197fc4SJason Wessel 
27753197fc4SJason Wessel 	tmp_hex = tmp_raw - 1;
27853197fc4SJason Wessel 	while (tmp_hex >= buf) {
27953197fc4SJason Wessel 		tmp_raw--;
28053197fc4SJason Wessel 		*tmp_raw = hex(*tmp_hex--);
28153197fc4SJason Wessel 		*tmp_raw |= hex(*tmp_hex--) << 4;
28253197fc4SJason Wessel 	}
28353197fc4SJason Wessel 
28453197fc4SJason Wessel 	return probe_kernel_write(mem, tmp_raw, count);
28553197fc4SJason Wessel }
28653197fc4SJason Wessel 
28753197fc4SJason Wessel /*
28853197fc4SJason Wessel  * While we find nice hex chars, build a long_val.
28953197fc4SJason Wessel  * Return number of chars processed.
29053197fc4SJason Wessel  */
29153197fc4SJason Wessel int kgdb_hex2long(char **ptr, unsigned long *long_val)
29253197fc4SJason Wessel {
29353197fc4SJason Wessel 	int hex_val;
29453197fc4SJason Wessel 	int num = 0;
29553197fc4SJason Wessel 	int negate = 0;
29653197fc4SJason Wessel 
29753197fc4SJason Wessel 	*long_val = 0;
29853197fc4SJason Wessel 
29953197fc4SJason Wessel 	if (**ptr == '-') {
30053197fc4SJason Wessel 		negate = 1;
30153197fc4SJason Wessel 		(*ptr)++;
30253197fc4SJason Wessel 	}
30353197fc4SJason Wessel 	while (**ptr) {
30453197fc4SJason Wessel 		hex_val = hex(**ptr);
30553197fc4SJason Wessel 		if (hex_val < 0)
30653197fc4SJason Wessel 			break;
30753197fc4SJason Wessel 
30853197fc4SJason Wessel 		*long_val = (*long_val << 4) | hex_val;
30953197fc4SJason Wessel 		num++;
31053197fc4SJason Wessel 		(*ptr)++;
31153197fc4SJason Wessel 	}
31253197fc4SJason Wessel 
31353197fc4SJason Wessel 	if (negate)
31453197fc4SJason Wessel 		*long_val = -*long_val;
31553197fc4SJason Wessel 
31653197fc4SJason Wessel 	return num;
31753197fc4SJason Wessel }
31853197fc4SJason Wessel 
31953197fc4SJason Wessel /*
32053197fc4SJason Wessel  * Copy the binary array pointed to by buf into mem.  Fix $, #, and
32153197fc4SJason Wessel  * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
32253197fc4SJason Wessel  * The input buf is overwitten with the result to write to mem.
32353197fc4SJason Wessel  */
32453197fc4SJason Wessel static int kgdb_ebin2mem(char *buf, char *mem, int count)
32553197fc4SJason Wessel {
32653197fc4SJason Wessel 	int size = 0;
32753197fc4SJason Wessel 	char *c = buf;
32853197fc4SJason Wessel 
32953197fc4SJason Wessel 	while (count-- > 0) {
33053197fc4SJason Wessel 		c[size] = *buf++;
33153197fc4SJason Wessel 		if (c[size] == 0x7d)
33253197fc4SJason Wessel 			c[size] = *buf++ ^ 0x20;
33353197fc4SJason Wessel 		size++;
33453197fc4SJason Wessel 	}
33553197fc4SJason Wessel 
33653197fc4SJason Wessel 	return probe_kernel_write(mem, c, size);
33753197fc4SJason Wessel }
33853197fc4SJason Wessel 
33953197fc4SJason Wessel /* Write memory due to an 'M' or 'X' packet. */
34053197fc4SJason Wessel static int write_mem_msg(int binary)
34153197fc4SJason Wessel {
34253197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
34353197fc4SJason Wessel 	unsigned long addr;
34453197fc4SJason Wessel 	unsigned long length;
34553197fc4SJason Wessel 	int err;
34653197fc4SJason Wessel 
34753197fc4SJason Wessel 	if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
34853197fc4SJason Wessel 	    kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
34953197fc4SJason Wessel 		if (binary)
35053197fc4SJason Wessel 			err = kgdb_ebin2mem(ptr, (char *)addr, length);
35153197fc4SJason Wessel 		else
35253197fc4SJason Wessel 			err = kgdb_hex2mem(ptr, (char *)addr, length);
35353197fc4SJason Wessel 		if (err)
35453197fc4SJason Wessel 			return err;
35553197fc4SJason Wessel 		if (CACHE_FLUSH_IS_SAFE)
35653197fc4SJason Wessel 			flush_icache_range(addr, addr + length);
35753197fc4SJason Wessel 		return 0;
35853197fc4SJason Wessel 	}
35953197fc4SJason Wessel 
36053197fc4SJason Wessel 	return -EINVAL;
36153197fc4SJason Wessel }
36253197fc4SJason Wessel 
36353197fc4SJason Wessel static void error_packet(char *pkt, int error)
36453197fc4SJason Wessel {
36553197fc4SJason Wessel 	error = -error;
36653197fc4SJason Wessel 	pkt[0] = 'E';
36753197fc4SJason Wessel 	pkt[1] = hex_asc[(error / 10)];
36853197fc4SJason Wessel 	pkt[2] = hex_asc[(error % 10)];
36953197fc4SJason Wessel 	pkt[3] = '\0';
37053197fc4SJason Wessel }
37153197fc4SJason Wessel 
37253197fc4SJason Wessel /*
37353197fc4SJason Wessel  * Thread ID accessors. We represent a flat TID space to GDB, where
37453197fc4SJason Wessel  * the per CPU idle threads (which under Linux all have PID 0) are
37553197fc4SJason Wessel  * remapped to negative TIDs.
37653197fc4SJason Wessel  */
37753197fc4SJason Wessel 
37853197fc4SJason Wessel #define BUF_THREAD_ID_SIZE	16
37953197fc4SJason Wessel 
38053197fc4SJason Wessel static char *pack_threadid(char *pkt, unsigned char *id)
38153197fc4SJason Wessel {
38253197fc4SJason Wessel 	char *limit;
38353197fc4SJason Wessel 
38453197fc4SJason Wessel 	limit = pkt + BUF_THREAD_ID_SIZE;
38553197fc4SJason Wessel 	while (pkt < limit)
38653197fc4SJason Wessel 		pkt = pack_hex_byte(pkt, *id++);
38753197fc4SJason Wessel 
38853197fc4SJason Wessel 	return pkt;
38953197fc4SJason Wessel }
39053197fc4SJason Wessel 
39153197fc4SJason Wessel static void int_to_threadref(unsigned char *id, int value)
39253197fc4SJason Wessel {
39353197fc4SJason Wessel 	unsigned char *scan;
39453197fc4SJason Wessel 	int i = 4;
39553197fc4SJason Wessel 
39653197fc4SJason Wessel 	scan = (unsigned char *)id;
39753197fc4SJason Wessel 	while (i--)
39853197fc4SJason Wessel 		*scan++ = 0;
39953197fc4SJason Wessel 	put_unaligned_be32(value, scan);
40053197fc4SJason Wessel }
40153197fc4SJason Wessel 
40253197fc4SJason Wessel static struct task_struct *getthread(struct pt_regs *regs, int tid)
40353197fc4SJason Wessel {
40453197fc4SJason Wessel 	/*
40553197fc4SJason Wessel 	 * Non-positive TIDs are remapped to the cpu shadow information
40653197fc4SJason Wessel 	 */
40753197fc4SJason Wessel 	if (tid == 0 || tid == -1)
40853197fc4SJason Wessel 		tid = -atomic_read(&kgdb_active) - 2;
40953197fc4SJason Wessel 	if (tid < -1 && tid > -NR_CPUS - 2) {
41053197fc4SJason Wessel 		if (kgdb_info[-tid - 2].task)
41153197fc4SJason Wessel 			return kgdb_info[-tid - 2].task;
41253197fc4SJason Wessel 		else
41353197fc4SJason Wessel 			return idle_task(-tid - 2);
41453197fc4SJason Wessel 	}
41553197fc4SJason Wessel 	if (tid <= 0) {
41653197fc4SJason Wessel 		printk(KERN_ERR "KGDB: Internal thread select error\n");
41753197fc4SJason Wessel 		dump_stack();
41853197fc4SJason Wessel 		return NULL;
41953197fc4SJason Wessel 	}
42053197fc4SJason Wessel 
42153197fc4SJason Wessel 	/*
42253197fc4SJason Wessel 	 * find_task_by_pid_ns() does not take the tasklist lock anymore
42353197fc4SJason Wessel 	 * but is nicely RCU locked - hence is a pretty resilient
42453197fc4SJason Wessel 	 * thing to use:
42553197fc4SJason Wessel 	 */
42653197fc4SJason Wessel 	return find_task_by_pid_ns(tid, &init_pid_ns);
42753197fc4SJason Wessel }
42853197fc4SJason Wessel 
42953197fc4SJason Wessel 
43053197fc4SJason Wessel /*
43153197fc4SJason Wessel  * Remap normal tasks to their real PID,
43253197fc4SJason Wessel  * CPU shadow threads are mapped to -CPU - 2
43353197fc4SJason Wessel  */
43453197fc4SJason Wessel static inline int shadow_pid(int realpid)
43553197fc4SJason Wessel {
43653197fc4SJason Wessel 	if (realpid)
43753197fc4SJason Wessel 		return realpid;
43853197fc4SJason Wessel 
43953197fc4SJason Wessel 	return -raw_smp_processor_id() - 2;
44053197fc4SJason Wessel }
44153197fc4SJason Wessel 
44253197fc4SJason Wessel /*
44353197fc4SJason Wessel  * All the functions that start with gdb_cmd are the various
44453197fc4SJason Wessel  * operations to implement the handlers for the gdbserial protocol
44553197fc4SJason Wessel  * where KGDB is communicating with an external debugger
44653197fc4SJason Wessel  */
44753197fc4SJason Wessel 
44853197fc4SJason Wessel /* Handle the '?' status packets */
44953197fc4SJason Wessel static void gdb_cmd_status(struct kgdb_state *ks)
45053197fc4SJason Wessel {
45153197fc4SJason Wessel 	/*
45253197fc4SJason Wessel 	 * We know that this packet is only sent
45353197fc4SJason Wessel 	 * during initial connect.  So to be safe,
45453197fc4SJason Wessel 	 * we clear out our breakpoints now in case
45553197fc4SJason Wessel 	 * GDB is reconnecting.
45653197fc4SJason Wessel 	 */
45753197fc4SJason Wessel 	dbg_remove_all_break();
45853197fc4SJason Wessel 
45953197fc4SJason Wessel 	remcom_out_buffer[0] = 'S';
46053197fc4SJason Wessel 	pack_hex_byte(&remcom_out_buffer[1], ks->signo);
46153197fc4SJason Wessel }
46253197fc4SJason Wessel 
46353197fc4SJason Wessel /* Handle the 'g' get registers request */
46453197fc4SJason Wessel static void gdb_cmd_getregs(struct kgdb_state *ks)
46553197fc4SJason Wessel {
46653197fc4SJason Wessel 	struct task_struct *thread;
46753197fc4SJason Wessel 	void *local_debuggerinfo;
46853197fc4SJason Wessel 	int i;
46953197fc4SJason Wessel 
47053197fc4SJason Wessel 	thread = kgdb_usethread;
47153197fc4SJason Wessel 	if (!thread) {
47253197fc4SJason Wessel 		thread = kgdb_info[ks->cpu].task;
47353197fc4SJason Wessel 		local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
47453197fc4SJason Wessel 	} else {
47553197fc4SJason Wessel 		local_debuggerinfo = NULL;
47653197fc4SJason Wessel 		for_each_online_cpu(i) {
47753197fc4SJason Wessel 			/*
47853197fc4SJason Wessel 			 * Try to find the task on some other
47953197fc4SJason Wessel 			 * or possibly this node if we do not
48053197fc4SJason Wessel 			 * find the matching task then we try
48153197fc4SJason Wessel 			 * to approximate the results.
48253197fc4SJason Wessel 			 */
48353197fc4SJason Wessel 			if (thread == kgdb_info[i].task)
48453197fc4SJason Wessel 				local_debuggerinfo = kgdb_info[i].debuggerinfo;
48553197fc4SJason Wessel 		}
48653197fc4SJason Wessel 	}
48753197fc4SJason Wessel 
48853197fc4SJason Wessel 	/*
48953197fc4SJason Wessel 	 * All threads that don't have debuggerinfo should be
49053197fc4SJason Wessel 	 * in schedule() sleeping, since all other CPUs
49153197fc4SJason Wessel 	 * are in kgdb_wait, and thus have debuggerinfo.
49253197fc4SJason Wessel 	 */
49353197fc4SJason Wessel 	if (local_debuggerinfo) {
49453197fc4SJason Wessel 		pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
49553197fc4SJason Wessel 	} else {
49653197fc4SJason Wessel 		/*
49753197fc4SJason Wessel 		 * Pull stuff saved during switch_to; nothing
49853197fc4SJason Wessel 		 * else is accessible (or even particularly
49953197fc4SJason Wessel 		 * relevant).
50053197fc4SJason Wessel 		 *
50153197fc4SJason Wessel 		 * This should be enough for a stack trace.
50253197fc4SJason Wessel 		 */
50353197fc4SJason Wessel 		sleeping_thread_to_gdb_regs(gdb_regs, thread);
50453197fc4SJason Wessel 	}
50553197fc4SJason Wessel 	kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
50653197fc4SJason Wessel }
50753197fc4SJason Wessel 
50853197fc4SJason Wessel /* Handle the 'G' set registers request */
50953197fc4SJason Wessel static void gdb_cmd_setregs(struct kgdb_state *ks)
51053197fc4SJason Wessel {
51153197fc4SJason Wessel 	kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
51253197fc4SJason Wessel 
51353197fc4SJason Wessel 	if (kgdb_usethread && kgdb_usethread != current) {
51453197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
51553197fc4SJason Wessel 	} else {
51653197fc4SJason Wessel 		gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
51753197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
51853197fc4SJason Wessel 	}
51953197fc4SJason Wessel }
52053197fc4SJason Wessel 
52153197fc4SJason Wessel /* Handle the 'm' memory read bytes */
52253197fc4SJason Wessel static void gdb_cmd_memread(struct kgdb_state *ks)
52353197fc4SJason Wessel {
52453197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
52553197fc4SJason Wessel 	unsigned long length;
52653197fc4SJason Wessel 	unsigned long addr;
52753197fc4SJason Wessel 	int err;
52853197fc4SJason Wessel 
52953197fc4SJason Wessel 	if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
53053197fc4SJason Wessel 					kgdb_hex2long(&ptr, &length) > 0) {
53153197fc4SJason Wessel 		err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
53253197fc4SJason Wessel 		if (err)
53353197fc4SJason Wessel 			error_packet(remcom_out_buffer, err);
53453197fc4SJason Wessel 	} else {
53553197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
53653197fc4SJason Wessel 	}
53753197fc4SJason Wessel }
53853197fc4SJason Wessel 
53953197fc4SJason Wessel /* Handle the 'M' memory write bytes */
54053197fc4SJason Wessel static void gdb_cmd_memwrite(struct kgdb_state *ks)
54153197fc4SJason Wessel {
54253197fc4SJason Wessel 	int err = write_mem_msg(0);
54353197fc4SJason Wessel 
54453197fc4SJason Wessel 	if (err)
54553197fc4SJason Wessel 		error_packet(remcom_out_buffer, err);
54653197fc4SJason Wessel 	else
54753197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
54853197fc4SJason Wessel }
54953197fc4SJason Wessel 
55053197fc4SJason Wessel /* Handle the 'X' memory binary write bytes */
55153197fc4SJason Wessel static void gdb_cmd_binwrite(struct kgdb_state *ks)
55253197fc4SJason Wessel {
55353197fc4SJason Wessel 	int err = write_mem_msg(1);
55453197fc4SJason Wessel 
55553197fc4SJason Wessel 	if (err)
55653197fc4SJason Wessel 		error_packet(remcom_out_buffer, err);
55753197fc4SJason Wessel 	else
55853197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
55953197fc4SJason Wessel }
56053197fc4SJason Wessel 
56153197fc4SJason Wessel /* Handle the 'D' or 'k', detach or kill packets */
56253197fc4SJason Wessel static void gdb_cmd_detachkill(struct kgdb_state *ks)
56353197fc4SJason Wessel {
56453197fc4SJason Wessel 	int error;
56553197fc4SJason Wessel 
56653197fc4SJason Wessel 	/* The detach case */
56753197fc4SJason Wessel 	if (remcom_in_buffer[0] == 'D') {
56853197fc4SJason Wessel 		error = dbg_remove_all_break();
56953197fc4SJason Wessel 		if (error < 0) {
57053197fc4SJason Wessel 			error_packet(remcom_out_buffer, error);
57153197fc4SJason Wessel 		} else {
57253197fc4SJason Wessel 			strcpy(remcom_out_buffer, "OK");
57353197fc4SJason Wessel 			kgdb_connected = 0;
57453197fc4SJason Wessel 		}
57553197fc4SJason Wessel 		put_packet(remcom_out_buffer);
57653197fc4SJason Wessel 	} else {
57753197fc4SJason Wessel 		/*
57853197fc4SJason Wessel 		 * Assume the kill case, with no exit code checking,
57953197fc4SJason Wessel 		 * trying to force detach the debugger:
58053197fc4SJason Wessel 		 */
58153197fc4SJason Wessel 		dbg_remove_all_break();
58253197fc4SJason Wessel 		kgdb_connected = 0;
58353197fc4SJason Wessel 	}
58453197fc4SJason Wessel }
58553197fc4SJason Wessel 
58653197fc4SJason Wessel /* Handle the 'R' reboot packets */
58753197fc4SJason Wessel static int gdb_cmd_reboot(struct kgdb_state *ks)
58853197fc4SJason Wessel {
58953197fc4SJason Wessel 	/* For now, only honor R0 */
59053197fc4SJason Wessel 	if (strcmp(remcom_in_buffer, "R0") == 0) {
59153197fc4SJason Wessel 		printk(KERN_CRIT "Executing emergency reboot\n");
59253197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
59353197fc4SJason Wessel 		put_packet(remcom_out_buffer);
59453197fc4SJason Wessel 
59553197fc4SJason Wessel 		/*
59653197fc4SJason Wessel 		 * Execution should not return from
59753197fc4SJason Wessel 		 * machine_emergency_restart()
59853197fc4SJason Wessel 		 */
59953197fc4SJason Wessel 		machine_emergency_restart();
60053197fc4SJason Wessel 		kgdb_connected = 0;
60153197fc4SJason Wessel 
60253197fc4SJason Wessel 		return 1;
60353197fc4SJason Wessel 	}
60453197fc4SJason Wessel 	return 0;
60553197fc4SJason Wessel }
60653197fc4SJason Wessel 
60753197fc4SJason Wessel /* Handle the 'q' query packets */
60853197fc4SJason Wessel static void gdb_cmd_query(struct kgdb_state *ks)
60953197fc4SJason Wessel {
61053197fc4SJason Wessel 	struct task_struct *g;
61153197fc4SJason Wessel 	struct task_struct *p;
61253197fc4SJason Wessel 	unsigned char thref[8];
61353197fc4SJason Wessel 	char *ptr;
61453197fc4SJason Wessel 	int i;
61553197fc4SJason Wessel 	int cpu;
61653197fc4SJason Wessel 	int finished = 0;
61753197fc4SJason Wessel 
61853197fc4SJason Wessel 	switch (remcom_in_buffer[1]) {
61953197fc4SJason Wessel 	case 's':
62053197fc4SJason Wessel 	case 'f':
62153197fc4SJason Wessel 		if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
62253197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
62353197fc4SJason Wessel 			break;
62453197fc4SJason Wessel 		}
62553197fc4SJason Wessel 
62653197fc4SJason Wessel 		i = 0;
62753197fc4SJason Wessel 		remcom_out_buffer[0] = 'm';
62853197fc4SJason Wessel 		ptr = remcom_out_buffer + 1;
62953197fc4SJason Wessel 		if (remcom_in_buffer[1] == 'f') {
63053197fc4SJason Wessel 			/* Each cpu is a shadow thread */
63153197fc4SJason Wessel 			for_each_online_cpu(cpu) {
63253197fc4SJason Wessel 				ks->thr_query = 0;
63353197fc4SJason Wessel 				int_to_threadref(thref, -cpu - 2);
63453197fc4SJason Wessel 				pack_threadid(ptr, thref);
63553197fc4SJason Wessel 				ptr += BUF_THREAD_ID_SIZE;
63653197fc4SJason Wessel 				*(ptr++) = ',';
63753197fc4SJason Wessel 				i++;
63853197fc4SJason Wessel 			}
63953197fc4SJason Wessel 		}
64053197fc4SJason Wessel 
64153197fc4SJason Wessel 		do_each_thread(g, p) {
64253197fc4SJason Wessel 			if (i >= ks->thr_query && !finished) {
64353197fc4SJason Wessel 				int_to_threadref(thref, p->pid);
64453197fc4SJason Wessel 				pack_threadid(ptr, thref);
64553197fc4SJason Wessel 				ptr += BUF_THREAD_ID_SIZE;
64653197fc4SJason Wessel 				*(ptr++) = ',';
64753197fc4SJason Wessel 				ks->thr_query++;
64853197fc4SJason Wessel 				if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
64953197fc4SJason Wessel 					finished = 1;
65053197fc4SJason Wessel 			}
65153197fc4SJason Wessel 			i++;
65253197fc4SJason Wessel 		} while_each_thread(g, p);
65353197fc4SJason Wessel 
65453197fc4SJason Wessel 		*(--ptr) = '\0';
65553197fc4SJason Wessel 		break;
65653197fc4SJason Wessel 
65753197fc4SJason Wessel 	case 'C':
65853197fc4SJason Wessel 		/* Current thread id */
65953197fc4SJason Wessel 		strcpy(remcom_out_buffer, "QC");
66053197fc4SJason Wessel 		ks->threadid = shadow_pid(current->pid);
66153197fc4SJason Wessel 		int_to_threadref(thref, ks->threadid);
66253197fc4SJason Wessel 		pack_threadid(remcom_out_buffer + 2, thref);
66353197fc4SJason Wessel 		break;
66453197fc4SJason Wessel 	case 'T':
66553197fc4SJason Wessel 		if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
66653197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
66753197fc4SJason Wessel 			break;
66853197fc4SJason Wessel 		}
66953197fc4SJason Wessel 		ks->threadid = 0;
67053197fc4SJason Wessel 		ptr = remcom_in_buffer + 17;
67153197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
67253197fc4SJason Wessel 		if (!getthread(ks->linux_regs, ks->threadid)) {
67353197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
67453197fc4SJason Wessel 			break;
67553197fc4SJason Wessel 		}
67653197fc4SJason Wessel 		if ((int)ks->threadid > 0) {
67753197fc4SJason Wessel 			kgdb_mem2hex(getthread(ks->linux_regs,
67853197fc4SJason Wessel 					ks->threadid)->comm,
67953197fc4SJason Wessel 					remcom_out_buffer, 16);
68053197fc4SJason Wessel 		} else {
68153197fc4SJason Wessel 			static char tmpstr[23 + BUF_THREAD_ID_SIZE];
68253197fc4SJason Wessel 
68353197fc4SJason Wessel 			sprintf(tmpstr, "shadowCPU%d",
68453197fc4SJason Wessel 					(int)(-ks->threadid - 2));
68553197fc4SJason Wessel 			kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
68653197fc4SJason Wessel 		}
68753197fc4SJason Wessel 		break;
68853197fc4SJason Wessel 	}
68953197fc4SJason Wessel }
69053197fc4SJason Wessel 
69153197fc4SJason Wessel /* Handle the 'H' task query packets */
69253197fc4SJason Wessel static void gdb_cmd_task(struct kgdb_state *ks)
69353197fc4SJason Wessel {
69453197fc4SJason Wessel 	struct task_struct *thread;
69553197fc4SJason Wessel 	char *ptr;
69653197fc4SJason Wessel 
69753197fc4SJason Wessel 	switch (remcom_in_buffer[1]) {
69853197fc4SJason Wessel 	case 'g':
69953197fc4SJason Wessel 		ptr = &remcom_in_buffer[2];
70053197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
70153197fc4SJason Wessel 		thread = getthread(ks->linux_regs, ks->threadid);
70253197fc4SJason Wessel 		if (!thread && ks->threadid > 0) {
70353197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
70453197fc4SJason Wessel 			break;
70553197fc4SJason Wessel 		}
70653197fc4SJason Wessel 		kgdb_usethread = thread;
70753197fc4SJason Wessel 		ks->kgdb_usethreadid = ks->threadid;
70853197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
70953197fc4SJason Wessel 		break;
71053197fc4SJason Wessel 	case 'c':
71153197fc4SJason Wessel 		ptr = &remcom_in_buffer[2];
71253197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
71353197fc4SJason Wessel 		if (!ks->threadid) {
71453197fc4SJason Wessel 			kgdb_contthread = NULL;
71553197fc4SJason Wessel 		} else {
71653197fc4SJason Wessel 			thread = getthread(ks->linux_regs, ks->threadid);
71753197fc4SJason Wessel 			if (!thread && ks->threadid > 0) {
71853197fc4SJason Wessel 				error_packet(remcom_out_buffer, -EINVAL);
71953197fc4SJason Wessel 				break;
72053197fc4SJason Wessel 			}
72153197fc4SJason Wessel 			kgdb_contthread = thread;
72253197fc4SJason Wessel 		}
72353197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
72453197fc4SJason Wessel 		break;
72553197fc4SJason Wessel 	}
72653197fc4SJason Wessel }
72753197fc4SJason Wessel 
72853197fc4SJason Wessel /* Handle the 'T' thread query packets */
72953197fc4SJason Wessel static void gdb_cmd_thread(struct kgdb_state *ks)
73053197fc4SJason Wessel {
73153197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
73253197fc4SJason Wessel 	struct task_struct *thread;
73353197fc4SJason Wessel 
73453197fc4SJason Wessel 	kgdb_hex2long(&ptr, &ks->threadid);
73553197fc4SJason Wessel 	thread = getthread(ks->linux_regs, ks->threadid);
73653197fc4SJason Wessel 	if (thread)
73753197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
73853197fc4SJason Wessel 	else
73953197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
74053197fc4SJason Wessel }
74153197fc4SJason Wessel 
74253197fc4SJason Wessel /* Handle the 'z' or 'Z' breakpoint remove or set packets */
74353197fc4SJason Wessel static void gdb_cmd_break(struct kgdb_state *ks)
74453197fc4SJason Wessel {
74553197fc4SJason Wessel 	/*
74653197fc4SJason Wessel 	 * Since GDB-5.3, it's been drafted that '0' is a software
74753197fc4SJason Wessel 	 * breakpoint, '1' is a hardware breakpoint, so let's do that.
74853197fc4SJason Wessel 	 */
74953197fc4SJason Wessel 	char *bpt_type = &remcom_in_buffer[1];
75053197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[2];
75153197fc4SJason Wessel 	unsigned long addr;
75253197fc4SJason Wessel 	unsigned long length;
75353197fc4SJason Wessel 	int error = 0;
75453197fc4SJason Wessel 
75553197fc4SJason Wessel 	if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
75653197fc4SJason Wessel 		/* Unsupported */
75753197fc4SJason Wessel 		if (*bpt_type > '4')
75853197fc4SJason Wessel 			return;
75953197fc4SJason Wessel 	} else {
76053197fc4SJason Wessel 		if (*bpt_type != '0' && *bpt_type != '1')
76153197fc4SJason Wessel 			/* Unsupported. */
76253197fc4SJason Wessel 			return;
76353197fc4SJason Wessel 	}
76453197fc4SJason Wessel 
76553197fc4SJason Wessel 	/*
76653197fc4SJason Wessel 	 * Test if this is a hardware breakpoint, and
76753197fc4SJason Wessel 	 * if we support it:
76853197fc4SJason Wessel 	 */
76953197fc4SJason Wessel 	if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
77053197fc4SJason Wessel 		/* Unsupported. */
77153197fc4SJason Wessel 		return;
77253197fc4SJason Wessel 
77353197fc4SJason Wessel 	if (*(ptr++) != ',') {
77453197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
77553197fc4SJason Wessel 		return;
77653197fc4SJason Wessel 	}
77753197fc4SJason Wessel 	if (!kgdb_hex2long(&ptr, &addr)) {
77853197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
77953197fc4SJason Wessel 		return;
78053197fc4SJason Wessel 	}
78153197fc4SJason Wessel 	if (*(ptr++) != ',' ||
78253197fc4SJason Wessel 		!kgdb_hex2long(&ptr, &length)) {
78353197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
78453197fc4SJason Wessel 		return;
78553197fc4SJason Wessel 	}
78653197fc4SJason Wessel 
78753197fc4SJason Wessel 	if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
78853197fc4SJason Wessel 		error = dbg_set_sw_break(addr);
78953197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
79053197fc4SJason Wessel 		error = dbg_remove_sw_break(addr);
79153197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'Z')
79253197fc4SJason Wessel 		error = arch_kgdb_ops.set_hw_breakpoint(addr,
79353197fc4SJason Wessel 			(int)length, *bpt_type - '0');
79453197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'z')
79553197fc4SJason Wessel 		error = arch_kgdb_ops.remove_hw_breakpoint(addr,
79653197fc4SJason Wessel 			(int) length, *bpt_type - '0');
79753197fc4SJason Wessel 
79853197fc4SJason Wessel 	if (error == 0)
79953197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
80053197fc4SJason Wessel 	else
80153197fc4SJason Wessel 		error_packet(remcom_out_buffer, error);
80253197fc4SJason Wessel }
80353197fc4SJason Wessel 
80453197fc4SJason Wessel /* Handle the 'C' signal / exception passing packets */
80553197fc4SJason Wessel static int gdb_cmd_exception_pass(struct kgdb_state *ks)
80653197fc4SJason Wessel {
80753197fc4SJason Wessel 	/* C09 == pass exception
80853197fc4SJason Wessel 	 * C15 == detach kgdb, pass exception
80953197fc4SJason Wessel 	 */
81053197fc4SJason Wessel 	if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
81153197fc4SJason Wessel 
81253197fc4SJason Wessel 		ks->pass_exception = 1;
81353197fc4SJason Wessel 		remcom_in_buffer[0] = 'c';
81453197fc4SJason Wessel 
81553197fc4SJason Wessel 	} else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
81653197fc4SJason Wessel 
81753197fc4SJason Wessel 		ks->pass_exception = 1;
81853197fc4SJason Wessel 		remcom_in_buffer[0] = 'D';
81953197fc4SJason Wessel 		dbg_remove_all_break();
82053197fc4SJason Wessel 		kgdb_connected = 0;
82153197fc4SJason Wessel 		return 1;
82253197fc4SJason Wessel 
82353197fc4SJason Wessel 	} else {
82453197fc4SJason Wessel 		gdbstub_msg_write("KGDB only knows signal 9 (pass)"
82553197fc4SJason Wessel 			" and 15 (pass and disconnect)\n"
82653197fc4SJason Wessel 			"Executing a continue without signal passing\n", 0);
82753197fc4SJason Wessel 		remcom_in_buffer[0] = 'c';
82853197fc4SJason Wessel 	}
82953197fc4SJason Wessel 
83053197fc4SJason Wessel 	/* Indicate fall through */
83153197fc4SJason Wessel 	return -1;
83253197fc4SJason Wessel }
83353197fc4SJason Wessel 
83453197fc4SJason Wessel /*
83553197fc4SJason Wessel  * This function performs all gdbserial command procesing
83653197fc4SJason Wessel  */
83753197fc4SJason Wessel int gdb_serial_stub(struct kgdb_state *ks)
83853197fc4SJason Wessel {
83953197fc4SJason Wessel 	int error = 0;
84053197fc4SJason Wessel 	int tmp;
84153197fc4SJason Wessel 
84253197fc4SJason Wessel 	/* Clear the out buffer. */
84353197fc4SJason Wessel 	memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
84453197fc4SJason Wessel 
84553197fc4SJason Wessel 	if (kgdb_connected) {
84653197fc4SJason Wessel 		unsigned char thref[8];
84753197fc4SJason Wessel 		char *ptr;
84853197fc4SJason Wessel 
84953197fc4SJason Wessel 		/* Reply to host that an exception has occurred */
85053197fc4SJason Wessel 		ptr = remcom_out_buffer;
85153197fc4SJason Wessel 		*ptr++ = 'T';
85253197fc4SJason Wessel 		ptr = pack_hex_byte(ptr, ks->signo);
85353197fc4SJason Wessel 		ptr += strlen(strcpy(ptr, "thread:"));
85453197fc4SJason Wessel 		int_to_threadref(thref, shadow_pid(current->pid));
85553197fc4SJason Wessel 		ptr = pack_threadid(ptr, thref);
85653197fc4SJason Wessel 		*ptr++ = ';';
85753197fc4SJason Wessel 		put_packet(remcom_out_buffer);
85853197fc4SJason Wessel 	}
85953197fc4SJason Wessel 
86053197fc4SJason Wessel 	kgdb_usethread = kgdb_info[ks->cpu].task;
86153197fc4SJason Wessel 	ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
86253197fc4SJason Wessel 	ks->pass_exception = 0;
86353197fc4SJason Wessel 
86453197fc4SJason Wessel 	while (1) {
86553197fc4SJason Wessel 		error = 0;
86653197fc4SJason Wessel 
86753197fc4SJason Wessel 		/* Clear the out buffer. */
86853197fc4SJason Wessel 		memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
86953197fc4SJason Wessel 
87053197fc4SJason Wessel 		get_packet(remcom_in_buffer);
87153197fc4SJason Wessel 
87253197fc4SJason Wessel 		switch (remcom_in_buffer[0]) {
87353197fc4SJason Wessel 		case '?': /* gdbserial status */
87453197fc4SJason Wessel 			gdb_cmd_status(ks);
87553197fc4SJason Wessel 			break;
87653197fc4SJason Wessel 		case 'g': /* return the value of the CPU registers */
87753197fc4SJason Wessel 			gdb_cmd_getregs(ks);
87853197fc4SJason Wessel 			break;
87953197fc4SJason Wessel 		case 'G': /* set the value of the CPU registers - return OK */
88053197fc4SJason Wessel 			gdb_cmd_setregs(ks);
88153197fc4SJason Wessel 			break;
88253197fc4SJason Wessel 		case 'm': /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
88353197fc4SJason Wessel 			gdb_cmd_memread(ks);
88453197fc4SJason Wessel 			break;
88553197fc4SJason Wessel 		case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
88653197fc4SJason Wessel 			gdb_cmd_memwrite(ks);
88753197fc4SJason Wessel 			break;
88853197fc4SJason Wessel 		case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
88953197fc4SJason Wessel 			gdb_cmd_binwrite(ks);
89053197fc4SJason Wessel 			break;
89153197fc4SJason Wessel 			/* kill or detach. KGDB should treat this like a
89253197fc4SJason Wessel 			 * continue.
89353197fc4SJason Wessel 			 */
89453197fc4SJason Wessel 		case 'D': /* Debugger detach */
89553197fc4SJason Wessel 		case 'k': /* Debugger detach via kill */
89653197fc4SJason Wessel 			gdb_cmd_detachkill(ks);
89753197fc4SJason Wessel 			goto default_handle;
89853197fc4SJason Wessel 		case 'R': /* Reboot */
89953197fc4SJason Wessel 			if (gdb_cmd_reboot(ks))
90053197fc4SJason Wessel 				goto default_handle;
90153197fc4SJason Wessel 			break;
90253197fc4SJason Wessel 		case 'q': /* query command */
90353197fc4SJason Wessel 			gdb_cmd_query(ks);
90453197fc4SJason Wessel 			break;
90553197fc4SJason Wessel 		case 'H': /* task related */
90653197fc4SJason Wessel 			gdb_cmd_task(ks);
90753197fc4SJason Wessel 			break;
90853197fc4SJason Wessel 		case 'T': /* Query thread status */
90953197fc4SJason Wessel 			gdb_cmd_thread(ks);
91053197fc4SJason Wessel 			break;
91153197fc4SJason Wessel 		case 'z': /* Break point remove */
91253197fc4SJason Wessel 		case 'Z': /* Break point set */
91353197fc4SJason Wessel 			gdb_cmd_break(ks);
91453197fc4SJason Wessel 			break;
915dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
916dcc78711SJason Wessel 		case '3': /* Escape into back into kdb */
917dcc78711SJason Wessel 			if (remcom_in_buffer[1] == '\0') {
918dcc78711SJason Wessel 				gdb_cmd_detachkill(ks);
919dcc78711SJason Wessel 				return DBG_PASS_EVENT;
920dcc78711SJason Wessel 			}
921dcc78711SJason Wessel #endif
92253197fc4SJason Wessel 		case 'C': /* Exception passing */
92353197fc4SJason Wessel 			tmp = gdb_cmd_exception_pass(ks);
92453197fc4SJason Wessel 			if (tmp > 0)
92553197fc4SJason Wessel 				goto default_handle;
92653197fc4SJason Wessel 			if (tmp == 0)
92753197fc4SJason Wessel 				break;
92853197fc4SJason Wessel 			/* Fall through on tmp < 0 */
92953197fc4SJason Wessel 		case 'c': /* Continue packet */
93053197fc4SJason Wessel 		case 's': /* Single step packet */
93153197fc4SJason Wessel 			if (kgdb_contthread && kgdb_contthread != current) {
93253197fc4SJason Wessel 				/* Can't switch threads in kgdb */
93353197fc4SJason Wessel 				error_packet(remcom_out_buffer, -EINVAL);
93453197fc4SJason Wessel 				break;
93553197fc4SJason Wessel 			}
93653197fc4SJason Wessel 			dbg_activate_sw_breakpoints();
93753197fc4SJason Wessel 			/* Fall through to default processing */
93853197fc4SJason Wessel 		default:
93953197fc4SJason Wessel default_handle:
94053197fc4SJason Wessel 			error = kgdb_arch_handle_exception(ks->ex_vector,
94153197fc4SJason Wessel 						ks->signo,
94253197fc4SJason Wessel 						ks->err_code,
94353197fc4SJason Wessel 						remcom_in_buffer,
94453197fc4SJason Wessel 						remcom_out_buffer,
94553197fc4SJason Wessel 						ks->linux_regs);
94653197fc4SJason Wessel 			/*
94753197fc4SJason Wessel 			 * Leave cmd processing on error, detach,
94853197fc4SJason Wessel 			 * kill, continue, or single step.
94953197fc4SJason Wessel 			 */
95053197fc4SJason Wessel 			if (error >= 0 || remcom_in_buffer[0] == 'D' ||
95153197fc4SJason Wessel 			    remcom_in_buffer[0] == 'k') {
95253197fc4SJason Wessel 				error = 0;
95353197fc4SJason Wessel 				goto kgdb_exit;
95453197fc4SJason Wessel 			}
95553197fc4SJason Wessel 
95653197fc4SJason Wessel 		}
95753197fc4SJason Wessel 
95853197fc4SJason Wessel 		/* reply to the request */
95953197fc4SJason Wessel 		put_packet(remcom_out_buffer);
96053197fc4SJason Wessel 	}
96153197fc4SJason Wessel 
96253197fc4SJason Wessel kgdb_exit:
96353197fc4SJason Wessel 	if (ks->pass_exception)
96453197fc4SJason Wessel 		error = 1;
96553197fc4SJason Wessel 	return error;
96653197fc4SJason Wessel }
967dcc78711SJason Wessel 
968dcc78711SJason Wessel int gdbstub_state(struct kgdb_state *ks, char *cmd)
969dcc78711SJason Wessel {
970dcc78711SJason Wessel 	int error;
971dcc78711SJason Wessel 
972dcc78711SJason Wessel 	switch (cmd[0]) {
973dcc78711SJason Wessel 	case 'e':
974dcc78711SJason Wessel 		error = kgdb_arch_handle_exception(ks->ex_vector,
975dcc78711SJason Wessel 						   ks->signo,
976dcc78711SJason Wessel 						   ks->err_code,
977dcc78711SJason Wessel 						   remcom_in_buffer,
978dcc78711SJason Wessel 						   remcom_out_buffer,
979dcc78711SJason Wessel 						   ks->linux_regs);
980dcc78711SJason Wessel 		return error;
981dcc78711SJason Wessel 	case 's':
982dcc78711SJason Wessel 	case 'c':
983dcc78711SJason Wessel 		strcpy(remcom_in_buffer, cmd);
984dcc78711SJason Wessel 		return 0;
985dcc78711SJason Wessel 	case '?':
986dcc78711SJason Wessel 		gdb_cmd_status(ks);
987dcc78711SJason Wessel 		break;
988dcc78711SJason Wessel 	case '\0':
989dcc78711SJason Wessel 		strcpy(remcom_out_buffer, "");
990dcc78711SJason Wessel 		break;
991dcc78711SJason Wessel 	}
992dcc78711SJason Wessel 	dbg_io_ops->write_char('+');
993dcc78711SJason Wessel 	put_packet(remcom_out_buffer);
994dcc78711SJason Wessel 	return 0;
995dcc78711SJason Wessel }
996