x86 64 内核堆栈

switch_to.h中

define switch_to(prev, next, last) \

do { \
/* \
* Context-switching clobbers all registers, so we clobber \
* them explicitly, via unused output variables. \
* (EAX and EBP is not listed because EBP is saved/restored \
* explicitly for wchan access and EAX is the return value of \
* __switch_to()) \
*/ \
unsigned long ebx, ecx, edx, esi, edi; \
\
asm volatile(“pushfl\n\t” /* save flags */ \
“pushl %%ebp\n\t” /* save EBP */ \
“movl %%esp,%[prev_sp]\n\t” /* save ESP */ \
“movl %[next_sp],%%esp\n\t” /* restore ESP */ \
“movl $1f,%[prev_ip]\n\t” /* save EIP */ \
“pushl %[next_ip]\n\t” /* restore EIP */ \
__switch_canary \
“jmp __switch_to\n” /* regparm call */ \
“1:\t” \
“popl %%ebp\n\t” /* restore EBP */ \
“popfl\n” /* restore flags */ \
\
/* output parameters */ \
: [prev_sp] “=m” (prev->thread.sp), \
[prev_ip] “=m” (prev->thread.ip), \
“=a” (last), \
\
/* clobbered output registers: */ \
“=b” (ebx), “=c” (ecx), “=d” (edx), \
“=S” (esi), “=D” (edi) \
\
__switch_canary_oparam \
\
/* input parameters: */ \
: [next_sp] “m” (next->thread.sp), \
[next_ip] “m” (next->thread.ip), \
\
/* regparm parameters for __switch_to(): */ \
[prev] “a” (prev), \
[next] “d” (next) \
\
__switch_canary_iparam \
\
: /* reloaded segment registers */ \
“memory”); \
} while (0)
会切换将切换前的进程内核堆栈指针用汇编movl %%esp,%[prev_sp] 保存。其中被保存的地址%[prev_sp]是prev->thread.sp ,切换后的进程esp从保存中恢复使用汇编”movl %[next_sp],%%esp,其中%[next_sp]就是保存到next->thread.sp中的。

即使一个进程切换到用户态,它对应的内核态栈被弹出,但是这部分内存不会被其他代码修改,因此可以在其他cpu上打印该进程在cpu的前一次内核堆栈。

注意: 只在进程切换的时候保存到thread.sp中,如果是中断、系统调用进出内核,不会保存,由汇编保存到堆栈中

每次重新进入内核时,sp通过
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 重新设置

猜你喜欢

转载自blog.csdn.net/wdjjwb/article/details/77527515
今日推荐