xref: /netbsd/sys/arch/hppa/hppa/sig_machdep.c (revision 6550d01e)
1 /*	$NetBSD: sig_machdep.c,v 1.24 2011/01/14 02:06:26 rmind 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1990, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * the Systems Programming Group of the University of Utah Computer
38  * Science Department.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	from: Utah Hdr: machdep.c 1.74 92/12/20
65  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
66  */
67 /*
68  * Copyright (c) 1988 University of Utah.
69  *
70  * This code is derived from software contributed to Berkeley by
71  * the Systems Programming Group of the University of Utah Computer
72  * Science Department.
73  *
74  * Redistribution and use in source and binary forms, with or without
75  * modification, are permitted provided that the following conditions
76  * are met:
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  * 2. Redistributions in binary form must reproduce the above copyright
80  *    notice, this list of conditions and the following disclaimer in the
81  *    documentation and/or other materials provided with the distribution.
82  * 3. All advertising materials mentioning features or use of this software
83  *    must display the following acknowledgement:
84  *	This product includes software developed by the University of
85  *	California, Berkeley and its contributors.
86  * 4. Neither the name of the University nor the names of its contributors
87  *    may be used to endorse or promote products derived from this software
88  *    without specific prior written permission.
89  *
90  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
91  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
94  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
95  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
96  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
97  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
98  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100  * SUCH DAMAGE.
101  *
102  *	from: Utah Hdr: machdep.c 1.74 92/12/20
103  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
104  */
105 
106 #include <sys/cdefs.h>
107 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.24 2011/01/14 02:06:26 rmind Exp $");
108 
109 #include "opt_compat_netbsd.h"
110 
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/kernel.h>
114 #include <sys/proc.h>
115 #include <sys/signal.h>
116 #include <sys/signalvar.h>
117 
118 #include <sys/mount.h>
119 #include <sys/syscallargs.h>
120 
121 #include <machine/cpu.h>
122 #include <machine/pcb.h>
123 #include <machine/reg.h>
124 #include <machine/frame.h>
125 
126 #ifdef DEBUG
127 int sigdebug = 0;
128 int sigpid = 0;
129 #define SDB_FOLLOW	0x01
130 #define SDB_KSTACK	0x02
131 #define SDB_FPSTATE	0x04
132 #endif
133 
134 static void *getframe(struct lwp *, int, int *);
135 static void *
136 getframe(struct lwp *l, int sig, int *onstack)
137 {
138 	struct proc *p = l->l_proc;
139 	struct trapframe *tf = l->l_md.md_regs;
140 
141 	/* Do we need to jump onto the signal stack? */
142 	*onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
143 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
144 	if (*onstack)
145 		return (void *)HPPA_FRAME_ROUND(l->l_sigstk.ss_sp);
146 	else
147 		return (void *)HPPA_FRAME_ROUND(tf->tf_sp);
148 }
149 
150 struct sigframe_siginfo {
151 	siginfo_t sf_si;
152 	ucontext_t sf_uc;
153 };
154 
155 void
156 sendsig_siginfo(const struct ksiginfo *ksi, const sigset_t *mask)
157 {
158 	struct lwp *l = curlwp;
159 	struct proc *p = l->l_proc;
160 	struct pcb *pcb = lwp_getpcb(l);
161 	struct sigacts *ps = p->p_sigacts;
162 	struct sigframe_siginfo *fp, frame;
163 	struct trapframe *tf;
164 	int sig = ksi->ksi_signo;
165 	sig_t catcher = SIGACTION(p, sig).sa_handler;
166 	int onstack, error;
167 
168 	fp = getframe(l, sig, &onstack);
169 	tf = (struct trapframe *)l->l_md.md_regs;
170 
171 	/* Build stack frame for signal trampoline. */
172 	switch (ps->sa_sigdesc[sig].sd_vers) {
173 	default:
174 		printf("sendsig_siginfo: bad version %d\n",
175 		       ps->sa_sigdesc[sig].sd_vers);
176 		sigexit(l, SIGILL);
177 	case 2:
178 		break;
179 	}
180 
181 	frame.sf_si._info = ksi->ksi_info;
182 	frame.sf_uc.uc_flags = _UC_SIGMASK |
183 		((l->l_sigstk.ss_flags & SS_ONSTACK) ?
184 		 _UC_SETSTACK : _UC_CLRSTACK);
185 	frame.sf_uc.uc_sigmask = *mask;
186 	frame.sf_uc.uc_link = l->l_ctxlink;
187 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
188 	sendsig_reset(l, sig);
189 	mutex_exit(p->p_lock);
190 	cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
191 	error = copyout(&frame, fp, sizeof(frame));
192 	mutex_enter(p->p_lock);
193 
194 	if (error != 0) {
195 
196 		/*
197 		 * Process has trashed its stack; give it an illegal
198 		 * instruction to halt it in its tracks.
199 		 */
200 		sigexit(l, SIGILL);
201 		/* NOTREACHED */
202 	}
203 
204 	/*
205 	 * Set up the registers to invoke the signal trampoline.
206 	 */
207 	tf->tf_arg0 = sig;
208 	tf->tf_arg1 = (__greg_t)&fp->sf_si;
209 	tf->tf_arg2 = (__greg_t)&fp->sf_uc;
210 	tf->tf_r3 = (__greg_t)&fp->sf_uc;
211 
212 	fp++;
213 	tf->tf_iisq_head = tf->tf_iisq_tail = pcb->pcb_space;
214 	tf->tf_iioq_head =
215 		(__greg_t)ps->sa_sigdesc[sig].sd_tramp | HPPA_PC_PRIV_USER;
216 	tf->tf_iioq_tail = tf->tf_iioq_head + 4;
217 	tf->tf_arg3 = (__greg_t)catcher;
218 	tf->tf_sp = HPPA_FRAME_ROUND(fp);
219 
220 	/* Remember that we're now on the signal stack. */
221 	if (onstack)
222 		l->l_sigstk.ss_flags |= SS_ONSTACK;
223 }
224