1 /* ptrace.c: Sparc process tracing support.
3 * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
5 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
8 * Added Linux support -miguel (weird, eh?, the orignal code was meant
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/security.h>
21 #include <linux/signal.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
27 #define MAGIC_CONSTANT 0x80000000
30 /* Returning from ptrace is a bit tricky because the syscall return
31 * low level code assumes any value returned which is negative and
32 * is a valid errno will mean setting the condition codes to indicate
33 * an error return. This doesn't work, so we have this hook.
35 static inline void pt_error_return(struct pt_regs
*regs
, unsigned long error
)
37 regs
->u_regs
[UREG_I0
] = error
;
43 static inline void pt_succ_return(struct pt_regs
*regs
, unsigned long value
)
45 regs
->u_regs
[UREG_I0
] = value
;
52 pt_succ_return_linux(struct pt_regs
*regs
, unsigned long value
, long __user
*addr
)
54 if (put_user(value
, addr
)) {
55 pt_error_return(regs
, EFAULT
);
58 regs
->u_regs
[UREG_I0
] = 0;
65 pt_os_succ_return (struct pt_regs
*regs
, unsigned long val
, long __user
*addr
)
67 if (current
->personality
== PER_SUNOS
)
68 pt_succ_return (regs
, val
);
70 pt_succ_return_linux (regs
, val
, addr
);
73 /* Fuck me gently with a chainsaw... */
74 static inline void read_sunos_user(struct pt_regs
*regs
, unsigned long offset
,
75 struct task_struct
*tsk
, long __user
*addr
)
77 struct pt_regs
*cregs
= tsk
->thread
.kregs
;
78 struct thread_info
*t
= task_thread_info(tsk
);
82 offset
-= 1024; /* whee... */
83 if(offset
& ((sizeof(unsigned long) - 1))) {
84 pt_error_return(regs
, EIO
);
87 if(offset
>= 16 && offset
< 784) {
88 offset
-= 16; offset
>>= 2;
89 pt_os_succ_return(regs
, *(((unsigned long *)(&t
->reg_window
[0]))+offset
), addr
);
92 if(offset
>= 784 && offset
< 832) {
93 offset
-= 784; offset
>>= 2;
94 pt_os_succ_return(regs
, *(((unsigned long *)(&t
->rwbuf_stkptrs
[0]))+offset
), addr
);
114 v
= cregs
->u_regs
[UREG_I0
];
117 v
= cregs
->u_regs
[UREG_I1
];
120 v
= cregs
->u_regs
[UREG_I2
];
123 v
= cregs
->u_regs
[UREG_I3
];
126 v
= cregs
->u_regs
[UREG_I4
];
129 v
= cregs
->u_regs
[UREG_I5
];
132 v
= cregs
->u_regs
[UREG_I6
];
135 if(tsk
->thread
.flags
& MAGIC_CONSTANT
)
136 v
= cregs
->u_regs
[UREG_G1
];
141 v
= cregs
->u_regs
[UREG_I0
];
144 v
= cregs
->u_regs
[UREG_I1
];
148 /* Isn't binary compatibility _fun_??? */
149 if(cregs
->psr
& PSR_C
)
150 v
= cregs
->u_regs
[UREG_I0
] << 24;
155 /* Rest of them are completely unsupported. */
157 printk("%s [%d]: Wants to read user offset %ld\n",
158 current
->comm
, current
->pid
, offset
);
159 pt_error_return(regs
, EIO
);
162 if (current
->personality
== PER_SUNOS
)
163 pt_succ_return (regs
, v
);
165 pt_succ_return_linux (regs
, v
, addr
);
169 static inline void write_sunos_user(struct pt_regs
*regs
, unsigned long offset
,
170 struct task_struct
*tsk
)
172 struct pt_regs
*cregs
= tsk
->thread
.kregs
;
173 struct thread_info
*t
= task_thread_info(tsk
);
174 unsigned long value
= regs
->u_regs
[UREG_I3
];
177 offset
-= 1024; /* whee... */
178 if(offset
& ((sizeof(unsigned long) - 1)))
180 if(offset
>= 16 && offset
< 784) {
181 offset
-= 16; offset
>>= 2;
182 *(((unsigned long *)(&t
->reg_window
[0]))+offset
) = value
;
185 if(offset
>= 784 && offset
< 832) {
186 offset
-= 784; offset
>>= 2;
187 *(((unsigned long *)(&t
->rwbuf_stkptrs
[0]))+offset
) = value
;
192 cregs
->u_regs
[UREG_I0
] = value
;
195 cregs
->u_regs
[UREG_I1
] = value
;
198 cregs
->u_regs
[UREG_I2
] = value
;
201 cregs
->u_regs
[UREG_I3
] = value
;
204 cregs
->u_regs
[UREG_I4
] = value
;
207 cregs
->u_regs
[UREG_I5
] = value
;
210 cregs
->u_regs
[UREG_I6
] = value
;
213 cregs
->u_regs
[UREG_I7
] = value
;
216 cregs
->u_regs
[UREG_I0
] = value
;
219 cregs
->u_regs
[UREG_I1
] = value
;
222 /* Rest of them are completely unsupported or "no-touch". */
224 printk("%s [%d]: Wants to write user offset %ld\n",
225 current
->comm
, current
->pid
, offset
);
229 pt_succ_return(regs
, 0);
232 pt_error_return(regs
, EIO
);
236 /* #define ALLOW_INIT_TRACING */
237 /* #define DEBUG_PTRACE */
241 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
242 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
243 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
244 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
245 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
246 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
247 /* 24 */ "SYSCALL", ""
252 * Called by kernel/ptrace.c when detaching..
254 * Make sure single step bits etc are not set.
256 void ptrace_disable(struct task_struct
*child
)
261 asmlinkage
void do_ptrace(struct pt_regs
*regs
)
263 unsigned long request
= regs
->u_regs
[UREG_I0
];
264 unsigned long pid
= regs
->u_regs
[UREG_I1
];
265 unsigned long addr
= regs
->u_regs
[UREG_I2
];
266 unsigned long data
= regs
->u_regs
[UREG_I3
];
267 unsigned long addr2
= regs
->u_regs
[UREG_I4
];
268 struct task_struct
*child
;
276 if ((request
>= 0) && (request
<= 24))
281 if (request
== PTRACE_POKEDATA
&& data
== 0x91d02001){
282 printk ("do_ptrace: breakpoint pid=%d, addr=%08lx addr2=%08lx\n",
285 printk("do_ptrace: rq=%s(%d) pid=%d addr=%08lx data=%08lx addr2=%08lx\n",
286 s
, (int) request
, (int) pid
, addr
, data
, addr2
);
290 if (request
== PTRACE_TRACEME
) {
291 ret
= ptrace_traceme();
292 pt_succ_return(regs
, 0);
296 child
= ptrace_get_task_struct(pid
);
298 ret
= PTR_ERR(child
);
299 pt_error_return(regs
, -ret
);
303 if ((current
->personality
== PER_SUNOS
&& request
== PTRACE_SUNATTACH
)
304 || (current
->personality
!= PER_SUNOS
&& request
== PTRACE_ATTACH
)) {
305 if (ptrace_attach(child
)) {
306 pt_error_return(regs
, EPERM
);
309 pt_succ_return(regs
, 0);
313 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
315 pt_error_return(regs
, -ret
);
320 case PTRACE_PEEKTEXT
: /* read word at location addr. */
321 case PTRACE_PEEKDATA
: {
324 if (access_process_vm(child
, addr
,
325 &tmp
, sizeof(tmp
), 0) == sizeof(tmp
))
326 pt_os_succ_return(regs
, tmp
, (long __user
*)data
);
328 pt_error_return(regs
, EIO
);
333 read_sunos_user(regs
, addr
, child
, (long __user
*) data
);
337 write_sunos_user(regs
, addr
, child
);
340 case PTRACE_POKETEXT
: /* write the word at location addr. */
341 case PTRACE_POKEDATA
: {
342 if (access_process_vm(child
, addr
,
343 &data
, sizeof(data
), 1) == sizeof(data
))
344 pt_succ_return(regs
, 0);
346 pt_error_return(regs
, EIO
);
350 case PTRACE_GETREGS
: {
351 struct pt_regs __user
*pregs
= (struct pt_regs __user
*) addr
;
352 struct pt_regs
*cregs
= child
->thread
.kregs
;
355 if (!access_ok(VERIFY_WRITE
, pregs
, sizeof(struct pt_regs
))) {
357 pt_error_return(regs
, -rval
);
360 __put_user(cregs
->psr
, (&pregs
->psr
));
361 __put_user(cregs
->pc
, (&pregs
->pc
));
362 __put_user(cregs
->npc
, (&pregs
->npc
));
363 __put_user(cregs
->y
, (&pregs
->y
));
364 for(rval
= 1; rval
< 16; rval
++)
365 __put_user(cregs
->u_regs
[rval
], (&pregs
->u_regs
[rval
- 1]));
366 pt_succ_return(regs
, 0);
368 printk ("PC=%x nPC=%x o7=%x\n", cregs
->pc
, cregs
->npc
, cregs
->u_regs
[15]);
373 case PTRACE_SETREGS
: {
374 struct pt_regs __user
*pregs
= (struct pt_regs __user
*) addr
;
375 struct pt_regs
*cregs
= child
->thread
.kregs
;
376 unsigned long psr
, pc
, npc
, y
;
379 /* Must be careful, tracing process can only set certain
382 if (!access_ok(VERIFY_READ
, pregs
, sizeof(struct pt_regs
))) {
383 pt_error_return(regs
, EFAULT
);
386 __get_user(psr
, (&pregs
->psr
));
387 __get_user(pc
, (&pregs
->pc
));
388 __get_user(npc
, (&pregs
->npc
));
389 __get_user(y
, (&pregs
->y
));
391 cregs
->psr
&= ~PSR_ICC
;
393 if (!((pc
| npc
) & 3)) {
398 for(i
= 1; i
< 16; i
++)
399 __get_user(cregs
->u_regs
[i
], (&pregs
->u_regs
[i
-1]));
400 pt_succ_return(regs
, 0);
404 case PTRACE_GETFPREGS
: {
406 unsigned long regs
[32];
412 unsigned long *insnaddr
;
416 struct fps __user
*fps
= (struct fps __user
*) addr
;
419 if (!access_ok(VERIFY_WRITE
, fps
, sizeof(struct fps
))) {
421 pt_error_return(regs
, -i
);
424 for(i
= 0; i
< 32; i
++)
425 __put_user(child
->thread
.float_regs
[i
], (&fps
->regs
[i
]));
426 __put_user(child
->thread
.fsr
, (&fps
->fsr
));
427 __put_user(child
->thread
.fpqdepth
, (&fps
->fpqd
));
428 __put_user(0, (&fps
->flags
));
429 __put_user(0, (&fps
->extra
));
430 for(i
= 0; i
< 16; i
++) {
431 __put_user(child
->thread
.fpqueue
[i
].insn_addr
,
432 (&fps
->fpq
[i
].insnaddr
));
433 __put_user(child
->thread
.fpqueue
[i
].insn
, (&fps
->fpq
[i
].insn
));
435 pt_succ_return(regs
, 0);
439 case PTRACE_SETFPREGS
: {
441 unsigned long regs
[32];
447 unsigned long *insnaddr
;
451 struct fps __user
*fps
= (struct fps __user
*) addr
;
454 if (!access_ok(VERIFY_READ
, fps
, sizeof(struct fps
))) {
456 pt_error_return(regs
, -i
);
459 copy_from_user(&child
->thread
.float_regs
[0], &fps
->regs
[0], (32 * sizeof(unsigned long)));
460 __get_user(child
->thread
.fsr
, (&fps
->fsr
));
461 __get_user(child
->thread
.fpqdepth
, (&fps
->fpqd
));
462 for(i
= 0; i
< 16; i
++) {
463 __get_user(child
->thread
.fpqueue
[i
].insn_addr
,
464 (&fps
->fpq
[i
].insnaddr
));
465 __get_user(child
->thread
.fpqueue
[i
].insn
, (&fps
->fpq
[i
].insn
));
467 pt_succ_return(regs
, 0);
471 case PTRACE_READTEXT
:
472 case PTRACE_READDATA
: {
473 int res
= ptrace_readdata(child
, addr
,
474 (void __user
*) addr2
, data
);
477 pt_succ_return(regs
, 0);
480 /* Partial read is an IO failure */
483 pt_error_return(regs
, -res
);
487 case PTRACE_WRITETEXT
:
488 case PTRACE_WRITEDATA
: {
489 int res
= ptrace_writedata(child
, (void __user
*) addr2
,
493 pt_succ_return(regs
, 0);
496 /* Partial write is an IO failure */
499 pt_error_return(regs
, -res
);
503 case PTRACE_SYSCALL
: /* continue and stop at (return from) syscall */
506 case PTRACE_CONT
: { /* restart after signal. */
507 if (!valid_signal(data
)) {
508 pt_error_return(regs
, EIO
);
512 if (request
== PTRACE_SYSCALL
)
513 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
515 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
517 child
->exit_code
= data
;
519 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n",
520 child
->comm
, child
->pid
, child
->exit_code
,
521 child
->thread
.kregs
->pc
,
522 child
->thread
.kregs
->npc
);
524 wake_up_process(child
);
525 pt_succ_return(regs
, 0);
530 * make the child exit. Best I can do is send it a sigkill.
531 * perhaps it should be put in the status that it wants to
535 if (child
->exit_state
== EXIT_ZOMBIE
) { /* already dead */
536 pt_succ_return(regs
, 0);
539 wake_up_process(child
);
540 child
->exit_code
= SIGKILL
;
541 pt_succ_return(regs
, 0);
545 case PTRACE_SUNDETACH
: { /* detach a process that was attached. */
546 int err
= ptrace_detach(child
, data
);
548 pt_error_return(regs
, EIO
);
551 pt_succ_return(regs
, 0);
555 /* PTRACE_DUMPCORE unsupported... */
558 int err
= ptrace_request(child
, request
, addr
, data
);
560 pt_error_return(regs
, -err
);
562 pt_succ_return(regs
, 0);
568 put_task_struct(child
);
573 asmlinkage
void syscall_trace(void)
576 printk("%s [%d]: syscall_trace\n", current
->comm
, current
->pid
);
578 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
580 if (!(current
->ptrace
& PT_PTRACED
))
582 current
->thread
.flags
^= MAGIC_CONSTANT
;
583 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
586 * this isn't the same as continuing with a signal, but it will do
587 * for normal use. strace only continues with a signal if the
588 * stopping signal is not SIGTRAP. -brl
591 printk("%s [%d]: syscall_trace exit= %x\n", current
->comm
,
592 current
->pid
, current
->exit_code
);
594 if (current
->exit_code
) {
595 send_sig (current
->exit_code
, current
, 1);
596 current
->exit_code
= 0;