kgdb.c 10 KB

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