/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Eric Haszlakiewicz.
*
* 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.
*
* Based on sys/arch/i386/i386/linux_machdep.c:
* linux_machdep.c,v 1.42 1998/09/11 12:50:06 mycroft Exp
* written by Frank van der Linden
*
*/
setregs(l, epp, stack);
#ifdef DEBUG
/*
* Linux has registers set to zero on entry; for DEBUG kernels
* the alpha setregs() fills registers with 0xbabefacedeadbeef.
*/
memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
#endif
}
void
setup_linux_rt_sigframe(struct trapframe *tf, const ksiginfo_t *ksi,
const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct linux_rt_sigframe *sfp, sigframe;
int onstack, error;
int fsize, rndfsize;
int sig = ksi->ksi_signo;
extern char linux_rt_sigcode[], linux_rt_esigcode[];
/* 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. */
fsize = sizeof(struct linux_rt_sigframe);
rndfsize = ((fsize + 15) / 16) * 16;
/*
* Build the signal context to be used by sigreturn.
*/
memset(&sigframe.uc, 0, sizeof(struct linux_ucontext));
sigframe.uc.uc_mcontext.sc_onstack = onstack;
/* Setup potentially partial signal mask in sc_mask. */
/* But get all of it in uc_sigmask */
native_to_linux_old_sigset(&sigframe.uc.uc_mcontext.sc_mask, mask);
native_to_linux_sigset(&sigframe.uc.uc_sigmask, mask);
if (error != 0) {
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sendsig(%d): copyout failed on sig %d\n",
p->p_pid, sig);
#endif
/*
* Process has trashed its stack; give it an illegal
* instruction to halt it in its tracks.
*/
sigexit(l, SIGILL);
/* NOTREACHED */
}
/* Pass pointers to siginfo and ucontext in the regs */
tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info;
tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc;
/* Address of trampoline code. End up at this PC after mi_switch */
tf->tf_regs[FRAME_PC] =
(u_int64_t)(p->p_psstrp - (linux_rt_esigcode - linux_rt_sigcode));
/* Adjust the stack */
alpha_pal_wrusp((unsigned long)sfp);
/* Remember that we're now on the signal stack. */
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}
void setup_linux_sigframe(struct trapframe *tf, const ksiginfo_t *ksi,
const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct linux_sigframe *sfp, sigframe;
int onstack, error;
int fsize, rndfsize;
int sig = ksi->ksi_signo;
extern char linux_sigcode[], linux_esigcode[];
/* 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. */
fsize = sizeof(struct linux_sigframe);
rndfsize = ((fsize + 15) / 16) * 16;
if (error != 0) {
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sendsig(%d): copyout failed on sig %d\n",
p->p_pid, sig);
#endif
/*
* Process has trashed its stack; give it an illegal
* instruction to halt it in its tracks.
*/
sigexit(l, SIGILL);
/* NOTREACHED */
}
/* Pass pointers to sigcontext in the regs */
tf->tf_regs[FRAME_A1] = 0;
tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc;
/* Address of trampoline code. End up at this PC after mi_switch */
tf->tf_regs[FRAME_PC] =
(u_int64_t)(p->p_psstrp - (linux_esigcode - linux_sigcode));
/* Adjust the stack */
alpha_pal_wrusp((unsigned long)sfp);
/* Remember that we're now on the signal stack. */
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}
/*
* Send an interrupt to process.
*
* Stack is set up to allow sigcode stored
* in u. to call routine, followed by kcall
* to sigreturn routine below. After sigreturn
* resets the signal mask, the stack, and the
* frame pointer, it returns to the user
* specified pc, psl.
*/
void
linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct trapframe *tf = l->l_md.md_tf;
const int sig = ksi->ksi_signo;
sig_t catcher = SIGACTION(p, sig).sa_handler;
#ifdef notyet
struct linux_emuldata *edp;
/* Setup the signal frame (and part of the trapframe) */
/*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/
/* XXX XAX this is broken now. need someplace to store what
XXX XAX kind of signal handler a signal has.*/
#if 0
edp = (struct linux_emuldata *)p->p_emuldata;
#else
edp = 0;
#endif
if (edp && sigismember(&edp->ps_siginfo, sig))
setup_linux_rt_sigframe(tf, ksi, mask);
else
#endif /* notyet */
setup_linux_sigframe(tf, ksi, mask);
/* Signal handler for trampoline code */
tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
tf->tf_regs[FRAME_A0] = native_to_linux_signo[sig];
/*
* Linux has a custom restorer option. To support it we would
* need to store an array of restorers and a sigcode block
* which knew to use it. Doesn't seem worth the trouble.
* -erh
*/
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid,
tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid)
printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig);
#endif
}
/*
* System call to cleanup state after a signal
* has been taken. Reset signal mask and
* stack state from context left by sendsig (above).
* Return to previous pc as specified by context
* left by sendsig.
* Linux real-time signals use a different sigframe,
* but the sigcontext is the same.
*/
/*
* Linux doesn't (yet) have alternate signal stacks.
* However, the OSF/1 sigcontext which they use has
* an onstack member. This could be needed in the future.
*/
mutex_enter(p->p_lock);
if (context.sc_onstack & LINUX_SA_ONSTACK)
l->l_sigstk.ss_flags |= SS_ONSTACK;
else
l->l_sigstk.ss_flags &= ~SS_ONSTACK;
/* Reset the signal mask */
(void) sigprocmask1(l, SIG_SETMASK, mask, 0);
mutex_exit(p->p_lock);
/*
* Check for security violations.
* Linux doesn't allow any changes to the PSL.
*/
if (context.sc_ps != ALPHA_PSL_USERMODE)
return(EINVAL);
/*
* The trampoline code hands us the context.
* It is unsafe to keep track of it ourselves, in the event that a
* program jumps out of a signal handler.
*/
sfp = SCARG(uap, sfp);
if (ALIGN(sfp) != (u_int64_t)sfp)
return(EINVAL);
/*
* Fetch the frame structure.
*/
if (copyin((void *)sfp, &sigframe,
sizeof(struct linux_rt_sigframe)) != 0)
return (EFAULT);
/* Grab the signal mask */
linux_to_native_sigset(&mask, &sigframe.uc.uc_sigmask);
/*
* The trampoline code hands us the context.
* It is unsafe to keep track of it ourselves, in the event that a
* program jumps out of a signal handler.
*/
sfp = SCARG(uap, sfp);
if (ALIGN(sfp) != (u_int64_t)sfp)
return(EINVAL);
/*
* Fetch the frame structure.
*/
if (copyin((void *)sfp, &frame, sizeof(struct linux_sigframe)) != 0)
return(EFAULT);
/* Grab the signal mask. */
/* XXX use frame.extramask */
linux_old_to_native_sigset(&mask, frame.sf_sc.sc_mask);