kgdb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 thread_info *ti;
  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. ti = task_thread_info(task);
  80. gdb_regs[_R4] = ti->cpu_context.r4;
  81. gdb_regs[_R5] = ti->cpu_context.r5;
  82. gdb_regs[_R6] = ti->cpu_context.r6;
  83. gdb_regs[_R7] = ti->cpu_context.r7;
  84. gdb_regs[_R8] = ti->cpu_context.r8;
  85. gdb_regs[_R9] = ti->cpu_context.r9;
  86. gdb_regs[_R10] = ti->cpu_context.sl;
  87. gdb_regs[_FP] = ti->cpu_context.fp;
  88. gdb_regs[_SPT] = ti->cpu_context.sp;
  89. gdb_regs[_PC] = ti->cpu_context.pc;
  90. }
  91. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  92. {
  93. regs->ARM_pc = pc;
  94. }
  95. static int compiled_break;
  96. int kgdb_arch_handle_exception(int exception_vector, int signo,
  97. int err_code, char *remcom_in_buffer,
  98. char *remcom_out_buffer,
  99. struct pt_regs *linux_regs)
  100. {
  101. unsigned long addr;
  102. char *ptr;
  103. switch (remcom_in_buffer[0]) {
  104. case 'D':
  105. case 'k':
  106. case 'c':
  107. /*
  108. * Try to read optional parameter, pc unchanged if no parm.
  109. * If this was a compiled breakpoint, we need to move
  110. * to the next instruction or we will just breakpoint
  111. * over and over again.
  112. */
  113. ptr = &remcom_in_buffer[1];
  114. if (kgdb_hex2long(&ptr, &addr))
  115. linux_regs->ARM_pc = addr;
  116. else if (compiled_break == 1)
  117. linux_regs->ARM_pc += 4;
  118. compiled_break = 0;
  119. return 0;
  120. }
  121. return -1;
  122. }
  123. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
  124. {
  125. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  126. return 0;
  127. }
  128. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
  129. {
  130. compiled_break = 1;
  131. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  132. return 0;
  133. }
  134. static struct undef_hook kgdb_brkpt_hook = {
  135. .instr_mask = 0xffffffff,
  136. .instr_val = KGDB_BREAKINST,
  137. .cpsr_mask = MODE_MASK,
  138. .cpsr_val = SVC_MODE,
  139. .fn = kgdb_brk_fn
  140. };
  141. static struct undef_hook kgdb_compiled_brkpt_hook = {
  142. .instr_mask = 0xffffffff,
  143. .instr_val = KGDB_COMPILED_BREAK,
  144. .cpsr_mask = MODE_MASK,
  145. .cpsr_val = SVC_MODE,
  146. .fn = kgdb_compiled_brk_fn
  147. };
  148. static void kgdb_call_nmi_hook(void *ignored)
  149. {
  150. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  151. }
  152. void kgdb_roundup_cpus(unsigned long flags)
  153. {
  154. local_irq_enable();
  155. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  156. local_irq_disable();
  157. }
  158. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  159. {
  160. struct pt_regs *regs = args->regs;
  161. if (kgdb_handle_exception(1, args->signr, cmd, regs))
  162. return NOTIFY_DONE;
  163. return NOTIFY_STOP;
  164. }
  165. static int
  166. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  167. {
  168. unsigned long flags;
  169. int ret;
  170. local_irq_save(flags);
  171. ret = __kgdb_notify(ptr, cmd);
  172. local_irq_restore(flags);
  173. return ret;
  174. }
  175. static struct notifier_block kgdb_notifier = {
  176. .notifier_call = kgdb_notify,
  177. .priority = -INT_MAX,
  178. };
  179. /**
  180. * kgdb_arch_init - Perform any architecture specific initalization.
  181. *
  182. * This function will handle the initalization of any architecture
  183. * specific callbacks.
  184. */
  185. int kgdb_arch_init(void)
  186. {
  187. int ret = register_die_notifier(&kgdb_notifier);
  188. if (ret != 0)
  189. return ret;
  190. register_undef_hook(&kgdb_brkpt_hook);
  191. register_undef_hook(&kgdb_compiled_brkpt_hook);
  192. return 0;
  193. }
  194. /**
  195. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  196. *
  197. * This function will handle the uninitalization of any architecture
  198. * specific callbacks, for dynamic registration and unregistration.
  199. */
  200. void kgdb_arch_exit(void)
  201. {
  202. unregister_undef_hook(&kgdb_brkpt_hook);
  203. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  204. unregister_die_notifier(&kgdb_notifier);
  205. }
  206. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  207. {
  208. int err;
  209. /* patch_text() only supports int-sized breakpoints */
  210. BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
  211. err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
  212. BREAK_INSTR_SIZE);
  213. if (err)
  214. return err;
  215. /* Machine is already stopped, so we can use __patch_text() directly */
  216. __patch_text((void *)bpt->bpt_addr,
  217. *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
  218. return err;
  219. }
  220. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  221. {
  222. /* Machine is already stopped, so we can use __patch_text() directly */
  223. __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
  224. return 0;
  225. }
  226. /*
  227. * Register our undef instruction hooks with ARM undef core.
  228. * We regsiter a hook specifically looking for the KGB break inst
  229. * and we handle the normal undef case within the do_undefinstr
  230. * handler.
  231. */
  232. struct kgdb_arch arch_kgdb_ops = {
  233. #ifndef __ARMEB__
  234. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  235. #else /* ! __ARMEB__ */
  236. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  237. #endif
  238. };