kgdb.c 8.6 KB

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