kgdb.c 8.9 KB

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