kgdb.c 18 KB

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