1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2009 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, |
… | |
… | |
86 | volatile coro_func func = coro_init_func; |
86 | volatile coro_func func = coro_init_func; |
87 | volatile void *arg = coro_init_arg; |
87 | volatile void *arg = coro_init_arg; |
88 | |
88 | |
89 | coro_transfer (new_coro, create_coro); |
89 | coro_transfer (new_coro, create_coro); |
90 | |
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 |
|
|
95 | |
91 | func ((void *)arg); |
96 | func ((void *)arg); |
92 | |
97 | |
93 | /* the new coro returned. bad. just abort() for now */ |
98 | /* the new coro returned. bad. just abort() for now */ |
94 | abort (); |
99 | abort (); |
95 | } |
100 | } |
… | |
… | |
115 | asm ( |
120 | asm ( |
116 | ".text\n" |
121 | ".text\n" |
117 | ".globl coro_transfer\n" |
122 | ".globl coro_transfer\n" |
118 | ".type coro_transfer, @function\n" |
123 | ".type coro_transfer, @function\n" |
119 | "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 */ |
120 | #if __amd64 |
127 | #if __amd64 |
|
|
128 | #ifdef _WIN32 |
|
|
129 | #define NUM_SAVED 8 |
|
|
130 | "\tpush %rsi\n" |
|
|
131 | "\tpush %rdi\n" |
|
|
132 | "\tpush %rbp\n" |
|
|
133 | "\tpush %rbx\n" |
|
|
134 | "\tpush %r12\n" |
|
|
135 | "\tpush %r13\n" |
|
|
136 | "\tpush %r14\n" |
|
|
137 | "\tpush %r15\n" |
|
|
138 | #if CORO_WIN_TIB |
|
|
139 | "\tpush %gs:0x0\n" |
|
|
140 | "\tpush %gs:0x8\n" |
|
|
141 | "\tpush %gs:0xc\n" |
|
|
142 | #endif |
|
|
143 | "\tmov %rsp, (%rcx)\n" |
|
|
144 | "\tmov (%rdx), %rsp\n" |
|
|
145 | #if CORO_WIN_TIB |
|
|
146 | "\tpop %gs:0xc\n" |
|
|
147 | "\tpop %gs:0x8\n" |
|
|
148 | "\tpop %gs:0x0\n" |
|
|
149 | #endif |
|
|
150 | "\tpop %r15\n" |
|
|
151 | "\tpop %r14\n" |
|
|
152 | "\tpop %r13\n" |
|
|
153 | "\tpop %r12\n" |
|
|
154 | "\tpop %rbx\n" |
|
|
155 | "\tpop %rbp\n" |
|
|
156 | "\tpop %rdi\n" |
|
|
157 | "\tpop %rsi\n" |
|
|
158 | #else |
121 | #define NUM_SAVED 6 |
159 | #define NUM_SAVED 6 |
122 | "\tpush %rbp\n" |
160 | "\tpush %rbp\n" |
123 | "\tpush %rbx\n" |
161 | "\tpush %rbx\n" |
124 | "\tpush %r12\n" |
162 | "\tpush %r12\n" |
125 | "\tpush %r13\n" |
163 | "\tpush %r13\n" |
126 | "\tpush %r14\n" |
164 | "\tpush %r14\n" |
127 | "\tpush %r15\n" |
165 | "\tpush %r15\n" |
128 | "\tmov %rsp, (%rdi)\n" |
166 | "\tmov %rsp, (%rdi)\n" |
129 | "\tmov (%rsi), %rsp\n" |
167 | "\tmov (%rsi), %rsp\n" |
130 | "\tpop %r15\n" |
168 | "\tpop %r15\n" |
131 | "\tpop %r14\n" |
169 | "\tpop %r14\n" |
132 | "\tpop %r13\n" |
170 | "\tpop %r13\n" |
133 | "\tpop %r12\n" |
171 | "\tpop %r12\n" |
134 | "\tpop %rbx\n" |
172 | "\tpop %rbx\n" |
135 | "\tpop %rbp\n" |
173 | "\tpop %rbp\n" |
|
|
174 | #endif |
136 | #elif __i386 |
175 | #elif __i386 |
137 | #define NUM_SAVED 4 |
176 | #define NUM_SAVED 4 |
138 | "\tpush %ebp\n" |
177 | "\tpush %ebp\n" |
139 | "\tpush %ebx\n" |
178 | "\tpush %ebx\n" |
140 | "\tpush %esi\n" |
179 | "\tpush %esi\n" |
141 | "\tpush %edi\n" |
180 | "\tpush %edi\n" |
|
|
181 | #if CORO_WIN_TIB |
|
|
182 | "\tpush %fs:0\n" |
|
|
183 | "\tpush %fs:4\n" |
|
|
184 | "\tpush %fs:8\n" |
|
|
185 | #endif |
142 | "\tmov %esp, (%eax)\n" |
186 | "\tmov %esp, (%eax)\n" |
143 | "\tmov (%edx), %esp\n" |
187 | "\tmov (%edx), %esp\n" |
|
|
188 | #if CORO_WIN_TIB |
|
|
189 | "\tpop %fs:8\n" |
|
|
190 | "\tpop %fs:4\n" |
|
|
191 | "\tpop %fs:0\n" |
|
|
192 | #endif |
144 | "\tpop %edi\n" |
193 | "\tpop %edi\n" |
145 | "\tpop %esi\n" |
194 | "\tpop %esi\n" |
146 | "\tpop %ebx\n" |
195 | "\tpop %ebx\n" |
147 | "\tpop %ebp\n" |
196 | "\tpop %ebp\n" |
148 | #else |
197 | #else |
149 | #error unsupported architecture |
198 | #error unsupported architecture |
150 | #endif |
199 | #endif |
151 | "\tret\n" |
200 | "\tret\n" |
152 | ); |
201 | ); |
… | |
… | |
188 | perror ("sigaction"); |
237 | perror ("sigaction"); |
189 | abort (); |
238 | abort (); |
190 | } |
239 | } |
191 | |
240 | |
192 | /* set the new stack */ |
241 | /* set the new stack */ |
193 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
242 | nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */ |
194 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
243 | nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize); |
195 | nstk.ss_flags = 0; |
244 | nstk.ss_flags = 0; |
196 | |
245 | |
197 | if (sigaltstack (&nstk, &ostk) < 0) |
246 | if (sigaltstack (&nstk, &ostk) < 0) |
198 | { |
247 | { |
199 | perror ("sigaltstack"); |
248 | perror ("sigaltstack"); |
… | |
… | |
275 | # elif CORO_ASM |
324 | # elif CORO_ASM |
276 | |
325 | |
277 | ctx->sp = (void **)(ssize + (char *)sptr); |
326 | ctx->sp = (void **)(ssize + (char *)sptr); |
278 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
327 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
279 | *--ctx->sp = (void *)coro_init; |
328 | *--ctx->sp = (void *)coro_init; |
|
|
329 | |
|
|
330 | #if CORO_WIN_TIB |
|
|
331 | *--ctx->sp = 0; /* ExceptionList */ |
|
|
332 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
|
|
333 | *--ctx->sp = sptr; /* StackLimit */ |
|
|
334 | #endif |
|
|
335 | |
280 | ctx->sp -= NUM_SAVED; |
336 | ctx->sp -= NUM_SAVED; |
|
|
337 | memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); |
281 | |
338 | |
282 | # elif CORO_UCONTEXT |
339 | # elif CORO_UCONTEXT |
283 | |
340 | |
284 | getcontext (&(ctx->uc)); |
341 | getcontext (&(ctx->uc)); |
285 | |
342 | |
… | |
… | |
373 | args.arg = arg; |
430 | args.arg = arg; |
374 | args.self = ctx; |
431 | args.self = ctx; |
375 | args.main = &nctx; |
432 | args.main = &nctx; |
376 | |
433 | |
377 | pthread_attr_init (&attr); |
434 | pthread_attr_init (&attr); |
|
|
435 | #if __UCLIBC__ |
|
|
436 | /* exists, but is borked */ |
|
|
437 | /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/ |
|
|
438 | #else |
378 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
439 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
440 | #endif |
379 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
441 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
380 | pthread_create (&ctx->id, &attr, coro_init, &args); |
442 | pthread_create (&ctx->id, &attr, coro_init, &args); |
381 | |
443 | |
382 | coro_transfer (args.main, args.self); |
444 | coro_transfer (args.main, args.self); |
383 | } |
445 | } |