blob: 3d008de9cb05e900749d3f83734f6d22ef871c8f [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030014 * Amit Shah <amit.shah@qumranet.com>
15 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020016 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 */
21
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020023#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080024#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080025#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020026#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030027#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030028#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020029#include "cpuid.h"
Paolo Bonzinic9eab582014-11-24 15:27:17 +010030#include "assigned-dev.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020031#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030032#include "hyperv.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020033
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020034#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030035#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020036#include <linux/kvm.h>
37#include <linux/fs.h>
38#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010039#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080040#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050041#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010042#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030043#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010044#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030045#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020046#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080048#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080049#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020050#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020051#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020052#include <linux/timekeeper_internal.h>
53#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080054#include <linux/kvm_irqfd.h>
55#include <linux/irqbypass.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030056#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#define CREATE_TRACE_POINTS
59#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020060
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020061#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080062#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020063#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080064#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020065#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020066#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100067#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030068#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080069#include <asm/irq_remapping.h>
Carsten Otte043405e2007-10-10 17:16:19 +020070
Carsten Otte313a3dc2007-10-11 19:16:52 +020071#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080072#define KVM_MAX_MCE_BANKS 32
Huang Ying5854dbc2010-10-08 16:24:14 +080073#define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
Huang Ying890ca9a2009-05-11 16:48:15 +080074
Avi Kivity0f65dd72011-04-20 13:37:53 +030075#define emul_to_vcpu(ctxt) \
76 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
77
Joerg Roedel50a37eb2008-01-31 14:57:38 +010078/* EFER defaults:
79 * - enable syscall per default because its emulated by KVM
80 * - enable LME and LMA per default on 64 bit KVM
81 */
82#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080083static
84u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010085#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080086static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010087#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020088
Avi Kivityba1389b2007-11-18 16:24:12 +020089#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
90#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050091
Gleb Natapovcb142eb2009-08-09 15:17:40 +030092static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +030093static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010094static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Avi Kivity674eea02008-02-11 18:37:23 +020095
Paolo Bonzini893590c2015-11-06 11:46:24 +010096struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030097EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080098
Paolo Bonzini893590c2015-11-06 11:46:24 +010099static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030100module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200101
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200102unsigned int min_timer_period_us = 500;
103module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
104
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300105static bool __read_mostly kvmclock_periodic_sync = true;
106module_param(kvmclock_periodic_sync, bool, S_IRUGO);
107
Paolo Bonzini893590c2015-11-06 11:46:24 +0100108bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100109EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100110u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100111EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800112u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
113EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
114u64 __read_mostly kvm_max_tsc_scaling_ratio;
115EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Haozhong Zhangad721882015-10-20 15:39:02 +0800116static u64 __read_mostly kvm_default_tsc_scaling_ratio;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100117
Zachary Amsdencc578282012-02-03 15:43:50 -0200118/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100119static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200120module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
121
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500122/* lapic timer advance (tscdeadline mode only) in nanoseconds */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100123unsigned int __read_mostly lapic_timer_advance_ns = 0;
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500124module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
125
Paolo Bonzini893590c2015-11-06 11:46:24 +0100126static bool __read_mostly backwards_tsc_observed = false;
Marcelo Tosatti16a96022014-05-14 12:43:24 -0300127
Avi Kivity18863bd2009-09-07 11:12:18 +0300128#define KVM_NR_SHARED_MSRS 16
129
130struct kvm_shared_msrs_global {
131 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800132 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300133};
134
135struct kvm_shared_msrs {
136 struct user_return_notifier urn;
137 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800138 struct kvm_shared_msr_values {
139 u64 host;
140 u64 curr;
141 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300142};
143
144static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200145static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300146
Hollis Blanchard417bc302007-10-31 17:24:23 -0500147struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200148 { "pf_fixed", VCPU_STAT(pf_fixed) },
149 { "pf_guest", VCPU_STAT(pf_guest) },
150 { "tlb_flush", VCPU_STAT(tlb_flush) },
151 { "invlpg", VCPU_STAT(invlpg) },
152 { "exits", VCPU_STAT(exits) },
153 { "io_exits", VCPU_STAT(io_exits) },
154 { "mmio_exits", VCPU_STAT(mmio_exits) },
155 { "signal_exits", VCPU_STAT(signal_exits) },
156 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800157 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200158 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100159 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200160 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200161 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530162 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200163 { "request_irq", VCPU_STAT(request_irq_exits) },
164 { "irq_exits", VCPU_STAT(irq_exits) },
165 { "host_state_reload", VCPU_STAT(host_state_reload) },
166 { "efer_reload", VCPU_STAT(efer_reload) },
167 { "fpu_reload", VCPU_STAT(fpu_reload) },
168 { "insn_emulation", VCPU_STAT(insn_emulation) },
169 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300170 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200171 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200172 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
173 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
174 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
175 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
176 { "mmu_flooded", VM_STAT(mmu_flooded) },
177 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200178 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300179 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200180 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300181 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500182 { NULL }
183};
184
Dexuan Cui2acf9232010-06-10 11:27:12 +0800185u64 __read_mostly host_xcr0;
186
Jan Kiszkab6785de2012-09-20 07:43:17 +0200187static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300188
Gleb Natapovaf585b92010-10-14 11:22:46 +0200189static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
190{
191 int i;
192 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
193 vcpu->arch.apf.gfns[i] = ~0;
194}
195
Avi Kivity18863bd2009-09-07 11:12:18 +0300196static void kvm_on_user_return(struct user_return_notifier *urn)
197{
198 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300199 struct kvm_shared_msrs *locals
200 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800201 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300202
203 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800204 values = &locals->values[slot];
205 if (values->host != values->curr) {
206 wrmsrl(shared_msrs_global.msrs[slot], values->host);
207 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300208 }
209 }
210 locals->registered = false;
211 user_return_notifier_unregister(urn);
212}
213
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800214static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300215{
Avi Kivity18863bd2009-09-07 11:12:18 +0300216 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200217 unsigned int cpu = smp_processor_id();
218 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300219
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800220 /* only read, and nobody should modify it at this time,
221 * so don't need lock */
222 if (slot >= shared_msrs_global.nr) {
223 printk(KERN_ERR "kvm: invalid MSR slot!");
224 return;
225 }
226 rdmsrl_safe(msr, &value);
227 smsr->values[slot].host = value;
228 smsr->values[slot].curr = value;
229}
230
231void kvm_define_shared_msr(unsigned slot, u32 msr)
232{
Nadav Amit0123be42014-07-24 15:06:56 +0300233 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200234 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300235 if (slot >= shared_msrs_global.nr)
236 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300237}
238EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
239
240static void kvm_shared_msr_cpu_online(void)
241{
242 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300243
244 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800245 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300246}
247
Andy Honig8b3c3102014-08-27 11:16:44 -0700248int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300249{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200250 unsigned int cpu = smp_processor_id();
251 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700252 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300253
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800254 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Andy Honig8b3c3102014-08-27 11:16:44 -0700255 return 0;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800256 smsr->values[slot].curr = value;
Andy Honig8b3c3102014-08-27 11:16:44 -0700257 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
258 if (err)
259 return 1;
260
Avi Kivity18863bd2009-09-07 11:12:18 +0300261 if (!smsr->registered) {
262 smsr->urn.on_user_return = kvm_on_user_return;
263 user_return_notifier_register(&smsr->urn);
264 smsr->registered = true;
265 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700266 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300267}
268EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
269
Radim Krčmář13a34e02014-08-28 15:13:03 +0200270static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200271{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200272 unsigned int cpu = smp_processor_id();
273 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200274
275 if (smsr->registered)
276 kvm_on_user_return(&smsr->urn);
277}
278
Carsten Otte6866b832007-10-29 16:09:10 +0100279u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
280{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300281 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100282}
283EXPORT_SYMBOL_GPL(kvm_get_apic_base);
284
Jan Kiszka58cb6282014-01-24 16:48:44 +0100285int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100286{
Jan Kiszka58cb6282014-01-24 16:48:44 +0100287 u64 old_state = vcpu->arch.apic_base &
288 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
289 u64 new_state = msr_info->data &
290 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
291 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
292 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
293
294 if (!msr_info->host_initiated &&
295 ((msr_info->data & reserved_bits) != 0 ||
296 new_state == X2APIC_ENABLE ||
297 (new_state == MSR_IA32_APICBASE_ENABLE &&
298 old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
299 (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
300 old_state == 0)))
301 return 1;
302
303 kvm_lapic_set_base(vcpu, msr_info->data);
304 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100305}
306EXPORT_SYMBOL_GPL(kvm_set_apic_base);
307
Andi Kleen2605fc22014-05-02 00:44:37 +0200308asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000309{
310 /* Fault while not rebooting. We want the trace. */
311 BUG();
312}
313EXPORT_SYMBOL_GPL(kvm_spurious_fault);
314
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200315#define EXCPT_BENIGN 0
316#define EXCPT_CONTRIBUTORY 1
317#define EXCPT_PF 2
318
319static int exception_class(int vector)
320{
321 switch (vector) {
322 case PF_VECTOR:
323 return EXCPT_PF;
324 case DE_VECTOR:
325 case TS_VECTOR:
326 case NP_VECTOR:
327 case SS_VECTOR:
328 case GP_VECTOR:
329 return EXCPT_CONTRIBUTORY;
330 default:
331 break;
332 }
333 return EXCPT_BENIGN;
334}
335
Nadav Amitd6e8c852014-07-24 14:51:24 +0300336#define EXCPT_FAULT 0
337#define EXCPT_TRAP 1
338#define EXCPT_ABORT 2
339#define EXCPT_INTERRUPT 3
340
341static int exception_type(int vector)
342{
343 unsigned int mask;
344
345 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
346 return EXCPT_INTERRUPT;
347
348 mask = 1 << vector;
349
350 /* #DB is trap, as instruction watchpoints are handled elsewhere */
351 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
352 return EXCPT_TRAP;
353
354 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
355 return EXCPT_ABORT;
356
357 /* Reserved exceptions will result in fault */
358 return EXCPT_FAULT;
359}
360
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200361static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200362 unsigned nr, bool has_error, u32 error_code,
363 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200364{
365 u32 prev_nr;
366 int class1, class2;
367
Avi Kivity3842d132010-07-27 12:30:24 +0300368 kvm_make_request(KVM_REQ_EVENT, vcpu);
369
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200370 if (!vcpu->arch.exception.pending) {
371 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200372 if (has_error && !is_protmode(vcpu))
373 has_error = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200374 vcpu->arch.exception.pending = true;
375 vcpu->arch.exception.has_error_code = has_error;
376 vcpu->arch.exception.nr = nr;
377 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200378 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200379 return;
380 }
381
382 /* to check exception */
383 prev_nr = vcpu->arch.exception.nr;
384 if (prev_nr == DF_VECTOR) {
385 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300386 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200387 return;
388 }
389 class1 = exception_class(prev_nr);
390 class2 = exception_class(nr);
391 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
392 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
393 /* generate double fault per SDM Table 5-5 */
394 vcpu->arch.exception.pending = true;
395 vcpu->arch.exception.has_error_code = true;
396 vcpu->arch.exception.nr = DF_VECTOR;
397 vcpu->arch.exception.error_code = 0;
398 } else
399 /* replace previous exception with a new one in a hope
400 that instruction re-execution will regenerate lost
401 exception */
402 goto queue;
403}
404
Avi Kivity298101d2007-11-25 13:41:11 +0200405void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
406{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200407 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200408}
409EXPORT_SYMBOL_GPL(kvm_queue_exception);
410
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200411void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
412{
413 kvm_multiple_exception(vcpu, nr, false, 0, true);
414}
415EXPORT_SYMBOL_GPL(kvm_requeue_exception);
416
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100417void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200418{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100419 if (err)
420 kvm_inject_gp(vcpu, 0);
421 else
422 kvm_x86_ops->skip_emulated_instruction(vcpu);
423}
424EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200425
Avi Kivity6389ee92010-11-29 16:12:30 +0200426void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200427{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200428 ++vcpu->stat.pf_guest;
Avi Kivity6389ee92010-11-29 16:12:30 +0200429 vcpu->arch.cr2 = fault->address;
430 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200431}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300432EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200433
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200434static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200435{
Avi Kivity6389ee92010-11-29 16:12:30 +0200436 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
437 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200438 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200439 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200440
441 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200442}
443
Sheng Yang3419ffc2008-05-15 09:52:48 +0800444void kvm_inject_nmi(struct kvm_vcpu *vcpu)
445{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300446 atomic_inc(&vcpu->arch.nmi_queued);
447 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800448}
449EXPORT_SYMBOL_GPL(kvm_inject_nmi);
450
Avi Kivity298101d2007-11-25 13:41:11 +0200451void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
452{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200453 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200454}
455EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
456
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200457void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
458{
459 kvm_multiple_exception(vcpu, nr, true, error_code, true);
460}
461EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
462
Carsten Ottea03490e2007-10-29 16:09:35 +0100463/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300464 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
465 * a #GP and return false.
466 */
467bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200468{
Avi Kivity0a79b002009-09-01 12:03:25 +0300469 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
470 return true;
471 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
472 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100473}
Avi Kivity0a79b002009-09-01 12:03:25 +0300474EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100475
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300476bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
477{
478 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
479 return true;
480
481 kvm_queue_exception(vcpu, UD_VECTOR);
482 return false;
483}
484EXPORT_SYMBOL_GPL(kvm_require_dr);
485
Carsten Ottea03490e2007-10-29 16:09:35 +0100486/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200487 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200488 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200489 * can read from guest physical or from the guest's guest physical memory.
490 */
491int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
492 gfn_t ngfn, void *data, int offset, int len,
493 u32 access)
494{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200495 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200496 gfn_t real_gfn;
497 gpa_t ngpa;
498
499 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200500 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200501 if (real_gfn == UNMAPPED_GVA)
502 return -EFAULT;
503
504 real_gfn = gpa_to_gfn(real_gfn);
505
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200506 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200507}
508EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
509
Fengguang Wu69b00492015-01-19 22:33:39 +0800510static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200511 void *data, int offset, int len, u32 access)
512{
513 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
514 data, offset, len, access);
515}
516
Joerg Roedelec92fe42010-09-10 17:30:51 +0200517/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100518 * Load the pae pdptrs. Return true is they are all valid.
519 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200520int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100521{
522 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
523 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
524 int i;
525 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200526 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100527
Joerg Roedelff03a072010-09-10 17:30:57 +0200528 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
529 offset * sizeof(u64), sizeof(pdpte),
530 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100531 if (ret < 0) {
532 ret = 0;
533 goto out;
534 }
535 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300536 if (is_present_gpte(pdpte[i]) &&
Xiao Guangronga0a64f52015-08-05 12:04:21 +0800537 (pdpte[i] &
538 vcpu->arch.mmu.guest_rsvd_check.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100539 ret = 0;
540 goto out;
541 }
542 }
543 ret = 1;
544
Joerg Roedelff03a072010-09-10 17:30:57 +0200545 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300546 __set_bit(VCPU_EXREG_PDPTR,
547 (unsigned long *)&vcpu->arch.regs_avail);
548 __set_bit(VCPU_EXREG_PDPTR,
549 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100550out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100551
552 return ret;
553}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100554EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100555
Avi Kivityd835dfe2007-11-21 02:57:59 +0200556static bool pdptrs_changed(struct kvm_vcpu *vcpu)
557{
Joerg Roedelff03a072010-09-10 17:30:57 +0200558 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200559 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200560 int offset;
561 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200562 int r;
563
564 if (is_long_mode(vcpu) || !is_pae(vcpu))
565 return false;
566
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300567 if (!test_bit(VCPU_EXREG_PDPTR,
568 (unsigned long *)&vcpu->arch.regs_avail))
569 return true;
570
Avi Kivity9f8fe502010-12-05 17:30:00 +0200571 gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
572 offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200573 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
574 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200575 if (r < 0)
576 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200577 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200578out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200579
580 return changed;
581}
582
Avi Kivity49a9b072010-06-10 17:02:14 +0300583int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100584{
Sheng Yangaad82702010-05-12 16:40:42 +0800585 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800586 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800587
Avi Kivityf9a48e62010-01-06 19:10:22 +0200588 cr0 |= X86_CR0_ET;
589
Gleb Natapovab344822010-01-21 15:28:46 +0200590#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300591 if (cr0 & 0xffffffff00000000UL)
592 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200593#endif
594
595 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100596
Gleb Natapov0f122442010-04-28 19:15:31 +0300597 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
598 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100599
Gleb Natapov0f122442010-04-28 19:15:31 +0300600 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
601 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100602
603 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
604#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200605 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100606 int cs_db, cs_l;
607
Gleb Natapov0f122442010-04-28 19:15:31 +0300608 if (!is_pae(vcpu))
609 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100610 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300611 if (cs_l)
612 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100613 } else
614#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200615 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200616 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300617 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100618 }
619
Mao, Junjiead756a12012-07-02 01:18:48 +0000620 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
621 return 1;
622
Carsten Ottea03490e2007-10-29 16:09:35 +0100623 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100624
Lai Jiangshand170c412011-02-21 11:21:30 +0800625 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800626 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800627 kvm_async_pf_hash_reset(vcpu);
628 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800629
Sheng Yangaad82702010-05-12 16:40:42 +0800630 if ((cr0 ^ old_cr0) & update_bits)
631 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800632
Laszlo Ersek879ae182015-11-04 12:54:41 +0100633 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
634 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
635 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800636 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
637
Gleb Natapov0f122442010-04-28 19:15:31 +0300638 return 0;
639}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200640EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100641
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200642void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100643{
Avi Kivity49a9b072010-06-10 17:02:14 +0300644 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100645}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200646EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100647
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300648static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
649{
650 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
651 !vcpu->guest_xcr0_loaded) {
652 /* kvm_set_xcr() also depends on this */
653 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
654 vcpu->guest_xcr0_loaded = 1;
655 }
656}
657
658static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
659{
660 if (vcpu->guest_xcr0_loaded) {
661 if (vcpu->arch.xcr0 != host_xcr0)
662 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
663 vcpu->guest_xcr0_loaded = 0;
664 }
665}
666
Fengguang Wu69b00492015-01-19 22:33:39 +0800667static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800668{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000669 u64 xcr0 = xcr;
670 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200671 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800672
673 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
674 if (index != XCR_XFEATURE_ENABLED_MASK)
675 return 1;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800676 if (!(xcr0 & XSTATE_FP))
677 return 1;
678 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
679 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200680
681 /*
682 * Do not allow the guest to set bits that we do not support
683 * saving. However, xcr0 bit 0 is always set, even if the
684 * emulated CPU does not support XSAVE (see fx_init).
685 */
686 valid_bits = vcpu->arch.guest_supported_xcr0 | XSTATE_FP;
687 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800688 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200689
Liu, Jinsong390bd522014-02-24 10:58:09 +0000690 if ((!(xcr0 & XSTATE_BNDREGS)) != (!(xcr0 & XSTATE_BNDCSR)))
691 return 1;
692
Chao Peng612263b2014-10-22 17:35:24 +0800693 if (xcr0 & XSTATE_AVX512) {
694 if (!(xcr0 & XSTATE_YMM))
695 return 1;
696 if ((xcr0 & XSTATE_AVX512) != XSTATE_AVX512)
697 return 1;
698 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300699 kvm_put_guest_xcr0(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800700 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000701
702 if ((xcr0 ^ old_xcr0) & XSTATE_EXTEND_MASK)
703 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800704 return 0;
705}
706
707int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
708{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000709 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
710 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800711 kvm_inject_gp(vcpu, 0);
712 return 1;
713 }
714 return 0;
715}
716EXPORT_SYMBOL_GPL(kvm_set_xcr);
717
Avi Kivitya83b29c2010-06-10 17:02:15 +0300718int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100719{
Avi Kivityfc78f512009-12-07 12:16:48 +0200720 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800721 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
722 X86_CR4_SMEP | X86_CR4_SMAP;
723
Gleb Natapov0f122442010-04-28 19:15:31 +0300724 if (cr4 & CR4_RESERVED_BITS)
725 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100726
Dexuan Cui2acf9232010-06-10 11:27:12 +0800727 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
728 return 1;
729
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800730 if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
731 return 1;
732
Feng Wu97ec8c02014-04-01 17:46:34 +0800733 if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
734 return 1;
735
H. Peter Anvinafcbf132013-04-27 16:37:47 -0700736 if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800737 return 1;
738
Carsten Ottea03490e2007-10-29 16:09:35 +0100739 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300740 if (!(cr4 & X86_CR4_PAE))
741 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300742 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
743 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200744 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
745 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300746 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100747
Mao, Junjiead756a12012-07-02 01:18:48 +0000748 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
749 if (!guest_cpuid_has_pcid(vcpu))
750 return 1;
751
752 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
753 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
754 return 1;
755 }
756
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300757 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300758 return 1;
759
Mao, Junjiead756a12012-07-02 01:18:48 +0000760 if (((cr4 ^ old_cr4) & pdptr_bits) ||
761 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800762 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300763
Dexuan Cui2acf9232010-06-10 11:27:12 +0800764 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +0200765 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800766
Gleb Natapov0f122442010-04-28 19:15:31 +0300767 return 0;
768}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200769EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100770
Avi Kivity23902182010-06-10 17:02:16 +0300771int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100772{
Paolo Bonziniac146232014-11-10 13:53:25 +0100773#ifdef CONFIG_X86_64
Nadav Amit9d88fca2014-11-02 11:54:52 +0200774 cr3 &= ~CR3_PCID_INVD;
Paolo Bonziniac146232014-11-10 13:53:25 +0100775#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200776
Avi Kivity9f8fe502010-12-05 17:30:00 +0200777 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300778 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -0400779 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300780 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200781 }
782
Carsten Ottea03490e2007-10-29 16:09:35 +0100783 if (is_long_mode(vcpu)) {
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200784 if (cr3 & CR3_L_MODE_RESERVED_BITS)
785 return 1;
786 } else if (is_pae(vcpu) && is_paging(vcpu) &&
787 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300788 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100789
Gleb Natapov0f122442010-04-28 19:15:31 +0300790 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200791 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Paolo Bonzinid8d173d2013-10-02 16:56:11 +0200792 kvm_mmu_new_cr3(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300793 return 0;
794}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200795EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100796
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100797int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100798{
Gleb Natapov0f122442010-04-28 19:15:31 +0300799 if (cr8 & CR8_RESERVED_BITS)
800 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200801 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100802 kvm_lapic_set_tpr(vcpu, cr8);
803 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800804 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300805 return 0;
806}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200807EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100808
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200809unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100810{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200811 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100812 return kvm_lapic_get_cr8(vcpu);
813 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800814 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100815}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200816EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100817
Nadav Amitae561ed2015-04-02 03:10:37 +0300818static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
819{
820 int i;
821
822 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
823 for (i = 0; i < KVM_NR_DB_REGS; i++)
824 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
825 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
826 }
827}
828
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100829static void kvm_update_dr6(struct kvm_vcpu *vcpu)
830{
831 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
832 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
833}
834
Jan Kiszkac8639012012-09-21 05:42:55 +0200835static void kvm_update_dr7(struct kvm_vcpu *vcpu)
836{
837 unsigned long dr7;
838
839 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
840 dr7 = vcpu->arch.guest_debug_dr7;
841 else
842 dr7 = vcpu->arch.dr7;
843 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100844 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
845 if (dr7 & DR7_BP_EN_MASK)
846 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200847}
848
Nadav Amit6f43ed02014-07-15 17:37:46 +0300849static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
850{
851 u64 fixed = DR6_FIXED_1;
852
853 if (!guest_cpuid_has_rtm(vcpu))
854 fixed |= DR6_RTM;
855 return fixed;
856}
857
Gleb Natapov338dbc92010-04-28 19:15:32 +0300858static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300859{
860 switch (dr) {
861 case 0 ... 3:
862 vcpu->arch.db[dr] = val;
863 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
864 vcpu->arch.eff_db[dr] = val;
865 break;
866 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300867 /* fall through */
868 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300869 if (val & 0xffffffff00000000ULL)
870 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300871 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100872 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300873 break;
874 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300875 /* fall through */
876 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300877 if (val & 0xffffffff00000000ULL)
878 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300879 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200880 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300881 break;
882 }
883
884 return 0;
885}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300886
887int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
888{
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300889 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +0300890 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300891 return 1;
892 }
893 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +0300894}
Gleb Natapov020df072010-04-13 10:05:23 +0300895EXPORT_SYMBOL_GPL(kvm_set_dr);
896
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300897int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300898{
899 switch (dr) {
900 case 0 ... 3:
901 *val = vcpu->arch.db[dr];
902 break;
903 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300904 /* fall through */
905 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100906 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
907 *val = vcpu->arch.dr6;
908 else
909 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300910 break;
911 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300912 /* fall through */
913 default: /* 7 */
914 *val = vcpu->arch.dr7;
915 break;
916 }
Gleb Natapov338dbc92010-04-28 19:15:32 +0300917 return 0;
918}
Gleb Natapov020df072010-04-13 10:05:23 +0300919EXPORT_SYMBOL_GPL(kvm_get_dr);
920
Avi Kivity022cd0e2011-11-10 14:57:23 +0200921bool kvm_rdpmc(struct kvm_vcpu *vcpu)
922{
923 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
924 u64 data;
925 int err;
926
Wei Huangc6702c92015-06-19 13:44:45 +0200927 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +0200928 if (err)
929 return err;
930 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
931 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
932 return err;
933}
934EXPORT_SYMBOL_GPL(kvm_rdpmc);
935
Carsten Otte043405e2007-10-10 17:16:19 +0200936/*
937 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
938 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
939 *
940 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400941 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200942 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
943 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +0200944 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400945
Carsten Otte043405e2007-10-10 17:16:19 +0200946static u32 msrs_to_save[] = {
947 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400948 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200949#ifdef CONFIG_X86_64
950 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
951#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800952 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Liu, Jinsong0dd376e2014-02-24 10:56:53 +0000953 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS
Carsten Otte043405e2007-10-10 17:16:19 +0200954};
955
956static unsigned num_msrs_to_save;
957
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200958static u32 emulated_msrs[] = {
959 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
960 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
961 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
962 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Andrey Smetanine7d95132015-07-03 15:01:37 +0300963 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
964 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300965 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +0300966 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300967 HV_X64_MSR_VP_RUNTIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200968 HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
969 MSR_KVM_PV_EOI_EN,
970
Will Auldba904632012-11-29 12:42:50 -0800971 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800972 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +0200973 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300974 MSR_IA32_MCG_STATUS,
975 MSR_IA32_MCG_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +0200976 MSR_IA32_SMBASE,
Carsten Otte043405e2007-10-10 17:16:19 +0200977};
978
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200979static unsigned num_emulated_msrs;
980
Jan Kiszka384bb782013-04-20 10:52:36 +0200981bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100982{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200983 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +0200984 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +0100985
Alexander Graf1b2fd702009-02-02 16:23:51 +0100986 if (efer & EFER_FFXSR) {
987 struct kvm_cpuid_entry2 *feat;
988
989 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200990 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200991 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +0100992 }
993
Alexander Grafd8017472008-11-25 20:17:11 +0100994 if (efer & EFER_SVME) {
995 struct kvm_cpuid_entry2 *feat;
996
997 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200998 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200999 return false;
Alexander Grafd8017472008-11-25 20:17:11 +01001000 }
1001
Jan Kiszka384bb782013-04-20 10:52:36 +02001002 return true;
1003}
1004EXPORT_SYMBOL_GPL(kvm_valid_efer);
1005
1006static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
1007{
1008 u64 old_efer = vcpu->arch.efer;
1009
1010 if (!kvm_valid_efer(vcpu, efer))
1011 return 1;
1012
1013 if (is_paging(vcpu)
1014 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1015 return 1;
1016
Carsten Otte15c4a642007-10-30 18:44:17 +01001017 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001018 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001019
Sheng Yanga3d204e2010-05-12 16:40:40 +08001020 kvm_x86_ops->set_efer(vcpu, efer);
1021
Sheng Yangaad82702010-05-12 16:40:42 +08001022 /* Update reserved bits */
1023 if ((efer ^ old_efer) & EFER_NX)
1024 kvm_mmu_reset_context(vcpu);
1025
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001026 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001027}
1028
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001029void kvm_enable_efer_bits(u64 mask)
1030{
1031 efer_reserved_bits &= ~mask;
1032}
1033EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1034
Carsten Otte15c4a642007-10-30 18:44:17 +01001035/*
1036 * Writes msr value into into the appropriate "register".
1037 * Returns 0 on success, non-0 otherwise.
1038 * Assumes vcpu_load() was already called.
1039 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001040int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01001041{
Nadav Amit854e8bb2014-09-16 03:24:05 +03001042 switch (msr->index) {
1043 case MSR_FS_BASE:
1044 case MSR_GS_BASE:
1045 case MSR_KERNEL_GS_BASE:
1046 case MSR_CSTAR:
1047 case MSR_LSTAR:
1048 if (is_noncanonical_address(msr->data))
1049 return 1;
1050 break;
1051 case MSR_IA32_SYSENTER_EIP:
1052 case MSR_IA32_SYSENTER_ESP:
1053 /*
1054 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1055 * non-canonical address is written on Intel but not on
1056 * AMD (which ignores the top 32-bits, because it does
1057 * not implement 64-bit SYSENTER).
1058 *
1059 * 64-bit code should hence be able to write a non-canonical
1060 * value on AMD. Making the address canonical ensures that
1061 * vmentry does not fail on Intel after writing a non-canonical
1062 * value, and that something deterministic happens if the guest
1063 * invokes 64-bit SYSENTER.
1064 */
1065 msr->data = get_canonical(msr->data);
1066 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001067 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001068}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001069EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001070
Carsten Otte313a3dc2007-10-11 19:16:52 +02001071/*
1072 * Adapt set_msr() to msr_io()'s calling convention
1073 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001074static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1075{
1076 struct msr_data msr;
1077 int r;
1078
1079 msr.index = index;
1080 msr.host_initiated = true;
1081 r = kvm_get_msr(vcpu, &msr);
1082 if (r)
1083 return r;
1084
1085 *data = msr.data;
1086 return 0;
1087}
1088
Carsten Otte313a3dc2007-10-11 19:16:52 +02001089static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1090{
Will Auld8fe8ab42012-11-29 12:42:12 -08001091 struct msr_data msr;
1092
1093 msr.data = *data;
1094 msr.index = index;
1095 msr.host_initiated = true;
1096 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001097}
1098
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001099#ifdef CONFIG_X86_64
1100struct pvclock_gtod_data {
1101 seqcount_t seq;
1102
1103 struct { /* extract of a clocksource struct */
1104 int vclock_mode;
1105 cycle_t cycle_last;
1106 cycle_t mask;
1107 u32 mult;
1108 u32 shift;
1109 } clock;
1110
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001111 u64 boot_ns;
1112 u64 nsec_base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001113};
1114
1115static struct pvclock_gtod_data pvclock_gtod_data;
1116
1117static void update_pvclock_gtod(struct timekeeper *tk)
1118{
1119 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001120 u64 boot_ns;
1121
Peter Zijlstra876e7882015-03-19 10:09:06 +01001122 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001123
1124 write_seqcount_begin(&vdata->seq);
1125
1126 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001127 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1128 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1129 vdata->clock.mask = tk->tkr_mono.mask;
1130 vdata->clock.mult = tk->tkr_mono.mult;
1131 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001132
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001133 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001134 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001135
1136 write_seqcount_end(&vdata->seq);
1137}
1138#endif
1139
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001140void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1141{
1142 /*
1143 * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
1144 * vcpu_enter_guest. This function is only called from
1145 * the physical CPU that is running vcpu.
1146 */
1147 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1148}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001149
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001150static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1151{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001152 int version;
1153 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001154 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +08001155 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001156
1157 if (!wall_clock)
1158 return;
1159
Avi Kivity9ed3c442010-05-04 15:00:37 +03001160 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1161 if (r)
1162 return;
1163
1164 if (version & 1)
1165 ++version; /* first time write, random junk */
1166
1167 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001168
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001169 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
1170
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001171 /*
1172 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001173 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001174 * wall clock specified here. guest system time equals host
1175 * system time for us, thus we must fill in host boot time here.
1176 */
Jason Wang923de3c2010-01-27 19:13:49 +08001177 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001178
Bruce Rogers4b648662012-07-20 10:44:24 -06001179 if (kvm->arch.kvmclock_offset) {
1180 struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
1181 boot = timespec_sub(boot, ts);
1182 }
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001183 wc.sec = boot.tv_sec;
1184 wc.nsec = boot.tv_nsec;
1185 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001186
1187 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1188
1189 version++;
1190 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001191}
1192
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001193static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1194{
1195 uint32_t quotient, remainder;
1196
1197 /* Don't try to replace with do_div(), this one calculates
1198 * "(dividend << 32) / divisor" */
1199 __asm__ ( "divl %4"
1200 : "=a" (quotient), "=d" (remainder)
1201 : "0" (0), "1" (dividend), "r" (divisor) );
1202 return quotient;
1203}
1204
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001205static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
1206 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001207{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001208 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001209 int32_t shift = 0;
1210 uint64_t tps64;
1211 uint32_t tps32;
1212
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001213 tps64 = base_khz * 1000LL;
1214 scaled64 = scaled_khz * 1000LL;
Jan Kiszka50933622010-09-26 13:00:53 +02001215 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001216 tps64 >>= 1;
1217 shift--;
1218 }
1219
1220 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001221 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1222 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001223 scaled64 >>= 1;
1224 else
1225 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001226 shift++;
1227 }
1228
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001229 *pshift = shift;
1230 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001231
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001232 pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
1233 __func__, base_khz, scaled_khz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001234}
1235
Marcelo Tosattid8281992012-11-27 23:29:01 -02001236#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001237static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001238#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001239
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001240static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001241static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001242
Joerg Roedel857e4092011-03-25 09:44:50 +01001243static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Zachary Amsden759379d2010-08-19 22:07:25 -10001244{
Zachary Amsdencc578282012-02-03 15:43:50 -02001245 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
1246 vcpu->arch.virtual_tsc_shift);
Zachary Amsden759379d2010-08-19 22:07:25 -10001247}
1248
Zachary Amsdencc578282012-02-03 15:43:50 -02001249static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001250{
Zachary Amsdencc578282012-02-03 15:43:50 -02001251 u64 v = (u64)khz * (1000000 + ppm);
1252 do_div(v, 1000000);
1253 return v;
1254}
1255
Haozhong Zhang381d5852015-10-20 15:39:04 +08001256static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1257{
1258 u64 ratio;
1259
1260 /* Guest TSC same frequency as host TSC? */
1261 if (!scale) {
1262 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1263 return 0;
1264 }
1265
1266 /* TSC scaling supported? */
1267 if (!kvm_has_tsc_control) {
1268 if (user_tsc_khz > tsc_khz) {
1269 vcpu->arch.tsc_catchup = 1;
1270 vcpu->arch.tsc_always_catchup = 1;
1271 return 0;
1272 } else {
1273 WARN(1, "user requested TSC rate below hardware speed\n");
1274 return -1;
1275 }
1276 }
1277
1278 /* TSC scaling required - calculate ratio */
1279 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1280 user_tsc_khz, tsc_khz);
1281
1282 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
1283 WARN_ONCE(1, "Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1284 user_tsc_khz);
1285 return -1;
1286 }
1287
1288 vcpu->arch.tsc_scaling_ratio = ratio;
1289 return 0;
1290}
1291
1292static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001293{
1294 u32 thresh_lo, thresh_hi;
1295 int use_scaling = 0;
1296
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001297 /* tsc_khz can be zero if TSC calibration fails */
Haozhong Zhangad721882015-10-20 15:39:02 +08001298 if (this_tsc_khz == 0) {
1299 /* set tsc_scaling_ratio to a safe value */
1300 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001301 return -1;
Haozhong Zhangad721882015-10-20 15:39:02 +08001302 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001303
Zachary Amsdenc2855452010-09-18 14:38:15 -10001304 /* Compute a scale to convert nanoseconds in TSC cycles */
1305 kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
Zachary Amsdencc578282012-02-03 15:43:50 -02001306 &vcpu->arch.virtual_tsc_shift,
1307 &vcpu->arch.virtual_tsc_mult);
1308 vcpu->arch.virtual_tsc_khz = this_tsc_khz;
1309
1310 /*
1311 * Compute the variation in TSC rate which is acceptable
1312 * within the range of tolerance and decide if the
1313 * rate being applied is within that bounds of the hardware
1314 * rate. If so, no scaling or compensation need be done.
1315 */
1316 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1317 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
1318 if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
1319 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
1320 use_scaling = 1;
1321 }
Haozhong Zhang381d5852015-10-20 15:39:04 +08001322 return set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001323}
1324
1325static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1326{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001327 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001328 vcpu->arch.virtual_tsc_mult,
1329 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001330 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001331 return tsc;
1332}
1333
Fengguang Wu69b00492015-01-19 22:33:39 +08001334static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001335{
1336#ifdef CONFIG_X86_64
1337 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001338 struct kvm_arch *ka = &vcpu->kvm->arch;
1339 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1340
1341 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1342 atomic_read(&vcpu->kvm->online_vcpus));
1343
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001344 /*
1345 * Once the masterclock is enabled, always perform request in
1346 * order to update it.
1347 *
1348 * In order to enable masterclock, the host clocksource must be TSC
1349 * and the vcpus need to have matched TSCs. When that happens,
1350 * perform request to enable masterclock.
1351 */
1352 if (ka->use_master_clock ||
1353 (gtod->clock.vclock_mode == VCLOCK_TSC && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001354 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1355
1356 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1357 atomic_read(&vcpu->kvm->online_vcpus),
1358 ka->use_master_clock, gtod->clock.vclock_mode);
1359#endif
1360}
1361
Will Auldba904632012-11-29 12:42:50 -08001362static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1363{
1364 u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
1365 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1366}
1367
Haozhong Zhang35181e82015-10-20 15:39:03 +08001368/*
1369 * Multiply tsc by a fixed point number represented by ratio.
1370 *
1371 * The most significant 64-N bits (mult) of ratio represent the
1372 * integral part of the fixed point number; the remaining N bits
1373 * (frac) represent the fractional part, ie. ratio represents a fixed
1374 * point number (mult + frac * 2^(-N)).
1375 *
1376 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1377 */
1378static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1379{
1380 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1381}
1382
1383u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1384{
1385 u64 _tsc = tsc;
1386 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1387
1388 if (ratio != kvm_default_tsc_scaling_ratio)
1389 _tsc = __scale_tsc(ratio, tsc);
1390
1391 return _tsc;
1392}
1393EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1394
Haozhong Zhang07c14192015-10-20 15:39:05 +08001395static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1396{
1397 u64 tsc;
1398
1399 tsc = kvm_scale_tsc(vcpu, rdtsc());
1400
1401 return target_tsc - tsc;
1402}
1403
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001404u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1405{
1406 return kvm_x86_ops->read_l1_tsc(vcpu, kvm_scale_tsc(vcpu, host_tsc));
1407}
1408EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1409
Will Auld8fe8ab42012-11-29 12:42:12 -08001410void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001411{
1412 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001413 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001414 unsigned long flags;
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001415 s64 usdiff;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001416 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001417 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001418 u64 data = msr->data;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001419
Jan Kiszka038f8c12011-02-04 10:49:11 +01001420 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001421 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001422 ns = get_kernel_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001423 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001424
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001425 if (vcpu->arch.virtual_tsc_khz) {
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001426 int faulted = 0;
1427
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001428 /* n.b - signed multiplication and division required */
1429 usdiff = data - kvm->arch.last_tsc_write;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001430#ifdef CONFIG_X86_64
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001431 usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001432#else
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001433 /* do_div() only does unsigned */
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001434 asm("1: idivl %[divisor]\n"
1435 "2: xor %%edx, %%edx\n"
1436 " movl $0, %[faulted]\n"
1437 "3:\n"
1438 ".section .fixup,\"ax\"\n"
1439 "4: movl $1, %[faulted]\n"
1440 " jmp 3b\n"
1441 ".previous\n"
1442
1443 _ASM_EXTABLE(1b, 4b)
1444
1445 : "=A"(usdiff), [faulted] "=r" (faulted)
1446 : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
1447
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001448#endif
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001449 do_div(elapsed, 1000);
1450 usdiff -= elapsed;
1451 if (usdiff < 0)
1452 usdiff = -usdiff;
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001453
1454 /* idivl overflow => difference is larger than USEC_PER_SEC */
1455 if (faulted)
1456 usdiff = USEC_PER_SEC;
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001457 } else
1458 usdiff = USEC_PER_SEC; /* disable TSC match window below */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001459
1460 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001461 * Special case: TSC write with a small delta (1 second) of virtual
1462 * cycle time against real time is interpreted as an attempt to
1463 * synchronize the CPU.
1464 *
1465 * For a reliable TSC, we can match TSC offsets, and for an unstable
1466 * TSC, we add elapsed time in this computation. We could let the
1467 * compensation code attempt to catch up if we fall behind, but
1468 * it's better to try to match offsets from the beginning.
1469 */
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001470 if (usdiff < USEC_PER_SEC &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001471 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001472 if (!check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001473 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001474 pr_debug("kvm: matched tsc offset for %llu\n", data);
1475 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001476 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001477 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08001478 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001479 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001480 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001481 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001482 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001483 } else {
1484 /*
1485 * We split periods of matched TSC writes into generations.
1486 * For each generation, we track the original measured
1487 * nanosecond time, offset, and write, so if TSCs are in
1488 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001489 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001490 *
1491 * These values are tracked in kvm->arch.cur_xxx variables.
1492 */
1493 kvm->arch.cur_tsc_generation++;
1494 kvm->arch.cur_tsc_nsec = ns;
1495 kvm->arch.cur_tsc_write = data;
1496 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001497 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001498 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001499 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001500 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001501
1502 /*
1503 * We also track th most recent recorded KHZ, write and time to
1504 * allow the matching interval to be extended at each write.
1505 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001506 kvm->arch.last_tsc_nsec = ns;
1507 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001508 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001509
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001510 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001511
1512 /* Keep track of which generation this VCPU has synchronized to */
1513 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1514 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1515 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1516
Will Auldba904632012-11-29 12:42:50 -08001517 if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
1518 update_ia32_tsc_adjust_msr(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001519 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1520 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001521
1522 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001523 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001524 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001525 } else if (!already_matched) {
1526 kvm->arch.nr_vcpus_matched_tsc++;
1527 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001528
1529 kvm_track_tsc_matching(vcpu);
1530 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001531}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001532
Zachary Amsden99e3e302010-08-19 22:07:17 -10001533EXPORT_SYMBOL_GPL(kvm_write_tsc);
1534
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001535static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1536 s64 adjustment)
1537{
1538 kvm_x86_ops->adjust_tsc_offset_guest(vcpu, adjustment);
1539}
1540
1541static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1542{
1543 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1544 WARN_ON(adjustment < 0);
1545 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
1546 kvm_x86_ops->adjust_tsc_offset_guest(vcpu, adjustment);
1547}
1548
Marcelo Tosattid8281992012-11-27 23:29:01 -02001549#ifdef CONFIG_X86_64
1550
1551static cycle_t read_tsc(void)
1552{
Andy Lutomirski03b97302015-06-25 18:44:08 +02001553 cycle_t ret = (cycle_t)rdtsc_ordered();
1554 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001555
1556 if (likely(ret >= last))
1557 return ret;
1558
1559 /*
1560 * GCC likes to generate cmov here, but this branch is extremely
1561 * predictable (it's just a funciton of time and the likely is
1562 * very likely) and there's a data dependence, so force GCC
1563 * to generate a branch instead. I don't barrier() because
1564 * we don't actually need a barrier, and if this function
1565 * ever gets inlined it will generate worse code.
1566 */
1567 asm volatile ("");
1568 return last;
1569}
1570
1571static inline u64 vgettsc(cycle_t *cycle_now)
1572{
1573 long v;
1574 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1575
1576 *cycle_now = read_tsc();
1577
1578 v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
1579 return v * gtod->clock.mult;
1580}
1581
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001582static int do_monotonic_boot(s64 *t, cycle_t *cycle_now)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001583{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001584 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001585 unsigned long seq;
1586 int mode;
1587 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001588
Marcelo Tosattid8281992012-11-27 23:29:01 -02001589 do {
1590 seq = read_seqcount_begin(&gtod->seq);
1591 mode = gtod->clock.vclock_mode;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001592 ns = gtod->nsec_base;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001593 ns += vgettsc(cycle_now);
1594 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001595 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001596 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001597 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001598
1599 return mode;
1600}
1601
1602/* returns true if host is using tsc clocksource */
1603static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
1604{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001605 /* checked again under seqlock below */
1606 if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
1607 return false;
1608
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001609 return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001610}
1611#endif
1612
1613/*
1614 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001615 * Assuming a stable TSC across physical CPUS, and a stable TSC
1616 * across virtual CPUs, the following condition is possible.
1617 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001618 * CPUs at the next numbered event.
1619 *
1620 * "timespecX" represents host monotonic time. "tscX" represents
1621 * RDTSC value.
1622 *
1623 * VCPU0 on CPU0 | VCPU1 on CPU1
1624 *
1625 * 1. read timespec0,tsc0
1626 * 2. | timespec1 = timespec0 + N
1627 * | tsc1 = tsc0 + M
1628 * 3. transition to guest | transition to guest
1629 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1630 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1631 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1632 *
1633 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1634 *
1635 * - ret0 < ret1
1636 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1637 * ...
1638 * - 0 < N - M => M < N
1639 *
1640 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1641 * always the case (the difference between two distinct xtime instances
1642 * might be smaller then the difference between corresponding TSC reads,
1643 * when updating guest vcpus pvclock areas).
1644 *
1645 * To avoid that problem, do not allow visibility of distinct
1646 * system_timestamp/tsc_timestamp values simultaneously: use a master
1647 * copy of host monotonic time values. Update that master copy
1648 * in lockstep.
1649 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001650 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001651 *
1652 */
1653
1654static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1655{
1656#ifdef CONFIG_X86_64
1657 struct kvm_arch *ka = &kvm->arch;
1658 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001659 bool host_tsc_clocksource, vcpus_matched;
1660
1661 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1662 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001663
1664 /*
1665 * If the host uses TSC clock, then passthrough TSC as stable
1666 * to the guest.
1667 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001668 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001669 &ka->master_kernel_ns,
1670 &ka->master_cycle_now);
1671
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001672 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Marcelo Tosatti54750f22015-01-20 15:54:52 -02001673 && !backwards_tsc_observed
1674 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001675
Marcelo Tosattid8281992012-11-27 23:29:01 -02001676 if (ka->use_master_clock)
1677 atomic_set(&kvm_guest_has_master_clock, 1);
1678
1679 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001680 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1681 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001682#endif
1683}
1684
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001685static void kvm_gen_update_masterclock(struct kvm *kvm)
1686{
1687#ifdef CONFIG_X86_64
1688 int i;
1689 struct kvm_vcpu *vcpu;
1690 struct kvm_arch *ka = &kvm->arch;
1691
1692 spin_lock(&ka->pvclock_gtod_sync_lock);
1693 kvm_make_mclock_inprogress_request(kvm);
1694 /* no guest entries from this point */
1695 pvclock_update_vm_gtod_copy(kvm);
1696
1697 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001698 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001699
1700 /* guest entries allowed */
1701 kvm_for_each_vcpu(i, vcpu, kvm)
1702 clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
1703
1704 spin_unlock(&ka->pvclock_gtod_sync_lock);
1705#endif
1706}
1707
Zachary Amsden34c238a2010-09-18 14:38:14 -10001708static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001709{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001710 unsigned long flags, this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001711 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001712 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02001713 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001714 u64 tsc_timestamp, host_tsc;
Andy Honig0b794592013-02-20 14:48:10 -08001715 struct pvclock_vcpu_time_info guest_hv_clock;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001716 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001717 bool use_master_clock;
1718
1719 kernel_ns = 0;
1720 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001721
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001722 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02001723 * If the host uses TSC clock, then passthrough TSC as stable
1724 * to the guest.
1725 */
1726 spin_lock(&ka->pvclock_gtod_sync_lock);
1727 use_master_clock = ka->use_master_clock;
1728 if (use_master_clock) {
1729 host_tsc = ka->master_cycle_now;
1730 kernel_ns = ka->master_kernel_ns;
1731 }
1732 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001733
1734 /* Keep irq disabled to prevent changes to the clock */
1735 local_irq_save(flags);
Christoph Lameter89cbc762014-08-17 12:30:40 -05001736 this_tsc_khz = __this_cpu_read(cpu_tsc_khz);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001737 if (unlikely(this_tsc_khz == 0)) {
1738 local_irq_restore(flags);
1739 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
1740 return 1;
1741 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001742 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001743 host_tsc = rdtsc();
Marcelo Tosattid8281992012-11-27 23:29:01 -02001744 kernel_ns = get_kernel_ns();
1745 }
1746
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001747 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001748
1749 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10001750 * We may have to catch up the TSC to match elapsed wall clock
1751 * time for two reasons, even if kvmclock is used.
1752 * 1) CPU could have been running below the maximum TSC rate
1753 * 2) Broken TSC compensation resets the base at each VCPU
1754 * entry to avoid unknown leaps of TSC even when running
1755 * again on the same CPU. This may cause apparent elapsed
1756 * time to disappear, and the guest to stand still or run
1757 * very slowly.
1758 */
1759 if (vcpu->tsc_catchup) {
1760 u64 tsc = compute_guest_tsc(v, kernel_ns);
1761 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001762 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001763 tsc_timestamp = tsc;
1764 }
1765 }
1766
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001767 local_irq_restore(flags);
1768
Andy Honig0b794592013-02-20 14:48:10 -08001769 if (!vcpu->pv_time_enabled)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001770 return 0;
1771
Zachary Amsdene48672f2010-08-19 22:07:23 -10001772 if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001773 kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
1774 &vcpu->hv_clock.tsc_shift,
1775 &vcpu->hv_clock.tsc_to_system_mul);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001776 vcpu->hw_tsc_khz = this_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001777 }
1778
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001779 /* With all the info we got, fill in the values */
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001780 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10001781 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10001782 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001783
Owen Hofmann09a0c3f2014-11-03 16:57:18 -08001784 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1785 &guest_hv_clock, sizeof(guest_hv_clock))))
1786 return 0;
1787
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001788 /* This VCPU is paused, but it's legal for a guest to read another
1789 * VCPU's kvmclock, so we really have to follow the specification where
1790 * it says that version is odd if data is being modified, and even after
1791 * it is consistent.
1792 *
1793 * Version field updates must be kept separate. This is because
1794 * kvm_write_guest_cached might use a "rep movs" instruction, and
1795 * writes within a string instruction are weakly ordered. So there
1796 * are three writes overall.
1797 *
1798 * As a small optimization, only write the version field in the first
1799 * and third write. The vcpu->pv_time cache is still valid, because the
1800 * version field is the first in the struct.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001801 */
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001802 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
1803
1804 vcpu->hv_clock.version = guest_hv_clock.version + 1;
1805 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1806 &vcpu->hv_clock,
1807 sizeof(vcpu->hv_clock.version));
1808
1809 smp_wmb();
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001810
1811 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
Andy Honig0b794592013-02-20 14:48:10 -08001812 pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001813
1814 if (vcpu->pvclock_set_guest_stopped_request) {
1815 pvclock_flags |= PVCLOCK_GUEST_STOPPED;
1816 vcpu->pvclock_set_guest_stopped_request = false;
1817 }
1818
Marcelo Tosattid8281992012-11-27 23:29:01 -02001819 /* If the host uses TSC clocksource, then it is stable */
1820 if (use_master_clock)
1821 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
1822
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001823 vcpu->hv_clock.flags = pvclock_flags;
1824
David Matlackce1a5e62014-11-05 11:46:42 -08001825 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
1826
Andy Honig0b794592013-02-20 14:48:10 -08001827 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1828 &vcpu->hv_clock,
1829 sizeof(vcpu->hv_clock));
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001830
1831 smp_wmb();
1832
1833 vcpu->hv_clock.version++;
1834 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1835 &vcpu->hv_clock,
1836 sizeof(vcpu->hv_clock.version));
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001837 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001838}
1839
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001840/*
1841 * kvmclock updates which are isolated to a given vcpu, such as
1842 * vcpu->cpu migration, should not allow system_timestamp from
1843 * the rest of the vcpus to remain static. Otherwise ntp frequency
1844 * correction applies to one vcpu's system_timestamp but not
1845 * the others.
1846 *
1847 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01001848 * We need to rate-limit these requests though, as they can
1849 * considerably slow guests that have a large number of vcpus.
1850 * The time for a remote vcpu to update its kvmclock is bound
1851 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001852 */
1853
Andrew Jones7e44e442014-02-28 12:52:54 +01001854#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
1855
1856static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001857{
1858 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01001859 struct delayed_work *dwork = to_delayed_work(work);
1860 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1861 kvmclock_update_work);
1862 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001863 struct kvm_vcpu *vcpu;
1864
1865 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001866 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001867 kvm_vcpu_kick(vcpu);
1868 }
1869}
1870
Andrew Jones7e44e442014-02-28 12:52:54 +01001871static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
1872{
1873 struct kvm *kvm = v->kvm;
1874
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001875 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01001876 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
1877 KVMCLOCK_UPDATE_DELAY);
1878}
1879
Andrew Jones332967a2014-02-28 12:52:55 +01001880#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
1881
1882static void kvmclock_sync_fn(struct work_struct *work)
1883{
1884 struct delayed_work *dwork = to_delayed_work(work);
1885 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1886 kvmclock_sync_work);
1887 struct kvm *kvm = container_of(ka, struct kvm, arch);
1888
Marcelo Tosatti630994b2015-05-12 22:42:04 -03001889 if (!kvmclock_periodic_sync)
1890 return;
1891
Andrew Jones332967a2014-02-28 12:52:55 +01001892 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
1893 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
1894 KVMCLOCK_SYNC_PERIOD);
1895}
1896
Huang Ying890ca9a2009-05-11 16:48:15 +08001897static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1898{
1899 u64 mcg_cap = vcpu->arch.mcg_cap;
1900 unsigned bank_num = mcg_cap & 0xff;
1901
1902 switch (msr) {
1903 case MSR_IA32_MCG_STATUS:
1904 vcpu->arch.mcg_status = data;
1905 break;
1906 case MSR_IA32_MCG_CTL:
1907 if (!(mcg_cap & MCG_CTL_P))
1908 return 1;
1909 if (data != 0 && data != ~(u64)0)
1910 return -1;
1911 vcpu->arch.mcg_ctl = data;
1912 break;
1913 default:
1914 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08001915 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08001916 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001917 /* only 0 or all 1s can be written to IA32_MCi_CTL
1918 * some Linux kernels though clear bit 10 in bank 4 to
1919 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1920 * this to avoid an uncatched #GP in the guest
1921 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001922 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001923 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001924 return -1;
1925 vcpu->arch.mce_banks[offset] = data;
1926 break;
1927 }
1928 return 1;
1929 }
1930 return 0;
1931}
1932
Ed Swierkffde22a2009-10-15 15:21:43 -07001933static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1934{
1935 struct kvm *kvm = vcpu->kvm;
1936 int lm = is_long_mode(vcpu);
1937 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1938 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1939 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1940 : kvm->arch.xen_hvm_config.blob_size_32;
1941 u32 page_num = data & ~PAGE_MASK;
1942 u64 page_addr = data & PAGE_MASK;
1943 u8 *page;
1944 int r;
1945
1946 r = -E2BIG;
1947 if (page_num >= blob_size)
1948 goto out;
1949 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001950 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
1951 if (IS_ERR(page)) {
1952 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07001953 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001954 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001955 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07001956 goto out_free;
1957 r = 0;
1958out_free:
1959 kfree(page);
1960out:
1961 return r;
1962}
1963
Gleb Natapov344d9582010-10-14 11:22:50 +02001964static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
1965{
1966 gpa_t gpa = data & ~0x3f;
1967
Guo Chao4a969982012-06-28 15:17:27 +08001968 /* Bits 2:5 are reserved, Should be zero */
Gleb Natapov6adba522010-10-14 11:22:55 +02001969 if (data & 0x3c)
Gleb Natapov344d9582010-10-14 11:22:50 +02001970 return 1;
1971
1972 vcpu->arch.apf.msr_val = data;
1973
1974 if (!(data & KVM_ASYNC_PF_ENABLED)) {
1975 kvm_clear_async_pf_completion_queue(vcpu);
1976 kvm_async_pf_hash_reset(vcpu);
1977 return 0;
1978 }
1979
Andrew Honig8f964522013-03-29 09:35:21 -07001980 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
1981 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02001982 return 1;
1983
Gleb Natapov6adba522010-10-14 11:22:55 +02001984 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Gleb Natapov344d9582010-10-14 11:22:50 +02001985 kvm_async_pf_wakeup_all(vcpu);
1986 return 0;
1987}
1988
Glauber Costa12f9a482011-02-01 14:16:40 -05001989static void kvmclock_reset(struct kvm_vcpu *vcpu)
1990{
Andy Honig0b794592013-02-20 14:48:10 -08001991 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05001992}
1993
Glauber Costac9aaa892011-07-11 15:28:14 -04001994static void accumulate_steal_time(struct kvm_vcpu *vcpu)
1995{
1996 u64 delta;
1997
1998 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
1999 return;
2000
2001 delta = current->sched_info.run_delay - vcpu->arch.st.last_steal;
2002 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2003 vcpu->arch.st.accum_steal = delta;
2004}
2005
2006static void record_steal_time(struct kvm_vcpu *vcpu)
2007{
Marcelo Tosatti7cae2be2015-10-14 19:33:09 -03002008 accumulate_steal_time(vcpu);
2009
Glauber Costac9aaa892011-07-11 15:28:14 -04002010 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2011 return;
2012
2013 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2014 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2015 return;
2016
2017 vcpu->arch.st.steal.steal += vcpu->arch.st.accum_steal;
2018 vcpu->arch.st.steal.version += 2;
2019 vcpu->arch.st.accum_steal = 0;
2020
2021 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2022 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2023}
2024
Will Auld8fe8ab42012-11-29 12:42:12 -08002025int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002026{
Gleb Natapov57537852012-01-15 14:17:22 +02002027 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002028 u32 msr = msr_info->index;
2029 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002030
Carsten Otte15c4a642007-10-30 18:44:17 +01002031 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002032 case MSR_AMD64_NB_CFG:
2033 case MSR_IA32_UCODE_REV:
2034 case MSR_IA32_UCODE_WRITE:
2035 case MSR_VM_HSAVE_PA:
2036 case MSR_AMD64_PATCH_LOADER:
2037 case MSR_AMD64_BU_CFG2:
2038 break;
2039
Carsten Otte15c4a642007-10-30 18:44:17 +01002040 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02002041 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002042 case MSR_K7_HWCR:
2043 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002044 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002045 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02002046 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02002047 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002048 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2049 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002050 return 1;
2051 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002052 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002053 case MSR_FAM10H_MMIO_CONF_BASE:
2054 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002055 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2056 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002057 return 1;
2058 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002059 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002060 case MSR_IA32_DEBUGCTLMSR:
2061 if (!data) {
2062 /* We support the non-activated case already */
2063 break;
2064 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2065 /* Values other than LBR and BTF are vendor-specific,
2066 thus reserved and should throw a #GP */
2067 return 1;
2068 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002069 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2070 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002071 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002072 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002073 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002074 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002075 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002076 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2077 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002078 case MSR_IA32_TSCDEADLINE:
2079 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2080 break;
Will Auldba904632012-11-29 12:42:50 -08002081 case MSR_IA32_TSC_ADJUST:
2082 if (guest_cpuid_has_tsc_adjust(vcpu)) {
2083 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002084 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002085 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002086 }
2087 vcpu->arch.ia32_tsc_adjust_msr = data;
2088 }
2089 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002090 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002091 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01002092 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002093 case MSR_IA32_SMBASE:
2094 if (!msr_info->host_initiated)
2095 return 1;
2096 vcpu->arch.smbase = data;
2097 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002098 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002099 case MSR_KVM_WALL_CLOCK:
2100 vcpu->kvm->arch.wall_clock = data;
2101 kvm_write_wall_clock(vcpu->kvm, data);
2102 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002103 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002104 case MSR_KVM_SYSTEM_TIME: {
Andy Honig0b794592013-02-20 14:48:10 -08002105 u64 gpa_offset;
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002106 struct kvm_arch *ka = &vcpu->kvm->arch;
2107
Glauber Costa12f9a482011-02-01 14:16:40 -05002108 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002109
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002110 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2111 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2112
2113 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
2114 set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
2115 &vcpu->requests);
2116
2117 ka->boot_vcpu_runs_old_kvmclock = tmp;
2118 }
2119
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002120 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002121 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002122
2123 /* we verify if the enable bit is set... */
2124 if (!(data & 1))
2125 break;
2126
Andy Honig0b794592013-02-20 14:48:10 -08002127 gpa_offset = data & ~(PAGE_MASK | 1);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002128
Andy Honig0b794592013-02-20 14:48:10 -08002129 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002130 &vcpu->arch.pv_time, data & ~1ULL,
2131 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002132 vcpu->arch.pv_time_enabled = false;
2133 else
2134 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002135
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002136 break;
2137 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002138 case MSR_KVM_ASYNC_PF_EN:
2139 if (kvm_pv_enable_async_pf(vcpu, data))
2140 return 1;
2141 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002142 case MSR_KVM_STEAL_TIME:
2143
2144 if (unlikely(!sched_info_on()))
2145 return 1;
2146
2147 if (data & KVM_STEAL_RESERVED_MASK)
2148 return 1;
2149
2150 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002151 data & KVM_STEAL_VALID_BITS,
2152 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002153 return 1;
2154
2155 vcpu->arch.st.msr_val = data;
2156
2157 if (!(data & KVM_MSR_ENABLED))
2158 break;
2159
Glauber Costac9aaa892011-07-11 15:28:14 -04002160 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2161
2162 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002163 case MSR_KVM_PV_EOI_EN:
2164 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2165 return 1;
2166 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002167
Huang Ying890ca9a2009-05-11 16:48:15 +08002168 case MSR_IA32_MCG_CTL:
2169 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002170 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Huang Ying890ca9a2009-05-11 16:48:15 +08002171 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002172
Wei Huang6912ac32015-06-12 01:34:56 -04002173 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2174 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2175 pr = true; /* fall through */
2176 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2177 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002178 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002179 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002180
2181 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002182 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2183 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002184 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002185 case MSR_K7_CLK_CTL:
2186 /*
2187 * Ignore all writes to this no longer documented MSR.
2188 * Writes are only relevant for old K7 processors,
2189 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002190 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002191 * affected processor models on the command line, hence
2192 * the need to ignore the workaround.
2193 */
2194 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002195 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002196 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2197 case HV_X64_MSR_CRASH_CTL:
2198 return kvm_hv_set_msr_common(vcpu, msr, data,
2199 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002200 case MSR_IA32_BBL_CR_CTL3:
2201 /* Drop writes to this legacy MSR -- see rdmsr
2202 * counterpart for further detail.
2203 */
Christoffer Dalla737f252012-06-03 21:17:48 +03002204 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002205 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002206 case MSR_AMD64_OSVW_ID_LENGTH:
2207 if (!guest_cpuid_has_osvw(vcpu))
2208 return 1;
2209 vcpu->arch.osvw.length = data;
2210 break;
2211 case MSR_AMD64_OSVW_STATUS:
2212 if (!guest_cpuid_has_osvw(vcpu))
2213 return 1;
2214 vcpu->arch.osvw.status = data;
2215 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002216 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002217 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2218 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002219 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002220 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002221 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002222 vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
2223 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002224 return 1;
2225 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002226 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
2227 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002228 break;
2229 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002230 }
2231 return 0;
2232}
2233EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2234
2235
2236/*
2237 * Reads an msr value (of 'msr_index') into 'pdata'.
2238 * Returns 0 on success, non-0 otherwise.
2239 * Assumes vcpu_load() was already called.
2240 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002241int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01002242{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002243 return kvm_x86_ops->get_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002244}
Wincy Vanff651cb2014-12-11 08:52:58 +03002245EXPORT_SYMBOL_GPL(kvm_get_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002246
Huang Ying890ca9a2009-05-11 16:48:15 +08002247static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2248{
2249 u64 data;
2250 u64 mcg_cap = vcpu->arch.mcg_cap;
2251 unsigned bank_num = mcg_cap & 0xff;
2252
2253 switch (msr) {
2254 case MSR_IA32_P5_MC_ADDR:
2255 case MSR_IA32_P5_MC_TYPE:
2256 data = 0;
2257 break;
2258 case MSR_IA32_MCG_CAP:
2259 data = vcpu->arch.mcg_cap;
2260 break;
2261 case MSR_IA32_MCG_CTL:
2262 if (!(mcg_cap & MCG_CTL_P))
2263 return 1;
2264 data = vcpu->arch.mcg_ctl;
2265 break;
2266 case MSR_IA32_MCG_STATUS:
2267 data = vcpu->arch.mcg_status;
2268 break;
2269 default:
2270 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002271 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002272 u32 offset = msr - MSR_IA32_MC0_CTL;
2273 data = vcpu->arch.mce_banks[offset];
2274 break;
2275 }
2276 return 1;
2277 }
2278 *pdata = data;
2279 return 0;
2280}
2281
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002282int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002283{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002284 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002285 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002286 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002287 case MSR_IA32_DEBUGCTLMSR:
2288 case MSR_IA32_LASTBRANCHFROMIP:
2289 case MSR_IA32_LASTBRANCHTOIP:
2290 case MSR_IA32_LASTINTFROMIP:
2291 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302292 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002293 case MSR_K8_TSEG_ADDR:
2294 case MSR_K8_TSEG_MASK:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302295 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002296 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002297 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002298 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002299 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002300 case MSR_AMD64_BU_CFG2:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002301 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002302 break;
Wei Huang6912ac32015-06-12 01:34:56 -04002303 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2304 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2305 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2306 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002307 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002308 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2309 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002310 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002311 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002312 msr_info->data = 0x100000000ULL;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002313 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002314 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002315 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002316 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002317 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002318 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002319 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002320 /*
2321 * MSR_EBC_FREQUENCY_ID
2322 * Conservative value valid for even the basic CPU models.
2323 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2324 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2325 * and 266MHz for model 3, or 4. Set Core Clock
2326 * Frequency to System Bus Frequency Ratio to 1 (bits
2327 * 31:24) even though these are only valid for CPU
2328 * models > 2, however guests may end up dividing or
2329 * multiplying by zero otherwise.
2330 */
2331 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002332 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002333 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002334 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002335 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002336 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002337 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002338 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002339 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002340 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002341 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002342 break;
Will Auldba904632012-11-29 12:42:50 -08002343 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002344 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002345 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002346 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002347 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002348 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002349 case MSR_IA32_SMBASE:
2350 if (!msr_info->host_initiated)
2351 return 1;
2352 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002353 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002354 case MSR_IA32_PERF_STATUS:
2355 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002356 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002357 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002358 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002359 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002360 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002361 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002362 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002363 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002364 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002365 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002366 break;
2367 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002368 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002369 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002370 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002371 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002372 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02002373 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002374 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002375 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04002376 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002377 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002378 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002379 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002380 case MSR_IA32_P5_MC_ADDR:
2381 case MSR_IA32_P5_MC_TYPE:
2382 case MSR_IA32_MCG_CAP:
2383 case MSR_IA32_MCG_CTL:
2384 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002385 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002386 return get_msr_mce(vcpu, msr_info->index, &msr_info->data);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002387 case MSR_K7_CLK_CTL:
2388 /*
2389 * Provide expected ramp-up count for K7. All other
2390 * are set to zero, indicating minimum divisors for
2391 * every field.
2392 *
2393 * This prevents guest kernels on AMD host with CPU
2394 * type 6, model 8 and higher from exploding due to
2395 * the rdmsr failing.
2396 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002397 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002398 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002399 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002400 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2401 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanine83d5882015-07-03 15:01:34 +03002402 return kvm_hv_get_msr_common(vcpu,
2403 msr_info->index, &msr_info->data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002404 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002405 case MSR_IA32_BBL_CR_CTL3:
2406 /* This legacy MSR exists but isn't fully documented in current
2407 * silicon. It is however accessed by winxp in very narrow
2408 * scenarios where it sets bit #19, itself documented as
2409 * a "reserved" bit. Best effort attempt to source coherent
2410 * read data here should the balance of the register be
2411 * interpreted by the guest:
2412 *
2413 * L2 cache control register 3: 64GB range, 256KB size,
2414 * enabled, latency 0x1, configured
2415 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002416 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05002417 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002418 case MSR_AMD64_OSVW_ID_LENGTH:
2419 if (!guest_cpuid_has_osvw(vcpu))
2420 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002421 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002422 break;
2423 case MSR_AMD64_OSVW_STATUS:
2424 if (!guest_cpuid_has_osvw(vcpu))
2425 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002426 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002427 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002428 default:
Wei Huangc6702c92015-06-19 13:44:45 +02002429 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002430 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002431 if (!ignore_msrs) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002432 vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02002433 return 1;
2434 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002435 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr_info->index);
2436 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02002437 }
2438 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002439 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002440 return 0;
2441}
2442EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2443
Carsten Otte313a3dc2007-10-11 19:16:52 +02002444/*
2445 * Read or write a bunch of msrs. All parameters are kernel addresses.
2446 *
2447 * @return number of msrs set successfully.
2448 */
2449static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2450 struct kvm_msr_entry *entries,
2451 int (*do_msr)(struct kvm_vcpu *vcpu,
2452 unsigned index, u64 *data))
2453{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002454 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002455
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002456 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002457 for (i = 0; i < msrs->nmsrs; ++i)
2458 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2459 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002460 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002461
Carsten Otte313a3dc2007-10-11 19:16:52 +02002462 return i;
2463}
2464
2465/*
2466 * Read or write a bunch of msrs. Parameters are user addresses.
2467 *
2468 * @return number of msrs set successfully.
2469 */
2470static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2471 int (*do_msr)(struct kvm_vcpu *vcpu,
2472 unsigned index, u64 *data),
2473 int writeback)
2474{
2475 struct kvm_msrs msrs;
2476 struct kvm_msr_entry *entries;
2477 int r, n;
2478 unsigned size;
2479
2480 r = -EFAULT;
2481 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2482 goto out;
2483
2484 r = -E2BIG;
2485 if (msrs.nmsrs >= MAX_IO_MSRS)
2486 goto out;
2487
Carsten Otte313a3dc2007-10-11 19:16:52 +02002488 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002489 entries = memdup_user(user_msrs->entries, size);
2490 if (IS_ERR(entries)) {
2491 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002492 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002493 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002494
2495 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2496 if (r < 0)
2497 goto out_free;
2498
2499 r = -EFAULT;
2500 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2501 goto out_free;
2502
2503 r = n;
2504
2505out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002506 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002507out:
2508 return r;
2509}
2510
Alexander Graf784aa3d2014-07-14 18:27:35 +02002511int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002512{
2513 int r;
2514
2515 switch (ext) {
2516 case KVM_CAP_IRQCHIP:
2517 case KVM_CAP_HLT:
2518 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002519 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002520 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002521 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002522 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002523 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002524 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002525 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002526 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002527 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002528 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002529 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002530 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002531 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002532 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002533 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002534 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002535 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002536 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002537 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002538 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002539 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002540 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002541 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002542 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002543 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002544 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002545 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002546 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002547 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002548 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002549 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002550 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01002551 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03002552 case KVM_CAP_ENABLE_CAP_VM:
2553 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02002554 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07002555 case KVM_CAP_SPLIT_IRQCHIP:
Alex Williamson2a5bab12013-04-16 13:49:18 -06002556#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2557 case KVM_CAP_ASSIGN_DEV_IRQ:
2558 case KVM_CAP_PCI_2_3:
2559#endif
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002560 r = 1;
2561 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02002562 case KVM_CAP_X86_SMM:
2563 /* SMBASE is usually relocated above 1M on modern chipsets,
2564 * and SMM handlers might indeed rely on 4G segment limits,
2565 * so do not report SMM to be available if real mode is
2566 * emulated via vm86 mode. Still, do not go to great lengths
2567 * to avoid userspace's usage of the feature, because it is a
2568 * fringe case that is not enabled except via specific settings
2569 * of the module parameters.
2570 */
2571 r = kvm_x86_ops->cpu_has_high_real_mode_segbase();
2572 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02002573 case KVM_CAP_COALESCED_MMIO:
2574 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
2575 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002576 case KVM_CAP_VAPIC:
2577 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2578 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002579 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002580 r = KVM_SOFT_MAX_VCPUS;
2581 break;
2582 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002583 r = KVM_MAX_VCPUS;
2584 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002585 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002586 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002587 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002588 case KVM_CAP_PV_MMU: /* obsolete */
2589 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002590 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002591#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002592 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +02002593 r = iommu_present(&pci_bus_type);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002594 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002595#endif
Huang Ying890ca9a2009-05-11 16:48:15 +08002596 case KVM_CAP_MCE:
2597 r = KVM_MAX_MCE_BANKS;
2598 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002599 case KVM_CAP_XCRS:
2600 r = cpu_has_xsave;
2601 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002602 case KVM_CAP_TSC_CONTROL:
2603 r = kvm_has_tsc_control;
2604 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002605 default:
2606 r = 0;
2607 break;
2608 }
2609 return r;
2610
2611}
2612
Carsten Otte043405e2007-10-10 17:16:19 +02002613long kvm_arch_dev_ioctl(struct file *filp,
2614 unsigned int ioctl, unsigned long arg)
2615{
2616 void __user *argp = (void __user *)arg;
2617 long r;
2618
2619 switch (ioctl) {
2620 case KVM_GET_MSR_INDEX_LIST: {
2621 struct kvm_msr_list __user *user_msr_list = argp;
2622 struct kvm_msr_list msr_list;
2623 unsigned n;
2624
2625 r = -EFAULT;
2626 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2627 goto out;
2628 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002629 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Carsten Otte043405e2007-10-10 17:16:19 +02002630 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2631 goto out;
2632 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002633 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02002634 goto out;
2635 r = -EFAULT;
2636 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2637 num_msrs_to_save * sizeof(u32)))
2638 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002639 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02002640 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002641 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02002642 goto out;
2643 r = 0;
2644 break;
2645 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002646 case KVM_GET_SUPPORTED_CPUID:
2647 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02002648 struct kvm_cpuid2 __user *cpuid_arg = argp;
2649 struct kvm_cpuid2 cpuid;
2650
2651 r = -EFAULT;
2652 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2653 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002654
2655 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2656 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02002657 if (r)
2658 goto out;
2659
2660 r = -EFAULT;
2661 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2662 goto out;
2663 r = 0;
2664 break;
2665 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002666 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
2667 u64 mce_cap;
2668
2669 mce_cap = KVM_MCE_CAP_SUPPORTED;
2670 r = -EFAULT;
2671 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
2672 goto out;
2673 r = 0;
2674 break;
2675 }
Carsten Otte043405e2007-10-10 17:16:19 +02002676 default:
2677 r = -EINVAL;
2678 }
2679out:
2680 return r;
2681}
2682
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002683static void wbinvd_ipi(void *garbage)
2684{
2685 wbinvd();
2686}
2687
2688static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
2689{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06002690 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002691}
2692
Carsten Otte313a3dc2007-10-11 19:16:52 +02002693void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2694{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002695 /* Address WBINVD may be executed by guest */
2696 if (need_emulate_wbinvd(vcpu)) {
2697 if (kvm_x86_ops->has_wbinvd_exit())
2698 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
2699 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
2700 smp_call_function_single(vcpu->cpu,
2701 wbinvd_ipi, NULL, 1);
2702 }
2703
Carsten Otte313a3dc2007-10-11 19:16:52 +02002704 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002705
2706 /* Apply any externally detected TSC adjustments (due to suspend) */
2707 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
2708 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
2709 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002710 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002711 }
2712
Zachary Amsden48434c202010-08-19 22:07:24 -10002713 if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002714 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002715 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10002716 if (tsc_delta < 0)
2717 mark_tsc_unstable("KVM discovered backwards TSC");
Zachary Amsdenc2855452010-09-18 14:38:15 -10002718 if (check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08002719 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002720 vcpu->arch.last_guest_tsc);
2721 kvm_x86_ops->write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002722 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002723 }
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02002724 /*
2725 * On a host with synchronized TSC, there is no need to update
2726 * kvmclock on vcpu->cpu migration
2727 */
2728 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002729 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002730 if (vcpu->cpu != cpu)
2731 kvm_migrate_timers(vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002732 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10002733 }
Glauber Costac9aaa892011-07-11 15:28:14 -04002734
Glauber Costac9aaa892011-07-11 15:28:14 -04002735 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002736}
2737
2738void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2739{
Avi Kivity02daab22009-12-30 12:40:26 +02002740 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03002741 kvm_put_guest_fpu(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002742 vcpu->arch.last_host_tsc = rdtsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02002743}
2744
Carsten Otte313a3dc2007-10-11 19:16:52 +02002745static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2746 struct kvm_lapic_state *s)
2747{
Yang Zhang5a717852013-04-11 19:25:16 +08002748 kvm_x86_ops->sync_pir_to_irr(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002749 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002750
2751 return 0;
2752}
2753
2754static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2755 struct kvm_lapic_state *s)
2756{
Gleb Natapov64eb0622012-08-08 15:24:36 +03002757 kvm_apic_post_state_restore(vcpu, s);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002758 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002759
2760 return 0;
2761}
2762
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002763static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2764 struct kvm_interrupt *irq)
2765{
Chen Gang02cdb502013-02-27 11:33:25 +08002766 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002767 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002768
2769 if (!irqchip_in_kernel(vcpu->kvm)) {
2770 kvm_queue_interrupt(vcpu, irq->irq, false);
2771 kvm_make_request(KVM_REQ_EVENT, vcpu);
2772 return 0;
2773 }
2774
2775 /*
2776 * With in-kernel LAPIC, we only use this to inject EXTINT, so
2777 * fail for in-kernel 8259.
2778 */
2779 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002780 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002781
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002782 if (vcpu->arch.pending_external_vector != -1)
2783 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002784
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002785 vcpu->arch.pending_external_vector = irq->irq;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002786 return 0;
2787}
2788
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002789static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2790{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002791 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002792
2793 return 0;
2794}
2795
Paolo Bonzinif0778252015-04-01 15:06:40 +02002796static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
2797{
Paolo Bonzini64d60672015-05-07 11:36:11 +02002798 kvm_make_request(KVM_REQ_SMI, vcpu);
2799
Paolo Bonzinif0778252015-04-01 15:06:40 +02002800 return 0;
2801}
2802
Avi Kivityb209749f2007-10-22 16:50:39 +02002803static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2804 struct kvm_tpr_access_ctl *tac)
2805{
2806 if (tac->flags)
2807 return -EINVAL;
2808 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2809 return 0;
2810}
2811
Huang Ying890ca9a2009-05-11 16:48:15 +08002812static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2813 u64 mcg_cap)
2814{
2815 int r;
2816 unsigned bank_num = mcg_cap & 0xff, bank;
2817
2818 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002819 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002820 goto out;
2821 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2822 goto out;
2823 r = 0;
2824 vcpu->arch.mcg_cap = mcg_cap;
2825 /* Init IA32_MCG_CTL to all 1s */
2826 if (mcg_cap & MCG_CTL_P)
2827 vcpu->arch.mcg_ctl = ~(u64)0;
2828 /* Init IA32_MCi_CTL to all 1s */
2829 for (bank = 0; bank < bank_num; bank++)
2830 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2831out:
2832 return r;
2833}
2834
2835static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2836 struct kvm_x86_mce *mce)
2837{
2838 u64 mcg_cap = vcpu->arch.mcg_cap;
2839 unsigned bank_num = mcg_cap & 0xff;
2840 u64 *banks = vcpu->arch.mce_banks;
2841
2842 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2843 return -EINVAL;
2844 /*
2845 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2846 * reporting is disabled
2847 */
2848 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2849 vcpu->arch.mcg_ctl != ~(u64)0)
2850 return 0;
2851 banks += 4 * mce->bank;
2852 /*
2853 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2854 * reporting is disabled for the bank
2855 */
2856 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2857 return 0;
2858 if (mce->status & MCI_STATUS_UC) {
2859 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002860 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002861 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002862 return 0;
2863 }
2864 if (banks[1] & MCI_STATUS_VAL)
2865 mce->status |= MCI_STATUS_OVER;
2866 banks[2] = mce->addr;
2867 banks[3] = mce->misc;
2868 vcpu->arch.mcg_status = mce->mcg_status;
2869 banks[1] = mce->status;
2870 kvm_queue_exception(vcpu, MC_VECTOR);
2871 } else if (!(banks[1] & MCI_STATUS_VAL)
2872 || !(banks[1] & MCI_STATUS_UC)) {
2873 if (banks[1] & MCI_STATUS_VAL)
2874 mce->status |= MCI_STATUS_OVER;
2875 banks[2] = mce->addr;
2876 banks[3] = mce->misc;
2877 banks[1] = mce->status;
2878 } else
2879 banks[1] |= MCI_STATUS_OVER;
2880 return 0;
2881}
2882
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002883static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2884 struct kvm_vcpu_events *events)
2885{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002886 process_nmi(vcpu);
Jan Kiszka03b82a32010-02-15 10:45:41 +01002887 events->exception.injected =
2888 vcpu->arch.exception.pending &&
2889 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002890 events->exception.nr = vcpu->arch.exception.nr;
2891 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002892 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002893 events->exception.error_code = vcpu->arch.exception.error_code;
2894
Jan Kiszka03b82a32010-02-15 10:45:41 +01002895 events->interrupt.injected =
2896 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002897 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002898 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002899 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002900
2901 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002902 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002903 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002904 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002905
Jan Kiszka66450a22013-03-13 12:42:34 +01002906 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002907
Paolo Bonzinif0778252015-04-01 15:06:40 +02002908 events->smi.smm = is_smm(vcpu);
2909 events->smi.pending = vcpu->arch.smi_pending;
2910 events->smi.smm_inside_nmi =
2911 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
2912 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
2913
Jan Kiszkadab4b912009-12-06 18:24:15 +01002914 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02002915 | KVM_VCPUEVENT_VALID_SHADOW
2916 | KVM_VCPUEVENT_VALID_SMM);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002917 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002918}
2919
2920static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2921 struct kvm_vcpu_events *events)
2922{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002923 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002924 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02002925 | KVM_VCPUEVENT_VALID_SHADOW
2926 | KVM_VCPUEVENT_VALID_SMM))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002927 return -EINVAL;
2928
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002929 process_nmi(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002930 vcpu->arch.exception.pending = events->exception.injected;
2931 vcpu->arch.exception.nr = events->exception.nr;
2932 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2933 vcpu->arch.exception.error_code = events->exception.error_code;
2934
2935 vcpu->arch.interrupt.pending = events->interrupt.injected;
2936 vcpu->arch.interrupt.nr = events->interrupt.nr;
2937 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01002938 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2939 kvm_x86_ops->set_interrupt_shadow(vcpu,
2940 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002941
2942 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002943 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2944 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002945 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2946
Jan Kiszka66450a22013-03-13 12:42:34 +01002947 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
2948 kvm_vcpu_has_lapic(vcpu))
2949 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002950
Paolo Bonzinif0778252015-04-01 15:06:40 +02002951 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
2952 if (events->smi.smm)
2953 vcpu->arch.hflags |= HF_SMM_MASK;
2954 else
2955 vcpu->arch.hflags &= ~HF_SMM_MASK;
2956 vcpu->arch.smi_pending = events->smi.pending;
2957 if (events->smi.smm_inside_nmi)
2958 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
2959 else
2960 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
2961 if (kvm_vcpu_has_lapic(vcpu)) {
2962 if (events->smi.latched_init)
2963 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
2964 else
2965 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
2966 }
2967 }
2968
Avi Kivity3842d132010-07-27 12:30:24 +03002969 kvm_make_request(KVM_REQ_EVENT, vcpu);
2970
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002971 return 0;
2972}
2973
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002974static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2975 struct kvm_debugregs *dbgregs)
2976{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002977 unsigned long val;
2978
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002979 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03002980 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002981 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002982 dbgregs->dr7 = vcpu->arch.dr7;
2983 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002984 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002985}
2986
2987static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2988 struct kvm_debugregs *dbgregs)
2989{
2990 if (dbgregs->flags)
2991 return -EINVAL;
2992
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002993 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03002994 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002995 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002996 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002997 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01002998 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002999
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003000 return 0;
3001}
3002
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003003#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3004
3005static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3006{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003007 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003008 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003009 u64 valid;
3010
3011 /*
3012 * Copy legacy XSAVE area, to avoid complications with CPUID
3013 * leaves 0 and 1 in the loop below.
3014 */
3015 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3016
3017 /* Set XSTATE_BV */
3018 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3019
3020 /*
3021 * Copy each region from the possibly compacted offset to the
3022 * non-compacted offset.
3023 */
3024 valid = xstate_bv & ~XSTATE_FPSSE;
3025 while (valid) {
3026 u64 feature = valid & -valid;
3027 int index = fls64(feature) - 1;
3028 void *src = get_xsave_addr(xsave, feature);
3029
3030 if (src) {
3031 u32 size, offset, ecx, edx;
3032 cpuid_count(XSTATE_CPUID, index,
3033 &size, &offset, &ecx, &edx);
3034 memcpy(dest + offset, src, size);
3035 }
3036
3037 valid -= feature;
3038 }
3039}
3040
3041static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3042{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003043 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003044 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3045 u64 valid;
3046
3047 /*
3048 * Copy legacy XSAVE area, to avoid complications with CPUID
3049 * leaves 0 and 1 in the loop below.
3050 */
3051 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3052
3053 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003054 xsave->header.xfeatures = xstate_bv;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003055 if (cpu_has_xsaves)
Ingo Molnar3a544502015-04-24 10:14:36 +02003056 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003057
3058 /*
3059 * Copy each region from the non-compacted offset to the
3060 * possibly compacted offset.
3061 */
3062 valid = xstate_bv & ~XSTATE_FPSSE;
3063 while (valid) {
3064 u64 feature = valid & -valid;
3065 int index = fls64(feature) - 1;
3066 void *dest = get_xsave_addr(xsave, feature);
3067
3068 if (dest) {
3069 u32 size, offset, ecx, edx;
3070 cpuid_count(XSTATE_CPUID, index,
3071 &size, &offset, &ecx, &edx);
3072 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08003073 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003074
3075 valid -= feature;
3076 }
3077}
3078
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003079static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3080 struct kvm_xsave *guest_xsave)
3081{
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003082 if (cpu_has_xsave) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003083 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3084 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003085 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003086 memcpy(guest_xsave->region,
Ingo Molnar7366ed72015-04-27 04:19:39 +02003087 &vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003088 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003089 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
3090 XSTATE_FPSSE;
3091 }
3092}
3093
3094static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3095 struct kvm_xsave *guest_xsave)
3096{
3097 u64 xstate_bv =
3098 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
3099
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003100 if (cpu_has_xsave) {
3101 /*
3102 * Here we allow setting states that are not present in
3103 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3104 * with old userspace.
3105 */
Paolo Bonzini4ff41732014-02-24 12:15:16 +01003106 if (xstate_bv & ~kvm_supported_xcr0())
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003107 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003108 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003109 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003110 if (xstate_bv & ~XSTATE_FPSSE)
3111 return -EINVAL;
Ingo Molnar7366ed72015-04-27 04:19:39 +02003112 memcpy(&vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003113 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003114 }
3115 return 0;
3116}
3117
3118static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3119 struct kvm_xcrs *guest_xcrs)
3120{
3121 if (!cpu_has_xsave) {
3122 guest_xcrs->nr_xcrs = 0;
3123 return;
3124 }
3125
3126 guest_xcrs->nr_xcrs = 1;
3127 guest_xcrs->flags = 0;
3128 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3129 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3130}
3131
3132static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3133 struct kvm_xcrs *guest_xcrs)
3134{
3135 int i, r = 0;
3136
3137 if (!cpu_has_xsave)
3138 return -EINVAL;
3139
3140 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3141 return -EINVAL;
3142
3143 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3144 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003145 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003146 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003147 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003148 break;
3149 }
3150 if (r)
3151 r = -EINVAL;
3152 return r;
3153}
3154
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003155/*
3156 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3157 * stopped by the hypervisor. This function will be called from the host only.
3158 * EINVAL is returned when the host attempts to set the flag for a guest that
3159 * does not support pv clocks.
3160 */
3161static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3162{
Andy Honig0b794592013-02-20 14:48:10 -08003163 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003164 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003165 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003166 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3167 return 0;
3168}
3169
Carsten Otte313a3dc2007-10-11 19:16:52 +02003170long kvm_arch_vcpu_ioctl(struct file *filp,
3171 unsigned int ioctl, unsigned long arg)
3172{
3173 struct kvm_vcpu *vcpu = filp->private_data;
3174 void __user *argp = (void __user *)arg;
3175 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003176 union {
3177 struct kvm_lapic_state *lapic;
3178 struct kvm_xsave *xsave;
3179 struct kvm_xcrs *xcrs;
3180 void *buffer;
3181 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003182
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003183 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003184 switch (ioctl) {
3185 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003186 r = -EINVAL;
3187 if (!vcpu->arch.apic)
3188 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003189 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003190
Dave Hansenb772ff32008-08-11 10:01:47 -07003191 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003192 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003193 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003194 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003195 if (r)
3196 goto out;
3197 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003198 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003199 goto out;
3200 r = 0;
3201 break;
3202 }
3203 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003204 r = -EINVAL;
3205 if (!vcpu->arch.apic)
3206 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003207 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Guo Chao18595412012-11-02 18:33:21 +08003208 if (IS_ERR(u.lapic))
3209 return PTR_ERR(u.lapic);
Sasha Levinff5c2c02011-12-04 19:36:29 +02003210
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003211 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003212 break;
3213 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003214 case KVM_INTERRUPT: {
3215 struct kvm_interrupt irq;
3216
3217 r = -EFAULT;
3218 if (copy_from_user(&irq, argp, sizeof irq))
3219 goto out;
3220 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003221 break;
3222 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003223 case KVM_NMI: {
3224 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003225 break;
3226 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003227 case KVM_SMI: {
3228 r = kvm_vcpu_ioctl_smi(vcpu);
3229 break;
3230 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003231 case KVM_SET_CPUID: {
3232 struct kvm_cpuid __user *cpuid_arg = argp;
3233 struct kvm_cpuid cpuid;
3234
3235 r = -EFAULT;
3236 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3237 goto out;
3238 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003239 break;
3240 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003241 case KVM_SET_CPUID2: {
3242 struct kvm_cpuid2 __user *cpuid_arg = argp;
3243 struct kvm_cpuid2 cpuid;
3244
3245 r = -EFAULT;
3246 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3247 goto out;
3248 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003249 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003250 break;
3251 }
3252 case KVM_GET_CPUID2: {
3253 struct kvm_cpuid2 __user *cpuid_arg = argp;
3254 struct kvm_cpuid2 cpuid;
3255
3256 r = -EFAULT;
3257 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3258 goto out;
3259 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003260 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003261 if (r)
3262 goto out;
3263 r = -EFAULT;
3264 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3265 goto out;
3266 r = 0;
3267 break;
3268 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003269 case KVM_GET_MSRS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003270 r = msr_io(vcpu, argp, do_get_msr, 1);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003271 break;
3272 case KVM_SET_MSRS:
3273 r = msr_io(vcpu, argp, do_set_msr, 0);
3274 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02003275 case KVM_TPR_ACCESS_REPORTING: {
3276 struct kvm_tpr_access_ctl tac;
3277
3278 r = -EFAULT;
3279 if (copy_from_user(&tac, argp, sizeof tac))
3280 goto out;
3281 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3282 if (r)
3283 goto out;
3284 r = -EFAULT;
3285 if (copy_to_user(argp, &tac, sizeof tac))
3286 goto out;
3287 r = 0;
3288 break;
3289 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003290 case KVM_SET_VAPIC_ADDR: {
3291 struct kvm_vapic_addr va;
3292
3293 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02003294 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02003295 goto out;
3296 r = -EFAULT;
3297 if (copy_from_user(&va, argp, sizeof va))
3298 goto out;
Andy Honigfda4e2e2013-11-20 10:23:22 -08003299 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Avi Kivityb93463a2007-10-25 16:52:32 +02003300 break;
3301 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003302 case KVM_X86_SETUP_MCE: {
3303 u64 mcg_cap;
3304
3305 r = -EFAULT;
3306 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3307 goto out;
3308 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3309 break;
3310 }
3311 case KVM_X86_SET_MCE: {
3312 struct kvm_x86_mce mce;
3313
3314 r = -EFAULT;
3315 if (copy_from_user(&mce, argp, sizeof mce))
3316 goto out;
3317 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3318 break;
3319 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003320 case KVM_GET_VCPU_EVENTS: {
3321 struct kvm_vcpu_events events;
3322
3323 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3324
3325 r = -EFAULT;
3326 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3327 break;
3328 r = 0;
3329 break;
3330 }
3331 case KVM_SET_VCPU_EVENTS: {
3332 struct kvm_vcpu_events events;
3333
3334 r = -EFAULT;
3335 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3336 break;
3337
3338 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3339 break;
3340 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003341 case KVM_GET_DEBUGREGS: {
3342 struct kvm_debugregs dbgregs;
3343
3344 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3345
3346 r = -EFAULT;
3347 if (copy_to_user(argp, &dbgregs,
3348 sizeof(struct kvm_debugregs)))
3349 break;
3350 r = 0;
3351 break;
3352 }
3353 case KVM_SET_DEBUGREGS: {
3354 struct kvm_debugregs dbgregs;
3355
3356 r = -EFAULT;
3357 if (copy_from_user(&dbgregs, argp,
3358 sizeof(struct kvm_debugregs)))
3359 break;
3360
3361 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3362 break;
3363 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003364 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003365 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003366 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003367 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003368 break;
3369
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003370 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003371
3372 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003373 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003374 break;
3375 r = 0;
3376 break;
3377 }
3378 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003379 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Guo Chao18595412012-11-02 18:33:21 +08003380 if (IS_ERR(u.xsave))
3381 return PTR_ERR(u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003382
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003383 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003384 break;
3385 }
3386 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003387 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003388 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003389 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003390 break;
3391
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003392 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003393
3394 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003395 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003396 sizeof(struct kvm_xcrs)))
3397 break;
3398 r = 0;
3399 break;
3400 }
3401 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003402 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Guo Chao18595412012-11-02 18:33:21 +08003403 if (IS_ERR(u.xcrs))
3404 return PTR_ERR(u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003405
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003406 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003407 break;
3408 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003409 case KVM_SET_TSC_KHZ: {
3410 u32 user_tsc_khz;
3411
3412 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003413 user_tsc_khz = (u32)arg;
3414
3415 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3416 goto out;
3417
Zachary Amsdencc578282012-02-03 15:43:50 -02003418 if (user_tsc_khz == 0)
3419 user_tsc_khz = tsc_khz;
3420
Haozhong Zhang381d5852015-10-20 15:39:04 +08003421 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
3422 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003423
Joerg Roedel92a1f122011-03-25 09:44:51 +01003424 goto out;
3425 }
3426 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003427 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003428 goto out;
3429 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003430 case KVM_KVMCLOCK_CTRL: {
3431 r = kvm_set_guest_paused(vcpu);
3432 goto out;
3433 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003434 default:
3435 r = -EINVAL;
3436 }
3437out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003438 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003439 return r;
3440}
3441
Carsten Otte5b1c1492012-01-04 10:25:23 +01003442int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
3443{
3444 return VM_FAULT_SIGBUS;
3445}
3446
Carsten Otte1fe779f2007-10-29 16:08:35 +01003447static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
3448{
3449 int ret;
3450
3451 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08003452 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003453 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
3454 return ret;
3455}
3456
Sheng Yangb927a3c2009-07-21 10:42:48 +08003457static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
3458 u64 ident_addr)
3459{
3460 kvm->arch.ept_identity_map_addr = ident_addr;
3461 return 0;
3462}
3463
Carsten Otte1fe779f2007-10-29 16:08:35 +01003464static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
3465 u32 kvm_nr_mmu_pages)
3466{
3467 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
3468 return -EINVAL;
3469
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003470 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003471
3472 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003473 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003474
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003475 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003476 return 0;
3477}
3478
3479static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
3480{
Dave Hansen39de71e2010-08-19 18:11:14 -07003481 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003482}
3483
Carsten Otte1fe779f2007-10-29 16:08:35 +01003484static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3485{
3486 int r;
3487
3488 r = 0;
3489 switch (chip->chip_id) {
3490 case KVM_IRQCHIP_PIC_MASTER:
3491 memcpy(&chip->chip.pic,
3492 &pic_irqchip(kvm)->pics[0],
3493 sizeof(struct kvm_pic_state));
3494 break;
3495 case KVM_IRQCHIP_PIC_SLAVE:
3496 memcpy(&chip->chip.pic,
3497 &pic_irqchip(kvm)->pics[1],
3498 sizeof(struct kvm_pic_state));
3499 break;
3500 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003501 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003502 break;
3503 default:
3504 r = -EINVAL;
3505 break;
3506 }
3507 return r;
3508}
3509
3510static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3511{
3512 int r;
3513
3514 r = 0;
3515 switch (chip->chip_id) {
3516 case KVM_IRQCHIP_PIC_MASTER:
Avi Kivityf4f51052010-09-19 18:44:07 +02003517 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003518 memcpy(&pic_irqchip(kvm)->pics[0],
3519 &chip->chip.pic,
3520 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003521 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003522 break;
3523 case KVM_IRQCHIP_PIC_SLAVE:
Avi Kivityf4f51052010-09-19 18:44:07 +02003524 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003525 memcpy(&pic_irqchip(kvm)->pics[1],
3526 &chip->chip.pic,
3527 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003528 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003529 break;
3530 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003531 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003532 break;
3533 default:
3534 r = -EINVAL;
3535 break;
3536 }
3537 kvm_pic_update_irq(pic_irqchip(kvm));
3538 return r;
3539}
3540
Sheng Yange0f63cb2008-03-04 00:50:59 +08003541static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3542{
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003543 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003544 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003545 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303546 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08003547}
3548
3549static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3550{
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003551 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003552 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04003553 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
3554 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303555 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04003556}
3557
3558static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3559{
Beth Kone9f42752009-07-07 11:50:38 -04003560 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3561 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
3562 sizeof(ps->channels));
3563 ps->flags = kvm->arch.vpit->pit_state.flags;
3564 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003565 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303566 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04003567}
3568
3569static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3570{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303571 int start = 0;
Beth Kone9f42752009-07-07 11:50:38 -04003572 u32 prev_legacy, cur_legacy;
3573 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3574 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
3575 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
3576 if (!prev_legacy && cur_legacy)
3577 start = 1;
3578 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
3579 sizeof(kvm->arch.vpit->pit_state.channels));
3580 kvm->arch.vpit->pit_state.flags = ps->flags;
3581 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003582 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303583 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08003584}
3585
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003586static int kvm_vm_ioctl_reinject(struct kvm *kvm,
3587 struct kvm_reinject_control *control)
3588{
3589 if (!kvm->arch.vpit)
3590 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003591 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Avi Kivity26ef1922012-07-26 18:01:53 +03003592 kvm->arch.vpit->pit_state.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003593 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003594 return 0;
3595}
3596
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003597/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003598 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
3599 * @kvm: kvm instance
3600 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003601 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003602 * Steps 1-4 below provide general overview of dirty page logging. See
3603 * kvm_get_dirty_log_protect() function description for additional details.
3604 *
3605 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
3606 * always flush the TLB (step 4) even if previous step failed and the dirty
3607 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
3608 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
3609 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003610 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003611 * 1. Take a snapshot of the bit and clear it if needed.
3612 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003613 * 3. Copy the snapshot to the userspace.
3614 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003615 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003616int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003617{
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003618 bool is_dirty = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003619 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003620
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003621 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003622
Kai Huang88178fd2015-01-28 10:54:27 +08003623 /*
3624 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
3625 */
3626 if (kvm_x86_ops->flush_log_dirty)
3627 kvm_x86_ops->flush_log_dirty(kvm);
3628
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003629 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003630
3631 /*
3632 * All the TLBs can be flushed out of mmu lock, see the comments in
3633 * kvm_mmu_slot_remove_write_access().
3634 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003635 lockdep_assert_held(&kvm->slots_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003636 if (is_dirty)
3637 kvm_flush_remote_tlbs(kvm);
3638
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003639 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003640 return r;
3641}
3642
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003643int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
3644 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003645{
3646 if (!irqchip_in_kernel(kvm))
3647 return -ENXIO;
3648
3649 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003650 irq_event->irq, irq_event->level,
3651 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003652 return 0;
3653}
3654
Nadav Amit90de4a12015-04-13 01:53:41 +03003655static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3656 struct kvm_enable_cap *cap)
3657{
3658 int r;
3659
3660 if (cap->flags)
3661 return -EINVAL;
3662
3663 switch (cap->cap) {
3664 case KVM_CAP_DISABLE_QUIRKS:
3665 kvm->arch.disabled_quirks = cap->args[0];
3666 r = 0;
3667 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07003668 case KVM_CAP_SPLIT_IRQCHIP: {
3669 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07003670 r = -EINVAL;
3671 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
3672 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07003673 r = -EEXIST;
3674 if (irqchip_in_kernel(kvm))
3675 goto split_irqchip_unlock;
3676 if (atomic_read(&kvm->online_vcpus))
3677 goto split_irqchip_unlock;
3678 r = kvm_setup_empty_irq_routing(kvm);
3679 if (r)
3680 goto split_irqchip_unlock;
3681 /* Pairs with irqchip_in_kernel. */
3682 smp_wmb();
3683 kvm->arch.irqchip_split = true;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07003684 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07003685 r = 0;
3686split_irqchip_unlock:
3687 mutex_unlock(&kvm->lock);
3688 break;
3689 }
Nadav Amit90de4a12015-04-13 01:53:41 +03003690 default:
3691 r = -EINVAL;
3692 break;
3693 }
3694 return r;
3695}
3696
Carsten Otte1fe779f2007-10-29 16:08:35 +01003697long kvm_arch_vm_ioctl(struct file *filp,
3698 unsigned int ioctl, unsigned long arg)
3699{
3700 struct kvm *kvm = filp->private_data;
3701 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03003702 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07003703 /*
3704 * This union makes it completely explicit to gcc-3.x
3705 * that these two variables' stack usage should be
3706 * combined, not added together.
3707 */
3708 union {
3709 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04003710 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003711 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07003712 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003713
3714 switch (ioctl) {
3715 case KVM_SET_TSS_ADDR:
3716 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003717 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003718 case KVM_SET_IDENTITY_MAP_ADDR: {
3719 u64 ident_addr;
3720
3721 r = -EFAULT;
3722 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
3723 goto out;
3724 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08003725 break;
3726 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003727 case KVM_SET_NR_MMU_PAGES:
3728 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003729 break;
3730 case KVM_GET_NR_MMU_PAGES:
3731 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3732 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003733 case KVM_CREATE_IRQCHIP: {
3734 struct kvm_pic *vpic;
3735
3736 mutex_lock(&kvm->lock);
3737 r = -EEXIST;
3738 if (kvm->arch.vpic)
3739 goto create_irqchip_unlock;
Avi Kivity3e515702012-03-05 14:23:29 +02003740 r = -EINVAL;
3741 if (atomic_read(&kvm->online_vcpus))
3742 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003743 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003744 vpic = kvm_create_pic(kvm);
3745 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003746 r = kvm_ioapic_init(kvm);
3747 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003748 mutex_lock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003749 kvm_destroy_pic(vpic);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003750 mutex_unlock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003751 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003752 }
3753 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003754 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02003755 r = kvm_setup_default_irq_routing(kvm);
3756 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003757 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003758 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003759 kvm_ioapic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003760 kvm_destroy_pic(vpic);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003761 mutex_unlock(&kvm->irq_lock);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003762 mutex_unlock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003763 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02003764 }
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003765 /* Write kvm->irq_routing before kvm->arch.vpic. */
3766 smp_wmb();
3767 kvm->arch.vpic = vpic;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003768 create_irqchip_unlock:
3769 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003770 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003771 }
Sheng Yang78376992008-01-28 05:10:22 +08003772 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003773 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3774 goto create_pit;
3775 case KVM_CREATE_PIT2:
3776 r = -EFAULT;
3777 if (copy_from_user(&u.pit_config, argp,
3778 sizeof(struct kvm_pit_config)))
3779 goto out;
3780 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003781 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003782 r = -EEXIST;
3783 if (kvm->arch.vpit)
3784 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003785 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003786 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003787 if (kvm->arch.vpit)
3788 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003789 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003790 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08003791 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003792 case KVM_GET_IRQCHIP: {
3793 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003794 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003795
Sasha Levinff5c2c02011-12-04 19:36:29 +02003796 chip = memdup_user(argp, sizeof(*chip));
3797 if (IS_ERR(chip)) {
3798 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003799 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003800 }
3801
Carsten Otte1fe779f2007-10-29 16:08:35 +01003802 r = -ENXIO;
Steve Rutherford49df6392015-07-29 23:21:40 -07003803 if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003804 goto get_irqchip_out;
3805 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3806 if (r)
3807 goto get_irqchip_out;
3808 r = -EFAULT;
3809 if (copy_to_user(argp, chip, sizeof *chip))
3810 goto get_irqchip_out;
3811 r = 0;
3812 get_irqchip_out:
3813 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003814 break;
3815 }
3816 case KVM_SET_IRQCHIP: {
3817 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003818 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003819
Sasha Levinff5c2c02011-12-04 19:36:29 +02003820 chip = memdup_user(argp, sizeof(*chip));
3821 if (IS_ERR(chip)) {
3822 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003823 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003824 }
3825
Carsten Otte1fe779f2007-10-29 16:08:35 +01003826 r = -ENXIO;
Steve Rutherford49df6392015-07-29 23:21:40 -07003827 if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003828 goto set_irqchip_out;
3829 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3830 if (r)
3831 goto set_irqchip_out;
3832 r = 0;
3833 set_irqchip_out:
3834 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003835 break;
3836 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003837 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003838 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003839 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003840 goto out;
3841 r = -ENXIO;
3842 if (!kvm->arch.vpit)
3843 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003844 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003845 if (r)
3846 goto out;
3847 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003848 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003849 goto out;
3850 r = 0;
3851 break;
3852 }
3853 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003854 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003855 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003856 goto out;
3857 r = -ENXIO;
3858 if (!kvm->arch.vpit)
3859 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003860 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003861 break;
3862 }
Beth Kone9f42752009-07-07 11:50:38 -04003863 case KVM_GET_PIT2: {
3864 r = -ENXIO;
3865 if (!kvm->arch.vpit)
3866 goto out;
3867 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3868 if (r)
3869 goto out;
3870 r = -EFAULT;
3871 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3872 goto out;
3873 r = 0;
3874 break;
3875 }
3876 case KVM_SET_PIT2: {
3877 r = -EFAULT;
3878 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3879 goto out;
3880 r = -ENXIO;
3881 if (!kvm->arch.vpit)
3882 goto out;
3883 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04003884 break;
3885 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003886 case KVM_REINJECT_CONTROL: {
3887 struct kvm_reinject_control control;
3888 r = -EFAULT;
3889 if (copy_from_user(&control, argp, sizeof(control)))
3890 goto out;
3891 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003892 break;
3893 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003894 case KVM_SET_BOOT_CPU_ID:
3895 r = 0;
3896 mutex_lock(&kvm->lock);
3897 if (atomic_read(&kvm->online_vcpus) != 0)
3898 r = -EBUSY;
3899 else
3900 kvm->arch.bsp_vcpu_id = arg;
3901 mutex_unlock(&kvm->lock);
3902 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07003903 case KVM_XEN_HVM_CONFIG: {
3904 r = -EFAULT;
3905 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3906 sizeof(struct kvm_xen_hvm_config)))
3907 goto out;
3908 r = -EINVAL;
3909 if (kvm->arch.xen_hvm_config.flags)
3910 goto out;
3911 r = 0;
3912 break;
3913 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003914 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003915 struct kvm_clock_data user_ns;
3916 u64 now_ns;
3917 s64 delta;
3918
3919 r = -EFAULT;
3920 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3921 goto out;
3922
3923 r = -EINVAL;
3924 if (user_ns.flags)
3925 goto out;
3926
3927 r = 0;
Avi Kivity395c6b02010-10-04 12:55:49 +02003928 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003929 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003930 delta = user_ns.clock - now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003931 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003932 kvm->arch.kvmclock_offset = delta;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03003933 kvm_gen_update_masterclock(kvm);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003934 break;
3935 }
3936 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003937 struct kvm_clock_data user_ns;
3938 u64 now_ns;
3939
Avi Kivity395c6b02010-10-04 12:55:49 +02003940 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003941 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003942 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003943 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003944 user_ns.flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003945 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003946
3947 r = -EFAULT;
3948 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3949 goto out;
3950 r = 0;
3951 break;
3952 }
Nadav Amit90de4a12015-04-13 01:53:41 +03003953 case KVM_ENABLE_CAP: {
3954 struct kvm_enable_cap cap;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003955
Nadav Amit90de4a12015-04-13 01:53:41 +03003956 r = -EFAULT;
3957 if (copy_from_user(&cap, argp, sizeof(cap)))
3958 goto out;
3959 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
3960 break;
3961 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003962 default:
Radim Krčmářc274e032014-11-21 22:21:50 +01003963 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003964 }
3965out:
3966 return r;
3967}
3968
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003969static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003970{
3971 u32 dummy[2];
3972 unsigned i, j;
3973
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003974 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003975 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3976 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003977
3978 /*
3979 * Even MSRs that are valid in the host may not be exposed
3980 * to the guests in some cases. We could work around this
3981 * in VMX with the generic MSR save/load machinery, but it
3982 * is not really worthwhile since it will really only
3983 * happen with nested virtualization.
3984 */
3985 switch (msrs_to_save[i]) {
3986 case MSR_IA32_BNDCFGS:
3987 if (!kvm_x86_ops->mpx_supported())
3988 continue;
3989 break;
3990 default:
3991 break;
3992 }
3993
Carsten Otte043405e2007-10-10 17:16:19 +02003994 if (j < i)
3995 msrs_to_save[j] = msrs_to_save[i];
3996 j++;
3997 }
3998 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003999
4000 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
4001 switch (emulated_msrs[i]) {
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004002 case MSR_IA32_SMBASE:
4003 if (!kvm_x86_ops->cpu_has_high_real_mode_segbase())
4004 continue;
4005 break;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004006 default:
4007 break;
4008 }
4009
4010 if (j < i)
4011 emulated_msrs[j] = emulated_msrs[i];
4012 j++;
4013 }
4014 num_emulated_msrs = j;
Carsten Otte043405e2007-10-10 17:16:19 +02004015}
4016
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004017static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
4018 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004019{
Avi Kivity70252a12010-01-19 12:51:22 +02004020 int handled = 0;
4021 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004022
Avi Kivity70252a12010-01-19 12:51:22 +02004023 do {
4024 n = min(len, 8);
4025 if (!(vcpu->arch.apic &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004026 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
4027 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004028 break;
4029 handled += n;
4030 addr += n;
4031 len -= n;
4032 v += n;
4033 } while (len);
4034
4035 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004036}
4037
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004038static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004039{
Avi Kivity70252a12010-01-19 12:51:22 +02004040 int handled = 0;
4041 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004042
Avi Kivity70252a12010-01-19 12:51:22 +02004043 do {
4044 n = min(len, 8);
4045 if (!(vcpu->arch.apic &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004046 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
4047 addr, n, v))
4048 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004049 break;
4050 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
4051 handled += n;
4052 addr += n;
4053 len -= n;
4054 v += n;
4055 } while (len);
4056
4057 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004058}
4059
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004060static void kvm_set_segment(struct kvm_vcpu *vcpu,
4061 struct kvm_segment *var, int seg)
4062{
4063 kvm_x86_ops->set_segment(vcpu, var, seg);
4064}
4065
4066void kvm_get_segment(struct kvm_vcpu *vcpu,
4067 struct kvm_segment *var, int seg)
4068{
4069 kvm_x86_ops->get_segment(vcpu, var, seg);
4070}
4071
Paolo Bonzini54987b72014-09-02 13:23:06 +02004072gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
4073 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004074{
4075 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004076
4077 BUG_ON(!mmu_is_nested(vcpu));
4078
4079 /* NPT walks are always user-walks */
4080 access |= PFERR_USER_MASK;
Paolo Bonzini54987b72014-09-02 13:23:06 +02004081 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004082
4083 return t_gpa;
4084}
4085
Avi Kivityab9ae312010-11-22 17:53:26 +02004086gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
4087 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004088{
4089 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02004090 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004091}
4092
Avi Kivityab9ae312010-11-22 17:53:26 +02004093 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4094 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004095{
4096 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4097 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004098 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004099}
4100
Avi Kivityab9ae312010-11-22 17:53:26 +02004101gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4102 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004103{
4104 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4105 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004106 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004107}
4108
4109/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004110gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4111 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004112{
Avi Kivityab9ae312010-11-22 17:53:26 +02004113 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004114}
4115
4116static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4117 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004118 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004119{
4120 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004121 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004122
4123 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004124 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004125 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004126 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004127 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004128 int ret;
4129
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004130 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004131 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004132 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
4133 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004134 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004135 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004136 goto out;
4137 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004138
Izik Eidus77c20022008-12-29 01:42:19 +02004139 bytes -= toread;
4140 data += toread;
4141 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004142 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004143out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004144 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004145}
Izik Eidus77c20022008-12-29 01:42:19 +02004146
Gleb Natapov1871c602010-02-10 14:21:32 +02004147/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004148static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4149 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004150 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004151{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004152 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004153 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004154 unsigned offset;
4155 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004156
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004157 /* Inline kvm_read_guest_virt_helper for speed. */
4158 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4159 exception);
4160 if (unlikely(gpa == UNMAPPED_GVA))
4161 return X86EMUL_PROPAGATE_FAULT;
4162
4163 offset = addr & (PAGE_SIZE-1);
4164 if (WARN_ON(offset + bytes > PAGE_SIZE))
4165 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004166 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
4167 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004168 if (unlikely(ret < 0))
4169 return X86EMUL_IO_NEEDED;
4170
4171 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004172}
4173
Nadav Har'El064aea72011-05-25 23:04:56 +03004174int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004175 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004176 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004177{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004178 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004179 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004180
Gleb Natapov1871c602010-02-10 14:21:32 +02004181 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004182 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004183}
Nadav Har'El064aea72011-05-25 23:04:56 +03004184EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004185
Avi Kivity0f65dd72011-04-20 13:37:53 +03004186static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
4187 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004188 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004189{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004190 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004191 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004192}
4193
Radim Krčmář7a036a62015-10-30 16:36:24 +01004194static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
4195 unsigned long addr, void *val, unsigned int bytes)
4196{
4197 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4198 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
4199
4200 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
4201}
4202
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004203int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004204 gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004205 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004206 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004207{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004208 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Izik Eidus77c20022008-12-29 01:42:19 +02004209 void *data = val;
4210 int r = X86EMUL_CONTINUE;
4211
4212 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004213 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
4214 PFERR_WRITE_MASK,
Avi Kivityab9ae312010-11-22 17:53:26 +02004215 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004216 unsigned offset = addr & (PAGE_SIZE-1);
4217 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4218 int ret;
4219
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004220 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004221 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004222 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02004223 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004224 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004225 goto out;
4226 }
4227
4228 bytes -= towrite;
4229 data += towrite;
4230 addr += towrite;
4231 }
4232out:
4233 return r;
4234}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004235EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004236
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004237static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
4238 gpa_t *gpa, struct x86_exception *exception,
4239 bool write)
4240{
Avi Kivity97d64b72012-09-12 14:52:00 +03004241 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
4242 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004243
Avi Kivity97d64b72012-09-12 14:52:00 +03004244 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08004245 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
4246 vcpu->arch.access, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004247 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
4248 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08004249 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004250 return 1;
4251 }
4252
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004253 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
4254
4255 if (*gpa == UNMAPPED_GVA)
4256 return -1;
4257
4258 /* For APIC access vmexit */
4259 if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4260 return 1;
4261
Xiao Guangrong4f022642011-07-12 03:34:24 +08004262 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
4263 trace_vcpu_match_mmio(gva, *gpa, write, true);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004264 return 1;
Xiao Guangrong4f022642011-07-12 03:34:24 +08004265 }
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004266
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004267 return 0;
4268}
4269
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004270int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004271 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02004272{
4273 int ret;
4274
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004275 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004276 if (ret < 0)
4277 return 0;
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004278 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004279 return 1;
4280}
4281
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004282struct read_write_emulator_ops {
4283 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
4284 int bytes);
4285 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
4286 void *val, int bytes);
4287 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4288 int bytes, void *val);
4289 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4290 void *val, int bytes);
4291 bool write;
4292};
4293
4294static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
4295{
4296 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004297 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Avi Kivityf78146b2012-04-18 19:22:47 +03004298 vcpu->mmio_fragments[0].gpa, *(u64 *)val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004299 vcpu->mmio_read_completed = 0;
4300 return 1;
4301 }
4302
4303 return 0;
4304}
4305
4306static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4307 void *val, int bytes)
4308{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004309 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004310}
4311
4312static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4313 void *val, int bytes)
4314{
4315 return emulator_write_phys(vcpu, gpa, val, bytes);
4316}
4317
4318static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
4319{
4320 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4321 return vcpu_mmio_write(vcpu, gpa, bytes, val);
4322}
4323
4324static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4325 void *val, int bytes)
4326{
4327 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4328 return X86EMUL_IO_NEEDED;
4329}
4330
4331static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4332 void *val, int bytes)
4333{
Avi Kivityf78146b2012-04-18 19:22:47 +03004334 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
4335
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004336 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004337 return X86EMUL_CONTINUE;
4338}
4339
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004340static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004341 .read_write_prepare = read_prepare,
4342 .read_write_emulate = read_emulate,
4343 .read_write_mmio = vcpu_mmio_read,
4344 .read_write_exit_mmio = read_exit_mmio,
4345};
4346
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004347static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004348 .read_write_emulate = write_emulate,
4349 .read_write_mmio = write_mmio,
4350 .read_write_exit_mmio = write_exit_mmio,
4351 .write = true,
4352};
4353
Xiao Guangrong22388a32011-07-13 14:32:31 +08004354static int emulator_read_write_onepage(unsigned long addr, void *val,
4355 unsigned int bytes,
4356 struct x86_exception *exception,
4357 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004358 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004359{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004360 gpa_t gpa;
4361 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004362 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03004363 struct kvm_mmio_fragment *frag;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004364
4365 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004366
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004367 if (ret < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004368 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004369
4370 /* For APIC access vmexit */
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004371 if (ret)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004372 goto mmio;
4373
Xiao Guangrong22388a32011-07-13 14:32:31 +08004374 if (ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004375 return X86EMUL_CONTINUE;
4376
4377mmio:
4378 /*
4379 * Is this MMIO handled locally?
4380 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08004381 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02004382 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004383 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004384
Avi Kivity70252a12010-01-19 12:51:22 +02004385 gpa += handled;
4386 bytes -= handled;
4387 val += handled;
4388
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004389 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
4390 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
4391 frag->gpa = gpa;
4392 frag->data = val;
4393 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03004394 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004395}
4396
Xiubo Li52eb5a62015-03-13 17:39:45 +08004397static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
4398 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004399 void *val, unsigned int bytes,
4400 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004401 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004402{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004403 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03004404 gpa_t gpa;
4405 int rc;
4406
4407 if (ops->read_write_prepare &&
4408 ops->read_write_prepare(vcpu, val, bytes))
4409 return X86EMUL_CONTINUE;
4410
4411 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004412
Carsten Ottebbd9b642007-10-30 18:44:21 +01004413 /* Crossing a page boundary? */
4414 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03004415 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004416
4417 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004418 rc = emulator_read_write_onepage(addr, val, now, exception,
4419 vcpu, ops);
4420
Carsten Ottebbd9b642007-10-30 18:44:21 +01004421 if (rc != X86EMUL_CONTINUE)
4422 return rc;
4423 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02004424 if (ctxt->mode != X86EMUL_MODE_PROT64)
4425 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004426 val += now;
4427 bytes -= now;
4428 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08004429
Avi Kivityf78146b2012-04-18 19:22:47 +03004430 rc = emulator_read_write_onepage(addr, val, bytes, exception,
4431 vcpu, ops);
4432 if (rc != X86EMUL_CONTINUE)
4433 return rc;
4434
4435 if (!vcpu->mmio_nr_fragments)
4436 return rc;
4437
4438 gpa = vcpu->mmio_fragments[0].gpa;
4439
4440 vcpu->mmio_needed = 1;
4441 vcpu->mmio_cur_fragment = 0;
4442
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004443 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03004444 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
4445 vcpu->run->exit_reason = KVM_EXIT_MMIO;
4446 vcpu->run->mmio.phys_addr = gpa;
4447
4448 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08004449}
4450
4451static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4452 unsigned long addr,
4453 void *val,
4454 unsigned int bytes,
4455 struct x86_exception *exception)
4456{
4457 return emulator_read_write(ctxt, addr, val, bytes,
4458 exception, &read_emultor);
4459}
4460
Xiubo Li52eb5a62015-03-13 17:39:45 +08004461static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004462 unsigned long addr,
4463 const void *val,
4464 unsigned int bytes,
4465 struct x86_exception *exception)
4466{
4467 return emulator_read_write(ctxt, addr, (void *)val, bytes,
4468 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004469}
Carsten Ottebbd9b642007-10-30 18:44:21 +01004470
Avi Kivitydaea3e72010-03-15 13:59:54 +02004471#define CMPXCHG_TYPE(t, ptr, old, new) \
4472 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
4473
4474#ifdef CONFIG_X86_64
4475# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
4476#else
4477# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01004478 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02004479#endif
4480
Avi Kivity0f65dd72011-04-20 13:37:53 +03004481static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
4482 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004483 const void *old,
4484 const void *new,
4485 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004486 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004487{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004488 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004489 gpa_t gpa;
4490 struct page *page;
4491 char *kaddr;
4492 bool exchanged;
4493
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004494 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02004495 if (bytes > 8 || (bytes & (bytes - 1)))
4496 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004497
Avi Kivitydaea3e72010-03-15 13:59:54 +02004498 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004499
Avi Kivitydaea3e72010-03-15 13:59:54 +02004500 if (gpa == UNMAPPED_GVA ||
4501 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4502 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004503
Avi Kivitydaea3e72010-03-15 13:59:54 +02004504 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
4505 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004506
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004507 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08004508 if (is_error_page(page))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08004509 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004510
Cong Wang8fd75e12011-11-25 23:14:17 +08004511 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004512 kaddr += offset_in_page(gpa);
4513 switch (bytes) {
4514 case 1:
4515 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
4516 break;
4517 case 2:
4518 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
4519 break;
4520 case 4:
4521 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
4522 break;
4523 case 8:
4524 exchanged = CMPXCHG64(kaddr, old, new);
4525 break;
4526 default:
4527 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004528 }
Cong Wang8fd75e12011-11-25 23:14:17 +08004529 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004530 kvm_release_page_dirty(page);
4531
4532 if (!exchanged)
4533 return X86EMUL_CMPXCHG_FAILED;
4534
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004535 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004536 kvm_mmu_pte_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004537
4538 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02004539
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004540emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02004541 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004542
Avi Kivity0f65dd72011-04-20 13:37:53 +03004543 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004544}
4545
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004546static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
4547{
4548 /* TODO: String I/O for in kernel device */
4549 int r;
4550
4551 if (vcpu->arch.pio.in)
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004552 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004553 vcpu->arch.pio.size, pd);
4554 else
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004555 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004556 vcpu->arch.pio.port, vcpu->arch.pio.size,
4557 pd);
4558 return r;
4559}
4560
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004561static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
4562 unsigned short port, void *val,
4563 unsigned int count, bool in)
4564{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004565 vcpu->arch.pio.port = port;
4566 vcpu->arch.pio.in = in;
4567 vcpu->arch.pio.count = count;
4568 vcpu->arch.pio.size = size;
4569
4570 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
4571 vcpu->arch.pio.count = 0;
4572 return 1;
4573 }
4574
4575 vcpu->run->exit_reason = KVM_EXIT_IO;
4576 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
4577 vcpu->run->io.size = size;
4578 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
4579 vcpu->run->io.count = count;
4580 vcpu->run->io.port = port;
4581
4582 return 0;
4583}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004584
Avi Kivityca1d4a92011-04-20 13:37:53 +03004585static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
4586 int size, unsigned short port, void *val,
4587 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004588{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004589 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004590 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03004591
Gleb Natapov79729952010-03-18 15:20:24 +02004592 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004593 goto data_avail;
4594
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004595 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
4596 if (ret) {
4597data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004598 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004599 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02004600 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004601 return 1;
4602 }
4603
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004604 return 0;
4605}
4606
Avi Kivityca1d4a92011-04-20 13:37:53 +03004607static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
4608 int size, unsigned short port,
4609 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004610{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004611 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4612
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004613 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004614 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004615 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004616}
4617
Carsten Ottebbd9b642007-10-30 18:44:21 +01004618static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
4619{
4620 return kvm_x86_ops->get_segment_base(vcpu, seg);
4621}
4622
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004623static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004624{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004625 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004626}
4627
Joel Schopp5cb56052015-03-02 13:43:31 -06004628int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004629{
4630 if (!need_emulate_wbinvd(vcpu))
4631 return X86EMUL_CONTINUE;
4632
4633 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01004634 int cpu = get_cpu();
4635
4636 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004637 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
4638 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004639 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004640 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004641 } else
4642 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004643 return X86EMUL_CONTINUE;
4644}
Joel Schopp5cb56052015-03-02 13:43:31 -06004645
4646int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
4647{
4648 kvm_x86_ops->skip_emulated_instruction(vcpu);
4649 return kvm_emulate_wbinvd_noskip(vcpu);
4650}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004651EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
4652
Joel Schopp5cb56052015-03-02 13:43:31 -06004653
4654
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004655static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
4656{
Joel Schopp5cb56052015-03-02 13:43:31 -06004657 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004658}
4659
Xiubo Li52eb5a62015-03-13 17:39:45 +08004660static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
4661 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004662{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004663 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004664}
4665
Xiubo Li52eb5a62015-03-13 17:39:45 +08004666static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
4667 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004668{
Gleb Natapov338dbc92010-04-28 19:15:32 +03004669
Avi Kivity717746e2011-04-20 13:37:53 +03004670 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004671}
4672
Gleb Natapov52a46612010-03-18 15:20:03 +02004673static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4674{
4675 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4676}
4677
Avi Kivity717746e2011-04-20 13:37:53 +03004678static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02004679{
Avi Kivity717746e2011-04-20 13:37:53 +03004680 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02004681 unsigned long value;
4682
4683 switch (cr) {
4684 case 0:
4685 value = kvm_read_cr0(vcpu);
4686 break;
4687 case 2:
4688 value = vcpu->arch.cr2;
4689 break;
4690 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004691 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02004692 break;
4693 case 4:
4694 value = kvm_read_cr4(vcpu);
4695 break;
4696 case 8:
4697 value = kvm_get_cr8(vcpu);
4698 break;
4699 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004700 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02004701 return 0;
4702 }
4703
4704 return value;
4705}
4706
Avi Kivity717746e2011-04-20 13:37:53 +03004707static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02004708{
Avi Kivity717746e2011-04-20 13:37:53 +03004709 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03004710 int res = 0;
4711
Gleb Natapov52a46612010-03-18 15:20:03 +02004712 switch (cr) {
4713 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03004714 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004715 break;
4716 case 2:
4717 vcpu->arch.cr2 = val;
4718 break;
4719 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004720 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004721 break;
4722 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03004723 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004724 break;
4725 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004726 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004727 break;
4728 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004729 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03004730 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02004731 }
Gleb Natapov0f122442010-04-28 19:15:31 +03004732
4733 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02004734}
4735
Avi Kivity717746e2011-04-20 13:37:53 +03004736static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02004737{
Avi Kivity717746e2011-04-20 13:37:53 +03004738 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02004739}
4740
Avi Kivity4bff1e862011-04-20 13:37:53 +03004741static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004742{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004743 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004744}
4745
Avi Kivity4bff1e862011-04-20 13:37:53 +03004746static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004747{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004748 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004749}
4750
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004751static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4752{
4753 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
4754}
4755
4756static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4757{
4758 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
4759}
4760
Avi Kivity4bff1e862011-04-20 13:37:53 +03004761static unsigned long emulator_get_cached_segment_base(
4762 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03004763{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004764 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03004765}
4766
Avi Kivity1aa36612011-04-27 13:20:30 +03004767static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
4768 struct desc_struct *desc, u32 *base3,
4769 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004770{
4771 struct kvm_segment var;
4772
Avi Kivity4bff1e862011-04-20 13:37:53 +03004773 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03004774 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004775
Gleb Natapov378a8b02013-01-21 15:36:48 +02004776 if (var.unusable) {
4777 memset(desc, 0, sizeof(*desc));
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004778 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02004779 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004780
4781 if (var.g)
4782 var.limit >>= 12;
4783 set_desc_limit(desc, var.limit);
4784 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02004785#ifdef CONFIG_X86_64
4786 if (base3)
4787 *base3 = var.base >> 32;
4788#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004789 desc->type = var.type;
4790 desc->s = var.s;
4791 desc->dpl = var.dpl;
4792 desc->p = var.present;
4793 desc->avl = var.avl;
4794 desc->l = var.l;
4795 desc->d = var.db;
4796 desc->g = var.g;
4797
4798 return true;
4799}
4800
Avi Kivity1aa36612011-04-27 13:20:30 +03004801static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
4802 struct desc_struct *desc, u32 base3,
4803 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004804{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004805 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004806 struct kvm_segment var;
4807
Avi Kivity1aa36612011-04-27 13:20:30 +03004808 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004809 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02004810#ifdef CONFIG_X86_64
4811 var.base |= ((u64)base3) << 32;
4812#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004813 var.limit = get_desc_limit(desc);
4814 if (desc->g)
4815 var.limit = (var.limit << 12) | 0xfff;
4816 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004817 var.dpl = desc->dpl;
4818 var.db = desc->d;
4819 var.s = desc->s;
4820 var.l = desc->l;
4821 var.g = desc->g;
4822 var.avl = desc->avl;
4823 var.present = desc->p;
4824 var.unusable = !var.present;
4825 var.padding = 0;
4826
4827 kvm_set_segment(vcpu, &var, seg);
4828 return;
4829}
4830
Avi Kivity717746e2011-04-20 13:37:53 +03004831static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
4832 u32 msr_index, u64 *pdata)
4833{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004834 struct msr_data msr;
4835 int r;
4836
4837 msr.index = msr_index;
4838 msr.host_initiated = false;
4839 r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
4840 if (r)
4841 return r;
4842
4843 *pdata = msr.data;
4844 return 0;
Avi Kivity717746e2011-04-20 13:37:53 +03004845}
4846
4847static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
4848 u32 msr_index, u64 data)
4849{
Will Auld8fe8ab42012-11-29 12:42:12 -08004850 struct msr_data msr;
4851
4852 msr.data = data;
4853 msr.index = msr_index;
4854 msr.host_initiated = false;
4855 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03004856}
4857
Paolo Bonzini64d60672015-05-07 11:36:11 +02004858static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
4859{
4860 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4861
4862 return vcpu->arch.smbase;
4863}
4864
4865static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
4866{
4867 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4868
4869 vcpu->arch.smbase = smbase;
4870}
4871
Nadav Amit67f4d422014-06-02 18:34:09 +03004872static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
4873 u32 pmc)
4874{
Wei Huangc6702c92015-06-19 13:44:45 +02004875 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03004876}
4877
Avi Kivity222d21a2011-11-10 14:57:30 +02004878static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
4879 u32 pmc, u64 *pdata)
4880{
Wei Huangc6702c92015-06-19 13:44:45 +02004881 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02004882}
4883
Avi Kivity6c3287f2011-04-20 15:43:05 +03004884static void emulator_halt(struct x86_emulate_ctxt *ctxt)
4885{
4886 emul_to_vcpu(ctxt)->arch.halt_request = 1;
4887}
4888
Avi Kivity5037f6f2011-03-28 16:53:59 +02004889static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
4890{
4891 preempt_disable();
Avi Kivity5197b802011-04-20 15:55:40 +03004892 kvm_load_guest_fpu(emul_to_vcpu(ctxt));
Avi Kivity5037f6f2011-03-28 16:53:59 +02004893 /*
4894 * CR0.TS may reference the host fpu state, not the guest fpu state,
4895 * so it may be clear at this point.
4896 */
4897 clts();
4898}
4899
4900static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
4901{
4902 preempt_enable();
4903}
4904
Avi Kivity29535382011-04-20 13:37:53 +03004905static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004906 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004907 enum x86_intercept_stage stage)
4908{
Avi Kivity29535382011-04-20 13:37:53 +03004909 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004910}
4911
Avi Kivity0017f932012-06-07 14:10:16 +03004912static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004913 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
4914{
Avi Kivity0017f932012-06-07 14:10:16 +03004915 kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004916}
4917
Avi Kivitydd856ef2012-08-27 23:46:17 +03004918static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
4919{
4920 return kvm_register_read(emul_to_vcpu(ctxt), reg);
4921}
4922
4923static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
4924{
4925 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
4926}
4927
Nadav Amit801806d2015-01-26 09:32:23 +02004928static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
4929{
4930 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
4931}
4932
Mathias Krause0225fb52012-08-30 01:30:16 +02004933static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03004934 .read_gpr = emulator_read_gpr,
4935 .write_gpr = emulator_write_gpr,
Gleb Natapov1871c602010-02-10 14:21:32 +02004936 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004937 .write_std = kvm_write_guest_virt_system,
Radim Krčmář7a036a62015-10-30 16:36:24 +01004938 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02004939 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004940 .read_emulated = emulator_read_emulated,
4941 .write_emulated = emulator_write_emulated,
4942 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004943 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004944 .pio_in_emulated = emulator_pio_in_emulated,
4945 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03004946 .get_segment = emulator_get_segment,
4947 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03004948 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004949 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004950 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004951 .set_gdt = emulator_set_gdt,
4952 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02004953 .get_cr = emulator_get_cr,
4954 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02004955 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03004956 .get_dr = emulator_get_dr,
4957 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02004958 .get_smbase = emulator_get_smbase,
4959 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03004960 .set_msr = emulator_set_msr,
4961 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03004962 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02004963 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03004964 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004965 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03004966 .fix_hypercall = emulator_fix_hypercall,
Avi Kivity5037f6f2011-03-28 16:53:59 +02004967 .get_fpu = emulator_get_fpu,
4968 .put_fpu = emulator_put_fpu,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004969 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004970 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02004971 .set_nmi_mask = emulator_set_nmi_mask,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004972};
4973
Gleb Natapov95cb2292010-04-28 19:15:43 +03004974static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
4975{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004976 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03004977 /*
4978 * an sti; sti; sequence only disable interrupts for the first
4979 * instruction. So, if the last instruction, be it emulated or
4980 * not, left the system with the INT_STI flag enabled, it
4981 * means that the last instruction is an sti. We should not
4982 * leave the flag on in this case. The same goes for mov ss
4983 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004984 if (int_shadow & mask)
4985 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004986 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004987 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004988 if (!mask)
4989 kvm_make_request(KVM_REQ_EVENT, vcpu);
4990 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03004991}
4992
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004993static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03004994{
4995 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02004996 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004997 return kvm_propagate_fault(vcpu, &ctxt->exception);
4998
4999 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02005000 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
5001 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03005002 else
Avi Kivityda9cb572010-11-22 17:53:21 +02005003 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005004 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03005005}
5006
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005007static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
5008{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005009 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005010 int cs_db, cs_l;
5011
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005012 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
5013
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005014 ctxt->eflags = kvm_get_rflags(vcpu);
5015 ctxt->eip = kvm_rip_read(vcpu);
5016 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
5017 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03005018 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005019 cs_db ? X86EMUL_MODE_PROT32 :
5020 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005021 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005022 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
5023 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005024 ctxt->emul_flags = vcpu->arch.hflags;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005025
Avi Kivitydd856ef2012-08-27 23:46:17 +03005026 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005027 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005028}
5029
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005030int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02005031{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005032 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02005033 int ret;
5034
5035 init_emulate_ctxt(vcpu);
5036
Avi Kivity9dac77f2011-06-01 15:34:25 +03005037 ctxt->op_bytes = 2;
5038 ctxt->ad_bytes = 2;
5039 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005040 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02005041
5042 if (ret != X86EMUL_CONTINUE)
5043 return EMULATE_FAIL;
5044
Avi Kivity9dac77f2011-06-01 15:34:25 +03005045 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005046 kvm_rip_write(vcpu, ctxt->eip);
5047 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02005048
5049 if (irq == NMI_VECTOR)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005050 vcpu->arch.nmi_pending = 0;
Mohammed Gamal63995652010-09-19 14:34:06 +02005051 else
5052 vcpu->arch.interrupt.pending = false;
5053
5054 return EMULATE_DONE;
5055}
5056EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
5057
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005058static int handle_emulation_failure(struct kvm_vcpu *vcpu)
5059{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005060 int r = EMULATE_DONE;
5061
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005062 ++vcpu->stat.insn_emulation_fail;
5063 trace_kvm_emulate_insn_failed(vcpu);
Nadav Amita2b9e6c2014-09-17 02:50:50 +03005064 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005065 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5066 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5067 vcpu->run->internal.ndata = 0;
5068 r = EMULATE_FAIL;
5069 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005070 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005071
5072 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005073}
5074
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005075static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03005076 bool write_fault_to_shadow_pgtable,
5077 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03005078{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005079 gpa_t gpa = cr2;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005080 pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005081
Gleb Natapov991eebf2013-04-11 12:10:51 +03005082 if (emulation_type & EMULTYPE_NO_REEXECUTE)
5083 return false;
5084
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005085 if (!vcpu->arch.mmu.direct_map) {
5086 /*
5087 * Write permission should be allowed since only
5088 * write access need to be emulated.
5089 */
5090 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03005091
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005092 /*
5093 * If the mapping is invalid in guest, let cpu retry
5094 * it to generate fault.
5095 */
5096 if (gpa == UNMAPPED_GVA)
5097 return true;
5098 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005099
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005100 /*
5101 * Do not retry the unhandleable instruction if it faults on the
5102 * readonly host memory, otherwise it will goto a infinite loop:
5103 * retry instruction -> write #PF -> emulation fail -> retry
5104 * instruction -> ...
5105 */
5106 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005107
5108 /*
5109 * If the instruction failed on the error pfn, it can not be fixed,
5110 * report the error to userspace.
5111 */
5112 if (is_error_noslot_pfn(pfn))
5113 return false;
5114
5115 kvm_release_pfn_clean(pfn);
5116
5117 /* The instructions are well-emulated on direct mmu. */
5118 if (vcpu->arch.mmu.direct_map) {
5119 unsigned int indirect_shadow_pages;
5120
5121 spin_lock(&vcpu->kvm->mmu_lock);
5122 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5123 spin_unlock(&vcpu->kvm->mmu_lock);
5124
5125 if (indirect_shadow_pages)
5126 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5127
Gleb Natapova6f177e2010-07-08 12:41:12 +03005128 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005129 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005130
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005131 /*
5132 * if emulation was due to access to shadowed page table
5133 * and it failed try to unshadow page and re-enter the
5134 * guest to let CPU execute the instruction.
5135 */
5136 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005137
5138 /*
5139 * If the access faults on its page table, it can not
5140 * be fixed by unprotecting shadow page and it should
5141 * be reported to userspace.
5142 */
5143 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005144}
5145
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005146static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5147 unsigned long cr2, int emulation_type)
5148{
5149 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5150 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5151
5152 last_retry_eip = vcpu->arch.last_retry_eip;
5153 last_retry_addr = vcpu->arch.last_retry_addr;
5154
5155 /*
5156 * If the emulation is caused by #PF and it is non-page_table
5157 * writing instruction, it means the VM-EXIT is caused by shadow
5158 * page protected, we can zap the shadow page and retry this
5159 * instruction directly.
5160 *
5161 * Note: if the guest uses a non-page-table modifying instruction
5162 * on the PDE that points to the instruction, then we will unmap
5163 * the instruction and go to an infinite loop. So, we cache the
5164 * last retried eip and the last fault address, if we meet the eip
5165 * and the address again, we can break out of the potential infinite
5166 * loop.
5167 */
5168 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5169
5170 if (!(emulation_type & EMULTYPE_RETRY))
5171 return false;
5172
5173 if (x86_page_table_writing_insn(ctxt))
5174 return false;
5175
5176 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5177 return false;
5178
5179 vcpu->arch.last_retry_eip = ctxt->eip;
5180 vcpu->arch.last_retry_addr = cr2;
5181
5182 if (!vcpu->arch.mmu.direct_map)
5183 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5184
Xiao Guangrong22368022013-01-13 23:44:12 +08005185 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005186
5187 return true;
5188}
5189
Gleb Natapov716d51a2012-09-03 15:24:26 +03005190static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5191static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5192
Paolo Bonzini64d60672015-05-07 11:36:11 +02005193static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02005194{
Paolo Bonzini64d60672015-05-07 11:36:11 +02005195 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005196 /* This is a good place to trace that we are exiting SMM. */
5197 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
5198
Paolo Bonzini64d60672015-05-07 11:36:11 +02005199 if (unlikely(vcpu->arch.smi_pending)) {
5200 kvm_make_request(KVM_REQ_SMI, vcpu);
5201 vcpu->arch.smi_pending = 0;
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02005202 } else {
5203 /* Process a latched INIT, if any. */
5204 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005205 }
5206 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02005207
5208 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005209}
5210
5211static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
5212{
5213 unsigned changed = vcpu->arch.hflags ^ emul_flags;
5214
Paolo Bonzinia5845392015-04-01 18:18:53 +02005215 vcpu->arch.hflags = emul_flags;
Paolo Bonzini64d60672015-05-07 11:36:11 +02005216
5217 if (changed & HF_SMM_MASK)
5218 kvm_smm_changed(vcpu);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005219}
5220
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005221static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
5222 unsigned long *db)
5223{
5224 u32 dr6 = 0;
5225 int i;
5226 u32 enable, rwlen;
5227
5228 enable = dr7;
5229 rwlen = dr7 >> 16;
5230 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
5231 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
5232 dr6 |= (1 << i);
5233 return dr6;
5234}
5235
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005236static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005237{
5238 struct kvm_run *kvm_run = vcpu->run;
5239
5240 /*
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005241 * rflags is the old, "raw" value of the flags. The new value has
5242 * not been saved yet.
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005243 *
5244 * This is correct even for TF set by the guest, because "the
5245 * processor will not generate this exception after the instruction
5246 * that sets the TF flag".
5247 */
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005248 if (unlikely(rflags & X86_EFLAGS_TF)) {
5249 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005250 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5251 DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005252 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5253 kvm_run->debug.arch.exception = DB_VECTOR;
5254 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5255 *r = EMULATE_USER_EXIT;
5256 } else {
5257 vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5258 /*
5259 * "Certain debug exceptions may clear bit 0-3. The
5260 * remaining contents of the DR6 register are never
5261 * cleared by the processor".
5262 */
5263 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005264 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005265 kvm_queue_exception(vcpu, DB_VECTOR);
5266 }
5267 }
5268}
5269
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005270static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
5271{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005272 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
5273 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005274 struct kvm_run *kvm_run = vcpu->run;
5275 unsigned long eip = kvm_get_linear_rip(vcpu);
5276 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005277 vcpu->arch.guest_debug_dr7,
5278 vcpu->arch.eff_db);
5279
5280 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005281 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02005282 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005283 kvm_run->debug.arch.exception = DB_VECTOR;
5284 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5285 *r = EMULATE_USER_EXIT;
5286 return true;
5287 }
5288 }
5289
Nadav Amit4161a562014-07-17 01:19:31 +03005290 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
5291 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005292 unsigned long eip = kvm_get_linear_rip(vcpu);
5293 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005294 vcpu->arch.dr7,
5295 vcpu->arch.db);
5296
5297 if (dr6 != 0) {
5298 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005299 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005300 kvm_queue_exception(vcpu, DB_VECTOR);
5301 *r = EMULATE_DONE;
5302 return true;
5303 }
5304 }
5305
5306 return false;
5307}
5308
Andre Przywara51d8b662010-12-21 11:12:02 +01005309int x86_emulate_instruction(struct kvm_vcpu *vcpu,
5310 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01005311 int emulation_type,
5312 void *insn,
5313 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005314{
Gleb Natapov95cb2292010-04-28 19:15:43 +03005315 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005316 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005317 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005318 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005319
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005320 /*
5321 * Clear write_fault_to_shadow_pgtable here to ensure it is
5322 * never reused.
5323 */
5324 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03005325 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03005326
Sheng Yang571008d2008-01-02 14:49:22 +08005327 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005328 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005329
5330 /*
5331 * We will reenter on the same instruction since
5332 * we do not set complete_userspace_io. This does not
5333 * handle watchpoints yet, those would be handled in
5334 * the emulate_ops.
5335 */
5336 if (kvm_vcpu_check_breakpoint(vcpu, &r))
5337 return r;
5338
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005339 ctxt->interruptibility = 0;
5340 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005341 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005342 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005343
Borislav Petkovb51e9742013-09-22 16:44:52 +02005344 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02005345
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005346 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02005347
Avi Kivitye46479f2010-04-11 13:05:16 +03005348 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02005349 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005350 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02005351 if (emulation_type & EMULTYPE_TRAP_UD)
5352 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03005353 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5354 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005355 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005356 if (emulation_type & EMULTYPE_SKIP)
5357 return EMULATE_FAIL;
5358 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005359 }
5360 }
5361
Gleb Natapovba8afb62009-04-12 13:36:57 +03005362 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005363 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03005364 if (ctxt->eflags & X86_EFLAGS_RF)
5365 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03005366 return EMULATE_DONE;
5367 }
5368
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005369 if (retry_instruction(ctxt, cr2, emulation_type))
5370 return EMULATE_DONE;
5371
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005372 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005373 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005374 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
5375 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005376 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005377 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005378
Gleb Natapov5cd21912010-03-18 15:20:26 +02005379restart:
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005380 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005381
Joerg Roedel775fde82011-04-04 12:39:24 +02005382 if (r == EMULATION_INTERCEPTED)
5383 return EMULATE_DONE;
5384
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005385 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03005386 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5387 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005388 return EMULATE_DONE;
5389
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005390 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005391 }
5392
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005393 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005394 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005395 if (inject_emulated_exception(vcpu))
5396 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005397 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02005398 if (!vcpu->arch.pio.in) {
5399 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03005400 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02005401 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005402 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005403 vcpu->arch.complete_userspace_io = complete_emulated_pio;
5404 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005405 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005406 } else if (vcpu->mmio_needed) {
5407 if (!vcpu->mmio_is_write)
5408 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005409 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005410 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005411 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03005412 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005413 else
5414 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03005415
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005416 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005417 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005418 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005419 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005420 if (vcpu->arch.hflags != ctxt->emul_flags)
5421 kvm_set_hflags(vcpu, ctxt->emul_flags);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005422 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005423 if (r == EMULATE_DONE)
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005424 kvm_vcpu_check_singlestep(vcpu, rflags, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02005425 if (!ctxt->have_exception ||
5426 exception_type(ctxt->exception.vector) == EXCPT_TRAP)
5427 __kvm_set_rflags(vcpu, ctxt->eflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005428
5429 /*
5430 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
5431 * do nothing, and it will be requested again as soon as
5432 * the shadow expires. But we still need to check here,
5433 * because POPF has no interrupt shadow.
5434 */
5435 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
5436 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005437 } else
5438 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03005439
Gleb Natapove85d28f2010-07-29 15:11:52 +03005440 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005441}
Andre Przywara51d8b662010-12-21 11:12:02 +01005442EXPORT_SYMBOL_GPL(x86_emulate_instruction);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005443
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005444int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01005445{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005446 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005447 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
5448 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005449 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02005450 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02005451 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01005452}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005453EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01005454
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005455static void tsc_bad(void *info)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005456{
Tejun Heo0a3aee02010-12-18 16:28:55 +01005457 __this_cpu_write(cpu_tsc_khz, 0);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005458}
5459
5460static void tsc_khz_changed(void *data)
5461{
5462 struct cpufreq_freqs *freq = data;
5463 unsigned long khz = 0;
5464
5465 if (data)
5466 khz = freq->new;
5467 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5468 khz = cpufreq_quick_get(raw_smp_processor_id());
5469 if (!khz)
5470 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01005471 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005472}
5473
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005474static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
5475 void *data)
5476{
5477 struct cpufreq_freqs *freq = data;
5478 struct kvm *kvm;
5479 struct kvm_vcpu *vcpu;
5480 int i, send_ipi = 0;
5481
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005482 /*
5483 * We allow guests to temporarily run on slowing clocks,
5484 * provided we notify them after, or to run on accelerating
5485 * clocks, provided we notify them before. Thus time never
5486 * goes backwards.
5487 *
5488 * However, we have a problem. We can't atomically update
5489 * the frequency of a given CPU from this function; it is
5490 * merely a notifier, which can be called from any CPU.
5491 * Changing the TSC frequency at arbitrary points in time
5492 * requires a recomputation of local variables related to
5493 * the TSC for each VCPU. We must flag these local variables
5494 * to be updated and be sure the update takes place with the
5495 * new frequency before any guests proceed.
5496 *
5497 * Unfortunately, the combination of hotplug CPU and frequency
5498 * change creates an intractable locking scenario; the order
5499 * of when these callouts happen is undefined with respect to
5500 * CPU hotplug, and they can race with each other. As such,
5501 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
5502 * undefined; you can actually have a CPU frequency change take
5503 * place in between the computation of X and the setting of the
5504 * variable. To protect against this problem, all updates of
5505 * the per_cpu tsc_khz variable are done in an interrupt
5506 * protected IPI, and all callers wishing to update the value
5507 * must wait for a synchronous IPI to complete (which is trivial
5508 * if the caller is on the CPU already). This establishes the
5509 * necessary total order on variable updates.
5510 *
5511 * Note that because a guest time update may take place
5512 * anytime after the setting of the VCPU's request bit, the
5513 * correct TSC value must be set before the request. However,
5514 * to ensure the update actually makes it to any guest which
5515 * starts running in hardware virtualization between the set
5516 * and the acquisition of the spinlock, we must also ping the
5517 * CPU after setting the request bit.
5518 *
5519 */
5520
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005521 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
5522 return 0;
5523 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
5524 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005525
5526 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005527
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005528 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005529 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005530 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005531 if (vcpu->cpu != freq->cpu)
5532 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10005533 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005534 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005535 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005536 }
5537 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005538 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005539
5540 if (freq->old < freq->new && send_ipi) {
5541 /*
5542 * We upscale the frequency. Must make the guest
5543 * doesn't see old kvmclock values while running with
5544 * the new frequency, otherwise we risk the guest sees
5545 * time go backwards.
5546 *
5547 * In case we update the frequency for another cpu
5548 * (which might be in guest context) send an interrupt
5549 * to kick the cpu out of guest context. Next time
5550 * guest context is entered kvmclock will be updated,
5551 * so the guest will not see stale values.
5552 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005553 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005554 }
5555 return 0;
5556}
5557
5558static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005559 .notifier_call = kvmclock_cpufreq_notifier
5560};
5561
5562static int kvmclock_cpu_notifier(struct notifier_block *nfb,
5563 unsigned long action, void *hcpu)
5564{
5565 unsigned int cpu = (unsigned long)hcpu;
5566
5567 switch (action) {
5568 case CPU_ONLINE:
5569 case CPU_DOWN_FAILED:
5570 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
5571 break;
5572 case CPU_DOWN_PREPARE:
5573 smp_call_function_single(cpu, tsc_bad, NULL, 1);
5574 break;
5575 }
5576 return NOTIFY_OK;
5577}
5578
5579static struct notifier_block kvmclock_cpu_notifier_block = {
5580 .notifier_call = kvmclock_cpu_notifier,
5581 .priority = -INT_MAX
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005582};
5583
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005584static void kvm_timer_init(void)
5585{
5586 int cpu;
5587
Zachary Amsdenc2855452010-09-18 14:38:15 -10005588 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305589
5590 cpu_notifier_register_begin();
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005591 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10005592#ifdef CONFIG_CPU_FREQ
5593 struct cpufreq_policy policy;
5594 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02005595 cpu = get_cpu();
5596 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10005597 if (policy.cpuinfo.max_freq)
5598 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02005599 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10005600#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005601 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
5602 CPUFREQ_TRANSITION_NOTIFIER);
5603 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10005604 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005605 for_each_online_cpu(cpu)
5606 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305607
5608 __register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
5609 cpu_notifier_register_done();
5610
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005611}
5612
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005613static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
5614
Gleb Natapovf5132b02011-11-10 14:57:22 +02005615int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005616{
Alex,Shi086c9852011-10-20 15:34:01 +08005617 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005618}
5619
5620static int kvm_is_user_mode(void)
5621{
5622 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005623
Alex,Shi086c9852011-10-20 15:34:01 +08005624 if (__this_cpu_read(current_vcpu))
5625 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005626
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005627 return user_mode != 0;
5628}
5629
5630static unsigned long kvm_get_guest_ip(void)
5631{
5632 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005633
Alex,Shi086c9852011-10-20 15:34:01 +08005634 if (__this_cpu_read(current_vcpu))
5635 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005636
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005637 return ip;
5638}
5639
5640static struct perf_guest_info_callbacks kvm_guest_cbs = {
5641 .is_in_guest = kvm_is_in_guest,
5642 .is_user_mode = kvm_is_user_mode,
5643 .get_guest_ip = kvm_get_guest_ip,
5644};
5645
5646void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
5647{
Alex,Shi086c9852011-10-20 15:34:01 +08005648 __this_cpu_write(current_vcpu, vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005649}
5650EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
5651
5652void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
5653{
Alex,Shi086c9852011-10-20 15:34:01 +08005654 __this_cpu_write(current_vcpu, NULL);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005655}
5656EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
5657
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005658static void kvm_set_mmio_spte_mask(void)
5659{
5660 u64 mask;
5661 int maxphyaddr = boot_cpu_data.x86_phys_bits;
5662
5663 /*
5664 * Set the reserved bits and the present bit of an paging-structure
5665 * entry to generate page fault with PFER.RSV = 1.
5666 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005667 /* Mask the reserved physical address bits. */
Tiejun Chend1431482014-09-01 18:44:04 +08005668 mask = rsvd_bits(maxphyaddr, 51);
Xiao Guangrong885032b2013-06-07 16:51:23 +08005669
5670 /* Bit 62 is always reserved for 32bit host. */
5671 mask |= 0x3ull << 62;
5672
5673 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005674 mask |= 1ull;
5675
5676#ifdef CONFIG_X86_64
5677 /*
5678 * If reserved bit is not supported, clear the present bit to disable
5679 * mmio page fault.
5680 */
5681 if (maxphyaddr == 52)
5682 mask &= ~1ull;
5683#endif
5684
5685 kvm_mmu_set_mmio_spte_mask(mask);
5686}
5687
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005688#ifdef CONFIG_X86_64
5689static void pvclock_gtod_update_fn(struct work_struct *work)
5690{
Marcelo Tosattid8281992012-11-27 23:29:01 -02005691 struct kvm *kvm;
5692
5693 struct kvm_vcpu *vcpu;
5694 int i;
5695
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005696 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005697 list_for_each_entry(kvm, &vm_list, vm_list)
5698 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08005699 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005700 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005701 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005702}
5703
5704static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
5705
5706/*
5707 * Notification about pvclock gtod data update.
5708 */
5709static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
5710 void *priv)
5711{
5712 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
5713 struct timekeeper *tk = priv;
5714
5715 update_pvclock_gtod(tk);
5716
5717 /* disable master clock if host does not trust, or does not
5718 * use, TSC clocksource
5719 */
5720 if (gtod->clock.vclock_mode != VCLOCK_TSC &&
5721 atomic_read(&kvm_guest_has_master_clock) != 0)
5722 queue_work(system_long_wq, &pvclock_gtod_work);
5723
5724 return 0;
5725}
5726
5727static struct notifier_block pvclock_gtod_notifier = {
5728 .notifier_call = pvclock_gtod_notify,
5729};
5730#endif
5731
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005732int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02005733{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005734 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02005735 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005736
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005737 if (kvm_x86_ops) {
5738 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005739 r = -EEXIST;
5740 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005741 }
5742
5743 if (!ops->cpu_has_kvm_support()) {
5744 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005745 r = -EOPNOTSUPP;
5746 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005747 }
5748 if (ops->disabled_by_bios()) {
5749 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005750 r = -EOPNOTSUPP;
5751 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005752 }
5753
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005754 r = -ENOMEM;
5755 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
5756 if (!shared_msrs) {
5757 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
5758 goto out;
5759 }
5760
Avi Kivity97db56c2008-01-13 13:23:56 +02005761 r = kvm_mmu_module_init();
5762 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005763 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02005764
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005765 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02005766
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005767 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01005768
Sheng Yang7b523452008-04-25 21:13:50 +08005769 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005770 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005771
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005772 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005773
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005774 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5775
Dexuan Cui2acf9232010-06-10 11:27:12 +08005776 if (cpu_has_xsave)
5777 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
5778
Gleb Natapovc5cc4212012-08-05 15:58:30 +03005779 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005780#ifdef CONFIG_X86_64
5781 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
5782#endif
5783
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005784 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005785
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005786out_free_percpu:
5787 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005788out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005789 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02005790}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005791
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005792void kvm_arch_exit(void)
5793{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005794 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5795
Jan Kiszka888d2562009-04-17 19:24:58 +02005796 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5797 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
5798 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005799 unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005800#ifdef CONFIG_X86_64
5801 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
5802#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005803 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005804 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005805 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005806}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005807
Joel Schopp5cb56052015-03-02 13:43:31 -06005808int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005809{
5810 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005811 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03005812 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005813 return 1;
5814 } else {
5815 vcpu->run->exit_reason = KVM_EXIT_HLT;
5816 return 0;
5817 }
5818}
Joel Schopp5cb56052015-03-02 13:43:31 -06005819EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
5820
5821int kvm_emulate_halt(struct kvm_vcpu *vcpu)
5822{
5823 kvm_x86_ops->skip_emulated_instruction(vcpu);
5824 return kvm_vcpu_halt(vcpu);
5825}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005826EXPORT_SYMBOL_GPL(kvm_emulate_halt);
5827
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305828/*
5829 * kvm_pv_kick_cpu_op: Kick a vcpu.
5830 *
5831 * @apicid - apicid of vcpu to be kicked.
5832 */
5833static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
5834{
Raghavendra K T24d21662013-08-26 14:18:35 +05305835 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305836
Raghavendra K T24d21662013-08-26 14:18:35 +05305837 lapic_irq.shorthand = 0;
5838 lapic_irq.dest_mode = 0;
5839 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06005840 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305841
Raghavendra K T24d21662013-08-26 14:18:35 +05305842 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08005843 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305844}
5845
Hollis Blanchard8776e512007-10-31 17:24:24 -05005846int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
5847{
5848 unsigned long nr, a0, a1, a2, a3, ret;
Nadav Amita449c7a2014-06-18 17:19:24 +03005849 int op_64_bit, r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005850
Joel Schopp5cb56052015-03-02 13:43:31 -06005851 kvm_x86_ops->skip_emulated_instruction(vcpu);
5852
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005853 if (kvm_hv_hypercall_enabled(vcpu->kvm))
5854 return kvm_hv_hypercall(vcpu);
5855
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005856 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
5857 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
5858 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
5859 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
5860 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005861
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005862 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005863
Nadav Amita449c7a2014-06-18 17:19:24 +03005864 op_64_bit = is_64_bit_mode(vcpu);
5865 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05005866 nr &= 0xFFFFFFFF;
5867 a0 &= 0xFFFFFFFF;
5868 a1 &= 0xFFFFFFFF;
5869 a2 &= 0xFFFFFFFF;
5870 a3 &= 0xFFFFFFFF;
5871 }
5872
Jan Kiszka07708c42009-08-03 18:43:28 +02005873 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
5874 ret = -KVM_EPERM;
5875 goto out;
5876 }
5877
Hollis Blanchard8776e512007-10-31 17:24:24 -05005878 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02005879 case KVM_HC_VAPIC_POLL_IRQ:
5880 ret = 0;
5881 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305882 case KVM_HC_KICK_CPU:
5883 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
5884 ret = 0;
5885 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005886 default:
5887 ret = -KVM_ENOSYS;
5888 break;
5889 }
Jan Kiszka07708c42009-08-03 18:43:28 +02005890out:
Nadav Amita449c7a2014-06-18 17:19:24 +03005891 if (!op_64_bit)
5892 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005893 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05305894 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05005895 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005896}
5897EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
5898
Jan Kiszkab6785de2012-09-20 07:43:17 +02005899static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005900{
Avi Kivityd6aa1002011-04-20 15:47:13 +03005901 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005902 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005903 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005904
Hollis Blanchard8776e512007-10-31 17:24:24 -05005905 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005906
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005907 return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005908}
5909
Hollis Blanchardd0752062007-10-31 17:24:25 -05005910/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005911 * Check if userspace requested an interrupt window, and that the
5912 * interrupt window is open.
5913 *
5914 * No need to exit to userspace if we already have an interrupt queued.
5915 */
Avi Kivity851ba692009-08-24 11:10:17 +03005916static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005917{
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02005918 if (!vcpu->run->request_interrupt_window || pic_in_kernel(vcpu->kvm))
5919 return false;
5920
5921 if (kvm_cpu_has_interrupt(vcpu))
5922 return false;
5923
5924 return (irqchip_split(vcpu->kvm)
5925 ? kvm_apic_accept_pic_intr(vcpu)
5926 : kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005927}
5928
Avi Kivity851ba692009-08-24 11:10:17 +03005929static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005930{
Avi Kivity851ba692009-08-24 11:10:17 +03005931 struct kvm_run *kvm_run = vcpu->run;
5932
Jan Kiszka91586a32009-10-05 13:07:21 +02005933 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02005934 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005935 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005936 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02005937 if (!irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005938 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03005939 kvm_arch_interrupt_allowed(vcpu) &&
5940 !kvm_cpu_has_interrupt(vcpu) &&
5941 !kvm_event_needs_reinjection(vcpu);
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02005942 else if (!pic_in_kernel(vcpu->kvm))
5943 kvm_run->ready_for_interrupt_injection =
5944 kvm_apic_accept_pic_intr(vcpu) &&
5945 !kvm_cpu_has_interrupt(vcpu);
5946 else
5947 kvm_run->ready_for_interrupt_injection = 1;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005948}
5949
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005950static void update_cr8_intercept(struct kvm_vcpu *vcpu)
5951{
5952 int max_irr, tpr;
5953
5954 if (!kvm_x86_ops->update_cr8_intercept)
5955 return;
5956
Avi Kivity88c808f2009-08-17 22:49:40 +03005957 if (!vcpu->arch.apic)
5958 return;
5959
Gleb Natapov8db3baa2009-05-11 13:35:54 +03005960 if (!vcpu->arch.apic->vapic_addr)
5961 max_irr = kvm_lapic_find_highest_irr(vcpu);
5962 else
5963 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005964
5965 if (max_irr != -1)
5966 max_irr >>= 4;
5967
5968 tpr = kvm_lapic_get_cr8(vcpu);
5969
5970 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
5971}
5972
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005973static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005974{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005975 int r;
5976
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005977 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005978 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02005979 trace_kvm_inj_exception(vcpu->arch.exception.nr,
5980 vcpu->arch.exception.has_error_code,
5981 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03005982
5983 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
5984 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
5985 X86_EFLAGS_RF);
5986
Nadav Amit6bdf0662014-09-30 20:49:14 +03005987 if (vcpu->arch.exception.nr == DB_VECTOR &&
5988 (vcpu->arch.dr7 & DR7_GD)) {
5989 vcpu->arch.dr7 &= ~DR7_GD;
5990 kvm_update_dr7(vcpu);
5991 }
5992
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005993 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
5994 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005995 vcpu->arch.exception.error_code,
5996 vcpu->arch.exception.reinject);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005997 return 0;
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005998 }
5999
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006000 if (vcpu->arch.nmi_injected) {
6001 kvm_x86_ops->set_nmi(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006002 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006003 }
6004
6005 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006006 kvm_x86_ops->set_irq(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006007 return 0;
6008 }
6009
6010 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6011 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6012 if (r != 0)
6013 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006014 }
6015
6016 /* try to inject new event if pending */
6017 if (vcpu->arch.nmi_pending) {
6018 if (kvm_x86_ops->nmi_allowed(vcpu)) {
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006019 --vcpu->arch.nmi_pending;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006020 vcpu->arch.nmi_injected = true;
6021 kvm_x86_ops->set_nmi(vcpu);
6022 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08006023 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04006024 /*
6025 * Because interrupts can be injected asynchronously, we are
6026 * calling check_nested_events again here to avoid a race condition.
6027 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
6028 * proposal and current concerns. Perhaps we should be setting
6029 * KVM_REQ_EVENT only on certain events and not unconditionally?
6030 */
6031 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6032 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6033 if (r != 0)
6034 return r;
6035 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006036 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006037 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
6038 false);
6039 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006040 }
6041 }
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006042 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006043}
6044
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006045static void process_nmi(struct kvm_vcpu *vcpu)
6046{
6047 unsigned limit = 2;
6048
6049 /*
6050 * x86 is limited to one NMI running, and one NMI pending after it.
6051 * If an NMI is already in progress, limit further NMIs to just one.
6052 * Otherwise, allow two (and we'll inject the first one immediately).
6053 */
6054 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
6055 limit = 1;
6056
6057 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
6058 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
6059 kvm_make_request(KVM_REQ_EVENT, vcpu);
6060}
6061
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006062#define put_smstate(type, buf, offset, val) \
6063 *(type *)((buf) + (offset) - 0x7e00) = val
6064
6065static u32 process_smi_get_segment_flags(struct kvm_segment *seg)
6066{
6067 u32 flags = 0;
6068 flags |= seg->g << 23;
6069 flags |= seg->db << 22;
6070 flags |= seg->l << 21;
6071 flags |= seg->avl << 20;
6072 flags |= seg->present << 15;
6073 flags |= seg->dpl << 13;
6074 flags |= seg->s << 12;
6075 flags |= seg->type << 8;
6076 return flags;
6077}
6078
6079static void process_smi_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
6080{
6081 struct kvm_segment seg;
6082 int offset;
6083
6084 kvm_get_segment(vcpu, &seg, n);
6085 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
6086
6087 if (n < 3)
6088 offset = 0x7f84 + n * 12;
6089 else
6090 offset = 0x7f2c + (n - 3) * 12;
6091
6092 put_smstate(u32, buf, offset + 8, seg.base);
6093 put_smstate(u32, buf, offset + 4, seg.limit);
6094 put_smstate(u32, buf, offset, process_smi_get_segment_flags(&seg));
6095}
6096
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06006097#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006098static void process_smi_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
6099{
6100 struct kvm_segment seg;
6101 int offset;
6102 u16 flags;
6103
6104 kvm_get_segment(vcpu, &seg, n);
6105 offset = 0x7e00 + n * 16;
6106
6107 flags = process_smi_get_segment_flags(&seg) >> 8;
6108 put_smstate(u16, buf, offset, seg.selector);
6109 put_smstate(u16, buf, offset + 2, flags);
6110 put_smstate(u32, buf, offset + 4, seg.limit);
6111 put_smstate(u64, buf, offset + 8, seg.base);
6112}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06006113#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006114
6115static void process_smi_save_state_32(struct kvm_vcpu *vcpu, char *buf)
6116{
6117 struct desc_ptr dt;
6118 struct kvm_segment seg;
6119 unsigned long val;
6120 int i;
6121
6122 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
6123 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
6124 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
6125 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
6126
6127 for (i = 0; i < 8; i++)
6128 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
6129
6130 kvm_get_dr(vcpu, 6, &val);
6131 put_smstate(u32, buf, 0x7fcc, (u32)val);
6132 kvm_get_dr(vcpu, 7, &val);
6133 put_smstate(u32, buf, 0x7fc8, (u32)val);
6134
6135 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6136 put_smstate(u32, buf, 0x7fc4, seg.selector);
6137 put_smstate(u32, buf, 0x7f64, seg.base);
6138 put_smstate(u32, buf, 0x7f60, seg.limit);
6139 put_smstate(u32, buf, 0x7f5c, process_smi_get_segment_flags(&seg));
6140
6141 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6142 put_smstate(u32, buf, 0x7fc0, seg.selector);
6143 put_smstate(u32, buf, 0x7f80, seg.base);
6144 put_smstate(u32, buf, 0x7f7c, seg.limit);
6145 put_smstate(u32, buf, 0x7f78, process_smi_get_segment_flags(&seg));
6146
6147 kvm_x86_ops->get_gdt(vcpu, &dt);
6148 put_smstate(u32, buf, 0x7f74, dt.address);
6149 put_smstate(u32, buf, 0x7f70, dt.size);
6150
6151 kvm_x86_ops->get_idt(vcpu, &dt);
6152 put_smstate(u32, buf, 0x7f58, dt.address);
6153 put_smstate(u32, buf, 0x7f54, dt.size);
6154
6155 for (i = 0; i < 6; i++)
6156 process_smi_save_seg_32(vcpu, buf, i);
6157
6158 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
6159
6160 /* revision id */
6161 put_smstate(u32, buf, 0x7efc, 0x00020000);
6162 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
6163}
6164
6165static void process_smi_save_state_64(struct kvm_vcpu *vcpu, char *buf)
6166{
6167#ifdef CONFIG_X86_64
6168 struct desc_ptr dt;
6169 struct kvm_segment seg;
6170 unsigned long val;
6171 int i;
6172
6173 for (i = 0; i < 16; i++)
6174 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
6175
6176 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
6177 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
6178
6179 kvm_get_dr(vcpu, 6, &val);
6180 put_smstate(u64, buf, 0x7f68, val);
6181 kvm_get_dr(vcpu, 7, &val);
6182 put_smstate(u64, buf, 0x7f60, val);
6183
6184 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
6185 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
6186 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
6187
6188 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
6189
6190 /* revision id */
6191 put_smstate(u32, buf, 0x7efc, 0x00020064);
6192
6193 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
6194
6195 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6196 put_smstate(u16, buf, 0x7e90, seg.selector);
6197 put_smstate(u16, buf, 0x7e92, process_smi_get_segment_flags(&seg) >> 8);
6198 put_smstate(u32, buf, 0x7e94, seg.limit);
6199 put_smstate(u64, buf, 0x7e98, seg.base);
6200
6201 kvm_x86_ops->get_idt(vcpu, &dt);
6202 put_smstate(u32, buf, 0x7e84, dt.size);
6203 put_smstate(u64, buf, 0x7e88, dt.address);
6204
6205 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6206 put_smstate(u16, buf, 0x7e70, seg.selector);
6207 put_smstate(u16, buf, 0x7e72, process_smi_get_segment_flags(&seg) >> 8);
6208 put_smstate(u32, buf, 0x7e74, seg.limit);
6209 put_smstate(u64, buf, 0x7e78, seg.base);
6210
6211 kvm_x86_ops->get_gdt(vcpu, &dt);
6212 put_smstate(u32, buf, 0x7e64, dt.size);
6213 put_smstate(u64, buf, 0x7e68, dt.address);
6214
6215 for (i = 0; i < 6; i++)
6216 process_smi_save_seg_64(vcpu, buf, i);
6217#else
6218 WARN_ON_ONCE(1);
6219#endif
6220}
6221
Paolo Bonzini64d60672015-05-07 11:36:11 +02006222static void process_smi(struct kvm_vcpu *vcpu)
6223{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006224 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02006225 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006226 char buf[512];
6227 u32 cr0;
6228
Paolo Bonzini64d60672015-05-07 11:36:11 +02006229 if (is_smm(vcpu)) {
6230 vcpu->arch.smi_pending = true;
6231 return;
6232 }
6233
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006234 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
6235 vcpu->arch.hflags |= HF_SMM_MASK;
6236 memset(buf, 0, 512);
6237 if (guest_cpuid_has_longmode(vcpu))
6238 process_smi_save_state_64(vcpu, buf);
6239 else
6240 process_smi_save_state_32(vcpu, buf);
6241
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006242 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006243
6244 if (kvm_x86_ops->get_nmi_mask(vcpu))
6245 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
6246 else
6247 kvm_x86_ops->set_nmi_mask(vcpu, true);
6248
6249 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6250 kvm_rip_write(vcpu, 0x8000);
6251
6252 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
6253 kvm_x86_ops->set_cr0(vcpu, cr0);
6254 vcpu->arch.cr0 = cr0;
6255
6256 kvm_x86_ops->set_cr4(vcpu, 0);
6257
Paolo Bonzini18c36262015-08-07 12:27:54 +02006258 /* Undocumented: IDT limit is set to zero on entry to SMM. */
6259 dt.address = dt.size = 0;
6260 kvm_x86_ops->set_idt(vcpu, &dt);
6261
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006262 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
6263
6264 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
6265 cs.base = vcpu->arch.smbase;
6266
6267 ds.selector = 0;
6268 ds.base = 0;
6269
6270 cs.limit = ds.limit = 0xffffffff;
6271 cs.type = ds.type = 0x3;
6272 cs.dpl = ds.dpl = 0;
6273 cs.db = ds.db = 0;
6274 cs.s = ds.s = 1;
6275 cs.l = ds.l = 0;
6276 cs.g = ds.g = 1;
6277 cs.avl = ds.avl = 0;
6278 cs.present = ds.present = 1;
6279 cs.unusable = ds.unusable = 0;
6280 cs.padding = ds.padding = 0;
6281
6282 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
6283 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
6284 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
6285 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
6286 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
6287 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
6288
6289 if (guest_cpuid_has_longmode(vcpu))
6290 kvm_x86_ops->set_efer(vcpu, 0);
6291
6292 kvm_update_cpuid(vcpu);
6293 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006294}
6295
Yang Zhang3d81bc72013-04-11 19:25:13 +08006296static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006297{
Yang Zhang3d81bc72013-04-11 19:25:13 +08006298 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
6299 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006300
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02006301 memset(vcpu->arch.eoi_exit_bitmap, 0, 256 / 8);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006302
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07006303 if (irqchip_split(vcpu->kvm))
6304 kvm_scan_ioapic_routes(vcpu, vcpu->arch.eoi_exit_bitmap);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02006305 else {
6306 kvm_x86_ops->sync_pir_to_irr(vcpu);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07006307 kvm_ioapic_scan_entry(vcpu, vcpu->arch.eoi_exit_bitmap);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02006308 }
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02006309 kvm_x86_ops->load_eoi_exitmap(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006310}
6311
Radim Krčmářa70656b2014-09-18 12:38:36 -04006312static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
6313{
6314 ++vcpu->stat.tlb_flush;
6315 kvm_x86_ops->tlb_flush(vcpu);
6316}
6317
Tang Chen4256f432014-09-24 15:57:54 +08006318void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
6319{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006320 struct page *page = NULL;
6321
Paolo Bonzini35754c92015-07-29 12:05:37 +02006322 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02006323 return;
6324
Tang Chen4256f432014-09-24 15:57:54 +08006325 if (!kvm_x86_ops->set_apic_access_page_addr)
6326 return;
6327
Tang Chenc24ae0d2014-09-24 15:57:58 +08006328 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02006329 if (is_error_page(page))
6330 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08006331 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
6332
6333 /*
6334 * Do not pin apic access page in memory, the MMU notifier
6335 * will call us again if it is migrated or swapped out.
6336 */
6337 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08006338}
6339EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
6340
Tang Chenfe715572014-09-24 15:57:57 +08006341void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
6342 unsigned long address)
6343{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006344 /*
6345 * The physical address of apic access page is stored in the VMCS.
6346 * Update it when it becomes invalid.
6347 */
6348 if (address == gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT))
6349 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Tang Chenfe715572014-09-24 15:57:57 +08006350}
6351
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006352/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01006353 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006354 * exiting to the userspace. Otherwise, the value will be returned to the
6355 * userspace.
6356 */
Avi Kivity851ba692009-08-24 11:10:17 +03006357static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006358{
6359 int r;
Paolo Bonzini35754c92015-07-29 12:05:37 +02006360 bool req_int_win = !lapic_in_kernel(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03006361 vcpu->run->request_interrupt_window;
Jan Kiszka730dca42013-04-28 10:50:52 +02006362 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006363
Avi Kivity3e007502010-06-23 14:26:18 +03006364 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03006365 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05006366 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006367 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03006368 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006369 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
6370 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03006371 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
6372 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10006373 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
6374 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006375 if (unlikely(r))
6376 goto out;
6377 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006378 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03006379 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006380 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Radim Krčmářa70656b2014-09-18 12:38:36 -04006381 kvm_vcpu_flush_tlb(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006382 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006383 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02006384 r = 0;
6385 goto out;
6386 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006387 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006388 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01006389 r = 0;
6390 goto out;
6391 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006392 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02006393 vcpu->fpu_active = 0;
6394 kvm_x86_ops->fpu_deactivate(vcpu);
6395 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006396 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
6397 /* Page is swapped out. Do synthetic halt */
6398 vcpu->arch.apf.halted = true;
6399 r = 1;
6400 goto out;
6401 }
Glauber Costac9aaa892011-07-11 15:28:14 -04006402 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
6403 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006404 if (kvm_check_request(KVM_REQ_SMI, vcpu))
6405 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006406 if (kvm_check_request(KVM_REQ_NMI, vcpu))
6407 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006408 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006409 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006410 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006411 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07006412 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
6413 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
6414 if (test_bit(vcpu->arch.pending_ioapic_eoi,
6415 (void *) vcpu->arch.eoi_exit_bitmap)) {
6416 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
6417 vcpu->run->eoi.vector =
6418 vcpu->arch.pending_ioapic_eoi;
6419 r = 0;
6420 goto out;
6421 }
6422 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08006423 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
6424 vcpu_scan_ioapic(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08006425 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
6426 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03006427 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
6428 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
6429 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
6430 r = 0;
6431 goto out;
6432 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03006433 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
6434 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
6435 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
6436 r = 0;
6437 goto out;
6438 }
Avi Kivity2f52d582008-01-16 12:49:30 +02006439 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006440
Feng Wubf9f6ac2015-09-18 22:29:55 +08006441 /*
6442 * KVM_REQ_EVENT is not set when posted interrupts are set by
6443 * VT-d hardware, so we have to update RVI unconditionally.
6444 */
6445 if (kvm_lapic_enabled(vcpu)) {
6446 /*
6447 * Update architecture specific hints for APIC
6448 * virtual interrupt delivery.
6449 */
6450 if (kvm_x86_ops->hwapic_irr_update)
6451 kvm_x86_ops->hwapic_irr_update(vcpu,
6452 kvm_lapic_find_highest_irr(vcpu));
6453 }
6454
Avi Kivityb463a6f2010-07-20 15:06:17 +03006455 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Jan Kiszka66450a22013-03-13 12:42:34 +01006456 kvm_apic_accept_events(vcpu);
6457 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
6458 r = 1;
6459 goto out;
6460 }
6461
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006462 if (inject_pending_event(vcpu, req_int_win) != 0)
6463 req_immediate_exit = true;
Avi Kivity3842d132010-07-27 12:30:24 +03006464 /* enable NMI/IRQ window open exits if needed */
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006465 else if (vcpu->arch.nmi_pending)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006466 kvm_x86_ops->enable_nmi_window(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006467 else if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006468 kvm_x86_ops->enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006469
Avi Kivity3842d132010-07-27 12:30:24 +03006470 if (kvm_lapic_enabled(vcpu)) {
6471 update_cr8_intercept(vcpu);
6472 kvm_lapic_sync_to_vapic(vcpu);
6473 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006474 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006475
Avi Kivityd8368af2012-05-14 18:07:56 +03006476 r = kvm_mmu_reload(vcpu);
6477 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006478 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03006479 }
6480
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006481 preempt_disable();
6482
6483 kvm_x86_ops->prepare_guest_switch(vcpu);
6484 if (vcpu->fpu_active)
6485 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006486 kvm_load_guest_xcr0(vcpu);
6487
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006488 vcpu->mode = IN_GUEST_MODE;
6489
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006490 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6491
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006492 /* We should set ->mode before check ->requests,
6493 * see the comment in make_all_cpus_request.
6494 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006495 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006496
6497 local_irq_disable();
6498
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006499 if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006500 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006501 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006502 smp_wmb();
6503 local_irq_enable();
6504 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006505 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006506 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006507 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006508 }
6509
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006510 if (req_immediate_exit)
6511 smp_send_reschedule(vcpu->cpu);
6512
Christian Borntraegerccf73aaf2015-04-30 13:43:31 +02006513 __kvm_guest_enter();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006514
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006515 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006516 set_debugreg(0, 7);
6517 set_debugreg(vcpu->arch.eff_db[0], 0);
6518 set_debugreg(vcpu->arch.eff_db[1], 1);
6519 set_debugreg(vcpu->arch.eff_db[2], 2);
6520 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006521 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03006522 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006523 }
6524
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006525 trace_kvm_entry(vcpu->vcpu_id);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05006526 wait_lapic_expire(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006527 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006528
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006529 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006530 * Do this here before restoring debug registers on the host. And
6531 * since we do this before handling the vmexit, a DR access vmexit
6532 * can (a) read the correct value of the debug registers, (b) set
6533 * KVM_DEBUGREG_WONT_EXIT again.
6534 */
6535 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
6536 int i;
6537
6538 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
6539 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
6540 for (i = 0; i < KVM_NR_DB_REGS; i++)
6541 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
6542 }
6543
6544 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006545 * If the guest has used debug registers, at least dr7
6546 * will be disabled while returning to the host.
6547 * If we don't have active breakpoints in the host, we don't
6548 * care about the messed up debug address registers. But if
6549 * we have some of them active, restore the old state.
6550 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01006551 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006552 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006553
Haozhong Zhang4ba76532015-10-20 15:39:07 +08006554 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10006555
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006556 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03006557 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006558
6559 /* Interrupt is enabled by handle_external_intr() */
6560 kvm_x86_ops->handle_external_intr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006561
6562 ++vcpu->stat.exits;
6563
6564 /*
6565 * We must have an instruction between local_irq_enable() and
6566 * kvm_guest_exit(), so the timer interrupt isn't delayed by
6567 * the interrupt shadow. The stat.exits increment will do nicely.
6568 * But we need to prevent reordering, hence this barrier():
6569 */
6570 barrier();
6571
6572 kvm_guest_exit();
6573
6574 preempt_enable();
6575
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006576 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006577
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006578 /*
6579 * Profile KVM exit RIPs:
6580 */
6581 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006582 unsigned long rip = kvm_rip_read(vcpu);
6583 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006584 }
6585
Zachary Amsdencc578282012-02-03 15:43:50 -02006586 if (unlikely(vcpu->arch.tsc_always_catchup))
6587 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02006588
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03006589 if (vcpu->arch.apic_attention)
6590 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02006591
Avi Kivity851ba692009-08-24 11:10:17 +03006592 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006593 return r;
6594
6595cancel_injection:
6596 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03006597 if (unlikely(vcpu->arch.apic_attention))
6598 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006599out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03006600 return r;
6601}
6602
Paolo Bonzini362c6982015-02-06 12:48:04 +01006603static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
6604{
Feng Wubf9f6ac2015-09-18 22:29:55 +08006605 if (!kvm_arch_vcpu_runnable(vcpu) &&
6606 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01006607 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
6608 kvm_vcpu_block(vcpu);
6609 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006610
6611 if (kvm_x86_ops->post_block)
6612 kvm_x86_ops->post_block(vcpu);
6613
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01006614 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
6615 return 1;
6616 }
Gleb Natapov09cec752009-03-23 15:11:44 +02006617
Paolo Bonzini362c6982015-02-06 12:48:04 +01006618 kvm_apic_accept_events(vcpu);
6619 switch(vcpu->arch.mp_state) {
6620 case KVM_MP_STATE_HALTED:
6621 vcpu->arch.pv.pv_unhalted = false;
6622 vcpu->arch.mp_state =
6623 KVM_MP_STATE_RUNNABLE;
6624 case KVM_MP_STATE_RUNNABLE:
6625 vcpu->arch.apf.halted = false;
6626 break;
6627 case KVM_MP_STATE_INIT_RECEIVED:
6628 break;
6629 default:
6630 return -EINTR;
6631 break;
6632 }
6633 return 1;
6634}
6635
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02006636static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
6637{
6638 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
6639 !vcpu->arch.apf.halted);
6640}
6641
Paolo Bonzini362c6982015-02-06 12:48:04 +01006642static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03006643{
6644 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006645 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006646
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006647 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006648
Paolo Bonzini362c6982015-02-06 12:48:04 +01006649 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02006650 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006651 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006652 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01006653 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006654 }
6655
Gleb Natapov09cec752009-03-23 15:11:44 +02006656 if (r <= 0)
6657 break;
6658
6659 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
6660 if (kvm_cpu_has_pending_timer(vcpu))
6661 kvm_inject_pending_timer_irqs(vcpu);
6662
Avi Kivity851ba692009-08-24 11:10:17 +03006663 if (dm_request_for_irq_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02006664 r = 0;
6665 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02006666 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006667 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006668 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006669
6670 kvm_check_async_pf_completion(vcpu);
6671
Gleb Natapov09cec752009-03-23 15:11:44 +02006672 if (signal_pending(current)) {
6673 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006674 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006675 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006676 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006677 }
6678 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006679 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09006680 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006681 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006682 }
6683 }
6684
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006685 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006686
6687 return r;
6688}
6689
Gleb Natapov716d51a2012-09-03 15:24:26 +03006690static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
6691{
6692 int r;
6693 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6694 r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
6695 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6696 if (r != EMULATE_DONE)
6697 return 0;
6698 return 1;
6699}
6700
6701static int complete_emulated_pio(struct kvm_vcpu *vcpu)
6702{
6703 BUG_ON(!vcpu->arch.pio.count);
6704
6705 return complete_emulated_io(vcpu);
6706}
6707
Avi Kivityf78146b2012-04-18 19:22:47 +03006708/*
6709 * Implements the following, as a state machine:
6710 *
6711 * read:
6712 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006713 * for each mmio piece in the fragment
6714 * write gpa, len
6715 * exit
6716 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03006717 * execute insn
6718 *
6719 * write:
6720 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006721 * for each mmio piece in the fragment
6722 * write gpa, len
6723 * copy data
6724 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03006725 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03006726static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02006727{
6728 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03006729 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006730 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02006731
Gleb Natapov716d51a2012-09-03 15:24:26 +03006732 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006733
Gleb Natapov716d51a2012-09-03 15:24:26 +03006734 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006735 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
6736 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006737 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006738 memcpy(frag->data, run->mmio.data, len);
6739
6740 if (frag->len <= 8) {
6741 /* Switch to the next fragment. */
6742 frag++;
6743 vcpu->mmio_cur_fragment++;
6744 } else {
6745 /* Go forward to the next mmio piece. */
6746 frag->data += len;
6747 frag->gpa += len;
6748 frag->len -= len;
6749 }
6750
Andrew Honiga08d3b32014-02-27 19:35:14 +01006751 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03006752 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006753
6754 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02006755 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03006756 return 1;
6757 vcpu->mmio_read_completed = 1;
6758 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02006759 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006760
Gleb Natapov716d51a2012-09-03 15:24:26 +03006761 run->exit_reason = KVM_EXIT_MMIO;
6762 run->mmio.phys_addr = frag->gpa;
6763 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006764 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
6765 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006766 run->mmio.is_write = vcpu->mmio_is_write;
6767 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
6768 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02006769}
6770
Gleb Natapov716d51a2012-09-03 15:24:26 +03006771
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006772int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6773{
Ingo Molnarc5bedc62015-04-23 12:49:20 +02006774 struct fpu *fpu = &current->thread.fpu;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006775 int r;
6776 sigset_t sigsaved;
6777
Ingo Molnarc4d72e22015-04-27 07:18:17 +02006778 fpu__activate_curr(fpu);
Avi Kivitye5c30142011-01-11 12:15:54 +02006779
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006780 if (vcpu->sigset_active)
6781 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
6782
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006783 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
6784 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006785 kvm_apic_accept_events(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006786 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006787 r = -EAGAIN;
6788 goto out;
6789 }
6790
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006791 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02006792 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006793 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
6794 r = -EINVAL;
6795 goto out;
6796 }
6797 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006798
Gleb Natapov716d51a2012-09-03 15:24:26 +03006799 if (unlikely(vcpu->arch.complete_userspace_io)) {
6800 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
6801 vcpu->arch.complete_userspace_io = NULL;
6802 r = cui(vcpu);
6803 if (r <= 0)
6804 goto out;
6805 } else
6806 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006807
Paolo Bonzini362c6982015-02-06 12:48:04 +01006808 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006809
6810out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03006811 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006812 if (vcpu->sigset_active)
6813 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
6814
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006815 return r;
6816}
6817
6818int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6819{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006820 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
6821 /*
6822 * We are here if userspace calls get_regs() in the middle of
6823 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08006824 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006825 * that usually, but some bad designed PV devices (vmware
6826 * backdoor interface) need this to work
6827 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03006828 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006829 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6830 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006831 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
6832 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
6833 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
6834 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
6835 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
6836 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
6837 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6838 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006839#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006840 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
6841 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
6842 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
6843 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
6844 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
6845 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
6846 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
6847 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006848#endif
6849
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006850 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02006851 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006852
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006853 return 0;
6854}
6855
6856int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6857{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006858 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
6859 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6860
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006861 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
6862 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
6863 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
6864 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
6865 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
6866 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
6867 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
6868 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006869#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006870 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
6871 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
6872 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
6873 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
6874 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
6875 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
6876 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
6877 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006878#endif
6879
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006880 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02006881 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006882
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02006883 vcpu->arch.exception.pending = false;
6884
Avi Kivity3842d132010-07-27 12:30:24 +03006885 kvm_make_request(KVM_REQ_EVENT, vcpu);
6886
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006887 return 0;
6888}
6889
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006890void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
6891{
6892 struct kvm_segment cs;
6893
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006894 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006895 *db = cs.db;
6896 *l = cs.l;
6897}
6898EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
6899
6900int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
6901 struct kvm_sregs *sregs)
6902{
Gleb Natapov89a27f42010-02-16 10:51:48 +02006903 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006904
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006905 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6906 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6907 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6908 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6909 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6910 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006911
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006912 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6913 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006914
6915 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006916 sregs->idt.limit = dt.size;
6917 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006918 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006919 sregs->gdt.limit = dt.size;
6920 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006921
Avi Kivity4d4ec082009-12-29 18:07:30 +02006922 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006923 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006924 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02006925 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006926 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02006927 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006928 sregs->apic_base = kvm_get_apic_base(vcpu);
6929
Gleb Natapov923c61b2009-05-11 13:35:48 +03006930 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006931
Gleb Natapov36752c92009-05-11 13:35:53 +03006932 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03006933 set_bit(vcpu->arch.interrupt.nr,
6934 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03006935
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006936 return 0;
6937}
6938
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006939int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
6940 struct kvm_mp_state *mp_state)
6941{
Jan Kiszka66450a22013-03-13 12:42:34 +01006942 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306943 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
6944 vcpu->arch.pv.pv_unhalted)
6945 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
6946 else
6947 mp_state->mp_state = vcpu->arch.mp_state;
6948
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006949 return 0;
6950}
6951
6952int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
6953 struct kvm_mp_state *mp_state)
6954{
Jan Kiszka66450a22013-03-13 12:42:34 +01006955 if (!kvm_vcpu_has_lapic(vcpu) &&
6956 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
6957 return -EINVAL;
6958
6959 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
6960 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
6961 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
6962 } else
6963 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03006964 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006965 return 0;
6966}
6967
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006968int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
6969 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006970{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006971 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006972 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006973
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006974 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006975
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006976 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006977 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02006978
Gleb Natapovc6975182010-02-18 12:15:01 +02006979 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03006980 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02006981
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006982 kvm_rip_write(vcpu, ctxt->eip);
6983 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03006984 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03006985 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02006986}
6987EXPORT_SYMBOL_GPL(kvm_task_switch);
6988
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006989int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
6990 struct kvm_sregs *sregs)
6991{
Jan Kiszka58cb6282014-01-24 16:48:44 +01006992 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006993 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006994 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02006995 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006996
Petr Matousek6d1068b2012-11-06 19:24:07 +01006997 if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
6998 return -EINVAL;
6999
Gleb Natapov89a27f42010-02-16 10:51:48 +02007000 dt.size = sregs->idt.limit;
7001 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007002 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02007003 dt.size = sregs->gdt.limit;
7004 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007005 kvm_x86_ops->set_gdt(vcpu, &dt);
7006
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007007 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02007008 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02007009 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02007010 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007011
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007012 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007013
Avi Kivityf6801df2010-01-21 15:31:50 +02007014 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007015 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Jan Kiszka58cb6282014-01-24 16:48:44 +01007016 apic_base_msr.data = sregs->apic_base;
7017 apic_base_msr.host_initiated = true;
7018 kvm_set_apic_base(vcpu, &apic_base_msr);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007019
Avi Kivity4d4ec082009-12-29 18:07:30 +02007020 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007021 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00007022 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007023
Avi Kivityfc78f512009-12-07 12:16:48 +02007024 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007025 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Sheng Yang3ea3aa82010-12-08 10:49:43 +08007026 if (sregs->cr4 & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +02007027 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007028
7029 idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02007030 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02007031 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02007032 mmu_reset_needed = 1;
7033 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007034 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007035
7036 if (mmu_reset_needed)
7037 kvm_mmu_reset_context(vcpu);
7038
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03007039 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03007040 pending_vec = find_first_bit(
7041 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
7042 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007043 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03007044 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007045 }
7046
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007047 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
7048 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
7049 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
7050 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
7051 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
7052 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007053
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007054 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
7055 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007056
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03007057 update_cr8_intercept(vcpu);
7058
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007059 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03007060 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007061 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02007062 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007063 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
7064
Avi Kivity3842d132010-07-27 12:30:24 +03007065 kvm_make_request(KVM_REQ_EVENT, vcpu);
7066
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007067 return 0;
7068}
7069
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007070int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
7071 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007072{
Jan Kiszka355be0b2009-10-03 00:31:21 +02007073 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007074 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007075
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007076 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
7077 r = -EBUSY;
7078 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03007079 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007080 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
7081 kvm_queue_exception(vcpu, DB_VECTOR);
7082 else
7083 kvm_queue_exception(vcpu, BP_VECTOR);
7084 }
7085
Jan Kiszka91586a32009-10-05 13:07:21 +02007086 /*
7087 * Read rflags as long as potentially injected trace flags are still
7088 * filtered out.
7089 */
7090 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02007091
7092 vcpu->guest_debug = dbg->control;
7093 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
7094 vcpu->guest_debug = 0;
7095
7096 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007097 for (i = 0; i < KVM_NR_DB_REGS; ++i)
7098 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02007099 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007100 } else {
7101 for (i = 0; i < KVM_NR_DB_REGS; i++)
7102 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007103 }
Jan Kiszkac8639012012-09-21 05:42:55 +02007104 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007105
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007106 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7107 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
7108 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007109
Jan Kiszka91586a32009-10-05 13:07:21 +02007110 /*
7111 * Trigger an rflags update that will inject or remove the trace
7112 * flags.
7113 */
7114 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007115
Jan Kiszkac8639012012-09-21 05:42:55 +02007116 kvm_x86_ops->update_db_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02007117
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007118 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007119
Avi Kivity2122ff52010-05-13 11:25:04 +03007120out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007121
7122 return r;
7123}
7124
7125/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08007126 * Translate a guest virtual address to a guest physical address.
7127 */
7128int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
7129 struct kvm_translation *tr)
7130{
7131 unsigned long vaddr = tr->linear_address;
7132 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007133 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007134
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007135 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02007136 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007137 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08007138 tr->physical_address = gpa;
7139 tr->valid = gpa != UNMAPPED_GVA;
7140 tr->writeable = 1;
7141 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007142
7143 return 0;
7144}
7145
Hollis Blanchardd0752062007-10-31 17:24:25 -05007146int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7147{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007148 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007149 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007150
Hollis Blanchardd0752062007-10-31 17:24:25 -05007151 memcpy(fpu->fpr, fxsave->st_space, 128);
7152 fpu->fcw = fxsave->cwd;
7153 fpu->fsw = fxsave->swd;
7154 fpu->ftwx = fxsave->twd;
7155 fpu->last_opcode = fxsave->fop;
7156 fpu->last_ip = fxsave->rip;
7157 fpu->last_dp = fxsave->rdp;
7158 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
7159
Hollis Blanchardd0752062007-10-31 17:24:25 -05007160 return 0;
7161}
7162
7163int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7164{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007165 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007166 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007167
Hollis Blanchardd0752062007-10-31 17:24:25 -05007168 memcpy(fxsave->st_space, fpu->fpr, 128);
7169 fxsave->cwd = fpu->fcw;
7170 fxsave->swd = fpu->fsw;
7171 fxsave->twd = fpu->ftwx;
7172 fxsave->fop = fpu->last_opcode;
7173 fxsave->rip = fpu->last_ip;
7174 fxsave->rdp = fpu->last_dp;
7175 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
7176
Hollis Blanchardd0752062007-10-31 17:24:25 -05007177 return 0;
7178}
7179
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007180static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007181{
Ingo Molnarbf935b02015-04-30 10:23:42 +02007182 fpstate_init(&vcpu->arch.guest_fpu.state);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007183 if (cpu_has_xsaves)
Ingo Molnar7366ed72015-04-27 04:19:39 +02007184 vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007185 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007186
Dexuan Cui2acf9232010-06-10 11:27:12 +08007187 /*
7188 * Ensure guest xcr0 is valid for loading
7189 */
7190 vcpu->arch.xcr0 = XSTATE_FP;
7191
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007192 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007193}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007194
7195void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
7196{
Avi Kivity2608d7a2010-01-21 15:31:45 +02007197 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007198 return;
7199
Dexuan Cui2acf9232010-06-10 11:27:12 +08007200 /*
7201 * Restore all possible states in the guest,
7202 * and assume host would use all available bits.
7203 * Guest xcr0 would be loaded later.
7204 */
7205 kvm_put_guest_xcr0(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007206 vcpu->guest_fpu_loaded = 1;
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007207 __kernel_fpu_begin();
Ingo Molnar003e2e82015-05-25 11:59:35 +02007208 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state);
Avi Kivity0c048512010-01-21 15:31:52 +02007209 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007210}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007211
7212void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
7213{
Dexuan Cui2acf9232010-06-10 11:27:12 +08007214 kvm_put_guest_xcr0(vcpu);
7215
Rik van Riel653f52c2015-04-23 11:52:37 -04007216 if (!vcpu->guest_fpu_loaded) {
7217 vcpu->fpu_counter = 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007218 return;
Rik van Riel653f52c2015-04-23 11:52:37 -04007219 }
Hollis Blanchardd0752062007-10-31 17:24:25 -05007220
7221 vcpu->guest_fpu_loaded = 0;
Ingo Molnar4f836342015-04-27 02:53:16 +02007222 copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007223 __kernel_fpu_end();
Avi Kivityf096ed82007-11-18 13:54:33 +02007224 ++vcpu->stat.fpu_reload;
Rik van Riel653f52c2015-04-23 11:52:37 -04007225 /*
7226 * If using eager FPU mode, or if the guest is a frequent user
7227 * of the FPU, just leave the FPU active for next time.
7228 * Every 255 times fpu_counter rolls over to 0; a guest that uses
7229 * the FPU in bursts will revert to loading it on demand.
7230 */
Paolo Bonzinia9b4fb72015-05-20 11:46:12 +02007231 if (!vcpu->arch.eager_fpu) {
Rik van Riel653f52c2015-04-23 11:52:37 -04007232 if (++vcpu->fpu_counter < 5)
7233 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
7234 }
Avi Kivity0c048512010-01-21 15:31:52 +02007235 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007236}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007237
7238void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
7239{
Glauber Costa12f9a482011-02-01 14:16:40 -05007240 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01007241
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007242 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007243 kvm_x86_ops->vcpu_free(vcpu);
7244}
7245
7246struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
7247 unsigned int id)
7248{
Liang Lic447e762015-05-21 04:41:25 +08007249 struct kvm_vcpu *vcpu;
7250
Zachary Amsden6755bae2010-08-19 22:07:22 -10007251 if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
7252 printk_once(KERN_WARNING
7253 "kvm: SMP vm created on host with unstable TSC; "
7254 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08007255
7256 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
7257
Liang Lic447e762015-05-21 04:41:25 +08007258 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02007259}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007260
Avi Kivity26e52152007-11-20 15:30:24 +02007261int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
7262{
7263 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007264
Xiao Guangrong19efffa2015-06-15 16:55:31 +08007265 kvm_vcpu_mtrr_init(vcpu);
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007266 r = vcpu_load(vcpu);
7267 if (r)
7268 return r;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007269 kvm_vcpu_reset(vcpu, false);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007270 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007271 vcpu_put(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02007272 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007273}
7274
Dominik Dingel31928aa2014-12-04 15:47:07 +01007275void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007276{
Will Auld8fe8ab42012-11-29 12:42:12 -08007277 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01007278 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007279
Dominik Dingel31928aa2014-12-04 15:47:07 +01007280 if (vcpu_load(vcpu))
7281 return;
Will Auld8fe8ab42012-11-29 12:42:12 -08007282 msr.data = 0x0;
7283 msr.index = MSR_IA32_TSC;
7284 msr.host_initiated = true;
7285 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007286 vcpu_put(vcpu);
7287
Marcelo Tosatti630994b2015-05-12 22:42:04 -03007288 if (!kvmclock_periodic_sync)
7289 return;
7290
Andrew Jones332967a2014-02-28 12:52:55 +01007291 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
7292 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007293}
7294
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06007295void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007296{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007297 int r;
Gleb Natapov344d9582010-10-14 11:22:50 +02007298 vcpu->arch.apf.msr_val = 0;
7299
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007300 r = vcpu_load(vcpu);
7301 BUG_ON(r);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007302 kvm_mmu_unload(vcpu);
7303 vcpu_put(vcpu);
7304
7305 kvm_x86_ops->vcpu_free(vcpu);
7306}
7307
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007308void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007309{
Paolo Bonzinie69fab52015-06-04 10:44:44 +02007310 vcpu->arch.hflags = 0;
7311
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007312 atomic_set(&vcpu->arch.nmi_queued, 0);
7313 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007314 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03007315 kvm_clear_interrupt_queue(vcpu);
7316 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007317
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007318 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03007319 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03007320 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007321 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007322 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02007323 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007324
Nadav Amit11190222015-04-02 03:10:38 +03007325 vcpu->arch.cr2 = 0;
7326
Avi Kivity3842d132010-07-27 12:30:24 +03007327 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02007328 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04007329 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03007330
Glauber Costa12f9a482011-02-01 14:16:40 -05007331 kvmclock_reset(vcpu);
7332
Gleb Natapovaf585b92010-10-14 11:22:46 +02007333 kvm_clear_async_pf_completion_queue(vcpu);
7334 kvm_async_pf_hash_reset(vcpu);
7335 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007336
Paolo Bonzini64d60672015-05-07 11:36:11 +02007337 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007338 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007339 vcpu->arch.smbase = 0x30000;
7340 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02007341
Julian Stecklina66f7b722012-12-05 15:26:19 +01007342 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
7343 vcpu->arch.regs_avail = ~0;
7344 vcpu->arch.regs_dirty = ~0;
7345
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007346 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007347}
7348
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01007349void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01007350{
7351 struct kvm_segment cs;
7352
7353 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
7354 cs.selector = vector << 8;
7355 cs.base = vector << 12;
7356 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7357 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007358}
7359
Radim Krčmář13a34e02014-08-28 15:13:03 +02007360int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007361{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10007362 struct kvm *kvm;
7363 struct kvm_vcpu *vcpu;
7364 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007365 int ret;
7366 u64 local_tsc;
7367 u64 max_tsc = 0;
7368 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03007369
7370 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02007371 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007372 if (ret != 0)
7373 return ret;
7374
Andy Lutomirski4ea16362015-06-25 18:44:07 +02007375 local_tsc = rdtsc();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007376 stable = !check_tsc_unstable();
7377 list_for_each_entry(kvm, &vm_list, vm_list) {
7378 kvm_for_each_vcpu(i, vcpu, kvm) {
7379 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007380 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007381 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
7382 backwards_tsc = true;
7383 if (vcpu->arch.last_host_tsc > max_tsc)
7384 max_tsc = vcpu->arch.last_host_tsc;
7385 }
7386 }
7387 }
7388
7389 /*
7390 * Sometimes, even reliable TSCs go backwards. This happens on
7391 * platforms that reset TSC during suspend or hibernate actions, but
7392 * maintain synchronization. We must compensate. Fortunately, we can
7393 * detect that condition here, which happens early in CPU bringup,
7394 * before any KVM threads can be running. Unfortunately, we can't
7395 * bring the TSCs fully up to date with real time, as we aren't yet far
7396 * enough into CPU bringup that we know how much real time has actually
7397 * elapsed; our helper function, get_kernel_ns() will be using boot
7398 * variables that haven't been updated yet.
7399 *
7400 * So we simply find the maximum observed TSC above, then record the
7401 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
7402 * the adjustment will be applied. Note that we accumulate
7403 * adjustments, in case multiple suspend cycles happen before some VCPU
7404 * gets a chance to run again. In the event that no KVM threads get a
7405 * chance to run, we will miss the entire elapsed period, as we'll have
7406 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
7407 * loose cycle time. This isn't too big a deal, since the loss will be
7408 * uniform across all VCPUs (not to mention the scenario is extremely
7409 * unlikely). It is possible that a second hibernate recovery happens
7410 * much faster than a first, causing the observed TSC here to be
7411 * smaller; this would require additional padding adjustment, which is
7412 * why we set last_host_tsc to the local tsc observed here.
7413 *
7414 * N.B. - this code below runs only on platforms with reliable TSC,
7415 * as that is the only way backwards_tsc is set above. Also note
7416 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
7417 * have the same delta_cyc adjustment applied if backwards_tsc
7418 * is detected. Note further, this adjustment is only done once,
7419 * as we reset last_host_tsc on all VCPUs to stop this from being
7420 * called multiple times (one for each physical CPU bringup).
7421 *
Guo Chao4a969982012-06-28 15:17:27 +08007422 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007423 * will be compensated by the logic in vcpu_load, which sets the TSC to
7424 * catchup mode. This will catchup all VCPUs to real time, but cannot
7425 * guarantee that they stay in perfect synchronization.
7426 */
7427 if (backwards_tsc) {
7428 u64 delta_cyc = max_tsc - local_tsc;
Marcelo Tosatti16a96022014-05-14 12:43:24 -03007429 backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007430 list_for_each_entry(kvm, &vm_list, vm_list) {
7431 kvm_for_each_vcpu(i, vcpu, kvm) {
7432 vcpu->arch.tsc_offset_adjustment += delta_cyc;
7433 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007434 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007435 }
7436
7437 /*
7438 * We have to disable TSC offset matching.. if you were
7439 * booting a VM while issuing an S4 host suspend....
7440 * you may have some problem. Solving this issue is
7441 * left as an exercise to the reader.
7442 */
7443 kvm->arch.last_tsc_nsec = 0;
7444 kvm->arch.last_tsc_write = 0;
7445 }
7446
7447 }
7448 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007449}
7450
Radim Krčmář13a34e02014-08-28 15:13:03 +02007451void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007452{
Radim Krčmář13a34e02014-08-28 15:13:03 +02007453 kvm_x86_ops->hardware_disable();
7454 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007455}
7456
7457int kvm_arch_hardware_setup(void)
7458{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03007459 int r;
7460
7461 r = kvm_x86_ops->hardware_setup();
7462 if (r != 0)
7463 return r;
7464
Haozhong Zhang35181e82015-10-20 15:39:03 +08007465 if (kvm_has_tsc_control) {
7466 /*
7467 * Make sure the user can only configure tsc_khz values that
7468 * fit into a signed integer.
7469 * A min value is not calculated needed because it will always
7470 * be 1 on all machines.
7471 */
7472 u64 max = min(0x7fffffffULL,
7473 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
7474 kvm_max_guest_tsc_khz = max;
7475
Haozhong Zhangad721882015-10-20 15:39:02 +08007476 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08007477 }
Haozhong Zhangad721882015-10-20 15:39:02 +08007478
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03007479 kvm_init_msr_list();
7480 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007481}
7482
7483void kvm_arch_hardware_unsetup(void)
7484{
7485 kvm_x86_ops->hardware_unsetup();
7486}
7487
7488void kvm_arch_check_processor_compat(void *rtn)
7489{
7490 kvm_x86_ops->check_processor_compatibility(rtn);
7491}
7492
Paolo Bonzinid71ba782015-07-29 11:56:48 +02007493bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
7494{
7495 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
7496}
7497EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
7498
7499bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
7500{
7501 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
7502}
7503
Avi Kivity3e515702012-03-05 14:23:29 +02007504bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
7505{
Paolo Bonzini35754c92015-07-29 12:05:37 +02007506 return irqchip_in_kernel(vcpu->kvm) == lapic_in_kernel(vcpu);
Avi Kivity3e515702012-03-05 14:23:29 +02007507}
7508
Gleb Natapov54e98182012-08-05 15:58:32 +03007509struct static_key kvm_no_apic_vcpu __read_mostly;
7510
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007511int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
7512{
7513 struct page *page;
7514 struct kvm *kvm;
7515 int r;
7516
7517 BUG_ON(vcpu->kvm == NULL);
7518 kvm = vcpu->kvm;
7519
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307520 vcpu->arch.pv.pv_unhalted = false;
Avi Kivity9aabc88f2010-07-29 15:11:50 +03007521 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
Nadav Amit58d269d2015-04-02 03:10:36 +03007522 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03007523 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007524 else
Avi Kivitya4535292008-04-13 17:54:35 +03007525 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007526
7527 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
7528 if (!page) {
7529 r = -ENOMEM;
7530 goto fail;
7531 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007532 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007533
Zachary Amsdencc578282012-02-03 15:43:50 -02007534 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007535
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007536 r = kvm_mmu_create(vcpu);
7537 if (r < 0)
7538 goto fail_free_pio_data;
7539
7540 if (irqchip_in_kernel(kvm)) {
7541 r = kvm_create_lapic(vcpu);
7542 if (r < 0)
7543 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03007544 } else
7545 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007546
Huang Ying890ca9a2009-05-11 16:48:15 +08007547 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
7548 GFP_KERNEL);
7549 if (!vcpu->arch.mce_banks) {
7550 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08007551 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08007552 }
7553 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
7554
Wei Yongjunf1797352013-04-18 07:41:00 +08007555 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
7556 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007557 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08007558 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007559
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007560 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01007561
Will Auldba904632012-11-29 12:42:50 -08007562 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
Andy Honig0b794592013-02-20 14:48:10 -08007563 vcpu->arch.pv_time_enabled = false;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007564
7565 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +02007566 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007567
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03007568 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
7569
Radim Krčmář74545702015-04-27 15:11:25 +02007570 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
7571
Gleb Natapovaf585b92010-10-14 11:22:46 +02007572 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007573 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007574
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02007575 vcpu->arch.pending_external_vector = -1;
7576
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007577 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007578
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007579fail_free_mce_banks:
7580 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08007581fail_free_lapic:
7582 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007583fail_mmu_destroy:
7584 kvm_mmu_destroy(vcpu);
7585fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007586 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007587fail:
7588 return r;
7589}
7590
7591void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
7592{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007593 int idx;
7594
Gleb Natapovf5132b02011-11-10 14:57:22 +02007595 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08007596 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007597 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007598 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007599 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007600 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007601 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007602 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03007603 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007604}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007605
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007606void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
7607{
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007608 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007609}
7610
Carsten Ottee08b9632012-01-04 10:25:20 +01007611int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007612{
Carsten Ottee08b9632012-01-04 10:25:20 +01007613 if (type)
7614 return -EINVAL;
7615
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01007616 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08007617 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08007618 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007619 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007620 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007621
Sheng Yang5550af42008-10-15 20:15:06 +08007622 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
7623 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06007624 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
7625 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
7626 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08007627
Jan Kiszka038f8c12011-02-04 10:49:11 +01007628 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007629 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007630 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
7631
7632 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01007633
Andrew Jones7e44e442014-02-28 12:52:54 +01007634 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01007635 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01007636
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01007637 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007638}
7639
7640static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
7641{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007642 int r;
7643 r = vcpu_load(vcpu);
7644 BUG_ON(r);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007645 kvm_mmu_unload(vcpu);
7646 vcpu_put(vcpu);
7647}
7648
7649static void kvm_free_vcpus(struct kvm *kvm)
7650{
7651 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007652 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007653
7654 /*
7655 * Unpin any mmu pages first.
7656 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02007657 kvm_for_each_vcpu(i, vcpu, kvm) {
7658 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007659 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007660 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007661 kvm_for_each_vcpu(i, vcpu, kvm)
7662 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007663
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007664 mutex_lock(&kvm->lock);
7665 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
7666 kvm->vcpus[i] = NULL;
7667
7668 atomic_set(&kvm->online_vcpus, 0);
7669 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007670}
7671
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007672void kvm_arch_sync_events(struct kvm *kvm)
7673{
Andrew Jones332967a2014-02-28 12:52:55 +01007674 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01007675 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Sheng Yangba4cef32009-01-06 10:03:03 +08007676 kvm_free_all_assigned_devices(kvm);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08007677 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007678}
7679
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007680int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007681{
7682 int i, r;
Paolo Bonzini25188b92015-10-14 15:51:08 +02007683 unsigned long hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007684 struct kvm_memslots *slots = kvm_memslots(kvm);
7685 struct kvm_memory_slot *slot, old;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007686
7687 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007688 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
7689 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007690
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007691 slot = id_to_memslot(slots, id);
7692 if (size) {
7693 if (WARN_ON(slot->npages))
7694 return -EEXIST;
7695
7696 /*
7697 * MAP_SHARED to prevent internal slot pages from being moved
7698 * by fork()/COW.
7699 */
7700 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
7701 MAP_SHARED | MAP_ANONYMOUS, 0);
7702 if (IS_ERR((void *)hva))
7703 return PTR_ERR((void *)hva);
7704 } else {
7705 if (!slot->npages)
7706 return 0;
7707
7708 hva = 0;
7709 }
7710
7711 old = *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007712 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007713 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007714
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007715 m.slot = id | (i << 16);
7716 m.flags = 0;
7717 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007718 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007719 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007720 r = __kvm_set_memory_region(kvm, &m);
7721 if (r < 0)
7722 return r;
7723 }
7724
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007725 if (!size) {
7726 r = vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
7727 WARN_ON(r < 0);
7728 }
7729
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007730 return 0;
7731}
7732EXPORT_SYMBOL_GPL(__x86_set_memory_region);
7733
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007734int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007735{
7736 int r;
7737
7738 mutex_lock(&kvm->slots_lock);
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007739 r = __x86_set_memory_region(kvm, id, gpa, size);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007740 mutex_unlock(&kvm->slots_lock);
7741
7742 return r;
7743}
7744EXPORT_SYMBOL_GPL(x86_set_memory_region);
7745
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007746void kvm_arch_destroy_vm(struct kvm *kvm)
7747{
Andrew Honig27469d22013-04-18 09:38:14 -07007748 if (current->mm == kvm->mm) {
7749 /*
7750 * Free memory regions allocated on behalf of userspace,
7751 * unless the the memory map has changed due to process exit
7752 * or fd copying.
7753 */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007754 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
7755 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
7756 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
Andrew Honig27469d22013-04-18 09:38:14 -07007757 }
Sheng Yang6eb55812008-10-31 12:37:41 +08007758 kvm_iommu_unmap_guest(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08007759 kfree(kvm->arch.vpic);
7760 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007761 kvm_free_vcpus(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007762 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007763}
Zhang Xiantao0de10342007-11-20 16:25:04 +08007764
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307765void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007766 struct kvm_memory_slot *dont)
7767{
7768 int i;
7769
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007770 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7771 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007772 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007773 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007774 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007775 if (i == 0)
7776 continue;
7777
7778 if (!dont || free->arch.lpage_info[i - 1] !=
7779 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007780 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007781 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007782 }
7783 }
7784}
7785
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307786int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7787 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007788{
7789 int i;
7790
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007791 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007792 unsigned long ugfn;
7793 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007794 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007795
7796 lpages = gfn_to_index(slot->base_gfn + npages - 1,
7797 slot->base_gfn, level) + 1;
7798
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007799 slot->arch.rmap[i] =
7800 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
7801 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007802 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007803 if (i == 0)
7804 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007805
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007806 slot->arch.lpage_info[i - 1] = kvm_kvzalloc(lpages *
7807 sizeof(*slot->arch.lpage_info[i - 1]));
7808 if (!slot->arch.lpage_info[i - 1])
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007809 goto out_free;
7810
7811 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007812 slot->arch.lpage_info[i - 1][0].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007813 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007814 slot->arch.lpage_info[i - 1][lpages - 1].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007815 ugfn = slot->userspace_addr >> PAGE_SHIFT;
7816 /*
7817 * If the gfn and userspace address are not aligned wrt each
7818 * other, or if explicitly asked to, disable large page
7819 * support for this slot
7820 */
7821 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
7822 !kvm_largepages_enabled()) {
7823 unsigned long j;
7824
7825 for (j = 0; j < lpages; ++j)
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007826 slot->arch.lpage_info[i - 1][j].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007827 }
7828 }
7829
7830 return 0;
7831
7832out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007833 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01007834 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007835 slot->arch.rmap[i] = NULL;
7836 if (i == 0)
7837 continue;
7838
Thomas Huth548ef282015-02-24 21:29:25 +01007839 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007840 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007841 }
7842 return -ENOMEM;
7843}
7844
Paolo Bonzini15f46012015-05-17 21:26:08 +02007845void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007846{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09007847 /*
7848 * memslots->generation has been incremented.
7849 * mmio generation may have reached its maximum value.
7850 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007851 kvm_mmu_invalidate_mmio_sptes(kvm, slots);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007852}
7853
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007854int kvm_arch_prepare_memory_region(struct kvm *kvm,
7855 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02007856 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007857 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08007858{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007859 return 0;
7860}
7861
Kai Huang88178fd2015-01-28 10:54:27 +08007862static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
7863 struct kvm_memory_slot *new)
7864{
7865 /* Still write protect RO slot */
7866 if (new->flags & KVM_MEM_READONLY) {
7867 kvm_mmu_slot_remove_write_access(kvm, new);
7868 return;
7869 }
7870
7871 /*
7872 * Call kvm_x86_ops dirty logging hooks when they are valid.
7873 *
7874 * kvm_x86_ops->slot_disable_log_dirty is called when:
7875 *
7876 * - KVM_MR_CREATE with dirty logging is disabled
7877 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
7878 *
7879 * The reason is, in case of PML, we need to set D-bit for any slots
7880 * with dirty logging disabled in order to eliminate unnecessary GPA
7881 * logging in PML buffer (and potential PML buffer full VMEXT). This
7882 * guarantees leaving PML enabled during guest's lifetime won't have
7883 * any additonal overhead from PML when guest is running with dirty
7884 * logging disabled for memory slots.
7885 *
7886 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
7887 * to dirty logging mode.
7888 *
7889 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
7890 *
7891 * In case of write protect:
7892 *
7893 * Write protect all pages for dirty logging.
7894 *
7895 * All the sptes including the large sptes which point to this
7896 * slot are set to readonly. We can not create any new large
7897 * spte on this slot until the end of the logging.
7898 *
7899 * See the comments in fast_page_fault().
7900 */
7901 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
7902 if (kvm_x86_ops->slot_enable_log_dirty)
7903 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
7904 else
7905 kvm_mmu_slot_remove_write_access(kvm, new);
7906 } else {
7907 if (kvm_x86_ops->slot_disable_log_dirty)
7908 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
7909 }
7910}
7911
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007912void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02007913 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007914 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007915 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007916 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007917{
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007918 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007919
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007920 if (!kvm->arch.n_requested_mmu_pages)
7921 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007922
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007923 if (nr_mmu_pages)
7924 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Kai Huang1c91cad42015-01-28 10:54:26 +08007925
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007926 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08007927 * Dirty logging tracks sptes in 4k granularity, meaning that large
7928 * sptes have to be split. If live migration is successful, the guest
7929 * in the source machine will be destroyed and large sptes will be
7930 * created in the destination. However, if the guest continues to run
7931 * in the source machine (for example if live migration fails), small
7932 * sptes will remain around and cause bad performance.
7933 *
7934 * Scan sptes if dirty logging has been stopped, dropping those
7935 * which can be collapsed into a single large-page spte. Later
7936 * page faults will create the large-page sptes.
7937 */
7938 if ((change != KVM_MR_DELETE) &&
7939 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
7940 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
7941 kvm_mmu_zap_collapsible_sptes(kvm, new);
7942
7943 /*
Kai Huang88178fd2015-01-28 10:54:27 +08007944 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08007945 *
Kai Huang88178fd2015-01-28 10:54:27 +08007946 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
7947 * been zapped so no dirty logging staff is needed for old slot. For
7948 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
7949 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007950 *
7951 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007952 */
Kai Huang88178fd2015-01-28 10:54:27 +08007953 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007954 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007955}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007956
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007957void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007958{
Xiao Guangrong6ca18b62013-05-31 08:36:23 +08007959 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007960}
7961
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007962void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
7963 struct kvm_memory_slot *slot)
7964{
Xiao Guangrong6ca18b62013-05-31 08:36:23 +08007965 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007966}
7967
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02007968static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
7969{
7970 if (!list_empty_careful(&vcpu->async_pf.done))
7971 return true;
7972
7973 if (kvm_apic_has_events(vcpu))
7974 return true;
7975
7976 if (vcpu->arch.pv.pv_unhalted)
7977 return true;
7978
7979 if (atomic_read(&vcpu->arch.nmi_queued))
7980 return true;
7981
Paolo Bonzini73917732015-10-13 10:19:35 +02007982 if (test_bit(KVM_REQ_SMI, &vcpu->requests))
7983 return true;
7984
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02007985 if (kvm_arch_interrupt_allowed(vcpu) &&
7986 kvm_cpu_has_interrupt(vcpu))
7987 return true;
7988
7989 return false;
7990}
7991
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007992int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
7993{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007994 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
7995 kvm_x86_ops->check_nested_events(vcpu, false);
7996
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02007997 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007998}
Zhang Xiantao57361992007-12-17 14:21:40 +08007999
Christoffer Dallb6d33832012-03-08 16:44:24 -05008000int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08008001{
Christoffer Dallb6d33832012-03-08 16:44:24 -05008002 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08008003}
Gleb Natapov78646122009-03-23 12:12:11 +02008004
8005int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
8006{
8007 return kvm_x86_ops->interrupt_allowed(vcpu);
8008}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008009
Nadav Amit82b32772014-11-02 11:54:45 +02008010unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
8011{
8012 if (is_64_bit_mode(vcpu))
8013 return kvm_rip_read(vcpu);
8014 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
8015 kvm_rip_read(vcpu));
8016}
8017EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
8018
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008019bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
8020{
Nadav Amit82b32772014-11-02 11:54:45 +02008021 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008022}
8023EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
8024
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008025unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
8026{
8027 unsigned long rflags;
8028
8029 rflags = kvm_x86_ops->get_rflags(vcpu);
8030 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01008031 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008032 return rflags;
8033}
8034EXPORT_SYMBOL_GPL(kvm_get_rflags);
8035
Paolo Bonzini6addfc42014-03-27 11:29:28 +01008036static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008037{
8038 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008039 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01008040 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008041 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01008042}
8043
8044void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
8045{
8046 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008047 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008048}
8049EXPORT_SYMBOL_GPL(kvm_set_rflags);
8050
Gleb Natapov56028d02010-10-17 18:13:42 +02008051void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
8052{
8053 int r;
8054
Xiao Guangrongfb67e142010-12-07 10:35:25 +08008055 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08008056 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02008057 return;
8058
8059 r = kvm_mmu_reload(vcpu);
8060 if (unlikely(r))
8061 return;
8062
Xiao Guangrongfb67e142010-12-07 10:35:25 +08008063 if (!vcpu->arch.mmu.direct_map &&
8064 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
8065 return;
8066
Gleb Natapov56028d02010-10-17 18:13:42 +02008067 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
8068}
8069
Gleb Natapovaf585b92010-10-14 11:22:46 +02008070static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
8071{
8072 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
8073}
8074
8075static inline u32 kvm_async_pf_next_probe(u32 key)
8076{
8077 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
8078}
8079
8080static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8081{
8082 u32 key = kvm_async_pf_hash_fn(gfn);
8083
8084 while (vcpu->arch.apf.gfns[key] != ~0)
8085 key = kvm_async_pf_next_probe(key);
8086
8087 vcpu->arch.apf.gfns[key] = gfn;
8088}
8089
8090static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
8091{
8092 int i;
8093 u32 key = kvm_async_pf_hash_fn(gfn);
8094
8095 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08008096 (vcpu->arch.apf.gfns[key] != gfn &&
8097 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02008098 key = kvm_async_pf_next_probe(key);
8099
8100 return key;
8101}
8102
8103bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8104{
8105 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
8106}
8107
8108static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8109{
8110 u32 i, j, k;
8111
8112 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
8113 while (true) {
8114 vcpu->arch.apf.gfns[i] = ~0;
8115 do {
8116 j = kvm_async_pf_next_probe(j);
8117 if (vcpu->arch.apf.gfns[j] == ~0)
8118 return;
8119 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
8120 /*
8121 * k lies cyclically in ]i,j]
8122 * | i.k.j |
8123 * |....j i.k.| or |.k..j i...|
8124 */
8125 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
8126 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
8127 i = j;
8128 }
8129}
8130
Gleb Natapov7c907052010-10-14 11:22:53 +02008131static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
8132{
8133
8134 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
8135 sizeof(val));
8136}
8137
Gleb Natapovaf585b92010-10-14 11:22:46 +02008138void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
8139 struct kvm_async_pf *work)
8140{
Avi Kivity6389ee92010-11-29 16:12:30 +02008141 struct x86_exception fault;
8142
Gleb Natapov7c907052010-10-14 11:22:53 +02008143 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008144 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02008145
8146 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02008147 (vcpu->arch.apf.send_user_only &&
8148 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02008149 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
8150 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008151 fault.vector = PF_VECTOR;
8152 fault.error_code_valid = true;
8153 fault.error_code = 0;
8154 fault.nested_page_fault = false;
8155 fault.address = work->arch.token;
8156 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008157 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008158}
8159
8160void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
8161 struct kvm_async_pf *work)
8162{
Avi Kivity6389ee92010-11-29 16:12:30 +02008163 struct x86_exception fault;
8164
Gleb Natapov7c907052010-10-14 11:22:53 +02008165 trace_kvm_async_pf_ready(work->arch.token, work->gva);
chai wenf2e10662013-10-14 22:22:33 +08008166 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02008167 work->arch.token = ~0; /* broadcast wakeup */
8168 else
8169 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
8170
8171 if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
8172 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008173 fault.vector = PF_VECTOR;
8174 fault.error_code_valid = true;
8175 fault.error_code = 0;
8176 fault.nested_page_fault = false;
8177 fault.address = work->arch.token;
8178 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008179 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08008180 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03008181 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02008182}
8183
8184bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
8185{
8186 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
8187 return true;
8188 else
8189 return !kvm_event_needs_reinjection(vcpu) &&
8190 kvm_x86_ops->interrupt_allowed(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008191}
8192
Paolo Bonzini5544eb92015-07-07 15:41:58 +02008193void kvm_arch_start_assignment(struct kvm *kvm)
8194{
8195 atomic_inc(&kvm->arch.assigned_device_count);
8196}
8197EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
8198
8199void kvm_arch_end_assignment(struct kvm *kvm)
8200{
8201 atomic_dec(&kvm->arch.assigned_device_count);
8202}
8203EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
8204
8205bool kvm_arch_has_assigned_device(struct kvm *kvm)
8206{
8207 return atomic_read(&kvm->arch.assigned_device_count);
8208}
8209EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
8210
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06008211void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
8212{
8213 atomic_inc(&kvm->arch.noncoherent_dma_count);
8214}
8215EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
8216
8217void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
8218{
8219 atomic_dec(&kvm->arch.noncoherent_dma_count);
8220}
8221EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
8222
8223bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
8224{
8225 return atomic_read(&kvm->arch.noncoherent_dma_count);
8226}
8227EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
8228
Feng Wu87276882015-09-18 22:29:40 +08008229int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
8230 struct irq_bypass_producer *prod)
8231{
8232 struct kvm_kernel_irqfd *irqfd =
8233 container_of(cons, struct kvm_kernel_irqfd, consumer);
8234
8235 if (kvm_x86_ops->update_pi_irte) {
8236 irqfd->producer = prod;
8237 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
8238 prod->irq, irqfd->gsi, 1);
8239 }
8240
8241 return -EINVAL;
8242}
8243
8244void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
8245 struct irq_bypass_producer *prod)
8246{
8247 int ret;
8248 struct kvm_kernel_irqfd *irqfd =
8249 container_of(cons, struct kvm_kernel_irqfd, consumer);
8250
8251 if (!kvm_x86_ops->update_pi_irte) {
8252 WARN_ON(irqfd->producer != NULL);
8253 return;
8254 }
8255
8256 WARN_ON(irqfd->producer != prod);
8257 irqfd->producer = NULL;
8258
8259 /*
8260 * When producer of consumer is unregistered, we change back to
8261 * remapped mode, so we can re-use the current implementation
8262 * when the irq is masked/disabed or the consumer side (KVM
8263 * int this case doesn't want to receive the interrupts.
8264 */
8265 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
8266 if (ret)
8267 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
8268 " fails: %d\n", irqfd->consumer.token, ret);
8269}
8270
8271int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
8272 uint32_t guest_irq, bool set)
8273{
8274 if (!kvm_x86_ops->update_pi_irte)
8275 return -EINVAL;
8276
8277 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
8278}
8279
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008280EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +08008281EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008282EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
8283EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
8284EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
8285EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02008286EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02008287EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02008288EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02008289EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02008290EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02008291EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01008292EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09008293EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +02008294EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
Kai Huang843e4332015-01-28 10:54:28 +08008295EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +08008296EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);