這周的課程信息量比較大,現作一個整理,也算是爲了完成做業。
0. 筆記
內嵌彙編語法以下:linux
__asm__ __volatile__ ( 彙編語句模板: 輸出部分: 輸入部分: 破壞描述部分 );
爲了方便查看,特將文件放在這裏,並在文件中進行註釋,加深本身的理解
(1)mypcb.h 頭文件tcp
/* * 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.cthis
/* * 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.cspa
/* * 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 被初始化時堆棧變化rest
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 |__________________________| |__________________________| |__________________________| |__________________________| |__________________________|
進程調度代碼:code
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...ip
對論壇上 else中"movl $1f,%1\n\t"
的問題,看到老師的回覆是「$1f放到eip裏使用纔算生效吧,不能見$1f就找標號1」。
個人理解是這樣的:if中的天然沒必要說,else中代碼只有進程第一次被執行時候纔會運行,此時將$1f存入prev->thread.ip,但當進程被從新調度執行的時候,此時進入了if代碼塊中,所以將執行if代碼塊中的1:標號處的代碼,因此else中沒有"1:"也就不奇怪了。
(3)截圖
(4)總結 本次試驗最重要的是進程的切換,進程執行過程當中,當時間片用完須要進行進程切換時,須要先將當前的進程執行環境進行保存,下次進程被調度時,須要恢復進程的執行環境。這樣實現多道進程的執行。 經過此次實驗,對中斷和進程切換的具體過程有了瞭解,是一次很好的實踐。