Home | History | Annotate | Line # | Download | only in hppa
sig_machdep.c revision 1.5
      1 /*	$NetBSD: sig_machdep.c,v 1.5 2003/08/31 01:26:36 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.5 2003/08/31 01:26:36 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(sig, mask, code)
    148 	int sig;
    149 	sigset_t *mask;
    150 	u_long code;
    151 {
    152 	struct lwp *l = curlwp;
    153 	struct proc *p = l->l_proc;
    154 	struct sigacts *ps = p->p_sigacts;
    155 	struct sigframe *fp, kf;
    156 	caddr_t sp;
    157 	struct trapframe *tf;
    158 	int onstack, fsize;
    159 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    160 
    161 	tf = (struct trapframe *)l->l_md.md_regs;
    162 
    163 	/* Do we need to jump onto the signal stack? */
    164 	onstack =
    165 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    166 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    167 
    168 	/*
    169 	 * Allocate space for the signal handler context.
    170 	 * The PA-RISC calling convention mandates that
    171 	 * the stack pointer must always be 64-byte aligned,
    172 	 * and points to the first *unused* byte.
    173 	 */
    174 	fsize = sizeof(struct sigframe);
    175 	sp = (onstack ?
    176 	      (caddr_t)p->p_sigctx.ps_sigstk.ss_sp :
    177 	      (caddr_t)tf->tf_sp);
    178 	sp = (caddr_t)(((u_int)(sp + fsize + 63)) & ~63);
    179 	fp = (struct sigframe *) (sp - fsize);
    180 
    181 #ifdef DEBUG
    182 	if ((sigdebug & SDB_FOLLOW) && (!sigpid || p->p_pid == sigpid))
    183 		printf("sendsig: %s[%d] sig %d catcher %p\n",
    184 		    p->p_comm, p->p_pid, sig, catcher);
    185 #endif
    186 
    187 	/*
    188 	 * Save necessary hardware state.  Currently this includes:
    189 	 *      - original exception frame
    190 	 *      - FP coprocessor state
    191 	 */
    192 	kf.sf_state.ss_flags = SS_USERREGS;
    193 	memcpy(&kf.sf_state.ss_frame, tf, sizeof(*tf));
    194 	/* XXX FP state */
    195 
    196 	/* Build the signal context to be used by sigreturn. */
    197 	kf.sf_sc.sc_sp = tf->tf_sp;
    198 	kf.sf_sc.sc_fp = tf->tf_sp;	/* XXX fredette - is this right? */
    199 	kf.sf_sc.sc_ap = (int)&fp->sf_state;
    200 	kf.sf_sc.sc_pcsqh = tf->tf_iisq_head;
    201 	kf.sf_sc.sc_pcoqh = tf->tf_iioq_head;
    202 	kf.sf_sc.sc_pcsqt = tf->tf_iisq_tail;
    203 	kf.sf_sc.sc_pcoqt = tf->tf_iioq_tail;
    204 	kf.sf_sc.sc_ps = tf->tf_ipsw;
    205 
    206 	/* Save signal stack. */
    207 	kf.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
    208 
    209 	/* Save signal mask. */
    210 	kf.sf_sc.sc_mask = *mask;
    211 
    212 	/* Fill the calling convention part of the signal frame. */
    213 	kf.sf_psp = 0;
    214 	kf.sf_clup = 0;		/* XXX fredette - is this right? */
    215 	kf.sf_sl = 0;		/* XXX fredette - is this right? */
    216 	kf.sf_edp = 0;		/* XXX fredette - is this right? */
    217 
    218 	/* Copy out the signal frame. */
    219 	if (copyout(&kf, fp, fsize)) {
    220 #ifdef DEBUG
    221 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    222 			printf("sendsig(%d): copyout failed on sig %d\n",
    223 			       p->p_pid, sig);
    224 #endif
    225 		/*
    226 		 * Process has trashed its stack; give it an illegal
    227 		 * instruction to halt it in its tracks.
    228 		 */
    229 		sigexit(l, SIGILL);
    230 		/* NOTREACHED */
    231 	}
    232 #ifdef DEBUG
    233 	if (sigdebug & SDB_FOLLOW)
    234 		printf("sendsig(%d): sig %d scp %p fp %p sc_sp %x sc_ap %x\n",
    235 		       p->p_pid, sig, &fp->sf_sc, fp,
    236 		       kf.sf_sc.sc_sp, kf.sf_sc.sc_ap);
    237 #endif
    238 
    239 	/* Set up the registers to return to sigcode. */
    240 	switch (ps->sa_sigdesc[sig].sd_vers) {
    241 #if 1 /* COMPAT_16 */
    242 	case 0:		/* legacy on-stack sigtramp */
    243 		tf->tf_iioq_head =
    244 		    (int)p->p_sigctx.ps_sigcode | HPPA_PC_PRIV_USER;
    245 		tf->tf_iioq_tail = tf->tf_iioq_head + 4;
    246 		break;
    247 #endif
    248 
    249 	case 1:
    250 		tf->tf_iioq_head =
    251 		    (int)ps->sa_sigdesc[sig].sd_tramp | HPPA_PC_PRIV_USER;
    252 		tf->tf_iioq_tail = tf->tf_iioq_head + 4;
    253 		break;
    254 
    255 	default:
    256 		/* Don't know what trampoline version; kill it. */
    257 		sigexit(l, SIGILL);
    258 	}
    259 
    260 	tf->tf_sp = (int)sp;
    261 	tf->tf_r3 = (int)&fp->sf_sc;
    262 	tf->tf_arg0 = sig;
    263 	tf->tf_arg1 = code;
    264 	tf->tf_arg2 = (int)&fp->sf_sc;
    265 	tf->tf_arg3 = (int)catcher;
    266 
    267 	/* Remember that we're now on the signal stack. */
    268 	if (onstack)
    269 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    270 
    271 #ifdef DEBUG
    272 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    273 		printf("sendsig(%d): sig %d returns\n",
    274 		       p->p_pid, sig);
    275 #endif
    276 }
    277 
    278 int
    279 sys___sigreturn14(l, v, retval)
    280 	struct lwp *l;
    281 	void *v;
    282 	register_t *retval;
    283 {
    284 	struct sys___sigreturn14_args /* {
    285 		syscallarg(struct sigcontext *) sigcntxp;
    286 	} */ *uap = v;
    287 	struct proc *p = l->l_proc;
    288 	struct sigcontext *scp;
    289 	struct trapframe *tf;
    290 	struct sigcontext tsigc;
    291 	struct sigstate tstate;
    292 	int rf, flags;
    293 
    294 	/*
    295 	 * The trampoline code hands us the context.
    296 	 * It is unsafe to keep track of it ourselves, in the event that a
    297 	 * program jumps out of a signal handler.
    298 	 */
    299 	scp = SCARG(uap, sigcntxp);
    300 #ifdef DEBUG
    301 	if (sigdebug & SDB_FOLLOW)
    302 		printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
    303 #endif
    304 	if ((int)scp & 3)
    305 		return (EINVAL);
    306 
    307 	if (copyin(scp, &tsigc, sizeof(tsigc)) != 0)
    308 		return (EFAULT);
    309 	scp = &tsigc;
    310 
    311 	/* Make sure the user isn't pulling a fast one on us! */
    312 	/* XXX fredette - until this is done, huge security hole here. */
    313 	/* XXX fredette - requiring that PSL_R be zero will hurt debuggers. */
    314 #define PSW_MBS (PSW_C|PSW_Q|PSW_P|PSW_D|PSW_I)
    315 #define PSW_MBZ (PSW_Y|PSW_Z|PSW_S|PSW_X|PSW_M|PSW_R)
    316 	if ((scp->sc_ps & (PSW_MBS|PSW_MBZ)) != PSW_MBS)
    317 		return (EINVAL);
    318 
    319 	/* Restore register context. */
    320 	tf = (struct trapframe *)l->l_md.md_regs;
    321 
    322 	/*
    323 	 * Grab pointer to hardware state information.
    324 	 * If zero, the user is probably doing a longjmp.
    325 	 */
    326 	if ((rf = scp->sc_ap) == 0)
    327 		goto restore;
    328 
    329 	/*
    330 	 * See if there is anything to do before we go to the
    331 	 * expense of copying in the trapframe
    332 	 */
    333 	flags = fuword((caddr_t)rf);
    334 #ifdef DEBUG
    335 	if (sigdebug & SDB_FOLLOW)
    336 		printf("sigreturn(%d): sc_ap %x flags %x\n",
    337 		       p->p_pid, rf, flags);
    338 #endif
    339 	/* fuword failed (bogus sc_ap value). */
    340 	if (flags == -1)
    341 		return (EINVAL);
    342 
    343 	if (flags == 0 || copyin((caddr_t)rf, &tstate, sizeof(tstate)) != 0)
    344 		goto restore;
    345 #ifdef DEBUG
    346 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    347 		printf("sigreturn(%d): ssp %p usp %x scp %p\n",
    348 		       p->p_pid, &flags, scp->sc_sp, SCARG(uap, sigcntxp));
    349 #endif
    350 
    351 	/*
    352 	 * Restore most of the users registers except for those
    353 	 * in the sigcontext; they will be handled below.
    354 	 */
    355 	if (flags & SS_USERREGS) {
    356 
    357 		/*
    358 		 * There are more registers that the user can tell
    359 		 * us to bash than registers that, for security
    360 		 * or other reasons, we must protect.  So it's
    361 		 * easier (but not faster), to copy these sensitive
    362 		 * register values into the user-provided frame,
    363 		 * then bulk-copy the user-provided frame into
    364 		 * the process' frame.
    365 		 */
    366 #define	SIG_PROTECT(r) tstate.ss_frame.r = tf->r
    367 		/* SRs 5,6,7 must be protected. */
    368 		SIG_PROTECT(tf_sr5);
    369 		SIG_PROTECT(tf_sr6);
    370 		SIG_PROTECT(tf_sr7);
    371 
    372 		/* all CRs except CR11 must be protected. */
    373 		SIG_PROTECT(tf_rctr);	/* CR0 */
    374 		/* CRs 1-8 are reserved */
    375 		SIG_PROTECT(tf_pidr1);	/* CR8 */
    376 		SIG_PROTECT(tf_pidr2);	/* CR9 */
    377 		SIG_PROTECT(tf_ccr);	/* CR10 */
    378 		SIG_PROTECT(tf_pidr3);	/* CR12 */
    379 		SIG_PROTECT(tf_pidr4);	/* CR14 */
    380 		SIG_PROTECT(tf_eiem);	/* CR15 */
    381 		/* CR17 is the IISQ head */
    382 		/* CR18 is the IIOQ head */
    383 		SIG_PROTECT(tf_iir);	/* CR19 */
    384 		SIG_PROTECT(tf_isr);	/* CR20 */
    385 		SIG_PROTECT(tf_ior);	/* CR21 */
    386 		/* CR22 is the IPSW */
    387 		SIG_PROTECT(tf_eirr);	/* CR23 */
    388 		SIG_PROTECT(tf_hptm);	/* CR24 */
    389 		SIG_PROTECT(tf_vtop);	/* CR25 */
    390 		/* XXX where are CR26, CR27, CR29, CR31? */
    391 		SIG_PROTECT(tf_cr28);	/* CR28 */
    392 		SIG_PROTECT(tf_cr30);	/* CR30 */
    393 #undef	SIG_PROTECT
    394 
    395 		/* The bulk copy. */
    396 		*tf = tstate.ss_frame;
    397 	}
    398 
    399 	/*
    400 	 * Restore the original FP context
    401 	 */
    402 	/* XXX fredette */
    403 
    404  restore:
    405 	/*
    406 	 * Restore the user supplied information.
    407 	 * This should be at the last so that the error (EINVAL)
    408 	 * is reported to the sigreturn caller, not to the
    409 	 * jump destination.
    410 	 */
    411 
    412 	tf->tf_sp = scp->sc_sp;
    413 	/* XXX should we be doing the space registers? */
    414 	tf->tf_iisq_head = scp->sc_pcsqh;
    415 	tf->tf_iioq_head = scp->sc_pcoqh | HPPA_PC_PRIV_USER;
    416 	tf->tf_iisq_tail = scp->sc_pcsqt;
    417 	tf->tf_iioq_tail = scp->sc_pcoqt | HPPA_PC_PRIV_USER;
    418 	tf->tf_ipsw = scp->sc_ps;
    419 
    420 	/* Restore signal stack. */
    421 	if (scp->sc_onstack & SS_ONSTACK)
    422 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    423 	else
    424 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    425 
    426 	/* Restore signal mask. */
    427 	(void) sigprocmask1(p, SIG_SETMASK, &scp->sc_mask, 0);
    428 
    429 #ifdef DEBUG
    430 #if 0 /* XXX FP state */
    431 	if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
    432 		printf("sigreturn(%d): copied in FP state (%x) at %p\n",
    433 		       p->p_pid, *(u_int *)&tstate.ss_fpstate,
    434 		       &tstate.ss_fpstate);
    435 #endif
    436 	if ((sigdebug & SDB_FOLLOW) ||
    437 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
    438 		printf("sigreturn(%d): returns\n", p->p_pid);
    439 #endif
    440 	return (EJUSTRETURN);
    441 }
    442 
    443