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