The relationship between kernel stack and user space stack of Linux-2.6-32

Source: Internet
Author: User

 

1. process stack

When the kernel creates a process, a colleague who creates task_struct will create a stack for the process. Each process has two stacks, one of which exists in the user space and the other exists in the kernel space. When a process is running in the user space, the content in the CPU Stack pointer register is the user stack address and the user stack is used. When the process is in the kernel space, the content in the CPU Stack pointer register is the address of the kernel stack space, using the kernel stack.

 

2. switching between process user stack and kernel stack

When a process is in the kernel state due to interruptions or system calls, the stack used by the process also needs to be switched from the user Stack

To the kernel stack.

After a process falls into the kernel state, it first saves the address of the user State stack in the kernel stack, and then sets the stack pointer register content as the address of the kernel stack, this completes the conversion from the user stack to the kernel stack. When the process recovers from the kernel state to the user State, at the end of the kernel state, the address of the user stack stored in the kernel stack is restored to the stack pointer register. In this way, the conversion between the kernel stack and the user stack is realized.

So, we know that the address of the user stack is saved in the kernel stack when the kernel is switched to the user State, but when it falls into the kernel, how do we know the address of the kernel stack?

The key is that the kernel stack of the process is always empty when the process is switched from the user State to the kernel state. This is because, when a process is running in the user State, it uses the user stack. when the process is in the kernel state, the kernel stack stores the confidence of the process running in the kernel state, however, once the process returns to the user State, the information saved in the kernel stack is invalid and will be completely restored. Therefore, the kernel stack obtained every time the process falls into the kernel state from the user State is empty. Therefore, when a process falls into the kernel, you can directly send the stack top address of the kernel stack to the stack pointer register.

 

3. Kernel stack implementation

The implementation of the kernel stack in kernel-2.4 and kernel-2.6 is different.

In the kernel-2.4, the kernel stack is implemented as follows:

Union task_union {

Struct task_struct task;

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

};

The size of init_stack_size can only be 8 K.

When the kernel assigns a task_struct struct to each process, two consecutive physical pages are actually allocated. The bottom part is used as the task_struct struct and the top part is used as the stack. Use the current () macro to access the currently running process descriptor.

Note: At this time, the task_struct structure is in the kernel stack, and the actual available size of the kernel stack is about 7 kb.

 

The kernel stack is implemented in kernel-2.6 (kernel-2.6.32 ):

Union
Thread_union {

Struct thread_info
Thread_info;

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

};

The thread_size can be 4 K or 8 K, and thread_info occupies 52 bytes.

When the kernel stack is 8 K, thread_info increases from the end of the stack to the starting address of the memory. Therefore, the current macro in kernel-2.6 needs to be modified. The task associated with thread_info needs to be obtained through the task_struct field in the thread_info struct. For more details, refer to the implementation of the corresponding current macro.

Struct
Thread_info {

Struct task_struct * task;

Struct exec_domain
* Exec_domain;

_ U32 flags;

_ U32 status;

_ U32 CPU;

... ..

};

Note: The task_struct struct is no longer in the kernel stack space.

 

4. process context

When the program runs a system call or triggers an exception and falls into the kernel space, we call the kernel as the process execution and in the context of the process. That is, the process context is called the process context when the process is switched to the site, contains all the information of a process, including process control blocks (PCB), relevant program segments, and corresponding datasets.

 

5. Process Control Block:

The process control block is a static process in the memory. in Linux, task_struct is used to represent a process. The static descriptor of the process must ensure that a process obtains the CPU and enters the running state again, it can precisely continue the operation at the last running position. The related program segments, data, and CPU field information should be stored. The CPU field information mainly includes the basic data of internal registers and stacks.

Process control blocks can be divided into process description information, process control information, process-related resource information, and CPU information protection mechanism.

 

6. process switching

When the time slice of a process is used up or is preemptible, the process needs to allow the CPU to run for other processes, and the kernel needs to switch the process.

In Linux, the schedule () function is used to schedule processes. In the kernel/sched. c file, kernel-2.6.32 is implemented as follows:

Asmlinkage
Void _ sched schedule (void)

{

Struct task_struct * Prev, * next;

Unsigned long * switch_count;

Struct RQ * rq;

... ...

}

 

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.