debug_core.c 29 KB

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