Task Stack in Linux

Source: Internet
Author: User

Linux uses four stacks:

1) stack temporarily used during system initialization;

2) There is only one stack (kernel stack) for the kernel program to use. It is located at a fixed position in the system address space and is also the user State stack of task 0;

3) Each task is called by the system and the stack used to execute the kernel program. This is called the kernel state stack of the task. Each task has its own independent kernel state stack;

4) The last is the stack where the task is executed in the user State, at the end of the address space of the task (process.


Task Stack
Each task has two stacks for execution of user-state and kernel-state programs, which are called user-state stacks and kernel-state stacks respectively. The main difference between the two stacks is that the core-state stack of the task is very small, so the amount of data stored cannot exceed (8096-task data structure) bytes at most, which is about 6 K bytes. However, the user-state stack of a task can be extended in the user's linear space.


When running in user mode
Each task (except task 0) has its own address space. When a task (process) is created, its user-mode Stack pointer is set at the end of its address space, the kernel state stack is set to the end of the page where the data structure of the task is located. This stack is always used by applications when running in user mode. The physical memory actually used by the stack is determined by the CPU paging mechanism. Since Linux implements the copy onwrite function, after a process is created, if the process and its parent process do not
When a stack is used, the two share the physical memory page corresponding to the same stack.


Run in kernel mode
Each task has its own kernel state stack, which is placed on the same page as the task data structure (task_struct) of each task. This is set by the fork () program in the kernel-level stack field (TSS. esp0 and TSS. ss0) of the task TSS segment when a new task is created. The kernel applies for memory for the new task to save the task_struct structure data, while the TSS structure (segment) is a field in task_struct. The task's kernel stack segment value TSS. ss0 is also set to the kernel data segment, while TSS. esp0 points to the end of the page for saving the task_struct structure.


Switch between task kernel state stack and user State Stack
When a task calls a system call, it enters the kernel and executes the kernel code. In this case, the kernel code will use the kernel state stack of the task for operations. When you enter the kernel program, the priority level changes (from user State to kernel state ), the stack segment, stack pointer, and eflags of the user State Stack are stored in the kernel state stack of the task. When iret is executed to exit the kernel program and return to the user program, the user State stack and eflags will be restored.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.