Search code examples
cx86xv6

Restoring Stackframe in x86


I am trying to write a program that implements signals into xv6

I have figured out how to manipulate the stack (I think) and I am just having trouble restoring it. Here is my code for signal deliver:

This function adds the signal frame ot the process stack and saves the volatile registers

void signal_deliver(int signum)
{
*((uint*) (proc->tf->esp-4)) = proc->tf->eip;
*((uint*) (proc->tf->esp-8)) = proc->tf->eax;
*((uint*) (proc->tf->esp-12)) = proc->tf->ecx;
*((uint*) (proc->tf->esp-16)) = proc->tf->edx;
*((uint*) (proc->tf->esp-20)) = signum;
*((uint*) (proc->tf->esp-24)) = *(uint*) proc -> signal_trampoline;
proc->tf->esp = proc->tf->esp-24;
proc->tf->eip = (uint) (proc->signal_handlers[signum]);
}

I am having trouble restoring my trapframe process in my void signal_return(void).

My attempt to restore the frame is:

    proc->tf->esp = proc->tf->esp + 24;
    *((uint*)(proc->tf->esp - 16)) = proc->tf->esp;
    *((uint*)(proc->tf->esp - 12)) = proc->tf->esp;
    *((uint*)(proc->tf->esp - 8)) = proc->tf->esp;
    proc->tf->eip = *((uint*)(proc->tf->esp - 4));

Can anyone point me in the right direction?


Solution

  • void signal_return(void) {
        proc->tf->esp = proc->tf->esp + 24;
        proc->tf->edx = *((uint*)(proc->tf->esp - 16));
        proc->tf->ecx = *((uint*)(proc->tf->esp - 12));
        proc->tf->eax = *((uint*)(proc->tf->esp - 8));
        proc->tf->eip = *((uint*)(proc->tf->esp - 4)); 
    }