blob: cf4b9dac4a0538d15f47a3c1c41974d2c81a551d [file] [log] [blame]
Roland McGrathfa1e03e2008-01-30 13:30:50 +01001/*
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
Roland McGrath7122ec82008-01-30 13:30:50 +01008#ifdef CONFIG_X86_32
9static
10#endif
Roland McGrathfa1e03e2008-01-30 13:30:50 +010011unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
12{
13 unsigned long addr, seg;
14
Roland McGrath7122ec82008-01-30 13:30:50 +010015#ifdef CONFIG_X86_64
Roland McGrathfa1e03e2008-01-30 13:30:50 +010016 addr = regs->rip;
17 seg = regs->cs & 0xffff;
Roland McGrath7122ec82008-01-30 13:30:50 +010018#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
Roland McGrathfa1e03e2008-01-30 13:30:50 +010026
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 */
Roland McGrath3f80c1a2008-01-30 13:30:50 +010033 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Roland McGrathfa1e03e2008-01-30 13:30:50 +010034 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
59static 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:
Roland McGrath5f76cb12008-01-30 13:30:50 +010081 case 0xf0: case 0xf2: case 0xf3:
Roland McGrathfa1e03e2008-01-30 13:30:50 +010082 continue;
83
Roland McGrath7122ec82008-01-30 13:30:50 +010084#ifdef CONFIG_X86_64
Roland McGrathfa1e03e2008-01-30 13:30:50 +010085 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;
Roland McGrath7122ec82008-01-30 13:30:50 +010091#endif
Roland McGrathfa1e03e2008-01-30 13:30:50 +010092
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
Roland McGrath10faa812008-01-30 13:30:54 +0100110/*
111 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
112 */
113static int enable_single_step(struct task_struct *child)
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100114{
115 struct pt_regs *regs = task_pt_regs(child);
116
117 /*
118 * Always set TIF_SINGLESTEP - this guarantees that
119 * we single-step system calls etc.. This will also
120 * cause us to set TF when returning to user mode.
121 */
122 set_tsk_thread_flag(child, TIF_SINGLESTEP);
123
124 /*
125 * If TF was already set, don't do anything else
126 */
127 if (regs->eflags & X86_EFLAGS_TF)
Roland McGrath10faa812008-01-30 13:30:54 +0100128 return 0;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100129
130 /* Set TF on the kernel stack.. */
131 regs->eflags |= X86_EFLAGS_TF;
132
133 /*
134 * ..but if TF is changed by the instruction we will trace,
135 * don't mark it as being "us" that set it, so that we
136 * won't clear it by hand later.
137 */
138 if (is_setting_trap_flag(child, regs))
Roland McGrath10faa812008-01-30 13:30:54 +0100139 return 0;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100140
Roland McGrathe1f28772008-01-30 13:30:50 +0100141 set_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrath10faa812008-01-30 13:30:54 +0100142
143 return 1;
144}
145
146/*
147 * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
148 */
149static void write_debugctlmsr(struct task_struct *child, unsigned long val)
150{
151 child->thread.debugctlmsr = val;
152
153 if (child != current)
154 return;
155
156#ifdef CONFIG_X86_64
157 wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
158#else
159 wrmsr(MSR_IA32_DEBUGCTLMSR, val, 0);
160#endif
161}
162
163/*
164 * Enable single or block step.
165 */
166static void enable_step(struct task_struct *child, bool block)
167{
168 /*
169 * Make sure block stepping (BTF) is not enabled unless it should be.
170 * Note that we don't try to worry about any is_setting_trap_flag()
171 * instructions after the first when using block stepping.
172 * So noone should try to use debugger block stepping in a program
173 * that uses user-mode single stepping itself.
174 */
175 if (enable_single_step(child) && block) {
176 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
177 write_debugctlmsr(child, DEBUGCTLMSR_BTF);
178 } else if (test_and_clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR)) {
179 write_debugctlmsr(child, 0);
180 }
181}
182
183void user_enable_single_step(struct task_struct *child)
184{
185 enable_step(child, 0);
186}
187
188void user_enable_block_step(struct task_struct *child)
189{
190 enable_step(child, 1);
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100191}
192
193void user_disable_single_step(struct task_struct *child)
194{
Roland McGrath10faa812008-01-30 13:30:54 +0100195 /*
196 * Make sure block stepping (BTF) is disabled.
197 */
198 if (test_and_clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR))
199 write_debugctlmsr(child, 0);
200
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100201 /* Always clear TIF_SINGLESTEP... */
202 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
203
204 /* But touch TF only if it was set by us.. */
Roland McGrathe1f28772008-01-30 13:30:50 +0100205 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
206 task_pt_regs(child)->eflags &= ~X86_EFLAGS_TF;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100207}