step.c 6.0 KB

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