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);
141 static void set_singlestep(struct task_struct *child)
145 set_tsk_thread_flag(child, TIF_SINGLESTEP);
146 eflags = get_stack_long(child, EFL_OFFSET);
147 put_stack_long(child, EFL_OFFSET, eflags | TRAP_FLAG);
148 child->ptrace |= PT_DTRACE;
151 static void clear_singlestep(struct task_struct *child)
153 if (child->ptrace & PT_DTRACE) {
156 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
157 eflags = get_stack_long(child, EFL_OFFSET);
158 put_stack_long(child, EFL_OFFSET, eflags & ~TRAP_FLAG);
159 child->ptrace &= ~PT_DTRACE;
164 * Called by kernel/ptrace.c when detaching..
166 * Make sure the single step bit is not set.
168 void ptrace_disable(struct task_struct *child)
170 clear_singlestep(child);
174 * Perform get_thread_area on behalf of the traced child.
177 ptrace_get_thread_area(struct task_struct *child,
178 int idx, struct user_desc __user *user_desc)
180 struct user_desc info;
181 struct desc_struct *desc;
184 * Get the current Thread-Local Storage area:
187 #define GET_BASE(desc) ( \
188 (((desc)->a >> 16) & 0x0000ffff) | \
189 (((desc)->b << 16) & 0x00ff0000) | \
190 ( (desc)->b & 0xff000000) )
192 #define GET_LIMIT(desc) ( \
193 ((desc)->a & 0x0ffff) | \
194 ((desc)->b & 0xf0000) )
196 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
197 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
198 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
199 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
200 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
201 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
203 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
206 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
208 info.entry_number = idx;
209 info.base_addr = GET_BASE(desc);
210 info.limit = GET_LIMIT(desc);
211 info.seg_32bit = GET_32BIT(desc);
212 info.contents = GET_CONTENTS(desc);
213 info.read_exec_only = !GET_WRITABLE(desc);
214 info.limit_in_pages = GET_LIMIT_PAGES(desc);
215 info.seg_not_present = !GET_PRESENT(desc);
216 info.useable = GET_USEABLE(desc);
218 if (copy_to_user(user_desc, &info, sizeof(info)))
225 * Perform set_thread_area on behalf of the traced child.
228 ptrace_set_thread_area(struct task_struct *child,
229 int idx, struct user_desc __user *user_desc)
231 struct user_desc info;
232 struct desc_struct *desc;
234 if (copy_from_user(&info, user_desc, sizeof(info)))
237 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
240 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
241 if (LDT_empty(&info)) {
245 desc->a = LDT_entry_a(&info);
246 desc->b = LDT_entry_b(&info);
252 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
254 struct task_struct *child;
255 struct user * dummy = NULL;
257 unsigned long __user *datap = (unsigned long __user *)data;
261 if (request == PTRACE_TRACEME) {
262 /* are we already being traced? */
263 if (current->ptrace & PT_PTRACED)
265 ret = security_ptrace(current->parent, current);
268 /* set the ptrace bit in the process flags. */
269 current->ptrace |= PT_PTRACED;
274 read_lock(&tasklist_lock);
275 child = find_task_by_pid(pid);
277 get_task_struct(child);
278 read_unlock(&tasklist_lock);
281 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
285 if (pid == 1) /* you may not mess with init */
288 if (request == PTRACE_ATTACH) {
289 ret = ptrace_attach(child);
293 ret = ptrace_check_attach(child, request == PTRACE_KILL);
298 /* when I and D space are separate, these will need to be fixed. */
299 case PTRACE_PEEKTEXT: /* read word at location addr. */
300 case PTRACE_PEEKDATA: {
304 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
306 if (copied != sizeof(tmp))
308 ret = put_user(tmp, datap);
312 /* read the word at location addr in the USER area. */
313 case PTRACE_PEEKUSR: {
317 if ((addr & 3) || addr < 0 ||
318 addr > sizeof(struct user) - 3)
321 tmp = 0; /* Default return condition */
322 if(addr < FRAME_SIZE*sizeof(long))
323 tmp = getreg(child, addr);
324 if(addr >= (long) &dummy->u_debugreg[0] &&
325 addr <= (long) &dummy->u_debugreg[7]){
326 addr -= (long) &dummy->u_debugreg[0];
328 tmp = child->thread.debugreg[addr];
330 ret = put_user(tmp, datap);
334 /* when I and D space are separate, this will have to be fixed. */
335 case PTRACE_POKETEXT: /* write the word at location addr. */
336 case PTRACE_POKEDATA:
338 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
343 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
345 if ((addr & 3) || addr < 0 ||
346 addr > sizeof(struct user) - 3)
349 if (addr < FRAME_SIZE*sizeof(long)) {
350 ret = putreg(child, addr, data);
353 /* We need to be very careful here. We implicitly
354 want to modify a portion of the task_struct, and we
355 have to be selective about what portions we allow someone
359 if(addr >= (long) &dummy->u_debugreg[0] &&
360 addr <= (long) &dummy->u_debugreg[7]){
362 if(addr == (long) &dummy->u_debugreg[4]) break;
363 if(addr == (long) &dummy->u_debugreg[5]) break;
364 if(addr < (long) &dummy->u_debugreg[4] &&
365 ((unsigned long) data) >= TASK_SIZE-3) break;
367 /* Sanity-check data. Take one half-byte at once with
368 * check = (val >> (16 + 4*i)) & 0xf. It contains the
369 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
370 * 2 and 3 are LENi. Given a list of invalid values,
371 * we do mask |= 1 << invalid_value, so that
372 * (mask >> check) & 1 is a correct test for invalid
375 * R/Wi contains the type of the breakpoint /
376 * watchpoint, LENi contains the length of the watched
377 * data in the watchpoint case.
379 * The invalid values are:
380 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
381 * - R/Wi == 0x10 (break on I/O reads or writes), so
383 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
386 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
388 * See the Intel Manual "System Programming Guide",
391 * Note that LENi == 0x10 is defined on x86_64 in long
392 * mode (i.e. even for 32-bit userspace software, but
393 * 64-bit kernel), so the x86_64 mask value is 0x5454.
394 * See the AMD manual no. 24593 (AMD64 System
397 if(addr == (long) &dummy->u_debugreg[7]) {
398 data &= ~DR_CONTROL_RESERVED;
400 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
404 addr -= (long) &dummy->u_debugreg;
406 child->thread.debugreg[addr] = data;
411 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
412 case PTRACE_CONT: /* restart after signal. */
414 if ((unsigned long) data > _NSIG)
416 if (request == PTRACE_SYSCALL) {
417 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
420 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
422 child->exit_code = data;
423 /* make sure the single step bit is not set. */
424 clear_singlestep(child);
425 wake_up_process(child);
430 * make the child exit. Best I can do is send it a sigkill.
431 * perhaps it should be put in the status that it wants to
436 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
438 child->exit_code = SIGKILL;
439 /* make sure the single step bit is not set. */
440 clear_singlestep(child);
441 wake_up_process(child);
444 case PTRACE_SINGLESTEP: /* set the trap flag. */
446 if ((unsigned long) data > _NSIG)
448 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
449 set_singlestep(child);
450 child->exit_code = data;
451 /* give it a chance to run. */
452 wake_up_process(child);
457 /* detach a process that was attached. */
458 ret = ptrace_detach(child, data);
461 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
462 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
466 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
467 __put_user(getreg(child, i), datap);
474 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
476 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
480 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
481 __get_user(tmp, datap);
482 putreg(child, i, tmp);
489 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
490 if (!access_ok(VERIFY_WRITE, datap,
491 sizeof(struct user_i387_struct))) {
496 if (!child->used_math)
498 get_fpregs((struct user_i387_struct __user *)data, child);
502 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
503 if (!access_ok(VERIFY_READ, datap,
504 sizeof(struct user_i387_struct))) {
508 child->used_math = 1;
509 set_fpregs(child, (struct user_i387_struct __user *)data);
514 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
515 if (!access_ok(VERIFY_WRITE, datap,
516 sizeof(struct user_fxsr_struct))) {
520 if (!child->used_math)
522 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
526 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
527 if (!access_ok(VERIFY_READ, datap,
528 sizeof(struct user_fxsr_struct))) {
532 child->used_math = 1;
533 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
537 case PTRACE_GET_THREAD_AREA:
538 ret = ptrace_get_thread_area(child, addr,
539 (struct user_desc __user *) data);
542 case PTRACE_SET_THREAD_AREA:
543 ret = ptrace_set_thread_area(child, addr,
544 (struct user_desc __user *) data);
548 ret = ptrace_request(child, request, addr, data);
552 put_task_struct(child);
558 /* notification of system call entry/exit
559 * - triggered by current->work.syscall_trace
561 __attribute__((regparm(3)))
562 void do_syscall_trace(struct pt_regs *regs, int entryexit)
564 if (unlikely(current->audit_context)) {
566 audit_syscall_entry(current, regs->orig_eax,
567 regs->ebx, regs->ecx,
568 regs->edx, regs->esi);
570 audit_syscall_exit(current, regs->eax);
573 if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
574 !test_thread_flag(TIF_SINGLESTEP))
576 if (!(current->ptrace & PT_PTRACED))
578 /* the 0x80 provides a way for the tracing parent to distinguish
579 between a syscall stop and SIGTRAP delivery */
580 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
581 !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
584 * this isn't the same as continuing with a signal, but it will do
585 * for normal use. strace only continues with a signal if the
586 * stopping signal is not SIGTRAP. -brl
588 if (current->exit_code) {
589 send_sig(current->exit_code, current, 1);
590 current->exit_code = 0;