Process kernel stack, user stack

Source: Internet
Author: User

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 . Each time the process gets into the kernel from the user state the kernel stack is empty (why?). ). 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.

Process kernel stack, user stack

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.