kgdb.c 12 KB

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