import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / debug / debug_core.c
CommitLineData
dc7d5527 1/*
53197fc4 2 * Kernel Debug Core
dc7d5527
JW
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>
a2531293 9 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
dc7d5527
JW
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
53197fc4 12 * Copyright (C) 2005-2009 Wind River Systems, Inc.
dc7d5527
JW
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>
7c3078b6 31#include <linux/clocksource.h>
16559ae4 32#include <linux/serial_core.h>
dc7d5527
JW
33#include <linux/interrupt.h>
34#include <linux/spinlock.h>
35#include <linux/console.h>
36#include <linux/threads.h>
37#include <linux/uaccess.h>
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/ptrace.h>
dc7d5527
JW
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/sched.h>
44#include <linux/sysrq.h>
2366e047 45#include <linux/reboot.h>
dc7d5527
JW
46#include <linux/init.h>
47#include <linux/kgdb.h>
dcc78711 48#include <linux/kdb.h>
dc7d5527
JW
49#include <linux/pid.h>
50#include <linux/smp.h>
51#include <linux/mm.h>
fb70b588 52#include <linux/rcupdate.h>
dc7d5527
JW
53
54#include <asm/cacheflush.h>
55#include <asm/byteorder.h>
60063497 56#include <linux/atomic.h>
dc7d5527 57
53197fc4 58#include "debug_core.h"
dc7d5527 59
53197fc4 60static int kgdb_break_asap;
62fae312 61
53197fc4 62struct debuggerinfo_struct kgdb_info[NR_CPUS];
dc7d5527
JW
63
64/**
65 * kgdb_connected - Is a host GDB connected to us?
66 */
67int kgdb_connected;
68EXPORT_SYMBOL_GPL(kgdb_connected);
69
70/* All the KGDB handlers are installed */
f503b5ae 71int kgdb_io_module_registered;
dc7d5527
JW
72
73/* Guard for recursive entry */
74static int exception_level;
75
53197fc4 76struct kgdb_io *dbg_io_ops;
dc7d5527
JW
77static DEFINE_SPINLOCK(kgdb_registration_lock);
78
bec4d62e
JW
79/* Action for the reboot notifiter, a global allow kdb to change it */
80static int kgdbreboot;
dc7d5527
JW
81/* kgdb console driver is loaded */
82static int kgdb_con_registered;
83/* determine if kgdb console output should be used */
84static int kgdb_use_con;
0b4b3827
JW
85/* Flag for alternate operations for early debugging */
86bool dbg_is_early = true;
dcc78711
JW
87/* Next cpu to become the master debug core */
88int dbg_switch_cpu;
6fa3eb70
S
89/* Flag for entering kdb when a panic occurs */
90static bool break_on_panic = true;
91/* Flag for entering kdb when an exception occurs */
92static bool break_on_exception = true;
dcc78711
JW
93
94/* Use kdb or gdbserver mode */
a0de055c 95int dbg_kdb_mode = 1;
dc7d5527
JW
96
97static int __init opt_kgdb_con(char *str)
98{
99 kgdb_use_con = 1;
100 return 0;
101}
102
103early_param("kgdbcon", opt_kgdb_con);
104
105module_param(kgdb_use_con, int, 0644);
bec4d62e 106module_param(kgdbreboot, int, 0644);
6fa3eb70
S
107module_param(break_on_panic, bool, 0644);
108module_param(break_on_exception, bool, 0644);
dc7d5527
JW
109
110/*
111 * Holds information about breakpoints in a kernel. These breakpoints are
112 * added and removed by gdb.
113 */
114static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
115 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
116};
117
118/*
119 * The CPU# of the active CPU, or -1 if none:
120 */
121atomic_t kgdb_active = ATOMIC_INIT(-1);
dcc78711 122EXPORT_SYMBOL_GPL(kgdb_active);
dfee3a7b
JW
123static DEFINE_RAW_SPINLOCK(dbg_master_lock);
124static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
dc7d5527
JW
125
126/*
127 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
128 * bootup code (which might not have percpu set up yet):
129 */
dfee3a7b
JW
130static atomic_t masters_in_kgdb;
131static atomic_t slaves_in_kgdb;
1cee5e35 132static atomic_t kgdb_break_tasklet_var;
dc7d5527
JW
133atomic_t kgdb_setting_breakpoint;
134
135struct task_struct *kgdb_usethread;
136struct task_struct *kgdb_contthread;
137
138int kgdb_single_step;
53197fc4 139static pid_t kgdb_sstep_pid;
dc7d5527
JW
140
141/* to keep track of the CPU which is doing the single stepping*/
142atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
143
144/*
145 * If you are debugging a problem where roundup (the collection of
146 * all other CPUs) is a problem [this should be extremely rare],
147 * then use the nokgdbroundup option to avoid roundup. In that case
148 * the other CPUs might interfere with your debugging context, so
149 * use this with care:
150 */
688b744d 151static int kgdb_do_roundup = 1;
dc7d5527
JW
152
153static int __init opt_nokgdbroundup(char *str)
154{
155 kgdb_do_roundup = 0;
156
157 return 0;
158}
159
160early_param("nokgdbroundup", opt_nokgdbroundup);
161
162/*
163 * Finally, some KGDB code :-)
164 */
165
166/*
167 * Weak aliases for breakpoint management,
168 * can be overriden by architectures when needed:
169 */
98b54aa1 170int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
dc7d5527
JW
171{
172 int err;
173
98b54aa1
JW
174 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
175 BREAK_INSTR_SIZE);
dc7d5527
JW
176 if (err)
177 return err;
98b54aa1
JW
178 err = probe_kernel_write((char *)bpt->bpt_addr,
179 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
180 return err;
dc7d5527
JW
181}
182
98b54aa1 183int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
dc7d5527 184{
98b54aa1
JW
185 return probe_kernel_write((char *)bpt->bpt_addr,
186 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
dc7d5527
JW
187}
188
a9b60bf4
JW
189int __weak kgdb_validate_break_address(unsigned long addr)
190{
98b54aa1 191 struct kgdb_bkpt tmp;
a9b60bf4 192 int err;
98b54aa1 193 /* Validate setting the breakpoint and then removing it. If the
a9b60bf4
JW
194 * remove fails, the kernel needs to emit a bad message because we
195 * are deep trouble not being able to put things back the way we
196 * found them.
197 */
98b54aa1
JW
198 tmp.bpt_addr = addr;
199 err = kgdb_arch_set_breakpoint(&tmp);
a9b60bf4
JW
200 if (err)
201 return err;
98b54aa1 202 err = kgdb_arch_remove_breakpoint(&tmp);
a9b60bf4
JW
203 if (err)
204 printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
205 "memory destroyed at: %lx", addr);
206 return err;
207}
208
dc7d5527
JW
209unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
210{
211 return instruction_pointer(regs);
212}
213
214int __weak kgdb_arch_init(void)
215{
216 return 0;
217}
218
b4b8ac52
JW
219int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
220{
221 return 0;
222}
223
dc7d5527
JW
224/*
225 * Some architectures need cache flushes when we set/clear a
226 * breakpoint:
227 */
228static void kgdb_flush_swbreak_addr(unsigned long addr)
229{
230 if (!CACHE_FLUSH_IS_SAFE)
231 return;
232
737a460f 233 if (current->mm && current->mm->mmap_cache) {
dc7d5527
JW
234 flush_cache_range(current->mm->mmap_cache,
235 addr, addr + BREAK_INSTR_SIZE);
dc7d5527 236 }
1a9a3e76
JW
237 /* Force flush instruction cache if it was outside the mm */
238 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
dc7d5527
JW
239}
240
241/*
242 * SW breakpoint management:
243 */
53197fc4 244int dbg_activate_sw_breakpoints(void)
dc7d5527 245{
7f8b7ed6
JW
246 int error;
247 int ret = 0;
dc7d5527
JW
248 int i;
249
250 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
251 if (kgdb_break[i].state != BP_SET)
252 continue;
253
98b54aa1 254 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
7f8b7ed6
JW
255 if (error) {
256 ret = error;
98b54aa1
JW
257 printk(KERN_INFO "KGDB: BP install failed: %lx",
258 kgdb_break[i].bpt_addr);
7f8b7ed6
JW
259 continue;
260 }
dc7d5527 261
98b54aa1 262 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
dc7d5527
JW
263 kgdb_break[i].state = BP_ACTIVE;
264 }
7f8b7ed6 265 return ret;
dc7d5527
JW
266}
267
53197fc4 268int dbg_set_sw_break(unsigned long addr)
dc7d5527
JW
269{
270 int err = kgdb_validate_break_address(addr);
271 int breakno = -1;
272 int i;
273
274 if (err)
275 return err;
276
277 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
278 if ((kgdb_break[i].state == BP_SET) &&
279 (kgdb_break[i].bpt_addr == addr))
280 return -EEXIST;
281 }
282 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
283 if (kgdb_break[i].state == BP_REMOVED &&
284 kgdb_break[i].bpt_addr == addr) {
285 breakno = i;
286 break;
287 }
288 }
289
290 if (breakno == -1) {
291 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
292 if (kgdb_break[i].state == BP_UNDEFINED) {
293 breakno = i;
294 break;
295 }
296 }
297 }
298
299 if (breakno == -1)
300 return -E2BIG;
301
302 kgdb_break[breakno].state = BP_SET;
303 kgdb_break[breakno].type = BP_BREAKPOINT;
304 kgdb_break[breakno].bpt_addr = addr;
305
306 return 0;
307}
308
dcc78711 309int dbg_deactivate_sw_breakpoints(void)
dc7d5527 310{
7f8b7ed6
JW
311 int error;
312 int ret = 0;
dc7d5527
JW
313 int i;
314
315 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
316 if (kgdb_break[i].state != BP_ACTIVE)
317 continue;
98b54aa1 318 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
7f8b7ed6 319 if (error) {
98b54aa1
JW
320 printk(KERN_INFO "KGDB: BP remove failed: %lx\n",
321 kgdb_break[i].bpt_addr);
7f8b7ed6
JW
322 ret = error;
323 }
dc7d5527 324
98b54aa1 325 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
dc7d5527
JW
326 kgdb_break[i].state = BP_SET;
327 }
7f8b7ed6 328 return ret;
dc7d5527
JW
329}
330
53197fc4 331int dbg_remove_sw_break(unsigned long addr)
dc7d5527
JW
332{
333 int i;
334
335 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
336 if ((kgdb_break[i].state == BP_SET) &&
337 (kgdb_break[i].bpt_addr == addr)) {
338 kgdb_break[i].state = BP_REMOVED;
339 return 0;
340 }
341 }
342 return -ENOENT;
343}
344
345int kgdb_isremovedbreak(unsigned long addr)
346{
347 int i;
348
349 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
350 if ((kgdb_break[i].state == BP_REMOVED) &&
351 (kgdb_break[i].bpt_addr == addr))
352 return 1;
353 }
354 return 0;
355}
356
53197fc4 357int dbg_remove_all_break(void)
dc7d5527 358{
dc7d5527
JW
359 int error;
360 int i;
361
362 /* Clear memory breakpoints. */
363 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
737a460f
JW
364 if (kgdb_break[i].state != BP_ACTIVE)
365 goto setundefined;
98b54aa1 366 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
dc7d5527 367 if (error)
737a460f 368 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
98b54aa1 369 kgdb_break[i].bpt_addr);
737a460f
JW
370setundefined:
371 kgdb_break[i].state = BP_UNDEFINED;
dc7d5527
JW
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
dc7d5527
JW
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 */
390static int kgdb_io_ready(int print_wait)
391{
53197fc4 392 if (!dbg_io_ops)
dc7d5527
JW
393 return 0;
394 if (kgdb_connected)
395 return 1;
396 if (atomic_read(&kgdb_setting_breakpoint))
397 return 1;
dcc78711
JW
398 if (print_wait) {
399#ifdef CONFIG_KGDB_KDB
400 if (!dbg_kdb_mode)
401 printk(KERN_CRIT "KGDB: waiting... or $3#33 for KDB\n");
402#else
dc7d5527 403 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
dcc78711
JW
404#endif
405 }
dc7d5527
JW
406 return 1;
407}
408
dc7d5527
JW
409static int kgdb_reenter_check(struct kgdb_state *ks)
410{
411 unsigned long addr;
412
413 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
414 return 0;
415
416 /* Panic on recursive debugger calls: */
417 exception_level++;
418 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
dcc78711 419 dbg_deactivate_sw_breakpoints();
dc7d5527
JW
420
421 /*
422 * If the break point removed ok at the place exception
423 * occurred, try to recover and print a warning to the end
424 * user because the user planted a breakpoint in a place that
425 * KGDB needs in order to function.
426 */
53197fc4 427 if (dbg_remove_sw_break(addr) == 0) {
dc7d5527
JW
428 exception_level = 0;
429 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
53197fc4 430 dbg_activate_sw_breakpoints();
67baf94c
JW
431 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
432 addr);
dc7d5527
JW
433 WARN_ON_ONCE(1);
434
435 return 1;
436 }
53197fc4 437 dbg_remove_all_break();
dc7d5527
JW
438 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
439
440 if (exception_level > 1) {
441 dump_stack();
442 panic("Recursive entry to debugger");
443 }
444
445 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
6d906340
JW
446#ifdef CONFIG_KGDB_KDB
447 /* Allow kdb to debug itself one level */
448 return 0;
449#endif
dc7d5527
JW
450 dump_stack();
451 panic("Recursive entry to debugger");
452
453 return 1;
454}
455
16cdc628
JW
456static void dbg_touch_watchdogs(void)
457{
458 touch_softlockup_watchdog_sync();
459 clocksource_touch_watchdog();
fb70b588 460 rcu_cpu_stall_reset();
16cdc628
JW
461}
462
dfee3a7b
JW
463static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
464 int exception_state)
dc7d5527 465{
dc7d5527 466 unsigned long flags;
028e7b17 467 int sstep_tries = 100;
dcc78711 468 int error;
dfee3a7b 469 int cpu;
4da75b9c 470 int trace_on = 0;
dfee3a7b 471 int online_cpus = num_online_cpus();
c1bb9a9c 472
6fa3eb70
S
473 #ifdef CONFIG_KGDB_KDB
474 if (force_panic) /* Force panic in previous KDB, so skip this time */
475 return NOTIFY_DONE;
476 #endif
477
dfee3a7b
JW
478 kgdb_info[ks->cpu].enter_kgdb++;
479 kgdb_info[ks->cpu].exception_state |= exception_state;
480
481 if (exception_state == DCPU_WANT_MASTER)
482 atomic_inc(&masters_in_kgdb);
483 else
484 atomic_inc(&slaves_in_kgdb);
d7ba979d
DD
485
486 if (arch_kgdb_ops.disable_hw_break)
487 arch_kgdb_ops.disable_hw_break(regs);
c1bb9a9c 488
dc7d5527
JW
489acquirelock:
490 /*
491 * Interrupts will be restored by the 'trap return' code, except when
492 * single stepping.
493 */
494 local_irq_save(flags);
495
62fae312
JW
496 cpu = ks->cpu;
497 kgdb_info[cpu].debuggerinfo = regs;
498 kgdb_info[cpu].task = current;
dcc78711
JW
499 kgdb_info[cpu].ret_state = 0;
500 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
dc7d5527 501
dfee3a7b
JW
502 /* Make sure the above info reaches the primary CPU */
503 smp_mb();
504
505 if (exception_level == 1) {
506 if (raw_spin_trylock(&dbg_master_lock))
507 atomic_xchg(&kgdb_active, cpu);
6d906340 508 goto cpu_master_loop;
dfee3a7b 509 }
6d906340 510
dc7d5527 511 /*
62fae312
JW
512 * CPU will loop if it is a slave or request to become a kgdb
513 * master cpu and acquire the kgdb_active lock:
dc7d5527 514 */
62fae312 515 while (1) {
dcc78711
JW
516cpu_loop:
517 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
518 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
519 goto cpu_master_loop;
520 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
dfee3a7b
JW
521 if (raw_spin_trylock(&dbg_master_lock)) {
522 atomic_xchg(&kgdb_active, cpu);
62fae312 523 break;
dfee3a7b 524 }
62fae312 525 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
dfee3a7b 526 if (!raw_spin_is_locked(&dbg_slave_lock))
62fae312
JW
527 goto return_normal;
528 } else {
529return_normal:
530 /* Return to normal operation by executing any
531 * hw breakpoint fixup.
532 */
533 if (arch_kgdb_ops.correct_hw_break)
534 arch_kgdb_ops.correct_hw_break();
4da75b9c
JW
535 if (trace_on)
536 tracing_on();
dfee3a7b
JW
537 kgdb_info[cpu].exception_state &=
538 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
539 kgdb_info[cpu].enter_kgdb--;
540 smp_mb__before_atomic_dec();
541 atomic_dec(&slaves_in_kgdb);
16cdc628 542 dbg_touch_watchdogs();
62fae312
JW
543 local_irq_restore(flags);
544 return 0;
545 }
dc7d5527 546 cpu_relax();
62fae312 547 }
dc7d5527
JW
548
549 /*
028e7b17 550 * For single stepping, try to only enter on the processor
25985edc 551 * that was single stepping. To guard against a deadlock, the
028e7b17
JW
552 * kernel will only try for the value of sstep_tries before
553 * giving up and continuing on.
dc7d5527
JW
554 */
555 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
028e7b17
JW
556 (kgdb_info[cpu].task &&
557 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
dc7d5527 558 atomic_set(&kgdb_active, -1);
dfee3a7b 559 raw_spin_unlock(&dbg_master_lock);
16cdc628 560 dbg_touch_watchdogs();
dc7d5527
JW
561 local_irq_restore(flags);
562
563 goto acquirelock;
564 }
565
566 if (!kgdb_io_ready(1)) {
dcc78711 567 kgdb_info[cpu].ret_state = 1;
53197fc4 568 goto kgdb_restore; /* No I/O connection, resume the system */
dc7d5527
JW
569 }
570
571 /*
572 * Don't enter if we have hit a removed breakpoint.
573 */
574 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
575 goto kgdb_restore;
576
577 /* Call the I/O driver's pre_exception routine */
53197fc4
JW
578 if (dbg_io_ops->pre_exception)
579 dbg_io_ops->pre_exception();
dc7d5527 580
dc7d5527
JW
581 /*
582 * Get the passive CPU lock which will hold all the non-primary
583 * CPU in a spin state while the debugger is active
584 */
dfee3a7b
JW
585 if (!kgdb_single_step)
586 raw_spin_lock(&dbg_slave_lock);
dc7d5527 587
56fb7093
JW
588#ifdef CONFIG_SMP
589 /* Signal the other CPUs to enter kgdb_wait() */
d7161a65 590 if ((!kgdb_single_step) && kgdb_do_roundup)
56fb7093
JW
591 kgdb_roundup_cpus(flags);
592#endif
593
dc7d5527
JW
594 /*
595 * Wait for the other CPUs to be notified and be waiting for us:
596 */
dfee3a7b
JW
597 while (kgdb_do_roundup && (atomic_read(&masters_in_kgdb) +
598 atomic_read(&slaves_in_kgdb)) != online_cpus)
599 cpu_relax();
dc7d5527
JW
600
601 /*
602 * At this point the primary processor is completely
603 * in the debugger and all secondary CPUs are quiescent
604 */
dcc78711 605 dbg_deactivate_sw_breakpoints();
dc7d5527 606 kgdb_single_step = 0;
d7161a65 607 kgdb_contthread = current;
dc7d5527 608 exception_level = 0;
4da75b9c
JW
609 trace_on = tracing_is_on();
610 if (trace_on)
611 tracing_off();
dc7d5527 612
dcc78711
JW
613 while (1) {
614cpu_master_loop:
615 if (dbg_kdb_mode) {
616 kgdb_connected = 1;
617 error = kdb_stub(ks);
3fa43aba
JW
618 if (error == -1)
619 continue;
b0679c63 620 kgdb_connected = 0;
dcc78711
JW
621 } else {
622 error = gdb_serial_stub(ks);
623 }
624
625 if (error == DBG_PASS_EVENT) {
626 dbg_kdb_mode = !dbg_kdb_mode;
dcc78711 627 } else if (error == DBG_SWITCH_CPU_EVENT) {
495363d3
JW
628 kgdb_info[dbg_switch_cpu].exception_state |=
629 DCPU_NEXT_MASTER;
dcc78711
JW
630 goto cpu_loop;
631 } else {
632 kgdb_info[cpu].ret_state = error;
633 break;
634 }
635 }
dc7d5527
JW
636
637 /* Call the I/O driver's post_exception routine */
53197fc4
JW
638 if (dbg_io_ops->post_exception)
639 dbg_io_ops->post_exception();
dc7d5527 640
d7161a65 641 if (!kgdb_single_step) {
dfee3a7b
JW
642 raw_spin_unlock(&dbg_slave_lock);
643 /* Wait till all the CPUs have quit from the debugger. */
644 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
645 cpu_relax();
dc7d5527
JW
646 }
647
648kgdb_restore:
028e7b17
JW
649 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
650 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
651 if (kgdb_info[sstep_cpu].task)
652 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
653 else
654 kgdb_sstep_pid = 0;
655 }
c1bb9a9c
DD
656 if (arch_kgdb_ops.correct_hw_break)
657 arch_kgdb_ops.correct_hw_break();
4da75b9c
JW
658 if (trace_on)
659 tracing_on();
dfee3a7b
JW
660
661 kgdb_info[cpu].exception_state &=
662 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
663 kgdb_info[cpu].enter_kgdb--;
664 smp_mb__before_atomic_dec();
665 atomic_dec(&masters_in_kgdb);
dc7d5527
JW
666 /* Free kgdb_active */
667 atomic_set(&kgdb_active, -1);
dfee3a7b 668 raw_spin_unlock(&dbg_master_lock);
16cdc628 669 dbg_touch_watchdogs();
dc7d5527
JW
670 local_irq_restore(flags);
671
6fa3eb70
S
672 #ifdef CONFIG_KGDB_KDB
673 /* If no user input, force trigger kernel panic here */
674 if (force_panic) {
675 printk("KDB : Force Kernal Panic ! \n");
676 do { *(volatile int *)0 = 0; } while (1);
677 }
678 #endif
679
dcc78711 680 return kgdb_info[cpu].ret_state;
dc7d5527
JW
681}
682
62fae312
JW
683/*
684 * kgdb_handle_exception() - main entry point from a kernel exception
685 *
686 * Locking hierarchy:
687 * interface locks, if any (begin_session)
688 * kgdb lock (kgdb_active)
689 */
690int
691kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
692{
693 struct kgdb_state kgdb_var;
694 struct kgdb_state *ks = &kgdb_var;
5a14fead
AV
695 int ret = 0;
696
697 if (arch_kgdb_ops.enable_nmi)
698 arch_kgdb_ops.enable_nmi(0);
62fae312 699
6fa3eb70
S
700 if (unlikely(signo != SIGTRAP && !break_on_exception))
701 return 1;
702
62fae312
JW
703 ks->cpu = raw_smp_processor_id();
704 ks->ex_vector = evector;
705 ks->signo = signo;
62fae312
JW
706 ks->err_code = ecode;
707 ks->kgdb_usethreadid = 0;
708 ks->linux_regs = regs;
709
710 if (kgdb_reenter_check(ks))
5a14fead 711 goto out; /* Ouch, double exception ! */
dfee3a7b 712 if (kgdb_info[ks->cpu].enter_kgdb != 0)
5a14fead 713 goto out;
dfee3a7b 714
5a14fead
AV
715 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
716out:
717 if (arch_kgdb_ops.enable_nmi)
718 arch_kgdb_ops.enable_nmi(1);
719 return ret;
62fae312
JW
720}
721
f30fed10
JW
722/*
723 * GDB places a breakpoint at this function to know dynamically
724 * loaded objects. It's not defined static so that only one instance with this
725 * name exists in the kernel.
726 */
727
728static int module_event(struct notifier_block *self, unsigned long val,
729 void *data)
730{
731 return 0;
732}
733
734static struct notifier_block dbg_module_load_nb = {
735 .notifier_call = module_event,
736};
737
dc7d5527
JW
738int kgdb_nmicallback(int cpu, void *regs)
739{
740#ifdef CONFIG_SMP
62fae312
JW
741 struct kgdb_state kgdb_var;
742 struct kgdb_state *ks = &kgdb_var;
743
744 memset(ks, 0, sizeof(struct kgdb_state));
745 ks->cpu = cpu;
746 ks->linux_regs = regs;
747
dfee3a7b
JW
748 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
749 raw_spin_is_locked(&dbg_master_lock)) {
750 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
dc7d5527
JW
751 return 0;
752 }
753#endif
754 return 1;
755}
756
aabdc3b8
JW
757static void kgdb_console_write(struct console *co, const char *s,
758 unsigned count)
dc7d5527
JW
759{
760 unsigned long flags;
761
762 /* If we're debugging, or KGDB has not connected, don't try
763 * and print. */
dcc78711 764 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
dc7d5527
JW
765 return;
766
767 local_irq_save(flags);
53197fc4 768 gdbstub_msg_write(s, count);
dc7d5527
JW
769 local_irq_restore(flags);
770}
771
772static struct console kgdbcons = {
773 .name = "kgdb",
774 .write = kgdb_console_write,
775 .flags = CON_PRINTBUFFER | CON_ENABLED,
776 .index = -1,
777};
778
779#ifdef CONFIG_MAGIC_SYSRQ
1495cc9d 780static void sysrq_handle_dbg(int key)
dc7d5527 781{
53197fc4 782 if (!dbg_io_ops) {
dc7d5527
JW
783 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
784 return;
785 }
dcc78711
JW
786 if (!kgdb_connected) {
787#ifdef CONFIG_KGDB_KDB
788 if (!dbg_kdb_mode)
789 printk(KERN_CRIT "KGDB or $3#33 for KDB\n");
790#else
dc7d5527 791 printk(KERN_CRIT "Entering KGDB\n");
dcc78711
JW
792#endif
793 }
dc7d5527
JW
794
795 kgdb_breakpoint();
796}
797
53197fc4
JW
798static struct sysrq_key_op sysrq_dbg_op = {
799 .handler = sysrq_handle_dbg,
f3456509 800 .help_msg = "debug(g)",
364b5b7b 801 .action_msg = "DEBUG",
dc7d5527
JW
802};
803#endif
804
4402c153
JW
805static int kgdb_panic_event(struct notifier_block *self,
806 unsigned long val,
807 void *data)
808{
6fa3eb70
S
809 #ifdef CONFIG_KGDB_KDB
810 if (force_panic) /* Force panic in previous KDB, so skip this time */
811 return NOTIFY_DONE;
812 #endif
813
814 if (!break_on_panic)
815 return NOTIFY_DONE;
816
4402c153
JW
817 if (dbg_kdb_mode)
818 kdb_printf("PANIC: %s\n", (char *)data);
819 kgdb_breakpoint();
820 return NOTIFY_DONE;
821}
822
823static struct notifier_block kgdb_panic_event_nb = {
824 .notifier_call = kgdb_panic_event,
825 .priority = INT_MAX,
826};
827
0b4b3827
JW
828void __weak kgdb_arch_late(void)
829{
830}
831
832void __init dbg_late_init(void)
833{
834 dbg_is_early = false;
835 if (kgdb_io_module_registered)
836 kgdb_arch_late();
837 kdb_init(KDB_INIT_FULL);
838}
839
2366e047
JW
840static int
841dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
842{
bec4d62e
JW
843 /*
844 * Take the following action on reboot notify depending on value:
845 * 1 == Enter debugger
846 * 0 == [the default] detatch debug client
847 * -1 == Do nothing... and use this until the board resets
848 */
849 switch (kgdbreboot) {
850 case 1:
851 kgdb_breakpoint();
852 case -1:
853 goto done;
854 }
2366e047
JW
855 if (!dbg_kdb_mode)
856 gdbstub_exit(code);
bec4d62e 857done:
2366e047
JW
858 return NOTIFY_DONE;
859}
860
861static struct notifier_block dbg_reboot_notifier = {
862 .notifier_call = dbg_notify_reboot,
863 .next = NULL,
864 .priority = INT_MAX,
865};
866
dc7d5527
JW
867static void kgdb_register_callbacks(void)
868{
869 if (!kgdb_io_module_registered) {
870 kgdb_io_module_registered = 1;
871 kgdb_arch_init();
0b4b3827
JW
872 if (!dbg_is_early)
873 kgdb_arch_late();
f30fed10 874 register_module_notifier(&dbg_module_load_nb);
2366e047 875 register_reboot_notifier(&dbg_reboot_notifier);
4402c153
JW
876 atomic_notifier_chain_register(&panic_notifier_list,
877 &kgdb_panic_event_nb);
dc7d5527 878#ifdef CONFIG_MAGIC_SYSRQ
53197fc4 879 register_sysrq_key('g', &sysrq_dbg_op);
dc7d5527
JW
880#endif
881 if (kgdb_use_con && !kgdb_con_registered) {
882 register_console(&kgdbcons);
883 kgdb_con_registered = 1;
884 }
885 }
886}
887
888static void kgdb_unregister_callbacks(void)
889{
890 /*
891 * When this routine is called KGDB should unregister from the
892 * panic handler and clean up, making sure it is not handling any
893 * break exceptions at the time.
894 */
895 if (kgdb_io_module_registered) {
896 kgdb_io_module_registered = 0;
2366e047 897 unregister_reboot_notifier(&dbg_reboot_notifier);
f30fed10 898 unregister_module_notifier(&dbg_module_load_nb);
4402c153
JW
899 atomic_notifier_chain_unregister(&panic_notifier_list,
900 &kgdb_panic_event_nb);
dc7d5527
JW
901 kgdb_arch_exit();
902#ifdef CONFIG_MAGIC_SYSRQ
53197fc4 903 unregister_sysrq_key('g', &sysrq_dbg_op);
dc7d5527
JW
904#endif
905 if (kgdb_con_registered) {
906 unregister_console(&kgdbcons);
907 kgdb_con_registered = 0;
908 }
909 }
910}
911
1cee5e35
JW
912/*
913 * There are times a tasklet needs to be used vs a compiled in
914 * break point so as to cause an exception outside a kgdb I/O module,
915 * such as is the case with kgdboe, where calling a breakpoint in the
916 * I/O driver itself would be fatal.
917 */
918static void kgdb_tasklet_bpt(unsigned long ing)
919{
920 kgdb_breakpoint();
921 atomic_set(&kgdb_break_tasklet_var, 0);
922}
923
924static DECLARE_TASKLET(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt, 0);
925
926void kgdb_schedule_breakpoint(void)
927{
928 if (atomic_read(&kgdb_break_tasklet_var) ||
929 atomic_read(&kgdb_active) != -1 ||
930 atomic_read(&kgdb_setting_breakpoint))
931 return;
932 atomic_inc(&kgdb_break_tasklet_var);
933 tasklet_schedule(&kgdb_tasklet_breakpoint);
934}
935EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
936
dc7d5527
JW
937static void kgdb_initial_breakpoint(void)
938{
939 kgdb_break_asap = 0;
940
941 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
942 kgdb_breakpoint();
943}
944
945/**
737a460f 946 * kgdb_register_io_module - register KGDB IO module
53197fc4 947 * @new_dbg_io_ops: the io ops vector
dc7d5527
JW
948 *
949 * Register it with the KGDB core.
950 */
53197fc4 951int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
dc7d5527
JW
952{
953 int err;
954
955 spin_lock(&kgdb_registration_lock);
956
53197fc4 957 if (dbg_io_ops) {
dc7d5527
JW
958 spin_unlock(&kgdb_registration_lock);
959
960 printk(KERN_ERR "kgdb: Another I/O driver is already "
961 "registered with KGDB.\n");
962 return -EBUSY;
963 }
964
53197fc4
JW
965 if (new_dbg_io_ops->init) {
966 err = new_dbg_io_ops->init();
dc7d5527
JW
967 if (err) {
968 spin_unlock(&kgdb_registration_lock);
969 return err;
970 }
971 }
972
53197fc4 973 dbg_io_ops = new_dbg_io_ops;
dc7d5527
JW
974
975 spin_unlock(&kgdb_registration_lock);
976
977 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
53197fc4 978 new_dbg_io_ops->name);
dc7d5527
JW
979
980 /* Arm KGDB now. */
981 kgdb_register_callbacks();
982
983 if (kgdb_break_asap)
984 kgdb_initial_breakpoint();
985
986 return 0;
987}
988EXPORT_SYMBOL_GPL(kgdb_register_io_module);
989
990/**
991 * kkgdb_unregister_io_module - unregister KGDB IO module
53197fc4 992 * @old_dbg_io_ops: the io ops vector
dc7d5527
JW
993 *
994 * Unregister it with the KGDB core.
995 */
53197fc4 996void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
dc7d5527
JW
997{
998 BUG_ON(kgdb_connected);
999
1000 /*
1001 * KGDB is no longer able to communicate out, so
1002 * unregister our callbacks and reset state.
1003 */
1004 kgdb_unregister_callbacks();
1005
1006 spin_lock(&kgdb_registration_lock);
1007
53197fc4
JW
1008 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1009 dbg_io_ops = NULL;
dc7d5527
JW
1010
1011 spin_unlock(&kgdb_registration_lock);
1012
1013 printk(KERN_INFO
1014 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
53197fc4 1015 old_dbg_io_ops->name);
dc7d5527
JW
1016}
1017EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1018
dcc78711
JW
1019int dbg_io_get_char(void)
1020{
1021 int ret = dbg_io_ops->read_char();
f5316b4a
JW
1022 if (ret == NO_POLL_CHAR)
1023 return -1;
dcc78711
JW
1024 if (!dbg_kdb_mode)
1025 return ret;
1026 if (ret == 127)
1027 return 8;
1028 return ret;
1029}
1030
dc7d5527
JW
1031/**
1032 * kgdb_breakpoint - generate breakpoint exception
1033 *
1034 * This function will generate a breakpoint exception. It is used at the
1035 * beginning of a program to sync up with a debugger and can be used
1036 * otherwise as a quick means to stop program execution and "break" into
1037 * the debugger.
1038 */
1039void kgdb_breakpoint(void)
1040{
ae6bf53e 1041 atomic_inc(&kgdb_setting_breakpoint);
dc7d5527
JW
1042 wmb(); /* Sync point before breakpoint */
1043 arch_kgdb_breakpoint();
1044 wmb(); /* Sync point after breakpoint */
ae6bf53e 1045 atomic_dec(&kgdb_setting_breakpoint);
dc7d5527
JW
1046}
1047EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1048
1049static int __init opt_kgdb_wait(char *str)
1050{
1051 kgdb_break_asap = 1;
1052
dcc78711 1053 kdb_init(KDB_INIT_EARLY);
dc7d5527
JW
1054 if (kgdb_io_module_registered)
1055 kgdb_initial_breakpoint();
1056
1057 return 0;
1058}
1059
1060early_param("kgdbwait", opt_kgdb_wait);