kgdb_64.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* kgdb.c: KGDB support for 64-bit sparc.
  3. *
  4. * Copyright (C) 2008 David S. Miller <[email protected]>
  5. */
  6. #include <linux/kgdb.h>
  7. #include <linux/kdebug.h>
  8. #include <linux/ftrace.h>
  9. #include <linux/context_tracking.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/kdebug.h>
  12. #include <asm/ptrace.h>
  13. #include <asm/irq.h>
  14. #include "kernel.h"
  15. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  16. {
  17. struct reg_window *win;
  18. int i;
  19. gdb_regs[GDB_G0] = 0;
  20. for (i = 0; i < 15; i++)
  21. gdb_regs[GDB_G1 + i] = regs->u_regs[UREG_G1 + i];
  22. win = (struct reg_window *) (regs->u_regs[UREG_FP] + STACK_BIAS);
  23. for (i = 0; i < 8; i++)
  24. gdb_regs[GDB_L0 + i] = win->locals[i];
  25. for (i = 0; i < 8; i++)
  26. gdb_regs[GDB_I0 + i] = win->ins[i];
  27. for (i = GDB_F0; i <= GDB_F62; i++)
  28. gdb_regs[i] = 0;
  29. gdb_regs[GDB_PC] = regs->tpc;
  30. gdb_regs[GDB_NPC] = regs->tnpc;
  31. gdb_regs[GDB_STATE] = regs->tstate;
  32. gdb_regs[GDB_FSR] = 0;
  33. gdb_regs[GDB_FPRS] = 0;
  34. gdb_regs[GDB_Y] = regs->y;
  35. }
  36. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  37. {
  38. struct thread_info *t = task_thread_info(p);
  39. extern unsigned int switch_to_pc;
  40. extern unsigned int ret_from_fork;
  41. struct reg_window *win;
  42. unsigned long pc, cwp;
  43. int i;
  44. for (i = GDB_G0; i < GDB_G6; i++)
  45. gdb_regs[i] = 0;
  46. gdb_regs[GDB_G6] = (unsigned long) t;
  47. gdb_regs[GDB_G7] = (unsigned long) p;
  48. for (i = GDB_O0; i < GDB_SP; i++)
  49. gdb_regs[i] = 0;
  50. gdb_regs[GDB_SP] = t->ksp;
  51. gdb_regs[GDB_O7] = 0;
  52. win = (struct reg_window *) (t->ksp + STACK_BIAS);
  53. for (i = 0; i < 8; i++)
  54. gdb_regs[GDB_L0 + i] = win->locals[i];
  55. for (i = 0; i < 8; i++)
  56. gdb_regs[GDB_I0 + i] = win->ins[i];
  57. for (i = GDB_F0; i <= GDB_F62; i++)
  58. gdb_regs[i] = 0;
  59. if (t->new_child)
  60. pc = (unsigned long) &ret_from_fork;
  61. else
  62. pc = (unsigned long) &switch_to_pc;
  63. gdb_regs[GDB_PC] = pc;
  64. gdb_regs[GDB_NPC] = pc + 4;
  65. cwp = __thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP];
  66. gdb_regs[GDB_STATE] = (TSTATE_PRIV | TSTATE_IE | cwp);
  67. gdb_regs[GDB_FSR] = 0;
  68. gdb_regs[GDB_FPRS] = 0;
  69. gdb_regs[GDB_Y] = 0;
  70. }
  71. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  72. {
  73. struct reg_window *win;
  74. int i;
  75. for (i = 0; i < 15; i++)
  76. regs->u_regs[UREG_G1 + i] = gdb_regs[GDB_G1 + i];
  77. /* If the TSTATE register is changing, we have to preserve
  78. * the CWP field, otherwise window save/restore explodes.
  79. */
  80. if (regs->tstate != gdb_regs[GDB_STATE]) {
  81. unsigned long cwp = regs->tstate & TSTATE_CWP;
  82. regs->tstate = (gdb_regs[GDB_STATE] & ~TSTATE_CWP) | cwp;
  83. }
  84. regs->tpc = gdb_regs[GDB_PC];
  85. regs->tnpc = gdb_regs[GDB_NPC];
  86. regs->y = gdb_regs[GDB_Y];
  87. win = (struct reg_window *) (regs->u_regs[UREG_FP] + STACK_BIAS);
  88. for (i = 0; i < 8; i++)
  89. win->locals[i] = gdb_regs[GDB_L0 + i];
  90. for (i = 0; i < 8; i++)
  91. win->ins[i] = gdb_regs[GDB_I0 + i];
  92. }
  93. #ifdef CONFIG_SMP
  94. void __irq_entry smp_kgdb_capture_client(int irq, struct pt_regs *regs)
  95. {
  96. unsigned long flags;
  97. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  98. "wrpr %0, %1, %%pstate"
  99. : "=r" (flags)
  100. : "i" (PSTATE_IE));
  101. flushw_all();
  102. if (atomic_read(&kgdb_active) != -1)
  103. kgdb_nmicallback(raw_smp_processor_id(), regs);
  104. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  105. : : "r" (flags));
  106. }
  107. #endif
  108. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  109. char *remcomInBuffer, char *remcomOutBuffer,
  110. struct pt_regs *linux_regs)
  111. {
  112. unsigned long addr;
  113. char *ptr;
  114. switch (remcomInBuffer[0]) {
  115. case 'c':
  116. /* try to read optional parameter, pc unchanged if no parm */
  117. ptr = &remcomInBuffer[1];
  118. if (kgdb_hex2long(&ptr, &addr)) {
  119. linux_regs->tpc = addr;
  120. linux_regs->tnpc = addr + 4;
  121. }
  122. fallthrough;
  123. case 'D':
  124. case 'k':
  125. if (linux_regs->tpc == (unsigned long) arch_kgdb_breakpoint) {
  126. linux_regs->tpc = linux_regs->tnpc;
  127. linux_regs->tnpc += 4;
  128. }
  129. return 0;
  130. }
  131. return -1;
  132. }
  133. asmlinkage void kgdb_trap(unsigned long trap_level, struct pt_regs *regs)
  134. {
  135. enum ctx_state prev_state = exception_enter();
  136. unsigned long flags;
  137. if (user_mode(regs)) {
  138. bad_trap(regs, trap_level);
  139. goto out;
  140. }
  141. flushw_all();
  142. local_irq_save(flags);
  143. kgdb_handle_exception(0x172, SIGTRAP, 0, regs);
  144. local_irq_restore(flags);
  145. out:
  146. exception_exit(prev_state);
  147. }
  148. int kgdb_arch_init(void)
  149. {
  150. return 0;
  151. }
  152. void kgdb_arch_exit(void)
  153. {
  154. }
  155. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  156. {
  157. regs->tpc = ip;
  158. regs->tnpc = regs->tpc + 4;
  159. }
  160. const struct kgdb_arch arch_kgdb_ops = {
  161. /* Breakpoint instruction: ta 0x72 */
  162. .gdb_bpt_instr = { 0x91, 0xd0, 0x20, 0x72 },
  163. };