OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [arch/] [or32/] [kernel/] [ptrace.c] - Rev 1765

Compare with Previous | Blame | View Log

/*
 *  linux/arch/or32/kernel/ptrace.c
 *
 *  or32 version
 *    author(s): Matjaz Breskvar (phoenix@opencores.org)
 *
 *  derived from cris, i386, m68k, ppc, sh ports.
 *
 *  changes:
 *  18. 11. 2003: Matjaz Breskvar (phoenix@opencores.org)
 *    initial port to or32 architecture
 *  10. 11. 2005: Gyorgy Jeney (nog@sdf.lonestar.org)
 *    Added actual ptrace implementation except for single step traceing
 *    (Basically copy arch/i386/kernel/ptrace.c)
 *
 *  Based on:
 *
 *  linux/arch/m68k/kernel/ptrace
 *
 *  Copyright (C) 1994 by Hamish Macdonald
 *  Taken from linux/kernel/ptrace.c and modified for M680x0.
 *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License.  See the file COPYING in the main directory of
 * this archive for more details.
 */
 
#include <stddef.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
 
#include <linux/mm.h>
#include <linux/smp_lock.h>
#include <linux/errno.h>
#include <linux/ptrace.h>
#include <linux/user.h>
 
#include <asm/segment.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/system.h>
 
/*
 * does not yet catch signals sent when the child dies.
 * in exit.c or in signal.c.
 */
 
/*
 * Get contents of register REGNO in task TASK.
 */
static inline long get_reg(struct task_struct *task, int regno)
{
	if(regno < sizeof(struct pt_regs))
		return *((unsigned long *)user_regs(task) + (regno >> 2));
	switch(regno) {
	case offsetof(struct user, start_code):
		return task->mm->start_code;
	case offsetof(struct user, start_data):
		return task->mm->start_data;
	case offsetof(struct user, start_stack):
		return task->mm->start_stack;
	default:
		printk("Don't know how to get offset %i of struct user\n",
		       regno);
	}
	return 0;
}
 
/*
 * Write contents of register REGNO in task TASK.
 */
static inline int put_reg(struct task_struct *task, int regno,
			  unsigned long data)
{
	/* Whatever happens, don't let any process the the sr flags and elevate
	 * their privaleges to kernel mode. */
	if((regno < sizeof(struct pt_regs)) &&
	   (regno != offsetof(struct pt_regs, sr))) {
		*((unsigned long *)task->thread.regs + (regno >> 2)) = data;
		return 0;
	}
	return -EIO;
}
 
/*
 * Called by kernel/ptrace.c when detaching..
 *
 * Make sure the single step bit is not set.
 */
void ptrace_disable(struct task_struct *child)
{
	printk("ptrace_disable(): TODO\n");
	/* Todo - pending singlesteps? */
}
 
asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
{
	struct task_struct *child;
	int ret;
 
	lock_kernel();
	ret = -EPERM;
	if (request == PTRACE_TRACEME) {
		/* are we already being traced? */
		if (current->ptrace & PT_PTRACED)
			goto out;
		/* set the ptrace bit in the process flags. */
		current->ptrace |= PT_PTRACED;
		ret = 0;
		goto out;
	}
 
	ret = -ESRCH;
	read_lock(&tasklist_lock);
	child = find_task_by_pid(pid);
	if (child)
		get_task_struct(child);
	read_unlock(&tasklist_lock);
	if (!child)
		goto out;
 
	ret = -EPERM;
	if (pid == 1)		/* you may not mess with init */
		goto out_tsk;
	if (request == PTRACE_ATTACH) {
		ret = ptrace_attach(child);
		goto out_tsk;
	}
 
	ret = ptrace_check_attach(child, request == PTRACE_KILL);
	if (ret < 0)
		goto out_tsk;
 
	switch (request) {
	/* when I and D space are separate, these will need to be fixed. */
	case PTRACE_PEEKTEXT: /* read word at location addr. */ 
	case PTRACE_PEEKDATA: {
		unsigned long tmp;
		int copied;
 
		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
		ret = -EIO;
		if (copied != sizeof(tmp))
			break;
		ret = put_user(tmp,(unsigned long *) data);
		break;
		}
 
	/* read the word at location addr in the USER area. */
	case PTRACE_PEEKUSR:
		if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
			return -EIO;
 
		ret = verify_area(VERIFY_WRITE, (void *) data,
				  sizeof(long));
		if (ret)
			break;
		put_user(get_reg(child, addr), (unsigned long *) data);
		break;
 
	/* when I and D space are separate, this will have to be fixed. */
	case PTRACE_POKETEXT: /* write the word at location addr. */
	case PTRACE_POKEDATA:
		ret = 0;
		if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
			break;
		ret = -EIO;
		break;
 
	case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
		if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
			return -EIO;
 
		ret = put_reg(child, addr, data);	    
		break;
 
	case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
	case PTRACE_CONT: { /* restart after signal. */
		long tmp;
		ret = -EIO;
		if ((unsigned long) data >= _NSIG)
			break;
		if (request == PTRACE_SYSCALL)
			child->ptrace |= PT_TRACESYS;
		else
			child->ptrace &= ~PT_TRACESYS;
		child->exit_code = data;
#if 0
 FIXME: The single step trace is set no-where so it will be clear here
			/* make sure the single step bit is not set. */
			tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
			put_reg(child, PT_SR, tmp);
#endif
		wake_up_process(child);
		ret = 0;
		break;
	}
 
/*
 * 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: {
		long tmp;
 
		ret = 0;
		if (child->state == TASK_ZOMBIE) /* already dead */
			break;
		child->exit_code = SIGKILL;
	/* make sure the single step bit is not set. */
#if 0
 FIXME: The single step trace is set no-where so it will be clear here
			tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
			put_reg(child, PT_SR, tmp);
#endif
		wake_up_process(child);
		break;
	}
 
	case PTRACE_SINGLESTEP: {  /* set the trap flag. */
		printk("SINGLESTEP: TODO\n");
		ret = -ENOSYS;
#if 0
		long tmp;
 
		if ((unsigned long) data >= _NSIG)
			return -EIO;
		child->ptrace &= ~PT_TRACESYS;
		tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
		put_reg(child, PT_SR, tmp);
 
		child->exit_code = data;
	/* give it a chance to run. */
		wake_up_process(child);
#endif
		break;
	}
 
	case PTRACE_DETACH: /* detach a process that was attached. */
		ret = ptrace_detach(child, data);
		break;
 
	default:
		ret = -EIO;
		break;
	}
out_tsk:
	free_task_struct(child);
out:
	unlock_kernel();
	return ret;
}
 
asmlinkage void syscall_trace(void)
{
   if ((current->ptrace & (PT_PTRACED | PT_TRACESYS)) !=
       (PT_PTRACED | PT_TRACESYS))
     return;
   /* TODO: make a way to distinguish between a syscall stop and SIGTRAP
    *          * delivery like in the i386 port ?
    *          */
   current->exit_code = SIGTRAP;
   current->state = TASK_STOPPED;
   notify_parent(current, SIGCHLD);
   schedule();
   /*
    *          * this isn't the same as continuing with a signal, but it will do
    *          * for normal use.  strace only continues with a signal if the
    *          * stopping signal is not SIGTRAP.  -brl
    *          */
   if (current->exit_code) {
 
      send_sig(current->exit_code, current, 1);
      current->exit_code = 0;
   }
 
}
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.