netbsd32_signal.c revision 1.8 1 1.8 perry /* $NetBSD: netbsd32_signal.c,v 1.8 2005/02/26 23:10:21 perry Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1998, 2001 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.2 lukem
31 1.2 lukem #include <sys/cdefs.h>
32 1.8 perry __KERNEL_RCSID(0, "$NetBSD: netbsd32_signal.c,v 1.8 2005/02/26 23:10:21 perry Exp $");
33 1.1 mrg
34 1.1 mrg #include <sys/param.h>
35 1.1 mrg #include <sys/systm.h>
36 1.1 mrg #include <sys/malloc.h>
37 1.1 mrg #include <sys/mount.h>
38 1.1 mrg #include <sys/stat.h>
39 1.1 mrg #include <sys/time.h>
40 1.1 mrg #include <sys/signalvar.h>
41 1.1 mrg #include <sys/proc.h>
42 1.7 fvdl #include <sys/wait.h>
43 1.7 fvdl
44 1.7 fvdl #include <uvm/uvm_extern.h>
45 1.1 mrg
46 1.1 mrg #include <compat/netbsd32/netbsd32.h>
47 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
48 1.1 mrg
49 1.1 mrg int
50 1.6 thorpej netbsd32_sigaction(l, v, retval)
51 1.6 thorpej struct lwp *l;
52 1.1 mrg void *v;
53 1.1 mrg register_t *retval;
54 1.1 mrg {
55 1.1 mrg struct netbsd32_sigaction_args /* {
56 1.1 mrg syscallarg(int) signum;
57 1.1 mrg syscallarg(const netbsd32_sigactionp_t) nsa;
58 1.1 mrg syscallarg(netbsd32_sigactionp_t) osa;
59 1.1 mrg } */ *uap = v;
60 1.1 mrg struct sigaction nsa, osa;
61 1.1 mrg struct netbsd32_sigaction *sa32p, sa32;
62 1.1 mrg int error;
63 1.1 mrg
64 1.1 mrg if (SCARG(uap, nsa)) {
65 1.4 scw sa32p =
66 1.4 scw (struct netbsd32_sigaction *)NETBSD32PTR64(SCARG(uap, nsa));
67 1.1 mrg if (copyin(sa32p, &sa32, sizeof(sa32)))
68 1.1 mrg return EFAULT;
69 1.5 atatat nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
70 1.5 atatat nsa.sa_mask = sa32.netbsd32_sa_mask;
71 1.5 atatat nsa.sa_flags = sa32.netbsd32_sa_flags;
72 1.1 mrg }
73 1.8 perry error = sigaction1(l->l_proc, SCARG(uap, signum),
74 1.8 perry SCARG(uap, nsa) ? &nsa : 0,
75 1.3 thorpej SCARG(uap, osa) ? &osa : 0,
76 1.3 thorpej NULL, 0);
77 1.8 perry
78 1.1 mrg if (error)
79 1.1 mrg return (error);
80 1.1 mrg
81 1.1 mrg if (SCARG(uap, osa)) {
82 1.5 atatat sa32.netbsd32_sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
83 1.5 atatat sa32.netbsd32_sa_mask = osa.sa_mask;
84 1.5 atatat sa32.netbsd32_sa_flags = osa.sa_flags;
85 1.4 scw sa32p =
86 1.4 scw (struct netbsd32_sigaction *)NETBSD32PTR64(SCARG(uap, osa));
87 1.1 mrg if (copyout(&sa32, sa32p, sizeof(sa32)))
88 1.1 mrg return EFAULT;
89 1.1 mrg }
90 1.1 mrg
91 1.1 mrg return (0);
92 1.1 mrg }
93 1.1 mrg
94 1.1 mrg int
95 1.6 thorpej netbsd32___sigaltstack14(l, v, retval)
96 1.6 thorpej struct lwp *l;
97 1.1 mrg void *v;
98 1.1 mrg register_t *retval;
99 1.1 mrg {
100 1.1 mrg struct netbsd32___sigaltstack14_args /* {
101 1.1 mrg syscallarg(const netbsd32_sigaltstackp_t) nss;
102 1.1 mrg syscallarg(netbsd32_sigaltstackp_t) oss;
103 1.1 mrg } */ *uap = v;
104 1.1 mrg struct netbsd32_sigaltstack s32;
105 1.1 mrg struct sigaltstack nss, oss;
106 1.1 mrg int error;
107 1.1 mrg
108 1.1 mrg if (SCARG(uap, nss)) {
109 1.4 scw error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nss)), &s32,
110 1.4 scw sizeof(s32));
111 1.1 mrg if (error)
112 1.1 mrg return (error);
113 1.4 scw nss.ss_sp = (void *)NETBSD32PTR64(s32.ss_sp);
114 1.1 mrg nss.ss_size = (size_t)s32.ss_size;
115 1.1 mrg nss.ss_flags = s32.ss_flags;
116 1.1 mrg }
117 1.6 thorpej error = sigaltstack1(l->l_proc,
118 1.1 mrg SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
119 1.1 mrg if (error)
120 1.1 mrg return (error);
121 1.1 mrg if (SCARG(uap, oss)) {
122 1.1 mrg s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
123 1.1 mrg s32.ss_size = (netbsd32_size_t)oss.ss_size;
124 1.1 mrg s32.ss_flags = oss.ss_flags;
125 1.4 scw error = copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, oss)),
126 1.4 scw sizeof(s32));
127 1.1 mrg if (error)
128 1.1 mrg return (error);
129 1.1 mrg }
130 1.1 mrg return (0);
131 1.1 mrg }
132 1.1 mrg
133 1.1 mrg /* ARGSUSED */
134 1.1 mrg int
135 1.6 thorpej netbsd32___sigaction14(l, v, retval)
136 1.6 thorpej struct lwp *l;
137 1.1 mrg void *v;
138 1.1 mrg register_t *retval;
139 1.1 mrg {
140 1.1 mrg struct netbsd32___sigaction14_args /* {
141 1.1 mrg syscallarg(int) signum;
142 1.1 mrg syscallarg(const struct sigaction *) nsa;
143 1.1 mrg syscallarg(struct sigaction *) osa;
144 1.1 mrg } */ *uap = v;
145 1.1 mrg struct netbsd32_sigaction sa32;
146 1.1 mrg struct sigaction nsa, osa;
147 1.1 mrg int error;
148 1.1 mrg
149 1.1 mrg if (SCARG(uap, nsa)) {
150 1.4 scw error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
151 1.4 scw sizeof(sa32));
152 1.1 mrg if (error)
153 1.1 mrg return (error);
154 1.5 atatat nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
155 1.5 atatat nsa.sa_mask = sa32.netbsd32_sa_mask;
156 1.5 atatat nsa.sa_flags = sa32.netbsd32_sa_flags;
157 1.1 mrg }
158 1.6 thorpej error = sigaction1(l->l_proc, SCARG(uap, signum),
159 1.3 thorpej SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
160 1.3 thorpej NULL, 0);
161 1.1 mrg if (error)
162 1.1 mrg return (error);
163 1.1 mrg if (SCARG(uap, osa)) {
164 1.5 atatat sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
165 1.5 atatat sa32.netbsd32_sa_mask = osa.sa_mask;
166 1.5 atatat sa32.netbsd32_sa_flags = osa.sa_flags;
167 1.4 scw error = copyout(&sa32, (caddr_t)NETBSD32PTR64(SCARG(uap, osa)),
168 1.4 scw sizeof(sa32));
169 1.4 scw if (error)
170 1.4 scw return (error);
171 1.4 scw }
172 1.4 scw return (0);
173 1.4 scw }
174 1.4 scw
175 1.4 scw /* ARGSUSED */
176 1.4 scw int
177 1.6 thorpej netbsd32___sigaction_sigtramp(l, v, retval)
178 1.6 thorpej struct lwp *l;
179 1.4 scw void *v;
180 1.4 scw register_t *retval;
181 1.4 scw {
182 1.4 scw struct netbsd32___sigaction_sigtramp_args /* {
183 1.4 scw syscallarg(int) signum;
184 1.4 scw syscallarg(const netbsd32_sigactionp_t) nsa;
185 1.4 scw syscallarg(netbsd32_sigactionp_t) osa;
186 1.4 scw syscallarg(netbsd32_voidp) tramp;
187 1.4 scw syscallarg(int) vers;
188 1.4 scw } */ *uap = v;
189 1.6 thorpej struct proc *p = l->l_proc;
190 1.4 scw struct netbsd32_sigaction sa32;
191 1.4 scw struct sigaction nsa, osa;
192 1.4 scw int error;
193 1.4 scw
194 1.4 scw if (SCARG(uap, nsa)) {
195 1.4 scw error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
196 1.4 scw sizeof(sa32));
197 1.4 scw if (error)
198 1.4 scw return (error);
199 1.5 atatat nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
200 1.5 atatat nsa.sa_mask = sa32.netbsd32_sa_mask;
201 1.5 atatat nsa.sa_flags = sa32.netbsd32_sa_flags;
202 1.4 scw }
203 1.4 scw error = sigaction1(p, SCARG(uap, signum),
204 1.4 scw SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
205 1.4 scw NETBSD32PTR64(SCARG(uap, tramp)), SCARG(uap, vers));
206 1.4 scw if (error)
207 1.4 scw return (error);
208 1.4 scw if (SCARG(uap, osa)) {
209 1.5 atatat sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
210 1.5 atatat sa32.netbsd32_sa_mask = osa.sa_mask;
211 1.5 atatat sa32.netbsd32_sa_flags = osa.sa_flags;
212 1.4 scw error = copyout(&sa32, (caddr_t)NETBSD32PTR64(SCARG(uap, osa)),
213 1.4 scw sizeof(sa32));
214 1.1 mrg if (error)
215 1.1 mrg return (error);
216 1.1 mrg }
217 1.1 mrg return (0);
218 1.7 fvdl }
219 1.7 fvdl
220 1.7 fvdl void
221 1.7 fvdl netbsd32_si32_to_si(siginfo_t *si, siginfo32_t *si32)
222 1.7 fvdl {
223 1.7 fvdl memset(si, 0, sizeof (*si));
224 1.7 fvdl si->si_signo = si32->si_signo;
225 1.7 fvdl si->si_code = si32->si_code;
226 1.7 fvdl si->si_errno = si32->si_errno;
227 1.7 fvdl
228 1.7 fvdl switch (si32->si_signo) {
229 1.7 fvdl case SIGILL:
230 1.7 fvdl case SIGBUS:
231 1.7 fvdl case SIGSEGV:
232 1.7 fvdl case SIGFPE:
233 1.7 fvdl case SIGTRAP:
234 1.7 fvdl si->si_addr = (void *)NETBSD32PTR64(si32->si_addr);
235 1.7 fvdl si->si_trap = si32->si_trap;
236 1.7 fvdl break;
237 1.7 fvdl case SIGALRM:
238 1.7 fvdl case SIGVTALRM:
239 1.7 fvdl case SIGPROF:
240 1.7 fvdl si->si_pid = si32->si_pid;
241 1.7 fvdl si->si_uid = si32->si_uid;
242 1.7 fvdl /*
243 1.7 fvdl * XXX sival_ptr is currently unused.
244 1.7 fvdl */
245 1.7 fvdl si->si_sigval.sival_int = si32->si_sigval.sival_int;
246 1.7 fvdl break;
247 1.7 fvdl case SIGCHLD:
248 1.7 fvdl si->si_pid = si32->si_pid;
249 1.7 fvdl si->si_uid = si32->si_uid;
250 1.7 fvdl si->si_utime = si32->si_utime;
251 1.7 fvdl si->si_stime = si32->si_stime;
252 1.7 fvdl break;
253 1.7 fvdl case SIGURG:
254 1.7 fvdl case SIGIO:
255 1.7 fvdl si->si_band = si32->si_band;
256 1.7 fvdl si->si_fd = si32->si_fd;
257 1.7 fvdl break;
258 1.7 fvdl }
259 1.7 fvdl }
260 1.7 fvdl
261 1.7 fvdl void
262 1.7 fvdl netbsd32_si_to_si32(siginfo32_t *si32, siginfo_t *si)
263 1.7 fvdl {
264 1.7 fvdl memset(si32, 0, sizeof (*si32));
265 1.7 fvdl si32->si_signo = si->si_signo;
266 1.7 fvdl si32->si_code = si->si_code;
267 1.7 fvdl si32->si_errno = si->si_errno;
268 1.7 fvdl
269 1.7 fvdl switch (si32->si_signo) {
270 1.7 fvdl case SIGILL:
271 1.7 fvdl case SIGBUS:
272 1.7 fvdl case SIGSEGV:
273 1.7 fvdl case SIGFPE:
274 1.7 fvdl case SIGTRAP:
275 1.7 fvdl si32->si_addr = (uint32_t)(uintptr_t)si->si_addr;
276 1.7 fvdl si32->si_trap = si->si_trap;
277 1.7 fvdl break;
278 1.7 fvdl case SIGALRM:
279 1.7 fvdl case SIGVTALRM:
280 1.7 fvdl case SIGPROF:
281 1.7 fvdl si32->si_pid = si->si_pid;
282 1.7 fvdl si32->si_uid = si->si_uid;
283 1.7 fvdl /*
284 1.7 fvdl * XXX sival_ptr is currently unused.
285 1.7 fvdl */
286 1.7 fvdl si32->si_sigval.sival_int = si->si_sigval.sival_int;
287 1.7 fvdl break;
288 1.7 fvdl case SIGCHLD:
289 1.7 fvdl si32->si_pid = si->si_pid;
290 1.7 fvdl si32->si_uid = si->si_uid;
291 1.7 fvdl si32->si_status = si->si_status;
292 1.7 fvdl si32->si_utime = si->si_utime;
293 1.7 fvdl si32->si_stime = si->si_stime;
294 1.7 fvdl break;
295 1.7 fvdl case SIGURG:
296 1.7 fvdl case SIGIO:
297 1.7 fvdl si32->si_band = si->si_band;
298 1.7 fvdl si32->si_fd = si->si_fd;
299 1.7 fvdl break;
300 1.7 fvdl }
301 1.7 fvdl }
302 1.7 fvdl
303 1.7 fvdl void
304 1.7 fvdl getucontext32(struct lwp *l, ucontext32_t *ucp)
305 1.7 fvdl {
306 1.7 fvdl struct proc *p;
307 1.7 fvdl
308 1.7 fvdl p = l->l_proc;
309 1.7 fvdl
310 1.7 fvdl ucp->uc_flags = 0;
311 1.7 fvdl ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink;
312 1.7 fvdl
313 1.7 fvdl (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
314 1.7 fvdl ucp->uc_flags |= _UC_SIGMASK;
315 1.7 fvdl
316 1.7 fvdl /*
317 1.7 fvdl * The (unsupplied) definition of the `current execution stack'
318 1.7 fvdl * in the System V Interface Definition appears to allow returning
319 1.7 fvdl * the main context stack.
320 1.7 fvdl */
321 1.7 fvdl if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
322 1.7 fvdl ucp->uc_stack.ss_sp = USRSTACK32;
323 1.7 fvdl ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
324 1.7 fvdl ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
325 1.7 fvdl } else {
326 1.7 fvdl /* Simply copy alternate signal execution stack. */
327 1.7 fvdl ucp->uc_stack.ss_sp =
328 1.7 fvdl (uint32_t)(intptr_t)p->p_sigctx.ps_sigstk.ss_sp;
329 1.7 fvdl ucp->uc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
330 1.7 fvdl ucp->uc_stack.ss_flags = p->p_sigctx.ps_sigstk.ss_flags;
331 1.7 fvdl }
332 1.7 fvdl ucp->uc_flags |= _UC_STACK;
333 1.7 fvdl
334 1.7 fvdl cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags);
335 1.7 fvdl }
336 1.7 fvdl
337 1.7 fvdl /* ARGSUSED */
338 1.7 fvdl int
339 1.7 fvdl netbsd32_getcontext(struct lwp *l, void *v, register_t *retval)
340 1.7 fvdl {
341 1.7 fvdl struct netbsd32_getcontext_args /* {
342 1.7 fvdl syscallarg(netbsd32_ucontextp) ucp;
343 1.7 fvdl } */ *uap = v;
344 1.7 fvdl ucontext32_t uc;
345 1.7 fvdl
346 1.7 fvdl getucontext32(l, &uc);
347 1.7 fvdl
348 1.7 fvdl return copyout(&uc, NETBSD32PTR64(SCARG(uap, ucp)),
349 1.7 fvdl sizeof (ucontext32_t));
350 1.7 fvdl }
351 1.7 fvdl
352 1.7 fvdl int
353 1.7 fvdl setucontext32(struct lwp *l, const ucontext32_t *ucp)
354 1.7 fvdl {
355 1.7 fvdl struct proc *p;
356 1.7 fvdl int error;
357 1.7 fvdl
358 1.7 fvdl p = l->l_proc;
359 1.7 fvdl if ((error = cpu_setmcontext32(l, &ucp->uc_mcontext,
360 1.7 fvdl ucp->uc_flags)) != 0)
361 1.7 fvdl return (error);
362 1.7 fvdl l->l_ctxlink = (void *)(intptr_t)ucp->uc_link;
363 1.7 fvdl /*
364 1.7 fvdl * We might want to take care of the stack portion here but currently
365 1.7 fvdl * don't; see the comment in getucontext().
366 1.7 fvdl */
367 1.7 fvdl if ((ucp->uc_flags & _UC_SIGMASK) != 0)
368 1.7 fvdl sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
369 1.7 fvdl
370 1.7 fvdl return 0;
371 1.7 fvdl }
372 1.7 fvdl
373 1.7 fvdl /* ARGSUSED */
374 1.7 fvdl int
375 1.7 fvdl netbsd32_setcontext(struct lwp *l, void *v, register_t *retval)
376 1.7 fvdl {
377 1.7 fvdl struct netbsd32_setcontext_args /* {
378 1.7 fvdl syscallarg(netbsd32_ucontextp) ucp;
379 1.7 fvdl } */ *uap = v;
380 1.7 fvdl ucontext32_t uc;
381 1.7 fvdl int error;
382 1.7 fvdl void *p;
383 1.7 fvdl
384 1.7 fvdl p = NETBSD32PTR64(SCARG(uap, ucp));
385 1.7 fvdl if (p == NULL)
386 1.7 fvdl exit1(l, W_EXITCODE(0, 0));
387 1.7 fvdl else if ((error = copyin(p, &uc, sizeof (uc))) != 0 ||
388 1.7 fvdl (error = setucontext32(l, &uc)) != 0)
389 1.7 fvdl return (error);
390 1.7 fvdl
391 1.7 fvdl return (EJUSTRETURN);
392 1.1 mrg }
393