您的位置:首页 > 其它

硬件文境的切换 -- __switch_to()

2007-01-11 11:11 411 查看
struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
{
struct thread_struct *prev = &prev_p->thread,
*next = &next_p->thread;

通过当前进程next的thread_info结构体获取该进程所在CPU的编号
|---------------------------------|
| int cpu = smp_processor_id(); |
|---------------------------------|

获取当前CPU的TSS段首地址,并用*tss索引(准备把新进程next的thread域加载在TSS段中)
|-----------------------------------------------------|
| struct tss_struct *tss = &per_cpu(init_tss, cpu); |
|-----------------------------------------------------|

有选择地保存进程prev_p(prev)的FPU、MMX以及XMM寄存器的内容
|---------------------------|
| __unlazy_fpu(prev_p); |
|---------------------------|

tss->esp0 = next_p->thread.esp0
task_struct.thread.esp0 内核态堆栈
|---------------------------|
| load_esp0(tss, next); |
|---------------------------|

将本地CPU的TLS(Thread-Local Storage)加载到GDT(Global Descriptor Table)
cpu_gdt_table[cpu][6] = next_p->thread.tls_array[0];
cpu_gdt_table[cpu][7] = next_p->thread.tls_array[1];
cpu_gdt_table[cpu][8] = next_p->thread.tls_array[2];
|---------------------------|
| load_TLS(next, cpu); |
|---------------------------|

prev_p->thread.fs = %%fs
prev_p->thread.gs = %%gs
|------------------------------------------------|
| asm volatile("mov %%fs,%0":"=m" (prev->fs)); |
| asm volatile("mov %%gs,%0":"=m" (prev->gs)); |
|------------------------------------------------|
if (unlikely(prev->fs | prev->gs | next->fs | next->gs)) {
loadsegment(fs, next->fs);
loadsegment(gs, next->gs);
}
if (unlikely(next->debugreg[7])) {
loaddebug(next, 0);
loaddebug(next, 1);
loaddebug(next, 2);
loaddebug(next, 3);
loaddebug(next, 6);
loaddebug(next, 7);
}
if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr))
handle_io_bitmap(next, tss);

return prev_p;
}

This function call is different from the average function call, though, because _ _switch_to( ) takes the prev_p and next_p parameters from the eax and edx registers (where we saw they were stored), not from the stack like most functions. To force the function to go to the registers for its parameters, the kernel uses the __attribute__ and regparm keywords, which are nonstandard extensions of the C language implemented by the gcc compiler. The __switch_to( ) function is declared in the include /asm-i386 /system.h header file as follows:
__switch_to(struct task_struct *prev_p,
struct task_struct *next_p)
__attribute__(regparm(3));

extern struct task_struct * FASTCALL(__switch_to(struct task_struct *prev,
struct task_struct *next));
#define FASTCALL(x) x __attribute__((regparm(3)))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: