kgdb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. */
  4. /*
  5. * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
  6. * Copyright (C) 2000-2001 VERITAS Software Corporation.
  7. * Copyright (C) 2002 Andi Kleen, SuSE Labs
  8. * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
  9. * Copyright (C) 2007 MontaVista Software, Inc.
  10. * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
  11. */
  12. /****************************************************************************
  13. * Contributor: Lake Stevens Instrument Division$
  14. * Written by: Glenn Engel $
  15. * Updated by: Amit Kale<akale@veritas.com>
  16. * Updated by: Tom Rini <trini@kernel.crashing.org>
  17. * Updated by: Jason Wessel <jason.wessel@windriver.com>
  18. * Modified for 386 by Jim Kingdon, Cygnus Support.
  19. * Origianl kgdb, compatibility with 2.1.xx kernel by
  20. * David Grothe <dave@gcom.com>
  21. * Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
  22. * X86_64 changes from Andi Kleen's patch merged by Jim Houston
  23. */
  24. #include <linux/spinlock.h>
  25. #include <linux/kdebug.h>
  26. #include <linux/string.h>
  27. #include <linux/kernel.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/sched.h>
  30. #include <linux/delay.h>
  31. #include <linux/kgdb.h>
  32. #include <linux/smp.h>
  33. #include <linux/nmi.h>
  34. #include <linux/hw_breakpoint.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/memory.h>
  37. #include <asm/text-patching.h>
  38. #include <asm/debugreg.h>
  39. #include <asm/apicdef.h>
  40. #include <asm/apic.h>
  41. #include <asm/nmi.h>
  42. #include <asm/switch_to.h>
  43. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  44. {
  45. #ifdef CONFIG_X86_32
  46. { "ax", 4, offsetof(struct pt_regs, ax) },
  47. { "cx", 4, offsetof(struct pt_regs, cx) },
  48. { "dx", 4, offsetof(struct pt_regs, dx) },
  49. { "bx", 4, offsetof(struct pt_regs, bx) },
  50. { "sp", 4, offsetof(struct pt_regs, sp) },
  51. { "bp", 4, offsetof(struct pt_regs, bp) },
  52. { "si", 4, offsetof(struct pt_regs, si) },
  53. { "di", 4, offsetof(struct pt_regs, di) },
  54. { "ip", 4, offsetof(struct pt_regs, ip) },
  55. { "flags", 4, offsetof(struct pt_regs, flags) },
  56. { "cs", 4, offsetof(struct pt_regs, cs) },
  57. { "ss", 4, offsetof(struct pt_regs, ss) },
  58. { "ds", 4, offsetof(struct pt_regs, ds) },
  59. { "es", 4, offsetof(struct pt_regs, es) },
  60. #else
  61. { "ax", 8, offsetof(struct pt_regs, ax) },
  62. { "bx", 8, offsetof(struct pt_regs, bx) },
  63. { "cx", 8, offsetof(struct pt_regs, cx) },
  64. { "dx", 8, offsetof(struct pt_regs, dx) },
  65. { "si", 8, offsetof(struct pt_regs, si) },
  66. { "di", 8, offsetof(struct pt_regs, di) },
  67. { "bp", 8, offsetof(struct pt_regs, bp) },
  68. { "sp", 8, offsetof(struct pt_regs, sp) },
  69. { "r8", 8, offsetof(struct pt_regs, r8) },
  70. { "r9", 8, offsetof(struct pt_regs, r9) },
  71. { "r10", 8, offsetof(struct pt_regs, r10) },
  72. { "r11", 8, offsetof(struct pt_regs, r11) },
  73. { "r12", 8, offsetof(struct pt_regs, r12) },
  74. { "r13", 8, offsetof(struct pt_regs, r13) },
  75. { "r14", 8, offsetof(struct pt_regs, r14) },
  76. { "r15", 8, offsetof(struct pt_regs, r15) },
  77. { "ip", 8, offsetof(struct pt_regs, ip) },
  78. { "flags", 4, offsetof(struct pt_regs, flags) },
  79. { "cs", 4, offsetof(struct pt_regs, cs) },
  80. { "ss", 4, offsetof(struct pt_regs, ss) },
  81. { "ds", 4, -1 },
  82. { "es", 4, -1 },
  83. #endif
  84. { "fs", 4, -1 },
  85. { "gs", 4, -1 },
  86. };
  87. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  88. {
  89. if (
  90. #ifdef CONFIG_X86_32
  91. regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
  92. #endif
  93. regno == GDB_SP || regno == GDB_ORIG_AX)
  94. return 0;
  95. if (dbg_reg_def[regno].offset != -1)
  96. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  97. dbg_reg_def[regno].size);
  98. return 0;
  99. }
  100. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  101. {
  102. if (regno == GDB_ORIG_AX) {
  103. memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
  104. return "orig_ax";
  105. }
  106. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  107. return NULL;
  108. if (dbg_reg_def[regno].offset != -1)
  109. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  110. dbg_reg_def[regno].size);
  111. #ifdef CONFIG_X86_32
  112. switch (regno) {
  113. case GDB_GS:
  114. case GDB_FS:
  115. *(unsigned long *)mem = 0xFFFF;
  116. break;
  117. }
  118. #endif
  119. return dbg_reg_def[regno].name;
  120. }
  121. /**
  122. * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
  123. * @gdb_regs: A pointer to hold the registers in the order GDB wants.
  124. * @p: The &struct task_struct of the desired process.
  125. *
  126. * Convert the register values of the sleeping process in @p to
  127. * the format that GDB expects.
  128. * This function is called when kgdb does not have access to the
  129. * &struct pt_regs and therefore it should fill the gdb registers
  130. * @gdb_regs with what has been saved in &struct thread_struct
  131. * thread field during switch_to.
  132. */
  133. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  134. {
  135. #ifndef CONFIG_X86_32
  136. u32 *gdb_regs32 = (u32 *)gdb_regs;
  137. #endif
  138. gdb_regs[GDB_AX] = 0;
  139. gdb_regs[GDB_BX] = 0;
  140. gdb_regs[GDB_CX] = 0;
  141. gdb_regs[GDB_DX] = 0;
  142. gdb_regs[GDB_SI] = 0;
  143. gdb_regs[GDB_DI] = 0;
  144. gdb_regs[GDB_BP] = ((struct inactive_task_frame *)p->thread.sp)->bp;
  145. #ifdef CONFIG_X86_32
  146. gdb_regs[GDB_DS] = __KERNEL_DS;
  147. gdb_regs[GDB_ES] = __KERNEL_DS;
  148. gdb_regs[GDB_PS] = 0;
  149. gdb_regs[GDB_CS] = __KERNEL_CS;
  150. gdb_regs[GDB_SS] = __KERNEL_DS;
  151. gdb_regs[GDB_FS] = 0xFFFF;
  152. gdb_regs[GDB_GS] = 0xFFFF;
  153. #else
  154. gdb_regs32[GDB_PS] = 0;
  155. gdb_regs32[GDB_CS] = __KERNEL_CS;
  156. gdb_regs32[GDB_SS] = __KERNEL_DS;
  157. gdb_regs[GDB_R8] = 0;
  158. gdb_regs[GDB_R9] = 0;
  159. gdb_regs[GDB_R10] = 0;
  160. gdb_regs[GDB_R11] = 0;
  161. gdb_regs[GDB_R12] = 0;
  162. gdb_regs[GDB_R13] = 0;
  163. gdb_regs[GDB_R14] = 0;
  164. gdb_regs[GDB_R15] = 0;
  165. #endif
  166. gdb_regs[GDB_PC] = 0;
  167. gdb_regs[GDB_SP] = p->thread.sp;
  168. }
  169. static struct hw_breakpoint {
  170. unsigned enabled;
  171. unsigned long addr;
  172. int len;
  173. int type;
  174. struct perf_event * __percpu *pev;
  175. } breakinfo[HBP_NUM];
  176. static unsigned long early_dr7;
  177. static void kgdb_correct_hw_break(void)
  178. {
  179. int breakno;
  180. for (breakno = 0; breakno < HBP_NUM; breakno++) {
  181. struct perf_event *bp;
  182. struct arch_hw_breakpoint *info;
  183. int val;
  184. int cpu = raw_smp_processor_id();
  185. if (!breakinfo[breakno].enabled)
  186. continue;
  187. if (dbg_is_early) {
  188. set_debugreg(breakinfo[breakno].addr, breakno);
  189. early_dr7 |= encode_dr7(breakno,
  190. breakinfo[breakno].len,
  191. breakinfo[breakno].type);
  192. set_debugreg(early_dr7, 7);
  193. continue;
  194. }
  195. bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
  196. info = counter_arch_bp(bp);
  197. if (bp->attr.disabled != 1)
  198. continue;
  199. bp->attr.bp_addr = breakinfo[breakno].addr;
  200. bp->attr.bp_len = breakinfo[breakno].len;
  201. bp->attr.bp_type = breakinfo[breakno].type;
  202. info->address = breakinfo[breakno].addr;
  203. info->len = breakinfo[breakno].len;
  204. info->type = breakinfo[breakno].type;
  205. val = arch_install_hw_breakpoint(bp);
  206. if (!val)
  207. bp->attr.disabled = 0;
  208. }
  209. if (!dbg_is_early)
  210. hw_breakpoint_restore();
  211. }
  212. static int hw_break_reserve_slot(int breakno)
  213. {
  214. int cpu;
  215. int cnt = 0;
  216. struct perf_event **pevent;
  217. if (dbg_is_early)
  218. return 0;
  219. for_each_online_cpu(cpu) {
  220. cnt++;
  221. pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
  222. if (dbg_reserve_bp_slot(*pevent))
  223. goto fail;
  224. }
  225. return 0;
  226. fail:
  227. for_each_online_cpu(cpu) {
  228. cnt--;
  229. if (!cnt)
  230. break;
  231. pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
  232. dbg_release_bp_slot(*pevent);
  233. }
  234. return -1;
  235. }
  236. static int hw_break_release_slot(int breakno)
  237. {
  238. struct perf_event **pevent;
  239. int cpu;
  240. if (dbg_is_early)
  241. return 0;
  242. for_each_online_cpu(cpu) {
  243. pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
  244. if (dbg_release_bp_slot(*pevent))
  245. /*
  246. * The debugger is responsible for handing the retry on
  247. * remove failure.
  248. */
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. static int
  254. kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  255. {
  256. int i;
  257. for (i = 0; i < HBP_NUM; i++)
  258. if (breakinfo[i].addr == addr && breakinfo[i].enabled)
  259. break;
  260. if (i == HBP_NUM)
  261. return -1;
  262. if (hw_break_release_slot(i)) {
  263. printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
  264. return -1;
  265. }
  266. breakinfo[i].enabled = 0;
  267. return 0;
  268. }
  269. static void kgdb_remove_all_hw_break(void)
  270. {
  271. int i;
  272. int cpu = raw_smp_processor_id();
  273. struct perf_event *bp;
  274. for (i = 0; i < HBP_NUM; i++) {
  275. if (!breakinfo[i].enabled)
  276. continue;
  277. bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
  278. if (!bp->attr.disabled) {
  279. arch_uninstall_hw_breakpoint(bp);
  280. bp->attr.disabled = 1;
  281. continue;
  282. }
  283. if (dbg_is_early)
  284. early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
  285. breakinfo[i].type);
  286. else if (hw_break_release_slot(i))
  287. printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
  288. breakinfo[i].addr);
  289. breakinfo[i].enabled = 0;
  290. }
  291. }
  292. static int
  293. kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  294. {
  295. int i;
  296. for (i = 0; i < HBP_NUM; i++)
  297. if (!breakinfo[i].enabled)
  298. break;
  299. if (i == HBP_NUM)
  300. return -1;
  301. switch (bptype) {
  302. case BP_HARDWARE_BREAKPOINT:
  303. len = 1;
  304. breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
  305. break;
  306. case BP_WRITE_WATCHPOINT:
  307. breakinfo[i].type = X86_BREAKPOINT_WRITE;
  308. break;
  309. case BP_ACCESS_WATCHPOINT:
  310. breakinfo[i].type = X86_BREAKPOINT_RW;
  311. break;
  312. default:
  313. return -1;
  314. }
  315. switch (len) {
  316. case 1:
  317. breakinfo[i].len = X86_BREAKPOINT_LEN_1;
  318. break;
  319. case 2:
  320. breakinfo[i].len = X86_BREAKPOINT_LEN_2;
  321. break;
  322. case 4:
  323. breakinfo[i].len = X86_BREAKPOINT_LEN_4;
  324. break;
  325. #ifdef CONFIG_X86_64
  326. case 8:
  327. breakinfo[i].len = X86_BREAKPOINT_LEN_8;
  328. break;
  329. #endif
  330. default:
  331. return -1;
  332. }
  333. breakinfo[i].addr = addr;
  334. if (hw_break_reserve_slot(i)) {
  335. breakinfo[i].addr = 0;
  336. return -1;
  337. }
  338. breakinfo[i].enabled = 1;
  339. return 0;
  340. }
  341. /**
  342. * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
  343. * @regs: Current &struct pt_regs.
  344. *
  345. * This function will be called if the particular architecture must
  346. * disable hardware debugging while it is processing gdb packets or
  347. * handling exception.
  348. */
  349. static void kgdb_disable_hw_debug(struct pt_regs *regs)
  350. {
  351. int i;
  352. int cpu = raw_smp_processor_id();
  353. struct perf_event *bp;
  354. /* Disable hardware debugging while we are in kgdb: */
  355. set_debugreg(0UL, 7);
  356. for (i = 0; i < HBP_NUM; i++) {
  357. if (!breakinfo[i].enabled)
  358. continue;
  359. if (dbg_is_early) {
  360. early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
  361. breakinfo[i].type);
  362. continue;
  363. }
  364. bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
  365. if (bp->attr.disabled == 1)
  366. continue;
  367. arch_uninstall_hw_breakpoint(bp);
  368. bp->attr.disabled = 1;
  369. }
  370. }
  371. #ifdef CONFIG_SMP
  372. /**
  373. * kgdb_roundup_cpus - Get other CPUs into a holding pattern
  374. *
  375. * On SMP systems, we need to get the attention of the other CPUs
  376. * and get them be in a known state. This should do what is needed
  377. * to get the other CPUs to call kgdb_wait(). Note that on some arches,
  378. * the NMI approach is not used for rounding up all the CPUs. For example,
  379. * in case of MIPS, smp_call_function() is used to roundup CPUs.
  380. *
  381. * On non-SMP systems, this is not called.
  382. */
  383. void kgdb_roundup_cpus(void)
  384. {
  385. apic_send_IPI_allbutself(NMI_VECTOR);
  386. }
  387. #endif
  388. /**
  389. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  390. * @e_vector: The error vector of the exception that happened.
  391. * @signo: The signal number of the exception that happened.
  392. * @err_code: The error code of the exception that happened.
  393. * @remcomInBuffer: The buffer of the packet we have read.
  394. * @remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into.
  395. * @linux_regs: The &struct pt_regs of the current process.
  396. *
  397. * This function MUST handle the 'c' and 's' command packets,
  398. * as well packets to set / remove a hardware breakpoint, if used.
  399. * If there are additional packets which the hardware needs to handle,
  400. * they are handled here. The code should return -1 if it wants to
  401. * process more packets, and a %0 or %1 if it wants to exit from the
  402. * kgdb callback.
  403. */
  404. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  405. char *remcomInBuffer, char *remcomOutBuffer,
  406. struct pt_regs *linux_regs)
  407. {
  408. unsigned long addr;
  409. char *ptr;
  410. switch (remcomInBuffer[0]) {
  411. case 'c':
  412. case 's':
  413. /* try to read optional parameter, pc unchanged if no parm */
  414. ptr = &remcomInBuffer[1];
  415. if (kgdb_hex2long(&ptr, &addr))
  416. linux_regs->ip = addr;
  417. fallthrough;
  418. case 'D':
  419. case 'k':
  420. /* clear the trace bit */
  421. linux_regs->flags &= ~X86_EFLAGS_TF;
  422. atomic_set(&kgdb_cpu_doing_single_step, -1);
  423. /* set the trace bit if we're stepping */
  424. if (remcomInBuffer[0] == 's') {
  425. linux_regs->flags |= X86_EFLAGS_TF;
  426. atomic_set(&kgdb_cpu_doing_single_step,
  427. raw_smp_processor_id());
  428. }
  429. return 0;
  430. }
  431. /* this means that we do not want to exit from the handler: */
  432. return -1;
  433. }
  434. static inline int
  435. single_step_cont(struct pt_regs *regs, struct die_args *args)
  436. {
  437. /*
  438. * Single step exception from kernel space to user space so
  439. * eat the exception and continue the process:
  440. */
  441. printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
  442. "resuming...\n");
  443. kgdb_arch_handle_exception(args->trapnr, args->signr,
  444. args->err, "c", "", regs);
  445. /*
  446. * Reset the BS bit in dr6 (pointed by args->err) to
  447. * denote completion of processing
  448. */
  449. (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
  450. return NOTIFY_STOP;
  451. }
  452. static DECLARE_BITMAP(was_in_debug_nmi, NR_CPUS);
  453. static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  454. {
  455. int cpu;
  456. switch (cmd) {
  457. case NMI_LOCAL:
  458. if (atomic_read(&kgdb_active) != -1) {
  459. /* KGDB CPU roundup */
  460. cpu = raw_smp_processor_id();
  461. kgdb_nmicallback(cpu, regs);
  462. set_bit(cpu, was_in_debug_nmi);
  463. touch_nmi_watchdog();
  464. return NMI_HANDLED;
  465. }
  466. break;
  467. case NMI_UNKNOWN:
  468. cpu = raw_smp_processor_id();
  469. if (__test_and_clear_bit(cpu, was_in_debug_nmi))
  470. return NMI_HANDLED;
  471. break;
  472. default:
  473. /* do nothing */
  474. break;
  475. }
  476. return NMI_DONE;
  477. }
  478. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  479. {
  480. struct pt_regs *regs = args->regs;
  481. switch (cmd) {
  482. case DIE_DEBUG:
  483. if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
  484. if (user_mode(regs))
  485. return single_step_cont(regs, args);
  486. break;
  487. } else if (test_thread_flag(TIF_SINGLESTEP))
  488. /* This means a user thread is single stepping
  489. * a system call which should be ignored
  490. */
  491. return NOTIFY_DONE;
  492. fallthrough;
  493. default:
  494. if (user_mode(regs))
  495. return NOTIFY_DONE;
  496. }
  497. if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
  498. return NOTIFY_DONE;
  499. /* Must touch watchdog before return to normal operation */
  500. touch_nmi_watchdog();
  501. return NOTIFY_STOP;
  502. }
  503. int kgdb_ll_trap(int cmd, const char *str,
  504. struct pt_regs *regs, long err, int trap, int sig)
  505. {
  506. struct die_args args = {
  507. .regs = regs,
  508. .str = str,
  509. .err = err,
  510. .trapnr = trap,
  511. .signr = sig,
  512. };
  513. if (!kgdb_io_module_registered)
  514. return NOTIFY_DONE;
  515. return __kgdb_notify(&args, cmd);
  516. }
  517. static int
  518. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  519. {
  520. unsigned long flags;
  521. int ret;
  522. local_irq_save(flags);
  523. ret = __kgdb_notify(ptr, cmd);
  524. local_irq_restore(flags);
  525. return ret;
  526. }
  527. static struct notifier_block kgdb_notifier = {
  528. .notifier_call = kgdb_notify,
  529. };
  530. /**
  531. * kgdb_arch_init - Perform any architecture specific initialization.
  532. *
  533. * This function will handle the initialization of any architecture
  534. * specific callbacks.
  535. */
  536. int kgdb_arch_init(void)
  537. {
  538. int retval;
  539. retval = register_die_notifier(&kgdb_notifier);
  540. if (retval)
  541. goto out;
  542. retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
  543. 0, "kgdb");
  544. if (retval)
  545. goto out1;
  546. retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
  547. 0, "kgdb");
  548. if (retval)
  549. goto out2;
  550. return retval;
  551. out2:
  552. unregister_nmi_handler(NMI_LOCAL, "kgdb");
  553. out1:
  554. unregister_die_notifier(&kgdb_notifier);
  555. out:
  556. return retval;
  557. }
  558. static void kgdb_hw_overflow_handler(struct perf_event *event,
  559. struct perf_sample_data *data, struct pt_regs *regs)
  560. {
  561. struct task_struct *tsk = current;
  562. int i;
  563. for (i = 0; i < 4; i++) {
  564. if (breakinfo[i].enabled)
  565. tsk->thread.virtual_dr6 |= (DR_TRAP0 << i);
  566. }
  567. }
  568. void kgdb_arch_late(void)
  569. {
  570. int i, cpu;
  571. struct perf_event_attr attr;
  572. struct perf_event **pevent;
  573. /*
  574. * Pre-allocate the hw breakpoint structions in the non-atomic
  575. * portion of kgdb because this operation requires mutexs to
  576. * complete.
  577. */
  578. hw_breakpoint_init(&attr);
  579. attr.bp_addr = (unsigned long)kgdb_arch_init;
  580. attr.bp_len = HW_BREAKPOINT_LEN_1;
  581. attr.bp_type = HW_BREAKPOINT_W;
  582. attr.disabled = 1;
  583. for (i = 0; i < HBP_NUM; i++) {
  584. if (breakinfo[i].pev)
  585. continue;
  586. breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
  587. if (IS_ERR((void * __force)breakinfo[i].pev)) {
  588. printk(KERN_ERR "kgdb: Could not allocate hw"
  589. "breakpoints\nDisabling the kernel debugger\n");
  590. breakinfo[i].pev = NULL;
  591. kgdb_arch_exit();
  592. return;
  593. }
  594. for_each_online_cpu(cpu) {
  595. pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
  596. pevent[0]->hw.sample_period = 1;
  597. pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
  598. if (pevent[0]->destroy != NULL) {
  599. pevent[0]->destroy = NULL;
  600. release_bp_slot(*pevent);
  601. }
  602. }
  603. }
  604. }
  605. /**
  606. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  607. *
  608. * This function will handle the uninitalization of any architecture
  609. * specific callbacks, for dynamic registration and unregistration.
  610. */
  611. void kgdb_arch_exit(void)
  612. {
  613. int i;
  614. for (i = 0; i < 4; i++) {
  615. if (breakinfo[i].pev) {
  616. unregister_wide_hw_breakpoint(breakinfo[i].pev);
  617. breakinfo[i].pev = NULL;
  618. }
  619. }
  620. unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
  621. unregister_nmi_handler(NMI_LOCAL, "kgdb");
  622. unregister_die_notifier(&kgdb_notifier);
  623. }
  624. /**
  625. *
  626. * kgdb_skipexception - Bail out of KGDB when we've been triggered.
  627. * @exception: Exception vector number
  628. * @regs: Current &struct pt_regs.
  629. *
  630. * On some architectures we need to skip a breakpoint exception when
  631. * it occurs after a breakpoint has been removed.
  632. *
  633. * Skip an int3 exception when it occurs after a breakpoint has been
  634. * removed. Backtrack eip by 1 since the int3 would have caused it to
  635. * increment by 1.
  636. */
  637. int kgdb_skipexception(int exception, struct pt_regs *regs)
  638. {
  639. if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
  640. regs->ip -= 1;
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
  646. {
  647. if (exception == 3)
  648. return instruction_pointer(regs) - 1;
  649. return instruction_pointer(regs);
  650. }
  651. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  652. {
  653. regs->ip = ip;
  654. }
  655. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  656. {
  657. int err;
  658. bpt->type = BP_BREAKPOINT;
  659. err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
  660. BREAK_INSTR_SIZE);
  661. if (err)
  662. return err;
  663. err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
  664. arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
  665. if (!err)
  666. return err;
  667. /*
  668. * It is safe to call text_poke_kgdb() because normal kernel execution
  669. * is stopped on all cores, so long as the text_mutex is not locked.
  670. */
  671. if (mutex_is_locked(&text_mutex))
  672. return -EBUSY;
  673. text_poke_kgdb((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
  674. BREAK_INSTR_SIZE);
  675. bpt->type = BP_POKE_BREAKPOINT;
  676. return 0;
  677. }
  678. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  679. {
  680. if (bpt->type != BP_POKE_BREAKPOINT)
  681. goto knl_write;
  682. /*
  683. * It is safe to call text_poke_kgdb() because normal kernel execution
  684. * is stopped on all cores, so long as the text_mutex is not locked.
  685. */
  686. if (mutex_is_locked(&text_mutex))
  687. goto knl_write;
  688. text_poke_kgdb((void *)bpt->bpt_addr, bpt->saved_instr,
  689. BREAK_INSTR_SIZE);
  690. return 0;
  691. knl_write:
  692. return copy_to_kernel_nofault((char *)bpt->bpt_addr,
  693. (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
  694. }
  695. const struct kgdb_arch arch_kgdb_ops = {
  696. /* Breakpoint instruction: */
  697. .gdb_bpt_instr = { 0xcc },
  698. .flags = KGDB_HW_BREAKPOINT,
  699. .set_hw_breakpoint = kgdb_set_hw_break,
  700. .remove_hw_breakpoint = kgdb_remove_hw_break,
  701. .disable_hw_break = kgdb_disable_hw_debug,
  702. .remove_all_hw_break = kgdb_remove_all_hw_break,
  703. .correct_hw_break = kgdb_correct_hw_break,
  704. };