1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2008 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2001-2009 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, |
… | |
… | |
100 | |
100 | |
101 | /* trampoline signal handler */ |
101 | /* trampoline signal handler */ |
102 | static void |
102 | static void |
103 | trampoline (int sig) |
103 | trampoline (int sig) |
104 | { |
104 | { |
105 | if ( |
|
|
106 | #if _XOPEN_UNIX > 0 |
|
|
107 | _setjmp (new_coro->env) |
105 | if (coro_setjmp (new_coro->env)) |
108 | #else |
|
|
109 | sigsetjmp (new_coro->env, 0) |
|
|
110 | #endif |
|
|
111 | ) { |
|
|
112 | coro_init (); /* start it */ |
106 | coro_init (); /* start it */ |
113 | } |
|
|
114 | else |
107 | else |
115 | trampoline_done = 1; |
108 | trampoline_done = 1; |
116 | } |
109 | } |
117 | |
110 | |
118 | # endif |
111 | # endif |
… | |
… | |
122 | asm ( |
115 | asm ( |
123 | ".text\n" |
116 | ".text\n" |
124 | ".globl coro_transfer\n" |
117 | ".globl coro_transfer\n" |
125 | ".type coro_transfer, @function\n" |
118 | ".type coro_transfer, @function\n" |
126 | "coro_transfer:\n" |
119 | "coro_transfer:\n" |
|
|
120 | /* windows, of course, gives a shit on the amd64 ABI and uses different registers */ |
|
|
121 | /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */ |
127 | #if __amd64 |
122 | #if __amd64 |
128 | #define NUM_SAVED 6 |
123 | #define NUM_SAVED 6 |
129 | "\tpush %rbp\n" |
124 | "\tpush %rbp\n" |
130 | "\tpush %rbx\n" |
125 | "\tpush %rbx\n" |
131 | "\tpush %r12\n" |
126 | "\tpush %r12\n" |
132 | "\tpush %r13\n" |
127 | "\tpush %r13\n" |
133 | "\tpush %r14\n" |
128 | "\tpush %r14\n" |
134 | "\tpush %r15\n" |
129 | "\tpush %r15\n" |
|
|
130 | #if CORO_WIN_TIB |
|
|
131 | "\tpush %gs:0x0\n" |
|
|
132 | "\tpush %gs:0x8\n" |
|
|
133 | "\tpush %gs:0xc\n" |
|
|
134 | #endif |
135 | "\tmov %rsp, (%rdi)\n" |
135 | "\tmov %rsp, (%rdi)\n" |
136 | "\tmov (%rsi), %rsp\n" |
136 | "\tmov (%rsi), %rsp\n" |
|
|
137 | #if CORO_WIN_TIB |
|
|
138 | "\tpop %gs:0xc\n" |
|
|
139 | "\tpop %gs:0x8\n" |
|
|
140 | "\tpop %gs:0x0\n" |
|
|
141 | #endif |
137 | "\tpop %r15\n" |
142 | "\tpop %r15\n" |
138 | "\tpop %r14\n" |
143 | "\tpop %r14\n" |
139 | "\tpop %r13\n" |
144 | "\tpop %r13\n" |
140 | "\tpop %r12\n" |
145 | "\tpop %r12\n" |
141 | "\tpop %rbx\n" |
146 | "\tpop %rbx\n" |
… | |
… | |
144 | #define NUM_SAVED 4 |
149 | #define NUM_SAVED 4 |
145 | "\tpush %ebp\n" |
150 | "\tpush %ebp\n" |
146 | "\tpush %ebx\n" |
151 | "\tpush %ebx\n" |
147 | "\tpush %esi\n" |
152 | "\tpush %esi\n" |
148 | "\tpush %edi\n" |
153 | "\tpush %edi\n" |
|
|
154 | #if CORO_WIN_TIB |
|
|
155 | "\tpush %fs:0\n" |
|
|
156 | "\tpush %fs:4\n" |
|
|
157 | "\tpush %fs:8\n" |
|
|
158 | #endif |
149 | "\tmov %esp, (%eax)\n" |
159 | "\tmov %esp, (%eax)\n" |
150 | "\tmov (%edx), %esp\n" |
160 | "\tmov (%edx), %esp\n" |
|
|
161 | #if CORO_WIN_TIB |
|
|
162 | "\tpop %fs:8\n" |
|
|
163 | "\tpop %fs:4\n" |
|
|
164 | "\tpop %fs:0\n" |
|
|
165 | #endif |
151 | "\tpop %edi\n" |
166 | "\tpop %edi\n" |
152 | "\tpop %esi\n" |
167 | "\tpop %esi\n" |
153 | "\tpop %ebx\n" |
168 | "\tpop %ebx\n" |
154 | "\tpop %ebp\n" |
169 | "\tpop %ebp\n" |
155 | #else |
170 | #else |
… | |
… | |
229 | sigaction (SIGUSR2, &osa, 0); |
244 | sigaction (SIGUSR2, &osa, 0); |
230 | sigprocmask (SIG_SETMASK, &osig, 0); |
245 | sigprocmask (SIG_SETMASK, &osig, 0); |
231 | |
246 | |
232 | # elif CORO_LOSER |
247 | # elif CORO_LOSER |
233 | |
248 | |
234 | setjmp (ctx->env); |
249 | coro_setjmp (ctx->env); |
235 | #if __CYGWIN__ |
250 | #if __CYGWIN__ && __i386 |
|
|
251 | ctx->env[8] = (long) coro_init; |
236 | ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); |
252 | ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long); |
237 | ctx->env[8] = (long)coro_init; |
253 | #elif __CYGWIN__ && __x86_64 |
|
|
254 | ctx->env[7] = (long) coro_init; |
|
|
255 | ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long); |
|
|
256 | #elif defined(__MINGW32__) |
|
|
257 | ctx->env[5] = (long) coro_init; |
|
|
258 | ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long); |
238 | #elif defined(_M_IX86) |
259 | #elif defined(_M_IX86) |
239 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
260 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init; |
240 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
261 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
241 | #elif defined(_M_AMD64) |
262 | #elif defined(_M_AMD64) |
242 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
263 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init; |
243 | ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
264 | ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64); |
244 | #elif defined(_M_IA64) |
265 | #elif defined(_M_IA64) |
245 | ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; |
266 | ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init; |
246 | ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
267 | ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64); |
247 | #else |
268 | #else |
248 | #error "microsoft libc or architecture not supported" |
269 | #error "microsoft libc or architecture not supported" |
249 | #endif |
270 | #endif |
250 | |
271 | |
251 | # elif CORO_LINUX |
272 | # elif CORO_LINUX |
252 | |
273 | |
253 | _setjmp (ctx->env); |
274 | coro_setjmp (ctx->env); |
254 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
275 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
255 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
276 | ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init; |
256 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
277 | ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
257 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
278 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
258 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
279 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
259 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long); |
280 | ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long); |
260 | #elif defined (__GNU_LIBRARY__) && defined (__i386__) |
281 | #elif defined (__GNU_LIBRARY__) && defined (__i386__) |
261 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
282 | ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init; |
262 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); |
283 | ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); |
263 | #elif defined (__GNU_LIBRARY__) && defined (__amd64__) |
284 | #elif defined (__GNU_LIBRARY__) && defined (__amd64__) |
264 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
285 | ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init; |
265 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); |
286 | ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); |
266 | #else |
287 | #else |
267 | #error "linux libc or architecture not supported" |
288 | #error "linux libc or architecture not supported" |
268 | #endif |
289 | #endif |
269 | |
290 | |
270 | # elif CORO_IRIX |
291 | # elif CORO_IRIX |
271 | |
292 | |
272 | sigsetjmp (ctx->env, 0); |
293 | coro_setjmp (ctx->env, 0); |
273 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
294 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
274 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
295 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
275 | |
296 | |
276 | # elif CORO_ASM |
297 | # elif CORO_ASM |
277 | |
298 | |
278 | ctx->sp = (void **)(ssize + (char *)sptr); |
299 | ctx->sp = (void **)(ssize + (char *)sptr); |
279 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
300 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
280 | *--ctx->sp = (void *)coro_init; |
301 | *--ctx->sp = (void *)coro_init; |
|
|
302 | |
|
|
303 | #if CORO_WIN_TIB |
|
|
304 | *--ctx->sp = 0; /* ExceptionList */ |
|
|
305 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
|
|
306 | *--ctx->sp = sptr; /* StackLimit */ |
|
|
307 | #endif |
|
|
308 | |
281 | ctx->sp -= NUM_SAVED; |
309 | ctx->sp -= NUM_SAVED; |
282 | |
310 | |
283 | # elif CORO_UCONTEXT |
311 | # elif CORO_UCONTEXT |
284 | |
312 | |
285 | getcontext (&(ctx->uc)); |
313 | getcontext (&(ctx->uc)); |
… | |
… | |
374 | args.arg = arg; |
402 | args.arg = arg; |
375 | args.self = ctx; |
403 | args.self = ctx; |
376 | args.main = &nctx; |
404 | args.main = &nctx; |
377 | |
405 | |
378 | pthread_attr_init (&attr); |
406 | pthread_attr_init (&attr); |
|
|
407 | #if __UCLIBC__ |
|
|
408 | pthread_attr_setstacksize (&attr, (size_t)ssize); |
|
|
409 | #else |
379 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
410 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
411 | #endif |
380 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
412 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
381 | pthread_create (&ctx->id, &attr, coro_init, &args); |
413 | pthread_create (&ctx->id, &attr, coro_init, &args); |
382 | |
414 | |
383 | coro_transfer (args.main, args.self); |
415 | coro_transfer (args.main, args.self); |
384 | } |
416 | } |