kgdb: print breakpoint removed on exception
[safe/jmp/linux-2.6] / kernel / kgdb.c
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>
31 #include <linux/clocksource.h>
32 #include <linux/interrupt.h>
33 #include <linux/spinlock.h>
34 #include <linux/console.h>
35 #include <linux/threads.h>
36 #include <linux/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/ptrace.h>
40 #include <linux/reboot.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/sysrq.h>
45 #include <linux/init.h>
46 #include <linux/kgdb.h>
47 #include <linux/pid.h>
48 #include <linux/smp.h>
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
56 static int kgdb_break_asap;
57
58 struct 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
69 static 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  */
77 int                             kgdb_connected;
78 EXPORT_SYMBOL_GPL(kgdb_connected);
79
80 /* All the KGDB handlers are installed */
81 static int                      kgdb_io_module_registered;
82
83 /* Guard for recursive entry */
84 static int                      exception_level;
85
86 static struct kgdb_io           *kgdb_io_ops;
87 static DEFINE_SPINLOCK(kgdb_registration_lock);
88
89 /* kgdb console driver is loaded */
90 static int kgdb_con_registered;
91 /* determine if kgdb console output should be used */
92 static int kgdb_use_con;
93
94 static int __init opt_kgdb_con(char *str)
95 {
96         kgdb_use_con = 1;
97         return 0;
98 }
99
100 early_param("kgdbcon", opt_kgdb_con);
101
102 module_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  */
108 static 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  */
115 atomic_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  */
121 static atomic_t                 passive_cpu_wait[NR_CPUS];
122 static atomic_t                 cpu_in_kgdb[NR_CPUS];
123 atomic_t                        kgdb_setting_breakpoint;
124
125 struct task_struct              *kgdb_usethread;
126 struct task_struct              *kgdb_contthread;
127
128 int                             kgdb_single_step;
129
130 /* Our I/O buffers. */
131 static char                     remcom_in_buffer[BUFMAX];
132 static char                     remcom_out_buffer[BUFMAX];
133
134 /* Storage for the registers, in GDB format. */
135 static 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*/
140 atomic_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  */
149 int                             kgdb_do_roundup = 1;
150
151 static int __init opt_nokgdbroundup(char *str)
152 {
153         kgdb_do_roundup = 0;
154
155         return 0;
156 }
157
158 early_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  */
168 int __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
175 int __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
187 int __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
193 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
194 {
195         return instruction_pointer(regs);
196 }
197
198 int __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  */
211 void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
212 {
213 }
214
215 /*
216  * GDB remote protocol parser:
217  */
218
219 static const char       hexchars[] = "0123456789abcdef";
220
221 static 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> */
233 static 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  */
287 static 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
338 static 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  */
350 int 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  */
380 static 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  */
405 int 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  */
430 int 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. */
451 static 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
474 static 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
491 static 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
502 static 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
516 static 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
537 static 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);
578         clocksource_touch_watchdog();
579         local_irq_restore(flags);
580 }
581 #endif
582
583 /*
584  * Some architectures need cache flushes when we set/clear a
585  * breakpoint:
586  */
587 static 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  */
603 static 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
625 static 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
666 static 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
687 static 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
701 int 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
713 int 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  */
741 static inline int shadow_pid(int realpid)
742 {
743         if (realpid)
744                 return realpid;
745
746         return -1-raw_smp_processor_id();
747 }
748
749 static char gdbmsgbuf[BUFMAX + 1];
750
751 static 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  */
793 static 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 */
813 static 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 */
828 static 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 */
873 static 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 */
886 static 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 */
904 static 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 */
915 static 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 */
926 static 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 */
951 static 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 */
972 static 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 */
1041 static 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 */
1078 static 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 */
1092 static 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 */
1154 static 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  */
1184 static 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:
1279 default_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
1302 kgdb_exit:
1303         if (ks->pass_exception)
1304                 error = 1;
1305         return error;
1306 }
1307
1308 static 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 %lx\n",
1331                         addr);
1332                 WARN_ON_ONCE(1);
1333
1334                 return 1;
1335         }
1336         remove_all_break();
1337         kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1338
1339         if (exception_level > 1) {
1340                 dump_stack();
1341                 panic("Recursive entry to debugger");
1342         }
1343
1344         printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1345         dump_stack();
1346         panic("Recursive entry to debugger");
1347
1348         return 1;
1349 }
1350
1351 /*
1352  * kgdb_handle_exception() - main entry point from a kernel exception
1353  *
1354  * Locking hierarchy:
1355  *      interface locks, if any (begin_session)
1356  *      kgdb lock (kgdb_active)
1357  */
1358 int
1359 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1360 {
1361         struct kgdb_state kgdb_var;
1362         struct kgdb_state *ks = &kgdb_var;
1363         unsigned long flags;
1364         int error = 0;
1365         int i, cpu;
1366
1367         ks->cpu                 = raw_smp_processor_id();
1368         ks->ex_vector           = evector;
1369         ks->signo               = signo;
1370         ks->ex_vector           = evector;
1371         ks->err_code            = ecode;
1372         ks->kgdb_usethreadid    = 0;
1373         ks->linux_regs          = regs;
1374
1375         if (kgdb_reenter_check(ks))
1376                 return 0; /* Ouch, double exception ! */
1377
1378 acquirelock:
1379         /*
1380          * Interrupts will be restored by the 'trap return' code, except when
1381          * single stepping.
1382          */
1383         local_irq_save(flags);
1384
1385         cpu = raw_smp_processor_id();
1386
1387         /*
1388          * Acquire the kgdb_active lock:
1389          */
1390         while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1391                 cpu_relax();
1392
1393         /*
1394          * Do not start the debugger connection on this CPU if the last
1395          * instance of the exception handler wanted to come into the
1396          * debugger on a different CPU via a single step
1397          */
1398         if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1399             atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
1400
1401                 atomic_set(&kgdb_active, -1);
1402                 clocksource_touch_watchdog();
1403                 local_irq_restore(flags);
1404
1405                 goto acquirelock;
1406         }
1407
1408         if (!kgdb_io_ready(1)) {
1409                 error = 1;
1410                 goto kgdb_restore; /* No I/O connection, so resume the system */
1411         }
1412
1413         /*
1414          * Don't enter if we have hit a removed breakpoint.
1415          */
1416         if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1417                 goto kgdb_restore;
1418
1419         /* Call the I/O driver's pre_exception routine */
1420         if (kgdb_io_ops->pre_exception)
1421                 kgdb_io_ops->pre_exception();
1422
1423         kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1424         kgdb_info[ks->cpu].task = current;
1425
1426         kgdb_disable_hw_debug(ks->linux_regs);
1427
1428         /*
1429          * Get the passive CPU lock which will hold all the non-primary
1430          * CPU in a spin state while the debugger is active
1431          */
1432         if (!kgdb_single_step || !kgdb_contthread) {
1433                 for (i = 0; i < NR_CPUS; i++)
1434                         atomic_set(&passive_cpu_wait[i], 1);
1435         }
1436
1437 #ifdef CONFIG_SMP
1438         /* Signal the other CPUs to enter kgdb_wait() */
1439         if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup)
1440                 kgdb_roundup_cpus(flags);
1441 #endif
1442
1443         /*
1444          * spin_lock code is good enough as a barrier so we don't
1445          * need one here:
1446          */
1447         atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1448
1449         /*
1450          * Wait for the other CPUs to be notified and be waiting for us:
1451          */
1452         for_each_online_cpu(i) {
1453                 while (!atomic_read(&cpu_in_kgdb[i]))
1454                         cpu_relax();
1455         }
1456
1457         /*
1458          * At this point the primary processor is completely
1459          * in the debugger and all secondary CPUs are quiescent
1460          */
1461         kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1462         kgdb_deactivate_sw_breakpoints();
1463         kgdb_single_step = 0;
1464         kgdb_contthread = NULL;
1465         exception_level = 0;
1466
1467         /* Talk to debugger with gdbserial protocol */
1468         error = gdb_serial_stub(ks);
1469
1470         /* Call the I/O driver's post_exception routine */
1471         if (kgdb_io_ops->post_exception)
1472                 kgdb_io_ops->post_exception();
1473
1474         kgdb_info[ks->cpu].debuggerinfo = NULL;
1475         kgdb_info[ks->cpu].task = NULL;
1476         atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1477
1478         if (!kgdb_single_step || !kgdb_contthread) {
1479                 for (i = NR_CPUS-1; i >= 0; i--)
1480                         atomic_set(&passive_cpu_wait[i], 0);
1481                 /*
1482                  * Wait till all the CPUs have quit
1483                  * from the debugger.
1484                  */
1485                 for_each_online_cpu(i) {
1486                         while (atomic_read(&cpu_in_kgdb[i]))
1487                                 cpu_relax();
1488                 }
1489         }
1490
1491 kgdb_restore:
1492         /* Free kgdb_active */
1493         atomic_set(&kgdb_active, -1);
1494         clocksource_touch_watchdog();
1495         local_irq_restore(flags);
1496
1497         return error;
1498 }
1499
1500 int kgdb_nmicallback(int cpu, void *regs)
1501 {
1502 #ifdef CONFIG_SMP
1503         if (!atomic_read(&cpu_in_kgdb[cpu]) &&
1504                         atomic_read(&kgdb_active) != cpu) {
1505                 kgdb_wait((struct pt_regs *)regs);
1506                 return 0;
1507         }
1508 #endif
1509         return 1;
1510 }
1511
1512 void kgdb_console_write(struct console *co, const char *s, unsigned count)
1513 {
1514         unsigned long flags;
1515
1516         /* If we're debugging, or KGDB has not connected, don't try
1517          * and print. */
1518         if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1519                 return;
1520
1521         local_irq_save(flags);
1522         kgdb_msg_write(s, count);
1523         local_irq_restore(flags);
1524 }
1525
1526 static struct console kgdbcons = {
1527         .name           = "kgdb",
1528         .write          = kgdb_console_write,
1529         .flags          = CON_PRINTBUFFER | CON_ENABLED,
1530         .index          = -1,
1531 };
1532
1533 #ifdef CONFIG_MAGIC_SYSRQ
1534 static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1535 {
1536         if (!kgdb_io_ops) {
1537                 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1538                 return;
1539         }
1540         if (!kgdb_connected)
1541                 printk(KERN_CRIT "Entering KGDB\n");
1542
1543         kgdb_breakpoint();
1544 }
1545
1546 static struct sysrq_key_op sysrq_gdb_op = {
1547         .handler        = sysrq_handle_gdb,
1548         .help_msg       = "Gdb",
1549         .action_msg     = "GDB",
1550 };
1551 #endif
1552
1553 static void kgdb_register_callbacks(void)
1554 {
1555         if (!kgdb_io_module_registered) {
1556                 kgdb_io_module_registered = 1;
1557                 kgdb_arch_init();
1558 #ifdef CONFIG_MAGIC_SYSRQ
1559                 register_sysrq_key('g', &sysrq_gdb_op);
1560 #endif
1561                 if (kgdb_use_con && !kgdb_con_registered) {
1562                         register_console(&kgdbcons);
1563                         kgdb_con_registered = 1;
1564                 }
1565         }
1566 }
1567
1568 static void kgdb_unregister_callbacks(void)
1569 {
1570         /*
1571          * When this routine is called KGDB should unregister from the
1572          * panic handler and clean up, making sure it is not handling any
1573          * break exceptions at the time.
1574          */
1575         if (kgdb_io_module_registered) {
1576                 kgdb_io_module_registered = 0;
1577                 kgdb_arch_exit();
1578 #ifdef CONFIG_MAGIC_SYSRQ
1579                 unregister_sysrq_key('g', &sysrq_gdb_op);
1580 #endif
1581                 if (kgdb_con_registered) {
1582                         unregister_console(&kgdbcons);
1583                         kgdb_con_registered = 0;
1584                 }
1585         }
1586 }
1587
1588 static void kgdb_initial_breakpoint(void)
1589 {
1590         kgdb_break_asap = 0;
1591
1592         printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1593         kgdb_breakpoint();
1594 }
1595
1596 /**
1597  *      kkgdb_register_io_module - register KGDB IO module
1598  *      @new_kgdb_io_ops: the io ops vector
1599  *
1600  *      Register it with the KGDB core.
1601  */
1602 int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1603 {
1604         int err;
1605
1606         spin_lock(&kgdb_registration_lock);
1607
1608         if (kgdb_io_ops) {
1609                 spin_unlock(&kgdb_registration_lock);
1610
1611                 printk(KERN_ERR "kgdb: Another I/O driver is already "
1612                                 "registered with KGDB.\n");
1613                 return -EBUSY;
1614         }
1615
1616         if (new_kgdb_io_ops->init) {
1617                 err = new_kgdb_io_ops->init();
1618                 if (err) {
1619                         spin_unlock(&kgdb_registration_lock);
1620                         return err;
1621                 }
1622         }
1623
1624         kgdb_io_ops = new_kgdb_io_ops;
1625
1626         spin_unlock(&kgdb_registration_lock);
1627
1628         printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1629                new_kgdb_io_ops->name);
1630
1631         /* Arm KGDB now. */
1632         kgdb_register_callbacks();
1633
1634         if (kgdb_break_asap)
1635                 kgdb_initial_breakpoint();
1636
1637         return 0;
1638 }
1639 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1640
1641 /**
1642  *      kkgdb_unregister_io_module - unregister KGDB IO module
1643  *      @old_kgdb_io_ops: the io ops vector
1644  *
1645  *      Unregister it with the KGDB core.
1646  */
1647 void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1648 {
1649         BUG_ON(kgdb_connected);
1650
1651         /*
1652          * KGDB is no longer able to communicate out, so
1653          * unregister our callbacks and reset state.
1654          */
1655         kgdb_unregister_callbacks();
1656
1657         spin_lock(&kgdb_registration_lock);
1658
1659         WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1660         kgdb_io_ops = NULL;
1661
1662         spin_unlock(&kgdb_registration_lock);
1663
1664         printk(KERN_INFO
1665                 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1666                 old_kgdb_io_ops->name);
1667 }
1668 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1669
1670 /**
1671  * kgdb_breakpoint - generate breakpoint exception
1672  *
1673  * This function will generate a breakpoint exception.  It is used at the
1674  * beginning of a program to sync up with a debugger and can be used
1675  * otherwise as a quick means to stop program execution and "break" into
1676  * the debugger.
1677  */
1678 void kgdb_breakpoint(void)
1679 {
1680         atomic_set(&kgdb_setting_breakpoint, 1);
1681         wmb(); /* Sync point before breakpoint */
1682         arch_kgdb_breakpoint();
1683         wmb(); /* Sync point after breakpoint */
1684         atomic_set(&kgdb_setting_breakpoint, 0);
1685 }
1686 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1687
1688 static int __init opt_kgdb_wait(char *str)
1689 {
1690         kgdb_break_asap = 1;
1691
1692         if (kgdb_io_module_registered)
1693                 kgdb_initial_breakpoint();
1694
1695         return 0;
1696 }
1697
1698 early_param("kgdbwait", opt_kgdb_wait);