views:

149

answers:

0

Im trying to modify a jmp_buf so it executes a function (after i setjmp and longjmp back in).

I have a struct called t that stores information about each thread. start_routine is a function pointer that should be executed.

/* this is inside an init() function setting stuff up */
void init(void *(*start_routine)(void*), void *arg)
{
    tcb *t = (tcb *)malloc(sizeof(tcb));
    t->stack = malloc(STACKSIZE);
    t->status = READY;
    t->jump = (jmp_buf*)malloc(sizeof(jmp_buf));

    int *ps, *tos;
    ps = (int *) &(t->jump);
    ps[5] = ptr_mangle((int)start_routine);/*program ctr*/
    tos = (t->stack) + (STACKSIZE/sizeof(int));
    tos[0] = (int)pthread_exit;
    tos[1] = (int)arg;
    ps[4] = ptr_mangle((int) tos);//stack ptr

    ret = setjmp(*(t->jump));
}
/* end init function code */


static int ptr_mangle(int p)
{
    unsigned int ret;
    asm(" movl %1, %%eax;\n"
        " xorl %%gs:0x18, %%eax;"
        " roll $0x9, %%eax;"
        " movl %%eax, %0;"
    : "=r"(ret)
    : "r"(p)
    : "%eax"
    );
    return ret;
}

if I try to longjmp into this jump buffer it does go to the functiion start_routine, but it just picks up right after setjmp. (exactly what i dont want).

so does anyone think i am setting up my stack incorrectly or modifying my jump buffer in a bad way?