打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
7.switch

第六讲(传送门:fork + execve:一个进程的诞生)我们介绍了进程的诞生,可是操作系统中有很多进程,进程之间怎么切换的,又有哪些奥秘?我们回到源码,细细阅读。

操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已。

对于理解操作系统的工作机制,反而是进程的调度时机与进程的切换机制更为关键。

进程调度的时机:

1.             中断处理过程(包括时钟中断、I/O中断、系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule();

2.             内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;

3.             用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。

 

进程的切换

为了控制进程的执行,内核必须有能力挂起正在CPU上执行的进程,并恢复以前挂起的某个进程的执行,这叫做进程切换、任务切换、上下文切换;

挂起正在CPU上执行的进程,与中断时保存现场是不同的,中断前后是在同一个进程上下文中,只是由用户态转向内核态执行;

进程上下文包含了进程执行需要的所有信息

1.      用户地址空间:包括程序代码,数据,用户堆栈等

2.      控制信息:进程描述符,内核堆栈等

3.      硬件上下文(注意中断也要保存硬件上下文只是保存的方法不同)

 

schedule()函数选择一个新的进程来运行,并调用context_switch进行上下文的切换,这个宏调用switch_to来进行关键上下文切换。

next= pick_next_task(rq, prev);//进程调度算法都封装这个函数内部

context_switch(rq,prev, next);//进程上下文切换

switch_to利用了prev和next两个参数:prev指向当前进程,next指向被调度的进程

 

 

我们从schedule()函数开始,一步步分析,看内核为切换进程做了哪些工作。schedule()函数位于linux-3.18.6\kernel\sched\core.c文件中。

  1. asmlinkage__visible void __sched schedule(void)
  2. {
  3. struct task_struct *tsk = current;
  4. sched_submit_work(tsk);
  5. __schedule();
  6. }


__visible指出在内核的任何位置都可以调用schedule()。schedule()的尾部调用了__schedule(),我们进入__schedule()看看,__schedule()位于linux-3.18.6\kernel\sched\core.c文件中。

进入__schedule();之后的函数栈为:schedule()-> __schedule()。


  1. static void__sched __schedule(void)
  2. {
  3. struct task_struct *prev, *next;
  4. unsigned long *switch_count;
  5. struct rq *rq;
  6. int cpu;
  7. need_resched:
  8. preempt_disable();
  9. cpu = smp_processor_id();
  10. rq = cpu_rq(cpu);
  11. rcu_note_context_switch(cpu);
  12. prev = rq->curr;
  13. schedule_debug(prev);
  14. if (sched_feat(HRTICK))
  15. hrtick_clear(rq);
  16. /*
  17. * Make sure thatsignal_pending_state()->signal_pending() below
  18. * can't be reordered with__set_current_state(TASK_INTERRUPTIBLE)
  19. * done by the caller to avoid the race withsignal_wake_up().
  20. */
  21. smp_mb__before_spinlock();
  22. raw_spin_lock_irq(&rq->lock);
  23. switch_count = &prev->nivcsw;
  24. if (prev->state && !(preempt_count()& PREEMPT_ACTIVE)) {
  25. if(unlikely(signal_pending_state(prev->state, prev))) {
  26. prev->state =TASK_RUNNING;
  27. } else {
  28. deactivate_task(rq,prev, DEQUEUE_SLEEP);
  29. prev->on_rq = 0;
  30. /*
  31. * If a worker went to sleep, notify and askworkqueue
  32. * whether it wants to wake up a task tomaintain
  33. * concurrency.
  34. */
  35. if (prev->flags& PF_WQ_WORKER) {
  36. structtask_struct *to_wakeup;
  37. to_wakeup =wq_worker_sleeping(prev, cpu);
  38. if(to_wakeup)
  39. try_to_wake_up_local(to_wakeup);
  40. }
  41. }
  42. switch_count =&prev->nvcsw;
  43. }
  44. if (task_on_rq_queued(prev) ||rq->skip_clock_update < 0)
  45. update_rq_clock(rq);
  46. next = pick_next_task(rq, prev);
  47. clear_tsk_need_resched(prev);
  48. clear_preempt_need_resched();
  49. rq->skip_clock_update = 0;
  50. if (likely(prev != next)) {
  51. rq->nr_switches++;
  52. rq->curr = next;
  53. ++*switch_count;
  54. context_switch(rq, prev,next); /* unlocks the rq */
  55. /*
  56. * The context switch have flipped the stackfrom under us
  57. * and restored the local variables which weresaved when
  58. * this task called schedule() in the past.prev == current
  59. * is still correct, but it can be moved toanother cpu/rq.
  60. */
  61. cpu = smp_processor_id();
  62. rq = cpu_rq(cpu);
  63. } else
  64. raw_spin_unlock_irq(&rq->lock);
  65. post_schedule(rq);
  66. sched_preempt_enable_no_resched();
  67. if (need_resched())
  68. goto need_resched;
  69. }


__schedule(void)的关键代码是

next =pick_next_task(rq, prev)


         pick_next_task()封装了Linux的进程调度策略,我们不关注Linux使用了何种进程调度策略(有兴趣的童鞋可以深入研究pick_next_task()函数),总之Linux选出了下一个将要执行的进程。

next =pick_next_task(rq, prev)

得到next之后下一步工作要完成进程上下文的切换,进程上下文的切换主要通过
context_switch(rq,prev, next);

来实现。

我们进入context_switch(rq,prev, next);函数,看看进程上下文切换的具体过程。context_switch()位于linux-3.18.6\kernel\sched\core.c文件中。

注意,进入context_switch()之后的函数栈为:schedule()-> __schedule() –> context_switch()。


  1. static inlinevoid
  2. context_switch(structrq *rq, struct task_struct *prev,
  3. struct task_struct *next)
  4. {
  5. struct mm_struct *mm, *oldmm;
  6. prepare_task_switch(rq, prev, next);
  7. mm = next->mm;
  8. oldmm = prev->active_mm;
  9. /*
  10. * For paravirt, this is coupled with an exitin switch_to to
  11. * combine the page table reload and the switchbackend into
  12. * one hypercall.
  13. */
  14. arch_start_context_switch(prev);
  15. if (!mm) {
  16. next->active_mm = oldmm;
  17. atomic_inc(&oldmm->mm_count);
  18. enter_lazy_tlb(oldmm, next);
  19. } else
  20. switch_mm(oldmm, mm, next);
  21. if (!prev->mm) {
  22. prev->active_mm = NULL;
  23. rq->prev_mm = oldmm;
  24. }
  25. /*
  26. * Since the runqueue lock will be released bythe next
  27. * task (which is an invalid locking op but inthe case
  28. * of the scheduler it's an obviousspecial-case), so we
  29. * do an early lockdep release here:
  30. */
  31. spin_release(&rq->lock.dep_map,1, _THIS_IP_);
  32. context_tracking_task_switch(prev,next);
  33. /* Here we just switch the registerstate and the stack. */
  34. switch_to(prev, next, prev);
  35. barrier();
  36. /*
  37. * this_rq must be evaluated again because prevmay have moved
  38. * CPUs since it called schedule(), thus the'rq' on its stack
  39. * frame will be invalid.
  40. */
  41. finish_task_switch(this_rq(), prev);
  42. }


context_switch()中的关键代码为

switch_to(prev,next, prev);


switch_to()是一个宏,而不是一个函数。我们注意到switch_to()头顶上的注释,/* Here we just switch the register state and the stack. */:切换寄存器的状态以及切换next进程和prev进程的堆栈。

我们去看看switch_to()的代码实现,switch_to()位于linux-3.18.6\arch\x86\include\asm\switch_to.h文件中。进入switch_to()后我们的函数栈为:schedule() -> __schedule() –> context_switch() –> switch_to()。(switch_to()虽然不算函数,可为了表示代码执行过程,姑且把switch_to()压入函数栈吧~)。


  1. /*
  2. * Saving eflags is important. It switches notonly IOPL between tasks,
  3. * it also protects other tasks from NT leakingthrough sysenter etc.
  4. */
  5. #defineswitch_to(prev, next, last) \
  6. do { \
  7. /* \
  8. * Context-switching clobbers all registers, sowe clobber \
  9. * them explicitly, via unused outputvariables. \
  10. * (EAX and EBP is not listed because EBP issaved/restored \
  11. * explicitly for wchan access and EAX is thereturn value of \
  12. * __switch_to()) \
  13. */ \
  14. unsigned long ebx, ecx, edx, esi, edi; \
  15. \
  16. asm volatile("pushfl\n\t" /* save flags */ \
  17. "pushl %%ebp\n\t" /* save EBP */ \
  18. "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
  19. "movl %[next_sp],%%esp\n\t" /* restore ESP */ \
  20. "movl $1f,%[prev_ip]\n\t" /* save EIP */ \
  21. "pushl %[next_ip]\n\t" /* restore EIP */ \
  22. __switch_canary \
  23. "jmp __switch_to\n" /* regparm call */ \
  24. "1:\t" \
  25. "popl %%ebp\n\t" /* restore EBP */ \
  26. "popfl\n" /* restore flags */ \
  27. \
  28. /* output parameters */ \
  29. : [prev_sp] "=m"(prev->thread.sp), \
  30. [prev_ip] "=m"(prev->thread.ip), \
  31. "=a" (last), \
  32. \
  33. /* clobbered output registers: */ \
  34. "=b" (ebx), "=c"(ecx), "=d" (edx), \
  35. "=S" (esi), "=D"(edi) \
  36. \
  37. __switch_canary_oparam \
  38. \
  39. /* input parameters: */ \
  40. : [next_sp] "m" (next->thread.sp), \
  41. [next_ip] "m" (next->thread.ip), \
  42. \
  43. /* regparm parameters for __switch_to():*/ \
  44. [prev] "a" (prev), \
  45. [next] "d" (next) \
  46. \
  47. __switch_canary_iparam \
  48. \
  49. : /* reloaded segment registers */ \
  50. "memory"); \
  51. } while (0)


switch_to()是AT&T语法的gcc内嵌汇编,其中已有不少注释,我来一句句的翻译注释吧。

 

1.        

"pushfl\n\t"

把当前进程(prev)的flag压入当前进程的内核堆栈;

 

2.        

"pushl %%ebp\n\t"

把当前进程(prev)的内核堆栈基址(%ebp寄存器值)压入当前进程的内核堆栈;

 

3.        

"movl%%esp,%[prev_sp]\n\t"

结合/* outputparameters */下的[prev_sp] "=m" (prev->thread.sp)可知%[prev_sp]代表prev->thread.sp。也就是把当前进程(prev)的内核堆栈栈顶(%esp寄存器值)保存到prev->thread.sp中。

 

4.        

"movl%[next_sp],%%esp\n\t"

结合/* inputparameters: */下的[next_sp]  "m"(next->thread.sp)可知%[next_sp]代表next->thread.sp。也就是把next进程的内核堆栈栈顶(next->thread.sp值)还原到esp寄存器中。

 

5.       

  1. "movl%%esp,%[prev_sp]\n\t"
  2. "movl %[next_sp],%%esp\n\t"

这两步完成了内核堆栈的切换。

"movl %[next_sp],%%esp\n\t"


这句之后的堆栈操作都是在next进程(next)的内核堆栈中进行的。

 

6.        

"movl$1f,%[prev_ip]\n\t"

把"1:\t"地址赋给prev进程的IP指针,当prev进程下次被switch_to回来时,就从"1:\t"这个位置开始执行,"1:\t"的后面两句是        "popl%%ebp\n\t"和"popfl\n",恢复prev进程的flag和内核堆栈基址。

 

7.       

"pushl%[next_ip]\n\t"

结合/* inputparameters: */下的[next_ip]  "m"(next->thread.ip)可知%[next_ip]代表next->thread.ip。也就是把next进程的ip指针(执行起点)压入next进程的内核堆栈栈顶。

 

8.        

"jmp __switch_to\n"

__switch_to是一个函数,这里没有call __switch_to函数,而是jmp __switch_to,jmp的方式通过寄存器传递参数。请看/* regparm parameters for __switch_to(): */         \注释

  1. [prev] "a"(prev),
  2. [next] "d" (next)


通过”a”(表示eax寄存器)和”d”(表示edx寄存器)传递参数。因为我们没有call __switch_to,故没有把__switch_to函数的下一条指令压入next进程的内核堆栈,虽然__switch_to没有被call,但是不影响__switch_to返回(ret)时pop(弹出)next进程内核堆栈栈顶的值,并把该值赋给了next的IP指针,next根据IP 指针指向的位置开始执行。

__switch_to返回时弹出的值是什么呢?next是要被换入执行的进程,那么next之前肯定被换出过,next被换出时同样执行了以下两句:

  1. "movl $1f,%[prev_ip]\n\t"
  2. "pushl %[next_ip]\n\t"


这两句使next的内核堆栈栈顶保存了"1:\t"的位置。

所以__switch_to返回时弹出了"1:\t"的位置,并把该位置赋给next的IP指针,进而可知next的开始执行位置是"1:\t"。

 

9.         

  1. "popl %%ebp\n\t" /* restore EBP */ \
  2. "popfl\n" /* restore flags */ \


恢复next进程的内核堆栈和标志,接下来next进程就可以愉快的执行了!

 

 

         __switch_to的模糊地带:

  1. "movl%%esp,%[prev_sp]\n\t"
  2. "movl%[next_sp],%%esp\n\t"


这两句话把prev的内核堆栈切换为了next的内核堆栈。

但是到

"1:\t"


时才开始执行next进程的第一条指令。

中间的这一段

  1. "movl$1f,%[prev_ip]\n\t" /*save EIP */ \
  2. "pushl%[next_ip]\n\t" /* restoreEIP */ \
  3. __switch_canary \
  4. "jmp__switch_to\n" /* regparmcall */ \


使用的是next进程的内核堆栈,但是还算在prev进程里执行。

总的而言

  1. "movl%%esp,%[prev_sp]\n\t"
  2. "movl%[next_sp],%%esp\n\t"
  3. "movl$1f,%[prev_ip]\n\t" /*save EIP */ \
  4. "pushl%[next_ip]\n\t" /* restoreEIP */ \
  5. __switch_canary \
  6. "jmp__switch_to\n" /* regparm call */ \
  7. "1:\t"


这段是prev和next相对模糊的地方,说不清是属于哪个进程的执行序列。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
switch_to
linux内核进程切换代码分析
Linux内核schedule函数分析
Linux进程上下文切换过程context
《深入理解Linux内核3rd》学习笔记——进程切换(下):switch
Using the TRACE
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服