Copy from http://blog.csdn.net/chenglian_999/article/details/4785720
In the past, when I was reading about Linux principles, I always encountered kernel stacks and user stacks. Today, I have taken a look at the use of the stack in Linux separately. Here I will make a summary.
I. linux Stack
The Linux0.11 core involves four types of stacks: the temporary stack during system boot, the stack used for Kernel initialization, the kernel state stack, and the user State stack.
1) Temporary stack during system guidance
In boosetct, when it moves itself to 0x90000, it will set a temporary stack, with 0x90000 as the segment address, 0xff00 as the offset address.
This stack is also used in Setup as a temporary stack for system guidance.
2) stack used for Kernel Initialization
When the head is executed in protection mode, the stack segment address is set at the data segment address of the kernel (in this case, the ss does not store the specific base address, but the stack segment selection character, of course, it is also the data segment selection character). The stack top Pointer Points to the top of the user_stack array and keeps one page of memory as the stack.
The stack set in head is used when you enter main for system initialization. However, after move_to_user_mode (), the code is switched to task 0 and Task 1 is created in task 0, task 1 uses its own stack (indicated in tss in Task 1's task_struct ). Task 0 still uses the previously used stack as its user stack.
3) User State Stack
When a user process is running, it involves stack operations such as process calling. At this time, it uses the user stack, and the base address of the user stack is the Data Segment Base Address of the task, the top pointer of the stack is located at the top of the linear address space (just near the top, where command line parameters and environment variables are stored at the top ). The actual physical pages used are allocated during "Copy at write time.
4) core State Stack
When a user process executes the kernel code, such as a system call, the core State stack is used, and each task has its own core State stack, its physical location is located on the page where task_struct of this task is located. The specific base address and stack top pointer are specified by ss0 and esp0 in tss.
Ii. switching between user-mode stack and kernel-mode Stack
When a task is switched, there is no doubt that the stack will be switched. The stack used by the task will change. The stack is switched between the user stacks of the two tasks, it is done through TSS. When the CPU privilege level changes, the stack it uses also changes, at this time, the switching is the switching between the user stack and the core stack of the same task. Next I will talk about the general process of switching between user-mode stack and core-mode stack.
All interrupts in the 0.11 core belong to the kernel code. Therefore, if an interruption occurs, the task is running the user code, that is, in the user State, then, the task changes from user State (privileged level 3) to kernel state (privileged level 0). At this time, switching from user State stack to core State stack occurs. The CPU first obtains the kernel state stack address from the TSS in task_struct, And then pushes the segment address and top pointer of the user State stack to the core State stack, then, the eflags, es, and ip are pushed into the stack. Then execute the interrupt processing program. At this time, the kernel state stack is used. After the interrupt processing program is executed, iret will return to the user mode to restore the user stack and eflags. In this way, the user stack is used again.
Note: in kernel mode, when the cpu is interrupted, the stack operations of ss and esp are not performed on the cpu.
Iii. Stack switching in task 0
At the beginning of the kernel, code execution will be handed over to task 0 (move_to_user_mode). The Kernel stack of task 0 is located at the end of the page where the data structure of its own task is located, the user stack of task 0 is the stack used when the CPU enters the protection mode, that is, usr_stack []. The user stack of task 0 is manually set: when iret is executed, if a privileged change occurs, iret also restores the stack base address and stack top pointer in the stack, then in move_to_user_mode, in LINUX, manually press the base address and top pointer of the user stack of task 0 into the stack, and press eflag, cs, and ip of task 0 into the stack. Pay attention to the SS pushed in at this time, although CS also points to the segment before the mode conversion, it includes privileged-level information, so when cs goes out of the stack, it finds its privileged level (3) different from the current (0), and lower than the current, so the new Stack's SS and esp will be released again. In this way, code execution is handed over to task 0 in user mode, and Task 1 is created in task 0 .......
Copy from http://blog.csdn.net/chenglian_999/article/details/4785720
In the past, when I was reading about Linux principles, I always encountered kernel stacks and user stacks. Today, I have taken a look at the use of the stack in Linux separately. Here I will make a summary.
I. linux Stack
The Linux0.11 core involves four types of stacks: the temporary stack during system boot, the stack used for Kernel initialization, the kernel state stack, and the user State stack.
1) Temporary stack during system guidance
In boosetct, when it moves itself to 0x90000, it will set a temporary stack, with 0x90000 as the segment address, 0xff00 as the offset address.
This stack is also used in Setup as a temporary stack for system guidance.
2) stack used for Kernel Initialization
When the head is executed in protection mode, the stack segment address is set at the data segment address of the kernel (in this case, the ss does not store the specific base address, but the stack segment selection character, of course, it is also the data segment selection character). The stack top Pointer Points to the top of the user_stack array and keeps one page of memory as the stack.
The stack set in head is used when you enter main for system initialization. However, after move_to_user_mode (), the code is switched to task 0 and Task 1 is created in task 0, task 1 uses its own stack (indicated in tss in Task 1's task_struct ). Task 0 still uses the previously used stack as its user stack.
3) User State Stack
When a user process is running, it involves stack operations such as process calling. At this time, it uses the user stack, and the base address of the user stack is the Data Segment Base Address of the task, the top pointer of the stack is located at the top of the linear address space (just near the top, where command line parameters and environment variables are stored at the top ). The actual physical pages used are allocated during "Copy at write time.
4) core State Stack
When a user process executes the kernel code, such as a system call, the core State stack is used, and each task has its own core State stack, its physical location is located on the page where task_struct of this task is located. The specific base address and stack top pointer are specified by ss0 and esp0 in tss.
Ii. switching between user-mode stack and kernel-mode Stack
When a task is switched, there is no doubt that the stack will be switched. The stack used by the task will change. The stack is switched between the user stacks of the two tasks, it is done through TSS. When the CPU privilege level changes, the stack it uses also changes, at this time, the switching is the switching between the user stack and the core stack of the same task. Next I will talk about the general process of switching between user-mode stack and core-mode stack.
All interrupts in the 0.11 core belong to the kernel code. Therefore, if an interruption occurs, the task is running the user code, that is, in the user State, then, the task changes from user State (privileged level 3) to kernel state (privileged level 0). At this time, switching from user State stack to core State stack occurs. The CPU first obtains the kernel state stack address from the TSS in task_struct, And then pushes the segment address and top pointer of the user State stack to the core State stack, then, the eflags, es, and ip are pushed into the stack. Then execute the interrupt processing program. At this time, the kernel state stack is used. After the interrupt processing program is executed, iret will return to the user mode to restore the user stack and eflags. In this way, the user stack is used again.
Note: in kernel mode, when the cpu is interrupted, the stack operations of ss and esp are not performed on the cpu.
Iii. Stack switching in task 0
At the beginning of the kernel, code execution will be handed over to task 0 (move_to_user_mode). The Kernel stack of task 0 is located at the end of the page where the data structure of its own task is located, the user stack of task 0 is the stack used when the CPU enters the protection mode, that is, usr_stack []. The user stack of task 0 is manually set: when iret is executed, if a privileged change occurs, iret also restores the stack base address and stack top pointer in the stack, then in move_to_user_mode, in LINUX, manually press the base address and top pointer of the user stack of task 0 into the stack, and press eflag, cs, and ip of task 0 into the stack. Pay attention to the SS pushed in at this time, although CS also points to the segment before the mode conversion, it includes privileged-level information, so when cs goes out of the stack, it finds its privileged level (3) different from the current (0), and lower than the current, so the new Stack's SS and esp will be released again. In this way, code execution is handed over to task 0 in user mode, and Task 1 is created in task 0 .......