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

    linux内核分析第二周学习笔记

    标签(空格分隔): 20135328陈都


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


    1.函数调用堆栈

    1.1小结

    三把宝剑:

    • 存储程序计算机
    • 函数调用堆栈
    • 中断机制

    1.2堆栈

    堆栈是C语言程序运行时必须的一个记录调用路径和参数的空间

    • 函数条用框架
    • 传递参数
    • 保存返回地址
    • 提供局部变量空间

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

    0. 内嵌汇编语法

    asm(汇编语句模板: 输出部分: 输入部分: 破坏描述部分)

    • 各部分使用“:”格开

    1. 汇编语句模板

    2. 输出部分

    3. 输入部分

    4. 破坏描述部分

    5. 限制字符

    实验:一个简单的操作系统内核源代码


    GitHub 多进程时间片轮转代码

     *  linux/mykernel/myinterrupt.c
     *
     *  Kernel internal my_timer_handler
     *
     *  Copyright (C) 2013  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 1
        if(time_count%1000 == 0 && my_need_sched != 1)
        {
            printk(KERN_NOTICE ">>>my_timer_handler here<<<
    ");
            my_need_sched = 1;
        } 
        time_count ++ ;  
    #endif
        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 */
        {
        	/* switch to next process */
        	asm volatile(	
            	"pushl %%ebp
    	" 	    /* save ebp */
            	"movl %%esp,%0
    	" 	/* save esp */
            	"movl %2,%%esp
    	"     /* restore  esp */
            	"movl $1f,%1
    	"       /* save eip */	
            	"pushl %3
    	" 
            	"ret
    	" 	            /* restore  eip */
            	"1:	"                  /* next process start here */
            	"popl %%ebp
    	"
            	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            	: "m" (next->thread.sp),"m" (next->thread.ip)
        	); 
        	my_current_task = next; 
        	printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid);   	
        }
        else
        {
            next->state = 0;
            my_current_task = next;
            printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid);
        	/* switch to new process */
        	asm volatile(	
            	"pushl %%ebp
    	" 	    /* save ebp */
            	"movl %%esp,%0
    	" 	/* save esp */
            	"movl %2,%%esp
    	"     /* restore  esp */
            	"movl %2,%%ebp
    	"     /* restore  ebp */
            	"movl $1f,%1
    	"       /* save eip */	
            	"pushl %3
    	" 
            	"ret
    	" 	            /* restore  eip */
            	: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            	: "m" (next->thread.sp),"m" (next->thread.ip)
        	);          
        }   
        return;	
    }
    
  • 相关阅读:
    POJ1182
    poj3225 线段树区间操作 (见鬼)
    斜率优化dp(POJ1180 Uva1451)
    POJ2528 线段树的区间操作
    POI2001 Gold mine(二叉排序树 黑书经典)
    POJ3921
    博弈论之威佐夫博弈(转载)
    poj3468(线段树 边覆盖)
    hdu 1166(树状数组 或 线段树)
    压缩软件的改进--- (续先前霍夫曼编码)
  • 原文地址:https://www.cnblogs.com/cdcode/p/5245630.html
Copyright © 2011-2022 走看看