基于mykernel 2.0编写一个操作系统内核
一:实验要求
- 按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译;
- 基于mykernel 2.0编写一个操作系统内核,参照https://github.com/mengning/mykernel 提供的范例代码
- 简要分析操作系统内核核心功能及运行工作机制
二:搭建实验环境
系统环境 aliyun Ubuntu 18.04.4
配置实验环境:终端依次输入命令
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 # Default configuration is based on 'x86_64_defconfig' 10 make -j$(nproc) //编译 11 sudo apt install qemu # install QEMU 12 qemu-system-x86_64 -kernel arch/x86/boot/bzImage //启动mykernel
第一步需要FQ,我直接从群里给的文件得到的。
运行结果如下:
my_timer_handler是时钟中断程序,每隔一定时间就会运行一次

三:进行实验
1)在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; //定义进程状态 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) //mykernel的入口函数,初始化0号进程以及启动0号进程 { int pid = 0; //初始化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 0号进程作为父进程创建更多子进程,并通过next指针来进行连接*/ 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] 启动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*/ ); } //启动0号进程的过程是:先将0号进程的栈顶指针sp传送给rsp寄存器,再将0号进程的栈顶指针sp和指令指针ip压入栈中
//再将栈中的指令指针ip传送给rip寄存器 int i = 0; //模拟进程调度,采用CPU时间片轮转调度算法的思想 void my_process(void) { while(1) { i++; if(i%120000000 == 0) { printk(KERN_NOTICE "process %d starts\n",my_current_task->pid); if(my_need_sched == 1) //进程切换的标志位,若my_need_sched==1则调用my_schedule()切换进程 { my_need_sched = 0; my_schedule(); } printk(KERN_NOTICE "process %d ends\n",my_current_task->pid); } } }
3)修改myinterrupt.c
#include <linux/types.h> #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 */
//对进程所已经占用的时间片进行统计,CPU产生时钟中断的时候会调用此函数,是time_count自增
//当time_count自增1000次后将my_need_sched标志位置为1,从而在mymain.c中调用my_schedule函数进行进程切换 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 */
//进行进程切换的过程:先将前一个进程(要交出CPU的进程)的rbp寄存器和rsp寄存器分别保存在栈中和前一个进程的sp中
//再将后一个进程(准备占用CPU的进程)中的sp传送给rsp寄存器中
//把前一个进程指令执行的位置保存到前一个进程中的线程的ip中
//先将后一个进程的ip传送进栈中,再取出来传送到rip寄存器中
//从堆栈中取出下一个进程的栈底保存到rbp寄存器中 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) ); } return; }
4)重新编译运行,得到结果:

浙公网安备 33010602011771号