zoukankan      html  css  js  c++  java
  • 2020-2021-1 202029325 《Linux内核原理与分析》第三周作业

    一、学习笔记

    • 1.计算机的三大法宝:存储程序计算、函数调用机制、中断。

    • 2.堆栈的具体作用有:记录函数调用框架、传递函数参数、保存返回值的地址、提供函数内部局部变量的存储空间等。

    • 3.堆栈相关的寄存器:
      ESP:堆栈指针(stack pointer)
      EBP:基址指针(base pointer),在C语言中用作记录当前函数调用基址。

    • 4.对于X86体系结构来讲,堆栈空间是从高地址向低地址增长的,如图所示:

    • 5.堆栈操作:
      push:栈顶地址减少4个字节(32位),并将操作数放入栈顶存储单元。
      pop:栈顶地址增加4个字节(32位),并将栈顶存储单元的内容放入操作数。

    • 6.其它一些指令:
      顺序执行:总是指向地址连续的下一条指令。
      跳转/分支:执行这样的指令时,CS:EIP的值会根据程序需要被修改。
      call:将当前CS:EIP 的值压入栈顶,CS:EIP指向被调用函数的入口地址。
      ret:从栈顶弹出原来保存在这样的CS:EIP的值,放入CS:EIP中。

    二、实验记录

    • 1.进程初始化代码,这里需要重点理解的是%1是指后面的“"d"(task[pid].thread.sp)”,%0是指后面的“"c"(task[pid].thread.ip)”,代码如下:
    asm volatile(
        "movl %1,%%esp
    	"   //将进程原堆栈栈顶的地址(这里是初始化的值)存入ESP寄存器。
        "pushl %1
    	"        //将当前EBP寄存器值入栈。
        "pushl %0
    	"        //将当前进程的EIP(这里是初始化的值)入栈。
        "ret
    	"             //ret命令正好可以让入栈的进程EIP保存到EIP寄存器中。
        "popl %%ebp
    	"      //与前面push指令结对出现。
    }
    
    • 2.按照庖丁解牛书中所讲my_timer_handler所控制输出循环次数,可以将代码中的100000改成1000,简化输出结果,代码如下
    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;  	
    }
    
    • 3.mymain.c是mykernel内核代码的入口,负责初始化内核的各个组成部分。在linux内核源码中,实际的入口是init/main.c中的__init my_start_kernel(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 rsp */
        	"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;
    
    • 4.myinterrupt.c中,主要是切换进程的my_schedule(void)函数
    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 rbp of prev */
            	"movl %%esp,%0
    	" 	/* save rsp of prev */
            	"movl %2,%%esp
    	"     /* restore  rsp 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;	
    }
    

    三、实验结果

    简化后的输出结果如下图:

  • 相关阅读:
    有了bootstrap,为什么还要做amaze ui
    互联网科普知识【野狗】
    诶,很有意思的点子——云适配
    nodeJS一些事儿
    主流浏览器js 引擎内核市场份额attialx总结vOa9
    50 个 Bootstrap 插件
    Bean Validation
    Spring Security的核心拦截器
    关于未来十年企业架构的十个关键词
    写一个函数对字符串数组排序,使所有变位词都相邻
  • 原文地址:https://www.cnblogs.com/journey97/p/13861184.html
Copyright © 2011-2022 走看看