学号后三位:256 原创作品,转载请注明出处。参考资料: https://github.com/mengning/linuxkernel/issues/2
1、mykernel部署
使用实验楼的虚拟机打开shell
依次输入如下指令:
cd LinuxKernel/linux-3.9.4 rm -rf mykernel patch -p1 < ../mykernel_for_linux3.9.4sc.patch make allnoconfig make qemu -kernel arch/x86/boot/bzImage
如图所示:
经过漫长时间的等待。我们得到了如下的界面:
接下来我们输入如下指令:
qemu -kernel arch/x86/boot/bzImage
得到如下的界面:
我们发现有两类字符串不断交替输出。
关闭QEMU窗口,并输入cd mykernel,可以看到在QEMU窗口输出的内容的代码mymain.c和myinterrupt.c。如图所示:
通过分析发现 my_timer_handler here字符串是由myinterrupt.c
中my_timer_handler
函数控制输出的,my_start_kernel here字符串是由mymain.c
中my_start_kernel
函数控制输出的
2、内核代码分析
1.在 https://github.com/mengning/mykernel 提供的代码。下载mymain.c myinterrupt.c mypcb.h三个文件。
2.copy文件,进入mykernel文件夹,覆盖原来的mymain.c myinterrupt.c 新建mypcb.h。
3.重新编译内核,在LinuxKernel/linux-3.9.4文件夹下,执行下面的命令。
make allnoconfig
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*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);
mymain.c
/* * linux/mykernel/mymain.c * * Kernel internal my_start_kernel * * Copyright (C) 2013 Mengning * */ #include#include string.h> #include #include #include #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 ) { memcpy(&task[i],&task[0],sizeof(tPCB)); task[i].pid = i; //*(&task[i].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-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 */ : : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ ); } int i = 0; void my_process(void) { 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); } } }
myinterrupt.c
/* * linux/mykernel/myinterrupt.c * * Kernel internal my_timer_handler * * Copyright (C) 2013 Mengning * */ #include#include string.h> #include #include #include #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; }
其中:
mypcb.h : 进程控制块PCB结构体定义。
mymain.c: 初始化各个进程并启动0号进程。
myinterrupt.c:时钟中断处理和进程调度算法。
pid:进程号
state:进程状态,在模拟系统中,所有进程控制块信息都会被创建出来,其初始化值就是-1,如果被调度运行起来,其值就会变成0
stack:进程使用的堆栈
thread:当前正在执行的线程信息
task_entry:进程入口函数
next:指向下一个PCB,模拟系统中所有的PCB是以链表的形式组织起来的。
my_start_kernel 是系统启动后最先调用的函数,在这个函数里完成了0号进程的初始化和启动,并创建了其它的进程PCB,以方便后面的调度。my_time_handler()函数,是个时间片轮转,周期性地发出中断信号,也就是my_need_sched。
三:操作系统是怎么工作的?
计算机的工作离不开
1.存储程序计算机
2.函数嗲用堆栈机制
3.中断支持
对于1来说,这是显而易见的,没有计算机的支持当然就没有操作系统施展拳脚的空间。
对于2来说,堆栈是C语言程序运行时必须使用的记录函数调用路径和参数存储的空间。
对于3来说,中断信号发生时,CPU把当前正在执行的程序的EIP、ESP寄存器的内容都压到堆栈当中进行保存。这样才能区别于批处理系统