Linux内核分析-week 2


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

这周的课程信息量比较大,现做一个整理,也算是为了完成作业。
0. 笔记
内嵌汇编语法如下:

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

为了方便查看,特将文件放在这里,并在文件中进行注释,加深自己的理解
(1)mypcb.h 头文件

/*
 *  linux/mykernel/mypcb.h
 *
 *  Kernel internal PCB types
 *
 *  Copyright (C) 2013  Mengning
 *
 */

#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*8

/* 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 */
    char 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);

(2)mymain.c

/*
 *  linux/mykernel/mymain.c
 *
 *  Kernel internal my_start_kernel
 *
 *  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"

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].state = -1;
        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\n\t"     /* set task[pid].thread.sp to esp */
        "pushl %1\n\t"          /* push ebp */
        "pushl %0\n\t"          /* push task[pid].thread.ip */
        "ret\n\t"               /* pop task[pid].thread.ip to eip */
        "popl %%ebp\n\t"
        : 
        : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)   /* input c or d mean %ecx/%edx*/
    );
}   
void my_process(void)
{
    int i = 0;
    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);
        }     
    }
}

(3)myinterrupt.c

/*
 *  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<<<\n");
        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<<<\n");
    /* 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\n\t"       /* save ebp */
            "movl %%esp,%0\n\t"     /* save esp */
            "movl %2,%%esp\n\t"     /* restore  esp */
            "movl $1f,%1\n\t"       /* save eip */  
            "pushl %3\n\t" 
            "ret\n\t"               /* restore  eip */
            "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)
        ); 
        my_current_task = next; 
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);      
    }
    else
    {
        next->state = 0;
        my_current_task = next;
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
        /* switch to new process */
        asm volatile(   
            "pushl %%ebp\n\t"       /* save ebp */
            "movl %%esp,%0\n\t"     /* save esp */
            "movl %2,%%esp\n\t"     /* restore  esp */
            "movl %2,%%ebp\n\t"     /* restore  ebp */
            "movl $1f,%1\n\t"       /* save eip */  
            "pushl %3\n\t" 
            "ret\n\t"               /* restore  eip */
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        );          
    }   
    return; 
}

2. 代码分析
对于以上文件中的数据类型定义等代码我就不赘述了,唯一重要的进程初始化、切换的几段汇编代码比较难理解,因此将详细分析记录下来。
第一个进程的初始化环境设置:

asm volatile(
    "movl %1,%%esp\n\t" /*将进程的堆栈值存入系统堆栈 */
    "pushl %1\n\t"      /* 将当前ebp寄存器值入栈 */
    "pushl %0\n\t"      /* 将当前进程的eip入栈*/
    "ret\n\t"           /*ret命令正好可以让入栈的进程eip保存到eip寄存器中 */
    "popl %%ebp\n\t"
    : 
    : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
    );

进程0 被初始化时堆栈变化

movl %1,%%esp
 __________________________
|__________________________|<---esp
|__________________________|
|__________________________|
|__________________________|
|__________________________|
|__________________________|

pushl %1
 __________________________
|__________________________|
|___task[pid].thread.sp____|<---esp
|__________________________|
|__________________________|
|__________________________|
|__________________________|

pushl %0
 __________________________
|__________________________|
|_____task[0].thread.sp____|
|_____task[0].thread.ip____|<---esp
|__________________________|
|__________________________|
|__________________________|

ret
 __________________________      
|__________________________|
|_____task[0].thread.sp____|<-----esp
|__________________________|----->eip = (unsigned long)my_process
|__________________________|
|__________________________|
|__________________________|


pop %%ebp
 __________________________      
|__________________________|<-----esp、ebp
|__________________________|
|__________________________|
|__________________________|
|__________________________|
|__________________________|

进程调度代码:

 if(next->state == 0)/*next->state == 0对应进程next对应进程曾经执行过*/
        {
            /*进行进程调度关键代码 */
            asm volatile(   
                "pushl %%ebp\n\t"   /* 保存当前ebp到堆栈中 */
                "movl %%esp,%0\n\t" /* 保存当前进程堆栈指针到当前进程tcb中*/
                "movl %2,%%esp\n\t" /*将下一进程的esp值存到esp寄存器 */
                "movl $1f,%1\n\t"   /*保存当前进程的eip值,下次恢复进                                                 程后将在1:开始执行 */   
                "pushl %3\n\t"      /*将新的eip存到栈中*/
                "ret\n\t"           /*保存eip到eip寄存器*/
                "1:\t"              /* 下一进程执行位置*/
                "popl %%ebp\n\t"    /* 恢复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<<<\n",prev->pid,next->pid);      
        }
        else   /*表明next该进程第一次被执行*/
        {
            next->state = 0;
            my_current_task = next;
            printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
            /* switch to new process */
            asm volatile(   
                "pushl %%ebp\n\t"       /* 保存当前进程ebp */
                "movl %%esp,%0\n\t"     /* 保存当前进程esp */
                "movl %2,%%esp\n\t"     /* 重新载入esp*/
                "movl %2,%%ebp\n\t"     /* 重新载入ebp */
                "movl $1f,%1\n\t"       /* 保存当前eip寄存器值 */   
                "pushl %3\n\t"          /* 把即将执行的进程的eip入栈 */
                "ret\n\t"               /* 重新载入eip*/
                : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
                : "m" (next->thread.sp),"m" (next->thread.ip)
            );          
        }

为了简便,我们假设系统只有两个进程,分别是进程0和进程1
我们从进程1被调度开始分析堆栈变化,此时执行else中的代码

   pushl %%ebp  process 0:"I should save my ebp firstly"
     __________________________
    |__________________________|
    |____ebp_of_process_0______|<---esp
    |__________________________|
    |__________________________|
    |__________________________|
    |__________________________|

    movl %%esp,%0   process 0:"...and asp as well..."
                    tcp[0].thread.sp >---
                                         |
                                         |
     __________________________          |
    |__________________________|         |
    |_____ebp_of_process_0_____|<--------
    |__________________________|
    |__________________________|
    |__________________________|
    |__________________________|


    movl %2,%%esp           tcb:" process 1,now you can have esp for your own!"
     __________________________             _________________________
    |__________________________|            |_________________________|<---esp
    |_____ebp_of_process_0_____|            |_________________________|      
    |__________________________|            |_________________________|
    |__________________________|            |_________________________|
    |__________________________|            |_________________________|
    |__________________________|            |_________________________|
    |__________________________|            |_________________________|


    movl %2,%%ebp                tcb:"...and Don't forget your ebp..."
     __________________________           _________________________
    |__________________________|         |_________________________|<---esp 、ebp
    |_____ebp_of_process_0_____|         |_________________________|      
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|


    movl $1f,%1  process 0 :"I should start at 1: for the next time"
     __________________________           _________________________
    |__________________________|         |_________________________|<---esp 、ebp
    |_____ebp_of_process_0_____|         |_________________________|      
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|

    pushl %3           tcb:"process 1,you should run at 'my_process' "
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |_____ebp_of_process_0_____|         |____task[1].thread.ip____|<--- esp 
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|

    ret            process 1:"get it!now I know where to start"
     __________________________           _________________________
    |__________________________|         |_________________________|<--- esp、ebp
    |_____ebp_of_process_0_____|         |____task[1].thread.ip____| --->eip
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|

进程0 重新被调度了!执行if中的代码

    pushl %%ebp    but,process 1 :"wait a minite,I should save some import thing,like ebp..."
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |_____ebp_of_process_0_____|         |______ebp_of_process_1___|<--- esp 
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|

    movl %%esp,%0                  process 1 :"...And the esp as well..."
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |_____ebp_of_process_0_____|         |______ebp_of_process_1___|<--- esp 
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|

    movl %2,%%esp 
        process 0:"LOL,now esp belong to me again!hahaha!"
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |______ebp_of_process_0____|<---esp  |______ebp_of_process_1___| 
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|


    movl $1f,%1         tcb:"Don't be sad,process 1.next time please start at "1:",remember?" 
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |_____ebp_of_process_0_____|<---esp  |______ebp_of_process_1___| 
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|


    pushl %3     process 0:"I don't know where should I start.Dear tcb,Could you tell me? " 
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |_____ebp_of_process_0_____|         |______ebp_of_process_1___| 
    |____________1f____________| <---esp |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|


    ret            tcb :"OK,you start at 1:"
     __________________________           _________________________
    |__________________________|         |_________________________|<--- ebp
    |_____ebp_of_process_0_____| <---esp |______ebp_of_process_1___| 
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|
    |__________________________|         |_________________________|

    popl %%ebp
        tcb:"but,Don't forget you stack base.It's at the top of your stack.Please pop it to ebp and Good lucky,boy"
     __________________________            _________________________
    |__________________________|<-esp、ebp|_________________________|
    |_____ebp_of_process_0_____|          |______ebp_of_process_1___| 
    |__________________________|          |_________________________|
    |__________________________|          |_________________________|
    |__________________________|          |_________________________|
    |__________________________|          |_________________________|
    |__________________________|          |_________________________|

    process 0:"I know,you LOL,I'm running again~~~~"

so,this is how process 0 and process 1 share the computer,they get certain time to own the system and share to another when time out...

对论坛上 else中"movl $1f,%1\n\t" 的问题,看到老师的回复是“$1f放到eip里使用才算生效吧,不能见$1f就找标号1”。我的理解是这样的:if中的自然不必说,else中代码只有进程第一次被执行时候才会运行,此时将$1f存入prev->thread.ip,但当进程被重新调度执行的时候,此时进入了if代码块中,因此将执行if代码块中的1:标号处的代码,所以else中没有"1:"也就不奇怪了。
(3)截图
图片描述
图片描述
(4)总结
本次试验最重要的是进程的切换,进程执行过程中,当时间片用完需要进行进程切换时,需要先将当前的进程执行环境进行保存,下次进程被调度时,需要恢复进程的执行环境。这样实现多道进程的执行。
通过这次实验,对中断和进程切换的具体过程有了了解,是一次很好的实践。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值