#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.18 2021/10/27 04:15:00 thorpej Exp $");
#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/syscallargs.h>
#include <machine/signal.h>
#include <machine/frame.h>
#if defined(COMPAT_16)
#include <compat/sys/signal.h>
#include <compat/sys/signalvar.h>
#ifdef DEBUG
#include <sparc64/sparc64/sigdebug.h>
#endif
#ifdef __arch64__
#define STACK_OFFSET BIAS
#define CPOUTREG(l,v) copyout(&(v), (l), sizeof(v))
#undef CCFSZ
#define CCFSZ CC64FSZ
#else
#define STACK_OFFSET 0
#define CPOUTREG(l,v) copyout(&(v), (l), sizeof(v))
#endif
struct sigframe_sigcontext {
int sf_signo;
int sf_code;
#ifndef __arch64__
struct sigcontext *sf_scp;
int sf_addr;
#endif
struct sigcontext sf_sc;
};
void
sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct sigacts *ps = p->p_sigacts;
const void *addr;
struct rwindow *newsp;
#ifdef NOT_DEBUG
struct rwindow tmpwin;
#endif
int onstack, error;
int sig = ksi->ksi_signo;
struct sigframe_sigcontext *fp = getframe(l, sig, &onstack);
struct sigframe_sigcontext sf;
sig_t catcher = SIGACTION(p, sig).sa_handler;
struct trapframe64 *tf = l->l_md.md_tf;
fp = (void *)((u_long)(fp - 1) & ~0x0f);
#ifdef DEBUG
sigpid = p->p_pid;
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
printf("sendsig_sigcontext: %s[%d] sig %d newusp %p scp %p\n",
p->p_comm, p->p_pid, sig, fp, &fp->sf_sc);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
}
#endif
sf.sf_signo = sig;
sf.sf_code = ksi->ksi_trap;
#ifndef __arch64__
sf.sf_scp = 0;
sf.sf_addr = 0;
#endif
sf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
sf.sf_sc.sc_mask = *mask;
#ifdef COMPAT_13
native_sigset_to_sigset13(mask, &sf.sf_sc.__sc_mask13);
#endif
sf.sf_sc.sc_sp = (long)tf->tf_out[6];
sf.sf_sc.sc_pc = tf->tf_pc;
sf.sf_sc.sc_npc = tf->tf_npc;
#ifdef __arch64__
sf.sf_sc.sc_tstate = tf->tf_tstate;
#else
sf.sf_sc.sc_psr = TSTATECCR_TO_PSR(tf->tf_tstate);
#endif
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 rwindow *)((vaddr_t)fp - sizeof(struct rwindow));
write_user_windows();
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK))
printf("sendsig: saving sf to %p, setting stack pointer %p to %p\n",
fp, &(((struct rwindow *)newsp)->rw_in[6]),
(void *)(unsigned long)tf->tf_out[6]);
#endif
error = (rwindow_save(l) || copyout((void *)&sf, (void *)fp, sizeof sf) ||
#ifdef NOT_DEBUG
copyin(oldsp, &tmpwin, sizeof(tmpwin)) || copyout(&tmpwin, newsp, sizeof(tmpwin)) ||
#endif
CPOUTREG(&(((struct rwindow *)newsp)->rw_in[6]), tf->tf_out[6]));
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);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
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
switch (ps->sa_sigdesc[sig].sd_vers) {
case __SIGTRAMP_SIGCODE_VERSION:
addr = p->p_sigctx.ps_sigcode;
break;
case __SIGTRAMP_SIGCONTEXT_VERSION:
addr = ps->sa_sigdesc[sig].sd_tramp;
break;
default:
addr = NULL;
sigexit(l, SIGILL);
}
tf->tf_global[1] = (vaddr_t)catcher;
tf->tf_pc = (const vaddr_t)addr;
tf->tf_npc = (const vaddr_t)addr + 4;
tf->tf_out[6] = (vaddr_t)newsp - STACK_OFFSET;
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, (void *)(unsigned long)addr);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
mutex_enter(p->p_lock);
}
#endif
}
int compat_16_sys___sigreturn14(struct lwp *, const struct compat_16_sys___sigreturn14_args *, register_t *);
int
compat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
{
struct sigcontext sc, *scp;
struct trapframe64 *tf;
int error = EINVAL;
struct proc *p = l->l_proc;
write_user_windows();
if (rwindow_save(l)) {
#ifdef DEBUG
printf("sigreturn14: rwindow_save(%p) failed, sending SIGILL\n", p);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
#endif
mutex_enter(p->p_lock);
sigexit(l, SIGILL);
}
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW) {
printf("sigreturn14: %s[%d], sigcntxp %p\n",
p->p_comm, p->p_pid, SCARG(uap, sigcntxp));
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
}
#endif
scp = SCARG(uap, sigcntxp);
if (((vaddr_t)scp & 3) != 0 ||
(error = copyin((void *)scp, &sc, sizeof sc)) != 0)
#ifdef DEBUG
{
printf("sigreturn14: copyin failed: scp=%p\n", scp);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
return (error);
}
#else
return (error);
#endif
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("sigreturn14: pc %p or npc %p invalid\n",
(void *)(unsigned long)sc.sc_pc,
(void *)(unsigned long)sc.sc_npc);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
return (EINVAL);
}
#else
return (EINVAL);
#endif
#ifdef __arch64__
tf->tf_tstate = (uint64_t)(tf->tf_tstate & ~TSTATE_CCR) | (scp->sc_tstate & TSTATE_CCR);
#else
tf->tf_tstate = (uint64_t)(tf->tf_tstate & ~TSTATE_CCR) | PSRCC_TO_TSTATE(scp->sc_psr);
#endif
tf->tf_pc = (uint64_t)scp->sc_pc;
tf->tf_npc = (uint64_t)scp->sc_npc;
tf->tf_global[1] = (uint64_t)scp->sc_g1;
tf->tf_out[0] = (uint64_t)scp->sc_o0;
tf->tf_out[6] = (uint64_t)scp->sc_sp;
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW) {
printf("sigreturn14: return trapframe pc=%p sp=%p tstate=%llx\n",
(void *)(unsigned long)tf->tf_pc,
(void *)(unsigned long)tf->tf_out[6],
(unsigned long long)tf->tf_tstate);
#ifdef DDB
if (sigdebug & SDB_DDB) Debugger();
#endif
}
#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);
}
#endif