2 /* By Ross Biro 1/23/92 */
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * x86-64 port 2000-2002 Andi Kleen
10 #include <linux/kernel.h>
11 #include <linux/sched.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>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/processor.h>
27 #include <asm/debugreg.h>
30 #include <asm/proto.h>
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
38 /* determines which flags the user has access to. */
39 /* 1 = access 0 = no access */
40 #define FLAG_MASK 0x44dd5UL
42 /* set's the trap flag. */
43 #define TRAP_FLAG 0x100UL
46 * eflags and offset of eflags on child stack..
48 #define EFLAGS offsetof(struct pt_regs, eflags)
49 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
52 * this routine will get a word off of the processes privileged stack.
53 * the offset is how far from the base addr as stored in the TSS.
54 * this routine assumes that all the privileged stacks are in our
57 static inline unsigned long get_stack_long(struct task_struct
*task
, int offset
)
61 stack
= (unsigned char *)task
->thread
.rsp0
;
63 return (*((unsigned long *)stack
));
66 static inline struct pt_regs
*get_child_regs(struct task_struct
*task
)
68 struct pt_regs
*regs
= (void *)task
->thread
.rsp0
;
73 * this routine will put a word on the processes privileged stack.
74 * the offset is how far from the base addr as stored in the TSS.
75 * this routine assumes that all the privileged stacks are in our
78 static inline long put_stack_long(struct task_struct
*task
, int offset
,
81 unsigned char * stack
;
83 stack
= (unsigned char *) task
->thread
.rsp0
;
85 *(unsigned long *) stack
= data
;
91 unsigned long convert_rip_to_linear(struct task_struct
*child
, struct pt_regs
*regs
)
93 unsigned long addr
, seg
;
96 seg
= regs
->cs
& 0xffff;
99 * We'll assume that the code segments in the GDT
100 * are all zero-based. That is largely true: the
101 * TLS segments are used for data, and the PNPBIOS
102 * and APM bios ones we just ignore here.
104 if (seg
& LDT_SEGMENT
) {
108 down(&child
->mm
->context
.sem
);
109 desc
= child
->mm
->context
.ldt
+ (seg
& ~7);
110 base
= (desc
[0] >> 16) | ((desc
[1] & 0xff) << 16) | (desc
[1] & 0xff000000);
112 /* 16-bit code segment? */
113 if (!((desc
[1] >> 22) & 1))
116 up(&child
->mm
->context
.sem
);
121 static int is_at_popf(struct task_struct
*child
, struct pt_regs
*regs
)
124 unsigned char opcode
[16];
125 unsigned long addr
= convert_rip_to_linear(child
, regs
);
127 copied
= access_process_vm(child
, addr
, opcode
, sizeof(opcode
), 0);
128 for (i
= 0; i
< copied
; i
++) {
136 /* opcode and address size prefixes */
137 case 0x66: case 0x67:
139 /* irrelevant prefixes (segment overrides and repeats) */
140 case 0x26: case 0x2e:
141 case 0x36: case 0x3e:
142 case 0x64: case 0x65:
143 case 0xf0: case 0xf2: case 0xf3:
150 /* CHECKME: f0, f2, f3 */
153 * pushf: NOTE! We should probably not let
154 * the user see the TF bit being set. But
155 * it's more pain than it's worth to avoid
156 * it, and a debugger could emulate this
157 * all in user space if it _really_ cares.
167 static void set_singlestep(struct task_struct
*child
)
169 struct pt_regs
*regs
= get_child_regs(child
);
172 * Always set TIF_SINGLESTEP - this guarantees that
173 * we single-step system calls etc.. This will also
174 * cause us to set TF when returning to user mode.
176 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
179 * If TF was already set, don't do anything else
181 if (regs
->eflags
& TRAP_FLAG
)
184 /* Set TF on the kernel stack.. */
185 regs
->eflags
|= TRAP_FLAG
;
188 * ..but if TF is changed by the instruction we will trace,
189 * don't mark it as being "us" that set it, so that we
190 * won't clear it by hand later.
192 * AK: this is not enough, LAHF and IRET can change TF in user space too.
194 if (is_at_popf(child
, regs
))
197 child
->ptrace
|= PT_DTRACE
;
200 static void clear_singlestep(struct task_struct
*child
)
202 /* Always clear TIF_SINGLESTEP... */
203 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
205 /* But touch TF only if it was set by us.. */
206 if (child
->ptrace
& PT_DTRACE
) {
207 struct pt_regs
*regs
= get_child_regs(child
);
208 regs
->eflags
&= ~TRAP_FLAG
;
209 child
->ptrace
&= ~PT_DTRACE
;
214 * Called by kernel/ptrace.c when detaching..
216 * Make sure the single step bit is not set.
218 void ptrace_disable(struct task_struct
*child
)
220 clear_singlestep(child
);
223 static int putreg(struct task_struct
*child
,
224 unsigned long regno
, unsigned long value
)
228 /* Some code in the 64bit emulation may not be 64bit clean.
229 Don't take any chances. */
230 if (test_tsk_thread_flag(child
, TIF_IA32
))
233 case offsetof(struct user_regs_struct
,fs
):
234 if (value
&& (value
& 3) != 3)
236 child
->thread
.fsindex
= value
& 0xffff;
238 case offsetof(struct user_regs_struct
,gs
):
239 if (value
&& (value
& 3) != 3)
241 child
->thread
.gsindex
= value
& 0xffff;
243 case offsetof(struct user_regs_struct
,ds
):
244 if (value
&& (value
& 3) != 3)
246 child
->thread
.ds
= value
& 0xffff;
248 case offsetof(struct user_regs_struct
,es
):
249 if (value
&& (value
& 3) != 3)
251 child
->thread
.es
= value
& 0xffff;
253 case offsetof(struct user_regs_struct
,ss
):
254 if ((value
& 3) != 3)
258 case offsetof(struct user_regs_struct
,fs_base
):
259 if (!((value
>> 48) == 0 || (value
>> 48) == 0xffff))
261 child
->thread
.fs
= value
;
263 case offsetof(struct user_regs_struct
,gs_base
):
264 if (!((value
>> 48) == 0 || (value
>> 48) == 0xffff))
266 child
->thread
.gs
= value
;
268 case offsetof(struct user_regs_struct
, eflags
):
270 tmp
= get_stack_long(child
, EFL_OFFSET
);
274 case offsetof(struct user_regs_struct
,cs
):
275 if ((value
& 3) != 3)
280 put_stack_long(child
, regno
- sizeof(struct pt_regs
), value
);
284 static unsigned long getreg(struct task_struct
*child
, unsigned long regno
)
288 case offsetof(struct user_regs_struct
, fs
):
289 return child
->thread
.fsindex
;
290 case offsetof(struct user_regs_struct
, gs
):
291 return child
->thread
.gsindex
;
292 case offsetof(struct user_regs_struct
, ds
):
293 return child
->thread
.ds
;
294 case offsetof(struct user_regs_struct
, es
):
295 return child
->thread
.es
;
296 case offsetof(struct user_regs_struct
, fs_base
):
297 return child
->thread
.fs
;
298 case offsetof(struct user_regs_struct
, gs_base
):
299 return child
->thread
.gs
;
301 regno
= regno
- sizeof(struct pt_regs
);
302 val
= get_stack_long(child
, regno
);
303 if (test_tsk_thread_flag(child
, TIF_IA32
))
310 asmlinkage
long sys_ptrace(long request
, long pid
, unsigned long addr
, long data
)
312 struct task_struct
*child
;
316 /* This lock_kernel fixes a subtle race with suid exec */
319 if (request
== PTRACE_TRACEME
) {
320 /* are we already being traced? */
321 if (current
->ptrace
& PT_PTRACED
)
323 ret
= security_ptrace(current
->parent
, current
);
326 /* set the ptrace bit in the process flags. */
327 current
->ptrace
|= PT_PTRACED
;
332 read_lock(&tasklist_lock
);
333 child
= find_task_by_pid(pid
);
335 get_task_struct(child
);
336 read_unlock(&tasklist_lock
);
341 if (pid
== 1) /* you may not mess with init */
344 if (request
== PTRACE_ATTACH
) {
345 ret
= ptrace_attach(child
);
348 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
353 /* when I and D space are separate, these will need to be fixed. */
354 case PTRACE_PEEKTEXT
: /* read word at location addr. */
355 case PTRACE_PEEKDATA
: {
359 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
361 if (copied
!= sizeof(tmp
))
363 ret
= put_user(tmp
,(unsigned long __user
*) data
);
367 /* read the word at location addr in the USER area. */
368 case PTRACE_PEEKUSR
: {
373 addr
> sizeof(struct user
) - 7)
377 case 0 ... sizeof(struct user_regs_struct
):
378 tmp
= getreg(child
, addr
);
380 case offsetof(struct user
, u_debugreg
[0]):
381 tmp
= child
->thread
.debugreg0
;
383 case offsetof(struct user
, u_debugreg
[1]):
384 tmp
= child
->thread
.debugreg1
;
386 case offsetof(struct user
, u_debugreg
[2]):
387 tmp
= child
->thread
.debugreg2
;
389 case offsetof(struct user
, u_debugreg
[3]):
390 tmp
= child
->thread
.debugreg3
;
392 case offsetof(struct user
, u_debugreg
[6]):
393 tmp
= child
->thread
.debugreg6
;
395 case offsetof(struct user
, u_debugreg
[7]):
396 tmp
= child
->thread
.debugreg7
;
402 ret
= put_user(tmp
,(unsigned long __user
*) data
);
406 /* when I and D space are separate, this will have to be fixed. */
407 case PTRACE_POKETEXT
: /* write the word at location addr. */
408 case PTRACE_POKEDATA
:
410 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
415 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
418 addr
> sizeof(struct user
) - 7)
422 case 0 ... sizeof(struct user_regs_struct
):
423 ret
= putreg(child
, addr
, data
);
425 /* Disallows to set a breakpoint into the vsyscall */
426 case offsetof(struct user
, u_debugreg
[0]):
427 if (data
>= TASK_SIZE
-7) break;
428 child
->thread
.debugreg0
= data
;
431 case offsetof(struct user
, u_debugreg
[1]):
432 if (data
>= TASK_SIZE
-7) break;
433 child
->thread
.debugreg1
= data
;
436 case offsetof(struct user
, u_debugreg
[2]):
437 if (data
>= TASK_SIZE
-7) break;
438 child
->thread
.debugreg2
= data
;
441 case offsetof(struct user
, u_debugreg
[3]):
442 if (data
>= TASK_SIZE
-7) break;
443 child
->thread
.debugreg3
= data
;
446 case offsetof(struct user
, u_debugreg
[6]):
449 child
->thread
.debugreg6
= data
;
452 case offsetof(struct user
, u_debugreg
[7]):
453 /* See arch/i386/kernel/ptrace.c for an explanation of
454 * this awkward check.*/
455 data
&= ~DR_CONTROL_RESERVED
;
457 if ((0x5454 >> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
460 child
->thread
.debugreg7
= data
;
466 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
467 case PTRACE_CONT
: /* restart after signal. */
470 if ((unsigned long) data
> _NSIG
)
472 if (request
== PTRACE_SYSCALL
)
473 set_tsk_thread_flag(child
,TIF_SYSCALL_TRACE
);
475 clear_tsk_thread_flag(child
,TIF_SYSCALL_TRACE
);
476 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
477 child
->exit_code
= data
;
478 /* make sure the single step bit is not set. */
479 clear_singlestep(child
);
480 wake_up_process(child
);
484 #ifdef CONFIG_IA32_EMULATION
485 /* This makes only sense with 32bit programs. Allow a
486 64bit debugger to fully examine them too. Better
487 don't use it against 64bit processes, use
488 PTRACE_ARCH_PRCTL instead. */
489 case PTRACE_SET_THREAD_AREA
: {
490 struct user_desc __user
*p
;
492 p
= (struct user_desc __user
*)data
;
493 get_user(old
, &p
->entry_number
);
494 put_user(addr
, &p
->entry_number
);
495 ret
= do_set_thread_area(&child
->thread
, p
);
496 put_user(old
, &p
->entry_number
);
498 case PTRACE_GET_THREAD_AREA
:
499 p
= (struct user_desc __user
*)data
;
500 get_user(old
, &p
->entry_number
);
501 put_user(addr
, &p
->entry_number
);
502 ret
= do_get_thread_area(&child
->thread
, p
);
503 put_user(old
, &p
->entry_number
);
507 /* normal 64bit interface to access TLS data.
508 Works just like arch_prctl, except that the arguments
510 case PTRACE_ARCH_PRCTL
:
511 ret
= do_arch_prctl(child
, data
, addr
);
515 * make the child exit. Best I can do is send it a sigkill.
516 * perhaps it should be put in the status that it wants to
521 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
523 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
524 child
->exit_code
= SIGKILL
;
525 /* make sure the single step bit is not set. */
526 clear_singlestep(child
);
527 wake_up_process(child
);
530 case PTRACE_SINGLESTEP
: /* set the trap flag. */
532 if ((unsigned long) data
> _NSIG
)
534 clear_tsk_thread_flag(child
,TIF_SYSCALL_TRACE
);
535 set_singlestep(child
);
536 child
->exit_code
= data
;
537 /* give it a chance to run. */
538 wake_up_process(child
);
543 /* detach a process that was attached. */
544 ret
= ptrace_detach(child
, data
);
547 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
548 if (!access_ok(VERIFY_WRITE
, (unsigned __user
*)data
,
549 sizeof(struct user_regs_struct
))) {
554 for (ui
= 0; ui
< sizeof(struct user_regs_struct
); ui
+= sizeof(long)) {
555 ret
|= __put_user(getreg(child
, ui
),(unsigned long __user
*) data
);
556 data
+= sizeof(long);
561 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
563 if (!access_ok(VERIFY_READ
, (unsigned __user
*)data
,
564 sizeof(struct user_regs_struct
))) {
569 for (ui
= 0; ui
< sizeof(struct user_regs_struct
); ui
+= sizeof(long)) {
570 ret
|= __get_user(tmp
, (unsigned long __user
*) data
);
571 putreg(child
, ui
, tmp
);
572 data
+= sizeof(long);
577 case PTRACE_GETFPREGS
: { /* Get the child extended FPU state. */
578 if (!access_ok(VERIFY_WRITE
, (unsigned __user
*)data
,
579 sizeof(struct user_i387_struct
))) {
583 ret
= get_fpregs((struct user_i387_struct __user
*)data
, child
);
587 case PTRACE_SETFPREGS
: { /* Set the child extended FPU state. */
588 if (!access_ok(VERIFY_READ
, (unsigned __user
*)data
,
589 sizeof(struct user_i387_struct
))) {
593 set_stopped_child_used_math(child
);
594 ret
= set_fpregs(child
, (struct user_i387_struct __user
*)data
);
599 ret
= ptrace_request(child
, request
, addr
, data
);
603 put_task_struct(child
);
609 static void syscall_trace(struct pt_regs
*regs
)
613 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
615 regs
->rip
, regs
->rsp
, regs
->rax
, regs
->orig_rax
, __builtin_return_address(0),
616 current_thread_info()->flags
, current
->ptrace
);
619 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
622 * this isn't the same as continuing with a signal, but it will do
623 * for normal use. strace only continues with a signal if the
624 * stopping signal is not SIGTRAP. -brl
626 if (current
->exit_code
) {
627 send_sig(current
->exit_code
, current
, 1);
628 current
->exit_code
= 0;
632 asmlinkage
void syscall_trace_enter(struct pt_regs
*regs
)
634 /* do the secure computing check first */
635 secure_computing(regs
->orig_rax
);
637 if (unlikely(current
->audit_context
))
638 audit_syscall_entry(current
, regs
->orig_rax
,
639 regs
->rdi
, regs
->rsi
,
640 regs
->rdx
, regs
->r10
);
642 if (test_thread_flag(TIF_SYSCALL_TRACE
)
643 && (current
->ptrace
& PT_PTRACED
))
647 asmlinkage
void syscall_trace_leave(struct pt_regs
*regs
)
649 if (unlikely(current
->audit_context
))
650 audit_syscall_exit(current
, regs
->rax
);
652 if ((test_thread_flag(TIF_SYSCALL_TRACE
)
653 || test_thread_flag(TIF_SINGLESTEP
))
654 && (current
->ptrace
& PT_PTRACED
))