1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
6 * x86-64 port 2000-2002 Andi Kleen
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <linux/smp.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>
29 #include <asm/proto.h>
33 * does not yet catch signals sent when the child dies.
34 * in exit.c or in signal.c.
38 * Determines which flags the user has access to [1 = access, 0 = no access].
39 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
40 * Also masks reserved bits (63-22, 15, 5, 3, 1).
42 #define FLAG_MASK 0x54dd5UL
44 /* set's the trap flag. */
45 #define TRAP_FLAG 0x100UL
48 * eflags and offset of eflags on child stack..
50 #define EFLAGS offsetof(struct pt_regs, eflags)
51 #define EFL_OFFSET ((int)(EFLAGS-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 unsigned long get_stack_long(struct task_struct *task, int offset)
63 stack = (unsigned char *)task->thread.rsp0;
65 return (*((unsigned long *)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 long put_stack_long(struct task_struct *task, int offset,
77 unsigned char * stack;
79 stack = (unsigned char *) task->thread.rsp0;
81 *(unsigned long *) stack = data;
87 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
89 unsigned long addr, seg;
92 seg = regs->cs & 0xffff;
95 * We'll assume that the code segments in the GDT
96 * are all zero-based. That is largely true: the
97 * TLS segments are used for data, and the PNPBIOS
98 * and APM bios ones we just ignore here.
100 if (seg & LDT_SEGMENT) {
106 mutex_lock(&child->mm->context.lock);
107 if (unlikely((seg >> 3) >= child->mm->context.size))
108 addr = -1L; /* bogus selector, access would fault */
110 desc = child->mm->context.ldt + seg;
111 base = ((desc[0] >> 16) |
112 ((desc[1] & 0xff) << 16) |
113 (desc[1] & 0xff000000));
115 /* 16-bit code segment? */
116 if (!((desc[1] >> 22) & 1))
120 mutex_unlock(&child->mm->context.lock);
126 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
129 unsigned char opcode[15];
130 unsigned long addr = convert_rip_to_linear(child, regs);
132 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
133 for (i = 0; i < copied; i++) {
136 case 0x9d: case 0xcf:
141 /* opcode and address size prefixes */
142 case 0x66: case 0x67:
144 /* irrelevant prefixes (segment overrides and repeats) */
145 case 0x26: case 0x2e:
146 case 0x36: case 0x3e:
147 case 0x64: case 0x65:
148 case 0xf2: case 0xf3:
152 if (regs->cs != __USER_CS)
153 /* 32-bit mode: register increment */
155 /* 64-bit mode: REX prefix */
158 /* CHECKME: f2, f3 */
161 * pushf: NOTE! We should probably not let
162 * the user see the TF bit being set. But
163 * it's more pain than it's worth to avoid
164 * it, and a debugger could emulate this
165 * all in user space if it _really_ cares.
175 static void set_singlestep(struct task_struct *child)
177 struct pt_regs *regs = task_pt_regs(child);
180 * Always set TIF_SINGLESTEP - this guarantees that
181 * we single-step system calls etc.. This will also
182 * cause us to set TF when returning to user mode.
184 set_tsk_thread_flag(child, TIF_SINGLESTEP);
187 * If TF was already set, don't do anything else
189 if (regs->eflags & TRAP_FLAG)
192 /* Set TF on the kernel stack.. */
193 regs->eflags |= TRAP_FLAG;
196 * ..but if TF is changed by the instruction we will trace,
197 * don't mark it as being "us" that set it, so that we
198 * won't clear it by hand later.
200 if (is_setting_trap_flag(child, regs))
203 child->ptrace |= PT_DTRACE;
206 static void clear_singlestep(struct task_struct *child)
208 /* Always clear TIF_SINGLESTEP... */
209 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
211 /* But touch TF only if it was set by us.. */
212 if (child->ptrace & PT_DTRACE) {
213 struct pt_regs *regs = task_pt_regs(child);
214 regs->eflags &= ~TRAP_FLAG;
215 child->ptrace &= ~PT_DTRACE;
220 * Called by kernel/ptrace.c when detaching..
222 * Make sure the single step bit is not set.
224 void ptrace_disable(struct task_struct *child)
226 clear_singlestep(child);
229 static int putreg(struct task_struct *child,
230 unsigned long regno, unsigned long value)
235 case offsetof(struct user_regs_struct,fs):
236 if (value && (value & 3) != 3)
238 child->thread.fsindex = value & 0xffff;
240 case offsetof(struct user_regs_struct,gs):
241 if (value && (value & 3) != 3)
243 child->thread.gsindex = value & 0xffff;
245 case offsetof(struct user_regs_struct,ds):
246 if (value && (value & 3) != 3)
248 child->thread.ds = value & 0xffff;
250 case offsetof(struct user_regs_struct,es):
251 if (value && (value & 3) != 3)
253 child->thread.es = value & 0xffff;
255 case offsetof(struct user_regs_struct,ss):
256 if ((value & 3) != 3)
260 case offsetof(struct user_regs_struct,fs_base):
261 if (value >= TASK_SIZE_OF(child))
263 child->thread.fs = value;
265 case offsetof(struct user_regs_struct,gs_base):
266 if (value >= TASK_SIZE_OF(child))
268 child->thread.gs = value;
270 case offsetof(struct user_regs_struct, eflags):
272 tmp = get_stack_long(child, EFL_OFFSET);
276 case offsetof(struct user_regs_struct,cs):
277 if ((value & 3) != 3)
282 put_stack_long(child, regno - sizeof(struct pt_regs), value);
286 static unsigned long getreg(struct task_struct *child, unsigned long regno)
290 case offsetof(struct user_regs_struct, fs):
291 return child->thread.fsindex;
292 case offsetof(struct user_regs_struct, gs):
293 return child->thread.gsindex;
294 case offsetof(struct user_regs_struct, ds):
295 return child->thread.ds;
296 case offsetof(struct user_regs_struct, es):
297 return child->thread.es;
298 case offsetof(struct user_regs_struct, fs_base):
299 return child->thread.fs;
300 case offsetof(struct user_regs_struct, gs_base):
301 return child->thread.gs;
303 regno = regno - sizeof(struct pt_regs);
304 val = get_stack_long(child, regno);
305 if (test_tsk_thread_flag(child, TIF_IA32))
312 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
318 /* when I and D space are separate, these will need to be fixed. */
319 case PTRACE_PEEKTEXT: /* read word at location addr. */
320 case PTRACE_PEEKDATA:
321 ret = generic_ptrace_peekdata(child, addr, data);
324 /* read the word at location addr in the USER area. */
325 case PTRACE_PEEKUSR: {
330 addr > sizeof(struct user) - 7)
334 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
335 tmp = getreg(child, addr);
337 case offsetof(struct user, u_debugreg[0]):
338 tmp = child->thread.debugreg0;
340 case offsetof(struct user, u_debugreg[1]):
341 tmp = child->thread.debugreg1;
343 case offsetof(struct user, u_debugreg[2]):
344 tmp = child->thread.debugreg2;
346 case offsetof(struct user, u_debugreg[3]):
347 tmp = child->thread.debugreg3;
349 case offsetof(struct user, u_debugreg[6]):
350 tmp = child->thread.debugreg6;
352 case offsetof(struct user, u_debugreg[7]):
353 tmp = child->thread.debugreg7;
359 ret = put_user(tmp,(unsigned long __user *) data);
363 /* when I and D space are separate, this will have to be fixed. */
364 case PTRACE_POKETEXT: /* write the word at location addr. */
365 case PTRACE_POKEDATA:
366 ret = generic_ptrace_pokedata(child, addr, data);
369 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
371 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
374 addr > sizeof(struct user) - 7)
378 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
379 ret = putreg(child, addr, data);
381 /* Disallows to set a breakpoint into the vsyscall */
382 case offsetof(struct user, u_debugreg[0]):
383 if (data >= TASK_SIZE_OF(child) - dsize) break;
384 child->thread.debugreg0 = data;
387 case offsetof(struct user, u_debugreg[1]):
388 if (data >= TASK_SIZE_OF(child) - dsize) break;
389 child->thread.debugreg1 = data;
392 case offsetof(struct user, u_debugreg[2]):
393 if (data >= TASK_SIZE_OF(child) - dsize) break;
394 child->thread.debugreg2 = data;
397 case offsetof(struct user, u_debugreg[3]):
398 if (data >= TASK_SIZE_OF(child) - dsize) break;
399 child->thread.debugreg3 = data;
402 case offsetof(struct user, u_debugreg[6]):
405 child->thread.debugreg6 = data;
408 case offsetof(struct user, u_debugreg[7]):
409 /* See arch/i386/kernel/ptrace.c for an explanation of
410 * this awkward check.*/
411 data &= ~DR_CONTROL_RESERVED;
413 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
416 child->thread.debugreg7 = data;
418 set_tsk_thread_flag(child, TIF_DEBUG);
420 clear_tsk_thread_flag(child, TIF_DEBUG);
427 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
428 case PTRACE_CONT: /* restart after signal. */
431 if (!valid_signal(data))
433 if (request == PTRACE_SYSCALL)
434 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
436 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
437 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
438 child->exit_code = data;
439 /* make sure the single step bit is not set. */
440 clear_singlestep(child);
441 wake_up_process(child);
445 #ifdef CONFIG_IA32_EMULATION
446 /* This makes only sense with 32bit programs. Allow a
447 64bit debugger to fully examine them too. Better
448 don't use it against 64bit processes, use
449 PTRACE_ARCH_PRCTL instead. */
450 case PTRACE_SET_THREAD_AREA: {
451 struct user_desc __user *p;
453 p = (struct user_desc __user *)data;
454 get_user(old, &p->entry_number);
455 put_user(addr, &p->entry_number);
456 ret = do_set_thread_area(&child->thread, p);
457 put_user(old, &p->entry_number);
459 case PTRACE_GET_THREAD_AREA:
460 p = (struct user_desc __user *)data;
461 get_user(old, &p->entry_number);
462 put_user(addr, &p->entry_number);
463 ret = do_get_thread_area(&child->thread, p);
464 put_user(old, &p->entry_number);
468 /* normal 64bit interface to access TLS data.
469 Works just like arch_prctl, except that the arguments
471 case PTRACE_ARCH_PRCTL:
472 ret = do_arch_prctl(child, data, addr);
476 * make the child exit. Best I can do is send it a sigkill.
477 * perhaps it should be put in the status that it wants to
482 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
484 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
485 child->exit_code = SIGKILL;
486 /* make sure the single step bit is not set. */
487 clear_singlestep(child);
488 wake_up_process(child);
491 case PTRACE_SINGLESTEP: /* set the trap flag. */
493 if (!valid_signal(data))
495 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
496 set_singlestep(child);
497 child->exit_code = data;
498 /* give it a chance to run. */
499 wake_up_process(child);
503 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
504 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
505 sizeof(struct user_regs_struct))) {
510 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
511 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
512 data += sizeof(long);
517 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
519 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
520 sizeof(struct user_regs_struct))) {
525 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
526 ret = __get_user(tmp, (unsigned long __user *) data);
529 ret = putreg(child, ui, tmp);
532 data += sizeof(long);
537 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
538 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
539 sizeof(struct user_i387_struct))) {
543 ret = get_fpregs((struct user_i387_struct __user *)data, child);
547 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
548 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
549 sizeof(struct user_i387_struct))) {
553 set_stopped_child_used_math(child);
554 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
559 ret = ptrace_request(child, request, addr, data);
565 static void syscall_trace(struct pt_regs *regs)
569 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
571 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
572 current_thread_info()->flags, current->ptrace);
575 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
578 * this isn't the same as continuing with a signal, but it will do
579 * for normal use. strace only continues with a signal if the
580 * stopping signal is not SIGTRAP. -brl
582 if (current->exit_code) {
583 send_sig(current->exit_code, current, 1);
584 current->exit_code = 0;
588 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
590 /* do the secure computing check first */
591 secure_computing(regs->orig_rax);
593 if (test_thread_flag(TIF_SYSCALL_TRACE)
594 && (current->ptrace & PT_PTRACED))
597 if (unlikely(current->audit_context)) {
598 if (test_thread_flag(TIF_IA32)) {
599 audit_syscall_entry(AUDIT_ARCH_I386,
601 regs->rbx, regs->rcx,
602 regs->rdx, regs->rsi);
604 audit_syscall_entry(AUDIT_ARCH_X86_64,
606 regs->rdi, regs->rsi,
607 regs->rdx, regs->r10);
612 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
614 if (unlikely(current->audit_context))
615 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
617 if ((test_thread_flag(TIF_SYSCALL_TRACE)
618 || test_thread_flag(TIF_SINGLESTEP))
619 && (current->ptrace & PT_PTRACED))