4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
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-2008 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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.
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
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.
30 #include <linux/pid_namespace.h>
31 #include <linux/clocksource.h>
32 #include <linux/interrupt.h>
33 #include <linux/spinlock.h>
34 #include <linux/console.h>
35 #include <linux/threads.h>
36 #include <linux/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/ptrace.h>
40 #include <linux/reboot.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/sysrq.h>
45 #include <linux/init.h>
46 #include <linux/kgdb.h>
47 #include <linux/pid.h>
48 #include <linux/smp.h>
51 #include <asm/cacheflush.h>
52 #include <asm/byteorder.h>
53 #include <asm/atomic.h>
54 #include <asm/system.h>
56 static int kgdb_break_asap;
65 long kgdb_usethreadid;
66 struct pt_regs *linux_regs;
69 static struct debuggerinfo_struct {
71 struct task_struct *task;
75 * kgdb_connected - Is a host GDB connected to us?
78 EXPORT_SYMBOL_GPL(kgdb_connected);
80 /* All the KGDB handlers are installed */
81 static int kgdb_io_module_registered;
83 /* Guard for recursive entry */
84 static int exception_level;
86 static struct kgdb_io *kgdb_io_ops;
87 static DEFINE_SPINLOCK(kgdb_registration_lock);
89 /* kgdb console driver is loaded */
90 static int kgdb_con_registered;
91 /* determine if kgdb console output should be used */
92 static int kgdb_use_con;
94 static int __init opt_kgdb_con(char *str)
100 early_param("kgdbcon", opt_kgdb_con);
102 module_param(kgdb_use_con, int, 0644);
105 * Holds information about breakpoints in a kernel. These breakpoints are
106 * added and removed by gdb.
108 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
109 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
113 * The CPU# of the active CPU, or -1 if none:
115 atomic_t kgdb_active = ATOMIC_INIT(-1);
118 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
119 * bootup code (which might not have percpu set up yet):
121 static atomic_t passive_cpu_wait[NR_CPUS];
122 static atomic_t cpu_in_kgdb[NR_CPUS];
123 atomic_t kgdb_setting_breakpoint;
125 struct task_struct *kgdb_usethread;
126 struct task_struct *kgdb_contthread;
128 int kgdb_single_step;
130 /* Our I/O buffers. */
131 static char remcom_in_buffer[BUFMAX];
132 static char remcom_out_buffer[BUFMAX];
134 /* Storage for the registers, in GDB format. */
135 static unsigned long gdb_regs[(NUMREGBYTES +
136 sizeof(unsigned long) - 1) /
137 sizeof(unsigned long)];
139 /* to keep track of the CPU which is doing the single stepping*/
140 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
143 * If you are debugging a problem where roundup (the collection of
144 * all other CPUs) is a problem [this should be extremely rare],
145 * then use the nokgdbroundup option to avoid roundup. In that case
146 * the other CPUs might interfere with your debugging context, so
147 * use this with care:
149 int kgdb_do_roundup = 1;
151 static int __init opt_nokgdbroundup(char *str)
158 early_param("nokgdbroundup", opt_nokgdbroundup);
161 * Finally, some KGDB code :-)
165 * Weak aliases for breakpoint management,
166 * can be overriden by architectures when needed:
168 int __weak kgdb_validate_break_address(unsigned long addr)
170 char tmp_variable[BREAK_INSTR_SIZE];
172 return probe_kernel_read(tmp_variable, (char *)addr, BREAK_INSTR_SIZE);
175 int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
179 err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
183 return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
187 int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
189 return probe_kernel_write((char *)addr,
190 (char *)bundle, BREAK_INSTR_SIZE);
193 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
195 return instruction_pointer(regs);
198 int __weak kgdb_arch_init(void)
203 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
209 kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
215 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
216 * @regs: Current &struct pt_regs.
218 * This function will be called if the particular architecture must
219 * disable hardware debugging while it is processing gdb packets or
220 * handling exception.
222 void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
227 * GDB remote protocol parser:
230 static const char hexchars[] = "0123456789abcdef";
232 static int hex(char ch)
234 if ((ch >= 'a') && (ch <= 'f'))
235 return ch - 'a' + 10;
236 if ((ch >= '0') && (ch <= '9'))
238 if ((ch >= 'A') && (ch <= 'F'))
239 return ch - 'A' + 10;
243 /* scan for the sequence $<data>#<checksum> */
244 static void get_packet(char *buffer)
246 unsigned char checksum;
247 unsigned char xmitcsum;
253 * Spin and wait around for the start character, ignore all
256 while ((ch = (kgdb_io_ops->read_char())) != '$')
266 * now, read until a # or end of buffer is found:
268 while (count < (BUFMAX - 1)) {
269 ch = kgdb_io_ops->read_char();
272 checksum = checksum + ch;
279 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
280 xmitcsum += hex(kgdb_io_ops->read_char());
282 if (checksum != xmitcsum)
283 /* failed checksum */
284 kgdb_io_ops->write_char('-');
286 /* successful transfer */
287 kgdb_io_ops->write_char('+');
288 if (kgdb_io_ops->flush)
289 kgdb_io_ops->flush();
291 } while (checksum != xmitcsum);
295 * Send the packet in buffer.
296 * Check for gdb connection if asked for.
298 static void put_packet(char *buffer)
300 unsigned char checksum;
305 * $<packet info>#<checksum>.
308 kgdb_io_ops->write_char('$');
312 while ((ch = buffer[count])) {
313 kgdb_io_ops->write_char(ch);
318 kgdb_io_ops->write_char('#');
319 kgdb_io_ops->write_char(hexchars[checksum >> 4]);
320 kgdb_io_ops->write_char(hexchars[checksum & 0xf]);
321 if (kgdb_io_ops->flush)
322 kgdb_io_ops->flush();
324 /* Now see what we get in reply. */
325 ch = kgdb_io_ops->read_char();
328 ch = kgdb_io_ops->read_char();
330 /* If we get an ACK, we are done. */
335 * If we get the start of another packet, this means
336 * that GDB is attempting to reconnect. We will NAK
337 * the packet being sent, and stop trying to send this
341 kgdb_io_ops->write_char('-');
342 if (kgdb_io_ops->flush)
343 kgdb_io_ops->flush();
349 static char *pack_hex_byte(char *pkt, u8 byte)
351 *pkt++ = hexchars[byte >> 4];
352 *pkt++ = hexchars[byte & 0xf];
358 * Convert the memory pointed to by mem into hex, placing result in buf.
359 * Return a pointer to the last char put in buf (null). May return an error.
361 int kgdb_mem2hex(char *mem, char *buf, int count)
367 * We use the upper half of buf as an intermediate buffer for the
368 * raw memory copy. Hex conversion will work against this one.
372 err = probe_kernel_read(tmp, mem, count);
375 buf = pack_hex_byte(buf, *tmp);
387 * Copy the binary array pointed to by buf into mem. Fix $, #, and
388 * 0x7d escaped with 0x7d. Return a pointer to the character after
389 * the last byte written.
391 static int kgdb_ebin2mem(char *buf, char *mem, int count)
396 while (count-- > 0) {
401 err = probe_kernel_write(mem, &c, 1);
412 * Convert the hex array pointed to by buf into binary to be placed in mem.
413 * Return a pointer to the character AFTER the last byte written.
414 * May return an error.
416 int kgdb_hex2mem(char *buf, char *mem, int count)
422 * We use the upper half of buf as an intermediate buffer for the
423 * raw memory that is converted from hex.
425 tmp_raw = buf + count * 2;
427 tmp_hex = tmp_raw - 1;
428 while (tmp_hex >= buf) {
430 *tmp_raw = hex(*tmp_hex--);
431 *tmp_raw |= hex(*tmp_hex--) << 4;
434 return probe_kernel_write(mem, tmp_raw, count);
438 * While we find nice hex chars, build a long_val.
439 * Return number of chars processed.
441 int kgdb_hex2long(char **ptr, long *long_val)
449 hex_val = hex(**ptr);
453 *long_val = (*long_val << 4) | hex_val;
461 /* Write memory due to an 'M' or 'X' packet. */
462 static int write_mem_msg(int binary)
464 char *ptr = &remcom_in_buffer[1];
466 unsigned long length;
469 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
470 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
472 err = kgdb_ebin2mem(ptr, (char *)addr, length);
474 err = kgdb_hex2mem(ptr, (char *)addr, length);
477 if (CACHE_FLUSH_IS_SAFE)
478 flush_icache_range(addr, addr + length + 1);
485 static void error_packet(char *pkt, int error)
489 pkt[1] = hexchars[(error / 10)];
490 pkt[2] = hexchars[(error % 10)];
495 * Thread ID accessors. We represent a flat TID space to GDB, where
496 * the per CPU idle threads (which under Linux all have PID 0) are
497 * remapped to negative TIDs.
500 #define BUF_THREAD_ID_SIZE 16
502 static char *pack_threadid(char *pkt, unsigned char *id)
506 limit = pkt + BUF_THREAD_ID_SIZE;
508 pkt = pack_hex_byte(pkt, *id++);
513 static void int_to_threadref(unsigned char *id, int value)
518 scan = (unsigned char *)id;
521 *scan++ = (value >> 24) & 0xff;
522 *scan++ = (value >> 16) & 0xff;
523 *scan++ = (value >> 8) & 0xff;
524 *scan++ = (value & 0xff);
527 static struct task_struct *getthread(struct pt_regs *regs, int tid)
530 * Non-positive TIDs are remapped idle tasks:
533 return idle_task(-tid);
536 * find_task_by_pid_ns() does not take the tasklist lock anymore
537 * but is nicely RCU locked - hence is a pretty resilient
540 return find_task_by_pid_ns(tid, &init_pid_ns);
544 * CPU debug state control:
548 static void kgdb_wait(struct pt_regs *regs)
553 local_irq_save(flags);
554 cpu = raw_smp_processor_id();
555 kgdb_info[cpu].debuggerinfo = regs;
556 kgdb_info[cpu].task = current;
558 * Make sure the above info reaches the primary CPU before
559 * our cpu_in_kgdb[] flag setting does:
562 atomic_set(&cpu_in_kgdb[cpu], 1);
565 * The primary CPU must be active to enter here, but this is
566 * guard in case the primary CPU had not been selected if
567 * this was an entry via nmi.
569 while (atomic_read(&kgdb_active) == -1)
572 /* Wait till primary CPU goes completely into the debugger. */
573 while (!atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)]))
576 /* Wait till primary CPU is done with debugging */
577 while (atomic_read(&passive_cpu_wait[cpu]))
580 kgdb_info[cpu].debuggerinfo = NULL;
581 kgdb_info[cpu].task = NULL;
583 /* fix up hardware debug registers on local cpu */
584 if (arch_kgdb_ops.correct_hw_break)
585 arch_kgdb_ops.correct_hw_break();
587 /* Signal the primary CPU that we are done: */
588 atomic_set(&cpu_in_kgdb[cpu], 0);
589 clocksource_touch_watchdog();
590 local_irq_restore(flags);
595 * Some architectures need cache flushes when we set/clear a
598 static void kgdb_flush_swbreak_addr(unsigned long addr)
600 if (!CACHE_FLUSH_IS_SAFE)
603 if (current->mm && current->mm->mmap_cache) {
604 flush_cache_range(current->mm->mmap_cache,
605 addr, addr + BREAK_INSTR_SIZE);
607 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
612 * SW breakpoint management:
614 static int kgdb_activate_sw_breakpoints(void)
620 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
621 if (kgdb_break[i].state != BP_SET)
624 addr = kgdb_break[i].bpt_addr;
625 error = kgdb_arch_set_breakpoint(addr,
626 kgdb_break[i].saved_instr);
630 kgdb_flush_swbreak_addr(addr);
631 kgdb_break[i].state = BP_ACTIVE;
636 static int kgdb_set_sw_break(unsigned long addr)
638 int err = kgdb_validate_break_address(addr);
645 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
646 if ((kgdb_break[i].state == BP_SET) &&
647 (kgdb_break[i].bpt_addr == addr))
650 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
651 if (kgdb_break[i].state == BP_REMOVED &&
652 kgdb_break[i].bpt_addr == addr) {
659 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
660 if (kgdb_break[i].state == BP_UNDEFINED) {
670 kgdb_break[breakno].state = BP_SET;
671 kgdb_break[breakno].type = BP_BREAKPOINT;
672 kgdb_break[breakno].bpt_addr = addr;
677 static int kgdb_deactivate_sw_breakpoints(void)
683 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
684 if (kgdb_break[i].state != BP_ACTIVE)
686 addr = kgdb_break[i].bpt_addr;
687 error = kgdb_arch_remove_breakpoint(addr,
688 kgdb_break[i].saved_instr);
692 kgdb_flush_swbreak_addr(addr);
693 kgdb_break[i].state = BP_SET;
698 static int kgdb_remove_sw_break(unsigned long addr)
702 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
703 if ((kgdb_break[i].state == BP_SET) &&
704 (kgdb_break[i].bpt_addr == addr)) {
705 kgdb_break[i].state = BP_REMOVED;
712 int kgdb_isremovedbreak(unsigned long addr)
716 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
717 if ((kgdb_break[i].state == BP_REMOVED) &&
718 (kgdb_break[i].bpt_addr == addr))
724 int remove_all_break(void)
730 /* Clear memory breakpoints. */
731 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
732 if (kgdb_break[i].state != BP_ACTIVE)
734 addr = kgdb_break[i].bpt_addr;
735 error = kgdb_arch_remove_breakpoint(addr,
736 kgdb_break[i].saved_instr);
738 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
741 kgdb_break[i].state = BP_UNDEFINED;
744 /* Clear hardware breakpoints. */
745 if (arch_kgdb_ops.remove_all_hw_break)
746 arch_kgdb_ops.remove_all_hw_break();
752 * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
754 static inline int shadow_pid(int realpid)
759 return -1-raw_smp_processor_id();
762 static char gdbmsgbuf[BUFMAX + 1];
764 static void kgdb_msg_write(const char *s, int len)
773 /* Fill and send buffers... */
775 bufptr = gdbmsgbuf + 1;
777 /* Calculate how many this time */
778 if ((len << 1) > (BUFMAX - 2))
779 wcount = (BUFMAX - 2) >> 1;
783 /* Pack in hex chars */
784 for (i = 0; i < wcount; i++)
785 bufptr = pack_hex_byte(bufptr, s[i]);
793 put_packet(gdbmsgbuf);
798 * Return true if there is a valid kgdb I/O module. Also if no
799 * debugger is attached a message can be printed to the console about
800 * waiting for the debugger to attach.
802 * The print_wait argument is only to be true when called from inside
803 * the core kgdb_handle_exception, because it will wait for the
804 * debugger to attach.
806 static int kgdb_io_ready(int print_wait)
812 if (atomic_read(&kgdb_setting_breakpoint))
815 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
820 * All the functions that start with gdb_cmd are the various
821 * operations to implement the handlers for the gdbserial protocol
822 * where KGDB is communicating with an external debugger
825 /* Handle the '?' status packets */
826 static void gdb_cmd_status(struct kgdb_state *ks)
829 * We know that this packet is only sent
830 * during initial connect. So to be safe,
831 * we clear out our breakpoints now in case
832 * GDB is reconnecting.
836 remcom_out_buffer[0] = 'S';
837 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
840 /* Handle the 'g' get registers request */
841 static void gdb_cmd_getregs(struct kgdb_state *ks)
843 struct task_struct *thread;
844 void *local_debuggerinfo;
847 thread = kgdb_usethread;
849 thread = kgdb_info[ks->cpu].task;
850 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
852 local_debuggerinfo = NULL;
853 for (i = 0; i < NR_CPUS; i++) {
855 * Try to find the task on some other
856 * or possibly this node if we do not
857 * find the matching task then we try
858 * to approximate the results.
860 if (thread == kgdb_info[i].task)
861 local_debuggerinfo = kgdb_info[i].debuggerinfo;
866 * All threads that don't have debuggerinfo should be
867 * in __schedule() sleeping, since all other CPUs
868 * are in kgdb_wait, and thus have debuggerinfo.
870 if (local_debuggerinfo) {
871 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
874 * Pull stuff saved during switch_to; nothing
875 * else is accessible (or even particularly
878 * This should be enough for a stack trace.
880 sleeping_thread_to_gdb_regs(gdb_regs, thread);
882 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
885 /* Handle the 'G' set registers request */
886 static void gdb_cmd_setregs(struct kgdb_state *ks)
888 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
890 if (kgdb_usethread && kgdb_usethread != current) {
891 error_packet(remcom_out_buffer, -EINVAL);
893 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
894 strcpy(remcom_out_buffer, "OK");
898 /* Handle the 'm' memory read bytes */
899 static void gdb_cmd_memread(struct kgdb_state *ks)
901 char *ptr = &remcom_in_buffer[1];
902 unsigned long length;
906 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
907 kgdb_hex2long(&ptr, &length) > 0) {
908 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
910 error_packet(remcom_out_buffer, err);
912 error_packet(remcom_out_buffer, -EINVAL);
916 /* Handle the 'M' memory write bytes */
917 static void gdb_cmd_memwrite(struct kgdb_state *ks)
919 int err = write_mem_msg(0);
922 error_packet(remcom_out_buffer, err);
924 strcpy(remcom_out_buffer, "OK");
927 /* Handle the 'X' memory binary write bytes */
928 static void gdb_cmd_binwrite(struct kgdb_state *ks)
930 int err = write_mem_msg(1);
933 error_packet(remcom_out_buffer, err);
935 strcpy(remcom_out_buffer, "OK");
938 /* Handle the 'D' or 'k', detach or kill packets */
939 static void gdb_cmd_detachkill(struct kgdb_state *ks)
943 /* The detach case */
944 if (remcom_in_buffer[0] == 'D') {
945 error = remove_all_break();
947 error_packet(remcom_out_buffer, error);
949 strcpy(remcom_out_buffer, "OK");
952 put_packet(remcom_out_buffer);
955 * Assume the kill case, with no exit code checking,
956 * trying to force detach the debugger:
963 /* Handle the 'R' reboot packets */
964 static int gdb_cmd_reboot(struct kgdb_state *ks)
966 /* For now, only honor R0 */
967 if (strcmp(remcom_in_buffer, "R0") == 0) {
968 printk(KERN_CRIT "Executing emergency reboot\n");
969 strcpy(remcom_out_buffer, "OK");
970 put_packet(remcom_out_buffer);
973 * Execution should not return from
974 * machine_emergency_restart()
976 machine_emergency_restart();
984 /* Handle the 'q' query packets */
985 static void gdb_cmd_query(struct kgdb_state *ks)
987 struct task_struct *thread;
988 unsigned char thref[8];
992 switch (remcom_in_buffer[1]) {
995 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
996 error_packet(remcom_out_buffer, -EINVAL);
1000 if (remcom_in_buffer[1] == 'f')
1003 remcom_out_buffer[0] = 'm';
1004 ptr = remcom_out_buffer + 1;
1006 for (i = 0; i < 17; ks->threadid++) {
1007 thread = getthread(ks->linux_regs, ks->threadid);
1009 int_to_threadref(thref, ks->threadid);
1010 pack_threadid(ptr, thref);
1011 ptr += BUF_THREAD_ID_SIZE;
1020 /* Current thread id */
1021 strcpy(remcom_out_buffer, "QC");
1022 ks->threadid = shadow_pid(current->pid);
1023 int_to_threadref(thref, ks->threadid);
1024 pack_threadid(remcom_out_buffer + 2, thref);
1027 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1028 error_packet(remcom_out_buffer, -EINVAL);
1032 ptr = remcom_in_buffer + 17;
1033 kgdb_hex2long(&ptr, &ks->threadid);
1034 if (!getthread(ks->linux_regs, ks->threadid)) {
1035 error_packet(remcom_out_buffer, -EINVAL);
1038 if (ks->threadid > 0) {
1039 kgdb_mem2hex(getthread(ks->linux_regs,
1040 ks->threadid)->comm,
1041 remcom_out_buffer, 16);
1043 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1045 sprintf(tmpstr, "Shadow task %d for pid 0",
1046 (int)(-ks->threadid-1));
1047 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1053 /* Handle the 'H' task query packets */
1054 static void gdb_cmd_task(struct kgdb_state *ks)
1056 struct task_struct *thread;
1059 switch (remcom_in_buffer[1]) {
1061 ptr = &remcom_in_buffer[2];
1062 kgdb_hex2long(&ptr, &ks->threadid);
1063 thread = getthread(ks->linux_regs, ks->threadid);
1064 if (!thread && ks->threadid > 0) {
1065 error_packet(remcom_out_buffer, -EINVAL);
1068 kgdb_usethread = thread;
1069 ks->kgdb_usethreadid = ks->threadid;
1070 strcpy(remcom_out_buffer, "OK");
1073 ptr = &remcom_in_buffer[2];
1074 kgdb_hex2long(&ptr, &ks->threadid);
1075 if (!ks->threadid) {
1076 kgdb_contthread = NULL;
1078 thread = getthread(ks->linux_regs, ks->threadid);
1079 if (!thread && ks->threadid > 0) {
1080 error_packet(remcom_out_buffer, -EINVAL);
1083 kgdb_contthread = thread;
1085 strcpy(remcom_out_buffer, "OK");
1090 /* Handle the 'T' thread query packets */
1091 static void gdb_cmd_thread(struct kgdb_state *ks)
1093 char *ptr = &remcom_in_buffer[1];
1094 struct task_struct *thread;
1096 kgdb_hex2long(&ptr, &ks->threadid);
1097 thread = getthread(ks->linux_regs, ks->threadid);
1099 strcpy(remcom_out_buffer, "OK");
1101 error_packet(remcom_out_buffer, -EINVAL);
1104 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1105 static void gdb_cmd_break(struct kgdb_state *ks)
1108 * Since GDB-5.3, it's been drafted that '0' is a software
1109 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1111 char *bpt_type = &remcom_in_buffer[1];
1112 char *ptr = &remcom_in_buffer[2];
1114 unsigned long length;
1117 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1119 if (*bpt_type > '4')
1122 if (*bpt_type != '0' && *bpt_type != '1')
1128 * Test if this is a hardware breakpoint, and
1131 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1135 if (*(ptr++) != ',') {
1136 error_packet(remcom_out_buffer, -EINVAL);
1139 if (!kgdb_hex2long(&ptr, &addr)) {
1140 error_packet(remcom_out_buffer, -EINVAL);
1143 if (*(ptr++) != ',' ||
1144 !kgdb_hex2long(&ptr, &length)) {
1145 error_packet(remcom_out_buffer, -EINVAL);
1149 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1150 error = kgdb_set_sw_break(addr);
1151 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1152 error = kgdb_remove_sw_break(addr);
1153 else if (remcom_in_buffer[0] == 'Z')
1154 error = arch_kgdb_ops.set_hw_breakpoint(addr,
1155 (int)length, *bpt_type - '0');
1156 else if (remcom_in_buffer[0] == 'z')
1157 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
1158 (int) length, *bpt_type - '0');
1161 strcpy(remcom_out_buffer, "OK");
1163 error_packet(remcom_out_buffer, error);
1166 /* Handle the 'C' signal / exception passing packets */
1167 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1169 /* C09 == pass exception
1170 * C15 == detach kgdb, pass exception
1172 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1174 ks->pass_exception = 1;
1175 remcom_in_buffer[0] = 'c';
1177 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1179 ks->pass_exception = 1;
1180 remcom_in_buffer[0] = 'D';
1186 error_packet(remcom_out_buffer, -EINVAL);
1190 /* Indicate fall through */
1195 * This function performs all gdbserial command procesing
1197 static int gdb_serial_stub(struct kgdb_state *ks)
1202 /* Clear the out buffer. */
1203 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1205 if (kgdb_connected) {
1206 unsigned char thref[8];
1209 /* Reply to host that an exception has occurred */
1210 ptr = remcom_out_buffer;
1212 ptr = pack_hex_byte(ptr, ks->signo);
1213 ptr += strlen(strcpy(ptr, "thread:"));
1214 int_to_threadref(thref, shadow_pid(current->pid));
1215 ptr = pack_threadid(ptr, thref);
1217 put_packet(remcom_out_buffer);
1220 kgdb_usethread = kgdb_info[ks->cpu].task;
1221 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1222 ks->pass_exception = 0;
1227 /* Clear the out buffer. */
1228 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1230 get_packet(remcom_in_buffer);
1232 switch (remcom_in_buffer[0]) {
1233 case '?': /* gdbserial status */
1236 case 'g': /* return the value of the CPU registers */
1237 gdb_cmd_getregs(ks);
1239 case 'G': /* set the value of the CPU registers - return OK */
1240 gdb_cmd_setregs(ks);
1242 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1243 gdb_cmd_memread(ks);
1245 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1246 gdb_cmd_memwrite(ks);
1248 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1249 gdb_cmd_binwrite(ks);
1251 /* kill or detach. KGDB should treat this like a
1254 case 'D': /* Debugger detach */
1255 case 'k': /* Debugger detach via kill */
1256 gdb_cmd_detachkill(ks);
1257 goto default_handle;
1258 case 'R': /* Reboot */
1259 if (gdb_cmd_reboot(ks))
1260 goto default_handle;
1262 case 'q': /* query command */
1265 case 'H': /* task related */
1268 case 'T': /* Query thread status */
1271 case 'z': /* Break point remove */
1272 case 'Z': /* Break point set */
1275 case 'C': /* Exception passing */
1276 tmp = gdb_cmd_exception_pass(ks);
1278 goto default_handle;
1281 /* Fall through on tmp < 0 */
1282 case 'c': /* Continue packet */
1283 case 's': /* Single step packet */
1284 if (kgdb_contthread && kgdb_contthread != current) {
1285 /* Can't switch threads in kgdb */
1286 error_packet(remcom_out_buffer, -EINVAL);
1289 kgdb_activate_sw_breakpoints();
1290 /* Fall through to default processing */
1293 error = kgdb_arch_handle_exception(ks->ex_vector,
1300 * Leave cmd processing on error, detach,
1301 * kill, continue, or single step.
1303 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1304 remcom_in_buffer[0] == 'k') {
1311 /* reply to the request */
1312 put_packet(remcom_out_buffer);
1316 if (ks->pass_exception)
1321 static int kgdb_reenter_check(struct kgdb_state *ks)
1325 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
1328 /* Panic on recursive debugger calls: */
1330 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
1331 kgdb_deactivate_sw_breakpoints();
1334 * If the break point removed ok at the place exception
1335 * occurred, try to recover and print a warning to the end
1336 * user because the user planted a breakpoint in a place that
1337 * KGDB needs in order to function.
1339 if (kgdb_remove_sw_break(addr) == 0) {
1340 exception_level = 0;
1341 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1342 kgdb_activate_sw_breakpoints();
1343 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
1350 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1352 if (exception_level > 1) {
1354 panic("Recursive entry to debugger");
1357 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1359 panic("Recursive entry to debugger");
1365 * kgdb_handle_exception() - main entry point from a kernel exception
1367 * Locking hierarchy:
1368 * interface locks, if any (begin_session)
1369 * kgdb lock (kgdb_active)
1372 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1374 struct kgdb_state kgdb_var;
1375 struct kgdb_state *ks = &kgdb_var;
1376 unsigned long flags;
1380 ks->cpu = raw_smp_processor_id();
1381 ks->ex_vector = evector;
1383 ks->ex_vector = evector;
1384 ks->err_code = ecode;
1385 ks->kgdb_usethreadid = 0;
1386 ks->linux_regs = regs;
1388 if (kgdb_reenter_check(ks))
1389 return 0; /* Ouch, double exception ! */
1393 * Interrupts will be restored by the 'trap return' code, except when
1396 local_irq_save(flags);
1398 cpu = raw_smp_processor_id();
1401 * Acquire the kgdb_active lock:
1403 while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1407 * Do not start the debugger connection on this CPU if the last
1408 * instance of the exception handler wanted to come into the
1409 * debugger on a different CPU via a single step
1411 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1412 atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
1414 atomic_set(&kgdb_active, -1);
1415 clocksource_touch_watchdog();
1416 local_irq_restore(flags);
1421 if (!kgdb_io_ready(1)) {
1423 goto kgdb_restore; /* No I/O connection, so resume the system */
1427 * Don't enter if we have hit a removed breakpoint.
1429 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1432 /* Call the I/O driver's pre_exception routine */
1433 if (kgdb_io_ops->pre_exception)
1434 kgdb_io_ops->pre_exception();
1436 kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1437 kgdb_info[ks->cpu].task = current;
1439 kgdb_disable_hw_debug(ks->linux_regs);
1442 * Get the passive CPU lock which will hold all the non-primary
1443 * CPU in a spin state while the debugger is active
1445 if (!kgdb_single_step || !kgdb_contthread) {
1446 for (i = 0; i < NR_CPUS; i++)
1447 atomic_set(&passive_cpu_wait[i], 1);
1451 /* Signal the other CPUs to enter kgdb_wait() */
1452 if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup)
1453 kgdb_roundup_cpus(flags);
1457 * spin_lock code is good enough as a barrier so we don't
1460 atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1463 * Wait for the other CPUs to be notified and be waiting for us:
1465 for_each_online_cpu(i) {
1466 while (!atomic_read(&cpu_in_kgdb[i]))
1471 * At this point the primary processor is completely
1472 * in the debugger and all secondary CPUs are quiescent
1474 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1475 kgdb_deactivate_sw_breakpoints();
1476 kgdb_single_step = 0;
1477 kgdb_contthread = NULL;
1478 exception_level = 0;
1480 /* Talk to debugger with gdbserial protocol */
1481 error = gdb_serial_stub(ks);
1483 /* Call the I/O driver's post_exception routine */
1484 if (kgdb_io_ops->post_exception)
1485 kgdb_io_ops->post_exception();
1487 kgdb_info[ks->cpu].debuggerinfo = NULL;
1488 kgdb_info[ks->cpu].task = NULL;
1489 atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1491 if (!kgdb_single_step || !kgdb_contthread) {
1492 for (i = NR_CPUS-1; i >= 0; i--)
1493 atomic_set(&passive_cpu_wait[i], 0);
1495 * Wait till all the CPUs have quit
1496 * from the debugger.
1498 for_each_online_cpu(i) {
1499 while (atomic_read(&cpu_in_kgdb[i]))
1505 /* Free kgdb_active */
1506 atomic_set(&kgdb_active, -1);
1507 clocksource_touch_watchdog();
1508 local_irq_restore(flags);
1513 int kgdb_nmicallback(int cpu, void *regs)
1516 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
1517 atomic_read(&kgdb_active) != cpu) {
1518 kgdb_wait((struct pt_regs *)regs);
1525 void kgdb_console_write(struct console *co, const char *s, unsigned count)
1527 unsigned long flags;
1529 /* If we're debugging, or KGDB has not connected, don't try
1531 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1534 local_irq_save(flags);
1535 kgdb_msg_write(s, count);
1536 local_irq_restore(flags);
1539 static struct console kgdbcons = {
1541 .write = kgdb_console_write,
1542 .flags = CON_PRINTBUFFER | CON_ENABLED,
1546 #ifdef CONFIG_MAGIC_SYSRQ
1547 static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1550 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1553 if (!kgdb_connected)
1554 printk(KERN_CRIT "Entering KGDB\n");
1559 static struct sysrq_key_op sysrq_gdb_op = {
1560 .handler = sysrq_handle_gdb,
1562 .action_msg = "GDB",
1566 static void kgdb_register_callbacks(void)
1568 if (!kgdb_io_module_registered) {
1569 kgdb_io_module_registered = 1;
1571 #ifdef CONFIG_MAGIC_SYSRQ
1572 register_sysrq_key('g', &sysrq_gdb_op);
1574 if (kgdb_use_con && !kgdb_con_registered) {
1575 register_console(&kgdbcons);
1576 kgdb_con_registered = 1;
1581 static void kgdb_unregister_callbacks(void)
1584 * When this routine is called KGDB should unregister from the
1585 * panic handler and clean up, making sure it is not handling any
1586 * break exceptions at the time.
1588 if (kgdb_io_module_registered) {
1589 kgdb_io_module_registered = 0;
1591 #ifdef CONFIG_MAGIC_SYSRQ
1592 unregister_sysrq_key('g', &sysrq_gdb_op);
1594 if (kgdb_con_registered) {
1595 unregister_console(&kgdbcons);
1596 kgdb_con_registered = 0;
1601 static void kgdb_initial_breakpoint(void)
1603 kgdb_break_asap = 0;
1605 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1610 * kgdb_register_io_module - register KGDB IO module
1611 * @new_kgdb_io_ops: the io ops vector
1613 * Register it with the KGDB core.
1615 int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1619 spin_lock(&kgdb_registration_lock);
1622 spin_unlock(&kgdb_registration_lock);
1624 printk(KERN_ERR "kgdb: Another I/O driver is already "
1625 "registered with KGDB.\n");
1629 if (new_kgdb_io_ops->init) {
1630 err = new_kgdb_io_ops->init();
1632 spin_unlock(&kgdb_registration_lock);
1637 kgdb_io_ops = new_kgdb_io_ops;
1639 spin_unlock(&kgdb_registration_lock);
1641 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1642 new_kgdb_io_ops->name);
1645 kgdb_register_callbacks();
1647 if (kgdb_break_asap)
1648 kgdb_initial_breakpoint();
1652 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1655 * kkgdb_unregister_io_module - unregister KGDB IO module
1656 * @old_kgdb_io_ops: the io ops vector
1658 * Unregister it with the KGDB core.
1660 void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1662 BUG_ON(kgdb_connected);
1665 * KGDB is no longer able to communicate out, so
1666 * unregister our callbacks and reset state.
1668 kgdb_unregister_callbacks();
1670 spin_lock(&kgdb_registration_lock);
1672 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1675 spin_unlock(&kgdb_registration_lock);
1678 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1679 old_kgdb_io_ops->name);
1681 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1684 * kgdb_breakpoint - generate breakpoint exception
1686 * This function will generate a breakpoint exception. It is used at the
1687 * beginning of a program to sync up with a debugger and can be used
1688 * otherwise as a quick means to stop program execution and "break" into
1691 void kgdb_breakpoint(void)
1693 atomic_set(&kgdb_setting_breakpoint, 1);
1694 wmb(); /* Sync point before breakpoint */
1695 arch_kgdb_breakpoint();
1696 wmb(); /* Sync point after breakpoint */
1697 atomic_set(&kgdb_setting_breakpoint, 0);
1699 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1701 static int __init opt_kgdb_wait(char *str)
1703 kgdb_break_asap = 1;
1705 if (kgdb_io_module_registered)
1706 kgdb_initial_breakpoint();
1711 early_param("kgdbwait", opt_kgdb_wait);