kgdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 SiFive
  4. */
  5. #include <linux/ptrace.h>
  6. #include <linux/kdebug.h>
  7. #include <linux/bug.h>
  8. #include <linux/kgdb.h>
  9. #include <linux/irqflags.h>
  10. #include <linux/string.h>
  11. #include <asm/cacheflush.h>
  12. #include <asm/gdb_xml.h>
  13. #include <asm/insn.h>
  14. enum {
  15. NOT_KGDB_BREAK = 0,
  16. KGDB_SW_BREAK,
  17. KGDB_COMPILED_BREAK,
  18. KGDB_SW_SINGLE_STEP
  19. };
  20. static unsigned long stepped_address;
  21. static unsigned int stepped_opcode;
  22. static int decode_register_index(unsigned long opcode, int offset)
  23. {
  24. return (opcode >> offset) & 0x1F;
  25. }
  26. static int decode_register_index_short(unsigned long opcode, int offset)
  27. {
  28. return ((opcode >> offset) & 0x7) + 8;
  29. }
  30. /* Calculate the new address for after a step */
  31. static int get_step_address(struct pt_regs *regs, unsigned long *next_addr)
  32. {
  33. unsigned long pc = regs->epc;
  34. unsigned long *regs_ptr = (unsigned long *)regs;
  35. unsigned int rs1_num, rs2_num;
  36. int op_code;
  37. if (get_kernel_nofault(op_code, (void *)pc))
  38. return -EINVAL;
  39. if ((op_code & __INSN_LENGTH_MASK) != __INSN_LENGTH_GE_32) {
  40. if (riscv_insn_is_c_jalr(op_code) ||
  41. riscv_insn_is_c_jr(op_code)) {
  42. rs1_num = decode_register_index(op_code, RVC_C2_RS1_OPOFF);
  43. *next_addr = regs_ptr[rs1_num];
  44. } else if (riscv_insn_is_c_j(op_code) ||
  45. riscv_insn_is_c_jal(op_code)) {
  46. *next_addr = RVC_EXTRACT_JTYPE_IMM(op_code) + pc;
  47. } else if (riscv_insn_is_c_beqz(op_code)) {
  48. rs1_num = decode_register_index_short(op_code,
  49. RVC_C1_RS1_OPOFF);
  50. if (!rs1_num || regs_ptr[rs1_num] == 0)
  51. *next_addr = RVC_EXTRACT_BTYPE_IMM(op_code) + pc;
  52. else
  53. *next_addr = pc + 2;
  54. } else if (riscv_insn_is_c_bnez(op_code)) {
  55. rs1_num =
  56. decode_register_index_short(op_code, RVC_C1_RS1_OPOFF);
  57. if (rs1_num && regs_ptr[rs1_num] != 0)
  58. *next_addr = RVC_EXTRACT_BTYPE_IMM(op_code) + pc;
  59. else
  60. *next_addr = pc + 2;
  61. } else {
  62. *next_addr = pc + 2;
  63. }
  64. } else {
  65. if ((op_code & __INSN_OPCODE_MASK) == __INSN_BRANCH_OPCODE) {
  66. bool result = false;
  67. long imm = RV_EXTRACT_BTYPE_IMM(op_code);
  68. unsigned long rs1_val = 0, rs2_val = 0;
  69. rs1_num = decode_register_index(op_code, RVG_RS1_OPOFF);
  70. rs2_num = decode_register_index(op_code, RVG_RS2_OPOFF);
  71. if (rs1_num)
  72. rs1_val = regs_ptr[rs1_num];
  73. if (rs2_num)
  74. rs2_val = regs_ptr[rs2_num];
  75. if (riscv_insn_is_beq(op_code))
  76. result = (rs1_val == rs2_val) ? true : false;
  77. else if (riscv_insn_is_bne(op_code))
  78. result = (rs1_val != rs2_val) ? true : false;
  79. else if (riscv_insn_is_blt(op_code))
  80. result =
  81. ((long)rs1_val <
  82. (long)rs2_val) ? true : false;
  83. else if (riscv_insn_is_bge(op_code))
  84. result =
  85. ((long)rs1_val >=
  86. (long)rs2_val) ? true : false;
  87. else if (riscv_insn_is_bltu(op_code))
  88. result = (rs1_val < rs2_val) ? true : false;
  89. else if (riscv_insn_is_bgeu(op_code))
  90. result = (rs1_val >= rs2_val) ? true : false;
  91. if (result)
  92. *next_addr = imm + pc;
  93. else
  94. *next_addr = pc + 4;
  95. } else if (riscv_insn_is_jal(op_code)) {
  96. *next_addr = RV_EXTRACT_JTYPE_IMM(op_code) + pc;
  97. } else if (riscv_insn_is_jalr(op_code)) {
  98. rs1_num = decode_register_index(op_code, RVG_RS1_OPOFF);
  99. if (rs1_num)
  100. *next_addr = ((unsigned long *)regs)[rs1_num];
  101. *next_addr += RV_EXTRACT_ITYPE_IMM(op_code);
  102. } else if (riscv_insn_is_sret(op_code)) {
  103. *next_addr = pc;
  104. } else {
  105. *next_addr = pc + 4;
  106. }
  107. }
  108. return 0;
  109. }
  110. static int do_single_step(struct pt_regs *regs)
  111. {
  112. /* Determine where the target instruction will send us to */
  113. unsigned long addr = 0;
  114. int error = get_step_address(regs, &addr);
  115. if (error)
  116. return error;
  117. /* Store the op code in the stepped address */
  118. error = get_kernel_nofault(stepped_opcode, (void *)addr);
  119. if (error)
  120. return error;
  121. stepped_address = addr;
  122. /* Replace the op code with the break instruction */
  123. error = copy_to_kernel_nofault((void *)stepped_address,
  124. arch_kgdb_ops.gdb_bpt_instr,
  125. BREAK_INSTR_SIZE);
  126. /* Flush and return */
  127. if (!error) {
  128. flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
  129. kgdb_single_step = 1;
  130. atomic_set(&kgdb_cpu_doing_single_step,
  131. raw_smp_processor_id());
  132. } else {
  133. stepped_address = 0;
  134. stepped_opcode = 0;
  135. }
  136. return error;
  137. }
  138. /* Undo a single step */
  139. static void undo_single_step(struct pt_regs *regs)
  140. {
  141. if (stepped_opcode != 0) {
  142. copy_to_kernel_nofault((void *)stepped_address,
  143. (void *)&stepped_opcode, BREAK_INSTR_SIZE);
  144. flush_icache_range(stepped_address,
  145. stepped_address + BREAK_INSTR_SIZE);
  146. }
  147. stepped_address = 0;
  148. stepped_opcode = 0;
  149. kgdb_single_step = 0;
  150. atomic_set(&kgdb_cpu_doing_single_step, -1);
  151. }
  152. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  153. {DBG_REG_ZERO, GDB_SIZEOF_REG, -1},
  154. {DBG_REG_RA, GDB_SIZEOF_REG, offsetof(struct pt_regs, ra)},
  155. {DBG_REG_SP, GDB_SIZEOF_REG, offsetof(struct pt_regs, sp)},
  156. {DBG_REG_GP, GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
  157. {DBG_REG_TP, GDB_SIZEOF_REG, offsetof(struct pt_regs, tp)},
  158. {DBG_REG_T0, GDB_SIZEOF_REG, offsetof(struct pt_regs, t0)},
  159. {DBG_REG_T1, GDB_SIZEOF_REG, offsetof(struct pt_regs, t1)},
  160. {DBG_REG_T2, GDB_SIZEOF_REG, offsetof(struct pt_regs, t2)},
  161. {DBG_REG_FP, GDB_SIZEOF_REG, offsetof(struct pt_regs, s0)},
  162. {DBG_REG_S1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)},
  163. {DBG_REG_A0, GDB_SIZEOF_REG, offsetof(struct pt_regs, a0)},
  164. {DBG_REG_A1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)},
  165. {DBG_REG_A2, GDB_SIZEOF_REG, offsetof(struct pt_regs, a2)},
  166. {DBG_REG_A3, GDB_SIZEOF_REG, offsetof(struct pt_regs, a3)},
  167. {DBG_REG_A4, GDB_SIZEOF_REG, offsetof(struct pt_regs, a4)},
  168. {DBG_REG_A5, GDB_SIZEOF_REG, offsetof(struct pt_regs, a5)},
  169. {DBG_REG_A6, GDB_SIZEOF_REG, offsetof(struct pt_regs, a6)},
  170. {DBG_REG_A7, GDB_SIZEOF_REG, offsetof(struct pt_regs, a7)},
  171. {DBG_REG_S2, GDB_SIZEOF_REG, offsetof(struct pt_regs, s2)},
  172. {DBG_REG_S3, GDB_SIZEOF_REG, offsetof(struct pt_regs, s3)},
  173. {DBG_REG_S4, GDB_SIZEOF_REG, offsetof(struct pt_regs, s4)},
  174. {DBG_REG_S5, GDB_SIZEOF_REG, offsetof(struct pt_regs, s5)},
  175. {DBG_REG_S6, GDB_SIZEOF_REG, offsetof(struct pt_regs, s6)},
  176. {DBG_REG_S7, GDB_SIZEOF_REG, offsetof(struct pt_regs, s7)},
  177. {DBG_REG_S8, GDB_SIZEOF_REG, offsetof(struct pt_regs, s8)},
  178. {DBG_REG_S9, GDB_SIZEOF_REG, offsetof(struct pt_regs, s9)},
  179. {DBG_REG_S10, GDB_SIZEOF_REG, offsetof(struct pt_regs, s10)},
  180. {DBG_REG_S11, GDB_SIZEOF_REG, offsetof(struct pt_regs, s11)},
  181. {DBG_REG_T3, GDB_SIZEOF_REG, offsetof(struct pt_regs, t3)},
  182. {DBG_REG_T4, GDB_SIZEOF_REG, offsetof(struct pt_regs, t4)},
  183. {DBG_REG_T5, GDB_SIZEOF_REG, offsetof(struct pt_regs, t5)},
  184. {DBG_REG_T6, GDB_SIZEOF_REG, offsetof(struct pt_regs, t6)},
  185. {DBG_REG_EPC, GDB_SIZEOF_REG, offsetof(struct pt_regs, epc)},
  186. {DBG_REG_STATUS, GDB_SIZEOF_REG, offsetof(struct pt_regs, status)},
  187. {DBG_REG_BADADDR, GDB_SIZEOF_REG, offsetof(struct pt_regs, badaddr)},
  188. {DBG_REG_CAUSE, GDB_SIZEOF_REG, offsetof(struct pt_regs, cause)},
  189. };
  190. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  191. {
  192. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  193. return NULL;
  194. if (dbg_reg_def[regno].offset != -1)
  195. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  196. dbg_reg_def[regno].size);
  197. else
  198. memset(mem, 0, dbg_reg_def[regno].size);
  199. return dbg_reg_def[regno].name;
  200. }
  201. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  202. {
  203. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  204. return -EINVAL;
  205. if (dbg_reg_def[regno].offset != -1)
  206. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  207. dbg_reg_def[regno].size);
  208. return 0;
  209. }
  210. void
  211. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  212. {
  213. /* Initialize to zero */
  214. memset((char *)gdb_regs, 0, NUMREGBYTES);
  215. gdb_regs[DBG_REG_SP_OFF] = task->thread.sp;
  216. gdb_regs[DBG_REG_FP_OFF] = task->thread.s[0];
  217. gdb_regs[DBG_REG_S1_OFF] = task->thread.s[1];
  218. gdb_regs[DBG_REG_S2_OFF] = task->thread.s[2];
  219. gdb_regs[DBG_REG_S3_OFF] = task->thread.s[3];
  220. gdb_regs[DBG_REG_S4_OFF] = task->thread.s[4];
  221. gdb_regs[DBG_REG_S5_OFF] = task->thread.s[5];
  222. gdb_regs[DBG_REG_S6_OFF] = task->thread.s[6];
  223. gdb_regs[DBG_REG_S7_OFF] = task->thread.s[7];
  224. gdb_regs[DBG_REG_S8_OFF] = task->thread.s[8];
  225. gdb_regs[DBG_REG_S9_OFF] = task->thread.s[10];
  226. gdb_regs[DBG_REG_S10_OFF] = task->thread.s[11];
  227. gdb_regs[DBG_REG_EPC_OFF] = task->thread.ra;
  228. }
  229. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  230. {
  231. regs->epc = pc;
  232. }
  233. void kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
  234. char *remcom_out_buffer)
  235. {
  236. if (!strncmp(remcom_in_buffer, gdb_xfer_read_target,
  237. sizeof(gdb_xfer_read_target)))
  238. strcpy(remcom_out_buffer, riscv_gdb_stub_target_desc);
  239. else if (!strncmp(remcom_in_buffer, gdb_xfer_read_cpuxml,
  240. sizeof(gdb_xfer_read_cpuxml)))
  241. strcpy(remcom_out_buffer, riscv_gdb_stub_cpuxml);
  242. }
  243. static inline void kgdb_arch_update_addr(struct pt_regs *regs,
  244. char *remcom_in_buffer)
  245. {
  246. unsigned long addr;
  247. char *ptr;
  248. ptr = &remcom_in_buffer[1];
  249. if (kgdb_hex2long(&ptr, &addr))
  250. regs->epc = addr;
  251. }
  252. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  253. char *remcom_in_buffer, char *remcom_out_buffer,
  254. struct pt_regs *regs)
  255. {
  256. int err = 0;
  257. undo_single_step(regs);
  258. switch (remcom_in_buffer[0]) {
  259. case 'c':
  260. case 'D':
  261. case 'k':
  262. if (remcom_in_buffer[0] == 'c')
  263. kgdb_arch_update_addr(regs, remcom_in_buffer);
  264. break;
  265. case 's':
  266. kgdb_arch_update_addr(regs, remcom_in_buffer);
  267. err = do_single_step(regs);
  268. break;
  269. default:
  270. err = -1;
  271. }
  272. return err;
  273. }
  274. static int kgdb_riscv_kgdbbreak(unsigned long addr)
  275. {
  276. if (stepped_address == addr)
  277. return KGDB_SW_SINGLE_STEP;
  278. if (atomic_read(&kgdb_setting_breakpoint))
  279. if (addr == (unsigned long)&kgdb_compiled_break)
  280. return KGDB_COMPILED_BREAK;
  281. return kgdb_has_hit_break(addr);
  282. }
  283. static int kgdb_riscv_notify(struct notifier_block *self, unsigned long cmd,
  284. void *ptr)
  285. {
  286. struct die_args *args = (struct die_args *)ptr;
  287. struct pt_regs *regs = args->regs;
  288. unsigned long flags;
  289. int type;
  290. if (user_mode(regs))
  291. return NOTIFY_DONE;
  292. type = kgdb_riscv_kgdbbreak(regs->epc);
  293. if (type == NOT_KGDB_BREAK && cmd == DIE_TRAP)
  294. return NOTIFY_DONE;
  295. local_irq_save(flags);
  296. if (kgdb_handle_exception(type == KGDB_SW_SINGLE_STEP ? 0 : 1,
  297. args->signr, cmd, regs))
  298. return NOTIFY_DONE;
  299. if (type == KGDB_COMPILED_BREAK)
  300. regs->epc += 4;
  301. local_irq_restore(flags);
  302. return NOTIFY_STOP;
  303. }
  304. static struct notifier_block kgdb_notifier = {
  305. .notifier_call = kgdb_riscv_notify,
  306. };
  307. int kgdb_arch_init(void)
  308. {
  309. register_die_notifier(&kgdb_notifier);
  310. return 0;
  311. }
  312. void kgdb_arch_exit(void)
  313. {
  314. unregister_die_notifier(&kgdb_notifier);
  315. }
  316. /*
  317. * Global data
  318. */
  319. #ifdef CONFIG_RISCV_ISA_C
  320. const struct kgdb_arch arch_kgdb_ops = {
  321. .gdb_bpt_instr = {0x02, 0x90}, /* c.ebreak */
  322. };
  323. #else
  324. const struct kgdb_arch arch_kgdb_ops = {
  325. .gdb_bpt_instr = {0x73, 0x00, 0x10, 0x00}, /* ebreak */
  326. };
  327. #endif