debug_core.c 23 KB

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