一、实验准备
实验要求:
- 按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译;
- 基于mykernel 2.0编写一个操作系统内核,参照https://github.com/mengning/mykernel 提供的范例代码
- 简要分析操作系统内核核心功能及运行工作机制
实验环境:
Ubuntu版本:ubuntu-18.04.4-desktop-amd64
因为原来自带的源太慢了,所以全部换成了阿里云的源。并从台湾大学的镜像下载Linux-5.4.34
二、配置及编译内核
在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 //这个镜像我无法访问 连接失败,换成了ntu的ftp镜像 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 qemu-system-x86_64 -kernel arch/x86/boot/bzImage
依次是获取内核补丁->安装axel->下载并解压Linux内核->安装补丁->安装相应依赖库->内核编译->使用QEMU启动内核
QEMU启动后,窗口中可以看到my_start_kernel在不断的执行,同时my_timer_handler时钟中断处理程序周期性执行。因为是在mymain.c中执行while(1)不断地运行、输出,所以my_start_kernel会不断地输出。
三、编写内核
1、在mykernel目录下增加一个mypcb.h的头文件,用来定义进程控制块,即进程结构体。主要有进程号、进程状态、分配存储区、保存进程的现场、进程入口等变量。
pid:进程号
state:进程状态 -1就绪 0运行 >0即阻塞
stack:进程堆栈
thread:当前县城信息
task_entry:进程入口函数
next:指向下一个PCB,以链表形式存在
#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程序,0号进程是手工创建的,所以需要一些汇编代码。
在mymain.c中添加my_process函数,作为进程的代码来模拟一个进程,本实验中采用进程用完一个时间片后主动让出CPU的调度方式。
#include#include #include #include #include #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 pid); if(my_need_sched == 1) { my_need_sched = 0; my_schedule(); } printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); } } }
3、在时钟中断过程中记录时间片,修改myinterrupt.c的my_timer_handler来记录时间片。
在myinterrupt.c中添加进程切换的代码my_schedule(void),调度策略就是简单的排队等待调度,next=my_current_task->next就是进程调度,调度下一个进程。进程切换是那段汇编代码。
#include#include #include #include #include #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; }
4、重新编译运行内核,效果如下图
四、分析内核核心功能
操作系统的进程在执行过程中,时间片用完后,要先保存当前的进程上下文环境,下次进程被调度执行的时候再恢复。
此次实验模拟了一个具有时钟中断和C语言编译环境的硬件平台,mymain.c中的代码不断地模拟进程执行,同时借助中断处理程序的上下文环境,周期性产生时钟中断信号,触发myinterrupt.c中的程序,产生进程切换。
进程切换的主要实现方法是下面这段代码
asm volatile( "pushq %%rbp\n\t" //将rbp寄存器的值保存在prev进程的堆顶 "movq %%rsp,%0\n\t" //将rsp寄存器的值保存在切换前进程的sp变量中 即将prev进程栈顶位置保存 "movq %2,%%rsp\n\t" //将切换后的进程,即next的栈顶指针sp放入rsp寄存器中,此时,堆栈已经发生切换,之后的操作都是在next进程中 "movq $1f,%1\n\t" //保存切换前进程的下一条指令到ip变量中,此时的prev进行的下一条指令就在1后面 "pushq %3\n\t" //将切换后进程的吓一跳指令地址ip压栈 "ret\n\t" //将栈顶的ip弹出到rip寄存器中 "1:\t" "popq %%rbp\n\t" //将切换后进程的栈定的堆栈基地址弹出,存入rbp寄存器中 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); }
在myinterrupt.c中,my_timer_handler函数会被周期性调用,每调用一千次,就将全局变量my_need_sched的值改为1,当my_start_kernel中的while循环发现这个值变成1后,便会进行进程的调度,完成进程切换,如此往复,模拟进程的切换调度。