diff options
Diffstat (limited to 'src/uctx.h')
-rw-r--r-- | src/uctx.h | 41 |
1 files changed, 26 insertions, 15 deletions
@@ -18,19 +18,18 @@ #ifdef VALGRIND #include <valgrind/valgrind.h> -#else -#define VALGRIND_STACK_REGISTER(stack_base, size) 0 -#define VALGRIND_STACK_DEREGISTER(stack_id) #endif #define STACK_GUARD 0xbad57ac4 struct tf_uctx { - int *stack_guard; - size_t size; - void *alloc; - void *current_sp; - unsigned int stack_id; + int * stack_guard; + size_t size; + void * alloc; + void * current_sp; +#ifdef VALGRIND + unsigned int stack_id; +#endif }; #if defined(__i386__) @@ -89,13 +88,14 @@ static inline void stack_push_ptr(void **stackptr, void *ptr) } -static inline void tf_uctx_create_self(struct tf_uctx *uctx) +static inline tf_uctx_t tf_uctx_create_self(struct tf_uctx *uctx) { static int dummy_guard = STACK_GUARD; *uctx = (struct tf_uctx) { .stack_guard = &dummy_guard, }; + return uctx; } static inline void * @@ -118,20 +118,24 @@ tf_uctx_create_embedded( /* Create initial stack frame (cdecl convention) */ stack = stack_pointer(stack_base, size); - user_data = stack_push(&stack, TF_ALIGN(private_size, 64)); + user_data = stack_push(&stack, TF_ALIGN(private_size, 16)); + uctx = stack_push(&stack, TF_ALIGN(sizeof(struct tf_uctx), 16)); stack_push_ptr(&stack, main_argument); stack_push_ptr(&stack, user_data); stack_push_ptr(&stack, NULL); stack_push_ptr(&stack, stack_frame_main); /* eip */ stack_push_ptr(&stack, NULL); /* ebp */ - uctx = user_data + uctx_offset; + *((tf_uctx_t *) (user_data + uctx_offset)) = uctx; + *uctx = (struct tf_uctx) { .stack_guard = stack_guard(stack_base, size), .alloc = stack_base, .size = size, .current_sp = stack, +#ifdef VALGRIND .stack_id = VALGRIND_STACK_REGISTER(stack_base, stack_base+size), +#endif }; *uctx->stack_guard = STACK_GUARD; @@ -139,18 +143,25 @@ tf_uctx_create_embedded( } static inline -void tf_uctx_destroy(struct tf_uctx *uctx) +void tf_uctx_destroy(tf_uctx_t ctx) { + struct tf_uctx *uctx = ctx; + if (uctx->alloc != NULL) { +#ifdef VALGRIND VALGRIND_STACK_DEREGISTER(uctx->stack_id); +#endif tf_bmem_free(uctx->alloc, uctx->size); } } static inline -void tf_uctx_transfer(struct tf_uctx *from, struct tf_uctx *to) +void tf_uctx_transfer(tf_uctx_t from, tf_uctx_t to) { + struct tf_uctx *ufrom = from; + struct tf_uctx *uto = to; + /* Switch stack pointers */ - TF_BUG_ON(*from->stack_guard != STACK_GUARD); - switch_fiber(from, to); + TF_BUG_ON(*ufrom->stack_guard != STACK_GUARD); + switch_fiber(ufrom, uto); } |