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>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
26 #define MAGIC_CONSTANT 0x80000000
29 /* Returning from ptrace is a bit tricky because the syscall return
30 * low level code assumes any value returned which is negative and
31 * is a valid errno will mean setting the condition codes to indicate
32 * an error return. This doesn't work, so we have this hook.
34 static inline void pt_error_return(struct pt_regs *regs, unsigned long error)
36 regs->u_regs[UREG_I0] = error;
42 static inline void pt_succ_return(struct pt_regs *regs, unsigned long value)
44 regs->u_regs[UREG_I0] = value;
51 pt_succ_return_linux(struct pt_regs *regs, unsigned long value, long __user *addr)
53 if (put_user(value, addr)) {
54 pt_error_return(regs, EFAULT);
57 regs->u_regs[UREG_I0] = 0;
64 pt_os_succ_return (struct pt_regs *regs, unsigned long val, long __user *addr)
66 if (current->personality == PER_SUNOS)
67 pt_succ_return (regs, val);
69 pt_succ_return_linux (regs, val, addr);
72 /* Fuck me gently with a chainsaw... */
73 static inline void read_sunos_user(struct pt_regs *regs, unsigned long offset,
74 struct task_struct *tsk, long __user *addr)
76 struct pt_regs *cregs = tsk->thread.kregs;
77 struct thread_info *t = tsk->thread_info;
81 offset -= 1024; /* whee... */
82 if(offset & ((sizeof(unsigned long) - 1))) {
83 pt_error_return(regs, EIO);
86 if(offset >= 16 && offset < 784) {
87 offset -= 16; offset >>= 2;
88 pt_os_succ_return(regs, *(((unsigned long *)(&t->reg_window[0]))+offset), addr);
91 if(offset >= 784 && offset < 832) {
92 offset -= 784; offset >>= 2;
93 pt_os_succ_return(regs, *(((unsigned long *)(&t->rwbuf_stkptrs[0]))+offset), addr);
113 v = cregs->u_regs[UREG_I0];
116 v = cregs->u_regs[UREG_I1];
119 v = cregs->u_regs[UREG_I2];
122 v = cregs->u_regs[UREG_I3];
125 v = cregs->u_regs[UREG_I4];
128 v = cregs->u_regs[UREG_I5];
131 v = cregs->u_regs[UREG_I6];
134 if(tsk->thread.flags & MAGIC_CONSTANT)
135 v = cregs->u_regs[UREG_G1];
140 v = cregs->u_regs[UREG_I0];
143 v = cregs->u_regs[UREG_I1];
147 /* Isn't binary compatibility _fun_??? */
148 if(cregs->psr & PSR_C)
149 v = cregs->u_regs[UREG_I0] << 24;
154 /* Rest of them are completely unsupported. */
156 printk("%s [%d]: Wants to read user offset %ld\n",
157 current->comm, current->pid, offset);
158 pt_error_return(regs, EIO);
161 if (current->personality == PER_SUNOS)
162 pt_succ_return (regs, v);
164 pt_succ_return_linux (regs, v, addr);
168 static inline void write_sunos_user(struct pt_regs *regs, unsigned long offset,
169 struct task_struct *tsk)
171 struct pt_regs *cregs = tsk->thread.kregs;
172 struct thread_info *t = tsk->thread_info;
173 unsigned long value = regs->u_regs[UREG_I3];
176 offset -= 1024; /* whee... */
177 if(offset & ((sizeof(unsigned long) - 1)))
179 if(offset >= 16 && offset < 784) {
180 offset -= 16; offset >>= 2;
181 *(((unsigned long *)(&t->reg_window[0]))+offset) = value;
184 if(offset >= 784 && offset < 832) {
185 offset -= 784; offset >>= 2;
186 *(((unsigned long *)(&t->rwbuf_stkptrs[0]))+offset) = value;
191 cregs->u_regs[UREG_I0] = value;
194 cregs->u_regs[UREG_I1] = value;
197 cregs->u_regs[UREG_I2] = value;
200 cregs->u_regs[UREG_I3] = value;
203 cregs->u_regs[UREG_I4] = value;
206 cregs->u_regs[UREG_I5] = value;
209 cregs->u_regs[UREG_I6] = value;
212 cregs->u_regs[UREG_I7] = value;
215 cregs->u_regs[UREG_I0] = value;
218 cregs->u_regs[UREG_I1] = value;
221 /* Rest of them are completely unsupported or "no-touch". */
223 printk("%s [%d]: Wants to write user offset %ld\n",
224 current->comm, current->pid, offset);
228 pt_succ_return(regs, 0);
231 pt_error_return(regs, EIO);
235 /* #define ALLOW_INIT_TRACING */
236 /* #define DEBUG_PTRACE */
240 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
241 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
242 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
243 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
244 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
245 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
246 /* 24 */ "SYSCALL", ""
251 * Called by kernel/ptrace.c when detaching..
253 * Make sure single step bits etc are not set.
255 void ptrace_disable(struct task_struct *child)
260 asmlinkage void do_ptrace(struct pt_regs *regs)
262 unsigned long request = regs->u_regs[UREG_I0];
263 unsigned long pid = regs->u_regs[UREG_I1];
264 unsigned long addr = regs->u_regs[UREG_I2];
265 unsigned long data = regs->u_regs[UREG_I3];
266 unsigned long addr2 = regs->u_regs[UREG_I4];
267 struct task_struct *child;
275 if ((request >= 0) && (request <= 24))
280 if (request == PTRACE_POKEDATA && data == 0x91d02001){
281 printk ("do_ptrace: breakpoint pid=%d, addr=%08lx addr2=%08lx\n",
284 printk("do_ptrace: rq=%s(%d) pid=%d addr=%08lx data=%08lx addr2=%08lx\n",
285 s, (int) request, (int) pid, addr, data, addr2);
288 if (request == PTRACE_TRACEME) {
291 /* are we already being traced? */
292 if (current->ptrace & PT_PTRACED) {
293 pt_error_return(regs, EPERM);
296 my_ret = security_ptrace(current->parent, current);
298 pt_error_return(regs, -my_ret);
302 /* set the ptrace bit in the process flags. */
303 current->ptrace |= PT_PTRACED;
304 pt_succ_return(regs, 0);
307 #ifndef ALLOW_INIT_TRACING
309 /* Can't dork with init. */
310 pt_error_return(regs, EPERM);
314 read_lock(&tasklist_lock);
315 child = find_task_by_pid(pid);
317 get_task_struct(child);
318 read_unlock(&tasklist_lock);
321 pt_error_return(regs, ESRCH);
325 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
326 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
327 if (ptrace_attach(child)) {
328 pt_error_return(regs, EPERM);
331 pt_succ_return(regs, 0);
335 ret = ptrace_check_attach(child, request == PTRACE_KILL);
337 pt_error_return(regs, -ret);
342 case PTRACE_PEEKTEXT: /* read word at location addr. */
343 case PTRACE_PEEKDATA: {
346 if (access_process_vm(child, addr,
347 &tmp, sizeof(tmp), 0) == sizeof(tmp))
348 pt_os_succ_return(regs, tmp, (long __user *)data);
350 pt_error_return(regs, EIO);
355 read_sunos_user(regs, addr, child, (long __user *) data);
359 write_sunos_user(regs, addr, child);
362 case PTRACE_POKETEXT: /* write the word at location addr. */
363 case PTRACE_POKEDATA: {
364 if (access_process_vm(child, addr,
365 &data, sizeof(data), 1) == sizeof(data))
366 pt_succ_return(regs, 0);
368 pt_error_return(regs, EIO);
372 case PTRACE_GETREGS: {
373 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
374 struct pt_regs *cregs = child->thread.kregs;
377 if (!access_ok(VERIFY_WRITE, pregs, sizeof(struct pt_regs))) {
379 pt_error_return(regs, -rval);
382 __put_user(cregs->psr, (&pregs->psr));
383 __put_user(cregs->pc, (&pregs->pc));
384 __put_user(cregs->npc, (&pregs->npc));
385 __put_user(cregs->y, (&pregs->y));
386 for(rval = 1; rval < 16; rval++)
387 __put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]));
388 pt_succ_return(regs, 0);
390 printk ("PC=%x nPC=%x o7=%x\n", cregs->pc, cregs->npc, cregs->u_regs [15]);
395 case PTRACE_SETREGS: {
396 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
397 struct pt_regs *cregs = child->thread.kregs;
398 unsigned long psr, pc, npc, y;
401 /* Must be careful, tracing process can only set certain
404 if (!access_ok(VERIFY_READ, pregs, sizeof(struct pt_regs))) {
405 pt_error_return(regs, EFAULT);
408 __get_user(psr, (&pregs->psr));
409 __get_user(pc, (&pregs->pc));
410 __get_user(npc, (&pregs->npc));
411 __get_user(y, (&pregs->y));
413 cregs->psr &= ~PSR_ICC;
415 if (!((pc | npc) & 3)) {
420 for(i = 1; i < 16; i++)
421 __get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]));
422 pt_succ_return(regs, 0);
426 case PTRACE_GETFPREGS: {
428 unsigned long regs[32];
434 unsigned long *insnaddr;
438 struct fps __user *fps = (struct fps __user *) addr;
441 if (!access_ok(VERIFY_WRITE, fps, sizeof(struct fps))) {
443 pt_error_return(regs, -i);
446 for(i = 0; i < 32; i++)
447 __put_user(child->thread.float_regs[i], (&fps->regs[i]));
448 __put_user(child->thread.fsr, (&fps->fsr));
449 __put_user(child->thread.fpqdepth, (&fps->fpqd));
450 __put_user(0, (&fps->flags));
451 __put_user(0, (&fps->extra));
452 for(i = 0; i < 16; i++) {
453 __put_user(child->thread.fpqueue[i].insn_addr,
454 (&fps->fpq[i].insnaddr));
455 __put_user(child->thread.fpqueue[i].insn, (&fps->fpq[i].insn));
457 pt_succ_return(regs, 0);
461 case PTRACE_SETFPREGS: {
463 unsigned long regs[32];
469 unsigned long *insnaddr;
473 struct fps __user *fps = (struct fps __user *) addr;
476 if (!access_ok(VERIFY_READ, fps, sizeof(struct fps))) {
478 pt_error_return(regs, -i);
481 copy_from_user(&child->thread.float_regs[0], &fps->regs[0], (32 * sizeof(unsigned long)));
482 __get_user(child->thread.fsr, (&fps->fsr));
483 __get_user(child->thread.fpqdepth, (&fps->fpqd));
484 for(i = 0; i < 16; i++) {
485 __get_user(child->thread.fpqueue[i].insn_addr,
486 (&fps->fpq[i].insnaddr));
487 __get_user(child->thread.fpqueue[i].insn, (&fps->fpq[i].insn));
489 pt_succ_return(regs, 0);
493 case PTRACE_READTEXT:
494 case PTRACE_READDATA: {
495 int res = ptrace_readdata(child, addr,
496 (void __user *) addr2, data);
499 pt_succ_return(regs, 0);
502 /* Partial read is an IO failure */
505 pt_error_return(regs, -res);
509 case PTRACE_WRITETEXT:
510 case PTRACE_WRITEDATA: {
511 int res = ptrace_writedata(child, (void __user *) addr2,
515 pt_succ_return(regs, 0);
518 /* Partial write is an IO failure */
521 pt_error_return(regs, -res);
525 case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
528 case PTRACE_CONT: { /* restart after signal. */
530 pt_error_return(regs, EIO);
534 if (request == PTRACE_SYSCALL)
535 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
537 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
539 child->exit_code = data;
541 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n",
542 child->comm, child->pid, child->exit_code,
543 child->thread.kregs->pc,
544 child->thread.kregs->npc);
546 wake_up_process(child);
547 pt_succ_return(regs, 0);
552 * make the child exit. Best I can do is send it a sigkill.
553 * perhaps it should be put in the status that it wants to
557 if (child->exit_state == EXIT_ZOMBIE) { /* already dead */
558 pt_succ_return(regs, 0);
561 wake_up_process(child);
562 child->exit_code = SIGKILL;
563 pt_succ_return(regs, 0);
567 case PTRACE_SUNDETACH: { /* detach a process that was attached. */
568 int err = ptrace_detach(child, data);
570 pt_error_return(regs, EIO);
573 pt_succ_return(regs, 0);
577 /* PTRACE_DUMPCORE unsupported... */
580 int err = ptrace_request(child, request, addr, data);
582 pt_error_return(regs, -err);
584 pt_succ_return(regs, 0);
590 put_task_struct(child);
595 asmlinkage void syscall_trace(void)
598 printk("%s [%d]: syscall_trace\n", current->comm, current->pid);
600 if (!test_thread_flag(TIF_SYSCALL_TRACE))
602 if (!(current->ptrace & PT_PTRACED))
604 current->thread.flags ^= MAGIC_CONSTANT;
605 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
608 * this isn't the same as continuing with a signal, but it will do
609 * for normal use. strace only continues with a signal if the
610 * stopping signal is not SIGTRAP. -brl
613 printk("%s [%d]: syscall_trace exit= %x\n", current->comm,
614 current->pid, current->exit_code);
616 if (current->exit_code) {
617 send_sig (current->exit_code, current, 1);
618 current->exit_code = 0;