kgdb.c 9.1 KB

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