zoukankan      html  css  js  c++  java
  • Linux内核分析第二周

    【李行之原创作品 转载请注明出处 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

    《Linux内核分析》 之 操作系统是如何工作的

    第一讲 函数调用堆栈

    1.计算机是如何工作的?(总结)——三个法宝

    • 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构;

    • 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要,但有了高级语言及函数,堆栈成为了计算机的基础功能;函数参数传递机制和局部变量存储

    • 中断,多道程序操作系统的基点,没有中断机制程序只能从头一直运行结束才有可能开始运行其他程序。

    2.参数传递过程

    下面的代码中有参数传递过程:

    #include <stdio.h>
    void p1(char c)
    {
    	printf("%c",c);
    }
    int p2(int x,int y)//重点关注这里的局部变量
    {
    	return x+y;
    }
    int main(void)
    {
    	char c ='a';
    	int x,y;
    	x =1;
    	y =2;
    	p1(c);
    	z = p2(x,y);//重点关注这里的参数传递过程
    	printf("%d = %d+%d",z,x,y);
    }
    

    进行反汇编之后,代码如下:

    1. 图1中红色圈出的部分是变址寻址,目的是将x+y的值赋给eax;
    2. 图2是main函数中z = p2(x,y);一句的汇编代码。可以看到的是,汇编代码中用变址寻址把y的值和x的值存放到堆栈中,然后进行局部变量调用。

    第二讲 借助Linux内核部分源代码模拟存储程序计算机工作模型及时钟中断

    1.利用mykernel实验模拟计算机硬件平台(实验 No.1)

    进入实验楼,打开shell之后按照说明输入;

    • 运行

    • 查看源代码

      • 查看mymain.c

      • 查看myinterrupt.c

    第三讲 在mykernel基础上构造一个简单地操作系统内核

    1.C代码中嵌入汇编代码的写法

    • 格式:

    • 练习:

      • 截图

      • 难点:

        • 最后的:eax是破坏描述部分,也就是说这段代码可能会破坏eax;
        • 最后输出的是两个内存变量temp和output
      • 结果:

        • 0,1

    2.一个简单的操作系统内核源代码(摘录)

    1.mypcb.h//头文件

    10 #define MAX_TASK_NUM 4 
    11 #define KERNEL_STACK_SIZE   1024*8 
    12 
     
    13 /* CPU-specific state of this task */ 
    14 struct Thread { 
    15 unsigned long		ip; 
    16 unsigned long		sp; 
    17 }; 
    18 
     
    19 typedef struct PCB{ 
    20 int pid; 
    21 volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */ 
    22 char stack[KERNEL_STACK_SIZE]; 
    23 /* CPU-specific state of this task */ 
    24 struct Thread thread; 
    25 unsigned long	task_entry; 
    26 struct PCB *next; 
    27 }tPCB; 
    28 
     
    29 void my_schedule(void); 
    

    2.mymain.c

    16 #include "mypcb.h" 
    17 
     
    18 tPCB task[MAX_TASK_NUM]; 
    19 tPCB * my_current_task = NULL; 
    20 volatile int my_need_sched = 0; 
    21 
     
    22 void my_process(void); 
    23 
    24 
     
    25 void __init my_start_kernel(void) 
    26 { 
    27 int pid = 0; 
    28 int i; 
    29 /* Initialize process 0*/ 
    30 task[pid].pid = pid; 
    31 task[pid].state = 0;
    32 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 
    33 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; 
    34 task[pid].next = &task[pid]; 
    
    35 /*fork more process */ 
    36 for(i=1;i<MAX_TASK_NUM;i++) 
    37 { 
    38 memcpy(&task[i],&task[0],sizeof(tPCB)); 
    39 task[i].pid = i; 
    40 task[i].state = -1; 
    41 task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1]; 
    42 task[i].next = task[i-1].next; 
    43 task[i-1].next = &task[i]; 
    44 } 
    
    45 /* start process 0 by task[0] */ 
    46 pid = 0;
    47 my_current_task = &task[pid]; 
    48 	asm volatile( 
    49 	"movl %1,%%esp
    	" 	/* set task[pid].thread.sp to esp */ 
    50 	"pushl %1
    	" 	/* push ebp */ 
    51 	"pushl %0
    	" 	/* push task[pid].thread.ip */ 
    52 	"ret
    	" 	/* pop task[pid].thread.ip to eip */ 
    53 	"popl %%ebp
    	" //弹出来ebp,内核初始化工作完成
    54 	:  
    55 	: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)	/* input c or d mean %ecx/%edx*/ 
    56 	); 
    57 }
    
    58 void my_process(void)
    59 { 
    60 int i = 0; 
    61 while(1) 
    62 { 
    63 i++; 
    64 if(i%10000000 == 0) 
    65 { 
    66 printk(KERN_NOTICE "this is process %d -
    ",my_current_task->pid); 
    67 if(my_need_sched == 1) //执行10 000 000次才判断一次是否需要调度
    68 { 
    69 my_need_sched = 0; 
    70 	my_schedule(); 
    71 	} 
    72 	printk(KERN_NOTICE "this is process %d +
    ",my_current_task->pid); 
    73 }  
    74 } 
    75 } 
    

    3.myinterrupt.c 调度机制

    15 #include "mypcb.h" 
    16 
     
    17 extern tPCB task[MAX_TASK_NUM]; 
    18 extern tPCB * my_current_task; 
    19 extern volatile int my_need_sched; 
    20 volatile int time_count = 0; 
    21 
     
    22 /* 
    23  * Called by timer interrupt. 
    24  * it runs in the name of current running process, 
    25  * so it use kernel stack of current running process 
    26  */ 
    27 void my_timer_handler(void) 
    28 { 
    29 #if 1 
    30 if(time_count%1000 == 0 && my_need_sched != 1) 
    31 { 
    32 printk(KERN_NOTICE ">>>my_timer_handler here<<<
    "); 
    33 my_need_sched = 1; 
    34 }  
    35 time_count ++ ;   
    36 #endif 
    37 return;  	 
    38 } 
    39 
     
    40 void my_schedule(void) 
    41 { 
    42 tPCB * next; 
    43 tPCB * prev; 
    44 
     
    45 if(my_current_task == NULL  
    46 || my_current_task->next == NULL) 
    47 { 
    48 	return; 
    49 } 
    50 printk(KERN_NOTICE ">>>my_schedule<<<
    "); 
    
    51 /* schedule */ 
    52 next = my_current_task->next; 
    53 prev = my_current_task; 
    
    54 if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ //在两个正在执行的进程之间做上下文切换
    55 { 
    56 	/* switch to next process */ 
    57 	asm volatile(	 
    58 	"pushl %%ebp
    	" 	/* save ebp */ 
    59 	"movl %%esp,%0
    	" 	/* save esp */ 
    60 	"movl %2,%%esp
    	" /* restore  esp */ 
    61 	"movl $1f,%1
    	"   /* save eip */ //$1f就是指标号1:的代码在内存中存储的地址	 
    62 	"pushl %3
    	"  
    63 	"ret
    	" 	/* restore  eip */ 
    64 	"1:	"  /* next process start here */ 
    65 	"popl %%ebp
    	" 
    66 	: "=m" (prev->thread.sp),"=m" (prev->thread.ip) 
    67 	: "m" (next->thread.sp),"m" (next->thread.ip) 
    68 	);  
    69 	my_current_task = next;  
    70 	printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid	 
    71 } 
    
    72 else 
    73 { 
    74 next->state = 0; 
    75 my_current_task = next; 
    76 printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid); 
    77 	/* switch to new process */ 
    78 	asm volatile(	 
    79 	"pushl %%ebp
    	" 	/* save ebp */ 
    80 	"movl %%esp,%0
    	" 	/* save esp */ 
    81 	"movl %2,%%esp
    	" /* restore  esp */ 
    82 	"movl %2,%%ebp
    	" /* restore  ebp */ 
    83 	"movl $1f,%1
    	"   /* save eip */	 
    84 	"pushl %3
    	"  
    85 	"ret
    	" 	/* restore  eip */ 
    86 	: "=m" (prev->thread.sp),"=m" (prev->thread.ip) 
    87 	: "m" (next->thread.sp),"m" (next->thread.ip) 
    88 	);   
    89 }
    90 return;	 
    91 }
    

    结束

    【 如何理解“操作系统是如何工作的?” 】

    操作系统具有“两把剑”即中断上下文和进程切换;这样的特性使得操作系统可以具有一定的标准来选择执行和中断程序,调配资源。

  • 相关阅读:
    @echo off
    小知识点
    字符串匹配方法
    一般保护错误
    Linux常用压缩与解压缩命令
    opencv__linux__配置
    opencv__配置
    Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql
    Web开发从零单排之一:在新浪云平台SAE上开发一个html5电子喜帖
    WPF中使用ValueConverter来实现“范围条件触发器”
  • 原文地址:https://www.cnblogs.com/shadow135211/p/5244213.html
Copyright © 2011-2022 走看看