xref: /netbsd-src/sys/arch/mips/mips/sig_machdep.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: sig_machdep.c,v 1.10 2004/07/02 12:32:16 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
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 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
40 
41 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.10 2004/07/02 12:32:16 simonb Exp $");
42 
43 #include "opt_cputype.h"
44 #include "opt_compat_netbsd.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/user.h>
51 #include <sys/signal.h>
52 #include <sys/signalvar.h>
53 #include <sys/mount.h>
54 #include <sys/sa.h>
55 #include <sys/syscallargs.h>
56 
57 #include <machine/cpu.h>
58 
59 #include <mips/frame.h>
60 #include <mips/regnum.h>
61 
62 void *
63 getframe(struct lwp *l, int sig, int *onstack)
64 {
65 	struct proc *p = l->l_proc;
66 	struct sigctx *ctx = &p->p_sigctx;
67 	struct frame *fp = l->l_md.md_regs;
68 
69 	/* Do we need to jump onto the signal stack? */
70 	*onstack = (ctx->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
71 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
72 	if (*onstack)
73 		return (char *)ctx->ps_sigstk.ss_sp + ctx->ps_sigstk.ss_size;
74 	else
75 		return (void *)fp->f_regs[_R_SP];
76 }
77 
78 struct sigframe_siginfo {
79 	siginfo_t sf_si;
80 	ucontext_t sf_uc;
81 };
82 
83 /*
84  * Send a signal to process.
85  */
86 static void
87 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
88 {
89 	struct lwp *l = curlwp;
90 	struct proc *p = l->l_proc;
91 	struct sigacts *ps = p->p_sigacts;
92 	int onstack;
93 	int sig = ksi->ksi_signo;
94 	struct sigframe_siginfo *fp = getframe(l, sig, &onstack);
95 	struct frame *tf;
96 	ucontext_t uc;
97 	size_t ucsz;
98 	sig_t catcher = SIGACTION(p, sig).sa_handler;
99 
100 	tf = (struct frame *)l->l_md.md_regs;
101 	fp--;
102 
103         /* Build stack frame for signal trampoline. */
104         switch (ps->sa_sigdesc[sig].sd_vers) {
105         case 0:         /* handled by sendsig_sigcontext */
106         case 1:         /* handled by sendsig_sigcontext */
107         default:        /* unknown version */
108                 printf("sendsig_siginfo: bad version %d\n",
109                     ps->sa_sigdesc[sig].sd_vers);
110                 sigexit(l, SIGILL);
111         case 2:
112                 break;
113         }
114 
115         uc.uc_flags = _UC_SIGMASK
116             | ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
117             ? _UC_SETSTACK : _UC_CLRSTACK);
118         uc.uc_sigmask = *mask;
119         uc.uc_link = NULL;
120         memset(&uc.uc_stack, 0, sizeof(uc.uc_stack));
121         cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
122         ucsz = (char *)&uc.__uc_pad - (char *)&uc;
123 
124 	if (copyout(&ksi->ksi_info, &fp->sf_si, sizeof(ksi->ksi_info)) != 0 ||
125 	    copyout(&uc, &fp->sf_uc, ucsz) != 0) {
126 		/*
127 		 * Process has trashed its stack; give it an illegal
128 		 * instruction to halt it in its tracks.
129 		 */
130 		sigexit(l, SIGILL);
131 		/* NOTREACHED */
132 	}
133 
134 	/*
135 	 * Set up the registers to directly invoke the signal
136 	 * handler.  The return address will be set up to point
137 	 * to the signal trampoline to bounce us back.
138 	 */
139 	tf->f_regs[_R_A0] = sig;
140 	tf->f_regs[_R_A1] = (__greg_t)&fp->sf_si;
141 	tf->f_regs[_R_A2] = (__greg_t)&fp->sf_uc;
142 
143 	tf->f_regs[_R_PC] = (__greg_t)catcher;
144 	tf->f_regs[_R_T9] = (__greg_t)catcher;
145 	tf->f_regs[_R_SP] = (__greg_t)fp;
146 	tf->f_regs[_R_RA] = (__greg_t)ps->sa_sigdesc[sig].sd_tramp;
147 
148 	/* Remember that we're now on the signal stack. */
149 	if (onstack)
150 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
151 }
152 
153 void
154 sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
155 {
156 #ifdef COMPAT_16
157 	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2)
158 		sendsig_sigcontext(ksi, mask);
159 	else
160 #endif
161 		sendsig_siginfo(ksi, mask);
162 }
163