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>
25 #include <asm/prctl.h>
27 #include <asm/debugreg.h>
30 #include <asm/proto.h>
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
39 * Determines which flags the user has access to [1 = access, 0 = no access].
40 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
41 * Also masks reserved bits (63-22, 15, 5, 3, 1).
43 #define FLAG_MASK 0x54dd5UL
46 * eflags and offset of eflags on child stack..
48 #define EFLAGS offsetof(struct pt_regs, eflags)
49 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
52 * this routine will get a word off of the processes privileged stack.
53 * the offset is how far from the base addr as stored in the TSS.
54 * this routine assumes that all the privileged stacks are in our
57 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
61 stack = (unsigned char *)task->thread.rsp0;
63 return (*((unsigned long *)stack));
67 * this routine will put a word on the processes privileged stack.
68 * the offset is how far from the base addr as stored in the TSS.
69 * this routine assumes that all the privileged stacks are in our
72 static inline long put_stack_long(struct task_struct *task, int offset,
75 unsigned char * stack;
77 stack = (unsigned char *) task->thread.rsp0;
79 *(unsigned long *) stack = data;
85 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
87 unsigned long addr, seg;
90 seg = regs->cs & 0xffff;
93 * We'll assume that the code segments in the GDT
94 * are all zero-based. That is largely true: the
95 * TLS segments are used for data, and the PNPBIOS
96 * and APM bios ones we just ignore here.
98 if (seg & LDT_SEGMENT) {
104 mutex_lock(&child->mm->context.lock);
105 if (unlikely((seg >> 3) >= child->mm->context.size))
106 addr = -1L; /* bogus selector, access would fault */
108 desc = child->mm->context.ldt + seg;
109 base = ((desc[0] >> 16) |
110 ((desc[1] & 0xff) << 16) |
111 (desc[1] & 0xff000000));
113 /* 16-bit code segment? */
114 if (!((desc[1] >> 22) & 1))
118 mutex_unlock(&child->mm->context.lock);
124 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
127 unsigned char opcode[15];
128 unsigned long addr = convert_rip_to_linear(child, regs);
130 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
131 for (i = 0; i < copied; i++) {
134 case 0x9d: case 0xcf:
139 /* opcode and address size prefixes */
140 case 0x66: case 0x67:
142 /* irrelevant prefixes (segment overrides and repeats) */
143 case 0x26: case 0x2e:
144 case 0x36: case 0x3e:
145 case 0x64: case 0x65:
146 case 0xf2: case 0xf3:
150 if (regs->cs != __USER_CS)
151 /* 32-bit mode: register increment */
153 /* 64-bit mode: REX prefix */
156 /* CHECKME: f2, f3 */
159 * pushf: NOTE! We should probably not let
160 * the user see the TF bit being set. But
161 * it's more pain than it's worth to avoid
162 * it, and a debugger could emulate this
163 * all in user space if it _really_ cares.
173 void user_enable_single_step(struct task_struct *child)
175 struct pt_regs *regs = task_pt_regs(child);
178 * Always set TIF_SINGLESTEP - this guarantees that
179 * we single-step system calls etc.. This will also
180 * cause us to set TF when returning to user mode.
182 set_tsk_thread_flag(child, TIF_SINGLESTEP);
185 * If TF was already set, don't do anything else
187 if (regs->eflags & X86_EFLAGS_TF)
190 /* Set TF on the kernel stack.. */
191 regs->eflags |= X86_EFLAGS_TF;
194 * ..but if TF is changed by the instruction we will trace,
195 * don't mark it as being "us" that set it, so that we
196 * won't clear it by hand later.
198 if (is_setting_trap_flag(child, regs))
201 child->ptrace |= PT_DTRACE;
204 void user_disable_single_step(struct task_struct *child)
206 /* Always clear TIF_SINGLESTEP... */
207 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
209 /* But touch TF only if it was set by us.. */
210 if (child->ptrace & PT_DTRACE) {
211 struct pt_regs *regs = task_pt_regs(child);
212 regs->eflags &= ~X86_EFLAGS_TF;
213 child->ptrace &= ~PT_DTRACE;
218 * Called by kernel/ptrace.c when detaching..
220 * Make sure the single step bit is not set.
222 void ptrace_disable(struct task_struct *child)
224 user_disable_single_step(child);
227 static int putreg(struct task_struct *child,
228 unsigned long regno, unsigned long value)
233 case offsetof(struct user_regs_struct,fs):
234 if (value && (value & 3) != 3)
236 child->thread.fsindex = value & 0xffff;
238 case offsetof(struct user_regs_struct,gs):
239 if (value && (value & 3) != 3)
241 child->thread.gsindex = value & 0xffff;
243 case offsetof(struct user_regs_struct,ds):
244 if (value && (value & 3) != 3)
246 child->thread.ds = value & 0xffff;
248 case offsetof(struct user_regs_struct,es):
249 if (value && (value & 3) != 3)
251 child->thread.es = value & 0xffff;
253 case offsetof(struct user_regs_struct,ss):
254 if ((value & 3) != 3)
258 case offsetof(struct user_regs_struct,fs_base):
259 if (value >= TASK_SIZE_OF(child))
262 * When changing the segment base, use do_arch_prctl
263 * to set either thread.fs or thread.fsindex and the
264 * corresponding GDT slot.
266 if (child->thread.fs != value)
267 return do_arch_prctl(child, ARCH_SET_FS, value);
269 case offsetof(struct user_regs_struct,gs_base):
271 * Exactly the same here as the %fs handling above.
273 if (value >= TASK_SIZE_OF(child))
275 if (child->thread.gs != value)
276 return do_arch_prctl(child, ARCH_SET_GS, value);
278 case offsetof(struct user_regs_struct, eflags):
280 tmp = get_stack_long(child, EFL_OFFSET);
284 case offsetof(struct user_regs_struct,cs):
285 if ((value & 3) != 3)
290 put_stack_long(child, regno - sizeof(struct pt_regs), value);
294 static unsigned long getreg(struct task_struct *child, unsigned long regno)
298 case offsetof(struct user_regs_struct, fs):
299 return child->thread.fsindex;
300 case offsetof(struct user_regs_struct, gs):
301 return child->thread.gsindex;
302 case offsetof(struct user_regs_struct, ds):
303 return child->thread.ds;
304 case offsetof(struct user_regs_struct, es):
305 return child->thread.es;
306 case offsetof(struct user_regs_struct, fs_base):
308 * do_arch_prctl may have used a GDT slot instead of
309 * the MSR. To userland, it appears the same either
310 * way, except the %fs segment selector might not be 0.
312 if (child->thread.fs != 0)
313 return child->thread.fs;
314 if (child->thread.fsindex != FS_TLS_SEL)
316 return get_desc_base(&child->thread.tls_array[FS_TLS]);
317 case offsetof(struct user_regs_struct, gs_base):
319 * Exactly the same here as the %fs handling above.
321 if (child->thread.gs != 0)
322 return child->thread.gs;
323 if (child->thread.gsindex != GS_TLS_SEL)
325 return get_desc_base(&child->thread.tls_array[GS_TLS]);
327 regno = regno - sizeof(struct pt_regs);
328 val = get_stack_long(child, regno);
329 if (test_tsk_thread_flag(child, TIF_IA32))
336 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
342 /* when I and D space are separate, these will need to be fixed. */
343 case PTRACE_PEEKTEXT: /* read word at location addr. */
344 case PTRACE_PEEKDATA:
345 ret = generic_ptrace_peekdata(child, addr, data);
348 /* read the word at location addr in the USER area. */
349 case PTRACE_PEEKUSR: {
354 addr > sizeof(struct user) - 7)
358 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
359 tmp = getreg(child, addr);
361 case offsetof(struct user, u_debugreg[0]):
362 tmp = child->thread.debugreg0;
364 case offsetof(struct user, u_debugreg[1]):
365 tmp = child->thread.debugreg1;
367 case offsetof(struct user, u_debugreg[2]):
368 tmp = child->thread.debugreg2;
370 case offsetof(struct user, u_debugreg[3]):
371 tmp = child->thread.debugreg3;
373 case offsetof(struct user, u_debugreg[6]):
374 tmp = child->thread.debugreg6;
376 case offsetof(struct user, u_debugreg[7]):
377 tmp = child->thread.debugreg7;
383 ret = put_user(tmp,(unsigned long __user *) data);
387 /* when I and D space are separate, this will have to be fixed. */
388 case PTRACE_POKETEXT: /* write the word at location addr. */
389 case PTRACE_POKEDATA:
390 ret = generic_ptrace_pokedata(child, addr, data);
393 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
395 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
398 addr > sizeof(struct user) - 7)
402 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
403 ret = putreg(child, addr, data);
405 /* Disallows to set a breakpoint into the vsyscall */
406 case offsetof(struct user, u_debugreg[0]):
407 if (data >= TASK_SIZE_OF(child) - dsize) break;
408 child->thread.debugreg0 = data;
411 case offsetof(struct user, u_debugreg[1]):
412 if (data >= TASK_SIZE_OF(child) - dsize) break;
413 child->thread.debugreg1 = data;
416 case offsetof(struct user, u_debugreg[2]):
417 if (data >= TASK_SIZE_OF(child) - dsize) break;
418 child->thread.debugreg2 = data;
421 case offsetof(struct user, u_debugreg[3]):
422 if (data >= TASK_SIZE_OF(child) - dsize) break;
423 child->thread.debugreg3 = data;
426 case offsetof(struct user, u_debugreg[6]):
429 child->thread.debugreg6 = data;
432 case offsetof(struct user, u_debugreg[7]):
433 /* See arch/i386/kernel/ptrace.c for an explanation of
434 * this awkward check.*/
435 data &= ~DR_CONTROL_RESERVED;
437 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
440 child->thread.debugreg7 = data;
442 set_tsk_thread_flag(child, TIF_DEBUG);
444 clear_tsk_thread_flag(child, TIF_DEBUG);
451 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
452 case PTRACE_CONT: /* restart after signal. */
455 if (!valid_signal(data))
457 if (request == PTRACE_SYSCALL)
458 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
460 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
461 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
462 child->exit_code = data;
463 /* make sure the single step bit is not set. */
464 user_disable_single_step(child);
465 wake_up_process(child);
469 #ifdef CONFIG_IA32_EMULATION
470 /* This makes only sense with 32bit programs. Allow a
471 64bit debugger to fully examine them too. Better
472 don't use it against 64bit processes, use
473 PTRACE_ARCH_PRCTL instead. */
474 case PTRACE_GET_THREAD_AREA:
477 ret = do_get_thread_area(child, addr,
478 (struct user_desc __user *) data);
481 case PTRACE_SET_THREAD_AREA:
484 ret = do_set_thread_area(child, addr,
485 (struct user_desc __user *) data, 0);
488 /* normal 64bit interface to access TLS data.
489 Works just like arch_prctl, except that the arguments
491 case PTRACE_ARCH_PRCTL:
492 ret = do_arch_prctl(child, data, addr);
496 * make the child exit. Best I can do is send it a sigkill.
497 * perhaps it should be put in the status that it wants to
502 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
504 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
505 child->exit_code = SIGKILL;
506 /* make sure the single step bit is not set. */
507 user_disable_single_step(child);
508 wake_up_process(child);
511 case PTRACE_SINGLESTEP: /* set the trap flag. */
513 if (!valid_signal(data))
515 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
516 user_enable_single_step(child);
517 child->exit_code = data;
518 /* give it a chance to run. */
519 wake_up_process(child);
523 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
524 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
525 sizeof(struct user_regs_struct))) {
530 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
531 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
532 data += sizeof(long);
537 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
539 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
540 sizeof(struct user_regs_struct))) {
545 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
546 ret = __get_user(tmp, (unsigned long __user *) data);
549 ret = putreg(child, ui, tmp);
552 data += sizeof(long);
557 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
558 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
559 sizeof(struct user_i387_struct))) {
563 ret = get_fpregs((struct user_i387_struct __user *)data, child);
567 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
568 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
569 sizeof(struct user_i387_struct))) {
573 set_stopped_child_used_math(child);
574 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
579 ret = ptrace_request(child, request, addr, data);
585 static void syscall_trace(struct pt_regs *regs)
589 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
591 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
592 current_thread_info()->flags, current->ptrace);
595 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
598 * this isn't the same as continuing with a signal, but it will do
599 * for normal use. strace only continues with a signal if the
600 * stopping signal is not SIGTRAP. -brl
602 if (current->exit_code) {
603 send_sig(current->exit_code, current, 1);
604 current->exit_code = 0;
608 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
610 /* do the secure computing check first */
611 secure_computing(regs->orig_rax);
613 if (test_thread_flag(TIF_SYSCALL_TRACE)
614 && (current->ptrace & PT_PTRACED))
617 if (unlikely(current->audit_context)) {
618 if (test_thread_flag(TIF_IA32)) {
619 audit_syscall_entry(AUDIT_ARCH_I386,
621 regs->rbx, regs->rcx,
622 regs->rdx, regs->rsi);
624 audit_syscall_entry(AUDIT_ARCH_X86_64,
626 regs->rdi, regs->rsi,
627 regs->rdx, regs->r10);
632 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
634 if (unlikely(current->audit_context))
635 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
637 if ((test_thread_flag(TIF_SYSCALL_TRACE)
638 || test_thread_flag(TIF_SINGLESTEP))
639 && (current->ptrace & PT_PTRACED))