0
votes

I'm reading some papers and source codes about OS implementation and have a questions about that. In some operating systems, found on github, the context switch is made within the interrupt handling from timer interrupts. They saved the registers rbx, r12, r13, r14, r15, rbp and rsp and recovered those registers from the saved state of the next thread.

These thread switches were made within the timer interrupt handling routine, before iret was called. My question is: When the mentioned registers where recovered by the interrupt handler, why is the iret called? On switching the thread, the next thread will start immediately - or is he starting after finishing the interrupt handler with the iret call?

2
Likely the interrupt handler would have changed the stack context and restored the registers to coincide with the thread that is intended to run. IRET is needed to end an interrupt routine. The thread won't actually be run until the IRET is finished executing. It will end up popping CS:RIP from the stack among other things. That CS:RIP will be the instruction pointer within thread where things will resume. In essence your thread doesn't truly start executing until the IRET is finished returning. This is a general guess without seeing the code.Michael Petch
We are sure getting a lot of questions recently about interrupts and context switches. Not sure what this can be attributed to, since school is out of session and this doesn't seem like a particularly compelling summer study project. Anyway, it's not clear what your question is. The normal way of returning from an interrupt handler is iret. What else should it call?Cody Gray
Thanks to both of you. The question was: will the thread start running before calling iret, @MichaelPetch answered it. Thanks :)jagemue
Possible duplicate of Where to return from an interruptCody Gray
interrupt handlers have nothing whatsoever to do with threads or context switches. They operate at a far lower level, an interrupt simply transfers control to the handler, preserving only the current instruction pointer and flags register. IRET restores them again and the interrupted code resumes executing as though nothing happened. It gets a lot more convoluted in a protected mode OS, ISRs run at ring 0 and will often ask the OS to schedule a task that is executed later, after the ISR is handled. Which certainly can cause a thread to be launched to, say, handle an I/O completion.Hans Passant

2 Answers

3
votes

When the mentioned registers where recovered by the interrupt handler, why is the iret called?

IRET returns the process to the state that it was in before the exception or interrupt that caused it to enter kernel mode.

The registers switches that you see change the process context but that is the state of the process when it was in a kernel mode handler. The IRET instruction then returns the process state to how it was in user mode.

1
votes

When the mentioned registers were recovered by the interrupt handler, why is the iret called? On switching the thread, the next thread will start immediately

You say, "on switching the thread," but the iret instruction is what makes the thread switch happen.

or is he starting after finishing the interrupt handler with the iret call?

Don't think of iret as "return from interrupt." Think of it as,"restore execution context" instead. It pops words from the stack into important context registers, always including the program counter, and maybe including registers that define virtual address space and privilege level. The next instruction that the CPU executes after the iret will be an instruction from the newly restored context.

The saved context that iret pops off the stack happens to be the same format as what a hardware interrupt pushes, but that doesn't mean that you can only pop the context that was pushed by the most recent hardware interrupt. You can pop a context that was pushed some time earlier, and then saved in some "thread" data structure. You can even pop an entirely new context that was manufactured from nothing in order to start a new thread.