1 /* ptrace.c: Sparc process tracing support.
3 * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
4 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
9 * Added Linux support -miguel (weird, eh?, the original code was meant
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/security.h>
22 #include <linux/seccomp.h>
23 #include <linux/audit.h>
24 #include <linux/signal.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/psrcompat.h>
31 #include <asm/visasm.h>
32 #include <asm/spitfire.h>
35 /* Returning from ptrace is a bit tricky because the syscall return
36 * low level code assumes any value returned which is negative and
37 * is a valid errno will mean setting the condition codes to indicate
38 * an error return. This doesn't work, so we have this hook.
40 static inline void pt_error_return(struct pt_regs *regs, unsigned long error)
42 regs->u_regs[UREG_I0] = error;
43 regs->tstate |= (TSTATE_ICARRY | TSTATE_XCARRY);
44 regs->tpc = regs->tnpc;
48 static inline void pt_succ_return(struct pt_regs *regs, unsigned long value)
50 regs->u_regs[UREG_I0] = value;
51 regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
52 regs->tpc = regs->tnpc;
57 pt_succ_return_linux(struct pt_regs *regs, unsigned long value, void __user *addr)
59 if (test_thread_flag(TIF_32BIT)) {
60 if (put_user(value, (unsigned int __user *) addr)) {
61 pt_error_return(regs, EFAULT);
65 if (put_user(value, (long __user *) addr)) {
66 pt_error_return(regs, EFAULT);
70 regs->u_regs[UREG_I0] = 0;
71 regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
72 regs->tpc = regs->tnpc;
77 pt_os_succ_return (struct pt_regs *regs, unsigned long val, void __user *addr)
79 if (current->personality == PER_SUNOS)
80 pt_succ_return (regs, val);
82 pt_succ_return_linux (regs, val, addr);
85 /* #define ALLOW_INIT_TRACING */
86 /* #define DEBUG_PTRACE */
90 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
91 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
92 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
93 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
94 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
95 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
96 /* 24 */ "SYSCALL", ""
101 * Called by kernel/ptrace.c when detaching..
103 * Make sure single step bits etc are not set.
105 void ptrace_disable(struct task_struct *child)
110 /* To get the necessary page struct, access_process_vm() first calls
111 * get_user_pages(). This has done a flush_dcache_page() on the
112 * accessed page. Then our caller (copy_{to,from}_user_page()) did
113 * to memcpy to read/write the data from that page.
115 * Now, the only thing we have to do is:
116 * 1) flush the D-cache if it's possible than an illegal alias
118 * 2) flush the I-cache if this is pre-cheetah and we did a write
120 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
121 unsigned long uaddr, void *kaddr,
122 unsigned long len, int write)
124 BUG_ON(len > PAGE_SIZE);
126 #ifdef DCACHE_ALIASING_POSSIBLE
127 /* If bit 13 of the kernel address we used to access the
128 * user page is the same as the virtual address that page
129 * is mapped to in the user's address space, we can skip the
132 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
133 unsigned long start = __pa(kaddr);
134 unsigned long end = start + len;
136 if (tlb_type == spitfire) {
137 for (; start < end; start += 32)
138 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
140 for (; start < end; start += 32)
141 __asm__ __volatile__(
142 "stxa %%g0, [%0] %1\n\t"
146 "i" (ASI_DCACHE_INVALIDATE));
150 if (write && tlb_type == spitfire) {
151 unsigned long start = (unsigned long) kaddr;
152 unsigned long end = start + len;
154 for (; start < end; start += 32)
159 asmlinkage void do_ptrace(struct pt_regs *regs)
161 int request = regs->u_regs[UREG_I0];
162 pid_t pid = regs->u_regs[UREG_I1];
163 unsigned long addr = regs->u_regs[UREG_I2];
164 unsigned long data = regs->u_regs[UREG_I3];
165 unsigned long addr2 = regs->u_regs[UREG_I4];
166 struct task_struct *child;
169 if (test_thread_flag(TIF_32BIT)) {
170 addr &= 0xffffffffUL;
171 data &= 0xffffffffUL;
172 addr2 &= 0xffffffffUL;
179 if ((request >= 0) && (request <= 24))
184 if (request == PTRACE_POKEDATA && data == 0x91d02001){
185 printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
188 printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
189 s, request, pid, addr, data, addr2);
192 if (request == PTRACE_TRACEME) {
195 /* are we already being traced? */
196 if (current->ptrace & PT_PTRACED) {
197 pt_error_return(regs, EPERM);
200 ret = security_ptrace(current->parent, current);
202 pt_error_return(regs, -ret);
206 /* set the ptrace bit in the process flags. */
207 current->ptrace |= PT_PTRACED;
208 pt_succ_return(regs, 0);
211 #ifndef ALLOW_INIT_TRACING
213 /* Can't dork with init. */
214 pt_error_return(regs, EPERM);
218 read_lock(&tasklist_lock);
219 child = find_task_by_pid(pid);
221 get_task_struct(child);
222 read_unlock(&tasklist_lock);
225 pt_error_return(regs, ESRCH);
229 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
230 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
231 if (ptrace_attach(child)) {
232 pt_error_return(regs, EPERM);
235 pt_succ_return(regs, 0);
239 ret = ptrace_check_attach(child, request == PTRACE_KILL);
241 pt_error_return(regs, -ret);
245 if (!(test_thread_flag(TIF_32BIT)) &&
246 ((request == PTRACE_READDATA64) ||
247 (request == PTRACE_WRITEDATA64) ||
248 (request == PTRACE_READTEXT64) ||
249 (request == PTRACE_WRITETEXT64) ||
250 (request == PTRACE_PEEKTEXT64) ||
251 (request == PTRACE_POKETEXT64) ||
252 (request == PTRACE_PEEKDATA64) ||
253 (request == PTRACE_POKEDATA64))) {
254 addr = regs->u_regs[UREG_G2];
255 addr2 = regs->u_regs[UREG_G3];
256 request -= 30; /* wheee... */
260 case PTRACE_PEEKTEXT: /* read word at location addr. */
261 case PTRACE_PEEKDATA: {
267 if (test_thread_flag(TIF_32BIT)) {
268 copied = access_process_vm(child, addr,
269 &tmp32, sizeof(tmp32), 0);
270 tmp64 = (unsigned long) tmp32;
271 if (copied == sizeof(tmp32))
274 copied = access_process_vm(child, addr,
275 &tmp64, sizeof(tmp64), 0);
276 if (copied == sizeof(tmp64))
280 pt_error_return(regs, -res);
282 pt_os_succ_return(regs, tmp64, (void __user *) data);
286 case PTRACE_POKETEXT: /* write the word at location addr. */
287 case PTRACE_POKEDATA: {
290 int copied, res = -EIO;
292 if (test_thread_flag(TIF_32BIT)) {
294 copied = access_process_vm(child, addr,
295 &tmp32, sizeof(tmp32), 1);
296 if (copied == sizeof(tmp32))
300 copied = access_process_vm(child, addr,
301 &tmp64, sizeof(tmp64), 1);
302 if (copied == sizeof(tmp64))
306 pt_error_return(regs, -res);
308 pt_succ_return(regs, res);
312 case PTRACE_GETREGS: {
313 struct pt_regs32 __user *pregs =
314 (struct pt_regs32 __user *) addr;
315 struct pt_regs *cregs = child->thread_info->kregs;
318 if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) ||
319 __put_user(cregs->tpc, (&pregs->pc)) ||
320 __put_user(cregs->tnpc, (&pregs->npc)) ||
321 __put_user(cregs->y, (&pregs->y))) {
322 pt_error_return(regs, EFAULT);
325 for (rval = 1; rval < 16; rval++)
326 if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
327 pt_error_return(regs, EFAULT);
330 pt_succ_return(regs, 0);
332 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
337 case PTRACE_GETREGS64: {
338 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
339 struct pt_regs *cregs = child->thread_info->kregs;
340 unsigned long tpc = cregs->tpc;
343 if ((child->thread_info->flags & _TIF_32BIT) != 0)
345 if (__put_user(cregs->tstate, (&pregs->tstate)) ||
346 __put_user(tpc, (&pregs->tpc)) ||
347 __put_user(cregs->tnpc, (&pregs->tnpc)) ||
348 __put_user(cregs->y, (&pregs->y))) {
349 pt_error_return(regs, EFAULT);
352 for (rval = 1; rval < 16; rval++)
353 if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
354 pt_error_return(regs, EFAULT);
357 pt_succ_return(regs, 0);
359 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
364 case PTRACE_SETREGS: {
365 struct pt_regs32 __user *pregs =
366 (struct pt_regs32 __user *) addr;
367 struct pt_regs *cregs = child->thread_info->kregs;
368 unsigned int psr, pc, npc, y;
371 /* Must be careful, tracing process can only set certain
374 if (__get_user(psr, (&pregs->psr)) ||
375 __get_user(pc, (&pregs->pc)) ||
376 __get_user(npc, (&pregs->npc)) ||
377 __get_user(y, (&pregs->y))) {
378 pt_error_return(regs, EFAULT);
381 cregs->tstate &= ~(TSTATE_ICC);
382 cregs->tstate |= psr_to_tstate_icc(psr);
383 if (!((pc | npc) & 3)) {
388 for (i = 1; i < 16; i++) {
389 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
390 pt_error_return(regs, EFAULT);
394 pt_succ_return(regs, 0);
398 case PTRACE_SETREGS64: {
399 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
400 struct pt_regs *cregs = child->thread_info->kregs;
401 unsigned long tstate, tpc, tnpc, y;
404 /* Must be careful, tracing process can only set certain
407 if (__get_user(tstate, (&pregs->tstate)) ||
408 __get_user(tpc, (&pregs->tpc)) ||
409 __get_user(tnpc, (&pregs->tnpc)) ||
410 __get_user(y, (&pregs->y))) {
411 pt_error_return(regs, EFAULT);
414 if ((child->thread_info->flags & _TIF_32BIT) != 0) {
418 tstate &= (TSTATE_ICC | TSTATE_XCC);
419 cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
420 cregs->tstate |= tstate;
421 if (!((tpc | tnpc) & 3)) {
426 for (i = 1; i < 16; i++) {
427 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
428 pt_error_return(regs, EFAULT);
432 pt_succ_return(regs, 0);
436 case PTRACE_GETFPREGS: {
438 unsigned int regs[32];
444 unsigned int insnaddr;
448 struct fps __user *fps = (struct fps __user *) addr;
449 unsigned long *fpregs = child->thread_info->fpregs;
451 if (copy_to_user(&fps->regs[0], fpregs,
452 (32 * sizeof(unsigned int))) ||
453 __put_user(child->thread_info->xfsr[0], (&fps->fsr)) ||
454 __put_user(0, (&fps->fpqd)) ||
455 __put_user(0, (&fps->flags)) ||
456 __put_user(0, (&fps->extra)) ||
457 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int))) {
458 pt_error_return(regs, EFAULT);
461 pt_succ_return(regs, 0);
465 case PTRACE_GETFPREGS64: {
467 unsigned int regs[64];
470 struct fps __user *fps = (struct fps __user *) addr;
471 unsigned long *fpregs = child->thread_info->fpregs;
473 if (copy_to_user(&fps->regs[0], fpregs,
474 (64 * sizeof(unsigned int))) ||
475 __put_user(child->thread_info->xfsr[0], (&fps->fsr))) {
476 pt_error_return(regs, EFAULT);
479 pt_succ_return(regs, 0);
483 case PTRACE_SETFPREGS: {
485 unsigned int regs[32];
491 unsigned int insnaddr;
495 struct fps __user *fps = (struct fps __user *) addr;
496 unsigned long *fpregs = child->thread_info->fpregs;
499 if (copy_from_user(fpregs, &fps->regs[0],
500 (32 * sizeof(unsigned int))) ||
501 __get_user(fsr, (&fps->fsr))) {
502 pt_error_return(regs, EFAULT);
505 child->thread_info->xfsr[0] &= 0xffffffff00000000UL;
506 child->thread_info->xfsr[0] |= fsr;
507 if (!(child->thread_info->fpsaved[0] & FPRS_FEF))
508 child->thread_info->gsr[0] = 0;
509 child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL);
510 pt_succ_return(regs, 0);
514 case PTRACE_SETFPREGS64: {
516 unsigned int regs[64];
519 struct fps __user *fps = (struct fps __user *) addr;
520 unsigned long *fpregs = child->thread_info->fpregs;
522 if (copy_from_user(fpregs, &fps->regs[0],
523 (64 * sizeof(unsigned int))) ||
524 __get_user(child->thread_info->xfsr[0], (&fps->fsr))) {
525 pt_error_return(regs, EFAULT);
528 if (!(child->thread_info->fpsaved[0] & FPRS_FEF))
529 child->thread_info->gsr[0] = 0;
530 child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL | FPRS_DU);
531 pt_succ_return(regs, 0);
535 case PTRACE_READTEXT:
536 case PTRACE_READDATA: {
537 int res = ptrace_readdata(child, addr,
538 (char __user *)addr2, data);
540 pt_succ_return(regs, 0);
545 pt_error_return(regs, -res);
549 case PTRACE_WRITETEXT:
550 case PTRACE_WRITEDATA: {
551 int res = ptrace_writedata(child, (char __user *) addr2,
554 pt_succ_return(regs, 0);
559 pt_error_return(regs, -res);
562 case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
565 case PTRACE_CONT: { /* restart after signal. */
566 if (!valid_signal(data)) {
567 pt_error_return(regs, EIO);
571 if (request == PTRACE_SYSCALL) {
572 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
574 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
577 child->exit_code = data;
579 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child->comm,
580 child->pid, child->exit_code,
581 child->thread_info->kregs->tpc,
582 child->thread_info->kregs->tnpc);
585 wake_up_process(child);
586 pt_succ_return(regs, 0);
591 * make the child exit. Best I can do is send it a sigkill.
592 * perhaps it should be put in the status that it wants to
596 if (child->exit_state == EXIT_ZOMBIE) { /* already dead */
597 pt_succ_return(regs, 0);
600 child->exit_code = SIGKILL;
601 wake_up_process(child);
602 pt_succ_return(regs, 0);
606 case PTRACE_SUNDETACH: { /* detach a process that was attached. */
607 int error = ptrace_detach(child, data);
609 pt_error_return(regs, EIO);
612 pt_succ_return(regs, 0);
616 /* PTRACE_DUMPCORE unsupported... */
619 int err = ptrace_request(child, request, addr, data);
621 pt_error_return(regs, -err);
623 pt_succ_return(regs, 0);
629 put_task_struct(child);
634 asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
636 /* do the secure computing check first */
637 secure_computing(regs->u_regs[UREG_G1]);
639 if (unlikely(current->audit_context) && syscall_exit_p) {
640 unsigned long tstate = regs->tstate;
641 int result = AUDITSC_SUCCESS;
643 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
644 result = AUDITSC_FAILURE;
646 audit_syscall_exit(current, result, regs->u_regs[UREG_I0]);
649 if (!(current->ptrace & PT_PTRACED))
652 if (!test_thread_flag(TIF_SYSCALL_TRACE))
655 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
659 * this isn't the same as continuing with a signal, but it will do
660 * for normal use. strace only continues with a signal if the
661 * stopping signal is not SIGTRAP. -brl
663 if (current->exit_code) {
664 send_sig(current->exit_code, current, 1);
665 current->exit_code = 0;
669 if (unlikely(current->audit_context) && !syscall_exit_p)
670 audit_syscall_entry(current,
671 (test_thread_flag(TIF_32BIT) ?
674 regs->u_regs[UREG_G1],
675 regs->u_regs[UREG_I0],
676 regs->u_regs[UREG_I1],
677 regs->u_regs[UREG_I2],
678 regs->u_regs[UREG_I3]);