blob: d86f563a68960163e2b2b50adfea0c2c5938bcca [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030014 * Amit Shah <amit.shah@qumranet.com>
15 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020016 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 */
21
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020023#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080024#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080025#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020026#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030027#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030028#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020029#include "cpuid.h"
Paolo Bonzinic9eab582014-11-24 15:27:17 +010030#include "assigned-dev.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020031#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030032#include "hyperv.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020033
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020034#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030035#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020036#include <linux/kvm.h>
37#include <linux/fs.h>
38#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010039#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080040#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050041#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010042#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030043#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010044#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030045#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020046#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080048#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080049#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020050#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020051#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020052#include <linux/timekeeper_internal.h>
53#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080054#include <linux/kvm_irqfd.h>
55#include <linux/irqbypass.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030056#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#define CREATE_TRACE_POINTS
59#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020060
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020061#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080062#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020063#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080064#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020065#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020066#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100067#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030068#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080069#include <asm/irq_remapping.h>
Carsten Otte043405e2007-10-10 17:16:19 +020070
Carsten Otte313a3dc2007-10-11 19:16:52 +020071#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080072#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080073u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
74EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080075
Avi Kivity0f65dd72011-04-20 13:37:53 +030076#define emul_to_vcpu(ctxt) \
77 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
78
Joerg Roedel50a37eb2008-01-31 14:57:38 +010079/* EFER defaults:
80 * - enable syscall per default because its emulated by KVM
81 * - enable LME and LMA per default on 64 bit KVM
82 */
83#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080084static
85u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010086#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080087static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010088#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020089
Avi Kivityba1389b2007-11-18 16:24:12 +020090#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
91#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050092
Radim Krčmář371313132016-07-12 22:09:27 +020093#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS)
94
Gleb Natapovcb142eb2009-08-09 15:17:40 +030095static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +030096static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +020097static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010098static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Avi Kivity674eea02008-02-11 18:37:23 +020099
Paolo Bonzini893590c2015-11-06 11:46:24 +0100100struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300101EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800102
Paolo Bonzini893590c2015-11-06 11:46:24 +0100103static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030104module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200105
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200106unsigned int min_timer_period_us = 500;
107module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
108
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300109static bool __read_mostly kvmclock_periodic_sync = true;
110module_param(kvmclock_periodic_sync, bool, S_IRUGO);
111
Paolo Bonzini893590c2015-11-06 11:46:24 +0100112bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100113EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100114u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100115EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800116u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
117EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
118u64 __read_mostly kvm_max_tsc_scaling_ratio;
119EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700120u64 __read_mostly kvm_default_tsc_scaling_ratio;
121EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100122
Zachary Amsdencc578282012-02-03 15:43:50 -0200123/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100124static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200125module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
126
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500127/* lapic timer advance (tscdeadline mode only) in nanoseconds */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128unsigned int __read_mostly lapic_timer_advance_ns = 0;
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500129module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
130
Feng Wu520040142016-01-25 16:53:33 +0800131static bool __read_mostly vector_hashing = true;
132module_param(vector_hashing, bool, S_IRUGO);
133
Paolo Bonzini893590c2015-11-06 11:46:24 +0100134static bool __read_mostly backwards_tsc_observed = false;
Marcelo Tosatti16a96022014-05-14 12:43:24 -0300135
Avi Kivity18863bd2009-09-07 11:12:18 +0300136#define KVM_NR_SHARED_MSRS 16
137
138struct kvm_shared_msrs_global {
139 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800140 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300141};
142
143struct kvm_shared_msrs {
144 struct user_return_notifier urn;
145 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800146 struct kvm_shared_msr_values {
147 u64 host;
148 u64 curr;
149 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300150};
151
152static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200153static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300154
Hollis Blanchard417bc302007-10-31 17:24:23 -0500155struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200156 { "pf_fixed", VCPU_STAT(pf_fixed) },
157 { "pf_guest", VCPU_STAT(pf_guest) },
158 { "tlb_flush", VCPU_STAT(tlb_flush) },
159 { "invlpg", VCPU_STAT(invlpg) },
160 { "exits", VCPU_STAT(exits) },
161 { "io_exits", VCPU_STAT(io_exits) },
162 { "mmio_exits", VCPU_STAT(mmio_exits) },
163 { "signal_exits", VCPU_STAT(signal_exits) },
164 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800165 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200166 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100167 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200168 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200169 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200170 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530171 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200172 { "request_irq", VCPU_STAT(request_irq_exits) },
173 { "irq_exits", VCPU_STAT(irq_exits) },
174 { "host_state_reload", VCPU_STAT(host_state_reload) },
175 { "efer_reload", VCPU_STAT(efer_reload) },
176 { "fpu_reload", VCPU_STAT(fpu_reload) },
177 { "insn_emulation", VCPU_STAT(insn_emulation) },
178 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300179 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200180 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200181 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
182 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
183 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
184 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
185 { "mmu_flooded", VM_STAT(mmu_flooded) },
186 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200187 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300188 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200189 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300190 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500191 { NULL }
192};
193
Dexuan Cui2acf9232010-06-10 11:27:12 +0800194u64 __read_mostly host_xcr0;
195
Jan Kiszkab6785de2012-09-20 07:43:17 +0200196static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300197
Gleb Natapovaf585b92010-10-14 11:22:46 +0200198static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
199{
200 int i;
201 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
202 vcpu->arch.apf.gfns[i] = ~0;
203}
204
Avi Kivity18863bd2009-09-07 11:12:18 +0300205static void kvm_on_user_return(struct user_return_notifier *urn)
206{
207 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300208 struct kvm_shared_msrs *locals
209 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800210 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300211
212 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800213 values = &locals->values[slot];
214 if (values->host != values->curr) {
215 wrmsrl(shared_msrs_global.msrs[slot], values->host);
216 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300217 }
218 }
219 locals->registered = false;
220 user_return_notifier_unregister(urn);
221}
222
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800223static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300224{
Avi Kivity18863bd2009-09-07 11:12:18 +0300225 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200226 unsigned int cpu = smp_processor_id();
227 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300228
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800229 /* only read, and nobody should modify it at this time,
230 * so don't need lock */
231 if (slot >= shared_msrs_global.nr) {
232 printk(KERN_ERR "kvm: invalid MSR slot!");
233 return;
234 }
235 rdmsrl_safe(msr, &value);
236 smsr->values[slot].host = value;
237 smsr->values[slot].curr = value;
238}
239
240void kvm_define_shared_msr(unsigned slot, u32 msr)
241{
Nadav Amit0123be42014-07-24 15:06:56 +0300242 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200243 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300244 if (slot >= shared_msrs_global.nr)
245 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300246}
247EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
248
249static void kvm_shared_msr_cpu_online(void)
250{
251 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300252
253 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800254 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300255}
256
Andy Honig8b3c3102014-08-27 11:16:44 -0700257int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300258{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200259 unsigned int cpu = smp_processor_id();
260 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700261 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300262
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800263 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Andy Honig8b3c3102014-08-27 11:16:44 -0700264 return 0;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800265 smsr->values[slot].curr = value;
Andy Honig8b3c3102014-08-27 11:16:44 -0700266 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
267 if (err)
268 return 1;
269
Avi Kivity18863bd2009-09-07 11:12:18 +0300270 if (!smsr->registered) {
271 smsr->urn.on_user_return = kvm_on_user_return;
272 user_return_notifier_register(&smsr->urn);
273 smsr->registered = true;
274 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700275 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300276}
277EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
278
Radim Krčmář13a34e02014-08-28 15:13:03 +0200279static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200280{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200281 unsigned int cpu = smp_processor_id();
282 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200283
284 if (smsr->registered)
285 kvm_on_user_return(&smsr->urn);
286}
287
Carsten Otte6866b832007-10-29 16:09:10 +0100288u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
289{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300290 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100291}
292EXPORT_SYMBOL_GPL(kvm_get_apic_base);
293
Jan Kiszka58cb6282014-01-24 16:48:44 +0100294int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100295{
Jan Kiszka58cb6282014-01-24 16:48:44 +0100296 u64 old_state = vcpu->arch.apic_base &
297 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
298 u64 new_state = msr_info->data &
299 (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
300 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
301 0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
302
303 if (!msr_info->host_initiated &&
304 ((msr_info->data & reserved_bits) != 0 ||
305 new_state == X2APIC_ENABLE ||
306 (new_state == MSR_IA32_APICBASE_ENABLE &&
307 old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
308 (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
309 old_state == 0)))
310 return 1;
311
312 kvm_lapic_set_base(vcpu, msr_info->data);
313 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100314}
315EXPORT_SYMBOL_GPL(kvm_set_apic_base);
316
Andi Kleen2605fc22014-05-02 00:44:37 +0200317asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000318{
319 /* Fault while not rebooting. We want the trace. */
320 BUG();
321}
322EXPORT_SYMBOL_GPL(kvm_spurious_fault);
323
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200324#define EXCPT_BENIGN 0
325#define EXCPT_CONTRIBUTORY 1
326#define EXCPT_PF 2
327
328static int exception_class(int vector)
329{
330 switch (vector) {
331 case PF_VECTOR:
332 return EXCPT_PF;
333 case DE_VECTOR:
334 case TS_VECTOR:
335 case NP_VECTOR:
336 case SS_VECTOR:
337 case GP_VECTOR:
338 return EXCPT_CONTRIBUTORY;
339 default:
340 break;
341 }
342 return EXCPT_BENIGN;
343}
344
Nadav Amitd6e8c852014-07-24 14:51:24 +0300345#define EXCPT_FAULT 0
346#define EXCPT_TRAP 1
347#define EXCPT_ABORT 2
348#define EXCPT_INTERRUPT 3
349
350static int exception_type(int vector)
351{
352 unsigned int mask;
353
354 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
355 return EXCPT_INTERRUPT;
356
357 mask = 1 << vector;
358
359 /* #DB is trap, as instruction watchpoints are handled elsewhere */
360 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
361 return EXCPT_TRAP;
362
363 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
364 return EXCPT_ABORT;
365
366 /* Reserved exceptions will result in fault */
367 return EXCPT_FAULT;
368}
369
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200370static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200371 unsigned nr, bool has_error, u32 error_code,
372 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200373{
374 u32 prev_nr;
375 int class1, class2;
376
Avi Kivity3842d132010-07-27 12:30:24 +0300377 kvm_make_request(KVM_REQ_EVENT, vcpu);
378
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200379 if (!vcpu->arch.exception.pending) {
380 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200381 if (has_error && !is_protmode(vcpu))
382 has_error = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200383 vcpu->arch.exception.pending = true;
384 vcpu->arch.exception.has_error_code = has_error;
385 vcpu->arch.exception.nr = nr;
386 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200387 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200388 return;
389 }
390
391 /* to check exception */
392 prev_nr = vcpu->arch.exception.nr;
393 if (prev_nr == DF_VECTOR) {
394 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300395 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200396 return;
397 }
398 class1 = exception_class(prev_nr);
399 class2 = exception_class(nr);
400 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
401 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
402 /* generate double fault per SDM Table 5-5 */
403 vcpu->arch.exception.pending = true;
404 vcpu->arch.exception.has_error_code = true;
405 vcpu->arch.exception.nr = DF_VECTOR;
406 vcpu->arch.exception.error_code = 0;
407 } else
408 /* replace previous exception with a new one in a hope
409 that instruction re-execution will regenerate lost
410 exception */
411 goto queue;
412}
413
Avi Kivity298101d2007-11-25 13:41:11 +0200414void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
415{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200416 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200417}
418EXPORT_SYMBOL_GPL(kvm_queue_exception);
419
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200420void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
421{
422 kvm_multiple_exception(vcpu, nr, false, 0, true);
423}
424EXPORT_SYMBOL_GPL(kvm_requeue_exception);
425
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100426void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200427{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100428 if (err)
429 kvm_inject_gp(vcpu, 0);
430 else
431 kvm_x86_ops->skip_emulated_instruction(vcpu);
432}
433EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200434
Avi Kivity6389ee92010-11-29 16:12:30 +0200435void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200436{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200437 ++vcpu->stat.pf_guest;
Avi Kivity6389ee92010-11-29 16:12:30 +0200438 vcpu->arch.cr2 = fault->address;
439 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200440}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300441EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200442
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200443static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200444{
Avi Kivity6389ee92010-11-29 16:12:30 +0200445 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
446 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200447 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200448 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200449
450 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200451}
452
Sheng Yang3419ffc2008-05-15 09:52:48 +0800453void kvm_inject_nmi(struct kvm_vcpu *vcpu)
454{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300455 atomic_inc(&vcpu->arch.nmi_queued);
456 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800457}
458EXPORT_SYMBOL_GPL(kvm_inject_nmi);
459
Avi Kivity298101d2007-11-25 13:41:11 +0200460void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
461{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200462 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200463}
464EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
465
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200466void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
467{
468 kvm_multiple_exception(vcpu, nr, true, error_code, true);
469}
470EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
471
Carsten Ottea03490e2007-10-29 16:09:35 +0100472/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300473 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
474 * a #GP and return false.
475 */
476bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200477{
Avi Kivity0a79b002009-09-01 12:03:25 +0300478 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
479 return true;
480 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
481 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100482}
Avi Kivity0a79b002009-09-01 12:03:25 +0300483EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100484
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300485bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
486{
487 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
488 return true;
489
490 kvm_queue_exception(vcpu, UD_VECTOR);
491 return false;
492}
493EXPORT_SYMBOL_GPL(kvm_require_dr);
494
Carsten Ottea03490e2007-10-29 16:09:35 +0100495/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200496 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200497 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200498 * can read from guest physical or from the guest's guest physical memory.
499 */
500int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
501 gfn_t ngfn, void *data, int offset, int len,
502 u32 access)
503{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200504 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200505 gfn_t real_gfn;
506 gpa_t ngpa;
507
508 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200509 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200510 if (real_gfn == UNMAPPED_GVA)
511 return -EFAULT;
512
513 real_gfn = gpa_to_gfn(real_gfn);
514
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200515 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200516}
517EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
518
Fengguang Wu69b00492015-01-19 22:33:39 +0800519static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200520 void *data, int offset, int len, u32 access)
521{
522 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
523 data, offset, len, access);
524}
525
Joerg Roedelec92fe42010-09-10 17:30:51 +0200526/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100527 * Load the pae pdptrs. Return true is they are all valid.
528 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200529int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100530{
531 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
532 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
533 int i;
534 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200535 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100536
Joerg Roedelff03a072010-09-10 17:30:57 +0200537 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
538 offset * sizeof(u64), sizeof(pdpte),
539 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100540 if (ret < 0) {
541 ret = 0;
542 goto out;
543 }
544 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400545 if ((pdpte[i] & PT_PRESENT_MASK) &&
Xiao Guangronga0a64f52015-08-05 12:04:21 +0800546 (pdpte[i] &
547 vcpu->arch.mmu.guest_rsvd_check.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100548 ret = 0;
549 goto out;
550 }
551 }
552 ret = 1;
553
Joerg Roedelff03a072010-09-10 17:30:57 +0200554 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300555 __set_bit(VCPU_EXREG_PDPTR,
556 (unsigned long *)&vcpu->arch.regs_avail);
557 __set_bit(VCPU_EXREG_PDPTR,
558 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100559out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100560
561 return ret;
562}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100563EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100564
Avi Kivityd835dfe2007-11-21 02:57:59 +0200565static bool pdptrs_changed(struct kvm_vcpu *vcpu)
566{
Joerg Roedelff03a072010-09-10 17:30:57 +0200567 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200568 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200569 int offset;
570 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200571 int r;
572
573 if (is_long_mode(vcpu) || !is_pae(vcpu))
574 return false;
575
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300576 if (!test_bit(VCPU_EXREG_PDPTR,
577 (unsigned long *)&vcpu->arch.regs_avail))
578 return true;
579
Avi Kivity9f8fe502010-12-05 17:30:00 +0200580 gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
581 offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200582 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
583 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200584 if (r < 0)
585 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200586 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200587out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200588
589 return changed;
590}
591
Avi Kivity49a9b072010-06-10 17:02:14 +0300592int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100593{
Sheng Yangaad82702010-05-12 16:40:42 +0800594 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800595 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800596
Avi Kivityf9a48e62010-01-06 19:10:22 +0200597 cr0 |= X86_CR0_ET;
598
Gleb Natapovab344822010-01-21 15:28:46 +0200599#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300600 if (cr0 & 0xffffffff00000000UL)
601 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200602#endif
603
604 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100605
Gleb Natapov0f122442010-04-28 19:15:31 +0300606 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
607 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100608
Gleb Natapov0f122442010-04-28 19:15:31 +0300609 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
610 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100611
612 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
613#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200614 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100615 int cs_db, cs_l;
616
Gleb Natapov0f122442010-04-28 19:15:31 +0300617 if (!is_pae(vcpu))
618 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100619 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300620 if (cs_l)
621 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100622 } else
623#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200624 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200625 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300626 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100627 }
628
Mao, Junjiead756a12012-07-02 01:18:48 +0000629 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
630 return 1;
631
Carsten Ottea03490e2007-10-29 16:09:35 +0100632 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100633
Lai Jiangshand170c412011-02-21 11:21:30 +0800634 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800635 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800636 kvm_async_pf_hash_reset(vcpu);
637 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800638
Sheng Yangaad82702010-05-12 16:40:42 +0800639 if ((cr0 ^ old_cr0) & update_bits)
640 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800641
Laszlo Ersek879ae182015-11-04 12:54:41 +0100642 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
643 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
644 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800645 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
646
Gleb Natapov0f122442010-04-28 19:15:31 +0300647 return 0;
648}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200649EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100650
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200651void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100652{
Avi Kivity49a9b072010-06-10 17:02:14 +0300653 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100654}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200655EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100656
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300657static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
658{
659 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
660 !vcpu->guest_xcr0_loaded) {
661 /* kvm_set_xcr() also depends on this */
662 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
663 vcpu->guest_xcr0_loaded = 1;
664 }
665}
666
667static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
668{
669 if (vcpu->guest_xcr0_loaded) {
670 if (vcpu->arch.xcr0 != host_xcr0)
671 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
672 vcpu->guest_xcr0_loaded = 0;
673 }
674}
675
Fengguang Wu69b00492015-01-19 22:33:39 +0800676static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800677{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000678 u64 xcr0 = xcr;
679 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200680 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800681
682 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
683 if (index != XCR_XFEATURE_ENABLED_MASK)
684 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700685 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800686 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700687 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800688 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200689
690 /*
691 * Do not allow the guest to set bits that we do not support
692 * saving. However, xcr0 bit 0 is always set, even if the
693 * emulated CPU does not support XSAVE (see fx_init).
694 */
Dave Hansend91cab72015-09-02 16:31:26 -0700695 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200696 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800697 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200698
Dave Hansend91cab72015-09-02 16:31:26 -0700699 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
700 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000701 return 1;
702
Dave Hansend91cab72015-09-02 16:31:26 -0700703 if (xcr0 & XFEATURE_MASK_AVX512) {
704 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800705 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700706 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800707 return 1;
708 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800709 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000710
Dave Hansend91cab72015-09-02 16:31:26 -0700711 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000712 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800713 return 0;
714}
715
716int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
717{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000718 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
719 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800720 kvm_inject_gp(vcpu, 0);
721 return 1;
722 }
723 return 0;
724}
725EXPORT_SYMBOL_GPL(kvm_set_xcr);
726
Avi Kivitya83b29c2010-06-10 17:02:15 +0300727int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100728{
Avi Kivityfc78f512009-12-07 12:16:48 +0200729 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800730 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800731 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800732
Gleb Natapov0f122442010-04-28 19:15:31 +0300733 if (cr4 & CR4_RESERVED_BITS)
734 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100735
Dexuan Cui2acf9232010-06-10 11:27:12 +0800736 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
737 return 1;
738
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800739 if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
740 return 1;
741
Feng Wu97ec8c02014-04-01 17:46:34 +0800742 if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
743 return 1;
744
H. Peter Anvinafcbf132013-04-27 16:37:47 -0700745 if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800746 return 1;
747
Huaitong Hanb9baba82016-03-22 16:51:21 +0800748 if (!guest_cpuid_has_pku(vcpu) && (cr4 & X86_CR4_PKE))
749 return 1;
750
Carsten Ottea03490e2007-10-29 16:09:35 +0100751 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300752 if (!(cr4 & X86_CR4_PAE))
753 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300754 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
755 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200756 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
757 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300758 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100759
Mao, Junjiead756a12012-07-02 01:18:48 +0000760 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
761 if (!guest_cpuid_has_pcid(vcpu))
762 return 1;
763
764 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
765 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
766 return 1;
767 }
768
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300769 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300770 return 1;
771
Mao, Junjiead756a12012-07-02 01:18:48 +0000772 if (((cr4 ^ old_cr4) & pdptr_bits) ||
773 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800774 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300775
Huaitong Hanb9baba82016-03-22 16:51:21 +0800776 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200777 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800778
Gleb Natapov0f122442010-04-28 19:15:31 +0300779 return 0;
780}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200781EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100782
Avi Kivity23902182010-06-10 17:02:16 +0300783int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100784{
Paolo Bonziniac146232014-11-10 13:53:25 +0100785#ifdef CONFIG_X86_64
Nadav Amit9d88fca2014-11-02 11:54:52 +0200786 cr3 &= ~CR3_PCID_INVD;
Paolo Bonziniac146232014-11-10 13:53:25 +0100787#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200788
Avi Kivity9f8fe502010-12-05 17:30:00 +0200789 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300790 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -0400791 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300792 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200793 }
794
Carsten Ottea03490e2007-10-29 16:09:35 +0100795 if (is_long_mode(vcpu)) {
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200796 if (cr3 & CR3_L_MODE_RESERVED_BITS)
797 return 1;
798 } else if (is_pae(vcpu) && is_paging(vcpu) &&
799 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300800 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100801
Gleb Natapov0f122442010-04-28 19:15:31 +0300802 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200803 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Paolo Bonzinid8d173d2013-10-02 16:56:11 +0200804 kvm_mmu_new_cr3(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300805 return 0;
806}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200807EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100808
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100809int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100810{
Gleb Natapov0f122442010-04-28 19:15:31 +0300811 if (cr8 & CR8_RESERVED_BITS)
812 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200813 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100814 kvm_lapic_set_tpr(vcpu, cr8);
815 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800816 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300817 return 0;
818}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200819EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100820
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200821unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100822{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200823 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100824 return kvm_lapic_get_cr8(vcpu);
825 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800826 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100827}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200828EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100829
Nadav Amitae561ed2015-04-02 03:10:37 +0300830static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
831{
832 int i;
833
834 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
835 for (i = 0; i < KVM_NR_DB_REGS; i++)
836 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
837 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
838 }
839}
840
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100841static void kvm_update_dr6(struct kvm_vcpu *vcpu)
842{
843 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
844 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
845}
846
Jan Kiszkac8639012012-09-21 05:42:55 +0200847static void kvm_update_dr7(struct kvm_vcpu *vcpu)
848{
849 unsigned long dr7;
850
851 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
852 dr7 = vcpu->arch.guest_debug_dr7;
853 else
854 dr7 = vcpu->arch.dr7;
855 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100856 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
857 if (dr7 & DR7_BP_EN_MASK)
858 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200859}
860
Nadav Amit6f43ed02014-07-15 17:37:46 +0300861static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
862{
863 u64 fixed = DR6_FIXED_1;
864
865 if (!guest_cpuid_has_rtm(vcpu))
866 fixed |= DR6_RTM;
867 return fixed;
868}
869
Gleb Natapov338dbc92010-04-28 19:15:32 +0300870static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300871{
872 switch (dr) {
873 case 0 ... 3:
874 vcpu->arch.db[dr] = val;
875 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
876 vcpu->arch.eff_db[dr] = val;
877 break;
878 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300879 /* fall through */
880 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300881 if (val & 0xffffffff00000000ULL)
882 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300883 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100884 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300885 break;
886 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300887 /* fall through */
888 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300889 if (val & 0xffffffff00000000ULL)
890 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300891 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200892 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300893 break;
894 }
895
896 return 0;
897}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300898
899int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
900{
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300901 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +0300902 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300903 return 1;
904 }
905 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +0300906}
Gleb Natapov020df072010-04-13 10:05:23 +0300907EXPORT_SYMBOL_GPL(kvm_set_dr);
908
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300909int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300910{
911 switch (dr) {
912 case 0 ... 3:
913 *val = vcpu->arch.db[dr];
914 break;
915 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300916 /* fall through */
917 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100918 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
919 *val = vcpu->arch.dr6;
920 else
921 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300922 break;
923 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300924 /* fall through */
925 default: /* 7 */
926 *val = vcpu->arch.dr7;
927 break;
928 }
Gleb Natapov338dbc92010-04-28 19:15:32 +0300929 return 0;
930}
Gleb Natapov020df072010-04-13 10:05:23 +0300931EXPORT_SYMBOL_GPL(kvm_get_dr);
932
Avi Kivity022cd0e2011-11-10 14:57:23 +0200933bool kvm_rdpmc(struct kvm_vcpu *vcpu)
934{
935 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
936 u64 data;
937 int err;
938
Wei Huangc6702c92015-06-19 13:44:45 +0200939 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +0200940 if (err)
941 return err;
942 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
943 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
944 return err;
945}
946EXPORT_SYMBOL_GPL(kvm_rdpmc);
947
Carsten Otte043405e2007-10-10 17:16:19 +0200948/*
949 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
950 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
951 *
952 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400953 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200954 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
955 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +0200956 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400957
Carsten Otte043405e2007-10-10 17:16:19 +0200958static u32 msrs_to_save[] = {
959 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400960 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200961#ifdef CONFIG_X86_64
962 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
963#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800964 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +0100965 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Carsten Otte043405e2007-10-10 17:16:19 +0200966};
967
968static unsigned num_msrs_to_save;
969
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200970static u32 emulated_msrs[] = {
971 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
972 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
973 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
974 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Andrey Smetanine7d95132015-07-03 15:01:37 +0300975 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
976 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300977 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +0300978 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300979 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300980 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300981 HV_X64_MSR_STIMER0_CONFIG,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200982 HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
983 MSR_KVM_PV_EOI_EN,
984
Will Auldba904632012-11-29 12:42:50 -0800985 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +0800986 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +0200987 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300988 MSR_IA32_MCG_STATUS,
989 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +0800990 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +0200991 MSR_IA32_SMBASE,
Carsten Otte043405e2007-10-10 17:16:19 +0200992};
993
Paolo Bonzini62ef68b2015-05-05 12:08:55 +0200994static unsigned num_emulated_msrs;
995
Jan Kiszka384bb782013-04-20 10:52:36 +0200996bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100997{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200998 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +0200999 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001000
Alexander Graf1b2fd702009-02-02 16:23:51 +01001001 if (efer & EFER_FFXSR) {
1002 struct kvm_cpuid_entry2 *feat;
1003
1004 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001005 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
Jan Kiszka384bb782013-04-20 10:52:36 +02001006 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +01001007 }
1008
Alexander Grafd8017472008-11-25 20:17:11 +01001009 if (efer & EFER_SVME) {
1010 struct kvm_cpuid_entry2 *feat;
1011
1012 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001013 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
Jan Kiszka384bb782013-04-20 10:52:36 +02001014 return false;
Alexander Grafd8017472008-11-25 20:17:11 +01001015 }
1016
Jan Kiszka384bb782013-04-20 10:52:36 +02001017 return true;
1018}
1019EXPORT_SYMBOL_GPL(kvm_valid_efer);
1020
1021static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
1022{
1023 u64 old_efer = vcpu->arch.efer;
1024
1025 if (!kvm_valid_efer(vcpu, efer))
1026 return 1;
1027
1028 if (is_paging(vcpu)
1029 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1030 return 1;
1031
Carsten Otte15c4a642007-10-30 18:44:17 +01001032 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001033 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001034
Sheng Yanga3d204e2010-05-12 16:40:40 +08001035 kvm_x86_ops->set_efer(vcpu, efer);
1036
Sheng Yangaad82702010-05-12 16:40:42 +08001037 /* Update reserved bits */
1038 if ((efer ^ old_efer) & EFER_NX)
1039 kvm_mmu_reset_context(vcpu);
1040
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001041 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001042}
1043
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001044void kvm_enable_efer_bits(u64 mask)
1045{
1046 efer_reserved_bits &= ~mask;
1047}
1048EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1049
Carsten Otte15c4a642007-10-30 18:44:17 +01001050/*
1051 * Writes msr value into into the appropriate "register".
1052 * Returns 0 on success, non-0 otherwise.
1053 * Assumes vcpu_load() was already called.
1054 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001055int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01001056{
Nadav Amit854e8bb2014-09-16 03:24:05 +03001057 switch (msr->index) {
1058 case MSR_FS_BASE:
1059 case MSR_GS_BASE:
1060 case MSR_KERNEL_GS_BASE:
1061 case MSR_CSTAR:
1062 case MSR_LSTAR:
1063 if (is_noncanonical_address(msr->data))
1064 return 1;
1065 break;
1066 case MSR_IA32_SYSENTER_EIP:
1067 case MSR_IA32_SYSENTER_ESP:
1068 /*
1069 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1070 * non-canonical address is written on Intel but not on
1071 * AMD (which ignores the top 32-bits, because it does
1072 * not implement 64-bit SYSENTER).
1073 *
1074 * 64-bit code should hence be able to write a non-canonical
1075 * value on AMD. Making the address canonical ensures that
1076 * vmentry does not fail on Intel after writing a non-canonical
1077 * value, and that something deterministic happens if the guest
1078 * invokes 64-bit SYSENTER.
1079 */
1080 msr->data = get_canonical(msr->data);
1081 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001082 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001083}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001084EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001085
Carsten Otte313a3dc2007-10-11 19:16:52 +02001086/*
1087 * Adapt set_msr() to msr_io()'s calling convention
1088 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001089static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1090{
1091 struct msr_data msr;
1092 int r;
1093
1094 msr.index = index;
1095 msr.host_initiated = true;
1096 r = kvm_get_msr(vcpu, &msr);
1097 if (r)
1098 return r;
1099
1100 *data = msr.data;
1101 return 0;
1102}
1103
Carsten Otte313a3dc2007-10-11 19:16:52 +02001104static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1105{
Will Auld8fe8ab42012-11-29 12:42:12 -08001106 struct msr_data msr;
1107
1108 msr.data = *data;
1109 msr.index = index;
1110 msr.host_initiated = true;
1111 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001112}
1113
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001114#ifdef CONFIG_X86_64
1115struct pvclock_gtod_data {
1116 seqcount_t seq;
1117
1118 struct { /* extract of a clocksource struct */
1119 int vclock_mode;
1120 cycle_t cycle_last;
1121 cycle_t mask;
1122 u32 mult;
1123 u32 shift;
1124 } clock;
1125
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001126 u64 boot_ns;
1127 u64 nsec_base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001128};
1129
1130static struct pvclock_gtod_data pvclock_gtod_data;
1131
1132static void update_pvclock_gtod(struct timekeeper *tk)
1133{
1134 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001135 u64 boot_ns;
1136
Peter Zijlstra876e7882015-03-19 10:09:06 +01001137 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001138
1139 write_seqcount_begin(&vdata->seq);
1140
1141 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001142 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1143 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1144 vdata->clock.mask = tk->tkr_mono.mask;
1145 vdata->clock.mult = tk->tkr_mono.mult;
1146 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001147
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001148 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001149 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001150
1151 write_seqcount_end(&vdata->seq);
1152}
1153#endif
1154
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001155void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1156{
1157 /*
1158 * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
1159 * vcpu_enter_guest. This function is only called from
1160 * the physical CPU that is running vcpu.
1161 */
1162 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1163}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001164
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001165static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1166{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001167 int version;
1168 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001169 struct pvclock_wall_clock wc;
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001170 struct timespec64 boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001171
1172 if (!wall_clock)
1173 return;
1174
Avi Kivity9ed3c442010-05-04 15:00:37 +03001175 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1176 if (r)
1177 return;
1178
1179 if (version & 1)
1180 ++version; /* first time write, random junk */
1181
1182 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001183
Nicholas Krause1dab1342015-12-30 13:08:46 -05001184 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1185 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001186
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001187 /*
1188 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001189 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001190 * wall clock specified here. guest system time equals host
1191 * system time for us, thus we must fill in host boot time here.
1192 */
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001193 getboottime64(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001194
Bruce Rogers4b648662012-07-20 10:44:24 -06001195 if (kvm->arch.kvmclock_offset) {
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001196 struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
1197 boot = timespec64_sub(boot, ts);
Bruce Rogers4b648662012-07-20 10:44:24 -06001198 }
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001199 wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001200 wc.nsec = boot.tv_nsec;
1201 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001202
1203 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1204
1205 version++;
1206 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001207}
1208
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001209static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1210{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001211 do_shl32_div32(dividend, divisor);
1212 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001213}
1214
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001215static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001216 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001217{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001218 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001219 int32_t shift = 0;
1220 uint64_t tps64;
1221 uint32_t tps32;
1222
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001223 tps64 = base_hz;
1224 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001225 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001226 tps64 >>= 1;
1227 shift--;
1228 }
1229
1230 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001231 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1232 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001233 scaled64 >>= 1;
1234 else
1235 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001236 shift++;
1237 }
1238
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001239 *pshift = shift;
1240 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001241
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001242 pr_debug("%s: base_hz %llu => %llu, shift %d, mul %u\n",
1243 __func__, base_hz, scaled_hz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001244}
1245
Marcelo Tosattid8281992012-11-27 23:29:01 -02001246#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001247static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001248#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001249
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001250static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001251static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001252
Joerg Roedel857e4092011-03-25 09:44:50 +01001253static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Zachary Amsden759379d2010-08-19 22:07:25 -10001254{
Zachary Amsdencc578282012-02-03 15:43:50 -02001255 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
1256 vcpu->arch.virtual_tsc_shift);
Zachary Amsden759379d2010-08-19 22:07:25 -10001257}
1258
Zachary Amsdencc578282012-02-03 15:43:50 -02001259static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001260{
Zachary Amsdencc578282012-02-03 15:43:50 -02001261 u64 v = (u64)khz * (1000000 + ppm);
1262 do_div(v, 1000000);
1263 return v;
1264}
1265
Haozhong Zhang381d5852015-10-20 15:39:04 +08001266static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1267{
1268 u64 ratio;
1269
1270 /* Guest TSC same frequency as host TSC? */
1271 if (!scale) {
1272 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1273 return 0;
1274 }
1275
1276 /* TSC scaling supported? */
1277 if (!kvm_has_tsc_control) {
1278 if (user_tsc_khz > tsc_khz) {
1279 vcpu->arch.tsc_catchup = 1;
1280 vcpu->arch.tsc_always_catchup = 1;
1281 return 0;
1282 } else {
1283 WARN(1, "user requested TSC rate below hardware speed\n");
1284 return -1;
1285 }
1286 }
1287
1288 /* TSC scaling required - calculate ratio */
1289 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1290 user_tsc_khz, tsc_khz);
1291
1292 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
1293 WARN_ONCE(1, "Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1294 user_tsc_khz);
1295 return -1;
1296 }
1297
1298 vcpu->arch.tsc_scaling_ratio = ratio;
1299 return 0;
1300}
1301
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001302static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001303{
1304 u32 thresh_lo, thresh_hi;
1305 int use_scaling = 0;
1306
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001307 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001308 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001309 /* set tsc_scaling_ratio to a safe value */
1310 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001311 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001312 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001313
Zachary Amsdenc2855452010-09-18 14:38:15 -10001314 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001315 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001316 &vcpu->arch.virtual_tsc_shift,
1317 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001318 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001319
1320 /*
1321 * Compute the variation in TSC rate which is acceptable
1322 * within the range of tolerance and decide if the
1323 * rate being applied is within that bounds of the hardware
1324 * rate. If so, no scaling or compensation need be done.
1325 */
1326 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1327 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001328 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1329 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi);
Zachary Amsdencc578282012-02-03 15:43:50 -02001330 use_scaling = 1;
1331 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001332 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001333}
1334
1335static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1336{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001337 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001338 vcpu->arch.virtual_tsc_mult,
1339 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001340 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001341 return tsc;
1342}
1343
Fengguang Wu69b00492015-01-19 22:33:39 +08001344static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001345{
1346#ifdef CONFIG_X86_64
1347 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001348 struct kvm_arch *ka = &vcpu->kvm->arch;
1349 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1350
1351 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1352 atomic_read(&vcpu->kvm->online_vcpus));
1353
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001354 /*
1355 * Once the masterclock is enabled, always perform request in
1356 * order to update it.
1357 *
1358 * In order to enable masterclock, the host clocksource must be TSC
1359 * and the vcpus need to have matched TSCs. When that happens,
1360 * perform request to enable masterclock.
1361 */
1362 if (ka->use_master_clock ||
1363 (gtod->clock.vclock_mode == VCLOCK_TSC && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001364 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1365
1366 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1367 atomic_read(&vcpu->kvm->online_vcpus),
1368 ka->use_master_clock, gtod->clock.vclock_mode);
1369#endif
1370}
1371
Will Auldba904632012-11-29 12:42:50 -08001372static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1373{
1374 u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
1375 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1376}
1377
Haozhong Zhang35181e82015-10-20 15:39:03 +08001378/*
1379 * Multiply tsc by a fixed point number represented by ratio.
1380 *
1381 * The most significant 64-N bits (mult) of ratio represent the
1382 * integral part of the fixed point number; the remaining N bits
1383 * (frac) represent the fractional part, ie. ratio represents a fixed
1384 * point number (mult + frac * 2^(-N)).
1385 *
1386 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1387 */
1388static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1389{
1390 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1391}
1392
1393u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1394{
1395 u64 _tsc = tsc;
1396 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1397
1398 if (ratio != kvm_default_tsc_scaling_ratio)
1399 _tsc = __scale_tsc(ratio, tsc);
1400
1401 return _tsc;
1402}
1403EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1404
Haozhong Zhang07c14192015-10-20 15:39:05 +08001405static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1406{
1407 u64 tsc;
1408
1409 tsc = kvm_scale_tsc(vcpu, rdtsc());
1410
1411 return target_tsc - tsc;
1412}
1413
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001414u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1415{
1416 return kvm_x86_ops->read_l1_tsc(vcpu, kvm_scale_tsc(vcpu, host_tsc));
1417}
1418EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1419
Will Auld8fe8ab42012-11-29 12:42:12 -08001420void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001421{
1422 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001423 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001424 unsigned long flags;
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001425 s64 usdiff;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001426 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001427 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001428 u64 data = msr->data;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001429
Jan Kiszka038f8c12011-02-04 10:49:11 +01001430 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001431 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001432 ns = get_kernel_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001433 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001434
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001435 if (vcpu->arch.virtual_tsc_khz) {
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001436 int faulted = 0;
1437
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001438 /* n.b - signed multiplication and division required */
1439 usdiff = data - kvm->arch.last_tsc_write;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001440#ifdef CONFIG_X86_64
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001441 usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001442#else
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001443 /* do_div() only does unsigned */
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001444 asm("1: idivl %[divisor]\n"
1445 "2: xor %%edx, %%edx\n"
1446 " movl $0, %[faulted]\n"
1447 "3:\n"
1448 ".section .fixup,\"ax\"\n"
1449 "4: movl $1, %[faulted]\n"
1450 " jmp 3b\n"
1451 ".previous\n"
1452
1453 _ASM_EXTABLE(1b, 4b)
1454
1455 : "=A"(usdiff), [faulted] "=r" (faulted)
1456 : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
1457
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001458#endif
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001459 do_div(elapsed, 1000);
1460 usdiff -= elapsed;
1461 if (usdiff < 0)
1462 usdiff = -usdiff;
Marcelo Tosatti8915aa272013-06-11 23:31:12 -03001463
1464 /* idivl overflow => difference is larger than USEC_PER_SEC */
1465 if (faulted)
1466 usdiff = USEC_PER_SEC;
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001467 } else
1468 usdiff = USEC_PER_SEC; /* disable TSC match window below */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001469
1470 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001471 * Special case: TSC write with a small delta (1 second) of virtual
1472 * cycle time against real time is interpreted as an attempt to
1473 * synchronize the CPU.
1474 *
1475 * For a reliable TSC, we can match TSC offsets, and for an unstable
1476 * TSC, we add elapsed time in this computation. We could let the
1477 * compensation code attempt to catch up if we fall behind, but
1478 * it's better to try to match offsets from the beginning.
1479 */
Marcelo Tosatti02626b62012-03-08 18:46:57 -03001480 if (usdiff < USEC_PER_SEC &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001481 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001482 if (!check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001483 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001484 pr_debug("kvm: matched tsc offset for %llu\n", data);
1485 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001486 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001487 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08001488 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001489 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001490 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001491 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001492 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001493 } else {
1494 /*
1495 * We split periods of matched TSC writes into generations.
1496 * For each generation, we track the original measured
1497 * nanosecond time, offset, and write, so if TSCs are in
1498 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001499 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001500 *
1501 * These values are tracked in kvm->arch.cur_xxx variables.
1502 */
1503 kvm->arch.cur_tsc_generation++;
1504 kvm->arch.cur_tsc_nsec = ns;
1505 kvm->arch.cur_tsc_write = data;
1506 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001507 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001508 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001509 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001510 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001511
1512 /*
1513 * We also track th most recent recorded KHZ, write and time to
1514 * allow the matching interval to be extended at each write.
1515 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001516 kvm->arch.last_tsc_nsec = ns;
1517 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001518 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001519
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001520 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001521
1522 /* Keep track of which generation this VCPU has synchronized to */
1523 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1524 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1525 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1526
Will Auldba904632012-11-29 12:42:50 -08001527 if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
1528 update_ia32_tsc_adjust_msr(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001529 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1530 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001531
1532 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001533 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001534 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001535 } else if (!already_matched) {
1536 kvm->arch.nr_vcpus_matched_tsc++;
1537 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001538
1539 kvm_track_tsc_matching(vcpu);
1540 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001541}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001542
Zachary Amsden99e3e302010-08-19 22:07:17 -10001543EXPORT_SYMBOL_GPL(kvm_write_tsc);
1544
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001545static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1546 s64 adjustment)
1547{
1548 kvm_x86_ops->adjust_tsc_offset_guest(vcpu, adjustment);
1549}
1550
1551static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1552{
1553 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1554 WARN_ON(adjustment < 0);
1555 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
1556 kvm_x86_ops->adjust_tsc_offset_guest(vcpu, adjustment);
1557}
1558
Marcelo Tosattid8281992012-11-27 23:29:01 -02001559#ifdef CONFIG_X86_64
1560
1561static cycle_t read_tsc(void)
1562{
Andy Lutomirski03b97302015-06-25 18:44:08 +02001563 cycle_t ret = (cycle_t)rdtsc_ordered();
1564 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001565
1566 if (likely(ret >= last))
1567 return ret;
1568
1569 /*
1570 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001571 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02001572 * very likely) and there's a data dependence, so force GCC
1573 * to generate a branch instead. I don't barrier() because
1574 * we don't actually need a barrier, and if this function
1575 * ever gets inlined it will generate worse code.
1576 */
1577 asm volatile ("");
1578 return last;
1579}
1580
1581static inline u64 vgettsc(cycle_t *cycle_now)
1582{
1583 long v;
1584 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1585
1586 *cycle_now = read_tsc();
1587
1588 v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
1589 return v * gtod->clock.mult;
1590}
1591
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001592static int do_monotonic_boot(s64 *t, cycle_t *cycle_now)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001593{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001594 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001595 unsigned long seq;
1596 int mode;
1597 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001598
Marcelo Tosattid8281992012-11-27 23:29:01 -02001599 do {
1600 seq = read_seqcount_begin(&gtod->seq);
1601 mode = gtod->clock.vclock_mode;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001602 ns = gtod->nsec_base;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001603 ns += vgettsc(cycle_now);
1604 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001605 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001606 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001607 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001608
1609 return mode;
1610}
1611
1612/* returns true if host is using tsc clocksource */
1613static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
1614{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001615 /* checked again under seqlock below */
1616 if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
1617 return false;
1618
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001619 return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001620}
1621#endif
1622
1623/*
1624 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001625 * Assuming a stable TSC across physical CPUS, and a stable TSC
1626 * across virtual CPUs, the following condition is possible.
1627 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001628 * CPUs at the next numbered event.
1629 *
1630 * "timespecX" represents host monotonic time. "tscX" represents
1631 * RDTSC value.
1632 *
1633 * VCPU0 on CPU0 | VCPU1 on CPU1
1634 *
1635 * 1. read timespec0,tsc0
1636 * 2. | timespec1 = timespec0 + N
1637 * | tsc1 = tsc0 + M
1638 * 3. transition to guest | transition to guest
1639 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1640 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1641 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1642 *
1643 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1644 *
1645 * - ret0 < ret1
1646 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1647 * ...
1648 * - 0 < N - M => M < N
1649 *
1650 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1651 * always the case (the difference between two distinct xtime instances
1652 * might be smaller then the difference between corresponding TSC reads,
1653 * when updating guest vcpus pvclock areas).
1654 *
1655 * To avoid that problem, do not allow visibility of distinct
1656 * system_timestamp/tsc_timestamp values simultaneously: use a master
1657 * copy of host monotonic time values. Update that master copy
1658 * in lockstep.
1659 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001660 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001661 *
1662 */
1663
1664static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1665{
1666#ifdef CONFIG_X86_64
1667 struct kvm_arch *ka = &kvm->arch;
1668 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001669 bool host_tsc_clocksource, vcpus_matched;
1670
1671 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1672 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001673
1674 /*
1675 * If the host uses TSC clock, then passthrough TSC as stable
1676 * to the guest.
1677 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001678 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001679 &ka->master_kernel_ns,
1680 &ka->master_cycle_now);
1681
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001682 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Marcelo Tosatti54750f22015-01-20 15:54:52 -02001683 && !backwards_tsc_observed
1684 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001685
Marcelo Tosattid8281992012-11-27 23:29:01 -02001686 if (ka->use_master_clock)
1687 atomic_set(&kvm_guest_has_master_clock, 1);
1688
1689 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001690 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1691 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001692#endif
1693}
1694
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01001695void kvm_make_mclock_inprogress_request(struct kvm *kvm)
1696{
1697 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
1698}
1699
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001700static void kvm_gen_update_masterclock(struct kvm *kvm)
1701{
1702#ifdef CONFIG_X86_64
1703 int i;
1704 struct kvm_vcpu *vcpu;
1705 struct kvm_arch *ka = &kvm->arch;
1706
1707 spin_lock(&ka->pvclock_gtod_sync_lock);
1708 kvm_make_mclock_inprogress_request(kvm);
1709 /* no guest entries from this point */
1710 pvclock_update_vm_gtod_copy(kvm);
1711
1712 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001713 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001714
1715 /* guest entries allowed */
1716 kvm_for_each_vcpu(i, vcpu, kvm)
1717 clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
1718
1719 spin_unlock(&ka->pvclock_gtod_sync_lock);
1720#endif
1721}
1722
Zachary Amsden34c238a2010-09-18 14:38:14 -10001723static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001724{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01001725 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001726 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001727 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02001728 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001729 u64 tsc_timestamp, host_tsc;
Andy Honig0b794592013-02-20 14:48:10 -08001730 struct pvclock_vcpu_time_info guest_hv_clock;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001731 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001732 bool use_master_clock;
1733
1734 kernel_ns = 0;
1735 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001736
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001737 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02001738 * If the host uses TSC clock, then passthrough TSC as stable
1739 * to the guest.
1740 */
1741 spin_lock(&ka->pvclock_gtod_sync_lock);
1742 use_master_clock = ka->use_master_clock;
1743 if (use_master_clock) {
1744 host_tsc = ka->master_cycle_now;
1745 kernel_ns = ka->master_kernel_ns;
1746 }
1747 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001748
1749 /* Keep irq disabled to prevent changes to the clock */
1750 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01001751 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
1752 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03001753 local_irq_restore(flags);
1754 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
1755 return 1;
1756 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001757 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001758 host_tsc = rdtsc();
Marcelo Tosattid8281992012-11-27 23:29:01 -02001759 kernel_ns = get_kernel_ns();
1760 }
1761
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001762 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001763
1764 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10001765 * We may have to catch up the TSC to match elapsed wall clock
1766 * time for two reasons, even if kvmclock is used.
1767 * 1) CPU could have been running below the maximum TSC rate
1768 * 2) Broken TSC compensation resets the base at each VCPU
1769 * entry to avoid unknown leaps of TSC even when running
1770 * again on the same CPU. This may cause apparent elapsed
1771 * time to disappear, and the guest to stand still or run
1772 * very slowly.
1773 */
1774 if (vcpu->tsc_catchup) {
1775 u64 tsc = compute_guest_tsc(v, kernel_ns);
1776 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001777 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001778 tsc_timestamp = tsc;
1779 }
1780 }
1781
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001782 local_irq_restore(flags);
1783
Andy Honig0b794592013-02-20 14:48:10 -08001784 if (!vcpu->pv_time_enabled)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001785 return 0;
1786
Paolo Bonzini78db6a52016-02-08 14:51:40 +01001787 if (kvm_has_tsc_control)
1788 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
1789
1790 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001791 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001792 &vcpu->hv_clock.tsc_shift,
1793 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01001794 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001795 }
1796
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001797 /* With all the info we got, fill in the values */
Zachary Amsden1d5f0662010-08-19 22:07:30 -10001798 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10001799 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10001800 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03001801
Owen Hofmann09a0c3f2014-11-03 16:57:18 -08001802 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1803 &guest_hv_clock, sizeof(guest_hv_clock))))
1804 return 0;
1805
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001806 /* This VCPU is paused, but it's legal for a guest to read another
1807 * VCPU's kvmclock, so we really have to follow the specification where
1808 * it says that version is odd if data is being modified, and even after
1809 * it is consistent.
1810 *
1811 * Version field updates must be kept separate. This is because
1812 * kvm_write_guest_cached might use a "rep movs" instruction, and
1813 * writes within a string instruction are weakly ordered. So there
1814 * are three writes overall.
1815 *
1816 * As a small optimization, only write the version field in the first
1817 * and third write. The vcpu->pv_time cache is still valid, because the
1818 * version field is the first in the struct.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001819 */
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001820 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
1821
1822 vcpu->hv_clock.version = guest_hv_clock.version + 1;
1823 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1824 &vcpu->hv_clock,
1825 sizeof(vcpu->hv_clock.version));
1826
1827 smp_wmb();
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001828
1829 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
Andy Honig0b794592013-02-20 14:48:10 -08001830 pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001831
1832 if (vcpu->pvclock_set_guest_stopped_request) {
1833 pvclock_flags |= PVCLOCK_GUEST_STOPPED;
1834 vcpu->pvclock_set_guest_stopped_request = false;
1835 }
1836
Marcelo Tosattid8281992012-11-27 23:29:01 -02001837 /* If the host uses TSC clocksource, then it is stable */
1838 if (use_master_clock)
1839 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
1840
Marcelo Tosatti78c03372012-11-27 23:28:47 -02001841 vcpu->hv_clock.flags = pvclock_flags;
1842
David Matlackce1a5e62014-11-05 11:46:42 -08001843 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
1844
Andy Honig0b794592013-02-20 14:48:10 -08001845 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1846 &vcpu->hv_clock,
1847 sizeof(vcpu->hv_clock));
Radim Krčmář5dca0d92015-03-25 12:08:14 +01001848
1849 smp_wmb();
1850
1851 vcpu->hv_clock.version++;
1852 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
1853 &vcpu->hv_clock,
1854 sizeof(vcpu->hv_clock.version));
Zachary Amsden8cfdc002010-08-19 22:07:21 -10001855 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001856}
1857
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001858/*
1859 * kvmclock updates which are isolated to a given vcpu, such as
1860 * vcpu->cpu migration, should not allow system_timestamp from
1861 * the rest of the vcpus to remain static. Otherwise ntp frequency
1862 * correction applies to one vcpu's system_timestamp but not
1863 * the others.
1864 *
1865 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01001866 * We need to rate-limit these requests though, as they can
1867 * considerably slow guests that have a large number of vcpus.
1868 * The time for a remote vcpu to update its kvmclock is bound
1869 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001870 */
1871
Andrew Jones7e44e442014-02-28 12:52:54 +01001872#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
1873
1874static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001875{
1876 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01001877 struct delayed_work *dwork = to_delayed_work(work);
1878 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1879 kvmclock_update_work);
1880 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001881 struct kvm_vcpu *vcpu;
1882
1883 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001884 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03001885 kvm_vcpu_kick(vcpu);
1886 }
1887}
1888
Andrew Jones7e44e442014-02-28 12:52:54 +01001889static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
1890{
1891 struct kvm *kvm = v->kvm;
1892
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001893 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01001894 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
1895 KVMCLOCK_UPDATE_DELAY);
1896}
1897
Andrew Jones332967a2014-02-28 12:52:55 +01001898#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
1899
1900static void kvmclock_sync_fn(struct work_struct *work)
1901{
1902 struct delayed_work *dwork = to_delayed_work(work);
1903 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
1904 kvmclock_sync_work);
1905 struct kvm *kvm = container_of(ka, struct kvm, arch);
1906
Marcelo Tosatti630994b2015-05-12 22:42:04 -03001907 if (!kvmclock_periodic_sync)
1908 return;
1909
Andrew Jones332967a2014-02-28 12:52:55 +01001910 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
1911 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
1912 KVMCLOCK_SYNC_PERIOD);
1913}
1914
Huang Ying890ca9a2009-05-11 16:48:15 +08001915static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1916{
1917 u64 mcg_cap = vcpu->arch.mcg_cap;
1918 unsigned bank_num = mcg_cap & 0xff;
1919
1920 switch (msr) {
1921 case MSR_IA32_MCG_STATUS:
1922 vcpu->arch.mcg_status = data;
1923 break;
1924 case MSR_IA32_MCG_CTL:
1925 if (!(mcg_cap & MCG_CTL_P))
1926 return 1;
1927 if (data != 0 && data != ~(u64)0)
1928 return -1;
1929 vcpu->arch.mcg_ctl = data;
1930 break;
1931 default:
1932 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08001933 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08001934 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001935 /* only 0 or all 1s can be written to IA32_MCi_CTL
1936 * some Linux kernels though clear bit 10 in bank 4 to
1937 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1938 * this to avoid an uncatched #GP in the guest
1939 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001940 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001941 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001942 return -1;
1943 vcpu->arch.mce_banks[offset] = data;
1944 break;
1945 }
1946 return 1;
1947 }
1948 return 0;
1949}
1950
Ed Swierkffde22a2009-10-15 15:21:43 -07001951static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1952{
1953 struct kvm *kvm = vcpu->kvm;
1954 int lm = is_long_mode(vcpu);
1955 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1956 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1957 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1958 : kvm->arch.xen_hvm_config.blob_size_32;
1959 u32 page_num = data & ~PAGE_MASK;
1960 u64 page_addr = data & PAGE_MASK;
1961 u8 *page;
1962 int r;
1963
1964 r = -E2BIG;
1965 if (page_num >= blob_size)
1966 goto out;
1967 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001968 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
1969 if (IS_ERR(page)) {
1970 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07001971 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02001972 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001973 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07001974 goto out_free;
1975 r = 0;
1976out_free:
1977 kfree(page);
1978out:
1979 return r;
1980}
1981
Gleb Natapov344d9582010-10-14 11:22:50 +02001982static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
1983{
1984 gpa_t gpa = data & ~0x3f;
1985
Guo Chao4a969982012-06-28 15:17:27 +08001986 /* Bits 2:5 are reserved, Should be zero */
Gleb Natapov6adba522010-10-14 11:22:55 +02001987 if (data & 0x3c)
Gleb Natapov344d9582010-10-14 11:22:50 +02001988 return 1;
1989
1990 vcpu->arch.apf.msr_val = data;
1991
1992 if (!(data & KVM_ASYNC_PF_ENABLED)) {
1993 kvm_clear_async_pf_completion_queue(vcpu);
1994 kvm_async_pf_hash_reset(vcpu);
1995 return 0;
1996 }
1997
Andrew Honig8f964522013-03-29 09:35:21 -07001998 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
1999 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002000 return 1;
2001
Gleb Natapov6adba522010-10-14 11:22:55 +02002002 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Gleb Natapov344d9582010-10-14 11:22:50 +02002003 kvm_async_pf_wakeup_all(vcpu);
2004 return 0;
2005}
2006
Glauber Costa12f9a482011-02-01 14:16:40 -05002007static void kvmclock_reset(struct kvm_vcpu *vcpu)
2008{
Andy Honig0b794592013-02-20 14:48:10 -08002009 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05002010}
2011
Glauber Costac9aaa892011-07-11 15:28:14 -04002012static void record_steal_time(struct kvm_vcpu *vcpu)
2013{
2014 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2015 return;
2016
2017 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2018 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2019 return;
2020
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002021 if (vcpu->arch.st.steal.version & 1)
2022 vcpu->arch.st.steal.version += 1; /* first time write, random junk */
2023
2024 vcpu->arch.st.steal.version += 1;
2025
2026 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2027 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2028
2029 smp_wmb();
2030
Liang Chenc54cdf12016-03-16 19:33:16 +08002031 vcpu->arch.st.steal.steal += current->sched_info.run_delay -
2032 vcpu->arch.st.last_steal;
2033 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002034
2035 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2036 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2037
2038 smp_wmb();
2039
2040 vcpu->arch.st.steal.version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002041
2042 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
2043 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2044}
2045
Will Auld8fe8ab42012-11-29 12:42:12 -08002046int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002047{
Gleb Natapov57537852012-01-15 14:17:22 +02002048 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002049 u32 msr = msr_info->index;
2050 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002051
Carsten Otte15c4a642007-10-30 18:44:17 +01002052 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002053 case MSR_AMD64_NB_CFG:
2054 case MSR_IA32_UCODE_REV:
2055 case MSR_IA32_UCODE_WRITE:
2056 case MSR_VM_HSAVE_PA:
2057 case MSR_AMD64_PATCH_LOADER:
2058 case MSR_AMD64_BU_CFG2:
2059 break;
2060
Carsten Otte15c4a642007-10-30 18:44:17 +01002061 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02002062 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002063 case MSR_K7_HWCR:
2064 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002065 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002066 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02002067 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02002068 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002069 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2070 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002071 return 1;
2072 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002073 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002074 case MSR_FAM10H_MMIO_CONF_BASE:
2075 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002076 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2077 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002078 return 1;
2079 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002080 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002081 case MSR_IA32_DEBUGCTLMSR:
2082 if (!data) {
2083 /* We support the non-activated case already */
2084 break;
2085 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2086 /* Values other than LBR and BTF are vendor-specific,
2087 thus reserved and should throw a #GP */
2088 return 1;
2089 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002090 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2091 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002092 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002093 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002094 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002095 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002096 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002097 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2098 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002099 case MSR_IA32_TSCDEADLINE:
2100 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2101 break;
Will Auldba904632012-11-29 12:42:50 -08002102 case MSR_IA32_TSC_ADJUST:
2103 if (guest_cpuid_has_tsc_adjust(vcpu)) {
2104 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002105 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002106 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002107 }
2108 vcpu->arch.ia32_tsc_adjust_msr = data;
2109 }
2110 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002111 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002112 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01002113 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002114 case MSR_IA32_SMBASE:
2115 if (!msr_info->host_initiated)
2116 return 1;
2117 vcpu->arch.smbase = data;
2118 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002119 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002120 case MSR_KVM_WALL_CLOCK:
2121 vcpu->kvm->arch.wall_clock = data;
2122 kvm_write_wall_clock(vcpu->kvm, data);
2123 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002124 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002125 case MSR_KVM_SYSTEM_TIME: {
Andy Honig0b794592013-02-20 14:48:10 -08002126 u64 gpa_offset;
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002127 struct kvm_arch *ka = &vcpu->kvm->arch;
2128
Glauber Costa12f9a482011-02-01 14:16:40 -05002129 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002130
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002131 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2132 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2133
2134 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
2135 set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
2136 &vcpu->requests);
2137
2138 ka->boot_vcpu_runs_old_kvmclock = tmp;
2139 }
2140
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002141 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002142 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002143
2144 /* we verify if the enable bit is set... */
2145 if (!(data & 1))
2146 break;
2147
Andy Honig0b794592013-02-20 14:48:10 -08002148 gpa_offset = data & ~(PAGE_MASK | 1);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002149
Andy Honig0b794592013-02-20 14:48:10 -08002150 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002151 &vcpu->arch.pv_time, data & ~1ULL,
2152 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002153 vcpu->arch.pv_time_enabled = false;
2154 else
2155 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002156
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002157 break;
2158 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002159 case MSR_KVM_ASYNC_PF_EN:
2160 if (kvm_pv_enable_async_pf(vcpu, data))
2161 return 1;
2162 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002163 case MSR_KVM_STEAL_TIME:
2164
2165 if (unlikely(!sched_info_on()))
2166 return 1;
2167
2168 if (data & KVM_STEAL_RESERVED_MASK)
2169 return 1;
2170
2171 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002172 data & KVM_STEAL_VALID_BITS,
2173 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002174 return 1;
2175
2176 vcpu->arch.st.msr_val = data;
2177
2178 if (!(data & KVM_MSR_ENABLED))
2179 break;
2180
Glauber Costac9aaa892011-07-11 15:28:14 -04002181 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2182
2183 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002184 case MSR_KVM_PV_EOI_EN:
2185 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2186 return 1;
2187 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002188
Huang Ying890ca9a2009-05-11 16:48:15 +08002189 case MSR_IA32_MCG_CTL:
2190 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002191 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Huang Ying890ca9a2009-05-11 16:48:15 +08002192 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02002193
Wei Huang6912ac32015-06-12 01:34:56 -04002194 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2195 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2196 pr = true; /* fall through */
2197 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2198 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002199 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002200 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002201
2202 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002203 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2204 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002205 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002206 case MSR_K7_CLK_CTL:
2207 /*
2208 * Ignore all writes to this no longer documented MSR.
2209 * Writes are only relevant for old K7 processors,
2210 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002211 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002212 * affected processor models on the command line, hence
2213 * the need to ignore the workaround.
2214 */
2215 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002216 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002217 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2218 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002219 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002220 return kvm_hv_set_msr_common(vcpu, msr, data,
2221 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002222 case MSR_IA32_BBL_CR_CTL3:
2223 /* Drop writes to this legacy MSR -- see rdmsr
2224 * counterpart for further detail.
2225 */
Christoffer Dalla737f252012-06-03 21:17:48 +03002226 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002227 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002228 case MSR_AMD64_OSVW_ID_LENGTH:
2229 if (!guest_cpuid_has_osvw(vcpu))
2230 return 1;
2231 vcpu->arch.osvw.length = data;
2232 break;
2233 case MSR_AMD64_OSVW_STATUS:
2234 if (!guest_cpuid_has_osvw(vcpu))
2235 return 1;
2236 vcpu->arch.osvw.status = data;
2237 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002238 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002239 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2240 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002241 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002242 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002243 if (!ignore_msrs) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002244 vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
2245 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002246 return 1;
2247 } else {
Christoffer Dalla737f252012-06-03 21:17:48 +03002248 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
2249 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002250 break;
2251 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002252 }
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2256
2257
2258/*
2259 * Reads an msr value (of 'msr_index') into 'pdata'.
2260 * Returns 0 on success, non-0 otherwise.
2261 * Assumes vcpu_load() was already called.
2262 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002263int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01002264{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002265 return kvm_x86_ops->get_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002266}
Wincy Vanff651cb2014-12-11 08:52:58 +03002267EXPORT_SYMBOL_GPL(kvm_get_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002268
Huang Ying890ca9a2009-05-11 16:48:15 +08002269static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
2270{
2271 u64 data;
2272 u64 mcg_cap = vcpu->arch.mcg_cap;
2273 unsigned bank_num = mcg_cap & 0xff;
2274
2275 switch (msr) {
2276 case MSR_IA32_P5_MC_ADDR:
2277 case MSR_IA32_P5_MC_TYPE:
2278 data = 0;
2279 break;
2280 case MSR_IA32_MCG_CAP:
2281 data = vcpu->arch.mcg_cap;
2282 break;
2283 case MSR_IA32_MCG_CTL:
2284 if (!(mcg_cap & MCG_CTL_P))
2285 return 1;
2286 data = vcpu->arch.mcg_ctl;
2287 break;
2288 case MSR_IA32_MCG_STATUS:
2289 data = vcpu->arch.mcg_status;
2290 break;
2291 default:
2292 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002293 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002294 u32 offset = msr - MSR_IA32_MC0_CTL;
2295 data = vcpu->arch.mce_banks[offset];
2296 break;
2297 }
2298 return 1;
2299 }
2300 *pdata = data;
2301 return 0;
2302}
2303
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002304int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002305{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002306 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002307 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002308 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002309 case MSR_IA32_DEBUGCTLMSR:
2310 case MSR_IA32_LASTBRANCHFROMIP:
2311 case MSR_IA32_LASTBRANCHTOIP:
2312 case MSR_IA32_LASTINTFROMIP:
2313 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302314 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002315 case MSR_K8_TSEG_ADDR:
2316 case MSR_K8_TSEG_MASK:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302317 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002318 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002319 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002320 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002321 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002322 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03002323 case MSR_IA32_PERF_CTL:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002324 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002325 break;
Wei Huang6912ac32015-06-12 01:34:56 -04002326 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2327 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2328 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2329 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002330 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002331 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2332 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002333 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002334 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002335 msr_info->data = 0x100000000ULL;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002336 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002337 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002338 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002339 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002340 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002341 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002342 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002343 /*
2344 * MSR_EBC_FREQUENCY_ID
2345 * Conservative value valid for even the basic CPU models.
2346 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2347 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2348 * and 266MHz for model 3, or 4. Set Core Clock
2349 * Frequency to System Bus Frequency Ratio to 1 (bits
2350 * 31:24) even though these are only valid for CPU
2351 * models > 2, however guests may end up dividing or
2352 * multiplying by zero otherwise.
2353 */
2354 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002355 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002356 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002357 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002358 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002359 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002360 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002361 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002362 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002363 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002364 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002365 break;
Will Auldba904632012-11-29 12:42:50 -08002366 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002367 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002368 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002369 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002370 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002371 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002372 case MSR_IA32_SMBASE:
2373 if (!msr_info->host_initiated)
2374 return 1;
2375 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002376 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002377 case MSR_IA32_PERF_STATUS:
2378 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002379 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002380 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002381 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002382 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002383 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002384 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002385 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002386 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002387 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002388 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002389 break;
2390 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002391 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002392 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002393 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002394 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002395 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02002396 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002397 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002398 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04002399 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002400 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002401 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002402 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002403 case MSR_IA32_P5_MC_ADDR:
2404 case MSR_IA32_P5_MC_TYPE:
2405 case MSR_IA32_MCG_CAP:
2406 case MSR_IA32_MCG_CTL:
2407 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002408 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002409 return get_msr_mce(vcpu, msr_info->index, &msr_info->data);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002410 case MSR_K7_CLK_CTL:
2411 /*
2412 * Provide expected ramp-up count for K7. All other
2413 * are set to zero, indicating minimum divisors for
2414 * every field.
2415 *
2416 * This prevents guest kernels on AMD host with CPU
2417 * type 6, model 8 and higher from exploding due to
2418 * the rdmsr failing.
2419 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002420 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002421 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002422 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002423 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2424 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002425 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Andrey Smetanine83d5882015-07-03 15:01:34 +03002426 return kvm_hv_get_msr_common(vcpu,
2427 msr_info->index, &msr_info->data);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002428 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002429 case MSR_IA32_BBL_CR_CTL3:
2430 /* This legacy MSR exists but isn't fully documented in current
2431 * silicon. It is however accessed by winxp in very narrow
2432 * scenarios where it sets bit #19, itself documented as
2433 * a "reserved" bit. Best effort attempt to source coherent
2434 * read data here should the balance of the register be
2435 * interpreted by the guest:
2436 *
2437 * L2 cache control register 3: 64GB range, 256KB size,
2438 * enabled, latency 0x1, configured
2439 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002440 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05002441 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002442 case MSR_AMD64_OSVW_ID_LENGTH:
2443 if (!guest_cpuid_has_osvw(vcpu))
2444 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002445 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002446 break;
2447 case MSR_AMD64_OSVW_STATUS:
2448 if (!guest_cpuid_has_osvw(vcpu))
2449 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002450 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002451 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002452 default:
Wei Huangc6702c92015-06-19 13:44:45 +02002453 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002454 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002455 if (!ignore_msrs) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002456 vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02002457 return 1;
2458 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002459 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr_info->index);
2460 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02002461 }
2462 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002463 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002464 return 0;
2465}
2466EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2467
Carsten Otte313a3dc2007-10-11 19:16:52 +02002468/*
2469 * Read or write a bunch of msrs. All parameters are kernel addresses.
2470 *
2471 * @return number of msrs set successfully.
2472 */
2473static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2474 struct kvm_msr_entry *entries,
2475 int (*do_msr)(struct kvm_vcpu *vcpu,
2476 unsigned index, u64 *data))
2477{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002478 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002479
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002480 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002481 for (i = 0; i < msrs->nmsrs; ++i)
2482 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2483 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002484 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002485
Carsten Otte313a3dc2007-10-11 19:16:52 +02002486 return i;
2487}
2488
2489/*
2490 * Read or write a bunch of msrs. Parameters are user addresses.
2491 *
2492 * @return number of msrs set successfully.
2493 */
2494static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2495 int (*do_msr)(struct kvm_vcpu *vcpu,
2496 unsigned index, u64 *data),
2497 int writeback)
2498{
2499 struct kvm_msrs msrs;
2500 struct kvm_msr_entry *entries;
2501 int r, n;
2502 unsigned size;
2503
2504 r = -EFAULT;
2505 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2506 goto out;
2507
2508 r = -E2BIG;
2509 if (msrs.nmsrs >= MAX_IO_MSRS)
2510 goto out;
2511
Carsten Otte313a3dc2007-10-11 19:16:52 +02002512 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002513 entries = memdup_user(user_msrs->entries, size);
2514 if (IS_ERR(entries)) {
2515 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002516 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002517 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002518
2519 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2520 if (r < 0)
2521 goto out_free;
2522
2523 r = -EFAULT;
2524 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2525 goto out_free;
2526
2527 r = n;
2528
2529out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002530 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002531out:
2532 return r;
2533}
2534
Alexander Graf784aa3d2014-07-14 18:27:35 +02002535int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002536{
2537 int r;
2538
2539 switch (ext) {
2540 case KVM_CAP_IRQCHIP:
2541 case KVM_CAP_HLT:
2542 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002543 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002544 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002545 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002546 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002547 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002548 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002549 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002550 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002551 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002552 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002553 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002554 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002555 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002556 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002557 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002558 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002559 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002560 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002561 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002562 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002563 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002564 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002565 case KVM_CAP_HYPERV_SYNIC:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002566 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002567 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002568 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002569 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002570 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002571 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002572 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002573 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002574 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002575 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01002576 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03002577 case KVM_CAP_ENABLE_CAP_VM:
2578 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02002579 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07002580 case KVM_CAP_SPLIT_IRQCHIP:
Alex Williamson2a5bab12013-04-16 13:49:18 -06002581#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
2582 case KVM_CAP_ASSIGN_DEV_IRQ:
2583 case KVM_CAP_PCI_2_3:
2584#endif
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002585 r = 1;
2586 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02002587 case KVM_CAP_X86_SMM:
2588 /* SMBASE is usually relocated above 1M on modern chipsets,
2589 * and SMM handlers might indeed rely on 4G segment limits,
2590 * so do not report SMM to be available if real mode is
2591 * emulated via vm86 mode. Still, do not go to great lengths
2592 * to avoid userspace's usage of the feature, because it is a
2593 * fringe case that is not enabled except via specific settings
2594 * of the module parameters.
2595 */
2596 r = kvm_x86_ops->cpu_has_high_real_mode_segbase();
2597 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02002598 case KVM_CAP_COALESCED_MMIO:
2599 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
2600 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002601 case KVM_CAP_VAPIC:
2602 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2603 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002604 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002605 r = KVM_SOFT_MAX_VCPUS;
2606 break;
2607 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002608 r = KVM_MAX_VCPUS;
2609 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002610 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002611 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002612 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002613 case KVM_CAP_PV_MMU: /* obsolete */
2614 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002615 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002616#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002617 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +02002618 r = iommu_present(&pci_bus_type);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03002619 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -06002620#endif
Huang Ying890ca9a2009-05-11 16:48:15 +08002621 case KVM_CAP_MCE:
2622 r = KVM_MAX_MCE_BANKS;
2623 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002624 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02002625 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002626 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002627 case KVM_CAP_TSC_CONTROL:
2628 r = kvm_has_tsc_control;
2629 break;
Radim Krčmář371313132016-07-12 22:09:27 +02002630 case KVM_CAP_X2APIC_API:
2631 r = KVM_X2APIC_API_VALID_FLAGS;
2632 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002633 default:
2634 r = 0;
2635 break;
2636 }
2637 return r;
2638
2639}
2640
Carsten Otte043405e2007-10-10 17:16:19 +02002641long kvm_arch_dev_ioctl(struct file *filp,
2642 unsigned int ioctl, unsigned long arg)
2643{
2644 void __user *argp = (void __user *)arg;
2645 long r;
2646
2647 switch (ioctl) {
2648 case KVM_GET_MSR_INDEX_LIST: {
2649 struct kvm_msr_list __user *user_msr_list = argp;
2650 struct kvm_msr_list msr_list;
2651 unsigned n;
2652
2653 r = -EFAULT;
2654 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2655 goto out;
2656 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002657 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Carsten Otte043405e2007-10-10 17:16:19 +02002658 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2659 goto out;
2660 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002661 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02002662 goto out;
2663 r = -EFAULT;
2664 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2665 num_msrs_to_save * sizeof(u32)))
2666 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02002667 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02002668 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02002669 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02002670 goto out;
2671 r = 0;
2672 break;
2673 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002674 case KVM_GET_SUPPORTED_CPUID:
2675 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02002676 struct kvm_cpuid2 __user *cpuid_arg = argp;
2677 struct kvm_cpuid2 cpuid;
2678
2679 r = -EFAULT;
2680 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2681 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002682
2683 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
2684 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02002685 if (r)
2686 goto out;
2687
2688 r = -EFAULT;
2689 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2690 goto out;
2691 r = 0;
2692 break;
2693 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002694 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08002695 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002696 if (copy_to_user(argp, &kvm_mce_cap_supported,
2697 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08002698 goto out;
2699 r = 0;
2700 break;
2701 }
Carsten Otte043405e2007-10-10 17:16:19 +02002702 default:
2703 r = -EINVAL;
2704 }
2705out:
2706 return r;
2707}
2708
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002709static void wbinvd_ipi(void *garbage)
2710{
2711 wbinvd();
2712}
2713
2714static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
2715{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06002716 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002717}
2718
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002719static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
2720{
2721 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
2722}
2723
Carsten Otte313a3dc2007-10-11 19:16:52 +02002724void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2725{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08002726 /* Address WBINVD may be executed by guest */
2727 if (need_emulate_wbinvd(vcpu)) {
2728 if (kvm_x86_ops->has_wbinvd_exit())
2729 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
2730 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
2731 smp_call_function_single(vcpu->cpu,
2732 wbinvd_ipi, NULL, 1);
2733 }
2734
Carsten Otte313a3dc2007-10-11 19:16:52 +02002735 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002736
2737 /* Apply any externally detected TSC adjustments (due to suspend) */
2738 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
2739 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
2740 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002741 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02002742 }
2743
Zachary Amsden48434c202010-08-19 22:07:24 -10002744 if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02002745 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002746 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10002747 if (tsc_delta < 0)
2748 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07002749
2750 if (kvm_lapic_hv_timer_in_use(vcpu) &&
2751 kvm_x86_ops->set_hv_timer(vcpu,
2752 kvm_get_lapic_tscdeadline_msr(vcpu)))
2753 kvm_lapic_switch_to_sw_timer(vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002754 if (check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08002755 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002756 vcpu->arch.last_guest_tsc);
2757 kvm_x86_ops->write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002758 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10002759 }
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02002760 /*
2761 * On a host with synchronized TSC, there is no need to update
2762 * kvmclock on vcpu->cpu migration
2763 */
2764 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002765 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002766 if (vcpu->cpu != cpu)
2767 kvm_migrate_timers(vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002768 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10002769 }
Glauber Costac9aaa892011-07-11 15:28:14 -04002770
Glauber Costac9aaa892011-07-11 15:28:14 -04002771 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002772}
2773
2774void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2775{
Avi Kivity02daab22009-12-30 12:40:26 +02002776 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03002777 kvm_put_guest_fpu(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002778 vcpu->arch.last_host_tsc = rdtsc();
Carsten Otte313a3dc2007-10-11 19:16:52 +02002779}
2780
Carsten Otte313a3dc2007-10-11 19:16:52 +02002781static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2782 struct kvm_lapic_state *s)
2783{
Andrey Smetanind62caab2015-11-10 15:36:33 +03002784 if (vcpu->arch.apicv_active)
2785 kvm_x86_ops->sync_pir_to_irr(vcpu);
2786
Radim Krčmářa92e2542016-07-12 22:09:22 +02002787 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002788}
2789
2790static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2791 struct kvm_lapic_state *s)
2792{
Radim Krčmářa92e2542016-07-12 22:09:22 +02002793 int r;
2794
2795 r = kvm_apic_set_state(vcpu, s);
2796 if (r)
2797 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002798 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002799
2800 return 0;
2801}
2802
Matt Gingell127a4572015-11-17 17:32:05 +01002803static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
2804{
2805 return (!lapic_in_kernel(vcpu) ||
2806 kvm_apic_accept_pic_intr(vcpu));
2807}
2808
Matt Gingell782d4222015-11-16 15:26:00 -08002809/*
2810 * if userspace requested an interrupt window, check that the
2811 * interrupt window is open.
2812 *
2813 * No need to exit to userspace if we already have an interrupt queued.
2814 */
2815static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
2816{
2817 return kvm_arch_interrupt_allowed(vcpu) &&
2818 !kvm_cpu_has_interrupt(vcpu) &&
2819 !kvm_event_needs_reinjection(vcpu) &&
2820 kvm_cpu_accept_dm_intr(vcpu);
2821}
2822
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002823static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2824 struct kvm_interrupt *irq)
2825{
Chen Gang02cdb502013-02-27 11:33:25 +08002826 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002827 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002828
2829 if (!irqchip_in_kernel(vcpu->kvm)) {
2830 kvm_queue_interrupt(vcpu, irq->irq, false);
2831 kvm_make_request(KVM_REQ_EVENT, vcpu);
2832 return 0;
2833 }
2834
2835 /*
2836 * With in-kernel LAPIC, we only use this to inject EXTINT, so
2837 * fail for in-kernel 8259.
2838 */
2839 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002840 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002841
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002842 if (vcpu->arch.pending_external_vector != -1)
2843 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002844
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02002845 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08002846 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002847 return 0;
2848}
2849
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002850static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2851{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002852 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002853
2854 return 0;
2855}
2856
Paolo Bonzinif0778252015-04-01 15:06:40 +02002857static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
2858{
Paolo Bonzini64d60672015-05-07 11:36:11 +02002859 kvm_make_request(KVM_REQ_SMI, vcpu);
2860
Paolo Bonzinif0778252015-04-01 15:06:40 +02002861 return 0;
2862}
2863
Avi Kivityb209749f2007-10-22 16:50:39 +02002864static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2865 struct kvm_tpr_access_ctl *tac)
2866{
2867 if (tac->flags)
2868 return -EINVAL;
2869 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2870 return 0;
2871}
2872
Huang Ying890ca9a2009-05-11 16:48:15 +08002873static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2874 u64 mcg_cap)
2875{
2876 int r;
2877 unsigned bank_num = mcg_cap & 0xff, bank;
2878
2879 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002880 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002881 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002882 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08002883 goto out;
2884 r = 0;
2885 vcpu->arch.mcg_cap = mcg_cap;
2886 /* Init IA32_MCG_CTL to all 1s */
2887 if (mcg_cap & MCG_CTL_P)
2888 vcpu->arch.mcg_ctl = ~(u64)0;
2889 /* Init IA32_MCi_CTL to all 1s */
2890 for (bank = 0; bank < bank_num; bank++)
2891 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002892
2893 if (kvm_x86_ops->setup_mce)
2894 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002895out:
2896 return r;
2897}
2898
2899static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2900 struct kvm_x86_mce *mce)
2901{
2902 u64 mcg_cap = vcpu->arch.mcg_cap;
2903 unsigned bank_num = mcg_cap & 0xff;
2904 u64 *banks = vcpu->arch.mce_banks;
2905
2906 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2907 return -EINVAL;
2908 /*
2909 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2910 * reporting is disabled
2911 */
2912 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2913 vcpu->arch.mcg_ctl != ~(u64)0)
2914 return 0;
2915 banks += 4 * mce->bank;
2916 /*
2917 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2918 * reporting is disabled for the bank
2919 */
2920 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2921 return 0;
2922 if (mce->status & MCI_STATUS_UC) {
2923 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002924 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002925 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002926 return 0;
2927 }
2928 if (banks[1] & MCI_STATUS_VAL)
2929 mce->status |= MCI_STATUS_OVER;
2930 banks[2] = mce->addr;
2931 banks[3] = mce->misc;
2932 vcpu->arch.mcg_status = mce->mcg_status;
2933 banks[1] = mce->status;
2934 kvm_queue_exception(vcpu, MC_VECTOR);
2935 } else if (!(banks[1] & MCI_STATUS_VAL)
2936 || !(banks[1] & MCI_STATUS_UC)) {
2937 if (banks[1] & MCI_STATUS_VAL)
2938 mce->status |= MCI_STATUS_OVER;
2939 banks[2] = mce->addr;
2940 banks[3] = mce->misc;
2941 banks[1] = mce->status;
2942 } else
2943 banks[1] |= MCI_STATUS_OVER;
2944 return 0;
2945}
2946
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002947static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2948 struct kvm_vcpu_events *events)
2949{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002950 process_nmi(vcpu);
Jan Kiszka03b82a32010-02-15 10:45:41 +01002951 events->exception.injected =
2952 vcpu->arch.exception.pending &&
2953 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002954 events->exception.nr = vcpu->arch.exception.nr;
2955 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002956 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002957 events->exception.error_code = vcpu->arch.exception.error_code;
2958
Jan Kiszka03b82a32010-02-15 10:45:41 +01002959 events->interrupt.injected =
2960 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002961 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002962 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002963 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002964
2965 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002966 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002967 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002968 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002969
Jan Kiszka66450a22013-03-13 12:42:34 +01002970 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002971
Paolo Bonzinif0778252015-04-01 15:06:40 +02002972 events->smi.smm = is_smm(vcpu);
2973 events->smi.pending = vcpu->arch.smi_pending;
2974 events->smi.smm_inside_nmi =
2975 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
2976 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
2977
Jan Kiszkadab4b912009-12-06 18:24:15 +01002978 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02002979 | KVM_VCPUEVENT_VALID_SHADOW
2980 | KVM_VCPUEVENT_VALID_SMM);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04002981 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002982}
2983
2984static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2985 struct kvm_vcpu_events *events)
2986{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002987 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002988 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02002989 | KVM_VCPUEVENT_VALID_SHADOW
2990 | KVM_VCPUEVENT_VALID_SMM))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002991 return -EINVAL;
2992
Paolo Bonzini78e546c2016-06-01 14:09:20 +02002993 if (events->exception.injected &&
2994 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
2995 return -EINVAL;
2996
Avi Kivity7460fb4a2011-09-20 13:43:14 +03002997 process_nmi(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002998 vcpu->arch.exception.pending = events->exception.injected;
2999 vcpu->arch.exception.nr = events->exception.nr;
3000 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3001 vcpu->arch.exception.error_code = events->exception.error_code;
3002
3003 vcpu->arch.interrupt.pending = events->interrupt.injected;
3004 vcpu->arch.interrupt.nr = events->interrupt.nr;
3005 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003006 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3007 kvm_x86_ops->set_interrupt_shadow(vcpu,
3008 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003009
3010 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003011 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3012 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003013 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3014
Jan Kiszka66450a22013-03-13 12:42:34 +01003015 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003016 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003017 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003018
Paolo Bonzinif0778252015-04-01 15:06:40 +02003019 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
3020 if (events->smi.smm)
3021 vcpu->arch.hflags |= HF_SMM_MASK;
3022 else
3023 vcpu->arch.hflags &= ~HF_SMM_MASK;
3024 vcpu->arch.smi_pending = events->smi.pending;
3025 if (events->smi.smm_inside_nmi)
3026 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
3027 else
3028 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003029 if (lapic_in_kernel(vcpu)) {
Paolo Bonzinif0778252015-04-01 15:06:40 +02003030 if (events->smi.latched_init)
3031 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3032 else
3033 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3034 }
3035 }
3036
Avi Kivity3842d132010-07-27 12:30:24 +03003037 kvm_make_request(KVM_REQ_EVENT, vcpu);
3038
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003039 return 0;
3040}
3041
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003042static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3043 struct kvm_debugregs *dbgregs)
3044{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003045 unsigned long val;
3046
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003047 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003048 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003049 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003050 dbgregs->dr7 = vcpu->arch.dr7;
3051 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003052 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003053}
3054
3055static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3056 struct kvm_debugregs *dbgregs)
3057{
3058 if (dbgregs->flags)
3059 return -EINVAL;
3060
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003061 if (dbgregs->dr6 & ~0xffffffffull)
3062 return -EINVAL;
3063 if (dbgregs->dr7 & ~0xffffffffull)
3064 return -EINVAL;
3065
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003066 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003067 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003068 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003069 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003070 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003071 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003072
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003073 return 0;
3074}
3075
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003076#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3077
3078static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3079{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003080 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003081 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003082 u64 valid;
3083
3084 /*
3085 * Copy legacy XSAVE area, to avoid complications with CPUID
3086 * leaves 0 and 1 in the loop below.
3087 */
3088 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3089
3090 /* Set XSTATE_BV */
3091 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3092
3093 /*
3094 * Copy each region from the possibly compacted offset to the
3095 * non-compacted offset.
3096 */
Dave Hansend91cab72015-09-02 16:31:26 -07003097 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003098 while (valid) {
3099 u64 feature = valid & -valid;
3100 int index = fls64(feature) - 1;
3101 void *src = get_xsave_addr(xsave, feature);
3102
3103 if (src) {
3104 u32 size, offset, ecx, edx;
3105 cpuid_count(XSTATE_CPUID, index,
3106 &size, &offset, &ecx, &edx);
3107 memcpy(dest + offset, src, size);
3108 }
3109
3110 valid -= feature;
3111 }
3112}
3113
3114static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3115{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003116 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003117 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3118 u64 valid;
3119
3120 /*
3121 * Copy legacy XSAVE area, to avoid complications with CPUID
3122 * leaves 0 and 1 in the loop below.
3123 */
3124 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3125
3126 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003127 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02003128 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02003129 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003130
3131 /*
3132 * Copy each region from the non-compacted offset to the
3133 * possibly compacted offset.
3134 */
Dave Hansend91cab72015-09-02 16:31:26 -07003135 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003136 while (valid) {
3137 u64 feature = valid & -valid;
3138 int index = fls64(feature) - 1;
3139 void *dest = get_xsave_addr(xsave, feature);
3140
3141 if (dest) {
3142 u32 size, offset, ecx, edx;
3143 cpuid_count(XSTATE_CPUID, index,
3144 &size, &offset, &ecx, &edx);
3145 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08003146 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003147
3148 valid -= feature;
3149 }
3150}
3151
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003152static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3153 struct kvm_xsave *guest_xsave)
3154{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003155 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003156 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3157 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003158 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003159 memcpy(guest_xsave->region,
Ingo Molnar7366ed72015-04-27 04:19:39 +02003160 &vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003161 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003162 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07003163 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003164 }
3165}
3166
3167static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3168 struct kvm_xsave *guest_xsave)
3169{
3170 u64 xstate_bv =
3171 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
3172
Borislav Petkovd366bf72016-04-04 22:25:02 +02003173 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003174 /*
3175 * Here we allow setting states that are not present in
3176 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3177 * with old userspace.
3178 */
Paolo Bonzini4ff41732014-02-24 12:15:16 +01003179 if (xstate_bv & ~kvm_supported_xcr0())
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003180 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003181 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003182 } else {
Dave Hansend91cab72015-09-02 16:31:26 -07003183 if (xstate_bv & ~XFEATURE_MASK_FPSSE)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003184 return -EINVAL;
Ingo Molnar7366ed72015-04-27 04:19:39 +02003185 memcpy(&vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003186 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003187 }
3188 return 0;
3189}
3190
3191static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3192 struct kvm_xcrs *guest_xcrs)
3193{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003194 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003195 guest_xcrs->nr_xcrs = 0;
3196 return;
3197 }
3198
3199 guest_xcrs->nr_xcrs = 1;
3200 guest_xcrs->flags = 0;
3201 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3202 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3203}
3204
3205static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3206 struct kvm_xcrs *guest_xcrs)
3207{
3208 int i, r = 0;
3209
Borislav Petkovd366bf72016-04-04 22:25:02 +02003210 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003211 return -EINVAL;
3212
3213 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3214 return -EINVAL;
3215
3216 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3217 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003218 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003219 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003220 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003221 break;
3222 }
3223 if (r)
3224 r = -EINVAL;
3225 return r;
3226}
3227
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003228/*
3229 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3230 * stopped by the hypervisor. This function will be called from the host only.
3231 * EINVAL is returned when the host attempts to set the flag for a guest that
3232 * does not support pv clocks.
3233 */
3234static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3235{
Andy Honig0b794592013-02-20 14:48:10 -08003236 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003237 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003238 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003239 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3240 return 0;
3241}
3242
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003243static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3244 struct kvm_enable_cap *cap)
3245{
3246 if (cap->flags)
3247 return -EINVAL;
3248
3249 switch (cap->cap) {
3250 case KVM_CAP_HYPERV_SYNIC:
3251 return kvm_hv_activate_synic(vcpu);
3252 default:
3253 return -EINVAL;
3254 }
3255}
3256
Carsten Otte313a3dc2007-10-11 19:16:52 +02003257long kvm_arch_vcpu_ioctl(struct file *filp,
3258 unsigned int ioctl, unsigned long arg)
3259{
3260 struct kvm_vcpu *vcpu = filp->private_data;
3261 void __user *argp = (void __user *)arg;
3262 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003263 union {
3264 struct kvm_lapic_state *lapic;
3265 struct kvm_xsave *xsave;
3266 struct kvm_xcrs *xcrs;
3267 void *buffer;
3268 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003269
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003270 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003271 switch (ioctl) {
3272 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003273 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003274 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003275 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003276 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003277
Dave Hansenb772ff32008-08-11 10:01:47 -07003278 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003279 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003280 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003281 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003282 if (r)
3283 goto out;
3284 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003285 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003286 goto out;
3287 r = 0;
3288 break;
3289 }
3290 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003291 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003292 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003293 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003294 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Guo Chao18595412012-11-02 18:33:21 +08003295 if (IS_ERR(u.lapic))
3296 return PTR_ERR(u.lapic);
Sasha Levinff5c2c02011-12-04 19:36:29 +02003297
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003298 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003299 break;
3300 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003301 case KVM_INTERRUPT: {
3302 struct kvm_interrupt irq;
3303
3304 r = -EFAULT;
3305 if (copy_from_user(&irq, argp, sizeof irq))
3306 goto out;
3307 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003308 break;
3309 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003310 case KVM_NMI: {
3311 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003312 break;
3313 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003314 case KVM_SMI: {
3315 r = kvm_vcpu_ioctl_smi(vcpu);
3316 break;
3317 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003318 case KVM_SET_CPUID: {
3319 struct kvm_cpuid __user *cpuid_arg = argp;
3320 struct kvm_cpuid cpuid;
3321
3322 r = -EFAULT;
3323 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3324 goto out;
3325 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003326 break;
3327 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003328 case KVM_SET_CPUID2: {
3329 struct kvm_cpuid2 __user *cpuid_arg = argp;
3330 struct kvm_cpuid2 cpuid;
3331
3332 r = -EFAULT;
3333 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3334 goto out;
3335 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003336 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003337 break;
3338 }
3339 case KVM_GET_CPUID2: {
3340 struct kvm_cpuid2 __user *cpuid_arg = argp;
3341 struct kvm_cpuid2 cpuid;
3342
3343 r = -EFAULT;
3344 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3345 goto out;
3346 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003347 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003348 if (r)
3349 goto out;
3350 r = -EFAULT;
3351 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3352 goto out;
3353 r = 0;
3354 break;
3355 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003356 case KVM_GET_MSRS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003357 r = msr_io(vcpu, argp, do_get_msr, 1);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003358 break;
3359 case KVM_SET_MSRS:
3360 r = msr_io(vcpu, argp, do_set_msr, 0);
3361 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02003362 case KVM_TPR_ACCESS_REPORTING: {
3363 struct kvm_tpr_access_ctl tac;
3364
3365 r = -EFAULT;
3366 if (copy_from_user(&tac, argp, sizeof tac))
3367 goto out;
3368 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3369 if (r)
3370 goto out;
3371 r = -EFAULT;
3372 if (copy_to_user(argp, &tac, sizeof tac))
3373 goto out;
3374 r = 0;
3375 break;
3376 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003377 case KVM_SET_VAPIC_ADDR: {
3378 struct kvm_vapic_addr va;
3379
3380 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02003381 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02003382 goto out;
3383 r = -EFAULT;
3384 if (copy_from_user(&va, argp, sizeof va))
3385 goto out;
Andy Honigfda4e2e2013-11-20 10:23:22 -08003386 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Avi Kivityb93463a2007-10-25 16:52:32 +02003387 break;
3388 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003389 case KVM_X86_SETUP_MCE: {
3390 u64 mcg_cap;
3391
3392 r = -EFAULT;
3393 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3394 goto out;
3395 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3396 break;
3397 }
3398 case KVM_X86_SET_MCE: {
3399 struct kvm_x86_mce mce;
3400
3401 r = -EFAULT;
3402 if (copy_from_user(&mce, argp, sizeof mce))
3403 goto out;
3404 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3405 break;
3406 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003407 case KVM_GET_VCPU_EVENTS: {
3408 struct kvm_vcpu_events events;
3409
3410 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3411
3412 r = -EFAULT;
3413 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3414 break;
3415 r = 0;
3416 break;
3417 }
3418 case KVM_SET_VCPU_EVENTS: {
3419 struct kvm_vcpu_events events;
3420
3421 r = -EFAULT;
3422 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3423 break;
3424
3425 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3426 break;
3427 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003428 case KVM_GET_DEBUGREGS: {
3429 struct kvm_debugregs dbgregs;
3430
3431 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3432
3433 r = -EFAULT;
3434 if (copy_to_user(argp, &dbgregs,
3435 sizeof(struct kvm_debugregs)))
3436 break;
3437 r = 0;
3438 break;
3439 }
3440 case KVM_SET_DEBUGREGS: {
3441 struct kvm_debugregs dbgregs;
3442
3443 r = -EFAULT;
3444 if (copy_from_user(&dbgregs, argp,
3445 sizeof(struct kvm_debugregs)))
3446 break;
3447
3448 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3449 break;
3450 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003451 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003452 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003453 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003454 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003455 break;
3456
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003457 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003458
3459 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003460 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003461 break;
3462 r = 0;
3463 break;
3464 }
3465 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003466 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Guo Chao18595412012-11-02 18:33:21 +08003467 if (IS_ERR(u.xsave))
3468 return PTR_ERR(u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003469
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003470 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003471 break;
3472 }
3473 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003474 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003475 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003476 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003477 break;
3478
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003479 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003480
3481 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003482 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003483 sizeof(struct kvm_xcrs)))
3484 break;
3485 r = 0;
3486 break;
3487 }
3488 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003489 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Guo Chao18595412012-11-02 18:33:21 +08003490 if (IS_ERR(u.xcrs))
3491 return PTR_ERR(u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003492
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003493 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003494 break;
3495 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003496 case KVM_SET_TSC_KHZ: {
3497 u32 user_tsc_khz;
3498
3499 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003500 user_tsc_khz = (u32)arg;
3501
3502 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3503 goto out;
3504
Zachary Amsdencc578282012-02-03 15:43:50 -02003505 if (user_tsc_khz == 0)
3506 user_tsc_khz = tsc_khz;
3507
Haozhong Zhang381d5852015-10-20 15:39:04 +08003508 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
3509 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003510
Joerg Roedel92a1f122011-03-25 09:44:51 +01003511 goto out;
3512 }
3513 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003514 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003515 goto out;
3516 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003517 case KVM_KVMCLOCK_CTRL: {
3518 r = kvm_set_guest_paused(vcpu);
3519 goto out;
3520 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003521 case KVM_ENABLE_CAP: {
3522 struct kvm_enable_cap cap;
3523
3524 r = -EFAULT;
3525 if (copy_from_user(&cap, argp, sizeof(cap)))
3526 goto out;
3527 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3528 break;
3529 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003530 default:
3531 r = -EINVAL;
3532 }
3533out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003534 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003535 return r;
3536}
3537
Carsten Otte5b1c1492012-01-04 10:25:23 +01003538int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
3539{
3540 return VM_FAULT_SIGBUS;
3541}
3542
Carsten Otte1fe779f2007-10-29 16:08:35 +01003543static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
3544{
3545 int ret;
3546
3547 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08003548 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003549 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
3550 return ret;
3551}
3552
Sheng Yangb927a3c2009-07-21 10:42:48 +08003553static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
3554 u64 ident_addr)
3555{
3556 kvm->arch.ept_identity_map_addr = ident_addr;
3557 return 0;
3558}
3559
Carsten Otte1fe779f2007-10-29 16:08:35 +01003560static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
3561 u32 kvm_nr_mmu_pages)
3562{
3563 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
3564 return -EINVAL;
3565
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003566 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003567
3568 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003569 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003570
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003571 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003572 return 0;
3573}
3574
3575static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
3576{
Dave Hansen39de71e2010-08-19 18:11:14 -07003577 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003578}
3579
Carsten Otte1fe779f2007-10-29 16:08:35 +01003580static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3581{
3582 int r;
3583
3584 r = 0;
3585 switch (chip->chip_id) {
3586 case KVM_IRQCHIP_PIC_MASTER:
3587 memcpy(&chip->chip.pic,
3588 &pic_irqchip(kvm)->pics[0],
3589 sizeof(struct kvm_pic_state));
3590 break;
3591 case KVM_IRQCHIP_PIC_SLAVE:
3592 memcpy(&chip->chip.pic,
3593 &pic_irqchip(kvm)->pics[1],
3594 sizeof(struct kvm_pic_state));
3595 break;
3596 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003597 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003598 break;
3599 default:
3600 r = -EINVAL;
3601 break;
3602 }
3603 return r;
3604}
3605
3606static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
3607{
3608 int r;
3609
3610 r = 0;
3611 switch (chip->chip_id) {
3612 case KVM_IRQCHIP_PIC_MASTER:
Avi Kivityf4f51052010-09-19 18:44:07 +02003613 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003614 memcpy(&pic_irqchip(kvm)->pics[0],
3615 &chip->chip.pic,
3616 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003617 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003618 break;
3619 case KVM_IRQCHIP_PIC_SLAVE:
Avi Kivityf4f51052010-09-19 18:44:07 +02003620 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003621 memcpy(&pic_irqchip(kvm)->pics[1],
3622 &chip->chip.pic,
3623 sizeof(struct kvm_pic_state));
Avi Kivityf4f51052010-09-19 18:44:07 +02003624 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003625 break;
3626 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03003627 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003628 break;
3629 default:
3630 r = -EINVAL;
3631 break;
3632 }
3633 kvm_pic_update_irq(pic_irqchip(kvm));
3634 return r;
3635}
3636
Sheng Yange0f63cb2008-03-04 00:50:59 +08003637static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3638{
Radim Krčmář34f39412016-03-02 22:56:50 +01003639 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
3640
3641 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
3642
3643 mutex_lock(&kps->lock);
3644 memcpy(ps, &kps->channels, sizeof(*ps));
3645 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303646 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08003647}
3648
3649static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
3650{
Andrew Honig01856042015-11-18 14:50:23 -08003651 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01003652 struct kvm_pit *pit = kvm->arch.vpit;
3653
3654 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01003655 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08003656 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01003657 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
3658 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303659 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04003660}
3661
3662static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3663{
Beth Kone9f42752009-07-07 11:50:38 -04003664 mutex_lock(&kvm->arch.vpit->pit_state.lock);
3665 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
3666 sizeof(ps->channels));
3667 ps->flags = kvm->arch.vpit->pit_state.flags;
3668 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003669 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303670 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04003671}
3672
3673static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
3674{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303675 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08003676 int i;
Beth Kone9f42752009-07-07 11:50:38 -04003677 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01003678 struct kvm_pit *pit = kvm->arch.vpit;
3679
3680 mutex_lock(&pit->pit_state.lock);
3681 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04003682 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
3683 if (!prev_legacy && cur_legacy)
3684 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01003685 memcpy(&pit->pit_state.channels, &ps->channels,
3686 sizeof(pit->pit_state.channels));
3687 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08003688 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01003689 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01003690 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01003691 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05303692 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08003693}
3694
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003695static int kvm_vm_ioctl_reinject(struct kvm *kvm,
3696 struct kvm_reinject_control *control)
3697{
Radim Krčmář71474e22016-03-02 22:56:45 +01003698 struct kvm_pit *pit = kvm->arch.vpit;
3699
3700 if (!pit)
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003701 return -ENXIO;
Radim Krčmářb39c90b2016-03-02 22:56:44 +01003702
Radim Krčmář71474e22016-03-02 22:56:45 +01003703 /* pit->pit_state.lock was overloaded to prevent userspace from getting
3704 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
3705 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
3706 */
3707 mutex_lock(&pit->pit_state.lock);
3708 kvm_pit_set_reinject(pit, control->pit_reinject);
3709 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01003710
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003711 return 0;
3712}
3713
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003714/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003715 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
3716 * @kvm: kvm instance
3717 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003718 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003719 * Steps 1-4 below provide general overview of dirty page logging. See
3720 * kvm_get_dirty_log_protect() function description for additional details.
3721 *
3722 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
3723 * always flush the TLB (step 4) even if previous step failed and the dirty
3724 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
3725 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
3726 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003727 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003728 * 1. Take a snapshot of the bit and clear it if needed.
3729 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003730 * 3. Copy the snapshot to the userspace.
3731 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09003732 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003733int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003734{
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09003735 bool is_dirty = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003736 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003737
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003738 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003739
Kai Huang88178fd2015-01-28 10:54:27 +08003740 /*
3741 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
3742 */
3743 if (kvm_x86_ops->flush_log_dirty)
3744 kvm_x86_ops->flush_log_dirty(kvm);
3745
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003746 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003747
3748 /*
3749 * All the TLBs can be flushed out of mmu lock, see the comments in
3750 * kvm_mmu_slot_remove_write_access().
3751 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08003752 lockdep_assert_held(&kvm->slots_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08003753 if (is_dirty)
3754 kvm_flush_remote_tlbs(kvm);
3755
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003756 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08003757 return r;
3758}
3759
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003760int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
3761 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003762{
3763 if (!irqchip_in_kernel(kvm))
3764 return -ENXIO;
3765
3766 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003767 irq_event->irq, irq_event->level,
3768 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003769 return 0;
3770}
3771
Nadav Amit90de4a12015-04-13 01:53:41 +03003772static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3773 struct kvm_enable_cap *cap)
3774{
3775 int r;
3776
3777 if (cap->flags)
3778 return -EINVAL;
3779
3780 switch (cap->cap) {
3781 case KVM_CAP_DISABLE_QUIRKS:
3782 kvm->arch.disabled_quirks = cap->args[0];
3783 r = 0;
3784 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07003785 case KVM_CAP_SPLIT_IRQCHIP: {
3786 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07003787 r = -EINVAL;
3788 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
3789 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07003790 r = -EEXIST;
3791 if (irqchip_in_kernel(kvm))
3792 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02003793 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07003794 goto split_irqchip_unlock;
3795 r = kvm_setup_empty_irq_routing(kvm);
3796 if (r)
3797 goto split_irqchip_unlock;
3798 /* Pairs with irqchip_in_kernel. */
3799 smp_wmb();
3800 kvm->arch.irqchip_split = true;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07003801 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07003802 r = 0;
3803split_irqchip_unlock:
3804 mutex_unlock(&kvm->lock);
3805 break;
3806 }
Radim Krčmář371313132016-07-12 22:09:27 +02003807 case KVM_CAP_X2APIC_API:
3808 r = -EINVAL;
3809 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
3810 break;
3811
3812 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
3813 kvm->arch.x2apic_format = true;
3814
3815 r = 0;
3816 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03003817 default:
3818 r = -EINVAL;
3819 break;
3820 }
3821 return r;
3822}
3823
Carsten Otte1fe779f2007-10-29 16:08:35 +01003824long kvm_arch_vm_ioctl(struct file *filp,
3825 unsigned int ioctl, unsigned long arg)
3826{
3827 struct kvm *kvm = filp->private_data;
3828 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03003829 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07003830 /*
3831 * This union makes it completely explicit to gcc-3.x
3832 * that these two variables' stack usage should be
3833 * combined, not added together.
3834 */
3835 union {
3836 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04003837 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003838 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07003839 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003840
3841 switch (ioctl) {
3842 case KVM_SET_TSS_ADDR:
3843 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003844 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003845 case KVM_SET_IDENTITY_MAP_ADDR: {
3846 u64 ident_addr;
3847
3848 r = -EFAULT;
3849 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
3850 goto out;
3851 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08003852 break;
3853 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003854 case KVM_SET_NR_MMU_PAGES:
3855 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003856 break;
3857 case KVM_GET_NR_MMU_PAGES:
3858 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3859 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003860 case KVM_CREATE_IRQCHIP: {
3861 struct kvm_pic *vpic;
3862
3863 mutex_lock(&kvm->lock);
3864 r = -EEXIST;
3865 if (kvm->arch.vpic)
3866 goto create_irqchip_unlock;
Avi Kivity3e515702012-03-05 14:23:29 +02003867 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02003868 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02003869 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003870 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003871 vpic = kvm_create_pic(kvm);
3872 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003873 r = kvm_ioapic_init(kvm);
3874 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003875 mutex_lock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003876 kvm_destroy_pic(vpic);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003877 mutex_unlock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003878 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003879 }
3880 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003881 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02003882 r = kvm_setup_default_irq_routing(kvm);
3883 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003884 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003885 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003886 kvm_ioapic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003887 kvm_destroy_pic(vpic);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003888 mutex_unlock(&kvm->irq_lock);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +09003889 mutex_unlock(&kvm->slots_lock);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003890 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02003891 }
Paolo Bonzini71ba9942015-07-29 12:31:15 +02003892 /* Write kvm->irq_routing before kvm->arch.vpic. */
3893 smp_wmb();
3894 kvm->arch.vpic = vpic;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003895 create_irqchip_unlock:
3896 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003897 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003898 }
Sheng Yang78376992008-01-28 05:10:22 +08003899 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003900 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3901 goto create_pit;
3902 case KVM_CREATE_PIT2:
3903 r = -EFAULT;
3904 if (copy_from_user(&u.pit_config, argp,
3905 sizeof(struct kvm_pit_config)))
3906 goto out;
3907 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02003908 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003909 r = -EEXIST;
3910 if (kvm->arch.vpit)
3911 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003912 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003913 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003914 if (kvm->arch.vpit)
3915 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003916 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02003917 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08003918 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003919 case KVM_GET_IRQCHIP: {
3920 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003921 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003922
Sasha Levinff5c2c02011-12-04 19:36:29 +02003923 chip = memdup_user(argp, sizeof(*chip));
3924 if (IS_ERR(chip)) {
3925 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003926 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003927 }
3928
Carsten Otte1fe779f2007-10-29 16:08:35 +01003929 r = -ENXIO;
Steve Rutherford49df6392015-07-29 23:21:40 -07003930 if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003931 goto get_irqchip_out;
3932 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3933 if (r)
3934 goto get_irqchip_out;
3935 r = -EFAULT;
3936 if (copy_to_user(argp, chip, sizeof *chip))
3937 goto get_irqchip_out;
3938 r = 0;
3939 get_irqchip_out:
3940 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003941 break;
3942 }
3943 case KVM_SET_IRQCHIP: {
3944 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02003945 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003946
Sasha Levinff5c2c02011-12-04 19:36:29 +02003947 chip = memdup_user(argp, sizeof(*chip));
3948 if (IS_ERR(chip)) {
3949 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003950 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003951 }
3952
Carsten Otte1fe779f2007-10-29 16:08:35 +01003953 r = -ENXIO;
Steve Rutherford49df6392015-07-29 23:21:40 -07003954 if (!irqchip_in_kernel(kvm) || irqchip_split(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003955 goto set_irqchip_out;
3956 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3957 if (r)
3958 goto set_irqchip_out;
3959 r = 0;
3960 set_irqchip_out:
3961 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003962 break;
3963 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003964 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003965 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003966 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003967 goto out;
3968 r = -ENXIO;
3969 if (!kvm->arch.vpit)
3970 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003971 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003972 if (r)
3973 goto out;
3974 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003975 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003976 goto out;
3977 r = 0;
3978 break;
3979 }
3980 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003981 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003982 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003983 goto out;
3984 r = -ENXIO;
3985 if (!kvm->arch.vpit)
3986 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003987 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003988 break;
3989 }
Beth Kone9f42752009-07-07 11:50:38 -04003990 case KVM_GET_PIT2: {
3991 r = -ENXIO;
3992 if (!kvm->arch.vpit)
3993 goto out;
3994 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3995 if (r)
3996 goto out;
3997 r = -EFAULT;
3998 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3999 goto out;
4000 r = 0;
4001 break;
4002 }
4003 case KVM_SET_PIT2: {
4004 r = -EFAULT;
4005 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
4006 goto out;
4007 r = -ENXIO;
4008 if (!kvm->arch.vpit)
4009 goto out;
4010 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04004011 break;
4012 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004013 case KVM_REINJECT_CONTROL: {
4014 struct kvm_reinject_control control;
4015 r = -EFAULT;
4016 if (copy_from_user(&control, argp, sizeof(control)))
4017 goto out;
4018 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004019 break;
4020 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004021 case KVM_SET_BOOT_CPU_ID:
4022 r = 0;
4023 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02004024 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004025 r = -EBUSY;
4026 else
4027 kvm->arch.bsp_vcpu_id = arg;
4028 mutex_unlock(&kvm->lock);
4029 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07004030 case KVM_XEN_HVM_CONFIG: {
4031 r = -EFAULT;
4032 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
4033 sizeof(struct kvm_xen_hvm_config)))
4034 goto out;
4035 r = -EINVAL;
4036 if (kvm->arch.xen_hvm_config.flags)
4037 goto out;
4038 r = 0;
4039 break;
4040 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004041 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004042 struct kvm_clock_data user_ns;
4043 u64 now_ns;
4044 s64 delta;
4045
4046 r = -EFAULT;
4047 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
4048 goto out;
4049
4050 r = -EINVAL;
4051 if (user_ns.flags)
4052 goto out;
4053
4054 r = 0;
Avi Kivity395c6b02010-10-04 12:55:49 +02004055 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10004056 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004057 delta = user_ns.clock - now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02004058 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004059 kvm->arch.kvmclock_offset = delta;
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03004060 kvm_gen_update_masterclock(kvm);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004061 break;
4062 }
4063 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004064 struct kvm_clock_data user_ns;
4065 u64 now_ns;
4066
Avi Kivity395c6b02010-10-04 12:55:49 +02004067 local_irq_disable();
Zachary Amsden759379d2010-08-19 22:07:25 -10004068 now_ns = get_kernel_ns();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004069 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
Avi Kivity395c6b02010-10-04 12:55:49 +02004070 local_irq_enable();
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004071 user_ns.flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004072 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004073
4074 r = -EFAULT;
4075 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
4076 goto out;
4077 r = 0;
4078 break;
4079 }
Nadav Amit90de4a12015-04-13 01:53:41 +03004080 case KVM_ENABLE_CAP: {
4081 struct kvm_enable_cap cap;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004082
Nadav Amit90de4a12015-04-13 01:53:41 +03004083 r = -EFAULT;
4084 if (copy_from_user(&cap, argp, sizeof(cap)))
4085 goto out;
4086 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
4087 break;
4088 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004089 default:
Radim Krčmářc274e032014-11-21 22:21:50 +01004090 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004091 }
4092out:
4093 return r;
4094}
4095
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08004096static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02004097{
4098 u32 dummy[2];
4099 unsigned i, j;
4100
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004101 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02004102 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
4103 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004104
4105 /*
4106 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01004107 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004108 */
4109 switch (msrs_to_save[i]) {
4110 case MSR_IA32_BNDCFGS:
4111 if (!kvm_x86_ops->mpx_supported())
4112 continue;
4113 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01004114 case MSR_TSC_AUX:
4115 if (!kvm_x86_ops->rdtscp_supported())
4116 continue;
4117 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004118 default:
4119 break;
4120 }
4121
Carsten Otte043405e2007-10-10 17:16:19 +02004122 if (j < i)
4123 msrs_to_save[j] = msrs_to_save[i];
4124 j++;
4125 }
4126 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004127
4128 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
4129 switch (emulated_msrs[i]) {
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004130 case MSR_IA32_SMBASE:
4131 if (!kvm_x86_ops->cpu_has_high_real_mode_segbase())
4132 continue;
4133 break;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004134 default:
4135 break;
4136 }
4137
4138 if (j < i)
4139 emulated_msrs[j] = emulated_msrs[i];
4140 j++;
4141 }
4142 num_emulated_msrs = j;
Carsten Otte043405e2007-10-10 17:16:19 +02004143}
4144
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004145static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
4146 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004147{
Avi Kivity70252a12010-01-19 12:51:22 +02004148 int handled = 0;
4149 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004150
Avi Kivity70252a12010-01-19 12:51:22 +02004151 do {
4152 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004153 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004154 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
4155 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004156 break;
4157 handled += n;
4158 addr += n;
4159 len -= n;
4160 v += n;
4161 } while (len);
4162
4163 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004164}
4165
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004166static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004167{
Avi Kivity70252a12010-01-19 12:51:22 +02004168 int handled = 0;
4169 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004170
Avi Kivity70252a12010-01-19 12:51:22 +02004171 do {
4172 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004173 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004174 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
4175 addr, n, v))
4176 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004177 break;
4178 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
4179 handled += n;
4180 addr += n;
4181 len -= n;
4182 v += n;
4183 } while (len);
4184
4185 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004186}
4187
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004188static void kvm_set_segment(struct kvm_vcpu *vcpu,
4189 struct kvm_segment *var, int seg)
4190{
4191 kvm_x86_ops->set_segment(vcpu, var, seg);
4192}
4193
4194void kvm_get_segment(struct kvm_vcpu *vcpu,
4195 struct kvm_segment *var, int seg)
4196{
4197 kvm_x86_ops->get_segment(vcpu, var, seg);
4198}
4199
Paolo Bonzini54987b72014-09-02 13:23:06 +02004200gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
4201 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004202{
4203 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004204
4205 BUG_ON(!mmu_is_nested(vcpu));
4206
4207 /* NPT walks are always user-walks */
4208 access |= PFERR_USER_MASK;
Paolo Bonzini54987b72014-09-02 13:23:06 +02004209 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004210
4211 return t_gpa;
4212}
4213
Avi Kivityab9ae312010-11-22 17:53:26 +02004214gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
4215 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004216{
4217 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02004218 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004219}
4220
Avi Kivityab9ae312010-11-22 17:53:26 +02004221 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4222 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004223{
4224 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4225 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004226 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004227}
4228
Avi Kivityab9ae312010-11-22 17:53:26 +02004229gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4230 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004231{
4232 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4233 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004234 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004235}
4236
4237/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004238gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4239 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004240{
Avi Kivityab9ae312010-11-22 17:53:26 +02004241 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004242}
4243
4244static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4245 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004246 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004247{
4248 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004249 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004250
4251 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004252 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004253 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004254 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004255 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004256 int ret;
4257
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004258 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004259 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004260 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
4261 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004262 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004263 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004264 goto out;
4265 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004266
Izik Eidus77c20022008-12-29 01:42:19 +02004267 bytes -= toread;
4268 data += toread;
4269 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004270 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004271out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004272 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004273}
Izik Eidus77c20022008-12-29 01:42:19 +02004274
Gleb Natapov1871c602010-02-10 14:21:32 +02004275/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004276static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4277 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004278 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004279{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004280 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004281 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004282 unsigned offset;
4283 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004284
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004285 /* Inline kvm_read_guest_virt_helper for speed. */
4286 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4287 exception);
4288 if (unlikely(gpa == UNMAPPED_GVA))
4289 return X86EMUL_PROPAGATE_FAULT;
4290
4291 offset = addr & (PAGE_SIZE-1);
4292 if (WARN_ON(offset + bytes > PAGE_SIZE))
4293 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004294 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
4295 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004296 if (unlikely(ret < 0))
4297 return X86EMUL_IO_NEEDED;
4298
4299 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004300}
4301
Nadav Har'El064aea72011-05-25 23:04:56 +03004302int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004303 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004304 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004305{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004306 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004307 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004308
Gleb Natapov1871c602010-02-10 14:21:32 +02004309 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004310 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004311}
Nadav Har'El064aea72011-05-25 23:04:56 +03004312EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004313
Avi Kivity0f65dd72011-04-20 13:37:53 +03004314static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
4315 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004316 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004317{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004318 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004319 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004320}
4321
Radim Krčmář7a036a62015-10-30 16:36:24 +01004322static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
4323 unsigned long addr, void *val, unsigned int bytes)
4324{
4325 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4326 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
4327
4328 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
4329}
4330
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004331int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004332 gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004333 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004334 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004335{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004336 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Izik Eidus77c20022008-12-29 01:42:19 +02004337 void *data = val;
4338 int r = X86EMUL_CONTINUE;
4339
4340 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004341 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
4342 PFERR_WRITE_MASK,
Avi Kivityab9ae312010-11-22 17:53:26 +02004343 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004344 unsigned offset = addr & (PAGE_SIZE-1);
4345 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4346 int ret;
4347
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004348 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004349 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004350 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02004351 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004352 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004353 goto out;
4354 }
4355
4356 bytes -= towrite;
4357 data += towrite;
4358 addr += towrite;
4359 }
4360out:
4361 return r;
4362}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004363EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004364
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004365static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
4366 gpa_t *gpa, struct x86_exception *exception,
4367 bool write)
4368{
Avi Kivity97d64b72012-09-12 14:52:00 +03004369 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
4370 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004371
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004372 /*
4373 * currently PKRU is only applied to ept enabled guest so
4374 * there is no pkey in EPT page table for L1 guest or EPT
4375 * shadow page table for L2 guest.
4376 */
Avi Kivity97d64b72012-09-12 14:52:00 +03004377 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08004378 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004379 vcpu->arch.access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004380 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
4381 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08004382 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004383 return 1;
4384 }
4385
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004386 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
4387
4388 if (*gpa == UNMAPPED_GVA)
4389 return -1;
4390
4391 /* For APIC access vmexit */
4392 if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4393 return 1;
4394
Xiao Guangrong4f022642011-07-12 03:34:24 +08004395 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
4396 trace_vcpu_match_mmio(gva, *gpa, write, true);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004397 return 1;
Xiao Guangrong4f022642011-07-12 03:34:24 +08004398 }
Xiao Guangrongbebb1062011-07-12 03:23:20 +08004399
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004400 return 0;
4401}
4402
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004403int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004404 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02004405{
4406 int ret;
4407
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004408 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004409 if (ret < 0)
4410 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08004411 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02004412 return 1;
4413}
4414
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004415struct read_write_emulator_ops {
4416 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
4417 int bytes);
4418 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
4419 void *val, int bytes);
4420 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4421 int bytes, void *val);
4422 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
4423 void *val, int bytes);
4424 bool write;
4425};
4426
4427static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
4428{
4429 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004430 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Avi Kivityf78146b2012-04-18 19:22:47 +03004431 vcpu->mmio_fragments[0].gpa, *(u64 *)val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004432 vcpu->mmio_read_completed = 0;
4433 return 1;
4434 }
4435
4436 return 0;
4437}
4438
4439static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4440 void *val, int bytes)
4441{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004442 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004443}
4444
4445static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
4446 void *val, int bytes)
4447{
4448 return emulator_write_phys(vcpu, gpa, val, bytes);
4449}
4450
4451static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
4452{
4453 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4454 return vcpu_mmio_write(vcpu, gpa, bytes, val);
4455}
4456
4457static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4458 void *val, int bytes)
4459{
4460 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4461 return X86EMUL_IO_NEEDED;
4462}
4463
4464static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
4465 void *val, int bytes)
4466{
Avi Kivityf78146b2012-04-18 19:22:47 +03004467 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
4468
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004469 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004470 return X86EMUL_CONTINUE;
4471}
4472
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004473static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004474 .read_write_prepare = read_prepare,
4475 .read_write_emulate = read_emulate,
4476 .read_write_mmio = vcpu_mmio_read,
4477 .read_write_exit_mmio = read_exit_mmio,
4478};
4479
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004480static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08004481 .read_write_emulate = write_emulate,
4482 .read_write_mmio = write_mmio,
4483 .read_write_exit_mmio = write_exit_mmio,
4484 .write = true,
4485};
4486
Xiao Guangrong22388a32011-07-13 14:32:31 +08004487static int emulator_read_write_onepage(unsigned long addr, void *val,
4488 unsigned int bytes,
4489 struct x86_exception *exception,
4490 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004491 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004492{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004493 gpa_t gpa;
4494 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004495 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03004496 struct kvm_mmio_fragment *frag;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004497
4498 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004499
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004500 if (ret < 0)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004501 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004502
4503 /* For APIC access vmexit */
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08004504 if (ret)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004505 goto mmio;
4506
Xiao Guangrong22388a32011-07-13 14:32:31 +08004507 if (ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004508 return X86EMUL_CONTINUE;
4509
4510mmio:
4511 /*
4512 * Is this MMIO handled locally?
4513 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08004514 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02004515 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004516 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004517
Avi Kivity70252a12010-01-19 12:51:22 +02004518 gpa += handled;
4519 bytes -= handled;
4520 val += handled;
4521
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004522 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
4523 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
4524 frag->gpa = gpa;
4525 frag->data = val;
4526 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03004527 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004528}
4529
Xiubo Li52eb5a62015-03-13 17:39:45 +08004530static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
4531 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004532 void *val, unsigned int bytes,
4533 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02004534 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004535{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004536 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03004537 gpa_t gpa;
4538 int rc;
4539
4540 if (ops->read_write_prepare &&
4541 ops->read_write_prepare(vcpu, val, bytes))
4542 return X86EMUL_CONTINUE;
4543
4544 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004545
Carsten Ottebbd9b642007-10-30 18:44:21 +01004546 /* Crossing a page boundary? */
4547 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03004548 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004549
4550 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08004551 rc = emulator_read_write_onepage(addr, val, now, exception,
4552 vcpu, ops);
4553
Carsten Ottebbd9b642007-10-30 18:44:21 +01004554 if (rc != X86EMUL_CONTINUE)
4555 return rc;
4556 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02004557 if (ctxt->mode != X86EMUL_MODE_PROT64)
4558 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004559 val += now;
4560 bytes -= now;
4561 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08004562
Avi Kivityf78146b2012-04-18 19:22:47 +03004563 rc = emulator_read_write_onepage(addr, val, bytes, exception,
4564 vcpu, ops);
4565 if (rc != X86EMUL_CONTINUE)
4566 return rc;
4567
4568 if (!vcpu->mmio_nr_fragments)
4569 return rc;
4570
4571 gpa = vcpu->mmio_fragments[0].gpa;
4572
4573 vcpu->mmio_needed = 1;
4574 vcpu->mmio_cur_fragment = 0;
4575
Xiao Guangrong87da7e62012-10-24 14:07:59 +08004576 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03004577 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
4578 vcpu->run->exit_reason = KVM_EXIT_MMIO;
4579 vcpu->run->mmio.phys_addr = gpa;
4580
4581 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08004582}
4583
4584static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4585 unsigned long addr,
4586 void *val,
4587 unsigned int bytes,
4588 struct x86_exception *exception)
4589{
4590 return emulator_read_write(ctxt, addr, val, bytes,
4591 exception, &read_emultor);
4592}
4593
Xiubo Li52eb5a62015-03-13 17:39:45 +08004594static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08004595 unsigned long addr,
4596 const void *val,
4597 unsigned int bytes,
4598 struct x86_exception *exception)
4599{
4600 return emulator_read_write(ctxt, addr, (void *)val, bytes,
4601 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004602}
Carsten Ottebbd9b642007-10-30 18:44:21 +01004603
Avi Kivitydaea3e72010-03-15 13:59:54 +02004604#define CMPXCHG_TYPE(t, ptr, old, new) \
4605 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
4606
4607#ifdef CONFIG_X86_64
4608# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
4609#else
4610# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01004611 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02004612#endif
4613
Avi Kivity0f65dd72011-04-20 13:37:53 +03004614static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
4615 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01004616 const void *old,
4617 const void *new,
4618 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004619 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004620{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004621 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004622 gpa_t gpa;
4623 struct page *page;
4624 char *kaddr;
4625 bool exchanged;
4626
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004627 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02004628 if (bytes > 8 || (bytes & (bytes - 1)))
4629 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004630
Avi Kivitydaea3e72010-03-15 13:59:54 +02004631 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004632
Avi Kivitydaea3e72010-03-15 13:59:54 +02004633 if (gpa == UNMAPPED_GVA ||
4634 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
4635 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004636
Avi Kivitydaea3e72010-03-15 13:59:54 +02004637 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
4638 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004639
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004640 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08004641 if (is_error_page(page))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08004642 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004643
Cong Wang8fd75e12011-11-25 23:14:17 +08004644 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004645 kaddr += offset_in_page(gpa);
4646 switch (bytes) {
4647 case 1:
4648 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
4649 break;
4650 case 2:
4651 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
4652 break;
4653 case 4:
4654 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
4655 break;
4656 case 8:
4657 exchanged = CMPXCHG64(kaddr, old, new);
4658 break;
4659 default:
4660 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004661 }
Cong Wang8fd75e12011-11-25 23:14:17 +08004662 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02004663 kvm_release_page_dirty(page);
4664
4665 if (!exchanged)
4666 return X86EMUL_CMPXCHG_FAILED;
4667
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004668 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08004669 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004670
4671 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02004672
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004673emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02004674 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05004675
Avi Kivity0f65dd72011-04-20 13:37:53 +03004676 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004677}
4678
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004679static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
4680{
4681 /* TODO: String I/O for in kernel device */
4682 int r;
4683
4684 if (vcpu->arch.pio.in)
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004685 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004686 vcpu->arch.pio.size, pd);
4687 else
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004688 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004689 vcpu->arch.pio.port, vcpu->arch.pio.size,
4690 pd);
4691 return r;
4692}
4693
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004694static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
4695 unsigned short port, void *val,
4696 unsigned int count, bool in)
4697{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004698 vcpu->arch.pio.port = port;
4699 vcpu->arch.pio.in = in;
4700 vcpu->arch.pio.count = count;
4701 vcpu->arch.pio.size = size;
4702
4703 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
4704 vcpu->arch.pio.count = 0;
4705 return 1;
4706 }
4707
4708 vcpu->run->exit_reason = KVM_EXIT_IO;
4709 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
4710 vcpu->run->io.size = size;
4711 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
4712 vcpu->run->io.count = count;
4713 vcpu->run->io.port = port;
4714
4715 return 0;
4716}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004717
Avi Kivityca1d4a92011-04-20 13:37:53 +03004718static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
4719 int size, unsigned short port, void *val,
4720 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004721{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004722 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004723 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03004724
Gleb Natapov79729952010-03-18 15:20:24 +02004725 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004726 goto data_avail;
4727
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004728 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
4729 if (ret) {
4730data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004731 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004732 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02004733 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004734 return 1;
4735 }
4736
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004737 return 0;
4738}
4739
Avi Kivityca1d4a92011-04-20 13:37:53 +03004740static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
4741 int size, unsigned short port,
4742 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004743{
Avi Kivityca1d4a92011-04-20 13:37:53 +03004744 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4745
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004746 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02004747 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08004748 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004749}
4750
Carsten Ottebbd9b642007-10-30 18:44:21 +01004751static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
4752{
4753 return kvm_x86_ops->get_segment_base(vcpu, seg);
4754}
4755
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004756static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004757{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03004758 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004759}
4760
Joel Schopp5cb56052015-03-02 13:43:31 -06004761int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004762{
4763 if (!need_emulate_wbinvd(vcpu))
4764 return X86EMUL_CONTINUE;
4765
4766 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01004767 int cpu = get_cpu();
4768
4769 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004770 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
4771 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004772 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004773 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01004774 } else
4775 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004776 return X86EMUL_CONTINUE;
4777}
Joel Schopp5cb56052015-03-02 13:43:31 -06004778
4779int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
4780{
4781 kvm_x86_ops->skip_emulated_instruction(vcpu);
4782 return kvm_emulate_wbinvd_noskip(vcpu);
4783}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004784EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
4785
Joel Schopp5cb56052015-03-02 13:43:31 -06004786
4787
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004788static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
4789{
Joel Schopp5cb56052015-03-02 13:43:31 -06004790 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03004791}
4792
Xiubo Li52eb5a62015-03-13 17:39:45 +08004793static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
4794 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004795{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004796 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004797}
4798
Xiubo Li52eb5a62015-03-13 17:39:45 +08004799static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
4800 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004801{
Gleb Natapov338dbc92010-04-28 19:15:32 +03004802
Avi Kivity717746e2011-04-20 13:37:53 +03004803 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004804}
4805
Gleb Natapov52a46612010-03-18 15:20:03 +02004806static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4807{
4808 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4809}
4810
Avi Kivity717746e2011-04-20 13:37:53 +03004811static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02004812{
Avi Kivity717746e2011-04-20 13:37:53 +03004813 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02004814 unsigned long value;
4815
4816 switch (cr) {
4817 case 0:
4818 value = kvm_read_cr0(vcpu);
4819 break;
4820 case 2:
4821 value = vcpu->arch.cr2;
4822 break;
4823 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004824 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02004825 break;
4826 case 4:
4827 value = kvm_read_cr4(vcpu);
4828 break;
4829 case 8:
4830 value = kvm_get_cr8(vcpu);
4831 break;
4832 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004833 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02004834 return 0;
4835 }
4836
4837 return value;
4838}
4839
Avi Kivity717746e2011-04-20 13:37:53 +03004840static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02004841{
Avi Kivity717746e2011-04-20 13:37:53 +03004842 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03004843 int res = 0;
4844
Gleb Natapov52a46612010-03-18 15:20:03 +02004845 switch (cr) {
4846 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03004847 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004848 break;
4849 case 2:
4850 vcpu->arch.cr2 = val;
4851 break;
4852 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004853 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004854 break;
4855 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03004856 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02004857 break;
4858 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004859 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02004860 break;
4861 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03004862 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03004863 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02004864 }
Gleb Natapov0f122442010-04-28 19:15:31 +03004865
4866 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02004867}
4868
Avi Kivity717746e2011-04-20 13:37:53 +03004869static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02004870{
Avi Kivity717746e2011-04-20 13:37:53 +03004871 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02004872}
4873
Avi Kivity4bff1e862011-04-20 13:37:53 +03004874static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004875{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004876 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004877}
4878
Avi Kivity4bff1e862011-04-20 13:37:53 +03004879static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004880{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004881 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03004882}
4883
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03004884static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4885{
4886 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
4887}
4888
4889static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
4890{
4891 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
4892}
4893
Avi Kivity4bff1e862011-04-20 13:37:53 +03004894static unsigned long emulator_get_cached_segment_base(
4895 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03004896{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004897 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03004898}
4899
Avi Kivity1aa36612011-04-27 13:20:30 +03004900static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
4901 struct desc_struct *desc, u32 *base3,
4902 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004903{
4904 struct kvm_segment var;
4905
Avi Kivity4bff1e862011-04-20 13:37:53 +03004906 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03004907 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004908
Gleb Natapov378a8b02013-01-21 15:36:48 +02004909 if (var.unusable) {
4910 memset(desc, 0, sizeof(*desc));
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004911 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02004912 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004913
4914 if (var.g)
4915 var.limit >>= 12;
4916 set_desc_limit(desc, var.limit);
4917 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02004918#ifdef CONFIG_X86_64
4919 if (base3)
4920 *base3 = var.base >> 32;
4921#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004922 desc->type = var.type;
4923 desc->s = var.s;
4924 desc->dpl = var.dpl;
4925 desc->p = var.present;
4926 desc->avl = var.avl;
4927 desc->l = var.l;
4928 desc->d = var.db;
4929 desc->g = var.g;
4930
4931 return true;
4932}
4933
Avi Kivity1aa36612011-04-27 13:20:30 +03004934static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
4935 struct desc_struct *desc, u32 base3,
4936 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004937{
Avi Kivity4bff1e862011-04-20 13:37:53 +03004938 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004939 struct kvm_segment var;
4940
Avi Kivity1aa36612011-04-27 13:20:30 +03004941 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004942 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02004943#ifdef CONFIG_X86_64
4944 var.base |= ((u64)base3) << 32;
4945#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004946 var.limit = get_desc_limit(desc);
4947 if (desc->g)
4948 var.limit = (var.limit << 12) | 0xfff;
4949 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004950 var.dpl = desc->dpl;
4951 var.db = desc->d;
4952 var.s = desc->s;
4953 var.l = desc->l;
4954 var.g = desc->g;
4955 var.avl = desc->avl;
4956 var.present = desc->p;
4957 var.unusable = !var.present;
4958 var.padding = 0;
4959
4960 kvm_set_segment(vcpu, &var, seg);
4961 return;
4962}
4963
Avi Kivity717746e2011-04-20 13:37:53 +03004964static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
4965 u32 msr_index, u64 *pdata)
4966{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004967 struct msr_data msr;
4968 int r;
4969
4970 msr.index = msr_index;
4971 msr.host_initiated = false;
4972 r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
4973 if (r)
4974 return r;
4975
4976 *pdata = msr.data;
4977 return 0;
Avi Kivity717746e2011-04-20 13:37:53 +03004978}
4979
4980static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
4981 u32 msr_index, u64 data)
4982{
Will Auld8fe8ab42012-11-29 12:42:12 -08004983 struct msr_data msr;
4984
4985 msr.data = data;
4986 msr.index = msr_index;
4987 msr.host_initiated = false;
4988 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03004989}
4990
Paolo Bonzini64d60672015-05-07 11:36:11 +02004991static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
4992{
4993 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4994
4995 return vcpu->arch.smbase;
4996}
4997
4998static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
4999{
5000 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5001
5002 vcpu->arch.smbase = smbase;
5003}
5004
Nadav Amit67f4d422014-06-02 18:34:09 +03005005static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
5006 u32 pmc)
5007{
Wei Huangc6702c92015-06-19 13:44:45 +02005008 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03005009}
5010
Avi Kivity222d21a2011-11-10 14:57:30 +02005011static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
5012 u32 pmc, u64 *pdata)
5013{
Wei Huangc6702c92015-06-19 13:44:45 +02005014 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02005015}
5016
Avi Kivity6c3287f2011-04-20 15:43:05 +03005017static void emulator_halt(struct x86_emulate_ctxt *ctxt)
5018{
5019 emul_to_vcpu(ctxt)->arch.halt_request = 1;
5020}
5021
Avi Kivity5037f6f2011-03-28 16:53:59 +02005022static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
5023{
5024 preempt_disable();
Avi Kivity5197b802011-04-20 15:55:40 +03005025 kvm_load_guest_fpu(emul_to_vcpu(ctxt));
Avi Kivity5037f6f2011-03-28 16:53:59 +02005026 /*
5027 * CR0.TS may reference the host fpu state, not the guest fpu state,
5028 * so it may be clear at this point.
5029 */
5030 clts();
5031}
5032
5033static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
5034{
5035 preempt_enable();
5036}
5037
Avi Kivity29535382011-04-20 13:37:53 +03005038static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005039 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02005040 enum x86_intercept_stage stage)
5041{
Avi Kivity29535382011-04-20 13:37:53 +03005042 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005043}
5044
Avi Kivity0017f932012-06-07 14:10:16 +03005045static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005046 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
5047{
Avi Kivity0017f932012-06-07 14:10:16 +03005048 kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005049}
5050
Avi Kivitydd856ef2012-08-27 23:46:17 +03005051static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
5052{
5053 return kvm_register_read(emul_to_vcpu(ctxt), reg);
5054}
5055
5056static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
5057{
5058 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
5059}
5060
Nadav Amit801806d2015-01-26 09:32:23 +02005061static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
5062{
5063 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
5064}
5065
Mathias Krause0225fb52012-08-30 01:30:16 +02005066static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03005067 .read_gpr = emulator_read_gpr,
5068 .write_gpr = emulator_write_gpr,
Gleb Natapov1871c602010-02-10 14:21:32 +02005069 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005070 .write_std = kvm_write_guest_virt_system,
Radim Krčmář7a036a62015-10-30 16:36:24 +01005071 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02005072 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005073 .read_emulated = emulator_read_emulated,
5074 .write_emulated = emulator_write_emulated,
5075 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005076 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005077 .pio_in_emulated = emulator_pio_in_emulated,
5078 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03005079 .get_segment = emulator_get_segment,
5080 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03005081 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005082 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005083 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03005084 .set_gdt = emulator_set_gdt,
5085 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02005086 .get_cr = emulator_get_cr,
5087 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02005088 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03005089 .get_dr = emulator_get_dr,
5090 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02005091 .get_smbase = emulator_get_smbase,
5092 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03005093 .set_msr = emulator_set_msr,
5094 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03005095 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02005096 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03005097 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005098 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03005099 .fix_hypercall = emulator_fix_hypercall,
Avi Kivity5037f6f2011-03-28 16:53:59 +02005100 .get_fpu = emulator_get_fpu,
5101 .put_fpu = emulator_put_fpu,
Avi Kivityc4f035c2011-04-04 12:39:22 +02005102 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005103 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02005104 .set_nmi_mask = emulator_set_nmi_mask,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005105};
5106
Gleb Natapov95cb2292010-04-28 19:15:43 +03005107static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
5108{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02005109 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03005110 /*
5111 * an sti; sti; sequence only disable interrupts for the first
5112 * instruction. So, if the last instruction, be it emulated or
5113 * not, left the system with the INT_STI flag enabled, it
5114 * means that the last instruction is an sti. We should not
5115 * leave the flag on in this case. The same goes for mov ss
5116 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02005117 if (int_shadow & mask)
5118 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005119 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03005120 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005121 if (!mask)
5122 kvm_make_request(KVM_REQ_EVENT, vcpu);
5123 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03005124}
5125
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005126static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03005127{
5128 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02005129 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005130 return kvm_propagate_fault(vcpu, &ctxt->exception);
5131
5132 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02005133 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
5134 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03005135 else
Avi Kivityda9cb572010-11-22 17:53:21 +02005136 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005137 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03005138}
5139
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005140static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
5141{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005142 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005143 int cs_db, cs_l;
5144
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005145 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
5146
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005147 ctxt->eflags = kvm_get_rflags(vcpu);
5148 ctxt->eip = kvm_rip_read(vcpu);
5149 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
5150 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03005151 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005152 cs_db ? X86EMUL_MODE_PROT32 :
5153 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005154 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005155 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
5156 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005157 ctxt->emul_flags = vcpu->arch.hflags;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005158
Avi Kivitydd856ef2012-08-27 23:46:17 +03005159 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005160 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005161}
5162
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005163int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02005164{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005165 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02005166 int ret;
5167
5168 init_emulate_ctxt(vcpu);
5169
Avi Kivity9dac77f2011-06-01 15:34:25 +03005170 ctxt->op_bytes = 2;
5171 ctxt->ad_bytes = 2;
5172 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005173 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02005174
5175 if (ret != X86EMUL_CONTINUE)
5176 return EMULATE_FAIL;
5177
Avi Kivity9dac77f2011-06-01 15:34:25 +03005178 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005179 kvm_rip_write(vcpu, ctxt->eip);
5180 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02005181
5182 if (irq == NMI_VECTOR)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03005183 vcpu->arch.nmi_pending = 0;
Mohammed Gamal63995652010-09-19 14:34:06 +02005184 else
5185 vcpu->arch.interrupt.pending = false;
5186
5187 return EMULATE_DONE;
5188}
5189EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
5190
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005191static int handle_emulation_failure(struct kvm_vcpu *vcpu)
5192{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005193 int r = EMULATE_DONE;
5194
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005195 ++vcpu->stat.insn_emulation_fail;
5196 trace_kvm_emulate_insn_failed(vcpu);
Nadav Amita2b9e6c2014-09-17 02:50:50 +03005197 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005198 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5199 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5200 vcpu->run->internal.ndata = 0;
5201 r = EMULATE_FAIL;
5202 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005203 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005204
5205 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005206}
5207
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005208static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03005209 bool write_fault_to_shadow_pgtable,
5210 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03005211{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005212 gpa_t gpa = cr2;
Dan Williamsba049e92016-01-15 16:56:11 -08005213 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005214
Gleb Natapov991eebf2013-04-11 12:10:51 +03005215 if (emulation_type & EMULTYPE_NO_REEXECUTE)
5216 return false;
5217
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005218 if (!vcpu->arch.mmu.direct_map) {
5219 /*
5220 * Write permission should be allowed since only
5221 * write access need to be emulated.
5222 */
5223 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03005224
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005225 /*
5226 * If the mapping is invalid in guest, let cpu retry
5227 * it to generate fault.
5228 */
5229 if (gpa == UNMAPPED_GVA)
5230 return true;
5231 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005232
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005233 /*
5234 * Do not retry the unhandleable instruction if it faults on the
5235 * readonly host memory, otherwise it will goto a infinite loop:
5236 * retry instruction -> write #PF -> emulation fail -> retry
5237 * instruction -> ...
5238 */
5239 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005240
5241 /*
5242 * If the instruction failed on the error pfn, it can not be fixed,
5243 * report the error to userspace.
5244 */
5245 if (is_error_noslot_pfn(pfn))
5246 return false;
5247
5248 kvm_release_pfn_clean(pfn);
5249
5250 /* The instructions are well-emulated on direct mmu. */
5251 if (vcpu->arch.mmu.direct_map) {
5252 unsigned int indirect_shadow_pages;
5253
5254 spin_lock(&vcpu->kvm->mmu_lock);
5255 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5256 spin_unlock(&vcpu->kvm->mmu_lock);
5257
5258 if (indirect_shadow_pages)
5259 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5260
Gleb Natapova6f177e2010-07-08 12:41:12 +03005261 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005262 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005263
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005264 /*
5265 * if emulation was due to access to shadowed page table
5266 * and it failed try to unshadow page and re-enter the
5267 * guest to let CPU execute the instruction.
5268 */
5269 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005270
5271 /*
5272 * If the access faults on its page table, it can not
5273 * be fixed by unprotecting shadow page and it should
5274 * be reported to userspace.
5275 */
5276 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005277}
5278
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005279static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5280 unsigned long cr2, int emulation_type)
5281{
5282 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5283 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5284
5285 last_retry_eip = vcpu->arch.last_retry_eip;
5286 last_retry_addr = vcpu->arch.last_retry_addr;
5287
5288 /*
5289 * If the emulation is caused by #PF and it is non-page_table
5290 * writing instruction, it means the VM-EXIT is caused by shadow
5291 * page protected, we can zap the shadow page and retry this
5292 * instruction directly.
5293 *
5294 * Note: if the guest uses a non-page-table modifying instruction
5295 * on the PDE that points to the instruction, then we will unmap
5296 * the instruction and go to an infinite loop. So, we cache the
5297 * last retried eip and the last fault address, if we meet the eip
5298 * and the address again, we can break out of the potential infinite
5299 * loop.
5300 */
5301 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5302
5303 if (!(emulation_type & EMULTYPE_RETRY))
5304 return false;
5305
5306 if (x86_page_table_writing_insn(ctxt))
5307 return false;
5308
5309 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5310 return false;
5311
5312 vcpu->arch.last_retry_eip = ctxt->eip;
5313 vcpu->arch.last_retry_addr = cr2;
5314
5315 if (!vcpu->arch.mmu.direct_map)
5316 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5317
Xiao Guangrong22368022013-01-13 23:44:12 +08005318 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005319
5320 return true;
5321}
5322
Gleb Natapov716d51a2012-09-03 15:24:26 +03005323static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5324static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5325
Paolo Bonzini64d60672015-05-07 11:36:11 +02005326static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02005327{
Paolo Bonzini64d60672015-05-07 11:36:11 +02005328 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005329 /* This is a good place to trace that we are exiting SMM. */
5330 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
5331
Paolo Bonzinic43203c2016-06-01 22:26:00 +02005332 /* Process a latched INIT or SMI, if any. */
5333 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005334 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02005335
5336 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005337}
5338
5339static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
5340{
5341 unsigned changed = vcpu->arch.hflags ^ emul_flags;
5342
Paolo Bonzinia5845392015-04-01 18:18:53 +02005343 vcpu->arch.hflags = emul_flags;
Paolo Bonzini64d60672015-05-07 11:36:11 +02005344
5345 if (changed & HF_SMM_MASK)
5346 kvm_smm_changed(vcpu);
Paolo Bonzinia5845392015-04-01 18:18:53 +02005347}
5348
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005349static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
5350 unsigned long *db)
5351{
5352 u32 dr6 = 0;
5353 int i;
5354 u32 enable, rwlen;
5355
5356 enable = dr7;
5357 rwlen = dr7 >> 16;
5358 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
5359 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
5360 dr6 |= (1 << i);
5361 return dr6;
5362}
5363
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005364static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005365{
5366 struct kvm_run *kvm_run = vcpu->run;
5367
5368 /*
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005369 * rflags is the old, "raw" value of the flags. The new value has
5370 * not been saved yet.
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005371 *
5372 * This is correct even for TF set by the guest, because "the
5373 * processor will not generate this exception after the instruction
5374 * that sets the TF flag".
5375 */
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005376 if (unlikely(rflags & X86_EFLAGS_TF)) {
5377 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005378 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5379 DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005380 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5381 kvm_run->debug.arch.exception = DB_VECTOR;
5382 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5383 *r = EMULATE_USER_EXIT;
5384 } else {
5385 vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5386 /*
5387 * "Certain debug exceptions may clear bit 0-3. The
5388 * remaining contents of the DR6 register are never
5389 * cleared by the processor".
5390 */
5391 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005392 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005393 kvm_queue_exception(vcpu, DB_VECTOR);
5394 }
5395 }
5396}
5397
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005398static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
5399{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005400 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
5401 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005402 struct kvm_run *kvm_run = vcpu->run;
5403 unsigned long eip = kvm_get_linear_rip(vcpu);
5404 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005405 vcpu->arch.guest_debug_dr7,
5406 vcpu->arch.eff_db);
5407
5408 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03005409 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02005410 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005411 kvm_run->debug.arch.exception = DB_VECTOR;
5412 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5413 *r = EMULATE_USER_EXIT;
5414 return true;
5415 }
5416 }
5417
Nadav Amit4161a562014-07-17 01:19:31 +03005418 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
5419 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02005420 unsigned long eip = kvm_get_linear_rip(vcpu);
5421 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005422 vcpu->arch.dr7,
5423 vcpu->arch.db);
5424
5425 if (dr6 != 0) {
5426 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005427 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005428 kvm_queue_exception(vcpu, DB_VECTOR);
5429 *r = EMULATE_DONE;
5430 return true;
5431 }
5432 }
5433
5434 return false;
5435}
5436
Andre Przywara51d8b662010-12-21 11:12:02 +01005437int x86_emulate_instruction(struct kvm_vcpu *vcpu,
5438 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01005439 int emulation_type,
5440 void *insn,
5441 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005442{
Gleb Natapov95cb2292010-04-28 19:15:43 +03005443 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005444 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005445 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005446 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005447
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005448 /*
5449 * Clear write_fault_to_shadow_pgtable here to ensure it is
5450 * never reused.
5451 */
5452 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03005453 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03005454
Sheng Yang571008d2008-01-02 14:49:22 +08005455 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005456 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02005457
5458 /*
5459 * We will reenter on the same instruction since
5460 * we do not set complete_userspace_io. This does not
5461 * handle watchpoints yet, those would be handled in
5462 * the emulate_ops.
5463 */
5464 if (kvm_vcpu_check_breakpoint(vcpu, &r))
5465 return r;
5466
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005467 ctxt->interruptibility = 0;
5468 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005469 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005470 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005471
Borislav Petkovb51e9742013-09-22 16:44:52 +02005472 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02005473
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005474 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02005475
Avi Kivitye46479f2010-04-11 13:05:16 +03005476 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02005477 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005478 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02005479 if (emulation_type & EMULTYPE_TRAP_UD)
5480 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03005481 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5482 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005483 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005484 if (emulation_type & EMULTYPE_SKIP)
5485 return EMULATE_FAIL;
5486 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005487 }
5488 }
5489
Gleb Natapovba8afb62009-04-12 13:36:57 +03005490 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005491 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03005492 if (ctxt->eflags & X86_EFLAGS_RF)
5493 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03005494 return EMULATE_DONE;
5495 }
5496
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005497 if (retry_instruction(ctxt, cr2, emulation_type))
5498 return EMULATE_DONE;
5499
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005500 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005501 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005502 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
5503 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005504 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005505 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005506
Gleb Natapov5cd21912010-03-18 15:20:26 +02005507restart:
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005508 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005509
Joerg Roedel775fde82011-04-04 12:39:24 +02005510 if (r == EMULATION_INTERCEPTED)
5511 return EMULATE_DONE;
5512
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005513 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03005514 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
5515 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005516 return EMULATE_DONE;
5517
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005518 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005519 }
5520
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005521 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005522 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005523 if (inject_emulated_exception(vcpu))
5524 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005525 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02005526 if (!vcpu->arch.pio.in) {
5527 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03005528 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02005529 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005530 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005531 vcpu->arch.complete_userspace_io = complete_emulated_pio;
5532 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005533 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005534 } else if (vcpu->mmio_needed) {
5535 if (!vcpu->mmio_is_write)
5536 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005537 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03005538 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005539 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03005540 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005541 else
5542 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03005543
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005544 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005545 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005546 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005547 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005548 if (vcpu->arch.hflags != ctxt->emul_flags)
5549 kvm_set_hflags(vcpu, ctxt->emul_flags);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005550 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02005551 if (r == EMULATE_DONE)
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005552 kvm_vcpu_check_singlestep(vcpu, rflags, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02005553 if (!ctxt->have_exception ||
5554 exception_type(ctxt->exception.vector) == EXCPT_TRAP)
5555 __kvm_set_rflags(vcpu, ctxt->eflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005556
5557 /*
5558 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
5559 * do nothing, and it will be requested again as soon as
5560 * the shadow expires. But we still need to check here,
5561 * because POPF has no interrupt shadow.
5562 */
5563 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
5564 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005565 } else
5566 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03005567
Gleb Natapove85d28f2010-07-29 15:11:52 +03005568 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005569}
Andre Przywara51d8b662010-12-21 11:12:02 +01005570EXPORT_SYMBOL_GPL(x86_emulate_instruction);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005571
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005572int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01005573{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005574 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005575 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
5576 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005577 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02005578 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02005579 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01005580}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005581EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01005582
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005583static void tsc_bad(void *info)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005584{
Tejun Heo0a3aee02010-12-18 16:28:55 +01005585 __this_cpu_write(cpu_tsc_khz, 0);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005586}
5587
5588static void tsc_khz_changed(void *data)
5589{
5590 struct cpufreq_freqs *freq = data;
5591 unsigned long khz = 0;
5592
5593 if (data)
5594 khz = freq->new;
5595 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5596 khz = cpufreq_quick_get(raw_smp_processor_id());
5597 if (!khz)
5598 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01005599 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005600}
5601
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005602static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
5603 void *data)
5604{
5605 struct cpufreq_freqs *freq = data;
5606 struct kvm *kvm;
5607 struct kvm_vcpu *vcpu;
5608 int i, send_ipi = 0;
5609
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005610 /*
5611 * We allow guests to temporarily run on slowing clocks,
5612 * provided we notify them after, or to run on accelerating
5613 * clocks, provided we notify them before. Thus time never
5614 * goes backwards.
5615 *
5616 * However, we have a problem. We can't atomically update
5617 * the frequency of a given CPU from this function; it is
5618 * merely a notifier, which can be called from any CPU.
5619 * Changing the TSC frequency at arbitrary points in time
5620 * requires a recomputation of local variables related to
5621 * the TSC for each VCPU. We must flag these local variables
5622 * to be updated and be sure the update takes place with the
5623 * new frequency before any guests proceed.
5624 *
5625 * Unfortunately, the combination of hotplug CPU and frequency
5626 * change creates an intractable locking scenario; the order
5627 * of when these callouts happen is undefined with respect to
5628 * CPU hotplug, and they can race with each other. As such,
5629 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
5630 * undefined; you can actually have a CPU frequency change take
5631 * place in between the computation of X and the setting of the
5632 * variable. To protect against this problem, all updates of
5633 * the per_cpu tsc_khz variable are done in an interrupt
5634 * protected IPI, and all callers wishing to update the value
5635 * must wait for a synchronous IPI to complete (which is trivial
5636 * if the caller is on the CPU already). This establishes the
5637 * necessary total order on variable updates.
5638 *
5639 * Note that because a guest time update may take place
5640 * anytime after the setting of the VCPU's request bit, the
5641 * correct TSC value must be set before the request. However,
5642 * to ensure the update actually makes it to any guest which
5643 * starts running in hardware virtualization between the set
5644 * and the acquisition of the spinlock, we must also ping the
5645 * CPU after setting the request bit.
5646 *
5647 */
5648
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005649 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
5650 return 0;
5651 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
5652 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005653
5654 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005655
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005656 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005657 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005658 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005659 if (vcpu->cpu != freq->cpu)
5660 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10005661 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005662 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005663 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005664 }
5665 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005666 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005667
5668 if (freq->old < freq->new && send_ipi) {
5669 /*
5670 * We upscale the frequency. Must make the guest
5671 * doesn't see old kvmclock values while running with
5672 * the new frequency, otherwise we risk the guest sees
5673 * time go backwards.
5674 *
5675 * In case we update the frequency for another cpu
5676 * (which might be in guest context) send an interrupt
5677 * to kick the cpu out of guest context. Next time
5678 * guest context is entered kvmclock will be updated,
5679 * so the guest will not see stale values.
5680 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005681 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005682 }
5683 return 0;
5684}
5685
5686static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005687 .notifier_call = kvmclock_cpufreq_notifier
5688};
5689
5690static int kvmclock_cpu_notifier(struct notifier_block *nfb,
5691 unsigned long action, void *hcpu)
5692{
5693 unsigned int cpu = (unsigned long)hcpu;
5694
5695 switch (action) {
5696 case CPU_ONLINE:
5697 case CPU_DOWN_FAILED:
5698 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
5699 break;
5700 case CPU_DOWN_PREPARE:
5701 smp_call_function_single(cpu, tsc_bad, NULL, 1);
5702 break;
5703 }
5704 return NOTIFY_OK;
5705}
5706
5707static struct notifier_block kvmclock_cpu_notifier_block = {
5708 .notifier_call = kvmclock_cpu_notifier,
5709 .priority = -INT_MAX
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005710};
5711
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005712static void kvm_timer_init(void)
5713{
5714 int cpu;
5715
Zachary Amsdenc2855452010-09-18 14:38:15 -10005716 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305717
5718 cpu_notifier_register_begin();
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005719 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10005720#ifdef CONFIG_CPU_FREQ
5721 struct cpufreq_policy policy;
5722 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02005723 cpu = get_cpu();
5724 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10005725 if (policy.cpuinfo.max_freq)
5726 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02005727 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10005728#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005729 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
5730 CPUFREQ_TRANSITION_NOTIFIER);
5731 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10005732 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005733 for_each_online_cpu(cpu)
5734 smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05305735
5736 __register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
5737 cpu_notifier_register_done();
5738
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005739}
5740
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005741static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
5742
Gleb Natapovf5132b02011-11-10 14:57:22 +02005743int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005744{
Alex,Shi086c9852011-10-20 15:34:01 +08005745 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005746}
5747
5748static int kvm_is_user_mode(void)
5749{
5750 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005751
Alex,Shi086c9852011-10-20 15:34:01 +08005752 if (__this_cpu_read(current_vcpu))
5753 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005754
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005755 return user_mode != 0;
5756}
5757
5758static unsigned long kvm_get_guest_ip(void)
5759{
5760 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005761
Alex,Shi086c9852011-10-20 15:34:01 +08005762 if (__this_cpu_read(current_vcpu))
5763 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08005764
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005765 return ip;
5766}
5767
5768static struct perf_guest_info_callbacks kvm_guest_cbs = {
5769 .is_in_guest = kvm_is_in_guest,
5770 .is_user_mode = kvm_is_user_mode,
5771 .get_guest_ip = kvm_get_guest_ip,
5772};
5773
5774void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
5775{
Alex,Shi086c9852011-10-20 15:34:01 +08005776 __this_cpu_write(current_vcpu, vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005777}
5778EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
5779
5780void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
5781{
Alex,Shi086c9852011-10-20 15:34:01 +08005782 __this_cpu_write(current_vcpu, NULL);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005783}
5784EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
5785
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005786static void kvm_set_mmio_spte_mask(void)
5787{
5788 u64 mask;
5789 int maxphyaddr = boot_cpu_data.x86_phys_bits;
5790
5791 /*
5792 * Set the reserved bits and the present bit of an paging-structure
5793 * entry to generate page fault with PFER.RSV = 1.
5794 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005795 /* Mask the reserved physical address bits. */
Tiejun Chend1431482014-09-01 18:44:04 +08005796 mask = rsvd_bits(maxphyaddr, 51);
Xiao Guangrong885032b2013-06-07 16:51:23 +08005797
5798 /* Bit 62 is always reserved for 32bit host. */
5799 mask |= 0x3ull << 62;
5800
5801 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005802 mask |= 1ull;
5803
5804#ifdef CONFIG_X86_64
5805 /*
5806 * If reserved bit is not supported, clear the present bit to disable
5807 * mmio page fault.
5808 */
5809 if (maxphyaddr == 52)
5810 mask &= ~1ull;
5811#endif
5812
5813 kvm_mmu_set_mmio_spte_mask(mask);
5814}
5815
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005816#ifdef CONFIG_X86_64
5817static void pvclock_gtod_update_fn(struct work_struct *work)
5818{
Marcelo Tosattid8281992012-11-27 23:29:01 -02005819 struct kvm *kvm;
5820
5821 struct kvm_vcpu *vcpu;
5822 int i;
5823
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005824 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005825 list_for_each_entry(kvm, &vm_list, vm_list)
5826 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08005827 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02005828 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005829 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005830}
5831
5832static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
5833
5834/*
5835 * Notification about pvclock gtod data update.
5836 */
5837static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
5838 void *priv)
5839{
5840 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
5841 struct timekeeper *tk = priv;
5842
5843 update_pvclock_gtod(tk);
5844
5845 /* disable master clock if host does not trust, or does not
5846 * use, TSC clocksource
5847 */
5848 if (gtod->clock.vclock_mode != VCLOCK_TSC &&
5849 atomic_read(&kvm_guest_has_master_clock) != 0)
5850 queue_work(system_long_wq, &pvclock_gtod_work);
5851
5852 return 0;
5853}
5854
5855static struct notifier_block pvclock_gtod_notifier = {
5856 .notifier_call = pvclock_gtod_notify,
5857};
5858#endif
5859
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005860int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02005861{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005862 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02005863 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005864
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005865 if (kvm_x86_ops) {
5866 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005867 r = -EEXIST;
5868 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005869 }
5870
5871 if (!ops->cpu_has_kvm_support()) {
5872 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005873 r = -EOPNOTSUPP;
5874 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005875 }
5876 if (ops->disabled_by_bios()) {
5877 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005878 r = -EOPNOTSUPP;
5879 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005880 }
5881
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005882 r = -ENOMEM;
5883 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
5884 if (!shared_msrs) {
5885 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
5886 goto out;
5887 }
5888
Avi Kivity97db56c2008-01-13 13:23:56 +02005889 r = kvm_mmu_module_init();
5890 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005891 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02005892
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005893 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02005894
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005895 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01005896
Sheng Yang7b523452008-04-25 21:13:50 +08005897 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04005898 PT_DIRTY_MASK, PT64_NX_MASK, 0,
5899 PT_PRESENT_MASK);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10005900 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01005901
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005902 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5903
Borislav Petkovd366bf72016-04-04 22:25:02 +02005904 if (boot_cpu_has(X86_FEATURE_XSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +08005905 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
5906
Gleb Natapovc5cc4212012-08-05 15:58:30 +03005907 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005908#ifdef CONFIG_X86_64
5909 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
5910#endif
5911
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005912 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005913
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005914out_free_percpu:
5915 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005916out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005917 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02005918}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005919
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005920void kvm_arch_exit(void)
5921{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005922 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5923
Jan Kiszka888d2562009-04-17 19:24:58 +02005924 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
5925 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
5926 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10005927 unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02005928#ifdef CONFIG_X86_64
5929 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
5930#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005931 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005932 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02005933 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08005934}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005935
Joel Schopp5cb56052015-03-02 13:43:31 -06005936int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05005937{
5938 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005939 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03005940 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005941 return 1;
5942 } else {
5943 vcpu->run->exit_reason = KVM_EXIT_HLT;
5944 return 0;
5945 }
5946}
Joel Schopp5cb56052015-03-02 13:43:31 -06005947EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
5948
5949int kvm_emulate_halt(struct kvm_vcpu *vcpu)
5950{
5951 kvm_x86_ops->skip_emulated_instruction(vcpu);
5952 return kvm_vcpu_halt(vcpu);
5953}
Hollis Blanchard8776e512007-10-31 17:24:24 -05005954EXPORT_SYMBOL_GPL(kvm_emulate_halt);
5955
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305956/*
5957 * kvm_pv_kick_cpu_op: Kick a vcpu.
5958 *
5959 * @apicid - apicid of vcpu to be kicked.
5960 */
5961static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
5962{
Raghavendra K T24d21662013-08-26 14:18:35 +05305963 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305964
Raghavendra K T24d21662013-08-26 14:18:35 +05305965 lapic_irq.shorthand = 0;
5966 lapic_irq.dest_mode = 0;
5967 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06005968 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305969
Raghavendra K T24d21662013-08-26 14:18:35 +05305970 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08005971 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05305972}
5973
Andrey Smetanind62caab2015-11-10 15:36:33 +03005974void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
5975{
5976 vcpu->arch.apicv_active = false;
5977 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
5978}
5979
Hollis Blanchard8776e512007-10-31 17:24:24 -05005980int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
5981{
5982 unsigned long nr, a0, a1, a2, a3, ret;
Nadav Amita449c7a2014-06-18 17:19:24 +03005983 int op_64_bit, r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05005984
Joel Schopp5cb56052015-03-02 13:43:31 -06005985 kvm_x86_ops->skip_emulated_instruction(vcpu);
5986
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005987 if (kvm_hv_hypercall_enabled(vcpu->kvm))
5988 return kvm_hv_hypercall(vcpu);
5989
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005990 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
5991 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
5992 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
5993 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
5994 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05005995
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005996 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005997
Nadav Amita449c7a2014-06-18 17:19:24 +03005998 op_64_bit = is_64_bit_mode(vcpu);
5999 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05006000 nr &= 0xFFFFFFFF;
6001 a0 &= 0xFFFFFFFF;
6002 a1 &= 0xFFFFFFFF;
6003 a2 &= 0xFFFFFFFF;
6004 a3 &= 0xFFFFFFFF;
6005 }
6006
Jan Kiszka07708c42009-08-03 18:43:28 +02006007 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
6008 ret = -KVM_EPERM;
6009 goto out;
6010 }
6011
Hollis Blanchard8776e512007-10-31 17:24:24 -05006012 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02006013 case KVM_HC_VAPIC_POLL_IRQ:
6014 ret = 0;
6015 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306016 case KVM_HC_KICK_CPU:
6017 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
6018 ret = 0;
6019 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05006020 default:
6021 ret = -KVM_ENOSYS;
6022 break;
6023 }
Jan Kiszka07708c42009-08-03 18:43:28 +02006024out:
Nadav Amita449c7a2014-06-18 17:19:24 +03006025 if (!op_64_bit)
6026 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006027 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05306028 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05006029 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05006030}
6031EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
6032
Jan Kiszkab6785de2012-09-20 07:43:17 +02006033static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05006034{
Avi Kivityd6aa1002011-04-20 15:47:13 +03006035 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006036 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006037 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006038
Hollis Blanchard8776e512007-10-31 17:24:24 -05006039 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006040
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006041 return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006042}
6043
Avi Kivity851ba692009-08-24 11:10:17 +03006044static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006045{
Matt Gingell782d4222015-11-16 15:26:00 -08006046 return vcpu->run->request_interrupt_window &&
6047 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006048}
6049
Avi Kivity851ba692009-08-24 11:10:17 +03006050static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006051{
Avi Kivity851ba692009-08-24 11:10:17 +03006052 struct kvm_run *kvm_run = vcpu->run;
6053
Jan Kiszka91586a32009-10-05 13:07:21 +02006054 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02006055 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006056 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006057 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01006058 kvm_run->ready_for_interrupt_injection =
6059 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08006060 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006061}
6062
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006063static void update_cr8_intercept(struct kvm_vcpu *vcpu)
6064{
6065 int max_irr, tpr;
6066
6067 if (!kvm_x86_ops->update_cr8_intercept)
6068 return;
6069
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006070 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03006071 return;
6072
Andrey Smetanind62caab2015-11-10 15:36:33 +03006073 if (vcpu->arch.apicv_active)
6074 return;
6075
Gleb Natapov8db3baa2009-05-11 13:35:54 +03006076 if (!vcpu->arch.apic->vapic_addr)
6077 max_irr = kvm_lapic_find_highest_irr(vcpu);
6078 else
6079 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006080
6081 if (max_irr != -1)
6082 max_irr >>= 4;
6083
6084 tpr = kvm_lapic_get_cr8(vcpu);
6085
6086 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
6087}
6088
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006089static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006090{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006091 int r;
6092
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006093 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006094 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02006095 trace_kvm_inj_exception(vcpu->arch.exception.nr,
6096 vcpu->arch.exception.has_error_code,
6097 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03006098
6099 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
6100 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
6101 X86_EFLAGS_RF);
6102
Nadav Amit6bdf0662014-09-30 20:49:14 +03006103 if (vcpu->arch.exception.nr == DB_VECTOR &&
6104 (vcpu->arch.dr7 & DR7_GD)) {
6105 vcpu->arch.dr7 &= ~DR7_GD;
6106 kvm_update_dr7(vcpu);
6107 }
6108
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006109 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
6110 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02006111 vcpu->arch.exception.error_code,
6112 vcpu->arch.exception.reinject);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006113 return 0;
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006114 }
6115
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006116 if (vcpu->arch.nmi_injected) {
6117 kvm_x86_ops->set_nmi(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006118 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006119 }
6120
6121 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006122 kvm_x86_ops->set_irq(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006123 return 0;
6124 }
6125
6126 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6127 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6128 if (r != 0)
6129 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006130 }
6131
6132 /* try to inject new event if pending */
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006133 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
6134 vcpu->arch.smi_pending = false;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006135 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006136 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00006137 --vcpu->arch.nmi_pending;
6138 vcpu->arch.nmi_injected = true;
6139 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006140 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04006141 /*
6142 * Because interrupts can be injected asynchronously, we are
6143 * calling check_nested_events again here to avoid a race condition.
6144 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
6145 * proposal and current concerns. Perhaps we should be setting
6146 * KVM_REQ_EVENT only on certain events and not unconditionally?
6147 */
6148 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6149 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6150 if (r != 0)
6151 return r;
6152 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006153 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006154 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
6155 false);
6156 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006157 }
6158 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006159
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006160 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006161}
6162
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006163static void process_nmi(struct kvm_vcpu *vcpu)
6164{
6165 unsigned limit = 2;
6166
6167 /*
6168 * x86 is limited to one NMI running, and one NMI pending after it.
6169 * If an NMI is already in progress, limit further NMIs to just one.
6170 * Otherwise, allow two (and we'll inject the first one immediately).
6171 */
6172 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
6173 limit = 1;
6174
6175 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
6176 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
6177 kvm_make_request(KVM_REQ_EVENT, vcpu);
6178}
6179
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006180#define put_smstate(type, buf, offset, val) \
6181 *(type *)((buf) + (offset) - 0x7e00) = val
6182
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006183static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006184{
6185 u32 flags = 0;
6186 flags |= seg->g << 23;
6187 flags |= seg->db << 22;
6188 flags |= seg->l << 21;
6189 flags |= seg->avl << 20;
6190 flags |= seg->present << 15;
6191 flags |= seg->dpl << 13;
6192 flags |= seg->s << 12;
6193 flags |= seg->type << 8;
6194 return flags;
6195}
6196
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006197static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006198{
6199 struct kvm_segment seg;
6200 int offset;
6201
6202 kvm_get_segment(vcpu, &seg, n);
6203 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
6204
6205 if (n < 3)
6206 offset = 0x7f84 + n * 12;
6207 else
6208 offset = 0x7f2c + (n - 3) * 12;
6209
6210 put_smstate(u32, buf, offset + 8, seg.base);
6211 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006212 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006213}
6214
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06006215#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006216static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006217{
6218 struct kvm_segment seg;
6219 int offset;
6220 u16 flags;
6221
6222 kvm_get_segment(vcpu, &seg, n);
6223 offset = 0x7e00 + n * 16;
6224
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006225 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006226 put_smstate(u16, buf, offset, seg.selector);
6227 put_smstate(u16, buf, offset + 2, flags);
6228 put_smstate(u32, buf, offset + 4, seg.limit);
6229 put_smstate(u64, buf, offset + 8, seg.base);
6230}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06006231#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006232
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006233static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006234{
6235 struct desc_ptr dt;
6236 struct kvm_segment seg;
6237 unsigned long val;
6238 int i;
6239
6240 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
6241 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
6242 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
6243 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
6244
6245 for (i = 0; i < 8; i++)
6246 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
6247
6248 kvm_get_dr(vcpu, 6, &val);
6249 put_smstate(u32, buf, 0x7fcc, (u32)val);
6250 kvm_get_dr(vcpu, 7, &val);
6251 put_smstate(u32, buf, 0x7fc8, (u32)val);
6252
6253 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6254 put_smstate(u32, buf, 0x7fc4, seg.selector);
6255 put_smstate(u32, buf, 0x7f64, seg.base);
6256 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006257 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006258
6259 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6260 put_smstate(u32, buf, 0x7fc0, seg.selector);
6261 put_smstate(u32, buf, 0x7f80, seg.base);
6262 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006263 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006264
6265 kvm_x86_ops->get_gdt(vcpu, &dt);
6266 put_smstate(u32, buf, 0x7f74, dt.address);
6267 put_smstate(u32, buf, 0x7f70, dt.size);
6268
6269 kvm_x86_ops->get_idt(vcpu, &dt);
6270 put_smstate(u32, buf, 0x7f58, dt.address);
6271 put_smstate(u32, buf, 0x7f54, dt.size);
6272
6273 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006274 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006275
6276 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
6277
6278 /* revision id */
6279 put_smstate(u32, buf, 0x7efc, 0x00020000);
6280 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
6281}
6282
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006283static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006284{
6285#ifdef CONFIG_X86_64
6286 struct desc_ptr dt;
6287 struct kvm_segment seg;
6288 unsigned long val;
6289 int i;
6290
6291 for (i = 0; i < 16; i++)
6292 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
6293
6294 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
6295 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
6296
6297 kvm_get_dr(vcpu, 6, &val);
6298 put_smstate(u64, buf, 0x7f68, val);
6299 kvm_get_dr(vcpu, 7, &val);
6300 put_smstate(u64, buf, 0x7f60, val);
6301
6302 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
6303 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
6304 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
6305
6306 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
6307
6308 /* revision id */
6309 put_smstate(u32, buf, 0x7efc, 0x00020064);
6310
6311 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
6312
6313 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
6314 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006315 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006316 put_smstate(u32, buf, 0x7e94, seg.limit);
6317 put_smstate(u64, buf, 0x7e98, seg.base);
6318
6319 kvm_x86_ops->get_idt(vcpu, &dt);
6320 put_smstate(u32, buf, 0x7e84, dt.size);
6321 put_smstate(u64, buf, 0x7e88, dt.address);
6322
6323 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
6324 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006325 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006326 put_smstate(u32, buf, 0x7e74, seg.limit);
6327 put_smstate(u64, buf, 0x7e78, seg.base);
6328
6329 kvm_x86_ops->get_gdt(vcpu, &dt);
6330 put_smstate(u32, buf, 0x7e64, dt.size);
6331 put_smstate(u64, buf, 0x7e68, dt.address);
6332
6333 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006334 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006335#else
6336 WARN_ON_ONCE(1);
6337#endif
6338}
6339
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006340static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02006341{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006342 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02006343 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006344 char buf[512];
6345 u32 cr0;
6346
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006347 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
6348 vcpu->arch.hflags |= HF_SMM_MASK;
6349 memset(buf, 0, 512);
6350 if (guest_cpuid_has_longmode(vcpu))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006351 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006352 else
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006353 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006354
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02006355 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006356
6357 if (kvm_x86_ops->get_nmi_mask(vcpu))
6358 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
6359 else
6360 kvm_x86_ops->set_nmi_mask(vcpu, true);
6361
6362 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6363 kvm_rip_write(vcpu, 0x8000);
6364
6365 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
6366 kvm_x86_ops->set_cr0(vcpu, cr0);
6367 vcpu->arch.cr0 = cr0;
6368
6369 kvm_x86_ops->set_cr4(vcpu, 0);
6370
Paolo Bonzini18c36262015-08-07 12:27:54 +02006371 /* Undocumented: IDT limit is set to zero on entry to SMM. */
6372 dt.address = dt.size = 0;
6373 kvm_x86_ops->set_idt(vcpu, &dt);
6374
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006375 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
6376
6377 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
6378 cs.base = vcpu->arch.smbase;
6379
6380 ds.selector = 0;
6381 ds.base = 0;
6382
6383 cs.limit = ds.limit = 0xffffffff;
6384 cs.type = ds.type = 0x3;
6385 cs.dpl = ds.dpl = 0;
6386 cs.db = ds.db = 0;
6387 cs.s = ds.s = 1;
6388 cs.l = ds.l = 0;
6389 cs.g = ds.g = 1;
6390 cs.avl = ds.avl = 0;
6391 cs.present = ds.present = 1;
6392 cs.unusable = ds.unusable = 0;
6393 cs.padding = ds.padding = 0;
6394
6395 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
6396 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
6397 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
6398 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
6399 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
6400 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
6401
6402 if (guest_cpuid_has_longmode(vcpu))
6403 kvm_x86_ops->set_efer(vcpu, 0);
6404
6405 kvm_update_cpuid(vcpu);
6406 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006407}
6408
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006409static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006410{
6411 vcpu->arch.smi_pending = true;
6412 kvm_make_request(KVM_REQ_EVENT, vcpu);
6413}
6414
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01006415void kvm_make_scan_ioapic_request(struct kvm *kvm)
6416{
6417 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
6418}
6419
Yang Zhang3d81bc72013-04-11 19:25:13 +08006420static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006421{
Andrey Smetanin5c9194122015-11-10 15:36:34 +03006422 u64 eoi_exit_bitmap[4];
6423
Yang Zhang3d81bc72013-04-11 19:25:13 +08006424 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
6425 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006426
Andrey Smetanin63086302015-11-10 15:36:32 +03006427 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006428
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07006429 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03006430 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02006431 else {
Andrey Smetanind62caab2015-11-10 15:36:33 +03006432 if (vcpu->arch.apicv_active)
6433 kvm_x86_ops->sync_pir_to_irr(vcpu);
Andrey Smetanin63086302015-11-10 15:36:32 +03006434 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02006435 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03006436 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
6437 vcpu_to_synic(vcpu)->vec_bitmap, 256);
6438 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006439}
6440
Radim Krčmářa70656b2014-09-18 12:38:36 -04006441static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
6442{
6443 ++vcpu->stat.tlb_flush;
6444 kvm_x86_ops->tlb_flush(vcpu);
6445}
6446
Tang Chen4256f432014-09-24 15:57:54 +08006447void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
6448{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006449 struct page *page = NULL;
6450
Paolo Bonzini35754c92015-07-29 12:05:37 +02006451 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02006452 return;
6453
Tang Chen4256f432014-09-24 15:57:54 +08006454 if (!kvm_x86_ops->set_apic_access_page_addr)
6455 return;
6456
Tang Chenc24ae0d2014-09-24 15:57:58 +08006457 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02006458 if (is_error_page(page))
6459 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08006460 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
6461
6462 /*
6463 * Do not pin apic access page in memory, the MMU notifier
6464 * will call us again if it is migrated or swapped out.
6465 */
6466 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08006467}
6468EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
6469
Tang Chenfe71557a2014-09-24 15:57:57 +08006470void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
6471 unsigned long address)
6472{
Tang Chenc24ae0d2014-09-24 15:57:58 +08006473 /*
6474 * The physical address of apic access page is stored in the VMCS.
6475 * Update it when it becomes invalid.
6476 */
6477 if (address == gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT))
6478 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Tang Chenfe71557a2014-09-24 15:57:57 +08006479}
6480
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006481/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01006482 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09006483 * exiting to the userspace. Otherwise, the value will be returned to the
6484 * userspace.
6485 */
Avi Kivity851ba692009-08-24 11:10:17 +03006486static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006487{
6488 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08006489 bool req_int_win =
6490 dm_request_for_irq_injection(vcpu) &&
6491 kvm_cpu_accept_dm_intr(vcpu);
6492
Jan Kiszka730dca42013-04-28 10:50:52 +02006493 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006494
Avi Kivity3e007502010-06-23 14:26:18 +03006495 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03006496 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05006497 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006498 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03006499 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006500 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
6501 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03006502 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
6503 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10006504 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
6505 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006506 if (unlikely(r))
6507 goto out;
6508 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006509 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03006510 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006511 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Radim Krčmářa70656b2014-09-18 12:38:36 -04006512 kvm_vcpu_flush_tlb(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03006513 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006514 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02006515 r = 0;
6516 goto out;
6517 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006518 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006519 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01006520 r = 0;
6521 goto out;
6522 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03006523 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02006524 vcpu->fpu_active = 0;
6525 kvm_x86_ops->fpu_deactivate(vcpu);
6526 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006527 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
6528 /* Page is swapped out. Do synthetic halt */
6529 vcpu->arch.apf.halted = true;
6530 r = 1;
6531 goto out;
6532 }
Glauber Costac9aaa892011-07-11 15:28:14 -04006533 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
6534 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006535 if (kvm_check_request(KVM_REQ_SMI, vcpu))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02006536 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03006537 if (kvm_check_request(KVM_REQ_NMI, vcpu))
6538 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006539 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006540 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02006541 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02006542 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07006543 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
6544 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
6545 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03006546 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07006547 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
6548 vcpu->run->eoi.vector =
6549 vcpu->arch.pending_ioapic_eoi;
6550 r = 0;
6551 goto out;
6552 }
6553 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08006554 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
6555 vcpu_scan_ioapic(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08006556 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
6557 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03006558 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
6559 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
6560 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
6561 r = 0;
6562 goto out;
6563 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03006564 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
6565 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
6566 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
6567 r = 0;
6568 goto out;
6569 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03006570 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
6571 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
6572 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
6573 r = 0;
6574 goto out;
6575 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03006576
6577 /*
6578 * KVM_REQ_HV_STIMER has to be processed after
6579 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
6580 * depend on the guest clock being up-to-date
6581 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03006582 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
6583 kvm_hv_process_stimers(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02006584 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006585
Feng Wubf9f6ac2015-09-18 22:29:55 +08006586 /*
6587 * KVM_REQ_EVENT is not set when posted interrupts are set by
6588 * VT-d hardware, so we have to update RVI unconditionally.
6589 */
6590 if (kvm_lapic_enabled(vcpu)) {
6591 /*
6592 * Update architecture specific hints for APIC
6593 * virtual interrupt delivery.
6594 */
Andrey Smetanind62caab2015-11-10 15:36:33 +03006595 if (vcpu->arch.apicv_active)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006596 kvm_x86_ops->hwapic_irr_update(vcpu,
6597 kvm_lapic_find_highest_irr(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006598 }
6599
Avi Kivityb463a6f2010-07-20 15:06:17 +03006600 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Jan Kiszka66450a22013-03-13 12:42:34 +01006601 kvm_apic_accept_events(vcpu);
6602 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
6603 r = 1;
6604 goto out;
6605 }
6606
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006607 if (inject_pending_event(vcpu, req_int_win) != 0)
6608 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00006609 else {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006610 /* Enable NMI/IRQ window open exits if needed.
6611 *
6612 * SMIs have two cases: 1) they can be nested, and
6613 * then there is nothing to do here because RSM will
6614 * cause a vmexit anyway; 2) or the SMI can be pending
6615 * because inject_pending_event has completed the
6616 * injection of an IRQ or NMI from the previous vmexit,
6617 * and then we request an immediate exit to inject the SMI.
6618 */
6619 if (vcpu->arch.smi_pending && !is_smm(vcpu))
6620 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00006621 if (vcpu->arch.nmi_pending)
6622 kvm_x86_ops->enable_nmi_window(vcpu);
6623 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
6624 kvm_x86_ops->enable_irq_window(vcpu);
6625 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03006626
Avi Kivity3842d132010-07-27 12:30:24 +03006627 if (kvm_lapic_enabled(vcpu)) {
6628 update_cr8_intercept(vcpu);
6629 kvm_lapic_sync_to_vapic(vcpu);
6630 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006631 }
Avi Kivityb93463a2007-10-25 16:52:32 +02006632
Avi Kivityd8368af2012-05-14 18:07:56 +03006633 r = kvm_mmu_reload(vcpu);
6634 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006635 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03006636 }
6637
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006638 preempt_disable();
6639
6640 kvm_x86_ops->prepare_guest_switch(vcpu);
6641 if (vcpu->fpu_active)
6642 kvm_load_guest_fpu(vcpu);
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006643 vcpu->mode = IN_GUEST_MODE;
6644
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006645 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6646
Lan Tianyu0f127d12016-03-13 11:10:29 +08006647 /*
6648 * We should set ->mode before check ->requests,
6649 * Please see the comment in kvm_make_all_cpus_request.
6650 * This also orders the write to mode from any reads
6651 * to the page tables done while the VCPU is running.
6652 * Please see the comment in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006653 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006654 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006655
6656 local_irq_disable();
6657
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006658 if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006659 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006660 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006661 smp_wmb();
6662 local_irq_enable();
6663 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02006664 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006665 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006666 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006667 }
6668
David Matlackfc5b7f32016-03-30 12:24:47 -07006669 kvm_load_guest_xcr0(vcpu);
6670
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006671 if (req_immediate_exit) {
6672 kvm_make_request(KVM_REQ_EVENT, vcpu);
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006673 smp_send_reschedule(vcpu->cpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006674 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03006675
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01006676 trace_kvm_entry(vcpu->vcpu_id);
6677 wait_lapic_expire(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02006678 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006679
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006680 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006681 set_debugreg(0, 7);
6682 set_debugreg(vcpu->arch.eff_db[0], 0);
6683 set_debugreg(vcpu->arch.eff_db[1], 1);
6684 set_debugreg(vcpu->arch.eff_db[2], 2);
6685 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006686 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03006687 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006688 }
6689
Avi Kivity851ba692009-08-24 11:10:17 +03006690 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006691
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006692 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006693 * Do this here before restoring debug registers on the host. And
6694 * since we do this before handling the vmexit, a DR access vmexit
6695 * can (a) read the correct value of the debug registers, (b) set
6696 * KVM_DEBUGREG_WONT_EXIT again.
6697 */
6698 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006699 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
6700 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01006701 kvm_update_dr0123(vcpu);
6702 kvm_update_dr6(vcpu);
6703 kvm_update_dr7(vcpu);
6704 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01006705 }
6706
6707 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006708 * If the guest has used debug registers, at least dr7
6709 * will be disabled while returning to the host.
6710 * If we don't have active breakpoints in the host, we don't
6711 * care about the messed up debug address registers. But if
6712 * we have some of them active, restore the old state.
6713 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01006714 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006715 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006716
Haozhong Zhang4ba76532015-10-20 15:39:07 +08006717 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10006718
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08006719 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03006720 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006721
David Matlackfc5b7f32016-03-30 12:24:47 -07006722 kvm_put_guest_xcr0(vcpu);
6723
Yang Zhanga547c6d2013-04-11 19:25:10 +08006724 /* Interrupt is enabled by handle_external_intr() */
6725 kvm_x86_ops->handle_external_intr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006726
6727 ++vcpu->stat.exits;
6728
Paolo Bonzinif2485b32016-06-15 15:23:11 +02006729 guest_exit_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006730
Paolo Bonzinif2485b32016-06-15 15:23:11 +02006731 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006732 preempt_enable();
6733
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006734 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03006735
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006736 /*
6737 * Profile KVM exit RIPs:
6738 */
6739 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006740 unsigned long rip = kvm_rip_read(vcpu);
6741 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006742 }
6743
Zachary Amsdencc578282012-02-03 15:43:50 -02006744 if (unlikely(vcpu->arch.tsc_always_catchup))
6745 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02006746
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03006747 if (vcpu->arch.apic_attention)
6748 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02006749
Avi Kivity851ba692009-08-24 11:10:17 +03006750 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03006751 return r;
6752
6753cancel_injection:
6754 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03006755 if (unlikely(vcpu->arch.apic_attention))
6756 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006757out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03006758 return r;
6759}
6760
Paolo Bonzini362c6982015-02-06 12:48:04 +01006761static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
6762{
Feng Wubf9f6ac2015-09-18 22:29:55 +08006763 if (!kvm_arch_vcpu_runnable(vcpu) &&
6764 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01006765 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
6766 kvm_vcpu_block(vcpu);
6767 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006768
6769 if (kvm_x86_ops->post_block)
6770 kvm_x86_ops->post_block(vcpu);
6771
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01006772 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
6773 return 1;
6774 }
Gleb Natapov09cec752009-03-23 15:11:44 +02006775
Paolo Bonzini362c6982015-02-06 12:48:04 +01006776 kvm_apic_accept_events(vcpu);
6777 switch(vcpu->arch.mp_state) {
6778 case KVM_MP_STATE_HALTED:
6779 vcpu->arch.pv.pv_unhalted = false;
6780 vcpu->arch.mp_state =
6781 KVM_MP_STATE_RUNNABLE;
6782 case KVM_MP_STATE_RUNNABLE:
6783 vcpu->arch.apf.halted = false;
6784 break;
6785 case KVM_MP_STATE_INIT_RECEIVED:
6786 break;
6787 default:
6788 return -EINTR;
6789 break;
6790 }
6791 return 1;
6792}
6793
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02006794static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
6795{
6796 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
6797 !vcpu->arch.apf.halted);
6798}
6799
Paolo Bonzini362c6982015-02-06 12:48:04 +01006800static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03006801{
6802 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006803 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03006804
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006805 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006806
Paolo Bonzini362c6982015-02-06 12:48:04 +01006807 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02006808 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006809 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006810 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01006811 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006812 }
6813
Gleb Natapov09cec752009-03-23 15:11:44 +02006814 if (r <= 0)
6815 break;
6816
6817 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
6818 if (kvm_cpu_has_pending_timer(vcpu))
6819 kvm_inject_pending_timer_irqs(vcpu);
6820
Matt Gingell782d4222015-11-16 15:26:00 -08006821 if (dm_request_for_irq_injection(vcpu) &&
6822 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02006823 r = 0;
6824 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02006825 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006826 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006827 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02006828
6829 kvm_check_async_pf_completion(vcpu);
6830
Gleb Natapov09cec752009-03-23 15:11:44 +02006831 if (signal_pending(current)) {
6832 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03006833 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02006834 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01006835 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02006836 }
6837 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006838 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09006839 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006840 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006841 }
6842 }
6843
Marcelo Tosattif656ce02009-12-23 14:35:25 -02006844 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006845
6846 return r;
6847}
6848
Gleb Natapov716d51a2012-09-03 15:24:26 +03006849static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
6850{
6851 int r;
6852 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6853 r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
6854 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
6855 if (r != EMULATE_DONE)
6856 return 0;
6857 return 1;
6858}
6859
6860static int complete_emulated_pio(struct kvm_vcpu *vcpu)
6861{
6862 BUG_ON(!vcpu->arch.pio.count);
6863
6864 return complete_emulated_io(vcpu);
6865}
6866
Avi Kivityf78146b2012-04-18 19:22:47 +03006867/*
6868 * Implements the following, as a state machine:
6869 *
6870 * read:
6871 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006872 * for each mmio piece in the fragment
6873 * write gpa, len
6874 * exit
6875 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03006876 * execute insn
6877 *
6878 * write:
6879 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006880 * for each mmio piece in the fragment
6881 * write gpa, len
6882 * copy data
6883 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03006884 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03006885static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02006886{
6887 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03006888 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006889 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02006890
Gleb Natapov716d51a2012-09-03 15:24:26 +03006891 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006892
Gleb Natapov716d51a2012-09-03 15:24:26 +03006893 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006894 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
6895 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006896 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006897 memcpy(frag->data, run->mmio.data, len);
6898
6899 if (frag->len <= 8) {
6900 /* Switch to the next fragment. */
6901 frag++;
6902 vcpu->mmio_cur_fragment++;
6903 } else {
6904 /* Go forward to the next mmio piece. */
6905 frag->data += len;
6906 frag->gpa += len;
6907 frag->len -= len;
6908 }
6909
Andrew Honiga08d3b32014-02-27 19:35:14 +01006910 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03006911 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006912
6913 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02006914 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03006915 return 1;
6916 vcpu->mmio_read_completed = 1;
6917 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02006918 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006919
Gleb Natapov716d51a2012-09-03 15:24:26 +03006920 run->exit_reason = KVM_EXIT_MMIO;
6921 run->mmio.phys_addr = frag->gpa;
6922 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08006923 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
6924 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03006925 run->mmio.is_write = vcpu->mmio_is_write;
6926 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
6927 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02006928}
6929
Gleb Natapov716d51a2012-09-03 15:24:26 +03006930
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006931int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6932{
Ingo Molnarc5bedc62015-04-23 12:49:20 +02006933 struct fpu *fpu = &current->thread.fpu;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006934 int r;
6935 sigset_t sigsaved;
6936
Ingo Molnarc4d72e22015-04-27 07:18:17 +02006937 fpu__activate_curr(fpu);
Avi Kivitye5c30142011-01-11 12:15:54 +02006938
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006939 if (vcpu->sigset_active)
6940 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
6941
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006942 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
6943 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01006944 kvm_apic_accept_events(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03006945 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03006946 r = -EAGAIN;
6947 goto out;
6948 }
6949
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006950 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02006951 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006952 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
6953 r = -EINVAL;
6954 goto out;
6955 }
6956 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006957
Gleb Natapov716d51a2012-09-03 15:24:26 +03006958 if (unlikely(vcpu->arch.complete_userspace_io)) {
6959 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
6960 vcpu->arch.complete_userspace_io = NULL;
6961 r = cui(vcpu);
6962 if (r <= 0)
6963 goto out;
6964 } else
6965 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02006966
Paolo Bonzini362c6982015-02-06 12:48:04 +01006967 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006968
6969out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03006970 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006971 if (vcpu->sigset_active)
6972 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
6973
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006974 return r;
6975}
6976
6977int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
6978{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006979 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
6980 /*
6981 * We are here if userspace calls get_regs() in the middle of
6982 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08006983 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006984 * that usually, but some bad designed PV devices (vmware
6985 * backdoor interface) need this to work
6986 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03006987 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006988 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
6989 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006990 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
6991 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
6992 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
6993 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
6994 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
6995 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
6996 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6997 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006998#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006999 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
7000 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
7001 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
7002 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
7003 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
7004 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
7005 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
7006 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007007#endif
7008
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007009 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02007010 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007011
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007012 return 0;
7013}
7014
7015int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
7016{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007017 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
7018 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
7019
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007020 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
7021 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
7022 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
7023 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
7024 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
7025 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
7026 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
7027 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007028#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007029 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
7030 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
7031 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
7032 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
7033 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
7034 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
7035 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
7036 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007037#endif
7038
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007039 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02007040 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007041
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02007042 vcpu->arch.exception.pending = false;
7043
Avi Kivity3842d132010-07-27 12:30:24 +03007044 kvm_make_request(KVM_REQ_EVENT, vcpu);
7045
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007046 return 0;
7047}
7048
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007049void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
7050{
7051 struct kvm_segment cs;
7052
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007053 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007054 *db = cs.db;
7055 *l = cs.l;
7056}
7057EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
7058
7059int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
7060 struct kvm_sregs *sregs)
7061{
Gleb Natapov89a27f42010-02-16 10:51:48 +02007062 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007063
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007064 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
7065 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
7066 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
7067 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
7068 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
7069 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007070
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007071 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
7072 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007073
7074 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02007075 sregs->idt.limit = dt.size;
7076 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007077 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02007078 sregs->gdt.limit = dt.size;
7079 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007080
Avi Kivity4d4ec082009-12-29 18:07:30 +02007081 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007082 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02007083 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02007084 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007085 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02007086 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007087 sregs->apic_base = kvm_get_apic_base(vcpu);
7088
Gleb Natapov923c61b2009-05-11 13:35:48 +03007089 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007090
Gleb Natapov36752c92009-05-11 13:35:53 +03007091 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03007092 set_bit(vcpu->arch.interrupt.nr,
7093 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03007094
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007095 return 0;
7096}
7097
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03007098int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
7099 struct kvm_mp_state *mp_state)
7100{
Jan Kiszka66450a22013-03-13 12:42:34 +01007101 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307102 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
7103 vcpu->arch.pv.pv_unhalted)
7104 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
7105 else
7106 mp_state->mp_state = vcpu->arch.mp_state;
7107
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03007108 return 0;
7109}
7110
7111int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
7112 struct kvm_mp_state *mp_state)
7113{
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007114 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01007115 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
7116 return -EINVAL;
7117
7118 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
7119 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
7120 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
7121 } else
7122 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03007123 kvm_make_request(KVM_REQ_EVENT, vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03007124 return 0;
7125}
7126
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007127int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
7128 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007129{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007130 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007131 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007132
Mohammed Gamal8ec47222010-08-16 00:47:01 +03007133 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007134
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007135 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007136 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02007137
Gleb Natapovc6975182010-02-18 12:15:01 +02007138 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03007139 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02007140
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09007141 kvm_rip_write(vcpu, ctxt->eip);
7142 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03007143 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03007144 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02007145}
7146EXPORT_SYMBOL_GPL(kvm_task_switch);
7147
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007148int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
7149 struct kvm_sregs *sregs)
7150{
Jan Kiszka58cb6282014-01-24 16:48:44 +01007151 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007152 int mmu_reset_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007153 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02007154 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007155
Petr Matousek6d1068b2012-11-06 19:24:07 +01007156 if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
7157 return -EINVAL;
7158
Gleb Natapov89a27f42010-02-16 10:51:48 +02007159 dt.size = sregs->idt.limit;
7160 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007161 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02007162 dt.size = sregs->gdt.limit;
7163 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007164 kvm_x86_ops->set_gdt(vcpu, &dt);
7165
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007166 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02007167 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02007168 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02007169 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007170
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007171 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007172
Avi Kivityf6801df2010-01-21 15:31:50 +02007173 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007174 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Jan Kiszka58cb6282014-01-24 16:48:44 +01007175 apic_base_msr.data = sregs->apic_base;
7176 apic_base_msr.host_initiated = true;
7177 kvm_set_apic_base(vcpu, &apic_base_msr);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007178
Avi Kivity4d4ec082009-12-29 18:07:30 +02007179 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007180 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00007181 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007182
Avi Kivityfc78f512009-12-07 12:16:48 +02007183 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007184 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Huaitong Hanb9baba82016-03-22 16:51:21 +08007185 if (sregs->cr4 & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +02007186 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007187
7188 idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02007189 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02007190 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02007191 mmu_reset_needed = 1;
7192 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08007193 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007194
7195 if (mmu_reset_needed)
7196 kvm_mmu_reset_context(vcpu);
7197
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03007198 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03007199 pending_vec = find_first_bit(
7200 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
7201 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007202 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03007203 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007204 }
7205
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007206 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
7207 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
7208 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
7209 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
7210 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
7211 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007212
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007213 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
7214 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007215
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03007216 update_cr8_intercept(vcpu);
7217
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007218 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03007219 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007220 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02007221 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03007222 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
7223
Avi Kivity3842d132010-07-27 12:30:24 +03007224 kvm_make_request(KVM_REQ_EVENT, vcpu);
7225
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007226 return 0;
7227}
7228
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007229int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
7230 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007231{
Jan Kiszka355be0b2009-10-03 00:31:21 +02007232 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007233 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007234
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007235 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
7236 r = -EBUSY;
7237 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03007238 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007239 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
7240 kvm_queue_exception(vcpu, DB_VECTOR);
7241 else
7242 kvm_queue_exception(vcpu, BP_VECTOR);
7243 }
7244
Jan Kiszka91586a32009-10-05 13:07:21 +02007245 /*
7246 * Read rflags as long as potentially injected trace flags are still
7247 * filtered out.
7248 */
7249 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02007250
7251 vcpu->guest_debug = dbg->control;
7252 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
7253 vcpu->guest_debug = 0;
7254
7255 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007256 for (i = 0; i < KVM_NR_DB_REGS; ++i)
7257 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02007258 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007259 } else {
7260 for (i = 0; i < KVM_NR_DB_REGS; i++)
7261 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007262 }
Jan Kiszkac8639012012-09-21 05:42:55 +02007263 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01007264
Jan Kiszkaf92653e2010-02-23 17:47:55 +01007265 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7266 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
7267 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007268
Jan Kiszka91586a32009-10-05 13:07:21 +02007269 /*
7270 * Trigger an rflags update that will inject or remove the trace
7271 * flags.
7272 */
7273 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007274
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007275 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02007276
Jan Kiszka4f926bf22009-10-30 12:46:59 +01007277 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007278
Avi Kivity2122ff52010-05-13 11:25:04 +03007279out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007280
7281 return r;
7282}
7283
7284/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08007285 * Translate a guest virtual address to a guest physical address.
7286 */
7287int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
7288 struct kvm_translation *tr)
7289{
7290 unsigned long vaddr = tr->linear_address;
7291 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007292 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007293
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007294 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02007295 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007296 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08007297 tr->physical_address = gpa;
7298 tr->valid = gpa != UNMAPPED_GVA;
7299 tr->writeable = 1;
7300 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08007301
7302 return 0;
7303}
7304
Hollis Blanchardd0752062007-10-31 17:24:25 -05007305int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7306{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007307 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007308 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007309
Hollis Blanchardd0752062007-10-31 17:24:25 -05007310 memcpy(fpu->fpr, fxsave->st_space, 128);
7311 fpu->fcw = fxsave->cwd;
7312 fpu->fsw = fxsave->swd;
7313 fpu->ftwx = fxsave->twd;
7314 fpu->last_opcode = fxsave->fop;
7315 fpu->last_ip = fxsave->rip;
7316 fpu->last_dp = fxsave->rdp;
7317 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
7318
Hollis Blanchardd0752062007-10-31 17:24:25 -05007319 return 0;
7320}
7321
7322int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
7323{
Ingo Molnarc47ada32015-04-30 17:15:32 +02007324 struct fxregs_state *fxsave =
Ingo Molnar7366ed72015-04-27 04:19:39 +02007325 &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007326
Hollis Blanchardd0752062007-10-31 17:24:25 -05007327 memcpy(fxsave->st_space, fpu->fpr, 128);
7328 fxsave->cwd = fpu->fcw;
7329 fxsave->swd = fpu->fsw;
7330 fxsave->twd = fpu->ftwx;
7331 fxsave->fop = fpu->last_opcode;
7332 fxsave->rip = fpu->last_ip;
7333 fxsave->rdp = fpu->last_dp;
7334 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
7335
Hollis Blanchardd0752062007-10-31 17:24:25 -05007336 return 0;
7337}
7338
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007339static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007340{
Ingo Molnarbf935b02015-04-30 10:23:42 +02007341 fpstate_init(&vcpu->arch.guest_fpu.state);
Borislav Petkov782511b2016-04-04 22:25:03 +02007342 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar7366ed72015-04-27 04:19:39 +02007343 vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01007344 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007345
Dexuan Cui2acf9232010-06-10 11:27:12 +08007346 /*
7347 * Ensure guest xcr0 is valid for loading
7348 */
Dave Hansend91cab72015-09-02 16:31:26 -07007349 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08007350
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007351 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007352}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007353
7354void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
7355{
Avi Kivity2608d7a2010-01-21 15:31:45 +02007356 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05007357 return;
7358
Dexuan Cui2acf9232010-06-10 11:27:12 +08007359 /*
7360 * Restore all possible states in the guest,
7361 * and assume host would use all available bits.
7362 * Guest xcr0 would be loaded later.
7363 */
Hollis Blanchardd0752062007-10-31 17:24:25 -05007364 vcpu->guest_fpu_loaded = 1;
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007365 __kernel_fpu_begin();
Ingo Molnar003e2e82015-05-25 11:59:35 +02007366 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state);
Avi Kivity0c048512010-01-21 15:31:52 +02007367 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007368}
Hollis Blanchardd0752062007-10-31 17:24:25 -05007369
7370void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
7371{
Rik van Riel653f52c2015-04-23 11:52:37 -04007372 if (!vcpu->guest_fpu_loaded) {
7373 vcpu->fpu_counter = 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05007374 return;
Rik van Riel653f52c2015-04-23 11:52:37 -04007375 }
Hollis Blanchardd0752062007-10-31 17:24:25 -05007376
7377 vcpu->guest_fpu_loaded = 0;
Ingo Molnar4f836342015-04-27 02:53:16 +02007378 copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07007379 __kernel_fpu_end();
Avi Kivityf096ed82007-11-18 13:54:33 +02007380 ++vcpu->stat.fpu_reload;
Rik van Riel653f52c2015-04-23 11:52:37 -04007381 /*
7382 * If using eager FPU mode, or if the guest is a frequent user
7383 * of the FPU, just leave the FPU active for next time.
7384 * Every 255 times fpu_counter rolls over to 0; a guest that uses
7385 * the FPU in bursts will revert to loading it on demand.
7386 */
Paolo Bonzini5a5fbdc2016-03-08 10:00:11 +01007387 if (!use_eager_fpu()) {
Rik van Riel653f52c2015-04-23 11:52:37 -04007388 if (++vcpu->fpu_counter < 5)
7389 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
7390 }
Avi Kivity0c048512010-01-21 15:31:52 +02007391 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05007392}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007393
7394void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
7395{
Glauber Costa12f9a482011-02-01 14:16:40 -05007396 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01007397
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007398 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007399 kvm_x86_ops->vcpu_free(vcpu);
7400}
7401
7402struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
7403 unsigned int id)
7404{
Liang Lic447e762015-05-21 04:41:25 +08007405 struct kvm_vcpu *vcpu;
7406
Zachary Amsden6755bae2010-08-19 22:07:22 -10007407 if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
7408 printk_once(KERN_WARNING
7409 "kvm: SMP vm created on host with unstable TSC; "
7410 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08007411
7412 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
7413
Liang Lic447e762015-05-21 04:41:25 +08007414 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02007415}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007416
Avi Kivity26e52152007-11-20 15:30:24 +02007417int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
7418{
7419 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007420
Xiao Guangrong19efffa2015-06-15 16:55:31 +08007421 kvm_vcpu_mtrr_init(vcpu);
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007422 r = vcpu_load(vcpu);
7423 if (r)
7424 return r;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007425 kvm_vcpu_reset(vcpu, false);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007426 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007427 vcpu_put(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02007428 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007429}
7430
Dominik Dingel31928aa2014-12-04 15:47:07 +01007431void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007432{
Will Auld8fe8ab42012-11-29 12:42:12 -08007433 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01007434 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007435
Dominik Dingel31928aa2014-12-04 15:47:07 +01007436 if (vcpu_load(vcpu))
7437 return;
Will Auld8fe8ab42012-11-29 12:42:12 -08007438 msr.data = 0x0;
7439 msr.index = MSR_IA32_TSC;
7440 msr.host_initiated = true;
7441 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007442 vcpu_put(vcpu);
7443
Marcelo Tosatti630994b2015-05-12 22:42:04 -03007444 if (!kvmclock_periodic_sync)
7445 return;
7446
Andrew Jones332967a2014-02-28 12:52:55 +01007447 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
7448 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02007449}
7450
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06007451void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007452{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007453 int r;
Gleb Natapov344d9582010-10-14 11:22:50 +02007454 vcpu->arch.apf.msr_val = 0;
7455
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007456 r = vcpu_load(vcpu);
7457 BUG_ON(r);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007458 kvm_mmu_unload(vcpu);
7459 vcpu_put(vcpu);
7460
7461 kvm_x86_ops->vcpu_free(vcpu);
7462}
7463
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007464void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007465{
Paolo Bonzinie69fab52015-06-04 10:44:44 +02007466 vcpu->arch.hflags = 0;
7467
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007468 vcpu->arch.smi_pending = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007469 atomic_set(&vcpu->arch.nmi_queued, 0);
7470 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007471 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03007472 kvm_clear_interrupt_queue(vcpu);
7473 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02007474
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007475 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03007476 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03007477 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007478 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007479 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02007480 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007481
Nadav Amit11190222015-04-02 03:10:38 +03007482 vcpu->arch.cr2 = 0;
7483
Avi Kivity3842d132010-07-27 12:30:24 +03007484 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02007485 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04007486 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03007487
Glauber Costa12f9a482011-02-01 14:16:40 -05007488 kvmclock_reset(vcpu);
7489
Gleb Natapovaf585b92010-10-14 11:22:46 +02007490 kvm_clear_async_pf_completion_queue(vcpu);
7491 kvm_async_pf_hash_reset(vcpu);
7492 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007493
Paolo Bonzini64d60672015-05-07 11:36:11 +02007494 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007495 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007496 vcpu->arch.smbase = 0x30000;
7497 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02007498
Julian Stecklina66f7b722012-12-05 15:26:19 +01007499 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
7500 vcpu->arch.regs_avail = ~0;
7501 vcpu->arch.regs_dirty = ~0;
7502
Nadav Amitd28bc9d2015-04-13 14:34:08 +03007503 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007504}
7505
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01007506void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01007507{
7508 struct kvm_segment cs;
7509
7510 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
7511 cs.selector = vector << 8;
7512 cs.base = vector << 12;
7513 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7514 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007515}
7516
Radim Krčmář13a34e02014-08-28 15:13:03 +02007517int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007518{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10007519 struct kvm *kvm;
7520 struct kvm_vcpu *vcpu;
7521 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007522 int ret;
7523 u64 local_tsc;
7524 u64 max_tsc = 0;
7525 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03007526
7527 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02007528 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007529 if (ret != 0)
7530 return ret;
7531
Andy Lutomirski4ea16362015-06-25 18:44:07 +02007532 local_tsc = rdtsc();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007533 stable = !check_tsc_unstable();
7534 list_for_each_entry(kvm, &vm_list, vm_list) {
7535 kvm_for_each_vcpu(i, vcpu, kvm) {
7536 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007537 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007538 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
7539 backwards_tsc = true;
7540 if (vcpu->arch.last_host_tsc > max_tsc)
7541 max_tsc = vcpu->arch.last_host_tsc;
7542 }
7543 }
7544 }
7545
7546 /*
7547 * Sometimes, even reliable TSCs go backwards. This happens on
7548 * platforms that reset TSC during suspend or hibernate actions, but
7549 * maintain synchronization. We must compensate. Fortunately, we can
7550 * detect that condition here, which happens early in CPU bringup,
7551 * before any KVM threads can be running. Unfortunately, we can't
7552 * bring the TSCs fully up to date with real time, as we aren't yet far
7553 * enough into CPU bringup that we know how much real time has actually
7554 * elapsed; our helper function, get_kernel_ns() will be using boot
7555 * variables that haven't been updated yet.
7556 *
7557 * So we simply find the maximum observed TSC above, then record the
7558 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
7559 * the adjustment will be applied. Note that we accumulate
7560 * adjustments, in case multiple suspend cycles happen before some VCPU
7561 * gets a chance to run again. In the event that no KVM threads get a
7562 * chance to run, we will miss the entire elapsed period, as we'll have
7563 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
7564 * loose cycle time. This isn't too big a deal, since the loss will be
7565 * uniform across all VCPUs (not to mention the scenario is extremely
7566 * unlikely). It is possible that a second hibernate recovery happens
7567 * much faster than a first, causing the observed TSC here to be
7568 * smaller; this would require additional padding adjustment, which is
7569 * why we set last_host_tsc to the local tsc observed here.
7570 *
7571 * N.B. - this code below runs only on platforms with reliable TSC,
7572 * as that is the only way backwards_tsc is set above. Also note
7573 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
7574 * have the same delta_cyc adjustment applied if backwards_tsc
7575 * is detected. Note further, this adjustment is only done once,
7576 * as we reset last_host_tsc on all VCPUs to stop this from being
7577 * called multiple times (one for each physical CPU bringup).
7578 *
Guo Chao4a969982012-06-28 15:17:27 +08007579 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007580 * will be compensated by the logic in vcpu_load, which sets the TSC to
7581 * catchup mode. This will catchup all VCPUs to real time, but cannot
7582 * guarantee that they stay in perfect synchronization.
7583 */
7584 if (backwards_tsc) {
7585 u64 delta_cyc = max_tsc - local_tsc;
Marcelo Tosatti16a96022014-05-14 12:43:24 -03007586 backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007587 list_for_each_entry(kvm, &vm_list, vm_list) {
7588 kvm_for_each_vcpu(i, vcpu, kvm) {
7589 vcpu->arch.tsc_offset_adjustment += delta_cyc;
7590 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007591 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02007592 }
7593
7594 /*
7595 * We have to disable TSC offset matching.. if you were
7596 * booting a VM while issuing an S4 host suspend....
7597 * you may have some problem. Solving this issue is
7598 * left as an exercise to the reader.
7599 */
7600 kvm->arch.last_tsc_nsec = 0;
7601 kvm->arch.last_tsc_write = 0;
7602 }
7603
7604 }
7605 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007606}
7607
Radim Krčmář13a34e02014-08-28 15:13:03 +02007608void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007609{
Radim Krčmář13a34e02014-08-28 15:13:03 +02007610 kvm_x86_ops->hardware_disable();
7611 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007612}
7613
7614int kvm_arch_hardware_setup(void)
7615{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03007616 int r;
7617
7618 r = kvm_x86_ops->hardware_setup();
7619 if (r != 0)
7620 return r;
7621
Haozhong Zhang35181e82015-10-20 15:39:03 +08007622 if (kvm_has_tsc_control) {
7623 /*
7624 * Make sure the user can only configure tsc_khz values that
7625 * fit into a signed integer.
7626 * A min value is not calculated needed because it will always
7627 * be 1 on all machines.
7628 */
7629 u64 max = min(0x7fffffffULL,
7630 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
7631 kvm_max_guest_tsc_khz = max;
7632
Haozhong Zhangad7218832015-10-20 15:39:02 +08007633 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08007634 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08007635
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03007636 kvm_init_msr_list();
7637 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007638}
7639
7640void kvm_arch_hardware_unsetup(void)
7641{
7642 kvm_x86_ops->hardware_unsetup();
7643}
7644
7645void kvm_arch_check_processor_compat(void *rtn)
7646{
7647 kvm_x86_ops->check_processor_compatibility(rtn);
7648}
7649
Paolo Bonzinid71ba782015-07-29 11:56:48 +02007650bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
7651{
7652 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
7653}
7654EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
7655
7656bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
7657{
7658 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
7659}
7660
Gleb Natapov54e98182012-08-05 15:58:32 +03007661struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007662EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03007663
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007664int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
7665{
7666 struct page *page;
7667 struct kvm *kvm;
7668 int r;
7669
7670 BUG_ON(vcpu->kvm == NULL);
7671 kvm = vcpu->kvm;
7672
Andrey Smetanind62caab2015-11-10 15:36:33 +03007673 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv();
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307674 vcpu->arch.pv.pv_unhalted = false;
Avi Kivity9aabc88f2010-07-29 15:11:50 +03007675 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
Nadav Amit58d269d2015-04-02 03:10:36 +03007676 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03007677 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007678 else
Avi Kivitya4535292008-04-13 17:54:35 +03007679 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007680
7681 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
7682 if (!page) {
7683 r = -ENOMEM;
7684 goto fail;
7685 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007686 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007687
Zachary Amsdencc578282012-02-03 15:43:50 -02007688 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007689
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007690 r = kvm_mmu_create(vcpu);
7691 if (r < 0)
7692 goto fail_free_pio_data;
7693
7694 if (irqchip_in_kernel(kvm)) {
7695 r = kvm_create_lapic(vcpu);
7696 if (r < 0)
7697 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03007698 } else
7699 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007700
Huang Ying890ca9a2009-05-11 16:48:15 +08007701 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
7702 GFP_KERNEL);
7703 if (!vcpu->arch.mce_banks) {
7704 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08007705 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08007706 }
7707 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
7708
Wei Yongjunf1797352013-04-18 07:41:00 +08007709 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
7710 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007711 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08007712 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007713
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007714 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01007715
Will Auldba904632012-11-29 12:42:50 -08007716 vcpu->arch.ia32_tsc_adjust_msr = 0x0;
Andy Honig0b794592013-02-20 14:48:10 -08007717 vcpu->arch.pv_time_enabled = false;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007718
7719 vcpu->arch.guest_supported_xcr0 = 0;
Paolo Bonzini4344ee92013-10-02 16:06:16 +02007720 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02007721
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03007722 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
7723
Radim Krčmář74545702015-04-27 15:11:25 +02007724 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
7725
Gleb Natapovaf585b92010-10-14 11:22:46 +02007726 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007727 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007728
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02007729 vcpu->arch.pending_external_vector = -1;
7730
Andrey Smetanin5c9194122015-11-10 15:36:34 +03007731 kvm_hv_vcpu_init(vcpu);
7732
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007733 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02007734
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007735fail_free_mce_banks:
7736 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08007737fail_free_lapic:
7738 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007739fail_mmu_destroy:
7740 kvm_mmu_destroy(vcpu);
7741fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007742 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007743fail:
7744 return r;
7745}
7746
7747void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
7748{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007749 int idx;
7750
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03007751 kvm_hv_vcpu_uninit(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007752 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08007753 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007754 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007755 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007756 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007757 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007758 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007759 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03007760 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08007761}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007762
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007763void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
7764{
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007765 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02007766}
7767
Carsten Ottee08b9632012-01-04 10:25:20 +01007768int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007769{
Carsten Ottee08b9632012-01-04 10:25:20 +01007770 if (type)
7771 return -EINVAL;
7772
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01007773 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08007774 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08007775 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007776 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007777 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007778
Sheng Yang5550af42008-10-15 20:15:06 +08007779 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
7780 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06007781 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
7782 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
7783 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08007784
Jan Kiszka038f8c12011-02-04 10:49:11 +01007785 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007786 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007787 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
7788
7789 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01007790
Andrew Jones7e44e442014-02-28 12:52:54 +01007791 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01007792 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01007793
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08007794 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08007795 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08007796
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05007797 if (kvm_x86_ops->vm_init)
7798 return kvm_x86_ops->vm_init(kvm);
7799
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01007800 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007801}
7802
7803static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
7804{
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03007805 int r;
7806 r = vcpu_load(vcpu);
7807 BUG_ON(r);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007808 kvm_mmu_unload(vcpu);
7809 vcpu_put(vcpu);
7810}
7811
7812static void kvm_free_vcpus(struct kvm *kvm)
7813{
7814 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007815 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007816
7817 /*
7818 * Unpin any mmu pages first.
7819 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02007820 kvm_for_each_vcpu(i, vcpu, kvm) {
7821 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007822 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02007823 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007824 kvm_for_each_vcpu(i, vcpu, kvm)
7825 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007826
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007827 mutex_lock(&kvm->lock);
7828 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
7829 kvm->vcpus[i] = NULL;
7830
7831 atomic_set(&kvm->online_vcpus, 0);
7832 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007833}
7834
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007835void kvm_arch_sync_events(struct kvm *kvm)
7836{
Andrew Jones332967a2014-02-28 12:52:55 +01007837 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01007838 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Sheng Yangba4cef32009-01-06 10:03:03 +08007839 kvm_free_all_assigned_devices(kvm);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08007840 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08007841}
7842
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007843int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007844{
7845 int i, r;
Paolo Bonzini25188b92015-10-14 15:51:08 +02007846 unsigned long hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007847 struct kvm_memslots *slots = kvm_memslots(kvm);
7848 struct kvm_memory_slot *slot, old;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007849
7850 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007851 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
7852 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007853
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007854 slot = id_to_memslot(slots, id);
7855 if (size) {
Paolo Bonzinib21629d2016-06-01 14:09:18 +02007856 if (slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007857 return -EEXIST;
7858
7859 /*
7860 * MAP_SHARED to prevent internal slot pages from being moved
7861 * by fork()/COW.
7862 */
7863 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
7864 MAP_SHARED | MAP_ANONYMOUS, 0);
7865 if (IS_ERR((void *)hva))
7866 return PTR_ERR((void *)hva);
7867 } else {
7868 if (!slot->npages)
7869 return 0;
7870
7871 hva = 0;
7872 }
7873
7874 old = *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007875 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007876 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007877
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007878 m.slot = id | (i << 16);
7879 m.flags = 0;
7880 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007881 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007882 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007883 r = __kvm_set_memory_region(kvm, &m);
7884 if (r < 0)
7885 return r;
7886 }
7887
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02007888 if (!size) {
7889 r = vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
7890 WARN_ON(r < 0);
7891 }
7892
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007893 return 0;
7894}
7895EXPORT_SYMBOL_GPL(__x86_set_memory_region);
7896
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007897int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007898{
7899 int r;
7900
7901 mutex_lock(&kvm->slots_lock);
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007902 r = __x86_set_memory_region(kvm, id, gpa, size);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02007903 mutex_unlock(&kvm->slots_lock);
7904
7905 return r;
7906}
7907EXPORT_SYMBOL_GPL(x86_set_memory_region);
7908
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007909void kvm_arch_destroy_vm(struct kvm *kvm)
7910{
Andrew Honig27469d22013-04-18 09:38:14 -07007911 if (current->mm == kvm->mm) {
7912 /*
7913 * Free memory regions allocated on behalf of userspace,
7914 * unless the the memory map has changed due to process exit
7915 * or fd copying.
7916 */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02007917 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
7918 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
7919 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
Andrew Honig27469d22013-04-18 09:38:14 -07007920 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05007921 if (kvm_x86_ops->vm_destroy)
7922 kvm_x86_ops->vm_destroy(kvm);
Sheng Yang6eb55812008-10-31 12:37:41 +08007923 kvm_iommu_unmap_guest(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08007924 kfree(kvm->arch.vpic);
7925 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007926 kvm_free_vcpus(kvm);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03007927 kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08007928 kvm_mmu_uninit_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08007929}
Zhang Xiantao0de10342007-11-20 16:25:04 +08007930
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307931void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007932 struct kvm_memory_slot *dont)
7933{
7934 int i;
7935
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007936 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
7937 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007938 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007939 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007940 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007941 if (i == 0)
7942 continue;
7943
7944 if (!dont || free->arch.lpage_info[i - 1] !=
7945 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01007946 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007947 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007948 }
7949 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08007950
7951 kvm_page_track_free_memslot(free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007952}
7953
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05307954int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
7955 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007956{
7957 int i;
7958
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007959 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08007960 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007961 unsigned long ugfn;
7962 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007963 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007964
7965 lpages = gfn_to_index(slot->base_gfn + npages - 1,
7966 slot->base_gfn, level) + 1;
7967
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007968 slot->arch.rmap[i] =
7969 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
7970 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007971 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09007972 if (i == 0)
7973 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09007974
Xiao Guangrong92f94f12016-02-24 17:51:06 +08007975 linfo = kvm_kvzalloc(lpages * sizeof(*linfo));
7976 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007977 goto out_free;
7978
Xiao Guangrong92f94f12016-02-24 17:51:06 +08007979 slot->arch.lpage_info[i - 1] = linfo;
7980
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007981 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08007982 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007983 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08007984 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007985 ugfn = slot->userspace_addr >> PAGE_SHIFT;
7986 /*
7987 * If the gfn and userspace address are not aligned wrt each
7988 * other, or if explicitly asked to, disable large page
7989 * support for this slot
7990 */
7991 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
7992 !kvm_largepages_enabled()) {
7993 unsigned long j;
7994
7995 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08007996 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09007997 }
7998 }
7999
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08008000 if (kvm_page_track_create_memslot(slot, npages))
8001 goto out_free;
8002
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09008003 return 0;
8004
8005out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09008006 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01008007 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09008008 slot->arch.rmap[i] = NULL;
8009 if (i == 0)
8010 continue;
8011
Thomas Huth548ef282015-02-24 21:29:25 +01008012 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09008013 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09008014 }
8015 return -ENOMEM;
8016}
8017
Paolo Bonzini15f46012015-05-17 21:26:08 +02008018void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09008019{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09008020 /*
8021 * memslots->generation has been incremented.
8022 * mmio generation may have reached its maximum value.
8023 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008024 kvm_mmu_invalidate_mmio_sptes(kvm, slots);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09008025}
8026
Marcelo Tosattif7784b82009-12-23 14:35:18 -02008027int kvm_arch_prepare_memory_region(struct kvm *kvm,
8028 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02008029 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09008030 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08008031{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02008032 return 0;
8033}
8034
Kai Huang88178fd2015-01-28 10:54:27 +08008035static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
8036 struct kvm_memory_slot *new)
8037{
8038 /* Still write protect RO slot */
8039 if (new->flags & KVM_MEM_READONLY) {
8040 kvm_mmu_slot_remove_write_access(kvm, new);
8041 return;
8042 }
8043
8044 /*
8045 * Call kvm_x86_ops dirty logging hooks when they are valid.
8046 *
8047 * kvm_x86_ops->slot_disable_log_dirty is called when:
8048 *
8049 * - KVM_MR_CREATE with dirty logging is disabled
8050 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
8051 *
8052 * The reason is, in case of PML, we need to set D-bit for any slots
8053 * with dirty logging disabled in order to eliminate unnecessary GPA
8054 * logging in PML buffer (and potential PML buffer full VMEXT). This
8055 * guarantees leaving PML enabled during guest's lifetime won't have
8056 * any additonal overhead from PML when guest is running with dirty
8057 * logging disabled for memory slots.
8058 *
8059 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
8060 * to dirty logging mode.
8061 *
8062 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
8063 *
8064 * In case of write protect:
8065 *
8066 * Write protect all pages for dirty logging.
8067 *
8068 * All the sptes including the large sptes which point to this
8069 * slot are set to readonly. We can not create any new large
8070 * spte on this slot until the end of the logging.
8071 *
8072 * See the comments in fast_page_fault().
8073 */
8074 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
8075 if (kvm_x86_ops->slot_enable_log_dirty)
8076 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
8077 else
8078 kvm_mmu_slot_remove_write_access(kvm, new);
8079 } else {
8080 if (kvm_x86_ops->slot_disable_log_dirty)
8081 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
8082 }
8083}
8084
Marcelo Tosattif7784b82009-12-23 14:35:18 -02008085void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02008086 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09008087 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02008088 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09008089 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02008090{
Takuya Yoshikawa84826442013-02-27 19:45:25 +09008091 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02008092
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08008093 if (!kvm->arch.n_requested_mmu_pages)
8094 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08008095
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08008096 if (nr_mmu_pages)
8097 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Kai Huang1c91cad42015-01-28 10:54:26 +08008098
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09008099 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08008100 * Dirty logging tracks sptes in 4k granularity, meaning that large
8101 * sptes have to be split. If live migration is successful, the guest
8102 * in the source machine will be destroyed and large sptes will be
8103 * created in the destination. However, if the guest continues to run
8104 * in the source machine (for example if live migration fails), small
8105 * sptes will remain around and cause bad performance.
8106 *
8107 * Scan sptes if dirty logging has been stopped, dropping those
8108 * which can be collapsed into a single large-page spte. Later
8109 * page faults will create the large-page sptes.
8110 */
8111 if ((change != KVM_MR_DELETE) &&
8112 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
8113 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
8114 kvm_mmu_zap_collapsible_sptes(kvm, new);
8115
8116 /*
Kai Huang88178fd2015-01-28 10:54:27 +08008117 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08008118 *
Kai Huang88178fd2015-01-28 10:54:27 +08008119 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
8120 * been zapped so no dirty logging staff is needed for old slot. For
8121 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
8122 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02008123 *
8124 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09008125 */
Kai Huang88178fd2015-01-28 10:54:27 +08008126 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02008127 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08008128}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08008129
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03008130void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03008131{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08008132 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03008133}
8134
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03008135void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
8136 struct kvm_memory_slot *slot)
8137{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08008138 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03008139}
8140
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008141static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
8142{
8143 if (!list_empty_careful(&vcpu->async_pf.done))
8144 return true;
8145
8146 if (kvm_apic_has_events(vcpu))
8147 return true;
8148
8149 if (vcpu->arch.pv.pv_unhalted)
8150 return true;
8151
8152 if (atomic_read(&vcpu->arch.nmi_queued))
8153 return true;
8154
Paolo Bonzini73917732015-10-13 10:19:35 +02008155 if (test_bit(KVM_REQ_SMI, &vcpu->requests))
8156 return true;
8157
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008158 if (kvm_arch_interrupt_allowed(vcpu) &&
8159 kvm_cpu_has_interrupt(vcpu))
8160 return true;
8161
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008162 if (kvm_hv_has_stimer_pending(vcpu))
8163 return true;
8164
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008165 return false;
8166}
8167
Zhang Xiantao1d737c82007-12-14 09:35:10 +08008168int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
8169{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008170 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
8171 kvm_x86_ops->check_nested_events(vcpu, false);
8172
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008173 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +08008174}
Zhang Xiantao57361992007-12-17 14:21:40 +08008175
Christoffer Dallb6d33832012-03-08 16:44:24 -05008176int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08008177{
Christoffer Dallb6d33832012-03-08 16:44:24 -05008178 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08008179}
Gleb Natapov78646122009-03-23 12:12:11 +02008180
8181int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
8182{
8183 return kvm_x86_ops->interrupt_allowed(vcpu);
8184}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008185
Nadav Amit82b32772014-11-02 11:54:45 +02008186unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
8187{
8188 if (is_64_bit_mode(vcpu))
8189 return kvm_rip_read(vcpu);
8190 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
8191 kvm_rip_read(vcpu));
8192}
8193EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
8194
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008195bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
8196{
Nadav Amit82b32772014-11-02 11:54:45 +02008197 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008198}
8199EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
8200
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008201unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
8202{
8203 unsigned long rflags;
8204
8205 rflags = kvm_x86_ops->get_rflags(vcpu);
8206 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01008207 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008208 return rflags;
8209}
8210EXPORT_SYMBOL_GPL(kvm_get_rflags);
8211
Paolo Bonzini6addfc42014-03-27 11:29:28 +01008212static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008213{
8214 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008215 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01008216 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008217 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01008218}
8219
8220void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
8221{
8222 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008223 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02008224}
8225EXPORT_SYMBOL_GPL(kvm_set_rflags);
8226
Gleb Natapov56028d02010-10-17 18:13:42 +02008227void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
8228{
8229 int r;
8230
Xiao Guangrongfb67e142010-12-07 10:35:25 +08008231 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08008232 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02008233 return;
8234
8235 r = kvm_mmu_reload(vcpu);
8236 if (unlikely(r))
8237 return;
8238
Xiao Guangrongfb67e142010-12-07 10:35:25 +08008239 if (!vcpu->arch.mmu.direct_map &&
8240 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
8241 return;
8242
Gleb Natapov56028d02010-10-17 18:13:42 +02008243 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
8244}
8245
Gleb Natapovaf585b92010-10-14 11:22:46 +02008246static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
8247{
8248 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
8249}
8250
8251static inline u32 kvm_async_pf_next_probe(u32 key)
8252{
8253 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
8254}
8255
8256static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8257{
8258 u32 key = kvm_async_pf_hash_fn(gfn);
8259
8260 while (vcpu->arch.apf.gfns[key] != ~0)
8261 key = kvm_async_pf_next_probe(key);
8262
8263 vcpu->arch.apf.gfns[key] = gfn;
8264}
8265
8266static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
8267{
8268 int i;
8269 u32 key = kvm_async_pf_hash_fn(gfn);
8270
8271 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08008272 (vcpu->arch.apf.gfns[key] != gfn &&
8273 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02008274 key = kvm_async_pf_next_probe(key);
8275
8276 return key;
8277}
8278
8279bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8280{
8281 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
8282}
8283
8284static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
8285{
8286 u32 i, j, k;
8287
8288 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
8289 while (true) {
8290 vcpu->arch.apf.gfns[i] = ~0;
8291 do {
8292 j = kvm_async_pf_next_probe(j);
8293 if (vcpu->arch.apf.gfns[j] == ~0)
8294 return;
8295 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
8296 /*
8297 * k lies cyclically in ]i,j]
8298 * | i.k.j |
8299 * |....j i.k.| or |.k..j i...|
8300 */
8301 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
8302 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
8303 i = j;
8304 }
8305}
8306
Gleb Natapov7c907052010-10-14 11:22:53 +02008307static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
8308{
8309
8310 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
8311 sizeof(val));
8312}
8313
Gleb Natapovaf585b92010-10-14 11:22:46 +02008314void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
8315 struct kvm_async_pf *work)
8316{
Avi Kivity6389ee92010-11-29 16:12:30 +02008317 struct x86_exception fault;
8318
Gleb Natapov7c907052010-10-14 11:22:53 +02008319 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008320 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02008321
8322 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02008323 (vcpu->arch.apf.send_user_only &&
8324 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02008325 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
8326 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008327 fault.vector = PF_VECTOR;
8328 fault.error_code_valid = true;
8329 fault.error_code = 0;
8330 fault.nested_page_fault = false;
8331 fault.address = work->arch.token;
8332 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008333 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008334}
8335
8336void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
8337 struct kvm_async_pf *work)
8338{
Avi Kivity6389ee92010-11-29 16:12:30 +02008339 struct x86_exception fault;
8340
Gleb Natapov7c907052010-10-14 11:22:53 +02008341 trace_kvm_async_pf_ready(work->arch.token, work->gva);
chai wenf2e10662013-10-14 22:22:33 +08008342 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02008343 work->arch.token = ~0; /* broadcast wakeup */
8344 else
8345 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
8346
8347 if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
8348 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02008349 fault.vector = PF_VECTOR;
8350 fault.error_code_valid = true;
8351 fault.error_code = 0;
8352 fault.nested_page_fault = false;
8353 fault.address = work->arch.token;
8354 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02008355 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08008356 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03008357 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02008358}
8359
8360bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
8361{
8362 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
8363 return true;
8364 else
8365 return !kvm_event_needs_reinjection(vcpu) &&
8366 kvm_x86_ops->interrupt_allowed(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008367}
8368
Paolo Bonzini5544eb92015-07-07 15:41:58 +02008369void kvm_arch_start_assignment(struct kvm *kvm)
8370{
8371 atomic_inc(&kvm->arch.assigned_device_count);
8372}
8373EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
8374
8375void kvm_arch_end_assignment(struct kvm *kvm)
8376{
8377 atomic_dec(&kvm->arch.assigned_device_count);
8378}
8379EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
8380
8381bool kvm_arch_has_assigned_device(struct kvm *kvm)
8382{
8383 return atomic_read(&kvm->arch.assigned_device_count);
8384}
8385EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
8386
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06008387void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
8388{
8389 atomic_inc(&kvm->arch.noncoherent_dma_count);
8390}
8391EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
8392
8393void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
8394{
8395 atomic_dec(&kvm->arch.noncoherent_dma_count);
8396}
8397EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
8398
8399bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
8400{
8401 return atomic_read(&kvm->arch.noncoherent_dma_count);
8402}
8403EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
8404
Alex Williamson14717e22016-05-05 11:58:35 -06008405bool kvm_arch_has_irq_bypass(void)
8406{
8407 return kvm_x86_ops->update_pi_irte != NULL;
8408}
8409
Feng Wu87276882015-09-18 22:29:40 +08008410int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
8411 struct irq_bypass_producer *prod)
8412{
8413 struct kvm_kernel_irqfd *irqfd =
8414 container_of(cons, struct kvm_kernel_irqfd, consumer);
8415
Alex Williamson14717e22016-05-05 11:58:35 -06008416 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +08008417
Alex Williamson14717e22016-05-05 11:58:35 -06008418 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
8419 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +08008420}
8421
8422void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
8423 struct irq_bypass_producer *prod)
8424{
8425 int ret;
8426 struct kvm_kernel_irqfd *irqfd =
8427 container_of(cons, struct kvm_kernel_irqfd, consumer);
8428
Feng Wu87276882015-09-18 22:29:40 +08008429 WARN_ON(irqfd->producer != prod);
8430 irqfd->producer = NULL;
8431
8432 /*
8433 * When producer of consumer is unregistered, we change back to
8434 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +02008435 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +08008436 * int this case doesn't want to receive the interrupts.
8437 */
8438 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
8439 if (ret)
8440 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
8441 " fails: %d\n", irqfd->consumer.token, ret);
8442}
8443
8444int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
8445 uint32_t guest_irq, bool set)
8446{
8447 if (!kvm_x86_ops->update_pi_irte)
8448 return -EINVAL;
8449
8450 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
8451}
8452
Feng Wu520040142016-01-25 16:53:33 +08008453bool kvm_vector_hashing_enabled(void)
8454{
8455 return vector_hashing;
8456}
8457EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
8458
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008459EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +08008460EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008461EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
8462EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
8463EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
8464EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02008465EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02008466EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02008467EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02008468EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02008469EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02008470EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01008471EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09008472EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +02008473EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
Kai Huang843e4332015-01-28 10:54:28 +08008474EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +08008475EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05008476EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
8477EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);