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

前言

  本文将参考相关文献https://mp.weixin.qq.com/s/SzpN1BNty5aPDZhNdCO5yA实现一个简易的linux操作系统内核,深入理解操作系统内核的基本工作原理,其源代码来自https://github.com/mengning/mykernel

  学过linux操作系统都知道系统启动后,内核入口是init/main.c中的start_kernel(void)函数、进程切换的代码是schedule(void)函数、进程控制块由struct tast_struct结构体定义。那么这里主要对这三个函数进行替换,对应自己的函数为my_start_kernel(void)、my_schedule(void)、struct PCB。

一、安装环境

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
make defconfig   # Default configuration is based on 'x86_64_defconfig'
make -j$(nproc) 
sudo apt install qemu # install QEMU

  如果全部顺利执行,接下来可以执行以下代码直接运行模拟器,运行结果如下:

qemu-system-x86_64 -kernel arch/x86/boot/bzImage

二、struct PCB分析

  进程控制块struct PCB在mypcb.h中定义,因为暂时只用了时间片轮转调度,因此PCB结构没有优先级,只具备最基本的成员。

/* CPU-specific state of this task */
struct Thread {
    unsigned long        ip;          // 进程ip
    unsigned long        sp;         // 栈顶指针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;                         /* 指向下一个PCB */
}tPCB;

 

三、my_start_kernel(void)分析

  my_start_kernel函数在mymain.c文件中定义,my_process函数是进程实体。my_start_kernel函数首先创建几个进程,然后运行0号进程,这部分由内嵌汇编代码实现,具体见注释。

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" /* 将RSP寄存器指向进程0的堆栈栈底,task[pid].thread.sp初始值即为进程0的堆栈栈底 */
        "pushq %1\n\t"      /* push rbp,将当前RBP寄存器值压栈 */
        "pushq %0\n\t"      /* push task[pid].thread.ip,将当前进程的RIP(这里是初始化的值my_process(void)函数的位置)压栈 */
        "ret\n\t"           /* ret 将栈顶位置的task[0].thread.ip,也就是my_process(void)函数的地址放入RIP寄存器中,相应的RSP寄存器指向的位置也发生了变化 */
        : 
        : "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);
        }     
    }
}

 

四、my_schedule(void)分析

   进程函数中每隔一定时间会执行一次调度,调度的代码在my_schedule(void)函数中实现,首先找到PCB链表中的下一个节点,然后切换到该进程,这部分代码由内嵌汇编实现。

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;  /* 下一个PCB */
    prev = my_current_task;        /* 当前PCB */
    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;    
}

  进程切换过程中prev进程和next进程的堆栈和相关寄存器的变化过程大致如下:

  1. pushq %%rbp  保存prev进程的当前RBP寄存器的值到prev进程的堆栈。

  2. movq %%rsp,%0 保存prev进程的当前RSP寄存器的值到prev->thread.sp,实际上就是将prev进程的栈顶地址保存。

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

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

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

  6. ret 就是将压入栈中的next->thread.ip放入RIP寄存器,为什么不直接放入RIP寄存器呢?因为程序不能直接使用RIP寄存器,只能通过call、ret等指令间接改变RIP寄存器。

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

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

五、实验结果

  将以上代码代码添加到mykernel文件夹下,并重新编译内核,再运行查看结果如下:

posted @ 2020-05-11 11:38  Tsungcheng  阅读(176)  评论(0编辑  收藏  举报