kgdb.c 11 KB

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