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

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










 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值