基于mykernel 2.0编写一个操作系统内核
一、实验要求
- 按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译;
- 基于mykernel 2.0编写一个操作系统内核,参照https://github.com/mengning/mykernel 提供的范例代码
- 简要分析操作系统内核核心功能及运行工作机制
二、配置实验环境:mykernel 2.0
打开虚拟机终端,按照https://github.com/mengning/mykernel输入命令
1 wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch 2 sudo apt install axel 3 axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz 4 xz -d linux-5.4.34.tar.xz 5 tar -xvf linux-5.4.34.tar 6 cd linux-5.4.34 7 patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch 8 sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev 9 make defconfig 10 make -j$(nproc) 11 sudo apt install qemu # install QEMU 12 qemu-system-x86_64 -kernel arch/x86/boot/bzImage
使用第一条wget命令下载mykernel-2.0_for_linux-5.4.34.patch时,会提示报错

可以先下载好mykernel-2.0_for_linux-5.4.34.patch 放置到/home/quoqi中
获取内核补丁-->安装axel-->下载解压linux内核-->打补丁-->安装相关依赖库-->内核编译-->使用QEMU启动内核

三、编写内核
1. 在mykernel目录下增加一个mypcb.h头文件,用来定义进程控制块,也就是进程结构体的定义。在结构体pcb中,定义了进程号,进程状态码(-1,0,>0时分别表示阻塞、可运行、停止状态),线程信息,指向下一个pcb的指针,分配存储区、保存进程的现场、进程入口等。
1 #define MAX_TASK_NUM 4 2 #define KERNEL_STACK_SIZE 1024*2 3 /* CPU-specific state of this task */ 4 struct Thread { 5 unsigned long ip; 6 unsigned long sp; 7 }; 8 9 typedef struct PCB{ 10 int pid; 11 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 12 unsigned long stack[KERNEL_STACK_SIZE]; 13 /* CPU-specific state of this task */ 14 struct Thread thread; 15 unsigned long task_entry; 16 struct PCB *next; 17 }tPCB; 18 19 void my_schedule(void);
2.修改原目录下的mymain.c文件。
在mymain.c中添加my_process函数,用来模拟一个进程,这里采用进程运行完一个时间片主动让出CPU的方式(时间片轮转)。
1 #include <linux/types.h> 2 #include <linux/string.h> 3 #include <linux/ctype.h> 4 #include <linux/tty.h> 5 #include <linux/vmalloc.h> 6 7 8 #include "mypcb.h" 9 10 tPCB task[MAX_TASK_NUM]; 11 tPCB * my_current_task = NULL; 12 volatile int my_need_sched = 0; 13 14 void my_process(void); 15 16 17 void __init my_start_kernel(void) 18 { 19 int pid = 0; 20 int i; 21 /* Initialize process 0*/ 22 task[pid].pid = pid; 23 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ 24 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 25 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; 26 task[pid].next = &task[pid]; 27 /*fork more process */ 28 for(i=1;i<MAX_TASK_NUM;i++) 29 { 30 memcpy(&task[i],&task[0],sizeof(tPCB)); 31 task[i].pid = i; 32 task[i].thread.sp = (unsigned long)(&task[i].stack[KERNEL_STACK_SIZE-1]); 33 task[i].next = task[i-1].next; 34 task[i-1].next = &task[i]; 35 } 36 /* start process 0 by task[0] */ 37 pid = 0; 38 my_current_task = &task[pid]; 39 asm volatile( 40 "movq %1,%%rsp\n\t" /* set task[pid].thread.sp to rsp */ 41 "pushq %1\n\t" /* push rbp */ 42 "pushq %0\n\t" /* push task[pid].thread.ip */ 43 "ret\n\t" /* pop task[pid].thread.ip to rip */ 44 : 45 : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ 46 ); 47 } 48 49 int i = 0; 50 51 void my_process(void) 52 { 53 while(1) 54 { 55 i++; 56 if(i%10000000 == 0) 57 { 58 printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid); 59 if(my_need_sched == 1) 60 { 61 my_need_sched = 0; 62 my_schedule(); 63 } 64 printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); 65 } 66 } 67 }
3.修改myinterrupt.c文件。修改myinterrupt.c的my_timer_handler来记录时间片,每完成1000次计数,就进行进程切换。
1 #include <linux/types.h> 2 #include <linux/string.h> 3 #include <linux/ctype.h> 4 #include <linux/tty.h> 5 #include <linux/vmalloc.h> 6 7 #include "mypcb.h" 8 9 extern tPCB task[MAX_TASK_NUM]; 10 extern tPCB * my_current_task; 11 extern volatile int my_need_sched; 12 volatile int time_count = 0; 13 14 /* 15 * Called by timer interrupt. 16 * it runs in the name of current running process, 17 * so it use kernel stack of current running process 18 */ 19 void my_timer_handler(void) 20 { 21 if(time_count%1000 == 0 && my_need_sched != 1) 22 { 23 printk(KERN_NOTICE ">>>my_timer_handler here<<<\n"); 24 my_need_sched = 1; 25 } 26 time_count ++ ; 27 return; 28 } 29 30 void my_schedule(void) 31 { 32 tPCB * next; 33 tPCB * prev; 34 35 if(my_current_task == NULL 36 || my_current_task->next == NULL) 37 { 38 return; 39 } 40 printk(KERN_NOTICE ">>>my_schedule<<<\n"); 41 /* schedule */ 42 next = my_current_task->next; 43 prev = my_current_task; 44 if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ 45 { 46 my_current_task = next; 47 printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); 48 /* switch to next process */ 49 asm volatile( 50 "pushq %%rbp\n\t" /* save rbp of prev */ 51 "movq %%rsp,%0\n\t" /* save rsp of prev */ 52 "movq %2,%%rsp\n\t" /* restore rsp of next */ 53 "movq $1f,%1\n\t" /* save rip of prev */ 54 "pushq %3\n\t" 55 "ret\n\t" /* restore rip of next */ 56 "1:\t" /* next process start here */ 57 "popq %%rbp\n\t" 58 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) 59 : "m" (next->thread.sp),"m" (next->thread.ip) 60 ); 61 } 62 return; 63 }
4.重新编译运行内核
1 make clean
2 make defconfig
3 make -j$(nproc)
4 qemu-system-x86_64 -kernel arch/x86/boot/bzImage
运行效果如下图所示

四、简要分析操作系统内核核心功能及运行工作机制
1.操作系统运行工作机制
操作系统中的进程在执⾏过程中,当进程的一个时间⽚⽤完操作系统需要进⾏进程切换时,需要先保存当前的进程上下⽂环境,下次进程被调度执⾏时,需要恢复进程上下⽂环境。我们通过Linux内核代码模拟了⼀个具有时钟中断和C代码执⾏环境的硬件平台,mymain.c中的代码在不停地执⾏。同时有⼀个中断处理程序的上下⽂环境,周期性地产⽣的时钟中断信号,能够触发myinterrupt.c中的代码,产生进程切换。
2.进程切换的关键代码
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) );
关键代码分析:

• 为了简便,假设系统只有两个进程,分别是进程0和进程1。进程0由内核启动时初始化执⾏,然后需要进程调度和进程切换,然后开始执⾏进程1。 进程切换过程中进程0和进程1的堆栈和相关寄存器的变化过程⼤致如下:
• pushq %%rbp 保存prev进程(本例中指进程0)当前RBP寄存器的值到堆栈;
• 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⼊栈,这时的next->thread.ip可能是进程1的起点my_process(void)函数,也 可能是$1f(标号1)。第⼀次被执⾏从头开始为进程1的起点my_process(void)函数,其余的情况均为$1f(标号1),因为next进程如果之前运⾏过 那么它就⼀定曾经也作为prev进程被进程切换过。
• ret 就是将压⼊栈中的next->thread.ip放⼊RIP寄存器,为什么不直接放⼊RIP寄存器呢?因为程序不能直接使⽤RIP寄存器,只能通过call、 ret等指令间接改变RIP寄存器。
• 1: 标号1是⼀个特殊的地址位置,该位置的地址是$1f。
• popq %%rbp 将next进程堆栈基地址从堆栈中恢复到RBP寄存器中。

浙公网安备 33010602011771号