kgdb.c 19 KB

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