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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include "lint.h" 31 #include "thr_uberdata.h" 32 #include <signal.h> 33 #include <siginfo.h> 34 #include <ucontext.h> 35 #include <sys/systm.h> 36 37 const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0}; /* maskable signals */ 38 39 /* 40 * Return true if the valid signal bits in both sets are the same. 41 */ 42 int 43 sigequalset(const sigset_t *s1, const sigset_t *s2) 44 { 45 /* 46 * We only test valid signal bits, not rubbish following MAXSIG 47 * (for speed). Algorithm: 48 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0 49 */ 50 return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) | 51 ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1))); 52 } 53 54 /* 55 * Common code for calling the user-specified signal handler. 56 */ 57 void 58 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp) 59 { 60 ulwp_t *self = curthread; 61 uberdata_t *udp = self->ul_uberdata; 62 struct sigaction uact; 63 volatile struct sigaction *sap; 64 65 /* 66 * If we are taking a signal while parked or about to be parked 67 * on __lwp_park() then remove ourself from the sleep queue so 68 * that we can grab locks. The code in mutex_lock_queue() and 69 * cond_wait_common() will detect this and deal with it when 70 * __lwp_park() returns. 71 */ 72 unsleep_self(); 73 set_parking_flag(self, 0); 74 75 if (__td_event_report(self, TD_CATCHSIG, udp)) { 76 self->ul_td_evbuf.eventnum = TD_CATCHSIG; 77 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig; 78 tdb_event(TD_CATCHSIG, udp); 79 } 80 81 /* 82 * Get a self-consistent set of flags, handler, and mask 83 * while holding the sig's sig_lock for the least possible time. 84 * We must acquire the sig's sig_lock because some thread running 85 * in sigaction() might be establishing a new signal handler. 86 * 87 * Locking exceptions: 88 * No locking for a child of vfork(). 89 * If the signal is SIGPROF with an si_code of PROF_SIG, 90 * then we assume that this signal was generated by 91 * setitimer(ITIMER_REALPROF) set up by the dbx collector. 92 * If the signal is SIGEMT with an si_code of EMT_CPCOVF, 93 * then we assume that the signal was generated by 94 * a hardware performance counter overflow. 95 * In these cases, assume that we need no locking. It is the 96 * monitoring program's responsibility to ensure correctness. 97 */ 98 sap = &udp->siguaction[sig].sig_uaction; 99 if (self->ul_vfork || 100 (sip != NULL && 101 ((sig == SIGPROF && sip->si_code == PROF_SIG) || 102 (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) { 103 /* we wish this assignment could be atomic */ 104 (void) _private_memcpy(&uact, (void *)sap, sizeof (uact)); 105 } else { 106 mutex_t *mp = &udp->siguaction[sig].sig_lock; 107 lmutex_lock(mp); 108 (void) _private_memcpy(&uact, (void *)sap, sizeof (uact)); 109 if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND)) 110 sap->sa_sigaction = SIG_DFL; 111 lmutex_unlock(mp); 112 } 113 114 /* 115 * Set the proper signal mask and call the user's signal handler. 116 * (We overrode the user-requested signal mask with maskset 117 * so we currently have all blockable signals blocked.) 118 * 119 * We would like to ASSERT() that the signal is not a member of the 120 * signal mask at the previous level (ucp->uc_sigmask) or the specified 121 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but 122 * /proc can override this via PCSSIG, so we don't bother. 123 * 124 * We would also like to ASSERT() that the signal mask at the previous 125 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()), 126 * but /proc can change the thread's signal mask via PCSHOLD, so we 127 * don't bother with that either. 128 */ 129 ASSERT(ucp->uc_flags & UC_SIGMASK); 130 if (self->ul_sigsuspend) { 131 ucp->uc_sigmask = self->ul_sigmask; 132 self->ul_sigsuspend = 0; 133 /* the sigsuspend() or pollsys() signal mask */ 134 sigorset(&uact.sa_mask, &self->ul_tmpmask); 135 } else { 136 /* the signal mask at the previous level */ 137 sigorset(&uact.sa_mask, &ucp->uc_sigmask); 138 } 139 if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */ 140 (void) _private_sigaddset(&uact.sa_mask, sig); 141 self->ul_sigmask = uact.sa_mask; 142 self->ul_siglink = ucp; 143 (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL); 144 145 /* 146 * If this thread has been sent SIGCANCEL from the kernel 147 * or from pthread_cancel(), it is being asked to exit. 148 * The kernel may send SIGCANCEL without a siginfo struct. 149 * If the SIGCANCEL is process-directed (from kill() or 150 * sigqueue()), treat it as an ordinary signal. 151 */ 152 if (sig == SIGCANCEL) { 153 if (sip == NULL || SI_FROMKERNEL(sip) || 154 sip->si_code == SI_LWP) { 155 do_sigcancel(); 156 goto out; 157 } 158 if (uact.sa_sigaction == SIG_DFL || 159 uact.sa_sigaction == SIG_IGN) 160 goto out; 161 } 162 163 if (!(uact.sa_flags & SA_SIGINFO)) 164 sip = NULL; 165 __sighndlr(sig, sip, ucp, uact.sa_sigaction); 166 167 #if defined(sparc) || defined(__sparc) 168 /* 169 * If this is a floating point exception and the queue 170 * is non-empty, pop the top entry from the queue. This 171 * is to maintain expected behavior. 172 */ 173 if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) { 174 fpregset_t *fp = &ucp->uc_mcontext.fpregs; 175 176 if (--fp->fpu_qcnt > 0) { 177 unsigned char i; 178 struct fq *fqp; 179 180 fqp = fp->fpu_q; 181 for (i = 0; i < fp->fpu_qcnt; i++) 182 fqp[i] = fqp[i+1]; 183 } 184 } 185 #endif /* sparc */ 186 187 out: 188 (void) _private_setcontext(ucp); 189 thr_panic("call_user_handler(): _setcontext() returned"); 190 } 191 192 /* 193 * take_deferred_signal() is called when ul_critical and ul_sigdefer become 194 * zero and a deferred signal has been recorded on the current thread. 195 * We are out of the critical region and are ready to take a signal. 196 * The kernel has all signals blocked on this lwp, but our value of 197 * ul_sigmask is the correct signal mask for the previous context. 198 */ 199 void 200 take_deferred_signal(int sig) 201 { 202 ulwp_t *self = curthread; 203 siginfo_t siginfo; 204 siginfo_t *sip; 205 ucontext_t uc; 206 volatile int returning; 207 208 ASSERT(self->ul_critical == 0); 209 ASSERT(self->ul_sigdefer == 0); 210 ASSERT(self->ul_cursig == 0); 211 212 returning = 0; 213 uc.uc_flags = UC_ALL; 214 /* 215 * We call _private_getcontext (a libc-private synonym for 216 * _getcontext) rather than _getcontext because we need to 217 * avoid the dynamic linker and link auditing problems here. 218 */ 219 (void) _private_getcontext(&uc); 220 /* 221 * If the application signal handler calls setcontext() on 222 * the ucontext we give it, it returns here, then we return. 223 */ 224 if (returning) 225 return; 226 returning = 1; 227 ASSERT(sigequalset(&uc.uc_sigmask, &maskset)); 228 if (self->ul_siginfo.si_signo == 0) 229 sip = NULL; 230 else { 231 (void) _private_memcpy(&siginfo, 232 &self->ul_siginfo, sizeof (siginfo)); 233 sip = &siginfo; 234 } 235 uc.uc_sigmask = self->ul_sigmask; 236 call_user_handler(sig, sip, &uc); 237 } 238 239 void 240 sigacthandler(int sig, siginfo_t *sip, void *uvp) 241 { 242 ucontext_t *ucp = uvp; 243 ulwp_t *self = curthread; 244 245 /* 246 * Do this in case we took a signal while in a cancelable system call. 247 * It does no harm if we were not in such a system call. 248 */ 249 self->ul_sp = 0; 250 if (sig != SIGCANCEL) 251 self->ul_cancel_async = self->ul_save_async; 252 253 /* 254 * If we are not in a critical region and are 255 * not deferring signals, take the signal now. 256 */ 257 if ((self->ul_critical + self->ul_sigdefer) == 0) { 258 call_user_handler(sig, sip, ucp); 259 return; /* call_user_handler() cannot return */ 260 } 261 262 /* 263 * We are in a critical region or we are deferring signals. When 264 * we emerge from the region we will call take_deferred_signal(). 265 */ 266 ASSERT(self->ul_cursig == 0); 267 self->ul_cursig = (char)sig; 268 if (sip != NULL) 269 (void) _private_memcpy(&self->ul_siginfo, 270 sip, sizeof (siginfo_t)); 271 else 272 self->ul_siginfo.si_signo = 0; 273 274 /* 275 * Make sure that if we return to a call to __lwp_park() 276 * or ___lwp_cond_wait() that it returns right away 277 * (giving us a spurious wakeup but not a deadlock). 278 */ 279 set_parking_flag(self, 0); 280 281 /* 282 * Return to the previous context with all signals blocked. 283 * We will restore the signal mask in take_deferred_signal(). 284 * Note that we are calling the system call trap here, not 285 * the _setcontext() wrapper. We don't want to change the 286 * thread's ul_sigmask by this operation. 287 */ 288 ucp->uc_sigmask = maskset; 289 (void) __setcontext_syscall(ucp); 290 thr_panic("sigacthandler(): __setcontext() returned"); 291 } 292 293 #pragma weak sigaction = _sigaction 294 int 295 _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