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