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>
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>
28 #include <asm/debugreg.h>
31 #include <asm/proto.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
61 static inline unsigned long get_stack_long(struct task_struct
*task
, int offset
)
65 stack
= (unsigned char *)task
->thread
.rsp0
;
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
76 static inline long put_stack_long(struct task_struct
*task
, int offset
,
79 unsigned char * stack
;
81 stack
= (unsigned char *) task
->thread
.rsp0
;
83 *(unsigned long *) stack
= data
;
89 unsigned long convert_rip_to_linear(struct task_struct
*child
, struct pt_regs
*regs
)
91 unsigned long addr
, seg
;
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
) {
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))
114 up(&child
->mm
->context
.sem
);
119 static int is_at_popf(struct task_struct
*child
, struct pt_regs
*regs
)
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
++) {
134 /* opcode and address size prefixes */
135 case 0x66: case 0x67:
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:
145 if (regs
->cs
!= __USER_CS
)
146 /* 32-bit mode: register increment */
148 /* 64-bit mode: REX prefix */
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.
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
)
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
))
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
)
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
))
234 case offsetof(struct user_regs_struct
,fs
):
235 if (value
&& (value
& 3) != 3)
237 child
->thread
.fsindex
= value
& 0xffff;
239 case offsetof(struct user_regs_struct
,gs
):
240 if (value
&& (value
& 3) != 3)
242 child
->thread
.gsindex
= value
& 0xffff;
244 case offsetof(struct user_regs_struct
,ds
):
245 if (value
&& (value
& 3) != 3)
247 child
->thread
.ds
= value
& 0xffff;
249 case offsetof(struct user_regs_struct
,es
):
250 if (value
&& (value
& 3) != 3)
252 child
->thread
.es
= value
& 0xffff;
254 case offsetof(struct user_regs_struct
,ss
):
255 if ((value
& 3) != 3)
259 case offsetof(struct user_regs_struct
,fs_base
):
260 if (value
>= TASK_SIZE_OF(child
))
262 child
->thread
.fs
= value
;
264 case offsetof(struct user_regs_struct
,gs_base
):
265 if (value
>= TASK_SIZE_OF(child
))
267 child
->thread
.gs
= value
;
269 case offsetof(struct user_regs_struct
, eflags
):
271 tmp
= get_stack_long(child
, EFL_OFFSET
);
275 case offsetof(struct user_regs_struct
,cs
):
276 if ((value
& 3) != 3)
281 put_stack_long(child
, regno
- sizeof(struct pt_regs
), value
);
285 static unsigned long getreg(struct task_struct
*child
, unsigned long 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
;
302 regno
= regno
- sizeof(struct pt_regs
);
303 val
= get_stack_long(child
, regno
);
304 if (test_tsk_thread_flag(child
, TIF_IA32
))
311 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
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
: {
323 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
325 if (copied
!= sizeof(tmp
))
327 ret
= put_user(tmp
,(unsigned long __user
*) data
);
331 /* read the word at location addr in the USER area. */
332 case PTRACE_PEEKUSR
: {
337 addr
> sizeof(struct user
) - 7)
341 case 0 ... sizeof(struct user_regs_struct
) - sizeof(long):
342 tmp
= getreg(child
, addr
);
344 case offsetof(struct user
, u_debugreg
[0]):
345 tmp
= child
->thread
.debugreg0
;
347 case offsetof(struct user
, u_debugreg
[1]):
348 tmp
= child
->thread
.debugreg1
;
350 case offsetof(struct user
, u_debugreg
[2]):
351 tmp
= child
->thread
.debugreg2
;
353 case offsetof(struct user
, u_debugreg
[3]):
354 tmp
= child
->thread
.debugreg3
;
356 case offsetof(struct user
, u_debugreg
[6]):
357 tmp
= child
->thread
.debugreg6
;
359 case offsetof(struct user
, u_debugreg
[7]):
360 tmp
= child
->thread
.debugreg7
;
366 ret
= put_user(tmp
,(unsigned long __user
*) data
);
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
:
374 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
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;
384 addr
> sizeof(struct user
) - 7)
388 case 0 ... sizeof(struct user_regs_struct
) - sizeof(long):
389 ret
= putreg(child
, addr
, data
);
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
;
397 case offsetof(struct user
, u_debugreg
[1]):
398 if (data
>= TASK_SIZE_OF(child
) - dsize
) break;
399 child
->thread
.debugreg1
= data
;
402 case offsetof(struct user
, u_debugreg
[2]):
403 if (data
>= TASK_SIZE_OF(child
) - dsize
) break;
404 child
->thread
.debugreg2
= data
;
407 case offsetof(struct user
, u_debugreg
[3]):
408 if (data
>= TASK_SIZE_OF(child
) - dsize
) break;
409 child
->thread
.debugreg3
= data
;
412 case offsetof(struct user
, u_debugreg
[6]):
415 child
->thread
.debugreg6
= data
;
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
;
423 if ((0x5554 >> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
426 child
->thread
.debugreg7
= data
;
428 set_tsk_thread_flag(child
, TIF_DEBUG
);
430 clear_tsk_thread_flag(child
, TIF_DEBUG
);
437 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
438 case PTRACE_CONT
: /* restart after signal. */
441 if (!valid_signal(data
))
443 if (request
== PTRACE_SYSCALL
)
444 set_tsk_thread_flag(child
,TIF_SYSCALL_TRACE
);
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
);
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
;
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
);
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
);
478 /* normal 64bit interface to access TLS data.
479 Works just like arch_prctl, except that the arguments
481 case PTRACE_ARCH_PRCTL
:
482 ret
= do_arch_prctl(child
, data
, addr
);
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
492 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
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
);
501 case PTRACE_SINGLESTEP
: /* set the trap flag. */
503 if (!valid_signal(data
))
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
);
514 /* detach a process that was attached. */
515 ret
= ptrace_detach(child
, data
);
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
))) {
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);
532 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
534 if (!access_ok(VERIFY_READ
, (unsigned __user
*)data
,
535 sizeof(struct user_regs_struct
))) {
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);
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
))) {
554 ret
= get_fpregs((struct user_i387_struct __user
*)data
, child
);
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
))) {
564 set_stopped_child_used_math(child
);
565 ret
= set_fpregs(child
, (struct user_i387_struct __user
*)data
);
570 ret
= ptrace_request(child
, request
, addr
, data
);
576 static void syscall_trace(struct pt_regs
*regs
)
580 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
582 regs
->rip
, regs
->rsp
, regs
->rax
, regs
->orig_rax
, __builtin_return_address(0),
583 current_thread_info()->flags
, current
->ptrace
);
586 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
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
))
608 if (unlikely(current
->audit_context
)) {
609 if (test_thread_flag(TIF_IA32
)) {
610 audit_syscall_entry(AUDIT_ARCH_I386
,
612 regs
->rbx
, regs
->rcx
,
613 regs
->rdx
, regs
->rsi
);
615 audit_syscall_entry(AUDIT_ARCH_X86_64
,
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
))