标签:
朱宇轲 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
本次课程老师为我们演示了一个简单时间片轮转多道程序内核代码,今天我们讲对它进行运行和分析。
实验截图
需要到github上下载mykernel的源代码并加载到Linux系统中。这里需要注意自己的Linux版本,我之前用的是CentOS,不能执行老师提供的命令,重装了ubuntu之后才终于解决了问题,神坑无比TAT
配置完环境后运行效果如下:
实验分析
这个mykernel内核实际上重点就是三个文件:mypcb.h、mymain.c和myinterrupt.c。它们各有不同的功能,mypcb.h定义了进程管理结构PCB和Thread,mymain.c定义了各个进程的PCB并初始化进程,myinterrupt.c定义了进程主动调度及时钟中断处理。
首先是mypcb.h文件
#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);
PCB定义了每个进程的组织结构,其中pid表示进程号,state代表进程状态,stack为进程的内存空间,thread.sp为进程内置的栈顶指针,thread.ip为进程当前指向的代码。注意next指针,它指向当前的下一个进程,实际上是使该进程组在内存中组织为一个循环链表。
然后是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].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); } } }
该文件的my_start_kernel函数首先初始化了每一个进程,然后执行汇编部分,前两句话目的是将ebp和esp都指向0号进程PCB中的栈底。3,4句则是跳转至进程0的起始地址。
至于my_process函数,则就是每个进程的执行函数。当my_need_sched==1时,进入中断切换到下一个进程。
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 */ { /* 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; }
my_timer_handler应该是一个系统内部运行的累加程序, 用来控制my_need_sched的值,从而来判断是否需要切换进程。
my_schedule则是进行进程切换的主要函数。首先需要判断当前进程是否为空,若否,则进入汇编模块。在汇编模块中,分别存储当前进程的ip、sp等信息,将要执行进程的sp赋给esp,并将它的ip入栈并赋给eip(ret操作),然后系统就会去进入下一个进程的代码空间,执行该进程。需要注意的是,汇编代码中“movl $1f,%1\n\t”这一段是将当前进程目前执行的代码处存储到sp中,当下一次进程调度轮到它时就会直接执行上次执行到的下一行代码。$1表示一个标号,也就是汇编代码段中“1:\t”处,下次再调度到该进程时就会从那里开始执行。
由于待执行的进程状态的不同,my_schedule函数分出了if和else两段代码,其实大同小异,大家可以自己再进一步理解一下其中的差异。
本次课程的代码就分析到这里,谢谢大家!
标签:
原文地址:http://www.cnblogs.com/wickedpriest/p/4340439.html