darkyinliang 发表于 2016-1-9 14:16:30

Xen的PV Guest如何wake up

  问题:一个PV guest如果一直block,当其内部有任务需要运行的时候,如果让VMM知道呢?
  
  Xen的PV guest想要wake up,一定要通过外部事件。比如,当有network request进来的时候,Xen VMM会给guest传递event,类似于virtual IRQ。
  
  当PV guest由于内部无任务运行的时候,它会运行idle process,被改写过的idle process通过hypercall调用SCHED_block达到释放CPU的目的。可是,何时能再得到CPU的运行权呢?
  
  看struct VCPU:
  struct vcpu {
  ... ...
struct timer periodic_timer;
struct timer singleshot_timer;
  ... ...
  }
  
  periodic_timer就是负责周期性的唤醒PV guest的。
  init_timer(&v->periodic_timer, vcpu_periodic_timer_fn,v, v->processor);
  By default, the timeout of periodic_timer is set to be "10ms" (100HZ, see "xen/arch/x86/domain.c"). Somehow,我在ia64目录下没有找到这个variable,难道64bit guest不用这个...? (需要进一步考证)
  
  注意:Driver domain (domain 0)的periodic_timer的频率是1000HZ,是在创建时设置的.
  (linux/arch/x86/kernel/time-xen.c)
  #define NS_PER_TICK (1000000000LL/HZ)
  static struct vcpu_set_periodic_timer xen_set_periodic_tick = {
.period_ns = NS_PER_TICK
};
  HYPERVISOR_vcpu_op(VCPUOP_set_periodic_timer, cpu, &xen_set_periodic_tick)
  
  ---------------------------------------------------------------------
  vcpu_periodic_timer_fn
  |
  vcpu_periodic_timer_work
  |
  send_timer_event
  |
  send_guest_vcpu_virq(v, VIRQ_TIMER);
  |
  evtchn_set_pending(v, port);
  |
  vcpu_mark_events_pending(v);
  |
  vcpu_kick(v);
  |
  vcpu_unblock(v);
  ---------------------------------------------------------------------
  
  到这里,PV guest被唤醒了,如果有任务需要执行,马上执行;否则进入idle process,然后继续do_block.
  
  说说另一个:singleshot_timer。从名字可以看出,single shot一下,然后就没了,不是periodic的。
  init_timer(&v->singleshot_timer, vcpu_singleshot_timer_fn,v, v->processor);
  static void vcpu_singleshot_timer_fn(void *data)
{
struct vcpu *v = data;
send_timer_event(v); //timer expire之后,没有再次set timeout,所以不是周期性的。
}
  其实,这个timer是由PV guest在running的状态下trigger的。
  
  在linux/arch/x86/xen/time.c中,有详细讲到xen如何实现clockevent (though it is not elegant ...)
  xen_vcpuop_set_next_event
  |
  HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, cpu, &single);
  |
  __HYPERVISOR_set_timer_op
  |
  do_set_timer_op
  |
  set_timer(&v->singleshot_timer, timeout);
  -----------------------------------------------------------------------
  
  补充,VIRQ_TIMER这个hypercall在PV Guest里起了什么作用?
  /linux/arch/x86/kernel/time-xen.c
  /linux/arch/x86/kernel/time.c
页: [1]
查看完整版本: Xen的PV Guest如何wake up