进程切换过程是由一段汇编代码switch_to完成,主要功能是保存当前进程的esp和eip,恢复切换到的进程的esp和eip。
关于C内嵌汇编的使用,可以参见: http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
其调用过程如下:
switch_to(prev, next, prev);
barrier();
finish_task_switch(this_rq(), prev);
从上面代码可以看出,保证进程被切换回来后,prev是一个正确值也是switch_to需要考虑的问题。
如: A=>B, B=>C, C=>A
若不进行处理时,在进程A被切换出去,再切换回来后,prev指向的是A。
而期望的值为C,这样才能对C做进一步的清理工作。
swith_to代码如下:
#define switch_to(prev,next,last) do { \
unsigned long esi,edi; \
asm volatile("pushfl\n\t" /* Save flags */ \
"pushl %%ebp\n\t" \
"movl %%esp,%0\n\t" /* save ESP */ \
"movl %5,%%esp\n\t" /* restore ESP */ \
"movl $1f,%1\n\t" /* save EIP */ \
"pushl %6\n\t" /* restore EIP */ \
"jmp __switch_to\n" \
"1:\t" \
"popl %%ebp\n\t" \
"popfl" \
:"=m" (prev->thread.esp),"=m" (prev->thread.eip), \
"=a" (last),"=S" (esi),"=D" (edi) \
:"m" (next->thread.esp),"m" (next->thread.eip), \
"2" (prev), "d" (next)); \
} while (0)
下面对A=>B, B=>C, C=>A的切换过程中,进程A的相关处理过程进行分析:
A=>B : switch_to(A, B, A)
1. pushfl
2. pushl %%ebp
pushl %ebp
3. movl %%esp,%0
movl %esp (A->thread.esp)
4. movl $1f,%1
movl $1f, (A->thread.eip)
C=>A: switch_to(C, A, C)
5. movl %5,%%esp
movl (A->thread.esp) %esp
6. pushl %6
pushl (A->thread.eip)
7. jmp __switch_to
__switch_to(%eax(C), %edx(A))
ret #popl %eip(A->thread.eip <=> $1f)
8. popl %%ebp
popl %ebp
9. popfl
10. "=m" (prev->thread.esp),"=m" (prev->thread.eip)
movl %esp, (A->thread.esp)
movl %eip, (A->thread.eip)
11. "=a" (last),"=S" (esi),"=D" (edi)
movl %eax(C), (last)(<=>prev)
movl %esi, (esi)
movl %edi, (edi)
注意这里的10和11两步:
在A的栈中,prev与next的值是在A=>B时赋予的,所以prev=A, next=B;
10这条语句更新的是A的thread相关字段
11这条语句执行后, prev=last=C,好做进一步处理
疑点:关于esi, edi两个变量的作用,还没有弄明白!
整理后的切换流程如下:
A=>B
pushfl
pushl %ebp
movl %esp (A->thread.esp)
movl $1f, (A->thread.eip)
C=>A
movl (A->thread.esp) %esp
pushl (A->thread.eip)
call __switch_to(C, A)
popl %eip
popl %ebp
popfl
movl %esp (A->thread.esp)
movl %eip (A->thread.eip)
movl (C) (last)