debug_core.c 24 KB

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