[PATCH] fix is_at_popf() for compat tasks
[linux-2.6/verdex.git] / arch / x86_64 / kernel / ptrace.c
blob3a52c7bcfd12b79ef594a5c9f4f732f3e6af0be6
1 /* ptrace.c */
2 /* By Ross Biro 1/23/92 */
3 /*
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
6 *
7 * x86-64 port 2000-2002 Andi Kleen
8 */
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
27 #include <asm/i387.h>
28 #include <asm/debugreg.h>
29 #include <asm/ldt.h>
30 #include <asm/desc.h>
31 #include <asm/proto.h>
32 #include <asm/ia32.h>
35 * does not yet catch signals sent when the child dies.
36 * in exit.c or in signal.c.
40 * Determines which flags the user has access to [1 = access, 0 = no access].
41 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
42 * Also masks reserved bits (63-22, 15, 5, 3, 1).
44 #define FLAG_MASK 0x54dd5UL
46 /* set's the trap flag. */
47 #define TRAP_FLAG 0x100UL
50 * eflags and offset of eflags on child stack..
52 #define EFLAGS offsetof(struct pt_regs, eflags)
53 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
56 * this routine will get a word off of the processes privileged stack.
57 * the offset is how far from the base addr as stored in the TSS.
58 * this routine assumes that all the privileged stacks are in our
59 * data space.
60 */
61 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
63 unsigned char *stack;
65 stack = (unsigned char *)task->thread.rsp0;
66 stack += offset;
67 return (*((unsigned long *)stack));
71 * this routine will put a word on the processes privileged stack.
72 * the offset is how far from the base addr as stored in the TSS.
73 * this routine assumes that all the privileged stacks are in our
74 * data space.
76 static inline long put_stack_long(struct task_struct *task, int offset,
77 unsigned long data)
79 unsigned char * stack;
81 stack = (unsigned char *) task->thread.rsp0;
82 stack += offset;
83 *(unsigned long *) stack = data;
84 return 0;
87 #define LDT_SEGMENT 4
89 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
91 unsigned long addr, seg;
93 addr = regs->rip;
94 seg = regs->cs & 0xffff;
97 * We'll assume that the code segments in the GDT
98 * are all zero-based. That is largely true: the
99 * TLS segments are used for data, and the PNPBIOS
100 * and APM bios ones we just ignore here.
102 if (seg & LDT_SEGMENT) {
103 u32 *desc;
104 unsigned long base;
106 down(&child->mm->context.sem);
107 desc = child->mm->context.ldt + (seg & ~7);
108 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
110 /* 16-bit code segment? */
111 if (!((desc[1] >> 22) & 1))
112 addr &= 0xffff;
113 addr += base;
114 up(&child->mm->context.sem);
116 return addr;
119 static int is_at_popf(struct task_struct *child, struct pt_regs *regs)
121 int i, copied;
122 unsigned char opcode[16];
123 unsigned long addr = convert_rip_to_linear(child, regs);
125 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
126 for (i = 0; i < copied; i++) {
127 switch (opcode[i]) {
128 /* popf */
129 case 0x9d:
130 return 1;
132 /* CHECKME: 64 65 */
134 /* opcode and address size prefixes */
135 case 0x66: case 0x67:
136 continue;
137 /* irrelevant prefixes (segment overrides and repeats) */
138 case 0x26: case 0x2e:
139 case 0x36: case 0x3e:
140 case 0x64: case 0x65:
141 case 0xf0: case 0xf2: case 0xf3:
142 continue;
144 case 0x40 ... 0x4f:
145 if (regs->cs != __USER_CS)
146 /* 32-bit mode: register increment */
147 return 0;
148 /* 64-bit mode: REX prefix */
149 continue;
151 /* CHECKME: f0, f2, f3 */
154 * pushf: NOTE! We should probably not let
155 * the user see the TF bit being set. But
156 * it's more pain than it's worth to avoid
157 * it, and a debugger could emulate this
158 * all in user space if it _really_ cares.
160 case 0x9c:
161 default:
162 return 0;
165 return 0;
168 static void set_singlestep(struct task_struct *child)
170 struct pt_regs *regs = task_pt_regs(child);
173 * Always set TIF_SINGLESTEP - this guarantees that
174 * we single-step system calls etc.. This will also
175 * cause us to set TF when returning to user mode.
177 set_tsk_thread_flag(child, TIF_SINGLESTEP);
180 * If TF was already set, don't do anything else
182 if (regs->eflags & TRAP_FLAG)
183 return;
185 /* Set TF on the kernel stack.. */
186 regs->eflags |= TRAP_FLAG;
189 * ..but if TF is changed by the instruction we will trace,
190 * don't mark it as being "us" that set it, so that we
191 * won't clear it by hand later.
193 * AK: this is not enough, LAHF and IRET can change TF in user space too.
195 if (is_at_popf(child, regs))
196 return;
198 child->ptrace |= PT_DTRACE;
201 static void clear_singlestep(struct task_struct *child)
203 /* Always clear TIF_SINGLESTEP... */
204 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
206 /* But touch TF only if it was set by us.. */
207 if (child->ptrace & PT_DTRACE) {
208 struct pt_regs *regs = task_pt_regs(child);
209 regs->eflags &= ~TRAP_FLAG;
210 child->ptrace &= ~PT_DTRACE;
215 * Called by kernel/ptrace.c when detaching..
217 * Make sure the single step bit is not set.
219 void ptrace_disable(struct task_struct *child)
221 clear_singlestep(child);
224 static int putreg(struct task_struct *child,
225 unsigned long regno, unsigned long value)
227 unsigned long tmp;
229 /* Some code in the 64bit emulation may not be 64bit clean.
230 Don't take any chances. */
231 if (test_tsk_thread_flag(child, TIF_IA32))
232 value &= 0xffffffff;
233 switch (regno) {
234 case offsetof(struct user_regs_struct,fs):
235 if (value && (value & 3) != 3)
236 return -EIO;
237 child->thread.fsindex = value & 0xffff;
238 return 0;
239 case offsetof(struct user_regs_struct,gs):
240 if (value && (value & 3) != 3)
241 return -EIO;
242 child->thread.gsindex = value & 0xffff;
243 return 0;
244 case offsetof(struct user_regs_struct,ds):
245 if (value && (value & 3) != 3)
246 return -EIO;
247 child->thread.ds = value & 0xffff;
248 return 0;
249 case offsetof(struct user_regs_struct,es):
250 if (value && (value & 3) != 3)
251 return -EIO;
252 child->thread.es = value & 0xffff;
253 return 0;
254 case offsetof(struct user_regs_struct,ss):
255 if ((value & 3) != 3)
256 return -EIO;
257 value &= 0xffff;
258 return 0;
259 case offsetof(struct user_regs_struct,fs_base):
260 if (value >= TASK_SIZE_OF(child))
261 return -EIO;
262 child->thread.fs = value;
263 return 0;
264 case offsetof(struct user_regs_struct,gs_base):
265 if (value >= TASK_SIZE_OF(child))
266 return -EIO;
267 child->thread.gs = value;
268 return 0;
269 case offsetof(struct user_regs_struct, eflags):
270 value &= FLAG_MASK;
271 tmp = get_stack_long(child, EFL_OFFSET);
272 tmp &= ~FLAG_MASK;
273 value |= tmp;
274 break;
275 case offsetof(struct user_regs_struct,cs):
276 if ((value & 3) != 3)
277 return -EIO;
278 value &= 0xffff;
279 break;
281 put_stack_long(child, regno - sizeof(struct pt_regs), value);
282 return 0;
285 static unsigned long getreg(struct task_struct *child, unsigned long regno)
287 unsigned long val;
288 switch (regno) {
289 case offsetof(struct user_regs_struct, fs):
290 return child->thread.fsindex;
291 case offsetof(struct user_regs_struct, gs):
292 return child->thread.gsindex;
293 case offsetof(struct user_regs_struct, ds):
294 return child->thread.ds;
295 case offsetof(struct user_regs_struct, es):
296 return child->thread.es;
297 case offsetof(struct user_regs_struct, fs_base):
298 return child->thread.fs;
299 case offsetof(struct user_regs_struct, gs_base):
300 return child->thread.gs;
301 default:
302 regno = regno - sizeof(struct pt_regs);
303 val = get_stack_long(child, regno);
304 if (test_tsk_thread_flag(child, TIF_IA32))
305 val &= 0xffffffff;
306 return val;
311 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
313 long i, ret;
314 unsigned ui;
316 switch (request) {
317 /* when I and D space are separate, these will need to be fixed. */
318 case PTRACE_PEEKTEXT: /* read word at location addr. */
319 case PTRACE_PEEKDATA: {
320 unsigned long tmp;
321 int copied;
323 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
324 ret = -EIO;
325 if (copied != sizeof(tmp))
326 break;
327 ret = put_user(tmp,(unsigned long __user *) data);
328 break;
331 /* read the word at location addr in the USER area. */
332 case PTRACE_PEEKUSR: {
333 unsigned long tmp;
335 ret = -EIO;
336 if ((addr & 7) ||
337 addr > sizeof(struct user) - 7)
338 break;
340 switch (addr) {
341 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
342 tmp = getreg(child, addr);
343 break;
344 case offsetof(struct user, u_debugreg[0]):
345 tmp = child->thread.debugreg0;
346 break;
347 case offsetof(struct user, u_debugreg[1]):
348 tmp = child->thread.debugreg1;
349 break;
350 case offsetof(struct user, u_debugreg[2]):
351 tmp = child->thread.debugreg2;
352 break;
353 case offsetof(struct user, u_debugreg[3]):
354 tmp = child->thread.debugreg3;
355 break;
356 case offsetof(struct user, u_debugreg[6]):
357 tmp = child->thread.debugreg6;
358 break;
359 case offsetof(struct user, u_debugreg[7]):
360 tmp = child->thread.debugreg7;
361 break;
362 default:
363 tmp = 0;
364 break;
366 ret = put_user(tmp,(unsigned long __user *) data);
367 break;
370 /* when I and D space are separate, this will have to be fixed. */
371 case PTRACE_POKETEXT: /* write the word at location addr. */
372 case PTRACE_POKEDATA:
373 ret = 0;
374 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
375 break;
376 ret = -EIO;
377 break;
379 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
381 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
382 ret = -EIO;
383 if ((addr & 7) ||
384 addr > sizeof(struct user) - 7)
385 break;
387 switch (addr) {
388 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
389 ret = putreg(child, addr, data);
390 break;
391 /* Disallows to set a breakpoint into the vsyscall */
392 case offsetof(struct user, u_debugreg[0]):
393 if (data >= TASK_SIZE_OF(child) - dsize) break;
394 child->thread.debugreg0 = data;
395 ret = 0;
396 break;
397 case offsetof(struct user, u_debugreg[1]):
398 if (data >= TASK_SIZE_OF(child) - dsize) break;
399 child->thread.debugreg1 = data;
400 ret = 0;
401 break;
402 case offsetof(struct user, u_debugreg[2]):
403 if (data >= TASK_SIZE_OF(child) - dsize) break;
404 child->thread.debugreg2 = data;
405 ret = 0;
406 break;
407 case offsetof(struct user, u_debugreg[3]):
408 if (data >= TASK_SIZE_OF(child) - dsize) break;
409 child->thread.debugreg3 = data;
410 ret = 0;
411 break;
412 case offsetof(struct user, u_debugreg[6]):
413 if (data >> 32)
414 break;
415 child->thread.debugreg6 = data;
416 ret = 0;
417 break;
418 case offsetof(struct user, u_debugreg[7]):
419 /* See arch/i386/kernel/ptrace.c for an explanation of
420 * this awkward check.*/
421 data &= ~DR_CONTROL_RESERVED;
422 for(i=0; i<4; i++)
423 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
424 break;
425 if (i == 4) {
426 child->thread.debugreg7 = data;
427 if (data)
428 set_tsk_thread_flag(child, TIF_DEBUG);
429 else
430 clear_tsk_thread_flag(child, TIF_DEBUG);
431 ret = 0;
433 break;
435 break;
437 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
438 case PTRACE_CONT: /* restart after signal. */
440 ret = -EIO;
441 if (!valid_signal(data))
442 break;
443 if (request == PTRACE_SYSCALL)
444 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
445 else
446 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
447 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
448 child->exit_code = data;
449 /* make sure the single step bit is not set. */
450 clear_singlestep(child);
451 wake_up_process(child);
452 ret = 0;
453 break;
455 #ifdef CONFIG_IA32_EMULATION
456 /* This makes only sense with 32bit programs. Allow a
457 64bit debugger to fully examine them too. Better
458 don't use it against 64bit processes, use
459 PTRACE_ARCH_PRCTL instead. */
460 case PTRACE_SET_THREAD_AREA: {
461 struct user_desc __user *p;
462 int old;
463 p = (struct user_desc __user *)data;
464 get_user(old, &p->entry_number);
465 put_user(addr, &p->entry_number);
466 ret = do_set_thread_area(&child->thread, p);
467 put_user(old, &p->entry_number);
468 break;
469 case PTRACE_GET_THREAD_AREA:
470 p = (struct user_desc __user *)data;
471 get_user(old, &p->entry_number);
472 put_user(addr, &p->entry_number);
473 ret = do_get_thread_area(&child->thread, p);
474 put_user(old, &p->entry_number);
475 break;
477 #endif
478 /* normal 64bit interface to access TLS data.
479 Works just like arch_prctl, except that the arguments
480 are reversed. */
481 case PTRACE_ARCH_PRCTL:
482 ret = do_arch_prctl(child, data, addr);
483 break;
486 * make the child exit. Best I can do is send it a sigkill.
487 * perhaps it should be put in the status that it wants to
488 * exit.
490 case PTRACE_KILL:
491 ret = 0;
492 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
493 break;
494 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
495 child->exit_code = SIGKILL;
496 /* make sure the single step bit is not set. */
497 clear_singlestep(child);
498 wake_up_process(child);
499 break;
501 case PTRACE_SINGLESTEP: /* set the trap flag. */
502 ret = -EIO;
503 if (!valid_signal(data))
504 break;
505 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
506 set_singlestep(child);
507 child->exit_code = data;
508 /* give it a chance to run. */
509 wake_up_process(child);
510 ret = 0;
511 break;
513 case PTRACE_DETACH:
514 /* detach a process that was attached. */
515 ret = ptrace_detach(child, data);
516 break;
518 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
519 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
520 sizeof(struct user_regs_struct))) {
521 ret = -EIO;
522 break;
524 ret = 0;
525 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
526 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
527 data += sizeof(long);
529 break;
532 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
533 unsigned long tmp;
534 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
535 sizeof(struct user_regs_struct))) {
536 ret = -EIO;
537 break;
539 ret = 0;
540 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
541 ret |= __get_user(tmp, (unsigned long __user *) data);
542 putreg(child, ui, tmp);
543 data += sizeof(long);
545 break;
548 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
549 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
550 sizeof(struct user_i387_struct))) {
551 ret = -EIO;
552 break;
554 ret = get_fpregs((struct user_i387_struct __user *)data, child);
555 break;
558 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
559 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
560 sizeof(struct user_i387_struct))) {
561 ret = -EIO;
562 break;
564 set_stopped_child_used_math(child);
565 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
566 break;
569 default:
570 ret = ptrace_request(child, request, addr, data);
571 break;
573 return ret;
576 static void syscall_trace(struct pt_regs *regs)
579 #if 0
580 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
581 current->comm,
582 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
583 current_thread_info()->flags, current->ptrace);
584 #endif
586 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
587 ? 0x80 : 0));
589 * this isn't the same as continuing with a signal, but it will do
590 * for normal use. strace only continues with a signal if the
591 * stopping signal is not SIGTRAP. -brl
593 if (current->exit_code) {
594 send_sig(current->exit_code, current, 1);
595 current->exit_code = 0;
599 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
601 /* do the secure computing check first */
602 secure_computing(regs->orig_rax);
604 if (test_thread_flag(TIF_SYSCALL_TRACE)
605 && (current->ptrace & PT_PTRACED))
606 syscall_trace(regs);
608 if (unlikely(current->audit_context)) {
609 if (test_thread_flag(TIF_IA32)) {
610 audit_syscall_entry(AUDIT_ARCH_I386,
611 regs->orig_rax,
612 regs->rbx, regs->rcx,
613 regs->rdx, regs->rsi);
614 } else {
615 audit_syscall_entry(AUDIT_ARCH_X86_64,
616 regs->orig_rax,
617 regs->rdi, regs->rsi,
618 regs->rdx, regs->r10);
623 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
625 if (unlikely(current->audit_context))
626 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
628 if ((test_thread_flag(TIF_SYSCALL_TRACE)
629 || test_thread_flag(TIF_SINGLESTEP))
630 && (current->ptrace & PT_PTRACED))
631 syscall_trace(regs);