kgdb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * arch/hexagon/kernel/kgdb.c - Hexagon KGDB Support
  3. *
  4. * Copyright (c) 2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <linux/kdebug.h>
  21. #include <linux/kgdb.h>
  22. /* All registers are 4 bytes, for now */
  23. #define GDB_SIZEOF_REG 4
  24. /* The register names are used during printing of the regs;
  25. * Keep these at three letters to pretty-print. */
  26. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  27. { " r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, r00)},
  28. { " r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, r01)},
  29. { " r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, r02)},
  30. { " r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, r03)},
  31. { " r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, r04)},
  32. { " r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, r05)},
  33. { " r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, r06)},
  34. { " r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, r07)},
  35. { " r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, r08)},
  36. { " r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, r09)},
  37. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, r10)},
  38. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, r11)},
  39. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, r12)},
  40. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, r13)},
  41. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, r14)},
  42. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, r15)},
  43. { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, r16)},
  44. { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, r17)},
  45. { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, r18)},
  46. { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, r19)},
  47. { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, r20)},
  48. { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, r21)},
  49. { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, r22)},
  50. { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, r23)},
  51. { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, r24)},
  52. { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, r25)},
  53. { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, r26)},
  54. { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, r27)},
  55. { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, r28)},
  56. { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, r29)},
  57. { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, r30)},
  58. { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, r31)},
  59. { "usr", GDB_SIZEOF_REG, offsetof(struct pt_regs, usr)},
  60. { "preds", GDB_SIZEOF_REG, offsetof(struct pt_regs, preds)},
  61. { " m0", GDB_SIZEOF_REG, offsetof(struct pt_regs, m0)},
  62. { " m1", GDB_SIZEOF_REG, offsetof(struct pt_regs, m1)},
  63. { "sa0", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa0)},
  64. { "sa1", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa1)},
  65. { "lc0", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc0)},
  66. { "lc1", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc1)},
  67. { " gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
  68. { "ugp", GDB_SIZEOF_REG, offsetof(struct pt_regs, ugp)},
  69. { "psp", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmpsp)},
  70. { "elr", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmel)},
  71. { "est", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmest)},
  72. { "badva", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmbadva)},
  73. { "restart_r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, restart_r0)},
  74. { "syscall_nr", GDB_SIZEOF_REG, offsetof(struct pt_regs, syscall_nr)},
  75. };
  76. struct kgdb_arch arch_kgdb_ops = {
  77. /* trap0(#0xDB) 0x0cdb0054 */
  78. .gdb_bpt_instr = {0x54, 0x00, 0xdb, 0x0c},
  79. };
  80. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  81. {
  82. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  83. return NULL;
  84. *((unsigned long *) mem) = *((unsigned long *) ((void *)regs +
  85. dbg_reg_def[regno].offset));
  86. return dbg_reg_def[regno].name;
  87. }
  88. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  89. {
  90. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  91. return -EINVAL;
  92. *((unsigned long *) ((void *)regs + dbg_reg_def[regno].offset)) =
  93. *((unsigned long *) mem);
  94. return 0;
  95. }
  96. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  97. {
  98. instruction_pointer(regs) = pc;
  99. }
  100. #ifdef CONFIG_SMP
  101. /**
  102. * kgdb_roundup_cpus - Get other CPUs into a holding pattern
  103. * @flags: Current IRQ state
  104. *
  105. * On SMP systems, we need to get the attention of the other CPUs
  106. * and get them be in a known state. This should do what is needed
  107. * to get the other CPUs to call kgdb_wait(). Note that on some arches,
  108. * the NMI approach is not used for rounding up all the CPUs. For example,
  109. * in case of MIPS, smp_call_function() is used to roundup CPUs. In
  110. * this case, we have to make sure that interrupts are enabled before
  111. * calling smp_call_function(). The argument to this function is
  112. * the flags that will be used when restoring the interrupts. There is
  113. * local_irq_save() call before kgdb_roundup_cpus().
  114. *
  115. * On non-SMP systems, this is not called.
  116. */
  117. static void hexagon_kgdb_nmi_hook(void *ignored)
  118. {
  119. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  120. }
  121. void kgdb_roundup_cpus(unsigned long flags)
  122. {
  123. local_irq_enable();
  124. smp_call_function(hexagon_kgdb_nmi_hook, NULL, 0);
  125. local_irq_disable();
  126. }
  127. #endif
  128. /* Not yet working */
  129. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
  130. struct task_struct *task)
  131. {
  132. struct pt_regs *thread_regs;
  133. if (task == NULL)
  134. return;
  135. /* Initialize to zero */
  136. memset(gdb_regs, 0, NUMREGBYTES);
  137. /* Otherwise, we have only some registers from switch_to() */
  138. thread_regs = task_pt_regs(task);
  139. gdb_regs[0] = thread_regs->r00;
  140. }
  141. /**
  142. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  143. * @vector: The error vector of the exception that happened.
  144. * @signo: The signal number of the exception that happened.
  145. * @err_code: The error code of the exception that happened.
  146. * @remcom_in_buffer: The buffer of the packet we have read.
  147. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  148. * @regs: The &struct pt_regs of the current process.
  149. *
  150. * This function MUST handle the 'c' and 's' command packets,
  151. * as well packets to set / remove a hardware breakpoint, if used.
  152. * If there are additional packets which the hardware needs to handle,
  153. * they are handled here. The code should return -1 if it wants to
  154. * process more packets, and a %0 or %1 if it wants to exit from the
  155. * kgdb callback.
  156. *
  157. * Not yet working.
  158. */
  159. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  160. char *remcom_in_buffer, char *remcom_out_buffer,
  161. struct pt_regs *linux_regs)
  162. {
  163. switch (remcom_in_buffer[0]) {
  164. case 's':
  165. case 'c':
  166. return 0;
  167. }
  168. /* Stay in the debugger. */
  169. return -1;
  170. }
  171. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  172. {
  173. /* cpu roundup */
  174. if (atomic_read(&kgdb_active) != -1) {
  175. kgdb_nmicallback(smp_processor_id(), args->regs);
  176. return NOTIFY_STOP;
  177. }
  178. if (user_mode(args->regs))
  179. return NOTIFY_DONE;
  180. if (kgdb_handle_exception(args->trapnr & 0xff, args->signr, args->err,
  181. args->regs))
  182. return NOTIFY_DONE;
  183. return NOTIFY_STOP;
  184. }
  185. static int
  186. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  187. {
  188. unsigned long flags;
  189. int ret;
  190. local_irq_save(flags);
  191. ret = __kgdb_notify(ptr, cmd);
  192. local_irq_restore(flags);
  193. return ret;
  194. }
  195. static struct notifier_block kgdb_notifier = {
  196. .notifier_call = kgdb_notify,
  197. /*
  198. * Lowest-prio notifier priority, we want to be notified last:
  199. */
  200. .priority = -INT_MAX,
  201. };
  202. /**
  203. * kgdb_arch_init - Perform any architecture specific initalization.
  204. *
  205. * This function will handle the initalization of any architecture
  206. * specific callbacks.
  207. */
  208. int kgdb_arch_init(void)
  209. {
  210. return register_die_notifier(&kgdb_notifier);
  211. }
  212. /**
  213. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  214. *
  215. * This function will handle the uninitalization of any architecture
  216. * specific callbacks, for dynamic registration and unregistration.
  217. */
  218. void kgdb_arch_exit(void)
  219. {
  220. unregister_die_notifier(&kgdb_notifier);
  221. }