xref: /dflybsd-src/lib/libc/gen/ucontext.c (revision c27d72478c10a7d8648217bc817f8429939e8828)
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 
39a32e3ba6SSascha Wildner int _swapcontext(ucontext_t *, const ucontext_t *);
40a32e3ba6SSascha Wildner int _setcontext(const ucontext_t *);
41a32e3ba6SSascha Wildner 
42e2742650SMatthew Dillon /*
43e2742650SMatthew Dillon  * We need to block most signals during a context switch so we do not
44e2742650SMatthew Dillon  * dispatch a signal vector during a context switch.
45e2742650SMatthew Dillon  */
4663261abbSMatthew Dillon #if 0
47e2742650SMatthew Dillon static sigset_t sigset_block_all;
48e2742650SMatthew Dillon 
49e2742650SMatthew Dillon static void __sigset_block_all_setup(void) __attribute__ ((constructor));
50e2742650SMatthew Dillon 
51e2742650SMatthew Dillon static void
52e2742650SMatthew Dillon __sigset_block_all_setup(void)
53e2742650SMatthew Dillon {
54e2742650SMatthew Dillon 	sigfillset(&sigset_block_all);
55e2742650SMatthew Dillon 	sigdelset(&sigset_block_all, SIGSEGV);
56e2742650SMatthew Dillon 	sigdelset(&sigset_block_all, SIGBUS);
57e2742650SMatthew Dillon 	sigdelset(&sigset_block_all, SIGILL);
58e2742650SMatthew Dillon }
5963261abbSMatthew Dillon #endif
60e2742650SMatthew Dillon 
61e2742650SMatthew Dillon /*
62e2742650SMatthew Dillon  * Save the calling context in (oucp) then switch to (ucp).
63e2742650SMatthew Dillon  */
64e2742650SMatthew Dillon int
_swapcontext(ucontext_t * oucp,const ucontext_t * ucp)65e2742650SMatthew Dillon _swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
66e2742650SMatthew Dillon {
67e2742650SMatthew Dillon 	int ret;
68e2742650SMatthew Dillon 
6963261abbSMatthew Dillon 	if (getcontext(oucp) == 0) {
70*c27d7247SMatthew Dillon 		if (ucp->uc_mcontext.mc_len == 0)
71*c27d7247SMatthew Dillon 			ret = -1;
72*c27d7247SMatthew Dillon 		else
7363261abbSMatthew Dillon 			ret = sigreturn(__DECONST(ucontext_t *, ucp));
74e2742650SMatthew Dillon 	} else {
7563261abbSMatthew Dillon 		ret = 0;
76e2742650SMatthew Dillon 	}
77e2742650SMatthew Dillon 	return(ret);
78e2742650SMatthew Dillon }
79e2742650SMatthew Dillon 
80e2742650SMatthew Dillon /*
8163261abbSMatthew Dillon  * Switch to the target context, use sigreturn() to properly restore
8263261abbSMatthew Dillon  * everything, including rflags and to avoid scribbling over the
8363261abbSMatthew Dillon  * target stack's red-zone.
84e2742650SMatthew Dillon  *
8563261abbSMatthew Dillon  * Note that setcontext() can be called with a ucontext from a signal,
8663261abbSMatthew Dillon  * so the signal state must be restored and there is really no way to
8763261abbSMatthew Dillon  * avoid making a system call :-(
88e2742650SMatthew Dillon  */
89e2742650SMatthew Dillon int
_setcontext(const ucontext_t * ucp)90f8406b33Szrj _setcontext(const ucontext_t *ucp)
91e2742650SMatthew Dillon {
92e2742650SMatthew Dillon 	int ret;
93e2742650SMatthew Dillon 
94*c27d7247SMatthew Dillon 	/*
95*c27d7247SMatthew Dillon 	 * Return failure if the context is invalid
96*c27d7247SMatthew Dillon 	 */
97*c27d7247SMatthew Dillon 	if (ucp->uc_mcontext.mc_len == 0)
98*c27d7247SMatthew Dillon 		return -1;
99*c27d7247SMatthew Dillon 
100*c27d7247SMatthew Dillon 	/*
101*c27d7247SMatthew Dillon 	 * XXX: shouldn't sigreturn() take const? or does it modify ucp?
102*c27d7247SMatthew Dillon 	 */
103f8406b33Szrj 	ret = sigreturn(__DECONST(ucontext_t *, ucp));
104819d0c16SMatthew Dillon 
105e2742650SMatthew Dillon 	return(ret);
106e2742650SMatthew Dillon }
107f8406b33Szrj 
108f8406b33Szrj __weak_reference(_swapcontext, swapcontext);
109f8406b33Szrj __weak_reference(_setcontext, setcontext);
110