kgdb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * LoongArch KGDB support
  4. *
  5. * Copyright (C) 2023 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/hw_breakpoint.h>
  8. #include <linux/kdebug.h>
  9. #include <linux/kgdb.h>
  10. #include <linux/objtool.h>
  11. #include <linux/processor.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/fpu.h>
  17. #include <asm/hw_breakpoint.h>
  18. #include <asm/inst.h>
  19. #include <asm/irq_regs.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/sigcontext.h>
  22. int kgdb_watch_activated;
  23. static unsigned int stepped_opcode;
  24. static unsigned long stepped_address;
  25. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  26. { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
  27. { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
  28. { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
  29. { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
  30. { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
  31. { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
  32. { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
  33. { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
  34. { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
  35. { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
  36. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
  37. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
  38. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
  39. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
  40. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
  41. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
  42. { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[16]) },
  43. { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[17]) },
  44. { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[18]) },
  45. { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[19]) },
  46. { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[20]) },
  47. { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[21]) },
  48. { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[22]) },
  49. { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[23]) },
  50. { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[24]) },
  51. { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[25]) },
  52. { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[26]) },
  53. { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[27]) },
  54. { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[28]) },
  55. { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[29]) },
  56. { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[30]) },
  57. { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[31]) },
  58. { "orig_a0", GDB_SIZEOF_REG, offsetof(struct pt_regs, orig_a0) },
  59. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, csr_era) },
  60. { "badv", GDB_SIZEOF_REG, offsetof(struct pt_regs, csr_badvaddr) },
  61. { "f0", GDB_SIZEOF_REG, 0 },
  62. { "f1", GDB_SIZEOF_REG, 1 },
  63. { "f2", GDB_SIZEOF_REG, 2 },
  64. { "f3", GDB_SIZEOF_REG, 3 },
  65. { "f4", GDB_SIZEOF_REG, 4 },
  66. { "f5", GDB_SIZEOF_REG, 5 },
  67. { "f6", GDB_SIZEOF_REG, 6 },
  68. { "f7", GDB_SIZEOF_REG, 7 },
  69. { "f8", GDB_SIZEOF_REG, 8 },
  70. { "f9", GDB_SIZEOF_REG, 9 },
  71. { "f10", GDB_SIZEOF_REG, 10 },
  72. { "f11", GDB_SIZEOF_REG, 11 },
  73. { "f12", GDB_SIZEOF_REG, 12 },
  74. { "f13", GDB_SIZEOF_REG, 13 },
  75. { "f14", GDB_SIZEOF_REG, 14 },
  76. { "f15", GDB_SIZEOF_REG, 15 },
  77. { "f16", GDB_SIZEOF_REG, 16 },
  78. { "f17", GDB_SIZEOF_REG, 17 },
  79. { "f18", GDB_SIZEOF_REG, 18 },
  80. { "f19", GDB_SIZEOF_REG, 19 },
  81. { "f20", GDB_SIZEOF_REG, 20 },
  82. { "f21", GDB_SIZEOF_REG, 21 },
  83. { "f22", GDB_SIZEOF_REG, 22 },
  84. { "f23", GDB_SIZEOF_REG, 23 },
  85. { "f24", GDB_SIZEOF_REG, 24 },
  86. { "f25", GDB_SIZEOF_REG, 25 },
  87. { "f26", GDB_SIZEOF_REG, 26 },
  88. { "f27", GDB_SIZEOF_REG, 27 },
  89. { "f28", GDB_SIZEOF_REG, 28 },
  90. { "f29", GDB_SIZEOF_REG, 29 },
  91. { "f30", GDB_SIZEOF_REG, 30 },
  92. { "f31", GDB_SIZEOF_REG, 31 },
  93. { "fcc0", 1, 0 },
  94. { "fcc1", 1, 1 },
  95. { "fcc2", 1, 2 },
  96. { "fcc3", 1, 3 },
  97. { "fcc4", 1, 4 },
  98. { "fcc5", 1, 5 },
  99. { "fcc6", 1, 6 },
  100. { "fcc7", 1, 7 },
  101. { "fcsr", 4, 0 },
  102. };
  103. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  104. {
  105. int reg_offset, reg_size;
  106. if (regno < 0 || regno >= DBG_MAX_REG_NUM)
  107. return NULL;
  108. reg_offset = dbg_reg_def[regno].offset;
  109. reg_size = dbg_reg_def[regno].size;
  110. if (reg_offset == -1)
  111. goto out;
  112. /* Handle general-purpose/orig_a0/pc/badv registers */
  113. if (regno <= DBG_PT_REGS_END) {
  114. memcpy(mem, (void *)regs + reg_offset, reg_size);
  115. goto out;
  116. }
  117. if (!(regs->csr_euen & CSR_EUEN_FPEN))
  118. goto out;
  119. save_fp(current);
  120. /* Handle FP registers */
  121. switch (regno) {
  122. case DBG_FCSR: /* Process the fcsr */
  123. memcpy(mem, (void *)&current->thread.fpu.fcsr, reg_size);
  124. break;
  125. case DBG_FCC_BASE ... DBG_FCC_END: /* Process the fcc */
  126. memcpy(mem, (void *)&current->thread.fpu.fcc + reg_offset, reg_size);
  127. break;
  128. case DBG_FPR_BASE ... DBG_FPR_END: /* Process the fpr */
  129. memcpy(mem, (void *)&current->thread.fpu.fpr[reg_offset], reg_size);
  130. break;
  131. default:
  132. break;
  133. }
  134. out:
  135. return dbg_reg_def[regno].name;
  136. }
  137. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  138. {
  139. int reg_offset, reg_size;
  140. if (regno < 0 || regno >= DBG_MAX_REG_NUM)
  141. return -EINVAL;
  142. reg_offset = dbg_reg_def[regno].offset;
  143. reg_size = dbg_reg_def[regno].size;
  144. if (reg_offset == -1)
  145. return 0;
  146. /* Handle general-purpose/orig_a0/pc/badv registers */
  147. if (regno <= DBG_PT_REGS_END) {
  148. memcpy((void *)regs + reg_offset, mem, reg_size);
  149. return 0;
  150. }
  151. if (!(regs->csr_euen & CSR_EUEN_FPEN))
  152. return 0;
  153. /* Handle FP registers */
  154. switch (regno) {
  155. case DBG_FCSR: /* Process the fcsr */
  156. memcpy((void *)&current->thread.fpu.fcsr, mem, reg_size);
  157. break;
  158. case DBG_FCC_BASE ... DBG_FCC_END: /* Process the fcc */
  159. memcpy((void *)&current->thread.fpu.fcc + reg_offset, mem, reg_size);
  160. break;
  161. case DBG_FPR_BASE ... DBG_FPR_END: /* Process the fpr */
  162. memcpy((void *)&current->thread.fpu.fpr[reg_offset], mem, reg_size);
  163. break;
  164. default:
  165. break;
  166. }
  167. restore_fp(current);
  168. return 0;
  169. }
  170. /*
  171. * Similar to regs_to_gdb_regs() except that process is sleeping and so
  172. * we may not be able to get all the info.
  173. */
  174. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  175. {
  176. /* Initialize to zero */
  177. memset((char *)gdb_regs, 0, NUMREGBYTES);
  178. gdb_regs[DBG_LOONGARCH_RA] = p->thread.reg01;
  179. gdb_regs[DBG_LOONGARCH_TP] = (long)p;
  180. gdb_regs[DBG_LOONGARCH_SP] = p->thread.reg03;
  181. /* S0 - S8 */
  182. gdb_regs[DBG_LOONGARCH_S0] = p->thread.reg23;
  183. gdb_regs[DBG_LOONGARCH_S1] = p->thread.reg24;
  184. gdb_regs[DBG_LOONGARCH_S2] = p->thread.reg25;
  185. gdb_regs[DBG_LOONGARCH_S3] = p->thread.reg26;
  186. gdb_regs[DBG_LOONGARCH_S4] = p->thread.reg27;
  187. gdb_regs[DBG_LOONGARCH_S5] = p->thread.reg28;
  188. gdb_regs[DBG_LOONGARCH_S6] = p->thread.reg29;
  189. gdb_regs[DBG_LOONGARCH_S7] = p->thread.reg30;
  190. gdb_regs[DBG_LOONGARCH_S8] = p->thread.reg31;
  191. /*
  192. * PC use return address (RA), i.e. the moment after return from __switch_to()
  193. */
  194. gdb_regs[DBG_LOONGARCH_PC] = p->thread.reg01;
  195. }
  196. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  197. {
  198. regs->csr_era = pc;
  199. }
  200. noinline void arch_kgdb_breakpoint(void)
  201. {
  202. __asm__ __volatile__ ( \
  203. ".globl kgdb_breakinst\n\t" \
  204. "kgdb_breakinst:\tbreak 2\n\t"); /* BRK_KDB = 2 */
  205. }
  206. STACK_FRAME_NON_STANDARD(arch_kgdb_breakpoint);
  207. /*
  208. * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
  209. * then try to fall into the debugger
  210. */
  211. static int kgdb_loongarch_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  212. {
  213. struct die_args *args = (struct die_args *)ptr;
  214. struct pt_regs *regs = args->regs;
  215. /* Userspace events, ignore. */
  216. if (user_mode(regs))
  217. return NOTIFY_DONE;
  218. if (!kgdb_io_module_registered)
  219. return NOTIFY_DONE;
  220. if (atomic_read(&kgdb_active) != -1)
  221. kgdb_nmicallback(smp_processor_id(), regs);
  222. if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
  223. return NOTIFY_DONE;
  224. if (atomic_read(&kgdb_setting_breakpoint))
  225. if (regs->csr_era == (unsigned long)&kgdb_breakinst)
  226. regs->csr_era += LOONGARCH_INSN_SIZE;
  227. return NOTIFY_STOP;
  228. }
  229. bool kgdb_breakpoint_handler(struct pt_regs *regs)
  230. {
  231. struct die_args args = {
  232. .regs = regs,
  233. .str = "Break",
  234. .err = BRK_KDB,
  235. .trapnr = read_csr_excode(),
  236. .signr = SIGTRAP,
  237. };
  238. return (kgdb_loongarch_notify(NULL, DIE_TRAP, &args) == NOTIFY_STOP) ? true : false;
  239. }
  240. static struct notifier_block kgdb_notifier = {
  241. .notifier_call = kgdb_loongarch_notify,
  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->csr_era = addr;
  251. }
  252. /* Calculate the new address for after a step */
  253. static int get_step_address(struct pt_regs *regs, unsigned long *next_addr)
  254. {
  255. char cj_val;
  256. unsigned int si, si_l, si_h, rd, rj, cj;
  257. unsigned long pc = instruction_pointer(regs);
  258. union loongarch_instruction *ip = (union loongarch_instruction *)pc;
  259. if (pc & 3) {
  260. pr_warn("%s: invalid pc 0x%lx\n", __func__, pc);
  261. return -EINVAL;
  262. }
  263. *next_addr = pc + LOONGARCH_INSN_SIZE;
  264. si_h = ip->reg0i26_format.immediate_h;
  265. si_l = ip->reg0i26_format.immediate_l;
  266. switch (ip->reg0i26_format.opcode) {
  267. case b_op:
  268. *next_addr = pc + sign_extend64((si_h << 16 | si_l) << 2, 27);
  269. return 0;
  270. case bl_op:
  271. *next_addr = pc + sign_extend64((si_h << 16 | si_l) << 2, 27);
  272. regs->regs[1] = pc + LOONGARCH_INSN_SIZE;
  273. return 0;
  274. }
  275. rj = ip->reg1i21_format.rj;
  276. cj = (rj & 0x07) + DBG_FCC_BASE;
  277. si_l = ip->reg1i21_format.immediate_l;
  278. si_h = ip->reg1i21_format.immediate_h;
  279. dbg_get_reg(cj, &cj_val, regs);
  280. switch (ip->reg1i21_format.opcode) {
  281. case beqz_op:
  282. if (regs->regs[rj] == 0)
  283. *next_addr = pc + sign_extend64((si_h << 16 | si_l) << 2, 22);
  284. return 0;
  285. case bnez_op:
  286. if (regs->regs[rj] != 0)
  287. *next_addr = pc + sign_extend64((si_h << 16 | si_l) << 2, 22);
  288. return 0;
  289. case bceqz_op: /* bceqz_op = bcnez_op */
  290. if (((rj & 0x18) == 0x00) && !cj_val) /* bceqz */
  291. *next_addr = pc + sign_extend64((si_h << 16 | si_l) << 2, 22);
  292. if (((rj & 0x18) == 0x08) && cj_val) /* bcnez */
  293. *next_addr = pc + sign_extend64((si_h << 16 | si_l) << 2, 22);
  294. return 0;
  295. }
  296. rj = ip->reg2i16_format.rj;
  297. rd = ip->reg2i16_format.rd;
  298. si = ip->reg2i16_format.immediate;
  299. switch (ip->reg2i16_format.opcode) {
  300. case beq_op:
  301. if (regs->regs[rj] == regs->regs[rd])
  302. *next_addr = pc + sign_extend64(si << 2, 17);
  303. return 0;
  304. case bne_op:
  305. if (regs->regs[rj] != regs->regs[rd])
  306. *next_addr = pc + sign_extend64(si << 2, 17);
  307. return 0;
  308. case blt_op:
  309. if ((long)regs->regs[rj] < (long)regs->regs[rd])
  310. *next_addr = pc + sign_extend64(si << 2, 17);
  311. return 0;
  312. case bge_op:
  313. if ((long)regs->regs[rj] >= (long)regs->regs[rd])
  314. *next_addr = pc + sign_extend64(si << 2, 17);
  315. return 0;
  316. case bltu_op:
  317. if (regs->regs[rj] < regs->regs[rd])
  318. *next_addr = pc + sign_extend64(si << 2, 17);
  319. return 0;
  320. case bgeu_op:
  321. if (regs->regs[rj] >= regs->regs[rd])
  322. *next_addr = pc + sign_extend64(si << 2, 17);
  323. return 0;
  324. case jirl_op:
  325. regs->regs[rd] = pc + LOONGARCH_INSN_SIZE;
  326. *next_addr = regs->regs[rj] + sign_extend64(si << 2, 17);
  327. return 0;
  328. }
  329. return 0;
  330. }
  331. static int do_single_step(struct pt_regs *regs)
  332. {
  333. int error = 0;
  334. unsigned long addr = 0; /* Determine where the target instruction will send us to */
  335. error = get_step_address(regs, &addr);
  336. if (error)
  337. return error;
  338. /* Store the opcode in the stepped address */
  339. error = get_kernel_nofault(stepped_opcode, (void *)addr);
  340. if (error)
  341. return error;
  342. stepped_address = addr;
  343. /* Replace the opcode with the break instruction */
  344. error = copy_to_kernel_nofault((void *)stepped_address,
  345. arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
  346. flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
  347. if (error) {
  348. stepped_opcode = 0;
  349. stepped_address = 0;
  350. } else {
  351. kgdb_single_step = 1;
  352. atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
  353. }
  354. return error;
  355. }
  356. /* Undo a single step */
  357. static void undo_single_step(struct pt_regs *regs)
  358. {
  359. if (stepped_opcode) {
  360. copy_to_kernel_nofault((void *)stepped_address,
  361. (void *)&stepped_opcode, BREAK_INSTR_SIZE);
  362. flush_icache_range(stepped_address, stepped_address + BREAK_INSTR_SIZE);
  363. }
  364. stepped_opcode = 0;
  365. stepped_address = 0;
  366. kgdb_single_step = 0;
  367. atomic_set(&kgdb_cpu_doing_single_step, -1);
  368. }
  369. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  370. char *remcom_in_buffer, char *remcom_out_buffer,
  371. struct pt_regs *regs)
  372. {
  373. int ret = 0;
  374. undo_single_step(regs);
  375. regs->csr_prmd |= CSR_PRMD_PWE;
  376. switch (remcom_in_buffer[0]) {
  377. case 'D':
  378. case 'k':
  379. regs->csr_prmd &= ~CSR_PRMD_PWE;
  380. fallthrough;
  381. case 'c':
  382. kgdb_arch_update_addr(regs, remcom_in_buffer);
  383. break;
  384. case 's':
  385. kgdb_arch_update_addr(regs, remcom_in_buffer);
  386. ret = do_single_step(regs);
  387. break;
  388. default:
  389. ret = -1;
  390. }
  391. return ret;
  392. }
  393. static struct hw_breakpoint {
  394. unsigned int enabled;
  395. unsigned long addr;
  396. int len;
  397. int type;
  398. struct perf_event * __percpu *pev;
  399. } breakinfo[LOONGARCH_MAX_BRP];
  400. static int hw_break_reserve_slot(int breakno)
  401. {
  402. int cpu, cnt = 0;
  403. struct perf_event **pevent;
  404. for_each_online_cpu(cpu) {
  405. cnt++;
  406. pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
  407. if (dbg_reserve_bp_slot(*pevent))
  408. goto fail;
  409. }
  410. return 0;
  411. fail:
  412. for_each_online_cpu(cpu) {
  413. cnt--;
  414. if (!cnt)
  415. break;
  416. pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
  417. dbg_release_bp_slot(*pevent);
  418. }
  419. return -1;
  420. }
  421. static int hw_break_release_slot(int breakno)
  422. {
  423. int cpu;
  424. struct perf_event **pevent;
  425. if (dbg_is_early)
  426. return 0;
  427. for_each_online_cpu(cpu) {
  428. pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
  429. if (dbg_release_bp_slot(*pevent))
  430. /*
  431. * The debugger is responsible for handing the retry on
  432. * remove failure.
  433. */
  434. return -1;
  435. }
  436. return 0;
  437. }
  438. static int kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  439. {
  440. int i;
  441. for (i = 0; i < LOONGARCH_MAX_BRP; i++)
  442. if (!breakinfo[i].enabled)
  443. break;
  444. if (i == LOONGARCH_MAX_BRP)
  445. return -1;
  446. switch (bptype) {
  447. case BP_HARDWARE_BREAKPOINT:
  448. breakinfo[i].type = HW_BREAKPOINT_X;
  449. break;
  450. case BP_READ_WATCHPOINT:
  451. breakinfo[i].type = HW_BREAKPOINT_R;
  452. break;
  453. case BP_WRITE_WATCHPOINT:
  454. breakinfo[i].type = HW_BREAKPOINT_W;
  455. break;
  456. case BP_ACCESS_WATCHPOINT:
  457. breakinfo[i].type = HW_BREAKPOINT_RW;
  458. break;
  459. default:
  460. return -1;
  461. }
  462. switch (len) {
  463. case 1:
  464. breakinfo[i].len = HW_BREAKPOINT_LEN_1;
  465. break;
  466. case 2:
  467. breakinfo[i].len = HW_BREAKPOINT_LEN_2;
  468. break;
  469. case 4:
  470. breakinfo[i].len = HW_BREAKPOINT_LEN_4;
  471. break;
  472. case 8:
  473. breakinfo[i].len = HW_BREAKPOINT_LEN_8;
  474. break;
  475. default:
  476. return -1;
  477. }
  478. breakinfo[i].addr = addr;
  479. if (hw_break_reserve_slot(i)) {
  480. breakinfo[i].addr = 0;
  481. return -1;
  482. }
  483. breakinfo[i].enabled = 1;
  484. return 0;
  485. }
  486. static int kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  487. {
  488. int i;
  489. for (i = 0; i < LOONGARCH_MAX_BRP; i++)
  490. if (breakinfo[i].addr == addr && breakinfo[i].enabled)
  491. break;
  492. if (i == LOONGARCH_MAX_BRP)
  493. return -1;
  494. if (hw_break_release_slot(i)) {
  495. pr_err("Cannot remove hw breakpoint at %lx\n", addr);
  496. return -1;
  497. }
  498. breakinfo[i].enabled = 0;
  499. return 0;
  500. }
  501. static void kgdb_disable_hw_break(struct pt_regs *regs)
  502. {
  503. int i;
  504. int cpu = raw_smp_processor_id();
  505. struct perf_event *bp;
  506. for (i = 0; i < LOONGARCH_MAX_BRP; i++) {
  507. if (!breakinfo[i].enabled)
  508. continue;
  509. bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
  510. if (bp->attr.disabled == 1)
  511. continue;
  512. arch_uninstall_hw_breakpoint(bp);
  513. bp->attr.disabled = 1;
  514. }
  515. /* Disable hardware debugging while we are in kgdb */
  516. csr_xchg32(0, CSR_CRMD_WE, LOONGARCH_CSR_CRMD);
  517. }
  518. static void kgdb_remove_all_hw_break(void)
  519. {
  520. int i;
  521. int cpu = raw_smp_processor_id();
  522. struct perf_event *bp;
  523. for (i = 0; i < LOONGARCH_MAX_BRP; i++) {
  524. if (!breakinfo[i].enabled)
  525. continue;
  526. bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
  527. if (!bp->attr.disabled) {
  528. arch_uninstall_hw_breakpoint(bp);
  529. bp->attr.disabled = 1;
  530. continue;
  531. }
  532. if (hw_break_release_slot(i))
  533. pr_err("KGDB: hw bpt remove failed %lx\n", breakinfo[i].addr);
  534. breakinfo[i].enabled = 0;
  535. }
  536. csr_xchg32(0, CSR_CRMD_WE, LOONGARCH_CSR_CRMD);
  537. kgdb_watch_activated = 0;
  538. }
  539. static void kgdb_correct_hw_break(void)
  540. {
  541. int i, activated = 0;
  542. for (i = 0; i < LOONGARCH_MAX_BRP; i++) {
  543. struct perf_event *bp;
  544. int val;
  545. int cpu = raw_smp_processor_id();
  546. if (!breakinfo[i].enabled)
  547. continue;
  548. bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
  549. if (bp->attr.disabled != 1)
  550. continue;
  551. bp->attr.bp_addr = breakinfo[i].addr;
  552. bp->attr.bp_len = breakinfo[i].len;
  553. bp->attr.bp_type = breakinfo[i].type;
  554. val = hw_breakpoint_arch_parse(bp, &bp->attr, counter_arch_bp(bp));
  555. if (val)
  556. return;
  557. val = arch_install_hw_breakpoint(bp);
  558. if (!val)
  559. bp->attr.disabled = 0;
  560. activated = 1;
  561. }
  562. csr_xchg32(activated ? CSR_CRMD_WE : 0, CSR_CRMD_WE, LOONGARCH_CSR_CRMD);
  563. kgdb_watch_activated = activated;
  564. }
  565. const struct kgdb_arch arch_kgdb_ops = {
  566. .gdb_bpt_instr = {0x02, 0x00, break_op >> 1, 0x00}, /* BRK_KDB = 2 */
  567. .flags = KGDB_HW_BREAKPOINT,
  568. .set_hw_breakpoint = kgdb_set_hw_break,
  569. .remove_hw_breakpoint = kgdb_remove_hw_break,
  570. .disable_hw_break = kgdb_disable_hw_break,
  571. .remove_all_hw_break = kgdb_remove_all_hw_break,
  572. .correct_hw_break = kgdb_correct_hw_break,
  573. };
  574. int kgdb_arch_init(void)
  575. {
  576. return register_die_notifier(&kgdb_notifier);
  577. }
  578. void kgdb_arch_late(void)
  579. {
  580. int i, cpu;
  581. struct perf_event_attr attr;
  582. struct perf_event **pevent;
  583. hw_breakpoint_init(&attr);
  584. attr.bp_addr = (unsigned long)kgdb_arch_init;
  585. attr.bp_len = HW_BREAKPOINT_LEN_4;
  586. attr.bp_type = HW_BREAKPOINT_W;
  587. attr.disabled = 1;
  588. for (i = 0; i < LOONGARCH_MAX_BRP; i++) {
  589. if (breakinfo[i].pev)
  590. continue;
  591. breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
  592. if (IS_ERR((void * __force)breakinfo[i].pev)) {
  593. pr_err("kgdb: Could not allocate hw breakpoints.\n");
  594. breakinfo[i].pev = NULL;
  595. return;
  596. }
  597. for_each_online_cpu(cpu) {
  598. pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
  599. if (pevent[0]->destroy) {
  600. pevent[0]->destroy = NULL;
  601. release_bp_slot(*pevent);
  602. }
  603. }
  604. }
  605. }
  606. void kgdb_arch_exit(void)
  607. {
  608. int i;
  609. for (i = 0; i < LOONGARCH_MAX_BRP; i++) {
  610. if (breakinfo[i].pev) {
  611. unregister_wide_hw_breakpoint(breakinfo[i].pev);
  612. breakinfo[i].pev = NULL;
  613. }
  614. }
  615. unregister_die_notifier(&kgdb_notifier);
  616. }