kgdb.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * SuperH KGDB support
  3. *
  4. * Copyright (C) 2008 - 2009 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 <asm/cacheflush.h>
  17. #include <asm/traps.h>
  18. /* Macros for single step instruction identification */
  19. #define OPCODE_BT(op) (((op) & 0xff00) == 0x8900)
  20. #define OPCODE_BF(op) (((op) & 0xff00) == 0x8b00)
  21. #define OPCODE_BTF_DISP(op) (((op) & 0x80) ? (((op) | 0xffffff80) << 1) : \
  22. (((op) & 0x7f ) << 1))
  23. #define OPCODE_BFS(op) (((op) & 0xff00) == 0x8f00)
  24. #define OPCODE_BTS(op) (((op) & 0xff00) == 0x8d00)
  25. #define OPCODE_BRA(op) (((op) & 0xf000) == 0xa000)
  26. #define OPCODE_BRA_DISP(op) (((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
  27. (((op) & 0x7ff) << 1))
  28. #define OPCODE_BRAF(op) (((op) & 0xf0ff) == 0x0023)
  29. #define OPCODE_BRAF_REG(op) (((op) & 0x0f00) >> 8)
  30. #define OPCODE_BSR(op) (((op) & 0xf000) == 0xb000)
  31. #define OPCODE_BSR_DISP(op) (((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
  32. (((op) & 0x7ff) << 1))
  33. #define OPCODE_BSRF(op) (((op) & 0xf0ff) == 0x0003)
  34. #define OPCODE_BSRF_REG(op) (((op) >> 8) & 0xf)
  35. #define OPCODE_JMP(op) (((op) & 0xf0ff) == 0x402b)
  36. #define OPCODE_JMP_REG(op) (((op) >> 8) & 0xf)
  37. #define OPCODE_JSR(op) (((op) & 0xf0ff) == 0x400b)
  38. #define OPCODE_JSR_REG(op) (((op) >> 8) & 0xf)
  39. #define OPCODE_RTS(op) ((op) == 0xb)
  40. #define OPCODE_RTE(op) ((op) == 0x2b)
  41. #define SR_T_BIT_MASK 0x1
  42. #define STEP_OPCODE 0xc33d
  43. /* Calculate the new address for after a step */
  44. static short *get_step_address(struct pt_regs *linux_regs)
  45. {
  46. insn_size_t op = __raw_readw(linux_regs->pc);
  47. long addr;
  48. /* BT */
  49. if (OPCODE_BT(op)) {
  50. if (linux_regs->sr & SR_T_BIT_MASK)
  51. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  52. else
  53. addr = linux_regs->pc + 2;
  54. }
  55. /* BTS */
  56. else if (OPCODE_BTS(op)) {
  57. if (linux_regs->sr & SR_T_BIT_MASK)
  58. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  59. else
  60. addr = linux_regs->pc + 4; /* Not in delay slot */
  61. }
  62. /* BF */
  63. else if (OPCODE_BF(op)) {
  64. if (!(linux_regs->sr & SR_T_BIT_MASK))
  65. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  66. else
  67. addr = linux_regs->pc + 2;
  68. }
  69. /* BFS */
  70. else if (OPCODE_BFS(op)) {
  71. if (!(linux_regs->sr & SR_T_BIT_MASK))
  72. addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
  73. else
  74. addr = linux_regs->pc + 4; /* Not in delay slot */
  75. }
  76. /* BRA */
  77. else if (OPCODE_BRA(op))
  78. addr = linux_regs->pc + 4 + OPCODE_BRA_DISP(op);
  79. /* BRAF */
  80. else if (OPCODE_BRAF(op))
  81. addr = linux_regs->pc + 4
  82. + linux_regs->regs[OPCODE_BRAF_REG(op)];
  83. /* BSR */
  84. else if (OPCODE_BSR(op))
  85. addr = linux_regs->pc + 4 + OPCODE_BSR_DISP(op);
  86. /* BSRF */
  87. else if (OPCODE_BSRF(op))
  88. addr = linux_regs->pc + 4
  89. + linux_regs->regs[OPCODE_BSRF_REG(op)];
  90. /* JMP */
  91. else if (OPCODE_JMP(op))
  92. addr = linux_regs->regs[OPCODE_JMP_REG(op)];
  93. /* JSR */
  94. else if (OPCODE_JSR(op))
  95. addr = linux_regs->regs[OPCODE_JSR_REG(op)];
  96. /* RTS */
  97. else if (OPCODE_RTS(op))
  98. addr = linux_regs->pr;
  99. /* RTE */
  100. else if (OPCODE_RTE(op))
  101. addr = linux_regs->regs[15];
  102. /* Other */
  103. else
  104. addr = linux_regs->pc + instruction_size(op);
  105. flush_icache_range(addr, addr + instruction_size(op));
  106. return (short *)addr;
  107. }
  108. /*
  109. * Replace the instruction immediately after the current instruction
  110. * (i.e. next in the expected flow of control) with a trap instruction,
  111. * so that returning will cause only a single instruction to be executed.
  112. * Note that this model is slightly broken for instructions with delay
  113. * slots (e.g. B[TF]S, BSR, BRA etc), where both the branch and the
  114. * instruction in the delay slot will be executed.
  115. */
  116. static unsigned long stepped_address;
  117. static insn_size_t stepped_opcode;
  118. static void do_single_step(struct pt_regs *linux_regs)
  119. {
  120. /* Determine where the target instruction will send us to */
  121. unsigned short *addr = get_step_address(linux_regs);
  122. stepped_address = (int)addr;
  123. /* Replace it */
  124. stepped_opcode = __raw_readw((long)addr);
  125. *addr = STEP_OPCODE;
  126. /* Flush and return */
  127. flush_icache_range((long)addr, (long)addr +
  128. instruction_size(stepped_opcode));
  129. }
  130. /* Undo a single step */
  131. static void undo_single_step(struct pt_regs *linux_regs)
  132. {
  133. /* If we have stepped, put back the old instruction */
  134. /* Use stepped_address in case we stopped elsewhere */
  135. if (stepped_opcode != 0) {
  136. __raw_writew(stepped_opcode, stepped_address);
  137. flush_icache_range(stepped_address, stepped_address + 2);
  138. }
  139. stepped_opcode = 0;
  140. }
  141. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  142. {
  143. int i;
  144. for (i = 0; i < 16; i++)
  145. gdb_regs[GDB_R0 + i] = regs->regs[i];
  146. gdb_regs[GDB_PC] = regs->pc;
  147. gdb_regs[GDB_PR] = regs->pr;
  148. gdb_regs[GDB_SR] = regs->sr;
  149. gdb_regs[GDB_GBR] = regs->gbr;
  150. gdb_regs[GDB_MACH] = regs->mach;
  151. gdb_regs[GDB_MACL] = regs->macl;
  152. __asm__ __volatile__ ("stc vbr, %0" : "=r" (gdb_regs[GDB_VBR]));
  153. }
  154. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  155. {
  156. int i;
  157. for (i = 0; i < 16; i++)
  158. regs->regs[GDB_R0 + i] = gdb_regs[GDB_R0 + i];
  159. regs->pc = gdb_regs[GDB_PC];
  160. regs->pr = gdb_regs[GDB_PR];
  161. regs->sr = gdb_regs[GDB_SR];
  162. regs->gbr = gdb_regs[GDB_GBR];
  163. regs->mach = gdb_regs[GDB_MACH];
  164. regs->macl = gdb_regs[GDB_MACL];
  165. }
  166. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  167. {
  168. gdb_regs[GDB_R15] = p->thread.sp;
  169. gdb_regs[GDB_PC] = p->thread.pc;
  170. }
  171. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  172. char *remcomInBuffer, char *remcomOutBuffer,
  173. struct pt_regs *linux_regs)
  174. {
  175. unsigned long addr;
  176. char *ptr;
  177. /* Undo any stepping we may have done */
  178. undo_single_step(linux_regs);
  179. switch (remcomInBuffer[0]) {
  180. case 'c':
  181. case 's':
  182. /* try to read optional parameter, pc unchanged if no parm */
  183. ptr = &remcomInBuffer[1];
  184. if (kgdb_hex2long(&ptr, &addr))
  185. linux_regs->pc = addr;
  186. case 'D':
  187. case 'k':
  188. atomic_set(&kgdb_cpu_doing_single_step, -1);
  189. if (remcomInBuffer[0] == 's') {
  190. do_single_step(linux_regs);
  191. kgdb_single_step = 1;
  192. atomic_set(&kgdb_cpu_doing_single_step,
  193. raw_smp_processor_id());
  194. }
  195. return 0;
  196. }
  197. /* this means that we do not want to exit from the handler: */
  198. return -1;
  199. }
  200. unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
  201. {
  202. if (exception == 60)
  203. return instruction_pointer(regs) - 2;
  204. return instruction_pointer(regs);
  205. }
  206. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  207. {
  208. regs->pc = ip;
  209. }
  210. /*
  211. * The primary entry points for the kgdb debug trap table entries.
  212. */
  213. BUILD_TRAP_HANDLER(singlestep)
  214. {
  215. unsigned long flags;
  216. TRAP_HANDLER_DECL;
  217. local_irq_save(flags);
  218. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  219. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  220. local_irq_restore(flags);
  221. }
  222. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  223. {
  224. int ret;
  225. switch (cmd) {
  226. case DIE_BREAKPOINT:
  227. /*
  228. * This means a user thread is single stepping
  229. * a system call which should be ignored
  230. */
  231. if (test_thread_flag(TIF_SINGLESTEP))
  232. return NOTIFY_DONE;
  233. ret = kgdb_handle_exception(args->trapnr & 0xff, args->signr,
  234. args->err, args->regs);
  235. if (ret)
  236. return NOTIFY_DONE;
  237. break;
  238. }
  239. return NOTIFY_STOP;
  240. }
  241. static int
  242. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  243. {
  244. unsigned long flags;
  245. int ret;
  246. local_irq_save(flags);
  247. ret = __kgdb_notify(ptr, cmd);
  248. local_irq_restore(flags);
  249. return ret;
  250. }
  251. static struct notifier_block kgdb_notifier = {
  252. .notifier_call = kgdb_notify,
  253. /*
  254. * Lowest-prio notifier priority, we want to be notified last:
  255. */
  256. .priority = -INT_MAX,
  257. };
  258. int kgdb_arch_init(void)
  259. {
  260. return register_die_notifier(&kgdb_notifier);
  261. }
  262. void kgdb_arch_exit(void)
  263. {
  264. unregister_die_notifier(&kgdb_notifier);
  265. }
  266. struct kgdb_arch arch_kgdb_ops = {
  267. /* Breakpoint instruction: trapa #0x3c */
  268. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  269. .gdb_bpt_instr = { 0x3c, 0xc3 },
  270. #else
  271. .gdb_bpt_instr = { 0xc3, 0x3c },
  272. #endif
  273. };