基于mykernel 2.0编写一个操作系统内核
作者:互联网
mykernel2.0基于linux-5.4.34版本,来源https://github.com/mengning/mykernel,在本实验中首先配置并编译了mykernel2.0,其次进程切换作为操作系统内核最核心的功能,在本实验中,实现了基于时间片轮转调度算法的进程切换功能。
1. 配置并编译mykernel 2.0
1.1 安装开发工具
sudo apt install build-essential gcc-multilib
sudo apt install qemu #一种模拟器
sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev
1.2 下载内核源码
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
wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch#打补丁
patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch
1.3 配置内核
make defconfig # 默认的配置基于'x86_64_defconfig' make menuconfig #图形化界面配置
1.4 编译和运行内核
make -j$(nproc)
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
因为在mykernel目录下,已有mymain.c和myinterrupt.c文件,所以在虚拟机上运行时会出现如下界面,说明mykernel配置完成并正常运行。
1.5制作内存跟文件系统
mkdir rootfs cd rootfs cp ../busybox-1.31.1/_install/* ./ -rf mkdir dev proc sys home sudo cp -a /dev/{null,console,tty,tty1,tty2,tty3,tty4} dev/
准备init文件:
#!/bin/sh mount -t proc none /proc mount -t sysfs none /sys echo "Wellcome MengningOS!" echo "--------------------" cd home /bin/sh
chmod +x init #添加权限
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz#打包成内存根文件系统镜像
2. 编写基于时间片轮转算法的进程切换程序
2.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; /* -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.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\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*/ ); } 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) { my_need_sched = 0; my_schedule(); } printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); } } }
2.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 */ 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 */ 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; }
重新编译内核后,运行qemu,界面如下:
3. 简要分析操作系统内核核心功能及运行工作机制
进程切换基于的时间片轮转算法,当一个进程执行时的时间片耗完,会出发一次时钟中断,cpu会切换到就绪队列中的下一个进程开始执行。
进程切换的过程如下:
1. 当前进程栈基址寄存器 rbp 入栈
2、下一个进程的sp赋值到栈顶寄存器 rsp
3. 将eip寄存器的值入栈保存。
4. 将下一个进程的ip赋值到eip寄存器中。
结尾附上孟老师的讲解视频:自己手写一个操作系统内核
标签:task,process,pid,next,include,内核,mykernel,2.0,my 来源: https://www.cnblogs.com/Luck-365/p/12885179.html