Home | History | Annotate | Line # | Download | only in mips
      1 /*	$NetBSD: compat_16_machdep.c,v 1.24 2025/04/25 00:26:59 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center and by Chris Demetriou.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright 1996 The Board of Trustees of The Leland Stanford
     35  * Junior University. All Rights Reserved.
     36  *
     37  * Permission to use, copy, modify, and distribute this
     38  * software and its documentation for any purpose and without
     39  * fee is hereby granted, provided that the above copyright
     40  * notice appear in all copies.  Stanford University
     41  * makes no representations about the suitability of this
     42  * software for any purpose.  It is provided "as is" without
     43  * express or implied warranty.
     44  */
     45 
     46 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     47 
     48 __KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.24 2025/04/25 00:26:59 riastradh Exp $");
     49 
     50 #ifdef _KERNEL_OPT
     51 #include "opt_cputype.h"
     52 #include "opt_compat_netbsd.h"
     53 #include "opt_compat_ultrix.h"
     54 #endif
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/kernel.h>
     59 #include <sys/proc.h>
     60 #include <sys/cpu.h>
     61 #include <sys/signal.h>
     62 #include <sys/signalvar.h>
     63 #include <sys/mount.h>
     64 #include <sys/syscallargs.h>
     65 
     66 #include <compat/sys/signal.h>
     67 #include <compat/sys/signalvar.h>
     68 
     69 #include <mips/regnum.h>
     70 #include <mips/frame.h>
     71 #include <mips/locore.h>
     72 #include <mips/pcb.h>
     73 #include <mips/reg.h>
     74 
     75 #if !defined(__mips_o32)
     76 #define	fpreg		fpreg_oabi
     77 #endif
     78 
     79 #ifdef DEBUG
     80 int sigdebug = 0;
     81 int sigpid = 0;
     82 #define SDB_FOLLOW	0x01
     83 #define SDB_KSTACK	0x02
     84 #define SDB_FPSTATE	0x04
     85 #endif
     86 
     87 /*
     88  * Send a signal to process.
     89  */
     90 void
     91 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *returnmask)
     92 {
     93 	int sig = ksi->ksi_signo;
     94 	struct lwp * const l = curlwp;
     95 	struct proc * const p = l->l_proc;
     96 	struct sigacts * const ps = p->p_sigacts;
     97 	struct pcb * const pcb = lwp_getpcb(l);
     98 	int onstack, error;
     99 	struct sigcontext *scp = getframe(l, sig, &onstack,
    100 	    sizeof(*scp), _Alignof(*scp));
    101 	struct sigcontext ksc;
    102 	struct trapframe * const tf = l->l_md.md_utf;
    103 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    104 
    105 #if !defined(__mips_o32)
    106 	if (p->p_md.md_abi != _MIPS_BSD_API_O32)
    107 		sigexit(l, SIGILL);
    108 #endif
    109 
    110 #ifdef DEBUG
    111 	if ((sigdebug & SDB_FOLLOW) ||
    112 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
    113 		printf("sendsig(%d): sig %d ssp %p scp %p\n",
    114 		       p->p_pid, sig, &onstack, scp);
    115 #endif
    116 
    117 	/* Build stack frame for signal trampoline. */
    118 	ksc.sc_pc = tf->tf_regs[_R_PC];
    119 	ksc.mullo = tf->tf_regs[_R_MULLO];
    120 	ksc.mulhi = tf->tf_regs[_R_MULHI];
    121 
    122 	/* Save register context. */
    123 	ksc.sc_regs[_R_ZERO] = 0xACEDBADE;		/* magic number */
    124 #if defined(__mips_o32)
    125 	memcpy(&ksc.sc_regs[1], &tf->tf_regs[1],
    126 	    sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0]));
    127 #else
    128 	for (size_t i = 1; i < 32; i++)
    129 		ksc.sc_regs[i] = tf->tf_regs[i];
    130 #endif
    131 
    132 	/* Save the FP state, if necessary, then copy it. */
    133 	ksc.sc_fpused = fpu_used_p(l);
    134 #if !defined(NOFPU)
    135 	if (ksc.sc_fpused) {
    136 		/* if FPU has current state, save it first */
    137 		fpu_save(l);
    138 	}
    139 #endif
    140 	*(struct fpreg *)ksc.sc_fpregs = *(struct fpreg *)&pcb->pcb_fpregs;
    141 
    142 	/* Save signal stack. */
    143 	ksc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
    144 
    145 	/* Save signal mask. */
    146 	ksc.sc_mask = *returnmask;
    147 
    148 #if defined(COMPAT_13) || defined(COMPAT_ULTRIX)
    149 	/*
    150 	 * XXX We always have to save an old style signal mask because
    151 	 * XXX we might be delivering a signal to a process which will
    152 	 * XXX escape from the signal in a non-standard way and invoke
    153 	 * XXX sigreturn() directly.
    154 	 */
    155 	native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13);
    156 #endif
    157 
    158 	sendsig_reset(l, sig);
    159 
    160 	mutex_exit(p->p_lock);
    161 	error = copyout(&ksc, (void *)scp, sizeof(ksc));
    162 	mutex_enter(p->p_lock);
    163 
    164 	if (error != 0) {
    165 		/*
    166 		 * Process has trashed its stack; give it an illegal
    167 		 * instruction to halt it in its tracks.
    168 		 */
    169 #ifdef DEBUG
    170 		if ((sigdebug & SDB_FOLLOW) ||
    171 		    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
    172 			printf("sendsig(%d): copyout failed on sig %d\n",
    173 			    p->p_pid, sig);
    174 #endif
    175 		sigexit(l, SIGILL);
    176 		/* NOTREACHED */
    177 	}
    178 
    179 	/*
    180 	 * Set up the registers to directly invoke the signal
    181 	 * handler.  The return address will be set up to point
    182 	 * to the signal trampoline to bounce us back.
    183 	 */
    184 	tf->tf_regs[_R_A0] = sig;
    185 	tf->tf_regs[_R_A1] = ksi->ksi_trap;
    186 	tf->tf_regs[_R_A2] = (intptr_t)scp;
    187 	tf->tf_regs[_R_A3] = (intptr_t)catcher;		/* XXX ??? */
    188 
    189 	tf->tf_regs[_R_PC] = (intptr_t)catcher;
    190 	tf->tf_regs[_R_T9] = (intptr_t)catcher;
    191 	tf->tf_regs[_R_SP] = (intptr_t)scp;
    192 
    193 	switch (ps->sa_sigdesc[sig].sd_vers) {
    194 	case __SIGTRAMP_SIGCODE_VERSION:	/* legacy on-stack sigtramp */
    195 		tf->tf_regs[_R_RA] = (intptr_t)p->p_sigctx.ps_sigcode;
    196 		break;
    197 #ifdef COMPAT_16
    198 	case __SIGTRAMP_SIGCONTEXT_VERSION:
    199 		tf->tf_regs[_R_RA] = (intptr_t)ps->sa_sigdesc[sig].sd_tramp;
    200 		break;
    201 #endif
    202 	default:
    203 		/* Don't know what trampoline version; kill it. */
    204 		sigexit(l, SIGILL);
    205 	}
    206 
    207 	/* Remember that we're now on the signal stack. */
    208 	if (onstack)
    209 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    210 
    211 #ifdef DEBUG
    212 	if ((sigdebug & SDB_FOLLOW) ||
    213 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
    214 		printf("sendsig(%d): sig %d returns\n",
    215 		       p->p_pid, sig);
    216 #endif
    217 }
    218 
    219 #ifdef COMPAT_16 /* not needed if COMPAT_ULTRIX only */
    220 /*
    221  * System call to cleanup state after a signal
    222  * has been taken.  Reset signal mask and
    223  * stack state from context left by sendsig (above).
    224  * Return to previous pc and psl as specified by
    225  * context left by sendsig. Check carefully to
    226  * make sure that the user has not modified the
    227  * psl to gain improper privileges or to cause
    228  * a machine fault.
    229  */
    230 /* ARGSUSED */
    231 int
    232 compat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
    233 {
    234 	/* {
    235 		syscallarg(struct sigcontext *) sigcntxp;
    236 	} */
    237 	struct sigcontext *scp, ksc;
    238 	struct trapframe * const tf = l->l_md.md_utf;
    239 	struct proc * const p = l->l_proc;
    240 	struct pcb * const pcb = lwp_getpcb(l);
    241 	int error;
    242 
    243 #if !defined(__mips_o32)
    244 	if (p->p_md.md_abi != _MIPS_BSD_API_O32)
    245 		return ENOSYS;
    246 #endif
    247 
    248 	/*
    249 	 * The trampoline code hands us the context.
    250 	 * It is unsafe to keep track of it ourselves, in the event that a
    251 	 * program jumps out of a signal handler.
    252 	 */
    253 	scp = SCARG(uap, sigcntxp);
    254 #ifdef DEBUG
    255 	if (sigdebug & SDB_FOLLOW)
    256 		printf("sigreturn: pid %d, scp %p\n", l->l_proc->p_pid, scp);
    257 #endif
    258 	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
    259 		return (error);
    260 
    261 	if ((u_int) ksc.sc_regs[_R_ZERO] != 0xacedbadeU)/* magic number */
    262 		return (EINVAL);
    263 
    264 	/* Restore the register context. */
    265 	tf->tf_regs[_R_PC] = ksc.sc_pc;
    266 	tf->tf_regs[_R_MULLO] = ksc.mullo;
    267 	tf->tf_regs[_R_MULHI] = ksc.mulhi;
    268 #if defined(__mips_o32)
    269 	memcpy(&tf->tf_regs[1], &scp->sc_regs[1],
    270 	    sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0]));
    271 #else
    272 	for (size_t i = 1; i < __arraycount(ksc.sc_regs); i++)
    273 		tf->tf_regs[i] = ksc.sc_regs[i];
    274 #endif
    275 #if !defined(NOFPU)
    276 	if (scp->sc_fpused) {
    277 		fpu_discard(l);
    278 	}
    279 #endif
    280 	*(struct fpreg *)&pcb->pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
    281 
    282 	mutex_enter(p->p_lock);
    283 	/* Restore signal stack. */
    284 	if (ksc.sc_onstack & SS_ONSTACK)
    285 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    286 	else
    287 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    288 	/* Restore signal mask. */
    289 	(void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0);
    290 	mutex_exit(p->p_lock);
    291 
    292 	return (EJUSTRETURN);
    293 }
    294 #endif /* COMPAT_16 */
    295