kgdb_32.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* kgdb.c: KGDB support for 32-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/sched.h>
  9. #include <asm/kdebug.h>
  10. #include <asm/ptrace.h>
  11. #include <asm/irq.h>
  12. #include <asm/cacheflush.h>
  13. #include "kernel.h"
  14. #include "entry.h"
  15. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  16. {
  17. struct reg_window32 *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_window32 *) regs->u_regs[UREG_FP];
  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_F31; i++)
  28. gdb_regs[i] = 0;
  29. gdb_regs[GDB_Y] = regs->y;
  30. gdb_regs[GDB_PSR] = regs->psr;
  31. gdb_regs[GDB_WIM] = 0;
  32. gdb_regs[GDB_TBR] = (unsigned long) &trapbase;
  33. gdb_regs[GDB_PC] = regs->pc;
  34. gdb_regs[GDB_NPC] = regs->npc;
  35. gdb_regs[GDB_FSR] = 0;
  36. gdb_regs[GDB_CSR] = 0;
  37. }
  38. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  39. {
  40. struct thread_info *t = task_thread_info(p);
  41. struct reg_window32 *win;
  42. int i;
  43. for (i = GDB_G0; i < GDB_G6; i++)
  44. gdb_regs[i] = 0;
  45. gdb_regs[GDB_G6] = (unsigned long) t;
  46. gdb_regs[GDB_G7] = 0;
  47. for (i = GDB_O0; i < GDB_SP; i++)
  48. gdb_regs[i] = 0;
  49. gdb_regs[GDB_SP] = t->ksp;
  50. gdb_regs[GDB_O7] = 0;
  51. win = (struct reg_window32 *) t->ksp;
  52. for (i = 0; i < 8; i++)
  53. gdb_regs[GDB_L0 + i] = win->locals[i];
  54. for (i = 0; i < 8; i++)
  55. gdb_regs[GDB_I0 + i] = win->ins[i];
  56. for (i = GDB_F0; i <= GDB_F31; i++)
  57. gdb_regs[i] = 0;
  58. gdb_regs[GDB_Y] = 0;
  59. gdb_regs[GDB_PSR] = t->kpsr;
  60. gdb_regs[GDB_WIM] = t->kwim;
  61. gdb_regs[GDB_TBR] = (unsigned long) &trapbase;
  62. gdb_regs[GDB_PC] = t->kpc;
  63. gdb_regs[GDB_NPC] = t->kpc + 4;
  64. gdb_regs[GDB_FSR] = 0;
  65. gdb_regs[GDB_CSR] = 0;
  66. }
  67. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  68. {
  69. struct reg_window32 *win;
  70. int i;
  71. for (i = 0; i < 15; i++)
  72. regs->u_regs[UREG_G1 + i] = gdb_regs[GDB_G1 + i];
  73. /* If the PSR register is changing, we have to preserve
  74. * the CWP field, otherwise window save/restore explodes.
  75. */
  76. if (regs->psr != gdb_regs[GDB_PSR]) {
  77. unsigned long cwp = regs->psr & PSR_CWP;
  78. regs->psr = (gdb_regs[GDB_PSR] & ~PSR_CWP) | cwp;
  79. }
  80. regs->pc = gdb_regs[GDB_PC];
  81. regs->npc = gdb_regs[GDB_NPC];
  82. regs->y = gdb_regs[GDB_Y];
  83. win = (struct reg_window32 *) regs->u_regs[UREG_FP];
  84. for (i = 0; i < 8; i++)
  85. win->locals[i] = gdb_regs[GDB_L0 + i];
  86. for (i = 0; i < 8; i++)
  87. win->ins[i] = gdb_regs[GDB_I0 + i];
  88. }
  89. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  90. char *remcomInBuffer, char *remcomOutBuffer,
  91. struct pt_regs *linux_regs)
  92. {
  93. unsigned long addr;
  94. char *ptr;
  95. switch (remcomInBuffer[0]) {
  96. case 'c':
  97. /* try to read optional parameter, pc unchanged if no parm */
  98. ptr = &remcomInBuffer[1];
  99. if (kgdb_hex2long(&ptr, &addr)) {
  100. linux_regs->pc = addr;
  101. linux_regs->npc = addr + 4;
  102. }
  103. fallthrough;
  104. case 'D':
  105. case 'k':
  106. if (linux_regs->pc == (unsigned long) arch_kgdb_breakpoint) {
  107. linux_regs->pc = linux_regs->npc;
  108. linux_regs->npc += 4;
  109. }
  110. return 0;
  111. }
  112. return -1;
  113. }
  114. asmlinkage void kgdb_trap(unsigned long trap_level, struct pt_regs *regs)
  115. {
  116. unsigned long flags;
  117. if (user_mode(regs)) {
  118. do_hw_interrupt(regs, trap_level);
  119. return;
  120. }
  121. flushw_all();
  122. local_irq_save(flags);
  123. kgdb_handle_exception(trap_level, SIGTRAP, 0, regs);
  124. local_irq_restore(flags);
  125. }
  126. int kgdb_arch_init(void)
  127. {
  128. return 0;
  129. }
  130. void kgdb_arch_exit(void)
  131. {
  132. }
  133. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  134. {
  135. regs->pc = ip;
  136. regs->npc = regs->pc + 4;
  137. }
  138. const struct kgdb_arch arch_kgdb_ops = {
  139. /* Breakpoint instruction: ta 0x7d */
  140. .gdb_bpt_instr = { 0x91, 0xd0, 0x20, 0x7d },
  141. };