打开shell
使用make命令编译内核
找到一个简单的时间片轮转多道程序内核代码mypcb.h;mymain.c;myinterrupt.c;
然后替换位于home/shiyanlou/LinuxKernel/linux-3.9.4/mykernel/中的mymain.c;myinterrupt.c;
将mypcb.h也放在这里。
接下来在shell中将当前工作目录退回到home/shiyanlou/LinuxKernel/linux-3.9.4/
然后执行make,重新编译内核,再次执行qemu -kernel arch/x86/boot/bzImage命令。
#define MAX_TASK_NUM 4 //最大进程数,这里设置为了4个。
#define KERNEL_STACK_SIZE 1024*8 //每个进程的内核栈的大小。
/* CPU-specific state of this task */
struct Thread {
unsigned long ip;//用于保存进程的eip
unsigned long sp;//用户保存进程的esp
};
typedef struct PCB{
int pid;//进程的id号
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);
#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[0].pid=0;
task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;//令0号进程的入口地址为my_process();
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];//0号进程的栈顶为stack[]数组的最后一个元素
task[pid].next = &task[pid];//next指针指向自己
/*fork more process */
for(i=1;i<MAX_TASK_NUM;i++)//根据0号进程,复制出几个只是编号不同的进程
{
memcpy(&task[i],&task[0],sizeof(tPCB));//void *memcpy(void *dest, const void *src, size_t n);从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
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;//新创建的进程的next指向0号进程的首地址
task[i-1].next = &task[i];//前一个进程的next指向最新创建的进程的首地址,从而成为一个循环链表。
}
/* start process 0 by task[0] */
pid = 0;
my_current_task = &task[pid];//当前运行的进程设置为0号进程。
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);//打印+号
}
}
}
#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;//将下一个将要运行的进程设置为my_current_task->next指向的下一个进程。
prev = my_current_task;//将当前进程设置为prev进程。
if(next->state == 0)/*如果下一个将要运行的进程已经处于运行状态 -1 unrunnable, 0 runnable, >0 stopped */
{
/* switch to next process */
asm volatile(
"pushl %%ebp\n\t" /* 保存当前进程的ebp到自己的栈中。 save ebp */
"movl %%esp,%0\n\t" /* 保存当前进程的esp到自己的栈中。 save esp */
"movl %2,%%esp\n\t" /* 从next->thread.sp中弹出下一个进程的esp。与第二句相对应。 restore esp */
"movl $1f,%1\n\t" /* 将下一个进程的eip设置为1f。$1f就是指标号1:的代码在内存中存储的地址 save eip */
"pushl %3\n\t" /* 将next->thread.ip压入当前进程的栈中。*/
"ret\n\t" /* 从当前进程的栈中弹出刚刚压入的next->thread.ip。完成进程切换。 restore eip */
"1:\t" /* 即$1f指向的位置。next process start here */
"popl %%ebp\n\t" /* 切换到的进程把ebp从栈中弹出至ebp寄存器。与第一句相对应。*/
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
my_current_task = next; //当前进程切换为next
printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); //打印切换信息
}
else//如果下一个将要运行的进程还从未运行过。
{
next->state = 0;//将其设置为运行状态。
my_current_task = next;当前进程切换为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" /* 将要被切换出去的进程的ip设置为$1f。这样等一下它被切换回来时(一定是运行状态)肯定会进入if判断分支,可以从if中的标号1处继续执行。 save eip */
"pushl %3\n\t" /* 将next->thread.ip(因为它还没有被运行过,所以next->thread.ip现在仍处于初始状态,即指向my_process(),压入将要被切换出去的进程的堆栈。*/
"ret\n\t" /* 将刚刚压入的next->thread.ip出栈至eip,完成进程切换。 restore eip */
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
一些疑惑及解答