kgdb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/kgdb.h>
  13. #include <asm/traps.h>
  14. /* Make a local copy of the registers passed into the handler (bletch) */
  15. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
  16. {
  17. int regno;
  18. /* Initialize all to zero. */
  19. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  20. gdb_regs[regno] = 0;
  21. gdb_regs[_R0] = kernel_regs->ARM_r0;
  22. gdb_regs[_R1] = kernel_regs->ARM_r1;
  23. gdb_regs[_R2] = kernel_regs->ARM_r2;
  24. gdb_regs[_R3] = kernel_regs->ARM_r3;
  25. gdb_regs[_R4] = kernel_regs->ARM_r4;
  26. gdb_regs[_R5] = kernel_regs->ARM_r5;
  27. gdb_regs[_R6] = kernel_regs->ARM_r6;
  28. gdb_regs[_R7] = kernel_regs->ARM_r7;
  29. gdb_regs[_R8] = kernel_regs->ARM_r8;
  30. gdb_regs[_R9] = kernel_regs->ARM_r9;
  31. gdb_regs[_R10] = kernel_regs->ARM_r10;
  32. gdb_regs[_FP] = kernel_regs->ARM_fp;
  33. gdb_regs[_IP] = kernel_regs->ARM_ip;
  34. gdb_regs[_SPT] = kernel_regs->ARM_sp;
  35. gdb_regs[_LR] = kernel_regs->ARM_lr;
  36. gdb_regs[_PC] = kernel_regs->ARM_pc;
  37. gdb_regs[_CPSR] = kernel_regs->ARM_cpsr;
  38. }
  39. /* Copy local gdb registers back to kgdb regs, for later copy to kernel */
  40. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
  41. {
  42. kernel_regs->ARM_r0 = gdb_regs[_R0];
  43. kernel_regs->ARM_r1 = gdb_regs[_R1];
  44. kernel_regs->ARM_r2 = gdb_regs[_R2];
  45. kernel_regs->ARM_r3 = gdb_regs[_R3];
  46. kernel_regs->ARM_r4 = gdb_regs[_R4];
  47. kernel_regs->ARM_r5 = gdb_regs[_R5];
  48. kernel_regs->ARM_r6 = gdb_regs[_R6];
  49. kernel_regs->ARM_r7 = gdb_regs[_R7];
  50. kernel_regs->ARM_r8 = gdb_regs[_R8];
  51. kernel_regs->ARM_r9 = gdb_regs[_R9];
  52. kernel_regs->ARM_r10 = gdb_regs[_R10];
  53. kernel_regs->ARM_fp = gdb_regs[_FP];
  54. kernel_regs->ARM_ip = gdb_regs[_IP];
  55. kernel_regs->ARM_sp = gdb_regs[_SPT];
  56. kernel_regs->ARM_lr = gdb_regs[_LR];
  57. kernel_regs->ARM_pc = gdb_regs[_PC];
  58. kernel_regs->ARM_cpsr = gdb_regs[_CPSR];
  59. }
  60. void
  61. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  62. {
  63. struct pt_regs *thread_regs;
  64. int regno;
  65. /* Just making sure... */
  66. if (task == NULL)
  67. return;
  68. /* Initialize to zero */
  69. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  70. gdb_regs[regno] = 0;
  71. /* Otherwise, we have only some registers from switch_to() */
  72. thread_regs = task_pt_regs(task);
  73. gdb_regs[_R0] = thread_regs->ARM_r0;
  74. gdb_regs[_R1] = thread_regs->ARM_r1;
  75. gdb_regs[_R2] = thread_regs->ARM_r2;
  76. gdb_regs[_R3] = thread_regs->ARM_r3;
  77. gdb_regs[_R4] = thread_regs->ARM_r4;
  78. gdb_regs[_R5] = thread_regs->ARM_r5;
  79. gdb_regs[_R6] = thread_regs->ARM_r6;
  80. gdb_regs[_R7] = thread_regs->ARM_r7;
  81. gdb_regs[_R8] = thread_regs->ARM_r8;
  82. gdb_regs[_R9] = thread_regs->ARM_r9;
  83. gdb_regs[_R10] = thread_regs->ARM_r10;
  84. gdb_regs[_FP] = thread_regs->ARM_fp;
  85. gdb_regs[_IP] = thread_regs->ARM_ip;
  86. gdb_regs[_SPT] = thread_regs->ARM_sp;
  87. gdb_regs[_LR] = thread_regs->ARM_lr;
  88. gdb_regs[_PC] = thread_regs->ARM_pc;
  89. gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
  90. }
  91. static int compiled_break;
  92. int kgdb_arch_handle_exception(int exception_vector, int signo,
  93. int err_code, char *remcom_in_buffer,
  94. char *remcom_out_buffer,
  95. struct pt_regs *linux_regs)
  96. {
  97. unsigned long addr;
  98. char *ptr;
  99. switch (remcom_in_buffer[0]) {
  100. case 'D':
  101. case 'k':
  102. case 'c':
  103. /*
  104. * Try to read optional parameter, pc unchanged if no parm.
  105. * If this was a compiled breakpoint, we need to move
  106. * to the next instruction or we will just breakpoint
  107. * over and over again.
  108. */
  109. ptr = &remcom_in_buffer[1];
  110. if (kgdb_hex2long(&ptr, &addr))
  111. linux_regs->ARM_pc = addr;
  112. else if (compiled_break == 1)
  113. linux_regs->ARM_pc += 4;
  114. compiled_break = 0;
  115. return 0;
  116. }
  117. return -1;
  118. }
  119. static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
  120. {
  121. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  122. return 0;
  123. }
  124. static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
  125. {
  126. compiled_break = 1;
  127. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  128. return 0;
  129. }
  130. static struct undef_hook kgdb_brkpt_hook = {
  131. .instr_mask = 0xffffffff,
  132. .instr_val = KGDB_BREAKINST,
  133. .fn = kgdb_brk_fn
  134. };
  135. static struct undef_hook kgdb_compiled_brkpt_hook = {
  136. .instr_mask = 0xffffffff,
  137. .instr_val = KGDB_COMPILED_BREAK,
  138. .fn = kgdb_compiled_brk_fn
  139. };
  140. /**
  141. * kgdb_arch_init - Perform any architecture specific initalization.
  142. *
  143. * This function will handle the initalization of any architecture
  144. * specific callbacks.
  145. */
  146. int kgdb_arch_init(void)
  147. {
  148. register_undef_hook(&kgdb_brkpt_hook);
  149. register_undef_hook(&kgdb_compiled_brkpt_hook);
  150. return 0;
  151. }
  152. /**
  153. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  154. *
  155. * This function will handle the uninitalization of any architecture
  156. * specific callbacks, for dynamic registration and unregistration.
  157. */
  158. void kgdb_arch_exit(void)
  159. {
  160. unregister_undef_hook(&kgdb_brkpt_hook);
  161. unregister_undef_hook(&kgdb_compiled_brkpt_hook);
  162. }
  163. /*
  164. * Register our undef instruction hooks with ARM undef core.
  165. * We regsiter a hook specifically looking for the KGB break inst
  166. * and we handle the normal undef case within the do_undefinstr
  167. * handler.
  168. */
  169. struct kgdb_arch arch_kgdb_ops = {
  170. #ifndef __ARMEB__
  171. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
  172. #else /* ! __ARMEB__ */
  173. .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
  174. #endif
  175. };