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

1.配置mykernel 2.0,熟悉Linux内核的编译

虚拟机环境:虚拟机Ubuntu 18.04.1 LTS amd64

 

 

打开终端,依次输入以下代码

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

配置成功后结果如下,从qemu窗口中您可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行

 

 2. 编写一个操作系统内核(参照-https://github.com/mengning/mykernel 

现在环境已经搭建好了,只要在mymain.c的基础上完成进程描述PCB和进程链表管理等,在myinterrupt.c的基础上完成进程切换代码,就可以完成⼀个可运⾏的OS kernel。

首先定义mypcb.h

//最大的任务数
#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*8

/* 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 */
    char 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中的my_start_kernel函数,并在mymain.c中实现了my_process函数,用来模拟一个个进程,时间片轮转调度。

#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].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];
    }
    /* 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*/
    );
}

void my_process(void)
{
    int i = 0;
    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.简要分析操作系统内核核心功能及运行工作机制

这是进程切换的核心代码 

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

  

1,2 保存的是前一个进程的rbp和rsp,rbp保存在栈中,rsp保存在pcb.sp中

3 更换了进程栈,原本rsp指向前一个进程的栈,而后指向了后一个进程的栈

4 将$1f 保存到了前一个线程的pcb.ip中(可以看做是保存当前进程的ip)

5,6 修改当前rip寄存器的值,相当于原来rip的内容为前一个进程的指令地址,现在为后一个进程的指令地址

7,8 将rbp寄存器的值修改为下一个进程的栈底

 

posted @ 2020-05-09 18:16  olddriver555111  阅读(225)  评论(0)    收藏  举报