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>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/psrcompat.h>
28 #include <asm/visasm.h>
29 #include <asm/spitfire.h>
31 /* Returning from ptrace is a bit tricky because the syscall return
32 * low level code assumes any value returned which is negative and
33 * is a valid errno will mean setting the condition codes to indicate
34 * an error return. This doesn't work, so we have this hook.
36 static inline void pt_error_return(struct pt_regs
*regs
, unsigned long error
)
38 regs
->u_regs
[UREG_I0
] = error
;
39 regs
->tstate
|= (TSTATE_ICARRY
| TSTATE_XCARRY
);
40 regs
->tpc
= regs
->tnpc
;
44 static inline void pt_succ_return(struct pt_regs
*regs
, unsigned long value
)
46 regs
->u_regs
[UREG_I0
] = value
;
47 regs
->tstate
&= ~(TSTATE_ICARRY
| TSTATE_XCARRY
);
48 regs
->tpc
= regs
->tnpc
;
53 pt_succ_return_linux(struct pt_regs
*regs
, unsigned long value
, void __user
*addr
)
55 if (test_thread_flag(TIF_32BIT
)) {
56 if (put_user(value
, (unsigned int __user
*) addr
)) {
57 pt_error_return(regs
, EFAULT
);
61 if (put_user(value
, (long __user
*) addr
)) {
62 pt_error_return(regs
, EFAULT
);
66 regs
->u_regs
[UREG_I0
] = 0;
67 regs
->tstate
&= ~(TSTATE_ICARRY
| TSTATE_XCARRY
);
68 regs
->tpc
= regs
->tnpc
;
73 pt_os_succ_return (struct pt_regs
*regs
, unsigned long val
, void __user
*addr
)
75 if (current
->personality
== PER_SUNOS
)
76 pt_succ_return (regs
, val
);
78 pt_succ_return_linux (regs
, val
, addr
);
81 /* #define ALLOW_INIT_TRACING */
82 /* #define DEBUG_PTRACE */
86 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
87 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
88 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
89 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
90 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
91 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
92 /* 24 */ "SYSCALL", ""
97 * Called by kernel/ptrace.c when detaching..
99 * Make sure single step bits etc are not set.
101 void ptrace_disable(struct task_struct
*child
)
106 /* To get the necessary page struct, access_process_vm() first calls
107 * get_user_pages(). This has done a flush_dcache_page() on the
108 * accessed page. Then our caller (copy_{to,from}_user_page()) did
109 * to memcpy to read/write the data from that page.
111 * Now, the only thing we have to do is:
112 * 1) flush the D-cache if it's possible than an illegal alias
114 * 2) flush the I-cache if this is pre-cheetah and we did a write
116 void flush_ptrace_access(struct vm_area_struct
*vma
, struct page
*page
,
117 unsigned long uaddr
, void *kaddr
,
118 unsigned long len
, int write
)
120 BUG_ON(len
> PAGE_SIZE
);
122 #ifdef DCACHE_ALIASING_POSSIBLE
123 /* If bit 13 of the kernel address we used to access the
124 * user page is the same as the virtual address that page
125 * is mapped to in the user's address space, we can skip the
128 if ((uaddr
^ kaddr
) & (1UL << 13)) {
129 unsigned long start
= __pa(kaddr
);
130 unsigned long end
= start
+ len
;
132 if (tlb_type
== spitfire
) {
133 for (; start
< end
; start
+= 32)
134 spitfire_put_dcache_tag(va
& 0x3fe0, 0x0);
136 for (; start
< end
; start
+= 32)
137 __asm__
__volatile__(
138 "stxa %%g0, [%0] %1\n\t"
142 "i" (ASI_DCACHE_INVALIDATE
));
146 if (write
&& tlb_type
== spitfire
) {
147 unsigned long start
= (unsigned long) kaddr
;
148 unsigned long end
= start
+ len
;
150 for (; start
< end
; start
+= 32)
155 asmlinkage
void do_ptrace(struct pt_regs
*regs
)
157 int request
= regs
->u_regs
[UREG_I0
];
158 pid_t pid
= regs
->u_regs
[UREG_I1
];
159 unsigned long addr
= regs
->u_regs
[UREG_I2
];
160 unsigned long data
= regs
->u_regs
[UREG_I3
];
161 unsigned long addr2
= regs
->u_regs
[UREG_I4
];
162 struct task_struct
*child
;
165 if (test_thread_flag(TIF_32BIT
)) {
166 addr
&= 0xffffffffUL
;
167 data
&= 0xffffffffUL
;
168 addr2
&= 0xffffffffUL
;
175 if ((request
>= 0) && (request
<= 24))
180 if (request
== PTRACE_POKEDATA
&& data
== 0x91d02001){
181 printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
184 printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
185 s
, request
, pid
, addr
, data
, addr2
);
188 if (request
== PTRACE_TRACEME
) {
191 /* are we already being traced? */
192 if (current
->ptrace
& PT_PTRACED
) {
193 pt_error_return(regs
, EPERM
);
196 ret
= security_ptrace(current
->parent
, current
);
198 pt_error_return(regs
, -ret
);
202 /* set the ptrace bit in the process flags. */
203 current
->ptrace
|= PT_PTRACED
;
204 pt_succ_return(regs
, 0);
207 #ifndef ALLOW_INIT_TRACING
209 /* Can't dork with init. */
210 pt_error_return(regs
, EPERM
);
214 read_lock(&tasklist_lock
);
215 child
= find_task_by_pid(pid
);
217 get_task_struct(child
);
218 read_unlock(&tasklist_lock
);
221 pt_error_return(regs
, ESRCH
);
225 if ((current
->personality
== PER_SUNOS
&& request
== PTRACE_SUNATTACH
)
226 || (current
->personality
!= PER_SUNOS
&& request
== PTRACE_ATTACH
)) {
227 if (ptrace_attach(child
)) {
228 pt_error_return(regs
, EPERM
);
231 pt_succ_return(regs
, 0);
235 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
237 pt_error_return(regs
, -ret
);
241 if (!(test_thread_flag(TIF_32BIT
)) &&
242 ((request
== PTRACE_READDATA64
) ||
243 (request
== PTRACE_WRITEDATA64
) ||
244 (request
== PTRACE_READTEXT64
) ||
245 (request
== PTRACE_WRITETEXT64
) ||
246 (request
== PTRACE_PEEKTEXT64
) ||
247 (request
== PTRACE_POKETEXT64
) ||
248 (request
== PTRACE_PEEKDATA64
) ||
249 (request
== PTRACE_POKEDATA64
))) {
250 addr
= regs
->u_regs
[UREG_G2
];
251 addr2
= regs
->u_regs
[UREG_G3
];
252 request
-= 30; /* wheee... */
256 case PTRACE_PEEKTEXT
: /* read word at location addr. */
257 case PTRACE_PEEKDATA
: {
263 if (test_thread_flag(TIF_32BIT
)) {
264 copied
= access_process_vm(child
, addr
,
265 &tmp32
, sizeof(tmp32
), 0);
266 tmp64
= (unsigned long) tmp32
;
267 if (copied
== sizeof(tmp32
))
270 copied
= access_process_vm(child
, addr
,
271 &tmp64
, sizeof(tmp64
), 0);
272 if (copied
== sizeof(tmp64
))
276 pt_error_return(regs
, -res
);
278 pt_os_succ_return(regs
, tmp64
, (void __user
*) data
);
282 case PTRACE_POKETEXT
: /* write the word at location addr. */
283 case PTRACE_POKEDATA
: {
286 int copied
, res
= -EIO
;
288 if (test_thread_flag(TIF_32BIT
)) {
290 copied
= access_process_vm(child
, addr
,
291 &tmp32
, sizeof(tmp32
), 1);
292 if (copied
== sizeof(tmp32
))
296 copied
= access_process_vm(child
, addr
,
297 &tmp64
, sizeof(tmp64
), 1);
298 if (copied
== sizeof(tmp64
))
302 pt_error_return(regs
, -res
);
304 pt_succ_return(regs
, res
);
308 case PTRACE_GETREGS
: {
309 struct pt_regs32 __user
*pregs
=
310 (struct pt_regs32 __user
*) addr
;
311 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
314 if (__put_user(tstate_to_psr(cregs
->tstate
), (&pregs
->psr
)) ||
315 __put_user(cregs
->tpc
, (&pregs
->pc
)) ||
316 __put_user(cregs
->tnpc
, (&pregs
->npc
)) ||
317 __put_user(cregs
->y
, (&pregs
->y
))) {
318 pt_error_return(regs
, EFAULT
);
321 for (rval
= 1; rval
< 16; rval
++)
322 if (__put_user(cregs
->u_regs
[rval
], (&pregs
->u_regs
[rval
- 1]))) {
323 pt_error_return(regs
, EFAULT
);
326 pt_succ_return(regs
, 0);
328 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs
->tpc
, cregs
->tnpc
, cregs
->u_regs
[15]);
333 case PTRACE_GETREGS64
: {
334 struct pt_regs __user
*pregs
= (struct pt_regs __user
*) addr
;
335 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
336 unsigned long tpc
= cregs
->tpc
;
339 if ((child
->thread_info
->flags
& _TIF_32BIT
) != 0)
341 if (__put_user(cregs
->tstate
, (&pregs
->tstate
)) ||
342 __put_user(tpc
, (&pregs
->tpc
)) ||
343 __put_user(cregs
->tnpc
, (&pregs
->tnpc
)) ||
344 __put_user(cregs
->y
, (&pregs
->y
))) {
345 pt_error_return(regs
, EFAULT
);
348 for (rval
= 1; rval
< 16; rval
++)
349 if (__put_user(cregs
->u_regs
[rval
], (&pregs
->u_regs
[rval
- 1]))) {
350 pt_error_return(regs
, EFAULT
);
353 pt_succ_return(regs
, 0);
355 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs
->tpc
, cregs
->tnpc
, cregs
->u_regs
[15]);
360 case PTRACE_SETREGS
: {
361 struct pt_regs32 __user
*pregs
=
362 (struct pt_regs32 __user
*) addr
;
363 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
364 unsigned int psr
, pc
, npc
, y
;
367 /* Must be careful, tracing process can only set certain
370 if (__get_user(psr
, (&pregs
->psr
)) ||
371 __get_user(pc
, (&pregs
->pc
)) ||
372 __get_user(npc
, (&pregs
->npc
)) ||
373 __get_user(y
, (&pregs
->y
))) {
374 pt_error_return(regs
, EFAULT
);
377 cregs
->tstate
&= ~(TSTATE_ICC
);
378 cregs
->tstate
|= psr_to_tstate_icc(psr
);
379 if (!((pc
| npc
) & 3)) {
384 for (i
= 1; i
< 16; i
++) {
385 if (__get_user(cregs
->u_regs
[i
], (&pregs
->u_regs
[i
-1]))) {
386 pt_error_return(regs
, EFAULT
);
390 pt_succ_return(regs
, 0);
394 case PTRACE_SETREGS64
: {
395 struct pt_regs __user
*pregs
= (struct pt_regs __user
*) addr
;
396 struct pt_regs
*cregs
= child
->thread_info
->kregs
;
397 unsigned long tstate
, tpc
, tnpc
, y
;
400 /* Must be careful, tracing process can only set certain
403 if (__get_user(tstate
, (&pregs
->tstate
)) ||
404 __get_user(tpc
, (&pregs
->tpc
)) ||
405 __get_user(tnpc
, (&pregs
->tnpc
)) ||
406 __get_user(y
, (&pregs
->y
))) {
407 pt_error_return(regs
, EFAULT
);
410 if ((child
->thread_info
->flags
& _TIF_32BIT
) != 0) {
414 tstate
&= (TSTATE_ICC
| TSTATE_XCC
);
415 cregs
->tstate
&= ~(TSTATE_ICC
| TSTATE_XCC
);
416 cregs
->tstate
|= tstate
;
417 if (!((tpc
| tnpc
) & 3)) {
422 for (i
= 1; i
< 16; i
++) {
423 if (__get_user(cregs
->u_regs
[i
], (&pregs
->u_regs
[i
-1]))) {
424 pt_error_return(regs
, EFAULT
);
428 pt_succ_return(regs
, 0);
432 case PTRACE_GETFPREGS
: {
434 unsigned int regs
[32];
440 unsigned int insnaddr
;
444 struct fps __user
*fps
= (struct fps __user
*) addr
;
445 unsigned long *fpregs
= child
->thread_info
->fpregs
;
447 if (copy_to_user(&fps
->regs
[0], fpregs
,
448 (32 * sizeof(unsigned int))) ||
449 __put_user(child
->thread_info
->xfsr
[0], (&fps
->fsr
)) ||
450 __put_user(0, (&fps
->fpqd
)) ||
451 __put_user(0, (&fps
->flags
)) ||
452 __put_user(0, (&fps
->extra
)) ||
453 clear_user(&fps
->fpq
[0], 32 * sizeof(unsigned int))) {
454 pt_error_return(regs
, EFAULT
);
457 pt_succ_return(regs
, 0);
461 case PTRACE_GETFPREGS64
: {
463 unsigned int regs
[64];
466 struct fps __user
*fps
= (struct fps __user
*) addr
;
467 unsigned long *fpregs
= child
->thread_info
->fpregs
;
469 if (copy_to_user(&fps
->regs
[0], fpregs
,
470 (64 * sizeof(unsigned int))) ||
471 __put_user(child
->thread_info
->xfsr
[0], (&fps
->fsr
))) {
472 pt_error_return(regs
, EFAULT
);
475 pt_succ_return(regs
, 0);
479 case PTRACE_SETFPREGS
: {
481 unsigned int regs
[32];
487 unsigned int insnaddr
;
491 struct fps __user
*fps
= (struct fps __user
*) addr
;
492 unsigned long *fpregs
= child
->thread_info
->fpregs
;
495 if (copy_from_user(fpregs
, &fps
->regs
[0],
496 (32 * sizeof(unsigned int))) ||
497 __get_user(fsr
, (&fps
->fsr
))) {
498 pt_error_return(regs
, EFAULT
);
501 child
->thread_info
->xfsr
[0] &= 0xffffffff00000000UL
;
502 child
->thread_info
->xfsr
[0] |= fsr
;
503 if (!(child
->thread_info
->fpsaved
[0] & FPRS_FEF
))
504 child
->thread_info
->gsr
[0] = 0;
505 child
->thread_info
->fpsaved
[0] |= (FPRS_FEF
| FPRS_DL
);
506 pt_succ_return(regs
, 0);
510 case PTRACE_SETFPREGS64
: {
512 unsigned int regs
[64];
515 struct fps __user
*fps
= (struct fps __user
*) addr
;
516 unsigned long *fpregs
= child
->thread_info
->fpregs
;
518 if (copy_from_user(fpregs
, &fps
->regs
[0],
519 (64 * sizeof(unsigned int))) ||
520 __get_user(child
->thread_info
->xfsr
[0], (&fps
->fsr
))) {
521 pt_error_return(regs
, EFAULT
);
524 if (!(child
->thread_info
->fpsaved
[0] & FPRS_FEF
))
525 child
->thread_info
->gsr
[0] = 0;
526 child
->thread_info
->fpsaved
[0] |= (FPRS_FEF
| FPRS_DL
| FPRS_DU
);
527 pt_succ_return(regs
, 0);
531 case PTRACE_READTEXT
:
532 case PTRACE_READDATA
: {
533 int res
= ptrace_readdata(child
, addr
,
534 (char __user
*)addr2
, data
);
536 pt_succ_return(regs
, 0);
541 pt_error_return(regs
, -res
);
545 case PTRACE_WRITETEXT
:
546 case PTRACE_WRITEDATA
: {
547 int res
= ptrace_writedata(child
, (char __user
*) addr2
,
550 pt_succ_return(regs
, 0);
555 pt_error_return(regs
, -res
);
558 case PTRACE_SYSCALL
: /* continue and stop at (return from) syscall */
561 case PTRACE_CONT
: { /* restart after signal. */
563 pt_error_return(regs
, EIO
);
567 if (request
== PTRACE_SYSCALL
) {
568 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
570 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
573 child
->exit_code
= data
;
575 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child
->comm
,
576 child
->pid
, child
->exit_code
,
577 child
->thread_info
->kregs
->tpc
,
578 child
->thread_info
->kregs
->tnpc
);
581 wake_up_process(child
);
582 pt_succ_return(regs
, 0);
587 * make the child exit. Best I can do is send it a sigkill.
588 * perhaps it should be put in the status that it wants to
592 if (child
->exit_state
== EXIT_ZOMBIE
) { /* already dead */
593 pt_succ_return(regs
, 0);
596 child
->exit_code
= SIGKILL
;
597 wake_up_process(child
);
598 pt_succ_return(regs
, 0);
602 case PTRACE_SUNDETACH
: { /* detach a process that was attached. */
603 int error
= ptrace_detach(child
, data
);
605 pt_error_return(regs
, EIO
);
608 pt_succ_return(regs
, 0);
612 /* PTRACE_DUMPCORE unsupported... */
615 int err
= ptrace_request(child
, request
, addr
, data
);
617 pt_error_return(regs
, -err
);
619 pt_succ_return(regs
, 0);
625 put_task_struct(child
);
630 asmlinkage
void syscall_trace(void)
633 printk("%s [%d]: syscall_trace\n", current
->comm
, current
->pid
);
635 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
637 if (!(current
->ptrace
& PT_PTRACED
))
639 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
643 * this isn't the same as continuing with a signal, but it will do
644 * for normal use. strace only continues with a signal if the
645 * stopping signal is not SIGTRAP. -brl
648 printk("%s [%d]: syscall_trace exit= %x\n", current
->comm
,
649 current
->pid
, current
->exit_code
);
651 if (current
->exit_code
) {
652 send_sig (current
->exit_code
, current
, 1);
653 current
->exit_code
= 0;