debug_core.c 25 KB

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