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