kgdb.c 6.6 KB

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