http://blog.163.com/[email protected]/blog/static/86118492201212534924900/
Two problems that have been intertwined: how to identify the kernel stack and the user stack? How do I switch between the kernel-state stack and the user-state stack?
User-State stack pointers: SS and ESP;
Kernel-State stack pointers: Ss0 and esp0;
Both are located in the TSS structure of the task. The task here is to refer to Common tasks except tasks 0 and 1.
When the CPU switches from the user-state stack to the kernel-state stack, the CPU acquires the new stack's segment selector and offset value from TSS in the task status segment of the current task, which is obtained from the SS0 and Esp0 fields of the TSS, after locating the new stack (the kernel-state stack), The CPU first presses the original user-state stack pointer SS and ESP into the kernel-state stack and then presses the flag register eflags content and return position CS, and the EIP into the kernel-state stack.
When a task invokes a system call, it enters the kernel and executes the interrupt service code in the kernel, where the kernel code uses the kernel-state stack of the task to do so. The user-state stack and eflags are restored when the execution Iret exits the kernel program and returns to the user program.
TSS structure of the task:
linux-0.11 stack switch for kernel tasks