• 操作系统工作流程


    计算机有三个关键性机制:存储程序计算机,堆栈机制和中断机制
    第一章中已经重点学习了存储程序的计算机,接下来我们重点学习堆栈机制和中断机制。

    堆栈机制

    堆栈机制是高级语言可以实现的基础机制,是C语言程序运行时必须使用的记录函数调用路径和参数存储的空间,他的具体作用有:记录函数调用框架,传递函数参数,保存返回值的地址,提供函数内部局部变量的存储空间等
    堆栈相关的寄存器有:
    1.ESP(堆栈指针寄存器)以及EBP(记录当前函数调用基址的基址指针寄存器)
    2.CS:EIP:总是指向下一条的指令地址。顺序执行时,总是指向地址连续的下一条指令;跳转/分支执行时,CS:EIP的值会根据程序需要被修改。
    3.EAX:保存返回值。如果有多个返回值,则返回一个内存地址。
    堆栈相关操作:
    push:栈顶地址减少4个字节,并将操作数放入栈顶存储单元
    pop:栈顶地址增加四个字节,并将栈顶存储单元的内容放入操作数
    call:将当前CS:EIP的值压入栈顶,CS;EIP指向被调用函数的入口地址
    ret:从栈顶弹出原来保存在这里的CS:EIP的值,放入CS:EIP的值
    enter和leave:一步对函数调用堆栈框架的建立和拆除进行封装
    参数传递;从左到右依次压栈

    中断机制实验

    虚拟X86的cpu硬件平台搭建

    本次实验在实验楼环境中进行,实验代码如下

    cd LinuxKernel/linux-3.9.4
    rm -rf mykernel
    patch -p1 < ../mykernel_for_linux3.9.4sc.patch
    make allnoconfig
    make #编译内核
    qemu -kernel arch/x86/boot/bzImage
    

    搭建起来后内核的启动效果如下
    实验楼

    在mykernel基础上完成一个简单的时间片轮转多道程序

    在前面试验的基础上cd mykernel ,增加一个mypcb.h的头文件
    mypcb.h

    #define MAX_TASK_NUM        4
    #define KERNEL_STACK_SIZE   1024*8
    /* CPU-specific state of this task */
    struct Thread {
        unsigned long       ip;  
        unsigned long       sp;  
    };
    
    typedef struct PCB{
        int pid;                
        volatile long state;   
        char stack[KERNEL_STACK_SIZE];
        /* CPU-specific state of this task */
        struct Thread thread;
        unsigned long   task_entry;  
        struct PCB *next; 
    }tPCB;//pcb结构体定义
    
    void my_schedule(void);
    

    修改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];  //PCB的数组task
    tPCB * my_current_task = NULL; //当前task指针
    volatile int my_need_sched = 0; //是否需要调度
    
    void my_process(void); 
    
    void __init my_start_kernel(void) //mykernel内核代码入口
    {
        int pid = 0;
        int i;
        /* 初始化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其他进程 */
        for(i=1;i<MAX_TASK_NUM;i++)
        {
            memcpy(&task[i],&task[0],sizeof(tPCB));
            task[i].pid = i;
            task[i].state = -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];
        }
        
        /* 用task[0]开始0号进程 */
        pid = 0;
        my_current_task = &task[pid];
        asm volatile(
            "movl %1,%%esp
    	"    
            "pushl %1
    	"        
            "pushl %0
    	"         
            "ret
    	"               
            "popl %%ebp
    	"
            : 
            : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)   /* input c or d mean %ecx/%edx*/
        );
    }   
    
    void my_process(void)
    {
        int i = 0;
        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);
            }     
        }
    }
    

    修改myinterrupt.c文件如下:

    /*
     *  linux/mykernel/myinterrupt.c
     *
     *  Kernel internal my_timer_handler
     *
     *  Copyright (C) 2013  Mengning
     *
     */
    #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 */
        {        
        	my_current_task = next; 
        	printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid);  
        	/* 进程切换 */
        	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)
        	); 
        }  
        return;	
    }
    

    重新make编译后qemu窗口查看,得到如下结果:
    实验楼
    实验楼

    遇到问题
    1.make编译出错
    2.重新启动后内核启动效果没有变化
    解决方法
    1.在mykernel目录下修改代码
    2.换了一组新的内核代码,最开始使用的代码切换进程过程不完成

  • 相关阅读:
    前端知识之HTML内容
    数据库之表操作
    初识数据库
    Python连接MySQL数据库之pymysql模块使用
    并发编程之协程
    个人库
    Apache conf配置文件 allow deny order files directory location解释,re(正则表达式)入门速成
    Arduino在vscode中输出乱码解决方案及解释
    UOJ Judgement Failed惨痛教训
    vscode导出插件列表
  • 原文地址:https://www.cnblogs.com/20189224sxy/p/9837701.html
Copyright © 2020-2023  润新知