debug_core.c 24 KB

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