debug_core.c 25 KB

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