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