/*-
* Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matthias Scheler and Emmanuel Dreyfus.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <lib/libkern/libkern.h> /* for offsetof() */
/*
* From Linux's include/asm-ppc/ptrace.h.
* structure used for storing reg context: defined in linux_machdep.h
* structure used for storing floating point context:
* Linux just uses a double fpr[32], no struct
*/
/*
* user struct for linux process - this is used for Linux ptrace emulation
* most of it is junk only used by gdb
*
* From Linux's include/asm-ppc/user.h
*
* XXX u_ar0 was a struct reg in Linux/powerpc
* Can't find out what a struct regs is for Linux/powerpc,
* so we use a struct pt_regs instead. don't know if this is right.
*/
struct linux_user {
struct linux_pt_regs regs;
#define lusr_startgdb regs
size_t u_tsize;
size_t u_dsize;
size_t u_ssize;
unsigned long start_code;
unsigned long start_data;
unsigned long start_stack;
long int signal;
struct linux_pt_regs *u_ar0; /* help gdb find registers */
unsigned long magic;
char u_comm[32];
#define lu_comm_end u_comm[31]
};
switch (request) {
case LINUX_PTRACE_PEEKUSR:
case LINUX_PTRACE_POKEUSR:
regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
break;
case LINUX_PTRACE_GETREGS:
case LINUX_PTRACE_SETREGS:
regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
linux_regs = kmem_alloc(sizeof(*linux_regs), KM_SLEEP);
if (request == LINUX_PTRACE_SETREGS) {
error = copyin((void *)SCARG(uap, data), linux_regs,
sizeof(struct linux_pt_regs));
if (error) {
goto out;
}
}
break;
case LINUX_PTRACE_GETFPREGS:
case LINUX_PTRACE_SETFPREGS:
fpregs = kmem_alloc(sizeof(struct fpreg), KM_SLEEP);
linux_fpreg = kmem_alloc(32 * sizeof(double), KM_SLEEP);
if (request == LINUX_PTRACE_SETFPREGS) {
error = copyin((void *)SCARG(uap, data), linux_fpreg,
32 * sizeof(double));
if (error) {
goto out;
}
}
break;
default:
error = EIO;
goto out;
}
/* Find the process we are supposed to be operating on. */
mutex_enter(&proc_lock);
if ((t = proc_find(SCARG(uap, pid))) == NULL) {
mutex_exit(&proc_lock);
error = ESRCH;
goto out;
}
mutex_enter(t->p_lock);
/*
* You cannot do what you want to the process if:
* 1. It is not being traced at all,
*/
if (!ISSET(t->p_slflag, PSL_TRACED)) {
mutex_exit(t->p_lock);
mutex_exit(&proc_lock);
error = EPERM;
goto out;
}
/*
* 2. It is not being traced by _you_, or
* 3. It is not currently stopped.
*/
if (t->p_pptr != p || t->p_stat != SSTOP || !t->p_waited) {
mutex_exit(t->p_lock);
mutex_exit(&proc_lock);
error = EBUSY;
goto out;
}
mutex_exit(&proc_lock);
/* XXX: ptrace needs revamp for multi-threading support. */
if (t->p_nlwps > 1) {
mutex_exit(t->p_lock);
error = ENOSYS;
goto out;
}
lt = LIST_FIRST(&t->p_lwps);
*retval = 0;