debug_core.c 25 KB

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