2019-2020-1 20199320 "Linux kernel principle and Analysis" in the third week of work

The second chapter how the operating system works

First, a virtual x86-CPU hardware platform

  1. Enter the following command in the laboratory building environment:

    > cd ~/LinuxKernel/linux-3.9.4
    > qemu -kernel arch/x86/boot/bzImage

    FIG Results obtained:

  2. QEMU output window is not displayed, the kernel starts to facilitate understanding of the effect, re-enter the following command:

    > cd ~/LinuxKernel/linux-3.9.4
    > rm -rf mykernel
    > patch -p1 < ../mykernel_for_linux3.9.4sc.patch
    > make allnoconfig
    > qemu -kernel arch/x86/boot/bzImage

    After the make, can clearly see the effect in the kernel starts QEMU window:

  3. Entering mykernel directory, and can see the code mymain.c myinterrupt.c QEMU outputting the content as:

    mymain.c executive function output once per 100,000 "my_start_kernel here i", while an interrupt handler context, periodically generates an interrupt the clock signal, the code can be triggered in myinterrupt.c (below), an output ">>> my_timer_hender here <<<<".

Second, construct a simple operating system kernel based on the mykernel

  1. Increasing a mypcb.h header, used to define the process control block, i.e. the definition of the process structure, specific code, please click "mypcb.h";

  2. Of mymain.c be modified to be the entrance mykernel kernel code, it is responsible for initializing the various components of the kernel. Now for some key code analysis:

    • The first process to start
     /* start process 0 by task[0] */
        pid = 0;
        my_current_task = &task[pid];
     asm volatile(
         "movl %1,%%esp\n\t"     /* 将进程原堆栈栈顶的地址(这里是初始化的值)存入ESP寄存器 */
         "pushl %1\n\t"          /* 将当前EBP寄存器值入栈 */
         "pushl %0\n\t"          /* 将当前进程的EIP(这里是初始化的值)入栈 */
         "ret\n\t"               /* ret命令正好可以让入栈的进程EIP保存到EIP寄存器中 */
         : 
         : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)   /* input c or d mean %ecx/%edx*/
     );
    } 

    FIG specifically as follows:

    PS: Here used AllDel vi editor, enter the enter :1,$dthe end of the first row to delete, delete other Click here to visit.

  3. For interrupt.c modifications, mainly to increase the process of code switching, it is critical to analyze the code:

    • 0 to start the process to begin executing code my_process (void) function.
    if(next->state==0)   /*next->state==0对应进程next对应进程曾经执行过。*/
    {
        //进行进程调度关键代码。
        asm volatile(
            "pushl %%ebp\n\t"   /*保存当前EBP到堆栈中。*/
            "movl %%esp,%0\n\t" /*保存当前ESP到当前PCB中。*/
            "movl %2,%%esp\n\t" /*将next进程的堆栈栈顶的值存到ESP寄存器。*/
            "movl $1f,%1\n\t"   /*保存当前进程的EIP值,下次恢复进程后将在标号1开始执行。*/
            "pushl %3\n\t"      /*将next进程继续执行的代码位置(标号1)压栈。*/
            "ret\n\t"           /*出栈标号1到EIP寄存器。*/
            "1:\t"              /*标号1,即next进程开始执行的位置。*/
            "pop1 %%ebp\n\t"    /*恢复EBP寄存器的值。*/
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        );
        my_current_task=next;
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
    }
    else
    {
        next-state=0;
        my_current_task=next;
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
    /*转换到新的进程中*/
        asm volatile(
          "pushl %%ebp\n\t"   /*保存当前EBP到堆栈中。*/
          "movl %%esp,%0\n\t" /*保存当前ESP到当前PCB中。*/
          "movl %2,%%esp\n\t" /*载入next进程的栈顶地址到ESP寄存器。*/
          "movl %2,%%ebp\n\t" /*载入next进程的堆栈基地址到EBP寄存器*/
          "movl $1f,%1\n\t"   /*保存当前EIP寄存器值到PCB,这里$1f是指上面的标号1。*/
          "push %3\n\t"       /*把即将执行的进程的代码入口地址入栈。*/
          "ret\n\t"           /*出栈进程的代码入口地址到EIP寄存器。*/
          : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
          : "m" (next->thread.sp),"m" (next->thread.ip)
        );
    }

    Third, the summary

    This chapter With the Linux kernel part of the source code to simulate the computer 3 magic (stored program computer, the function call stack, interrupt), the analysis part of a serious compilation of source code, a basic understanding of the process of switching, a process during execution, if when there is need to schedule other processes, you must first save the context of the current process, when the process is scheduled to be restored to the context, in order to achieve concurrent execution process, greatly improving the operating efficiency of the computer.

Guess you like

Origin www.cnblogs.com/liangxu111/p/11604983.html