compat_16_machdep.c revision 1.17
11.17Srmind/*	$NetBSD: compat_16_machdep.c,v 1.17 2011/01/14 02:06:28 rmind 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 *
201.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
211.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
221.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
231.1Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
241.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
271.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
281.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
291.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
301.1Schristos * POSSIBILITY OF SUCH DAMAGE.
311.1Schristos */
321.1Schristos
331.1Schristos/*
341.1Schristos * Copyright 1996 The Board of Trustees of The Leland Stanford
351.1Schristos * Junior University. All Rights Reserved.
361.1Schristos *
371.1Schristos * Permission to use, copy, modify, and distribute this
381.1Schristos * software and its documentation for any purpose and without
391.1Schristos * fee is hereby granted, provided that the above copyright
401.1Schristos * notice appear in all copies.  Stanford University
411.1Schristos * makes no representations about the suitability of this
421.1Schristos * software for any purpose.  It is provided "as is" without
431.1Schristos * express or implied warranty.
441.1Schristos */
451.1Schristos
461.1Schristos#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
471.1Schristos
481.17Srmind__KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.17 2011/01/14 02:06:28 rmind Exp $");
491.1Schristos
501.13She#ifdef _KERNEL_OPT
511.1Schristos#include "opt_cputype.h"
521.1Schristos#include "opt_compat_netbsd.h"
531.1Schristos#include "opt_compat_ultrix.h"
541.13She#endif
551.1Schristos
561.1Schristos#include <sys/param.h>
571.1Schristos#include <sys/systm.h>
581.1Schristos#include <sys/kernel.h>
591.1Schristos#include <sys/proc.h>
601.1Schristos#include <sys/signal.h>
611.1Schristos#include <sys/signalvar.h>
621.1Schristos#include <sys/mount.h>
631.1Schristos#include <sys/syscallargs.h>
641.1Schristos
651.5She#include <compat/sys/signal.h>
661.5She#include <compat/sys/signalvar.h>
671.5She
681.1Schristos#include <machine/cpu.h>
691.1Schristos
701.1Schristos#include <mips/regnum.h>
711.1Schristos#include <mips/frame.h>
721.17Srmind#include <mips/pcb.h>
731.15Smatt#include <mips/reg.h>
741.15Smatt
751.15Smatt#if !defined(__mips_o32)
761.15Smatt#define	fpreg		fpreg_oabi
771.15Smatt#endif
781.1Schristos
791.1Schristos#ifdef DEBUG
801.1Schristosint sigdebug = 0;
811.1Schristosint sigpid = 0;
821.1Schristos#define SDB_FOLLOW	0x01
831.1Schristos#define SDB_KSTACK	0x02
841.1Schristos#define SDB_FPSTATE	0x04
851.1Schristos#endif
861.1Schristos
871.1Schristos/*
881.1Schristos * Send a signal to process.
891.1Schristos */
901.1Schristosvoid
911.1Schristossendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *returnmask)
921.1Schristos{
931.1Schristos	int sig = ksi->ksi_signo;
941.1Schristos	struct lwp *l = curlwp;
951.1Schristos	struct proc *p = l->l_proc;
961.14Srmind	struct pcb *pcb;
971.1Schristos	struct sigacts *ps = p->p_sigacts;
981.8Sad	int onstack, error;
991.15Smatt	struct sigcontext *scp = getframe(l, sig, &onstack);
1001.15Smatt	struct sigcontext ksc;
1011.15Smatt	struct frame *f = l->l_md.md_regs;
1021.1Schristos	sig_t catcher = SIGACTION(p, sig).sa_handler;
1031.1Schristos
1041.15Smatt#if !defined(__mips_o32)
1051.15Smatt	if (p->p_md.md_abi != _MIPS_BSD_API_O32)
1061.15Smatt		sigexit(l, SIGILL);
1071.15Smatt#endif
1081.1Schristos
1091.1Schristos	scp--;
1101.1Schristos
1111.1Schristos#ifdef DEBUG
1121.1Schristos	if ((sigdebug & SDB_FOLLOW) ||
1131.1Schristos	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
1141.1Schristos		printf("sendsig(%d): sig %d ssp %p scp %p\n",
1151.1Schristos		       p->p_pid, sig, &onstack, scp);
1161.1Schristos#endif
1171.1Schristos
1181.1Schristos	/* Build stack frame for signal trampoline. */
1191.2She	ksc.sc_pc = f->f_regs[_R_PC];
1201.2She	ksc.mullo = f->f_regs[_R_MULLO];
1211.2She	ksc.mulhi = f->f_regs[_R_MULHI];
1221.1Schristos
1231.1Schristos	/* Save register context. */
1241.2She	ksc.sc_regs[_R_ZERO] = 0xACEDBADE;		/* magic number */
1251.15Smatt#if defined(__mips_o32)
1261.1Schristos	memcpy(&ksc.sc_regs[1], &f->f_regs[1],
1271.1Schristos	    sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0]));
1281.15Smatt#else
1291.16Suebayasi	size_t i;
1301.16Suebayasi	for (i = 1; i < 32; i++)
1311.15Smatt		ksc.sc_regs[i] = f->f_regs[i];
1321.15Smatt#endif
1331.1Schristos
1341.1Schristos	/* Save the FP state, if necessary, then copy it. */
1351.14Srmind	pcb = lwp_getpcb(l);
1361.1Schristos#ifndef SOFTFLOAT
1371.1Schristos	ksc.sc_fpused = l->l_md.md_flags & MDP_FPUSED;
1381.1Schristos	if (ksc.sc_fpused) {
1391.1Schristos		/* if FPU has current state, save it first */
1401.15Smatt		savefpregs(l);
1411.1Schristos	}
1421.1Schristos#endif
1431.15Smatt	*(struct fpreg *)ksc.sc_fpregs = *(struct fpreg *)&pcb->pcb_fpregs;
1441.1Schristos
1451.1Schristos	/* Save signal stack. */
1461.8Sad	ksc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
1471.1Schristos
1481.1Schristos	/* Save signal mask. */
1491.1Schristos	ksc.sc_mask = *returnmask;
1501.1Schristos
1511.1Schristos#if defined(COMPAT_13) || defined(COMPAT_ULTRIX)
1521.1Schristos	/*
1531.1Schristos	 * XXX We always have to save an old style signal mask because
1541.1Schristos	 * XXX we might be delivering a signal to a process which will
1551.1Schristos	 * XXX escape from the signal in a non-standard way and invoke
1561.1Schristos	 * XXX sigreturn() directly.
1571.1Schristos	 */
1581.1Schristos	native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13);
1591.1Schristos#endif
1601.1Schristos
1611.8Sad	sendsig_reset(l, sig);
1621.8Sad
1631.11Sad	mutex_exit(p->p_lock);
1641.9Schristos	error = copyout(&ksc, (void *)scp, sizeof(ksc));
1651.11Sad	mutex_enter(p->p_lock);
1661.8Sad
1671.8Sad	if (error != 0) {
1681.1Schristos		/*
1691.1Schristos		 * Process has trashed its stack; give it an illegal
1701.1Schristos		 * instruction to halt it in its tracks.
1711.1Schristos		 */
1721.1Schristos#ifdef DEBUG
1731.1Schristos		if ((sigdebug & SDB_FOLLOW) ||
1741.1Schristos		    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
1751.1Schristos			printf("sendsig(%d): copyout failed on sig %d\n",
1761.1Schristos			    p->p_pid, sig);
1771.1Schristos#endif
1781.1Schristos		sigexit(l, SIGILL);
1791.1Schristos		/* NOTREACHED */
1801.1Schristos	}
1811.1Schristos
1821.1Schristos	/*
1831.1Schristos	 * Set up the registers to directly invoke the signal
1841.1Schristos	 * handler.  The return address will be set up to point
1851.1Schristos	 * to the signal trampoline to bounce us back.
1861.1Schristos	 */
1871.2She	f->f_regs[_R_A0] = sig;
1881.2She	f->f_regs[_R_A1] = ksi->ksi_trap;
1891.7Smatt	f->f_regs[_R_A2] = (intptr_t)scp;
1901.7Smatt	f->f_regs[_R_A3] = (intptr_t)catcher;		/* XXX ??? */
1911.2She
1921.7Smatt	f->f_regs[_R_PC] = (intptr_t)catcher;
1931.7Smatt	f->f_regs[_R_T9] = (intptr_t)catcher;
1941.7Smatt	f->f_regs[_R_SP] = (intptr_t)scp;
1951.1Schristos
1961.1Schristos	switch (ps->sa_sigdesc[sig].sd_vers) {
1971.1Schristos	case 0:		/* legacy on-stack sigtramp */
1981.7Smatt		f->f_regs[_R_RA] = (intptr_t)p->p_sigctx.ps_sigcode;
1991.1Schristos		break;
2001.4Sdrochner#ifdef COMPAT_16
2011.1Schristos	case 1:
2021.7Smatt		f->f_regs[_R_RA] = (intptr_t)ps->sa_sigdesc[sig].sd_tramp;
2031.1Schristos		break;
2041.4Sdrochner#endif
2051.1Schristos	default:
2061.1Schristos		/* Don't know what trampoline version; kill it. */
2071.1Schristos		sigexit(l, SIGILL);
2081.1Schristos	}
2091.1Schristos
2101.1Schristos	/* Remember that we're now on the signal stack. */
2111.1Schristos	if (onstack)
2121.8Sad		l->l_sigstk.ss_flags |= SS_ONSTACK;
2131.1Schristos
2141.1Schristos#ifdef DEBUG
2151.1Schristos	if ((sigdebug & SDB_FOLLOW) ||
2161.1Schristos	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
2171.1Schristos		printf("sendsig(%d): sig %d returns\n",
2181.1Schristos		       p->p_pid, sig);
2191.1Schristos#endif
2201.1Schristos}
2211.1Schristos
2221.4Sdrochner#ifdef COMPAT_16 /* not needed if COMPAT_ULTRIX only */
2231.1Schristos/*
2241.1Schristos * System call to cleanup state after a signal
2251.1Schristos * has been taken.  Reset signal mask and
2261.1Schristos * stack state from context left by sendsig (above).
2271.1Schristos * Return to previous pc and psl as specified by
2281.1Schristos * context left by sendsig. Check carefully to
2291.1Schristos * make sure that the user has not modified the
2301.1Schristos * psl to gain improper privileges or to cause
2311.1Schristos * a machine fault.
2321.1Schristos */
2331.1Schristos/* ARGSUSED */
2341.1Schristosint
2351.10Sdslcompat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
2361.1Schristos{
2371.10Sdsl	/* {
2381.1Schristos		syscallarg(struct sigcontext *) sigcntxp;
2391.10Sdsl	} */
2401.1Schristos	struct sigcontext *scp, ksc;
2411.1Schristos	struct frame *f;
2421.1Schristos	struct proc *p = l->l_proc;
2431.14Srmind	struct pcb *pcb;
2441.1Schristos	int error;
2451.1Schristos
2461.15Smatt#if !defined(__mips_o32)
2471.15Smatt	if (p->p_md.md_abi != _MIPS_BSD_API_O32)
2481.15Smatt		return ENOSYS;
2491.15Smatt#endif
2501.15Smatt
2511.1Schristos	/*
2521.1Schristos	 * The trampoline code hands us the context.
2531.1Schristos	 * It is unsafe to keep track of it ourselves, in the event that a
2541.1Schristos	 * program jumps out of a signal handler.
2551.1Schristos	 */
2561.1Schristos	scp = SCARG(uap, sigcntxp);
2571.1Schristos#ifdef DEBUG
2581.1Schristos	if (sigdebug & SDB_FOLLOW)
2591.1Schristos		printf("sigreturn: pid %d, scp %p\n", l->l_proc->p_pid, scp);
2601.1Schristos#endif
2611.1Schristos	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
2621.1Schristos		return (error);
2631.1Schristos
2641.2She	if ((u_int) ksc.sc_regs[_R_ZERO] != 0xacedbadeU)/* magic number */
2651.1Schristos		return (EINVAL);
2661.1Schristos
2671.1Schristos	/* Restore the register context. */
2681.15Smatt	f = l->l_md.md_regs;
2691.2She	f->f_regs[_R_PC] = ksc.sc_pc;
2701.2She	f->f_regs[_R_MULLO] = ksc.mullo;
2711.2She	f->f_regs[_R_MULHI] = ksc.mulhi;
2721.15Smatt#if defined(__mips_o32)
2731.1Schristos	memcpy(&f->f_regs[1], &scp->sc_regs[1],
2741.1Schristos	    sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0]));
2751.14Srmind
2761.15Smatt#else
2771.16Suebayasi	size_t i;
2781.16Suebayasi	for (i = 1; i < __arraycount(f->f_regs); i++)
2791.15Smatt		f->f_regs[i] = ksc.sc_regs[i];
2801.15Smatt#endif
2811.1Schristos#ifndef	SOFTFLOAT
2821.1Schristos	if (scp->sc_fpused) {
2831.1Schristos		/* Disable the FPU to fault in FP registers. */
2841.2She		f->f_regs[_R_SR] &= ~MIPS_SR_COP_1_BIT;
2851.15Smatt		fpcurlwp = &lwp0;
2861.1Schristos	}
2871.1Schristos#endif
2881.15Smatt	pcb = lwp_getpcb(l);
2891.15Smatt	*(struct fpreg *)&pcb->pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
2901.1Schristos
2911.11Sad	mutex_enter(p->p_lock);
2921.1Schristos	/* Restore signal stack. */
2931.1Schristos	if (ksc.sc_onstack & SS_ONSTACK)
2941.8Sad		l->l_sigstk.ss_flags |= SS_ONSTACK;
2951.1Schristos	else
2961.8Sad		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
2971.1Schristos	/* Restore signal mask. */
2981.8Sad	(void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0);
2991.11Sad	mutex_exit(p->p_lock);
3001.1Schristos
3011.1Schristos	return (EJUSTRETURN);
3021.1Schristos}
3031.4Sdrochner#endif /* COMPAT_16 */
304