2013-01-24 02:21:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 ARM Ltd.
|
|
|
|
* Author: Marc Zyngier <marc.zyngier@arm.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ASM_ARM_KVM_ARCH_TIMER_H
|
|
|
|
#define __ASM_ARM_KVM_ARCH_TIMER_H
|
|
|
|
|
|
|
|
#include <linux/clocksource.h>
|
|
|
|
#include <linux/hrtimer.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
|
2017-02-03 23:19:59 +08:00
|
|
|
struct arch_timer_context {
|
2013-01-24 02:21:58 +08:00
|
|
|
/* Registers: control register, timer value */
|
2017-02-03 23:19:59 +08:00
|
|
|
u32 cnt_ctl;
|
|
|
|
u64 cnt_cval;
|
|
|
|
|
|
|
|
/* Timer IRQ */
|
|
|
|
struct kvm_irq_level irq;
|
|
|
|
|
|
|
|
/* Active IRQ state caching */
|
|
|
|
bool active_cleared_last;
|
2017-02-03 23:20:00 +08:00
|
|
|
|
|
|
|
/* Virtual offset */
|
|
|
|
u64 cntvoff;
|
2017-02-03 23:19:59 +08:00
|
|
|
};
|
2013-01-24 02:21:58 +08:00
|
|
|
|
2017-02-03 23:19:59 +08:00
|
|
|
struct arch_timer_cpu {
|
|
|
|
struct arch_timer_context vtimer;
|
2017-02-03 23:20:02 +08:00
|
|
|
struct arch_timer_context ptimer;
|
2013-01-24 02:21:58 +08:00
|
|
|
|
|
|
|
/* Background timer used when the guest is not running */
|
|
|
|
struct hrtimer timer;
|
|
|
|
|
|
|
|
/* Work queued with the above timer expires */
|
|
|
|
struct work_struct expired;
|
|
|
|
|
|
|
|
/* Background timer active */
|
|
|
|
bool armed;
|
|
|
|
|
2016-05-18 23:26:00 +08:00
|
|
|
/* Is the timer enabled */
|
|
|
|
bool enabled;
|
2013-01-24 02:21:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int kvm_timer_hyp_init(void);
|
2016-05-18 23:26:00 +08:00
|
|
|
int kvm_timer_enable(struct kvm_vcpu *vcpu);
|
2014-06-23 20:59:13 +08:00
|
|
|
int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
|
2017-02-03 23:20:03 +08:00
|
|
|
const struct kvm_irq_level *virt_irq,
|
|
|
|
const struct kvm_irq_level *phys_irq);
|
2013-01-24 02:21:58 +08:00
|
|
|
void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu);
|
2014-07-04 22:54:14 +08:00
|
|
|
|
|
|
|
u64 kvm_arm_timer_get_reg(struct kvm_vcpu *, u64 regid);
|
|
|
|
int kvm_arm_timer_set_reg(struct kvm_vcpu *, u64 regid, u64 value);
|
|
|
|
|
2017-02-03 23:20:01 +08:00
|
|
|
bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
|
arm/arm64: KVM: arch_timer: Only schedule soft timer on vcpu_block
We currently schedule a soft timer every time we exit the guest if the
timer did not expire while running the guest. This is really not
necessary, because the only work we do in the timer work function is to
kick the vcpu.
Kicking the vcpu does two things:
(1) If the vpcu thread is on a waitqueue, make it runnable and remove it
from the waitqueue.
(2) If the vcpu is running on a different physical CPU from the one
doing the kick, it sends a reschedule IPI.
The second case cannot happen, because the soft timer is only ever
scheduled when the vcpu is not running. The first case is only relevant
when the vcpu thread is on a waitqueue, which is only the case when the
vcpu thread has called kvm_vcpu_block().
Therefore, we only need to make sure a timer is scheduled for
kvm_vcpu_block(), which we do by encapsulating all calls to
kvm_vcpu_block() with kvm_timer_{un}schedule calls.
Additionally, we only schedule a soft timer if the timer is enabled and
unmasked, since it is useless otherwise.
Note that theoretically userspace can use the SET_ONE_REG interface to
change registers that should cause the timer to fire, even if the vcpu
is blocked without a scheduled timer, but this case was not supported
before this patch and we leave it for future work for now.
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
2015-08-26 01:48:21 +08:00
|
|
|
void kvm_timer_schedule(struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_timer_unschedule(struct kvm_vcpu *vcpu);
|
arm/arm64: KVM: Fix migration race in the arch timer
When a VCPU is no longer running, we currently check to see if it has a
timer scheduled in the future, and if it does, we schedule a host
hrtimer to notify is in case the timer expires while the VCPU is still
not running. When the hrtimer fires, we mask the guest's timer and
inject the timer IRQ (still relying on the guest unmasking the time when
it receives the IRQ).
This is all good and fine, but when migration a VM (checkpoint/restore)
this introduces a race. It is unlikely, but possible, for the following
sequence of events to happen:
1. Userspace stops the VM
2. Hrtimer for VCPU is scheduled
3. Userspace checkpoints the VGIC state (no pending timer interrupts)
4. The hrtimer fires, schedules work in a workqueue
5. Workqueue function runs, masks the timer and injects timer interrupt
6. Userspace checkpoints the timer state (timer masked)
At restore time, you end up with a masked timer without any timer
interrupts and your guest halts never receiving timer interrupts.
Fix this by only kicking the VCPU in the workqueue function, and sample
the expired state of the timer when entering the guest again and inject
the interrupt and mask the timer only then.
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
2015-03-14 01:02:55 +08:00
|
|
|
|
2017-02-03 23:20:08 +08:00
|
|
|
u64 kvm_phys_timer_read(void);
|
|
|
|
|
2016-01-30 03:04:48 +08:00
|
|
|
void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu);
|
|
|
|
|
2016-12-02 03:32:05 +08:00
|
|
|
void kvm_timer_init_vhe(void);
|
2017-02-03 23:19:59 +08:00
|
|
|
|
|
|
|
#define vcpu_vtimer(v) (&(v)->arch.timer_cpu.vtimer)
|
2017-02-03 23:20:02 +08:00
|
|
|
#define vcpu_ptimer(v) (&(v)->arch.timer_cpu.ptimer)
|
2013-01-24 02:21:58 +08:00
|
|
|
#endif
|