compat_16_machdep.c revision 1.4
11.4Sdrochner/*	$NetBSD: compat_16_machdep.c,v 1.4 2005/01/24 10:03:58 drochner Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * This code is derived from software contributed to The NetBSD Foundation
81.1Schristos * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
91.1Schristos * NASA Ames Research Center and by Chris Demetriou.
101.1Schristos *
111.1Schristos * Redistribution and use in source and binary forms, with or without
121.1Schristos * modification, are permitted provided that the following conditions
131.1Schristos * are met:
141.1Schristos * 1. Redistributions of source code must retain the above copyright
151.1Schristos *    notice, this list of conditions and the following disclaimer.
161.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
171.1Schristos *    notice, this list of conditions and the following disclaimer in the
181.1Schristos *    documentation and/or other materials provided with the distribution.
191.1Schristos * 3. All advertising materials mentioning features or use of this software
201.1Schristos *    must display the following acknowledgement:
211.1Schristos *	This product includes software developed by the NetBSD
221.1Schristos *	Foundation, Inc. and its contributors.
231.1Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
241.1Schristos *    contributors may be used to endorse or promote products derived
251.1Schristos *    from this software without specific prior written permission.
261.1Schristos *
271.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
281.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
291.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
301.1Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
311.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
321.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
331.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
341.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
351.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
361.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
371.1Schristos * POSSIBILITY OF SUCH DAMAGE.
381.1Schristos */
391.1Schristos
401.1Schristos/*
411.1Schristos * Copyright 1996 The Board of Trustees of The Leland Stanford
421.1Schristos * Junior University. All Rights Reserved.
431.1Schristos *
441.1Schristos * Permission to use, copy, modify, and distribute this
451.1Schristos * software and its documentation for any purpose and without
461.1Schristos * fee is hereby granted, provided that the above copyright
471.1Schristos * notice appear in all copies.  Stanford University
481.1Schristos * makes no representations about the suitability of this
491.1Schristos * software for any purpose.  It is provided "as is" without
501.1Schristos * express or implied warranty.
511.1Schristos */
521.1Schristos
531.1Schristos#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
541.1Schristos
551.4Sdrochner__KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.4 2005/01/24 10:03:58 drochner Exp $");
561.1Schristos
571.1Schristos#include "opt_cputype.h"
581.1Schristos#include "opt_compat_netbsd.h"
591.1Schristos#include "opt_compat_ultrix.h"
601.1Schristos
611.1Schristos#include <sys/param.h>
621.1Schristos#include <sys/systm.h>
631.1Schristos#include <sys/kernel.h>
641.1Schristos#include <sys/proc.h>
651.1Schristos#include <sys/user.h>
661.1Schristos#include <sys/signal.h>
671.1Schristos#include <sys/signalvar.h>
681.1Schristos#include <sys/mount.h>
691.1Schristos#include <sys/sa.h>
701.1Schristos#include <sys/syscallargs.h>
711.1Schristos
721.1Schristos#include <machine/cpu.h>
731.1Schristos
741.1Schristos#include <mips/regnum.h>
751.1Schristos#include <mips/frame.h>
761.1Schristos
771.1Schristos#ifdef DEBUG
781.1Schristosint sigdebug = 0;
791.1Schristosint sigpid = 0;
801.1Schristos#define SDB_FOLLOW	0x01
811.1Schristos#define SDB_KSTACK	0x02
821.1Schristos#define SDB_FPSTATE	0x04
831.1Schristos#endif
841.1Schristos
851.1Schristos/*
861.1Schristos * Send a signal to process.
871.1Schristos */
881.1Schristosvoid
891.1Schristossendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *returnmask)
901.1Schristos{
911.1Schristos	int sig = ksi->ksi_signo;
921.1Schristos	struct lwp *l = curlwp;
931.1Schristos	struct proc *p = l->l_proc;
941.1Schristos	struct sigacts *ps = p->p_sigacts;
951.1Schristos	int onstack;
961.1Schristos	struct sigcontext *scp = getframe(l, sig, &onstack), ksc;
971.1Schristos	struct frame *f;
981.1Schristos	sig_t catcher = SIGACTION(p, sig).sa_handler;
991.1Schristos
1001.1Schristos	f = (struct frame *)l->l_md.md_regs;
1011.1Schristos
1021.1Schristos	scp--;
1031.1Schristos
1041.1Schristos#ifdef DEBUG
1051.1Schristos	if ((sigdebug & SDB_FOLLOW) ||
1061.1Schristos	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
1071.1Schristos		printf("sendsig(%d): sig %d ssp %p scp %p\n",
1081.1Schristos		       p->p_pid, sig, &onstack, scp);
1091.1Schristos#endif
1101.1Schristos
1111.1Schristos	/* Build stack frame for signal trampoline. */
1121.2She	ksc.sc_pc = f->f_regs[_R_PC];
1131.2She	ksc.mullo = f->f_regs[_R_MULLO];
1141.2She	ksc.mulhi = f->f_regs[_R_MULHI];
1151.1Schristos
1161.1Schristos	/* Save register context. */
1171.2She	ksc.sc_regs[_R_ZERO] = 0xACEDBADE;		/* magic number */
1181.1Schristos	memcpy(&ksc.sc_regs[1], &f->f_regs[1],
1191.1Schristos	    sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0]));
1201.1Schristos
1211.1Schristos	/* Save the FP state, if necessary, then copy it. */
1221.1Schristos#ifndef SOFTFLOAT
1231.1Schristos	ksc.sc_fpused = l->l_md.md_flags & MDP_FPUSED;
1241.1Schristos	if (ksc.sc_fpused) {
1251.1Schristos		/* if FPU has current state, save it first */
1261.1Schristos		if (l == fpcurlwp)
1271.1Schristos			savefpregs(l);
1281.1Schristos		*(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs;
1291.1Schristos	}
1301.1Schristos#else
1311.1Schristos	*(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs;
1321.1Schristos#endif
1331.1Schristos
1341.1Schristos	/* Save signal stack. */
1351.1Schristos	ksc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
1361.1Schristos
1371.1Schristos	/* Save signal mask. */
1381.1Schristos	ksc.sc_mask = *returnmask;
1391.1Schristos
1401.1Schristos#if defined(COMPAT_13) || defined(COMPAT_ULTRIX)
1411.1Schristos	/*
1421.1Schristos	 * XXX We always have to save an old style signal mask because
1431.1Schristos	 * XXX we might be delivering a signal to a process which will
1441.1Schristos	 * XXX escape from the signal in a non-standard way and invoke
1451.1Schristos	 * XXX sigreturn() directly.
1461.1Schristos	 */
1471.1Schristos	native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13);
1481.1Schristos#endif
1491.1Schristos
1501.1Schristos	if (copyout(&ksc, (caddr_t)scp, sizeof(ksc))) {
1511.1Schristos		/*
1521.1Schristos		 * Process has trashed its stack; give it an illegal
1531.1Schristos		 * instruction to halt it in its tracks.
1541.1Schristos		 */
1551.1Schristos#ifdef DEBUG
1561.1Schristos		if ((sigdebug & SDB_FOLLOW) ||
1571.1Schristos		    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
1581.1Schristos			printf("sendsig(%d): copyout failed on sig %d\n",
1591.1Schristos			    p->p_pid, sig);
1601.1Schristos#endif
1611.1Schristos		sigexit(l, SIGILL);
1621.1Schristos		/* NOTREACHED */
1631.1Schristos	}
1641.1Schristos
1651.1Schristos	/*
1661.1Schristos	 * Set up the registers to directly invoke the signal
1671.1Schristos	 * handler.  The return address will be set up to point
1681.1Schristos	 * to the signal trampoline to bounce us back.
1691.1Schristos	 */
1701.2She	f->f_regs[_R_A0] = sig;
1711.2She	f->f_regs[_R_A1] = ksi->ksi_trap;
1721.2She	f->f_regs[_R_A2] = (int)scp;
1731.2She	f->f_regs[_R_A3] = (int)catcher;		/* XXX ??? */
1741.2She
1751.2She	f->f_regs[_R_PC] = (int)catcher;
1761.2She	f->f_regs[_R_T9] = (int)catcher;
1771.2She	f->f_regs[_R_SP] = (int)scp;
1781.1Schristos
1791.1Schristos	switch (ps->sa_sigdesc[sig].sd_vers) {
1801.1Schristos	case 0:		/* legacy on-stack sigtramp */
1811.2She		f->f_regs[_R_RA] = (int)p->p_sigctx.ps_sigcode;
1821.1Schristos		break;
1831.4Sdrochner#ifdef COMPAT_16
1841.1Schristos	case 1:
1851.2She		f->f_regs[_R_RA] = (int)ps->sa_sigdesc[sig].sd_tramp;
1861.1Schristos		break;
1871.4Sdrochner#endif
1881.1Schristos	default:
1891.1Schristos		/* Don't know what trampoline version; kill it. */
1901.1Schristos		sigexit(l, SIGILL);
1911.1Schristos	}
1921.1Schristos
1931.1Schristos	/* Remember that we're now on the signal stack. */
1941.1Schristos	if (onstack)
1951.1Schristos		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
1961.1Schristos
1971.1Schristos#ifdef DEBUG
1981.1Schristos	if ((sigdebug & SDB_FOLLOW) ||
1991.1Schristos	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
2001.1Schristos		printf("sendsig(%d): sig %d returns\n",
2011.1Schristos		       p->p_pid, sig);
2021.1Schristos#endif
2031.1Schristos}
2041.1Schristos
2051.4Sdrochner#ifdef COMPAT_16 /* not needed if COMPAT_ULTRIX only */
2061.1Schristos/*
2071.1Schristos * System call to cleanup state after a signal
2081.1Schristos * has been taken.  Reset signal mask and
2091.1Schristos * stack state from context left by sendsig (above).
2101.1Schristos * Return to previous pc and psl as specified by
2111.1Schristos * context left by sendsig. Check carefully to
2121.1Schristos * make sure that the user has not modified the
2131.1Schristos * psl to gain improper privileges or to cause
2141.1Schristos * a machine fault.
2151.1Schristos */
2161.1Schristos/* ARGSUSED */
2171.1Schristosint
2181.1Schristoscompat_16_sys___sigreturn14(struct lwp *l, void *v, register_t *retval)
2191.1Schristos{
2201.1Schristos	struct compat_16_sys___sigreturn14_args /* {
2211.1Schristos		syscallarg(struct sigcontext *) sigcntxp;
2221.1Schristos	} */ *uap = v;
2231.1Schristos	struct sigcontext *scp, ksc;
2241.1Schristos	struct frame *f;
2251.1Schristos	struct proc *p = l->l_proc;
2261.1Schristos	int error;
2271.1Schristos
2281.1Schristos	/*
2291.1Schristos	 * The trampoline code hands us the context.
2301.1Schristos	 * It is unsafe to keep track of it ourselves, in the event that a
2311.1Schristos	 * program jumps out of a signal handler.
2321.1Schristos	 */
2331.1Schristos	scp = SCARG(uap, sigcntxp);
2341.1Schristos#ifdef DEBUG
2351.1Schristos	if (sigdebug & SDB_FOLLOW)
2361.1Schristos		printf("sigreturn: pid %d, scp %p\n", l->l_proc->p_pid, scp);
2371.1Schristos#endif
2381.1Schristos	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
2391.1Schristos		return (error);
2401.1Schristos
2411.2She	if ((u_int) ksc.sc_regs[_R_ZERO] != 0xacedbadeU)/* magic number */
2421.1Schristos		return (EINVAL);
2431.1Schristos
2441.1Schristos	/* Restore the register context. */
2451.1Schristos	f = (struct frame *)l->l_md.md_regs;
2461.2She	f->f_regs[_R_PC] = ksc.sc_pc;
2471.2She	f->f_regs[_R_MULLO] = ksc.mullo;
2481.2She	f->f_regs[_R_MULHI] = ksc.mulhi;
2491.1Schristos	memcpy(&f->f_regs[1], &scp->sc_regs[1],
2501.1Schristos	    sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0]));
2511.1Schristos#ifndef	SOFTFLOAT
2521.1Schristos	if (scp->sc_fpused) {
2531.1Schristos		/* Disable the FPU to fault in FP registers. */
2541.2She		f->f_regs[_R_SR] &= ~MIPS_SR_COP_1_BIT;
2551.3Ssimonb		if (l == fpcurlwp)
2561.3Ssimonb			fpcurlwp = NULL;
2571.1Schristos		l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
2581.1Schristos	}
2591.1Schristos#else
2601.1Schristos	l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
2611.1Schristos#endif
2621.1Schristos
2631.1Schristos	/* Restore signal stack. */
2641.1Schristos	if (ksc.sc_onstack & SS_ONSTACK)
2651.1Schristos		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
2661.1Schristos	else
2671.1Schristos		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
2681.1Schristos
2691.1Schristos	/* Restore signal mask. */
2701.1Schristos	(void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
2711.1Schristos
2721.1Schristos	return (EJUSTRETURN);
2731.1Schristos}
2741.4Sdrochner#endif /* COMPAT_16 */
275