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