1 |
/* |
2 |
* Copyright (c) 2001-2005 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_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
53 |
|
54 |
#include <stdlib.h> |
55 |
|
56 |
#if CORO_SJLJ |
57 |
# include <stdio.h> |
58 |
# include <signal.h> |
59 |
# include <unistd.h> |
60 |
#endif |
61 |
|
62 |
static volatile coro_func coro_init_func; |
63 |
static volatile void *coro_init_arg; |
64 |
static volatile coro_context *new_coro, *create_coro; |
65 |
|
66 |
static void |
67 |
coro_init (void) |
68 |
{ |
69 |
volatile coro_func func = coro_init_func; |
70 |
volatile void *arg = coro_init_arg; |
71 |
|
72 |
coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro); |
73 |
|
74 |
func ((void *)arg); |
75 |
|
76 |
/* the new coro returned. bad. just abort() for now */ |
77 |
abort (); |
78 |
} |
79 |
|
80 |
# if CORO_SJLJ |
81 |
|
82 |
static volatile int trampoline_count; |
83 |
|
84 |
/* trampoline signal handler */ |
85 |
static void |
86 |
trampoline (int sig) |
87 |
{ |
88 |
if (setjmp (((coro_context *)new_coro)->env)) |
89 |
coro_init (); /* start it */ |
90 |
else |
91 |
trampoline_count++; |
92 |
} |
93 |
|
94 |
# endif |
95 |
|
96 |
#endif |
97 |
|
98 |
/* initialize a machine state */ |
99 |
void coro_create(coro_context *ctx, |
100 |
coro_func coro, void *arg, |
101 |
void *sptr, long ssize) |
102 |
{ |
103 |
#if CORO_UCONTEXT |
104 |
|
105 |
getcontext (&(ctx->uc)); |
106 |
|
107 |
ctx->uc.uc_link = 0; |
108 |
ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize); |
109 |
ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize); |
110 |
ctx->uc.uc_stack.ss_flags = 0; |
111 |
|
112 |
makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
113 |
|
114 |
#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
115 |
|
116 |
# if CORO_SJLJ |
117 |
stack_t ostk, nstk; |
118 |
struct sigaction osa, nsa; |
119 |
sigset_t nsig, osig; |
120 |
# endif |
121 |
coro_context nctx; |
122 |
|
123 |
coro_init_func = coro; |
124 |
coro_init_arg = arg; |
125 |
|
126 |
new_coro = ctx; |
127 |
create_coro = &nctx; |
128 |
|
129 |
# if CORO_SJLJ |
130 |
/* we use SIGUSR2. first block it, then fiddle with it. */ |
131 |
|
132 |
sigemptyset (&nsig); |
133 |
sigaddset (&nsig, SIGUSR2); |
134 |
sigprocmask (SIG_BLOCK, &nsig, &osig); |
135 |
|
136 |
nsa.sa_handler = trampoline; |
137 |
sigemptyset (&nsa.sa_mask); |
138 |
nsa.sa_flags = SA_ONSTACK; |
139 |
|
140 |
if (sigaction (SIGUSR2, &nsa, &osa)) |
141 |
perror ("sigaction"); |
142 |
|
143 |
/* set the new stack */ |
144 |
nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
145 |
nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
146 |
nstk.ss_flags = 0; |
147 |
|
148 |
if (sigaltstack (&nstk, &ostk) < 0) |
149 |
perror ("sigaltstack"); |
150 |
|
151 |
trampoline_count = 0; |
152 |
kill (getpid (), SIGUSR2); |
153 |
sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
154 |
|
155 |
while (!trampoline_count) |
156 |
sigsuspend (&nsig); |
157 |
|
158 |
sigaltstack (0, &nstk); |
159 |
nstk.ss_flags = SS_DISABLE; |
160 |
if (sigaltstack (&nstk, 0) < 0) |
161 |
perror ("sigaltstack"); |
162 |
|
163 |
sigaltstack (0, &nstk); |
164 |
if (~nstk.ss_flags & SS_DISABLE) |
165 |
abort (); |
166 |
|
167 |
if (~ostk.ss_flags & SS_DISABLE) |
168 |
sigaltstack (&ostk, 0); |
169 |
|
170 |
sigaction (SIGUSR2, &osa, 0); |
171 |
|
172 |
sigprocmask (SIG_SETMASK, &osig, 0); |
173 |
|
174 |
# elif CORO_LOSER |
175 |
|
176 |
setjmp (ctx->env); |
177 |
ctx->env[7] = (long)((char *)sptr + ssize); |
178 |
ctx->env[8] = (long)coro_init; |
179 |
|
180 |
# elif CORO_LINUX |
181 |
|
182 |
setjmp (ctx->env); |
183 |
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
184 |
&& __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
185 |
ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
186 |
ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
187 |
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
188 |
&& __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
189 |
ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
190 |
ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
191 |
#elif defined(__GNU_LIBRARY__) && defined(__i386__) |
192 |
ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
193 |
ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
194 |
#elif defined(__GNU_LIBRARY__) && defined(__amd64__) |
195 |
ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
196 |
ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
197 |
#else |
198 |
#error "linux libc or architecture not supported" |
199 |
#endif |
200 |
|
201 |
# elif CORO_IRIX |
202 |
|
203 |
setjmp (ctx->env); |
204 |
ctx->env[JB_PC] = (__uint64_t)coro_init; |
205 |
ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize); |
206 |
|
207 |
# endif |
208 |
|
209 |
coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
210 |
|
211 |
#else |
212 |
error unsupported architecture |
213 |
#endif |
214 |
} |
215 |
|