debug_core.c 23 KB

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