kgdb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH KGDB support
  4. *
  5. * Copyright (C) 2008 - 2012 Paul Mundt
  6. *
  7. * Single stepping taken from the old stub by Henry Bell and Jeremy Siegel.
  8. */
  9. #include <linux/kgdb.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/irq.h>
  12. #include <linux/io.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/traps.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. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  141. { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
  142. { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
  143. { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
  144. { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
  145. { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
  146. { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
  147. { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
  148. { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
  149. { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
  150. { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
  151. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
  152. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
  153. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
  154. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
  155. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
  156. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
  157. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, pc) },
  158. { "pr", GDB_SIZEOF_REG, offsetof(struct pt_regs, pr) },
  159. { "sr", GDB_SIZEOF_REG, offsetof(struct pt_regs, sr) },
  160. { "gbr", GDB_SIZEOF_REG, offsetof(struct pt_regs, gbr) },
  161. { "mach", GDB_SIZEOF_REG, offsetof(struct pt_regs, mach) },
  162. { "macl", GDB_SIZEOF_REG, offsetof(struct pt_regs, macl) },
  163. { "vbr", GDB_SIZEOF_REG, -1 },
  164. };
  165. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  166. {
  167. if (regno < 0 || regno >= DBG_MAX_REG_NUM)
  168. return -EINVAL;
  169. if (dbg_reg_def[regno].offset != -1)
  170. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  171. dbg_reg_def[regno].size);
  172. return 0;
  173. }
  174. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  175. {
  176. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  177. return NULL;
  178. if (dbg_reg_def[regno].size != -1)
  179. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  180. dbg_reg_def[regno].size);
  181. switch (regno) {
  182. case GDB_VBR:
  183. __asm__ __volatile__ ("stc vbr, %0" : "=r" (mem));
  184. break;
  185. }
  186. return dbg_reg_def[regno].name;
  187. }
  188. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  189. {
  190. struct pt_regs *thread_regs = task_pt_regs(p);
  191. int reg;
  192. /* Initialize to zero */
  193. for (reg = 0; reg < DBG_MAX_REG_NUM; reg++)
  194. gdb_regs[reg] = 0;
  195. /*
  196. * Copy out GP regs 8 to 14.
  197. *
  198. * switch_to() relies on SR.RB toggling, so regs 0->7 are banked
  199. * and need privileged instructions to get to. The r15 value we
  200. * fetch from the thread info directly.
  201. */
  202. for (reg = GDB_R8; reg < GDB_R15; reg++)
  203. gdb_regs[reg] = thread_regs->regs[reg];
  204. gdb_regs[GDB_R15] = p->thread.sp;
  205. gdb_regs[GDB_PC] = p->thread.pc;
  206. /*
  207. * Additional registers we have context for
  208. */
  209. gdb_regs[GDB_PR] = thread_regs->pr;
  210. gdb_regs[GDB_GBR] = thread_regs->gbr;
  211. }
  212. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  213. char *remcomInBuffer, char *remcomOutBuffer,
  214. struct pt_regs *linux_regs)
  215. {
  216. unsigned long addr;
  217. char *ptr;
  218. /* Undo any stepping we may have done */
  219. undo_single_step(linux_regs);
  220. switch (remcomInBuffer[0]) {
  221. case 'c':
  222. case 's':
  223. /* try to read optional parameter, pc unchanged if no parm */
  224. ptr = &remcomInBuffer[1];
  225. if (kgdb_hex2long(&ptr, &addr))
  226. linux_regs->pc = addr;
  227. fallthrough;
  228. case 'D':
  229. case 'k':
  230. atomic_set(&kgdb_cpu_doing_single_step, -1);
  231. if (remcomInBuffer[0] == 's') {
  232. do_single_step(linux_regs);
  233. kgdb_single_step = 1;
  234. atomic_set(&kgdb_cpu_doing_single_step,
  235. raw_smp_processor_id());
  236. }
  237. return 0;
  238. }
  239. /* this means that we do not want to exit from the handler: */
  240. return -1;
  241. }
  242. unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
  243. {
  244. if (exception == 60)
  245. return instruction_pointer(regs) - 2;
  246. return instruction_pointer(regs);
  247. }
  248. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  249. {
  250. regs->pc = ip;
  251. }
  252. /*
  253. * The primary entry points for the kgdb debug trap table entries.
  254. */
  255. BUILD_TRAP_HANDLER(singlestep)
  256. {
  257. unsigned long flags;
  258. TRAP_HANDLER_DECL;
  259. local_irq_save(flags);
  260. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  261. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  262. local_irq_restore(flags);
  263. }
  264. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  265. {
  266. int ret;
  267. switch (cmd) {
  268. case DIE_BREAKPOINT:
  269. /*
  270. * This means a user thread is single stepping
  271. * a system call which should be ignored
  272. */
  273. if (test_thread_flag(TIF_SINGLESTEP))
  274. return NOTIFY_DONE;
  275. ret = kgdb_handle_exception(args->trapnr & 0xff, args->signr,
  276. args->err, args->regs);
  277. if (ret)
  278. return NOTIFY_DONE;
  279. break;
  280. }
  281. return NOTIFY_STOP;
  282. }
  283. static int
  284. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  285. {
  286. unsigned long flags;
  287. int ret;
  288. local_irq_save(flags);
  289. ret = __kgdb_notify(ptr, cmd);
  290. local_irq_restore(flags);
  291. return ret;
  292. }
  293. static struct notifier_block kgdb_notifier = {
  294. .notifier_call = kgdb_notify,
  295. /*
  296. * Lowest-prio notifier priority, we want to be notified last:
  297. */
  298. .priority = -INT_MAX,
  299. };
  300. int kgdb_arch_init(void)
  301. {
  302. return register_die_notifier(&kgdb_notifier);
  303. }
  304. void kgdb_arch_exit(void)
  305. {
  306. unregister_die_notifier(&kgdb_notifier);
  307. }
  308. const struct kgdb_arch arch_kgdb_ops = {
  309. /* Breakpoint instruction: trapa #0x3c */
  310. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  311. .gdb_bpt_instr = { 0x3c, 0xc3 },
  312. #else
  313. .gdb_bpt_instr = { 0xc3, 0x3c },
  314. #endif
  315. };