kgdb.c 16 KB

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