kgdb.c 8.5 KB

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