Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 1 | #include <linux/kvm_host.h> |
| 2 | #include <linux/kvm.h> |
| 3 | #include <linux/hrtimer.h> |
| 4 | #include <asm/atomic.h> |
| 5 | #include "kvm_timer.h" |
| 6 | |
| 7 | static int __kvm_timer_fn(struct kvm_vcpu *vcpu, struct kvm_timer *ktimer) |
| 8 | { |
| 9 | int restart_timer = 0; |
| 10 | wait_queue_head_t *q = &vcpu->wq; |
| 11 | |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 12 | /* |
| 13 | * There is a race window between reading and incrementing, but we do |
| 14 | * not care about potentially loosing timer events in the !reinject |
Marcelo Tosatti | 041b135 | 2010-03-23 14:15:53 -0300 | [diff] [blame] | 15 | * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked |
| 16 | * in vcpu_enter_guest. |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 17 | */ |
| 18 | if (ktimer->reinject || !atomic_read(&ktimer->pending)) { |
Jan Kiszka | 681405b | 2009-06-09 15:37:03 +0200 | [diff] [blame] | 19 | atomic_inc(&ktimer->pending); |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 20 | /* FIXME: this code should not know anything about vcpus */ |
Jan Kiszka | 681405b | 2009-06-09 15:37:03 +0200 | [diff] [blame] | 21 | set_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 22 | } |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 23 | |
| 24 | if (waitqueue_active(q)) |
| 25 | wake_up_interruptible(q); |
| 26 | |
| 27 | if (ktimer->t_ops->is_periodic(ktimer)) { |
| 28 | hrtimer_add_expires_ns(&ktimer->timer, ktimer->period); |
| 29 | restart_timer = 1; |
| 30 | } |
| 31 | |
| 32 | return restart_timer; |
| 33 | } |
| 34 | |
| 35 | enum hrtimer_restart kvm_timer_fn(struct hrtimer *data) |
| 36 | { |
| 37 | int restart_timer; |
| 38 | struct kvm_vcpu *vcpu; |
| 39 | struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); |
| 40 | |
Gleb Natapov | 1ed0ce0 | 2009-06-09 15:56:27 +0300 | [diff] [blame] | 41 | vcpu = ktimer->vcpu; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 42 | if (!vcpu) |
| 43 | return HRTIMER_NORESTART; |
| 44 | |
| 45 | restart_timer = __kvm_timer_fn(vcpu, ktimer); |
| 46 | if (restart_timer) |
| 47 | return HRTIMER_RESTART; |
| 48 | else |
| 49 | return HRTIMER_NORESTART; |
| 50 | } |
| 51 | |