kgdb.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/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. { "cs0", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs0)},
  70. { "cs1", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs1)},
  71. { "psp", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmpsp)},
  72. { "elr", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmel)},
  73. { "est", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmest)},
  74. { "badva", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmbadva)},
  75. { "restart_r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, restart_r0)},
  76. { "syscall_nr", GDB_SIZEOF_REG, offsetof(struct pt_regs, syscall_nr)},
  77. };
  78. struct kgdb_arch arch_kgdb_ops = {
  79. /* trap0(#0xDB) 0x0cdb0054 */
  80. .gdb_bpt_instr = {0x54, 0x00, 0xdb, 0x0c},
  81. };
  82. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  83. {
  84. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  85. return NULL;
  86. *((unsigned long *) mem) = *((unsigned long *) ((void *)regs +
  87. dbg_reg_def[regno].offset));
  88. return dbg_reg_def[regno].name;
  89. }
  90. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  91. {
  92. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  93. return -EINVAL;
  94. *((unsigned long *) ((void *)regs + dbg_reg_def[regno].offset)) =
  95. *((unsigned long *) mem);
  96. return 0;
  97. }
  98. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  99. {
  100. instruction_pointer(regs) = pc;
  101. }
  102. #ifdef CONFIG_SMP
  103. /**
  104. * kgdb_roundup_cpus - Get other CPUs into a holding pattern
  105. * @flags: Current IRQ state
  106. *
  107. * On SMP systems, we need to get the attention of the other CPUs
  108. * and get them be in a known state. This should do what is needed
  109. * to get the other CPUs to call kgdb_wait(). Note that on some arches,
  110. * the NMI approach is not used for rounding up all the CPUs. For example,
  111. * in case of MIPS, smp_call_function() is used to roundup CPUs. In
  112. * this case, we have to make sure that interrupts are enabled before
  113. * calling smp_call_function(). The argument to this function is
  114. * the flags that will be used when restoring the interrupts. There is
  115. * local_irq_save() call before kgdb_roundup_cpus().
  116. *
  117. * On non-SMP systems, this is not called.
  118. */
  119. static void hexagon_kgdb_nmi_hook(void *ignored)
  120. {
  121. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  122. }
  123. void kgdb_roundup_cpus(unsigned long flags)
  124. {
  125. local_irq_enable();
  126. smp_call_function(hexagon_kgdb_nmi_hook, NULL, 0);
  127. local_irq_disable();
  128. }
  129. #endif
  130. /* Not yet working */
  131. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
  132. struct task_struct *task)
  133. {
  134. struct pt_regs *thread_regs;
  135. if (task == NULL)
  136. return;
  137. /* Initialize to zero */
  138. memset(gdb_regs, 0, NUMREGBYTES);
  139. /* Otherwise, we have only some registers from switch_to() */
  140. thread_regs = task_pt_regs(task);
  141. gdb_regs[0] = thread_regs->r00;
  142. }
  143. /**
  144. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  145. * @vector: The error vector of the exception that happened.
  146. * @signo: The signal number of the exception that happened.
  147. * @err_code: The error code of the exception that happened.
  148. * @remcom_in_buffer: The buffer of the packet we have read.
  149. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  150. * @regs: The &struct pt_regs of the current process.
  151. *
  152. * This function MUST handle the 'c' and 's' command packets,
  153. * as well packets to set / remove a hardware breakpoint, if used.
  154. * If there are additional packets which the hardware needs to handle,
  155. * they are handled here. The code should return -1 if it wants to
  156. * process more packets, and a %0 or %1 if it wants to exit from the
  157. * kgdb callback.
  158. *
  159. * Not yet working.
  160. */
  161. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  162. char *remcom_in_buffer, char *remcom_out_buffer,
  163. struct pt_regs *linux_regs)
  164. {
  165. switch (remcom_in_buffer[0]) {
  166. case 's':
  167. case 'c':
  168. return 0;
  169. }
  170. /* Stay in the debugger. */
  171. return -1;
  172. }
  173. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  174. {
  175. /* cpu roundup */
  176. if (atomic_read(&kgdb_active) != -1) {
  177. kgdb_nmicallback(smp_processor_id(), args->regs);
  178. return NOTIFY_STOP;
  179. }
  180. if (user_mode(args->regs))
  181. return NOTIFY_DONE;
  182. if (kgdb_handle_exception(args->trapnr & 0xff, args->signr, args->err,
  183. args->regs))
  184. return NOTIFY_DONE;
  185. return NOTIFY_STOP;
  186. }
  187. static int
  188. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  189. {
  190. unsigned long flags;
  191. int ret;
  192. local_irq_save(flags);
  193. ret = __kgdb_notify(ptr, cmd);
  194. local_irq_restore(flags);
  195. return ret;
  196. }
  197. static struct notifier_block kgdb_notifier = {
  198. .notifier_call = kgdb_notify,
  199. /*
  200. * Lowest-prio notifier priority, we want to be notified last:
  201. */
  202. .priority = -INT_MAX,
  203. };
  204. /**
  205. * kgdb_arch_init - Perform any architecture specific initalization.
  206. *
  207. * This function will handle the initalization of any architecture
  208. * specific callbacks.
  209. */
  210. int kgdb_arch_init(void)
  211. {
  212. return register_die_notifier(&kgdb_notifier);
  213. }
  214. /**
  215. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  216. *
  217. * This function will handle the uninitalization of any architecture
  218. * specific callbacks, for dynamic registration and unregistration.
  219. */
  220. void kgdb_arch_exit(void)
  221. {
  222. unregister_die_notifier(&kgdb_notifier);
  223. }