kgdb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * SuperH KGDB support
  3. *
  4. * Copyright (C) 2008 - 2012 Paul Mundt
  5. *
  6. * Single stepping taken from the old stub by Henry Bell and Jeremy Siegel.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/kgdb.h>
  13. #include <linux/kdebug.h>
  14. #include <linux/irq.h>
  15. #include <linux/io.h>
  16. #include <linux/sched.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/traps.h>
  19. /* Macros for single step instruction identification */
  20. #define OPCODE_BT(op) (((op) & 0xff00) == 0x8900)
  21. #define OPCODE_BF(op) (((op) & 0xff00) == 0x8b00)
  22. #define OPCODE_BTF_DISP(op) (((op) & 0x80) ? (((op) | 0xffffff80) << 1) : \
  23. (((op) & 0x7f ) << 1))
  24. #define OPCODE_BFS(op) (((op) & 0xff00) == 0x8f00)
  25. #define OPCODE_BTS(op) (((op) & 0xff00) == 0x8d00)
  26. #define OPCODE_BRA(op) (((op) & 0xf000) == 0xa000)
  27. #define OPCODE_BRA_DISP(op) (((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
  28. (((op) & 0x7ff) << 1))
  29. #define OPCODE_BRAF(op) (((op) & 0xf0ff) == 0x0023)
  30. #define OPCODE_BRAF_REG(op) (((op) & 0x0f00) >> 8)
  31. #define OPCODE_BSR(op) (((op) & 0xf000) == 0xb000)
  32. #define OPCODE_BSR_DISP(op) (((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
  33. (((op) & 0x7ff) << 1))
  34. #define OPCODE_BSRF(op) (((op) & 0xf0ff) == 0x0003)
  35. #define OPCODE_BSRF_REG(op) (((op) >> 8) & 0xf)
  36. #define OPCODE_JMP(op) (((op) & 0xf0ff) == 0x402b)
  37. #define OPCODE_JMP_REG(op) (((op) >> 8) & 0xf)
  38. #define OPCODE_JSR(op) (((op) & 0xf0ff) == 0x400b)
  39. #define OPCODE_JSR_REG(op) (((op) >> 8) & 0xf)
  40. #define OPCODE_RTS(op) ((op) == 0xb)
  41. #define OPCODE_RTE(op) ((op) == 0x2b)
  42. #define SR_T_BIT_MASK 0x1
  43. #define STEP_OPCODE 0xc33d
  44. /* Calculate the new address for after a step */
  45. static short *get_step_address(struct pt_regs *linux_regs)
  46. {
  47. insn_size_t op = __raw_readw(linux_regs->pc);
  48. long addr;
  49. /* BT */
  50. if (OPCODE_BT(op)) {
  51. if (linux_regs->sr & SR_T_BIT_MASK)
  52. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  53. else
  54. addr = linux_regs->pc + 2;
  55. }
  56. /* BTS */
  57. else if (OPCODE_BTS(op)) {
  58. if (linux_regs->sr & SR_T_BIT_MASK)
  59. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  60. else
  61. addr = linux_regs->pc + 4; /* Not in delay slot */
  62. }
  63. /* BF */
  64. else if (OPCODE_BF(op)) {
  65. if (!(linux_regs->sr & SR_T_BIT_MASK))
  66. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  67. else
  68. addr = linux_regs->pc + 2;
  69. }
  70. /* BFS */
  71. else if (OPCODE_BFS(op)) {
  72. if (!(linux_regs->sr & SR_T_BIT_MASK))
  73. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  74. else
  75. addr = linux_regs->pc + 4; /* Not in delay slot */
  76. }
  77. /* BRA */
  78. else if (OPCODE_BRA(op))
  79. addr = linux_regs->pc + 4 + OPCODE_BRA_DISP(op);
  80. /* BRAF */
  81. else if (OPCODE_BRAF(op))
  82. addr = linux_regs->pc + 4
  83. + linux_regs->regs[OPCODE_BRAF_REG(op)];
  84. /* BSR */
  85. else if (OPCODE_BSR(op))
  86. addr = linux_regs->pc + 4 + OPCODE_BSR_DISP(op);
  87. /* BSRF */
  88. else if (OPCODE_BSRF(op))
  89. addr = linux_regs->pc + 4
  90. + linux_regs->regs[OPCODE_BSRF_REG(op)];
  91. /* JMP */
  92. else if (OPCODE_JMP(op))
  93. addr = linux_regs->regs[OPCODE_JMP_REG(op)];
  94. /* JSR */
  95. else if (OPCODE_JSR(op))
  96. addr = linux_regs->regs[OPCODE_JSR_REG(op)];
  97. /* RTS */
  98. else if (OPCODE_RTS(op))
  99. addr = linux_regs->pr;
  100. /* RTE */
  101. else if (OPCODE_RTE(op))
  102. addr = linux_regs->regs[15];
  103. /* Other */
  104. else
  105. addr = linux_regs->pc + instruction_size(op);
  106. flush_icache_range(addr, addr + instruction_size(op));
  107. return (short *)addr;
  108. }
  109. /*
  110. * Replace the instruction immediately after the current instruction
  111. * (i.e. next in the expected flow of control) with a trap instruction,
  112. * so that returning will cause only a single instruction to be executed.
  113. * Note that this model is slightly broken for instructions with delay
  114. * slots (e.g. B[TF]S, BSR, BRA etc), where both the branch and the
  115. * instruction in the delay slot will be executed.
  116. */
  117. static unsigned long stepped_address;
  118. static insn_size_t stepped_opcode;
  119. static void do_single_step(struct pt_regs *linux_regs)
  120. {
  121. /* Determine where the target instruction will send us to */
  122. unsigned short *addr = get_step_address(linux_regs);
  123. stepped_address = (int)addr;
  124. /* Replace it */
  125. stepped_opcode = __raw_readw((long)addr);
  126. *addr = STEP_OPCODE;
  127. /* Flush and return */
  128. flush_icache_range((long)addr, (long)addr +
  129. instruction_size(stepped_opcode));
  130. }
  131. /* Undo a single step */
  132. static void undo_single_step(struct pt_regs *linux_regs)
  133. {
  134. /* If we have stepped, put back the old instruction */
  135. /* Use stepped_address in case we stopped elsewhere */
  136. if (stepped_opcode != 0) {
  137. __raw_writew(stepped_opcode, stepped_address);
  138. flush_icache_range(stepped_address, stepped_address + 2);
  139. }
  140. stepped_opcode = 0;
  141. }
  142. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  143. { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
  144. { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
  145. { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
  146. { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
  147. { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
  148. { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
  149. { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
  150. { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
  151. { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
  152. { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
  153. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
  154. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
  155. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
  156. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
  157. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
  158. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
  159. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, pc) },
  160. { "pr", GDB_SIZEOF_REG, offsetof(struct pt_regs, pr) },
  161. { "sr", GDB_SIZEOF_REG, offsetof(struct pt_regs, sr) },
  162. { "gbr", GDB_SIZEOF_REG, offsetof(struct pt_regs, gbr) },
  163. { "mach", GDB_SIZEOF_REG, offsetof(struct pt_regs, mach) },
  164. { "macl", GDB_SIZEOF_REG, offsetof(struct pt_regs, macl) },
  165. { "vbr", GDB_SIZEOF_REG, -1 },
  166. };
  167. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  168. {
  169. if (regno < 0 || regno >= DBG_MAX_REG_NUM)
  170. return -EINVAL;
  171. if (dbg_reg_def[regno].offset != -1)
  172. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  173. dbg_reg_def[regno].size);
  174. return 0;
  175. }
  176. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  177. {
  178. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  179. return NULL;
  180. if (dbg_reg_def[regno].size != -1)
  181. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  182. dbg_reg_def[regno].size);
  183. switch (regno) {
  184. case GDB_VBR:
  185. __asm__ __volatile__ ("stc vbr, %0" : "=r" (mem));
  186. break;
  187. }
  188. return dbg_reg_def[regno].name;
  189. }
  190. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  191. {
  192. struct pt_regs *thread_regs = task_pt_regs(p);
  193. int reg;
  194. /* Initialize to zero */
  195. for (reg = 0; reg < DBG_MAX_REG_NUM; reg++)
  196. gdb_regs[reg] = 0;
  197. /*
  198. * Copy out GP regs 8 to 14.
  199. *
  200. * switch_to() relies on SR.RB toggling, so regs 0->7 are banked
  201. * and need privileged instructions to get to. The r15 value we
  202. * fetch from the thread info directly.
  203. */
  204. for (reg = GDB_R8; reg < GDB_R15; reg++)
  205. gdb_regs[reg] = thread_regs->regs[reg];
  206. gdb_regs[GDB_R15] = p->thread.sp;
  207. gdb_regs[GDB_PC] = p->thread.pc;
  208. /*
  209. * Additional registers we have context for
  210. */
  211. gdb_regs[GDB_PR] = thread_regs->pr;
  212. gdb_regs[GDB_GBR] = thread_regs->gbr;
  213. }
  214. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  215. char *remcomInBuffer, char *remcomOutBuffer,
  216. struct pt_regs *linux_regs)
  217. {
  218. unsigned long addr;
  219. char *ptr;
  220. /* Undo any stepping we may have done */
  221. undo_single_step(linux_regs);
  222. switch (remcomInBuffer[0]) {
  223. case 'c':
  224. case 's':
  225. /* try to read optional parameter, pc unchanged if no parm */
  226. ptr = &remcomInBuffer[1];
  227. if (kgdb_hex2long(&ptr, &addr))
  228. linux_regs->pc = addr;
  229. case 'D':
  230. case 'k':
  231. atomic_set(&kgdb_cpu_doing_single_step, -1);
  232. if (remcomInBuffer[0] == 's') {
  233. do_single_step(linux_regs);
  234. kgdb_single_step = 1;
  235. atomic_set(&kgdb_cpu_doing_single_step,
  236. raw_smp_processor_id());
  237. }
  238. return 0;
  239. }
  240. /* this means that we do not want to exit from the handler: */
  241. return -1;
  242. }
  243. unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
  244. {
  245. if (exception == 60)
  246. return instruction_pointer(regs) - 2;
  247. return instruction_pointer(regs);
  248. }
  249. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  250. {
  251. regs->pc = ip;
  252. }
  253. /*
  254. * The primary entry points for the kgdb debug trap table entries.
  255. */
  256. BUILD_TRAP_HANDLER(singlestep)
  257. {
  258. unsigned long flags;
  259. TRAP_HANDLER_DECL;
  260. local_irq_save(flags);
  261. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  262. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  263. local_irq_restore(flags);
  264. }
  265. static void kgdb_call_nmi_hook(void *ignored)
  266. {
  267. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  268. }
  269. void kgdb_roundup_cpus(unsigned long flags)
  270. {
  271. local_irq_enable();
  272. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  273. local_irq_disable();
  274. }
  275. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  276. {
  277. int ret;
  278. switch (cmd) {
  279. case DIE_BREAKPOINT:
  280. /*
  281. * This means a user thread is single stepping
  282. * a system call which should be ignored
  283. */
  284. if (test_thread_flag(TIF_SINGLESTEP))
  285. return NOTIFY_DONE;
  286. ret = kgdb_handle_exception(args->trapnr & 0xff, args->signr,
  287. args->err, args->regs);
  288. if (ret)
  289. return NOTIFY_DONE;
  290. break;
  291. }
  292. return NOTIFY_STOP;
  293. }
  294. static int
  295. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  296. {
  297. unsigned long flags;
  298. int ret;
  299. local_irq_save(flags);
  300. ret = __kgdb_notify(ptr, cmd);
  301. local_irq_restore(flags);
  302. return ret;
  303. }
  304. static struct notifier_block kgdb_notifier = {
  305. .notifier_call = kgdb_notify,
  306. /*
  307. * Lowest-prio notifier priority, we want to be notified last:
  308. */
  309. .priority = -INT_MAX,
  310. };
  311. int kgdb_arch_init(void)
  312. {
  313. return register_die_notifier(&kgdb_notifier);
  314. }
  315. void kgdb_arch_exit(void)
  316. {
  317. unregister_die_notifier(&kgdb_notifier);
  318. }
  319. struct kgdb_arch arch_kgdb_ops = {
  320. /* Breakpoint instruction: trapa #0x3c */
  321. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  322. .gdb_bpt_instr = { 0x3c, 0xc3 },
  323. #else
  324. .gdb_bpt_instr = { 0xc3, 0x3c },
  325. #endif
  326. };