netbsd32_signal.c revision 1.21.6.1 1 /* $NetBSD: netbsd32_signal.c,v 1.21.6.1 2007/03/18 00:06:37 reinoud Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_signal.c,v 1.21.6.1 2007/03/18 00:06:37 reinoud Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/mount.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <sys/signalvar.h>
41 #include <sys/proc.h>
42 #include <sys/wait.h>
43 #include <sys/dirent.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <compat/netbsd32/netbsd32.h>
48 #include <compat/netbsd32/netbsd32_conv.h>
49 #include <compat/netbsd32/netbsd32_syscallargs.h>
50
51 #include <compat/sys/signal.h>
52 #include <compat/sys/signalvar.h>
53 #include <compat/sys/siginfo.h>
54 #include <compat/sys/ucontext.h>
55
56 #ifdef unused
57 static void netbsd32_si32_to_si(siginfo_t *, const siginfo32_t *);
58 #endif
59
60
61 int
62 netbsd32_sigaction(l, v, retval)
63 struct lwp *l;
64 void *v;
65 register_t *retval;
66 {
67 struct netbsd32_sigaction_args /* {
68 syscallarg(int) signum;
69 syscallarg(const netbsd32_sigactionp_t) nsa;
70 syscallarg(netbsd32_sigactionp_t) osa;
71 } */ *uap = v;
72 struct sigaction nsa, osa;
73 struct netbsd32_sigaction *sa32p, sa32;
74 int error;
75
76 if (NETBSD32PTR64(SCARG(uap, nsa))) {
77 sa32p = NETBSD32PTR64(SCARG(uap, nsa));
78 if (copyin(sa32p, &sa32, sizeof(sa32)))
79 return EFAULT;
80 nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
81 nsa.sa_mask = sa32.netbsd32_sa_mask;
82 nsa.sa_flags = sa32.netbsd32_sa_flags;
83 }
84 error = sigaction1(l, SCARG(uap, signum),
85 NETBSD32PTR64(SCARG(uap, nsa)) ? &nsa : 0,
86 NETBSD32PTR64(SCARG(uap, osa)) ? &osa : 0,
87 NULL, 0);
88
89 if (error)
90 return (error);
91
92 if (NETBSD32PTR64(SCARG(uap, osa))) {
93 NETBSD32PTR32(sa32.netbsd32_sa_handler, osa.sa_handler);
94 sa32.netbsd32_sa_mask = osa.sa_mask;
95 sa32.netbsd32_sa_flags = osa.sa_flags;
96 sa32p = NETBSD32PTR64(SCARG(uap, osa));
97 if (copyout(&sa32, sa32p, sizeof(sa32)))
98 return EFAULT;
99 }
100
101 return (0);
102 }
103
104 int
105 netbsd32___sigaltstack14(l, v, retval)
106 struct lwp *l;
107 void *v;
108 register_t *retval;
109 {
110 struct netbsd32___sigaltstack14_args /* {
111 syscallarg(const netbsd32_sigaltstackp_t) nss;
112 syscallarg(netbsd32_sigaltstackp_t) oss;
113 } */ *uap = v;
114 struct netbsd32_sigaltstack s32;
115 struct sigaltstack nss, oss;
116 int error;
117
118 if (NETBSD32PTR64(SCARG(uap, nss))) {
119 error = copyin(NETBSD32PTR64(SCARG(uap, nss)), &s32,
120 sizeof(s32));
121 if (error)
122 return (error);
123 nss.ss_sp = NETBSD32PTR64(s32.ss_sp);
124 nss.ss_size = (size_t)s32.ss_size;
125 nss.ss_flags = s32.ss_flags;
126 }
127 error = sigaltstack1(l, NETBSD32PTR64(SCARG(uap, nss)) ? &nss : 0,
128 NETBSD32PTR64(SCARG(uap, oss)) ? &oss : 0);
129 if (error)
130 return (error);
131 if (NETBSD32PTR64(SCARG(uap, oss))) {
132 NETBSD32PTR32(s32.ss_sp, oss.ss_sp);
133 s32.ss_size = (netbsd32_size_t)oss.ss_size;
134 s32.ss_flags = oss.ss_flags;
135 error = copyout(&s32, NETBSD32PTR64(SCARG(uap, oss)),
136 sizeof(s32));
137 if (error)
138 return (error);
139 }
140 return (0);
141 }
142
143 /* ARGSUSED */
144 int
145 netbsd32___sigaction14(l, v, retval)
146 struct lwp *l;
147 void *v;
148 register_t *retval;
149 {
150 struct netbsd32___sigaction14_args /* {
151 syscallarg(int) signum;
152 syscallarg(const struct sigaction *) nsa;
153 syscallarg(struct sigaction *) osa;
154 } */ *uap = v;
155 struct netbsd32_sigaction sa32;
156 struct sigaction nsa, osa;
157 int error;
158
159 if (NETBSD32PTR64(SCARG(uap, nsa))) {
160 error = copyin(NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
161 sizeof(sa32));
162 if (error)
163 return (error);
164 nsa.sa_handler = NETBSD32PTR64(sa32.netbsd32_sa_handler);
165 nsa.sa_mask = sa32.netbsd32_sa_mask;
166 nsa.sa_flags = sa32.netbsd32_sa_flags;
167 }
168 error = sigaction1(l, SCARG(uap, signum),
169 NETBSD32PTR64(SCARG(uap, nsa)) ? &nsa : 0,
170 NETBSD32PTR64(SCARG(uap, osa)) ? &osa : 0,
171 NULL, 0);
172 if (error)
173 return (error);
174 if (NETBSD32PTR64(SCARG(uap, osa))) {
175 NETBSD32PTR32(sa32.netbsd32_sa_handler, osa.sa_handler);
176 sa32.netbsd32_sa_mask = osa.sa_mask;
177 sa32.netbsd32_sa_flags = osa.sa_flags;
178 error = copyout(&sa32, NETBSD32PTR64(SCARG(uap, osa)),
179 sizeof(sa32));
180 if (error)
181 return (error);
182 }
183 return (0);
184 }
185
186 /* ARGSUSED */
187 int
188 netbsd32___sigaction_sigtramp(l, v, retval)
189 struct lwp *l;
190 void *v;
191 register_t *retval;
192 {
193 struct netbsd32___sigaction_sigtramp_args /* {
194 syscallarg(int) signum;
195 syscallarg(const netbsd32_sigactionp_t) nsa;
196 syscallarg(netbsd32_sigactionp_t) osa;
197 syscallarg(netbsd32_voidp) tramp;
198 syscallarg(int) vers;
199 } */ *uap = v;
200 struct netbsd32_sigaction sa32;
201 struct sigaction nsa, osa;
202 int error;
203
204 if (NETBSD32PTR64(SCARG(uap, nsa))) {
205 error = copyin(NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
206 sizeof(sa32));
207 if (error)
208 return (error);
209 nsa.sa_handler = NETBSD32PTR64(sa32.netbsd32_sa_handler);
210 nsa.sa_mask = sa32.netbsd32_sa_mask;
211 nsa.sa_flags = sa32.netbsd32_sa_flags;
212 }
213 error = sigaction1(l, SCARG(uap, signum),
214 NETBSD32PTR64(SCARG(uap, nsa)) ? &nsa : 0,
215 NETBSD32PTR64(SCARG(uap, osa)) ? &osa : 0,
216 NETBSD32PTR64(SCARG(uap, tramp)), SCARG(uap, vers));
217 if (error)
218 return (error);
219 if (NETBSD32PTR64(SCARG(uap, osa))) {
220 NETBSD32PTR32(sa32.netbsd32_sa_handler, osa.sa_handler);
221 sa32.netbsd32_sa_mask = osa.sa_mask;
222 sa32.netbsd32_sa_flags = osa.sa_flags;
223 error = copyout(&sa32, NETBSD32PTR64(SCARG(uap, osa)),
224 sizeof(sa32));
225 if (error)
226 return (error);
227 }
228 return (0);
229 }
230
231 #ifdef unused
232 static void
233 netbsd32_si32_to_si(siginfo_t *si, const siginfo32_t *si32)
234 {
235 memset(si, 0, sizeof (*si));
236 si->si_signo = si32->si_signo;
237 si->si_code = si32->si_code;
238 si->si_errno = si32->si_errno;
239
240 switch (si32->si_signo) {
241 case SIGILL:
242 case SIGBUS:
243 case SIGSEGV:
244 case SIGFPE:
245 case SIGTRAP:
246 si->si_addr = NETBSD32PTR64(si32->si_addr);
247 si->si_trap = si32->si_trap;
248 break;
249 case SIGALRM:
250 case SIGVTALRM:
251 case SIGPROF:
252 si->si_pid = si32->si_pid;
253 si->si_uid = si32->si_uid;
254 /*
255 * XXX sival_ptr is currently unused.
256 */
257 si->si_sigval.sival_int = si32->si_sigval.sival_int;
258 break;
259 case SIGCHLD:
260 si->si_pid = si32->si_pid;
261 si->si_uid = si32->si_uid;
262 si->si_utime = si32->si_utime;
263 si->si_stime = si32->si_stime;
264 break;
265 case SIGURG:
266 case SIGIO:
267 si->si_band = si32->si_band;
268 si->si_fd = si32->si_fd;
269 break;
270 }
271 }
272 #endif
273
274 void
275 netbsd32_si_to_si32(siginfo32_t *si32, const siginfo_t *si)
276 {
277 memset(si32, 0, sizeof (*si32));
278 si32->si_signo = si->si_signo;
279 si32->si_code = si->si_code;
280 si32->si_errno = si->si_errno;
281
282 switch (si32->si_signo) {
283 case 0: /* SA */
284 si32->si_sigval.sival_int = si->si_sigval.sival_int;
285 break;
286 case SIGILL:
287 case SIGBUS:
288 case SIGSEGV:
289 case SIGFPE:
290 case SIGTRAP:
291 si32->si_addr = (uint32_t)(uintptr_t)si->si_addr;
292 si32->si_trap = si->si_trap;
293 break;
294 case SIGALRM:
295 case SIGVTALRM:
296 case SIGPROF:
297 si32->si_pid = si->si_pid;
298 si32->si_uid = si->si_uid;
299 /*
300 * XXX sival_ptr is currently unused.
301 */
302 si32->si_sigval.sival_int = si->si_sigval.sival_int;
303 break;
304 case SIGCHLD:
305 si32->si_pid = si->si_pid;
306 si32->si_uid = si->si_uid;
307 si32->si_status = si->si_status;
308 si32->si_utime = si->si_utime;
309 si32->si_stime = si->si_stime;
310 break;
311 case SIGURG:
312 case SIGIO:
313 si32->si_band = si->si_band;
314 si32->si_fd = si->si_fd;
315 break;
316 }
317 }
318
319 void
320 getucontext32(struct lwp *l, ucontext32_t *ucp)
321 {
322 struct proc *p = l->l_proc;
323
324 LOCK_ASSERT(mutex_owned(&p->p_smutex));
325
326 ucp->uc_flags = 0;
327 ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink;
328
329 ucp->uc_sigmask = l->l_sigmask;
330 ucp->uc_flags |= _UC_SIGMASK;
331
332 /*
333 * The (unsupplied) definition of the `current execution stack'
334 * in the System V Interface Definition appears to allow returning
335 * the main context stack.
336 */
337 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
338 ucp->uc_stack.ss_sp = USRSTACK32;
339 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
340 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
341 } else {
342 /* Simply copy alternate signal execution stack. */
343 ucp->uc_stack.ss_sp =
344 (uint32_t)(intptr_t)l->l_sigstk.ss_sp;
345 ucp->uc_stack.ss_size = l->l_sigstk.ss_size;
346 ucp->uc_stack.ss_flags = l->l_sigstk.ss_flags;
347 }
348 ucp->uc_flags |= _UC_STACK;
349 mutex_exit(&p->p_smutex);
350 cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags);
351 mutex_enter(&p->p_smutex);
352 }
353
354 /* ARGSUSED */
355 int
356 netbsd32_getcontext(struct lwp *l, void *v, register_t *retval)
357 {
358 struct netbsd32_getcontext_args /* {
359 syscallarg(netbsd32_ucontextp) ucp;
360 } */ *uap = v;
361 struct proc *p = l->l_proc;
362 ucontext32_t uc;
363
364 mutex_enter(&p->p_smutex);
365 getucontext32(l, &uc);
366 mutex_exit(&p->p_smutex);
367
368 return copyout(&uc, NETBSD32PTR64(SCARG(uap, ucp)),
369 sizeof (ucontext32_t));
370 }
371
372 int
373 setucontext32(struct lwp *l, const ucontext32_t *ucp)
374 {
375 struct proc *p = l->l_proc;
376 int error;
377
378 LOCK_ASSERT(mutex_owned(&p->p_smutex));
379
380 if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
381 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
382 if (error != 0)
383 return error;
384 }
385
386 mutex_exit(&p->p_smutex);
387 error = cpu_setmcontext32(l, &ucp->uc_mcontext, ucp->uc_flags);
388 mutex_enter(&p->p_smutex);
389 if (error != 0)
390 return (error);
391
392 l->l_ctxlink = (void *)(intptr_t)ucp->uc_link;
393
394 /*
395 * If there was stack information, update whether or not we are
396 * still running on an alternate signal stack.
397 */
398 if ((ucp->uc_flags & _UC_STACK) != 0) {
399 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
400 l->l_sigstk.ss_flags |= SS_ONSTACK;
401 else
402 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
403 }
404
405 return 0;
406 }
407
408 /* ARGSUSED */
409 int
410 netbsd32_setcontext(struct lwp *l, void *v, register_t *retval)
411 {
412 struct netbsd32_setcontext_args /* {
413 syscallarg(netbsd32_ucontextp) ucp;
414 } */ *uap = v;
415 ucontext32_t uc;
416 int error;
417 struct proc *p = l->l_proc;
418
419 error = copyin(NETBSD32PTR64(SCARG(uap, ucp)), &uc, sizeof (uc));
420 if (error)
421 return (error);
422 if (!(uc.uc_flags & _UC_CPU))
423 return (EINVAL);
424 mutex_enter(&p->p_smutex);
425 error = setucontext32(l, &uc);
426 mutex_exit(&p->p_smutex);
427 if (error)
428 return (error);
429
430 return (EJUSTRETURN);
431 }
432
433 static int
434 netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
435 {
436 const siginfo_t *info = src;
437 siginfo32_t info32;
438
439 netbsd32_si_to_si32(&info32, info);
440
441 return copyout(&info32, dst, sizeof(info32));
442 }
443
444 static int
445 netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
446 {
447 struct timespec *ts = dst;
448 struct netbsd32_timespec ts32;
449 int error;
450
451 error = copyin(src, &ts32, sizeof(ts32));
452 if (error)
453 return error;
454
455 netbsd32_to_timespec(&ts32, ts);
456 return 0;
457 }
458
459 static int
460 netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
461 {
462 const struct timespec *ts = src;
463 struct netbsd32_timespec ts32;
464
465 netbsd32_from_timespec(ts, &ts32);
466
467 return copyout(&ts32, dst, sizeof(ts32));
468 }
469
470 int
471 netbsd32___sigtimedwait(struct lwp *l, void *v, register_t *retval)
472 {
473 struct netbsd32___sigtimedwait_args /* {
474 syscallarg(netbsd32_sigsetp_t) set;
475 syscallarg(netbsd32_siginfop_t) info;
476 syscallarg(netbsd32_timespecp_t) timeout;
477 } */ *uap = v;
478 struct sys___sigtimedwait_args ua;
479
480 NETBSD32TOP_UAP(set, const sigset_t);
481 NETBSD32TOP_UAP(info, siginfo_t);
482 NETBSD32TOP_UAP(timeout, struct timespec);
483
484 return __sigtimedwait1(l, &ua, retval, netbsd32_sigtimedwait_put_info,
485 netbsd32_sigtimedwait_fetch_timeout,
486 netbsd32_sigtimedwait_put_timeout);
487 }
488