diff --git a/README.md b/README.md index 0ffe2af..141f3bf 100644 --- a/README.md +++ b/README.md @@ -5,32 +5,48 @@ Develop your own OS kernel by reusing Linux infrastructure, based on x86-64/Linu ## Set up mykernel 2.0 in Ubuntu 18.04 ``` -sudo apt install build-essential -sudo apt install axel -sudo apt install qemu # install QEMU -sudo apt install libncurses-dev bison flex libssl-dev libelf-dev wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.3.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.3.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' -make -j$(nproc) +# 使用allnoconfig编译出来qemu无法加载启动,不知道为什么?有明白的告诉我,完整编译太慢了,消耗的资源也多。 +make -j$(nproc) # 编译的时间比较久哦 +sudo apt install qemu # install QEMU qemu-system-x86_64 -kernel arch/x86/boot/bzImage ``` - + 从qemu窗口中您可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行。 - + cd mykernel 您可以看到qemu窗口输出的内容的代码mymain.c和myinterrupt.c - + 当前有一个CPU执行C代码的上下文环境,同时具有中断处理程序的上下文环境,我们初始化好了系统环境。 - + 您只要在mymain.c基础上继续写进程描述PCB和进程链表管理等代码,在myinterrupt.c的基础上完成进程切换代码,一个可运行的小OS kernel就完成了。 - + start to write your own OS kernel,enjoy it! +从qemu窗口中您可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行。 + +进入mykernel目录您可以看到qemu窗口输出的内容的代码mymain.c和myinterrupt.c。当前有一个CPU执行C代码的上下文环境,同时具有中断处理程序的上下文环境,我们通过Linux内核代码模拟了一个具有时钟中断和C代码执行环境的硬件平台。 + +您只要在mymain.c基础上继续写进程描述PCB和进程链表管理等代码,在myinterrupt.c的基础上完成进程切换代码,一个可运行的小OS kernel就完成了。start to write your own OS kernel, enjoy it! + mykernel-2.0 patch generated by this command: + diff -Naur linux-5.4.34 linux-5.4.34-mykernel > mykernel-2.0_for_linux-5.3.34.patch ## your own OS kernel example code -* mypcb.h、mymain.c和myinterrupt.c待升级到mykernel 2.0,欢迎pr +* mypcb.h、mymain.c和myinterrupt.c实现了一个简单的时间片轮转调度进程的精简内核,如下为进程上下文切换的关键代码: +``` + 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) + ); +``` ## Comments