Reputation: 393
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?
Upvotes: 0
Views: 1814
Reputation: 21607
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.
Upvotes: 3
Reputation: 27115
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.
Upvotes: 1