1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2008 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2001-2011 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
… | |
… | |
78 | |
78 | |
79 | static coro_func coro_init_func; |
79 | static coro_func coro_init_func; |
80 | static void *coro_init_arg; |
80 | static void *coro_init_arg; |
81 | static coro_context *new_coro, *create_coro; |
81 | static coro_context *new_coro, *create_coro; |
82 | |
82 | |
83 | /* what we really want to detect here is wether we use a new-enough version of GAS */ |
|
|
84 | /* with dwarf debug info. instead, check for gcc 3, ELF and GNU/Linux and hope for the best */ |
|
|
85 | # if __GNUC__ >= 3 && __ELF__ && __linux__ |
|
|
86 | # define HAVE_CFI 1 |
|
|
87 | # endif |
|
|
88 | |
|
|
89 | static void |
83 | static void |
90 | coro_init (void) |
84 | coro_init (void) |
91 | { |
85 | { |
92 | volatile coro_func func = coro_init_func; |
86 | volatile coro_func func = coro_init_func; |
93 | volatile void *arg = coro_init_arg; |
87 | volatile void *arg = coro_init_arg; |
94 | |
88 | |
95 | coro_transfer (new_coro, create_coro); |
89 | coro_transfer (new_coro, create_coro); |
|
|
90 | |
|
|
91 | #if __linux && __amd64 |
|
|
92 | /* we blindly assume on any __linux with __amd64 we have a new enough gas with .cfi_undefined support */ |
|
|
93 | asm (".cfi_undefined rip"); |
|
|
94 | #endif |
96 | |
95 | |
97 | func ((void *)arg); |
96 | func ((void *)arg); |
98 | |
97 | |
99 | /* the new coro returned. bad. just abort() for now */ |
98 | /* the new coro returned. bad. just abort() for now */ |
100 | abort (); |
99 | abort (); |
… | |
… | |
106 | |
105 | |
107 | /* trampoline signal handler */ |
106 | /* trampoline signal handler */ |
108 | static void |
107 | static void |
109 | trampoline (int sig) |
108 | trampoline (int sig) |
110 | { |
109 | { |
111 | if ( |
|
|
112 | #if _XOPEN_UNIX > 0 |
|
|
113 | _setjmp (new_coro->env) |
110 | if (coro_setjmp (new_coro->env)) |
114 | #else |
|
|
115 | sigsetjmp (new_coro->env, 0) |
|
|
116 | #endif |
|
|
117 | ) { |
|
|
118 | #if HAVE_CFI |
|
|
119 | asm (".cfi_startproc"); |
|
|
120 | #endif |
|
|
121 | coro_init (); /* start it */ |
111 | coro_init (); /* start it */ |
122 | #if HAVE_CFI |
|
|
123 | asm (".cfi_endproc"); |
|
|
124 | #endif |
|
|
125 | } |
|
|
126 | else |
112 | else |
127 | trampoline_done = 1; |
113 | trampoline_done = 1; |
128 | } |
114 | } |
129 | |
115 | |
130 | # endif |
116 | # endif |
… | |
… | |
134 | asm ( |
120 | asm ( |
135 | ".text\n" |
121 | ".text\n" |
136 | ".globl coro_transfer\n" |
122 | ".globl coro_transfer\n" |
137 | ".type coro_transfer, @function\n" |
123 | ".type coro_transfer, @function\n" |
138 | "coro_transfer:\n" |
124 | "coro_transfer:\n" |
|
|
125 | /* windows, of course, gives a shit on the amd64 ABI and uses different registers */ |
|
|
126 | /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */ |
139 | #if __amd64 |
127 | #if __amd64 |
|
|
128 | #ifdef _WIN32 |
|
|
129 | /* TODO: xmm6..15 also would need to be saved. sigh. */ |
|
|
130 | #define NUM_SAVED 8 |
|
|
131 | #undef CORO_WIN_TIB |
|
|
132 | "\tpushq %rsi\n" |
|
|
133 | "\tpushq %rdi\n" |
|
|
134 | "\tpushq %rbp\n" |
|
|
135 | "\tpushq %rbx\n" |
|
|
136 | "\tpushq %r12\n" |
|
|
137 | "\tpushq %r13\n" |
|
|
138 | "\tpushq %r14\n" |
|
|
139 | "\tpushq %r15\n" |
|
|
140 | "\tmovq %rsp, (%rcx)\n" |
|
|
141 | "\tmovq (%rdx), %rsp\n" |
|
|
142 | "\tpopq %r15\n" |
|
|
143 | "\tpopq %r14\n" |
|
|
144 | "\tpopq %r13\n" |
|
|
145 | "\tpopq %r12\n" |
|
|
146 | "\tpopq %rbx\n" |
|
|
147 | "\tpopq %rbp\n" |
|
|
148 | "\tpopq %rdi\n" |
|
|
149 | "\tpopq %rsi\n" |
|
|
150 | #else |
140 | #define NUM_SAVED 6 |
151 | #define NUM_SAVED 6 |
141 | "\tpush %rbp\n" |
152 | "\tpushq %rbp\n" |
142 | "\tpush %rbx\n" |
153 | "\tpushq %rbx\n" |
143 | "\tpush %r12\n" |
154 | "\tpushq %r12\n" |
144 | "\tpush %r13\n" |
155 | "\tpushq %r13\n" |
145 | "\tpush %r14\n" |
156 | "\tpushq %r14\n" |
146 | "\tpush %r15\n" |
157 | "\tpushq %r15\n" |
147 | "\tmov %rsp, (%rdi)\n" |
158 | "\tmovq %rsp, (%rdi)\n" |
148 | "\tmov (%rsi), %rsp\n" |
159 | "\tmovq (%rsi), %rsp\n" |
149 | "\tpop %r15\n" |
160 | "\tpopq %r15\n" |
150 | "\tpop %r14\n" |
161 | "\tpopq %r14\n" |
151 | "\tpop %r13\n" |
162 | "\tpopq %r13\n" |
152 | "\tpop %r12\n" |
163 | "\tpopq %r12\n" |
153 | "\tpop %rbx\n" |
164 | "\tpopq %rbx\n" |
154 | "\tpop %rbp\n" |
165 | "\tpopq %rbp\n" |
|
|
166 | #endif |
155 | #elif __i386 |
167 | #elif __i386 |
156 | #define NUM_SAVED 4 |
168 | #define NUM_SAVED 4 |
157 | "\tpush %ebp\n" |
169 | "\tpushl %ebp\n" |
158 | "\tpush %ebx\n" |
170 | "\tpushl %ebx\n" |
159 | "\tpush %esi\n" |
171 | "\tpushl %esi\n" |
160 | "\tpush %edi\n" |
172 | "\tpushl %edi\n" |
|
|
173 | #if CORO_WIN_TIB |
|
|
174 | "\tpushl %fs:0\n" |
|
|
175 | "\tpushl %fs:4\n" |
|
|
176 | "\tpushl %fs:8\n" |
|
|
177 | #endif |
161 | "\tmov %esp, (%eax)\n" |
178 | "\tmovl %esp, (%eax)\n" |
162 | "\tmov (%edx), %esp\n" |
179 | "\tmovl (%edx), %esp\n" |
|
|
180 | #if CORO_WIN_TIB |
|
|
181 | "\tpopl %fs:8\n" |
|
|
182 | "\tpopl %fs:4\n" |
|
|
183 | "\tpopl %fs:0\n" |
|
|
184 | #endif |
163 | "\tpop %edi\n" |
185 | "\tpopl %edi\n" |
164 | "\tpop %esi\n" |
186 | "\tpopl %esi\n" |
165 | "\tpop %ebx\n" |
187 | "\tpopl %ebx\n" |
166 | "\tpop %ebp\n" |
188 | "\tpopl %ebp\n" |
167 | #else |
189 | #else |
168 | #error unsupported architecture |
190 | #error unsupported architecture |
169 | #endif |
191 | #endif |
170 | "\tret\n" |
192 | "\tret\n" |
171 | ); |
193 | ); |
… | |
… | |
207 | perror ("sigaction"); |
229 | perror ("sigaction"); |
208 | abort (); |
230 | abort (); |
209 | } |
231 | } |
210 | |
232 | |
211 | /* set the new stack */ |
233 | /* set the new stack */ |
212 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
234 | nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */ |
213 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
235 | nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize); |
214 | nstk.ss_flags = 0; |
236 | nstk.ss_flags = 0; |
215 | |
237 | |
216 | if (sigaltstack (&nstk, &ostk) < 0) |
238 | if (sigaltstack (&nstk, &ostk) < 0) |
217 | { |
239 | { |
218 | perror ("sigaltstack"); |
240 | perror ("sigaltstack"); |
… | |
… | |
241 | sigaction (SIGUSR2, &osa, 0); |
263 | sigaction (SIGUSR2, &osa, 0); |
242 | sigprocmask (SIG_SETMASK, &osig, 0); |
264 | sigprocmask (SIG_SETMASK, &osig, 0); |
243 | |
265 | |
244 | # elif CORO_LOSER |
266 | # elif CORO_LOSER |
245 | |
267 | |
246 | setjmp (ctx->env); |
268 | coro_setjmp (ctx->env); |
247 | #if __CYGWIN__ |
269 | #if __CYGWIN__ && __i386 |
|
|
270 | ctx->env[8] = (long) coro_init; |
248 | ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); |
271 | ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long); |
249 | ctx->env[8] = (long)coro_init; |
272 | #elif __CYGWIN__ && __x86_64 |
|
|
273 | ctx->env[7] = (long) coro_init; |
|
|
274 | ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long); |
|
|
275 | #elif defined(__MINGW32__) |
|
|
276 | ctx->env[5] = (long) coro_init; |
|
|
277 | ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long); |
250 | #elif defined(_M_IX86) |
278 | #elif defined(_M_IX86) |
251 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
279 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init; |
252 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
280 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
253 | #elif defined(_M_AMD64) |
281 | #elif defined(_M_AMD64) |
254 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
282 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init; |
255 | ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
283 | ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64); |
256 | #elif defined(_M_IA64) |
284 | #elif defined(_M_IA64) |
257 | ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; |
285 | ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init; |
258 | ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
286 | ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64); |
259 | #else |
287 | #else |
260 | #error "microsoft libc or architecture not supported" |
288 | #error "microsoft libc or architecture not supported" |
261 | #endif |
289 | #endif |
262 | |
290 | |
263 | # elif CORO_LINUX |
291 | # elif CORO_LINUX |
264 | |
292 | |
265 | _setjmp (ctx->env); |
293 | coro_setjmp (ctx->env); |
266 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
294 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
267 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
295 | ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init; |
268 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
296 | ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
269 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
297 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
270 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
298 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
271 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long); |
299 | ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long); |
272 | #elif defined (__GNU_LIBRARY__) && defined (__i386__) |
300 | #elif defined (__GNU_LIBRARY__) && defined (__i386__) |
273 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
301 | ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init; |
274 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); |
302 | ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); |
275 | #elif defined (__GNU_LIBRARY__) && defined (__amd64__) |
303 | #elif defined (__GNU_LIBRARY__) && defined (__amd64__) |
276 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
304 | ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init; |
277 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); |
305 | ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); |
278 | #else |
306 | #else |
279 | #error "linux libc or architecture not supported" |
307 | #error "linux libc or architecture not supported" |
280 | #endif |
308 | #endif |
281 | |
309 | |
282 | # elif CORO_IRIX |
310 | # elif CORO_IRIX |
283 | |
311 | |
284 | sigsetjmp (ctx->env, 0); |
312 | coro_setjmp (ctx->env, 0); |
285 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
313 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
286 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
314 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
287 | |
315 | |
288 | # elif CORO_ASM |
316 | # elif CORO_ASM |
289 | |
317 | |
290 | ctx->sp = (void **)(ssize + (char *)sptr); |
318 | ctx->sp = (void **)(ssize + (char *)sptr); |
291 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
319 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
292 | *--ctx->sp = (void *)coro_init; |
320 | *--ctx->sp = (void *)coro_init; |
|
|
321 | |
|
|
322 | #if CORO_WIN_TIB |
|
|
323 | *--ctx->sp = 0; /* ExceptionList */ |
|
|
324 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
|
|
325 | *--ctx->sp = sptr; /* StackLimit */ |
|
|
326 | #endif |
|
|
327 | |
293 | ctx->sp -= NUM_SAVED; |
328 | ctx->sp -= NUM_SAVED; |
|
|
329 | memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); |
294 | |
330 | |
295 | # elif CORO_UCONTEXT |
331 | # elif CORO_UCONTEXT |
296 | |
332 | |
297 | getcontext (&(ctx->uc)); |
333 | getcontext (&(ctx->uc)); |
298 | |
334 | |
… | |
… | |
353 | void |
389 | void |
354 | coro_transfer (coro_context *prev, coro_context *next) |
390 | coro_transfer (coro_context *prev, coro_context *next) |
355 | { |
391 | { |
356 | pthread_cond_signal (&next->cv); |
392 | pthread_cond_signal (&next->cv); |
357 | pthread_cond_wait (&prev->cv, &coro_mutex); |
393 | pthread_cond_wait (&prev->cv, &coro_mutex); |
|
|
394 | #if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */ |
|
|
395 | pthread_testcancel (); |
|
|
396 | #endif |
358 | } |
397 | } |
359 | |
398 | |
360 | void |
399 | void |
361 | coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) |
400 | coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) |
362 | { |
401 | { |
… | |
… | |
383 | args.arg = arg; |
422 | args.arg = arg; |
384 | args.self = ctx; |
423 | args.self = ctx; |
385 | args.main = &nctx; |
424 | args.main = &nctx; |
386 | |
425 | |
387 | pthread_attr_init (&attr); |
426 | pthread_attr_init (&attr); |
|
|
427 | #if __UCLIBC__ |
|
|
428 | /* exists, but is borked */ |
|
|
429 | /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/ |
|
|
430 | #else |
388 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
431 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
432 | #endif |
389 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
433 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
390 | pthread_create (&ctx->id, &attr, coro_init, &args); |
434 | pthread_create (&ctx->id, &attr, coro_init, &args); |
391 | |
435 | |
392 | coro_transfer (args.main, args.self); |
436 | coro_transfer (args.main, args.self); |
393 | } |
437 | } |