操作系统是如何工作的
内核实现
本周学习内容是实现一个简单的时间片轮转多道程序内核。
首先需要在Linux虚拟机中再构建一个虚拟的x86CPU硬件平台。这个平台的构建我们利用了部分Linux 3.9.4版本源代码以及网上的内核源代码。
内核搭建好后其启动效果如下:
而后,我们需要在搭建好的内核的基础上,修改内核的源程序,构建我们所需的程序内核。
首先我们增加一个mypcb.h头文件,用于定义进程控制块(PCB)的数据结构。我们知道,在操作系统中,每一个进程都依赖于一个PCB,以用于进程调度,来实现进程的并发执行。因此,我们的PCB结构定义如下:
struct Thread {
unsigned long ip; //进程eip
unsigned long sp; //进程esp
};
typedef struct PCB{
int pid; //进程id
volatile long state; //进程状态
char stack[KERNEL_STACK_SIZE]; //进程堆栈
struct Thread thread; //
unsigned long task_entry; //进程的起始入口地址
struct PCB *next; //指向下一个进程
}tPCB;
void my_schedule(void); //进行进程调度
然后,我们修改一下mymain.c文件,此文件为内核的入口,用于内核各组件的初始化。我们通过这段程序来启动内核的0号进程,其代码如下:
#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;
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];
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];
}
pid = 0;
my_current_task = &task[pid];
asm volatile(
"movl %1,%%esp
"
"pushl %1
"
"pushl %0
"
"ret
"
"popl %%ebp
"
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
);
}
void my_process(void)
{
int i = 0;
while(1)
{
i++;
if(i%10000000 == 0)
{
printk(KERN_NOTICE "this is process %d -
",my_current_task->pid);
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +
",my_current_task->pid);
}
}
}
另外还要修改myinterrupt.c文件,增加进程调度函数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"
extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
extern volatile int my_need_sched;
volatile int time_count = 0;
void my_timer_handler(void)
{
#if 1
if(time_count%10000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<<
");
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<<<
");
/* schedule */
next = my_current_task->next;
prev = my_current_task;
if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
{
asm volatile(
"pushl %%ebp
"
"movl %%esp,%0
"
"movl %2,%%esp
"
"movl $1f,%1
"
"pushl %3
"
"ret
"
"1: "
"popl %%ebp
"
: "=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<<<
",prev->pid,next->pid);
}
else
{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<<
",prev->pid,next->pid);
/* switch to new process */
asm volatile(
"pushl %%ebp
"
"movl %%esp,%0
"
"movl %2,%%esp
"
"movl %2,%%ebp
"
"movl $1f,%1
"
"pushl %3
"
"ret
"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
可以看出,整个内核的运行过程是:
首先启动内核,启动0号进程进行初始化,其中0号进程的启动部分使用了内嵌汇编代码编写:
asm volatile(
"movl %1,%%esp
" /*将进程原堆栈栈顶的地址(这里是初始化的值)存入ESP寄存器 */
"pushl %1
" /* 将当前EBP寄存器值入栈 */
"pushl %0
" /* 将当前进程的EIP(这里是初始化的值)入栈*/
"ret
" /* ret命令正好可以让入栈的进程EIP保存到EIP寄存器中*/
"popl %%ebp
" /*这里永远不会被执行,知识与前面push指令结对出现,是一种编码习惯*/
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
);
当进程0的时间片用完(my_process中的i累加到100000000),将利用进程调度函数,运行进程1,其调度过程执行如下:
if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
{
asm volatile(
"pushl %%ebp
"
"movl %%esp,%0
"
"movl %2,%%esp
"
"movl $1f,%1
"
"pushl %3
"
"ret
"
"1: "
"popl %%ebp
"
: "=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<<<
",prev->pid,next->pid);
}
else
{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<<
",prev->pid,next->pid);
/* switch to new process */
asm volatile(
"pushl %%ebp
"
"movl %%esp,%0
"
"movl %2,%%esp
"
"movl %2,%%ebp
"
"movl $1f,%1
"
"pushl %3
"
"ret
"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
由于进行1并未被执行过,其调度过程中执行else部分的进程调度代码。
同理,当进程1的时间片用完,将再次利用调度函数将运行进程0,此时进程0是从新被执行,将使用if部分的进程调度代码。
由此,每过一个时间片(通过my_process中i的不断模100000000累加),内核将不断在进程0与进程1之间切换。
问题与解决
本周在搭建内核环境时,在make的过程中,gcc报错。
原因是因为找不到compiler-gcc7.h头文件,进入到文件目录下,发现确实没有该文件,只有compiler-gcc.h、compiler-gcc3.h、compiler-gcc4.h三个文件,猜测可能是因为下载的Linux源文件版本不同的原因。在网上下载compiler-gcc7.h文件后重新编译,内核可正常运行。