#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.26 2025/04/25 00:26:59 riastradh Exp $");
#include "opt_cputype.h"
#include <sys/param.h>
#include <sys/cpu.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/mount.h>
#include <sys/syscallargs.h>
#include <mips/frame.h>
#include <mips/regnum.h>
#include <mips/locore.h>
void *
getframe(struct lwp *l, int sig, int *onstack, size_t size, size_t align)
{
struct proc * const p = l->l_proc;
struct trapframe * const tf = l->l_md.md_utf;
uintptr_t sp;
KASSERT((align & (align - 1)) == 0);
*onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
&& (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
if (*onstack)
sp = (uintptr_t)l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
else
sp = (uintptr_t)tf->tf_regs[_R_SP];
sp -= size;
#ifndef __mips_o32
if (p->p_md.md_abi == _MIPS_BSD_API_O32)
sp &= ~(STACK_ALIGNBYTES_O32 | (align - 1));
else
#endif
sp &= ~(STACK_ALIGNBYTES | (align - 1));
return (void *)sp;
}
struct sigframe_siginfo {
siginfo_t sf_si;
ucontext_t sf_uc;
};
void
sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
{
struct lwp * const l = curlwp;
struct proc * const p = l->l_proc;
struct sigacts * const sa = p->p_sigacts;
struct trapframe * const tf = l->l_md.md_utf;
int onstack, error;
const int signo = ksi->ksi_signo;
struct sigframe_siginfo *sf = getframe(l, signo, &onstack,
sizeof(*sf), _Alignof(*sf));
struct sigframe_siginfo ksf;
const sig_t catcher = SIGACTION(p, signo).sa_handler;
memset(&ksf, 0, sizeof(ksf));
ksf.sf_si._info = ksi->ksi_info;
ksf.sf_uc.uc_flags = _UC_SIGMASK
| (l->l_sigstk.ss_flags & SS_ONSTACK ? _UC_SETSTACK : _UC_CLRSTACK);
ksf.sf_uc.uc_sigmask = *mask;
ksf.sf_uc.uc_link = l->l_ctxlink;
sendsig_reset(l, signo);
mutex_exit(p->p_lock);
cpu_getmcontext(l, &ksf.sf_uc.uc_mcontext, &ksf.sf_uc.uc_flags);
error = copyout(&ksf, sf, sizeof(ksf));
mutex_enter(p->p_lock);
if (error != 0) {
sigexit(l, SIGILL);
}
tf->tf_regs[_R_A0] = signo;
tf->tf_regs[_R_A1] = (intptr_t)&sf->sf_si;
tf->tf_regs[_R_A2] = (intptr_t)&sf->sf_uc;
tf->tf_regs[_R_PC] = (intptr_t)catcher;
tf->tf_regs[_R_T9] = (intptr_t)catcher;
tf->tf_regs[_R_SP] = (intptr_t)sf;
tf->tf_regs[_R_RA] = (intptr_t)sa->sa_sigdesc[signo].sd_tramp;
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}