#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sunos_machdep.c,v 1.40 2023/12/20 00:40:43 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/filedesc.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/kernel.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/buf.h>
#include <sys/syscallargs.h>
#include <compat/sunos/sunos.h>
#include <compat/sunos/sunos_syscallargs.h>
#include <compat/sys/signal.h>
#include <compat/sys/signalvar.h>
#include <machine/reg.h>
#ifdef DEBUG
extern int sigdebug;
extern int sigpid;
#define SDB_FOLLOW 0x01
#define SDB_KSTACK 0x02
#define SDB_FPSTATE 0x04
#endif
struct sunos_sigcontext {
int sc_onstack;
int sc_mask;
int sc_sp;
int sc_pc;
int sc_ps;
};
struct sunos_sigframe {
int sf_signum;
int sf_code;
struct sunos_sigcontext *sf_scp;
u_int sf_addr;
struct sunos_sigcontext sf_sc;
};
void
sunos_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
{
u_long code = KSI_TRAPCODE(ksi);
int sig = ksi->ksi_signo;
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct frame *frame = (struct frame *)l->l_md.md_regs;
int onstack, error;
struct sunos_sigframe *fp = getframe(l, sig, &onstack), kf;
sig_t catcher = SIGACTION(p, sig).sa_handler;
short ft = frame->f_format;
if (ft >= FMT9) {
SIGACTION(p, sig).sa_handler = SIG_DFL;
sigdelset(&p->p_sigctx.ps_sigignore, sig);
sigdelset(&p->p_sigctx.ps_sigcatch, sig);
sigdelset(&l->l_sigmask, sig);
mutex_exit(p->p_lock);
psignal(p, sig);
mutex_enter(p->p_lock);
return;
}
fp--;
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sunos_sendsig(%d): sig %d ssp %p usp %p scp %p ft %d\n",
p->p_pid, sig, &onstack, fp, &fp->sf_sc, ft);
#endif
kf.sf_signum = sig;
kf.sf_code = code;
kf.sf_scp = &fp->sf_sc;
kf.sf_addr = ~0;
kf.sf_sc.sc_sp = frame->f_regs[SP];
kf.sf_sc.sc_pc = frame->f_pc;
kf.sf_sc.sc_ps = frame->f_sr;
kf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
native_sigset_to_sigset13(mask, &kf.sf_sc.sc_mask);
sendsig_reset(l, sig);
mutex_exit(p->p_lock);
error = copyout(&kf, fp, sizeof(kf));
mutex_enter(p->p_lock);
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
sigexit(l, SIGILL);
}
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sunos_sendsig(%d): sig %d scp %p sc_sp %x\n",
p->p_pid, sig, &fp->sf_sc,kf.sf_sc.sc_sp);
#endif
buildcontext(l, catcher, fp);
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sunos_sendsig(%d): sig %d returns\n",
p->p_pid, sig);
#endif
}
int
sunos_sys_sigreturn(struct lwp *l, const struct sunos_sys_sigreturn_args *uap, register_t *retval)
{
struct proc *p = l->l_proc;
struct sunos_sigcontext *scp;
struct frame *frame;
struct sunos_sigcontext tsigc;
sigset_t mask;
scp = (struct sunos_sigcontext *) SCARG(uap, sigcntxp);
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sunos_sigreturn: pid %d, scp %p\n", p->p_pid, scp);
#endif
if ((int)scp & 1)
return EINVAL;
if (copyin((void *)scp, (void *)&tsigc, sizeof(tsigc)) != 0)
return EFAULT;
scp = &tsigc;
if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
return EINVAL;
frame = (struct frame *) l->l_md.md_regs;
frame->f_regs[SP] = scp->sc_sp;
frame->f_pc = scp->sc_pc;
frame->f_sr = scp->sc_ps;
mutex_enter(p->p_lock);
if (scp->sc_onstack & SS_ONSTACK)
l->l_sigstk.ss_flags |= SS_ONSTACK;
else
l->l_sigstk.ss_flags &= ~SS_ONSTACK;
native_sigset13_to_sigset(&scp->sc_mask, &mask);
(void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
mutex_exit(p->p_lock);
return EJUSTRETURN;
}