kgdb.c 19 KB

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