基于mykernel 2.0编写一个操作系统内核
一、实验环境
ubuntu18.04(基于双系统环境 )
二、配置MyKernel 2.0实验环境
打开ubuntu终端,输入以下命令:
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' # 使用allnoconfig编译出来qemu无法加载启动,不知道为什么?有明白的告诉我,完整编译太慢了,消耗的资源也多。 make -j$(nproc) # 编译的时间比较久哦 sudo apt install qemu # install QEMU qemu-system-x86_64 -kernel arch/x86/boot/bzImage
在进行第一步时,发生了wget无法校验证书的情况,所以我直接使用git下载了整个项目。
git clone https://github.com/mengning/mykernel.git 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 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
最后实现的结果如下:
三、实现一个操作系统
首先是mypcb.h,在这里定义了一个进程结构体。
进程结构体结构体中定义了CPU在进行进程调度时的数据成员,包括ip进程指令指针和sp进程堆栈指针。其中
ip进程指令指针对程序员透明,只向CPU调度进程所要执行的指令位置,sp指向当前进程的栈顶。
PCB结构体中主要定义了一个进程的基本信息:
PID:进程在系统中的唯一标识符。
state:表示当前进程的状态,其中-1代表阻塞,0代表运行中,正数代表就绪
stack:当前进程的堆栈空间
task_entry:程序的入口
next:指向下一个PCB进程控制块的指针,用于进程切换.
/* * linux/mykernel/mypcb.h * * Kernel internal PCB types * * Copyright (C) 2013 Mengning * */ #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);
其次是对mymain.c进行修改,这里是mykernel内核代码的入口,负责初始化内核的各个组成部分。这里的作用主要是:定义了多个进程,并确定执行顺序和进程的执行函数。
/* * linux/mykernel/mymain.c * * Kernel internal my_start_kernel * Change IA32 to x86-64 arch, 2020/4/26 * * Copyright (C) 2013, 2020 Mengning * */ #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\n\t" "pushq %1\n\t" "pushq %0\n\t" "ret\n\t" : : "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) {m my_need_sched = 0; my_schedule(); } printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); } } }
最后是模拟进程的切换:
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){ my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);/* 切换进程 */ asm volatile( "pushq %%rbp\n\t" "movq %%rsp,%0\n\t" "movq %2,%%rsp\n\t" "movq $1f,%1\n\t" "pushq %3\n\t" "ret\n\t" "1:\t" "popq %%rbp\n\t" : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }
重新编译并且运行,会出现如下结果: