kgdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  51. {
  52. { "zero", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
  53. { "at", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
  54. { "v0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
  55. { "v1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
  56. { "a0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
  57. { "a1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
  58. { "a2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
  59. { "a3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
  60. { "t0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
  61. { "t1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
  62. { "t2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
  63. { "t3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
  64. { "t4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
  65. { "t5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
  66. { "t6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
  67. { "t7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
  68. { "s0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[16]) },
  69. { "s1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[17]) },
  70. { "s2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[18]) },
  71. { "s3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[19]) },
  72. { "s4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[20]) },
  73. { "s5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[21]) },
  74. { "s6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[22]) },
  75. { "s7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[23]) },
  76. { "t8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[24]) },
  77. { "t9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[25]) },
  78. { "k0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[26]) },
  79. { "k1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[27]) },
  80. { "gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[28]) },
  81. { "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[29]) },
  82. { "s8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[30]) },
  83. { "ra", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[31]) },
  84. { "sr", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_status) },
  85. { "lo", GDB_SIZEOF_REG, offsetof(struct pt_regs, lo) },
  86. { "hi", GDB_SIZEOF_REG, offsetof(struct pt_regs, hi) },
  87. { "bad", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_badvaddr) },
  88. { "cause", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_cause) },
  89. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_epc) },
  90. { "f0", GDB_SIZEOF_REG, 0 },
  91. { "f1", GDB_SIZEOF_REG, 1 },
  92. { "f2", GDB_SIZEOF_REG, 2 },
  93. { "f3", GDB_SIZEOF_REG, 3 },
  94. { "f4", GDB_SIZEOF_REG, 4 },
  95. { "f5", GDB_SIZEOF_REG, 5 },
  96. { "f6", GDB_SIZEOF_REG, 6 },
  97. { "f7", GDB_SIZEOF_REG, 7 },
  98. { "f8", GDB_SIZEOF_REG, 8 },
  99. { "f9", GDB_SIZEOF_REG, 9 },
  100. { "f10", GDB_SIZEOF_REG, 10 },
  101. { "f11", GDB_SIZEOF_REG, 11 },
  102. { "f12", GDB_SIZEOF_REG, 12 },
  103. { "f13", GDB_SIZEOF_REG, 13 },
  104. { "f14", GDB_SIZEOF_REG, 14 },
  105. { "f15", GDB_SIZEOF_REG, 15 },
  106. { "f16", GDB_SIZEOF_REG, 16 },
  107. { "f17", GDB_SIZEOF_REG, 17 },
  108. { "f18", GDB_SIZEOF_REG, 18 },
  109. { "f19", GDB_SIZEOF_REG, 19 },
  110. { "f20", GDB_SIZEOF_REG, 20 },
  111. { "f21", GDB_SIZEOF_REG, 21 },
  112. { "f22", GDB_SIZEOF_REG, 22 },
  113. { "f23", GDB_SIZEOF_REG, 23 },
  114. { "f24", GDB_SIZEOF_REG, 24 },
  115. { "f25", GDB_SIZEOF_REG, 25 },
  116. { "f26", GDB_SIZEOF_REG, 26 },
  117. { "f27", GDB_SIZEOF_REG, 27 },
  118. { "f28", GDB_SIZEOF_REG, 28 },
  119. { "f29", GDB_SIZEOF_REG, 29 },
  120. { "f30", GDB_SIZEOF_REG, 30 },
  121. { "f31", GDB_SIZEOF_REG, 31 },
  122. { "fsr", GDB_SIZEOF_REG, 0 },
  123. { "fir", GDB_SIZEOF_REG, 0 },
  124. };
  125. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  126. {
  127. int fp_reg;
  128. if (regno < 0 || regno >= DBG_MAX_REG_NUM)
  129. return -EINVAL;
  130. if (dbg_reg_def[regno].offset != -1 && regno < 38) {
  131. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  132. dbg_reg_def[regno].size);
  133. } else if (current && dbg_reg_def[regno].offset != -1 && regno < 72) {
  134. /* FP registers 38 -> 69 */
  135. if (!(regs->cp0_status & ST0_CU1))
  136. return 0;
  137. if (regno == 70) {
  138. /* Process the fcr31/fsr (register 70) */
  139. memcpy((void *)&current->thread.fpu.fcr31, mem,
  140. dbg_reg_def[regno].size);
  141. goto out_save;
  142. } else if (regno == 71) {
  143. /* Ignore the fir (register 71) */
  144. goto out_save;
  145. }
  146. fp_reg = dbg_reg_def[regno].offset;
  147. memcpy((void *)&current->thread.fpu.fpr[fp_reg], mem,
  148. dbg_reg_def[regno].size);
  149. out_save:
  150. restore_fp(current);
  151. }
  152. return 0;
  153. }
  154. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  155. {
  156. int fp_reg;
  157. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  158. return NULL;
  159. if (dbg_reg_def[regno].offset != -1 && regno < 38) {
  160. /* First 38 registers */
  161. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  162. dbg_reg_def[regno].size);
  163. } else if (current && dbg_reg_def[regno].offset != -1 && regno < 72) {
  164. /* FP registers 38 -> 69 */
  165. if (!(regs->cp0_status & ST0_CU1))
  166. goto out;
  167. save_fp(current);
  168. if (regno == 70) {
  169. /* Process the fcr31/fsr (register 70) */
  170. memcpy(mem, (void *)&current->thread.fpu.fcr31,
  171. dbg_reg_def[regno].size);
  172. goto out;
  173. } else if (regno == 71) {
  174. /* Ignore the fir (register 71) */
  175. memset(mem, 0, dbg_reg_def[regno].size);
  176. goto out;
  177. }
  178. fp_reg = dbg_reg_def[regno].offset;
  179. memcpy(mem, (void *)&current->thread.fpu.fpr[fp_reg],
  180. dbg_reg_def[regno].size);
  181. }
  182. out:
  183. return dbg_reg_def[regno].name;
  184. }
  185. void arch_kgdb_breakpoint(void)
  186. {
  187. __asm__ __volatile__(
  188. ".globl breakinst\n\t"
  189. ".set\tnoreorder\n\t"
  190. "nop\n"
  191. "breakinst:\tbreak\n\t"
  192. "nop\n\t"
  193. ".set\treorder");
  194. }
  195. static void kgdb_call_nmi_hook(void *ignored)
  196. {
  197. kgdb_nmicallback(raw_smp_processor_id(), NULL);
  198. }
  199. void kgdb_roundup_cpus(unsigned long flags)
  200. {
  201. local_irq_enable();
  202. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  203. local_irq_disable();
  204. }
  205. static int compute_signal(int tt)
  206. {
  207. struct hard_trap_info *ht;
  208. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  209. if (ht->tt == tt)
  210. return ht->signo;
  211. return SIGHUP; /* default for things we don't know about */
  212. }
  213. /*
  214. * Similar to regs_to_gdb_regs() except that process is sleeping and so
  215. * we may not be able to get all the info.
  216. */
  217. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  218. {
  219. int reg;
  220. struct thread_info *ti = task_thread_info(p);
  221. unsigned long ksp = (unsigned long)ti + THREAD_SIZE - 32;
  222. struct pt_regs *regs = (struct pt_regs *)ksp - 1;
  223. #if (KGDB_GDB_REG_SIZE == 32)
  224. u32 *ptr = (u32 *)gdb_regs;
  225. #else
  226. u64 *ptr = (u64 *)gdb_regs;
  227. #endif
  228. for (reg = 0; reg < 16; reg++)
  229. *(ptr++) = regs->regs[reg];
  230. /* S0 - S7 */
  231. for (reg = 16; reg < 24; reg++)
  232. *(ptr++) = regs->regs[reg];
  233. for (reg = 24; reg < 28; reg++)
  234. *(ptr++) = 0;
  235. /* GP, SP, FP, RA */
  236. for (reg = 28; reg < 32; reg++)
  237. *(ptr++) = regs->regs[reg];
  238. *(ptr++) = regs->cp0_status;
  239. *(ptr++) = regs->lo;
  240. *(ptr++) = regs->hi;
  241. *(ptr++) = regs->cp0_badvaddr;
  242. *(ptr++) = regs->cp0_cause;
  243. *(ptr++) = regs->cp0_epc;
  244. }
  245. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  246. {
  247. regs->cp0_epc = pc;
  248. }
  249. /*
  250. * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
  251. * then try to fall into the debugger
  252. */
  253. static int kgdb_mips_notify(struct notifier_block *self, unsigned long cmd,
  254. void *ptr)
  255. {
  256. struct die_args *args = (struct die_args *)ptr;
  257. struct pt_regs *regs = args->regs;
  258. int trap = (regs->cp0_cause & 0x7c) >> 2;
  259. /* Userspace events, ignore. */
  260. if (user_mode(regs))
  261. return NOTIFY_DONE;
  262. if (atomic_read(&kgdb_active) != -1)
  263. kgdb_nmicallback(smp_processor_id(), regs);
  264. if (kgdb_handle_exception(trap, compute_signal(trap), cmd, regs))
  265. return NOTIFY_DONE;
  266. if (atomic_read(&kgdb_setting_breakpoint))
  267. if ((trap == 9) && (regs->cp0_epc == (unsigned long)breakinst))
  268. regs->cp0_epc += 4;
  269. /* In SMP mode, __flush_cache_all does IPI */
  270. local_irq_enable();
  271. __flush_cache_all();
  272. return NOTIFY_STOP;
  273. }
  274. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  275. int kgdb_ll_trap(int cmd, const char *str,
  276. struct pt_regs *regs, long err, int trap, int sig)
  277. {
  278. struct die_args args = {
  279. .regs = regs,
  280. .str = str,
  281. .err = err,
  282. .trapnr = trap,
  283. .signr = sig,
  284. };
  285. if (!kgdb_io_module_registered)
  286. return NOTIFY_DONE;
  287. return kgdb_mips_notify(NULL, cmd, &args);
  288. }
  289. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  290. static struct notifier_block kgdb_notifier = {
  291. .notifier_call = kgdb_mips_notify,
  292. };
  293. /*
  294. * Handle the 'c' command
  295. */
  296. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  297. char *remcom_in_buffer, char *remcom_out_buffer,
  298. struct pt_regs *regs)
  299. {
  300. char *ptr;
  301. unsigned long address;
  302. switch (remcom_in_buffer[0]) {
  303. case 'c':
  304. /* handle the optional parameter */
  305. ptr = &remcom_in_buffer[1];
  306. if (kgdb_hex2long(&ptr, &address))
  307. regs->cp0_epc = address;
  308. return 0;
  309. }
  310. return -1;
  311. }
  312. struct kgdb_arch arch_kgdb_ops;
  313. /*
  314. * We use kgdb_early_setup so that functions we need to call now don't
  315. * cause trouble when called again later.
  316. */
  317. int kgdb_arch_init(void)
  318. {
  319. union mips_instruction insn = {
  320. .r_format = {
  321. .opcode = spec_op,
  322. .func = break_op,
  323. }
  324. };
  325. memcpy(arch_kgdb_ops.gdb_bpt_instr, insn.byte, BREAK_INSTR_SIZE);
  326. register_die_notifier(&kgdb_notifier);
  327. return 0;
  328. }
  329. /*
  330. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  331. *
  332. * This function will handle the uninitalization of any architecture
  333. * specific callbacks, for dynamic registration and unregistration.
  334. */
  335. void kgdb_arch_exit(void)
  336. {
  337. unregister_die_notifier(&kgdb_notifier);
  338. }