… | |
… | |
65 | |
65 | |
66 | static volatile coro_func coro_init_func; |
66 | static volatile coro_func coro_init_func; |
67 | static volatile void *coro_init_arg; |
67 | static volatile void *coro_init_arg; |
68 | static volatile coro_context *new_coro, *create_coro; |
68 | static volatile coro_context *new_coro, *create_coro; |
69 | |
69 | |
|
|
70 | /* what we really want to detect here is wether we use a new-enough version of GAS */ |
|
|
71 | /* instead, check for gcc 3 and ELF and hope for the best */ |
|
|
72 | #if __GNUC__ >= 3 && __ELF__ |
|
|
73 | # define HAVE_CFI 1 |
|
|
74 | #endif |
|
|
75 | |
70 | static void |
76 | static void |
71 | coro_init (void) |
77 | coro_init (void) |
72 | { |
78 | { |
73 | volatile coro_func func = coro_init_func; |
79 | volatile coro_func func = coro_init_func; |
74 | volatile void *arg = coro_init_arg; |
80 | volatile void *arg = coro_init_arg; |
… | |
… | |
88 | /* trampoline signal handler */ |
94 | /* trampoline signal handler */ |
89 | static void |
95 | static void |
90 | trampoline (int sig) |
96 | trampoline (int sig) |
91 | { |
97 | { |
92 | if (setjmp (((coro_context *)new_coro)->env)) |
98 | if (setjmp (((coro_context *)new_coro)->env)) |
|
|
99 | { |
|
|
100 | #if HAVE_CFI |
|
|
101 | asm (".cfi_startproc"); |
|
|
102 | #endif |
93 | coro_init (); /* start it */ |
103 | coro_init (); /* start it */ |
|
|
104 | #if HAVE_CFI |
|
|
105 | asm (".cfi_endproc"); |
|
|
106 | #endif |
|
|
107 | } |
94 | else |
108 | else |
95 | trampoline_count++; |
109 | trampoline_count++; |
96 | } |
110 | } |
97 | |
111 | |
98 | # endif |
112 | # endif |
99 | |
113 | |
100 | #endif |
114 | #endif |
101 | |
115 | |
102 | /* initialize a machine state */ |
116 | /* initialize a machine state */ |
103 | void coro_create(coro_context *ctx, |
117 | void coro_create (coro_context *ctx, |
104 | coro_func coro, void *arg, |
118 | coro_func coro, void *arg, |
105 | void *sptr, long ssize) |
119 | void *sptr, long ssize) |
106 | { |
120 | { |
107 | #if CORO_UCONTEXT |
121 | #if CORO_UCONTEXT |
108 | |
122 | |
109 | getcontext (&(ctx->uc)); |
123 | getcontext (&(ctx->uc)); |
110 | |
124 | |
… | |
… | |
140 | nsa.sa_handler = trampoline; |
154 | nsa.sa_handler = trampoline; |
141 | sigemptyset (&nsa.sa_mask); |
155 | sigemptyset (&nsa.sa_mask); |
142 | nsa.sa_flags = SA_ONSTACK; |
156 | nsa.sa_flags = SA_ONSTACK; |
143 | |
157 | |
144 | if (sigaction (SIGUSR2, &nsa, &osa)) |
158 | if (sigaction (SIGUSR2, &nsa, &osa)) |
|
|
159 | { |
145 | perror ("sigaction"); |
160 | perror ("sigaction"); |
|
|
161 | abort (); |
|
|
162 | } |
146 | |
163 | |
147 | /* set the new stack */ |
164 | /* set the new stack */ |
148 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
165 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
149 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
166 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
150 | nstk.ss_flags = 0; |
167 | nstk.ss_flags = 0; |
151 | |
168 | |
152 | if (sigaltstack (&nstk, &ostk) < 0) |
169 | if (sigaltstack (&nstk, &ostk) < 0) |
|
|
170 | { |
153 | perror ("sigaltstack"); |
171 | perror ("sigaltstack"); |
|
|
172 | abort (); |
|
|
173 | } |
154 | |
174 | |
155 | trampoline_count = 0; |
175 | trampoline_count = 0; |
156 | kill (getpid (), SIGUSR2); |
176 | kill (getpid (), SIGUSR2); |
157 | sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
177 | sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); |
158 | |
178 | |
… | |
… | |
176 | sigprocmask (SIG_SETMASK, &osig, 0); |
196 | sigprocmask (SIG_SETMASK, &osig, 0); |
177 | |
197 | |
178 | # elif CORO_LOSER |
198 | # elif CORO_LOSER |
179 | |
199 | |
180 | setjmp (ctx->env); |
200 | setjmp (ctx->env); |
|
|
201 | #if __CYGWIN__ |
181 | ctx->env[7] = (long)((char *)sptr + ssize); |
202 | ctx->env[7] = (long)((char *)sptr + ssize); |
182 | ctx->env[8] = (long)coro_init; |
203 | ctx->env[8] = (long)coro_init; |
|
|
204 | #elif defined(_M_IX86) |
|
|
205 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
|
|
206 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr,ssize); |
|
|
207 | #elif defined(_M_AMD64) |
|
|
208 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
|
|
209 | ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr,ssize); |
|
|
210 | #elif defined(_M_IA64) |
|
|
211 | ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; |
|
|
212 | ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize); |
|
|
213 | #else |
|
|
214 | #error "microsoft libc or architecture not supported" |
|
|
215 | #endif |
183 | |
216 | |
184 | # elif CORO_LINUX |
217 | # elif CORO_LINUX |
185 | |
218 | |
186 | setjmp (ctx->env); |
219 | _setjmp (ctx->env); |
187 | #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
|
|
188 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
220 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
189 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
221 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
190 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
222 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize); |
191 | #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
|
|
192 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
223 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
193 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
224 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
194 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
225 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
195 | #elif defined(__GNU_LIBRARY__) && defined(__i386__) |
226 | #elif defined (__GNU_LIBRARY__) && defined (__i386__) |
196 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
227 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
197 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
228 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
198 | #elif defined(__GNU_LIBRARY__) && defined(__amd64__) |
229 | #elif defined (__GNU_LIBRARY__) && defined (__amd64__) |
199 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
230 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
200 | ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
231 | ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr, ssize); |
201 | #else |
232 | #else |
202 | #error "linux libc or architecture not supported" |
233 | #error "linux libc or architecture not supported" |
203 | #endif |
234 | #endif |
204 | |
235 | |
205 | # elif CORO_IRIX |
236 | # elif CORO_IRIX |
206 | |
237 | |
207 | setjmp (ctx->env); |
238 | setjmp (ctx->env); |
208 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
239 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
209 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize); |
240 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize); |
210 | |
241 | |
211 | # endif |
242 | # endif |
212 | |
243 | |
213 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
244 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
214 | |
245 | |