##《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ”学习笔记 ##
源码组成:mypcb.h mymain.c myinterrupt.c
#define MAX_TASK_NUM 4 //进程数组链表的最大值
#define KERNEL_STACK_SIZE 1024*8 //内核堆栈的大小
/* CPU-specific state of this task */
struct Thread {
unsigned long ip;/*用于保存eip 指向CPU执行的下一条指令的地址*/
unsigned long sp;/*用于保存esp 堆栈指针*/
};
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;/*进程入口 本程序中都设置为my_process*/
struct PCB *next; /*进程链表中的下一个进程*/
}tPCB;
void my_schedule(void);/*用于实现进程上下文的切换调度*/
2.mymain.c
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 */
/*进程入口地址与进程的eip设为my_process*/
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;itask[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];
/*构建0号进程的堆栈环境 ,启动0号进程
c语言中嵌入汇编代码,格式如下:
asm (
内容
: 输出
:输入
);
加入volatitle表示不让编译器优化*/
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)
{
/*调度标志位,在myinterrupt.c的my_timer_handler()函数中设置*/
my_need_sched = 0;
/*主动调用进程列表的下一个进程*/
my_schedule();
}
printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
}
}
}
3.myinterrupt.c
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(
/*保存当前进程的ebp esp*/
"pushl %%ebp\n\t" /* save ebp */
"movl %%esp,%0\n\t" /* save esp */
/*构建下一个进程的esp*/
"movl %2,%%esp\n\t" /* restore esp */
/*$1f是指下面标号为1的位置,就是下一个进程启动的位置*/
"movl $1f,%1\n\t" /* save eip */
/*下一个进程eip压栈*/
"pushl %3\n\t"
/*恢复现场:eip指向下下个进程的地址ebp恢复为第一条指令压栈保存的ebp*/
"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 */
/*新的进程从来没有执行过,所以栈为空esp与ebp指向同一位置*/
"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;
}
4.小结
进程切换过程与函数调用堆栈基本类似