#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: freebsd_machdep.c,v 1.5 2021/09/07 11:43:03 riastradh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signalvar.h>
#include <sys/proc.h>
#include <sys/exec.h>
#include <sys/mount.h>
#include <compat/sys/signal.h>
#include <machine/cpufunc.h>
#include <x86/fpu.h>
#include <machine/reg.h>
#include <machine/vmparam.h>
#include <compat/freebsd/freebsd_machdep.h>
#include <compat/freebsd/freebsd_syscallargs.h>
#include <compat/freebsd/freebsd_exec.h>
#include <compat/freebsd/freebsd_signal.h>
void
freebsd_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
{
setregs(l, epp, stack);
fpu_set_default_cw(l, __FreeBSD_NPXCW__);
}
void
freebsd_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
{
int sig = ksi->ksi_signo;
u_long code = KSI_TRAPCODE(ksi);
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
int onstack, error;
struct freebsd_sigframe *fp = getframe(l, sig, &onstack), frame;
sig_t catcher = SIGACTION(p, sig).sa_handler;
struct trapframe *tf = l->l_md.md_regs;
fp--;
memset(&frame, 0, sizeof(frame));
frame.sf_signum = sig;
frame.sf_code = code;
frame.sf_scp = &fp->sf_sc;
frame.sf_addr = (char *)rcr2();
frame.sf_handler = catcher;
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_efl = 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_isp = 0;
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_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
frame.sf_sc.sc_mask = *mask;
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, GUCODEBIG_SEL, p->p_sigctx.ps_sigcode, fp);
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}
int
freebsd_sys_sigreturn(struct lwp *l, const struct freebsd_sys_sigreturn_args *uap, register_t *retval)
{
struct proc *p = l->l_proc;
struct freebsd_sigcontext *scp, context;
struct trapframe *tf;
sigset_t mask;
scp = SCARG(uap, scp);
if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
return (EFAULT);
tf = l->l_md.md_regs;
if (((context.sc_efl ^ 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_efl & 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;
mask = context.sc_mask;
(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
mutex_exit(p->p_lock);
return (EJUSTRETURN);
}