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/signal.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/psrcompat.h>
29 #include <asm/visasm.h>
30 #include <asm/spitfire.h>
32 /* Returning from ptrace is a bit tricky because the syscall return
33 * low level code assumes any value returned which is negative and
34 * is a valid errno will mean setting the condition codes to indicate
35 * an error return. This doesn't work, so we have this hook.
37 static inline void pt_error_return(struct pt_regs
*regs
, unsigned long error
)
39 regs
->u_regs
[UREG_I0
] = error
;
40 regs
->tstate
|= (TSTATE_ICARRY
| TSTATE_XCARRY
);
41 regs
->tpc
= regs
->tnpc
;
45 static inline void pt_succ_return(struct pt_regs
*regs
, unsigned long value
)
47 regs
->u_regs
[UREG_I0
] = value
;
48 regs
->tstate
&= ~(TSTATE_ICARRY
| TSTATE_XCARRY
);
49 regs
->tpc
= regs
->tnpc
;
54 pt_succ_return_linux(struct pt_regs
*regs
, unsigned long value
, void __user
*addr
)
56 if (test_thread_flag(TIF_32BIT
)) {
57 if (put_user(value
, (unsigned int __user
*) addr
)) {
58 pt_error_return(regs
, EFAULT
);
62 if (put_user(value
, (long __user
*) addr
)) {
63 pt_error_return(regs
, EFAULT
);
67 regs
->u_regs
[UREG_I0
] = 0;
68 regs
->tstate
&= ~(TSTATE_ICARRY
| TSTATE_XCARRY
);
69 regs
->tpc
= regs
->tnpc
;
74 pt_os_succ_return (struct pt_regs
*regs
, unsigned long val
, void __user
*addr
)
76 if (current
->personality
== PER_SUNOS
)
77 pt_succ_return (regs
, val
);
79 pt_succ_return_linux (regs
, val
, addr
);
82 /* #define ALLOW_INIT_TRACING */
83 /* #define DEBUG_PTRACE */
87 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
88 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
89 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
90 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
91 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
92 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
93 /* 24 */ "SYSCALL", ""
98 * Called by kernel/ptrace.c when detaching..
100 * Make sure single step bits etc are not set.
102 void ptrace_disable(struct task_struct
*child
)
107 /* To get the necessary page struct, access_process_vm() first calls
108 * get_user_pages(). This has done a flush_dcache_page() on the
109 * accessed page. Then our caller (copy_{to,from}_user_page()) did
110 * to memcpy to read/write the data from that page.
112 * Now, the only thing we have to do is:
113 * 1) flush the D-cache if it's possible than an illegal alias
115 * 2) flush the I-cache if this is pre-cheetah and we did a write
117 void flush_ptrace_access(struct vm_area_struct
*vma
, struct page
*page
,
118 unsigned long uaddr
, void *kaddr
,
119 unsigned long len
, int write
)
121 BUG_ON(len
> PAGE_SIZE
);
123 #ifdef DCACHE_ALIASING_POSSIBLE
124 /* If bit 13 of the kernel address we used to access the
125 * user page is the same as the virtual address that page
126 * is mapped to in the user's address space, we can skip the
129 if ((uaddr
^ kaddr
) & (1UL << 13)) {
130 unsigned long start
= __pa(kaddr
);
131 unsigned long end
= start
+ len
;
133 if (tlb_type
== spitfire
) {
134 for (; start
< end
; start
+= 32)
135 spitfire_put_dcache_tag(va
& 0x3fe0, 0x0);
137 for (; start
< end
; start
+= 32)
138 __asm__
__volatile__(
139 "stxa %%g0, [%0] %1\n\t"
143 "i" (ASI_DCACHE_INVALIDATE
));
147 if (write
&& tlb_type
== spitfire
) {
148 unsigned long start
= (unsigned long) kaddr
;
149 unsigned long end
= start
+ len
;
151 for (; start
< end
; start
+= 32)
156 asmlinkage
void do_ptrace(struct pt_regs
*regs
)
158 int request
= regs
->u_regs
[UREG_I0
];
159 pid_t pid
= regs
->u_regs
[UREG_I1
];
160 unsigned long addr
= regs
->u_regs
[UREG_I2
];
161 unsigned long data
= regs
->u_regs
[UREG_I3
];
162 unsigned long addr2
= regs
->u_regs
[UREG_I4
];
163 struct task_struct
*child
;
166 if (test_thread_flag(TIF_32BIT
)) {
167 addr
&= 0xffffffffUL
;
168 data
&= 0xffffffffUL
;
169 addr2
&= 0xffffffffUL
;
176 if ((request
>= 0) && (request
<= 24))
181 if (request
== PTRACE_POKEDATA
&& data
== 0x91d02001){
182 printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
185 printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
186 s
, request
, pid
, addr
, data
, addr2
);
189 if (request
== PTRACE_TRACEME
) {
192 /* are we already being traced? */
193 if (current
->ptrace
& PT_PTRACED
) {
194 pt_error_return(regs
, EPERM
);
197 ret
= security_ptrace(current
->parent
, current
);
199 pt_error_return(regs
, -ret
);
203 /* set the ptrace bit in the process flags. */
204 current
->ptrace
|= PT_PTRACED
;
205 pt_succ_return(regs
, 0);
208 #ifndef ALLOW_INIT_TRACING
210 /* Can't dork with init. */
211 pt_error_return(regs
, EPERM
);
215 read_lock(&tasklist_lock
);
216 child
= find_task_by_pid(pid
);
218 get_task_struct(child
);
219 read_unlock(&tasklist_lock
);
222 pt_error_return(regs
, ESRCH
);
226 if ((current
->personality
== PER_SUNOS
&& request
== PTRACE_SUNATTACH
)
227 || (current
->personality
!= PER_SUNOS
&& request
== PTRACE_ATTACH
)) {
228 if (ptrace_attach(child
)) {
229 pt_error_return(regs
, EPERM
);
232 pt_succ_return(regs
, 0);
236 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
238 pt_error_return(regs
, -ret
);
242 if (!(test_thread_flag(TIF_32BIT
)) &&
243 ((request
== PTRACE_READDATA64
) ||
244 (request
== PTRACE_WRITEDATA64
) ||
245 (request
== PTRACE_READTEXT64
) ||
246 (request
== PTRACE_WRITETEXT64
) ||
247 (request
== PTRACE_PEEKTEXT64
) ||
248 (request
== PTRACE_POKETEXT64
) ||
249 (request
== PTRACE_PEEKDATA64
) ||
250 (request
== PTRACE_POKEDATA64
))) {
251 addr
= regs
->u_regs
[UREG_G2
];
252 addr2
= regs
->u_regs
[UREG_G3
];
253 request
-= 30; /* wheee... */
257 case PTRACE_PEEKTEXT
: /* read word at location addr. */
258 case PTRACE_PEEKDATA
: {
264 if (test_thread_flag(TIF_32BIT
)) {
265 copied
= access_process_vm(child
, addr
,
266 &tmp32
, sizeof(tmp32
), 0);
267 tmp64
= (unsigned long) tmp32
;
268 if (copied
== sizeof(tmp32
))
271 copied
= access_process_vm(child
, addr
,
272 &tmp64
, sizeof(tmp64
), 0);
273 if (copied
== sizeof(tmp64
))
277 pt_error_return(regs
, -res
);
279 pt_os_succ_return(regs
, tmp64
, (void __user
*) data
);
283 case PTRACE_POKETEXT
: /* write the word at location addr. */
284 case PTRACE_POKEDATA
: {
287 int copied
, res
= -EIO
;
289 if (test_thread_flag(TIF_32BIT
)) {
291 copied
= access_process_vm(child
, addr
,
292 &tmp32
, sizeof(tmp32
), 1);
293 if (copied
== sizeof(tmp32
))
297 copied
= access_process_vm(child
, addr
,
298 &tmp64
, sizeof(tmp64
), 1);
299 if (copied
== sizeof(tmp64
))
303 pt_error_return(regs
, -res
);
305 pt_succ_return(regs
, res
);
309 case PTRACE_GETREGS
: {
310 struct pt_regs32 __user
*pregs
=
311 (struct pt_regs32 __user
*) addr
;
312 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
315 if (__put_user(tstate_to_psr(cregs
->tstate
), (&pregs
->psr
)) ||
316 __put_user(cregs
->tpc
, (&pregs
->pc
)) ||
317 __put_user(cregs
->tnpc
, (&pregs
->npc
)) ||
318 __put_user(cregs
->y
, (&pregs
->y
))) {
319 pt_error_return(regs
, EFAULT
);
322 for (rval
= 1; rval
< 16; rval
++)
323 if (__put_user(cregs
->u_regs
[rval
], (&pregs
->u_regs
[rval
- 1]))) {
324 pt_error_return(regs
, EFAULT
);
327 pt_succ_return(regs
, 0);
329 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs
->tpc
, cregs
->tnpc
, cregs
->u_regs
[15]);
334 case PTRACE_GETREGS64
: {
335 struct pt_regs __user
*pregs
= (struct pt_regs __user
*) addr
;
336 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
337 unsigned long tpc
= cregs
->tpc
;
340 if ((child
->thread_info
->flags
& _TIF_32BIT
) != 0)
342 if (__put_user(cregs
->tstate
, (&pregs
->tstate
)) ||
343 __put_user(tpc
, (&pregs
->tpc
)) ||
344 __put_user(cregs
->tnpc
, (&pregs
->tnpc
)) ||
345 __put_user(cregs
->y
, (&pregs
->y
))) {
346 pt_error_return(regs
, EFAULT
);
349 for (rval
= 1; rval
< 16; rval
++)
350 if (__put_user(cregs
->u_regs
[rval
], (&pregs
->u_regs
[rval
- 1]))) {
351 pt_error_return(regs
, EFAULT
);
354 pt_succ_return(regs
, 0);
356 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs
->tpc
, cregs
->tnpc
, cregs
->u_regs
[15]);
361 case PTRACE_SETREGS
: {
362 struct pt_regs32 __user
*pregs
=
363 (struct pt_regs32 __user
*) addr
;
364 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
365 unsigned int psr
, pc
, npc
, y
;
368 /* Must be careful, tracing process can only set certain
371 if (__get_user(psr
, (&pregs
->psr
)) ||
372 __get_user(pc
, (&pregs
->pc
)) ||
373 __get_user(npc
, (&pregs
->npc
)) ||
374 __get_user(y
, (&pregs
->y
))) {
375 pt_error_return(regs
, EFAULT
);
378 cregs
->tstate
&= ~(TSTATE_ICC
);
379 cregs
->tstate
|= psr_to_tstate_icc(psr
);
380 if (!((pc
| npc
) & 3)) {
385 for (i
= 1; i
< 16; i
++) {
386 if (__get_user(cregs
->u_regs
[i
], (&pregs
->u_regs
[i
-1]))) {
387 pt_error_return(regs
, EFAULT
);
391 pt_succ_return(regs
, 0);
395 case PTRACE_SETREGS64
: {
396 struct pt_regs __user
*pregs
= (struct pt_regs __user
*) addr
;
397 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
398 unsigned long tstate
, tpc
, tnpc
, y
;
401 /* Must be careful, tracing process can only set certain
404 if (__get_user(tstate
, (&pregs
->tstate
)) ||
405 __get_user(tpc
, (&pregs
->tpc
)) ||
406 __get_user(tnpc
, (&pregs
->tnpc
)) ||
407 __get_user(y
, (&pregs
->y
))) {
408 pt_error_return(regs
, EFAULT
);
411 if ((child
->thread_info
->flags
& _TIF_32BIT
) != 0) {
415 tstate
&= (TSTATE_ICC
| TSTATE_XCC
);
416 cregs
->tstate
&= ~(TSTATE_ICC
| TSTATE_XCC
);
417 cregs
->tstate
|= tstate
;
418 if (!((tpc
| tnpc
) & 3)) {
423 for (i
= 1; i
< 16; i
++) {
424 if (__get_user(cregs
->u_regs
[i
], (&pregs
->u_regs
[i
-1]))) {
425 pt_error_return(regs
, EFAULT
);
429 pt_succ_return(regs
, 0);
433 case PTRACE_GETFPREGS
: {
435 unsigned int regs
[32];
441 unsigned int insnaddr
;
445 struct fps __user
*fps
= (struct fps __user
*) addr
;
446 unsigned long *fpregs
= child
->thread_info
->fpregs
;
448 if (copy_to_user(&fps
->regs
[0], fpregs
,
449 (32 * sizeof(unsigned int))) ||
450 __put_user(child
->thread_info
->xfsr
[0], (&fps
->fsr
)) ||
451 __put_user(0, (&fps
->fpqd
)) ||
452 __put_user(0, (&fps
->flags
)) ||
453 __put_user(0, (&fps
->extra
)) ||
454 clear_user(&fps
->fpq
[0], 32 * sizeof(unsigned int))) {
455 pt_error_return(regs
, EFAULT
);
458 pt_succ_return(regs
, 0);
462 case PTRACE_GETFPREGS64
: {
464 unsigned int regs
[64];
467 struct fps __user
*fps
= (struct fps __user
*) addr
;
468 unsigned long *fpregs
= child
->thread_info
->fpregs
;
470 if (copy_to_user(&fps
->regs
[0], fpregs
,
471 (64 * sizeof(unsigned int))) ||
472 __put_user(child
->thread_info
->xfsr
[0], (&fps
->fsr
))) {
473 pt_error_return(regs
, EFAULT
);
476 pt_succ_return(regs
, 0);
480 case PTRACE_SETFPREGS
: {
482 unsigned int regs
[32];
488 unsigned int insnaddr
;
492 struct fps __user
*fps
= (struct fps __user
*) addr
;
493 unsigned long *fpregs
= child
->thread_info
->fpregs
;
496 if (copy_from_user(fpregs
, &fps
->regs
[0],
497 (32 * sizeof(unsigned int))) ||
498 __get_user(fsr
, (&fps
->fsr
))) {
499 pt_error_return(regs
, EFAULT
);
502 child
->thread_info
->xfsr
[0] &= 0xffffffff00000000UL
;
503 child
->thread_info
->xfsr
[0] |= fsr
;
504 if (!(child
->thread_info
->fpsaved
[0] & FPRS_FEF
))
505 child
->thread_info
->gsr
[0] = 0;
506 child
->thread_info
->fpsaved
[0] |= (FPRS_FEF
| FPRS_DL
);
507 pt_succ_return(regs
, 0);
511 case PTRACE_SETFPREGS64
: {
513 unsigned int regs
[64];
516 struct fps __user
*fps
= (struct fps __user
*) addr
;
517 unsigned long *fpregs
= child
->thread_info
->fpregs
;
519 if (copy_from_user(fpregs
, &fps
->regs
[0],
520 (64 * sizeof(unsigned int))) ||
521 __get_user(child
->thread_info
->xfsr
[0], (&fps
->fsr
))) {
522 pt_error_return(regs
, EFAULT
);
525 if (!(child
->thread_info
->fpsaved
[0] & FPRS_FEF
))
526 child
->thread_info
->gsr
[0] = 0;
527 child
->thread_info
->fpsaved
[0] |= (FPRS_FEF
| FPRS_DL
| FPRS_DU
);
528 pt_succ_return(regs
, 0);
532 case PTRACE_READTEXT
:
533 case PTRACE_READDATA
: {
534 int res
= ptrace_readdata(child
, addr
,
535 (char __user
*)addr2
, data
);
537 pt_succ_return(regs
, 0);
542 pt_error_return(regs
, -res
);
546 case PTRACE_WRITETEXT
:
547 case PTRACE_WRITEDATA
: {
548 int res
= ptrace_writedata(child
, (char __user
*) addr2
,
551 pt_succ_return(regs
, 0);
556 pt_error_return(regs
, -res
);
559 case PTRACE_SYSCALL
: /* continue and stop at (return from) syscall */
562 case PTRACE_CONT
: { /* restart after signal. */
563 if (!valid_signal(data
)) {
564 pt_error_return(regs
, EIO
);
568 if (request
== PTRACE_SYSCALL
) {
569 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
571 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
574 child
->exit_code
= data
;
576 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child
->comm
,
577 child
->pid
, child
->exit_code
,
578 child
->thread_info
->kregs
->tpc
,
579 child
->thread_info
->kregs
->tnpc
);
582 wake_up_process(child
);
583 pt_succ_return(regs
, 0);
588 * make the child exit. Best I can do is send it a sigkill.
589 * perhaps it should be put in the status that it wants to
593 if (child
->exit_state
== EXIT_ZOMBIE
) { /* already dead */
594 pt_succ_return(regs
, 0);
597 child
->exit_code
= SIGKILL
;
598 wake_up_process(child
);
599 pt_succ_return(regs
, 0);
603 case PTRACE_SUNDETACH
: { /* detach a process that was attached. */
604 int error
= ptrace_detach(child
, data
);
606 pt_error_return(regs
, EIO
);
609 pt_succ_return(regs
, 0);
613 /* PTRACE_DUMPCORE unsupported... */
616 int err
= ptrace_request(child
, request
, addr
, data
);
618 pt_error_return(regs
, -err
);
620 pt_succ_return(regs
, 0);
626 put_task_struct(child
);
631 asmlinkage
void syscall_trace(void)
634 printk("%s [%d]: syscall_trace\n", current
->comm
, current
->pid
);
636 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
638 if (!(current
->ptrace
& PT_PTRACED
))
640 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
644 * this isn't the same as continuing with a signal, but it will do
645 * for normal use. strace only continues with a signal if the
646 * stopping signal is not SIGTRAP. -brl
649 printk("%s [%d]: syscall_trace exit= %x\n", current
->comm
,
650 current
->pid
, current
->exit_code
);
652 if (current
->exit_code
) {
653 send_sig (current
->exit_code
, current
, 1);
654 current
->exit_code
= 0;