kgdb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kgdb support for ARC
  4. *
  5. * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
  6. */
  7. #include <linux/kgdb.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <asm/disasm.h>
  11. #include <asm/cacheflush.h>
  12. static void to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
  13. struct callee_regs *cregs)
  14. {
  15. int regno;
  16. for (regno = 0; regno <= 26; regno++)
  17. gdb_regs[_R0 + regno] = get_reg(regno, kernel_regs, cregs);
  18. for (regno = 27; regno < GDB_MAX_REGS; regno++)
  19. gdb_regs[regno] = 0;
  20. gdb_regs[_FP] = kernel_regs->fp;
  21. gdb_regs[__SP] = kernel_regs->sp;
  22. gdb_regs[_BLINK] = kernel_regs->blink;
  23. gdb_regs[_RET] = kernel_regs->ret;
  24. gdb_regs[_STATUS32] = kernel_regs->status32;
  25. gdb_regs[_LP_COUNT] = kernel_regs->lp_count;
  26. gdb_regs[_LP_END] = kernel_regs->lp_end;
  27. gdb_regs[_LP_START] = kernel_regs->lp_start;
  28. gdb_regs[_BTA] = kernel_regs->bta;
  29. gdb_regs[_STOP_PC] = kernel_regs->ret;
  30. }
  31. static void from_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
  32. struct callee_regs *cregs)
  33. {
  34. int regno;
  35. for (regno = 0; regno <= 26; regno++)
  36. set_reg(regno, gdb_regs[regno + _R0], kernel_regs, cregs);
  37. kernel_regs->fp = gdb_regs[_FP];
  38. kernel_regs->sp = gdb_regs[__SP];
  39. kernel_regs->blink = gdb_regs[_BLINK];
  40. kernel_regs->ret = gdb_regs[_RET];
  41. kernel_regs->status32 = gdb_regs[_STATUS32];
  42. kernel_regs->lp_count = gdb_regs[_LP_COUNT];
  43. kernel_regs->lp_end = gdb_regs[_LP_END];
  44. kernel_regs->lp_start = gdb_regs[_LP_START];
  45. kernel_regs->bta = gdb_regs[_BTA];
  46. }
  47. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
  48. {
  49. to_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
  50. current->thread.callee_reg);
  51. }
  52. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
  53. {
  54. from_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
  55. current->thread.callee_reg);
  56. }
  57. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
  58. struct task_struct *task)
  59. {
  60. if (task)
  61. to_gdb_regs(gdb_regs, task_pt_regs(task),
  62. (struct callee_regs *) task->thread.callee_reg);
  63. }
  64. struct single_step_data_t {
  65. uint16_t opcode[2];
  66. unsigned long address[2];
  67. int is_branch;
  68. int armed;
  69. } single_step_data;
  70. static void undo_single_step(struct pt_regs *regs)
  71. {
  72. if (single_step_data.armed) {
  73. int i;
  74. for (i = 0; i < (single_step_data.is_branch ? 2 : 1); i++) {
  75. memcpy((void *) single_step_data.address[i],
  76. &single_step_data.opcode[i],
  77. BREAK_INSTR_SIZE);
  78. flush_icache_range(single_step_data.address[i],
  79. single_step_data.address[i] +
  80. BREAK_INSTR_SIZE);
  81. }
  82. single_step_data.armed = 0;
  83. }
  84. }
  85. static void place_trap(unsigned long address, void *save)
  86. {
  87. memcpy(save, (void *) address, BREAK_INSTR_SIZE);
  88. memcpy((void *) address, &arch_kgdb_ops.gdb_bpt_instr,
  89. BREAK_INSTR_SIZE);
  90. flush_icache_range(address, address + BREAK_INSTR_SIZE);
  91. }
  92. static void do_single_step(struct pt_regs *regs)
  93. {
  94. single_step_data.is_branch = disasm_next_pc((unsigned long)
  95. regs->ret, regs, (struct callee_regs *)
  96. current->thread.callee_reg,
  97. &single_step_data.address[0],
  98. &single_step_data.address[1]);
  99. place_trap(single_step_data.address[0], &single_step_data.opcode[0]);
  100. if (single_step_data.is_branch) {
  101. place_trap(single_step_data.address[1],
  102. &single_step_data.opcode[1]);
  103. }
  104. single_step_data.armed++;
  105. }
  106. int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
  107. char *remcomInBuffer, char *remcomOutBuffer,
  108. struct pt_regs *regs)
  109. {
  110. unsigned long addr;
  111. char *ptr;
  112. undo_single_step(regs);
  113. switch (remcomInBuffer[0]) {
  114. case 's':
  115. case 'c':
  116. ptr = &remcomInBuffer[1];
  117. if (kgdb_hex2long(&ptr, &addr))
  118. regs->ret = addr;
  119. fallthrough;
  120. case 'D':
  121. case 'k':
  122. atomic_set(&kgdb_cpu_doing_single_step, -1);
  123. if (remcomInBuffer[0] == 's') {
  124. do_single_step(regs);
  125. atomic_set(&kgdb_cpu_doing_single_step,
  126. smp_processor_id());
  127. }
  128. return 0;
  129. }
  130. return -1;
  131. }
  132. int kgdb_arch_init(void)
  133. {
  134. single_step_data.armed = 0;
  135. return 0;
  136. }
  137. void kgdb_trap(struct pt_regs *regs)
  138. {
  139. /* trap_s 3 is used for breakpoints that overwrite existing
  140. * instructions, while trap_s 4 is used for compiled breakpoints.
  141. *
  142. * with trap_s 3 breakpoints the original instruction needs to be
  143. * restored and continuation needs to start at the location of the
  144. * breakpoint.
  145. *
  146. * with trap_s 4 (compiled) breakpoints, continuation needs to
  147. * start after the breakpoint.
  148. */
  149. if (regs->ecr_param == 3)
  150. instruction_pointer(regs) -= BREAK_INSTR_SIZE;
  151. kgdb_handle_exception(1, SIGTRAP, 0, regs);
  152. }
  153. void kgdb_arch_exit(void)
  154. {
  155. }
  156. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  157. {
  158. instruction_pointer(regs) = ip;
  159. }
  160. void kgdb_call_nmi_hook(void *ignored)
  161. {
  162. /* Default implementation passes get_irq_regs() but we don't */
  163. kgdb_nmicallback(raw_smp_processor_id(), NULL);
  164. }
  165. const struct kgdb_arch arch_kgdb_ops = {
  166. /* breakpoint instruction: TRAP_S 0x3 */
  167. #ifdef CONFIG_CPU_BIG_ENDIAN
  168. .gdb_bpt_instr = {0x78, 0x7e},
  169. #else
  170. .gdb_bpt_instr = {0x7e, 0x78},
  171. #endif
  172. };