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