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>
18 #include <linux/seccomp.h>
19 #include <linux/signal.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/processor.h>
26 #include <asm/debugreg.h>
31 * does not yet catch signals sent when the child dies.
32 * in exit.c or in signal.c.
35 /* determines which flags the user has access to. */
36 /* 1 = access 0 = no access */
37 #define FLAG_MASK 0x00044dd5
39 /* set's the trap flag. */
40 #define TRAP_FLAG 0x100
43 * Offset of eflags on child stack..
45 #define EFL_OFFSET ((EFL-2)*4-sizeof(struct pt_regs))
47 static inline struct pt_regs *get_child_regs(struct task_struct *task)
49 void *stack_top = (void *)task->thread.esp0;
50 return stack_top - sizeof(struct pt_regs);
54 * this routine will get a word off of the processes privileged stack.
55 * the offset is how far from the base addr as stored in the TSS.
56 * this routine assumes that all the privileged stacks are in our
59 static inline int get_stack_long(struct task_struct *task, int offset)
63 stack = (unsigned char *)task->thread.esp0;
65 return (*((int *)stack));
69 * this routine will put a word on the processes privileged stack.
70 * the offset is how far from the base addr as stored in the TSS.
71 * this routine assumes that all the privileged stacks are in our
74 static inline int put_stack_long(struct task_struct *task, int offset,
77 unsigned char * stack;
79 stack = (unsigned char *) task->thread.esp0;
81 *(unsigned long *) stack = data;
85 static int putreg(struct task_struct *child,
86 unsigned long regno, unsigned long value)
90 if (value && (value & 3) != 3)
92 child->thread.fs = value;
95 if (value && (value & 3) != 3)
97 child->thread.gs = value;
101 if (value && (value & 3) != 3)
107 if ((value & 3) != 3)
113 value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
118 put_stack_long(child, regno - sizeof(struct pt_regs), value);
122 static unsigned long getreg(struct task_struct *child,
125 unsigned long retval = ~0UL;
127 switch (regno >> 2) {
129 retval = child->thread.fs;
132 retval = child->thread.gs;
143 regno = regno - sizeof(struct pt_regs);
144 retval &= get_stack_long(child, regno);
149 #define LDT_SEGMENT 4
151 static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs)
153 unsigned long addr, seg;
156 seg = regs->xcs & 0xffff;
157 if (regs->eflags & VM_MASK) {
158 addr = (addr & 0xffff) + (seg << 4);
163 * We'll assume that the code segments in the GDT
164 * are all zero-based. That is largely true: the
165 * TLS segments are used for data, and the PNPBIOS
166 * and APM bios ones we just ignore here.
168 if (seg & LDT_SEGMENT) {
172 down(&child->mm->context.sem);
173 desc = child->mm->context.ldt + (seg & ~7);
174 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
176 /* 16-bit code segment? */
177 if (!((desc[1] >> 22) & 1))
180 up(&child->mm->context.sem);
185 static inline int is_at_popf(struct task_struct *child, struct pt_regs *regs)
188 unsigned char opcode[16];
189 unsigned long addr = convert_eip_to_linear(child, regs);
191 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
192 for (i = 0; i < copied; i++) {
197 /* opcode and address size prefixes */
198 case 0x66: case 0x67:
200 /* irrelevant prefixes (segment overrides and repeats) */
201 case 0x26: case 0x2e:
202 case 0x36: case 0x3e:
203 case 0x64: case 0x65:
204 case 0xf0: case 0xf2: case 0xf3:
208 * pushf: NOTE! We should probably not let
209 * the user see the TF bit being set. But
210 * it's more pain than it's worth to avoid
211 * it, and a debugger could emulate this
212 * all in user space if it _really_ cares.
222 static void set_singlestep(struct task_struct *child)
224 struct pt_regs *regs = get_child_regs(child);
227 * Always set TIF_SINGLESTEP - this guarantees that
228 * we single-step system calls etc.. This will also
229 * cause us to set TF when returning to user mode.
231 set_tsk_thread_flag(child, TIF_SINGLESTEP);
234 * If TF was already set, don't do anything else
236 if (regs->eflags & TRAP_FLAG)
239 /* Set TF on the kernel stack.. */
240 regs->eflags |= TRAP_FLAG;
243 * ..but if TF is changed by the instruction we will trace,
244 * don't mark it as being "us" that set it, so that we
245 * won't clear it by hand later.
247 if (is_at_popf(child, regs))
250 child->ptrace |= PT_DTRACE;
253 static void clear_singlestep(struct task_struct *child)
255 /* Always clear TIF_SINGLESTEP... */
256 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
258 /* But touch TF only if it was set by us.. */
259 if (child->ptrace & PT_DTRACE) {
260 struct pt_regs *regs = get_child_regs(child);
261 regs->eflags &= ~TRAP_FLAG;
262 child->ptrace &= ~PT_DTRACE;
267 * Called by kernel/ptrace.c when detaching..
269 * Make sure the single step bit is not set.
271 void ptrace_disable(struct task_struct *child)
273 clear_singlestep(child);
277 * Perform get_thread_area on behalf of the traced child.
280 ptrace_get_thread_area(struct task_struct *child,
281 int idx, struct user_desc __user *user_desc)
283 struct user_desc info;
284 struct desc_struct *desc;
287 * Get the current Thread-Local Storage area:
290 #define GET_BASE(desc) ( \
291 (((desc)->a >> 16) & 0x0000ffff) | \
292 (((desc)->b << 16) & 0x00ff0000) | \
293 ( (desc)->b & 0xff000000) )
295 #define GET_LIMIT(desc) ( \
296 ((desc)->a & 0x0ffff) | \
297 ((desc)->b & 0xf0000) )
299 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
300 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
301 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
302 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
303 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
304 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
306 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
309 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
311 info.entry_number = idx;
312 info.base_addr = GET_BASE(desc);
313 info.limit = GET_LIMIT(desc);
314 info.seg_32bit = GET_32BIT(desc);
315 info.contents = GET_CONTENTS(desc);
316 info.read_exec_only = !GET_WRITABLE(desc);
317 info.limit_in_pages = GET_LIMIT_PAGES(desc);
318 info.seg_not_present = !GET_PRESENT(desc);
319 info.useable = GET_USEABLE(desc);
321 if (copy_to_user(user_desc, &info, sizeof(info)))
328 * Perform set_thread_area on behalf of the traced child.
331 ptrace_set_thread_area(struct task_struct *child,
332 int idx, struct user_desc __user *user_desc)
334 struct user_desc info;
335 struct desc_struct *desc;
337 if (copy_from_user(&info, user_desc, sizeof(info)))
340 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
343 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
344 if (LDT_empty(&info)) {
348 desc->a = LDT_entry_a(&info);
349 desc->b = LDT_entry_b(&info);
355 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
357 struct task_struct *child;
358 struct user * dummy = NULL;
360 unsigned long __user *datap = (unsigned long __user *)data;
364 if (request == PTRACE_TRACEME) {
365 /* are we already being traced? */
366 if (current->ptrace & PT_PTRACED)
368 ret = security_ptrace(current->parent, current);
371 /* set the ptrace bit in the process flags. */
372 current->ptrace |= PT_PTRACED;
377 read_lock(&tasklist_lock);
378 child = find_task_by_pid(pid);
380 get_task_struct(child);
381 read_unlock(&tasklist_lock);
386 if (pid == 1) /* you may not mess with init */
389 if (request == PTRACE_ATTACH) {
390 ret = ptrace_attach(child);
394 ret = ptrace_check_attach(child, request == PTRACE_KILL);
399 /* when I and D space are separate, these will need to be fixed. */
400 case PTRACE_PEEKTEXT: /* read word at location addr. */
401 case PTRACE_PEEKDATA: {
405 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
407 if (copied != sizeof(tmp))
409 ret = put_user(tmp, datap);
413 /* read the word at location addr in the USER area. */
414 case PTRACE_PEEKUSR: {
418 if ((addr & 3) || addr < 0 ||
419 addr > sizeof(struct user) - 3)
422 tmp = 0; /* Default return condition */
423 if(addr < FRAME_SIZE*sizeof(long))
424 tmp = getreg(child, addr);
425 if(addr >= (long) &dummy->u_debugreg[0] &&
426 addr <= (long) &dummy->u_debugreg[7]){
427 addr -= (long) &dummy->u_debugreg[0];
429 tmp = child->thread.debugreg[addr];
431 ret = put_user(tmp, datap);
435 /* when I and D space are separate, this will have to be fixed. */
436 case PTRACE_POKETEXT: /* write the word at location addr. */
437 case PTRACE_POKEDATA:
439 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
444 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
446 if ((addr & 3) || addr < 0 ||
447 addr > sizeof(struct user) - 3)
450 if (addr < FRAME_SIZE*sizeof(long)) {
451 ret = putreg(child, addr, data);
454 /* We need to be very careful here. We implicitly
455 want to modify a portion of the task_struct, and we
456 have to be selective about what portions we allow someone
460 if(addr >= (long) &dummy->u_debugreg[0] &&
461 addr <= (long) &dummy->u_debugreg[7]){
463 if(addr == (long) &dummy->u_debugreg[4]) break;
464 if(addr == (long) &dummy->u_debugreg[5]) break;
465 if(addr < (long) &dummy->u_debugreg[4] &&
466 ((unsigned long) data) >= TASK_SIZE-3) break;
468 /* Sanity-check data. Take one half-byte at once with
469 * check = (val >> (16 + 4*i)) & 0xf. It contains the
470 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
471 * 2 and 3 are LENi. Given a list of invalid values,
472 * we do mask |= 1 << invalid_value, so that
473 * (mask >> check) & 1 is a correct test for invalid
476 * R/Wi contains the type of the breakpoint /
477 * watchpoint, LENi contains the length of the watched
478 * data in the watchpoint case.
480 * The invalid values are:
481 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
482 * - R/Wi == 0x10 (break on I/O reads or writes), so
484 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
487 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
489 * See the Intel Manual "System Programming Guide",
492 * Note that LENi == 0x10 is defined on x86_64 in long
493 * mode (i.e. even for 32-bit userspace software, but
494 * 64-bit kernel), so the x86_64 mask value is 0x5454.
495 * See the AMD manual no. 24593 (AMD64 System
498 if(addr == (long) &dummy->u_debugreg[7]) {
499 data &= ~DR_CONTROL_RESERVED;
501 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
505 addr -= (long) &dummy->u_debugreg;
507 child->thread.debugreg[addr] = data;
512 case PTRACE_SYSEMU: /* continue and stop at next syscall, which will not be executed */
513 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
514 case PTRACE_CONT: /* restart after signal. */
516 if (!valid_signal(data))
518 if (request == PTRACE_SYSEMU) {
519 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
520 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
521 } else if (request == PTRACE_SYSCALL) {
522 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
523 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
525 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
526 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
528 child->exit_code = data;
529 /* make sure the single step bit is not set. */
530 clear_singlestep(child);
531 wake_up_process(child);
536 * make the child exit. Best I can do is send it a sigkill.
537 * perhaps it should be put in the status that it wants to
542 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
544 child->exit_code = SIGKILL;
545 /* make sure the single step bit is not set. */
546 clear_singlestep(child);
547 wake_up_process(child);
550 case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */
551 case PTRACE_SINGLESTEP: /* set the trap flag. */
553 if (!valid_signal(data))
556 if (request == PTRACE_SYSEMU_SINGLESTEP)
557 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
559 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
561 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
562 set_singlestep(child);
563 child->exit_code = data;
564 /* give it a chance to run. */
565 wake_up_process(child);
570 /* detach a process that was attached. */
571 ret = ptrace_detach(child, data);
574 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
575 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
579 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
580 __put_user(getreg(child, i), datap);
587 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
589 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
593 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
594 __get_user(tmp, datap);
595 putreg(child, i, tmp);
602 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
603 if (!access_ok(VERIFY_WRITE, datap,
604 sizeof(struct user_i387_struct))) {
609 if (!tsk_used_math(child))
611 get_fpregs((struct user_i387_struct __user *)data, child);
615 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
616 if (!access_ok(VERIFY_READ, datap,
617 sizeof(struct user_i387_struct))) {
621 set_stopped_child_used_math(child);
622 set_fpregs(child, (struct user_i387_struct __user *)data);
627 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
628 if (!access_ok(VERIFY_WRITE, datap,
629 sizeof(struct user_fxsr_struct))) {
633 if (!tsk_used_math(child))
635 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
639 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
640 if (!access_ok(VERIFY_READ, datap,
641 sizeof(struct user_fxsr_struct))) {
645 set_stopped_child_used_math(child);
646 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
650 case PTRACE_GET_THREAD_AREA:
651 ret = ptrace_get_thread_area(child, addr,
652 (struct user_desc __user *) data);
655 case PTRACE_SET_THREAD_AREA:
656 ret = ptrace_set_thread_area(child, addr,
657 (struct user_desc __user *) data);
661 ret = ptrace_request(child, request, addr, data);
665 put_task_struct(child);
671 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
675 tsk->thread.trap_no = 1;
676 tsk->thread.error_code = error_code;
678 memset(&info, 0, sizeof(info));
679 info.si_signo = SIGTRAP;
680 info.si_code = TRAP_BRKPT;
683 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL;
685 /* Send us the fakey SIGTRAP */
686 force_sig_info(SIGTRAP, &info, tsk);
689 /* notification of system call entry/exit
690 * - triggered by current->work.syscall_trace
692 __attribute__((regparm(3)))
693 int do_syscall_trace(struct pt_regs *regs, int entryexit)
695 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU), ret = 0;
696 /* With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP */
697 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
699 /* do the secure computing check first */
700 secure_computing(regs->orig_eax);
702 if (unlikely(current->audit_context) && entryexit)
703 audit_syscall_exit(current, AUDITSC_RESULT(regs->eax), regs->eax);
705 if (!(current->ptrace & PT_PTRACED))
708 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
709 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
710 * here. We have to check this and return */
711 if (is_sysemu && entryexit)
714 /* Fake a debug trap */
716 send_sigtrap(current, regs, 0);
718 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
721 /* the 0x80 provides a way for the tracing parent to distinguish
722 between a syscall stop and SIGTRAP delivery */
723 /* Note that the debugger could change the result of test_thread_flag!*/
724 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
727 * this isn't the same as continuing with a signal, but it will do
728 * for normal use. strace only continues with a signal if the
729 * stopping signal is not SIGTRAP. -brl
731 if (current->exit_code) {
732 send_sig(current->exit_code, current, 1);
733 current->exit_code = 0;
737 if (unlikely(current->audit_context) && !entryexit)
738 audit_syscall_entry(current, AUDIT_ARCH_I386, regs->orig_eax,
739 regs->ebx, regs->ecx, regs->edx, regs->esi);
743 regs->orig_eax = -1; /* force skip of syscall restarting */
744 if (unlikely(current->audit_context))
745 audit_syscall_exit(current, AUDITSC_RESULT(regs->eax), regs->eax);