基于mykernel 2.0编写一个操作系统内核
1.下载linux-5.4.34和mykernel
在kernel.org下载内核速度特别慢,推荐在USTC开源镜像站下载,速度飞起.linux-5.4.34的下载地址是
https://mirrors.ustc.edu.cn/kernel.org/linux/kernel/v5.x/linux-5.4.34.tar.xz
mykernel的代码直接通过git clone即可
git clone https://github.com/mengning/mykernel.git
通过下面的命令将下载的内核进行解压
xz -d linux-5.4.34.tar.xz tar -xvf linux-5.4.34.tar
最后的目录结构如下
2. 将patch整合到内核代码中
cd linux-5.4.34 patch -p1 < ../mykernel/mykernel-2.0_for_linux-5.4.34.patch
3. 安装编译内核所需的库
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
我的机器上之前已经安装过
4. 编译内核(时间较长)
make defconfig # Default configuration is based on 'x86_64_defconfig'
make -j$(nproc) # 编译的时间比较久哦
5. 安装QEMU虚拟机并加载内核运行
在终端运行以下代码(我的机器之前已经安装过)
sudo apt install qemu
通过下面的代码加载内核
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
从qemu窗口中您可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行。这样我们就模拟了⼀个具有时钟中断和C代码执⾏环境的硬件平台
6. 将自己的程序以及进程调度模块整合进内核
切换到内核目录下的mykernel文件夹,将github中的myinterrupt.c,mymain.c,mypcb.h文件复制到该文件夹中,然后重新编译内核.再将内核加载到QEMU中运行.
通过下面的截图我们看到了中断发生后进程调度模块工作,进程1和进程2发生切换.
7. 代码分析(见注释)
7.1 mypcb.c
#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; }; /* 进程有唯一的pid标识 进程共有三个状态(与操作系统原理中不同) 每个进程有自己的栈,大小为2KB 每个进程有一个线程用于抢占物理CPU时间 进程有自己任务入口 多个进程通过循环链表进行组织,所以当前进程还有指向下一个进程的指针 */ 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);
7.2 myinterrupt.c
#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<<<\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" /* 保存rsp的值到当前进程PCB的thread.sp*/ "movq %2,%%rsp\n\t" /* 设置rsp为scheduler选中的下一个进程的sp*/ "movq $1f,%1\n\t" /* $1f指向label1代码段的位置 */ "pushq %3\n\t" "ret\n\t" /* 64,65l两行共同完成设置rip的值为next.thread.ip*/ "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; }
7.3 mymain.c
#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]; //创建4个PCB tPCB * my_current_task = NULL; volatile int my_need_sched = 0; void my_process(void); void __init my_start_kernel(void) { int pid = 0; //进程id从0开始增长 int i; /* Initialize process 0*/ task[pid].pid = pid; task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ // 进程初始化时thread.ip和task_entry相同,都是指向my_process的程序入口 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; // 将进程栈的高地址赋给0号进程的thread.sp task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; // 0号进程的next指针指向自己(循环链表的边界情况) task[pid].next = &task[pid]; /*fork more process */ for(i=1;i<MAX_TASK_NUM;i++) { memcpy(&task[i],&task[0],sizeof(tPCB)); // fork后的进程state不用修改, 或者说进程创建时默认状态就是runnable // 这里task_entry也没有修改,因为四个进程的代码块都是同一个my_process 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); } } }