kgdb.c 5.1 KB

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