實(shí)驗(yàn):
首先盛泡,通過使用實(shí)驗(yàn)樓虛擬機(jī)shell搭建好虛擬x86平臺(tái)
然后傍菇,查看myinterrupt.c代碼
執(zhí)行mytimerhandler每次時(shí)鐘中斷時(shí)調(diào)用一次台谊,在中斷處理時(shí)做中斷操作
查看mymain.c代碼
執(zhí)行mystartkernel執(zhí)行操作系統(tǒng)入口爹殊,循環(huán)在每100000次打印一次執(zhí)行信息
構(gòu)造一個(gè)簡(jiǎn)單的操作系統(tǒng)內(nèi)核
mypcb.h
#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;
}tPCB;
void my_schedule(void);
分析:
在頭文件中
-定義了兩個(gè)變量,MAX_TASK_NUM最大任務(wù)數(shù)嫁怀,和KERNEL_STACK_SIZE用來標(biāo)椛杈瑁空間大小
-定義了一個(gè)Tread結(jié)構(gòu),中有兩個(gè)變量ip和sp塘淑,其功能是保護(hù)現(xiàn)場(chǎng)
-定義了一個(gè)PCB結(jié)構(gòu)萝招,即進(jìn)程控制塊,中有六個(gè)變量
--pid進(jìn)程標(biāo)識(shí)
--state狀態(tài)存捺,1為不可運(yùn)行槐沼,0為可運(yùn)行,>0為停止
--stack[KERNEL_STACK_SIZE]定義了一個(gè)和進(jìn)程控制塊自己的棸浦危空間
--thread一個(gè)Tread結(jié)構(gòu)
--task_entry一個(gè)任務(wù)入口點(diǎn)
--next為下一個(gè)PCB結(jié)構(gòu)的指針
-最后定義了一個(gè)my_schedule函數(shù)
mymain.c代碼
#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].thread.sp - 1) = task[i].thread.sp;
task[i].thread.sp -= 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);
}
}
}
分析:
-定義了一個(gè)最大任務(wù)數(shù)為大小的PCB結(jié)構(gòu)數(shù)組
-my_current_task=NULL岗钩,用來指向當(dāng)前PCB的指針
-my_need_sched=0,用來記錄所需schedule數(shù)量
-定義了一個(gè)my_start_kernel函數(shù)
--首先對(duì)0號(hào)進(jìn)程進(jìn)行初始化具滴,設(shè)置其標(biāo)號(hào)凹嘲、狀態(tài)前后關(guān)系、和鏈表關(guān)系變量等
--然后用for循環(huán)對(duì)1號(hào)及其之后變量依次進(jìn)行初始化以fork更多進(jìn)程
--最后通過內(nèi)嵌匯編代碼開始運(yùn)行0號(hào)進(jìn)程塊
---具體操作為將0進(jìn)程中stread.sp值存入esp构韵,當(dāng)前代碼地址入棧,保存現(xiàn)場(chǎng)
-定義了一個(gè)my_process函數(shù)
--用一個(gè)循環(huán)在操作控制臺(tái)顯示進(jìn)程正在運(yùn)行的信息
--也可以用來進(jìn)行進(jìn)程具體操作
myinterrupt.c
#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;
}
分析:
-定義了全局task[MAX_TASK_NUM]一個(gè)PCB數(shù)組
-定義了全局my_current_task一個(gè)PCB指針
-定義了全局整型my_need_sched
-定義了time_count = 0趋艘,一個(gè)計(jì)時(shí)器
-定義了一個(gè)my_time_handler函數(shù)
--該函數(shù)每隔1000ms產(chǎn)生一個(gè)中斷疲恢,并置my_need_sched為1
--用來為my_process函數(shù)調(diào)用my_schedule函數(shù)創(chuàng)造機(jī)會(huì)
-定義了一個(gè)my_schedule函數(shù)
--首先定義兩個(gè)PCB指針next和prev
--若此時(shí)沒有當(dāng)前需要執(zhí)行的進(jìn)程了,即返回
--開始設(shè)置next和prev指針
--此時(shí)開始判定條件
---如果next所指PCB的狀態(tài)為0瓷胧,則切換至next所指PCB開始執(zhí)行
---如果next所指PCB狀態(tài)不為0显拳,則切換至一個(gè)新的PCB開始執(zhí)行
---切換PCB執(zhí)行的動(dòng)作通過內(nèi)嵌匯編代碼來保存現(xiàn)場(chǎng)以便執(zhí)行完成之后恢復(fù)現(xiàn)場(chǎng)
總結(jié):
mypcb.h提供了時(shí)間片輪轉(zhuǎn)進(jìn)程控制塊所需數(shù)據(jù)結(jié)構(gòu),mymain.c對(duì)進(jìn)程進(jìn)行初始化并提供了進(jìn)程開始執(zhí)行的入口搓萧,
myinterrupt.c產(chǎn)生中斷信號(hào)并提供了進(jìn)程管理的核心功能杂数。
其中需要嵌入?yún)R編代碼來完成進(jìn)程切換動(dòng)作宛畦,說明中斷保存現(xiàn)場(chǎng)工作需要由cpu和內(nèi)核代碼共同實(shí)現(xiàn)
王瀟洋
《Linux內(nèi)核分析》MOOC課程http://mooc.study.163.com/course/USTC-1000029000