debug_core.c 23 KB

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