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>
21 #include <asm/pgtable.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
25 #define MAGIC_CONSTANT 0x80000000
28 /* Returning from ptrace is a bit tricky because the syscall return
29 * low level code assumes any value returned which is negative and
30 * is a valid errno will mean setting the condition codes to indicate
31 * an error return. This doesn't work, so we have this hook.
33 static inline void pt_error_return(struct pt_regs *regs, unsigned long error)
35 regs->u_regs[UREG_I0] = error;
41 static inline void pt_succ_return(struct pt_regs *regs, unsigned long value)
43 regs->u_regs[UREG_I0] = value;
50 pt_succ_return_linux(struct pt_regs *regs, unsigned long value, long *addr)
52 if(put_user(value, addr))
53 return pt_error_return(regs, EFAULT);
54 regs->u_regs[UREG_I0] = 0;
61 pt_os_succ_return (struct pt_regs *regs, unsigned long val, long *addr)
63 if (current->personality == PER_SUNOS)
64 pt_succ_return (regs, val);
66 pt_succ_return_linux (regs, val, addr);
69 /* Fuck me gently with a chainsaw... */
70 static inline void read_sunos_user(struct pt_regs *regs, unsigned long offset,
71 struct task_struct *tsk, long *addr)
73 struct pt_regs *cregs = tsk->thread.kregs;
74 struct thread_struct *t = &tsk->thread;
78 offset -= 1024; /* whee... */
79 if(offset & ((sizeof(unsigned long) - 1))) {
80 pt_error_return(regs, EIO);
83 if(offset >= 16 && offset < 784) {
84 offset -= 16; offset >>= 2;
85 pt_os_succ_return(regs, *(((unsigned long *)(&t->reg_window[0]))+offset), addr);
88 if(offset >= 784 && offset < 832) {
89 offset -= 784; offset >>= 2;
90 pt_os_succ_return(regs, *(((unsigned long *)(&t->rwbuf_stkptrs[0]))+offset), addr);
110 v = cregs->u_regs[UREG_I0];
113 v = cregs->u_regs[UREG_I1];
116 v = cregs->u_regs[UREG_I2];
119 v = cregs->u_regs[UREG_I3];
122 v = cregs->u_regs[UREG_I4];
125 v = cregs->u_regs[UREG_I5];
128 v = cregs->u_regs[UREG_I6];
131 if(tsk->thread.flags & MAGIC_CONSTANT)
132 v = cregs->u_regs[UREG_G1];
137 v = cregs->u_regs[UREG_I0];
140 v = cregs->u_regs[UREG_I1];
144 /* Isn't binary compatibility _fun_??? */
145 if(cregs->psr & PSR_C)
146 v = cregs->u_regs[UREG_I0] << 24;
151 /* Rest of them are completely unsupported. */
153 printk("%s [%d]: Wants to read user offset %ld\n",
154 current->comm, current->pid, offset);
155 pt_error_return(regs, EIO);
158 if (current->personality == PER_SUNOS)
159 pt_succ_return (regs, v);
161 pt_succ_return_linux (regs, v, addr);
165 static inline void write_sunos_user(struct pt_regs *regs, unsigned long offset,
166 struct task_struct *tsk)
168 struct pt_regs *cregs = tsk->thread.kregs;
169 struct thread_struct *t = &tsk->thread;
170 unsigned long value = regs->u_regs[UREG_I3];
173 offset -= 1024; /* whee... */
174 if(offset & ((sizeof(unsigned long) - 1)))
176 if(offset >= 16 && offset < 784) {
177 offset -= 16; offset >>= 2;
178 *(((unsigned long *)(&t->reg_window[0]))+offset) = value;
181 if(offset >= 784 && offset < 832) {
182 offset -= 784; offset >>= 2;
183 *(((unsigned long *)(&t->rwbuf_stkptrs[0]))+offset) = value;
188 cregs->u_regs[UREG_I0] = value;
191 cregs->u_regs[UREG_I1] = value;
194 cregs->u_regs[UREG_I2] = value;
197 cregs->u_regs[UREG_I3] = value;
200 cregs->u_regs[UREG_I4] = value;
203 cregs->u_regs[UREG_I5] = value;
206 cregs->u_regs[UREG_I6] = value;
209 cregs->u_regs[UREG_I7] = value;
212 cregs->u_regs[UREG_I0] = value;
215 cregs->u_regs[UREG_I1] = value;
218 /* Rest of them are completely unsupported or "no-touch". */
220 printk("%s [%d]: Wants to write user offset %ld\n",
221 current->comm, current->pid, offset);
225 pt_succ_return(regs, 0);
228 pt_error_return(regs, EIO);
232 /* #define ALLOW_INIT_TRACING */
233 /* #define DEBUG_PTRACE */
263 asmlinkage void do_ptrace(struct pt_regs *regs)
265 unsigned long request = regs->u_regs[UREG_I0];
266 unsigned long pid = regs->u_regs[UREG_I1];
267 unsigned long addr = regs->u_regs[UREG_I2];
268 unsigned long data = regs->u_regs[UREG_I3];
269 unsigned long addr2 = regs->u_regs[UREG_I4];
270 struct task_struct *child;
277 if ((request > 0) && (request < 21))
282 if (request == PTRACE_POKEDATA && data == 0x91d02001){
283 printk ("do_ptrace: breakpoint pid=%d, addr=%08lx addr2=%08lx\n",
286 printk("do_ptrace: rq=%s(%d) pid=%d addr=%08lx data=%08lx addr2=%08lx\n",
287 s, (int) request, (int) pid, addr, data, addr2);
290 if(request == PTRACE_TRACEME) {
291 /* are we already being traced? */
292 if (current->ptrace & PT_PTRACED) {
293 pt_error_return(regs, EPERM);
296 /* set the ptrace bit in the process flags. */
297 current->ptrace |= PT_PTRACED;
298 pt_succ_return(regs, 0);
301 #ifndef ALLOW_INIT_TRACING
303 /* Can't dork with init. */
304 pt_error_return(regs, EPERM);
308 read_lock(&tasklist_lock);
309 child = find_task_by_pid(pid);
311 get_task_struct(child);
312 read_unlock(&tasklist_lock);
315 pt_error_return(regs, ESRCH);
319 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
320 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
323 if(child == current) {
324 /* Try this under SunOS/Solaris, bwa haha
325 * You'll never be able to kill the process. ;-)
327 pt_error_return(regs, EPERM);
330 if((!child->dumpable ||
331 (current->uid != child->euid) ||
332 (current->uid != child->uid) ||
333 (current->uid != child->suid) ||
334 (current->gid != child->egid) ||
335 (current->gid != child->sgid) ||
336 (!cap_issubset(child->cap_permitted, current->cap_permitted)) ||
337 (current->gid != child->gid)) && !capable(CAP_SYS_PTRACE)) {
338 pt_error_return(regs, EPERM);
341 /* the same process cannot be attached many times */
342 if (child->ptrace & PT_PTRACED) {
343 pt_error_return(regs, EPERM);
346 child->ptrace |= PT_PTRACED;
347 write_lock_irqsave(&tasklist_lock, flags);
348 if(child->p_pptr != current) {
350 child->p_pptr = current;
353 write_unlock_irqrestore(&tasklist_lock, flags);
354 send_sig(SIGSTOP, child, 1);
355 pt_succ_return(regs, 0);
358 if (!(child->ptrace & PT_PTRACED)) {
359 pt_error_return(regs, ESRCH);
362 if(child->state != TASK_STOPPED) {
363 if(request != PTRACE_KILL) {
364 pt_error_return(regs, ESRCH);
368 if(child->p_pptr != current) {
369 pt_error_return(regs, ESRCH);
373 case PTRACE_PEEKTEXT: /* read word at location addr. */
374 case PTRACE_PEEKDATA: {
377 if (access_process_vm(child, addr,
378 &tmp, sizeof(tmp), 0) == sizeof(tmp))
379 pt_os_succ_return(regs, tmp, (long *)data);
381 pt_error_return(regs, EIO);
386 read_sunos_user(regs, addr, child, (long *) data);
390 write_sunos_user(regs, addr, child);
393 case PTRACE_POKETEXT: /* write the word at location addr. */
394 case PTRACE_POKEDATA: {
395 if (access_process_vm(child, addr,
396 &data, sizeof(data), 1) == sizeof(data))
397 pt_succ_return(regs, 0);
399 pt_error_return(regs, EIO);
403 case PTRACE_GETREGS: {
404 struct pt_regs *pregs = (struct pt_regs *) addr;
405 struct pt_regs *cregs = child->thread.kregs;
408 rval = verify_area(VERIFY_WRITE, pregs, sizeof(struct pt_regs));
410 pt_error_return(regs, -rval);
413 __put_user(cregs->psr, (&pregs->psr));
414 __put_user(cregs->pc, (&pregs->pc));
415 __put_user(cregs->npc, (&pregs->npc));
416 __put_user(cregs->y, (&pregs->y));
417 for(rval = 1; rval < 16; rval++)
418 __put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]));
419 pt_succ_return(regs, 0);
421 printk ("PC=%x nPC=%x o7=%x\n", cregs->pc, cregs->npc, cregs->u_regs [15]);
426 case PTRACE_SETREGS: {
427 struct pt_regs *pregs = (struct pt_regs *) addr;
428 struct pt_regs *cregs = child->thread.kregs;
429 unsigned long psr, pc, npc, y;
432 /* Must be careful, tracing process can only set certain
435 i = verify_area(VERIFY_READ, pregs, sizeof(struct pt_regs));
437 pt_error_return(regs, -i);
440 __get_user(psr, (&pregs->psr));
441 __get_user(pc, (&pregs->pc));
442 __get_user(npc, (&pregs->npc));
443 __get_user(y, (&pregs->y));
445 cregs->psr &= ~PSR_ICC;
447 if(!((pc | npc) & 3)) {
452 for(i = 1; i < 16; i++)
453 __get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]));
454 pt_succ_return(regs, 0);
458 case PTRACE_GETFPREGS: {
460 unsigned long regs[32];
466 unsigned long *insnaddr;
469 } *fps = (struct fps *) addr;
472 i = verify_area(VERIFY_WRITE, fps, sizeof(struct fps));
474 pt_error_return(regs, -i);
477 for(i = 0; i < 32; i++)
478 __put_user(child->thread.float_regs[i], (&fps->regs[i]));
479 __put_user(child->thread.fsr, (&fps->fsr));
480 __put_user(child->thread.fpqdepth, (&fps->fpqd));
481 __put_user(0, (&fps->flags));
482 __put_user(0, (&fps->extra));
483 for(i = 0; i < 16; i++) {
484 __put_user(child->thread.fpqueue[i].insn_addr,
485 (&fps->fpq[i].insnaddr));
486 __put_user(child->thread.fpqueue[i].insn, (&fps->fpq[i].insn));
488 pt_succ_return(regs, 0);
492 case PTRACE_SETFPREGS: {
494 unsigned long regs[32];
500 unsigned long *insnaddr;
503 } *fps = (struct fps *) addr;
506 i = verify_area(VERIFY_READ, fps, sizeof(struct fps));
508 pt_error_return(regs, -i);
511 copy_from_user(&child->thread.float_regs[0], &fps->regs[0], (32 * sizeof(unsigned long)));
512 __get_user(child->thread.fsr, (&fps->fsr));
513 __get_user(child->thread.fpqdepth, (&fps->fpqd));
514 for(i = 0; i < 16; i++) {
515 __get_user(child->thread.fpqueue[i].insn_addr,
516 (&fps->fpq[i].insnaddr));
517 __get_user(child->thread.fpqueue[i].insn, (&fps->fpq[i].insn));
519 pt_succ_return(regs, 0);
523 case PTRACE_READTEXT:
524 case PTRACE_READDATA: {
525 int res = ptrace_readdata(child, addr, (void *) addr2, data);
528 pt_succ_return(regs, 0);
531 /* Partial read is an IO failure */
534 pt_error_return(regs, -res);
538 case PTRACE_WRITETEXT:
539 case PTRACE_WRITEDATA: {
540 int res = ptrace_writedata(child, (void *) addr2, addr, data);
543 pt_succ_return(regs, 0);
546 /* Partial write is an IO failure */
549 pt_error_return(regs, -res);
553 case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
556 case PTRACE_CONT: { /* restart after signal. */
557 if ((unsigned long) data > _NSIG) {
558 pt_error_return(regs, EIO);
563 pt_error_return(regs, EINVAL);
567 printk ("Original: %08lx %08lx\n", child->thread.kregs->pc, child->thread.kregs->npc);
568 printk ("Continuing with %08lx %08lx\n", addr, addr+4);
570 child->thread.kregs->pc = addr;
571 child->thread.kregs->npc = addr + 4;
574 if (request == PTRACE_SYSCALL)
575 child->ptrace |= PT_TRACESYS;
577 child->ptrace &= ~PT_TRACESYS;
579 child->exit_code = data;
581 printk("CONT: %s [%d]: set exit_code = %x %x %x\n", child->comm,
582 child->pid, child->exit_code,
583 child->thread.kregs->pc,
584 child->thread.kregs->npc);
587 wake_up_process(child);
588 pt_succ_return(regs, 0);
593 * make the child exit. Best I can do is send it a sigkill.
594 * perhaps it should be put in the status that it wants to
598 if (child->state == TASK_ZOMBIE) { /* already dead */
599 pt_succ_return(regs, 0);
602 wake_up_process(child);
603 child->exit_code = SIGKILL;
604 pt_succ_return(regs, 0);
608 case PTRACE_SUNDETACH: { /* detach a process that was attached. */
610 if ((unsigned long) data > _NSIG) {
611 pt_error_return(regs, EIO);
614 child->ptrace &= ~(PT_PTRACED|PT_TRACESYS);
615 wake_up_process(child);
616 child->exit_code = data;
617 write_lock_irqsave(&tasklist_lock, flags);
619 child->p_pptr = child->p_opptr;
621 write_unlock_irqrestore(&tasklist_lock, flags);
622 pt_succ_return(regs, 0);
626 /* PTRACE_DUMPCORE unsupported... */
629 pt_error_return(regs, EIO);
634 free_task_struct(child);
639 asmlinkage void syscall_trace(void)
642 printk("%s [%d]: syscall_trace\n", current->comm, current->pid);
644 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
645 != (PT_PTRACED|PT_TRACESYS))
647 current->exit_code = SIGTRAP;
648 current->state = TASK_STOPPED;
649 current->thread.flags ^= MAGIC_CONSTANT;
650 notify_parent(current, SIGCHLD);
653 * this isn't the same as continuing with a signal, but it will do
654 * for normal use. strace only continues with a signal if the
655 * stopping signal is not SIGTRAP. -brl
658 printk("%s [%d]: syscall_trace exit= %x\n", current->comm,
659 current->pid, current->exit_code);
661 if (current->exit_code) {
662 send_sig (current->exit_code, current, 1);
663 current->exit_code = 0;