zoukankan      html  css  js  c++  java
  • 20189220 余超《Linux内核原理与分析》第三周作业

    操作系统如何工作的

    第二章的基础知识

    • 计算机的三大法宝:存储程序计算机,函数调用堆栈机制,中断。
    • 堆栈:堆栈是C语言程序运行时必须使用的几率函数条用路径和参数存储的空间,具体作用分为:记录函数条用的框架,传递函数参数,保存返回的地址,提供函数内部局部变量的存储空间等。
    • esp:是栈指针,是cpu机制决定的,push、pop指令会自动调整esp的值。
    • ebp:只是存取某时刻的esp,这个时刻就是进入一个函数内后,cpu会将esp的值赋给ebp,此时就可以通过ebp对栈进行操作,比如获取函数参数,局部变量等,实际上使用esp也可以。
    • 因为esp在函数运行时会不断的变化,所以保存一个一进入某个函数的esp到ebp中会方便程序员访问参数和局部变量,而且还方便调试器分析函数调用过程中的堆栈情况。前面说了,这个ebp不是必须要有的,你非要使用esp来访问函数参数和局部变量也是可行的,只不过这样会麻烦一些。
    • CS:EIP:总是指向下一条指令。
      顺序执行:总是执向地址连续的下一条指令
      跳转/分支:CS:EIP指向被调用函数的入口地址
      call:将当前CS:EIP的值压入栈顶,CS:EIP指向被调用函数的入口地址
      ret:从栈顶弹出来原来保存在这里的CS:EIP的值,放入CS:EIP中

    内嵌汇编

    • 语法:asm(

                       汇编语句模板: 
                       输出部分 : 
                       输入部分 : 
                       破坏描述部分:
                                             ); 
      

    即格式为asm(“statements”:output_regs:input_regs:clobbered_regs);

    • 内嵌汇编关键词asm volatile的括号内部第一部分是汇编代码,这里的汇编代码和之前学习的汇编代码有一点点差异,体现在%转义符号。寄存器前面会多一个%的转义符号,有两个%;而%加一个数字这表示第二部分输出,第三部分输入以及第四部分破坏描述。

    多道程序内核代码内核分析

    我们完成内核调度的主要实现主要是靠三个文件完成:mypcb.h、mymain.c、myinterrupt.c。我们就对他们各自的代码做详细的分析:
    mypcb.h:

    #define MAX_TASK_NUM 4
    #define KERNEL_STACK_SIZE 1024*2 # unsigned long
    /* CPU-specific state of this task */
    struct Thread {
    unsigned long	ip;
    unsigned long	sp;
    };
    typedef struct PCB{
    int pid;
    volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
    unsigned long stack[KERNEL_STACK_SIZE];
    /* CPU-specific state of this task */
    struct Thread thread;
    unsigned long	task_entry;
    struct PCB *next;
    }tPCB;
    void my_schedule(void)
    
    • 这段代码主要用于定义进程控制块,也就是进程结构体的定义。struct Thread中定义了两个变量ip和sp,他们是分别用来存储eip和esp。而struct PCB是用来表示进程控制块的,pid表示进程的id,state表示进程的状态,-1表示该进程未运行,0表示正在运行,大于0则表示停止。数组stack[]表示进程堆栈,也表示为内核堆栈,还有就是线程thread以及任务的入口task_entry。还有就是*next连接下一个进程,即用链表把他们链接起来。最后还声明了一个调度器my_schedule。而且最大进程数量MAX_TASK_NUM=4。

    mymain.c:

    #include <linux/types.h>
    #include <linux/string.h>
    #include <linux/ctype.h>
    #include <linux/tty.h>
    #include <linux/vmalloc.h>
    #include "mypcb.h"
    tPCB task[MAX_TASK_NUM];
    tPCB * my_current_task = NULL;
    volatile int my_need_sched = 0;
    void my_process(void)
    void __init my_start_kernel(void)
    {
        int pid = 0;
        int i;
        /* Initialize process 0*/
        task[pid].pid = pid;
        task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
        task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
        task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
        task[pid].next = &task[pid];
        /*fork more process */
        for(i=1;i<MAX_TASK_NUM;i++)
        {
            memcpy(&task[i],&task[0],sizeof(tPCB));
            task[i].pid = i;
    	//*(&task[i].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
    	task[i].thread.sp = (unsigned long)(&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] */
        pid = 0;
        my_current_task = &task[pid];
    	asm volatile(
        	"movl %1,%%esp
    	" 	/* set task[pid].thread.sp to esp */
        	"pushl %1
    	" 	        /* push ebp */
        	"pushl %0
    	" 	        /* push task[pid].thread.ip */
        	"ret
    	" 	            /* pop task[pid].thread.ip to eip */
        	: 
        	: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)	/* input c or d mean %ecx/%edx*/
    	);
    } 
    
    int i = 0;
    void my_process(void)
    {    
        while(1)
        {
            i++;
            if(i%10000000 == 0)
            {
                printk(KERN_NOTICE "this is process %d -
    ",my_current_task->pid);
                if(my_need_sched == 1)
                {
                    my_need_sched = 0;
            	    my_schedule();
            	}
            	printk(KERN_NOTICE "this is process %d +
    ",my_current_task->pid);
            }     
        }
    }
    
    • 这段代码主要是介绍了内核的初始化和0号进程的启动,首先初始化了pid、state、task_entry、thread.ip、thread.sp等信息。进程的入口则是my_process,此时系统中只有0号进程,所以该进程的next还是先指向自己。然后用一个for循环也迅速初始化了其余的三个进程,当他们都处于未运行的状态,且都用链表连接起来的。my_current_task则是记录现在正在运行的进程,汇编代码则是启动0号进程。让esp指向0号进程栈顶(此时也是栈底).,因为是第一次执行,所以esp和ebp指向的是同一个地点。把当前thread.ip入栈,也就是my_process 入口地址。跳转到函数my_process. 除非 my_process 返回, 否则 “popl %%ebp ” 永远不会执行。其中my_need_sched是用来控制调度的标志,在my_process的代码中我们可以看到每执行一千万次打印一次,如果my_need_sched等于1,则执行进程切换的函数my_schedule。

    myinterrupt.c:

    #include <linux/types.h>
    #include <linux/string.h>
    #include <linux/ctype.h>
    #include <linux/tty.h>
    #include <linux/vmalloc.h>
    #include "mypcb.h"
    
    extern tPCB task[MAX_TASK_NUM];
    extern tPCB * my_current_task;
    extern volatile int my_need_sched;
    volatile int time_count = 0;
    /*
     * Called by timer interrupt.
     * it runs in the name of current running process,
     * so it use kernel stack of current running process
     */
    void my_timer_handler(void)
    {
    #if 1
        if(time_count%1000 == 0 && my_need_sched != 1)
        {
            printk(KERN_NOTICE ">>>my_timer_handler here<<<
    ");
            my_need_sched = 1;
        } 
        time_count ++ ;  
    #endif
        return;     
    }
    
    void my_schedule(void)
    {
        tPCB * next;
        tPCB * prev;
    
        if(my_current_task == NULL 
            || my_current_task->next == NULL)
        {
            return;
        }
        printk(KERN_NOTICE ">>>my_schedule<<<
    ");
        /* schedule */
        next = my_current_task->next;
        prev = my_current_task;
        if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
        {
            /* switch to next process */
            asm volatile(   
                "pushl %%ebp
    	"       /* save ebp */
                "movl %%esp,%0
    	"     /* save esp */
                "movl %2,%%esp
    	"     /* restore  esp */
                "movl $1f,%1
    	"       /* save eip */ 
                "pushl %3
    	" 
                "ret
    	"               /* restore  eip */
                "1:	"                  /* next process start here */
                "popl %%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<<<
    ",prev->pid,next->pid);      
        }
        else
        {
            next->state = 0;
            my_current_task = next;
            printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid);
            /* switch to new process */
            asm volatile(   
                "pushl %%ebp
    	"       /* save ebp */
                "movl %%esp,%0
    	"     /* save esp */
                "movl %2,%%esp
    	"     /* restore  esp */
                "movl %2,%%ebp
    	"     /* restore  ebp */
                "movl $1f,%1
    	"       /* save eip */ 
                "pushl %3
    	" 
                "ret
    	"               /* restore  eip */
                : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
                : "m" (next->thread.sp),"m" (next->thread.ip)
            );          
        }   
        return; 
    }
    
    • 从这段代码中我们可以看到有两个中断函数:一个是时间中断处理函数my_timer_handler,再另一个就是调度处理函数my_schedule。在my_timer_handler的函数中,通过时钟计数发生1000次且my_need_sched的标志位不等于1就打印一次,并且将标志位重新置位成1,也即是设置时间片的大小,时间片用完了就重新设置一下时间片的调度标志。
    • 实验结果截图:

    下面重点分析一下两个正在运行的进程做切换的代码:

    asm volatile(     
                "pushl %%ebp
    	"       /* save ebp,保存当前进程的ebp */  
                "movl %%esp,%0
    	"     /* save esp,保存当前进程的esp到 prev->thread.sp*/  
                "movl %2,%%esp
    	"     /* restore  esp,读取下个进程的esp */  
                "movl %2,%%ebp
    	"     /* restore  ebp ,赋值ebp,使得esp=ebp*/  
                "movl $1f,%1
    	"       /* save eip ,保存eip,即存储1编号出的指针运行地址到prev->thread.eip中*/      
                "pushl %3
    	"           /*压入下个进程的ip*/  
                "ret
    	"               /* restore  eip,运行下个进程 */  
                : "=m" (prev->thread.sp),"=m" (prev->thread.ip)  
                : "m" (next->thread.sp),"m" (next->thread.ip)  
            );            
    
    • 首先从这段代码中,我们要知道prev->thread.sp表示当前进程的esp值,在代码中为%0,prev->thread.ip表示当前进程的eip值,在代码中表示为%1,next->thread.sp表示下一个进程的esp值,代码里面表示为%2,next->thread.ip表示下一个进程的eip值,代码里面表示的是%3。
    • 现在来分析进程切换的堆栈变化:
      1.初始状态如下:

    2.执行第一条语句"pushl %%ebp ",将当前进程ebp入栈,esp减4,并且保存好此时进程栈底的底部,也即是保存现场。我们可以看到此时CPU中的SP=esp_1,PC_=eip_1;CPU 按照 PC 指针,到存储器去取指令代码,CPU 按照 SP 指针,到存储器存取地址或数据。

    3.执行第二条语句"movl %%esp,%0 " ,这条语句的意义是将当前进程的栈顶指针esp存入当前进程的 thread.sp中。

    4.执行第三条语句"movl %2,%%esp ",%2代表的是下一个进程esp,也即是将下一个进程的esp的值赋值给当前的esp,栈顶指向了下一个进程的位置。此时CPU的SP=esp_2

    5.执行第四条语句 "movl %2,%%ebp ",在这个地方下一个进程是第一次执行,下一个进程的栈是空栈,所以下一个进程的esp=ebp,也即是这里的%2代表的是将下一个进程的ebp赋值给当前的ebp,堆栈指针指向了下一个进程的位置

    6.执行第五条语句 "movl $1f,%1 " ,存储当前进程eip,即进程再一次运行会在此处继续

    1. 执行第六条语句"pushl %3 " ,%3代表的是下一个进程eip,也即是将next->thread.ip压栈

    8.执行第七条语句"ret " ,将下一个进程中堆栈保存的next->thread.ip存到eip寄存器中

    总结

    • 操作系统首先初始化内核相关的进程,然后开始循环运行这些进程,进程间进行切换时,则利用内核堆栈所保存的每个进程的sp,ip即所对应的%esp,%eip寄存器中的值,对当前的进程的sp,ip即对应%esp,%eip寄存器的值进行保存(中断上下文),并用下一个进程的sp,ip的值赋值给%esp,%eip寄存器(进程间切换)
  • 相关阅读:
    pytest.4.Fixture
    pytest.3.Assert
    pytest.2.运行多个文件
    [LeetCode 378.] Kth Smallest Element in a Sorted Matrix
    priority_queue 自定义 comparator
    原地调整法查找数组元素
    [LeetCode 436.] Find Right Interval
    [LeetCode 611.] Valid Triangle Number
    二叉树Morris遍历
    用户态IO:DPDK
  • 原文地址:https://www.cnblogs.com/yuchao123/p/9853470.html
Copyright © 2011-2022 走看看