kgdb.c 19 KB

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