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 | } |
… | |
… | |
118 | ".type coro_transfer, @function\n" |
123 | ".type coro_transfer, @function\n" |
119 | "coro_transfer:\n" |
124 | "coro_transfer:\n" |
120 | /* windows, of course, gives a shit on the amd64 ABI and uses different registers */ |
125 | /* 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 */ |
126 | /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */ |
122 | #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 |
123 | #define NUM_SAVED 6 |
151 | #define NUM_SAVED 6 |
124 | "\tpush %rbp\n" |
152 | "\tpushq %rbp\n" |
125 | "\tpush %rbx\n" |
153 | "\tpushq %rbx\n" |
126 | "\tpush %r12\n" |
154 | "\tpushq %r12\n" |
127 | "\tpush %r13\n" |
155 | "\tpushq %r13\n" |
128 | "\tpush %r14\n" |
156 | "\tpushq %r14\n" |
129 | "\tpush %r15\n" |
157 | "\tpushq %r15\n" |
130 | #if CORO_WIN_TIB |
158 | "\tmovq %rsp, (%rdi)\n" |
|
|
159 | "\tmovq (%rsi), %rsp\n" |
|
|
160 | "\tpopq %r15\n" |
|
|
161 | "\tpopq %r14\n" |
|
|
162 | "\tpopq %r13\n" |
|
|
163 | "\tpopq %r12\n" |
131 | "\tpush %gs:0x0\n" |
164 | "\tpopq %rbx\n" |
132 | "\tpush %gs:0x8\n" |
165 | "\tpopq %rbp\n" |
133 | "\tpush %gs:0xc\n" |
|
|
134 | #endif |
166 | #endif |
135 | "\tmov %rsp, (%rdi)\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 |
|
|
142 | "\tpop %r15\n" |
|
|
143 | "\tpop %r14\n" |
|
|
144 | "\tpop %r13\n" |
|
|
145 | "\tpop %r12\n" |
|
|
146 | "\tpop %rbx\n" |
|
|
147 | "\tpop %rbp\n" |
|
|
148 | #elif __i386 |
167 | #elif __i386 |
149 | #define NUM_SAVED 4 |
168 | #define NUM_SAVED 4 |
150 | "\tpush %ebp\n" |
169 | "\tpushl %ebp\n" |
151 | "\tpush %ebx\n" |
170 | "\tpushl %ebx\n" |
152 | "\tpush %esi\n" |
171 | "\tpushl %esi\n" |
153 | "\tpush %edi\n" |
172 | "\tpushl %edi\n" |
154 | #if CORO_WIN_TIB |
173 | #if CORO_WIN_TIB |
155 | "\tpush %fs:0\n" |
174 | "\tpushl %fs:0\n" |
156 | "\tpush %fs:4\n" |
175 | "\tpushl %fs:4\n" |
157 | "\tpush %fs:8\n" |
176 | "\tpushl %fs:8\n" |
158 | #endif |
177 | #endif |
159 | "\tmov %esp, (%eax)\n" |
178 | "\tmovl %esp, (%eax)\n" |
160 | "\tmov (%edx), %esp\n" |
179 | "\tmovl (%edx), %esp\n" |
161 | #if CORO_WIN_TIB |
180 | #if CORO_WIN_TIB |
162 | "\tpop %fs:8\n" |
181 | "\tpopl %fs:8\n" |
163 | "\tpop %fs:4\n" |
182 | "\tpopl %fs:4\n" |
164 | "\tpop %fs:0\n" |
183 | "\tpopl %fs:0\n" |
165 | #endif |
184 | #endif |
166 | "\tpop %edi\n" |
185 | "\tpopl %edi\n" |
167 | "\tpop %esi\n" |
186 | "\tpopl %esi\n" |
168 | "\tpop %ebx\n" |
187 | "\tpopl %ebx\n" |
169 | "\tpop %ebp\n" |
188 | "\tpopl %ebp\n" |
170 | #else |
189 | #else |
171 | #error unsupported architecture |
190 | #error unsupported architecture |
172 | #endif |
191 | #endif |
173 | "\tret\n" |
192 | "\tret\n" |
174 | ); |
193 | ); |
… | |
… | |
210 | perror ("sigaction"); |
229 | perror ("sigaction"); |
211 | abort (); |
230 | abort (); |
212 | } |
231 | } |
213 | |
232 | |
214 | /* set the new stack */ |
233 | /* set the new stack */ |
215 | 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. */ |
216 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
235 | nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize); |
217 | nstk.ss_flags = 0; |
236 | nstk.ss_flags = 0; |
218 | |
237 | |
219 | if (sigaltstack (&nstk, &ostk) < 0) |
238 | if (sigaltstack (&nstk, &ostk) < 0) |
220 | { |
239 | { |
221 | perror ("sigaltstack"); |
240 | perror ("sigaltstack"); |
… | |
… | |
305 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
324 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
306 | *--ctx->sp = sptr; /* StackLimit */ |
325 | *--ctx->sp = sptr; /* StackLimit */ |
307 | #endif |
326 | #endif |
308 | |
327 | |
309 | ctx->sp -= NUM_SAVED; |
328 | ctx->sp -= NUM_SAVED; |
|
|
329 | memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); |
310 | |
330 | |
311 | # elif CORO_UCONTEXT |
331 | # elif CORO_UCONTEXT |
312 | |
332 | |
313 | getcontext (&(ctx->uc)); |
333 | getcontext (&(ctx->uc)); |
314 | |
334 | |
… | |
… | |
402 | args.arg = arg; |
422 | args.arg = arg; |
403 | args.self = ctx; |
423 | args.self = ctx; |
404 | args.main = &nctx; |
424 | args.main = &nctx; |
405 | |
425 | |
406 | 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 |
407 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
431 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
432 | #endif |
408 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
433 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
409 | pthread_create (&ctx->id, &attr, coro_init, &args); |
434 | pthread_create (&ctx->id, &attr, coro_init, &args); |
410 | |
435 | |
411 | coro_transfer (args.main, args.self); |
436 | coro_transfer (args.main, args.self); |
412 | } |
437 | } |