xref: /openbmc/linux/kernel/debug/gdbstub.c (revision 9fbe465efc76044dd87afe764db5464ae61aeabc)
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>
9a2531293SPavel Machek  * Copyright (C) 2004 Pavel Machek <pavel@ucw.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>
33f5316b4aSJason 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];
45f679c498SJason Wessel static int			gdbstub_use_prev_in_buf;
46f679c498SJason Wessel static int			gdbstub_prev_in_buf_pos;
4753197fc4SJason Wessel 
4853197fc4SJason Wessel /* Storage for the registers, in GDB format. */
4953197fc4SJason Wessel static unsigned long		gdb_regs[(NUMREGBYTES +
5053197fc4SJason Wessel 					sizeof(unsigned long) - 1) /
5153197fc4SJason Wessel 					sizeof(unsigned long)];
5253197fc4SJason Wessel 
5353197fc4SJason Wessel /*
5453197fc4SJason Wessel  * GDB remote protocol parser:
5553197fc4SJason Wessel  */
5653197fc4SJason Wessel 
57f5316b4aSJason Wessel #ifdef CONFIG_KGDB_KDB
58f5316b4aSJason Wessel static int gdbstub_read_wait(void)
59f5316b4aSJason Wessel {
60f5316b4aSJason Wessel 	int ret = -1;
61f5316b4aSJason Wessel 	int i;
62f5316b4aSJason Wessel 
63f679c498SJason Wessel 	if (unlikely(gdbstub_use_prev_in_buf)) {
64f679c498SJason Wessel 		if (gdbstub_prev_in_buf_pos < gdbstub_use_prev_in_buf)
65f679c498SJason Wessel 			return remcom_in_buffer[gdbstub_prev_in_buf_pos++];
66f679c498SJason Wessel 		else
67f679c498SJason Wessel 			gdbstub_use_prev_in_buf = 0;
68f679c498SJason Wessel 	}
69f679c498SJason Wessel 
70f5316b4aSJason Wessel 	/* poll any additional I/O interfaces that are defined */
71f5316b4aSJason Wessel 	while (ret < 0)
72f5316b4aSJason Wessel 		for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
73f5316b4aSJason Wessel 			ret = kdb_poll_funcs[i]();
74f5316b4aSJason Wessel 			if (ret > 0)
75f5316b4aSJason Wessel 				break;
76f5316b4aSJason Wessel 		}
77f5316b4aSJason Wessel 	return ret;
78f5316b4aSJason Wessel }
79f5316b4aSJason Wessel #else
80f5316b4aSJason Wessel static int gdbstub_read_wait(void)
81f5316b4aSJason Wessel {
82f5316b4aSJason Wessel 	int ret = dbg_io_ops->read_char();
83f5316b4aSJason Wessel 	while (ret == NO_POLL_CHAR)
84f5316b4aSJason Wessel 		ret = dbg_io_ops->read_char();
85f5316b4aSJason Wessel 	return ret;
86f5316b4aSJason Wessel }
87f5316b4aSJason Wessel #endif
8853197fc4SJason Wessel /* scan for the sequence $<data>#<checksum> */
8953197fc4SJason Wessel static void get_packet(char *buffer)
9053197fc4SJason Wessel {
9153197fc4SJason Wessel 	unsigned char checksum;
9253197fc4SJason Wessel 	unsigned char xmitcsum;
9353197fc4SJason Wessel 	int count;
9453197fc4SJason Wessel 	char ch;
9553197fc4SJason Wessel 
9653197fc4SJason Wessel 	do {
9753197fc4SJason Wessel 		/*
9853197fc4SJason Wessel 		 * Spin and wait around for the start character, ignore all
9953197fc4SJason Wessel 		 * other characters:
10053197fc4SJason Wessel 		 */
101f5316b4aSJason Wessel 		while ((ch = (gdbstub_read_wait())) != '$')
10253197fc4SJason Wessel 			/* nothing */;
10353197fc4SJason Wessel 
10453197fc4SJason Wessel 		kgdb_connected = 1;
10553197fc4SJason Wessel 		checksum = 0;
10653197fc4SJason Wessel 		xmitcsum = -1;
10753197fc4SJason Wessel 
10853197fc4SJason Wessel 		count = 0;
10953197fc4SJason Wessel 
11053197fc4SJason Wessel 		/*
11153197fc4SJason Wessel 		 * now, read until a # or end of buffer is found:
11253197fc4SJason Wessel 		 */
11353197fc4SJason Wessel 		while (count < (BUFMAX - 1)) {
114f5316b4aSJason Wessel 			ch = gdbstub_read_wait();
11553197fc4SJason Wessel 			if (ch == '#')
11653197fc4SJason Wessel 				break;
11753197fc4SJason Wessel 			checksum = checksum + ch;
11853197fc4SJason Wessel 			buffer[count] = ch;
11953197fc4SJason Wessel 			count = count + 1;
12053197fc4SJason Wessel 		}
12153197fc4SJason Wessel 
12253197fc4SJason Wessel 		if (ch == '#') {
123a9fa20a7SAndy Shevchenko 			xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
124a9fa20a7SAndy Shevchenko 			xmitcsum += hex_to_bin(gdbstub_read_wait());
12553197fc4SJason Wessel 
12653197fc4SJason Wessel 			if (checksum != xmitcsum)
12753197fc4SJason Wessel 				/* failed checksum */
12853197fc4SJason Wessel 				dbg_io_ops->write_char('-');
12953197fc4SJason Wessel 			else
13053197fc4SJason Wessel 				/* successful transfer */
13153197fc4SJason Wessel 				dbg_io_ops->write_char('+');
13253197fc4SJason Wessel 			if (dbg_io_ops->flush)
13353197fc4SJason Wessel 				dbg_io_ops->flush();
13453197fc4SJason Wessel 		}
135f679c498SJason Wessel 		buffer[count] = 0;
13653197fc4SJason Wessel 	} while (checksum != xmitcsum);
13753197fc4SJason Wessel }
13853197fc4SJason Wessel 
13953197fc4SJason Wessel /*
14053197fc4SJason Wessel  * Send the packet in buffer.
14153197fc4SJason Wessel  * Check for gdb connection if asked for.
14253197fc4SJason Wessel  */
14353197fc4SJason Wessel static void put_packet(char *buffer)
14453197fc4SJason Wessel {
14553197fc4SJason Wessel 	unsigned char checksum;
14653197fc4SJason Wessel 	int count;
14753197fc4SJason Wessel 	char ch;
14853197fc4SJason Wessel 
14953197fc4SJason Wessel 	/*
15053197fc4SJason Wessel 	 * $<packet info>#<checksum>.
15153197fc4SJason Wessel 	 */
15253197fc4SJason Wessel 	while (1) {
15353197fc4SJason Wessel 		dbg_io_ops->write_char('$');
15453197fc4SJason Wessel 		checksum = 0;
15553197fc4SJason Wessel 		count = 0;
15653197fc4SJason Wessel 
15753197fc4SJason Wessel 		while ((ch = buffer[count])) {
15853197fc4SJason Wessel 			dbg_io_ops->write_char(ch);
15953197fc4SJason Wessel 			checksum += ch;
16053197fc4SJason Wessel 			count++;
16153197fc4SJason Wessel 		}
16253197fc4SJason Wessel 
16353197fc4SJason Wessel 		dbg_io_ops->write_char('#');
16453197fc4SJason Wessel 		dbg_io_ops->write_char(hex_asc_hi(checksum));
16553197fc4SJason Wessel 		dbg_io_ops->write_char(hex_asc_lo(checksum));
16653197fc4SJason Wessel 		if (dbg_io_ops->flush)
16753197fc4SJason Wessel 			dbg_io_ops->flush();
16853197fc4SJason Wessel 
16953197fc4SJason Wessel 		/* Now see what we get in reply. */
170f5316b4aSJason Wessel 		ch = gdbstub_read_wait();
17153197fc4SJason Wessel 
17253197fc4SJason Wessel 		if (ch == 3)
173f5316b4aSJason Wessel 			ch = gdbstub_read_wait();
17453197fc4SJason Wessel 
17553197fc4SJason Wessel 		/* If we get an ACK, we are done. */
17653197fc4SJason Wessel 		if (ch == '+')
17753197fc4SJason Wessel 			return;
17853197fc4SJason Wessel 
17953197fc4SJason Wessel 		/*
18053197fc4SJason Wessel 		 * If we get the start of another packet, this means
18153197fc4SJason Wessel 		 * that GDB is attempting to reconnect.  We will NAK
18253197fc4SJason Wessel 		 * the packet being sent, and stop trying to send this
18353197fc4SJason Wessel 		 * packet.
18453197fc4SJason Wessel 		 */
18553197fc4SJason Wessel 		if (ch == '$') {
18653197fc4SJason Wessel 			dbg_io_ops->write_char('-');
18753197fc4SJason Wessel 			if (dbg_io_ops->flush)
18853197fc4SJason Wessel 				dbg_io_ops->flush();
18953197fc4SJason Wessel 			return;
19053197fc4SJason Wessel 		}
19153197fc4SJason Wessel 	}
19253197fc4SJason Wessel }
19353197fc4SJason Wessel 
19453197fc4SJason Wessel static char gdbmsgbuf[BUFMAX + 1];
19553197fc4SJason Wessel 
19653197fc4SJason Wessel void gdbstub_msg_write(const char *s, int len)
19753197fc4SJason Wessel {
19853197fc4SJason Wessel 	char *bufptr;
19953197fc4SJason Wessel 	int wcount;
20053197fc4SJason Wessel 	int i;
20153197fc4SJason Wessel 
202a0de055cSJason Wessel 	if (len == 0)
203a0de055cSJason Wessel 		len = strlen(s);
204a0de055cSJason Wessel 
20553197fc4SJason Wessel 	/* 'O'utput */
20653197fc4SJason Wessel 	gdbmsgbuf[0] = 'O';
20753197fc4SJason Wessel 
20853197fc4SJason Wessel 	/* Fill and send buffers... */
20953197fc4SJason Wessel 	while (len > 0) {
21053197fc4SJason Wessel 		bufptr = gdbmsgbuf + 1;
21153197fc4SJason Wessel 
21253197fc4SJason Wessel 		/* Calculate how many this time */
21353197fc4SJason Wessel 		if ((len << 1) > (BUFMAX - 2))
21453197fc4SJason Wessel 			wcount = (BUFMAX - 2) >> 1;
21553197fc4SJason Wessel 		else
21653197fc4SJason Wessel 			wcount = len;
21753197fc4SJason Wessel 
21853197fc4SJason Wessel 		/* Pack in hex chars */
21953197fc4SJason Wessel 		for (i = 0; i < wcount; i++)
22050e1499fSAndy Shevchenko 			bufptr = hex_byte_pack(bufptr, s[i]);
22153197fc4SJason Wessel 		*bufptr = '\0';
22253197fc4SJason Wessel 
22353197fc4SJason Wessel 		/* Move up */
22453197fc4SJason Wessel 		s += wcount;
22553197fc4SJason Wessel 		len -= wcount;
22653197fc4SJason Wessel 
22753197fc4SJason Wessel 		/* Write packet */
22853197fc4SJason Wessel 		put_packet(gdbmsgbuf);
22953197fc4SJason Wessel 	}
23053197fc4SJason Wessel }
23153197fc4SJason Wessel 
23253197fc4SJason Wessel /*
23353197fc4SJason Wessel  * Convert the memory pointed to by mem into hex, placing result in
23453197fc4SJason Wessel  * buf.  Return a pointer to the last char put in buf (null). May
23553197fc4SJason Wessel  * return an error.
23653197fc4SJason Wessel  */
23755751145SJason Wessel char *kgdb_mem2hex(char *mem, char *buf, int count)
23853197fc4SJason Wessel {
23953197fc4SJason Wessel 	char *tmp;
24053197fc4SJason Wessel 	int err;
24153197fc4SJason Wessel 
24253197fc4SJason Wessel 	/*
24353197fc4SJason Wessel 	 * We use the upper half of buf as an intermediate buffer for the
24453197fc4SJason Wessel 	 * raw memory copy.  Hex conversion will work against this one.
24553197fc4SJason Wessel 	 */
24653197fc4SJason Wessel 	tmp = buf + count;
24753197fc4SJason Wessel 
24853197fc4SJason Wessel 	err = probe_kernel_read(tmp, mem, count);
24955751145SJason Wessel 	if (err)
25055751145SJason Wessel 		return NULL;
25153197fc4SJason Wessel 	while (count > 0) {
25250e1499fSAndy Shevchenko 		buf = hex_byte_pack(buf, *tmp);
25353197fc4SJason Wessel 		tmp++;
25453197fc4SJason Wessel 		count--;
25553197fc4SJason Wessel 	}
25653197fc4SJason Wessel 	*buf = 0;
25753197fc4SJason Wessel 
25855751145SJason Wessel 	return buf;
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--;
280a9fa20a7SAndy Shevchenko 		*tmp_raw = hex_to_bin(*tmp_hex--);
281a9fa20a7SAndy Shevchenko 		*tmp_raw |= hex_to_bin(*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) {
304a9fa20a7SAndy Shevchenko 		hex_val = hex_to_bin(**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 
339534af108SJason Wessel #if DBG_MAX_REG_NUM > 0
340534af108SJason Wessel void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
341534af108SJason Wessel {
342534af108SJason Wessel 	int i;
343534af108SJason Wessel 	int idx = 0;
344534af108SJason Wessel 	char *ptr = (char *)gdb_regs;
345534af108SJason Wessel 
346534af108SJason Wessel 	for (i = 0; i < DBG_MAX_REG_NUM; i++) {
347534af108SJason Wessel 		dbg_get_reg(i, ptr + idx, regs);
348534af108SJason Wessel 		idx += dbg_reg_def[i].size;
349534af108SJason Wessel 	}
350534af108SJason Wessel }
351534af108SJason Wessel 
352534af108SJason Wessel void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
353534af108SJason Wessel {
354534af108SJason Wessel 	int i;
355534af108SJason Wessel 	int idx = 0;
356534af108SJason Wessel 	char *ptr = (char *)gdb_regs;
357534af108SJason Wessel 
358534af108SJason Wessel 	for (i = 0; i < DBG_MAX_REG_NUM; i++) {
359534af108SJason Wessel 		dbg_set_reg(i, ptr + idx, regs);
360534af108SJason Wessel 		idx += dbg_reg_def[i].size;
361534af108SJason Wessel 	}
362534af108SJason Wessel }
363534af108SJason Wessel #endif /* DBG_MAX_REG_NUM > 0 */
364534af108SJason Wessel 
36553197fc4SJason Wessel /* Write memory due to an 'M' or 'X' packet. */
36653197fc4SJason Wessel static int write_mem_msg(int binary)
36753197fc4SJason Wessel {
36853197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
36953197fc4SJason Wessel 	unsigned long addr;
37053197fc4SJason Wessel 	unsigned long length;
37153197fc4SJason Wessel 	int err;
37253197fc4SJason Wessel 
37353197fc4SJason Wessel 	if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
37453197fc4SJason Wessel 	    kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
37553197fc4SJason Wessel 		if (binary)
37653197fc4SJason Wessel 			err = kgdb_ebin2mem(ptr, (char *)addr, length);
37753197fc4SJason Wessel 		else
37853197fc4SJason Wessel 			err = kgdb_hex2mem(ptr, (char *)addr, length);
37953197fc4SJason Wessel 		if (err)
38053197fc4SJason Wessel 			return err;
38153197fc4SJason Wessel 		if (CACHE_FLUSH_IS_SAFE)
38253197fc4SJason Wessel 			flush_icache_range(addr, addr + length);
38353197fc4SJason Wessel 		return 0;
38453197fc4SJason Wessel 	}
38553197fc4SJason Wessel 
38653197fc4SJason Wessel 	return -EINVAL;
38753197fc4SJason Wessel }
38853197fc4SJason Wessel 
38953197fc4SJason Wessel static void error_packet(char *pkt, int error)
39053197fc4SJason Wessel {
39153197fc4SJason Wessel 	error = -error;
39253197fc4SJason Wessel 	pkt[0] = 'E';
39353197fc4SJason Wessel 	pkt[1] = hex_asc[(error / 10)];
39453197fc4SJason Wessel 	pkt[2] = hex_asc[(error % 10)];
39553197fc4SJason Wessel 	pkt[3] = '\0';
39653197fc4SJason Wessel }
39753197fc4SJason Wessel 
39853197fc4SJason Wessel /*
39953197fc4SJason Wessel  * Thread ID accessors. We represent a flat TID space to GDB, where
40053197fc4SJason Wessel  * the per CPU idle threads (which under Linux all have PID 0) are
40153197fc4SJason Wessel  * remapped to negative TIDs.
40253197fc4SJason Wessel  */
40353197fc4SJason Wessel 
40484a0bd5bSJason Wessel #define BUF_THREAD_ID_SIZE	8
40553197fc4SJason Wessel 
40653197fc4SJason Wessel static char *pack_threadid(char *pkt, unsigned char *id)
40753197fc4SJason Wessel {
40884a0bd5bSJason Wessel 	unsigned char *limit;
40984a0bd5bSJason Wessel 	int lzero = 1;
41053197fc4SJason Wessel 
41184a0bd5bSJason Wessel 	limit = id + (BUF_THREAD_ID_SIZE / 2);
41284a0bd5bSJason Wessel 	while (id < limit) {
41384a0bd5bSJason Wessel 		if (!lzero || *id != 0) {
41450e1499fSAndy Shevchenko 			pkt = hex_byte_pack(pkt, *id);
41584a0bd5bSJason Wessel 			lzero = 0;
41684a0bd5bSJason Wessel 		}
41784a0bd5bSJason Wessel 		id++;
41884a0bd5bSJason Wessel 	}
41984a0bd5bSJason Wessel 
42084a0bd5bSJason Wessel 	if (lzero)
42150e1499fSAndy Shevchenko 		pkt = hex_byte_pack(pkt, 0);
42253197fc4SJason Wessel 
42353197fc4SJason Wessel 	return pkt;
42453197fc4SJason Wessel }
42553197fc4SJason Wessel 
42653197fc4SJason Wessel static void int_to_threadref(unsigned char *id, int value)
42753197fc4SJason Wessel {
42884a0bd5bSJason Wessel 	put_unaligned_be32(value, id);
42953197fc4SJason Wessel }
43053197fc4SJason Wessel 
43153197fc4SJason Wessel static struct task_struct *getthread(struct pt_regs *regs, int tid)
43253197fc4SJason Wessel {
43353197fc4SJason Wessel 	/*
43453197fc4SJason Wessel 	 * Non-positive TIDs are remapped to the cpu shadow information
43553197fc4SJason Wessel 	 */
43653197fc4SJason Wessel 	if (tid == 0 || tid == -1)
43753197fc4SJason Wessel 		tid = -atomic_read(&kgdb_active) - 2;
43853197fc4SJason Wessel 	if (tid < -1 && tid > -NR_CPUS - 2) {
43953197fc4SJason Wessel 		if (kgdb_info[-tid - 2].task)
44053197fc4SJason Wessel 			return kgdb_info[-tid - 2].task;
44153197fc4SJason Wessel 		else
44253197fc4SJason Wessel 			return idle_task(-tid - 2);
44353197fc4SJason Wessel 	}
44453197fc4SJason Wessel 	if (tid <= 0) {
44553197fc4SJason Wessel 		printk(KERN_ERR "KGDB: Internal thread select error\n");
44653197fc4SJason Wessel 		dump_stack();
44753197fc4SJason Wessel 		return NULL;
44853197fc4SJason Wessel 	}
44953197fc4SJason Wessel 
45053197fc4SJason Wessel 	/*
45153197fc4SJason Wessel 	 * find_task_by_pid_ns() does not take the tasklist lock anymore
45253197fc4SJason Wessel 	 * but is nicely RCU locked - hence is a pretty resilient
45353197fc4SJason Wessel 	 * thing to use:
45453197fc4SJason Wessel 	 */
45553197fc4SJason Wessel 	return find_task_by_pid_ns(tid, &init_pid_ns);
45653197fc4SJason Wessel }
45753197fc4SJason Wessel 
45853197fc4SJason Wessel 
45953197fc4SJason Wessel /*
46053197fc4SJason Wessel  * Remap normal tasks to their real PID,
46153197fc4SJason Wessel  * CPU shadow threads are mapped to -CPU - 2
46253197fc4SJason Wessel  */
46353197fc4SJason Wessel static inline int shadow_pid(int realpid)
46453197fc4SJason Wessel {
46553197fc4SJason Wessel 	if (realpid)
46653197fc4SJason Wessel 		return realpid;
46753197fc4SJason Wessel 
46853197fc4SJason Wessel 	return -raw_smp_processor_id() - 2;
46953197fc4SJason Wessel }
47053197fc4SJason Wessel 
47153197fc4SJason Wessel /*
47253197fc4SJason Wessel  * All the functions that start with gdb_cmd are the various
47353197fc4SJason Wessel  * operations to implement the handlers for the gdbserial protocol
47453197fc4SJason Wessel  * where KGDB is communicating with an external debugger
47553197fc4SJason Wessel  */
47653197fc4SJason Wessel 
47753197fc4SJason Wessel /* Handle the '?' status packets */
47853197fc4SJason Wessel static void gdb_cmd_status(struct kgdb_state *ks)
47953197fc4SJason Wessel {
48053197fc4SJason Wessel 	/*
48153197fc4SJason Wessel 	 * We know that this packet is only sent
48253197fc4SJason Wessel 	 * during initial connect.  So to be safe,
48353197fc4SJason Wessel 	 * we clear out our breakpoints now in case
48453197fc4SJason Wessel 	 * GDB is reconnecting.
48553197fc4SJason Wessel 	 */
48653197fc4SJason Wessel 	dbg_remove_all_break();
48753197fc4SJason Wessel 
48853197fc4SJason Wessel 	remcom_out_buffer[0] = 'S';
48950e1499fSAndy Shevchenko 	hex_byte_pack(&remcom_out_buffer[1], ks->signo);
49053197fc4SJason Wessel }
49153197fc4SJason Wessel 
49255751145SJason Wessel static void gdb_get_regs_helper(struct kgdb_state *ks)
49353197fc4SJason Wessel {
49453197fc4SJason Wessel 	struct task_struct *thread;
49553197fc4SJason Wessel 	void *local_debuggerinfo;
49653197fc4SJason Wessel 	int i;
49753197fc4SJason Wessel 
49853197fc4SJason Wessel 	thread = kgdb_usethread;
49953197fc4SJason Wessel 	if (!thread) {
50053197fc4SJason Wessel 		thread = kgdb_info[ks->cpu].task;
50153197fc4SJason Wessel 		local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
50253197fc4SJason Wessel 	} else {
50353197fc4SJason Wessel 		local_debuggerinfo = NULL;
50453197fc4SJason Wessel 		for_each_online_cpu(i) {
50553197fc4SJason Wessel 			/*
50653197fc4SJason Wessel 			 * Try to find the task on some other
50753197fc4SJason Wessel 			 * or possibly this node if we do not
50853197fc4SJason Wessel 			 * find the matching task then we try
50953197fc4SJason Wessel 			 * to approximate the results.
51053197fc4SJason Wessel 			 */
51153197fc4SJason Wessel 			if (thread == kgdb_info[i].task)
51253197fc4SJason Wessel 				local_debuggerinfo = kgdb_info[i].debuggerinfo;
51353197fc4SJason Wessel 		}
51453197fc4SJason Wessel 	}
51553197fc4SJason Wessel 
51653197fc4SJason Wessel 	/*
51753197fc4SJason Wessel 	 * All threads that don't have debuggerinfo should be
51853197fc4SJason Wessel 	 * in schedule() sleeping, since all other CPUs
51953197fc4SJason Wessel 	 * are in kgdb_wait, and thus have debuggerinfo.
52053197fc4SJason Wessel 	 */
52153197fc4SJason Wessel 	if (local_debuggerinfo) {
52253197fc4SJason Wessel 		pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
52353197fc4SJason Wessel 	} else {
52453197fc4SJason Wessel 		/*
52553197fc4SJason Wessel 		 * Pull stuff saved during switch_to; nothing
52653197fc4SJason Wessel 		 * else is accessible (or even particularly
52753197fc4SJason Wessel 		 * relevant).
52853197fc4SJason Wessel 		 *
52953197fc4SJason Wessel 		 * This should be enough for a stack trace.
53053197fc4SJason Wessel 		 */
53153197fc4SJason Wessel 		sleeping_thread_to_gdb_regs(gdb_regs, thread);
53253197fc4SJason Wessel 	}
53355751145SJason Wessel }
53455751145SJason Wessel 
53555751145SJason Wessel /* Handle the 'g' get registers request */
53655751145SJason Wessel static void gdb_cmd_getregs(struct kgdb_state *ks)
53755751145SJason Wessel {
53855751145SJason Wessel 	gdb_get_regs_helper(ks);
53953197fc4SJason Wessel 	kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
54053197fc4SJason Wessel }
54153197fc4SJason Wessel 
54253197fc4SJason Wessel /* Handle the 'G' set registers request */
54353197fc4SJason Wessel static void gdb_cmd_setregs(struct kgdb_state *ks)
54453197fc4SJason Wessel {
54553197fc4SJason Wessel 	kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
54653197fc4SJason Wessel 
54753197fc4SJason Wessel 	if (kgdb_usethread && kgdb_usethread != current) {
54853197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
54953197fc4SJason Wessel 	} else {
55053197fc4SJason Wessel 		gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
55153197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
55253197fc4SJason Wessel 	}
55353197fc4SJason Wessel }
55453197fc4SJason Wessel 
55553197fc4SJason Wessel /* Handle the 'm' memory read bytes */
55653197fc4SJason Wessel static void gdb_cmd_memread(struct kgdb_state *ks)
55753197fc4SJason Wessel {
55853197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
55953197fc4SJason Wessel 	unsigned long length;
56053197fc4SJason Wessel 	unsigned long addr;
56155751145SJason Wessel 	char *err;
56253197fc4SJason Wessel 
56353197fc4SJason Wessel 	if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
56453197fc4SJason Wessel 					kgdb_hex2long(&ptr, &length) > 0) {
56553197fc4SJason Wessel 		err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
56655751145SJason Wessel 		if (!err)
56755751145SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
56853197fc4SJason Wessel 	} else {
56953197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
57053197fc4SJason Wessel 	}
57153197fc4SJason Wessel }
57253197fc4SJason Wessel 
57353197fc4SJason Wessel /* Handle the 'M' memory write bytes */
57453197fc4SJason Wessel static void gdb_cmd_memwrite(struct kgdb_state *ks)
57553197fc4SJason Wessel {
57653197fc4SJason Wessel 	int err = write_mem_msg(0);
57753197fc4SJason Wessel 
57853197fc4SJason Wessel 	if (err)
57953197fc4SJason Wessel 		error_packet(remcom_out_buffer, err);
58053197fc4SJason Wessel 	else
58153197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
58253197fc4SJason Wessel }
58353197fc4SJason Wessel 
58455751145SJason Wessel #if DBG_MAX_REG_NUM > 0
58555751145SJason Wessel static char *gdb_hex_reg_helper(int regnum, char *out)
58655751145SJason Wessel {
58755751145SJason Wessel 	int i;
58855751145SJason Wessel 	int offset = 0;
58955751145SJason Wessel 
59055751145SJason Wessel 	for (i = 0; i < regnum; i++)
59155751145SJason Wessel 		offset += dbg_reg_def[i].size;
59255751145SJason Wessel 	return kgdb_mem2hex((char *)gdb_regs + offset, out,
59355751145SJason Wessel 			    dbg_reg_def[i].size);
59455751145SJason Wessel }
59555751145SJason Wessel 
59655751145SJason Wessel /* Handle the 'p' individual regster get */
59755751145SJason Wessel static void gdb_cmd_reg_get(struct kgdb_state *ks)
59855751145SJason Wessel {
59955751145SJason Wessel 	unsigned long regnum;
60055751145SJason Wessel 	char *ptr = &remcom_in_buffer[1];
60155751145SJason Wessel 
60255751145SJason Wessel 	kgdb_hex2long(&ptr, &regnum);
60355751145SJason Wessel 	if (regnum >= DBG_MAX_REG_NUM) {
60455751145SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
60555751145SJason Wessel 		return;
60655751145SJason Wessel 	}
60755751145SJason Wessel 	gdb_get_regs_helper(ks);
60855751145SJason Wessel 	gdb_hex_reg_helper(regnum, remcom_out_buffer);
60955751145SJason Wessel }
61055751145SJason Wessel 
61155751145SJason Wessel /* Handle the 'P' individual regster set */
61255751145SJason Wessel static void gdb_cmd_reg_set(struct kgdb_state *ks)
61355751145SJason Wessel {
61455751145SJason Wessel 	unsigned long regnum;
61555751145SJason Wessel 	char *ptr = &remcom_in_buffer[1];
6166d855b1dSJason Wessel 	int i = 0;
61755751145SJason Wessel 
61855751145SJason Wessel 	kgdb_hex2long(&ptr, &regnum);
61955751145SJason Wessel 	if (*ptr++ != '=' ||
62055751145SJason Wessel 	    !(!kgdb_usethread || kgdb_usethread == current) ||
62155751145SJason Wessel 	    !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) {
62255751145SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
62355751145SJason Wessel 		return;
62455751145SJason Wessel 	}
6256d855b1dSJason Wessel 	memset(gdb_regs, 0, sizeof(gdb_regs));
6266d855b1dSJason Wessel 	while (i < sizeof(gdb_regs) * 2)
6276d855b1dSJason Wessel 		if (hex_to_bin(ptr[i]) >= 0)
6286d855b1dSJason Wessel 			i++;
6296d855b1dSJason Wessel 		else
6306d855b1dSJason Wessel 			break;
6316d855b1dSJason Wessel 	i = i / 2;
6326d855b1dSJason Wessel 	kgdb_hex2mem(ptr, (char *)gdb_regs, i);
63355751145SJason Wessel 	dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
63455751145SJason Wessel 	strcpy(remcom_out_buffer, "OK");
63555751145SJason Wessel }
63655751145SJason Wessel #endif /* DBG_MAX_REG_NUM > 0 */
63755751145SJason Wessel 
63853197fc4SJason Wessel /* Handle the 'X' memory binary write bytes */
63953197fc4SJason Wessel static void gdb_cmd_binwrite(struct kgdb_state *ks)
64053197fc4SJason Wessel {
64153197fc4SJason Wessel 	int err = write_mem_msg(1);
64253197fc4SJason Wessel 
64353197fc4SJason Wessel 	if (err)
64453197fc4SJason Wessel 		error_packet(remcom_out_buffer, err);
64553197fc4SJason Wessel 	else
64653197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
64753197fc4SJason Wessel }
64853197fc4SJason Wessel 
64953197fc4SJason Wessel /* Handle the 'D' or 'k', detach or kill packets */
65053197fc4SJason Wessel static void gdb_cmd_detachkill(struct kgdb_state *ks)
65153197fc4SJason Wessel {
65253197fc4SJason Wessel 	int error;
65353197fc4SJason Wessel 
65453197fc4SJason Wessel 	/* The detach case */
65553197fc4SJason Wessel 	if (remcom_in_buffer[0] == 'D') {
65653197fc4SJason Wessel 		error = dbg_remove_all_break();
65753197fc4SJason Wessel 		if (error < 0) {
65853197fc4SJason Wessel 			error_packet(remcom_out_buffer, error);
65953197fc4SJason Wessel 		} else {
66053197fc4SJason Wessel 			strcpy(remcom_out_buffer, "OK");
66153197fc4SJason Wessel 			kgdb_connected = 0;
66253197fc4SJason Wessel 		}
66353197fc4SJason Wessel 		put_packet(remcom_out_buffer);
66453197fc4SJason Wessel 	} else {
66553197fc4SJason Wessel 		/*
66653197fc4SJason Wessel 		 * Assume the kill case, with no exit code checking,
66753197fc4SJason Wessel 		 * trying to force detach the debugger:
66853197fc4SJason Wessel 		 */
66953197fc4SJason Wessel 		dbg_remove_all_break();
67053197fc4SJason Wessel 		kgdb_connected = 0;
67153197fc4SJason Wessel 	}
67253197fc4SJason Wessel }
67353197fc4SJason Wessel 
67453197fc4SJason Wessel /* Handle the 'R' reboot packets */
67553197fc4SJason Wessel static int gdb_cmd_reboot(struct kgdb_state *ks)
67653197fc4SJason Wessel {
67753197fc4SJason Wessel 	/* For now, only honor R0 */
67853197fc4SJason Wessel 	if (strcmp(remcom_in_buffer, "R0") == 0) {
67953197fc4SJason Wessel 		printk(KERN_CRIT "Executing emergency reboot\n");
68053197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
68153197fc4SJason Wessel 		put_packet(remcom_out_buffer);
68253197fc4SJason Wessel 
68353197fc4SJason Wessel 		/*
68453197fc4SJason Wessel 		 * Execution should not return from
68553197fc4SJason Wessel 		 * machine_emergency_restart()
68653197fc4SJason Wessel 		 */
68753197fc4SJason Wessel 		machine_emergency_restart();
68853197fc4SJason Wessel 		kgdb_connected = 0;
68953197fc4SJason Wessel 
69053197fc4SJason Wessel 		return 1;
69153197fc4SJason Wessel 	}
69253197fc4SJason Wessel 	return 0;
69353197fc4SJason Wessel }
69453197fc4SJason Wessel 
69553197fc4SJason Wessel /* Handle the 'q' query packets */
69653197fc4SJason Wessel static void gdb_cmd_query(struct kgdb_state *ks)
69753197fc4SJason Wessel {
69853197fc4SJason Wessel 	struct task_struct *g;
69953197fc4SJason Wessel 	struct task_struct *p;
70084a0bd5bSJason Wessel 	unsigned char thref[BUF_THREAD_ID_SIZE];
70153197fc4SJason Wessel 	char *ptr;
70253197fc4SJason Wessel 	int i;
70353197fc4SJason Wessel 	int cpu;
70453197fc4SJason Wessel 	int finished = 0;
70553197fc4SJason Wessel 
70653197fc4SJason Wessel 	switch (remcom_in_buffer[1]) {
70753197fc4SJason Wessel 	case 's':
70853197fc4SJason Wessel 	case 'f':
709fb82c0ffSJason Wessel 		if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
71053197fc4SJason Wessel 			break;
71153197fc4SJason Wessel 
71253197fc4SJason Wessel 		i = 0;
71353197fc4SJason Wessel 		remcom_out_buffer[0] = 'm';
71453197fc4SJason Wessel 		ptr = remcom_out_buffer + 1;
71553197fc4SJason Wessel 		if (remcom_in_buffer[1] == 'f') {
71653197fc4SJason Wessel 			/* Each cpu is a shadow thread */
71753197fc4SJason Wessel 			for_each_online_cpu(cpu) {
71853197fc4SJason Wessel 				ks->thr_query = 0;
71953197fc4SJason Wessel 				int_to_threadref(thref, -cpu - 2);
72084a0bd5bSJason Wessel 				ptr = pack_threadid(ptr, thref);
72153197fc4SJason Wessel 				*(ptr++) = ',';
72253197fc4SJason Wessel 				i++;
72353197fc4SJason Wessel 			}
72453197fc4SJason Wessel 		}
72553197fc4SJason Wessel 
72653197fc4SJason Wessel 		do_each_thread(g, p) {
72753197fc4SJason Wessel 			if (i >= ks->thr_query && !finished) {
72853197fc4SJason Wessel 				int_to_threadref(thref, p->pid);
72984a0bd5bSJason Wessel 				ptr = pack_threadid(ptr, thref);
73053197fc4SJason Wessel 				*(ptr++) = ',';
73153197fc4SJason Wessel 				ks->thr_query++;
73253197fc4SJason Wessel 				if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
73353197fc4SJason Wessel 					finished = 1;
73453197fc4SJason Wessel 			}
73553197fc4SJason Wessel 			i++;
73653197fc4SJason Wessel 		} while_each_thread(g, p);
73753197fc4SJason Wessel 
73853197fc4SJason Wessel 		*(--ptr) = '\0';
73953197fc4SJason Wessel 		break;
74053197fc4SJason Wessel 
74153197fc4SJason Wessel 	case 'C':
74253197fc4SJason Wessel 		/* Current thread id */
74353197fc4SJason Wessel 		strcpy(remcom_out_buffer, "QC");
74453197fc4SJason Wessel 		ks->threadid = shadow_pid(current->pid);
74553197fc4SJason Wessel 		int_to_threadref(thref, ks->threadid);
74653197fc4SJason Wessel 		pack_threadid(remcom_out_buffer + 2, thref);
74753197fc4SJason Wessel 		break;
74853197fc4SJason Wessel 	case 'T':
749fb82c0ffSJason Wessel 		if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
75053197fc4SJason Wessel 			break;
751fb82c0ffSJason Wessel 
75253197fc4SJason Wessel 		ks->threadid = 0;
75353197fc4SJason Wessel 		ptr = remcom_in_buffer + 17;
75453197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
75553197fc4SJason Wessel 		if (!getthread(ks->linux_regs, ks->threadid)) {
75653197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
75753197fc4SJason Wessel 			break;
75853197fc4SJason Wessel 		}
75953197fc4SJason Wessel 		if ((int)ks->threadid > 0) {
76053197fc4SJason Wessel 			kgdb_mem2hex(getthread(ks->linux_regs,
76153197fc4SJason Wessel 					ks->threadid)->comm,
76253197fc4SJason Wessel 					remcom_out_buffer, 16);
76353197fc4SJason Wessel 		} else {
76453197fc4SJason Wessel 			static char tmpstr[23 + BUF_THREAD_ID_SIZE];
76553197fc4SJason Wessel 
76653197fc4SJason Wessel 			sprintf(tmpstr, "shadowCPU%d",
76753197fc4SJason Wessel 					(int)(-ks->threadid - 2));
76853197fc4SJason Wessel 			kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
76953197fc4SJason Wessel 		}
77053197fc4SJason Wessel 		break;
771a0de055cSJason Wessel #ifdef CONFIG_KGDB_KDB
772a0de055cSJason Wessel 	case 'R':
773a0de055cSJason Wessel 		if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
774a0de055cSJason Wessel 			int len = strlen(remcom_in_buffer + 6);
775a0de055cSJason Wessel 
776a0de055cSJason Wessel 			if ((len % 2) != 0) {
777a0de055cSJason Wessel 				strcpy(remcom_out_buffer, "E01");
778a0de055cSJason Wessel 				break;
779a0de055cSJason Wessel 			}
780a0de055cSJason Wessel 			kgdb_hex2mem(remcom_in_buffer + 6,
781a0de055cSJason Wessel 				     remcom_out_buffer, len);
782a0de055cSJason Wessel 			len = len / 2;
783a0de055cSJason Wessel 			remcom_out_buffer[len++] = 0;
784a0de055cSJason Wessel 
785a0de055cSJason Wessel 			kdb_parse(remcom_out_buffer);
786a0de055cSJason Wessel 			strcpy(remcom_out_buffer, "OK");
787a0de055cSJason Wessel 		}
788a0de055cSJason Wessel 		break;
789a0de055cSJason Wessel #endif
79053197fc4SJason Wessel 	}
79153197fc4SJason Wessel }
79253197fc4SJason Wessel 
79353197fc4SJason Wessel /* Handle the 'H' task query packets */
79453197fc4SJason Wessel static void gdb_cmd_task(struct kgdb_state *ks)
79553197fc4SJason Wessel {
79653197fc4SJason Wessel 	struct task_struct *thread;
79753197fc4SJason Wessel 	char *ptr;
79853197fc4SJason Wessel 
79953197fc4SJason Wessel 	switch (remcom_in_buffer[1]) {
80053197fc4SJason Wessel 	case 'g':
80153197fc4SJason Wessel 		ptr = &remcom_in_buffer[2];
80253197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
80353197fc4SJason Wessel 		thread = getthread(ks->linux_regs, ks->threadid);
80453197fc4SJason Wessel 		if (!thread && ks->threadid > 0) {
80553197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
80653197fc4SJason Wessel 			break;
80753197fc4SJason Wessel 		}
80853197fc4SJason Wessel 		kgdb_usethread = thread;
80953197fc4SJason Wessel 		ks->kgdb_usethreadid = ks->threadid;
81053197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
81153197fc4SJason Wessel 		break;
81253197fc4SJason Wessel 	case 'c':
81353197fc4SJason Wessel 		ptr = &remcom_in_buffer[2];
81453197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
81553197fc4SJason Wessel 		if (!ks->threadid) {
81653197fc4SJason Wessel 			kgdb_contthread = NULL;
81753197fc4SJason Wessel 		} else {
81853197fc4SJason Wessel 			thread = getthread(ks->linux_regs, ks->threadid);
81953197fc4SJason Wessel 			if (!thread && ks->threadid > 0) {
82053197fc4SJason Wessel 				error_packet(remcom_out_buffer, -EINVAL);
82153197fc4SJason Wessel 				break;
82253197fc4SJason Wessel 			}
82353197fc4SJason Wessel 			kgdb_contthread = thread;
82453197fc4SJason Wessel 		}
82553197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
82653197fc4SJason Wessel 		break;
82753197fc4SJason Wessel 	}
82853197fc4SJason Wessel }
82953197fc4SJason Wessel 
83053197fc4SJason Wessel /* Handle the 'T' thread query packets */
83153197fc4SJason Wessel static void gdb_cmd_thread(struct kgdb_state *ks)
83253197fc4SJason Wessel {
83353197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
83453197fc4SJason Wessel 	struct task_struct *thread;
83553197fc4SJason Wessel 
83653197fc4SJason Wessel 	kgdb_hex2long(&ptr, &ks->threadid);
83753197fc4SJason Wessel 	thread = getthread(ks->linux_regs, ks->threadid);
83853197fc4SJason Wessel 	if (thread)
83953197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
84053197fc4SJason Wessel 	else
84153197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
84253197fc4SJason Wessel }
84353197fc4SJason Wessel 
84453197fc4SJason Wessel /* Handle the 'z' or 'Z' breakpoint remove or set packets */
84553197fc4SJason Wessel static void gdb_cmd_break(struct kgdb_state *ks)
84653197fc4SJason Wessel {
84753197fc4SJason Wessel 	/*
84853197fc4SJason Wessel 	 * Since GDB-5.3, it's been drafted that '0' is a software
84953197fc4SJason Wessel 	 * breakpoint, '1' is a hardware breakpoint, so let's do that.
85053197fc4SJason Wessel 	 */
85153197fc4SJason Wessel 	char *bpt_type = &remcom_in_buffer[1];
85253197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[2];
85353197fc4SJason Wessel 	unsigned long addr;
85453197fc4SJason Wessel 	unsigned long length;
85553197fc4SJason Wessel 	int error = 0;
85653197fc4SJason Wessel 
85753197fc4SJason Wessel 	if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
85853197fc4SJason Wessel 		/* Unsupported */
85953197fc4SJason Wessel 		if (*bpt_type > '4')
86053197fc4SJason Wessel 			return;
86153197fc4SJason Wessel 	} else {
86253197fc4SJason Wessel 		if (*bpt_type != '0' && *bpt_type != '1')
86353197fc4SJason Wessel 			/* Unsupported. */
86453197fc4SJason Wessel 			return;
86553197fc4SJason Wessel 	}
86653197fc4SJason Wessel 
86753197fc4SJason Wessel 	/*
86853197fc4SJason Wessel 	 * Test if this is a hardware breakpoint, and
86953197fc4SJason Wessel 	 * if we support it:
87053197fc4SJason Wessel 	 */
87153197fc4SJason Wessel 	if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
87253197fc4SJason Wessel 		/* Unsupported. */
87353197fc4SJason Wessel 		return;
87453197fc4SJason Wessel 
87553197fc4SJason Wessel 	if (*(ptr++) != ',') {
87653197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
87753197fc4SJason Wessel 		return;
87853197fc4SJason Wessel 	}
87953197fc4SJason Wessel 	if (!kgdb_hex2long(&ptr, &addr)) {
88053197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
88153197fc4SJason Wessel 		return;
88253197fc4SJason Wessel 	}
88353197fc4SJason Wessel 	if (*(ptr++) != ',' ||
88453197fc4SJason Wessel 		!kgdb_hex2long(&ptr, &length)) {
88553197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
88653197fc4SJason Wessel 		return;
88753197fc4SJason Wessel 	}
88853197fc4SJason Wessel 
88953197fc4SJason Wessel 	if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
89053197fc4SJason Wessel 		error = dbg_set_sw_break(addr);
89153197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
89253197fc4SJason Wessel 		error = dbg_remove_sw_break(addr);
89353197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'Z')
89453197fc4SJason Wessel 		error = arch_kgdb_ops.set_hw_breakpoint(addr,
89553197fc4SJason Wessel 			(int)length, *bpt_type - '0');
89653197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'z')
89753197fc4SJason Wessel 		error = arch_kgdb_ops.remove_hw_breakpoint(addr,
89853197fc4SJason Wessel 			(int) length, *bpt_type - '0');
89953197fc4SJason Wessel 
90053197fc4SJason Wessel 	if (error == 0)
90153197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
90253197fc4SJason Wessel 	else
90353197fc4SJason Wessel 		error_packet(remcom_out_buffer, error);
90453197fc4SJason Wessel }
90553197fc4SJason Wessel 
90653197fc4SJason Wessel /* Handle the 'C' signal / exception passing packets */
90753197fc4SJason Wessel static int gdb_cmd_exception_pass(struct kgdb_state *ks)
90853197fc4SJason Wessel {
90953197fc4SJason Wessel 	/* C09 == pass exception
91053197fc4SJason Wessel 	 * C15 == detach kgdb, pass exception
91153197fc4SJason Wessel 	 */
91253197fc4SJason Wessel 	if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
91353197fc4SJason Wessel 
91453197fc4SJason Wessel 		ks->pass_exception = 1;
91553197fc4SJason Wessel 		remcom_in_buffer[0] = 'c';
91653197fc4SJason Wessel 
91753197fc4SJason Wessel 	} else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
91853197fc4SJason Wessel 
91953197fc4SJason Wessel 		ks->pass_exception = 1;
92053197fc4SJason Wessel 		remcom_in_buffer[0] = 'D';
92153197fc4SJason Wessel 		dbg_remove_all_break();
92253197fc4SJason Wessel 		kgdb_connected = 0;
92353197fc4SJason Wessel 		return 1;
92453197fc4SJason Wessel 
92553197fc4SJason Wessel 	} else {
92653197fc4SJason Wessel 		gdbstub_msg_write("KGDB only knows signal 9 (pass)"
92753197fc4SJason Wessel 			" and 15 (pass and disconnect)\n"
92853197fc4SJason Wessel 			"Executing a continue without signal passing\n", 0);
92953197fc4SJason Wessel 		remcom_in_buffer[0] = 'c';
93053197fc4SJason Wessel 	}
93153197fc4SJason Wessel 
93253197fc4SJason Wessel 	/* Indicate fall through */
93353197fc4SJason Wessel 	return -1;
93453197fc4SJason Wessel }
93553197fc4SJason Wessel 
93653197fc4SJason Wessel /*
93753197fc4SJason Wessel  * This function performs all gdbserial command procesing
93853197fc4SJason Wessel  */
93953197fc4SJason Wessel int gdb_serial_stub(struct kgdb_state *ks)
94053197fc4SJason Wessel {
94153197fc4SJason Wessel 	int error = 0;
94253197fc4SJason Wessel 	int tmp;
94353197fc4SJason Wessel 
94455751145SJason Wessel 	/* Initialize comm buffer and globals. */
94553197fc4SJason Wessel 	memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
94655751145SJason Wessel 	kgdb_usethread = kgdb_info[ks->cpu].task;
94755751145SJason Wessel 	ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
94855751145SJason Wessel 	ks->pass_exception = 0;
94953197fc4SJason Wessel 
95053197fc4SJason Wessel 	if (kgdb_connected) {
95184a0bd5bSJason Wessel 		unsigned char thref[BUF_THREAD_ID_SIZE];
95253197fc4SJason Wessel 		char *ptr;
95353197fc4SJason Wessel 
95453197fc4SJason Wessel 		/* Reply to host that an exception has occurred */
95553197fc4SJason Wessel 		ptr = remcom_out_buffer;
95653197fc4SJason Wessel 		*ptr++ = 'T';
95750e1499fSAndy Shevchenko 		ptr = hex_byte_pack(ptr, ks->signo);
95853197fc4SJason Wessel 		ptr += strlen(strcpy(ptr, "thread:"));
95953197fc4SJason Wessel 		int_to_threadref(thref, shadow_pid(current->pid));
96053197fc4SJason Wessel 		ptr = pack_threadid(ptr, thref);
96153197fc4SJason Wessel 		*ptr++ = ';';
96253197fc4SJason Wessel 		put_packet(remcom_out_buffer);
96353197fc4SJason Wessel 	}
96453197fc4SJason Wessel 
96553197fc4SJason Wessel 	while (1) {
96653197fc4SJason Wessel 		error = 0;
96753197fc4SJason Wessel 
96853197fc4SJason Wessel 		/* Clear the out buffer. */
96953197fc4SJason Wessel 		memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
97053197fc4SJason Wessel 
97153197fc4SJason Wessel 		get_packet(remcom_in_buffer);
97253197fc4SJason Wessel 
97353197fc4SJason Wessel 		switch (remcom_in_buffer[0]) {
97453197fc4SJason Wessel 		case '?': /* gdbserial status */
97553197fc4SJason Wessel 			gdb_cmd_status(ks);
97653197fc4SJason Wessel 			break;
97753197fc4SJason Wessel 		case 'g': /* return the value of the CPU registers */
97853197fc4SJason Wessel 			gdb_cmd_getregs(ks);
97953197fc4SJason Wessel 			break;
98053197fc4SJason Wessel 		case 'G': /* set the value of the CPU registers - return OK */
98153197fc4SJason Wessel 			gdb_cmd_setregs(ks);
98253197fc4SJason Wessel 			break;
98353197fc4SJason Wessel 		case 'm': /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
98453197fc4SJason Wessel 			gdb_cmd_memread(ks);
98553197fc4SJason Wessel 			break;
98653197fc4SJason Wessel 		case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
98753197fc4SJason Wessel 			gdb_cmd_memwrite(ks);
98853197fc4SJason Wessel 			break;
98955751145SJason Wessel #if DBG_MAX_REG_NUM > 0
99055751145SJason Wessel 		case 'p': /* pXX Return gdb register XX (in hex) */
99155751145SJason Wessel 			gdb_cmd_reg_get(ks);
99255751145SJason Wessel 			break;
99355751145SJason Wessel 		case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
99455751145SJason Wessel 			gdb_cmd_reg_set(ks);
99555751145SJason Wessel 			break;
99655751145SJason Wessel #endif /* DBG_MAX_REG_NUM > 0 */
99753197fc4SJason Wessel 		case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
99853197fc4SJason Wessel 			gdb_cmd_binwrite(ks);
99953197fc4SJason Wessel 			break;
100053197fc4SJason Wessel 			/* kill or detach. KGDB should treat this like a
100153197fc4SJason Wessel 			 * continue.
100253197fc4SJason Wessel 			 */
100353197fc4SJason Wessel 		case 'D': /* Debugger detach */
100453197fc4SJason Wessel 		case 'k': /* Debugger detach via kill */
100553197fc4SJason Wessel 			gdb_cmd_detachkill(ks);
100653197fc4SJason Wessel 			goto default_handle;
100753197fc4SJason Wessel 		case 'R': /* Reboot */
100853197fc4SJason Wessel 			if (gdb_cmd_reboot(ks))
100953197fc4SJason Wessel 				goto default_handle;
101053197fc4SJason Wessel 			break;
101153197fc4SJason Wessel 		case 'q': /* query command */
101253197fc4SJason Wessel 			gdb_cmd_query(ks);
101353197fc4SJason Wessel 			break;
101453197fc4SJason Wessel 		case 'H': /* task related */
101553197fc4SJason Wessel 			gdb_cmd_task(ks);
101653197fc4SJason Wessel 			break;
101753197fc4SJason Wessel 		case 'T': /* Query thread status */
101853197fc4SJason Wessel 			gdb_cmd_thread(ks);
101953197fc4SJason Wessel 			break;
102053197fc4SJason Wessel 		case 'z': /* Break point remove */
102153197fc4SJason Wessel 		case 'Z': /* Break point set */
102253197fc4SJason Wessel 			gdb_cmd_break(ks);
102353197fc4SJason Wessel 			break;
1024dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
1025dcc78711SJason Wessel 		case '3': /* Escape into back into kdb */
1026dcc78711SJason Wessel 			if (remcom_in_buffer[1] == '\0') {
1027dcc78711SJason Wessel 				gdb_cmd_detachkill(ks);
1028dcc78711SJason Wessel 				return DBG_PASS_EVENT;
1029dcc78711SJason Wessel 			}
1030dcc78711SJason Wessel #endif
103153197fc4SJason Wessel 		case 'C': /* Exception passing */
103253197fc4SJason Wessel 			tmp = gdb_cmd_exception_pass(ks);
103353197fc4SJason Wessel 			if (tmp > 0)
103453197fc4SJason Wessel 				goto default_handle;
103553197fc4SJason Wessel 			if (tmp == 0)
103653197fc4SJason Wessel 				break;
103753197fc4SJason Wessel 			/* Fall through on tmp < 0 */
103853197fc4SJason Wessel 		case 'c': /* Continue packet */
103953197fc4SJason Wessel 		case 's': /* Single step packet */
104053197fc4SJason Wessel 			if (kgdb_contthread && kgdb_contthread != current) {
104153197fc4SJason Wessel 				/* Can't switch threads in kgdb */
104253197fc4SJason Wessel 				error_packet(remcom_out_buffer, -EINVAL);
104353197fc4SJason Wessel 				break;
104453197fc4SJason Wessel 			}
104553197fc4SJason Wessel 			dbg_activate_sw_breakpoints();
104653197fc4SJason Wessel 			/* Fall through to default processing */
104753197fc4SJason Wessel 		default:
104853197fc4SJason Wessel default_handle:
104953197fc4SJason Wessel 			error = kgdb_arch_handle_exception(ks->ex_vector,
105053197fc4SJason Wessel 						ks->signo,
105153197fc4SJason Wessel 						ks->err_code,
105253197fc4SJason Wessel 						remcom_in_buffer,
105353197fc4SJason Wessel 						remcom_out_buffer,
105453197fc4SJason Wessel 						ks->linux_regs);
105553197fc4SJason Wessel 			/*
105653197fc4SJason Wessel 			 * Leave cmd processing on error, detach,
105753197fc4SJason Wessel 			 * kill, continue, or single step.
105853197fc4SJason Wessel 			 */
105953197fc4SJason Wessel 			if (error >= 0 || remcom_in_buffer[0] == 'D' ||
106053197fc4SJason Wessel 			    remcom_in_buffer[0] == 'k') {
106153197fc4SJason Wessel 				error = 0;
106253197fc4SJason Wessel 				goto kgdb_exit;
106353197fc4SJason Wessel 			}
106453197fc4SJason Wessel 
106553197fc4SJason Wessel 		}
106653197fc4SJason Wessel 
106753197fc4SJason Wessel 		/* reply to the request */
106853197fc4SJason Wessel 		put_packet(remcom_out_buffer);
106953197fc4SJason Wessel 	}
107053197fc4SJason Wessel 
107153197fc4SJason Wessel kgdb_exit:
107253197fc4SJason Wessel 	if (ks->pass_exception)
107353197fc4SJason Wessel 		error = 1;
107453197fc4SJason Wessel 	return error;
107553197fc4SJason Wessel }
1076dcc78711SJason Wessel 
1077dcc78711SJason Wessel int gdbstub_state(struct kgdb_state *ks, char *cmd)
1078dcc78711SJason Wessel {
1079dcc78711SJason Wessel 	int error;
1080dcc78711SJason Wessel 
1081dcc78711SJason Wessel 	switch (cmd[0]) {
1082dcc78711SJason Wessel 	case 'e':
1083dcc78711SJason Wessel 		error = kgdb_arch_handle_exception(ks->ex_vector,
1084dcc78711SJason Wessel 						   ks->signo,
1085dcc78711SJason Wessel 						   ks->err_code,
1086dcc78711SJason Wessel 						   remcom_in_buffer,
1087dcc78711SJason Wessel 						   remcom_out_buffer,
1088dcc78711SJason Wessel 						   ks->linux_regs);
1089dcc78711SJason Wessel 		return error;
1090dcc78711SJason Wessel 	case 's':
1091dcc78711SJason Wessel 	case 'c':
1092dcc78711SJason Wessel 		strcpy(remcom_in_buffer, cmd);
1093dcc78711SJason Wessel 		return 0;
1094f679c498SJason Wessel 	case '$':
1095f679c498SJason Wessel 		strcpy(remcom_in_buffer, cmd);
1096f679c498SJason Wessel 		gdbstub_use_prev_in_buf = strlen(remcom_in_buffer);
1097f679c498SJason Wessel 		gdbstub_prev_in_buf_pos = 0;
1098f679c498SJason Wessel 		return 0;
1099dcc78711SJason Wessel 	}
1100dcc78711SJason Wessel 	dbg_io_ops->write_char('+');
1101dcc78711SJason Wessel 	put_packet(remcom_out_buffer);
1102dcc78711SJason Wessel 	return 0;
1103dcc78711SJason Wessel }
1104d57f078bSDavid Howells 
1105d57f078bSDavid Howells /**
1106d57f078bSDavid Howells  * gdbstub_exit - Send an exit message to GDB
1107d57f078bSDavid Howells  * @status: The exit code to report.
1108d57f078bSDavid Howells  */
1109d57f078bSDavid Howells void gdbstub_exit(int status)
1110d57f078bSDavid Howells {
1111d57f078bSDavid Howells 	unsigned char checksum, ch, buffer[3];
1112d57f078bSDavid Howells 	int loop;
1113d57f078bSDavid Howells 
1114d57f078bSDavid Howells 	buffer[0] = 'W';
1115d57f078bSDavid Howells 	buffer[1] = hex_asc_hi(status);
1116d57f078bSDavid Howells 	buffer[2] = hex_asc_lo(status);
1117d57f078bSDavid Howells 
1118d57f078bSDavid Howells 	dbg_io_ops->write_char('$');
1119d57f078bSDavid Howells 	checksum = 0;
1120d57f078bSDavid Howells 
1121d57f078bSDavid Howells 	for (loop = 0; loop < 3; loop++) {
1122d57f078bSDavid Howells 		ch = buffer[loop];
1123d57f078bSDavid Howells 		checksum += ch;
1124d57f078bSDavid Howells 		dbg_io_ops->write_char(ch);
1125d57f078bSDavid Howells 	}
1126d57f078bSDavid Howells 
1127d57f078bSDavid Howells 	dbg_io_ops->write_char('#');
1128d57f078bSDavid Howells 	dbg_io_ops->write_char(hex_asc_hi(checksum));
1129d57f078bSDavid Howells 	dbg_io_ops->write_char(hex_asc_lo(checksum));
1130d57f078bSDavid Howells 
1131d57f078bSDavid Howells 	/* make sure the output is flushed, lest the bootloader clobber it */
1132*9fbe465eSJan Kiszka 	if (dbg_io_ops->flush)
1133d57f078bSDavid Howells 		dbg_io_ops->flush();
1134d57f078bSDavid Howells }
1135