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

一、实验环境
  阿里云学生版 Ubuntu 16.04
二、基于mykernel 2.0编写一个操作系统内核,参照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 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

 


三、结合详细代码
简要分析操作系统内核核心功能及运行工作机制
  mypcb.h:
复制代码
#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);
复制代码

  mypcb.h 模拟真实内核中的进程控制块,基于这个角度,可以更好理解内容。

  • Thread结构体模拟了进程,ip,sp是用于描述进程的相关状态;根据课程回忆,大概是sp标识线程栈底,ip标识下一条指令地址(不是十分确定)
  • PCB结构体实现了简单的的进程控制块
  • pid:进程的id号,用于标识进程
  • state:继承的状态。-1阻塞,0就绪,>0停止
  • stack:模拟一个进程所具有的栈空间,在开头定义为1024*2
  • thread:进程中的线程
  • task_entry:函数入口
  • * next:指针,指向下一个PCB。说明PCB之间的组织结构是链表。
  • my_schedule:方法签名?留待后用。

  mymain.c

复制代码
#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\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);
        }     
    }
}

复制代码

  my_start_kernel()

  • 初始化最开始的进程的相关信息,为最开始的进程控制块的的信息初始化值,作为时间片轮转的开始
  • 以第一块PCB其实,不断初始化新的PCB,形成队列
  • 在之后的汇编代码是启动0号PCB

 

  my_process()

  • 模拟每个进程的执行任务,用输出该进程的进程号代替实际的进程任务
  • 进程间的切换采用时间片轮转机制,在my_need_sched =1时,进行轮转,并重置为0;

 

  关键汇编代码分析

  • movq %1,%%rsp :%1,是sp。将sp的值赋给rsp寄存器中,相当于使rsp指向0号进程的栈底。
  • pushq %1 :因为是进程0初始化,所以空栈。此时,rbp=rsp。在压栈之后,rsp指向栈顶。
  • pushq %0\n\t:0进程的ip值入栈。rsp指向它。
  • ret\n\t:将栈顶,也就是0进程ip值返回,使ip进入rip寄存器中。rsp指向第一个单元。
  • 三、四两步使用了tricky的方式,将ip值放入rip中,因为rip安全起见程序员接触不到,但是在这种情况下,可以对rip进行操作。
  • 至此,rip,rsp都初始化为0进程的对应的值,my_process()可以启动。

 

 

 

 

  myinterrupt.c

复制代码
#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 */    
            "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;    
}
复制代码

 

   my_timer_handler()
  • 相当于一个计时决策机制,当timer_count%1000==0且my_need_sched=0时,标识时间片到期,my_need_sched=1。
  • 当mymain.c中观察到my_need_sched=1时,my_process(0调用my_schedule()执行线程调度。

 

  my_schedule()

  • 实际上执行进程调度的函数,通过汇编语言描述PCB链表中的进程切换。

  

 

  关键汇编代码分析

  • pushq %%rbp\n\t:保存当前进程的rbp寄存器中的值,放在当前进程的堆栈中
  • movq %%rsp,%0\n\t:保存当前进程的rsp的值,放在pcb的sp中。实质是结合上一步,将当前进程的栈顶栈底地址保存起来。
  • movq %2,%%rsp\n\t:将下一个进程的sp值从pcb中取出,放到rsp寄存器里。此时堆栈已经切换到了下一个进程
  • movq $1f,%1\n\t:将当前进程的rip的值存入pcb中。 $1f是一个特殊符号,标号1.个人理解就是当前程序的下一条指令的地址
  • pushq %3\n\t:下一进程的ip入栈。ip的具体值既有可能使进程最开始的起点,也有可能使标号1
  • ret\n\t:结合上一步,使ip值进入rip。前有,不赘述。
  • 1:\t:标号1是一个特殊的地址位置,该位置的地址是$1f
  • popq %%rbp\n\t:将下一进程的堆栈基址从堆栈中取出,放入rbp中

 

 

posted @ 2020-05-12 19:29  LittleTurtle  阅读(161)  评论(0编辑  收藏  举报