實驗內容
- 編寫匯編程序 switch_to:
- 完成主體框架;
- 在主體框架下依次完成 PCB 切換、內核棧切換、LDT 切換等;
- 修改 fork(),由於是基於內核棧的切換,所以進程需要創建出能完成內核棧切換的樣子。
- 修改 PCB,即 task_struct 結構,增加相應的內容域,同時處理由於修改了 task_struct 所造成的影響。
- 用修改后的 Linux 0.11 仍然可以啟動、可以正常使用。
實驗步驟
1.修改/kernel/system_call.s文件
.globl system_call,sys_fork,timer_interrupt,sys_execve
.globl hd_interrupt,floppy_interrupt,parallel_interrupt
.globl device_not_available, coprocessor_error
# 以上是原代碼部分,以下是需要新建的代碼
# system_call.s
# 匯編語言中定義的方法可以被其他調用需要
.globl switch_to
.globl first_return_from_kernel
# 硬編碼改變 these are offsets into the task-struct
ESP0 = 4
KERNEL_STACK = 12
state = 0 # these are offsets into the task-struct.
counter = 4
priority = 8
kernelstack = 12
signal = 16
sigaction = 20 # MUST be 16 (=len of sigaction)
blocked = (37*16)
switch_to:
pushl %ebp
movl %esp,%ebp
pushl %ecx
pushl %ebx
pushl %eax
movl 8(%ebp),%ebx
cmpl %ebx,current
je 1f
# switch_to PCB
movl %ebx,%eax
xchgl %eax,current
# rewrite TSS pointer
movl tss,%ecx
addl $4096,%ebx
movl %ebx,ESP0(%ecx)
# switch_to system core stack
movl %esp,KERNEL_STACK(%eax)
movl 8(%ebp),%ebx
movl KERNEL_STACK(%ebx),%esp
# switch_to LDT
movl 12(%ebp), %ecx
lldt %cx
movl $0x17,%ecx
mov %cx,%fs
# nonsense
cmpl %eax,last_task_used_math
jne 1f
clts
1: popl %eax
popl %ebx
popl %ecx
popl %ebp
ret
.align 2
first_return_from_kernel:
popl %edx
popl %edi
popl %esi
pop %gs
pop %fs
pop %es
pop %ds
iret
該段代碼完成的工作如下:
1.push l %ebp
首先在匯編中處理棧幀,即處理 ebp 寄存器
2.cmpl %ebx,current
接下來要取出表示下一個進程 PCB 的參數,並和 current 做一個比較,如果等於 current,則什么也不用做。不等於 current,就開始進程切換。
3.switch_to PCB完成 PCB 的切換
ebx是從參數中取出來的下一個進程的 PCB 指針,經過兩條指令以后,eax 指向現在的當前進程,ebx指向下一個進程,全局變量 current 也指向下一個進程。
4.rewrite TSS pointerTSS 中的內核棧指針的重寫
中斷處理時需要尋找當前進程的內核棧,否則就不能從用戶棧切到內核棧(中斷處理沒法完成),內核棧的尋找是借助當前進程TSS中存放的信息來完成的。
5.switch_to system core stack內核棧的切換
將寄存器 esp(內核棧使用到當前情況時的棧頂位置)的值保存到當前 PCB 中,再從下一個 PCB 中的對應位置上取出保存的內核棧棧頂放入 esp寄存器,這樣處理完以后,再使用內核棧時使用的就是下一個進程的內核棧了。
6.switch_to LDTLDT的切換
指令 movl 12(%ebp),%ecx 負責取出對應 LDT(next)的那個參數,指令 lldt %cx 負責修改 LDTR 寄存器,一旦完成了修改,下一個進程在執行用戶態程序時使用的映射表就是自己的 LDT 表了,地址空間實現了分離。
最后,通過FS操作系統才能訪問進程的用戶態內存。這里LDT切換完成意味着切換到了新的用戶態地址空間,所以需要重置FS。
代碼截圖如下(部分):
2.修改/include/linux/sched.h文件
注釋掉原來switch_to宏函數,截圖如下:
基於堆棧的切換程序要做到承上啟下:
- 承上:基於堆棧的切換,要用到當前進程(current指向)與目標進程的PCB,當前進程與目標進程的內核棧等
Linux 0.11 進程的內核棧和該進程的 PCB 在同一頁內存上(一塊 4KB 大小的內存),其中 PCB 位於這頁內存的低地址,棧位於這頁內存的高地址 - 啟下:要將next傳遞下去,雖然 TSS(next)不再需要了,但是 LDT(next)仍然是需要的。
之前的進程控制塊(pcb)中是沒有保存內核棧信息的寄存器的,所以需要在sched.h中的task_struct(也就是pcb)中添加kernelstack。
struct task_struct {
/* these are hardcoded - don't touch */
long state; /* -1 unrunnable, 0 runnable, >0 stopped */
long counter;
long priority;
//新增kernelstack
long kernelstack;
long signal;
struct sigaction sigaction[32];
//......
代碼截圖如下:
由於這里將 PCB 結構體的定義改變了,所以在產生 0 號進程的 PCB 初始化時也要跟着一起變化,需要修改 #define INIT_TASK,即在 PCB 的第四項中增加關於內核棧棧指針的初始化。
#define INIT_TASK \
/* state etc */ { 0,15,15,PAGE_SIZE+(long)&init_task, \
//......
代碼截圖如下:
3.修改/kernel/sched.c文件
// 添加的代碼,定義tss
struct task_struct *tss= &(init_task.task.tss);
void schedule(void)
{
int i,next,c;
struct task_struct ** p;
struct task_struct *pnext = NULL; // 添加的代碼,賦值初始化任務的指針
/* check alarm, wake up any interruptible tasks that have got a signal */
for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
if (*p) {
if ((*p)->alarm && (*p)->alarm < jiffies) {
(*p)->signal |= (1<<(SIGALRM-1));
(*p)->alarm = 0;
}
if (((*p)->signal & ~(_BLOCKABLE & (*p)->blocked)) &&
(*p)->state==TASK_INTERRUPTIBLE)
(*p)->state=TASK_RUNNING;
}
/* this is the scheduler proper: */
while (1) {
c = -1;
next = 0;
// 添加的代碼. 如果系統沒有進程可以調度時傳遞進去的是一個空值,系統宕機,
// 所以加上這句,這樣就可以在next=0時不會有空指針傳遞
pnext = task[next];
i = NR_TASKS;
p = &task[NR_TASKS];
while (--i) {
if (!*--p)
continue;
if ((*p)->state == TASK_RUNNING && (*p)->counter> c)
c = (*p)->counter, next = i, pnext=*p;// 修改添加的代碼
}
if (c) break;
for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
if (*p)
(*p)->counter = ((*p)->counter >> 1) +
(*p)->priority;
}
//switch_to(next);
switch_to(pnext, _LDT(next)); // 修改添加的代碼
}
更改截圖如下:
4.修改fork.c文件
對fork()的修改就是對子進程的內核棧的初始化,在fork()的核心實現copy_process中,p = (struct task_struct) get_free_page();用來完成申請一頁內存作為子進程的PCB,而p指針加上頁面大小就是子進程的內核棧位置. 所以需要再定義一個指針變量krnstack, 並將其初始化為內核棧頂指針, 然后再根據傳遞進來的參數把前一個進程的PCB中各種信息都保存到當前棧中。
可以將原代碼copy_process函數注釋,替換為以下:
//fork.c
//6th
extern void first_return_from_kernel(void);
//fork.c copy_process()
int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
long ebx,long ecx,long edx,
long fs,long es,long ds,
long eip,long cs,long eflags,long esp,long ss)
{
struct task_struct *p;
int i;
struct file *f;
long * krnstack;
//1st
p = (struct task_struct *) get_free_page();
if (!p)
return -EAGAIN;
task[nr] = p;
*p = *current; /* NOTE! this doesn't copy the supervisor stack */
p->state = TASK_UNINTERRUPTIBLE;
p->pid = last_pid;
p->father = current->pid;
p->counter = p->priority;
p->signal = 0;
p->alarm = 0;
p->leader = 0; /* process leadership doesn't inherit */
p->utime = p->stime = 0;
p->cutime = p->cstime = 0;
p->start_time = jiffies;
if (last_task_used_math == current)
__asm__("clts ; fnsave %0"::"m" (p->tss.i387));
if (copy_mem(nr,p)) {
task[nr] = NULL;
free_page((long) p);
return -EAGAIN;
}
//2nd
krnstack = (long *) (PAGE_SIZE + (long) p);
*(--krnstack) = ss & 0xffff;
*(--krnstack) = esp;
*(--krnstack) = eflags;
*(--krnstack) = cs & 0xffff;
*(--krnstack) = eip;
*(--krnstack) = ds & 0xffff;
*(--krnstack) = es & 0xffff;
*(--krnstack) = fs & 0xffff;
*(--krnstack) = gs & 0xffff;
*(--krnstack) = esi;
*(--krnstack) = edi;
*(--krnstack) = edx;
//3rd
*(--krnstack) = first_return_from_kernel;
//4th
*(--krnstack) = ebp;
*(--krnstack) = ecx;
*(--krnstack) = ebx;
*(--krnstack) = 0;
//5th
p->kernelstack = krnstack;
for (i=0; i<NR_OPEN;i++)
if ((f=p->filp[i]))
f->f_count++;
if (current->pwd)
current->pwd->i_count++;
if (current->root)
current->root->i_count++;
if (current->executable)
current->executable->i_count++;
set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
p->state = TASK_RUNNING; /* do this last, just in case */
return last_pid;
}
5.驗證結果:
經過驗證,用修改后的 Linux 0.11 仍然可以啟動、可以正常使用。