kgdb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * H8/300 KGDB support
  3. *
  4. * Copyright (C) 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/ptrace.h>
  11. #include <linux/kgdb.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/io.h>
  14. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  15. { "er0", GDB_SIZEOF_REG, offsetof(struct pt_regs, er0) },
  16. { "er1", GDB_SIZEOF_REG, offsetof(struct pt_regs, er1) },
  17. { "er2", GDB_SIZEOF_REG, offsetof(struct pt_regs, er2) },
  18. { "er3", GDB_SIZEOF_REG, offsetof(struct pt_regs, er3) },
  19. { "er4", GDB_SIZEOF_REG, offsetof(struct pt_regs, er4) },
  20. { "er5", GDB_SIZEOF_REG, offsetof(struct pt_regs, er5) },
  21. { "er6", GDB_SIZEOF_REG, offsetof(struct pt_regs, er6) },
  22. { "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, sp) },
  23. { "ccr", GDB_SIZEOF_REG, offsetof(struct pt_regs, ccr) },
  24. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, pc) },
  25. { "cycles", GDB_SIZEOF_REG, -1 },
  26. #if defined(CONFIG_CPU_H8S)
  27. { "exr", GDB_SIZEOF_REG, offsetof(struct pt_regs, exr) },
  28. #endif
  29. { "tick", GDB_SIZEOF_REG, -1 },
  30. { "inst", GDB_SIZEOF_REG, -1 },
  31. };
  32. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  33. {
  34. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  35. return NULL;
  36. switch (regno) {
  37. case GDB_CCR:
  38. #if defined(CONFIG_CPU_H8S)
  39. case GDB_EXR:
  40. #endif
  41. *(u32 *)mem = *(u16 *)((void *)regs +
  42. dbg_reg_def[regno].offset);
  43. break;
  44. default:
  45. if (dbg_reg_def[regno].offset >= 0)
  46. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  47. dbg_reg_def[regno].size);
  48. else
  49. memset(mem, 0, dbg_reg_def[regno].size);
  50. break;
  51. }
  52. return dbg_reg_def[regno].name;
  53. }
  54. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  55. {
  56. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  57. return -EINVAL;
  58. switch (regno) {
  59. case GDB_CCR:
  60. #if defined(CONFIG_CPU_H8S)
  61. case GDB_EXR:
  62. #endif
  63. *(u16 *)((void *)regs +
  64. dbg_reg_def[regno].offset) = *(u32 *)mem;
  65. break;
  66. default:
  67. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  68. dbg_reg_def[regno].size);
  69. }
  70. return 0;
  71. }
  72. asmlinkage void h8300_kgdb_trap(struct pt_regs *regs)
  73. {
  74. regs->pc &= 0x00ffffff;
  75. if (kgdb_handle_exception(10, SIGTRAP, 0, regs))
  76. return;
  77. if (*(u16 *)(regs->pc) == *(u16 *)&arch_kgdb_ops.gdb_bpt_instr)
  78. regs->pc += BREAK_INSTR_SIZE;
  79. regs->pc |= regs->ccr << 24;
  80. }
  81. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  82. {
  83. memset((char *)gdb_regs, 0, NUMREGBYTES);
  84. gdb_regs[GDB_SP] = p->thread.ksp;
  85. gdb_regs[GDB_PC] = KSTK_EIP(p);
  86. }
  87. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  88. {
  89. regs->pc = pc;
  90. }
  91. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  92. char *remcom_in_buffer, char *remcom_out_buffer,
  93. struct pt_regs *regs)
  94. {
  95. char *ptr;
  96. unsigned long addr;
  97. switch (remcom_in_buffer[0]) {
  98. case 's':
  99. case 'c':
  100. /* handle the optional parameters */
  101. ptr = &remcom_in_buffer[1];
  102. if (kgdb_hex2long(&ptr, &addr))
  103. regs->pc = addr;
  104. return 0;
  105. }
  106. return -1; /* this means that we do not want to exit from the handler */
  107. }
  108. int kgdb_arch_init(void)
  109. {
  110. return 0;
  111. }
  112. void kgdb_arch_exit(void)
  113. {
  114. /* Nothing to do */
  115. }
  116. const struct kgdb_arch arch_kgdb_ops = {
  117. /* Breakpoint instruction: trapa #2 */
  118. .gdb_bpt_instr = { 0x57, 0x20 },
  119. };