--- libcoro/coro.c 2010/04/02 20:21:21 1.52 +++ libcoro/coro.c 2011/06/10 12:27:02 1.58 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001-2009 Marc Alexander Lehmann + * Copyright (c) 2001-2011 Marc Alexander Lehmann * * Redistribution and use in source and binary forms, with or without modifica- * tion, are permitted provided that the following conditions are met: @@ -88,6 +88,11 @@ coro_transfer (new_coro, create_coro); +#if __linux && __amd64 + /* we blindly assume on any __linux with __amd64 we have a new enough gas with .cfi_undefined support */ + asm (".cfi_undefined rip"); +#endif + func ((void *)arg); /* the new coro returned. bad. just abort() for now */ @@ -120,31 +125,53 @@ /* windows, of course, gives a shit on the amd64 ABI and uses different registers */ /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */ #if __amd64 - #define NUM_SAVED 6 - "\tpush %rbp\n" - "\tpush %rbx\n" - "\tpush %r12\n" - "\tpush %r13\n" - "\tpush %r14\n" - "\tpush %r15\n" - #if CORO_WIN_TIB - "\tpush %gs:0x0\n" - "\tpush %gs:0x8\n" - "\tpush %gs:0xc\n" + #ifdef _WIN32 + #define NUM_SAVED 8 + "\tpush %rsi\n" + "\tpush %rdi\n" + "\tpush %rbp\n" + "\tpush %rbx\n" + "\tpush %r12\n" + "\tpush %r13\n" + "\tpush %r14\n" + "\tpush %r15\n" + #if CORO_WIN_TIB + "\tpush %gs:0x0\n" + "\tpush %gs:0x8\n" + "\tpush %gs:0xc\n" + #endif + "\tmov %rsp, (%rcx)\n" + "\tmov (%rdx), %rsp\n" + #if CORO_WIN_TIB + "\tpop %gs:0xc\n" + "\tpop %gs:0x8\n" + "\tpop %gs:0x0\n" + #endif + "\tpop %r15\n" + "\tpop %r14\n" + "\tpop %r13\n" + "\tpop %r12\n" + "\tpop %rbx\n" + "\tpop %rbp\n" + "\tpop %rdi\n" + "\tpop %rsi\n" + #else + #define NUM_SAVED 6 + "\tpush %rbp\n" + "\tpush %rbx\n" + "\tpush %r12\n" + "\tpush %r13\n" + "\tpush %r14\n" + "\tpush %r15\n" + "\tmov %rsp, (%rdi)\n" + "\tmov (%rsi), %rsp\n" + "\tpop %r15\n" + "\tpop %r14\n" + "\tpop %r13\n" + "\tpop %r12\n" + "\tpop %rbx\n" + "\tpop %rbp\n" #endif - "\tmov %rsp, (%rdi)\n" - "\tmov (%rsi), %rsp\n" - #if CORO_WIN_TIB - "\tpop %gs:0xc\n" - "\tpop %gs:0x8\n" - "\tpop %gs:0x0\n" - #endif - "\tpop %r15\n" - "\tpop %r14\n" - "\tpop %r13\n" - "\tpop %r12\n" - "\tpop %rbx\n" - "\tpop %rbp\n" #elif __i386 #define NUM_SAVED 4 "\tpush %ebp\n" @@ -156,17 +183,17 @@ "\tpush %fs:4\n" "\tpush %fs:8\n" #endif - "\tmov %esp, (%eax)\n" - "\tmov (%edx), %esp\n" + "\tmov %esp, (%eax)\n" + "\tmov (%edx), %esp\n" #if CORO_WIN_TIB - "\tpop %fs:8\n" - "\tpop %fs:4\n" - "\tpop %fs:0\n" + "\tpop %fs:8\n" + "\tpop %fs:4\n" + "\tpop %fs:0\n" #endif - "\tpop %edi\n" - "\tpop %esi\n" - "\tpop %ebx\n" - "\tpop %ebp\n" + "\tpop %edi\n" + "\tpop %esi\n" + "\tpop %ebx\n" + "\tpop %ebp\n" #else #error unsupported architecture #endif @@ -212,8 +239,8 @@ } /* set the new stack */ - nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ - nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); + nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */ + nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize); nstk.ss_flags = 0; if (sigaltstack (&nstk, &ostk) < 0) @@ -307,6 +334,7 @@ #endif ctx->sp -= NUM_SAVED; + memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); # elif CORO_UCONTEXT @@ -404,7 +432,12 @@ args.main = &nctx; pthread_attr_init (&attr); +#if __UCLIBC__ + /* exists, but is borked */ + /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/ +#else pthread_attr_setstack (&attr, sptr, (size_t)ssize); +#endif pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); pthread_create (&ctx->id, &attr, coro_init, &args);