kgdb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static int compiled_break;
  93. int kgdb_arch_handle_exception(int exception_vector, int signo,
  94. int err_code, char *remcom_in_buffer,
  95. char *remcom_out_buffer,
  96. struct pt_regs *linux_regs)
  97. {
  98. unsigned long addr;
  99. char *ptr;
  100. switch (remcom_in_buffer[0]) {
  101. case 'D':
  102. case 'k':
  103. case 'c':
  104. /*
  105. * Try to read optional parameter, pc unchanged if no parm.
  106. * If this was a compiled breakpoint, we need to move
  107. * to the next instruction or we will just breakpoint
  108. * over and over again.
  109. */
  110. ptr = &remcom_in_buffer[1];
  111. if (kgdb_hex2long(&ptr, &addr))
  112. linux_regs->ARM_pc = addr;
  113. else if (compiled_break == 1)
  114. linux_regs->ARM_pc += 4;
  115. compiled_break = 0;
  116. return 0;
  117. }
  118. return -1;
  119. }
  120. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
  121. {
  122. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  123. return 0;
  124. }
  125. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
  126. {
  127. compiled_break = 1;
  128. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  129. return 0;
  130. }
  131. static struct undef_hook kgdb_brkpt_hook = {
  132. .instr_mask = 0xffffffff,
  133. .instr_val = KGDB_BREAKINST,
  134. .fn = kgdb_brk_fn
  135. };
  136. static struct undef_hook kgdb_compiled_brkpt_hook = {
  137. .instr_mask = 0xffffffff,
  138. .instr_val = KGDB_COMPILED_BREAK,
  139. .fn = kgdb_compiled_brk_fn
  140. };
  141. static void kgdb_call_nmi_hook(void *ignored)
  142. {
  143. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  144. }
  145. void kgdb_roundup_cpus(unsigned long flags)
  146. {
  147. local_irq_enable();
  148. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  149. local_irq_disable();
  150. }
  151. /**
  152. * kgdb_arch_init - Perform any architecture specific initalization.
  153. *
  154. * This function will handle the initalization of any architecture
  155. * specific callbacks.
  156. */
  157. int kgdb_arch_init(void)
  158. {
  159. register_undef_hook(&kgdb_brkpt_hook);
  160. register_undef_hook(&kgdb_compiled_brkpt_hook);
  161. return 0;
  162. }
  163. /**
  164. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  165. *
  166. * This function will handle the uninitalization of any architecture
  167. * specific callbacks, for dynamic registration and unregistration.
  168. */
  169. void kgdb_arch_exit(void)
  170. {
  171. unregister_undef_hook(&kgdb_brkpt_hook);
  172. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  173. }
  174. /*
  175. * Register our undef instruction hooks with ARM undef core.
  176. * We regsiter a hook specifically looking for the KGB break inst
  177. * and we handle the normal undef case within the do_undefinstr
  178. * handler.
  179. */
  180. struct kgdb_arch arch_kgdb_ops = {
  181. #ifndef __ARMEB__
  182. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  183. #else /* ! __ARMEB__ */
  184. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  185. #endif
  186. };