黄志恒
原创作品转载请注明出处
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ”
这篇文章继续Linux操作系统工作原理的分析。文章题目的两把剑是老师总结的,我深以为然。是这两把剑让系统能够对程序进行调度,也是这两把剑让系统中的各个程序的运行秩序井然。
本文依然以一个程序为线索来分析Linux操作系统的工作原理。这个程序与一般的程序有些不同。这个程序是模仿一个Linux内核(不停的循环运行),有多个进程(模拟)运行在内核中,然后内核进行切换。这个内核不是凭空构想出来的,而是在真实Linux内核源码的基础上精简修改而来。所以是十分值得学习的。废话不多说,上代码。
在实验楼的环境中,将mypch.h,mymain.c,myinterrupt.c三个文件替换../LinuxKernel/linux-3.9.4/mykernel文件夹中的同名文件,然后在linux-3.9.4目录下make,变可以编译。之后执行 qemu -kernel arch/x86/boot/bzImage
运行如图所示
下面来挨个分析代码,首先是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);
这里面主要是定义了2个结构体和一个函数。
Thread结构体根据名称可以想到是来模拟一个进程的数据结构,这里的ip是用来存储该进程向下一条指令的地址,sp是存储该进程的栈的栈顶指针的地址。
PCB结构体是用来模拟一个进程控制块的。
pid是进程号。
state是进程状态:-1表示不可运行,0表示正在运行,>0表示停止。
stack[]表示该进程栈。
thread表示该进程。
task_entry用来存储该进程的入口地址。
*next 用来指向下一个进程控制块。
my_schedule(void)函数在myinterrupt.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);
}
}
}
void __init my_start_kernel(void)是内核启动时调用的函数。
这个函数做了这么几个事:
1、初始化一个初始进程,并把它的状态设置为运行态(state=0)。
2、初始了MAX_TASK_NUM个进程,把这些进程的状态设置为不可执行(state=-1)。
3、运行“O”号进程。即:将0号进程的esp和ip压栈。
void my_process(void)函数
这个函数中有while(1)语句,所以这个函数是不会自动停止的。它的功能是将my_need_sched置为0,然后调用进程切换程序my_schedule();
最后是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;
}
void my_timer_handler(void)函数
功能:每1000次就做一次判断,输出一段语句,然后将my_need_sched置为1。
void my_schedule(void)函数,重头戏,具体如下。
首先是定义两个PCB,prev和next。这里先抛出一个概念:在进程切换中,prev将会在某个时间点变为next。具体会有具体解释,先记住这个概念。
然后做一个判空操作。
之后,对prev和next赋值。然后判断,当next的状态是可执行时,则做:
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;
解释:%0=prev->thread.sp %1=prev->thread.ip %2=next->thread.sp %3=next->thread.ip
"pushl %%ebp\n\t":将当前的栈底指针压栈,即保存栈底指针。
"movl %%esp,%0\n\t":令prev->thread.sp=esp,即保存当前进程的栈顶地址(因为esp保存着栈顶指针,栈顶是被执行的地方。所以恢复后可以将栈顶恢复。)
"movl %2 %%esp\n\t":令esp=next->thread.sp,即将栈顶指向next进程的栈顶。
"movl $1f,%1\n\t":令prev->thread.ip=eip。这里$1f是编译器提供的一个标号,此标号意味着"1"中保存着当前eip的地址。所以这句话是保存当前进程的eip地址。
"pushl %3\n\t":将next->thread.ip压栈,下面就开始执行next进程了。
"ret\n\t":返回
"1:\t":1标号,代表着eip的地址
"popl %%ebp\n\t":这句代码有待商榷。因为执行ret后,此代码是不会执行到的。
my_current_task=next;:将next变为current,继续后面的进程切换。
这里解释下之前提到的:在进程切换中,prev将会在某个时间点变为next。在进程切换中,prev切换到next,但是切换总会在切回prev这个进程中。如果系统始终没有切换回prev这个进程中,那么prev在被切出去后就永远得不到执行。这与中断和进程切换的功能相悖。所以,当某一时刻,切换回prev进程时,这个prev就成为了next。这就是这段代码能够在prev和next间切换的原理。
下面,当next的状态是非“可执行”态的时候,程序做:
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)
);
首先,将next的状态变为可运行态:next->state=0
然后,将next变为current。
之后做:
解释:%0=prev->thread.sp %1=prev->thread.ip %2=next->thread.sp %3=next->thread.ip
"pushl %%ebp\n\t":将ebp压栈
"movl %%esp,%0\n\t":令prev->thread.sp=esp,即保存当前进程的栈顶地址。
"movl %2,%%esp\n\t":令esp=next->thread.sp,即将栈顶指针指向next的栈顶的地址
"movl %2,%%ebp\n\t":令ebp=next->thread.sp,即将栈底指针也指向next的栈顶的地址(典型的函数开头的汇编代码:pushl %ebp,movl %esp,%ebp)
"movl $1f,%1\n\t":令prev->thread.ip=eip,即保存当前的eip的地址。$1f的作用上面有解释,不在重复。
这样,就可以完成了对处于不可运行态的next进程的切换。
到此为止,这段程序就解读完了。
总结:
通过将Linux内核的代码精简和修改,我们可以很容易,很清晰的了解中断和上下文切换的原理。用一句话概括就是:保存当前进程的esp和eip,然后将栈和eip交个将要运行的进程,进入这个进程执行代码。当然实际的内核执行远比这个复杂的多,需要考虑的细节更多。最后感谢老师的知道,和论坛上同学们的帮助!