mykernel

My kernel 代码解析(1)

学号后三位:118
原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/
项目地址:https://github.com/mengning/mykernel

代码分析

mypcb.h

进程控制块PCB定义在mypcb.h中:

#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);

为了实现进程模型,OS需要创建一个process table,每个进程对应process table中的一个实例(process control block,PCB)。PCB包含了关于进程状态的信息,包括:program counter, stack pointer, memory location, the status of its open files…最重要的概念是,PCB中的信息要能够保证,当OS读取该PCB以后能够在CPU中还原对应进程的运行状态,以继续执行该进程。
在mykernel.h中,使用tPCB结构体来保存进程信息。

  1. pid唯一标志该进程
  2. state标志该进程当前所处的状态,-1表示不可运行,0表示可运行,>0表示挂起状态
  3. stack是一个数组,其中的元素为无符号长整型。与寄存器的位数相同。stack表示该进程的运行时栈
  4. struct Thread中包含了该进程ip、sp。ip指向该下一条需要执行指令的地址,sp将会指向运行时栈的栈顶。
  5. task_entry表示该进程的入口地址
  6. OS将PCB组织成链表形式

mymain.c

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].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);
        }     
    }
}

my_start_kernel函数用来启动内核。

tPCB task[MAX_TASK_NUM]

创建一个task数组,数组元素为tPCB,数组长度为MAX_TASK_NUM(==4)

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];

为process 0 创建属于它的PCB,task_entry保存了process 0的入口地址(函数名my_process就是一个指向函数代码段第一条语句的指针)。
因为该进程还未运行,所以其thread.ip也为my_process。
thread.sp指向进程运行时栈的栈顶。在创建进程PCB时为进程分配了运行时栈,该栈还未使用,所以此时stack的大小为KERNEL_STACK_SIZE。
在创建第一个进程后,系统中只有该进程,所以next指针指向自己。这一步也是为后面创建进程时写代码方便。

至此。OS完成了第一个进程的初始化工作。

接着,以process 0 为模板。继续对其他3个进程完成初始化。

for(i=1;i<MAX_TASK_NUM;i++)
    {
        memcpy(&task[i],&task[0],sizeof(tPCB));
        task[i].pid = i;
        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];
    }

在完成进程创建以后,把process 0 转入运行状态:

/* 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*/
	);

myinterrupt.c

myinterrupt.c中定义了中断处理函数my_time_handler

void my_timer_handler(void)
{
#if 1
	if (time_count % 1000 == 0 && my_need_sched != 1)
	{
		printk(KERN_NOTICE ">>>my_timer_handler here<<<\n\n\n\n\n\n");
		my_need_sched = 1;
	}
	time_count++;
#endif
	return;
}

每有一次时钟中断就会调用一次该函数,1000次时钟中断后将把my_need_sched赋值为1,表示允许进行进程调度。my_process中就会调用my_schedule函数。

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;
		unsigned ct = 1000000;
		while (ct)
		{
			ct--;
		}
		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;
}

my_schedule函数完成进程的切换工作。
最关键的是其中的一段汇编代码:

/* 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_time_hendler后面加了一些换行符。

实验理解

加深了对操作系统如何进程进程切换的理解。

事件

事件是指改变CPU当前控制流的事情。包括中断、异常。
一般中断指外部事件,是当前运行程序之外导致的,比如I/O中断、时钟中断、硬件故障。
异常通常指发生在当前程序中的事件。比如系统调用、算术溢出、页故障(page fault)等。

外部中断

CPU总是在每条指令执行阶段的最后时刻,查询所有的设备是否有中断请求。来自CPU的中断查询信号将所有中断源的中断请求触发器INTR置1,然后中断源将中断请求信号发送给CPU。每一个中断源都对应CPU中断标志寄存器中的一位,CPU对中断标志寄存器中的值进行编码,得到中断向量向量地址。通过中断向量可以跳转执行中断服务程序。

当发现有中断后,OS将处理器状态设置为内核态,准备执行内核代码,因此需要将被中断的用户进程运行状态压栈(内核栈)。在中断处理程序结束之后再将当前处理器状态恢复。

查找中断服务程序入口地址有两种方式:

  1. 利用硬件产生中断向量地址(前文所述),中断向量是中断向量表中的元素,中断向量包括了该中断处理程序的入口地址和需要的处理机状态字。通过中断向量地址查找中断服务程序的入口地址。中断向量表存储在存储器内,在系统启动时进行初始化。
  2. 利用软件,由程序员自己设定对应中断服务程序的地址。

在获得中断服务程序入口地址后,OS根据中断向量的内容,将CPU设置为对应中断处理程序的运行环境,然后跳转执行中断服务程序直到结束。在CPU执行中断处理程序时,防止操作被新的中断打断,需要在中断周期内关闭中断。

中断服务程序结束以后,将原先压栈的被中断程序信息pop出来,再将CPU恢复到原先被中断程序的状态。或者,如果中断处理程序是调度程序(中断源为时钟中断),那么就由调度程序将把被调入CPU执行的进程的信息,放入对应的CPU寄存器中。

系统调用

系统调用是内部中断的一种。在Linux的中断向量表中,中断向量号为128的中断向量表示当前中断为用于系统调用的可编程异常。

在x86处理器中,中断向量表被中断描述符表替代。中断描述符表(IDT)中的中断描述符包含有目标中断服务程序的段偏移地址,以及段选择符,用来指定中断服务程序所在的程序段。段选择符中包含一个全局描述符表(GDT)中的偏移量,全局描述符中包含了中断服务程序所在的段基地址。

用户程序通过高级语言使用系统调用。编译器将用户程序中调用系统调用的部分编译成一个特定的汇编语句。比如x86中的int 0x80 指定就会引发一次系统调用。用户程序与系统调用之间通常使用通用寄存器来传递参数(参数包括有系统调用号等)。这种特用的汇编语句通常叫做陷入指令或者访管指令。

当系统执行到这些特定的陷入语句时:

  1. 需要进入内核,所以切换栈,将用户栈sp、bp、以及cs、ip等寄存器值压入内核栈,然后把内核栈基地址、栈顶地址放入bp、sp。完成栈切换。
  2. 用128在IDT中找到中断描述符,取出段选择符、段偏移量,找到GDT中的段基地址,然后段基地址+段偏移量找到system_call()的入口(system_call是所有系统调用的入口函数,它根据系统调用号的不同来选择具体的系统调用程序)
  3. 结束系统调用,返回用户进程,或者将用户进程挂起由调度程序选择别的就绪进程继续执行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值