基于mykernel 2.0编写一个操作系统内核
1.配置mykernel 2.0,熟悉Linux内核的编译;
实验环境::Ubuntu 16.04.1(虚拟机上)
一次键入如下代码:
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' make -j$(nproc) # 编译的时间比较久哦 sudo apt install qemu # install QEMU qemu-system-x86_64 -kernel arch/x86/boot/bzImage
在输入 patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch 时报错,bash: ../mykernel-2.0_for_linux-5.4.34.patch: 没有那个文件或目录,将patch文件复制进根目录后解决。
执行qemu后,结果如下:
2.在mykernel目录下进行程序的编写:
1)在mykernel目录中添加 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);
2)修改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]; 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 " /* set task[pid].thread.sp to rsp */ "pushq %1 " /* push rbp */ "pushq %0 " /* push task[pid].thread.ip */ "ret " /* 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 - ",my_current_task->pid); if(my_need_sched == 1) { my_need_sched = 0; my_schedule(); } printk(KERN_NOTICE "this is process %d + ",my_current_task->pid); } } }
可以看到在my_process中有一个进程切换标志位:my_need_sched,在该标志位为1时进行进程切换,通过改写中断服务程序来定时改变标志位,起到时间片调度的效果。
3)修改myinerrupt.c
void my_timer_handler(void) { if(time_count%1000 == 0 && my_need_sched != 1) { printk(KERN_NOTICE ">>>my_timer_handler here<<< "); 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<<< "); /* 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<<< ",prev->pid,next->pid); /* switch to next process */ asm volatile( "pushq %%rbp " /* save rbp of prev */ "movq %%rsp,%0 " /* save rsp of prev */ "movq %2,%%rsp " /* restore rsp of next */ "movq $1f,%1 " /* save rip of prev */ "pushq %3 " "ret " /* restore rip of next */ "1: " /* next process start here */ "popq %%rbp " : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }
my_timer_handler⽤来记录时间⽚,每完成1000次计数,就进行进程切换,在my_schedule中也增加了进程切换的代码。
执行
3.分析内核核心功能
运行工作机制:操作系统的进程在执⾏过程中,当时间⽚⽤完需要进⾏进程切换时,需要先保存当前的进程上下⽂环境,下次进程被调度执⾏时,需要恢复进程上下⽂环境。我们通过Linux内核代码模拟 了⼀个具有时钟中断和C代码执⾏环境的硬件平台,mymain.c中的代码在不停地执⾏。同时有⼀个中断处理程序的上下⽂环境,周期性地产⽣的时钟中断信号,能够触发myinterrupt.c中的代码,产生进程切换。
这段汇编代码就是进程切换的主要实现方法。分析如下:
1)pushq %%rbp:将rbp寄存器值保存在切换前进程(prev)的堆顶。
2)movq %%rsp,%0:将rsp寄存器值保存在切换前进程的sp变量中。(将prev进程栈顶位置保存)
3)movq %2,%%rsp:将切换后进程(next)的栈顶指针sp放入rsp寄存器。(此时已发生堆栈切换,之后的堆栈操作都是在next进程中的)
4)movq $1f,%1:保存切换前进程的下一条指令地址到ip变量中。这里prev进程的下一条指令就在标号1后面。
5)pushq %3:这里将切换后进程的下一条指令地址ip压栈(rip寄存器程序员没有权限进行写入,需要多一个步骤)
6)ret :将栈顶的ip,pop出来到rip寄存器(此时进程执行指令也被切换成功)
7)popq %%rbp:将切换后进程的栈顶的堆栈基地址pop出来,放入rbp寄存器