kgdb.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AArch64 KGDB support
  4. *
  5. * Based on arch/arm/kernel/kgdb.c
  6. *
  7. * Copyright (C) 2013 Cavium Inc.
  8. * Author: Vijaya Kumar K <[email protected]>
  9. */
  10. #include <linux/bug.h>
  11. #include <linux/irq.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/kgdb.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <asm/debug-monitors.h>
  17. #include <asm/insn.h>
  18. #include <asm/patching.h>
  19. #include <asm/traps.h>
  20. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  21. { "x0", 8, offsetof(struct pt_regs, regs[0])},
  22. { "x1", 8, offsetof(struct pt_regs, regs[1])},
  23. { "x2", 8, offsetof(struct pt_regs, regs[2])},
  24. { "x3", 8, offsetof(struct pt_regs, regs[3])},
  25. { "x4", 8, offsetof(struct pt_regs, regs[4])},
  26. { "x5", 8, offsetof(struct pt_regs, regs[5])},
  27. { "x6", 8, offsetof(struct pt_regs, regs[6])},
  28. { "x7", 8, offsetof(struct pt_regs, regs[7])},
  29. { "x8", 8, offsetof(struct pt_regs, regs[8])},
  30. { "x9", 8, offsetof(struct pt_regs, regs[9])},
  31. { "x10", 8, offsetof(struct pt_regs, regs[10])},
  32. { "x11", 8, offsetof(struct pt_regs, regs[11])},
  33. { "x12", 8, offsetof(struct pt_regs, regs[12])},
  34. { "x13", 8, offsetof(struct pt_regs, regs[13])},
  35. { "x14", 8, offsetof(struct pt_regs, regs[14])},
  36. { "x15", 8, offsetof(struct pt_regs, regs[15])},
  37. { "x16", 8, offsetof(struct pt_regs, regs[16])},
  38. { "x17", 8, offsetof(struct pt_regs, regs[17])},
  39. { "x18", 8, offsetof(struct pt_regs, regs[18])},
  40. { "x19", 8, offsetof(struct pt_regs, regs[19])},
  41. { "x20", 8, offsetof(struct pt_regs, regs[20])},
  42. { "x21", 8, offsetof(struct pt_regs, regs[21])},
  43. { "x22", 8, offsetof(struct pt_regs, regs[22])},
  44. { "x23", 8, offsetof(struct pt_regs, regs[23])},
  45. { "x24", 8, offsetof(struct pt_regs, regs[24])},
  46. { "x25", 8, offsetof(struct pt_regs, regs[25])},
  47. { "x26", 8, offsetof(struct pt_regs, regs[26])},
  48. { "x27", 8, offsetof(struct pt_regs, regs[27])},
  49. { "x28", 8, offsetof(struct pt_regs, regs[28])},
  50. { "x29", 8, offsetof(struct pt_regs, regs[29])},
  51. { "x30", 8, offsetof(struct pt_regs, regs[30])},
  52. { "sp", 8, offsetof(struct pt_regs, sp)},
  53. { "pc", 8, offsetof(struct pt_regs, pc)},
  54. /*
  55. * struct pt_regs thinks PSTATE is 64-bits wide but gdb remote
  56. * protocol disagrees. Therefore we must extract only the lower
  57. * 32-bits. Look for the big comment in asm/kgdb.h for more
  58. * detail.
  59. */
  60. { "pstate", 4, offsetof(struct pt_regs, pstate)
  61. #ifdef CONFIG_CPU_BIG_ENDIAN
  62. + 4
  63. #endif
  64. },
  65. { "v0", 16, -1 },
  66. { "v1", 16, -1 },
  67. { "v2", 16, -1 },
  68. { "v3", 16, -1 },
  69. { "v4", 16, -1 },
  70. { "v5", 16, -1 },
  71. { "v6", 16, -1 },
  72. { "v7", 16, -1 },
  73. { "v8", 16, -1 },
  74. { "v9", 16, -1 },
  75. { "v10", 16, -1 },
  76. { "v11", 16, -1 },
  77. { "v12", 16, -1 },
  78. { "v13", 16, -1 },
  79. { "v14", 16, -1 },
  80. { "v15", 16, -1 },
  81. { "v16", 16, -1 },
  82. { "v17", 16, -1 },
  83. { "v18", 16, -1 },
  84. { "v19", 16, -1 },
  85. { "v20", 16, -1 },
  86. { "v21", 16, -1 },
  87. { "v22", 16, -1 },
  88. { "v23", 16, -1 },
  89. { "v24", 16, -1 },
  90. { "v25", 16, -1 },
  91. { "v26", 16, -1 },
  92. { "v27", 16, -1 },
  93. { "v28", 16, -1 },
  94. { "v29", 16, -1 },
  95. { "v30", 16, -1 },
  96. { "v31", 16, -1 },
  97. { "fpsr", 4, -1 },
  98. { "fpcr", 4, -1 },
  99. };
  100. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  101. {
  102. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  103. return NULL;
  104. if (dbg_reg_def[regno].offset != -1)
  105. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  106. dbg_reg_def[regno].size);
  107. else
  108. memset(mem, 0, dbg_reg_def[regno].size);
  109. return dbg_reg_def[regno].name;
  110. }
  111. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  112. {
  113. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  114. return -EINVAL;
  115. if (dbg_reg_def[regno].offset != -1)
  116. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  117. dbg_reg_def[regno].size);
  118. return 0;
  119. }
  120. void
  121. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  122. {
  123. struct cpu_context *cpu_context = &task->thread.cpu_context;
  124. /* Initialize to zero */
  125. memset((char *)gdb_regs, 0, NUMREGBYTES);
  126. gdb_regs[19] = cpu_context->x19;
  127. gdb_regs[20] = cpu_context->x20;
  128. gdb_regs[21] = cpu_context->x21;
  129. gdb_regs[22] = cpu_context->x22;
  130. gdb_regs[23] = cpu_context->x23;
  131. gdb_regs[24] = cpu_context->x24;
  132. gdb_regs[25] = cpu_context->x25;
  133. gdb_regs[26] = cpu_context->x26;
  134. gdb_regs[27] = cpu_context->x27;
  135. gdb_regs[28] = cpu_context->x28;
  136. gdb_regs[29] = cpu_context->fp;
  137. gdb_regs[31] = cpu_context->sp;
  138. gdb_regs[32] = cpu_context->pc;
  139. }
  140. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  141. {
  142. regs->pc = pc;
  143. }
  144. static int compiled_break;
  145. static void kgdb_arch_update_addr(struct pt_regs *regs,
  146. char *remcom_in_buffer)
  147. {
  148. unsigned long addr;
  149. char *ptr;
  150. ptr = &remcom_in_buffer[1];
  151. if (kgdb_hex2long(&ptr, &addr))
  152. kgdb_arch_set_pc(regs, addr);
  153. else if (compiled_break == 1)
  154. kgdb_arch_set_pc(regs, regs->pc + 4);
  155. compiled_break = 0;
  156. }
  157. int kgdb_arch_handle_exception(int exception_vector, int signo,
  158. int err_code, char *remcom_in_buffer,
  159. char *remcom_out_buffer,
  160. struct pt_regs *linux_regs)
  161. {
  162. int err;
  163. switch (remcom_in_buffer[0]) {
  164. case 'D':
  165. case 'k':
  166. /*
  167. * Packet D (Detach), k (kill). No special handling
  168. * is required here. Handle same as c packet.
  169. */
  170. case 'c':
  171. /*
  172. * Packet c (Continue) to continue executing.
  173. * Set pc to required address.
  174. * Try to read optional parameter and set pc.
  175. * If this was a compiled breakpoint, we need to move
  176. * to the next instruction else we will just breakpoint
  177. * over and over again.
  178. */
  179. kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
  180. atomic_set(&kgdb_cpu_doing_single_step, -1);
  181. kgdb_single_step = 0;
  182. /*
  183. * Received continue command, disable single step
  184. */
  185. if (kernel_active_single_step())
  186. kernel_disable_single_step();
  187. err = 0;
  188. break;
  189. case 's':
  190. /*
  191. * Update step address value with address passed
  192. * with step packet.
  193. * On debug exception return PC is copied to ELR
  194. * So just update PC.
  195. * If no step address is passed, resume from the address
  196. * pointed by PC. Do not update PC
  197. */
  198. kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
  199. atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
  200. kgdb_single_step = 1;
  201. /*
  202. * Enable single step handling
  203. */
  204. if (!kernel_active_single_step())
  205. kernel_enable_single_step(linux_regs);
  206. else
  207. kernel_rewind_single_step(linux_regs);
  208. err = 0;
  209. break;
  210. default:
  211. err = -1;
  212. }
  213. return err;
  214. }
  215. static int kgdb_brk_fn(struct pt_regs *regs, unsigned long esr)
  216. {
  217. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  218. return DBG_HOOK_HANDLED;
  219. }
  220. NOKPROBE_SYMBOL(kgdb_brk_fn)
  221. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned long esr)
  222. {
  223. compiled_break = 1;
  224. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  225. return DBG_HOOK_HANDLED;
  226. }
  227. NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
  228. static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned long esr)
  229. {
  230. if (!kgdb_single_step)
  231. return DBG_HOOK_ERROR;
  232. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  233. return DBG_HOOK_HANDLED;
  234. }
  235. NOKPROBE_SYMBOL(kgdb_step_brk_fn);
  236. static struct break_hook kgdb_brkpt_hook = {
  237. .fn = kgdb_brk_fn,
  238. .imm = KGDB_DYN_DBG_BRK_IMM,
  239. };
  240. static struct break_hook kgdb_compiled_brkpt_hook = {
  241. .fn = kgdb_compiled_brk_fn,
  242. .imm = KGDB_COMPILED_DBG_BRK_IMM,
  243. };
  244. static struct step_hook kgdb_step_hook = {
  245. .fn = kgdb_step_brk_fn
  246. };
  247. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  248. {
  249. struct pt_regs *regs = args->regs;
  250. if (kgdb_handle_exception(1, args->signr, cmd, regs))
  251. return NOTIFY_DONE;
  252. return NOTIFY_STOP;
  253. }
  254. static int
  255. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  256. {
  257. unsigned long flags;
  258. int ret;
  259. local_irq_save(flags);
  260. ret = __kgdb_notify(ptr, cmd);
  261. local_irq_restore(flags);
  262. return ret;
  263. }
  264. static struct notifier_block kgdb_notifier = {
  265. .notifier_call = kgdb_notify,
  266. /*
  267. * Want to be lowest priority
  268. */
  269. .priority = -INT_MAX,
  270. };
  271. /*
  272. * kgdb_arch_init - Perform any architecture specific initialization.
  273. * This function will handle the initialization of any architecture
  274. * specific callbacks.
  275. */
  276. int kgdb_arch_init(void)
  277. {
  278. int ret = register_die_notifier(&kgdb_notifier);
  279. if (ret != 0)
  280. return ret;
  281. register_kernel_break_hook(&kgdb_brkpt_hook);
  282. register_kernel_break_hook(&kgdb_compiled_brkpt_hook);
  283. register_kernel_step_hook(&kgdb_step_hook);
  284. return 0;
  285. }
  286. /*
  287. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  288. * This function will handle the uninitalization of any architecture
  289. * specific callbacks, for dynamic registration and unregistration.
  290. */
  291. void kgdb_arch_exit(void)
  292. {
  293. unregister_kernel_break_hook(&kgdb_brkpt_hook);
  294. unregister_kernel_break_hook(&kgdb_compiled_brkpt_hook);
  295. unregister_kernel_step_hook(&kgdb_step_hook);
  296. unregister_die_notifier(&kgdb_notifier);
  297. }
  298. const struct kgdb_arch arch_kgdb_ops;
  299. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  300. {
  301. int err;
  302. BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
  303. err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
  304. if (err)
  305. return err;
  306. return aarch64_insn_write((void *)bpt->bpt_addr,
  307. (u32)AARCH64_BREAK_KGDB_DYN_DBG);
  308. }
  309. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  310. {
  311. return aarch64_insn_write((void *)bpt->bpt_addr,
  312. *(u32 *)bpt->saved_instr);
  313. }