1 |
/* |
2 |
* Copyright (c) 2001-2006 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without modifica- |
5 |
* tion, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright notice, |
8 |
* this list of conditions and the following disclaimer. |
9 |
* |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* |
14 |
* 3. The name of the author may not be used to endorse or promote products |
15 |
* derived from this software without specific prior written permission. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 |
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 |
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
25 |
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
* |
28 |
* This library is modelled strictly after Ralf S. Engelschalls article at |
29 |
* http://www.gnu.org/software/pth/rse-pmt.ps. So most of the credit must |
30 |
* go to Ralf S. Engelschall <rse@engelschall.com>. |
31 |
*/ |
32 |
|
33 |
#include "coro.h" |
34 |
|
35 |
#if !defined(STACK_ADJUST_PTR) |
36 |
/* IRIX is decidedly NON-unix */ |
37 |
# if __sgi |
38 |
# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) |
39 |
# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) |
40 |
# elif __i386__ && CORO_LINUX |
41 |
# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss)) |
42 |
# define STACK_ADJUST_SIZE(sp,ss) (ss) |
43 |
# elif __amd64__ && CORO_LINUX |
44 |
# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) |
45 |
# define STACK_ADJUST_SIZE(sp,ss) (ss) |
46 |
# else |
47 |
# define STACK_ADJUST_PTR(sp,ss) (sp) |
48 |
# define STACK_ADJUST_SIZE(sp,ss) (ss) |
49 |
# endif |
50 |
#endif |
51 |
|
52 |
#if CORO_UCONTEXT |
53 |
# include <stddef.h> |
54 |
#endif |
55 |
|
56 |
#if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
57 |
|
58 |
#include <stdlib.h> |
59 |
|
60 |
#if CORO_SJLJ |
61 |
# include <stdio.h> |
62 |
# include <signal.h> |
63 |
# include <unistd.h> |
64 |
#endif |
65 |
|
66 |
static volatile coro_func coro_init_func; |
67 |
static volatile void *coro_init_arg; |
68 |
static volatile coro_context *new_coro, *create_coro; |
69 |
|
70 |
static void |
71 |
coro_init (void) |
72 |
{ |
73 |
volatile coro_func func = coro_init_func; |
74 |
volatile void *arg = coro_init_arg; |
75 |
|
76 |
coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro); |
77 |
|
78 |
func ((void *)arg); |
79 |
|
80 |
/* the new coro returned. bad. just abort() for now */ |
81 |
abort (); |
82 |
} |
83 |
|
84 |
# if CORO_SJLJ |
85 |
|
86 |
static volatile int trampoline_count; |
87 |
|
88 |
/* trampoline signal handler */ |
89 |
static void |
90 |
trampoline (int sig) |
91 |
{ |
92 |
if (setjmp (((coro_context *)new_coro)->env)) |
93 |
coro_init (); /* start it */ |
94 |
else |
95 |
trampoline_count++; |
96 |
} |
97 |
|
98 |
# endif |
99 |
|
100 |
#endif |
101 |
|
102 |
/* initialize a machine state */ |
103 |
void coro_create(coro_context *ctx, |
104 |
coro_func coro, void *arg, |
105 |
void *sptr, long ssize) |
106 |
{ |
107 |
#if CORO_UCONTEXT |
108 |
|
109 |
getcontext (&(ctx->uc)); |
110 |
|
111 |
ctx->uc.uc_link = 0; |
112 |
ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize); |
113 |
ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize); |
114 |
ctx->uc.uc_stack.ss_flags = 0; |
115 |
|
116 |
makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
117 |
|
118 |
#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
119 |
|
120 |
# if CORO_SJLJ |
121 |
stack_t ostk, nstk; |
122 |
struct sigaction osa, nsa; |
123 |
sigset_t nsig, osig; |
124 |
# endif |
125 |
coro_context nctx; |
126 |
|
127 |
coro_init_func = coro; |
128 |
coro_init_arg = arg; |
129 |
|
130 |
new_coro = ctx; |
131 |
create_coro = &nctx; |
132 |
|
133 |
# if CORO_SJLJ |
134 |
/* we use SIGUSR2. first block it, then fiddle with it. */ |
135 |
|
136 |
sigemptyset (&nsig); |
137 |
sigaddset (&nsig, SIGUSR2); |
138 |
sigprocmask (SIG_BLOCK, &nsig, &osig); |
139 |
|
140 |
nsa.sa_handler = trampoline; |
141 |
sigemptyset (&nsa.sa_mask); |
142 |
nsa.sa_flags = SA_ONSTACK; |
143 |
|
144 |
if (sigaction (SIGUSR2, &nsa, &osa)) |
145 |
perror ("sigaction"); |
146 |
|
147 |
/* set the new stack */ |
148 |
nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
149 |
nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
150 |
nstk.ss_flags = 0; |
151 |
|
152 |
if (sigaltstack (&nstk, &ostk) < 0) |
153 |
perror ("sigaltstack"); |
154 |
|
155 |
trampoline_count = 0; |
156 |
kill (getpid (), SIGUSR2); |
157 |
sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
158 |
|
159 |
while (!trampoline_count) |
160 |
sigsuspend (&nsig); |
161 |
|
162 |
sigaltstack (0, &nstk); |
163 |
nstk.ss_flags = SS_DISABLE; |
164 |
if (sigaltstack (&nstk, 0) < 0) |
165 |
perror ("sigaltstack"); |
166 |
|
167 |
sigaltstack (0, &nstk); |
168 |
if (~nstk.ss_flags & SS_DISABLE) |
169 |
abort (); |
170 |
|
171 |
if (~ostk.ss_flags & SS_DISABLE) |
172 |
sigaltstack (&ostk, 0); |
173 |
|
174 |
sigaction (SIGUSR2, &osa, 0); |
175 |
|
176 |
sigprocmask (SIG_SETMASK, &osig, 0); |
177 |
|
178 |
# elif CORO_LOSER |
179 |
|
180 |
setjmp (ctx->env); |
181 |
#if __CYGWIN__ |
182 |
ctx->env[7] = (long)((char *)sptr + ssize); |
183 |
ctx->env[8] = (long)coro_init; |
184 |
#elif defined(_M_IX86) |
185 |
((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
186 |
((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr,ssize); |
187 |
#elif defined(_M_AMD64) |
188 |
((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
189 |
((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr,ssize); |
190 |
#elif defined(_M_IA64) |
191 |
((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; |
192 |
((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize); |
193 |
#else |
194 |
#error "microsoft libc or architecture not supported" |
195 |
#endif |
196 |
|
197 |
# elif CORO_LINUX |
198 |
|
199 |
_setjmp (ctx->env); |
200 |
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
201 |
&& __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
202 |
ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
203 |
ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
204 |
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
205 |
&& __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
206 |
ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
207 |
ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
208 |
#elif defined(__GNU_LIBRARY__) && defined(__i386__) |
209 |
ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
210 |
ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
211 |
#elif defined(__GNU_LIBRARY__) && defined(__amd64__) |
212 |
ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
213 |
ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
214 |
#else |
215 |
#error "linux libc or architecture not supported" |
216 |
#endif |
217 |
|
218 |
# elif CORO_IRIX |
219 |
|
220 |
setjmp (ctx->env); |
221 |
ctx->env[JB_PC] = (__uint64_t)coro_init; |
222 |
ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize); |
223 |
|
224 |
# endif |
225 |
|
226 |
coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
227 |
|
228 |
#else |
229 |
error unsupported architecture |
230 |
#endif |
231 |
} |
232 |
|