kgdb.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/arm/kernel/kgdb.c
  4. *
  5. * ARM KGDB support
  6. *
  7. * Copyright (c) 2002-2004 MontaVista Software, Inc
  8. * Copyright (c) 2008 Wind River Systems, Inc.
  9. *
  10. * Authors: George Davis <[email protected]>
  11. * Deepak Saxena <[email protected]>
  12. */
  13. #include <linux/irq.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/kgdb.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/patch.h>
  18. #include <asm/traps.h>
  19. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  20. {
  21. { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
  22. { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
  23. { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
  24. { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
  25. { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
  26. { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
  27. { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
  28. { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
  29. { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
  30. { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
  31. { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
  32. { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
  33. { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
  34. { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
  35. { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
  36. { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
  37. { "f0", 12, -1 },
  38. { "f1", 12, -1 },
  39. { "f2", 12, -1 },
  40. { "f3", 12, -1 },
  41. { "f4", 12, -1 },
  42. { "f5", 12, -1 },
  43. { "f6", 12, -1 },
  44. { "f7", 12, -1 },
  45. { "fps", 4, -1 },
  46. { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
  47. };
  48. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  49. {
  50. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  51. return NULL;
  52. if (dbg_reg_def[regno].offset != -1)
  53. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  54. dbg_reg_def[regno].size);
  55. else
  56. memset(mem, 0, dbg_reg_def[regno].size);
  57. return dbg_reg_def[regno].name;
  58. }
  59. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  60. {
  61. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  62. return -EINVAL;
  63. if (dbg_reg_def[regno].offset != -1)
  64. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  65. dbg_reg_def[regno].size);
  66. return 0;
  67. }
  68. void
  69. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  70. {
  71. struct thread_info *ti;
  72. int regno;
  73. /* Just making sure... */
  74. if (task == NULL)
  75. return;
  76. /* Initialize to zero */
  77. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  78. gdb_regs[regno] = 0;
  79. /* Otherwise, we have only some registers from switch_to() */
  80. ti = task_thread_info(task);
  81. gdb_regs[_R4] = ti->cpu_context.r4;
  82. gdb_regs[_R5] = ti->cpu_context.r5;
  83. gdb_regs[_R6] = ti->cpu_context.r6;
  84. gdb_regs[_R7] = ti->cpu_context.r7;
  85. gdb_regs[_R8] = ti->cpu_context.r8;
  86. gdb_regs[_R9] = ti->cpu_context.r9;
  87. gdb_regs[_R10] = ti->cpu_context.sl;
  88. gdb_regs[_FP] = ti->cpu_context.fp;
  89. gdb_regs[_SPT] = ti->cpu_context.sp;
  90. gdb_regs[_PC] = ti->cpu_context.pc;
  91. }
  92. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  93. {
  94. regs->ARM_pc = pc;
  95. }
  96. static int compiled_break;
  97. int kgdb_arch_handle_exception(int exception_vector, int signo,
  98. int err_code, char *remcom_in_buffer,
  99. char *remcom_out_buffer,
  100. struct pt_regs *linux_regs)
  101. {
  102. unsigned long addr;
  103. char *ptr;
  104. switch (remcom_in_buffer[0]) {
  105. case 'D':
  106. case 'k':
  107. case 'c':
  108. /*
  109. * Try to read optional parameter, pc unchanged if no parm.
  110. * If this was a compiled breakpoint, we need to move
  111. * to the next instruction or we will just breakpoint
  112. * over and over again.
  113. */
  114. ptr = &remcom_in_buffer[1];
  115. if (kgdb_hex2long(&ptr, &addr))
  116. linux_regs->ARM_pc = addr;
  117. else if (compiled_break == 1)
  118. linux_regs->ARM_pc += 4;
  119. compiled_break = 0;
  120. return 0;
  121. }
  122. return -1;
  123. }
  124. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
  125. {
  126. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  127. return 0;
  128. }
  129. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
  130. {
  131. compiled_break = 1;
  132. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  133. return 0;
  134. }
  135. static struct undef_hook kgdb_brkpt_arm_hook = {
  136. .instr_mask = 0xffffffff,
  137. .instr_val = KGDB_BREAKINST,
  138. .cpsr_mask = PSR_T_BIT | MODE_MASK,
  139. .cpsr_val = SVC_MODE,
  140. .fn = kgdb_brk_fn
  141. };
  142. static struct undef_hook kgdb_brkpt_thumb_hook = {
  143. .instr_mask = 0xffff,
  144. .instr_val = KGDB_BREAKINST & 0xffff,
  145. .cpsr_mask = PSR_T_BIT | MODE_MASK,
  146. .cpsr_val = PSR_T_BIT | SVC_MODE,
  147. .fn = kgdb_brk_fn
  148. };
  149. static struct undef_hook kgdb_compiled_brkpt_arm_hook = {
  150. .instr_mask = 0xffffffff,
  151. .instr_val = KGDB_COMPILED_BREAK,
  152. .cpsr_mask = PSR_T_BIT | MODE_MASK,
  153. .cpsr_val = SVC_MODE,
  154. .fn = kgdb_compiled_brk_fn
  155. };
  156. static struct undef_hook kgdb_compiled_brkpt_thumb_hook = {
  157. .instr_mask = 0xffff,
  158. .instr_val = KGDB_COMPILED_BREAK & 0xffff,
  159. .cpsr_mask = PSR_T_BIT | MODE_MASK,
  160. .cpsr_val = PSR_T_BIT | SVC_MODE,
  161. .fn = kgdb_compiled_brk_fn
  162. };
  163. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  164. {
  165. struct pt_regs *regs = args->regs;
  166. if (kgdb_handle_exception(1, args->signr, cmd, regs))
  167. return NOTIFY_DONE;
  168. return NOTIFY_STOP;
  169. }
  170. static int
  171. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  172. {
  173. unsigned long flags;
  174. int ret;
  175. local_irq_save(flags);
  176. ret = __kgdb_notify(ptr, cmd);
  177. local_irq_restore(flags);
  178. return ret;
  179. }
  180. static struct notifier_block kgdb_notifier = {
  181. .notifier_call = kgdb_notify,
  182. .priority = -INT_MAX,
  183. };
  184. /**
  185. * kgdb_arch_init - Perform any architecture specific initalization.
  186. *
  187. * This function will handle the initalization of any architecture
  188. * specific callbacks.
  189. */
  190. int kgdb_arch_init(void)
  191. {
  192. int ret = register_die_notifier(&kgdb_notifier);
  193. if (ret != 0)
  194. return ret;
  195. register_undef_hook(&kgdb_brkpt_arm_hook);
  196. register_undef_hook(&kgdb_brkpt_thumb_hook);
  197. register_undef_hook(&kgdb_compiled_brkpt_arm_hook);
  198. register_undef_hook(&kgdb_compiled_brkpt_thumb_hook);
  199. return 0;
  200. }
  201. /**
  202. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  203. *
  204. * This function will handle the uninitalization of any architecture
  205. * specific callbacks, for dynamic registration and unregistration.
  206. */
  207. void kgdb_arch_exit(void)
  208. {
  209. unregister_undef_hook(&kgdb_brkpt_arm_hook);
  210. unregister_undef_hook(&kgdb_brkpt_thumb_hook);
  211. unregister_undef_hook(&kgdb_compiled_brkpt_arm_hook);
  212. unregister_undef_hook(&kgdb_compiled_brkpt_thumb_hook);
  213. unregister_die_notifier(&kgdb_notifier);
  214. }
  215. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  216. {
  217. int err;
  218. /* patch_text() only supports int-sized breakpoints */
  219. BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
  220. err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
  221. BREAK_INSTR_SIZE);
  222. if (err)
  223. return err;
  224. /* Machine is already stopped, so we can use __patch_text() directly */
  225. __patch_text((void *)bpt->bpt_addr,
  226. *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
  227. return err;
  228. }
  229. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  230. {
  231. /* Machine is already stopped, so we can use __patch_text() directly */
  232. __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
  233. return 0;
  234. }
  235. /*
  236. * Register our undef instruction hooks with ARM undef core.
  237. * We register a hook specifically looking for the KGB break inst
  238. * and we handle the normal undef case within the do_undefinstr
  239. * handler.
  240. */
  241. const struct kgdb_arch arch_kgdb_ops = {
  242. #ifndef __ARMEB__
  243. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  244. #else /* ! __ARMEB__ */
  245. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  246. #endif
  247. };