blob: 7b25aa2725f8ba72c1752ef2befd1f0e195eecc9 [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"
Carsten Otte313a3dc2007-10-11 19:16:52 +020030
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020031#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030032#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020033#include <linux/kvm.h>
34#include <linux/fs.h>
35#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010036#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080037#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050038#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010039#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030040#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010041#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030042#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020043#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080045#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080046#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020047#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020048#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020049#include <linux/timekeeper_internal.h>
50#include <linux/pvclock_gtod.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030051#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080052
Marcelo Tosatti229456f2009-06-17 09:22:14 -030053#define CREATE_TRACE_POINTS
54#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020055
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020056#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080057#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020058#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080059#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080060#include <asm/mce.h>
Sheng Yang7cf30852010-05-17 17:08:27 +080061#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080062#include <asm/fpu-internal.h> /* Ugh! */
Sheng Yang98918832010-05-17 17:08:28 +080063#include <asm/xcr.h>
Zachary Amsden1d5f0662010-08-19 22:07:30 -100064#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030065#include <asm/div64.h>
Carsten Otte043405e2007-10-10 17:16:19 +020066
Carsten Otte313a3dc2007-10-11 19:16:52 +020067#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080068#define KVM_MAX_MCE_BANKS 32
Huang Ying5854dbc2010-10-08 16:24:14 +080069#define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
Huang Ying890ca9a2009-05-11 16:48:15 +080070
Avi Kivity0f65dd72011-04-20 13:37:53 +030071#define emul_to_vcpu(ctxt) \
72 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
73
Joerg Roedel50a37eb2008-01-31 14:57:38 +010074/* EFER defaults:
75 * - enable syscall per default because its emulated by KVM
76 * - enable LME and LMA per default on 64 bit KVM
77 */
78#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080079static
80u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010081#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080082static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010083#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020084
Avi Kivityba1389b2007-11-18 16:24:12 +020085#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
86#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050087
Gleb Natapovcb142eb2009-08-09 15:17:40 +030088static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +030089static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010090static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Avi Kivity674eea02008-02-11 18:37:23 +020091
Zhang Xiantao97896d02007-11-14 20:09:30 +080092struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030093EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080094
Rusty Russell476bc002012-01-13 09:32:18 +103095static bool ignore_msrs = 0;
96module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +020097
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -020098unsigned int min_timer_period_us = 500;
99module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
100
Joerg Roedel92a1f122011-03-25 09:44:51 +0100101bool kvm_has_tsc_control;
102EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
103u32 kvm_max_guest_tsc_khz;
104EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
105
Zachary Amsdencc578282012-02-03 15:43:50 -0200106/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
107static u32 tsc_tolerance_ppm = 250;
108module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
109
Marcelo Tosatti16a96022014-05-14 12:43:24 -0300110static bool backwards_tsc_observed = false;
111
Avi Kivity18863bd2009-09-07 11:12:18 +0300112#define KVM_NR_SHARED_MSRS 16
113
114struct kvm_shared_msrs_global {
115 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800116 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300117};
118
119struct kvm_shared_msrs {
120 struct user_return_notifier urn;
121 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800122 struct kvm_shared_msr_values {
123 u64 host;
124 u64 curr;
125 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300126};
127
128static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200129static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300130
Hollis Blanchard417bc302007-10-31 17:24:23 -0500131struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200132 { "pf_fixed", VCPU_STAT(pf_fixed) },
133 { "pf_guest", VCPU_STAT(pf_guest) },
134 { "tlb_flush", VCPU_STAT(tlb_flush) },
135 { "invlpg", VCPU_STAT(invlpg) },
136 { "exits", VCPU_STAT(exits) },
137 { "io_exits", VCPU_STAT(io_exits) },
138 { "mmio_exits", VCPU_STAT(mmio_exits) },
139 { "signal_exits", VCPU_STAT(signal_exits) },
140 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800141 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200142 { "halt_exits", VCPU_STAT(halt_exits) },
143 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530144 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200145 { "request_irq", VCPU_STAT(request_irq_exits) },
146 { "irq_exits", VCPU_STAT(irq_exits) },
147 { "host_state_reload", VCPU_STAT(host_state_reload) },
148 { "efer_reload", VCPU_STAT(efer_reload) },
149 { "fpu_reload", VCPU_STAT(fpu_reload) },
150 { "insn_emulation", VCPU_STAT(insn_emulation) },
151 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300152 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200153 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200154 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
155 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
156 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
157 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
158 { "mmu_flooded", VM_STAT(mmu_flooded) },
159 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200160 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300161 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200162 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300163 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500164 { NULL }
165};
166
Dexuan Cui2acf9232010-06-10 11:27:12 +0800167u64 __read_mostly host_xcr0;
168
Jan Kiszkab6785de2012-09-20 07:43:17 +0200169static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300170
Gleb Natapovaf585b92010-10-14 11:22:46 +0200171static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
172{
173 int i;
174 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
175 vcpu->arch.apf.gfns[i] = ~0;
176}
177
Avi Kivity18863bd2009-09-07 11:12:18 +0300178static void kvm_on_user_return(struct user_return_notifier *urn)
179{
180 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300181 struct kvm_shared_msrs *locals
182 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800183 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300184
185 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800186 values = &locals->values[slot];
187 if (values->host != values->curr) {
188 wrmsrl(shared_msrs_global.msrs[slot], values->host);
189 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300190 }
191 }
192 locals->registered = false;
193 user_return_notifier_unregister(urn);
194}
195
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800196static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300197{
Avi Kivity18863bd2009-09-07 11:12:18 +0300198 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200199 unsigned int cpu = smp_processor_id();
200 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300201
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800202 /* only read, and nobody should modify it at this time,
203 * so don't need lock */
204 if (slot >= shared_msrs_global.nr) {
205 printk(KERN_ERR "kvm: invalid MSR slot!");
206 return;
207 }
208 rdmsrl_safe(msr, &value);
209 smsr->values[slot].host = value;
210 smsr->values[slot].curr = value;
211}
212
213void kvm_define_shared_msr(unsigned slot, u32 msr)
214{
Nadav Amit0123be42014-07-24 15:06:56 +0300215 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Avi Kivity18863bd2009-09-07 11:12:18 +0300216 if (slot >= shared_msrs_global.nr)
217 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800218 shared_msrs_global.msrs[slot] = msr;
219 /* we need ensured the shared_msr_global have been updated */
220 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300221}
222EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
223
224static void kvm_shared_msr_cpu_online(void)
225{
226 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300227
228 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800229 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300230}
231
Avi Kivityd5696722009-12-02 12:28:47 +0200232void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300233{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200234 unsigned int cpu = smp_processor_id();
235 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300236
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800237 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300238 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800239 smsr->values[slot].curr = value;
240 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300241 if (!smsr->registered) {
242 smsr->urn.on_user_return = kvm_on_user_return;
243 user_return_notifier_register(&smsr->urn);
244 smsr->registered = true;
245 }
246}
247EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
248
Radim Krčmář13a34e02014-08-28 15:13:03 +0200249static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200250{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200251 unsigned int cpu = smp_processor_id();
252 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200253
254 if (smsr->registered)
255 kvm_on_user_return(&smsr->urn);
256}
257
Carsten Otte6866b832007-10-29 16:09:10 +0100258u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
259{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300260 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100261}
262EXPORT_SYMBOL_GPL(kvm_get_apic_base);
263
Jan Kiszka58cb6282014-01-24 16:48:44 +0100264int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100265{
Jan Kiszka58cb6282014-01-24 16:48:44 +0100266 u64 old_state = vcpu->arch.apic_base &
267 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
268 u64 new_state = msr_info->data &
269 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
270 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
271 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
272
273 if (!msr_info->host_initiated &&
274 ((msr_info->data & reserved_bits) != 0 ||
275 new_state == X2APIC_ENABLE ||
276 (new_state == MSR_IA32_APICBASE_ENABLE &&
277 old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
278 (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
279 old_state == 0)))
280 return 1;
281
282 kvm_lapic_set_base(vcpu, msr_info->data);
283 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100284}
285EXPORT_SYMBOL_GPL(kvm_set_apic_base);
286
Andi Kleen2605fc22014-05-02 00:44:37 +0200287asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000288{
289 /* Fault while not rebooting. We want the trace. */
290 BUG();
291}
292EXPORT_SYMBOL_GPL(kvm_spurious_fault);
293
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200294#define EXCPT_BENIGN 0
295#define EXCPT_CONTRIBUTORY 1
296#define EXCPT_PF 2
297
298static int exception_class(int vector)
299{
300 switch (vector) {
301 case PF_VECTOR:
302 return EXCPT_PF;
303 case DE_VECTOR:
304 case TS_VECTOR:
305 case NP_VECTOR:
306 case SS_VECTOR:
307 case GP_VECTOR:
308 return EXCPT_CONTRIBUTORY;
309 default:
310 break;
311 }
312 return EXCPT_BENIGN;
313}
314
Nadav Amitd6e8c852014-07-24 14:51:24 +0300315#define EXCPT_FAULT 0
316#define EXCPT_TRAP 1
317#define EXCPT_ABORT 2
318#define EXCPT_INTERRUPT 3
319
320static int exception_type(int vector)
321{
322 unsigned int mask;
323
324 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
325 return EXCPT_INTERRUPT;
326
327 mask = 1 << vector;
328
329 /* #DB is trap, as instruction watchpoints are handled elsewhere */
330 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
331 return EXCPT_TRAP;
332
333 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
334 return EXCPT_ABORT;
335
336 /* Reserved exceptions will result in fault */
337 return EXCPT_FAULT;
338}
339
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200340static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200341 unsigned nr, bool has_error, u32 error_code,
342 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200343{
344 u32 prev_nr;
345 int class1, class2;
346
Avi Kivity3842d132010-07-27 12:30:24 +0300347 kvm_make_request(KVM_REQ_EVENT, vcpu);
348
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200349 if (!vcpu->arch.exception.pending) {
350 queue:
351 vcpu->arch.exception.pending = true;
352 vcpu->arch.exception.has_error_code = has_error;
353 vcpu->arch.exception.nr = nr;
354 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200355 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200356 return;
357 }
358
359 /* to check exception */
360 prev_nr = vcpu->arch.exception.nr;
361 if (prev_nr == DF_VECTOR) {
362 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300363 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200364 return;
365 }
366 class1 = exception_class(prev_nr);
367 class2 = exception_class(nr);
368 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
369 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
370 /* generate double fault per SDM Table 5-5 */
371 vcpu->arch.exception.pending = true;
372 vcpu->arch.exception.has_error_code = true;
373 vcpu->arch.exception.nr = DF_VECTOR;
374 vcpu->arch.exception.error_code = 0;
375 } else
376 /* replace previous exception with a new one in a hope
377 that instruction re-execution will regenerate lost
378 exception */
379 goto queue;
380}
381
Avi Kivity298101d2007-11-25 13:41:11 +0200382void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
383{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200384 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200385}
386EXPORT_SYMBOL_GPL(kvm_queue_exception);
387
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200388void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
389{
390 kvm_multiple_exception(vcpu, nr, false, 0, true);
391}
392EXPORT_SYMBOL_GPL(kvm_requeue_exception);
393
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100394void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200395{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100396 if (err)
397 kvm_inject_gp(vcpu, 0);
398 else
399 kvm_x86_ops->skip_emulated_instruction(vcpu);
400}
401EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200402
Avi Kivity6389ee92010-11-29 16:12:30 +0200403void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200404{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200405 ++vcpu->stat.pf_guest;
Avi Kivity6389ee92010-11-29 16:12:30 +0200406 vcpu->arch.cr2 = fault->address;
407 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200408}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300409EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200410
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200411static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200412{
Avi Kivity6389ee92010-11-29 16:12:30 +0200413 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
414 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200415 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200416 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200417
418 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200419}
420
Sheng Yang3419ffc2008-05-15 09:52:48 +0800421void kvm_inject_nmi(struct kvm_vcpu *vcpu)
422{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300423 atomic_inc(&vcpu->arch.nmi_queued);
424 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800425}
426EXPORT_SYMBOL_GPL(kvm_inject_nmi);
427
Avi Kivity298101d2007-11-25 13:41:11 +0200428void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
429{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200430 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200431}
432EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
433
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200434void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
435{
436 kvm_multiple_exception(vcpu, nr, true, error_code, true);
437}
438EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
439
Carsten Ottea03490e2007-10-29 16:09:35 +0100440/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300441 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
442 * a #GP and return false.
443 */
444bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200445{
Avi Kivity0a79b002009-09-01 12:03:25 +0300446 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
447 return true;
448 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
449 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100450}
Avi Kivity0a79b002009-09-01 12:03:25 +0300451EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100452
453/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200454 * This function will be used to read from the physical memory of the currently
455 * running guest. The difference to kvm_read_guest_page is that this function
456 * can read from guest physical or from the guest's guest physical memory.
457 */
458int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
459 gfn_t ngfn, void *data, int offset, int len,
460 u32 access)
461{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200462 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200463 gfn_t real_gfn;
464 gpa_t ngpa;
465
466 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200467 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200468 if (real_gfn == UNMAPPED_GVA)
469 return -EFAULT;
470
471 real_gfn = gpa_to_gfn(real_gfn);
472
473 return kvm_read_guest_page(vcpu->kvm, real_gfn, data, offset, len);
474}
475EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
476
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200477int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
478 void *data, int offset, int len, u32 access)
479{
480 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
481 data, offset, len, access);
482}
483
Joerg Roedelec92fe42010-09-10 17:30:51 +0200484/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100485 * Load the pae pdptrs. Return true is they are all valid.
486 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200487int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100488{
489 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
490 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
491 int i;
492 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200493 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100494
Joerg Roedelff03a072010-09-10 17:30:57 +0200495 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
496 offset * sizeof(u64), sizeof(pdpte),
497 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100498 if (ret < 0) {
499 ret = 0;
500 goto out;
501 }
502 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300503 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800504 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100505 ret = 0;
506 goto out;
507 }
508 }
509 ret = 1;
510
Joerg Roedelff03a072010-09-10 17:30:57 +0200511 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300512 __set_bit(VCPU_EXREG_PDPTR,
513 (unsigned long *)&vcpu->arch.regs_avail);
514 __set_bit(VCPU_EXREG_PDPTR,
515 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100516out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100517
518 return ret;
519}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100520EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100521
Avi Kivityd835dfe2007-11-21 02:57:59 +0200522static bool pdptrs_changed(struct kvm_vcpu *vcpu)
523{
Joerg Roedelff03a072010-09-10 17:30:57 +0200524 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200525 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200526 int offset;
527 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200528 int r;
529
530 if (is_long_mode(vcpu) || !is_pae(vcpu))
531 return false;
532
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300533 if (!test_bit(VCPU_EXREG_PDPTR,
534 (unsigned long *)&vcpu->arch.regs_avail))
535 return true;
536
Avi Kivity9f8fe502010-12-05 17:30:00 +0200537 gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
538 offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200539 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
540 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200541 if (r < 0)
542 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200543 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200544out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200545
546 return changed;
547}
548
Avi Kivity49a9b072010-06-10 17:02:14 +0300549int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100550{
Sheng Yangaad82702010-05-12 16:40:42 +0800551 unsigned long old_cr0 = kvm_read_cr0(vcpu);
552 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
553 X86_CR0_CD | X86_CR0_NW;
554
Avi Kivityf9a48e62010-01-06 19:10:22 +0200555 cr0 |= X86_CR0_ET;
556
Gleb Natapovab344822010-01-21 15:28:46 +0200557#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300558 if (cr0 & 0xffffffff00000000UL)
559 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200560#endif
561
562 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100563
Gleb Natapov0f122442010-04-28 19:15:31 +0300564 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
565 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100566
Gleb Natapov0f122442010-04-28 19:15:31 +0300567 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
568 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100569
570 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
571#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200572 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100573 int cs_db, cs_l;
574
Gleb Natapov0f122442010-04-28 19:15:31 +0300575 if (!is_pae(vcpu))
576 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100577 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300578 if (cs_l)
579 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100580 } else
581#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200582 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200583 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300584 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100585 }
586
Mao, Junjiead756a12012-07-02 01:18:48 +0000587 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
588 return 1;
589
Carsten Ottea03490e2007-10-29 16:09:35 +0100590 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100591
Lai Jiangshand170c412011-02-21 11:21:30 +0800592 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800593 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800594 kvm_async_pf_hash_reset(vcpu);
595 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800596
Sheng Yangaad82702010-05-12 16:40:42 +0800597 if ((cr0 ^ old_cr0) & update_bits)
598 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300599 return 0;
600}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200601EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100602
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200603void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100604{
Avi Kivity49a9b072010-06-10 17:02:14 +0300605 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100606}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200607EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100608
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300609static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
610{
611 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
612 !vcpu->guest_xcr0_loaded) {
613 /* kvm_set_xcr() also depends on this */
614 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
615 vcpu->guest_xcr0_loaded = 1;
616 }
617}
618
619static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
620{
621 if (vcpu->guest_xcr0_loaded) {
622 if (vcpu->arch.xcr0 != host_xcr0)
623 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
624 vcpu->guest_xcr0_loaded = 0;
625 }
626}
627
Dexuan Cui2acf9232010-06-10 11:27:12 +0800628int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
629{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000630 u64 xcr0 = xcr;
631 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200632 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800633
634 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
635 if (index != XCR_XFEATURE_ENABLED_MASK)
636 return 1;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800637 if (!(xcr0 & XSTATE_FP))
638 return 1;
639 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
640 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200641
642 /*
643 * Do not allow the guest to set bits that we do not support
644 * saving. However, xcr0 bit 0 is always set, even if the
645 * emulated CPU does not support XSAVE (see fx_init).
646 */
647 valid_bits = vcpu->arch.guest_supported_xcr0 | XSTATE_FP;
648 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800649 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200650
Liu, Jinsong390bd522014-02-24 10:58:09 +0000651 if ((!(xcr0 & XSTATE_BNDREGS)) != (!(xcr0 & XSTATE_BNDCSR)))
652 return 1;
653
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300654 kvm_put_guest_xcr0(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800655 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000656
657 if ((xcr0 ^ old_xcr0) & XSTATE_EXTEND_MASK)
658 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800659 return 0;
660}
661
662int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
663{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000664 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
665 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800666 kvm_inject_gp(vcpu, 0);
667 return 1;
668 }
669 return 0;
670}
671EXPORT_SYMBOL_GPL(kvm_set_xcr);
672
Avi Kivitya83b29c2010-06-10 17:02:15 +0300673int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100674{
Avi Kivityfc78f512009-12-07 12:16:48 +0200675 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800676 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE |
677 X86_CR4_PAE | X86_CR4_SMEP;
Gleb Natapov0f122442010-04-28 19:15:31 +0300678 if (cr4 & CR4_RESERVED_BITS)
679 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100680
Dexuan Cui2acf9232010-06-10 11:27:12 +0800681 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
682 return 1;
683
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800684 if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
685 return 1;
686
Feng Wu97ec8c02014-04-01 17:46:34 +0800687 if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
688 return 1;
689
H. Peter Anvinafcbf132013-04-27 16:37:47 -0700690 if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800691 return 1;
692
Carsten Ottea03490e2007-10-29 16:09:35 +0100693 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300694 if (!(cr4 & X86_CR4_PAE))
695 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300696 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
697 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200698 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
699 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300700 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100701
Mao, Junjiead756a12012-07-02 01:18:48 +0000702 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
703 if (!guest_cpuid_has_pcid(vcpu))
704 return 1;
705
706 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
707 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
708 return 1;
709 }
710
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300711 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300712 return 1;
713
Mao, Junjiead756a12012-07-02 01:18:48 +0000714 if (((cr4 ^ old_cr4) & pdptr_bits) ||
715 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800716 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300717
Feng Wu97ec8c02014-04-01 17:46:34 +0800718 if ((cr4 ^ old_cr4) & X86_CR4_SMAP)
719 update_permission_bitmask(vcpu, vcpu->arch.walk_mmu, false);
720
Dexuan Cui2acf9232010-06-10 11:27:12 +0800721 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +0200722 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800723
Gleb Natapov0f122442010-04-28 19:15:31 +0300724 return 0;
725}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200726EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100727
Avi Kivity23902182010-06-10 17:02:16 +0300728int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100729{
Avi Kivity9f8fe502010-12-05 17:30:00 +0200730 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300731 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200732 kvm_mmu_flush_tlb(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300733 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200734 }
735
Carsten Ottea03490e2007-10-29 16:09:35 +0100736 if (is_long_mode(vcpu)) {
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200737 if (cr3 & CR3_L_MODE_RESERVED_BITS)
738 return 1;
739 } else if (is_pae(vcpu) && is_paging(vcpu) &&
740 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300741 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100742
Gleb Natapov0f122442010-04-28 19:15:31 +0300743 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200744 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Paolo Bonzinid8d173d2013-10-02 16:56:11 +0200745 kvm_mmu_new_cr3(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300746 return 0;
747}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200748EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100749
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100750int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100751{
Gleb Natapov0f122442010-04-28 19:15:31 +0300752 if (cr8 & CR8_RESERVED_BITS)
753 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100754 if (irqchip_in_kernel(vcpu->kvm))
755 kvm_lapic_set_tpr(vcpu, cr8);
756 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800757 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300758 return 0;
759}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200760EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100761
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200762unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100763{
764 if (irqchip_in_kernel(vcpu->kvm))
765 return kvm_lapic_get_cr8(vcpu);
766 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800767 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100768}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200769EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100770
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100771static void kvm_update_dr6(struct kvm_vcpu *vcpu)
772{
773 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
774 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
775}
776
Jan Kiszkac8639012012-09-21 05:42:55 +0200777static void kvm_update_dr7(struct kvm_vcpu *vcpu)
778{
779 unsigned long dr7;
780
781 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
782 dr7 = vcpu->arch.guest_debug_dr7;
783 else
784 dr7 = vcpu->arch.dr7;
785 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100786 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
787 if (dr7 & DR7_BP_EN_MASK)
788 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200789}
790
Nadav Amit6f43ed02014-07-15 17:37:46 +0300791static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
792{
793 u64 fixed = DR6_FIXED_1;
794
795 if (!guest_cpuid_has_rtm(vcpu))
796 fixed |= DR6_RTM;
797 return fixed;
798}
799
Gleb Natapov338dbc92010-04-28 19:15:32 +0300800static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300801{
802 switch (dr) {
803 case 0 ... 3:
804 vcpu->arch.db[dr] = val;
805 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
806 vcpu->arch.eff_db[dr] = val;
807 break;
808 case 4:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300809 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
810 return 1; /* #UD */
Gleb Natapov020df072010-04-13 10:05:23 +0300811 /* fall through */
812 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300813 if (val & 0xffffffff00000000ULL)
814 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300815 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100816 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300817 break;
818 case 5:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300819 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
820 return 1; /* #UD */
Gleb Natapov020df072010-04-13 10:05:23 +0300821 /* fall through */
822 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300823 if (val & 0xffffffff00000000ULL)
824 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300825 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200826 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300827 break;
828 }
829
830 return 0;
831}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300832
833int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
834{
835 int res;
836
837 res = __kvm_set_dr(vcpu, dr, val);
838 if (res > 0)
839 kvm_queue_exception(vcpu, UD_VECTOR);
840 else if (res < 0)
841 kvm_inject_gp(vcpu, 0);
842
843 return res;
844}
Gleb Natapov020df072010-04-13 10:05:23 +0300845EXPORT_SYMBOL_GPL(kvm_set_dr);
846
Gleb Natapov338dbc92010-04-28 19:15:32 +0300847static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300848{
849 switch (dr) {
850 case 0 ... 3:
851 *val = vcpu->arch.db[dr];
852 break;
853 case 4:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300854 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Gleb Natapov020df072010-04-13 10:05:23 +0300855 return 1;
Gleb Natapov020df072010-04-13 10:05:23 +0300856 /* fall through */
857 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100858 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
859 *val = vcpu->arch.dr6;
860 else
861 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300862 break;
863 case 5:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300864 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Gleb Natapov020df072010-04-13 10:05:23 +0300865 return 1;
Gleb Natapov020df072010-04-13 10:05:23 +0300866 /* fall through */
867 default: /* 7 */
868 *val = vcpu->arch.dr7;
869 break;
870 }
871
872 return 0;
873}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300874
875int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
876{
877 if (_kvm_get_dr(vcpu, dr, val)) {
878 kvm_queue_exception(vcpu, UD_VECTOR);
879 return 1;
880 }
881 return 0;
882}
Gleb Natapov020df072010-04-13 10:05:23 +0300883EXPORT_SYMBOL_GPL(kvm_get_dr);
884
Avi Kivity022cd0e2011-11-10 14:57:23 +0200885bool kvm_rdpmc(struct kvm_vcpu *vcpu)
886{
887 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
888 u64 data;
889 int err;
890
891 err = kvm_pmu_read_pmc(vcpu, ecx, &data);
892 if (err)
893 return err;
894 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
895 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
896 return err;
897}
898EXPORT_SYMBOL_GPL(kvm_rdpmc);
899
Carsten Otte043405e2007-10-10 17:16:19 +0200900/*
901 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
902 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
903 *
904 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400905 * capabilities of the host cpu. This capabilities test skips MSRs that are
906 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200907 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400908
Vadim Rozenfelde9840972014-01-16 20:18:37 +1100909#define KVM_SAVE_MSRS_BEGIN 12
Carsten Otte043405e2007-10-10 17:16:19 +0200910static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400911 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Glauber Costa11c6bff2010-05-11 12:17:41 -0400912 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200913 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Vadim Rozenfelde9840972014-01-16 20:18:37 +1100914 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Glauber Costac9aaa892011-07-11 15:28:14 -0400915 HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300916 MSR_KVM_PV_EOI_EN,
Carsten Otte043405e2007-10-10 17:16:19 +0200917 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400918 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200919#ifdef CONFIG_X86_64
920 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
921#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800922 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Liu, Jinsong0dd376e2014-02-24 10:56:53 +0000923 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS
Carsten Otte043405e2007-10-10 17:16:19 +0200924};
925
926static unsigned num_msrs_to_save;
927
Mathias Krausef1d24832012-08-30 01:30:18 +0200928static const u32 emulated_msrs[] = {
Will Auldba904632012-11-29 12:42:50 -0800929 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800930 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +0200931 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300932 MSR_IA32_MCG_STATUS,
933 MSR_IA32_MCG_CTL,
Carsten Otte043405e2007-10-10 17:16:19 +0200934};
935
Jan Kiszka384bb782013-04-20 10:52:36 +0200936bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100937{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200938 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +0200939 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +0100940
Alexander Graf1b2fd702009-02-02 16:23:51 +0100941 if (efer & EFER_FFXSR) {
942 struct kvm_cpuid_entry2 *feat;
943
944 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200945 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200946 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +0100947 }
948
Alexander Grafd8017472008-11-25 20:17:11 +0100949 if (efer & EFER_SVME) {
950 struct kvm_cpuid_entry2 *feat;
951
952 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200953 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200954 return false;
Alexander Grafd8017472008-11-25 20:17:11 +0100955 }
956
Jan Kiszka384bb782013-04-20 10:52:36 +0200957 return true;
958}
959EXPORT_SYMBOL_GPL(kvm_valid_efer);
960
961static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
962{
963 u64 old_efer = vcpu->arch.efer;
964
965 if (!kvm_valid_efer(vcpu, efer))
966 return 1;
967
968 if (is_paging(vcpu)
969 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
970 return 1;
971
Carsten Otte15c4a642007-10-30 18:44:17 +0100972 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200973 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100974
Sheng Yanga3d204e2010-05-12 16:40:40 +0800975 kvm_x86_ops->set_efer(vcpu, efer);
976
Sheng Yangaad82702010-05-12 16:40:42 +0800977 /* Update reserved bits */
978 if ((efer ^ old_efer) & EFER_NX)
979 kvm_mmu_reset_context(vcpu);
980
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200981 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +0100982}
983
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100984void kvm_enable_efer_bits(u64 mask)
985{
986 efer_reserved_bits &= ~mask;
987}
988EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
989
990
Carsten Otte15c4a642007-10-30 18:44:17 +0100991/*
992 * Writes msr value into into the appropriate "register".
993 * Returns 0 on success, non-0 otherwise.
994 * Assumes vcpu_load() was already called.
995 */
Will Auld8fe8ab42012-11-29 12:42:12 -0800996int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +0100997{
Will Auld8fe8ab42012-11-29 12:42:12 -0800998 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +0100999}
1000
Carsten Otte313a3dc2007-10-11 19:16:52 +02001001/*
1002 * Adapt set_msr() to msr_io()'s calling convention
1003 */
1004static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1005{
Will Auld8fe8ab42012-11-29 12:42:12 -08001006 struct msr_data msr;
1007
1008 msr.data = *data;
1009 msr.index = index;
1010 msr.host_initiated = true;
1011 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001012}
1013
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001014#ifdef CONFIG_X86_64
1015struct pvclock_gtod_data {
1016 seqcount_t seq;
1017
1018 struct { /* extract of a clocksource struct */
1019 int vclock_mode;
1020 cycle_t cycle_last;
1021 cycle_t mask;
1022 u32 mult;
1023 u32 shift;
1024 } clock;
1025
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001026 u64 boot_ns;
1027 u64 nsec_base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001028};
1029
1030static struct pvclock_gtod_data pvclock_gtod_data;
1031
1032static void update_pvclock_gtod(struct timekeeper *tk)
1033{
1034 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001035 u64 boot_ns;
1036
Thomas Gleixnerd28ede82014-07-16 21:05:16 +00001037 boot_ns = ktime_to_ns(ktime_add(tk->tkr.base_mono, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001038
1039 write_seqcount_begin(&vdata->seq);
1040
1041 /* copy pvclock gtod data */
Thomas Gleixnerd28ede82014-07-16 21:05:16 +00001042 vdata->clock.vclock_mode = tk->tkr.clock->archdata.vclock_mode;
1043 vdata->clock.cycle_last = tk->tkr.cycle_last;
1044 vdata->clock.mask = tk->tkr.mask;
1045 vdata->clock.mult = tk->tkr.mult;
1046 vdata->clock.shift = tk->tkr.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001047
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001048 vdata->boot_ns = boot_ns;
Thomas Gleixnerd28ede82014-07-16 21:05:16 +00001049 vdata->nsec_base = tk->tkr.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001050
1051 write_seqcount_end(&vdata->seq);
1052}
1053#endif
1054
1055
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001056static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1057{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001058 int version;
1059 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001060 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +08001061 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001062
1063 if (!wall_clock)
1064 return;
1065
Avi Kivity9ed3c442010-05-04 15:00:37 +03001066 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1067 if (r)
1068 return;
1069
1070 if (version & 1)
1071 ++version; /* first time write, random junk */
1072
1073 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001074
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001075 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
1076
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001077 /*
1078 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001079 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001080 * wall clock specified here. guest system time equals host
1081 * system time for us, thus we must fill in host boot time here.
1082 */
Jason Wang923de3c2010-01-27 19:13:49 +08001083 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001084
Bruce Rogers4b648662012-07-20 10:44:24 -06001085 if (kvm->arch.kvmclock_offset) {
1086 struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
1087 boot = timespec_sub(boot, ts);
1088 }
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001089 wc.sec = boot.tv_sec;
1090 wc.nsec = boot.tv_nsec;
1091 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001092
1093 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1094
1095 version++;
1096 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001097}
1098
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001099static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1100{
1101 uint32_t quotient, remainder;
1102
1103 /* Don't try to replace with do_div(), this one calculates
1104 * "(dividend << 32) / divisor" */
1105 __asm__ ( "divl %4"
1106 : "=a" (quotient), "=d" (remainder)
1107 : "0" (0), "1" (dividend), "r" (divisor) );
1108 return quotient;
1109}
1110
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001111static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
1112 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001113{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001114 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001115 int32_t shift = 0;
1116 uint64_t tps64;
1117 uint32_t tps32;
1118
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001119 tps64 = base_khz * 1000LL;
1120 scaled64 = scaled_khz * 1000LL;
Jan Kiszka50933622010-09-26 13:00:53 +02001121 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001122 tps64 >>= 1;
1123 shift--;
1124 }
1125
1126 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001127 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1128 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001129 scaled64 >>= 1;
1130 else
1131 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001132 shift++;
1133 }
1134
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001135 *pshift = shift;
1136 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001137
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001138 pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
1139 __func__, base_khz, scaled_khz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001140}
1141
Zachary Amsden759379d2010-08-19 22:07:25 -10001142static inline u64 get_kernel_ns(void)
1143{
Thomas Gleixnerbb0b5812014-07-16 21:04:52 +00001144 return ktime_get_boot_ns();
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001145}
1146
Marcelo Tosattid8281992012-11-27 23:29:01 -02001147#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001148static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001149#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001150
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001151static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001152unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001153
Joerg Roedel857e4092011-03-25 09:44:50 +01001154static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Zachary Amsden759379d2010-08-19 22:07:25 -10001155{
Zachary Amsdencc578282012-02-03 15:43:50 -02001156 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
1157 vcpu->arch.virtual_tsc_shift);
Zachary Amsden759379d2010-08-19 22:07:25 -10001158}
1159
Zachary Amsdencc578282012-02-03 15:43:50 -02001160static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001161{
Zachary Amsdencc578282012-02-03 15:43:50 -02001162 u64 v = (u64)khz * (1000000 + ppm);
1163 do_div(v, 1000000);
1164 return v;
1165}
1166
1167static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
1168{
1169 u32 thresh_lo, thresh_hi;
1170 int use_scaling = 0;
1171
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001172 /* tsc_khz can be zero if TSC calibration fails */
1173 if (this_tsc_khz == 0)
1174 return;
1175
Zachary Amsdenc2855452010-09-18 14:38:15 -10001176 /* Compute a scale to convert nanoseconds in TSC cycles */
1177 kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
Zachary Amsdencc578282012-02-03 15:43:50 -02001178 &vcpu->arch.virtual_tsc_shift,
1179 &vcpu->arch.virtual_tsc_mult);
1180 vcpu->arch.virtual_tsc_khz = this_tsc_khz;
1181
1182 /*
1183 * Compute the variation in TSC rate which is acceptable
1184 * within the range of tolerance and decide if the
1185 * rate being applied is within that bounds of the hardware
1186 * rate. If so, no scaling or compensation need be done.
1187 */
1188 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1189 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
1190 if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
1191 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
1192 use_scaling = 1;
1193 }
1194 kvm_x86_ops->set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001195}
1196
1197static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1198{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001199 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001200 vcpu->arch.virtual_tsc_mult,
1201 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001202 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001203 return tsc;
1204}
1205
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001206void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
1207{
1208#ifdef CONFIG_X86_64
1209 bool vcpus_matched;
1210 bool do_request = false;
1211 struct kvm_arch *ka = &vcpu->kvm->arch;
1212 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1213
1214 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1215 atomic_read(&vcpu->kvm->online_vcpus));
1216
1217 if (vcpus_matched && gtod->clock.vclock_mode == VCLOCK_TSC)
1218 if (!ka->use_master_clock)
1219 do_request = 1;
1220
1221 if (!vcpus_matched && ka->use_master_clock)
1222 do_request = 1;
1223
1224 if (do_request)
1225 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1226
1227 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1228 atomic_read(&vcpu->kvm->online_vcpus),
1229 ka->use_master_clock, gtod->clock.vclock_mode);
1230#endif
1231}
1232
Will Auldba904632012-11-29 12:42:50 -08001233static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1234{
1235 u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
1236 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1237}
1238
Will Auld8fe8ab42012-11-29 12:42:12 -08001239void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001240{
1241 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001242 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001243 unsigned long flags;
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001244 s64 usdiff;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001245 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001246 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001247 u64 data = msr->data;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001248
Jan Kiszka038f8c12011-02-04 10:49:11 +01001249 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Joerg Roedel857e4092011-03-25 09:44:50 +01001250 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001251 ns = get_kernel_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001252 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001253
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001254 if (vcpu->arch.virtual_tsc_khz) {
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001255 int faulted = 0;
1256
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001257 /* n.b - signed multiplication and division required */
1258 usdiff = data - kvm->arch.last_tsc_write;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001259#ifdef CONFIG_X86_64
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001260 usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001261#else
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001262 /* do_div() only does unsigned */
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001263 asm("1: idivl %[divisor]\n"
1264 "2: xor %%edx, %%edx\n"
1265 " movl $0, %[faulted]\n"
1266 "3:\n"
1267 ".section .fixup,\"ax\"\n"
1268 "4: movl $1, %[faulted]\n"
1269 " jmp 3b\n"
1270 ".previous\n"
1271
1272 _ASM_EXTABLE(1b, 4b)
1273
1274 : "=A"(usdiff), [faulted] "=r" (faulted)
1275 : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
1276
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001277#endif
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001278 do_div(elapsed, 1000);
1279 usdiff -= elapsed;
1280 if (usdiff < 0)
1281 usdiff = -usdiff;
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001282
1283 /* idivl overflow => difference is larger than USEC_PER_SEC */
1284 if (faulted)
1285 usdiff = USEC_PER_SEC;
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001286 } else
1287 usdiff = USEC_PER_SEC; /* disable TSC match window below */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001288
1289 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001290 * Special case: TSC write with a small delta (1 second) of virtual
1291 * cycle time against real time is interpreted as an attempt to
1292 * synchronize the CPU.
1293 *
1294 * For a reliable TSC, we can match TSC offsets, and for an unstable
1295 * TSC, we add elapsed time in this computation. We could let the
1296 * compensation code attempt to catch up if we fall behind, but
1297 * it's better to try to match offsets from the beginning.
1298 */
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001299 if (usdiff < USEC_PER_SEC &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001300 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001301 if (!check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001302 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001303 pr_debug("kvm: matched tsc offset for %llu\n", data);
1304 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001305 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001306 data += delta;
1307 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001308 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001309 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001310 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001311 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001312 } else {
1313 /*
1314 * We split periods of matched TSC writes into generations.
1315 * For each generation, we track the original measured
1316 * nanosecond time, offset, and write, so if TSCs are in
1317 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001318 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001319 *
1320 * These values are tracked in kvm->arch.cur_xxx variables.
1321 */
1322 kvm->arch.cur_tsc_generation++;
1323 kvm->arch.cur_tsc_nsec = ns;
1324 kvm->arch.cur_tsc_write = data;
1325 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001326 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001327 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001328 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001329 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001330
1331 /*
1332 * We also track th most recent recorded KHZ, write and time to
1333 * allow the matching interval to be extended at each write.
1334 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001335 kvm->arch.last_tsc_nsec = ns;
1336 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001337 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001338
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001339 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001340
1341 /* Keep track of which generation this VCPU has synchronized to */
1342 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1343 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1344 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1345
Will Auldba904632012-11-29 12:42:50 -08001346 if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
1347 update_ia32_tsc_adjust_msr(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001348 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1349 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001350
1351 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001352 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001353 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001354 } else if (!already_matched) {
1355 kvm->arch.nr_vcpus_matched_tsc++;
1356 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001357
1358 kvm_track_tsc_matching(vcpu);
1359 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001360}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001361
Zachary Amsden99e3e302010-08-19 22:07:17 -10001362EXPORT_SYMBOL_GPL(kvm_write_tsc);
1363
Marcelo Tosattid8281992012-11-27 23:29:01 -02001364#ifdef CONFIG_X86_64
1365
1366static cycle_t read_tsc(void)
1367{
1368 cycle_t ret;
1369 u64 last;
1370
1371 /*
1372 * Empirically, a fence (of type that depends on the CPU)
1373 * before rdtsc is enough to ensure that rdtsc is ordered
1374 * with respect to loads. The various CPU manuals are unclear
1375 * as to whether rdtsc can be reordered with later loads,
1376 * but no one has ever seen it happen.
1377 */
1378 rdtsc_barrier();
1379 ret = (cycle_t)vget_cycles();
1380
1381 last = pvclock_gtod_data.clock.cycle_last;
1382
1383 if (likely(ret >= last))
1384 return ret;
1385
1386 /*
1387 * GCC likes to generate cmov here, but this branch is extremely
1388 * predictable (it's just a funciton of time and the likely is
1389 * very likely) and there's a data dependence, so force GCC
1390 * to generate a branch instead. I don't barrier() because
1391 * we don't actually need a barrier, and if this function
1392 * ever gets inlined it will generate worse code.
1393 */
1394 asm volatile ("");
1395 return last;
1396}
1397
1398static inline u64 vgettsc(cycle_t *cycle_now)
1399{
1400 long v;
1401 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1402
1403 *cycle_now = read_tsc();
1404
1405 v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
1406 return v * gtod->clock.mult;
1407}
1408
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001409static int do_monotonic_boot(s64 *t, cycle_t *cycle_now)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001410{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001411 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001412 unsigned long seq;
1413 int mode;
1414 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001415
Marcelo Tosattid8281992012-11-27 23:29:01 -02001416 do {
1417 seq = read_seqcount_begin(&gtod->seq);
1418 mode = gtod->clock.vclock_mode;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001419 ns = gtod->nsec_base;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001420 ns += vgettsc(cycle_now);
1421 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001422 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001423 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001424 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001425
1426 return mode;
1427}
1428
1429/* returns true if host is using tsc clocksource */
1430static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
1431{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001432 /* checked again under seqlock below */
1433 if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
1434 return false;
1435
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001436 return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001437}
1438#endif
1439
1440/*
1441 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001442 * Assuming a stable TSC across physical CPUS, and a stable TSC
1443 * across virtual CPUs, the following condition is possible.
1444 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001445 * CPUs at the next numbered event.
1446 *
1447 * "timespecX" represents host monotonic time. "tscX" represents
1448 * RDTSC value.
1449 *
1450 * VCPU0 on CPU0 | VCPU1 on CPU1
1451 *
1452 * 1. read timespec0,tsc0
1453 * 2. | timespec1 = timespec0 + N
1454 * | tsc1 = tsc0 + M
1455 * 3. transition to guest | transition to guest
1456 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1457 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1458 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1459 *
1460 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1461 *
1462 * - ret0 < ret1
1463 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1464 * ...
1465 * - 0 < N - M => M < N
1466 *
1467 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1468 * always the case (the difference between two distinct xtime instances
1469 * might be smaller then the difference between corresponding TSC reads,
1470 * when updating guest vcpus pvclock areas).
1471 *
1472 * To avoid that problem, do not allow visibility of distinct
1473 * system_timestamp/tsc_timestamp values simultaneously: use a master
1474 * copy of host monotonic time values. Update that master copy
1475 * in lockstep.
1476 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001477 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001478 *
1479 */
1480
1481static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1482{
1483#ifdef CONFIG_X86_64
1484 struct kvm_arch *ka = &kvm->arch;
1485 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001486 bool host_tsc_clocksource, vcpus_matched;
1487
1488 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1489 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001490
1491 /*
1492 * If the host uses TSC clock, then passthrough TSC as stable
1493 * to the guest.
1494 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001495 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001496 &ka->master_kernel_ns,
1497 &ka->master_cycle_now);
1498
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001499 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
1500 && !backwards_tsc_observed;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001501
Marcelo Tosattid8281992012-11-27 23:29:01 -02001502 if (ka->use_master_clock)
1503 atomic_set(&kvm_guest_has_master_clock, 1);
1504
1505 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001506 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1507 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001508#endif
1509}
1510
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001511static void kvm_gen_update_masterclock(struct kvm *kvm)
1512{
1513#ifdef CONFIG_X86_64
1514 int i;
1515 struct kvm_vcpu *vcpu;
1516 struct kvm_arch *ka = &kvm->arch;
1517
1518 spin_lock(&ka->pvclock_gtod_sync_lock);
1519 kvm_make_mclock_inprogress_request(kvm);
1520 /* no guest entries from this point */
1521 pvclock_update_vm_gtod_copy(kvm);
1522
1523 kvm_for_each_vcpu(i, vcpu, kvm)
1524 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
1525
1526 /* guest entries allowed */
1527 kvm_for_each_vcpu(i, vcpu, kvm)
1528 clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
1529
1530 spin_unlock(&ka->pvclock_gtod_sync_lock);
1531#endif
1532}
1533
Zachary Amsden34c238a2010-09-18 14:38:14 -10001534static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001535{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001536 unsigned long flags, this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001537 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001538 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02001539 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001540 u64 tsc_timestamp, host_tsc;
Andy Honig0b794592013-02-20 14:48:10 -08001541 struct pvclock_vcpu_time_info guest_hv_clock;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001542 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001543 bool use_master_clock;
1544
1545 kernel_ns = 0;
1546 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001547
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001548 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02001549 * If the host uses TSC clock, then passthrough TSC as stable
1550 * to the guest.
1551 */
1552 spin_lock(&ka->pvclock_gtod_sync_lock);
1553 use_master_clock = ka->use_master_clock;
1554 if (use_master_clock) {
1555 host_tsc = ka->master_cycle_now;
1556 kernel_ns = ka->master_kernel_ns;
1557 }
1558 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001559
1560 /* Keep irq disabled to prevent changes to the clock */
1561 local_irq_save(flags);
1562 this_tsc_khz = __get_cpu_var(cpu_tsc_khz);
1563 if (unlikely(this_tsc_khz == 0)) {
1564 local_irq_restore(flags);
1565 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
1566 return 1;
1567 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001568 if (!use_master_clock) {
1569 host_tsc = native_read_tsc();
1570 kernel_ns = get_kernel_ns();
1571 }
1572
1573 tsc_timestamp = kvm_x86_ops->read_l1_tsc(v, host_tsc);
1574
1575 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10001576 * We may have to catch up the TSC to match elapsed wall clock
1577 * time for two reasons, even if kvmclock is used.
1578 * 1) CPU could have been running below the maximum TSC rate
1579 * 2) Broken TSC compensation resets the base at each VCPU
1580 * entry to avoid unknown leaps of TSC even when running
1581 * again on the same CPU. This may cause apparent elapsed
1582 * time to disappear, and the guest to stand still or run
1583 * very slowly.
1584 */
1585 if (vcpu->tsc_catchup) {
1586 u64 tsc = compute_guest_tsc(v, kernel_ns);
1587 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001588 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001589 tsc_timestamp = tsc;
1590 }
1591 }
1592
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001593 local_irq_restore(flags);
1594
Andy Honig0b794592013-02-20 14:48:10 -08001595 if (!vcpu->pv_time_enabled)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001596 return 0;
1597
Zachary Amsdene48672f2010-08-19 22:07:23 -10001598 if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001599 kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
1600 &vcpu->hv_clock.tsc_shift,
1601 &vcpu->hv_clock.tsc_to_system_mul);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001602 vcpu->hw_tsc_khz = this_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001603 }
1604
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001605 /* With all the info we got, fill in the values */
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001606 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10001607 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10001608 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001609
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001610 /*
1611 * The interface expects us to write an even number signaling that the
1612 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001613 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001614 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001615 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001616
Andy Honig0b794592013-02-20 14:48:10 -08001617 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1618 &guest_hv_clock, sizeof(guest_hv_clock))))
1619 return 0;
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001620
1621 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
Andy Honig0b794592013-02-20 14:48:10 -08001622 pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001623
1624 if (vcpu->pvclock_set_guest_stopped_request) {
1625 pvclock_flags |= PVCLOCK_GUEST_STOPPED;
1626 vcpu->pvclock_set_guest_stopped_request = false;
1627 }
1628
Marcelo Tosattid8281992012-11-27 23:29:01 -02001629 /* If the host uses TSC clocksource, then it is stable */
1630 if (use_master_clock)
1631 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
1632
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001633 vcpu->hv_clock.flags = pvclock_flags;
1634
Andy Honig0b794592013-02-20 14:48:10 -08001635 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1636 &vcpu->hv_clock,
1637 sizeof(vcpu->hv_clock));
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001638 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001639}
1640
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001641/*
1642 * kvmclock updates which are isolated to a given vcpu, such as
1643 * vcpu->cpu migration, should not allow system_timestamp from
1644 * the rest of the vcpus to remain static. Otherwise ntp frequency
1645 * correction applies to one vcpu's system_timestamp but not
1646 * the others.
1647 *
1648 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01001649 * We need to rate-limit these requests though, as they can
1650 * considerably slow guests that have a large number of vcpus.
1651 * The time for a remote vcpu to update its kvmclock is bound
1652 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001653 */
1654
Andrew Jones7e44e442014-02-28 12:52:54 +01001655#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
1656
1657static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001658{
1659 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01001660 struct delayed_work *dwork = to_delayed_work(work);
1661 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1662 kvmclock_update_work);
1663 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001664 struct kvm_vcpu *vcpu;
1665
1666 kvm_for_each_vcpu(i, vcpu, kvm) {
1667 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
1668 kvm_vcpu_kick(vcpu);
1669 }
1670}
1671
Andrew Jones7e44e442014-02-28 12:52:54 +01001672static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
1673{
1674 struct kvm *kvm = v->kvm;
1675
1676 set_bit(KVM_REQ_CLOCK_UPDATE, &v->requests);
1677 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
1678 KVMCLOCK_UPDATE_DELAY);
1679}
1680
Andrew Jones332967a2014-02-28 12:52:55 +01001681#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
1682
1683static void kvmclock_sync_fn(struct work_struct *work)
1684{
1685 struct delayed_work *dwork = to_delayed_work(work);
1686 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1687 kvmclock_sync_work);
1688 struct kvm *kvm = container_of(ka, struct kvm, arch);
1689
1690 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
1691 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
1692 KVMCLOCK_SYNC_PERIOD);
1693}
1694
Avi Kivity9ba075a2008-05-26 20:06:35 +03001695static bool msr_mtrr_valid(unsigned msr)
1696{
1697 switch (msr) {
1698 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
1699 case MSR_MTRRfix64K_00000:
1700 case MSR_MTRRfix16K_80000:
1701 case MSR_MTRRfix16K_A0000:
1702 case MSR_MTRRfix4K_C0000:
1703 case MSR_MTRRfix4K_C8000:
1704 case MSR_MTRRfix4K_D0000:
1705 case MSR_MTRRfix4K_D8000:
1706 case MSR_MTRRfix4K_E0000:
1707 case MSR_MTRRfix4K_E8000:
1708 case MSR_MTRRfix4K_F0000:
1709 case MSR_MTRRfix4K_F8000:
1710 case MSR_MTRRdefType:
1711 case MSR_IA32_CR_PAT:
1712 return true;
1713 case 0x2f8:
1714 return true;
1715 }
1716 return false;
1717}
1718
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001719static bool valid_pat_type(unsigned t)
1720{
1721 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
1722}
1723
1724static bool valid_mtrr_type(unsigned t)
1725{
1726 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
1727}
1728
1729static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1730{
1731 int i;
Paolo Bonzinifd275232014-08-29 18:56:01 +02001732 u64 mask;
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001733
1734 if (!msr_mtrr_valid(msr))
1735 return false;
1736
1737 if (msr == MSR_IA32_CR_PAT) {
1738 for (i = 0; i < 8; i++)
1739 if (!valid_pat_type((data >> (i * 8)) & 0xff))
1740 return false;
1741 return true;
1742 } else if (msr == MSR_MTRRdefType) {
1743 if (data & ~0xcff)
1744 return false;
1745 return valid_mtrr_type(data & 0xff);
1746 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
1747 for (i = 0; i < 8 ; i++)
1748 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
1749 return false;
1750 return true;
1751 }
1752
1753 /* variable MTRRs */
Wanpeng Liadfb5d22014-08-19 17:04:39 +08001754 WARN_ON(!(msr >= 0x200 && msr < 0x200 + 2 * KVM_NR_VAR_MTRR));
1755
Paolo Bonzinifd275232014-08-29 18:56:01 +02001756 mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
Wanpeng Lid7a2a242014-08-19 17:04:40 +08001757 if ((msr & 1) == 0) {
Wanpeng Liadfb5d22014-08-19 17:04:39 +08001758 /* MTRR base */
Wanpeng Lid7a2a242014-08-19 17:04:40 +08001759 if (!valid_mtrr_type(data & 0xff))
1760 return false;
1761 mask |= 0xf00;
1762 } else
1763 /* MTRR mask */
1764 mask |= 0x7ff;
1765 if (data & mask) {
1766 kvm_inject_gp(vcpu, 0);
1767 return false;
1768 }
1769
Wanpeng Liadfb5d22014-08-19 17:04:39 +08001770 return true;
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001771}
1772
Avi Kivity9ba075a2008-05-26 20:06:35 +03001773static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1774{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001775 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1776
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001777 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +03001778 return 1;
1779
Sheng Yang0bed3b52008-10-09 16:01:54 +08001780 if (msr == MSR_MTRRdefType) {
1781 vcpu->arch.mtrr_state.def_type = data;
1782 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
1783 } else if (msr == MSR_MTRRfix64K_00000)
1784 p[0] = data;
1785 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1786 p[1 + msr - MSR_MTRRfix16K_80000] = data;
1787 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1788 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
1789 else if (msr == MSR_IA32_CR_PAT)
1790 vcpu->arch.pat = data;
1791 else { /* Variable MTRRs */
1792 int idx, is_mtrr_mask;
1793 u64 *pt;
1794
1795 idx = (msr - 0x200) / 2;
1796 is_mtrr_mask = msr - 0x200 - 2 * idx;
1797 if (!is_mtrr_mask)
1798 pt =
1799 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1800 else
1801 pt =
1802 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1803 *pt = data;
1804 }
1805
1806 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +03001807 return 0;
1808}
Carsten Otte15c4a642007-10-30 18:44:17 +01001809
Huang Ying890ca9a2009-05-11 16:48:15 +08001810static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1811{
1812 u64 mcg_cap = vcpu->arch.mcg_cap;
1813 unsigned bank_num = mcg_cap & 0xff;
1814
1815 switch (msr) {
1816 case MSR_IA32_MCG_STATUS:
1817 vcpu->arch.mcg_status = data;
1818 break;
1819 case MSR_IA32_MCG_CTL:
1820 if (!(mcg_cap & MCG_CTL_P))
1821 return 1;
1822 if (data != 0 && data != ~(u64)0)
1823 return -1;
1824 vcpu->arch.mcg_ctl = data;
1825 break;
1826 default:
1827 if (msr >= MSR_IA32_MC0_CTL &&
1828 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1829 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001830 /* only 0 or all 1s can be written to IA32_MCi_CTL
1831 * some Linux kernels though clear bit 10 in bank 4 to
1832 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1833 * this to avoid an uncatched #GP in the guest
1834 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001835 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001836 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001837 return -1;
1838 vcpu->arch.mce_banks[offset] = data;
1839 break;
1840 }
1841 return 1;
1842 }
1843 return 0;
1844}
1845
Ed Swierkffde22a2009-10-15 15:21:43 -07001846static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1847{
1848 struct kvm *kvm = vcpu->kvm;
1849 int lm = is_long_mode(vcpu);
1850 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1851 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1852 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1853 : kvm->arch.xen_hvm_config.blob_size_32;
1854 u32 page_num = data & ~PAGE_MASK;
1855 u64 page_addr = data & PAGE_MASK;
1856 u8 *page;
1857 int r;
1858
1859 r = -E2BIG;
1860 if (page_num >= blob_size)
1861 goto out;
1862 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001863 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
1864 if (IS_ERR(page)) {
1865 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07001866 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001867 }
Ed Swierkffde22a2009-10-15 15:21:43 -07001868 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1869 goto out_free;
1870 r = 0;
1871out_free:
1872 kfree(page);
1873out:
1874 return r;
1875}
1876
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001877static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1878{
1879 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1880}
1881
1882static bool kvm_hv_msr_partition_wide(u32 msr)
1883{
1884 bool r = false;
1885 switch (msr) {
1886 case HV_X64_MSR_GUEST_OS_ID:
1887 case HV_X64_MSR_HYPERCALL:
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001888 case HV_X64_MSR_REFERENCE_TSC:
1889 case HV_X64_MSR_TIME_REF_COUNT:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001890 r = true;
1891 break;
1892 }
1893
1894 return r;
1895}
1896
1897static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1898{
1899 struct kvm *kvm = vcpu->kvm;
1900
1901 switch (msr) {
1902 case HV_X64_MSR_GUEST_OS_ID:
1903 kvm->arch.hv_guest_os_id = data;
1904 /* setting guest os id to zero disables hypercall page */
1905 if (!kvm->arch.hv_guest_os_id)
1906 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1907 break;
1908 case HV_X64_MSR_HYPERCALL: {
1909 u64 gfn;
1910 unsigned long addr;
1911 u8 instructions[4];
1912
1913 /* if guest os id is not set hypercall should remain disabled */
1914 if (!kvm->arch.hv_guest_os_id)
1915 break;
1916 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1917 kvm->arch.hv_hypercall = data;
1918 break;
1919 }
1920 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1921 addr = gfn_to_hva(kvm, gfn);
1922 if (kvm_is_error_hva(addr))
1923 return 1;
1924 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1925 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001926 if (__copy_to_user((void __user *)addr, instructions, 4))
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001927 return 1;
1928 kvm->arch.hv_hypercall = data;
Vadim Rozenfeldb94b64c2014-01-23 18:12:52 +11001929 mark_page_dirty(kvm, gfn);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001930 break;
1931 }
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001932 case HV_X64_MSR_REFERENCE_TSC: {
1933 u64 gfn;
1934 HV_REFERENCE_TSC_PAGE tsc_ref;
1935 memset(&tsc_ref, 0, sizeof(tsc_ref));
1936 kvm->arch.hv_tsc_page = data;
1937 if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1938 break;
1939 gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
Xiaoming Gaoe1fa108d2014-06-19 19:14:57 +08001940 if (kvm_write_guest(kvm, gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001941 &tsc_ref, sizeof(tsc_ref)))
1942 return 1;
1943 mark_page_dirty(kvm, gfn);
1944 break;
1945 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001946 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03001947 vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1948 "data 0x%llx\n", msr, data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001949 return 1;
1950 }
1951 return 0;
1952}
1953
1954static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1955{
Gleb Natapov10388a02010-01-17 15:51:23 +02001956 switch (msr) {
1957 case HV_X64_MSR_APIC_ASSIST_PAGE: {
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001958 u64 gfn;
Gleb Natapov10388a02010-01-17 15:51:23 +02001959 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001960
Gleb Natapov10388a02010-01-17 15:51:23 +02001961 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1962 vcpu->arch.hv_vapic = data;
Michael S. Tsirkinb63cf422014-05-07 16:29:48 +03001963 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1964 return 1;
Gleb Natapov10388a02010-01-17 15:51:23 +02001965 break;
1966 }
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001967 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
1968 addr = gfn_to_hva(vcpu->kvm, gfn);
Gleb Natapov10388a02010-01-17 15:51:23 +02001969 if (kvm_is_error_hva(addr))
1970 return 1;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001971 if (__clear_user((void __user *)addr, PAGE_SIZE))
Gleb Natapov10388a02010-01-17 15:51:23 +02001972 return 1;
1973 vcpu->arch.hv_vapic = data;
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001974 mark_page_dirty(vcpu->kvm, gfn);
Michael S. Tsirkinb63cf422014-05-07 16:29:48 +03001975 if (kvm_lapic_enable_pv_eoi(vcpu, gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1976 return 1;
Gleb Natapov10388a02010-01-17 15:51:23 +02001977 break;
1978 }
1979 case HV_X64_MSR_EOI:
1980 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1981 case HV_X64_MSR_ICR:
1982 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1983 case HV_X64_MSR_TPR:
1984 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1985 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03001986 vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1987 "data 0x%llx\n", msr, data);
Gleb Natapov10388a02010-01-17 15:51:23 +02001988 return 1;
1989 }
1990
1991 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001992}
1993
Gleb Natapov344d9582010-10-14 11:22:50 +02001994static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
1995{
1996 gpa_t gpa = data & ~0x3f;
1997
Guo Chao4a969982012-06-28 15:17:27 +08001998 /* Bits 2:5 are reserved, Should be zero */
Gleb Natapov6adba522010-10-14 11:22:55 +02001999 if (data & 0x3c)
Gleb Natapov344d9582010-10-14 11:22:50 +02002000 return 1;
2001
2002 vcpu->arch.apf.msr_val = data;
2003
2004 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2005 kvm_clear_async_pf_completion_queue(vcpu);
2006 kvm_async_pf_hash_reset(vcpu);
2007 return 0;
2008 }
2009
Andrew Honig8f964522013-03-29 09:35:21 -07002010 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
2011 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002012 return 1;
2013
Gleb Natapov6adba522010-10-14 11:22:55 +02002014 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Gleb Natapov344d9582010-10-14 11:22:50 +02002015 kvm_async_pf_wakeup_all(vcpu);
2016 return 0;
2017}
2018
Glauber Costa12f9a482011-02-01 14:16:40 -05002019static void kvmclock_reset(struct kvm_vcpu *vcpu)
2020{
Andy Honig0b794592013-02-20 14:48:10 -08002021 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05002022}
2023
Glauber Costac9aaa892011-07-11 15:28:14 -04002024static void accumulate_steal_time(struct kvm_vcpu *vcpu)
2025{
2026 u64 delta;
2027
2028 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2029 return;
2030
2031 delta = current->sched_info.run_delay - vcpu->arch.st.last_steal;
2032 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2033 vcpu->arch.st.accum_steal = delta;
2034}
2035
2036static void record_steal_time(struct kvm_vcpu *vcpu)
2037{
2038 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2039 return;
2040
2041 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2042 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2043 return;
2044
2045 vcpu->arch.st.steal.steal += vcpu->arch.st.accum_steal;
2046 vcpu->arch.st.steal.version += 2;
2047 vcpu->arch.st.accum_steal = 0;
2048
2049 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2050 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2051}
2052
Will Auld8fe8ab42012-11-29 12:42:12 -08002053int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002054{
Gleb Natapov57537852012-01-15 14:17:22 +02002055 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002056 u32 msr = msr_info->index;
2057 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002058
Carsten Otte15c4a642007-10-30 18:44:17 +01002059 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002060 case MSR_AMD64_NB_CFG:
2061 case MSR_IA32_UCODE_REV:
2062 case MSR_IA32_UCODE_WRITE:
2063 case MSR_VM_HSAVE_PA:
2064 case MSR_AMD64_PATCH_LOADER:
2065 case MSR_AMD64_BU_CFG2:
2066 break;
2067
Carsten Otte15c4a642007-10-30 18:44:17 +01002068 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02002069 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002070 case MSR_K7_HWCR:
2071 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002072 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002073 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02002074 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02002075 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002076 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2077 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002078 return 1;
2079 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002080 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002081 case MSR_FAM10H_MMIO_CONF_BASE:
2082 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002083 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2084 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002085 return 1;
2086 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002087 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002088 case MSR_IA32_DEBUGCTLMSR:
2089 if (!data) {
2090 /* We support the non-activated case already */
2091 break;
2092 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2093 /* Values other than LBR and BTF are vendor-specific,
2094 thus reserved and should throw a #GP */
2095 return 1;
2096 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002097 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2098 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002099 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002100 case 0x200 ... 0x2ff:
2101 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002102 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002103 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002104 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2105 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002106 case MSR_IA32_TSCDEADLINE:
2107 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2108 break;
Will Auldba904632012-11-29 12:42:50 -08002109 case MSR_IA32_TSC_ADJUST:
2110 if (guest_cpuid_has_tsc_adjust(vcpu)) {
2111 if (!msr_info->host_initiated) {
2112 u64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
2113 kvm_x86_ops->adjust_tsc_offset(vcpu, adj, true);
2114 }
2115 vcpu->arch.ia32_tsc_adjust_msr = data;
2116 }
2117 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002118 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002119 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01002120 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002121 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002122 case MSR_KVM_WALL_CLOCK:
2123 vcpu->kvm->arch.wall_clock = data;
2124 kvm_write_wall_clock(vcpu->kvm, data);
2125 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002126 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002127 case MSR_KVM_SYSTEM_TIME: {
Andy Honig0b794592013-02-20 14:48:10 -08002128 u64 gpa_offset;
Glauber Costa12f9a482011-02-01 14:16:40 -05002129 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002130
2131 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002132 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002133
2134 /* we verify if the enable bit is set... */
2135 if (!(data & 1))
2136 break;
2137
Andy Honig0b794592013-02-20 14:48:10 -08002138 gpa_offset = data & ~(PAGE_MASK | 1);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002139
Andy Honig0b794592013-02-20 14:48:10 -08002140 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002141 &vcpu->arch.pv_time, data & ~1ULL,
2142 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002143 vcpu->arch.pv_time_enabled = false;
2144 else
2145 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002146
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002147 break;
2148 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002149 case MSR_KVM_ASYNC_PF_EN:
2150 if (kvm_pv_enable_async_pf(vcpu, data))
2151 return 1;
2152 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002153 case MSR_KVM_STEAL_TIME:
2154
2155 if (unlikely(!sched_info_on()))
2156 return 1;
2157
2158 if (data & KVM_STEAL_RESERVED_MASK)
2159 return 1;
2160
2161 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002162 data & KVM_STEAL_VALID_BITS,
2163 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002164 return 1;
2165
2166 vcpu->arch.st.msr_val = data;
2167
2168 if (!(data & KVM_MSR_ENABLED))
2169 break;
2170
2171 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2172
2173 preempt_disable();
2174 accumulate_steal_time(vcpu);
2175 preempt_enable();
2176
2177 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2178
2179 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002180 case MSR_KVM_PV_EOI_EN:
2181 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2182 return 1;
2183 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002184
Huang Ying890ca9a2009-05-11 16:48:15 +08002185 case MSR_IA32_MCG_CTL:
2186 case MSR_IA32_MCG_STATUS:
2187 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
2188 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002189
2190 /* Performance counters are not protected by a CPUID bit,
2191 * so we should check all of them in the generic path for the sake of
2192 * cross vendor migration.
2193 * Writing a zero into the event select MSRs disables them,
2194 * which we perfectly emulate ;-). Any other value should be at least
2195 * reported, some guests depend on them.
2196 */
Andre Przywara71db6022009-06-12 22:01:29 +02002197 case MSR_K7_EVNTSEL0:
2198 case MSR_K7_EVNTSEL1:
2199 case MSR_K7_EVNTSEL2:
2200 case MSR_K7_EVNTSEL3:
2201 if (data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002202 vcpu_unimpl(vcpu, "unimplemented perfctr wrmsr: "
2203 "0x%x data 0x%llx\n", msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002204 break;
2205 /* at least RHEL 4 unconditionally writes to the perfctr registers,
2206 * so we ignore writes to make it happy.
2207 */
Andre Przywara71db6022009-06-12 22:01:29 +02002208 case MSR_K7_PERFCTR0:
2209 case MSR_K7_PERFCTR1:
2210 case MSR_K7_PERFCTR2:
2211 case MSR_K7_PERFCTR3:
Christoffer Dalla737f252012-06-03 21:17:48 +03002212 vcpu_unimpl(vcpu, "unimplemented perfctr wrmsr: "
2213 "0x%x data 0x%llx\n", msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002214 break;
Gleb Natapov57537852012-01-15 14:17:22 +02002215 case MSR_P6_PERFCTR0:
2216 case MSR_P6_PERFCTR1:
2217 pr = true;
2218 case MSR_P6_EVNTSEL0:
2219 case MSR_P6_EVNTSEL1:
2220 if (kvm_pmu_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002221 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002222
2223 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002224 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2225 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002226 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002227 case MSR_K7_CLK_CTL:
2228 /*
2229 * Ignore all writes to this no longer documented MSR.
2230 * Writes are only relevant for old K7 processors,
2231 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002232 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002233 * affected processor models on the command line, hence
2234 * the need to ignore the workaround.
2235 */
2236 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002237 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
2238 if (kvm_hv_msr_partition_wide(msr)) {
2239 int r;
2240 mutex_lock(&vcpu->kvm->lock);
2241 r = set_msr_hyperv_pw(vcpu, msr, data);
2242 mutex_unlock(&vcpu->kvm->lock);
2243 return r;
2244 } else
2245 return set_msr_hyperv(vcpu, msr, data);
2246 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002247 case MSR_IA32_BBL_CR_CTL3:
2248 /* Drop writes to this legacy MSR -- see rdmsr
2249 * counterpart for further detail.
2250 */
Christoffer Dalla737f252012-06-03 21:17:48 +03002251 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002252 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002253 case MSR_AMD64_OSVW_ID_LENGTH:
2254 if (!guest_cpuid_has_osvw(vcpu))
2255 return 1;
2256 vcpu->arch.osvw.length = data;
2257 break;
2258 case MSR_AMD64_OSVW_STATUS:
2259 if (!guest_cpuid_has_osvw(vcpu))
2260 return 1;
2261 vcpu->arch.osvw.status = data;
2262 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002263 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002264 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2265 return xen_hvm_config(vcpu, data);
Gleb Natapovf5132b02011-11-10 14:57:22 +02002266 if (kvm_pmu_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002267 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002268 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002269 vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
2270 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002271 return 1;
2272 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002273 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
2274 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002275 break;
2276 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002277 }
2278 return 0;
2279}
2280EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2281
2282
2283/*
2284 * Reads an msr value (of 'msr_index') into 'pdata'.
2285 * Returns 0 on success, non-0 otherwise.
2286 * Assumes vcpu_load() was already called.
2287 */
2288int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2289{
2290 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
2291}
2292
Avi Kivity9ba075a2008-05-26 20:06:35 +03002293static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2294{
Sheng Yang0bed3b52008-10-09 16:01:54 +08002295 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
2296
Avi Kivity9ba075a2008-05-26 20:06:35 +03002297 if (!msr_mtrr_valid(msr))
2298 return 1;
2299
Sheng Yang0bed3b52008-10-09 16:01:54 +08002300 if (msr == MSR_MTRRdefType)
2301 *pdata = vcpu->arch.mtrr_state.def_type +
2302 (vcpu->arch.mtrr_state.enabled << 10);
2303 else if (msr == MSR_MTRRfix64K_00000)
2304 *pdata = p[0];
2305 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
2306 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
2307 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
2308 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
2309 else if (msr == MSR_IA32_CR_PAT)
2310 *pdata = vcpu->arch.pat;
2311 else { /* Variable MTRRs */
2312 int idx, is_mtrr_mask;
2313 u64 *pt;
2314
2315 idx = (msr - 0x200) / 2;
2316 is_mtrr_mask = msr - 0x200 - 2 * idx;
2317 if (!is_mtrr_mask)
2318 pt =
2319 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
2320 else
2321 pt =
2322 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
2323 *pdata = *pt;
2324 }
2325
Avi Kivity9ba075a2008-05-26 20:06:35 +03002326 return 0;
2327}
2328
Huang Ying890ca9a2009-05-11 16:48:15 +08002329static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2330{
2331 u64 data;
2332 u64 mcg_cap = vcpu->arch.mcg_cap;
2333 unsigned bank_num = mcg_cap & 0xff;
2334
2335 switch (msr) {
2336 case MSR_IA32_P5_MC_ADDR:
2337 case MSR_IA32_P5_MC_TYPE:
2338 data = 0;
2339 break;
2340 case MSR_IA32_MCG_CAP:
2341 data = vcpu->arch.mcg_cap;
2342 break;
2343 case MSR_IA32_MCG_CTL:
2344 if (!(mcg_cap & MCG_CTL_P))
2345 return 1;
2346 data = vcpu->arch.mcg_ctl;
2347 break;
2348 case MSR_IA32_MCG_STATUS:
2349 data = vcpu->arch.mcg_status;
2350 break;
2351 default:
2352 if (msr >= MSR_IA32_MC0_CTL &&
2353 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
2354 u32 offset = msr - MSR_IA32_MC0_CTL;
2355 data = vcpu->arch.mce_banks[offset];
2356 break;
2357 }
2358 return 1;
2359 }
2360 *pdata = data;
2361 return 0;
2362}
2363
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002364static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2365{
2366 u64 data = 0;
2367 struct kvm *kvm = vcpu->kvm;
2368
2369 switch (msr) {
2370 case HV_X64_MSR_GUEST_OS_ID:
2371 data = kvm->arch.hv_guest_os_id;
2372 break;
2373 case HV_X64_MSR_HYPERCALL:
2374 data = kvm->arch.hv_hypercall;
2375 break;
Vadim Rozenfelde9840972014-01-16 20:18:37 +11002376 case HV_X64_MSR_TIME_REF_COUNT: {
2377 data =
2378 div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
2379 break;
2380 }
2381 case HV_X64_MSR_REFERENCE_TSC:
2382 data = kvm->arch.hv_tsc_page;
2383 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002384 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03002385 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002386 return 1;
2387 }
2388
2389 *pdata = data;
2390 return 0;
2391}
2392
2393static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2394{
2395 u64 data = 0;
2396
2397 switch (msr) {
2398 case HV_X64_MSR_VP_INDEX: {
2399 int r;
2400 struct kvm_vcpu *v;
Takuya Yoshikawa684851a2014-02-27 15:08:31 +09002401 kvm_for_each_vcpu(r, v, vcpu->kvm) {
2402 if (v == vcpu) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002403 data = r;
Takuya Yoshikawa684851a2014-02-27 15:08:31 +09002404 break;
2405 }
2406 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002407 break;
2408 }
Gleb Natapov10388a02010-01-17 15:51:23 +02002409 case HV_X64_MSR_EOI:
2410 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
2411 case HV_X64_MSR_ICR:
2412 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
2413 case HV_X64_MSR_TPR:
2414 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Mike Waychison14fa67e2011-07-21 15:38:10 -07002415 case HV_X64_MSR_APIC_ASSIST_PAGE:
Mike Waychisond1613ad2011-07-23 00:31:45 -07002416 data = vcpu->arch.hv_vapic;
2417 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002418 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03002419 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002420 return 1;
2421 }
2422 *pdata = data;
2423 return 0;
2424}
2425
Carsten Otte15c4a642007-10-30 18:44:17 +01002426int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2427{
2428 u64 data;
2429
2430 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002431 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002432 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002433 case MSR_IA32_DEBUGCTLMSR:
2434 case MSR_IA32_LASTBRANCHFROMIP:
2435 case MSR_IA32_LASTBRANCHTOIP:
2436 case MSR_IA32_LASTINTFROMIP:
2437 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302438 case MSR_K8_SYSCFG:
2439 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002440 case MSR_VM_HSAVE_PA:
Amit Shah9e699622009-06-15 13:25:34 +05302441 case MSR_K7_EVNTSEL0:
Wei Huangdc9b2d92014-08-13 12:06:14 -04002442 case MSR_K7_EVNTSEL1:
2443 case MSR_K7_EVNTSEL2:
2444 case MSR_K7_EVNTSEL3:
Amit Shah1f3ee612009-06-30 16:24:28 +05302445 case MSR_K7_PERFCTR0:
Wei Huangdc9b2d92014-08-13 12:06:14 -04002446 case MSR_K7_PERFCTR1:
2447 case MSR_K7_PERFCTR2:
2448 case MSR_K7_PERFCTR3:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002449 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002450 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002451 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002452 case MSR_AMD64_BU_CFG2:
Carsten Otte15c4a642007-10-30 18:44:17 +01002453 data = 0;
2454 break;
Gleb Natapov57537852012-01-15 14:17:22 +02002455 case MSR_P6_PERFCTR0:
2456 case MSR_P6_PERFCTR1:
2457 case MSR_P6_EVNTSEL0:
2458 case MSR_P6_EVNTSEL1:
2459 if (kvm_pmu_msr(vcpu, msr))
2460 return kvm_pmu_get_msr(vcpu, msr, pdata);
2461 data = 0;
2462 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002463 case MSR_IA32_UCODE_REV:
2464 data = 0x100000000ULL;
2465 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002466 case MSR_MTRRcap:
2467 data = 0x500 | KVM_NR_VAR_MTRR;
2468 break;
2469 case 0x200 ... 0x2ff:
2470 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01002471 case 0xcd: /* fsb frequency */
2472 data = 3;
2473 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002474 /*
2475 * MSR_EBC_FREQUENCY_ID
2476 * Conservative value valid for even the basic CPU models.
2477 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2478 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2479 * and 266MHz for model 3, or 4. Set Core Clock
2480 * Frequency to System Bus Frequency Ratio to 1 (bits
2481 * 31:24) even though these are only valid for CPU
2482 * models > 2, however guests may end up dividing or
2483 * multiplying by zero otherwise.
2484 */
2485 case MSR_EBC_FREQUENCY_ID:
2486 data = 1 << 24;
2487 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002488 case MSR_IA32_APICBASE:
2489 data = kvm_get_apic_base(vcpu);
2490 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002491 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2492 return kvm_x2apic_msr_read(vcpu, msr, pdata);
2493 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002494 case MSR_IA32_TSCDEADLINE:
2495 data = kvm_get_lapic_tscdeadline_msr(vcpu);
2496 break;
Will Auldba904632012-11-29 12:42:50 -08002497 case MSR_IA32_TSC_ADJUST:
2498 data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
2499 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002500 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002501 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002502 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002503 case MSR_IA32_PERF_STATUS:
2504 /* TSC increment by tick */
2505 data = 1000ULL;
2506 /* CPU multiplier */
2507 data |= (((uint64_t)4ULL) << 40);
2508 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002509 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02002510 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002511 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002512 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002513 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002514 data = vcpu->kvm->arch.wall_clock;
2515 break;
2516 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002517 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002518 data = vcpu->arch.time;
2519 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002520 case MSR_KVM_ASYNC_PF_EN:
2521 data = vcpu->arch.apf.msr_val;
2522 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002523 case MSR_KVM_STEAL_TIME:
2524 data = vcpu->arch.st.msr_val;
2525 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002526 case MSR_KVM_PV_EOI_EN:
2527 data = vcpu->arch.pv_eoi.msr_val;
2528 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002529 case MSR_IA32_P5_MC_ADDR:
2530 case MSR_IA32_P5_MC_TYPE:
2531 case MSR_IA32_MCG_CAP:
2532 case MSR_IA32_MCG_CTL:
2533 case MSR_IA32_MCG_STATUS:
2534 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
2535 return get_msr_mce(vcpu, msr, pdata);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002536 case MSR_K7_CLK_CTL:
2537 /*
2538 * Provide expected ramp-up count for K7. All other
2539 * are set to zero, indicating minimum divisors for
2540 * every field.
2541 *
2542 * This prevents guest kernels on AMD host with CPU
2543 * type 6, model 8 and higher from exploding due to
2544 * the rdmsr failing.
2545 */
2546 data = 0x20000000;
2547 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002548 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
2549 if (kvm_hv_msr_partition_wide(msr)) {
2550 int r;
2551 mutex_lock(&vcpu->kvm->lock);
2552 r = get_msr_hyperv_pw(vcpu, msr, pdata);
2553 mutex_unlock(&vcpu->kvm->lock);
2554 return r;
2555 } else
2556 return get_msr_hyperv(vcpu, msr, pdata);
2557 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002558 case MSR_IA32_BBL_CR_CTL3:
2559 /* This legacy MSR exists but isn't fully documented in current
2560 * silicon. It is however accessed by winxp in very narrow
2561 * scenarios where it sets bit #19, itself documented as
2562 * a "reserved" bit. Best effort attempt to source coherent
2563 * read data here should the balance of the register be
2564 * interpreted by the guest:
2565 *
2566 * L2 cache control register 3: 64GB range, 256KB size,
2567 * enabled, latency 0x1, configured
2568 */
2569 data = 0xbe702111;
2570 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002571 case MSR_AMD64_OSVW_ID_LENGTH:
2572 if (!guest_cpuid_has_osvw(vcpu))
2573 return 1;
2574 data = vcpu->arch.osvw.length;
2575 break;
2576 case MSR_AMD64_OSVW_STATUS:
2577 if (!guest_cpuid_has_osvw(vcpu))
2578 return 1;
2579 data = vcpu->arch.osvw.status;
2580 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002581 default:
Gleb Natapovf5132b02011-11-10 14:57:22 +02002582 if (kvm_pmu_msr(vcpu, msr))
2583 return kvm_pmu_get_msr(vcpu, msr, pdata);
Andre Przywaraed85c062009-06-25 12:36:49 +02002584 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002585 vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Andre Przywaraed85c062009-06-25 12:36:49 +02002586 return 1;
2587 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002588 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
Andre Przywaraed85c062009-06-25 12:36:49 +02002589 data = 0;
2590 }
2591 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002592 }
2593 *pdata = data;
2594 return 0;
2595}
2596EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2597
Carsten Otte313a3dc2007-10-11 19:16:52 +02002598/*
2599 * Read or write a bunch of msrs. All parameters are kernel addresses.
2600 *
2601 * @return number of msrs set successfully.
2602 */
2603static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2604 struct kvm_msr_entry *entries,
2605 int (*do_msr)(struct kvm_vcpu *vcpu,
2606 unsigned index, u64 *data))
2607{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002608 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002609
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002610 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002611 for (i = 0; i < msrs->nmsrs; ++i)
2612 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2613 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002614 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002615
Carsten Otte313a3dc2007-10-11 19:16:52 +02002616 return i;
2617}
2618
2619/*
2620 * Read or write a bunch of msrs. Parameters are user addresses.
2621 *
2622 * @return number of msrs set successfully.
2623 */
2624static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2625 int (*do_msr)(struct kvm_vcpu *vcpu,
2626 unsigned index, u64 *data),
2627 int writeback)
2628{
2629 struct kvm_msrs msrs;
2630 struct kvm_msr_entry *entries;
2631 int r, n;
2632 unsigned size;
2633
2634 r = -EFAULT;
2635 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2636 goto out;
2637
2638 r = -E2BIG;
2639 if (msrs.nmsrs >= MAX_IO_MSRS)
2640 goto out;
2641
Carsten Otte313a3dc2007-10-11 19:16:52 +02002642 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002643 entries = memdup_user(user_msrs->entries, size);
2644 if (IS_ERR(entries)) {
2645 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002646 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002647 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002648
2649 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2650 if (r < 0)
2651 goto out_free;
2652
2653 r = -EFAULT;
2654 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2655 goto out_free;
2656
2657 r = n;
2658
2659out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002660 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002661out:
2662 return r;
2663}
2664
Alexander Graf784aa3d2014-07-14 18:27:35 +02002665int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002666{
2667 int r;
2668
2669 switch (ext) {
2670 case KVM_CAP_IRQCHIP:
2671 case KVM_CAP_HLT:
2672 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002673 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002674 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002675 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002676 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002677 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002678 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002679 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002680 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002681 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002682 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002683 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskins721eecb2009-05-20 10:30:49 -04002684 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002685 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002686 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002687 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002688 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002689 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002690 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002691 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002692 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002693 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002694 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002695 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002696 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002697 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002698 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002699 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002700 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002701 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002702 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002703 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002704 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002705 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Alex Williamson2a5bab12013-04-16 13:49:18 -06002706#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2707 case KVM_CAP_ASSIGN_DEV_IRQ:
2708 case KVM_CAP_PCI_2_3:
2709#endif
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002710 r = 1;
2711 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02002712 case KVM_CAP_COALESCED_MMIO:
2713 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
2714 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002715 case KVM_CAP_VAPIC:
2716 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2717 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002718 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002719 r = KVM_SOFT_MAX_VCPUS;
2720 break;
2721 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002722 r = KVM_MAX_VCPUS;
2723 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002724 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002725 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002726 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002727 case KVM_CAP_PV_MMU: /* obsolete */
2728 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002729 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002730#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002731 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +02002732 r = iommu_present(&pci_bus_type);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002733 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002734#endif
Huang Ying890ca9a2009-05-11 16:48:15 +08002735 case KVM_CAP_MCE:
2736 r = KVM_MAX_MCE_BANKS;
2737 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002738 case KVM_CAP_XCRS:
2739 r = cpu_has_xsave;
2740 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002741 case KVM_CAP_TSC_CONTROL:
2742 r = kvm_has_tsc_control;
2743 break;
Jan Kiszka4d25a0662011-12-21 12:28:29 +01002744 case KVM_CAP_TSC_DEADLINE_TIMER:
2745 r = boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER);
2746 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002747 default:
2748 r = 0;
2749 break;
2750 }
2751 return r;
2752
2753}
2754
Carsten Otte043405e2007-10-10 17:16:19 +02002755long kvm_arch_dev_ioctl(struct file *filp,
2756 unsigned int ioctl, unsigned long arg)
2757{
2758 void __user *argp = (void __user *)arg;
2759 long r;
2760
2761 switch (ioctl) {
2762 case KVM_GET_MSR_INDEX_LIST: {
2763 struct kvm_msr_list __user *user_msr_list = argp;
2764 struct kvm_msr_list msr_list;
2765 unsigned n;
2766
2767 r = -EFAULT;
2768 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2769 goto out;
2770 n = msr_list.nmsrs;
2771 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2772 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2773 goto out;
2774 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002775 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02002776 goto out;
2777 r = -EFAULT;
2778 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2779 num_msrs_to_save * sizeof(u32)))
2780 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002781 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02002782 &emulated_msrs,
2783 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2784 goto out;
2785 r = 0;
2786 break;
2787 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002788 case KVM_GET_SUPPORTED_CPUID:
2789 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02002790 struct kvm_cpuid2 __user *cpuid_arg = argp;
2791 struct kvm_cpuid2 cpuid;
2792
2793 r = -EFAULT;
2794 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2795 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002796
2797 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2798 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02002799 if (r)
2800 goto out;
2801
2802 r = -EFAULT;
2803 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2804 goto out;
2805 r = 0;
2806 break;
2807 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002808 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
2809 u64 mce_cap;
2810
2811 mce_cap = KVM_MCE_CAP_SUPPORTED;
2812 r = -EFAULT;
2813 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
2814 goto out;
2815 r = 0;
2816 break;
2817 }
Carsten Otte043405e2007-10-10 17:16:19 +02002818 default:
2819 r = -EINVAL;
2820 }
2821out:
2822 return r;
2823}
2824
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002825static void wbinvd_ipi(void *garbage)
2826{
2827 wbinvd();
2828}
2829
2830static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
2831{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06002832 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002833}
2834
Carsten Otte313a3dc2007-10-11 19:16:52 +02002835void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2836{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002837 /* Address WBINVD may be executed by guest */
2838 if (need_emulate_wbinvd(vcpu)) {
2839 if (kvm_x86_ops->has_wbinvd_exit())
2840 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
2841 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
2842 smp_call_function_single(vcpu->cpu,
2843 wbinvd_ipi, NULL, 1);
2844 }
2845
Carsten Otte313a3dc2007-10-11 19:16:52 +02002846 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002847
2848 /* Apply any externally detected TSC adjustments (due to suspend) */
2849 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
2850 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
2851 vcpu->arch.tsc_offset_adjustment = 0;
2852 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
2853 }
2854
Zachary Amsden48434c202010-08-19 22:07:24 -10002855 if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002856 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
2857 native_read_tsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10002858 if (tsc_delta < 0)
2859 mark_tsc_unstable("KVM discovered backwards TSC");
Zachary Amsdenc2855452010-09-18 14:38:15 -10002860 if (check_tsc_unstable()) {
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002861 u64 offset = kvm_x86_ops->compute_tsc_offset(vcpu,
2862 vcpu->arch.last_guest_tsc);
2863 kvm_x86_ops->write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002864 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002865 }
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02002866 /*
2867 * On a host with synchronized TSC, there is no need to update
2868 * kvmclock on vcpu->cpu migration
2869 */
2870 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002871 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002872 if (vcpu->cpu != cpu)
2873 kvm_migrate_timers(vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002874 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10002875 }
Glauber Costac9aaa892011-07-11 15:28:14 -04002876
2877 accumulate_steal_time(vcpu);
2878 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002879}
2880
2881void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2882{
Avi Kivity02daab22009-12-30 12:40:26 +02002883 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03002884 kvm_put_guest_fpu(vcpu);
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002885 vcpu->arch.last_host_tsc = native_read_tsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02002886}
2887
Carsten Otte313a3dc2007-10-11 19:16:52 +02002888static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2889 struct kvm_lapic_state *s)
2890{
Yang Zhang5a717852013-04-11 19:25:16 +08002891 kvm_x86_ops->sync_pir_to_irr(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002892 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002893
2894 return 0;
2895}
2896
2897static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2898 struct kvm_lapic_state *s)
2899{
Gleb Natapov64eb0622012-08-08 15:24:36 +03002900 kvm_apic_post_state_restore(vcpu, s);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002901 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002902
2903 return 0;
2904}
2905
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002906static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2907 struct kvm_interrupt *irq)
2908{
Chen Gang02cdb502013-02-27 11:33:25 +08002909 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002910 return -EINVAL;
2911 if (irqchip_in_kernel(vcpu->kvm))
2912 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002913
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002914 kvm_queue_interrupt(vcpu, irq->irq, false);
Avi Kivity3842d132010-07-27 12:30:24 +03002915 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002916
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002917 return 0;
2918}
2919
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002920static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2921{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002922 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002923
2924 return 0;
2925}
2926
Avi Kivityb209749f2007-10-22 16:50:39 +02002927static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2928 struct kvm_tpr_access_ctl *tac)
2929{
2930 if (tac->flags)
2931 return -EINVAL;
2932 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2933 return 0;
2934}
2935
Huang Ying890ca9a2009-05-11 16:48:15 +08002936static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2937 u64 mcg_cap)
2938{
2939 int r;
2940 unsigned bank_num = mcg_cap & 0xff, bank;
2941
2942 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002943 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002944 goto out;
2945 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2946 goto out;
2947 r = 0;
2948 vcpu->arch.mcg_cap = mcg_cap;
2949 /* Init IA32_MCG_CTL to all 1s */
2950 if (mcg_cap & MCG_CTL_P)
2951 vcpu->arch.mcg_ctl = ~(u64)0;
2952 /* Init IA32_MCi_CTL to all 1s */
2953 for (bank = 0; bank < bank_num; bank++)
2954 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2955out:
2956 return r;
2957}
2958
2959static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2960 struct kvm_x86_mce *mce)
2961{
2962 u64 mcg_cap = vcpu->arch.mcg_cap;
2963 unsigned bank_num = mcg_cap & 0xff;
2964 u64 *banks = vcpu->arch.mce_banks;
2965
2966 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2967 return -EINVAL;
2968 /*
2969 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2970 * reporting is disabled
2971 */
2972 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2973 vcpu->arch.mcg_ctl != ~(u64)0)
2974 return 0;
2975 banks += 4 * mce->bank;
2976 /*
2977 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2978 * reporting is disabled for the bank
2979 */
2980 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2981 return 0;
2982 if (mce->status & MCI_STATUS_UC) {
2983 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002984 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002985 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002986 return 0;
2987 }
2988 if (banks[1] & MCI_STATUS_VAL)
2989 mce->status |= MCI_STATUS_OVER;
2990 banks[2] = mce->addr;
2991 banks[3] = mce->misc;
2992 vcpu->arch.mcg_status = mce->mcg_status;
2993 banks[1] = mce->status;
2994 kvm_queue_exception(vcpu, MC_VECTOR);
2995 } else if (!(banks[1] & MCI_STATUS_VAL)
2996 || !(banks[1] & MCI_STATUS_UC)) {
2997 if (banks[1] & MCI_STATUS_VAL)
2998 mce->status |= MCI_STATUS_OVER;
2999 banks[2] = mce->addr;
3000 banks[3] = mce->misc;
3001 banks[1] = mce->status;
3002 } else
3003 banks[1] |= MCI_STATUS_OVER;
3004 return 0;
3005}
3006
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003007static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3008 struct kvm_vcpu_events *events)
3009{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003010 process_nmi(vcpu);
Jan Kiszka03b82a32010-02-15 10:45:41 +01003011 events->exception.injected =
3012 vcpu->arch.exception.pending &&
3013 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003014 events->exception.nr = vcpu->arch.exception.nr;
3015 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003016 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003017 events->exception.error_code = vcpu->arch.exception.error_code;
3018
Jan Kiszka03b82a32010-02-15 10:45:41 +01003019 events->interrupt.injected =
3020 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003021 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003022 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003023 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003024
3025 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003026 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003027 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003028 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003029
Jan Kiszka66450a22013-03-13 12:42:34 +01003030 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003031
Jan Kiszkadab4b912009-12-06 18:24:15 +01003032 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003033 | KVM_VCPUEVENT_VALID_SHADOW);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003034 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003035}
3036
3037static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3038 struct kvm_vcpu_events *events)
3039{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003040 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003041 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
3042 | KVM_VCPUEVENT_VALID_SHADOW))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003043 return -EINVAL;
3044
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003045 process_nmi(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003046 vcpu->arch.exception.pending = events->exception.injected;
3047 vcpu->arch.exception.nr = events->exception.nr;
3048 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3049 vcpu->arch.exception.error_code = events->exception.error_code;
3050
3051 vcpu->arch.interrupt.pending = events->interrupt.injected;
3052 vcpu->arch.interrupt.nr = events->interrupt.nr;
3053 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003054 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3055 kvm_x86_ops->set_interrupt_shadow(vcpu,
3056 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003057
3058 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003059 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3060 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003061 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3062
Jan Kiszka66450a22013-03-13 12:42:34 +01003063 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
3064 kvm_vcpu_has_lapic(vcpu))
3065 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003066
Avi Kivity3842d132010-07-27 12:30:24 +03003067 kvm_make_request(KVM_REQ_EVENT, vcpu);
3068
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003069 return 0;
3070}
3071
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003072static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3073 struct kvm_debugregs *dbgregs)
3074{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003075 unsigned long val;
3076
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003077 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003078 _kvm_get_dr(vcpu, 6, &val);
3079 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003080 dbgregs->dr7 = vcpu->arch.dr7;
3081 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003082 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003083}
3084
3085static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3086 struct kvm_debugregs *dbgregs)
3087{
3088 if (dbgregs->flags)
3089 return -EINVAL;
3090
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003091 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
3092 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003093 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003094 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003095 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003096
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003097 return 0;
3098}
3099
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003100static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3101 struct kvm_xsave *guest_xsave)
3102{
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003103 if (cpu_has_xsave) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003104 memcpy(guest_xsave->region,
3105 &vcpu->arch.guest_fpu.state->xsave,
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003106 vcpu->arch.guest_xstate_size);
3107 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] &=
3108 vcpu->arch.guest_supported_xcr0 | XSTATE_FPSSE;
3109 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003110 memcpy(guest_xsave->region,
3111 &vcpu->arch.guest_fpu.state->fxsave,
3112 sizeof(struct i387_fxsave_struct));
3113 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
3114 XSTATE_FPSSE;
3115 }
3116}
3117
3118static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3119 struct kvm_xsave *guest_xsave)
3120{
3121 u64 xstate_bv =
3122 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
3123
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003124 if (cpu_has_xsave) {
3125 /*
3126 * Here we allow setting states that are not present in
3127 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3128 * with old userspace.
3129 */
Paolo Bonzini4ff41732014-02-24 12:15:16 +01003130 if (xstate_bv & ~kvm_supported_xcr0())
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003131 return -EINVAL;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003132 memcpy(&vcpu->arch.guest_fpu.state->xsave,
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003133 guest_xsave->region, vcpu->arch.guest_xstate_size);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003134 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003135 if (xstate_bv & ~XSTATE_FPSSE)
3136 return -EINVAL;
3137 memcpy(&vcpu->arch.guest_fpu.state->fxsave,
3138 guest_xsave->region, sizeof(struct i387_fxsave_struct));
3139 }
3140 return 0;
3141}
3142
3143static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3144 struct kvm_xcrs *guest_xcrs)
3145{
3146 if (!cpu_has_xsave) {
3147 guest_xcrs->nr_xcrs = 0;
3148 return;
3149 }
3150
3151 guest_xcrs->nr_xcrs = 1;
3152 guest_xcrs->flags = 0;
3153 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3154 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3155}
3156
3157static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3158 struct kvm_xcrs *guest_xcrs)
3159{
3160 int i, r = 0;
3161
3162 if (!cpu_has_xsave)
3163 return -EINVAL;
3164
3165 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3166 return -EINVAL;
3167
3168 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3169 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003170 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003171 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003172 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003173 break;
3174 }
3175 if (r)
3176 r = -EINVAL;
3177 return r;
3178}
3179
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003180/*
3181 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3182 * stopped by the hypervisor. This function will be called from the host only.
3183 * EINVAL is returned when the host attempts to set the flag for a guest that
3184 * does not support pv clocks.
3185 */
3186static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3187{
Andy Honig0b794592013-02-20 14:48:10 -08003188 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003189 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003190 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003191 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3192 return 0;
3193}
3194
Carsten Otte313a3dc2007-10-11 19:16:52 +02003195long kvm_arch_vcpu_ioctl(struct file *filp,
3196 unsigned int ioctl, unsigned long arg)
3197{
3198 struct kvm_vcpu *vcpu = filp->private_data;
3199 void __user *argp = (void __user *)arg;
3200 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003201 union {
3202 struct kvm_lapic_state *lapic;
3203 struct kvm_xsave *xsave;
3204 struct kvm_xcrs *xcrs;
3205 void *buffer;
3206 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003207
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003208 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003209 switch (ioctl) {
3210 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003211 r = -EINVAL;
3212 if (!vcpu->arch.apic)
3213 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003214 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003215
Dave Hansenb772ff32008-08-11 10:01:47 -07003216 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003217 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003218 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003219 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003220 if (r)
3221 goto out;
3222 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003223 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003224 goto out;
3225 r = 0;
3226 break;
3227 }
3228 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003229 r = -EINVAL;
3230 if (!vcpu->arch.apic)
3231 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003232 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Guo Chao18595412012-11-02 18:33:21 +08003233 if (IS_ERR(u.lapic))
3234 return PTR_ERR(u.lapic);
Sasha Levinff5c2c02011-12-04 19:36:29 +02003235
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003236 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003237 break;
3238 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003239 case KVM_INTERRUPT: {
3240 struct kvm_interrupt irq;
3241
3242 r = -EFAULT;
3243 if (copy_from_user(&irq, argp, sizeof irq))
3244 goto out;
3245 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003246 break;
3247 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003248 case KVM_NMI: {
3249 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003250 break;
3251 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003252 case KVM_SET_CPUID: {
3253 struct kvm_cpuid __user *cpuid_arg = argp;
3254 struct kvm_cpuid cpuid;
3255
3256 r = -EFAULT;
3257 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3258 goto out;
3259 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003260 break;
3261 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003262 case KVM_SET_CPUID2: {
3263 struct kvm_cpuid2 __user *cpuid_arg = argp;
3264 struct kvm_cpuid2 cpuid;
3265
3266 r = -EFAULT;
3267 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3268 goto out;
3269 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003270 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003271 break;
3272 }
3273 case KVM_GET_CPUID2: {
3274 struct kvm_cpuid2 __user *cpuid_arg = argp;
3275 struct kvm_cpuid2 cpuid;
3276
3277 r = -EFAULT;
3278 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3279 goto out;
3280 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003281 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003282 if (r)
3283 goto out;
3284 r = -EFAULT;
3285 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3286 goto out;
3287 r = 0;
3288 break;
3289 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003290 case KVM_GET_MSRS:
3291 r = msr_io(vcpu, argp, kvm_get_msr, 1);
3292 break;
3293 case KVM_SET_MSRS:
3294 r = msr_io(vcpu, argp, do_set_msr, 0);
3295 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02003296 case KVM_TPR_ACCESS_REPORTING: {
3297 struct kvm_tpr_access_ctl tac;
3298
3299 r = -EFAULT;
3300 if (copy_from_user(&tac, argp, sizeof tac))
3301 goto out;
3302 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3303 if (r)
3304 goto out;
3305 r = -EFAULT;
3306 if (copy_to_user(argp, &tac, sizeof tac))
3307 goto out;
3308 r = 0;
3309 break;
3310 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003311 case KVM_SET_VAPIC_ADDR: {
3312 struct kvm_vapic_addr va;
3313
3314 r = -EINVAL;
3315 if (!irqchip_in_kernel(vcpu->kvm))
3316 goto out;
3317 r = -EFAULT;
3318 if (copy_from_user(&va, argp, sizeof va))
3319 goto out;
Andy Honigfda4e2e82013-11-20 10:23:22 -08003320 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Avi Kivityb93463a2007-10-25 16:52:32 +02003321 break;
3322 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003323 case KVM_X86_SETUP_MCE: {
3324 u64 mcg_cap;
3325
3326 r = -EFAULT;
3327 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3328 goto out;
3329 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3330 break;
3331 }
3332 case KVM_X86_SET_MCE: {
3333 struct kvm_x86_mce mce;
3334
3335 r = -EFAULT;
3336 if (copy_from_user(&mce, argp, sizeof mce))
3337 goto out;
3338 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3339 break;
3340 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003341 case KVM_GET_VCPU_EVENTS: {
3342 struct kvm_vcpu_events events;
3343
3344 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3345
3346 r = -EFAULT;
3347 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3348 break;
3349 r = 0;
3350 break;
3351 }
3352 case KVM_SET_VCPU_EVENTS: {
3353 struct kvm_vcpu_events events;
3354
3355 r = -EFAULT;
3356 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3357 break;
3358
3359 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3360 break;
3361 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003362 case KVM_GET_DEBUGREGS: {
3363 struct kvm_debugregs dbgregs;
3364
3365 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3366
3367 r = -EFAULT;
3368 if (copy_to_user(argp, &dbgregs,
3369 sizeof(struct kvm_debugregs)))
3370 break;
3371 r = 0;
3372 break;
3373 }
3374 case KVM_SET_DEBUGREGS: {
3375 struct kvm_debugregs dbgregs;
3376
3377 r = -EFAULT;
3378 if (copy_from_user(&dbgregs, argp,
3379 sizeof(struct kvm_debugregs)))
3380 break;
3381
3382 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3383 break;
3384 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003385 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003386 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003387 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003388 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003389 break;
3390
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003391 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003392
3393 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003394 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003395 break;
3396 r = 0;
3397 break;
3398 }
3399 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003400 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Guo Chao18595412012-11-02 18:33:21 +08003401 if (IS_ERR(u.xsave))
3402 return PTR_ERR(u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003403
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003404 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003405 break;
3406 }
3407 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003408 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003409 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003410 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003411 break;
3412
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003413 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003414
3415 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003416 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003417 sizeof(struct kvm_xcrs)))
3418 break;
3419 r = 0;
3420 break;
3421 }
3422 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003423 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Guo Chao18595412012-11-02 18:33:21 +08003424 if (IS_ERR(u.xcrs))
3425 return PTR_ERR(u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003426
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003427 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003428 break;
3429 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003430 case KVM_SET_TSC_KHZ: {
3431 u32 user_tsc_khz;
3432
3433 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003434 user_tsc_khz = (u32)arg;
3435
3436 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3437 goto out;
3438
Zachary Amsdencc578282012-02-03 15:43:50 -02003439 if (user_tsc_khz == 0)
3440 user_tsc_khz = tsc_khz;
3441
3442 kvm_set_tsc_khz(vcpu, user_tsc_khz);
Joerg Roedel92a1f122011-03-25 09:44:51 +01003443
3444 r = 0;
3445 goto out;
3446 }
3447 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003448 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003449 goto out;
3450 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003451 case KVM_KVMCLOCK_CTRL: {
3452 r = kvm_set_guest_paused(vcpu);
3453 goto out;
3454 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003455 default:
3456 r = -EINVAL;
3457 }
3458out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003459 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003460 return r;
3461}
3462
Carsten Otte5b1c1492012-01-04 10:25:23 +01003463int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
3464{
3465 return VM_FAULT_SIGBUS;
3466}
3467
Carsten Otte1fe779f2007-10-29 16:08:35 +01003468static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
3469{
3470 int ret;
3471
3472 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08003473 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003474 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
3475 return ret;
3476}
3477
Sheng Yangb927a3c2009-07-21 10:42:48 +08003478static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
3479 u64 ident_addr)
3480{
3481 kvm->arch.ept_identity_map_addr = ident_addr;
3482 return 0;
3483}
3484
Carsten Otte1fe779f2007-10-29 16:08:35 +01003485static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
3486 u32 kvm_nr_mmu_pages)
3487{
3488 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
3489 return -EINVAL;
3490
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003491 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003492
3493 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003494 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003495
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003496 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003497 return 0;
3498}
3499
3500static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
3501{
Dave Hansen39de71e2010-08-19 18:11:14 -07003502 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003503}
3504
Carsten Otte1fe779f2007-10-29 16:08:35 +01003505static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3506{
3507 int r;
3508
3509 r = 0;
3510 switch (chip->chip_id) {
3511 case KVM_IRQCHIP_PIC_MASTER:
3512 memcpy(&chip->chip.pic,
3513 &pic_irqchip(kvm)->pics[0],
3514 sizeof(struct kvm_pic_state));
3515 break;
3516 case KVM_IRQCHIP_PIC_SLAVE:
3517 memcpy(&chip->chip.pic,
3518 &pic_irqchip(kvm)->pics[1],
3519 sizeof(struct kvm_pic_state));
3520 break;
3521 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003522 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003523 break;
3524 default:
3525 r = -EINVAL;
3526 break;
3527 }
3528 return r;
3529}
3530
3531static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3532{
3533 int r;
3534
3535 r = 0;
3536 switch (chip->chip_id) {
3537 case KVM_IRQCHIP_PIC_MASTER:
Avi Kivityf4f51052010-09-19 18:44:07 +02003538 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003539 memcpy(&pic_irqchip(kvm)->pics[0],
3540 &chip->chip.pic,
3541 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003542 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003543 break;
3544 case KVM_IRQCHIP_PIC_SLAVE:
Avi Kivityf4f51052010-09-19 18:44:07 +02003545 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003546 memcpy(&pic_irqchip(kvm)->pics[1],
3547 &chip->chip.pic,
3548 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003549 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003550 break;
3551 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003552 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003553 break;
3554 default:
3555 r = -EINVAL;
3556 break;
3557 }
3558 kvm_pic_update_irq(pic_irqchip(kvm));
3559 return r;
3560}
3561
Sheng Yange0f63cb2008-03-04 00:50:59 +08003562static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3563{
3564 int r = 0;
3565
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003566 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003567 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003568 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003569 return r;
3570}
3571
3572static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3573{
3574 int r = 0;
3575
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003576 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003577 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04003578 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
3579 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
3580 return r;
3581}
3582
3583static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3584{
3585 int r = 0;
3586
3587 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3588 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
3589 sizeof(ps->channels));
3590 ps->flags = kvm->arch.vpit->pit_state.flags;
3591 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003592 memset(&ps->reserved, 0, sizeof(ps->reserved));
Beth Kone9f42752009-07-07 11:50:38 -04003593 return r;
3594}
3595
3596static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3597{
3598 int r = 0, start = 0;
3599 u32 prev_legacy, cur_legacy;
3600 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3601 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
3602 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
3603 if (!prev_legacy && cur_legacy)
3604 start = 1;
3605 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
3606 sizeof(kvm->arch.vpit->pit_state.channels));
3607 kvm->arch.vpit->pit_state.flags = ps->flags;
3608 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003609 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003610 return r;
3611}
3612
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003613static int kvm_vm_ioctl_reinject(struct kvm *kvm,
3614 struct kvm_reinject_control *control)
3615{
3616 if (!kvm->arch.vpit)
3617 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003618 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Avi Kivity26ef1922012-07-26 18:01:53 +03003619 kvm->arch.vpit->pit_state.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003620 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003621 return 0;
3622}
3623
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003624/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003625 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
3626 * @kvm: kvm instance
3627 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003628 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003629 * We need to keep it in mind that VCPU threads can write to the bitmap
3630 * concurrently. So, to avoid losing data, we keep the following order for
3631 * each bit:
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003632 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003633 * 1. Take a snapshot of the bit and clear it if needed.
3634 * 2. Write protect the corresponding page.
3635 * 3. Flush TLB's if needed.
3636 * 4. Copy the snapshot to the userspace.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003637 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003638 * Between 2 and 3, the guest may write to the page using the remaining TLB
3639 * entry. This is not a problem because the page will be reported dirty at
3640 * step 4 using the snapshot taken before and step 3 ensures that successive
3641 * writes will be logged for the next call.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003642 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003643int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003644{
Takuya Yoshikawa7850ac52011-11-14 18:23:34 +09003645 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003646 struct kvm_memory_slot *memslot;
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003647 unsigned long n, i;
3648 unsigned long *dirty_bitmap;
3649 unsigned long *dirty_bitmap_buffer;
3650 bool is_dirty = false;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003651
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003652 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003653
Marcelo Tosattib050b012009-12-23 14:35:22 -02003654 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07003655 if (log->slot >= KVM_USER_MEM_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003656 goto out;
3657
Xiao Guangrong28a37542011-11-24 19:04:35 +08003658 memslot = id_to_memslot(kvm->memslots, log->slot);
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003659
3660 dirty_bitmap = memslot->dirty_bitmap;
Marcelo Tosattib050b012009-12-23 14:35:22 -02003661 r = -ENOENT;
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003662 if (!dirty_bitmap)
Marcelo Tosattib050b012009-12-23 14:35:22 -02003663 goto out;
3664
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09003665 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02003666
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003667 dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long);
3668 memset(dirty_bitmap_buffer, 0, n);
Marcelo Tosattib050b012009-12-23 14:35:22 -02003669
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003670 spin_lock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02003671
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003672 for (i = 0; i < n / sizeof(long); i++) {
3673 unsigned long mask;
3674 gfn_t offset;
Sasha Levincdfca7b2011-12-04 19:36:28 +02003675
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003676 if (!dirty_bitmap[i])
3677 continue;
Marcelo Tosattib050b012009-12-23 14:35:22 -02003678
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003679 is_dirty = true;
Takuya Yoshikawa914ebcc2010-04-28 18:50:36 +09003680
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003681 mask = xchg(&dirty_bitmap[i], 0);
3682 dirty_bitmap_buffer[i] = mask;
Michael S. Tsirkinedde99c2010-10-25 03:21:24 +02003683
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003684 offset = i * BITS_PER_LONG;
3685 kvm_mmu_write_protect_pt_masked(kvm, memslot, offset, mask);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003686 }
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003687
3688 spin_unlock(&kvm->mmu_lock);
3689
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003690 /* See the comments in kvm_mmu_slot_remove_write_access(). */
3691 lockdep_assert_held(&kvm->slots_lock);
3692
3693 /*
3694 * All the TLBs can be flushed out of mmu lock, see the comments in
3695 * kvm_mmu_slot_remove_write_access().
3696 */
3697 if (is_dirty)
3698 kvm_flush_remote_tlbs(kvm);
3699
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003700 r = -EFAULT;
3701 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
3702 goto out;
Marcelo Tosattib050b012009-12-23 14:35:22 -02003703
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003704 r = 0;
3705out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003706 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003707 return r;
3708}
3709
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003710int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
3711 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003712{
3713 if (!irqchip_in_kernel(kvm))
3714 return -ENXIO;
3715
3716 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003717 irq_event->irq, irq_event->level,
3718 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003719 return 0;
3720}
3721
Carsten Otte1fe779f2007-10-29 16:08:35 +01003722long kvm_arch_vm_ioctl(struct file *filp,
3723 unsigned int ioctl, unsigned long arg)
3724{
3725 struct kvm *kvm = filp->private_data;
3726 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03003727 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07003728 /*
3729 * This union makes it completely explicit to gcc-3.x
3730 * that these two variables' stack usage should be
3731 * combined, not added together.
3732 */
3733 union {
3734 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04003735 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003736 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07003737 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003738
3739 switch (ioctl) {
3740 case KVM_SET_TSS_ADDR:
3741 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003742 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003743 case KVM_SET_IDENTITY_MAP_ADDR: {
3744 u64 ident_addr;
3745
3746 r = -EFAULT;
3747 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
3748 goto out;
3749 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08003750 break;
3751 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003752 case KVM_SET_NR_MMU_PAGES:
3753 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003754 break;
3755 case KVM_GET_NR_MMU_PAGES:
3756 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3757 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003758 case KVM_CREATE_IRQCHIP: {
3759 struct kvm_pic *vpic;
3760
3761 mutex_lock(&kvm->lock);
3762 r = -EEXIST;
3763 if (kvm->arch.vpic)
3764 goto create_irqchip_unlock;
Avi Kivity3e515702012-03-05 14:23:29 +02003765 r = -EINVAL;
3766 if (atomic_read(&kvm->online_vcpus))
3767 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003768 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003769 vpic = kvm_create_pic(kvm);
3770 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003771 r = kvm_ioapic_init(kvm);
3772 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003773 mutex_lock(&kvm->slots_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003774 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
Sasha Levin743eeb02011-07-27 16:00:48 +03003775 &vpic->dev_master);
3776 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3777 &vpic->dev_slave);
3778 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3779 &vpic->dev_eclr);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003780 mutex_unlock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003781 kfree(vpic);
3782 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003783 }
3784 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003785 goto create_irqchip_unlock;
3786 smp_wmb();
3787 kvm->arch.vpic = vpic;
3788 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02003789 r = kvm_setup_default_irq_routing(kvm);
3790 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003791 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003792 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003793 kvm_ioapic_destroy(kvm);
3794 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003795 mutex_unlock(&kvm->irq_lock);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003796 mutex_unlock(&kvm->slots_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02003797 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003798 create_irqchip_unlock:
3799 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003800 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003801 }
Sheng Yang78376992008-01-28 05:10:22 +08003802 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003803 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3804 goto create_pit;
3805 case KVM_CREATE_PIT2:
3806 r = -EFAULT;
3807 if (copy_from_user(&u.pit_config, argp,
3808 sizeof(struct kvm_pit_config)))
3809 goto out;
3810 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003811 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003812 r = -EEXIST;
3813 if (kvm->arch.vpit)
3814 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003815 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003816 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003817 if (kvm->arch.vpit)
3818 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003819 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003820 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08003821 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003822 case KVM_GET_IRQCHIP: {
3823 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003824 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003825
Sasha Levinff5c2c02011-12-04 19:36:29 +02003826 chip = memdup_user(argp, sizeof(*chip));
3827 if (IS_ERR(chip)) {
3828 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003829 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003830 }
3831
Carsten Otte1fe779f2007-10-29 16:08:35 +01003832 r = -ENXIO;
3833 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003834 goto get_irqchip_out;
3835 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3836 if (r)
3837 goto get_irqchip_out;
3838 r = -EFAULT;
3839 if (copy_to_user(argp, chip, sizeof *chip))
3840 goto get_irqchip_out;
3841 r = 0;
3842 get_irqchip_out:
3843 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003844 break;
3845 }
3846 case KVM_SET_IRQCHIP: {
3847 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003848 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003849
Sasha Levinff5c2c02011-12-04 19:36:29 +02003850 chip = memdup_user(argp, sizeof(*chip));
3851 if (IS_ERR(chip)) {
3852 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003853 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003854 }
3855
Carsten Otte1fe779f2007-10-29 16:08:35 +01003856 r = -ENXIO;
3857 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003858 goto set_irqchip_out;
3859 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3860 if (r)
3861 goto set_irqchip_out;
3862 r = 0;
3863 set_irqchip_out:
3864 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003865 break;
3866 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003867 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003868 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003869 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003870 goto out;
3871 r = -ENXIO;
3872 if (!kvm->arch.vpit)
3873 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003874 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003875 if (r)
3876 goto out;
3877 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003878 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003879 goto out;
3880 r = 0;
3881 break;
3882 }
3883 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003884 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003885 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003886 goto out;
3887 r = -ENXIO;
3888 if (!kvm->arch.vpit)
3889 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003890 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003891 break;
3892 }
Beth Kone9f42752009-07-07 11:50:38 -04003893 case KVM_GET_PIT2: {
3894 r = -ENXIO;
3895 if (!kvm->arch.vpit)
3896 goto out;
3897 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3898 if (r)
3899 goto out;
3900 r = -EFAULT;
3901 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3902 goto out;
3903 r = 0;
3904 break;
3905 }
3906 case KVM_SET_PIT2: {
3907 r = -EFAULT;
3908 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3909 goto out;
3910 r = -ENXIO;
3911 if (!kvm->arch.vpit)
3912 goto out;
3913 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04003914 break;
3915 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003916 case KVM_REINJECT_CONTROL: {
3917 struct kvm_reinject_control control;
3918 r = -EFAULT;
3919 if (copy_from_user(&control, argp, sizeof(control)))
3920 goto out;
3921 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003922 break;
3923 }
Ed Swierkffde22a2009-10-15 15:21:43 -07003924 case KVM_XEN_HVM_CONFIG: {
3925 r = -EFAULT;
3926 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3927 sizeof(struct kvm_xen_hvm_config)))
3928 goto out;
3929 r = -EINVAL;
3930 if (kvm->arch.xen_hvm_config.flags)
3931 goto out;
3932 r = 0;
3933 break;
3934 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003935 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003936 struct kvm_clock_data user_ns;
3937 u64 now_ns;
3938 s64 delta;
3939
3940 r = -EFAULT;
3941 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3942 goto out;
3943
3944 r = -EINVAL;
3945 if (user_ns.flags)
3946 goto out;
3947
3948 r = 0;
Avi Kivity395c6b02010-10-04 12:55:49 +02003949 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003950 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003951 delta = user_ns.clock - now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003952 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003953 kvm->arch.kvmclock_offset = delta;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03003954 kvm_gen_update_masterclock(kvm);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003955 break;
3956 }
3957 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003958 struct kvm_clock_data user_ns;
3959 u64 now_ns;
3960
Avi Kivity395c6b02010-10-04 12:55:49 +02003961 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003962 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003963 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003964 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003965 user_ns.flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003966 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003967
3968 r = -EFAULT;
3969 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3970 goto out;
3971 r = 0;
3972 break;
3973 }
3974
Carsten Otte1fe779f2007-10-29 16:08:35 +01003975 default:
3976 ;
3977 }
3978out:
3979 return r;
3980}
3981
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003982static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003983{
3984 u32 dummy[2];
3985 unsigned i, j;
3986
Glauber Costae3267cb2009-10-06 13:24:50 -04003987 /* skip the first msrs in the list. KVM-specific */
3988 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003989 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3990 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003991
3992 /*
3993 * Even MSRs that are valid in the host may not be exposed
3994 * to the guests in some cases. We could work around this
3995 * in VMX with the generic MSR save/load machinery, but it
3996 * is not really worthwhile since it will really only
3997 * happen with nested virtualization.
3998 */
3999 switch (msrs_to_save[i]) {
4000 case MSR_IA32_BNDCFGS:
4001 if (!kvm_x86_ops->mpx_supported())
4002 continue;
4003 break;
4004 default:
4005 break;
4006 }
4007
Carsten Otte043405e2007-10-10 17:16:19 +02004008 if (j < i)
4009 msrs_to_save[j] = msrs_to_save[i];
4010 j++;
4011 }
4012 num_msrs_to_save = j;
4013}
4014
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004015static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
4016 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004017{
Avi Kivity70252a12010-01-19 12:51:22 +02004018 int handled = 0;
4019 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004020
Avi Kivity70252a12010-01-19 12:51:22 +02004021 do {
4022 n = min(len, 8);
4023 if (!(vcpu->arch.apic &&
4024 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
4025 && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
4026 break;
4027 handled += n;
4028 addr += n;
4029 len -= n;
4030 v += n;
4031 } while (len);
4032
4033 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004034}
4035
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004036static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004037{
Avi Kivity70252a12010-01-19 12:51:22 +02004038 int handled = 0;
4039 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004040
Avi Kivity70252a12010-01-19 12:51:22 +02004041 do {
4042 n = min(len, 8);
4043 if (!(vcpu->arch.apic &&
4044 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
4045 && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
4046 break;
4047 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
4048 handled += n;
4049 addr += n;
4050 len -= n;
4051 v += n;
4052 } while (len);
4053
4054 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004055}
4056
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004057static void kvm_set_segment(struct kvm_vcpu *vcpu,
4058 struct kvm_segment *var, int seg)
4059{
4060 kvm_x86_ops->set_segment(vcpu, var, seg);
4061}
4062
4063void kvm_get_segment(struct kvm_vcpu *vcpu,
4064 struct kvm_segment *var, int seg)
4065{
4066 kvm_x86_ops->get_segment(vcpu, var, seg);
4067}
4068
Paolo Bonzini54987b72014-09-02 13:23:06 +02004069gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
4070 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004071{
4072 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004073
4074 BUG_ON(!mmu_is_nested(vcpu));
4075
4076 /* NPT walks are always user-walks */
4077 access |= PFERR_USER_MASK;
Paolo Bonzini54987b72014-09-02 13:23:06 +02004078 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004079
4080 return t_gpa;
4081}
4082
Avi Kivityab9ae312010-11-22 17:53:26 +02004083gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
4084 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004085{
4086 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02004087 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004088}
4089
Avi Kivityab9ae312010-11-22 17:53:26 +02004090 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4091 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004092{
4093 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4094 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004095 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004096}
4097
Avi Kivityab9ae312010-11-22 17:53:26 +02004098gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4099 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004100{
4101 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4102 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004103 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004104}
4105
4106/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004107gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4108 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004109{
Avi Kivityab9ae312010-11-22 17:53:26 +02004110 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004111}
4112
4113static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4114 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004115 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004116{
4117 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004118 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004119
4120 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004121 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004122 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004123 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004124 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004125 int ret;
4126
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004127 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004128 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004129 ret = kvm_read_guest_page(vcpu->kvm, gpa >> PAGE_SHIFT, data,
4130 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004131 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004132 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004133 goto out;
4134 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004135
Izik Eidus77c20022008-12-29 01:42:19 +02004136 bytes -= toread;
4137 data += toread;
4138 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004139 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004140out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004141 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004142}
Izik Eidus77c20022008-12-29 01:42:19 +02004143
Gleb Natapov1871c602010-02-10 14:21:32 +02004144/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004145static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4146 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004147 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004148{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004149 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004150 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004151 unsigned offset;
4152 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004153
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004154 /* Inline kvm_read_guest_virt_helper for speed. */
4155 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4156 exception);
4157 if (unlikely(gpa == UNMAPPED_GVA))
4158 return X86EMUL_PROPAGATE_FAULT;
4159
4160 offset = addr & (PAGE_SIZE-1);
4161 if (WARN_ON(offset + bytes > PAGE_SIZE))
4162 bytes = (unsigned)PAGE_SIZE - offset;
4163 ret = kvm_read_guest_page(vcpu->kvm, gpa >> PAGE_SHIFT, val,
4164 offset, bytes);
4165 if (unlikely(ret < 0))
4166 return X86EMUL_IO_NEEDED;
4167
4168 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004169}
4170
Nadav Har'El064aea72011-05-25 23:04:56 +03004171int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004172 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004173 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004174{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004175 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004176 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004177
Gleb Natapov1871c602010-02-10 14:21:32 +02004178 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004179 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004180}
Nadav Har'El064aea72011-05-25 23:04:56 +03004181EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004182
Avi Kivity0f65dd72011-04-20 13:37:53 +03004183static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
4184 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004185 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004186{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004187 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004188 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004189}
4190
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004191int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004192 gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004193 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004194 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004195{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004196 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Izik Eidus77c20022008-12-29 01:42:19 +02004197 void *data = val;
4198 int r = X86EMUL_CONTINUE;
4199
4200 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004201 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
4202 PFERR_WRITE_MASK,
Avi Kivityab9ae312010-11-22 17:53:26 +02004203 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004204 unsigned offset = addr & (PAGE_SIZE-1);
4205 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4206 int ret;
4207
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004208 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004209 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus77c20022008-12-29 01:42:19 +02004210 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
4211 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004212 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004213 goto out;
4214 }
4215
4216 bytes -= towrite;
4217 data += towrite;
4218 addr += towrite;
4219 }
4220out:
4221 return r;
4222}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004223EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004224
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004225static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
4226 gpa_t *gpa, struct x86_exception *exception,
4227 bool write)
4228{
Avi Kivity97d64b72012-09-12 14:52:00 +03004229 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
4230 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004231
Avi Kivity97d64b72012-09-12 14:52:00 +03004232 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08004233 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
4234 vcpu->arch.access, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004235 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
4236 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08004237 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004238 return 1;
4239 }
4240
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004241 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
4242
4243 if (*gpa == UNMAPPED_GVA)
4244 return -1;
4245
4246 /* For APIC access vmexit */
4247 if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4248 return 1;
4249
Xiao Guangrong4f022642011-07-12 03:34:24 +08004250 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
4251 trace_vcpu_match_mmio(gva, *gpa, write, true);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004252 return 1;
Xiao Guangrong4f022642011-07-12 03:34:24 +08004253 }
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004254
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004255 return 0;
4256}
4257
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004258int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004259 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02004260{
4261 int ret;
4262
4263 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
4264 if (ret < 0)
4265 return 0;
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004266 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004267 return 1;
4268}
4269
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004270struct read_write_emulator_ops {
4271 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
4272 int bytes);
4273 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
4274 void *val, int bytes);
4275 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4276 int bytes, void *val);
4277 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4278 void *val, int bytes);
4279 bool write;
4280};
4281
4282static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
4283{
4284 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004285 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Avi Kivityf78146b2012-04-18 19:22:47 +03004286 vcpu->mmio_fragments[0].gpa, *(u64 *)val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004287 vcpu->mmio_read_completed = 0;
4288 return 1;
4289 }
4290
4291 return 0;
4292}
4293
4294static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4295 void *val, int bytes)
4296{
4297 return !kvm_read_guest(vcpu->kvm, gpa, val, bytes);
4298}
4299
4300static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4301 void *val, int bytes)
4302{
4303 return emulator_write_phys(vcpu, gpa, val, bytes);
4304}
4305
4306static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
4307{
4308 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4309 return vcpu_mmio_write(vcpu, gpa, bytes, val);
4310}
4311
4312static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4313 void *val, int bytes)
4314{
4315 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4316 return X86EMUL_IO_NEEDED;
4317}
4318
4319static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4320 void *val, int bytes)
4321{
Avi Kivityf78146b2012-04-18 19:22:47 +03004322 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
4323
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004324 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004325 return X86EMUL_CONTINUE;
4326}
4327
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004328static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004329 .read_write_prepare = read_prepare,
4330 .read_write_emulate = read_emulate,
4331 .read_write_mmio = vcpu_mmio_read,
4332 .read_write_exit_mmio = read_exit_mmio,
4333};
4334
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004335static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004336 .read_write_emulate = write_emulate,
4337 .read_write_mmio = write_mmio,
4338 .read_write_exit_mmio = write_exit_mmio,
4339 .write = true,
4340};
4341
Xiao Guangrong22388a32011-07-13 14:32:31 +08004342static int emulator_read_write_onepage(unsigned long addr, void *val,
4343 unsigned int bytes,
4344 struct x86_exception *exception,
4345 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004346 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004347{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004348 gpa_t gpa;
4349 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004350 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03004351 struct kvm_mmio_fragment *frag;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004352
4353 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004354
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004355 if (ret < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004356 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004357
4358 /* For APIC access vmexit */
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004359 if (ret)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004360 goto mmio;
4361
Xiao Guangrong22388a32011-07-13 14:32:31 +08004362 if (ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004363 return X86EMUL_CONTINUE;
4364
4365mmio:
4366 /*
4367 * Is this MMIO handled locally?
4368 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08004369 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02004370 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004371 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004372
Avi Kivity70252a12010-01-19 12:51:22 +02004373 gpa += handled;
4374 bytes -= handled;
4375 val += handled;
4376
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004377 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
4378 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
4379 frag->gpa = gpa;
4380 frag->data = val;
4381 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03004382 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004383}
4384
Xiao Guangrong22388a32011-07-13 14:32:31 +08004385int emulator_read_write(struct x86_emulate_ctxt *ctxt, unsigned long addr,
4386 void *val, unsigned int bytes,
4387 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004388 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004389{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004390 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03004391 gpa_t gpa;
4392 int rc;
4393
4394 if (ops->read_write_prepare &&
4395 ops->read_write_prepare(vcpu, val, bytes))
4396 return X86EMUL_CONTINUE;
4397
4398 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004399
Carsten Ottebbd9b642007-10-30 18:44:21 +01004400 /* Crossing a page boundary? */
4401 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03004402 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004403
4404 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004405 rc = emulator_read_write_onepage(addr, val, now, exception,
4406 vcpu, ops);
4407
Carsten Ottebbd9b642007-10-30 18:44:21 +01004408 if (rc != X86EMUL_CONTINUE)
4409 return rc;
4410 addr += now;
4411 val += now;
4412 bytes -= now;
4413 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08004414
Avi Kivityf78146b2012-04-18 19:22:47 +03004415 rc = emulator_read_write_onepage(addr, val, bytes, exception,
4416 vcpu, ops);
4417 if (rc != X86EMUL_CONTINUE)
4418 return rc;
4419
4420 if (!vcpu->mmio_nr_fragments)
4421 return rc;
4422
4423 gpa = vcpu->mmio_fragments[0].gpa;
4424
4425 vcpu->mmio_needed = 1;
4426 vcpu->mmio_cur_fragment = 0;
4427
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004428 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03004429 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
4430 vcpu->run->exit_reason = KVM_EXIT_MMIO;
4431 vcpu->run->mmio.phys_addr = gpa;
4432
4433 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08004434}
4435
4436static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4437 unsigned long addr,
4438 void *val,
4439 unsigned int bytes,
4440 struct x86_exception *exception)
4441{
4442 return emulator_read_write(ctxt, addr, val, bytes,
4443 exception, &read_emultor);
4444}
4445
4446int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
4447 unsigned long addr,
4448 const void *val,
4449 unsigned int bytes,
4450 struct x86_exception *exception)
4451{
4452 return emulator_read_write(ctxt, addr, (void *)val, bytes,
4453 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004454}
Carsten Ottebbd9b642007-10-30 18:44:21 +01004455
Avi Kivitydaea3e72010-03-15 13:59:54 +02004456#define CMPXCHG_TYPE(t, ptr, old, new) \
4457 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
4458
4459#ifdef CONFIG_X86_64
4460# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
4461#else
4462# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01004463 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02004464#endif
4465
Avi Kivity0f65dd72011-04-20 13:37:53 +03004466static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
4467 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004468 const void *old,
4469 const void *new,
4470 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004471 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004472{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004473 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004474 gpa_t gpa;
4475 struct page *page;
4476 char *kaddr;
4477 bool exchanged;
4478
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004479 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02004480 if (bytes > 8 || (bytes & (bytes - 1)))
4481 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004482
Avi Kivitydaea3e72010-03-15 13:59:54 +02004483 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004484
Avi Kivitydaea3e72010-03-15 13:59:54 +02004485 if (gpa == UNMAPPED_GVA ||
4486 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4487 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004488
Avi Kivitydaea3e72010-03-15 13:59:54 +02004489 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
4490 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004491
Avi Kivitydaea3e72010-03-15 13:59:54 +02004492 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08004493 if (is_error_page(page))
Wei Yongjunc19b8bd62010-07-15 08:51:58 +08004494 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004495
Cong Wang8fd75e12011-11-25 23:14:17 +08004496 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004497 kaddr += offset_in_page(gpa);
4498 switch (bytes) {
4499 case 1:
4500 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
4501 break;
4502 case 2:
4503 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
4504 break;
4505 case 4:
4506 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
4507 break;
4508 case 8:
4509 exchanged = CMPXCHG64(kaddr, old, new);
4510 break;
4511 default:
4512 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004513 }
Cong Wang8fd75e12011-11-25 23:14:17 +08004514 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004515 kvm_release_page_dirty(page);
4516
4517 if (!exchanged)
4518 return X86EMUL_CMPXCHG_FAILED;
4519
Marcelo Tosattid3714012014-02-25 22:44:54 -03004520 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004521 kvm_mmu_pte_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004522
4523 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02004524
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004525emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02004526 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004527
Avi Kivity0f65dd72011-04-20 13:37:53 +03004528 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004529}
4530
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004531static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
4532{
4533 /* TODO: String I/O for in kernel device */
4534 int r;
4535
4536 if (vcpu->arch.pio.in)
4537 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
4538 vcpu->arch.pio.size, pd);
4539 else
4540 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
4541 vcpu->arch.pio.port, vcpu->arch.pio.size,
4542 pd);
4543 return r;
4544}
4545
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004546static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
4547 unsigned short port, void *val,
4548 unsigned int count, bool in)
4549{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004550 vcpu->arch.pio.port = port;
4551 vcpu->arch.pio.in = in;
4552 vcpu->arch.pio.count = count;
4553 vcpu->arch.pio.size = size;
4554
4555 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
4556 vcpu->arch.pio.count = 0;
4557 return 1;
4558 }
4559
4560 vcpu->run->exit_reason = KVM_EXIT_IO;
4561 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
4562 vcpu->run->io.size = size;
4563 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
4564 vcpu->run->io.count = count;
4565 vcpu->run->io.port = port;
4566
4567 return 0;
4568}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004569
Avi Kivityca1d4a92011-04-20 13:37:53 +03004570static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
4571 int size, unsigned short port, void *val,
4572 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004573{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004574 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004575 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03004576
Gleb Natapov79729952010-03-18 15:20:24 +02004577 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004578 goto data_avail;
4579
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004580 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
4581 if (ret) {
4582data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004583 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004584 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02004585 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004586 return 1;
4587 }
4588
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004589 return 0;
4590}
4591
Avi Kivityca1d4a92011-04-20 13:37:53 +03004592static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
4593 int size, unsigned short port,
4594 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004595{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004596 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4597
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004598 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004599 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004600 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004601}
4602
Carsten Ottebbd9b642007-10-30 18:44:21 +01004603static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
4604{
4605 return kvm_x86_ops->get_segment_base(vcpu, seg);
4606}
4607
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004608static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004609{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004610 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004611}
4612
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004613int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
4614{
4615 if (!need_emulate_wbinvd(vcpu))
4616 return X86EMUL_CONTINUE;
4617
4618 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01004619 int cpu = get_cpu();
4620
4621 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004622 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
4623 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004624 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004625 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004626 } else
4627 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004628 return X86EMUL_CONTINUE;
4629}
4630EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
4631
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004632static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
4633{
4634 kvm_emulate_wbinvd(emul_to_vcpu(ctxt));
4635}
4636
Avi Kivity717746e2011-04-20 13:37:53 +03004637int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004638{
Avi Kivity717746e2011-04-20 13:37:53 +03004639 return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004640}
4641
Avi Kivity717746e2011-04-20 13:37:53 +03004642int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004643{
Gleb Natapov338dbc92010-04-28 19:15:32 +03004644
Avi Kivity717746e2011-04-20 13:37:53 +03004645 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004646}
4647
Gleb Natapov52a46612010-03-18 15:20:03 +02004648static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4649{
4650 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4651}
4652
Avi Kivity717746e2011-04-20 13:37:53 +03004653static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02004654{
Avi Kivity717746e2011-04-20 13:37:53 +03004655 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02004656 unsigned long value;
4657
4658 switch (cr) {
4659 case 0:
4660 value = kvm_read_cr0(vcpu);
4661 break;
4662 case 2:
4663 value = vcpu->arch.cr2;
4664 break;
4665 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004666 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02004667 break;
4668 case 4:
4669 value = kvm_read_cr4(vcpu);
4670 break;
4671 case 8:
4672 value = kvm_get_cr8(vcpu);
4673 break;
4674 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004675 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02004676 return 0;
4677 }
4678
4679 return value;
4680}
4681
Avi Kivity717746e2011-04-20 13:37:53 +03004682static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02004683{
Avi Kivity717746e2011-04-20 13:37:53 +03004684 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03004685 int res = 0;
4686
Gleb Natapov52a46612010-03-18 15:20:03 +02004687 switch (cr) {
4688 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03004689 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004690 break;
4691 case 2:
4692 vcpu->arch.cr2 = val;
4693 break;
4694 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004695 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004696 break;
4697 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03004698 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004699 break;
4700 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004701 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004702 break;
4703 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004704 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03004705 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02004706 }
Gleb Natapov0f122442010-04-28 19:15:31 +03004707
4708 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02004709}
4710
Avi Kivity717746e2011-04-20 13:37:53 +03004711static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02004712{
Avi Kivity717746e2011-04-20 13:37:53 +03004713 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02004714}
4715
Avi Kivity4bff1e862011-04-20 13:37:53 +03004716static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004717{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004718 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004719}
4720
Avi Kivity4bff1e862011-04-20 13:37:53 +03004721static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004722{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004723 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004724}
4725
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004726static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4727{
4728 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
4729}
4730
4731static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4732{
4733 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
4734}
4735
Avi Kivity4bff1e862011-04-20 13:37:53 +03004736static unsigned long emulator_get_cached_segment_base(
4737 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03004738{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004739 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03004740}
4741
Avi Kivity1aa36612011-04-27 13:20:30 +03004742static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
4743 struct desc_struct *desc, u32 *base3,
4744 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004745{
4746 struct kvm_segment var;
4747
Avi Kivity4bff1e862011-04-20 13:37:53 +03004748 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03004749 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004750
Gleb Natapov378a8b02013-01-21 15:36:48 +02004751 if (var.unusable) {
4752 memset(desc, 0, sizeof(*desc));
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004753 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02004754 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004755
4756 if (var.g)
4757 var.limit >>= 12;
4758 set_desc_limit(desc, var.limit);
4759 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02004760#ifdef CONFIG_X86_64
4761 if (base3)
4762 *base3 = var.base >> 32;
4763#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004764 desc->type = var.type;
4765 desc->s = var.s;
4766 desc->dpl = var.dpl;
4767 desc->p = var.present;
4768 desc->avl = var.avl;
4769 desc->l = var.l;
4770 desc->d = var.db;
4771 desc->g = var.g;
4772
4773 return true;
4774}
4775
Avi Kivity1aa36612011-04-27 13:20:30 +03004776static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
4777 struct desc_struct *desc, u32 base3,
4778 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004779{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004780 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004781 struct kvm_segment var;
4782
Avi Kivity1aa36612011-04-27 13:20:30 +03004783 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004784 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02004785#ifdef CONFIG_X86_64
4786 var.base |= ((u64)base3) << 32;
4787#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004788 var.limit = get_desc_limit(desc);
4789 if (desc->g)
4790 var.limit = (var.limit << 12) | 0xfff;
4791 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004792 var.dpl = desc->dpl;
4793 var.db = desc->d;
4794 var.s = desc->s;
4795 var.l = desc->l;
4796 var.g = desc->g;
4797 var.avl = desc->avl;
4798 var.present = desc->p;
4799 var.unusable = !var.present;
4800 var.padding = 0;
4801
4802 kvm_set_segment(vcpu, &var, seg);
4803 return;
4804}
4805
Avi Kivity717746e2011-04-20 13:37:53 +03004806static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
4807 u32 msr_index, u64 *pdata)
4808{
4809 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
4810}
4811
4812static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
4813 u32 msr_index, u64 data)
4814{
Will Auld8fe8ab42012-11-29 12:42:12 -08004815 struct msr_data msr;
4816
4817 msr.data = data;
4818 msr.index = msr_index;
4819 msr.host_initiated = false;
4820 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03004821}
4822
Nadav Amit67f4d422014-06-02 18:34:09 +03004823static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
4824 u32 pmc)
4825{
4826 return kvm_pmu_check_pmc(emul_to_vcpu(ctxt), pmc);
4827}
4828
Avi Kivity222d21a2011-11-10 14:57:30 +02004829static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
4830 u32 pmc, u64 *pdata)
4831{
4832 return kvm_pmu_read_pmc(emul_to_vcpu(ctxt), pmc, pdata);
4833}
4834
Avi Kivity6c3287f2011-04-20 15:43:05 +03004835static void emulator_halt(struct x86_emulate_ctxt *ctxt)
4836{
4837 emul_to_vcpu(ctxt)->arch.halt_request = 1;
4838}
4839
Avi Kivity5037f6f2011-03-28 16:53:59 +02004840static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
4841{
4842 preempt_disable();
Avi Kivity5197b802011-04-20 15:55:40 +03004843 kvm_load_guest_fpu(emul_to_vcpu(ctxt));
Avi Kivity5037f6f2011-03-28 16:53:59 +02004844 /*
4845 * CR0.TS may reference the host fpu state, not the guest fpu state,
4846 * so it may be clear at this point.
4847 */
4848 clts();
4849}
4850
4851static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
4852{
4853 preempt_enable();
4854}
4855
Avi Kivity29535382011-04-20 13:37:53 +03004856static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004857 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004858 enum x86_intercept_stage stage)
4859{
Avi Kivity29535382011-04-20 13:37:53 +03004860 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004861}
4862
Avi Kivity0017f932012-06-07 14:10:16 +03004863static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004864 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
4865{
Avi Kivity0017f932012-06-07 14:10:16 +03004866 kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004867}
4868
Avi Kivitydd856ef2012-08-27 23:46:17 +03004869static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
4870{
4871 return kvm_register_read(emul_to_vcpu(ctxt), reg);
4872}
4873
4874static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
4875{
4876 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
4877}
4878
Mathias Krause0225fb52012-08-30 01:30:16 +02004879static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03004880 .read_gpr = emulator_read_gpr,
4881 .write_gpr = emulator_write_gpr,
Gleb Natapov1871c602010-02-10 14:21:32 +02004882 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004883 .write_std = kvm_write_guest_virt_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02004884 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004885 .read_emulated = emulator_read_emulated,
4886 .write_emulated = emulator_write_emulated,
4887 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004888 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004889 .pio_in_emulated = emulator_pio_in_emulated,
4890 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03004891 .get_segment = emulator_get_segment,
4892 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03004893 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004894 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004895 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004896 .set_gdt = emulator_set_gdt,
4897 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02004898 .get_cr = emulator_get_cr,
4899 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02004900 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03004901 .get_dr = emulator_get_dr,
4902 .set_dr = emulator_set_dr,
Avi Kivity717746e2011-04-20 13:37:53 +03004903 .set_msr = emulator_set_msr,
4904 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03004905 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02004906 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03004907 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004908 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03004909 .fix_hypercall = emulator_fix_hypercall,
Avi Kivity5037f6f2011-03-28 16:53:59 +02004910 .get_fpu = emulator_get_fpu,
4911 .put_fpu = emulator_put_fpu,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004912 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004913 .get_cpuid = emulator_get_cpuid,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004914};
4915
Gleb Natapov95cb2292010-04-28 19:15:43 +03004916static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
4917{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004918 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03004919 /*
4920 * an sti; sti; sequence only disable interrupts for the first
4921 * instruction. So, if the last instruction, be it emulated or
4922 * not, left the system with the INT_STI flag enabled, it
4923 * means that the last instruction is an sti. We should not
4924 * leave the flag on in this case. The same goes for mov ss
4925 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004926 if (int_shadow & mask)
4927 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004928 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004929 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004930 if (!mask)
4931 kvm_make_request(KVM_REQ_EVENT, vcpu);
4932 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03004933}
4934
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004935static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03004936{
4937 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02004938 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004939 return kvm_propagate_fault(vcpu, &ctxt->exception);
4940
4941 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02004942 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
4943 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03004944 else
Avi Kivityda9cb572010-11-22 17:53:21 +02004945 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004946 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03004947}
4948
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004949static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
4950{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004951 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004952 int cs_db, cs_l;
4953
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004954 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4955
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004956 ctxt->eflags = kvm_get_rflags(vcpu);
4957 ctxt->eip = kvm_rip_read(vcpu);
4958 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
4959 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03004960 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004961 cs_db ? X86EMUL_MODE_PROT32 :
4962 X86EMUL_MODE_PROT16;
4963 ctxt->guest_mode = is_guest_mode(vcpu);
4964
Avi Kivitydd856ef2012-08-27 23:46:17 +03004965 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02004966 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004967}
4968
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004969int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02004970{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004971 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02004972 int ret;
4973
4974 init_emulate_ctxt(vcpu);
4975
Avi Kivity9dac77f2011-06-01 15:34:25 +03004976 ctxt->op_bytes = 2;
4977 ctxt->ad_bytes = 2;
4978 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004979 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02004980
4981 if (ret != X86EMUL_CONTINUE)
4982 return EMULATE_FAIL;
4983
Avi Kivity9dac77f2011-06-01 15:34:25 +03004984 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004985 kvm_rip_write(vcpu, ctxt->eip);
4986 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02004987
4988 if (irq == NMI_VECTOR)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004989 vcpu->arch.nmi_pending = 0;
Mohammed Gamal63995652010-09-19 14:34:06 +02004990 else
4991 vcpu->arch.interrupt.pending = false;
4992
4993 return EMULATE_DONE;
4994}
4995EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
4996
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004997static int handle_emulation_failure(struct kvm_vcpu *vcpu)
4998{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004999 int r = EMULATE_DONE;
5000
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005001 ++vcpu->stat.insn_emulation_fail;
5002 trace_kvm_emulate_insn_failed(vcpu);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005003 if (!is_guest_mode(vcpu)) {
5004 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5005 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5006 vcpu->run->internal.ndata = 0;
5007 r = EMULATE_FAIL;
5008 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005009 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005010
5011 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005012}
5013
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005014static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03005015 bool write_fault_to_shadow_pgtable,
5016 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03005017{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005018 gpa_t gpa = cr2;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005019 pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005020
Gleb Natapov991eebf2013-04-11 12:10:51 +03005021 if (emulation_type & EMULTYPE_NO_REEXECUTE)
5022 return false;
5023
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005024 if (!vcpu->arch.mmu.direct_map) {
5025 /*
5026 * Write permission should be allowed since only
5027 * write access need to be emulated.
5028 */
5029 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03005030
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005031 /*
5032 * If the mapping is invalid in guest, let cpu retry
5033 * it to generate fault.
5034 */
5035 if (gpa == UNMAPPED_GVA)
5036 return true;
5037 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005038
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005039 /*
5040 * Do not retry the unhandleable instruction if it faults on the
5041 * readonly host memory, otherwise it will goto a infinite loop:
5042 * retry instruction -> write #PF -> emulation fail -> retry
5043 * instruction -> ...
5044 */
5045 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005046
5047 /*
5048 * If the instruction failed on the error pfn, it can not be fixed,
5049 * report the error to userspace.
5050 */
5051 if (is_error_noslot_pfn(pfn))
5052 return false;
5053
5054 kvm_release_pfn_clean(pfn);
5055
5056 /* The instructions are well-emulated on direct mmu. */
5057 if (vcpu->arch.mmu.direct_map) {
5058 unsigned int indirect_shadow_pages;
5059
5060 spin_lock(&vcpu->kvm->mmu_lock);
5061 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5062 spin_unlock(&vcpu->kvm->mmu_lock);
5063
5064 if (indirect_shadow_pages)
5065 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5066
Gleb Natapova6f177e2010-07-08 12:41:12 +03005067 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005068 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005069
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005070 /*
5071 * if emulation was due to access to shadowed page table
5072 * and it failed try to unshadow page and re-enter the
5073 * guest to let CPU execute the instruction.
5074 */
5075 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005076
5077 /*
5078 * If the access faults on its page table, it can not
5079 * be fixed by unprotecting shadow page and it should
5080 * be reported to userspace.
5081 */
5082 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005083}
5084
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005085static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5086 unsigned long cr2, int emulation_type)
5087{
5088 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5089 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5090
5091 last_retry_eip = vcpu->arch.last_retry_eip;
5092 last_retry_addr = vcpu->arch.last_retry_addr;
5093
5094 /*
5095 * If the emulation is caused by #PF and it is non-page_table
5096 * writing instruction, it means the VM-EXIT is caused by shadow
5097 * page protected, we can zap the shadow page and retry this
5098 * instruction directly.
5099 *
5100 * Note: if the guest uses a non-page-table modifying instruction
5101 * on the PDE that points to the instruction, then we will unmap
5102 * the instruction and go to an infinite loop. So, we cache the
5103 * last retried eip and the last fault address, if we meet the eip
5104 * and the address again, we can break out of the potential infinite
5105 * loop.
5106 */
5107 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5108
5109 if (!(emulation_type & EMULTYPE_RETRY))
5110 return false;
5111
5112 if (x86_page_table_writing_insn(ctxt))
5113 return false;
5114
5115 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5116 return false;
5117
5118 vcpu->arch.last_retry_eip = ctxt->eip;
5119 vcpu->arch.last_retry_addr = cr2;
5120
5121 if (!vcpu->arch.mmu.direct_map)
5122 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5123
Xiao Guangrong22368022013-01-13 23:44:12 +08005124 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005125
5126 return true;
5127}
5128
Gleb Natapov716d51a2012-09-03 15:24:26 +03005129static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5130static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5131
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005132static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
5133 unsigned long *db)
5134{
5135 u32 dr6 = 0;
5136 int i;
5137 u32 enable, rwlen;
5138
5139 enable = dr7;
5140 rwlen = dr7 >> 16;
5141 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
5142 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
5143 dr6 |= (1 << i);
5144 return dr6;
5145}
5146
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005147static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005148{
5149 struct kvm_run *kvm_run = vcpu->run;
5150
5151 /*
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005152 * rflags is the old, "raw" value of the flags. The new value has
5153 * not been saved yet.
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005154 *
5155 * This is correct even for TF set by the guest, because "the
5156 * processor will not generate this exception after the instruction
5157 * that sets the TF flag".
5158 */
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005159 if (unlikely(rflags & X86_EFLAGS_TF)) {
5160 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005161 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5162 DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005163 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5164 kvm_run->debug.arch.exception = DB_VECTOR;
5165 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5166 *r = EMULATE_USER_EXIT;
5167 } else {
5168 vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5169 /*
5170 * "Certain debug exceptions may clear bit 0-3. The
5171 * remaining contents of the DR6 register are never
5172 * cleared by the processor".
5173 */
5174 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005175 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005176 kvm_queue_exception(vcpu, DB_VECTOR);
5177 }
5178 }
5179}
5180
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005181static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
5182{
5183 struct kvm_run *kvm_run = vcpu->run;
5184 unsigned long eip = vcpu->arch.emulate_ctxt.eip;
5185 u32 dr6 = 0;
5186
5187 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
5188 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
5189 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
5190 vcpu->arch.guest_debug_dr7,
5191 vcpu->arch.eff_db);
5192
5193 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005194 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005195 kvm_run->debug.arch.pc = kvm_rip_read(vcpu) +
5196 get_segment_base(vcpu, VCPU_SREG_CS);
5197
5198 kvm_run->debug.arch.exception = DB_VECTOR;
5199 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5200 *r = EMULATE_USER_EXIT;
5201 return true;
5202 }
5203 }
5204
Nadav Amit4161a562014-07-17 01:19:31 +03005205 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
5206 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005207 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
5208 vcpu->arch.dr7,
5209 vcpu->arch.db);
5210
5211 if (dr6 != 0) {
5212 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005213 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005214 kvm_queue_exception(vcpu, DB_VECTOR);
5215 *r = EMULATE_DONE;
5216 return true;
5217 }
5218 }
5219
5220 return false;
5221}
5222
Andre Przywara51d8b662010-12-21 11:12:02 +01005223int x86_emulate_instruction(struct kvm_vcpu *vcpu,
5224 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01005225 int emulation_type,
5226 void *insn,
5227 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005228{
Gleb Natapov95cb2292010-04-28 19:15:43 +03005229 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005230 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005231 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005232 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005233
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005234 /*
5235 * Clear write_fault_to_shadow_pgtable here to ensure it is
5236 * never reused.
5237 */
5238 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03005239 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03005240
Sheng Yang571008d2008-01-02 14:49:22 +08005241 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005242 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005243
5244 /*
5245 * We will reenter on the same instruction since
5246 * we do not set complete_userspace_io. This does not
5247 * handle watchpoints yet, those would be handled in
5248 * the emulate_ops.
5249 */
5250 if (kvm_vcpu_check_breakpoint(vcpu, &r))
5251 return r;
5252
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005253 ctxt->interruptibility = 0;
5254 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005255 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005256 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005257
Borislav Petkovb51e9742013-09-22 16:44:52 +02005258 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02005259
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005260 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02005261
Avi Kivitye46479f2010-04-11 13:05:16 +03005262 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02005263 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005264 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02005265 if (emulation_type & EMULTYPE_TRAP_UD)
5266 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03005267 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5268 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005269 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005270 if (emulation_type & EMULTYPE_SKIP)
5271 return EMULATE_FAIL;
5272 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005273 }
5274 }
5275
Gleb Natapovba8afb62009-04-12 13:36:57 +03005276 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005277 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03005278 if (ctxt->eflags & X86_EFLAGS_RF)
5279 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03005280 return EMULATE_DONE;
5281 }
5282
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005283 if (retry_instruction(ctxt, cr2, emulation_type))
5284 return EMULATE_DONE;
5285
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005286 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005287 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005288 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
5289 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005290 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005291 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005292
Gleb Natapov5cd21912010-03-18 15:20:26 +02005293restart:
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005294 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005295
Joerg Roedel775fde82011-04-04 12:39:24 +02005296 if (r == EMULATION_INTERCEPTED)
5297 return EMULATE_DONE;
5298
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005299 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03005300 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5301 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005302 return EMULATE_DONE;
5303
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005304 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005305 }
5306
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005307 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005308 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005309 if (inject_emulated_exception(vcpu))
5310 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005311 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02005312 if (!vcpu->arch.pio.in) {
5313 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03005314 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02005315 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005316 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005317 vcpu->arch.complete_userspace_io = complete_emulated_pio;
5318 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005319 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005320 } else if (vcpu->mmio_needed) {
5321 if (!vcpu->mmio_is_write)
5322 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005323 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005324 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005325 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03005326 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005327 else
5328 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03005329
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005330 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005331 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005332 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005333 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005334 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005335 if (r == EMULATE_DONE)
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005336 kvm_vcpu_check_singlestep(vcpu, rflags, &r);
5337 __kvm_set_rflags(vcpu, ctxt->eflags);
5338
5339 /*
5340 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
5341 * do nothing, and it will be requested again as soon as
5342 * the shadow expires. But we still need to check here,
5343 * because POPF has no interrupt shadow.
5344 */
5345 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
5346 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005347 } else
5348 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03005349
Gleb Natapove85d28f2010-07-29 15:11:52 +03005350 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005351}
Andre Przywara51d8b662010-12-21 11:12:02 +01005352EXPORT_SYMBOL_GPL(x86_emulate_instruction);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005353
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005354int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01005355{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005356 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005357 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
5358 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005359 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02005360 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02005361 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01005362}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005363EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01005364
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005365static void tsc_bad(void *info)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005366{
Tejun Heo0a3aee02010-12-18 16:28:55 +01005367 __this_cpu_write(cpu_tsc_khz, 0);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005368}
5369
5370static void tsc_khz_changed(void *data)
5371{
5372 struct cpufreq_freqs *freq = data;
5373 unsigned long khz = 0;
5374
5375 if (data)
5376 khz = freq->new;
5377 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5378 khz = cpufreq_quick_get(raw_smp_processor_id());
5379 if (!khz)
5380 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01005381 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005382}
5383
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005384static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
5385 void *data)
5386{
5387 struct cpufreq_freqs *freq = data;
5388 struct kvm *kvm;
5389 struct kvm_vcpu *vcpu;
5390 int i, send_ipi = 0;
5391
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005392 /*
5393 * We allow guests to temporarily run on slowing clocks,
5394 * provided we notify them after, or to run on accelerating
5395 * clocks, provided we notify them before. Thus time never
5396 * goes backwards.
5397 *
5398 * However, we have a problem. We can't atomically update
5399 * the frequency of a given CPU from this function; it is
5400 * merely a notifier, which can be called from any CPU.
5401 * Changing the TSC frequency at arbitrary points in time
5402 * requires a recomputation of local variables related to
5403 * the TSC for each VCPU. We must flag these local variables
5404 * to be updated and be sure the update takes place with the
5405 * new frequency before any guests proceed.
5406 *
5407 * Unfortunately, the combination of hotplug CPU and frequency
5408 * change creates an intractable locking scenario; the order
5409 * of when these callouts happen is undefined with respect to
5410 * CPU hotplug, and they can race with each other. As such,
5411 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
5412 * undefined; you can actually have a CPU frequency change take
5413 * place in between the computation of X and the setting of the
5414 * variable. To protect against this problem, all updates of
5415 * the per_cpu tsc_khz variable are done in an interrupt
5416 * protected IPI, and all callers wishing to update the value
5417 * must wait for a synchronous IPI to complete (which is trivial
5418 * if the caller is on the CPU already). This establishes the
5419 * necessary total order on variable updates.
5420 *
5421 * Note that because a guest time update may take place
5422 * anytime after the setting of the VCPU's request bit, the
5423 * correct TSC value must be set before the request. However,
5424 * to ensure the update actually makes it to any guest which
5425 * starts running in hardware virtualization between the set
5426 * and the acquisition of the spinlock, we must also ping the
5427 * CPU after setting the request bit.
5428 *
5429 */
5430
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005431 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
5432 return 0;
5433 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
5434 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005435
5436 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005437
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005438 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005439 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005440 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005441 if (vcpu->cpu != freq->cpu)
5442 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10005443 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005444 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005445 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005446 }
5447 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005448 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005449
5450 if (freq->old < freq->new && send_ipi) {
5451 /*
5452 * We upscale the frequency. Must make the guest
5453 * doesn't see old kvmclock values while running with
5454 * the new frequency, otherwise we risk the guest sees
5455 * time go backwards.
5456 *
5457 * In case we update the frequency for another cpu
5458 * (which might be in guest context) send an interrupt
5459 * to kick the cpu out of guest context. Next time
5460 * guest context is entered kvmclock will be updated,
5461 * so the guest will not see stale values.
5462 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005463 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005464 }
5465 return 0;
5466}
5467
5468static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005469 .notifier_call = kvmclock_cpufreq_notifier
5470};
5471
5472static int kvmclock_cpu_notifier(struct notifier_block *nfb,
5473 unsigned long action, void *hcpu)
5474{
5475 unsigned int cpu = (unsigned long)hcpu;
5476
5477 switch (action) {
5478 case CPU_ONLINE:
5479 case CPU_DOWN_FAILED:
5480 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
5481 break;
5482 case CPU_DOWN_PREPARE:
5483 smp_call_function_single(cpu, tsc_bad, NULL, 1);
5484 break;
5485 }
5486 return NOTIFY_OK;
5487}
5488
5489static struct notifier_block kvmclock_cpu_notifier_block = {
5490 .notifier_call = kvmclock_cpu_notifier,
5491 .priority = -INT_MAX
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005492};
5493
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005494static void kvm_timer_init(void)
5495{
5496 int cpu;
5497
Zachary Amsdenc2855452010-09-18 14:38:15 -10005498 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305499
5500 cpu_notifier_register_begin();
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005501 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10005502#ifdef CONFIG_CPU_FREQ
5503 struct cpufreq_policy policy;
5504 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02005505 cpu = get_cpu();
5506 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10005507 if (policy.cpuinfo.max_freq)
5508 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02005509 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10005510#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005511 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
5512 CPUFREQ_TRANSITION_NOTIFIER);
5513 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10005514 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005515 for_each_online_cpu(cpu)
5516 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305517
5518 __register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
5519 cpu_notifier_register_done();
5520
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005521}
5522
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005523static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
5524
Gleb Natapovf5132b02011-11-10 14:57:22 +02005525int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005526{
Alex,Shi086c9852011-10-20 15:34:01 +08005527 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005528}
5529
5530static int kvm_is_user_mode(void)
5531{
5532 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005533
Alex,Shi086c9852011-10-20 15:34:01 +08005534 if (__this_cpu_read(current_vcpu))
5535 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005536
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005537 return user_mode != 0;
5538}
5539
5540static unsigned long kvm_get_guest_ip(void)
5541{
5542 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005543
Alex,Shi086c9852011-10-20 15:34:01 +08005544 if (__this_cpu_read(current_vcpu))
5545 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005546
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005547 return ip;
5548}
5549
5550static struct perf_guest_info_callbacks kvm_guest_cbs = {
5551 .is_in_guest = kvm_is_in_guest,
5552 .is_user_mode = kvm_is_user_mode,
5553 .get_guest_ip = kvm_get_guest_ip,
5554};
5555
5556void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
5557{
Alex,Shi086c9852011-10-20 15:34:01 +08005558 __this_cpu_write(current_vcpu, vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005559}
5560EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
5561
5562void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
5563{
Alex,Shi086c9852011-10-20 15:34:01 +08005564 __this_cpu_write(current_vcpu, NULL);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005565}
5566EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
5567
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005568static void kvm_set_mmio_spte_mask(void)
5569{
5570 u64 mask;
5571 int maxphyaddr = boot_cpu_data.x86_phys_bits;
5572
5573 /*
5574 * Set the reserved bits and the present bit of an paging-structure
5575 * entry to generate page fault with PFER.RSV = 1.
5576 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005577 /* Mask the reserved physical address bits. */
Tiejun Chend1431482014-09-01 18:44:04 +08005578 mask = rsvd_bits(maxphyaddr, 51);
Xiao Guangrong885032b2013-06-07 16:51:23 +08005579
5580 /* Bit 62 is always reserved for 32bit host. */
5581 mask |= 0x3ull << 62;
5582
5583 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005584 mask |= 1ull;
5585
5586#ifdef CONFIG_X86_64
5587 /*
5588 * If reserved bit is not supported, clear the present bit to disable
5589 * mmio page fault.
5590 */
5591 if (maxphyaddr == 52)
5592 mask &= ~1ull;
5593#endif
5594
5595 kvm_mmu_set_mmio_spte_mask(mask);
5596}
5597
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005598#ifdef CONFIG_X86_64
5599static void pvclock_gtod_update_fn(struct work_struct *work)
5600{
Marcelo Tosattid8281992012-11-27 23:29:01 -02005601 struct kvm *kvm;
5602
5603 struct kvm_vcpu *vcpu;
5604 int i;
5605
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005606 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005607 list_for_each_entry(kvm, &vm_list, vm_list)
5608 kvm_for_each_vcpu(i, vcpu, kvm)
5609 set_bit(KVM_REQ_MASTERCLOCK_UPDATE, &vcpu->requests);
5610 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005611 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005612}
5613
5614static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
5615
5616/*
5617 * Notification about pvclock gtod data update.
5618 */
5619static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
5620 void *priv)
5621{
5622 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
5623 struct timekeeper *tk = priv;
5624
5625 update_pvclock_gtod(tk);
5626
5627 /* disable master clock if host does not trust, or does not
5628 * use, TSC clocksource
5629 */
5630 if (gtod->clock.vclock_mode != VCLOCK_TSC &&
5631 atomic_read(&kvm_guest_has_master_clock) != 0)
5632 queue_work(system_long_wq, &pvclock_gtod_work);
5633
5634 return 0;
5635}
5636
5637static struct notifier_block pvclock_gtod_notifier = {
5638 .notifier_call = pvclock_gtod_notify,
5639};
5640#endif
5641
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005642int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02005643{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005644 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02005645 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005646
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005647 if (kvm_x86_ops) {
5648 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005649 r = -EEXIST;
5650 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005651 }
5652
5653 if (!ops->cpu_has_kvm_support()) {
5654 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005655 r = -EOPNOTSUPP;
5656 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005657 }
5658 if (ops->disabled_by_bios()) {
5659 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005660 r = -EOPNOTSUPP;
5661 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005662 }
5663
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005664 r = -ENOMEM;
5665 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
5666 if (!shared_msrs) {
5667 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
5668 goto out;
5669 }
5670
Avi Kivity97db56c2008-01-13 13:23:56 +02005671 r = kvm_mmu_module_init();
5672 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005673 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02005674
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005675 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02005676
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005677 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01005678 kvm_init_msr_list();
5679
Sheng Yang7b523452008-04-25 21:13:50 +08005680 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005681 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005682
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005683 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005684
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005685 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5686
Dexuan Cui2acf9232010-06-10 11:27:12 +08005687 if (cpu_has_xsave)
5688 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
5689
Gleb Natapovc5cc4212012-08-05 15:58:30 +03005690 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005691#ifdef CONFIG_X86_64
5692 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
5693#endif
5694
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005695 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005696
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005697out_free_percpu:
5698 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005699out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005700 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02005701}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005702
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005703void kvm_arch_exit(void)
5704{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005705 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5706
Jan Kiszka888d2562009-04-17 19:24:58 +02005707 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5708 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
5709 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005710 unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005711#ifdef CONFIG_X86_64
5712 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
5713#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005714 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005715 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005716 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005717}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005718
Hollis Blanchard8776e512007-10-31 17:24:24 -05005719int kvm_emulate_halt(struct kvm_vcpu *vcpu)
5720{
5721 ++vcpu->stat.halt_exits;
5722 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03005723 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005724 return 1;
5725 } else {
5726 vcpu->run->exit_reason = KVM_EXIT_HLT;
5727 return 0;
5728 }
5729}
5730EXPORT_SYMBOL_GPL(kvm_emulate_halt);
5731
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005732int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
5733{
5734 u64 param, ingpa, outgpa, ret;
5735 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
5736 bool fast, longmode;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005737
5738 /*
5739 * hypercall generates UD from non zero cpl and real mode
5740 * per HYPER-V spec
5741 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02005742 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005743 kvm_queue_exception(vcpu, UD_VECTOR);
5744 return 0;
5745 }
5746
Nadav Amita449c7a2014-06-18 17:19:24 +03005747 longmode = is_64_bit_mode(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005748
5749 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02005750 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
5751 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
5752 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
5753 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
5754 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
5755 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005756 }
5757#ifdef CONFIG_X86_64
5758 else {
5759 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
5760 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
5761 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
5762 }
5763#endif
5764
5765 code = param & 0xffff;
5766 fast = (param >> 16) & 0x1;
5767 rep_cnt = (param >> 32) & 0xfff;
5768 rep_idx = (param >> 48) & 0xfff;
5769
5770 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
5771
Gleb Natapovc25bc162010-01-17 15:51:24 +02005772 switch (code) {
5773 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
5774 kvm_vcpu_on_spin(vcpu);
5775 break;
5776 default:
5777 res = HV_STATUS_INVALID_HYPERCALL_CODE;
5778 break;
5779 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005780
5781 ret = res | (((u64)rep_done & 0xfff) << 32);
5782 if (longmode) {
5783 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
5784 } else {
5785 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
5786 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
5787 }
5788
5789 return 1;
5790}
5791
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305792/*
5793 * kvm_pv_kick_cpu_op: Kick a vcpu.
5794 *
5795 * @apicid - apicid of vcpu to be kicked.
5796 */
5797static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
5798{
Raghavendra K T24d21662013-08-26 14:18:35 +05305799 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305800
Raghavendra K T24d21662013-08-26 14:18:35 +05305801 lapic_irq.shorthand = 0;
5802 lapic_irq.dest_mode = 0;
5803 lapic_irq.dest_id = apicid;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305804
Raghavendra K T24d21662013-08-26 14:18:35 +05305805 lapic_irq.delivery_mode = APIC_DM_REMRD;
5806 kvm_irq_delivery_to_apic(kvm, 0, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305807}
5808
Hollis Blanchard8776e512007-10-31 17:24:24 -05005809int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
5810{
5811 unsigned long nr, a0, a1, a2, a3, ret;
Nadav Amita449c7a2014-06-18 17:19:24 +03005812 int op_64_bit, r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005813
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005814 if (kvm_hv_hypercall_enabled(vcpu->kvm))
5815 return kvm_hv_hypercall(vcpu);
5816
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005817 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
5818 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
5819 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
5820 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
5821 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005822
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005823 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005824
Nadav Amita449c7a2014-06-18 17:19:24 +03005825 op_64_bit = is_64_bit_mode(vcpu);
5826 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05005827 nr &= 0xFFFFFFFF;
5828 a0 &= 0xFFFFFFFF;
5829 a1 &= 0xFFFFFFFF;
5830 a2 &= 0xFFFFFFFF;
5831 a3 &= 0xFFFFFFFF;
5832 }
5833
Jan Kiszka07708c42009-08-03 18:43:28 +02005834 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
5835 ret = -KVM_EPERM;
5836 goto out;
5837 }
5838
Hollis Blanchard8776e512007-10-31 17:24:24 -05005839 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02005840 case KVM_HC_VAPIC_POLL_IRQ:
5841 ret = 0;
5842 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305843 case KVM_HC_KICK_CPU:
5844 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
5845 ret = 0;
5846 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005847 default:
5848 ret = -KVM_ENOSYS;
5849 break;
5850 }
Jan Kiszka07708c42009-08-03 18:43:28 +02005851out:
Nadav Amita449c7a2014-06-18 17:19:24 +03005852 if (!op_64_bit)
5853 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005854 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05305855 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05005856 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005857}
5858EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
5859
Jan Kiszkab6785de2012-09-20 07:43:17 +02005860static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005861{
Avi Kivityd6aa1002011-04-20 15:47:13 +03005862 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005863 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005864 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005865
Hollis Blanchard8776e512007-10-31 17:24:24 -05005866 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005867
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005868 return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005869}
5870
Hollis Blanchardd0752062007-10-31 17:24:25 -05005871/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005872 * Check if userspace requested an interrupt window, and that the
5873 * interrupt window is open.
5874 *
5875 * No need to exit to userspace if we already have an interrupt queued.
5876 */
Avi Kivity851ba692009-08-24 11:10:17 +03005877static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005878{
Gleb Natapov80618232009-04-21 17:44:56 +03005879 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005880 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03005881 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005882}
5883
Avi Kivity851ba692009-08-24 11:10:17 +03005884static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005885{
Avi Kivity851ba692009-08-24 11:10:17 +03005886 struct kvm_run *kvm_run = vcpu->run;
5887
Jan Kiszka91586a32009-10-05 13:07:21 +02005888 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005889 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005890 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01005891 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005892 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01005893 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005894 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03005895 kvm_arch_interrupt_allowed(vcpu) &&
5896 !kvm_cpu_has_interrupt(vcpu) &&
5897 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005898}
5899
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005900static void update_cr8_intercept(struct kvm_vcpu *vcpu)
5901{
5902 int max_irr, tpr;
5903
5904 if (!kvm_x86_ops->update_cr8_intercept)
5905 return;
5906
Avi Kivity88c808f2009-08-17 22:49:40 +03005907 if (!vcpu->arch.apic)
5908 return;
5909
Gleb Natapov8db3baa2009-05-11 13:35:54 +03005910 if (!vcpu->arch.apic->vapic_addr)
5911 max_irr = kvm_lapic_find_highest_irr(vcpu);
5912 else
5913 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005914
5915 if (max_irr != -1)
5916 max_irr >>= 4;
5917
5918 tpr = kvm_lapic_get_cr8(vcpu);
5919
5920 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
5921}
5922
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005923static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005924{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005925 int r;
5926
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005927 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005928 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d2010-03-11 13:01:59 +02005929 trace_kvm_inj_exception(vcpu->arch.exception.nr,
5930 vcpu->arch.exception.has_error_code,
5931 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03005932
5933 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
5934 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
5935 X86_EFLAGS_RF);
5936
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005937 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
5938 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005939 vcpu->arch.exception.error_code,
5940 vcpu->arch.exception.reinject);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005941 return 0;
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005942 }
5943
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005944 if (vcpu->arch.nmi_injected) {
5945 kvm_x86_ops->set_nmi(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005946 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005947 }
5948
5949 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005950 kvm_x86_ops->set_irq(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005951 return 0;
5952 }
5953
5954 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
5955 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
5956 if (r != 0)
5957 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005958 }
5959
5960 /* try to inject new event if pending */
5961 if (vcpu->arch.nmi_pending) {
5962 if (kvm_x86_ops->nmi_allowed(vcpu)) {
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005963 --vcpu->arch.nmi_pending;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005964 vcpu->arch.nmi_injected = true;
5965 kvm_x86_ops->set_nmi(vcpu);
5966 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08005967 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04005968 /*
5969 * Because interrupts can be injected asynchronously, we are
5970 * calling check_nested_events again here to avoid a race condition.
5971 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
5972 * proposal and current concerns. Perhaps we should be setting
5973 * KVM_REQ_EVENT only on certain events and not unconditionally?
5974 */
5975 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
5976 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
5977 if (r != 0)
5978 return r;
5979 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005980 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005981 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
5982 false);
5983 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005984 }
5985 }
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005986 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005987}
5988
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005989static void process_nmi(struct kvm_vcpu *vcpu)
5990{
5991 unsigned limit = 2;
5992
5993 /*
5994 * x86 is limited to one NMI running, and one NMI pending after it.
5995 * If an NMI is already in progress, limit further NMIs to just one.
5996 * Otherwise, allow two (and we'll inject the first one immediately).
5997 */
5998 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
5999 limit = 1;
6000
6001 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
6002 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
6003 kvm_make_request(KVM_REQ_EVENT, vcpu);
6004}
6005
Yang Zhang3d81bc72013-04-11 19:25:13 +08006006static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006007{
6008 u64 eoi_exit_bitmap[4];
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006009 u32 tmr[8];
Yang Zhangc7c9c562013-01-25 10:18:51 +08006010
Yang Zhang3d81bc72013-04-11 19:25:13 +08006011 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
6012 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006013
6014 memset(eoi_exit_bitmap, 0, 32);
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006015 memset(tmr, 0, 32);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006016
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006017 kvm_ioapic_scan_entry(vcpu, eoi_exit_bitmap, tmr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006018 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006019 kvm_apic_update_tmr(vcpu, tmr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006020}
6021
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006022/*
6023 * Returns 1 to let __vcpu_run() continue the guest execution loop without
6024 * exiting to the userspace. Otherwise, the value will be returned to the
6025 * userspace.
6026 */
Avi Kivity851ba692009-08-24 11:10:17 +03006027static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006028{
6029 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006030 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03006031 vcpu->run->request_interrupt_window;
Jan Kiszka730dca42013-04-28 10:50:52 +02006032 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006033
Avi Kivity3e007502010-06-23 14:26:18 +03006034 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03006035 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05006036 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006037 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03006038 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006039 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
6040 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03006041 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
6042 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10006043 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
6044 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006045 if (unlikely(r))
6046 goto out;
6047 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006048 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03006049 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006050 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03006051 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006052 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006053 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02006054 r = 0;
6055 goto out;
6056 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006057 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006058 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01006059 r = 0;
6060 goto out;
6061 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006062 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02006063 vcpu->fpu_active = 0;
6064 kvm_x86_ops->fpu_deactivate(vcpu);
6065 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006066 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
6067 /* Page is swapped out. Do synthetic halt */
6068 vcpu->arch.apf.halted = true;
6069 r = 1;
6070 goto out;
6071 }
Glauber Costac9aaa892011-07-11 15:28:14 -04006072 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
6073 record_steal_time(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006074 if (kvm_check_request(KVM_REQ_NMI, vcpu))
6075 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006076 if (kvm_check_request(KVM_REQ_PMU, vcpu))
6077 kvm_handle_pmu_event(vcpu);
6078 if (kvm_check_request(KVM_REQ_PMI, vcpu))
6079 kvm_deliver_pmi(vcpu);
Yang Zhang3d81bc72013-04-11 19:25:13 +08006080 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
6081 vcpu_scan_ioapic(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02006082 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006083
Avi Kivityb463a6f2010-07-20 15:06:17 +03006084 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Jan Kiszka66450a22013-03-13 12:42:34 +01006085 kvm_apic_accept_events(vcpu);
6086 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
6087 r = 1;
6088 goto out;
6089 }
6090
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006091 if (inject_pending_event(vcpu, req_int_win) != 0)
6092 req_immediate_exit = true;
Avi Kivity3842d132010-07-27 12:30:24 +03006093 /* enable NMI/IRQ window open exits if needed */
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006094 else if (vcpu->arch.nmi_pending)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006095 kvm_x86_ops->enable_nmi_window(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006096 else if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006097 kvm_x86_ops->enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006098
Avi Kivity3842d132010-07-27 12:30:24 +03006099 if (kvm_lapic_enabled(vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006100 /*
6101 * Update architecture specific hints for APIC
6102 * virtual interrupt delivery.
6103 */
6104 if (kvm_x86_ops->hwapic_irr_update)
6105 kvm_x86_ops->hwapic_irr_update(vcpu,
6106 kvm_lapic_find_highest_irr(vcpu));
Avi Kivity3842d132010-07-27 12:30:24 +03006107 update_cr8_intercept(vcpu);
6108 kvm_lapic_sync_to_vapic(vcpu);
6109 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006110 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006111
Avi Kivityd8368af2012-05-14 18:07:56 +03006112 r = kvm_mmu_reload(vcpu);
6113 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006114 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03006115 }
6116
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006117 preempt_disable();
6118
6119 kvm_x86_ops->prepare_guest_switch(vcpu);
6120 if (vcpu->fpu_active)
6121 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006122 kvm_load_guest_xcr0(vcpu);
6123
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006124 vcpu->mode = IN_GUEST_MODE;
6125
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006126 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6127
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006128 /* We should set ->mode before check ->requests,
6129 * see the comment in make_all_cpus_request.
6130 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006131 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006132
6133 local_irq_disable();
6134
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006135 if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006136 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006137 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006138 smp_wmb();
6139 local_irq_enable();
6140 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006141 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006142 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006143 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006144 }
6145
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006146 if (req_immediate_exit)
6147 smp_send_reschedule(vcpu->cpu);
6148
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006149 kvm_guest_enter();
6150
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006151 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006152 set_debugreg(0, 7);
6153 set_debugreg(vcpu->arch.eff_db[0], 0);
6154 set_debugreg(vcpu->arch.eff_db[1], 1);
6155 set_debugreg(vcpu->arch.eff_db[2], 2);
6156 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006157 set_debugreg(vcpu->arch.dr6, 6);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006158 }
6159
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006160 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03006161 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006162
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006163 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006164 * Do this here before restoring debug registers on the host. And
6165 * since we do this before handling the vmexit, a DR access vmexit
6166 * can (a) read the correct value of the debug registers, (b) set
6167 * KVM_DEBUGREG_WONT_EXIT again.
6168 */
6169 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
6170 int i;
6171
6172 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
6173 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
6174 for (i = 0; i < KVM_NR_DB_REGS; i++)
6175 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
6176 }
6177
6178 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006179 * If the guest has used debug registers, at least dr7
6180 * will be disabled while returning to the host.
6181 * If we don't have active breakpoints in the host, we don't
6182 * care about the messed up debug address registers. But if
6183 * we have some of them active, restore the old state.
6184 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01006185 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006186 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006187
Marcelo Tosatti886b4702012-11-27 23:28:58 -02006188 vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu,
6189 native_read_tsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10006190
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006191 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03006192 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006193
6194 /* Interrupt is enabled by handle_external_intr() */
6195 kvm_x86_ops->handle_external_intr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006196
6197 ++vcpu->stat.exits;
6198
6199 /*
6200 * We must have an instruction between local_irq_enable() and
6201 * kvm_guest_exit(), so the timer interrupt isn't delayed by
6202 * the interrupt shadow. The stat.exits increment will do nicely.
6203 * But we need to prevent reordering, hence this barrier():
6204 */
6205 barrier();
6206
6207 kvm_guest_exit();
6208
6209 preempt_enable();
6210
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006211 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006212
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006213 /*
6214 * Profile KVM exit RIPs:
6215 */
6216 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006217 unsigned long rip = kvm_rip_read(vcpu);
6218 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006219 }
6220
Zachary Amsdencc578282012-02-03 15:43:50 -02006221 if (unlikely(vcpu->arch.tsc_always_catchup))
6222 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02006223
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03006224 if (vcpu->arch.apic_attention)
6225 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02006226
Avi Kivity851ba692009-08-24 11:10:17 +03006227 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006228 return r;
6229
6230cancel_injection:
6231 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03006232 if (unlikely(vcpu->arch.apic_attention))
6233 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006234out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03006235 return r;
6236}
6237
Gleb Natapov09cec752009-03-23 15:11:44 +02006238
Avi Kivity851ba692009-08-24 11:10:17 +03006239static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03006240{
6241 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006242 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006243
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006244 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006245
6246 r = 1;
6247 while (r > 0) {
Gleb Natapovaf585b92010-10-14 11:22:46 +02006248 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
6249 !vcpu->arch.apf.halted)
Avi Kivity851ba692009-08-24 11:10:17 +03006250 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006251 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006252 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006253 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006254 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006255 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
6256 kvm_apic_accept_events(vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02006257 switch(vcpu->arch.mp_state) {
6258 case KVM_MP_STATE_HALTED:
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306259 vcpu->arch.pv.pv_unhalted = false;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006260 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02006261 KVM_MP_STATE_RUNNABLE;
6262 case KVM_MP_STATE_RUNNABLE:
Gleb Natapovaf585b92010-10-14 11:22:46 +02006263 vcpu->arch.apf.halted = false;
Gleb Natapov09cec752009-03-23 15:11:44 +02006264 break;
Jan Kiszka66450a22013-03-13 12:42:34 +01006265 case KVM_MP_STATE_INIT_RECEIVED:
6266 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006267 default:
6268 r = -EINTR;
6269 break;
6270 }
6271 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03006272 }
6273
Gleb Natapov09cec752009-03-23 15:11:44 +02006274 if (r <= 0)
6275 break;
6276
6277 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
6278 if (kvm_cpu_has_pending_timer(vcpu))
6279 kvm_inject_pending_timer_irqs(vcpu);
6280
Avi Kivity851ba692009-08-24 11:10:17 +03006281 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02006282 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006283 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006284 ++vcpu->stat.request_irq_exits;
6285 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006286
6287 kvm_check_async_pf_completion(vcpu);
6288
Gleb Natapov09cec752009-03-23 15:11:44 +02006289 if (signal_pending(current)) {
6290 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006291 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006292 ++vcpu->stat.signal_exits;
6293 }
6294 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006295 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09006296 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006297 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006298 }
6299 }
6300
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006301 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006302
6303 return r;
6304}
6305
Gleb Natapov716d51a2012-09-03 15:24:26 +03006306static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
6307{
6308 int r;
6309 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6310 r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
6311 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6312 if (r != EMULATE_DONE)
6313 return 0;
6314 return 1;
6315}
6316
6317static int complete_emulated_pio(struct kvm_vcpu *vcpu)
6318{
6319 BUG_ON(!vcpu->arch.pio.count);
6320
6321 return complete_emulated_io(vcpu);
6322}
6323
Avi Kivityf78146b2012-04-18 19:22:47 +03006324/*
6325 * Implements the following, as a state machine:
6326 *
6327 * read:
6328 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006329 * for each mmio piece in the fragment
6330 * write gpa, len
6331 * exit
6332 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03006333 * execute insn
6334 *
6335 * write:
6336 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006337 * for each mmio piece in the fragment
6338 * write gpa, len
6339 * copy data
6340 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03006341 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03006342static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02006343{
6344 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03006345 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006346 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02006347
Gleb Natapov716d51a2012-09-03 15:24:26 +03006348 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006349
Gleb Natapov716d51a2012-09-03 15:24:26 +03006350 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006351 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
6352 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006353 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006354 memcpy(frag->data, run->mmio.data, len);
6355
6356 if (frag->len <= 8) {
6357 /* Switch to the next fragment. */
6358 frag++;
6359 vcpu->mmio_cur_fragment++;
6360 } else {
6361 /* Go forward to the next mmio piece. */
6362 frag->data += len;
6363 frag->gpa += len;
6364 frag->len -= len;
6365 }
6366
Andrew Honiga08d3b32014-02-27 19:35:14 +01006367 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03006368 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006369
6370 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02006371 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03006372 return 1;
6373 vcpu->mmio_read_completed = 1;
6374 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02006375 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006376
Gleb Natapov716d51a2012-09-03 15:24:26 +03006377 run->exit_reason = KVM_EXIT_MMIO;
6378 run->mmio.phys_addr = frag->gpa;
6379 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006380 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
6381 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006382 run->mmio.is_write = vcpu->mmio_is_write;
6383 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
6384 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02006385}
6386
Gleb Natapov716d51a2012-09-03 15:24:26 +03006387
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006388int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6389{
6390 int r;
6391 sigset_t sigsaved;
6392
Avi Kivitye5c30142011-01-11 12:15:54 +02006393 if (!tsk_used_math(current) && init_fpu(current))
6394 return -ENOMEM;
6395
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006396 if (vcpu->sigset_active)
6397 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
6398
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006399 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
6400 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006401 kvm_apic_accept_events(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006402 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006403 r = -EAGAIN;
6404 goto out;
6405 }
6406
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006407 /* re-sync apic's tpr */
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006408 if (!irqchip_in_kernel(vcpu->kvm)) {
6409 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
6410 r = -EINVAL;
6411 goto out;
6412 }
6413 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006414
Gleb Natapov716d51a2012-09-03 15:24:26 +03006415 if (unlikely(vcpu->arch.complete_userspace_io)) {
6416 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
6417 vcpu->arch.complete_userspace_io = NULL;
6418 r = cui(vcpu);
6419 if (r <= 0)
6420 goto out;
6421 } else
6422 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006423
Avi Kivity851ba692009-08-24 11:10:17 +03006424 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006425
6426out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03006427 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006428 if (vcpu->sigset_active)
6429 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
6430
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006431 return r;
6432}
6433
6434int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6435{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006436 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
6437 /*
6438 * We are here if userspace calls get_regs() in the middle of
6439 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08006440 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006441 * that usually, but some bad designed PV devices (vmware
6442 * backdoor interface) need this to work
6443 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03006444 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006445 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6446 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006447 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
6448 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
6449 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
6450 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
6451 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
6452 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
6453 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6454 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006455#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006456 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
6457 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
6458 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
6459 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
6460 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
6461 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
6462 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
6463 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006464#endif
6465
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006466 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02006467 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006468
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006469 return 0;
6470}
6471
6472int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6473{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006474 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
6475 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6476
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006477 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
6478 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
6479 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
6480 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
6481 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
6482 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
6483 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
6484 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006485#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006486 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
6487 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
6488 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
6489 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
6490 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
6491 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
6492 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
6493 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006494#endif
6495
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006496 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02006497 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006498
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02006499 vcpu->arch.exception.pending = false;
6500
Avi Kivity3842d132010-07-27 12:30:24 +03006501 kvm_make_request(KVM_REQ_EVENT, vcpu);
6502
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006503 return 0;
6504}
6505
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006506void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
6507{
6508 struct kvm_segment cs;
6509
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006510 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006511 *db = cs.db;
6512 *l = cs.l;
6513}
6514EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
6515
6516int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
6517 struct kvm_sregs *sregs)
6518{
Gleb Natapov89a27f42010-02-16 10:51:48 +02006519 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006520
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006521 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6522 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6523 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6524 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6525 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6526 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006527
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006528 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6529 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006530
6531 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006532 sregs->idt.limit = dt.size;
6533 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006534 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006535 sregs->gdt.limit = dt.size;
6536 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006537
Avi Kivity4d4ec082009-12-29 18:07:30 +02006538 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006539 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006540 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02006541 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006542 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02006543 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006544 sregs->apic_base = kvm_get_apic_base(vcpu);
6545
Gleb Natapov923c61b2009-05-11 13:35:48 +03006546 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006547
Gleb Natapov36752c92009-05-11 13:35:53 +03006548 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03006549 set_bit(vcpu->arch.interrupt.nr,
6550 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03006551
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006552 return 0;
6553}
6554
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006555int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
6556 struct kvm_mp_state *mp_state)
6557{
Jan Kiszka66450a22013-03-13 12:42:34 +01006558 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306559 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
6560 vcpu->arch.pv.pv_unhalted)
6561 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
6562 else
6563 mp_state->mp_state = vcpu->arch.mp_state;
6564
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006565 return 0;
6566}
6567
6568int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
6569 struct kvm_mp_state *mp_state)
6570{
Jan Kiszka66450a22013-03-13 12:42:34 +01006571 if (!kvm_vcpu_has_lapic(vcpu) &&
6572 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
6573 return -EINVAL;
6574
6575 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
6576 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
6577 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
6578 } else
6579 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03006580 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006581 return 0;
6582}
6583
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006584int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
6585 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006586{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006587 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006588 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006589
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006590 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006591
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006592 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006593 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02006594
Gleb Natapovc6975182010-02-18 12:15:01 +02006595 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03006596 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02006597
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006598 kvm_rip_write(vcpu, ctxt->eip);
6599 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03006600 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03006601 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02006602}
6603EXPORT_SYMBOL_GPL(kvm_task_switch);
6604
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006605int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
6606 struct kvm_sregs *sregs)
6607{
Jan Kiszka58cb6282014-01-24 16:48:44 +01006608 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006609 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006610 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02006611 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006612
Petr Matousek6d1068b2012-11-06 19:24:07 +01006613 if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
6614 return -EINVAL;
6615
Gleb Natapov89a27f42010-02-16 10:51:48 +02006616 dt.size = sregs->idt.limit;
6617 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006618 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006619 dt.size = sregs->gdt.limit;
6620 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006621 kvm_x86_ops->set_gdt(vcpu, &dt);
6622
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006623 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006624 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02006625 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02006626 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006627
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006628 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006629
Avi Kivityf6801df2010-01-21 15:31:50 +02006630 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006631 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006632 apic_base_msr.data = sregs->apic_base;
6633 apic_base_msr.host_initiated = true;
6634 kvm_set_apic_base(vcpu, &apic_base_msr);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006635
Avi Kivity4d4ec082009-12-29 18:07:30 +02006636 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006637 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00006638 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006639
Avi Kivityfc78f512009-12-07 12:16:48 +02006640 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006641 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Sheng Yang3ea3aa82010-12-08 10:49:43 +08006642 if (sregs->cr4 & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +02006643 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006644
6645 idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02006646 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02006647 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02006648 mmu_reset_needed = 1;
6649 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006650 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006651
6652 if (mmu_reset_needed)
6653 kvm_mmu_reset_context(vcpu);
6654
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03006655 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03006656 pending_vec = find_first_bit(
6657 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
6658 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006659 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03006660 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006661 }
6662
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006663 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6664 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6665 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6666 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6667 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6668 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006669
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006670 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6671 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006672
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03006673 update_cr8_intercept(vcpu);
6674
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006675 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03006676 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006677 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02006678 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006679 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
6680
Avi Kivity3842d132010-07-27 12:30:24 +03006681 kvm_make_request(KVM_REQ_EVENT, vcpu);
6682
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006683 return 0;
6684}
6685
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006686int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
6687 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006688{
Jan Kiszka355be0b2009-10-03 00:31:21 +02006689 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006690 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006691
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006692 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
6693 r = -EBUSY;
6694 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03006695 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006696 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
6697 kvm_queue_exception(vcpu, DB_VECTOR);
6698 else
6699 kvm_queue_exception(vcpu, BP_VECTOR);
6700 }
6701
Jan Kiszka91586a32009-10-05 13:07:21 +02006702 /*
6703 * Read rflags as long as potentially injected trace flags are still
6704 * filtered out.
6705 */
6706 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02006707
6708 vcpu->guest_debug = dbg->control;
6709 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
6710 vcpu->guest_debug = 0;
6711
6712 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006713 for (i = 0; i < KVM_NR_DB_REGS; ++i)
6714 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02006715 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006716 } else {
6717 for (i = 0; i < KVM_NR_DB_REGS; i++)
6718 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006719 }
Jan Kiszkac8639012012-09-21 05:42:55 +02006720 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006721
Jan Kiszkaf92653e2010-02-23 17:47:55 +01006722 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6723 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
6724 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006725
Jan Kiszka91586a32009-10-05 13:07:21 +02006726 /*
6727 * Trigger an rflags update that will inject or remove the trace
6728 * flags.
6729 */
6730 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006731
Jan Kiszkac8639012012-09-21 05:42:55 +02006732 kvm_x86_ops->update_db_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02006733
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006734 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006735
Avi Kivity2122ff52010-05-13 11:25:04 +03006736out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006737
6738 return r;
6739}
6740
6741/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08006742 * Translate a guest virtual address to a guest physical address.
6743 */
6744int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
6745 struct kvm_translation *tr)
6746{
6747 unsigned long vaddr = tr->linear_address;
6748 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006749 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08006750
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006751 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02006752 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006753 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08006754 tr->physical_address = gpa;
6755 tr->valid = gpa != UNMAPPED_GVA;
6756 tr->writeable = 1;
6757 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08006758
6759 return 0;
6760}
6761
Hollis Blanchardd0752062007-10-31 17:24:25 -05006762int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
6763{
Sheng Yang98918832010-05-17 17:08:28 +08006764 struct i387_fxsave_struct *fxsave =
6765 &vcpu->arch.guest_fpu.state->fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05006766
Hollis Blanchardd0752062007-10-31 17:24:25 -05006767 memcpy(fpu->fpr, fxsave->st_space, 128);
6768 fpu->fcw = fxsave->cwd;
6769 fpu->fsw = fxsave->swd;
6770 fpu->ftwx = fxsave->twd;
6771 fpu->last_opcode = fxsave->fop;
6772 fpu->last_ip = fxsave->rip;
6773 fpu->last_dp = fxsave->rdp;
6774 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
6775
Hollis Blanchardd0752062007-10-31 17:24:25 -05006776 return 0;
6777}
6778
6779int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
6780{
Sheng Yang98918832010-05-17 17:08:28 +08006781 struct i387_fxsave_struct *fxsave =
6782 &vcpu->arch.guest_fpu.state->fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05006783
Hollis Blanchardd0752062007-10-31 17:24:25 -05006784 memcpy(fxsave->st_space, fpu->fpr, 128);
6785 fxsave->cwd = fpu->fcw;
6786 fxsave->swd = fpu->fsw;
6787 fxsave->twd = fpu->ftwx;
6788 fxsave->fop = fpu->last_opcode;
6789 fxsave->rip = fpu->last_ip;
6790 fxsave->rdp = fpu->last_dp;
6791 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
6792
Hollis Blanchardd0752062007-10-31 17:24:25 -05006793 return 0;
6794}
6795
Jan Kiszka10ab25c2010-05-25 16:01:50 +02006796int fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05006797{
Jan Kiszka10ab25c2010-05-25 16:01:50 +02006798 int err;
6799
6800 err = fpu_alloc(&vcpu->arch.guest_fpu);
6801 if (err)
6802 return err;
6803
Sheng Yang98918832010-05-17 17:08:28 +08006804 fpu_finit(&vcpu->arch.guest_fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006805
Dexuan Cui2acf9232010-06-10 11:27:12 +08006806 /*
6807 * Ensure guest xcr0 is valid for loading
6808 */
6809 vcpu->arch.xcr0 = XSTATE_FP;
6810
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006811 vcpu->arch.cr0 |= X86_CR0_ET;
Jan Kiszka10ab25c2010-05-25 16:01:50 +02006812
6813 return 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05006814}
6815EXPORT_SYMBOL_GPL(fx_init);
6816
Sheng Yang98918832010-05-17 17:08:28 +08006817static void fx_free(struct kvm_vcpu *vcpu)
6818{
6819 fpu_free(&vcpu->arch.guest_fpu);
6820}
6821
Hollis Blanchardd0752062007-10-31 17:24:25 -05006822void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
6823{
Avi Kivity2608d7a2010-01-21 15:31:45 +02006824 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05006825 return;
6826
Dexuan Cui2acf9232010-06-10 11:27:12 +08006827 /*
6828 * Restore all possible states in the guest,
6829 * and assume host would use all available bits.
6830 * Guest xcr0 would be loaded later.
6831 */
6832 kvm_put_guest_xcr0(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006833 vcpu->guest_fpu_loaded = 1;
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07006834 __kernel_fpu_begin();
Sheng Yang98918832010-05-17 17:08:28 +08006835 fpu_restore_checking(&vcpu->arch.guest_fpu);
Avi Kivity0c048512010-01-21 15:31:52 +02006836 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006837}
Hollis Blanchardd0752062007-10-31 17:24:25 -05006838
6839void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
6840{
Dexuan Cui2acf9232010-06-10 11:27:12 +08006841 kvm_put_guest_xcr0(vcpu);
6842
Hollis Blanchardd0752062007-10-31 17:24:25 -05006843 if (!vcpu->guest_fpu_loaded)
6844 return;
6845
6846 vcpu->guest_fpu_loaded = 0;
Sheng Yang98918832010-05-17 17:08:28 +08006847 fpu_save_init(&vcpu->arch.guest_fpu);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07006848 __kernel_fpu_end();
Avi Kivityf096ed82007-11-18 13:54:33 +02006849 ++vcpu->stat.fpu_reload;
Avi Kivitya8eeb042010-05-10 12:34:53 +03006850 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
Avi Kivity0c048512010-01-21 15:31:52 +02006851 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006852}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006853
6854void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
6855{
Glauber Costa12f9a482011-02-01 14:16:40 -05006856 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01006857
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006858 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Sheng Yang98918832010-05-17 17:08:28 +08006859 fx_free(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006860 kvm_x86_ops->vcpu_free(vcpu);
6861}
6862
6863struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
6864 unsigned int id)
6865{
Zachary Amsden6755bae2010-08-19 22:07:22 -10006866 if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
6867 printk_once(KERN_WARNING
6868 "kvm: SMP vm created on host with unstable TSC; "
6869 "guest TSC will not be reliable\n");
Avi Kivity26e52152007-11-20 15:30:24 +02006870 return kvm_x86_ops->vcpu_create(kvm, id);
6871}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006872
Avi Kivity26e52152007-11-20 15:30:24 +02006873int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
6874{
6875 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006876
Sheng Yang0bed3b52008-10-09 16:01:54 +08006877 vcpu->arch.mtrr_state.have_fixed = 1;
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03006878 r = vcpu_load(vcpu);
6879 if (r)
6880 return r;
Jan Kiszka57f252f2013-03-12 10:20:24 +01006881 kvm_vcpu_reset(vcpu);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02006882 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006883 vcpu_put(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006884
Avi Kivity26e52152007-11-20 15:30:24 +02006885 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006886}
6887
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006888int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
6889{
6890 int r;
Will Auld8fe8ab42012-11-29 12:42:12 -08006891 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01006892 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006893
6894 r = vcpu_load(vcpu);
6895 if (r)
6896 return r;
Will Auld8fe8ab42012-11-29 12:42:12 -08006897 msr.data = 0x0;
6898 msr.index = MSR_IA32_TSC;
6899 msr.host_initiated = true;
6900 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006901 vcpu_put(vcpu);
6902
Andrew Jones332967a2014-02-28 12:52:55 +01006903 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
6904 KVMCLOCK_SYNC_PERIOD);
6905
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006906 return r;
6907}
6908
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06006909void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006910{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03006911 int r;
Gleb Natapov344d9582010-10-14 11:22:50 +02006912 vcpu->arch.apf.msr_val = 0;
6913
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03006914 r = vcpu_load(vcpu);
6915 BUG_ON(r);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006916 kvm_mmu_unload(vcpu);
6917 vcpu_put(vcpu);
6918
Sheng Yang98918832010-05-17 17:08:28 +08006919 fx_free(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006920 kvm_x86_ops->vcpu_free(vcpu);
6921}
6922
Jan Kiszka66450a22013-03-13 12:42:34 +01006923void kvm_vcpu_reset(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006924{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006925 atomic_set(&vcpu->arch.nmi_queued, 0);
6926 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02006927 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03006928 kvm_clear_interrupt_queue(vcpu);
6929 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02006930
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006931 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amit6f43ed02014-07-15 17:37:46 +03006932 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006933 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006934 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02006935 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006936
Avi Kivity3842d132010-07-27 12:30:24 +03006937 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02006938 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04006939 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03006940
Glauber Costa12f9a482011-02-01 14:16:40 -05006941 kvmclock_reset(vcpu);
6942
Gleb Natapovaf585b92010-10-14 11:22:46 +02006943 kvm_clear_async_pf_completion_queue(vcpu);
6944 kvm_async_pf_hash_reset(vcpu);
6945 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006946
Gleb Natapovf5132b02011-11-10 14:57:22 +02006947 kvm_pmu_reset(vcpu);
6948
Julian Stecklina66f7b722012-12-05 15:26:19 +01006949 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
6950 vcpu->arch.regs_avail = ~0;
6951 vcpu->arch.regs_dirty = ~0;
6952
Jan Kiszka57f252f2013-03-12 10:20:24 +01006953 kvm_x86_ops->vcpu_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006954}
6955
Jan Kiszka66450a22013-03-13 12:42:34 +01006956void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, unsigned int vector)
6957{
6958 struct kvm_segment cs;
6959
6960 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
6961 cs.selector = vector << 8;
6962 cs.base = vector << 12;
6963 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
6964 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006965}
6966
Radim Krčmář13a34e02014-08-28 15:13:03 +02006967int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006968{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10006969 struct kvm *kvm;
6970 struct kvm_vcpu *vcpu;
6971 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006972 int ret;
6973 u64 local_tsc;
6974 u64 max_tsc = 0;
6975 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03006976
6977 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02006978 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006979 if (ret != 0)
6980 return ret;
6981
6982 local_tsc = native_read_tsc();
6983 stable = !check_tsc_unstable();
6984 list_for_each_entry(kvm, &vm_list, vm_list) {
6985 kvm_for_each_vcpu(i, vcpu, kvm) {
6986 if (!stable && vcpu->cpu == smp_processor_id())
6987 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
6988 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
6989 backwards_tsc = true;
6990 if (vcpu->arch.last_host_tsc > max_tsc)
6991 max_tsc = vcpu->arch.last_host_tsc;
6992 }
6993 }
6994 }
6995
6996 /*
6997 * Sometimes, even reliable TSCs go backwards. This happens on
6998 * platforms that reset TSC during suspend or hibernate actions, but
6999 * maintain synchronization. We must compensate. Fortunately, we can
7000 * detect that condition here, which happens early in CPU bringup,
7001 * before any KVM threads can be running. Unfortunately, we can't
7002 * bring the TSCs fully up to date with real time, as we aren't yet far
7003 * enough into CPU bringup that we know how much real time has actually
7004 * elapsed; our helper function, get_kernel_ns() will be using boot
7005 * variables that haven't been updated yet.
7006 *
7007 * So we simply find the maximum observed TSC above, then record the
7008 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
7009 * the adjustment will be applied. Note that we accumulate
7010 * adjustments, in case multiple suspend cycles happen before some VCPU
7011 * gets a chance to run again. In the event that no KVM threads get a
7012 * chance to run, we will miss the entire elapsed period, as we'll have
7013 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
7014 * loose cycle time. This isn't too big a deal, since the loss will be
7015 * uniform across all VCPUs (not to mention the scenario is extremely
7016 * unlikely). It is possible that a second hibernate recovery happens
7017 * much faster than a first, causing the observed TSC here to be
7018 * smaller; this would require additional padding adjustment, which is
7019 * why we set last_host_tsc to the local tsc observed here.
7020 *
7021 * N.B. - this code below runs only on platforms with reliable TSC,
7022 * as that is the only way backwards_tsc is set above. Also note
7023 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
7024 * have the same delta_cyc adjustment applied if backwards_tsc
7025 * is detected. Note further, this adjustment is only done once,
7026 * as we reset last_host_tsc on all VCPUs to stop this from being
7027 * called multiple times (one for each physical CPU bringup).
7028 *
Guo Chao4a969982012-06-28 15:17:27 +08007029 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007030 * will be compensated by the logic in vcpu_load, which sets the TSC to
7031 * catchup mode. This will catchup all VCPUs to real time, but cannot
7032 * guarantee that they stay in perfect synchronization.
7033 */
7034 if (backwards_tsc) {
7035 u64 delta_cyc = max_tsc - local_tsc;
Marcelo Tosatti16a96022014-05-14 12:43:24 -03007036 backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007037 list_for_each_entry(kvm, &vm_list, vm_list) {
7038 kvm_for_each_vcpu(i, vcpu, kvm) {
7039 vcpu->arch.tsc_offset_adjustment += delta_cyc;
7040 vcpu->arch.last_host_tsc = local_tsc;
Marcelo Tosattid8281992012-11-27 23:29:01 -02007041 set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
7042 &vcpu->requests);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007043 }
7044
7045 /*
7046 * We have to disable TSC offset matching.. if you were
7047 * booting a VM while issuing an S4 host suspend....
7048 * you may have some problem. Solving this issue is
7049 * left as an exercise to the reader.
7050 */
7051 kvm->arch.last_tsc_nsec = 0;
7052 kvm->arch.last_tsc_write = 0;
7053 }
7054
7055 }
7056 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007057}
7058
Radim Krčmář13a34e02014-08-28 15:13:03 +02007059void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007060{
Radim Krčmář13a34e02014-08-28 15:13:03 +02007061 kvm_x86_ops->hardware_disable();
7062 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007063}
7064
7065int kvm_arch_hardware_setup(void)
7066{
7067 return kvm_x86_ops->hardware_setup();
7068}
7069
7070void kvm_arch_hardware_unsetup(void)
7071{
7072 kvm_x86_ops->hardware_unsetup();
7073}
7074
7075void kvm_arch_check_processor_compat(void *rtn)
7076{
7077 kvm_x86_ops->check_processor_compatibility(rtn);
7078}
7079
Avi Kivity3e515702012-03-05 14:23:29 +02007080bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
7081{
7082 return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
7083}
7084
Gleb Natapov54e98182012-08-05 15:58:32 +03007085struct static_key kvm_no_apic_vcpu __read_mostly;
7086
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007087int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
7088{
7089 struct page *page;
7090 struct kvm *kvm;
7091 int r;
7092
7093 BUG_ON(vcpu->kvm == NULL);
7094 kvm = vcpu->kvm;
7095
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307096 vcpu->arch.pv.pv_unhalted = false;
Avi Kivity9aabc88f2010-07-29 15:11:50 +03007097 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03007098 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03007099 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007100 else
Avi Kivitya4535292008-04-13 17:54:35 +03007101 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007102
7103 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
7104 if (!page) {
7105 r = -ENOMEM;
7106 goto fail;
7107 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007108 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007109
Zachary Amsdencc578282012-02-03 15:43:50 -02007110 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007111
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007112 r = kvm_mmu_create(vcpu);
7113 if (r < 0)
7114 goto fail_free_pio_data;
7115
7116 if (irqchip_in_kernel(kvm)) {
7117 r = kvm_create_lapic(vcpu);
7118 if (r < 0)
7119 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03007120 } else
7121 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007122
Huang Ying890ca9a2009-05-11 16:48:15 +08007123 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
7124 GFP_KERNEL);
7125 if (!vcpu->arch.mce_banks) {
7126 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08007127 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08007128 }
7129 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
7130
Wei Yongjunf1797352013-04-18 07:41:00 +08007131 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
7132 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007133 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08007134 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007135
Julian Stecklina66f7b722012-12-05 15:26:19 +01007136 r = fx_init(vcpu);
7137 if (r)
7138 goto fail_free_wbinvd_dirty_mask;
7139
Will Auldba904632012-11-29 12:42:50 -08007140 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
Andy Honig0b794592013-02-20 14:48:10 -08007141 vcpu->arch.pv_time_enabled = false;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007142
7143 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +02007144 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007145
Gleb Natapovaf585b92010-10-14 11:22:46 +02007146 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007147 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007148
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007149 return 0;
Julian Stecklina66f7b722012-12-05 15:26:19 +01007150fail_free_wbinvd_dirty_mask:
7151 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007152fail_free_mce_banks:
7153 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08007154fail_free_lapic:
7155 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007156fail_mmu_destroy:
7157 kvm_mmu_destroy(vcpu);
7158fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007159 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007160fail:
7161 return r;
7162}
7163
7164void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
7165{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007166 int idx;
7167
Gleb Natapovf5132b02011-11-10 14:57:22 +02007168 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08007169 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007170 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007171 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007172 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007173 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007174 free_page((unsigned long)vcpu->arch.pio_data);
Gleb Natapov54e98182012-08-05 15:58:32 +03007175 if (!irqchip_in_kernel(vcpu->kvm))
7176 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007177}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007178
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007179void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
7180{
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007181 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007182}
7183
Carsten Ottee08b9632012-01-04 10:25:20 +01007184int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007185{
Carsten Ottee08b9632012-01-04 10:25:20 +01007186 if (type)
7187 return -EINVAL;
7188
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08007189 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08007190 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007191 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007192 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007193
Sheng Yang5550af42008-10-15 20:15:06 +08007194 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
7195 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06007196 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
7197 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
7198 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08007199
Jan Kiszka038f8c12011-02-04 10:49:11 +01007200 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007201 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007202 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
7203
7204 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01007205
Andrew Jones7e44e442014-02-28 12:52:54 +01007206 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01007207 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01007208
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01007209 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007210}
7211
7212static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
7213{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007214 int r;
7215 r = vcpu_load(vcpu);
7216 BUG_ON(r);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007217 kvm_mmu_unload(vcpu);
7218 vcpu_put(vcpu);
7219}
7220
7221static void kvm_free_vcpus(struct kvm *kvm)
7222{
7223 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007224 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007225
7226 /*
7227 * Unpin any mmu pages first.
7228 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02007229 kvm_for_each_vcpu(i, vcpu, kvm) {
7230 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007231 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007232 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007233 kvm_for_each_vcpu(i, vcpu, kvm)
7234 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007235
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007236 mutex_lock(&kvm->lock);
7237 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
7238 kvm->vcpus[i] = NULL;
7239
7240 atomic_set(&kvm->online_vcpus, 0);
7241 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007242}
7243
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007244void kvm_arch_sync_events(struct kvm *kvm)
7245{
Andrew Jones332967a2014-02-28 12:52:55 +01007246 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01007247 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Sheng Yangba4cef32009-01-06 10:03:03 +08007248 kvm_free_all_assigned_devices(kvm);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08007249 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007250}
7251
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007252void kvm_arch_destroy_vm(struct kvm *kvm)
7253{
Andrew Honig27469d22013-04-18 09:38:14 -07007254 if (current->mm == kvm->mm) {
7255 /*
7256 * Free memory regions allocated on behalf of userspace,
7257 * unless the the memory map has changed due to process exit
7258 * or fd copying.
7259 */
7260 struct kvm_userspace_memory_region mem;
7261 memset(&mem, 0, sizeof(mem));
7262 mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
7263 kvm_set_memory_region(kvm, &mem);
7264
7265 mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
7266 kvm_set_memory_region(kvm, &mem);
7267
7268 mem.slot = TSS_PRIVATE_MEMSLOT;
7269 kvm_set_memory_region(kvm, &mem);
7270 }
Sheng Yang6eb55812008-10-31 12:37:41 +08007271 kvm_iommu_unmap_guest(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08007272 kfree(kvm->arch.vpic);
7273 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007274 kvm_free_vcpus(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02007275 if (kvm->arch.apic_access_page)
7276 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007277 if (kvm->arch.ept_identity_pagetable)
7278 put_page(kvm->arch.ept_identity_pagetable);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007279 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007280}
Zhang Xiantao0de10342007-11-20 16:25:04 +08007281
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307282void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007283 struct kvm_memory_slot *dont)
7284{
7285 int i;
7286
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007287 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7288 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
7289 kvm_kvfree(free->arch.rmap[i]);
7290 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007291 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007292 if (i == 0)
7293 continue;
7294
7295 if (!dont || free->arch.lpage_info[i - 1] !=
7296 dont->arch.lpage_info[i - 1]) {
7297 kvm_kvfree(free->arch.lpage_info[i - 1]);
7298 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007299 }
7300 }
7301}
7302
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307303int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7304 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007305{
7306 int i;
7307
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007308 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007309 unsigned long ugfn;
7310 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007311 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007312
7313 lpages = gfn_to_index(slot->base_gfn + npages - 1,
7314 slot->base_gfn, level) + 1;
7315
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007316 slot->arch.rmap[i] =
7317 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
7318 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007319 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007320 if (i == 0)
7321 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007322
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007323 slot->arch.lpage_info[i - 1] = kvm_kvzalloc(lpages *
7324 sizeof(*slot->arch.lpage_info[i - 1]));
7325 if (!slot->arch.lpage_info[i - 1])
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007326 goto out_free;
7327
7328 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007329 slot->arch.lpage_info[i - 1][0].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007330 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007331 slot->arch.lpage_info[i - 1][lpages - 1].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007332 ugfn = slot->userspace_addr >> PAGE_SHIFT;
7333 /*
7334 * If the gfn and userspace address are not aligned wrt each
7335 * other, or if explicitly asked to, disable large page
7336 * support for this slot
7337 */
7338 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
7339 !kvm_largepages_enabled()) {
7340 unsigned long j;
7341
7342 for (j = 0; j < lpages; ++j)
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007343 slot->arch.lpage_info[i - 1][j].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007344 }
7345 }
7346
7347 return 0;
7348
7349out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007350 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7351 kvm_kvfree(slot->arch.rmap[i]);
7352 slot->arch.rmap[i] = NULL;
7353 if (i == 0)
7354 continue;
7355
7356 kvm_kvfree(slot->arch.lpage_info[i - 1]);
7357 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007358 }
7359 return -ENOMEM;
7360}
7361
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007362void kvm_arch_memslots_updated(struct kvm *kvm)
7363{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09007364 /*
7365 * memslots->generation has been incremented.
7366 * mmio generation may have reached its maximum value.
7367 */
7368 kvm_mmu_invalidate_mmio_sptes(kvm);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007369}
7370
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007371int kvm_arch_prepare_memory_region(struct kvm *kvm,
7372 struct kvm_memory_slot *memslot,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007373 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007374 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08007375{
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007376 /*
7377 * Only private memory slots need to be mapped here since
7378 * KVM_SET_MEMORY_REGION ioctl is no longer supported.
Zhang Xiantao0de10342007-11-20 16:25:04 +08007379 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007380 if ((memslot->id >= KVM_USER_MEM_SLOTS) && (change == KVM_MR_CREATE)) {
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007381 unsigned long userspace_addr;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007382
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007383 /*
7384 * MAP_SHARED to prevent internal slot pages from being moved
7385 * by fork()/COW.
7386 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007387 userspace_addr = vm_mmap(NULL, 0, memslot->npages * PAGE_SIZE,
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007388 PROT_READ | PROT_WRITE,
7389 MAP_SHARED | MAP_ANONYMOUS, 0);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007390
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007391 if (IS_ERR((void *)userspace_addr))
7392 return PTR_ERR((void *)userspace_addr);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007393
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007394 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08007395 }
7396
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007397 return 0;
7398}
7399
7400void kvm_arch_commit_memory_region(struct kvm *kvm,
7401 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007402 const struct kvm_memory_slot *old,
7403 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007404{
7405
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007406 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007407
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007408 if ((mem->slot >= KVM_USER_MEM_SLOTS) && (change == KVM_MR_DELETE)) {
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007409 int ret;
7410
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007411 ret = vm_munmap(old->userspace_addr,
7412 old->npages * PAGE_SIZE);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007413 if (ret < 0)
7414 printk(KERN_WARNING
7415 "kvm_vm_ioctl_set_memory_region: "
7416 "failed to munmap memory\n");
7417 }
7418
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007419 if (!kvm->arch.n_requested_mmu_pages)
7420 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007421
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007422 if (nr_mmu_pages)
7423 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007424 /*
7425 * Write protect all pages for dirty logging.
Xiao Guangrongc126d942014-04-17 17:06:14 +08007426 *
7427 * All the sptes including the large sptes which point to this
7428 * slot are set to readonly. We can not create any new large
7429 * spte on this slot until the end of the logging.
7430 *
7431 * See the comments in fast_page_fault().
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007432 */
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007433 if ((change != KVM_MR_DELETE) && (mem->flags & KVM_MEM_LOG_DIRTY_PAGES))
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007434 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007435}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007436
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007437void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007438{
Xiao Guangrong6ca18b62013-05-31 08:36:23 +08007439 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007440}
7441
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007442void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
7443 struct kvm_memory_slot *slot)
7444{
Xiao Guangrong6ca18b62013-05-31 08:36:23 +08007445 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007446}
7447
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007448int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
7449{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007450 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
7451 kvm_x86_ops->check_nested_events(vcpu, false);
7452
Gleb Natapovaf585b92010-10-14 11:22:46 +02007453 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
7454 !vcpu->arch.apf.halted)
7455 || !list_empty_careful(&vcpu->async_pf.done)
Jan Kiszka66450a22013-03-13 12:42:34 +01007456 || kvm_apic_has_events(vcpu)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307457 || vcpu->arch.pv.pv_unhalted
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007458 || atomic_read(&vcpu->arch.nmi_queued) ||
Gleb Natapova1b37102009-07-09 15:33:52 +03007459 (kvm_arch_interrupt_allowed(vcpu) &&
7460 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007461}
Zhang Xiantao57361992007-12-17 14:21:40 +08007462
Christoffer Dallb6d33832012-03-08 16:44:24 -05007463int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08007464{
Christoffer Dallb6d33832012-03-08 16:44:24 -05007465 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08007466}
Gleb Natapov78646122009-03-23 12:12:11 +02007467
7468int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
7469{
7470 return kvm_x86_ops->interrupt_allowed(vcpu);
7471}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007472
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007473bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
7474{
7475 unsigned long current_rip = kvm_rip_read(vcpu) +
7476 get_segment_base(vcpu, VCPU_SREG_CS);
7477
7478 return current_rip == linear_rip;
7479}
7480EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
7481
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007482unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
7483{
7484 unsigned long rflags;
7485
7486 rflags = kvm_x86_ops->get_rflags(vcpu);
7487 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01007488 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007489 return rflags;
7490}
7491EXPORT_SYMBOL_GPL(kvm_get_rflags);
7492
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007493static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007494{
7495 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007496 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01007497 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007498 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007499}
7500
7501void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
7502{
7503 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03007504 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007505}
7506EXPORT_SYMBOL_GPL(kvm_set_rflags);
7507
Gleb Natapov56028d02010-10-17 18:13:42 +02007508void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
7509{
7510 int r;
7511
Xiao Guangrongfb67e142010-12-07 10:35:25 +08007512 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08007513 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02007514 return;
7515
7516 r = kvm_mmu_reload(vcpu);
7517 if (unlikely(r))
7518 return;
7519
Xiao Guangrongfb67e142010-12-07 10:35:25 +08007520 if (!vcpu->arch.mmu.direct_map &&
7521 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
7522 return;
7523
Gleb Natapov56028d02010-10-17 18:13:42 +02007524 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
7525}
7526
Gleb Natapovaf585b92010-10-14 11:22:46 +02007527static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
7528{
7529 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
7530}
7531
7532static inline u32 kvm_async_pf_next_probe(u32 key)
7533{
7534 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
7535}
7536
7537static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7538{
7539 u32 key = kvm_async_pf_hash_fn(gfn);
7540
7541 while (vcpu->arch.apf.gfns[key] != ~0)
7542 key = kvm_async_pf_next_probe(key);
7543
7544 vcpu->arch.apf.gfns[key] = gfn;
7545}
7546
7547static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
7548{
7549 int i;
7550 u32 key = kvm_async_pf_hash_fn(gfn);
7551
7552 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08007553 (vcpu->arch.apf.gfns[key] != gfn &&
7554 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02007555 key = kvm_async_pf_next_probe(key);
7556
7557 return key;
7558}
7559
7560bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7561{
7562 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
7563}
7564
7565static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7566{
7567 u32 i, j, k;
7568
7569 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
7570 while (true) {
7571 vcpu->arch.apf.gfns[i] = ~0;
7572 do {
7573 j = kvm_async_pf_next_probe(j);
7574 if (vcpu->arch.apf.gfns[j] == ~0)
7575 return;
7576 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
7577 /*
7578 * k lies cyclically in ]i,j]
7579 * | i.k.j |
7580 * |....j i.k.| or |.k..j i...|
7581 */
7582 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
7583 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
7584 i = j;
7585 }
7586}
7587
Gleb Natapov7c907052010-10-14 11:22:53 +02007588static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
7589{
7590
7591 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
7592 sizeof(val));
7593}
7594
Gleb Natapovaf585b92010-10-14 11:22:46 +02007595void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
7596 struct kvm_async_pf *work)
7597{
Avi Kivity6389ee92010-11-29 16:12:30 +02007598 struct x86_exception fault;
7599
Gleb Natapov7c907052010-10-14 11:22:53 +02007600 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007601 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02007602
7603 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02007604 (vcpu->arch.apf.send_user_only &&
7605 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02007606 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
7607 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02007608 fault.vector = PF_VECTOR;
7609 fault.error_code_valid = true;
7610 fault.error_code = 0;
7611 fault.nested_page_fault = false;
7612 fault.address = work->arch.token;
7613 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02007614 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02007615}
7616
7617void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
7618 struct kvm_async_pf *work)
7619{
Avi Kivity6389ee92010-11-29 16:12:30 +02007620 struct x86_exception fault;
7621
Gleb Natapov7c907052010-10-14 11:22:53 +02007622 trace_kvm_async_pf_ready(work->arch.token, work->gva);
chai wenf2e10662013-10-14 22:22:33 +08007623 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02007624 work->arch.token = ~0; /* broadcast wakeup */
7625 else
7626 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
7627
7628 if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
7629 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02007630 fault.vector = PF_VECTOR;
7631 fault.error_code_valid = true;
7632 fault.error_code = 0;
7633 fault.nested_page_fault = false;
7634 fault.address = work->arch.token;
7635 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02007636 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08007637 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03007638 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02007639}
7640
7641bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
7642{
7643 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
7644 return true;
7645 else
7646 return !kvm_event_needs_reinjection(vcpu) &&
7647 kvm_x86_ops->interrupt_allowed(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007648}
7649
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007650void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
7651{
7652 atomic_inc(&kvm->arch.noncoherent_dma_count);
7653}
7654EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
7655
7656void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
7657{
7658 atomic_dec(&kvm->arch.noncoherent_dma_count);
7659}
7660EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
7661
7662bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
7663{
7664 return atomic_read(&kvm->arch.noncoherent_dma_count);
7665}
7666EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
7667
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007668EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
7669EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
7670EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
7671EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
7672EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02007673EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02007674EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02007675EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02007676EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02007677EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02007678EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01007679EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09007680EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +02007681EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);