blob: 3978ace4ddbc95caafc23ea095ff84ac9a4ed44b [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030014 * Amit Shah <amit.shah@qumranet.com>
15 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020016 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 */
21
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020023#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080024#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080025#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020026#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030027#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030028#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020029#include "cpuid.h"
Paolo Bonzinic9eab582014-11-24 15:27:17 +010030#include "assigned-dev.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020031#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030032#include "hyperv.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020033
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020034#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030035#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020036#include <linux/kvm.h>
37#include <linux/fs.h>
38#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010039#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080040#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050041#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010042#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030043#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010044#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030045#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020046#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080048#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080049#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020050#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020051#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020052#include <linux/timekeeper_internal.h>
53#include <linux/pvclock_gtod.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030054#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080055
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#define CREATE_TRACE_POINTS
57#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020058
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020059#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080060#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020061#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080062#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020063#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020064#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100065#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030066#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080067#include <asm/irq_remapping.h>
Carsten Otte043405e2007-10-10 17:16:19 +020068
Carsten Otte313a3dc2007-10-11 19:16:52 +020069#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080070#define KVM_MAX_MCE_BANKS 32
Huang Ying5854dbc2010-10-08 16:24:14 +080071#define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
Huang Ying890ca9a2009-05-11 16:48:15 +080072
Avi Kivity0f65dd72011-04-20 13:37:53 +030073#define emul_to_vcpu(ctxt) \
74 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
75
Joerg Roedel50a37eb2008-01-31 14:57:38 +010076/* EFER defaults:
77 * - enable syscall per default because its emulated by KVM
78 * - enable LME and LMA per default on 64 bit KVM
79 */
80#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080081static
82u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010083#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080084static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010085#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020086
Avi Kivityba1389b2007-11-18 16:24:12 +020087#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
88#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050089
Gleb Natapovcb142eb2009-08-09 15:17:40 +030090static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +030091static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010092static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Avi Kivity674eea02008-02-11 18:37:23 +020093
Zhang Xiantao97896d02007-11-14 20:09:30 +080094struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030095EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool ignore_msrs = 0;
98module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +020099
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200100unsigned int min_timer_period_us = 500;
101module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
102
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300103static bool __read_mostly kvmclock_periodic_sync = true;
104module_param(kvmclock_periodic_sync, bool, S_IRUGO);
105
Joerg Roedel92a1f122011-03-25 09:44:51 +0100106bool kvm_has_tsc_control;
107EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
108u32 kvm_max_guest_tsc_khz;
109EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
110
Zachary Amsdencc578282012-02-03 15:43:50 -0200111/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
112static u32 tsc_tolerance_ppm = 250;
113module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
114
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500115/* lapic timer advance (tscdeadline mode only) in nanoseconds */
116unsigned int lapic_timer_advance_ns = 0;
117module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
118
Marcelo Tosatti16a96022014-05-14 12:43:24 -0300119static bool backwards_tsc_observed = false;
120
Avi Kivity18863bd2009-09-07 11:12:18 +0300121#define KVM_NR_SHARED_MSRS 16
122
123struct kvm_shared_msrs_global {
124 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800125 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300126};
127
128struct kvm_shared_msrs {
129 struct user_return_notifier urn;
130 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800131 struct kvm_shared_msr_values {
132 u64 host;
133 u64 curr;
134 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300135};
136
137static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200138static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300139
Hollis Blanchard417bc302007-10-31 17:24:23 -0500140struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200141 { "pf_fixed", VCPU_STAT(pf_fixed) },
142 { "pf_guest", VCPU_STAT(pf_guest) },
143 { "tlb_flush", VCPU_STAT(tlb_flush) },
144 { "invlpg", VCPU_STAT(invlpg) },
145 { "exits", VCPU_STAT(exits) },
146 { "io_exits", VCPU_STAT(io_exits) },
147 { "mmio_exits", VCPU_STAT(mmio_exits) },
148 { "signal_exits", VCPU_STAT(signal_exits) },
149 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800150 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200151 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100152 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200153 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200154 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530155 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200156 { "request_irq", VCPU_STAT(request_irq_exits) },
157 { "irq_exits", VCPU_STAT(irq_exits) },
158 { "host_state_reload", VCPU_STAT(host_state_reload) },
159 { "efer_reload", VCPU_STAT(efer_reload) },
160 { "fpu_reload", VCPU_STAT(fpu_reload) },
161 { "insn_emulation", VCPU_STAT(insn_emulation) },
162 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300163 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200164 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200165 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
166 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
167 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
168 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
169 { "mmu_flooded", VM_STAT(mmu_flooded) },
170 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200171 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300172 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200173 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300174 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500175 { NULL }
176};
177
Dexuan Cui2acf9232010-06-10 11:27:12 +0800178u64 __read_mostly host_xcr0;
179
Jan Kiszkab6785de2012-09-20 07:43:17 +0200180static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300181
Gleb Natapovaf585b92010-10-14 11:22:46 +0200182static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
183{
184 int i;
185 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
186 vcpu->arch.apf.gfns[i] = ~0;
187}
188
Avi Kivity18863bd2009-09-07 11:12:18 +0300189static void kvm_on_user_return(struct user_return_notifier *urn)
190{
191 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300192 struct kvm_shared_msrs *locals
193 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800194 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300195
196 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800197 values = &locals->values[slot];
198 if (values->host != values->curr) {
199 wrmsrl(shared_msrs_global.msrs[slot], values->host);
200 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300201 }
202 }
203 locals->registered = false;
204 user_return_notifier_unregister(urn);
205}
206
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800207static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300208{
Avi Kivity18863bd2009-09-07 11:12:18 +0300209 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200210 unsigned int cpu = smp_processor_id();
211 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300212
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800213 /* only read, and nobody should modify it at this time,
214 * so don't need lock */
215 if (slot >= shared_msrs_global.nr) {
216 printk(KERN_ERR "kvm: invalid MSR slot!");
217 return;
218 }
219 rdmsrl_safe(msr, &value);
220 smsr->values[slot].host = value;
221 smsr->values[slot].curr = value;
222}
223
224void kvm_define_shared_msr(unsigned slot, u32 msr)
225{
Nadav Amit0123be42014-07-24 15:06:56 +0300226 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200227 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300228 if (slot >= shared_msrs_global.nr)
229 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300230}
231EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
232
233static void kvm_shared_msr_cpu_online(void)
234{
235 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300236
237 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800238 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300239}
240
Andy Honig8b3c3102014-08-27 11:16:44 -0700241int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300242{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200243 unsigned int cpu = smp_processor_id();
244 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700245 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300246
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800247 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Andy Honig8b3c3102014-08-27 11:16:44 -0700248 return 0;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800249 smsr->values[slot].curr = value;
Andy Honig8b3c3102014-08-27 11:16:44 -0700250 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
251 if (err)
252 return 1;
253
Avi Kivity18863bd2009-09-07 11:12:18 +0300254 if (!smsr->registered) {
255 smsr->urn.on_user_return = kvm_on_user_return;
256 user_return_notifier_register(&smsr->urn);
257 smsr->registered = true;
258 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700259 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300260}
261EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
262
Radim Krčmář13a34e02014-08-28 15:13:03 +0200263static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200264{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200265 unsigned int cpu = smp_processor_id();
266 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200267
268 if (smsr->registered)
269 kvm_on_user_return(&smsr->urn);
270}
271
Carsten Otte6866b832007-10-29 16:09:10 +0100272u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
273{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300274 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100275}
276EXPORT_SYMBOL_GPL(kvm_get_apic_base);
277
Jan Kiszka58cb6282014-01-24 16:48:44 +0100278int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100279{
Jan Kiszka58cb6282014-01-24 16:48:44 +0100280 u64 old_state = vcpu->arch.apic_base &
281 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
282 u64 new_state = msr_info->data &
283 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
284 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
285 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
286
287 if (!msr_info->host_initiated &&
288 ((msr_info->data & reserved_bits) != 0 ||
289 new_state == X2APIC_ENABLE ||
290 (new_state == MSR_IA32_APICBASE_ENABLE &&
291 old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
292 (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
293 old_state == 0)))
294 return 1;
295
296 kvm_lapic_set_base(vcpu, msr_info->data);
297 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100298}
299EXPORT_SYMBOL_GPL(kvm_set_apic_base);
300
Andi Kleen2605fc22014-05-02 00:44:37 +0200301asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000302{
303 /* Fault while not rebooting. We want the trace. */
304 BUG();
305}
306EXPORT_SYMBOL_GPL(kvm_spurious_fault);
307
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200308#define EXCPT_BENIGN 0
309#define EXCPT_CONTRIBUTORY 1
310#define EXCPT_PF 2
311
312static int exception_class(int vector)
313{
314 switch (vector) {
315 case PF_VECTOR:
316 return EXCPT_PF;
317 case DE_VECTOR:
318 case TS_VECTOR:
319 case NP_VECTOR:
320 case SS_VECTOR:
321 case GP_VECTOR:
322 return EXCPT_CONTRIBUTORY;
323 default:
324 break;
325 }
326 return EXCPT_BENIGN;
327}
328
Nadav Amitd6e8c852014-07-24 14:51:24 +0300329#define EXCPT_FAULT 0
330#define EXCPT_TRAP 1
331#define EXCPT_ABORT 2
332#define EXCPT_INTERRUPT 3
333
334static int exception_type(int vector)
335{
336 unsigned int mask;
337
338 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
339 return EXCPT_INTERRUPT;
340
341 mask = 1 << vector;
342
343 /* #DB is trap, as instruction watchpoints are handled elsewhere */
344 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
345 return EXCPT_TRAP;
346
347 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
348 return EXCPT_ABORT;
349
350 /* Reserved exceptions will result in fault */
351 return EXCPT_FAULT;
352}
353
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200354static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200355 unsigned nr, bool has_error, u32 error_code,
356 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200357{
358 u32 prev_nr;
359 int class1, class2;
360
Avi Kivity3842d132010-07-27 12:30:24 +0300361 kvm_make_request(KVM_REQ_EVENT, vcpu);
362
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200363 if (!vcpu->arch.exception.pending) {
364 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200365 if (has_error && !is_protmode(vcpu))
366 has_error = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200367 vcpu->arch.exception.pending = true;
368 vcpu->arch.exception.has_error_code = has_error;
369 vcpu->arch.exception.nr = nr;
370 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200371 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200372 return;
373 }
374
375 /* to check exception */
376 prev_nr = vcpu->arch.exception.nr;
377 if (prev_nr == DF_VECTOR) {
378 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300379 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200380 return;
381 }
382 class1 = exception_class(prev_nr);
383 class2 = exception_class(nr);
384 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
385 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
386 /* generate double fault per SDM Table 5-5 */
387 vcpu->arch.exception.pending = true;
388 vcpu->arch.exception.has_error_code = true;
389 vcpu->arch.exception.nr = DF_VECTOR;
390 vcpu->arch.exception.error_code = 0;
391 } else
392 /* replace previous exception with a new one in a hope
393 that instruction re-execution will regenerate lost
394 exception */
395 goto queue;
396}
397
Avi Kivity298101d2007-11-25 13:41:11 +0200398void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
399{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200400 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200401}
402EXPORT_SYMBOL_GPL(kvm_queue_exception);
403
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200404void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
405{
406 kvm_multiple_exception(vcpu, nr, false, 0, true);
407}
408EXPORT_SYMBOL_GPL(kvm_requeue_exception);
409
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100410void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200411{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100412 if (err)
413 kvm_inject_gp(vcpu, 0);
414 else
415 kvm_x86_ops->skip_emulated_instruction(vcpu);
416}
417EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200418
Avi Kivity6389ee92010-11-29 16:12:30 +0200419void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200420{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200421 ++vcpu->stat.pf_guest;
Avi Kivity6389ee92010-11-29 16:12:30 +0200422 vcpu->arch.cr2 = fault->address;
423 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200424}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300425EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200426
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200427static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200428{
Avi Kivity6389ee92010-11-29 16:12:30 +0200429 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
430 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200431 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200432 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200433
434 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200435}
436
Sheng Yang3419ffc2008-05-15 09:52:48 +0800437void kvm_inject_nmi(struct kvm_vcpu *vcpu)
438{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300439 atomic_inc(&vcpu->arch.nmi_queued);
440 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800441}
442EXPORT_SYMBOL_GPL(kvm_inject_nmi);
443
Avi Kivity298101d2007-11-25 13:41:11 +0200444void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
445{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200446 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200447}
448EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
449
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200450void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
451{
452 kvm_multiple_exception(vcpu, nr, true, error_code, true);
453}
454EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
455
Carsten Ottea03490e2007-10-29 16:09:35 +0100456/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300457 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
458 * a #GP and return false.
459 */
460bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200461{
Avi Kivity0a79b002009-09-01 12:03:25 +0300462 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
463 return true;
464 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
465 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100466}
Avi Kivity0a79b002009-09-01 12:03:25 +0300467EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100468
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300469bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
470{
471 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
472 return true;
473
474 kvm_queue_exception(vcpu, UD_VECTOR);
475 return false;
476}
477EXPORT_SYMBOL_GPL(kvm_require_dr);
478
Carsten Ottea03490e2007-10-29 16:09:35 +0100479/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200480 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200481 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200482 * can read from guest physical or from the guest's guest physical memory.
483 */
484int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
485 gfn_t ngfn, void *data, int offset, int len,
486 u32 access)
487{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200488 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200489 gfn_t real_gfn;
490 gpa_t ngpa;
491
492 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200493 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200494 if (real_gfn == UNMAPPED_GVA)
495 return -EFAULT;
496
497 real_gfn = gpa_to_gfn(real_gfn);
498
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200499 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200500}
501EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
502
Fengguang Wu69b00492015-01-19 22:33:39 +0800503static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200504 void *data, int offset, int len, u32 access)
505{
506 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
507 data, offset, len, access);
508}
509
Joerg Roedelec92fe42010-09-10 17:30:51 +0200510/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100511 * Load the pae pdptrs. Return true is they are all valid.
512 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200513int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100514{
515 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
516 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
517 int i;
518 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200519 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100520
Joerg Roedelff03a072010-09-10 17:30:57 +0200521 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
522 offset * sizeof(u64), sizeof(pdpte),
523 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100524 if (ret < 0) {
525 ret = 0;
526 goto out;
527 }
528 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300529 if (is_present_gpte(pdpte[i]) &&
Xiao Guangronga0a64f52015-08-05 12:04:21 +0800530 (pdpte[i] &
531 vcpu->arch.mmu.guest_rsvd_check.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100532 ret = 0;
533 goto out;
534 }
535 }
536 ret = 1;
537
Joerg Roedelff03a072010-09-10 17:30:57 +0200538 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300539 __set_bit(VCPU_EXREG_PDPTR,
540 (unsigned long *)&vcpu->arch.regs_avail);
541 __set_bit(VCPU_EXREG_PDPTR,
542 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100543out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100544
545 return ret;
546}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100547EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100548
Avi Kivityd835dfe2007-11-21 02:57:59 +0200549static bool pdptrs_changed(struct kvm_vcpu *vcpu)
550{
Joerg Roedelff03a072010-09-10 17:30:57 +0200551 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200552 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200553 int offset;
554 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200555 int r;
556
557 if (is_long_mode(vcpu) || !is_pae(vcpu))
558 return false;
559
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300560 if (!test_bit(VCPU_EXREG_PDPTR,
561 (unsigned long *)&vcpu->arch.regs_avail))
562 return true;
563
Avi Kivity9f8fe502010-12-05 17:30:00 +0200564 gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
565 offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200566 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
567 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200568 if (r < 0)
569 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200570 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200571out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200572
573 return changed;
574}
575
Avi Kivity49a9b072010-06-10 17:02:14 +0300576int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100577{
Sheng Yangaad82702010-05-12 16:40:42 +0800578 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800579 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800580
Avi Kivityf9a48e62010-01-06 19:10:22 +0200581 cr0 |= X86_CR0_ET;
582
Gleb Natapovab344822010-01-21 15:28:46 +0200583#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300584 if (cr0 & 0xffffffff00000000UL)
585 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200586#endif
587
588 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100589
Gleb Natapov0f122442010-04-28 19:15:31 +0300590 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
591 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100592
Gleb Natapov0f122442010-04-28 19:15:31 +0300593 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
594 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100595
596 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
597#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200598 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100599 int cs_db, cs_l;
600
Gleb Natapov0f122442010-04-28 19:15:31 +0300601 if (!is_pae(vcpu))
602 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100603 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300604 if (cs_l)
605 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100606 } else
607#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200608 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200609 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300610 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100611 }
612
Mao, Junjiead756a12012-07-02 01:18:48 +0000613 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
614 return 1;
615
Carsten Ottea03490e2007-10-29 16:09:35 +0100616 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100617
Lai Jiangshand170c412011-02-21 11:21:30 +0800618 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800619 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800620 kvm_async_pf_hash_reset(vcpu);
621 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800622
Sheng Yangaad82702010-05-12 16:40:42 +0800623 if ((cr0 ^ old_cr0) & update_bits)
624 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800625
626 if ((cr0 ^ old_cr0) & X86_CR0_CD)
627 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
628
Gleb Natapov0f122442010-04-28 19:15:31 +0300629 return 0;
630}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200631EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100632
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200633void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100634{
Avi Kivity49a9b072010-06-10 17:02:14 +0300635 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100636}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200637EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100638
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300639static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
640{
641 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
642 !vcpu->guest_xcr0_loaded) {
643 /* kvm_set_xcr() also depends on this */
644 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
645 vcpu->guest_xcr0_loaded = 1;
646 }
647}
648
649static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
650{
651 if (vcpu->guest_xcr0_loaded) {
652 if (vcpu->arch.xcr0 != host_xcr0)
653 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
654 vcpu->guest_xcr0_loaded = 0;
655 }
656}
657
Fengguang Wu69b00492015-01-19 22:33:39 +0800658static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800659{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000660 u64 xcr0 = xcr;
661 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200662 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800663
664 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
665 if (index != XCR_XFEATURE_ENABLED_MASK)
666 return 1;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800667 if (!(xcr0 & XSTATE_FP))
668 return 1;
669 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
670 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200671
672 /*
673 * Do not allow the guest to set bits that we do not support
674 * saving. However, xcr0 bit 0 is always set, even if the
675 * emulated CPU does not support XSAVE (see fx_init).
676 */
677 valid_bits = vcpu->arch.guest_supported_xcr0 | XSTATE_FP;
678 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800679 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200680
Liu, Jinsong390bd522014-02-24 10:58:09 +0000681 if ((!(xcr0 & XSTATE_BNDREGS)) != (!(xcr0 & XSTATE_BNDCSR)))
682 return 1;
683
Chao Peng612263b2014-10-22 17:35:24 +0800684 if (xcr0 & XSTATE_AVX512) {
685 if (!(xcr0 & XSTATE_YMM))
686 return 1;
687 if ((xcr0 & XSTATE_AVX512) != XSTATE_AVX512)
688 return 1;
689 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300690 kvm_put_guest_xcr0(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800691 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000692
693 if ((xcr0 ^ old_xcr0) & XSTATE_EXTEND_MASK)
694 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800695 return 0;
696}
697
698int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
699{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000700 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
701 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800702 kvm_inject_gp(vcpu, 0);
703 return 1;
704 }
705 return 0;
706}
707EXPORT_SYMBOL_GPL(kvm_set_xcr);
708
Avi Kivitya83b29c2010-06-10 17:02:15 +0300709int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100710{
Avi Kivityfc78f512009-12-07 12:16:48 +0200711 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800712 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
713 X86_CR4_SMEP | X86_CR4_SMAP;
714
Gleb Natapov0f122442010-04-28 19:15:31 +0300715 if (cr4 & CR4_RESERVED_BITS)
716 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100717
Dexuan Cui2acf9232010-06-10 11:27:12 +0800718 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
719 return 1;
720
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800721 if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
722 return 1;
723
Feng Wu97ec8c02014-04-01 17:46:34 +0800724 if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
725 return 1;
726
H. Peter Anvinafcbf132013-04-27 16:37:47 -0700727 if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800728 return 1;
729
Carsten Ottea03490e2007-10-29 16:09:35 +0100730 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300731 if (!(cr4 & X86_CR4_PAE))
732 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300733 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
734 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200735 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
736 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300737 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100738
Mao, Junjiead756a12012-07-02 01:18:48 +0000739 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
740 if (!guest_cpuid_has_pcid(vcpu))
741 return 1;
742
743 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
744 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
745 return 1;
746 }
747
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300748 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300749 return 1;
750
Mao, Junjiead756a12012-07-02 01:18:48 +0000751 if (((cr4 ^ old_cr4) & pdptr_bits) ||
752 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800753 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300754
Dexuan Cui2acf9232010-06-10 11:27:12 +0800755 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +0200756 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800757
Gleb Natapov0f122442010-04-28 19:15:31 +0300758 return 0;
759}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200760EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100761
Avi Kivity23902182010-06-10 17:02:16 +0300762int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100763{
Paolo Bonziniac146232014-11-10 13:53:25 +0100764#ifdef CONFIG_X86_64
Nadav Amit9d88fca2014-11-02 11:54:52 +0200765 cr3 &= ~CR3_PCID_INVD;
Paolo Bonziniac146232014-11-10 13:53:25 +0100766#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200767
Avi Kivity9f8fe502010-12-05 17:30:00 +0200768 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300769 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -0400770 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300771 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200772 }
773
Carsten Ottea03490e2007-10-29 16:09:35 +0100774 if (is_long_mode(vcpu)) {
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200775 if (cr3 & CR3_L_MODE_RESERVED_BITS)
776 return 1;
777 } else if (is_pae(vcpu) && is_paging(vcpu) &&
778 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300779 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100780
Gleb Natapov0f122442010-04-28 19:15:31 +0300781 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200782 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Paolo Bonzinid8d173d2013-10-02 16:56:11 +0200783 kvm_mmu_new_cr3(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300784 return 0;
785}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200786EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100787
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100788int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100789{
Gleb Natapov0f122442010-04-28 19:15:31 +0300790 if (cr8 & CR8_RESERVED_BITS)
791 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200792 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100793 kvm_lapic_set_tpr(vcpu, cr8);
794 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800795 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300796 return 0;
797}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200798EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100799
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200800unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100801{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200802 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100803 return kvm_lapic_get_cr8(vcpu);
804 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800805 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100806}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200807EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100808
Nadav Amitae561ed2015-04-02 03:10:37 +0300809static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
810{
811 int i;
812
813 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
814 for (i = 0; i < KVM_NR_DB_REGS; i++)
815 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
816 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
817 }
818}
819
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100820static void kvm_update_dr6(struct kvm_vcpu *vcpu)
821{
822 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
823 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
824}
825
Jan Kiszkac8639012012-09-21 05:42:55 +0200826static void kvm_update_dr7(struct kvm_vcpu *vcpu)
827{
828 unsigned long dr7;
829
830 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
831 dr7 = vcpu->arch.guest_debug_dr7;
832 else
833 dr7 = vcpu->arch.dr7;
834 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100835 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
836 if (dr7 & DR7_BP_EN_MASK)
837 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200838}
839
Nadav Amit6f43ed02014-07-15 17:37:46 +0300840static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
841{
842 u64 fixed = DR6_FIXED_1;
843
844 if (!guest_cpuid_has_rtm(vcpu))
845 fixed |= DR6_RTM;
846 return fixed;
847}
848
Gleb Natapov338dbc92010-04-28 19:15:32 +0300849static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300850{
851 switch (dr) {
852 case 0 ... 3:
853 vcpu->arch.db[dr] = val;
854 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
855 vcpu->arch.eff_db[dr] = val;
856 break;
857 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300858 /* fall through */
859 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300860 if (val & 0xffffffff00000000ULL)
861 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300862 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100863 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300864 break;
865 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300866 /* fall through */
867 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300868 if (val & 0xffffffff00000000ULL)
869 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300870 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200871 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300872 break;
873 }
874
875 return 0;
876}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300877
878int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
879{
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300880 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +0300881 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300882 return 1;
883 }
884 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +0300885}
Gleb Natapov020df072010-04-13 10:05:23 +0300886EXPORT_SYMBOL_GPL(kvm_set_dr);
887
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300888int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300889{
890 switch (dr) {
891 case 0 ... 3:
892 *val = vcpu->arch.db[dr];
893 break;
894 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300895 /* fall through */
896 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100897 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
898 *val = vcpu->arch.dr6;
899 else
900 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300901 break;
902 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300903 /* fall through */
904 default: /* 7 */
905 *val = vcpu->arch.dr7;
906 break;
907 }
Gleb Natapov338dbc92010-04-28 19:15:32 +0300908 return 0;
909}
Gleb Natapov020df072010-04-13 10:05:23 +0300910EXPORT_SYMBOL_GPL(kvm_get_dr);
911
Avi Kivity022cd0e2011-11-10 14:57:23 +0200912bool kvm_rdpmc(struct kvm_vcpu *vcpu)
913{
914 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
915 u64 data;
916 int err;
917
Wei Huangc6702c92015-06-19 13:44:45 +0200918 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +0200919 if (err)
920 return err;
921 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
922 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
923 return err;
924}
925EXPORT_SYMBOL_GPL(kvm_rdpmc);
926
Carsten Otte043405e2007-10-10 17:16:19 +0200927/*
928 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
929 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
930 *
931 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400932 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200933 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
934 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +0200935 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400936
Carsten Otte043405e2007-10-10 17:16:19 +0200937static u32 msrs_to_save[] = {
938 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400939 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200940#ifdef CONFIG_X86_64
941 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
942#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800943 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Liu, Jinsong0dd376e2014-02-24 10:56:53 +0000944 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS
Carsten Otte043405e2007-10-10 17:16:19 +0200945};
946
947static unsigned num_msrs_to_save;
948
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200949static u32 emulated_msrs[] = {
950 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
951 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
952 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
953 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Andrey Smetanine7d95132015-07-03 15:01:37 +0300954 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
955 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300956 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +0300957 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300958 HV_X64_MSR_VP_RUNTIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200959 HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
960 MSR_KVM_PV_EOI_EN,
961
Will Auldba904632012-11-29 12:42:50 -0800962 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800963 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +0200964 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300965 MSR_IA32_MCG_STATUS,
966 MSR_IA32_MCG_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +0200967 MSR_IA32_SMBASE,
Carsten Otte043405e2007-10-10 17:16:19 +0200968};
969
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200970static unsigned num_emulated_msrs;
971
Jan Kiszka384bb782013-04-20 10:52:36 +0200972bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100973{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200974 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +0200975 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +0100976
Alexander Graf1b2fd702009-02-02 16:23:51 +0100977 if (efer & EFER_FFXSR) {
978 struct kvm_cpuid_entry2 *feat;
979
980 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200981 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200982 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +0100983 }
984
Alexander Grafd8017472008-11-25 20:17:11 +0100985 if (efer & EFER_SVME) {
986 struct kvm_cpuid_entry2 *feat;
987
988 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200989 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
Jan Kiszka384bb782013-04-20 10:52:36 +0200990 return false;
Alexander Grafd8017472008-11-25 20:17:11 +0100991 }
992
Jan Kiszka384bb782013-04-20 10:52:36 +0200993 return true;
994}
995EXPORT_SYMBOL_GPL(kvm_valid_efer);
996
997static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
998{
999 u64 old_efer = vcpu->arch.efer;
1000
1001 if (!kvm_valid_efer(vcpu, efer))
1002 return 1;
1003
1004 if (is_paging(vcpu)
1005 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1006 return 1;
1007
Carsten Otte15c4a642007-10-30 18:44:17 +01001008 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001009 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001010
Sheng Yanga3d204e2010-05-12 16:40:40 +08001011 kvm_x86_ops->set_efer(vcpu, efer);
1012
Sheng Yangaad82702010-05-12 16:40:42 +08001013 /* Update reserved bits */
1014 if ((efer ^ old_efer) & EFER_NX)
1015 kvm_mmu_reset_context(vcpu);
1016
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001017 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001018}
1019
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001020void kvm_enable_efer_bits(u64 mask)
1021{
1022 efer_reserved_bits &= ~mask;
1023}
1024EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1025
Carsten Otte15c4a642007-10-30 18:44:17 +01001026/*
1027 * Writes msr value into into the appropriate "register".
1028 * Returns 0 on success, non-0 otherwise.
1029 * Assumes vcpu_load() was already called.
1030 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001031int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01001032{
Nadav Amit854e8bb2014-09-16 03:24:05 +03001033 switch (msr->index) {
1034 case MSR_FS_BASE:
1035 case MSR_GS_BASE:
1036 case MSR_KERNEL_GS_BASE:
1037 case MSR_CSTAR:
1038 case MSR_LSTAR:
1039 if (is_noncanonical_address(msr->data))
1040 return 1;
1041 break;
1042 case MSR_IA32_SYSENTER_EIP:
1043 case MSR_IA32_SYSENTER_ESP:
1044 /*
1045 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1046 * non-canonical address is written on Intel but not on
1047 * AMD (which ignores the top 32-bits, because it does
1048 * not implement 64-bit SYSENTER).
1049 *
1050 * 64-bit code should hence be able to write a non-canonical
1051 * value on AMD. Making the address canonical ensures that
1052 * vmentry does not fail on Intel after writing a non-canonical
1053 * value, and that something deterministic happens if the guest
1054 * invokes 64-bit SYSENTER.
1055 */
1056 msr->data = get_canonical(msr->data);
1057 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001058 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001059}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001060EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001061
Carsten Otte313a3dc2007-10-11 19:16:52 +02001062/*
1063 * Adapt set_msr() to msr_io()'s calling convention
1064 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001065static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1066{
1067 struct msr_data msr;
1068 int r;
1069
1070 msr.index = index;
1071 msr.host_initiated = true;
1072 r = kvm_get_msr(vcpu, &msr);
1073 if (r)
1074 return r;
1075
1076 *data = msr.data;
1077 return 0;
1078}
1079
Carsten Otte313a3dc2007-10-11 19:16:52 +02001080static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1081{
Will Auld8fe8ab42012-11-29 12:42:12 -08001082 struct msr_data msr;
1083
1084 msr.data = *data;
1085 msr.index = index;
1086 msr.host_initiated = true;
1087 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001088}
1089
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001090#ifdef CONFIG_X86_64
1091struct pvclock_gtod_data {
1092 seqcount_t seq;
1093
1094 struct { /* extract of a clocksource struct */
1095 int vclock_mode;
1096 cycle_t cycle_last;
1097 cycle_t mask;
1098 u32 mult;
1099 u32 shift;
1100 } clock;
1101
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001102 u64 boot_ns;
1103 u64 nsec_base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001104};
1105
1106static struct pvclock_gtod_data pvclock_gtod_data;
1107
1108static void update_pvclock_gtod(struct timekeeper *tk)
1109{
1110 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001111 u64 boot_ns;
1112
Peter Zijlstra876e7882015-03-19 10:09:06 +01001113 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001114
1115 write_seqcount_begin(&vdata->seq);
1116
1117 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001118 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1119 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1120 vdata->clock.mask = tk->tkr_mono.mask;
1121 vdata->clock.mult = tk->tkr_mono.mult;
1122 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001123
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001124 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001125 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001126
1127 write_seqcount_end(&vdata->seq);
1128}
1129#endif
1130
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001131void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1132{
1133 /*
1134 * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
1135 * vcpu_enter_guest. This function is only called from
1136 * the physical CPU that is running vcpu.
1137 */
1138 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1139}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001140
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001141static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1142{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001143 int version;
1144 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001145 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +08001146 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001147
1148 if (!wall_clock)
1149 return;
1150
Avi Kivity9ed3c442010-05-04 15:00:37 +03001151 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1152 if (r)
1153 return;
1154
1155 if (version & 1)
1156 ++version; /* first time write, random junk */
1157
1158 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001159
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001160 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
1161
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001162 /*
1163 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001164 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001165 * wall clock specified here. guest system time equals host
1166 * system time for us, thus we must fill in host boot time here.
1167 */
Jason Wang923de3c2010-01-27 19:13:49 +08001168 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001169
Bruce Rogers4b648662012-07-20 10:44:24 -06001170 if (kvm->arch.kvmclock_offset) {
1171 struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
1172 boot = timespec_sub(boot, ts);
1173 }
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001174 wc.sec = boot.tv_sec;
1175 wc.nsec = boot.tv_nsec;
1176 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001177
1178 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1179
1180 version++;
1181 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001182}
1183
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001184static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1185{
1186 uint32_t quotient, remainder;
1187
1188 /* Don't try to replace with do_div(), this one calculates
1189 * "(dividend << 32) / divisor" */
1190 __asm__ ( "divl %4"
1191 : "=a" (quotient), "=d" (remainder)
1192 : "0" (0), "1" (dividend), "r" (divisor) );
1193 return quotient;
1194}
1195
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001196static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
1197 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001198{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001199 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001200 int32_t shift = 0;
1201 uint64_t tps64;
1202 uint32_t tps32;
1203
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001204 tps64 = base_khz * 1000LL;
1205 scaled64 = scaled_khz * 1000LL;
Jan Kiszka50933622010-09-26 13:00:53 +02001206 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001207 tps64 >>= 1;
1208 shift--;
1209 }
1210
1211 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001212 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1213 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001214 scaled64 >>= 1;
1215 else
1216 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001217 shift++;
1218 }
1219
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001220 *pshift = shift;
1221 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001222
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001223 pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
1224 __func__, base_khz, scaled_khz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001225}
1226
Marcelo Tosattid8281992012-11-27 23:29:01 -02001227#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001228static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001229#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001230
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001231static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001232static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001233
Joerg Roedel857e4092011-03-25 09:44:50 +01001234static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Zachary Amsden759379d2010-08-19 22:07:25 -10001235{
Zachary Amsdencc578282012-02-03 15:43:50 -02001236 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
1237 vcpu->arch.virtual_tsc_shift);
Zachary Amsden759379d2010-08-19 22:07:25 -10001238}
1239
Zachary Amsdencc578282012-02-03 15:43:50 -02001240static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001241{
Zachary Amsdencc578282012-02-03 15:43:50 -02001242 u64 v = (u64)khz * (1000000 + ppm);
1243 do_div(v, 1000000);
1244 return v;
1245}
1246
1247static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
1248{
1249 u32 thresh_lo, thresh_hi;
1250 int use_scaling = 0;
1251
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001252 /* tsc_khz can be zero if TSC calibration fails */
1253 if (this_tsc_khz == 0)
1254 return;
1255
Zachary Amsdenc2855452010-09-18 14:38:15 -10001256 /* Compute a scale to convert nanoseconds in TSC cycles */
1257 kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
Zachary Amsdencc578282012-02-03 15:43:50 -02001258 &vcpu->arch.virtual_tsc_shift,
1259 &vcpu->arch.virtual_tsc_mult);
1260 vcpu->arch.virtual_tsc_khz = this_tsc_khz;
1261
1262 /*
1263 * Compute the variation in TSC rate which is acceptable
1264 * within the range of tolerance and decide if the
1265 * rate being applied is within that bounds of the hardware
1266 * rate. If so, no scaling or compensation need be done.
1267 */
1268 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1269 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
1270 if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
1271 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
1272 use_scaling = 1;
1273 }
1274 kvm_x86_ops->set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001275}
1276
1277static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1278{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001279 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001280 vcpu->arch.virtual_tsc_mult,
1281 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001282 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001283 return tsc;
1284}
1285
Fengguang Wu69b00492015-01-19 22:33:39 +08001286static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001287{
1288#ifdef CONFIG_X86_64
1289 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001290 struct kvm_arch *ka = &vcpu->kvm->arch;
1291 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1292
1293 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1294 atomic_read(&vcpu->kvm->online_vcpus));
1295
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001296 /*
1297 * Once the masterclock is enabled, always perform request in
1298 * order to update it.
1299 *
1300 * In order to enable masterclock, the host clocksource must be TSC
1301 * and the vcpus need to have matched TSCs. When that happens,
1302 * perform request to enable masterclock.
1303 */
1304 if (ka->use_master_clock ||
1305 (gtod->clock.vclock_mode == VCLOCK_TSC && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001306 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1307
1308 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1309 atomic_read(&vcpu->kvm->online_vcpus),
1310 ka->use_master_clock, gtod->clock.vclock_mode);
1311#endif
1312}
1313
Will Auldba904632012-11-29 12:42:50 -08001314static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1315{
1316 u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
1317 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1318}
1319
Will Auld8fe8ab42012-11-29 12:42:12 -08001320void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001321{
1322 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001323 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001324 unsigned long flags;
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001325 s64 usdiff;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001326 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001327 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001328 u64 data = msr->data;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001329
Jan Kiszka038f8c12011-02-04 10:49:11 +01001330 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Joerg Roedel857e4092011-03-25 09:44:50 +01001331 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001332 ns = get_kernel_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001333 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001334
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001335 if (vcpu->arch.virtual_tsc_khz) {
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001336 int faulted = 0;
1337
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001338 /* n.b - signed multiplication and division required */
1339 usdiff = data - kvm->arch.last_tsc_write;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001340#ifdef CONFIG_X86_64
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001341 usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001342#else
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001343 /* do_div() only does unsigned */
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001344 asm("1: idivl %[divisor]\n"
1345 "2: xor %%edx, %%edx\n"
1346 " movl $0, %[faulted]\n"
1347 "3:\n"
1348 ".section .fixup,\"ax\"\n"
1349 "4: movl $1, %[faulted]\n"
1350 " jmp 3b\n"
1351 ".previous\n"
1352
1353 _ASM_EXTABLE(1b, 4b)
1354
1355 : "=A"(usdiff), [faulted] "=r" (faulted)
1356 : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
1357
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001358#endif
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001359 do_div(elapsed, 1000);
1360 usdiff -= elapsed;
1361 if (usdiff < 0)
1362 usdiff = -usdiff;
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001363
1364 /* idivl overflow => difference is larger than USEC_PER_SEC */
1365 if (faulted)
1366 usdiff = USEC_PER_SEC;
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001367 } else
1368 usdiff = USEC_PER_SEC; /* disable TSC match window below */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001369
1370 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001371 * Special case: TSC write with a small delta (1 second) of virtual
1372 * cycle time against real time is interpreted as an attempt to
1373 * synchronize the CPU.
1374 *
1375 * For a reliable TSC, we can match TSC offsets, and for an unstable
1376 * TSC, we add elapsed time in this computation. We could let the
1377 * compensation code attempt to catch up if we fall behind, but
1378 * it's better to try to match offsets from the beginning.
1379 */
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001380 if (usdiff < USEC_PER_SEC &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001381 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001382 if (!check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001383 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001384 pr_debug("kvm: matched tsc offset for %llu\n", data);
1385 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001386 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001387 data += delta;
1388 offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001389 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001390 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001391 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001392 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001393 } else {
1394 /*
1395 * We split periods of matched TSC writes into generations.
1396 * For each generation, we track the original measured
1397 * nanosecond time, offset, and write, so if TSCs are in
1398 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001399 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001400 *
1401 * These values are tracked in kvm->arch.cur_xxx variables.
1402 */
1403 kvm->arch.cur_tsc_generation++;
1404 kvm->arch.cur_tsc_nsec = ns;
1405 kvm->arch.cur_tsc_write = data;
1406 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001407 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001408 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001409 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001410 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001411
1412 /*
1413 * We also track th most recent recorded KHZ, write and time to
1414 * allow the matching interval to be extended at each write.
1415 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001416 kvm->arch.last_tsc_nsec = ns;
1417 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001418 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001419
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001420 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001421
1422 /* Keep track of which generation this VCPU has synchronized to */
1423 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1424 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1425 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1426
Will Auldba904632012-11-29 12:42:50 -08001427 if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
1428 update_ia32_tsc_adjust_msr(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001429 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1430 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001431
1432 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001433 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001434 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001435 } else if (!already_matched) {
1436 kvm->arch.nr_vcpus_matched_tsc++;
1437 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001438
1439 kvm_track_tsc_matching(vcpu);
1440 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001441}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001442
Zachary Amsden99e3e302010-08-19 22:07:17 -10001443EXPORT_SYMBOL_GPL(kvm_write_tsc);
1444
Marcelo Tosattid8281992012-11-27 23:29:01 -02001445#ifdef CONFIG_X86_64
1446
1447static cycle_t read_tsc(void)
1448{
Andy Lutomirski03b97302015-06-25 18:44:08 +02001449 cycle_t ret = (cycle_t)rdtsc_ordered();
1450 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001451
1452 if (likely(ret >= last))
1453 return ret;
1454
1455 /*
1456 * GCC likes to generate cmov here, but this branch is extremely
1457 * predictable (it's just a funciton of time and the likely is
1458 * very likely) and there's a data dependence, so force GCC
1459 * to generate a branch instead. I don't barrier() because
1460 * we don't actually need a barrier, and if this function
1461 * ever gets inlined it will generate worse code.
1462 */
1463 asm volatile ("");
1464 return last;
1465}
1466
1467static inline u64 vgettsc(cycle_t *cycle_now)
1468{
1469 long v;
1470 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1471
1472 *cycle_now = read_tsc();
1473
1474 v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
1475 return v * gtod->clock.mult;
1476}
1477
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001478static int do_monotonic_boot(s64 *t, cycle_t *cycle_now)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001479{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001480 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001481 unsigned long seq;
1482 int mode;
1483 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001484
Marcelo Tosattid8281992012-11-27 23:29:01 -02001485 do {
1486 seq = read_seqcount_begin(&gtod->seq);
1487 mode = gtod->clock.vclock_mode;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001488 ns = gtod->nsec_base;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001489 ns += vgettsc(cycle_now);
1490 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001491 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001492 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001493 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001494
1495 return mode;
1496}
1497
1498/* returns true if host is using tsc clocksource */
1499static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
1500{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001501 /* checked again under seqlock below */
1502 if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
1503 return false;
1504
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001505 return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001506}
1507#endif
1508
1509/*
1510 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001511 * Assuming a stable TSC across physical CPUS, and a stable TSC
1512 * across virtual CPUs, the following condition is possible.
1513 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001514 * CPUs at the next numbered event.
1515 *
1516 * "timespecX" represents host monotonic time. "tscX" represents
1517 * RDTSC value.
1518 *
1519 * VCPU0 on CPU0 | VCPU1 on CPU1
1520 *
1521 * 1. read timespec0,tsc0
1522 * 2. | timespec1 = timespec0 + N
1523 * | tsc1 = tsc0 + M
1524 * 3. transition to guest | transition to guest
1525 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1526 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1527 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1528 *
1529 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1530 *
1531 * - ret0 < ret1
1532 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1533 * ...
1534 * - 0 < N - M => M < N
1535 *
1536 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1537 * always the case (the difference between two distinct xtime instances
1538 * might be smaller then the difference between corresponding TSC reads,
1539 * when updating guest vcpus pvclock areas).
1540 *
1541 * To avoid that problem, do not allow visibility of distinct
1542 * system_timestamp/tsc_timestamp values simultaneously: use a master
1543 * copy of host monotonic time values. Update that master copy
1544 * in lockstep.
1545 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001546 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001547 *
1548 */
1549
1550static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1551{
1552#ifdef CONFIG_X86_64
1553 struct kvm_arch *ka = &kvm->arch;
1554 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001555 bool host_tsc_clocksource, vcpus_matched;
1556
1557 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1558 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001559
1560 /*
1561 * If the host uses TSC clock, then passthrough TSC as stable
1562 * to the guest.
1563 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001564 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001565 &ka->master_kernel_ns,
1566 &ka->master_cycle_now);
1567
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001568 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Marcelo Tosatti54750f22015-01-20 15:54:52 -02001569 && !backwards_tsc_observed
1570 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001571
Marcelo Tosattid8281992012-11-27 23:29:01 -02001572 if (ka->use_master_clock)
1573 atomic_set(&kvm_guest_has_master_clock, 1);
1574
1575 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001576 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1577 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001578#endif
1579}
1580
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001581static void kvm_gen_update_masterclock(struct kvm *kvm)
1582{
1583#ifdef CONFIG_X86_64
1584 int i;
1585 struct kvm_vcpu *vcpu;
1586 struct kvm_arch *ka = &kvm->arch;
1587
1588 spin_lock(&ka->pvclock_gtod_sync_lock);
1589 kvm_make_mclock_inprogress_request(kvm);
1590 /* no guest entries from this point */
1591 pvclock_update_vm_gtod_copy(kvm);
1592
1593 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001594 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001595
1596 /* guest entries allowed */
1597 kvm_for_each_vcpu(i, vcpu, kvm)
1598 clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
1599
1600 spin_unlock(&ka->pvclock_gtod_sync_lock);
1601#endif
1602}
1603
Zachary Amsden34c238a2010-09-18 14:38:14 -10001604static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001605{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001606 unsigned long flags, this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001607 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001608 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02001609 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001610 u64 tsc_timestamp, host_tsc;
Andy Honig0b794592013-02-20 14:48:10 -08001611 struct pvclock_vcpu_time_info guest_hv_clock;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001612 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001613 bool use_master_clock;
1614
1615 kernel_ns = 0;
1616 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001617
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001618 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02001619 * If the host uses TSC clock, then passthrough TSC as stable
1620 * to the guest.
1621 */
1622 spin_lock(&ka->pvclock_gtod_sync_lock);
1623 use_master_clock = ka->use_master_clock;
1624 if (use_master_clock) {
1625 host_tsc = ka->master_cycle_now;
1626 kernel_ns = ka->master_kernel_ns;
1627 }
1628 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001629
1630 /* Keep irq disabled to prevent changes to the clock */
1631 local_irq_save(flags);
Christoph Lameter89cbc762014-08-17 12:30:40 -05001632 this_tsc_khz = __this_cpu_read(cpu_tsc_khz);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001633 if (unlikely(this_tsc_khz == 0)) {
1634 local_irq_restore(flags);
1635 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
1636 return 1;
1637 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001638 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001639 host_tsc = rdtsc();
Marcelo Tosattid8281992012-11-27 23:29:01 -02001640 kernel_ns = get_kernel_ns();
1641 }
1642
1643 tsc_timestamp = kvm_x86_ops->read_l1_tsc(v, host_tsc);
1644
1645 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10001646 * We may have to catch up the TSC to match elapsed wall clock
1647 * time for two reasons, even if kvmclock is used.
1648 * 1) CPU could have been running below the maximum TSC rate
1649 * 2) Broken TSC compensation resets the base at each VCPU
1650 * entry to avoid unknown leaps of TSC even when running
1651 * again on the same CPU. This may cause apparent elapsed
1652 * time to disappear, and the guest to stand still or run
1653 * very slowly.
1654 */
1655 if (vcpu->tsc_catchup) {
1656 u64 tsc = compute_guest_tsc(v, kernel_ns);
1657 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001658 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001659 tsc_timestamp = tsc;
1660 }
1661 }
1662
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001663 local_irq_restore(flags);
1664
Andy Honig0b794592013-02-20 14:48:10 -08001665 if (!vcpu->pv_time_enabled)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001666 return 0;
1667
Zachary Amsdene48672f2010-08-19 22:07:23 -10001668 if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001669 kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
1670 &vcpu->hv_clock.tsc_shift,
1671 &vcpu->hv_clock.tsc_to_system_mul);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001672 vcpu->hw_tsc_khz = this_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001673 }
1674
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001675 /* With all the info we got, fill in the values */
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001676 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10001677 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10001678 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001679
Owen Hofmann09a0c3f2014-11-03 16:57:18 -08001680 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1681 &guest_hv_clock, sizeof(guest_hv_clock))))
1682 return 0;
1683
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001684 /* This VCPU is paused, but it's legal for a guest to read another
1685 * VCPU's kvmclock, so we really have to follow the specification where
1686 * it says that version is odd if data is being modified, and even after
1687 * it is consistent.
1688 *
1689 * Version field updates must be kept separate. This is because
1690 * kvm_write_guest_cached might use a "rep movs" instruction, and
1691 * writes within a string instruction are weakly ordered. So there
1692 * are three writes overall.
1693 *
1694 * As a small optimization, only write the version field in the first
1695 * and third write. The vcpu->pv_time cache is still valid, because the
1696 * version field is the first in the struct.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001697 */
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001698 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
1699
1700 vcpu->hv_clock.version = guest_hv_clock.version + 1;
1701 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1702 &vcpu->hv_clock,
1703 sizeof(vcpu->hv_clock.version));
1704
1705 smp_wmb();
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001706
1707 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
Andy Honig0b794592013-02-20 14:48:10 -08001708 pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001709
1710 if (vcpu->pvclock_set_guest_stopped_request) {
1711 pvclock_flags |= PVCLOCK_GUEST_STOPPED;
1712 vcpu->pvclock_set_guest_stopped_request = false;
1713 }
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 Tosatti0061d53d2013-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 Tosatti0061d53d2013-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 Tosatti0061d53d2013-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 Tosatti0061d53d2013-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 Tosatti0061d53d2013-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 Natapov344d9582010-10-14 11:22:50 +02001860static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
1861{
1862 gpa_t gpa = data & ~0x3f;
1863
Guo Chao4a969982012-06-28 15:17:27 +08001864 /* Bits 2:5 are reserved, Should be zero */
Gleb Natapov6adba522010-10-14 11:22:55 +02001865 if (data & 0x3c)
Gleb Natapov344d9582010-10-14 11:22:50 +02001866 return 1;
1867
1868 vcpu->arch.apf.msr_val = data;
1869
1870 if (!(data & KVM_ASYNC_PF_ENABLED)) {
1871 kvm_clear_async_pf_completion_queue(vcpu);
1872 kvm_async_pf_hash_reset(vcpu);
1873 return 0;
1874 }
1875
Andrew Honig8f964522013-03-29 09:35:21 -07001876 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
1877 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02001878 return 1;
1879
Gleb Natapov6adba522010-10-14 11:22:55 +02001880 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Gleb Natapov344d9582010-10-14 11:22:50 +02001881 kvm_async_pf_wakeup_all(vcpu);
1882 return 0;
1883}
1884
Glauber Costa12f9a482011-02-01 14:16:40 -05001885static void kvmclock_reset(struct kvm_vcpu *vcpu)
1886{
Andy Honig0b794592013-02-20 14:48:10 -08001887 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05001888}
1889
Glauber Costac9aaa892011-07-11 15:28:14 -04001890static void accumulate_steal_time(struct kvm_vcpu *vcpu)
1891{
1892 u64 delta;
1893
1894 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
1895 return;
1896
1897 delta = current->sched_info.run_delay - vcpu->arch.st.last_steal;
1898 vcpu->arch.st.last_steal = current->sched_info.run_delay;
1899 vcpu->arch.st.accum_steal = delta;
1900}
1901
1902static void record_steal_time(struct kvm_vcpu *vcpu)
1903{
1904 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
1905 return;
1906
1907 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
1908 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
1909 return;
1910
1911 vcpu->arch.st.steal.steal += vcpu->arch.st.accum_steal;
1912 vcpu->arch.st.steal.version += 2;
1913 vcpu->arch.st.accum_steal = 0;
1914
1915 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
1916 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
1917}
1918
Will Auld8fe8ab42012-11-29 12:42:12 -08001919int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01001920{
Gleb Natapov57537852012-01-15 14:17:22 +02001921 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08001922 u32 msr = msr_info->index;
1923 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02001924
Carsten Otte15c4a642007-10-30 18:44:17 +01001925 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01001926 case MSR_AMD64_NB_CFG:
1927 case MSR_IA32_UCODE_REV:
1928 case MSR_IA32_UCODE_WRITE:
1929 case MSR_VM_HSAVE_PA:
1930 case MSR_AMD64_PATCH_LOADER:
1931 case MSR_AMD64_BU_CFG2:
1932 break;
1933
Carsten Otte15c4a642007-10-30 18:44:17 +01001934 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001935 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02001936 case MSR_K7_HWCR:
1937 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01001938 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08001939 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02001940 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02001941 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03001942 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1943 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02001944 return 1;
1945 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001946 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001947 case MSR_FAM10H_MMIO_CONF_BASE:
1948 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03001949 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1950 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001951 return 1;
1952 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001953 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001954 case MSR_IA32_DEBUGCTLMSR:
1955 if (!data) {
1956 /* We support the non-activated case already */
1957 break;
1958 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1959 /* Values other than LBR and BTF are vendor-specific,
1960 thus reserved and should throw a #GP */
1961 return 1;
1962 }
Christoffer Dalla737f252012-06-03 21:17:48 +03001963 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1964 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001965 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001966 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08001967 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001968 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01001969 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001970 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1971 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001972 case MSR_IA32_TSCDEADLINE:
1973 kvm_set_lapic_tscdeadline_msr(vcpu, data);
1974 break;
Will Auldba904632012-11-29 12:42:50 -08001975 case MSR_IA32_TSC_ADJUST:
1976 if (guest_cpuid_has_tsc_adjust(vcpu)) {
1977 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06001978 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08001979 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08001980 }
1981 vcpu->arch.ia32_tsc_adjust_msr = data;
1982 }
1983 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001984 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001985 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001986 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02001987 case MSR_IA32_SMBASE:
1988 if (!msr_info->host_initiated)
1989 return 1;
1990 vcpu->arch.smbase = data;
1991 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04001992 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001993 case MSR_KVM_WALL_CLOCK:
1994 vcpu->kvm->arch.wall_clock = data;
1995 kvm_write_wall_clock(vcpu->kvm, data);
1996 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04001997 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001998 case MSR_KVM_SYSTEM_TIME: {
Andy Honig0b794592013-02-20 14:48:10 -08001999 u64 gpa_offset;
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002000 struct kvm_arch *ka = &vcpu->kvm->arch;
2001
Glauber Costa12f9a482011-02-01 14:16:40 -05002002 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002003
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002004 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2005 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2006
2007 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
2008 set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
2009 &vcpu->requests);
2010
2011 ka->boot_vcpu_runs_old_kvmclock = tmp;
2012 }
2013
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002014 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002015 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002016
2017 /* we verify if the enable bit is set... */
2018 if (!(data & 1))
2019 break;
2020
Andy Honig0b794592013-02-20 14:48:10 -08002021 gpa_offset = data & ~(PAGE_MASK | 1);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002022
Andy Honig0b794592013-02-20 14:48:10 -08002023 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002024 &vcpu->arch.pv_time, data & ~1ULL,
2025 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002026 vcpu->arch.pv_time_enabled = false;
2027 else
2028 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002029
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002030 break;
2031 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002032 case MSR_KVM_ASYNC_PF_EN:
2033 if (kvm_pv_enable_async_pf(vcpu, data))
2034 return 1;
2035 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002036 case MSR_KVM_STEAL_TIME:
2037
2038 if (unlikely(!sched_info_on()))
2039 return 1;
2040
2041 if (data & KVM_STEAL_RESERVED_MASK)
2042 return 1;
2043
2044 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002045 data & KVM_STEAL_VALID_BITS,
2046 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002047 return 1;
2048
2049 vcpu->arch.st.msr_val = data;
2050
2051 if (!(data & KVM_MSR_ENABLED))
2052 break;
2053
2054 vcpu->arch.st.last_steal = current->sched_info.run_delay;
2055
2056 preempt_disable();
2057 accumulate_steal_time(vcpu);
2058 preempt_enable();
2059
2060 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2061
2062 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002063 case MSR_KVM_PV_EOI_EN:
2064 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2065 return 1;
2066 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002067
Huang Ying890ca9a2009-05-11 16:48:15 +08002068 case MSR_IA32_MCG_CTL:
2069 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002070 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Huang Ying890ca9a2009-05-11 16:48:15 +08002071 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002072
Wei Huang6912ac32015-06-12 01:34:56 -04002073 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2074 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2075 pr = true; /* fall through */
2076 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2077 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002078 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002079 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002080
2081 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002082 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2083 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002084 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002085 case MSR_K7_CLK_CTL:
2086 /*
2087 * Ignore all writes to this no longer documented MSR.
2088 * Writes are only relevant for old K7 processors,
2089 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002090 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002091 * affected processor models on the command line, hence
2092 * the need to ignore the workaround.
2093 */
2094 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002095 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002096 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2097 case HV_X64_MSR_CRASH_CTL:
2098 return kvm_hv_set_msr_common(vcpu, msr, data,
2099 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002100 case MSR_IA32_BBL_CR_CTL3:
2101 /* Drop writes to this legacy MSR -- see rdmsr
2102 * counterpart for further detail.
2103 */
Christoffer Dalla737f252012-06-03 21:17:48 +03002104 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002105 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002106 case MSR_AMD64_OSVW_ID_LENGTH:
2107 if (!guest_cpuid_has_osvw(vcpu))
2108 return 1;
2109 vcpu->arch.osvw.length = data;
2110 break;
2111 case MSR_AMD64_OSVW_STATUS:
2112 if (!guest_cpuid_has_osvw(vcpu))
2113 return 1;
2114 vcpu->arch.osvw.status = data;
2115 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002116 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002117 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2118 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002119 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002120 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002121 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002122 vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
2123 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002124 return 1;
2125 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002126 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
2127 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002128 break;
2129 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002130 }
2131 return 0;
2132}
2133EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2134
2135
2136/*
2137 * Reads an msr value (of 'msr_index') into 'pdata'.
2138 * Returns 0 on success, non-0 otherwise.
2139 * Assumes vcpu_load() was already called.
2140 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002141int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01002142{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002143 return kvm_x86_ops->get_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002144}
Wincy Vanff651cb2014-12-11 08:52:58 +03002145EXPORT_SYMBOL_GPL(kvm_get_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002146
Huang Ying890ca9a2009-05-11 16:48:15 +08002147static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2148{
2149 u64 data;
2150 u64 mcg_cap = vcpu->arch.mcg_cap;
2151 unsigned bank_num = mcg_cap & 0xff;
2152
2153 switch (msr) {
2154 case MSR_IA32_P5_MC_ADDR:
2155 case MSR_IA32_P5_MC_TYPE:
2156 data = 0;
2157 break;
2158 case MSR_IA32_MCG_CAP:
2159 data = vcpu->arch.mcg_cap;
2160 break;
2161 case MSR_IA32_MCG_CTL:
2162 if (!(mcg_cap & MCG_CTL_P))
2163 return 1;
2164 data = vcpu->arch.mcg_ctl;
2165 break;
2166 case MSR_IA32_MCG_STATUS:
2167 data = vcpu->arch.mcg_status;
2168 break;
2169 default:
2170 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002171 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002172 u32 offset = msr - MSR_IA32_MC0_CTL;
2173 data = vcpu->arch.mce_banks[offset];
2174 break;
2175 }
2176 return 1;
2177 }
2178 *pdata = data;
2179 return 0;
2180}
2181
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002182int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002183{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002184 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002185 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002186 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002187 case MSR_IA32_DEBUGCTLMSR:
2188 case MSR_IA32_LASTBRANCHFROMIP:
2189 case MSR_IA32_LASTBRANCHTOIP:
2190 case MSR_IA32_LASTINTFROMIP:
2191 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302192 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002193 case MSR_K8_TSEG_ADDR:
2194 case MSR_K8_TSEG_MASK:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302195 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002196 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002197 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002198 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002199 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002200 case MSR_AMD64_BU_CFG2:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002201 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002202 break;
Wei Huang6912ac32015-06-12 01:34:56 -04002203 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2204 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2205 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2206 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002207 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002208 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2209 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002210 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002211 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002212 msr_info->data = 0x100000000ULL;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002213 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002214 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002215 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002216 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002217 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002218 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002219 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002220 /*
2221 * MSR_EBC_FREQUENCY_ID
2222 * Conservative value valid for even the basic CPU models.
2223 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2224 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2225 * and 266MHz for model 3, or 4. Set Core Clock
2226 * Frequency to System Bus Frequency Ratio to 1 (bits
2227 * 31:24) even though these are only valid for CPU
2228 * models > 2, however guests may end up dividing or
2229 * multiplying by zero otherwise.
2230 */
2231 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002232 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002233 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002234 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002235 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002236 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002237 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002238 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002239 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002240 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002241 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002242 break;
Will Auldba904632012-11-29 12:42:50 -08002243 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002244 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002245 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002246 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002247 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002248 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002249 case MSR_IA32_SMBASE:
2250 if (!msr_info->host_initiated)
2251 return 1;
2252 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002253 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002254 case MSR_IA32_PERF_STATUS:
2255 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002256 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002257 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002258 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002259 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002260 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002261 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002262 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002263 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002264 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002265 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002266 break;
2267 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002268 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002269 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002270 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002271 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002272 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02002273 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002274 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002275 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04002276 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002277 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002278 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002279 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002280 case MSR_IA32_P5_MC_ADDR:
2281 case MSR_IA32_P5_MC_TYPE:
2282 case MSR_IA32_MCG_CAP:
2283 case MSR_IA32_MCG_CTL:
2284 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002285 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002286 return get_msr_mce(vcpu, msr_info->index, &msr_info->data);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002287 case MSR_K7_CLK_CTL:
2288 /*
2289 * Provide expected ramp-up count for K7. All other
2290 * are set to zero, indicating minimum divisors for
2291 * every field.
2292 *
2293 * This prevents guest kernels on AMD host with CPU
2294 * type 6, model 8 and higher from exploding due to
2295 * the rdmsr failing.
2296 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002297 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002298 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002299 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002300 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2301 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanine83d5882015-07-03 15:01:34 +03002302 return kvm_hv_get_msr_common(vcpu,
2303 msr_info->index, &msr_info->data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002304 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002305 case MSR_IA32_BBL_CR_CTL3:
2306 /* This legacy MSR exists but isn't fully documented in current
2307 * silicon. It is however accessed by winxp in very narrow
2308 * scenarios where it sets bit #19, itself documented as
2309 * a "reserved" bit. Best effort attempt to source coherent
2310 * read data here should the balance of the register be
2311 * interpreted by the guest:
2312 *
2313 * L2 cache control register 3: 64GB range, 256KB size,
2314 * enabled, latency 0x1, configured
2315 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002316 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05002317 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002318 case MSR_AMD64_OSVW_ID_LENGTH:
2319 if (!guest_cpuid_has_osvw(vcpu))
2320 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002321 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002322 break;
2323 case MSR_AMD64_OSVW_STATUS:
2324 if (!guest_cpuid_has_osvw(vcpu))
2325 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002326 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002327 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002328 default:
Wei Huangc6702c92015-06-19 13:44:45 +02002329 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002330 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002331 if (!ignore_msrs) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002332 vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02002333 return 1;
2334 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002335 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr_info->index);
2336 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02002337 }
2338 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002339 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002340 return 0;
2341}
2342EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2343
Carsten Otte313a3dc2007-10-11 19:16:52 +02002344/*
2345 * Read or write a bunch of msrs. All parameters are kernel addresses.
2346 *
2347 * @return number of msrs set successfully.
2348 */
2349static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2350 struct kvm_msr_entry *entries,
2351 int (*do_msr)(struct kvm_vcpu *vcpu,
2352 unsigned index, u64 *data))
2353{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002354 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002355
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002356 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002357 for (i = 0; i < msrs->nmsrs; ++i)
2358 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2359 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002360 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002361
Carsten Otte313a3dc2007-10-11 19:16:52 +02002362 return i;
2363}
2364
2365/*
2366 * Read or write a bunch of msrs. Parameters are user addresses.
2367 *
2368 * @return number of msrs set successfully.
2369 */
2370static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2371 int (*do_msr)(struct kvm_vcpu *vcpu,
2372 unsigned index, u64 *data),
2373 int writeback)
2374{
2375 struct kvm_msrs msrs;
2376 struct kvm_msr_entry *entries;
2377 int r, n;
2378 unsigned size;
2379
2380 r = -EFAULT;
2381 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2382 goto out;
2383
2384 r = -E2BIG;
2385 if (msrs.nmsrs >= MAX_IO_MSRS)
2386 goto out;
2387
Carsten Otte313a3dc2007-10-11 19:16:52 +02002388 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002389 entries = memdup_user(user_msrs->entries, size);
2390 if (IS_ERR(entries)) {
2391 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002392 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002393 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002394
2395 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2396 if (r < 0)
2397 goto out_free;
2398
2399 r = -EFAULT;
2400 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2401 goto out_free;
2402
2403 r = n;
2404
2405out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002406 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002407out:
2408 return r;
2409}
2410
Alexander Graf784aa3d2014-07-14 18:27:35 +02002411int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002412{
2413 int r;
2414
2415 switch (ext) {
2416 case KVM_CAP_IRQCHIP:
2417 case KVM_CAP_HLT:
2418 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002419 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002420 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002421 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002422 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002423 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002424 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002425 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002426 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002427 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002428 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002429 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002430 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002431 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002432 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002433 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002434 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002435 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002436 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002437 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002438 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002439 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002440 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002441 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002442 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002443 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002444 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002445 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002446 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002447 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002448 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002449 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002450 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01002451 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03002452 case KVM_CAP_ENABLE_CAP_VM:
2453 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02002454 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07002455 case KVM_CAP_SPLIT_IRQCHIP:
Alex Williamson2a5bab12013-04-16 13:49:18 -06002456#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2457 case KVM_CAP_ASSIGN_DEV_IRQ:
2458 case KVM_CAP_PCI_2_3:
2459#endif
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002460 r = 1;
2461 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02002462 case KVM_CAP_X86_SMM:
2463 /* SMBASE is usually relocated above 1M on modern chipsets,
2464 * and SMM handlers might indeed rely on 4G segment limits,
2465 * so do not report SMM to be available if real mode is
2466 * emulated via vm86 mode. Still, do not go to great lengths
2467 * to avoid userspace's usage of the feature, because it is a
2468 * fringe case that is not enabled except via specific settings
2469 * of the module parameters.
2470 */
2471 r = kvm_x86_ops->cpu_has_high_real_mode_segbase();
2472 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02002473 case KVM_CAP_COALESCED_MMIO:
2474 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
2475 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002476 case KVM_CAP_VAPIC:
2477 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2478 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002479 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002480 r = KVM_SOFT_MAX_VCPUS;
2481 break;
2482 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002483 r = KVM_MAX_VCPUS;
2484 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002485 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002486 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002487 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002488 case KVM_CAP_PV_MMU: /* obsolete */
2489 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002490 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002491#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002492 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +02002493 r = iommu_present(&pci_bus_type);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002494 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002495#endif
Huang Ying890ca9a2009-05-11 16:48:15 +08002496 case KVM_CAP_MCE:
2497 r = KVM_MAX_MCE_BANKS;
2498 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002499 case KVM_CAP_XCRS:
2500 r = cpu_has_xsave;
2501 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002502 case KVM_CAP_TSC_CONTROL:
2503 r = kvm_has_tsc_control;
2504 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002505 default:
2506 r = 0;
2507 break;
2508 }
2509 return r;
2510
2511}
2512
Carsten Otte043405e2007-10-10 17:16:19 +02002513long kvm_arch_dev_ioctl(struct file *filp,
2514 unsigned int ioctl, unsigned long arg)
2515{
2516 void __user *argp = (void __user *)arg;
2517 long r;
2518
2519 switch (ioctl) {
2520 case KVM_GET_MSR_INDEX_LIST: {
2521 struct kvm_msr_list __user *user_msr_list = argp;
2522 struct kvm_msr_list msr_list;
2523 unsigned n;
2524
2525 r = -EFAULT;
2526 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2527 goto out;
2528 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002529 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Carsten Otte043405e2007-10-10 17:16:19 +02002530 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2531 goto out;
2532 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002533 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02002534 goto out;
2535 r = -EFAULT;
2536 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2537 num_msrs_to_save * sizeof(u32)))
2538 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002539 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02002540 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002541 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02002542 goto out;
2543 r = 0;
2544 break;
2545 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002546 case KVM_GET_SUPPORTED_CPUID:
2547 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02002548 struct kvm_cpuid2 __user *cpuid_arg = argp;
2549 struct kvm_cpuid2 cpuid;
2550
2551 r = -EFAULT;
2552 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2553 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002554
2555 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2556 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02002557 if (r)
2558 goto out;
2559
2560 r = -EFAULT;
2561 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2562 goto out;
2563 r = 0;
2564 break;
2565 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002566 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
2567 u64 mce_cap;
2568
2569 mce_cap = KVM_MCE_CAP_SUPPORTED;
2570 r = -EFAULT;
2571 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
2572 goto out;
2573 r = 0;
2574 break;
2575 }
Carsten Otte043405e2007-10-10 17:16:19 +02002576 default:
2577 r = -EINVAL;
2578 }
2579out:
2580 return r;
2581}
2582
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002583static void wbinvd_ipi(void *garbage)
2584{
2585 wbinvd();
2586}
2587
2588static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
2589{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06002590 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002591}
2592
Carsten Otte313a3dc2007-10-11 19:16:52 +02002593void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2594{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002595 /* Address WBINVD may be executed by guest */
2596 if (need_emulate_wbinvd(vcpu)) {
2597 if (kvm_x86_ops->has_wbinvd_exit())
2598 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
2599 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
2600 smp_call_function_single(vcpu->cpu,
2601 wbinvd_ipi, NULL, 1);
2602 }
2603
Carsten Otte313a3dc2007-10-11 19:16:52 +02002604 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002605
2606 /* Apply any externally detected TSC adjustments (due to suspend) */
2607 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
2608 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
2609 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002610 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002611 }
2612
Zachary Amsden48434c202010-08-19 22:07:24 -10002613 if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002614 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002615 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10002616 if (tsc_delta < 0)
2617 mark_tsc_unstable("KVM discovered backwards TSC");
Zachary Amsdenc2855452010-09-18 14:38:15 -10002618 if (check_tsc_unstable()) {
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002619 u64 offset = kvm_x86_ops->compute_tsc_offset(vcpu,
2620 vcpu->arch.last_guest_tsc);
2621 kvm_x86_ops->write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002622 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002623 }
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02002624 /*
2625 * On a host with synchronized TSC, there is no need to update
2626 * kvmclock on vcpu->cpu migration
2627 */
2628 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002629 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002630 if (vcpu->cpu != cpu)
2631 kvm_migrate_timers(vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002632 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10002633 }
Glauber Costac9aaa892011-07-11 15:28:14 -04002634
2635 accumulate_steal_time(vcpu);
2636 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002637}
2638
2639void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2640{
Avi Kivity02daab22009-12-30 12:40:26 +02002641 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03002642 kvm_put_guest_fpu(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002643 vcpu->arch.last_host_tsc = rdtsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02002644}
2645
Carsten Otte313a3dc2007-10-11 19:16:52 +02002646static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2647 struct kvm_lapic_state *s)
2648{
Yang Zhang5a717852013-04-11 19:25:16 +08002649 kvm_x86_ops->sync_pir_to_irr(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002650 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002651
2652 return 0;
2653}
2654
2655static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2656 struct kvm_lapic_state *s)
2657{
Gleb Natapov64eb0622012-08-08 15:24:36 +03002658 kvm_apic_post_state_restore(vcpu, s);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002659 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002660
2661 return 0;
2662}
2663
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002664static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2665 struct kvm_interrupt *irq)
2666{
Chen Gang02cdb502013-02-27 11:33:25 +08002667 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002668 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002669
2670 if (!irqchip_in_kernel(vcpu->kvm)) {
2671 kvm_queue_interrupt(vcpu, irq->irq, false);
2672 kvm_make_request(KVM_REQ_EVENT, vcpu);
2673 return 0;
2674 }
2675
2676 /*
2677 * With in-kernel LAPIC, we only use this to inject EXTINT, so
2678 * fail for in-kernel 8259.
2679 */
2680 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002681 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002682
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002683 if (vcpu->arch.pending_external_vector != -1)
2684 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002685
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002686 vcpu->arch.pending_external_vector = irq->irq;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002687 return 0;
2688}
2689
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002690static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2691{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002692 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002693
2694 return 0;
2695}
2696
Paolo Bonzinif0778252015-04-01 15:06:40 +02002697static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
2698{
Paolo Bonzini64d60672015-05-07 11:36:11 +02002699 kvm_make_request(KVM_REQ_SMI, vcpu);
2700
Paolo Bonzinif0778252015-04-01 15:06:40 +02002701 return 0;
2702}
2703
Avi Kivityb209749f2007-10-22 16:50:39 +02002704static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2705 struct kvm_tpr_access_ctl *tac)
2706{
2707 if (tac->flags)
2708 return -EINVAL;
2709 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2710 return 0;
2711}
2712
Huang Ying890ca9a2009-05-11 16:48:15 +08002713static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2714 u64 mcg_cap)
2715{
2716 int r;
2717 unsigned bank_num = mcg_cap & 0xff, bank;
2718
2719 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002720 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002721 goto out;
2722 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2723 goto out;
2724 r = 0;
2725 vcpu->arch.mcg_cap = mcg_cap;
2726 /* Init IA32_MCG_CTL to all 1s */
2727 if (mcg_cap & MCG_CTL_P)
2728 vcpu->arch.mcg_ctl = ~(u64)0;
2729 /* Init IA32_MCi_CTL to all 1s */
2730 for (bank = 0; bank < bank_num; bank++)
2731 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2732out:
2733 return r;
2734}
2735
2736static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2737 struct kvm_x86_mce *mce)
2738{
2739 u64 mcg_cap = vcpu->arch.mcg_cap;
2740 unsigned bank_num = mcg_cap & 0xff;
2741 u64 *banks = vcpu->arch.mce_banks;
2742
2743 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2744 return -EINVAL;
2745 /*
2746 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2747 * reporting is disabled
2748 */
2749 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2750 vcpu->arch.mcg_ctl != ~(u64)0)
2751 return 0;
2752 banks += 4 * mce->bank;
2753 /*
2754 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2755 * reporting is disabled for the bank
2756 */
2757 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2758 return 0;
2759 if (mce->status & MCI_STATUS_UC) {
2760 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002761 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002762 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002763 return 0;
2764 }
2765 if (banks[1] & MCI_STATUS_VAL)
2766 mce->status |= MCI_STATUS_OVER;
2767 banks[2] = mce->addr;
2768 banks[3] = mce->misc;
2769 vcpu->arch.mcg_status = mce->mcg_status;
2770 banks[1] = mce->status;
2771 kvm_queue_exception(vcpu, MC_VECTOR);
2772 } else if (!(banks[1] & MCI_STATUS_VAL)
2773 || !(banks[1] & MCI_STATUS_UC)) {
2774 if (banks[1] & MCI_STATUS_VAL)
2775 mce->status |= MCI_STATUS_OVER;
2776 banks[2] = mce->addr;
2777 banks[3] = mce->misc;
2778 banks[1] = mce->status;
2779 } else
2780 banks[1] |= MCI_STATUS_OVER;
2781 return 0;
2782}
2783
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002784static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2785 struct kvm_vcpu_events *events)
2786{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002787 process_nmi(vcpu);
Jan Kiszka03b82a32010-02-15 10:45:41 +01002788 events->exception.injected =
2789 vcpu->arch.exception.pending &&
2790 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002791 events->exception.nr = vcpu->arch.exception.nr;
2792 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002793 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002794 events->exception.error_code = vcpu->arch.exception.error_code;
2795
Jan Kiszka03b82a32010-02-15 10:45:41 +01002796 events->interrupt.injected =
2797 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002798 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002799 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002800 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002801
2802 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002803 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002804 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002805 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002806
Jan Kiszka66450a22013-03-13 12:42:34 +01002807 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002808
Paolo Bonzinif0778252015-04-01 15:06:40 +02002809 events->smi.smm = is_smm(vcpu);
2810 events->smi.pending = vcpu->arch.smi_pending;
2811 events->smi.smm_inside_nmi =
2812 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
2813 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
2814
Jan Kiszkadab4b912009-12-06 18:24:15 +01002815 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02002816 | KVM_VCPUEVENT_VALID_SHADOW
2817 | KVM_VCPUEVENT_VALID_SMM);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002818 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002819}
2820
2821static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2822 struct kvm_vcpu_events *events)
2823{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002824 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002825 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02002826 | KVM_VCPUEVENT_VALID_SHADOW
2827 | KVM_VCPUEVENT_VALID_SMM))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002828 return -EINVAL;
2829
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002830 process_nmi(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002831 vcpu->arch.exception.pending = events->exception.injected;
2832 vcpu->arch.exception.nr = events->exception.nr;
2833 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2834 vcpu->arch.exception.error_code = events->exception.error_code;
2835
2836 vcpu->arch.interrupt.pending = events->interrupt.injected;
2837 vcpu->arch.interrupt.nr = events->interrupt.nr;
2838 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01002839 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2840 kvm_x86_ops->set_interrupt_shadow(vcpu,
2841 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002842
2843 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002844 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2845 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002846 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2847
Jan Kiszka66450a22013-03-13 12:42:34 +01002848 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
2849 kvm_vcpu_has_lapic(vcpu))
2850 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002851
Paolo Bonzinif0778252015-04-01 15:06:40 +02002852 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
2853 if (events->smi.smm)
2854 vcpu->arch.hflags |= HF_SMM_MASK;
2855 else
2856 vcpu->arch.hflags &= ~HF_SMM_MASK;
2857 vcpu->arch.smi_pending = events->smi.pending;
2858 if (events->smi.smm_inside_nmi)
2859 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
2860 else
2861 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
2862 if (kvm_vcpu_has_lapic(vcpu)) {
2863 if (events->smi.latched_init)
2864 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
2865 else
2866 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
2867 }
2868 }
2869
Avi Kivity3842d132010-07-27 12:30:24 +03002870 kvm_make_request(KVM_REQ_EVENT, vcpu);
2871
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002872 return 0;
2873}
2874
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002875static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2876 struct kvm_debugregs *dbgregs)
2877{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002878 unsigned long val;
2879
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002880 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03002881 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002882 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002883 dbgregs->dr7 = vcpu->arch.dr7;
2884 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002885 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002886}
2887
2888static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2889 struct kvm_debugregs *dbgregs)
2890{
2891 if (dbgregs->flags)
2892 return -EINVAL;
2893
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002894 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03002895 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002896 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002897 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002898 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01002899 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002900
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002901 return 0;
2902}
2903
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002904#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
2905
2906static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
2907{
Ingo Molnarc47ada32015-04-30 17:15:32 +02002908 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02002909 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002910 u64 valid;
2911
2912 /*
2913 * Copy legacy XSAVE area, to avoid complications with CPUID
2914 * leaves 0 and 1 in the loop below.
2915 */
2916 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
2917
2918 /* Set XSTATE_BV */
2919 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
2920
2921 /*
2922 * Copy each region from the possibly compacted offset to the
2923 * non-compacted offset.
2924 */
2925 valid = xstate_bv & ~XSTATE_FPSSE;
2926 while (valid) {
2927 u64 feature = valid & -valid;
2928 int index = fls64(feature) - 1;
2929 void *src = get_xsave_addr(xsave, feature);
2930
2931 if (src) {
2932 u32 size, offset, ecx, edx;
2933 cpuid_count(XSTATE_CPUID, index,
2934 &size, &offset, &ecx, &edx);
2935 memcpy(dest + offset, src, size);
2936 }
2937
2938 valid -= feature;
2939 }
2940}
2941
2942static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
2943{
Ingo Molnarc47ada32015-04-30 17:15:32 +02002944 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002945 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
2946 u64 valid;
2947
2948 /*
2949 * Copy legacy XSAVE area, to avoid complications with CPUID
2950 * leaves 0 and 1 in the loop below.
2951 */
2952 memcpy(xsave, src, XSAVE_HDR_OFFSET);
2953
2954 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02002955 xsave->header.xfeatures = xstate_bv;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002956 if (cpu_has_xsaves)
Ingo Molnar3a544502015-04-24 10:14:36 +02002957 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002958
2959 /*
2960 * Copy each region from the non-compacted offset to the
2961 * possibly compacted offset.
2962 */
2963 valid = xstate_bv & ~XSTATE_FPSSE;
2964 while (valid) {
2965 u64 feature = valid & -valid;
2966 int index = fls64(feature) - 1;
2967 void *dest = get_xsave_addr(xsave, feature);
2968
2969 if (dest) {
2970 u32 size, offset, ecx, edx;
2971 cpuid_count(XSTATE_CPUID, index,
2972 &size, &offset, &ecx, &edx);
2973 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08002974 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002975
2976 valid -= feature;
2977 }
2978}
2979
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002980static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
2981 struct kvm_xsave *guest_xsave)
2982{
Paolo Bonzini4344ee92013-10-02 16:06:16 +02002983 if (cpu_has_xsave) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01002984 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
2985 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02002986 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002987 memcpy(guest_xsave->region,
Ingo Molnar7366ed72015-04-27 04:19:39 +02002988 &vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02002989 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002990 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
2991 XSTATE_FPSSE;
2992 }
2993}
2994
2995static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
2996 struct kvm_xsave *guest_xsave)
2997{
2998 u64 xstate_bv =
2999 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
3000
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003001 if (cpu_has_xsave) {
3002 /*
3003 * Here we allow setting states that are not present in
3004 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3005 * with old userspace.
3006 */
Paolo Bonzini4ff41732014-02-24 12:15:16 +01003007 if (xstate_bv & ~kvm_supported_xcr0())
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003008 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003009 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003010 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003011 if (xstate_bv & ~XSTATE_FPSSE)
3012 return -EINVAL;
Ingo Molnar7366ed72015-04-27 04:19:39 +02003013 memcpy(&vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003014 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003015 }
3016 return 0;
3017}
3018
3019static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3020 struct kvm_xcrs *guest_xcrs)
3021{
3022 if (!cpu_has_xsave) {
3023 guest_xcrs->nr_xcrs = 0;
3024 return;
3025 }
3026
3027 guest_xcrs->nr_xcrs = 1;
3028 guest_xcrs->flags = 0;
3029 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3030 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3031}
3032
3033static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3034 struct kvm_xcrs *guest_xcrs)
3035{
3036 int i, r = 0;
3037
3038 if (!cpu_has_xsave)
3039 return -EINVAL;
3040
3041 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3042 return -EINVAL;
3043
3044 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3045 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003046 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003047 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003048 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003049 break;
3050 }
3051 if (r)
3052 r = -EINVAL;
3053 return r;
3054}
3055
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003056/*
3057 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3058 * stopped by the hypervisor. This function will be called from the host only.
3059 * EINVAL is returned when the host attempts to set the flag for a guest that
3060 * does not support pv clocks.
3061 */
3062static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3063{
Andy Honig0b794592013-02-20 14:48:10 -08003064 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003065 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003066 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003067 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3068 return 0;
3069}
3070
Carsten Otte313a3dc2007-10-11 19:16:52 +02003071long kvm_arch_vcpu_ioctl(struct file *filp,
3072 unsigned int ioctl, unsigned long arg)
3073{
3074 struct kvm_vcpu *vcpu = filp->private_data;
3075 void __user *argp = (void __user *)arg;
3076 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003077 union {
3078 struct kvm_lapic_state *lapic;
3079 struct kvm_xsave *xsave;
3080 struct kvm_xcrs *xcrs;
3081 void *buffer;
3082 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003083
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003084 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003085 switch (ioctl) {
3086 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003087 r = -EINVAL;
3088 if (!vcpu->arch.apic)
3089 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003090 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003091
Dave Hansenb772ff32008-08-11 10:01:47 -07003092 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003093 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003094 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003095 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003096 if (r)
3097 goto out;
3098 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003099 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003100 goto out;
3101 r = 0;
3102 break;
3103 }
3104 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003105 r = -EINVAL;
3106 if (!vcpu->arch.apic)
3107 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003108 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Guo Chao18595412012-11-02 18:33:21 +08003109 if (IS_ERR(u.lapic))
3110 return PTR_ERR(u.lapic);
Sasha Levinff5c2c02011-12-04 19:36:29 +02003111
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003112 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003113 break;
3114 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003115 case KVM_INTERRUPT: {
3116 struct kvm_interrupt irq;
3117
3118 r = -EFAULT;
3119 if (copy_from_user(&irq, argp, sizeof irq))
3120 goto out;
3121 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003122 break;
3123 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003124 case KVM_NMI: {
3125 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003126 break;
3127 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003128 case KVM_SMI: {
3129 r = kvm_vcpu_ioctl_smi(vcpu);
3130 break;
3131 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003132 case KVM_SET_CPUID: {
3133 struct kvm_cpuid __user *cpuid_arg = argp;
3134 struct kvm_cpuid cpuid;
3135
3136 r = -EFAULT;
3137 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3138 goto out;
3139 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003140 break;
3141 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003142 case KVM_SET_CPUID2: {
3143 struct kvm_cpuid2 __user *cpuid_arg = argp;
3144 struct kvm_cpuid2 cpuid;
3145
3146 r = -EFAULT;
3147 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3148 goto out;
3149 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003150 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003151 break;
3152 }
3153 case KVM_GET_CPUID2: {
3154 struct kvm_cpuid2 __user *cpuid_arg = argp;
3155 struct kvm_cpuid2 cpuid;
3156
3157 r = -EFAULT;
3158 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3159 goto out;
3160 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003161 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003162 if (r)
3163 goto out;
3164 r = -EFAULT;
3165 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3166 goto out;
3167 r = 0;
3168 break;
3169 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003170 case KVM_GET_MSRS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003171 r = msr_io(vcpu, argp, do_get_msr, 1);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003172 break;
3173 case KVM_SET_MSRS:
3174 r = msr_io(vcpu, argp, do_set_msr, 0);
3175 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02003176 case KVM_TPR_ACCESS_REPORTING: {
3177 struct kvm_tpr_access_ctl tac;
3178
3179 r = -EFAULT;
3180 if (copy_from_user(&tac, argp, sizeof tac))
3181 goto out;
3182 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3183 if (r)
3184 goto out;
3185 r = -EFAULT;
3186 if (copy_to_user(argp, &tac, sizeof tac))
3187 goto out;
3188 r = 0;
3189 break;
3190 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003191 case KVM_SET_VAPIC_ADDR: {
3192 struct kvm_vapic_addr va;
3193
3194 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02003195 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02003196 goto out;
3197 r = -EFAULT;
3198 if (copy_from_user(&va, argp, sizeof va))
3199 goto out;
Andy Honigfda4e2e2013-11-20 10:23:22 -08003200 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Avi Kivityb93463a2007-10-25 16:52:32 +02003201 break;
3202 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003203 case KVM_X86_SETUP_MCE: {
3204 u64 mcg_cap;
3205
3206 r = -EFAULT;
3207 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3208 goto out;
3209 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3210 break;
3211 }
3212 case KVM_X86_SET_MCE: {
3213 struct kvm_x86_mce mce;
3214
3215 r = -EFAULT;
3216 if (copy_from_user(&mce, argp, sizeof mce))
3217 goto out;
3218 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3219 break;
3220 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003221 case KVM_GET_VCPU_EVENTS: {
3222 struct kvm_vcpu_events events;
3223
3224 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3225
3226 r = -EFAULT;
3227 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3228 break;
3229 r = 0;
3230 break;
3231 }
3232 case KVM_SET_VCPU_EVENTS: {
3233 struct kvm_vcpu_events events;
3234
3235 r = -EFAULT;
3236 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3237 break;
3238
3239 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3240 break;
3241 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003242 case KVM_GET_DEBUGREGS: {
3243 struct kvm_debugregs dbgregs;
3244
3245 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3246
3247 r = -EFAULT;
3248 if (copy_to_user(argp, &dbgregs,
3249 sizeof(struct kvm_debugregs)))
3250 break;
3251 r = 0;
3252 break;
3253 }
3254 case KVM_SET_DEBUGREGS: {
3255 struct kvm_debugregs dbgregs;
3256
3257 r = -EFAULT;
3258 if (copy_from_user(&dbgregs, argp,
3259 sizeof(struct kvm_debugregs)))
3260 break;
3261
3262 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3263 break;
3264 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003265 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003266 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003267 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003268 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003269 break;
3270
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003271 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003272
3273 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003274 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003275 break;
3276 r = 0;
3277 break;
3278 }
3279 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003280 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Guo Chao18595412012-11-02 18:33:21 +08003281 if (IS_ERR(u.xsave))
3282 return PTR_ERR(u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003283
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003284 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003285 break;
3286 }
3287 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003288 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003289 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003290 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003291 break;
3292
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003293 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003294
3295 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003296 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003297 sizeof(struct kvm_xcrs)))
3298 break;
3299 r = 0;
3300 break;
3301 }
3302 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003303 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Guo Chao18595412012-11-02 18:33:21 +08003304 if (IS_ERR(u.xcrs))
3305 return PTR_ERR(u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003306
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003307 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003308 break;
3309 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003310 case KVM_SET_TSC_KHZ: {
3311 u32 user_tsc_khz;
3312
3313 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003314 user_tsc_khz = (u32)arg;
3315
3316 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3317 goto out;
3318
Zachary Amsdencc578282012-02-03 15:43:50 -02003319 if (user_tsc_khz == 0)
3320 user_tsc_khz = tsc_khz;
3321
3322 kvm_set_tsc_khz(vcpu, user_tsc_khz);
Joerg Roedel92a1f122011-03-25 09:44:51 +01003323
3324 r = 0;
3325 goto out;
3326 }
3327 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003328 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003329 goto out;
3330 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003331 case KVM_KVMCLOCK_CTRL: {
3332 r = kvm_set_guest_paused(vcpu);
3333 goto out;
3334 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003335 default:
3336 r = -EINVAL;
3337 }
3338out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003339 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003340 return r;
3341}
3342
Carsten Otte5b1c1492012-01-04 10:25:23 +01003343int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
3344{
3345 return VM_FAULT_SIGBUS;
3346}
3347
Carsten Otte1fe779f2007-10-29 16:08:35 +01003348static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
3349{
3350 int ret;
3351
3352 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08003353 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003354 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
3355 return ret;
3356}
3357
Sheng Yangb927a3c2009-07-21 10:42:48 +08003358static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
3359 u64 ident_addr)
3360{
3361 kvm->arch.ept_identity_map_addr = ident_addr;
3362 return 0;
3363}
3364
Carsten Otte1fe779f2007-10-29 16:08:35 +01003365static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
3366 u32 kvm_nr_mmu_pages)
3367{
3368 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
3369 return -EINVAL;
3370
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003371 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003372
3373 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003374 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003375
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003376 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003377 return 0;
3378}
3379
3380static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
3381{
Dave Hansen39de71e2010-08-19 18:11:14 -07003382 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003383}
3384
Carsten Otte1fe779f2007-10-29 16:08:35 +01003385static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3386{
3387 int r;
3388
3389 r = 0;
3390 switch (chip->chip_id) {
3391 case KVM_IRQCHIP_PIC_MASTER:
3392 memcpy(&chip->chip.pic,
3393 &pic_irqchip(kvm)->pics[0],
3394 sizeof(struct kvm_pic_state));
3395 break;
3396 case KVM_IRQCHIP_PIC_SLAVE:
3397 memcpy(&chip->chip.pic,
3398 &pic_irqchip(kvm)->pics[1],
3399 sizeof(struct kvm_pic_state));
3400 break;
3401 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003402 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003403 break;
3404 default:
3405 r = -EINVAL;
3406 break;
3407 }
3408 return r;
3409}
3410
3411static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3412{
3413 int r;
3414
3415 r = 0;
3416 switch (chip->chip_id) {
3417 case KVM_IRQCHIP_PIC_MASTER:
Avi Kivityf4f51052010-09-19 18:44:07 +02003418 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003419 memcpy(&pic_irqchip(kvm)->pics[0],
3420 &chip->chip.pic,
3421 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003422 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003423 break;
3424 case KVM_IRQCHIP_PIC_SLAVE:
Avi Kivityf4f51052010-09-19 18:44:07 +02003425 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003426 memcpy(&pic_irqchip(kvm)->pics[1],
3427 &chip->chip.pic,
3428 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003429 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003430 break;
3431 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003432 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003433 break;
3434 default:
3435 r = -EINVAL;
3436 break;
3437 }
3438 kvm_pic_update_irq(pic_irqchip(kvm));
3439 return r;
3440}
3441
Sheng Yange0f63cb2008-03-04 00:50:59 +08003442static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3443{
3444 int r = 0;
3445
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003446 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003447 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003448 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003449 return r;
3450}
3451
3452static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3453{
3454 int r = 0;
3455
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003456 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003457 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04003458 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
3459 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
3460 return r;
3461}
3462
3463static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3464{
3465 int r = 0;
3466
3467 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3468 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
3469 sizeof(ps->channels));
3470 ps->flags = kvm->arch.vpit->pit_state.flags;
3471 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003472 memset(&ps->reserved, 0, sizeof(ps->reserved));
Beth Kone9f42752009-07-07 11:50:38 -04003473 return r;
3474}
3475
3476static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3477{
3478 int r = 0, start = 0;
3479 u32 prev_legacy, cur_legacy;
3480 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3481 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
3482 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
3483 if (!prev_legacy && cur_legacy)
3484 start = 1;
3485 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
3486 sizeof(kvm->arch.vpit->pit_state.channels));
3487 kvm->arch.vpit->pit_state.flags = ps->flags;
3488 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003489 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003490 return r;
3491}
3492
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003493static int kvm_vm_ioctl_reinject(struct kvm *kvm,
3494 struct kvm_reinject_control *control)
3495{
3496 if (!kvm->arch.vpit)
3497 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003498 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Avi Kivity26ef1922012-07-26 18:01:53 +03003499 kvm->arch.vpit->pit_state.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03003500 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003501 return 0;
3502}
3503
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003504/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003505 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
3506 * @kvm: kvm instance
3507 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003508 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003509 * Steps 1-4 below provide general overview of dirty page logging. See
3510 * kvm_get_dirty_log_protect() function description for additional details.
3511 *
3512 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
3513 * always flush the TLB (step 4) even if previous step failed and the dirty
3514 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
3515 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
3516 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003517 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003518 * 1. Take a snapshot of the bit and clear it if needed.
3519 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003520 * 3. Copy the snapshot to the userspace.
3521 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003522 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003523int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003524{
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003525 bool is_dirty = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003526 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003527
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003528 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003529
Kai Huang88178fd2015-01-28 10:54:27 +08003530 /*
3531 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
3532 */
3533 if (kvm_x86_ops->flush_log_dirty)
3534 kvm_x86_ops->flush_log_dirty(kvm);
3535
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003536 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003537
3538 /*
3539 * All the TLBs can be flushed out of mmu lock, see the comments in
3540 * kvm_mmu_slot_remove_write_access().
3541 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003542 lockdep_assert_held(&kvm->slots_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003543 if (is_dirty)
3544 kvm_flush_remote_tlbs(kvm);
3545
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003546 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003547 return r;
3548}
3549
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003550int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
3551 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003552{
3553 if (!irqchip_in_kernel(kvm))
3554 return -ENXIO;
3555
3556 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003557 irq_event->irq, irq_event->level,
3558 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003559 return 0;
3560}
3561
Nadav Amit90de4a12015-04-13 01:53:41 +03003562static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3563 struct kvm_enable_cap *cap)
3564{
3565 int r;
3566
3567 if (cap->flags)
3568 return -EINVAL;
3569
3570 switch (cap->cap) {
3571 case KVM_CAP_DISABLE_QUIRKS:
3572 kvm->arch.disabled_quirks = cap->args[0];
3573 r = 0;
3574 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07003575 case KVM_CAP_SPLIT_IRQCHIP: {
3576 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07003577 r = -EINVAL;
3578 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
3579 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07003580 r = -EEXIST;
3581 if (irqchip_in_kernel(kvm))
3582 goto split_irqchip_unlock;
3583 if (atomic_read(&kvm->online_vcpus))
3584 goto split_irqchip_unlock;
3585 r = kvm_setup_empty_irq_routing(kvm);
3586 if (r)
3587 goto split_irqchip_unlock;
3588 /* Pairs with irqchip_in_kernel. */
3589 smp_wmb();
3590 kvm->arch.irqchip_split = true;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07003591 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07003592 r = 0;
3593split_irqchip_unlock:
3594 mutex_unlock(&kvm->lock);
3595 break;
3596 }
Nadav Amit90de4a12015-04-13 01:53:41 +03003597 default:
3598 r = -EINVAL;
3599 break;
3600 }
3601 return r;
3602}
3603
Carsten Otte1fe779f2007-10-29 16:08:35 +01003604long kvm_arch_vm_ioctl(struct file *filp,
3605 unsigned int ioctl, unsigned long arg)
3606{
3607 struct kvm *kvm = filp->private_data;
3608 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03003609 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07003610 /*
3611 * This union makes it completely explicit to gcc-3.x
3612 * that these two variables' stack usage should be
3613 * combined, not added together.
3614 */
3615 union {
3616 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04003617 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003618 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07003619 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003620
3621 switch (ioctl) {
3622 case KVM_SET_TSS_ADDR:
3623 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003624 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003625 case KVM_SET_IDENTITY_MAP_ADDR: {
3626 u64 ident_addr;
3627
3628 r = -EFAULT;
3629 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
3630 goto out;
3631 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08003632 break;
3633 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003634 case KVM_SET_NR_MMU_PAGES:
3635 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003636 break;
3637 case KVM_GET_NR_MMU_PAGES:
3638 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3639 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003640 case KVM_CREATE_IRQCHIP: {
3641 struct kvm_pic *vpic;
3642
3643 mutex_lock(&kvm->lock);
3644 r = -EEXIST;
3645 if (kvm->arch.vpic)
3646 goto create_irqchip_unlock;
Avi Kivity3e515702012-03-05 14:23:29 +02003647 r = -EINVAL;
3648 if (atomic_read(&kvm->online_vcpus))
3649 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003650 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003651 vpic = kvm_create_pic(kvm);
3652 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003653 r = kvm_ioapic_init(kvm);
3654 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003655 mutex_lock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003656 kvm_destroy_pic(vpic);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003657 mutex_unlock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003658 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003659 }
3660 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003661 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02003662 r = kvm_setup_default_irq_routing(kvm);
3663 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003664 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003665 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003666 kvm_ioapic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003667 kvm_destroy_pic(vpic);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003668 mutex_unlock(&kvm->irq_lock);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003669 mutex_unlock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003670 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02003671 }
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003672 /* Write kvm->irq_routing before kvm->arch.vpic. */
3673 smp_wmb();
3674 kvm->arch.vpic = vpic;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003675 create_irqchip_unlock:
3676 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003677 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003678 }
Sheng Yang78376992008-01-28 05:10:22 +08003679 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003680 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3681 goto create_pit;
3682 case KVM_CREATE_PIT2:
3683 r = -EFAULT;
3684 if (copy_from_user(&u.pit_config, argp,
3685 sizeof(struct kvm_pit_config)))
3686 goto out;
3687 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003688 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003689 r = -EEXIST;
3690 if (kvm->arch.vpit)
3691 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003692 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003693 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003694 if (kvm->arch.vpit)
3695 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003696 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003697 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08003698 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003699 case KVM_GET_IRQCHIP: {
3700 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003701 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003702
Sasha Levinff5c2c02011-12-04 19:36:29 +02003703 chip = memdup_user(argp, sizeof(*chip));
3704 if (IS_ERR(chip)) {
3705 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003706 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003707 }
3708
Carsten Otte1fe779f2007-10-29 16:08:35 +01003709 r = -ENXIO;
Steve Rutherford49df6392015-07-29 23:21:40 -07003710 if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003711 goto get_irqchip_out;
3712 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3713 if (r)
3714 goto get_irqchip_out;
3715 r = -EFAULT;
3716 if (copy_to_user(argp, chip, sizeof *chip))
3717 goto get_irqchip_out;
3718 r = 0;
3719 get_irqchip_out:
3720 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003721 break;
3722 }
3723 case KVM_SET_IRQCHIP: {
3724 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003725 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003726
Sasha Levinff5c2c02011-12-04 19:36:29 +02003727 chip = memdup_user(argp, sizeof(*chip));
3728 if (IS_ERR(chip)) {
3729 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003730 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003731 }
3732
Carsten Otte1fe779f2007-10-29 16:08:35 +01003733 r = -ENXIO;
Steve Rutherford49df6392015-07-29 23:21:40 -07003734 if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003735 goto set_irqchip_out;
3736 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3737 if (r)
3738 goto set_irqchip_out;
3739 r = 0;
3740 set_irqchip_out:
3741 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003742 break;
3743 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003744 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003745 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003746 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003747 goto out;
3748 r = -ENXIO;
3749 if (!kvm->arch.vpit)
3750 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003751 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003752 if (r)
3753 goto out;
3754 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003755 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003756 goto out;
3757 r = 0;
3758 break;
3759 }
3760 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003761 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003762 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003763 goto out;
3764 r = -ENXIO;
3765 if (!kvm->arch.vpit)
3766 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003767 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003768 break;
3769 }
Beth Kone9f42752009-07-07 11:50:38 -04003770 case KVM_GET_PIT2: {
3771 r = -ENXIO;
3772 if (!kvm->arch.vpit)
3773 goto out;
3774 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3775 if (r)
3776 goto out;
3777 r = -EFAULT;
3778 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3779 goto out;
3780 r = 0;
3781 break;
3782 }
3783 case KVM_SET_PIT2: {
3784 r = -EFAULT;
3785 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3786 goto out;
3787 r = -ENXIO;
3788 if (!kvm->arch.vpit)
3789 goto out;
3790 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04003791 break;
3792 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003793 case KVM_REINJECT_CONTROL: {
3794 struct kvm_reinject_control control;
3795 r = -EFAULT;
3796 if (copy_from_user(&control, argp, sizeof(control)))
3797 goto out;
3798 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003799 break;
3800 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003801 case KVM_SET_BOOT_CPU_ID:
3802 r = 0;
3803 mutex_lock(&kvm->lock);
3804 if (atomic_read(&kvm->online_vcpus) != 0)
3805 r = -EBUSY;
3806 else
3807 kvm->arch.bsp_vcpu_id = arg;
3808 mutex_unlock(&kvm->lock);
3809 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07003810 case KVM_XEN_HVM_CONFIG: {
3811 r = -EFAULT;
3812 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3813 sizeof(struct kvm_xen_hvm_config)))
3814 goto out;
3815 r = -EINVAL;
3816 if (kvm->arch.xen_hvm_config.flags)
3817 goto out;
3818 r = 0;
3819 break;
3820 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003821 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003822 struct kvm_clock_data user_ns;
3823 u64 now_ns;
3824 s64 delta;
3825
3826 r = -EFAULT;
3827 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3828 goto out;
3829
3830 r = -EINVAL;
3831 if (user_ns.flags)
3832 goto out;
3833
3834 r = 0;
Avi Kivity395c6b02010-10-04 12:55:49 +02003835 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003836 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003837 delta = user_ns.clock - now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003838 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003839 kvm->arch.kvmclock_offset = delta;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03003840 kvm_gen_update_masterclock(kvm);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003841 break;
3842 }
3843 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003844 struct kvm_clock_data user_ns;
3845 u64 now_ns;
3846
Avi Kivity395c6b02010-10-04 12:55:49 +02003847 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10003848 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003849 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02003850 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003851 user_ns.flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003852 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003853
3854 r = -EFAULT;
3855 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3856 goto out;
3857 r = 0;
3858 break;
3859 }
Nadav Amit90de4a12015-04-13 01:53:41 +03003860 case KVM_ENABLE_CAP: {
3861 struct kvm_enable_cap cap;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003862
Nadav Amit90de4a12015-04-13 01:53:41 +03003863 r = -EFAULT;
3864 if (copy_from_user(&cap, argp, sizeof(cap)))
3865 goto out;
3866 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
3867 break;
3868 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003869 default:
Radim Krčmářc274e032014-11-21 22:21:50 +01003870 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003871 }
3872out:
3873 return r;
3874}
3875
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003876static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003877{
3878 u32 dummy[2];
3879 unsigned i, j;
3880
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003881 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003882 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3883 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003884
3885 /*
3886 * Even MSRs that are valid in the host may not be exposed
3887 * to the guests in some cases. We could work around this
3888 * in VMX with the generic MSR save/load machinery, but it
3889 * is not really worthwhile since it will really only
3890 * happen with nested virtualization.
3891 */
3892 switch (msrs_to_save[i]) {
3893 case MSR_IA32_BNDCFGS:
3894 if (!kvm_x86_ops->mpx_supported())
3895 continue;
3896 break;
3897 default:
3898 break;
3899 }
3900
Carsten Otte043405e2007-10-10 17:16:19 +02003901 if (j < i)
3902 msrs_to_save[j] = msrs_to_save[i];
3903 j++;
3904 }
3905 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003906
3907 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
3908 switch (emulated_msrs[i]) {
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003909 case MSR_IA32_SMBASE:
3910 if (!kvm_x86_ops->cpu_has_high_real_mode_segbase())
3911 continue;
3912 break;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003913 default:
3914 break;
3915 }
3916
3917 if (j < i)
3918 emulated_msrs[j] = emulated_msrs[i];
3919 j++;
3920 }
3921 num_emulated_msrs = j;
Carsten Otte043405e2007-10-10 17:16:19 +02003922}
3923
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003924static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3925 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003926{
Avi Kivity70252a12010-01-19 12:51:22 +02003927 int handled = 0;
3928 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003929
Avi Kivity70252a12010-01-19 12:51:22 +02003930 do {
3931 n = min(len, 8);
3932 if (!(vcpu->arch.apic &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00003933 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
3934 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02003935 break;
3936 handled += n;
3937 addr += n;
3938 len -= n;
3939 v += n;
3940 } while (len);
3941
3942 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003943}
3944
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003945static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003946{
Avi Kivity70252a12010-01-19 12:51:22 +02003947 int handled = 0;
3948 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003949
Avi Kivity70252a12010-01-19 12:51:22 +02003950 do {
3951 n = min(len, 8);
3952 if (!(vcpu->arch.apic &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00003953 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
3954 addr, n, v))
3955 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02003956 break;
3957 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
3958 handled += n;
3959 addr += n;
3960 len -= n;
3961 v += n;
3962 } while (len);
3963
3964 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003965}
3966
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003967static void kvm_set_segment(struct kvm_vcpu *vcpu,
3968 struct kvm_segment *var, int seg)
3969{
3970 kvm_x86_ops->set_segment(vcpu, var, seg);
3971}
3972
3973void kvm_get_segment(struct kvm_vcpu *vcpu,
3974 struct kvm_segment *var, int seg)
3975{
3976 kvm_x86_ops->get_segment(vcpu, var, seg);
3977}
3978
Paolo Bonzini54987b72014-09-02 13:23:06 +02003979gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
3980 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003981{
3982 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003983
3984 BUG_ON(!mmu_is_nested(vcpu));
3985
3986 /* NPT walks are always user-walks */
3987 access |= PFERR_USER_MASK;
Paolo Bonzini54987b72014-09-02 13:23:06 +02003988 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003989
3990 return t_gpa;
3991}
3992
Avi Kivityab9ae312010-11-22 17:53:26 +02003993gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
3994 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02003995{
3996 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02003997 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02003998}
3999
Avi Kivityab9ae312010-11-22 17:53:26 +02004000 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4001 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004002{
4003 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4004 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004005 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004006}
4007
Avi Kivityab9ae312010-11-22 17:53:26 +02004008gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4009 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004010{
4011 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4012 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004013 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004014}
4015
4016/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004017gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4018 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004019{
Avi Kivityab9ae312010-11-22 17:53:26 +02004020 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004021}
4022
4023static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4024 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004025 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004026{
4027 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004028 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004029
4030 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004031 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004032 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004033 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004034 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004035 int ret;
4036
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004037 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004038 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004039 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
4040 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004041 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004042 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004043 goto out;
4044 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004045
Izik Eidus77c20022008-12-29 01:42:19 +02004046 bytes -= toread;
4047 data += toread;
4048 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004049 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004050out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004051 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004052}
Izik Eidus77c20022008-12-29 01:42:19 +02004053
Gleb Natapov1871c602010-02-10 14:21:32 +02004054/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004055static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4056 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004057 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004058{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004059 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004060 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004061 unsigned offset;
4062 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004063
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004064 /* Inline kvm_read_guest_virt_helper for speed. */
4065 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4066 exception);
4067 if (unlikely(gpa == UNMAPPED_GVA))
4068 return X86EMUL_PROPAGATE_FAULT;
4069
4070 offset = addr & (PAGE_SIZE-1);
4071 if (WARN_ON(offset + bytes > PAGE_SIZE))
4072 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004073 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
4074 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004075 if (unlikely(ret < 0))
4076 return X86EMUL_IO_NEEDED;
4077
4078 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004079}
4080
Nadav Har'El064aea72011-05-25 23:04:56 +03004081int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004082 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004083 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004084{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004085 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004086 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004087
Gleb Natapov1871c602010-02-10 14:21:32 +02004088 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004089 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004090}
Nadav Har'El064aea72011-05-25 23:04:56 +03004091EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004092
Avi Kivity0f65dd72011-04-20 13:37:53 +03004093static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
4094 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004095 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004096{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004097 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004098 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004099}
4100
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004101int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004102 gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004103 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004104 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004105{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004106 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Izik Eidus77c20022008-12-29 01:42:19 +02004107 void *data = val;
4108 int r = X86EMUL_CONTINUE;
4109
4110 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004111 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
4112 PFERR_WRITE_MASK,
Avi Kivityab9ae312010-11-22 17:53:26 +02004113 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004114 unsigned offset = addr & (PAGE_SIZE-1);
4115 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4116 int ret;
4117
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004118 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004119 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004120 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02004121 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004122 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004123 goto out;
4124 }
4125
4126 bytes -= towrite;
4127 data += towrite;
4128 addr += towrite;
4129 }
4130out:
4131 return r;
4132}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004133EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004134
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004135static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
4136 gpa_t *gpa, struct x86_exception *exception,
4137 bool write)
4138{
Avi Kivity97d64b72012-09-12 14:52:00 +03004139 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
4140 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004141
Avi Kivity97d64b72012-09-12 14:52:00 +03004142 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08004143 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
4144 vcpu->arch.access, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004145 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
4146 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08004147 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004148 return 1;
4149 }
4150
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004151 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
4152
4153 if (*gpa == UNMAPPED_GVA)
4154 return -1;
4155
4156 /* For APIC access vmexit */
4157 if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4158 return 1;
4159
Xiao Guangrong4f022642011-07-12 03:34:24 +08004160 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
4161 trace_vcpu_match_mmio(gva, *gpa, write, true);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004162 return 1;
Xiao Guangrong4f022642011-07-12 03:34:24 +08004163 }
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004164
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004165 return 0;
4166}
4167
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004168int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004169 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02004170{
4171 int ret;
4172
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004173 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004174 if (ret < 0)
4175 return 0;
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004176 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004177 return 1;
4178}
4179
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004180struct read_write_emulator_ops {
4181 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
4182 int bytes);
4183 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
4184 void *val, int bytes);
4185 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4186 int bytes, void *val);
4187 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4188 void *val, int bytes);
4189 bool write;
4190};
4191
4192static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
4193{
4194 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004195 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Avi Kivityf78146b2012-04-18 19:22:47 +03004196 vcpu->mmio_fragments[0].gpa, *(u64 *)val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004197 vcpu->mmio_read_completed = 0;
4198 return 1;
4199 }
4200
4201 return 0;
4202}
4203
4204static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4205 void *val, int bytes)
4206{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004207 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004208}
4209
4210static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4211 void *val, int bytes)
4212{
4213 return emulator_write_phys(vcpu, gpa, val, bytes);
4214}
4215
4216static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
4217{
4218 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4219 return vcpu_mmio_write(vcpu, gpa, bytes, val);
4220}
4221
4222static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4223 void *val, int bytes)
4224{
4225 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4226 return X86EMUL_IO_NEEDED;
4227}
4228
4229static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4230 void *val, int bytes)
4231{
Avi Kivityf78146b2012-04-18 19:22:47 +03004232 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
4233
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004234 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004235 return X86EMUL_CONTINUE;
4236}
4237
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004238static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004239 .read_write_prepare = read_prepare,
4240 .read_write_emulate = read_emulate,
4241 .read_write_mmio = vcpu_mmio_read,
4242 .read_write_exit_mmio = read_exit_mmio,
4243};
4244
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004245static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004246 .read_write_emulate = write_emulate,
4247 .read_write_mmio = write_mmio,
4248 .read_write_exit_mmio = write_exit_mmio,
4249 .write = true,
4250};
4251
Xiao Guangrong22388a32011-07-13 14:32:31 +08004252static int emulator_read_write_onepage(unsigned long addr, void *val,
4253 unsigned int bytes,
4254 struct x86_exception *exception,
4255 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004256 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004257{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004258 gpa_t gpa;
4259 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004260 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03004261 struct kvm_mmio_fragment *frag;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004262
4263 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004264
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004265 if (ret < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004266 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004267
4268 /* For APIC access vmexit */
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004269 if (ret)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004270 goto mmio;
4271
Xiao Guangrong22388a32011-07-13 14:32:31 +08004272 if (ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004273 return X86EMUL_CONTINUE;
4274
4275mmio:
4276 /*
4277 * Is this MMIO handled locally?
4278 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08004279 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02004280 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004281 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004282
Avi Kivity70252a12010-01-19 12:51:22 +02004283 gpa += handled;
4284 bytes -= handled;
4285 val += handled;
4286
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004287 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
4288 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
4289 frag->gpa = gpa;
4290 frag->data = val;
4291 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03004292 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004293}
4294
Xiubo Li52eb5a62015-03-13 17:39:45 +08004295static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
4296 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004297 void *val, unsigned int bytes,
4298 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004299 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004300{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004301 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03004302 gpa_t gpa;
4303 int rc;
4304
4305 if (ops->read_write_prepare &&
4306 ops->read_write_prepare(vcpu, val, bytes))
4307 return X86EMUL_CONTINUE;
4308
4309 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004310
Carsten Ottebbd9b642007-10-30 18:44:21 +01004311 /* Crossing a page boundary? */
4312 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03004313 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004314
4315 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004316 rc = emulator_read_write_onepage(addr, val, now, exception,
4317 vcpu, ops);
4318
Carsten Ottebbd9b642007-10-30 18:44:21 +01004319 if (rc != X86EMUL_CONTINUE)
4320 return rc;
4321 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02004322 if (ctxt->mode != X86EMUL_MODE_PROT64)
4323 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004324 val += now;
4325 bytes -= now;
4326 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08004327
Avi Kivityf78146b2012-04-18 19:22:47 +03004328 rc = emulator_read_write_onepage(addr, val, bytes, exception,
4329 vcpu, ops);
4330 if (rc != X86EMUL_CONTINUE)
4331 return rc;
4332
4333 if (!vcpu->mmio_nr_fragments)
4334 return rc;
4335
4336 gpa = vcpu->mmio_fragments[0].gpa;
4337
4338 vcpu->mmio_needed = 1;
4339 vcpu->mmio_cur_fragment = 0;
4340
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004341 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03004342 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
4343 vcpu->run->exit_reason = KVM_EXIT_MMIO;
4344 vcpu->run->mmio.phys_addr = gpa;
4345
4346 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08004347}
4348
4349static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4350 unsigned long addr,
4351 void *val,
4352 unsigned int bytes,
4353 struct x86_exception *exception)
4354{
4355 return emulator_read_write(ctxt, addr, val, bytes,
4356 exception, &read_emultor);
4357}
4358
Xiubo Li52eb5a62015-03-13 17:39:45 +08004359static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004360 unsigned long addr,
4361 const void *val,
4362 unsigned int bytes,
4363 struct x86_exception *exception)
4364{
4365 return emulator_read_write(ctxt, addr, (void *)val, bytes,
4366 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004367}
Carsten Ottebbd9b642007-10-30 18:44:21 +01004368
Avi Kivitydaea3e72010-03-15 13:59:54 +02004369#define CMPXCHG_TYPE(t, ptr, old, new) \
4370 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
4371
4372#ifdef CONFIG_X86_64
4373# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
4374#else
4375# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01004376 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02004377#endif
4378
Avi Kivity0f65dd72011-04-20 13:37:53 +03004379static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
4380 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004381 const void *old,
4382 const void *new,
4383 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004384 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004385{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004386 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004387 gpa_t gpa;
4388 struct page *page;
4389 char *kaddr;
4390 bool exchanged;
4391
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004392 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02004393 if (bytes > 8 || (bytes & (bytes - 1)))
4394 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004395
Avi Kivitydaea3e72010-03-15 13:59:54 +02004396 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004397
Avi Kivitydaea3e72010-03-15 13:59:54 +02004398 if (gpa == UNMAPPED_GVA ||
4399 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4400 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004401
Avi Kivitydaea3e72010-03-15 13:59:54 +02004402 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
4403 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004404
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004405 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08004406 if (is_error_page(page))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08004407 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004408
Cong Wang8fd75e12011-11-25 23:14:17 +08004409 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004410 kaddr += offset_in_page(gpa);
4411 switch (bytes) {
4412 case 1:
4413 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
4414 break;
4415 case 2:
4416 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
4417 break;
4418 case 4:
4419 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
4420 break;
4421 case 8:
4422 exchanged = CMPXCHG64(kaddr, old, new);
4423 break;
4424 default:
4425 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004426 }
Cong Wang8fd75e12011-11-25 23:14:17 +08004427 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004428 kvm_release_page_dirty(page);
4429
4430 if (!exchanged)
4431 return X86EMUL_CMPXCHG_FAILED;
4432
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004433 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrongf57f2ef2011-09-22 16:56:39 +08004434 kvm_mmu_pte_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004435
4436 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02004437
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004438emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02004439 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004440
Avi Kivity0f65dd72011-04-20 13:37:53 +03004441 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004442}
4443
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004444static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
4445{
4446 /* TODO: String I/O for in kernel device */
4447 int r;
4448
4449 if (vcpu->arch.pio.in)
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004450 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004451 vcpu->arch.pio.size, pd);
4452 else
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004453 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004454 vcpu->arch.pio.port, vcpu->arch.pio.size,
4455 pd);
4456 return r;
4457}
4458
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004459static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
4460 unsigned short port, void *val,
4461 unsigned int count, bool in)
4462{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004463 vcpu->arch.pio.port = port;
4464 vcpu->arch.pio.in = in;
4465 vcpu->arch.pio.count = count;
4466 vcpu->arch.pio.size = size;
4467
4468 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
4469 vcpu->arch.pio.count = 0;
4470 return 1;
4471 }
4472
4473 vcpu->run->exit_reason = KVM_EXIT_IO;
4474 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
4475 vcpu->run->io.size = size;
4476 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
4477 vcpu->run->io.count = count;
4478 vcpu->run->io.port = port;
4479
4480 return 0;
4481}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004482
Avi Kivityca1d4a92011-04-20 13:37:53 +03004483static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
4484 int size, unsigned short port, void *val,
4485 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004486{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004487 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004488 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03004489
Gleb Natapov79729952010-03-18 15:20:24 +02004490 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004491 goto data_avail;
4492
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004493 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
4494 if (ret) {
4495data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004496 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004497 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02004498 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004499 return 1;
4500 }
4501
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004502 return 0;
4503}
4504
Avi Kivityca1d4a92011-04-20 13:37:53 +03004505static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
4506 int size, unsigned short port,
4507 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004508{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004509 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4510
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004511 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004512 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004513 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004514}
4515
Carsten Ottebbd9b642007-10-30 18:44:21 +01004516static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
4517{
4518 return kvm_x86_ops->get_segment_base(vcpu, seg);
4519}
4520
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004521static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004522{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004523 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004524}
4525
Joel Schopp5cb56052015-03-02 13:43:31 -06004526int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004527{
4528 if (!need_emulate_wbinvd(vcpu))
4529 return X86EMUL_CONTINUE;
4530
4531 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01004532 int cpu = get_cpu();
4533
4534 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004535 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
4536 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004537 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004538 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004539 } else
4540 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004541 return X86EMUL_CONTINUE;
4542}
Joel Schopp5cb56052015-03-02 13:43:31 -06004543
4544int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
4545{
4546 kvm_x86_ops->skip_emulated_instruction(vcpu);
4547 return kvm_emulate_wbinvd_noskip(vcpu);
4548}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004549EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
4550
Joel Schopp5cb56052015-03-02 13:43:31 -06004551
4552
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004553static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
4554{
Joel Schopp5cb56052015-03-02 13:43:31 -06004555 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004556}
4557
Xiubo Li52eb5a62015-03-13 17:39:45 +08004558static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
4559 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004560{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004561 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004562}
4563
Xiubo Li52eb5a62015-03-13 17:39:45 +08004564static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
4565 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004566{
Gleb Natapov338dbc92010-04-28 19:15:32 +03004567
Avi Kivity717746e2011-04-20 13:37:53 +03004568 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004569}
4570
Gleb Natapov52a46612010-03-18 15:20:03 +02004571static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4572{
4573 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4574}
4575
Avi Kivity717746e2011-04-20 13:37:53 +03004576static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02004577{
Avi Kivity717746e2011-04-20 13:37:53 +03004578 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02004579 unsigned long value;
4580
4581 switch (cr) {
4582 case 0:
4583 value = kvm_read_cr0(vcpu);
4584 break;
4585 case 2:
4586 value = vcpu->arch.cr2;
4587 break;
4588 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004589 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02004590 break;
4591 case 4:
4592 value = kvm_read_cr4(vcpu);
4593 break;
4594 case 8:
4595 value = kvm_get_cr8(vcpu);
4596 break;
4597 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004598 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02004599 return 0;
4600 }
4601
4602 return value;
4603}
4604
Avi Kivity717746e2011-04-20 13:37:53 +03004605static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02004606{
Avi Kivity717746e2011-04-20 13:37:53 +03004607 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03004608 int res = 0;
4609
Gleb Natapov52a46612010-03-18 15:20:03 +02004610 switch (cr) {
4611 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03004612 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004613 break;
4614 case 2:
4615 vcpu->arch.cr2 = val;
4616 break;
4617 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004618 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004619 break;
4620 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03004621 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004622 break;
4623 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004624 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004625 break;
4626 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004627 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03004628 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02004629 }
Gleb Natapov0f122442010-04-28 19:15:31 +03004630
4631 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02004632}
4633
Avi Kivity717746e2011-04-20 13:37:53 +03004634static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02004635{
Avi Kivity717746e2011-04-20 13:37:53 +03004636 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02004637}
4638
Avi Kivity4bff1e862011-04-20 13:37:53 +03004639static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004640{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004641 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004642}
4643
Avi Kivity4bff1e862011-04-20 13:37:53 +03004644static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004645{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004646 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004647}
4648
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004649static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4650{
4651 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
4652}
4653
4654static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4655{
4656 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
4657}
4658
Avi Kivity4bff1e862011-04-20 13:37:53 +03004659static unsigned long emulator_get_cached_segment_base(
4660 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03004661{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004662 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03004663}
4664
Avi Kivity1aa36612011-04-27 13:20:30 +03004665static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
4666 struct desc_struct *desc, u32 *base3,
4667 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004668{
4669 struct kvm_segment var;
4670
Avi Kivity4bff1e862011-04-20 13:37:53 +03004671 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03004672 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004673
Gleb Natapov378a8b02013-01-21 15:36:48 +02004674 if (var.unusable) {
4675 memset(desc, 0, sizeof(*desc));
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004676 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02004677 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004678
4679 if (var.g)
4680 var.limit >>= 12;
4681 set_desc_limit(desc, var.limit);
4682 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02004683#ifdef CONFIG_X86_64
4684 if (base3)
4685 *base3 = var.base >> 32;
4686#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004687 desc->type = var.type;
4688 desc->s = var.s;
4689 desc->dpl = var.dpl;
4690 desc->p = var.present;
4691 desc->avl = var.avl;
4692 desc->l = var.l;
4693 desc->d = var.db;
4694 desc->g = var.g;
4695
4696 return true;
4697}
4698
Avi Kivity1aa36612011-04-27 13:20:30 +03004699static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
4700 struct desc_struct *desc, u32 base3,
4701 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004702{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004703 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004704 struct kvm_segment var;
4705
Avi Kivity1aa36612011-04-27 13:20:30 +03004706 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004707 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02004708#ifdef CONFIG_X86_64
4709 var.base |= ((u64)base3) << 32;
4710#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004711 var.limit = get_desc_limit(desc);
4712 if (desc->g)
4713 var.limit = (var.limit << 12) | 0xfff;
4714 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004715 var.dpl = desc->dpl;
4716 var.db = desc->d;
4717 var.s = desc->s;
4718 var.l = desc->l;
4719 var.g = desc->g;
4720 var.avl = desc->avl;
4721 var.present = desc->p;
4722 var.unusable = !var.present;
4723 var.padding = 0;
4724
4725 kvm_set_segment(vcpu, &var, seg);
4726 return;
4727}
4728
Avi Kivity717746e2011-04-20 13:37:53 +03004729static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
4730 u32 msr_index, u64 *pdata)
4731{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004732 struct msr_data msr;
4733 int r;
4734
4735 msr.index = msr_index;
4736 msr.host_initiated = false;
4737 r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
4738 if (r)
4739 return r;
4740
4741 *pdata = msr.data;
4742 return 0;
Avi Kivity717746e2011-04-20 13:37:53 +03004743}
4744
4745static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
4746 u32 msr_index, u64 data)
4747{
Will Auld8fe8ab42012-11-29 12:42:12 -08004748 struct msr_data msr;
4749
4750 msr.data = data;
4751 msr.index = msr_index;
4752 msr.host_initiated = false;
4753 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03004754}
4755
Paolo Bonzini64d60672015-05-07 11:36:11 +02004756static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
4757{
4758 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4759
4760 return vcpu->arch.smbase;
4761}
4762
4763static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
4764{
4765 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4766
4767 vcpu->arch.smbase = smbase;
4768}
4769
Nadav Amit67f4d422014-06-02 18:34:09 +03004770static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
4771 u32 pmc)
4772{
Wei Huangc6702c92015-06-19 13:44:45 +02004773 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03004774}
4775
Avi Kivity222d21a2011-11-10 14:57:30 +02004776static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
4777 u32 pmc, u64 *pdata)
4778{
Wei Huangc6702c92015-06-19 13:44:45 +02004779 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02004780}
4781
Avi Kivity6c3287f2011-04-20 15:43:05 +03004782static void emulator_halt(struct x86_emulate_ctxt *ctxt)
4783{
4784 emul_to_vcpu(ctxt)->arch.halt_request = 1;
4785}
4786
Avi Kivity5037f6f2011-03-28 16:53:59 +02004787static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
4788{
4789 preempt_disable();
Avi Kivity5197b802011-04-20 15:55:40 +03004790 kvm_load_guest_fpu(emul_to_vcpu(ctxt));
Avi Kivity5037f6f2011-03-28 16:53:59 +02004791 /*
4792 * CR0.TS may reference the host fpu state, not the guest fpu state,
4793 * so it may be clear at this point.
4794 */
4795 clts();
4796}
4797
4798static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
4799{
4800 preempt_enable();
4801}
4802
Avi Kivity29535382011-04-20 13:37:53 +03004803static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004804 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004805 enum x86_intercept_stage stage)
4806{
Avi Kivity29535382011-04-20 13:37:53 +03004807 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004808}
4809
Avi Kivity0017f932012-06-07 14:10:16 +03004810static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004811 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
4812{
Avi Kivity0017f932012-06-07 14:10:16 +03004813 kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004814}
4815
Avi Kivitydd856ef2012-08-27 23:46:17 +03004816static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
4817{
4818 return kvm_register_read(emul_to_vcpu(ctxt), reg);
4819}
4820
4821static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
4822{
4823 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
4824}
4825
Nadav Amit801806d2015-01-26 09:32:23 +02004826static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
4827{
4828 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
4829}
4830
Mathias Krause0225fb52012-08-30 01:30:16 +02004831static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03004832 .read_gpr = emulator_read_gpr,
4833 .write_gpr = emulator_write_gpr,
Gleb Natapov1871c602010-02-10 14:21:32 +02004834 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004835 .write_std = kvm_write_guest_virt_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02004836 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004837 .read_emulated = emulator_read_emulated,
4838 .write_emulated = emulator_write_emulated,
4839 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004840 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004841 .pio_in_emulated = emulator_pio_in_emulated,
4842 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03004843 .get_segment = emulator_get_segment,
4844 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03004845 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004846 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004847 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004848 .set_gdt = emulator_set_gdt,
4849 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02004850 .get_cr = emulator_get_cr,
4851 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02004852 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03004853 .get_dr = emulator_get_dr,
4854 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02004855 .get_smbase = emulator_get_smbase,
4856 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03004857 .set_msr = emulator_set_msr,
4858 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03004859 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02004860 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03004861 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004862 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03004863 .fix_hypercall = emulator_fix_hypercall,
Avi Kivity5037f6f2011-03-28 16:53:59 +02004864 .get_fpu = emulator_get_fpu,
4865 .put_fpu = emulator_put_fpu,
Avi Kivityc4f035c2011-04-04 12:39:22 +02004866 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01004867 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02004868 .set_nmi_mask = emulator_set_nmi_mask,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004869};
4870
Gleb Natapov95cb2292010-04-28 19:15:43 +03004871static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
4872{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004873 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03004874 /*
4875 * an sti; sti; sequence only disable interrupts for the first
4876 * instruction. So, if the last instruction, be it emulated or
4877 * not, left the system with the INT_STI flag enabled, it
4878 * means that the last instruction is an sti. We should not
4879 * leave the flag on in this case. The same goes for mov ss
4880 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02004881 if (int_shadow & mask)
4882 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004883 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004884 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01004885 if (!mask)
4886 kvm_make_request(KVM_REQ_EVENT, vcpu);
4887 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03004888}
4889
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004890static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03004891{
4892 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02004893 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004894 return kvm_propagate_fault(vcpu, &ctxt->exception);
4895
4896 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02004897 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
4898 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03004899 else
Avi Kivityda9cb572010-11-22 17:53:21 +02004900 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02004901 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03004902}
4903
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004904static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
4905{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004906 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004907 int cs_db, cs_l;
4908
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004909 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4910
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004911 ctxt->eflags = kvm_get_rflags(vcpu);
4912 ctxt->eip = kvm_rip_read(vcpu);
4913 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
4914 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03004915 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004916 cs_db ? X86EMUL_MODE_PROT32 :
4917 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02004918 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02004919 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
4920 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Paolo Bonzinia5845392015-04-01 18:18:53 +02004921 ctxt->emul_flags = vcpu->arch.hflags;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09004922
Avi Kivitydd856ef2012-08-27 23:46:17 +03004923 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02004924 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03004925}
4926
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004927int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02004928{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004929 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02004930 int ret;
4931
4932 init_emulate_ctxt(vcpu);
4933
Avi Kivity9dac77f2011-06-01 15:34:25 +03004934 ctxt->op_bytes = 2;
4935 ctxt->ad_bytes = 2;
4936 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004937 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02004938
4939 if (ret != X86EMUL_CONTINUE)
4940 return EMULATE_FAIL;
4941
Avi Kivity9dac77f2011-06-01 15:34:25 +03004942 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004943 kvm_rip_write(vcpu, ctxt->eip);
4944 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02004945
4946 if (irq == NMI_VECTOR)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03004947 vcpu->arch.nmi_pending = 0;
Mohammed Gamal63995652010-09-19 14:34:06 +02004948 else
4949 vcpu->arch.interrupt.pending = false;
4950
4951 return EMULATE_DONE;
4952}
4953EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
4954
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004955static int handle_emulation_failure(struct kvm_vcpu *vcpu)
4956{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004957 int r = EMULATE_DONE;
4958
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004959 ++vcpu->stat.insn_emulation_fail;
4960 trace_kvm_emulate_insn_failed(vcpu);
Nadav Amita2b9e6c2014-09-17 02:50:50 +03004961 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004962 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4963 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4964 vcpu->run->internal.ndata = 0;
4965 r = EMULATE_FAIL;
4966 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004967 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01004968
4969 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004970}
4971
Xiao Guangrong93c05d32013-01-13 23:49:07 +08004972static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03004973 bool write_fault_to_shadow_pgtable,
4974 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03004975{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08004976 gpa_t gpa = cr2;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08004977 pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03004978
Gleb Natapov991eebf2013-04-11 12:10:51 +03004979 if (emulation_type & EMULTYPE_NO_REEXECUTE)
4980 return false;
4981
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08004982 if (!vcpu->arch.mmu.direct_map) {
4983 /*
4984 * Write permission should be allowed since only
4985 * write access need to be emulated.
4986 */
4987 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03004988
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08004989 /*
4990 * If the mapping is invalid in guest, let cpu retry
4991 * it to generate fault.
4992 */
4993 if (gpa == UNMAPPED_GVA)
4994 return true;
4995 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03004996
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08004997 /*
4998 * Do not retry the unhandleable instruction if it faults on the
4999 * readonly host memory, otherwise it will goto a infinite loop:
5000 * retry instruction -> write #PF -> emulation fail -> retry
5001 * instruction -> ...
5002 */
5003 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005004
5005 /*
5006 * If the instruction failed on the error pfn, it can not be fixed,
5007 * report the error to userspace.
5008 */
5009 if (is_error_noslot_pfn(pfn))
5010 return false;
5011
5012 kvm_release_pfn_clean(pfn);
5013
5014 /* The instructions are well-emulated on direct mmu. */
5015 if (vcpu->arch.mmu.direct_map) {
5016 unsigned int indirect_shadow_pages;
5017
5018 spin_lock(&vcpu->kvm->mmu_lock);
5019 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5020 spin_unlock(&vcpu->kvm->mmu_lock);
5021
5022 if (indirect_shadow_pages)
5023 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5024
Gleb Natapova6f177e2010-07-08 12:41:12 +03005025 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005026 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005027
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005028 /*
5029 * if emulation was due to access to shadowed page table
5030 * and it failed try to unshadow page and re-enter the
5031 * guest to let CPU execute the instruction.
5032 */
5033 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005034
5035 /*
5036 * If the access faults on its page table, it can not
5037 * be fixed by unprotecting shadow page and it should
5038 * be reported to userspace.
5039 */
5040 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005041}
5042
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005043static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5044 unsigned long cr2, int emulation_type)
5045{
5046 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5047 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5048
5049 last_retry_eip = vcpu->arch.last_retry_eip;
5050 last_retry_addr = vcpu->arch.last_retry_addr;
5051
5052 /*
5053 * If the emulation is caused by #PF and it is non-page_table
5054 * writing instruction, it means the VM-EXIT is caused by shadow
5055 * page protected, we can zap the shadow page and retry this
5056 * instruction directly.
5057 *
5058 * Note: if the guest uses a non-page-table modifying instruction
5059 * on the PDE that points to the instruction, then we will unmap
5060 * the instruction and go to an infinite loop. So, we cache the
5061 * last retried eip and the last fault address, if we meet the eip
5062 * and the address again, we can break out of the potential infinite
5063 * loop.
5064 */
5065 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5066
5067 if (!(emulation_type & EMULTYPE_RETRY))
5068 return false;
5069
5070 if (x86_page_table_writing_insn(ctxt))
5071 return false;
5072
5073 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5074 return false;
5075
5076 vcpu->arch.last_retry_eip = ctxt->eip;
5077 vcpu->arch.last_retry_addr = cr2;
5078
5079 if (!vcpu->arch.mmu.direct_map)
5080 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5081
Xiao Guangrong22368022013-01-13 23:44:12 +08005082 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005083
5084 return true;
5085}
5086
Gleb Natapov716d51a2012-09-03 15:24:26 +03005087static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5088static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5089
Paolo Bonzini64d60672015-05-07 11:36:11 +02005090static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02005091{
Paolo Bonzini64d60672015-05-07 11:36:11 +02005092 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005093 /* This is a good place to trace that we are exiting SMM. */
5094 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
5095
Paolo Bonzini64d60672015-05-07 11:36:11 +02005096 if (unlikely(vcpu->arch.smi_pending)) {
5097 kvm_make_request(KVM_REQ_SMI, vcpu);
5098 vcpu->arch.smi_pending = 0;
Paolo Bonzinicd7764f2015-06-04 10:41:21 +02005099 } else {
5100 /* Process a latched INIT, if any. */
5101 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005102 }
5103 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02005104
5105 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005106}
5107
5108static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
5109{
5110 unsigned changed = vcpu->arch.hflags ^ emul_flags;
5111
Paolo Bonzinia5845392015-04-01 18:18:53 +02005112 vcpu->arch.hflags = emul_flags;
Paolo Bonzini64d60672015-05-07 11:36:11 +02005113
5114 if (changed & HF_SMM_MASK)
5115 kvm_smm_changed(vcpu);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005116}
5117
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005118static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
5119 unsigned long *db)
5120{
5121 u32 dr6 = 0;
5122 int i;
5123 u32 enable, rwlen;
5124
5125 enable = dr7;
5126 rwlen = dr7 >> 16;
5127 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
5128 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
5129 dr6 |= (1 << i);
5130 return dr6;
5131}
5132
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005133static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005134{
5135 struct kvm_run *kvm_run = vcpu->run;
5136
5137 /*
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005138 * rflags is the old, "raw" value of the flags. The new value has
5139 * not been saved yet.
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005140 *
5141 * This is correct even for TF set by the guest, because "the
5142 * processor will not generate this exception after the instruction
5143 * that sets the TF flag".
5144 */
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005145 if (unlikely(rflags & X86_EFLAGS_TF)) {
5146 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005147 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5148 DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005149 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5150 kvm_run->debug.arch.exception = DB_VECTOR;
5151 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5152 *r = EMULATE_USER_EXIT;
5153 } else {
5154 vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5155 /*
5156 * "Certain debug exceptions may clear bit 0-3. The
5157 * remaining contents of the DR6 register are never
5158 * cleared by the processor".
5159 */
5160 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005161 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005162 kvm_queue_exception(vcpu, DB_VECTOR);
5163 }
5164 }
5165}
5166
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005167static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
5168{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005169 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
5170 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005171 struct kvm_run *kvm_run = vcpu->run;
5172 unsigned long eip = kvm_get_linear_rip(vcpu);
5173 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005174 vcpu->arch.guest_debug_dr7,
5175 vcpu->arch.eff_db);
5176
5177 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005178 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02005179 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005180 kvm_run->debug.arch.exception = DB_VECTOR;
5181 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5182 *r = EMULATE_USER_EXIT;
5183 return true;
5184 }
5185 }
5186
Nadav Amit4161a562014-07-17 01:19:31 +03005187 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
5188 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005189 unsigned long eip = kvm_get_linear_rip(vcpu);
5190 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005191 vcpu->arch.dr7,
5192 vcpu->arch.db);
5193
5194 if (dr6 != 0) {
5195 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005196 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005197 kvm_queue_exception(vcpu, DB_VECTOR);
5198 *r = EMULATE_DONE;
5199 return true;
5200 }
5201 }
5202
5203 return false;
5204}
5205
Andre Przywara51d8b662010-12-21 11:12:02 +01005206int x86_emulate_instruction(struct kvm_vcpu *vcpu,
5207 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01005208 int emulation_type,
5209 void *insn,
5210 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005211{
Gleb Natapov95cb2292010-04-28 19:15:43 +03005212 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005213 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005214 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005215 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005216
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005217 /*
5218 * Clear write_fault_to_shadow_pgtable here to ensure it is
5219 * never reused.
5220 */
5221 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03005222 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03005223
Sheng Yang571008d2008-01-02 14:49:22 +08005224 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005225 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005226
5227 /*
5228 * We will reenter on the same instruction since
5229 * we do not set complete_userspace_io. This does not
5230 * handle watchpoints yet, those would be handled in
5231 * the emulate_ops.
5232 */
5233 if (kvm_vcpu_check_breakpoint(vcpu, &r))
5234 return r;
5235
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005236 ctxt->interruptibility = 0;
5237 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005238 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005239 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005240
Borislav Petkovb51e9742013-09-22 16:44:52 +02005241 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02005242
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005243 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02005244
Avi Kivitye46479f2010-04-11 13:05:16 +03005245 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02005246 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005247 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02005248 if (emulation_type & EMULTYPE_TRAP_UD)
5249 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03005250 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5251 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005252 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005253 if (emulation_type & EMULTYPE_SKIP)
5254 return EMULATE_FAIL;
5255 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005256 }
5257 }
5258
Gleb Natapovba8afb62009-04-12 13:36:57 +03005259 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005260 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03005261 if (ctxt->eflags & X86_EFLAGS_RF)
5262 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03005263 return EMULATE_DONE;
5264 }
5265
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005266 if (retry_instruction(ctxt, cr2, emulation_type))
5267 return EMULATE_DONE;
5268
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005269 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005270 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005271 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
5272 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005273 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005274 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005275
Gleb Natapov5cd21912010-03-18 15:20:26 +02005276restart:
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005277 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005278
Joerg Roedel775fde82011-04-04 12:39:24 +02005279 if (r == EMULATION_INTERCEPTED)
5280 return EMULATE_DONE;
5281
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005282 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03005283 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5284 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005285 return EMULATE_DONE;
5286
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005287 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005288 }
5289
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005290 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005291 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005292 if (inject_emulated_exception(vcpu))
5293 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005294 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02005295 if (!vcpu->arch.pio.in) {
5296 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03005297 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02005298 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005299 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005300 vcpu->arch.complete_userspace_io = complete_emulated_pio;
5301 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005302 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005303 } else if (vcpu->mmio_needed) {
5304 if (!vcpu->mmio_is_write)
5305 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005306 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005307 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005308 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03005309 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005310 else
5311 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03005312
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005313 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005314 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005315 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005316 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005317 if (vcpu->arch.hflags != ctxt->emul_flags)
5318 kvm_set_hflags(vcpu, ctxt->emul_flags);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005319 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005320 if (r == EMULATE_DONE)
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005321 kvm_vcpu_check_singlestep(vcpu, rflags, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02005322 if (!ctxt->have_exception ||
5323 exception_type(ctxt->exception.vector) == EXCPT_TRAP)
5324 __kvm_set_rflags(vcpu, ctxt->eflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005325
5326 /*
5327 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
5328 * do nothing, and it will be requested again as soon as
5329 * the shadow expires. But we still need to check here,
5330 * because POPF has no interrupt shadow.
5331 */
5332 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
5333 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005334 } else
5335 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03005336
Gleb Natapove85d28f2010-07-29 15:11:52 +03005337 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005338}
Andre Przywara51d8b662010-12-21 11:12:02 +01005339EXPORT_SYMBOL_GPL(x86_emulate_instruction);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005340
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005341int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01005342{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005343 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005344 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
5345 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005346 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02005347 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02005348 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01005349}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005350EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01005351
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005352static void tsc_bad(void *info)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005353{
Tejun Heo0a3aee02010-12-18 16:28:55 +01005354 __this_cpu_write(cpu_tsc_khz, 0);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005355}
5356
5357static void tsc_khz_changed(void *data)
5358{
5359 struct cpufreq_freqs *freq = data;
5360 unsigned long khz = 0;
5361
5362 if (data)
5363 khz = freq->new;
5364 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5365 khz = cpufreq_quick_get(raw_smp_processor_id());
5366 if (!khz)
5367 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01005368 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005369}
5370
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005371static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
5372 void *data)
5373{
5374 struct cpufreq_freqs *freq = data;
5375 struct kvm *kvm;
5376 struct kvm_vcpu *vcpu;
5377 int i, send_ipi = 0;
5378
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005379 /*
5380 * We allow guests to temporarily run on slowing clocks,
5381 * provided we notify them after, or to run on accelerating
5382 * clocks, provided we notify them before. Thus time never
5383 * goes backwards.
5384 *
5385 * However, we have a problem. We can't atomically update
5386 * the frequency of a given CPU from this function; it is
5387 * merely a notifier, which can be called from any CPU.
5388 * Changing the TSC frequency at arbitrary points in time
5389 * requires a recomputation of local variables related to
5390 * the TSC for each VCPU. We must flag these local variables
5391 * to be updated and be sure the update takes place with the
5392 * new frequency before any guests proceed.
5393 *
5394 * Unfortunately, the combination of hotplug CPU and frequency
5395 * change creates an intractable locking scenario; the order
5396 * of when these callouts happen is undefined with respect to
5397 * CPU hotplug, and they can race with each other. As such,
5398 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
5399 * undefined; you can actually have a CPU frequency change take
5400 * place in between the computation of X and the setting of the
5401 * variable. To protect against this problem, all updates of
5402 * the per_cpu tsc_khz variable are done in an interrupt
5403 * protected IPI, and all callers wishing to update the value
5404 * must wait for a synchronous IPI to complete (which is trivial
5405 * if the caller is on the CPU already). This establishes the
5406 * necessary total order on variable updates.
5407 *
5408 * Note that because a guest time update may take place
5409 * anytime after the setting of the VCPU's request bit, the
5410 * correct TSC value must be set before the request. However,
5411 * to ensure the update actually makes it to any guest which
5412 * starts running in hardware virtualization between the set
5413 * and the acquisition of the spinlock, we must also ping the
5414 * CPU after setting the request bit.
5415 *
5416 */
5417
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005418 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
5419 return 0;
5420 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
5421 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005422
5423 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005424
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005425 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005426 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005427 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005428 if (vcpu->cpu != freq->cpu)
5429 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10005430 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005431 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005432 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005433 }
5434 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005435 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005436
5437 if (freq->old < freq->new && send_ipi) {
5438 /*
5439 * We upscale the frequency. Must make the guest
5440 * doesn't see old kvmclock values while running with
5441 * the new frequency, otherwise we risk the guest sees
5442 * time go backwards.
5443 *
5444 * In case we update the frequency for another cpu
5445 * (which might be in guest context) send an interrupt
5446 * to kick the cpu out of guest context. Next time
5447 * guest context is entered kvmclock will be updated,
5448 * so the guest will not see stale values.
5449 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005450 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005451 }
5452 return 0;
5453}
5454
5455static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005456 .notifier_call = kvmclock_cpufreq_notifier
5457};
5458
5459static int kvmclock_cpu_notifier(struct notifier_block *nfb,
5460 unsigned long action, void *hcpu)
5461{
5462 unsigned int cpu = (unsigned long)hcpu;
5463
5464 switch (action) {
5465 case CPU_ONLINE:
5466 case CPU_DOWN_FAILED:
5467 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
5468 break;
5469 case CPU_DOWN_PREPARE:
5470 smp_call_function_single(cpu, tsc_bad, NULL, 1);
5471 break;
5472 }
5473 return NOTIFY_OK;
5474}
5475
5476static struct notifier_block kvmclock_cpu_notifier_block = {
5477 .notifier_call = kvmclock_cpu_notifier,
5478 .priority = -INT_MAX
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005479};
5480
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005481static void kvm_timer_init(void)
5482{
5483 int cpu;
5484
Zachary Amsdenc2855452010-09-18 14:38:15 -10005485 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305486
5487 cpu_notifier_register_begin();
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005488 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10005489#ifdef CONFIG_CPU_FREQ
5490 struct cpufreq_policy policy;
5491 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02005492 cpu = get_cpu();
5493 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10005494 if (policy.cpuinfo.max_freq)
5495 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02005496 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10005497#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005498 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
5499 CPUFREQ_TRANSITION_NOTIFIER);
5500 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10005501 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005502 for_each_online_cpu(cpu)
5503 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305504
5505 __register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
5506 cpu_notifier_register_done();
5507
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005508}
5509
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005510static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
5511
Gleb Natapovf5132b02011-11-10 14:57:22 +02005512int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005513{
Alex,Shi086c9852011-10-20 15:34:01 +08005514 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005515}
5516
5517static int kvm_is_user_mode(void)
5518{
5519 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005520
Alex,Shi086c9852011-10-20 15:34:01 +08005521 if (__this_cpu_read(current_vcpu))
5522 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005523
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005524 return user_mode != 0;
5525}
5526
5527static unsigned long kvm_get_guest_ip(void)
5528{
5529 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005530
Alex,Shi086c9852011-10-20 15:34:01 +08005531 if (__this_cpu_read(current_vcpu))
5532 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005533
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005534 return ip;
5535}
5536
5537static struct perf_guest_info_callbacks kvm_guest_cbs = {
5538 .is_in_guest = kvm_is_in_guest,
5539 .is_user_mode = kvm_is_user_mode,
5540 .get_guest_ip = kvm_get_guest_ip,
5541};
5542
5543void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
5544{
Alex,Shi086c9852011-10-20 15:34:01 +08005545 __this_cpu_write(current_vcpu, vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005546}
5547EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
5548
5549void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
5550{
Alex,Shi086c9852011-10-20 15:34:01 +08005551 __this_cpu_write(current_vcpu, NULL);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005552}
5553EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
5554
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005555static void kvm_set_mmio_spte_mask(void)
5556{
5557 u64 mask;
5558 int maxphyaddr = boot_cpu_data.x86_phys_bits;
5559
5560 /*
5561 * Set the reserved bits and the present bit of an paging-structure
5562 * entry to generate page fault with PFER.RSV = 1.
5563 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005564 /* Mask the reserved physical address bits. */
Tiejun Chend1431482014-09-01 18:44:04 +08005565 mask = rsvd_bits(maxphyaddr, 51);
Xiao Guangrong885032b2013-06-07 16:51:23 +08005566
5567 /* Bit 62 is always reserved for 32bit host. */
5568 mask |= 0x3ull << 62;
5569
5570 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005571 mask |= 1ull;
5572
5573#ifdef CONFIG_X86_64
5574 /*
5575 * If reserved bit is not supported, clear the present bit to disable
5576 * mmio page fault.
5577 */
5578 if (maxphyaddr == 52)
5579 mask &= ~1ull;
5580#endif
5581
5582 kvm_mmu_set_mmio_spte_mask(mask);
5583}
5584
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005585#ifdef CONFIG_X86_64
5586static void pvclock_gtod_update_fn(struct work_struct *work)
5587{
Marcelo Tosattid8281992012-11-27 23:29:01 -02005588 struct kvm *kvm;
5589
5590 struct kvm_vcpu *vcpu;
5591 int i;
5592
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005593 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005594 list_for_each_entry(kvm, &vm_list, vm_list)
5595 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08005596 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005597 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005598 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005599}
5600
5601static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
5602
5603/*
5604 * Notification about pvclock gtod data update.
5605 */
5606static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
5607 void *priv)
5608{
5609 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
5610 struct timekeeper *tk = priv;
5611
5612 update_pvclock_gtod(tk);
5613
5614 /* disable master clock if host does not trust, or does not
5615 * use, TSC clocksource
5616 */
5617 if (gtod->clock.vclock_mode != VCLOCK_TSC &&
5618 atomic_read(&kvm_guest_has_master_clock) != 0)
5619 queue_work(system_long_wq, &pvclock_gtod_work);
5620
5621 return 0;
5622}
5623
5624static struct notifier_block pvclock_gtod_notifier = {
5625 .notifier_call = pvclock_gtod_notify,
5626};
5627#endif
5628
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005629int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02005630{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005631 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02005632 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005633
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005634 if (kvm_x86_ops) {
5635 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005636 r = -EEXIST;
5637 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005638 }
5639
5640 if (!ops->cpu_has_kvm_support()) {
5641 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005642 r = -EOPNOTSUPP;
5643 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005644 }
5645 if (ops->disabled_by_bios()) {
5646 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005647 r = -EOPNOTSUPP;
5648 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005649 }
5650
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005651 r = -ENOMEM;
5652 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
5653 if (!shared_msrs) {
5654 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
5655 goto out;
5656 }
5657
Avi Kivity97db56c2008-01-13 13:23:56 +02005658 r = kvm_mmu_module_init();
5659 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005660 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02005661
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005662 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02005663
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005664 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01005665
Sheng Yang7b523452008-04-25 21:13:50 +08005666 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005667 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005668
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005669 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005670
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005671 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5672
Dexuan Cui2acf9232010-06-10 11:27:12 +08005673 if (cpu_has_xsave)
5674 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
5675
Gleb Natapovc5cc4212012-08-05 15:58:30 +03005676 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005677#ifdef CONFIG_X86_64
5678 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
5679#endif
5680
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005681 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005682
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005683out_free_percpu:
5684 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005685out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005686 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02005687}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005688
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005689void kvm_arch_exit(void)
5690{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005691 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5692
Jan Kiszka888d2562009-04-17 19:24:58 +02005693 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5694 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
5695 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005696 unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005697#ifdef CONFIG_X86_64
5698 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
5699#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005700 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005701 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005702 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005703}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005704
Joel Schopp5cb56052015-03-02 13:43:31 -06005705int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005706{
5707 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005708 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03005709 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005710 return 1;
5711 } else {
5712 vcpu->run->exit_reason = KVM_EXIT_HLT;
5713 return 0;
5714 }
5715}
Joel Schopp5cb56052015-03-02 13:43:31 -06005716EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
5717
5718int kvm_emulate_halt(struct kvm_vcpu *vcpu)
5719{
5720 kvm_x86_ops->skip_emulated_instruction(vcpu);
5721 return kvm_vcpu_halt(vcpu);
5722}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005723EXPORT_SYMBOL_GPL(kvm_emulate_halt);
5724
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305725/*
5726 * kvm_pv_kick_cpu_op: Kick a vcpu.
5727 *
5728 * @apicid - apicid of vcpu to be kicked.
5729 */
5730static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
5731{
Raghavendra K T24d21662013-08-26 14:18:35 +05305732 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305733
Raghavendra K T24d21662013-08-26 14:18:35 +05305734 lapic_irq.shorthand = 0;
5735 lapic_irq.dest_mode = 0;
5736 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06005737 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305738
Raghavendra K T24d21662013-08-26 14:18:35 +05305739 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08005740 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305741}
5742
Hollis Blanchard8776e512007-10-31 17:24:24 -05005743int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
5744{
5745 unsigned long nr, a0, a1, a2, a3, ret;
Nadav Amita449c7a2014-06-18 17:19:24 +03005746 int op_64_bit, r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005747
Joel Schopp5cb56052015-03-02 13:43:31 -06005748 kvm_x86_ops->skip_emulated_instruction(vcpu);
5749
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005750 if (kvm_hv_hypercall_enabled(vcpu->kvm))
5751 return kvm_hv_hypercall(vcpu);
5752
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005753 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
5754 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
5755 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
5756 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
5757 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005758
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005759 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005760
Nadav Amita449c7a2014-06-18 17:19:24 +03005761 op_64_bit = is_64_bit_mode(vcpu);
5762 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05005763 nr &= 0xFFFFFFFF;
5764 a0 &= 0xFFFFFFFF;
5765 a1 &= 0xFFFFFFFF;
5766 a2 &= 0xFFFFFFFF;
5767 a3 &= 0xFFFFFFFF;
5768 }
5769
Jan Kiszka07708c42009-08-03 18:43:28 +02005770 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
5771 ret = -KVM_EPERM;
5772 goto out;
5773 }
5774
Hollis Blanchard8776e512007-10-31 17:24:24 -05005775 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02005776 case KVM_HC_VAPIC_POLL_IRQ:
5777 ret = 0;
5778 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305779 case KVM_HC_KICK_CPU:
5780 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
5781 ret = 0;
5782 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005783 default:
5784 ret = -KVM_ENOSYS;
5785 break;
5786 }
Jan Kiszka07708c42009-08-03 18:43:28 +02005787out:
Nadav Amita449c7a2014-06-18 17:19:24 +03005788 if (!op_64_bit)
5789 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005790 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05305791 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05005792 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005793}
5794EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
5795
Jan Kiszkab6785de2012-09-20 07:43:17 +02005796static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005797{
Avi Kivityd6aa1002011-04-20 15:47:13 +03005798 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005799 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005800 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005801
Hollis Blanchard8776e512007-10-31 17:24:24 -05005802 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005803
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005804 return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005805}
5806
Hollis Blanchardd0752062007-10-31 17:24:25 -05005807/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005808 * Check if userspace requested an interrupt window, and that the
5809 * interrupt window is open.
5810 *
5811 * No need to exit to userspace if we already have an interrupt queued.
5812 */
Avi Kivity851ba692009-08-24 11:10:17 +03005813static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005814{
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02005815 if (!vcpu->run->request_interrupt_window || pic_in_kernel(vcpu->kvm))
5816 return false;
5817
5818 if (kvm_cpu_has_interrupt(vcpu))
5819 return false;
5820
5821 return (irqchip_split(vcpu->kvm)
5822 ? kvm_apic_accept_pic_intr(vcpu)
5823 : kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005824}
5825
Avi Kivity851ba692009-08-24 11:10:17 +03005826static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005827{
Avi Kivity851ba692009-08-24 11:10:17 +03005828 struct kvm_run *kvm_run = vcpu->run;
5829
Jan Kiszka91586a32009-10-05 13:07:21 +02005830 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02005831 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005832 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005833 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02005834 if (!irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005835 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03005836 kvm_arch_interrupt_allowed(vcpu) &&
5837 !kvm_cpu_has_interrupt(vcpu) &&
5838 !kvm_event_needs_reinjection(vcpu);
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02005839 else if (!pic_in_kernel(vcpu->kvm))
5840 kvm_run->ready_for_interrupt_injection =
5841 kvm_apic_accept_pic_intr(vcpu) &&
5842 !kvm_cpu_has_interrupt(vcpu);
5843 else
5844 kvm_run->ready_for_interrupt_injection = 1;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005845}
5846
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005847static void update_cr8_intercept(struct kvm_vcpu *vcpu)
5848{
5849 int max_irr, tpr;
5850
5851 if (!kvm_x86_ops->update_cr8_intercept)
5852 return;
5853
Avi Kivity88c808f2009-08-17 22:49:40 +03005854 if (!vcpu->arch.apic)
5855 return;
5856
Gleb Natapov8db3baa2009-05-11 13:35:54 +03005857 if (!vcpu->arch.apic->vapic_addr)
5858 max_irr = kvm_lapic_find_highest_irr(vcpu);
5859 else
5860 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005861
5862 if (max_irr != -1)
5863 max_irr >>= 4;
5864
5865 tpr = kvm_lapic_get_cr8(vcpu);
5866
5867 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
5868}
5869
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005870static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005871{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005872 int r;
5873
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005874 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005875 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02005876 trace_kvm_inj_exception(vcpu->arch.exception.nr,
5877 vcpu->arch.exception.has_error_code,
5878 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03005879
5880 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
5881 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
5882 X86_EFLAGS_RF);
5883
Nadav Amit6bdf0662014-09-30 20:49:14 +03005884 if (vcpu->arch.exception.nr == DB_VECTOR &&
5885 (vcpu->arch.dr7 & DR7_GD)) {
5886 vcpu->arch.dr7 &= ~DR7_GD;
5887 kvm_update_dr7(vcpu);
5888 }
5889
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005890 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
5891 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005892 vcpu->arch.exception.error_code,
5893 vcpu->arch.exception.reinject);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005894 return 0;
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03005895 }
5896
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005897 if (vcpu->arch.nmi_injected) {
5898 kvm_x86_ops->set_nmi(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005899 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005900 }
5901
5902 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005903 kvm_x86_ops->set_irq(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005904 return 0;
5905 }
5906
5907 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
5908 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
5909 if (r != 0)
5910 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005911 }
5912
5913 /* try to inject new event if pending */
5914 if (vcpu->arch.nmi_pending) {
5915 if (kvm_x86_ops->nmi_allowed(vcpu)) {
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005916 --vcpu->arch.nmi_pending;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005917 vcpu->arch.nmi_injected = true;
5918 kvm_x86_ops->set_nmi(vcpu);
5919 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08005920 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04005921 /*
5922 * Because interrupts can be injected asynchronously, we are
5923 * calling check_nested_events again here to avoid a race condition.
5924 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
5925 * proposal and current concerns. Perhaps we should be setting
5926 * KVM_REQ_EVENT only on certain events and not unconditionally?
5927 */
5928 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
5929 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
5930 if (r != 0)
5931 return r;
5932 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005933 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005934 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
5935 false);
5936 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005937 }
5938 }
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005939 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005940}
5941
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005942static void process_nmi(struct kvm_vcpu *vcpu)
5943{
5944 unsigned limit = 2;
5945
5946 /*
5947 * x86 is limited to one NMI running, and one NMI pending after it.
5948 * If an NMI is already in progress, limit further NMIs to just one.
5949 * Otherwise, allow two (and we'll inject the first one immediately).
5950 */
5951 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
5952 limit = 1;
5953
5954 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
5955 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
5956 kvm_make_request(KVM_REQ_EVENT, vcpu);
5957}
5958
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005959#define put_smstate(type, buf, offset, val) \
5960 *(type *)((buf) + (offset) - 0x7e00) = val
5961
5962static u32 process_smi_get_segment_flags(struct kvm_segment *seg)
5963{
5964 u32 flags = 0;
5965 flags |= seg->g << 23;
5966 flags |= seg->db << 22;
5967 flags |= seg->l << 21;
5968 flags |= seg->avl << 20;
5969 flags |= seg->present << 15;
5970 flags |= seg->dpl << 13;
5971 flags |= seg->s << 12;
5972 flags |= seg->type << 8;
5973 return flags;
5974}
5975
5976static void process_smi_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
5977{
5978 struct kvm_segment seg;
5979 int offset;
5980
5981 kvm_get_segment(vcpu, &seg, n);
5982 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
5983
5984 if (n < 3)
5985 offset = 0x7f84 + n * 12;
5986 else
5987 offset = 0x7f2c + (n - 3) * 12;
5988
5989 put_smstate(u32, buf, offset + 8, seg.base);
5990 put_smstate(u32, buf, offset + 4, seg.limit);
5991 put_smstate(u32, buf, offset, process_smi_get_segment_flags(&seg));
5992}
5993
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06005994#ifdef CONFIG_X86_64
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005995static void process_smi_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
5996{
5997 struct kvm_segment seg;
5998 int offset;
5999 u16 flags;
6000
6001 kvm_get_segment(vcpu, &seg, n);
6002 offset = 0x7e00 + n * 16;
6003
6004 flags = process_smi_get_segment_flags(&seg) >> 8;
6005 put_smstate(u16, buf, offset, seg.selector);
6006 put_smstate(u16, buf, offset + 2, flags);
6007 put_smstate(u32, buf, offset + 4, seg.limit);
6008 put_smstate(u64, buf, offset + 8, seg.base);
6009}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06006010#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006011
6012static void process_smi_save_state_32(struct kvm_vcpu *vcpu, char *buf)
6013{
6014 struct desc_ptr dt;
6015 struct kvm_segment seg;
6016 unsigned long val;
6017 int i;
6018
6019 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
6020 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
6021 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
6022 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
6023
6024 for (i = 0; i < 8; i++)
6025 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
6026
6027 kvm_get_dr(vcpu, 6, &val);
6028 put_smstate(u32, buf, 0x7fcc, (u32)val);
6029 kvm_get_dr(vcpu, 7, &val);
6030 put_smstate(u32, buf, 0x7fc8, (u32)val);
6031
6032 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6033 put_smstate(u32, buf, 0x7fc4, seg.selector);
6034 put_smstate(u32, buf, 0x7f64, seg.base);
6035 put_smstate(u32, buf, 0x7f60, seg.limit);
6036 put_smstate(u32, buf, 0x7f5c, process_smi_get_segment_flags(&seg));
6037
6038 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6039 put_smstate(u32, buf, 0x7fc0, seg.selector);
6040 put_smstate(u32, buf, 0x7f80, seg.base);
6041 put_smstate(u32, buf, 0x7f7c, seg.limit);
6042 put_smstate(u32, buf, 0x7f78, process_smi_get_segment_flags(&seg));
6043
6044 kvm_x86_ops->get_gdt(vcpu, &dt);
6045 put_smstate(u32, buf, 0x7f74, dt.address);
6046 put_smstate(u32, buf, 0x7f70, dt.size);
6047
6048 kvm_x86_ops->get_idt(vcpu, &dt);
6049 put_smstate(u32, buf, 0x7f58, dt.address);
6050 put_smstate(u32, buf, 0x7f54, dt.size);
6051
6052 for (i = 0; i < 6; i++)
6053 process_smi_save_seg_32(vcpu, buf, i);
6054
6055 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
6056
6057 /* revision id */
6058 put_smstate(u32, buf, 0x7efc, 0x00020000);
6059 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
6060}
6061
6062static void process_smi_save_state_64(struct kvm_vcpu *vcpu, char *buf)
6063{
6064#ifdef CONFIG_X86_64
6065 struct desc_ptr dt;
6066 struct kvm_segment seg;
6067 unsigned long val;
6068 int i;
6069
6070 for (i = 0; i < 16; i++)
6071 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
6072
6073 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
6074 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
6075
6076 kvm_get_dr(vcpu, 6, &val);
6077 put_smstate(u64, buf, 0x7f68, val);
6078 kvm_get_dr(vcpu, 7, &val);
6079 put_smstate(u64, buf, 0x7f60, val);
6080
6081 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
6082 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
6083 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
6084
6085 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
6086
6087 /* revision id */
6088 put_smstate(u32, buf, 0x7efc, 0x00020064);
6089
6090 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
6091
6092 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6093 put_smstate(u16, buf, 0x7e90, seg.selector);
6094 put_smstate(u16, buf, 0x7e92, process_smi_get_segment_flags(&seg) >> 8);
6095 put_smstate(u32, buf, 0x7e94, seg.limit);
6096 put_smstate(u64, buf, 0x7e98, seg.base);
6097
6098 kvm_x86_ops->get_idt(vcpu, &dt);
6099 put_smstate(u32, buf, 0x7e84, dt.size);
6100 put_smstate(u64, buf, 0x7e88, dt.address);
6101
6102 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6103 put_smstate(u16, buf, 0x7e70, seg.selector);
6104 put_smstate(u16, buf, 0x7e72, process_smi_get_segment_flags(&seg) >> 8);
6105 put_smstate(u32, buf, 0x7e74, seg.limit);
6106 put_smstate(u64, buf, 0x7e78, seg.base);
6107
6108 kvm_x86_ops->get_gdt(vcpu, &dt);
6109 put_smstate(u32, buf, 0x7e64, dt.size);
6110 put_smstate(u64, buf, 0x7e68, dt.address);
6111
6112 for (i = 0; i < 6; i++)
6113 process_smi_save_seg_64(vcpu, buf, i);
6114#else
6115 WARN_ON_ONCE(1);
6116#endif
6117}
6118
Paolo Bonzini64d60672015-05-07 11:36:11 +02006119static void process_smi(struct kvm_vcpu *vcpu)
6120{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006121 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02006122 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006123 char buf[512];
6124 u32 cr0;
6125
Paolo Bonzini64d60672015-05-07 11:36:11 +02006126 if (is_smm(vcpu)) {
6127 vcpu->arch.smi_pending = true;
6128 return;
6129 }
6130
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006131 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
6132 vcpu->arch.hflags |= HF_SMM_MASK;
6133 memset(buf, 0, 512);
6134 if (guest_cpuid_has_longmode(vcpu))
6135 process_smi_save_state_64(vcpu, buf);
6136 else
6137 process_smi_save_state_32(vcpu, buf);
6138
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006139 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006140
6141 if (kvm_x86_ops->get_nmi_mask(vcpu))
6142 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
6143 else
6144 kvm_x86_ops->set_nmi_mask(vcpu, true);
6145
6146 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6147 kvm_rip_write(vcpu, 0x8000);
6148
6149 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
6150 kvm_x86_ops->set_cr0(vcpu, cr0);
6151 vcpu->arch.cr0 = cr0;
6152
6153 kvm_x86_ops->set_cr4(vcpu, 0);
6154
Paolo Bonzini18c36262015-08-07 12:27:54 +02006155 /* Undocumented: IDT limit is set to zero on entry to SMM. */
6156 dt.address = dt.size = 0;
6157 kvm_x86_ops->set_idt(vcpu, &dt);
6158
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006159 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
6160
6161 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
6162 cs.base = vcpu->arch.smbase;
6163
6164 ds.selector = 0;
6165 ds.base = 0;
6166
6167 cs.limit = ds.limit = 0xffffffff;
6168 cs.type = ds.type = 0x3;
6169 cs.dpl = ds.dpl = 0;
6170 cs.db = ds.db = 0;
6171 cs.s = ds.s = 1;
6172 cs.l = ds.l = 0;
6173 cs.g = ds.g = 1;
6174 cs.avl = ds.avl = 0;
6175 cs.present = ds.present = 1;
6176 cs.unusable = ds.unusable = 0;
6177 cs.padding = ds.padding = 0;
6178
6179 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
6180 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
6181 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
6182 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
6183 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
6184 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
6185
6186 if (guest_cpuid_has_longmode(vcpu))
6187 kvm_x86_ops->set_efer(vcpu, 0);
6188
6189 kvm_update_cpuid(vcpu);
6190 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006191}
6192
Yang Zhang3d81bc72013-04-11 19:25:13 +08006193static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006194{
Yang Zhang3d81bc72013-04-11 19:25:13 +08006195 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
6196 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006197
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02006198 memset(vcpu->arch.eoi_exit_bitmap, 0, 256 / 8);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006199
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07006200 if (irqchip_split(vcpu->kvm))
6201 kvm_scan_ioapic_routes(vcpu, vcpu->arch.eoi_exit_bitmap);
6202 else
6203 kvm_ioapic_scan_entry(vcpu, vcpu->arch.eoi_exit_bitmap);
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02006204 kvm_x86_ops->load_eoi_exitmap(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006205}
6206
Radim Krčmářa70656b2014-09-18 12:38:36 -04006207static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
6208{
6209 ++vcpu->stat.tlb_flush;
6210 kvm_x86_ops->tlb_flush(vcpu);
6211}
6212
Tang Chen4256f432014-09-24 15:57:54 +08006213void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
6214{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006215 struct page *page = NULL;
6216
Paolo Bonzini35754c92015-07-29 12:05:37 +02006217 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02006218 return;
6219
Tang Chen4256f432014-09-24 15:57:54 +08006220 if (!kvm_x86_ops->set_apic_access_page_addr)
6221 return;
6222
Tang Chenc24ae0d2014-09-24 15:57:58 +08006223 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02006224 if (is_error_page(page))
6225 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08006226 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
6227
6228 /*
6229 * Do not pin apic access page in memory, the MMU notifier
6230 * will call us again if it is migrated or swapped out.
6231 */
6232 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08006233}
6234EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
6235
Tang Chenfe71557a2014-09-24 15:57:57 +08006236void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
6237 unsigned long address)
6238{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006239 /*
6240 * The physical address of apic access page is stored in the VMCS.
6241 * Update it when it becomes invalid.
6242 */
6243 if (address == gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT))
6244 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Tang Chenfe71557a2014-09-24 15:57:57 +08006245}
6246
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006247/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01006248 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006249 * exiting to the userspace. Otherwise, the value will be returned to the
6250 * userspace.
6251 */
Avi Kivity851ba692009-08-24 11:10:17 +03006252static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006253{
6254 int r;
Paolo Bonzini35754c92015-07-29 12:05:37 +02006255 bool req_int_win = !lapic_in_kernel(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03006256 vcpu->run->request_interrupt_window;
Jan Kiszka730dca42013-04-28 10:50:52 +02006257 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006258
Avi Kivity3e007502010-06-23 14:26:18 +03006259 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03006260 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05006261 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006262 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03006263 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006264 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
6265 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03006266 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
6267 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10006268 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
6269 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006270 if (unlikely(r))
6271 goto out;
6272 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006273 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03006274 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006275 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Radim Krčmářa70656b2014-09-18 12:38:36 -04006276 kvm_vcpu_flush_tlb(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006277 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006278 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02006279 r = 0;
6280 goto out;
6281 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006282 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006283 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01006284 r = 0;
6285 goto out;
6286 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006287 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02006288 vcpu->fpu_active = 0;
6289 kvm_x86_ops->fpu_deactivate(vcpu);
6290 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006291 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
6292 /* Page is swapped out. Do synthetic halt */
6293 vcpu->arch.apf.halted = true;
6294 r = 1;
6295 goto out;
6296 }
Glauber Costac9aaa892011-07-11 15:28:14 -04006297 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
6298 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006299 if (kvm_check_request(KVM_REQ_SMI, vcpu))
6300 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006301 if (kvm_check_request(KVM_REQ_NMI, vcpu))
6302 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006303 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006304 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006305 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006306 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07006307 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
6308 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
6309 if (test_bit(vcpu->arch.pending_ioapic_eoi,
6310 (void *) vcpu->arch.eoi_exit_bitmap)) {
6311 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
6312 vcpu->run->eoi.vector =
6313 vcpu->arch.pending_ioapic_eoi;
6314 r = 0;
6315 goto out;
6316 }
6317 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08006318 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
6319 vcpu_scan_ioapic(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08006320 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
6321 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03006322 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
6323 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
6324 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
6325 r = 0;
6326 goto out;
6327 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03006328 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
6329 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
6330 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
6331 r = 0;
6332 goto out;
6333 }
Avi Kivity2f52d582008-01-16 12:49:30 +02006334 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006335
Avi Kivityb463a6f2010-07-20 15:06:17 +03006336 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Jan Kiszka66450a22013-03-13 12:42:34 +01006337 kvm_apic_accept_events(vcpu);
6338 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
6339 r = 1;
6340 goto out;
6341 }
6342
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006343 if (inject_pending_event(vcpu, req_int_win) != 0)
6344 req_immediate_exit = true;
Avi Kivity3842d132010-07-27 12:30:24 +03006345 /* enable NMI/IRQ window open exits if needed */
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006346 else if (vcpu->arch.nmi_pending)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006347 kvm_x86_ops->enable_nmi_window(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006348 else if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Jan Kiszkac9a79532014-03-07 20:03:15 +01006349 kvm_x86_ops->enable_irq_window(vcpu);
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006350
Avi Kivity3842d132010-07-27 12:30:24 +03006351 if (kvm_lapic_enabled(vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006352 /*
6353 * Update architecture specific hints for APIC
6354 * virtual interrupt delivery.
6355 */
6356 if (kvm_x86_ops->hwapic_irr_update)
6357 kvm_x86_ops->hwapic_irr_update(vcpu,
6358 kvm_lapic_find_highest_irr(vcpu));
Avi Kivity3842d132010-07-27 12:30:24 +03006359 update_cr8_intercept(vcpu);
6360 kvm_lapic_sync_to_vapic(vcpu);
6361 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006362 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006363
Avi Kivityd8368af2012-05-14 18:07:56 +03006364 r = kvm_mmu_reload(vcpu);
6365 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006366 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03006367 }
6368
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006369 preempt_disable();
6370
6371 kvm_x86_ops->prepare_guest_switch(vcpu);
6372 if (vcpu->fpu_active)
6373 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006374 kvm_load_guest_xcr0(vcpu);
6375
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006376 vcpu->mode = IN_GUEST_MODE;
6377
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006378 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6379
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006380 /* We should set ->mode before check ->requests,
6381 * see the comment in make_all_cpus_request.
6382 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006383 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006384
6385 local_irq_disable();
6386
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006387 if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006388 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006389 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006390 smp_wmb();
6391 local_irq_enable();
6392 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006393 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006394 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006395 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006396 }
6397
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006398 if (req_immediate_exit)
6399 smp_send_reschedule(vcpu->cpu);
6400
Christian Borntraegerccf73aaf2015-04-30 13:43:31 +02006401 __kvm_guest_enter();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006402
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006403 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006404 set_debugreg(0, 7);
6405 set_debugreg(vcpu->arch.eff_db[0], 0);
6406 set_debugreg(vcpu->arch.eff_db[1], 1);
6407 set_debugreg(vcpu->arch.eff_db[2], 2);
6408 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006409 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03006410 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006411 }
6412
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006413 trace_kvm_entry(vcpu->vcpu_id);
Marcelo Tosattid0659d92014-12-16 09:08:15 -05006414 wait_lapic_expire(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006415 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006416
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006417 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006418 * Do this here before restoring debug registers on the host. And
6419 * since we do this before handling the vmexit, a DR access vmexit
6420 * can (a) read the correct value of the debug registers, (b) set
6421 * KVM_DEBUGREG_WONT_EXIT again.
6422 */
6423 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
6424 int i;
6425
6426 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
6427 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
6428 for (i = 0; i < KVM_NR_DB_REGS; i++)
6429 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
6430 }
6431
6432 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006433 * If the guest has used debug registers, at least dr7
6434 * will be disabled while returning to the host.
6435 * If we don't have active breakpoints in the host, we don't
6436 * care about the messed up debug address registers. But if
6437 * we have some of them active, restore the old state.
6438 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01006439 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006440 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006441
Marcelo Tosatti886b4702012-11-27 23:28:58 -02006442 vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu,
Andy Lutomirski4ea16362015-06-25 18:44:07 +02006443 rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10006444
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006445 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03006446 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006447
6448 /* Interrupt is enabled by handle_external_intr() */
6449 kvm_x86_ops->handle_external_intr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006450
6451 ++vcpu->stat.exits;
6452
6453 /*
6454 * We must have an instruction between local_irq_enable() and
6455 * kvm_guest_exit(), so the timer interrupt isn't delayed by
6456 * the interrupt shadow. The stat.exits increment will do nicely.
6457 * But we need to prevent reordering, hence this barrier():
6458 */
6459 barrier();
6460
6461 kvm_guest_exit();
6462
6463 preempt_enable();
6464
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006465 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006466
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006467 /*
6468 * Profile KVM exit RIPs:
6469 */
6470 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006471 unsigned long rip = kvm_rip_read(vcpu);
6472 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006473 }
6474
Zachary Amsdencc578282012-02-03 15:43:50 -02006475 if (unlikely(vcpu->arch.tsc_always_catchup))
6476 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02006477
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03006478 if (vcpu->arch.apic_attention)
6479 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02006480
Avi Kivity851ba692009-08-24 11:10:17 +03006481 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006482 return r;
6483
6484cancel_injection:
6485 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03006486 if (unlikely(vcpu->arch.apic_attention))
6487 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006488out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03006489 return r;
6490}
6491
Paolo Bonzini362c6982015-02-06 12:48:04 +01006492static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
6493{
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01006494 if (!kvm_arch_vcpu_runnable(vcpu)) {
6495 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
6496 kvm_vcpu_block(vcpu);
6497 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
6498 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
6499 return 1;
6500 }
Gleb Natapov09cec752009-03-23 15:11:44 +02006501
Paolo Bonzini362c6982015-02-06 12:48:04 +01006502 kvm_apic_accept_events(vcpu);
6503 switch(vcpu->arch.mp_state) {
6504 case KVM_MP_STATE_HALTED:
6505 vcpu->arch.pv.pv_unhalted = false;
6506 vcpu->arch.mp_state =
6507 KVM_MP_STATE_RUNNABLE;
6508 case KVM_MP_STATE_RUNNABLE:
6509 vcpu->arch.apf.halted = false;
6510 break;
6511 case KVM_MP_STATE_INIT_RECEIVED:
6512 break;
6513 default:
6514 return -EINTR;
6515 break;
6516 }
6517 return 1;
6518}
6519
6520static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03006521{
6522 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006523 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006524
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006525 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006526
Paolo Bonzini362c6982015-02-06 12:48:04 +01006527 for (;;) {
Gleb Natapovaf585b92010-10-14 11:22:46 +02006528 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
6529 !vcpu->arch.apf.halted)
Avi Kivity851ba692009-08-24 11:10:17 +03006530 r = vcpu_enter_guest(vcpu);
Paolo Bonzini362c6982015-02-06 12:48:04 +01006531 else
6532 r = vcpu_block(kvm, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02006533 if (r <= 0)
6534 break;
6535
6536 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
6537 if (kvm_cpu_has_pending_timer(vcpu))
6538 kvm_inject_pending_timer_irqs(vcpu);
6539
Avi Kivity851ba692009-08-24 11:10:17 +03006540 if (dm_request_for_irq_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02006541 r = 0;
6542 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02006543 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006544 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006545 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006546
6547 kvm_check_async_pf_completion(vcpu);
6548
Gleb Natapov09cec752009-03-23 15:11:44 +02006549 if (signal_pending(current)) {
6550 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006551 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006552 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006553 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006554 }
6555 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006556 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09006557 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006558 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006559 }
6560 }
6561
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006562 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006563
6564 return r;
6565}
6566
Gleb Natapov716d51a2012-09-03 15:24:26 +03006567static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
6568{
6569 int r;
6570 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6571 r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
6572 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6573 if (r != EMULATE_DONE)
6574 return 0;
6575 return 1;
6576}
6577
6578static int complete_emulated_pio(struct kvm_vcpu *vcpu)
6579{
6580 BUG_ON(!vcpu->arch.pio.count);
6581
6582 return complete_emulated_io(vcpu);
6583}
6584
Avi Kivityf78146b2012-04-18 19:22:47 +03006585/*
6586 * Implements the following, as a state machine:
6587 *
6588 * read:
6589 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006590 * for each mmio piece in the fragment
6591 * write gpa, len
6592 * exit
6593 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03006594 * execute insn
6595 *
6596 * write:
6597 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006598 * for each mmio piece in the fragment
6599 * write gpa, len
6600 * copy data
6601 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03006602 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03006603static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02006604{
6605 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03006606 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006607 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02006608
Gleb Natapov716d51a2012-09-03 15:24:26 +03006609 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006610
Gleb Natapov716d51a2012-09-03 15:24:26 +03006611 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006612 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
6613 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006614 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006615 memcpy(frag->data, run->mmio.data, len);
6616
6617 if (frag->len <= 8) {
6618 /* Switch to the next fragment. */
6619 frag++;
6620 vcpu->mmio_cur_fragment++;
6621 } else {
6622 /* Go forward to the next mmio piece. */
6623 frag->data += len;
6624 frag->gpa += len;
6625 frag->len -= len;
6626 }
6627
Andrew Honiga08d3b32014-02-27 19:35:14 +01006628 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03006629 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006630
6631 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02006632 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03006633 return 1;
6634 vcpu->mmio_read_completed = 1;
6635 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02006636 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006637
Gleb Natapov716d51a2012-09-03 15:24:26 +03006638 run->exit_reason = KVM_EXIT_MMIO;
6639 run->mmio.phys_addr = frag->gpa;
6640 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006641 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
6642 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006643 run->mmio.is_write = vcpu->mmio_is_write;
6644 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
6645 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02006646}
6647
Gleb Natapov716d51a2012-09-03 15:24:26 +03006648
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006649int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6650{
Ingo Molnarc5bedc62015-04-23 12:49:20 +02006651 struct fpu *fpu = &current->thread.fpu;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006652 int r;
6653 sigset_t sigsaved;
6654
Ingo Molnarc4d72e22015-04-27 07:18:17 +02006655 fpu__activate_curr(fpu);
Avi Kivitye5c30142011-01-11 12:15:54 +02006656
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006657 if (vcpu->sigset_active)
6658 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
6659
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006660 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
6661 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006662 kvm_apic_accept_events(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006663 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006664 r = -EAGAIN;
6665 goto out;
6666 }
6667
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006668 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02006669 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006670 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
6671 r = -EINVAL;
6672 goto out;
6673 }
6674 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006675
Gleb Natapov716d51a2012-09-03 15:24:26 +03006676 if (unlikely(vcpu->arch.complete_userspace_io)) {
6677 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
6678 vcpu->arch.complete_userspace_io = NULL;
6679 r = cui(vcpu);
6680 if (r <= 0)
6681 goto out;
6682 } else
6683 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006684
Paolo Bonzini362c6982015-02-06 12:48:04 +01006685 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006686
6687out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03006688 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006689 if (vcpu->sigset_active)
6690 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
6691
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006692 return r;
6693}
6694
6695int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6696{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006697 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
6698 /*
6699 * We are here if userspace calls get_regs() in the middle of
6700 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08006701 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006702 * that usually, but some bad designed PV devices (vmware
6703 * backdoor interface) need this to work
6704 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03006705 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006706 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6707 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006708 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
6709 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
6710 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
6711 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
6712 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
6713 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
6714 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6715 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006716#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006717 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
6718 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
6719 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
6720 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
6721 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
6722 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
6723 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
6724 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006725#endif
6726
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006727 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02006728 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006729
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006730 return 0;
6731}
6732
6733int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6734{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006735 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
6736 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6737
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006738 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
6739 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
6740 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
6741 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
6742 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
6743 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
6744 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
6745 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006746#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006747 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
6748 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
6749 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
6750 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
6751 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
6752 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
6753 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
6754 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006755#endif
6756
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006757 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02006758 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006759
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02006760 vcpu->arch.exception.pending = false;
6761
Avi Kivity3842d132010-07-27 12:30:24 +03006762 kvm_make_request(KVM_REQ_EVENT, vcpu);
6763
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006764 return 0;
6765}
6766
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006767void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
6768{
6769 struct kvm_segment cs;
6770
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006771 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006772 *db = cs.db;
6773 *l = cs.l;
6774}
6775EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
6776
6777int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
6778 struct kvm_sregs *sregs)
6779{
Gleb Natapov89a27f42010-02-16 10:51:48 +02006780 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006781
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006782 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6783 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6784 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6785 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6786 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6787 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006788
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006789 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6790 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006791
6792 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006793 sregs->idt.limit = dt.size;
6794 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006795 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006796 sregs->gdt.limit = dt.size;
6797 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006798
Avi Kivity4d4ec082009-12-29 18:07:30 +02006799 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006800 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006801 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02006802 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006803 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02006804 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006805 sregs->apic_base = kvm_get_apic_base(vcpu);
6806
Gleb Natapov923c61b2009-05-11 13:35:48 +03006807 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006808
Gleb Natapov36752c92009-05-11 13:35:53 +03006809 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03006810 set_bit(vcpu->arch.interrupt.nr,
6811 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03006812
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006813 return 0;
6814}
6815
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006816int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
6817 struct kvm_mp_state *mp_state)
6818{
Jan Kiszka66450a22013-03-13 12:42:34 +01006819 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306820 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
6821 vcpu->arch.pv.pv_unhalted)
6822 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
6823 else
6824 mp_state->mp_state = vcpu->arch.mp_state;
6825
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006826 return 0;
6827}
6828
6829int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
6830 struct kvm_mp_state *mp_state)
6831{
Jan Kiszka66450a22013-03-13 12:42:34 +01006832 if (!kvm_vcpu_has_lapic(vcpu) &&
6833 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
6834 return -EINVAL;
6835
6836 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
6837 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
6838 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
6839 } else
6840 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03006841 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03006842 return 0;
6843}
6844
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006845int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
6846 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006847{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006848 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006849 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006850
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006851 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006852
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006853 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006854 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02006855
Gleb Natapovc6975182010-02-18 12:15:01 +02006856 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03006857 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02006858
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006859 kvm_rip_write(vcpu, ctxt->eip);
6860 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03006861 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03006862 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02006863}
6864EXPORT_SYMBOL_GPL(kvm_task_switch);
6865
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006866int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
6867 struct kvm_sregs *sregs)
6868{
Jan Kiszka58cb6282014-01-24 16:48:44 +01006869 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006870 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006871 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02006872 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006873
Petr Matousek6d1068b2012-11-06 19:24:07 +01006874 if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
6875 return -EINVAL;
6876
Gleb Natapov89a27f42010-02-16 10:51:48 +02006877 dt.size = sregs->idt.limit;
6878 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006879 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02006880 dt.size = sregs->gdt.limit;
6881 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006882 kvm_x86_ops->set_gdt(vcpu, &dt);
6883
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006884 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02006885 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02006886 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02006887 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006888
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006889 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006890
Avi Kivityf6801df2010-01-21 15:31:50 +02006891 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006892 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006893 apic_base_msr.data = sregs->apic_base;
6894 apic_base_msr.host_initiated = true;
6895 kvm_set_apic_base(vcpu, &apic_base_msr);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006896
Avi Kivity4d4ec082009-12-29 18:07:30 +02006897 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006898 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00006899 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006900
Avi Kivityfc78f512009-12-07 12:16:48 +02006901 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006902 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Sheng Yang3ea3aa82010-12-08 10:49:43 +08006903 if (sregs->cr4 & X86_CR4_OSXSAVE)
Avi Kivity00b27a32011-11-23 16:30:32 +02006904 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006905
6906 idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02006907 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02006908 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02006909 mmu_reset_needed = 1;
6910 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08006911 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006912
6913 if (mmu_reset_needed)
6914 kvm_mmu_reset_context(vcpu);
6915
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03006916 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03006917 pending_vec = find_first_bit(
6918 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
6919 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006920 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03006921 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006922 }
6923
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006924 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
6925 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
6926 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
6927 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
6928 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
6929 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006930
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02006931 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
6932 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006933
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03006934 update_cr8_intercept(vcpu);
6935
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006936 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03006937 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006938 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02006939 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03006940 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
6941
Avi Kivity3842d132010-07-27 12:30:24 +03006942 kvm_make_request(KVM_REQ_EVENT, vcpu);
6943
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006944 return 0;
6945}
6946
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006947int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
6948 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006949{
Jan Kiszka355be0b2009-10-03 00:31:21 +02006950 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006951 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006952
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006953 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
6954 r = -EBUSY;
6955 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03006956 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006957 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
6958 kvm_queue_exception(vcpu, DB_VECTOR);
6959 else
6960 kvm_queue_exception(vcpu, BP_VECTOR);
6961 }
6962
Jan Kiszka91586a32009-10-05 13:07:21 +02006963 /*
6964 * Read rflags as long as potentially injected trace flags are still
6965 * filtered out.
6966 */
6967 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02006968
6969 vcpu->guest_debug = dbg->control;
6970 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
6971 vcpu->guest_debug = 0;
6972
6973 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006974 for (i = 0; i < KVM_NR_DB_REGS; ++i)
6975 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02006976 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006977 } else {
6978 for (i = 0; i < KVM_NR_DB_REGS; i++)
6979 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006980 }
Jan Kiszkac8639012012-09-21 05:42:55 +02006981 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01006982
Jan Kiszkaf92653e2010-02-23 17:47:55 +01006983 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6984 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
6985 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006986
Jan Kiszka91586a32009-10-05 13:07:21 +02006987 /*
6988 * Trigger an rflags update that will inject or remove the trace
6989 * flags.
6990 */
6991 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006992
Jan Kiszkac8639012012-09-21 05:42:55 +02006993 kvm_x86_ops->update_db_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02006994
Jan Kiszka4f926bf22009-10-30 12:46:59 +01006995 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006996
Avi Kivity2122ff52010-05-13 11:25:04 +03006997out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006998
6999 return r;
7000}
7001
7002/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08007003 * Translate a guest virtual address to a guest physical address.
7004 */
7005int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
7006 struct kvm_translation *tr)
7007{
7008 unsigned long vaddr = tr->linear_address;
7009 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007010 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007011
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007012 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02007013 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007014 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08007015 tr->physical_address = gpa;
7016 tr->valid = gpa != UNMAPPED_GVA;
7017 tr->writeable = 1;
7018 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007019
7020 return 0;
7021}
7022
Hollis Blanchardd0752062007-10-31 17:24:25 -05007023int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7024{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007025 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007026 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007027
Hollis Blanchardd0752062007-10-31 17:24:25 -05007028 memcpy(fpu->fpr, fxsave->st_space, 128);
7029 fpu->fcw = fxsave->cwd;
7030 fpu->fsw = fxsave->swd;
7031 fpu->ftwx = fxsave->twd;
7032 fpu->last_opcode = fxsave->fop;
7033 fpu->last_ip = fxsave->rip;
7034 fpu->last_dp = fxsave->rdp;
7035 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
7036
Hollis Blanchardd0752062007-10-31 17:24:25 -05007037 return 0;
7038}
7039
7040int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7041{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007042 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007043 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007044
Hollis Blanchardd0752062007-10-31 17:24:25 -05007045 memcpy(fxsave->st_space, fpu->fpr, 128);
7046 fxsave->cwd = fpu->fcw;
7047 fxsave->swd = fpu->fsw;
7048 fxsave->twd = fpu->ftwx;
7049 fxsave->fop = fpu->last_opcode;
7050 fxsave->rip = fpu->last_ip;
7051 fxsave->rdp = fpu->last_dp;
7052 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
7053
Hollis Blanchardd0752062007-10-31 17:24:25 -05007054 return 0;
7055}
7056
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007057static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007058{
Ingo Molnarbf935b02015-04-30 10:23:42 +02007059 fpstate_init(&vcpu->arch.guest_fpu.state);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007060 if (cpu_has_xsaves)
Ingo Molnar7366ed72015-04-27 04:19:39 +02007061 vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007062 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007063
Dexuan Cui2acf9232010-06-10 11:27:12 +08007064 /*
7065 * Ensure guest xcr0 is valid for loading
7066 */
7067 vcpu->arch.xcr0 = XSTATE_FP;
7068
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007069 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007070}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007071
7072void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
7073{
Avi Kivity2608d7a2010-01-21 15:31:45 +02007074 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007075 return;
7076
Dexuan Cui2acf9232010-06-10 11:27:12 +08007077 /*
7078 * Restore all possible states in the guest,
7079 * and assume host would use all available bits.
7080 * Guest xcr0 would be loaded later.
7081 */
7082 kvm_put_guest_xcr0(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007083 vcpu->guest_fpu_loaded = 1;
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007084 __kernel_fpu_begin();
Ingo Molnar003e2e82015-05-25 11:59:35 +02007085 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state);
Avi Kivity0c048512010-01-21 15:31:52 +02007086 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007087}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007088
7089void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
7090{
Dexuan Cui2acf9232010-06-10 11:27:12 +08007091 kvm_put_guest_xcr0(vcpu);
7092
Rik van Riel653f52c2015-04-23 11:52:37 -04007093 if (!vcpu->guest_fpu_loaded) {
7094 vcpu->fpu_counter = 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007095 return;
Rik van Riel653f52c2015-04-23 11:52:37 -04007096 }
Hollis Blanchardd0752062007-10-31 17:24:25 -05007097
7098 vcpu->guest_fpu_loaded = 0;
Ingo Molnar4f836342015-04-27 02:53:16 +02007099 copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007100 __kernel_fpu_end();
Avi Kivityf096ed82007-11-18 13:54:33 +02007101 ++vcpu->stat.fpu_reload;
Rik van Riel653f52c2015-04-23 11:52:37 -04007102 /*
7103 * If using eager FPU mode, or if the guest is a frequent user
7104 * of the FPU, just leave the FPU active for next time.
7105 * Every 255 times fpu_counter rolls over to 0; a guest that uses
7106 * the FPU in bursts will revert to loading it on demand.
7107 */
Paolo Bonzinia9b4fb72015-05-20 11:46:12 +02007108 if (!vcpu->arch.eager_fpu) {
Rik van Riel653f52c2015-04-23 11:52:37 -04007109 if (++vcpu->fpu_counter < 5)
7110 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
7111 }
Avi Kivity0c048512010-01-21 15:31:52 +02007112 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007113}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007114
7115void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
7116{
Glauber Costa12f9a482011-02-01 14:16:40 -05007117 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01007118
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007119 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007120 kvm_x86_ops->vcpu_free(vcpu);
7121}
7122
7123struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
7124 unsigned int id)
7125{
Liang Lic447e762015-05-21 04:41:25 +08007126 struct kvm_vcpu *vcpu;
7127
Zachary Amsden6755bae2010-08-19 22:07:22 -10007128 if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
7129 printk_once(KERN_WARNING
7130 "kvm: SMP vm created on host with unstable TSC; "
7131 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08007132
7133 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
7134
Liang Lic447e762015-05-21 04:41:25 +08007135 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02007136}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007137
Avi Kivity26e52152007-11-20 15:30:24 +02007138int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
7139{
7140 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007141
Xiao Guangrong19efffa2015-06-15 16:55:31 +08007142 kvm_vcpu_mtrr_init(vcpu);
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007143 r = vcpu_load(vcpu);
7144 if (r)
7145 return r;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007146 kvm_vcpu_reset(vcpu, false);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007147 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007148 vcpu_put(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02007149 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007150}
7151
Dominik Dingel31928aa2014-12-04 15:47:07 +01007152void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007153{
Will Auld8fe8ab42012-11-29 12:42:12 -08007154 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01007155 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007156
Dominik Dingel31928aa2014-12-04 15:47:07 +01007157 if (vcpu_load(vcpu))
7158 return;
Will Auld8fe8ab42012-11-29 12:42:12 -08007159 msr.data = 0x0;
7160 msr.index = MSR_IA32_TSC;
7161 msr.host_initiated = true;
7162 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007163 vcpu_put(vcpu);
7164
Marcelo Tosatti630994b2015-05-12 22:42:04 -03007165 if (!kvmclock_periodic_sync)
7166 return;
7167
Andrew Jones332967a2014-02-28 12:52:55 +01007168 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
7169 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007170}
7171
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06007172void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007173{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007174 int r;
Gleb Natapov344d9582010-10-14 11:22:50 +02007175 vcpu->arch.apf.msr_val = 0;
7176
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007177 r = vcpu_load(vcpu);
7178 BUG_ON(r);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007179 kvm_mmu_unload(vcpu);
7180 vcpu_put(vcpu);
7181
7182 kvm_x86_ops->vcpu_free(vcpu);
7183}
7184
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007185void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007186{
Paolo Bonzinie69fab52015-06-04 10:44:44 +02007187 vcpu->arch.hflags = 0;
7188
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007189 atomic_set(&vcpu->arch.nmi_queued, 0);
7190 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007191 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03007192 kvm_clear_interrupt_queue(vcpu);
7193 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007194
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007195 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03007196 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03007197 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007198 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007199 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02007200 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007201
Nadav Amit11190222015-04-02 03:10:38 +03007202 vcpu->arch.cr2 = 0;
7203
Avi Kivity3842d132010-07-27 12:30:24 +03007204 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02007205 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04007206 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03007207
Glauber Costa12f9a482011-02-01 14:16:40 -05007208 kvmclock_reset(vcpu);
7209
Gleb Natapovaf585b92010-10-14 11:22:46 +02007210 kvm_clear_async_pf_completion_queue(vcpu);
7211 kvm_async_pf_hash_reset(vcpu);
7212 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007213
Paolo Bonzini64d60672015-05-07 11:36:11 +02007214 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007215 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007216 vcpu->arch.smbase = 0x30000;
7217 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02007218
Julian Stecklina66f7b722012-12-05 15:26:19 +01007219 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
7220 vcpu->arch.regs_avail = ~0;
7221 vcpu->arch.regs_dirty = ~0;
7222
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007223 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007224}
7225
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01007226void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01007227{
7228 struct kvm_segment cs;
7229
7230 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
7231 cs.selector = vector << 8;
7232 cs.base = vector << 12;
7233 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7234 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007235}
7236
Radim Krčmář13a34e02014-08-28 15:13:03 +02007237int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007238{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10007239 struct kvm *kvm;
7240 struct kvm_vcpu *vcpu;
7241 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007242 int ret;
7243 u64 local_tsc;
7244 u64 max_tsc = 0;
7245 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03007246
7247 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02007248 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007249 if (ret != 0)
7250 return ret;
7251
Andy Lutomirski4ea16362015-06-25 18:44:07 +02007252 local_tsc = rdtsc();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007253 stable = !check_tsc_unstable();
7254 list_for_each_entry(kvm, &vm_list, vm_list) {
7255 kvm_for_each_vcpu(i, vcpu, kvm) {
7256 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007257 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007258 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
7259 backwards_tsc = true;
7260 if (vcpu->arch.last_host_tsc > max_tsc)
7261 max_tsc = vcpu->arch.last_host_tsc;
7262 }
7263 }
7264 }
7265
7266 /*
7267 * Sometimes, even reliable TSCs go backwards. This happens on
7268 * platforms that reset TSC during suspend or hibernate actions, but
7269 * maintain synchronization. We must compensate. Fortunately, we can
7270 * detect that condition here, which happens early in CPU bringup,
7271 * before any KVM threads can be running. Unfortunately, we can't
7272 * bring the TSCs fully up to date with real time, as we aren't yet far
7273 * enough into CPU bringup that we know how much real time has actually
7274 * elapsed; our helper function, get_kernel_ns() will be using boot
7275 * variables that haven't been updated yet.
7276 *
7277 * So we simply find the maximum observed TSC above, then record the
7278 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
7279 * the adjustment will be applied. Note that we accumulate
7280 * adjustments, in case multiple suspend cycles happen before some VCPU
7281 * gets a chance to run again. In the event that no KVM threads get a
7282 * chance to run, we will miss the entire elapsed period, as we'll have
7283 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
7284 * loose cycle time. This isn't too big a deal, since the loss will be
7285 * uniform across all VCPUs (not to mention the scenario is extremely
7286 * unlikely). It is possible that a second hibernate recovery happens
7287 * much faster than a first, causing the observed TSC here to be
7288 * smaller; this would require additional padding adjustment, which is
7289 * why we set last_host_tsc to the local tsc observed here.
7290 *
7291 * N.B. - this code below runs only on platforms with reliable TSC,
7292 * as that is the only way backwards_tsc is set above. Also note
7293 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
7294 * have the same delta_cyc adjustment applied if backwards_tsc
7295 * is detected. Note further, this adjustment is only done once,
7296 * as we reset last_host_tsc on all VCPUs to stop this from being
7297 * called multiple times (one for each physical CPU bringup).
7298 *
Guo Chao4a969982012-06-28 15:17:27 +08007299 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007300 * will be compensated by the logic in vcpu_load, which sets the TSC to
7301 * catchup mode. This will catchup all VCPUs to real time, but cannot
7302 * guarantee that they stay in perfect synchronization.
7303 */
7304 if (backwards_tsc) {
7305 u64 delta_cyc = max_tsc - local_tsc;
Marcelo Tosatti16a96022014-05-14 12:43:24 -03007306 backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007307 list_for_each_entry(kvm, &vm_list, vm_list) {
7308 kvm_for_each_vcpu(i, vcpu, kvm) {
7309 vcpu->arch.tsc_offset_adjustment += delta_cyc;
7310 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007311 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007312 }
7313
7314 /*
7315 * We have to disable TSC offset matching.. if you were
7316 * booting a VM while issuing an S4 host suspend....
7317 * you may have some problem. Solving this issue is
7318 * left as an exercise to the reader.
7319 */
7320 kvm->arch.last_tsc_nsec = 0;
7321 kvm->arch.last_tsc_write = 0;
7322 }
7323
7324 }
7325 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007326}
7327
Radim Krčmář13a34e02014-08-28 15:13:03 +02007328void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007329{
Radim Krčmář13a34e02014-08-28 15:13:03 +02007330 kvm_x86_ops->hardware_disable();
7331 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007332}
7333
7334int kvm_arch_hardware_setup(void)
7335{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03007336 int r;
7337
7338 r = kvm_x86_ops->hardware_setup();
7339 if (r != 0)
7340 return r;
7341
7342 kvm_init_msr_list();
7343 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007344}
7345
7346void kvm_arch_hardware_unsetup(void)
7347{
7348 kvm_x86_ops->hardware_unsetup();
7349}
7350
7351void kvm_arch_check_processor_compat(void *rtn)
7352{
7353 kvm_x86_ops->check_processor_compatibility(rtn);
7354}
7355
Paolo Bonzinid71ba782015-07-29 11:56:48 +02007356bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
7357{
7358 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
7359}
7360EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
7361
7362bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
7363{
7364 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
7365}
7366
Avi Kivity3e515702012-03-05 14:23:29 +02007367bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
7368{
Paolo Bonzini35754c92015-07-29 12:05:37 +02007369 return irqchip_in_kernel(vcpu->kvm) == lapic_in_kernel(vcpu);
Avi Kivity3e515702012-03-05 14:23:29 +02007370}
7371
Gleb Natapov54e98182012-08-05 15:58:32 +03007372struct static_key kvm_no_apic_vcpu __read_mostly;
7373
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007374int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
7375{
7376 struct page *page;
7377 struct kvm *kvm;
7378 int r;
7379
7380 BUG_ON(vcpu->kvm == NULL);
7381 kvm = vcpu->kvm;
7382
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307383 vcpu->arch.pv.pv_unhalted = false;
Avi Kivity9aabc88f2010-07-29 15:11:50 +03007384 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
Nadav Amit58d269d2015-04-02 03:10:36 +03007385 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03007386 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007387 else
Avi Kivitya4535292008-04-13 17:54:35 +03007388 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007389
7390 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
7391 if (!page) {
7392 r = -ENOMEM;
7393 goto fail;
7394 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007395 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007396
Zachary Amsdencc578282012-02-03 15:43:50 -02007397 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007398
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007399 r = kvm_mmu_create(vcpu);
7400 if (r < 0)
7401 goto fail_free_pio_data;
7402
7403 if (irqchip_in_kernel(kvm)) {
7404 r = kvm_create_lapic(vcpu);
7405 if (r < 0)
7406 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03007407 } else
7408 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007409
Huang Ying890ca9a2009-05-11 16:48:15 +08007410 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
7411 GFP_KERNEL);
7412 if (!vcpu->arch.mce_banks) {
7413 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08007414 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08007415 }
7416 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
7417
Wei Yongjunf1797352013-04-18 07:41:00 +08007418 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
7419 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007420 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08007421 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007422
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007423 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01007424
Will Auldba904632012-11-29 12:42:50 -08007425 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
Andy Honig0b794592013-02-20 14:48:10 -08007426 vcpu->arch.pv_time_enabled = false;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007427
7428 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +02007429 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007430
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03007431 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
7432
Radim Krčmář74545702015-04-27 15:11:25 +02007433 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
7434
Gleb Natapovaf585b92010-10-14 11:22:46 +02007435 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007436 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007437
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02007438 vcpu->arch.pending_external_vector = -1;
7439
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007440 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007441
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007442fail_free_mce_banks:
7443 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08007444fail_free_lapic:
7445 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007446fail_mmu_destroy:
7447 kvm_mmu_destroy(vcpu);
7448fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007449 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007450fail:
7451 return r;
7452}
7453
7454void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
7455{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007456 int idx;
7457
Gleb Natapovf5132b02011-11-10 14:57:22 +02007458 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08007459 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007460 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007461 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007462 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007463 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007464 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007465 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03007466 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007467}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007468
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007469void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
7470{
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007471 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007472}
7473
Carsten Ottee08b9632012-01-04 10:25:20 +01007474int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007475{
Carsten Ottee08b9632012-01-04 10:25:20 +01007476 if (type)
7477 return -EINVAL;
7478
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01007479 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08007480 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08007481 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007482 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007483 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007484
Sheng Yang5550af42008-10-15 20:15:06 +08007485 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
7486 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06007487 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
7488 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
7489 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08007490
Jan Kiszka038f8c12011-02-04 10:49:11 +01007491 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007492 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007493 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
7494
7495 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01007496
Andrew Jones7e44e442014-02-28 12:52:54 +01007497 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01007498 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01007499
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01007500 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007501}
7502
7503static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
7504{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007505 int r;
7506 r = vcpu_load(vcpu);
7507 BUG_ON(r);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007508 kvm_mmu_unload(vcpu);
7509 vcpu_put(vcpu);
7510}
7511
7512static void kvm_free_vcpus(struct kvm *kvm)
7513{
7514 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007515 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007516
7517 /*
7518 * Unpin any mmu pages first.
7519 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02007520 kvm_for_each_vcpu(i, vcpu, kvm) {
7521 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007522 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007523 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007524 kvm_for_each_vcpu(i, vcpu, kvm)
7525 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007526
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007527 mutex_lock(&kvm->lock);
7528 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
7529 kvm->vcpus[i] = NULL;
7530
7531 atomic_set(&kvm->online_vcpus, 0);
7532 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007533}
7534
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007535void kvm_arch_sync_events(struct kvm *kvm)
7536{
Andrew Jones332967a2014-02-28 12:52:55 +01007537 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01007538 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Sheng Yangba4cef32009-01-06 10:03:03 +08007539 kvm_free_all_assigned_devices(kvm);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08007540 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007541}
7542
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007543int __x86_set_memory_region(struct kvm *kvm,
7544 const struct kvm_userspace_memory_region *mem)
7545{
7546 int i, r;
7547
7548 /* Called with kvm->slots_lock held. */
7549 BUG_ON(mem->slot >= KVM_MEM_SLOTS_NUM);
7550
7551 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
7552 struct kvm_userspace_memory_region m = *mem;
7553
7554 m.slot |= i << 16;
7555 r = __kvm_set_memory_region(kvm, &m);
7556 if (r < 0)
7557 return r;
7558 }
7559
7560 return 0;
7561}
7562EXPORT_SYMBOL_GPL(__x86_set_memory_region);
7563
7564int x86_set_memory_region(struct kvm *kvm,
7565 const struct kvm_userspace_memory_region *mem)
7566{
7567 int r;
7568
7569 mutex_lock(&kvm->slots_lock);
7570 r = __x86_set_memory_region(kvm, mem);
7571 mutex_unlock(&kvm->slots_lock);
7572
7573 return r;
7574}
7575EXPORT_SYMBOL_GPL(x86_set_memory_region);
7576
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007577void kvm_arch_destroy_vm(struct kvm *kvm)
7578{
Andrew Honig27469d22013-04-18 09:38:14 -07007579 if (current->mm == kvm->mm) {
7580 /*
7581 * Free memory regions allocated on behalf of userspace,
7582 * unless the the memory map has changed due to process exit
7583 * or fd copying.
7584 */
7585 struct kvm_userspace_memory_region mem;
7586 memset(&mem, 0, sizeof(mem));
7587 mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007588 x86_set_memory_region(kvm, &mem);
Andrew Honig27469d22013-04-18 09:38:14 -07007589
7590 mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007591 x86_set_memory_region(kvm, &mem);
Andrew Honig27469d22013-04-18 09:38:14 -07007592
7593 mem.slot = TSS_PRIVATE_MEMSLOT;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007594 x86_set_memory_region(kvm, &mem);
Andrew Honig27469d22013-04-18 09:38:14 -07007595 }
Sheng Yang6eb55812008-10-31 12:37:41 +08007596 kvm_iommu_unmap_guest(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08007597 kfree(kvm->arch.vpic);
7598 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007599 kvm_free_vcpus(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007600 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007601}
Zhang Xiantao0de10342007-11-20 16:25:04 +08007602
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307603void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007604 struct kvm_memory_slot *dont)
7605{
7606 int i;
7607
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007608 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7609 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007610 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007611 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007612 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007613 if (i == 0)
7614 continue;
7615
7616 if (!dont || free->arch.lpage_info[i - 1] !=
7617 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007618 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007619 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007620 }
7621 }
7622}
7623
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307624int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7625 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007626{
7627 int i;
7628
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007629 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007630 unsigned long ugfn;
7631 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007632 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007633
7634 lpages = gfn_to_index(slot->base_gfn + npages - 1,
7635 slot->base_gfn, level) + 1;
7636
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007637 slot->arch.rmap[i] =
7638 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
7639 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007640 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007641 if (i == 0)
7642 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007643
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007644 slot->arch.lpage_info[i - 1] = kvm_kvzalloc(lpages *
7645 sizeof(*slot->arch.lpage_info[i - 1]));
7646 if (!slot->arch.lpage_info[i - 1])
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007647 goto out_free;
7648
7649 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007650 slot->arch.lpage_info[i - 1][0].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007651 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007652 slot->arch.lpage_info[i - 1][lpages - 1].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007653 ugfn = slot->userspace_addr >> PAGE_SHIFT;
7654 /*
7655 * If the gfn and userspace address are not aligned wrt each
7656 * other, or if explicitly asked to, disable large page
7657 * support for this slot
7658 */
7659 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
7660 !kvm_largepages_enabled()) {
7661 unsigned long j;
7662
7663 for (j = 0; j < lpages; ++j)
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007664 slot->arch.lpage_info[i - 1][j].write_count = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007665 }
7666 }
7667
7668 return 0;
7669
7670out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007671 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01007672 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007673 slot->arch.rmap[i] = NULL;
7674 if (i == 0)
7675 continue;
7676
Thomas Huth548ef282015-02-24 21:29:25 +01007677 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007678 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007679 }
7680 return -ENOMEM;
7681}
7682
Paolo Bonzini15f46012015-05-17 21:26:08 +02007683void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007684{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09007685 /*
7686 * memslots->generation has been incremented.
7687 * mmio generation may have reached its maximum value.
7688 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007689 kvm_mmu_invalidate_mmio_sptes(kvm, slots);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09007690}
7691
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007692int kvm_arch_prepare_memory_region(struct kvm *kvm,
7693 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02007694 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007695 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08007696{
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007697 /*
7698 * Only private memory slots need to be mapped here since
7699 * KVM_SET_MEMORY_REGION ioctl is no longer supported.
Zhang Xiantao0de10342007-11-20 16:25:04 +08007700 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007701 if ((memslot->id >= KVM_USER_MEM_SLOTS) && (change == KVM_MR_CREATE)) {
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007702 unsigned long userspace_addr;
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007703
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007704 /*
7705 * MAP_SHARED to prevent internal slot pages from being moved
7706 * by fork()/COW.
7707 */
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09007708 userspace_addr = vm_mmap(NULL, 0, memslot->npages * PAGE_SIZE,
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007709 PROT_READ | PROT_WRITE,
7710 MAP_SHARED | MAP_ANONYMOUS, 0);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007711
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007712 if (IS_ERR((void *)userspace_addr))
7713 return PTR_ERR((void *)userspace_addr);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02007714
Takuya Yoshikawa7a905b12013-02-07 18:55:57 +09007715 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08007716 }
7717
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007718 return 0;
7719}
7720
Kai Huang88178fd2015-01-28 10:54:27 +08007721static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
7722 struct kvm_memory_slot *new)
7723{
7724 /* Still write protect RO slot */
7725 if (new->flags & KVM_MEM_READONLY) {
7726 kvm_mmu_slot_remove_write_access(kvm, new);
7727 return;
7728 }
7729
7730 /*
7731 * Call kvm_x86_ops dirty logging hooks when they are valid.
7732 *
7733 * kvm_x86_ops->slot_disable_log_dirty is called when:
7734 *
7735 * - KVM_MR_CREATE with dirty logging is disabled
7736 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
7737 *
7738 * The reason is, in case of PML, we need to set D-bit for any slots
7739 * with dirty logging disabled in order to eliminate unnecessary GPA
7740 * logging in PML buffer (and potential PML buffer full VMEXT). This
7741 * guarantees leaving PML enabled during guest's lifetime won't have
7742 * any additonal overhead from PML when guest is running with dirty
7743 * logging disabled for memory slots.
7744 *
7745 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
7746 * to dirty logging mode.
7747 *
7748 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
7749 *
7750 * In case of write protect:
7751 *
7752 * Write protect all pages for dirty logging.
7753 *
7754 * All the sptes including the large sptes which point to this
7755 * slot are set to readonly. We can not create any new large
7756 * spte on this slot until the end of the logging.
7757 *
7758 * See the comments in fast_page_fault().
7759 */
7760 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
7761 if (kvm_x86_ops->slot_enable_log_dirty)
7762 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
7763 else
7764 kvm_mmu_slot_remove_write_access(kvm, new);
7765 } else {
7766 if (kvm_x86_ops->slot_disable_log_dirty)
7767 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
7768 }
7769}
7770
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007771void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02007772 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007773 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007774 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007775 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007776{
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007777 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007778
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007779 if (change == KVM_MR_DELETE && old->id >= KVM_USER_MEM_SLOTS) {
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007780 int ret;
7781
Takuya Yoshikawa84826442013-02-27 19:45:25 +09007782 ret = vm_munmap(old->userspace_addr,
7783 old->npages * PAGE_SIZE);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02007784 if (ret < 0)
7785 printk(KERN_WARNING
7786 "kvm_vm_ioctl_set_memory_region: "
7787 "failed to munmap memory\n");
7788 }
7789
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007790 if (!kvm->arch.n_requested_mmu_pages)
7791 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007792
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08007793 if (nr_mmu_pages)
7794 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Kai Huang1c91cad42015-01-28 10:54:26 +08007795
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007796 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08007797 * Dirty logging tracks sptes in 4k granularity, meaning that large
7798 * sptes have to be split. If live migration is successful, the guest
7799 * in the source machine will be destroyed and large sptes will be
7800 * created in the destination. However, if the guest continues to run
7801 * in the source machine (for example if live migration fails), small
7802 * sptes will remain around and cause bad performance.
7803 *
7804 * Scan sptes if dirty logging has been stopped, dropping those
7805 * which can be collapsed into a single large-page spte. Later
7806 * page faults will create the large-page sptes.
7807 */
7808 if ((change != KVM_MR_DELETE) &&
7809 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
7810 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
7811 kvm_mmu_zap_collapsible_sptes(kvm, new);
7812
7813 /*
Kai Huang88178fd2015-01-28 10:54:27 +08007814 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08007815 *
Kai Huang88178fd2015-01-28 10:54:27 +08007816 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
7817 * been zapped so no dirty logging staff is needed for old slot. For
7818 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
7819 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007820 *
7821 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09007822 */
Kai Huang88178fd2015-01-28 10:54:27 +08007823 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02007824 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08007825}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007826
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007827void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007828{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08007829 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03007830}
7831
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007832void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
7833 struct kvm_memory_slot *slot)
7834{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08007835 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03007836}
7837
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007838int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
7839{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007840 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
7841 kvm_x86_ops->check_nested_events(vcpu, false);
7842
Gleb Natapovaf585b92010-10-14 11:22:46 +02007843 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
7844 !vcpu->arch.apf.halted)
7845 || !list_empty_careful(&vcpu->async_pf.done)
Jan Kiszka66450a22013-03-13 12:42:34 +01007846 || kvm_apic_has_events(vcpu)
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307847 || vcpu->arch.pv.pv_unhalted
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007848 || atomic_read(&vcpu->arch.nmi_queued) ||
Gleb Natapova1b37102009-07-09 15:33:52 +03007849 (kvm_arch_interrupt_allowed(vcpu) &&
7850 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08007851}
Zhang Xiantao57361992007-12-17 14:21:40 +08007852
Christoffer Dallb6d33832012-03-08 16:44:24 -05007853int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08007854{
Christoffer Dallb6d33832012-03-08 16:44:24 -05007855 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08007856}
Gleb Natapov78646122009-03-23 12:12:11 +02007857
7858int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
7859{
7860 return kvm_x86_ops->interrupt_allowed(vcpu);
7861}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007862
Nadav Amit82b32772014-11-02 11:54:45 +02007863unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
7864{
7865 if (is_64_bit_mode(vcpu))
7866 return kvm_rip_read(vcpu);
7867 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
7868 kvm_rip_read(vcpu));
7869}
7870EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
7871
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007872bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
7873{
Nadav Amit82b32772014-11-02 11:54:45 +02007874 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007875}
7876EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
7877
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007878unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
7879{
7880 unsigned long rflags;
7881
7882 rflags = kvm_x86_ops->get_rflags(vcpu);
7883 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01007884 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007885 return rflags;
7886}
7887EXPORT_SYMBOL_GPL(kvm_get_rflags);
7888
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007889static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007890{
7891 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007892 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01007893 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007894 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01007895}
7896
7897void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
7898{
7899 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03007900 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02007901}
7902EXPORT_SYMBOL_GPL(kvm_set_rflags);
7903
Gleb Natapov56028d02010-10-17 18:13:42 +02007904void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
7905{
7906 int r;
7907
Xiao Guangrongfb67e142010-12-07 10:35:25 +08007908 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08007909 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02007910 return;
7911
7912 r = kvm_mmu_reload(vcpu);
7913 if (unlikely(r))
7914 return;
7915
Xiao Guangrongfb67e142010-12-07 10:35:25 +08007916 if (!vcpu->arch.mmu.direct_map &&
7917 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
7918 return;
7919
Gleb Natapov56028d02010-10-17 18:13:42 +02007920 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
7921}
7922
Gleb Natapovaf585b92010-10-14 11:22:46 +02007923static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
7924{
7925 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
7926}
7927
7928static inline u32 kvm_async_pf_next_probe(u32 key)
7929{
7930 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
7931}
7932
7933static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7934{
7935 u32 key = kvm_async_pf_hash_fn(gfn);
7936
7937 while (vcpu->arch.apf.gfns[key] != ~0)
7938 key = kvm_async_pf_next_probe(key);
7939
7940 vcpu->arch.apf.gfns[key] = gfn;
7941}
7942
7943static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
7944{
7945 int i;
7946 u32 key = kvm_async_pf_hash_fn(gfn);
7947
7948 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08007949 (vcpu->arch.apf.gfns[key] != gfn &&
7950 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02007951 key = kvm_async_pf_next_probe(key);
7952
7953 return key;
7954}
7955
7956bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7957{
7958 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
7959}
7960
7961static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
7962{
7963 u32 i, j, k;
7964
7965 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
7966 while (true) {
7967 vcpu->arch.apf.gfns[i] = ~0;
7968 do {
7969 j = kvm_async_pf_next_probe(j);
7970 if (vcpu->arch.apf.gfns[j] == ~0)
7971 return;
7972 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
7973 /*
7974 * k lies cyclically in ]i,j]
7975 * | i.k.j |
7976 * |....j i.k.| or |.k..j i...|
7977 */
7978 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
7979 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
7980 i = j;
7981 }
7982}
7983
Gleb Natapov7c907052010-10-14 11:22:53 +02007984static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
7985{
7986
7987 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
7988 sizeof(val));
7989}
7990
Gleb Natapovaf585b92010-10-14 11:22:46 +02007991void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
7992 struct kvm_async_pf *work)
7993{
Avi Kivity6389ee92010-11-29 16:12:30 +02007994 struct x86_exception fault;
7995
Gleb Natapov7c907052010-10-14 11:22:53 +02007996 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007997 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02007998
7999 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02008000 (vcpu->arch.apf.send_user_only &&
8001 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02008002 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
8003 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008004 fault.vector = PF_VECTOR;
8005 fault.error_code_valid = true;
8006 fault.error_code = 0;
8007 fault.nested_page_fault = false;
8008 fault.address = work->arch.token;
8009 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008010 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008011}
8012
8013void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
8014 struct kvm_async_pf *work)
8015{
Avi Kivity6389ee92010-11-29 16:12:30 +02008016 struct x86_exception fault;
8017
Gleb Natapov7c907052010-10-14 11:22:53 +02008018 trace_kvm_async_pf_ready(work->arch.token, work->gva);
chai wenf2e10662013-10-14 22:22:33 +08008019 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02008020 work->arch.token = ~0; /* broadcast wakeup */
8021 else
8022 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
8023
8024 if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
8025 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008026 fault.vector = PF_VECTOR;
8027 fault.error_code_valid = true;
8028 fault.error_code = 0;
8029 fault.nested_page_fault = false;
8030 fault.address = work->arch.token;
8031 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008032 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08008033 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03008034 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02008035}
8036
8037bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
8038{
8039 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
8040 return true;
8041 else
8042 return !kvm_event_needs_reinjection(vcpu) &&
8043 kvm_x86_ops->interrupt_allowed(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008044}
8045
Paolo Bonzini5544eb92015-07-07 15:41:58 +02008046void kvm_arch_start_assignment(struct kvm *kvm)
8047{
8048 atomic_inc(&kvm->arch.assigned_device_count);
8049}
8050EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
8051
8052void kvm_arch_end_assignment(struct kvm *kvm)
8053{
8054 atomic_dec(&kvm->arch.assigned_device_count);
8055}
8056EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
8057
8058bool kvm_arch_has_assigned_device(struct kvm *kvm)
8059{
8060 return atomic_read(&kvm->arch.assigned_device_count);
8061}
8062EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
8063
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06008064void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
8065{
8066 atomic_inc(&kvm->arch.noncoherent_dma_count);
8067}
8068EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
8069
8070void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
8071{
8072 atomic_dec(&kvm->arch.noncoherent_dma_count);
8073}
8074EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
8075
8076bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
8077{
8078 return atomic_read(&kvm->arch.noncoherent_dma_count);
8079}
8080EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
8081
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008082EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +08008083EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008084EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
8085EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
8086EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
8087EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02008088EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02008089EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02008090EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02008091EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02008092EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02008093EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01008094EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09008095EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +02008096EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
Kai Huang843e4332015-01-28 10:54:28 +08008097EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +08008098EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);