1.实验要求:

  1. 按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译;
  2. 基于mykernel 2.0编写一个操作系统内核,参照https://github.com/mengning/mykernel 提供的范例代码;
  3. 简要分析操作系统内核核心功能及运行工作机制

2.实验过程

实验环境:VMware Workstation 12,Ubuntu16.04

2.1完成mykernel 2.0配置:

准备工作:按照老师提供的范例代码,安装axel和Linux内核源码,解压,安装依赖库

wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch
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 libncurses-dev bison flex libssl-dev libelf-dev

 

编译内核,使用qemu启动:

make defconfig
make -j$(nproc)
sudo apt install qemu # install QEMU
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

 

 

从qemu窗口中可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行。

 

 注:中途出现的错误:

原因:最初使用的linux-5.6.6.tar.xz,与mykernel-2.0_for_linux-5.4.34.patch版本有些冲突,更换linux-5.4.34.tar.xz就解决了;

           如果有一些软件包安装不成功,也可能是镜像源的问题,换一个镜像源即可。

2.2 基于mykernel 2.0编写一个简单的操作系统内核

参照https://github.com/mengning/mykernel ,首先在mykernel目录下增加一个mypcb.h 头文件,用来定义进程控制块(Process Control Block,也就是进程结构体的定义,在Linux内核中是struct tast_struct结构体。

/*
 *  linux/mykernel/mypcb.h
 *
 *  Kernel internal PCB types
 *
 *  Copyright (C) 2013  Mengning
 *
 */

#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*2
/* 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);

 

 

对mymain.c进行修改,这里是mykernel内核代码的入口,负责初始化内核的各个组成部分。在Linux内核源代码中,实际的内核入口是init/main.c中的start_kernel(void)函数。添加了my_process(void)函数,用来作为进程的代码模拟一个个进程,这里采用的是进程运行完一个时间片后主动让出CPU的方式,没有采用中断的时机完成进程切换,因为中断机制实现起来较为复杂。 

#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].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(
        "movq %1,%%rsp\n\t"     /* set task[pid].thread.sp to rsp */
        "pushq %1\n\t"             /* push rbp */
        "pushq %0\n\t"             /* push task[pid].thread.ip */
        "ret\n\t"                 /* pop task[pid].thread.ip to rip */
        : 
        : "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 -\n",my_current_task->pid);
            if(my_need_sched == 1)
            {
                my_need_sched = 0;
                my_schedule();
            }
            printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
        }     
    }
}

 

 

如果想知道进程运行过程中时间片是否消耗完,我们需要时钟中断处理过程中记录时间片。对myinterrupt.c中修改my_timer_handler用来记录时间片。对myinterrupt.c进行修改,增加了进程切换的代码my_schedule(void)函数,在Linux内核源代码中对应的是schedule(void)函数。

#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(time_count%1000 == 0 && my_need_sched != 1)
    {
        printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
        my_need_sched = 1;
    } 
    time_count ++ ;  
    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<<<\n");
    /* 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<<<\n",prev->pid,next->pid);  
        /* switch to next process */
        asm volatile(    
            "pushq %%rbp\n\t"         /* save rbp of prev */
            "movq %%rsp,%0\n\t"     /* save rsp of prev */
            "movq %2,%%rsp\n\t"     /* restore  rsp of next */
            "movq $1f,%1\n\t"       /* save rip of prev */    
            "pushq %3\n\t" 
            "ret\n\t"                 /* restore  rip of next */
            "1:\t"                  /* next process start here */
            "popq %%rbp\n\t"
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        ); 
    }  
    return;    
}

 

 

三个文件中代码修改完成后,Linux-5.3.34内核源代码根目录下,执行以下代码重新编译内核:

make defconfig
make -j$(nproc)
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

 

启动QEMU,我们可以看到进程切换:

 

 

 3.简要分析操作系统内核核心功能及运行工作机制

核心功能:简单的时间片轮转来调度进程

工作机制:

  1. 内核入口是init/main.c中的start_kernel(void)函数,负责初始化内核的各个组成部分。系统启动后,就运行mymain.c中的my_start_kernel(void)函数
  2. 在mymain.c中添加了my_process函数,用来作为进程的代码模拟一个个进程,进程运行完一个时间片后主动让出CPU的,my_timer_handler 函数被内核周期性调用,每调用1000次,修改全局变量my_need_sched的值为1
  3. 对myinterrupt.c进行修改,添加了进程切换的代码my_schedule(void)函数,my_start_kernel中的while循环在my_need_sched值变为1后,就进行进程的调度,完成进程的切换,,来实现进程切换。

 

 

注:进程切换的关键汇编代码分析,摘取https://mp.weixin.qq.com/s/SzpN1BNty5aPDZhNdCO5yA孟宁老师的分析

/* 进程调度关键代码 */
    asm volatile(
          "pushq %%rbp\n\t"       /* save rbp of prev */
          "movq %%rsp,%0\n\t"   /* save rsp of prev */
          "movq %2,%%rsp\n\t"     /* restore  rsp of next */
          "movq $1f,%1\n\t"       /* save rip of prev */
          "pushq %3\n\t"
          "ret\n\t"               /* restore  rip of next */
          "1:\t"                  /* next process start here */
          "popq %%rbp\n\t"
        : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        : "m" (next->thread.sp),"m" (next->thread.ip)
    );
}

假设系统只有两个进程,分别是进程0和进程1

  • pushq %%rbp  保存prev进程(本例中指进程0)当前RBP寄存器的值到prev进程的堆栈;

  • movq %%rsp,%0 保存prev进程(本例中指进程0)当前RSP寄存器的值到prev->thread.sp,这时RSP寄存器指向进程的栈顶地址,实际上就是将prev进程的栈顶地址保存;%0、%1...指这段汇编代码下面输入输出部分的编号。

  • movq %2,%%rsp 将next进程的栈顶地址next->thread.sp放入RSP寄存器,完成了进程0和进程1的堆栈切换。

  • movq $1f,%1 保存prev进程当前RIP寄存器值到prev->thread.ip,这里$1f是指标号1。

  • pushq %3 把即将执行的next进程的指令地址next->thread.ip入栈,这时的next->thread.ip可能是进程1的起点my_process(void)函数,也可能是$1f(标号1)。第一次被执行从头开始为进程1的起点my_process(void)函数,其余的情况均为$1f(标号1),因为next进程如果之前运行过那么它就一定曾经也作为prev进程被进程切换过。

  • ret 就是将压入栈中的next->thread.ip放入RIP寄存器。

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

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

到这里开始执行进程1了,如果进程1执行的过程中发生了进程调度和进程切换,进程0重新被调度执行了,就是从进程1再切换到进程0,prev进程变成了进程1,而next进程变成进程0。

posted on 2020-05-12 20:57  术桔  阅读(339)  评论(0)    收藏  举报