kgdb.c 15 KB

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