1 /* 2 * Copyright (c) 2007 Matthew T. Emmerton <matt@gsicomp.on.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Neither the name of the author nor the names of its contributors 11 * may be used to endorse or promote products derived from this software 12 * without specific prior written permission. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $DragonFly: src/lib/libc/gen/ucontext.c,v 1.1 2007/01/16 07:16:23 dillon Exp $ 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/signal.h> 32 #include <sys/ucontext.h> 33 34 #include <errno.h> 35 #include <stdarg.h> 36 #include <stdlib.h> 37 #include <unistd.h> 38 #include <signal.h> 39 40 /* Prototypes */ 41 __weak_reference(_swapcontext, swapcontext); 42 __weak_reference(_setcontext, setcontext); 43 44 int get_mcontext(mcontext_t *); 45 int set_mcontext(const mcontext_t *); 46 47 /* 48 * We need to block most signals during a context switch so we do not 49 * dispatch a signal vector during a context switch. 50 */ 51 static sigset_t sigset_block_all; 52 53 static void __sigset_block_all_setup(void) __attribute__ ((constructor)); 54 55 static void 56 __sigset_block_all_setup(void) 57 { 58 sigfillset(&sigset_block_all); 59 sigdelset(&sigset_block_all, SIGSEGV); 60 sigdelset(&sigset_block_all, SIGBUS); 61 sigdelset(&sigset_block_all, SIGILL); 62 } 63 64 /* 65 * Save the calling context in (oucp) then switch to (ucp). 66 * 67 * Block all signals while switching contexts. get_mcontext() returns zero 68 * when retrieving a context. 69 * 70 * When some other thread calls set_mcontext() to resume our thread, 71 * the resume point causes get_mcontext() to return non-zero to us. 72 * Signals will be blocked and we must restore the signal mask before 73 * returning. 74 */ 75 int 76 _swapcontext(ucontext_t *oucp, const ucontext_t *ucp) 77 { 78 int ret; 79 80 ret = _sigprocmask(SIG_BLOCK, &sigset_block_all, &oucp->uc_sigmask); 81 if (ret == 0) { 82 if (get_mcontext(&oucp->uc_mcontext) == 0) { 83 ret = set_mcontext(&ucp->uc_mcontext); 84 } else { 85 ret = _sigprocmask(SIG_SETMASK, &oucp->uc_sigmask, NULL); 86 } 87 } 88 return(ret); 89 } 90 91 /* 92 * Switch to the target context. The current signal mask is saved in ucp 93 * and all signals are blocked. The call to set_mcontext() causes the 94 * specified context to be switched to (usually resuming as a return from 95 * the get_mcontext() procedure). The current context is thrown away. 96 * 97 * The target context being resumed is responsible for restoring the 98 * signal mask appropriate for the target context. 99 */ 100 int 101 _setcontext(ucontext_t *ucp) 102 { 103 int ret; 104 105 ret = _sigprocmask(SIG_BLOCK, &sigset_block_all, &ucp->uc_sigmask); 106 if (ret == 0) 107 ret = set_mcontext(&ucp->uc_mcontext); 108 return(ret); 109 } 110 111