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 | #define NUM_SAVED 6 |
128 | #define NUM_SAVED 6 |
122 | "\tpush %rbp\n" |
129 | "\tpush %rbp\n" |
123 | "\tpush %rbx\n" |
130 | "\tpush %rbx\n" |
124 | "\tpush %r12\n" |
131 | "\tpush %r12\n" |
125 | "\tpush %r13\n" |
132 | "\tpush %r13\n" |
126 | "\tpush %r14\n" |
133 | "\tpush %r14\n" |
127 | "\tpush %r15\n" |
134 | "\tpush %r15\n" |
128 | #if CORO_WIN_TIB |
135 | #if CORO_WIN_TIB |
129 | "\tpush %fs:0x0\n" |
136 | "\tpush %gs:0x0\n" |
130 | "\tpush %fs:0x8\n" |
137 | "\tpush %gs:0x8\n" |
131 | "\tpush %fs:0xc\n" |
138 | "\tpush %gs:0xc\n" |
132 | #endif |
139 | #endif |
133 | "\tmov %rsp, (%rdi)\n" |
140 | "\tmov %rsp, (%rdi)\n" |
134 | "\tmov (%rsi), %rsp\n" |
141 | "\tmov (%rsi), %rsp\n" |
135 | #if CORO_WIN_TIB |
142 | #if CORO_WIN_TIB |
136 | "\tpop %fs:0xc\n" |
143 | "\tpop %gs:0xc\n" |
137 | "\tpop %fs:0x8\n" |
144 | "\tpop %gs:0x8\n" |
138 | "\tpop %fs:0x0\n" |
145 | "\tpop %gs:0x0\n" |
139 | #endif |
146 | #endif |
140 | "\tpop %r15\n" |
147 | "\tpop %r15\n" |
141 | "\tpop %r14\n" |
148 | "\tpop %r14\n" |
142 | "\tpop %r13\n" |
149 | "\tpop %r13\n" |
143 | "\tpop %r12\n" |
150 | "\tpop %r12\n" |
… | |
… | |
208 | perror ("sigaction"); |
215 | perror ("sigaction"); |
209 | abort (); |
216 | abort (); |
210 | } |
217 | } |
211 | |
218 | |
212 | /* set the new stack */ |
219 | /* set the new stack */ |
213 | 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. */ |
214 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
221 | nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize); |
215 | nstk.ss_flags = 0; |
222 | nstk.ss_flags = 0; |
216 | |
223 | |
217 | if (sigaltstack (&nstk, &ostk) < 0) |
224 | if (sigaltstack (&nstk, &ostk) < 0) |
218 | { |
225 | { |
219 | perror ("sigaltstack"); |
226 | perror ("sigaltstack"); |
… | |
… | |
303 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
310 | *--ctx->sp = (char *)sptr + ssize; /* StackBase */ |
304 | *--ctx->sp = sptr; /* StackLimit */ |
311 | *--ctx->sp = sptr; /* StackLimit */ |
305 | #endif |
312 | #endif |
306 | |
313 | |
307 | ctx->sp -= NUM_SAVED; |
314 | ctx->sp -= NUM_SAVED; |
|
|
315 | memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); |
308 | |
316 | |
309 | # elif CORO_UCONTEXT |
317 | # elif CORO_UCONTEXT |
310 | |
318 | |
311 | getcontext (&(ctx->uc)); |
319 | getcontext (&(ctx->uc)); |
312 | |
320 | |
… | |
… | |
400 | args.arg = arg; |
408 | args.arg = arg; |
401 | args.self = ctx; |
409 | args.self = ctx; |
402 | args.main = &nctx; |
410 | args.main = &nctx; |
403 | |
411 | |
404 | 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 |
405 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
417 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
418 | #endif |
406 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
419 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
407 | pthread_create (&ctx->id, &attr, coro_init, &args); |
420 | pthread_create (&ctx->id, &attr, coro_init, &args); |
408 | |
421 | |
409 | coro_transfer (args.main, args.self); |
422 | coro_transfer (args.main, args.self); |
410 | } |
423 | } |