- 论坛徽章:
- 0
|
回复 4# 巨人史玉柱
这是x86_64架构上的switch_to宏:(linux-2.6.1
- 20 #define __EXTRA_CLOBBER \
- 21 ,"rcx","rbx","rdx","r8","r9","r10","r11","r12","r13","r14","r15"
- 22
- 23 /* Save restore flags to clear handle leaking NT */
- 24 #define switch_to(prev,next,last) \
- 25 asm volatile(SAVE_CONTEXT \
- 26 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
- 27 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
- 28 "call __switch_to\n\t" \
- 29 ".globl thread_return\n" \
- 30 "thread_return:\n\t" \
- 31 "movq %%gs:%P[pda_pcurrent],%%rsi\n\t" \
- 32 "movq %P[thread_info](%%rsi),%%r8\n\t" \
- 33 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
- 34 "movq %%rax,%%rdi\n\t" \
- 35 "jc ret_from_fork\n\t" \
- 36 RESTORE_CONTEXT \
- 37 : "=a" (last) \
- 38 : [next] "S" (next), [prev] "D" (prev), \
- 39 [threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \
- 40 [ti_flags] "i" (offsetof(struct thread_info, flags)),\
- 41 [tif_fork] "i" (TIF_FORK), \
- 42 [thread_info] "i" (offsetof(struct task_struct, thread_info)), \
- 43 [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \
- 44 : "memory", "cc" __EXTRA_CLOBBER)
复制代码 这个__EXTRA_CLOBBER 指的就是rcx,rdx等寄存器的值在展开成汇编时不能使用他们,这样也就确保了这些寄存器的值不被改变。但是,32位的switch_to却没有这个东东,我也很奇怪,所以继续往下找了一下linux-2.6.36的内核代码,发现后来也加了这个东西,arch/x86/include/asm/system.h里第一个switch_to的宏定义,有下面一段:
- 76 /* clobbered output registers: */ \
- 77 "=b" (ebx), "=c" (ecx), "=d" (edx), \
- 78 "=S" (esi), "=D" (edi) \
复制代码 可能是后来加的 |
|