Returning from kernel mode to user mode

≯℡__Kan透↙ 提交于 2019-12-21 06:24:29

问题


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 ?


回答1:


iret restores all that stuff

The iret instruction is quite complex. To quote the Intel architecture manual:


When executing a return from an interrupt or exception handler from a different privilege level than the interrupted procedure, the processor performs these actions:

  1. Performs a privilege check.
  2. Restores the CS and EIP registers to their values prior to the interrupt or exception.
  3. Restores the EFLAGS register.
  4. Restores the SS and ESP registers to their values prior to the interrupt or exception, resulting in a stack switch back to the stack of the interrupted procedure.
  5. Resumes execution of the interrupted procedure.


来源:https://stackoverflow.com/questions/9968028/returning-from-kernel-mode-to-user-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!