Linux kernel Analysis-how the operating system works

Source: Internet
Author: User
Tags volatile

"Peace of Blessing + Original works reproduced please specify the source +" Linux kernel analysis "MOOC course http://mooc.study.163.com/course/USTC-1000029000 "

first, the process of initialization

The operating system kernel boot entry function is void __init my_start_kernel (void);

Here is a simple definition of the two CPU states of a process:

struct Thread {
unsigned long IP; Indicates an EIP directive
unsigned long sp;//represents ESP, stack top pointer
};

Initializes the first process--pid=0 in this function; All processes take it as the parent process.

When the first process is initialized, the allocation process pid=0, specifying the top pointer of the stack, the command that initializes the PCB, the value of the IP-the entry address of the process my_process.

Process for running the first process

Set the process status to running, using the embedded assembler to make the process occupy the CPU

ASM volatile (
"Movl%1,%%esp\n\t"/* Set TASK[PID].THREAD.SP to ESP */
"PUSHL%1\n\t"/* Push EBP */
"PUSHL%0\n\t"/* Push TASK[PID].THREAD.IP */
"Ret\n\t"/* pop task[pid].thread.ip to EIP */
"Popl%%ebp\n\t"
:
: "C" (Task[pid].thread.ip), "D" (TASK[PID].THREAD.SP)/* input C or D mean%ecx/%edx*/
);

First the ESP jumps to the top of the stack of the process, pushes the process EBP into the stack (due to the first process Ebp==esp), and pushes the process's IP to the top of the stack. The RET instruction can then modify the value of the EIP so that the process obtains the CPU's command permissions. Then pop the EBP and restore the bottom of the stack.

Second, the process of switching

This small time slice rotation uses the clock interrupt method to dispatch. In the process of my_process, the flag of detecting interrupts, if any, the process is switched.

Process switching in two cases, one is a running process switch, the other is a process switch that is not running

A running process switch

Switch through the following embedded assembly, and then convert the next process into a running state

ASM volatile (
"PUSHL%%ebp\n\t"/* Save EBP */
"Movl%%esp,%0\n\t"/* Save ESP */
"Movl%2,%%esp\n\t"/* Restore ESP */
"Movl $1f,%1\n\t"/* Save EIP */
"Pushl%3\n\t"
"Ret\n\t"/* Restore EIP */
"1:\t"/* Next process start here */
"Popl%%ebp\n\t"
: "=m" (PREV->THREAD.SP), "=m" (PREV->THREAD.IP)
: "M" (NEXT->THREAD.SP), "M" (NEXT->THREAD.IP)
);

First save the original EBP,ESP,IP, save the value of ESP and the IP value by adding EBP to the stack, process CPU state.

Next, the ESP jumps to the top of the stack to be switched, pushes its IP value to the stack, then the RET command gives the EIP its CPU command, and finally the EBP equals the value of the original process EBP. That is, the bottom of the stack is unchanged.

There are no process switches running on the CPU

First, the transition process state is running and then switched through the following embedded assembly implementation

ASM volatile (
"PUSHL%%ebp\n\t"/* Save EBP */
"Movl%%esp,%0\n\t"/* Save ESP */
"Movl%2,%%esp\n\t"/* Restore ESP */
"Movl%2,%%ebp\n\t"/* Restore EBP */
"Movl $1f,%1\n\t"/* Save EIP */
"Pushl%3\n\t"
"Ret\n\t"/* Restore EIP */
: "=m" (PREV->THREAD.SP), "=m" (PREV->THREAD.IP)
: "M" (NEXT->THREAD.SP), "M" (NEXT->THREAD.IP)
);

First save the original EBP,ESP,IP, save the value of ESP and the IP value by adding EBP to the stack, process CPU state.

Next, the ESP jumps to the top of the stack to switch, EBP jumps to the bottom of the stack to switch, because the process is not running, so ebp=esp, the IP value of the stack, and then the RET instruction to the EIP to get its CPU command right.

The following is a picture of the kernel compilation complete:

Here is the picture of the process when switching

Iii. Summary

The operating system first enters the init boot kernel, completes the first No. 0 process when the kernel is started, and then creates the process as needed. And the process is switched according to a certain scheduling algorithm.

Linux kernel Analysis-how the operating system works

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.