kgdb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <davis_g@mvista.com>
  11. * Deepak Saxena <dsaxena@plexity.net>
  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_hook = {
  136. .instr_mask = 0xffffffff,
  137. .instr_val = KGDB_BREAKINST,
  138. .cpsr_mask = MODE_MASK,
  139. .cpsr_val = SVC_MODE,
  140. .fn = kgdb_brk_fn
  141. };
  142. static struct undef_hook kgdb_compiled_brkpt_hook = {
  143. .instr_mask = 0xffffffff,
  144. .instr_val = KGDB_COMPILED_BREAK,
  145. .cpsr_mask = MODE_MASK,
  146. .cpsr_val = SVC_MODE,
  147. .fn = kgdb_compiled_brk_fn
  148. };
  149. static void kgdb_call_nmi_hook(void *ignored)
  150. {
  151. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  152. }
  153. void kgdb_roundup_cpus(unsigned long flags)
  154. {
  155. local_irq_enable();
  156. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  157. local_irq_disable();
  158. }
  159. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  160. {
  161. struct pt_regs *regs = args->regs;
  162. if (kgdb_handle_exception(1, args->signr, cmd, regs))
  163. return NOTIFY_DONE;
  164. return NOTIFY_STOP;
  165. }
  166. static int
  167. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  168. {
  169. unsigned long flags;
  170. int ret;
  171. local_irq_save(flags);
  172. ret = __kgdb_notify(ptr, cmd);
  173. local_irq_restore(flags);
  174. return ret;
  175. }
  176. static struct notifier_block kgdb_notifier = {
  177. .notifier_call = kgdb_notify,
  178. .priority = -INT_MAX,
  179. };
  180. /**
  181. * kgdb_arch_init - Perform any architecture specific initalization.
  182. *
  183. * This function will handle the initalization of any architecture
  184. * specific callbacks.
  185. */
  186. int kgdb_arch_init(void)
  187. {
  188. int ret = register_die_notifier(&kgdb_notifier);
  189. if (ret != 0)
  190. return ret;
  191. register_undef_hook(&kgdb_brkpt_hook);
  192. register_undef_hook(&kgdb_compiled_brkpt_hook);
  193. return 0;
  194. }
  195. /**
  196. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  197. *
  198. * This function will handle the uninitalization of any architecture
  199. * specific callbacks, for dynamic registration and unregistration.
  200. */
  201. void kgdb_arch_exit(void)
  202. {
  203. unregister_undef_hook(&kgdb_brkpt_hook);
  204. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  205. unregister_die_notifier(&kgdb_notifier);
  206. }
  207. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  208. {
  209. int err;
  210. /* patch_text() only supports int-sized breakpoints */
  211. BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
  212. err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
  213. BREAK_INSTR_SIZE);
  214. if (err)
  215. return err;
  216. /* Machine is already stopped, so we can use __patch_text() directly */
  217. __patch_text((void *)bpt->bpt_addr,
  218. *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
  219. return err;
  220. }
  221. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  222. {
  223. /* Machine is already stopped, so we can use __patch_text() directly */
  224. __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
  225. return 0;
  226. }
  227. /*
  228. * Register our undef instruction hooks with ARM undef core.
  229. * We register a hook specifically looking for the KGB break inst
  230. * and we handle the normal undef case within the do_undefinstr
  231. * handler.
  232. */
  233. struct kgdb_arch arch_kgdb_ops = {
  234. #ifndef __ARMEB__
  235. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  236. #else /* ! __ARMEB__ */
  237. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  238. #endif
  239. };