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