Linux Mykernel第一次實驗報告
-
- MyKernel簡介
- 實驗初探
- 一個簡單的時間片輪轉多道程式
- 源代碼分析
- 總結
學号085 原創作品轉載請注明出處
基于mykernel的一個簡單的時間片輪轉多道程式核心代碼分析
《Linux核心分析》MOOC課程 http://mooc.study.163.com/course/USTC-1000029000
MyKernel簡介
MyKernel是由中科大孟甯老師建立的一個基于Linux Kernel 3.9.4 source code的用于開發自己的作業系統核心的平台。我們不僅可以通過括号中連結找到mykernel的源代碼( https://github.com/mengning/mykernel ) 并按照上面的指南部署到自己的系統上,還可以使用實驗樓(http://www.shiyanlou.com/courses/195 ) 提供的虛拟機,使用實驗樓已經部署好的平台完成本實驗。本文在這裡使用實驗樓的環境。
實驗初探
我們先使用實驗樓的虛拟機打開shell,輸入以下指令,即可以啟動mykernel,
cd LinuxKernel/linux-3.9.4
rm -rf mykernel
patch -p1 < …/mykernel_for_linux3.9.4sc.patch
make allnoconfig
make #編譯核心請耐心等待
qemu -kernel arch/x86/boot/bzImage
以下是實驗樓環境下的運作截圖(注意切換到圖形化界面):
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPB90MnpmTxsmaNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZwpmLyEzN5ITOxIjM2AzMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
在QEMU視窗,我們可以看到一個簡單的作業系統已經跑起來了,當然這個系統很簡單,隻是不停的輸出一些字元串:>>>>>my_timer_handler here <<<<< 和 my_start_kernel here 。
然後關閉qemu視窗,cd mykernel ,我們可以找到輸出這些字元串的源代碼mymain.c和myinterrupt.c
打開這兩個檔案,我們可以看到,在mymain.c的my_start_kernel函數中
有一個循環,不停的輸出 my_start_kernel here.
在myinterrupt.c中,可以看到一個會被時鐘中斷周期調用的函數my_timer_handler ,在這個函數裡,會輸出類似>>>>>my_timer_handler here <<<<< 的字元串。
這兩個函數的輸出,就是前面我們這QEMU視窗中看到的内容。
通過這個實驗我們可以知道,mykernel系統啟動後,會
1、調用my_start_kernel函數
2、周期性的調用my_timer_handler函數
我們隻要編寫這兩個函數,完成系統程序的初始化和程序的輪轉排程,就可以寫出一個簡單的作業系統了。
一個簡單的時間片輪轉多道程式
通過上面的分析,我們再來看一個稍微複雜一點的實驗,這個實驗也就是擴充了my_start_kernel和my_timer_handler函數,模拟了一個基于時間片輪轉的多道程式。
我們先做實驗,再來分析代碼。
1、從這裡擷取實驗用的源代碼,https://github.com/mengning/mykernel ,主要就這三個檔案:mypcb.h,myinterrupt.c和mymain.c
2、在實驗樓的虛拟機環境中,将這三個檔案拷貝到mykernel平台中,即要覆寫前文所述的mykernel檔案夾下mymain.c和myinterrupt.c,并新增mypcb.h
3、回到 LinuxKernel/linux-3.9.4檔案夾,使用以下指令編譯、運作
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage
運作結果如下:
從QEMU的圖中可以看出,系統從執行process1切換到process2的過程。
源代碼分析
這裡主要分析上面實驗中改寫的三個檔案,其作用簡述如下,
mypcb.h : 程序控制塊PCB結構體定義。
mymain.c: 初始化各個程序并啟動0号程序。
myinterrupt.c:時鐘中斷處理和程序排程算法。
詳細分析:首先打開mypcb.h,
/* mykernel--Simple simulation of the linux OS process schedule
*
* linux/mykernel/mypcb.h
*
* Kernel internal my_timer_handler
*
* Copyright (C) 2013 Mengning
*
* Modified 2014 Yunquan Zhang <[email protected]>
*
*
* You can redistribute or modify this program under the terms
* of the GNU General Public License as published by
* the Free Software Foundation.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_TASK_NUM 10 // max num of task in system
#define KERNEL_STACK_SIZE 1024*8
#define PRIORITY_MAX 30 //priority range from 0 to 30
/* CPU-specific state of this task */
struct Thread {
unsigned long ip;//point to cpu run address
unsigned long sp;//point to the thread stack's top address
//todo add other attrubte of system thread
};
//PCB Struct
typedef struct PCB{
int pid; // pcb id
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry;//the task execute entry memory address
struct PCB *next;//pcb is a circular linked list
unsigned long priority;// task priority
//todo add other attrubte of process control block
}tPCB;
//void my_schedule(int pid);
void my_schedule(void);
在這個檔案裡,定義了 Thread 結構體,用于存儲目前程序中正在執行的線程的ip和sp,PCB結構體中的各個字段含義如下
pid:程序号
state:程序狀态,在模拟系統中,所有程序控制塊資訊都會被建立出來,其初始化值就是-1,如果被排程運作起來,其值就會變成0
stack:程序使用的堆棧
thread:目前正在執行的線程資訊
task_entry:程序入口函數
next:指向下一個PCB,模拟系統中所有的PCB是以連結清單的形式組織起來的。
這裡還有一個函數的聲明 my_schedule,它的實作在my_interrupt.c中,在mymain.c中的各個程序函數會根據一個全局變量的狀态來決定是否調用它,進而實作主動排程。
再來看看檔案 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);
}
}
}
正如前文所述,這裡的函數 my_start_kernel 是系統啟動後,最先調用的函數,在這個函數裡完成了0号程序的初始化和啟動,并建立了其它的程序PCB,以友善後面的排程。在模拟系統裡,每個程序的函數代碼都是一樣的,即 my_process 函數,my_process 在執行的時候,會列印出目前程序的 id,進而使得我們能夠看到目前哪個程序正在執行。
另外,在 my_process 也會檢查一個全局标志變量 my_need_sched,一旦發現其值為 1 ,就調用 my_schedule 完成程序的排程。
0号線程的啟動,采用了内聯彙編代碼完成,詳細參見源碼中的注釋。
再來看看最後一個檔案,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;
}
這裡 my_timer_handler 函數會被核心周期性的調用,每調用1000次,就去将全局變量my_need_sched的值修改為1,通知正在執行的程序執行排程程式my_schedule。在my_schedule函數中,完成程序的切換。程序的切換分兩種情況,一種情況是下一個程序沒有被排程過,另外一種情況是下一個程序被排程過,可以通過下一個程序的state知道其狀态。程序切換依然是通過内聯彙編代碼實作,無非是儲存舊程序的eip和堆棧,将新程序的eip和堆棧的值存入對應的寄存器中,詳見代碼中的注釋。
總結
通過本講的學習和實驗,我們知道作業系統的核心功能就是:程序排程和中斷機制,通過與硬體的配合實作多任務處理,再加上上層應用軟體的支援,最終變成可以使使用者可以很容易操作的計算機系統。最後推一下孟甯老師的《庖丁解牛Linux核心分析》,這本書非常适合新手學習Linux。