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