kgdb: clocksource watchdog
[linux-2.6-block.git] / kernel / kgdb.c
CommitLineData
dc7d5527
JW
1/*
2 * KGDB stub.
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-2008 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#include <linux/pid_namespace.h>
7c3078b6 31#include <linux/clocksource.h>
dc7d5527
JW
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>
49#include <linux/mm.h>
50
51#include <asm/cacheflush.h>
52#include <asm/byteorder.h>
53#include <asm/atomic.h>
54#include <asm/system.h>
55
56static int kgdb_break_asap;
57
58struct kgdb_state {
59 int ex_vector;
60 int signo;
61 int err_code;
62 int cpu;
63 int pass_exception;
64 long threadid;
65 long kgdb_usethreadid;
66 struct pt_regs *linux_regs;
67};
68
69static struct debuggerinfo_struct {
70 void *debuggerinfo;
71 struct task_struct *task;
72} kgdb_info[NR_CPUS];
73
74/**
75 * kgdb_connected - Is a host GDB connected to us?
76 */
77int kgdb_connected;
78EXPORT_SYMBOL_GPL(kgdb_connected);
79
80/* All the KGDB handlers are installed */
81static int kgdb_io_module_registered;
82
83/* Guard for recursive entry */
84static int exception_level;
85
86static struct kgdb_io *kgdb_io_ops;
87static DEFINE_SPINLOCK(kgdb_registration_lock);
88
89/* kgdb console driver is loaded */
90static int kgdb_con_registered;
91/* determine if kgdb console output should be used */
92static int kgdb_use_con;
93
94static int __init opt_kgdb_con(char *str)
95{
96 kgdb_use_con = 1;
97 return 0;
98}
99
100early_param("kgdbcon", opt_kgdb_con);
101
102module_param(kgdb_use_con, int, 0644);
103
104/*
105 * Holds information about breakpoints in a kernel. These breakpoints are
106 * added and removed by gdb.
107 */
108static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
109 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
110};
111
112/*
113 * The CPU# of the active CPU, or -1 if none:
114 */
115atomic_t kgdb_active = ATOMIC_INIT(-1);
116
117/*
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):
120 */
121static atomic_t passive_cpu_wait[NR_CPUS];
122static atomic_t cpu_in_kgdb[NR_CPUS];
123atomic_t kgdb_setting_breakpoint;
124
125struct task_struct *kgdb_usethread;
126struct task_struct *kgdb_contthread;
127
128int kgdb_single_step;
129
130/* Our I/O buffers. */
131static char remcom_in_buffer[BUFMAX];
132static char remcom_out_buffer[BUFMAX];
133
134/* Storage for the registers, in GDB format. */
135static unsigned long gdb_regs[(NUMREGBYTES +
136 sizeof(unsigned long) - 1) /
137 sizeof(unsigned long)];
138
139/* to keep track of the CPU which is doing the single stepping*/
140atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
141
142/*
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:
148 */
149int kgdb_do_roundup = 1;
150
151static int __init opt_nokgdbroundup(char *str)
152{
153 kgdb_do_roundup = 0;
154
155 return 0;
156}
157
158early_param("nokgdbroundup", opt_nokgdbroundup);
159
160/*
161 * Finally, some KGDB code :-)
162 */
163
164/*
165 * Weak aliases for breakpoint management,
166 * can be overriden by architectures when needed:
167 */
168int __weak kgdb_validate_break_address(unsigned long addr)
169{
170 char tmp_variable[BREAK_INSTR_SIZE];
171
172 return probe_kernel_read(tmp_variable, (char *)addr, BREAK_INSTR_SIZE);
173}
174
175int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
176{
177 int err;
178
179 err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
180 if (err)
181 return err;
182
183 return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
184 BREAK_INSTR_SIZE);
185}
186
187int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
188{
189 return probe_kernel_write((char *)addr,
190 (char *)bundle, BREAK_INSTR_SIZE);
191}
192
193unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
194{
195 return instruction_pointer(regs);
196}
197
198int __weak kgdb_arch_init(void)
199{
200 return 0;
201}
202
203/**
204 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
205 * @regs: Current &struct pt_regs.
206 *
207 * This function will be called if the particular architecture must
208 * disable hardware debugging while it is processing gdb packets or
209 * handling exception.
210 */
211void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
212{
213}
214
215/*
216 * GDB remote protocol parser:
217 */
218
219static const char hexchars[] = "0123456789abcdef";
220
221static int hex(char ch)
222{
223 if ((ch >= 'a') && (ch <= 'f'))
224 return ch - 'a' + 10;
225 if ((ch >= '0') && (ch <= '9'))
226 return ch - '0';
227 if ((ch >= 'A') && (ch <= 'F'))
228 return ch - 'A' + 10;
229 return -1;
230}
231
232/* scan for the sequence $<data>#<checksum> */
233static void get_packet(char *buffer)
234{
235 unsigned char checksum;
236 unsigned char xmitcsum;
237 int count;
238 char ch;
239
240 do {
241 /*
242 * Spin and wait around for the start character, ignore all
243 * other characters:
244 */
245 while ((ch = (kgdb_io_ops->read_char())) != '$')
246 /* nothing */;
247
248 kgdb_connected = 1;
249 checksum = 0;
250 xmitcsum = -1;
251
252 count = 0;
253
254 /*
255 * now, read until a # or end of buffer is found:
256 */
257 while (count < (BUFMAX - 1)) {
258 ch = kgdb_io_ops->read_char();
259 if (ch == '#')
260 break;
261 checksum = checksum + ch;
262 buffer[count] = ch;
263 count = count + 1;
264 }
265 buffer[count] = 0;
266
267 if (ch == '#') {
268 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
269 xmitcsum += hex(kgdb_io_ops->read_char());
270
271 if (checksum != xmitcsum)
272 /* failed checksum */
273 kgdb_io_ops->write_char('-');
274 else
275 /* successful transfer */
276 kgdb_io_ops->write_char('+');
277 if (kgdb_io_ops->flush)
278 kgdb_io_ops->flush();
279 }
280 } while (checksum != xmitcsum);
281}
282
283/*
284 * Send the packet in buffer.
285 * Check for gdb connection if asked for.
286 */
287static void put_packet(char *buffer)
288{
289 unsigned char checksum;
290 int count;
291 char ch;
292
293 /*
294 * $<packet info>#<checksum>.
295 */
296 while (1) {
297 kgdb_io_ops->write_char('$');
298 checksum = 0;
299 count = 0;
300
301 while ((ch = buffer[count])) {
302 kgdb_io_ops->write_char(ch);
303 checksum += ch;
304 count++;
305 }
306
307 kgdb_io_ops->write_char('#');
308 kgdb_io_ops->write_char(hexchars[checksum >> 4]);
309 kgdb_io_ops->write_char(hexchars[checksum & 0xf]);
310 if (kgdb_io_ops->flush)
311 kgdb_io_ops->flush();
312
313 /* Now see what we get in reply. */
314 ch = kgdb_io_ops->read_char();
315
316 if (ch == 3)
317 ch = kgdb_io_ops->read_char();
318
319 /* If we get an ACK, we are done. */
320 if (ch == '+')
321 return;
322
323 /*
324 * If we get the start of another packet, this means
325 * that GDB is attempting to reconnect. We will NAK
326 * the packet being sent, and stop trying to send this
327 * packet.
328 */
329 if (ch == '$') {
330 kgdb_io_ops->write_char('-');
331 if (kgdb_io_ops->flush)
332 kgdb_io_ops->flush();
333 return;
334 }
335 }
336}
337
338static char *pack_hex_byte(char *pkt, u8 byte)
339{
340 *pkt++ = hexchars[byte >> 4];
341 *pkt++ = hexchars[byte & 0xf];
342
343 return pkt;
344}
345
346/*
347 * Convert the memory pointed to by mem into hex, placing result in buf.
348 * Return a pointer to the last char put in buf (null). May return an error.
349 */
350int kgdb_mem2hex(char *mem, char *buf, int count)
351{
352 char *tmp;
353 int err;
354
355 /*
356 * We use the upper half of buf as an intermediate buffer for the
357 * raw memory copy. Hex conversion will work against this one.
358 */
359 tmp = buf + count;
360
361 err = probe_kernel_read(tmp, mem, count);
362 if (!err) {
363 while (count > 0) {
364 buf = pack_hex_byte(buf, *tmp);
365 tmp++;
366 count--;
367 }
368
369 *buf = 0;
370 }
371
372 return err;
373}
374
375/*
376 * Copy the binary array pointed to by buf into mem. Fix $, #, and
377 * 0x7d escaped with 0x7d. Return a pointer to the character after
378 * the last byte written.
379 */
380static int kgdb_ebin2mem(char *buf, char *mem, int count)
381{
382 int err = 0;
383 char c;
384
385 while (count-- > 0) {
386 c = *buf++;
387 if (c == 0x7d)
388 c = *buf++ ^ 0x20;
389
390 err = probe_kernel_write(mem, &c, 1);
391 if (err)
392 break;
393
394 mem++;
395 }
396
397 return err;
398}
399
400/*
401 * Convert the hex array pointed to by buf into binary to be placed in mem.
402 * Return a pointer to the character AFTER the last byte written.
403 * May return an error.
404 */
405int kgdb_hex2mem(char *buf, char *mem, int count)
406{
407 char *tmp_raw;
408 char *tmp_hex;
409
410 /*
411 * We use the upper half of buf as an intermediate buffer for the
412 * raw memory that is converted from hex.
413 */
414 tmp_raw = buf + count * 2;
415
416 tmp_hex = tmp_raw - 1;
417 while (tmp_hex >= buf) {
418 tmp_raw--;
419 *tmp_raw = hex(*tmp_hex--);
420 *tmp_raw |= hex(*tmp_hex--) << 4;
421 }
422
423 return probe_kernel_write(mem, tmp_raw, count);
424}
425
426/*
427 * While we find nice hex chars, build a long_val.
428 * Return number of chars processed.
429 */
430int kgdb_hex2long(char **ptr, long *long_val)
431{
432 int hex_val;
433 int num = 0;
434
435 *long_val = 0;
436
437 while (**ptr) {
438 hex_val = hex(**ptr);
439 if (hex_val < 0)
440 break;
441
442 *long_val = (*long_val << 4) | hex_val;
443 num++;
444 (*ptr)++;
445 }
446
447 return num;
448}
449
450/* Write memory due to an 'M' or 'X' packet. */
451static int write_mem_msg(int binary)
452{
453 char *ptr = &remcom_in_buffer[1];
454 unsigned long addr;
455 unsigned long length;
456 int err;
457
458 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
459 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
460 if (binary)
461 err = kgdb_ebin2mem(ptr, (char *)addr, length);
462 else
463 err = kgdb_hex2mem(ptr, (char *)addr, length);
464 if (err)
465 return err;
466 if (CACHE_FLUSH_IS_SAFE)
467 flush_icache_range(addr, addr + length + 1);
468 return 0;
469 }
470
471 return -EINVAL;
472}
473
474static void error_packet(char *pkt, int error)
475{
476 error = -error;
477 pkt[0] = 'E';
478 pkt[1] = hexchars[(error / 10)];
479 pkt[2] = hexchars[(error % 10)];
480 pkt[3] = '\0';
481}
482
483/*
484 * Thread ID accessors. We represent a flat TID space to GDB, where
485 * the per CPU idle threads (which under Linux all have PID 0) are
486 * remapped to negative TIDs.
487 */
488
489#define BUF_THREAD_ID_SIZE 16
490
491static char *pack_threadid(char *pkt, unsigned char *id)
492{
493 char *limit;
494
495 limit = pkt + BUF_THREAD_ID_SIZE;
496 while (pkt < limit)
497 pkt = pack_hex_byte(pkt, *id++);
498
499 return pkt;
500}
501
502static void int_to_threadref(unsigned char *id, int value)
503{
504 unsigned char *scan;
505 int i = 4;
506
507 scan = (unsigned char *)id;
508 while (i--)
509 *scan++ = 0;
510 *scan++ = (value >> 24) & 0xff;
511 *scan++ = (value >> 16) & 0xff;
512 *scan++ = (value >> 8) & 0xff;
513 *scan++ = (value & 0xff);
514}
515
516static struct task_struct *getthread(struct pt_regs *regs, int tid)
517{
518 /*
519 * Non-positive TIDs are remapped idle tasks:
520 */
521 if (tid <= 0)
522 return idle_task(-tid);
523
524 /*
525 * find_task_by_pid_ns() does not take the tasklist lock anymore
526 * but is nicely RCU locked - hence is a pretty resilient
527 * thing to use:
528 */
529 return find_task_by_pid_ns(tid, &init_pid_ns);
530}
531
532/*
533 * CPU debug state control:
534 */
535
536#ifdef CONFIG_SMP
537static void kgdb_wait(struct pt_regs *regs)
538{
539 unsigned long flags;
540 int cpu;
541
542 local_irq_save(flags);
543 cpu = raw_smp_processor_id();
544 kgdb_info[cpu].debuggerinfo = regs;
545 kgdb_info[cpu].task = current;
546 /*
547 * Make sure the above info reaches the primary CPU before
548 * our cpu_in_kgdb[] flag setting does:
549 */
550 smp_wmb();
551 atomic_set(&cpu_in_kgdb[cpu], 1);
552
553 /*
554 * The primary CPU must be active to enter here, but this is
555 * guard in case the primary CPU had not been selected if
556 * this was an entry via nmi.
557 */
558 while (atomic_read(&kgdb_active) == -1)
559 cpu_relax();
560
561 /* Wait till primary CPU goes completely into the debugger. */
562 while (!atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)]))
563 cpu_relax();
564
565 /* Wait till primary CPU is done with debugging */
566 while (atomic_read(&passive_cpu_wait[cpu]))
567 cpu_relax();
568
569 kgdb_info[cpu].debuggerinfo = NULL;
570 kgdb_info[cpu].task = NULL;
571
572 /* fix up hardware debug registers on local cpu */
573 if (arch_kgdb_ops.correct_hw_break)
574 arch_kgdb_ops.correct_hw_break();
575
576 /* Signal the primary CPU that we are done: */
577 atomic_set(&cpu_in_kgdb[cpu], 0);
7c3078b6 578 clocksource_touch_watchdog();
dc7d5527
JW
579 local_irq_restore(flags);
580}
581#endif
582
583/*
584 * Some architectures need cache flushes when we set/clear a
585 * breakpoint:
586 */
587static void kgdb_flush_swbreak_addr(unsigned long addr)
588{
589 if (!CACHE_FLUSH_IS_SAFE)
590 return;
591
592 if (current->mm) {
593 flush_cache_range(current->mm->mmap_cache,
594 addr, addr + BREAK_INSTR_SIZE);
595 } else {
596 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
597 }
598}
599
600/*
601 * SW breakpoint management:
602 */
603static int kgdb_activate_sw_breakpoints(void)
604{
605 unsigned long addr;
606 int error = 0;
607 int i;
608
609 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
610 if (kgdb_break[i].state != BP_SET)
611 continue;
612
613 addr = kgdb_break[i].bpt_addr;
614 error = kgdb_arch_set_breakpoint(addr,
615 kgdb_break[i].saved_instr);
616 if (error)
617 return error;
618
619 kgdb_flush_swbreak_addr(addr);
620 kgdb_break[i].state = BP_ACTIVE;
621 }
622 return 0;
623}
624
625static int kgdb_set_sw_break(unsigned long addr)
626{
627 int err = kgdb_validate_break_address(addr);
628 int breakno = -1;
629 int i;
630
631 if (err)
632 return err;
633
634 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
635 if ((kgdb_break[i].state == BP_SET) &&
636 (kgdb_break[i].bpt_addr == addr))
637 return -EEXIST;
638 }
639 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
640 if (kgdb_break[i].state == BP_REMOVED &&
641 kgdb_break[i].bpt_addr == addr) {
642 breakno = i;
643 break;
644 }
645 }
646
647 if (breakno == -1) {
648 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
649 if (kgdb_break[i].state == BP_UNDEFINED) {
650 breakno = i;
651 break;
652 }
653 }
654 }
655
656 if (breakno == -1)
657 return -E2BIG;
658
659 kgdb_break[breakno].state = BP_SET;
660 kgdb_break[breakno].type = BP_BREAKPOINT;
661 kgdb_break[breakno].bpt_addr = addr;
662
663 return 0;
664}
665
666static int kgdb_deactivate_sw_breakpoints(void)
667{
668 unsigned long addr;
669 int error = 0;
670 int i;
671
672 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
673 if (kgdb_break[i].state != BP_ACTIVE)
674 continue;
675 addr = kgdb_break[i].bpt_addr;
676 error = kgdb_arch_remove_breakpoint(addr,
677 kgdb_break[i].saved_instr);
678 if (error)
679 return error;
680
681 kgdb_flush_swbreak_addr(addr);
682 kgdb_break[i].state = BP_SET;
683 }
684 return 0;
685}
686
687static int kgdb_remove_sw_break(unsigned long addr)
688{
689 int i;
690
691 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
692 if ((kgdb_break[i].state == BP_SET) &&
693 (kgdb_break[i].bpt_addr == addr)) {
694 kgdb_break[i].state = BP_REMOVED;
695 return 0;
696 }
697 }
698 return -ENOENT;
699}
700
701int kgdb_isremovedbreak(unsigned long addr)
702{
703 int i;
704
705 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
706 if ((kgdb_break[i].state == BP_REMOVED) &&
707 (kgdb_break[i].bpt_addr == addr))
708 return 1;
709 }
710 return 0;
711}
712
713int remove_all_break(void)
714{
715 unsigned long addr;
716 int error;
717 int i;
718
719 /* Clear memory breakpoints. */
720 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
721 if (kgdb_break[i].state != BP_SET)
722 continue;
723 addr = kgdb_break[i].bpt_addr;
724 error = kgdb_arch_remove_breakpoint(addr,
725 kgdb_break[i].saved_instr);
726 if (error)
727 return error;
728 kgdb_break[i].state = BP_REMOVED;
729 }
730
731 /* Clear hardware breakpoints. */
732 if (arch_kgdb_ops.remove_all_hw_break)
733 arch_kgdb_ops.remove_all_hw_break();
734
735 return 0;
736}
737
738/*
739 * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
740 */
741static inline int shadow_pid(int realpid)
742{
743 if (realpid)
744 return realpid;
745
746 return -1-raw_smp_processor_id();
747}
748
749static char gdbmsgbuf[BUFMAX + 1];
750
751static void kgdb_msg_write(const char *s, int len)
752{
753 char *bufptr;
754 int wcount;
755 int i;
756
757 /* 'O'utput */
758 gdbmsgbuf[0] = 'O';
759
760 /* Fill and send buffers... */
761 while (len > 0) {
762 bufptr = gdbmsgbuf + 1;
763
764 /* Calculate how many this time */
765 if ((len << 1) > (BUFMAX - 2))
766 wcount = (BUFMAX - 2) >> 1;
767 else
768 wcount = len;
769
770 /* Pack in hex chars */
771 for (i = 0; i < wcount; i++)
772 bufptr = pack_hex_byte(bufptr, s[i]);
773 *bufptr = '\0';
774
775 /* Move up */
776 s += wcount;
777 len -= wcount;
778
779 /* Write packet */
780 put_packet(gdbmsgbuf);
781 }
782}
783
784/*
785 * Return true if there is a valid kgdb I/O module. Also if no
786 * debugger is attached a message can be printed to the console about
787 * waiting for the debugger to attach.
788 *
789 * The print_wait argument is only to be true when called from inside
790 * the core kgdb_handle_exception, because it will wait for the
791 * debugger to attach.
792 */
793static int kgdb_io_ready(int print_wait)
794{
795 if (!kgdb_io_ops)
796 return 0;
797 if (kgdb_connected)
798 return 1;
799 if (atomic_read(&kgdb_setting_breakpoint))
800 return 1;
801 if (print_wait)
802 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
803 return 1;
804}
805
806/*
807 * All the functions that start with gdb_cmd are the various
808 * operations to implement the handlers for the gdbserial protocol
809 * where KGDB is communicating with an external debugger
810 */
811
812/* Handle the '?' status packets */
813static void gdb_cmd_status(struct kgdb_state *ks)
814{
815 /*
816 * We know that this packet is only sent
817 * during initial connect. So to be safe,
818 * we clear out our breakpoints now in case
819 * GDB is reconnecting.
820 */
821 remove_all_break();
822
823 remcom_out_buffer[0] = 'S';
824 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
825}
826
827/* Handle the 'g' get registers request */
828static void gdb_cmd_getregs(struct kgdb_state *ks)
829{
830 struct task_struct *thread;
831 void *local_debuggerinfo;
832 int i;
833
834 thread = kgdb_usethread;
835 if (!thread) {
836 thread = kgdb_info[ks->cpu].task;
837 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
838 } else {
839 local_debuggerinfo = NULL;
840 for (i = 0; i < NR_CPUS; i++) {
841 /*
842 * Try to find the task on some other
843 * or possibly this node if we do not
844 * find the matching task then we try
845 * to approximate the results.
846 */
847 if (thread == kgdb_info[i].task)
848 local_debuggerinfo = kgdb_info[i].debuggerinfo;
849 }
850 }
851
852 /*
853 * All threads that don't have debuggerinfo should be
854 * in __schedule() sleeping, since all other CPUs
855 * are in kgdb_wait, and thus have debuggerinfo.
856 */
857 if (local_debuggerinfo) {
858 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
859 } else {
860 /*
861 * Pull stuff saved during switch_to; nothing
862 * else is accessible (or even particularly
863 * relevant).
864 *
865 * This should be enough for a stack trace.
866 */
867 sleeping_thread_to_gdb_regs(gdb_regs, thread);
868 }
869 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
870}
871
872/* Handle the 'G' set registers request */
873static void gdb_cmd_setregs(struct kgdb_state *ks)
874{
875 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
876
877 if (kgdb_usethread && kgdb_usethread != current) {
878 error_packet(remcom_out_buffer, -EINVAL);
879 } else {
880 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
881 strcpy(remcom_out_buffer, "OK");
882 }
883}
884
885/* Handle the 'm' memory read bytes */
886static void gdb_cmd_memread(struct kgdb_state *ks)
887{
888 char *ptr = &remcom_in_buffer[1];
889 unsigned long length;
890 unsigned long addr;
891 int err;
892
893 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
894 kgdb_hex2long(&ptr, &length) > 0) {
895 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
896 if (err)
897 error_packet(remcom_out_buffer, err);
898 } else {
899 error_packet(remcom_out_buffer, -EINVAL);
900 }
901}
902
903/* Handle the 'M' memory write bytes */
904static void gdb_cmd_memwrite(struct kgdb_state *ks)
905{
906 int err = write_mem_msg(0);
907
908 if (err)
909 error_packet(remcom_out_buffer, err);
910 else
911 strcpy(remcom_out_buffer, "OK");
912}
913
914/* Handle the 'X' memory binary write bytes */
915static void gdb_cmd_binwrite(struct kgdb_state *ks)
916{
917 int err = write_mem_msg(1);
918
919 if (err)
920 error_packet(remcom_out_buffer, err);
921 else
922 strcpy(remcom_out_buffer, "OK");
923}
924
925/* Handle the 'D' or 'k', detach or kill packets */
926static void gdb_cmd_detachkill(struct kgdb_state *ks)
927{
928 int error;
929
930 /* The detach case */
931 if (remcom_in_buffer[0] == 'D') {
932 error = remove_all_break();
933 if (error < 0) {
934 error_packet(remcom_out_buffer, error);
935 } else {
936 strcpy(remcom_out_buffer, "OK");
937 kgdb_connected = 0;
938 }
939 put_packet(remcom_out_buffer);
940 } else {
941 /*
942 * Assume the kill case, with no exit code checking,
943 * trying to force detach the debugger:
944 */
945 remove_all_break();
946 kgdb_connected = 0;
947 }
948}
949
950/* Handle the 'R' reboot packets */
951static int gdb_cmd_reboot(struct kgdb_state *ks)
952{
953 /* For now, only honor R0 */
954 if (strcmp(remcom_in_buffer, "R0") == 0) {
955 printk(KERN_CRIT "Executing emergency reboot\n");
956 strcpy(remcom_out_buffer, "OK");
957 put_packet(remcom_out_buffer);
958
959 /*
960 * Execution should not return from
961 * machine_emergency_restart()
962 */
963 machine_emergency_restart();
964 kgdb_connected = 0;
965
966 return 1;
967 }
968 return 0;
969}
970
971/* Handle the 'q' query packets */
972static void gdb_cmd_query(struct kgdb_state *ks)
973{
974 struct task_struct *thread;
975 unsigned char thref[8];
976 char *ptr;
977 int i;
978
979 switch (remcom_in_buffer[1]) {
980 case 's':
981 case 'f':
982 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
983 error_packet(remcom_out_buffer, -EINVAL);
984 break;
985 }
986
987 if (remcom_in_buffer[1] == 'f')
988 ks->threadid = 1;
989
990 remcom_out_buffer[0] = 'm';
991 ptr = remcom_out_buffer + 1;
992
993 for (i = 0; i < 17; ks->threadid++) {
994 thread = getthread(ks->linux_regs, ks->threadid);
995 if (thread) {
996 int_to_threadref(thref, ks->threadid);
997 pack_threadid(ptr, thref);
998 ptr += BUF_THREAD_ID_SIZE;
999 *(ptr++) = ',';
1000 i++;
1001 }
1002 }
1003 *(--ptr) = '\0';
1004 break;
1005
1006 case 'C':
1007 /* Current thread id */
1008 strcpy(remcom_out_buffer, "QC");
1009 ks->threadid = shadow_pid(current->pid);
1010 int_to_threadref(thref, ks->threadid);
1011 pack_threadid(remcom_out_buffer + 2, thref);
1012 break;
1013 case 'T':
1014 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1015 error_packet(remcom_out_buffer, -EINVAL);
1016 break;
1017 }
1018 ks->threadid = 0;
1019 ptr = remcom_in_buffer + 17;
1020 kgdb_hex2long(&ptr, &ks->threadid);
1021 if (!getthread(ks->linux_regs, ks->threadid)) {
1022 error_packet(remcom_out_buffer, -EINVAL);
1023 break;
1024 }
1025 if (ks->threadid > 0) {
1026 kgdb_mem2hex(getthread(ks->linux_regs,
1027 ks->threadid)->comm,
1028 remcom_out_buffer, 16);
1029 } else {
1030 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1031
1032 sprintf(tmpstr, "Shadow task %d for pid 0",
1033 (int)(-ks->threadid-1));
1034 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1035 }
1036 break;
1037 }
1038}
1039
1040/* Handle the 'H' task query packets */
1041static void gdb_cmd_task(struct kgdb_state *ks)
1042{
1043 struct task_struct *thread;
1044 char *ptr;
1045
1046 switch (remcom_in_buffer[1]) {
1047 case 'g':
1048 ptr = &remcom_in_buffer[2];
1049 kgdb_hex2long(&ptr, &ks->threadid);
1050 thread = getthread(ks->linux_regs, ks->threadid);
1051 if (!thread && ks->threadid > 0) {
1052 error_packet(remcom_out_buffer, -EINVAL);
1053 break;
1054 }
1055 kgdb_usethread = thread;
1056 ks->kgdb_usethreadid = ks->threadid;
1057 strcpy(remcom_out_buffer, "OK");
1058 break;
1059 case 'c':
1060 ptr = &remcom_in_buffer[2];
1061 kgdb_hex2long(&ptr, &ks->threadid);
1062 if (!ks->threadid) {
1063 kgdb_contthread = NULL;
1064 } else {
1065 thread = getthread(ks->linux_regs, ks->threadid);
1066 if (!thread && ks->threadid > 0) {
1067 error_packet(remcom_out_buffer, -EINVAL);
1068 break;
1069 }
1070 kgdb_contthread = thread;
1071 }
1072 strcpy(remcom_out_buffer, "OK");
1073 break;
1074 }
1075}
1076
1077/* Handle the 'T' thread query packets */
1078static void gdb_cmd_thread(struct kgdb_state *ks)
1079{
1080 char *ptr = &remcom_in_buffer[1];
1081 struct task_struct *thread;
1082
1083 kgdb_hex2long(&ptr, &ks->threadid);
1084 thread = getthread(ks->linux_regs, ks->threadid);
1085 if (thread)
1086 strcpy(remcom_out_buffer, "OK");
1087 else
1088 error_packet(remcom_out_buffer, -EINVAL);
1089}
1090
1091/* Handle the 'z' or 'Z' breakpoint remove or set packets */
1092static void gdb_cmd_break(struct kgdb_state *ks)
1093{
1094 /*
1095 * Since GDB-5.3, it's been drafted that '0' is a software
1096 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1097 */
1098 char *bpt_type = &remcom_in_buffer[1];
1099 char *ptr = &remcom_in_buffer[2];
1100 unsigned long addr;
1101 unsigned long length;
1102 int error = 0;
1103
1104 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1105 /* Unsupported */
1106 if (*bpt_type > '4')
1107 return;
1108 } else {
1109 if (*bpt_type != '0' && *bpt_type != '1')
1110 /* Unsupported. */
1111 return;
1112 }
1113
1114 /*
1115 * Test if this is a hardware breakpoint, and
1116 * if we support it:
1117 */
1118 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1119 /* Unsupported. */
1120 return;
1121
1122 if (*(ptr++) != ',') {
1123 error_packet(remcom_out_buffer, -EINVAL);
1124 return;
1125 }
1126 if (!kgdb_hex2long(&ptr, &addr)) {
1127 error_packet(remcom_out_buffer, -EINVAL);
1128 return;
1129 }
1130 if (*(ptr++) != ',' ||
1131 !kgdb_hex2long(&ptr, &length)) {
1132 error_packet(remcom_out_buffer, -EINVAL);
1133 return;
1134 }
1135
1136 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1137 error = kgdb_set_sw_break(addr);
1138 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1139 error = kgdb_remove_sw_break(addr);
1140 else if (remcom_in_buffer[0] == 'Z')
1141 error = arch_kgdb_ops.set_hw_breakpoint(addr,
1142 (int)length, *bpt_type);
1143 else if (remcom_in_buffer[0] == 'z')
1144 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
1145 (int) length, *bpt_type);
1146
1147 if (error == 0)
1148 strcpy(remcom_out_buffer, "OK");
1149 else
1150 error_packet(remcom_out_buffer, error);
1151}
1152
1153/* Handle the 'C' signal / exception passing packets */
1154static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1155{
1156 /* C09 == pass exception
1157 * C15 == detach kgdb, pass exception
1158 */
1159 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1160
1161 ks->pass_exception = 1;
1162 remcom_in_buffer[0] = 'c';
1163
1164 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1165
1166 ks->pass_exception = 1;
1167 remcom_in_buffer[0] = 'D';
1168 remove_all_break();
1169 kgdb_connected = 0;
1170 return 1;
1171
1172 } else {
1173 error_packet(remcom_out_buffer, -EINVAL);
1174 return 0;
1175 }
1176
1177 /* Indicate fall through */
1178 return -1;
1179}
1180
1181/*
1182 * This function performs all gdbserial command procesing
1183 */
1184static int gdb_serial_stub(struct kgdb_state *ks)
1185{
1186 int error = 0;
1187 int tmp;
1188
1189 /* Clear the out buffer. */
1190 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1191
1192 if (kgdb_connected) {
1193 unsigned char thref[8];
1194 char *ptr;
1195
1196 /* Reply to host that an exception has occurred */
1197 ptr = remcom_out_buffer;
1198 *ptr++ = 'T';
1199 ptr = pack_hex_byte(ptr, ks->signo);
1200 ptr += strlen(strcpy(ptr, "thread:"));
1201 int_to_threadref(thref, shadow_pid(current->pid));
1202 ptr = pack_threadid(ptr, thref);
1203 *ptr++ = ';';
1204 put_packet(remcom_out_buffer);
1205 }
1206
1207 kgdb_usethread = kgdb_info[ks->cpu].task;
1208 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1209 ks->pass_exception = 0;
1210
1211 while (1) {
1212 error = 0;
1213
1214 /* Clear the out buffer. */
1215 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1216
1217 get_packet(remcom_in_buffer);
1218
1219 switch (remcom_in_buffer[0]) {
1220 case '?': /* gdbserial status */
1221 gdb_cmd_status(ks);
1222 break;
1223 case 'g': /* return the value of the CPU registers */
1224 gdb_cmd_getregs(ks);
1225 break;
1226 case 'G': /* set the value of the CPU registers - return OK */
1227 gdb_cmd_setregs(ks);
1228 break;
1229 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1230 gdb_cmd_memread(ks);
1231 break;
1232 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1233 gdb_cmd_memwrite(ks);
1234 break;
1235 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1236 gdb_cmd_binwrite(ks);
1237 break;
1238 /* kill or detach. KGDB should treat this like a
1239 * continue.
1240 */
1241 case 'D': /* Debugger detach */
1242 case 'k': /* Debugger detach via kill */
1243 gdb_cmd_detachkill(ks);
1244 goto default_handle;
1245 case 'R': /* Reboot */
1246 if (gdb_cmd_reboot(ks))
1247 goto default_handle;
1248 break;
1249 case 'q': /* query command */
1250 gdb_cmd_query(ks);
1251 break;
1252 case 'H': /* task related */
1253 gdb_cmd_task(ks);
1254 break;
1255 case 'T': /* Query thread status */
1256 gdb_cmd_thread(ks);
1257 break;
1258 case 'z': /* Break point remove */
1259 case 'Z': /* Break point set */
1260 gdb_cmd_break(ks);
1261 break;
1262 case 'C': /* Exception passing */
1263 tmp = gdb_cmd_exception_pass(ks);
1264 if (tmp > 0)
1265 goto default_handle;
1266 if (tmp == 0)
1267 break;
1268 /* Fall through on tmp < 0 */
1269 case 'c': /* Continue packet */
1270 case 's': /* Single step packet */
1271 if (kgdb_contthread && kgdb_contthread != current) {
1272 /* Can't switch threads in kgdb */
1273 error_packet(remcom_out_buffer, -EINVAL);
1274 break;
1275 }
1276 kgdb_activate_sw_breakpoints();
1277 /* Fall through to default processing */
1278 default:
1279default_handle:
1280 error = kgdb_arch_handle_exception(ks->ex_vector,
1281 ks->signo,
1282 ks->err_code,
1283 remcom_in_buffer,
1284 remcom_out_buffer,
1285 ks->linux_regs);
1286 /*
1287 * Leave cmd processing on error, detach,
1288 * kill, continue, or single step.
1289 */
1290 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1291 remcom_in_buffer[0] == 'k') {
1292 error = 0;
1293 goto kgdb_exit;
1294 }
1295
1296 }
1297
1298 /* reply to the request */
1299 put_packet(remcom_out_buffer);
1300 }
1301
1302kgdb_exit:
1303 if (ks->pass_exception)
1304 error = 1;
1305 return error;
1306}
1307
1308static int kgdb_reenter_check(struct kgdb_state *ks)
1309{
1310 unsigned long addr;
1311
1312 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
1313 return 0;
1314
1315 /* Panic on recursive debugger calls: */
1316 exception_level++;
1317 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
1318 kgdb_deactivate_sw_breakpoints();
1319
1320 /*
1321 * If the break point removed ok at the place exception
1322 * occurred, try to recover and print a warning to the end
1323 * user because the user planted a breakpoint in a place that
1324 * KGDB needs in order to function.
1325 */
1326 if (kgdb_remove_sw_break(addr) == 0) {
1327 exception_level = 0;
1328 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1329 kgdb_activate_sw_breakpoints();
1330 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed\n");
1331 WARN_ON_ONCE(1);
1332
1333 return 1;
1334 }
1335 remove_all_break();
1336 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1337
1338 if (exception_level > 1) {
1339 dump_stack();
1340 panic("Recursive entry to debugger");
1341 }
1342
1343 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1344 dump_stack();
1345 panic("Recursive entry to debugger");
1346
1347 return 1;
1348}
1349
1350/*
1351 * kgdb_handle_exception() - main entry point from a kernel exception
1352 *
1353 * Locking hierarchy:
1354 * interface locks, if any (begin_session)
1355 * kgdb lock (kgdb_active)
1356 */
1357int
1358kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1359{
1360 struct kgdb_state kgdb_var;
1361 struct kgdb_state *ks = &kgdb_var;
1362 unsigned long flags;
1363 int error = 0;
1364 int i, cpu;
1365
1366 ks->cpu = raw_smp_processor_id();
1367 ks->ex_vector = evector;
1368 ks->signo = signo;
1369 ks->ex_vector = evector;
1370 ks->err_code = ecode;
1371 ks->kgdb_usethreadid = 0;
1372 ks->linux_regs = regs;
1373
1374 if (kgdb_reenter_check(ks))
1375 return 0; /* Ouch, double exception ! */
1376
1377acquirelock:
1378 /*
1379 * Interrupts will be restored by the 'trap return' code, except when
1380 * single stepping.
1381 */
1382 local_irq_save(flags);
1383
1384 cpu = raw_smp_processor_id();
1385
1386 /*
1387 * Acquire the kgdb_active lock:
1388 */
1389 while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1390 cpu_relax();
1391
1392 /*
1393 * Do not start the debugger connection on this CPU if the last
1394 * instance of the exception handler wanted to come into the
1395 * debugger on a different CPU via a single step
1396 */
1397 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1398 atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
1399
1400 atomic_set(&kgdb_active, -1);
7c3078b6 1401 clocksource_touch_watchdog();
dc7d5527
JW
1402 local_irq_restore(flags);
1403
1404 goto acquirelock;
1405 }
1406
1407 if (!kgdb_io_ready(1)) {
1408 error = 1;
1409 goto kgdb_restore; /* No I/O connection, so resume the system */
1410 }
1411
1412 /*
1413 * Don't enter if we have hit a removed breakpoint.
1414 */
1415 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1416 goto kgdb_restore;
1417
1418 /* Call the I/O driver's pre_exception routine */
1419 if (kgdb_io_ops->pre_exception)
1420 kgdb_io_ops->pre_exception();
1421
1422 kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1423 kgdb_info[ks->cpu].task = current;
1424
1425 kgdb_disable_hw_debug(ks->linux_regs);
1426
1427 /*
1428 * Get the passive CPU lock which will hold all the non-primary
1429 * CPU in a spin state while the debugger is active
1430 */
1431 if (!kgdb_single_step || !kgdb_contthread) {
1432 for (i = 0; i < NR_CPUS; i++)
1433 atomic_set(&passive_cpu_wait[i], 1);
1434 }
1435
1436#ifdef CONFIG_SMP
1437 /* Signal the other CPUs to enter kgdb_wait() */
1438 if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup)
1439 kgdb_roundup_cpus(flags);
1440#endif
1441
1442 /*
1443 * spin_lock code is good enough as a barrier so we don't
1444 * need one here:
1445 */
1446 atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1447
1448 /*
1449 * Wait for the other CPUs to be notified and be waiting for us:
1450 */
1451 for_each_online_cpu(i) {
1452 while (!atomic_read(&cpu_in_kgdb[i]))
1453 cpu_relax();
1454 }
1455
1456 /*
1457 * At this point the primary processor is completely
1458 * in the debugger and all secondary CPUs are quiescent
1459 */
1460 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1461 kgdb_deactivate_sw_breakpoints();
1462 kgdb_single_step = 0;
1463 kgdb_contthread = NULL;
1464 exception_level = 0;
1465
1466 /* Talk to debugger with gdbserial protocol */
1467 error = gdb_serial_stub(ks);
1468
1469 /* Call the I/O driver's post_exception routine */
1470 if (kgdb_io_ops->post_exception)
1471 kgdb_io_ops->post_exception();
1472
1473 kgdb_info[ks->cpu].debuggerinfo = NULL;
1474 kgdb_info[ks->cpu].task = NULL;
1475 atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1476
1477 if (!kgdb_single_step || !kgdb_contthread) {
1478 for (i = NR_CPUS-1; i >= 0; i--)
1479 atomic_set(&passive_cpu_wait[i], 0);
1480 /*
1481 * Wait till all the CPUs have quit
1482 * from the debugger.
1483 */
1484 for_each_online_cpu(i) {
1485 while (atomic_read(&cpu_in_kgdb[i]))
1486 cpu_relax();
1487 }
1488 }
1489
1490kgdb_restore:
1491 /* Free kgdb_active */
1492 atomic_set(&kgdb_active, -1);
7c3078b6 1493 clocksource_touch_watchdog();
dc7d5527
JW
1494 local_irq_restore(flags);
1495
1496 return error;
1497}
1498
1499int kgdb_nmicallback(int cpu, void *regs)
1500{
1501#ifdef CONFIG_SMP
1502 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
1503 atomic_read(&kgdb_active) != cpu) {
1504 kgdb_wait((struct pt_regs *)regs);
1505 return 0;
1506 }
1507#endif
1508 return 1;
1509}
1510
1511void kgdb_console_write(struct console *co, const char *s, unsigned count)
1512{
1513 unsigned long flags;
1514
1515 /* If we're debugging, or KGDB has not connected, don't try
1516 * and print. */
1517 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1518 return;
1519
1520 local_irq_save(flags);
1521 kgdb_msg_write(s, count);
1522 local_irq_restore(flags);
1523}
1524
1525static struct console kgdbcons = {
1526 .name = "kgdb",
1527 .write = kgdb_console_write,
1528 .flags = CON_PRINTBUFFER | CON_ENABLED,
1529 .index = -1,
1530};
1531
1532#ifdef CONFIG_MAGIC_SYSRQ
1533static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1534{
1535 if (!kgdb_io_ops) {
1536 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1537 return;
1538 }
1539 if (!kgdb_connected)
1540 printk(KERN_CRIT "Entering KGDB\n");
1541
1542 kgdb_breakpoint();
1543}
1544
1545static struct sysrq_key_op sysrq_gdb_op = {
1546 .handler = sysrq_handle_gdb,
1547 .help_msg = "Gdb",
1548 .action_msg = "GDB",
1549};
1550#endif
1551
1552static void kgdb_register_callbacks(void)
1553{
1554 if (!kgdb_io_module_registered) {
1555 kgdb_io_module_registered = 1;
1556 kgdb_arch_init();
1557#ifdef CONFIG_MAGIC_SYSRQ
1558 register_sysrq_key('g', &sysrq_gdb_op);
1559#endif
1560 if (kgdb_use_con && !kgdb_con_registered) {
1561 register_console(&kgdbcons);
1562 kgdb_con_registered = 1;
1563 }
1564 }
1565}
1566
1567static void kgdb_unregister_callbacks(void)
1568{
1569 /*
1570 * When this routine is called KGDB should unregister from the
1571 * panic handler and clean up, making sure it is not handling any
1572 * break exceptions at the time.
1573 */
1574 if (kgdb_io_module_registered) {
1575 kgdb_io_module_registered = 0;
1576 kgdb_arch_exit();
1577#ifdef CONFIG_MAGIC_SYSRQ
1578 unregister_sysrq_key('g', &sysrq_gdb_op);
1579#endif
1580 if (kgdb_con_registered) {
1581 unregister_console(&kgdbcons);
1582 kgdb_con_registered = 0;
1583 }
1584 }
1585}
1586
1587static void kgdb_initial_breakpoint(void)
1588{
1589 kgdb_break_asap = 0;
1590
1591 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1592 kgdb_breakpoint();
1593}
1594
1595/**
1596 * kkgdb_register_io_module - register KGDB IO module
1597 * @new_kgdb_io_ops: the io ops vector
1598 *
1599 * Register it with the KGDB core.
1600 */
1601int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1602{
1603 int err;
1604
1605 spin_lock(&kgdb_registration_lock);
1606
1607 if (kgdb_io_ops) {
1608 spin_unlock(&kgdb_registration_lock);
1609
1610 printk(KERN_ERR "kgdb: Another I/O driver is already "
1611 "registered with KGDB.\n");
1612 return -EBUSY;
1613 }
1614
1615 if (new_kgdb_io_ops->init) {
1616 err = new_kgdb_io_ops->init();
1617 if (err) {
1618 spin_unlock(&kgdb_registration_lock);
1619 return err;
1620 }
1621 }
1622
1623 kgdb_io_ops = new_kgdb_io_ops;
1624
1625 spin_unlock(&kgdb_registration_lock);
1626
1627 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1628 new_kgdb_io_ops->name);
1629
1630 /* Arm KGDB now. */
1631 kgdb_register_callbacks();
1632
1633 if (kgdb_break_asap)
1634 kgdb_initial_breakpoint();
1635
1636 return 0;
1637}
1638EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1639
1640/**
1641 * kkgdb_unregister_io_module - unregister KGDB IO module
1642 * @old_kgdb_io_ops: the io ops vector
1643 *
1644 * Unregister it with the KGDB core.
1645 */
1646void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1647{
1648 BUG_ON(kgdb_connected);
1649
1650 /*
1651 * KGDB is no longer able to communicate out, so
1652 * unregister our callbacks and reset state.
1653 */
1654 kgdb_unregister_callbacks();
1655
1656 spin_lock(&kgdb_registration_lock);
1657
1658 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1659 kgdb_io_ops = NULL;
1660
1661 spin_unlock(&kgdb_registration_lock);
1662
1663 printk(KERN_INFO
1664 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1665 old_kgdb_io_ops->name);
1666}
1667EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1668
1669/**
1670 * kgdb_breakpoint - generate breakpoint exception
1671 *
1672 * This function will generate a breakpoint exception. It is used at the
1673 * beginning of a program to sync up with a debugger and can be used
1674 * otherwise as a quick means to stop program execution and "break" into
1675 * the debugger.
1676 */
1677void kgdb_breakpoint(void)
1678{
1679 atomic_set(&kgdb_setting_breakpoint, 1);
1680 wmb(); /* Sync point before breakpoint */
1681 arch_kgdb_breakpoint();
1682 wmb(); /* Sync point after breakpoint */
1683 atomic_set(&kgdb_setting_breakpoint, 0);
1684}
1685EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1686
1687static int __init opt_kgdb_wait(char *str)
1688{
1689 kgdb_break_asap = 1;
1690
1691 if (kgdb_io_module_registered)
1692 kgdb_initial_breakpoint();
1693
1694 return 0;
1695}
1696
1697early_param("kgdbwait", opt_kgdb_wait);