kgdb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #if (KGDB_GDB_REG_SIZE == 32)
  226. u32 *ptr = (u32 *)gdb_regs;
  227. #else
  228. u64 *ptr = (u64 *)gdb_regs;
  229. #endif
  230. for (reg = 0; reg < 16; reg++)
  231. *(ptr++) = 0;
  232. /* S0 - S7 */
  233. *(ptr++) = p->thread.reg16;
  234. *(ptr++) = p->thread.reg17;
  235. *(ptr++) = p->thread.reg18;
  236. *(ptr++) = p->thread.reg19;
  237. *(ptr++) = p->thread.reg20;
  238. *(ptr++) = p->thread.reg21;
  239. *(ptr++) = p->thread.reg22;
  240. *(ptr++) = p->thread.reg23;
  241. for (reg = 24; reg < 28; reg++)
  242. *(ptr++) = 0;
  243. /* GP, SP, FP, RA */
  244. *(ptr++) = (long)p;
  245. *(ptr++) = p->thread.reg29;
  246. *(ptr++) = p->thread.reg30;
  247. *(ptr++) = p->thread.reg31;
  248. *(ptr++) = p->thread.cp0_status;
  249. /* lo, hi */
  250. *(ptr++) = 0;
  251. *(ptr++) = 0;
  252. /*
  253. * BadVAddr, Cause
  254. * Ideally these would come from the last exception frame up the stack
  255. * but that requires unwinding, otherwise we can't know much for sure.
  256. */
  257. *(ptr++) = 0;
  258. *(ptr++) = 0;
  259. /*
  260. * PC
  261. * use return address (RA), i.e. the moment after return from resume()
  262. */
  263. *(ptr++) = p->thread.reg31;
  264. }
  265. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  266. {
  267. regs->cp0_epc = pc;
  268. }
  269. /*
  270. * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
  271. * then try to fall into the debugger
  272. */
  273. static int kgdb_mips_notify(struct notifier_block *self, unsigned long cmd,
  274. void *ptr)
  275. {
  276. struct die_args *args = (struct die_args *)ptr;
  277. struct pt_regs *regs = args->regs;
  278. int trap = (regs->cp0_cause & 0x7c) >> 2;
  279. mm_segment_t old_fs;
  280. #ifdef CONFIG_KPROBES
  281. /*
  282. * Return immediately if the kprobes fault notifier has set
  283. * DIE_PAGE_FAULT.
  284. */
  285. if (cmd == DIE_PAGE_FAULT)
  286. return NOTIFY_DONE;
  287. #endif /* CONFIG_KPROBES */
  288. /* Userspace events, ignore. */
  289. if (user_mode(regs))
  290. return NOTIFY_DONE;
  291. /* Kernel mode. Set correct address limit */
  292. old_fs = get_fs();
  293. set_fs(get_ds());
  294. if (atomic_read(&kgdb_active) != -1)
  295. kgdb_nmicallback(smp_processor_id(), regs);
  296. if (kgdb_handle_exception(trap, compute_signal(trap), cmd, regs)) {
  297. set_fs(old_fs);
  298. return NOTIFY_DONE;
  299. }
  300. if (atomic_read(&kgdb_setting_breakpoint))
  301. if ((trap == 9) && (regs->cp0_epc == (unsigned long)breakinst))
  302. regs->cp0_epc += 4;
  303. /* In SMP mode, __flush_cache_all does IPI */
  304. local_irq_enable();
  305. __flush_cache_all();
  306. set_fs(old_fs);
  307. return NOTIFY_STOP;
  308. }
  309. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  310. int kgdb_ll_trap(int cmd, const char *str,
  311. struct pt_regs *regs, long err, int trap, int sig)
  312. {
  313. struct die_args args = {
  314. .regs = regs,
  315. .str = str,
  316. .err = err,
  317. .trapnr = trap,
  318. .signr = sig,
  319. };
  320. if (!kgdb_io_module_registered)
  321. return NOTIFY_DONE;
  322. return kgdb_mips_notify(NULL, cmd, &args);
  323. }
  324. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  325. static struct notifier_block kgdb_notifier = {
  326. .notifier_call = kgdb_mips_notify,
  327. };
  328. /*
  329. * Handle the 'c' command
  330. */
  331. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  332. char *remcom_in_buffer, char *remcom_out_buffer,
  333. struct pt_regs *regs)
  334. {
  335. char *ptr;
  336. unsigned long address;
  337. switch (remcom_in_buffer[0]) {
  338. case 'c':
  339. /* handle the optional parameter */
  340. ptr = &remcom_in_buffer[1];
  341. if (kgdb_hex2long(&ptr, &address))
  342. regs->cp0_epc = address;
  343. return 0;
  344. }
  345. return -1;
  346. }
  347. struct kgdb_arch arch_kgdb_ops;
  348. int kgdb_arch_init(void)
  349. {
  350. union mips_instruction insn = {
  351. .r_format = {
  352. .opcode = spec_op,
  353. .func = break_op,
  354. }
  355. };
  356. memcpy(arch_kgdb_ops.gdb_bpt_instr, insn.byte, BREAK_INSTR_SIZE);
  357. register_die_notifier(&kgdb_notifier);
  358. return 0;
  359. }
  360. /*
  361. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  362. *
  363. * This function will handle the uninitalization of any architecture
  364. * specific callbacks, for dynamic registration and unregistration.
  365. */
  366. void kgdb_arch_exit(void)
  367. {
  368. unregister_die_notifier(&kgdb_notifier);
  369. }