kgdb.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Nios2 KGDB support
  3. *
  4. * Copyright (C) 2015 Altera Corporation
  5. * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
  6. *
  7. * Based on the code posted by Kazuyasu on the Altera Forum at:
  8. * http://www.alteraforum.com/forum/showpost.php?p=77003&postcount=20
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include <linux/ptrace.h>
  25. #include <linux/kgdb.h>
  26. #include <linux/kdebug.h>
  27. #include <linux/io.h>
  28. static int wait_for_remote_debugger;
  29. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  30. {
  31. { "zero", GDB_SIZEOF_REG, -1 },
  32. { "at", GDB_SIZEOF_REG, offsetof(struct pt_regs, r1) },
  33. { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, r2) },
  34. { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, r3) },
  35. { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, r4) },
  36. { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, r5) },
  37. { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, r6) },
  38. { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, r7) },
  39. { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, r8) },
  40. { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, r9) },
  41. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, r10) },
  42. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, r11) },
  43. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, r12) },
  44. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, r13) },
  45. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, r14) },
  46. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, r15) },
  47. { "r16", GDB_SIZEOF_REG, -1 },
  48. { "r17", GDB_SIZEOF_REG, -1 },
  49. { "r18", GDB_SIZEOF_REG, -1 },
  50. { "r19", GDB_SIZEOF_REG, -1 },
  51. { "r20", GDB_SIZEOF_REG, -1 },
  52. { "r21", GDB_SIZEOF_REG, -1 },
  53. { "r22", GDB_SIZEOF_REG, -1 },
  54. { "r23", GDB_SIZEOF_REG, -1 },
  55. { "et", GDB_SIZEOF_REG, -1 },
  56. { "bt", GDB_SIZEOF_REG, -1 },
  57. { "gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, gp) },
  58. { "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, sp) },
  59. { "fp", GDB_SIZEOF_REG, offsetof(struct pt_regs, fp) },
  60. { "ea", GDB_SIZEOF_REG, -1 },
  61. { "ba", GDB_SIZEOF_REG, -1 },
  62. { "ra", GDB_SIZEOF_REG, offsetof(struct pt_regs, ra) },
  63. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, ea) },
  64. { "status", GDB_SIZEOF_REG, -1 },
  65. { "estatus", GDB_SIZEOF_REG, offsetof(struct pt_regs, estatus) },
  66. { "bstatus", GDB_SIZEOF_REG, -1 },
  67. { "ienable", GDB_SIZEOF_REG, -1 },
  68. { "ipending", GDB_SIZEOF_REG, -1},
  69. { "cpuid", GDB_SIZEOF_REG, -1 },
  70. { "ctl6", GDB_SIZEOF_REG, -1 },
  71. { "exception", GDB_SIZEOF_REG, -1 },
  72. { "pteaddr", GDB_SIZEOF_REG, -1 },
  73. { "tlbacc", GDB_SIZEOF_REG, -1 },
  74. { "tlbmisc", GDB_SIZEOF_REG, -1 },
  75. { "eccinj", GDB_SIZEOF_REG, -1 },
  76. { "badaddr", GDB_SIZEOF_REG, -1 },
  77. { "config", GDB_SIZEOF_REG, -1 },
  78. { "mpubase", GDB_SIZEOF_REG, -1 },
  79. { "mpuacc", GDB_SIZEOF_REG, -1 },
  80. };
  81. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  82. {
  83. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  84. return NULL;
  85. if (dbg_reg_def[regno].offset != -1)
  86. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  87. dbg_reg_def[regno].size);
  88. else
  89. memset(mem, 0, dbg_reg_def[regno].size);
  90. return dbg_reg_def[regno].name;
  91. }
  92. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  93. {
  94. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  95. return -EINVAL;
  96. if (dbg_reg_def[regno].offset != -1)
  97. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  98. dbg_reg_def[regno].size);
  99. return 0;
  100. }
  101. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  102. {
  103. memset((char *)gdb_regs, 0, NUMREGBYTES);
  104. gdb_regs[GDB_SP] = p->thread.kregs->sp;
  105. gdb_regs[GDB_PC] = p->thread.kregs->ea;
  106. }
  107. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  108. {
  109. regs->ea = pc;
  110. }
  111. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  112. char *remcom_in_buffer, char *remcom_out_buffer,
  113. struct pt_regs *regs)
  114. {
  115. char *ptr;
  116. unsigned long addr;
  117. switch (remcom_in_buffer[0]) {
  118. case 's':
  119. case 'c':
  120. /* handle the optional parameters */
  121. ptr = &remcom_in_buffer[1];
  122. if (kgdb_hex2long(&ptr, &addr))
  123. regs->ea = addr;
  124. return 0;
  125. }
  126. return -1; /* this means that we do not want to exit from the handler */
  127. }
  128. asmlinkage void kgdb_breakpoint_c(struct pt_regs *regs)
  129. {
  130. /*
  131. * The breakpoint entry code has moved the PC on by 4 bytes, so we must
  132. * move it back. This could be done on the host but we do it here
  133. */
  134. if (!wait_for_remote_debugger)
  135. regs->ea -= 4;
  136. else /* pass the first trap 30 code */
  137. wait_for_remote_debugger = 0;
  138. kgdb_handle_exception(30, SIGTRAP, 0, regs);
  139. }
  140. int kgdb_arch_init(void)
  141. {
  142. wait_for_remote_debugger = 1;
  143. return 0;
  144. }
  145. void kgdb_arch_exit(void)
  146. {
  147. /* Nothing to do */
  148. }
  149. struct kgdb_arch arch_kgdb_ops = {
  150. /* Breakpoint instruction: trap 30 */
  151. .gdb_bpt_instr = { 0xba, 0x6f, 0x3b, 0x00 },
  152. };