Separate the gdbstub from the debug core
[sfrench/cifs-2.6.git] / kernel / debug / debug_core.c
1 /*
2  * Kernel Debug Core
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-2009 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/string.h>
41 #include <linux/delay.h>
42 #include <linux/sched.h>
43 #include <linux/sysrq.h>
44 #include <linux/init.h>
45 #include <linux/kgdb.h>
46 #include <linux/pid.h>
47 #include <linux/smp.h>
48 #include <linux/mm.h>
49
50 #include <asm/cacheflush.h>
51 #include <asm/byteorder.h>
52 #include <asm/atomic.h>
53 #include <asm/system.h>
54
55 #include "debug_core.h"
56
57 static int kgdb_break_asap;
58
59 struct debuggerinfo_struct kgdb_info[NR_CPUS];
60
61 /**
62  * kgdb_connected - Is a host GDB connected to us?
63  */
64 int                             kgdb_connected;
65 EXPORT_SYMBOL_GPL(kgdb_connected);
66
67 /* All the KGDB handlers are installed */
68 static int                      kgdb_io_module_registered;
69
70 /* Guard for recursive entry */
71 static int                      exception_level;
72
73 struct kgdb_io          *dbg_io_ops;
74 static DEFINE_SPINLOCK(kgdb_registration_lock);
75
76 /* kgdb console driver is loaded */
77 static int kgdb_con_registered;
78 /* determine if kgdb console output should be used */
79 static int kgdb_use_con;
80
81 static int __init opt_kgdb_con(char *str)
82 {
83         kgdb_use_con = 1;
84         return 0;
85 }
86
87 early_param("kgdbcon", opt_kgdb_con);
88
89 module_param(kgdb_use_con, int, 0644);
90
91 /*
92  * Holds information about breakpoints in a kernel. These breakpoints are
93  * added and removed by gdb.
94  */
95 static struct kgdb_bkpt         kgdb_break[KGDB_MAX_BREAKPOINTS] = {
96         [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
97 };
98
99 /*
100  * The CPU# of the active CPU, or -1 if none:
101  */
102 atomic_t                        kgdb_active = ATOMIC_INIT(-1);
103
104 /*
105  * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
106  * bootup code (which might not have percpu set up yet):
107  */
108 static atomic_t                 passive_cpu_wait[NR_CPUS];
109 static atomic_t                 cpu_in_kgdb[NR_CPUS];
110 atomic_t                        kgdb_setting_breakpoint;
111
112 struct task_struct              *kgdb_usethread;
113 struct task_struct              *kgdb_contthread;
114
115 int                             kgdb_single_step;
116 static pid_t                    kgdb_sstep_pid;
117
118 /* to keep track of the CPU which is doing the single stepping*/
119 atomic_t                        kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
120
121 /*
122  * If you are debugging a problem where roundup (the collection of
123  * all other CPUs) is a problem [this should be extremely rare],
124  * then use the nokgdbroundup option to avoid roundup. In that case
125  * the other CPUs might interfere with your debugging context, so
126  * use this with care:
127  */
128 static int kgdb_do_roundup = 1;
129
130 static int __init opt_nokgdbroundup(char *str)
131 {
132         kgdb_do_roundup = 0;
133
134         return 0;
135 }
136
137 early_param("nokgdbroundup", opt_nokgdbroundup);
138
139 /*
140  * Finally, some KGDB code :-)
141  */
142
143 /*
144  * Weak aliases for breakpoint management,
145  * can be overriden by architectures when needed:
146  */
147 int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
148 {
149         int err;
150
151         err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
152         if (err)
153                 return err;
154
155         return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
156                                   BREAK_INSTR_SIZE);
157 }
158
159 int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
160 {
161         return probe_kernel_write((char *)addr,
162                                   (char *)bundle, BREAK_INSTR_SIZE);
163 }
164
165 int __weak kgdb_validate_break_address(unsigned long addr)
166 {
167         char tmp_variable[BREAK_INSTR_SIZE];
168         int err;
169         /* Validate setting the breakpoint and then removing it.  In the
170          * remove fails, the kernel needs to emit a bad message because we
171          * are deep trouble not being able to put things back the way we
172          * found them.
173          */
174         err = kgdb_arch_set_breakpoint(addr, tmp_variable);
175         if (err)
176                 return err;
177         err = kgdb_arch_remove_breakpoint(addr, tmp_variable);
178         if (err)
179                 printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
180                    "memory destroyed at: %lx", addr);
181         return err;
182 }
183
184 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
185 {
186         return instruction_pointer(regs);
187 }
188
189 int __weak kgdb_arch_init(void)
190 {
191         return 0;
192 }
193
194 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
195 {
196         return 0;
197 }
198
199 void __weak
200 kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
201 {
202         return;
203 }
204
205 /**
206  *      kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
207  *      @regs: Current &struct pt_regs.
208  *
209  *      This function will be called if the particular architecture must
210  *      disable hardware debugging while it is processing gdb packets or
211  *      handling exception.
212  */
213 void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
214 {
215 }
216
217 /*
218  * Some architectures need cache flushes when we set/clear a
219  * breakpoint:
220  */
221 static void kgdb_flush_swbreak_addr(unsigned long addr)
222 {
223         if (!CACHE_FLUSH_IS_SAFE)
224                 return;
225
226         if (current->mm && current->mm->mmap_cache) {
227                 flush_cache_range(current->mm->mmap_cache,
228                                   addr, addr + BREAK_INSTR_SIZE);
229         }
230         /* Force flush instruction cache if it was outside the mm */
231         flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
232 }
233
234 /*
235  * SW breakpoint management:
236  */
237 int dbg_activate_sw_breakpoints(void)
238 {
239         unsigned long addr;
240         int error;
241         int ret = 0;
242         int i;
243
244         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
245                 if (kgdb_break[i].state != BP_SET)
246                         continue;
247
248                 addr = kgdb_break[i].bpt_addr;
249                 error = kgdb_arch_set_breakpoint(addr,
250                                 kgdb_break[i].saved_instr);
251                 if (error) {
252                         ret = error;
253                         printk(KERN_INFO "KGDB: BP install failed: %lx", addr);
254                         continue;
255                 }
256
257                 kgdb_flush_swbreak_addr(addr);
258                 kgdb_break[i].state = BP_ACTIVE;
259         }
260         return ret;
261 }
262
263 int dbg_set_sw_break(unsigned long addr)
264 {
265         int err = kgdb_validate_break_address(addr);
266         int breakno = -1;
267         int i;
268
269         if (err)
270                 return err;
271
272         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
273                 if ((kgdb_break[i].state == BP_SET) &&
274                                         (kgdb_break[i].bpt_addr == addr))
275                         return -EEXIST;
276         }
277         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
278                 if (kgdb_break[i].state == BP_REMOVED &&
279                                         kgdb_break[i].bpt_addr == addr) {
280                         breakno = i;
281                         break;
282                 }
283         }
284
285         if (breakno == -1) {
286                 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
287                         if (kgdb_break[i].state == BP_UNDEFINED) {
288                                 breakno = i;
289                                 break;
290                         }
291                 }
292         }
293
294         if (breakno == -1)
295                 return -E2BIG;
296
297         kgdb_break[breakno].state = BP_SET;
298         kgdb_break[breakno].type = BP_BREAKPOINT;
299         kgdb_break[breakno].bpt_addr = addr;
300
301         return 0;
302 }
303
304 static int kgdb_deactivate_sw_breakpoints(void)
305 {
306         unsigned long addr;
307         int error;
308         int ret = 0;
309         int i;
310
311         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
312                 if (kgdb_break[i].state != BP_ACTIVE)
313                         continue;
314                 addr = kgdb_break[i].bpt_addr;
315                 error = kgdb_arch_remove_breakpoint(addr,
316                                         kgdb_break[i].saved_instr);
317                 if (error) {
318                         printk(KERN_INFO "KGDB: BP remove failed: %lx\n", addr);
319                         ret = error;
320                 }
321
322                 kgdb_flush_swbreak_addr(addr);
323                 kgdb_break[i].state = BP_SET;
324         }
325         return ret;
326 }
327
328 int dbg_remove_sw_break(unsigned long addr)
329 {
330         int i;
331
332         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
333                 if ((kgdb_break[i].state == BP_SET) &&
334                                 (kgdb_break[i].bpt_addr == addr)) {
335                         kgdb_break[i].state = BP_REMOVED;
336                         return 0;
337                 }
338         }
339         return -ENOENT;
340 }
341
342 int kgdb_isremovedbreak(unsigned long addr)
343 {
344         int i;
345
346         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
347                 if ((kgdb_break[i].state == BP_REMOVED) &&
348                                         (kgdb_break[i].bpt_addr == addr))
349                         return 1;
350         }
351         return 0;
352 }
353
354 int dbg_remove_all_break(void)
355 {
356         unsigned long addr;
357         int error;
358         int i;
359
360         /* Clear memory breakpoints. */
361         for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
362                 if (kgdb_break[i].state != BP_ACTIVE)
363                         goto setundefined;
364                 addr = kgdb_break[i].bpt_addr;
365                 error = kgdb_arch_remove_breakpoint(addr,
366                                 kgdb_break[i].saved_instr);
367                 if (error)
368                         printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
369                            addr);
370 setundefined:
371                 kgdb_break[i].state = BP_UNDEFINED;
372         }
373
374         /* Clear hardware breakpoints. */
375         if (arch_kgdb_ops.remove_all_hw_break)
376                 arch_kgdb_ops.remove_all_hw_break();
377
378         return 0;
379 }
380
381 /*
382  * Return true if there is a valid kgdb I/O module.  Also if no
383  * debugger is attached a message can be printed to the console about
384  * waiting for the debugger to attach.
385  *
386  * The print_wait argument is only to be true when called from inside
387  * the core kgdb_handle_exception, because it will wait for the
388  * debugger to attach.
389  */
390 static int kgdb_io_ready(int print_wait)
391 {
392         if (!dbg_io_ops)
393                 return 0;
394         if (kgdb_connected)
395                 return 1;
396         if (atomic_read(&kgdb_setting_breakpoint))
397                 return 1;
398         if (print_wait)
399                 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
400         return 1;
401 }
402
403 static int kgdb_reenter_check(struct kgdb_state *ks)
404 {
405         unsigned long addr;
406
407         if (atomic_read(&kgdb_active) != raw_smp_processor_id())
408                 return 0;
409
410         /* Panic on recursive debugger calls: */
411         exception_level++;
412         addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
413         kgdb_deactivate_sw_breakpoints();
414
415         /*
416          * If the break point removed ok at the place exception
417          * occurred, try to recover and print a warning to the end
418          * user because the user planted a breakpoint in a place that
419          * KGDB needs in order to function.
420          */
421         if (dbg_remove_sw_break(addr) == 0) {
422                 exception_level = 0;
423                 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
424                 dbg_activate_sw_breakpoints();
425                 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
426                         addr);
427                 WARN_ON_ONCE(1);
428
429                 return 1;
430         }
431         dbg_remove_all_break();
432         kgdb_skipexception(ks->ex_vector, ks->linux_regs);
433
434         if (exception_level > 1) {
435                 dump_stack();
436                 panic("Recursive entry to debugger");
437         }
438
439         printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
440         dump_stack();
441         panic("Recursive entry to debugger");
442
443         return 1;
444 }
445
446 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
447 {
448         unsigned long flags;
449         int sstep_tries = 100;
450         int error = 0;
451         int i, cpu;
452         int trace_on = 0;
453 acquirelock:
454         /*
455          * Interrupts will be restored by the 'trap return' code, except when
456          * single stepping.
457          */
458         local_irq_save(flags);
459
460         cpu = ks->cpu;
461         kgdb_info[cpu].debuggerinfo = regs;
462         kgdb_info[cpu].task = current;
463         /*
464          * Make sure the above info reaches the primary CPU before
465          * our cpu_in_kgdb[] flag setting does:
466          */
467         atomic_inc(&cpu_in_kgdb[cpu]);
468
469         /*
470          * CPU will loop if it is a slave or request to become a kgdb
471          * master cpu and acquire the kgdb_active lock:
472          */
473         while (1) {
474                 if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
475                         if (atomic_cmpxchg(&kgdb_active, -1, cpu) == cpu)
476                                 break;
477                 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
478                         if (!atomic_read(&passive_cpu_wait[cpu]))
479                                 goto return_normal;
480                 } else {
481 return_normal:
482                         /* Return to normal operation by executing any
483                          * hw breakpoint fixup.
484                          */
485                         if (arch_kgdb_ops.correct_hw_break)
486                                 arch_kgdb_ops.correct_hw_break();
487                         if (trace_on)
488                                 tracing_on();
489                         atomic_dec(&cpu_in_kgdb[cpu]);
490                         touch_softlockup_watchdog_sync();
491                         clocksource_touch_watchdog();
492                         local_irq_restore(flags);
493                         return 0;
494                 }
495                 cpu_relax();
496         }
497
498         /*
499          * For single stepping, try to only enter on the processor
500          * that was single stepping.  To gaurd against a deadlock, the
501          * kernel will only try for the value of sstep_tries before
502          * giving up and continuing on.
503          */
504         if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
505             (kgdb_info[cpu].task &&
506              kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
507                 atomic_set(&kgdb_active, -1);
508                 touch_softlockup_watchdog_sync();
509                 clocksource_touch_watchdog();
510                 local_irq_restore(flags);
511
512                 goto acquirelock;
513         }
514
515         if (!kgdb_io_ready(1)) {
516                 error = 1;
517                 goto kgdb_restore; /* No I/O connection, resume the system */
518         }
519
520         /*
521          * Don't enter if we have hit a removed breakpoint.
522          */
523         if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
524                 goto kgdb_restore;
525
526         /* Call the I/O driver's pre_exception routine */
527         if (dbg_io_ops->pre_exception)
528                 dbg_io_ops->pre_exception();
529
530         kgdb_disable_hw_debug(ks->linux_regs);
531
532         /*
533          * Get the passive CPU lock which will hold all the non-primary
534          * CPU in a spin state while the debugger is active
535          */
536         if (!kgdb_single_step) {
537                 for (i = 0; i < NR_CPUS; i++)
538                         atomic_inc(&passive_cpu_wait[i]);
539         }
540
541 #ifdef CONFIG_SMP
542         /* Signal the other CPUs to enter kgdb_wait() */
543         if ((!kgdb_single_step) && kgdb_do_roundup)
544                 kgdb_roundup_cpus(flags);
545 #endif
546
547         /*
548          * Wait for the other CPUs to be notified and be waiting for us:
549          */
550         for_each_online_cpu(i) {
551                 while (!atomic_read(&cpu_in_kgdb[i]))
552                         cpu_relax();
553         }
554
555         /*
556          * At this point the primary processor is completely
557          * in the debugger and all secondary CPUs are quiescent
558          */
559         kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
560         kgdb_deactivate_sw_breakpoints();
561         kgdb_single_step = 0;
562         kgdb_contthread = current;
563         exception_level = 0;
564         trace_on = tracing_is_on();
565         if (trace_on)
566                 tracing_off();
567
568         /* Talk to debugger with gdbserial protocol */
569         error = gdb_serial_stub(ks);
570
571         /* Call the I/O driver's post_exception routine */
572         if (dbg_io_ops->post_exception)
573                 dbg_io_ops->post_exception();
574
575         atomic_dec(&cpu_in_kgdb[ks->cpu]);
576
577         if (!kgdb_single_step) {
578                 for (i = NR_CPUS-1; i >= 0; i--)
579                         atomic_dec(&passive_cpu_wait[i]);
580                 /*
581                  * Wait till all the CPUs have quit
582                  * from the debugger.
583                  */
584                 for_each_online_cpu(i) {
585                         while (atomic_read(&cpu_in_kgdb[i]))
586                                 cpu_relax();
587                 }
588         }
589
590 kgdb_restore:
591         if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
592                 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
593                 if (kgdb_info[sstep_cpu].task)
594                         kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
595                 else
596                         kgdb_sstep_pid = 0;
597         }
598         if (trace_on)
599                 tracing_on();
600         /* Free kgdb_active */
601         atomic_set(&kgdb_active, -1);
602         touch_softlockup_watchdog_sync();
603         clocksource_touch_watchdog();
604         local_irq_restore(flags);
605
606         return error;
607 }
608
609 /*
610  * kgdb_handle_exception() - main entry point from a kernel exception
611  *
612  * Locking hierarchy:
613  *      interface locks, if any (begin_session)
614  *      kgdb lock (kgdb_active)
615  */
616 int
617 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
618 {
619         struct kgdb_state kgdb_var;
620         struct kgdb_state *ks = &kgdb_var;
621         int ret;
622
623         ks->cpu                 = raw_smp_processor_id();
624         ks->ex_vector           = evector;
625         ks->signo               = signo;
626         ks->ex_vector           = evector;
627         ks->err_code            = ecode;
628         ks->kgdb_usethreadid    = 0;
629         ks->linux_regs          = regs;
630
631         if (kgdb_reenter_check(ks))
632                 return 0; /* Ouch, double exception ! */
633         kgdb_info[ks->cpu].exception_state |= DCPU_WANT_MASTER;
634         ret = kgdb_cpu_enter(ks, regs);
635         kgdb_info[ks->cpu].exception_state &= ~DCPU_WANT_MASTER;
636         return ret;
637 }
638
639 int kgdb_nmicallback(int cpu, void *regs)
640 {
641 #ifdef CONFIG_SMP
642         struct kgdb_state kgdb_var;
643         struct kgdb_state *ks = &kgdb_var;
644
645         memset(ks, 0, sizeof(struct kgdb_state));
646         ks->cpu                 = cpu;
647         ks->linux_regs          = regs;
648
649         if (!atomic_read(&cpu_in_kgdb[cpu]) &&
650             atomic_read(&kgdb_active) != -1 &&
651             atomic_read(&kgdb_active) != cpu) {
652                 kgdb_info[cpu].exception_state |= DCPU_IS_SLAVE;
653                 kgdb_cpu_enter(ks, regs);
654                 kgdb_info[cpu].exception_state &= ~DCPU_IS_SLAVE;
655                 return 0;
656         }
657 #endif
658         return 1;
659 }
660
661 static void kgdb_console_write(struct console *co, const char *s,
662    unsigned count)
663 {
664         unsigned long flags;
665
666         /* If we're debugging, or KGDB has not connected, don't try
667          * and print. */
668         if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
669                 return;
670
671         local_irq_save(flags);
672         gdbstub_msg_write(s, count);
673         local_irq_restore(flags);
674 }
675
676 static struct console kgdbcons = {
677         .name           = "kgdb",
678         .write          = kgdb_console_write,
679         .flags          = CON_PRINTBUFFER | CON_ENABLED,
680         .index          = -1,
681 };
682
683 #ifdef CONFIG_MAGIC_SYSRQ
684 static void sysrq_handle_dbg(int key, struct tty_struct *tty)
685 {
686         if (!dbg_io_ops) {
687                 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
688                 return;
689         }
690         if (!kgdb_connected)
691                 printk(KERN_CRIT "Entering KGDB\n");
692
693         kgdb_breakpoint();
694 }
695
696 static struct sysrq_key_op sysrq_dbg_op = {
697         .handler        = sysrq_handle_dbg,
698         .help_msg       = "debug(G)",
699         .action_msg     = "DEBUG",
700 };
701 #endif
702
703 static void kgdb_register_callbacks(void)
704 {
705         if (!kgdb_io_module_registered) {
706                 kgdb_io_module_registered = 1;
707                 kgdb_arch_init();
708 #ifdef CONFIG_MAGIC_SYSRQ
709                 register_sysrq_key('g', &sysrq_dbg_op);
710 #endif
711                 if (kgdb_use_con && !kgdb_con_registered) {
712                         register_console(&kgdbcons);
713                         kgdb_con_registered = 1;
714                 }
715         }
716 }
717
718 static void kgdb_unregister_callbacks(void)
719 {
720         /*
721          * When this routine is called KGDB should unregister from the
722          * panic handler and clean up, making sure it is not handling any
723          * break exceptions at the time.
724          */
725         if (kgdb_io_module_registered) {
726                 kgdb_io_module_registered = 0;
727                 kgdb_arch_exit();
728 #ifdef CONFIG_MAGIC_SYSRQ
729                 unregister_sysrq_key('g', &sysrq_dbg_op);
730 #endif
731                 if (kgdb_con_registered) {
732                         unregister_console(&kgdbcons);
733                         kgdb_con_registered = 0;
734                 }
735         }
736 }
737
738 static void kgdb_initial_breakpoint(void)
739 {
740         kgdb_break_asap = 0;
741
742         printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
743         kgdb_breakpoint();
744 }
745
746 /**
747  *      kgdb_register_io_module - register KGDB IO module
748  *      @new_dbg_io_ops: the io ops vector
749  *
750  *      Register it with the KGDB core.
751  */
752 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
753 {
754         int err;
755
756         spin_lock(&kgdb_registration_lock);
757
758         if (dbg_io_ops) {
759                 spin_unlock(&kgdb_registration_lock);
760
761                 printk(KERN_ERR "kgdb: Another I/O driver is already "
762                                 "registered with KGDB.\n");
763                 return -EBUSY;
764         }
765
766         if (new_dbg_io_ops->init) {
767                 err = new_dbg_io_ops->init();
768                 if (err) {
769                         spin_unlock(&kgdb_registration_lock);
770                         return err;
771                 }
772         }
773
774         dbg_io_ops = new_dbg_io_ops;
775
776         spin_unlock(&kgdb_registration_lock);
777
778         printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
779                new_dbg_io_ops->name);
780
781         /* Arm KGDB now. */
782         kgdb_register_callbacks();
783
784         if (kgdb_break_asap)
785                 kgdb_initial_breakpoint();
786
787         return 0;
788 }
789 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
790
791 /**
792  *      kkgdb_unregister_io_module - unregister KGDB IO module
793  *      @old_dbg_io_ops: the io ops vector
794  *
795  *      Unregister it with the KGDB core.
796  */
797 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
798 {
799         BUG_ON(kgdb_connected);
800
801         /*
802          * KGDB is no longer able to communicate out, so
803          * unregister our callbacks and reset state.
804          */
805         kgdb_unregister_callbacks();
806
807         spin_lock(&kgdb_registration_lock);
808
809         WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
810         dbg_io_ops = NULL;
811
812         spin_unlock(&kgdb_registration_lock);
813
814         printk(KERN_INFO
815                 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
816                 old_dbg_io_ops->name);
817 }
818 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
819
820 /**
821  * kgdb_breakpoint - generate breakpoint exception
822  *
823  * This function will generate a breakpoint exception.  It is used at the
824  * beginning of a program to sync up with a debugger and can be used
825  * otherwise as a quick means to stop program execution and "break" into
826  * the debugger.
827  */
828 void kgdb_breakpoint(void)
829 {
830         atomic_inc(&kgdb_setting_breakpoint);
831         wmb(); /* Sync point before breakpoint */
832         arch_kgdb_breakpoint();
833         wmb(); /* Sync point after breakpoint */
834         atomic_dec(&kgdb_setting_breakpoint);
835 }
836 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
837
838 static int __init opt_kgdb_wait(char *str)
839 {
840         kgdb_break_asap = 1;
841
842         if (kgdb_io_module_registered)
843                 kgdb_initial_breakpoint();
844
845         return 0;
846 }
847
848 early_param("kgdbwait", opt_kgdb_wait);