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