From ffe17e686d99efc672b31e6876dcc4e30a05a572 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Tue, 1 Dec 2009 13:55:43 +0200 Subject: libtf: optimize x86 fiber switching by letting gcc figure out better how to represent stack pointers. also uses labels to make the code more readable. --- src/uctx.h | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'src/uctx.h') diff --git a/src/uctx.h b/src/uctx.h index 03750e9..541fcf9 100644 --- a/src/uctx.h +++ b/src/uctx.h @@ -32,10 +32,32 @@ struct tf_uctx { #if defined(__i386__) +#define switch_fiber(prev, next) \ + do { \ + unsigned eax, ebx, ecx, edx, esi, edi; \ + __asm__ __volatile__ ( \ + "push %%ebp \n" \ + "call 1f \n" \ + "1: \n" \ + "addl $2f-1b, (%%esp) \n" \ + "movl %%esp, %[prev_sp] \n" \ + "movl %[next_sp], %%esp \n" \ + "ret \n" \ + "2: \n" \ + "pop %%ebp \n" \ + : [prev_sp] "=m"(prev->current_sp), \ + "=a"(eax), "=b"(ebx), "=c"(ecx), \ + "=d"(edx), "=S"(esi), "=D"(edi) \ + : [next_sp] "m"(next->current_sp) \ + : "memory", "cc"); \ + } while (0) + #define STACK_GROWS_UP #else -#error Your architecture is not supported. Send email to timo.teras@iki.fi. + +#error Your architecture is not supported. Contact timo.teras@iki.fi. + #endif #if defined(STACK_GROWS_UP) @@ -108,19 +130,6 @@ void tf_uctx_destroy(struct tf_fiber *fiber) free(uctx->alloc); } -#define switch_fiber(oldspptr, newsp) \ - __asm__ __volatile__ ( \ - "push %%ebp ;" \ - "call .+5 ;" \ - "add $0x9,(%%esp) ;" \ - "mov %%esp, (%0) ;" \ - "mov %1, %%esp ;" \ - "ret ;" \ - "pop %%ebp" \ - : \ - : "a"(oldspptr), "d"(newsp) \ - : "%ebx", "%ecx", "%esi", "%edi", "memory", "cc"); - static inline void tf_uctx_transfer(struct tf_fiber *from, struct tf_fiber *to) { @@ -130,5 +139,5 @@ void tf_uctx_transfer(struct tf_fiber *from, struct tf_fiber *to) /* Switch stack pointers */ TF_BUG_ON(*ufrom->stack_guard != STACK_GUARD); - switch_fiber(&ufrom->current_sp, uto->current_sp); + switch_fiber(ufrom, uto); } -- cgit v1.2.3