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

1. 实验要求

·按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译。

·基于mykernel 2.0编写一个操作系统内核,参照https://github.com/mengning/mykernel 提供的范例代码。

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

 

2.实验环境

 

  Ubuntu18.04物理机

 

3. 环境配置

1)在ubuntu的终端依次执行

 

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'
# 使用allnoconfig编译出来qemu无法加载启动,不知道为什么?有明白的告诉我,完整编译太慢了,消耗的资源也多。
make -j$(nproc) # 编译的时间比较久哦
sudo apt install qemu # install QEMU
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

 

2)配置结果

基于mykernel 2.0编写一个操作系统内核_第1张图片

 

4. 代码分析

执行过程

首先调用调用my_start_kernel方法,该方法主要创建,初始化一些列进程,并启动0号进程。其中所创建的这些进程的ip都指向my_process方法,也就是说,当这些进程开始执行时,都会去执行my_process方法。my_process方法是每进过1000(可自行调节)个时钟中断并且i增加10000000,便会执行一次进程切换,用于模拟时间片轮转算法。

 

PCB结构体(进程控制块):对进程的调度本质上是对进程控制块进行操作。进程控制块中封装有进程id,进程状态,进程的堆栈信息,代码入口ip,sp。在本实验中,我们使用的是一个pcb链表来管理进程的pcb。本实验中,进程的状态有三种unrunnable,  runnable, stopped。pcb.h代码如下:

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;

 

My_start_kernel:这里是mykernel内核代码的入口,负责初始化内核的各个组成部分(见注释)。

void __init my_start_kernel(void)
{
    int pid = 0;
    int i;
    // 初始化进程0
    /* Initialize process 0*/
    task[pid].pid = pid;
    task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */

    // 0号进程的ip指向my_process程序位置
    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 
 

 

my_process:用来作为进程的代码,模拟时间片轮转。

 

// 该程序用来作为进程的代码,模拟一个进程,当进程运行完一个时间片后主动让出CPU的方式。
void my_process(void)
{    
    while(1)
    {
        i++;
        // 每经过10000000累加,以及当my_need_sched为1时,进程进行切换。
        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_timer_handler:时钟中断处理过程,每一次时钟中断,time_count加一。如果time_count%==0,则将my_need_sched设置为1,表明当前需要进行进程调度。

 

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

 

my_schedule:用于进程切换,主要功能有保存当前进程的上下文,恢复待调度进程的上下文,并将控制权移交给待调度进程(见代码注释)。

 

// 进程切换
void my_schedule(void)
{
    tPCB * next;
    tPCB * prev;

    // 如果当前进程pcb为空,或者当前进程的下一个pcb为空,则返回
    if(my_current_task == NULL 
        || my_current_task->next == NULL)
    {
        return;
    }
    printk(KERN_NOTICE ">>>my_schedule<<<\n");
    /* schedule */

    // next指向需要切换的进程,也就是当前进程的下一个进程
    // prev指向的是当前进程
    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;    
}

 

5. 运行结果

基于mykernel 2.0编写一个操作系统内核_第2张图片

 

你可能感兴趣的:(基于mykernel 2.0编写一个操作系统内核)