#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sunos_machdep.c,v 1.25 2025/04/24 01:52:38 riastradh Exp $");
#if defined(_KERNEL_OPT)
#include "opt_compat_netbsd.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/mount.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <compat/sys/signal.h>
#include <compat/sys/signalvar.h>
#include <sys/syscallargs.h>
#include <compat/sunos/sunos.h>
#include <compat/sunos/sunos_syscallargs.h>
#include <machine/frame.h>
#include <machine/cpu.h>
#ifdef DEBUG
int sunos_sigdebug = 0;
int sunos_sigpid = 0;
#define SDB_FOLLOW 0x01
#define SDB_KSTACK 0x02
#define SDB_FPSTATE 0x04
#endif
struct sunos_sigframe {
int sf_signo;
int sf_code;
struct sigcontext13 *sf_scp;
int sf_addr;
struct sigcontext13 sf_sc;
};
void sunos_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct sunos_sigframe *fp;
struct trapframe *tf;
int addr, onstack, oldsp, newsp, error;
int sig = ksi->ksi_signo;
u_long code = ksi->ksi_code;
sig_t catcher = SIGACTION(p, sig).sa_handler;
struct sunos_sigframe sf;
tf = l->l_md.md_tf;
oldsp = tf->tf_out[6];
onstack =
(l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
(SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
if (onstack)
fp = (struct sunos_sigframe *)
((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
else
fp = (struct sunos_sigframe *)oldsp;
fp = (struct sunos_sigframe *)((int)(fp - 1) & ~STACK_ALIGNBYTES);
#ifdef DEBUG
if ((sunos_sigdebug & SDB_KSTACK) && p->p_pid == sunos_sigpid)
printf("sendsig: %s[%d] sig %d newusp %p scp %p\n",
p->p_comm, p->p_pid, sig, fp, &fp->sf_sc);
#endif
sf.sf_signo = sig;
sf.sf_code = code;
sf.sf_scp = &fp->sf_sc;
sf.sf_addr = 0;
sf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
native_sigset_to_sigset13(mask, &sf.sf_sc.sc_mask);
sf.sf_sc.sc_sp = oldsp;
sf.sf_sc.sc_pc = tf->tf_pc;
sf.sf_sc.sc_npc = tf->tf_npc;
sf.sf_sc.sc_psr = tf->tf_psr;
sf.sf_sc.sc_g1 = tf->tf_global[1];
sf.sf_sc.sc_o0 = tf->tf_out[0];
sendsig_reset(l, sig);
mutex_exit(p->p_lock);
newsp = (int)fp - sizeof(struct rwindow);
write_user_windows();
error = (rwindow_save(l) || copyout((void *)&sf, (void *)fp, sizeof sf) ||
ustore_int((u_int *)&((struct rwindow *)newsp)->rw_in[6], oldsp));
mutex_enter(p->p_lock);
if (error) {
#ifdef DEBUG
if ((sunos_sigdebug & SDB_KSTACK) && p->p_pid == sunos_sigpid)
printf("sendsig: window save or copyout error\n");
#endif
sigexit(l, SIGILL);
}
#ifdef DEBUG
if (sunos_sigdebug & SDB_FOLLOW)
printf("sendsig: %s[%d] sig %d scp %p\n",
p->p_comm, p->p_pid, sig, &fp->sf_sc);
#endif
addr = (int)catcher;
tf->tf_pc = addr;
tf->tf_npc = addr + 4;
tf->tf_out[6] = newsp;
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
#ifdef DEBUG
if ((sunos_sigdebug & SDB_KSTACK) && p->p_pid == sunos_sigpid)
printf("sendsig: about to return to catcher\n");
#endif
}
int
sunos_sys_sigreturn(struct lwp *l, const struct sunos_sys_sigreturn_args *uap, register_t *retval)
{
return (compat_13_sys_sigreturn(l,
(const struct compat_13_sys_sigreturn_args *)uap, retval));
}