kgdb.c 5.2 KB

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