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