x86: single_step 0xf0
[linux-2.6-block.git] / arch / x86 / kernel / step.c
CommitLineData
fa1e03ea
RM
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
fa1e03ea
RM
8unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
9{
10 unsigned long addr, seg;
11
12 addr = regs->rip;
13 seg = regs->cs & 0xffff;
14
15 /*
16 * We'll assume that the code segments in the GDT
17 * are all zero-based. That is largely true: the
18 * TLS segments are used for data, and the PNPBIOS
19 * and APM bios ones we just ignore here.
20 */
3f80c1ad 21 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
fa1e03ea
RM
22 u32 *desc;
23 unsigned long base;
24
25 seg &= ~7UL;
26
27 mutex_lock(&child->mm->context.lock);
28 if (unlikely((seg >> 3) >= child->mm->context.size))
29 addr = -1L; /* bogus selector, access would fault */
30 else {
31 desc = child->mm->context.ldt + seg;
32 base = ((desc[0] >> 16) |
33 ((desc[1] & 0xff) << 16) |
34 (desc[1] & 0xff000000));
35
36 /* 16-bit code segment? */
37 if (!((desc[1] >> 22) & 1))
38 addr &= 0xffff;
39 addr += base;
40 }
41 mutex_unlock(&child->mm->context.lock);
42 }
43
44 return addr;
45}
46
47static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
48{
49 int i, copied;
50 unsigned char opcode[15];
51 unsigned long addr = convert_rip_to_linear(child, regs);
52
53 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
54 for (i = 0; i < copied; i++) {
55 switch (opcode[i]) {
56 /* popf and iret */
57 case 0x9d: case 0xcf:
58 return 1;
59
60 /* CHECKME: 64 65 */
61
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:
5f76cb1f 69 case 0xf0: case 0xf2: case 0xf3:
fa1e03ea
RM
70 continue;
71
72 case 0x40 ... 0x4f:
73 if (regs->cs != __USER_CS)
74 /* 32-bit mode: register increment */
75 return 0;
76 /* 64-bit mode: REX prefix */
77 continue;
78
79 /* CHECKME: f2, f3 */
80
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
96void user_enable_single_step(struct task_struct *child)
97{
98 struct pt_regs *regs = task_pt_regs(child);
99
100 /*
101 * Always set TIF_SINGLESTEP - this guarantees that
102 * we single-step system calls etc.. This will also
103 * cause us to set TF when returning to user mode.
104 */
105 set_tsk_thread_flag(child, TIF_SINGLESTEP);
106
107 /*
108 * If TF was already set, don't do anything else
109 */
110 if (regs->eflags & X86_EFLAGS_TF)
111 return;
112
113 /* Set TF on the kernel stack.. */
114 regs->eflags |= X86_EFLAGS_TF;
115
116 /*
117 * ..but if TF is changed by the instruction we will trace,
118 * don't mark it as being "us" that set it, so that we
119 * won't clear it by hand later.
120 */
121 if (is_setting_trap_flag(child, regs))
122 return;
123
124 child->ptrace |= PT_DTRACE;
125}
126
127void user_disable_single_step(struct task_struct *child)
128{
129 /* Always clear TIF_SINGLESTEP... */
130 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
131
132 /* But touch TF only if it was set by us.. */
133 if (child->ptrace & PT_DTRACE) {
134 struct pt_regs *regs = task_pt_regs(child);
135 regs->eflags &= ~X86_EFLAGS_TF;
136 child->ptrace &= ~PT_DTRACE;
137 }
138}