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.
36 * Determines which flags the user has access to [1 = access, 0 = no access].
37 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
38 * Also masks reserved bits (31-22, 15, 5, 3, 1).
40 #define FLAG_MASK 0x00050dd5
42 /* set's the trap flag. */
43 #define TRAP_FLAG 0x100
46 * Offset of eflags on child stack..
48 #define EFL_OFFSET ((EFL-2)*4-sizeof(struct pt_regs))
50 static inline struct pt_regs *get_child_regs(struct task_struct *task)
52 void *stack_top = (void *)task->thread.esp0;
53 return stack_top - sizeof(struct pt_regs);
57 * this routine will get a word off of the processes privileged stack.
58 * the offset is how far from the base addr as stored in the TSS.
59 * this routine assumes that all the privileged stacks are in our
62 static inline int get_stack_long(struct task_struct *task, int offset)
66 stack = (unsigned char *)task->thread.esp0;
68 return (*((int *)stack));
72 * this routine will put a word on the processes privileged stack.
73 * the offset is how far from the base addr as stored in the TSS.
74 * this routine assumes that all the privileged stacks are in our
77 static inline int put_stack_long(struct task_struct *task, int offset,
80 unsigned char * stack;
82 stack = (unsigned char *) task->thread.esp0;
84 *(unsigned long *) stack = data;
88 static int putreg(struct task_struct *child,
89 unsigned long regno, unsigned long value)
93 if (value && (value & 3) != 3)
95 child->thread.fs = value;
100 if (value && (value & 3) != 3)
106 if ((value & 3) != 3)
112 value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
117 put_stack_long(child, regno - sizeof(struct pt_regs), value);
121 static unsigned long getreg(struct task_struct *child,
124 unsigned long retval = ~0UL;
126 switch (regno >> 2) {
128 retval = child->thread.fs;
140 regno = regno - sizeof(struct pt_regs);
141 retval &= get_stack_long(child, regno);
146 #define LDT_SEGMENT 4
148 static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs)
150 unsigned long addr, seg;
153 seg = regs->xcs & 0xffff;
154 if (regs->eflags & VM_MASK) {
155 addr = (addr & 0xffff) + (seg << 4);
160 * We'll assume that the code segments in the GDT
161 * are all zero-based. That is largely true: the
162 * TLS segments are used for data, and the PNPBIOS
163 * and APM bios ones we just ignore here.
165 if (seg & LDT_SEGMENT) {
169 down(&child->mm->context.sem);
170 desc = child->mm->context.ldt + (seg & ~7);
171 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
173 /* 16-bit code segment? */
174 if (!((desc[1] >> 22) & 1))
177 up(&child->mm->context.sem);
182 static inline int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
185 unsigned char opcode[15];
186 unsigned long addr = convert_eip_to_linear(child, regs);
188 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
189 for (i = 0; i < copied; i++) {
192 case 0x9d: case 0xcf:
194 /* opcode and address size prefixes */
195 case 0x66: case 0x67:
197 /* irrelevant prefixes (segment overrides and repeats) */
198 case 0x26: case 0x2e:
199 case 0x36: case 0x3e:
200 case 0x64: case 0x65:
201 case 0xf0: case 0xf2: case 0xf3:
205 * pushf: NOTE! We should probably not let
206 * the user see the TF bit being set. But
207 * it's more pain than it's worth to avoid
208 * it, and a debugger could emulate this
209 * all in user space if it _really_ cares.
219 static void set_singlestep(struct task_struct *child)
221 struct pt_regs *regs = get_child_regs(child);
224 * Always set TIF_SINGLESTEP - this guarantees that
225 * we single-step system calls etc.. This will also
226 * cause us to set TF when returning to user mode.
228 set_tsk_thread_flag(child, TIF_SINGLESTEP);
231 * If TF was already set, don't do anything else
233 if (regs->eflags & TRAP_FLAG)
236 /* Set TF on the kernel stack.. */
237 regs->eflags |= TRAP_FLAG;
240 * ..but if TF is changed by the instruction we will trace,
241 * don't mark it as being "us" that set it, so that we
242 * won't clear it by hand later.
244 if (is_setting_trap_flag(child, regs))
247 child->ptrace |= PT_DTRACE;
250 static void clear_singlestep(struct task_struct *child)
252 /* Always clear TIF_SINGLESTEP... */
253 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
255 /* But touch TF only if it was set by us.. */
256 if (child->ptrace & PT_DTRACE) {
257 struct pt_regs *regs = get_child_regs(child);
258 regs->eflags &= ~TRAP_FLAG;
259 child->ptrace &= ~PT_DTRACE;
264 * Called by kernel/ptrace.c when detaching..
266 * Make sure the single step bit is not set.
268 void ptrace_disable(struct task_struct *child)
270 clear_singlestep(child);
271 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
272 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
276 * Perform get_thread_area on behalf of the traced child.
279 ptrace_get_thread_area(struct task_struct *child,
280 int idx, struct user_desc __user *user_desc)
282 struct user_desc info;
283 struct desc_struct *desc;
286 * Get the current Thread-Local Storage area:
289 #define GET_BASE(desc) ( \
290 (((desc)->a >> 16) & 0x0000ffff) | \
291 (((desc)->b << 16) & 0x00ff0000) | \
292 ( (desc)->b & 0xff000000) )
294 #define GET_LIMIT(desc) ( \
295 ((desc)->a & 0x0ffff) | \
296 ((desc)->b & 0xf0000) )
298 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
299 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
300 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
301 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
302 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
303 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
305 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
308 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
310 info.entry_number = idx;
311 info.base_addr = GET_BASE(desc);
312 info.limit = GET_LIMIT(desc);
313 info.seg_32bit = GET_32BIT(desc);
314 info.contents = GET_CONTENTS(desc);
315 info.read_exec_only = !GET_WRITABLE(desc);
316 info.limit_in_pages = GET_LIMIT_PAGES(desc);
317 info.seg_not_present = !GET_PRESENT(desc);
318 info.useable = GET_USEABLE(desc);
320 if (copy_to_user(user_desc, &info, sizeof(info)))
327 * Perform set_thread_area on behalf of the traced child.
330 ptrace_set_thread_area(struct task_struct *child,
331 int idx, struct user_desc __user *user_desc)
333 struct user_desc info;
334 struct desc_struct *desc;
336 if (copy_from_user(&info, user_desc, sizeof(info)))
339 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
342 desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
343 if (LDT_empty(&info)) {
347 desc->a = LDT_entry_a(&info);
348 desc->b = LDT_entry_b(&info);
354 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
356 struct user * dummy = NULL;
358 unsigned long __user *datap = (unsigned long __user *)data;
361 /* when I and D space are separate, these will need to be fixed. */
362 case PTRACE_PEEKTEXT: /* read word at location addr. */
363 case PTRACE_PEEKDATA: {
367 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
369 if (copied != sizeof(tmp))
371 ret = put_user(tmp, datap);
375 /* read the word at location addr in the USER area. */
376 case PTRACE_PEEKUSR: {
380 if ((addr & 3) || addr < 0 ||
381 addr > sizeof(struct user) - 3)
384 tmp = 0; /* Default return condition */
385 if(addr < FRAME_SIZE*sizeof(long))
386 tmp = getreg(child, addr);
387 if(addr >= (long) &dummy->u_debugreg[0] &&
388 addr <= (long) &dummy->u_debugreg[7]){
389 addr -= (long) &dummy->u_debugreg[0];
391 tmp = child->thread.debugreg[addr];
393 ret = put_user(tmp, datap);
397 /* when I and D space are separate, this will have to be fixed. */
398 case PTRACE_POKETEXT: /* write the word at location addr. */
399 case PTRACE_POKEDATA:
401 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
406 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
408 if ((addr & 3) || addr < 0 ||
409 addr > sizeof(struct user) - 3)
412 if (addr < FRAME_SIZE*sizeof(long)) {
413 ret = putreg(child, addr, data);
416 /* We need to be very careful here. We implicitly
417 want to modify a portion of the task_struct, and we
418 have to be selective about what portions we allow someone
422 if(addr >= (long) &dummy->u_debugreg[0] &&
423 addr <= (long) &dummy->u_debugreg[7]){
425 if(addr == (long) &dummy->u_debugreg[4]) break;
426 if(addr == (long) &dummy->u_debugreg[5]) break;
427 if(addr < (long) &dummy->u_debugreg[4] &&
428 ((unsigned long) data) >= TASK_SIZE-3) break;
430 /* Sanity-check data. Take one half-byte at once with
431 * check = (val >> (16 + 4*i)) & 0xf. It contains the
432 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
433 * 2 and 3 are LENi. Given a list of invalid values,
434 * we do mask |= 1 << invalid_value, so that
435 * (mask >> check) & 1 is a correct test for invalid
438 * R/Wi contains the type of the breakpoint /
439 * watchpoint, LENi contains the length of the watched
440 * data in the watchpoint case.
442 * The invalid values are:
443 * - LENi == 0x10 (undefined), so mask |= 0x0f00.
444 * - R/Wi == 0x10 (break on I/O reads or writes), so
446 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
449 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
451 * See the Intel Manual "System Programming Guide",
454 * Note that LENi == 0x10 is defined on x86_64 in long
455 * mode (i.e. even for 32-bit userspace software, but
456 * 64-bit kernel), so the x86_64 mask value is 0x5454.
457 * See the AMD manual no. 24593 (AMD64 System
460 if(addr == (long) &dummy->u_debugreg[7]) {
461 data &= ~DR_CONTROL_RESERVED;
463 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
466 set_tsk_thread_flag(child, TIF_DEBUG);
468 clear_tsk_thread_flag(child, TIF_DEBUG);
470 addr -= (long) &dummy->u_debugreg;
472 child->thread.debugreg[addr] = data;
477 case PTRACE_SYSEMU: /* continue and stop at next syscall, which will not be executed */
478 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
479 case PTRACE_CONT: /* restart after signal. */
481 if (!valid_signal(data))
483 if (request == PTRACE_SYSEMU) {
484 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
485 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
486 } else if (request == PTRACE_SYSCALL) {
487 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
488 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
490 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
491 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
493 child->exit_code = data;
494 /* make sure the single step bit is not set. */
495 clear_singlestep(child);
496 wake_up_process(child);
501 * make the child exit. Best I can do is send it a sigkill.
502 * perhaps it should be put in the status that it wants to
507 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
509 child->exit_code = SIGKILL;
510 /* make sure the single step bit is not set. */
511 clear_singlestep(child);
512 wake_up_process(child);
515 case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */
516 case PTRACE_SINGLESTEP: /* set the trap flag. */
518 if (!valid_signal(data))
521 if (request == PTRACE_SYSEMU_SINGLESTEP)
522 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
524 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
526 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
527 set_singlestep(child);
528 child->exit_code = data;
529 /* give it a chance to run. */
530 wake_up_process(child);
535 /* detach a process that was attached. */
536 ret = ptrace_detach(child, data);
539 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
540 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
544 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
545 __put_user(getreg(child, i), datap);
552 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
554 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
558 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
559 __get_user(tmp, datap);
560 putreg(child, i, tmp);
567 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
568 if (!access_ok(VERIFY_WRITE, datap,
569 sizeof(struct user_i387_struct))) {
574 if (!tsk_used_math(child))
576 get_fpregs((struct user_i387_struct __user *)data, child);
580 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
581 if (!access_ok(VERIFY_READ, datap,
582 sizeof(struct user_i387_struct))) {
586 set_stopped_child_used_math(child);
587 set_fpregs(child, (struct user_i387_struct __user *)data);
592 case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
593 if (!access_ok(VERIFY_WRITE, datap,
594 sizeof(struct user_fxsr_struct))) {
598 if (!tsk_used_math(child))
600 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
604 case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
605 if (!access_ok(VERIFY_READ, datap,
606 sizeof(struct user_fxsr_struct))) {
610 set_stopped_child_used_math(child);
611 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
615 case PTRACE_GET_THREAD_AREA:
616 ret = ptrace_get_thread_area(child, addr,
617 (struct user_desc __user *) data);
620 case PTRACE_SET_THREAD_AREA:
621 ret = ptrace_set_thread_area(child, addr,
622 (struct user_desc __user *) data);
626 ret = ptrace_request(child, request, addr, data);
633 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
637 tsk->thread.trap_no = 1;
638 tsk->thread.error_code = error_code;
640 memset(&info, 0, sizeof(info));
641 info.si_signo = SIGTRAP;
642 info.si_code = TRAP_BRKPT;
645 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL;
647 /* Send us the fakey SIGTRAP */
648 force_sig_info(SIGTRAP, &info, tsk);
651 /* notification of system call entry/exit
652 * - triggered by current->work.syscall_trace
654 __attribute__((regparm(3)))
655 int do_syscall_trace(struct pt_regs *regs, int entryexit)
657 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
659 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
662 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
665 /* do the secure computing check first */
667 secure_computing(regs->orig_eax);
669 if (unlikely(current->audit_context)) {
671 audit_syscall_exit(AUDITSC_RESULT(regs->eax),
673 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
674 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
675 * not used, entry.S will call us only on syscall exit, not
676 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
677 * calling send_sigtrap() on syscall entry.
679 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
680 * is_singlestep is false, despite his name, so we will still do
683 else if (is_singlestep)
687 if (!(current->ptrace & PT_PTRACED))
690 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
691 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
692 * here. We have to check this and return */
693 if (is_sysemu && entryexit)
696 /* Fake a debug trap */
698 send_sigtrap(current, regs, 0);
700 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
703 /* the 0x80 provides a way for the tracing parent to distinguish
704 between a syscall stop and SIGTRAP delivery */
705 /* Note that the debugger could change the result of test_thread_flag!*/
706 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
709 * this isn't the same as continuing with a signal, but it will do
710 * for normal use. strace only continues with a signal if the
711 * stopping signal is not SIGTRAP. -brl
713 if (current->exit_code) {
714 send_sig(current->exit_code, current, 1);
715 current->exit_code = 0;
719 if (unlikely(current->audit_context) && !entryexit)
720 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_eax,
721 regs->ebx, regs->ecx, regs->edx, regs->esi);
725 regs->orig_eax = -1; /* force skip of syscall restarting */
726 if (unlikely(current->audit_context))
727 audit_syscall_exit(AUDITSC_RESULT(regs->eax), regs->eax);