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)))