#include "opt_armfpe.h"
#include <sys/param.h>
__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.53 2024/04/14 07:56:13 skrll Exp $");
#include <sys/mount.h>
#include <sys/cpu.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/syscallargs.h>
#include <sys/systm.h>
#include <sys/ras.h>
#include <sys/ucontext.h>
#include <arm/locore.h>
#include <machine/pcb.h>
#include <arm/cpufunc.h>
void *
getframe(struct lwp *l, int sig, int *onstack)
{
struct proc * const p = l->l_proc;
struct trapframe * const tf = lwp_trapframe(l);
*onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
&& (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
if (*onstack)
return (char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
return (void *)tf->tf_usr_sp;
}
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 ps = p->p_sigacts;
struct trapframe * const tf = lwp_trapframe(l);
struct sigframe_siginfo *fp, frame;
int onstack, error;
int sig = ksi->ksi_signo;
sig_t catcher = SIGACTION(p, sig).sa_handler;
fp = getframe(l, sig, &onstack);
fp--;
fp = (struct sigframe_siginfo *)STACK_ALIGN(fp, STACK_ALIGNBYTES);
memset(&frame, 0, sizeof(frame));
frame.sf_si._info = ksi->ksi_info;
frame.sf_uc.uc_flags = _UC_SIGMASK;
frame.sf_uc.uc_sigmask = *mask;
frame.sf_uc.uc_link = l->l_ctxlink;
frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
? _UC_SETSTACK : _UC_CLRSTACK;
sendsig_reset(l, sig);
mutex_exit(p->p_lock);
cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
error = copyout(&frame, fp, sizeof(frame));
mutex_enter(p->p_lock);
if (error != 0) {
sigexit(l, SIGILL);
}
tf->tf_r0 = sig;
tf->tf_r1 = (int)&fp->sf_si;
tf->tf_r2 = (int)&fp->sf_uc;
tf->tf_r5 = (int)&fp->sf_uc;
tf->tf_pc = (int)catcher;
#ifdef THUMB_CODE
if (((int) catcher) & 1)
tf->tf_spsr |= PSR_T_bit;
else
tf->tf_spsr &= ~PSR_T_bit;
#endif
tf->tf_usr_sp = (int)fp;
tf->tf_usr_lr = (int)ps->sa_sigdesc[sig].sd_tramp;
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}
void
cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
{
struct trapframe * const tf = lwp_trapframe(l);
__greg_t * const gr = mcp->__gregs;
__greg_t ras_pc;
gr[_REG_R0] = tf->tf_r0;
gr[_REG_R1] = tf->tf_r1;
gr[_REG_R2] = tf->tf_r2;
gr[_REG_R3] = tf->tf_r3;
gr[_REG_R4] = tf->tf_r4;
gr[_REG_R5] = tf->tf_r5;
gr[_REG_R6] = tf->tf_r6;
gr[_REG_R7] = tf->tf_r7;
gr[_REG_R8] = tf->tf_r8;
gr[_REG_R9] = tf->tf_r9;
gr[_REG_R10] = tf->tf_r10;
gr[_REG_R11] = tf->tf_r11;
gr[_REG_R12] = tf->tf_r12;
gr[_REG_SP] = tf->tf_usr_sp;
gr[_REG_LR] = tf->tf_usr_lr;
gr[_REG_PC] = tf->tf_pc;
gr[_REG_CPSR] = tf->tf_spsr;
KASSERTMSG(VALID_PSR(gr[_REG_CPSR]), "%#x", gr[_REG_CPSR]);
if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
(void *) gr[_REG_PC])) != -1)
gr[_REG_PC] = ras_pc;
*flags |= _UC_CPU;
#ifdef FPU_VFP
vfp_getcontext(l, mcp, flags);
#endif
mcp->_mc_tlsbase = (uintptr_t)l->l_private;
*flags |= _UC_TLSBASE;
const struct pcb * const pcb = lwp_getpcb(l);
mcp->_mc_user_tpid = pcb->pcb_user_pid_rw;
}
int
cpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
{
const __greg_t * const gr = mcp->__gregs;
if (!VALID_PSR(gr[_REG_CPSR]))
return EINVAL;
return 0;
}
int
cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
{
struct trapframe * const tf = lwp_trapframe(l);
const __greg_t * const gr = mcp->__gregs;
struct proc * const p = l->l_proc;
int error;
#ifdef FPU_VFP
if ((flags & _UC_FPU)
&& (curcpu()->ci_vfp_id == 0 || (flags & _UC_ARM_VFP) == 0))
return EINVAL;
#endif
if ((flags & _UC_CPU) != 0) {
error = cpu_mcontext_validate(l, mcp);
if (error)
return error;
tf->tf_r0 = gr[_REG_R0];
tf->tf_r1 = gr[_REG_R1];
tf->tf_r2 = gr[_REG_R2];
tf->tf_r3 = gr[_REG_R3];
tf->tf_r4 = gr[_REG_R4];
tf->tf_r5 = gr[_REG_R5];
tf->tf_r6 = gr[_REG_R6];
tf->tf_r7 = gr[_REG_R7];
tf->tf_r8 = gr[_REG_R8];
tf->tf_r9 = gr[_REG_R9];
tf->tf_r10 = gr[_REG_R10];
tf->tf_r11 = gr[_REG_R11];
tf->tf_r12 = gr[_REG_R12];
tf->tf_usr_sp = gr[_REG_SP];
tf->tf_usr_lr = gr[_REG_LR];
tf->tf_pc = gr[_REG_PC];
tf->tf_spsr = gr[_REG_CPSR];
}
#ifdef FPU_VFP
if ((flags & _UC_FPU) != 0) {
vfp_setcontext(l, mcp);
}
#endif
if ((flags & _UC_TLSBASE) != 0)
lwp_setprivate(l, (void *)(uintptr_t)mcp->_mc_tlsbase);
mutex_enter(p->p_lock);
if (flags & _UC_SETSTACK)
l->l_sigstk.ss_flags |= SS_ONSTACK;
if (flags & _UC_CLRSTACK)
l->l_sigstk.ss_flags &= ~SS_ONSTACK;
mutex_exit(p->p_lock);
struct pcb * const pcb = lwp_getpcb(l);
pcb->pcb_user_pid_rw = mcp->_mc_user_tpid;
return (0);
}