基于mykernel完成多进程的简单内核

学号076,本实验资源来源

1.实验准备:https://github.com/mengning/mykernel

由于在pc上总是装不好qemu,所以采用实验楼配置好的虚拟机进行实验。

打开虚拟机的终端,依次输入以下命令:

cd LinuxKernel/linux-3.9.4

rm -rf mykernel

patch -p1 < ../mykernel_for_linux3.9.4sc.patch

make allnoconfig

结果如下:

再输入命令make,进行内核编译,结果如下:

在终端输入命令qemu -kernel arch/x86/boot/bzImage,结果如下图所示:

关闭QEMU窗口,终端输入cd mykernel,再输入ls可以查看mykernel文件夹里的内容,可以看到里面包含mymain.c以及myinterrupt.c

输入vim mymain.c,可以看到mymain.c的内容如下:

my_start_kernel函数中有一个循环,不停地输出my_start_kernel here,同理在终端输入vim myinterrupt.c,可以看到myinterrupt.c的内容如下:

my_timer_handler函数会被时钟中断周期调用,输出类似>>>>>my_timer_handler here <<<<< 的字符串。

2.一个简单的时间片轮转多道程序

(1)步骤:

https://github.com/mengning/mykernel下的mypcb.h、mymain.c和myinterrupt.c复制到mykernel目录下,取代原来的myinterrupt.c、mymain.c。对myinterrupt.c、mymain.c的内容进行修改,具体代码后面会进行相关描述,在终端输入命令make编译内核,编译完成后,输入命令qemu -kernel arch/x86/boot/bzImage运行内核程序,运行结果如下图所示:

(2)源代码分析

这里主要分析上面复制的3个文件,其作用简述如下:

mypcb.h : 进程控制块PCB结构体定义。
mymain.c: 初始化各个进程并启动0号进程。
myinterrupt.c:时钟中断处理和进程调度算法。
详细分析:首先分析mypcb.h,代码如下:

/*
 *  linux/mykernel/mypcb.h
 *
 *  Kernel internal PCB types
 *
 *  Copyright (C) 2013  Mengning
 *
 */
 
#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*8
 
/* 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 */
    char 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);

 

结构体Thread 结构体,用于存储当前进程中正在执行的线程的ip和sp,PCB结构体中的各个字段含义如下
pid:进程号

state:进程状态,-1表示就绪态,0表示运行态,大于0表示阻塞态

stack:进程使用的堆栈

thread:当前正在执行的线程信息

task_entry:进程入口函数

next:指向下一个PCB,模拟系统中所有的PCB是以链表的形式组织起来的。

函数声明 my_schedule,它的实现是在my_interrupt.c中,在mymain.c中的各个进程函数会根据一个全局变量的状态来决定是否调用它,从而实现主动调度。

接着分析mymain.c,代码如下:

/*
	 *  linux/mykernel/mymain.c
	 *
	 *  Kernel internal my_start_kernel
	 *
	 *  Copyright (C) 2013  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].state = -1;
			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(
			"movl %1,%%esp\n\t" 	/* set task[pid].thread.sp to esp */
			"pushl %1\n\t" 	        /* push ebp */
			"pushl %0\n\t" 	        /* push task[pid].thread.ip */
			"ret\n\t" 	            /* pop task[pid].thread.ip to eip */
			"popl %%ebp\n\t"
			: 
			: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)	/* input c or d mean %ecx/%edx*/
		);
	}   
	void my_process(void)
	{
		int i = 0;
		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);
			}     
		}
	}

 

系统启动后最先调用函数 my_start_kernel ,完成了0号进程的初始化和启动,并创建了其它的进程PCB便于调度。my_process 函数在执行的时候,会打印出当前进程的 id,从而使得我们能够看到当前哪个进程正在执行。

另外,在 my_process 也会检查一个全局标志变量 my_need_sched,一旦发现其值为 1 ,就调用 my_schedule 完成进程的调度。

0号线程的启动,嵌入了汇编代码,详细参见源码中的注释。
最后,分析myinterrupt.c,代码如下:

/*
 *  linux/mykernel/myinterrupt.c
 *
 *  Kernel internal my_timer_handler
 *
 *  Copyright (C) 2013  Mengning
 *
 */
#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 1
    if(time_count%1000 == 0 && my_need_sched != 1)
    {
        printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
        my_need_sched = 1;
    } 
    time_count ++ ;  
#endif
    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 */
    {
    	/* switch to next process */
    	asm volatile(	
        	"pushl %%ebp\n\t" 	    /* save ebp */
        	"movl %%esp,%0\n\t" 	/* save esp */
        	"movl %2,%%esp\n\t"     /* restore  esp */
        	"movl $1f,%1\n\t"       /* save eip */	
        	"pushl %3\n\t" 
        	"ret\n\t" 	            /* restore  eip */
        	"1:\t"                  /* next process start here */
        	"popl %%ebp\n\t"
        	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        	: "m" (next->thread.sp),"m" (next->thread.ip)
    	); 
    	my_current_task = next; 
    	printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);   	
    }
    else
    {
        next->state = 0;
        my_current_task = next;
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
    	/* switch to new process */
    	asm volatile(	
        	"pushl %%ebp\n\t" 	    /* save ebp */
        	"movl %%esp,%0\n\t" 	/* save esp */
        	"movl %2,%%esp\n\t"     /* restore  esp */
        	"movl %2,%%ebp\n\t"     /* restore  ebp */
        	"movl $1f,%1\n\t"       /* save eip */	
        	"pushl %3\n\t" 
        	"ret\n\t" 	            /* restore  eip */
        	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        	: "m" (next->thread.sp),"m" (next->thread.ip)
    	);          
    }   
    return;	
}

 

my_timer_handler 函数会被内核周期性的调用,每调用1000次,就去将全局变量my_need_sched的值修改为1,通知正在执行的进程执行调度程序my_schedule。在my_schedule函数中,完成进程的切换。进程的切换分两种情况,一种情况是下一个进程没有被调度过,另外一种情况是下一个进程被调度过,可以通过下一个进程的state知道其状态。进程切换依然是通过内嵌汇编代码实现,无非是保存旧进程的eip和堆栈,将新进程的eip和堆栈的值存入对应的寄存器中,详见代码中的注释。

3.总结

通过此次实验可以得出:现代操作系统的核心是进程的调度与中断机制,需要利用堆栈空间,通过esp堆栈指针寄存器、ebp基址指针寄存器和cs:eip寄存器等关键寄存器来保存和恢复现场,进行进程上下文的切换。进程的切换需要与计算机的硬件相配合,操作系统通过进程的中断与切换使CPU时间合理分配,保证计算机的性能发挥。

 

posted @ 2019-03-11 23:49  若我  阅读(324)  评论(0编辑  收藏  举报