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 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  * Copyright 2015 Joyent, Inc.
  26  */
  27 
  28 #include "lint.h"
  29 #include <sys/feature_tests.h>
  30 /*
  31  * setcontext() really can return, if UC_CPU is not specified.
  32  * Make the compiler shut up about it.
  33  */
  34 #if defined(__NORETURN)
  35 #undef  __NORETURN
  36 #endif
  37 #define __NORETURN
  38 #include "thr_uberdata.h"
  39 #include "asyncio.h"
  40 #include <signal.h>
  41 #include <siginfo.h>
  42 #include <sys/systm.h>
  43 
  44 /* maskable signals */
  45 const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3};
  46 
  47 /*
  48  * Return true if the valid signal bits in both sets are the same.
  49  */
  50 int
  51 sigequalset(const sigset_t *s1, const sigset_t *s2)
  52 {
  53         /*
  54          * We only test valid signal bits, not rubbish following MAXSIG
  55          * (for speed).  Algorithm:
  56          * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
  57          */
  58 /* see lib/libc/inc/thr_uberdata.h for why this must be true */
  59 #if (MAXSIG > (2 * 32) && MAXSIG <= (3 * 32))
  60         return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
  61             (s1->__sigbits[1] ^ s2->__sigbits[1]) |
  62             ((s1->__sigbits[2] ^ s2->__sigbits[2]) & FILLSET2)));
  63 #else
  64 #error "fix me: MAXSIG out of bounds"
  65 #endif
  66 }
  67 
  68 /*
  69  * Common code for calling the user-specified signal handler.
  70  */
  71 void
  72 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
  73 {
  74         ulwp_t *self = curthread;
  75         uberdata_t *udp = self->ul_uberdata;
  76         struct sigaction uact;
  77         volatile struct sigaction *sap;
  78 
  79         /*
  80          * If we are taking a signal while parked or about to be parked
  81          * on __lwp_park() then remove ourself from the sleep queue so
  82          * that we can grab locks.  The code in mutex_lock_queue() and
  83          * cond_wait_common() will detect this and deal with it when
  84          * __lwp_park() returns.
  85          */
  86         unsleep_self();
  87         set_parking_flag(self, 0);
  88 
  89         if (__td_event_report(self, TD_CATCHSIG, udp)) {
  90                 self->ul_td_evbuf.eventnum = TD_CATCHSIG;
  91                 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
  92                 tdb_event(TD_CATCHSIG, udp);
  93         }
  94 
  95         /*
  96          * Get a self-consistent set of flags, handler, and mask
  97          * while holding the sig's sig_lock for the least possible time.
  98          * We must acquire the sig's sig_lock because some thread running
  99          * in sigaction() might be establishing a new signal handler.
 100          * The code in sigaction() acquires the writer lock; here
 101          * we acquire the readers lock to ehance concurrency in the
 102          * face of heavy signal traffic, such as generated by java.
 103          *
 104          * Locking exceptions:
 105          * No locking for a child of vfork().
 106          * If the signal is SIGPROF with an si_code of PROF_SIG,
 107          * then we assume that this signal was generated by
 108          * setitimer(ITIMER_REALPROF) set up by the dbx collector.
 109          * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
 110          * then we assume that the signal was generated by
 111          * a hardware performance counter overflow.
 112          * In these cases, assume that we need no locking.  It is the
 113          * monitoring program's responsibility to ensure correctness.
 114          */
 115         sap = &udp->siguaction[sig].sig_uaction;
 116         if (self->ul_vfork ||
 117             (sip != NULL &&
 118             ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
 119             (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
 120                 /* we wish this assignment could be atomic */
 121                 (void) memcpy(&uact, (void *)sap, sizeof (uact));
 122         } else {
 123                 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
 124                 lrw_rdlock(rwlp);
 125                 (void) memcpy(&uact, (void *)sap, sizeof (uact));
 126                 if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) &&
 127                     (sap->sa_flags & SA_RESETHAND))
 128                         sap->sa_sigaction = SIG_DFL;
 129                 lrw_unlock(rwlp);
 130         }
 131 
 132         /*
 133          * Set the proper signal mask and call the user's signal handler.
 134          * (We overrode the user-requested signal mask with maskset
 135          * so we currently have all blockable signals blocked.)
 136          *
 137          * We would like to ASSERT() that the signal is not a member of the
 138          * signal mask at the previous level (ucp->uc_sigmask) or the specified
 139          * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
 140          * /proc can override this via PCSSIG, so we don't bother.
 141          *
 142          * We would also like to ASSERT() that the signal mask at the previous
 143          * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
 144          * but /proc can change the thread's signal mask via PCSHOLD, so we
 145          * don't bother with that either.
 146          */
 147         ASSERT(ucp->uc_flags & UC_SIGMASK);
 148         if (self->ul_sigsuspend) {
 149                 ucp->uc_sigmask = self->ul_sigmask;
 150                 self->ul_sigsuspend = 0;
 151                 /* the sigsuspend() or pollsys() signal mask */
 152                 sigorset(&uact.sa_mask, &self->ul_tmpmask);
 153         } else {
 154                 /* the signal mask at the previous level */
 155                 sigorset(&uact.sa_mask, &ucp->uc_sigmask);
 156         }
 157         if (!(uact.sa_flags & SA_NODEFER))  /* add current signal */
 158                 (void) sigaddset(&uact.sa_mask, sig);
 159         self->ul_sigmask = uact.sa_mask;
 160         self->ul_siglink = ucp;
 161         (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask);
 162 
 163         /*
 164          * If this thread has been sent SIGCANCEL from the kernel
 165          * or from pthread_cancel(), it is being asked to exit.
 166          * The kernel may send SIGCANCEL without a siginfo struct.
 167          * If the SIGCANCEL is process-directed (from kill() or
 168          * sigqueue()), treat it as an ordinary signal.
 169          */
 170         if (sig == SIGCANCEL) {
 171                 if (sip == NULL || SI_FROMKERNEL(sip) ||
 172                     sip->si_code == SI_LWP) {
 173                         do_sigcancel();
 174                         goto out;
 175                 }
 176                 /* SIGCANCEL is ignored by default */
 177                 if (uact.sa_sigaction == SIG_DFL ||
 178                     uact.sa_sigaction == SIG_IGN)
 179                         goto out;
 180         }
 181 
 182         /*
 183          * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
 184          * we are an aio worker thread, cancel the aio request.
 185          */
 186         if (sig == SIGAIOCANCEL) {
 187                 aio_worker_t *aiowp = pthread_getspecific(_aio_key);
 188 
 189                 if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
 190                         siglongjmp(aiowp->work_jmp_buf, 1);
 191                 /* SIGLWP is ignored by default */
 192                 if (uact.sa_sigaction == SIG_DFL ||
 193                     uact.sa_sigaction == SIG_IGN)
 194                         goto out;
 195         }
 196 
 197         if (!(uact.sa_flags & SA_SIGINFO))
 198                 sip = NULL;
 199         __sighndlr(sig, sip, ucp, uact.sa_sigaction);
 200 
 201 #if defined(sparc) || defined(__sparc)
 202         /*
 203          * If this is a floating point exception and the queue
 204          * is non-empty, pop the top entry from the queue.  This
 205          * is to maintain expected behavior.
 206          */
 207         if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
 208                 fpregset_t *fp = &ucp->uc_mcontext.fpregs;
 209 
 210                 if (--fp->fpu_qcnt > 0) {
 211                         unsigned char i;
 212                         struct fq *fqp;
 213 
 214                         fqp = fp->fpu_q;
 215                         for (i = 0; i < fp->fpu_qcnt; i++)
 216                                 fqp[i] = fqp[i+1];
 217                 }
 218         }
 219 #endif  /* sparc */
 220 
 221 out:
 222         (void) setcontext(ucp);
 223         thr_panic("call_user_handler(): setcontext() returned");
 224 }
 225 
 226 /*
 227  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
 228  * zero and a deferred signal has been recorded on the current thread.
 229  * We are out of the critical region and are ready to take a signal.
 230  * The kernel has all signals blocked on this lwp, but our value of
 231  * ul_sigmask is the correct signal mask for the previous context.
 232  *
 233  * We call __sigresend() to atomically restore the signal mask and
 234  * cause the signal to be sent again with the remembered siginfo.
 235  * We will not return successfully from __sigresend() until the
 236  * application's signal handler has been run via sigacthandler().
 237  */
 238 void
 239 take_deferred_signal(int sig)
 240 {
 241         extern int __sigresend(int, siginfo_t *, sigset_t *);
 242         ulwp_t *self = curthread;
 243         siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
 244         siginfo_t *sip;
 245         int error;
 246 
 247         ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
 248 
 249         /*
 250          * If the signal handler was established with SA_RESETHAND,
 251          * the kernel has reset the handler to SIG_DFL, so we have
 252          * to reestablish the handler now so that it will be entered
 253          * again when we call __sigresend(), below.
 254          *
 255          * Logically, we should acquire and release the signal's
 256          * sig_lock around this operation to protect the integrity
 257          * of the signal action while we copy it, as is done below
 258          * in _libc_sigaction().  However, we may be on a user-level
 259          * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
 260          * might attempt to sleep on a different sleep queue and
 261          * that would corrupt the entire sleep queue mechanism.
 262          *
 263          * If we are on a sleep queue we will remove ourself from
 264          * it in call_user_handler(), called from sigacthandler(),
 265          * before entering the application's signal handler.
 266          * In the meantime, we must not acquire any locks.
 267          */
 268         if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
 269                 struct sigaction tact = suap->sig_uaction;
 270                 tact.sa_flags &= ~SA_NODEFER;
 271                 tact.sa_sigaction = self->ul_uberdata->sigacthandler;
 272                 tact.sa_mask = maskset;
 273                 (void) __sigaction(sig, &tact, NULL);
 274         }
 275 
 276         if (self->ul_siginfo.si_signo == 0)
 277                 sip = NULL;
 278         else
 279                 sip = &self->ul_siginfo;
 280 
 281         /* EAGAIN can happen only for a pending SIGSTOP signal */
 282         while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
 283                 continue;
 284         if (error)
 285                 thr_panic("take_deferred_signal(): __sigresend() failed");
 286 }
 287 
 288 /*
 289  * sigacthandler() attempts to clean up dangling uc_link pointers in
 290  * signal handling contexts when libc believes us to have escaped
 291  * a signal handler incorrectly in the past.
 292  *
 293  * Branded processes have a legitimate use for a chain including contexts
 294  * other than those used for signal handling when tracking emulation
 295  * requests from the kernel.  We allow them to disable this cleanup
 296  * behaviour.
 297  */
 298 static int escaped_context_cleanup = 1;
 299 
 300 void
 301 set_escaped_context_cleanup(int on)
 302 {
 303         escaped_context_cleanup = on;
 304 }
 305 
 306 void
 307 sigacthandler(int sig, siginfo_t *sip, void *uvp)
 308 {
 309         ucontext_t *ucp = uvp;
 310         ulwp_t *self = curthread;
 311 
 312         /*
 313          * Do this in case we took a signal while in a cancelable system call.
 314          * It does no harm if we were not in such a system call.
 315          */
 316         self->ul_sp = 0;
 317         if (sig != SIGCANCEL)
 318                 self->ul_cancel_async = self->ul_save_async;
 319 
 320         /*
 321          * If this thread has performed a longjmp() from a signal handler
 322          * back to main level some time in the past, it has left the kernel
 323          * thinking that it is still in the signal context.  We repair this
 324          * possible damage by setting ucp->uc_link to NULL if we know that
 325          * we are actually executing at main level (self->ul_siglink == NULL).
 326          * See the code for setjmp()/longjmp() for more details.
 327          */
 328         if (escaped_context_cleanup && self->ul_siglink == NULL)
 329                 ucp->uc_link = NULL;
 330 
 331         /*
 332          * If we are not in a critical region and are
 333          * not deferring signals, take the signal now.
 334          */
 335         if ((self->ul_critical + self->ul_sigdefer) == 0) {
 336                 call_user_handler(sig, sip, ucp);
 337                 /*
 338                  * On the surface, the following call seems redundant
 339                  * because call_user_handler() cannot return. However,
 340                  * we don't want to return from here because the compiler
 341                  * might recycle our frame. We want to keep it on the
 342                  * stack to assist debuggers such as pstack in identifying
 343                  * signal frames. The call to thr_panic() serves to prevent
 344                  * tail-call optimisation here.
 345                  */
 346                 thr_panic("sigacthandler(): call_user_handler() returned");
 347         }
 348 
 349         /*
 350          * We are in a critical region or we are deferring signals.  When
 351          * we emerge from the region we will call take_deferred_signal().
 352          */
 353         ASSERT(self->ul_cursig == 0);
 354         self->ul_cursig = (char)sig;
 355         if (sip != NULL)
 356                 (void) memcpy(&self->ul_siginfo,
 357                     sip, sizeof (siginfo_t));
 358         else
 359                 self->ul_siginfo.si_signo = 0;
 360 
 361         /*
 362          * Make sure that if we return to a call to __lwp_park()
 363          * or ___lwp_cond_wait() that it returns right away
 364          * (giving us a spurious wakeup but not a deadlock).
 365          */
 366         set_parking_flag(self, 0);
 367 
 368         /*
 369          * Return to the previous context with all signals blocked.
 370          * We will restore the signal mask in take_deferred_signal().
 371          * Note that we are calling the system call trap here, not
 372          * the setcontext() wrapper.  We don't want to change the
 373          * thread's ul_sigmask by this operation.
 374          */
 375         ucp->uc_sigmask = maskset;
 376         (void) __setcontext(ucp);
 377         thr_panic("sigacthandler(): __setcontext() returned");
 378 }
 379 
 380 #pragma weak _sigaction = sigaction
 381 int
 382 sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
 383 {
 384         ulwp_t *self = curthread;
 385         uberdata_t *udp = self->ul_uberdata;
 386         struct sigaction oaction;
 387         struct sigaction tact;
 388         struct sigaction *tactp = NULL;
 389         int rv;
 390 
 391         if (sig <= 0 || sig >= NSIG) {
 392                 errno = EINVAL;
 393                 return (-1);
 394         }
 395 
 396         if (!self->ul_vfork)
 397                 lrw_wrlock(&udp->siguaction[sig].sig_lock);
 398 
 399         oaction = udp->siguaction[sig].sig_uaction;
 400 
 401         if (nact != NULL) {
 402                 tact = *nact;   /* make a copy so we can modify it */
 403                 tactp = &tact;
 404                 delete_reserved_signals(&tact.sa_mask);
 405 
 406 #if !defined(_LP64)
 407                 tact.sa_resv[0] = tact.sa_resv[1] = 0;  /* cleanliness */
 408 #endif
 409                 /*
 410                  * To be compatible with the behavior of SunOS 4.x:
 411                  * If the new signal handler is SIG_IGN or SIG_DFL, do
 412                  * not change the signal's entry in the siguaction array.
 413                  * This allows a child of vfork(2) to set signal handlers
 414                  * to SIG_IGN or SIG_DFL without affecting the parent.
 415                  *
 416                  * This also covers a race condition with some thread
 417                  * setting the signal action to SIG_DFL or SIG_IGN
 418                  * when the thread has also received and deferred
 419                  * that signal.  When the thread takes the deferred
 420                  * signal, even though it has set the action to SIG_DFL
 421                  * or SIG_IGN, it will execute the old signal handler
 422                  * anyway.  This is an inherent signaling race condition
 423                  * and is not a bug.
 424                  *
 425                  * A child of vfork() is not allowed to change signal
 426                  * handlers to anything other than SIG_DFL or SIG_IGN.
 427                  */
 428                 if (self->ul_vfork) {
 429                         if (tact.sa_sigaction != SIG_IGN)
 430                                 tact.sa_sigaction = SIG_DFL;
 431                 } else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
 432                         /*
 433                          * Always catch these signals.
 434                          * We need SIGCANCEL for pthread_cancel() to work.
 435                          * We need SIGAIOCANCEL for aio_cancel() to work.
 436                          */
 437                         udp->siguaction[sig].sig_uaction = tact;
 438                         if (tact.sa_sigaction == SIG_DFL ||
 439                             tact.sa_sigaction == SIG_IGN)
 440                                 tact.sa_flags = SA_SIGINFO;
 441                         else {
 442                                 tact.sa_flags |= SA_SIGINFO;
 443                                 tact.sa_flags &=
 444                                     ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
 445                         }
 446                         tact.sa_sigaction = udp->sigacthandler;
 447                         tact.sa_mask = maskset;
 448                 } else if (tact.sa_sigaction != SIG_DFL &&
 449                     tact.sa_sigaction != SIG_IGN) {
 450                         udp->siguaction[sig].sig_uaction = tact;
 451                         tact.sa_flags &= ~SA_NODEFER;
 452                         tact.sa_sigaction = udp->sigacthandler;
 453                         tact.sa_mask = maskset;
 454                 }
 455         }
 456 
 457         if ((rv = __sigaction(sig, tactp, oact)) != 0)
 458                 udp->siguaction[sig].sig_uaction = oaction;
 459         else if (oact != NULL &&
 460             oact->sa_sigaction != SIG_DFL &&
 461             oact->sa_sigaction != SIG_IGN)
 462                 *oact = oaction;
 463 
 464         /*
 465          * We detect setting the disposition of SIGIO just to set the
 466          * _sigio_enabled flag for the asynchronous i/o (aio) code.
 467          */
 468         if (sig == SIGIO && rv == 0 && tactp != NULL) {
 469                 _sigio_enabled =
 470                     (tactp->sa_handler != SIG_DFL &&
 471                     tactp->sa_handler != SIG_IGN);
 472         }
 473 
 474         if (!self->ul_vfork)
 475                 lrw_unlock(&udp->siguaction[sig].sig_lock);
 476         return (rv);
 477 }
 478 
 479 /*
 480  * This is a private interface for the lx brand.
 481  */
 482 void
 483 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
 484     void (**osigacthandler)(int, siginfo_t *, void *),
 485     int (*brsetctxt)(const ucontext_t *))
 486 {
 487         ulwp_t *self = curthread;
 488         uberdata_t *udp = self->ul_uberdata;
 489 
 490         if (osigacthandler != NULL)
 491                 *osigacthandler = udp->sigacthandler;
 492 
 493         udp->sigacthandler = nsigacthandler;
 494 
 495         if (brsetctxt != NULL)
 496                 udp->setctxt = brsetctxt;
 497 }
 498 
 499 /*
 500  * Tell the kernel to block all signals.
 501  * Use the schedctl interface, or failing that, use __lwp_sigmask().
 502  * This action can be rescinded only by making a system call that
 503  * sets the signal mask:
 504  *      __lwp_sigmask(), __sigprocmask(), __setcontext(),
 505  *      __sigsuspend() or __pollsys().
 506  * In particular, this action cannot be reversed by assigning
 507  * scp->sc_sigblock = 0.  That would be a way to lose signals.
 508  * See the definition of restore_signals(self).
 509  */
 510 void
 511 block_all_signals(ulwp_t *self)
 512 {
 513         volatile sc_shared_t *scp;
 514 
 515         enter_critical(self);
 516         if ((scp = self->ul_schedctl) != NULL ||
 517             (scp = setup_schedctl()) != NULL)
 518                 scp->sc_sigblock = 1;
 519         else
 520                 (void) __lwp_sigmask(SIG_SETMASK, &maskset);
 521         exit_critical(self);
 522 }
 523 
 524 /*
 525  * setcontext() has code that forcibly restores the curthread
 526  * pointer in a context passed to the setcontext(2) syscall.
 527  *
 528  * Certain processes may need to disable this feature, so these routines
 529  * provide the mechanism to do so.
 530  *
 531  * (As an example, branded 32-bit x86 processes may use %gs for their own
 532  * purposes, so they need to be able to specify a %gs value to be restored
 533  * on return from a signal handler via the passed ucontext_t.)
 534  */
 535 static int setcontext_enforcement = 1;
 536 
 537 void
 538 set_setcontext_enforcement(int on)
 539 {
 540         setcontext_enforcement = on;
 541 }
 542 
 543 /*
 544  * The LX brand emulation library implements an operation that is analogous to
 545  * setcontext(), but takes a different path in to the kernel.  So that it can
 546  * correctly restore a signal mask, we expose just the signal mask handling
 547  * part of the regular setcontext() routine as a private interface.
 548  */
 549 void
 550 setcontext_sigmask(ucontext_t *ucp)
 551 {
 552         ulwp_t *self = curthread;
 553 
 554         if (ucp->uc_flags & UC_SIGMASK) {
 555                 block_all_signals(self);
 556                 delete_reserved_signals(&ucp->uc_sigmask);
 557                 self->ul_sigmask = ucp->uc_sigmask;
 558                 if (self->ul_cursig) {
 559                         /*
 560                          * We have a deferred signal present.
 561                          * The signal mask will be set when the
 562                          * signal is taken in take_deferred_signal().
 563                          */
 564                         ASSERT(self->ul_critical + self->ul_sigdefer != 0);
 565                         ucp->uc_flags &= ~UC_SIGMASK;
 566                 }
 567         }
 568 }
 569 
 570 #pragma weak _setcontext = setcontext
 571 int
 572 setcontext(const ucontext_t *ucp)
 573 {
 574         ulwp_t *self = curthread;
 575         uberdata_t *udp = self->ul_uberdata;
 576         int ret;
 577         ucontext_t uc;
 578 
 579         /*
 580          * Returning from the main context (uc_link == NULL) causes
 581          * the thread to exit.  See setcontext(2) and makecontext(3C).
 582          */
 583         if (ucp == NULL)
 584                 thr_exit(NULL);
 585         (void) memcpy(&uc, ucp, sizeof (uc));
 586 
 587         /*
 588          * Restore previous signal mask and context link.
 589          */
 590         setcontext_sigmask(&uc);
 591         self->ul_siglink = uc.uc_link;
 592 
 593         /*
 594          * We don't know where this context structure has been.
 595          * Preserve the curthread pointer, at least.
 596          *
 597          * Allow this feature to be disabled if a particular process
 598          * requests it.
 599          */
 600         if (setcontext_enforcement) {
 601 #if defined(__sparc)
 602                 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
 603 #elif defined(__amd64)
 604                 uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
 605 #elif defined(__i386)
 606                 uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
 607 #else
 608 #error "none of __sparc, __amd64, __i386 defined"
 609 #endif
 610         }
 611 
 612         /*
 613          * Make sure that if we return to a call to __lwp_park()
 614          * or ___lwp_cond_wait() that it returns right away
 615          * (giving us a spurious wakeup but not a deadlock).
 616          */
 617         set_parking_flag(self, 0);
 618         self->ul_sp = 0;
 619         ret = udp->setctxt(&uc);
 620 
 621         /*
 622          * It is OK for setcontext() to return if the user has not specified
 623          * UC_CPU.
 624          */
 625         if (uc.uc_flags & UC_CPU)
 626                 thr_panic("setcontext(): __setcontext() returned");
 627         return (ret);
 628 }
 629 
 630 #pragma weak _thr_sigsetmask = thr_sigsetmask
 631 int
 632 thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
 633 {
 634         ulwp_t *self = curthread;
 635         sigset_t saveset;
 636 
 637         if (set == NULL) {
 638                 enter_critical(self);
 639                 if (oset != NULL)
 640                         *oset = self->ul_sigmask;
 641                 exit_critical(self);
 642         } else {
 643                 switch (how) {
 644                 case SIG_BLOCK:
 645                 case SIG_UNBLOCK:
 646                 case SIG_SETMASK:
 647                         break;
 648                 default:
 649                         return (EINVAL);
 650                 }
 651 
 652                 /*
 653                  * The assignments to self->ul_sigmask must be protected from
 654                  * signals.  The nuances of this code are subtle.  Be careful.
 655                  */
 656                 block_all_signals(self);
 657                 if (oset != NULL)
 658                         saveset = self->ul_sigmask;
 659                 switch (how) {
 660                 case SIG_BLOCK:
 661                         self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
 662                         self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
 663                         self->ul_sigmask.__sigbits[2] |= set->__sigbits[2];
 664                         self->ul_sigmask.__sigbits[3] |= set->__sigbits[3];
 665                         break;
 666                 case SIG_UNBLOCK:
 667                         self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
 668                         self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
 669                         self->ul_sigmask.__sigbits[2] &= ~set->__sigbits[2];
 670                         self->ul_sigmask.__sigbits[3] &= ~set->__sigbits[3];
 671                         break;
 672                 case SIG_SETMASK:
 673                         self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
 674                         self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
 675                         self->ul_sigmask.__sigbits[2] = set->__sigbits[2];
 676                         self->ul_sigmask.__sigbits[3] = set->__sigbits[3];
 677                         break;
 678                 }
 679                 delete_reserved_signals(&self->ul_sigmask);
 680                 if (oset != NULL)
 681                         *oset = saveset;
 682                 restore_signals(self);
 683         }
 684 
 685         return (0);
 686 }
 687 
 688 #pragma weak _pthread_sigmask = pthread_sigmask
 689 int
 690 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
 691 {
 692         return (thr_sigsetmask(how, set, oset));
 693 }
 694 
 695 #pragma weak _sigprocmask = sigprocmask
 696 int
 697 sigprocmask(int how, const sigset_t *set, sigset_t *oset)
 698 {
 699         int error;
 700 
 701         /*
 702          * Guard against children of vfork().
 703          */
 704         if (curthread->ul_vfork)
 705                 return (__sigprocmask(how, set, oset));
 706 
 707         if ((error = thr_sigsetmask(how, set, oset)) != 0) {
 708                 errno = error;
 709                 return (-1);
 710         }
 711 
 712         return (0);
 713 }
 714 
 715 /*
 716  * Called at library initialization to set up signal handling.
 717  * All we really do is initialize the sig_lock rwlocks.
 718  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
 719  * However, if any signal handlers were established on alternate
 720  * link maps before the primary link map has been initialized,
 721  * then inform the kernel of the new sigacthandler.
 722  */
 723 void
 724 signal_init()
 725 {
 726         uberdata_t *udp = curthread->ul_uberdata;
 727         struct sigaction *sap;
 728         struct sigaction act;
 729         rwlock_t *rwlp;
 730         int sig;
 731 
 732         for (sig = 0; sig < NSIG; sig++) {
 733                 rwlp = &udp->siguaction[sig].sig_lock;
 734                 rwlp->rwlock_magic = RWL_MAGIC;
 735                 rwlp->mutex.mutex_flag = LOCK_INITED;
 736                 rwlp->mutex.mutex_magic = MUTEX_MAGIC;
 737                 sap = &udp->siguaction[sig].sig_uaction;
 738                 if (sap->sa_sigaction != SIG_DFL &&
 739                     sap->sa_sigaction != SIG_IGN &&
 740                     __sigaction(sig, NULL, &act) == 0 &&
 741                     act.sa_sigaction != SIG_DFL &&
 742                     act.sa_sigaction != SIG_IGN) {
 743                         act = *sap;
 744                         act.sa_flags &= ~SA_NODEFER;
 745                         act.sa_sigaction = udp->sigacthandler;
 746                         act.sa_mask = maskset;
 747                         (void) __sigaction(sig, &act, NULL);
 748                 }
 749         }
 750 }
 751 
 752 /*
 753  * Common code for cancelling self in _sigcancel() and pthread_cancel().
 754  * First record the fact that a cancellation is pending.
 755  * Then, if cancellation is disabled or if we are holding unprotected
 756  * libc locks, just return to defer the cancellation.
 757  * Then, if we are at a cancellation point (ul_cancelable) just
 758  * return and let _canceloff() do the exit.
 759  * Else exit immediately if async mode is in effect.
 760  */
 761 void
 762 do_sigcancel(void)
 763 {
 764         ulwp_t *self = curthread;
 765 
 766         ASSERT(self->ul_critical == 0);
 767         ASSERT(self->ul_sigdefer == 0);
 768         self->ul_cancel_pending = 1;
 769         if (self->ul_cancel_async &&
 770             !self->ul_cancel_disabled &&
 771             self->ul_libc_locks == 0 &&
 772             !self->ul_cancelable)
 773                 pthread_exit(PTHREAD_CANCELED);
 774         set_cancel_pending_flag(self, 0);
 775 }
 776 
 777 /*
 778  * Set up the SIGCANCEL handler for threads cancellation,
 779  * needed only when we have more than one thread,
 780  * or the SIGAIOCANCEL handler for aio cancellation,
 781  * called when aio is initialized, in __uaio_init().
 782  */
 783 void
 784 setup_cancelsig(int sig)
 785 {
 786         uberdata_t *udp = curthread->ul_uberdata;
 787         rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
 788         struct sigaction act;
 789 
 790         ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
 791         lrw_rdlock(rwlp);
 792         act = udp->siguaction[sig].sig_uaction;
 793         lrw_unlock(rwlp);
 794         if (act.sa_sigaction == SIG_DFL ||
 795             act.sa_sigaction == SIG_IGN)
 796                 act.sa_flags = SA_SIGINFO;
 797         else {
 798                 act.sa_flags |= SA_SIGINFO;
 799                 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
 800         }
 801         act.sa_sigaction = udp->sigacthandler;
 802         act.sa_mask = maskset;
 803         (void) __sigaction(sig, &act, NULL);
 804 }