kgdb.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 pt_regs *thread_regs;
  134. /* Initialize to zero */
  135. memset((char *)gdb_regs, 0, NUMREGBYTES);
  136. thread_regs = task_pt_regs(task);
  137. memcpy((void *)gdb_regs, (void *)thread_regs->regs, GP_REG_BYTES);
  138. /* Special case for PSTATE (check comments in asm/kgdb.h for details) */
  139. dbg_get_reg(33, gdb_regs + GP_REG_BYTES, thread_regs);
  140. }
  141. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  142. {
  143. regs->pc = pc;
  144. }
  145. static int compiled_break;
  146. static void kgdb_arch_update_addr(struct pt_regs *regs,
  147. char *remcom_in_buffer)
  148. {
  149. unsigned long addr;
  150. char *ptr;
  151. ptr = &remcom_in_buffer[1];
  152. if (kgdb_hex2long(&ptr, &addr))
  153. kgdb_arch_set_pc(regs, addr);
  154. else if (compiled_break == 1)
  155. kgdb_arch_set_pc(regs, regs->pc + 4);
  156. compiled_break = 0;
  157. }
  158. int kgdb_arch_handle_exception(int exception_vector, int signo,
  159. int err_code, char *remcom_in_buffer,
  160. char *remcom_out_buffer,
  161. struct pt_regs *linux_regs)
  162. {
  163. int err;
  164. switch (remcom_in_buffer[0]) {
  165. case 'D':
  166. case 'k':
  167. /*
  168. * Packet D (Detach), k (kill). No special handling
  169. * is required here. Handle same as c packet.
  170. */
  171. case 'c':
  172. /*
  173. * Packet c (Continue) to continue executing.
  174. * Set pc to required address.
  175. * Try to read optional parameter and set pc.
  176. * If this was a compiled breakpoint, we need to move
  177. * to the next instruction else we will just breakpoint
  178. * over and over again.
  179. */
  180. kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
  181. atomic_set(&kgdb_cpu_doing_single_step, -1);
  182. kgdb_single_step = 0;
  183. /*
  184. * Received continue command, disable single step
  185. */
  186. if (kernel_active_single_step())
  187. kernel_disable_single_step();
  188. err = 0;
  189. break;
  190. case 's':
  191. /*
  192. * Update step address value with address passed
  193. * with step packet.
  194. * On debug exception return PC is copied to ELR
  195. * So just update PC.
  196. * If no step address is passed, resume from the address
  197. * pointed by PC. Do not update PC
  198. */
  199. kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
  200. atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
  201. kgdb_single_step = 1;
  202. /*
  203. * Enable single step handling
  204. */
  205. if (!kernel_active_single_step())
  206. kernel_enable_single_step(linux_regs);
  207. err = 0;
  208. break;
  209. default:
  210. err = -1;
  211. }
  212. return err;
  213. }
  214. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
  215. {
  216. if (user_mode(regs))
  217. return DBG_HOOK_ERROR;
  218. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  219. return DBG_HOOK_HANDLED;
  220. }
  221. NOKPROBE_SYMBOL(kgdb_brk_fn)
  222. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
  223. {
  224. if (user_mode(regs))
  225. return DBG_HOOK_ERROR;
  226. compiled_break = 1;
  227. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  228. return DBG_HOOK_HANDLED;
  229. }
  230. NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
  231. static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
  232. {
  233. if (user_mode(regs) || !kgdb_single_step)
  234. return DBG_HOOK_ERROR;
  235. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  236. return DBG_HOOK_HANDLED;
  237. }
  238. NOKPROBE_SYMBOL(kgdb_step_brk_fn);
  239. static struct break_hook kgdb_brkpt_hook = {
  240. .esr_mask = 0xffffffff,
  241. .esr_val = (u32)ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
  242. .fn = kgdb_brk_fn
  243. };
  244. static struct break_hook kgdb_compiled_brkpt_hook = {
  245. .esr_mask = 0xffffffff,
  246. .esr_val = (u32)ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
  247. .fn = kgdb_compiled_brk_fn
  248. };
  249. static struct step_hook kgdb_step_hook = {
  250. .fn = kgdb_step_brk_fn
  251. };
  252. static void kgdb_call_nmi_hook(void *ignored)
  253. {
  254. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  255. }
  256. void kgdb_roundup_cpus(unsigned long flags)
  257. {
  258. local_irq_enable();
  259. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  260. local_irq_disable();
  261. }
  262. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  263. {
  264. struct pt_regs *regs = args->regs;
  265. if (kgdb_handle_exception(1, args->signr, cmd, regs))
  266. return NOTIFY_DONE;
  267. return NOTIFY_STOP;
  268. }
  269. static int
  270. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  271. {
  272. unsigned long flags;
  273. int ret;
  274. local_irq_save(flags);
  275. ret = __kgdb_notify(ptr, cmd);
  276. local_irq_restore(flags);
  277. return ret;
  278. }
  279. static struct notifier_block kgdb_notifier = {
  280. .notifier_call = kgdb_notify,
  281. /*
  282. * Want to be lowest priority
  283. */
  284. .priority = -INT_MAX,
  285. };
  286. /*
  287. * kgdb_arch_init - Perform any architecture specific initialization.
  288. * This function will handle the initialization of any architecture
  289. * specific callbacks.
  290. */
  291. int kgdb_arch_init(void)
  292. {
  293. int ret = register_die_notifier(&kgdb_notifier);
  294. if (ret != 0)
  295. return ret;
  296. register_break_hook(&kgdb_brkpt_hook);
  297. register_break_hook(&kgdb_compiled_brkpt_hook);
  298. register_step_hook(&kgdb_step_hook);
  299. return 0;
  300. }
  301. /*
  302. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  303. * This function will handle the uninitalization of any architecture
  304. * specific callbacks, for dynamic registration and unregistration.
  305. */
  306. void kgdb_arch_exit(void)
  307. {
  308. unregister_break_hook(&kgdb_brkpt_hook);
  309. unregister_break_hook(&kgdb_compiled_brkpt_hook);
  310. unregister_step_hook(&kgdb_step_hook);
  311. unregister_die_notifier(&kgdb_notifier);
  312. }
  313. struct kgdb_arch arch_kgdb_ops;
  314. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  315. {
  316. int err;
  317. BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
  318. err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
  319. if (err)
  320. return err;
  321. return aarch64_insn_write((void *)bpt->bpt_addr,
  322. (u32)AARCH64_BREAK_KGDB_DYN_DBG);
  323. }
  324. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  325. {
  326. return aarch64_insn_write((void *)bpt->bpt_addr,
  327. *(u32 *)bpt->saved_instr);
  328. }