zoukankan      html  css  js  c++  java
  • Linux内核分析——第二周学习笔记

    20135313吴子怡.北京电子科技学院

    chapter 1 知识点梳理

    (一)计算机是如何工作的?(总结)——三个法宝

    ①存储程序计算机工作模型,计算机系统最最基础性的逻辑结构;

    ②函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要,但有了高级语言及函数,堆栈成为了计算机的基础功能;

      • enter 
        pushl %ebp
        movl %esp,%ebp
        
        leave 
        movl %ebp,%esp
        popl %ebp  
      • 函数参数传递机制和局部变量存储

    ③中断,多道程序操作系统的基点,没有中断机制程序只能从头一直运行结束才有可能开始运行其他程序。

    (二)函数调用堆栈

    堆栈

    1.堆栈是C语言程序运行时必须的一个记录调用路径和参数的空间。

    2.堆栈存在的目的:函数调用框架;传递参数;保存返回地址;提供局部变量空间;等等。 

    3.C语言编译器对堆栈的使用有一套的规则。

    4.了解堆栈存在的目的和编译器对堆栈使用的规则是理解操 作系统一些关键性代码的基础。

    堆栈寄存器和堆栈操作

    pop:从高地址向低地址
    push:从低地址向高地址

    1.堆栈相关的寄存器:esp,堆栈指针(stack pointer):ebp,基址指针(base pointer)

    2.堆栈操作:push 栈顶地址减少4个字节(32位) pop 栈顶地址增加4个字节

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

    其他关键寄存器

    cs : eip:总是指向下一条的指令地址

    顺序执行:总是指向地址连续的下一条指令
    跳转/分支:执行这样的指令的时候,cs : eip的值会 根据程序需要被修改
    call:将当前cs : eip的值压入栈顶,cs : eip指向被 调用函数的入口地址
    ret:从栈顶弹出原来保存在这里的cs : eip的值,放 入cs : eip中
    发生中断时

    (三)借助Linux内核部分源代码模拟存储程序计算机工作模型及时钟中断

    当一个中断信号发生时,CPU把当前的eip,esp,ebp压到内核堆栈中去,并把eip指向中断处理程序的入口。

    C代码中嵌入汇编代码

     

    (四)在mykernel基础上构造一个简单的操作系统内核

    chapter 2   实验

    1.实验步骤1:

    每执行my_ start_ kernel函数两次或一次,my_ time_ hander函数执行一次。

    2.实验步骤2:mymain.c

     

     3.实验步骤3:myinterrupt.c

    4.程序分析

    mypcb.h

    /*
         *  linux/mykernel/mypcb.h
         *
         *  Kernel internal PCB types
         *
         *  Copyright (C) 2013  Mengning
         *
         */
    
        #define MAX_TASK_NUM        4
        #define KERNEL_STACK_SIZE   1024*8
    
        /* CPU-specific state of this task */
        struct Thread {
            unsigned long       ip;  //保存eip
            unsigned long       sp;  //保存esp
        };
    
        typedef struct PCB{
            int pid;
            volatile long state;    /* 记录进程状态,-1 未运行, 0 运行中, >0 阻塞停止 */
            char stack[KERNEL_STACK_SIZE];  /* 定义堆栈结构*/
            struct Thread thread;
            unsigned long   task_entry;   /* 定义程序入口,通常是main函数*/
            struct PCB *next;
        }tPCB;
    
        void my_schedule(void);//调度器函数
    

    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);
                }     
            }
        }
    复制代码

     

    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; 
        }
    复制代码

     

    chapter 3  总结

    阐明对“操作系统是如何工作的”的理解。

    操作系统工作的基础:存储程序计算机、堆栈机制、中断机制。操作系统是管理电脑硬件与软件资源的程序,同时也是计算机系统的内核与基石。操作系统是管理计算机系统的全部硬件资源包括软件资源及数据资源;控制程序运行;改善人机界面;为其它应用软件提供支持等,使计算机系统所有资源最大限度地发挥作用,为用户提供方便的、有效的、友善的服务界面。操作系统是一个庞大的管理控制程序,大致包括5个方面的管理功能:进程与处理机管理、作业管理、存储管理、设备管理、文件管理。Linux系统有一个进程控制表(process table),一个进程就是其中的一项。进程控制表中的每一项都是一个task_struct结构,在task_struct结构中存储各种低级和高级的信息,包括从一些硬件设备的寄存器拷贝到进程的工作目录的链接点。进程控制表既是一个数组,又是一个双向链表,同时又是一个树,其物理实现是一个包括多个指针的静态数组。系统启动后,内核通常作为某一个进程的代表。一个指向task_struct的全局指针变量current用来记录正在运行的进程。

    chapter 4 附录

    作者:吴子怡

    学号:20135313

    原创作品转载请注明出处

    《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

     
  • 相关阅读:
    IP 协议
    以太网协议
    制作Win10系统安装U盘和安装纯净版Win10
    IP地址的配置
    进制转换
    设置QQ环境变量
    修改IE默认页的指向
    虚拟机安装Linux ubuntu19.10
    【Eclipse】Editor does not contain a main type
    Vmware Workstation虚拟机
  • 原文地址:https://www.cnblogs.com/paperfish/p/5236556.html
Copyright © 2011-2022 走看看