2

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?

bkennedy
  • 403
  • 1
  • 4
  • 17
  • 1
    what should the restoration do? The `proc->tf->esp = proc->tf->esp + 24;` looks to revert the action of `signal_deliver`, and the last `eip =` restores the `eip` variable. But why do you write into the released stack the stack pointer itself, in place where `eax/ecx/edx` was stored in `signal_deliver`? Don't you want rather to restore those instead? Ie. `proc->tf->eax = *((uint*) (proc->tf->esp-8));` ... (beware, I have no idea what is going on in that code, and what are `proc->tf`, if this is live CPU regs, I wonder how this even can work without crashing due to interrupts/etc.) – Ped7g Oct 18 '16 at 13:01
  • @Ped7g you were able to help me understand how to implement the `signal_deliver` thank you. Perhaps you want to post the answer? I completed it and can post it as an answer if you do not want to. – bkennedy Oct 18 '16 at 20:51
  • 1
    go ahead, post working solution. – Ped7g Oct 19 '16 at 02:00

1 Answers1

2
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)); 
}
bkennedy
  • 403
  • 1
  • 4
  • 17