x86: rename the struct pt_regs members for 32/64-bit consistency
[linux-2.6/x86.git] / arch / x86 / kernel / step.c
blobf55c003f5b6392737be0adea0733c9f1970b9a13
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>
8 #ifdef CONFIG_X86_32
9 static
10 #endif
11 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
13 unsigned long addr, seg;
15 addr = regs->ip;
16 seg = regs->cs & 0xffff;
17 if (v8086_mode(regs)) {
18 addr = (addr & 0xffff) + (seg << 4);
19 return addr;
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.
28 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
29 u32 *desc;
30 unsigned long base;
32 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));
43 /* 16-bit code segment? */
44 if (!((desc[1] >> 22) & 1))
45 addr &= 0xffff;
46 addr += base;
48 mutex_unlock(&child->mm->context.lock);
51 return addr;
54 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
56 int i, copied;
57 unsigned char opcode[15];
58 unsigned long addr = convert_rip_to_linear(child, regs);
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;
67 /* CHECKME: 64 65 */
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;
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
88 /* CHECKME: f2, f3 */
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.
97 case 0x9c:
98 default:
99 return 0;
102 return 0;
106 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
108 static int enable_single_step(struct task_struct *child)
110 struct pt_regs *regs = task_pt_regs(child);
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.
117 set_tsk_thread_flag(child, TIF_SINGLESTEP);
120 * If TF was already set, don't do anything else
122 if (regs->flags & X86_EFLAGS_TF)
123 return 0;
125 /* Set TF on the kernel stack.. */
126 regs->flags |= X86_EFLAGS_TF;
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.
133 if (is_setting_trap_flag(child, regs))
134 return 0;
136 set_tsk_thread_flag(child, TIF_FORCED_TF);
138 return 1;
142 * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
144 static void write_debugctlmsr(struct task_struct *child, unsigned long val)
146 child->thread.debugctlmsr = val;
148 if (child != current)
149 return;
151 #ifdef CONFIG_X86_64
152 wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
153 #else
154 wrmsr(MSR_IA32_DEBUGCTLMSR, val, 0);
155 #endif
159 * Enable single or block step.
161 static void enable_step(struct task_struct *child, bool block)
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.
170 if (enable_single_step(child) && block) {
171 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
172 write_debugctlmsr(child, DEBUGCTLMSR_BTF);
173 } else if (test_and_clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR)) {
174 write_debugctlmsr(child, 0);
178 void user_enable_single_step(struct task_struct *child)
180 enable_step(child, 0);
183 void user_enable_block_step(struct task_struct *child)
185 enable_step(child, 1);
188 void user_disable_single_step(struct task_struct *child)
191 * Make sure block stepping (BTF) is disabled.
193 if (test_and_clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR))
194 write_debugctlmsr(child, 0);
196 /* Always clear TIF_SINGLESTEP... */
197 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
199 /* But touch TF only if it was set by us.. */
200 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
201 task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;