kgdb.c 9.7 KB

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