step.c 4.8 KB

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