/*-
* Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Emmanuel Dreyfus
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR 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 AUTHOR 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.
*/
/* Do we need to jump onto the signal stack? */
onstack =
(l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
(SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
/* Allocate space for the signal handler context. */
if (onstack)
sp = ((char *)l->l_sigstk.ss_sp +
l->l_sigstk.ss_size);
else
sp = (char *)tf->tf_rsp - 128;
/* Save FPU state */
sp = (char *) (((long)sp - sizeof (*fpsp)) & ~0xfUL);
fpsp = (struct linux__fpstate *)sp;
/*
* The user context
*/
sigframe.uc.luc_flags = 0;
sigframe.uc.luc_link = NULL;
/* This is used regardless of SA_ONSTACK in Linux */
sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
sigframe.uc.luc_stack.ss_flags = 0;
if (l->l_sigstk.ss_flags & SS_ONSTACK)
sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
if (l->l_sigstk.ss_flags & SS_DISABLE)
sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
/*
* Save FPU state, if any
*/
if (fpsp != NULL) {
size_t fp_size = sizeof fpregs;
/* The netbsd and linux structures both match the fxsave data */
memset(&fpregs, 0, sizeof(fpregs));
(void)process_read_fpregs(l, &fpregs, &fp_size);
error = copyout(&fpregs, fpsp, sizeof(*fpsp));
}
if (error == 0)
error = copyout(&sigframe, sp, sizeof(sigframe));
mutex_enter(p->p_lock);
if (error != 0) {
sigexit(l, SIGILL);
return;
}
if ((vaddr_t)catcher >= VM_MAXUSER_ADDRESS) {
sigexit(l, SIGILL);
return;
}
/*
* Set the flags. Linux always have CPU, stack and signal state,
* FPU is optional. uc_flags is not used to tell what we have.
*/
uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
if (lsigctx->fpstate != NULL)
uctx.uc_flags |= _UC_FPU;
uctx.uc_link = NULL;
/*
* Signal set
*/
linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
/*
* FPU state
*/
if (lsigctx->fpstate != NULL) {
/* Both structures match the fxstate data */
error = copyin(lsigctx->fpstate, fxarea, sizeof(*fxarea));
if (error != 0) {
mutex_enter(l->l_proc->p_lock);
sigexit(l, SIGILL);
return error;
}
}
/*
* And the stack
*/
uctx.uc_stack.ss_flags = 0;
if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
uctx.uc_stack.ss_flags |= SS_ONSTACK;
if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
uctx.uc_stack.ss_flags |= SS_DISABLE;
/*
* And let setucontext deal with that.
*/
mutex_enter(l->l_proc->p_lock);
error = setucontext(l, &uctx);
mutex_exit(l->l_proc->p_lock);
if (error)
return error;
/*
* Check for a vsyscall. %rip must be the fault address,
* and the address must be in the Linux vsyscall area.
* Also, vsyscalls are only done at 1024-byte boundaries.
*/
if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
return 0;
if (trapaddr != tf->tf_rip)
return 0;
if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
return 0;
/*
* Get the return address from the top of the stack,
* and fix up the return address.
* This assumes the faulting instruction was callq *reg,
* which is the only way that vsyscalls are ever entered.
*/
if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
return 0;
if ((vaddr_t)retaddr >= VM_MAXUSER_ADDRESS)
return 0;
tf->tf_rip = retaddr;
tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
tf->tf_rsp += 8; /* "pop" the return address */