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

时间:2022-11-07 15:45:36

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

 

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

1、 内容说明

内嵌汇编语法如下:

Linux内核分析 操作系统是如何工作的
 具体代码如下:

 (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;   /* -1unrunnable, 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"

 

tPCBtask[MAX_TASK_NUM];

tPCB *my_current_task = NULL;

volatile intmy_need_sched = 0;

 

voidmy_process(void);

 

void __initmy_start_kernel(void)

{

    int pid = 0;

    int i;

    /* Initialize process 0*/

    task[pid].pid = pid;

    task[pid].state = 0;/* -1 unrunnable, 0runnable, >0 stopped */

    task[pid].task_entry = task[pid].thread.ip= (unsigned long)my_process;

    task[pid].thread.sp = (unsignedlong)&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 = (unsignedlong)&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"   /* settask[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*/

         );

}  

voidmy_process(void)

{

    int i = 0;

    while(1)

    {

        i++;

        if(i%10000000 == 0)

        {

            printk(KERN_NOTICE "this isprocess %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 currentrunning process,

 * so it use kernel stack ofcurrent 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)/* -1unrunnable, 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)

    );

进程调度代码:

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)

            );         

        }

3、 试验截图
Linux内核分析 操作系统是如何工作的

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

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

4、总结
本次试实验要求实现多道进程的实行,重点在于进程的切换,进程执行过程中,当时间片用完需要进行进程切换时,需要先将当前的进程执行环境进行保存,下次进程被调度时,需要恢复进程的执行环境。