zoukankan      html  css  js  c++  java
  • Lab1——基于mykernel 2.0编写一个操作系统内核

     一、升级Vmware Workstation Pro

    由于原来使用的Vmware Workstation Pro是12.0,不支持ubuntu18.04的linux版本内核,所以要进行升级

    从官网下载和电脑操作系统对应的Vmware Workstation Pro15版本,然后按照提示进行就OK了,中间需要重启一次

    最后要填入一个激活码才能永久使用,这个网上一搜有一堆,随便试了一个就OK了。。。

    最终环境是:Vmware Workstation Pro15.5 + Ununtu18.04 LTS

    二、为 mykernel2.0 配置开发环境 

    git clone https://github.com/mengning/mykernel
    sudo apt install axel
    axel -n -20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz
    xz -d linux-5.4.34.tar.xz
    tar -xvf linux-5.4.34.tar
    cd linux-5.4.34
    patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch
    sudo apt install build-essential gcc-multilib
    sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev
    sudo apt install qemu # install QEMU

    三、然后对内核进行编译,并且从QEMU窗口中观察my_start_kernel的执行

    make defconfig 
    make -j$(nproc) #这里大概编译了20分钟
    qemu-system-x86_64 -kernel arch/x86/boot/bzImage

    编译完成结果如下所示:

    如下所示,从QEMU窗口中可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行

     四、完成一个可运行的小OSkernel

    参考:https://github.com/mengning/mykernel

    1、首先在mykernel目录下增加一个mypcb.h头文件,用来定义进程控制块。也就是进程结构体的定义,在Linux内核中是struct tast_struct结构体。

    操作系统进程控制块中包含的信息有:
    • 进程描述信息:PID,进程标识符用于唯一的标识一个进程。
    • 进程控制信息:进程当前状态、进程优先级、程序开始地址、各种计时信息、通信信息、
    • 资源信息:占用内存大小及管理用数据结构指针、交换区相关信息、I/O设备号、缓冲、设备相关的数结构、文件系统相关指针
    • 现场保护信息(cpu进行进程切换时):寄存器、PC、程序状态字PSW、栈指针

    这里对PCB进行了简化,只保留我们所能用到的信息即可;

     1 /*
     2  *  linux/mykernel/mypcb.h
     3  *  Kernel internal PCB type
     4  */
     5 
     6 #define MAX_TASK_NUM        4
     7 #define KERNEL_STACK_SIZE   1024*2
     8 /* CPU-specific state of this task */
     9 
    10 //存储ip和sp
    11 struct Thread {
    12     unsigned long        ip; //函数入口指针
    13     unsigned long        sp; //栈顶指针
    14 };
    15 
    16 //PCB结构
    17 typedef struct PCB{
    18     int pid;//进程id
    19     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
    20     unsigned long stack[KERNEL_STACK_SIZE];//进程堆栈
    21     /* CPU-specific state of this task */
    22     struct Thread thread;//线程
    23     unsigned long    task_entry;//进程入口地址
    24     struct PCB *next;//下一个进程控制块地址
    25 }tPCB;
    26 
    27 void my_schedule(void);//调度函数

    2、对mymain.c进⾏修改,这⾥是mykernel内核代码的⼊⼝,负责初始化内核的各个组成部分。在Linux内核源代码中,实际的内核⼊⼝是init/main.c中的start_kernel(void)函数。

     1 /*
     2  *  linux/mykernel/mymain.c
     3  *  Kernel internal my_start_kernel
     4  */
     5  #include <linux/types.h>
     6  #include <linux/string.h>
     7  #include <linux/ctype.h>
     8  #include <linux/tty.h>
     9  #include <linux/vmalloc.h>
    10 
    11  
    12  #include "mypcb.h"
    13  
    14  tPCB task[MAX_TASK_NUM];
    15  tPCB * my_current_task = NULL;
    16  volatile int my_need_sched = 0;
    17  
    18  void my_process(void);//模拟进程执行代码
    19  
    20  
    21  void __init my_start_kernel(void)
    22  {
    23      int pid = 0;//0号进程
    24      int i;
    25      /* 初始化0号进程PCB信息*/
    26      task[pid].pid = pid;
    27      task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
    28     //任务入口地址,将my_process的地址赋给ip
    29      task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
    30      //0号进程PCB堆栈栈顶地址赋给sp
    31      task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
    32          //系统刚开始只有一个进程,下一个进程地址指向自身
    33      task[pid].next = &task[pid];
    34      /*fork more process */
    35          //复制0号进程创建更多进程,并对它们赋值,插入队列
    36      for(i=1;i<MAX_TASK_NUM;i++)
    37      {
    38          memcpy(&task[i],&task[0],sizeof(tPCB));
    39          task[i].pid = i;
    40          task[i].thread.sp = (unsigned long)(&task[i].stack[KERNEL_STACK_SIZE-1]);
    41          task[i].next = task[i-1].next;
    42          task[i-1].next = &task[i];
    43      }
    44      /* start process 0 by task[0] */
    45          //启动0号任务,开始执行0号进程
    46      pid = 0;
    47     my_current_task = &task[pid];//当前任务指针
    48          
    49          /*下面这一段是进程执行的关键汇编代码,下文会对其进行详细分析
    50            *  %1指task[pid].thread.sp,%0指task[pid].thread.ip
    51            */
    52      asm volatile(
    53          "movq %1,%%rsp
    	"     /* set task[pid].thread.sp to rsp */
    54          "pushq %1
    	"             /* push rbp */
    55          "pushq %0
    	"             /* push task[pid].thread.ip */
    56         "ret
    	"                 /* pop task[pid].thread.ip to rip */
    57          : 
    58          : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)    /* input c or d mean %ecx/%edx*/
    59      );
    60  } 
    61  
    62  int i = 0;
    63 
    64  //模拟进程执行过程,这⾥采⽤的是进程运⾏完⼀个时间⽚后主动让出CPU的⽅式。
    65  void my_process(void)
    66  {    
    67      while(1)
    68      {
    69          i++;
    70         if(i%10000000 == 0)
    71          {
    72              printk(KERN_NOTICE "this is process %d -
    ",my_current_task->pid);
    73            if(my_need_sched == 1)//判断是否需要调度
    74             {
    75                 my_need_sched = 0;
    76                 my_schedule();//执行调度
    77              }
    78              printk(KERN_NOTICE "this is process %d +
    ",my_current_task->pid);
    79          }     
    80     }
    81  

    3、进程运⾏过程中是怎么知道时间⽚消耗完了呢?这就需要时钟中断处理过程中记录时间⽚。

     1 /*
     2  * Called by timer interrupt.
     3  * it runs in the name of current running process,
     4  * so it use kernel stack of current running process
     5  */
     6 void my_timer_handler(void)
     7 {
     8     if(time_count%1000 == 0 && my_need_sched != 1)//设置时间片的大小,时间片用完则开始调度
     9     {
    10         printk(KERN_NOTICE ">>>my_timer_handler here<<<
    ");
    11         my_need_sched = 1;//设置进程调度标志
    12     } 
    13     time_count ++ ;  
    14     return;      
    15 }
    16 
    17 //调度函数
    18 void my_schedule(void)
    19 {
    20     tPCB * next;//下一个进程指针
    21     tPCB * prev;//当前进程指针
    22 
    23     if(my_current_task == NULL 
    24         || my_current_task->next == NULL)
    25     {
    26         return;
    27     }
    28     printk(KERN_NOTICE ">>>my_schedule<<<
    ");
    29     /* schedule */
    30     next = my_current_task->next;//下一个进程
    31     prev = my_current_task;//当前进程
    32     if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
    33     {        
    34         my_current_task = next; //排队策略
    35         printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid);  
    36         /* 进程切换 */
    37     //下面是进程切换的汇编代码,下文将进行详细分析
    38         asm volatile(    
    39             "pushq %%rbp
    	"         /* save rbp of prev */
    40             "movq %%rsp,%0
    	"     /* save rsp of prev */
    41             "movq %2,%%rsp
    	"     /* restore  rsp of next */
    42             "movq $1f,%1
    	"       /* save rip of prev */    
    43             "pushq %3
    	" 
    44             "ret
    	"                 /* restore  rip of next */
    45             "1:	"                  /* next process start here */
    46             "popq %%rbp
    	"
    47             : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
    48             : "m" (next->thread.sp),"m" (next->thread.ip)
    49         ); 
    50     }  
    51     return;    
    52 }

    五、关键汇编代码分析

    1、启动执⾏第⼀个进程的关键汇编代码

    %1是指后⾯的task[pid].thread.sp,%0是指后⾯的task[pid]. thread.ip

    1 asm volatile(
    2 "movq %1,%%rsp
    	" /* 将进程原堆栈栈顶的地址存⼊RSP寄存器 */
    3 "pushq %1
    	" /* 将当前RBP寄存器值压栈 */
    4 "pushq %0
    	" /* 将当前进程的RIP压栈 */
    5 "ret
    	" /* ret命令正好可以让压栈的进程RIP保存到RIP寄存器中 */
    6 :
    7 : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
    8 );

    这一段代码用于启动0号进程:

    movq %1,%%rsp  #将rsp寄存器指向进程0的堆栈栈底,task[pid].thread.sp的初始值就是堆栈栈底;

    pushq %1             #将当前rsp寄存器的值压栈,rsp=rsp-8;

    pushq %0             #将当前进程的rip入栈,相应的rsp寄存器指向的位置也发生了变化。rsp=rsp-8;

    ret                        #将栈顶位置task[0]. thread.ip,也就是my_process(void)函数的地址放入rip寄存器,rsp=rsp+8;

    这样就完成了进程0的启动,开始执行my_process(void)函数的代码

    2、进程切换关键代码分析

    %0指pre->thread.sp, %1指prev->thread.ip

    %2指next->thread.sp,%3指next->thread.ip

     1 asm volatile(    
     2             "pushq %%rbp
    	"         /* save rbp of prev */
     3             "movq %%rsp,%0
    	"     /* save rsp of prev */
     4             "movq %2,%%rsp
    	"     /* restore  rsp of next */
     5             "movq $1f,%1
    	"       /* save rip of prev */    
     6             "pushq %3
    	" 
     7             "ret
    	"                 /* restore  rip of next */
     8             "1:	"                  /* next process start here */
     9             "popq %%rbp
    	"
    10             : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
    11             : "m" (next->thread.sp),"m" (next->thread.ip)
    12         );

    为了简便,假设系统只有两个进程,分别是进程0和进程1。进程0由内核启动时初始化执⾏,然后需要进程调度和进程切换,然后开始执⾏进程1。
    进程切换过程中进程0和进程1的堆栈和相关寄存器的变化过程⼤致如下:

    pushq %%rbp        #保存prev进程的rbp的值,入栈

    movq %%rsp,%0  #当前rsp寄存器的值到prev->thread.sp,这时rsp寄存器指向进程的栈顶地址,也就是保存prev进程栈顶地址

    movq %2,%%rsp  #将next进程的栈顶地址放入rsp寄存器,完成进程0和进程1的堆栈切换

    movq $1f,%1        #保存prev进程当前rip寄存器的值到prev->thread.ip,$1f指标号1

    pushq %3             #把即将执行的next进程的指令地址next->thread.ip入栈

    ret                        #将压入栈的next->thread.ip放入rip寄存器

    1:                         #标号1是一个特殊的地址位置,地址是$1f

    popq %%rbp       #将next进程堆栈基地址从堆栈恢复到rbp寄存器中

    六 、结果演示

    完成代码修改,重新编译运行

    我们可以看到进程0切换到进程1

     七、小结

    通过这次实验,我对linux进程启动和调度切换有了更加深刻的理解,并且实现mykernel,对进程的实现细节和进程切换的底层堆栈变化有了更深的了解。总之,受益匪浅。

  • 相关阅读:
    BZOJ3992 [SDOI2015]序列统计 【生成函数 + 多项式快速幂】
    BZOJ3993 [SDOI2015]星际战争 【二分 + 网络流】
    BZOJ3325 [Scoi2013]密码 【manacher】
    BZOJ3534 [Sdoi2014]重建 【矩阵树定理】
    BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
    BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
    BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】
    BZOJ4817 [Sdoi2017]树点涂色 【LCT + 线段树】
    BZOJ1195 [HNOI2006]最短母串 【状压dp】
    malloc的使用、用malloc动态分配内存以适应用户的需求的源代码实例
  • 原文地址:https://www.cnblogs.com/pghzl-123/p/12825669.html
Copyright © 2011-2022 走看看