blob: 6b85cc647f346fed962e4bb5b870fdefc2cbd8cf [file] [log] [blame]
Avi Kivity221d0592010-05-23 18:37:00 +03001/*
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 *
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity221d0592010-05-23 18:37:00 +030010 *
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
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030015#include <linux/kvm_host.h>
16#include <linux/kvm.h>
17#include <linux/hrtimer.h>
Arun Sharma600634972011-07-26 16:09:06 -070018#include <linux/atomic.h>
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030019#include "kvm_timer.h"
20
Jan Kiszka3f2e5262011-09-14 09:58:32 +020021enum hrtimer_restart kvm_timer_fn(struct hrtimer *data)
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030022{
Jan Kiszka3f2e5262011-09-14 09:58:32 +020023 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
24 struct kvm_vcpu *vcpu = ktimer->vcpu;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030025 wait_queue_head_t *q = &vcpu->wq;
26
Jan Kiszkaf7104db2009-06-09 15:37:01 +020027 /*
28 * There is a race window between reading and incrementing, but we do
Lucas De Marchi0d2eb442011-03-17 16:24:16 -030029 * not care about potentially losing timer events in the !reinject
Marcelo Tosatti041b1352010-03-23 14:15:53 -030030 * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked
31 * in vcpu_enter_guest.
Jan Kiszkaf7104db2009-06-09 15:37:01 +020032 */
33 if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
Jan Kiszka681405b2009-06-09 15:37:03 +020034 atomic_inc(&ktimer->pending);
Jan Kiszkaf7104db2009-06-09 15:37:01 +020035 /* FIXME: this code should not know anything about vcpus */
Avi Kivitya8eeb042010-05-10 12:34:53 +030036 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Jan Kiszkaf7104db2009-06-09 15:37:01 +020037 }
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030038
39 if (waitqueue_active(q))
40 wake_up_interruptible(q);
41
42 if (ktimer->t_ops->is_periodic(ktimer)) {
43 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030044 return HRTIMER_RESTART;
Jan Kiszka3f2e5262011-09-14 09:58:32 +020045 } else
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030046 return HRTIMER_NORESTART;
47}