kgdb.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Originally written by Glenn Engel, Lake Stevens Instrument Division
  3. *
  4. * Contributed by HP Systems
  5. *
  6. * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
  7. * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
  8. *
  9. * Copyright (C) 1995 Andreas Busse
  10. *
  11. * Copyright (C) 2003 MontaVista Software Inc.
  12. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  13. *
  14. * Copyright (C) 2004-2005 MontaVista Software Inc.
  15. * Author: Manish Lachwani, mlachwani@mvista.com or manish@koffee-break.com
  16. *
  17. * Copyright (C) 2007-2008 Wind River Systems, Inc.
  18. * Author/Maintainer: Jason Wessel, jason.wessel@windriver.com
  19. *
  20. * This file is licensed under the terms of the GNU General Public License
  21. * version 2. This program is licensed "as is" without any warranty of any
  22. * kind, whether express or implied.
  23. */
  24. #include <linux/ptrace.h> /* for linux pt_regs struct */
  25. #include <linux/kgdb.h>
  26. #include <linux/kdebug.h>
  27. #include <linux/sched.h>
  28. #include <asm/inst.h>
  29. #include <asm/fpu.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/processor.h>
  32. #include <asm/sigcontext.h>
  33. static struct hard_trap_info {
  34. unsigned char tt; /* Trap type code for MIPS R3xxx and R4xxx */
  35. unsigned char signo; /* Signal that we map this trap into */
  36. } hard_trap_info[] = {
  37. { 6, SIGBUS }, /* instruction bus error */
  38. { 7, SIGBUS }, /* data bus error */
  39. { 9, SIGTRAP }, /* break */
  40. /* { 11, SIGILL }, */ /* CPU unusable */
  41. { 12, SIGFPE }, /* overflow */
  42. { 13, SIGTRAP }, /* trap */
  43. { 14, SIGSEGV }, /* virtual instruction cache coherency */
  44. { 15, SIGFPE }, /* floating point exception */
  45. { 23, SIGSEGV }, /* watch */
  46. { 31, SIGSEGV }, /* virtual data cache coherency */
  47. { 0, 0} /* Must be last */
  48. };
  49. void arch_kgdb_breakpoint(void)
  50. {
  51. __asm__ __volatile__(
  52. ".globl breakinst\n\t"
  53. ".set\tnoreorder\n\t"
  54. "nop\n"
  55. "breakinst:\tbreak\n\t"
  56. "nop\n\t"
  57. ".set\treorder");
  58. }
  59. static void kgdb_call_nmi_hook(void *ignored)
  60. {
  61. kgdb_nmicallback(raw_smp_processor_id(), NULL);
  62. }
  63. void kgdb_roundup_cpus(unsigned long flags)
  64. {
  65. local_irq_enable();
  66. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  67. local_irq_disable();
  68. }
  69. static int compute_signal(int tt)
  70. {
  71. struct hard_trap_info *ht;
  72. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  73. if (ht->tt == tt)
  74. return ht->signo;
  75. return SIGHUP; /* default for things we don't know about */
  76. }
  77. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  78. {
  79. int reg;
  80. #if (KGDB_GDB_REG_SIZE == 32)
  81. u32 *ptr = (u32 *)gdb_regs;
  82. #else
  83. u64 *ptr = (u64 *)gdb_regs;
  84. #endif
  85. for (reg = 0; reg < 32; reg++)
  86. *(ptr++) = regs->regs[reg];
  87. *(ptr++) = regs->cp0_status;
  88. *(ptr++) = regs->lo;
  89. *(ptr++) = regs->hi;
  90. *(ptr++) = regs->cp0_badvaddr;
  91. *(ptr++) = regs->cp0_cause;
  92. *(ptr++) = regs->cp0_epc;
  93. /* FP REGS */
  94. if (!(current && (regs->cp0_status & ST0_CU1)))
  95. return;
  96. save_fp(current);
  97. for (reg = 0; reg < 32; reg++)
  98. *(ptr++) = current->thread.fpu.fpr[reg];
  99. }
  100. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  101. {
  102. int reg;
  103. #if (KGDB_GDB_REG_SIZE == 32)
  104. const u32 *ptr = (u32 *)gdb_regs;
  105. #else
  106. const u64 *ptr = (u64 *)gdb_regs;
  107. #endif
  108. for (reg = 0; reg < 32; reg++)
  109. regs->regs[reg] = *(ptr++);
  110. regs->cp0_status = *(ptr++);
  111. regs->lo = *(ptr++);
  112. regs->hi = *(ptr++);
  113. regs->cp0_badvaddr = *(ptr++);
  114. regs->cp0_cause = *(ptr++);
  115. regs->cp0_epc = *(ptr++);
  116. /* FP REGS from current */
  117. if (!(current && (regs->cp0_status & ST0_CU1)))
  118. return;
  119. for (reg = 0; reg < 32; reg++)
  120. current->thread.fpu.fpr[reg] = *(ptr++);
  121. restore_fp(current);
  122. }
  123. /*
  124. * Similar to regs_to_gdb_regs() except that process is sleeping and so
  125. * we may not be able to get all the info.
  126. */
  127. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  128. {
  129. int reg;
  130. struct thread_info *ti = task_thread_info(p);
  131. unsigned long ksp = (unsigned long)ti + THREAD_SIZE - 32;
  132. struct pt_regs *regs = (struct pt_regs *)ksp - 1;
  133. #if (KGDB_GDB_REG_SIZE == 32)
  134. u32 *ptr = (u32 *)gdb_regs;
  135. #else
  136. u64 *ptr = (u64 *)gdb_regs;
  137. #endif
  138. for (reg = 0; reg < 16; reg++)
  139. *(ptr++) = regs->regs[reg];
  140. /* S0 - S7 */
  141. for (reg = 16; reg < 24; reg++)
  142. *(ptr++) = regs->regs[reg];
  143. for (reg = 24; reg < 28; reg++)
  144. *(ptr++) = 0;
  145. /* GP, SP, FP, RA */
  146. for (reg = 28; reg < 32; reg++)
  147. *(ptr++) = regs->regs[reg];
  148. *(ptr++) = regs->cp0_status;
  149. *(ptr++) = regs->lo;
  150. *(ptr++) = regs->hi;
  151. *(ptr++) = regs->cp0_badvaddr;
  152. *(ptr++) = regs->cp0_cause;
  153. *(ptr++) = regs->cp0_epc;
  154. }
  155. /*
  156. * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
  157. * then try to fall into the debugger
  158. */
  159. static int kgdb_mips_notify(struct notifier_block *self, unsigned long cmd,
  160. void *ptr)
  161. {
  162. struct die_args *args = (struct die_args *)ptr;
  163. struct pt_regs *regs = args->regs;
  164. int trap = (regs->cp0_cause & 0x7c) >> 2;
  165. /* Userpace events, ignore. */
  166. if (user_mode(regs))
  167. return NOTIFY_DONE;
  168. if (atomic_read(&kgdb_active) != -1)
  169. kgdb_nmicallback(smp_processor_id(), regs);
  170. if (kgdb_handle_exception(trap, compute_signal(trap), 0, regs))
  171. return NOTIFY_DONE;
  172. if (atomic_read(&kgdb_setting_breakpoint))
  173. if ((trap == 9) && (regs->cp0_epc == (unsigned long)breakinst))
  174. regs->cp0_epc += 4;
  175. /* In SMP mode, __flush_cache_all does IPI */
  176. local_irq_enable();
  177. __flush_cache_all();
  178. return NOTIFY_STOP;
  179. }
  180. static struct notifier_block kgdb_notifier = {
  181. .notifier_call = kgdb_mips_notify,
  182. };
  183. /*
  184. * Handle the 's' and 'c' commands
  185. */
  186. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  187. char *remcom_in_buffer, char *remcom_out_buffer,
  188. struct pt_regs *regs)
  189. {
  190. char *ptr;
  191. unsigned long address;
  192. int cpu = smp_processor_id();
  193. switch (remcom_in_buffer[0]) {
  194. case 's':
  195. case 'c':
  196. /* handle the optional parameter */
  197. ptr = &remcom_in_buffer[1];
  198. if (kgdb_hex2long(&ptr, &address))
  199. regs->cp0_epc = address;
  200. atomic_set(&kgdb_cpu_doing_single_step, -1);
  201. if (remcom_in_buffer[0] == 's')
  202. atomic_set(&kgdb_cpu_doing_single_step, cpu);
  203. return 0;
  204. }
  205. return -1;
  206. }
  207. struct kgdb_arch arch_kgdb_ops;
  208. /*
  209. * We use kgdb_early_setup so that functions we need to call now don't
  210. * cause trouble when called again later.
  211. */
  212. int kgdb_arch_init(void)
  213. {
  214. union mips_instruction insn = {
  215. .r_format = {
  216. .opcode = spec_op,
  217. .func = break_op,
  218. }
  219. };
  220. memcpy(arch_kgdb_ops.gdb_bpt_instr, insn.byte, BREAK_INSTR_SIZE);
  221. register_die_notifier(&kgdb_notifier);
  222. return 0;
  223. }
  224. /*
  225. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  226. *
  227. * This function will handle the uninitalization of any architecture
  228. * specific callbacks, for dynamic registration and unregistration.
  229. */
  230. void kgdb_arch_exit(void)
  231. {
  232. unregister_die_notifier(&kgdb_notifier);
  233. }