extractedLnx/linux/arch/sparc64/kernel/ptrace.c_do_ptrace.c
asmlinkage void do_ptrace(struct pt_regs *regs)
{
int request = regs->u_regs[UREG_I0];
pid_t pid = regs->u_regs[UREG_I1];
unsigned long addr = regs->u_regs[UREG_I2];
unsigned long data = regs->u_regs[UREG_I3];
unsigned long addr2 = regs->u_regs[UREG_I4];
struct task_struct *child;
if (current->tss.flags & SPARC_FLAG_32BIT) {
addr &= 0xffffffffUL;
data &= 0xffffffffUL;
addr2 &= 0xffffffffUL;
}
lock_kernel();
#ifdef DEBUG_PTRACE
{
char *s;
if ((request > 0) && (request < 21))
s = pt_rq [request];
else
s = "unknown";
if (request == PTRACE_POKEDATA && data == 0x91d02001){
printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
pid, addr, addr2);
} else
printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
s, request, pid, addr, data, addr2);
}
#endif
if(request == PTRACE_TRACEME) {
/* are we already being traced? */
if (current->flags & PF_PTRACED) {
pt_error_return(regs, EPERM);
goto out;
}
/* set the ptrace bit in the process flags. */
current->flags |= PF_PTRACED;
pt_succ_return(regs, 0);
goto out;
}
#ifndef ALLOW_INIT_TRACING
if(pid == 1) {
/* Can't dork with init. */
pt_error_return(regs, EPERM);
goto out;
}
#endif
read_lock(&tasklist_lock);
child = find_task_by_pid(pid);
read_unlock(&tasklist_lock);
if(!child) {
pt_error_return(regs, ESRCH);
goto out;
}
if (((current->personality & PER_BSD) && (request == PTRACE_SUNATTACH))
|| (!(current->personality & PER_BSD) && (request == PTRACE_ATTACH))) {
unsigned long flags;
if(child == current) {
/* Try this under SunOS/Solaris, bwa haha
* You'll never be able to kill the process. ;-)
*/
pt_error_return(regs, EPERM);
goto out;
}
if((!child->dumpable ||
(current->uid != child->euid) ||
(current->uid != child->uid) ||
(current->uid != child->suid) ||
(current->gid != child->egid) ||
(current->gid != child->sgid) ||
(!cap_issubset(child->cap_permitted, current->cap_permitted)) ||
(current->gid != child->gid)) && !capable(CAP_SYS_PTRACE)) {
pt_error_return(regs, EPERM);
goto out;
}
/* the same process cannot be attached many times */
if (child->flags & PF_PTRACED) {
pt_error_return(regs, EPERM);
goto out;
}
child->flags |= PF_PTRACED;
write_lock_irqsave(&tasklist_lock, flags);
if(child->p_pptr != current) {
REMOVE_LINKS(child);
child->p_pptr = current;
SET_LINKS(child);
}
write_unlock_irqrestore(&tasklist_lock, flags);
send_sig(SIGSTOP, child, 1);
pt_succ_return(regs, 0);
goto out;
}
if (!(child->flags & PF_PTRACED)
&& ((current->personality & PER_BSD) && (request != PTRACE_SUNATTACH))
&& (!(current->personality & PER_BSD) && (request != PTRACE_ATTACH))) {
pt_error_return(regs, ESRCH);
goto out;
}
if(child->state != TASK_STOPPED) {
if(request != PTRACE_KILL) {
pt_error_return(regs, ESRCH);
goto out;
}
}
if(child->p_pptr != current) {
pt_error_return(regs, ESRCH);
goto out;
}
if(!(child->tss.flags & SPARC_FLAG_32BIT) &&
((request == PTRACE_READDATA64) ||
(request == PTRACE_WRITEDATA64) ||
(request == PTRACE_READTEXT64) ||
(request == PTRACE_WRITETEXT64) ||
(request == PTRACE_PEEKTEXT64) ||
(request == PTRACE_POKETEXT64) ||
(request == PTRACE_PEEKDATA64) ||
(request == PTRACE_POKEDATA64))) {
addr = regs->u_regs[UREG_G2];
addr2 = regs->u_regs[UREG_G3];
request -= 30; /* wheee... */
}
switch(request) {
case PTRACE_PEEKTEXT: /* read word at location addr. */
case PTRACE_PEEKDATA: {
unsigned long tmp;
int res;
/* Non-word alignment _not_ allowed on Sparc. */
if (current->tss.flags & SPARC_FLAG_32BIT) {
unsigned int x;
if(addr & (sizeof(unsigned int) - 1)) {
pt_error_return(regs, EINVAL);
goto out;
}
down(&child->mm->mmap_sem);
res = read_int(child, addr, &x);
up(&child->mm->mmap_sem);
tmp = x;
} else {
if(addr & (sizeof(unsigned long) - 1)) {
pt_error_return(regs, EINVAL);
goto out;
}
down(&child->mm->mmap_sem);
res = read_long(child, addr, &tmp);
up(&child->mm->mmap_sem);
}
if (res < 0) {
pt_error_return(regs, -res);
goto out;
}
pt_os_succ_return(regs, tmp, (long *) data);
goto out;
}
case PTRACE_PEEKUSR:
#if 0
read_sunos_user(regs, addr, child, (long *) data);
#endif
goto out;
case PTRACE_POKEUSR:
#if 0
write_sunos_user(regs, addr, child);
#endif
goto out;
case PTRACE_POKETEXT: /* write the word at location addr. */
case PTRACE_POKEDATA: {
int res;
/* Non-word alignment _not_ allowed on Sparc. */
if (current->tss.flags & SPARC_FLAG_32BIT) {
if(addr & (sizeof(unsigned int) - 1)) {
pt_error_return(regs, EINVAL);
goto out;
}
down(&child->mm->mmap_sem);
res = write_int(child, addr, data);
up(&child->mm->mmap_sem);
} else {
if(addr & (sizeof(unsigned long) - 1)) {
pt_error_return(regs, EINVAL);
goto out;
}
down(&child->mm->mmap_sem);
res = write_long(child, addr, data);
up(&child->mm->mmap_sem);
}
if(res < 0)
pt_error_return(regs, -res);
else
pt_succ_return(regs, res);
goto out;
}
case PTRACE_GETREGS: {
struct pt_regs32 *pregs = (struct pt_regs32 *) addr;
struct pt_regs *cregs = child->tss.kregs;
int rval;
if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) ||
__put_user(cregs->tpc, (&pregs->pc)) ||
__put_user(cregs->tnpc, (&pregs->npc)) ||
__put_user(cregs->y, (&pregs->y))) {
pt_error_return(regs, EFAULT);
goto out;
}
for(rval = 1; rval < 16; rval++)
if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
pt_error_return(regs, EFAULT);
goto out;
}
pt_succ_return(regs, 0);
#ifdef DEBUG_PTRACE
printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
#endif
goto out;
}
case PTRACE_GETREGS64: {
struct pt_regs *pregs = (struct pt_regs *) addr;
struct pt_regs *cregs = child->tss.kregs;
int rval;
if (__put_user(cregs->tstate, (&pregs->tstate)) ||
__put_user(cregs->tpc, (&pregs->tpc)) ||
__put_user(cregs->tnpc, (&pregs->tnpc)) ||
__put_user(cregs->y, (&pregs->y))) {
pt_error_return(regs, EFAULT);
goto out;
}
for(rval = 1; rval < 16; rval++)
if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
pt_error_return(regs, EFAULT);
goto out;
}
pt_succ_return(regs, 0);
#ifdef DEBUG_PTRACE
printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
#endif
goto out;
}
case PTRACE_SETREGS: {
struct pt_regs32 *pregs = (struct pt_regs32 *) addr;
struct pt_regs *cregs = child->tss.kregs;
unsigned int psr, pc, npc, y;
int i;
/* Must be careful, tracing process can only set certain
* bits in the psr.
*/
if (__get_user(psr, (&pregs->psr)) ||
__get_user(pc, (&pregs->pc)) ||
__get_user(npc, (&pregs->npc)) ||
__get_user(y, (&pregs->y))) {
pt_error_return(regs, EFAULT);
goto out;
}
cregs->tstate &= ~(TSTATE_ICC);
cregs->tstate |= psr_to_tstate_icc(psr);
if(!((pc | npc) & 3)) {
cregs->tpc = pc;
cregs->tnpc = npc;
}
cregs->y = y;
for(i = 1; i < 16; i++) {
if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
pt_error_return(regs, EFAULT);
goto out;
}
}
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_SETREGS64: {
struct pt_regs *pregs = (struct pt_regs *) addr;
struct pt_regs *cregs = child->tss.kregs;
unsigned long tstate, tpc, tnpc, y;
int i;
/* Must be careful, tracing process can only set certain
* bits in the psr.
*/
if (__get_user(tstate, (&pregs->tstate)) ||
__get_user(tpc, (&pregs->tpc)) ||
__get_user(tnpc, (&pregs->tnpc)) ||
__get_user(y, (&pregs->y))) {
pt_error_return(regs, EFAULT);
goto out;
}
tstate &= (TSTATE_ICC | TSTATE_XCC);
cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
cregs->tstate |= tstate;
if(!((tpc | tnpc) & 3)) {
cregs->tpc = tpc;
cregs->tnpc = tnpc;
}
cregs->y = y;
for(i = 1; i < 16; i++) {
if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
pt_error_return(regs, EFAULT);
goto out;
}
}
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_GETFPREGS: {
struct fps {
unsigned int regs[32];
unsigned int fsr;
unsigned int flags;
unsigned int extra;
unsigned int fpqd;
struct fq {
unsigned int insnaddr;
unsigned int insn;
} fpq[16];
} *fps = (struct fps *) addr;
unsigned long *fpregs = (unsigned long *)(((char *)child) + AOFF_task_fpregs);
if (copy_to_user(&fps->regs[0], fpregs,
(32 * sizeof(unsigned int))) ||
__put_user(child->tss.xfsr[0], (&fps->fsr)) ||
__put_user(0, (&fps->fpqd)) ||
__put_user(0, (&fps->flags)) ||
__put_user(0, (&fps->extra)) ||
clear_user(&fps->fpq[0], 32 * sizeof(unsigned int))) {
pt_error_return(regs, EFAULT);
goto out;
}
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_GETFPREGS64: {
struct fps {
unsigned int regs[64];
unsigned long fsr;
} *fps = (struct fps *) addr;
unsigned long *fpregs = (unsigned long *)(((char *)child) + AOFF_task_fpregs);
if (copy_to_user(&fps->regs[0], fpregs,
(64 * sizeof(unsigned int))) ||
__put_user(child->tss.xfsr[0], (&fps->fsr))) {
pt_error_return(regs, EFAULT);
goto out;
}
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_SETFPREGS: {
struct fps {
unsigned int regs[32];
unsigned int fsr;
unsigned int flags;
unsigned int extra;
unsigned int fpqd;
struct fq {
unsigned int insnaddr;
unsigned int insn;
} fpq[16];
} *fps = (struct fps *) addr;
unsigned long *fpregs = (unsigned long *)(((char *)child) + AOFF_task_fpregs);
unsigned fsr;
if (copy_from_user(fpregs, &fps->regs[0],
(32 * sizeof(unsigned int))) ||
__get_user(fsr, (&fps->fsr))) {
pt_error_return(regs, EFAULT);
goto out;
}
child->tss.xfsr[0] &= 0xffffffff00000000UL;
child->tss.xfsr[0] |= fsr;
if (!(child->tss.fpsaved[0] & FPRS_FEF))
child->tss.gsr[0] = 0;
child->tss.fpsaved[0] |= (FPRS_FEF | FPRS_DL);
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_SETFPREGS64: {
struct fps {
unsigned int regs[64];
unsigned long fsr;
} *fps = (struct fps *) addr;
unsigned long *fpregs = (unsigned long *)(((char *)child) + AOFF_task_fpregs);
if (copy_from_user(fpregs, &fps->regs[0],
(64 * sizeof(unsigned int))) ||
__get_user(child->tss.xfsr[0], (&fps->fsr))) {
pt_error_return(regs, EFAULT);
goto out;
}
if (!(child->tss.fpsaved[0] & FPRS_FEF))
child->tss.gsr[0] = 0;
child->tss.fpsaved[0] |= (FPRS_FEF | FPRS_DL | FPRS_DU);
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_READTEXT:
case PTRACE_READDATA: {
unsigned char *dest = (unsigned char *) addr2;
unsigned long src = addr;
int len = data, curlen;
struct vm_area_struct *vma;
pte_t *pgtable;
unsigned long page;
while(len) {
down(&child->mm->mmap_sem);
vma = find_extend_vma(child, src);
if (!vma) {
up(&child->mm->mmap_sem);
pt_error_return(regs, EIO);
goto flush_and_out;
}
pgtable = ptrace_get_page (child, vma, src, 0);
up(&child->mm->mmap_sem);
if (src & ~PAGE_MASK) {
curlen = PAGE_SIZE - (src & ~PAGE_MASK);
if (curlen > len) curlen = len;
} else if (len > PAGE_SIZE)
curlen = PAGE_SIZE;
else
curlen = len;
if (pgtable && MAP_NR(page = pte_page(*pgtable)) < max_mapnr) {
if (copy_to_user (dest, ((char *)page) + (src & ~PAGE_MASK), curlen)) {
flush_page_to_ram(page);
pt_error_return(regs, EFAULT);
goto flush_and_out;
}
flush_page_to_ram(page);
} else {
if (clear_user (dest, curlen)) {
pt_error_return(regs, EFAULT);
goto flush_and_out;
}
}
src += curlen;
dest += curlen;
len -= curlen;
}
pt_succ_return(regs, 0);
goto flush_and_out;
}
case PTRACE_WRITETEXT:
case PTRACE_WRITEDATA: {
unsigned char *src = (unsigned char *) addr2;
unsigned long dest = addr;
int len = data, curlen;
struct vm_area_struct *vma;
pte_t *pgtable;
unsigned long page;
while(len) {
down(&child->mm->mmap_sem);
vma = find_extend_vma(child, dest);
if (!vma) {
up(&child->mm->mmap_sem);
pt_error_return(regs, EIO);
goto flush_and_out;
}
pgtable = ptrace_get_page (child, vma, dest, 1);
up(&child->mm->mmap_sem);
if (dest & ~PAGE_MASK) {
curlen = PAGE_SIZE - (dest & ~PAGE_MASK);
if (curlen > len) curlen = len;
} else if (len > PAGE_SIZE)
curlen = PAGE_SIZE;
else
curlen = len;
if (pgtable && MAP_NR(page = pte_page(*pgtable)) < max_mapnr) {
flush_cache_page(vma, dest);
if (copy_from_user (((char *)page) + (dest & ~PAGE_MASK), src, curlen)) {
flush_page_to_ram(page);
set_pte(pgtable, pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
flush_tlb_page(vma, dest);
pt_error_return(regs, EFAULT);
goto flush_and_out;
}
flush_page_to_ram(page);
set_pte(pgtable, pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
flush_tlb_page(vma, dest);
}
src += curlen;
dest += curlen;
len -= curlen;
}
pt_succ_return(regs, 0);
goto flush_and_out;
}
case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
addr = 1;
case PTRACE_CONT: { /* restart after signal. */
if (data > _NSIG) {
pt_error_return(regs, EIO);
goto out;
}
if (addr != 1) {
if (addr & 3) {
pt_error_return(regs, EINVAL);
goto out;
}
#ifdef DEBUG_PTRACE
printk ("Original: %016lx %016lx\n", child->tss.kregs->tpc, child->tss.kregs->tnpc);
printk ("Continuing with %016lx %016lx\n", addr, addr+4);
#endif
child->tss.kregs->tpc = addr;
child->tss.kregs->tnpc = addr + 4;
}
if (request == PTRACE_SYSCALL)
child->flags |= PF_TRACESYS;
else
child->flags &= ~PF_TRACESYS;
child->exit_code = data;
#ifdef DEBUG_PTRACE
printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child->comm,
child->pid, child->exit_code,
child->tss.kregs->tpc,
child->tss.kregs->tnpc);
#endif
wake_up_process(child);
pt_succ_return(regs, 0);
goto out;
}
/*
* make the child exit. Best I can do is send it a sigkill.
* perhaps it should be put in the status that it wants to
* exit.
*/
case PTRACE_KILL: {
if (child->state == TASK_ZOMBIE) { /* already dead */
pt_succ_return(regs, 0);
goto out;
}
child->exit_code = SIGKILL;
wake_up_process(child);
pt_succ_return(regs, 0);
goto out;
}
case PTRACE_SUNDETACH: { /* detach a process that was attached. */
unsigned long flags;
if ((unsigned long) data > _NSIG) {
pt_error_return(regs, EIO);
goto out;
}
child->flags &= ~(PF_PTRACED|PF_TRACESYS);
child->exit_code = data;
write_lock_irqsave(&tasklist_lock, flags);
REMOVE_LINKS(child);
child->p_pptr = child->p_opptr;
SET_LINKS(child);
write_unlock_irqrestore(&tasklist_lock, flags);
wake_up_process(child);
pt_succ_return(regs, 0);
goto out;
}
/* PTRACE_DUMPCORE unsupported... */
default:
pt_error_return(regs, EIO);
goto out;
}
flush_and_out:
{
unsigned long va;
for(va = 0; va < (PAGE_SIZE << 1); va += 32)
spitfire_put_dcache_tag(va, 0x0);
}
out:
unlock_kernel();
}
Generated by GNU enscript 1.6.4.