[PATCH] x86_64: Fix VSMP build
[linux-2.6/pdupreez.git] / arch / x86_64 / kernel / ptrace.c
blob53205622351c3cc8b1c7110b93239fa8dc84fce4
1 /* ptrace.c */
2 /* By Ross Biro 1/23/92 */
3 /*
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
6 *
7 * x86-64 port 2000-2002 Andi Kleen
8 */
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
27 #include <asm/i387.h>
28 #include <asm/debugreg.h>
29 #include <asm/ldt.h>
30 #include <asm/desc.h>
31 #include <asm/proto.h>
32 #include <asm/ia32.h>
35 * does not yet catch signals sent when the child dies.
36 * in exit.c or in signal.c.
40 * Determines which flags the user has access to [1 = access, 0 = no access].
41 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
42 * Also masks reserved bits (63-22, 15, 5, 3, 1).
44 #define FLAG_MASK 0x54dd5UL
46 /* set's the trap flag. */
47 #define TRAP_FLAG 0x100UL
50 * eflags and offset of eflags on child stack..
52 #define EFLAGS offsetof(struct pt_regs, eflags)
53 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
56 * this routine will get a word off of the processes privileged stack.
57 * the offset is how far from the base addr as stored in the TSS.
58 * this routine assumes that all the privileged stacks are in our
59 * data space.
60 */
61 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
63 unsigned char *stack;
65 stack = (unsigned char *)task->thread.rsp0;
66 stack += offset;
67 return (*((unsigned long *)stack));
71 * this routine will put a word on the processes privileged stack.
72 * the offset is how far from the base addr as stored in the TSS.
73 * this routine assumes that all the privileged stacks are in our
74 * data space.
76 static inline long put_stack_long(struct task_struct *task, int offset,
77 unsigned long data)
79 unsigned char * stack;
81 stack = (unsigned char *) task->thread.rsp0;
82 stack += offset;
83 *(unsigned long *) stack = data;
84 return 0;
87 #define LDT_SEGMENT 4
89 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
91 unsigned long addr, seg;
93 addr = regs->rip;
94 seg = regs->cs & 0xffff;
97 * We'll assume that the code segments in the GDT
98 * are all zero-based. That is largely true: the
99 * TLS segments are used for data, and the PNPBIOS
100 * and APM bios ones we just ignore here.
102 if (seg & LDT_SEGMENT) {
103 u32 *desc;
104 unsigned long base;
106 down(&child->mm->context.sem);
107 desc = child->mm->context.ldt + (seg & ~7);
108 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
110 /* 16-bit code segment? */
111 if (!((desc[1] >> 22) & 1))
112 addr &= 0xffff;
113 addr += base;
114 up(&child->mm->context.sem);
116 return addr;
119 static int is_at_popf(struct task_struct *child, struct pt_regs *regs)
121 int i, copied;
122 unsigned char opcode[16];
123 unsigned long addr = convert_rip_to_linear(child, regs);
125 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
126 for (i = 0; i < copied; i++) {
127 switch (opcode[i]) {
128 /* popf */
129 case 0x9d:
130 return 1;
132 /* CHECKME: 64 65 */
134 /* opcode and address size prefixes */
135 case 0x66: case 0x67:
136 continue;
137 /* irrelevant prefixes (segment overrides and repeats) */
138 case 0x26: case 0x2e:
139 case 0x36: case 0x3e:
140 case 0x64: case 0x65:
141 case 0xf0: case 0xf2: case 0xf3:
142 continue;
144 /* REX prefixes */
145 case 0x40 ... 0x4f:
146 continue;
148 /* CHECKME: f0, f2, f3 */
151 * pushf: NOTE! We should probably not let
152 * the user see the TF bit being set. But
153 * it's more pain than it's worth to avoid
154 * it, and a debugger could emulate this
155 * all in user space if it _really_ cares.
157 case 0x9c:
158 default:
159 return 0;
162 return 0;
165 static void set_singlestep(struct task_struct *child)
167 struct pt_regs *regs = task_pt_regs(child);
170 * Always set TIF_SINGLESTEP - this guarantees that
171 * we single-step system calls etc.. This will also
172 * cause us to set TF when returning to user mode.
174 set_tsk_thread_flag(child, TIF_SINGLESTEP);
177 * If TF was already set, don't do anything else
179 if (regs->eflags & TRAP_FLAG)
180 return;
182 /* Set TF on the kernel stack.. */
183 regs->eflags |= TRAP_FLAG;
186 * ..but if TF is changed by the instruction we will trace,
187 * don't mark it as being "us" that set it, so that we
188 * won't clear it by hand later.
190 * AK: this is not enough, LAHF and IRET can change TF in user space too.
192 if (is_at_popf(child, regs))
193 return;
195 child->ptrace |= PT_DTRACE;
198 static void clear_singlestep(struct task_struct *child)
200 /* Always clear TIF_SINGLESTEP... */
201 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
203 /* But touch TF only if it was set by us.. */
204 if (child->ptrace & PT_DTRACE) {
205 struct pt_regs *regs = task_pt_regs(child);
206 regs->eflags &= ~TRAP_FLAG;
207 child->ptrace &= ~PT_DTRACE;
212 * Called by kernel/ptrace.c when detaching..
214 * Make sure the single step bit is not set.
216 void ptrace_disable(struct task_struct *child)
218 clear_singlestep(child);
221 static int putreg(struct task_struct *child,
222 unsigned long regno, unsigned long value)
224 unsigned long tmp;
226 /* Some code in the 64bit emulation may not be 64bit clean.
227 Don't take any chances. */
228 if (test_tsk_thread_flag(child, TIF_IA32))
229 value &= 0xffffffff;
230 switch (regno) {
231 case offsetof(struct user_regs_struct,fs):
232 if (value && (value & 3) != 3)
233 return -EIO;
234 child->thread.fsindex = value & 0xffff;
235 return 0;
236 case offsetof(struct user_regs_struct,gs):
237 if (value && (value & 3) != 3)
238 return -EIO;
239 child->thread.gsindex = value & 0xffff;
240 return 0;
241 case offsetof(struct user_regs_struct,ds):
242 if (value && (value & 3) != 3)
243 return -EIO;
244 child->thread.ds = value & 0xffff;
245 return 0;
246 case offsetof(struct user_regs_struct,es):
247 if (value && (value & 3) != 3)
248 return -EIO;
249 child->thread.es = value & 0xffff;
250 return 0;
251 case offsetof(struct user_regs_struct,ss):
252 if ((value & 3) != 3)
253 return -EIO;
254 value &= 0xffff;
255 return 0;
256 case offsetof(struct user_regs_struct,fs_base):
257 if (value >= TASK_SIZE_OF(child))
258 return -EIO;
259 child->thread.fs = value;
260 return 0;
261 case offsetof(struct user_regs_struct,gs_base):
262 if (value >= TASK_SIZE_OF(child))
263 return -EIO;
264 child->thread.gs = value;
265 return 0;
266 case offsetof(struct user_regs_struct, eflags):
267 value &= FLAG_MASK;
268 tmp = get_stack_long(child, EFL_OFFSET);
269 tmp &= ~FLAG_MASK;
270 value |= tmp;
271 break;
272 case offsetof(struct user_regs_struct,cs):
273 if ((value & 3) != 3)
274 return -EIO;
275 value &= 0xffff;
276 break;
277 case offsetof(struct user_regs_struct, rip):
278 /* Check if the new RIP address is canonical */
279 if (value >= TASK_SIZE_OF(child))
280 return -EIO;
281 break;
283 put_stack_long(child, regno - sizeof(struct pt_regs), value);
284 return 0;
287 static unsigned long getreg(struct task_struct *child, unsigned long regno)
289 unsigned long val;
290 switch (regno) {
291 case offsetof(struct user_regs_struct, fs):
292 return child->thread.fsindex;
293 case offsetof(struct user_regs_struct, gs):
294 return child->thread.gsindex;
295 case offsetof(struct user_regs_struct, ds):
296 return child->thread.ds;
297 case offsetof(struct user_regs_struct, es):
298 return child->thread.es;
299 case offsetof(struct user_regs_struct, fs_base):
300 return child->thread.fs;
301 case offsetof(struct user_regs_struct, gs_base):
302 return child->thread.gs;
303 default:
304 regno = regno - sizeof(struct pt_regs);
305 val = get_stack_long(child, regno);
306 if (test_tsk_thread_flag(child, TIF_IA32))
307 val &= 0xffffffff;
308 return val;
313 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
315 long i, ret;
316 unsigned ui;
318 switch (request) {
319 /* when I and D space are separate, these will need to be fixed. */
320 case PTRACE_PEEKTEXT: /* read word at location addr. */
321 case PTRACE_PEEKDATA: {
322 unsigned long tmp;
323 int copied;
325 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
326 ret = -EIO;
327 if (copied != sizeof(tmp))
328 break;
329 ret = put_user(tmp,(unsigned long __user *) data);
330 break;
333 /* read the word at location addr in the USER area. */
334 case PTRACE_PEEKUSR: {
335 unsigned long tmp;
337 ret = -EIO;
338 if ((addr & 7) ||
339 addr > sizeof(struct user) - 7)
340 break;
342 switch (addr) {
343 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
344 tmp = getreg(child, addr);
345 break;
346 case offsetof(struct user, u_debugreg[0]):
347 tmp = child->thread.debugreg0;
348 break;
349 case offsetof(struct user, u_debugreg[1]):
350 tmp = child->thread.debugreg1;
351 break;
352 case offsetof(struct user, u_debugreg[2]):
353 tmp = child->thread.debugreg2;
354 break;
355 case offsetof(struct user, u_debugreg[3]):
356 tmp = child->thread.debugreg3;
357 break;
358 case offsetof(struct user, u_debugreg[6]):
359 tmp = child->thread.debugreg6;
360 break;
361 case offsetof(struct user, u_debugreg[7]):
362 tmp = child->thread.debugreg7;
363 break;
364 default:
365 tmp = 0;
366 break;
368 ret = put_user(tmp,(unsigned long __user *) data);
369 break;
372 /* when I and D space are separate, this will have to be fixed. */
373 case PTRACE_POKETEXT: /* write the word at location addr. */
374 case PTRACE_POKEDATA:
375 ret = 0;
376 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
377 break;
378 ret = -EIO;
379 break;
381 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
383 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
384 ret = -EIO;
385 if ((addr & 7) ||
386 addr > sizeof(struct user) - 7)
387 break;
389 switch (addr) {
390 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
391 ret = putreg(child, addr, data);
392 break;
393 /* Disallows to set a breakpoint into the vsyscall */
394 case offsetof(struct user, u_debugreg[0]):
395 if (data >= TASK_SIZE_OF(child) - dsize) break;
396 child->thread.debugreg0 = data;
397 ret = 0;
398 break;
399 case offsetof(struct user, u_debugreg[1]):
400 if (data >= TASK_SIZE_OF(child) - dsize) break;
401 child->thread.debugreg1 = data;
402 ret = 0;
403 break;
404 case offsetof(struct user, u_debugreg[2]):
405 if (data >= TASK_SIZE_OF(child) - dsize) break;
406 child->thread.debugreg2 = data;
407 ret = 0;
408 break;
409 case offsetof(struct user, u_debugreg[3]):
410 if (data >= TASK_SIZE_OF(child) - dsize) break;
411 child->thread.debugreg3 = data;
412 ret = 0;
413 break;
414 case offsetof(struct user, u_debugreg[6]):
415 if (data >> 32)
416 break;
417 child->thread.debugreg6 = data;
418 ret = 0;
419 break;
420 case offsetof(struct user, u_debugreg[7]):
421 /* See arch/i386/kernel/ptrace.c for an explanation of
422 * this awkward check.*/
423 data &= ~DR_CONTROL_RESERVED;
424 for(i=0; i<4; i++)
425 if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
426 break;
427 if (i == 4) {
428 child->thread.debugreg7 = data;
429 ret = 0;
431 break;
433 break;
435 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
436 case PTRACE_CONT: /* restart after signal. */
438 ret = -EIO;
439 if (!valid_signal(data))
440 break;
441 if (request == PTRACE_SYSCALL)
442 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
443 else
444 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
445 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
446 child->exit_code = data;
447 /* make sure the single step bit is not set. */
448 clear_singlestep(child);
449 wake_up_process(child);
450 ret = 0;
451 break;
453 #ifdef CONFIG_IA32_EMULATION
454 /* This makes only sense with 32bit programs. Allow a
455 64bit debugger to fully examine them too. Better
456 don't use it against 64bit processes, use
457 PTRACE_ARCH_PRCTL instead. */
458 case PTRACE_SET_THREAD_AREA: {
459 struct user_desc __user *p;
460 int old;
461 p = (struct user_desc __user *)data;
462 get_user(old, &p->entry_number);
463 put_user(addr, &p->entry_number);
464 ret = do_set_thread_area(&child->thread, p);
465 put_user(old, &p->entry_number);
466 break;
467 case PTRACE_GET_THREAD_AREA:
468 p = (struct user_desc __user *)data;
469 get_user(old, &p->entry_number);
470 put_user(addr, &p->entry_number);
471 ret = do_get_thread_area(&child->thread, p);
472 put_user(old, &p->entry_number);
473 break;
475 #endif
476 /* normal 64bit interface to access TLS data.
477 Works just like arch_prctl, except that the arguments
478 are reversed. */
479 case PTRACE_ARCH_PRCTL:
480 ret = do_arch_prctl(child, data, addr);
481 break;
484 * make the child exit. Best I can do is send it a sigkill.
485 * perhaps it should be put in the status that it wants to
486 * exit.
488 case PTRACE_KILL:
489 ret = 0;
490 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
491 break;
492 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
493 child->exit_code = SIGKILL;
494 /* make sure the single step bit is not set. */
495 clear_singlestep(child);
496 wake_up_process(child);
497 break;
499 case PTRACE_SINGLESTEP: /* set the trap flag. */
500 ret = -EIO;
501 if (!valid_signal(data))
502 break;
503 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
504 set_singlestep(child);
505 child->exit_code = data;
506 /* give it a chance to run. */
507 wake_up_process(child);
508 ret = 0;
509 break;
511 case PTRACE_DETACH:
512 /* detach a process that was attached. */
513 ret = ptrace_detach(child, data);
514 break;
516 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
517 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
518 sizeof(struct user_regs_struct))) {
519 ret = -EIO;
520 break;
522 ret = 0;
523 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
524 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
525 data += sizeof(long);
527 break;
530 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
531 unsigned long tmp;
532 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
533 sizeof(struct user_regs_struct))) {
534 ret = -EIO;
535 break;
537 ret = 0;
538 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
539 ret |= __get_user(tmp, (unsigned long __user *) data);
540 putreg(child, ui, tmp);
541 data += sizeof(long);
543 break;
546 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
547 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
548 sizeof(struct user_i387_struct))) {
549 ret = -EIO;
550 break;
552 ret = get_fpregs((struct user_i387_struct __user *)data, child);
553 break;
556 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
557 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
558 sizeof(struct user_i387_struct))) {
559 ret = -EIO;
560 break;
562 set_stopped_child_used_math(child);
563 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
564 break;
567 default:
568 ret = ptrace_request(child, request, addr, data);
569 break;
571 return ret;
574 static void syscall_trace(struct pt_regs *regs)
577 #if 0
578 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
579 current->comm,
580 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
581 current_thread_info()->flags, current->ptrace);
582 #endif
584 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
585 ? 0x80 : 0));
587 * this isn't the same as continuing with a signal, but it will do
588 * for normal use. strace only continues with a signal if the
589 * stopping signal is not SIGTRAP. -brl
591 if (current->exit_code) {
592 send_sig(current->exit_code, current, 1);
593 current->exit_code = 0;
597 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
599 /* do the secure computing check first */
600 secure_computing(regs->orig_rax);
602 if (test_thread_flag(TIF_SYSCALL_TRACE)
603 && (current->ptrace & PT_PTRACED))
604 syscall_trace(regs);
606 if (unlikely(current->audit_context)) {
607 if (test_thread_flag(TIF_IA32)) {
608 audit_syscall_entry(current, AUDIT_ARCH_I386,
609 regs->orig_rax,
610 regs->rbx, regs->rcx,
611 regs->rdx, regs->rsi);
612 } else {
613 audit_syscall_entry(current, AUDIT_ARCH_X86_64,
614 regs->orig_rax,
615 regs->rdi, regs->rsi,
616 regs->rdx, regs->r10);
621 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
623 if (unlikely(current->audit_context))
624 audit_syscall_exit(current, AUDITSC_RESULT(regs->rax), regs->rax);
626 if ((test_thread_flag(TIF_SYSCALL_TRACE)
627 || test_thread_flag(TIF_SINGLESTEP))
628 && (current->ptrace & PT_PTRACED))
629 syscall_trace(regs);