debug_core.c 21 KB

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