How the Linux kernel Analysis operating system works

Source: Internet
Author: User
Tags prev switches volatile

In this week's course, Meng teacher mainly explained how the operating system is working, I wrote this blog according to their own understanding, please the small partners to correct.

I. Summary of knowledge points

1. Three Magic Weapon

Storage program computer: A logical framework for all computer fundamentals.

Stack: A starting point for a high-level language, a function call requires a stack mechanism.

Interrupt mechanism: The basis of multi-channel system is the key to improve the efficiency of computer.

2. Function Call stack

A stack is a space that the C language program must run with a record call path and parameters, that is, a lot of functionality has been integrated within the CPU.

The stack contains the following elements:

Function call Framework

Passing parameters

Save return Address (%EAX)

Provide local variable space and so on

The C language compiler has a set of rules for using stacks.

Stack-related registers:

ESP, stack pointer

EBP, Base point pointer

Stack operation:

Push, stack top address reduced by 4 bytes (32 bits)

Pop, stack top address increased by 4 bytes Note: EBP is used in the C language to record the current function call base.

Other key registers:

CS:EIP: The next instruction pointing to an address continuous

Sequential execution: Always points to the next instruction in consecutive addresses

Jump/Branch: When executing such an instruction, the value of CS:EIP will be modified according to the program needs

Call: Pushes the value of the current CS:EIP into the top of the stack, Cs:eip points to the address of the called function

RET: Eject the value of the CS:EIP from the top of the stack that was originally saved here, and put it in CS:EIP when an interrupt occurs

Second, Mykernel thin kernel program experiment

1. The experimental process

First, use the following two commands

1 cd linuxkernel/linux-3.9. 4 2 Qemu-kernel arch/x86/boot/bzimage

The graph shows that each my_ Start_ kernel function is executed once or two times, and the My_ time_ hander function executes once.

mymain.c--the only process in the system. Mystartkernel before the hardware initialization, followed by the entire operating system entrance, the start of the operating system. Where the code completes the work is 10,000 times per loop, printing a sentence.

myinterrupt.c--Time Interrupt Handler

Each time a clock interrupt is executed, each clock interrupt is called PRINTK and output.

The above experimental environment is just a simulation of the clock interrupt, has always been a process of run. You need to add the following code to start the process switching schedule.

you need to re-update the file under Mykernel: MYINTERRUPT.C, primarily responsible for interrupts and process switching, MYMAIN.C, initializes the system environment , Mypcb.h describes the definition of the process Control block.

Re-compile to the Linux3.9.4 directory, using the following command:

1 Make allnoconfig 2  3 qemu-kernel arch/x86/boot/bzimage

2. Process start-up and process switching mechanism

Start of process: starting from Void__init my_start_kernel (void), the function is preceded by some initialization work, including the fork for the process. Each process initially, in addition to the PID, for the defined PCB, the other is the same, a linked list to organize the processes. The main function of the embedded assembly code is to initialize the No. 0 process, the ESP of process No. 0 is written to the ESP register, because the process has not yet begun to execute, so the process of ESP and EBP should point to the bottom of the stack (empty stack), in addition, the NO. 0 process of the EIP here has pointed to the My_ Process Code snippet address, after RET, begins execution of Route No. 0.

1 void__init My_start_kernel (void)  2 {  3     intPID =0; 4     inti; 5     /*Initialize Process 0*/  6Task[pid].pid =pid; 7Task[pid].state =0;/*-1 unrunnable, 0 runnable, >0 stopped*/  8Task[pid].task_entry = Task[pid].thread.ip = (unsignedLong) my_process; 9TASK[PID].THREAD.SP = (unsignedLong) &task[pid].stack[kernel_stack_size-1]; TenTask[pid].next = &Task[pid];  One     /*Fork More Process*/   A      for(i=1; i<max_task_num;i++)   -     {   -memcpy (&task[i],&task[0],sizeof(TPCB));  theTask[i].pid =i;  -Task[i].state =-1;  -TASK[I].THREAD.SP = (unsignedLong) &task[i].stack[kernel_stack_size-1];  -Task[i].next = task[i-1].next;  +task[i-1].next = &Task[i];  -     }   +     /*START process 0 by task[0]*/   APID =0;  atMy_current_task = &Task[pid];  -Asmvolatile(   -         "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*/   in         "popl%%ebp\n\t"   -         :    to:"C"(TASK[PID].THREAD.IP),"D"(TASK[PID].THREAD.SP)/*input C or D mean%ecx/%edx*/   +     );  -}

process switching: through the schedule () function for process switching, when a clock interrupt occurs, the interrupt handler will set the flag to be dispatched to 1, while at the same time each currently executing process is polled, if flag is set to 1, The process switch is started. CASE1: When the process starts to switch, it is a process that switches to a process state that is already running. Before switching the process, save 1f to Prev's EIP, 1f refers to the address labeled 1, after RET, the next EIP stack, the start to execute next process.

1 if(Next->state = =0)/*-1 unrunnable, 0 runnable, >0 stopped*/  2    {  3     /*switch to Next process*/  4Asmvolatile(     5         "PUSHL%%ebp\n\t"       /*Save EBP*/  6         "MOVL%%esp,%0\n\t"     /*Save ESP*/  7         "MOVL%2,%%esp\n\t"     /*Restore ESP*/  8         "MOVL $1f,%1\n\t"       /*Save Eip*/    9         "PUSHL%3\n\t"   Ten         "ret\n\t"               /*Restore EIP*/   One         "1:\t"                  /*Next process start here*/   A         "popl%%ebp\n\t"   -:"=m"(PREV-&GT;THREAD.SP),"=m"(prev->Thread.ip) -:"m"(NEXT-&GT;THREAD.SP),"m"(next->Thread.ip) the     );  -My_current_task =Next;  -PRINTK (Kern_notice">>>switch%d to%d<<<\n",prev->pid,next->pid);  -}

CASE2: The process switches to a new process. Since this switch process is a new process, so here the state is set to 0 (running status), where the 1f in the MOVL $1f,%1\n\t in the process again dispatched back, the walk is Case1 Branch code block, so case2 in the There is no code block labeled 1, and there is no action for the pop ebp.

1 Else  2     {  3Next->state =0; 4My_current_task =Next; 5PRINTK (Kern_notice">>>switch%d to%d<<<\n",prev->pid,next->pid); 6         /*switch to New process*/  7Asmvolatile(     8             "PUSHL%%ebp\n\t"       /*Save EBP*/  9             "MOVL%%esp,%0\n\t"     /*Save ESP*/  Ten             "MOVL%2,%%esp\n\t"     /*Restore ESP*/   One             "MOVL%2,%%ebp\n\t"     /*Restore EBP*/   A             "MOVL $1f,%1\n\t"       /*Save Eip*/     -             "PUSHL%3\n\t"    -             "ret\n\t"               /*Restore EIP*/   the:"=m"(PREV-&GT;THREAD.SP),"=m"(prev->Thread.ip) -:"m"(NEXT-&GT;THREAD.SP),"m"(next->Thread.ip) -         );  -}

Iii. understanding of how the operating system works

Operating system is to manage all the hardware resources of computer system including software resources and data resources, control program operation, improve human-machine interface, and support other application software, so as to maximize the function of all resources of computer system, and provide users with convenient and effective service interface. The Linux kernel executes from a function that initializes the context, that is, the Start_kernel function, creates many processes or fork several processes, and when interrupts occur, such as when a clock interrupt occurs in Mykernel, the OS then schedules the processes. Select a suitable process in the dispatch queue, which holds the registers information of the previous process by the CPU and the kernel stack (the thread structure in the process description block holds most of the CPU registers, but these general-purpose registers such as EAX, EBX, etc. are saved and stacked). The EIP points to the address to be executed by the process to be dispatched and begins execution. In this course, I am still not familiar with the contents of the embedded assembly, so in order to understand Linux more deeply understanding of the assembly and the operating system should be strengthened.

Liu Shuai

Original works reproduced please indicate the source

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

How the Linux kernel Analysis 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.