debug_core.c 23 KB

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