kgdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <asm/apicdef.h>
  45. #include <asm/system.h>
  46. #ifdef CONFIG_X86_32
  47. # include <mach_ipi.h>
  48. #else
  49. # include <asm/mach_apic.h>
  50. #endif
  51. /*
  52. * Put the error code here just in case the user cares:
  53. */
  54. static int gdb_x86errcode;
  55. /*
  56. * Likewise, the vector number here (since GDB only gets the signal
  57. * number through the usual means, and that's not very specific):
  58. */
  59. static int gdb_x86vector = -1;
  60. /**
  61. * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
  62. * @gdb_regs: A pointer to hold the registers in the order GDB wants.
  63. * @regs: The &struct pt_regs of the current process.
  64. *
  65. * Convert the pt_regs in @regs into the format for registers that
  66. * GDB expects, stored in @gdb_regs.
  67. */
  68. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  69. {
  70. gdb_regs[GDB_AX] = regs->ax;
  71. gdb_regs[GDB_BX] = regs->bx;
  72. gdb_regs[GDB_CX] = regs->cx;
  73. gdb_regs[GDB_DX] = regs->dx;
  74. gdb_regs[GDB_SI] = regs->si;
  75. gdb_regs[GDB_DI] = regs->di;
  76. gdb_regs[GDB_BP] = regs->bp;
  77. gdb_regs[GDB_PS] = regs->flags;
  78. gdb_regs[GDB_PC] = regs->ip;
  79. #ifdef CONFIG_X86_32
  80. gdb_regs[GDB_DS] = regs->ds;
  81. gdb_regs[GDB_ES] = regs->es;
  82. gdb_regs[GDB_CS] = regs->cs;
  83. gdb_regs[GDB_SS] = __KERNEL_DS;
  84. gdb_regs[GDB_FS] = 0xFFFF;
  85. gdb_regs[GDB_GS] = 0xFFFF;
  86. #else
  87. gdb_regs[GDB_R8] = regs->r8;
  88. gdb_regs[GDB_R9] = regs->r9;
  89. gdb_regs[GDB_R10] = regs->r10;
  90. gdb_regs[GDB_R11] = regs->r11;
  91. gdb_regs[GDB_R12] = regs->r12;
  92. gdb_regs[GDB_R13] = regs->r13;
  93. gdb_regs[GDB_R14] = regs->r14;
  94. gdb_regs[GDB_R15] = regs->r15;
  95. #endif
  96. gdb_regs[GDB_SP] = regs->sp;
  97. }
  98. /**
  99. * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
  100. * @gdb_regs: A pointer to hold the registers in the order GDB wants.
  101. * @p: The &struct task_struct of the desired process.
  102. *
  103. * Convert the register values of the sleeping process in @p to
  104. * the format that GDB expects.
  105. * This function is called when kgdb does not have access to the
  106. * &struct pt_regs and therefore it should fill the gdb registers
  107. * @gdb_regs with what has been saved in &struct thread_struct
  108. * thread field during switch_to.
  109. */
  110. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  111. {
  112. gdb_regs[GDB_AX] = 0;
  113. gdb_regs[GDB_BX] = 0;
  114. gdb_regs[GDB_CX] = 0;
  115. gdb_regs[GDB_DX] = 0;
  116. gdb_regs[GDB_SI] = 0;
  117. gdb_regs[GDB_DI] = 0;
  118. gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
  119. #ifdef CONFIG_X86_32
  120. gdb_regs[GDB_DS] = __KERNEL_DS;
  121. gdb_regs[GDB_ES] = __KERNEL_DS;
  122. gdb_regs[GDB_PS] = 0;
  123. gdb_regs[GDB_CS] = __KERNEL_CS;
  124. gdb_regs[GDB_PC] = p->thread.ip;
  125. gdb_regs[GDB_SS] = __KERNEL_DS;
  126. gdb_regs[GDB_FS] = 0xFFFF;
  127. gdb_regs[GDB_GS] = 0xFFFF;
  128. #else
  129. gdb_regs[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
  130. gdb_regs[GDB_PC] = 0;
  131. gdb_regs[GDB_R8] = 0;
  132. gdb_regs[GDB_R9] = 0;
  133. gdb_regs[GDB_R10] = 0;
  134. gdb_regs[GDB_R11] = 0;
  135. gdb_regs[GDB_R12] = 0;
  136. gdb_regs[GDB_R13] = 0;
  137. gdb_regs[GDB_R14] = 0;
  138. gdb_regs[GDB_R15] = 0;
  139. #endif
  140. gdb_regs[GDB_SP] = p->thread.sp;
  141. }
  142. /**
  143. * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
  144. * @gdb_regs: A pointer to hold the registers we've received from GDB.
  145. * @regs: A pointer to a &struct pt_regs to hold these values in.
  146. *
  147. * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
  148. * in @regs.
  149. */
  150. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  151. {
  152. regs->ax = gdb_regs[GDB_AX];
  153. regs->bx = gdb_regs[GDB_BX];
  154. regs->cx = gdb_regs[GDB_CX];
  155. regs->dx = gdb_regs[GDB_DX];
  156. regs->si = gdb_regs[GDB_SI];
  157. regs->di = gdb_regs[GDB_DI];
  158. regs->bp = gdb_regs[GDB_BP];
  159. regs->flags = gdb_regs[GDB_PS];
  160. regs->ip = gdb_regs[GDB_PC];
  161. #ifdef CONFIG_X86_32
  162. regs->ds = gdb_regs[GDB_DS];
  163. regs->es = gdb_regs[GDB_ES];
  164. regs->cs = gdb_regs[GDB_CS];
  165. #else
  166. regs->r8 = gdb_regs[GDB_R8];
  167. regs->r9 = gdb_regs[GDB_R9];
  168. regs->r10 = gdb_regs[GDB_R10];
  169. regs->r11 = gdb_regs[GDB_R11];
  170. regs->r12 = gdb_regs[GDB_R12];
  171. regs->r13 = gdb_regs[GDB_R13];
  172. regs->r14 = gdb_regs[GDB_R14];
  173. regs->r15 = gdb_regs[GDB_R15];
  174. #endif
  175. }
  176. static struct hw_breakpoint {
  177. unsigned enabled;
  178. unsigned type;
  179. unsigned len;
  180. unsigned long addr;
  181. } breakinfo[4];
  182. static void kgdb_correct_hw_break(void)
  183. {
  184. unsigned long dr7;
  185. int correctit = 0;
  186. int breakbit;
  187. int breakno;
  188. get_debugreg(dr7, 7);
  189. for (breakno = 0; breakno < 4; breakno++) {
  190. breakbit = 2 << (breakno << 1);
  191. if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
  192. correctit = 1;
  193. dr7 |= breakbit;
  194. dr7 &= ~(0xf0000 << (breakno << 2));
  195. dr7 |= ((breakinfo[breakno].len << 2) |
  196. breakinfo[breakno].type) <<
  197. ((breakno << 2) + 16);
  198. if (breakno >= 0 && breakno <= 3)
  199. set_debugreg(breakinfo[breakno].addr, breakno);
  200. } else {
  201. if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
  202. correctit = 1;
  203. dr7 &= ~breakbit;
  204. dr7 &= ~(0xf0000 << (breakno << 2));
  205. }
  206. }
  207. }
  208. if (correctit)
  209. set_debugreg(dr7, 7);
  210. }
  211. static int
  212. kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  213. {
  214. int i;
  215. for (i = 0; i < 4; i++)
  216. if (breakinfo[i].addr == addr && breakinfo[i].enabled)
  217. break;
  218. if (i == 4)
  219. return -1;
  220. breakinfo[i].enabled = 0;
  221. return 0;
  222. }
  223. static void kgdb_remove_all_hw_break(void)
  224. {
  225. int i;
  226. for (i = 0; i < 4; i++)
  227. memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
  228. }
  229. static int
  230. kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  231. {
  232. unsigned type;
  233. int i;
  234. for (i = 0; i < 4; i++)
  235. if (!breakinfo[i].enabled)
  236. break;
  237. if (i == 4)
  238. return -1;
  239. switch (bptype) {
  240. case BP_HARDWARE_BREAKPOINT:
  241. type = 0;
  242. len = 1;
  243. break;
  244. case BP_WRITE_WATCHPOINT:
  245. type = 1;
  246. break;
  247. case BP_ACCESS_WATCHPOINT:
  248. type = 3;
  249. break;
  250. default:
  251. return -1;
  252. }
  253. if (len == 1 || len == 2 || len == 4)
  254. breakinfo[i].len = len - 1;
  255. else
  256. return -1;
  257. breakinfo[i].enabled = 1;
  258. breakinfo[i].addr = addr;
  259. breakinfo[i].type = type;
  260. return 0;
  261. }
  262. /**
  263. * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
  264. * @regs: Current &struct pt_regs.
  265. *
  266. * This function will be called if the particular architecture must
  267. * disable hardware debugging while it is processing gdb packets or
  268. * handling exception.
  269. */
  270. void kgdb_disable_hw_debug(struct pt_regs *regs)
  271. {
  272. /* Disable hardware debugging while we are in kgdb: */
  273. set_debugreg(0UL, 7);
  274. }
  275. /**
  276. * kgdb_post_primary_code - Save error vector/code numbers.
  277. * @regs: Original pt_regs.
  278. * @e_vector: Original error vector.
  279. * @err_code: Original error code.
  280. *
  281. * This is needed on architectures which support SMP and KGDB.
  282. * This function is called after all the slave cpus have been put
  283. * to a know spin state and the primary CPU has control over KGDB.
  284. */
  285. void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
  286. {
  287. /* primary processor is completely in the debugger */
  288. gdb_x86vector = e_vector;
  289. gdb_x86errcode = err_code;
  290. }
  291. #ifdef CONFIG_SMP
  292. /**
  293. * kgdb_roundup_cpus - Get other CPUs into a holding pattern
  294. * @flags: Current IRQ state
  295. *
  296. * On SMP systems, we need to get the attention of the other CPUs
  297. * and get them be in a known state. This should do what is needed
  298. * to get the other CPUs to call kgdb_wait(). Note that on some arches,
  299. * the NMI approach is not used for rounding up all the CPUs. For example,
  300. * in case of MIPS, smp_call_function() is used to roundup CPUs. In
  301. * this case, we have to make sure that interrupts are enabled before
  302. * calling smp_call_function(). The argument to this function is
  303. * the flags that will be used when restoring the interrupts. There is
  304. * local_irq_save() call before kgdb_roundup_cpus().
  305. *
  306. * On non-SMP systems, this is not called.
  307. */
  308. void kgdb_roundup_cpus(unsigned long flags)
  309. {
  310. send_IPI_allbutself(APIC_DM_NMI);
  311. }
  312. #endif
  313. /**
  314. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  315. * @vector: The error vector of the exception that happened.
  316. * @signo: The signal number of the exception that happened.
  317. * @err_code: The error code of the exception that happened.
  318. * @remcom_in_buffer: The buffer of the packet we have read.
  319. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  320. * @regs: The &struct pt_regs of the current process.
  321. *
  322. * This function MUST handle the 'c' and 's' command packets,
  323. * as well packets to set / remove a hardware breakpoint, if used.
  324. * If there are additional packets which the hardware needs to handle,
  325. * they are handled here. The code should return -1 if it wants to
  326. * process more packets, and a %0 or %1 if it wants to exit from the
  327. * kgdb callback.
  328. */
  329. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  330. char *remcomInBuffer, char *remcomOutBuffer,
  331. struct pt_regs *linux_regs)
  332. {
  333. unsigned long addr;
  334. unsigned long dr6;
  335. char *ptr;
  336. int newPC;
  337. switch (remcomInBuffer[0]) {
  338. case 'c':
  339. case 's':
  340. /* try to read optional parameter, pc unchanged if no parm */
  341. ptr = &remcomInBuffer[1];
  342. if (kgdb_hex2long(&ptr, &addr))
  343. linux_regs->ip = addr;
  344. case 'D':
  345. case 'k':
  346. newPC = linux_regs->ip;
  347. /* clear the trace bit */
  348. linux_regs->flags &= ~X86_EFLAGS_TF;
  349. atomic_set(&kgdb_cpu_doing_single_step, -1);
  350. /* set the trace bit if we're stepping */
  351. if (remcomInBuffer[0] == 's') {
  352. linux_regs->flags |= X86_EFLAGS_TF;
  353. kgdb_single_step = 1;
  354. if (kgdb_contthread) {
  355. atomic_set(&kgdb_cpu_doing_single_step,
  356. raw_smp_processor_id());
  357. }
  358. }
  359. get_debugreg(dr6, 6);
  360. if (!(dr6 & 0x4000)) {
  361. int breakno;
  362. for (breakno = 0; breakno < 4; breakno++) {
  363. if (dr6 & (1 << breakno) &&
  364. breakinfo[breakno].type == 0) {
  365. /* Set restore flag: */
  366. linux_regs->flags |= X86_EFLAGS_RF;
  367. break;
  368. }
  369. }
  370. }
  371. set_debugreg(0UL, 6);
  372. kgdb_correct_hw_break();
  373. return 0;
  374. }
  375. /* this means that we do not want to exit from the handler: */
  376. return -1;
  377. }
  378. static inline int
  379. single_step_cont(struct pt_regs *regs, struct die_args *args)
  380. {
  381. /*
  382. * Single step exception from kernel space to user space so
  383. * eat the exception and continue the process:
  384. */
  385. printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
  386. "resuming...\n");
  387. kgdb_arch_handle_exception(args->trapnr, args->signr,
  388. args->err, "c", "", regs);
  389. return NOTIFY_STOP;
  390. }
  391. static int was_in_debug_nmi[NR_CPUS];
  392. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  393. {
  394. struct pt_regs *regs = args->regs;
  395. switch (cmd) {
  396. case DIE_NMI:
  397. if (atomic_read(&kgdb_active) != -1) {
  398. /* KGDB CPU roundup */
  399. kgdb_nmicallback(raw_smp_processor_id(), regs);
  400. was_in_debug_nmi[raw_smp_processor_id()] = 1;
  401. touch_nmi_watchdog();
  402. return NOTIFY_STOP;
  403. }
  404. return NOTIFY_DONE;
  405. case DIE_NMI_IPI:
  406. if (atomic_read(&kgdb_active) != -1) {
  407. /* KGDB CPU roundup */
  408. kgdb_nmicallback(raw_smp_processor_id(), regs);
  409. was_in_debug_nmi[raw_smp_processor_id()] = 1;
  410. touch_nmi_watchdog();
  411. }
  412. return NOTIFY_DONE;
  413. case DIE_NMIUNKNOWN:
  414. if (was_in_debug_nmi[raw_smp_processor_id()]) {
  415. was_in_debug_nmi[raw_smp_processor_id()] = 0;
  416. return NOTIFY_STOP;
  417. }
  418. return NOTIFY_DONE;
  419. case DIE_NMIWATCHDOG:
  420. if (atomic_read(&kgdb_active) != -1) {
  421. /* KGDB CPU roundup: */
  422. kgdb_nmicallback(raw_smp_processor_id(), regs);
  423. return NOTIFY_STOP;
  424. }
  425. /* Enter debugger: */
  426. break;
  427. case DIE_DEBUG:
  428. if (atomic_read(&kgdb_cpu_doing_single_step) ==
  429. raw_smp_processor_id() &&
  430. user_mode(regs))
  431. return single_step_cont(regs, args);
  432. /* fall through */
  433. default:
  434. if (user_mode(regs))
  435. return NOTIFY_DONE;
  436. }
  437. if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
  438. return NOTIFY_DONE;
  439. /* Must touch watchdog before return to normal operation */
  440. touch_nmi_watchdog();
  441. return NOTIFY_STOP;
  442. }
  443. static int
  444. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  445. {
  446. unsigned long flags;
  447. int ret;
  448. local_irq_save(flags);
  449. ret = __kgdb_notify(ptr, cmd);
  450. local_irq_restore(flags);
  451. return ret;
  452. }
  453. static struct notifier_block kgdb_notifier = {
  454. .notifier_call = kgdb_notify,
  455. /*
  456. * Lowest-prio notifier priority, we want to be notified last:
  457. */
  458. .priority = -INT_MAX,
  459. };
  460. /**
  461. * kgdb_arch_init - Perform any architecture specific initalization.
  462. *
  463. * This function will handle the initalization of any architecture
  464. * specific callbacks.
  465. */
  466. int kgdb_arch_init(void)
  467. {
  468. return register_die_notifier(&kgdb_notifier);
  469. }
  470. /**
  471. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  472. *
  473. * This function will handle the uninitalization of any architecture
  474. * specific callbacks, for dynamic registration and unregistration.
  475. */
  476. void kgdb_arch_exit(void)
  477. {
  478. unregister_die_notifier(&kgdb_notifier);
  479. }
  480. /**
  481. *
  482. * kgdb_skipexception - Bail out of KGDB when we've been triggered.
  483. * @exception: Exception vector number
  484. * @regs: Current &struct pt_regs.
  485. *
  486. * On some architectures we need to skip a breakpoint exception when
  487. * it occurs after a breakpoint has been removed.
  488. *
  489. * Skip an int3 exception when it occurs after a breakpoint has been
  490. * removed. Backtrack eip by 1 since the int3 would have caused it to
  491. * increment by 1.
  492. */
  493. int kgdb_skipexception(int exception, struct pt_regs *regs)
  494. {
  495. if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
  496. regs->ip -= 1;
  497. return 1;
  498. }
  499. return 0;
  500. }
  501. unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
  502. {
  503. if (exception == 3)
  504. return instruction_pointer(regs) - 1;
  505. return instruction_pointer(regs);
  506. }
  507. struct kgdb_arch arch_kgdb_ops = {
  508. /* Breakpoint instruction: */
  509. .gdb_bpt_instr = { 0xcc },
  510. .flags = KGDB_HW_BREAKPOINT,
  511. .set_hw_breakpoint = kgdb_set_hw_break,
  512. .remove_hw_breakpoint = kgdb_remove_hw_break,
  513. .remove_all_hw_break = kgdb_remove_all_hw_break,
  514. .correct_hw_break = kgdb_correct_hw_break,
  515. };