kgdb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <asm/traps.h>
  16. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  17. {
  18. { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
  19. { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
  20. { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
  21. { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
  22. { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
  23. { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
  24. { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
  25. { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
  26. { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
  27. { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
  28. { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
  29. { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
  30. { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
  31. { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
  32. { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
  33. { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
  34. { "f0", 12, -1 },
  35. { "f1", 12, -1 },
  36. { "f2", 12, -1 },
  37. { "f3", 12, -1 },
  38. { "f4", 12, -1 },
  39. { "f5", 12, -1 },
  40. { "f6", 12, -1 },
  41. { "f7", 12, -1 },
  42. { "fps", 4, -1 },
  43. { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
  44. };
  45. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  46. {
  47. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  48. return NULL;
  49. if (dbg_reg_def[regno].offset != -1)
  50. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  51. dbg_reg_def[regno].size);
  52. else
  53. memset(mem, 0, dbg_reg_def[regno].size);
  54. return dbg_reg_def[regno].name;
  55. }
  56. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  57. {
  58. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  59. return -EINVAL;
  60. if (dbg_reg_def[regno].offset != -1)
  61. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  62. dbg_reg_def[regno].size);
  63. return 0;
  64. }
  65. void
  66. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  67. {
  68. struct pt_regs *thread_regs;
  69. int regno;
  70. /* Just making sure... */
  71. if (task == NULL)
  72. return;
  73. /* Initialize to zero */
  74. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  75. gdb_regs[regno] = 0;
  76. /* Otherwise, we have only some registers from switch_to() */
  77. thread_regs = task_pt_regs(task);
  78. gdb_regs[_R0] = thread_regs->ARM_r0;
  79. gdb_regs[_R1] = thread_regs->ARM_r1;
  80. gdb_regs[_R2] = thread_regs->ARM_r2;
  81. gdb_regs[_R3] = thread_regs->ARM_r3;
  82. gdb_regs[_R4] = thread_regs->ARM_r4;
  83. gdb_regs[_R5] = thread_regs->ARM_r5;
  84. gdb_regs[_R6] = thread_regs->ARM_r6;
  85. gdb_regs[_R7] = thread_regs->ARM_r7;
  86. gdb_regs[_R8] = thread_regs->ARM_r8;
  87. gdb_regs[_R9] = thread_regs->ARM_r9;
  88. gdb_regs[_R10] = thread_regs->ARM_r10;
  89. gdb_regs[_FP] = thread_regs->ARM_fp;
  90. gdb_regs[_IP] = thread_regs->ARM_ip;
  91. gdb_regs[_SPT] = thread_regs->ARM_sp;
  92. gdb_regs[_LR] = thread_regs->ARM_lr;
  93. gdb_regs[_PC] = thread_regs->ARM_pc;
  94. gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
  95. }
  96. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  97. {
  98. regs->ARM_pc = pc;
  99. }
  100. static int compiled_break;
  101. int kgdb_arch_handle_exception(int exception_vector, int signo,
  102. int err_code, char *remcom_in_buffer,
  103. char *remcom_out_buffer,
  104. struct pt_regs *linux_regs)
  105. {
  106. unsigned long addr;
  107. char *ptr;
  108. switch (remcom_in_buffer[0]) {
  109. case 'D':
  110. case 'k':
  111. case 'c':
  112. /*
  113. * Try to read optional parameter, pc unchanged if no parm.
  114. * If this was a compiled breakpoint, we need to move
  115. * to the next instruction or we will just breakpoint
  116. * over and over again.
  117. */
  118. ptr = &remcom_in_buffer[1];
  119. if (kgdb_hex2long(&ptr, &addr))
  120. linux_regs->ARM_pc = addr;
  121. else if (compiled_break == 1)
  122. linux_regs->ARM_pc += 4;
  123. compiled_break = 0;
  124. return 0;
  125. }
  126. return -1;
  127. }
  128. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
  129. {
  130. if (user_mode(regs))
  131. return -1;
  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. if (user_mode(regs))
  138. return -1;
  139. compiled_break = 1;
  140. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  141. return 0;
  142. }
  143. static struct undef_hook kgdb_brkpt_hook = {
  144. .instr_mask = 0xffffffff,
  145. .instr_val = KGDB_BREAKINST,
  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. .fn = kgdb_compiled_brk_fn
  152. };
  153. static void kgdb_call_nmi_hook(void *ignored)
  154. {
  155. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  156. }
  157. void kgdb_roundup_cpus(unsigned long flags)
  158. {
  159. local_irq_enable();
  160. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  161. local_irq_disable();
  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_hook);
  196. register_undef_hook(&kgdb_compiled_brkpt_hook);
  197. return 0;
  198. }
  199. /**
  200. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  201. *
  202. * This function will handle the uninitalization of any architecture
  203. * specific callbacks, for dynamic registration and unregistration.
  204. */
  205. void kgdb_arch_exit(void)
  206. {
  207. unregister_undef_hook(&kgdb_brkpt_hook);
  208. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  209. unregister_die_notifier(&kgdb_notifier);
  210. }
  211. /*
  212. * Register our undef instruction hooks with ARM undef core.
  213. * We regsiter a hook specifically looking for the KGB break inst
  214. * and we handle the normal undef case within the do_undefinstr
  215. * handler.
  216. */
  217. struct kgdb_arch arch_kgdb_ops = {
  218. #ifndef __ARMEB__
  219. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  220. #else /* ! __ARMEB__ */
  221. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  222. #endif
  223. };