xref: /openbmc/linux/kernel/debug/gdbstub.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1*f8416aa2SCai Huoqing // SPDX-License-Identifier: GPL-2.0-only
253197fc4SJason Wessel /*
353197fc4SJason Wessel  * Kernel Debug Core
453197fc4SJason Wessel  *
553197fc4SJason Wessel  * Maintainer: Jason Wessel <jason.wessel@windriver.com>
653197fc4SJason Wessel  *
753197fc4SJason Wessel  * Copyright (C) 2000-2001 VERITAS Software Corporation.
853197fc4SJason Wessel  * Copyright (C) 2002-2004 Timesys Corporation
953197fc4SJason Wessel  * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
10a2531293SPavel Machek  * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
1153197fc4SJason Wessel  * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
1253197fc4SJason Wessel  * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
1353197fc4SJason Wessel  * Copyright (C) 2005-2009 Wind River Systems, Inc.
1453197fc4SJason Wessel  * Copyright (C) 2007 MontaVista Software, Inc.
1553197fc4SJason Wessel  * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
1653197fc4SJason Wessel  *
1753197fc4SJason Wessel  * Contributors at various stages not listed above:
1853197fc4SJason Wessel  *  Jason Wessel ( jason.wessel@windriver.com )
1953197fc4SJason Wessel  *  George Anzinger <george@mvista.com>
2053197fc4SJason Wessel  *  Anurekh Saxena (anurekh.saxena@timesys.com)
2153197fc4SJason Wessel  *  Lake Stevens Instrument Division (Glenn Engel)
2253197fc4SJason Wessel  *  Jim Kingdon, Cygnus Support.
2353197fc4SJason Wessel  *
2453197fc4SJason Wessel  * Original KGDB stub: David Grothe <dave@gcom.com>,
2553197fc4SJason Wessel  * Tigran Aivazian <tigran@sco.com>
2653197fc4SJason Wessel  */
2753197fc4SJason Wessel 
2853197fc4SJason Wessel #include <linux/kernel.h>
293f07c014SIngo Molnar #include <linux/sched/signal.h>
3053197fc4SJason Wessel #include <linux/kgdb.h>
31f5316b4aSJason Wessel #include <linux/kdb.h>
3216559ae4SGreg Kroah-Hartman #include <linux/serial_core.h>
3353197fc4SJason Wessel #include <linux/reboot.h>
3453197fc4SJason Wessel #include <linux/uaccess.h>
3553197fc4SJason Wessel #include <asm/cacheflush.h>
3653197fc4SJason Wessel #include <asm/unaligned.h>
3753197fc4SJason Wessel #include "debug_core.h"
3853197fc4SJason Wessel 
3953197fc4SJason Wessel #define KGDB_MAX_THREAD_QUERY 17
4053197fc4SJason Wessel 
4153197fc4SJason Wessel /* Our I/O buffers. */
4253197fc4SJason Wessel static char			remcom_in_buffer[BUFMAX];
4353197fc4SJason Wessel static char			remcom_out_buffer[BUFMAX];
44f679c498SJason Wessel static int			gdbstub_use_prev_in_buf;
45f679c498SJason Wessel static int			gdbstub_prev_in_buf_pos;
4653197fc4SJason Wessel 
4753197fc4SJason Wessel /* Storage for the registers, in GDB format. */
4853197fc4SJason Wessel static unsigned long		gdb_regs[(NUMREGBYTES +
4953197fc4SJason Wessel 					sizeof(unsigned long) - 1) /
5053197fc4SJason Wessel 					sizeof(unsigned long)];
5153197fc4SJason Wessel 
5253197fc4SJason Wessel /*
5353197fc4SJason Wessel  * GDB remote protocol parser:
5453197fc4SJason Wessel  */
5553197fc4SJason Wessel 
56f5316b4aSJason Wessel #ifdef CONFIG_KGDB_KDB
gdbstub_read_wait(void)57f5316b4aSJason Wessel static int gdbstub_read_wait(void)
58f5316b4aSJason Wessel {
59f5316b4aSJason Wessel 	int ret = -1;
60f5316b4aSJason Wessel 	int i;
61f5316b4aSJason Wessel 
62f679c498SJason Wessel 	if (unlikely(gdbstub_use_prev_in_buf)) {
63f679c498SJason Wessel 		if (gdbstub_prev_in_buf_pos < gdbstub_use_prev_in_buf)
64f679c498SJason Wessel 			return remcom_in_buffer[gdbstub_prev_in_buf_pos++];
65f679c498SJason Wessel 		else
66f679c498SJason Wessel 			gdbstub_use_prev_in_buf = 0;
67f679c498SJason Wessel 	}
68f679c498SJason Wessel 
69f5316b4aSJason Wessel 	/* poll any additional I/O interfaces that are defined */
70f5316b4aSJason Wessel 	while (ret < 0)
71f5316b4aSJason Wessel 		for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
72f5316b4aSJason Wessel 			ret = kdb_poll_funcs[i]();
73f5316b4aSJason Wessel 			if (ret > 0)
74f5316b4aSJason Wessel 				break;
75f5316b4aSJason Wessel 		}
76f5316b4aSJason Wessel 	return ret;
77f5316b4aSJason Wessel }
78f5316b4aSJason Wessel #else
gdbstub_read_wait(void)79f5316b4aSJason Wessel static int gdbstub_read_wait(void)
80f5316b4aSJason Wessel {
81f5316b4aSJason Wessel 	int ret = dbg_io_ops->read_char();
82f5316b4aSJason Wessel 	while (ret == NO_POLL_CHAR)
83f5316b4aSJason Wessel 		ret = dbg_io_ops->read_char();
84f5316b4aSJason Wessel 	return ret;
85f5316b4aSJason Wessel }
86f5316b4aSJason Wessel #endif
8753197fc4SJason Wessel /* scan for the sequence $<data>#<checksum> */
get_packet(char * buffer)8853197fc4SJason Wessel static void get_packet(char *buffer)
8953197fc4SJason Wessel {
9053197fc4SJason Wessel 	unsigned char checksum;
9153197fc4SJason Wessel 	unsigned char xmitcsum;
9253197fc4SJason Wessel 	int count;
9353197fc4SJason Wessel 	char ch;
9453197fc4SJason Wessel 
9553197fc4SJason Wessel 	do {
9653197fc4SJason Wessel 		/*
9753197fc4SJason Wessel 		 * Spin and wait around for the start character, ignore all
9853197fc4SJason Wessel 		 * other characters:
9953197fc4SJason Wessel 		 */
100f5316b4aSJason Wessel 		while ((ch = (gdbstub_read_wait())) != '$')
10153197fc4SJason Wessel 			/* nothing */;
10253197fc4SJason Wessel 
10353197fc4SJason Wessel 		kgdb_connected = 1;
10453197fc4SJason Wessel 		checksum = 0;
10553197fc4SJason Wessel 		xmitcsum = -1;
10653197fc4SJason Wessel 
10753197fc4SJason Wessel 		count = 0;
10853197fc4SJason Wessel 
10953197fc4SJason Wessel 		/*
11053197fc4SJason Wessel 		 * now, read until a # or end of buffer is found:
11153197fc4SJason Wessel 		 */
11253197fc4SJason Wessel 		while (count < (BUFMAX - 1)) {
113f5316b4aSJason Wessel 			ch = gdbstub_read_wait();
11453197fc4SJason Wessel 			if (ch == '#')
11553197fc4SJason Wessel 				break;
11653197fc4SJason Wessel 			checksum = checksum + ch;
11753197fc4SJason Wessel 			buffer[count] = ch;
11853197fc4SJason Wessel 			count = count + 1;
11953197fc4SJason Wessel 		}
12053197fc4SJason Wessel 
12153197fc4SJason Wessel 		if (ch == '#') {
122a9fa20a7SAndy Shevchenko 			xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
123a9fa20a7SAndy Shevchenko 			xmitcsum += hex_to_bin(gdbstub_read_wait());
12453197fc4SJason Wessel 
12553197fc4SJason Wessel 			if (checksum != xmitcsum)
12653197fc4SJason Wessel 				/* failed checksum */
12753197fc4SJason Wessel 				dbg_io_ops->write_char('-');
12853197fc4SJason Wessel 			else
12953197fc4SJason Wessel 				/* successful transfer */
13053197fc4SJason Wessel 				dbg_io_ops->write_char('+');
13153197fc4SJason Wessel 			if (dbg_io_ops->flush)
13253197fc4SJason Wessel 				dbg_io_ops->flush();
13353197fc4SJason Wessel 		}
134f679c498SJason Wessel 		buffer[count] = 0;
13553197fc4SJason Wessel 	} while (checksum != xmitcsum);
13653197fc4SJason Wessel }
13753197fc4SJason Wessel 
13853197fc4SJason Wessel /*
13953197fc4SJason Wessel  * Send the packet in buffer.
14053197fc4SJason Wessel  * Check for gdb connection if asked for.
14153197fc4SJason Wessel  */
put_packet(char * buffer)14253197fc4SJason Wessel static void put_packet(char *buffer)
14353197fc4SJason Wessel {
14453197fc4SJason Wessel 	unsigned char checksum;
14553197fc4SJason Wessel 	int count;
14653197fc4SJason Wessel 	char ch;
14753197fc4SJason Wessel 
14853197fc4SJason Wessel 	/*
14953197fc4SJason Wessel 	 * $<packet info>#<checksum>.
15053197fc4SJason Wessel 	 */
15153197fc4SJason Wessel 	while (1) {
15253197fc4SJason Wessel 		dbg_io_ops->write_char('$');
15353197fc4SJason Wessel 		checksum = 0;
15453197fc4SJason Wessel 		count = 0;
15553197fc4SJason Wessel 
15653197fc4SJason Wessel 		while ((ch = buffer[count])) {
15753197fc4SJason Wessel 			dbg_io_ops->write_char(ch);
15853197fc4SJason Wessel 			checksum += ch;
15953197fc4SJason Wessel 			count++;
16053197fc4SJason Wessel 		}
16153197fc4SJason Wessel 
16253197fc4SJason Wessel 		dbg_io_ops->write_char('#');
16353197fc4SJason Wessel 		dbg_io_ops->write_char(hex_asc_hi(checksum));
16453197fc4SJason Wessel 		dbg_io_ops->write_char(hex_asc_lo(checksum));
16553197fc4SJason Wessel 		if (dbg_io_ops->flush)
16653197fc4SJason Wessel 			dbg_io_ops->flush();
16753197fc4SJason Wessel 
16853197fc4SJason Wessel 		/* Now see what we get in reply. */
169f5316b4aSJason Wessel 		ch = gdbstub_read_wait();
17053197fc4SJason Wessel 
17153197fc4SJason Wessel 		if (ch == 3)
172f5316b4aSJason Wessel 			ch = gdbstub_read_wait();
17353197fc4SJason Wessel 
17453197fc4SJason Wessel 		/* If we get an ACK, we are done. */
17553197fc4SJason Wessel 		if (ch == '+')
17653197fc4SJason Wessel 			return;
17753197fc4SJason Wessel 
17853197fc4SJason Wessel 		/*
17953197fc4SJason Wessel 		 * If we get the start of another packet, this means
18053197fc4SJason Wessel 		 * that GDB is attempting to reconnect.  We will NAK
18153197fc4SJason Wessel 		 * the packet being sent, and stop trying to send this
18253197fc4SJason Wessel 		 * packet.
18353197fc4SJason Wessel 		 */
18453197fc4SJason Wessel 		if (ch == '$') {
18553197fc4SJason Wessel 			dbg_io_ops->write_char('-');
18653197fc4SJason Wessel 			if (dbg_io_ops->flush)
18753197fc4SJason Wessel 				dbg_io_ops->flush();
18853197fc4SJason Wessel 			return;
18953197fc4SJason Wessel 		}
19053197fc4SJason Wessel 	}
19153197fc4SJason Wessel }
19253197fc4SJason Wessel 
19353197fc4SJason Wessel static char gdbmsgbuf[BUFMAX + 1];
19453197fc4SJason Wessel 
gdbstub_msg_write(const char * s,int len)19553197fc4SJason Wessel void gdbstub_msg_write(const char *s, int len)
19653197fc4SJason Wessel {
19753197fc4SJason Wessel 	char *bufptr;
19853197fc4SJason Wessel 	int wcount;
19953197fc4SJason Wessel 	int i;
20053197fc4SJason Wessel 
201a0de055cSJason Wessel 	if (len == 0)
202a0de055cSJason Wessel 		len = strlen(s);
203a0de055cSJason 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++)
21950e1499fSAndy Shevchenko 			bufptr = hex_byte_pack(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  */
kgdb_mem2hex(char * mem,char * buf,int count)23655751145SJason Wessel char *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 
247fe557319SChristoph Hellwig 	err = copy_from_kernel_nofault(tmp, mem, count);
24855751145SJason Wessel 	if (err)
24955751145SJason Wessel 		return NULL;
25053197fc4SJason Wessel 	while (count > 0) {
25150e1499fSAndy Shevchenko 		buf = hex_byte_pack(buf, *tmp);
25253197fc4SJason Wessel 		tmp++;
25353197fc4SJason Wessel 		count--;
25453197fc4SJason Wessel 	}
25553197fc4SJason Wessel 	*buf = 0;
25653197fc4SJason Wessel 
25755751145SJason Wessel 	return buf;
25853197fc4SJason Wessel }
25953197fc4SJason Wessel 
26053197fc4SJason Wessel /*
26153197fc4SJason Wessel  * Convert the hex array pointed to by buf into binary to be placed in
26253197fc4SJason Wessel  * mem.  Return a pointer to the character AFTER the last byte
26353197fc4SJason Wessel  * written.  May return an error.
26453197fc4SJason Wessel  */
kgdb_hex2mem(char * buf,char * mem,int count)26553197fc4SJason Wessel int kgdb_hex2mem(char *buf, char *mem, int count)
26653197fc4SJason Wessel {
26753197fc4SJason Wessel 	char *tmp_raw;
26853197fc4SJason Wessel 	char *tmp_hex;
26953197fc4SJason Wessel 
27053197fc4SJason Wessel 	/*
27153197fc4SJason Wessel 	 * We use the upper half of buf as an intermediate buffer for the
27253197fc4SJason Wessel 	 * raw memory that is converted from hex.
27353197fc4SJason Wessel 	 */
27453197fc4SJason Wessel 	tmp_raw = buf + count * 2;
27553197fc4SJason Wessel 
27653197fc4SJason Wessel 	tmp_hex = tmp_raw - 1;
27753197fc4SJason Wessel 	while (tmp_hex >= buf) {
27853197fc4SJason Wessel 		tmp_raw--;
279a9fa20a7SAndy Shevchenko 		*tmp_raw = hex_to_bin(*tmp_hex--);
280a9fa20a7SAndy Shevchenko 		*tmp_raw |= hex_to_bin(*tmp_hex--) << 4;
28153197fc4SJason Wessel 	}
28253197fc4SJason Wessel 
283fe557319SChristoph Hellwig 	return copy_to_kernel_nofault(mem, tmp_raw, count);
28453197fc4SJason Wessel }
28553197fc4SJason Wessel 
28653197fc4SJason Wessel /*
28753197fc4SJason Wessel  * While we find nice hex chars, build a long_val.
28853197fc4SJason Wessel  * Return number of chars processed.
28953197fc4SJason Wessel  */
kgdb_hex2long(char ** ptr,unsigned long * long_val)29053197fc4SJason Wessel int kgdb_hex2long(char **ptr, unsigned long *long_val)
29153197fc4SJason Wessel {
29253197fc4SJason Wessel 	int hex_val;
29353197fc4SJason Wessel 	int num = 0;
29453197fc4SJason Wessel 	int negate = 0;
29553197fc4SJason Wessel 
29653197fc4SJason Wessel 	*long_val = 0;
29753197fc4SJason Wessel 
29853197fc4SJason Wessel 	if (**ptr == '-') {
29953197fc4SJason Wessel 		negate = 1;
30053197fc4SJason Wessel 		(*ptr)++;
30153197fc4SJason Wessel 	}
30253197fc4SJason Wessel 	while (**ptr) {
303a9fa20a7SAndy Shevchenko 		hex_val = hex_to_bin(**ptr);
30453197fc4SJason Wessel 		if (hex_val < 0)
30553197fc4SJason Wessel 			break;
30653197fc4SJason Wessel 
30753197fc4SJason Wessel 		*long_val = (*long_val << 4) | hex_val;
30853197fc4SJason Wessel 		num++;
30953197fc4SJason Wessel 		(*ptr)++;
31053197fc4SJason Wessel 	}
31153197fc4SJason Wessel 
31253197fc4SJason Wessel 	if (negate)
31353197fc4SJason Wessel 		*long_val = -*long_val;
31453197fc4SJason Wessel 
31553197fc4SJason Wessel 	return num;
31653197fc4SJason Wessel }
31753197fc4SJason Wessel 
31853197fc4SJason Wessel /*
31953197fc4SJason Wessel  * Copy the binary array pointed to by buf into mem.  Fix $, #, and
32053197fc4SJason Wessel  * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
3212bbd9b0fSBhaskar Chowdhury  * The input buf is overwritten with the result to write to mem.
32253197fc4SJason Wessel  */
kgdb_ebin2mem(char * buf,char * mem,int count)32353197fc4SJason Wessel static int kgdb_ebin2mem(char *buf, char *mem, int count)
32453197fc4SJason Wessel {
32553197fc4SJason Wessel 	int size = 0;
32653197fc4SJason Wessel 	char *c = buf;
32753197fc4SJason Wessel 
32853197fc4SJason Wessel 	while (count-- > 0) {
32953197fc4SJason Wessel 		c[size] = *buf++;
33053197fc4SJason Wessel 		if (c[size] == 0x7d)
33153197fc4SJason Wessel 			c[size] = *buf++ ^ 0x20;
33253197fc4SJason Wessel 		size++;
33353197fc4SJason Wessel 	}
33453197fc4SJason Wessel 
335fe557319SChristoph Hellwig 	return copy_to_kernel_nofault(mem, c, size);
33653197fc4SJason Wessel }
33753197fc4SJason Wessel 
338534af108SJason Wessel #if DBG_MAX_REG_NUM > 0
pt_regs_to_gdb_regs(unsigned long * gdb_regs,struct pt_regs * regs)339534af108SJason Wessel void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
340534af108SJason Wessel {
341534af108SJason Wessel 	int i;
342534af108SJason Wessel 	int idx = 0;
343534af108SJason Wessel 	char *ptr = (char *)gdb_regs;
344534af108SJason Wessel 
345534af108SJason Wessel 	for (i = 0; i < DBG_MAX_REG_NUM; i++) {
346534af108SJason Wessel 		dbg_get_reg(i, ptr + idx, regs);
347534af108SJason Wessel 		idx += dbg_reg_def[i].size;
348534af108SJason Wessel 	}
349534af108SJason Wessel }
350534af108SJason Wessel 
gdb_regs_to_pt_regs(unsigned long * gdb_regs,struct pt_regs * regs)351534af108SJason Wessel void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
352534af108SJason Wessel {
353534af108SJason Wessel 	int i;
354534af108SJason Wessel 	int idx = 0;
355534af108SJason Wessel 	char *ptr = (char *)gdb_regs;
356534af108SJason Wessel 
357534af108SJason Wessel 	for (i = 0; i < DBG_MAX_REG_NUM; i++) {
358534af108SJason Wessel 		dbg_set_reg(i, ptr + idx, regs);
359534af108SJason Wessel 		idx += dbg_reg_def[i].size;
360534af108SJason Wessel 	}
361534af108SJason Wessel }
362534af108SJason Wessel #endif /* DBG_MAX_REG_NUM > 0 */
363534af108SJason Wessel 
36453197fc4SJason Wessel /* Write memory due to an 'M' or 'X' packet. */
write_mem_msg(int binary)36553197fc4SJason Wessel static int write_mem_msg(int binary)
36653197fc4SJason Wessel {
36753197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
36853197fc4SJason Wessel 	unsigned long addr;
36953197fc4SJason Wessel 	unsigned long length;
37053197fc4SJason Wessel 	int err;
37153197fc4SJason Wessel 
37253197fc4SJason Wessel 	if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
37353197fc4SJason Wessel 	    kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
37453197fc4SJason Wessel 		if (binary)
37553197fc4SJason Wessel 			err = kgdb_ebin2mem(ptr, (char *)addr, length);
37653197fc4SJason Wessel 		else
37753197fc4SJason Wessel 			err = kgdb_hex2mem(ptr, (char *)addr, length);
37853197fc4SJason Wessel 		if (err)
37953197fc4SJason Wessel 			return err;
38053197fc4SJason Wessel 		if (CACHE_FLUSH_IS_SAFE)
38153197fc4SJason Wessel 			flush_icache_range(addr, addr + length);
38253197fc4SJason Wessel 		return 0;
38353197fc4SJason Wessel 	}
38453197fc4SJason Wessel 
38553197fc4SJason Wessel 	return -EINVAL;
38653197fc4SJason Wessel }
38753197fc4SJason Wessel 
error_packet(char * pkt,int error)38853197fc4SJason Wessel static void error_packet(char *pkt, int error)
38953197fc4SJason Wessel {
39053197fc4SJason Wessel 	error = -error;
39153197fc4SJason Wessel 	pkt[0] = 'E';
39253197fc4SJason Wessel 	pkt[1] = hex_asc[(error / 10)];
39353197fc4SJason Wessel 	pkt[2] = hex_asc[(error % 10)];
39453197fc4SJason Wessel 	pkt[3] = '\0';
39553197fc4SJason Wessel }
39653197fc4SJason Wessel 
39753197fc4SJason Wessel /*
39853197fc4SJason Wessel  * Thread ID accessors. We represent a flat TID space to GDB, where
39953197fc4SJason Wessel  * the per CPU idle threads (which under Linux all have PID 0) are
40053197fc4SJason Wessel  * remapped to negative TIDs.
40153197fc4SJason Wessel  */
40253197fc4SJason Wessel 
40384a0bd5bSJason Wessel #define BUF_THREAD_ID_SIZE	8
40453197fc4SJason Wessel 
pack_threadid(char * pkt,unsigned char * id)40553197fc4SJason Wessel static char *pack_threadid(char *pkt, unsigned char *id)
40653197fc4SJason Wessel {
40784a0bd5bSJason Wessel 	unsigned char *limit;
40884a0bd5bSJason Wessel 	int lzero = 1;
40953197fc4SJason Wessel 
41084a0bd5bSJason Wessel 	limit = id + (BUF_THREAD_ID_SIZE / 2);
41184a0bd5bSJason Wessel 	while (id < limit) {
41284a0bd5bSJason Wessel 		if (!lzero || *id != 0) {
41350e1499fSAndy Shevchenko 			pkt = hex_byte_pack(pkt, *id);
41484a0bd5bSJason Wessel 			lzero = 0;
41584a0bd5bSJason Wessel 		}
41684a0bd5bSJason Wessel 		id++;
41784a0bd5bSJason Wessel 	}
41884a0bd5bSJason Wessel 
41984a0bd5bSJason Wessel 	if (lzero)
42050e1499fSAndy Shevchenko 		pkt = hex_byte_pack(pkt, 0);
42153197fc4SJason Wessel 
42253197fc4SJason Wessel 	return pkt;
42353197fc4SJason Wessel }
42453197fc4SJason Wessel 
int_to_threadref(unsigned char * id,int value)42553197fc4SJason Wessel static void int_to_threadref(unsigned char *id, int value)
42653197fc4SJason Wessel {
42784a0bd5bSJason Wessel 	put_unaligned_be32(value, id);
42853197fc4SJason Wessel }
42953197fc4SJason Wessel 
getthread(struct pt_regs * regs,int tid)43053197fc4SJason Wessel static struct task_struct *getthread(struct pt_regs *regs, int tid)
43153197fc4SJason Wessel {
43253197fc4SJason Wessel 	/*
43353197fc4SJason Wessel 	 * Non-positive TIDs are remapped to the cpu shadow information
43453197fc4SJason Wessel 	 */
43553197fc4SJason Wessel 	if (tid == 0 || tid == -1)
43653197fc4SJason Wessel 		tid = -atomic_read(&kgdb_active) - 2;
43753197fc4SJason Wessel 	if (tid < -1 && tid > -NR_CPUS - 2) {
43853197fc4SJason Wessel 		if (kgdb_info[-tid - 2].task)
43953197fc4SJason Wessel 			return kgdb_info[-tid - 2].task;
44053197fc4SJason Wessel 		else
44153197fc4SJason Wessel 			return idle_task(-tid - 2);
44253197fc4SJason Wessel 	}
44353197fc4SJason Wessel 	if (tid <= 0) {
44453197fc4SJason Wessel 		printk(KERN_ERR "KGDB: Internal thread select error\n");
44553197fc4SJason Wessel 		dump_stack();
44653197fc4SJason Wessel 		return NULL;
44753197fc4SJason Wessel 	}
44853197fc4SJason Wessel 
44953197fc4SJason Wessel 	/*
45053197fc4SJason Wessel 	 * find_task_by_pid_ns() does not take the tasklist lock anymore
45153197fc4SJason Wessel 	 * but is nicely RCU locked - hence is a pretty resilient
45253197fc4SJason Wessel 	 * thing to use:
45353197fc4SJason Wessel 	 */
45453197fc4SJason Wessel 	return find_task_by_pid_ns(tid, &init_pid_ns);
45553197fc4SJason Wessel }
45653197fc4SJason Wessel 
45753197fc4SJason Wessel 
45853197fc4SJason Wessel /*
45953197fc4SJason Wessel  * Remap normal tasks to their real PID,
46053197fc4SJason Wessel  * CPU shadow threads are mapped to -CPU - 2
46153197fc4SJason Wessel  */
shadow_pid(int realpid)46253197fc4SJason Wessel static inline int shadow_pid(int realpid)
46353197fc4SJason Wessel {
46453197fc4SJason Wessel 	if (realpid)
46553197fc4SJason Wessel 		return realpid;
46653197fc4SJason Wessel 
46753197fc4SJason Wessel 	return -raw_smp_processor_id() - 2;
46853197fc4SJason Wessel }
46953197fc4SJason Wessel 
47053197fc4SJason Wessel /*
47153197fc4SJason Wessel  * All the functions that start with gdb_cmd are the various
47253197fc4SJason Wessel  * operations to implement the handlers for the gdbserial protocol
47353197fc4SJason Wessel  * where KGDB is communicating with an external debugger
47453197fc4SJason Wessel  */
47553197fc4SJason Wessel 
47653197fc4SJason Wessel /* Handle the '?' status packets */
gdb_cmd_status(struct kgdb_state * ks)47753197fc4SJason Wessel static void gdb_cmd_status(struct kgdb_state *ks)
47853197fc4SJason Wessel {
47953197fc4SJason Wessel 	/*
48053197fc4SJason Wessel 	 * We know that this packet is only sent
48153197fc4SJason Wessel 	 * during initial connect.  So to be safe,
48253197fc4SJason Wessel 	 * we clear out our breakpoints now in case
48353197fc4SJason Wessel 	 * GDB is reconnecting.
48453197fc4SJason Wessel 	 */
48553197fc4SJason Wessel 	dbg_remove_all_break();
48653197fc4SJason Wessel 
48753197fc4SJason Wessel 	remcom_out_buffer[0] = 'S';
48850e1499fSAndy Shevchenko 	hex_byte_pack(&remcom_out_buffer[1], ks->signo);
48953197fc4SJason Wessel }
49053197fc4SJason Wessel 
gdb_get_regs_helper(struct kgdb_state * ks)49155751145SJason Wessel static void gdb_get_regs_helper(struct kgdb_state *ks)
49253197fc4SJason Wessel {
49353197fc4SJason Wessel 	struct task_struct *thread;
49453197fc4SJason Wessel 	void *local_debuggerinfo;
49553197fc4SJason Wessel 	int i;
49653197fc4SJason Wessel 
49753197fc4SJason Wessel 	thread = kgdb_usethread;
49853197fc4SJason Wessel 	if (!thread) {
49953197fc4SJason Wessel 		thread = kgdb_info[ks->cpu].task;
50053197fc4SJason Wessel 		local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
50153197fc4SJason Wessel 	} else {
50253197fc4SJason Wessel 		local_debuggerinfo = NULL;
50353197fc4SJason Wessel 		for_each_online_cpu(i) {
50453197fc4SJason Wessel 			/*
50553197fc4SJason Wessel 			 * Try to find the task on some other
50653197fc4SJason Wessel 			 * or possibly this node if we do not
50753197fc4SJason Wessel 			 * find the matching task then we try
50853197fc4SJason Wessel 			 * to approximate the results.
50953197fc4SJason Wessel 			 */
51053197fc4SJason Wessel 			if (thread == kgdb_info[i].task)
51153197fc4SJason Wessel 				local_debuggerinfo = kgdb_info[i].debuggerinfo;
51253197fc4SJason Wessel 		}
51353197fc4SJason Wessel 	}
51453197fc4SJason Wessel 
51553197fc4SJason Wessel 	/*
51653197fc4SJason Wessel 	 * All threads that don't have debuggerinfo should be
51753197fc4SJason Wessel 	 * in schedule() sleeping, since all other CPUs
51853197fc4SJason Wessel 	 * are in kgdb_wait, and thus have debuggerinfo.
51953197fc4SJason Wessel 	 */
52053197fc4SJason Wessel 	if (local_debuggerinfo) {
52153197fc4SJason Wessel 		pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
52253197fc4SJason Wessel 	} else {
52353197fc4SJason Wessel 		/*
52453197fc4SJason Wessel 		 * Pull stuff saved during switch_to; nothing
52553197fc4SJason Wessel 		 * else is accessible (or even particularly
52653197fc4SJason Wessel 		 * relevant).
52753197fc4SJason Wessel 		 *
52853197fc4SJason Wessel 		 * This should be enough for a stack trace.
52953197fc4SJason Wessel 		 */
53053197fc4SJason Wessel 		sleeping_thread_to_gdb_regs(gdb_regs, thread);
53153197fc4SJason Wessel 	}
53255751145SJason Wessel }
53355751145SJason Wessel 
53455751145SJason Wessel /* Handle the 'g' get registers request */
gdb_cmd_getregs(struct kgdb_state * ks)53555751145SJason Wessel static void gdb_cmd_getregs(struct kgdb_state *ks)
53655751145SJason Wessel {
53755751145SJason Wessel 	gdb_get_regs_helper(ks);
53853197fc4SJason Wessel 	kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
53953197fc4SJason Wessel }
54053197fc4SJason Wessel 
54153197fc4SJason Wessel /* Handle the 'G' set registers request */
gdb_cmd_setregs(struct kgdb_state * ks)54253197fc4SJason Wessel static void gdb_cmd_setregs(struct kgdb_state *ks)
54353197fc4SJason Wessel {
54453197fc4SJason Wessel 	kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
54553197fc4SJason Wessel 
54653197fc4SJason Wessel 	if (kgdb_usethread && kgdb_usethread != current) {
54753197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
54853197fc4SJason Wessel 	} else {
54953197fc4SJason Wessel 		gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
55053197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
55153197fc4SJason Wessel 	}
55253197fc4SJason Wessel }
55353197fc4SJason Wessel 
55453197fc4SJason Wessel /* Handle the 'm' memory read bytes */
gdb_cmd_memread(struct kgdb_state * ks)55553197fc4SJason Wessel static void gdb_cmd_memread(struct kgdb_state *ks)
55653197fc4SJason Wessel {
55753197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
55853197fc4SJason Wessel 	unsigned long length;
55953197fc4SJason Wessel 	unsigned long addr;
56055751145SJason Wessel 	char *err;
56153197fc4SJason Wessel 
56253197fc4SJason Wessel 	if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
56353197fc4SJason Wessel 					kgdb_hex2long(&ptr, &length) > 0) {
56453197fc4SJason Wessel 		err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
56555751145SJason Wessel 		if (!err)
56655751145SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
56753197fc4SJason Wessel 	} else {
56853197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
56953197fc4SJason Wessel 	}
57053197fc4SJason Wessel }
57153197fc4SJason Wessel 
57253197fc4SJason Wessel /* Handle the 'M' memory write bytes */
gdb_cmd_memwrite(struct kgdb_state * ks)57353197fc4SJason Wessel static void gdb_cmd_memwrite(struct kgdb_state *ks)
57453197fc4SJason Wessel {
57553197fc4SJason Wessel 	int err = write_mem_msg(0);
57653197fc4SJason Wessel 
57753197fc4SJason Wessel 	if (err)
57853197fc4SJason Wessel 		error_packet(remcom_out_buffer, err);
57953197fc4SJason Wessel 	else
58053197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
58153197fc4SJason Wessel }
58253197fc4SJason Wessel 
58355751145SJason Wessel #if DBG_MAX_REG_NUM > 0
gdb_hex_reg_helper(int regnum,char * out)58455751145SJason Wessel static char *gdb_hex_reg_helper(int regnum, char *out)
58555751145SJason Wessel {
58655751145SJason Wessel 	int i;
58755751145SJason Wessel 	int offset = 0;
58855751145SJason Wessel 
58955751145SJason Wessel 	for (i = 0; i < regnum; i++)
59055751145SJason Wessel 		offset += dbg_reg_def[i].size;
59155751145SJason Wessel 	return kgdb_mem2hex((char *)gdb_regs + offset, out,
59255751145SJason Wessel 			    dbg_reg_def[i].size);
59355751145SJason Wessel }
59455751145SJason Wessel 
595cbd026e1Swengjianfeng /* Handle the 'p' individual register get */
gdb_cmd_reg_get(struct kgdb_state * ks)59655751145SJason Wessel static void gdb_cmd_reg_get(struct kgdb_state *ks)
59755751145SJason Wessel {
59855751145SJason Wessel 	unsigned long regnum;
59955751145SJason Wessel 	char *ptr = &remcom_in_buffer[1];
60055751145SJason Wessel 
60155751145SJason Wessel 	kgdb_hex2long(&ptr, &regnum);
60255751145SJason Wessel 	if (regnum >= DBG_MAX_REG_NUM) {
60355751145SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
60455751145SJason Wessel 		return;
60555751145SJason Wessel 	}
60655751145SJason Wessel 	gdb_get_regs_helper(ks);
60755751145SJason Wessel 	gdb_hex_reg_helper(regnum, remcom_out_buffer);
60855751145SJason Wessel }
60955751145SJason Wessel 
610cbd026e1Swengjianfeng /* Handle the 'P' individual register set */
gdb_cmd_reg_set(struct kgdb_state * ks)61155751145SJason Wessel static void gdb_cmd_reg_set(struct kgdb_state *ks)
61255751145SJason Wessel {
61355751145SJason Wessel 	unsigned long regnum;
61455751145SJason Wessel 	char *ptr = &remcom_in_buffer[1];
6156d855b1dSJason Wessel 	int i = 0;
61655751145SJason Wessel 
61755751145SJason Wessel 	kgdb_hex2long(&ptr, &regnum);
61855751145SJason Wessel 	if (*ptr++ != '=' ||
61955751145SJason Wessel 	    !(!kgdb_usethread || kgdb_usethread == current) ||
62055751145SJason Wessel 	    !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) {
62155751145SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
62255751145SJason Wessel 		return;
62355751145SJason Wessel 	}
6246d855b1dSJason Wessel 	memset(gdb_regs, 0, sizeof(gdb_regs));
6256d855b1dSJason Wessel 	while (i < sizeof(gdb_regs) * 2)
6266d855b1dSJason Wessel 		if (hex_to_bin(ptr[i]) >= 0)
6276d855b1dSJason Wessel 			i++;
6286d855b1dSJason Wessel 		else
6296d855b1dSJason Wessel 			break;
6306d855b1dSJason Wessel 	i = i / 2;
6316d855b1dSJason Wessel 	kgdb_hex2mem(ptr, (char *)gdb_regs, i);
63255751145SJason Wessel 	dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
63355751145SJason Wessel 	strcpy(remcom_out_buffer, "OK");
63455751145SJason Wessel }
63555751145SJason Wessel #endif /* DBG_MAX_REG_NUM > 0 */
63655751145SJason Wessel 
63753197fc4SJason Wessel /* Handle the 'X' memory binary write bytes */
gdb_cmd_binwrite(struct kgdb_state * ks)63853197fc4SJason Wessel static void gdb_cmd_binwrite(struct kgdb_state *ks)
63953197fc4SJason Wessel {
64053197fc4SJason Wessel 	int err = write_mem_msg(1);
64153197fc4SJason Wessel 
64253197fc4SJason Wessel 	if (err)
64353197fc4SJason Wessel 		error_packet(remcom_out_buffer, err);
64453197fc4SJason Wessel 	else
64553197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
64653197fc4SJason Wessel }
64753197fc4SJason Wessel 
64853197fc4SJason Wessel /* Handle the 'D' or 'k', detach or kill packets */
gdb_cmd_detachkill(struct kgdb_state * ks)64953197fc4SJason Wessel static void gdb_cmd_detachkill(struct kgdb_state *ks)
65053197fc4SJason Wessel {
65153197fc4SJason Wessel 	int error;
65253197fc4SJason Wessel 
65353197fc4SJason Wessel 	/* The detach case */
65453197fc4SJason Wessel 	if (remcom_in_buffer[0] == 'D') {
65553197fc4SJason Wessel 		error = dbg_remove_all_break();
65653197fc4SJason Wessel 		if (error < 0) {
65753197fc4SJason Wessel 			error_packet(remcom_out_buffer, error);
65853197fc4SJason Wessel 		} else {
65953197fc4SJason Wessel 			strcpy(remcom_out_buffer, "OK");
66053197fc4SJason Wessel 			kgdb_connected = 0;
66153197fc4SJason Wessel 		}
66253197fc4SJason Wessel 		put_packet(remcom_out_buffer);
66353197fc4SJason Wessel 	} else {
66453197fc4SJason Wessel 		/*
66553197fc4SJason Wessel 		 * Assume the kill case, with no exit code checking,
66653197fc4SJason Wessel 		 * trying to force detach the debugger:
66753197fc4SJason Wessel 		 */
66853197fc4SJason Wessel 		dbg_remove_all_break();
66953197fc4SJason Wessel 		kgdb_connected = 0;
67053197fc4SJason Wessel 	}
67153197fc4SJason Wessel }
67253197fc4SJason Wessel 
67353197fc4SJason Wessel /* Handle the 'R' reboot packets */
gdb_cmd_reboot(struct kgdb_state * ks)67453197fc4SJason Wessel static int gdb_cmd_reboot(struct kgdb_state *ks)
67553197fc4SJason Wessel {
67653197fc4SJason Wessel 	/* For now, only honor R0 */
67753197fc4SJason Wessel 	if (strcmp(remcom_in_buffer, "R0") == 0) {
67853197fc4SJason Wessel 		printk(KERN_CRIT "Executing emergency reboot\n");
67953197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
68053197fc4SJason Wessel 		put_packet(remcom_out_buffer);
68153197fc4SJason Wessel 
68253197fc4SJason Wessel 		/*
68353197fc4SJason Wessel 		 * Execution should not return from
68453197fc4SJason Wessel 		 * machine_emergency_restart()
68553197fc4SJason Wessel 		 */
68653197fc4SJason Wessel 		machine_emergency_restart();
68753197fc4SJason Wessel 		kgdb_connected = 0;
68853197fc4SJason Wessel 
68953197fc4SJason Wessel 		return 1;
69053197fc4SJason Wessel 	}
69153197fc4SJason Wessel 	return 0;
69253197fc4SJason Wessel }
69353197fc4SJason Wessel 
69453197fc4SJason Wessel /* Handle the 'q' query packets */
gdb_cmd_query(struct kgdb_state * ks)69553197fc4SJason Wessel static void gdb_cmd_query(struct kgdb_state *ks)
69653197fc4SJason Wessel {
69753197fc4SJason Wessel 	struct task_struct *g;
69853197fc4SJason Wessel 	struct task_struct *p;
69984a0bd5bSJason Wessel 	unsigned char thref[BUF_THREAD_ID_SIZE];
70053197fc4SJason Wessel 	char *ptr;
70153197fc4SJason Wessel 	int i;
70253197fc4SJason Wessel 	int cpu;
70353197fc4SJason Wessel 	int finished = 0;
70453197fc4SJason Wessel 
70553197fc4SJason Wessel 	switch (remcom_in_buffer[1]) {
70653197fc4SJason Wessel 	case 's':
70753197fc4SJason Wessel 	case 'f':
708fb82c0ffSJason Wessel 		if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
70953197fc4SJason Wessel 			break;
71053197fc4SJason Wessel 
71153197fc4SJason Wessel 		i = 0;
71253197fc4SJason Wessel 		remcom_out_buffer[0] = 'm';
71353197fc4SJason Wessel 		ptr = remcom_out_buffer + 1;
71453197fc4SJason Wessel 		if (remcom_in_buffer[1] == 'f') {
71553197fc4SJason Wessel 			/* Each cpu is a shadow thread */
71653197fc4SJason Wessel 			for_each_online_cpu(cpu) {
71753197fc4SJason Wessel 				ks->thr_query = 0;
71853197fc4SJason Wessel 				int_to_threadref(thref, -cpu - 2);
71984a0bd5bSJason Wessel 				ptr = pack_threadid(ptr, thref);
72053197fc4SJason Wessel 				*(ptr++) = ',';
72153197fc4SJason Wessel 				i++;
72253197fc4SJason Wessel 			}
72353197fc4SJason Wessel 		}
72453197fc4SJason Wessel 
725ece4ceafSDavidlohr Bueso 		for_each_process_thread(g, p) {
72653197fc4SJason Wessel 			if (i >= ks->thr_query && !finished) {
72753197fc4SJason Wessel 				int_to_threadref(thref, p->pid);
72884a0bd5bSJason Wessel 				ptr = pack_threadid(ptr, thref);
72953197fc4SJason Wessel 				*(ptr++) = ',';
73053197fc4SJason Wessel 				ks->thr_query++;
73153197fc4SJason Wessel 				if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
73253197fc4SJason Wessel 					finished = 1;
73353197fc4SJason Wessel 			}
73453197fc4SJason Wessel 			i++;
735ece4ceafSDavidlohr Bueso 		}
73653197fc4SJason Wessel 
73753197fc4SJason Wessel 		*(--ptr) = '\0';
73853197fc4SJason Wessel 		break;
73953197fc4SJason Wessel 
74053197fc4SJason Wessel 	case 'C':
74153197fc4SJason Wessel 		/* Current thread id */
74253197fc4SJason Wessel 		strcpy(remcom_out_buffer, "QC");
74353197fc4SJason Wessel 		ks->threadid = shadow_pid(current->pid);
74453197fc4SJason Wessel 		int_to_threadref(thref, ks->threadid);
74553197fc4SJason Wessel 		pack_threadid(remcom_out_buffer + 2, thref);
74653197fc4SJason Wessel 		break;
74753197fc4SJason Wessel 	case 'T':
748fb82c0ffSJason Wessel 		if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
74953197fc4SJason Wessel 			break;
750fb82c0ffSJason Wessel 
75153197fc4SJason Wessel 		ks->threadid = 0;
75253197fc4SJason Wessel 		ptr = remcom_in_buffer + 17;
75353197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
75453197fc4SJason Wessel 		if (!getthread(ks->linux_regs, ks->threadid)) {
75553197fc4SJason Wessel 			error_packet(remcom_out_buffer, -EINVAL);
75653197fc4SJason Wessel 			break;
75753197fc4SJason Wessel 		}
75853197fc4SJason Wessel 		if ((int)ks->threadid > 0) {
75953197fc4SJason Wessel 			kgdb_mem2hex(getthread(ks->linux_regs,
76053197fc4SJason Wessel 					ks->threadid)->comm,
76153197fc4SJason Wessel 					remcom_out_buffer, 16);
76253197fc4SJason Wessel 		} else {
76353197fc4SJason Wessel 			static char tmpstr[23 + BUF_THREAD_ID_SIZE];
76453197fc4SJason Wessel 
76553197fc4SJason Wessel 			sprintf(tmpstr, "shadowCPU%d",
76653197fc4SJason Wessel 					(int)(-ks->threadid - 2));
76753197fc4SJason Wessel 			kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
76853197fc4SJason Wessel 		}
76953197fc4SJason Wessel 		break;
770a0de055cSJason Wessel #ifdef CONFIG_KGDB_KDB
771a0de055cSJason Wessel 	case 'R':
772a0de055cSJason Wessel 		if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
773a0de055cSJason Wessel 			int len = strlen(remcom_in_buffer + 6);
774a0de055cSJason Wessel 
775a0de055cSJason Wessel 			if ((len % 2) != 0) {
776a0de055cSJason Wessel 				strcpy(remcom_out_buffer, "E01");
777a0de055cSJason Wessel 				break;
778a0de055cSJason Wessel 			}
779a0de055cSJason Wessel 			kgdb_hex2mem(remcom_in_buffer + 6,
780a0de055cSJason Wessel 				     remcom_out_buffer, len);
781a0de055cSJason Wessel 			len = len / 2;
782a0de055cSJason Wessel 			remcom_out_buffer[len++] = 0;
783a0de055cSJason Wessel 
78400370b8fSMatt Klein 			kdb_common_init_state(ks);
785a0de055cSJason Wessel 			kdb_parse(remcom_out_buffer);
78600370b8fSMatt Klein 			kdb_common_deinit_state();
78700370b8fSMatt Klein 
788a0de055cSJason Wessel 			strcpy(remcom_out_buffer, "OK");
789a0de055cSJason Wessel 		}
790a0de055cSJason Wessel 		break;
791a0de055cSJason Wessel #endif
7928c080d3aSVincent Chen #ifdef CONFIG_HAVE_ARCH_KGDB_QXFER_PKT
7938c080d3aSVincent Chen 	case 'S':
7948c080d3aSVincent Chen 		if (!strncmp(remcom_in_buffer, "qSupported:", 11))
7958c080d3aSVincent Chen 			strcpy(remcom_out_buffer, kgdb_arch_gdb_stub_feature);
7968c080d3aSVincent Chen 		break;
7978c080d3aSVincent Chen 	case 'X':
7988c080d3aSVincent Chen 		if (!strncmp(remcom_in_buffer, "qXfer:", 6))
7998c080d3aSVincent Chen 			kgdb_arch_handle_qxfer_pkt(remcom_in_buffer,
8008c080d3aSVincent Chen 						   remcom_out_buffer);
8018c080d3aSVincent Chen 		break;
8028c080d3aSVincent Chen #endif
8038c080d3aSVincent Chen 	default:
8048c080d3aSVincent Chen 		break;
80553197fc4SJason Wessel 	}
80653197fc4SJason Wessel }
80753197fc4SJason Wessel 
80853197fc4SJason Wessel /* Handle the 'H' task query packets */
gdb_cmd_task(struct kgdb_state * ks)80953197fc4SJason Wessel static void gdb_cmd_task(struct kgdb_state *ks)
81053197fc4SJason Wessel {
81153197fc4SJason Wessel 	struct task_struct *thread;
81253197fc4SJason Wessel 	char *ptr;
81353197fc4SJason Wessel 
81453197fc4SJason Wessel 	switch (remcom_in_buffer[1]) {
81553197fc4SJason Wessel 	case 'g':
81653197fc4SJason Wessel 		ptr = &remcom_in_buffer[2];
81753197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
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_usethread = thread;
82453197fc4SJason Wessel 		ks->kgdb_usethreadid = ks->threadid;
82553197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
82653197fc4SJason Wessel 		break;
82753197fc4SJason Wessel 	case 'c':
82853197fc4SJason Wessel 		ptr = &remcom_in_buffer[2];
82953197fc4SJason Wessel 		kgdb_hex2long(&ptr, &ks->threadid);
83053197fc4SJason Wessel 		if (!ks->threadid) {
83153197fc4SJason Wessel 			kgdb_contthread = NULL;
83253197fc4SJason Wessel 		} else {
83353197fc4SJason Wessel 			thread = getthread(ks->linux_regs, ks->threadid);
83453197fc4SJason Wessel 			if (!thread && ks->threadid > 0) {
83553197fc4SJason Wessel 				error_packet(remcom_out_buffer, -EINVAL);
83653197fc4SJason Wessel 				break;
83753197fc4SJason Wessel 			}
83853197fc4SJason Wessel 			kgdb_contthread = thread;
83953197fc4SJason Wessel 		}
84053197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
84153197fc4SJason Wessel 		break;
84253197fc4SJason Wessel 	}
84353197fc4SJason Wessel }
84453197fc4SJason Wessel 
84553197fc4SJason Wessel /* Handle the 'T' thread query packets */
gdb_cmd_thread(struct kgdb_state * ks)84653197fc4SJason Wessel static void gdb_cmd_thread(struct kgdb_state *ks)
84753197fc4SJason Wessel {
84853197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[1];
84953197fc4SJason Wessel 	struct task_struct *thread;
85053197fc4SJason Wessel 
85153197fc4SJason Wessel 	kgdb_hex2long(&ptr, &ks->threadid);
85253197fc4SJason Wessel 	thread = getthread(ks->linux_regs, ks->threadid);
85353197fc4SJason Wessel 	if (thread)
85453197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
85553197fc4SJason Wessel 	else
85653197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
85753197fc4SJason Wessel }
85853197fc4SJason Wessel 
85953197fc4SJason Wessel /* Handle the 'z' or 'Z' breakpoint remove or set packets */
gdb_cmd_break(struct kgdb_state * ks)86053197fc4SJason Wessel static void gdb_cmd_break(struct kgdb_state *ks)
86153197fc4SJason Wessel {
86253197fc4SJason Wessel 	/*
86353197fc4SJason Wessel 	 * Since GDB-5.3, it's been drafted that '0' is a software
86453197fc4SJason Wessel 	 * breakpoint, '1' is a hardware breakpoint, so let's do that.
86553197fc4SJason Wessel 	 */
86653197fc4SJason Wessel 	char *bpt_type = &remcom_in_buffer[1];
86753197fc4SJason Wessel 	char *ptr = &remcom_in_buffer[2];
86853197fc4SJason Wessel 	unsigned long addr;
86953197fc4SJason Wessel 	unsigned long length;
87053197fc4SJason Wessel 	int error = 0;
87153197fc4SJason Wessel 
87253197fc4SJason Wessel 	if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
87353197fc4SJason Wessel 		/* Unsupported */
87453197fc4SJason Wessel 		if (*bpt_type > '4')
87553197fc4SJason Wessel 			return;
87653197fc4SJason Wessel 	} else {
87753197fc4SJason Wessel 		if (*bpt_type != '0' && *bpt_type != '1')
87853197fc4SJason Wessel 			/* Unsupported. */
87953197fc4SJason Wessel 			return;
88053197fc4SJason Wessel 	}
88153197fc4SJason Wessel 
88253197fc4SJason Wessel 	/*
88353197fc4SJason Wessel 	 * Test if this is a hardware breakpoint, and
88453197fc4SJason Wessel 	 * if we support it:
88553197fc4SJason Wessel 	 */
88653197fc4SJason Wessel 	if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
88753197fc4SJason Wessel 		/* Unsupported. */
88853197fc4SJason Wessel 		return;
88953197fc4SJason Wessel 
89053197fc4SJason Wessel 	if (*(ptr++) != ',') {
89153197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
89253197fc4SJason Wessel 		return;
89353197fc4SJason Wessel 	}
89453197fc4SJason Wessel 	if (!kgdb_hex2long(&ptr, &addr)) {
89553197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
89653197fc4SJason Wessel 		return;
89753197fc4SJason Wessel 	}
89853197fc4SJason Wessel 	if (*(ptr++) != ',' ||
89953197fc4SJason Wessel 		!kgdb_hex2long(&ptr, &length)) {
90053197fc4SJason Wessel 		error_packet(remcom_out_buffer, -EINVAL);
90153197fc4SJason Wessel 		return;
90253197fc4SJason Wessel 	}
90353197fc4SJason Wessel 
90453197fc4SJason Wessel 	if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
90553197fc4SJason Wessel 		error = dbg_set_sw_break(addr);
90653197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
90753197fc4SJason Wessel 		error = dbg_remove_sw_break(addr);
90853197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'Z')
90953197fc4SJason Wessel 		error = arch_kgdb_ops.set_hw_breakpoint(addr,
91053197fc4SJason Wessel 			(int)length, *bpt_type - '0');
91153197fc4SJason Wessel 	else if (remcom_in_buffer[0] == 'z')
91253197fc4SJason Wessel 		error = arch_kgdb_ops.remove_hw_breakpoint(addr,
91353197fc4SJason Wessel 			(int) length, *bpt_type - '0');
91453197fc4SJason Wessel 
91553197fc4SJason Wessel 	if (error == 0)
91653197fc4SJason Wessel 		strcpy(remcom_out_buffer, "OK");
91753197fc4SJason Wessel 	else
91853197fc4SJason Wessel 		error_packet(remcom_out_buffer, error);
91953197fc4SJason Wessel }
92053197fc4SJason Wessel 
92153197fc4SJason Wessel /* Handle the 'C' signal / exception passing packets */
gdb_cmd_exception_pass(struct kgdb_state * ks)92253197fc4SJason Wessel static int gdb_cmd_exception_pass(struct kgdb_state *ks)
92353197fc4SJason Wessel {
92453197fc4SJason Wessel 	/* C09 == pass exception
92553197fc4SJason Wessel 	 * C15 == detach kgdb, pass exception
92653197fc4SJason Wessel 	 */
92753197fc4SJason Wessel 	if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
92853197fc4SJason Wessel 
92953197fc4SJason Wessel 		ks->pass_exception = 1;
93053197fc4SJason Wessel 		remcom_in_buffer[0] = 'c';
93153197fc4SJason Wessel 
93253197fc4SJason Wessel 	} else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
93353197fc4SJason Wessel 
93453197fc4SJason Wessel 		ks->pass_exception = 1;
93553197fc4SJason Wessel 		remcom_in_buffer[0] = 'D';
93653197fc4SJason Wessel 		dbg_remove_all_break();
93753197fc4SJason Wessel 		kgdb_connected = 0;
93853197fc4SJason Wessel 		return 1;
93953197fc4SJason Wessel 
94053197fc4SJason Wessel 	} else {
94153197fc4SJason Wessel 		gdbstub_msg_write("KGDB only knows signal 9 (pass)"
94253197fc4SJason Wessel 			" and 15 (pass and disconnect)\n"
94353197fc4SJason Wessel 			"Executing a continue without signal passing\n", 0);
94453197fc4SJason Wessel 		remcom_in_buffer[0] = 'c';
94553197fc4SJason Wessel 	}
94653197fc4SJason Wessel 
94753197fc4SJason Wessel 	/* Indicate fall through */
94853197fc4SJason Wessel 	return -1;
94953197fc4SJason Wessel }
95053197fc4SJason Wessel 
95153197fc4SJason Wessel /*
9522bbd9b0fSBhaskar Chowdhury  * This function performs all gdbserial command processing
95353197fc4SJason Wessel  */
gdb_serial_stub(struct kgdb_state * ks)95453197fc4SJason Wessel int gdb_serial_stub(struct kgdb_state *ks)
95553197fc4SJason Wessel {
95653197fc4SJason Wessel 	int error = 0;
95753197fc4SJason Wessel 	int tmp;
95853197fc4SJason Wessel 
95955751145SJason Wessel 	/* Initialize comm buffer and globals. */
96053197fc4SJason Wessel 	memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
96155751145SJason Wessel 	kgdb_usethread = kgdb_info[ks->cpu].task;
96255751145SJason Wessel 	ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
96355751145SJason Wessel 	ks->pass_exception = 0;
96453197fc4SJason Wessel 
96553197fc4SJason Wessel 	if (kgdb_connected) {
96684a0bd5bSJason Wessel 		unsigned char thref[BUF_THREAD_ID_SIZE];
96753197fc4SJason Wessel 		char *ptr;
96853197fc4SJason Wessel 
96953197fc4SJason Wessel 		/* Reply to host that an exception has occurred */
97053197fc4SJason Wessel 		ptr = remcom_out_buffer;
97153197fc4SJason Wessel 		*ptr++ = 'T';
97250e1499fSAndy Shevchenko 		ptr = hex_byte_pack(ptr, ks->signo);
97353197fc4SJason Wessel 		ptr += strlen(strcpy(ptr, "thread:"));
97453197fc4SJason Wessel 		int_to_threadref(thref, shadow_pid(current->pid));
97553197fc4SJason Wessel 		ptr = pack_threadid(ptr, thref);
97653197fc4SJason Wessel 		*ptr++ = ';';
97753197fc4SJason Wessel 		put_packet(remcom_out_buffer);
97853197fc4SJason Wessel 	}
97953197fc4SJason Wessel 
98053197fc4SJason Wessel 	while (1) {
98153197fc4SJason Wessel 		error = 0;
98253197fc4SJason Wessel 
98353197fc4SJason Wessel 		/* Clear the out buffer. */
98453197fc4SJason Wessel 		memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
98553197fc4SJason Wessel 
98653197fc4SJason Wessel 		get_packet(remcom_in_buffer);
98753197fc4SJason Wessel 
98853197fc4SJason Wessel 		switch (remcom_in_buffer[0]) {
98953197fc4SJason Wessel 		case '?': /* gdbserial status */
99053197fc4SJason Wessel 			gdb_cmd_status(ks);
99153197fc4SJason Wessel 			break;
99253197fc4SJason Wessel 		case 'g': /* return the value of the CPU registers */
99353197fc4SJason Wessel 			gdb_cmd_getregs(ks);
99453197fc4SJason Wessel 			break;
99553197fc4SJason Wessel 		case 'G': /* set the value of the CPU registers - return OK */
99653197fc4SJason Wessel 			gdb_cmd_setregs(ks);
99753197fc4SJason Wessel 			break;
99853197fc4SJason Wessel 		case 'm': /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
99953197fc4SJason Wessel 			gdb_cmd_memread(ks);
100053197fc4SJason Wessel 			break;
100153197fc4SJason Wessel 		case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
100253197fc4SJason Wessel 			gdb_cmd_memwrite(ks);
100353197fc4SJason Wessel 			break;
100455751145SJason Wessel #if DBG_MAX_REG_NUM > 0
100555751145SJason Wessel 		case 'p': /* pXX Return gdb register XX (in hex) */
100655751145SJason Wessel 			gdb_cmd_reg_get(ks);
100755751145SJason Wessel 			break;
100855751145SJason Wessel 		case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
100955751145SJason Wessel 			gdb_cmd_reg_set(ks);
101055751145SJason Wessel 			break;
101155751145SJason Wessel #endif /* DBG_MAX_REG_NUM > 0 */
101253197fc4SJason Wessel 		case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
101353197fc4SJason Wessel 			gdb_cmd_binwrite(ks);
101453197fc4SJason Wessel 			break;
101553197fc4SJason Wessel 			/* kill or detach. KGDB should treat this like a
101653197fc4SJason Wessel 			 * continue.
101753197fc4SJason Wessel 			 */
101853197fc4SJason Wessel 		case 'D': /* Debugger detach */
101953197fc4SJason Wessel 		case 'k': /* Debugger detach via kill */
102053197fc4SJason Wessel 			gdb_cmd_detachkill(ks);
102153197fc4SJason Wessel 			goto default_handle;
102253197fc4SJason Wessel 		case 'R': /* Reboot */
102353197fc4SJason Wessel 			if (gdb_cmd_reboot(ks))
102453197fc4SJason Wessel 				goto default_handle;
102553197fc4SJason Wessel 			break;
102653197fc4SJason Wessel 		case 'q': /* query command */
102753197fc4SJason Wessel 			gdb_cmd_query(ks);
102853197fc4SJason Wessel 			break;
102953197fc4SJason Wessel 		case 'H': /* task related */
103053197fc4SJason Wessel 			gdb_cmd_task(ks);
103153197fc4SJason Wessel 			break;
103253197fc4SJason Wessel 		case 'T': /* Query thread status */
103353197fc4SJason Wessel 			gdb_cmd_thread(ks);
103453197fc4SJason Wessel 			break;
103553197fc4SJason Wessel 		case 'z': /* Break point remove */
103653197fc4SJason Wessel 		case 'Z': /* Break point set */
103753197fc4SJason Wessel 			gdb_cmd_break(ks);
103853197fc4SJason Wessel 			break;
1039dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
1040dcc78711SJason Wessel 		case '3': /* Escape into back into kdb */
1041dcc78711SJason Wessel 			if (remcom_in_buffer[1] == '\0') {
1042dcc78711SJason Wessel 				gdb_cmd_detachkill(ks);
1043dcc78711SJason Wessel 				return DBG_PASS_EVENT;
1044dcc78711SJason Wessel 			}
1045df561f66SGustavo A. R. Silva 			fallthrough;
10461adee589SGustavo A. R. Silva #endif
104753197fc4SJason Wessel 		case 'C': /* Exception passing */
104853197fc4SJason Wessel 			tmp = gdb_cmd_exception_pass(ks);
104953197fc4SJason Wessel 			if (tmp > 0)
105053197fc4SJason Wessel 				goto default_handle;
105153197fc4SJason Wessel 			if (tmp == 0)
105253197fc4SJason Wessel 				break;
1053df561f66SGustavo A. R. Silva 			fallthrough;	/* on tmp < 0 */
105453197fc4SJason Wessel 		case 'c': /* Continue packet */
105553197fc4SJason Wessel 		case 's': /* Single step packet */
105653197fc4SJason Wessel 			if (kgdb_contthread && kgdb_contthread != current) {
105753197fc4SJason Wessel 				/* Can't switch threads in kgdb */
105853197fc4SJason Wessel 				error_packet(remcom_out_buffer, -EINVAL);
105953197fc4SJason Wessel 				break;
106053197fc4SJason Wessel 			}
1061df561f66SGustavo A. R. Silva 			fallthrough;	/* to default processing */
106253197fc4SJason Wessel 		default:
106353197fc4SJason Wessel default_handle:
106453197fc4SJason Wessel 			error = kgdb_arch_handle_exception(ks->ex_vector,
106553197fc4SJason Wessel 						ks->signo,
106653197fc4SJason Wessel 						ks->err_code,
106753197fc4SJason Wessel 						remcom_in_buffer,
106853197fc4SJason Wessel 						remcom_out_buffer,
106953197fc4SJason Wessel 						ks->linux_regs);
107053197fc4SJason Wessel 			/*
107153197fc4SJason Wessel 			 * Leave cmd processing on error, detach,
107253197fc4SJason Wessel 			 * kill, continue, or single step.
107353197fc4SJason Wessel 			 */
107453197fc4SJason Wessel 			if (error >= 0 || remcom_in_buffer[0] == 'D' ||
107553197fc4SJason Wessel 			    remcom_in_buffer[0] == 'k') {
107653197fc4SJason Wessel 				error = 0;
107753197fc4SJason Wessel 				goto kgdb_exit;
107853197fc4SJason Wessel 			}
107953197fc4SJason Wessel 
108053197fc4SJason Wessel 		}
108153197fc4SJason Wessel 
108253197fc4SJason Wessel 		/* reply to the request */
108353197fc4SJason Wessel 		put_packet(remcom_out_buffer);
108453197fc4SJason Wessel 	}
108553197fc4SJason Wessel 
108653197fc4SJason Wessel kgdb_exit:
108753197fc4SJason Wessel 	if (ks->pass_exception)
108853197fc4SJason Wessel 		error = 1;
108953197fc4SJason Wessel 	return error;
109053197fc4SJason Wessel }
1091dcc78711SJason Wessel 
gdbstub_state(struct kgdb_state * ks,char * cmd)1092dcc78711SJason Wessel int gdbstub_state(struct kgdb_state *ks, char *cmd)
1093dcc78711SJason Wessel {
1094dcc78711SJason Wessel 	int error;
1095dcc78711SJason Wessel 
1096dcc78711SJason Wessel 	switch (cmd[0]) {
1097dcc78711SJason Wessel 	case 'e':
1098dcc78711SJason Wessel 		error = kgdb_arch_handle_exception(ks->ex_vector,
1099dcc78711SJason Wessel 						   ks->signo,
1100dcc78711SJason Wessel 						   ks->err_code,
1101dcc78711SJason Wessel 						   remcom_in_buffer,
1102dcc78711SJason Wessel 						   remcom_out_buffer,
1103dcc78711SJason Wessel 						   ks->linux_regs);
1104dcc78711SJason Wessel 		return error;
1105dcc78711SJason Wessel 	case 's':
1106dcc78711SJason Wessel 	case 'c':
11074cc168eaSGustavo A. R. Silva 		strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
1108dcc78711SJason Wessel 		return 0;
1109f679c498SJason Wessel 	case '$':
11104cc168eaSGustavo A. R. Silva 		strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
1111f679c498SJason Wessel 		gdbstub_use_prev_in_buf = strlen(remcom_in_buffer);
1112f679c498SJason Wessel 		gdbstub_prev_in_buf_pos = 0;
1113f679c498SJason Wessel 		return 0;
1114dcc78711SJason Wessel 	}
1115dcc78711SJason Wessel 	dbg_io_ops->write_char('+');
1116dcc78711SJason Wessel 	put_packet(remcom_out_buffer);
1117dcc78711SJason Wessel 	return 0;
1118dcc78711SJason Wessel }
1119d57f078bSDavid Howells 
1120d57f078bSDavid Howells /**
1121d57f078bSDavid Howells  * gdbstub_exit - Send an exit message to GDB
1122d57f078bSDavid Howells  * @status: The exit code to report.
1123d57f078bSDavid Howells  */
gdbstub_exit(int status)1124d57f078bSDavid Howells void gdbstub_exit(int status)
1125d57f078bSDavid Howells {
1126d57f078bSDavid Howells 	unsigned char checksum, ch, buffer[3];
1127d57f078bSDavid Howells 	int loop;
1128d57f078bSDavid Howells 
11292366e047SJason Wessel 	if (!kgdb_connected)
11302366e047SJason Wessel 		return;
11312366e047SJason Wessel 	kgdb_connected = 0;
11322366e047SJason Wessel 
11332366e047SJason Wessel 	if (!dbg_io_ops || dbg_kdb_mode)
11342366e047SJason Wessel 		return;
11352366e047SJason Wessel 
1136d57f078bSDavid Howells 	buffer[0] = 'W';
1137d57f078bSDavid Howells 	buffer[1] = hex_asc_hi(status);
1138d57f078bSDavid Howells 	buffer[2] = hex_asc_lo(status);
1139d57f078bSDavid Howells 
1140d57f078bSDavid Howells 	dbg_io_ops->write_char('$');
1141d57f078bSDavid Howells 	checksum = 0;
1142d57f078bSDavid Howells 
1143d57f078bSDavid Howells 	for (loop = 0; loop < 3; loop++) {
1144d57f078bSDavid Howells 		ch = buffer[loop];
1145d57f078bSDavid Howells 		checksum += ch;
1146d57f078bSDavid Howells 		dbg_io_ops->write_char(ch);
1147d57f078bSDavid Howells 	}
1148d57f078bSDavid Howells 
1149d57f078bSDavid Howells 	dbg_io_ops->write_char('#');
1150d57f078bSDavid Howells 	dbg_io_ops->write_char(hex_asc_hi(checksum));
1151d57f078bSDavid Howells 	dbg_io_ops->write_char(hex_asc_lo(checksum));
1152d57f078bSDavid Howells 
1153d57f078bSDavid Howells 	/* make sure the output is flushed, lest the bootloader clobber it */
11549fbe465eSJan Kiszka 	if (dbg_io_ops->flush)
1155d57f078bSDavid Howells 		dbg_io_ops->flush();
1156d57f078bSDavid Howells }
1157