step.c 4.7 KB

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