xref: /dflybsd-src/lib/libc/gen/ucontext.c (revision f8406b33bbed7faf9fbde74cd27d301a5cae7cbc)
1e2742650SMatthew Dillon /*
2e2742650SMatthew Dillon  * Copyright (c) 2007 Matthew T. Emmerton <matt@gsicomp.on.ca>
3e2742650SMatthew Dillon  * All rights reserved.
4e2742650SMatthew Dillon  *
5e2742650SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6e2742650SMatthew Dillon  * modification, are permitted provided that the following conditions
7e2742650SMatthew Dillon  * are met:
8e2742650SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
9e2742650SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
10e2742650SMatthew Dillon  * 2. Neither the name of the author nor the names of its contributors
11e2742650SMatthew Dillon  *    may be used to endorse or promote products derived from this software
12e2742650SMatthew Dillon  *    without specific prior written permission.
13e2742650SMatthew Dillon  *
14e2742650SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e2742650SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e2742650SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e2742650SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e2742650SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e2742650SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e2742650SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e2742650SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e2742650SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e2742650SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e2742650SMatthew Dillon  * SUCH DAMAGE.
25e2742650SMatthew Dillon  */
26e2742650SMatthew Dillon 
27e2b7bcaeSSascha Wildner #include "namespace.h"
28e2742650SMatthew Dillon #include <sys/param.h>
29e2742650SMatthew Dillon #include <sys/signal.h>
30e2742650SMatthew Dillon #include <sys/ucontext.h>
31e2742650SMatthew Dillon 
32e2742650SMatthew Dillon #include <errno.h>
33e2742650SMatthew Dillon #include <stdarg.h>
34e2742650SMatthew Dillon #include <stdlib.h>
35e2742650SMatthew Dillon #include <unistd.h>
36e2742650SMatthew Dillon #include <signal.h>
37e2b7bcaeSSascha Wildner #include "un-namespace.h"
38e2742650SMatthew Dillon 
39e2742650SMatthew Dillon /*
40e2742650SMatthew Dillon  * We need to block most signals during a context switch so we do not
41e2742650SMatthew Dillon  * dispatch a signal vector during a context switch.
42e2742650SMatthew Dillon  */
4363261abbSMatthew Dillon #if 0
44e2742650SMatthew Dillon static sigset_t sigset_block_all;
45e2742650SMatthew Dillon 
46e2742650SMatthew Dillon static void __sigset_block_all_setup(void) __attribute__ ((constructor));
47e2742650SMatthew Dillon 
48e2742650SMatthew Dillon static void
49e2742650SMatthew Dillon __sigset_block_all_setup(void)
50e2742650SMatthew Dillon {
51e2742650SMatthew Dillon 	sigfillset(&sigset_block_all);
52e2742650SMatthew Dillon 	sigdelset(&sigset_block_all, SIGSEGV);
53e2742650SMatthew Dillon 	sigdelset(&sigset_block_all, SIGBUS);
54e2742650SMatthew Dillon 	sigdelset(&sigset_block_all, SIGILL);
55e2742650SMatthew Dillon }
5663261abbSMatthew Dillon #endif
57e2742650SMatthew Dillon 
58e2742650SMatthew Dillon /*
59e2742650SMatthew Dillon  * Save the calling context in (oucp) then switch to (ucp).
60e2742650SMatthew Dillon  */
61e2742650SMatthew Dillon int
62e2742650SMatthew Dillon _swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
63e2742650SMatthew Dillon {
64e2742650SMatthew Dillon 	int ret;
65e2742650SMatthew Dillon 
6663261abbSMatthew Dillon 	if (getcontext(oucp) == 0) {
6763261abbSMatthew Dillon 		ret = sigreturn(__DECONST(ucontext_t *, ucp));
68e2742650SMatthew Dillon 	} else {
6963261abbSMatthew Dillon 		ret = 0;
70e2742650SMatthew Dillon 	}
71e2742650SMatthew Dillon 	return(ret);
72e2742650SMatthew Dillon }
73e2742650SMatthew Dillon 
74e2742650SMatthew Dillon /*
7563261abbSMatthew Dillon  * Switch to the target context, use sigreturn() to properly restore
7663261abbSMatthew Dillon  * everything, including rflags and to avoid scribbling over the
7763261abbSMatthew Dillon  * target stack's red-zone.
78e2742650SMatthew Dillon  *
7963261abbSMatthew Dillon  * Note that setcontext() can be called with a ucontext from a signal,
8063261abbSMatthew Dillon  * so the signal state must be restored and there is really no way to
8163261abbSMatthew Dillon  * avoid making a system call :-(
82e2742650SMatthew Dillon  */
83e2742650SMatthew Dillon int
84*f8406b33Szrj _setcontext(const ucontext_t *ucp)
85e2742650SMatthew Dillon {
86e2742650SMatthew Dillon 	int ret;
87e2742650SMatthew Dillon 
88*f8406b33Szrj 	/* XXX: shouldn't sigreturn() take const? or does it modify ucp? */
89*f8406b33Szrj 	ret = sigreturn(__DECONST(ucontext_t *, ucp));
90819d0c16SMatthew Dillon 
91e2742650SMatthew Dillon 	return(ret);
92e2742650SMatthew Dillon }
93*f8406b33Szrj 
94*f8406b33Szrj __weak_reference(_swapcontext, swapcontext);
95*f8406b33Szrj __weak_reference(_setcontext, setcontext);
96