aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorJason Wessel <jason.wessel@windriver.com>2010-04-02 12:48:03 -0400
committerJason Wessel <jason.wessel@windriver.com>2010-05-20 22:04:19 -0400
commit53197fc49549240f6c6a963b2713a4cd9517964b (patch)
tree5a89c338ef8acd188e4aa1503370a4e928c33edd /kernel
parentc433820971ffa854feda6adc17f5f24201354f11 (diff)
Separate the gdbstub from the debug core
Split the former kernel/kgdb.c into debug_core.c which contains the kernel debugger exception logic and to the gdbstub.c which contains the logic for allowing gdb to talk to the debug core. This also created a private include file called debug_core.h which contains all the definitions to glue the debug_core to any other debugger connections. CC: Ingo Molnar <mingo@elte.hu> Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/debug/Makefile3
-rw-r--r--kernel/debug/debug_core.c994
-rw-r--r--kernel/debug/debug_core.h55
-rw-r--r--kernel/debug/gdbstub.c934
4 files changed, 1029 insertions, 957 deletions
diff --git a/kernel/debug/Makefile b/kernel/debug/Makefile
index 5d7850415266..fd4dc6e7782c 100644
--- a/kernel/debug/Makefile
+++ b/kernel/debug/Makefile
@@ -2,5 +2,4 @@
2# Makefile for the linux kernel debugger 2# Makefile for the linux kernel debugger
3# 3#
4 4
5obj-$(CONFIG_KGDB) += debug_core.o 5obj-$(CONFIG_KGDB) += debug_core.o gdbstub.o
6
diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c
index 11f3515ca83f..7e03969330bc 100644
--- a/kernel/debug/debug_core.c
+++ b/kernel/debug/debug_core.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * KGDB stub. 2 * Kernel Debug Core
3 * 3 *
4 * Maintainer: Jason Wessel <jason.wessel@windriver.com> 4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
5 * 5 *
@@ -9,7 +9,7 @@
9 * Copyright (C) 2004 Pavel Machek <pavel@suse.cz> 9 * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> 10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. 11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2008 Wind River Systems, Inc. 12 * Copyright (C) 2005-2009 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc. 13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
15 * 15 *
@@ -37,7 +37,6 @@
37#include <linux/kernel.h> 37#include <linux/kernel.h>
38#include <linux/module.h> 38#include <linux/module.h>
39#include <linux/ptrace.h> 39#include <linux/ptrace.h>
40#include <linux/reboot.h>
41#include <linux/string.h> 40#include <linux/string.h>
42#include <linux/delay.h> 41#include <linux/delay.h>
43#include <linux/sched.h> 42#include <linux/sched.h>
@@ -52,34 +51,12 @@
52#include <asm/byteorder.h> 51#include <asm/byteorder.h>
53#include <asm/atomic.h> 52#include <asm/atomic.h>
54#include <asm/system.h> 53#include <asm/system.h>
55#include <asm/unaligned.h>
56 54
57static int kgdb_break_asap; 55#include "debug_core.h"
58
59#define KGDB_MAX_THREAD_QUERY 17
60struct kgdb_state {
61 int ex_vector;
62 int signo;
63 int err_code;
64 int cpu;
65 int pass_exception;
66 unsigned long thr_query;
67 unsigned long threadid;
68 long kgdb_usethreadid;
69 struct pt_regs *linux_regs;
70};
71 56
72/* Exception state values */ 57static int kgdb_break_asap;
73#define DCPU_WANT_MASTER 0x1 /* Waiting to become a master kgdb cpu */
74#define DCPU_NEXT_MASTER 0x2 /* Transition from one master cpu to another */
75#define DCPU_IS_SLAVE 0x4 /* Slave cpu enter exception */
76#define DCPU_SSTEP 0x8 /* CPU is single stepping */
77 58
78static struct debuggerinfo_struct { 59struct debuggerinfo_struct kgdb_info[NR_CPUS];
79 void *debuggerinfo;
80 struct task_struct *task;
81 int exception_state;
82} kgdb_info[NR_CPUS];
83 60
84/** 61/**
85 * kgdb_connected - Is a host GDB connected to us? 62 * kgdb_connected - Is a host GDB connected to us?
@@ -93,7 +70,7 @@ static int kgdb_io_module_registered;
93/* Guard for recursive entry */ 70/* Guard for recursive entry */
94static int exception_level; 71static int exception_level;
95 72
96static struct kgdb_io *kgdb_io_ops; 73struct kgdb_io *dbg_io_ops;
97static DEFINE_SPINLOCK(kgdb_registration_lock); 74static DEFINE_SPINLOCK(kgdb_registration_lock);
98 75
99/* kgdb console driver is loaded */ 76/* kgdb console driver is loaded */
@@ -136,16 +113,7 @@ struct task_struct *kgdb_usethread;
136struct task_struct *kgdb_contthread; 113struct task_struct *kgdb_contthread;
137 114
138int kgdb_single_step; 115int kgdb_single_step;
139pid_t kgdb_sstep_pid; 116static pid_t kgdb_sstep_pid;
140
141/* Our I/O buffers. */
142static char remcom_in_buffer[BUFMAX];
143static char remcom_out_buffer[BUFMAX];
144
145/* Storage for the registers, in GDB format. */
146static unsigned long gdb_regs[(NUMREGBYTES +
147 sizeof(unsigned long) - 1) /
148 sizeof(unsigned long)];
149 117
150/* to keep track of the CPU which is doing the single stepping*/ 118/* to keep track of the CPU which is doing the single stepping*/
151atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1); 119atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
@@ -247,324 +215,6 @@ void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
247} 215}
248 216
249/* 217/*
250 * GDB remote protocol parser:
251 */
252
253static int hex(char ch)
254{
255 if ((ch >= 'a') && (ch <= 'f'))
256 return ch - 'a' + 10;
257 if ((ch >= '0') && (ch <= '9'))
258 return ch - '0';
259 if ((ch >= 'A') && (ch <= 'F'))
260 return ch - 'A' + 10;
261 return -1;
262}
263
264/* scan for the sequence $<data>#<checksum> */
265static void get_packet(char *buffer)
266{
267 unsigned char checksum;
268 unsigned char xmitcsum;
269 int count;
270 char ch;
271
272 do {
273 /*
274 * Spin and wait around for the start character, ignore all
275 * other characters:
276 */
277 while ((ch = (kgdb_io_ops->read_char())) != '$')
278 /* nothing */;
279
280 kgdb_connected = 1;
281 checksum = 0;
282 xmitcsum = -1;
283
284 count = 0;
285
286 /*
287 * now, read until a # or end of buffer is found:
288 */
289 while (count < (BUFMAX - 1)) {
290 ch = kgdb_io_ops->read_char();
291 if (ch == '#')
292 break;
293 checksum = checksum + ch;
294 buffer[count] = ch;
295 count = count + 1;
296 }
297 buffer[count] = 0;
298
299 if (ch == '#') {
300 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
301 xmitcsum += hex(kgdb_io_ops->read_char());
302
303 if (checksum != xmitcsum)
304 /* failed checksum */
305 kgdb_io_ops->write_char('-');
306 else
307 /* successful transfer */
308 kgdb_io_ops->write_char('+');
309 if (kgdb_io_ops->flush)
310 kgdb_io_ops->flush();
311 }
312 } while (checksum != xmitcsum);
313}
314
315/*
316 * Send the packet in buffer.
317 * Check for gdb connection if asked for.
318 */
319static void put_packet(char *buffer)
320{
321 unsigned char checksum;
322 int count;
323 char ch;
324
325 /*
326 * $<packet info>#<checksum>.
327 */
328 while (1) {
329 kgdb_io_ops->write_char('$');
330 checksum = 0;
331 count = 0;
332
333 while ((ch = buffer[count])) {
334 kgdb_io_ops->write_char(ch);
335 checksum += ch;
336 count++;
337 }
338
339 kgdb_io_ops->write_char('#');
340 kgdb_io_ops->write_char(hex_asc_hi(checksum));
341 kgdb_io_ops->write_char(hex_asc_lo(checksum));
342 if (kgdb_io_ops->flush)
343 kgdb_io_ops->flush();
344
345 /* Now see what we get in reply. */
346 ch = kgdb_io_ops->read_char();
347
348 if (ch == 3)
349 ch = kgdb_io_ops->read_char();
350
351 /* If we get an ACK, we are done. */
352 if (ch == '+')
353 return;
354
355 /*
356 * If we get the start of another packet, this means
357 * that GDB is attempting to reconnect. We will NAK
358 * the packet being sent, and stop trying to send this
359 * packet.
360 */
361 if (ch == '$') {
362 kgdb_io_ops->write_char('-');
363 if (kgdb_io_ops->flush)
364 kgdb_io_ops->flush();
365 return;
366 }
367 }
368}
369
370/*
371 * Convert the memory pointed to by mem into hex, placing result in buf.
372 * Return a pointer to the last char put in buf (null). May return an error.
373 */
374int kgdb_mem2hex(char *mem, char *buf, int count)
375{
376 char *tmp;
377 int err;
378
379 /*
380 * We use the upper half of buf as an intermediate buffer for the
381 * raw memory copy. Hex conversion will work against this one.
382 */
383 tmp = buf + count;
384
385 err = probe_kernel_read(tmp, mem, count);
386 if (!err) {
387 while (count > 0) {
388 buf = pack_hex_byte(buf, *tmp);
389 tmp++;
390 count--;
391 }
392
393 *buf = 0;
394 }
395
396 return err;
397}
398
399/*
400 * Copy the binary array pointed to by buf into mem. Fix $, #, and
401 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
402 * The input buf is overwitten with the result to write to mem.
403 */
404static int kgdb_ebin2mem(char *buf, char *mem, int count)
405{
406 int size = 0;
407 char *c = buf;
408
409 while (count-- > 0) {
410 c[size] = *buf++;
411 if (c[size] == 0x7d)
412 c[size] = *buf++ ^ 0x20;
413 size++;
414 }
415
416 return probe_kernel_write(mem, c, size);
417}
418
419/*
420 * Convert the hex array pointed to by buf into binary to be placed in mem.
421 * Return a pointer to the character AFTER the last byte written.
422 * May return an error.
423 */
424int kgdb_hex2mem(char *buf, char *mem, int count)
425{
426 char *tmp_raw;
427 char *tmp_hex;
428
429 /*
430 * We use the upper half of buf as an intermediate buffer for the
431 * raw memory that is converted from hex.
432 */
433 tmp_raw = buf + count * 2;
434
435 tmp_hex = tmp_raw - 1;
436 while (tmp_hex >= buf) {
437 tmp_raw--;
438 *tmp_raw = hex(*tmp_hex--);
439 *tmp_raw |= hex(*tmp_hex--) << 4;
440 }
441
442 return probe_kernel_write(mem, tmp_raw, count);
443}
444
445/*
446 * While we find nice hex chars, build a long_val.
447 * Return number of chars processed.
448 */
449int kgdb_hex2long(char **ptr, unsigned long *long_val)
450{
451 int hex_val;
452 int num = 0;
453 int negate = 0;
454
455 *long_val = 0;
456
457 if (**ptr == '-') {
458 negate = 1;
459 (*ptr)++;
460 }
461 while (**ptr) {
462 hex_val = hex(**ptr);
463 if (hex_val < 0)
464 break;
465
466 *long_val = (*long_val << 4) | hex_val;
467 num++;
468 (*ptr)++;
469 }
470
471 if (negate)
472 *long_val = -*long_val;
473
474 return num;
475}
476
477/* Write memory due to an 'M' or 'X' packet. */
478static int write_mem_msg(int binary)
479{
480 char *ptr = &remcom_in_buffer[1];
481 unsigned long addr;
482 unsigned long length;
483 int err;
484
485 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
486 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
487 if (binary)
488 err = kgdb_ebin2mem(ptr, (char *)addr, length);
489 else
490 err = kgdb_hex2mem(ptr, (char *)addr, length);
491 if (err)
492 return err;
493 if (CACHE_FLUSH_IS_SAFE)
494 flush_icache_range(addr, addr + length);
495 return 0;
496 }
497
498 return -EINVAL;
499}
500
501static void error_packet(char *pkt, int error)
502{
503 error = -error;
504 pkt[0] = 'E';
505 pkt[1] = hex_asc[(error / 10)];
506 pkt[2] = hex_asc[(error % 10)];
507 pkt[3] = '\0';
508}
509
510/*
511 * Thread ID accessors. We represent a flat TID space to GDB, where
512 * the per CPU idle threads (which under Linux all have PID 0) are
513 * remapped to negative TIDs.
514 */
515
516#define BUF_THREAD_ID_SIZE 16
517
518static char *pack_threadid(char *pkt, unsigned char *id)
519{
520 char *limit;
521
522 limit = pkt + BUF_THREAD_ID_SIZE;
523 while (pkt < limit)
524 pkt = pack_hex_byte(pkt, *id++);
525
526 return pkt;
527}
528
529static void int_to_threadref(unsigned char *id, int value)
530{
531 unsigned char *scan;
532 int i = 4;
533
534 scan = (unsigned char *)id;
535 while (i--)
536 *scan++ = 0;
537 put_unaligned_be32(value, scan);
538}
539
540static struct task_struct *getthread(struct pt_regs *regs, int tid)
541{
542 /*
543 * Non-positive TIDs are remapped to the cpu shadow information
544 */
545 if (tid == 0 || tid == -1)
546 tid = -atomic_read(&kgdb_active) - 2;
547 if (tid < -1 && tid > -NR_CPUS - 2) {
548 if (kgdb_info[-tid - 2].task)
549 return kgdb_info[-tid - 2].task;
550 else
551 return idle_task(-tid - 2);
552 }
553 if (tid <= 0) {
554 printk(KERN_ERR "KGDB: Internal thread select error\n");
555 dump_stack();
556 return NULL;
557 }
558
559 /*
560 * find_task_by_pid_ns() does not take the tasklist lock anymore
561 * but is nicely RCU locked - hence is a pretty resilient
562 * thing to use:
563 */
564 return find_task_by_pid_ns(tid, &init_pid_ns);
565}
566
567/*
568 * Some architectures need cache flushes when we set/clear a 218 * Some architectures need cache flushes when we set/clear a
569 * breakpoint: 219 * breakpoint:
570 */ 220 */
@@ -584,7 +234,7 @@ static void kgdb_flush_swbreak_addr(unsigned long addr)
584/* 234/*
585 * SW breakpoint management: 235 * SW breakpoint management:
586 */ 236 */
587static int kgdb_activate_sw_breakpoints(void) 237int dbg_activate_sw_breakpoints(void)
588{ 238{
589 unsigned long addr; 239 unsigned long addr;
590 int error; 240 int error;
@@ -610,7 +260,7 @@ static int kgdb_activate_sw_breakpoints(void)
610 return ret; 260 return ret;
611} 261}
612 262
613static int kgdb_set_sw_break(unsigned long addr) 263int dbg_set_sw_break(unsigned long addr)
614{ 264{
615 int err = kgdb_validate_break_address(addr); 265 int err = kgdb_validate_break_address(addr);
616 int breakno = -1; 266 int breakno = -1;
@@ -675,7 +325,7 @@ static int kgdb_deactivate_sw_breakpoints(void)
675 return ret; 325 return ret;
676} 326}
677 327
678static int kgdb_remove_sw_break(unsigned long addr) 328int dbg_remove_sw_break(unsigned long addr)
679{ 329{
680 int i; 330 int i;
681 331
@@ -701,7 +351,7 @@ int kgdb_isremovedbreak(unsigned long addr)
701 return 0; 351 return 0;
702} 352}
703 353
704static int remove_all_break(void) 354int dbg_remove_all_break(void)
705{ 355{
706 unsigned long addr; 356 unsigned long addr;
707 int error; 357 int error;
@@ -729,53 +379,6 @@ setundefined:
729} 379}
730 380
731/* 381/*
732 * Remap normal tasks to their real PID,
733 * CPU shadow threads are mapped to -CPU - 2
734 */
735static inline int shadow_pid(int realpid)
736{
737 if (realpid)
738 return realpid;
739
740 return -raw_smp_processor_id() - 2;
741}
742
743static char gdbmsgbuf[BUFMAX + 1];
744
745static void kgdb_msg_write(const char *s, int len)
746{
747 char *bufptr;
748 int wcount;
749 int i;
750
751 /* 'O'utput */
752 gdbmsgbuf[0] = 'O';
753
754 /* Fill and send buffers... */
755 while (len > 0) {
756 bufptr = gdbmsgbuf + 1;
757
758 /* Calculate how many this time */
759 if ((len << 1) > (BUFMAX - 2))
760 wcount = (BUFMAX - 2) >> 1;
761 else
762 wcount = len;
763
764 /* Pack in hex chars */
765 for (i = 0; i < wcount; i++)
766 bufptr = pack_hex_byte(bufptr, s[i]);
767 *bufptr = '\0';
768
769 /* Move up */
770 s += wcount;
771 len -= wcount;
772
773 /* Write packet */
774 put_packet(gdbmsgbuf);
775 }
776}
777
778/*
779 * Return true if there is a valid kgdb I/O module. Also if no 382 * Return true if there is a valid kgdb I/O module. Also if no
780 * debugger is attached a message can be printed to the console about 383 * debugger is attached a message can be printed to the console about
781 * waiting for the debugger to attach. 384 * waiting for the debugger to attach.
@@ -786,7 +389,7 @@ static void kgdb_msg_write(const char *s, int len)
786 */ 389 */
787static int kgdb_io_ready(int print_wait) 390static int kgdb_io_ready(int print_wait)
788{ 391{
789 if (!kgdb_io_ops) 392 if (!dbg_io_ops)
790 return 0; 393 return 0;
791 if (kgdb_connected) 394 if (kgdb_connected)
792 return 1; 395 return 1;
@@ -797,525 +400,6 @@ static int kgdb_io_ready(int print_wait)
797 return 1; 400 return 1;
798} 401}
799 402
800/*
801 * All the functions that start with gdb_cmd are the various
802 * operations to implement the handlers for the gdbserial protocol
803 * where KGDB is communicating with an external debugger
804 */
805
806/* Handle the '?' status packets */
807static void gdb_cmd_status(struct kgdb_state *ks)
808{
809 /*
810 * We know that this packet is only sent
811 * during initial connect. So to be safe,
812 * we clear out our breakpoints now in case
813 * GDB is reconnecting.
814 */
815 remove_all_break();
816
817 remcom_out_buffer[0] = 'S';
818 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
819}
820
821/* Handle the 'g' get registers request */
822static void gdb_cmd_getregs(struct kgdb_state *ks)
823{
824 struct task_struct *thread;
825 void *local_debuggerinfo;
826 int i;
827
828 thread = kgdb_usethread;
829 if (!thread) {
830 thread = kgdb_info[ks->cpu].task;
831 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
832 } else {
833 local_debuggerinfo = NULL;
834 for_each_online_cpu(i) {
835 /*
836 * Try to find the task on some other
837 * or possibly this node if we do not
838 * find the matching task then we try
839 * to approximate the results.
840 */
841 if (thread == kgdb_info[i].task)
842 local_debuggerinfo = kgdb_info[i].debuggerinfo;
843 }
844 }
845
846 /*
847 * All threads that don't have debuggerinfo should be
848 * in schedule() sleeping, since all other CPUs
849 * are in kgdb_wait, and thus have debuggerinfo.
850 */
851 if (local_debuggerinfo) {
852 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
853 } else {
854 /*
855 * Pull stuff saved during switch_to; nothing
856 * else is accessible (or even particularly
857 * relevant).
858 *
859 * This should be enough for a stack trace.
860 */
861 sleeping_thread_to_gdb_regs(gdb_regs, thread);
862 }
863 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
864}
865
866/* Handle the 'G' set registers request */
867static void gdb_cmd_setregs(struct kgdb_state *ks)
868{
869 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
870
871 if (kgdb_usethread && kgdb_usethread != current) {
872 error_packet(remcom_out_buffer, -EINVAL);
873 } else {
874 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
875 strcpy(remcom_out_buffer, "OK");
876 }
877}
878
879/* Handle the 'm' memory read bytes */
880static void gdb_cmd_memread(struct kgdb_state *ks)
881{
882 char *ptr = &remcom_in_buffer[1];
883 unsigned long length;
884 unsigned long addr;
885 int err;
886
887 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
888 kgdb_hex2long(&ptr, &length) > 0) {
889 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
890 if (err)
891 error_packet(remcom_out_buffer, err);
892 } else {
893 error_packet(remcom_out_buffer, -EINVAL);
894 }
895}
896
897/* Handle the 'M' memory write bytes */
898static void gdb_cmd_memwrite(struct kgdb_state *ks)
899{
900 int err = write_mem_msg(0);
901
902 if (err)
903 error_packet(remcom_out_buffer, err);
904 else
905 strcpy(remcom_out_buffer, "OK");
906}
907
908/* Handle the 'X' memory binary write bytes */
909static void gdb_cmd_binwrite(struct kgdb_state *ks)
910{
911 int err = write_mem_msg(1);
912
913 if (err)
914 error_packet(remcom_out_buffer, err);
915 else
916 strcpy(remcom_out_buffer, "OK");
917}
918
919/* Handle the 'D' or 'k', detach or kill packets */
920static void gdb_cmd_detachkill(struct kgdb_state *ks)
921{
922 int error;
923
924 /* The detach case */
925 if (remcom_in_buffer[0] == 'D') {
926 error = remove_all_break();
927 if (error < 0) {
928 error_packet(remcom_out_buffer, error);
929 } else {
930 strcpy(remcom_out_buffer, "OK");
931 kgdb_connected = 0;
932 }
933 put_packet(remcom_out_buffer);
934 } else {
935 /*
936 * Assume the kill case, with no exit code checking,
937 * trying to force detach the debugger:
938 */
939 remove_all_break();
940 kgdb_connected = 0;
941 }
942}
943
944/* Handle the 'R' reboot packets */
945static int gdb_cmd_reboot(struct kgdb_state *ks)
946{
947 /* For now, only honor R0 */
948 if (strcmp(remcom_in_buffer, "R0") == 0) {
949 printk(KERN_CRIT "Executing emergency reboot\n");
950 strcpy(remcom_out_buffer, "OK");
951 put_packet(remcom_out_buffer);
952
953 /*
954 * Execution should not return from
955 * machine_emergency_restart()
956 */
957 machine_emergency_restart();
958 kgdb_connected = 0;
959
960 return 1;
961 }
962 return 0;
963}
964
965/* Handle the 'q' query packets */
966static void gdb_cmd_query(struct kgdb_state *ks)
967{
968 struct task_struct *g;
969 struct task_struct *p;
970 unsigned char thref[8];
971 char *ptr;
972 int i;
973 int cpu;
974 int finished = 0;
975
976 switch (remcom_in_buffer[1]) {
977 case 's':
978 case 'f':
979 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
980 error_packet(remcom_out_buffer, -EINVAL);
981 break;
982 }
983
984 i = 0;
985 remcom_out_buffer[0] = 'm';
986 ptr = remcom_out_buffer + 1;
987 if (remcom_in_buffer[1] == 'f') {
988 /* Each cpu is a shadow thread */
989 for_each_online_cpu(cpu) {
990 ks->thr_query = 0;
991 int_to_threadref(thref, -cpu - 2);
992 pack_threadid(ptr, thref);
993 ptr += BUF_THREAD_ID_SIZE;
994 *(ptr++) = ',';
995 i++;
996 }
997 }
998
999 do_each_thread(g, p) {
1000 if (i >= ks->thr_query && !finished) {
1001 int_to_threadref(thref, p->pid);
1002 pack_threadid(ptr, thref);
1003 ptr += BUF_THREAD_ID_SIZE;
1004 *(ptr++) = ',';
1005 ks->thr_query++;
1006 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
1007 finished = 1;
1008 }
1009 i++;
1010 } while_each_thread(g, p);
1011
1012 *(--ptr) = '\0';
1013 break;
1014
1015 case 'C':
1016 /* Current thread id */
1017 strcpy(remcom_out_buffer, "QC");
1018 ks->threadid = shadow_pid(current->pid);
1019 int_to_threadref(thref, ks->threadid);
1020 pack_threadid(remcom_out_buffer + 2, thref);
1021 break;
1022 case 'T':
1023 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1024 error_packet(remcom_out_buffer, -EINVAL);
1025 break;
1026 }
1027 ks->threadid = 0;
1028 ptr = remcom_in_buffer + 17;
1029 kgdb_hex2long(&ptr, &ks->threadid);
1030 if (!getthread(ks->linux_regs, ks->threadid)) {
1031 error_packet(remcom_out_buffer, -EINVAL);
1032 break;
1033 }
1034 if ((int)ks->threadid > 0) {
1035 kgdb_mem2hex(getthread(ks->linux_regs,
1036 ks->threadid)->comm,
1037 remcom_out_buffer, 16);
1038 } else {
1039 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1040
1041 sprintf(tmpstr, "shadowCPU%d",
1042 (int)(-ks->threadid - 2));
1043 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1044 }
1045 break;
1046 }
1047}
1048
1049/* Handle the 'H' task query packets */
1050static void gdb_cmd_task(struct kgdb_state *ks)
1051{
1052 struct task_struct *thread;
1053 char *ptr;
1054
1055 switch (remcom_in_buffer[1]) {
1056 case 'g':
1057 ptr = &remcom_in_buffer[2];
1058 kgdb_hex2long(&ptr, &ks->threadid);
1059 thread = getthread(ks->linux_regs, ks->threadid);
1060 if (!thread && ks->threadid > 0) {
1061 error_packet(remcom_out_buffer, -EINVAL);
1062 break;
1063 }
1064 kgdb_usethread = thread;
1065 ks->kgdb_usethreadid = ks->threadid;
1066 strcpy(remcom_out_buffer, "OK");
1067 break;
1068 case 'c':
1069 ptr = &remcom_in_buffer[2];
1070 kgdb_hex2long(&ptr, &ks->threadid);
1071 if (!ks->threadid) {
1072 kgdb_contthread = NULL;
1073 } else {
1074 thread = getthread(ks->linux_regs, ks->threadid);
1075 if (!thread && ks->threadid > 0) {
1076 error_packet(remcom_out_buffer, -EINVAL);
1077 break;
1078 }
1079 kgdb_contthread = thread;
1080 }
1081 strcpy(remcom_out_buffer, "OK");
1082 break;
1083 }
1084}
1085
1086/* Handle the 'T' thread query packets */
1087static void gdb_cmd_thread(struct kgdb_state *ks)
1088{
1089 char *ptr = &remcom_in_buffer[1];
1090 struct task_struct *thread;
1091
1092 kgdb_hex2long(&ptr, &ks->threadid);
1093 thread = getthread(ks->linux_regs, ks->threadid);
1094 if (thread)
1095 strcpy(remcom_out_buffer, "OK");
1096 else
1097 error_packet(remcom_out_buffer, -EINVAL);
1098}
1099
1100/* Handle the 'z' or 'Z' breakpoint remove or set packets */
1101static void gdb_cmd_break(struct kgdb_state *ks)
1102{
1103 /*
1104 * Since GDB-5.3, it's been drafted that '0' is a software
1105 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1106 */
1107 char *bpt_type = &remcom_in_buffer[1];
1108 char *ptr = &remcom_in_buffer[2];
1109 unsigned long addr;
1110 unsigned long length;
1111 int error = 0;
1112
1113 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1114 /* Unsupported */
1115 if (*bpt_type > '4')
1116 return;
1117 } else {
1118 if (*bpt_type != '0' && *bpt_type != '1')
1119 /* Unsupported. */
1120 return;
1121 }
1122
1123 /*
1124 * Test if this is a hardware breakpoint, and
1125 * if we support it:
1126 */
1127 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1128 /* Unsupported. */
1129 return;
1130
1131 if (*(ptr++) != ',') {
1132 error_packet(remcom_out_buffer, -EINVAL);
1133 return;
1134 }
1135 if (!kgdb_hex2long(&ptr, &addr)) {
1136 error_packet(remcom_out_buffer, -EINVAL);
1137 return;
1138 }
1139 if (*(ptr++) != ',' ||
1140 !kgdb_hex2long(&ptr, &length)) {
1141 error_packet(remcom_out_buffer, -EINVAL);
1142 return;
1143 }
1144
1145 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1146 error = kgdb_set_sw_break(addr);
1147 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1148 error = kgdb_remove_sw_break(addr);
1149 else if (remcom_in_buffer[0] == 'Z')
1150 error = arch_kgdb_ops.set_hw_breakpoint(addr,
1151 (int)length, *bpt_type - '0');
1152 else if (remcom_in_buffer[0] == 'z')
1153 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
1154 (int) length, *bpt_type - '0');
1155
1156 if (error == 0)
1157 strcpy(remcom_out_buffer, "OK");
1158 else
1159 error_packet(remcom_out_buffer, error);
1160}
1161
1162/* Handle the 'C' signal / exception passing packets */
1163static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1164{
1165 /* C09 == pass exception
1166 * C15 == detach kgdb, pass exception
1167 */
1168 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1169
1170 ks->pass_exception = 1;
1171 remcom_in_buffer[0] = 'c';
1172
1173 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1174
1175 ks->pass_exception = 1;
1176 remcom_in_buffer[0] = 'D';
1177 remove_all_break();
1178 kgdb_connected = 0;
1179 return 1;
1180
1181 } else {
1182 kgdb_msg_write("KGDB only knows signal 9 (pass)"
1183 " and 15 (pass and disconnect)\n"
1184 "Executing a continue without signal passing\n", 0);
1185 remcom_in_buffer[0] = 'c';
1186 }
1187
1188 /* Indicate fall through */
1189 return -1;
1190}
1191
1192/*
1193 * This function performs all gdbserial command procesing
1194 */
1195static int gdb_serial_stub(struct kgdb_state *ks)
1196{
1197 int error = 0;
1198 int tmp;
1199
1200 /* Clear the out buffer. */
1201 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1202
1203 if (kgdb_connected) {
1204 unsigned char thref[8];
1205 char *ptr;
1206
1207 /* Reply to host that an exception has occurred */
1208 ptr = remcom_out_buffer;
1209 *ptr++ = 'T';
1210 ptr = pack_hex_byte(ptr, ks->signo);
1211 ptr += strlen(strcpy(ptr, "thread:"));
1212 int_to_threadref(thref, shadow_pid(current->pid));
1213 ptr = pack_threadid(ptr, thref);
1214 *ptr++ = ';';
1215 put_packet(remcom_out_buffer);
1216 }
1217
1218 kgdb_usethread = kgdb_info[ks->cpu].task;
1219 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1220 ks->pass_exception = 0;
1221
1222 while (1) {
1223 error = 0;
1224
1225 /* Clear the out buffer. */
1226 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1227
1228 get_packet(remcom_in_buffer);
1229
1230 switch (remcom_in_buffer[0]) {
1231 case '?': /* gdbserial status */
1232 gdb_cmd_status(ks);
1233 break;
1234 case 'g': /* return the value of the CPU registers */
1235 gdb_cmd_getregs(ks);
1236 break;
1237 case 'G': /* set the value of the CPU registers - return OK */
1238 gdb_cmd_setregs(ks);
1239 break;
1240 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1241 gdb_cmd_memread(ks);
1242 break;
1243 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1244 gdb_cmd_memwrite(ks);
1245 break;
1246 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1247 gdb_cmd_binwrite(ks);
1248 break;
1249 /* kill or detach. KGDB should treat this like a
1250 * continue.
1251 */
1252 case 'D': /* Debugger detach */
1253 case 'k': /* Debugger detach via kill */
1254 gdb_cmd_detachkill(ks);
1255 goto default_handle;
1256 case 'R': /* Reboot */
1257 if (gdb_cmd_reboot(ks))
1258 goto default_handle;
1259 break;
1260 case 'q': /* query command */
1261 gdb_cmd_query(ks);
1262 break;
1263 case 'H': /* task related */
1264 gdb_cmd_task(ks);
1265 break;
1266 case 'T': /* Query thread status */
1267 gdb_cmd_thread(ks);
1268 break;
1269 case 'z': /* Break point remove */
1270 case 'Z': /* Break point set */
1271 gdb_cmd_break(ks);
1272 break;
1273 case 'C': /* Exception passing */
1274 tmp = gdb_cmd_exception_pass(ks);
1275 if (tmp > 0)
1276 goto default_handle;
1277 if (tmp == 0)
1278 break;
1279 /* Fall through on tmp < 0 */
1280 case 'c': /* Continue packet */
1281 case 's': /* Single step packet */
1282 if (kgdb_contthread && kgdb_contthread != current) {
1283 /* Can't switch threads in kgdb */
1284 error_packet(remcom_out_buffer, -EINVAL);
1285 break;
1286 }
1287 kgdb_activate_sw_breakpoints();
1288 /* Fall through to default processing */
1289 default:
1290default_handle:
1291 error = kgdb_arch_handle_exception(ks->ex_vector,
1292 ks->signo,
1293 ks->err_code,
1294 remcom_in_buffer,
1295 remcom_out_buffer,
1296 ks->linux_regs);
1297 /*
1298 * Leave cmd processing on error, detach,
1299 * kill, continue, or single step.
1300 */
1301 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1302 remcom_in_buffer[0] == 'k') {
1303 error = 0;
1304 goto kgdb_exit;
1305 }
1306
1307 }
1308
1309 /* reply to the request */
1310 put_packet(remcom_out_buffer);
1311 }
1312
1313kgdb_exit:
1314 if (ks->pass_exception)
1315 error = 1;
1316 return error;
1317}
1318
1319static int kgdb_reenter_check(struct kgdb_state *ks) 403static int kgdb_reenter_check(struct kgdb_state *ks)
1320{ 404{
1321 unsigned long addr; 405 unsigned long addr;
@@ -1334,17 +418,17 @@ static int kgdb_reenter_check(struct kgdb_state *ks)
1334 * user because the user planted a breakpoint in a place that 418 * user because the user planted a breakpoint in a place that
1335 * KGDB needs in order to function. 419 * KGDB needs in order to function.
1336 */ 420 */
1337 if (kgdb_remove_sw_break(addr) == 0) { 421 if (dbg_remove_sw_break(addr) == 0) {
1338 exception_level = 0; 422 exception_level = 0;
1339 kgdb_skipexception(ks->ex_vector, ks->linux_regs); 423 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1340 kgdb_activate_sw_breakpoints(); 424 dbg_activate_sw_breakpoints();
1341 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n", 425 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
1342 addr); 426 addr);
1343 WARN_ON_ONCE(1); 427 WARN_ON_ONCE(1);
1344 428
1345 return 1; 429 return 1;
1346 } 430 }
1347 remove_all_break(); 431 dbg_remove_all_break();
1348 kgdb_skipexception(ks->ex_vector, ks->linux_regs); 432 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1349 433
1350 if (exception_level > 1) { 434 if (exception_level > 1) {
@@ -1430,7 +514,7 @@ return_normal:
1430 514
1431 if (!kgdb_io_ready(1)) { 515 if (!kgdb_io_ready(1)) {
1432 error = 1; 516 error = 1;
1433 goto kgdb_restore; /* No I/O connection, so resume the system */ 517 goto kgdb_restore; /* No I/O connection, resume the system */
1434 } 518 }
1435 519
1436 /* 520 /*
@@ -1440,8 +524,8 @@ return_normal:
1440 goto kgdb_restore; 524 goto kgdb_restore;
1441 525
1442 /* Call the I/O driver's pre_exception routine */ 526 /* Call the I/O driver's pre_exception routine */
1443 if (kgdb_io_ops->pre_exception) 527 if (dbg_io_ops->pre_exception)
1444 kgdb_io_ops->pre_exception(); 528 dbg_io_ops->pre_exception();
1445 529
1446 kgdb_disable_hw_debug(ks->linux_regs); 530 kgdb_disable_hw_debug(ks->linux_regs);
1447 531
@@ -1485,8 +569,8 @@ return_normal:
1485 error = gdb_serial_stub(ks); 569 error = gdb_serial_stub(ks);
1486 570
1487 /* Call the I/O driver's post_exception routine */ 571 /* Call the I/O driver's post_exception routine */
1488 if (kgdb_io_ops->post_exception) 572 if (dbg_io_ops->post_exception)
1489 kgdb_io_ops->post_exception(); 573 dbg_io_ops->post_exception();
1490 574
1491 atomic_dec(&cpu_in_kgdb[ks->cpu]); 575 atomic_dec(&cpu_in_kgdb[ks->cpu]);
1492 576
@@ -1585,7 +669,7 @@ static void kgdb_console_write(struct console *co, const char *s,
1585 return; 669 return;
1586 670
1587 local_irq_save(flags); 671 local_irq_save(flags);
1588 kgdb_msg_write(s, count); 672 gdbstub_msg_write(s, count);
1589 local_irq_restore(flags); 673 local_irq_restore(flags);
1590} 674}
1591 675
@@ -1597,9 +681,9 @@ static struct console kgdbcons = {
1597}; 681};
1598 682
1599#ifdef CONFIG_MAGIC_SYSRQ 683#ifdef CONFIG_MAGIC_SYSRQ
1600static void sysrq_handle_gdb(int key, struct tty_struct *tty) 684static void sysrq_handle_dbg(int key, struct tty_struct *tty)
1601{ 685{
1602 if (!kgdb_io_ops) { 686 if (!dbg_io_ops) {
1603 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n"); 687 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1604 return; 688 return;
1605 } 689 }
@@ -1609,8 +693,8 @@ static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1609 kgdb_breakpoint(); 693 kgdb_breakpoint();
1610} 694}
1611 695
1612static struct sysrq_key_op sysrq_gdb_op = { 696static struct sysrq_key_op sysrq_dbg_op = {
1613 .handler = sysrq_handle_gdb, 697 .handler = sysrq_handle_dbg,
1614 .help_msg = "debug(G)", 698 .help_msg = "debug(G)",
1615 .action_msg = "DEBUG", 699 .action_msg = "DEBUG",
1616}; 700};
@@ -1622,7 +706,7 @@ static void kgdb_register_callbacks(void)
1622 kgdb_io_module_registered = 1; 706 kgdb_io_module_registered = 1;
1623 kgdb_arch_init(); 707 kgdb_arch_init();
1624#ifdef CONFIG_MAGIC_SYSRQ 708#ifdef CONFIG_MAGIC_SYSRQ
1625 register_sysrq_key('g', &sysrq_gdb_op); 709 register_sysrq_key('g', &sysrq_dbg_op);
1626#endif 710#endif
1627 if (kgdb_use_con && !kgdb_con_registered) { 711 if (kgdb_use_con && !kgdb_con_registered) {
1628 register_console(&kgdbcons); 712 register_console(&kgdbcons);
@@ -1642,7 +726,7 @@ static void kgdb_unregister_callbacks(void)
1642 kgdb_io_module_registered = 0; 726 kgdb_io_module_registered = 0;
1643 kgdb_arch_exit(); 727 kgdb_arch_exit();
1644#ifdef CONFIG_MAGIC_SYSRQ 728#ifdef CONFIG_MAGIC_SYSRQ
1645 unregister_sysrq_key('g', &sysrq_gdb_op); 729 unregister_sysrq_key('g', &sysrq_dbg_op);
1646#endif 730#endif
1647 if (kgdb_con_registered) { 731 if (kgdb_con_registered) {
1648 unregister_console(&kgdbcons); 732 unregister_console(&kgdbcons);
@@ -1661,17 +745,17 @@ static void kgdb_initial_breakpoint(void)
1661 745
1662/** 746/**
1663 * kgdb_register_io_module - register KGDB IO module 747 * kgdb_register_io_module - register KGDB IO module
1664 * @new_kgdb_io_ops: the io ops vector 748 * @new_dbg_io_ops: the io ops vector
1665 * 749 *
1666 * Register it with the KGDB core. 750 * Register it with the KGDB core.
1667 */ 751 */
1668int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops) 752int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1669{ 753{
1670 int err; 754 int err;
1671 755
1672 spin_lock(&kgdb_registration_lock); 756 spin_lock(&kgdb_registration_lock);
1673 757
1674 if (kgdb_io_ops) { 758 if (dbg_io_ops) {
1675 spin_unlock(&kgdb_registration_lock); 759 spin_unlock(&kgdb_registration_lock);
1676 760
1677 printk(KERN_ERR "kgdb: Another I/O driver is already " 761 printk(KERN_ERR "kgdb: Another I/O driver is already "
@@ -1679,20 +763,20 @@ int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1679 return -EBUSY; 763 return -EBUSY;
1680 } 764 }
1681 765
1682 if (new_kgdb_io_ops->init) { 766 if (new_dbg_io_ops->init) {
1683 err = new_kgdb_io_ops->init(); 767 err = new_dbg_io_ops->init();
1684 if (err) { 768 if (err) {
1685 spin_unlock(&kgdb_registration_lock); 769 spin_unlock(&kgdb_registration_lock);
1686 return err; 770 return err;
1687 } 771 }
1688 } 772 }
1689 773
1690 kgdb_io_ops = new_kgdb_io_ops; 774 dbg_io_ops = new_dbg_io_ops;
1691 775
1692 spin_unlock(&kgdb_registration_lock); 776 spin_unlock(&kgdb_registration_lock);
1693 777
1694 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n", 778 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1695 new_kgdb_io_ops->name); 779 new_dbg_io_ops->name);
1696 780
1697 /* Arm KGDB now. */ 781 /* Arm KGDB now. */
1698 kgdb_register_callbacks(); 782 kgdb_register_callbacks();
@@ -1706,11 +790,11 @@ EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1706 790
1707/** 791/**
1708 * kkgdb_unregister_io_module - unregister KGDB IO module 792 * kkgdb_unregister_io_module - unregister KGDB IO module
1709 * @old_kgdb_io_ops: the io ops vector 793 * @old_dbg_io_ops: the io ops vector
1710 * 794 *
1711 * Unregister it with the KGDB core. 795 * Unregister it with the KGDB core.
1712 */ 796 */
1713void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops) 797void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1714{ 798{
1715 BUG_ON(kgdb_connected); 799 BUG_ON(kgdb_connected);
1716 800
@@ -1722,14 +806,14 @@ void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1722 806
1723 spin_lock(&kgdb_registration_lock); 807 spin_lock(&kgdb_registration_lock);
1724 808
1725 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops); 809 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1726 kgdb_io_ops = NULL; 810 dbg_io_ops = NULL;
1727 811
1728 spin_unlock(&kgdb_registration_lock); 812 spin_unlock(&kgdb_registration_lock);
1729 813
1730 printk(KERN_INFO 814 printk(KERN_INFO
1731 "kgdb: Unregistered I/O driver %s, debugger disabled.\n", 815 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1732 old_kgdb_io_ops->name); 816 old_dbg_io_ops->name);
1733} 817}
1734EXPORT_SYMBOL_GPL(kgdb_unregister_io_module); 818EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1735 819
diff --git a/kernel/debug/debug_core.h b/kernel/debug/debug_core.h
new file mode 100644
index 000000000000..db554f9be51d
--- /dev/null
+++ b/kernel/debug/debug_core.h
@@ -0,0 +1,55 @@
1/*
2 * Created by: Jason Wessel <jason.wessel@windriver.com>
3 *
4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#ifndef _DEBUG_CORE_H_
12#define _DEBUG_CORE_H_
13/*
14 * These are the private implementation headers between the kernel
15 * debugger core and the debugger front end code.
16 */
17
18/* kernel debug core data structures */
19struct kgdb_state {
20 int ex_vector;
21 int signo;
22 int err_code;
23 int cpu;
24 int pass_exception;
25 unsigned long thr_query;
26 unsigned long threadid;
27 long kgdb_usethreadid;
28 struct pt_regs *linux_regs;
29};
30
31/* Exception state values */
32#define DCPU_WANT_MASTER 0x1 /* Waiting to become a master kgdb cpu */
33#define DCPU_NEXT_MASTER 0x2 /* Transition from one master cpu to another */
34#define DCPU_IS_SLAVE 0x4 /* Slave cpu enter exception */
35#define DCPU_SSTEP 0x8 /* CPU is single stepping */
36
37struct debuggerinfo_struct {
38 void *debuggerinfo;
39 struct task_struct *task;
40 int exception_state;
41};
42
43extern struct debuggerinfo_struct kgdb_info[];
44
45/* kernel debug core break point routines */
46extern int dbg_remove_all_break(void);
47extern int dbg_set_sw_break(unsigned long addr);
48extern int dbg_remove_sw_break(unsigned long addr);
49extern int dbg_activate_sw_breakpoints(void);
50
51/* gdbstub interface functions */
52extern int gdb_serial_stub(struct kgdb_state *ks);
53extern void gdbstub_msg_write(const char *s, int len);
54
55#endif /* _DEBUG_CORE_H_ */
diff --git a/kernel/debug/gdbstub.c b/kernel/debug/gdbstub.c
new file mode 100644
index 000000000000..ccdf0929f12d
--- /dev/null
+++ b/kernel/debug/gdbstub.c
@@ -0,0 +1,934 @@
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/reboot.h>
34#include <linux/uaccess.h>
35#include <asm/cacheflush.h>
36#include <asm/unaligned.h>
37#include "debug_core.h"
38
39#define KGDB_MAX_THREAD_QUERY 17
40
41/* Our I/O buffers. */
42static char remcom_in_buffer[BUFMAX];
43static char remcom_out_buffer[BUFMAX];
44
45/* Storage for the registers, in GDB format. */
46static unsigned long gdb_regs[(NUMREGBYTES +
47 sizeof(unsigned long) - 1) /
48 sizeof(unsigned long)];
49
50/*
51 * GDB remote protocol parser:
52 */
53
54static int hex(char ch)
55{
56 if ((ch >= 'a') && (ch <= 'f'))
57 return ch - 'a' + 10;
58 if ((ch >= '0') && (ch <= '9'))
59 return ch - '0';
60 if ((ch >= 'A') && (ch <= 'F'))
61 return ch - 'A' + 10;
62 return -1;
63}
64
65/* scan for the sequence $<data>#<checksum> */
66static void get_packet(char *buffer)
67{
68 unsigned char checksum;
69 unsigned char xmitcsum;
70 int count;
71 char ch;
72
73 do {
74 /*
75 * Spin and wait around for the start character, ignore all
76 * other characters:
77 */
78 while ((ch = (dbg_io_ops->read_char())) != '$')
79 /* nothing */;
80
81 kgdb_connected = 1;
82 checksum = 0;
83 xmitcsum = -1;
84
85 count = 0;
86
87 /*
88 * now, read until a # or end of buffer is found:
89 */
90 while (count < (BUFMAX - 1)) {
91 ch = dbg_io_ops->read_char();
92 if (ch == '#')
93 break;
94 checksum = checksum + ch;
95 buffer[count] = ch;
96 count = count + 1;
97 }
98 buffer[count] = 0;
99
100 if (ch == '#') {
101 xmitcsum = hex(dbg_io_ops->read_char()) << 4;
102 xmitcsum += hex(dbg_io_ops->read_char());
103
104 if (checksum != xmitcsum)
105 /* failed checksum */
106 dbg_io_ops->write_char('-');
107 else
108 /* successful transfer */
109 dbg_io_ops->write_char('+');
110 if (dbg_io_ops->flush)
111 dbg_io_ops->flush();
112 }
113 } while (checksum != xmitcsum);
114}
115
116/*
117 * Send the packet in buffer.
118 * Check for gdb connection if asked for.
119 */
120static void put_packet(char *buffer)
121{
122 unsigned char checksum;
123 int count;
124 char ch;
125
126 /*
127 * $<packet info>#<checksum>.
128 */
129 while (1) {
130 dbg_io_ops->write_char('$');
131 checksum = 0;
132 count = 0;
133
134 while ((ch = buffer[count])) {
135 dbg_io_ops->write_char(ch);
136 checksum += ch;
137 count++;
138 }
139
140 dbg_io_ops->write_char('#');
141 dbg_io_ops->write_char(hex_asc_hi(checksum));
142 dbg_io_ops->write_char(hex_asc_lo(checksum));
143 if (dbg_io_ops->flush)
144 dbg_io_ops->flush();
145
146 /* Now see what we get in reply. */
147 ch = dbg_io_ops->read_char();
148
149 if (ch == 3)
150 ch = dbg_io_ops->read_char();
151
152 /* If we get an ACK, we are done. */
153 if (ch == '+')
154 return;
155
156 /*
157 * If we get the start of another packet, this means
158 * that GDB is attempting to reconnect. We will NAK
159 * the packet being sent, and stop trying to send this
160 * packet.
161 */
162 if (ch == '$') {
163 dbg_io_ops->write_char('-');
164 if (dbg_io_ops->flush)
165 dbg_io_ops->flush();
166 return;
167 }
168 }
169}
170
171static char gdbmsgbuf[BUFMAX + 1];
172
173void gdbstub_msg_write(const char *s, int len)
174{
175 char *bufptr;
176 int wcount;
177 int i;
178
179 /* 'O'utput */
180 gdbmsgbuf[0] = 'O';
181
182 /* Fill and send buffers... */
183 while (len > 0) {
184 bufptr = gdbmsgbuf + 1;
185
186 /* Calculate how many this time */
187 if ((len << 1) > (BUFMAX - 2))
188 wcount = (BUFMAX - 2) >> 1;
189 else
190 wcount = len;
191
192 /* Pack in hex chars */
193 for (i = 0; i < wcount; i++)
194 bufptr = pack_hex_byte(bufptr, s[i]);
195 *bufptr = '\0';
196
197 /* Move up */
198 s += wcount;
199 len -= wcount;
200
201 /* Write packet */
202 put_packet(gdbmsgbuf);
203 }
204}
205
206/*
207 * Convert the memory pointed to by mem into hex, placing result in
208 * buf. Return a pointer to the last char put in buf (null). May
209 * return an error.
210 */
211int kgdb_mem2hex(char *mem, char *buf, int count)
212{
213 char *tmp;
214 int err;
215
216 /*
217 * We use the upper half of buf as an intermediate buffer for the
218 * raw memory copy. Hex conversion will work against this one.
219 */
220 tmp = buf + count;
221
222 err = probe_kernel_read(tmp, mem, count);
223 if (!err) {
224 while (count > 0) {
225 buf = pack_hex_byte(buf, *tmp);
226 tmp++;
227 count--;
228 }
229
230 *buf = 0;
231 }
232
233 return err;
234}
235
236/*
237 * Convert the hex array pointed to by buf into binary to be placed in
238 * mem. Return a pointer to the character AFTER the last byte
239 * written. May return an error.
240 */
241int kgdb_hex2mem(char *buf, char *mem, int count)
242{
243 char *tmp_raw;
244 char *tmp_hex;
245
246 /*
247 * We use the upper half of buf as an intermediate buffer for the
248 * raw memory that is converted from hex.
249 */
250 tmp_raw = buf + count * 2;
251
252 tmp_hex = tmp_raw - 1;
253 while (tmp_hex >= buf) {
254 tmp_raw--;
255 *tmp_raw = hex(*tmp_hex--);
256 *tmp_raw |= hex(*tmp_hex--) << 4;
257 }
258
259 return probe_kernel_write(mem, tmp_raw, count);
260}
261
262/*
263 * While we find nice hex chars, build a long_val.
264 * Return number of chars processed.
265 */
266int kgdb_hex2long(char **ptr, unsigned long *long_val)
267{
268 int hex_val;
269 int num = 0;
270 int negate = 0;
271
272 *long_val = 0;
273
274 if (**ptr == '-') {
275 negate = 1;
276 (*ptr)++;
277 }
278 while (**ptr) {
279 hex_val = hex(**ptr);
280 if (hex_val < 0)
281 break;
282
283 *long_val = (*long_val << 4) | hex_val;
284 num++;
285 (*ptr)++;
286 }
287
288 if (negate)
289 *long_val = -*long_val;
290
291 return num;
292}
293
294/*
295 * Copy the binary array pointed to by buf into mem. Fix $, #, and
296 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
297 * The input buf is overwitten with the result to write to mem.
298 */
299static int kgdb_ebin2mem(char *buf, char *mem, int count)
300{
301 int size = 0;
302 char *c = buf;
303
304 while (count-- > 0) {
305 c[size] = *buf++;
306 if (c[size] == 0x7d)
307 c[size] = *buf++ ^ 0x20;
308 size++;
309 }
310
311 return probe_kernel_write(mem, c, size);
312}
313
314/* Write memory due to an 'M' or 'X' packet. */
315static int write_mem_msg(int binary)
316{
317 char *ptr = &remcom_in_buffer[1];
318 unsigned long addr;
319 unsigned long length;
320 int err;
321
322 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
323 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
324 if (binary)
325 err = kgdb_ebin2mem(ptr, (char *)addr, length);
326 else
327 err = kgdb_hex2mem(ptr, (char *)addr, length);
328 if (err)
329 return err;
330 if (CACHE_FLUSH_IS_SAFE)
331 flush_icache_range(addr, addr + length);
332 return 0;
333 }
334
335 return -EINVAL;
336}
337
338static void error_packet(char *pkt, int error)
339{
340 error = -error;
341 pkt[0] = 'E';
342 pkt[1] = hex_asc[(error / 10)];
343 pkt[2] = hex_asc[(error % 10)];
344 pkt[3] = '\0';
345}
346
347/*
348 * Thread ID accessors. We represent a flat TID space to GDB, where
349 * the per CPU idle threads (which under Linux all have PID 0) are
350 * remapped to negative TIDs.
351 */
352
353#define BUF_THREAD_ID_SIZE 16
354
355static char *pack_threadid(char *pkt, unsigned char *id)
356{
357 char *limit;
358
359 limit = pkt + BUF_THREAD_ID_SIZE;
360 while (pkt < limit)
361 pkt = pack_hex_byte(pkt, *id++);
362
363 return pkt;
364}
365
366static void int_to_threadref(unsigned char *id, int value)
367{
368 unsigned char *scan;
369 int i = 4;
370
371 scan = (unsigned char *)id;
372 while (i--)
373 *scan++ = 0;
374 put_unaligned_be32(value, scan);
375}
376
377static struct task_struct *getthread(struct pt_regs *regs, int tid)
378{
379 /*
380 * Non-positive TIDs are remapped to the cpu shadow information
381 */
382 if (tid == 0 || tid == -1)
383 tid = -atomic_read(&kgdb_active) - 2;
384 if (tid < -1 && tid > -NR_CPUS - 2) {
385 if (kgdb_info[-tid - 2].task)
386 return kgdb_info[-tid - 2].task;
387 else
388 return idle_task(-tid - 2);
389 }
390 if (tid <= 0) {
391 printk(KERN_ERR "KGDB: Internal thread select error\n");
392 dump_stack();
393 return NULL;
394 }
395
396 /*
397 * find_task_by_pid_ns() does not take the tasklist lock anymore
398 * but is nicely RCU locked - hence is a pretty resilient
399 * thing to use:
400 */
401 return find_task_by_pid_ns(tid, &init_pid_ns);
402}
403
404
405/*
406 * Remap normal tasks to their real PID,
407 * CPU shadow threads are mapped to -CPU - 2
408 */
409static inline int shadow_pid(int realpid)
410{
411 if (realpid)
412 return realpid;
413
414 return -raw_smp_processor_id() - 2;
415}
416
417/*
418 * All the functions that start with gdb_cmd are the various
419 * operations to implement the handlers for the gdbserial protocol
420 * where KGDB is communicating with an external debugger
421 */
422
423/* Handle the '?' status packets */
424static void gdb_cmd_status(struct kgdb_state *ks)
425{
426 /*
427 * We know that this packet is only sent
428 * during initial connect. So to be safe,
429 * we clear out our breakpoints now in case
430 * GDB is reconnecting.
431 */
432 dbg_remove_all_break();
433
434 remcom_out_buffer[0] = 'S';
435 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
436}
437
438/* Handle the 'g' get registers request */
439static void gdb_cmd_getregs(struct kgdb_state *ks)
440{
441 struct task_struct *thread;
442 void *local_debuggerinfo;
443 int i;
444
445 thread = kgdb_usethread;
446 if (!thread) {
447 thread = kgdb_info[ks->cpu].task;
448 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
449 } else {
450 local_debuggerinfo = NULL;
451 for_each_online_cpu(i) {
452 /*
453 * Try to find the task on some other
454 * or possibly this node if we do not
455 * find the matching task then we try
456 * to approximate the results.
457 */
458 if (thread == kgdb_info[i].task)
459 local_debuggerinfo = kgdb_info[i].debuggerinfo;
460 }
461 }
462
463 /*
464 * All threads that don't have debuggerinfo should be
465 * in schedule() sleeping, since all other CPUs
466 * are in kgdb_wait, and thus have debuggerinfo.
467 */
468 if (local_debuggerinfo) {
469 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
470 } else {
471 /*
472 * Pull stuff saved during switch_to; nothing
473 * else is accessible (or even particularly
474 * relevant).
475 *
476 * This should be enough for a stack trace.
477 */
478 sleeping_thread_to_gdb_regs(gdb_regs, thread);
479 }
480 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
481}
482
483/* Handle the 'G' set registers request */
484static void gdb_cmd_setregs(struct kgdb_state *ks)
485{
486 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
487
488 if (kgdb_usethread && kgdb_usethread != current) {
489 error_packet(remcom_out_buffer, -EINVAL);
490 } else {
491 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
492 strcpy(remcom_out_buffer, "OK");
493 }
494}
495
496/* Handle the 'm' memory read bytes */
497static void gdb_cmd_memread(struct kgdb_state *ks)
498{
499 char *ptr = &remcom_in_buffer[1];
500 unsigned long length;
501 unsigned long addr;
502 int err;
503
504 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
505 kgdb_hex2long(&ptr, &length) > 0) {
506 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
507 if (err)
508 error_packet(remcom_out_buffer, err);
509 } else {
510 error_packet(remcom_out_buffer, -EINVAL);
511 }
512}
513
514/* Handle the 'M' memory write bytes */
515static void gdb_cmd_memwrite(struct kgdb_state *ks)
516{
517 int err = write_mem_msg(0);
518
519 if (err)
520 error_packet(remcom_out_buffer, err);
521 else
522 strcpy(remcom_out_buffer, "OK");
523}
524
525/* Handle the 'X' memory binary write bytes */
526static void gdb_cmd_binwrite(struct kgdb_state *ks)
527{
528 int err = write_mem_msg(1);
529
530 if (err)
531 error_packet(remcom_out_buffer, err);
532 else
533 strcpy(remcom_out_buffer, "OK");
534}
535
536/* Handle the 'D' or 'k', detach or kill packets */
537static void gdb_cmd_detachkill(struct kgdb_state *ks)
538{
539 int error;
540
541 /* The detach case */
542 if (remcom_in_buffer[0] == 'D') {
543 error = dbg_remove_all_break();
544 if (error < 0) {
545 error_packet(remcom_out_buffer, error);
546 } else {
547 strcpy(remcom_out_buffer, "OK");
548 kgdb_connected = 0;
549 }
550 put_packet(remcom_out_buffer);
551 } else {
552 /*
553 * Assume the kill case, with no exit code checking,
554 * trying to force detach the debugger:
555 */
556 dbg_remove_all_break();
557 kgdb_connected = 0;
558 }
559}
560
561/* Handle the 'R' reboot packets */
562static int gdb_cmd_reboot(struct kgdb_state *ks)
563{
564 /* For now, only honor R0 */
565 if (strcmp(remcom_in_buffer, "R0") == 0) {
566 printk(KERN_CRIT "Executing emergency reboot\n");
567 strcpy(remcom_out_buffer, "OK");
568 put_packet(remcom_out_buffer);
569
570 /*
571 * Execution should not return from
572 * machine_emergency_restart()
573 */
574 machine_emergency_restart();
575 kgdb_connected = 0;
576
577 return 1;
578 }
579 return 0;
580}
581
582/* Handle the 'q' query packets */
583static void gdb_cmd_query(struct kgdb_state *ks)
584{
585 struct task_struct *g;
586 struct task_struct *p;
587 unsigned char thref[8];
588 char *ptr;
589 int i;
590 int cpu;
591 int finished = 0;
592
593 switch (remcom_in_buffer[1]) {
594 case 's':
595 case 'f':
596 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
597 error_packet(remcom_out_buffer, -EINVAL);
598 break;
599 }
600
601 i = 0;
602 remcom_out_buffer[0] = 'm';
603 ptr = remcom_out_buffer + 1;
604 if (remcom_in_buffer[1] == 'f') {
605 /* Each cpu is a shadow thread */
606 for_each_online_cpu(cpu) {
607 ks->thr_query = 0;
608 int_to_threadref(thref, -cpu - 2);
609 pack_threadid(ptr, thref);
610 ptr += BUF_THREAD_ID_SIZE;
611 *(ptr++) = ',';
612 i++;
613 }
614 }
615
616 do_each_thread(g, p) {
617 if (i >= ks->thr_query && !finished) {
618 int_to_threadref(thref, p->pid);
619 pack_threadid(ptr, thref);
620 ptr += BUF_THREAD_ID_SIZE;
621 *(ptr++) = ',';
622 ks->thr_query++;
623 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
624 finished = 1;
625 }
626 i++;
627 } while_each_thread(g, p);
628
629 *(--ptr) = '\0';
630 break;
631
632 case 'C':
633 /* Current thread id */
634 strcpy(remcom_out_buffer, "QC");
635 ks->threadid = shadow_pid(current->pid);
636 int_to_threadref(thref, ks->threadid);
637 pack_threadid(remcom_out_buffer + 2, thref);
638 break;
639 case 'T':
640 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
641 error_packet(remcom_out_buffer, -EINVAL);
642 break;
643 }
644 ks->threadid = 0;
645 ptr = remcom_in_buffer + 17;
646 kgdb_hex2long(&ptr, &ks->threadid);
647 if (!getthread(ks->linux_regs, ks->threadid)) {
648 error_packet(remcom_out_buffer, -EINVAL);
649 break;
650 }
651 if ((int)ks->threadid > 0) {
652 kgdb_mem2hex(getthread(ks->linux_regs,
653 ks->threadid)->comm,
654 remcom_out_buffer, 16);
655 } else {
656 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
657
658 sprintf(tmpstr, "shadowCPU%d",
659 (int)(-ks->threadid - 2));
660 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
661 }
662 break;
663 }
664}
665
666/* Handle the 'H' task query packets */
667static void gdb_cmd_task(struct kgdb_state *ks)
668{
669 struct task_struct *thread;
670 char *ptr;
671
672 switch (remcom_in_buffer[1]) {
673 case 'g':
674 ptr = &remcom_in_buffer[2];
675 kgdb_hex2long(&ptr, &ks->threadid);
676 thread = getthread(ks->linux_regs, ks->threadid);
677 if (!thread && ks->threadid > 0) {
678 error_packet(remcom_out_buffer, -EINVAL);
679 break;
680 }
681 kgdb_usethread = thread;
682 ks->kgdb_usethreadid = ks->threadid;
683 strcpy(remcom_out_buffer, "OK");
684 break;
685 case 'c':
686 ptr = &remcom_in_buffer[2];
687 kgdb_hex2long(&ptr, &ks->threadid);
688 if (!ks->threadid) {
689 kgdb_contthread = NULL;
690 } else {
691 thread = getthread(ks->linux_regs, ks->threadid);
692 if (!thread && ks->threadid > 0) {
693 error_packet(remcom_out_buffer, -EINVAL);
694 break;
695 }
696 kgdb_contthread = thread;
697 }
698 strcpy(remcom_out_buffer, "OK");
699 break;
700 }
701}
702
703/* Handle the 'T' thread query packets */
704static void gdb_cmd_thread(struct kgdb_state *ks)
705{
706 char *ptr = &remcom_in_buffer[1];
707 struct task_struct *thread;
708
709 kgdb_hex2long(&ptr, &ks->threadid);
710 thread = getthread(ks->linux_regs, ks->threadid);
711 if (thread)
712 strcpy(remcom_out_buffer, "OK");
713 else
714 error_packet(remcom_out_buffer, -EINVAL);
715}
716
717/* Handle the 'z' or 'Z' breakpoint remove or set packets */
718static void gdb_cmd_break(struct kgdb_state *ks)
719{
720 /*
721 * Since GDB-5.3, it's been drafted that '0' is a software
722 * breakpoint, '1' is a hardware breakpoint, so let's do that.
723 */
724 char *bpt_type = &remcom_in_buffer[1];
725 char *ptr = &remcom_in_buffer[2];
726 unsigned long addr;
727 unsigned long length;
728 int error = 0;
729
730 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
731 /* Unsupported */
732 if (*bpt_type > '4')
733 return;
734 } else {
735 if (*bpt_type != '0' && *bpt_type != '1')
736 /* Unsupported. */
737 return;
738 }
739
740 /*
741 * Test if this is a hardware breakpoint, and
742 * if we support it:
743 */
744 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
745 /* Unsupported. */
746 return;
747
748 if (*(ptr++) != ',') {
749 error_packet(remcom_out_buffer, -EINVAL);
750 return;
751 }
752 if (!kgdb_hex2long(&ptr, &addr)) {
753 error_packet(remcom_out_buffer, -EINVAL);
754 return;
755 }
756 if (*(ptr++) != ',' ||
757 !kgdb_hex2long(&ptr, &length)) {
758 error_packet(remcom_out_buffer, -EINVAL);
759 return;
760 }
761
762 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
763 error = dbg_set_sw_break(addr);
764 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
765 error = dbg_remove_sw_break(addr);
766 else if (remcom_in_buffer[0] == 'Z')
767 error = arch_kgdb_ops.set_hw_breakpoint(addr,
768 (int)length, *bpt_type - '0');
769 else if (remcom_in_buffer[0] == 'z')
770 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
771 (int) length, *bpt_type - '0');
772
773 if (error == 0)
774 strcpy(remcom_out_buffer, "OK");
775 else
776 error_packet(remcom_out_buffer, error);
777}
778
779/* Handle the 'C' signal / exception passing packets */
780static int gdb_cmd_exception_pass(struct kgdb_state *ks)
781{
782 /* C09 == pass exception
783 * C15 == detach kgdb, pass exception
784 */
785 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
786
787 ks->pass_exception = 1;
788 remcom_in_buffer[0] = 'c';
789
790 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
791
792 ks->pass_exception = 1;
793 remcom_in_buffer[0] = 'D';
794 dbg_remove_all_break();
795 kgdb_connected = 0;
796 return 1;
797
798 } else {
799 gdbstub_msg_write("KGDB only knows signal 9 (pass)"
800 " and 15 (pass and disconnect)\n"
801 "Executing a continue without signal passing\n", 0);
802 remcom_in_buffer[0] = 'c';
803 }
804
805 /* Indicate fall through */
806 return -1;
807}
808
809/*
810 * This function performs all gdbserial command procesing
811 */
812int gdb_serial_stub(struct kgdb_state *ks)
813{
814 int error = 0;
815 int tmp;
816
817 /* Clear the out buffer. */
818 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
819
820 if (kgdb_connected) {
821 unsigned char thref[8];
822 char *ptr;
823
824 /* Reply to host that an exception has occurred */
825 ptr = remcom_out_buffer;
826 *ptr++ = 'T';
827 ptr = pack_hex_byte(ptr, ks->signo);
828 ptr += strlen(strcpy(ptr, "thread:"));
829 int_to_threadref(thref, shadow_pid(current->pid));
830 ptr = pack_threadid(ptr, thref);
831 *ptr++ = ';';
832 put_packet(remcom_out_buffer);
833 }
834
835 kgdb_usethread = kgdb_info[ks->cpu].task;
836 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
837 ks->pass_exception = 0;
838
839 while (1) {
840 error = 0;
841
842 /* Clear the out buffer. */
843 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
844
845 get_packet(remcom_in_buffer);
846
847 switch (remcom_in_buffer[0]) {
848 case '?': /* gdbserial status */
849 gdb_cmd_status(ks);
850 break;
851 case 'g': /* return the value of the CPU registers */
852 gdb_cmd_getregs(ks);
853 break;
854 case 'G': /* set the value of the CPU registers - return OK */
855 gdb_cmd_setregs(ks);
856 break;
857 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
858 gdb_cmd_memread(ks);
859 break;
860 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
861 gdb_cmd_memwrite(ks);
862 break;
863 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
864 gdb_cmd_binwrite(ks);
865 break;
866 /* kill or detach. KGDB should treat this like a
867 * continue.
868 */
869 case 'D': /* Debugger detach */
870 case 'k': /* Debugger detach via kill */
871 gdb_cmd_detachkill(ks);
872 goto default_handle;
873 case 'R': /* Reboot */
874 if (gdb_cmd_reboot(ks))
875 goto default_handle;
876 break;
877 case 'q': /* query command */
878 gdb_cmd_query(ks);
879 break;
880 case 'H': /* task related */
881 gdb_cmd_task(ks);
882 break;
883 case 'T': /* Query thread status */
884 gdb_cmd_thread(ks);
885 break;
886 case 'z': /* Break point remove */
887 case 'Z': /* Break point set */
888 gdb_cmd_break(ks);
889 break;
890 case 'C': /* Exception passing */
891 tmp = gdb_cmd_exception_pass(ks);
892 if (tmp > 0)
893 goto default_handle;
894 if (tmp == 0)
895 break;
896 /* Fall through on tmp < 0 */
897 case 'c': /* Continue packet */
898 case 's': /* Single step packet */
899 if (kgdb_contthread && kgdb_contthread != current) {
900 /* Can't switch threads in kgdb */
901 error_packet(remcom_out_buffer, -EINVAL);
902 break;
903 }
904 dbg_activate_sw_breakpoints();
905 /* Fall through to default processing */
906 default:
907default_handle:
908 error = kgdb_arch_handle_exception(ks->ex_vector,
909 ks->signo,
910 ks->err_code,
911 remcom_in_buffer,
912 remcom_out_buffer,
913 ks->linux_regs);
914 /*
915 * Leave cmd processing on error, detach,
916 * kill, continue, or single step.
917 */
918 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
919 remcom_in_buffer[0] == 'k') {
920 error = 0;
921 goto kgdb_exit;
922 }
923
924 }
925
926 /* reply to the request */
927 put_packet(remcom_out_buffer);
928 }
929
930kgdb_exit:
931 if (ks->pass_exception)
932 error = 1;
933 return error;
934}