[PATCH] i386/x86-64: rename is_at_popf(), add iret to tests and fix
[deliverable/linux.git] / arch / x86_64 / kernel / ptrace.c
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>
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>
22
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>
33
34 /*
35 * does not yet catch signals sent when the child dies.
36 * in exit.c or in signal.c.
37 */
38
39 /*
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).
43 */
44 #define FLAG_MASK 0x54dd5UL
45
46 /* set's the trap flag. */
47 #define TRAP_FLAG 0x100UL
48
49 /*
50 * eflags and offset of eflags on child stack..
51 */
52 #define EFLAGS offsetof(struct pt_regs, eflags)
53 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
54
55 /*
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)
62 {
63 unsigned char *stack;
64
65 stack = (unsigned char *)task->thread.rsp0;
66 stack += offset;
67 return (*((unsigned long *)stack));
68 }
69
70 /*
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.
75 */
76 static inline long put_stack_long(struct task_struct *task, int offset,
77 unsigned long data)
78 {
79 unsigned char * stack;
80
81 stack = (unsigned char *) task->thread.rsp0;
82 stack += offset;
83 *(unsigned long *) stack = data;
84 return 0;
85 }
86
87 #define LDT_SEGMENT 4
88
89 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
90 {
91 unsigned long addr, seg;
92
93 addr = regs->rip;
94 seg = regs->cs & 0xffff;
95
96 /*
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.
101 */
102 if (seg & LDT_SEGMENT) {
103 u32 *desc;
104 unsigned long base;
105
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);
109
110 /* 16-bit code segment? */
111 if (!((desc[1] >> 22) & 1))
112 addr &= 0xffff;
113 addr += base;
114 up(&child->mm->context.sem);
115 }
116 return addr;
117 }
118
119 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
120 {
121 int i, copied;
122 unsigned char opcode[15];
123 unsigned long addr = convert_rip_to_linear(child, regs);
124
125 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
126 for (i = 0; i < copied; i++) {
127 switch (opcode[i]) {
128 /* popf and iret */
129 case 0x9d: case 0xcf:
130 return 1;
131
132 /* CHECKME: 64 65 */
133
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 0xf2: case 0xf3:
142 continue;
143
144 case 0x40 ... 0x4f:
145 if (regs->cs != __USER_CS)
146 /* 32-bit mode: register increment */
147 return 0;
148 /* 64-bit mode: REX prefix */
149 continue;
150
151 /* CHECKME: f2, f3 */
152
153 /*
154 * pushf: NOTE! We should probably not let
155 * the user see the TF bit being set. But
156 * it's more pain than it's worth to avoid
157 * it, and a debugger could emulate this
158 * all in user space if it _really_ cares.
159 */
160 case 0x9c:
161 default:
162 return 0;
163 }
164 }
165 return 0;
166 }
167
168 static void set_singlestep(struct task_struct *child)
169 {
170 struct pt_regs *regs = task_pt_regs(child);
171
172 /*
173 * Always set TIF_SINGLESTEP - this guarantees that
174 * we single-step system calls etc.. This will also
175 * cause us to set TF when returning to user mode.
176 */
177 set_tsk_thread_flag(child, TIF_SINGLESTEP);
178
179 /*
180 * If TF was already set, don't do anything else
181 */
182 if (regs->eflags & TRAP_FLAG)
183 return;
184
185 /* Set TF on the kernel stack.. */
186 regs->eflags |= TRAP_FLAG;
187
188 /*
189 * ..but if TF is changed by the instruction we will trace,
190 * don't mark it as being "us" that set it, so that we
191 * won't clear it by hand later.
192 */
193 if (is_setting_trap_flag(child, regs))
194 return;
195
196 child->ptrace |= PT_DTRACE;
197 }
198
199 static void clear_singlestep(struct task_struct *child)
200 {
201 /* Always clear TIF_SINGLESTEP... */
202 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
203
204 /* But touch TF only if it was set by us.. */
205 if (child->ptrace & PT_DTRACE) {
206 struct pt_regs *regs = task_pt_regs(child);
207 regs->eflags &= ~TRAP_FLAG;
208 child->ptrace &= ~PT_DTRACE;
209 }
210 }
211
212 /*
213 * Called by kernel/ptrace.c when detaching..
214 *
215 * Make sure the single step bit is not set.
216 */
217 void ptrace_disable(struct task_struct *child)
218 {
219 clear_singlestep(child);
220 }
221
222 static int putreg(struct task_struct *child,
223 unsigned long regno, unsigned long value)
224 {
225 unsigned long tmp;
226
227 /* Some code in the 64bit emulation may not be 64bit clean.
228 Don't take any chances. */
229 if (test_tsk_thread_flag(child, TIF_IA32))
230 value &= 0xffffffff;
231 switch (regno) {
232 case offsetof(struct user_regs_struct,fs):
233 if (value && (value & 3) != 3)
234 return -EIO;
235 child->thread.fsindex = value & 0xffff;
236 return 0;
237 case offsetof(struct user_regs_struct,gs):
238 if (value && (value & 3) != 3)
239 return -EIO;
240 child->thread.gsindex = value & 0xffff;
241 return 0;
242 case offsetof(struct user_regs_struct,ds):
243 if (value && (value & 3) != 3)
244 return -EIO;
245 child->thread.ds = value & 0xffff;
246 return 0;
247 case offsetof(struct user_regs_struct,es):
248 if (value && (value & 3) != 3)
249 return -EIO;
250 child->thread.es = value & 0xffff;
251 return 0;
252 case offsetof(struct user_regs_struct,ss):
253 if ((value & 3) != 3)
254 return -EIO;
255 value &= 0xffff;
256 return 0;
257 case offsetof(struct user_regs_struct,fs_base):
258 if (value >= TASK_SIZE_OF(child))
259 return -EIO;
260 child->thread.fs = value;
261 return 0;
262 case offsetof(struct user_regs_struct,gs_base):
263 if (value >= TASK_SIZE_OF(child))
264 return -EIO;
265 child->thread.gs = value;
266 return 0;
267 case offsetof(struct user_regs_struct, eflags):
268 value &= FLAG_MASK;
269 tmp = get_stack_long(child, EFL_OFFSET);
270 tmp &= ~FLAG_MASK;
271 value |= tmp;
272 break;
273 case offsetof(struct user_regs_struct,cs):
274 if ((value & 3) != 3)
275 return -EIO;
276 value &= 0xffff;
277 break;
278 }
279 put_stack_long(child, regno - sizeof(struct pt_regs), value);
280 return 0;
281 }
282
283 static unsigned long getreg(struct task_struct *child, unsigned long regno)
284 {
285 unsigned long val;
286 switch (regno) {
287 case offsetof(struct user_regs_struct, fs):
288 return child->thread.fsindex;
289 case offsetof(struct user_regs_struct, gs):
290 return child->thread.gsindex;
291 case offsetof(struct user_regs_struct, ds):
292 return child->thread.ds;
293 case offsetof(struct user_regs_struct, es):
294 return child->thread.es;
295 case offsetof(struct user_regs_struct, fs_base):
296 return child->thread.fs;
297 case offsetof(struct user_regs_struct, gs_base):
298 return child->thread.gs;
299 default:
300 regno = regno - sizeof(struct pt_regs);
301 val = get_stack_long(child, regno);
302 if (test_tsk_thread_flag(child, TIF_IA32))
303 val &= 0xffffffff;
304 return val;
305 }
306
307 }
308
309 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
310 {
311 long i, ret;
312 unsigned ui;
313
314 switch (request) {
315 /* when I and D space are separate, these will need to be fixed. */
316 case PTRACE_PEEKTEXT: /* read word at location addr. */
317 case PTRACE_PEEKDATA: {
318 unsigned long tmp;
319 int copied;
320
321 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
322 ret = -EIO;
323 if (copied != sizeof(tmp))
324 break;
325 ret = put_user(tmp,(unsigned long __user *) data);
326 break;
327 }
328
329 /* read the word at location addr in the USER area. */
330 case PTRACE_PEEKUSR: {
331 unsigned long tmp;
332
333 ret = -EIO;
334 if ((addr & 7) ||
335 addr > sizeof(struct user) - 7)
336 break;
337
338 switch (addr) {
339 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
340 tmp = getreg(child, addr);
341 break;
342 case offsetof(struct user, u_debugreg[0]):
343 tmp = child->thread.debugreg0;
344 break;
345 case offsetof(struct user, u_debugreg[1]):
346 tmp = child->thread.debugreg1;
347 break;
348 case offsetof(struct user, u_debugreg[2]):
349 tmp = child->thread.debugreg2;
350 break;
351 case offsetof(struct user, u_debugreg[3]):
352 tmp = child->thread.debugreg3;
353 break;
354 case offsetof(struct user, u_debugreg[6]):
355 tmp = child->thread.debugreg6;
356 break;
357 case offsetof(struct user, u_debugreg[7]):
358 tmp = child->thread.debugreg7;
359 break;
360 default:
361 tmp = 0;
362 break;
363 }
364 ret = put_user(tmp,(unsigned long __user *) data);
365 break;
366 }
367
368 /* when I and D space are separate, this will have to be fixed. */
369 case PTRACE_POKETEXT: /* write the word at location addr. */
370 case PTRACE_POKEDATA:
371 ret = 0;
372 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
373 break;
374 ret = -EIO;
375 break;
376
377 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
378 {
379 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
380 ret = -EIO;
381 if ((addr & 7) ||
382 addr > sizeof(struct user) - 7)
383 break;
384
385 switch (addr) {
386 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
387 ret = putreg(child, addr, data);
388 break;
389 /* Disallows to set a breakpoint into the vsyscall */
390 case offsetof(struct user, u_debugreg[0]):
391 if (data >= TASK_SIZE_OF(child) - dsize) break;
392 child->thread.debugreg0 = data;
393 ret = 0;
394 break;
395 case offsetof(struct user, u_debugreg[1]):
396 if (data >= TASK_SIZE_OF(child) - dsize) break;
397 child->thread.debugreg1 = data;
398 ret = 0;
399 break;
400 case offsetof(struct user, u_debugreg[2]):
401 if (data >= TASK_SIZE_OF(child) - dsize) break;
402 child->thread.debugreg2 = data;
403 ret = 0;
404 break;
405 case offsetof(struct user, u_debugreg[3]):
406 if (data >= TASK_SIZE_OF(child) - dsize) break;
407 child->thread.debugreg3 = data;
408 ret = 0;
409 break;
410 case offsetof(struct user, u_debugreg[6]):
411 if (data >> 32)
412 break;
413 child->thread.debugreg6 = data;
414 ret = 0;
415 break;
416 case offsetof(struct user, u_debugreg[7]):
417 /* See arch/i386/kernel/ptrace.c for an explanation of
418 * this awkward check.*/
419 data &= ~DR_CONTROL_RESERVED;
420 for(i=0; i<4; i++)
421 if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
422 break;
423 if (i == 4) {
424 child->thread.debugreg7 = data;
425 if (data)
426 set_tsk_thread_flag(child, TIF_DEBUG);
427 else
428 clear_tsk_thread_flag(child, TIF_DEBUG);
429 ret = 0;
430 }
431 break;
432 }
433 break;
434 }
435 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
436 case PTRACE_CONT: /* restart after signal. */
437
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;
452
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;
474 }
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;
482
483 /*
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.
487 */
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;
498
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;
510
511 case PTRACE_DETACH:
512 /* detach a process that was attached. */
513 ret = ptrace_detach(child, data);
514 break;
515
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;
521 }
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);
526 }
527 break;
528 }
529
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;
536 }
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);
542 }
543 break;
544 }
545
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;
551 }
552 ret = get_fpregs((struct user_i387_struct __user *)data, child);
553 break;
554 }
555
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;
561 }
562 set_stopped_child_used_math(child);
563 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
564 break;
565 }
566
567 default:
568 ret = ptrace_request(child, request, addr, data);
569 break;
570 }
571 return ret;
572 }
573
574 static void syscall_trace(struct pt_regs *regs)
575 {
576
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
583
584 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
585 ? 0x80 : 0));
586 /*
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
590 */
591 if (current->exit_code) {
592 send_sig(current->exit_code, current, 1);
593 current->exit_code = 0;
594 }
595 }
596
597 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
598 {
599 /* do the secure computing check first */
600 secure_computing(regs->orig_rax);
601
602 if (test_thread_flag(TIF_SYSCALL_TRACE)
603 && (current->ptrace & PT_PTRACED))
604 syscall_trace(regs);
605
606 if (unlikely(current->audit_context)) {
607 if (test_thread_flag(TIF_IA32)) {
608 audit_syscall_entry(AUDIT_ARCH_I386,
609 regs->orig_rax,
610 regs->rbx, regs->rcx,
611 regs->rdx, regs->rsi);
612 } else {
613 audit_syscall_entry(AUDIT_ARCH_X86_64,
614 regs->orig_rax,
615 regs->rdi, regs->rsi,
616 regs->rdx, regs->r10);
617 }
618 }
619 }
620
621 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
622 {
623 if (unlikely(current->audit_context))
624 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
625
626 if ((test_thread_flag(TIF_SYSCALL_TRACE)
627 || test_thread_flag(TIF_SINGLESTEP))
628 && (current->ptrace & PT_PTRACED))
629 syscall_trace(regs);
630 }
This page took 0.04406 seconds and 5 git commands to generate.