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

配置mykernel2.0,熟悉Linux内核的编译

在terminal中逐条输入以下命令:

 

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

 

即可完成mykernel2.0的下载、配置和编译。

其中, make -j$(nproc) 命令是编译内核,nproc是一个内核参数,指系统最大进程数,$(nproc)获取系统该参数,完成内核编译。

 

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

1. 定义进程结构体

在mykernel目录下新建mypcb.h头文件,用于定义进程控制块PCB,在Linux内核中,实际上是struct task_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阻塞态 0就绪态 >0停止执行
    unsigned long stack[KERNEL_STACK_SIZE];  //当前进程栈空间
    /* CPU-specific state of this task */
    struct Thread thread;  //当前线程
    unsigned long	task_entry;  //进程入口地址
    struct PCB *next;  //指向下一个进程的PCB
}tPCB;

void my_schedule(void);

 2. 模拟进程运行

对mymain.c进行修改,这里是mykernel内核代码的入口,在my_start_kernel中对内核各个部分进行初始化。

首先是初始化0号进程,然后对1~MAX_TASK_NUM号进程进行初始化,所以进程的state初始化为0,即就绪态,可以运行。

所有进程组成一个循环链表,即最后一个tPCB的next指向首个tPCB。

添加my_process函数,模拟进程执行的过程。my_need_sched表示是否需要进程调度,值为1时调用my_schedule函数进行进程切换。

void __init my_start_kernel(void)
{
    int pid = 0;
    int i;

    //初始化0号进程
    task[pid].pid = pid;
    task[pid].state = 0;
    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];

    //创建后续进程
    for (i = 1; i < MAX_TASK_NUM;++i){
        memcpy(&task[i], &task[0], sizeof(tPCB));
        task[pid].pid = i;
        task[pid].state = 0;
        task[pid].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE - 1];
        task[i].next = task[i - 1].next;
        task[i - 1].next = &task[i];
    }
    
    //进入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)   /*1表示需要切换到下一个进程,0表示不需要*/
            {
                my_need_sched = 0;    /*重新置0,执行一个时间片*/
                my_schedule();        /*切换进程*/
            }
            printk(KERN_NOTICE "this is process %d +\n", my_current_task->pid);
        }
    }
}

3. 进程调度和切换

修改myinterrupt.c中的my_timer_handler用于记录时间片,同时增加my_schedule函数用于进程切换。

 

void my_timer_handler(void)
{
	if(time_count % 1000 == 0 && my_need_sched == 0){
		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");

	next = my_current_task->next;
	prev = my_current_task;

	if(next->state == 0){
		my_current_task = next;
    	printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);/* 切换进程 */
    	asm volatile(
    		"pushq %%rbp\n\t"
        	"movq %%rsp,%0\n\t"
        	"movq %2,%%rsp\n\t"
        	"movq $1f,%1\n\t"
        	"pushq %3\n\t"
	        "ret\n\t"
        	"1:\t"
        	"popq %%rbp\n\t"
        	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        	: "m" (next->thread.sp),"m" (next->thread.ip)
  		);
	}
	return;
}

 

代码修改完成后,执行命令:diff -Naur linux-5.4.34 linux-5.4.34-mykernel > mykernel-2.0_for_linux-5.3.34.patch  生成补丁,并重新编译。

再次运行可以看到进程不断切换。

 

 

操作系统内核核心功能及运行工作机制

 操作系统内核的核心功能之一是进程管理,包括对进程的创建和销毁、进程间通信以及实现进程间的切换和对CPU的共享实用。

 以下通过进程切换代码分析其运行工作机制:

asm volatile(
    		"pushq %%rbp\n\t"
        	"movq %%rsp,%0\n\t"
        	"movq %2,%%rsp\n\t"
        	"movq $1f,%1\n\t"
        	"pushq %3\n\t"
	        "ret\n\t"
        	"1:\t"
        	"popq %%rbp\n\t"
        	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        	: "m" (next->thread.sp),"m" (next->thread.ip)
  		);

1)"pushq %%rbp\n\t"

将RBP中的值压入堆栈中,完成对跳转前进程栈基地址的保存
2)"movq %%rsp,%0\n\t"

将RSP中的值存入thread.sp中,完成对跳转前进程RSP的保存
3)"movq %2,%%rsp\n\t"

将next.thread.sp的值传入RSP中,也就是RSP表示的栈顶指针指向下一个进程的内存区
4)"movq $1f,%1\n\t"

将$1f存入thread.ip中
5)"pushq %3\n\t"

将thread.ip的值($1f)压入栈中,因为要完成RIP的存储切换,但RIP不能随意更改,只能通过堆栈操作进行更改,所以使用这样的方法来保存

6)"ret\n\t"

pop next.thread的rip,执行next.thread也就是$1后面的语句
7)"popq %%rbp\n\t"

 pop出的值存入RBP中,也就是RBP指向当前进程的栈底

posted @ 2020-05-13 19:51  _khf  阅读(233)  评论(0)    收藏  举报