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 | #ifdef __linux && __elf |
|
|
92 | /* we blindly assume on any __linux with __elf 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 |
121 | #if CORO_WIN_TIB |
|
|
122 | #define NUM_SAVED 6+3 |
|
|
123 | "\tpush %fs:0x0\n" |
|
|
124 | "\tpush %fs:0x8\n" |
|
|
125 | "\tpush %fs:0xc\n" |
|
|
126 | #else |
|
|
127 | #define NUM_SAVED 6 |
128 | #define NUM_SAVED 6 |
128 | #endif |
|
|
129 | "\tpush %rbp\n" |
129 | "\tpush %rbp\n" |
130 | "\tpush %rbx\n" |
130 | "\tpush %rbx\n" |
131 | "\tpush %r12\n" |
131 | "\tpush %r12\n" |
132 | "\tpush %r13\n" |
132 | "\tpush %r13\n" |
133 | "\tpush %r14\n" |
133 | "\tpush %r14\n" |
134 | "\tpush %r15\n" |
134 | "\tpush %r15\n" |
|
|
135 | #if CORO_WIN_TIB |
|
|
136 | "\tpush %gs:0x0\n" |
|
|
137 | "\tpush %gs:0x8\n" |
|
|
138 | "\tpush %gs:0xc\n" |
|
|
139 | #endif |
135 | "\tmov %rsp, (%rdi)\n" |
140 | "\tmov %rsp, (%rdi)\n" |
136 | "\tmov (%rsi), %rsp\n" |
141 | "\tmov (%rsi), %rsp\n" |
|
|
142 | #if CORO_WIN_TIB |
|
|
143 | "\tpop %gs:0xc\n" |
|
|
144 | "\tpop %gs:0x8\n" |
|
|
145 | "\tpop %gs:0x0\n" |
|
|
146 | #endif |
137 | "\tpop %r15\n" |
147 | "\tpop %r15\n" |
138 | "\tpop %r14\n" |
148 | "\tpop %r14\n" |
139 | "\tpop %r13\n" |
149 | "\tpop %r13\n" |
140 | "\tpop %r12\n" |
150 | "\tpop %r12\n" |
141 | "\tpop %rbx\n" |
151 | "\tpop %rbx\n" |
142 | "\tpop %rbp\n" |
152 | "\tpop %rbp\n" |
143 | #if CORO_WIN_TIB |
|
|
144 | "\tpop %fs:0xc\n" |
|
|
145 | "\tpop %fs:0x8\n" |
|
|
146 | "\tpop %fs:0x0\n" |
|
|
147 | #endif |
|
|
148 | #elif __i386 |
153 | #elif __i386 |
149 | #if CORO_WIN_TIB |
|
|
150 | #define NUM_SAVED 4+3 |
|
|
151 | "\tpush %fs:0x0\n" |
|
|
152 | "\tpush %fs:0x4\n" |
|
|
153 | "\tpush %fs:0x8\n" |
|
|
154 | #else |
|
|
155 | #define NUM_SAVED 4 |
154 | #define NUM_SAVED 4 |
156 | #endif |
|
|
157 | "\tpush %ebp\n" |
155 | "\tpush %ebp\n" |
158 | "\tpush %ebx\n" |
156 | "\tpush %ebx\n" |
159 | "\tpush %esi\n" |
157 | "\tpush %esi\n" |
160 | "\tpush %edi\n" |
158 | "\tpush %edi\n" |
|
|
159 | #if CORO_WIN_TIB |
|
|
160 | "\tpush %fs:0\n" |
|
|
161 | "\tpush %fs:4\n" |
|
|
162 | "\tpush %fs:8\n" |
|
|
163 | #endif |
161 | "\tmov %esp, (%eax)\n" |
164 | "\tmov %esp, (%eax)\n" |
162 | "\tmov (%edx), %esp\n" |
165 | "\tmov (%edx), %esp\n" |
|
|
166 | #if CORO_WIN_TIB |
|
|
167 | "\tpop %fs:8\n" |
|
|
168 | "\tpop %fs:4\n" |
|
|
169 | "\tpop %fs:0\n" |
|
|
170 | #endif |
163 | "\tpop %edi\n" |
171 | "\tpop %edi\n" |
164 | "\tpop %esi\n" |
172 | "\tpop %esi\n" |
165 | "\tpop %ebx\n" |
173 | "\tpop %ebx\n" |
166 | "\tpop %ebp\n" |
174 | "\tpop %ebp\n" |
167 | #if CORO_WIN_TIB |
|
|
168 | "\tpop %fs:0x8\n" |
|
|
169 | "\tpop %fs:0x4\n" |
|
|
170 | "\tpop %fs:0x0\n" |
|
|
171 | #endif |
|
|
172 | #else |
175 | #else |
173 | #error unsupported architecture |
176 | #error unsupported architecture |
174 | #endif |
177 | #endif |
175 | "\tret\n" |
178 | "\tret\n" |
176 | ); |
179 | ); |
… | |
… | |
212 | perror ("sigaction"); |
215 | perror ("sigaction"); |
213 | abort (); |
216 | abort (); |
214 | } |
217 | } |
215 | |
218 | |
216 | /* set the new stack */ |
219 | /* set the new stack */ |
217 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
220 | nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */ |
218 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
221 | nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize); |
219 | nstk.ss_flags = 0; |
222 | nstk.ss_flags = 0; |
220 | |
223 | |
221 | if (sigaltstack (&nstk, &ostk) < 0) |
224 | if (sigaltstack (&nstk, &ostk) < 0) |
222 | { |
225 | { |
223 | perror ("sigaltstack"); |
226 | perror ("sigaltstack"); |
… | |
… | |
299 | # elif CORO_ASM |
302 | # elif CORO_ASM |
300 | |
303 | |
301 | ctx->sp = (void **)(ssize + (char *)sptr); |
304 | ctx->sp = (void **)(ssize + (char *)sptr); |
302 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
305 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
303 | *--ctx->sp = (void *)coro_init; |
306 | *--ctx->sp = (void *)coro_init; |
|
|
307 | |
|
|
308 | #if CORO_WIN_TIB |
|
|
309 | *--ctx->sp = 0; /* ExceptionList */ |
|
|
310 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
|
|
311 | *--ctx->sp = sptr; /* StackLimit */ |
|
|
312 | #endif |
|
|
313 | |
304 | ctx->sp -= NUM_SAVED; |
314 | ctx->sp -= NUM_SAVED; |
|
|
315 | memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); |
305 | |
316 | |
306 | # elif CORO_UCONTEXT |
317 | # elif CORO_UCONTEXT |
307 | |
318 | |
308 | getcontext (&(ctx->uc)); |
319 | getcontext (&(ctx->uc)); |
309 | |
320 | |
… | |
… | |
397 | args.arg = arg; |
408 | args.arg = arg; |
398 | args.self = ctx; |
409 | args.self = ctx; |
399 | args.main = &nctx; |
410 | args.main = &nctx; |
400 | |
411 | |
401 | pthread_attr_init (&attr); |
412 | pthread_attr_init (&attr); |
|
|
413 | #if __UCLIBC__ |
|
|
414 | /* exists, but is borked */ |
|
|
415 | /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/ |
|
|
416 | #else |
402 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
417 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
418 | #endif |
403 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
419 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
404 | pthread_create (&ctx->id, &attr, coro_init, &args); |
420 | pthread_create (&ctx->id, &attr, coro_init, &args); |
405 | |
421 | |
406 | coro_transfer (args.main, args.self); |
422 | coro_transfer (args.main, args.self); |
407 | } |
423 | } |