]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* By Ross Biro 1/23/92 */ |
2 | /* | |
3 | * Pentium III FXSR, SSE support | |
4 | * Gareth Hughes <gareth@valinux.com>, May 2000 | |
5 | */ | |
6 | ||
7 | #include <linux/kernel.h> | |
8 | #include <linux/sched.h> | |
9 | #include <linux/mm.h> | |
10 | #include <linux/smp.h> | |
1da177e4 LT |
11 | #include <linux/errno.h> |
12 | #include <linux/ptrace.h> | |
13 | #include <linux/user.h> | |
14 | #include <linux/security.h> | |
15 | #include <linux/audit.h> | |
16 | #include <linux/seccomp.h> | |
7ed20e1a | 17 | #include <linux/signal.h> |
1da177e4 LT |
18 | |
19 | #include <asm/uaccess.h> | |
20 | #include <asm/pgtable.h> | |
21 | #include <asm/system.h> | |
22 | #include <asm/processor.h> | |
23 | #include <asm/i387.h> | |
24 | #include <asm/debugreg.h> | |
25 | #include <asm/ldt.h> | |
26 | #include <asm/desc.h> | |
27 | ||
28 | /* | |
29 | * does not yet catch signals sent when the child dies. | |
30 | * in exit.c or in signal.c. | |
31 | */ | |
32 | ||
9f155b98 CE |
33 | /* |
34 | * Determines which flags the user has access to [1 = access, 0 = no access]. | |
9f155b98 | 35 | */ |
e39c2891 RM |
36 | #define FLAG_MASK_32 ((unsigned long) \ |
37 | (X86_EFLAGS_CF | X86_EFLAGS_PF | \ | |
38 | X86_EFLAGS_AF | X86_EFLAGS_ZF | \ | |
39 | X86_EFLAGS_SF | X86_EFLAGS_TF | \ | |
40 | X86_EFLAGS_DF | X86_EFLAGS_OF | \ | |
41 | X86_EFLAGS_RF | X86_EFLAGS_AC)) | |
42 | ||
43 | #define FLAG_MASK FLAG_MASK_32 | |
1da177e4 | 44 | |
62a97d44 | 45 | static long *pt_regs_access(struct pt_regs *regs, unsigned long regno) |
1da177e4 | 46 | { |
65ea5b03 | 47 | BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0); |
62a97d44 RM |
48 | if (regno > FS) |
49 | --regno; | |
65ea5b03 | 50 | return ®s->bx + regno; |
1da177e4 LT |
51 | } |
52 | ||
53 | static int putreg(struct task_struct *child, | |
54 | unsigned long regno, unsigned long value) | |
55 | { | |
62a97d44 RM |
56 | struct pt_regs *regs = task_pt_regs(child); |
57 | regno >>= 2; | |
58 | switch (regno) { | |
9e714bed RM |
59 | case GS: |
60 | if (value && (value & 3) != 3) | |
61 | return -EIO; | |
62 | child->thread.gs = value; | |
5fd4d16b RM |
63 | if (child == current) |
64 | /* | |
65 | * The user-mode %gs is not affected by | |
66 | * kernel entry, so we must update the CPU. | |
67 | */ | |
68 | loadsegment(gs, value); | |
9e714bed RM |
69 | return 0; |
70 | case DS: | |
71 | case ES: | |
72 | case FS: | |
73 | if (value && (value & 3) != 3) | |
74 | return -EIO; | |
75 | value &= 0xffff; | |
76 | break; | |
77 | case SS: | |
78 | case CS: | |
79 | if ((value & 3) != 3) | |
80 | return -EIO; | |
81 | value &= 0xffff; | |
82 | break; | |
83 | case EFL: | |
84 | value &= FLAG_MASK; | |
85 | /* | |
86 | * If the user value contains TF, mark that | |
87 | * it was not "us" (the debugger) that set it. | |
88 | * If not, make sure it stays set if we had. | |
89 | */ | |
90 | if (value & X86_EFLAGS_TF) | |
91 | clear_tsk_thread_flag(child, TIF_FORCED_TF); | |
92 | else if (test_tsk_thread_flag(child, TIF_FORCED_TF)) | |
93 | value |= X86_EFLAGS_TF; | |
94 | value |= regs->flags & ~FLAG_MASK; | |
95 | break; | |
1da177e4 | 96 | } |
62a97d44 | 97 | *pt_regs_access(regs, regno) = value; |
1da177e4 LT |
98 | return 0; |
99 | } | |
100 | ||
62a97d44 | 101 | static unsigned long getreg(struct task_struct *child, unsigned long regno) |
1da177e4 | 102 | { |
62a97d44 | 103 | struct pt_regs *regs = task_pt_regs(child); |
1da177e4 LT |
104 | unsigned long retval = ~0UL; |
105 | ||
62a97d44 RM |
106 | regno >>= 2; |
107 | switch (regno) { | |
9e714bed RM |
108 | case EFL: |
109 | /* | |
110 | * If the debugger set TF, hide it from the readout. | |
111 | */ | |
112 | retval = regs->flags; | |
113 | if (test_tsk_thread_flag(child, TIF_FORCED_TF)) | |
114 | retval &= ~X86_EFLAGS_TF; | |
115 | break; | |
116 | case GS: | |
117 | retval = child->thread.gs; | |
5fd4d16b RM |
118 | if (child == current) |
119 | savesegment(gs, retval); | |
9e714bed RM |
120 | break; |
121 | case DS: | |
122 | case ES: | |
123 | case FS: | |
124 | case SS: | |
125 | case CS: | |
126 | retval = 0xffff; | |
127 | /* fall through */ | |
128 | default: | |
129 | retval &= *pt_regs_access(regs, regno); | |
1da177e4 LT |
130 | } |
131 | return retval; | |
132 | } | |
133 | ||
d9771e8c RM |
134 | /* |
135 | * This function is trivial and will be inlined by the compiler. | |
136 | * Having it separates the implementation details of debug | |
137 | * registers from the interface details of ptrace. | |
138 | */ | |
139 | static unsigned long ptrace_get_debugreg(struct task_struct *child, int n) | |
140 | { | |
0f534093 RM |
141 | switch (n) { |
142 | case 0: return child->thread.debugreg0; | |
143 | case 1: return child->thread.debugreg1; | |
144 | case 2: return child->thread.debugreg2; | |
145 | case 3: return child->thread.debugreg3; | |
146 | case 6: return child->thread.debugreg6; | |
147 | case 7: return child->thread.debugreg7; | |
148 | } | |
149 | return 0; | |
d9771e8c RM |
150 | } |
151 | ||
152 | static int ptrace_set_debugreg(struct task_struct *child, | |
153 | int n, unsigned long data) | |
154 | { | |
0f534093 RM |
155 | int i; |
156 | ||
d9771e8c RM |
157 | if (unlikely(n == 4 || n == 5)) |
158 | return -EIO; | |
159 | ||
160 | if (n < 4 && unlikely(data >= TASK_SIZE - 3)) | |
161 | return -EIO; | |
162 | ||
0f534093 RM |
163 | switch (n) { |
164 | case 0: child->thread.debugreg0 = data; break; | |
165 | case 1: child->thread.debugreg1 = data; break; | |
166 | case 2: child->thread.debugreg2 = data; break; | |
167 | case 3: child->thread.debugreg3 = data; break; | |
168 | ||
169 | case 6: | |
170 | child->thread.debugreg6 = data; | |
171 | break; | |
172 | ||
173 | case 7: | |
d9771e8c RM |
174 | /* |
175 | * Sanity-check data. Take one half-byte at once with | |
176 | * check = (val >> (16 + 4*i)) & 0xf. It contains the | |
177 | * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits | |
178 | * 2 and 3 are LENi. Given a list of invalid values, | |
179 | * we do mask |= 1 << invalid_value, so that | |
180 | * (mask >> check) & 1 is a correct test for invalid | |
181 | * values. | |
182 | * | |
183 | * R/Wi contains the type of the breakpoint / | |
184 | * watchpoint, LENi contains the length of the watched | |
185 | * data in the watchpoint case. | |
186 | * | |
187 | * The invalid values are: | |
188 | * - LENi == 0x10 (undefined), so mask |= 0x0f00. | |
189 | * - R/Wi == 0x10 (break on I/O reads or writes), so | |
190 | * mask |= 0x4444. | |
191 | * - R/Wi == 0x00 && LENi != 0x00, so we have mask |= | |
192 | * 0x1110. | |
193 | * | |
194 | * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54. | |
195 | * | |
196 | * See the Intel Manual "System Programming Guide", | |
197 | * 15.2.4 | |
198 | * | |
199 | * Note that LENi == 0x10 is defined on x86_64 in long | |
200 | * mode (i.e. even for 32-bit userspace software, but | |
201 | * 64-bit kernel), so the x86_64 mask value is 0x5454. | |
202 | * See the AMD manual no. 24593 (AMD64 System Programming) | |
203 | */ | |
d9771e8c RM |
204 | data &= ~DR_CONTROL_RESERVED; |
205 | for (i = 0; i < 4; i++) | |
206 | if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1) | |
207 | return -EIO; | |
0f534093 | 208 | child->thread.debugreg7 = data; |
d9771e8c RM |
209 | if (data) |
210 | set_tsk_thread_flag(child, TIF_DEBUG); | |
211 | else | |
212 | clear_tsk_thread_flag(child, TIF_DEBUG); | |
0f534093 | 213 | break; |
d9771e8c RM |
214 | } |
215 | ||
d9771e8c RM |
216 | return 0; |
217 | } | |
218 | ||
1da177e4 LT |
219 | /* |
220 | * Called by kernel/ptrace.c when detaching.. | |
221 | * | |
222 | * Make sure the single step bit is not set. | |
223 | */ | |
224 | void ptrace_disable(struct task_struct *child) | |
9e714bed | 225 | { |
7f232343 | 226 | user_disable_single_step(child); |
ab1c23c2 | 227 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
1da177e4 LT |
228 | } |
229 | ||
481bed45 | 230 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
1da177e4 | 231 | { |
1da177e4 LT |
232 | struct user * dummy = NULL; |
233 | int i, ret; | |
234 | unsigned long __user *datap = (unsigned long __user *)data; | |
235 | ||
1da177e4 LT |
236 | switch (request) { |
237 | /* when I and D space are separate, these will need to be fixed. */ | |
9e714bed | 238 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
76647323 AD |
239 | case PTRACE_PEEKDATA: |
240 | ret = generic_ptrace_peekdata(child, addr, data); | |
1da177e4 | 241 | break; |
1da177e4 LT |
242 | |
243 | /* read the word at location addr in the USER area. */ | |
244 | case PTRACE_PEEKUSR: { | |
245 | unsigned long tmp; | |
246 | ||
247 | ret = -EIO; | |
9e714bed | 248 | if ((addr & 3) || addr < 0 || |
1da177e4 LT |
249 | addr > sizeof(struct user) - 3) |
250 | break; | |
251 | ||
252 | tmp = 0; /* Default return condition */ | |
253 | if(addr < FRAME_SIZE*sizeof(long)) | |
254 | tmp = getreg(child, addr); | |
255 | if(addr >= (long) &dummy->u_debugreg[0] && | |
256 | addr <= (long) &dummy->u_debugreg[7]){ | |
257 | addr -= (long) &dummy->u_debugreg[0]; | |
258 | addr = addr >> 2; | |
d9771e8c | 259 | tmp = ptrace_get_debugreg(child, addr); |
1da177e4 LT |
260 | } |
261 | ret = put_user(tmp, datap); | |
262 | break; | |
263 | } | |
264 | ||
265 | /* when I and D space are separate, this will have to be fixed. */ | |
266 | case PTRACE_POKETEXT: /* write the word at location addr. */ | |
267 | case PTRACE_POKEDATA: | |
f284ce72 | 268 | ret = generic_ptrace_pokedata(child, addr, data); |
1da177e4 LT |
269 | break; |
270 | ||
271 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ | |
272 | ret = -EIO; | |
9e714bed | 273 | if ((addr & 3) || addr < 0 || |
1da177e4 LT |
274 | addr > sizeof(struct user) - 3) |
275 | break; | |
276 | ||
277 | if (addr < FRAME_SIZE*sizeof(long)) { | |
278 | ret = putreg(child, addr, data); | |
279 | break; | |
280 | } | |
281 | /* We need to be very careful here. We implicitly | |
282 | want to modify a portion of the task_struct, and we | |
283 | have to be selective about what portions we allow someone | |
284 | to modify. */ | |
285 | ||
286 | ret = -EIO; | |
287 | if(addr >= (long) &dummy->u_debugreg[0] && | |
288 | addr <= (long) &dummy->u_debugreg[7]){ | |
1da177e4 LT |
289 | addr -= (long) &dummy->u_debugreg; |
290 | addr = addr >> 2; | |
d9771e8c | 291 | ret = ptrace_set_debugreg(child, addr, data); |
1da177e4 LT |
292 | } |
293 | break; | |
294 | ||
1da177e4 LT |
295 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ |
296 | if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) { | |
297 | ret = -EIO; | |
298 | break; | |
299 | } | |
300 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { | |
301 | __put_user(getreg(child, i), datap); | |
302 | datap++; | |
303 | } | |
304 | ret = 0; | |
305 | break; | |
306 | } | |
307 | ||
308 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ | |
309 | unsigned long tmp; | |
310 | if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) { | |
311 | ret = -EIO; | |
312 | break; | |
313 | } | |
314 | for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) { | |
315 | __get_user(tmp, datap); | |
316 | putreg(child, i, tmp); | |
317 | datap++; | |
318 | } | |
319 | ret = 0; | |
320 | break; | |
321 | } | |
322 | ||
323 | case PTRACE_GETFPREGS: { /* Get the child FPU state. */ | |
324 | if (!access_ok(VERIFY_WRITE, datap, | |
325 | sizeof(struct user_i387_struct))) { | |
326 | ret = -EIO; | |
327 | break; | |
328 | } | |
329 | ret = 0; | |
330 | if (!tsk_used_math(child)) | |
331 | init_fpu(child); | |
332 | get_fpregs((struct user_i387_struct __user *)data, child); | |
333 | break; | |
334 | } | |
335 | ||
336 | case PTRACE_SETFPREGS: { /* Set the child FPU state. */ | |
337 | if (!access_ok(VERIFY_READ, datap, | |
338 | sizeof(struct user_i387_struct))) { | |
339 | ret = -EIO; | |
340 | break; | |
341 | } | |
342 | set_stopped_child_used_math(child); | |
343 | set_fpregs(child, (struct user_i387_struct __user *)data); | |
344 | ret = 0; | |
345 | break; | |
346 | } | |
347 | ||
348 | case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */ | |
349 | if (!access_ok(VERIFY_WRITE, datap, | |
350 | sizeof(struct user_fxsr_struct))) { | |
351 | ret = -EIO; | |
352 | break; | |
353 | } | |
354 | if (!tsk_used_math(child)) | |
355 | init_fpu(child); | |
356 | ret = get_fpxregs((struct user_fxsr_struct __user *)data, child); | |
357 | break; | |
358 | } | |
359 | ||
360 | case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */ | |
361 | if (!access_ok(VERIFY_READ, datap, | |
362 | sizeof(struct user_fxsr_struct))) { | |
363 | ret = -EIO; | |
364 | break; | |
365 | } | |
366 | set_stopped_child_used_math(child); | |
367 | ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data); | |
368 | break; | |
369 | } | |
370 | ||
371 | case PTRACE_GET_THREAD_AREA: | |
efd1ca52 RM |
372 | if (addr < 0) |
373 | return -EIO; | |
374 | ret = do_get_thread_area(child, addr, | |
375 | (struct user_desc __user *) data); | |
1da177e4 LT |
376 | break; |
377 | ||
378 | case PTRACE_SET_THREAD_AREA: | |
efd1ca52 RM |
379 | if (addr < 0) |
380 | return -EIO; | |
381 | ret = do_set_thread_area(child, addr, | |
382 | (struct user_desc __user *) data, 0); | |
1da177e4 LT |
383 | break; |
384 | ||
385 | default: | |
386 | ret = ptrace_request(child, request, addr, data); | |
387 | break; | |
388 | } | |
d9771e8c | 389 | |
1da177e4 LT |
390 | return ret; |
391 | } | |
392 | ||
393 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code) | |
394 | { | |
395 | struct siginfo info; | |
396 | ||
397 | tsk->thread.trap_no = 1; | |
398 | tsk->thread.error_code = error_code; | |
399 | ||
400 | memset(&info, 0, sizeof(info)); | |
401 | info.si_signo = SIGTRAP; | |
402 | info.si_code = TRAP_BRKPT; | |
403 | ||
65ea5b03 PA |
404 | /* User-mode ip? */ |
405 | info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL; | |
1da177e4 | 406 | |
27b46d76 | 407 | /* Send us the fake SIGTRAP */ |
1da177e4 LT |
408 | force_sig_info(SIGTRAP, &info, tsk); |
409 | } | |
410 | ||
411 | /* notification of system call entry/exit | |
412 | * - triggered by current->work.syscall_trace | |
413 | */ | |
414 | __attribute__((regparm(3))) | |
ed75e8d5 | 415 | int do_syscall_trace(struct pt_regs *regs, int entryexit) |
1da177e4 | 416 | { |
4c7fc722 AA |
417 | int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU); |
418 | /* | |
419 | * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall | |
420 | * interception | |
421 | */ | |
1b38f006 | 422 | int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP); |
4c7fc722 | 423 | int ret = 0; |
1b38f006 | 424 | |
1da177e4 | 425 | /* do the secure computing check first */ |
4c7fc722 | 426 | if (!entryexit) |
65ea5b03 | 427 | secure_computing(regs->orig_ax); |
1da177e4 | 428 | |
ab1c23c2 BS |
429 | if (unlikely(current->audit_context)) { |
430 | if (entryexit) | |
65ea5b03 PA |
431 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), |
432 | regs->ax); | |
ab1c23c2 BS |
433 | /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only |
434 | * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is | |
435 | * not used, entry.S will call us only on syscall exit, not | |
436 | * entry; so when TIF_SYSCALL_AUDIT is used we must avoid | |
437 | * calling send_sigtrap() on syscall entry. | |
438 | * | |
439 | * Note that when PTRACE_SYSEMU_SINGLESTEP is used, | |
440 | * is_singlestep is false, despite his name, so we will still do | |
441 | * the correct thing. | |
442 | */ | |
443 | else if (is_singlestep) | |
444 | goto out; | |
445 | } | |
1da177e4 LT |
446 | |
447 | if (!(current->ptrace & PT_PTRACED)) | |
2fd6f58b | 448 | goto out; |
1da177e4 | 449 | |
1b38f006 BS |
450 | /* If a process stops on the 1st tracepoint with SYSCALL_TRACE |
451 | * and then is resumed with SYSEMU_SINGLESTEP, it will come in | |
452 | * here. We have to check this and return */ | |
453 | if (is_sysemu && entryexit) | |
454 | return 0; | |
ed75e8d5 | 455 | |
1da177e4 | 456 | /* Fake a debug trap */ |
c8c86cec | 457 | if (is_singlestep) |
1da177e4 LT |
458 | send_sigtrap(current, regs, 0); |
459 | ||
c8c86cec | 460 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu) |
2fd6f58b | 461 | goto out; |
1da177e4 LT |
462 | |
463 | /* the 0x80 provides a way for the tracing parent to distinguish | |
464 | between a syscall stop and SIGTRAP delivery */ | |
ed75e8d5 | 465 | /* Note that the debugger could change the result of test_thread_flag!*/ |
4c7fc722 | 466 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0)); |
1da177e4 LT |
467 | |
468 | /* | |
469 | * this isn't the same as continuing with a signal, but it will do | |
470 | * for normal use. strace only continues with a signal if the | |
471 | * stopping signal is not SIGTRAP. -brl | |
472 | */ | |
473 | if (current->exit_code) { | |
474 | send_sig(current->exit_code, current, 1); | |
475 | current->exit_code = 0; | |
476 | } | |
ed75e8d5 | 477 | ret = is_sysemu; |
4c7fc722 | 478 | out: |
2fd6f58b | 479 | if (unlikely(current->audit_context) && !entryexit) |
65ea5b03 PA |
480 | audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax, |
481 | regs->bx, regs->cx, regs->dx, regs->si); | |
c8c86cec BS |
482 | if (ret == 0) |
483 | return 0; | |
484 | ||
65ea5b03 | 485 | regs->orig_ax = -1; /* force skip of syscall restarting */ |
c8c86cec | 486 | if (unlikely(current->audit_context)) |
65ea5b03 | 487 | audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax); |
c8c86cec | 488 | return 1; |
1da177e4 | 489 | } |