debug_core.c 25 KB

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