step.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * x86 single-step support code, common to 32-bit and 64-bit.
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/mm.h>
  6. #include <linux/ptrace.h>
  7. #include <asm/desc.h>
  8. #include <asm/mmu_context.h>
  9. unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
  10. {
  11. unsigned long addr, seg;
  12. addr = regs->ip;
  13. seg = regs->cs & 0xffff;
  14. if (v8086_mode(regs)) {
  15. addr = (addr & 0xffff) + (seg << 4);
  16. return addr;
  17. }
  18. /*
  19. * We'll assume that the code segments in the GDT
  20. * are all zero-based. That is largely true: the
  21. * TLS segments are used for data, and the PNPBIOS
  22. * and APM bios ones we just ignore here.
  23. */
  24. if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  25. struct desc_struct *desc;
  26. unsigned long base;
  27. seg >>= 3;
  28. mutex_lock(&child->mm->context.lock);
  29. if (unlikely(!child->mm->context.ldt ||
  30. seg >= child->mm->context.ldt->size))
  31. addr = -1L; /* bogus selector, access would fault */
  32. else {
  33. desc = &child->mm->context.ldt->entries[seg];
  34. base = get_desc_base(desc);
  35. /* 16-bit code segment? */
  36. if (!desc->d)
  37. addr &= 0xffff;
  38. addr += base;
  39. }
  40. mutex_unlock(&child->mm->context.lock);
  41. }
  42. return addr;
  43. }
  44. static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
  45. {
  46. int i, copied;
  47. unsigned char opcode[15];
  48. unsigned long addr = convert_ip_to_linear(child, regs);
  49. copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
  50. for (i = 0; i < copied; i++) {
  51. switch (opcode[i]) {
  52. /* popf and iret */
  53. case 0x9d: case 0xcf:
  54. return 1;
  55. /* CHECKME: 64 65 */
  56. /* opcode and address size prefixes */
  57. case 0x66: case 0x67:
  58. continue;
  59. /* irrelevant prefixes (segment overrides and repeats) */
  60. case 0x26: case 0x2e:
  61. case 0x36: case 0x3e:
  62. case 0x64: case 0x65:
  63. case 0xf0: case 0xf2: case 0xf3:
  64. continue;
  65. #ifdef CONFIG_X86_64
  66. case 0x40 ... 0x4f:
  67. if (!user_64bit_mode(regs))
  68. /* 32-bit mode: register increment */
  69. return 0;
  70. /* 64-bit mode: REX prefix */
  71. continue;
  72. #endif
  73. /* CHECKME: f2, f3 */
  74. /*
  75. * pushf: NOTE! We should probably not let
  76. * the user see the TF bit being set. But
  77. * it's more pain than it's worth to avoid
  78. * it, and a debugger could emulate this
  79. * all in user space if it _really_ cares.
  80. */
  81. case 0x9c:
  82. default:
  83. return 0;
  84. }
  85. }
  86. return 0;
  87. }
  88. /*
  89. * Enable single-stepping. Return nonzero if user mode is not using TF itself.
  90. */
  91. static int enable_single_step(struct task_struct *child)
  92. {
  93. struct pt_regs *regs = task_pt_regs(child);
  94. unsigned long oflags;
  95. /*
  96. * If we stepped into a sysenter/syscall insn, it trapped in
  97. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  98. * If user-mode had set TF itself, then it's still clear from
  99. * do_debug() and we need to set it again to restore the user
  100. * state so we don't wrongly set TIF_FORCED_TF below.
  101. * If enable_single_step() was used last and that is what
  102. * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
  103. * already set and our bookkeeping is fine.
  104. */
  105. if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
  106. regs->flags |= X86_EFLAGS_TF;
  107. /*
  108. * Always set TIF_SINGLESTEP - this guarantees that
  109. * we single-step system calls etc.. This will also
  110. * cause us to set TF when returning to user mode.
  111. */
  112. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  113. oflags = regs->flags;
  114. /* Set TF on the kernel stack.. */
  115. regs->flags |= X86_EFLAGS_TF;
  116. /*
  117. * ..but if TF is changed by the instruction we will trace,
  118. * don't mark it as being "us" that set it, so that we
  119. * won't clear it by hand later.
  120. *
  121. * Note that if we don't actually execute the popf because
  122. * of a signal arriving right now or suchlike, we will lose
  123. * track of the fact that it really was "us" that set it.
  124. */
  125. if (is_setting_trap_flag(child, regs)) {
  126. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  127. return 0;
  128. }
  129. /*
  130. * If TF was already set, check whether it was us who set it.
  131. * If not, we should never attempt a block step.
  132. */
  133. if (oflags & X86_EFLAGS_TF)
  134. return test_tsk_thread_flag(child, TIF_FORCED_TF);
  135. set_tsk_thread_flag(child, TIF_FORCED_TF);
  136. return 1;
  137. }
  138. static void set_task_blockstep(struct task_struct *task, bool on)
  139. {
  140. unsigned long debugctl;
  141. /*
  142. * Ensure irq/preemption can't change debugctl in between.
  143. * Note also that both TIF_BLOCKSTEP and debugctl should
  144. * be changed atomically wrt preemption.
  145. * FIXME: this means that set/clear TIF_BLOCKSTEP is simply
  146. * wrong if task != current, SIGKILL can wakeup the stopped
  147. * tracee and set/clear can play with the running task, this
  148. * can confuse the next __switch_to_xtra().
  149. */
  150. local_irq_disable();
  151. debugctl = get_debugctlmsr();
  152. if (on) {
  153. debugctl |= DEBUGCTLMSR_BTF;
  154. set_tsk_thread_flag(task, TIF_BLOCKSTEP);
  155. } else {
  156. debugctl &= ~DEBUGCTLMSR_BTF;
  157. clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
  158. }
  159. if (task == current)
  160. update_debugctlmsr(debugctl);
  161. local_irq_enable();
  162. }
  163. /*
  164. * Enable single or block step.
  165. */
  166. static void enable_step(struct task_struct *child, bool block)
  167. {
  168. /*
  169. * Make sure block stepping (BTF) is not enabled unless it should be.
  170. * Note that we don't try to worry about any is_setting_trap_flag()
  171. * instructions after the first when using block stepping.
  172. * So no one should try to use debugger block stepping in a program
  173. * that uses user-mode single stepping itself.
  174. */
  175. if (enable_single_step(child) && block)
  176. set_task_blockstep(child, true);
  177. else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
  178. set_task_blockstep(child, false);
  179. }
  180. void user_enable_single_step(struct task_struct *child)
  181. {
  182. enable_step(child, 0);
  183. }
  184. void user_enable_block_step(struct task_struct *child)
  185. {
  186. enable_step(child, 1);
  187. }
  188. void user_disable_single_step(struct task_struct *child)
  189. {
  190. /*
  191. * Make sure block stepping (BTF) is disabled.
  192. */
  193. if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
  194. set_task_blockstep(child, false);
  195. /* Always clear TIF_SINGLESTEP... */
  196. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  197. /* But touch TF only if it was set by us.. */
  198. if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
  199. task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
  200. }