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

1.从github上直接下载的mykernel代码不能使用

回答:

直接下载的mykernel代码是64位,需要修改的地方如:rep->eip;pushq->pushl等。

2.发现下载的代码和课程的代码存在不同,比如课程代码中还有对第一次运行的进程段的处理,而kernel2.0没有,修改的原因是什么

回答:

经过实验,发现二者在运行时并无差别:

kernel2.0

kernel1.0

对此,我的解释是,1.0版本的kernel把对第一次运行的进程的处理另外进行处理,实际上并不需要,在对一般进程进行处理的过程中,所使用的

        "1:\t"                  
        "popl %%ebp\n\t"

指向了下一条指令(包括第一次运行的指令),所以可以将对第一次运行的进程的处理集成到对一般进程的处理中去。

3.附实验代码

myinterupt.c

/*
 *  linux/mykernel/myinterrupt.c
 *
 *  Kernel internal my_timer_handler
 *  Change IA32 to x86-64 arch, 2020/4/26
 *
 *  Copyright (C) 2013, 2020  Mengning
 *
 */
#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<<<\n");
        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<<<\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 rbp of prev */
        	"movl %%esp,%0\n\t" 	/* save rsp of prev */
        	"movl %2,%%esp\n\t"     /* restore  rsp of next */
        	"movl $1f,%1\n\t"       /* save rip of prev */	
        	"pushl %3\n\t" 
        	"ret\n\t" 	            /* restore  rip of next */
        	"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;	
}
posted @ 2020-10-21 22:27  20209324  阅读(136)  评论(1编辑  收藏  举报