#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/11/06 20:42:56 thorpej Exp $");
#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
#include "opt_modular.h"
#include "opt_execfmt.h"
#include "firm_events.h"
#endif
#include <sys/param.h>
#include <sys/exec.h>
#include <sys/exec_aout.h>
#include <sys/filedesc.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/systm.h>
#include <sys/core.h>
#include <sys/mount.h>
#include <sys/buf.h>
#include <sys/vnode.h>
#include <sys/select.h>
#include <sys/socketvar.h>
#include <sys/ucontext.h>
#include <sys/ioctl.h>
#include <sys/kmem.h>
#include <dev/sun/event_var.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/igmp.h>
#include <netinet/igmp_var.h>
#include <netinet/ip_mroute.h>
#include <compat/netbsd32/netbsd32.h>
#include <compat/netbsd32/netbsd32_ioctl.h>
#include <compat/netbsd32/netbsd32_syscallargs.h>
#include <compat/netbsd32/netbsd32_exec.h>
#include <compat/sys/signal.h>
#include <compat/sys/signalvar.h>
#include <compat/sys/siginfo.h>
#include <compat/sys/ucontext.h>
#include <machine/frame.h>
#include <machine/pcb.h>
#include <machine/reg.h>
#include <machine/vmparam.h>
#include <machine/vuid_event.h>
#include <machine/netbsd32_machdep.h>
#include <machine/userret.h>
struct sparc32_sigframe {
int sf_signo;
int sf_code;
u_int sf_scp;
int sf_addr;
struct netbsd32_sigcontext sf_sc;
};
#undef DEBUG
#ifdef DEBUG
extern int sigdebug;
#endif
static void
netbsd32_sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
{
int sig = ksi->ksi_signo;
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct sparc32_sigframe *fp;
struct trapframe64 *tf;
int addr, onstack, error;
struct rwindow32 *oldsp, *newsp;
register32_t sp;
sig_t catcher = SIGACTION(p, sig).sa_handler;
struct sparc32_sigframe sf;
extern char netbsd32_sigcode[], netbsd32_esigcode[];
#define szsigcode (netbsd32_esigcode - netbsd32_sigcode)
tf = l->l_md.md_tf;
oldsp = (struct rwindow32 *)(u_long)(u_int)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 sparc32_sigframe *)((char *)l->l_sigstk.ss_sp +
l->l_sigstk.ss_size);
l->l_sigstk.ss_flags |= SS_ONSTACK;
} else
fp = (struct sparc32_sigframe *)oldsp;
fp = (struct sparc32_sigframe *)((u_long)(fp - 1) & ~7);
#ifdef DEBUG
sigpid = p->p_pid;
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
printf("sendsig: %s[%d] sig %d newusp %p scp %p oldsp %p\n",
p->p_comm, p->p_pid, sig, fp, &fp->sf_sc, oldsp);
if (sigdebug & SDB_DDB) Debugger();
}
#endif
memset(&sf, 0, sizeof(sf));
sf.sf_signo = sig;
sf.sf_code = (u_int)ksi->ksi_trap;
sf.sf_scp = (u_long)&fp->sf_sc;
sf.sf_addr = 0;
sf.sf_sc.sc_onstack = onstack;
sf.sf_sc.sc_mask = *mask;
sf.sf_sc.sc_sp = (u_long)oldsp;
sf.sf_sc.sc_pc = tf->tf_pc;
sf.sf_sc.sc_npc = tf->tf_npc;
sf.sf_sc.sc_psr = TSTATECCR_TO_PSR(tf->tf_tstate);
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 = (struct rwindow32 *)((long)fp - sizeof(struct rwindow32));
write_user_windows();
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK))
printf("sendsig: saving sf to %p, setting stack pointer %p to %p\n",
fp, &(((struct rwindow32 *)newsp)->rw_in[6]), oldsp);
#endif
sp = NETBSD32PTR32I(oldsp);
error = (rwindow_save(l) ||
copyout(&sf, fp, sizeof sf) ||
copyout(&sp, &(((struct rwindow32 *)newsp)->rw_in[6]),
sizeof(sp)));
mutex_enter(p->p_lock);
if (error) {
#ifdef DEBUG
mutex_exit(p->p_lock);
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sendsig: window save or copyout error\n");
printf("sendsig: stack was trashed trying to send sig %d, sending SIGILL\n", sig);
if (sigdebug & SDB_DDB) Debugger();
mutex_enter(p->p_lock);
#endif
sigexit(l, SIGILL);
}
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW) {
printf("sendsig: %s[%d] sig %d scp %p\n",
p->p_comm, p->p_pid, sig, &fp->sf_sc);
}
#endif
addr = p->p_psstrp - szsigcode;
tf->tf_global[1] = (long)catcher;
tf->tf_pc = addr;
tf->tf_npc = addr + 4;
tf->tf_out[6] = (uint64_t)(u_int)(u_long)newsp;
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
mutex_exit(p->p_lock);
printf("sendsig: about to return to catcher %p thru %p\n",
catcher, addr);
if (sigdebug & SDB_DDB) Debugger();
mutex_enter(p->p_lock);
}
#endif
}
struct sparc32_sigframe_siginfo {
siginfo32_t sf_si;
ucontext32_t sf_uc;
};
#undef DEBUG
int
compat_16_netbsd32___sigreturn14(struct lwp *l, const struct compat_16_netbsd32___sigreturn14_args *uap, register_t *retval)
{
struct netbsd32_sigcontext sc, *scp;
struct trapframe64 *tf;
struct proc *p = l->l_proc;
write_user_windows();
if (rwindow_save(l)) {
#ifdef DEBUG
printf("netbsd32_sigreturn14: rwindow_save(%p) failed, sending SIGILL\n", p);
Debugger();
#endif
mutex_enter(p->p_lock);
sigexit(l, SIGILL);
}
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW) {
printf("netbsd32_sigreturn14: %s[%d], sigcntxp %p\n",
p->p_comm, p->p_pid, SCARG(uap, sigcntxp));
if (sigdebug & SDB_DDB) Debugger();
}
#endif
scp = (struct netbsd32_sigcontext *)(u_long)SCARG(uap, sigcntxp);
if ((vaddr_t)scp & 3 || (copyin((void *)scp, &sc, sizeof sc) != 0))
{
#ifdef DEBUG
printf("netbsd32_sigreturn14: copyin failed: scp=%p\n", scp);
Debugger();
#endif
return (EINVAL);
}
scp = ≻
tf = l->l_md.md_tf;
if (((sc.sc_pc | sc.sc_npc) & 3) != 0 || (sc.sc_pc == 0) || (sc.sc_npc == 0))
#ifdef DEBUG
{
printf("netbsd32_sigreturn14: pc %p or npc %p invalid\n", sc.sc_pc, sc.sc_npc);
Debugger();
return (EINVAL);
}
#else
return (EINVAL);
#endif
tf->tf_tstate = (int64_t)(tf->tf_tstate & ~TSTATE_CCR) | PSRCC_TO_TSTATE(sc.sc_psr);
tf->tf_pc = (int64_t)sc.sc_pc;
tf->tf_npc = (int64_t)sc.sc_npc;
tf->tf_global[1] = (int64_t)sc.sc_g1;
tf->tf_out[0] = (int64_t)sc.sc_o0;
tf->tf_out[6] = (int64_t)sc.sc_sp;
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW) {
printf("netbsd32_sigreturn14: return trapframe pc=%p sp=%p tstate=%llx\n",
(vaddr_t)tf->tf_pc, (vaddr_t)tf->tf_out[6], tf->tf_tstate);
if (sigdebug & SDB_DDB) Debugger();
}
#endif
mutex_enter(p->p_lock);
if (sc.sc_onstack & SS_ONSTACK)
l->l_sigstk.ss_flags |= SS_ONSTACK;
else
l->l_sigstk.ss_flags &= ~SS_ONSTACK;
(void) sigprocmask1(l, SIG_SETMASK, &sc.sc_mask, 0);
mutex_exit(p->p_lock);
return (EJUSTRETURN);
}
void
netbsd32_machdep_md_16_init(void)
{
MODULE_HOOK_SET(netbsd32_sendsig_sigcontext_16_hook,
netbsd32_sendsig_sigcontext);
}
void
netbsd32_machdep_md_16_fini(void)
{
MODULE_HOOK_UNSET(netbsd32_sendsig_sigcontext_16_hook);
}