基于mykernel 2.0编写一个操作系统内核

一、配置实验环境

1、虚拟机的系统为Ubuntu 18.04,按照https://github.com/mengning/mykernel中提供的命令配置即可。

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 gcc-multilib
sudo apt install qemu # install QEMU
sudo apt install libncurses-dev bison flex libssl-dev libelf-dev
make defconfig # Default configuration is based on 'x86_64_defconfig'
make -j$(nproc)
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

在安装依赖时可能会出现类似于E: Unable to locate package flex的错误,可以尝试切换Ubuntu镜像源,并执行sudo apt-get update即可,最好单独执行那个依赖的安装命令。

2、配置完成,进行Linux内核的编译,编译完成后如下图所示。

3、运行qemu,结果如下,可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行

4、对于老师上课所说的使用make allnoconfig进行编译,通过查找资料,发现make allnoconfig是除必须的选项外,其它选项一律不选. (常用于嵌入式系统)。而老师使用的make defconfig,一般是默认的配置方式,与机器的平台有关。 尝试使用make allnoconfig进行编译,可以看到编译配置文件中输出格式为i386。

 

编译完成后使用命令

qemu-system-i386 -kernel arch/i386/boot/bzImage

发现会卡在如下界面

 

如果将mykernel文件夹替换成https://github.com/mengning/mykernel中的文件,重新编译会出现编译错误,不支持某些指令。

对于出现的错误,查找资料后并没有找到好的解决方法,所以最后还是按照老师提供的命令进行编译来完成实验。

二、基于mykernel 2.0编写一个操作系统内核

参照https://github.com/mengning/mykernel 提供的范例代码,将mykernel文件夹中的文件进行修改即可。

1、增加mypcb.h,进程控制块PCB结构体定义。

/*
 *  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 */

//存储ip,sp
struct Thread {
    unsigned long        ip;
    unsigned long        sp;
};

typedef struct PCB{
    int pid;    //进程的id
    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;    //指向下一个进程PCB
}tPCB;

//调度函数
void my_schedule(void);

2、mymain.c,首先调用my_start_kernel函数,启动0号进程并创建了其它进程PCB,在my_process函数中,根据my_need_sched变量,判断当前进程是否进行调度。

/*
 *  linux/mykernel/mymain.c
 *
 *  Kernel internal my_start_kernel
 *  Change IA32 to x86-64 arch, 2020/4/26
 *
 *  Copyright (C) 2013, 2020  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];     //声明tPCB类型的数组
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;
    /* Initialize process 0*/
    task[pid].pid = pid;      //初始化0号进程
    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);
        }     
    }
}

3、myinterrupt.c,时钟中断处理和进程调度算法。my_timer_handler函数记录时间,每经过固定的时间片就执行调度,通过调用my_schedule函数,如果下一个进程状态时runnable,就进行进程的切换。

/*
 *  linux/mykernel/myinterrupt.c
 *
 *  Kernel internal my_timer_handler
 *  Change IA32 to x86-64 arch, 2020/4/26
 *
 *  Copyright (C) 2013, 2020  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(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 ,%1f指接下来的标号为1的位置*/    
            "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;    
}

4、利用修改后的代码,重新编译运行,得到下面的结果,可以看到进程的切换。

三、操作系统内核核心功能及运行工作机制

1、内核核心功能是mymain.c和myinterrupt.c中的两段汇编代码,下面逐一分析。

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*/
    );

mymain.c中的这一段汇编代码进行0号进程的启动。

(1)将task[0].thread.sp拿去修改rsp的值,这时候内核堆栈的栈顶指针rsp指向task[0]的栈顶;

(2)在task[0]的sp位置处压入rbp的值,来保护原来的内核堆栈;

(3)设置task[0].thread.ip的值给rip,这样就能够保证cpu下一步能够执行0号进程,完成了进入my_process()的过程。

   此时eip的值已经被修改,指令进入my_process(),所以最后一句的ret并不会被立即执行了。

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 ,%1f指接下来的标号为1的位置*/  
        "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)
)

myinterrupt.c中的这段汇编代码用于进程切换。

(1)保存prev进程的rbp值到堆栈中。

(2)修改prev->thread.sp的值为当前rsp寄存器的值,保存prev进程的栈顶指针。

(3)将next->thread.sp的值赋给rsp寄存器,完成进程的切换。

(4)保存prev进程rip寄存器值到prev->thread.ip,这里$1f是指标号1。

(5)把即将执行的next进程的指令地址next->thread.ip入栈。

(6)ret 就是将压入栈中的next->thread.ip放入rip寄存器,rip寄存器现在存储next进程的指令。

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

(8)next进程栈底从堆栈中恢复到rbp寄存器中,开始next进程的执行。

2、运行工作机制

 程序的运行过程如下:首先从my_start_kernel函数开始,启动并初始化0号进程,并通过复制创建其他的进程。接着进入my_process函数,通过死循环不同重复变量i的自增,每10000000次检查my_need_sched变量,同时内核周期性调用my_timer_handler函数,通过time_count变量的自增来控制时间片,当时间片结束时,将my_need_sched变量的值修改为1,此时如果my_process函数中检测到my_need_sched的值为1,便调用my_schedule函数,进行进程调度。不断循环。

四、小结

在这次实验中,首先完成了实验环境的配置和linux内核编译,通过尝试不同的编译配置方法,对编译内核有了更深的理解。接着通过范例代码,对时间片进程调度算法进行分析,堆栈,rsp,rbp,rip等在进程切换的过程中对上下文的保存和切换有着重要的作用。

posted @ 2020-05-08 02:36  zda1234  阅读(204)  评论(0编辑  收藏  举报