kgdb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/kgdb.h>
  14. #include <asm/traps.h>
  15. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  16. {
  17. { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
  18. { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
  19. { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
  20. { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
  21. { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
  22. { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
  23. { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
  24. { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
  25. { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
  26. { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
  27. { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
  28. { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
  29. { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
  30. { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
  31. { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
  32. { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
  33. { "f0", 12, -1 },
  34. { "f1", 12, -1 },
  35. { "f2", 12, -1 },
  36. { "f3", 12, -1 },
  37. { "f4", 12, -1 },
  38. { "f5", 12, -1 },
  39. { "f6", 12, -1 },
  40. { "f7", 12, -1 },
  41. { "fps", 4, -1 },
  42. { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
  43. };
  44. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  45. {
  46. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  47. return NULL;
  48. if (dbg_reg_def[regno].offset != -1)
  49. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  50. dbg_reg_def[regno].size);
  51. else
  52. memset(mem, 0, dbg_reg_def[regno].size);
  53. return dbg_reg_def[regno].name;
  54. }
  55. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  56. {
  57. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  58. return -EINVAL;
  59. if (dbg_reg_def[regno].offset != -1)
  60. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  61. dbg_reg_def[regno].size);
  62. return 0;
  63. }
  64. void
  65. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  66. {
  67. struct pt_regs *thread_regs;
  68. int regno;
  69. /* Just making sure... */
  70. if (task == NULL)
  71. return;
  72. /* Initialize to zero */
  73. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  74. gdb_regs[regno] = 0;
  75. /* Otherwise, we have only some registers from switch_to() */
  76. thread_regs = task_pt_regs(task);
  77. gdb_regs[_R0] = thread_regs->ARM_r0;
  78. gdb_regs[_R1] = thread_regs->ARM_r1;
  79. gdb_regs[_R2] = thread_regs->ARM_r2;
  80. gdb_regs[_R3] = thread_regs->ARM_r3;
  81. gdb_regs[_R4] = thread_regs->ARM_r4;
  82. gdb_regs[_R5] = thread_regs->ARM_r5;
  83. gdb_regs[_R6] = thread_regs->ARM_r6;
  84. gdb_regs[_R7] = thread_regs->ARM_r7;
  85. gdb_regs[_R8] = thread_regs->ARM_r8;
  86. gdb_regs[_R9] = thread_regs->ARM_r9;
  87. gdb_regs[_R10] = thread_regs->ARM_r10;
  88. gdb_regs[_FP] = thread_regs->ARM_fp;
  89. gdb_regs[_IP] = thread_regs->ARM_ip;
  90. gdb_regs[_SPT] = thread_regs->ARM_sp;
  91. gdb_regs[_LR] = thread_regs->ARM_lr;
  92. gdb_regs[_PC] = thread_regs->ARM_pc;
  93. gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
  94. }
  95. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  96. {
  97. regs->ARM_pc = pc;
  98. }
  99. static int compiled_break;
  100. int kgdb_arch_handle_exception(int exception_vector, int signo,
  101. int err_code, char *remcom_in_buffer,
  102. char *remcom_out_buffer,
  103. struct pt_regs *linux_regs)
  104. {
  105. unsigned long addr;
  106. char *ptr;
  107. switch (remcom_in_buffer[0]) {
  108. case 'D':
  109. case 'k':
  110. case 'c':
  111. /*
  112. * Try to read optional parameter, pc unchanged if no parm.
  113. * If this was a compiled breakpoint, we need to move
  114. * to the next instruction or we will just breakpoint
  115. * over and over again.
  116. */
  117. ptr = &remcom_in_buffer[1];
  118. if (kgdb_hex2long(&ptr, &addr))
  119. linux_regs->ARM_pc = addr;
  120. else if (compiled_break == 1)
  121. linux_regs->ARM_pc += 4;
  122. compiled_break = 0;
  123. return 0;
  124. }
  125. return -1;
  126. }
  127. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
  128. {
  129. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  130. return 0;
  131. }
  132. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
  133. {
  134. compiled_break = 1;
  135. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  136. return 0;
  137. }
  138. static struct undef_hook kgdb_brkpt_hook = {
  139. .instr_mask = 0xffffffff,
  140. .instr_val = KGDB_BREAKINST,
  141. .fn = kgdb_brk_fn
  142. };
  143. static struct undef_hook kgdb_compiled_brkpt_hook = {
  144. .instr_mask = 0xffffffff,
  145. .instr_val = KGDB_COMPILED_BREAK,
  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. /**
  159. * kgdb_arch_init - Perform any architecture specific initalization.
  160. *
  161. * This function will handle the initalization of any architecture
  162. * specific callbacks.
  163. */
  164. int kgdb_arch_init(void)
  165. {
  166. register_undef_hook(&kgdb_brkpt_hook);
  167. register_undef_hook(&kgdb_compiled_brkpt_hook);
  168. return 0;
  169. }
  170. /**
  171. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  172. *
  173. * This function will handle the uninitalization of any architecture
  174. * specific callbacks, for dynamic registration and unregistration.
  175. */
  176. void kgdb_arch_exit(void)
  177. {
  178. unregister_undef_hook(&kgdb_brkpt_hook);
  179. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  180. }
  181. /*
  182. * Register our undef instruction hooks with ARM undef core.
  183. * We regsiter a hook specifically looking for the KGB break inst
  184. * and we handle the normal undef case within the do_undefinstr
  185. * handler.
  186. */
  187. struct kgdb_arch arch_kgdb_ops = {
  188. #ifndef __ARMEB__
  189. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  190. #else /* ! __ARMEB__ */
  191. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  192. #endif
  193. };