Summary of kernel stacks and user stacks

Source: Internet
Author: User

Reprint to: "Http://www.cnblogs.com/longdouhzt/archive/2012/10/13/2722969.html"

Each process (including normal process and kernel process) address space is divided into user address space and the kernel address space two parts, on the 32-bit x86 machine, the scope of the user address space is 0~3g, the scope of the kernel address space is 3g~ 4G. For different processes, the user address space varies depending on the process, but the kernel address space for all processes is the same. For kernel processes, because they are always running in the kernel state, there is no user address space, and the corresponding tast_struct structure of the MM field is also assigned a value of NULL. The concept of the heap should only exist in the user address space of the process, so the kernel process is not a heap. Kernel threads can request memory at run time with Kmalloc or Vmalloc. The memory requested by Kmalloc or vmalloc can be used throughout the kernel. For example, kernel thread A has applied for a piece of memory a, as long as it passes the first address of the memory to another kernel thread B, the memory can also be used in B.

All processes, including kernel processes and normal processes, have a kernel stack that can be 4KB or 8KB on the x86 32-bit machine, which can be configured when the kernel is compiled. There are two uses for the kernel stack: 1) When the process is in the kernel state, that is, when the kernel is executing system calls on behalf of the process, the parameters of the system call are placed on the kernel stack, the kernel stack records the process's call chain in the kernel, and 2) when the Interrupt Service program interrupts the current process It will use the kernel stack of the currently interrupted process.
For a user process, it has both a stack in the user's address space and its own kernel stack. Kernel processes are only kernel stacks.

[Turn] http://blog.csdn.net/dongmianshu/article/details/5979244

------------------------------------

Process kernel stack, user stack

1. Stack of processes

When the kernel creates a process, the colleague who creates the task_struct creates the appropriate stack for the process. Each process will have two stacks, a user stack, exist in the user space, a kernel stack, exist in the kernel space. When the process runs in user space, the contents of the CPU stack pointer register are the user stack address, the user stack is used, and when the process is in kernel space, the contents of the CPU stack pointer register are the kernel stack space address, using the kernel stack.

2. Switching between the process user stack and the kernel stack

When a process falls into a kernel state because of an outage or a system call, the stack used by the process goes from the user stack to the kernel stack.

After the process is in the kernel state, the address of the user-state stack is stored in the kernel stack, then the contents of the stack pointer register are set to the address of the kernel stack, which completes the conversion of the user stack to the kernel stack, and when the process recovers from the kernel state to the user state, At the end of the kernel-state line, the address of the user stack stored in the kernel stack is restored to the stack pointer register. This enables the core stack and the user stack of the mutual transfer.

So, we know that the address of the user stack when it goes from the kernel to the user state is stored in the kernel stack when it is trapped in the kernel, but how do we know the address of the kernel stack when we get into the kernel?

The key is that the kernel stack of the process is always empty when the process goes from the user state to the kernel state. This is because when the process is running in the user state, the user stack is used, and when the process falls into the kernel state, the kernel stack holds the confidence that the process is running in the kernel state, but once the process returns to the user state, the information stored in the kernel stack is not valid and will be restored. So every time the process gets into the kernel from the user state, the kernel stack is empty. So when the process is in the kernel, the stack top address of the kernel stack is given directly to the stacking pointer register.

3. Implementation of kernel stacks

Kernel stacks are implemented differently in kernel-2.4 and kernel-2.6.

Inside the kernel-2.4 kernel, the implementation of the kernel stack is:

Union Task_union {

Struct task_struct task;

Unsigned long stack[init_stack_size/sizeof (long)];

};

Among them, the size of the init_stack_size can only be 8K.

When the kernel allocates task_struct structures for each process, it actually allocates two contiguous physical pages, the bottom being used as the task_struct struct, and the stack above the structure. Use the current () macro to access the currently running process descriptor.

Note: This time the TASK_STRUCT structure is inside the kernel stack, the actual size of the kernel stack is about 7 K.

The implementation of the kernel stack in kernel-2.6 is (kernel-2.6.32):

Union Thread_union {

Struct Thread_info Thread_info;

Unsigned long stack[thread_size/sizeof (long)];

};

The size of the thread_size can be 4K, or 8k,thread_info to 52bytes.

When the kernel stack is 8K, thread_info at the start address of this memory, and the kernel stack grows downward from the end of the stack. So at this point, the current macro in kernel-2.6 needs to be changed. The task that is associated with Thread_info is obtained by thread_info the task_struct domain in the struct body. Refer to the implementation of the corresponding current macro in more detail.

struct Thread_info {

struct Task_struct *task;

struct Exec_domain *exec_domain;

__U32 flags;

__U32 status;

__U32 CPU;

... ..

};

Note: The TASK_STRUCT structure is not already in the kernel stack space at this time.

Summary of kernel stacks and user stacks

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.