基于mykernel的时间片轮转多道程序内核代码分析
学号:055
原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/
mykernel 简介
It is a platform to write your own OS kernel,its based on Linux Kernel 3.9.4 source code. 来自中科大孟宁老师的Github
mykernel是一个精致小巧的linux模拟内核,对于初学者理解linux内核有很大的帮助。
你可以轻松的从孟宁老师的Github中找到mykernel的安装配置方法。
也可以选择实验楼提供的在线虚拟环境。
实验环境
实验楼提供的虚拟环境,https://www.shiyanlou.com/courses/195
实验要求
- 完成一个简单的时间片轮转多道程序内核代码
- 详细分析该精简内核的源代码并给出实验截图
- 需要仔细分析进程的启动和进程的切换机制
- 阐明自己对“操作系统是如何工作的”理解
实验过程
编译
cd LinuxKernel/linux-3.9.4
rm -rf mykernel
patch -p1 < ../mykernel_for_linux3.9.4sc.patch
make allnoconfig
make # 编译内核请耐心等待
编译结束后,可以看到如下画面
在qemu下启动
qemu -kernel arch/x86/boot/bzImage
可以看到一个简单的系统已经运行起来了,不过只是简单的输出>>>>>>>>my_timer_handler here <<<<<<<<
和 my_start_kernel here
这是为什么呢?
我们打开mykernel目录,查看一下源代码。
在mymain.c
的 void __init my_start_kernel
中,我们看到了如下代码:
在myinterrupt.c
中我们看到了如下代码
这就很容易解释最初循环显示>>>>>>>>my_timer_handler here <<<<<<<<
和 my_start_kernel here
的原因了。
理解了上述原因后,就可以动手实现我们的实验任务了
实现
幸运的是,孟宁老师为我们提供了代码参考,我们可以拷贝下来使用。
git clone https://github.com/mengning/mykernel
完成后,将mykernel中的myinterrupt.c
,mymain.c
和mypcb.h
替换LinuxKernel/linux-3.9.4/mykernel文件夹中的myinterrupt.c
和mymain.c
随后,输入以下命令重新编译
cd ~ # 回到home/shiyanlou目录
cd LinuxKernel/linux-3.9.4
make allnoconfig
make //重新编译
qemu -kernel arch/x86/boot/bzImage
编译时出现了错误,将#unsigned long删除掉即可
编译完成后,启动qemu运行
qemu -kernel arch/x86/boot/bzImage
可以看到,系统从process1 切换到 process2
源码分析
我们主要分析mypcb.h、mymain.c和 myinterrupt.c,如下
####(1)mypcb.h
/*
* linux/mykernel/mypcb.h
*
* Kernel internal PCB types
*
* Copyright (C) 2013 Mengning
*
*/
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2 # unsigned long
/* 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; // 下一个PCB的地址
}tPCB;
void my_schedule(void);
头文件定义了进程控制块PCB,线程,声明了函数my_schedule。
各个字段的含义见代码注释
(2) 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].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-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 */
:
: "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);
}
}
}
(3) 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 */
{
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
/* 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)
);
}
return;
}
总结
此次实验加深了对操作系统的理解,从进程的观点来看,操作系统是由若干个可以并发执行的进程和一个对进程进行控制和协调的核心组成。 现代操作系统的核心是进程的调度与中断机制,通过软件和硬件来实现,为上层用户提供透明的高性能的环境。