kgdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/parse_asm.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. #if __riscv_xlen == 32
  23. /* C.JAL is an RV32C-only instruction */
  24. DECLARE_INSN(c_jal, MATCH_C_JAL, MASK_C_JAL)
  25. #else
  26. #define is_c_jal_insn(opcode) 0
  27. #endif
  28. DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
  29. DECLARE_INSN(jal, MATCH_JAL, MASK_JAL)
  30. DECLARE_INSN(c_jr, MATCH_C_JR, MASK_C_JR)
  31. DECLARE_INSN(c_jalr, MATCH_C_JALR, MASK_C_JALR)
  32. DECLARE_INSN(c_j, MATCH_C_J, MASK_C_J)
  33. DECLARE_INSN(beq, MATCH_BEQ, MASK_BEQ)
  34. DECLARE_INSN(bne, MATCH_BNE, MASK_BNE)
  35. DECLARE_INSN(blt, MATCH_BLT, MASK_BLT)
  36. DECLARE_INSN(bge, MATCH_BGE, MASK_BGE)
  37. DECLARE_INSN(bltu, MATCH_BLTU, MASK_BLTU)
  38. DECLARE_INSN(bgeu, MATCH_BGEU, MASK_BGEU)
  39. DECLARE_INSN(c_beqz, MATCH_C_BEQZ, MASK_C_BEQZ)
  40. DECLARE_INSN(c_bnez, MATCH_C_BNEZ, MASK_C_BNEZ)
  41. DECLARE_INSN(sret, MATCH_SRET, MASK_SRET)
  42. static int decode_register_index(unsigned long opcode, int offset)
  43. {
  44. return (opcode >> offset) & 0x1F;
  45. }
  46. static int decode_register_index_short(unsigned long opcode, int offset)
  47. {
  48. return ((opcode >> offset) & 0x7) + 8;
  49. }
  50. /* Calculate the new address for after a step */
  51. static int get_step_address(struct pt_regs *regs, unsigned long *next_addr)
  52. {
  53. unsigned long pc = regs->epc;
  54. unsigned long *regs_ptr = (unsigned long *)regs;
  55. unsigned int rs1_num, rs2_num;
  56. int op_code;
  57. if (get_kernel_nofault(op_code, (void *)pc))
  58. return -EINVAL;
  59. if ((op_code & __INSN_LENGTH_MASK) != __INSN_LENGTH_GE_32) {
  60. if (is_c_jalr_insn(op_code) || is_c_jr_insn(op_code)) {
  61. rs1_num = decode_register_index(op_code, RVC_C2_RS1_OPOFF);
  62. *next_addr = regs_ptr[rs1_num];
  63. } else if (is_c_j_insn(op_code) || is_c_jal_insn(op_code)) {
  64. *next_addr = EXTRACT_RVC_J_IMM(op_code) + pc;
  65. } else if (is_c_beqz_insn(op_code)) {
  66. rs1_num = decode_register_index_short(op_code,
  67. RVC_C1_RS1_OPOFF);
  68. if (!rs1_num || regs_ptr[rs1_num] == 0)
  69. *next_addr = EXTRACT_RVC_B_IMM(op_code) + pc;
  70. else
  71. *next_addr = pc + 2;
  72. } else if (is_c_bnez_insn(op_code)) {
  73. rs1_num =
  74. decode_register_index_short(op_code, RVC_C1_RS1_OPOFF);
  75. if (rs1_num && regs_ptr[rs1_num] != 0)
  76. *next_addr = EXTRACT_RVC_B_IMM(op_code) + pc;
  77. else
  78. *next_addr = pc + 2;
  79. } else {
  80. *next_addr = pc + 2;
  81. }
  82. } else {
  83. if ((op_code & __INSN_OPCODE_MASK) == __INSN_BRANCH_OPCODE) {
  84. bool result = false;
  85. long imm = EXTRACT_BTYPE_IMM(op_code);
  86. unsigned long rs1_val = 0, rs2_val = 0;
  87. rs1_num = decode_register_index(op_code, RVG_RS1_OPOFF);
  88. rs2_num = decode_register_index(op_code, RVG_RS2_OPOFF);
  89. if (rs1_num)
  90. rs1_val = regs_ptr[rs1_num];
  91. if (rs2_num)
  92. rs2_val = regs_ptr[rs2_num];
  93. if (is_beq_insn(op_code))
  94. result = (rs1_val == rs2_val) ? true : false;
  95. else if (is_bne_insn(op_code))
  96. result = (rs1_val != rs2_val) ? true : false;
  97. else if (is_blt_insn(op_code))
  98. result =
  99. ((long)rs1_val <
  100. (long)rs2_val) ? true : false;
  101. else if (is_bge_insn(op_code))
  102. result =
  103. ((long)rs1_val >=
  104. (long)rs2_val) ? true : false;
  105. else if (is_bltu_insn(op_code))
  106. result = (rs1_val < rs2_val) ? true : false;
  107. else if (is_bgeu_insn(op_code))
  108. result = (rs1_val >= rs2_val) ? true : false;
  109. if (result)
  110. *next_addr = imm + pc;
  111. else
  112. *next_addr = pc + 4;
  113. } else if (is_jal_insn(op_code)) {
  114. *next_addr = EXTRACT_JTYPE_IMM(op_code) + pc;
  115. } else if (is_jalr_insn(op_code)) {
  116. rs1_num = decode_register_index(op_code, RVG_RS1_OPOFF);
  117. if (rs1_num)
  118. *next_addr = ((unsigned long *)regs)[rs1_num];
  119. *next_addr += EXTRACT_ITYPE_IMM(op_code);
  120. } else if (is_sret_insn(op_code)) {
  121. *next_addr = pc;
  122. } else {
  123. *next_addr = pc + 4;
  124. }
  125. }
  126. return 0;
  127. }
  128. static int do_single_step(struct pt_regs *regs)
  129. {
  130. /* Determine where the target instruction will send us to */
  131. unsigned long addr = 0;
  132. int error = get_step_address(regs, &addr);
  133. if (error)
  134. return error;
  135. /* Store the op code in the stepped address */
  136. error = get_kernel_nofault(stepped_opcode, (void *)addr);
  137. if (error)
  138. return error;
  139. stepped_address = addr;
  140. /* Replace the op code with the break instruction */
  141. error = copy_to_kernel_nofault((void *)stepped_address,
  142. arch_kgdb_ops.gdb_bpt_instr,
  143. BREAK_INSTR_SIZE);
  144. /* Flush and return */
  145. if (!error) {
  146. flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
  147. kgdb_single_step = 1;
  148. atomic_set(&kgdb_cpu_doing_single_step,
  149. raw_smp_processor_id());
  150. } else {
  151. stepped_address = 0;
  152. stepped_opcode = 0;
  153. }
  154. return error;
  155. }
  156. /* Undo a single step */
  157. static void undo_single_step(struct pt_regs *regs)
  158. {
  159. if (stepped_opcode != 0) {
  160. copy_to_kernel_nofault((void *)stepped_address,
  161. (void *)&stepped_opcode, BREAK_INSTR_SIZE);
  162. flush_icache_range(stepped_address,
  163. stepped_address + BREAK_INSTR_SIZE);
  164. }
  165. stepped_address = 0;
  166. stepped_opcode = 0;
  167. kgdb_single_step = 0;
  168. atomic_set(&kgdb_cpu_doing_single_step, -1);
  169. }
  170. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  171. {DBG_REG_ZERO, GDB_SIZEOF_REG, -1},
  172. {DBG_REG_RA, GDB_SIZEOF_REG, offsetof(struct pt_regs, ra)},
  173. {DBG_REG_SP, GDB_SIZEOF_REG, offsetof(struct pt_regs, sp)},
  174. {DBG_REG_GP, GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
  175. {DBG_REG_TP, GDB_SIZEOF_REG, offsetof(struct pt_regs, tp)},
  176. {DBG_REG_T0, GDB_SIZEOF_REG, offsetof(struct pt_regs, t0)},
  177. {DBG_REG_T1, GDB_SIZEOF_REG, offsetof(struct pt_regs, t1)},
  178. {DBG_REG_T2, GDB_SIZEOF_REG, offsetof(struct pt_regs, t2)},
  179. {DBG_REG_FP, GDB_SIZEOF_REG, offsetof(struct pt_regs, s0)},
  180. {DBG_REG_S1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)},
  181. {DBG_REG_A0, GDB_SIZEOF_REG, offsetof(struct pt_regs, a0)},
  182. {DBG_REG_A1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)},
  183. {DBG_REG_A2, GDB_SIZEOF_REG, offsetof(struct pt_regs, a2)},
  184. {DBG_REG_A3, GDB_SIZEOF_REG, offsetof(struct pt_regs, a3)},
  185. {DBG_REG_A4, GDB_SIZEOF_REG, offsetof(struct pt_regs, a4)},
  186. {DBG_REG_A5, GDB_SIZEOF_REG, offsetof(struct pt_regs, a5)},
  187. {DBG_REG_A6, GDB_SIZEOF_REG, offsetof(struct pt_regs, a6)},
  188. {DBG_REG_A7, GDB_SIZEOF_REG, offsetof(struct pt_regs, a7)},
  189. {DBG_REG_S2, GDB_SIZEOF_REG, offsetof(struct pt_regs, s2)},
  190. {DBG_REG_S3, GDB_SIZEOF_REG, offsetof(struct pt_regs, s3)},
  191. {DBG_REG_S4, GDB_SIZEOF_REG, offsetof(struct pt_regs, s4)},
  192. {DBG_REG_S5, GDB_SIZEOF_REG, offsetof(struct pt_regs, s5)},
  193. {DBG_REG_S6, GDB_SIZEOF_REG, offsetof(struct pt_regs, s6)},
  194. {DBG_REG_S7, GDB_SIZEOF_REG, offsetof(struct pt_regs, s7)},
  195. {DBG_REG_S8, GDB_SIZEOF_REG, offsetof(struct pt_regs, s8)},
  196. {DBG_REG_S9, GDB_SIZEOF_REG, offsetof(struct pt_regs, s9)},
  197. {DBG_REG_S10, GDB_SIZEOF_REG, offsetof(struct pt_regs, s10)},
  198. {DBG_REG_S11, GDB_SIZEOF_REG, offsetof(struct pt_regs, s11)},
  199. {DBG_REG_T3, GDB_SIZEOF_REG, offsetof(struct pt_regs, t3)},
  200. {DBG_REG_T4, GDB_SIZEOF_REG, offsetof(struct pt_regs, t4)},
  201. {DBG_REG_T5, GDB_SIZEOF_REG, offsetof(struct pt_regs, t5)},
  202. {DBG_REG_T6, GDB_SIZEOF_REG, offsetof(struct pt_regs, t6)},
  203. {DBG_REG_EPC, GDB_SIZEOF_REG, offsetof(struct pt_regs, epc)},
  204. {DBG_REG_STATUS, GDB_SIZEOF_REG, offsetof(struct pt_regs, status)},
  205. {DBG_REG_BADADDR, GDB_SIZEOF_REG, offsetof(struct pt_regs, badaddr)},
  206. {DBG_REG_CAUSE, GDB_SIZEOF_REG, offsetof(struct pt_regs, cause)},
  207. };
  208. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  209. {
  210. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  211. return NULL;
  212. if (dbg_reg_def[regno].offset != -1)
  213. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  214. dbg_reg_def[regno].size);
  215. else
  216. memset(mem, 0, dbg_reg_def[regno].size);
  217. return dbg_reg_def[regno].name;
  218. }
  219. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  220. {
  221. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  222. return -EINVAL;
  223. if (dbg_reg_def[regno].offset != -1)
  224. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  225. dbg_reg_def[regno].size);
  226. return 0;
  227. }
  228. void
  229. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  230. {
  231. /* Initialize to zero */
  232. memset((char *)gdb_regs, 0, NUMREGBYTES);
  233. gdb_regs[DBG_REG_SP_OFF] = task->thread.sp;
  234. gdb_regs[DBG_REG_FP_OFF] = task->thread.s[0];
  235. gdb_regs[DBG_REG_S1_OFF] = task->thread.s[1];
  236. gdb_regs[DBG_REG_S2_OFF] = task->thread.s[2];
  237. gdb_regs[DBG_REG_S3_OFF] = task->thread.s[3];
  238. gdb_regs[DBG_REG_S4_OFF] = task->thread.s[4];
  239. gdb_regs[DBG_REG_S5_OFF] = task->thread.s[5];
  240. gdb_regs[DBG_REG_S6_OFF] = task->thread.s[6];
  241. gdb_regs[DBG_REG_S7_OFF] = task->thread.s[7];
  242. gdb_regs[DBG_REG_S8_OFF] = task->thread.s[8];
  243. gdb_regs[DBG_REG_S9_OFF] = task->thread.s[10];
  244. gdb_regs[DBG_REG_S10_OFF] = task->thread.s[11];
  245. gdb_regs[DBG_REG_EPC_OFF] = task->thread.ra;
  246. }
  247. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  248. {
  249. regs->epc = pc;
  250. }
  251. void kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
  252. char *remcom_out_buffer)
  253. {
  254. if (!strncmp(remcom_in_buffer, gdb_xfer_read_target,
  255. sizeof(gdb_xfer_read_target)))
  256. strcpy(remcom_out_buffer, riscv_gdb_stub_target_desc);
  257. else if (!strncmp(remcom_in_buffer, gdb_xfer_read_cpuxml,
  258. sizeof(gdb_xfer_read_cpuxml)))
  259. strcpy(remcom_out_buffer, riscv_gdb_stub_cpuxml);
  260. }
  261. static inline void kgdb_arch_update_addr(struct pt_regs *regs,
  262. char *remcom_in_buffer)
  263. {
  264. unsigned long addr;
  265. char *ptr;
  266. ptr = &remcom_in_buffer[1];
  267. if (kgdb_hex2long(&ptr, &addr))
  268. regs->epc = addr;
  269. }
  270. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  271. char *remcom_in_buffer, char *remcom_out_buffer,
  272. struct pt_regs *regs)
  273. {
  274. int err = 0;
  275. undo_single_step(regs);
  276. switch (remcom_in_buffer[0]) {
  277. case 'c':
  278. case 'D':
  279. case 'k':
  280. if (remcom_in_buffer[0] == 'c')
  281. kgdb_arch_update_addr(regs, remcom_in_buffer);
  282. break;
  283. case 's':
  284. kgdb_arch_update_addr(regs, remcom_in_buffer);
  285. err = do_single_step(regs);
  286. break;
  287. default:
  288. err = -1;
  289. }
  290. return err;
  291. }
  292. static int kgdb_riscv_kgdbbreak(unsigned long addr)
  293. {
  294. if (stepped_address == addr)
  295. return KGDB_SW_SINGLE_STEP;
  296. if (atomic_read(&kgdb_setting_breakpoint))
  297. if (addr == (unsigned long)&kgdb_compiled_break)
  298. return KGDB_COMPILED_BREAK;
  299. return kgdb_has_hit_break(addr);
  300. }
  301. static int kgdb_riscv_notify(struct notifier_block *self, unsigned long cmd,
  302. void *ptr)
  303. {
  304. struct die_args *args = (struct die_args *)ptr;
  305. struct pt_regs *regs = args->regs;
  306. unsigned long flags;
  307. int type;
  308. if (user_mode(regs))
  309. return NOTIFY_DONE;
  310. type = kgdb_riscv_kgdbbreak(regs->epc);
  311. if (type == NOT_KGDB_BREAK && cmd == DIE_TRAP)
  312. return NOTIFY_DONE;
  313. local_irq_save(flags);
  314. if (kgdb_handle_exception(type == KGDB_SW_SINGLE_STEP ? 0 : 1,
  315. args->signr, cmd, regs))
  316. return NOTIFY_DONE;
  317. if (type == KGDB_COMPILED_BREAK)
  318. regs->epc += 4;
  319. local_irq_restore(flags);
  320. return NOTIFY_STOP;
  321. }
  322. static struct notifier_block kgdb_notifier = {
  323. .notifier_call = kgdb_riscv_notify,
  324. };
  325. int kgdb_arch_init(void)
  326. {
  327. register_die_notifier(&kgdb_notifier);
  328. return 0;
  329. }
  330. void kgdb_arch_exit(void)
  331. {
  332. unregister_die_notifier(&kgdb_notifier);
  333. }
  334. /*
  335. * Global data
  336. */
  337. #ifdef CONFIG_RISCV_ISA_C
  338. const struct kgdb_arch arch_kgdb_ops = {
  339. .gdb_bpt_instr = {0x02, 0x90}, /* c.ebreak */
  340. };
  341. #else
  342. const struct kgdb_arch arch_kgdb_ops = {
  343. .gdb_bpt_instr = {0x73, 0x00, 0x10, 0x00}, /* ebreak */
  344. };
  345. #endif