kgdb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* Make a local copy of the registers passed into the handler (bletch) */
  16. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
  17. {
  18. int regno;
  19. /* Initialize all to zero. */
  20. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  21. gdb_regs[regno] = 0;
  22. gdb_regs[_R0] = kernel_regs->ARM_r0;
  23. gdb_regs[_R1] = kernel_regs->ARM_r1;
  24. gdb_regs[_R2] = kernel_regs->ARM_r2;
  25. gdb_regs[_R3] = kernel_regs->ARM_r3;
  26. gdb_regs[_R4] = kernel_regs->ARM_r4;
  27. gdb_regs[_R5] = kernel_regs->ARM_r5;
  28. gdb_regs[_R6] = kernel_regs->ARM_r6;
  29. gdb_regs[_R7] = kernel_regs->ARM_r7;
  30. gdb_regs[_R8] = kernel_regs->ARM_r8;
  31. gdb_regs[_R9] = kernel_regs->ARM_r9;
  32. gdb_regs[_R10] = kernel_regs->ARM_r10;
  33. gdb_regs[_FP] = kernel_regs->ARM_fp;
  34. gdb_regs[_IP] = kernel_regs->ARM_ip;
  35. gdb_regs[_SPT] = kernel_regs->ARM_sp;
  36. gdb_regs[_LR] = kernel_regs->ARM_lr;
  37. gdb_regs[_PC] = kernel_regs->ARM_pc;
  38. gdb_regs[_CPSR] = kernel_regs->ARM_cpsr;
  39. }
  40. /* Copy local gdb registers back to kgdb regs, for later copy to kernel */
  41. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
  42. {
  43. kernel_regs->ARM_r0 = gdb_regs[_R0];
  44. kernel_regs->ARM_r1 = gdb_regs[_R1];
  45. kernel_regs->ARM_r2 = gdb_regs[_R2];
  46. kernel_regs->ARM_r3 = gdb_regs[_R3];
  47. kernel_regs->ARM_r4 = gdb_regs[_R4];
  48. kernel_regs->ARM_r5 = gdb_regs[_R5];
  49. kernel_regs->ARM_r6 = gdb_regs[_R6];
  50. kernel_regs->ARM_r7 = gdb_regs[_R7];
  51. kernel_regs->ARM_r8 = gdb_regs[_R8];
  52. kernel_regs->ARM_r9 = gdb_regs[_R9];
  53. kernel_regs->ARM_r10 = gdb_regs[_R10];
  54. kernel_regs->ARM_fp = gdb_regs[_FP];
  55. kernel_regs->ARM_ip = gdb_regs[_IP];
  56. kernel_regs->ARM_sp = gdb_regs[_SPT];
  57. kernel_regs->ARM_lr = gdb_regs[_LR];
  58. kernel_regs->ARM_pc = gdb_regs[_PC];
  59. kernel_regs->ARM_cpsr = gdb_regs[_CPSR];
  60. }
  61. void
  62. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  63. {
  64. struct pt_regs *thread_regs;
  65. int regno;
  66. /* Just making sure... */
  67. if (task == NULL)
  68. return;
  69. /* Initialize to zero */
  70. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  71. gdb_regs[regno] = 0;
  72. /* Otherwise, we have only some registers from switch_to() */
  73. thread_regs = task_pt_regs(task);
  74. gdb_regs[_R0] = thread_regs->ARM_r0;
  75. gdb_regs[_R1] = thread_regs->ARM_r1;
  76. gdb_regs[_R2] = thread_regs->ARM_r2;
  77. gdb_regs[_R3] = thread_regs->ARM_r3;
  78. gdb_regs[_R4] = thread_regs->ARM_r4;
  79. gdb_regs[_R5] = thread_regs->ARM_r5;
  80. gdb_regs[_R6] = thread_regs->ARM_r6;
  81. gdb_regs[_R7] = thread_regs->ARM_r7;
  82. gdb_regs[_R8] = thread_regs->ARM_r8;
  83. gdb_regs[_R9] = thread_regs->ARM_r9;
  84. gdb_regs[_R10] = thread_regs->ARM_r10;
  85. gdb_regs[_FP] = thread_regs->ARM_fp;
  86. gdb_regs[_IP] = thread_regs->ARM_ip;
  87. gdb_regs[_SPT] = thread_regs->ARM_sp;
  88. gdb_regs[_LR] = thread_regs->ARM_lr;
  89. gdb_regs[_PC] = thread_regs->ARM_pc;
  90. gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
  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. .fn = kgdb_brk_fn
  139. };
  140. static struct undef_hook kgdb_compiled_brkpt_hook = {
  141. .instr_mask = 0xffffffff,
  142. .instr_val = KGDB_COMPILED_BREAK,
  143. .fn = kgdb_compiled_brk_fn
  144. };
  145. static void kgdb_call_nmi_hook(void *ignored)
  146. {
  147. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  148. }
  149. void kgdb_roundup_cpus(unsigned long flags)
  150. {
  151. local_irq_enable();
  152. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  153. local_irq_disable();
  154. }
  155. /**
  156. * kgdb_arch_init - Perform any architecture specific initalization.
  157. *
  158. * This function will handle the initalization of any architecture
  159. * specific callbacks.
  160. */
  161. int kgdb_arch_init(void)
  162. {
  163. register_undef_hook(&kgdb_brkpt_hook);
  164. register_undef_hook(&kgdb_compiled_brkpt_hook);
  165. return 0;
  166. }
  167. /**
  168. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  169. *
  170. * This function will handle the uninitalization of any architecture
  171. * specific callbacks, for dynamic registration and unregistration.
  172. */
  173. void kgdb_arch_exit(void)
  174. {
  175. unregister_undef_hook(&kgdb_brkpt_hook);
  176. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  177. }
  178. /*
  179. * Register our undef instruction hooks with ARM undef core.
  180. * We regsiter a hook specifically looking for the KGB break inst
  181. * and we handle the normal undef case within the do_undefinstr
  182. * handler.
  183. */
  184. struct kgdb_arch arch_kgdb_ops = {
  185. #ifndef __ARMEB__
  186. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  187. #else /* ! __ARMEB__ */
  188. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  189. #endif
  190. };