硬件文境的切换 -- __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)))
{
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)))
相关文章推荐
- xenomai 3.0.2 -任务切换《xnarch_switch_to》
- linux0.11任务切换switch_to
- Linux进程(之)进程切换函数switch_to()解析
- Linux任务切换代码(switch_to)详解
- How to switch to another database in oracle 11g(如何在oracle中从一个数据库切换到另一个数据库)
- Linux任务切换代码(switch_to)详解(转)
- switch_to及ret_from_sys_call控制任务的切换与返回
- switch_to_frame,切换frame框架
- Linux 0.12 switch_to切换过程
- switch_to及ret_from_sys_call控制任务的切换与返回
- web自动化测试第11步:切换窗口、frame、alert的新方法:switch_to包详解
- 进程切换switch_to()注释
- switch_to及ret_from_sys_call控制任务的切换与返回
- How to force a log switch-强制切换日志
- selenium switchTo() 切换窗口
- Oracle Data Guard Switchover 切换
- DataGuard之DG 故障切换(switchover和failover)以及利用flashback 进行恢复
- How to Switch Between GDM and KDM on Ubuntu
- Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)(十二)
- glibc静态链接 libc.a(nsswitch.o)(.data+0x64):undefined reference to `_nss_files_getaliasent_r' 错误解决方法