kgdb.c 7.1 KB

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