kgdb.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/hexagon/kernel/kgdb.c - Hexagon KGDB Support
  4. *
  5. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/irq.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/kgdb.h>
  12. /* All registers are 4 bytes, for now */
  13. #define GDB_SIZEOF_REG 4
  14. /* The register names are used during printing of the regs;
  15. * Keep these at three letters to pretty-print. */
  16. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  17. { " r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, r00)},
  18. { " r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, r01)},
  19. { " r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, r02)},
  20. { " r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, r03)},
  21. { " r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, r04)},
  22. { " r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, r05)},
  23. { " r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, r06)},
  24. { " r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, r07)},
  25. { " r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, r08)},
  26. { " r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, r09)},
  27. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, r10)},
  28. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, r11)},
  29. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, r12)},
  30. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, r13)},
  31. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, r14)},
  32. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, r15)},
  33. { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, r16)},
  34. { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, r17)},
  35. { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, r18)},
  36. { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, r19)},
  37. { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, r20)},
  38. { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, r21)},
  39. { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, r22)},
  40. { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, r23)},
  41. { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, r24)},
  42. { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, r25)},
  43. { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, r26)},
  44. { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, r27)},
  45. { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, r28)},
  46. { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, r29)},
  47. { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, r30)},
  48. { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, r31)},
  49. { "usr", GDB_SIZEOF_REG, offsetof(struct pt_regs, usr)},
  50. { "preds", GDB_SIZEOF_REG, offsetof(struct pt_regs, preds)},
  51. { " m0", GDB_SIZEOF_REG, offsetof(struct pt_regs, m0)},
  52. { " m1", GDB_SIZEOF_REG, offsetof(struct pt_regs, m1)},
  53. { "sa0", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa0)},
  54. { "sa1", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa1)},
  55. { "lc0", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc0)},
  56. { "lc1", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc1)},
  57. { " gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
  58. { "ugp", GDB_SIZEOF_REG, offsetof(struct pt_regs, ugp)},
  59. { "cs0", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs0)},
  60. { "cs1", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs1)},
  61. { "psp", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmpsp)},
  62. { "elr", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmel)},
  63. { "est", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmest)},
  64. { "badva", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmbadva)},
  65. { "restart_r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, restart_r0)},
  66. { "syscall_nr", GDB_SIZEOF_REG, offsetof(struct pt_regs, syscall_nr)},
  67. };
  68. const struct kgdb_arch arch_kgdb_ops = {
  69. /* trap0(#0xDB) 0x0cdb0054 */
  70. .gdb_bpt_instr = {0x54, 0x00, 0xdb, 0x0c},
  71. };
  72. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  73. {
  74. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  75. return NULL;
  76. *((unsigned long *) mem) = *((unsigned long *) ((void *)regs +
  77. dbg_reg_def[regno].offset));
  78. return dbg_reg_def[regno].name;
  79. }
  80. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  81. {
  82. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  83. return -EINVAL;
  84. *((unsigned long *) ((void *)regs + dbg_reg_def[regno].offset)) =
  85. *((unsigned long *) mem);
  86. return 0;
  87. }
  88. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  89. {
  90. instruction_pointer(regs) = pc;
  91. }
  92. /* Not yet working */
  93. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
  94. struct task_struct *task)
  95. {
  96. struct pt_regs *thread_regs;
  97. if (task == NULL)
  98. return;
  99. /* Initialize to zero */
  100. memset(gdb_regs, 0, NUMREGBYTES);
  101. /* Otherwise, we have only some registers from switch_to() */
  102. thread_regs = task_pt_regs(task);
  103. gdb_regs[0] = thread_regs->r00;
  104. }
  105. /**
  106. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  107. * @vector: The error vector of the exception that happened.
  108. * @signo: The signal number of the exception that happened.
  109. * @err_code: The error code of the exception that happened.
  110. * @remcom_in_buffer: The buffer of the packet we have read.
  111. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  112. * @regs: The &struct pt_regs of the current process.
  113. *
  114. * This function MUST handle the 'c' and 's' command packets,
  115. * as well packets to set / remove a hardware breakpoint, if used.
  116. * If there are additional packets which the hardware needs to handle,
  117. * they are handled here. The code should return -1 if it wants to
  118. * process more packets, and a %0 or %1 if it wants to exit from the
  119. * kgdb callback.
  120. *
  121. * Not yet working.
  122. */
  123. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  124. char *remcom_in_buffer, char *remcom_out_buffer,
  125. struct pt_regs *linux_regs)
  126. {
  127. switch (remcom_in_buffer[0]) {
  128. case 's':
  129. case 'c':
  130. return 0;
  131. }
  132. /* Stay in the debugger. */
  133. return -1;
  134. }
  135. static int __kgdb_notify(struct die_args *args, unsigned long cmd)
  136. {
  137. /* cpu roundup */
  138. if (atomic_read(&kgdb_active) != -1) {
  139. kgdb_nmicallback(smp_processor_id(), args->regs);
  140. return NOTIFY_STOP;
  141. }
  142. if (user_mode(args->regs))
  143. return NOTIFY_DONE;
  144. if (kgdb_handle_exception(args->trapnr & 0xff, args->signr, args->err,
  145. args->regs))
  146. return NOTIFY_DONE;
  147. return NOTIFY_STOP;
  148. }
  149. static int
  150. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  151. {
  152. unsigned long flags;
  153. int ret;
  154. local_irq_save(flags);
  155. ret = __kgdb_notify(ptr, cmd);
  156. local_irq_restore(flags);
  157. return ret;
  158. }
  159. static struct notifier_block kgdb_notifier = {
  160. .notifier_call = kgdb_notify,
  161. /*
  162. * Lowest-prio notifier priority, we want to be notified last:
  163. */
  164. .priority = -INT_MAX,
  165. };
  166. /**
  167. * kgdb_arch_init - Perform any architecture specific initialization.
  168. *
  169. * This function will handle the initialization of any architecture
  170. * specific callbacks.
  171. */
  172. int kgdb_arch_init(void)
  173. {
  174. return register_die_notifier(&kgdb_notifier);
  175. }
  176. /**
  177. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  178. *
  179. * This function will handle the uninitalization of any architecture
  180. * specific callbacks, for dynamic registration and unregistration.
  181. */
  182. void kgdb_arch_exit(void)
  183. {
  184. unregister_die_notifier(&kgdb_notifier);
  185. }