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;
84 * Called by kernel/ptrace.c when detaching..
86 * Make sure the single step bit is not set.
88 void ptrace_disable(struct task_struct *child)
90 user_disable_single_step(child);
93 static int putreg(struct task_struct *child,
94 unsigned long regno, unsigned long value)
99 case offsetof(struct user_regs_struct,fs):
100 if (value && (value & 3) != 3)
102 child->thread.fsindex = value & 0xffff;
104 case offsetof(struct user_regs_struct,gs):
105 if (value && (value & 3) != 3)
107 child->thread.gsindex = value & 0xffff;
109 case offsetof(struct user_regs_struct,ds):
110 if (value && (value & 3) != 3)
112 child->thread.ds = value & 0xffff;
114 case offsetof(struct user_regs_struct,es):
115 if (value && (value & 3) != 3)
117 child->thread.es = value & 0xffff;
119 case offsetof(struct user_regs_struct,ss):
120 if ((value & 3) != 3)
124 case offsetof(struct user_regs_struct,fs_base):
125 if (value >= TASK_SIZE_OF(child))
128 * When changing the segment base, use do_arch_prctl
129 * to set either thread.fs or thread.fsindex and the
130 * corresponding GDT slot.
132 if (child->thread.fs != value)
133 return do_arch_prctl(child, ARCH_SET_FS, value);
135 case offsetof(struct user_regs_struct,gs_base):
137 * Exactly the same here as the %fs handling above.
139 if (value >= TASK_SIZE_OF(child))
141 if (child->thread.gs != value)
142 return do_arch_prctl(child, ARCH_SET_GS, value);
144 case offsetof(struct user_regs_struct, eflags):
147 * If the user value contains TF, mark that
148 * it was not "us" (the debugger) that set it.
149 * If not, make sure it stays set if we had.
151 if (value & X86_EFLAGS_TF)
152 clear_tsk_thread_flag(child, TIF_FORCED_TF);
153 else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
154 value |= X86_EFLAGS_TF;
155 tmp = get_stack_long(child, EFL_OFFSET);
159 case offsetof(struct user_regs_struct,cs):
160 if ((value & 3) != 3)
165 put_stack_long(child, regno - sizeof(struct pt_regs), value);
169 static unsigned long getreg(struct task_struct *child, unsigned long regno)
173 case offsetof(struct user_regs_struct, fs):
174 return child->thread.fsindex;
175 case offsetof(struct user_regs_struct, gs):
176 return child->thread.gsindex;
177 case offsetof(struct user_regs_struct, ds):
178 return child->thread.ds;
179 case offsetof(struct user_regs_struct, es):
180 return child->thread.es;
181 case offsetof(struct user_regs_struct, fs_base):
183 * do_arch_prctl may have used a GDT slot instead of
184 * the MSR. To userland, it appears the same either
185 * way, except the %fs segment selector might not be 0.
187 if (child->thread.fs != 0)
188 return child->thread.fs;
189 if (child->thread.fsindex != FS_TLS_SEL)
191 return get_desc_base(&child->thread.tls_array[FS_TLS]);
192 case offsetof(struct user_regs_struct, gs_base):
194 * Exactly the same here as the %fs handling above.
196 if (child->thread.gs != 0)
197 return child->thread.gs;
198 if (child->thread.gsindex != GS_TLS_SEL)
200 return get_desc_base(&child->thread.tls_array[GS_TLS]);
201 case offsetof(struct user_regs_struct, eflags):
203 * If the debugger set TF, hide it from the readout.
205 regno = regno - sizeof(struct pt_regs);
206 val = get_stack_long(child, regno);
207 if (test_tsk_thread_flag(child, TIF_IA32))
209 if (test_tsk_thread_flag(child, TIF_FORCED_TF))
210 val &= ~X86_EFLAGS_TF;
213 regno = regno - sizeof(struct pt_regs);
214 val = get_stack_long(child, regno);
215 if (test_tsk_thread_flag(child, TIF_IA32))
222 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
228 /* when I and D space are separate, these will need to be fixed. */
229 case PTRACE_PEEKTEXT: /* read word at location addr. */
230 case PTRACE_PEEKDATA:
231 ret = generic_ptrace_peekdata(child, addr, data);
234 /* read the word at location addr in the USER area. */
235 case PTRACE_PEEKUSR: {
240 addr > sizeof(struct user) - 7)
244 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
245 tmp = getreg(child, addr);
247 case offsetof(struct user, u_debugreg[0]):
248 tmp = child->thread.debugreg0;
250 case offsetof(struct user, u_debugreg[1]):
251 tmp = child->thread.debugreg1;
253 case offsetof(struct user, u_debugreg[2]):
254 tmp = child->thread.debugreg2;
256 case offsetof(struct user, u_debugreg[3]):
257 tmp = child->thread.debugreg3;
259 case offsetof(struct user, u_debugreg[6]):
260 tmp = child->thread.debugreg6;
262 case offsetof(struct user, u_debugreg[7]):
263 tmp = child->thread.debugreg7;
269 ret = put_user(tmp,(unsigned long __user *) data);
273 /* when I and D space are separate, this will have to be fixed. */
274 case PTRACE_POKETEXT: /* write the word at location addr. */
275 case PTRACE_POKEDATA:
276 ret = generic_ptrace_pokedata(child, addr, data);
279 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
281 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
284 addr > sizeof(struct user) - 7)
288 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
289 ret = putreg(child, addr, data);
291 /* Disallows to set a breakpoint into the vsyscall */
292 case offsetof(struct user, u_debugreg[0]):
293 if (data >= TASK_SIZE_OF(child) - dsize) break;
294 child->thread.debugreg0 = data;
297 case offsetof(struct user, u_debugreg[1]):
298 if (data >= TASK_SIZE_OF(child) - dsize) break;
299 child->thread.debugreg1 = data;
302 case offsetof(struct user, u_debugreg[2]):
303 if (data >= TASK_SIZE_OF(child) - dsize) break;
304 child->thread.debugreg2 = data;
307 case offsetof(struct user, u_debugreg[3]):
308 if (data >= TASK_SIZE_OF(child) - dsize) break;
309 child->thread.debugreg3 = data;
312 case offsetof(struct user, u_debugreg[6]):
315 child->thread.debugreg6 = data;
318 case offsetof(struct user, u_debugreg[7]):
319 /* See arch/i386/kernel/ptrace.c for an explanation of
320 * this awkward check.*/
321 data &= ~DR_CONTROL_RESERVED;
323 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
326 child->thread.debugreg7 = data;
328 set_tsk_thread_flag(child, TIF_DEBUG);
330 clear_tsk_thread_flag(child, TIF_DEBUG);
338 #ifdef CONFIG_IA32_EMULATION
339 /* This makes only sense with 32bit programs. Allow a
340 64bit debugger to fully examine them too. Better
341 don't use it against 64bit processes, use
342 PTRACE_ARCH_PRCTL instead. */
343 case PTRACE_GET_THREAD_AREA:
346 ret = do_get_thread_area(child, addr,
347 (struct user_desc __user *) data);
350 case PTRACE_SET_THREAD_AREA:
353 ret = do_set_thread_area(child, addr,
354 (struct user_desc __user *) data, 0);
357 /* normal 64bit interface to access TLS data.
358 Works just like arch_prctl, except that the arguments
360 case PTRACE_ARCH_PRCTL:
361 ret = do_arch_prctl(child, data, addr);
364 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
365 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
366 sizeof(struct user_regs_struct))) {
371 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
372 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
373 data += sizeof(long);
378 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
380 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
381 sizeof(struct user_regs_struct))) {
386 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
387 ret = __get_user(tmp, (unsigned long __user *) data);
390 ret = putreg(child, ui, tmp);
393 data += sizeof(long);
398 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
399 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
400 sizeof(struct user_i387_struct))) {
404 ret = get_fpregs((struct user_i387_struct __user *)data, child);
408 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
409 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
410 sizeof(struct user_i387_struct))) {
414 set_stopped_child_used_math(child);
415 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
420 ret = ptrace_request(child, request, addr, data);
426 static void syscall_trace(struct pt_regs *regs)
430 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
432 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
433 current_thread_info()->flags, current->ptrace);
436 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
439 * this isn't the same as continuing with a signal, but it will do
440 * for normal use. strace only continues with a signal if the
441 * stopping signal is not SIGTRAP. -brl
443 if (current->exit_code) {
444 send_sig(current->exit_code, current, 1);
445 current->exit_code = 0;
449 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
451 /* do the secure computing check first */
452 secure_computing(regs->orig_rax);
454 if (test_thread_flag(TIF_SYSCALL_TRACE)
455 && (current->ptrace & PT_PTRACED))
458 if (unlikely(current->audit_context)) {
459 if (test_thread_flag(TIF_IA32)) {
460 audit_syscall_entry(AUDIT_ARCH_I386,
462 regs->rbx, regs->rcx,
463 regs->rdx, regs->rsi);
465 audit_syscall_entry(AUDIT_ARCH_X86_64,
467 regs->rdi, regs->rsi,
468 regs->rdx, regs->r10);
473 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
475 if (unlikely(current->audit_context))
476 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
478 if ((test_thread_flag(TIF_SYSCALL_TRACE)
479 || test_thread_flag(TIF_SINGLESTEP))
480 && (current->ptrace & PT_PTRACED))