kgdb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <asm/debug-monitors.h>
  27. #include <asm/insn.h>
  28. #include <asm/traps.h>
  29. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  30. { "x0", 8, offsetof(struct pt_regs, regs[0])},
  31. { "x1", 8, offsetof(struct pt_regs, regs[1])},
  32. { "x2", 8, offsetof(struct pt_regs, regs[2])},
  33. { "x3", 8, offsetof(struct pt_regs, regs[3])},
  34. { "x4", 8, offsetof(struct pt_regs, regs[4])},
  35. { "x5", 8, offsetof(struct pt_regs, regs[5])},
  36. { "x6", 8, offsetof(struct pt_regs, regs[6])},
  37. { "x7", 8, offsetof(struct pt_regs, regs[7])},
  38. { "x8", 8, offsetof(struct pt_regs, regs[8])},
  39. { "x9", 8, offsetof(struct pt_regs, regs[9])},
  40. { "x10", 8, offsetof(struct pt_regs, regs[10])},
  41. { "x11", 8, offsetof(struct pt_regs, regs[11])},
  42. { "x12", 8, offsetof(struct pt_regs, regs[12])},
  43. { "x13", 8, offsetof(struct pt_regs, regs[13])},
  44. { "x14", 8, offsetof(struct pt_regs, regs[14])},
  45. { "x15", 8, offsetof(struct pt_regs, regs[15])},
  46. { "x16", 8, offsetof(struct pt_regs, regs[16])},
  47. { "x17", 8, offsetof(struct pt_regs, regs[17])},
  48. { "x18", 8, offsetof(struct pt_regs, regs[18])},
  49. { "x19", 8, offsetof(struct pt_regs, regs[19])},
  50. { "x20", 8, offsetof(struct pt_regs, regs[20])},
  51. { "x21", 8, offsetof(struct pt_regs, regs[21])},
  52. { "x22", 8, offsetof(struct pt_regs, regs[22])},
  53. { "x23", 8, offsetof(struct pt_regs, regs[23])},
  54. { "x24", 8, offsetof(struct pt_regs, regs[24])},
  55. { "x25", 8, offsetof(struct pt_regs, regs[25])},
  56. { "x26", 8, offsetof(struct pt_regs, regs[26])},
  57. { "x27", 8, offsetof(struct pt_regs, regs[27])},
  58. { "x28", 8, offsetof(struct pt_regs, regs[28])},
  59. { "x29", 8, offsetof(struct pt_regs, regs[29])},
  60. { "x30", 8, offsetof(struct pt_regs, regs[30])},
  61. { "sp", 8, offsetof(struct pt_regs, sp)},
  62. { "pc", 8, offsetof(struct pt_regs, pc)},
  63. /*
  64. * struct pt_regs thinks PSTATE is 64-bits wide but gdb remote
  65. * protocol disagrees. Therefore we must extract only the lower
  66. * 32-bits. Look for the big comment in asm/kgdb.h for more
  67. * detail.
  68. */
  69. { "pstate", 4, offsetof(struct pt_regs, pstate)
  70. #ifdef CONFIG_CPU_BIG_ENDIAN
  71. + 4
  72. #endif
  73. },
  74. { "v0", 16, -1 },
  75. { "v1", 16, -1 },
  76. { "v2", 16, -1 },
  77. { "v3", 16, -1 },
  78. { "v4", 16, -1 },
  79. { "v5", 16, -1 },
  80. { "v6", 16, -1 },
  81. { "v7", 16, -1 },
  82. { "v8", 16, -1 },
  83. { "v9", 16, -1 },
  84. { "v10", 16, -1 },
  85. { "v11", 16, -1 },
  86. { "v12", 16, -1 },
  87. { "v13", 16, -1 },
  88. { "v14", 16, -1 },
  89. { "v15", 16, -1 },
  90. { "v16", 16, -1 },
  91. { "v17", 16, -1 },
  92. { "v18", 16, -1 },
  93. { "v19", 16, -1 },
  94. { "v20", 16, -1 },
  95. { "v21", 16, -1 },
  96. { "v22", 16, -1 },
  97. { "v23", 16, -1 },
  98. { "v24", 16, -1 },
  99. { "v25", 16, -1 },
  100. { "v26", 16, -1 },
  101. { "v27", 16, -1 },
  102. { "v28", 16, -1 },
  103. { "v29", 16, -1 },
  104. { "v30", 16, -1 },
  105. { "v31", 16, -1 },
  106. { "fpsr", 4, -1 },
  107. { "fpcr", 4, -1 },
  108. };
  109. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  110. {
  111. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  112. return NULL;
  113. if (dbg_reg_def[regno].offset != -1)
  114. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  115. dbg_reg_def[regno].size);
  116. else
  117. memset(mem, 0, dbg_reg_def[regno].size);
  118. return dbg_reg_def[regno].name;
  119. }
  120. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  121. {
  122. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  123. return -EINVAL;
  124. if (dbg_reg_def[regno].offset != -1)
  125. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  126. dbg_reg_def[regno].size);
  127. return 0;
  128. }
  129. void
  130. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  131. {
  132. struct pt_regs *thread_regs;
  133. /* Initialize to zero */
  134. memset((char *)gdb_regs, 0, NUMREGBYTES);
  135. thread_regs = task_pt_regs(task);
  136. memcpy((void *)gdb_regs, (void *)thread_regs->regs, GP_REG_BYTES);
  137. /* Special case for PSTATE (check comments in asm/kgdb.h for details) */
  138. dbg_get_reg(33, gdb_regs + GP_REG_BYTES, thread_regs);
  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. err = 0;
  207. break;
  208. default:
  209. err = -1;
  210. }
  211. return err;
  212. }
  213. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
  214. {
  215. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  216. return 0;
  217. }
  218. NOKPROBE_SYMBOL(kgdb_brk_fn)
  219. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
  220. {
  221. compiled_break = 1;
  222. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  223. return 0;
  224. }
  225. NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
  226. static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
  227. {
  228. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  229. return 0;
  230. }
  231. NOKPROBE_SYMBOL(kgdb_step_brk_fn);
  232. static struct break_hook kgdb_brkpt_hook = {
  233. .esr_mask = 0xffffffff,
  234. .esr_val = (u32)ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
  235. .fn = kgdb_brk_fn
  236. };
  237. static struct break_hook kgdb_compiled_brkpt_hook = {
  238. .esr_mask = 0xffffffff,
  239. .esr_val = (u32)ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
  240. .fn = kgdb_compiled_brk_fn
  241. };
  242. static struct step_hook kgdb_step_hook = {
  243. .fn = kgdb_step_brk_fn
  244. };
  245. static void kgdb_call_nmi_hook(void *ignored)
  246. {
  247. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  248. }
  249. void kgdb_roundup_cpus(unsigned long flags)
  250. {
  251. local_irq_enable();
  252. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  253. local_irq_disable();
  254. }
  255. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  256. {
  257. struct pt_regs *regs = args->regs;
  258. if (kgdb_handle_exception(1, args->signr, cmd, regs))
  259. return NOTIFY_DONE;
  260. return NOTIFY_STOP;
  261. }
  262. static int
  263. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  264. {
  265. unsigned long flags;
  266. int ret;
  267. local_irq_save(flags);
  268. ret = __kgdb_notify(ptr, cmd);
  269. local_irq_restore(flags);
  270. return ret;
  271. }
  272. static struct notifier_block kgdb_notifier = {
  273. .notifier_call = kgdb_notify,
  274. /*
  275. * Want to be lowest priority
  276. */
  277. .priority = -INT_MAX,
  278. };
  279. /*
  280. * kgdb_arch_init - Perform any architecture specific initialization.
  281. * This function will handle the initialization of any architecture
  282. * specific callbacks.
  283. */
  284. int kgdb_arch_init(void)
  285. {
  286. int ret = register_die_notifier(&kgdb_notifier);
  287. if (ret != 0)
  288. return ret;
  289. register_break_hook(&kgdb_brkpt_hook);
  290. register_break_hook(&kgdb_compiled_brkpt_hook);
  291. register_step_hook(&kgdb_step_hook);
  292. return 0;
  293. }
  294. /*
  295. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  296. * This function will handle the uninitalization of any architecture
  297. * specific callbacks, for dynamic registration and unregistration.
  298. */
  299. void kgdb_arch_exit(void)
  300. {
  301. unregister_break_hook(&kgdb_brkpt_hook);
  302. unregister_break_hook(&kgdb_compiled_brkpt_hook);
  303. unregister_step_hook(&kgdb_step_hook);
  304. unregister_die_notifier(&kgdb_notifier);
  305. }
  306. struct kgdb_arch arch_kgdb_ops;
  307. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  308. {
  309. int err;
  310. BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
  311. err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
  312. if (err)
  313. return err;
  314. return aarch64_insn_write((void *)bpt->bpt_addr,
  315. (u32)AARCH64_BREAK_KGDB_DYN_DBG);
  316. }
  317. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  318. {
  319. return aarch64_insn_write((void *)bpt->bpt_addr,
  320. *(u32 *)bpt->saved_instr);
  321. }