zoukankan      html  css  js  c++  java
  • Linux内核设计(第二周)——操作系统工作原理

    Linux内核设计(第二周)——操作系统工作原理

    一、学习笔记总结

    1.函数调用堆栈

    (1)、函数调用堆栈。

    堆栈是C语言程序运行时必须的一个记录调用路径和参数的空间。
    cpu内部已经集成好的功能,pop,push,enter……

    1. 函数调用构架

    2. 传递参数,通过堆栈

    3. 保存返回值,%eax

    4. 提供局部变量空间
      ……

    C语言编译器对堆栈的使用有一套自己的规则,功能相同,指令有区别。

    (2)、深入理解函数调用堆栈

    1. 堆栈相关的寄存器:

      %esp——堆栈指针
      %ebp——基址指针

    2. 堆栈操作

      push——栈顶地址减少
      pop——相反

    3. %ebp在C语言中用作记录当前函数调用基址

    4. 其他关键寄存器
      CS:eip:总是指向下一条的指令地址
      顺序执行、跳转|分支(cs:eip的值会根据程序的需求更改)、call、ret、发生中断时。

    5. 调用函数
      call指令:

    (1) 将eip中下一条指令的地址A保存在栈顶;
    (2) 设置eip指向被调用程序代码开始处。
    ret(return)指令:将地址A恢复到eip中

    (3)、传递参数与局部变量

    方法:gcc-g生成可执行文件,用objdump -S获得反汇编文件。

    2.利用Linux内核部分源代码分析存储程序计算机工作模型及时钟中断

    (1).mykernel实验平台涉及的思想

    三大法宝:

    1. 存储程序计算机

    2. 函数调用堆栈

    3. 中断

    当中断发生时,由CPU和内核代码共同实现了保存现场和恢复现场。
    把eip指向中断处理程序的入口,保存现场。

    二.利用mykernel实验模拟计算机硬件平台

    1.实验过程

    使用实验楼的虚拟机打开shell

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

    enter description here

    然后cd mykernel 您可以看到qemu窗口输出的内容的代码mymain.c和myinterrupt.c

    enter description here
    mymain.c文件关键代码部分
    enter description here
    myinterrupt.c文件关键代码部分
    enter description here

    2.代码分析

    (1)mymain.c

    /*
     *  linux/mykernel/mymain.c
     *
     *  Kernel internal my_start_kernel
     *
     *  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"
    
    tPCB task[MAX_TASK_NUM]; //声明一个PCB数组
    tPCB * my_current_task = NULL;  //声明当前task指针 
    volatile int my_need_sched = 0;  //是否需要调度标志
    
    void my_process(void);
    
    
    void __init my_start_kernel(void)
    {
        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;  /* 实际上是my_process*/
        task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
        task[pid].next = &task[pid];  // 定义堆栈的栈顶
        /*创建更多的子进程*/
        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];
        }
        /* 从0号进程开始启动 */
        pid = 0;
        my_current_task = &task[pid];
        asm volatile(
            "movl %1,%%esp
    	"     /* 设置 esp 的值*/
            "pushl %1
    	"          /* 将 ebp 压栈(此时esp=ebp),%1相当于task[pid].thread.sp*/
            "pushl %0
    	"          /* 将 eip 压栈,%0相当于task[pid].thread.ip*/
            "ret
    	"               /* 相当于 eip 出栈 */
            "popl %%ebp
    	"        /* 0号进程正是启动 */
            : 
            : "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);
            }     
        }
    }
    

    (2)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 */
        {
            /* 进程切换跳转到下一进程 */
            asm volatile(   
                "pushl %%ebp
    	"       /* 保存当前ebp */
                "movl %%esp,%0
    	"     /* 保存当前esp */
                "movl %2,%%esp
    	"     /* 重新记录要跳转进程的 esp,%2为 next->thread.sp*/
                "movl $1f,%1
    	"       /* 保存当前 eip ,%1为prev->thread.ip*/   
                "pushl %3
    	" 
                "ret
    	"               /* 记录要跳转进程的  eip,%3为 next->thread.ip*/
                "1:	"                  /* 下一个进程开始执行 */
                "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
    	"       /* 保存当前 ebp */
                "movl %%esp,%0
    	"     /* 保存当前 esp */
                "movl %2,%%esp
    	"     /* 重新记录要跳转进程的 esp ,%2为 next->thread.sp*/
                "movl %2,%%ebp
    	"     /* 重新记录要跳转进程的  ebp,%2为 next->thread.sp */
                "movl $1f,%1
    	"       /* 保存当前  eip ,%1为prev->thread.ip,%1f就是指标号1:的代码在内存中存储的地址*/   
                "pushl %3
    	" 
                "ret
    	"               /* 重新记录要跳转进程的  eip,%3为 next->thread.ip */
                : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
                : "m" (next->thread.sp),"m" (next->thread.ip)
            );          
        }   
        return; 
    }
    

    #三、总结
    本周从计算机操作系统对于程序的调用学起,结合了以前学习的汇编、C语言的知识,对于计算机内部对于中断的处理和进程切换有新的认识。有一些不明白的内容老师也在课堂上已经做出了详细的解答,很形象生动。本周因为一些个人因素进度有些太慢,这种情况应该有所规避,以后要改正。

  • 相关阅读:
    系统学习(javascript)_基础(语法)
    系统学习(javascript)_基础(数据类型之间的转换)
    系统学习(javascript)_基础(数据类型一)
    npm_一个有意思的npm包
    java_环境安装(window10)
    window10_使用技巧
    剑指Offer_编程题_16
    剑指Offer_编程题_15
    剑指Offer_编程题_14
    剑指Offer_编程题_13
  • 原文地址:https://www.cnblogs.com/suzhengsheng/p/5248111.html
Copyright © 2011-2022 走看看