1.实验环境搭建
- VMware
- win10
- Ubuntu 18.04 虚拟机
实验步骤:
在内核linux-5.4.34得基础上打补丁,用虚拟机qemu运行内核。
实验使用Axel下载Linux内核,Axel是一个命令行下载工具,支持多来源、多线程,能够保证下载的速度和稳定性。
Ubuntu缺省情况下,并没有提供C/C++的编译环境,build-essential提供基本的完整C/C++的编译环境。
libncurses-dev、bison、flex、libelf-dev提供编译和调试内核所需的软件包。libssl-dev用于提供wegt安全连接(有些下载需要,eg:github)。
准备工作完成后,进入内核的根目录,用patch命令加入补丁,make编译,编译成功后用qemu运行编译出的内核。
可以看到下图的不停的时间中断和打印my_start_kernel。
sudo apt install axel sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev sudo apt install qemu wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch 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 make defconfig make -j$(nproc) qemu-system-x86_64 -kernel arch/x86/boot/bzImage
2.基于mykernel 实现简单的时间片轮转
时间片轮转算法的基本思想是,在每次时钟中断执行调度,根据进程的信息决定是否执行,一般是基于优先级判断,不过出于简单开发的考虑,循环遍历所有进程,每一个进程不会连续执行。
为了实现上述的需求,至少需要描述进程的数据结构,调度算法,进程上下文切换,时钟中断触发调度等功能。
我们从定义描述进程的数据结构PCB出发,并依次实现调度算法和进程上下问切换,最后实现时钟中断触发调度。
首先进入内核根目录下的mykernel目录。
在mypch.h中,定义进程控制块PCB:
1 #define MAX_TASK_NUM 4 2 #define KERNEL_STACK_SIZE 1024*2 3 /* CPU-specific state of this task */ 4 struct Thread { 5 unsigned long ip; 6 unsigned long sp; 7 }; 8 9 typedef struct PCB{ 10 int pid; 11 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 12 unsigned long stack[KERNEL_STACK_SIZE]; 13 /* CPU-specific state of this task */ 14 struct Thread thread; 15 unsigned long task_entry; 16 struct PCB *next; 17 }tPCB; 18 19 void my_schedule(void);
实现mymain.c:
1 #include2 #include string.h> 3 #include 4 #include 5 #include 6 7 8 #include "mypcb.h" 9 10 tPCB task[MAX_TASK_NUM]; 11 tPCB * my_current_task = NULL; 12 volatile int my_need_sched = 0; 13 14 void my_process(void); 15 16 17 void __init my_start_kernel(void) 18 { 19 int pid = 0; 20 int i; 21 /* Initialize process 0*/ 22 task[pid].pid = pid; 23 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ 24 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 25 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; 26 task[pid].next = &task[pid]; 27 /*fork more process */ 28 for(i=1;i ) 29 { 30 memcpy(&task[i],&task[0],sizeof(tPCB)); 31 task[i].pid = i; 32 task[i].thread.sp = (unsigned long)(&task[i].stack[KERNEL_STACK_SIZE-1]); 33 task[i].next = task[i-1].next; 34 task[i-1].next = &task[i]; 35 } 36 /* start process 0 by task[0] */ 37 pid = 0; 38 my_current_task = &task[pid]; 39 asm volatile( 40 "movq %1,%%rsp\n\t" /* set task[pid].thread.sp to rsp */ 41 "pushq %1\n\t" /* push rbp */ 42 "pushq %0\n\t" /* push task[pid].thread.ip */ 43 "ret\n\t" /* pop task[pid].thread.ip to rip */ 44 : 45 : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ 46 ); 47 } 48 49 int i = 0; 50 51 void my_process(void) 52 { 53 while(1) 54 { 55 i++; 56 if(i%10000000 == 0) 57 { 58 printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid); 59 if(my_need_sched == 1) 60 { 61 my_need_sched = 0; 62 my_schedule(); 63 } 64 printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); 65 } 66 } 67 }
实现myinterrupt.c,时间中断触发调度:
1 #include2 #include string.h> 3 #include 4 #include 5 #include 6 7 #include "mypcb.h" 8 9 extern tPCB task[MAX_TASK_NUM]; 10 extern tPCB * my_current_task; 11 extern volatile int my_need_sched; 12 volatile int time_count = 0; 13 14 /* 15 * Called by timer interrupt. 16 * it runs in the name of current running process, 17 * so it use kernel stack of current running process 18 */ 19 void my_timer_handler(void) 20 { 21 if(time_count%1000 == 0 && my_need_sched != 1) 22 { 23 printk(KERN_NOTICE ">>>my_timer_handler here<<<\n"); 24 my_need_sched = 1; 25 } 26 time_count ++ ; 27 return; 28 } 29 30 void my_schedule(void) 31 { 32 tPCB * next; 33 tPCB * prev; 34 35 if(my_current_task == NULL 36 || my_current_task->next == NULL) 37 { 38 return; 39 } 40 printk(KERN_NOTICE ">>>my_schedule<<<\n"); 41 /* schedule */ 42 next = my_current_task->next; 43 prev = my_current_task; 44 if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ 45 { 46 my_current_task = next; 47 printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); 48 /* switch to next process */ 49 asm volatile( 50 "pushq %%rbp\n\t" /* save rbp of prev */ 51 "movq %%rsp,%0\n\t" /* save rsp of prev */ 52 "movq %2,%%rsp\n\t" /* restore rsp of next */ 53 "movq $1f,%1\n\t" /* save rip of prev */ 54 "pushq %3\n\t" 55 "ret\n\t" /* restore rip of next */ 56 "1:\t" /* next process start here */ 57 "popq %%rbp\n\t" 58 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) 59 : "m" (next->thread.sp),"m" (next->thread.ip) 60 ); 61 } 62 return; 63 }