I'm a bit confused about the understanding of a mode switch in Unix kernel. I give my understanding here and open it for discussion/correction.
While transitioning from user mode to kernel mode, the processor makes a switch between the per-process-user-stack and the per-process-kernel-stack. Then the user-per-process stack segment selector and stack pointer is stored in the kernel stack and then the eip
instruction pointer (return address at user mode) and other hardware registers are pushed on to the kernel stack
When the kernel has to return to user mode, the trapret
code pops all values stored in the kernel stack back to the hardware registers.
But when iret
pops eip from the kernel stack, the next instruction that should get executed is the return address in user mode.
This happens without completely popping the other values of the kernel stack.
How do the rest of the values (%cs, %eflags, %esp, %ss
) get restored ?
How is the user-stack-pointer present in kernel stack popped back to %esp ?