C call back function from assembly (x86) and process switching -


this code undergraduate os course. trying call function on same stack of new_sp , return whatever new_sp doing. not working , i'm not sure how de-bug it. suggestions or solution great. tried through if there left out please let me know.

notes: part of special os (xinu) using class. function called within c code. reference using: http://www.unixwiz.net/techtips/win32-callconv-asm.html

this part of code saves process stack before switched out. stack pointed new_sp saved way well.

    .text     .globl  callsw      /*---------------------------------------------------------------------     * callsw -  call callsw(&old_sp, &new_sp, &call_back)     *---------------------------------------------------------------------*/ callsw:     /*keep of old contex switch instructions work       cxtsw*/     pushl   %ebp          /* push ebp onto stack          */     movl    %esp,%ebp       /* record current sp in ebp     */     pushfl                  /* record flags                 */     pushal                  /* save general regs on stack   */      /* save old segment registers here, if multiple allowed */      movl    8(%ebp),%eax    /* mem location in */     /*  save old process's sp   */     movl    %esp,(%eax)     /* save old process's sp        */     movl    12(%ebp),%eax   /* location   */ 

here begins code switch call function. want call function run , return executing code assoated new_sp stack.

    /* switch new stach instead */      movl    (%eax),%esp     /* pick new process's sp     */      /* restore new seg. registers here, if multiple allowed */      popal                   /* restore general registers    */     movl    (%ebp),%eax     /*keep old ebp acess arg 3*/     movl    4(%esp),%ebp    /* pick ebp before restoring */     /*   interrupts                 */     popfl                   /* restore interrupt mask       */     add     $4,%esp         /* skip saved value of ebp      */ /* switch new call instead */      movl    (%eax),%esp     /* pick new process's sp     */      /* restore new seg. registers here, if multiple allowed */      popal                   /* restore general registers    */     movl    (%ebp),%eax     /*keep old ebp acess arg 3*/     movl    4(%esp),%ebp    /* pick ebp before restoring */     /*   interrupts                 */     popfl                   /* restore interrupt mask       */     add     $4,%esp         /* skip saved value of ebp      */ 

this try set stack , jump new process. function calling c function no arguments , no return value.

    pop     %ebx /* save ra*/      movl    %esp,%ebp /* move base pointer bottom of stack */     add     -18,%ebp /* move stack down*/     /*set stack , jump */     movl    %ebp,%esp /* nothing on stack = */     movl    %ebx, 0(%ebp) /* save ebp stack */     movl    %ebx, 4(%ebp) /* save ra */     movl    16(%eax),%ecx     jmp (%ecx)     /* jump call_back        */ 

i think problem here:

popal                   /* restore general registers    */ movl    (%ebp),%eax     /*keep old ebp acess arg 3*/ 

the popal restores registers (including ebp) ones saved on stack you're switching to. movl (%ebp),%eax loads value new stack (actually value of %ebp belonging caller of callsw. when later do

movl    (%eax),%esp     /* pick new process's sp     */ 

you're not getting new process's sp -- you're getting frame pointer 2 levels stack (the caller of caller of callsw).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -