http://lxr.linux.no/#linux+v2.6.31/arch/x86/kernel/process_32.c#L347319/* 320 * switch_to(x,yn) should switch tasks from x to y. 321 * 322 * We fsave/fwait so that an exception goes off at the right time 323 * (as a call from the fsave or fwait in effect) rather than to 324 * the wrong process. Lazy FP saving no longer makes any sense 325 * with modern CPU's, and this simplifies a lot of things (SMP 326 * and UP become the same). 327 * 328 * NOTE! We used to use the x86 hardware context switching. The 329 * reason for not using it any more becomes apparent when you 330 * try to recover gracefully from saved state that is no longer 331 * valid (stale segment register values in particular). With the 332 * hardware task-switch, there is no way to fix up bad state in 333 * a reasonable manner. 334 * 335 * The fact that Intel documents the hardware task-switching to 336 * be slow is a fairly red herring - this code is not noticeably 337 * faster. However, there _is_ some room for improvement here, 338 * so the performance issues may eventually be a valid point. 339 * More important, however, is the fact that this allows us much 340 * more flexibility. 341 * 342 * The return value (in %ax) will be the "prev" task after 343 * the task-switch, and shows up in ret_from_fork in entry.S, 344 * for example. 345 */ 346__notrace_funcgraph struct task_struct * 347__switch_to(struct task_struct *prev_p, struct task_struct *next_p) 348{ 349 struct thread_struct *prev = &prev_p->thread, 350 *next = &next_p->thread; 351 int cpu = smp_processor_id(); 352 struct tss_struct *tss = &per_cpu(init_tss, cpu); 353 354 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */ 355 356 __unlazy_fpu(prev_p); 357 358 359 /* we're going to use this soon, after a few expensive things */ 360 if (next_p->fpu_counter > 5) 361 prefetch(next->xstate); 362 363 /* 364 * Reload esp0. 365 */ 366 load_sp0(tss, next); 367 368 /* 369 * Save away %gs. No need to save %fs, as it was saved on the 370 * stack on entry. No need to save %es and %ds, as those are 371 * always kernel segments while inside the kernel. Doing this 372 * before setting the new TLS descriptors avoids the situation 373 * where we temporarily have non-reloadable segments in %fs 374 * and %gs. This could be an issue if the NMI handler ever 375 * used %fs or %gs (it does not today), or if the kernel is 376 * running inside of a hypervisor layer. 377 */ 378 lazy_save_gs(prev->gs); 379 380 /* 381 * Load the per-thread Thread-Local Storage descriptor. 382 */ 383 load_TLS(next, cpu); 384 385 /* 386 * Restore IOPL if needed. In normal use, the flags restore 387 * in the switch assembly will handle this. But if the kernel 388 * is running virtualized at a non-zero CPL, the popf will 389 * not restore flags, so it must be done in a separate step. 390 */ 391 if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl)) 392 set_iopl_mask(next->iopl); 393 394 /* 395 * Now maybe handle debug registers and/or IO bitmaps 396 */ 397 if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV || 398 task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT)) 399 __switch_to_xtra(prev_p, next_p, tss); 400 401 /* 402 * Leave lazy mode, flushing any hypercalls made here. 403 * This must be done before restoring TLS segments so 404 * the GDT and LDT are properly updated, and must be 405 * done before math_state_restore, so the TS bit is up 406 * to date. 407 */ 408 arch_end_context_switch(next_p); 409 410 /* If the task has used fpu the last 5 timeslices, just do a full 411 * restore of the math state immediately to avoid the trap; the 412 * chances of needing FPU soon are obviously high now 413 * 414 * tsk_used_math() checks prevent calling math_state_restore(), 415 * which can sleep in the case of !tsk_used_math() 416 */ 417 if (tsk_used_math(next_p) && next_p->fpu_counter > 5) 418 math_state_restore(); 419 420 /* 421 * Restore %gs if needed (which is common) 422 */ 423 if (prev->gs | next->gs) 424 lazy_load_gs(next->gs); 425 426 percpu_write(current_task, next_p); 427 428 return prev_p; 429} 430 |
