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

实验题目   基于mykernel 2.0编写一个操作系统内核

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

   1.进入该网址按照给定的命令行配置环境

      其中也遇到了一些问题,由于patch文件老师已经给了,所以没有使用wget命令下载,但是下载的位置是在根目录下,所以需要将文件复制到根目录下才不会报错。

      加一行命令 sudo cp mykernel-2.0_for_linux-5.4.34.patch  / #将文件复制到根目录下。

  将上述命令执行之后可以看到my_start_kernel在qemu窗口执行,同时my_timer_handler时钟中断处理程序周期性执行如下图。

当前有一个CPU执行C代码的上下文环境,同时具有中断处理程序的上下文环境,通过Linux内核代码模拟了一个具有时钟中断和C代码执行环境的硬件平台。

 从输出内容可以看出每100000次计数便输出一次my_start_kernel here,而my_timer_hander here就是周期性产生的时钟中断。如下为代码

 

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

接下来要在mymain.c基础上继续写进程描述PCB和进程链表管理等代码,在myinterrupt.c的基础上完成进程切换代码,一个可运行的小OS kernel就完成了

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

#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)函数。

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

在mymain.c中添加了my_process函数,用来作为进程的代码模拟一个个进程

此外还需要时钟记录中断处理过程中的时间片,对myinterrupt.c中修改my_timer_handler用来记录时间片。

 

对myinterrupt.c进行修改,主要是增加了进程切换的代码my_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<<<
");
        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<<<
");
    /* 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<<<
",prev->pid,next->pid);  
        /* switch to next process */
        asm volatile(    
            "pushq %%rbp
	"         /* save rbp of prev */
            "movq %%rsp,%0
	"     /* save rsp of prev */
            "movq %2,%%rsp
	"     /* restore  rsp of next */
            "movq $1f,%1
	"       /* save rip of prev */    
            "pushq %3
	" 
            "ret
	"                 /* restore  rip of next */
            "1:	"                  /* next process start here */
            "popq %%rbp
	"
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        ); 
    }  
    return;    
}

最后对修改好的内核按照上次重新编译运行结果如下

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

汇编代码分析:

进程0启动代码

    asm volatile(
        "movq %1,%%rsp "     /* set task[pid].thread.sp to rsp */
        "pushq %1 "          /* push rbp */
        "pushq %0 "          /* push task[pid].thread.ip */
        "ret "               /* pop task[pid].thread.ip to rip */
        : 
        : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)   /* input c or d mean %ecx/%edx*/
    );
其中%1指后面的task[pid].thread.sp,%0是指后面的task[pid]. thread.ip。

movq %1,%%rsp :将RSP寄存器指向进程0的堆栈栈底,task[pid].thread.sp初始值即为进程0的堆栈栈底。

pushq %1 :将当前RBP寄存器的值压栈,因为是空栈,所以RSP与RBP相同。

pushq %0 :将当前进程的RIP(这里是初始化的值my_process(void)函数的位置)入栈,相应的RSP寄存器指向的位置也发生了变化,RSP = RSP - 8,RSP寄存器指向堆栈底部第二个64位的存储单元

ret :将栈顶位置的task[0].thread.ip,也就是my_process(void)函数的地址放入RIP寄存器中,相应的RSP寄存器指向的位置也发生了变化,RSP = RSP + 8,RSP寄存器指向堆栈底部第一个64位的存储单元。

这样就完成 了进程0的启动。

进程切换关键汇编代码

 asm volatile(   
            "pushq %%rbp "       /* save rbp of prev */
            "movq %%rsp,%0 "     /* save rsp of prev */
            "movq %2,%%rsp "     /* restore  rsp of next */
            "movq $1f,%1 "       /* save rip of prev */  
            "pushq %3 " 
            "ret "               /* restore  rip of next */
            "1: "                  /* next process start here */
            "popq %%rbp "
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        ); 
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入栈。
ret 就是将压入栈中的next->thread.ip放入RIP寄存器。
popq %%rbp 将next进程堆栈基地址从堆栈中恢复到RBP寄存器中。
到这里开始执行进程1了,如果进程1执行的过程中发生了进程调度和进程切换,进程0重新被调度执行了,就是从进程1再切换到进程0,prev进程变成了进程1,而next进程变成进程0。
原文地址:https://www.cnblogs.com/russian/p/12873826.html