2020-2021-1 20209327 《Linux内核原理与分析》第三周作业

时间片轮转多道程序内核代码分析


代码实现

使用实验楼的环境进行实验,结果如图:

LinuxKernel/linux-3.9.4/mykernel目录下修改源代码:

在mypcb.h中定义PCB:

#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*2
/* 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中实现内核代码的入口,负责初始化内核的各个组成部分:

#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].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
	" 	/* set task[pid].thread.sp to esp */
    	"pushl %1
	" 	        /* push rbp */
    	"pushl %0
	" 	        /* push task[pid].thread.ip */
    	"ret
	" 	            /* pop task[pid].thread.ip to rip */
    	: 
    	: "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 -
",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中实现进程切换:

#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(time_count%1000 == 0 && my_need_sched != 1)
    {
        printk(KERN_NOTICE ">>>my_timer_handler here<<<
");
        my_need_sched = 1;
    } 
    time_count ++ ;  
    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 */
    {        
    	my_current_task = next; 
    	printk(KERN_NOTICE ">>>switch %d to %d<<<
",prev->pid,next->pid);  
    	/* switch to next process */
    	asm volatile(	
        	"pushl %%ebp
	" 	    /* save ebp of prev */
        	"movl %%esp,%0
	" 	/* save esp of prev */
        	"movl %2,%%esp
	"     /* restore  esp of next */
        	"movl $1f,%1
	"       /* save rip of prev */	
        	"pushl %3
	" 
        	"ret
	" 	            /* restore  rip of next */
        	"1:	"                  /* next process start here */
        	"popl %%ebp
	"
        	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        	: "m" (next->thread.sp),"m" (next->thread.ip)
    	); 
    }  
    return;	
}

执行结果如图:

代码分析

进程初始化

初始化一个进程的内嵌汇编代码如下:

asm volatile(
    	"movl %1,%%esp
	" 	
    	"pushl %1
	" 	        
    	"pushl %0
	" 	        
    	"ret
	" 	            
        "popl %%ebp
	"
    	: 
    	: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
	);
  • movl %1,%%esp :将esp指向task[0].thread.sp,此时esp和ebp都指向进程0的堆栈栈底
  • pushl %1 :将ebp的值入栈,即esp移动一个存储单元,将task[0].thread.sp压入栈中
  • pushl %0 :将0号进程的入口task[0].thread.ip压入栈中
  • ret :将当前栈顶的值弹出至eip中,即将task[0].thread.ip存入eip,表示接下来即将启动0进程
  • popl %%ebp :作为编码习惯出现,不会被执行

进程切换

假设系统有进程0和进程1,若由当前的进程0切换至进程1,则进行如下操作:

asm volatile(   
            "pushl %%ebp
	"          
            "movl %%esp,%0
	"     
            "movl %2,%%esp
	" 
            "movl %2,%%ebp
	"   
            "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)
        ); 
  • pushl %%ebp :在栈中保存进程0的ebp
  • movl %%esp,%0 :在prev->thread.sp中保存进程0的esp
  • movl %2,%%esp :将esp指向next->thread.sp,将工作堆栈切换到进程1的堆栈空间
  • movl %2,%%ebp :将ebp指向进程1的栈底
  • movl $1f,%1 :将$1f保存至进程0的thread.ip
  • pushl %3 :在进程1的堆栈空间中,将task[1].thread.ip压入栈中
  • ret :将栈顶的task[1].thread.ip弹出至eip中,即将执行进程1
  • 1: :与$1f配合使用,表示再次切换进程0时从标号1处开始执行
  • popl %%ebp :作为编码习惯出现,不会被执行

问题与总结

问题1:最初的想法是在自己的Ubuntu16.04虚拟机中进行实验,在按照教材执行wget https://raw.github.com/mengning/mykernel/master/mykernel_for_linux3.9.4sc.patch时,出现如下错误:

解决方法如下:

首先查询raw.githubusercontent.com的IP:

然后将151.101.76.133 raw.githubusercontent.com添加到/etc/hosts文件中:

再次执行命令,发现问题已解决:

问题2:在自己的虚拟机进行make时,出现如下错误:

于是我根据网络上的教程不小心将gcc相关配置文件删除了,最终也没能解决,于是决定还原虚拟机备份并在实验楼做实验。

最后,总结一下操作系统是如何工作的:

Linux操作系统由内核来实现具体工作。创建或切换一个进程时,首先将原本正在运行的进程的上下文保存在堆栈中,然后将新进程的上下文信息加载到相应的寄存器中,并运行当前新进程;运行完毕后根据系统的调度继续执行相应的操作。

参考链接

原文地址:https://www.cnblogs.com/TracerElena/p/13869732.html