基于mykernel的一个简单的时间片轮转多道程序内核代码分析
学号后三位:492
原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/
实验过程:
一、简单实验
1.1 cd linux-3.9.4
patch -p1 < …/mykernel_for_linux3.9.4sc.patch
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage
1.2 查看mykernel源码
(1)mymain.c
在mymain.c中,我们可以发现有一个my_start_kernel函数,内部是一个无限循环,每当i%100000==0时,打印输出 字符串“my_start_kernel here ”和i的值。 (2)myinterrupt.c
在myinterrupt.c中,有一个被时钟中断周期调用的函数my_timer_handler ,打印输出字符串>>>>>>>my_timer_handler here <<<<<<< 。
二、基于上述实验,修改linux-3.9.4/mykernel/下的mymain.c 和myinterrupt.c完成简单时间片轮转多道程序算法
实验源码来自孟宁老师的GIthub——https://github.com/mengning/mykernel
主要是三个文件mymain.c,myinterrupt.c和mypcb.h
2.1 在修改mymain.c 和myinterrupt.c之前增加一个mypcb.h头文件,用来定义进程控制块,也就是进程结构体的定义,在Linux内核中是struct tast_struct结构体。
2.2 将https://github.com/mengning/mykernel 里的mymain.c,myinterrupt.c复制到linux- 3.94/mykernel/下,覆盖原来的mymain.c,myinterrupt.c文件。
2.3 重新编译内核,启动qemu
2.4 代码分析
2.4.1 mypcb.h
定义进程控制块,用来记录进程的有关信息。
2.4.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].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);
}
}
}
分析:
启动执行第一个进程的关键汇编代码:
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*/
);
下面分析0号进程被初始化时,0号进程的堆栈和相关寄存器的变化过程
movl %1,%%esp
将ESP寄存器指向进程0的堆栈栈底,pid=0,task[pid].thread.sp初始值即为进程0的堆栈栈底。
pushl %1
表示将当前ebp寄存器的值入栈,因为此时的栈是一个空栈,所以esp与ebp指向同一块地址,将task[pid].thread.sp压栈相当于将ebp压栈,此时esp的值减去4字节(32位)。
pushl %0
将当前进程的eip入栈,当前进程eip的初始值为
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process,也就是my_process(void)函数的位置,相应的esp寄存器(向下)减去4字节。
ret
ret指令将栈顶位置的刚刚入栈的task[0].thread.ip也就是my_process(void)函数的位置放入eip寄存器中,相应的esp寄存器(向上)增加4字节。
接下来0号进程启动,开始从my_process(void)函数入口执行。
2.4.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;
}
这里分析进程调度代码:
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)
);
}
在一个进程切换到另一个进程时,首先会将当前ebp到堆栈中:
pushl %%ebp
然后把esp保存到当前进程PCB中:
movl %%esp,%0
接着将next进程的堆栈栈底的值存到esp寄存器:
movl %2,%%esp
保存当前进程的eip值,下次恢复进程后将在标号1开始执行
movl $1f,%1
将next进程继续执行的代码位置(标号1)压栈
pushl %3
出栈标号1到eip寄存器。
ret
总结:
计算机的三大法宝:
一、存储程序计算机;
二、函数调用堆栈;
三、中断。