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

《Linux内核原理与分析》第三周作业

一、实验:完成一个简单的时间片轮转多道程序内核代码

输入以下指令
cd LinuxKernel/linux-3.9.4
查看mymain.c和myinterrupt.c,根据GitHub和书上知识,将代码改一下

原来代码

mymain.c

my interrupt.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].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,%%rsp\n\t" 	/* set task[pid].thread.sp to rsp */
    	"pushl %1\n\t" 	        /* push rbp */
    	"pushl %0\n\t" 	        /* push task[pid].thread.ip */
    	"ret\n\t" 	            /* 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 -\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
#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 %%rbp\n\t" 	    /* save rbp of prev */
        	"movl %%rsp,%0\n\t" 	/* save rsp of prev */
        	"movl %2,%%rsp\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 %%rbp\n\t"
        	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
        	: "m" (next->thread.sp),"m" (next->thread.ip)
    	); 
    }  
    return;	
}

控制时间片轮转的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);



make一下,编译完可以看出时间片轮转的特征

二、学习知识

  • 计算机三个法宝(3个关键性的方法机制):存储程序计算机、函数调用堆栈、中断机制
  • 堆栈的作用是:记录函数调用框架、传递函数参数、保存返回值地址、提供函数内部局部变量的存储空间。
  • 堆栈相关的寄存器:
    • ESP:堆栈指针,指向堆栈栈顶
    • EBP:基址指针,指向堆栈栈底
  • 堆栈操作
    • push: 栈顶地址减少4个字节,将操作数放入栈顶存储单元
    • pop :将操作数从栈顶存储单元移出,栈顶地址增加4个字节
  • 其他关键寄存器
    • CS:EIP 总是指向下一条指令地址。CS是代码段寄存器, EIP是指向下一条指令的地址
    • 跳转/分支:执行这样的命令时,CS:EIP的值会根据程序需要被修改
    • call:将当前CS:EIP的值压入栈顶,CS:EIP指向被调用函数的入口地址
    • ret:从栈顶弹出原来保存在这里CS:EIP的值,放入CS:EIP中
  • c语言内嵌汇编语言
    asm volatile(
        "movl $0,%%eax\n\t"
        /*  将eax寄存器清零  */
        "addl %1,%%eax\n\t"
        /* %1 是指下面的输入输出部分,从0开始编号,所以%1指的是val1*/
        /* 这条语句的就是就是将ecx中存储的val1的值与eax寄存器中的值相加,结果为1*/
        "addl %2,%%eax\n\t"
        /* %2 是指val2存在edx寄存器中*/
        /*这条语句就是将val2与寄存器eax中的值相加,放回eax中*/
        "movl %%eax,%0\n\t"
        /* val1+val2的值写入到%0中去,也就是val3*/

        /*输出部分 */
        :"=m"(val3)
           /* =m”代表内存变量,m就是memory,也就是直接把变量写到内存val3中*/

         /*输入部分 */
        :"c"(vall),"d"(val2)
            /* c代表%二次项,d![](https://img2018.cnblogs.com/blog/1800798/201909/1800798-20190928193228032-1611440033.png)
代表%edx,就是使用这一存储器存储相应变量的值*/
    );

三、总结

本次学习主要学习了计算机的三大法宝清楚它们各自的作用是什么。
学习了C语言内嵌入汇编代码。
对时间片轮转的实际代码得到了学习。

posted @ 2020-10-21 01:32  20209316滕源  阅读(216)  评论(2编辑  收藏  举报