step.c 5.7 KB

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