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 */
26
27 #include "lint.h"
28 #include <sys/feature_tests.h>
29 /*
30 * setcontext() really can return, if UC_CPU is not specified.
31 * Make the compiler shut up about it.
32 */
33 #if defined(__NORETURN)
34 #undef __NORETURN
35 #endif
36 #define __NORETURN
37 #include "thr_uberdata.h"
38 #include "asyncio.h"
39 #include <signal.h>
40 #include <siginfo.h>
41 #include <sys/systm.h>
42
43 /* maskable signals */
44 const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3};
267 if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
268 struct sigaction tact = suap->sig_uaction;
269 tact.sa_flags &= ~SA_NODEFER;
270 tact.sa_sigaction = self->ul_uberdata->sigacthandler;
271 tact.sa_mask = maskset;
272 (void) __sigaction(sig, &tact, NULL);
273 }
274
275 if (self->ul_siginfo.si_signo == 0)
276 sip = NULL;
277 else
278 sip = &self->ul_siginfo;
279
280 /* EAGAIN can happen only for a pending SIGSTOP signal */
281 while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
282 continue;
283 if (error)
284 thr_panic("take_deferred_signal(): __sigresend() failed");
285 }
286
287 void
288 sigacthandler(int sig, siginfo_t *sip, void *uvp)
289 {
290 ucontext_t *ucp = uvp;
291 ulwp_t *self = curthread;
292
293 /*
294 * Do this in case we took a signal while in a cancelable system call.
295 * It does no harm if we were not in such a system call.
296 */
297 self->ul_sp = 0;
298 if (sig != SIGCANCEL)
299 self->ul_cancel_async = self->ul_save_async;
300
301 /*
302 * If this thread has performed a longjmp() from a signal handler
303 * back to main level some time in the past, it has left the kernel
304 * thinking that it is still in the signal context. We repair this
305 * possible damage by setting ucp->uc_link to NULL if we know that
306 * we are actually executing at main level (self->ul_siglink == NULL).
307 * See the code for setjmp()/longjmp() for more details.
308 */
309 if (self->ul_siglink == NULL)
310 ucp->uc_link = NULL;
311
312 /*
313 * If we are not in a critical region and are
314 * not deferring signals, take the signal now.
315 */
316 if ((self->ul_critical + self->ul_sigdefer) == 0) {
317 call_user_handler(sig, sip, ucp);
318 /*
319 * On the surface, the following call seems redundant
320 * because call_user_handler() cannot return. However,
321 * we don't want to return from here because the compiler
322 * might recycle our frame. We want to keep it on the
323 * stack to assist debuggers such as pstack in identifying
324 * signal frames. The call to thr_panic() serves to prevent
325 * tail-call optimisation here.
326 */
327 thr_panic("sigacthandler(): call_user_handler() returned");
328 }
329
441 oact->sa_sigaction != SIG_DFL &&
442 oact->sa_sigaction != SIG_IGN)
443 *oact = oaction;
444
445 /*
446 * We detect setting the disposition of SIGIO just to set the
447 * _sigio_enabled flag for the asynchronous i/o (aio) code.
448 */
449 if (sig == SIGIO && rv == 0 && tactp != NULL) {
450 _sigio_enabled =
451 (tactp->sa_handler != SIG_DFL &&
452 tactp->sa_handler != SIG_IGN);
453 }
454
455 if (!self->ul_vfork)
456 lrw_unlock(&udp->siguaction[sig].sig_lock);
457 return (rv);
458 }
459
460 /*
461 * This is a private interface for the linux brand interface.
462 */
463 void
464 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
465 void (**osigacthandler)(int, siginfo_t *, void *))
466 {
467 ulwp_t *self = curthread;
468 uberdata_t *udp = self->ul_uberdata;
469
470 if (osigacthandler != NULL)
471 *osigacthandler = udp->sigacthandler;
472
473 udp->sigacthandler = nsigacthandler;
474 }
475
476 /*
477 * Tell the kernel to block all signals.
478 * Use the schedctl interface, or failing that, use __lwp_sigmask().
479 * This action can be rescinded only by making a system call that
480 * sets the signal mask:
481 * __lwp_sigmask(), __sigprocmask(), __setcontext(),
482 * __sigsuspend() or __pollsys().
483 * In particular, this action cannot be reversed by assigning
484 * scp->sc_sigblock = 0. That would be a way to lose signals.
485 * See the definition of restore_signals(self).
486 */
487 void
488 block_all_signals(ulwp_t *self)
489 {
490 volatile sc_shared_t *scp;
491
492 enter_critical(self);
493 if ((scp = self->ul_schedctl) != NULL ||
500
501 /*
502 * setcontext() has code that forcibly restores the curthread
503 * pointer in a context passed to the setcontext(2) syscall.
504 *
505 * Certain processes may need to disable this feature, so these routines
506 * provide the mechanism to do so.
507 *
508 * (As an example, branded 32-bit x86 processes may use %gs for their own
509 * purposes, so they need to be able to specify a %gs value to be restored
510 * on return from a signal handler via the passed ucontext_t.)
511 */
512 static int setcontext_enforcement = 1;
513
514 void
515 set_setcontext_enforcement(int on)
516 {
517 setcontext_enforcement = on;
518 }
519
520 #pragma weak _setcontext = setcontext
521 int
522 setcontext(const ucontext_t *ucp)
523 {
524 ulwp_t *self = curthread;
525 int ret;
526 ucontext_t uc;
527
528 /*
529 * Returning from the main context (uc_link == NULL) causes
530 * the thread to exit. See setcontext(2) and makecontext(3C).
531 */
532 if (ucp == NULL)
533 thr_exit(NULL);
534 (void) memcpy(&uc, ucp, sizeof (uc));
535
536 /*
537 * Restore previous signal mask and context link.
538 */
539 if (uc.uc_flags & UC_SIGMASK) {
540 block_all_signals(self);
541 delete_reserved_signals(&uc.uc_sigmask);
542 self->ul_sigmask = uc.uc_sigmask;
543 if (self->ul_cursig) {
544 /*
545 * We have a deferred signal present.
546 * The signal mask will be set when the
547 * signal is taken in take_deferred_signal().
548 */
549 ASSERT(self->ul_critical + self->ul_sigdefer != 0);
550 uc.uc_flags &= ~UC_SIGMASK;
551 }
552 }
553 self->ul_siglink = uc.uc_link;
554
555 /*
556 * We don't know where this context structure has been.
557 * Preserve the curthread pointer, at least.
558 *
559 * Allow this feature to be disabled if a particular process
560 * requests it.
561 */
562 if (setcontext_enforcement) {
563 #if defined(__sparc)
564 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
565 #elif defined(__amd64)
566 uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
567 #elif defined(__i386)
568 uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
569 #else
570 #error "none of __sparc, __amd64, __i386 defined"
571 #endif
572 }
573
574 /*
575 * Make sure that if we return to a call to __lwp_park()
576 * or ___lwp_cond_wait() that it returns right away
577 * (giving us a spurious wakeup but not a deadlock).
578 */
579 set_parking_flag(self, 0);
580 self->ul_sp = 0;
581 ret = __setcontext(&uc);
582
583 /*
584 * It is OK for setcontext() to return if the user has not specified
585 * UC_CPU.
586 */
587 if (uc.uc_flags & UC_CPU)
588 thr_panic("setcontext(): __setcontext() returned");
589 return (ret);
590 }
591
592 #pragma weak _thr_sigsetmask = thr_sigsetmask
593 int
594 thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
595 {
596 ulwp_t *self = curthread;
597 sigset_t saveset;
598
599 if (set == NULL) {
600 enter_critical(self);
601 if (oset != NULL)
|
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};
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
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 ||
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)
|