After a long period of development, many users are familiar with the Linux system. Here I will share with you the four types of stack used in Linux.
1. System-guided initialization of temporarily used stacks
2. After the protection mode is enabled, the stack is used by kernel programs. This stack is also the user State stack used by task 0.
3. Each task is called by the system and the stack used to execute the kernel program is called the kernel state stack of the task. Each task has its own independent kernel state stack.
4. The stack where the task is executed in user mode, located in the task process). The logical address space is near the end.
The main reason for using multiple stacks or using different stacks in different situations
(1) As the instance enters the protection mode, the CPU changes the memory addressing access mode. Therefore, you need to reset the stack area.
(2) In order to solve the protection problem caused by stack sharing with different CPU privileges, different stacks are required for executing level 0 kernel code and executing level 3 user code. When a task enters the kernel state, it uses the stack pointer TSS with the privilege level 0 given in its tss segment. ss0.tss. esp0 is the kernel stack. The original user Stack pointer is stored in the kernel stack. When the user State is returned from the kernel state, the user State stack will be restored.
The following are the descriptions.
Bootsect. s, setup. s)
When the bootsect code is loaded to the physical memory 0x7c00 by the rom bios boot, the stack segment is not set, and the program does not use the stack until the bootsect is moved to 0x9000: 0, stack segment register SS is set to 0x9000, stack pointer esp register is set to 0xff00, so the stack at 0x9000: 0xff00 boot/bootsect. s L61, 62) setup. s also uses this stack
Head. s, L31 when entering protection mode)
In this case, the stack segment is set to the kernel data segment 0x10), and the stack pointer esp is set to point to the user_stack array sched. c L67 ~ 72) at the top of the stack, one page of memory is retained for use as the stack.
Main. c)
Before executing the move_to_user_mode () code to hand over control to task 0, the system always uses the above stack. After executing move_to_user_mode (), main. c code is switched to execute in task 0. By executing the fork () System Call, main. in c, init () will be executed in Task 1 and the stack of Task 1 will be used, while main () itself will be "switched" to task 0, we are still using the above Kernel Program's own stack as the user-state stack of task 0.
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.
Apart from being at different CPU privilege levels, the main difference between the two stacks is that the kernel-state stack of the task is very small, and the stored data cannot exceed 4096 bytes at most, the user-mode stack of the task can be extended in the user's 64 MB space.
When running in user mode
In addition to task 0 and Task 1, each task has its own 64 MB address space. When a task process is created, its user-mode Stack pointer is set near the end of its address space, which is used by applications during user-mode running, the actual physical address memory is determined by the CPU paging mechanism.
Run in kernel mode
Each task has its own kernel state stack, Which is used during the execution of the task in the kernel code. The position in the linear address is specified by the ss0 and esp0 fields in the TSS segment of the task. The task kernel state stack is set at the end of the page where the task data structure is located, that is, the task data structure task_struct of the task is placed on the same page. For more information, see kernel/fork. c L93
P-> tss. esp0 = PAGE_SIZE + (long) p;
P-> tss. ss0 = 0x10
* Why can a page of memory applied from the primary storage area for saving the task data structure be set as data in the kernel data segment? That is to say, why can tss. ss0 be 0x10?
The user's kernel state still belongs to the kernel data space, in the head. in s, set the kernel code segment and data segment descriptor. The segment length is set to 16 MB. The length value is the maximum physical memory length head supported by the Linux kernel. (s, comments starting from 110). Therefore, the kernel code can address any location in the entire physical memory range, including the primary memory, whenever a task executes a kernel program and needs to use its kernel stack, the CPU uses the TSS structure to set its kernel state stack to a tss. ss0 and tss. the esp0 values constitute
Task 0 idle process idle) and Task 1 initialization process init) Stack
The code segments and data segments of task 0 and Task 1 are the same. The limit length is 640KB, but they are mapped to different linear address spaces. The base address of task 0 ranges from linear address 0, the base address of Task 1 starts from 64 MB, but all of them are mapped to the physical address 0 ~ In the 640KB range, this address is where the kernel code and basic data are stored. After move_to_user_mode () is executed, the kernel state stacks of task 0 and Task 1 are located at the end of the page where the data structure of each task is located, and the user State stack of task 0 is the stack used after entering protection mode, that is, the location of the user_stack [] array. Because Task 1 copies the user stack of task 0 at creation, task 0 and Task 1 share the same user stack space at the beginning, however, when Task 1 starts to run, the replication mechanism for writing will allocate a primary storage page for Task 1 as the stack space. At this time, task 1 starts to use its own user stack memory page. Therefore, the stack of task 0 must be kept clean before Task 1 actually starts to use, that is, task 0 cannot use the stack at this time, to ensure that the copied stack page does not contain data of task 0
In this way, you will learn how to use four types of stack knowledge in Linux.
- Setting Linux to delete automatic backup saves system space
- Configure and use Samba to share directories in Linux
- Abandon the Linux operating system and select Microsoft?
- Better online shopping for Linux Online Banking
- Business empire's "cloud computing" Linux operating system