kgdb.c 15 KB

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