2 /* By Ross Biro 1/23/92 */
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/processor.h>
24 #include <asm/debugreg.h>
29 * does not yet catch signals sent when the child dies.
30 * in exit.c or in signal.c.
33 /* determines which flags the user has access to. */
34 /* 1 = access 0 = no access */
35 #define FLAG_MASK 0x00044dd5
37 /* set's the trap flag. */
38 #define TRAP_FLAG 0x100
41 * Offset of eflags on child stack..
43 #define EFL_OFFSET ((EFL-2)*4-sizeof(struct pt_regs))
46 * this routine will get a word off of the processes privileged stack.
47 * the offset is how far from the base addr as stored in the TSS.
48 * this routine assumes that all the privileged stacks are in our
51 static inline int get_stack_long(struct task_struct
*task
, int offset
)
55 stack
= (unsigned char *)task
->thread
.esp0
;
57 return (*((int *)stack
));
61 * this routine will put a word on the processes privileged stack.
62 * the offset is how far from the base addr as stored in the TSS.
63 * this routine assumes that all the privileged stacks are in our
66 static inline int put_stack_long(struct task_struct
*task
, int offset
,
69 unsigned char * stack
;
71 stack
= (unsigned char *) task
->thread
.esp0
;
73 *(unsigned long *) stack
= data
;
77 static int putreg(struct task_struct
*child
,
78 unsigned long regno
, unsigned long value
)
82 if (value
&& (value
& 3) != 3)
84 child
->thread
.fs
= value
;
87 if (value
&& (value
& 3) != 3)
89 child
->thread
.gs
= value
;
93 if (value
&& (value
& 3) != 3)
105 value
|= get_stack_long(child
, EFL_OFFSET
) & ~FLAG_MASK
;
110 put_stack_long(child
, regno
- sizeof(struct pt_regs
), value
);
114 static unsigned long getreg(struct task_struct
*child
,
117 unsigned long retval
= ~0UL;
119 switch (regno
>> 2) {
121 retval
= child
->thread
.fs
;
124 retval
= child
->thread
.gs
;
135 regno
= regno
- sizeof(struct pt_regs
);
136 retval
&= get_stack_long(child
, regno
);
142 * Called by kernel/ptrace.c when detaching..
144 * Make sure the single step bit is not set.
146 void ptrace_disable(struct task_struct
*child
)
150 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
151 tmp
= get_stack_long(child
, EFL_OFFSET
) & ~TRAP_FLAG
;
152 put_stack_long(child
, EFL_OFFSET
, tmp
);
156 * Perform get_thread_area on behalf of the traced child.
159 ptrace_get_thread_area(struct task_struct
*child
,
160 int idx
, struct user_desc __user
*user_desc
)
162 struct user_desc info
;
163 struct desc_struct
*desc
;
166 * Get the current Thread-Local Storage area:
169 #define GET_BASE(desc) ( \
170 (((desc)->a >> 16) & 0x0000ffff) | \
171 (((desc)->b << 16) & 0x00ff0000) | \
172 ( (desc)->b & 0xff000000) )
174 #define GET_LIMIT(desc) ( \
175 ((desc)->a & 0x0ffff) | \
176 ((desc)->b & 0xf0000) )
178 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
179 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
180 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
181 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
182 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
183 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
185 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
188 desc
= child
->thread
.tls_array
+ idx
- GDT_ENTRY_TLS_MIN
;
190 info
.entry_number
= idx
;
191 info
.base_addr
= GET_BASE(desc
);
192 info
.limit
= GET_LIMIT(desc
);
193 info
.seg_32bit
= GET_32BIT(desc
);
194 info
.contents
= GET_CONTENTS(desc
);
195 info
.read_exec_only
= !GET_WRITABLE(desc
);
196 info
.limit_in_pages
= GET_LIMIT_PAGES(desc
);
197 info
.seg_not_present
= !GET_PRESENT(desc
);
198 info
.useable
= GET_USEABLE(desc
);
200 if (copy_to_user(user_desc
, &info
, sizeof(info
)))
207 * Perform set_thread_area on behalf of the traced child.
210 ptrace_set_thread_area(struct task_struct
*child
,
211 int idx
, struct user_desc __user
*user_desc
)
213 struct user_desc info
;
214 struct desc_struct
*desc
;
216 if (copy_from_user(&info
, user_desc
, sizeof(info
)))
219 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
222 desc
= child
->thread
.tls_array
+ idx
- GDT_ENTRY_TLS_MIN
;
223 if (LDT_empty(&info
)) {
227 desc
->a
= LDT_entry_a(&info
);
228 desc
->b
= LDT_entry_b(&info
);
234 asmlinkage
int sys_ptrace(long request
, long pid
, long addr
, long data
)
236 struct task_struct
*child
;
237 struct user
* dummy
= NULL
;
239 unsigned long __user
*datap
= (unsigned long __user
*)data
;
243 if (request
== PTRACE_TRACEME
) {
244 /* are we already being traced? */
245 if (current
->ptrace
& PT_PTRACED
)
247 ret
= security_ptrace(current
->parent
, current
);
250 /* set the ptrace bit in the process flags. */
251 current
->ptrace
|= PT_PTRACED
;
256 read_lock(&tasklist_lock
);
257 child
= find_task_by_pid(pid
);
259 get_task_struct(child
);
260 read_unlock(&tasklist_lock
);
265 if (pid
== 1) /* you may not mess with init */
268 if (request
== PTRACE_ATTACH
) {
269 ret
= ptrace_attach(child
);
273 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
278 /* when I and D space are separate, these will need to be fixed. */
279 case PTRACE_PEEKTEXT
: /* read word at location addr. */
280 case PTRACE_PEEKDATA
: {
284 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
286 if (copied
!= sizeof(tmp
))
288 ret
= put_user(tmp
, datap
);
292 /* read the word at location addr in the USER area. */
293 case PTRACE_PEEKUSR
: {
297 if ((addr
& 3) || addr
< 0 ||
298 addr
> sizeof(struct user
) - 3)
301 tmp
= 0; /* Default return condition */
302 if(addr
< FRAME_SIZE
*sizeof(long))
303 tmp
= getreg(child
, addr
);
304 if(addr
>= (long) &dummy
->u_debugreg
[0] &&
305 addr
<= (long) &dummy
->u_debugreg
[7]){
306 addr
-= (long) &dummy
->u_debugreg
[0];
308 tmp
= child
->thread
.debugreg
[addr
];
310 ret
= put_user(tmp
, datap
);
314 /* when I and D space are separate, this will have to be fixed. */
315 case PTRACE_POKETEXT
: /* write the word at location addr. */
316 case PTRACE_POKEDATA
:
318 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
323 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
325 if ((addr
& 3) || addr
< 0 ||
326 addr
> sizeof(struct user
) - 3)
329 if (addr
< FRAME_SIZE
*sizeof(long)) {
330 ret
= putreg(child
, addr
, data
);
333 /* We need to be very careful here. We implicitly
334 want to modify a portion of the task_struct, and we
335 have to be selective about what portions we allow someone
339 if(addr
>= (long) &dummy
->u_debugreg
[0] &&
340 addr
<= (long) &dummy
->u_debugreg
[7]){
342 if(addr
== (long) &dummy
->u_debugreg
[4]) break;
343 if(addr
== (long) &dummy
->u_debugreg
[5]) break;
344 if(addr
< (long) &dummy
->u_debugreg
[4] &&
345 ((unsigned long) data
) >= TASK_SIZE
-3) break;
347 if(addr
== (long) &dummy
->u_debugreg
[7]) {
348 data
&= ~DR_CONTROL_RESERVED
;
350 if ((0x5f54 >> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
354 addr
-= (long) &dummy
->u_debugreg
;
356 child
->thread
.debugreg
[addr
] = data
;
361 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
362 case PTRACE_CONT
: { /* restart after signal. */
366 if ((unsigned long) data
> _NSIG
)
368 if (request
== PTRACE_SYSCALL
) {
369 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
372 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
374 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
375 child
->exit_code
= data
;
376 /* make sure the single step bit is not set. */
377 tmp
= get_stack_long(child
, EFL_OFFSET
) & ~TRAP_FLAG
;
378 put_stack_long(child
, EFL_OFFSET
,tmp
);
379 wake_up_process(child
);
385 * make the child exit. Best I can do is send it a sigkill.
386 * perhaps it should be put in the status that it wants to
393 if (child
->state
== TASK_ZOMBIE
) /* already dead */
395 child
->exit_code
= SIGKILL
;
396 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
397 /* make sure the single step bit is not set. */
398 tmp
= get_stack_long(child
, EFL_OFFSET
) & ~TRAP_FLAG
;
399 put_stack_long(child
, EFL_OFFSET
, tmp
);
400 wake_up_process(child
);
404 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
408 if ((unsigned long) data
> _NSIG
)
410 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
411 if ((child
->ptrace
& PT_DTRACE
) == 0) {
412 /* Spurious delayed TF traps may occur */
413 child
->ptrace
|= PT_DTRACE
;
415 tmp
= get_stack_long(child
, EFL_OFFSET
) | TRAP_FLAG
;
416 put_stack_long(child
, EFL_OFFSET
, tmp
);
417 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
418 child
->exit_code
= data
;
419 /* give it a chance to run. */
420 wake_up_process(child
);
426 /* detach a process that was attached. */
427 ret
= ptrace_detach(child
, data
);
430 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
431 if (!access_ok(VERIFY_WRITE
, datap
, FRAME_SIZE
*sizeof(long))) {
435 for ( i
= 0; i
< FRAME_SIZE
*sizeof(long); i
+= sizeof(long) ) {
436 __put_user(getreg(child
, i
), datap
);
443 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
445 if (!access_ok(VERIFY_READ
, datap
, FRAME_SIZE
*sizeof(long))) {
449 for ( i
= 0; i
< FRAME_SIZE
*sizeof(long); i
+= sizeof(long) ) {
450 __get_user(tmp
, datap
);
451 putreg(child
, i
, tmp
);
458 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
459 if (!access_ok(VERIFY_WRITE
, datap
,
460 sizeof(struct user_i387_struct
))) {
465 if (!child
->used_math
)
467 get_fpregs((struct user_i387_struct __user
*)data
, child
);
471 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
472 if (!access_ok(VERIFY_READ
, datap
,
473 sizeof(struct user_i387_struct
))) {
477 child
->used_math
= 1;
478 set_fpregs(child
, (struct user_i387_struct __user
*)data
);
483 case PTRACE_GETFPXREGS
: { /* Get the child extended FPU state. */
484 if (!access_ok(VERIFY_WRITE
, datap
,
485 sizeof(struct user_fxsr_struct
))) {
489 if (!child
->used_math
)
491 ret
= get_fpxregs((struct user_fxsr_struct __user
*)data
, child
);
495 case PTRACE_SETFPXREGS
: { /* Set the child extended FPU state. */
496 if (!access_ok(VERIFY_READ
, datap
,
497 sizeof(struct user_fxsr_struct
))) {
501 child
->used_math
= 1;
502 ret
= set_fpxregs(child
, (struct user_fxsr_struct __user
*)data
);
506 case PTRACE_GET_THREAD_AREA
:
507 ret
= ptrace_get_thread_area(child
, addr
,
508 (struct user_desc __user
*) data
);
511 case PTRACE_SET_THREAD_AREA
:
512 ret
= ptrace_set_thread_area(child
, addr
,
513 (struct user_desc __user
*) data
);
517 ret
= ptrace_request(child
, request
, addr
, data
);
521 put_task_struct(child
);
527 /* notification of system call entry/exit
528 * - triggered by current->work.syscall_trace
530 __attribute__((regparm(3)))
531 void do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
533 if (unlikely(current
->audit_context
)) {
535 audit_syscall_entry(current
, regs
->orig_eax
,
536 regs
->ebx
, regs
->ecx
,
537 regs
->edx
, regs
->esi
);
539 audit_syscall_exit(current
, regs
->eax
);
542 if (!test_thread_flag(TIF_SYSCALL_TRACE
) &&
543 !test_thread_flag(TIF_SINGLESTEP
))
545 if (!(current
->ptrace
& PT_PTRACED
))
547 /* the 0x80 provides a way for the tracing parent to distinguish
548 between a syscall stop and SIGTRAP delivery */
549 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) &&
550 !test_thread_flag(TIF_SINGLESTEP
) ? 0x80 : 0));
553 * this isn't the same as continuing with a signal, but it will do
554 * for normal use. strace only continues with a signal if the
555 * stopping signal is not SIGTRAP. -brl
557 if (current
->exit_code
) {
558 send_sig(current
->exit_code
, current
, 1);
559 current
->exit_code
= 0;