#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.34 2022/08/20 23:49:31 riastradh 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 <uvm/uvm_extern.h>
#include <machine/pmap.h>
#include <machine/pmap_private.h>
#include <machine/vmparam.h>
#include <x86/fpu.h>
#if defined(COMPAT_16)
#include <compat/sys/signal.h>
#include <compat/sys/signalvar.h>
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 proc *p = l->l_proc;
struct sigcontext *scp, context;
struct trapframe *tf;
scp = SCARG(uap, sigcntxp);
if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
return (EFAULT);
tf = l->l_md.md_regs;
if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
!USERMODE(context.sc_cs))
return (EINVAL);
tf->tf_gs = context.sc_gs;
tf->tf_fs = context.sc_fs;
tf->tf_es = context.sc_es;
tf->tf_ds = context.sc_ds;
tf->tf_eflags &= ~PSL_USER;
tf->tf_eflags |= context.sc_eflags & PSL_USER;
tf->tf_edi = context.sc_edi;
tf->tf_esi = context.sc_esi;
tf->tf_ebp = context.sc_ebp;
tf->tf_ebx = context.sc_ebx;
tf->tf_edx = context.sc_edx;
tf->tf_ecx = context.sc_ecx;
tf->tf_eax = context.sc_eax;
tf->tf_eip = context.sc_eip;
tf->tf_cs = context.sc_cs;
tf->tf_esp = context.sc_esp;
tf->tf_ss = context.sc_ss;
mutex_enter(p->p_lock);
if (context.sc_onstack & SS_ONSTACK)
l->l_sigstk.ss_flags |= SS_ONSTACK;
else
l->l_sigstk.ss_flags &= ~SS_ONSTACK;
(void) sigprocmask1(l, SIG_SETMASK, &context.sc_mask, 0);
mutex_exit(p->p_lock);
return (EJUSTRETURN);
}
#endif
#ifdef COMPAT_16
void
sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct pmap *pmap = vm_map_pmap(&p->p_vmspace->vm_map);
int sel = pmap->pm_hiexec > I386_MAX_EXE_ADDR ?
GUCODEBIG_SEL : GUCODE_SEL;
struct sigacts *ps = p->p_sigacts;
struct trapframe *tf = l->l_md.md_regs;
int onstack, error;
int sig = ksi->ksi_signo;
u_long code = KSI_TRAPCODE(ksi);
struct sigframe_sigcontext *fp = getframe(l, sig, &onstack), frame;
sig_t catcher = SIGACTION(p, sig).sa_handler;
fp--;
switch (ps->sa_sigdesc[sig].sd_vers) {
case __SIGTRAMP_SIGCODE_VERSION:
frame.sf_ra = (int)p->p_sigctx.ps_sigcode;
break;
case __SIGTRAMP_SIGCONTEXT_VERSION:
frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
break;
default:
sigexit(l, SIGILL);
}
frame.sf_signum = sig;
frame.sf_code = code;
frame.sf_scp = &fp->sf_sc;
frame.sf_sc.sc_gs = tf->tf_gs;
frame.sf_sc.sc_fs = tf->tf_fs;
frame.sf_sc.sc_es = tf->tf_es;
frame.sf_sc.sc_ds = tf->tf_ds;
frame.sf_sc.sc_eflags = tf->tf_eflags;
frame.sf_sc.sc_edi = tf->tf_edi;
frame.sf_sc.sc_esi = tf->tf_esi;
frame.sf_sc.sc_ebp = tf->tf_ebp;
frame.sf_sc.sc_ebx = tf->tf_ebx;
frame.sf_sc.sc_edx = tf->tf_edx;
frame.sf_sc.sc_ecx = tf->tf_ecx;
frame.sf_sc.sc_eax = tf->tf_eax;
frame.sf_sc.sc_eip = tf->tf_eip;
frame.sf_sc.sc_cs = tf->tf_cs;
frame.sf_sc.sc_esp = tf->tf_esp;
frame.sf_sc.sc_ss = tf->tf_ss;
frame.sf_sc.sc_trapno = tf->tf_trapno;
frame.sf_sc.sc_err = tf->tf_err;
frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
frame.sf_sc.sc_mask = *mask;
#ifdef COMPAT_13
native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
#endif
sendsig_reset(l, sig);
mutex_exit(p->p_lock);
error = copyout(&frame, fp, sizeof(frame));
mutex_enter(p->p_lock);
if (error != 0) {
sigexit(l, SIGILL);
}
buildcontext(l, sel, catcher, fp);
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}
#endif