blob: 08c48a3c7c3ac713904e36573e86f7c82e54b500 [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{
Avi Kivity18863bd2009-09-07 11:12:18 +0300215 if (slot >= shared_msrs_global.nr)
216 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800217 shared_msrs_global.msrs[slot] = msr;
218 /* we need ensured the shared_msr_global have been updated */
219 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300220}
221EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
222
223static void kvm_shared_msr_cpu_online(void)
224{
225 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300226
227 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800228 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300229}
230
Avi Kivityd5696722009-12-02 12:28:47 +0200231void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300232{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200233 unsigned int cpu = smp_processor_id();
234 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300235
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800236 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300237 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800238 smsr->values[slot].curr = value;
239 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300240 if (!smsr->registered) {
241 smsr->urn.on_user_return = kvm_on_user_return;
242 user_return_notifier_register(&smsr->urn);
243 smsr->registered = true;
244 }
245}
246EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
247
Avi Kivity3548bab2009-11-28 14:18:47 +0200248static void drop_user_return_notifiers(void *ignore)
249{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200250 unsigned int cpu = smp_processor_id();
251 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200252
253 if (smsr->registered)
254 kvm_on_user_return(&smsr->urn);
255}
256
Carsten Otte6866b832007-10-29 16:09:10 +0100257u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
258{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300259 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100260}
261EXPORT_SYMBOL_GPL(kvm_get_apic_base);
262
Jan Kiszka58cb6282014-01-24 16:48:44 +0100263int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100264{
Jan Kiszka58cb6282014-01-24 16:48:44 +0100265 u64 old_state = vcpu->arch.apic_base &
266 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
267 u64 new_state = msr_info->data &
268 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
269 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
270 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
271
272 if (!msr_info->host_initiated &&
273 ((msr_info->data & reserved_bits) != 0 ||
274 new_state == X2APIC_ENABLE ||
275 (new_state == MSR_IA32_APICBASE_ENABLE &&
276 old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
277 (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
278 old_state == 0)))
279 return 1;
280
281 kvm_lapic_set_base(vcpu, msr_info->data);
282 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100283}
284EXPORT_SYMBOL_GPL(kvm_set_apic_base);
285
Andi Kleen2605fc22014-05-02 00:44:37 +0200286asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000287{
288 /* Fault while not rebooting. We want the trace. */
289 BUG();
290}
291EXPORT_SYMBOL_GPL(kvm_spurious_fault);
292
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200293#define EXCPT_BENIGN 0
294#define EXCPT_CONTRIBUTORY 1
295#define EXCPT_PF 2
296
297static int exception_class(int vector)
298{
299 switch (vector) {
300 case PF_VECTOR:
301 return EXCPT_PF;
302 case DE_VECTOR:
303 case TS_VECTOR:
304 case NP_VECTOR:
305 case SS_VECTOR:
306 case GP_VECTOR:
307 return EXCPT_CONTRIBUTORY;
308 default:
309 break;
310 }
311 return EXCPT_BENIGN;
312}
313
314static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200315 unsigned nr, bool has_error, u32 error_code,
316 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200317{
318 u32 prev_nr;
319 int class1, class2;
320
Avi Kivity3842d132010-07-27 12:30:24 +0300321 kvm_make_request(KVM_REQ_EVENT, vcpu);
322
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200323 if (!vcpu->arch.exception.pending) {
324 queue:
325 vcpu->arch.exception.pending = true;
326 vcpu->arch.exception.has_error_code = has_error;
327 vcpu->arch.exception.nr = nr;
328 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200329 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200330 return;
331 }
332
333 /* to check exception */
334 prev_nr = vcpu->arch.exception.nr;
335 if (prev_nr == DF_VECTOR) {
336 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300337 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200338 return;
339 }
340 class1 = exception_class(prev_nr);
341 class2 = exception_class(nr);
342 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
343 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
344 /* generate double fault per SDM Table 5-5 */
345 vcpu->arch.exception.pending = true;
346 vcpu->arch.exception.has_error_code = true;
347 vcpu->arch.exception.nr = DF_VECTOR;
348 vcpu->arch.exception.error_code = 0;
349 } else
350 /* replace previous exception with a new one in a hope
351 that instruction re-execution will regenerate lost
352 exception */
353 goto queue;
354}
355
Avi Kivity298101d2007-11-25 13:41:11 +0200356void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
357{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200358 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200359}
360EXPORT_SYMBOL_GPL(kvm_queue_exception);
361
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200362void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
363{
364 kvm_multiple_exception(vcpu, nr, false, 0, true);
365}
366EXPORT_SYMBOL_GPL(kvm_requeue_exception);
367
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100368void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200369{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100370 if (err)
371 kvm_inject_gp(vcpu, 0);
372 else
373 kvm_x86_ops->skip_emulated_instruction(vcpu);
374}
375EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200376
Avi Kivity6389ee92010-11-29 16:12:30 +0200377void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200378{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200379 ++vcpu->stat.pf_guest;
Avi Kivity6389ee92010-11-29 16:12:30 +0200380 vcpu->arch.cr2 = fault->address;
381 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200382}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300383EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200384
Avi Kivity6389ee92010-11-29 16:12:30 +0200385void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200386{
Avi Kivity6389ee92010-11-29 16:12:30 +0200387 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
388 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200389 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200390 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200391}
392
Sheng Yang3419ffc2008-05-15 09:52:48 +0800393void kvm_inject_nmi(struct kvm_vcpu *vcpu)
394{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300395 atomic_inc(&vcpu->arch.nmi_queued);
396 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800397}
398EXPORT_SYMBOL_GPL(kvm_inject_nmi);
399
Avi Kivity298101d2007-11-25 13:41:11 +0200400void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
401{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200402 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200403}
404EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
405
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200406void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
407{
408 kvm_multiple_exception(vcpu, nr, true, error_code, true);
409}
410EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
411
Carsten Ottea03490e2007-10-29 16:09:35 +0100412/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300413 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
414 * a #GP and return false.
415 */
416bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200417{
Avi Kivity0a79b002009-09-01 12:03:25 +0300418 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
419 return true;
420 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
421 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100422}
Avi Kivity0a79b002009-09-01 12:03:25 +0300423EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100424
425/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200426 * This function will be used to read from the physical memory of the currently
427 * running guest. The difference to kvm_read_guest_page is that this function
428 * can read from guest physical or from the guest's guest physical memory.
429 */
430int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
431 gfn_t ngfn, void *data, int offset, int len,
432 u32 access)
433{
434 gfn_t real_gfn;
435 gpa_t ngpa;
436
437 ngpa = gfn_to_gpa(ngfn);
438 real_gfn = mmu->translate_gpa(vcpu, ngpa, access);
439 if (real_gfn == UNMAPPED_GVA)
440 return -EFAULT;
441
442 real_gfn = gpa_to_gfn(real_gfn);
443
444 return kvm_read_guest_page(vcpu->kvm, real_gfn, data, offset, len);
445}
446EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
447
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200448int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
449 void *data, int offset, int len, u32 access)
450{
451 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
452 data, offset, len, access);
453}
454
Joerg Roedelec92fe42010-09-10 17:30:51 +0200455/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100456 * Load the pae pdptrs. Return true is they are all valid.
457 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200458int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100459{
460 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
461 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
462 int i;
463 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200464 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100465
Joerg Roedelff03a072010-09-10 17:30:57 +0200466 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
467 offset * sizeof(u64), sizeof(pdpte),
468 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100469 if (ret < 0) {
470 ret = 0;
471 goto out;
472 }
473 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300474 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800475 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100476 ret = 0;
477 goto out;
478 }
479 }
480 ret = 1;
481
Joerg Roedelff03a072010-09-10 17:30:57 +0200482 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300483 __set_bit(VCPU_EXREG_PDPTR,
484 (unsigned long *)&vcpu->arch.regs_avail);
485 __set_bit(VCPU_EXREG_PDPTR,
486 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100487out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100488
489 return ret;
490}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100491EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100492
Avi Kivityd835dfe2007-11-21 02:57:59 +0200493static bool pdptrs_changed(struct kvm_vcpu *vcpu)
494{
Joerg Roedelff03a072010-09-10 17:30:57 +0200495 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200496 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200497 int offset;
498 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200499 int r;
500
501 if (is_long_mode(vcpu) || !is_pae(vcpu))
502 return false;
503
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300504 if (!test_bit(VCPU_EXREG_PDPTR,
505 (unsigned long *)&vcpu->arch.regs_avail))
506 return true;
507
Avi Kivity9f8fe502010-12-05 17:30:00 +0200508 gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
509 offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200510 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
511 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200512 if (r < 0)
513 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200514 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200515out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200516
517 return changed;
518}
519
Avi Kivity49a9b072010-06-10 17:02:14 +0300520int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100521{
Sheng Yangaad82702010-05-12 16:40:42 +0800522 unsigned long old_cr0 = kvm_read_cr0(vcpu);
523 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
524 X86_CR0_CD | X86_CR0_NW;
525
Avi Kivityf9a48e62010-01-06 19:10:22 +0200526 cr0 |= X86_CR0_ET;
527
Gleb Natapovab344822010-01-21 15:28:46 +0200528#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300529 if (cr0 & 0xffffffff00000000UL)
530 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200531#endif
532
533 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100534
Gleb Natapov0f122442010-04-28 19:15:31 +0300535 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
536 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100537
Gleb Natapov0f122442010-04-28 19:15:31 +0300538 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
539 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100540
541 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
542#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200543 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100544 int cs_db, cs_l;
545
Gleb Natapov0f122442010-04-28 19:15:31 +0300546 if (!is_pae(vcpu))
547 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100548 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300549 if (cs_l)
550 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100551 } else
552#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200553 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200554 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300555 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100556 }
557
Mao, Junjiead756a12012-07-02 01:18:48 +0000558 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
559 return 1;
560
Carsten Ottea03490e2007-10-29 16:09:35 +0100561 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100562
Lai Jiangshand170c412011-02-21 11:21:30 +0800563 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800564 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800565 kvm_async_pf_hash_reset(vcpu);
566 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800567
Sheng Yangaad82702010-05-12 16:40:42 +0800568 if ((cr0 ^ old_cr0) & update_bits)
569 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300570 return 0;
571}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200572EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100573
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200574void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100575{
Avi Kivity49a9b072010-06-10 17:02:14 +0300576 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100577}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200578EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100579
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300580static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
581{
582 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
583 !vcpu->guest_xcr0_loaded) {
584 /* kvm_set_xcr() also depends on this */
585 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
586 vcpu->guest_xcr0_loaded = 1;
587 }
588}
589
590static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
591{
592 if (vcpu->guest_xcr0_loaded) {
593 if (vcpu->arch.xcr0 != host_xcr0)
594 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
595 vcpu->guest_xcr0_loaded = 0;
596 }
597}
598
Dexuan Cui2acf9232010-06-10 11:27:12 +0800599int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
600{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000601 u64 xcr0 = xcr;
602 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200603 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800604
605 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
606 if (index != XCR_XFEATURE_ENABLED_MASK)
607 return 1;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800608 if (!(xcr0 & XSTATE_FP))
609 return 1;
610 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
611 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200612
613 /*
614 * Do not allow the guest to set bits that we do not support
615 * saving. However, xcr0 bit 0 is always set, even if the
616 * emulated CPU does not support XSAVE (see fx_init).
617 */
618 valid_bits = vcpu->arch.guest_supported_xcr0 | XSTATE_FP;
619 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800620 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200621
Liu, Jinsong390bd522014-02-24 10:58:09 +0000622 if ((!(xcr0 & XSTATE_BNDREGS)) != (!(xcr0 & XSTATE_BNDCSR)))
623 return 1;
624
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300625 kvm_put_guest_xcr0(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800626 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000627
628 if ((xcr0 ^ old_xcr0) & XSTATE_EXTEND_MASK)
629 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800630 return 0;
631}
632
633int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
634{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000635 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
636 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800637 kvm_inject_gp(vcpu, 0);
638 return 1;
639 }
640 return 0;
641}
642EXPORT_SYMBOL_GPL(kvm_set_xcr);
643
Avi Kivitya83b29c2010-06-10 17:02:15 +0300644int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100645{
Avi Kivityfc78f512009-12-07 12:16:48 +0200646 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800647 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE |
648 X86_CR4_PAE | X86_CR4_SMEP;
Gleb Natapov0f122442010-04-28 19:15:31 +0300649 if (cr4 & CR4_RESERVED_BITS)
650 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100651
Dexuan Cui2acf9232010-06-10 11:27:12 +0800652 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
653 return 1;
654
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800655 if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
656 return 1;
657
Feng Wu97ec8c02014-04-01 17:46:34 +0800658 if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
659 return 1;
660
H. Peter Anvinafcbf132013-04-27 16:37:47 -0700661 if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800662 return 1;
663
Carsten Ottea03490e2007-10-29 16:09:35 +0100664 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300665 if (!(cr4 & X86_CR4_PAE))
666 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300667 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
668 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200669 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
670 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300671 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100672
Mao, Junjiead756a12012-07-02 01:18:48 +0000673 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
674 if (!guest_cpuid_has_pcid(vcpu))
675 return 1;
676
677 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
678 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
679 return 1;
680 }
681
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300682 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300683 return 1;
684
Mao, Junjiead756a12012-07-02 01:18:48 +0000685 if (((cr4 ^ old_cr4) & pdptr_bits) ||
686 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800687 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300688
Feng Wu97ec8c02014-04-01 17:46:34 +0800689 if ((cr4 ^ old_cr4) & X86_CR4_SMAP)
690 update_permission_bitmask(vcpu, vcpu->arch.walk_mmu, false);
691
Dexuan Cui2acf9232010-06-10 11:27:12 +0800692 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +0200693 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800694
Gleb Natapov0f122442010-04-28 19:15:31 +0300695 return 0;
696}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200697EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100698
Avi Kivity23902182010-06-10 17:02:16 +0300699int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100700{
Avi Kivity9f8fe502010-12-05 17:30:00 +0200701 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300702 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200703 kvm_mmu_flush_tlb(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300704 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200705 }
706
Carsten Ottea03490e2007-10-29 16:09:35 +0100707 if (is_long_mode(vcpu)) {
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200708 if (cr3 & CR3_L_MODE_RESERVED_BITS)
709 return 1;
710 } else if (is_pae(vcpu) && is_paging(vcpu) &&
711 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300712 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100713
Gleb Natapov0f122442010-04-28 19:15:31 +0300714 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200715 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Paolo Bonzinid8d173d2013-10-02 16:56:11 +0200716 kvm_mmu_new_cr3(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300717 return 0;
718}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200719EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100720
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100721int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100722{
Gleb Natapov0f122442010-04-28 19:15:31 +0300723 if (cr8 & CR8_RESERVED_BITS)
724 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100725 if (irqchip_in_kernel(vcpu->kvm))
726 kvm_lapic_set_tpr(vcpu, cr8);
727 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800728 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300729 return 0;
730}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200731EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100732
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200733unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100734{
735 if (irqchip_in_kernel(vcpu->kvm))
736 return kvm_lapic_get_cr8(vcpu);
737 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800738 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100739}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200740EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100741
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100742static void kvm_update_dr6(struct kvm_vcpu *vcpu)
743{
744 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
745 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
746}
747
Jan Kiszkac8639012012-09-21 05:42:55 +0200748static void kvm_update_dr7(struct kvm_vcpu *vcpu)
749{
750 unsigned long dr7;
751
752 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
753 dr7 = vcpu->arch.guest_debug_dr7;
754 else
755 dr7 = vcpu->arch.dr7;
756 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100757 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
758 if (dr7 & DR7_BP_EN_MASK)
759 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200760}
761
Nadav Amit6f43ed02014-07-15 17:37:46 +0300762static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
763{
764 u64 fixed = DR6_FIXED_1;
765
766 if (!guest_cpuid_has_rtm(vcpu))
767 fixed |= DR6_RTM;
768 return fixed;
769}
770
Gleb Natapov338dbc92010-04-28 19:15:32 +0300771static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300772{
773 switch (dr) {
774 case 0 ... 3:
775 vcpu->arch.db[dr] = val;
776 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
777 vcpu->arch.eff_db[dr] = val;
778 break;
779 case 4:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300780 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
781 return 1; /* #UD */
Gleb Natapov020df072010-04-13 10:05:23 +0300782 /* fall through */
783 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300784 if (val & 0xffffffff00000000ULL)
785 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300786 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100787 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300788 break;
789 case 5:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300790 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
791 return 1; /* #UD */
Gleb Natapov020df072010-04-13 10:05:23 +0300792 /* fall through */
793 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300794 if (val & 0xffffffff00000000ULL)
795 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300796 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200797 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300798 break;
799 }
800
801 return 0;
802}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300803
804int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
805{
806 int res;
807
808 res = __kvm_set_dr(vcpu, dr, val);
809 if (res > 0)
810 kvm_queue_exception(vcpu, UD_VECTOR);
811 else if (res < 0)
812 kvm_inject_gp(vcpu, 0);
813
814 return res;
815}
Gleb Natapov020df072010-04-13 10:05:23 +0300816EXPORT_SYMBOL_GPL(kvm_set_dr);
817
Gleb Natapov338dbc92010-04-28 19:15:32 +0300818static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300819{
820 switch (dr) {
821 case 0 ... 3:
822 *val = vcpu->arch.db[dr];
823 break;
824 case 4:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300825 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Gleb Natapov020df072010-04-13 10:05:23 +0300826 return 1;
Gleb Natapov020df072010-04-13 10:05:23 +0300827 /* fall through */
828 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100829 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
830 *val = vcpu->arch.dr6;
831 else
832 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300833 break;
834 case 5:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300835 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Gleb Natapov020df072010-04-13 10:05:23 +0300836 return 1;
Gleb Natapov020df072010-04-13 10:05:23 +0300837 /* fall through */
838 default: /* 7 */
839 *val = vcpu->arch.dr7;
840 break;
841 }
842
843 return 0;
844}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300845
846int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
847{
848 if (_kvm_get_dr(vcpu, dr, val)) {
849 kvm_queue_exception(vcpu, UD_VECTOR);
850 return 1;
851 }
852 return 0;
853}
Gleb Natapov020df072010-04-13 10:05:23 +0300854EXPORT_SYMBOL_GPL(kvm_get_dr);
855
Avi Kivity022cd0e2011-11-10 14:57:23 +0200856bool kvm_rdpmc(struct kvm_vcpu *vcpu)
857{
858 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
859 u64 data;
860 int err;
861
862 err = kvm_pmu_read_pmc(vcpu, ecx, &data);
863 if (err)
864 return err;
865 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
866 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
867 return err;
868}
869EXPORT_SYMBOL_GPL(kvm_rdpmc);
870
Carsten Otte043405e2007-10-10 17:16:19 +0200871/*
872 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
873 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
874 *
875 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400876 * capabilities of the host cpu. This capabilities test skips MSRs that are
877 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200878 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400879
Vadim Rozenfelde9840972014-01-16 20:18:37 +1100880#define KVM_SAVE_MSRS_BEGIN 12
Carsten Otte043405e2007-10-10 17:16:19 +0200881static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400882 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Glauber Costa11c6bff2010-05-11 12:17:41 -0400883 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200884 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Vadim Rozenfelde9840972014-01-16 20:18:37 +1100885 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Glauber Costac9aaa892011-07-11 15:28:14 -0400886 HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +0300887 MSR_KVM_PV_EOI_EN,
Carsten Otte043405e2007-10-10 17:16:19 +0200888 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400889 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200890#ifdef CONFIG_X86_64
891 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
892#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800893 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Liu, Jinsong0dd376e2014-02-24 10:56:53 +0000894 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS
Carsten Otte043405e2007-10-10 17:16:19 +0200895};
896
897static unsigned num_msrs_to_save;
898
Mathias Krausef1d24832012-08-30 01:30:18 +0200899static const u32 emulated_msrs[] = {
Will Auldba904632012-11-29 12:42:50 -0800900 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800901 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +0200902 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300903 MSR_IA32_MCG_STATUS,
904 MSR_IA32_MCG_CTL,
Carsten Otte043405e2007-10-10 17:16:19 +0200905};
906
Jan Kiszka384bb782013-04-20 10:52:36 +0200907bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100908{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200909 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +0200910 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +0100911
Alexander Graf1b2fd702009-02-02 16:23:51 +0100912 if (efer & EFER_FFXSR) {
913 struct kvm_cpuid_entry2 *feat;
914
915 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200916 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200917 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +0100918 }
919
Alexander Grafd8017472008-11-25 20:17:11 +0100920 if (efer & EFER_SVME) {
921 struct kvm_cpuid_entry2 *feat;
922
923 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200924 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200925 return false;
Alexander Grafd8017472008-11-25 20:17:11 +0100926 }
927
Jan Kiszka384bb782013-04-20 10:52:36 +0200928 return true;
929}
930EXPORT_SYMBOL_GPL(kvm_valid_efer);
931
932static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
933{
934 u64 old_efer = vcpu->arch.efer;
935
936 if (!kvm_valid_efer(vcpu, efer))
937 return 1;
938
939 if (is_paging(vcpu)
940 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
941 return 1;
942
Carsten Otte15c4a642007-10-30 18:44:17 +0100943 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200944 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100945
Sheng Yanga3d204e2010-05-12 16:40:40 +0800946 kvm_x86_ops->set_efer(vcpu, efer);
947
Sheng Yangaad82702010-05-12 16:40:42 +0800948 /* Update reserved bits */
949 if ((efer ^ old_efer) & EFER_NX)
950 kvm_mmu_reset_context(vcpu);
951
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200952 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +0100953}
954
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100955void kvm_enable_efer_bits(u64 mask)
956{
957 efer_reserved_bits &= ~mask;
958}
959EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
960
961
Carsten Otte15c4a642007-10-30 18:44:17 +0100962/*
963 * Writes msr value into into the appropriate "register".
964 * Returns 0 on success, non-0 otherwise.
965 * Assumes vcpu_load() was already called.
966 */
Will Auld8fe8ab42012-11-29 12:42:12 -0800967int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +0100968{
Will Auld8fe8ab42012-11-29 12:42:12 -0800969 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +0100970}
971
Carsten Otte313a3dc2007-10-11 19:16:52 +0200972/*
973 * Adapt set_msr() to msr_io()'s calling convention
974 */
975static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
976{
Will Auld8fe8ab42012-11-29 12:42:12 -0800977 struct msr_data msr;
978
979 msr.data = *data;
980 msr.index = index;
981 msr.host_initiated = true;
982 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200983}
984
Marcelo Tosatti16e8d742012-11-27 23:29:00 -0200985#ifdef CONFIG_X86_64
986struct pvclock_gtod_data {
987 seqcount_t seq;
988
989 struct { /* extract of a clocksource struct */
990 int vclock_mode;
991 cycle_t cycle_last;
992 cycle_t mask;
993 u32 mult;
994 u32 shift;
995 } clock;
996
997 /* open coded 'struct timespec' */
998 u64 monotonic_time_snsec;
999 time_t monotonic_time_sec;
1000};
1001
1002static struct pvclock_gtod_data pvclock_gtod_data;
1003
1004static void update_pvclock_gtod(struct timekeeper *tk)
1005{
1006 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1007
1008 write_seqcount_begin(&vdata->seq);
1009
1010 /* copy pvclock gtod data */
1011 vdata->clock.vclock_mode = tk->clock->archdata.vclock_mode;
1012 vdata->clock.cycle_last = tk->clock->cycle_last;
1013 vdata->clock.mask = tk->clock->mask;
1014 vdata->clock.mult = tk->mult;
1015 vdata->clock.shift = tk->shift;
1016
1017 vdata->monotonic_time_sec = tk->xtime_sec
1018 + tk->wall_to_monotonic.tv_sec;
1019 vdata->monotonic_time_snsec = tk->xtime_nsec
1020 + (tk->wall_to_monotonic.tv_nsec
1021 << tk->shift);
1022 while (vdata->monotonic_time_snsec >=
1023 (((u64)NSEC_PER_SEC) << tk->shift)) {
1024 vdata->monotonic_time_snsec -=
1025 ((u64)NSEC_PER_SEC) << tk->shift;
1026 vdata->monotonic_time_sec++;
1027 }
1028
1029 write_seqcount_end(&vdata->seq);
1030}
1031#endif
1032
1033
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001034static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1035{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001036 int version;
1037 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001038 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +08001039 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001040
1041 if (!wall_clock)
1042 return;
1043
Avi Kivity9ed3c442010-05-04 15:00:37 +03001044 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1045 if (r)
1046 return;
1047
1048 if (version & 1)
1049 ++version; /* first time write, random junk */
1050
1051 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001052
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001053 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
1054
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001055 /*
1056 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001057 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001058 * wall clock specified here. guest system time equals host
1059 * system time for us, thus we must fill in host boot time here.
1060 */
Jason Wang923de3c2010-01-27 19:13:49 +08001061 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001062
Bruce Rogers4b648662012-07-20 10:44:24 -06001063 if (kvm->arch.kvmclock_offset) {
1064 struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
1065 boot = timespec_sub(boot, ts);
1066 }
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001067 wc.sec = boot.tv_sec;
1068 wc.nsec = boot.tv_nsec;
1069 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001070
1071 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1072
1073 version++;
1074 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001075}
1076
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001077static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1078{
1079 uint32_t quotient, remainder;
1080
1081 /* Don't try to replace with do_div(), this one calculates
1082 * "(dividend << 32) / divisor" */
1083 __asm__ ( "divl %4"
1084 : "=a" (quotient), "=d" (remainder)
1085 : "0" (0), "1" (dividend), "r" (divisor) );
1086 return quotient;
1087}
1088
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001089static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
1090 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001091{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001092 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001093 int32_t shift = 0;
1094 uint64_t tps64;
1095 uint32_t tps32;
1096
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001097 tps64 = base_khz * 1000LL;
1098 scaled64 = scaled_khz * 1000LL;
Jan Kiszka50933622010-09-26 13:00:53 +02001099 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001100 tps64 >>= 1;
1101 shift--;
1102 }
1103
1104 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001105 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1106 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001107 scaled64 >>= 1;
1108 else
1109 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001110 shift++;
1111 }
1112
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001113 *pshift = shift;
1114 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001115
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001116 pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
1117 __func__, base_khz, scaled_khz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001118}
1119
Zachary Amsden759379d2010-08-19 22:07:25 -10001120static inline u64 get_kernel_ns(void)
1121{
1122 struct timespec ts;
1123
Zachary Amsden759379d2010-08-19 22:07:25 -10001124 ktime_get_ts(&ts);
1125 monotonic_to_bootbased(&ts);
1126 return timespec_to_ns(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001127}
1128
Marcelo Tosattid8281992012-11-27 23:29:01 -02001129#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001130static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001131#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001132
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001133static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001134unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001135
Joerg Roedel857e4092011-03-25 09:44:50 +01001136static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Zachary Amsden759379d2010-08-19 22:07:25 -10001137{
Zachary Amsdencc578282012-02-03 15:43:50 -02001138 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
1139 vcpu->arch.virtual_tsc_shift);
Zachary Amsden759379d2010-08-19 22:07:25 -10001140}
1141
Zachary Amsdencc578282012-02-03 15:43:50 -02001142static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001143{
Zachary Amsdencc578282012-02-03 15:43:50 -02001144 u64 v = (u64)khz * (1000000 + ppm);
1145 do_div(v, 1000000);
1146 return v;
1147}
1148
1149static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
1150{
1151 u32 thresh_lo, thresh_hi;
1152 int use_scaling = 0;
1153
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001154 /* tsc_khz can be zero if TSC calibration fails */
1155 if (this_tsc_khz == 0)
1156 return;
1157
Zachary Amsdenc2855452010-09-18 14:38:15 -10001158 /* Compute a scale to convert nanoseconds in TSC cycles */
1159 kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
Zachary Amsdencc578282012-02-03 15:43:50 -02001160 &vcpu->arch.virtual_tsc_shift,
1161 &vcpu->arch.virtual_tsc_mult);
1162 vcpu->arch.virtual_tsc_khz = this_tsc_khz;
1163
1164 /*
1165 * Compute the variation in TSC rate which is acceptable
1166 * within the range of tolerance and decide if the
1167 * rate being applied is within that bounds of the hardware
1168 * rate. If so, no scaling or compensation need be done.
1169 */
1170 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1171 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
1172 if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
1173 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
1174 use_scaling = 1;
1175 }
1176 kvm_x86_ops->set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001177}
1178
1179static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1180{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001181 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001182 vcpu->arch.virtual_tsc_mult,
1183 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001184 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001185 return tsc;
1186}
1187
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001188void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
1189{
1190#ifdef CONFIG_X86_64
1191 bool vcpus_matched;
1192 bool do_request = false;
1193 struct kvm_arch *ka = &vcpu->kvm->arch;
1194 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1195
1196 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1197 atomic_read(&vcpu->kvm->online_vcpus));
1198
1199 if (vcpus_matched && gtod->clock.vclock_mode == VCLOCK_TSC)
1200 if (!ka->use_master_clock)
1201 do_request = 1;
1202
1203 if (!vcpus_matched && ka->use_master_clock)
1204 do_request = 1;
1205
1206 if (do_request)
1207 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1208
1209 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1210 atomic_read(&vcpu->kvm->online_vcpus),
1211 ka->use_master_clock, gtod->clock.vclock_mode);
1212#endif
1213}
1214
Will Auldba904632012-11-29 12:42:50 -08001215static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1216{
1217 u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
1218 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1219}
1220
Will Auld8fe8ab42012-11-29 12:42:12 -08001221void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001222{
1223 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001224 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001225 unsigned long flags;
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001226 s64 usdiff;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001227 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001228 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001229 u64 data = msr->data;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001230
Jan Kiszka038f8c12011-02-04 10:49:11 +01001231 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Joerg Roedel857e4092011-03-25 09:44:50 +01001232 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001233 ns = get_kernel_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001234 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001235
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001236 if (vcpu->arch.virtual_tsc_khz) {
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001237 int faulted = 0;
1238
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001239 /* n.b - signed multiplication and division required */
1240 usdiff = data - kvm->arch.last_tsc_write;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001241#ifdef CONFIG_X86_64
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001242 usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001243#else
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001244 /* do_div() only does unsigned */
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001245 asm("1: idivl %[divisor]\n"
1246 "2: xor %%edx, %%edx\n"
1247 " movl $0, %[faulted]\n"
1248 "3:\n"
1249 ".section .fixup,\"ax\"\n"
1250 "4: movl $1, %[faulted]\n"
1251 " jmp 3b\n"
1252 ".previous\n"
1253
1254 _ASM_EXTABLE(1b, 4b)
1255
1256 : "=A"(usdiff), [faulted] "=r" (faulted)
1257 : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
1258
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001259#endif
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001260 do_div(elapsed, 1000);
1261 usdiff -= elapsed;
1262 if (usdiff < 0)
1263 usdiff = -usdiff;
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001264
1265 /* idivl overflow => difference is larger than USEC_PER_SEC */
1266 if (faulted)
1267 usdiff = USEC_PER_SEC;
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001268 } else
1269 usdiff = USEC_PER_SEC; /* disable TSC match window below */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001270
1271 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001272 * Special case: TSC write with a small delta (1 second) of virtual
1273 * cycle time against real time is interpreted as an attempt to
1274 * synchronize the CPU.
1275 *
1276 * For a reliable TSC, we can match TSC offsets, and for an unstable
1277 * TSC, we add elapsed time in this computation. We could let the
1278 * compensation code attempt to catch up if we fall behind, but
1279 * it's better to try to match offsets from the beginning.
1280 */
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001281 if (usdiff < USEC_PER_SEC &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001282 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001283 if (!check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001284 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001285 pr_debug("kvm: matched tsc offset for %llu\n", data);
1286 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001287 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001288 data += delta;
1289 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001290 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001291 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001292 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001293 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001294 } else {
1295 /*
1296 * We split periods of matched TSC writes into generations.
1297 * For each generation, we track the original measured
1298 * nanosecond time, offset, and write, so if TSCs are in
1299 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001300 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001301 *
1302 * These values are tracked in kvm->arch.cur_xxx variables.
1303 */
1304 kvm->arch.cur_tsc_generation++;
1305 kvm->arch.cur_tsc_nsec = ns;
1306 kvm->arch.cur_tsc_write = data;
1307 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001308 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001309 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001310 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001311 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001312
1313 /*
1314 * We also track th most recent recorded KHZ, write and time to
1315 * allow the matching interval to be extended at each write.
1316 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001317 kvm->arch.last_tsc_nsec = ns;
1318 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001319 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001320
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001321 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001322
1323 /* Keep track of which generation this VCPU has synchronized to */
1324 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1325 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1326 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1327
Will Auldba904632012-11-29 12:42:50 -08001328 if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
1329 update_ia32_tsc_adjust_msr(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001330 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1331 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001332
1333 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001334 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001335 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001336 } else if (!already_matched) {
1337 kvm->arch.nr_vcpus_matched_tsc++;
1338 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001339
1340 kvm_track_tsc_matching(vcpu);
1341 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001342}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001343
Zachary Amsden99e3e302010-08-19 22:07:17 -10001344EXPORT_SYMBOL_GPL(kvm_write_tsc);
1345
Marcelo Tosattid8281992012-11-27 23:29:01 -02001346#ifdef CONFIG_X86_64
1347
1348static cycle_t read_tsc(void)
1349{
1350 cycle_t ret;
1351 u64 last;
1352
1353 /*
1354 * Empirically, a fence (of type that depends on the CPU)
1355 * before rdtsc is enough to ensure that rdtsc is ordered
1356 * with respect to loads. The various CPU manuals are unclear
1357 * as to whether rdtsc can be reordered with later loads,
1358 * but no one has ever seen it happen.
1359 */
1360 rdtsc_barrier();
1361 ret = (cycle_t)vget_cycles();
1362
1363 last = pvclock_gtod_data.clock.cycle_last;
1364
1365 if (likely(ret >= last))
1366 return ret;
1367
1368 /*
1369 * GCC likes to generate cmov here, but this branch is extremely
1370 * predictable (it's just a funciton of time and the likely is
1371 * very likely) and there's a data dependence, so force GCC
1372 * to generate a branch instead. I don't barrier() because
1373 * we don't actually need a barrier, and if this function
1374 * ever gets inlined it will generate worse code.
1375 */
1376 asm volatile ("");
1377 return last;
1378}
1379
1380static inline u64 vgettsc(cycle_t *cycle_now)
1381{
1382 long v;
1383 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1384
1385 *cycle_now = read_tsc();
1386
1387 v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
1388 return v * gtod->clock.mult;
1389}
1390
1391static int do_monotonic(struct timespec *ts, cycle_t *cycle_now)
1392{
1393 unsigned long seq;
1394 u64 ns;
1395 int mode;
1396 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1397
1398 ts->tv_nsec = 0;
1399 do {
1400 seq = read_seqcount_begin(&gtod->seq);
1401 mode = gtod->clock.vclock_mode;
1402 ts->tv_sec = gtod->monotonic_time_sec;
1403 ns = gtod->monotonic_time_snsec;
1404 ns += vgettsc(cycle_now);
1405 ns >>= gtod->clock.shift;
1406 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
1407 timespec_add_ns(ts, ns);
1408
1409 return mode;
1410}
1411
1412/* returns true if host is using tsc clocksource */
1413static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
1414{
1415 struct timespec ts;
1416
1417 /* checked again under seqlock below */
1418 if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
1419 return false;
1420
1421 if (do_monotonic(&ts, cycle_now) != VCLOCK_TSC)
1422 return false;
1423
1424 monotonic_to_bootbased(&ts);
1425 *kernel_ns = timespec_to_ns(&ts);
1426
1427 return true;
1428}
1429#endif
1430
1431/*
1432 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001433 * Assuming a stable TSC across physical CPUS, and a stable TSC
1434 * across virtual CPUs, the following condition is possible.
1435 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001436 * CPUs at the next numbered event.
1437 *
1438 * "timespecX" represents host monotonic time. "tscX" represents
1439 * RDTSC value.
1440 *
1441 * VCPU0 on CPU0 | VCPU1 on CPU1
1442 *
1443 * 1. read timespec0,tsc0
1444 * 2. | timespec1 = timespec0 + N
1445 * | tsc1 = tsc0 + M
1446 * 3. transition to guest | transition to guest
1447 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1448 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1449 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1450 *
1451 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1452 *
1453 * - ret0 < ret1
1454 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1455 * ...
1456 * - 0 < N - M => M < N
1457 *
1458 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1459 * always the case (the difference between two distinct xtime instances
1460 * might be smaller then the difference between corresponding TSC reads,
1461 * when updating guest vcpus pvclock areas).
1462 *
1463 * To avoid that problem, do not allow visibility of distinct
1464 * system_timestamp/tsc_timestamp values simultaneously: use a master
1465 * copy of host monotonic time values. Update that master copy
1466 * in lockstep.
1467 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001468 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001469 *
1470 */
1471
1472static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1473{
1474#ifdef CONFIG_X86_64
1475 struct kvm_arch *ka = &kvm->arch;
1476 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001477 bool host_tsc_clocksource, vcpus_matched;
1478
1479 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1480 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001481
1482 /*
1483 * If the host uses TSC clock, then passthrough TSC as stable
1484 * to the guest.
1485 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001486 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001487 &ka->master_kernel_ns,
1488 &ka->master_cycle_now);
1489
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001490 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
1491 && !backwards_tsc_observed;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001492
Marcelo Tosattid8281992012-11-27 23:29:01 -02001493 if (ka->use_master_clock)
1494 atomic_set(&kvm_guest_has_master_clock, 1);
1495
1496 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001497 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1498 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001499#endif
1500}
1501
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001502static void kvm_gen_update_masterclock(struct kvm *kvm)
1503{
1504#ifdef CONFIG_X86_64
1505 int i;
1506 struct kvm_vcpu *vcpu;
1507 struct kvm_arch *ka = &kvm->arch;
1508
1509 spin_lock(&ka->pvclock_gtod_sync_lock);
1510 kvm_make_mclock_inprogress_request(kvm);
1511 /* no guest entries from this point */
1512 pvclock_update_vm_gtod_copy(kvm);
1513
1514 kvm_for_each_vcpu(i, vcpu, kvm)
1515 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
1516
1517 /* guest entries allowed */
1518 kvm_for_each_vcpu(i, vcpu, kvm)
1519 clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
1520
1521 spin_unlock(&ka->pvclock_gtod_sync_lock);
1522#endif
1523}
1524
Zachary Amsden34c238a2010-09-18 14:38:14 -10001525static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001526{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001527 unsigned long flags, this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001528 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001529 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02001530 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001531 u64 tsc_timestamp, host_tsc;
Andy Honig0b794592013-02-20 14:48:10 -08001532 struct pvclock_vcpu_time_info guest_hv_clock;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001533 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001534 bool use_master_clock;
1535
1536 kernel_ns = 0;
1537 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001538
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001539 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02001540 * If the host uses TSC clock, then passthrough TSC as stable
1541 * to the guest.
1542 */
1543 spin_lock(&ka->pvclock_gtod_sync_lock);
1544 use_master_clock = ka->use_master_clock;
1545 if (use_master_clock) {
1546 host_tsc = ka->master_cycle_now;
1547 kernel_ns = ka->master_kernel_ns;
1548 }
1549 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001550
1551 /* Keep irq disabled to prevent changes to the clock */
1552 local_irq_save(flags);
1553 this_tsc_khz = __get_cpu_var(cpu_tsc_khz);
1554 if (unlikely(this_tsc_khz == 0)) {
1555 local_irq_restore(flags);
1556 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
1557 return 1;
1558 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001559 if (!use_master_clock) {
1560 host_tsc = native_read_tsc();
1561 kernel_ns = get_kernel_ns();
1562 }
1563
1564 tsc_timestamp = kvm_x86_ops->read_l1_tsc(v, host_tsc);
1565
1566 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10001567 * We may have to catch up the TSC to match elapsed wall clock
1568 * time for two reasons, even if kvmclock is used.
1569 * 1) CPU could have been running below the maximum TSC rate
1570 * 2) Broken TSC compensation resets the base at each VCPU
1571 * entry to avoid unknown leaps of TSC even when running
1572 * again on the same CPU. This may cause apparent elapsed
1573 * time to disappear, and the guest to stand still or run
1574 * very slowly.
1575 */
1576 if (vcpu->tsc_catchup) {
1577 u64 tsc = compute_guest_tsc(v, kernel_ns);
1578 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001579 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001580 tsc_timestamp = tsc;
1581 }
1582 }
1583
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001584 local_irq_restore(flags);
1585
Andy Honig0b794592013-02-20 14:48:10 -08001586 if (!vcpu->pv_time_enabled)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001587 return 0;
1588
Zachary Amsdene48672f2010-08-19 22:07:23 -10001589 if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001590 kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
1591 &vcpu->hv_clock.tsc_shift,
1592 &vcpu->hv_clock.tsc_to_system_mul);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001593 vcpu->hw_tsc_khz = this_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001594 }
1595
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001596 /* With all the info we got, fill in the values */
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001597 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10001598 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10001599 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001600
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001601 /*
1602 * The interface expects us to write an even number signaling that the
1603 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001604 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001605 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001606 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001607
Andy Honig0b794592013-02-20 14:48:10 -08001608 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1609 &guest_hv_clock, sizeof(guest_hv_clock))))
1610 return 0;
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001611
1612 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
Andy Honig0b794592013-02-20 14:48:10 -08001613 pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001614
1615 if (vcpu->pvclock_set_guest_stopped_request) {
1616 pvclock_flags |= PVCLOCK_GUEST_STOPPED;
1617 vcpu->pvclock_set_guest_stopped_request = false;
1618 }
1619
Marcelo Tosattid8281992012-11-27 23:29:01 -02001620 /* If the host uses TSC clocksource, then it is stable */
1621 if (use_master_clock)
1622 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
1623
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001624 vcpu->hv_clock.flags = pvclock_flags;
1625
Andy Honig0b794592013-02-20 14:48:10 -08001626 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1627 &vcpu->hv_clock,
1628 sizeof(vcpu->hv_clock));
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001629 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001630}
1631
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001632/*
1633 * kvmclock updates which are isolated to a given vcpu, such as
1634 * vcpu->cpu migration, should not allow system_timestamp from
1635 * the rest of the vcpus to remain static. Otherwise ntp frequency
1636 * correction applies to one vcpu's system_timestamp but not
1637 * the others.
1638 *
1639 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01001640 * We need to rate-limit these requests though, as they can
1641 * considerably slow guests that have a large number of vcpus.
1642 * The time for a remote vcpu to update its kvmclock is bound
1643 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001644 */
1645
Andrew Jones7e44e442014-02-28 12:52:54 +01001646#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
1647
1648static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001649{
1650 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01001651 struct delayed_work *dwork = to_delayed_work(work);
1652 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1653 kvmclock_update_work);
1654 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001655 struct kvm_vcpu *vcpu;
1656
1657 kvm_for_each_vcpu(i, vcpu, kvm) {
1658 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
1659 kvm_vcpu_kick(vcpu);
1660 }
1661}
1662
Andrew Jones7e44e442014-02-28 12:52:54 +01001663static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
1664{
1665 struct kvm *kvm = v->kvm;
1666
1667 set_bit(KVM_REQ_CLOCK_UPDATE, &v->requests);
1668 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
1669 KVMCLOCK_UPDATE_DELAY);
1670}
1671
Andrew Jones332967a2014-02-28 12:52:55 +01001672#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
1673
1674static void kvmclock_sync_fn(struct work_struct *work)
1675{
1676 struct delayed_work *dwork = to_delayed_work(work);
1677 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1678 kvmclock_sync_work);
1679 struct kvm *kvm = container_of(ka, struct kvm, arch);
1680
1681 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
1682 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
1683 KVMCLOCK_SYNC_PERIOD);
1684}
1685
Avi Kivity9ba075a2008-05-26 20:06:35 +03001686static bool msr_mtrr_valid(unsigned msr)
1687{
1688 switch (msr) {
1689 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
1690 case MSR_MTRRfix64K_00000:
1691 case MSR_MTRRfix16K_80000:
1692 case MSR_MTRRfix16K_A0000:
1693 case MSR_MTRRfix4K_C0000:
1694 case MSR_MTRRfix4K_C8000:
1695 case MSR_MTRRfix4K_D0000:
1696 case MSR_MTRRfix4K_D8000:
1697 case MSR_MTRRfix4K_E0000:
1698 case MSR_MTRRfix4K_E8000:
1699 case MSR_MTRRfix4K_F0000:
1700 case MSR_MTRRfix4K_F8000:
1701 case MSR_MTRRdefType:
1702 case MSR_IA32_CR_PAT:
1703 return true;
1704 case 0x2f8:
1705 return true;
1706 }
1707 return false;
1708}
1709
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001710static bool valid_pat_type(unsigned t)
1711{
1712 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
1713}
1714
1715static bool valid_mtrr_type(unsigned t)
1716{
1717 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
1718}
1719
1720static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1721{
1722 int i;
1723
1724 if (!msr_mtrr_valid(msr))
1725 return false;
1726
1727 if (msr == MSR_IA32_CR_PAT) {
1728 for (i = 0; i < 8; i++)
1729 if (!valid_pat_type((data >> (i * 8)) & 0xff))
1730 return false;
1731 return true;
1732 } else if (msr == MSR_MTRRdefType) {
1733 if (data & ~0xcff)
1734 return false;
1735 return valid_mtrr_type(data & 0xff);
1736 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
1737 for (i = 0; i < 8 ; i++)
1738 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
1739 return false;
1740 return true;
1741 }
1742
1743 /* variable MTRRs */
1744 return valid_mtrr_type(data & 0xff);
1745}
1746
Avi Kivity9ba075a2008-05-26 20:06:35 +03001747static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1748{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001749 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1750
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001751 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +03001752 return 1;
1753
Sheng Yang0bed3b52008-10-09 16:01:54 +08001754 if (msr == MSR_MTRRdefType) {
1755 vcpu->arch.mtrr_state.def_type = data;
1756 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
1757 } else if (msr == MSR_MTRRfix64K_00000)
1758 p[0] = data;
1759 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1760 p[1 + msr - MSR_MTRRfix16K_80000] = data;
1761 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1762 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
1763 else if (msr == MSR_IA32_CR_PAT)
1764 vcpu->arch.pat = data;
1765 else { /* Variable MTRRs */
1766 int idx, is_mtrr_mask;
1767 u64 *pt;
1768
1769 idx = (msr - 0x200) / 2;
1770 is_mtrr_mask = msr - 0x200 - 2 * idx;
1771 if (!is_mtrr_mask)
1772 pt =
1773 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1774 else
1775 pt =
1776 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1777 *pt = data;
1778 }
1779
1780 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +03001781 return 0;
1782}
Carsten Otte15c4a642007-10-30 18:44:17 +01001783
Huang Ying890ca9a2009-05-11 16:48:15 +08001784static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1785{
1786 u64 mcg_cap = vcpu->arch.mcg_cap;
1787 unsigned bank_num = mcg_cap & 0xff;
1788
1789 switch (msr) {
1790 case MSR_IA32_MCG_STATUS:
1791 vcpu->arch.mcg_status = data;
1792 break;
1793 case MSR_IA32_MCG_CTL:
1794 if (!(mcg_cap & MCG_CTL_P))
1795 return 1;
1796 if (data != 0 && data != ~(u64)0)
1797 return -1;
1798 vcpu->arch.mcg_ctl = data;
1799 break;
1800 default:
1801 if (msr >= MSR_IA32_MC0_CTL &&
1802 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1803 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001804 /* only 0 or all 1s can be written to IA32_MCi_CTL
1805 * some Linux kernels though clear bit 10 in bank 4 to
1806 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1807 * this to avoid an uncatched #GP in the guest
1808 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001809 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001810 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001811 return -1;
1812 vcpu->arch.mce_banks[offset] = data;
1813 break;
1814 }
1815 return 1;
1816 }
1817 return 0;
1818}
1819
Ed Swierkffde22a2009-10-15 15:21:43 -07001820static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1821{
1822 struct kvm *kvm = vcpu->kvm;
1823 int lm = is_long_mode(vcpu);
1824 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1825 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1826 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1827 : kvm->arch.xen_hvm_config.blob_size_32;
1828 u32 page_num = data & ~PAGE_MASK;
1829 u64 page_addr = data & PAGE_MASK;
1830 u8 *page;
1831 int r;
1832
1833 r = -E2BIG;
1834 if (page_num >= blob_size)
1835 goto out;
1836 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001837 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
1838 if (IS_ERR(page)) {
1839 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07001840 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001841 }
Ed Swierkffde22a2009-10-15 15:21:43 -07001842 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1843 goto out_free;
1844 r = 0;
1845out_free:
1846 kfree(page);
1847out:
1848 return r;
1849}
1850
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001851static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1852{
1853 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1854}
1855
1856static bool kvm_hv_msr_partition_wide(u32 msr)
1857{
1858 bool r = false;
1859 switch (msr) {
1860 case HV_X64_MSR_GUEST_OS_ID:
1861 case HV_X64_MSR_HYPERCALL:
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001862 case HV_X64_MSR_REFERENCE_TSC:
1863 case HV_X64_MSR_TIME_REF_COUNT:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001864 r = true;
1865 break;
1866 }
1867
1868 return r;
1869}
1870
1871static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1872{
1873 struct kvm *kvm = vcpu->kvm;
1874
1875 switch (msr) {
1876 case HV_X64_MSR_GUEST_OS_ID:
1877 kvm->arch.hv_guest_os_id = data;
1878 /* setting guest os id to zero disables hypercall page */
1879 if (!kvm->arch.hv_guest_os_id)
1880 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1881 break;
1882 case HV_X64_MSR_HYPERCALL: {
1883 u64 gfn;
1884 unsigned long addr;
1885 u8 instructions[4];
1886
1887 /* if guest os id is not set hypercall should remain disabled */
1888 if (!kvm->arch.hv_guest_os_id)
1889 break;
1890 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1891 kvm->arch.hv_hypercall = data;
1892 break;
1893 }
1894 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1895 addr = gfn_to_hva(kvm, gfn);
1896 if (kvm_is_error_hva(addr))
1897 return 1;
1898 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1899 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001900 if (__copy_to_user((void __user *)addr, instructions, 4))
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001901 return 1;
1902 kvm->arch.hv_hypercall = data;
Vadim Rozenfeldb94b64c2014-01-23 18:12:52 +11001903 mark_page_dirty(kvm, gfn);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001904 break;
1905 }
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001906 case HV_X64_MSR_REFERENCE_TSC: {
1907 u64 gfn;
1908 HV_REFERENCE_TSC_PAGE tsc_ref;
1909 memset(&tsc_ref, 0, sizeof(tsc_ref));
1910 kvm->arch.hv_tsc_page = data;
1911 if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1912 break;
1913 gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
1914 if (kvm_write_guest(kvm, data,
1915 &tsc_ref, sizeof(tsc_ref)))
1916 return 1;
1917 mark_page_dirty(kvm, gfn);
1918 break;
1919 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001920 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03001921 vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1922 "data 0x%llx\n", msr, data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001923 return 1;
1924 }
1925 return 0;
1926}
1927
1928static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1929{
Gleb Natapov10388a02010-01-17 15:51:23 +02001930 switch (msr) {
1931 case HV_X64_MSR_APIC_ASSIST_PAGE: {
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001932 u64 gfn;
Gleb Natapov10388a02010-01-17 15:51:23 +02001933 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001934
Gleb Natapov10388a02010-01-17 15:51:23 +02001935 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1936 vcpu->arch.hv_vapic = data;
Michael S. Tsirkinb63cf422014-05-07 16:29:48 +03001937 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1938 return 1;
Gleb Natapov10388a02010-01-17 15:51:23 +02001939 break;
1940 }
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001941 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
1942 addr = gfn_to_hva(vcpu->kvm, gfn);
Gleb Natapov10388a02010-01-17 15:51:23 +02001943 if (kvm_is_error_hva(addr))
1944 return 1;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001945 if (__clear_user((void __user *)addr, PAGE_SIZE))
Gleb Natapov10388a02010-01-17 15:51:23 +02001946 return 1;
1947 vcpu->arch.hv_vapic = data;
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001948 mark_page_dirty(vcpu->kvm, gfn);
Michael S. Tsirkinb63cf422014-05-07 16:29:48 +03001949 if (kvm_lapic_enable_pv_eoi(vcpu, gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1950 return 1;
Gleb Natapov10388a02010-01-17 15:51:23 +02001951 break;
1952 }
1953 case HV_X64_MSR_EOI:
1954 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1955 case HV_X64_MSR_ICR:
1956 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1957 case HV_X64_MSR_TPR:
1958 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1959 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03001960 vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1961 "data 0x%llx\n", msr, data);
Gleb Natapov10388a02010-01-17 15:51:23 +02001962 return 1;
1963 }
1964
1965 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001966}
1967
Gleb Natapov344d9582010-10-14 11:22:50 +02001968static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
1969{
1970 gpa_t gpa = data & ~0x3f;
1971
Guo Chao4a969982012-06-28 15:17:27 +08001972 /* Bits 2:5 are reserved, Should be zero */
Gleb Natapov6adba522010-10-14 11:22:55 +02001973 if (data & 0x3c)
Gleb Natapov344d9582010-10-14 11:22:50 +02001974 return 1;
1975
1976 vcpu->arch.apf.msr_val = data;
1977
1978 if (!(data & KVM_ASYNC_PF_ENABLED)) {
1979 kvm_clear_async_pf_completion_queue(vcpu);
1980 kvm_async_pf_hash_reset(vcpu);
1981 return 0;
1982 }
1983
Andrew Honig8f964522013-03-29 09:35:21 -07001984 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
1985 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02001986 return 1;
1987
Gleb Natapov6adba522010-10-14 11:22:55 +02001988 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Gleb Natapov344d9582010-10-14 11:22:50 +02001989 kvm_async_pf_wakeup_all(vcpu);
1990 return 0;
1991}
1992
Glauber Costa12f9a482011-02-01 14:16:40 -05001993static void kvmclock_reset(struct kvm_vcpu *vcpu)
1994{
Andy Honig0b794592013-02-20 14:48:10 -08001995 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05001996}
1997
Glauber Costac9aaa892011-07-11 15:28:14 -04001998static void accumulate_steal_time(struct kvm_vcpu *vcpu)
1999{
2000 u64 delta;
2001
2002 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2003 return;
2004
2005 delta = current->sched_info.run_delay - vcpu->arch.st.last_steal;
2006 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2007 vcpu->arch.st.accum_steal = delta;
2008}
2009
2010static void record_steal_time(struct kvm_vcpu *vcpu)
2011{
2012 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2013 return;
2014
2015 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2016 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2017 return;
2018
2019 vcpu->arch.st.steal.steal += vcpu->arch.st.accum_steal;
2020 vcpu->arch.st.steal.version += 2;
2021 vcpu->arch.st.accum_steal = 0;
2022
2023 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2024 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2025}
2026
Will Auld8fe8ab42012-11-29 12:42:12 -08002027int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002028{
Gleb Natapov57537852012-01-15 14:17:22 +02002029 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002030 u32 msr = msr_info->index;
2031 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002032
Carsten Otte15c4a642007-10-30 18:44:17 +01002033 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002034 case MSR_AMD64_NB_CFG:
2035 case MSR_IA32_UCODE_REV:
2036 case MSR_IA32_UCODE_WRITE:
2037 case MSR_VM_HSAVE_PA:
2038 case MSR_AMD64_PATCH_LOADER:
2039 case MSR_AMD64_BU_CFG2:
2040 break;
2041
Carsten Otte15c4a642007-10-30 18:44:17 +01002042 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02002043 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002044 case MSR_K7_HWCR:
2045 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002046 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002047 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02002048 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02002049 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002050 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2051 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002052 return 1;
2053 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002054 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002055 case MSR_FAM10H_MMIO_CONF_BASE:
2056 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002057 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2058 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002059 return 1;
2060 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002061 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002062 case MSR_IA32_DEBUGCTLMSR:
2063 if (!data) {
2064 /* We support the non-activated case already */
2065 break;
2066 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2067 /* Values other than LBR and BTF are vendor-specific,
2068 thus reserved and should throw a #GP */
2069 return 1;
2070 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002071 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2072 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002073 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002074 case 0x200 ... 0x2ff:
2075 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002076 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002077 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002078 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2079 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002080 case MSR_IA32_TSCDEADLINE:
2081 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2082 break;
Will Auldba904632012-11-29 12:42:50 -08002083 case MSR_IA32_TSC_ADJUST:
2084 if (guest_cpuid_has_tsc_adjust(vcpu)) {
2085 if (!msr_info->host_initiated) {
2086 u64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
2087 kvm_x86_ops->adjust_tsc_offset(vcpu, adj, true);
2088 }
2089 vcpu->arch.ia32_tsc_adjust_msr = data;
2090 }
2091 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002092 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002093 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01002094 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002095 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002096 case MSR_KVM_WALL_CLOCK:
2097 vcpu->kvm->arch.wall_clock = data;
2098 kvm_write_wall_clock(vcpu->kvm, data);
2099 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002100 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002101 case MSR_KVM_SYSTEM_TIME: {
Andy Honig0b794592013-02-20 14:48:10 -08002102 u64 gpa_offset;
Glauber Costa12f9a482011-02-01 14:16:40 -05002103 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002104
2105 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002106 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002107
2108 /* we verify if the enable bit is set... */
2109 if (!(data & 1))
2110 break;
2111
Andy Honig0b794592013-02-20 14:48:10 -08002112 gpa_offset = data & ~(PAGE_MASK | 1);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002113
Andy Honig0b794592013-02-20 14:48:10 -08002114 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002115 &vcpu->arch.pv_time, data & ~1ULL,
2116 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002117 vcpu->arch.pv_time_enabled = false;
2118 else
2119 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002120
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002121 break;
2122 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002123 case MSR_KVM_ASYNC_PF_EN:
2124 if (kvm_pv_enable_async_pf(vcpu, data))
2125 return 1;
2126 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002127 case MSR_KVM_STEAL_TIME:
2128
2129 if (unlikely(!sched_info_on()))
2130 return 1;
2131
2132 if (data & KVM_STEAL_RESERVED_MASK)
2133 return 1;
2134
2135 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002136 data & KVM_STEAL_VALID_BITS,
2137 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002138 return 1;
2139
2140 vcpu->arch.st.msr_val = data;
2141
2142 if (!(data & KVM_MSR_ENABLED))
2143 break;
2144
2145 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2146
2147 preempt_disable();
2148 accumulate_steal_time(vcpu);
2149 preempt_enable();
2150
2151 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2152
2153 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002154 case MSR_KVM_PV_EOI_EN:
2155 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2156 return 1;
2157 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002158
Huang Ying890ca9a2009-05-11 16:48:15 +08002159 case MSR_IA32_MCG_CTL:
2160 case MSR_IA32_MCG_STATUS:
2161 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
2162 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002163
2164 /* Performance counters are not protected by a CPUID bit,
2165 * so we should check all of them in the generic path for the sake of
2166 * cross vendor migration.
2167 * Writing a zero into the event select MSRs disables them,
2168 * which we perfectly emulate ;-). Any other value should be at least
2169 * reported, some guests depend on them.
2170 */
Andre Przywara71db6022009-06-12 22:01:29 +02002171 case MSR_K7_EVNTSEL0:
2172 case MSR_K7_EVNTSEL1:
2173 case MSR_K7_EVNTSEL2:
2174 case MSR_K7_EVNTSEL3:
2175 if (data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002176 vcpu_unimpl(vcpu, "unimplemented perfctr wrmsr: "
2177 "0x%x data 0x%llx\n", msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002178 break;
2179 /* at least RHEL 4 unconditionally writes to the perfctr registers,
2180 * so we ignore writes to make it happy.
2181 */
Andre Przywara71db6022009-06-12 22:01:29 +02002182 case MSR_K7_PERFCTR0:
2183 case MSR_K7_PERFCTR1:
2184 case MSR_K7_PERFCTR2:
2185 case MSR_K7_PERFCTR3:
Christoffer Dalla737f252012-06-03 21:17:48 +03002186 vcpu_unimpl(vcpu, "unimplemented perfctr wrmsr: "
2187 "0x%x data 0x%llx\n", msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002188 break;
Gleb Natapov57537852012-01-15 14:17:22 +02002189 case MSR_P6_PERFCTR0:
2190 case MSR_P6_PERFCTR1:
2191 pr = true;
2192 case MSR_P6_EVNTSEL0:
2193 case MSR_P6_EVNTSEL1:
2194 if (kvm_pmu_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002195 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002196
2197 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002198 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2199 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002200 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002201 case MSR_K7_CLK_CTL:
2202 /*
2203 * Ignore all writes to this no longer documented MSR.
2204 * Writes are only relevant for old K7 processors,
2205 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002206 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002207 * affected processor models on the command line, hence
2208 * the need to ignore the workaround.
2209 */
2210 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002211 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
2212 if (kvm_hv_msr_partition_wide(msr)) {
2213 int r;
2214 mutex_lock(&vcpu->kvm->lock);
2215 r = set_msr_hyperv_pw(vcpu, msr, data);
2216 mutex_unlock(&vcpu->kvm->lock);
2217 return r;
2218 } else
2219 return set_msr_hyperv(vcpu, msr, data);
2220 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002221 case MSR_IA32_BBL_CR_CTL3:
2222 /* Drop writes to this legacy MSR -- see rdmsr
2223 * counterpart for further detail.
2224 */
Christoffer Dalla737f252012-06-03 21:17:48 +03002225 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002226 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002227 case MSR_AMD64_OSVW_ID_LENGTH:
2228 if (!guest_cpuid_has_osvw(vcpu))
2229 return 1;
2230 vcpu->arch.osvw.length = data;
2231 break;
2232 case MSR_AMD64_OSVW_STATUS:
2233 if (!guest_cpuid_has_osvw(vcpu))
2234 return 1;
2235 vcpu->arch.osvw.status = data;
2236 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002237 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002238 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2239 return xen_hvm_config(vcpu, data);
Gleb Natapovf5132b02011-11-10 14:57:22 +02002240 if (kvm_pmu_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002241 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002242 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002243 vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
2244 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002245 return 1;
2246 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002247 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
2248 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002249 break;
2250 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002251 }
2252 return 0;
2253}
2254EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2255
2256
2257/*
2258 * Reads an msr value (of 'msr_index') into 'pdata'.
2259 * Returns 0 on success, non-0 otherwise.
2260 * Assumes vcpu_load() was already called.
2261 */
2262int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2263{
2264 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
2265}
2266
Avi Kivity9ba075a2008-05-26 20:06:35 +03002267static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2268{
Sheng Yang0bed3b52008-10-09 16:01:54 +08002269 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
2270
Avi Kivity9ba075a2008-05-26 20:06:35 +03002271 if (!msr_mtrr_valid(msr))
2272 return 1;
2273
Sheng Yang0bed3b52008-10-09 16:01:54 +08002274 if (msr == MSR_MTRRdefType)
2275 *pdata = vcpu->arch.mtrr_state.def_type +
2276 (vcpu->arch.mtrr_state.enabled << 10);
2277 else if (msr == MSR_MTRRfix64K_00000)
2278 *pdata = p[0];
2279 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
2280 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
2281 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
2282 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
2283 else if (msr == MSR_IA32_CR_PAT)
2284 *pdata = vcpu->arch.pat;
2285 else { /* Variable MTRRs */
2286 int idx, is_mtrr_mask;
2287 u64 *pt;
2288
2289 idx = (msr - 0x200) / 2;
2290 is_mtrr_mask = msr - 0x200 - 2 * idx;
2291 if (!is_mtrr_mask)
2292 pt =
2293 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
2294 else
2295 pt =
2296 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
2297 *pdata = *pt;
2298 }
2299
Avi Kivity9ba075a2008-05-26 20:06:35 +03002300 return 0;
2301}
2302
Huang Ying890ca9a2009-05-11 16:48:15 +08002303static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2304{
2305 u64 data;
2306 u64 mcg_cap = vcpu->arch.mcg_cap;
2307 unsigned bank_num = mcg_cap & 0xff;
2308
2309 switch (msr) {
2310 case MSR_IA32_P5_MC_ADDR:
2311 case MSR_IA32_P5_MC_TYPE:
2312 data = 0;
2313 break;
2314 case MSR_IA32_MCG_CAP:
2315 data = vcpu->arch.mcg_cap;
2316 break;
2317 case MSR_IA32_MCG_CTL:
2318 if (!(mcg_cap & MCG_CTL_P))
2319 return 1;
2320 data = vcpu->arch.mcg_ctl;
2321 break;
2322 case MSR_IA32_MCG_STATUS:
2323 data = vcpu->arch.mcg_status;
2324 break;
2325 default:
2326 if (msr >= MSR_IA32_MC0_CTL &&
2327 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
2328 u32 offset = msr - MSR_IA32_MC0_CTL;
2329 data = vcpu->arch.mce_banks[offset];
2330 break;
2331 }
2332 return 1;
2333 }
2334 *pdata = data;
2335 return 0;
2336}
2337
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002338static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2339{
2340 u64 data = 0;
2341 struct kvm *kvm = vcpu->kvm;
2342
2343 switch (msr) {
2344 case HV_X64_MSR_GUEST_OS_ID:
2345 data = kvm->arch.hv_guest_os_id;
2346 break;
2347 case HV_X64_MSR_HYPERCALL:
2348 data = kvm->arch.hv_hypercall;
2349 break;
Vadim Rozenfelde9840972014-01-16 20:18:37 +11002350 case HV_X64_MSR_TIME_REF_COUNT: {
2351 data =
2352 div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
2353 break;
2354 }
2355 case HV_X64_MSR_REFERENCE_TSC:
2356 data = kvm->arch.hv_tsc_page;
2357 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002358 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03002359 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002360 return 1;
2361 }
2362
2363 *pdata = data;
2364 return 0;
2365}
2366
2367static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2368{
2369 u64 data = 0;
2370
2371 switch (msr) {
2372 case HV_X64_MSR_VP_INDEX: {
2373 int r;
2374 struct kvm_vcpu *v;
Takuya Yoshikawa684851a2014-02-27 15:08:31 +09002375 kvm_for_each_vcpu(r, v, vcpu->kvm) {
2376 if (v == vcpu) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002377 data = r;
Takuya Yoshikawa684851a2014-02-27 15:08:31 +09002378 break;
2379 }
2380 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002381 break;
2382 }
Gleb Natapov10388a02010-01-17 15:51:23 +02002383 case HV_X64_MSR_EOI:
2384 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
2385 case HV_X64_MSR_ICR:
2386 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
2387 case HV_X64_MSR_TPR:
2388 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Mike Waychison14fa67e2011-07-21 15:38:10 -07002389 case HV_X64_MSR_APIC_ASSIST_PAGE:
Mike Waychisond1613ad2011-07-23 00:31:45 -07002390 data = vcpu->arch.hv_vapic;
2391 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002392 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03002393 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002394 return 1;
2395 }
2396 *pdata = data;
2397 return 0;
2398}
2399
Carsten Otte15c4a642007-10-30 18:44:17 +01002400int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2401{
2402 u64 data;
2403
2404 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002405 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002406 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002407 case MSR_IA32_DEBUGCTLMSR:
2408 case MSR_IA32_LASTBRANCHFROMIP:
2409 case MSR_IA32_LASTBRANCHTOIP:
2410 case MSR_IA32_LASTINTFROMIP:
2411 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302412 case MSR_K8_SYSCFG:
2413 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002414 case MSR_VM_HSAVE_PA:
Amit Shah9e699622009-06-15 13:25:34 +05302415 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05302416 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002417 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002418 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002419 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002420 case MSR_AMD64_BU_CFG2:
Carsten Otte15c4a642007-10-30 18:44:17 +01002421 data = 0;
2422 break;
Gleb Natapov57537852012-01-15 14:17:22 +02002423 case MSR_P6_PERFCTR0:
2424 case MSR_P6_PERFCTR1:
2425 case MSR_P6_EVNTSEL0:
2426 case MSR_P6_EVNTSEL1:
2427 if (kvm_pmu_msr(vcpu, msr))
2428 return kvm_pmu_get_msr(vcpu, msr, pdata);
2429 data = 0;
2430 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002431 case MSR_IA32_UCODE_REV:
2432 data = 0x100000000ULL;
2433 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002434 case MSR_MTRRcap:
2435 data = 0x500 | KVM_NR_VAR_MTRR;
2436 break;
2437 case 0x200 ... 0x2ff:
2438 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01002439 case 0xcd: /* fsb frequency */
2440 data = 3;
2441 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002442 /*
2443 * MSR_EBC_FREQUENCY_ID
2444 * Conservative value valid for even the basic CPU models.
2445 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2446 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2447 * and 266MHz for model 3, or 4. Set Core Clock
2448 * Frequency to System Bus Frequency Ratio to 1 (bits
2449 * 31:24) even though these are only valid for CPU
2450 * models > 2, however guests may end up dividing or
2451 * multiplying by zero otherwise.
2452 */
2453 case MSR_EBC_FREQUENCY_ID:
2454 data = 1 << 24;
2455 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002456 case MSR_IA32_APICBASE:
2457 data = kvm_get_apic_base(vcpu);
2458 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002459 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2460 return kvm_x2apic_msr_read(vcpu, msr, pdata);
2461 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002462 case MSR_IA32_TSCDEADLINE:
2463 data = kvm_get_lapic_tscdeadline_msr(vcpu);
2464 break;
Will Auldba904632012-11-29 12:42:50 -08002465 case MSR_IA32_TSC_ADJUST:
2466 data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
2467 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002468 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002469 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002470 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002471 case MSR_IA32_PERF_STATUS:
2472 /* TSC increment by tick */
2473 data = 1000ULL;
2474 /* CPU multiplier */
2475 data |= (((uint64_t)4ULL) << 40);
2476 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002477 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02002478 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002479 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002480 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002481 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002482 data = vcpu->kvm->arch.wall_clock;
2483 break;
2484 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002485 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002486 data = vcpu->arch.time;
2487 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002488 case MSR_KVM_ASYNC_PF_EN:
2489 data = vcpu->arch.apf.msr_val;
2490 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002491 case MSR_KVM_STEAL_TIME:
2492 data = vcpu->arch.st.msr_val;
2493 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002494 case MSR_KVM_PV_EOI_EN:
2495 data = vcpu->arch.pv_eoi.msr_val;
2496 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002497 case MSR_IA32_P5_MC_ADDR:
2498 case MSR_IA32_P5_MC_TYPE:
2499 case MSR_IA32_MCG_CAP:
2500 case MSR_IA32_MCG_CTL:
2501 case MSR_IA32_MCG_STATUS:
2502 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
2503 return get_msr_mce(vcpu, msr, pdata);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002504 case MSR_K7_CLK_CTL:
2505 /*
2506 * Provide expected ramp-up count for K7. All other
2507 * are set to zero, indicating minimum divisors for
2508 * every field.
2509 *
2510 * This prevents guest kernels on AMD host with CPU
2511 * type 6, model 8 and higher from exploding due to
2512 * the rdmsr failing.
2513 */
2514 data = 0x20000000;
2515 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002516 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
2517 if (kvm_hv_msr_partition_wide(msr)) {
2518 int r;
2519 mutex_lock(&vcpu->kvm->lock);
2520 r = get_msr_hyperv_pw(vcpu, msr, pdata);
2521 mutex_unlock(&vcpu->kvm->lock);
2522 return r;
2523 } else
2524 return get_msr_hyperv(vcpu, msr, pdata);
2525 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002526 case MSR_IA32_BBL_CR_CTL3:
2527 /* This legacy MSR exists but isn't fully documented in current
2528 * silicon. It is however accessed by winxp in very narrow
2529 * scenarios where it sets bit #19, itself documented as
2530 * a "reserved" bit. Best effort attempt to source coherent
2531 * read data here should the balance of the register be
2532 * interpreted by the guest:
2533 *
2534 * L2 cache control register 3: 64GB range, 256KB size,
2535 * enabled, latency 0x1, configured
2536 */
2537 data = 0xbe702111;
2538 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002539 case MSR_AMD64_OSVW_ID_LENGTH:
2540 if (!guest_cpuid_has_osvw(vcpu))
2541 return 1;
2542 data = vcpu->arch.osvw.length;
2543 break;
2544 case MSR_AMD64_OSVW_STATUS:
2545 if (!guest_cpuid_has_osvw(vcpu))
2546 return 1;
2547 data = vcpu->arch.osvw.status;
2548 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002549 default:
Gleb Natapovf5132b02011-11-10 14:57:22 +02002550 if (kvm_pmu_msr(vcpu, msr))
2551 return kvm_pmu_get_msr(vcpu, msr, pdata);
Andre Przywaraed85c062009-06-25 12:36:49 +02002552 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002553 vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Andre Przywaraed85c062009-06-25 12:36:49 +02002554 return 1;
2555 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002556 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
Andre Przywaraed85c062009-06-25 12:36:49 +02002557 data = 0;
2558 }
2559 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002560 }
2561 *pdata = data;
2562 return 0;
2563}
2564EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2565
Carsten Otte313a3dc2007-10-11 19:16:52 +02002566/*
2567 * Read or write a bunch of msrs. All parameters are kernel addresses.
2568 *
2569 * @return number of msrs set successfully.
2570 */
2571static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2572 struct kvm_msr_entry *entries,
2573 int (*do_msr)(struct kvm_vcpu *vcpu,
2574 unsigned index, u64 *data))
2575{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002576 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002577
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002578 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002579 for (i = 0; i < msrs->nmsrs; ++i)
2580 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2581 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002582 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002583
Carsten Otte313a3dc2007-10-11 19:16:52 +02002584 return i;
2585}
2586
2587/*
2588 * Read or write a bunch of msrs. Parameters are user addresses.
2589 *
2590 * @return number of msrs set successfully.
2591 */
2592static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2593 int (*do_msr)(struct kvm_vcpu *vcpu,
2594 unsigned index, u64 *data),
2595 int writeback)
2596{
2597 struct kvm_msrs msrs;
2598 struct kvm_msr_entry *entries;
2599 int r, n;
2600 unsigned size;
2601
2602 r = -EFAULT;
2603 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2604 goto out;
2605
2606 r = -E2BIG;
2607 if (msrs.nmsrs >= MAX_IO_MSRS)
2608 goto out;
2609
Carsten Otte313a3dc2007-10-11 19:16:52 +02002610 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002611 entries = memdup_user(user_msrs->entries, size);
2612 if (IS_ERR(entries)) {
2613 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002614 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002615 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002616
2617 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2618 if (r < 0)
2619 goto out_free;
2620
2621 r = -EFAULT;
2622 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2623 goto out_free;
2624
2625 r = n;
2626
2627out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002628 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002629out:
2630 return r;
2631}
2632
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002633int kvm_dev_ioctl_check_extension(long ext)
2634{
2635 int r;
2636
2637 switch (ext) {
2638 case KVM_CAP_IRQCHIP:
2639 case KVM_CAP_HLT:
2640 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002641 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002642 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002643 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002644 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002645 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002646 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002647 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002648 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002649 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002650 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002651 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04002652 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002653 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002654 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002655 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002656 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002657 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002658 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002659 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002660 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002661 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002662 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002663 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002664 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002665 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002666 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002667 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002668 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002669 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002670 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002671 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002672 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002673 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Alex Williamson2a5bab12013-04-16 13:49:18 -06002674#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2675 case KVM_CAP_ASSIGN_DEV_IRQ:
2676 case KVM_CAP_PCI_2_3:
2677#endif
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002678 r = 1;
2679 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02002680 case KVM_CAP_COALESCED_MMIO:
2681 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
2682 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002683 case KVM_CAP_VAPIC:
2684 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2685 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002686 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002687 r = KVM_SOFT_MAX_VCPUS;
2688 break;
2689 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002690 r = KVM_MAX_VCPUS;
2691 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002692 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002693 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002694 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002695 case KVM_CAP_PV_MMU: /* obsolete */
2696 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002697 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002698#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002699 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +02002700 r = iommu_present(&pci_bus_type);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002701 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002702#endif
Huang Ying890ca9a2009-05-11 16:48:15 +08002703 case KVM_CAP_MCE:
2704 r = KVM_MAX_MCE_BANKS;
2705 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002706 case KVM_CAP_XCRS:
2707 r = cpu_has_xsave;
2708 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002709 case KVM_CAP_TSC_CONTROL:
2710 r = kvm_has_tsc_control;
2711 break;
Jan Kiszka4d25a0662011-12-21 12:28:29 +01002712 case KVM_CAP_TSC_DEADLINE_TIMER:
2713 r = boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER);
2714 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002715 default:
2716 r = 0;
2717 break;
2718 }
2719 return r;
2720
2721}
2722
Carsten Otte043405e2007-10-10 17:16:19 +02002723long kvm_arch_dev_ioctl(struct file *filp,
2724 unsigned int ioctl, unsigned long arg)
2725{
2726 void __user *argp = (void __user *)arg;
2727 long r;
2728
2729 switch (ioctl) {
2730 case KVM_GET_MSR_INDEX_LIST: {
2731 struct kvm_msr_list __user *user_msr_list = argp;
2732 struct kvm_msr_list msr_list;
2733 unsigned n;
2734
2735 r = -EFAULT;
2736 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2737 goto out;
2738 n = msr_list.nmsrs;
2739 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2740 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2741 goto out;
2742 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002743 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02002744 goto out;
2745 r = -EFAULT;
2746 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2747 num_msrs_to_save * sizeof(u32)))
2748 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002749 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02002750 &emulated_msrs,
2751 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2752 goto out;
2753 r = 0;
2754 break;
2755 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002756 case KVM_GET_SUPPORTED_CPUID:
2757 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02002758 struct kvm_cpuid2 __user *cpuid_arg = argp;
2759 struct kvm_cpuid2 cpuid;
2760
2761 r = -EFAULT;
2762 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2763 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002764
2765 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2766 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02002767 if (r)
2768 goto out;
2769
2770 r = -EFAULT;
2771 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2772 goto out;
2773 r = 0;
2774 break;
2775 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002776 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
2777 u64 mce_cap;
2778
2779 mce_cap = KVM_MCE_CAP_SUPPORTED;
2780 r = -EFAULT;
2781 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
2782 goto out;
2783 r = 0;
2784 break;
2785 }
Carsten Otte043405e2007-10-10 17:16:19 +02002786 default:
2787 r = -EINVAL;
2788 }
2789out:
2790 return r;
2791}
2792
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002793static void wbinvd_ipi(void *garbage)
2794{
2795 wbinvd();
2796}
2797
2798static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
2799{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06002800 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002801}
2802
Carsten Otte313a3dc2007-10-11 19:16:52 +02002803void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2804{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002805 /* Address WBINVD may be executed by guest */
2806 if (need_emulate_wbinvd(vcpu)) {
2807 if (kvm_x86_ops->has_wbinvd_exit())
2808 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
2809 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
2810 smp_call_function_single(vcpu->cpu,
2811 wbinvd_ipi, NULL, 1);
2812 }
2813
Carsten Otte313a3dc2007-10-11 19:16:52 +02002814 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002815
2816 /* Apply any externally detected TSC adjustments (due to suspend) */
2817 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
2818 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
2819 vcpu->arch.tsc_offset_adjustment = 0;
2820 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
2821 }
2822
Zachary Amsden48434c202010-08-19 22:07:24 -10002823 if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002824 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
2825 native_read_tsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10002826 if (tsc_delta < 0)
2827 mark_tsc_unstable("KVM discovered backwards TSC");
Zachary Amsdenc2855452010-09-18 14:38:15 -10002828 if (check_tsc_unstable()) {
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002829 u64 offset = kvm_x86_ops->compute_tsc_offset(vcpu,
2830 vcpu->arch.last_guest_tsc);
2831 kvm_x86_ops->write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002832 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002833 }
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02002834 /*
2835 * On a host with synchronized TSC, there is no need to update
2836 * kvmclock on vcpu->cpu migration
2837 */
2838 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002839 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002840 if (vcpu->cpu != cpu)
2841 kvm_migrate_timers(vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002842 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10002843 }
Glauber Costac9aaa892011-07-11 15:28:14 -04002844
2845 accumulate_steal_time(vcpu);
2846 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002847}
2848
2849void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2850{
Avi Kivity02daab22009-12-30 12:40:26 +02002851 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03002852 kvm_put_guest_fpu(vcpu);
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002853 vcpu->arch.last_host_tsc = native_read_tsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02002854}
2855
Carsten Otte313a3dc2007-10-11 19:16:52 +02002856static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2857 struct kvm_lapic_state *s)
2858{
Yang Zhang5a717852013-04-11 19:25:16 +08002859 kvm_x86_ops->sync_pir_to_irr(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002860 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002861
2862 return 0;
2863}
2864
2865static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2866 struct kvm_lapic_state *s)
2867{
Gleb Natapov64eb0622012-08-08 15:24:36 +03002868 kvm_apic_post_state_restore(vcpu, s);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002869 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002870
2871 return 0;
2872}
2873
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002874static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2875 struct kvm_interrupt *irq)
2876{
Chen Gang02cdb502013-02-27 11:33:25 +08002877 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002878 return -EINVAL;
2879 if (irqchip_in_kernel(vcpu->kvm))
2880 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002881
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002882 kvm_queue_interrupt(vcpu, irq->irq, false);
Avi Kivity3842d132010-07-27 12:30:24 +03002883 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002884
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002885 return 0;
2886}
2887
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002888static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2889{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002890 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002891
2892 return 0;
2893}
2894
Avi Kivityb209749f2007-10-22 16:50:39 +02002895static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2896 struct kvm_tpr_access_ctl *tac)
2897{
2898 if (tac->flags)
2899 return -EINVAL;
2900 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2901 return 0;
2902}
2903
Huang Ying890ca9a2009-05-11 16:48:15 +08002904static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2905 u64 mcg_cap)
2906{
2907 int r;
2908 unsigned bank_num = mcg_cap & 0xff, bank;
2909
2910 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002911 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002912 goto out;
2913 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2914 goto out;
2915 r = 0;
2916 vcpu->arch.mcg_cap = mcg_cap;
2917 /* Init IA32_MCG_CTL to all 1s */
2918 if (mcg_cap & MCG_CTL_P)
2919 vcpu->arch.mcg_ctl = ~(u64)0;
2920 /* Init IA32_MCi_CTL to all 1s */
2921 for (bank = 0; bank < bank_num; bank++)
2922 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2923out:
2924 return r;
2925}
2926
2927static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2928 struct kvm_x86_mce *mce)
2929{
2930 u64 mcg_cap = vcpu->arch.mcg_cap;
2931 unsigned bank_num = mcg_cap & 0xff;
2932 u64 *banks = vcpu->arch.mce_banks;
2933
2934 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2935 return -EINVAL;
2936 /*
2937 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2938 * reporting is disabled
2939 */
2940 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2941 vcpu->arch.mcg_ctl != ~(u64)0)
2942 return 0;
2943 banks += 4 * mce->bank;
2944 /*
2945 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2946 * reporting is disabled for the bank
2947 */
2948 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2949 return 0;
2950 if (mce->status & MCI_STATUS_UC) {
2951 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002952 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002953 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002954 return 0;
2955 }
2956 if (banks[1] & MCI_STATUS_VAL)
2957 mce->status |= MCI_STATUS_OVER;
2958 banks[2] = mce->addr;
2959 banks[3] = mce->misc;
2960 vcpu->arch.mcg_status = mce->mcg_status;
2961 banks[1] = mce->status;
2962 kvm_queue_exception(vcpu, MC_VECTOR);
2963 } else if (!(banks[1] & MCI_STATUS_VAL)
2964 || !(banks[1] & MCI_STATUS_UC)) {
2965 if (banks[1] & MCI_STATUS_VAL)
2966 mce->status |= MCI_STATUS_OVER;
2967 banks[2] = mce->addr;
2968 banks[3] = mce->misc;
2969 banks[1] = mce->status;
2970 } else
2971 banks[1] |= MCI_STATUS_OVER;
2972 return 0;
2973}
2974
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002975static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2976 struct kvm_vcpu_events *events)
2977{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002978 process_nmi(vcpu);
Jan Kiszka03b82a32010-02-15 10:45:41 +01002979 events->exception.injected =
2980 vcpu->arch.exception.pending &&
2981 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002982 events->exception.nr = vcpu->arch.exception.nr;
2983 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002984 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002985 events->exception.error_code = vcpu->arch.exception.error_code;
2986
Jan Kiszka03b82a32010-02-15 10:45:41 +01002987 events->interrupt.injected =
2988 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002989 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002990 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002991 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002992
2993 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002994 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002995 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002996 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002997
Jan Kiszka66450a22013-03-13 12:42:34 +01002998 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002999
Jan Kiszkadab4b912009-12-06 18:24:15 +01003000 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003001 | KVM_VCPUEVENT_VALID_SHADOW);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003002 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003003}
3004
3005static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3006 struct kvm_vcpu_events *events)
3007{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003008 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003009 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
3010 | KVM_VCPUEVENT_VALID_SHADOW))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003011 return -EINVAL;
3012
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003013 process_nmi(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003014 vcpu->arch.exception.pending = events->exception.injected;
3015 vcpu->arch.exception.nr = events->exception.nr;
3016 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3017 vcpu->arch.exception.error_code = events->exception.error_code;
3018
3019 vcpu->arch.interrupt.pending = events->interrupt.injected;
3020 vcpu->arch.interrupt.nr = events->interrupt.nr;
3021 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003022 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3023 kvm_x86_ops->set_interrupt_shadow(vcpu,
3024 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003025
3026 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003027 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3028 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003029 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3030
Jan Kiszka66450a22013-03-13 12:42:34 +01003031 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
3032 kvm_vcpu_has_lapic(vcpu))
3033 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003034
Avi Kivity3842d132010-07-27 12:30:24 +03003035 kvm_make_request(KVM_REQ_EVENT, vcpu);
3036
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003037 return 0;
3038}
3039
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003040static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3041 struct kvm_debugregs *dbgregs)
3042{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003043 unsigned long val;
3044
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003045 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003046 _kvm_get_dr(vcpu, 6, &val);
3047 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003048 dbgregs->dr7 = vcpu->arch.dr7;
3049 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003050 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003051}
3052
3053static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3054 struct kvm_debugregs *dbgregs)
3055{
3056 if (dbgregs->flags)
3057 return -EINVAL;
3058
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003059 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
3060 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003061 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003062 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003063 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003064
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003065 return 0;
3066}
3067
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003068static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3069 struct kvm_xsave *guest_xsave)
3070{
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003071 if (cpu_has_xsave) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003072 memcpy(guest_xsave->region,
3073 &vcpu->arch.guest_fpu.state->xsave,
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003074 vcpu->arch.guest_xstate_size);
3075 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] &=
3076 vcpu->arch.guest_supported_xcr0 | XSTATE_FPSSE;
3077 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003078 memcpy(guest_xsave->region,
3079 &vcpu->arch.guest_fpu.state->fxsave,
3080 sizeof(struct i387_fxsave_struct));
3081 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
3082 XSTATE_FPSSE;
3083 }
3084}
3085
3086static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3087 struct kvm_xsave *guest_xsave)
3088{
3089 u64 xstate_bv =
3090 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
3091
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003092 if (cpu_has_xsave) {
3093 /*
3094 * Here we allow setting states that are not present in
3095 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3096 * with old userspace.
3097 */
Paolo Bonzini4ff41732014-02-24 12:15:16 +01003098 if (xstate_bv & ~kvm_supported_xcr0())
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003099 return -EINVAL;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003100 memcpy(&vcpu->arch.guest_fpu.state->xsave,
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003101 guest_xsave->region, vcpu->arch.guest_xstate_size);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003102 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003103 if (xstate_bv & ~XSTATE_FPSSE)
3104 return -EINVAL;
3105 memcpy(&vcpu->arch.guest_fpu.state->fxsave,
3106 guest_xsave->region, sizeof(struct i387_fxsave_struct));
3107 }
3108 return 0;
3109}
3110
3111static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3112 struct kvm_xcrs *guest_xcrs)
3113{
3114 if (!cpu_has_xsave) {
3115 guest_xcrs->nr_xcrs = 0;
3116 return;
3117 }
3118
3119 guest_xcrs->nr_xcrs = 1;
3120 guest_xcrs->flags = 0;
3121 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3122 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3123}
3124
3125static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3126 struct kvm_xcrs *guest_xcrs)
3127{
3128 int i, r = 0;
3129
3130 if (!cpu_has_xsave)
3131 return -EINVAL;
3132
3133 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3134 return -EINVAL;
3135
3136 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3137 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003138 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003139 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003140 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003141 break;
3142 }
3143 if (r)
3144 r = -EINVAL;
3145 return r;
3146}
3147
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003148/*
3149 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3150 * stopped by the hypervisor. This function will be called from the host only.
3151 * EINVAL is returned when the host attempts to set the flag for a guest that
3152 * does not support pv clocks.
3153 */
3154static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3155{
Andy Honig0b794592013-02-20 14:48:10 -08003156 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003157 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003158 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003159 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3160 return 0;
3161}
3162
Carsten Otte313a3dc2007-10-11 19:16:52 +02003163long kvm_arch_vcpu_ioctl(struct file *filp,
3164 unsigned int ioctl, unsigned long arg)
3165{
3166 struct kvm_vcpu *vcpu = filp->private_data;
3167 void __user *argp = (void __user *)arg;
3168 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003169 union {
3170 struct kvm_lapic_state *lapic;
3171 struct kvm_xsave *xsave;
3172 struct kvm_xcrs *xcrs;
3173 void *buffer;
3174 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003175
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003176 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003177 switch (ioctl) {
3178 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003179 r = -EINVAL;
3180 if (!vcpu->arch.apic)
3181 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003182 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003183
Dave Hansenb772ff32008-08-11 10:01:47 -07003184 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003185 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003186 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003187 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003188 if (r)
3189 goto out;
3190 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003191 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003192 goto out;
3193 r = 0;
3194 break;
3195 }
3196 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003197 r = -EINVAL;
3198 if (!vcpu->arch.apic)
3199 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003200 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Guo Chao18595412012-11-02 18:33:21 +08003201 if (IS_ERR(u.lapic))
3202 return PTR_ERR(u.lapic);
Sasha Levinff5c2c02011-12-04 19:36:29 +02003203
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003204 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003205 break;
3206 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003207 case KVM_INTERRUPT: {
3208 struct kvm_interrupt irq;
3209
3210 r = -EFAULT;
3211 if (copy_from_user(&irq, argp, sizeof irq))
3212 goto out;
3213 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003214 break;
3215 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003216 case KVM_NMI: {
3217 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003218 break;
3219 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003220 case KVM_SET_CPUID: {
3221 struct kvm_cpuid __user *cpuid_arg = argp;
3222 struct kvm_cpuid cpuid;
3223
3224 r = -EFAULT;
3225 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3226 goto out;
3227 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003228 break;
3229 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003230 case KVM_SET_CPUID2: {
3231 struct kvm_cpuid2 __user *cpuid_arg = argp;
3232 struct kvm_cpuid2 cpuid;
3233
3234 r = -EFAULT;
3235 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3236 goto out;
3237 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003238 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003239 break;
3240 }
3241 case KVM_GET_CPUID2: {
3242 struct kvm_cpuid2 __user *cpuid_arg = argp;
3243 struct kvm_cpuid2 cpuid;
3244
3245 r = -EFAULT;
3246 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3247 goto out;
3248 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003249 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003250 if (r)
3251 goto out;
3252 r = -EFAULT;
3253 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3254 goto out;
3255 r = 0;
3256 break;
3257 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003258 case KVM_GET_MSRS:
3259 r = msr_io(vcpu, argp, kvm_get_msr, 1);
3260 break;
3261 case KVM_SET_MSRS:
3262 r = msr_io(vcpu, argp, do_set_msr, 0);
3263 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02003264 case KVM_TPR_ACCESS_REPORTING: {
3265 struct kvm_tpr_access_ctl tac;
3266
3267 r = -EFAULT;
3268 if (copy_from_user(&tac, argp, sizeof tac))
3269 goto out;
3270 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3271 if (r)
3272 goto out;
3273 r = -EFAULT;
3274 if (copy_to_user(argp, &tac, sizeof tac))
3275 goto out;
3276 r = 0;
3277 break;
3278 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003279 case KVM_SET_VAPIC_ADDR: {
3280 struct kvm_vapic_addr va;
3281
3282 r = -EINVAL;
3283 if (!irqchip_in_kernel(vcpu->kvm))
3284 goto out;
3285 r = -EFAULT;
3286 if (copy_from_user(&va, argp, sizeof va))
3287 goto out;
Andy Honigfda4e2e2013-11-20 10:23:22 -08003288 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Avi Kivityb93463a2007-10-25 16:52:32 +02003289 break;
3290 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003291 case KVM_X86_SETUP_MCE: {
3292 u64 mcg_cap;
3293
3294 r = -EFAULT;
3295 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3296 goto out;
3297 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3298 break;
3299 }
3300 case KVM_X86_SET_MCE: {
3301 struct kvm_x86_mce mce;
3302
3303 r = -EFAULT;
3304 if (copy_from_user(&mce, argp, sizeof mce))
3305 goto out;
3306 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3307 break;
3308 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003309 case KVM_GET_VCPU_EVENTS: {
3310 struct kvm_vcpu_events events;
3311
3312 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3313
3314 r = -EFAULT;
3315 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3316 break;
3317 r = 0;
3318 break;
3319 }
3320 case KVM_SET_VCPU_EVENTS: {
3321 struct kvm_vcpu_events events;
3322
3323 r = -EFAULT;
3324 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3325 break;
3326
3327 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3328 break;
3329 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003330 case KVM_GET_DEBUGREGS: {
3331 struct kvm_debugregs dbgregs;
3332
3333 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3334
3335 r = -EFAULT;
3336 if (copy_to_user(argp, &dbgregs,
3337 sizeof(struct kvm_debugregs)))
3338 break;
3339 r = 0;
3340 break;
3341 }
3342 case KVM_SET_DEBUGREGS: {
3343 struct kvm_debugregs dbgregs;
3344
3345 r = -EFAULT;
3346 if (copy_from_user(&dbgregs, argp,
3347 sizeof(struct kvm_debugregs)))
3348 break;
3349
3350 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3351 break;
3352 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003353 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003354 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003355 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003356 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003357 break;
3358
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003359 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003360
3361 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003362 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003363 break;
3364 r = 0;
3365 break;
3366 }
3367 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003368 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Guo Chao18595412012-11-02 18:33:21 +08003369 if (IS_ERR(u.xsave))
3370 return PTR_ERR(u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003371
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003372 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003373 break;
3374 }
3375 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003376 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003377 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003378 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003379 break;
3380
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003381 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003382
3383 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003384 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003385 sizeof(struct kvm_xcrs)))
3386 break;
3387 r = 0;
3388 break;
3389 }
3390 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003391 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Guo Chao18595412012-11-02 18:33:21 +08003392 if (IS_ERR(u.xcrs))
3393 return PTR_ERR(u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003394
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003395 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003396 break;
3397 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003398 case KVM_SET_TSC_KHZ: {
3399 u32 user_tsc_khz;
3400
3401 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003402 user_tsc_khz = (u32)arg;
3403
3404 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3405 goto out;
3406
Zachary Amsdencc578282012-02-03 15:43:50 -02003407 if (user_tsc_khz == 0)
3408 user_tsc_khz = tsc_khz;
3409
3410 kvm_set_tsc_khz(vcpu, user_tsc_khz);
Joerg Roedel92a1f122011-03-25 09:44:51 +01003411
3412 r = 0;
3413 goto out;
3414 }
3415 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003416 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003417 goto out;
3418 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003419 case KVM_KVMCLOCK_CTRL: {
3420 r = kvm_set_guest_paused(vcpu);
3421 goto out;
3422 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003423 default:
3424 r = -EINVAL;
3425 }
3426out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003427 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003428 return r;
3429}
3430
Carsten Otte5b1c1492012-01-04 10:25:23 +01003431int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
3432{
3433 return VM_FAULT_SIGBUS;
3434}
3435
Carsten Otte1fe779f2007-10-29 16:08:35 +01003436static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
3437{
3438 int ret;
3439
3440 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08003441 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003442 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
3443 return ret;
3444}
3445
Sheng Yangb927a3c2009-07-21 10:42:48 +08003446static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
3447 u64 ident_addr)
3448{
3449 kvm->arch.ept_identity_map_addr = ident_addr;
3450 return 0;
3451}
3452
Carsten Otte1fe779f2007-10-29 16:08:35 +01003453static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
3454 u32 kvm_nr_mmu_pages)
3455{
3456 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
3457 return -EINVAL;
3458
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003459 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003460
3461 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003462 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003463
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003464 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003465 return 0;
3466}
3467
3468static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
3469{
Dave Hansen39de71e2010-08-19 18:11:14 -07003470 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003471}
3472
Carsten Otte1fe779f2007-10-29 16:08:35 +01003473static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3474{
3475 int r;
3476
3477 r = 0;
3478 switch (chip->chip_id) {
3479 case KVM_IRQCHIP_PIC_MASTER:
3480 memcpy(&chip->chip.pic,
3481 &pic_irqchip(kvm)->pics[0],
3482 sizeof(struct kvm_pic_state));
3483 break;
3484 case KVM_IRQCHIP_PIC_SLAVE:
3485 memcpy(&chip->chip.pic,
3486 &pic_irqchip(kvm)->pics[1],
3487 sizeof(struct kvm_pic_state));
3488 break;
3489 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003490 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003491 break;
3492 default:
3493 r = -EINVAL;
3494 break;
3495 }
3496 return r;
3497}
3498
3499static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3500{
3501 int r;
3502
3503 r = 0;
3504 switch (chip->chip_id) {
3505 case KVM_IRQCHIP_PIC_MASTER:
Avi Kivityf4f51052010-09-19 18:44:07 +02003506 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003507 memcpy(&pic_irqchip(kvm)->pics[0],
3508 &chip->chip.pic,
3509 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003510 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003511 break;
3512 case KVM_IRQCHIP_PIC_SLAVE:
Avi Kivityf4f51052010-09-19 18:44:07 +02003513 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003514 memcpy(&pic_irqchip(kvm)->pics[1],
3515 &chip->chip.pic,
3516 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003517 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003518 break;
3519 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003520 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003521 break;
3522 default:
3523 r = -EINVAL;
3524 break;
3525 }
3526 kvm_pic_update_irq(pic_irqchip(kvm));
3527 return r;
3528}
3529
Sheng Yange0f63cb2008-03-04 00:50:59 +08003530static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3531{
3532 int r = 0;
3533
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003534 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003535 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003536 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003537 return r;
3538}
3539
3540static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3541{
3542 int r = 0;
3543
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003544 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003545 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04003546 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
3547 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
3548 return r;
3549}
3550
3551static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3552{
3553 int r = 0;
3554
3555 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3556 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
3557 sizeof(ps->channels));
3558 ps->flags = kvm->arch.vpit->pit_state.flags;
3559 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003560 memset(&ps->reserved, 0, sizeof(ps->reserved));
Beth Kone9f42752009-07-07 11:50:38 -04003561 return r;
3562}
3563
3564static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3565{
3566 int r = 0, start = 0;
3567 u32 prev_legacy, cur_legacy;
3568 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3569 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
3570 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
3571 if (!prev_legacy && cur_legacy)
3572 start = 1;
3573 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
3574 sizeof(kvm->arch.vpit->pit_state.channels));
3575 kvm->arch.vpit->pit_state.flags = ps->flags;
3576 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003577 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003578 return r;
3579}
3580
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003581static int kvm_vm_ioctl_reinject(struct kvm *kvm,
3582 struct kvm_reinject_control *control)
3583{
3584 if (!kvm->arch.vpit)
3585 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003586 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Avi Kivity26ef1922012-07-26 18:01:53 +03003587 kvm->arch.vpit->pit_state.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003588 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003589 return 0;
3590}
3591
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003592/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003593 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
3594 * @kvm: kvm instance
3595 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003596 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003597 * We need to keep it in mind that VCPU threads can write to the bitmap
3598 * concurrently. So, to avoid losing data, we keep the following order for
3599 * each bit:
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003600 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003601 * 1. Take a snapshot of the bit and clear it if needed.
3602 * 2. Write protect the corresponding page.
3603 * 3. Flush TLB's if needed.
3604 * 4. Copy the snapshot to the userspace.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003605 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003606 * Between 2 and 3, the guest may write to the page using the remaining TLB
3607 * entry. This is not a problem because the page will be reported dirty at
3608 * step 4 using the snapshot taken before and step 3 ensures that successive
3609 * writes will be logged for the next call.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003610 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003611int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003612{
Takuya Yoshikawa7850ac52011-11-14 18:23:34 +09003613 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003614 struct kvm_memory_slot *memslot;
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003615 unsigned long n, i;
3616 unsigned long *dirty_bitmap;
3617 unsigned long *dirty_bitmap_buffer;
3618 bool is_dirty = false;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003619
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003620 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003621
Marcelo Tosattib050b012009-12-23 14:35:22 -02003622 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07003623 if (log->slot >= KVM_USER_MEM_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003624 goto out;
3625
Xiao Guangrong28a37542011-11-24 19:04:35 +08003626 memslot = id_to_memslot(kvm->memslots, log->slot);
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003627
3628 dirty_bitmap = memslot->dirty_bitmap;
Marcelo Tosattib050b012009-12-23 14:35:22 -02003629 r = -ENOENT;
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003630 if (!dirty_bitmap)
Marcelo Tosattib050b012009-12-23 14:35:22 -02003631 goto out;
3632
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09003633 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02003634
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003635 dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long);
3636 memset(dirty_bitmap_buffer, 0, n);
Marcelo Tosattib050b012009-12-23 14:35:22 -02003637
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003638 spin_lock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02003639
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003640 for (i = 0; i < n / sizeof(long); i++) {
3641 unsigned long mask;
3642 gfn_t offset;
Sasha Levincdfca7b2011-12-04 19:36:28 +02003643
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003644 if (!dirty_bitmap[i])
3645 continue;
Marcelo Tosattib050b012009-12-23 14:35:22 -02003646
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003647 is_dirty = true;
Takuya Yoshikawa914ebcc2010-04-28 18:50:36 +09003648
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003649 mask = xchg(&dirty_bitmap[i], 0);
3650 dirty_bitmap_buffer[i] = mask;
Michael S. Tsirkinedde99c2010-10-25 03:21:24 +02003651
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003652 offset = i * BITS_PER_LONG;
3653 kvm_mmu_write_protect_pt_masked(kvm, memslot, offset, mask);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003654 }
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003655
3656 spin_unlock(&kvm->mmu_lock);
3657
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003658 /* See the comments in kvm_mmu_slot_remove_write_access(). */
3659 lockdep_assert_held(&kvm->slots_lock);
3660
3661 /*
3662 * All the TLBs can be flushed out of mmu lock, see the comments in
3663 * kvm_mmu_slot_remove_write_access().
3664 */
3665 if (is_dirty)
3666 kvm_flush_remote_tlbs(kvm);
3667
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003668 r = -EFAULT;
3669 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
3670 goto out;
Marcelo Tosattib050b012009-12-23 14:35:22 -02003671
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003672 r = 0;
3673out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003674 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003675 return r;
3676}
3677
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003678int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
3679 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003680{
3681 if (!irqchip_in_kernel(kvm))
3682 return -ENXIO;
3683
3684 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003685 irq_event->irq, irq_event->level,
3686 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003687 return 0;
3688}
3689
Carsten Otte1fe779f2007-10-29 16:08:35 +01003690long kvm_arch_vm_ioctl(struct file *filp,
3691 unsigned int ioctl, unsigned long arg)
3692{
3693 struct kvm *kvm = filp->private_data;
3694 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03003695 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07003696 /*
3697 * This union makes it completely explicit to gcc-3.x
3698 * that these two variables' stack usage should be
3699 * combined, not added together.
3700 */
3701 union {
3702 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04003703 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003704 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07003705 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003706
3707 switch (ioctl) {
3708 case KVM_SET_TSS_ADDR:
3709 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003710 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003711 case KVM_SET_IDENTITY_MAP_ADDR: {
3712 u64 ident_addr;
3713
3714 r = -EFAULT;
3715 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
3716 goto out;
3717 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08003718 break;
3719 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003720 case KVM_SET_NR_MMU_PAGES:
3721 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003722 break;
3723 case KVM_GET_NR_MMU_PAGES:
3724 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3725 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003726 case KVM_CREATE_IRQCHIP: {
3727 struct kvm_pic *vpic;
3728
3729 mutex_lock(&kvm->lock);
3730 r = -EEXIST;
3731 if (kvm->arch.vpic)
3732 goto create_irqchip_unlock;
Avi Kivity3e515702012-03-05 14:23:29 +02003733 r = -EINVAL;
3734 if (atomic_read(&kvm->online_vcpus))
3735 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003736 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003737 vpic = kvm_create_pic(kvm);
3738 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003739 r = kvm_ioapic_init(kvm);
3740 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003741 mutex_lock(&kvm->slots_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003742 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
Sasha Levin743eeb02011-07-27 16:00:48 +03003743 &vpic->dev_master);
3744 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3745 &vpic->dev_slave);
3746 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3747 &vpic->dev_eclr);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003748 mutex_unlock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003749 kfree(vpic);
3750 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003751 }
3752 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003753 goto create_irqchip_unlock;
3754 smp_wmb();
3755 kvm->arch.vpic = vpic;
3756 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02003757 r = kvm_setup_default_irq_routing(kvm);
3758 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003759 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003760 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003761 kvm_ioapic_destroy(kvm);
3762 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003763 mutex_unlock(&kvm->irq_lock);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003764 mutex_unlock(&kvm->slots_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02003765 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003766 create_irqchip_unlock:
3767 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003768 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003769 }
Sheng Yang78376992008-01-28 05:10:22 +08003770 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003771 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3772 goto create_pit;
3773 case KVM_CREATE_PIT2:
3774 r = -EFAULT;
3775 if (copy_from_user(&u.pit_config, argp,
3776 sizeof(struct kvm_pit_config)))
3777 goto out;
3778 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003779 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003780 r = -EEXIST;
3781 if (kvm->arch.vpit)
3782 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003783 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003784 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003785 if (kvm->arch.vpit)
3786 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003787 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003788 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08003789 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003790 case KVM_GET_IRQCHIP: {
3791 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003792 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003793
Sasha Levinff5c2c02011-12-04 19:36:29 +02003794 chip = memdup_user(argp, sizeof(*chip));
3795 if (IS_ERR(chip)) {
3796 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003797 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003798 }
3799
Carsten Otte1fe779f2007-10-29 16:08:35 +01003800 r = -ENXIO;
3801 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003802 goto get_irqchip_out;
3803 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3804 if (r)
3805 goto get_irqchip_out;
3806 r = -EFAULT;
3807 if (copy_to_user(argp, chip, sizeof *chip))
3808 goto get_irqchip_out;
3809 r = 0;
3810 get_irqchip_out:
3811 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003812 break;
3813 }
3814 case KVM_SET_IRQCHIP: {
3815 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003816 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003817
Sasha Levinff5c2c02011-12-04 19:36:29 +02003818 chip = memdup_user(argp, sizeof(*chip));
3819 if (IS_ERR(chip)) {
3820 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003821 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003822 }
3823
Carsten Otte1fe779f2007-10-29 16:08:35 +01003824 r = -ENXIO;
3825 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003826 goto set_irqchip_out;
3827 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3828 if (r)
3829 goto set_irqchip_out;
3830 r = 0;
3831 set_irqchip_out:
3832 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003833 break;
3834 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003835 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003836 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003837 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003838 goto out;
3839 r = -ENXIO;
3840 if (!kvm->arch.vpit)
3841 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003842 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003843 if (r)
3844 goto out;
3845 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003846 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003847 goto out;
3848 r = 0;
3849 break;
3850 }
3851 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003852 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003853 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003854 goto out;
3855 r = -ENXIO;
3856 if (!kvm->arch.vpit)
3857 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003858 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003859 break;
3860 }
Beth Kone9f42752009-07-07 11:50:38 -04003861 case KVM_GET_PIT2: {
3862 r = -ENXIO;
3863 if (!kvm->arch.vpit)
3864 goto out;
3865 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3866 if (r)
3867 goto out;
3868 r = -EFAULT;
3869 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3870 goto out;
3871 r = 0;
3872 break;
3873 }
3874 case KVM_SET_PIT2: {
3875 r = -EFAULT;
3876 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3877 goto out;
3878 r = -ENXIO;
3879 if (!kvm->arch.vpit)
3880 goto out;
3881 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04003882 break;
3883 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003884 case KVM_REINJECT_CONTROL: {
3885 struct kvm_reinject_control control;
3886 r = -EFAULT;
3887 if (copy_from_user(&control, argp, sizeof(control)))
3888 goto out;
3889 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003890 break;
3891 }
Ed Swierkffde22a2009-10-15 15:21:43 -07003892 case KVM_XEN_HVM_CONFIG: {
3893 r = -EFAULT;
3894 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3895 sizeof(struct kvm_xen_hvm_config)))
3896 goto out;
3897 r = -EINVAL;
3898 if (kvm->arch.xen_hvm_config.flags)
3899 goto out;
3900 r = 0;
3901 break;
3902 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003903 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003904 struct kvm_clock_data user_ns;
3905 u64 now_ns;
3906 s64 delta;
3907
3908 r = -EFAULT;
3909 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3910 goto out;
3911
3912 r = -EINVAL;
3913 if (user_ns.flags)
3914 goto out;
3915
3916 r = 0;
Avi Kivity395c6b02010-10-04 12:55:49 +02003917 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003918 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003919 delta = user_ns.clock - now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003920 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003921 kvm->arch.kvmclock_offset = delta;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03003922 kvm_gen_update_masterclock(kvm);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003923 break;
3924 }
3925 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003926 struct kvm_clock_data user_ns;
3927 u64 now_ns;
3928
Avi Kivity395c6b02010-10-04 12:55:49 +02003929 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003930 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003931 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003932 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003933 user_ns.flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003934 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003935
3936 r = -EFAULT;
3937 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3938 goto out;
3939 r = 0;
3940 break;
3941 }
3942
Carsten Otte1fe779f2007-10-29 16:08:35 +01003943 default:
3944 ;
3945 }
3946out:
3947 return r;
3948}
3949
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003950static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003951{
3952 u32 dummy[2];
3953 unsigned i, j;
3954
Glauber Costae3267cb2009-10-06 13:24:50 -04003955 /* skip the first msrs in the list. KVM-specific */
3956 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003957 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3958 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003959
3960 /*
3961 * Even MSRs that are valid in the host may not be exposed
3962 * to the guests in some cases. We could work around this
3963 * in VMX with the generic MSR save/load machinery, but it
3964 * is not really worthwhile since it will really only
3965 * happen with nested virtualization.
3966 */
3967 switch (msrs_to_save[i]) {
3968 case MSR_IA32_BNDCFGS:
3969 if (!kvm_x86_ops->mpx_supported())
3970 continue;
3971 break;
3972 default:
3973 break;
3974 }
3975
Carsten Otte043405e2007-10-10 17:16:19 +02003976 if (j < i)
3977 msrs_to_save[j] = msrs_to_save[i];
3978 j++;
3979 }
3980 num_msrs_to_save = j;
3981}
3982
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003983static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3984 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003985{
Avi Kivity70252a12010-01-19 12:51:22 +02003986 int handled = 0;
3987 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003988
Avi Kivity70252a12010-01-19 12:51:22 +02003989 do {
3990 n = min(len, 8);
3991 if (!(vcpu->arch.apic &&
3992 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
3993 && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
3994 break;
3995 handled += n;
3996 addr += n;
3997 len -= n;
3998 v += n;
3999 } while (len);
4000
4001 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004002}
4003
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004004static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004005{
Avi Kivity70252a12010-01-19 12:51:22 +02004006 int handled = 0;
4007 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004008
Avi Kivity70252a12010-01-19 12:51:22 +02004009 do {
4010 n = min(len, 8);
4011 if (!(vcpu->arch.apic &&
4012 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
4013 && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
4014 break;
4015 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
4016 handled += n;
4017 addr += n;
4018 len -= n;
4019 v += n;
4020 } while (len);
4021
4022 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004023}
4024
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004025static void kvm_set_segment(struct kvm_vcpu *vcpu,
4026 struct kvm_segment *var, int seg)
4027{
4028 kvm_x86_ops->set_segment(vcpu, var, seg);
4029}
4030
4031void kvm_get_segment(struct kvm_vcpu *vcpu,
4032 struct kvm_segment *var, int seg)
4033{
4034 kvm_x86_ops->get_segment(vcpu, var, seg);
4035}
4036
Xiao Guangronge459e322011-11-28 20:42:16 +08004037gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004038{
4039 gpa_t t_gpa;
Avi Kivityab9ae312010-11-22 17:53:26 +02004040 struct x86_exception exception;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004041
4042 BUG_ON(!mmu_is_nested(vcpu));
4043
4044 /* NPT walks are always user-walks */
4045 access |= PFERR_USER_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004046 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, &exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004047
4048 return t_gpa;
4049}
4050
Avi Kivityab9ae312010-11-22 17:53:26 +02004051gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
4052 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004053{
4054 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02004055 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004056}
4057
Avi Kivityab9ae312010-11-22 17:53:26 +02004058 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4059 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004060{
4061 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4062 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004063 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004064}
4065
Avi Kivityab9ae312010-11-22 17:53:26 +02004066gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4067 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004068{
4069 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4070 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004071 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004072}
4073
4074/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004075gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4076 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004077{
Avi Kivityab9ae312010-11-22 17:53:26 +02004078 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004079}
4080
4081static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4082 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004083 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004084{
4085 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004086 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004087
4088 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004089 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004090 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004091 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004092 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004093 int ret;
4094
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004095 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004096 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004097 ret = kvm_read_guest_page(vcpu->kvm, gpa >> PAGE_SHIFT, data,
4098 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004099 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004100 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004101 goto out;
4102 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004103
Izik Eidus77c20022008-12-29 01:42:19 +02004104 bytes -= toread;
4105 data += toread;
4106 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004107 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004108out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004109 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004110}
Izik Eidus77c20022008-12-29 01:42:19 +02004111
Gleb Natapov1871c602010-02-10 14:21:32 +02004112/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004113static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4114 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004115 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004116{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004117 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004118 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004119 unsigned offset;
4120 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004121
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004122 /* Inline kvm_read_guest_virt_helper for speed. */
4123 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4124 exception);
4125 if (unlikely(gpa == UNMAPPED_GVA))
4126 return X86EMUL_PROPAGATE_FAULT;
4127
4128 offset = addr & (PAGE_SIZE-1);
4129 if (WARN_ON(offset + bytes > PAGE_SIZE))
4130 bytes = (unsigned)PAGE_SIZE - offset;
4131 ret = kvm_read_guest_page(vcpu->kvm, gpa >> PAGE_SHIFT, val,
4132 offset, bytes);
4133 if (unlikely(ret < 0))
4134 return X86EMUL_IO_NEEDED;
4135
4136 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004137}
4138
Nadav Har'El064aea72011-05-25 23:04:56 +03004139int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004140 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004141 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004142{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004143 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004144 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004145
Gleb Natapov1871c602010-02-10 14:21:32 +02004146 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004147 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004148}
Nadav Har'El064aea72011-05-25 23:04:56 +03004149EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004150
Avi Kivity0f65dd72011-04-20 13:37:53 +03004151static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
4152 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004153 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004154{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004155 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004156 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004157}
4158
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004159int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004160 gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004161 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004162 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004163{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004164 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Izik Eidus77c20022008-12-29 01:42:19 +02004165 void *data = val;
4166 int r = X86EMUL_CONTINUE;
4167
4168 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004169 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
4170 PFERR_WRITE_MASK,
Avi Kivityab9ae312010-11-22 17:53:26 +02004171 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004172 unsigned offset = addr & (PAGE_SIZE-1);
4173 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4174 int ret;
4175
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004176 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004177 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus77c20022008-12-29 01:42:19 +02004178 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
4179 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004180 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004181 goto out;
4182 }
4183
4184 bytes -= towrite;
4185 data += towrite;
4186 addr += towrite;
4187 }
4188out:
4189 return r;
4190}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004191EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004192
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004193static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
4194 gpa_t *gpa, struct x86_exception *exception,
4195 bool write)
4196{
Avi Kivity97d64b72012-09-12 14:52:00 +03004197 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
4198 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004199
Avi Kivity97d64b72012-09-12 14:52:00 +03004200 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08004201 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
4202 vcpu->arch.access, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004203 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
4204 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08004205 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004206 return 1;
4207 }
4208
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004209 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
4210
4211 if (*gpa == UNMAPPED_GVA)
4212 return -1;
4213
4214 /* For APIC access vmexit */
4215 if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4216 return 1;
4217
Xiao Guangrong4f022642011-07-12 03:34:24 +08004218 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
4219 trace_vcpu_match_mmio(gva, *gpa, write, true);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004220 return 1;
Xiao Guangrong4f022642011-07-12 03:34:24 +08004221 }
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004222
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004223 return 0;
4224}
4225
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004226int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004227 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02004228{
4229 int ret;
4230
4231 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
4232 if (ret < 0)
4233 return 0;
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004234 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004235 return 1;
4236}
4237
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004238struct read_write_emulator_ops {
4239 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
4240 int bytes);
4241 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
4242 void *val, int bytes);
4243 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4244 int bytes, void *val);
4245 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4246 void *val, int bytes);
4247 bool write;
4248};
4249
4250static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
4251{
4252 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004253 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Avi Kivityf78146b2012-04-18 19:22:47 +03004254 vcpu->mmio_fragments[0].gpa, *(u64 *)val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004255 vcpu->mmio_read_completed = 0;
4256 return 1;
4257 }
4258
4259 return 0;
4260}
4261
4262static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4263 void *val, int bytes)
4264{
4265 return !kvm_read_guest(vcpu->kvm, gpa, val, bytes);
4266}
4267
4268static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4269 void *val, int bytes)
4270{
4271 return emulator_write_phys(vcpu, gpa, val, bytes);
4272}
4273
4274static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
4275{
4276 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4277 return vcpu_mmio_write(vcpu, gpa, bytes, val);
4278}
4279
4280static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4281 void *val, int bytes)
4282{
4283 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4284 return X86EMUL_IO_NEEDED;
4285}
4286
4287static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4288 void *val, int bytes)
4289{
Avi Kivityf78146b2012-04-18 19:22:47 +03004290 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
4291
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004292 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004293 return X86EMUL_CONTINUE;
4294}
4295
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004296static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004297 .read_write_prepare = read_prepare,
4298 .read_write_emulate = read_emulate,
4299 .read_write_mmio = vcpu_mmio_read,
4300 .read_write_exit_mmio = read_exit_mmio,
4301};
4302
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004303static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004304 .read_write_emulate = write_emulate,
4305 .read_write_mmio = write_mmio,
4306 .read_write_exit_mmio = write_exit_mmio,
4307 .write = true,
4308};
4309
Xiao Guangrong22388a32011-07-13 14:32:31 +08004310static int emulator_read_write_onepage(unsigned long addr, void *val,
4311 unsigned int bytes,
4312 struct x86_exception *exception,
4313 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004314 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004315{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004316 gpa_t gpa;
4317 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004318 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03004319 struct kvm_mmio_fragment *frag;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004320
4321 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004322
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004323 if (ret < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004324 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004325
4326 /* For APIC access vmexit */
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004327 if (ret)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004328 goto mmio;
4329
Xiao Guangrong22388a32011-07-13 14:32:31 +08004330 if (ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004331 return X86EMUL_CONTINUE;
4332
4333mmio:
4334 /*
4335 * Is this MMIO handled locally?
4336 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08004337 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02004338 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004339 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004340
Avi Kivity70252a12010-01-19 12:51:22 +02004341 gpa += handled;
4342 bytes -= handled;
4343 val += handled;
4344
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004345 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
4346 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
4347 frag->gpa = gpa;
4348 frag->data = val;
4349 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03004350 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004351}
4352
Xiao Guangrong22388a32011-07-13 14:32:31 +08004353int emulator_read_write(struct x86_emulate_ctxt *ctxt, unsigned long addr,
4354 void *val, unsigned int bytes,
4355 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004356 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004357{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004358 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03004359 gpa_t gpa;
4360 int rc;
4361
4362 if (ops->read_write_prepare &&
4363 ops->read_write_prepare(vcpu, val, bytes))
4364 return X86EMUL_CONTINUE;
4365
4366 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004367
Carsten Ottebbd9b642007-10-30 18:44:21 +01004368 /* Crossing a page boundary? */
4369 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03004370 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004371
4372 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004373 rc = emulator_read_write_onepage(addr, val, now, exception,
4374 vcpu, ops);
4375
Carsten Ottebbd9b642007-10-30 18:44:21 +01004376 if (rc != X86EMUL_CONTINUE)
4377 return rc;
4378 addr += now;
4379 val += now;
4380 bytes -= now;
4381 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08004382
Avi Kivityf78146b2012-04-18 19:22:47 +03004383 rc = emulator_read_write_onepage(addr, val, bytes, exception,
4384 vcpu, ops);
4385 if (rc != X86EMUL_CONTINUE)
4386 return rc;
4387
4388 if (!vcpu->mmio_nr_fragments)
4389 return rc;
4390
4391 gpa = vcpu->mmio_fragments[0].gpa;
4392
4393 vcpu->mmio_needed = 1;
4394 vcpu->mmio_cur_fragment = 0;
4395
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004396 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03004397 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
4398 vcpu->run->exit_reason = KVM_EXIT_MMIO;
4399 vcpu->run->mmio.phys_addr = gpa;
4400
4401 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08004402}
4403
4404static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4405 unsigned long addr,
4406 void *val,
4407 unsigned int bytes,
4408 struct x86_exception *exception)
4409{
4410 return emulator_read_write(ctxt, addr, val, bytes,
4411 exception, &read_emultor);
4412}
4413
4414int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
4415 unsigned long addr,
4416 const void *val,
4417 unsigned int bytes,
4418 struct x86_exception *exception)
4419{
4420 return emulator_read_write(ctxt, addr, (void *)val, bytes,
4421 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004422}
Carsten Ottebbd9b642007-10-30 18:44:21 +01004423
Avi Kivitydaea3e72010-03-15 13:59:54 +02004424#define CMPXCHG_TYPE(t, ptr, old, new) \
4425 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
4426
4427#ifdef CONFIG_X86_64
4428# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
4429#else
4430# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01004431 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02004432#endif
4433
Avi Kivity0f65dd72011-04-20 13:37:53 +03004434static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
4435 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004436 const void *old,
4437 const void *new,
4438 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004439 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004440{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004441 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004442 gpa_t gpa;
4443 struct page *page;
4444 char *kaddr;
4445 bool exchanged;
4446
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004447 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02004448 if (bytes > 8 || (bytes & (bytes - 1)))
4449 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004450
Avi Kivitydaea3e72010-03-15 13:59:54 +02004451 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004452
Avi Kivitydaea3e72010-03-15 13:59:54 +02004453 if (gpa == UNMAPPED_GVA ||
4454 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4455 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004456
Avi Kivitydaea3e72010-03-15 13:59:54 +02004457 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
4458 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004459
Avi Kivitydaea3e72010-03-15 13:59:54 +02004460 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08004461 if (is_error_page(page))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08004462 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004463
Cong Wang8fd75e12011-11-25 23:14:17 +08004464 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004465 kaddr += offset_in_page(gpa);
4466 switch (bytes) {
4467 case 1:
4468 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
4469 break;
4470 case 2:
4471 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
4472 break;
4473 case 4:
4474 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
4475 break;
4476 case 8:
4477 exchanged = CMPXCHG64(kaddr, old, new);
4478 break;
4479 default:
4480 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004481 }
Cong Wang8fd75e12011-11-25 23:14:17 +08004482 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004483 kvm_release_page_dirty(page);
4484
4485 if (!exchanged)
4486 return X86EMUL_CMPXCHG_FAILED;
4487
Marcelo Tosattid3714012014-02-25 22:44:54 -03004488 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004489 kvm_mmu_pte_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004490
4491 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02004492
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004493emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02004494 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004495
Avi Kivity0f65dd72011-04-20 13:37:53 +03004496 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004497}
4498
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004499static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
4500{
4501 /* TODO: String I/O for in kernel device */
4502 int r;
4503
4504 if (vcpu->arch.pio.in)
4505 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
4506 vcpu->arch.pio.size, pd);
4507 else
4508 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
4509 vcpu->arch.pio.port, vcpu->arch.pio.size,
4510 pd);
4511 return r;
4512}
4513
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004514static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
4515 unsigned short port, void *val,
4516 unsigned int count, bool in)
4517{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004518 vcpu->arch.pio.port = port;
4519 vcpu->arch.pio.in = in;
4520 vcpu->arch.pio.count = count;
4521 vcpu->arch.pio.size = size;
4522
4523 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
4524 vcpu->arch.pio.count = 0;
4525 return 1;
4526 }
4527
4528 vcpu->run->exit_reason = KVM_EXIT_IO;
4529 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
4530 vcpu->run->io.size = size;
4531 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
4532 vcpu->run->io.count = count;
4533 vcpu->run->io.port = port;
4534
4535 return 0;
4536}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004537
Avi Kivityca1d4a92011-04-20 13:37:53 +03004538static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
4539 int size, unsigned short port, void *val,
4540 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004541{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004542 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004543 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03004544
Gleb Natapov79729952010-03-18 15:20:24 +02004545 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004546 goto data_avail;
4547
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004548 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
4549 if (ret) {
4550data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004551 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004552 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02004553 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004554 return 1;
4555 }
4556
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004557 return 0;
4558}
4559
Avi Kivityca1d4a92011-04-20 13:37:53 +03004560static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
4561 int size, unsigned short port,
4562 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004563{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004564 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4565
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004566 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004567 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004568 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004569}
4570
Carsten Ottebbd9b642007-10-30 18:44:21 +01004571static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
4572{
4573 return kvm_x86_ops->get_segment_base(vcpu, seg);
4574}
4575
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004576static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004577{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004578 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004579}
4580
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004581int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
4582{
4583 if (!need_emulate_wbinvd(vcpu))
4584 return X86EMUL_CONTINUE;
4585
4586 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01004587 int cpu = get_cpu();
4588
4589 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004590 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
4591 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004592 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004593 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004594 } else
4595 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004596 return X86EMUL_CONTINUE;
4597}
4598EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
4599
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004600static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
4601{
4602 kvm_emulate_wbinvd(emul_to_vcpu(ctxt));
4603}
4604
Avi Kivity717746e2011-04-20 13:37:53 +03004605int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004606{
Avi Kivity717746e2011-04-20 13:37:53 +03004607 return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004608}
4609
Avi Kivity717746e2011-04-20 13:37:53 +03004610int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004611{
Gleb Natapov338dbc92010-04-28 19:15:32 +03004612
Avi Kivity717746e2011-04-20 13:37:53 +03004613 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004614}
4615
Gleb Natapov52a46612010-03-18 15:20:03 +02004616static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4617{
4618 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4619}
4620
Avi Kivity717746e2011-04-20 13:37:53 +03004621static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02004622{
Avi Kivity717746e2011-04-20 13:37:53 +03004623 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02004624 unsigned long value;
4625
4626 switch (cr) {
4627 case 0:
4628 value = kvm_read_cr0(vcpu);
4629 break;
4630 case 2:
4631 value = vcpu->arch.cr2;
4632 break;
4633 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004634 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02004635 break;
4636 case 4:
4637 value = kvm_read_cr4(vcpu);
4638 break;
4639 case 8:
4640 value = kvm_get_cr8(vcpu);
4641 break;
4642 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004643 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02004644 return 0;
4645 }
4646
4647 return value;
4648}
4649
Avi Kivity717746e2011-04-20 13:37:53 +03004650static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02004651{
Avi Kivity717746e2011-04-20 13:37:53 +03004652 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03004653 int res = 0;
4654
Gleb Natapov52a46612010-03-18 15:20:03 +02004655 switch (cr) {
4656 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03004657 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004658 break;
4659 case 2:
4660 vcpu->arch.cr2 = val;
4661 break;
4662 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004663 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004664 break;
4665 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03004666 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004667 break;
4668 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004669 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004670 break;
4671 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004672 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03004673 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02004674 }
Gleb Natapov0f122442010-04-28 19:15:31 +03004675
4676 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02004677}
4678
Avi Kivity717746e2011-04-20 13:37:53 +03004679static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02004680{
Avi Kivity717746e2011-04-20 13:37:53 +03004681 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02004682}
4683
Avi Kivity4bff1e862011-04-20 13:37:53 +03004684static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004685{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004686 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004687}
4688
Avi Kivity4bff1e862011-04-20 13:37:53 +03004689static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004690{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004691 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004692}
4693
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004694static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4695{
4696 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
4697}
4698
4699static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4700{
4701 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
4702}
4703
Avi Kivity4bff1e862011-04-20 13:37:53 +03004704static unsigned long emulator_get_cached_segment_base(
4705 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03004706{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004707 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03004708}
4709
Avi Kivity1aa36612011-04-27 13:20:30 +03004710static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
4711 struct desc_struct *desc, u32 *base3,
4712 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004713{
4714 struct kvm_segment var;
4715
Avi Kivity4bff1e862011-04-20 13:37:53 +03004716 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03004717 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004718
Gleb Natapov378a8b02013-01-21 15:36:48 +02004719 if (var.unusable) {
4720 memset(desc, 0, sizeof(*desc));
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004721 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02004722 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004723
4724 if (var.g)
4725 var.limit >>= 12;
4726 set_desc_limit(desc, var.limit);
4727 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02004728#ifdef CONFIG_X86_64
4729 if (base3)
4730 *base3 = var.base >> 32;
4731#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004732 desc->type = var.type;
4733 desc->s = var.s;
4734 desc->dpl = var.dpl;
4735 desc->p = var.present;
4736 desc->avl = var.avl;
4737 desc->l = var.l;
4738 desc->d = var.db;
4739 desc->g = var.g;
4740
4741 return true;
4742}
4743
Avi Kivity1aa36612011-04-27 13:20:30 +03004744static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
4745 struct desc_struct *desc, u32 base3,
4746 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004747{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004748 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004749 struct kvm_segment var;
4750
Avi Kivity1aa36612011-04-27 13:20:30 +03004751 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004752 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02004753#ifdef CONFIG_X86_64
4754 var.base |= ((u64)base3) << 32;
4755#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004756 var.limit = get_desc_limit(desc);
4757 if (desc->g)
4758 var.limit = (var.limit << 12) | 0xfff;
4759 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004760 var.dpl = desc->dpl;
4761 var.db = desc->d;
4762 var.s = desc->s;
4763 var.l = desc->l;
4764 var.g = desc->g;
4765 var.avl = desc->avl;
4766 var.present = desc->p;
4767 var.unusable = !var.present;
4768 var.padding = 0;
4769
4770 kvm_set_segment(vcpu, &var, seg);
4771 return;
4772}
4773
Avi Kivity717746e2011-04-20 13:37:53 +03004774static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
4775 u32 msr_index, u64 *pdata)
4776{
4777 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
4778}
4779
4780static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
4781 u32 msr_index, u64 data)
4782{
Will Auld8fe8ab42012-11-29 12:42:12 -08004783 struct msr_data msr;
4784
4785 msr.data = data;
4786 msr.index = msr_index;
4787 msr.host_initiated = false;
4788 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03004789}
4790
Nadav Amit67f4d422014-06-02 18:34:09 +03004791static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
4792 u32 pmc)
4793{
4794 return kvm_pmu_check_pmc(emul_to_vcpu(ctxt), pmc);
4795}
4796
Avi Kivity222d21a2011-11-10 14:57:30 +02004797static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
4798 u32 pmc, u64 *pdata)
4799{
4800 return kvm_pmu_read_pmc(emul_to_vcpu(ctxt), pmc, pdata);
4801}
4802
Avi Kivity6c3287f2011-04-20 15:43:05 +03004803static void emulator_halt(struct x86_emulate_ctxt *ctxt)
4804{
4805 emul_to_vcpu(ctxt)->arch.halt_request = 1;
4806}
4807
Avi Kivity5037f6f2011-03-28 16:53:59 +02004808static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
4809{
4810 preempt_disable();
Avi Kivity5197b802011-04-20 15:55:40 +03004811 kvm_load_guest_fpu(emul_to_vcpu(ctxt));
Avi Kivity5037f6f2011-03-28 16:53:59 +02004812 /*
4813 * CR0.TS may reference the host fpu state, not the guest fpu state,
4814 * so it may be clear at this point.
4815 */
4816 clts();
4817}
4818
4819static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
4820{
4821 preempt_enable();
4822}
4823
Avi Kivity29535382011-04-20 13:37:53 +03004824static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004825 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004826 enum x86_intercept_stage stage)
4827{
Avi Kivity29535382011-04-20 13:37:53 +03004828 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004829}
4830
Avi Kivity0017f932012-06-07 14:10:16 +03004831static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004832 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
4833{
Avi Kivity0017f932012-06-07 14:10:16 +03004834 kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004835}
4836
Avi Kivitydd856ef2012-08-27 23:46:17 +03004837static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
4838{
4839 return kvm_register_read(emul_to_vcpu(ctxt), reg);
4840}
4841
4842static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
4843{
4844 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
4845}
4846
Mathias Krause0225fb52012-08-30 01:30:16 +02004847static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03004848 .read_gpr = emulator_read_gpr,
4849 .write_gpr = emulator_write_gpr,
Gleb Natapov1871c602010-02-10 14:21:32 +02004850 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004851 .write_std = kvm_write_guest_virt_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02004852 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004853 .read_emulated = emulator_read_emulated,
4854 .write_emulated = emulator_write_emulated,
4855 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004856 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004857 .pio_in_emulated = emulator_pio_in_emulated,
4858 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03004859 .get_segment = emulator_get_segment,
4860 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03004861 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004862 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004863 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004864 .set_gdt = emulator_set_gdt,
4865 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02004866 .get_cr = emulator_get_cr,
4867 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02004868 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03004869 .get_dr = emulator_get_dr,
4870 .set_dr = emulator_set_dr,
Avi Kivity717746e2011-04-20 13:37:53 +03004871 .set_msr = emulator_set_msr,
4872 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03004873 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02004874 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03004875 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004876 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03004877 .fix_hypercall = emulator_fix_hypercall,
Avi Kivity5037f6f2011-03-28 16:53:59 +02004878 .get_fpu = emulator_get_fpu,
4879 .put_fpu = emulator_put_fpu,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004880 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004881 .get_cpuid = emulator_get_cpuid,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004882};
4883
Gleb Natapov95cb2292010-04-28 19:15:43 +03004884static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
4885{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004886 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03004887 /*
4888 * an sti; sti; sequence only disable interrupts for the first
4889 * instruction. So, if the last instruction, be it emulated or
4890 * not, left the system with the INT_STI flag enabled, it
4891 * means that the last instruction is an sti. We should not
4892 * leave the flag on in this case. The same goes for mov ss
4893 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004894 if (int_shadow & mask)
4895 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004896 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004897 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004898 if (!mask)
4899 kvm_make_request(KVM_REQ_EVENT, vcpu);
4900 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03004901}
4902
Gleb Natapov54b84862010-04-28 19:15:44 +03004903static void inject_emulated_exception(struct kvm_vcpu *vcpu)
4904{
4905 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02004906 if (ctxt->exception.vector == PF_VECTOR)
Avi Kivity6389ee92010-11-29 16:12:30 +02004907 kvm_propagate_fault(vcpu, &ctxt->exception);
Avi Kivityda9cb572010-11-22 17:53:21 +02004908 else if (ctxt->exception.error_code_valid)
4909 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
4910 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03004911 else
Avi Kivityda9cb572010-11-22 17:53:21 +02004912 kvm_queue_exception(vcpu, ctxt->exception.vector);
Gleb Natapov54b84862010-04-28 19:15:44 +03004913}
4914
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004915static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
4916{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004917 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004918 int cs_db, cs_l;
4919
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004920 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4921
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004922 ctxt->eflags = kvm_get_rflags(vcpu);
4923 ctxt->eip = kvm_rip_read(vcpu);
4924 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
4925 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03004926 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004927 cs_db ? X86EMUL_MODE_PROT32 :
4928 X86EMUL_MODE_PROT16;
4929 ctxt->guest_mode = is_guest_mode(vcpu);
4930
Avi Kivitydd856ef2012-08-27 23:46:17 +03004931 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02004932 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004933}
4934
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004935int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02004936{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004937 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02004938 int ret;
4939
4940 init_emulate_ctxt(vcpu);
4941
Avi Kivity9dac77f2011-06-01 15:34:25 +03004942 ctxt->op_bytes = 2;
4943 ctxt->ad_bytes = 2;
4944 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004945 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02004946
4947 if (ret != X86EMUL_CONTINUE)
4948 return EMULATE_FAIL;
4949
Avi Kivity9dac77f2011-06-01 15:34:25 +03004950 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004951 kvm_rip_write(vcpu, ctxt->eip);
4952 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02004953
4954 if (irq == NMI_VECTOR)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004955 vcpu->arch.nmi_pending = 0;
Mohammed Gamal63995652010-09-19 14:34:06 +02004956 else
4957 vcpu->arch.interrupt.pending = false;
4958
4959 return EMULATE_DONE;
4960}
4961EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
4962
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004963static int handle_emulation_failure(struct kvm_vcpu *vcpu)
4964{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004965 int r = EMULATE_DONE;
4966
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004967 ++vcpu->stat.insn_emulation_fail;
4968 trace_kvm_emulate_insn_failed(vcpu);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004969 if (!is_guest_mode(vcpu)) {
4970 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4971 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4972 vcpu->run->internal.ndata = 0;
4973 r = EMULATE_FAIL;
4974 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004975 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004976
4977 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004978}
4979
Xiao Guangrong93c05d32013-01-13 23:49:07 +08004980static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03004981 bool write_fault_to_shadow_pgtable,
4982 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03004983{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08004984 gpa_t gpa = cr2;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08004985 pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03004986
Gleb Natapov991eebf2013-04-11 12:10:51 +03004987 if (emulation_type & EMULTYPE_NO_REEXECUTE)
4988 return false;
4989
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08004990 if (!vcpu->arch.mmu.direct_map) {
4991 /*
4992 * Write permission should be allowed since only
4993 * write access need to be emulated.
4994 */
4995 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03004996
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08004997 /*
4998 * If the mapping is invalid in guest, let cpu retry
4999 * it to generate fault.
5000 */
5001 if (gpa == UNMAPPED_GVA)
5002 return true;
5003 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005004
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005005 /*
5006 * Do not retry the unhandleable instruction if it faults on the
5007 * readonly host memory, otherwise it will goto a infinite loop:
5008 * retry instruction -> write #PF -> emulation fail -> retry
5009 * instruction -> ...
5010 */
5011 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005012
5013 /*
5014 * If the instruction failed on the error pfn, it can not be fixed,
5015 * report the error to userspace.
5016 */
5017 if (is_error_noslot_pfn(pfn))
5018 return false;
5019
5020 kvm_release_pfn_clean(pfn);
5021
5022 /* The instructions are well-emulated on direct mmu. */
5023 if (vcpu->arch.mmu.direct_map) {
5024 unsigned int indirect_shadow_pages;
5025
5026 spin_lock(&vcpu->kvm->mmu_lock);
5027 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5028 spin_unlock(&vcpu->kvm->mmu_lock);
5029
5030 if (indirect_shadow_pages)
5031 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5032
Gleb Natapova6f177e2010-07-08 12:41:12 +03005033 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005034 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005035
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005036 /*
5037 * if emulation was due to access to shadowed page table
5038 * and it failed try to unshadow page and re-enter the
5039 * guest to let CPU execute the instruction.
5040 */
5041 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005042
5043 /*
5044 * If the access faults on its page table, it can not
5045 * be fixed by unprotecting shadow page and it should
5046 * be reported to userspace.
5047 */
5048 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005049}
5050
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005051static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5052 unsigned long cr2, int emulation_type)
5053{
5054 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5055 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5056
5057 last_retry_eip = vcpu->arch.last_retry_eip;
5058 last_retry_addr = vcpu->arch.last_retry_addr;
5059
5060 /*
5061 * If the emulation is caused by #PF and it is non-page_table
5062 * writing instruction, it means the VM-EXIT is caused by shadow
5063 * page protected, we can zap the shadow page and retry this
5064 * instruction directly.
5065 *
5066 * Note: if the guest uses a non-page-table modifying instruction
5067 * on the PDE that points to the instruction, then we will unmap
5068 * the instruction and go to an infinite loop. So, we cache the
5069 * last retried eip and the last fault address, if we meet the eip
5070 * and the address again, we can break out of the potential infinite
5071 * loop.
5072 */
5073 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5074
5075 if (!(emulation_type & EMULTYPE_RETRY))
5076 return false;
5077
5078 if (x86_page_table_writing_insn(ctxt))
5079 return false;
5080
5081 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5082 return false;
5083
5084 vcpu->arch.last_retry_eip = ctxt->eip;
5085 vcpu->arch.last_retry_addr = cr2;
5086
5087 if (!vcpu->arch.mmu.direct_map)
5088 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5089
Xiao Guangrong22368022013-01-13 23:44:12 +08005090 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005091
5092 return true;
5093}
5094
Gleb Natapov716d51a2012-09-03 15:24:26 +03005095static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5096static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5097
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005098static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
5099 unsigned long *db)
5100{
5101 u32 dr6 = 0;
5102 int i;
5103 u32 enable, rwlen;
5104
5105 enable = dr7;
5106 rwlen = dr7 >> 16;
5107 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
5108 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
5109 dr6 |= (1 << i);
5110 return dr6;
5111}
5112
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005113static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005114{
5115 struct kvm_run *kvm_run = vcpu->run;
5116
5117 /*
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005118 * rflags is the old, "raw" value of the flags. The new value has
5119 * not been saved yet.
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005120 *
5121 * This is correct even for TF set by the guest, because "the
5122 * processor will not generate this exception after the instruction
5123 * that sets the TF flag".
5124 */
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005125 if (unlikely(rflags & X86_EFLAGS_TF)) {
5126 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005127 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5128 DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005129 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5130 kvm_run->debug.arch.exception = DB_VECTOR;
5131 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5132 *r = EMULATE_USER_EXIT;
5133 } else {
5134 vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5135 /*
5136 * "Certain debug exceptions may clear bit 0-3. The
5137 * remaining contents of the DR6 register are never
5138 * cleared by the processor".
5139 */
5140 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005141 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005142 kvm_queue_exception(vcpu, DB_VECTOR);
5143 }
5144 }
5145}
5146
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005147static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
5148{
5149 struct kvm_run *kvm_run = vcpu->run;
5150 unsigned long eip = vcpu->arch.emulate_ctxt.eip;
5151 u32 dr6 = 0;
5152
5153 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
5154 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
5155 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
5156 vcpu->arch.guest_debug_dr7,
5157 vcpu->arch.eff_db);
5158
5159 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005160 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005161 kvm_run->debug.arch.pc = kvm_rip_read(vcpu) +
5162 get_segment_base(vcpu, VCPU_SREG_CS);
5163
5164 kvm_run->debug.arch.exception = DB_VECTOR;
5165 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5166 *r = EMULATE_USER_EXIT;
5167 return true;
5168 }
5169 }
5170
Nadav Amit4161a562014-07-17 01:19:31 +03005171 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
5172 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005173 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
5174 vcpu->arch.dr7,
5175 vcpu->arch.db);
5176
5177 if (dr6 != 0) {
5178 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005179 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005180 kvm_queue_exception(vcpu, DB_VECTOR);
5181 *r = EMULATE_DONE;
5182 return true;
5183 }
5184 }
5185
5186 return false;
5187}
5188
Andre Przywara51d8b662010-12-21 11:12:02 +01005189int x86_emulate_instruction(struct kvm_vcpu *vcpu,
5190 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01005191 int emulation_type,
5192 void *insn,
5193 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005194{
Gleb Natapov95cb2292010-04-28 19:15:43 +03005195 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005196 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005197 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005198 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005199
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005200 /*
5201 * Clear write_fault_to_shadow_pgtable here to ensure it is
5202 * never reused.
5203 */
5204 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03005205 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03005206
Sheng Yang571008d2008-01-02 14:49:22 +08005207 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005208 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005209
5210 /*
5211 * We will reenter on the same instruction since
5212 * we do not set complete_userspace_io. This does not
5213 * handle watchpoints yet, those would be handled in
5214 * the emulate_ops.
5215 */
5216 if (kvm_vcpu_check_breakpoint(vcpu, &r))
5217 return r;
5218
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005219 ctxt->interruptibility = 0;
5220 ctxt->have_exception = false;
5221 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005222
Borislav Petkovb51e9742013-09-22 16:44:52 +02005223 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02005224
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005225 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02005226
Avi Kivitye46479f2010-04-11 13:05:16 +03005227 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02005228 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005229 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02005230 if (emulation_type & EMULTYPE_TRAP_UD)
5231 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03005232 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5233 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005234 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005235 if (emulation_type & EMULTYPE_SKIP)
5236 return EMULATE_FAIL;
5237 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005238 }
5239 }
5240
Gleb Natapovba8afb62009-04-12 13:36:57 +03005241 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005242 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03005243 if (ctxt->eflags & X86_EFLAGS_RF)
5244 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03005245 return EMULATE_DONE;
5246 }
5247
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005248 if (retry_instruction(ctxt, cr2, emulation_type))
5249 return EMULATE_DONE;
5250
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005251 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005252 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005253 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
5254 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005255 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005256 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005257
Gleb Natapov5cd21912010-03-18 15:20:26 +02005258restart:
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005259 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005260
Joerg Roedel775fde82011-04-04 12:39:24 +02005261 if (r == EMULATION_INTERCEPTED)
5262 return EMULATE_DONE;
5263
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005264 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03005265 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5266 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005267 return EMULATE_DONE;
5268
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005269 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005270 }
5271
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005272 if (ctxt->have_exception) {
Gleb Natapove85d28f2010-07-29 15:11:52 +03005273 inject_emulated_exception(vcpu);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005274 r = EMULATE_DONE;
5275 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02005276 if (!vcpu->arch.pio.in) {
5277 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03005278 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02005279 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005280 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005281 vcpu->arch.complete_userspace_io = complete_emulated_pio;
5282 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005283 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005284 } else if (vcpu->mmio_needed) {
5285 if (!vcpu->mmio_is_write)
5286 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005287 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005288 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005289 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03005290 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005291 else
5292 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03005293
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005294 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005295 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005296 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005297 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005298 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005299 if (r == EMULATE_DONE)
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005300 kvm_vcpu_check_singlestep(vcpu, rflags, &r);
5301 __kvm_set_rflags(vcpu, ctxt->eflags);
5302
5303 /*
5304 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
5305 * do nothing, and it will be requested again as soon as
5306 * the shadow expires. But we still need to check here,
5307 * because POPF has no interrupt shadow.
5308 */
5309 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
5310 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005311 } else
5312 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03005313
Gleb Natapove85d28f2010-07-29 15:11:52 +03005314 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005315}
Andre Przywara51d8b662010-12-21 11:12:02 +01005316EXPORT_SYMBOL_GPL(x86_emulate_instruction);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005317
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005318int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01005319{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005320 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005321 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
5322 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005323 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02005324 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02005325 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01005326}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005327EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01005328
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005329static void tsc_bad(void *info)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005330{
Tejun Heo0a3aee02010-12-18 16:28:55 +01005331 __this_cpu_write(cpu_tsc_khz, 0);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005332}
5333
5334static void tsc_khz_changed(void *data)
5335{
5336 struct cpufreq_freqs *freq = data;
5337 unsigned long khz = 0;
5338
5339 if (data)
5340 khz = freq->new;
5341 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5342 khz = cpufreq_quick_get(raw_smp_processor_id());
5343 if (!khz)
5344 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01005345 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005346}
5347
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005348static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
5349 void *data)
5350{
5351 struct cpufreq_freqs *freq = data;
5352 struct kvm *kvm;
5353 struct kvm_vcpu *vcpu;
5354 int i, send_ipi = 0;
5355
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005356 /*
5357 * We allow guests to temporarily run on slowing clocks,
5358 * provided we notify them after, or to run on accelerating
5359 * clocks, provided we notify them before. Thus time never
5360 * goes backwards.
5361 *
5362 * However, we have a problem. We can't atomically update
5363 * the frequency of a given CPU from this function; it is
5364 * merely a notifier, which can be called from any CPU.
5365 * Changing the TSC frequency at arbitrary points in time
5366 * requires a recomputation of local variables related to
5367 * the TSC for each VCPU. We must flag these local variables
5368 * to be updated and be sure the update takes place with the
5369 * new frequency before any guests proceed.
5370 *
5371 * Unfortunately, the combination of hotplug CPU and frequency
5372 * change creates an intractable locking scenario; the order
5373 * of when these callouts happen is undefined with respect to
5374 * CPU hotplug, and they can race with each other. As such,
5375 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
5376 * undefined; you can actually have a CPU frequency change take
5377 * place in between the computation of X and the setting of the
5378 * variable. To protect against this problem, all updates of
5379 * the per_cpu tsc_khz variable are done in an interrupt
5380 * protected IPI, and all callers wishing to update the value
5381 * must wait for a synchronous IPI to complete (which is trivial
5382 * if the caller is on the CPU already). This establishes the
5383 * necessary total order on variable updates.
5384 *
5385 * Note that because a guest time update may take place
5386 * anytime after the setting of the VCPU's request bit, the
5387 * correct TSC value must be set before the request. However,
5388 * to ensure the update actually makes it to any guest which
5389 * starts running in hardware virtualization between the set
5390 * and the acquisition of the spinlock, we must also ping the
5391 * CPU after setting the request bit.
5392 *
5393 */
5394
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005395 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
5396 return 0;
5397 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
5398 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005399
5400 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005401
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005402 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005403 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005404 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005405 if (vcpu->cpu != freq->cpu)
5406 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10005407 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005408 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005409 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005410 }
5411 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005412 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005413
5414 if (freq->old < freq->new && send_ipi) {
5415 /*
5416 * We upscale the frequency. Must make the guest
5417 * doesn't see old kvmclock values while running with
5418 * the new frequency, otherwise we risk the guest sees
5419 * time go backwards.
5420 *
5421 * In case we update the frequency for another cpu
5422 * (which might be in guest context) send an interrupt
5423 * to kick the cpu out of guest context. Next time
5424 * guest context is entered kvmclock will be updated,
5425 * so the guest will not see stale values.
5426 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005427 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005428 }
5429 return 0;
5430}
5431
5432static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005433 .notifier_call = kvmclock_cpufreq_notifier
5434};
5435
5436static int kvmclock_cpu_notifier(struct notifier_block *nfb,
5437 unsigned long action, void *hcpu)
5438{
5439 unsigned int cpu = (unsigned long)hcpu;
5440
5441 switch (action) {
5442 case CPU_ONLINE:
5443 case CPU_DOWN_FAILED:
5444 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
5445 break;
5446 case CPU_DOWN_PREPARE:
5447 smp_call_function_single(cpu, tsc_bad, NULL, 1);
5448 break;
5449 }
5450 return NOTIFY_OK;
5451}
5452
5453static struct notifier_block kvmclock_cpu_notifier_block = {
5454 .notifier_call = kvmclock_cpu_notifier,
5455 .priority = -INT_MAX
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005456};
5457
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005458static void kvm_timer_init(void)
5459{
5460 int cpu;
5461
Zachary Amsdenc2855452010-09-18 14:38:15 -10005462 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305463
5464 cpu_notifier_register_begin();
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005465 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10005466#ifdef CONFIG_CPU_FREQ
5467 struct cpufreq_policy policy;
5468 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02005469 cpu = get_cpu();
5470 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10005471 if (policy.cpuinfo.max_freq)
5472 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02005473 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10005474#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005475 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
5476 CPUFREQ_TRANSITION_NOTIFIER);
5477 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10005478 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005479 for_each_online_cpu(cpu)
5480 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305481
5482 __register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
5483 cpu_notifier_register_done();
5484
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005485}
5486
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005487static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
5488
Gleb Natapovf5132b02011-11-10 14:57:22 +02005489int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005490{
Alex,Shi086c9852011-10-20 15:34:01 +08005491 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005492}
5493
5494static int kvm_is_user_mode(void)
5495{
5496 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005497
Alex,Shi086c9852011-10-20 15:34:01 +08005498 if (__this_cpu_read(current_vcpu))
5499 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005500
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005501 return user_mode != 0;
5502}
5503
5504static unsigned long kvm_get_guest_ip(void)
5505{
5506 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005507
Alex,Shi086c9852011-10-20 15:34:01 +08005508 if (__this_cpu_read(current_vcpu))
5509 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005510
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005511 return ip;
5512}
5513
5514static struct perf_guest_info_callbacks kvm_guest_cbs = {
5515 .is_in_guest = kvm_is_in_guest,
5516 .is_user_mode = kvm_is_user_mode,
5517 .get_guest_ip = kvm_get_guest_ip,
5518};
5519
5520void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
5521{
Alex,Shi086c9852011-10-20 15:34:01 +08005522 __this_cpu_write(current_vcpu, vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005523}
5524EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
5525
5526void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
5527{
Alex,Shi086c9852011-10-20 15:34:01 +08005528 __this_cpu_write(current_vcpu, NULL);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005529}
5530EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
5531
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005532static void kvm_set_mmio_spte_mask(void)
5533{
5534 u64 mask;
5535 int maxphyaddr = boot_cpu_data.x86_phys_bits;
5536
5537 /*
5538 * Set the reserved bits and the present bit of an paging-structure
5539 * entry to generate page fault with PFER.RSV = 1.
5540 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005541 /* Mask the reserved physical address bits. */
5542 mask = ((1ull << (51 - maxphyaddr + 1)) - 1) << maxphyaddr;
5543
5544 /* Bit 62 is always reserved for 32bit host. */
5545 mask |= 0x3ull << 62;
5546
5547 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005548 mask |= 1ull;
5549
5550#ifdef CONFIG_X86_64
5551 /*
5552 * If reserved bit is not supported, clear the present bit to disable
5553 * mmio page fault.
5554 */
5555 if (maxphyaddr == 52)
5556 mask &= ~1ull;
5557#endif
5558
5559 kvm_mmu_set_mmio_spte_mask(mask);
5560}
5561
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005562#ifdef CONFIG_X86_64
5563static void pvclock_gtod_update_fn(struct work_struct *work)
5564{
Marcelo Tosattid8281992012-11-27 23:29:01 -02005565 struct kvm *kvm;
5566
5567 struct kvm_vcpu *vcpu;
5568 int i;
5569
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005570 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005571 list_for_each_entry(kvm, &vm_list, vm_list)
5572 kvm_for_each_vcpu(i, vcpu, kvm)
5573 set_bit(KVM_REQ_MASTERCLOCK_UPDATE, &vcpu->requests);
5574 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005575 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005576}
5577
5578static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
5579
5580/*
5581 * Notification about pvclock gtod data update.
5582 */
5583static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
5584 void *priv)
5585{
5586 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
5587 struct timekeeper *tk = priv;
5588
5589 update_pvclock_gtod(tk);
5590
5591 /* disable master clock if host does not trust, or does not
5592 * use, TSC clocksource
5593 */
5594 if (gtod->clock.vclock_mode != VCLOCK_TSC &&
5595 atomic_read(&kvm_guest_has_master_clock) != 0)
5596 queue_work(system_long_wq, &pvclock_gtod_work);
5597
5598 return 0;
5599}
5600
5601static struct notifier_block pvclock_gtod_notifier = {
5602 .notifier_call = pvclock_gtod_notify,
5603};
5604#endif
5605
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005606int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02005607{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005608 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02005609 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005610
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005611 if (kvm_x86_ops) {
5612 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005613 r = -EEXIST;
5614 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005615 }
5616
5617 if (!ops->cpu_has_kvm_support()) {
5618 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005619 r = -EOPNOTSUPP;
5620 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005621 }
5622 if (ops->disabled_by_bios()) {
5623 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005624 r = -EOPNOTSUPP;
5625 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005626 }
5627
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005628 r = -ENOMEM;
5629 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
5630 if (!shared_msrs) {
5631 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
5632 goto out;
5633 }
5634
Avi Kivity97db56c2008-01-13 13:23:56 +02005635 r = kvm_mmu_module_init();
5636 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005637 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02005638
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005639 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02005640
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005641 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01005642 kvm_init_msr_list();
5643
Sheng Yang7b523452008-04-25 21:13:50 +08005644 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005645 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005646
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005647 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005648
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005649 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5650
Dexuan Cui2acf9232010-06-10 11:27:12 +08005651 if (cpu_has_xsave)
5652 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
5653
Gleb Natapovc5cc4212012-08-05 15:58:30 +03005654 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005655#ifdef CONFIG_X86_64
5656 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
5657#endif
5658
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005659 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005660
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005661out_free_percpu:
5662 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005663out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005664 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02005665}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005666
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005667void kvm_arch_exit(void)
5668{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005669 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5670
Jan Kiszka888d2562009-04-17 19:24:58 +02005671 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5672 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
5673 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005674 unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005675#ifdef CONFIG_X86_64
5676 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
5677#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005678 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005679 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005680 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005681}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005682
Hollis Blanchard8776e512007-10-31 17:24:24 -05005683int kvm_emulate_halt(struct kvm_vcpu *vcpu)
5684{
5685 ++vcpu->stat.halt_exits;
5686 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03005687 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005688 return 1;
5689 } else {
5690 vcpu->run->exit_reason = KVM_EXIT_HLT;
5691 return 0;
5692 }
5693}
5694EXPORT_SYMBOL_GPL(kvm_emulate_halt);
5695
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005696int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
5697{
5698 u64 param, ingpa, outgpa, ret;
5699 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
5700 bool fast, longmode;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005701
5702 /*
5703 * hypercall generates UD from non zero cpl and real mode
5704 * per HYPER-V spec
5705 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02005706 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005707 kvm_queue_exception(vcpu, UD_VECTOR);
5708 return 0;
5709 }
5710
Nadav Amita449c7a2014-06-18 17:19:24 +03005711 longmode = is_64_bit_mode(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005712
5713 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02005714 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
5715 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
5716 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
5717 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
5718 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
5719 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005720 }
5721#ifdef CONFIG_X86_64
5722 else {
5723 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
5724 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
5725 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
5726 }
5727#endif
5728
5729 code = param & 0xffff;
5730 fast = (param >> 16) & 0x1;
5731 rep_cnt = (param >> 32) & 0xfff;
5732 rep_idx = (param >> 48) & 0xfff;
5733
5734 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
5735
Gleb Natapovc25bc162010-01-17 15:51:24 +02005736 switch (code) {
5737 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
5738 kvm_vcpu_on_spin(vcpu);
5739 break;
5740 default:
5741 res = HV_STATUS_INVALID_HYPERCALL_CODE;
5742 break;
5743 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005744
5745 ret = res | (((u64)rep_done & 0xfff) << 32);
5746 if (longmode) {
5747 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
5748 } else {
5749 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
5750 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
5751 }
5752
5753 return 1;
5754}
5755
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305756/*
5757 * kvm_pv_kick_cpu_op: Kick a vcpu.
5758 *
5759 * @apicid - apicid of vcpu to be kicked.
5760 */
5761static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
5762{
Raghavendra K T24d21662013-08-26 14:18:35 +05305763 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305764
Raghavendra K T24d21662013-08-26 14:18:35 +05305765 lapic_irq.shorthand = 0;
5766 lapic_irq.dest_mode = 0;
5767 lapic_irq.dest_id = apicid;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305768
Raghavendra K T24d21662013-08-26 14:18:35 +05305769 lapic_irq.delivery_mode = APIC_DM_REMRD;
5770 kvm_irq_delivery_to_apic(kvm, 0, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305771}
5772
Hollis Blanchard8776e512007-10-31 17:24:24 -05005773int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
5774{
5775 unsigned long nr, a0, a1, a2, a3, ret;
Nadav Amita449c7a2014-06-18 17:19:24 +03005776 int op_64_bit, r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005777
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005778 if (kvm_hv_hypercall_enabled(vcpu->kvm))
5779 return kvm_hv_hypercall(vcpu);
5780
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005781 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
5782 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
5783 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
5784 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
5785 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005786
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005787 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005788
Nadav Amita449c7a2014-06-18 17:19:24 +03005789 op_64_bit = is_64_bit_mode(vcpu);
5790 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05005791 nr &= 0xFFFFFFFF;
5792 a0 &= 0xFFFFFFFF;
5793 a1 &= 0xFFFFFFFF;
5794 a2 &= 0xFFFFFFFF;
5795 a3 &= 0xFFFFFFFF;
5796 }
5797
Jan Kiszka07708c42009-08-03 18:43:28 +02005798 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
5799 ret = -KVM_EPERM;
5800 goto out;
5801 }
5802
Hollis Blanchard8776e512007-10-31 17:24:24 -05005803 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02005804 case KVM_HC_VAPIC_POLL_IRQ:
5805 ret = 0;
5806 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305807 case KVM_HC_KICK_CPU:
5808 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
5809 ret = 0;
5810 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005811 default:
5812 ret = -KVM_ENOSYS;
5813 break;
5814 }
Jan Kiszka07708c42009-08-03 18:43:28 +02005815out:
Nadav Amita449c7a2014-06-18 17:19:24 +03005816 if (!op_64_bit)
5817 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005818 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05305819 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05005820 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005821}
5822EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
5823
Jan Kiszkab6785de2012-09-20 07:43:17 +02005824static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005825{
Avi Kivityd6aa1002011-04-20 15:47:13 +03005826 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005827 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005828 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005829
Hollis Blanchard8776e512007-10-31 17:24:24 -05005830 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005831
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005832 return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005833}
5834
Hollis Blanchardd0752062007-10-31 17:24:25 -05005835/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005836 * Check if userspace requested an interrupt window, and that the
5837 * interrupt window is open.
5838 *
5839 * No need to exit to userspace if we already have an interrupt queued.
5840 */
Avi Kivity851ba692009-08-24 11:10:17 +03005841static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005842{
Gleb Natapov80618232009-04-21 17:44:56 +03005843 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005844 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03005845 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005846}
5847
Avi Kivity851ba692009-08-24 11:10:17 +03005848static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005849{
Avi Kivity851ba692009-08-24 11:10:17 +03005850 struct kvm_run *kvm_run = vcpu->run;
5851
Jan Kiszka91586a32009-10-05 13:07:21 +02005852 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005853 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005854 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01005855 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005856 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01005857 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005858 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03005859 kvm_arch_interrupt_allowed(vcpu) &&
5860 !kvm_cpu_has_interrupt(vcpu) &&
5861 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005862}
5863
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005864static void update_cr8_intercept(struct kvm_vcpu *vcpu)
5865{
5866 int max_irr, tpr;
5867
5868 if (!kvm_x86_ops->update_cr8_intercept)
5869 return;
5870
Avi Kivity88c808f2009-08-17 22:49:40 +03005871 if (!vcpu->arch.apic)
5872 return;
5873
Gleb Natapov8db3baa2009-05-11 13:35:54 +03005874 if (!vcpu->arch.apic->vapic_addr)
5875 max_irr = kvm_lapic_find_highest_irr(vcpu);
5876 else
5877 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005878
5879 if (max_irr != -1)
5880 max_irr >>= 4;
5881
5882 tpr = kvm_lapic_get_cr8(vcpu);
5883
5884 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
5885}
5886
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005887static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005888{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005889 int r;
5890
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005891 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005892 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02005893 trace_kvm_inj_exception(vcpu->arch.exception.nr,
5894 vcpu->arch.exception.has_error_code,
5895 vcpu->arch.exception.error_code);
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005896 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
5897 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005898 vcpu->arch.exception.error_code,
5899 vcpu->arch.exception.reinject);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005900 return 0;
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005901 }
5902
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005903 if (vcpu->arch.nmi_injected) {
5904 kvm_x86_ops->set_nmi(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005905 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005906 }
5907
5908 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005909 kvm_x86_ops->set_irq(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005910 return 0;
5911 }
5912
5913 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
5914 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
5915 if (r != 0)
5916 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005917 }
5918
5919 /* try to inject new event if pending */
5920 if (vcpu->arch.nmi_pending) {
5921 if (kvm_x86_ops->nmi_allowed(vcpu)) {
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005922 --vcpu->arch.nmi_pending;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005923 vcpu->arch.nmi_injected = true;
5924 kvm_x86_ops->set_nmi(vcpu);
5925 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08005926 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005927 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005928 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
5929 false);
5930 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005931 }
5932 }
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005933 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005934}
5935
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005936static void process_nmi(struct kvm_vcpu *vcpu)
5937{
5938 unsigned limit = 2;
5939
5940 /*
5941 * x86 is limited to one NMI running, and one NMI pending after it.
5942 * If an NMI is already in progress, limit further NMIs to just one.
5943 * Otherwise, allow two (and we'll inject the first one immediately).
5944 */
5945 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
5946 limit = 1;
5947
5948 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
5949 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
5950 kvm_make_request(KVM_REQ_EVENT, vcpu);
5951}
5952
Yang Zhang3d81bc72013-04-11 19:25:13 +08005953static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005954{
5955 u64 eoi_exit_bitmap[4];
Yang Zhangcf9e65b2013-04-11 19:25:14 +08005956 u32 tmr[8];
Yang Zhangc7c9c562013-01-25 10:18:51 +08005957
Yang Zhang3d81bc72013-04-11 19:25:13 +08005958 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
5959 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005960
5961 memset(eoi_exit_bitmap, 0, 32);
Yang Zhangcf9e65b2013-04-11 19:25:14 +08005962 memset(tmr, 0, 32);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005963
Yang Zhangcf9e65b2013-04-11 19:25:14 +08005964 kvm_ioapic_scan_entry(vcpu, eoi_exit_bitmap, tmr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005965 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangcf9e65b2013-04-11 19:25:14 +08005966 kvm_apic_update_tmr(vcpu, tmr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005967}
5968
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09005969/*
5970 * Returns 1 to let __vcpu_run() continue the guest execution loop without
5971 * exiting to the userspace. Otherwise, the value will be returned to the
5972 * userspace.
5973 */
Avi Kivity851ba692009-08-24 11:10:17 +03005974static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005975{
5976 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03005977 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005978 vcpu->run->request_interrupt_window;
Jan Kiszka730dca42013-04-28 10:50:52 +02005979 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005980
Avi Kivity3e007502010-06-23 14:26:18 +03005981 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03005982 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05005983 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03005984 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03005985 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005986 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
5987 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03005988 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
5989 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10005990 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
5991 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005992 if (unlikely(r))
5993 goto out;
5994 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03005995 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03005996 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03005997 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03005998 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03005999 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006000 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02006001 r = 0;
6002 goto out;
6003 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006004 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006005 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01006006 r = 0;
6007 goto out;
6008 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006009 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02006010 vcpu->fpu_active = 0;
6011 kvm_x86_ops->fpu_deactivate(vcpu);
6012 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006013 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
6014 /* Page is swapped out. Do synthetic halt */
6015 vcpu->arch.apf.halted = true;
6016 r = 1;
6017 goto out;
6018 }
Glauber Costac9aaa892011-07-11 15:28:14 -04006019 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
6020 record_steal_time(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006021 if (kvm_check_request(KVM_REQ_NMI, vcpu))
6022 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006023 if (kvm_check_request(KVM_REQ_PMU, vcpu))
6024 kvm_handle_pmu_event(vcpu);
6025 if (kvm_check_request(KVM_REQ_PMI, vcpu))
6026 kvm_deliver_pmi(vcpu);
Yang Zhang3d81bc72013-04-11 19:25:13 +08006027 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
6028 vcpu_scan_ioapic(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02006029 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006030
Avi Kivityb463a6f2010-07-20 15:06:17 +03006031 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Jan Kiszka66450a22013-03-13 12:42:34 +01006032 kvm_apic_accept_events(vcpu);
6033 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
6034 r = 1;
6035 goto out;
6036 }
6037
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006038 if (inject_pending_event(vcpu, req_int_win) != 0)
6039 req_immediate_exit = true;
Avi Kivity3842d132010-07-27 12:30:24 +03006040 /* enable NMI/IRQ window open exits if needed */
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006041 else if (vcpu->arch.nmi_pending)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006042 kvm_x86_ops->enable_nmi_window(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006043 else if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006044 kvm_x86_ops->enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006045
Avi Kivity3842d132010-07-27 12:30:24 +03006046 if (kvm_lapic_enabled(vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006047 /*
6048 * Update architecture specific hints for APIC
6049 * virtual interrupt delivery.
6050 */
6051 if (kvm_x86_ops->hwapic_irr_update)
6052 kvm_x86_ops->hwapic_irr_update(vcpu,
6053 kvm_lapic_find_highest_irr(vcpu));
Avi Kivity3842d132010-07-27 12:30:24 +03006054 update_cr8_intercept(vcpu);
6055 kvm_lapic_sync_to_vapic(vcpu);
6056 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006057 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006058
Avi Kivityd8368af2012-05-14 18:07:56 +03006059 r = kvm_mmu_reload(vcpu);
6060 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006061 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03006062 }
6063
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006064 preempt_disable();
6065
6066 kvm_x86_ops->prepare_guest_switch(vcpu);
6067 if (vcpu->fpu_active)
6068 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006069 kvm_load_guest_xcr0(vcpu);
6070
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006071 vcpu->mode = IN_GUEST_MODE;
6072
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006073 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6074
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006075 /* We should set ->mode before check ->requests,
6076 * see the comment in make_all_cpus_request.
6077 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006078 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006079
6080 local_irq_disable();
6081
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006082 if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006083 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006084 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006085 smp_wmb();
6086 local_irq_enable();
6087 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006088 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006089 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006090 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006091 }
6092
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006093 if (req_immediate_exit)
6094 smp_send_reschedule(vcpu->cpu);
6095
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006096 kvm_guest_enter();
6097
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006098 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006099 set_debugreg(0, 7);
6100 set_debugreg(vcpu->arch.eff_db[0], 0);
6101 set_debugreg(vcpu->arch.eff_db[1], 1);
6102 set_debugreg(vcpu->arch.eff_db[2], 2);
6103 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006104 set_debugreg(vcpu->arch.dr6, 6);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006105 }
6106
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006107 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03006108 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006109
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006110 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006111 * Do this here before restoring debug registers on the host. And
6112 * since we do this before handling the vmexit, a DR access vmexit
6113 * can (a) read the correct value of the debug registers, (b) set
6114 * KVM_DEBUGREG_WONT_EXIT again.
6115 */
6116 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
6117 int i;
6118
6119 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
6120 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
6121 for (i = 0; i < KVM_NR_DB_REGS; i++)
6122 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
6123 }
6124
6125 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006126 * If the guest has used debug registers, at least dr7
6127 * will be disabled while returning to the host.
6128 * If we don't have active breakpoints in the host, we don't
6129 * care about the messed up debug address registers. But if
6130 * we have some of them active, restore the old state.
6131 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01006132 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006133 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006134
Marcelo Tosatti886b4702012-11-27 23:28:58 -02006135 vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu,
6136 native_read_tsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10006137
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006138 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03006139 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006140
6141 /* Interrupt is enabled by handle_external_intr() */
6142 kvm_x86_ops->handle_external_intr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006143
6144 ++vcpu->stat.exits;
6145
6146 /*
6147 * We must have an instruction between local_irq_enable() and
6148 * kvm_guest_exit(), so the timer interrupt isn't delayed by
6149 * the interrupt shadow. The stat.exits increment will do nicely.
6150 * But we need to prevent reordering, hence this barrier():
6151 */
6152 barrier();
6153
6154 kvm_guest_exit();
6155
6156 preempt_enable();
6157
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006158 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006159
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006160 /*
6161 * Profile KVM exit RIPs:
6162 */
6163 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006164 unsigned long rip = kvm_rip_read(vcpu);
6165 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006166 }
6167
Zachary Amsdencc578282012-02-03 15:43:50 -02006168 if (unlikely(vcpu->arch.tsc_always_catchup))
6169 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02006170
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03006171 if (vcpu->arch.apic_attention)
6172 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02006173
Avi Kivity851ba692009-08-24 11:10:17 +03006174 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006175 return r;
6176
6177cancel_injection:
6178 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03006179 if (unlikely(vcpu->arch.apic_attention))
6180 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006181out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03006182 return r;
6183}
6184
Gleb Natapov09cec752009-03-23 15:11:44 +02006185
Avi Kivity851ba692009-08-24 11:10:17 +03006186static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03006187{
6188 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006189 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006190
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006191 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006192
6193 r = 1;
6194 while (r > 0) {
Gleb Natapovaf585b92010-10-14 11:22:46 +02006195 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
6196 !vcpu->arch.apf.halted)
Avi Kivity851ba692009-08-24 11:10:17 +03006197 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006198 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006199 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006200 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006201 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006202 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
6203 kvm_apic_accept_events(vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02006204 switch(vcpu->arch.mp_state) {
6205 case KVM_MP_STATE_HALTED:
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306206 vcpu->arch.pv.pv_unhalted = false;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006207 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02006208 KVM_MP_STATE_RUNNABLE;
6209 case KVM_MP_STATE_RUNNABLE:
Gleb Natapovaf585b92010-10-14 11:22:46 +02006210 vcpu->arch.apf.halted = false;
Gleb Natapov09cec752009-03-23 15:11:44 +02006211 break;
Jan Kiszka66450a22013-03-13 12:42:34 +01006212 case KVM_MP_STATE_INIT_RECEIVED:
6213 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006214 default:
6215 r = -EINTR;
6216 break;
6217 }
6218 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03006219 }
6220
Gleb Natapov09cec752009-03-23 15:11:44 +02006221 if (r <= 0)
6222 break;
6223
6224 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
6225 if (kvm_cpu_has_pending_timer(vcpu))
6226 kvm_inject_pending_timer_irqs(vcpu);
6227
Avi Kivity851ba692009-08-24 11:10:17 +03006228 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02006229 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006230 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006231 ++vcpu->stat.request_irq_exits;
6232 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006233
6234 kvm_check_async_pf_completion(vcpu);
6235
Gleb Natapov09cec752009-03-23 15:11:44 +02006236 if (signal_pending(current)) {
6237 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006238 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006239 ++vcpu->stat.signal_exits;
6240 }
6241 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006242 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09006243 cond_resched();
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 }
6247
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006248 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006249
6250 return r;
6251}
6252
Gleb Natapov716d51a2012-09-03 15:24:26 +03006253static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
6254{
6255 int r;
6256 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6257 r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
6258 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6259 if (r != EMULATE_DONE)
6260 return 0;
6261 return 1;
6262}
6263
6264static int complete_emulated_pio(struct kvm_vcpu *vcpu)
6265{
6266 BUG_ON(!vcpu->arch.pio.count);
6267
6268 return complete_emulated_io(vcpu);
6269}
6270
Avi Kivityf78146b2012-04-18 19:22:47 +03006271/*
6272 * Implements the following, as a state machine:
6273 *
6274 * read:
6275 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006276 * for each mmio piece in the fragment
6277 * write gpa, len
6278 * exit
6279 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03006280 * execute insn
6281 *
6282 * write:
6283 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006284 * for each mmio piece in the fragment
6285 * write gpa, len
6286 * copy data
6287 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03006288 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03006289static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02006290{
6291 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03006292 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006293 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02006294
Gleb Natapov716d51a2012-09-03 15:24:26 +03006295 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006296
Gleb Natapov716d51a2012-09-03 15:24:26 +03006297 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006298 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
6299 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006300 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006301 memcpy(frag->data, run->mmio.data, len);
6302
6303 if (frag->len <= 8) {
6304 /* Switch to the next fragment. */
6305 frag++;
6306 vcpu->mmio_cur_fragment++;
6307 } else {
6308 /* Go forward to the next mmio piece. */
6309 frag->data += len;
6310 frag->gpa += len;
6311 frag->len -= len;
6312 }
6313
Andrew Honiga08d3b32014-02-27 19:35:14 +01006314 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03006315 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006316
6317 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02006318 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03006319 return 1;
6320 vcpu->mmio_read_completed = 1;
6321 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02006322 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006323
Gleb Natapov716d51a2012-09-03 15:24:26 +03006324 run->exit_reason = KVM_EXIT_MMIO;
6325 run->mmio.phys_addr = frag->gpa;
6326 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006327 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
6328 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006329 run->mmio.is_write = vcpu->mmio_is_write;
6330 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
6331 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02006332}
6333
Gleb Natapov716d51a2012-09-03 15:24:26 +03006334
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006335int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6336{
6337 int r;
6338 sigset_t sigsaved;
6339
Avi Kivitye5c30142011-01-11 12:15:54 +02006340 if (!tsk_used_math(current) && init_fpu(current))
6341 return -ENOMEM;
6342
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006343 if (vcpu->sigset_active)
6344 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
6345
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006346 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
6347 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006348 kvm_apic_accept_events(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006349 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006350 r = -EAGAIN;
6351 goto out;
6352 }
6353
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006354 /* re-sync apic's tpr */
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006355 if (!irqchip_in_kernel(vcpu->kvm)) {
6356 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
6357 r = -EINVAL;
6358 goto out;
6359 }
6360 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006361
Gleb Natapov716d51a2012-09-03 15:24:26 +03006362 if (unlikely(vcpu->arch.complete_userspace_io)) {
6363 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
6364 vcpu->arch.complete_userspace_io = NULL;
6365 r = cui(vcpu);
6366 if (r <= 0)
6367 goto out;
6368 } else
6369 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006370
Avi Kivity851ba692009-08-24 11:10:17 +03006371 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006372
6373out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03006374 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006375 if (vcpu->sigset_active)
6376 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
6377
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006378 return r;
6379}
6380
6381int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6382{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006383 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
6384 /*
6385 * We are here if userspace calls get_regs() in the middle of
6386 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08006387 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006388 * that usually, but some bad designed PV devices (vmware
6389 * backdoor interface) need this to work
6390 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03006391 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006392 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6393 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006394 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
6395 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
6396 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
6397 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
6398 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
6399 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
6400 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6401 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006402#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006403 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
6404 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
6405 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
6406 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
6407 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
6408 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
6409 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
6410 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006411#endif
6412
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006413 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02006414 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006415
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006416 return 0;
6417}
6418
6419int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6420{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006421 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
6422 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6423
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006424 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
6425 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
6426 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
6427 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
6428 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
6429 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
6430 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
6431 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006432#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006433 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
6434 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
6435 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
6436 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
6437 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
6438 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
6439 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
6440 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006441#endif
6442
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006443 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02006444 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006445
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02006446 vcpu->arch.exception.pending = false;
6447
Avi Kivity3842d132010-07-27 12:30:24 +03006448 kvm_make_request(KVM_REQ_EVENT, vcpu);
6449
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006450 return 0;
6451}
6452
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006453void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
6454{
6455 struct kvm_segment cs;
6456
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006457 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006458 *db = cs.db;
6459 *l = cs.l;
6460}
6461EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
6462
6463int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
6464 struct kvm_sregs *sregs)
6465{
Gleb Natapov89a27f42010-02-16 10:51:48 +02006466 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006467
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006468 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6469 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6470 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6471 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6472 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6473 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006474
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006475 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6476 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006477
6478 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006479 sregs->idt.limit = dt.size;
6480 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006481 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006482 sregs->gdt.limit = dt.size;
6483 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006484
Avi Kivity4d4ec082009-12-29 18:07:30 +02006485 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006486 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006487 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02006488 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006489 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02006490 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006491 sregs->apic_base = kvm_get_apic_base(vcpu);
6492
Gleb Natapov923c61b2009-05-11 13:35:48 +03006493 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006494
Gleb Natapov36752c92009-05-11 13:35:53 +03006495 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03006496 set_bit(vcpu->arch.interrupt.nr,
6497 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03006498
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006499 return 0;
6500}
6501
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006502int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
6503 struct kvm_mp_state *mp_state)
6504{
Jan Kiszka66450a22013-03-13 12:42:34 +01006505 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306506 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
6507 vcpu->arch.pv.pv_unhalted)
6508 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
6509 else
6510 mp_state->mp_state = vcpu->arch.mp_state;
6511
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006512 return 0;
6513}
6514
6515int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
6516 struct kvm_mp_state *mp_state)
6517{
Jan Kiszka66450a22013-03-13 12:42:34 +01006518 if (!kvm_vcpu_has_lapic(vcpu) &&
6519 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
6520 return -EINVAL;
6521
6522 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
6523 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
6524 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
6525 } else
6526 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03006527 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006528 return 0;
6529}
6530
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006531int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
6532 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006533{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006534 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006535 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006536
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006537 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006538
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006539 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006540 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02006541
Gleb Natapovc6975182010-02-18 12:15:01 +02006542 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03006543 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02006544
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006545 kvm_rip_write(vcpu, ctxt->eip);
6546 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03006547 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03006548 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02006549}
6550EXPORT_SYMBOL_GPL(kvm_task_switch);
6551
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006552int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
6553 struct kvm_sregs *sregs)
6554{
Jan Kiszka58cb6282014-01-24 16:48:44 +01006555 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006556 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006557 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02006558 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006559
Petr Matousek6d1068b2012-11-06 19:24:07 +01006560 if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
6561 return -EINVAL;
6562
Gleb Natapov89a27f42010-02-16 10:51:48 +02006563 dt.size = sregs->idt.limit;
6564 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006565 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006566 dt.size = sregs->gdt.limit;
6567 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006568 kvm_x86_ops->set_gdt(vcpu, &dt);
6569
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006570 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006571 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02006572 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02006573 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006574
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006575 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006576
Avi Kivityf6801df2010-01-21 15:31:50 +02006577 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006578 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006579 apic_base_msr.data = sregs->apic_base;
6580 apic_base_msr.host_initiated = true;
6581 kvm_set_apic_base(vcpu, &apic_base_msr);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006582
Avi Kivity4d4ec082009-12-29 18:07:30 +02006583 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006584 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00006585 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006586
Avi Kivityfc78f512009-12-07 12:16:48 +02006587 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006588 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Sheng Yang3ea3aa82010-12-08 10:49:43 +08006589 if (sregs->cr4 & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +02006590 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006591
6592 idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02006593 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02006594 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02006595 mmu_reset_needed = 1;
6596 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006597 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006598
6599 if (mmu_reset_needed)
6600 kvm_mmu_reset_context(vcpu);
6601
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03006602 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03006603 pending_vec = find_first_bit(
6604 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
6605 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006606 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03006607 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006608 }
6609
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006610 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6611 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6612 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6613 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6614 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6615 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006616
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006617 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6618 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006619
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03006620 update_cr8_intercept(vcpu);
6621
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006622 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03006623 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006624 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02006625 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006626 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
6627
Avi Kivity3842d132010-07-27 12:30:24 +03006628 kvm_make_request(KVM_REQ_EVENT, vcpu);
6629
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006630 return 0;
6631}
6632
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006633int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
6634 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006635{
Jan Kiszka355be0b2009-10-03 00:31:21 +02006636 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006637 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006638
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006639 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
6640 r = -EBUSY;
6641 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03006642 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006643 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
6644 kvm_queue_exception(vcpu, DB_VECTOR);
6645 else
6646 kvm_queue_exception(vcpu, BP_VECTOR);
6647 }
6648
Jan Kiszka91586a32009-10-05 13:07:21 +02006649 /*
6650 * Read rflags as long as potentially injected trace flags are still
6651 * filtered out.
6652 */
6653 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02006654
6655 vcpu->guest_debug = dbg->control;
6656 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
6657 vcpu->guest_debug = 0;
6658
6659 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006660 for (i = 0; i < KVM_NR_DB_REGS; ++i)
6661 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02006662 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006663 } else {
6664 for (i = 0; i < KVM_NR_DB_REGS; i++)
6665 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006666 }
Jan Kiszkac8639012012-09-21 05:42:55 +02006667 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006668
Jan Kiszkaf92653e2010-02-23 17:47:55 +01006669 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6670 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
6671 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006672
Jan Kiszka91586a32009-10-05 13:07:21 +02006673 /*
6674 * Trigger an rflags update that will inject or remove the trace
6675 * flags.
6676 */
6677 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006678
Jan Kiszkac8639012012-09-21 05:42:55 +02006679 kvm_x86_ops->update_db_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02006680
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006681 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006682
Avi Kivity2122ff52010-05-13 11:25:04 +03006683out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006684
6685 return r;
6686}
6687
6688/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08006689 * Translate a guest virtual address to a guest physical address.
6690 */
6691int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
6692 struct kvm_translation *tr)
6693{
6694 unsigned long vaddr = tr->linear_address;
6695 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006696 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08006697
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006698 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02006699 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006700 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08006701 tr->physical_address = gpa;
6702 tr->valid = gpa != UNMAPPED_GVA;
6703 tr->writeable = 1;
6704 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08006705
6706 return 0;
6707}
6708
Hollis Blanchardd0752062007-10-31 17:24:25 -05006709int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
6710{
Sheng Yang98918832010-05-17 17:08:28 +08006711 struct i387_fxsave_struct *fxsave =
6712 &vcpu->arch.guest_fpu.state->fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05006713
Hollis Blanchardd0752062007-10-31 17:24:25 -05006714 memcpy(fpu->fpr, fxsave->st_space, 128);
6715 fpu->fcw = fxsave->cwd;
6716 fpu->fsw = fxsave->swd;
6717 fpu->ftwx = fxsave->twd;
6718 fpu->last_opcode = fxsave->fop;
6719 fpu->last_ip = fxsave->rip;
6720 fpu->last_dp = fxsave->rdp;
6721 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
6722
Hollis Blanchardd0752062007-10-31 17:24:25 -05006723 return 0;
6724}
6725
6726int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
6727{
Sheng Yang98918832010-05-17 17:08:28 +08006728 struct i387_fxsave_struct *fxsave =
6729 &vcpu->arch.guest_fpu.state->fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05006730
Hollis Blanchardd0752062007-10-31 17:24:25 -05006731 memcpy(fxsave->st_space, fpu->fpr, 128);
6732 fxsave->cwd = fpu->fcw;
6733 fxsave->swd = fpu->fsw;
6734 fxsave->twd = fpu->ftwx;
6735 fxsave->fop = fpu->last_opcode;
6736 fxsave->rip = fpu->last_ip;
6737 fxsave->rdp = fpu->last_dp;
6738 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
6739
Hollis Blanchardd0752062007-10-31 17:24:25 -05006740 return 0;
6741}
6742
Jan Kiszka10ab25c2010-05-25 16:01:50 +02006743int fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05006744{
Jan Kiszka10ab25c2010-05-25 16:01:50 +02006745 int err;
6746
6747 err = fpu_alloc(&vcpu->arch.guest_fpu);
6748 if (err)
6749 return err;
6750
Sheng Yang98918832010-05-17 17:08:28 +08006751 fpu_finit(&vcpu->arch.guest_fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006752
Dexuan Cui2acf9232010-06-10 11:27:12 +08006753 /*
6754 * Ensure guest xcr0 is valid for loading
6755 */
6756 vcpu->arch.xcr0 = XSTATE_FP;
6757
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006758 vcpu->arch.cr0 |= X86_CR0_ET;
Jan Kiszka10ab25c2010-05-25 16:01:50 +02006759
6760 return 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05006761}
6762EXPORT_SYMBOL_GPL(fx_init);
6763
Sheng Yang98918832010-05-17 17:08:28 +08006764static void fx_free(struct kvm_vcpu *vcpu)
6765{
6766 fpu_free(&vcpu->arch.guest_fpu);
6767}
6768
Hollis Blanchardd0752062007-10-31 17:24:25 -05006769void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
6770{
Avi Kivity2608d7a2010-01-21 15:31:45 +02006771 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05006772 return;
6773
Dexuan Cui2acf9232010-06-10 11:27:12 +08006774 /*
6775 * Restore all possible states in the guest,
6776 * and assume host would use all available bits.
6777 * Guest xcr0 would be loaded later.
6778 */
6779 kvm_put_guest_xcr0(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006780 vcpu->guest_fpu_loaded = 1;
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07006781 __kernel_fpu_begin();
Sheng Yang98918832010-05-17 17:08:28 +08006782 fpu_restore_checking(&vcpu->arch.guest_fpu);
Avi Kivity0c048512010-01-21 15:31:52 +02006783 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006784}
Hollis Blanchardd0752062007-10-31 17:24:25 -05006785
6786void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
6787{
Dexuan Cui2acf9232010-06-10 11:27:12 +08006788 kvm_put_guest_xcr0(vcpu);
6789
Hollis Blanchardd0752062007-10-31 17:24:25 -05006790 if (!vcpu->guest_fpu_loaded)
6791 return;
6792
6793 vcpu->guest_fpu_loaded = 0;
Sheng Yang98918832010-05-17 17:08:28 +08006794 fpu_save_init(&vcpu->arch.guest_fpu);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07006795 __kernel_fpu_end();
Avi Kivityf096ed82007-11-18 13:54:33 +02006796 ++vcpu->stat.fpu_reload;
Avi Kivitya8eeb042010-05-10 12:34:53 +03006797 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
Avi Kivity0c048512010-01-21 15:31:52 +02006798 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05006799}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006800
6801void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
6802{
Glauber Costa12f9a482011-02-01 14:16:40 -05006803 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01006804
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006805 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Sheng Yang98918832010-05-17 17:08:28 +08006806 fx_free(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006807 kvm_x86_ops->vcpu_free(vcpu);
6808}
6809
6810struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
6811 unsigned int id)
6812{
Zachary Amsden6755bae2010-08-19 22:07:22 -10006813 if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
6814 printk_once(KERN_WARNING
6815 "kvm: SMP vm created on host with unstable TSC; "
6816 "guest TSC will not be reliable\n");
Avi Kivity26e52152007-11-20 15:30:24 +02006817 return kvm_x86_ops->vcpu_create(kvm, id);
6818}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006819
Avi Kivity26e52152007-11-20 15:30:24 +02006820int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
6821{
6822 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006823
Sheng Yang0bed3b52008-10-09 16:01:54 +08006824 vcpu->arch.mtrr_state.have_fixed = 1;
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03006825 r = vcpu_load(vcpu);
6826 if (r)
6827 return r;
Jan Kiszka57f252f2013-03-12 10:20:24 +01006828 kvm_vcpu_reset(vcpu);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02006829 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006830 vcpu_put(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006831
Avi Kivity26e52152007-11-20 15:30:24 +02006832 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006833}
6834
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006835int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
6836{
6837 int r;
Will Auld8fe8ab42012-11-29 12:42:12 -08006838 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01006839 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006840
6841 r = vcpu_load(vcpu);
6842 if (r)
6843 return r;
Will Auld8fe8ab42012-11-29 12:42:12 -08006844 msr.data = 0x0;
6845 msr.index = MSR_IA32_TSC;
6846 msr.host_initiated = true;
6847 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006848 vcpu_put(vcpu);
6849
Andrew Jones332967a2014-02-28 12:52:55 +01006850 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
6851 KVMCLOCK_SYNC_PERIOD);
6852
Marcelo Tosatti42897d82012-11-27 23:29:02 -02006853 return r;
6854}
6855
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06006856void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006857{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03006858 int r;
Gleb Natapov344d9582010-10-14 11:22:50 +02006859 vcpu->arch.apf.msr_val = 0;
6860
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03006861 r = vcpu_load(vcpu);
6862 BUG_ON(r);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006863 kvm_mmu_unload(vcpu);
6864 vcpu_put(vcpu);
6865
Sheng Yang98918832010-05-17 17:08:28 +08006866 fx_free(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006867 kvm_x86_ops->vcpu_free(vcpu);
6868}
6869
Jan Kiszka66450a22013-03-13 12:42:34 +01006870void kvm_vcpu_reset(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006871{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006872 atomic_set(&vcpu->arch.nmi_queued, 0);
6873 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02006874 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03006875 kvm_clear_interrupt_queue(vcpu);
6876 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02006877
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006878 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amit6f43ed02014-07-15 17:37:46 +03006879 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006880 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006881 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02006882 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006883
Avi Kivity3842d132010-07-27 12:30:24 +03006884 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02006885 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04006886 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03006887
Glauber Costa12f9a482011-02-01 14:16:40 -05006888 kvmclock_reset(vcpu);
6889
Gleb Natapovaf585b92010-10-14 11:22:46 +02006890 kvm_clear_async_pf_completion_queue(vcpu);
6891 kvm_async_pf_hash_reset(vcpu);
6892 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006893
Gleb Natapovf5132b02011-11-10 14:57:22 +02006894 kvm_pmu_reset(vcpu);
6895
Julian Stecklina66f7b722012-12-05 15:26:19 +01006896 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
6897 vcpu->arch.regs_avail = ~0;
6898 vcpu->arch.regs_dirty = ~0;
6899
Jan Kiszka57f252f2013-03-12 10:20:24 +01006900 kvm_x86_ops->vcpu_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006901}
6902
Jan Kiszka66450a22013-03-13 12:42:34 +01006903void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, unsigned int vector)
6904{
6905 struct kvm_segment cs;
6906
6907 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
6908 cs.selector = vector << 8;
6909 cs.base = vector << 12;
6910 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
6911 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006912}
6913
Alexander Graf10474ae2009-09-15 11:37:46 +02006914int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08006915{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10006916 struct kvm *kvm;
6917 struct kvm_vcpu *vcpu;
6918 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006919 int ret;
6920 u64 local_tsc;
6921 u64 max_tsc = 0;
6922 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03006923
6924 kvm_shared_msr_cpu_online();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006925 ret = kvm_x86_ops->hardware_enable(garbage);
6926 if (ret != 0)
6927 return ret;
6928
6929 local_tsc = native_read_tsc();
6930 stable = !check_tsc_unstable();
6931 list_for_each_entry(kvm, &vm_list, vm_list) {
6932 kvm_for_each_vcpu(i, vcpu, kvm) {
6933 if (!stable && vcpu->cpu == smp_processor_id())
6934 set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
6935 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
6936 backwards_tsc = true;
6937 if (vcpu->arch.last_host_tsc > max_tsc)
6938 max_tsc = vcpu->arch.last_host_tsc;
6939 }
6940 }
6941 }
6942
6943 /*
6944 * Sometimes, even reliable TSCs go backwards. This happens on
6945 * platforms that reset TSC during suspend or hibernate actions, but
6946 * maintain synchronization. We must compensate. Fortunately, we can
6947 * detect that condition here, which happens early in CPU bringup,
6948 * before any KVM threads can be running. Unfortunately, we can't
6949 * bring the TSCs fully up to date with real time, as we aren't yet far
6950 * enough into CPU bringup that we know how much real time has actually
6951 * elapsed; our helper function, get_kernel_ns() will be using boot
6952 * variables that haven't been updated yet.
6953 *
6954 * So we simply find the maximum observed TSC above, then record the
6955 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
6956 * the adjustment will be applied. Note that we accumulate
6957 * adjustments, in case multiple suspend cycles happen before some VCPU
6958 * gets a chance to run again. In the event that no KVM threads get a
6959 * chance to run, we will miss the entire elapsed period, as we'll have
6960 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
6961 * loose cycle time. This isn't too big a deal, since the loss will be
6962 * uniform across all VCPUs (not to mention the scenario is extremely
6963 * unlikely). It is possible that a second hibernate recovery happens
6964 * much faster than a first, causing the observed TSC here to be
6965 * smaller; this would require additional padding adjustment, which is
6966 * why we set last_host_tsc to the local tsc observed here.
6967 *
6968 * N.B. - this code below runs only on platforms with reliable TSC,
6969 * as that is the only way backwards_tsc is set above. Also note
6970 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
6971 * have the same delta_cyc adjustment applied if backwards_tsc
6972 * is detected. Note further, this adjustment is only done once,
6973 * as we reset last_host_tsc on all VCPUs to stop this from being
6974 * called multiple times (one for each physical CPU bringup).
6975 *
Guo Chao4a969982012-06-28 15:17:27 +08006976 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006977 * will be compensated by the logic in vcpu_load, which sets the TSC to
6978 * catchup mode. This will catchup all VCPUs to real time, but cannot
6979 * guarantee that they stay in perfect synchronization.
6980 */
6981 if (backwards_tsc) {
6982 u64 delta_cyc = max_tsc - local_tsc;
Marcelo Tosatti16a96022014-05-14 12:43:24 -03006983 backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006984 list_for_each_entry(kvm, &vm_list, vm_list) {
6985 kvm_for_each_vcpu(i, vcpu, kvm) {
6986 vcpu->arch.tsc_offset_adjustment += delta_cyc;
6987 vcpu->arch.last_host_tsc = local_tsc;
Marcelo Tosattid8281992012-11-27 23:29:01 -02006988 set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
6989 &vcpu->requests);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02006990 }
6991
6992 /*
6993 * We have to disable TSC offset matching.. if you were
6994 * booting a VM while issuing an S4 host suspend....
6995 * you may have some problem. Solving this issue is
6996 * left as an exercise to the reader.
6997 */
6998 kvm->arch.last_tsc_nsec = 0;
6999 kvm->arch.last_tsc_write = 0;
7000 }
7001
7002 }
7003 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007004}
7005
7006void kvm_arch_hardware_disable(void *garbage)
7007{
7008 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02007009 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007010}
7011
7012int kvm_arch_hardware_setup(void)
7013{
7014 return kvm_x86_ops->hardware_setup();
7015}
7016
7017void kvm_arch_hardware_unsetup(void)
7018{
7019 kvm_x86_ops->hardware_unsetup();
7020}
7021
7022void kvm_arch_check_processor_compat(void *rtn)
7023{
7024 kvm_x86_ops->check_processor_compatibility(rtn);
7025}
7026
Avi Kivity3e515702012-03-05 14:23:29 +02007027bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
7028{
7029 return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
7030}
7031
Gleb Natapov54e98182012-08-05 15:58:32 +03007032struct static_key kvm_no_apic_vcpu __read_mostly;
7033
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007034int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
7035{
7036 struct page *page;
7037 struct kvm *kvm;
7038 int r;
7039
7040 BUG_ON(vcpu->kvm == NULL);
7041 kvm = vcpu->kvm;
7042
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307043 vcpu->arch.pv.pv_unhalted = false;
Avi Kivity9aabc88f2010-07-29 15:11:50 +03007044 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03007045 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03007046 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007047 else
Avi Kivitya4535292008-04-13 17:54:35 +03007048 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007049
7050 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
7051 if (!page) {
7052 r = -ENOMEM;
7053 goto fail;
7054 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007055 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007056
Zachary Amsdencc578282012-02-03 15:43:50 -02007057 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007058
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007059 r = kvm_mmu_create(vcpu);
7060 if (r < 0)
7061 goto fail_free_pio_data;
7062
7063 if (irqchip_in_kernel(kvm)) {
7064 r = kvm_create_lapic(vcpu);
7065 if (r < 0)
7066 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03007067 } else
7068 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007069
Huang Ying890ca9a2009-05-11 16:48:15 +08007070 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
7071 GFP_KERNEL);
7072 if (!vcpu->arch.mce_banks) {
7073 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08007074 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08007075 }
7076 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
7077
Wei Yongjunf1797352013-04-18 07:41:00 +08007078 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
7079 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007080 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08007081 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007082
Julian Stecklina66f7b722012-12-05 15:26:19 +01007083 r = fx_init(vcpu);
7084 if (r)
7085 goto fail_free_wbinvd_dirty_mask;
7086
Will Auldba904632012-11-29 12:42:50 -08007087 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
Andy Honig0b794592013-02-20 14:48:10 -08007088 vcpu->arch.pv_time_enabled = false;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007089
7090 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +02007091 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007092
Gleb Natapovaf585b92010-10-14 11:22:46 +02007093 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007094 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007095
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007096 return 0;
Julian Stecklina66f7b722012-12-05 15:26:19 +01007097fail_free_wbinvd_dirty_mask:
7098 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007099fail_free_mce_banks:
7100 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08007101fail_free_lapic:
7102 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007103fail_mmu_destroy:
7104 kvm_mmu_destroy(vcpu);
7105fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007106 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007107fail:
7108 return r;
7109}
7110
7111void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
7112{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007113 int idx;
7114
Gleb Natapovf5132b02011-11-10 14:57:22 +02007115 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08007116 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007117 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007118 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007119 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007120 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007121 free_page((unsigned long)vcpu->arch.pio_data);
Gleb Natapov54e98182012-08-05 15:58:32 +03007122 if (!irqchip_in_kernel(vcpu->kvm))
7123 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007124}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007125
Carsten Ottee08b9632012-01-04 10:25:20 +01007126int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007127{
Carsten Ottee08b9632012-01-04 10:25:20 +01007128 if (type)
7129 return -EINVAL;
7130
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08007131 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08007132 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007133 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007134 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007135
Sheng Yang5550af42008-10-15 20:15:06 +08007136 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
7137 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06007138 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
7139 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
7140 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08007141
Jan Kiszka038f8c12011-02-04 10:49:11 +01007142 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007143 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007144 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
7145
7146 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01007147
Andrew Jones7e44e442014-02-28 12:52:54 +01007148 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01007149 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01007150
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01007151 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007152}
7153
7154static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
7155{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007156 int r;
7157 r = vcpu_load(vcpu);
7158 BUG_ON(r);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007159 kvm_mmu_unload(vcpu);
7160 vcpu_put(vcpu);
7161}
7162
7163static void kvm_free_vcpus(struct kvm *kvm)
7164{
7165 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007166 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007167
7168 /*
7169 * Unpin any mmu pages first.
7170 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02007171 kvm_for_each_vcpu(i, vcpu, kvm) {
7172 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007173 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007174 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007175 kvm_for_each_vcpu(i, vcpu, kvm)
7176 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007177
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007178 mutex_lock(&kvm->lock);
7179 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
7180 kvm->vcpus[i] = NULL;
7181
7182 atomic_set(&kvm->online_vcpus, 0);
7183 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007184}
7185
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007186void kvm_arch_sync_events(struct kvm *kvm)
7187{
Andrew Jones332967a2014-02-28 12:52:55 +01007188 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01007189 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Sheng Yangba4cef32009-01-06 10:03:03 +08007190 kvm_free_all_assigned_devices(kvm);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08007191 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007192}
7193
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007194void kvm_arch_destroy_vm(struct kvm *kvm)
7195{
Andrew Honig27469d22013-04-18 09:38:14 -07007196 if (current->mm == kvm->mm) {
7197 /*
7198 * Free memory regions allocated on behalf of userspace,
7199 * unless the the memory map has changed due to process exit
7200 * or fd copying.
7201 */
7202 struct kvm_userspace_memory_region mem;
7203 memset(&mem, 0, sizeof(mem));
7204 mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
7205 kvm_set_memory_region(kvm, &mem);
7206
7207 mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
7208 kvm_set_memory_region(kvm, &mem);
7209
7210 mem.slot = TSS_PRIVATE_MEMSLOT;
7211 kvm_set_memory_region(kvm, &mem);
7212 }
Sheng Yang6eb55812008-10-31 12:37:41 +08007213 kvm_iommu_unmap_guest(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08007214 kfree(kvm->arch.vpic);
7215 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007216 kvm_free_vcpus(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02007217 if (kvm->arch.apic_access_page)
7218 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007219 if (kvm->arch.ept_identity_pagetable)
7220 put_page(kvm->arch.ept_identity_pagetable);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007221 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007222}
Zhang Xiantao0de10342007-11-20 16:25:04 +08007223
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307224void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007225 struct kvm_memory_slot *dont)
7226{
7227 int i;
7228
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007229 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7230 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
7231 kvm_kvfree(free->arch.rmap[i]);
7232 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007233 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007234 if (i == 0)
7235 continue;
7236
7237 if (!dont || free->arch.lpage_info[i - 1] !=
7238 dont->arch.lpage_info[i - 1]) {
7239 kvm_kvfree(free->arch.lpage_info[i - 1]);
7240 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007241 }
7242 }
7243}
7244
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307245int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7246 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007247{
7248 int i;
7249
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007250 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007251 unsigned long ugfn;
7252 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007253 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007254
7255 lpages = gfn_to_index(slot->base_gfn + npages - 1,
7256 slot->base_gfn, level) + 1;
7257
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007258 slot->arch.rmap[i] =
7259 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
7260 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007261 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007262 if (i == 0)
7263 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007264
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007265 slot->arch.lpage_info[i - 1] = kvm_kvzalloc(lpages *
7266 sizeof(*slot->arch.lpage_info[i - 1]));
7267 if (!slot->arch.lpage_info[i - 1])
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007268 goto out_free;
7269
7270 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007271 slot->arch.lpage_info[i - 1][0].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007272 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007273 slot->arch.lpage_info[i - 1][lpages - 1].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007274 ugfn = slot->userspace_addr >> PAGE_SHIFT;
7275 /*
7276 * If the gfn and userspace address are not aligned wrt each
7277 * other, or if explicitly asked to, disable large page
7278 * support for this slot
7279 */
7280 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
7281 !kvm_largepages_enabled()) {
7282 unsigned long j;
7283
7284 for (j = 0; j < lpages; ++j)
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007285 slot->arch.lpage_info[i - 1][j].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007286 }
7287 }
7288
7289 return 0;
7290
7291out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007292 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7293 kvm_kvfree(slot->arch.rmap[i]);
7294 slot->arch.rmap[i] = NULL;
7295 if (i == 0)
7296 continue;
7297
7298 kvm_kvfree(slot->arch.lpage_info[i - 1]);
7299 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007300 }
7301 return -ENOMEM;
7302}
7303
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007304void kvm_arch_memslots_updated(struct kvm *kvm)
7305{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09007306 /*
7307 * memslots->generation has been incremented.
7308 * mmio generation may have reached its maximum value.
7309 */
7310 kvm_mmu_invalidate_mmio_sptes(kvm);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007311}
7312
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007313int kvm_arch_prepare_memory_region(struct kvm *kvm,
7314 struct kvm_memory_slot *memslot,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007315 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007316 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08007317{
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007318 /*
7319 * Only private memory slots need to be mapped here since
7320 * KVM_SET_MEMORY_REGION ioctl is no longer supported.
Zhang Xiantao0de10342007-11-20 16:25:04 +08007321 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007322 if ((memslot->id >= KVM_USER_MEM_SLOTS) && (change == KVM_MR_CREATE)) {
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007323 unsigned long userspace_addr;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007324
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007325 /*
7326 * MAP_SHARED to prevent internal slot pages from being moved
7327 * by fork()/COW.
7328 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007329 userspace_addr = vm_mmap(NULL, 0, memslot->npages * PAGE_SIZE,
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007330 PROT_READ | PROT_WRITE,
7331 MAP_SHARED | MAP_ANONYMOUS, 0);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007332
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007333 if (IS_ERR((void *)userspace_addr))
7334 return PTR_ERR((void *)userspace_addr);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007335
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007336 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08007337 }
7338
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007339 return 0;
7340}
7341
7342void kvm_arch_commit_memory_region(struct kvm *kvm,
7343 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007344 const struct kvm_memory_slot *old,
7345 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007346{
7347
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007348 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007349
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007350 if ((mem->slot >= KVM_USER_MEM_SLOTS) && (change == KVM_MR_DELETE)) {
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007351 int ret;
7352
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007353 ret = vm_munmap(old->userspace_addr,
7354 old->npages * PAGE_SIZE);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007355 if (ret < 0)
7356 printk(KERN_WARNING
7357 "kvm_vm_ioctl_set_memory_region: "
7358 "failed to munmap memory\n");
7359 }
7360
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007361 if (!kvm->arch.n_requested_mmu_pages)
7362 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007363
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007364 if (nr_mmu_pages)
7365 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007366 /*
7367 * Write protect all pages for dirty logging.
Xiao Guangrongc126d942014-04-17 17:06:14 +08007368 *
7369 * All the sptes including the large sptes which point to this
7370 * slot are set to readonly. We can not create any new large
7371 * spte on this slot until the end of the logging.
7372 *
7373 * See the comments in fast_page_fault().
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007374 */
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007375 if ((change != KVM_MR_DELETE) && (mem->flags & KVM_MEM_LOG_DIRTY_PAGES))
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007376 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007377}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007378
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007379void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007380{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08007381 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007382}
7383
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007384void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
7385 struct kvm_memory_slot *slot)
7386{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08007387 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007388}
7389
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007390int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
7391{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007392 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
7393 kvm_x86_ops->check_nested_events(vcpu, false);
7394
Gleb Natapovaf585b92010-10-14 11:22:46 +02007395 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
7396 !vcpu->arch.apf.halted)
7397 || !list_empty_careful(&vcpu->async_pf.done)
Jan Kiszka66450a22013-03-13 12:42:34 +01007398 || kvm_apic_has_events(vcpu)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307399 || vcpu->arch.pv.pv_unhalted
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007400 || atomic_read(&vcpu->arch.nmi_queued) ||
Gleb Natapova1b37102009-07-09 15:33:52 +03007401 (kvm_arch_interrupt_allowed(vcpu) &&
7402 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007403}
Zhang Xiantao57361992007-12-17 14:21:40 +08007404
Christoffer Dallb6d33832012-03-08 16:44:24 -05007405int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08007406{
Christoffer Dallb6d33832012-03-08 16:44:24 -05007407 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08007408}
Gleb Natapov78646122009-03-23 12:12:11 +02007409
7410int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
7411{
7412 return kvm_x86_ops->interrupt_allowed(vcpu);
7413}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007414
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007415bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
7416{
7417 unsigned long current_rip = kvm_rip_read(vcpu) +
7418 get_segment_base(vcpu, VCPU_SREG_CS);
7419
7420 return current_rip == linear_rip;
7421}
7422EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
7423
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007424unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
7425{
7426 unsigned long rflags;
7427
7428 rflags = kvm_x86_ops->get_rflags(vcpu);
7429 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01007430 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007431 return rflags;
7432}
7433EXPORT_SYMBOL_GPL(kvm_get_rflags);
7434
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007435static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007436{
7437 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007438 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01007439 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007440 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007441}
7442
7443void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
7444{
7445 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03007446 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007447}
7448EXPORT_SYMBOL_GPL(kvm_set_rflags);
7449
Gleb Natapov56028d02010-10-17 18:13:42 +02007450void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
7451{
7452 int r;
7453
Xiao Guangrongfb67e142010-12-07 10:35:25 +08007454 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08007455 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02007456 return;
7457
7458 r = kvm_mmu_reload(vcpu);
7459 if (unlikely(r))
7460 return;
7461
Xiao Guangrongfb67e142010-12-07 10:35:25 +08007462 if (!vcpu->arch.mmu.direct_map &&
7463 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
7464 return;
7465
Gleb Natapov56028d02010-10-17 18:13:42 +02007466 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
7467}
7468
Gleb Natapovaf585b92010-10-14 11:22:46 +02007469static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
7470{
7471 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
7472}
7473
7474static inline u32 kvm_async_pf_next_probe(u32 key)
7475{
7476 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
7477}
7478
7479static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7480{
7481 u32 key = kvm_async_pf_hash_fn(gfn);
7482
7483 while (vcpu->arch.apf.gfns[key] != ~0)
7484 key = kvm_async_pf_next_probe(key);
7485
7486 vcpu->arch.apf.gfns[key] = gfn;
7487}
7488
7489static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
7490{
7491 int i;
7492 u32 key = kvm_async_pf_hash_fn(gfn);
7493
7494 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08007495 (vcpu->arch.apf.gfns[key] != gfn &&
7496 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02007497 key = kvm_async_pf_next_probe(key);
7498
7499 return key;
7500}
7501
7502bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7503{
7504 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
7505}
7506
7507static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7508{
7509 u32 i, j, k;
7510
7511 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
7512 while (true) {
7513 vcpu->arch.apf.gfns[i] = ~0;
7514 do {
7515 j = kvm_async_pf_next_probe(j);
7516 if (vcpu->arch.apf.gfns[j] == ~0)
7517 return;
7518 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
7519 /*
7520 * k lies cyclically in ]i,j]
7521 * | i.k.j |
7522 * |....j i.k.| or |.k..j i...|
7523 */
7524 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
7525 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
7526 i = j;
7527 }
7528}
7529
Gleb Natapov7c907052010-10-14 11:22:53 +02007530static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
7531{
7532
7533 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
7534 sizeof(val));
7535}
7536
Gleb Natapovaf585b92010-10-14 11:22:46 +02007537void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
7538 struct kvm_async_pf *work)
7539{
Avi Kivity6389ee92010-11-29 16:12:30 +02007540 struct x86_exception fault;
7541
Gleb Natapov7c907052010-10-14 11:22:53 +02007542 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007543 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02007544
7545 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02007546 (vcpu->arch.apf.send_user_only &&
7547 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02007548 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
7549 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02007550 fault.vector = PF_VECTOR;
7551 fault.error_code_valid = true;
7552 fault.error_code = 0;
7553 fault.nested_page_fault = false;
7554 fault.address = work->arch.token;
7555 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02007556 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02007557}
7558
7559void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
7560 struct kvm_async_pf *work)
7561{
Avi Kivity6389ee92010-11-29 16:12:30 +02007562 struct x86_exception fault;
7563
Gleb Natapov7c907052010-10-14 11:22:53 +02007564 trace_kvm_async_pf_ready(work->arch.token, work->gva);
chai wenf2e10662013-10-14 22:22:33 +08007565 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02007566 work->arch.token = ~0; /* broadcast wakeup */
7567 else
7568 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
7569
7570 if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
7571 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02007572 fault.vector = PF_VECTOR;
7573 fault.error_code_valid = true;
7574 fault.error_code = 0;
7575 fault.nested_page_fault = false;
7576 fault.address = work->arch.token;
7577 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02007578 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08007579 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03007580 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02007581}
7582
7583bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
7584{
7585 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
7586 return true;
7587 else
7588 return !kvm_event_needs_reinjection(vcpu) &&
7589 kvm_x86_ops->interrupt_allowed(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007590}
7591
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007592void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
7593{
7594 atomic_inc(&kvm->arch.noncoherent_dma_count);
7595}
7596EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
7597
7598void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
7599{
7600 atomic_dec(&kvm->arch.noncoherent_dma_count);
7601}
7602EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
7603
7604bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
7605{
7606 return atomic_read(&kvm->arch.noncoherent_dma_count);
7607}
7608EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
7609
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007610EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
7611EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
7612EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
7613EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
7614EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02007615EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02007616EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02007617EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02007618EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02007619EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02007620EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01007621EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09007622EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);