debug_core.c 26 KB

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