step.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. void user_enable_single_step(struct task_struct *child)
  96. {
  97. struct pt_regs *regs = task_pt_regs(child);
  98. /*
  99. * Always set TIF_SINGLESTEP - this guarantees that
  100. * we single-step system calls etc.. This will also
  101. * cause us to set TF when returning to user mode.
  102. */
  103. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  104. /*
  105. * If TF was already set, don't do anything else
  106. */
  107. if (regs->eflags & X86_EFLAGS_TF)
  108. return;
  109. /* Set TF on the kernel stack.. */
  110. regs->eflags |= X86_EFLAGS_TF;
  111. /*
  112. * ..but if TF is changed by the instruction we will trace,
  113. * don't mark it as being "us" that set it, so that we
  114. * won't clear it by hand later.
  115. */
  116. if (is_setting_trap_flag(child, regs))
  117. return;
  118. child->ptrace |= PT_DTRACE;
  119. }
  120. void user_disable_single_step(struct task_struct *child)
  121. {
  122. /* Always clear TIF_SINGLESTEP... */
  123. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  124. /* But touch TF only if it was set by us.. */
  125. if (child->ptrace & PT_DTRACE) {
  126. struct pt_regs *regs = task_pt_regs(child);
  127. regs->eflags &= ~X86_EFLAGS_TF;
  128. child->ptrace &= ~PT_DTRACE;
  129. }
  130. }