netbsd32_signal.c revision 1.40 1 /* $NetBSD: netbsd32_signal.c,v 1.40 2016/09/13 07:39:45 martin 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_signal.c,v 1.40 2016/09/13 07:39:45 martin Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mount.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/signalvar.h>
38 #include <sys/ktrace.h>
39 #include <sys/proc.h>
40 #include <sys/wait.h>
41 #include <sys/dirent.h>
42
43 #include <uvm/uvm_extern.h>
44
45 #include <compat/netbsd32/netbsd32.h>
46 #include <compat/netbsd32/netbsd32_conv.h>
47 #include <compat/netbsd32/netbsd32_syscallargs.h>
48
49 #include <compat/sys/signal.h>
50 #include <compat/sys/signalvar.h>
51 #include <compat/sys/siginfo.h>
52 #include <compat/sys/ucontext.h>
53 #include <compat/common/compat_sigaltstack.h>
54
55 void netbsd32_ktrpsig(int, sig_t, const sigset_t *, const ksiginfo_t *);
56
57 int
58 netbsd32_sigaction(struct lwp *l, const struct netbsd32_sigaction_args *uap, register_t *retval)
59 {
60 /* {
61 syscallarg(int) signum;
62 syscallarg(const netbsd32_sigactionp_t) nsa;
63 syscallarg(netbsd32_sigactionp_t) osa;
64 } */
65 struct sigaction nsa, osa;
66 struct netbsd32_sigaction13 *sa32p, sa32;
67 int error;
68
69 if (SCARG_P32(uap, nsa)) {
70 sa32p = SCARG_P32(uap, nsa);
71 if (copyin(sa32p, &sa32, sizeof(sa32)))
72 return EFAULT;
73 nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
74 memset(&nsa.sa_mask, 0, sizeof(nsa.sa_mask));
75 nsa.sa_mask.__bits[0] = sa32.netbsd32_sa_mask;
76 nsa.sa_flags = sa32.netbsd32_sa_flags;
77 }
78 error = sigaction1(l, SCARG(uap, signum),
79 SCARG_P32(uap, nsa) ? &nsa : 0,
80 SCARG_P32(uap, osa) ? &osa : 0,
81 NULL, 0);
82
83 if (error)
84 return (error);
85
86 if (SCARG_P32(uap, osa)) {
87 NETBSD32PTR32(sa32.netbsd32_sa_handler, osa.sa_handler);
88 sa32.netbsd32_sa_mask = osa.sa_mask.__bits[0];
89 sa32.netbsd32_sa_flags = osa.sa_flags;
90 sa32p = SCARG_P32(uap, osa);
91 if (copyout(&sa32, sa32p, sizeof(sa32)))
92 return EFAULT;
93 }
94
95 return (0);
96 }
97
98 int
99 netbsd32___sigaltstack14(struct lwp *l, const struct netbsd32___sigaltstack14_args *uap, register_t *retval)
100 {
101 /* {
102 syscallarg(const netbsd32_sigaltstackp_t) nss;
103 syscallarg(netbsd32_sigaltstackp_t) oss;
104 } */
105 compat_sigaltstack(uap, netbsd32_sigaltstack, SS_ONSTACK, SS_DISABLE);
106 }
107
108 /* ARGSUSED */
109 int
110 netbsd32___sigaction14(struct lwp *l, const struct netbsd32___sigaction14_args *uap, register_t *retval)
111 {
112 /* {
113 syscallarg(int) signum;
114 syscallarg(const struct sigaction *) nsa;
115 syscallarg(struct sigaction *) osa;
116 } */
117 struct netbsd32_sigaction sa32;
118 struct sigaction nsa, osa;
119 int error;
120
121 if (SCARG_P32(uap, nsa)) {
122 error = copyin(SCARG_P32(uap, nsa), &sa32, sizeof(sa32));
123 if (error)
124 return (error);
125 nsa.sa_handler = NETBSD32PTR64(sa32.netbsd32_sa_handler);
126 nsa.sa_mask = sa32.netbsd32_sa_mask;
127 nsa.sa_flags = sa32.netbsd32_sa_flags;
128 }
129 error = sigaction1(l, SCARG(uap, signum),
130 SCARG_P32(uap, nsa) ? &nsa : 0,
131 SCARG_P32(uap, osa) ? &osa : 0,
132 NULL, 0);
133 if (error)
134 return (error);
135 if (SCARG_P32(uap, osa)) {
136 NETBSD32PTR32(sa32.netbsd32_sa_handler, osa.sa_handler);
137 sa32.netbsd32_sa_mask = osa.sa_mask;
138 sa32.netbsd32_sa_flags = osa.sa_flags;
139 error = copyout(&sa32, SCARG_P32(uap, osa), sizeof(sa32));
140 if (error)
141 return (error);
142 }
143 return (0);
144 }
145
146 /* ARGSUSED */
147 int
148 netbsd32___sigaction_sigtramp(struct lwp *l, const struct netbsd32___sigaction_sigtramp_args *uap, register_t *retval)
149 {
150 /* {
151 syscallarg(int) signum;
152 syscallarg(const netbsd32_sigactionp_t) nsa;
153 syscallarg(netbsd32_sigactionp_t) osa;
154 syscallarg(netbsd32_voidp) tramp;
155 syscallarg(int) vers;
156 } */
157 struct netbsd32_sigaction sa32;
158 struct sigaction nsa, osa;
159 int error;
160
161 if (SCARG_P32(uap, nsa)) {
162 error = copyin(SCARG_P32(uap, nsa), &sa32, sizeof(sa32));
163 if (error)
164 return (error);
165 nsa.sa_handler = NETBSD32PTR64(sa32.netbsd32_sa_handler);
166 nsa.sa_mask = sa32.netbsd32_sa_mask;
167 nsa.sa_flags = sa32.netbsd32_sa_flags;
168 }
169 error = sigaction1(l, SCARG(uap, signum),
170 SCARG_P32(uap, nsa) ? &nsa : 0,
171 SCARG_P32(uap, osa) ? &osa : 0,
172 SCARG_P32(uap, tramp), SCARG(uap, vers));
173 if (error)
174 return (error);
175 if (SCARG_P32(uap, osa)) {
176 NETBSD32PTR32(sa32.netbsd32_sa_handler, osa.sa_handler);
177 sa32.netbsd32_sa_mask = osa.sa_mask;
178 sa32.netbsd32_sa_flags = osa.sa_flags;
179 error = copyout(&sa32, SCARG_P32(uap, osa), sizeof(sa32));
180 if (error)
181 return (error);
182 }
183 return (0);
184 }
185
186 void
187 netbsd32_ksi32_to_ksi(struct _ksiginfo *si, const struct __ksiginfo32 *si32)
188 {
189 memset(si, 0, sizeof (*si));
190 si->_signo = si32->_signo;
191 si->_code = si32->_code;
192 si->_errno = si32->_errno;
193
194 switch (si32->_signo) {
195 case SIGILL:
196 case SIGBUS:
197 case SIGSEGV:
198 case SIGFPE:
199 case SIGTRAP:
200 si->_reason._fault._addr = NETBSD32IPTR64(si32->_reason._fault._addr);
201 si->_reason._fault._trap = si32->_reason._fault._trap;
202 break;
203 case SIGALRM:
204 case SIGVTALRM:
205 case SIGPROF:
206 default: /* see sigqueue() and kill1() */
207 si->_reason._rt._pid = si32->_reason._rt._pid;
208 si->_reason._rt._uid = si32->_reason._rt._uid;
209 si->_reason._rt._value.sival_int = si32->_reason._rt._value.sival_int;
210 break;
211 case SIGCHLD:
212 si->_reason._child._pid = si32->_reason._child._pid;
213 si->_reason._child._uid = si32->_reason._child._uid;
214 si->_reason._child._utime = si32->_reason._child._utime;
215 si->_reason._child._stime = si32->_reason._child._stime;
216 break;
217 case SIGURG:
218 case SIGIO:
219 si->_reason._poll._band = si32->_reason._poll._band;
220 si->_reason._poll._fd = si32->_reason._poll._fd;
221 break;
222 }
223 }
224
225 static void
226 netbsd32_ksi_to_ksi32(struct __ksiginfo32 *si32, const struct _ksiginfo *si)
227 {
228 memset(si32, 0, sizeof (*si32));
229 si32->_signo = si->_signo;
230 si32->_code = si->_code;
231 si32->_errno = si->_errno;
232
233 switch (si->_signo) {
234 case SIGILL:
235 case SIGBUS:
236 case SIGSEGV:
237 case SIGFPE:
238 case SIGTRAP:
239 si32->_reason._fault._addr =
240 NETBSD32PTR32I(si->_reason._fault._addr);
241 si32->_reason._fault._trap = si->_reason._fault._trap;
242 break;
243 case SIGALRM:
244 case SIGVTALRM:
245 case SIGPROF:
246 default: /* see sigqueue() and kill1() */
247 si32->_reason._rt._pid = si->_reason._rt._pid;
248 si32->_reason._rt._uid = si->_reason._rt._uid;
249 si32->_reason._rt._value.sival_int = si->_reason._rt._value.sival_int;
250 break;
251 case SIGCHLD:
252 si32->_reason._child._pid = si->_reason._child._pid;
253 si32->_reason._child._uid = si->_reason._child._uid;
254 si32->_reason._child._utime = si->_reason._child._utime;
255 si32->_reason._child._stime = si->_reason._child._stime;
256 break;
257 case SIGURG:
258 case SIGIO:
259 si32->_reason._poll._band = si->_reason._poll._band;
260 si32->_reason._poll._fd = si->_reason._poll._fd;
261 break;
262 }
263 }
264
265 void
266 netbsd32_si_to_si32(siginfo32_t *si32, const siginfo_t *si)
267 {
268 memset(si32, 0, sizeof (*si32));
269 si32->si_signo = si->si_signo;
270 si32->si_code = si->si_code;
271 si32->si_errno = si->si_errno;
272
273 switch (si32->si_signo) {
274 case 0: /* SA */
275 si32->si_value.sival_int = si->si_value.sival_int;
276 break;
277 case SIGILL:
278 case SIGBUS:
279 case SIGSEGV:
280 case SIGFPE:
281 case SIGTRAP:
282 si32->si_addr = (uint32_t)(uintptr_t)si->si_addr;
283 si32->si_trap = si->si_trap;
284 break;
285 case SIGALRM:
286 case SIGVTALRM:
287 case SIGPROF:
288 default:
289 si32->si_pid = si->si_pid;
290 si32->si_uid = si->si_uid;
291 si32->si_value.sival_int = si->si_value.sival_int;
292 break;
293 case SIGCHLD:
294 si32->si_pid = si->si_pid;
295 si32->si_uid = si->si_uid;
296 si32->si_status = si->si_status;
297 si32->si_utime = si->si_utime;
298 si32->si_stime = si->si_stime;
299 break;
300 case SIGURG:
301 case SIGIO:
302 si32->si_band = si->si_band;
303 si32->si_fd = si->si_fd;
304 break;
305 }
306 }
307
308 void
309 getucontext32(struct lwp *l, ucontext32_t *ucp)
310 {
311 struct proc *p = l->l_proc;
312
313 KASSERT(mutex_owned(p->p_lock));
314
315 ucp->uc_flags = 0;
316 ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink;
317 ucp->uc_sigmask = l->l_sigmask;
318 ucp->uc_flags |= _UC_SIGMASK;
319
320 /*
321 * The (unsupplied) definition of the `current execution stack'
322 * in the System V Interface Definition appears to allow returning
323 * the main context stack.
324 */
325 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
326 ucp->uc_stack.ss_sp = USRSTACK32;
327 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
328 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
329 } else {
330 /* Simply copy alternate signal execution stack. */
331 ucp->uc_stack.ss_sp =
332 (uint32_t)(intptr_t)l->l_sigstk.ss_sp;
333 ucp->uc_stack.ss_size = l->l_sigstk.ss_size;
334 ucp->uc_stack.ss_flags = l->l_sigstk.ss_flags;
335 }
336 ucp->uc_flags |= _UC_STACK;
337 mutex_exit(p->p_lock);
338 cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags);
339 mutex_enter(p->p_lock);
340 }
341
342 int
343 netbsd32_getcontext(struct lwp *l, const struct netbsd32_getcontext_args *uap, register_t *retval)
344 {
345 /* {
346 syscallarg(netbsd32_ucontextp) ucp;
347 } */
348 struct proc *p = l->l_proc;
349 ucontext32_t uc;
350
351 memset(&uc, 0, sizeof(uc));
352
353 mutex_enter(p->p_lock);
354 getucontext32(l, &uc);
355 mutex_exit(p->p_lock);
356
357 return copyout(&uc, SCARG_P32(uap, ucp), sizeof (ucontext32_t));
358 }
359
360 int
361 setucontext32(struct lwp *l, const ucontext32_t *ucp)
362 {
363 struct proc *p = l->l_proc;
364 int error;
365
366 KASSERT(mutex_owned(p->p_lock));
367
368 if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
369 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
370 if (error != 0)
371 return error;
372 }
373
374 mutex_exit(p->p_lock);
375 error = cpu_setmcontext32(l, &ucp->uc_mcontext, ucp->uc_flags);
376 mutex_enter(p->p_lock);
377 if (error != 0)
378 return (error);
379
380 l->l_ctxlink = (void *)(intptr_t)ucp->uc_link;
381
382 /*
383 * If there was stack information, update whether or not we are
384 * still running on an alternate signal stack.
385 */
386 if ((ucp->uc_flags & _UC_STACK) != 0) {
387 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
388 l->l_sigstk.ss_flags |= SS_ONSTACK;
389 else
390 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
391 }
392
393 return 0;
394 }
395
396 /* ARGSUSED */
397 int
398 netbsd32_setcontext(struct lwp *l, const struct netbsd32_setcontext_args *uap, register_t *retval)
399 {
400 /* {
401 syscallarg(netbsd32_ucontextp) ucp;
402 } */
403 ucontext32_t uc;
404 int error;
405 struct proc *p = l->l_proc;
406
407 error = copyin(SCARG_P32(uap, ucp), &uc, sizeof (uc));
408 if (error)
409 return (error);
410 if (!(uc.uc_flags & _UC_CPU))
411 return (EINVAL);
412 mutex_enter(p->p_lock);
413 error = setucontext32(l, &uc);
414 mutex_exit(p->p_lock);
415 if (error)
416 return (error);
417
418 return (EJUSTRETURN);
419 }
420
421 static int
422 netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
423 {
424 const siginfo_t *info = src;
425 siginfo32_t info32;
426
427 netbsd32_si_to_si32(&info32, info);
428
429 return copyout(&info32, dst, sizeof(info32));
430 }
431
432 static int
433 netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
434 {
435 struct timespec *ts = dst;
436 struct netbsd32_timespec ts32;
437 int error;
438
439 error = copyin(src, &ts32, sizeof(ts32));
440 if (error)
441 return error;
442
443 netbsd32_to_timespec(&ts32, ts);
444 return 0;
445 }
446
447 static int
448 netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
449 {
450 const struct timespec *ts = src;
451 struct netbsd32_timespec ts32;
452
453 netbsd32_from_timespec(ts, &ts32);
454
455 return copyout(&ts32, dst, sizeof(ts32));
456 }
457
458 int
459 netbsd32_____sigtimedwait50(struct lwp *l, const struct netbsd32_____sigtimedwait50_args *uap, register_t *retval)
460 {
461 /* {
462 syscallarg(netbsd32_sigsetp_t) set;
463 syscallarg(netbsd32_siginfop_t) info;
464 syscallarg(netbsd32_timespec50p_t) timeout;
465 } */
466 struct sys_____sigtimedwait50_args ua;
467
468 NETBSD32TOP_UAP(set, const sigset_t);
469 NETBSD32TOP_UAP(info, siginfo_t);
470 NETBSD32TOP_UAP(timeout, struct timespec);
471
472 return sigtimedwait1(l, &ua, retval,
473 copyin,
474 netbsd32_sigtimedwait_put_info,
475 netbsd32_sigtimedwait_fetch_timeout,
476 netbsd32_sigtimedwait_put_timeout);
477 }
478
479 int
480 netbsd32_sigqueueinfo(struct lwp *l,
481 const struct netbsd32_sigqueueinfo_args *uap, register_t *retval)
482 {
483 /* {
484 syscallarg(pid_t) pid;
485 syscallarg(const netbsd32_siginfop_t) info;
486 } */
487 struct __ksiginfo32 ksi32;
488 ksiginfo_t ksi;
489 int error;
490
491 if ((error = copyin(SCARG_P32(uap, info), &ksi32,
492 sizeof(ksi32))) != 0)
493 return error;
494
495 KSI_INIT(&ksi);
496 netbsd32_ksi32_to_ksi(&ksi.ksi_info, &ksi32);
497
498 return kill1(l, SCARG(uap, pid), &ksi, retval);
499 }
500
501 struct netbsd32_ktr_psig {
502 int signo;
503 netbsd32_pointer_t action;
504 sigset_t mask;
505 int code;
506 /* and optional siginfo_t */
507 };
508
509 void
510 netbsd32_ktrpsig(int sig, sig_t action, const sigset_t *mask,
511 const ksiginfo_t *ksi)
512 {
513 struct ktrace_entry *kte;
514 lwp_t *l = curlwp;
515 struct {
516 struct netbsd32_ktr_psig kp;
517 siginfo32_t si;
518 } *kbuf;
519
520 if (!KTRPOINT(l->l_proc, KTR_PSIG))
521 return;
522
523 if (ktealloc(&kte, (void *)&kbuf, l, KTR_PSIG, sizeof(*kbuf)))
524 return;
525
526 kbuf->kp.signo = (char)sig;
527 NETBSD32PTR32(kbuf->kp.action, action);
528 kbuf->kp.mask = *mask;
529
530 if (ksi) {
531 kbuf->kp.code = KSI_TRAPCODE(ksi);
532 (void)memset(&kbuf->si, 0, sizeof(kbuf->si));
533 netbsd32_ksi_to_ksi32(&kbuf->si._info, &ksi->ksi_info);
534 ktesethdrlen(kte, sizeof(*kbuf));
535 } else {
536 kbuf->kp.code = 0;
537 ktesethdrlen(kte, sizeof(struct netbsd32_ktr_psig));
538 }
539
540 ktraddentry(l, kte, KTA_WAITOK);
541 }
542
543
544