Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 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 | * |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 9 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 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 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 15 | #include <linux/kvm_host.h> |
| 16 | #include <linux/kvm.h> |
| 17 | #include <linux/hrtimer.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 18 | #include <linux/atomic.h> |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 19 | #include "kvm_timer.h" |
| 20 | |
Jan Kiszka | 3f2e526 | 2011-09-14 09:58:32 +0200 | [diff] [blame] | 21 | enum hrtimer_restart kvm_timer_fn(struct hrtimer *data) |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 22 | { |
Jan Kiszka | 3f2e526 | 2011-09-14 09:58:32 +0200 | [diff] [blame] | 23 | struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); |
| 24 | struct kvm_vcpu *vcpu = ktimer->vcpu; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 25 | wait_queue_head_t *q = &vcpu->wq; |
| 26 | |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 27 | /* |
| 28 | * There is a race window between reading and incrementing, but we do |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 29 | * not care about potentially losing timer events in the !reinject |
Marcelo Tosatti | 041b135 | 2010-03-23 14:15:53 -0300 | [diff] [blame] | 30 | * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked |
| 31 | * in vcpu_enter_guest. |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 32 | */ |
| 33 | if (ktimer->reinject || !atomic_read(&ktimer->pending)) { |
Jan Kiszka | 681405b | 2009-06-09 15:37:03 +0200 | [diff] [blame] | 34 | atomic_inc(&ktimer->pending); |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 35 | /* FIXME: this code should not know anything about vcpus */ |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 36 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
Jan Kiszka | f7104db | 2009-06-09 15:37:01 +0200 | [diff] [blame] | 37 | } |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 38 | |
| 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 Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 44 | return HRTIMER_RESTART; |
Jan Kiszka | 3f2e526 | 2011-09-14 09:58:32 +0200 | [diff] [blame] | 45 | } else |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 46 | return HRTIMER_NORESTART; |
| 47 | } |