1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include "lint.h" 30 #include "thr_uberdata.h" 31 #include <signal.h> 32 #include <siginfo.h> 33 #include <ucontext.h> 34 #include <sys/systm.h> 35 36 const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0}; /* maskable signals */ 37 38 /* 39 * Return true if the valid signal bits in both sets are the same. 40 */ 41 int 42 sigequalset(const sigset_t *s1, const sigset_t *s2) 43 { 44 /* 45 * We only test valid signal bits, not rubbish following MAXSIG 46 * (for speed). Algorithm: 47 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0 48 */ 49 return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) | 50 ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1))); 51 } 52 53 /* 54 * Common code for calling the user-specified signal handler. 55 */ 56 void 57 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp) 58 { 59 ulwp_t *self = curthread; 60 uberdata_t *udp = self->ul_uberdata; 61 struct sigaction uact; 62 volatile struct sigaction *sap; 63 64 /* 65 * If we are taking a signal while parked or about to be parked 66 * on __lwp_park() then remove ourself from the sleep queue so 67 * that we can grab locks. The code in mutex_lock_queue() and 68 * cond_wait_common() will detect this and deal with it when 69 * __lwp_park() returns. 70 */ 71 unsleep_self(); 72 set_parking_flag(self, 0); 73 74 if (__td_event_report(self, TD_CATCHSIG, udp)) { 75 self->ul_td_evbuf.eventnum = TD_CATCHSIG; 76 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig; 77 tdb_event(TD_CATCHSIG, udp); 78 } 79 80 /* 81 * Get a self-consistent set of flags, handler, and mask 82 * while holding the sig's sig_lock for the least possible time. 83 * We must acquire the sig's sig_lock because some thread running 84 * in sigaction() might be establishing a new signal handler. 85 * 86 * Locking exceptions: 87 * No locking for a child of vfork(). 88 * If the signal is SIGPROF with an si_code of PROF_SIG, 89 * then we assume that this signal was generated by 90 * setitimer(ITIMER_REALPROF) set up by the dbx collector. 91 * If the signal is SIGEMT with an si_code of EMT_CPCOVF, 92 * then we assume that the signal was generated by 93 * a hardware performance counter overflow. 94 * In these cases, assume that we need no locking. It is the 95 * monitoring program's responsibility to ensure correctness. 96 */ 97 sap = &udp->siguaction[sig].sig_uaction; 98 if (self->ul_vfork || 99 (sip != NULL && 100 ((sig == SIGPROF && sip->si_code == PROF_SIG) || 101 (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) { 102 /* we wish this assignment could be atomic */ 103 (void) _private_memcpy(&uact, (void *)sap, sizeof (uact)); 104 } else { 105 mutex_t *mp = &udp->siguaction[sig].sig_lock; 106 lmutex_lock(mp); 107 (void) _private_memcpy(&uact, (void *)sap, sizeof (uact)); 108 if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND)) 109 sap->sa_sigaction = SIG_DFL; 110 lmutex_unlock(mp); 111 } 112 113 /* 114 * Set the proper signal mask and call the user's signal handler. 115 * (We overrode the user-requested signal mask with maskset 116 * so we currently have all blockable signals blocked.) 117 * 118 * We would like to ASSERT() that the signal is not a member of the 119 * signal mask at the previous level (ucp->uc_sigmask) or the specified 120 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but 121 * /proc can override this via PCSSIG, so we don't bother. 122 * 123 * We would also like to ASSERT() that the signal mask at the previous 124 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()), 125 * but /proc can change the thread's signal mask via PCSHOLD, so we 126 * don't bother with that either. 127 */ 128 ASSERT(ucp->uc_flags & UC_SIGMASK); 129 if (self->ul_sigsuspend) { 130 ucp->uc_sigmask = self->ul_sigmask; 131 self->ul_sigsuspend = 0; 132 /* the sigsuspend() or pollsys() signal mask */ 133 sigorset(&uact.sa_mask, &self->ul_tmpmask); 134 } else { 135 /* the signal mask at the previous level */ 136 sigorset(&uact.sa_mask, &ucp->uc_sigmask); 137 } 138 if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */ 139 (void) _private_sigaddset(&uact.sa_mask, sig); 140 self->ul_sigmask = uact.sa_mask; 141 self->ul_siglink = ucp; 142 (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL); 143 144 /* 145 * If this thread has been sent SIGCANCEL from the kernel 146 * or from pthread_cancel(), it is being asked to exit. 147 * The kernel may send SIGCANCEL without a siginfo struct. 148 * If the SIGCANCEL is process-directed (from kill() or 149 * sigqueue()), treat it as an ordinary signal. 150 */ 151 if (sig == SIGCANCEL) { 152 if (sip == NULL || SI_FROMKERNEL(sip) || 153 sip->si_code == SI_LWP) { 154 do_sigcancel(); 155 goto out; 156 } 157 if (uact.sa_sigaction == SIG_DFL || 158 uact.sa_sigaction == SIG_IGN) 159 goto out; 160 } 161 162 if (!(uact.sa_flags & SA_SIGINFO)) 163 sip = NULL; 164 __sighndlr(sig, sip, ucp, uact.sa_sigaction); 165 166 #if defined(sparc) || defined(__sparc) 167 /* 168 * If this is a floating point exception and the queue 169 * is non-empty, pop the top entry from the queue. This 170 * is to maintain expected behavior. 171 */ 172 if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) { 173 fpregset_t *fp = &ucp->uc_mcontext.fpregs; 174 175 if (--fp->fpu_qcnt > 0) { 176 unsigned char i; 177 struct fq *fqp; 178 179 fqp = fp->fpu_q; 180 for (i = 0; i < fp->fpu_qcnt; i++) 181 fqp[i] = fqp[i+1]; 182 } 183 } 184 #endif /* sparc */ 185 186 out: 187 (void) _private_setcontext(ucp); 188 thr_panic("call_user_handler(): _setcontext() returned"); 189 } 190 191 /* 192 * take_deferred_signal() is called when ul_critical and ul_sigdefer become 193 * zero and a deferred signal has been recorded on the current thread. 194 * We are out of the critical region and are ready to take a signal. 195 * The kernel has all signals blocked on this lwp, but our value of 196 * ul_sigmask is the correct signal mask for the previous context. 197 */ 198 void 199 take_deferred_signal(int sig) 200 { 201 ulwp_t *self = curthread; 202 siginfo_t siginfo; 203 siginfo_t *sip; 204 ucontext_t uc; 205 volatile int returning; 206 207 ASSERT(self->ul_critical == 0); 208 ASSERT(self->ul_sigdefer == 0); 209 ASSERT(self->ul_cursig == 0); 210 211 returning = 0; 212 uc.uc_flags = UC_ALL; 213 /* 214 * We call _private_getcontext (a libc-private synonym for 215 * _getcontext) rather than _getcontext because we need to 216 * avoid the dynamic linker and link auditing problems here. 217 */ 218 (void) _private_getcontext(&uc); 219 /* 220 * If the application signal handler calls setcontext() on 221 * the ucontext we give it, it returns here, then we return. 222 */ 223 if (returning) 224 return; 225 returning = 1; 226 ASSERT(sigequalset(&uc.uc_sigmask, &maskset)); 227 if (self->ul_siginfo.si_signo == 0) 228 sip = NULL; 229 else { 230 (void) _private_memcpy(&siginfo, 231 &self->ul_siginfo, sizeof (siginfo)); 232 sip = &siginfo; 233 } 234 uc.uc_sigmask = self->ul_sigmask; 235 call_user_handler(sig, sip, &uc); 236 } 237 238 void 239 sigacthandler(int sig, siginfo_t *sip, void *uvp) 240 { 241 ucontext_t *ucp = uvp; 242 ulwp_t *self = curthread; 243 244 /* 245 * Do this in case we took a signal while in a cancelable system call. 246 * It does no harm if we were not in such a system call. 247 */ 248 self->ul_sp = 0; 249 if (sig != SIGCANCEL) 250 self->ul_cancel_async = self->ul_save_async; 251 252 /* 253 * If we are not in a critical region and are 254 * not deferring signals, take the signal now. 255 */ 256 if ((self->ul_critical + self->ul_sigdefer) == 0) { 257 call_user_handler(sig, sip, ucp); 258 return; /* call_user_handler() cannot return */ 259 } 260 261 /* 262 * We are in a critical region or we are deferring signals. When 263 * we emerge from the region we will call take_deferred_signal(). 264 */ 265 ASSERT(self->ul_cursig == 0); 266 self->ul_cursig = (char)sig; 267 if (sip != NULL) 268 (void) _private_memcpy(&self->ul_siginfo, 269 sip, sizeof (siginfo_t)); 270 else 271 self->ul_siginfo.si_signo = 0; 272 273 /* 274 * Make sure that if we return to a call to __lwp_park() 275 * or ___lwp_cond_wait() that it returns right away 276 * (giving us a spurious wakeup but not a deadlock). 277 */ 278 set_parking_flag(self, 0); 279 280 /* 281 * Return to the previous context with all signals blocked. 282 * We will restore the signal mask in take_deferred_signal(). 283 * Note that we are calling the system call trap here, not 284 * the _setcontext() wrapper. We don't want to change the 285 * thread's ul_sigmask by this operation. 286 */ 287 ucp->uc_sigmask = maskset; 288 (void) __setcontext_syscall(ucp); 289 thr_panic("sigacthandler(): __setcontext() returned"); 290 } 291 292 #pragma weak sigaction = _libc_sigaction 293 #pragma weak _sigaction = _libc_sigaction 294 int 295 _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact) 296 { 297 ulwp_t *self = curthread; 298 uberdata_t *udp = self->ul_uberdata; 299 struct sigaction oaction; 300 struct sigaction tact; 301 struct sigaction *tactp = NULL; 302 int rv; 303 304 if (sig <= 0 || sig >= NSIG) { 305 errno = EINVAL; 306 return (-1); 307 } 308 309 if (!self->ul_vfork) 310 lmutex_lock(&udp->siguaction[sig].sig_lock); 311 312 oaction = udp->siguaction[sig].sig_uaction; 313 314 if (nact != NULL) { 315 tact = *nact; /* make a copy so we can modify it */ 316 tactp = &tact; 317 delete_reserved_signals(&tact.sa_mask); 318 319 #if !defined(_LP64) 320 tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */ 321 #endif 322 /* 323 * To be compatible with the behavior of SunOS 4.x: 324 * If the new signal handler is SIG_IGN or SIG_DFL, do 325 * not change the signal's entry in the siguaction array. 326 * This allows a child of vfork(2) to set signal handlers 327 * to SIG_IGN or SIG_DFL without affecting the parent. 328 * 329 * This also covers a race condition with some thread 330 * setting the signal action to SIG_DFL or SIG_IGN 331 * when the thread has also received and deferred 332 * that signal. When the thread takes the deferred 333 * signal, even though it has set the action to SIG_DFL 334 * or SIG_IGN, it will execute the old signal handler 335 * anyway. This is an inherent signaling race condition 336 * and is not a bug. 337 * 338 * A child of vfork() is not allowed to change signal 339 * handlers to anything other than SIG_DFL or SIG_IGN. 340 */ 341 if (self->ul_vfork) { 342 if (tact.sa_sigaction != SIG_IGN) 343 tact.sa_sigaction = SIG_DFL; 344 } else if (sig == SIGCANCEL) { 345 /* 346 * Always catch SIGCANCEL. 347 * We need it for pthread_cancel() to work. 348 */ 349 udp->siguaction[sig].sig_uaction = tact; 350 if (tact.sa_sigaction == SIG_DFL || 351 tact.sa_sigaction == SIG_IGN) 352 tact.sa_flags = SA_SIGINFO; 353 else { 354 tact.sa_flags |= SA_SIGINFO; 355 tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND); 356 } 357 tact.sa_sigaction = udp->sigacthandler; 358 tact.sa_mask = maskset; 359 } else if (tact.sa_sigaction != SIG_DFL && 360 tact.sa_sigaction != SIG_IGN) { 361 udp->siguaction[sig].sig_uaction = tact; 362 tact.sa_flags &= ~SA_NODEFER; 363 tact.sa_sigaction = udp->sigacthandler; 364 tact.sa_mask = maskset; 365 } 366 } 367 368 if ((rv = __sigaction(sig, tactp, oact)) != 0) 369 udp->siguaction[sig].sig_uaction = oaction; 370 else if (oact != NULL && 371 oact->sa_sigaction != SIG_DFL && 372 oact->sa_sigaction != SIG_IGN) 373 *oact = oaction; 374 375 if (!self->ul_vfork) 376 lmutex_unlock(&udp->siguaction[sig].sig_lock); 377 return (rv); 378 } 379 380 /* 381 * Calling set_parking_flag(curthread, 1) informs the kernel that we are 382 * calling __lwp_park or ___lwp_cond_wait(). If we take a signal in 383 * the unprotected (from signals) interval before reaching the kernel, 384 * sigacthandler() will call set_parking_flag(curthread, 0) to inform 385 * the kernel to return immediately from these system calls, giving us 386 * a spurious wakeup but not a deadlock. 387 */ 388 void 389 set_parking_flag(ulwp_t *self, int park) 390 { 391 volatile sc_shared_t *scp; 392 393 enter_critical(self); 394 if ((scp = self->ul_schedctl) != NULL || 395 (scp = setup_schedctl()) != NULL) 396 scp->sc_park = park; 397 else if (park == 0) /* schedctl failed, do it the long way */ 398 __lwp_unpark(self->ul_lwpid); 399 exit_critical(self); 400 } 401 402 /* 403 * Tell the kernel to block all signals. 404 * Use the schedctl interface, or failing that, use __lwp_sigmask(). 405 * This action can be rescinded only by making a system call that 406 * sets the signal mask: 407 * __lwp_sigmask(), __sigprocmask(), __setcontext(), 408 * __sigsuspend() or __pollsys(). 409 * In particular, this action cannot be reversed by assigning 410 * scp->sc_sigblock = 0. That would be a way to lose signals. 411 * See the definition of restore_signals(self). 412 */ 413 void 414 block_all_signals(ulwp_t *self) 415 { 416 volatile sc_shared_t *scp; 417 418 enter_critical(self); 419 if ((scp = self->ul_schedctl) != NULL || 420 (scp = setup_schedctl()) != NULL) 421 scp->sc_sigblock = 1; 422 else 423 (void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL); 424 exit_critical(self); 425 } 426 427 #pragma weak setcontext = _private_setcontext 428 #pragma weak _setcontext = _private_setcontext 429 int 430 _private_setcontext(const ucontext_t *ucp) 431 { 432 ulwp_t *self = curthread; 433 int ret; 434 ucontext_t uc; 435 436 /* 437 * Returning from the main context (uc_link == NULL) causes 438 * the thread to exit. See setcontext(2) and makecontext(3C). 439 */ 440 if (ucp == NULL) 441 _thr_exit(NULL); 442 (void) _private_memcpy(&uc, ucp, sizeof (uc)); 443 444 /* 445 * Restore previous signal mask and context link. 446 */ 447 if (uc.uc_flags & UC_SIGMASK) { 448 block_all_signals(self); 449 delete_reserved_signals(&uc.uc_sigmask); 450 self->ul_sigmask = uc.uc_sigmask; 451 if (self->ul_cursig) { 452 /* 453 * We have a deferred signal present. 454 * The signal mask will be set when the 455 * signal is taken in take_deferred_signal(). 456 */ 457 ASSERT(self->ul_critical + self->ul_sigdefer != 0); 458 uc.uc_flags &= ~UC_SIGMASK; 459 } 460 } 461 self->ul_siglink = uc.uc_link; 462 463 /* 464 * We don't know where this context structure has been. 465 * Preserve the curthread pointer, at least. 466 */ 467 #if defined(__sparc) 468 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self; 469 #elif defined(__amd64) 470 uc.uc_mcontext.gregs[REG_FS] = (greg_t)self->ul_gs; 471 #elif defined(__i386) 472 uc.uc_mcontext.gregs[GS] = (greg_t)self->ul_gs; 473 #else 474 #error "none of __sparc, __amd64, __i386 defined" 475 #endif 476 /* 477 * Make sure that if we return to a call to __lwp_park() 478 * or ___lwp_cond_wait() that it returns right away 479 * (giving us a spurious wakeup but not a deadlock). 480 */ 481 set_parking_flag(self, 0); 482 self->ul_sp = 0; 483 ret = __setcontext_syscall(&uc); 484 485 /* 486 * It is OK for setcontext() to return if the user has not specified 487 * UC_CPU. 488 */ 489 if (uc.uc_flags & UC_CPU) 490 thr_panic("setcontext(): __setcontext() returned"); 491 return (ret); 492 } 493 494 #pragma weak thr_sigsetmask = _thr_sigsetmask 495 #pragma weak pthread_sigmask = _thr_sigsetmask 496 #pragma weak _pthread_sigmask = _thr_sigsetmask 497 int 498 _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset) 499 { 500 ulwp_t *self = curthread; 501 sigset_t saveset; 502 503 if (set == NULL) { 504 enter_critical(self); 505 if (oset != NULL) 506 *oset = self->ul_sigmask; 507 exit_critical(self); 508 } else { 509 switch (how) { 510 case SIG_BLOCK: 511 case SIG_UNBLOCK: 512 case SIG_SETMASK: 513 break; 514 default: 515 return (EINVAL); 516 } 517 518 /* 519 * The assignments to self->ul_sigmask must be protected from 520 * signals. The nuances of this code are subtle. Be careful. 521 */ 522 block_all_signals(self); 523 if (oset != NULL) 524 saveset = self->ul_sigmask; 525 switch (how) { 526 case SIG_BLOCK: 527 self->ul_sigmask.__sigbits[0] |= set->__sigbits[0]; 528 self->ul_sigmask.__sigbits[1] |= set->__sigbits[1]; 529 break; 530 case SIG_UNBLOCK: 531 self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0]; 532 self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1]; 533 break; 534 case SIG_SETMASK: 535 self->ul_sigmask.__sigbits[0] = set->__sigbits[0]; 536 self->ul_sigmask.__sigbits[1] = set->__sigbits[1]; 537 break; 538 } 539 delete_reserved_signals(&self->ul_sigmask); 540 if (oset != NULL) 541 *oset = saveset; 542 restore_signals(self); 543 } 544 545 return (0); 546 } 547 548 #pragma weak sigprocmask = _sigprocmask 549 int 550 _sigprocmask(int how, const sigset_t *set, sigset_t *oset) 551 { 552 int error; 553 554 /* 555 * Guard against children of vfork(). 556 */ 557 if (curthread->ul_vfork) 558 return (__lwp_sigmask(how, set, oset)); 559 560 if ((error = _thr_sigsetmask(how, set, oset)) != 0) { 561 errno = error; 562 return (-1); 563 } 564 565 return (0); 566 } 567 568 /* 569 * Called at library initialization to set up signal handling. 570 * All we really do is initialize the sig_lock mutexes. 571 * All signal handlers are either SIG_DFL or SIG_IGN on exec(). 572 * However, if any signal handlers were established on alternate 573 * link maps before the primary link map has been initialized, 574 * then inform the kernel of the new sigacthandler. 575 */ 576 void 577 signal_init() 578 { 579 uberdata_t *udp = curthread->ul_uberdata; 580 struct sigaction *sap; 581 struct sigaction act; 582 int sig; 583 584 for (sig = 0; sig < NSIG; sig++) { 585 udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC; 586 sap = &udp->siguaction[sig].sig_uaction; 587 if (sap->sa_sigaction != SIG_DFL && 588 sap->sa_sigaction != SIG_IGN && 589 __sigaction(sig, NULL, &act) == 0 && 590 act.sa_sigaction != SIG_DFL && 591 act.sa_sigaction != SIG_IGN) { 592 act = *sap; 593 act.sa_flags &= ~SA_NODEFER; 594 act.sa_sigaction = udp->sigacthandler; 595 act.sa_mask = maskset; 596 (void) __sigaction(sig, &act, NULL); 597 } 598 } 599 } 600 601 /* 602 * Common code for cancelling self in _sigcancel() and pthread_cancel(). 603 * If the thread is at a cancellation point (ul_cancelable) then just 604 * return and let _canceloff() do the exit, else exit immediately if 605 * async mode is in effect. 606 */ 607 void 608 do_sigcancel() 609 { 610 ulwp_t *self = curthread; 611 612 ASSERT(self->ul_critical == 0); 613 ASSERT(self->ul_sigdefer == 0); 614 self->ul_cancel_pending = 1; 615 if (self->ul_cancel_async && 616 !self->ul_cancel_disabled && 617 !self->ul_cancelable) 618 _pthread_exit(PTHREAD_CANCELED); 619 } 620 621 /* 622 * Set up the SIGCANCEL handler for threads cancellation 623 * (needed only when we have more than one thread). 624 * We need no locks here because we are called from 625 * finish_init() while still single-threaded. 626 */ 627 void 628 init_sigcancel() 629 { 630 uberdata_t *udp = curthread->ul_uberdata; 631 struct sigaction act; 632 633 act = udp->siguaction[SIGCANCEL].sig_uaction; 634 if (act.sa_sigaction == SIG_DFL || 635 act.sa_sigaction == SIG_IGN) 636 act.sa_flags = SA_SIGINFO; 637 else { 638 act.sa_flags |= SA_SIGINFO; 639 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND); 640 } 641 act.sa_sigaction = udp->sigacthandler; 642 act.sa_mask = maskset; 643 (void) __sigaction(SIGCANCEL, &act, NULL); 644 } 645