step.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /*
  94. * Always set TIF_SINGLESTEP - this guarantees that
  95. * we single-step system calls etc.. This will also
  96. * cause us to set TF when returning to user mode.
  97. */
  98. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  99. /*
  100. * If TF was already set, don't do anything else
  101. */
  102. if (regs->flags & X86_EFLAGS_TF)
  103. return 0;
  104. /* Set TF on the kernel stack.. */
  105. regs->flags |= X86_EFLAGS_TF;
  106. /*
  107. * ..but if TF is changed by the instruction we will trace,
  108. * don't mark it as being "us" that set it, so that we
  109. * won't clear it by hand later.
  110. */
  111. if (is_setting_trap_flag(child, regs))
  112. return 0;
  113. set_tsk_thread_flag(child, TIF_FORCED_TF);
  114. return 1;
  115. }
  116. /*
  117. * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
  118. */
  119. static void write_debugctlmsr(struct task_struct *child, unsigned long val)
  120. {
  121. child->thread.debugctlmsr = val;
  122. if (child != current)
  123. return;
  124. wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
  125. }
  126. /*
  127. * Enable single or block step.
  128. */
  129. static void enable_step(struct task_struct *child, bool block)
  130. {
  131. /*
  132. * Make sure block stepping (BTF) is not enabled unless it should be.
  133. * Note that we don't try to worry about any is_setting_trap_flag()
  134. * instructions after the first when using block stepping.
  135. * So noone should try to use debugger block stepping in a program
  136. * that uses user-mode single stepping itself.
  137. */
  138. if (enable_single_step(child) && block) {
  139. set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  140. write_debugctlmsr(child,
  141. child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
  142. } else {
  143. write_debugctlmsr(child,
  144. child->thread.debugctlmsr & ~TIF_DEBUGCTLMSR);
  145. if (!child->thread.debugctlmsr)
  146. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  147. }
  148. }
  149. void user_enable_single_step(struct task_struct *child)
  150. {
  151. enable_step(child, 0);
  152. }
  153. void user_enable_block_step(struct task_struct *child)
  154. {
  155. enable_step(child, 1);
  156. }
  157. void user_disable_single_step(struct task_struct *child)
  158. {
  159. /*
  160. * Make sure block stepping (BTF) is disabled.
  161. */
  162. write_debugctlmsr(child,
  163. child->thread.debugctlmsr & ~TIF_DEBUGCTLMSR);
  164. if (!child->thread.debugctlmsr)
  165. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  166. /* Always clear TIF_SINGLESTEP... */
  167. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  168. /* But touch TF only if it was set by us.. */
  169. if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
  170. task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
  171. }