xref: /openbmc/linux/kernel/debug/gdbstub.c (revision f5316b4aea024da9266d740322a5481657f6ce59)
1 /*
2  * Kernel Debug Core
3  *
4  * Maintainer: Jason Wessel <jason.wessel@windriver.com>
5  *
6  * Copyright (C) 2000-2001 VERITAS Software Corporation.
7  * Copyright (C) 2002-2004 Timesys Corporation
8  * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9  * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
10  * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11  * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12  * Copyright (C) 2005-2009 Wind River Systems, Inc.
13  * Copyright (C) 2007 MontaVista Software, Inc.
14  * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
15  *
16  * Contributors at various stages not listed above:
17  *  Jason Wessel ( jason.wessel@windriver.com )
18  *  George Anzinger <george@mvista.com>
19  *  Anurekh Saxena (anurekh.saxena@timesys.com)
20  *  Lake Stevens Instrument Division (Glenn Engel)
21  *  Jim Kingdon, Cygnus Support.
22  *
23  * Original KGDB stub: David Grothe <dave@gcom.com>,
24  * Tigran Aivazian <tigran@sco.com>
25  *
26  * This file is licensed under the terms of the GNU General Public License
27  * version 2. This program is licensed "as is" without any warranty of any
28  * kind, whether express or implied.
29  */
30 
31 #include <linux/kernel.h>
32 #include <linux/kgdb.h>
33 #include <linux/kdb.h>
34 #include <linux/reboot.h>
35 #include <linux/uaccess.h>
36 #include <asm/cacheflush.h>
37 #include <asm/unaligned.h>
38 #include "debug_core.h"
39 
40 #define KGDB_MAX_THREAD_QUERY 17
41 
42 /* Our I/O buffers. */
43 static char			remcom_in_buffer[BUFMAX];
44 static char			remcom_out_buffer[BUFMAX];
45 
46 /* Storage for the registers, in GDB format. */
47 static unsigned long		gdb_regs[(NUMREGBYTES +
48 					sizeof(unsigned long) - 1) /
49 					sizeof(unsigned long)];
50 
51 /*
52  * GDB remote protocol parser:
53  */
54 
55 static int hex(char ch)
56 {
57 	if ((ch >= 'a') && (ch <= 'f'))
58 		return ch - 'a' + 10;
59 	if ((ch >= '0') && (ch <= '9'))
60 		return ch - '0';
61 	if ((ch >= 'A') && (ch <= 'F'))
62 		return ch - 'A' + 10;
63 	return -1;
64 }
65 
66 #ifdef CONFIG_KGDB_KDB
67 static int gdbstub_read_wait(void)
68 {
69 	int ret = -1;
70 	int i;
71 
72 	/* poll any additional I/O interfaces that are defined */
73 	while (ret < 0)
74 		for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
75 			ret = kdb_poll_funcs[i]();
76 			if (ret > 0)
77 				break;
78 		}
79 	return ret;
80 }
81 #else
82 static int gdbstub_read_wait(void)
83 {
84 	int ret = dbg_io_ops->read_char();
85 	while (ret == NO_POLL_CHAR)
86 		ret = dbg_io_ops->read_char();
87 	return ret;
88 }
89 #endif
90 /* scan for the sequence $<data>#<checksum> */
91 static void get_packet(char *buffer)
92 {
93 	unsigned char checksum;
94 	unsigned char xmitcsum;
95 	int count;
96 	char ch;
97 
98 	do {
99 		/*
100 		 * Spin and wait around for the start character, ignore all
101 		 * other characters:
102 		 */
103 		while ((ch = (gdbstub_read_wait())) != '$')
104 			/* nothing */;
105 
106 		kgdb_connected = 1;
107 		checksum = 0;
108 		xmitcsum = -1;
109 
110 		count = 0;
111 
112 		/*
113 		 * now, read until a # or end of buffer is found:
114 		 */
115 		while (count < (BUFMAX - 1)) {
116 			ch = gdbstub_read_wait();
117 			if (ch == '#')
118 				break;
119 			checksum = checksum + ch;
120 			buffer[count] = ch;
121 			count = count + 1;
122 		}
123 		buffer[count] = 0;
124 
125 		if (ch == '#') {
126 			xmitcsum = hex(gdbstub_read_wait()) << 4;
127 			xmitcsum += hex(gdbstub_read_wait());
128 
129 			if (checksum != xmitcsum)
130 				/* failed checksum */
131 				dbg_io_ops->write_char('-');
132 			else
133 				/* successful transfer */
134 				dbg_io_ops->write_char('+');
135 			if (dbg_io_ops->flush)
136 				dbg_io_ops->flush();
137 		}
138 	} while (checksum != xmitcsum);
139 }
140 
141 /*
142  * Send the packet in buffer.
143  * Check for gdb connection if asked for.
144  */
145 static void put_packet(char *buffer)
146 {
147 	unsigned char checksum;
148 	int count;
149 	char ch;
150 
151 	/*
152 	 * $<packet info>#<checksum>.
153 	 */
154 	while (1) {
155 		dbg_io_ops->write_char('$');
156 		checksum = 0;
157 		count = 0;
158 
159 		while ((ch = buffer[count])) {
160 			dbg_io_ops->write_char(ch);
161 			checksum += ch;
162 			count++;
163 		}
164 
165 		dbg_io_ops->write_char('#');
166 		dbg_io_ops->write_char(hex_asc_hi(checksum));
167 		dbg_io_ops->write_char(hex_asc_lo(checksum));
168 		if (dbg_io_ops->flush)
169 			dbg_io_ops->flush();
170 
171 		/* Now see what we get in reply. */
172 		ch = gdbstub_read_wait();
173 
174 		if (ch == 3)
175 			ch = gdbstub_read_wait();
176 
177 		/* If we get an ACK, we are done. */
178 		if (ch == '+')
179 			return;
180 
181 		/*
182 		 * If we get the start of another packet, this means
183 		 * that GDB is attempting to reconnect.  We will NAK
184 		 * the packet being sent, and stop trying to send this
185 		 * packet.
186 		 */
187 		if (ch == '$') {
188 			dbg_io_ops->write_char('-');
189 			if (dbg_io_ops->flush)
190 				dbg_io_ops->flush();
191 			return;
192 		}
193 	}
194 }
195 
196 static char gdbmsgbuf[BUFMAX + 1];
197 
198 void gdbstub_msg_write(const char *s, int len)
199 {
200 	char *bufptr;
201 	int wcount;
202 	int i;
203 
204 	/* 'O'utput */
205 	gdbmsgbuf[0] = 'O';
206 
207 	/* Fill and send buffers... */
208 	while (len > 0) {
209 		bufptr = gdbmsgbuf + 1;
210 
211 		/* Calculate how many this time */
212 		if ((len << 1) > (BUFMAX - 2))
213 			wcount = (BUFMAX - 2) >> 1;
214 		else
215 			wcount = len;
216 
217 		/* Pack in hex chars */
218 		for (i = 0; i < wcount; i++)
219 			bufptr = pack_hex_byte(bufptr, s[i]);
220 		*bufptr = '\0';
221 
222 		/* Move up */
223 		s += wcount;
224 		len -= wcount;
225 
226 		/* Write packet */
227 		put_packet(gdbmsgbuf);
228 	}
229 }
230 
231 /*
232  * Convert the memory pointed to by mem into hex, placing result in
233  * buf.  Return a pointer to the last char put in buf (null). May
234  * return an error.
235  */
236 int kgdb_mem2hex(char *mem, char *buf, int count)
237 {
238 	char *tmp;
239 	int err;
240 
241 	/*
242 	 * We use the upper half of buf as an intermediate buffer for the
243 	 * raw memory copy.  Hex conversion will work against this one.
244 	 */
245 	tmp = buf + count;
246 
247 	err = probe_kernel_read(tmp, mem, count);
248 	if (!err) {
249 		while (count > 0) {
250 			buf = pack_hex_byte(buf, *tmp);
251 			tmp++;
252 			count--;
253 		}
254 
255 		*buf = 0;
256 	}
257 
258 	return err;
259 }
260 
261 /*
262  * Convert the hex array pointed to by buf into binary to be placed in
263  * mem.  Return a pointer to the character AFTER the last byte
264  * written.  May return an error.
265  */
266 int kgdb_hex2mem(char *buf, char *mem, int count)
267 {
268 	char *tmp_raw;
269 	char *tmp_hex;
270 
271 	/*
272 	 * We use the upper half of buf as an intermediate buffer for the
273 	 * raw memory that is converted from hex.
274 	 */
275 	tmp_raw = buf + count * 2;
276 
277 	tmp_hex = tmp_raw - 1;
278 	while (tmp_hex >= buf) {
279 		tmp_raw--;
280 		*tmp_raw = hex(*tmp_hex--);
281 		*tmp_raw |= hex(*tmp_hex--) << 4;
282 	}
283 
284 	return probe_kernel_write(mem, tmp_raw, count);
285 }
286 
287 /*
288  * While we find nice hex chars, build a long_val.
289  * Return number of chars processed.
290  */
291 int kgdb_hex2long(char **ptr, unsigned long *long_val)
292 {
293 	int hex_val;
294 	int num = 0;
295 	int negate = 0;
296 
297 	*long_val = 0;
298 
299 	if (**ptr == '-') {
300 		negate = 1;
301 		(*ptr)++;
302 	}
303 	while (**ptr) {
304 		hex_val = hex(**ptr);
305 		if (hex_val < 0)
306 			break;
307 
308 		*long_val = (*long_val << 4) | hex_val;
309 		num++;
310 		(*ptr)++;
311 	}
312 
313 	if (negate)
314 		*long_val = -*long_val;
315 
316 	return num;
317 }
318 
319 /*
320  * Copy the binary array pointed to by buf into mem.  Fix $, #, and
321  * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
322  * The input buf is overwitten with the result to write to mem.
323  */
324 static int kgdb_ebin2mem(char *buf, char *mem, int count)
325 {
326 	int size = 0;
327 	char *c = buf;
328 
329 	while (count-- > 0) {
330 		c[size] = *buf++;
331 		if (c[size] == 0x7d)
332 			c[size] = *buf++ ^ 0x20;
333 		size++;
334 	}
335 
336 	return probe_kernel_write(mem, c, size);
337 }
338 
339 /* Write memory due to an 'M' or 'X' packet. */
340 static int write_mem_msg(int binary)
341 {
342 	char *ptr = &remcom_in_buffer[1];
343 	unsigned long addr;
344 	unsigned long length;
345 	int err;
346 
347 	if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
348 	    kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
349 		if (binary)
350 			err = kgdb_ebin2mem(ptr, (char *)addr, length);
351 		else
352 			err = kgdb_hex2mem(ptr, (char *)addr, length);
353 		if (err)
354 			return err;
355 		if (CACHE_FLUSH_IS_SAFE)
356 			flush_icache_range(addr, addr + length);
357 		return 0;
358 	}
359 
360 	return -EINVAL;
361 }
362 
363 static void error_packet(char *pkt, int error)
364 {
365 	error = -error;
366 	pkt[0] = 'E';
367 	pkt[1] = hex_asc[(error / 10)];
368 	pkt[2] = hex_asc[(error % 10)];
369 	pkt[3] = '\0';
370 }
371 
372 /*
373  * Thread ID accessors. We represent a flat TID space to GDB, where
374  * the per CPU idle threads (which under Linux all have PID 0) are
375  * remapped to negative TIDs.
376  */
377 
378 #define BUF_THREAD_ID_SIZE	16
379 
380 static char *pack_threadid(char *pkt, unsigned char *id)
381 {
382 	char *limit;
383 
384 	limit = pkt + BUF_THREAD_ID_SIZE;
385 	while (pkt < limit)
386 		pkt = pack_hex_byte(pkt, *id++);
387 
388 	return pkt;
389 }
390 
391 static void int_to_threadref(unsigned char *id, int value)
392 {
393 	unsigned char *scan;
394 	int i = 4;
395 
396 	scan = (unsigned char *)id;
397 	while (i--)
398 		*scan++ = 0;
399 	put_unaligned_be32(value, scan);
400 }
401 
402 static struct task_struct *getthread(struct pt_regs *regs, int tid)
403 {
404 	/*
405 	 * Non-positive TIDs are remapped to the cpu shadow information
406 	 */
407 	if (tid == 0 || tid == -1)
408 		tid = -atomic_read(&kgdb_active) - 2;
409 	if (tid < -1 && tid > -NR_CPUS - 2) {
410 		if (kgdb_info[-tid - 2].task)
411 			return kgdb_info[-tid - 2].task;
412 		else
413 			return idle_task(-tid - 2);
414 	}
415 	if (tid <= 0) {
416 		printk(KERN_ERR "KGDB: Internal thread select error\n");
417 		dump_stack();
418 		return NULL;
419 	}
420 
421 	/*
422 	 * find_task_by_pid_ns() does not take the tasklist lock anymore
423 	 * but is nicely RCU locked - hence is a pretty resilient
424 	 * thing to use:
425 	 */
426 	return find_task_by_pid_ns(tid, &init_pid_ns);
427 }
428 
429 
430 /*
431  * Remap normal tasks to their real PID,
432  * CPU shadow threads are mapped to -CPU - 2
433  */
434 static inline int shadow_pid(int realpid)
435 {
436 	if (realpid)
437 		return realpid;
438 
439 	return -raw_smp_processor_id() - 2;
440 }
441 
442 /*
443  * All the functions that start with gdb_cmd are the various
444  * operations to implement the handlers for the gdbserial protocol
445  * where KGDB is communicating with an external debugger
446  */
447 
448 /* Handle the '?' status packets */
449 static void gdb_cmd_status(struct kgdb_state *ks)
450 {
451 	/*
452 	 * We know that this packet is only sent
453 	 * during initial connect.  So to be safe,
454 	 * we clear out our breakpoints now in case
455 	 * GDB is reconnecting.
456 	 */
457 	dbg_remove_all_break();
458 
459 	remcom_out_buffer[0] = 'S';
460 	pack_hex_byte(&remcom_out_buffer[1], ks->signo);
461 }
462 
463 /* Handle the 'g' get registers request */
464 static void gdb_cmd_getregs(struct kgdb_state *ks)
465 {
466 	struct task_struct *thread;
467 	void *local_debuggerinfo;
468 	int i;
469 
470 	thread = kgdb_usethread;
471 	if (!thread) {
472 		thread = kgdb_info[ks->cpu].task;
473 		local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
474 	} else {
475 		local_debuggerinfo = NULL;
476 		for_each_online_cpu(i) {
477 			/*
478 			 * Try to find the task on some other
479 			 * or possibly this node if we do not
480 			 * find the matching task then we try
481 			 * to approximate the results.
482 			 */
483 			if (thread == kgdb_info[i].task)
484 				local_debuggerinfo = kgdb_info[i].debuggerinfo;
485 		}
486 	}
487 
488 	/*
489 	 * All threads that don't have debuggerinfo should be
490 	 * in schedule() sleeping, since all other CPUs
491 	 * are in kgdb_wait, and thus have debuggerinfo.
492 	 */
493 	if (local_debuggerinfo) {
494 		pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
495 	} else {
496 		/*
497 		 * Pull stuff saved during switch_to; nothing
498 		 * else is accessible (or even particularly
499 		 * relevant).
500 		 *
501 		 * This should be enough for a stack trace.
502 		 */
503 		sleeping_thread_to_gdb_regs(gdb_regs, thread);
504 	}
505 	kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
506 }
507 
508 /* Handle the 'G' set registers request */
509 static void gdb_cmd_setregs(struct kgdb_state *ks)
510 {
511 	kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
512 
513 	if (kgdb_usethread && kgdb_usethread != current) {
514 		error_packet(remcom_out_buffer, -EINVAL);
515 	} else {
516 		gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
517 		strcpy(remcom_out_buffer, "OK");
518 	}
519 }
520 
521 /* Handle the 'm' memory read bytes */
522 static void gdb_cmd_memread(struct kgdb_state *ks)
523 {
524 	char *ptr = &remcom_in_buffer[1];
525 	unsigned long length;
526 	unsigned long addr;
527 	int err;
528 
529 	if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
530 					kgdb_hex2long(&ptr, &length) > 0) {
531 		err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
532 		if (err)
533 			error_packet(remcom_out_buffer, err);
534 	} else {
535 		error_packet(remcom_out_buffer, -EINVAL);
536 	}
537 }
538 
539 /* Handle the 'M' memory write bytes */
540 static void gdb_cmd_memwrite(struct kgdb_state *ks)
541 {
542 	int err = write_mem_msg(0);
543 
544 	if (err)
545 		error_packet(remcom_out_buffer, err);
546 	else
547 		strcpy(remcom_out_buffer, "OK");
548 }
549 
550 /* Handle the 'X' memory binary write bytes */
551 static void gdb_cmd_binwrite(struct kgdb_state *ks)
552 {
553 	int err = write_mem_msg(1);
554 
555 	if (err)
556 		error_packet(remcom_out_buffer, err);
557 	else
558 		strcpy(remcom_out_buffer, "OK");
559 }
560 
561 /* Handle the 'D' or 'k', detach or kill packets */
562 static void gdb_cmd_detachkill(struct kgdb_state *ks)
563 {
564 	int error;
565 
566 	/* The detach case */
567 	if (remcom_in_buffer[0] == 'D') {
568 		error = dbg_remove_all_break();
569 		if (error < 0) {
570 			error_packet(remcom_out_buffer, error);
571 		} else {
572 			strcpy(remcom_out_buffer, "OK");
573 			kgdb_connected = 0;
574 		}
575 		put_packet(remcom_out_buffer);
576 	} else {
577 		/*
578 		 * Assume the kill case, with no exit code checking,
579 		 * trying to force detach the debugger:
580 		 */
581 		dbg_remove_all_break();
582 		kgdb_connected = 0;
583 	}
584 }
585 
586 /* Handle the 'R' reboot packets */
587 static int gdb_cmd_reboot(struct kgdb_state *ks)
588 {
589 	/* For now, only honor R0 */
590 	if (strcmp(remcom_in_buffer, "R0") == 0) {
591 		printk(KERN_CRIT "Executing emergency reboot\n");
592 		strcpy(remcom_out_buffer, "OK");
593 		put_packet(remcom_out_buffer);
594 
595 		/*
596 		 * Execution should not return from
597 		 * machine_emergency_restart()
598 		 */
599 		machine_emergency_restart();
600 		kgdb_connected = 0;
601 
602 		return 1;
603 	}
604 	return 0;
605 }
606 
607 /* Handle the 'q' query packets */
608 static void gdb_cmd_query(struct kgdb_state *ks)
609 {
610 	struct task_struct *g;
611 	struct task_struct *p;
612 	unsigned char thref[8];
613 	char *ptr;
614 	int i;
615 	int cpu;
616 	int finished = 0;
617 
618 	switch (remcom_in_buffer[1]) {
619 	case 's':
620 	case 'f':
621 		if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
622 			error_packet(remcom_out_buffer, -EINVAL);
623 			break;
624 		}
625 
626 		i = 0;
627 		remcom_out_buffer[0] = 'm';
628 		ptr = remcom_out_buffer + 1;
629 		if (remcom_in_buffer[1] == 'f') {
630 			/* Each cpu is a shadow thread */
631 			for_each_online_cpu(cpu) {
632 				ks->thr_query = 0;
633 				int_to_threadref(thref, -cpu - 2);
634 				pack_threadid(ptr, thref);
635 				ptr += BUF_THREAD_ID_SIZE;
636 				*(ptr++) = ',';
637 				i++;
638 			}
639 		}
640 
641 		do_each_thread(g, p) {
642 			if (i >= ks->thr_query && !finished) {
643 				int_to_threadref(thref, p->pid);
644 				pack_threadid(ptr, thref);
645 				ptr += BUF_THREAD_ID_SIZE;
646 				*(ptr++) = ',';
647 				ks->thr_query++;
648 				if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
649 					finished = 1;
650 			}
651 			i++;
652 		} while_each_thread(g, p);
653 
654 		*(--ptr) = '\0';
655 		break;
656 
657 	case 'C':
658 		/* Current thread id */
659 		strcpy(remcom_out_buffer, "QC");
660 		ks->threadid = shadow_pid(current->pid);
661 		int_to_threadref(thref, ks->threadid);
662 		pack_threadid(remcom_out_buffer + 2, thref);
663 		break;
664 	case 'T':
665 		if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
666 			error_packet(remcom_out_buffer, -EINVAL);
667 			break;
668 		}
669 		ks->threadid = 0;
670 		ptr = remcom_in_buffer + 17;
671 		kgdb_hex2long(&ptr, &ks->threadid);
672 		if (!getthread(ks->linux_regs, ks->threadid)) {
673 			error_packet(remcom_out_buffer, -EINVAL);
674 			break;
675 		}
676 		if ((int)ks->threadid > 0) {
677 			kgdb_mem2hex(getthread(ks->linux_regs,
678 					ks->threadid)->comm,
679 					remcom_out_buffer, 16);
680 		} else {
681 			static char tmpstr[23 + BUF_THREAD_ID_SIZE];
682 
683 			sprintf(tmpstr, "shadowCPU%d",
684 					(int)(-ks->threadid - 2));
685 			kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
686 		}
687 		break;
688 	}
689 }
690 
691 /* Handle the 'H' task query packets */
692 static void gdb_cmd_task(struct kgdb_state *ks)
693 {
694 	struct task_struct *thread;
695 	char *ptr;
696 
697 	switch (remcom_in_buffer[1]) {
698 	case 'g':
699 		ptr = &remcom_in_buffer[2];
700 		kgdb_hex2long(&ptr, &ks->threadid);
701 		thread = getthread(ks->linux_regs, ks->threadid);
702 		if (!thread && ks->threadid > 0) {
703 			error_packet(remcom_out_buffer, -EINVAL);
704 			break;
705 		}
706 		kgdb_usethread = thread;
707 		ks->kgdb_usethreadid = ks->threadid;
708 		strcpy(remcom_out_buffer, "OK");
709 		break;
710 	case 'c':
711 		ptr = &remcom_in_buffer[2];
712 		kgdb_hex2long(&ptr, &ks->threadid);
713 		if (!ks->threadid) {
714 			kgdb_contthread = NULL;
715 		} else {
716 			thread = getthread(ks->linux_regs, ks->threadid);
717 			if (!thread && ks->threadid > 0) {
718 				error_packet(remcom_out_buffer, -EINVAL);
719 				break;
720 			}
721 			kgdb_contthread = thread;
722 		}
723 		strcpy(remcom_out_buffer, "OK");
724 		break;
725 	}
726 }
727 
728 /* Handle the 'T' thread query packets */
729 static void gdb_cmd_thread(struct kgdb_state *ks)
730 {
731 	char *ptr = &remcom_in_buffer[1];
732 	struct task_struct *thread;
733 
734 	kgdb_hex2long(&ptr, &ks->threadid);
735 	thread = getthread(ks->linux_regs, ks->threadid);
736 	if (thread)
737 		strcpy(remcom_out_buffer, "OK");
738 	else
739 		error_packet(remcom_out_buffer, -EINVAL);
740 }
741 
742 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
743 static void gdb_cmd_break(struct kgdb_state *ks)
744 {
745 	/*
746 	 * Since GDB-5.3, it's been drafted that '0' is a software
747 	 * breakpoint, '1' is a hardware breakpoint, so let's do that.
748 	 */
749 	char *bpt_type = &remcom_in_buffer[1];
750 	char *ptr = &remcom_in_buffer[2];
751 	unsigned long addr;
752 	unsigned long length;
753 	int error = 0;
754 
755 	if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
756 		/* Unsupported */
757 		if (*bpt_type > '4')
758 			return;
759 	} else {
760 		if (*bpt_type != '0' && *bpt_type != '1')
761 			/* Unsupported. */
762 			return;
763 	}
764 
765 	/*
766 	 * Test if this is a hardware breakpoint, and
767 	 * if we support it:
768 	 */
769 	if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
770 		/* Unsupported. */
771 		return;
772 
773 	if (*(ptr++) != ',') {
774 		error_packet(remcom_out_buffer, -EINVAL);
775 		return;
776 	}
777 	if (!kgdb_hex2long(&ptr, &addr)) {
778 		error_packet(remcom_out_buffer, -EINVAL);
779 		return;
780 	}
781 	if (*(ptr++) != ',' ||
782 		!kgdb_hex2long(&ptr, &length)) {
783 		error_packet(remcom_out_buffer, -EINVAL);
784 		return;
785 	}
786 
787 	if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
788 		error = dbg_set_sw_break(addr);
789 	else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
790 		error = dbg_remove_sw_break(addr);
791 	else if (remcom_in_buffer[0] == 'Z')
792 		error = arch_kgdb_ops.set_hw_breakpoint(addr,
793 			(int)length, *bpt_type - '0');
794 	else if (remcom_in_buffer[0] == 'z')
795 		error = arch_kgdb_ops.remove_hw_breakpoint(addr,
796 			(int) length, *bpt_type - '0');
797 
798 	if (error == 0)
799 		strcpy(remcom_out_buffer, "OK");
800 	else
801 		error_packet(remcom_out_buffer, error);
802 }
803 
804 /* Handle the 'C' signal / exception passing packets */
805 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
806 {
807 	/* C09 == pass exception
808 	 * C15 == detach kgdb, pass exception
809 	 */
810 	if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
811 
812 		ks->pass_exception = 1;
813 		remcom_in_buffer[0] = 'c';
814 
815 	} else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
816 
817 		ks->pass_exception = 1;
818 		remcom_in_buffer[0] = 'D';
819 		dbg_remove_all_break();
820 		kgdb_connected = 0;
821 		return 1;
822 
823 	} else {
824 		gdbstub_msg_write("KGDB only knows signal 9 (pass)"
825 			" and 15 (pass and disconnect)\n"
826 			"Executing a continue without signal passing\n", 0);
827 		remcom_in_buffer[0] = 'c';
828 	}
829 
830 	/* Indicate fall through */
831 	return -1;
832 }
833 
834 /*
835  * This function performs all gdbserial command procesing
836  */
837 int gdb_serial_stub(struct kgdb_state *ks)
838 {
839 	int error = 0;
840 	int tmp;
841 
842 	/* Clear the out buffer. */
843 	memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
844 
845 	if (kgdb_connected) {
846 		unsigned char thref[8];
847 		char *ptr;
848 
849 		/* Reply to host that an exception has occurred */
850 		ptr = remcom_out_buffer;
851 		*ptr++ = 'T';
852 		ptr = pack_hex_byte(ptr, ks->signo);
853 		ptr += strlen(strcpy(ptr, "thread:"));
854 		int_to_threadref(thref, shadow_pid(current->pid));
855 		ptr = pack_threadid(ptr, thref);
856 		*ptr++ = ';';
857 		put_packet(remcom_out_buffer);
858 	}
859 
860 	kgdb_usethread = kgdb_info[ks->cpu].task;
861 	ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
862 	ks->pass_exception = 0;
863 
864 	while (1) {
865 		error = 0;
866 
867 		/* Clear the out buffer. */
868 		memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
869 
870 		get_packet(remcom_in_buffer);
871 
872 		switch (remcom_in_buffer[0]) {
873 		case '?': /* gdbserial status */
874 			gdb_cmd_status(ks);
875 			break;
876 		case 'g': /* return the value of the CPU registers */
877 			gdb_cmd_getregs(ks);
878 			break;
879 		case 'G': /* set the value of the CPU registers - return OK */
880 			gdb_cmd_setregs(ks);
881 			break;
882 		case 'm': /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
883 			gdb_cmd_memread(ks);
884 			break;
885 		case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
886 			gdb_cmd_memwrite(ks);
887 			break;
888 		case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
889 			gdb_cmd_binwrite(ks);
890 			break;
891 			/* kill or detach. KGDB should treat this like a
892 			 * continue.
893 			 */
894 		case 'D': /* Debugger detach */
895 		case 'k': /* Debugger detach via kill */
896 			gdb_cmd_detachkill(ks);
897 			goto default_handle;
898 		case 'R': /* Reboot */
899 			if (gdb_cmd_reboot(ks))
900 				goto default_handle;
901 			break;
902 		case 'q': /* query command */
903 			gdb_cmd_query(ks);
904 			break;
905 		case 'H': /* task related */
906 			gdb_cmd_task(ks);
907 			break;
908 		case 'T': /* Query thread status */
909 			gdb_cmd_thread(ks);
910 			break;
911 		case 'z': /* Break point remove */
912 		case 'Z': /* Break point set */
913 			gdb_cmd_break(ks);
914 			break;
915 #ifdef CONFIG_KGDB_KDB
916 		case '3': /* Escape into back into kdb */
917 			if (remcom_in_buffer[1] == '\0') {
918 				gdb_cmd_detachkill(ks);
919 				return DBG_PASS_EVENT;
920 			}
921 #endif
922 		case 'C': /* Exception passing */
923 			tmp = gdb_cmd_exception_pass(ks);
924 			if (tmp > 0)
925 				goto default_handle;
926 			if (tmp == 0)
927 				break;
928 			/* Fall through on tmp < 0 */
929 		case 'c': /* Continue packet */
930 		case 's': /* Single step packet */
931 			if (kgdb_contthread && kgdb_contthread != current) {
932 				/* Can't switch threads in kgdb */
933 				error_packet(remcom_out_buffer, -EINVAL);
934 				break;
935 			}
936 			dbg_activate_sw_breakpoints();
937 			/* Fall through to default processing */
938 		default:
939 default_handle:
940 			error = kgdb_arch_handle_exception(ks->ex_vector,
941 						ks->signo,
942 						ks->err_code,
943 						remcom_in_buffer,
944 						remcom_out_buffer,
945 						ks->linux_regs);
946 			/*
947 			 * Leave cmd processing on error, detach,
948 			 * kill, continue, or single step.
949 			 */
950 			if (error >= 0 || remcom_in_buffer[0] == 'D' ||
951 			    remcom_in_buffer[0] == 'k') {
952 				error = 0;
953 				goto kgdb_exit;
954 			}
955 
956 		}
957 
958 		/* reply to the request */
959 		put_packet(remcom_out_buffer);
960 	}
961 
962 kgdb_exit:
963 	if (ks->pass_exception)
964 		error = 1;
965 	return error;
966 }
967 
968 int gdbstub_state(struct kgdb_state *ks, char *cmd)
969 {
970 	int error;
971 
972 	switch (cmd[0]) {
973 	case 'e':
974 		error = kgdb_arch_handle_exception(ks->ex_vector,
975 						   ks->signo,
976 						   ks->err_code,
977 						   remcom_in_buffer,
978 						   remcom_out_buffer,
979 						   ks->linux_regs);
980 		return error;
981 	case 's':
982 	case 'c':
983 		strcpy(remcom_in_buffer, cmd);
984 		return 0;
985 	case '?':
986 		gdb_cmd_status(ks);
987 		break;
988 	case '\0':
989 		strcpy(remcom_out_buffer, "");
990 		break;
991 	}
992 	dbg_io_ops->write_char('+');
993 	put_packet(remcom_out_buffer);
994 	return 0;
995 }
996