step.c 6.0 KB

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