debug_core.c 23 KB

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