Commit | Line | Data |
---|---|---|
221d059d AK |
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 | * | |
9611c187 | 9 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
221d059d AK |
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 | ||
d3c7b77d MT |
15 | #include <linux/kvm_host.h> |
16 | #include <linux/kvm.h> | |
17 | #include <linux/hrtimer.h> | |
18 | #include <asm/atomic.h> | |
19 | #include "kvm_timer.h" | |
20 | ||
21 | static int __kvm_timer_fn(struct kvm_vcpu *vcpu, struct kvm_timer *ktimer) | |
22 | { | |
23 | int restart_timer = 0; | |
24 | wait_queue_head_t *q = &vcpu->wq; | |
25 | ||
f7104db2 JK |
26 | /* |
27 | * There is a race window between reading and incrementing, but we do | |
28 | * not care about potentially loosing timer events in the !reinject | |
041b1359 MT |
29 | * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked |
30 | * in vcpu_enter_guest. | |
f7104db2 JK |
31 | */ |
32 | if (ktimer->reinject || !atomic_read(&ktimer->pending)) { | |
681405bf | 33 | atomic_inc(&ktimer->pending); |
f7104db2 | 34 | /* FIXME: this code should not know anything about vcpus */ |
a8eeb04a | 35 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
f7104db2 | 36 | } |
d3c7b77d MT |
37 | |
38 | if (waitqueue_active(q)) | |
39 | wake_up_interruptible(q); | |
40 | ||
41 | if (ktimer->t_ops->is_periodic(ktimer)) { | |
42 | hrtimer_add_expires_ns(&ktimer->timer, ktimer->period); | |
43 | restart_timer = 1; | |
44 | } | |
45 | ||
46 | return restart_timer; | |
47 | } | |
48 | ||
49 | enum hrtimer_restart kvm_timer_fn(struct hrtimer *data) | |
50 | { | |
51 | int restart_timer; | |
52 | struct kvm_vcpu *vcpu; | |
53 | struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); | |
54 | ||
1ed0ce00 | 55 | vcpu = ktimer->vcpu; |
d3c7b77d MT |
56 | if (!vcpu) |
57 | return HRTIMER_NORESTART; | |
58 | ||
59 | restart_timer = __kvm_timer_fn(vcpu, ktimer); | |
60 | if (restart_timer) | |
61 | return HRTIMER_RESTART; | |
62 | else | |
63 | return HRTIMER_NORESTART; | |
64 | } | |
65 |