Linux kernel stack user stack toggle "Go"

Source: Internet
Author: User

Transferred from: http://www.kerneltravel.net/kernel-book/%E7%AC%AC%E5%9B%9B%E7%AB%A0%20%E8%BF%9B%E7%A8%8B%E6%8F%8F%E8%BF%B0/4.4.1.htm

4.4.1 Process Kernel Stack

Each process has its own kernel stack. When the process enters the kernel state from the user state, the CPU automatically sets the kernel stack of the process, that is, the CPU loads the kernel stack pointer esp from the TSS in the task State segment (see the section on process switching in the next chapter).

The distribution of the X86 kernel stack is shown in 4.2:

Figure 4.2 Distribution map of the kernel stack

In an Intel system, the stack starts at the end and grows in the direction of the memory area. The kernel stack of the process is always empty after the user state has just switched to the kernel state, so the ESP register points directly to the top of the memory area. In Figure 4.2, after switching from the user state to the kernel state, the ESP register contains an address of 0x018fc00. The process descriptor is stored at the address starting from 0x015fa00. As long as the data is written into the stack, the value of ESP is decremented.

The following federated structure is defined in/include/linux/sched.h:

Union task_union {

struct TASK_STRUCT task;

unsigned long stack[2408];

};

From this structure, we can see that the kernel stack occupies 8kb of memory area. In fact, the memory of the TASK_STRUCT structure of the process is allocated dynamically by the kernel, rather, the kernel does not allocate memory to task_struct at all, but allocates only 8K of memory to the kernel stack and uses it as part of the task_struct.

The TASK_STRUCT structure accounts for about 1K of bytes, and its specific number is related to the kernel version because the different versions have slightly different domains. Therefore, the size of the kernel stack cannot exceed 7K, otherwise the kernel stack will overwrite the TASK_STRUCT structure, causing the kernel to crash. However, the 7K size is sufficient for the kernel stack.

Putting the TASK_STRUCT structure together with the kernel stack has the following benefits:

· The kernel can easily and quickly find this structure, described in pseudo-code as follows:

Task_struct = (struct task_struct *) Stack_pointer & 0xffffe000

· Avoid allocating additional memory dynamically when creating a process

· The start address of the TASK_STRUCT structure always starts at the boundary of the page size (page_size).

Linux kernel stack user stack toggle "Go"

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.