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 * Copyright 2005 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 uact = *sap; 104 } else { 105 mutex_t *mp = &udp->siguaction[sig].sig_lock; 106 lmutex_lock(mp); 107 uact = *sap; 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 siginfo = self->ul_siginfo; 231 sip = &siginfo; 232 } 233 uc.uc_sigmask = self->ul_sigmask; 234 call_user_handler(sig, sip, &uc); 235 } 236 237 void 238 sigacthandler(int sig, siginfo_t *sip, void *uvp) 239 { 240 ucontext_t *ucp = uvp; 241 ulwp_t *self = curthread; 242 243 /* 244 * Do this in case we took a signal while in a cancelable system call. 245 * It does no harm if we were not in such a system call. 246 */ 247 self->ul_sp = 0; 248 if (sig != SIGCANCEL) 249 self->ul_cancel_async = self->ul_save_async; 250 251 /* 252 * If we are not in a critical region and are 253 * not deferring signals, take the signal now. 254 */ 255 if ((self->ul_critical + self->ul_sigdefer) == 0) { 256 call_user_handler(sig, sip, ucp); 257 return; /* call_user_handler() cannot return */ 258 } 259 260 /* 261 * We are in a critical region or we are deferring signals. When 262 * we emerge from the region we will call take_deferred_signal(). 263 */ 264 ASSERT(self->ul_cursig == 0); 265 self->ul_cursig = (char)sig; 266 if (sip != NULL) 267 self->ul_siginfo = *sip; 268 else 269 self->ul_siginfo.si_signo = 0; 270 271 /* 272 * Make sure that if we return to a call to __lwp_park() 273 * or ___lwp_cond_wait() that it returns right away 274 * (giving us a spurious wakeup but not a deadlock). 275 */ 276 set_parking_flag(self, 0); 277 278 /* 279 * Return to the previous context with all signals blocked. 280 * We will restore the signal mask in take_deferred_signal(). 281 * Note that we are calling the system call trap here, not 282 * the _setcontext() wrapper. We don't want to change the 283 * thread's ul_sigmask by this operation. 284 */ 285 ucp->uc_sigmask = maskset; 286 (void) __setcontext_syscall(ucp); 287 thr_panic("sigacthandler(): __setcontext() returned"); 288 } 289 290 #pragma weak sigaction = _sigaction 291 int 292 _sigaction(int sig, const struct sigaction *nact, struct sigaction *oact) 293 { 294 ulwp_t *self = curthread; 295 uberdata_t *udp = self->ul_uberdata; 296 struct sigaction oaction; 297 struct sigaction tact; 298 struct sigaction *tactp = NULL; 299 int rv; 300 301 if (sig <= 0 || sig >= NSIG) { 302 errno = EINVAL; 303 return (-1); 304 } 305 306 if (!self->ul_vfork) 307 lmutex_lock(&udp->siguaction[sig].sig_lock); 308 309 oaction = udp->siguaction[sig].sig_uaction; 310 311 if (nact != NULL) { 312 tact = *nact; /* make a copy so we can modify it */ 313 tactp = &tact; 314 delete_reserved_signals(&tact.sa_mask); 315 316 #if !defined(_LP64) 317 tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */ 318 #endif 319 /* 320 * To be compatible with the behavior of SunOS 4.x: 321 * If the new signal handler is SIG_IGN or SIG_DFL, do 322 * not change the signal's entry in the siguaction array. 323 * This allows a child of vfork(2) to set signal handlers 324 * to SIG_IGN or SIG_DFL without affecting the parent. 325 * 326 * This also covers a race condition with some thread 327 * setting the signal action to SIG_DFL or SIG_IGN 328 * when the thread has also received and deferred 329 * that signal. When the thread takes the deferred 330 * signal, even though it has set the action to SIG_DFL 331 * or SIG_IGN, it will execute the old signal handler 332 * anyway. This is an inherent signaling race condition 333 * and is not a bug. 334 * 335 * A child of vfork() is not allowed to change signal 336 * handlers to anything other than SIG_DFL or SIG_IGN. 337 */ 338 if (self->ul_vfork) { 339 if (tact.sa_sigaction != SIG_IGN) 340 tact.sa_sigaction = SIG_DFL; 341 } else if (sig == SIGCANCEL) { 342 /* 343 * Always catch SIGCANCEL. 344 * We need it for pthread_cancel() to work. 345 */ 346 udp->siguaction[sig].sig_uaction = tact; 347 if (tact.sa_sigaction == SIG_DFL || 348 tact.sa_sigaction == SIG_IGN) 349 tact.sa_flags = SA_SIGINFO; 350 else { 351 tact.sa_flags |= SA_SIGINFO; 352 tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND); 353 } 354 tact.sa_sigaction = udp->sigacthandler; 355 tact.sa_mask = maskset; 356 } else if (tact.sa_sigaction != SIG_DFL && 357 tact.sa_sigaction != SIG_IGN) { 358 udp->siguaction[sig].sig_uaction = tact; 359 tact.sa_flags &= ~SA_NODEFER; 360 tact.sa_sigaction = udp->sigacthandler; 361 tact.sa_mask = maskset; 362 } 363 } 364 365 if ((rv = __sigaction(sig, tactp, oact)) != 0) 366 udp->siguaction[sig].sig_uaction = oaction; 367 else if (oact != NULL && 368 oact->sa_sigaction != SIG_DFL && 369 oact->sa_sigaction != SIG_IGN) 370 *oact = oaction; 371 372 if (!self->ul_vfork) 373 lmutex_unlock(&udp->siguaction[sig].sig_lock); 374 return (rv); 375 } 376 377 /* 378 * Calling set_parking_flag(curthread, 1) informs the kernel that we are 379 * calling __lwp_park or ___lwp_cond_wait(). If we take a signal in 380 * the unprotected (from signals) interval before reaching the kernel, 381 * sigacthandler() will call set_parking_flag(curthread, 0) to inform 382 * the kernel to return immediately from these system calls, giving us 383 * a spurious wakeup but not a deadlock. 384 */ 385 void 386 set_parking_flag(ulwp_t *self, int park) 387 { 388 volatile sc_shared_t *scp; 389 390 enter_critical(self); 391 if ((scp = self->ul_schedctl) != NULL || 392 (scp = setup_schedctl()) != NULL) 393 scp->sc_park = park; 394 else if (park == 0) /* schedctl failed, do it the long way */ 395 __lwp_unpark(self->ul_lwpid); 396 exit_critical(self); 397 } 398 399 /* 400 * Tell the kernel to block all signals. 401 * Use the schedctl interface, or failing that, use __lwp_sigmask(). 402 * This action can be rescinded only by making a system call that 403 * sets the signal mask: 404 * __lwp_sigmask(), __sigprocmask(), __setcontext(), 405 * __sigsuspend() or __pollsys(). 406 * In particular, this action cannot be reversed by assigning 407 * scp->sc_sigblock = 0. That would be a way to lose signals. 408 * See the definition of restore_signals(self). 409 */ 410 void 411 block_all_signals(ulwp_t *self) 412 { 413 volatile sc_shared_t *scp; 414 415 enter_critical(self); 416 if ((scp = self->ul_schedctl) != NULL || 417 (scp = setup_schedctl()) != NULL) 418 scp->sc_sigblock = 1; 419 else 420 (void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL); 421 exit_critical(self); 422 } 423 424 #pragma weak setcontext = _private_setcontext 425 #pragma weak _setcontext = _private_setcontext 426 int 427 _private_setcontext(const ucontext_t *ucp) 428 { 429 ulwp_t *self = curthread; 430 int ret; 431 ucontext_t uc; 432 433 /* 434 * Returning from the main context (uc_link == NULL) causes 435 * the thread to exit. See setcontext(2) and makecontext(3C). 436 */ 437 if (ucp == NULL) 438 _thr_exit(NULL); 439 uc = *ucp; 440 441 /* 442 * Restore previous signal mask and context link. 443 */ 444 if (uc.uc_flags & UC_SIGMASK) { 445 block_all_signals(self); 446 delete_reserved_signals(&uc.uc_sigmask); 447 self->ul_sigmask = uc.uc_sigmask; 448 if (self->ul_cursig) { 449 /* 450 * We have a deferred signal present. 451 * The signal mask will be set when the 452 * signal is taken in take_deferred_signal(). 453 */ 454 ASSERT(self->ul_critical + self->ul_sigdefer != 0); 455 uc.uc_flags &= ~UC_SIGMASK; 456 } 457 } 458 self->ul_siglink = uc.uc_link; 459 460 /* 461 * We don't know where this context structure has been. 462 * Preserve the curthread pointer, at least. 463 */ 464 #if defined(__sparc) 465 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self; 466 #elif defined(__amd64) 467 uc.uc_mcontext.gregs[REG_FS] = (greg_t)self->ul_gs; 468 #elif defined(__i386) 469 uc.uc_mcontext.gregs[GS] = (greg_t)self->ul_gs; 470 #else 471 #error "none of __sparc, __amd64, __i386 defined" 472 #endif 473 /* 474 * Make sure that if we return to a call to __lwp_park() 475 * or ___lwp_cond_wait() that it returns right away 476 * (giving us a spurious wakeup but not a deadlock). 477 */ 478 set_parking_flag(self, 0); 479 self->ul_sp = 0; 480 ret = __setcontext_syscall(&uc); 481 482 /* 483 * It is OK for setcontext() to return if the user has not specified 484 * UC_CPU. 485 */ 486 if (uc.uc_flags & UC_CPU) 487 thr_panic("setcontext(): __setcontext() returned"); 488 return (ret); 489 } 490 491 #pragma weak thr_sigsetmask = _thr_sigsetmask 492 #pragma weak pthread_sigmask = _thr_sigsetmask 493 #pragma weak _pthread_sigmask = _thr_sigsetmask 494 int 495 _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset) 496 { 497 ulwp_t *self = curthread; 498 sigset_t saveset; 499 500 if (set == NULL) { 501 enter_critical(self); 502 if (oset != NULL) 503 *oset = self->ul_sigmask; 504 exit_critical(self); 505 } else { 506 switch (how) { 507 case SIG_BLOCK: 508 case SIG_UNBLOCK: 509 case SIG_SETMASK: 510 break; 511 default: 512 return (EINVAL); 513 } 514 515 /* 516 * The assignments to self->ul_sigmask must be protected from 517 * signals. The nuances of this code are subtle. Be careful. 518 */ 519 block_all_signals(self); 520 if (oset != NULL) 521 saveset = self->ul_sigmask; 522 switch (how) { 523 case SIG_BLOCK: 524 self->ul_sigmask.__sigbits[0] |= set->__sigbits[0]; 525 self->ul_sigmask.__sigbits[1] |= set->__sigbits[1]; 526 break; 527 case SIG_UNBLOCK: 528 self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0]; 529 self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1]; 530 break; 531 case SIG_SETMASK: 532 self->ul_sigmask.__sigbits[0] = set->__sigbits[0]; 533 self->ul_sigmask.__sigbits[1] = set->__sigbits[1]; 534 break; 535 } 536 delete_reserved_signals(&self->ul_sigmask); 537 if (oset != NULL) 538 *oset = saveset; 539 restore_signals(self); 540 } 541 542 return (0); 543 } 544 545 #pragma weak sigprocmask = _sigprocmask 546 int 547 _sigprocmask(int how, const sigset_t *set, sigset_t *oset) 548 { 549 int error; 550 551 /* 552 * Guard against children of vfork(). 553 */ 554 if (curthread->ul_vfork) 555 return (__lwp_sigmask(how, set, oset)); 556 557 if ((error = _thr_sigsetmask(how, set, oset)) != 0) { 558 errno = error; 559 return (-1); 560 } 561 562 return (0); 563 } 564 565 /* 566 * Called at library initialization to set up signal handling. 567 * All we really do is initialize the sig_lock mutexes. 568 * All signal handlers are either SIG_DFL or SIG_IGN on exec(). 569 * However, if any signal handlers were established on alternate 570 * link maps before the primary link map has been initialized, 571 * then inform the kernel of the new sigacthandler. 572 */ 573 void 574 signal_init() 575 { 576 uberdata_t *udp = curthread->ul_uberdata; 577 struct sigaction *sap; 578 struct sigaction act; 579 int sig; 580 581 for (sig = 0; sig < NSIG; sig++) { 582 udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC; 583 sap = &udp->siguaction[sig].sig_uaction; 584 if (sap->sa_sigaction != SIG_DFL && 585 sap->sa_sigaction != SIG_IGN && 586 __sigaction(sig, NULL, &act) == 0 && 587 act.sa_sigaction != SIG_DFL && 588 act.sa_sigaction != SIG_IGN) { 589 act = *sap; 590 act.sa_flags &= ~SA_NODEFER; 591 act.sa_sigaction = udp->sigacthandler; 592 act.sa_mask = maskset; 593 (void) __sigaction(sig, &act, NULL); 594 } 595 } 596 } 597 598 /* 599 * Common code for cancelling self in _sigcancel() and pthread_cancel(). 600 * If the thread is at a cancellation point (ul_cancelable) then just 601 * return and let _canceloff() do the exit, else exit immediately if 602 * async mode is in effect. 603 */ 604 void 605 do_sigcancel() 606 { 607 ulwp_t *self = curthread; 608 609 ASSERT(self->ul_critical == 0); 610 ASSERT(self->ul_sigdefer == 0); 611 self->ul_cancel_pending = 1; 612 if (self->ul_cancel_async && 613 !self->ul_cancel_disabled && 614 !self->ul_cancelable) 615 _pthread_exit(PTHREAD_CANCELED); 616 } 617 618 /* 619 * Set up the SIGCANCEL handler for threads cancellation 620 * (needed only when we have more than one thread). 621 * We need no locks here because we are called from 622 * finish_init() while still single-threaded. 623 */ 624 void 625 init_sigcancel() 626 { 627 uberdata_t *udp = curthread->ul_uberdata; 628 struct sigaction act; 629 630 act = udp->siguaction[SIGCANCEL].sig_uaction; 631 if (act.sa_sigaction == SIG_DFL || 632 act.sa_sigaction == SIG_IGN) 633 act.sa_flags = SA_SIGINFO; 634 else { 635 act.sa_flags |= SA_SIGINFO; 636 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND); 637 } 638 act.sa_sigaction = udp->sigacthandler; 639 act.sa_mask = maskset; 640 (void) __sigaction(SIGCANCEL, &act, NULL); 641 } 642