硬體文境的切換 — __switch_to()

來源:互聯網
上載者:User
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)))

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.