kgdb.c 8.5 KB

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