1 /* $NetBSD: netbsd32_signal.c,v 1.45 2017/12/17 20:59:27 christos 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.45 2017/12/17 20:59:27 christos 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 memset(si, 0, sizeof (*si)); 192 si->_signo = si32->_signo; 193 si->_code = si32->_code; 194 si->_errno = si32->_errno; 195 196 switch (si32->_signo) { 197 case SIGILL: 198 case SIGBUS: 199 case SIGSEGV: 200 case SIGFPE: 201 case SIGTRAP: 202 si->_reason._fault._addr = 203 NETBSD32IPTR64(si32->_reason._fault._addr); 204 si->_reason._fault._trap = si32->_reason._fault._trap; 205 break; 206 case SIGALRM: 207 case SIGVTALRM: 208 case SIGPROF: 209 default: /* see sigqueue() and kill1() */ 210 si->_reason._rt._pid = si32->_reason._rt._pid; 211 si->_reason._rt._uid = si32->_reason._rt._uid; 212 si->_reason._rt._value.sival_int = 213 si32->_reason._rt._value.sival_int; 214 break; 215 case SIGCHLD: 216 si->_reason._child._pid = si32->_reason._child._pid; 217 si->_reason._child._uid = si32->_reason._child._uid; 218 si->_reason._child._utime = si32->_reason._child._utime; 219 si->_reason._child._stime = si32->_reason._child._stime; 220 break; 221 case SIGURG: 222 case SIGIO: 223 si->_reason._poll._band = si32->_reason._poll._band; 224 si->_reason._poll._fd = si32->_reason._poll._fd; 225 break; 226 } 227 } 228 229 #ifdef notyet 230 #ifdef KTRACE 231 static void 232 netbsd32_ksi_to_ksi32(struct __ksiginfo32 *si32, const struct _ksiginfo *si) 233 { 234 memset(si32, 0, sizeof (*si32)); 235 si32->_signo = si->_signo; 236 si32->_code = si->_code; 237 si32->_errno = si->_errno; 238 239 switch (si->_signo) { 240 case SIGILL: 241 case SIGBUS: 242 case SIGSEGV: 243 case SIGFPE: 244 case SIGTRAP: 245 si32->_reason._fault._addr = 246 NETBSD32PTR32I(si->_reason._fault._addr); 247 si32->_reason._fault._trap = si->_reason._fault._trap; 248 break; 249 case SIGALRM: 250 case SIGVTALRM: 251 case SIGPROF: 252 default: /* see sigqueue() and kill1() */ 253 si32->_reason._rt._pid = si->_reason._rt._pid; 254 si32->_reason._rt._uid = si->_reason._rt._uid; 255 si32->_reason._rt._value.sival_int = 256 si->_reason._rt._value.sival_int; 257 break; 258 case SIGCHLD: 259 si32->_reason._child._pid = si->_reason._child._pid; 260 si32->_reason._child._uid = si->_reason._child._uid; 261 si32->_reason._child._utime = si->_reason._child._utime; 262 si32->_reason._child._stime = si->_reason._child._stime; 263 break; 264 case SIGURG: 265 case SIGIO: 266 si32->_reason._poll._band = si->_reason._poll._band; 267 si32->_reason._poll._fd = si->_reason._poll._fd; 268 break; 269 } 270 } 271 #endif 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_value.sival_int = si->si_value.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 default: 298 si32->si_pid = si->si_pid; 299 si32->si_uid = si->si_uid; 300 si32->si_value.sival_int = si->si_value.sival_int; 301 break; 302 case SIGCHLD: 303 si32->si_pid = si->si_pid; 304 si32->si_uid = si->si_uid; 305 si32->si_status = si->si_status; 306 si32->si_utime = si->si_utime; 307 si32->si_stime = si->si_stime; 308 break; 309 case SIGURG: 310 case SIGIO: 311 si32->si_band = si->si_band; 312 si32->si_fd = si->si_fd; 313 break; 314 } 315 } 316 317 void 318 netbsd32_si32_to_si(siginfo_t *si, const siginfo32_t *si32) 319 { 320 memset(si, 0, sizeof (*si)); 321 si->si_signo = si32->si_signo; 322 si->si_code = si32->si_code; 323 si->si_errno = si32->si_errno; 324 325 switch (si->si_signo) { 326 case 0: /* SA */ 327 si->si_value.sival_int = si32->si_value.sival_int; 328 break; 329 case SIGILL: 330 case SIGBUS: 331 case SIGSEGV: 332 case SIGFPE: 333 case SIGTRAP: 334 si->si_addr = (void *)(uintptr_t)si32->si_addr; 335 si->si_trap = si32->si_trap; 336 break; 337 case SIGALRM: 338 case SIGVTALRM: 339 case SIGPROF: 340 default: 341 si->si_pid = si32->si_pid; 342 si->si_uid = si32->si_uid; 343 si->si_value.sival_int = si32->si_value.sival_int; 344 break; 345 case SIGCHLD: 346 si->si_pid = si32->si_pid; 347 si->si_uid = si32->si_uid; 348 si->si_status = si32->si_status; 349 si->si_utime = si32->si_utime; 350 si->si_stime = si32->si_stime; 351 break; 352 case SIGURG: 353 case SIGIO: 354 si->si_band = si32->si_band; 355 si->si_fd = si32->si_fd; 356 break; 357 } 358 } 359 360 void 361 getucontext32(struct lwp *l, ucontext32_t *ucp) 362 { 363 struct proc *p = l->l_proc; 364 365 KASSERT(mutex_owned(p->p_lock)); 366 367 ucp->uc_flags = 0; 368 ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink; 369 ucp->uc_sigmask = l->l_sigmask; 370 ucp->uc_flags |= _UC_SIGMASK; 371 372 /* 373 * The (unsupplied) definition of the `current execution stack' 374 * in the System V Interface Definition appears to allow returning 375 * the main context stack. 376 */ 377 if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) { 378 ucp->uc_stack.ss_sp = USRSTACK32; 379 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize); 380 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */ 381 } else { 382 /* Simply copy alternate signal execution stack. */ 383 ucp->uc_stack.ss_sp = 384 (uint32_t)(intptr_t)l->l_sigstk.ss_sp; 385 ucp->uc_stack.ss_size = l->l_sigstk.ss_size; 386 ucp->uc_stack.ss_flags = l->l_sigstk.ss_flags; 387 } 388 ucp->uc_flags |= _UC_STACK; 389 mutex_exit(p->p_lock); 390 cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags); 391 mutex_enter(p->p_lock); 392 } 393 394 int 395 netbsd32_getcontext(struct lwp *l, const struct netbsd32_getcontext_args *uap, register_t *retval) 396 { 397 /* { 398 syscallarg(netbsd32_ucontextp) ucp; 399 } */ 400 struct proc *p = l->l_proc; 401 ucontext32_t uc; 402 403 memset(&uc, 0, sizeof(uc)); 404 405 mutex_enter(p->p_lock); 406 getucontext32(l, &uc); 407 mutex_exit(p->p_lock); 408 409 return copyout(&uc, SCARG_P32(uap, ucp), sizeof (ucontext32_t)); 410 } 411 412 int 413 setucontext32(struct lwp *l, const ucontext32_t *ucp) 414 { 415 struct proc *p = l->l_proc; 416 int error; 417 418 KASSERT(mutex_owned(p->p_lock)); 419 420 if ((ucp->uc_flags & _UC_SIGMASK) != 0) { 421 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL); 422 if (error != 0) 423 return error; 424 } 425 426 mutex_exit(p->p_lock); 427 error = cpu_setmcontext32(l, &ucp->uc_mcontext, ucp->uc_flags); 428 mutex_enter(p->p_lock); 429 if (error != 0) 430 return (error); 431 432 l->l_ctxlink = (void *)(intptr_t)ucp->uc_link; 433 434 /* 435 * If there was stack information, update whether or not we are 436 * still running on an alternate signal stack. 437 */ 438 if ((ucp->uc_flags & _UC_STACK) != 0) { 439 if (ucp->uc_stack.ss_flags & SS_ONSTACK) 440 l->l_sigstk.ss_flags |= SS_ONSTACK; 441 else 442 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 443 } 444 445 return 0; 446 } 447 448 /* ARGSUSED */ 449 int 450 netbsd32_setcontext(struct lwp *l, const struct netbsd32_setcontext_args *uap, register_t *retval) 451 { 452 /* { 453 syscallarg(netbsd32_ucontextp) ucp; 454 } */ 455 ucontext32_t uc; 456 int error; 457 struct proc *p = l->l_proc; 458 459 error = copyin(SCARG_P32(uap, ucp), &uc, sizeof (uc)); 460 if (error) 461 return (error); 462 if (!(uc.uc_flags & _UC_CPU)) 463 return (EINVAL); 464 mutex_enter(p->p_lock); 465 error = setucontext32(l, &uc); 466 mutex_exit(p->p_lock); 467 if (error) 468 return (error); 469 470 return (EJUSTRETURN); 471 } 472 473 static int 474 netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size) 475 { 476 const siginfo_t *info = src; 477 siginfo32_t info32; 478 479 netbsd32_si_to_si32(&info32, info); 480 481 return copyout(&info32, dst, sizeof(info32)); 482 } 483 484 static int 485 netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size) 486 { 487 struct timespec *ts = dst; 488 struct netbsd32_timespec ts32; 489 int error; 490 491 error = copyin(src, &ts32, sizeof(ts32)); 492 if (error) 493 return error; 494 495 netbsd32_to_timespec(&ts32, ts); 496 return 0; 497 } 498 499 static int 500 netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size) 501 { 502 const struct timespec *ts = src; 503 struct netbsd32_timespec ts32; 504 505 netbsd32_from_timespec(ts, &ts32); 506 507 return copyout(&ts32, dst, sizeof(ts32)); 508 } 509 510 int 511 netbsd32_____sigtimedwait50(struct lwp *l, const struct netbsd32_____sigtimedwait50_args *uap, register_t *retval) 512 { 513 /* { 514 syscallarg(netbsd32_sigsetp_t) set; 515 syscallarg(netbsd32_siginfop_t) info; 516 syscallarg(netbsd32_timespec50p_t) timeout; 517 } */ 518 struct sys_____sigtimedwait50_args ua; 519 520 NETBSD32TOP_UAP(set, const sigset_t); 521 NETBSD32TOP_UAP(info, siginfo_t); 522 NETBSD32TOP_UAP(timeout, struct timespec); 523 524 return sigtimedwait1(l, &ua, retval, 525 copyin, 526 netbsd32_sigtimedwait_put_info, 527 netbsd32_sigtimedwait_fetch_timeout, 528 netbsd32_sigtimedwait_put_timeout); 529 } 530 531 int 532 netbsd32_sigqueueinfo(struct lwp *l, 533 const struct netbsd32_sigqueueinfo_args *uap, register_t *retval) 534 { 535 /* { 536 syscallarg(pid_t) pid; 537 syscallarg(const netbsd32_siginfop_t) info; 538 } */ 539 struct __ksiginfo32 ksi32; 540 ksiginfo_t ksi; 541 int error; 542 543 if ((error = copyin(SCARG_P32(uap, info), &ksi32, 544 sizeof(ksi32))) != 0) 545 return error; 546 547 KSI_INIT(&ksi); 548 netbsd32_ksi32_to_ksi(&ksi.ksi_info, &ksi32); 549 550 return kill1(l, SCARG(uap, pid), &ksi, retval); 551 } 552 553 struct netbsd32_ktr_psig { 554 int signo; 555 netbsd32_pointer_t action; 556 sigset_t mask; 557 int code; 558 /* and optional siginfo_t */ 559 }; 560 561 #ifdef notyet 562 #ifdef KTRACE 563 void 564 netbsd32_ktrpsig(int sig, sig_t action, const sigset_t *mask, 565 const ksiginfo_t *ksi) 566 { 567 struct ktrace_entry *kte; 568 lwp_t *l = curlwp; 569 struct { 570 struct netbsd32_ktr_psig kp; 571 siginfo32_t si; 572 } *kbuf; 573 574 if (!KTRPOINT(l->l_proc, KTR_PSIG)) 575 return; 576 577 if (ktealloc(&kte, (void *)&kbuf, l, KTR_PSIG, sizeof(*kbuf))) 578 return; 579 580 kbuf->kp.signo = (char)sig; 581 NETBSD32PTR32(kbuf->kp.action, action); 582 kbuf->kp.mask = *mask; 583 584 if (ksi) { 585 kbuf->kp.code = KSI_TRAPCODE(ksi); 586 (void)memset(&kbuf->si, 0, sizeof(kbuf->si)); 587 netbsd32_ksi_to_ksi32(&kbuf->si._info, &ksi->ksi_info); 588 ktesethdrlen(kte, sizeof(*kbuf)); 589 } else { 590 kbuf->kp.code = 0; 591 ktesethdrlen(kte, sizeof(struct netbsd32_ktr_psig)); 592 } 593 594 ktraddentry(l, kte, KTA_WAITOK); 595 } 596 #endif 597 #endif 598