blob: 8d73ec8a236437af4690e67ad56493cd4b06f881 [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030014 * Amit Shah <amit.shah@qumranet.com>
15 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020016 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 */
21
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020023#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080024#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080025#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020026#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030027#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030028#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020029#include "cpuid.h"
Paolo Bonzinic9eab582014-11-24 15:27:17 +010030#include "assigned-dev.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020031#include "pmu.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020032
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020033#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030034#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020035#include <linux/kvm.h>
36#include <linux/fs.h>
37#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010038#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080039#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050040#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010041#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030042#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010043#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030044#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020045#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080047#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080048#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020049#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020050#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020051#include <linux/timekeeper_internal.h>
52#include <linux/pvclock_gtod.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030053#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080054
Marcelo Tosatti229456f2009-06-17 09:22:14 -030055#define CREATE_TRACE_POINTS
56#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020057
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020058#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080059#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020060#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080061#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020062#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020063#include <asm/fpu/internal.h> /* Ugh! */
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
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300101static bool __read_mostly kvmclock_periodic_sync = true;
102module_param(kvmclock_periodic_sync, bool, S_IRUGO);
103
Joerg Roedel92a1f122011-03-25 09:44:51 +0100104bool kvm_has_tsc_control;
105EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
106u32 kvm_max_guest_tsc_khz;
107EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
108
Zachary Amsdencc578282012-02-03 15:43:50 -0200109/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
110static u32 tsc_tolerance_ppm = 250;
111module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
112
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500113/* lapic timer advance (tscdeadline mode only) in nanoseconds */
114unsigned int lapic_timer_advance_ns = 0;
115module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
116
Marcelo Tosatti16a96022014-05-14 12:43:24 -0300117static bool backwards_tsc_observed = false;
118
Avi Kivity18863bd2009-09-07 11:12:18 +0300119#define KVM_NR_SHARED_MSRS 16
120
121struct kvm_shared_msrs_global {
122 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800123 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300124};
125
126struct kvm_shared_msrs {
127 struct user_return_notifier urn;
128 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800129 struct kvm_shared_msr_values {
130 u64 host;
131 u64 curr;
132 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300133};
134
135static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200136static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300137
Hollis Blanchard417bc302007-10-31 17:24:23 -0500138struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200139 { "pf_fixed", VCPU_STAT(pf_fixed) },
140 { "pf_guest", VCPU_STAT(pf_guest) },
141 { "tlb_flush", VCPU_STAT(tlb_flush) },
142 { "invlpg", VCPU_STAT(invlpg) },
143 { "exits", VCPU_STAT(exits) },
144 { "io_exits", VCPU_STAT(io_exits) },
145 { "mmio_exits", VCPU_STAT(mmio_exits) },
146 { "signal_exits", VCPU_STAT(signal_exits) },
147 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800148 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200149 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100150 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200151 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530152 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200153 { "request_irq", VCPU_STAT(request_irq_exits) },
154 { "irq_exits", VCPU_STAT(irq_exits) },
155 { "host_state_reload", VCPU_STAT(host_state_reload) },
156 { "efer_reload", VCPU_STAT(efer_reload) },
157 { "fpu_reload", VCPU_STAT(fpu_reload) },
158 { "insn_emulation", VCPU_STAT(insn_emulation) },
159 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300160 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200161 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200162 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
163 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
164 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
165 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
166 { "mmu_flooded", VM_STAT(mmu_flooded) },
167 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200168 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300169 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200170 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300171 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500172 { NULL }
173};
174
Dexuan Cui2acf9232010-06-10 11:27:12 +0800175u64 __read_mostly host_xcr0;
176
Jan Kiszkab6785de2012-09-20 07:43:17 +0200177static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300178
Gleb Natapovaf585b92010-10-14 11:22:46 +0200179static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
180{
181 int i;
182 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
183 vcpu->arch.apf.gfns[i] = ~0;
184}
185
Avi Kivity18863bd2009-09-07 11:12:18 +0300186static void kvm_on_user_return(struct user_return_notifier *urn)
187{
188 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300189 struct kvm_shared_msrs *locals
190 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800191 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300192
193 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800194 values = &locals->values[slot];
195 if (values->host != values->curr) {
196 wrmsrl(shared_msrs_global.msrs[slot], values->host);
197 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300198 }
199 }
200 locals->registered = false;
201 user_return_notifier_unregister(urn);
202}
203
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800204static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300205{
Avi Kivity18863bd2009-09-07 11:12:18 +0300206 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200207 unsigned int cpu = smp_processor_id();
208 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300209
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800210 /* only read, and nobody should modify it at this time,
211 * so don't need lock */
212 if (slot >= shared_msrs_global.nr) {
213 printk(KERN_ERR "kvm: invalid MSR slot!");
214 return;
215 }
216 rdmsrl_safe(msr, &value);
217 smsr->values[slot].host = value;
218 smsr->values[slot].curr = value;
219}
220
221void kvm_define_shared_msr(unsigned slot, u32 msr)
222{
Nadav Amit0123be42014-07-24 15:06:56 +0300223 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Avi Kivity18863bd2009-09-07 11:12:18 +0300224 if (slot >= shared_msrs_global.nr)
225 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800226 shared_msrs_global.msrs[slot] = msr;
227 /* we need ensured the shared_msr_global have been updated */
228 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300229}
230EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
231
232static void kvm_shared_msr_cpu_online(void)
233{
234 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300235
236 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800237 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300238}
239
Andy Honig8b3c3102014-08-27 11:16:44 -0700240int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300241{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200242 unsigned int cpu = smp_processor_id();
243 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700244 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300245
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800246 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Andy Honig8b3c3102014-08-27 11:16:44 -0700247 return 0;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800248 smsr->values[slot].curr = value;
Andy Honig8b3c3102014-08-27 11:16:44 -0700249 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
250 if (err)
251 return 1;
252
Avi Kivity18863bd2009-09-07 11:12:18 +0300253 if (!smsr->registered) {
254 smsr->urn.on_user_return = kvm_on_user_return;
255 user_return_notifier_register(&smsr->urn);
256 smsr->registered = true;
257 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700258 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300259}
260EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
261
Radim Krčmář13a34e02014-08-28 15:13:03 +0200262static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200263{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200264 unsigned int cpu = smp_processor_id();
265 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200266
267 if (smsr->registered)
268 kvm_on_user_return(&smsr->urn);
269}
270
Carsten Otte6866b832007-10-29 16:09:10 +0100271u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
272{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300273 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100274}
275EXPORT_SYMBOL_GPL(kvm_get_apic_base);
276
Jan Kiszka58cb6282014-01-24 16:48:44 +0100277int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100278{
Jan Kiszka58cb6282014-01-24 16:48:44 +0100279 u64 old_state = vcpu->arch.apic_base &
280 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
281 u64 new_state = msr_info->data &
282 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
283 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
284 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
285
286 if (!msr_info->host_initiated &&
287 ((msr_info->data & reserved_bits) != 0 ||
288 new_state == X2APIC_ENABLE ||
289 (new_state == MSR_IA32_APICBASE_ENABLE &&
290 old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
291 (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
292 old_state == 0)))
293 return 1;
294
295 kvm_lapic_set_base(vcpu, msr_info->data);
296 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100297}
298EXPORT_SYMBOL_GPL(kvm_set_apic_base);
299
Andi Kleen2605fc22014-05-02 00:44:37 +0200300asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000301{
302 /* Fault while not rebooting. We want the trace. */
303 BUG();
304}
305EXPORT_SYMBOL_GPL(kvm_spurious_fault);
306
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200307#define EXCPT_BENIGN 0
308#define EXCPT_CONTRIBUTORY 1
309#define EXCPT_PF 2
310
311static int exception_class(int vector)
312{
313 switch (vector) {
314 case PF_VECTOR:
315 return EXCPT_PF;
316 case DE_VECTOR:
317 case TS_VECTOR:
318 case NP_VECTOR:
319 case SS_VECTOR:
320 case GP_VECTOR:
321 return EXCPT_CONTRIBUTORY;
322 default:
323 break;
324 }
325 return EXCPT_BENIGN;
326}
327
Nadav Amitd6e8c852014-07-24 14:51:24 +0300328#define EXCPT_FAULT 0
329#define EXCPT_TRAP 1
330#define EXCPT_ABORT 2
331#define EXCPT_INTERRUPT 3
332
333static int exception_type(int vector)
334{
335 unsigned int mask;
336
337 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
338 return EXCPT_INTERRUPT;
339
340 mask = 1 << vector;
341
342 /* #DB is trap, as instruction watchpoints are handled elsewhere */
343 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
344 return EXCPT_TRAP;
345
346 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
347 return EXCPT_ABORT;
348
349 /* Reserved exceptions will result in fault */
350 return EXCPT_FAULT;
351}
352
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200353static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200354 unsigned nr, bool has_error, u32 error_code,
355 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200356{
357 u32 prev_nr;
358 int class1, class2;
359
Avi Kivity3842d132010-07-27 12:30:24 +0300360 kvm_make_request(KVM_REQ_EVENT, vcpu);
361
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200362 if (!vcpu->arch.exception.pending) {
363 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200364 if (has_error && !is_protmode(vcpu))
365 has_error = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200366 vcpu->arch.exception.pending = true;
367 vcpu->arch.exception.has_error_code = has_error;
368 vcpu->arch.exception.nr = nr;
369 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200370 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200371 return;
372 }
373
374 /* to check exception */
375 prev_nr = vcpu->arch.exception.nr;
376 if (prev_nr == DF_VECTOR) {
377 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300378 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200379 return;
380 }
381 class1 = exception_class(prev_nr);
382 class2 = exception_class(nr);
383 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
384 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
385 /* generate double fault per SDM Table 5-5 */
386 vcpu->arch.exception.pending = true;
387 vcpu->arch.exception.has_error_code = true;
388 vcpu->arch.exception.nr = DF_VECTOR;
389 vcpu->arch.exception.error_code = 0;
390 } else
391 /* replace previous exception with a new one in a hope
392 that instruction re-execution will regenerate lost
393 exception */
394 goto queue;
395}
396
Avi Kivity298101d2007-11-25 13:41:11 +0200397void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
398{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200399 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200400}
401EXPORT_SYMBOL_GPL(kvm_queue_exception);
402
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200403void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
404{
405 kvm_multiple_exception(vcpu, nr, false, 0, true);
406}
407EXPORT_SYMBOL_GPL(kvm_requeue_exception);
408
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100409void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200410{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100411 if (err)
412 kvm_inject_gp(vcpu, 0);
413 else
414 kvm_x86_ops->skip_emulated_instruction(vcpu);
415}
416EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200417
Avi Kivity6389ee92010-11-29 16:12:30 +0200418void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200419{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200420 ++vcpu->stat.pf_guest;
Avi Kivity6389ee92010-11-29 16:12:30 +0200421 vcpu->arch.cr2 = fault->address;
422 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200423}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300424EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200425
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200426static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200427{
Avi Kivity6389ee92010-11-29 16:12:30 +0200428 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
429 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200430 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200431 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200432
433 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200434}
435
Sheng Yang3419ffc2008-05-15 09:52:48 +0800436void kvm_inject_nmi(struct kvm_vcpu *vcpu)
437{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300438 atomic_inc(&vcpu->arch.nmi_queued);
439 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800440}
441EXPORT_SYMBOL_GPL(kvm_inject_nmi);
442
Avi Kivity298101d2007-11-25 13:41:11 +0200443void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
444{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200445 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200446}
447EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
448
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200449void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
450{
451 kvm_multiple_exception(vcpu, nr, true, error_code, true);
452}
453EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
454
Carsten Ottea03490e2007-10-29 16:09:35 +0100455/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300456 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
457 * a #GP and return false.
458 */
459bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200460{
Avi Kivity0a79b002009-09-01 12:03:25 +0300461 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
462 return true;
463 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
464 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100465}
Avi Kivity0a79b002009-09-01 12:03:25 +0300466EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100467
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300468bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
469{
470 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
471 return true;
472
473 kvm_queue_exception(vcpu, UD_VECTOR);
474 return false;
475}
476EXPORT_SYMBOL_GPL(kvm_require_dr);
477
Carsten Ottea03490e2007-10-29 16:09:35 +0100478/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200479 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200480 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200481 * can read from guest physical or from the guest's guest physical memory.
482 */
483int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
484 gfn_t ngfn, void *data, int offset, int len,
485 u32 access)
486{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200487 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200488 gfn_t real_gfn;
489 gpa_t ngpa;
490
491 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200492 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200493 if (real_gfn == UNMAPPED_GVA)
494 return -EFAULT;
495
496 real_gfn = gpa_to_gfn(real_gfn);
497
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200498 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200499}
500EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
501
Fengguang Wu69b00492015-01-19 22:33:39 +0800502static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200503 void *data, int offset, int len, u32 access)
504{
505 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
506 data, offset, len, access);
507}
508
Joerg Roedelec92fe42010-09-10 17:30:51 +0200509/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100510 * Load the pae pdptrs. Return true is they are all valid.
511 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200512int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100513{
514 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
515 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
516 int i;
517 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200518 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100519
Joerg Roedelff03a072010-09-10 17:30:57 +0200520 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
521 offset * sizeof(u64), sizeof(pdpte),
522 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100523 if (ret < 0) {
524 ret = 0;
525 goto out;
526 }
527 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300528 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800529 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100530 ret = 0;
531 goto out;
532 }
533 }
534 ret = 1;
535
Joerg Roedelff03a072010-09-10 17:30:57 +0200536 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300537 __set_bit(VCPU_EXREG_PDPTR,
538 (unsigned long *)&vcpu->arch.regs_avail);
539 __set_bit(VCPU_EXREG_PDPTR,
540 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100541out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100542
543 return ret;
544}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100545EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100546
Avi Kivityd835dfe2007-11-21 02:57:59 +0200547static bool pdptrs_changed(struct kvm_vcpu *vcpu)
548{
Joerg Roedelff03a072010-09-10 17:30:57 +0200549 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200550 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200551 int offset;
552 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200553 int r;
554
555 if (is_long_mode(vcpu) || !is_pae(vcpu))
556 return false;
557
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300558 if (!test_bit(VCPU_EXREG_PDPTR,
559 (unsigned long *)&vcpu->arch.regs_avail))
560 return true;
561
Avi Kivity9f8fe502010-12-05 17:30:00 +0200562 gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
563 offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200564 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
565 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200566 if (r < 0)
567 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200568 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200569out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200570
571 return changed;
572}
573
Avi Kivity49a9b072010-06-10 17:02:14 +0300574int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100575{
Sheng Yangaad82702010-05-12 16:40:42 +0800576 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800577 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800578
Avi Kivityf9a48e62010-01-06 19:10:22 +0200579 cr0 |= X86_CR0_ET;
580
Gleb Natapovab344822010-01-21 15:28:46 +0200581#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300582 if (cr0 & 0xffffffff00000000UL)
583 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200584#endif
585
586 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100587
Gleb Natapov0f122442010-04-28 19:15:31 +0300588 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
589 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100590
Gleb Natapov0f122442010-04-28 19:15:31 +0300591 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
592 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100593
594 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
595#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200596 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100597 int cs_db, cs_l;
598
Gleb Natapov0f122442010-04-28 19:15:31 +0300599 if (!is_pae(vcpu))
600 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100601 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300602 if (cs_l)
603 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100604 } else
605#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200606 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200607 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300608 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100609 }
610
Mao, Junjiead756a12012-07-02 01:18:48 +0000611 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
612 return 1;
613
Carsten Ottea03490e2007-10-29 16:09:35 +0100614 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100615
Lai Jiangshand170c412011-02-21 11:21:30 +0800616 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800617 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800618 kvm_async_pf_hash_reset(vcpu);
619 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800620
Sheng Yangaad82702010-05-12 16:40:42 +0800621 if ((cr0 ^ old_cr0) & update_bits)
622 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800623
624 if ((cr0 ^ old_cr0) & X86_CR0_CD)
625 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
626
Gleb Natapov0f122442010-04-28 19:15:31 +0300627 return 0;
628}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200629EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100630
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200631void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100632{
Avi Kivity49a9b072010-06-10 17:02:14 +0300633 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100634}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200635EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100636
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300637static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
638{
639 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
640 !vcpu->guest_xcr0_loaded) {
641 /* kvm_set_xcr() also depends on this */
642 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
643 vcpu->guest_xcr0_loaded = 1;
644 }
645}
646
647static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
648{
649 if (vcpu->guest_xcr0_loaded) {
650 if (vcpu->arch.xcr0 != host_xcr0)
651 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
652 vcpu->guest_xcr0_loaded = 0;
653 }
654}
655
Fengguang Wu69b00492015-01-19 22:33:39 +0800656static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800657{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000658 u64 xcr0 = xcr;
659 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200660 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800661
662 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
663 if (index != XCR_XFEATURE_ENABLED_MASK)
664 return 1;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800665 if (!(xcr0 & XSTATE_FP))
666 return 1;
667 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
668 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200669
670 /*
671 * Do not allow the guest to set bits that we do not support
672 * saving. However, xcr0 bit 0 is always set, even if the
673 * emulated CPU does not support XSAVE (see fx_init).
674 */
675 valid_bits = vcpu->arch.guest_supported_xcr0 | XSTATE_FP;
676 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800677 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200678
Liu, Jinsong390bd522014-02-24 10:58:09 +0000679 if ((!(xcr0 & XSTATE_BNDREGS)) != (!(xcr0 & XSTATE_BNDCSR)))
680 return 1;
681
Chao Peng612263b2014-10-22 17:35:24 +0800682 if (xcr0 & XSTATE_AVX512) {
683 if (!(xcr0 & XSTATE_YMM))
684 return 1;
685 if ((xcr0 & XSTATE_AVX512) != XSTATE_AVX512)
686 return 1;
687 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300688 kvm_put_guest_xcr0(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800689 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000690
691 if ((xcr0 ^ old_xcr0) & XSTATE_EXTEND_MASK)
692 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800693 return 0;
694}
695
696int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
697{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000698 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
699 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800700 kvm_inject_gp(vcpu, 0);
701 return 1;
702 }
703 return 0;
704}
705EXPORT_SYMBOL_GPL(kvm_set_xcr);
706
Avi Kivitya83b29c2010-06-10 17:02:15 +0300707int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100708{
Avi Kivityfc78f512009-12-07 12:16:48 +0200709 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800710 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
711 X86_CR4_SMEP | X86_CR4_SMAP;
712
Gleb Natapov0f122442010-04-28 19:15:31 +0300713 if (cr4 & CR4_RESERVED_BITS)
714 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100715
Dexuan Cui2acf9232010-06-10 11:27:12 +0800716 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
717 return 1;
718
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800719 if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
720 return 1;
721
Feng Wu97ec8c02014-04-01 17:46:34 +0800722 if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
723 return 1;
724
H. Peter Anvinafcbf132013-04-27 16:37:47 -0700725 if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800726 return 1;
727
Carsten Ottea03490e2007-10-29 16:09:35 +0100728 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300729 if (!(cr4 & X86_CR4_PAE))
730 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300731 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
732 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200733 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
734 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300735 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100736
Mao, Junjiead756a12012-07-02 01:18:48 +0000737 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
738 if (!guest_cpuid_has_pcid(vcpu))
739 return 1;
740
741 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
742 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
743 return 1;
744 }
745
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300746 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300747 return 1;
748
Mao, Junjiead756a12012-07-02 01:18:48 +0000749 if (((cr4 ^ old_cr4) & pdptr_bits) ||
750 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800751 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300752
Dexuan Cui2acf9232010-06-10 11:27:12 +0800753 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +0200754 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800755
Gleb Natapov0f122442010-04-28 19:15:31 +0300756 return 0;
757}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200758EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100759
Avi Kivity23902182010-06-10 17:02:16 +0300760int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100761{
Paolo Bonziniac146232014-11-10 13:53:25 +0100762#ifdef CONFIG_X86_64
Nadav Amit9d88fca2014-11-02 11:54:52 +0200763 cr3 &= ~CR3_PCID_INVD;
Paolo Bonziniac146232014-11-10 13:53:25 +0100764#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200765
Avi Kivity9f8fe502010-12-05 17:30:00 +0200766 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300767 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -0400768 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300769 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200770 }
771
Carsten Ottea03490e2007-10-29 16:09:35 +0100772 if (is_long_mode(vcpu)) {
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200773 if (cr3 & CR3_L_MODE_RESERVED_BITS)
774 return 1;
775 } else if (is_pae(vcpu) && is_paging(vcpu) &&
776 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300777 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100778
Gleb Natapov0f122442010-04-28 19:15:31 +0300779 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200780 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Paolo Bonzinid8d173d2013-10-02 16:56:11 +0200781 kvm_mmu_new_cr3(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300782 return 0;
783}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200784EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100785
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100786int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100787{
Gleb Natapov0f122442010-04-28 19:15:31 +0300788 if (cr8 & CR8_RESERVED_BITS)
789 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100790 if (irqchip_in_kernel(vcpu->kvm))
791 kvm_lapic_set_tpr(vcpu, cr8);
792 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800793 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300794 return 0;
795}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200796EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100797
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200798unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100799{
800 if (irqchip_in_kernel(vcpu->kvm))
801 return kvm_lapic_get_cr8(vcpu);
802 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800803 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100804}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200805EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100806
Nadav Amitae561ed2015-04-02 03:10:37 +0300807static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
808{
809 int i;
810
811 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
812 for (i = 0; i < KVM_NR_DB_REGS; i++)
813 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
814 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
815 }
816}
817
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100818static void kvm_update_dr6(struct kvm_vcpu *vcpu)
819{
820 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
821 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
822}
823
Jan Kiszkac8639012012-09-21 05:42:55 +0200824static void kvm_update_dr7(struct kvm_vcpu *vcpu)
825{
826 unsigned long dr7;
827
828 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
829 dr7 = vcpu->arch.guest_debug_dr7;
830 else
831 dr7 = vcpu->arch.dr7;
832 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100833 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
834 if (dr7 & DR7_BP_EN_MASK)
835 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200836}
837
Nadav Amit6f43ed02014-07-15 17:37:46 +0300838static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
839{
840 u64 fixed = DR6_FIXED_1;
841
842 if (!guest_cpuid_has_rtm(vcpu))
843 fixed |= DR6_RTM;
844 return fixed;
845}
846
Gleb Natapov338dbc92010-04-28 19:15:32 +0300847static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300848{
849 switch (dr) {
850 case 0 ... 3:
851 vcpu->arch.db[dr] = val;
852 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
853 vcpu->arch.eff_db[dr] = val;
854 break;
855 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300856 /* fall through */
857 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300858 if (val & 0xffffffff00000000ULL)
859 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300860 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100861 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300862 break;
863 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300864 /* fall through */
865 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300866 if (val & 0xffffffff00000000ULL)
867 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300868 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200869 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300870 break;
871 }
872
873 return 0;
874}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300875
876int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
877{
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300878 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +0300879 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300880 return 1;
881 }
882 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +0300883}
Gleb Natapov020df072010-04-13 10:05:23 +0300884EXPORT_SYMBOL_GPL(kvm_set_dr);
885
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300886int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300887{
888 switch (dr) {
889 case 0 ... 3:
890 *val = vcpu->arch.db[dr];
891 break;
892 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300893 /* fall through */
894 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100895 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
896 *val = vcpu->arch.dr6;
897 else
898 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300899 break;
900 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300901 /* fall through */
902 default: /* 7 */
903 *val = vcpu->arch.dr7;
904 break;
905 }
Gleb Natapov338dbc92010-04-28 19:15:32 +0300906 return 0;
907}
Gleb Natapov020df072010-04-13 10:05:23 +0300908EXPORT_SYMBOL_GPL(kvm_get_dr);
909
Avi Kivity022cd0e2011-11-10 14:57:23 +0200910bool kvm_rdpmc(struct kvm_vcpu *vcpu)
911{
912 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
913 u64 data;
914 int err;
915
Wei Huangc6702c92015-06-19 13:44:45 +0200916 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +0200917 if (err)
918 return err;
919 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
920 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
921 return err;
922}
923EXPORT_SYMBOL_GPL(kvm_rdpmc);
924
Carsten Otte043405e2007-10-10 17:16:19 +0200925/*
926 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
927 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
928 *
929 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400930 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200931 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
932 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +0200933 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400934
Carsten Otte043405e2007-10-10 17:16:19 +0200935static u32 msrs_to_save[] = {
936 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400937 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200938#ifdef CONFIG_X86_64
939 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
940#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800941 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Liu, Jinsong0dd376e2014-02-24 10:56:53 +0000942 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS
Carsten Otte043405e2007-10-10 17:16:19 +0200943};
944
945static unsigned num_msrs_to_save;
946
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200947static u32 emulated_msrs[] = {
948 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
949 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
950 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
951 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
952 HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
953 MSR_KVM_PV_EOI_EN,
954
Will Auldba904632012-11-29 12:42:50 -0800955 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800956 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +0200957 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300958 MSR_IA32_MCG_STATUS,
959 MSR_IA32_MCG_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +0200960 MSR_IA32_SMBASE,
Carsten Otte043405e2007-10-10 17:16:19 +0200961};
962
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200963static unsigned num_emulated_msrs;
964
Jan Kiszka384bb782013-04-20 10:52:36 +0200965bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100966{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200967 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +0200968 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +0100969
Alexander Graf1b2fd702009-02-02 16:23:51 +0100970 if (efer & EFER_FFXSR) {
971 struct kvm_cpuid_entry2 *feat;
972
973 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200974 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200975 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +0100976 }
977
Alexander Grafd8017472008-11-25 20:17:11 +0100978 if (efer & EFER_SVME) {
979 struct kvm_cpuid_entry2 *feat;
980
981 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200982 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200983 return false;
Alexander Grafd8017472008-11-25 20:17:11 +0100984 }
985
Jan Kiszka384bb782013-04-20 10:52:36 +0200986 return true;
987}
988EXPORT_SYMBOL_GPL(kvm_valid_efer);
989
990static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
991{
992 u64 old_efer = vcpu->arch.efer;
993
994 if (!kvm_valid_efer(vcpu, efer))
995 return 1;
996
997 if (is_paging(vcpu)
998 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
999 return 1;
1000
Carsten Otte15c4a642007-10-30 18:44:17 +01001001 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001002 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001003
Sheng Yanga3d204e2010-05-12 16:40:40 +08001004 kvm_x86_ops->set_efer(vcpu, efer);
1005
Sheng Yangaad82702010-05-12 16:40:42 +08001006 /* Update reserved bits */
1007 if ((efer ^ old_efer) & EFER_NX)
1008 kvm_mmu_reset_context(vcpu);
1009
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001010 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001011}
1012
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001013void kvm_enable_efer_bits(u64 mask)
1014{
1015 efer_reserved_bits &= ~mask;
1016}
1017EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1018
Carsten Otte15c4a642007-10-30 18:44:17 +01001019/*
1020 * Writes msr value into into the appropriate "register".
1021 * Returns 0 on success, non-0 otherwise.
1022 * Assumes vcpu_load() was already called.
1023 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001024int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01001025{
Nadav Amit854e8bb2014-09-16 03:24:05 +03001026 switch (msr->index) {
1027 case MSR_FS_BASE:
1028 case MSR_GS_BASE:
1029 case MSR_KERNEL_GS_BASE:
1030 case MSR_CSTAR:
1031 case MSR_LSTAR:
1032 if (is_noncanonical_address(msr->data))
1033 return 1;
1034 break;
1035 case MSR_IA32_SYSENTER_EIP:
1036 case MSR_IA32_SYSENTER_ESP:
1037 /*
1038 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1039 * non-canonical address is written on Intel but not on
1040 * AMD (which ignores the top 32-bits, because it does
1041 * not implement 64-bit SYSENTER).
1042 *
1043 * 64-bit code should hence be able to write a non-canonical
1044 * value on AMD. Making the address canonical ensures that
1045 * vmentry does not fail on Intel after writing a non-canonical
1046 * value, and that something deterministic happens if the guest
1047 * invokes 64-bit SYSENTER.
1048 */
1049 msr->data = get_canonical(msr->data);
1050 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001051 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001052}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001053EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001054
Carsten Otte313a3dc2007-10-11 19:16:52 +02001055/*
1056 * Adapt set_msr() to msr_io()'s calling convention
1057 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001058static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1059{
1060 struct msr_data msr;
1061 int r;
1062
1063 msr.index = index;
1064 msr.host_initiated = true;
1065 r = kvm_get_msr(vcpu, &msr);
1066 if (r)
1067 return r;
1068
1069 *data = msr.data;
1070 return 0;
1071}
1072
Carsten Otte313a3dc2007-10-11 19:16:52 +02001073static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1074{
Will Auld8fe8ab42012-11-29 12:42:12 -08001075 struct msr_data msr;
1076
1077 msr.data = *data;
1078 msr.index = index;
1079 msr.host_initiated = true;
1080 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001081}
1082
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001083#ifdef CONFIG_X86_64
1084struct pvclock_gtod_data {
1085 seqcount_t seq;
1086
1087 struct { /* extract of a clocksource struct */
1088 int vclock_mode;
1089 cycle_t cycle_last;
1090 cycle_t mask;
1091 u32 mult;
1092 u32 shift;
1093 } clock;
1094
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001095 u64 boot_ns;
1096 u64 nsec_base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001097};
1098
1099static struct pvclock_gtod_data pvclock_gtod_data;
1100
1101static void update_pvclock_gtod(struct timekeeper *tk)
1102{
1103 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001104 u64 boot_ns;
1105
Peter Zijlstra876e7882015-03-19 10:09:06 +01001106 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001107
1108 write_seqcount_begin(&vdata->seq);
1109
1110 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001111 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1112 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1113 vdata->clock.mask = tk->tkr_mono.mask;
1114 vdata->clock.mult = tk->tkr_mono.mult;
1115 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001116
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001117 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001118 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001119
1120 write_seqcount_end(&vdata->seq);
1121}
1122#endif
1123
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001124void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1125{
1126 /*
1127 * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
1128 * vcpu_enter_guest. This function is only called from
1129 * the physical CPU that is running vcpu.
1130 */
1131 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1132}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001133
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001134static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1135{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001136 int version;
1137 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001138 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +08001139 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001140
1141 if (!wall_clock)
1142 return;
1143
Avi Kivity9ed3c442010-05-04 15:00:37 +03001144 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1145 if (r)
1146 return;
1147
1148 if (version & 1)
1149 ++version; /* first time write, random junk */
1150
1151 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001152
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001153 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
1154
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001155 /*
1156 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001157 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001158 * wall clock specified here. guest system time equals host
1159 * system time for us, thus we must fill in host boot time here.
1160 */
Jason Wang923de3c2010-01-27 19:13:49 +08001161 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001162
Bruce Rogers4b648662012-07-20 10:44:24 -06001163 if (kvm->arch.kvmclock_offset) {
1164 struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
1165 boot = timespec_sub(boot, ts);
1166 }
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001167 wc.sec = boot.tv_sec;
1168 wc.nsec = boot.tv_nsec;
1169 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001170
1171 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1172
1173 version++;
1174 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001175}
1176
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001177static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1178{
1179 uint32_t quotient, remainder;
1180
1181 /* Don't try to replace with do_div(), this one calculates
1182 * "(dividend << 32) / divisor" */
1183 __asm__ ( "divl %4"
1184 : "=a" (quotient), "=d" (remainder)
1185 : "0" (0), "1" (dividend), "r" (divisor) );
1186 return quotient;
1187}
1188
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001189static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
1190 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001191{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001192 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001193 int32_t shift = 0;
1194 uint64_t tps64;
1195 uint32_t tps32;
1196
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001197 tps64 = base_khz * 1000LL;
1198 scaled64 = scaled_khz * 1000LL;
Jan Kiszka50933622010-09-26 13:00:53 +02001199 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001200 tps64 >>= 1;
1201 shift--;
1202 }
1203
1204 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001205 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1206 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001207 scaled64 >>= 1;
1208 else
1209 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001210 shift++;
1211 }
1212
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001213 *pshift = shift;
1214 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001215
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001216 pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
1217 __func__, base_khz, scaled_khz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001218}
1219
Zachary Amsden759379d2010-08-19 22:07:25 -10001220static inline u64 get_kernel_ns(void)
1221{
Thomas Gleixnerbb0b5812014-07-16 21:04:52 +00001222 return ktime_get_boot_ns();
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001223}
1224
Marcelo Tosattid8281992012-11-27 23:29:01 -02001225#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001226static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001227#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001228
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001229static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001230static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001231
Joerg Roedel857e4092011-03-25 09:44:50 +01001232static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Zachary Amsden759379d2010-08-19 22:07:25 -10001233{
Zachary Amsdencc578282012-02-03 15:43:50 -02001234 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
1235 vcpu->arch.virtual_tsc_shift);
Zachary Amsden759379d2010-08-19 22:07:25 -10001236}
1237
Zachary Amsdencc578282012-02-03 15:43:50 -02001238static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001239{
Zachary Amsdencc578282012-02-03 15:43:50 -02001240 u64 v = (u64)khz * (1000000 + ppm);
1241 do_div(v, 1000000);
1242 return v;
1243}
1244
1245static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
1246{
1247 u32 thresh_lo, thresh_hi;
1248 int use_scaling = 0;
1249
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001250 /* tsc_khz can be zero if TSC calibration fails */
1251 if (this_tsc_khz == 0)
1252 return;
1253
Zachary Amsdenc2855452010-09-18 14:38:15 -10001254 /* Compute a scale to convert nanoseconds in TSC cycles */
1255 kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
Zachary Amsdencc578282012-02-03 15:43:50 -02001256 &vcpu->arch.virtual_tsc_shift,
1257 &vcpu->arch.virtual_tsc_mult);
1258 vcpu->arch.virtual_tsc_khz = this_tsc_khz;
1259
1260 /*
1261 * Compute the variation in TSC rate which is acceptable
1262 * within the range of tolerance and decide if the
1263 * rate being applied is within that bounds of the hardware
1264 * rate. If so, no scaling or compensation need be done.
1265 */
1266 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1267 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
1268 if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
1269 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
1270 use_scaling = 1;
1271 }
1272 kvm_x86_ops->set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001273}
1274
1275static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1276{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001277 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001278 vcpu->arch.virtual_tsc_mult,
1279 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001280 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001281 return tsc;
1282}
1283
Fengguang Wu69b00492015-01-19 22:33:39 +08001284static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001285{
1286#ifdef CONFIG_X86_64
1287 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001288 struct kvm_arch *ka = &vcpu->kvm->arch;
1289 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1290
1291 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1292 atomic_read(&vcpu->kvm->online_vcpus));
1293
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001294 /*
1295 * Once the masterclock is enabled, always perform request in
1296 * order to update it.
1297 *
1298 * In order to enable masterclock, the host clocksource must be TSC
1299 * and the vcpus need to have matched TSCs. When that happens,
1300 * perform request to enable masterclock.
1301 */
1302 if (ka->use_master_clock ||
1303 (gtod->clock.vclock_mode == VCLOCK_TSC && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001304 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1305
1306 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1307 atomic_read(&vcpu->kvm->online_vcpus),
1308 ka->use_master_clock, gtod->clock.vclock_mode);
1309#endif
1310}
1311
Will Auldba904632012-11-29 12:42:50 -08001312static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1313{
1314 u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
1315 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1316}
1317
Will Auld8fe8ab42012-11-29 12:42:12 -08001318void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001319{
1320 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001321 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001322 unsigned long flags;
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001323 s64 usdiff;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001324 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001325 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001326 u64 data = msr->data;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001327
Jan Kiszka038f8c12011-02-04 10:49:11 +01001328 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Joerg Roedel857e4092011-03-25 09:44:50 +01001329 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001330 ns = get_kernel_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001331 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001332
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001333 if (vcpu->arch.virtual_tsc_khz) {
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001334 int faulted = 0;
1335
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001336 /* n.b - signed multiplication and division required */
1337 usdiff = data - kvm->arch.last_tsc_write;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001338#ifdef CONFIG_X86_64
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001339 usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001340#else
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001341 /* do_div() only does unsigned */
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001342 asm("1: idivl %[divisor]\n"
1343 "2: xor %%edx, %%edx\n"
1344 " movl $0, %[faulted]\n"
1345 "3:\n"
1346 ".section .fixup,\"ax\"\n"
1347 "4: movl $1, %[faulted]\n"
1348 " jmp 3b\n"
1349 ".previous\n"
1350
1351 _ASM_EXTABLE(1b, 4b)
1352
1353 : "=A"(usdiff), [faulted] "=r" (faulted)
1354 : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
1355
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001356#endif
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001357 do_div(elapsed, 1000);
1358 usdiff -= elapsed;
1359 if (usdiff < 0)
1360 usdiff = -usdiff;
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001361
1362 /* idivl overflow => difference is larger than USEC_PER_SEC */
1363 if (faulted)
1364 usdiff = USEC_PER_SEC;
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001365 } else
1366 usdiff = USEC_PER_SEC; /* disable TSC match window below */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001367
1368 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001369 * Special case: TSC write with a small delta (1 second) of virtual
1370 * cycle time against real time is interpreted as an attempt to
1371 * synchronize the CPU.
1372 *
1373 * For a reliable TSC, we can match TSC offsets, and for an unstable
1374 * TSC, we add elapsed time in this computation. We could let the
1375 * compensation code attempt to catch up if we fall behind, but
1376 * it's better to try to match offsets from the beginning.
1377 */
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001378 if (usdiff < USEC_PER_SEC &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001379 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001380 if (!check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001381 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001382 pr_debug("kvm: matched tsc offset for %llu\n", data);
1383 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001384 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001385 data += delta;
1386 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001387 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001388 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001389 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001390 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001391 } else {
1392 /*
1393 * We split periods of matched TSC writes into generations.
1394 * For each generation, we track the original measured
1395 * nanosecond time, offset, and write, so if TSCs are in
1396 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001397 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001398 *
1399 * These values are tracked in kvm->arch.cur_xxx variables.
1400 */
1401 kvm->arch.cur_tsc_generation++;
1402 kvm->arch.cur_tsc_nsec = ns;
1403 kvm->arch.cur_tsc_write = data;
1404 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001405 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001406 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001407 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001408 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001409
1410 /*
1411 * We also track th most recent recorded KHZ, write and time to
1412 * allow the matching interval to be extended at each write.
1413 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001414 kvm->arch.last_tsc_nsec = ns;
1415 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001416 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001417
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001418 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001419
1420 /* Keep track of which generation this VCPU has synchronized to */
1421 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1422 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1423 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1424
Will Auldba904632012-11-29 12:42:50 -08001425 if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
1426 update_ia32_tsc_adjust_msr(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001427 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1428 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001429
1430 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001431 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001432 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001433 } else if (!already_matched) {
1434 kvm->arch.nr_vcpus_matched_tsc++;
1435 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001436
1437 kvm_track_tsc_matching(vcpu);
1438 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001439}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001440
Zachary Amsden99e3e302010-08-19 22:07:17 -10001441EXPORT_SYMBOL_GPL(kvm_write_tsc);
1442
Marcelo Tosattid8281992012-11-27 23:29:01 -02001443#ifdef CONFIG_X86_64
1444
1445static cycle_t read_tsc(void)
1446{
Andy Lutomirski03b97302015-06-25 18:44:08 +02001447 cycle_t ret = (cycle_t)rdtsc_ordered();
1448 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001449
1450 if (likely(ret >= last))
1451 return ret;
1452
1453 /*
1454 * GCC likes to generate cmov here, but this branch is extremely
1455 * predictable (it's just a funciton of time and the likely is
1456 * very likely) and there's a data dependence, so force GCC
1457 * to generate a branch instead. I don't barrier() because
1458 * we don't actually need a barrier, and if this function
1459 * ever gets inlined it will generate worse code.
1460 */
1461 asm volatile ("");
1462 return last;
1463}
1464
1465static inline u64 vgettsc(cycle_t *cycle_now)
1466{
1467 long v;
1468 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1469
1470 *cycle_now = read_tsc();
1471
1472 v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
1473 return v * gtod->clock.mult;
1474}
1475
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001476static int do_monotonic_boot(s64 *t, cycle_t *cycle_now)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001477{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001478 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001479 unsigned long seq;
1480 int mode;
1481 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001482
Marcelo Tosattid8281992012-11-27 23:29:01 -02001483 do {
1484 seq = read_seqcount_begin(&gtod->seq);
1485 mode = gtod->clock.vclock_mode;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001486 ns = gtod->nsec_base;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001487 ns += vgettsc(cycle_now);
1488 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001489 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001490 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001491 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001492
1493 return mode;
1494}
1495
1496/* returns true if host is using tsc clocksource */
1497static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
1498{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001499 /* checked again under seqlock below */
1500 if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
1501 return false;
1502
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001503 return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001504}
1505#endif
1506
1507/*
1508 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001509 * Assuming a stable TSC across physical CPUS, and a stable TSC
1510 * across virtual CPUs, the following condition is possible.
1511 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001512 * CPUs at the next numbered event.
1513 *
1514 * "timespecX" represents host monotonic time. "tscX" represents
1515 * RDTSC value.
1516 *
1517 * VCPU0 on CPU0 | VCPU1 on CPU1
1518 *
1519 * 1. read timespec0,tsc0
1520 * 2. | timespec1 = timespec0 + N
1521 * | tsc1 = tsc0 + M
1522 * 3. transition to guest | transition to guest
1523 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1524 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1525 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1526 *
1527 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1528 *
1529 * - ret0 < ret1
1530 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1531 * ...
1532 * - 0 < N - M => M < N
1533 *
1534 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1535 * always the case (the difference between two distinct xtime instances
1536 * might be smaller then the difference between corresponding TSC reads,
1537 * when updating guest vcpus pvclock areas).
1538 *
1539 * To avoid that problem, do not allow visibility of distinct
1540 * system_timestamp/tsc_timestamp values simultaneously: use a master
1541 * copy of host monotonic time values. Update that master copy
1542 * in lockstep.
1543 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001544 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001545 *
1546 */
1547
1548static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1549{
1550#ifdef CONFIG_X86_64
1551 struct kvm_arch *ka = &kvm->arch;
1552 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001553 bool host_tsc_clocksource, vcpus_matched;
1554
1555 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1556 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001557
1558 /*
1559 * If the host uses TSC clock, then passthrough TSC as stable
1560 * to the guest.
1561 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001562 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001563 &ka->master_kernel_ns,
1564 &ka->master_cycle_now);
1565
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001566 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Marcelo Tosatti54750f22015-01-20 15:54:52 -02001567 && !backwards_tsc_observed
1568 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001569
Marcelo Tosattid8281992012-11-27 23:29:01 -02001570 if (ka->use_master_clock)
1571 atomic_set(&kvm_guest_has_master_clock, 1);
1572
1573 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001574 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1575 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001576#endif
1577}
1578
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001579static void kvm_gen_update_masterclock(struct kvm *kvm)
1580{
1581#ifdef CONFIG_X86_64
1582 int i;
1583 struct kvm_vcpu *vcpu;
1584 struct kvm_arch *ka = &kvm->arch;
1585
1586 spin_lock(&ka->pvclock_gtod_sync_lock);
1587 kvm_make_mclock_inprogress_request(kvm);
1588 /* no guest entries from this point */
1589 pvclock_update_vm_gtod_copy(kvm);
1590
1591 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001592 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001593
1594 /* guest entries allowed */
1595 kvm_for_each_vcpu(i, vcpu, kvm)
1596 clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
1597
1598 spin_unlock(&ka->pvclock_gtod_sync_lock);
1599#endif
1600}
1601
Zachary Amsden34c238a2010-09-18 14:38:14 -10001602static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001603{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001604 unsigned long flags, this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001605 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001606 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02001607 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001608 u64 tsc_timestamp, host_tsc;
Andy Honig0b794592013-02-20 14:48:10 -08001609 struct pvclock_vcpu_time_info guest_hv_clock;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001610 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001611 bool use_master_clock;
1612
1613 kernel_ns = 0;
1614 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001615
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001616 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02001617 * If the host uses TSC clock, then passthrough TSC as stable
1618 * to the guest.
1619 */
1620 spin_lock(&ka->pvclock_gtod_sync_lock);
1621 use_master_clock = ka->use_master_clock;
1622 if (use_master_clock) {
1623 host_tsc = ka->master_cycle_now;
1624 kernel_ns = ka->master_kernel_ns;
1625 }
1626 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001627
1628 /* Keep irq disabled to prevent changes to the clock */
1629 local_irq_save(flags);
Christoph Lameter89cbc762014-08-17 12:30:40 -05001630 this_tsc_khz = __this_cpu_read(cpu_tsc_khz);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001631 if (unlikely(this_tsc_khz == 0)) {
1632 local_irq_restore(flags);
1633 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
1634 return 1;
1635 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001636 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001637 host_tsc = rdtsc();
Marcelo Tosattid8281992012-11-27 23:29:01 -02001638 kernel_ns = get_kernel_ns();
1639 }
1640
1641 tsc_timestamp = kvm_x86_ops->read_l1_tsc(v, host_tsc);
1642
1643 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10001644 * We may have to catch up the TSC to match elapsed wall clock
1645 * time for two reasons, even if kvmclock is used.
1646 * 1) CPU could have been running below the maximum TSC rate
1647 * 2) Broken TSC compensation resets the base at each VCPU
1648 * entry to avoid unknown leaps of TSC even when running
1649 * again on the same CPU. This may cause apparent elapsed
1650 * time to disappear, and the guest to stand still or run
1651 * very slowly.
1652 */
1653 if (vcpu->tsc_catchup) {
1654 u64 tsc = compute_guest_tsc(v, kernel_ns);
1655 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001656 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001657 tsc_timestamp = tsc;
1658 }
1659 }
1660
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001661 local_irq_restore(flags);
1662
Andy Honig0b794592013-02-20 14:48:10 -08001663 if (!vcpu->pv_time_enabled)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001664 return 0;
1665
Zachary Amsdene48672f2010-08-19 22:07:23 -10001666 if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001667 kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
1668 &vcpu->hv_clock.tsc_shift,
1669 &vcpu->hv_clock.tsc_to_system_mul);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001670 vcpu->hw_tsc_khz = this_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001671 }
1672
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001673 /* With all the info we got, fill in the values */
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001674 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10001675 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10001676 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001677
Owen Hofmann09a0c3f2014-11-03 16:57:18 -08001678 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1679 &guest_hv_clock, sizeof(guest_hv_clock))))
1680 return 0;
1681
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001682 /* This VCPU is paused, but it's legal for a guest to read another
1683 * VCPU's kvmclock, so we really have to follow the specification where
1684 * it says that version is odd if data is being modified, and even after
1685 * it is consistent.
1686 *
1687 * Version field updates must be kept separate. This is because
1688 * kvm_write_guest_cached might use a "rep movs" instruction, and
1689 * writes within a string instruction are weakly ordered. So there
1690 * are three writes overall.
1691 *
1692 * As a small optimization, only write the version field in the first
1693 * and third write. The vcpu->pv_time cache is still valid, because the
1694 * version field is the first in the struct.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001695 */
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001696 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
1697
1698 vcpu->hv_clock.version = guest_hv_clock.version + 1;
1699 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1700 &vcpu->hv_clock,
1701 sizeof(vcpu->hv_clock.version));
1702
1703 smp_wmb();
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001704
1705 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
Andy Honig0b794592013-02-20 14:48:10 -08001706 pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001707
1708 if (vcpu->pvclock_set_guest_stopped_request) {
1709 pvclock_flags |= PVCLOCK_GUEST_STOPPED;
1710 vcpu->pvclock_set_guest_stopped_request = false;
1711 }
1712
Marcelo Tosattib7e60c52015-05-28 20:20:41 -03001713 pvclock_flags |= PVCLOCK_COUNTS_FROM_ZERO;
1714
Marcelo Tosattid8281992012-11-27 23:29:01 -02001715 /* If the host uses TSC clocksource, then it is stable */
1716 if (use_master_clock)
1717 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
1718
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001719 vcpu->hv_clock.flags = pvclock_flags;
1720
David Matlackce1a5e62014-11-05 11:46:42 -08001721 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
1722
Andy Honig0b794592013-02-20 14:48:10 -08001723 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1724 &vcpu->hv_clock,
1725 sizeof(vcpu->hv_clock));
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001726
1727 smp_wmb();
1728
1729 vcpu->hv_clock.version++;
1730 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1731 &vcpu->hv_clock,
1732 sizeof(vcpu->hv_clock.version));
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001733 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001734}
1735
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001736/*
1737 * kvmclock updates which are isolated to a given vcpu, such as
1738 * vcpu->cpu migration, should not allow system_timestamp from
1739 * the rest of the vcpus to remain static. Otherwise ntp frequency
1740 * correction applies to one vcpu's system_timestamp but not
1741 * the others.
1742 *
1743 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01001744 * We need to rate-limit these requests though, as they can
1745 * considerably slow guests that have a large number of vcpus.
1746 * The time for a remote vcpu to update its kvmclock is bound
1747 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001748 */
1749
Andrew Jones7e44e442014-02-28 12:52:54 +01001750#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
1751
1752static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001753{
1754 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01001755 struct delayed_work *dwork = to_delayed_work(work);
1756 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1757 kvmclock_update_work);
1758 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001759 struct kvm_vcpu *vcpu;
1760
1761 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001762 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03001763 kvm_vcpu_kick(vcpu);
1764 }
1765}
1766
Andrew Jones7e44e442014-02-28 12:52:54 +01001767static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
1768{
1769 struct kvm *kvm = v->kvm;
1770
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001771 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01001772 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
1773 KVMCLOCK_UPDATE_DELAY);
1774}
1775
Andrew Jones332967a2014-02-28 12:52:55 +01001776#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
1777
1778static void kvmclock_sync_fn(struct work_struct *work)
1779{
1780 struct delayed_work *dwork = to_delayed_work(work);
1781 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1782 kvmclock_sync_work);
1783 struct kvm *kvm = container_of(ka, struct kvm, arch);
1784
Marcelo Tosatti630994b2015-05-12 22:42:04 -03001785 if (!kvmclock_periodic_sync)
1786 return;
1787
Andrew Jones332967a2014-02-28 12:52:55 +01001788 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
1789 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
1790 KVMCLOCK_SYNC_PERIOD);
1791}
1792
Huang Ying890ca9a2009-05-11 16:48:15 +08001793static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1794{
1795 u64 mcg_cap = vcpu->arch.mcg_cap;
1796 unsigned bank_num = mcg_cap & 0xff;
1797
1798 switch (msr) {
1799 case MSR_IA32_MCG_STATUS:
1800 vcpu->arch.mcg_status = data;
1801 break;
1802 case MSR_IA32_MCG_CTL:
1803 if (!(mcg_cap & MCG_CTL_P))
1804 return 1;
1805 if (data != 0 && data != ~(u64)0)
1806 return -1;
1807 vcpu->arch.mcg_ctl = data;
1808 break;
1809 default:
1810 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08001811 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08001812 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001813 /* only 0 or all 1s can be written to IA32_MCi_CTL
1814 * some Linux kernels though clear bit 10 in bank 4 to
1815 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1816 * this to avoid an uncatched #GP in the guest
1817 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001818 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001819 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001820 return -1;
1821 vcpu->arch.mce_banks[offset] = data;
1822 break;
1823 }
1824 return 1;
1825 }
1826 return 0;
1827}
1828
Ed Swierkffde22a2009-10-15 15:21:43 -07001829static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1830{
1831 struct kvm *kvm = vcpu->kvm;
1832 int lm = is_long_mode(vcpu);
1833 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1834 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1835 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1836 : kvm->arch.xen_hvm_config.blob_size_32;
1837 u32 page_num = data & ~PAGE_MASK;
1838 u64 page_addr = data & PAGE_MASK;
1839 u8 *page;
1840 int r;
1841
1842 r = -E2BIG;
1843 if (page_num >= blob_size)
1844 goto out;
1845 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001846 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
1847 if (IS_ERR(page)) {
1848 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07001849 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001850 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001851 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07001852 goto out_free;
1853 r = 0;
1854out_free:
1855 kfree(page);
1856out:
1857 return r;
1858}
1859
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001860static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1861{
1862 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1863}
1864
1865static bool kvm_hv_msr_partition_wide(u32 msr)
1866{
1867 bool r = false;
1868 switch (msr) {
1869 case HV_X64_MSR_GUEST_OS_ID:
1870 case HV_X64_MSR_HYPERCALL:
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001871 case HV_X64_MSR_REFERENCE_TSC:
1872 case HV_X64_MSR_TIME_REF_COUNT:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001873 r = true;
1874 break;
1875 }
1876
1877 return r;
1878}
1879
1880static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1881{
1882 struct kvm *kvm = vcpu->kvm;
1883
1884 switch (msr) {
1885 case HV_X64_MSR_GUEST_OS_ID:
1886 kvm->arch.hv_guest_os_id = data;
1887 /* setting guest os id to zero disables hypercall page */
1888 if (!kvm->arch.hv_guest_os_id)
1889 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1890 break;
1891 case HV_X64_MSR_HYPERCALL: {
1892 u64 gfn;
1893 unsigned long addr;
1894 u8 instructions[4];
1895
1896 /* if guest os id is not set hypercall should remain disabled */
1897 if (!kvm->arch.hv_guest_os_id)
1898 break;
1899 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1900 kvm->arch.hv_hypercall = data;
1901 break;
1902 }
1903 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1904 addr = gfn_to_hva(kvm, gfn);
1905 if (kvm_is_error_hva(addr))
1906 return 1;
1907 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1908 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001909 if (__copy_to_user((void __user *)addr, instructions, 4))
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001910 return 1;
1911 kvm->arch.hv_hypercall = data;
Vadim Rozenfeldb94b64c2014-01-23 18:12:52 +11001912 mark_page_dirty(kvm, gfn);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001913 break;
1914 }
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001915 case HV_X64_MSR_REFERENCE_TSC: {
1916 u64 gfn;
1917 HV_REFERENCE_TSC_PAGE tsc_ref;
1918 memset(&tsc_ref, 0, sizeof(tsc_ref));
1919 kvm->arch.hv_tsc_page = data;
1920 if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1921 break;
1922 gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
Xiaoming Gaoe1fa108d2014-06-19 19:14:57 +08001923 if (kvm_write_guest(kvm, gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
Vadim Rozenfelde9840972014-01-16 20:18:37 +11001924 &tsc_ref, sizeof(tsc_ref)))
1925 return 1;
1926 mark_page_dirty(kvm, gfn);
1927 break;
1928 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001929 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03001930 vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1931 "data 0x%llx\n", msr, data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001932 return 1;
1933 }
1934 return 0;
1935}
1936
1937static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1938{
Gleb Natapov10388a02010-01-17 15:51:23 +02001939 switch (msr) {
1940 case HV_X64_MSR_APIC_ASSIST_PAGE: {
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001941 u64 gfn;
Gleb Natapov10388a02010-01-17 15:51:23 +02001942 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001943
Gleb Natapov10388a02010-01-17 15:51:23 +02001944 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1945 vcpu->arch.hv_vapic = data;
Michael S. Tsirkinb63cf422014-05-07 16:29:48 +03001946 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1947 return 1;
Gleb Natapov10388a02010-01-17 15:51:23 +02001948 break;
1949 }
Vadim Rozenfeldb3af1e82014-01-23 18:12:53 +11001950 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001951 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
Gleb Natapov10388a02010-01-17 15:51:23 +02001952 if (kvm_is_error_hva(addr))
1953 return 1;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08001954 if (__clear_user((void __user *)addr, PAGE_SIZE))
Gleb Natapov10388a02010-01-17 15:51:23 +02001955 return 1;
1956 vcpu->arch.hv_vapic = data;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001957 kvm_vcpu_mark_page_dirty(vcpu, gfn);
Michael S. Tsirkinb63cf422014-05-07 16:29:48 +03001958 if (kvm_lapic_enable_pv_eoi(vcpu, gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1959 return 1;
Gleb Natapov10388a02010-01-17 15:51:23 +02001960 break;
1961 }
1962 case HV_X64_MSR_EOI:
1963 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1964 case HV_X64_MSR_ICR:
1965 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1966 case HV_X64_MSR_TPR:
1967 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1968 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03001969 vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1970 "data 0x%llx\n", msr, data);
Gleb Natapov10388a02010-01-17 15:51:23 +02001971 return 1;
1972 }
1973
1974 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001975}
1976
Gleb Natapov344d9582010-10-14 11:22:50 +02001977static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
1978{
1979 gpa_t gpa = data & ~0x3f;
1980
Guo Chao4a969982012-06-28 15:17:27 +08001981 /* Bits 2:5 are reserved, Should be zero */
Gleb Natapov6adba522010-10-14 11:22:55 +02001982 if (data & 0x3c)
Gleb Natapov344d9582010-10-14 11:22:50 +02001983 return 1;
1984
1985 vcpu->arch.apf.msr_val = data;
1986
1987 if (!(data & KVM_ASYNC_PF_ENABLED)) {
1988 kvm_clear_async_pf_completion_queue(vcpu);
1989 kvm_async_pf_hash_reset(vcpu);
1990 return 0;
1991 }
1992
Andrew Honig8f964522013-03-29 09:35:21 -07001993 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
1994 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02001995 return 1;
1996
Gleb Natapov6adba522010-10-14 11:22:55 +02001997 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Gleb Natapov344d9582010-10-14 11:22:50 +02001998 kvm_async_pf_wakeup_all(vcpu);
1999 return 0;
2000}
2001
Glauber Costa12f9a482011-02-01 14:16:40 -05002002static void kvmclock_reset(struct kvm_vcpu *vcpu)
2003{
Andy Honig0b794592013-02-20 14:48:10 -08002004 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05002005}
2006
Glauber Costac9aaa892011-07-11 15:28:14 -04002007static void accumulate_steal_time(struct kvm_vcpu *vcpu)
2008{
2009 u64 delta;
2010
2011 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2012 return;
2013
2014 delta = current->sched_info.run_delay - vcpu->arch.st.last_steal;
2015 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2016 vcpu->arch.st.accum_steal = delta;
2017}
2018
2019static void record_steal_time(struct kvm_vcpu *vcpu)
2020{
2021 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2022 return;
2023
2024 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2025 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2026 return;
2027
2028 vcpu->arch.st.steal.steal += vcpu->arch.st.accum_steal;
2029 vcpu->arch.st.steal.version += 2;
2030 vcpu->arch.st.accum_steal = 0;
2031
2032 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2033 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2034}
2035
Will Auld8fe8ab42012-11-29 12:42:12 -08002036int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002037{
Gleb Natapov57537852012-01-15 14:17:22 +02002038 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002039 u32 msr = msr_info->index;
2040 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002041
Carsten Otte15c4a642007-10-30 18:44:17 +01002042 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002043 case MSR_AMD64_NB_CFG:
2044 case MSR_IA32_UCODE_REV:
2045 case MSR_IA32_UCODE_WRITE:
2046 case MSR_VM_HSAVE_PA:
2047 case MSR_AMD64_PATCH_LOADER:
2048 case MSR_AMD64_BU_CFG2:
2049 break;
2050
Carsten Otte15c4a642007-10-30 18:44:17 +01002051 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02002052 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002053 case MSR_K7_HWCR:
2054 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002055 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002056 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02002057 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02002058 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002059 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2060 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002061 return 1;
2062 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002063 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002064 case MSR_FAM10H_MMIO_CONF_BASE:
2065 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002066 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2067 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002068 return 1;
2069 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002070 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002071 case MSR_IA32_DEBUGCTLMSR:
2072 if (!data) {
2073 /* We support the non-activated case already */
2074 break;
2075 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2076 /* Values other than LBR and BTF are vendor-specific,
2077 thus reserved and should throw a #GP */
2078 return 1;
2079 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002080 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2081 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002082 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002083 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002084 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002085 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002086 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002087 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2088 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002089 case MSR_IA32_TSCDEADLINE:
2090 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2091 break;
Will Auldba904632012-11-29 12:42:50 -08002092 case MSR_IA32_TSC_ADJUST:
2093 if (guest_cpuid_has_tsc_adjust(vcpu)) {
2094 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002095 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002096 kvm_x86_ops->adjust_tsc_offset(vcpu, adj, true);
2097 }
2098 vcpu->arch.ia32_tsc_adjust_msr = data;
2099 }
2100 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002101 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002102 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01002103 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002104 case MSR_IA32_SMBASE:
2105 if (!msr_info->host_initiated)
2106 return 1;
2107 vcpu->arch.smbase = data;
2108 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002109 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002110 case MSR_KVM_WALL_CLOCK:
2111 vcpu->kvm->arch.wall_clock = data;
2112 kvm_write_wall_clock(vcpu->kvm, data);
2113 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002114 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002115 case MSR_KVM_SYSTEM_TIME: {
Andy Honig0b794592013-02-20 14:48:10 -08002116 u64 gpa_offset;
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002117 struct kvm_arch *ka = &vcpu->kvm->arch;
2118
Glauber Costa12f9a482011-02-01 14:16:40 -05002119 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002120
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002121 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2122 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2123
2124 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
2125 set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
2126 &vcpu->requests);
2127
2128 ka->boot_vcpu_runs_old_kvmclock = tmp;
Marcelo Tosattib7e60c52015-05-28 20:20:41 -03002129
2130 ka->kvmclock_offset = -get_kernel_ns();
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002131 }
2132
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002133 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002134 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002135
2136 /* we verify if the enable bit is set... */
2137 if (!(data & 1))
2138 break;
2139
Andy Honig0b794592013-02-20 14:48:10 -08002140 gpa_offset = data & ~(PAGE_MASK | 1);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002141
Andy Honig0b794592013-02-20 14:48:10 -08002142 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002143 &vcpu->arch.pv_time, data & ~1ULL,
2144 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002145 vcpu->arch.pv_time_enabled = false;
2146 else
2147 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002148
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002149 break;
2150 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002151 case MSR_KVM_ASYNC_PF_EN:
2152 if (kvm_pv_enable_async_pf(vcpu, data))
2153 return 1;
2154 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002155 case MSR_KVM_STEAL_TIME:
2156
2157 if (unlikely(!sched_info_on()))
2158 return 1;
2159
2160 if (data & KVM_STEAL_RESERVED_MASK)
2161 return 1;
2162
2163 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002164 data & KVM_STEAL_VALID_BITS,
2165 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002166 return 1;
2167
2168 vcpu->arch.st.msr_val = data;
2169
2170 if (!(data & KVM_MSR_ENABLED))
2171 break;
2172
2173 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2174
2175 preempt_disable();
2176 accumulate_steal_time(vcpu);
2177 preempt_enable();
2178
2179 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2180
2181 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002182 case MSR_KVM_PV_EOI_EN:
2183 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2184 return 1;
2185 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002186
Huang Ying890ca9a2009-05-11 16:48:15 +08002187 case MSR_IA32_MCG_CTL:
2188 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002189 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Huang Ying890ca9a2009-05-11 16:48:15 +08002190 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002191
Wei Huang6912ac32015-06-12 01:34:56 -04002192 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2193 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2194 pr = true; /* fall through */
2195 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2196 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002197 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002198 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002199
2200 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002201 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2202 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002203 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002204 case MSR_K7_CLK_CTL:
2205 /*
2206 * Ignore all writes to this no longer documented MSR.
2207 * Writes are only relevant for old K7 processors,
2208 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002209 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002210 * affected processor models on the command line, hence
2211 * the need to ignore the workaround.
2212 */
2213 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002214 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
2215 if (kvm_hv_msr_partition_wide(msr)) {
2216 int r;
2217 mutex_lock(&vcpu->kvm->lock);
2218 r = set_msr_hyperv_pw(vcpu, msr, data);
2219 mutex_unlock(&vcpu->kvm->lock);
2220 return r;
2221 } else
2222 return set_msr_hyperv(vcpu, msr, data);
2223 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002224 case MSR_IA32_BBL_CR_CTL3:
2225 /* Drop writes to this legacy MSR -- see rdmsr
2226 * counterpart for further detail.
2227 */
Christoffer Dalla737f252012-06-03 21:17:48 +03002228 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002229 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002230 case MSR_AMD64_OSVW_ID_LENGTH:
2231 if (!guest_cpuid_has_osvw(vcpu))
2232 return 1;
2233 vcpu->arch.osvw.length = data;
2234 break;
2235 case MSR_AMD64_OSVW_STATUS:
2236 if (!guest_cpuid_has_osvw(vcpu))
2237 return 1;
2238 vcpu->arch.osvw.status = data;
2239 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002240 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002241 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2242 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002243 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002244 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002245 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002246 vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
2247 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002248 return 1;
2249 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002250 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
2251 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002252 break;
2253 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002254 }
2255 return 0;
2256}
2257EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2258
2259
2260/*
2261 * Reads an msr value (of 'msr_index') into 'pdata'.
2262 * Returns 0 on success, non-0 otherwise.
2263 * Assumes vcpu_load() was already called.
2264 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002265int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01002266{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002267 return kvm_x86_ops->get_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002268}
Wincy Vanff651cb2014-12-11 08:52:58 +03002269EXPORT_SYMBOL_GPL(kvm_get_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002270
Huang Ying890ca9a2009-05-11 16:48:15 +08002271static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2272{
2273 u64 data;
2274 u64 mcg_cap = vcpu->arch.mcg_cap;
2275 unsigned bank_num = mcg_cap & 0xff;
2276
2277 switch (msr) {
2278 case MSR_IA32_P5_MC_ADDR:
2279 case MSR_IA32_P5_MC_TYPE:
2280 data = 0;
2281 break;
2282 case MSR_IA32_MCG_CAP:
2283 data = vcpu->arch.mcg_cap;
2284 break;
2285 case MSR_IA32_MCG_CTL:
2286 if (!(mcg_cap & MCG_CTL_P))
2287 return 1;
2288 data = vcpu->arch.mcg_ctl;
2289 break;
2290 case MSR_IA32_MCG_STATUS:
2291 data = vcpu->arch.mcg_status;
2292 break;
2293 default:
2294 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002295 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002296 u32 offset = msr - MSR_IA32_MC0_CTL;
2297 data = vcpu->arch.mce_banks[offset];
2298 break;
2299 }
2300 return 1;
2301 }
2302 *pdata = data;
2303 return 0;
2304}
2305
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002306static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2307{
2308 u64 data = 0;
2309 struct kvm *kvm = vcpu->kvm;
2310
2311 switch (msr) {
2312 case HV_X64_MSR_GUEST_OS_ID:
2313 data = kvm->arch.hv_guest_os_id;
2314 break;
2315 case HV_X64_MSR_HYPERCALL:
2316 data = kvm->arch.hv_hypercall;
2317 break;
Vadim Rozenfelde9840972014-01-16 20:18:37 +11002318 case HV_X64_MSR_TIME_REF_COUNT: {
2319 data =
2320 div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
2321 break;
2322 }
2323 case HV_X64_MSR_REFERENCE_TSC:
2324 data = kvm->arch.hv_tsc_page;
2325 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002326 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03002327 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002328 return 1;
2329 }
2330
2331 *pdata = data;
2332 return 0;
2333}
2334
2335static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2336{
2337 u64 data = 0;
2338
2339 switch (msr) {
2340 case HV_X64_MSR_VP_INDEX: {
2341 int r;
2342 struct kvm_vcpu *v;
Takuya Yoshikawa684851a2014-02-27 15:08:31 +09002343 kvm_for_each_vcpu(r, v, vcpu->kvm) {
2344 if (v == vcpu) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002345 data = r;
Takuya Yoshikawa684851a2014-02-27 15:08:31 +09002346 break;
2347 }
2348 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002349 break;
2350 }
Gleb Natapov10388a02010-01-17 15:51:23 +02002351 case HV_X64_MSR_EOI:
2352 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
2353 case HV_X64_MSR_ICR:
2354 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
2355 case HV_X64_MSR_TPR:
2356 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Mike Waychison14fa67e2011-07-21 15:38:10 -07002357 case HV_X64_MSR_APIC_ASSIST_PAGE:
Mike Waychisond1613ad2011-07-23 00:31:45 -07002358 data = vcpu->arch.hv_vapic;
2359 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002360 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03002361 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002362 return 1;
2363 }
2364 *pdata = data;
2365 return 0;
2366}
2367
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002368int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002369{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002370 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002371 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002372 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002373 case MSR_IA32_DEBUGCTLMSR:
2374 case MSR_IA32_LASTBRANCHFROMIP:
2375 case MSR_IA32_LASTBRANCHTOIP:
2376 case MSR_IA32_LASTINTFROMIP:
2377 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302378 case MSR_K8_SYSCFG:
2379 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002380 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002381 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002382 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002383 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002384 case MSR_AMD64_BU_CFG2:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002385 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002386 break;
Wei Huang6912ac32015-06-12 01:34:56 -04002387 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2388 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2389 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2390 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002391 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002392 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2393 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002394 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002395 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002396 msr_info->data = 0x100000000ULL;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002397 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002398 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002399 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002400 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002401 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002402 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002403 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002404 /*
2405 * MSR_EBC_FREQUENCY_ID
2406 * Conservative value valid for even the basic CPU models.
2407 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2408 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2409 * and 266MHz for model 3, or 4. Set Core Clock
2410 * Frequency to System Bus Frequency Ratio to 1 (bits
2411 * 31:24) even though these are only valid for CPU
2412 * models > 2, however guests may end up dividing or
2413 * multiplying by zero otherwise.
2414 */
2415 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002416 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002417 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002418 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002419 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002420 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002421 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002422 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002423 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002424 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002425 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002426 break;
Will Auldba904632012-11-29 12:42:50 -08002427 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002428 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002429 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002430 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002431 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002432 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002433 case MSR_IA32_SMBASE:
2434 if (!msr_info->host_initiated)
2435 return 1;
2436 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002437 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002438 case MSR_IA32_PERF_STATUS:
2439 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002440 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002441 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002442 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002443 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002444 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002445 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002446 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002447 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002448 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002449 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002450 break;
2451 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002452 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002453 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002454 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002455 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002456 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02002457 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002458 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002459 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04002460 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002461 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002462 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002463 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002464 case MSR_IA32_P5_MC_ADDR:
2465 case MSR_IA32_P5_MC_TYPE:
2466 case MSR_IA32_MCG_CAP:
2467 case MSR_IA32_MCG_CTL:
2468 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002469 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002470 return get_msr_mce(vcpu, msr_info->index, &msr_info->data);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002471 case MSR_K7_CLK_CTL:
2472 /*
2473 * Provide expected ramp-up count for K7. All other
2474 * are set to zero, indicating minimum divisors for
2475 * every field.
2476 *
2477 * This prevents guest kernels on AMD host with CPU
2478 * type 6, model 8 and higher from exploding due to
2479 * the rdmsr failing.
2480 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002481 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002482 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002483 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002484 if (kvm_hv_msr_partition_wide(msr_info->index)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002485 int r;
2486 mutex_lock(&vcpu->kvm->lock);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002487 r = get_msr_hyperv_pw(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002488 mutex_unlock(&vcpu->kvm->lock);
2489 return r;
2490 } else
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002491 return get_msr_hyperv(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002492 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002493 case MSR_IA32_BBL_CR_CTL3:
2494 /* This legacy MSR exists but isn't fully documented in current
2495 * silicon. It is however accessed by winxp in very narrow
2496 * scenarios where it sets bit #19, itself documented as
2497 * a "reserved" bit. Best effort attempt to source coherent
2498 * read data here should the balance of the register be
2499 * interpreted by the guest:
2500 *
2501 * L2 cache control register 3: 64GB range, 256KB size,
2502 * enabled, latency 0x1, configured
2503 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002504 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05002505 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002506 case MSR_AMD64_OSVW_ID_LENGTH:
2507 if (!guest_cpuid_has_osvw(vcpu))
2508 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002509 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002510 break;
2511 case MSR_AMD64_OSVW_STATUS:
2512 if (!guest_cpuid_has_osvw(vcpu))
2513 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002514 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002515 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002516 default:
Wei Huangc6702c92015-06-19 13:44:45 +02002517 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002518 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002519 if (!ignore_msrs) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002520 vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02002521 return 1;
2522 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002523 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr_info->index);
2524 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02002525 }
2526 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002527 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002528 return 0;
2529}
2530EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2531
Carsten Otte313a3dc2007-10-11 19:16:52 +02002532/*
2533 * Read or write a bunch of msrs. All parameters are kernel addresses.
2534 *
2535 * @return number of msrs set successfully.
2536 */
2537static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2538 struct kvm_msr_entry *entries,
2539 int (*do_msr)(struct kvm_vcpu *vcpu,
2540 unsigned index, u64 *data))
2541{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002542 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002543
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002544 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002545 for (i = 0; i < msrs->nmsrs; ++i)
2546 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2547 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002548 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002549
Carsten Otte313a3dc2007-10-11 19:16:52 +02002550 return i;
2551}
2552
2553/*
2554 * Read or write a bunch of msrs. Parameters are user addresses.
2555 *
2556 * @return number of msrs set successfully.
2557 */
2558static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2559 int (*do_msr)(struct kvm_vcpu *vcpu,
2560 unsigned index, u64 *data),
2561 int writeback)
2562{
2563 struct kvm_msrs msrs;
2564 struct kvm_msr_entry *entries;
2565 int r, n;
2566 unsigned size;
2567
2568 r = -EFAULT;
2569 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2570 goto out;
2571
2572 r = -E2BIG;
2573 if (msrs.nmsrs >= MAX_IO_MSRS)
2574 goto out;
2575
Carsten Otte313a3dc2007-10-11 19:16:52 +02002576 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002577 entries = memdup_user(user_msrs->entries, size);
2578 if (IS_ERR(entries)) {
2579 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002580 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002581 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002582
2583 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2584 if (r < 0)
2585 goto out_free;
2586
2587 r = -EFAULT;
2588 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2589 goto out_free;
2590
2591 r = n;
2592
2593out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002594 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002595out:
2596 return r;
2597}
2598
Alexander Graf784aa3d2014-07-14 18:27:35 +02002599int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002600{
2601 int r;
2602
2603 switch (ext) {
2604 case KVM_CAP_IRQCHIP:
2605 case KVM_CAP_HLT:
2606 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002607 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002608 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002609 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002610 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002611 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002612 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002613 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002614 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002615 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002616 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002617 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002618 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002619 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002620 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002621 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002622 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002623 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002624 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002625 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002626 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002627 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002628 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002629 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002630 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002631 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002632 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002633 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002634 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002635 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002636 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002637 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002638 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01002639 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03002640 case KVM_CAP_ENABLE_CAP_VM:
2641 case KVM_CAP_DISABLE_QUIRKS:
Alex Williamson2a5bab12013-04-16 13:49:18 -06002642#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2643 case KVM_CAP_ASSIGN_DEV_IRQ:
2644 case KVM_CAP_PCI_2_3:
2645#endif
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002646 r = 1;
2647 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02002648 case KVM_CAP_X86_SMM:
2649 /* SMBASE is usually relocated above 1M on modern chipsets,
2650 * and SMM handlers might indeed rely on 4G segment limits,
2651 * so do not report SMM to be available if real mode is
2652 * emulated via vm86 mode. Still, do not go to great lengths
2653 * to avoid userspace's usage of the feature, because it is a
2654 * fringe case that is not enabled except via specific settings
2655 * of the module parameters.
2656 */
2657 r = kvm_x86_ops->cpu_has_high_real_mode_segbase();
2658 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02002659 case KVM_CAP_COALESCED_MMIO:
2660 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
2661 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002662 case KVM_CAP_VAPIC:
2663 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2664 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002665 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002666 r = KVM_SOFT_MAX_VCPUS;
2667 break;
2668 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002669 r = KVM_MAX_VCPUS;
2670 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002671 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002672 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002673 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002674 case KVM_CAP_PV_MMU: /* obsolete */
2675 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002676 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002677#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002678 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +02002679 r = iommu_present(&pci_bus_type);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002680 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002681#endif
Huang Ying890ca9a2009-05-11 16:48:15 +08002682 case KVM_CAP_MCE:
2683 r = KVM_MAX_MCE_BANKS;
2684 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002685 case KVM_CAP_XCRS:
2686 r = cpu_has_xsave;
2687 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002688 case KVM_CAP_TSC_CONTROL:
2689 r = kvm_has_tsc_control;
2690 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002691 default:
2692 r = 0;
2693 break;
2694 }
2695 return r;
2696
2697}
2698
Carsten Otte043405e2007-10-10 17:16:19 +02002699long kvm_arch_dev_ioctl(struct file *filp,
2700 unsigned int ioctl, unsigned long arg)
2701{
2702 void __user *argp = (void __user *)arg;
2703 long r;
2704
2705 switch (ioctl) {
2706 case KVM_GET_MSR_INDEX_LIST: {
2707 struct kvm_msr_list __user *user_msr_list = argp;
2708 struct kvm_msr_list msr_list;
2709 unsigned n;
2710
2711 r = -EFAULT;
2712 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2713 goto out;
2714 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002715 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Carsten Otte043405e2007-10-10 17:16:19 +02002716 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2717 goto out;
2718 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002719 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02002720 goto out;
2721 r = -EFAULT;
2722 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2723 num_msrs_to_save * sizeof(u32)))
2724 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002725 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02002726 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002727 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02002728 goto out;
2729 r = 0;
2730 break;
2731 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002732 case KVM_GET_SUPPORTED_CPUID:
2733 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02002734 struct kvm_cpuid2 __user *cpuid_arg = argp;
2735 struct kvm_cpuid2 cpuid;
2736
2737 r = -EFAULT;
2738 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2739 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002740
2741 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2742 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02002743 if (r)
2744 goto out;
2745
2746 r = -EFAULT;
2747 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2748 goto out;
2749 r = 0;
2750 break;
2751 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002752 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
2753 u64 mce_cap;
2754
2755 mce_cap = KVM_MCE_CAP_SUPPORTED;
2756 r = -EFAULT;
2757 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
2758 goto out;
2759 r = 0;
2760 break;
2761 }
Carsten Otte043405e2007-10-10 17:16:19 +02002762 default:
2763 r = -EINVAL;
2764 }
2765out:
2766 return r;
2767}
2768
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002769static void wbinvd_ipi(void *garbage)
2770{
2771 wbinvd();
2772}
2773
2774static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
2775{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06002776 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002777}
2778
Carsten Otte313a3dc2007-10-11 19:16:52 +02002779void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2780{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002781 /* Address WBINVD may be executed by guest */
2782 if (need_emulate_wbinvd(vcpu)) {
2783 if (kvm_x86_ops->has_wbinvd_exit())
2784 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
2785 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
2786 smp_call_function_single(vcpu->cpu,
2787 wbinvd_ipi, NULL, 1);
2788 }
2789
Carsten Otte313a3dc2007-10-11 19:16:52 +02002790 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002791
2792 /* Apply any externally detected TSC adjustments (due to suspend) */
2793 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
2794 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
2795 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002796 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002797 }
2798
Zachary Amsden48434c202010-08-19 22:07:24 -10002799 if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002800 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002801 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10002802 if (tsc_delta < 0)
2803 mark_tsc_unstable("KVM discovered backwards TSC");
Zachary Amsdenc2855452010-09-18 14:38:15 -10002804 if (check_tsc_unstable()) {
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002805 u64 offset = kvm_x86_ops->compute_tsc_offset(vcpu,
2806 vcpu->arch.last_guest_tsc);
2807 kvm_x86_ops->write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002808 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002809 }
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02002810 /*
2811 * On a host with synchronized TSC, there is no need to update
2812 * kvmclock on vcpu->cpu migration
2813 */
2814 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002815 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002816 if (vcpu->cpu != cpu)
2817 kvm_migrate_timers(vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002818 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10002819 }
Glauber Costac9aaa892011-07-11 15:28:14 -04002820
2821 accumulate_steal_time(vcpu);
2822 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002823}
2824
2825void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2826{
Avi Kivity02daab22009-12-30 12:40:26 +02002827 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03002828 kvm_put_guest_fpu(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002829 vcpu->arch.last_host_tsc = rdtsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02002830}
2831
Carsten Otte313a3dc2007-10-11 19:16:52 +02002832static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2833 struct kvm_lapic_state *s)
2834{
Yang Zhang5a717852013-04-11 19:25:16 +08002835 kvm_x86_ops->sync_pir_to_irr(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002836 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002837
2838 return 0;
2839}
2840
2841static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2842 struct kvm_lapic_state *s)
2843{
Gleb Natapov64eb0622012-08-08 15:24:36 +03002844 kvm_apic_post_state_restore(vcpu, s);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002845 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002846
2847 return 0;
2848}
2849
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002850static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2851 struct kvm_interrupt *irq)
2852{
Chen Gang02cdb502013-02-27 11:33:25 +08002853 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002854 return -EINVAL;
2855 if (irqchip_in_kernel(vcpu->kvm))
2856 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002857
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002858 kvm_queue_interrupt(vcpu, irq->irq, false);
Avi Kivity3842d132010-07-27 12:30:24 +03002859 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002860
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002861 return 0;
2862}
2863
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002864static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2865{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002866 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002867
2868 return 0;
2869}
2870
Paolo Bonzinif0778252015-04-01 15:06:40 +02002871static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
2872{
Paolo Bonzini64d60672015-05-07 11:36:11 +02002873 kvm_make_request(KVM_REQ_SMI, vcpu);
2874
Paolo Bonzinif0778252015-04-01 15:06:40 +02002875 return 0;
2876}
2877
Avi Kivityb209749f2007-10-22 16:50:39 +02002878static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2879 struct kvm_tpr_access_ctl *tac)
2880{
2881 if (tac->flags)
2882 return -EINVAL;
2883 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2884 return 0;
2885}
2886
Huang Ying890ca9a2009-05-11 16:48:15 +08002887static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2888 u64 mcg_cap)
2889{
2890 int r;
2891 unsigned bank_num = mcg_cap & 0xff, bank;
2892
2893 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002894 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002895 goto out;
2896 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2897 goto out;
2898 r = 0;
2899 vcpu->arch.mcg_cap = mcg_cap;
2900 /* Init IA32_MCG_CTL to all 1s */
2901 if (mcg_cap & MCG_CTL_P)
2902 vcpu->arch.mcg_ctl = ~(u64)0;
2903 /* Init IA32_MCi_CTL to all 1s */
2904 for (bank = 0; bank < bank_num; bank++)
2905 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2906out:
2907 return r;
2908}
2909
2910static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2911 struct kvm_x86_mce *mce)
2912{
2913 u64 mcg_cap = vcpu->arch.mcg_cap;
2914 unsigned bank_num = mcg_cap & 0xff;
2915 u64 *banks = vcpu->arch.mce_banks;
2916
2917 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2918 return -EINVAL;
2919 /*
2920 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2921 * reporting is disabled
2922 */
2923 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2924 vcpu->arch.mcg_ctl != ~(u64)0)
2925 return 0;
2926 banks += 4 * mce->bank;
2927 /*
2928 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2929 * reporting is disabled for the bank
2930 */
2931 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2932 return 0;
2933 if (mce->status & MCI_STATUS_UC) {
2934 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002935 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002936 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002937 return 0;
2938 }
2939 if (banks[1] & MCI_STATUS_VAL)
2940 mce->status |= MCI_STATUS_OVER;
2941 banks[2] = mce->addr;
2942 banks[3] = mce->misc;
2943 vcpu->arch.mcg_status = mce->mcg_status;
2944 banks[1] = mce->status;
2945 kvm_queue_exception(vcpu, MC_VECTOR);
2946 } else if (!(banks[1] & MCI_STATUS_VAL)
2947 || !(banks[1] & MCI_STATUS_UC)) {
2948 if (banks[1] & MCI_STATUS_VAL)
2949 mce->status |= MCI_STATUS_OVER;
2950 banks[2] = mce->addr;
2951 banks[3] = mce->misc;
2952 banks[1] = mce->status;
2953 } else
2954 banks[1] |= MCI_STATUS_OVER;
2955 return 0;
2956}
2957
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002958static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2959 struct kvm_vcpu_events *events)
2960{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002961 process_nmi(vcpu);
Jan Kiszka03b82a32010-02-15 10:45:41 +01002962 events->exception.injected =
2963 vcpu->arch.exception.pending &&
2964 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002965 events->exception.nr = vcpu->arch.exception.nr;
2966 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002967 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002968 events->exception.error_code = vcpu->arch.exception.error_code;
2969
Jan Kiszka03b82a32010-02-15 10:45:41 +01002970 events->interrupt.injected =
2971 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002972 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002973 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002974 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002975
2976 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002977 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002978 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002979 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002980
Jan Kiszka66450a22013-03-13 12:42:34 +01002981 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002982
Paolo Bonzinif0778252015-04-01 15:06:40 +02002983 events->smi.smm = is_smm(vcpu);
2984 events->smi.pending = vcpu->arch.smi_pending;
2985 events->smi.smm_inside_nmi =
2986 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
2987 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
2988
Jan Kiszkadab4b912009-12-06 18:24:15 +01002989 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02002990 | KVM_VCPUEVENT_VALID_SHADOW
2991 | KVM_VCPUEVENT_VALID_SMM);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002992 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002993}
2994
2995static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2996 struct kvm_vcpu_events *events)
2997{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002998 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002999 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003000 | KVM_VCPUEVENT_VALID_SHADOW
3001 | KVM_VCPUEVENT_VALID_SMM))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003002 return -EINVAL;
3003
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003004 process_nmi(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003005 vcpu->arch.exception.pending = events->exception.injected;
3006 vcpu->arch.exception.nr = events->exception.nr;
3007 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3008 vcpu->arch.exception.error_code = events->exception.error_code;
3009
3010 vcpu->arch.interrupt.pending = events->interrupt.injected;
3011 vcpu->arch.interrupt.nr = events->interrupt.nr;
3012 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003013 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3014 kvm_x86_ops->set_interrupt_shadow(vcpu,
3015 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003016
3017 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003018 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3019 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003020 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3021
Jan Kiszka66450a22013-03-13 12:42:34 +01003022 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
3023 kvm_vcpu_has_lapic(vcpu))
3024 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003025
Paolo Bonzinif0778252015-04-01 15:06:40 +02003026 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
3027 if (events->smi.smm)
3028 vcpu->arch.hflags |= HF_SMM_MASK;
3029 else
3030 vcpu->arch.hflags &= ~HF_SMM_MASK;
3031 vcpu->arch.smi_pending = events->smi.pending;
3032 if (events->smi.smm_inside_nmi)
3033 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
3034 else
3035 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
3036 if (kvm_vcpu_has_lapic(vcpu)) {
3037 if (events->smi.latched_init)
3038 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3039 else
3040 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3041 }
3042 }
3043
Avi Kivity3842d132010-07-27 12:30:24 +03003044 kvm_make_request(KVM_REQ_EVENT, vcpu);
3045
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003046 return 0;
3047}
3048
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003049static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3050 struct kvm_debugregs *dbgregs)
3051{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003052 unsigned long val;
3053
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003054 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003055 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003056 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003057 dbgregs->dr7 = vcpu->arch.dr7;
3058 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003059 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003060}
3061
3062static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3063 struct kvm_debugregs *dbgregs)
3064{
3065 if (dbgregs->flags)
3066 return -EINVAL;
3067
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003068 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003069 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003070 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003071 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003072 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003073 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003074
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003075 return 0;
3076}
3077
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003078#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3079
3080static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3081{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003082 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003083 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003084 u64 valid;
3085
3086 /*
3087 * Copy legacy XSAVE area, to avoid complications with CPUID
3088 * leaves 0 and 1 in the loop below.
3089 */
3090 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3091
3092 /* Set XSTATE_BV */
3093 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3094
3095 /*
3096 * Copy each region from the possibly compacted offset to the
3097 * non-compacted offset.
3098 */
3099 valid = xstate_bv & ~XSTATE_FPSSE;
3100 while (valid) {
3101 u64 feature = valid & -valid;
3102 int index = fls64(feature) - 1;
3103 void *src = get_xsave_addr(xsave, feature);
3104
3105 if (src) {
3106 u32 size, offset, ecx, edx;
3107 cpuid_count(XSTATE_CPUID, index,
3108 &size, &offset, &ecx, &edx);
3109 memcpy(dest + offset, src, size);
3110 }
3111
3112 valid -= feature;
3113 }
3114}
3115
3116static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3117{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003118 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003119 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3120 u64 valid;
3121
3122 /*
3123 * Copy legacy XSAVE area, to avoid complications with CPUID
3124 * leaves 0 and 1 in the loop below.
3125 */
3126 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3127
3128 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003129 xsave->header.xfeatures = xstate_bv;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003130 if (cpu_has_xsaves)
Ingo Molnar3a544502015-04-24 10:14:36 +02003131 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003132
3133 /*
3134 * Copy each region from the non-compacted offset to the
3135 * possibly compacted offset.
3136 */
3137 valid = xstate_bv & ~XSTATE_FPSSE;
3138 while (valid) {
3139 u64 feature = valid & -valid;
3140 int index = fls64(feature) - 1;
3141 void *dest = get_xsave_addr(xsave, feature);
3142
3143 if (dest) {
3144 u32 size, offset, ecx, edx;
3145 cpuid_count(XSTATE_CPUID, index,
3146 &size, &offset, &ecx, &edx);
3147 memcpy(dest, src + offset, size);
3148 } else
3149 WARN_ON_ONCE(1);
3150
3151 valid -= feature;
3152 }
3153}
3154
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003155static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3156 struct kvm_xsave *guest_xsave)
3157{
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003158 if (cpu_has_xsave) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003159 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3160 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003161 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003162 memcpy(guest_xsave->region,
Ingo Molnar7366ed72015-04-27 04:19:39 +02003163 &vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003164 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003165 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
3166 XSTATE_FPSSE;
3167 }
3168}
3169
3170static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3171 struct kvm_xsave *guest_xsave)
3172{
3173 u64 xstate_bv =
3174 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
3175
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003176 if (cpu_has_xsave) {
3177 /*
3178 * Here we allow setting states that are not present in
3179 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3180 * with old userspace.
3181 */
Paolo Bonzini4ff41732014-02-24 12:15:16 +01003182 if (xstate_bv & ~kvm_supported_xcr0())
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003183 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003184 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003185 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003186 if (xstate_bv & ~XSTATE_FPSSE)
3187 return -EINVAL;
Ingo Molnar7366ed72015-04-27 04:19:39 +02003188 memcpy(&vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003189 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003190 }
3191 return 0;
3192}
3193
3194static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3195 struct kvm_xcrs *guest_xcrs)
3196{
3197 if (!cpu_has_xsave) {
3198 guest_xcrs->nr_xcrs = 0;
3199 return;
3200 }
3201
3202 guest_xcrs->nr_xcrs = 1;
3203 guest_xcrs->flags = 0;
3204 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3205 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3206}
3207
3208static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3209 struct kvm_xcrs *guest_xcrs)
3210{
3211 int i, r = 0;
3212
3213 if (!cpu_has_xsave)
3214 return -EINVAL;
3215
3216 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3217 return -EINVAL;
3218
3219 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3220 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003221 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003222 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003223 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003224 break;
3225 }
3226 if (r)
3227 r = -EINVAL;
3228 return r;
3229}
3230
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003231/*
3232 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3233 * stopped by the hypervisor. This function will be called from the host only.
3234 * EINVAL is returned when the host attempts to set the flag for a guest that
3235 * does not support pv clocks.
3236 */
3237static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3238{
Andy Honig0b794592013-02-20 14:48:10 -08003239 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003240 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003241 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003242 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3243 return 0;
3244}
3245
Carsten Otte313a3dc2007-10-11 19:16:52 +02003246long kvm_arch_vcpu_ioctl(struct file *filp,
3247 unsigned int ioctl, unsigned long arg)
3248{
3249 struct kvm_vcpu *vcpu = filp->private_data;
3250 void __user *argp = (void __user *)arg;
3251 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003252 union {
3253 struct kvm_lapic_state *lapic;
3254 struct kvm_xsave *xsave;
3255 struct kvm_xcrs *xcrs;
3256 void *buffer;
3257 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003258
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003259 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003260 switch (ioctl) {
3261 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003262 r = -EINVAL;
3263 if (!vcpu->arch.apic)
3264 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003265 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003266
Dave Hansenb772ff32008-08-11 10:01:47 -07003267 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003268 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003269 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003270 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003271 if (r)
3272 goto out;
3273 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003274 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003275 goto out;
3276 r = 0;
3277 break;
3278 }
3279 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003280 r = -EINVAL;
3281 if (!vcpu->arch.apic)
3282 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003283 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Guo Chao18595412012-11-02 18:33:21 +08003284 if (IS_ERR(u.lapic))
3285 return PTR_ERR(u.lapic);
Sasha Levinff5c2c02011-12-04 19:36:29 +02003286
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003287 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003288 break;
3289 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003290 case KVM_INTERRUPT: {
3291 struct kvm_interrupt irq;
3292
3293 r = -EFAULT;
3294 if (copy_from_user(&irq, argp, sizeof irq))
3295 goto out;
3296 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003297 break;
3298 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003299 case KVM_NMI: {
3300 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003301 break;
3302 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003303 case KVM_SMI: {
3304 r = kvm_vcpu_ioctl_smi(vcpu);
3305 break;
3306 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003307 case KVM_SET_CPUID: {
3308 struct kvm_cpuid __user *cpuid_arg = argp;
3309 struct kvm_cpuid cpuid;
3310
3311 r = -EFAULT;
3312 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3313 goto out;
3314 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003315 break;
3316 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003317 case KVM_SET_CPUID2: {
3318 struct kvm_cpuid2 __user *cpuid_arg = argp;
3319 struct kvm_cpuid2 cpuid;
3320
3321 r = -EFAULT;
3322 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3323 goto out;
3324 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003325 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003326 break;
3327 }
3328 case KVM_GET_CPUID2: {
3329 struct kvm_cpuid2 __user *cpuid_arg = argp;
3330 struct kvm_cpuid2 cpuid;
3331
3332 r = -EFAULT;
3333 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3334 goto out;
3335 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003336 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003337 if (r)
3338 goto out;
3339 r = -EFAULT;
3340 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3341 goto out;
3342 r = 0;
3343 break;
3344 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003345 case KVM_GET_MSRS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003346 r = msr_io(vcpu, argp, do_get_msr, 1);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003347 break;
3348 case KVM_SET_MSRS:
3349 r = msr_io(vcpu, argp, do_set_msr, 0);
3350 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02003351 case KVM_TPR_ACCESS_REPORTING: {
3352 struct kvm_tpr_access_ctl tac;
3353
3354 r = -EFAULT;
3355 if (copy_from_user(&tac, argp, sizeof tac))
3356 goto out;
3357 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3358 if (r)
3359 goto out;
3360 r = -EFAULT;
3361 if (copy_to_user(argp, &tac, sizeof tac))
3362 goto out;
3363 r = 0;
3364 break;
3365 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003366 case KVM_SET_VAPIC_ADDR: {
3367 struct kvm_vapic_addr va;
3368
3369 r = -EINVAL;
3370 if (!irqchip_in_kernel(vcpu->kvm))
3371 goto out;
3372 r = -EFAULT;
3373 if (copy_from_user(&va, argp, sizeof va))
3374 goto out;
Andy Honigfda4e2e2013-11-20 10:23:22 -08003375 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Avi Kivityb93463a2007-10-25 16:52:32 +02003376 break;
3377 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003378 case KVM_X86_SETUP_MCE: {
3379 u64 mcg_cap;
3380
3381 r = -EFAULT;
3382 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3383 goto out;
3384 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3385 break;
3386 }
3387 case KVM_X86_SET_MCE: {
3388 struct kvm_x86_mce mce;
3389
3390 r = -EFAULT;
3391 if (copy_from_user(&mce, argp, sizeof mce))
3392 goto out;
3393 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3394 break;
3395 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003396 case KVM_GET_VCPU_EVENTS: {
3397 struct kvm_vcpu_events events;
3398
3399 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3400
3401 r = -EFAULT;
3402 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3403 break;
3404 r = 0;
3405 break;
3406 }
3407 case KVM_SET_VCPU_EVENTS: {
3408 struct kvm_vcpu_events events;
3409
3410 r = -EFAULT;
3411 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3412 break;
3413
3414 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3415 break;
3416 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003417 case KVM_GET_DEBUGREGS: {
3418 struct kvm_debugregs dbgregs;
3419
3420 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3421
3422 r = -EFAULT;
3423 if (copy_to_user(argp, &dbgregs,
3424 sizeof(struct kvm_debugregs)))
3425 break;
3426 r = 0;
3427 break;
3428 }
3429 case KVM_SET_DEBUGREGS: {
3430 struct kvm_debugregs dbgregs;
3431
3432 r = -EFAULT;
3433 if (copy_from_user(&dbgregs, argp,
3434 sizeof(struct kvm_debugregs)))
3435 break;
3436
3437 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3438 break;
3439 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003440 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003441 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003442 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003443 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003444 break;
3445
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003446 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003447
3448 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003449 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003450 break;
3451 r = 0;
3452 break;
3453 }
3454 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003455 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Guo Chao18595412012-11-02 18:33:21 +08003456 if (IS_ERR(u.xsave))
3457 return PTR_ERR(u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003458
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003459 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003460 break;
3461 }
3462 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003463 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003464 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003465 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003466 break;
3467
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003468 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003469
3470 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003471 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003472 sizeof(struct kvm_xcrs)))
3473 break;
3474 r = 0;
3475 break;
3476 }
3477 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003478 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Guo Chao18595412012-11-02 18:33:21 +08003479 if (IS_ERR(u.xcrs))
3480 return PTR_ERR(u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003481
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003482 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003483 break;
3484 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003485 case KVM_SET_TSC_KHZ: {
3486 u32 user_tsc_khz;
3487
3488 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003489 user_tsc_khz = (u32)arg;
3490
3491 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3492 goto out;
3493
Zachary Amsdencc578282012-02-03 15:43:50 -02003494 if (user_tsc_khz == 0)
3495 user_tsc_khz = tsc_khz;
3496
3497 kvm_set_tsc_khz(vcpu, user_tsc_khz);
Joerg Roedel92a1f122011-03-25 09:44:51 +01003498
3499 r = 0;
3500 goto out;
3501 }
3502 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003503 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003504 goto out;
3505 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003506 case KVM_KVMCLOCK_CTRL: {
3507 r = kvm_set_guest_paused(vcpu);
3508 goto out;
3509 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003510 default:
3511 r = -EINVAL;
3512 }
3513out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003514 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003515 return r;
3516}
3517
Carsten Otte5b1c1492012-01-04 10:25:23 +01003518int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
3519{
3520 return VM_FAULT_SIGBUS;
3521}
3522
Carsten Otte1fe779f2007-10-29 16:08:35 +01003523static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
3524{
3525 int ret;
3526
3527 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08003528 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003529 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
3530 return ret;
3531}
3532
Sheng Yangb927a3c2009-07-21 10:42:48 +08003533static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
3534 u64 ident_addr)
3535{
3536 kvm->arch.ept_identity_map_addr = ident_addr;
3537 return 0;
3538}
3539
Carsten Otte1fe779f2007-10-29 16:08:35 +01003540static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
3541 u32 kvm_nr_mmu_pages)
3542{
3543 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
3544 return -EINVAL;
3545
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003546 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003547
3548 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003549 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003550
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003551 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003552 return 0;
3553}
3554
3555static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
3556{
Dave Hansen39de71e2010-08-19 18:11:14 -07003557 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003558}
3559
Carsten Otte1fe779f2007-10-29 16:08:35 +01003560static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3561{
3562 int r;
3563
3564 r = 0;
3565 switch (chip->chip_id) {
3566 case KVM_IRQCHIP_PIC_MASTER:
3567 memcpy(&chip->chip.pic,
3568 &pic_irqchip(kvm)->pics[0],
3569 sizeof(struct kvm_pic_state));
3570 break;
3571 case KVM_IRQCHIP_PIC_SLAVE:
3572 memcpy(&chip->chip.pic,
3573 &pic_irqchip(kvm)->pics[1],
3574 sizeof(struct kvm_pic_state));
3575 break;
3576 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003577 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003578 break;
3579 default:
3580 r = -EINVAL;
3581 break;
3582 }
3583 return r;
3584}
3585
3586static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3587{
3588 int r;
3589
3590 r = 0;
3591 switch (chip->chip_id) {
3592 case KVM_IRQCHIP_PIC_MASTER:
Avi Kivityf4f51052010-09-19 18:44:07 +02003593 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003594 memcpy(&pic_irqchip(kvm)->pics[0],
3595 &chip->chip.pic,
3596 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003597 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003598 break;
3599 case KVM_IRQCHIP_PIC_SLAVE:
Avi Kivityf4f51052010-09-19 18:44:07 +02003600 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003601 memcpy(&pic_irqchip(kvm)->pics[1],
3602 &chip->chip.pic,
3603 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003604 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003605 break;
3606 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003607 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003608 break;
3609 default:
3610 r = -EINVAL;
3611 break;
3612 }
3613 kvm_pic_update_irq(pic_irqchip(kvm));
3614 return r;
3615}
3616
Sheng Yange0f63cb2008-03-04 00:50:59 +08003617static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3618{
3619 int r = 0;
3620
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003621 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003622 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003623 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003624 return r;
3625}
3626
3627static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3628{
3629 int r = 0;
3630
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003631 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003632 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04003633 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
3634 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
3635 return r;
3636}
3637
3638static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3639{
3640 int r = 0;
3641
3642 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3643 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
3644 sizeof(ps->channels));
3645 ps->flags = kvm->arch.vpit->pit_state.flags;
3646 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003647 memset(&ps->reserved, 0, sizeof(ps->reserved));
Beth Kone9f42752009-07-07 11:50:38 -04003648 return r;
3649}
3650
3651static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3652{
3653 int r = 0, start = 0;
3654 u32 prev_legacy, cur_legacy;
3655 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3656 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
3657 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
3658 if (!prev_legacy && cur_legacy)
3659 start = 1;
3660 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
3661 sizeof(kvm->arch.vpit->pit_state.channels));
3662 kvm->arch.vpit->pit_state.flags = ps->flags;
3663 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003664 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003665 return r;
3666}
3667
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003668static int kvm_vm_ioctl_reinject(struct kvm *kvm,
3669 struct kvm_reinject_control *control)
3670{
3671 if (!kvm->arch.vpit)
3672 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003673 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Avi Kivity26ef1922012-07-26 18:01:53 +03003674 kvm->arch.vpit->pit_state.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003675 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003676 return 0;
3677}
3678
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003679/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003680 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
3681 * @kvm: kvm instance
3682 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003683 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003684 * Steps 1-4 below provide general overview of dirty page logging. See
3685 * kvm_get_dirty_log_protect() function description for additional details.
3686 *
3687 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
3688 * always flush the TLB (step 4) even if previous step failed and the dirty
3689 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
3690 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
3691 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003692 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003693 * 1. Take a snapshot of the bit and clear it if needed.
3694 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003695 * 3. Copy the snapshot to the userspace.
3696 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003697 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003698int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003699{
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003700 bool is_dirty = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003701 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003702
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003703 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003704
Kai Huang88178fd2015-01-28 10:54:27 +08003705 /*
3706 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
3707 */
3708 if (kvm_x86_ops->flush_log_dirty)
3709 kvm_x86_ops->flush_log_dirty(kvm);
3710
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003711 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003712
3713 /*
3714 * All the TLBs can be flushed out of mmu lock, see the comments in
3715 * kvm_mmu_slot_remove_write_access().
3716 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003717 lockdep_assert_held(&kvm->slots_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003718 if (is_dirty)
3719 kvm_flush_remote_tlbs(kvm);
3720
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003721 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003722 return r;
3723}
3724
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003725int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
3726 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003727{
3728 if (!irqchip_in_kernel(kvm))
3729 return -ENXIO;
3730
3731 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003732 irq_event->irq, irq_event->level,
3733 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003734 return 0;
3735}
3736
Nadav Amit90de4a12015-04-13 01:53:41 +03003737static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3738 struct kvm_enable_cap *cap)
3739{
3740 int r;
3741
3742 if (cap->flags)
3743 return -EINVAL;
3744
3745 switch (cap->cap) {
3746 case KVM_CAP_DISABLE_QUIRKS:
3747 kvm->arch.disabled_quirks = cap->args[0];
3748 r = 0;
3749 break;
3750 default:
3751 r = -EINVAL;
3752 break;
3753 }
3754 return r;
3755}
3756
Carsten Otte1fe779f2007-10-29 16:08:35 +01003757long kvm_arch_vm_ioctl(struct file *filp,
3758 unsigned int ioctl, unsigned long arg)
3759{
3760 struct kvm *kvm = filp->private_data;
3761 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03003762 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07003763 /*
3764 * This union makes it completely explicit to gcc-3.x
3765 * that these two variables' stack usage should be
3766 * combined, not added together.
3767 */
3768 union {
3769 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04003770 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003771 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07003772 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003773
3774 switch (ioctl) {
3775 case KVM_SET_TSS_ADDR:
3776 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003777 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003778 case KVM_SET_IDENTITY_MAP_ADDR: {
3779 u64 ident_addr;
3780
3781 r = -EFAULT;
3782 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
3783 goto out;
3784 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08003785 break;
3786 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003787 case KVM_SET_NR_MMU_PAGES:
3788 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003789 break;
3790 case KVM_GET_NR_MMU_PAGES:
3791 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3792 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003793 case KVM_CREATE_IRQCHIP: {
3794 struct kvm_pic *vpic;
3795
3796 mutex_lock(&kvm->lock);
3797 r = -EEXIST;
3798 if (kvm->arch.vpic)
3799 goto create_irqchip_unlock;
Avi Kivity3e515702012-03-05 14:23:29 +02003800 r = -EINVAL;
3801 if (atomic_read(&kvm->online_vcpus))
3802 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003803 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003804 vpic = kvm_create_pic(kvm);
3805 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003806 r = kvm_ioapic_init(kvm);
3807 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003808 mutex_lock(&kvm->slots_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003809 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
Sasha Levin743eeb02011-07-27 16:00:48 +03003810 &vpic->dev_master);
3811 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3812 &vpic->dev_slave);
3813 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3814 &vpic->dev_eclr);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003815 mutex_unlock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003816 kfree(vpic);
3817 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003818 }
3819 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003820 goto create_irqchip_unlock;
3821 smp_wmb();
3822 kvm->arch.vpic = vpic;
3823 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02003824 r = kvm_setup_default_irq_routing(kvm);
3825 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003826 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003827 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003828 kvm_ioapic_destroy(kvm);
3829 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003830 mutex_unlock(&kvm->irq_lock);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003831 mutex_unlock(&kvm->slots_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02003832 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003833 create_irqchip_unlock:
3834 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003835 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003836 }
Sheng Yang78376992008-01-28 05:10:22 +08003837 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003838 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3839 goto create_pit;
3840 case KVM_CREATE_PIT2:
3841 r = -EFAULT;
3842 if (copy_from_user(&u.pit_config, argp,
3843 sizeof(struct kvm_pit_config)))
3844 goto out;
3845 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003846 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003847 r = -EEXIST;
3848 if (kvm->arch.vpit)
3849 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003850 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003851 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003852 if (kvm->arch.vpit)
3853 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003854 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003855 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08003856 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003857 case KVM_GET_IRQCHIP: {
3858 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003859 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003860
Sasha Levinff5c2c02011-12-04 19:36:29 +02003861 chip = memdup_user(argp, sizeof(*chip));
3862 if (IS_ERR(chip)) {
3863 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003864 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003865 }
3866
Carsten Otte1fe779f2007-10-29 16:08:35 +01003867 r = -ENXIO;
3868 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003869 goto get_irqchip_out;
3870 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3871 if (r)
3872 goto get_irqchip_out;
3873 r = -EFAULT;
3874 if (copy_to_user(argp, chip, sizeof *chip))
3875 goto get_irqchip_out;
3876 r = 0;
3877 get_irqchip_out:
3878 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003879 break;
3880 }
3881 case KVM_SET_IRQCHIP: {
3882 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003883 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003884
Sasha Levinff5c2c02011-12-04 19:36:29 +02003885 chip = memdup_user(argp, sizeof(*chip));
3886 if (IS_ERR(chip)) {
3887 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003888 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003889 }
3890
Carsten Otte1fe779f2007-10-29 16:08:35 +01003891 r = -ENXIO;
3892 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003893 goto set_irqchip_out;
3894 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3895 if (r)
3896 goto set_irqchip_out;
3897 r = 0;
3898 set_irqchip_out:
3899 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003900 break;
3901 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003902 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003903 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003904 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003905 goto out;
3906 r = -ENXIO;
3907 if (!kvm->arch.vpit)
3908 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003909 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003910 if (r)
3911 goto out;
3912 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003913 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003914 goto out;
3915 r = 0;
3916 break;
3917 }
3918 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003919 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003920 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003921 goto out;
3922 r = -ENXIO;
3923 if (!kvm->arch.vpit)
3924 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003925 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003926 break;
3927 }
Beth Kone9f42752009-07-07 11:50:38 -04003928 case KVM_GET_PIT2: {
3929 r = -ENXIO;
3930 if (!kvm->arch.vpit)
3931 goto out;
3932 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3933 if (r)
3934 goto out;
3935 r = -EFAULT;
3936 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3937 goto out;
3938 r = 0;
3939 break;
3940 }
3941 case KVM_SET_PIT2: {
3942 r = -EFAULT;
3943 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3944 goto out;
3945 r = -ENXIO;
3946 if (!kvm->arch.vpit)
3947 goto out;
3948 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04003949 break;
3950 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003951 case KVM_REINJECT_CONTROL: {
3952 struct kvm_reinject_control control;
3953 r = -EFAULT;
3954 if (copy_from_user(&control, argp, sizeof(control)))
3955 goto out;
3956 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003957 break;
3958 }
Ed Swierkffde22a2009-10-15 15:21:43 -07003959 case KVM_XEN_HVM_CONFIG: {
3960 r = -EFAULT;
3961 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3962 sizeof(struct kvm_xen_hvm_config)))
3963 goto out;
3964 r = -EINVAL;
3965 if (kvm->arch.xen_hvm_config.flags)
3966 goto out;
3967 r = 0;
3968 break;
3969 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003970 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003971 struct kvm_clock_data user_ns;
3972 u64 now_ns;
3973 s64 delta;
3974
3975 r = -EFAULT;
3976 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3977 goto out;
3978
3979 r = -EINVAL;
3980 if (user_ns.flags)
3981 goto out;
3982
3983 r = 0;
Avi Kivity395c6b02010-10-04 12:55:49 +02003984 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003985 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003986 delta = user_ns.clock - now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003987 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003988 kvm->arch.kvmclock_offset = delta;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03003989 kvm_gen_update_masterclock(kvm);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003990 break;
3991 }
3992 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003993 struct kvm_clock_data user_ns;
3994 u64 now_ns;
3995
Avi Kivity395c6b02010-10-04 12:55:49 +02003996 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003997 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003998 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003999 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004000 user_ns.flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004001 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004002
4003 r = -EFAULT;
4004 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
4005 goto out;
4006 r = 0;
4007 break;
4008 }
Nadav Amit90de4a12015-04-13 01:53:41 +03004009 case KVM_ENABLE_CAP: {
4010 struct kvm_enable_cap cap;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004011
Nadav Amit90de4a12015-04-13 01:53:41 +03004012 r = -EFAULT;
4013 if (copy_from_user(&cap, argp, sizeof(cap)))
4014 goto out;
4015 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
4016 break;
4017 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004018 default:
Radim Krčmářc274e032014-11-21 22:21:50 +01004019 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004020 }
4021out:
4022 return r;
4023}
4024
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08004025static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02004026{
4027 u32 dummy[2];
4028 unsigned i, j;
4029
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004030 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02004031 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
4032 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004033
4034 /*
4035 * Even MSRs that are valid in the host may not be exposed
4036 * to the guests in some cases. We could work around this
4037 * in VMX with the generic MSR save/load machinery, but it
4038 * is not really worthwhile since it will really only
4039 * happen with nested virtualization.
4040 */
4041 switch (msrs_to_save[i]) {
4042 case MSR_IA32_BNDCFGS:
4043 if (!kvm_x86_ops->mpx_supported())
4044 continue;
4045 break;
4046 default:
4047 break;
4048 }
4049
Carsten Otte043405e2007-10-10 17:16:19 +02004050 if (j < i)
4051 msrs_to_save[j] = msrs_to_save[i];
4052 j++;
4053 }
4054 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004055
4056 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
4057 switch (emulated_msrs[i]) {
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004058 case MSR_IA32_SMBASE:
4059 if (!kvm_x86_ops->cpu_has_high_real_mode_segbase())
4060 continue;
4061 break;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004062 default:
4063 break;
4064 }
4065
4066 if (j < i)
4067 emulated_msrs[j] = emulated_msrs[i];
4068 j++;
4069 }
4070 num_emulated_msrs = j;
Carsten Otte043405e2007-10-10 17:16:19 +02004071}
4072
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004073static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
4074 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004075{
Avi Kivity70252a12010-01-19 12:51:22 +02004076 int handled = 0;
4077 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004078
Avi Kivity70252a12010-01-19 12:51:22 +02004079 do {
4080 n = min(len, 8);
4081 if (!(vcpu->arch.apic &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004082 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
4083 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004084 break;
4085 handled += n;
4086 addr += n;
4087 len -= n;
4088 v += n;
4089 } while (len);
4090
4091 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004092}
4093
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004094static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004095{
Avi Kivity70252a12010-01-19 12:51:22 +02004096 int handled = 0;
4097 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004098
Avi Kivity70252a12010-01-19 12:51:22 +02004099 do {
4100 n = min(len, 8);
4101 if (!(vcpu->arch.apic &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004102 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
4103 addr, n, v))
4104 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004105 break;
4106 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
4107 handled += n;
4108 addr += n;
4109 len -= n;
4110 v += n;
4111 } while (len);
4112
4113 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004114}
4115
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004116static void kvm_set_segment(struct kvm_vcpu *vcpu,
4117 struct kvm_segment *var, int seg)
4118{
4119 kvm_x86_ops->set_segment(vcpu, var, seg);
4120}
4121
4122void kvm_get_segment(struct kvm_vcpu *vcpu,
4123 struct kvm_segment *var, int seg)
4124{
4125 kvm_x86_ops->get_segment(vcpu, var, seg);
4126}
4127
Paolo Bonzini54987b72014-09-02 13:23:06 +02004128gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
4129 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004130{
4131 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004132
4133 BUG_ON(!mmu_is_nested(vcpu));
4134
4135 /* NPT walks are always user-walks */
4136 access |= PFERR_USER_MASK;
Paolo Bonzini54987b72014-09-02 13:23:06 +02004137 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004138
4139 return t_gpa;
4140}
4141
Avi Kivityab9ae312010-11-22 17:53:26 +02004142gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
4143 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004144{
4145 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02004146 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004147}
4148
Avi Kivityab9ae312010-11-22 17:53:26 +02004149 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4150 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004151{
4152 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4153 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004154 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004155}
4156
Avi Kivityab9ae312010-11-22 17:53:26 +02004157gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4158 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004159{
4160 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4161 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004162 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004163}
4164
4165/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004166gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4167 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004168{
Avi Kivityab9ae312010-11-22 17:53:26 +02004169 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004170}
4171
4172static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4173 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004174 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004175{
4176 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004177 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004178
4179 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004180 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004181 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004182 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004183 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004184 int ret;
4185
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004186 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004187 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004188 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
4189 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004190 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004191 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004192 goto out;
4193 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004194
Izik Eidus77c20022008-12-29 01:42:19 +02004195 bytes -= toread;
4196 data += toread;
4197 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004198 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004199out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004200 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004201}
Izik Eidus77c20022008-12-29 01:42:19 +02004202
Gleb Natapov1871c602010-02-10 14:21:32 +02004203/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004204static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4205 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004206 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004207{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004208 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004209 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004210 unsigned offset;
4211 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004212
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004213 /* Inline kvm_read_guest_virt_helper for speed. */
4214 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4215 exception);
4216 if (unlikely(gpa == UNMAPPED_GVA))
4217 return X86EMUL_PROPAGATE_FAULT;
4218
4219 offset = addr & (PAGE_SIZE-1);
4220 if (WARN_ON(offset + bytes > PAGE_SIZE))
4221 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004222 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
4223 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004224 if (unlikely(ret < 0))
4225 return X86EMUL_IO_NEEDED;
4226
4227 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004228}
4229
Nadav Har'El064aea72011-05-25 23:04:56 +03004230int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004231 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004232 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004233{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004234 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004235 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004236
Gleb Natapov1871c602010-02-10 14:21:32 +02004237 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004238 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004239}
Nadav Har'El064aea72011-05-25 23:04:56 +03004240EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004241
Avi Kivity0f65dd72011-04-20 13:37:53 +03004242static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
4243 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004244 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004245{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004246 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004247 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004248}
4249
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004250int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004251 gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004252 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004253 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004254{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004255 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Izik Eidus77c20022008-12-29 01:42:19 +02004256 void *data = val;
4257 int r = X86EMUL_CONTINUE;
4258
4259 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004260 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
4261 PFERR_WRITE_MASK,
Avi Kivityab9ae312010-11-22 17:53:26 +02004262 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004263 unsigned offset = addr & (PAGE_SIZE-1);
4264 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4265 int ret;
4266
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004267 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004268 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004269 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02004270 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004271 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004272 goto out;
4273 }
4274
4275 bytes -= towrite;
4276 data += towrite;
4277 addr += towrite;
4278 }
4279out:
4280 return r;
4281}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004282EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004283
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004284static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
4285 gpa_t *gpa, struct x86_exception *exception,
4286 bool write)
4287{
Avi Kivity97d64b72012-09-12 14:52:00 +03004288 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
4289 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004290
Avi Kivity97d64b72012-09-12 14:52:00 +03004291 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08004292 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
4293 vcpu->arch.access, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004294 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
4295 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08004296 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004297 return 1;
4298 }
4299
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004300 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
4301
4302 if (*gpa == UNMAPPED_GVA)
4303 return -1;
4304
4305 /* For APIC access vmexit */
4306 if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4307 return 1;
4308
Xiao Guangrong4f022642011-07-12 03:34:24 +08004309 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
4310 trace_vcpu_match_mmio(gva, *gpa, write, true);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004311 return 1;
Xiao Guangrong4f022642011-07-12 03:34:24 +08004312 }
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004313
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004314 return 0;
4315}
4316
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004317int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004318 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02004319{
4320 int ret;
4321
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004322 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004323 if (ret < 0)
4324 return 0;
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004325 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004326 return 1;
4327}
4328
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004329struct read_write_emulator_ops {
4330 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
4331 int bytes);
4332 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
4333 void *val, int bytes);
4334 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4335 int bytes, void *val);
4336 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4337 void *val, int bytes);
4338 bool write;
4339};
4340
4341static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
4342{
4343 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004344 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Avi Kivityf78146b2012-04-18 19:22:47 +03004345 vcpu->mmio_fragments[0].gpa, *(u64 *)val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004346 vcpu->mmio_read_completed = 0;
4347 return 1;
4348 }
4349
4350 return 0;
4351}
4352
4353static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4354 void *val, int bytes)
4355{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004356 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004357}
4358
4359static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4360 void *val, int bytes)
4361{
4362 return emulator_write_phys(vcpu, gpa, val, bytes);
4363}
4364
4365static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
4366{
4367 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4368 return vcpu_mmio_write(vcpu, gpa, bytes, val);
4369}
4370
4371static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4372 void *val, int bytes)
4373{
4374 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4375 return X86EMUL_IO_NEEDED;
4376}
4377
4378static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4379 void *val, int bytes)
4380{
Avi Kivityf78146b2012-04-18 19:22:47 +03004381 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
4382
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004383 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004384 return X86EMUL_CONTINUE;
4385}
4386
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004387static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004388 .read_write_prepare = read_prepare,
4389 .read_write_emulate = read_emulate,
4390 .read_write_mmio = vcpu_mmio_read,
4391 .read_write_exit_mmio = read_exit_mmio,
4392};
4393
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004394static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004395 .read_write_emulate = write_emulate,
4396 .read_write_mmio = write_mmio,
4397 .read_write_exit_mmio = write_exit_mmio,
4398 .write = true,
4399};
4400
Xiao Guangrong22388a32011-07-13 14:32:31 +08004401static int emulator_read_write_onepage(unsigned long addr, void *val,
4402 unsigned int bytes,
4403 struct x86_exception *exception,
4404 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004405 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004406{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004407 gpa_t gpa;
4408 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004409 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03004410 struct kvm_mmio_fragment *frag;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004411
4412 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004413
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004414 if (ret < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004415 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004416
4417 /* For APIC access vmexit */
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004418 if (ret)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004419 goto mmio;
4420
Xiao Guangrong22388a32011-07-13 14:32:31 +08004421 if (ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004422 return X86EMUL_CONTINUE;
4423
4424mmio:
4425 /*
4426 * Is this MMIO handled locally?
4427 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08004428 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02004429 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004430 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004431
Avi Kivity70252a12010-01-19 12:51:22 +02004432 gpa += handled;
4433 bytes -= handled;
4434 val += handled;
4435
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004436 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
4437 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
4438 frag->gpa = gpa;
4439 frag->data = val;
4440 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03004441 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004442}
4443
Xiubo Li52eb5a62015-03-13 17:39:45 +08004444static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
4445 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004446 void *val, unsigned int bytes,
4447 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004448 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004449{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004450 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03004451 gpa_t gpa;
4452 int rc;
4453
4454 if (ops->read_write_prepare &&
4455 ops->read_write_prepare(vcpu, val, bytes))
4456 return X86EMUL_CONTINUE;
4457
4458 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004459
Carsten Ottebbd9b642007-10-30 18:44:21 +01004460 /* Crossing a page boundary? */
4461 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03004462 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004463
4464 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004465 rc = emulator_read_write_onepage(addr, val, now, exception,
4466 vcpu, ops);
4467
Carsten Ottebbd9b642007-10-30 18:44:21 +01004468 if (rc != X86EMUL_CONTINUE)
4469 return rc;
4470 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02004471 if (ctxt->mode != X86EMUL_MODE_PROT64)
4472 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004473 val += now;
4474 bytes -= now;
4475 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08004476
Avi Kivityf78146b2012-04-18 19:22:47 +03004477 rc = emulator_read_write_onepage(addr, val, bytes, exception,
4478 vcpu, ops);
4479 if (rc != X86EMUL_CONTINUE)
4480 return rc;
4481
4482 if (!vcpu->mmio_nr_fragments)
4483 return rc;
4484
4485 gpa = vcpu->mmio_fragments[0].gpa;
4486
4487 vcpu->mmio_needed = 1;
4488 vcpu->mmio_cur_fragment = 0;
4489
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004490 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03004491 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
4492 vcpu->run->exit_reason = KVM_EXIT_MMIO;
4493 vcpu->run->mmio.phys_addr = gpa;
4494
4495 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08004496}
4497
4498static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4499 unsigned long addr,
4500 void *val,
4501 unsigned int bytes,
4502 struct x86_exception *exception)
4503{
4504 return emulator_read_write(ctxt, addr, val, bytes,
4505 exception, &read_emultor);
4506}
4507
Xiubo Li52eb5a62015-03-13 17:39:45 +08004508static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004509 unsigned long addr,
4510 const void *val,
4511 unsigned int bytes,
4512 struct x86_exception *exception)
4513{
4514 return emulator_read_write(ctxt, addr, (void *)val, bytes,
4515 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004516}
Carsten Ottebbd9b642007-10-30 18:44:21 +01004517
Avi Kivitydaea3e72010-03-15 13:59:54 +02004518#define CMPXCHG_TYPE(t, ptr, old, new) \
4519 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
4520
4521#ifdef CONFIG_X86_64
4522# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
4523#else
4524# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01004525 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02004526#endif
4527
Avi Kivity0f65dd72011-04-20 13:37:53 +03004528static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
4529 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004530 const void *old,
4531 const void *new,
4532 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004533 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004534{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004535 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004536 gpa_t gpa;
4537 struct page *page;
4538 char *kaddr;
4539 bool exchanged;
4540
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004541 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02004542 if (bytes > 8 || (bytes & (bytes - 1)))
4543 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004544
Avi Kivitydaea3e72010-03-15 13:59:54 +02004545 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004546
Avi Kivitydaea3e72010-03-15 13:59:54 +02004547 if (gpa == UNMAPPED_GVA ||
4548 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4549 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004550
Avi Kivitydaea3e72010-03-15 13:59:54 +02004551 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
4552 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004553
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004554 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08004555 if (is_error_page(page))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08004556 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004557
Cong Wang8fd75e12011-11-25 23:14:17 +08004558 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004559 kaddr += offset_in_page(gpa);
4560 switch (bytes) {
4561 case 1:
4562 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
4563 break;
4564 case 2:
4565 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
4566 break;
4567 case 4:
4568 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
4569 break;
4570 case 8:
4571 exchanged = CMPXCHG64(kaddr, old, new);
4572 break;
4573 default:
4574 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004575 }
Cong Wang8fd75e12011-11-25 23:14:17 +08004576 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004577 kvm_release_page_dirty(page);
4578
4579 if (!exchanged)
4580 return X86EMUL_CMPXCHG_FAILED;
4581
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004582 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004583 kvm_mmu_pte_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004584
4585 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02004586
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004587emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02004588 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004589
Avi Kivity0f65dd72011-04-20 13:37:53 +03004590 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004591}
4592
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004593static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
4594{
4595 /* TODO: String I/O for in kernel device */
4596 int r;
4597
4598 if (vcpu->arch.pio.in)
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004599 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004600 vcpu->arch.pio.size, pd);
4601 else
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004602 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004603 vcpu->arch.pio.port, vcpu->arch.pio.size,
4604 pd);
4605 return r;
4606}
4607
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004608static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
4609 unsigned short port, void *val,
4610 unsigned int count, bool in)
4611{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004612 vcpu->arch.pio.port = port;
4613 vcpu->arch.pio.in = in;
4614 vcpu->arch.pio.count = count;
4615 vcpu->arch.pio.size = size;
4616
4617 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
4618 vcpu->arch.pio.count = 0;
4619 return 1;
4620 }
4621
4622 vcpu->run->exit_reason = KVM_EXIT_IO;
4623 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
4624 vcpu->run->io.size = size;
4625 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
4626 vcpu->run->io.count = count;
4627 vcpu->run->io.port = port;
4628
4629 return 0;
4630}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004631
Avi Kivityca1d4a92011-04-20 13:37:53 +03004632static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
4633 int size, unsigned short port, void *val,
4634 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004635{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004636 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004637 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03004638
Gleb Natapov79729952010-03-18 15:20:24 +02004639 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004640 goto data_avail;
4641
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004642 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
4643 if (ret) {
4644data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004645 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004646 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02004647 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004648 return 1;
4649 }
4650
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004651 return 0;
4652}
4653
Avi Kivityca1d4a92011-04-20 13:37:53 +03004654static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
4655 int size, unsigned short port,
4656 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004657{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004658 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4659
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004660 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004661 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004662 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004663}
4664
Carsten Ottebbd9b642007-10-30 18:44:21 +01004665static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
4666{
4667 return kvm_x86_ops->get_segment_base(vcpu, seg);
4668}
4669
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004670static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004671{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004672 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004673}
4674
Joel Schopp5cb56052015-03-02 13:43:31 -06004675int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004676{
4677 if (!need_emulate_wbinvd(vcpu))
4678 return X86EMUL_CONTINUE;
4679
4680 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01004681 int cpu = get_cpu();
4682
4683 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004684 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
4685 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004686 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004687 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004688 } else
4689 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004690 return X86EMUL_CONTINUE;
4691}
Joel Schopp5cb56052015-03-02 13:43:31 -06004692
4693int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
4694{
4695 kvm_x86_ops->skip_emulated_instruction(vcpu);
4696 return kvm_emulate_wbinvd_noskip(vcpu);
4697}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004698EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
4699
Joel Schopp5cb56052015-03-02 13:43:31 -06004700
4701
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004702static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
4703{
Joel Schopp5cb56052015-03-02 13:43:31 -06004704 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004705}
4706
Xiubo Li52eb5a62015-03-13 17:39:45 +08004707static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
4708 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004709{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004710 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004711}
4712
Xiubo Li52eb5a62015-03-13 17:39:45 +08004713static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
4714 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004715{
Gleb Natapov338dbc92010-04-28 19:15:32 +03004716
Avi Kivity717746e2011-04-20 13:37:53 +03004717 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004718}
4719
Gleb Natapov52a46612010-03-18 15:20:03 +02004720static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4721{
4722 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4723}
4724
Avi Kivity717746e2011-04-20 13:37:53 +03004725static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02004726{
Avi Kivity717746e2011-04-20 13:37:53 +03004727 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02004728 unsigned long value;
4729
4730 switch (cr) {
4731 case 0:
4732 value = kvm_read_cr0(vcpu);
4733 break;
4734 case 2:
4735 value = vcpu->arch.cr2;
4736 break;
4737 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004738 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02004739 break;
4740 case 4:
4741 value = kvm_read_cr4(vcpu);
4742 break;
4743 case 8:
4744 value = kvm_get_cr8(vcpu);
4745 break;
4746 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004747 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02004748 return 0;
4749 }
4750
4751 return value;
4752}
4753
Avi Kivity717746e2011-04-20 13:37:53 +03004754static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02004755{
Avi Kivity717746e2011-04-20 13:37:53 +03004756 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03004757 int res = 0;
4758
Gleb Natapov52a46612010-03-18 15:20:03 +02004759 switch (cr) {
4760 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03004761 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004762 break;
4763 case 2:
4764 vcpu->arch.cr2 = val;
4765 break;
4766 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004767 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004768 break;
4769 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03004770 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004771 break;
4772 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004773 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004774 break;
4775 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004776 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03004777 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02004778 }
Gleb Natapov0f122442010-04-28 19:15:31 +03004779
4780 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02004781}
4782
Avi Kivity717746e2011-04-20 13:37:53 +03004783static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02004784{
Avi Kivity717746e2011-04-20 13:37:53 +03004785 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02004786}
4787
Avi Kivity4bff1e862011-04-20 13:37:53 +03004788static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004789{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004790 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004791}
4792
Avi Kivity4bff1e862011-04-20 13:37:53 +03004793static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004794{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004795 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004796}
4797
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004798static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4799{
4800 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
4801}
4802
4803static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4804{
4805 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
4806}
4807
Avi Kivity4bff1e862011-04-20 13:37:53 +03004808static unsigned long emulator_get_cached_segment_base(
4809 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03004810{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004811 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03004812}
4813
Avi Kivity1aa36612011-04-27 13:20:30 +03004814static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
4815 struct desc_struct *desc, u32 *base3,
4816 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004817{
4818 struct kvm_segment var;
4819
Avi Kivity4bff1e862011-04-20 13:37:53 +03004820 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03004821 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004822
Gleb Natapov378a8b02013-01-21 15:36:48 +02004823 if (var.unusable) {
4824 memset(desc, 0, sizeof(*desc));
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004825 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02004826 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004827
4828 if (var.g)
4829 var.limit >>= 12;
4830 set_desc_limit(desc, var.limit);
4831 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02004832#ifdef CONFIG_X86_64
4833 if (base3)
4834 *base3 = var.base >> 32;
4835#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004836 desc->type = var.type;
4837 desc->s = var.s;
4838 desc->dpl = var.dpl;
4839 desc->p = var.present;
4840 desc->avl = var.avl;
4841 desc->l = var.l;
4842 desc->d = var.db;
4843 desc->g = var.g;
4844
4845 return true;
4846}
4847
Avi Kivity1aa36612011-04-27 13:20:30 +03004848static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
4849 struct desc_struct *desc, u32 base3,
4850 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004851{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004852 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004853 struct kvm_segment var;
4854
Avi Kivity1aa36612011-04-27 13:20:30 +03004855 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004856 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02004857#ifdef CONFIG_X86_64
4858 var.base |= ((u64)base3) << 32;
4859#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004860 var.limit = get_desc_limit(desc);
4861 if (desc->g)
4862 var.limit = (var.limit << 12) | 0xfff;
4863 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004864 var.dpl = desc->dpl;
4865 var.db = desc->d;
4866 var.s = desc->s;
4867 var.l = desc->l;
4868 var.g = desc->g;
4869 var.avl = desc->avl;
4870 var.present = desc->p;
4871 var.unusable = !var.present;
4872 var.padding = 0;
4873
4874 kvm_set_segment(vcpu, &var, seg);
4875 return;
4876}
4877
Avi Kivity717746e2011-04-20 13:37:53 +03004878static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
4879 u32 msr_index, u64 *pdata)
4880{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004881 struct msr_data msr;
4882 int r;
4883
4884 msr.index = msr_index;
4885 msr.host_initiated = false;
4886 r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
4887 if (r)
4888 return r;
4889
4890 *pdata = msr.data;
4891 return 0;
Avi Kivity717746e2011-04-20 13:37:53 +03004892}
4893
4894static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
4895 u32 msr_index, u64 data)
4896{
Will Auld8fe8ab42012-11-29 12:42:12 -08004897 struct msr_data msr;
4898
4899 msr.data = data;
4900 msr.index = msr_index;
4901 msr.host_initiated = false;
4902 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03004903}
4904
Paolo Bonzini64d60672015-05-07 11:36:11 +02004905static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
4906{
4907 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4908
4909 return vcpu->arch.smbase;
4910}
4911
4912static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
4913{
4914 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4915
4916 vcpu->arch.smbase = smbase;
4917}
4918
Nadav Amit67f4d422014-06-02 18:34:09 +03004919static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
4920 u32 pmc)
4921{
Wei Huangc6702c92015-06-19 13:44:45 +02004922 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03004923}
4924
Avi Kivity222d21a2011-11-10 14:57:30 +02004925static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
4926 u32 pmc, u64 *pdata)
4927{
Wei Huangc6702c92015-06-19 13:44:45 +02004928 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02004929}
4930
Avi Kivity6c3287f2011-04-20 15:43:05 +03004931static void emulator_halt(struct x86_emulate_ctxt *ctxt)
4932{
4933 emul_to_vcpu(ctxt)->arch.halt_request = 1;
4934}
4935
Avi Kivity5037f6f2011-03-28 16:53:59 +02004936static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
4937{
4938 preempt_disable();
Avi Kivity5197b802011-04-20 15:55:40 +03004939 kvm_load_guest_fpu(emul_to_vcpu(ctxt));
Avi Kivity5037f6f2011-03-28 16:53:59 +02004940 /*
4941 * CR0.TS may reference the host fpu state, not the guest fpu state,
4942 * so it may be clear at this point.
4943 */
4944 clts();
4945}
4946
4947static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
4948{
4949 preempt_enable();
4950}
4951
Avi Kivity29535382011-04-20 13:37:53 +03004952static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004953 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004954 enum x86_intercept_stage stage)
4955{
Avi Kivity29535382011-04-20 13:37:53 +03004956 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004957}
4958
Avi Kivity0017f932012-06-07 14:10:16 +03004959static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004960 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
4961{
Avi Kivity0017f932012-06-07 14:10:16 +03004962 kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004963}
4964
Avi Kivitydd856ef2012-08-27 23:46:17 +03004965static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
4966{
4967 return kvm_register_read(emul_to_vcpu(ctxt), reg);
4968}
4969
4970static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
4971{
4972 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
4973}
4974
Nadav Amit801806d2015-01-26 09:32:23 +02004975static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
4976{
4977 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
4978}
4979
Mathias Krause0225fb52012-08-30 01:30:16 +02004980static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03004981 .read_gpr = emulator_read_gpr,
4982 .write_gpr = emulator_write_gpr,
Gleb Natapov1871c602010-02-10 14:21:32 +02004983 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004984 .write_std = kvm_write_guest_virt_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02004985 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004986 .read_emulated = emulator_read_emulated,
4987 .write_emulated = emulator_write_emulated,
4988 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004989 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004990 .pio_in_emulated = emulator_pio_in_emulated,
4991 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03004992 .get_segment = emulator_get_segment,
4993 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03004994 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004995 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004996 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004997 .set_gdt = emulator_set_gdt,
4998 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02004999 .get_cr = emulator_get_cr,
5000 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02005001 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03005002 .get_dr = emulator_get_dr,
5003 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02005004 .get_smbase = emulator_get_smbase,
5005 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03005006 .set_msr = emulator_set_msr,
5007 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03005008 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02005009 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03005010 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005011 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03005012 .fix_hypercall = emulator_fix_hypercall,
Avi Kivity5037f6f2011-03-28 16:53:59 +02005013 .get_fpu = emulator_get_fpu,
5014 .put_fpu = emulator_put_fpu,
Avi Kivityc4f035c2011-04-04 12:39:22 +02005015 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005016 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02005017 .set_nmi_mask = emulator_set_nmi_mask,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005018};
5019
Gleb Natapov95cb2292010-04-28 19:15:43 +03005020static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
5021{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02005022 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03005023 /*
5024 * an sti; sti; sequence only disable interrupts for the first
5025 * instruction. So, if the last instruction, be it emulated or
5026 * not, left the system with the INT_STI flag enabled, it
5027 * means that the last instruction is an sti. We should not
5028 * leave the flag on in this case. The same goes for mov ss
5029 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02005030 if (int_shadow & mask)
5031 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005032 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03005033 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005034 if (!mask)
5035 kvm_make_request(KVM_REQ_EVENT, vcpu);
5036 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03005037}
5038
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005039static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03005040{
5041 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02005042 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005043 return kvm_propagate_fault(vcpu, &ctxt->exception);
5044
5045 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02005046 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
5047 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03005048 else
Avi Kivityda9cb572010-11-22 17:53:21 +02005049 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005050 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03005051}
5052
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005053static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
5054{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005055 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005056 int cs_db, cs_l;
5057
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005058 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
5059
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005060 ctxt->eflags = kvm_get_rflags(vcpu);
5061 ctxt->eip = kvm_rip_read(vcpu);
5062 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
5063 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03005064 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005065 cs_db ? X86EMUL_MODE_PROT32 :
5066 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005067 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005068 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
5069 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005070 ctxt->emul_flags = vcpu->arch.hflags;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005071
Avi Kivitydd856ef2012-08-27 23:46:17 +03005072 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005073 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005074}
5075
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005076int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02005077{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005078 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02005079 int ret;
5080
5081 init_emulate_ctxt(vcpu);
5082
Avi Kivity9dac77f2011-06-01 15:34:25 +03005083 ctxt->op_bytes = 2;
5084 ctxt->ad_bytes = 2;
5085 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005086 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02005087
5088 if (ret != X86EMUL_CONTINUE)
5089 return EMULATE_FAIL;
5090
Avi Kivity9dac77f2011-06-01 15:34:25 +03005091 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005092 kvm_rip_write(vcpu, ctxt->eip);
5093 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02005094
5095 if (irq == NMI_VECTOR)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005096 vcpu->arch.nmi_pending = 0;
Mohammed Gamal63995652010-09-19 14:34:06 +02005097 else
5098 vcpu->arch.interrupt.pending = false;
5099
5100 return EMULATE_DONE;
5101}
5102EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
5103
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005104static int handle_emulation_failure(struct kvm_vcpu *vcpu)
5105{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005106 int r = EMULATE_DONE;
5107
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005108 ++vcpu->stat.insn_emulation_fail;
5109 trace_kvm_emulate_insn_failed(vcpu);
Nadav Amita2b9e6c2014-09-17 02:50:50 +03005110 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005111 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5112 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5113 vcpu->run->internal.ndata = 0;
5114 r = EMULATE_FAIL;
5115 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005116 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005117
5118 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005119}
5120
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005121static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03005122 bool write_fault_to_shadow_pgtable,
5123 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03005124{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005125 gpa_t gpa = cr2;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005126 pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005127
Gleb Natapov991eebf2013-04-11 12:10:51 +03005128 if (emulation_type & EMULTYPE_NO_REEXECUTE)
5129 return false;
5130
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005131 if (!vcpu->arch.mmu.direct_map) {
5132 /*
5133 * Write permission should be allowed since only
5134 * write access need to be emulated.
5135 */
5136 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03005137
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005138 /*
5139 * If the mapping is invalid in guest, let cpu retry
5140 * it to generate fault.
5141 */
5142 if (gpa == UNMAPPED_GVA)
5143 return true;
5144 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005145
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005146 /*
5147 * Do not retry the unhandleable instruction if it faults on the
5148 * readonly host memory, otherwise it will goto a infinite loop:
5149 * retry instruction -> write #PF -> emulation fail -> retry
5150 * instruction -> ...
5151 */
5152 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005153
5154 /*
5155 * If the instruction failed on the error pfn, it can not be fixed,
5156 * report the error to userspace.
5157 */
5158 if (is_error_noslot_pfn(pfn))
5159 return false;
5160
5161 kvm_release_pfn_clean(pfn);
5162
5163 /* The instructions are well-emulated on direct mmu. */
5164 if (vcpu->arch.mmu.direct_map) {
5165 unsigned int indirect_shadow_pages;
5166
5167 spin_lock(&vcpu->kvm->mmu_lock);
5168 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5169 spin_unlock(&vcpu->kvm->mmu_lock);
5170
5171 if (indirect_shadow_pages)
5172 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5173
Gleb Natapova6f177e2010-07-08 12:41:12 +03005174 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005175 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005176
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005177 /*
5178 * if emulation was due to access to shadowed page table
5179 * and it failed try to unshadow page and re-enter the
5180 * guest to let CPU execute the instruction.
5181 */
5182 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005183
5184 /*
5185 * If the access faults on its page table, it can not
5186 * be fixed by unprotecting shadow page and it should
5187 * be reported to userspace.
5188 */
5189 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005190}
5191
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005192static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5193 unsigned long cr2, int emulation_type)
5194{
5195 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5196 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5197
5198 last_retry_eip = vcpu->arch.last_retry_eip;
5199 last_retry_addr = vcpu->arch.last_retry_addr;
5200
5201 /*
5202 * If the emulation is caused by #PF and it is non-page_table
5203 * writing instruction, it means the VM-EXIT is caused by shadow
5204 * page protected, we can zap the shadow page and retry this
5205 * instruction directly.
5206 *
5207 * Note: if the guest uses a non-page-table modifying instruction
5208 * on the PDE that points to the instruction, then we will unmap
5209 * the instruction and go to an infinite loop. So, we cache the
5210 * last retried eip and the last fault address, if we meet the eip
5211 * and the address again, we can break out of the potential infinite
5212 * loop.
5213 */
5214 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5215
5216 if (!(emulation_type & EMULTYPE_RETRY))
5217 return false;
5218
5219 if (x86_page_table_writing_insn(ctxt))
5220 return false;
5221
5222 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5223 return false;
5224
5225 vcpu->arch.last_retry_eip = ctxt->eip;
5226 vcpu->arch.last_retry_addr = cr2;
5227
5228 if (!vcpu->arch.mmu.direct_map)
5229 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5230
Xiao Guangrong22368022013-01-13 23:44:12 +08005231 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005232
5233 return true;
5234}
5235
Gleb Natapov716d51a2012-09-03 15:24:26 +03005236static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5237static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5238
Paolo Bonzini64d60672015-05-07 11:36:11 +02005239static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02005240{
Paolo Bonzini64d60672015-05-07 11:36:11 +02005241 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005242 /* This is a good place to trace that we are exiting SMM. */
5243 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
5244
Paolo Bonzini64d60672015-05-07 11:36:11 +02005245 if (unlikely(vcpu->arch.smi_pending)) {
5246 kvm_make_request(KVM_REQ_SMI, vcpu);
5247 vcpu->arch.smi_pending = 0;
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02005248 } else {
5249 /* Process a latched INIT, if any. */
5250 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005251 }
5252 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02005253
5254 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005255}
5256
5257static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
5258{
5259 unsigned changed = vcpu->arch.hflags ^ emul_flags;
5260
Paolo Bonzinia5845392015-04-01 18:18:53 +02005261 vcpu->arch.hflags = emul_flags;
Paolo Bonzini64d60672015-05-07 11:36:11 +02005262
5263 if (changed & HF_SMM_MASK)
5264 kvm_smm_changed(vcpu);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005265}
5266
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005267static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
5268 unsigned long *db)
5269{
5270 u32 dr6 = 0;
5271 int i;
5272 u32 enable, rwlen;
5273
5274 enable = dr7;
5275 rwlen = dr7 >> 16;
5276 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
5277 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
5278 dr6 |= (1 << i);
5279 return dr6;
5280}
5281
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005282static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005283{
5284 struct kvm_run *kvm_run = vcpu->run;
5285
5286 /*
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005287 * rflags is the old, "raw" value of the flags. The new value has
5288 * not been saved yet.
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005289 *
5290 * This is correct even for TF set by the guest, because "the
5291 * processor will not generate this exception after the instruction
5292 * that sets the TF flag".
5293 */
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005294 if (unlikely(rflags & X86_EFLAGS_TF)) {
5295 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005296 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5297 DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005298 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5299 kvm_run->debug.arch.exception = DB_VECTOR;
5300 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5301 *r = EMULATE_USER_EXIT;
5302 } else {
5303 vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5304 /*
5305 * "Certain debug exceptions may clear bit 0-3. The
5306 * remaining contents of the DR6 register are never
5307 * cleared by the processor".
5308 */
5309 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005310 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005311 kvm_queue_exception(vcpu, DB_VECTOR);
5312 }
5313 }
5314}
5315
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005316static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
5317{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005318 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
5319 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005320 struct kvm_run *kvm_run = vcpu->run;
5321 unsigned long eip = kvm_get_linear_rip(vcpu);
5322 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005323 vcpu->arch.guest_debug_dr7,
5324 vcpu->arch.eff_db);
5325
5326 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005327 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02005328 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005329 kvm_run->debug.arch.exception = DB_VECTOR;
5330 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5331 *r = EMULATE_USER_EXIT;
5332 return true;
5333 }
5334 }
5335
Nadav Amit4161a562014-07-17 01:19:31 +03005336 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
5337 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005338 unsigned long eip = kvm_get_linear_rip(vcpu);
5339 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005340 vcpu->arch.dr7,
5341 vcpu->arch.db);
5342
5343 if (dr6 != 0) {
5344 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005345 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005346 kvm_queue_exception(vcpu, DB_VECTOR);
5347 *r = EMULATE_DONE;
5348 return true;
5349 }
5350 }
5351
5352 return false;
5353}
5354
Andre Przywara51d8b662010-12-21 11:12:02 +01005355int x86_emulate_instruction(struct kvm_vcpu *vcpu,
5356 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01005357 int emulation_type,
5358 void *insn,
5359 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005360{
Gleb Natapov95cb2292010-04-28 19:15:43 +03005361 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005362 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005363 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005364 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005365
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005366 /*
5367 * Clear write_fault_to_shadow_pgtable here to ensure it is
5368 * never reused.
5369 */
5370 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03005371 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03005372
Sheng Yang571008d2008-01-02 14:49:22 +08005373 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005374 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005375
5376 /*
5377 * We will reenter on the same instruction since
5378 * we do not set complete_userspace_io. This does not
5379 * handle watchpoints yet, those would be handled in
5380 * the emulate_ops.
5381 */
5382 if (kvm_vcpu_check_breakpoint(vcpu, &r))
5383 return r;
5384
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005385 ctxt->interruptibility = 0;
5386 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005387 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005388 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005389
Borislav Petkovb51e9742013-09-22 16:44:52 +02005390 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02005391
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005392 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02005393
Avi Kivitye46479f2010-04-11 13:05:16 +03005394 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02005395 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005396 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02005397 if (emulation_type & EMULTYPE_TRAP_UD)
5398 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03005399 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5400 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005401 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005402 if (emulation_type & EMULTYPE_SKIP)
5403 return EMULATE_FAIL;
5404 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005405 }
5406 }
5407
Gleb Natapovba8afb62009-04-12 13:36:57 +03005408 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005409 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03005410 if (ctxt->eflags & X86_EFLAGS_RF)
5411 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03005412 return EMULATE_DONE;
5413 }
5414
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005415 if (retry_instruction(ctxt, cr2, emulation_type))
5416 return EMULATE_DONE;
5417
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005418 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005419 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005420 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
5421 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005422 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005423 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005424
Gleb Natapov5cd21912010-03-18 15:20:26 +02005425restart:
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005426 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005427
Joerg Roedel775fde82011-04-04 12:39:24 +02005428 if (r == EMULATION_INTERCEPTED)
5429 return EMULATE_DONE;
5430
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005431 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03005432 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5433 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005434 return EMULATE_DONE;
5435
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005436 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005437 }
5438
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005439 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005440 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005441 if (inject_emulated_exception(vcpu))
5442 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005443 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02005444 if (!vcpu->arch.pio.in) {
5445 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03005446 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02005447 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005448 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005449 vcpu->arch.complete_userspace_io = complete_emulated_pio;
5450 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005451 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005452 } else if (vcpu->mmio_needed) {
5453 if (!vcpu->mmio_is_write)
5454 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005455 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005456 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005457 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03005458 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005459 else
5460 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03005461
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005462 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005463 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005464 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005465 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005466 if (vcpu->arch.hflags != ctxt->emul_flags)
5467 kvm_set_hflags(vcpu, ctxt->emul_flags);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005468 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005469 if (r == EMULATE_DONE)
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005470 kvm_vcpu_check_singlestep(vcpu, rflags, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02005471 if (!ctxt->have_exception ||
5472 exception_type(ctxt->exception.vector) == EXCPT_TRAP)
5473 __kvm_set_rflags(vcpu, ctxt->eflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005474
5475 /*
5476 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
5477 * do nothing, and it will be requested again as soon as
5478 * the shadow expires. But we still need to check here,
5479 * because POPF has no interrupt shadow.
5480 */
5481 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
5482 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005483 } else
5484 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03005485
Gleb Natapove85d28f2010-07-29 15:11:52 +03005486 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005487}
Andre Przywara51d8b662010-12-21 11:12:02 +01005488EXPORT_SYMBOL_GPL(x86_emulate_instruction);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005489
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005490int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01005491{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005492 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005493 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
5494 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005495 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02005496 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02005497 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01005498}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005499EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01005500
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005501static void tsc_bad(void *info)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005502{
Tejun Heo0a3aee02010-12-18 16:28:55 +01005503 __this_cpu_write(cpu_tsc_khz, 0);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005504}
5505
5506static void tsc_khz_changed(void *data)
5507{
5508 struct cpufreq_freqs *freq = data;
5509 unsigned long khz = 0;
5510
5511 if (data)
5512 khz = freq->new;
5513 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5514 khz = cpufreq_quick_get(raw_smp_processor_id());
5515 if (!khz)
5516 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01005517 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005518}
5519
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005520static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
5521 void *data)
5522{
5523 struct cpufreq_freqs *freq = data;
5524 struct kvm *kvm;
5525 struct kvm_vcpu *vcpu;
5526 int i, send_ipi = 0;
5527
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005528 /*
5529 * We allow guests to temporarily run on slowing clocks,
5530 * provided we notify them after, or to run on accelerating
5531 * clocks, provided we notify them before. Thus time never
5532 * goes backwards.
5533 *
5534 * However, we have a problem. We can't atomically update
5535 * the frequency of a given CPU from this function; it is
5536 * merely a notifier, which can be called from any CPU.
5537 * Changing the TSC frequency at arbitrary points in time
5538 * requires a recomputation of local variables related to
5539 * the TSC for each VCPU. We must flag these local variables
5540 * to be updated and be sure the update takes place with the
5541 * new frequency before any guests proceed.
5542 *
5543 * Unfortunately, the combination of hotplug CPU and frequency
5544 * change creates an intractable locking scenario; the order
5545 * of when these callouts happen is undefined with respect to
5546 * CPU hotplug, and they can race with each other. As such,
5547 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
5548 * undefined; you can actually have a CPU frequency change take
5549 * place in between the computation of X and the setting of the
5550 * variable. To protect against this problem, all updates of
5551 * the per_cpu tsc_khz variable are done in an interrupt
5552 * protected IPI, and all callers wishing to update the value
5553 * must wait for a synchronous IPI to complete (which is trivial
5554 * if the caller is on the CPU already). This establishes the
5555 * necessary total order on variable updates.
5556 *
5557 * Note that because a guest time update may take place
5558 * anytime after the setting of the VCPU's request bit, the
5559 * correct TSC value must be set before the request. However,
5560 * to ensure the update actually makes it to any guest which
5561 * starts running in hardware virtualization between the set
5562 * and the acquisition of the spinlock, we must also ping the
5563 * CPU after setting the request bit.
5564 *
5565 */
5566
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005567 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
5568 return 0;
5569 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
5570 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005571
5572 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005573
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005574 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005575 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005576 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005577 if (vcpu->cpu != freq->cpu)
5578 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10005579 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005580 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005581 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005582 }
5583 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005584 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005585
5586 if (freq->old < freq->new && send_ipi) {
5587 /*
5588 * We upscale the frequency. Must make the guest
5589 * doesn't see old kvmclock values while running with
5590 * the new frequency, otherwise we risk the guest sees
5591 * time go backwards.
5592 *
5593 * In case we update the frequency for another cpu
5594 * (which might be in guest context) send an interrupt
5595 * to kick the cpu out of guest context. Next time
5596 * guest context is entered kvmclock will be updated,
5597 * so the guest will not see stale values.
5598 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005599 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005600 }
5601 return 0;
5602}
5603
5604static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005605 .notifier_call = kvmclock_cpufreq_notifier
5606};
5607
5608static int kvmclock_cpu_notifier(struct notifier_block *nfb,
5609 unsigned long action, void *hcpu)
5610{
5611 unsigned int cpu = (unsigned long)hcpu;
5612
5613 switch (action) {
5614 case CPU_ONLINE:
5615 case CPU_DOWN_FAILED:
5616 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
5617 break;
5618 case CPU_DOWN_PREPARE:
5619 smp_call_function_single(cpu, tsc_bad, NULL, 1);
5620 break;
5621 }
5622 return NOTIFY_OK;
5623}
5624
5625static struct notifier_block kvmclock_cpu_notifier_block = {
5626 .notifier_call = kvmclock_cpu_notifier,
5627 .priority = -INT_MAX
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005628};
5629
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005630static void kvm_timer_init(void)
5631{
5632 int cpu;
5633
Zachary Amsdenc2855452010-09-18 14:38:15 -10005634 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305635
5636 cpu_notifier_register_begin();
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005637 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10005638#ifdef CONFIG_CPU_FREQ
5639 struct cpufreq_policy policy;
5640 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02005641 cpu = get_cpu();
5642 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10005643 if (policy.cpuinfo.max_freq)
5644 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02005645 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10005646#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005647 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
5648 CPUFREQ_TRANSITION_NOTIFIER);
5649 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10005650 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005651 for_each_online_cpu(cpu)
5652 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305653
5654 __register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
5655 cpu_notifier_register_done();
5656
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005657}
5658
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005659static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
5660
Gleb Natapovf5132b02011-11-10 14:57:22 +02005661int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005662{
Alex,Shi086c9852011-10-20 15:34:01 +08005663 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005664}
5665
5666static int kvm_is_user_mode(void)
5667{
5668 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005669
Alex,Shi086c9852011-10-20 15:34:01 +08005670 if (__this_cpu_read(current_vcpu))
5671 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005672
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005673 return user_mode != 0;
5674}
5675
5676static unsigned long kvm_get_guest_ip(void)
5677{
5678 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005679
Alex,Shi086c9852011-10-20 15:34:01 +08005680 if (__this_cpu_read(current_vcpu))
5681 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005682
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005683 return ip;
5684}
5685
5686static struct perf_guest_info_callbacks kvm_guest_cbs = {
5687 .is_in_guest = kvm_is_in_guest,
5688 .is_user_mode = kvm_is_user_mode,
5689 .get_guest_ip = kvm_get_guest_ip,
5690};
5691
5692void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
5693{
Alex,Shi086c9852011-10-20 15:34:01 +08005694 __this_cpu_write(current_vcpu, vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005695}
5696EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
5697
5698void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
5699{
Alex,Shi086c9852011-10-20 15:34:01 +08005700 __this_cpu_write(current_vcpu, NULL);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005701}
5702EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
5703
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005704static void kvm_set_mmio_spte_mask(void)
5705{
5706 u64 mask;
5707 int maxphyaddr = boot_cpu_data.x86_phys_bits;
5708
5709 /*
5710 * Set the reserved bits and the present bit of an paging-structure
5711 * entry to generate page fault with PFER.RSV = 1.
5712 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005713 /* Mask the reserved physical address bits. */
Tiejun Chend1431482014-09-01 18:44:04 +08005714 mask = rsvd_bits(maxphyaddr, 51);
Xiao Guangrong885032b2013-06-07 16:51:23 +08005715
5716 /* Bit 62 is always reserved for 32bit host. */
5717 mask |= 0x3ull << 62;
5718
5719 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005720 mask |= 1ull;
5721
5722#ifdef CONFIG_X86_64
5723 /*
5724 * If reserved bit is not supported, clear the present bit to disable
5725 * mmio page fault.
5726 */
5727 if (maxphyaddr == 52)
5728 mask &= ~1ull;
5729#endif
5730
5731 kvm_mmu_set_mmio_spte_mask(mask);
5732}
5733
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005734#ifdef CONFIG_X86_64
5735static void pvclock_gtod_update_fn(struct work_struct *work)
5736{
Marcelo Tosattid8281992012-11-27 23:29:01 -02005737 struct kvm *kvm;
5738
5739 struct kvm_vcpu *vcpu;
5740 int i;
5741
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005742 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005743 list_for_each_entry(kvm, &vm_list, vm_list)
5744 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08005745 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005746 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005747 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005748}
5749
5750static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
5751
5752/*
5753 * Notification about pvclock gtod data update.
5754 */
5755static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
5756 void *priv)
5757{
5758 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
5759 struct timekeeper *tk = priv;
5760
5761 update_pvclock_gtod(tk);
5762
5763 /* disable master clock if host does not trust, or does not
5764 * use, TSC clocksource
5765 */
5766 if (gtod->clock.vclock_mode != VCLOCK_TSC &&
5767 atomic_read(&kvm_guest_has_master_clock) != 0)
5768 queue_work(system_long_wq, &pvclock_gtod_work);
5769
5770 return 0;
5771}
5772
5773static struct notifier_block pvclock_gtod_notifier = {
5774 .notifier_call = pvclock_gtod_notify,
5775};
5776#endif
5777
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005778int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02005779{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005780 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02005781 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005782
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005783 if (kvm_x86_ops) {
5784 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005785 r = -EEXIST;
5786 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005787 }
5788
5789 if (!ops->cpu_has_kvm_support()) {
5790 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005791 r = -EOPNOTSUPP;
5792 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005793 }
5794 if (ops->disabled_by_bios()) {
5795 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005796 r = -EOPNOTSUPP;
5797 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005798 }
5799
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005800 r = -ENOMEM;
5801 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
5802 if (!shared_msrs) {
5803 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
5804 goto out;
5805 }
5806
Avi Kivity97db56c2008-01-13 13:23:56 +02005807 r = kvm_mmu_module_init();
5808 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005809 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02005810
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005811 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02005812
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005813 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01005814
Sheng Yang7b523452008-04-25 21:13:50 +08005815 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005816 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005817
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005818 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005819
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005820 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5821
Dexuan Cui2acf9232010-06-10 11:27:12 +08005822 if (cpu_has_xsave)
5823 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
5824
Gleb Natapovc5cc4212012-08-05 15:58:30 +03005825 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005826#ifdef CONFIG_X86_64
5827 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
5828#endif
5829
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005830 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005831
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005832out_free_percpu:
5833 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005834out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005835 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02005836}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005837
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005838void kvm_arch_exit(void)
5839{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005840 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5841
Jan Kiszka888d2562009-04-17 19:24:58 +02005842 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5843 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
5844 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005845 unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005846#ifdef CONFIG_X86_64
5847 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
5848#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005849 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005850 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005851 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005852}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005853
Joel Schopp5cb56052015-03-02 13:43:31 -06005854int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005855{
5856 ++vcpu->stat.halt_exits;
5857 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03005858 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005859 return 1;
5860 } else {
5861 vcpu->run->exit_reason = KVM_EXIT_HLT;
5862 return 0;
5863 }
5864}
Joel Schopp5cb56052015-03-02 13:43:31 -06005865EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
5866
5867int kvm_emulate_halt(struct kvm_vcpu *vcpu)
5868{
5869 kvm_x86_ops->skip_emulated_instruction(vcpu);
5870 return kvm_vcpu_halt(vcpu);
5871}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005872EXPORT_SYMBOL_GPL(kvm_emulate_halt);
5873
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005874int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
5875{
5876 u64 param, ingpa, outgpa, ret;
5877 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
5878 bool fast, longmode;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005879
5880 /*
5881 * hypercall generates UD from non zero cpl and real mode
5882 * per HYPER-V spec
5883 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02005884 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005885 kvm_queue_exception(vcpu, UD_VECTOR);
5886 return 0;
5887 }
5888
Nadav Amita449c7a2014-06-18 17:19:24 +03005889 longmode = is_64_bit_mode(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005890
5891 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02005892 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
5893 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
5894 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
5895 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
5896 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
5897 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005898 }
5899#ifdef CONFIG_X86_64
5900 else {
5901 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
5902 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
5903 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
5904 }
5905#endif
5906
5907 code = param & 0xffff;
5908 fast = (param >> 16) & 0x1;
5909 rep_cnt = (param >> 32) & 0xfff;
5910 rep_idx = (param >> 48) & 0xfff;
5911
5912 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
5913
Gleb Natapovc25bc162010-01-17 15:51:24 +02005914 switch (code) {
5915 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
5916 kvm_vcpu_on_spin(vcpu);
5917 break;
5918 default:
5919 res = HV_STATUS_INVALID_HYPERCALL_CODE;
5920 break;
5921 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005922
5923 ret = res | (((u64)rep_done & 0xfff) << 32);
5924 if (longmode) {
5925 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
5926 } else {
5927 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
5928 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
5929 }
5930
5931 return 1;
5932}
5933
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305934/*
5935 * kvm_pv_kick_cpu_op: Kick a vcpu.
5936 *
5937 * @apicid - apicid of vcpu to be kicked.
5938 */
5939static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
5940{
Raghavendra K T24d21662013-08-26 14:18:35 +05305941 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305942
Raghavendra K T24d21662013-08-26 14:18:35 +05305943 lapic_irq.shorthand = 0;
5944 lapic_irq.dest_mode = 0;
5945 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06005946 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305947
Raghavendra K T24d21662013-08-26 14:18:35 +05305948 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08005949 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305950}
5951
Hollis Blanchard8776e512007-10-31 17:24:24 -05005952int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
5953{
5954 unsigned long nr, a0, a1, a2, a3, ret;
Nadav Amita449c7a2014-06-18 17:19:24 +03005955 int op_64_bit, r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005956
Joel Schopp5cb56052015-03-02 13:43:31 -06005957 kvm_x86_ops->skip_emulated_instruction(vcpu);
5958
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005959 if (kvm_hv_hypercall_enabled(vcpu->kvm))
5960 return kvm_hv_hypercall(vcpu);
5961
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005962 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
5963 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
5964 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
5965 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
5966 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005967
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005968 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005969
Nadav Amita449c7a2014-06-18 17:19:24 +03005970 op_64_bit = is_64_bit_mode(vcpu);
5971 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05005972 nr &= 0xFFFFFFFF;
5973 a0 &= 0xFFFFFFFF;
5974 a1 &= 0xFFFFFFFF;
5975 a2 &= 0xFFFFFFFF;
5976 a3 &= 0xFFFFFFFF;
5977 }
5978
Jan Kiszka07708c42009-08-03 18:43:28 +02005979 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
5980 ret = -KVM_EPERM;
5981 goto out;
5982 }
5983
Hollis Blanchard8776e512007-10-31 17:24:24 -05005984 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02005985 case KVM_HC_VAPIC_POLL_IRQ:
5986 ret = 0;
5987 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305988 case KVM_HC_KICK_CPU:
5989 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
5990 ret = 0;
5991 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005992 default:
5993 ret = -KVM_ENOSYS;
5994 break;
5995 }
Jan Kiszka07708c42009-08-03 18:43:28 +02005996out:
Nadav Amita449c7a2014-06-18 17:19:24 +03005997 if (!op_64_bit)
5998 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005999 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05306000 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05006001 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05006002}
6003EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
6004
Jan Kiszkab6785de2012-09-20 07:43:17 +02006005static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05006006{
Avi Kivityd6aa1002011-04-20 15:47:13 +03006007 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006008 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006009 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006010
Hollis Blanchard8776e512007-10-31 17:24:24 -05006011 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006012
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006013 return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006014}
6015
Hollis Blanchardd0752062007-10-31 17:24:25 -05006016/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006017 * Check if userspace requested an interrupt window, and that the
6018 * interrupt window is open.
6019 *
6020 * No need to exit to userspace if we already have an interrupt queued.
6021 */
Avi Kivity851ba692009-08-24 11:10:17 +03006022static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006023{
Gleb Natapov80618232009-04-21 17:44:56 +03006024 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03006025 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03006026 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006027}
6028
Avi Kivity851ba692009-08-24 11:10:17 +03006029static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006030{
Avi Kivity851ba692009-08-24 11:10:17 +03006031 struct kvm_run *kvm_run = vcpu->run;
6032
Jan Kiszka91586a32009-10-05 13:07:21 +02006033 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02006034 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006035 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006036 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01006037 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006038 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01006039 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006040 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03006041 kvm_arch_interrupt_allowed(vcpu) &&
6042 !kvm_cpu_has_interrupt(vcpu) &&
6043 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006044}
6045
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006046static void update_cr8_intercept(struct kvm_vcpu *vcpu)
6047{
6048 int max_irr, tpr;
6049
6050 if (!kvm_x86_ops->update_cr8_intercept)
6051 return;
6052
Avi Kivity88c808f2009-08-17 22:49:40 +03006053 if (!vcpu->arch.apic)
6054 return;
6055
Gleb Natapov8db3baa2009-05-11 13:35:54 +03006056 if (!vcpu->arch.apic->vapic_addr)
6057 max_irr = kvm_lapic_find_highest_irr(vcpu);
6058 else
6059 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006060
6061 if (max_irr != -1)
6062 max_irr >>= 4;
6063
6064 tpr = kvm_lapic_get_cr8(vcpu);
6065
6066 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
6067}
6068
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006069static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006070{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006071 int r;
6072
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006073 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006074 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02006075 trace_kvm_inj_exception(vcpu->arch.exception.nr,
6076 vcpu->arch.exception.has_error_code,
6077 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03006078
6079 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
6080 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
6081 X86_EFLAGS_RF);
6082
Nadav Amit6bdf0662014-09-30 20:49:14 +03006083 if (vcpu->arch.exception.nr == DB_VECTOR &&
6084 (vcpu->arch.dr7 & DR7_GD)) {
6085 vcpu->arch.dr7 &= ~DR7_GD;
6086 kvm_update_dr7(vcpu);
6087 }
6088
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006089 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
6090 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02006091 vcpu->arch.exception.error_code,
6092 vcpu->arch.exception.reinject);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006093 return 0;
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006094 }
6095
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006096 if (vcpu->arch.nmi_injected) {
6097 kvm_x86_ops->set_nmi(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006098 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006099 }
6100
6101 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006102 kvm_x86_ops->set_irq(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006103 return 0;
6104 }
6105
6106 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6107 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6108 if (r != 0)
6109 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006110 }
6111
6112 /* try to inject new event if pending */
6113 if (vcpu->arch.nmi_pending) {
6114 if (kvm_x86_ops->nmi_allowed(vcpu)) {
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006115 --vcpu->arch.nmi_pending;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006116 vcpu->arch.nmi_injected = true;
6117 kvm_x86_ops->set_nmi(vcpu);
6118 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08006119 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04006120 /*
6121 * Because interrupts can be injected asynchronously, we are
6122 * calling check_nested_events again here to avoid a race condition.
6123 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
6124 * proposal and current concerns. Perhaps we should be setting
6125 * KVM_REQ_EVENT only on certain events and not unconditionally?
6126 */
6127 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6128 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6129 if (r != 0)
6130 return r;
6131 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006132 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006133 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
6134 false);
6135 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006136 }
6137 }
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006138 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006139}
6140
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006141static void process_nmi(struct kvm_vcpu *vcpu)
6142{
6143 unsigned limit = 2;
6144
6145 /*
6146 * x86 is limited to one NMI running, and one NMI pending after it.
6147 * If an NMI is already in progress, limit further NMIs to just one.
6148 * Otherwise, allow two (and we'll inject the first one immediately).
6149 */
6150 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
6151 limit = 1;
6152
6153 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
6154 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
6155 kvm_make_request(KVM_REQ_EVENT, vcpu);
6156}
6157
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006158#define put_smstate(type, buf, offset, val) \
6159 *(type *)((buf) + (offset) - 0x7e00) = val
6160
6161static u32 process_smi_get_segment_flags(struct kvm_segment *seg)
6162{
6163 u32 flags = 0;
6164 flags |= seg->g << 23;
6165 flags |= seg->db << 22;
6166 flags |= seg->l << 21;
6167 flags |= seg->avl << 20;
6168 flags |= seg->present << 15;
6169 flags |= seg->dpl << 13;
6170 flags |= seg->s << 12;
6171 flags |= seg->type << 8;
6172 return flags;
6173}
6174
6175static void process_smi_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
6176{
6177 struct kvm_segment seg;
6178 int offset;
6179
6180 kvm_get_segment(vcpu, &seg, n);
6181 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
6182
6183 if (n < 3)
6184 offset = 0x7f84 + n * 12;
6185 else
6186 offset = 0x7f2c + (n - 3) * 12;
6187
6188 put_smstate(u32, buf, offset + 8, seg.base);
6189 put_smstate(u32, buf, offset + 4, seg.limit);
6190 put_smstate(u32, buf, offset, process_smi_get_segment_flags(&seg));
6191}
6192
6193static void process_smi_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
6194{
6195 struct kvm_segment seg;
6196 int offset;
6197 u16 flags;
6198
6199 kvm_get_segment(vcpu, &seg, n);
6200 offset = 0x7e00 + n * 16;
6201
6202 flags = process_smi_get_segment_flags(&seg) >> 8;
6203 put_smstate(u16, buf, offset, seg.selector);
6204 put_smstate(u16, buf, offset + 2, flags);
6205 put_smstate(u32, buf, offset + 4, seg.limit);
6206 put_smstate(u64, buf, offset + 8, seg.base);
6207}
6208
6209static void process_smi_save_state_32(struct kvm_vcpu *vcpu, char *buf)
6210{
6211 struct desc_ptr dt;
6212 struct kvm_segment seg;
6213 unsigned long val;
6214 int i;
6215
6216 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
6217 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
6218 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
6219 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
6220
6221 for (i = 0; i < 8; i++)
6222 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
6223
6224 kvm_get_dr(vcpu, 6, &val);
6225 put_smstate(u32, buf, 0x7fcc, (u32)val);
6226 kvm_get_dr(vcpu, 7, &val);
6227 put_smstate(u32, buf, 0x7fc8, (u32)val);
6228
6229 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6230 put_smstate(u32, buf, 0x7fc4, seg.selector);
6231 put_smstate(u32, buf, 0x7f64, seg.base);
6232 put_smstate(u32, buf, 0x7f60, seg.limit);
6233 put_smstate(u32, buf, 0x7f5c, process_smi_get_segment_flags(&seg));
6234
6235 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6236 put_smstate(u32, buf, 0x7fc0, seg.selector);
6237 put_smstate(u32, buf, 0x7f80, seg.base);
6238 put_smstate(u32, buf, 0x7f7c, seg.limit);
6239 put_smstate(u32, buf, 0x7f78, process_smi_get_segment_flags(&seg));
6240
6241 kvm_x86_ops->get_gdt(vcpu, &dt);
6242 put_smstate(u32, buf, 0x7f74, dt.address);
6243 put_smstate(u32, buf, 0x7f70, dt.size);
6244
6245 kvm_x86_ops->get_idt(vcpu, &dt);
6246 put_smstate(u32, buf, 0x7f58, dt.address);
6247 put_smstate(u32, buf, 0x7f54, dt.size);
6248
6249 for (i = 0; i < 6; i++)
6250 process_smi_save_seg_32(vcpu, buf, i);
6251
6252 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
6253
6254 /* revision id */
6255 put_smstate(u32, buf, 0x7efc, 0x00020000);
6256 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
6257}
6258
6259static void process_smi_save_state_64(struct kvm_vcpu *vcpu, char *buf)
6260{
6261#ifdef CONFIG_X86_64
6262 struct desc_ptr dt;
6263 struct kvm_segment seg;
6264 unsigned long val;
6265 int i;
6266
6267 for (i = 0; i < 16; i++)
6268 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
6269
6270 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
6271 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
6272
6273 kvm_get_dr(vcpu, 6, &val);
6274 put_smstate(u64, buf, 0x7f68, val);
6275 kvm_get_dr(vcpu, 7, &val);
6276 put_smstate(u64, buf, 0x7f60, val);
6277
6278 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
6279 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
6280 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
6281
6282 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
6283
6284 /* revision id */
6285 put_smstate(u32, buf, 0x7efc, 0x00020064);
6286
6287 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
6288
6289 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6290 put_smstate(u16, buf, 0x7e90, seg.selector);
6291 put_smstate(u16, buf, 0x7e92, process_smi_get_segment_flags(&seg) >> 8);
6292 put_smstate(u32, buf, 0x7e94, seg.limit);
6293 put_smstate(u64, buf, 0x7e98, seg.base);
6294
6295 kvm_x86_ops->get_idt(vcpu, &dt);
6296 put_smstate(u32, buf, 0x7e84, dt.size);
6297 put_smstate(u64, buf, 0x7e88, dt.address);
6298
6299 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6300 put_smstate(u16, buf, 0x7e70, seg.selector);
6301 put_smstate(u16, buf, 0x7e72, process_smi_get_segment_flags(&seg) >> 8);
6302 put_smstate(u32, buf, 0x7e74, seg.limit);
6303 put_smstate(u64, buf, 0x7e78, seg.base);
6304
6305 kvm_x86_ops->get_gdt(vcpu, &dt);
6306 put_smstate(u32, buf, 0x7e64, dt.size);
6307 put_smstate(u64, buf, 0x7e68, dt.address);
6308
6309 for (i = 0; i < 6; i++)
6310 process_smi_save_seg_64(vcpu, buf, i);
6311#else
6312 WARN_ON_ONCE(1);
6313#endif
6314}
6315
Paolo Bonzini64d60672015-05-07 11:36:11 +02006316static void process_smi(struct kvm_vcpu *vcpu)
6317{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006318 struct kvm_segment cs, ds;
6319 char buf[512];
6320 u32 cr0;
6321
Paolo Bonzini64d60672015-05-07 11:36:11 +02006322 if (is_smm(vcpu)) {
6323 vcpu->arch.smi_pending = true;
6324 return;
6325 }
6326
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006327 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
6328 vcpu->arch.hflags |= HF_SMM_MASK;
6329 memset(buf, 0, 512);
6330 if (guest_cpuid_has_longmode(vcpu))
6331 process_smi_save_state_64(vcpu, buf);
6332 else
6333 process_smi_save_state_32(vcpu, buf);
6334
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006335 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006336
6337 if (kvm_x86_ops->get_nmi_mask(vcpu))
6338 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
6339 else
6340 kvm_x86_ops->set_nmi_mask(vcpu, true);
6341
6342 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6343 kvm_rip_write(vcpu, 0x8000);
6344
6345 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
6346 kvm_x86_ops->set_cr0(vcpu, cr0);
6347 vcpu->arch.cr0 = cr0;
6348
6349 kvm_x86_ops->set_cr4(vcpu, 0);
6350
6351 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
6352
6353 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
6354 cs.base = vcpu->arch.smbase;
6355
6356 ds.selector = 0;
6357 ds.base = 0;
6358
6359 cs.limit = ds.limit = 0xffffffff;
6360 cs.type = ds.type = 0x3;
6361 cs.dpl = ds.dpl = 0;
6362 cs.db = ds.db = 0;
6363 cs.s = ds.s = 1;
6364 cs.l = ds.l = 0;
6365 cs.g = ds.g = 1;
6366 cs.avl = ds.avl = 0;
6367 cs.present = ds.present = 1;
6368 cs.unusable = ds.unusable = 0;
6369 cs.padding = ds.padding = 0;
6370
6371 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
6372 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
6373 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
6374 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
6375 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
6376 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
6377
6378 if (guest_cpuid_has_longmode(vcpu))
6379 kvm_x86_ops->set_efer(vcpu, 0);
6380
6381 kvm_update_cpuid(vcpu);
6382 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006383}
6384
Yang Zhang3d81bc72013-04-11 19:25:13 +08006385static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006386{
6387 u64 eoi_exit_bitmap[4];
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006388 u32 tmr[8];
Yang Zhangc7c9c562013-01-25 10:18:51 +08006389
Yang Zhang3d81bc72013-04-11 19:25:13 +08006390 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
6391 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006392
6393 memset(eoi_exit_bitmap, 0, 32);
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006394 memset(tmr, 0, 32);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006395
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006396 kvm_ioapic_scan_entry(vcpu, eoi_exit_bitmap, tmr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006397 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangcf9e65b2013-04-11 19:25:14 +08006398 kvm_apic_update_tmr(vcpu, tmr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006399}
6400
Radim Krčmářa70656b2014-09-18 12:38:36 -04006401static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
6402{
6403 ++vcpu->stat.tlb_flush;
6404 kvm_x86_ops->tlb_flush(vcpu);
6405}
6406
Tang Chen4256f432014-09-24 15:57:54 +08006407void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
6408{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006409 struct page *page = NULL;
6410
Paolo Bonzinif439ed22014-10-02 13:53:24 +02006411 if (!irqchip_in_kernel(vcpu->kvm))
6412 return;
6413
Tang Chen4256f432014-09-24 15:57:54 +08006414 if (!kvm_x86_ops->set_apic_access_page_addr)
6415 return;
6416
Tang Chenc24ae0d2014-09-24 15:57:58 +08006417 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02006418 if (is_error_page(page))
6419 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08006420 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
6421
6422 /*
6423 * Do not pin apic access page in memory, the MMU notifier
6424 * will call us again if it is migrated or swapped out.
6425 */
6426 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08006427}
6428EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
6429
Tang Chenfe715572014-09-24 15:57:57 +08006430void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
6431 unsigned long address)
6432{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006433 /*
6434 * The physical address of apic access page is stored in the VMCS.
6435 * Update it when it becomes invalid.
6436 */
6437 if (address == gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT))
6438 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Tang Chenfe715572014-09-24 15:57:57 +08006439}
6440
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006441/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01006442 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006443 * exiting to the userspace. Otherwise, the value will be returned to the
6444 * userspace.
6445 */
Avi Kivity851ba692009-08-24 11:10:17 +03006446static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006447{
6448 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006449 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03006450 vcpu->run->request_interrupt_window;
Jan Kiszka730dca42013-04-28 10:50:52 +02006451 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006452
Avi Kivity3e007502010-06-23 14:26:18 +03006453 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03006454 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05006455 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006456 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03006457 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006458 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
6459 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03006460 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
6461 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10006462 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
6463 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006464 if (unlikely(r))
6465 goto out;
6466 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006467 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03006468 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006469 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Radim Krčmářa70656b2014-09-18 12:38:36 -04006470 kvm_vcpu_flush_tlb(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006471 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006472 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02006473 r = 0;
6474 goto out;
6475 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006476 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006477 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01006478 r = 0;
6479 goto out;
6480 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006481 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02006482 vcpu->fpu_active = 0;
6483 kvm_x86_ops->fpu_deactivate(vcpu);
6484 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006485 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
6486 /* Page is swapped out. Do synthetic halt */
6487 vcpu->arch.apf.halted = true;
6488 r = 1;
6489 goto out;
6490 }
Glauber Costac9aaa892011-07-11 15:28:14 -04006491 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
6492 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006493 if (kvm_check_request(KVM_REQ_SMI, vcpu))
6494 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006495 if (kvm_check_request(KVM_REQ_NMI, vcpu))
6496 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006497 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006498 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006499 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006500 kvm_pmu_deliver_pmi(vcpu);
Yang Zhang3d81bc72013-04-11 19:25:13 +08006501 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
6502 vcpu_scan_ioapic(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08006503 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
6504 kvm_vcpu_reload_apic_access_page(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02006505 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006506
Avi Kivityb463a6f2010-07-20 15:06:17 +03006507 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Jan Kiszka66450a22013-03-13 12:42:34 +01006508 kvm_apic_accept_events(vcpu);
6509 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
6510 r = 1;
6511 goto out;
6512 }
6513
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006514 if (inject_pending_event(vcpu, req_int_win) != 0)
6515 req_immediate_exit = true;
Avi Kivity3842d132010-07-27 12:30:24 +03006516 /* enable NMI/IRQ window open exits if needed */
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006517 else if (vcpu->arch.nmi_pending)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006518 kvm_x86_ops->enable_nmi_window(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006519 else if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006520 kvm_x86_ops->enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006521
Avi Kivity3842d132010-07-27 12:30:24 +03006522 if (kvm_lapic_enabled(vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006523 /*
6524 * Update architecture specific hints for APIC
6525 * virtual interrupt delivery.
6526 */
6527 if (kvm_x86_ops->hwapic_irr_update)
6528 kvm_x86_ops->hwapic_irr_update(vcpu,
6529 kvm_lapic_find_highest_irr(vcpu));
Avi Kivity3842d132010-07-27 12:30:24 +03006530 update_cr8_intercept(vcpu);
6531 kvm_lapic_sync_to_vapic(vcpu);
6532 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006533 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006534
Avi Kivityd8368af2012-05-14 18:07:56 +03006535 r = kvm_mmu_reload(vcpu);
6536 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006537 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03006538 }
6539
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006540 preempt_disable();
6541
6542 kvm_x86_ops->prepare_guest_switch(vcpu);
6543 if (vcpu->fpu_active)
6544 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006545 kvm_load_guest_xcr0(vcpu);
6546
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006547 vcpu->mode = IN_GUEST_MODE;
6548
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006549 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6550
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006551 /* We should set ->mode before check ->requests,
6552 * see the comment in make_all_cpus_request.
6553 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006554 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006555
6556 local_irq_disable();
6557
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006558 if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006559 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006560 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006561 smp_wmb();
6562 local_irq_enable();
6563 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006564 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006565 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006566 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006567 }
6568
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006569 if (req_immediate_exit)
6570 smp_send_reschedule(vcpu->cpu);
6571
Christian Borntraegerccf73aaf2015-04-30 13:43:31 +02006572 __kvm_guest_enter();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006573
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006574 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006575 set_debugreg(0, 7);
6576 set_debugreg(vcpu->arch.eff_db[0], 0);
6577 set_debugreg(vcpu->arch.eff_db[1], 1);
6578 set_debugreg(vcpu->arch.eff_db[2], 2);
6579 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006580 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03006581 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006582 }
6583
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006584 trace_kvm_entry(vcpu->vcpu_id);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05006585 wait_lapic_expire(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006586 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006587
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006588 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006589 * Do this here before restoring debug registers on the host. And
6590 * since we do this before handling the vmexit, a DR access vmexit
6591 * can (a) read the correct value of the debug registers, (b) set
6592 * KVM_DEBUGREG_WONT_EXIT again.
6593 */
6594 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
6595 int i;
6596
6597 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
6598 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
6599 for (i = 0; i < KVM_NR_DB_REGS; i++)
6600 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
6601 }
6602
6603 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006604 * If the guest has used debug registers, at least dr7
6605 * will be disabled while returning to the host.
6606 * If we don't have active breakpoints in the host, we don't
6607 * care about the messed up debug address registers. But if
6608 * we have some of them active, restore the old state.
6609 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01006610 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006611 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006612
Marcelo Tosatti886b4702012-11-27 23:28:58 -02006613 vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu,
Andy Lutomirski4ea16362015-06-25 18:44:07 +02006614 rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10006615
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006616 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03006617 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006618
6619 /* Interrupt is enabled by handle_external_intr() */
6620 kvm_x86_ops->handle_external_intr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006621
6622 ++vcpu->stat.exits;
6623
6624 /*
6625 * We must have an instruction between local_irq_enable() and
6626 * kvm_guest_exit(), so the timer interrupt isn't delayed by
6627 * the interrupt shadow. The stat.exits increment will do nicely.
6628 * But we need to prevent reordering, hence this barrier():
6629 */
6630 barrier();
6631
6632 kvm_guest_exit();
6633
6634 preempt_enable();
6635
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006636 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006637
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006638 /*
6639 * Profile KVM exit RIPs:
6640 */
6641 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006642 unsigned long rip = kvm_rip_read(vcpu);
6643 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006644 }
6645
Zachary Amsdencc578282012-02-03 15:43:50 -02006646 if (unlikely(vcpu->arch.tsc_always_catchup))
6647 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02006648
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03006649 if (vcpu->arch.apic_attention)
6650 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02006651
Avi Kivity851ba692009-08-24 11:10:17 +03006652 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006653 return r;
6654
6655cancel_injection:
6656 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03006657 if (unlikely(vcpu->arch.apic_attention))
6658 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006659out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03006660 return r;
6661}
6662
Paolo Bonzini362c6982015-02-06 12:48:04 +01006663static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
6664{
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01006665 if (!kvm_arch_vcpu_runnable(vcpu)) {
6666 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
6667 kvm_vcpu_block(vcpu);
6668 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
6669 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
6670 return 1;
6671 }
Gleb Natapov09cec752009-03-23 15:11:44 +02006672
Paolo Bonzini362c6982015-02-06 12:48:04 +01006673 kvm_apic_accept_events(vcpu);
6674 switch(vcpu->arch.mp_state) {
6675 case KVM_MP_STATE_HALTED:
6676 vcpu->arch.pv.pv_unhalted = false;
6677 vcpu->arch.mp_state =
6678 KVM_MP_STATE_RUNNABLE;
6679 case KVM_MP_STATE_RUNNABLE:
6680 vcpu->arch.apf.halted = false;
6681 break;
6682 case KVM_MP_STATE_INIT_RECEIVED:
6683 break;
6684 default:
6685 return -EINTR;
6686 break;
6687 }
6688 return 1;
6689}
6690
6691static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03006692{
6693 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006694 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006695
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006696 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006697
Paolo Bonzini362c6982015-02-06 12:48:04 +01006698 for (;;) {
Gleb Natapovaf585b92010-10-14 11:22:46 +02006699 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
6700 !vcpu->arch.apf.halted)
Avi Kivity851ba692009-08-24 11:10:17 +03006701 r = vcpu_enter_guest(vcpu);
Paolo Bonzini362c6982015-02-06 12:48:04 +01006702 else
6703 r = vcpu_block(kvm, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02006704 if (r <= 0)
6705 break;
6706
6707 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
6708 if (kvm_cpu_has_pending_timer(vcpu))
6709 kvm_inject_pending_timer_irqs(vcpu);
6710
Avi Kivity851ba692009-08-24 11:10:17 +03006711 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02006712 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006713 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006714 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006715 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006716 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006717
6718 kvm_check_async_pf_completion(vcpu);
6719
Gleb Natapov09cec752009-03-23 15:11:44 +02006720 if (signal_pending(current)) {
6721 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006722 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006723 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006724 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006725 }
6726 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006727 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09006728 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006729 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006730 }
6731 }
6732
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006733 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006734
6735 return r;
6736}
6737
Gleb Natapov716d51a2012-09-03 15:24:26 +03006738static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
6739{
6740 int r;
6741 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6742 r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
6743 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6744 if (r != EMULATE_DONE)
6745 return 0;
6746 return 1;
6747}
6748
6749static int complete_emulated_pio(struct kvm_vcpu *vcpu)
6750{
6751 BUG_ON(!vcpu->arch.pio.count);
6752
6753 return complete_emulated_io(vcpu);
6754}
6755
Avi Kivityf78146b2012-04-18 19:22:47 +03006756/*
6757 * Implements the following, as a state machine:
6758 *
6759 * read:
6760 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006761 * for each mmio piece in the fragment
6762 * write gpa, len
6763 * exit
6764 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03006765 * execute insn
6766 *
6767 * write:
6768 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006769 * for each mmio piece in the fragment
6770 * write gpa, len
6771 * copy data
6772 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03006773 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03006774static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02006775{
6776 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03006777 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006778 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02006779
Gleb Natapov716d51a2012-09-03 15:24:26 +03006780 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006781
Gleb Natapov716d51a2012-09-03 15:24:26 +03006782 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006783 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
6784 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006785 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006786 memcpy(frag->data, run->mmio.data, len);
6787
6788 if (frag->len <= 8) {
6789 /* Switch to the next fragment. */
6790 frag++;
6791 vcpu->mmio_cur_fragment++;
6792 } else {
6793 /* Go forward to the next mmio piece. */
6794 frag->data += len;
6795 frag->gpa += len;
6796 frag->len -= len;
6797 }
6798
Andrew Honiga08d3b32014-02-27 19:35:14 +01006799 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03006800 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006801
6802 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02006803 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03006804 return 1;
6805 vcpu->mmio_read_completed = 1;
6806 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02006807 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006808
Gleb Natapov716d51a2012-09-03 15:24:26 +03006809 run->exit_reason = KVM_EXIT_MMIO;
6810 run->mmio.phys_addr = frag->gpa;
6811 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006812 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
6813 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006814 run->mmio.is_write = vcpu->mmio_is_write;
6815 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
6816 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02006817}
6818
Gleb Natapov716d51a2012-09-03 15:24:26 +03006819
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006820int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6821{
Ingo Molnarc5bedc62015-04-23 12:49:20 +02006822 struct fpu *fpu = &current->thread.fpu;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006823 int r;
6824 sigset_t sigsaved;
6825
Ingo Molnarc4d72e22015-04-27 07:18:17 +02006826 fpu__activate_curr(fpu);
Avi Kivitye5c30142011-01-11 12:15:54 +02006827
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006828 if (vcpu->sigset_active)
6829 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
6830
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006831 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
6832 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006833 kvm_apic_accept_events(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006834 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006835 r = -EAGAIN;
6836 goto out;
6837 }
6838
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006839 /* re-sync apic's tpr */
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006840 if (!irqchip_in_kernel(vcpu->kvm)) {
6841 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
6842 r = -EINVAL;
6843 goto out;
6844 }
6845 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006846
Gleb Natapov716d51a2012-09-03 15:24:26 +03006847 if (unlikely(vcpu->arch.complete_userspace_io)) {
6848 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
6849 vcpu->arch.complete_userspace_io = NULL;
6850 r = cui(vcpu);
6851 if (r <= 0)
6852 goto out;
6853 } else
6854 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006855
Paolo Bonzini362c6982015-02-06 12:48:04 +01006856 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006857
6858out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03006859 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006860 if (vcpu->sigset_active)
6861 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
6862
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006863 return r;
6864}
6865
6866int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6867{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006868 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
6869 /*
6870 * We are here if userspace calls get_regs() in the middle of
6871 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08006872 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006873 * that usually, but some bad designed PV devices (vmware
6874 * backdoor interface) need this to work
6875 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03006876 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006877 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6878 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006879 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
6880 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
6881 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
6882 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
6883 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
6884 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
6885 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6886 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006887#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006888 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
6889 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
6890 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
6891 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
6892 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
6893 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
6894 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
6895 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006896#endif
6897
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006898 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02006899 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006900
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006901 return 0;
6902}
6903
6904int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6905{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006906 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
6907 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6908
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006909 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
6910 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
6911 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
6912 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
6913 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
6914 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
6915 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
6916 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006917#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006918 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
6919 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
6920 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
6921 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
6922 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
6923 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
6924 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
6925 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006926#endif
6927
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006928 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02006929 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006930
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02006931 vcpu->arch.exception.pending = false;
6932
Avi Kivity3842d132010-07-27 12:30:24 +03006933 kvm_make_request(KVM_REQ_EVENT, vcpu);
6934
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006935 return 0;
6936}
6937
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006938void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
6939{
6940 struct kvm_segment cs;
6941
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006942 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006943 *db = cs.db;
6944 *l = cs.l;
6945}
6946EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
6947
6948int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
6949 struct kvm_sregs *sregs)
6950{
Gleb Natapov89a27f42010-02-16 10:51:48 +02006951 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006952
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006953 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6954 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6955 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6956 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6957 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6958 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006959
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006960 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6961 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006962
6963 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006964 sregs->idt.limit = dt.size;
6965 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006966 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006967 sregs->gdt.limit = dt.size;
6968 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006969
Avi Kivity4d4ec082009-12-29 18:07:30 +02006970 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006971 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006972 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02006973 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006974 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02006975 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006976 sregs->apic_base = kvm_get_apic_base(vcpu);
6977
Gleb Natapov923c61b2009-05-11 13:35:48 +03006978 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006979
Gleb Natapov36752c92009-05-11 13:35:53 +03006980 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03006981 set_bit(vcpu->arch.interrupt.nr,
6982 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03006983
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006984 return 0;
6985}
6986
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006987int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
6988 struct kvm_mp_state *mp_state)
6989{
Jan Kiszka66450a22013-03-13 12:42:34 +01006990 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306991 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
6992 vcpu->arch.pv.pv_unhalted)
6993 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
6994 else
6995 mp_state->mp_state = vcpu->arch.mp_state;
6996
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006997 return 0;
6998}
6999
7000int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
7001 struct kvm_mp_state *mp_state)
7002{
Jan Kiszka66450a22013-03-13 12:42:34 +01007003 if (!kvm_vcpu_has_lapic(vcpu) &&
7004 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
7005 return -EINVAL;
7006
7007 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
7008 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
7009 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
7010 } else
7011 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03007012 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03007013 return 0;
7014}
7015
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007016int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
7017 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007018{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007019 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007020 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007021
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007022 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007023
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007024 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007025 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02007026
Gleb Natapovc6975182010-02-18 12:15:01 +02007027 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03007028 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02007029
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007030 kvm_rip_write(vcpu, ctxt->eip);
7031 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03007032 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03007033 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02007034}
7035EXPORT_SYMBOL_GPL(kvm_task_switch);
7036
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007037int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
7038 struct kvm_sregs *sregs)
7039{
Jan Kiszka58cb6282014-01-24 16:48:44 +01007040 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007041 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007042 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02007043 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007044
Petr Matousek6d1068b2012-11-06 19:24:07 +01007045 if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
7046 return -EINVAL;
7047
Gleb Natapov89a27f42010-02-16 10:51:48 +02007048 dt.size = sregs->idt.limit;
7049 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007050 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02007051 dt.size = sregs->gdt.limit;
7052 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007053 kvm_x86_ops->set_gdt(vcpu, &dt);
7054
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007055 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02007056 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02007057 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02007058 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007059
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007060 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007061
Avi Kivityf6801df2010-01-21 15:31:50 +02007062 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007063 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Jan Kiszka58cb6282014-01-24 16:48:44 +01007064 apic_base_msr.data = sregs->apic_base;
7065 apic_base_msr.host_initiated = true;
7066 kvm_set_apic_base(vcpu, &apic_base_msr);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007067
Avi Kivity4d4ec082009-12-29 18:07:30 +02007068 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007069 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00007070 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007071
Avi Kivityfc78f512009-12-07 12:16:48 +02007072 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007073 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Sheng Yang3ea3aa82010-12-08 10:49:43 +08007074 if (sregs->cr4 & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +02007075 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007076
7077 idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02007078 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02007079 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02007080 mmu_reset_needed = 1;
7081 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007082 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007083
7084 if (mmu_reset_needed)
7085 kvm_mmu_reset_context(vcpu);
7086
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03007087 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03007088 pending_vec = find_first_bit(
7089 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
7090 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007091 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03007092 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007093 }
7094
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007095 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
7096 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
7097 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
7098 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
7099 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
7100 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007101
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007102 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
7103 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007104
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03007105 update_cr8_intercept(vcpu);
7106
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007107 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03007108 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007109 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02007110 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007111 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
7112
Avi Kivity3842d132010-07-27 12:30:24 +03007113 kvm_make_request(KVM_REQ_EVENT, vcpu);
7114
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007115 return 0;
7116}
7117
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007118int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
7119 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007120{
Jan Kiszka355be0b2009-10-03 00:31:21 +02007121 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007122 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007123
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007124 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
7125 r = -EBUSY;
7126 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03007127 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007128 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
7129 kvm_queue_exception(vcpu, DB_VECTOR);
7130 else
7131 kvm_queue_exception(vcpu, BP_VECTOR);
7132 }
7133
Jan Kiszka91586a32009-10-05 13:07:21 +02007134 /*
7135 * Read rflags as long as potentially injected trace flags are still
7136 * filtered out.
7137 */
7138 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02007139
7140 vcpu->guest_debug = dbg->control;
7141 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
7142 vcpu->guest_debug = 0;
7143
7144 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007145 for (i = 0; i < KVM_NR_DB_REGS; ++i)
7146 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02007147 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007148 } else {
7149 for (i = 0; i < KVM_NR_DB_REGS; i++)
7150 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007151 }
Jan Kiszkac8639012012-09-21 05:42:55 +02007152 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007153
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007154 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7155 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
7156 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007157
Jan Kiszka91586a32009-10-05 13:07:21 +02007158 /*
7159 * Trigger an rflags update that will inject or remove the trace
7160 * flags.
7161 */
7162 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007163
Jan Kiszkac8639012012-09-21 05:42:55 +02007164 kvm_x86_ops->update_db_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02007165
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007166 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007167
Avi Kivity2122ff52010-05-13 11:25:04 +03007168out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007169
7170 return r;
7171}
7172
7173/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08007174 * Translate a guest virtual address to a guest physical address.
7175 */
7176int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
7177 struct kvm_translation *tr)
7178{
7179 unsigned long vaddr = tr->linear_address;
7180 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007181 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007182
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007183 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02007184 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007185 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08007186 tr->physical_address = gpa;
7187 tr->valid = gpa != UNMAPPED_GVA;
7188 tr->writeable = 1;
7189 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007190
7191 return 0;
7192}
7193
Hollis Blanchardd0752062007-10-31 17:24:25 -05007194int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7195{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007196 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007197 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007198
Hollis Blanchardd0752062007-10-31 17:24:25 -05007199 memcpy(fpu->fpr, fxsave->st_space, 128);
7200 fpu->fcw = fxsave->cwd;
7201 fpu->fsw = fxsave->swd;
7202 fpu->ftwx = fxsave->twd;
7203 fpu->last_opcode = fxsave->fop;
7204 fpu->last_ip = fxsave->rip;
7205 fpu->last_dp = fxsave->rdp;
7206 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
7207
Hollis Blanchardd0752062007-10-31 17:24:25 -05007208 return 0;
7209}
7210
7211int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7212{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007213 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007214 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007215
Hollis Blanchardd0752062007-10-31 17:24:25 -05007216 memcpy(fxsave->st_space, fpu->fpr, 128);
7217 fxsave->cwd = fpu->fcw;
7218 fxsave->swd = fpu->fsw;
7219 fxsave->twd = fpu->ftwx;
7220 fxsave->fop = fpu->last_opcode;
7221 fxsave->rip = fpu->last_ip;
7222 fxsave->rdp = fpu->last_dp;
7223 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
7224
Hollis Blanchardd0752062007-10-31 17:24:25 -05007225 return 0;
7226}
7227
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007228static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007229{
Ingo Molnarbf935b02015-04-30 10:23:42 +02007230 fpstate_init(&vcpu->arch.guest_fpu.state);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007231 if (cpu_has_xsaves)
Ingo Molnar7366ed72015-04-27 04:19:39 +02007232 vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007233 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007234
Dexuan Cui2acf9232010-06-10 11:27:12 +08007235 /*
7236 * Ensure guest xcr0 is valid for loading
7237 */
7238 vcpu->arch.xcr0 = XSTATE_FP;
7239
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007240 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007241}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007242
7243void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
7244{
Avi Kivity2608d7a2010-01-21 15:31:45 +02007245 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007246 return;
7247
Dexuan Cui2acf9232010-06-10 11:27:12 +08007248 /*
7249 * Restore all possible states in the guest,
7250 * and assume host would use all available bits.
7251 * Guest xcr0 would be loaded later.
7252 */
7253 kvm_put_guest_xcr0(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007254 vcpu->guest_fpu_loaded = 1;
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007255 __kernel_fpu_begin();
Ingo Molnar003e2e82015-05-25 11:59:35 +02007256 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state);
Avi Kivity0c048512010-01-21 15:31:52 +02007257 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007258}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007259
7260void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
7261{
Dexuan Cui2acf9232010-06-10 11:27:12 +08007262 kvm_put_guest_xcr0(vcpu);
7263
Rik van Riel653f52c2015-04-23 11:52:37 -04007264 if (!vcpu->guest_fpu_loaded) {
7265 vcpu->fpu_counter = 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007266 return;
Rik van Riel653f52c2015-04-23 11:52:37 -04007267 }
Hollis Blanchardd0752062007-10-31 17:24:25 -05007268
7269 vcpu->guest_fpu_loaded = 0;
Ingo Molnar4f836342015-04-27 02:53:16 +02007270 copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007271 __kernel_fpu_end();
Avi Kivityf096ed82007-11-18 13:54:33 +02007272 ++vcpu->stat.fpu_reload;
Rik van Riel653f52c2015-04-23 11:52:37 -04007273 /*
7274 * If using eager FPU mode, or if the guest is a frequent user
7275 * of the FPU, just leave the FPU active for next time.
7276 * Every 255 times fpu_counter rolls over to 0; a guest that uses
7277 * the FPU in bursts will revert to loading it on demand.
7278 */
Paolo Bonzinia9b4fb72015-05-20 11:46:12 +02007279 if (!vcpu->arch.eager_fpu) {
Rik van Riel653f52c2015-04-23 11:52:37 -04007280 if (++vcpu->fpu_counter < 5)
7281 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
7282 }
Avi Kivity0c048512010-01-21 15:31:52 +02007283 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007284}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007285
7286void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
7287{
Glauber Costa12f9a482011-02-01 14:16:40 -05007288 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01007289
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007290 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007291 kvm_x86_ops->vcpu_free(vcpu);
7292}
7293
7294struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
7295 unsigned int id)
7296{
Liang Lic447e762015-05-21 04:41:25 +08007297 struct kvm_vcpu *vcpu;
7298
Zachary Amsden6755bae2010-08-19 22:07:22 -10007299 if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
7300 printk_once(KERN_WARNING
7301 "kvm: SMP vm created on host with unstable TSC; "
7302 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08007303
7304 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
7305
7306 /*
7307 * Activate fpu unconditionally in case the guest needs eager FPU. It will be
7308 * deactivated soon if it doesn't.
7309 */
7310 kvm_x86_ops->fpu_activate(vcpu);
7311 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02007312}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007313
Avi Kivity26e52152007-11-20 15:30:24 +02007314int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
7315{
7316 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007317
Xiao Guangrong19efffa2015-06-15 16:55:31 +08007318 kvm_vcpu_mtrr_init(vcpu);
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007319 r = vcpu_load(vcpu);
7320 if (r)
7321 return r;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007322 kvm_vcpu_reset(vcpu, false);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007323 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007324 vcpu_put(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02007325 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007326}
7327
Dominik Dingel31928aa2014-12-04 15:47:07 +01007328void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007329{
Will Auld8fe8ab42012-11-29 12:42:12 -08007330 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01007331 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007332
Dominik Dingel31928aa2014-12-04 15:47:07 +01007333 if (vcpu_load(vcpu))
7334 return;
Will Auld8fe8ab42012-11-29 12:42:12 -08007335 msr.data = 0x0;
7336 msr.index = MSR_IA32_TSC;
7337 msr.host_initiated = true;
7338 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007339 vcpu_put(vcpu);
7340
Marcelo Tosatti630994b2015-05-12 22:42:04 -03007341 if (!kvmclock_periodic_sync)
7342 return;
7343
Andrew Jones332967a2014-02-28 12:52:55 +01007344 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
7345 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007346}
7347
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06007348void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007349{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007350 int r;
Gleb Natapov344d9582010-10-14 11:22:50 +02007351 vcpu->arch.apf.msr_val = 0;
7352
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007353 r = vcpu_load(vcpu);
7354 BUG_ON(r);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007355 kvm_mmu_unload(vcpu);
7356 vcpu_put(vcpu);
7357
7358 kvm_x86_ops->vcpu_free(vcpu);
7359}
7360
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007361void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007362{
Paolo Bonzinie69fab52015-06-04 10:44:44 +02007363 vcpu->arch.hflags = 0;
7364
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007365 atomic_set(&vcpu->arch.nmi_queued, 0);
7366 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007367 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03007368 kvm_clear_interrupt_queue(vcpu);
7369 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007370
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007371 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03007372 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03007373 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007374 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007375 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02007376 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007377
Nadav Amit11190222015-04-02 03:10:38 +03007378 vcpu->arch.cr2 = 0;
7379
Avi Kivity3842d132010-07-27 12:30:24 +03007380 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02007381 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04007382 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03007383
Glauber Costa12f9a482011-02-01 14:16:40 -05007384 kvmclock_reset(vcpu);
7385
Gleb Natapovaf585b92010-10-14 11:22:46 +02007386 kvm_clear_async_pf_completion_queue(vcpu);
7387 kvm_async_pf_hash_reset(vcpu);
7388 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007389
Paolo Bonzini64d60672015-05-07 11:36:11 +02007390 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007391 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007392 vcpu->arch.smbase = 0x30000;
7393 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02007394
Julian Stecklina66f7b722012-12-05 15:26:19 +01007395 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
7396 vcpu->arch.regs_avail = ~0;
7397 vcpu->arch.regs_dirty = ~0;
7398
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007399 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007400}
7401
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01007402void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01007403{
7404 struct kvm_segment cs;
7405
7406 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
7407 cs.selector = vector << 8;
7408 cs.base = vector << 12;
7409 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7410 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007411}
7412
Radim Krčmář13a34e02014-08-28 15:13:03 +02007413int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007414{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10007415 struct kvm *kvm;
7416 struct kvm_vcpu *vcpu;
7417 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007418 int ret;
7419 u64 local_tsc;
7420 u64 max_tsc = 0;
7421 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03007422
7423 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02007424 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007425 if (ret != 0)
7426 return ret;
7427
Andy Lutomirski4ea16362015-06-25 18:44:07 +02007428 local_tsc = rdtsc();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007429 stable = !check_tsc_unstable();
7430 list_for_each_entry(kvm, &vm_list, vm_list) {
7431 kvm_for_each_vcpu(i, vcpu, kvm) {
7432 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007433 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007434 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
7435 backwards_tsc = true;
7436 if (vcpu->arch.last_host_tsc > max_tsc)
7437 max_tsc = vcpu->arch.last_host_tsc;
7438 }
7439 }
7440 }
7441
7442 /*
7443 * Sometimes, even reliable TSCs go backwards. This happens on
7444 * platforms that reset TSC during suspend or hibernate actions, but
7445 * maintain synchronization. We must compensate. Fortunately, we can
7446 * detect that condition here, which happens early in CPU bringup,
7447 * before any KVM threads can be running. Unfortunately, we can't
7448 * bring the TSCs fully up to date with real time, as we aren't yet far
7449 * enough into CPU bringup that we know how much real time has actually
7450 * elapsed; our helper function, get_kernel_ns() will be using boot
7451 * variables that haven't been updated yet.
7452 *
7453 * So we simply find the maximum observed TSC above, then record the
7454 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
7455 * the adjustment will be applied. Note that we accumulate
7456 * adjustments, in case multiple suspend cycles happen before some VCPU
7457 * gets a chance to run again. In the event that no KVM threads get a
7458 * chance to run, we will miss the entire elapsed period, as we'll have
7459 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
7460 * loose cycle time. This isn't too big a deal, since the loss will be
7461 * uniform across all VCPUs (not to mention the scenario is extremely
7462 * unlikely). It is possible that a second hibernate recovery happens
7463 * much faster than a first, causing the observed TSC here to be
7464 * smaller; this would require additional padding adjustment, which is
7465 * why we set last_host_tsc to the local tsc observed here.
7466 *
7467 * N.B. - this code below runs only on platforms with reliable TSC,
7468 * as that is the only way backwards_tsc is set above. Also note
7469 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
7470 * have the same delta_cyc adjustment applied if backwards_tsc
7471 * is detected. Note further, this adjustment is only done once,
7472 * as we reset last_host_tsc on all VCPUs to stop this from being
7473 * called multiple times (one for each physical CPU bringup).
7474 *
Guo Chao4a969982012-06-28 15:17:27 +08007475 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007476 * will be compensated by the logic in vcpu_load, which sets the TSC to
7477 * catchup mode. This will catchup all VCPUs to real time, but cannot
7478 * guarantee that they stay in perfect synchronization.
7479 */
7480 if (backwards_tsc) {
7481 u64 delta_cyc = max_tsc - local_tsc;
Marcelo Tosatti16a96022014-05-14 12:43:24 -03007482 backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007483 list_for_each_entry(kvm, &vm_list, vm_list) {
7484 kvm_for_each_vcpu(i, vcpu, kvm) {
7485 vcpu->arch.tsc_offset_adjustment += delta_cyc;
7486 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007487 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007488 }
7489
7490 /*
7491 * We have to disable TSC offset matching.. if you were
7492 * booting a VM while issuing an S4 host suspend....
7493 * you may have some problem. Solving this issue is
7494 * left as an exercise to the reader.
7495 */
7496 kvm->arch.last_tsc_nsec = 0;
7497 kvm->arch.last_tsc_write = 0;
7498 }
7499
7500 }
7501 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007502}
7503
Radim Krčmář13a34e02014-08-28 15:13:03 +02007504void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007505{
Radim Krčmář13a34e02014-08-28 15:13:03 +02007506 kvm_x86_ops->hardware_disable();
7507 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007508}
7509
7510int kvm_arch_hardware_setup(void)
7511{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03007512 int r;
7513
7514 r = kvm_x86_ops->hardware_setup();
7515 if (r != 0)
7516 return r;
7517
7518 kvm_init_msr_list();
7519 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007520}
7521
7522void kvm_arch_hardware_unsetup(void)
7523{
7524 kvm_x86_ops->hardware_unsetup();
7525}
7526
7527void kvm_arch_check_processor_compat(void *rtn)
7528{
7529 kvm_x86_ops->check_processor_compatibility(rtn);
7530}
7531
Avi Kivity3e515702012-03-05 14:23:29 +02007532bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
7533{
7534 return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
7535}
7536
Gleb Natapov54e98182012-08-05 15:58:32 +03007537struct static_key kvm_no_apic_vcpu __read_mostly;
7538
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007539int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
7540{
7541 struct page *page;
7542 struct kvm *kvm;
7543 int r;
7544
7545 BUG_ON(vcpu->kvm == NULL);
7546 kvm = vcpu->kvm;
7547
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307548 vcpu->arch.pv.pv_unhalted = false;
Avi Kivity9aabc88f2010-07-29 15:11:50 +03007549 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
Nadav Amit58d269d2015-04-02 03:10:36 +03007550 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03007551 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007552 else
Avi Kivitya4535292008-04-13 17:54:35 +03007553 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007554
7555 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
7556 if (!page) {
7557 r = -ENOMEM;
7558 goto fail;
7559 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007560 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007561
Zachary Amsdencc578282012-02-03 15:43:50 -02007562 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007563
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007564 r = kvm_mmu_create(vcpu);
7565 if (r < 0)
7566 goto fail_free_pio_data;
7567
7568 if (irqchip_in_kernel(kvm)) {
7569 r = kvm_create_lapic(vcpu);
7570 if (r < 0)
7571 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03007572 } else
7573 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007574
Huang Ying890ca9a2009-05-11 16:48:15 +08007575 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
7576 GFP_KERNEL);
7577 if (!vcpu->arch.mce_banks) {
7578 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08007579 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08007580 }
7581 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
7582
Wei Yongjunf1797352013-04-18 07:41:00 +08007583 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
7584 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007585 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08007586 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007587
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007588 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01007589
Will Auldba904632012-11-29 12:42:50 -08007590 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
Andy Honig0b794592013-02-20 14:48:10 -08007591 vcpu->arch.pv_time_enabled = false;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007592
7593 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +02007594 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007595
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03007596 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
7597
Radim Krčmář74545702015-04-27 15:11:25 +02007598 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
7599
Gleb Natapovaf585b92010-10-14 11:22:46 +02007600 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007601 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007602
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007603 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007604
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007605fail_free_mce_banks:
7606 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08007607fail_free_lapic:
7608 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007609fail_mmu_destroy:
7610 kvm_mmu_destroy(vcpu);
7611fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007612 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007613fail:
7614 return r;
7615}
7616
7617void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
7618{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007619 int idx;
7620
Gleb Natapovf5132b02011-11-10 14:57:22 +02007621 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08007622 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007623 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007624 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007625 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007626 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007627 free_page((unsigned long)vcpu->arch.pio_data);
Gleb Natapov54e98182012-08-05 15:58:32 +03007628 if (!irqchip_in_kernel(vcpu->kvm))
7629 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007630}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007631
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007632void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
7633{
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007634 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007635}
7636
Carsten Ottee08b9632012-01-04 10:25:20 +01007637int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007638{
Carsten Ottee08b9632012-01-04 10:25:20 +01007639 if (type)
7640 return -EINVAL;
7641
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01007642 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08007643 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08007644 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007645 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007646 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007647
Sheng Yang5550af42008-10-15 20:15:06 +08007648 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
7649 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06007650 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
7651 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
7652 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08007653
Jan Kiszka038f8c12011-02-04 10:49:11 +01007654 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007655 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007656 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
7657
7658 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01007659
Andrew Jones7e44e442014-02-28 12:52:54 +01007660 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01007661 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01007662
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01007663 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007664}
7665
7666static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
7667{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007668 int r;
7669 r = vcpu_load(vcpu);
7670 BUG_ON(r);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007671 kvm_mmu_unload(vcpu);
7672 vcpu_put(vcpu);
7673}
7674
7675static void kvm_free_vcpus(struct kvm *kvm)
7676{
7677 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007678 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007679
7680 /*
7681 * Unpin any mmu pages first.
7682 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02007683 kvm_for_each_vcpu(i, vcpu, kvm) {
7684 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007685 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007686 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007687 kvm_for_each_vcpu(i, vcpu, kvm)
7688 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007689
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007690 mutex_lock(&kvm->lock);
7691 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
7692 kvm->vcpus[i] = NULL;
7693
7694 atomic_set(&kvm->online_vcpus, 0);
7695 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007696}
7697
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007698void kvm_arch_sync_events(struct kvm *kvm)
7699{
Andrew Jones332967a2014-02-28 12:52:55 +01007700 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01007701 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Sheng Yangba4cef32009-01-06 10:03:03 +08007702 kvm_free_all_assigned_devices(kvm);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08007703 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007704}
7705
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007706int __x86_set_memory_region(struct kvm *kvm,
7707 const struct kvm_userspace_memory_region *mem)
7708{
7709 int i, r;
7710
7711 /* Called with kvm->slots_lock held. */
7712 BUG_ON(mem->slot >= KVM_MEM_SLOTS_NUM);
7713
7714 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
7715 struct kvm_userspace_memory_region m = *mem;
7716
7717 m.slot |= i << 16;
7718 r = __kvm_set_memory_region(kvm, &m);
7719 if (r < 0)
7720 return r;
7721 }
7722
7723 return 0;
7724}
7725EXPORT_SYMBOL_GPL(__x86_set_memory_region);
7726
7727int x86_set_memory_region(struct kvm *kvm,
7728 const struct kvm_userspace_memory_region *mem)
7729{
7730 int r;
7731
7732 mutex_lock(&kvm->slots_lock);
7733 r = __x86_set_memory_region(kvm, mem);
7734 mutex_unlock(&kvm->slots_lock);
7735
7736 return r;
7737}
7738EXPORT_SYMBOL_GPL(x86_set_memory_region);
7739
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007740void kvm_arch_destroy_vm(struct kvm *kvm)
7741{
Andrew Honig27469d22013-04-18 09:38:14 -07007742 if (current->mm == kvm->mm) {
7743 /*
7744 * Free memory regions allocated on behalf of userspace,
7745 * unless the the memory map has changed due to process exit
7746 * or fd copying.
7747 */
7748 struct kvm_userspace_memory_region mem;
7749 memset(&mem, 0, sizeof(mem));
7750 mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007751 x86_set_memory_region(kvm, &mem);
Andrew Honig27469d22013-04-18 09:38:14 -07007752
7753 mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007754 x86_set_memory_region(kvm, &mem);
Andrew Honig27469d22013-04-18 09:38:14 -07007755
7756 mem.slot = TSS_PRIVATE_MEMSLOT;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007757 x86_set_memory_region(kvm, &mem);
Andrew Honig27469d22013-04-18 09:38:14 -07007758 }
Sheng Yang6eb55812008-10-31 12:37:41 +08007759 kvm_iommu_unmap_guest(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08007760 kfree(kvm->arch.vpic);
7761 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007762 kvm_free_vcpus(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007763 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007764}
Zhang Xiantao0de10342007-11-20 16:25:04 +08007765
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307766void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007767 struct kvm_memory_slot *dont)
7768{
7769 int i;
7770
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007771 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7772 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007773 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007774 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007775 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007776 if (i == 0)
7777 continue;
7778
7779 if (!dont || free->arch.lpage_info[i - 1] !=
7780 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007781 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007782 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007783 }
7784 }
7785}
7786
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307787int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7788 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007789{
7790 int i;
7791
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007792 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007793 unsigned long ugfn;
7794 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007795 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007796
7797 lpages = gfn_to_index(slot->base_gfn + npages - 1,
7798 slot->base_gfn, level) + 1;
7799
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007800 slot->arch.rmap[i] =
7801 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
7802 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007803 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007804 if (i == 0)
7805 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007806
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007807 slot->arch.lpage_info[i - 1] = kvm_kvzalloc(lpages *
7808 sizeof(*slot->arch.lpage_info[i - 1]));
7809 if (!slot->arch.lpage_info[i - 1])
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007810 goto out_free;
7811
7812 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007813 slot->arch.lpage_info[i - 1][0].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007814 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007815 slot->arch.lpage_info[i - 1][lpages - 1].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007816 ugfn = slot->userspace_addr >> PAGE_SHIFT;
7817 /*
7818 * If the gfn and userspace address are not aligned wrt each
7819 * other, or if explicitly asked to, disable large page
7820 * support for this slot
7821 */
7822 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
7823 !kvm_largepages_enabled()) {
7824 unsigned long j;
7825
7826 for (j = 0; j < lpages; ++j)
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007827 slot->arch.lpage_info[i - 1][j].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007828 }
7829 }
7830
7831 return 0;
7832
7833out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007834 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01007835 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007836 slot->arch.rmap[i] = NULL;
7837 if (i == 0)
7838 continue;
7839
Thomas Huth548ef282015-02-24 21:29:25 +01007840 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007841 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007842 }
7843 return -ENOMEM;
7844}
7845
Paolo Bonzini15f46012015-05-17 21:26:08 +02007846void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007847{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09007848 /*
7849 * memslots->generation has been incremented.
7850 * mmio generation may have reached its maximum value.
7851 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007852 kvm_mmu_invalidate_mmio_sptes(kvm, slots);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007853}
7854
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007855int kvm_arch_prepare_memory_region(struct kvm *kvm,
7856 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02007857 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007858 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08007859{
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007860 /*
7861 * Only private memory slots need to be mapped here since
7862 * KVM_SET_MEMORY_REGION ioctl is no longer supported.
Zhang Xiantao0de10342007-11-20 16:25:04 +08007863 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007864 if ((memslot->id >= KVM_USER_MEM_SLOTS) && (change == KVM_MR_CREATE)) {
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007865 unsigned long userspace_addr;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007866
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007867 /*
7868 * MAP_SHARED to prevent internal slot pages from being moved
7869 * by fork()/COW.
7870 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007871 userspace_addr = vm_mmap(NULL, 0, memslot->npages * PAGE_SIZE,
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007872 PROT_READ | PROT_WRITE,
7873 MAP_SHARED | MAP_ANONYMOUS, 0);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007874
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007875 if (IS_ERR((void *)userspace_addr))
7876 return PTR_ERR((void *)userspace_addr);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007877
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007878 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08007879 }
7880
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007881 return 0;
7882}
7883
Kai Huang88178fd2015-01-28 10:54:27 +08007884static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
7885 struct kvm_memory_slot *new)
7886{
7887 /* Still write protect RO slot */
7888 if (new->flags & KVM_MEM_READONLY) {
7889 kvm_mmu_slot_remove_write_access(kvm, new);
7890 return;
7891 }
7892
7893 /*
7894 * Call kvm_x86_ops dirty logging hooks when they are valid.
7895 *
7896 * kvm_x86_ops->slot_disable_log_dirty is called when:
7897 *
7898 * - KVM_MR_CREATE with dirty logging is disabled
7899 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
7900 *
7901 * The reason is, in case of PML, we need to set D-bit for any slots
7902 * with dirty logging disabled in order to eliminate unnecessary GPA
7903 * logging in PML buffer (and potential PML buffer full VMEXT). This
7904 * guarantees leaving PML enabled during guest's lifetime won't have
7905 * any additonal overhead from PML when guest is running with dirty
7906 * logging disabled for memory slots.
7907 *
7908 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
7909 * to dirty logging mode.
7910 *
7911 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
7912 *
7913 * In case of write protect:
7914 *
7915 * Write protect all pages for dirty logging.
7916 *
7917 * All the sptes including the large sptes which point to this
7918 * slot are set to readonly. We can not create any new large
7919 * spte on this slot until the end of the logging.
7920 *
7921 * See the comments in fast_page_fault().
7922 */
7923 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
7924 if (kvm_x86_ops->slot_enable_log_dirty)
7925 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
7926 else
7927 kvm_mmu_slot_remove_write_access(kvm, new);
7928 } else {
7929 if (kvm_x86_ops->slot_disable_log_dirty)
7930 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
7931 }
7932}
7933
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007934void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02007935 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007936 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007937 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007938 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007939{
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007940 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007941
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007942 if (change == KVM_MR_DELETE && old->id >= KVM_USER_MEM_SLOTS) {
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007943 int ret;
7944
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007945 ret = vm_munmap(old->userspace_addr,
7946 old->npages * PAGE_SIZE);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007947 if (ret < 0)
7948 printk(KERN_WARNING
7949 "kvm_vm_ioctl_set_memory_region: "
7950 "failed to munmap memory\n");
7951 }
7952
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007953 if (!kvm->arch.n_requested_mmu_pages)
7954 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007955
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007956 if (nr_mmu_pages)
7957 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Kai Huang1c91cad42015-01-28 10:54:26 +08007958
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007959 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08007960 * Dirty logging tracks sptes in 4k granularity, meaning that large
7961 * sptes have to be split. If live migration is successful, the guest
7962 * in the source machine will be destroyed and large sptes will be
7963 * created in the destination. However, if the guest continues to run
7964 * in the source machine (for example if live migration fails), small
7965 * sptes will remain around and cause bad performance.
7966 *
7967 * Scan sptes if dirty logging has been stopped, dropping those
7968 * which can be collapsed into a single large-page spte. Later
7969 * page faults will create the large-page sptes.
7970 */
7971 if ((change != KVM_MR_DELETE) &&
7972 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
7973 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
7974 kvm_mmu_zap_collapsible_sptes(kvm, new);
7975
7976 /*
Kai Huang88178fd2015-01-28 10:54:27 +08007977 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08007978 *
Kai Huang88178fd2015-01-28 10:54:27 +08007979 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
7980 * been zapped so no dirty logging staff is needed for old slot. For
7981 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
7982 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007983 *
7984 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007985 */
Kai Huang88178fd2015-01-28 10:54:27 +08007986 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007987 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007988}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007989
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007990void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007991{
Xiao Guangrong6ca18b62013-05-31 08:36:23 +08007992 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007993}
7994
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007995void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
7996 struct kvm_memory_slot *slot)
7997{
Xiao Guangrong6ca18b62013-05-31 08:36:23 +08007998 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007999}
8000
Zhang Xiantao1d737c82007-12-14 09:35:10 +08008001int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
8002{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008003 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
8004 kvm_x86_ops->check_nested_events(vcpu, false);
8005
Gleb Natapovaf585b92010-10-14 11:22:46 +02008006 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8007 !vcpu->arch.apf.halted)
8008 || !list_empty_careful(&vcpu->async_pf.done)
Jan Kiszka66450a22013-03-13 12:42:34 +01008009 || kvm_apic_has_events(vcpu)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308010 || vcpu->arch.pv.pv_unhalted
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008011 || atomic_read(&vcpu->arch.nmi_queued) ||
Gleb Natapova1b37102009-07-09 15:33:52 +03008012 (kvm_arch_interrupt_allowed(vcpu) &&
8013 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08008014}
Zhang Xiantao57361992007-12-17 14:21:40 +08008015
Christoffer Dallb6d33832012-03-08 16:44:24 -05008016int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08008017{
Christoffer Dallb6d33832012-03-08 16:44:24 -05008018 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08008019}
Gleb Natapov78646122009-03-23 12:12:11 +02008020
8021int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
8022{
8023 return kvm_x86_ops->interrupt_allowed(vcpu);
8024}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008025
Nadav Amit82b32772014-11-02 11:54:45 +02008026unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
8027{
8028 if (is_64_bit_mode(vcpu))
8029 return kvm_rip_read(vcpu);
8030 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
8031 kvm_rip_read(vcpu));
8032}
8033EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
8034
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008035bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
8036{
Nadav Amit82b32772014-11-02 11:54:45 +02008037 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008038}
8039EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
8040
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008041unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
8042{
8043 unsigned long rflags;
8044
8045 rflags = kvm_x86_ops->get_rflags(vcpu);
8046 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01008047 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008048 return rflags;
8049}
8050EXPORT_SYMBOL_GPL(kvm_get_rflags);
8051
Paolo Bonzini6addfc42014-03-27 11:29:28 +01008052static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008053{
8054 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008055 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01008056 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008057 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01008058}
8059
8060void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
8061{
8062 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008063 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008064}
8065EXPORT_SYMBOL_GPL(kvm_set_rflags);
8066
Gleb Natapov56028d02010-10-17 18:13:42 +02008067void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
8068{
8069 int r;
8070
Xiao Guangrongfb67e142010-12-07 10:35:25 +08008071 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08008072 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02008073 return;
8074
8075 r = kvm_mmu_reload(vcpu);
8076 if (unlikely(r))
8077 return;
8078
Xiao Guangrongfb67e142010-12-07 10:35:25 +08008079 if (!vcpu->arch.mmu.direct_map &&
8080 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
8081 return;
8082
Gleb Natapov56028d02010-10-17 18:13:42 +02008083 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
8084}
8085
Gleb Natapovaf585b92010-10-14 11:22:46 +02008086static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
8087{
8088 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
8089}
8090
8091static inline u32 kvm_async_pf_next_probe(u32 key)
8092{
8093 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
8094}
8095
8096static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8097{
8098 u32 key = kvm_async_pf_hash_fn(gfn);
8099
8100 while (vcpu->arch.apf.gfns[key] != ~0)
8101 key = kvm_async_pf_next_probe(key);
8102
8103 vcpu->arch.apf.gfns[key] = gfn;
8104}
8105
8106static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
8107{
8108 int i;
8109 u32 key = kvm_async_pf_hash_fn(gfn);
8110
8111 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08008112 (vcpu->arch.apf.gfns[key] != gfn &&
8113 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02008114 key = kvm_async_pf_next_probe(key);
8115
8116 return key;
8117}
8118
8119bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8120{
8121 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
8122}
8123
8124static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8125{
8126 u32 i, j, k;
8127
8128 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
8129 while (true) {
8130 vcpu->arch.apf.gfns[i] = ~0;
8131 do {
8132 j = kvm_async_pf_next_probe(j);
8133 if (vcpu->arch.apf.gfns[j] == ~0)
8134 return;
8135 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
8136 /*
8137 * k lies cyclically in ]i,j]
8138 * | i.k.j |
8139 * |....j i.k.| or |.k..j i...|
8140 */
8141 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
8142 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
8143 i = j;
8144 }
8145}
8146
Gleb Natapov7c907052010-10-14 11:22:53 +02008147static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
8148{
8149
8150 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
8151 sizeof(val));
8152}
8153
Gleb Natapovaf585b92010-10-14 11:22:46 +02008154void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
8155 struct kvm_async_pf *work)
8156{
Avi Kivity6389ee92010-11-29 16:12:30 +02008157 struct x86_exception fault;
8158
Gleb Natapov7c907052010-10-14 11:22:53 +02008159 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008160 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02008161
8162 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02008163 (vcpu->arch.apf.send_user_only &&
8164 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02008165 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
8166 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008167 fault.vector = PF_VECTOR;
8168 fault.error_code_valid = true;
8169 fault.error_code = 0;
8170 fault.nested_page_fault = false;
8171 fault.address = work->arch.token;
8172 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008173 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008174}
8175
8176void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
8177 struct kvm_async_pf *work)
8178{
Avi Kivity6389ee92010-11-29 16:12:30 +02008179 struct x86_exception fault;
8180
Gleb Natapov7c907052010-10-14 11:22:53 +02008181 trace_kvm_async_pf_ready(work->arch.token, work->gva);
chai wenf2e10662013-10-14 22:22:33 +08008182 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02008183 work->arch.token = ~0; /* broadcast wakeup */
8184 else
8185 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
8186
8187 if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
8188 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008189 fault.vector = PF_VECTOR;
8190 fault.error_code_valid = true;
8191 fault.error_code = 0;
8192 fault.nested_page_fault = false;
8193 fault.address = work->arch.token;
8194 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008195 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08008196 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03008197 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02008198}
8199
8200bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
8201{
8202 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
8203 return true;
8204 else
8205 return !kvm_event_needs_reinjection(vcpu) &&
8206 kvm_x86_ops->interrupt_allowed(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008207}
8208
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06008209void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
8210{
8211 atomic_inc(&kvm->arch.noncoherent_dma_count);
8212}
8213EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
8214
8215void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
8216{
8217 atomic_dec(&kvm->arch.noncoherent_dma_count);
8218}
8219EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
8220
8221bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
8222{
8223 return atomic_read(&kvm->arch.noncoherent_dma_count);
8224}
8225EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
8226
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008227EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
8228EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
8229EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
8230EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
8231EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02008232EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02008233EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02008234EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02008235EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02008236EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02008237EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01008238EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09008239EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +02008240EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
Kai Huang843e4332015-01-28 10:54:28 +08008241EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);