Home | History | Annotate | Line # | Download | only in hppa
sig_machdep.c revision 1.7
      1 /*	$NetBSD: sig_machdep.c,v 1.7 2003/11/28 19:02:25 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matthew Fredette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1982, 1986, 1990, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	from: Utah Hdr: machdep.c 1.74 92/12/20
     72  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
     73  */
     74 /*
     75  * Copyright (c) 1988 University of Utah.
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * the Systems Programming Group of the University of Utah Computer
     79  * Science Department.
     80  *
     81  * Redistribution and use in source and binary forms, with or without
     82  * modification, are permitted provided that the following conditions
     83  * are met:
     84  * 1. Redistributions of source code must retain the above copyright
     85  *    notice, this list of conditions and the following disclaimer.
     86  * 2. Redistributions in binary form must reproduce the above copyright
     87  *    notice, this list of conditions and the following disclaimer in the
     88  *    documentation and/or other materials provided with the distribution.
     89  * 3. All advertising materials mentioning features or use of this software
     90  *    must display the following acknowledgement:
     91  *	This product includes software developed by the University of
     92  *	California, Berkeley and its contributors.
     93  * 4. Neither the name of the University nor the names of its contributors
     94  *    may be used to endorse or promote products derived from this software
     95  *    without specific prior written permission.
     96  *
     97  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     98  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     99  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    100  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    101  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    102  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    103  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    104  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    105  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    106  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    107  * SUCH DAMAGE.
    108  *
    109  *	from: Utah Hdr: machdep.c 1.74 92/12/20
    110  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
    111  */
    112 
    113 #include <sys/cdefs.h>
    114 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.7 2003/11/28 19:02:25 chs Exp $");
    115 
    116 #include "opt_compat_netbsd.h"
    117 
    118 #define __HPPA_SIGNAL_PRIVATE
    119 
    120 #include <sys/param.h>
    121 #include <sys/systm.h>
    122 #include <sys/kernel.h>
    123 #include <sys/proc.h>
    124 #include <sys/user.h>
    125 #include <sys/signal.h>
    126 #include <sys/signalvar.h>
    127 
    128 #include <sys/mount.h>
    129 #include <sys/sa.h>
    130 #include <sys/syscallargs.h>
    131 
    132 #include <machine/cpu.h>
    133 #include <machine/reg.h>
    134 
    135 #ifdef DEBUG
    136 int sigdebug = 0xff;
    137 int sigpid = 0;
    138 #define SDB_FOLLOW	0x01
    139 #define SDB_KSTACK	0x02
    140 #define SDB_FPSTATE	0x04
    141 #endif
    142 
    143 /*
    144  * Send an interrupt to process.
    145  */
    146 void
    147 sendsig(int sig, const sigset_t *mask, u_long code)
    148 {
    149 	struct lwp *l = curlwp;
    150 	struct proc *p = l->l_proc;
    151 	struct sigacts *ps = p->p_sigacts;
    152 	struct sigframe *fp, kf;
    153 	caddr_t sp;
    154 	struct trapframe *tf;
    155 	int onstack, fsize;
    156 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    157 
    158 	tf = (struct trapframe *)l->l_md.md_regs;
    159 
    160 	/* Do we need to jump onto the signal stack? */
    161 	onstack =
    162 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    163 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    164 
    165 	/*
    166 	 * Allocate space for the signal handler context.
    167 	 * The PA-RISC calling convention mandates that
    168 	 * the stack pointer must always be 64-byte aligned,
    169 	 * and points to the first *unused* byte.
    170 	 */
    171 	fsize = sizeof(struct sigframe);
    172 	sp = (onstack ?
    173 	      (caddr_t)p->p_sigctx.ps_sigstk.ss_sp :
    174 	      (caddr_t)tf->tf_sp);
    175 	sp = (caddr_t)(((u_int)(sp + fsize + 63)) & ~63);
    176 	fp = (struct sigframe *) (sp - fsize);
    177 
    178 #ifdef DEBUG
    179 	if ((sigdebug & SDB_FOLLOW) && (!sigpid || p->p_pid == sigpid))
    180 		printf("sendsig: %s[%d] sig %d catcher %p\n",
    181 		    p->p_comm, p->p_pid, sig, catcher);
    182 #endif
    183 
    184 	/*
    185 	 * Save necessary hardware state.  Currently this includes:
    186 	 *      - original exception frame
    187 	 *      - FP coprocessor state
    188 	 */
    189 	kf.sf_state.ss_flags = SS_USERREGS;
    190 	memcpy(&kf.sf_state.ss_frame, tf, sizeof(*tf));
    191 	/* XXX FP state */
    192 
    193 	/* Build the signal context to be used by sigreturn. */
    194 	kf.sf_sc.sc_sp = tf->tf_sp;
    195 	kf.sf_sc.sc_fp = tf->tf_sp;	/* XXX fredette - is this right? */
    196 	kf.sf_sc.sc_ap = (int)&fp->sf_state;
    197 	kf.sf_sc.sc_pcsqh = tf->tf_iisq_head;
    198 	kf.sf_sc.sc_pcoqh = tf->tf_iioq_head;
    199 	kf.sf_sc.sc_pcsqt = tf->tf_iisq_tail;
    200 	kf.sf_sc.sc_pcoqt = tf->tf_iioq_tail;
    201 	kf.sf_sc.sc_ps = tf->tf_ipsw;
    202 
    203 	/* Save signal stack. */
    204 	kf.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
    205 
    206 	/* Save signal mask. */
    207 	kf.sf_sc.sc_mask = *mask;
    208 
    209 	/* Fill the calling convention part of the signal frame. */
    210 	kf.sf_psp = 0;
    211 	kf.sf_clup = 0;		/* XXX fredette - is this right? */
    212 	kf.sf_sl = 0;		/* XXX fredette - is this right? */
    213 	kf.sf_edp = 0;		/* XXX fredette - is this right? */
    214 
    215 	/* Copy out the signal frame. */
    216 	if (copyout(&kf, fp, fsize)) {
    217 #ifdef DEBUG
    218 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    219 			printf("sendsig(%d): copyout failed on sig %d\n",
    220 			       p->p_pid, sig);
    221 #endif
    222 		/*
    223 		 * Process has trashed its stack; give it an illegal
    224 		 * instruction to halt it in its tracks.
    225 		 */
    226 		sigexit(l, SIGILL);
    227 		/* NOTREACHED */
    228 	}
    229 #ifdef DEBUG
    230 	if (sigdebug & SDB_FOLLOW)
    231 		printf("sendsig(%d): sig %d scp %p fp %p sc_sp %x sc_ap %x\n",
    232 		       p->p_pid, sig, &fp->sf_sc, fp,
    233 		       kf.sf_sc.sc_sp, kf.sf_sc.sc_ap);
    234 #endif
    235 
    236 	/* Set up the registers to return to sigcode. */
    237 	switch (ps->sa_sigdesc[sig].sd_vers) {
    238 #if 1 /* COMPAT_16 */
    239 	case 0:		/* legacy on-stack sigtramp */
    240 		tf->tf_iioq_head =
    241 		    (int)p->p_sigctx.ps_sigcode | HPPA_PC_PRIV_USER;
    242 		tf->tf_iioq_tail = tf->tf_iioq_head + 4;
    243 		break;
    244 #endif
    245 
    246 	case 1:
    247 		tf->tf_iioq_head =
    248 		    (int)ps->sa_sigdesc[sig].sd_tramp | HPPA_PC_PRIV_USER;
    249 		tf->tf_iioq_tail = tf->tf_iioq_head + 4;
    250 		break;
    251 
    252 	default:
    253 		/* Don't know what trampoline version; kill it. */
    254 		sigexit(l, SIGILL);
    255 	}
    256 
    257 	tf->tf_sp = (int)sp;
    258 	tf->tf_r3 = (int)&fp->sf_sc;
    259 	tf->tf_arg0 = sig;
    260 	tf->tf_arg1 = code;
    261 	tf->tf_arg2 = (int)&fp->sf_sc;
    262 	tf->tf_arg3 = (int)catcher;
    263 
    264 	/* Remember that we're now on the signal stack. */
    265 	if (onstack)
    266 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    267 
    268 #ifdef DEBUG
    269 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    270 		printf("sendsig(%d): sig %d returns\n",
    271 		       p->p_pid, sig);
    272 #endif
    273 }
    274 
    275 int
    276 sys___sigreturn14(struct lwp *l, void *v, register_t *retval)
    277 {
    278 	struct sys___sigreturn14_args /* {
    279 		syscallarg(struct sigcontext *) sigcntxp;
    280 	} */ *uap = v;
    281 	struct proc *p = l->l_proc;
    282 	struct sigcontext *scp;
    283 	struct trapframe *tf;
    284 	struct sigcontext tsigc;
    285 	struct sigstate tstate;
    286 	int rf, flags;
    287 
    288 	/*
    289 	 * The trampoline code hands us the context.
    290 	 * It is unsafe to keep track of it ourselves, in the event that a
    291 	 * program jumps out of a signal handler.
    292 	 */
    293 	scp = SCARG(uap, sigcntxp);
    294 #ifdef DEBUG
    295 	if (sigdebug & SDB_FOLLOW)
    296 		printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
    297 #endif
    298 	if ((int)scp & 3)
    299 		return (EINVAL);
    300 
    301 	if (copyin(scp, &tsigc, sizeof(tsigc)) != 0)
    302 		return (EFAULT);
    303 	scp = &tsigc;
    304 
    305 	/* Make sure the user isn't pulling a fast one on us! */
    306 	/* XXX fredette - until this is done, huge security hole here. */
    307 	/* XXX fredette - requiring that PSL_R be zero will hurt debuggers. */
    308 #define PSW_MBS (PSW_C|PSW_Q|PSW_P|PSW_D|PSW_I)
    309 #define PSW_MBZ (PSW_Y|PSW_Z|PSW_S|PSW_X|PSW_M|PSW_R)
    310 	if ((scp->sc_ps & (PSW_MBS|PSW_MBZ)) != PSW_MBS)
    311 		return (EINVAL);
    312 
    313 	/* Restore register context. */
    314 	tf = (struct trapframe *)l->l_md.md_regs;
    315 
    316 	/*
    317 	 * Grab pointer to hardware state information.
    318 	 * If zero, the user is probably doing a longjmp.
    319 	 */
    320 	if ((rf = scp->sc_ap) == 0)
    321 		goto restore;
    322 
    323 	/*
    324 	 * See if there is anything to do before we go to the
    325 	 * expense of copying in the trapframe
    326 	 */
    327 	flags = fuword((caddr_t)rf);
    328 #ifdef DEBUG
    329 	if (sigdebug & SDB_FOLLOW)
    330 		printf("sigreturn(%d): sc_ap %x flags %x\n",
    331 		       p->p_pid, rf, flags);
    332 #endif
    333 	/* fuword failed (bogus sc_ap value). */
    334 	if (flags == -1)
    335 		return (EINVAL);
    336 
    337 	if (flags == 0 || copyin((caddr_t)rf, &tstate, sizeof(tstate)) != 0)
    338 		goto restore;
    339 #ifdef DEBUG
    340 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    341 		printf("sigreturn(%d): ssp %p usp %x scp %p\n",
    342 		       p->p_pid, &flags, scp->sc_sp, SCARG(uap, sigcntxp));
    343 #endif
    344 
    345 	/*
    346 	 * Restore most of the users registers except for those
    347 	 * in the sigcontext; they will be handled below.
    348 	 */
    349 	if (flags & SS_USERREGS) {
    350 
    351 		/*
    352 		 * There are more registers that the user can tell
    353 		 * us to bash than registers that, for security
    354 		 * or other reasons, we must protect.  So it's
    355 		 * easier (but not faster), to copy these sensitive
    356 		 * register values into the user-provided frame,
    357 		 * then bulk-copy the user-provided frame into
    358 		 * the process' frame.
    359 		 */
    360 #define	SIG_PROTECT(r) tstate.ss_frame.r = tf->r
    361 		/* SRs 5,6,7 must be protected. */
    362 		SIG_PROTECT(tf_sr5);
    363 		SIG_PROTECT(tf_sr6);
    364 		SIG_PROTECT(tf_sr7);
    365 
    366 		/* all CRs except CR11 must be protected. */
    367 		SIG_PROTECT(tf_rctr);	/* CR0 */
    368 		/* CRs 1-8 are reserved */
    369 		SIG_PROTECT(tf_pidr1);	/* CR8 */
    370 		SIG_PROTECT(tf_pidr2);	/* CR9 */
    371 		SIG_PROTECT(tf_ccr);	/* CR10 */
    372 		SIG_PROTECT(tf_pidr3);	/* CR12 */
    373 		SIG_PROTECT(tf_pidr4);	/* CR14 */
    374 		SIG_PROTECT(tf_eiem);	/* CR15 */
    375 		/* CR17 is the IISQ head */
    376 		/* CR18 is the IIOQ head */
    377 		SIG_PROTECT(tf_iir);	/* CR19 */
    378 		SIG_PROTECT(tf_isr);	/* CR20 */
    379 		SIG_PROTECT(tf_ior);	/* CR21 */
    380 		/* CR22 is the IPSW */
    381 		SIG_PROTECT(tf_eirr);	/* CR23 */
    382 		SIG_PROTECT(tf_hptm);	/* CR24 */
    383 		SIG_PROTECT(tf_vtop);	/* CR25 */
    384 		/* XXX where are CR26, CR27, CR29, CR31? */
    385 		SIG_PROTECT(tf_cr28);	/* CR28 */
    386 		SIG_PROTECT(tf_cr30);	/* CR30 */
    387 #undef	SIG_PROTECT
    388 
    389 		/* The bulk copy. */
    390 		*tf = tstate.ss_frame;
    391 	}
    392 
    393 	/*
    394 	 * Restore the original FP context
    395 	 */
    396 	/* XXX fredette */
    397 
    398  restore:
    399 	/*
    400 	 * Restore the user supplied information.
    401 	 * This should be at the last so that the error (EINVAL)
    402 	 * is reported to the sigreturn caller, not to the
    403 	 * jump destination.
    404 	 */
    405 
    406 	tf->tf_sp = scp->sc_sp;
    407 	/* XXX should we be doing the space registers? */
    408 	tf->tf_iisq_head = scp->sc_pcsqh;
    409 	tf->tf_iioq_head = scp->sc_pcoqh | HPPA_PC_PRIV_USER;
    410 	tf->tf_iisq_tail = scp->sc_pcsqt;
    411 	tf->tf_iioq_tail = scp->sc_pcoqt | HPPA_PC_PRIV_USER;
    412 	tf->tf_ipsw = scp->sc_ps;
    413 
    414 	/* Restore signal stack. */
    415 	if (scp->sc_onstack & SS_ONSTACK)
    416 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    417 	else
    418 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    419 
    420 	/* Restore signal mask. */
    421 	(void) sigprocmask1(p, SIG_SETMASK, &scp->sc_mask, 0);
    422 
    423 #ifdef DEBUG
    424 #if 0 /* XXX FP state */
    425 	if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
    426 		printf("sigreturn(%d): copied in FP state (%x) at %p\n",
    427 		       p->p_pid, *(u_int *)&tstate.ss_fpstate,
    428 		       &tstate.ss_fpstate);
    429 #endif
    430 	if ((sigdebug & SDB_FOLLOW) ||
    431 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
    432 		printf("sigreturn(%d): returns\n", p->p_pid);
    433 #endif
    434 	return (EJUSTRETURN);
    435 }
    436 
    437