/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matthias Scheler.
*
* 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() */
struct linux_reg {
long ebx;
long ecx;
long edx;
long esi;
long edi;
long ebp;
long eax;
long xds, xes; /* unsigned short ds, __ds, es, __es; */
long __fs, __gs; /* unsigned short fs, __fs, gs, __gs; */
long orig_eax;
long eip;
long xcs; /* unsigned short cs, __cs; */
long eflags;
long esp;
long xss; /* unsigned short ss, __ss; */
};
/* structure used for storing floating point context */
struct linux_fpctx {
long cwd;
long swd;
long twd;
long fip;
long fcs;
long foo;
long fos;
long st_space[20];
};
/* user struct for linux process - this is used for Linux ptrace emulation */
/* most of it is junk only used by gdb */
struct linux_user {
struct linux_reg regs; /* registers */
int u_fpvalid; /* true if math co-processor being used. */
struct linux_fpctx i387; /* Math Co-processor registers. */
/* The rest of this junk is to help gdb figure out what goes where */
#define lusr_startgdb u_tsize
unsigned long int u_tsize; /* Text segment size (pages). */
unsigned long int u_dsize; /* Data segment size (pages). */
unsigned long int u_ssize; /* Stack segment size (pages). */
unsigned long start_code; /* Starting virtual address of text. */
unsigned long start_stack; /* Starting virtual address of stack
area. This is actually the bottom of
the stack, the top of the stack is
always found in the esp register. */
long int __signal; /* Signal that caused the core dump. */
int __reserved; /* unused */
void *u_ar0; /* Used by gdb to help find the values
for the registers. */
struct linux_fpctx *u_fpstate; /* Math Co-processor pointer. */
unsigned long __magic; /* To uniquely identify a core file */
char u_comm[32]; /* User command that was responsible */
int u_debugreg[8];
#define u_debugreg_end u_debugreg[7]
};
switch (request) {
case LINUX_PTRACE_PEEKUSR:
case LINUX_PTRACE_POKEUSR:
break;
case LINUX_PTRACE_GETREGS:
case LINUX_PTRACE_SETREGS:
regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP);
if (request == LINUX_PTRACE_SETREGS) {
error = copyin((void *)SCARG(uap, data), linux_regs,
sizeof(struct linux_reg));
if (error) {
goto out;
}
}
break;
case LINUX_PTRACE_GETFPREGS:
case LINUX_PTRACE_SETFPREGS:
fpregs = kmem_alloc(sizeof(struct fpreg), KM_SLEEP);
linux_fpregs = kmem_alloc(sizeof(struct linux_fpctx), KM_SLEEP);
if (request == LINUX_PTRACE_SETFPREGS) {
error = copyin((void *)SCARG(uap, data), linux_fpregs,
sizeof(struct linux_fpctx));
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;
case LINUX_PTRACE_POKEUSR:
/* we only support setting debugregs for now */
addr = SCARG(uap, addr);
if (addr >= LUSR_OFF(u_debugreg) &&
addr <= LUSR_OFF(u_debugreg_end)) {
int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
int data = SCARG(uap, data);
/* only do this for Linux processes */
if (t->p_emul != &emul_linux) {
mutex_exit(t->p_lock);
return EINVAL;
}
led = lt->l_emuldata;
led->led_debugreg[off] = data;
}
mutex_exit(t->p_lock);
break;
default:
mutex_exit(t->p_lock);
break;
}
out:
if (regs)
kmem_free(regs, sizeof(*regs));
if (linux_regs)
kmem_free(linux_regs, sizeof(*linux_regs));
if (fpregs)
kmem_free(fpregs, sizeof(*fpregs));
if (linux_fpregs)
kmem_free(linux_fpregs, sizeof(*linux_fpregs));