kgdb.c 7.0 KB

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