]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86_64/kernel/ptrace.c
PTRACE_PEEKDATA consolidation
[net-next-2.6.git] / arch / x86_64 / kernel / ptrace.c
CommitLineData
1da177e4
LT
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 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/ptrace.h>
16#include <linux/user.h>
17#include <linux/security.h>
18#include <linux/audit.h>
19#include <linux/seccomp.h>
7ed20e1a 20#include <linux/signal.h>
1da177e4
LT
21
22#include <asm/uaccess.h>
23#include <asm/pgtable.h>
24#include <asm/system.h>
25#include <asm/processor.h>
26#include <asm/i387.h>
27#include <asm/debugreg.h>
28#include <asm/ldt.h>
29#include <asm/desc.h>
30#include <asm/proto.h>
31#include <asm/ia32.h>
32
33/*
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
36 */
37
60923df3
CE
38/*
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).
42 */
43#define FLAG_MASK 0x54dd5UL
1da177e4
LT
44
45/* set's the trap flag. */
46#define TRAP_FLAG 0x100UL
47
48/*
49 * eflags and offset of eflags on child stack..
50 */
51#define EFLAGS offsetof(struct pt_regs, eflags)
52#define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
53
54/*
55 * this routine will get a word off of the processes privileged stack.
56 * the offset is how far from the base addr as stored in the TSS.
57 * this routine assumes that all the privileged stacks are in our
58 * data space.
59 */
60static inline unsigned long get_stack_long(struct task_struct *task, int offset)
61{
62 unsigned char *stack;
63
64 stack = (unsigned char *)task->thread.rsp0;
65 stack += offset;
66 return (*((unsigned long *)stack));
67}
68
69/*
70 * this routine will put a word on the processes privileged stack.
71 * the offset is how far from the base addr as stored in the TSS.
72 * this routine assumes that all the privileged stacks are in our
73 * data space.
74 */
75static inline long put_stack_long(struct task_struct *task, int offset,
76 unsigned long data)
77{
78 unsigned char * stack;
79
80 stack = (unsigned char *) task->thread.rsp0;
81 stack += offset;
82 *(unsigned long *) stack = data;
83 return 0;
84}
85
e502cdd6
AK
86#define LDT_SEGMENT 4
87
88unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
89{
90 unsigned long addr, seg;
91
92 addr = regs->rip;
93 seg = regs->cs & 0xffff;
94
95 /*
96 * We'll assume that the code segments in the GDT
97 * are all zero-based. That is largely true: the
98 * TLS segments are used for data, and the PNPBIOS
99 * and APM bios ones we just ignore here.
100 */
101 if (seg & LDT_SEGMENT) {
102 u32 *desc;
103 unsigned long base;
104
105 down(&child->mm->context.sem);
106 desc = child->mm->context.ldt + (seg & ~7);
107 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
108
109 /* 16-bit code segment? */
110 if (!((desc[1] >> 22) & 1))
111 addr &= 0xffff;
112 addr += base;
113 up(&child->mm->context.sem);
114 }
115 return addr;
116}
117
2ade2920 118static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
e502cdd6
AK
119{
120 int i, copied;
2ade2920 121 unsigned char opcode[15];
e502cdd6
AK
122 unsigned long addr = convert_rip_to_linear(child, regs);
123
124 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
125 for (i = 0; i < copied; i++) {
126 switch (opcode[i]) {
2ade2920
CE
127 /* popf and iret */
128 case 0x9d: case 0xcf:
e502cdd6
AK
129 return 1;
130
131 /* CHECKME: 64 65 */
132
133 /* opcode and address size prefixes */
134 case 0x66: case 0x67:
135 continue;
136 /* irrelevant prefixes (segment overrides and repeats) */
137 case 0x26: case 0x2e:
138 case 0x36: case 0x3e:
139 case 0x64: case 0x65:
d4d35854 140 case 0xf2: case 0xf3:
e502cdd6
AK
141 continue;
142
e502cdd6 143 case 0x40 ... 0x4f:
a752d719
CE
144 if (regs->cs != __USER_CS)
145 /* 32-bit mode: register increment */
146 return 0;
147 /* 64-bit mode: REX prefix */
e502cdd6
AK
148 continue;
149
d4d35854 150 /* CHECKME: f2, f3 */
e502cdd6
AK
151
152 /*
153 * pushf: NOTE! We should probably not let
154 * the user see the TF bit being set. But
155 * it's more pain than it's worth to avoid
156 * it, and a debugger could emulate this
157 * all in user space if it _really_ cares.
158 */
159 case 0x9c:
160 default:
161 return 0;
162 }
163 }
164 return 0;
165}
166
aa85b9af
AK
167static void set_singlestep(struct task_struct *child)
168{
bb049232 169 struct pt_regs *regs = task_pt_regs(child);
aa85b9af
AK
170
171 /*
172 * Always set TIF_SINGLESTEP - this guarantees that
173 * we single-step system calls etc.. This will also
174 * cause us to set TF when returning to user mode.
175 */
176 set_tsk_thread_flag(child, TIF_SINGLESTEP);
177
178 /*
179 * If TF was already set, don't do anything else
180 */
181 if (regs->eflags & TRAP_FLAG)
182 return;
183
184 /* Set TF on the kernel stack.. */
185 regs->eflags |= TRAP_FLAG;
186
e502cdd6
AK
187 /*
188 * ..but if TF is changed by the instruction we will trace,
189 * don't mark it as being "us" that set it, so that we
190 * won't clear it by hand later.
e502cdd6 191 */
2ade2920 192 if (is_setting_trap_flag(child, regs))
e502cdd6
AK
193 return;
194
aa85b9af
AK
195 child->ptrace |= PT_DTRACE;
196}
197
198static void clear_singlestep(struct task_struct *child)
199{
200 /* Always clear TIF_SINGLESTEP... */
201 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
202
203 /* But touch TF only if it was set by us.. */
204 if (child->ptrace & PT_DTRACE) {
bb049232 205 struct pt_regs *regs = task_pt_regs(child);
aa85b9af
AK
206 regs->eflags &= ~TRAP_FLAG;
207 child->ptrace &= ~PT_DTRACE;
208 }
209}
210
1da177e4
LT
211/*
212 * Called by kernel/ptrace.c when detaching..
213 *
214 * Make sure the single step bit is not set.
215 */
216void ptrace_disable(struct task_struct *child)
217{
aa85b9af 218 clear_singlestep(child);
1da177e4
LT
219}
220
221static int putreg(struct task_struct *child,
222 unsigned long regno, unsigned long value)
223{
224 unsigned long tmp;
225
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):
84929801 257 if (value >= TASK_SIZE_OF(child))
f6b8d477 258 return -EIO;
1da177e4
LT
259 child->thread.fs = value;
260 return 0;
261 case offsetof(struct user_regs_struct,gs_base):
84929801 262 if (value >= TASK_SIZE_OF(child))
f6b8d477 263 return -EIO;
1da177e4
LT
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 }
278 put_stack_long(child, regno - sizeof(struct pt_regs), value);
279 return 0;
280}
281
282static unsigned long getreg(struct task_struct *child, unsigned long regno)
283{
284 unsigned long val;
285 switch (regno) {
286 case offsetof(struct user_regs_struct, fs):
287 return child->thread.fsindex;
288 case offsetof(struct user_regs_struct, gs):
289 return child->thread.gsindex;
290 case offsetof(struct user_regs_struct, ds):
291 return child->thread.ds;
292 case offsetof(struct user_regs_struct, es):
293 return child->thread.es;
294 case offsetof(struct user_regs_struct, fs_base):
295 return child->thread.fs;
296 case offsetof(struct user_regs_struct, gs_base):
297 return child->thread.gs;
298 default:
299 regno = regno - sizeof(struct pt_regs);
300 val = get_stack_long(child, regno);
301 if (test_tsk_thread_flag(child, TIF_IA32))
302 val &= 0xffffffff;
303 return val;
304 }
305
306}
307
481bed45 308long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1da177e4 309{
1da177e4
LT
310 long i, ret;
311 unsigned ui;
312
1da177e4
LT
313 switch (request) {
314 /* when I and D space are separate, these will need to be fixed. */
315 case PTRACE_PEEKTEXT: /* read word at location addr. */
76647323
AD
316 case PTRACE_PEEKDATA:
317 ret = generic_ptrace_peekdata(child, addr, data);
1da177e4 318 break;
1da177e4
LT
319
320 /* read the word at location addr in the USER area. */
321 case PTRACE_PEEKUSR: {
322 unsigned long tmp;
323
324 ret = -EIO;
325 if ((addr & 7) ||
326 addr > sizeof(struct user) - 7)
327 break;
328
329 switch (addr) {
c4d1fcf3 330 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
1da177e4
LT
331 tmp = getreg(child, addr);
332 break;
333 case offsetof(struct user, u_debugreg[0]):
334 tmp = child->thread.debugreg0;
335 break;
336 case offsetof(struct user, u_debugreg[1]):
337 tmp = child->thread.debugreg1;
338 break;
339 case offsetof(struct user, u_debugreg[2]):
340 tmp = child->thread.debugreg2;
341 break;
342 case offsetof(struct user, u_debugreg[3]):
343 tmp = child->thread.debugreg3;
344 break;
345 case offsetof(struct user, u_debugreg[6]):
346 tmp = child->thread.debugreg6;
347 break;
348 case offsetof(struct user, u_debugreg[7]):
349 tmp = child->thread.debugreg7;
350 break;
351 default:
352 tmp = 0;
353 break;
354 }
355 ret = put_user(tmp,(unsigned long __user *) data);
356 break;
357 }
358
359 /* when I and D space are separate, this will have to be fixed. */
360 case PTRACE_POKETEXT: /* write the word at location addr. */
361 case PTRACE_POKEDATA:
362 ret = 0;
363 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
364 break;
365 ret = -EIO;
366 break;
367
368 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
84929801
SS
369 {
370 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
1da177e4
LT
371 ret = -EIO;
372 if ((addr & 7) ||
373 addr > sizeof(struct user) - 7)
374 break;
375
376 switch (addr) {
c4d1fcf3 377 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
1da177e4
LT
378 ret = putreg(child, addr, data);
379 break;
380 /* Disallows to set a breakpoint into the vsyscall */
381 case offsetof(struct user, u_debugreg[0]):
84929801 382 if (data >= TASK_SIZE_OF(child) - dsize) break;
1da177e4
LT
383 child->thread.debugreg0 = data;
384 ret = 0;
385 break;
386 case offsetof(struct user, u_debugreg[1]):
84929801 387 if (data >= TASK_SIZE_OF(child) - dsize) break;
1da177e4
LT
388 child->thread.debugreg1 = data;
389 ret = 0;
390 break;
391 case offsetof(struct user, u_debugreg[2]):
84929801 392 if (data >= TASK_SIZE_OF(child) - dsize) break;
1da177e4
LT
393 child->thread.debugreg2 = data;
394 ret = 0;
395 break;
396 case offsetof(struct user, u_debugreg[3]):
84929801 397 if (data >= TASK_SIZE_OF(child) - dsize) break;
1da177e4
LT
398 child->thread.debugreg3 = data;
399 ret = 0;
400 break;
401 case offsetof(struct user, u_debugreg[6]):
402 if (data >> 32)
403 break;
404 child->thread.debugreg6 = data;
405 ret = 0;
406 break;
407 case offsetof(struct user, u_debugreg[7]):
408 /* See arch/i386/kernel/ptrace.c for an explanation of
409 * this awkward check.*/
893efca9
JB
410 data &= ~DR_CONTROL_RESERVED;
411 for(i=0; i<4; i++)
412 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
1da177e4
LT
413 break;
414 if (i == 4) {
d3a4f48d
SE
415 child->thread.debugreg7 = data;
416 if (data)
417 set_tsk_thread_flag(child, TIF_DEBUG);
418 else
419 clear_tsk_thread_flag(child, TIF_DEBUG);
1da177e4 420 ret = 0;
d3a4f48d 421 }
1da177e4
LT
422 break;
423 }
424 break;
84929801 425 }
1da177e4 426 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
aa85b9af 427 case PTRACE_CONT: /* restart after signal. */
1da177e4
LT
428
429 ret = -EIO;
7ed20e1a 430 if (!valid_signal(data))
1da177e4
LT
431 break;
432 if (request == PTRACE_SYSCALL)
433 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
434 else
435 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
436 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
437 child->exit_code = data;
aa85b9af
AK
438 /* make sure the single step bit is not set. */
439 clear_singlestep(child);
1da177e4
LT
440 wake_up_process(child);
441 ret = 0;
442 break;
1da177e4
LT
443
444#ifdef CONFIG_IA32_EMULATION
445 /* This makes only sense with 32bit programs. Allow a
446 64bit debugger to fully examine them too. Better
447 don't use it against 64bit processes, use
448 PTRACE_ARCH_PRCTL instead. */
449 case PTRACE_SET_THREAD_AREA: {
450 struct user_desc __user *p;
451 int old;
452 p = (struct user_desc __user *)data;
453 get_user(old, &p->entry_number);
454 put_user(addr, &p->entry_number);
455 ret = do_set_thread_area(&child->thread, p);
456 put_user(old, &p->entry_number);
457 break;
458 case PTRACE_GET_THREAD_AREA:
459 p = (struct user_desc __user *)data;
460 get_user(old, &p->entry_number);
461 put_user(addr, &p->entry_number);
462 ret = do_get_thread_area(&child->thread, p);
463 put_user(old, &p->entry_number);
464 break;
465 }
466#endif
467 /* normal 64bit interface to access TLS data.
468 Works just like arch_prctl, except that the arguments
469 are reversed. */
470 case PTRACE_ARCH_PRCTL:
471 ret = do_arch_prctl(child, data, addr);
472 break;
473
474/*
475 * make the child exit. Best I can do is send it a sigkill.
476 * perhaps it should be put in the status that it wants to
477 * exit.
478 */
aa85b9af 479 case PTRACE_KILL:
1da177e4
LT
480 ret = 0;
481 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
482 break;
483 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
484 child->exit_code = SIGKILL;
485 /* make sure the single step bit is not set. */
aa85b9af 486 clear_singlestep(child);
1da177e4
LT
487 wake_up_process(child);
488 break;
1da177e4 489
aa85b9af 490 case PTRACE_SINGLESTEP: /* set the trap flag. */
1da177e4 491 ret = -EIO;
7ed20e1a 492 if (!valid_signal(data))
1da177e4
LT
493 break;
494 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
aa85b9af 495 set_singlestep(child);
1da177e4
LT
496 child->exit_code = data;
497 /* give it a chance to run. */
498 wake_up_process(child);
499 ret = 0;
500 break;
1da177e4
LT
501
502 case PTRACE_DETACH:
503 /* detach a process that was attached. */
504 ret = ptrace_detach(child, data);
505 break;
506
507 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
508 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
509 sizeof(struct user_regs_struct))) {
510 ret = -EIO;
511 break;
512 }
513 ret = 0;
514 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
515 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
516 data += sizeof(long);
517 }
518 break;
519 }
520
521 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
522 unsigned long tmp;
523 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
524 sizeof(struct user_regs_struct))) {
525 ret = -EIO;
526 break;
527 }
528 ret = 0;
529 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
f49481bc
AK
530 ret = __get_user(tmp, (unsigned long __user *) data);
531 if (ret)
532 break;
533 ret = putreg(child, ui, tmp);
534 if (ret)
535 break;
1da177e4
LT
536 data += sizeof(long);
537 }
538 break;
539 }
540
541 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
542 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
543 sizeof(struct user_i387_struct))) {
544 ret = -EIO;
545 break;
546 }
547 ret = get_fpregs((struct user_i387_struct __user *)data, child);
548 break;
549 }
550
551 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
552 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
553 sizeof(struct user_i387_struct))) {
554 ret = -EIO;
555 break;
556 }
557 set_stopped_child_used_math(child);
558 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
559 break;
560 }
561
562 default:
563 ret = ptrace_request(child, request, addr, data);
564 break;
565 }
1da177e4
LT
566 return ret;
567}
568
569static void syscall_trace(struct pt_regs *regs)
570{
571
572#if 0
573 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
574 current->comm,
575 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
576 current_thread_info()->flags, current->ptrace);
577#endif
578
579 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
580 ? 0x80 : 0));
581 /*
582 * this isn't the same as continuing with a signal, but it will do
583 * for normal use. strace only continues with a signal if the
584 * stopping signal is not SIGTRAP. -brl
585 */
586 if (current->exit_code) {
587 send_sig(current->exit_code, current, 1);
588 current->exit_code = 0;
589 }
590}
591
592asmlinkage void syscall_trace_enter(struct pt_regs *regs)
593{
594 /* do the secure computing check first */
595 secure_computing(regs->orig_rax);
596
2fd6f58b
DW
597 if (test_thread_flag(TIF_SYSCALL_TRACE)
598 && (current->ptrace & PT_PTRACED))
599 syscall_trace(regs);
600
488f2eac
DW
601 if (unlikely(current->audit_context)) {
602 if (test_thread_flag(TIF_IA32)) {
5411be59 603 audit_syscall_entry(AUDIT_ARCH_I386,
488f2eac
DW
604 regs->orig_rax,
605 regs->rbx, regs->rcx,
606 regs->rdx, regs->rsi);
607 } else {
5411be59 608 audit_syscall_entry(AUDIT_ARCH_X86_64,
488f2eac
DW
609 regs->orig_rax,
610 regs->rdi, regs->rsi,
611 regs->rdx, regs->r10);
612 }
613 }
1da177e4
LT
614}
615
616asmlinkage void syscall_trace_leave(struct pt_regs *regs)
617{
618 if (unlikely(current->audit_context))
5411be59 619 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
1da177e4
LT
620
621 if ((test_thread_flag(TIF_SYSCALL_TRACE)
622 || test_thread_flag(TIF_SINGLESTEP))
623 && (current->ptrace & PT_PTRACED))
624 syscall_trace(regs);
625}