step.c 6.3 KB

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