timer.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * timer support
  8. *
  9. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. */
  14. #include <linux/kvm_host.h>
  15. #include <linux/kvm.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/atomic.h>
  18. #include "kvm_timer.h"
  19. enum hrtimer_restart kvm_timer_fn(struct hrtimer *data)
  20. {
  21. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  22. struct kvm_vcpu *vcpu = ktimer->vcpu;
  23. wait_queue_head_t *q = &vcpu->wq;
  24. /*
  25. * There is a race window between reading and incrementing, but we do
  26. * not care about potentially losing timer events in the !reinject
  27. * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked
  28. * in vcpu_enter_guest.
  29. */
  30. if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
  31. atomic_inc(&ktimer->pending);
  32. /* FIXME: this code should not know anything about vcpus */
  33. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  34. }
  35. if (waitqueue_active(q))
  36. wake_up_interruptible(q);
  37. if (ktimer->t_ops->is_periodic(ktimer)) {
  38. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  39. return HRTIMER_RESTART;
  40. } else
  41. return HRTIMER_NORESTART;
  42. }