blob: 9d0fda9056de6dc81ee848330477bdada2d7131e [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"
Wei Huang474a5bb2015-06-19 13:54:23 +020030#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030031#include "hyperv.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020032
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020033#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030034#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020035#include <linux/kvm.h>
36#include <linux/fs.h>
37#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040038#include <linux/export.h>
39#include <linux/moduleparam.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>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010056#include <linux/sched/stat.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050057#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010058
Avi Kivityaec51dc2009-07-01 16:01:02 +030059#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080060
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>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010070#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010071#include <asm/hypervisor.h>
Carsten Otte043405e2007-10-10 17:16:19 +020072
Dave Hansend1898b72016-06-01 10:42:20 -070073#define CREATE_TRACE_POINTS
74#include "trace.h"
75
Carsten Otte313a3dc2007-10-11 19:16:52 +020076#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080077#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080078u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
79EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080080
Avi Kivity0f65dd72011-04-20 13:37:53 +030081#define emul_to_vcpu(ctxt) \
82 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
83
Joerg Roedel50a37eb2008-01-31 14:57:38 +010084/* EFER defaults:
85 * - enable syscall per default because its emulated by KVM
86 * - enable LME and LMA per default on 64 bit KVM
87 */
88#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080089static
90u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010091#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080092static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010093#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020094
Avi Kivityba1389b2007-11-18 16:24:12 +020095#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
96#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050097
Radim Krčmářc5192652016-07-12 22:09:28 +020098#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
99 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200100
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300101static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300102static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200103static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100104static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800105static void store_regs(struct kvm_vcpu *vcpu);
106static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200107
Paolo Bonzini893590c2015-11-06 11:46:24 +0100108struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300109EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800110
Paolo Bonzini893590c2015-11-06 11:46:24 +0100111static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030112module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200113
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200114static bool __read_mostly report_ignored_msrs = true;
115module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
116
Wanpeng Li4c276252018-05-05 04:02:32 -0700117unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200118module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
119
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300120static bool __read_mostly kvmclock_periodic_sync = true;
121module_param(kvmclock_periodic_sync, bool, S_IRUGO);
122
Paolo Bonzini893590c2015-11-06 11:46:24 +0100123bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100124EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100125u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100126EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800127u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
128EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
129u64 __read_mostly kvm_max_tsc_scaling_ratio;
130EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700131u64 __read_mostly kvm_default_tsc_scaling_ratio;
132EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100133
Zachary Amsdencc578282012-02-03 15:43:50 -0200134/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100135static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200136module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
137
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500138/* lapic timer advance (tscdeadline mode only) in nanoseconds */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100139unsigned int __read_mostly lapic_timer_advance_ns = 0;
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500140module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
Wanpeng Lic5ce8232018-05-29 14:53:17 +0800141EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500142
Feng Wu520040142016-01-25 16:53:33 +0800143static bool __read_mostly vector_hashing = true;
144module_param(vector_hashing, bool, S_IRUGO);
145
Liran Alonc4ae60e2018-03-12 13:12:47 +0200146bool __read_mostly enable_vmware_backdoor = false;
147module_param(enable_vmware_backdoor, bool, S_IRUGO);
148EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
149
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700150static bool __read_mostly force_emulation_prefix = false;
151module_param(force_emulation_prefix, bool, S_IRUGO);
152
Avi Kivity18863bd2009-09-07 11:12:18 +0300153#define KVM_NR_SHARED_MSRS 16
154
155struct kvm_shared_msrs_global {
156 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800157 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300158};
159
160struct kvm_shared_msrs {
161 struct user_return_notifier urn;
162 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800163 struct kvm_shared_msr_values {
164 u64 host;
165 u64 curr;
166 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300167};
168
169static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200170static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300171
Hollis Blanchard417bc302007-10-31 17:24:23 -0500172struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200173 { "pf_fixed", VCPU_STAT(pf_fixed) },
174 { "pf_guest", VCPU_STAT(pf_guest) },
175 { "tlb_flush", VCPU_STAT(tlb_flush) },
176 { "invlpg", VCPU_STAT(invlpg) },
177 { "exits", VCPU_STAT(exits) },
178 { "io_exits", VCPU_STAT(io_exits) },
179 { "mmio_exits", VCPU_STAT(mmio_exits) },
180 { "signal_exits", VCPU_STAT(signal_exits) },
181 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800182 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200183 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100184 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200185 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200186 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200187 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530188 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200189 { "request_irq", VCPU_STAT(request_irq_exits) },
190 { "irq_exits", VCPU_STAT(irq_exits) },
191 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200192 { "fpu_reload", VCPU_STAT(fpu_reload) },
193 { "insn_emulation", VCPU_STAT(insn_emulation) },
194 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300195 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200196 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100197 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200198 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200199 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
200 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
201 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
202 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
203 { "mmu_flooded", VM_STAT(mmu_flooded) },
204 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200205 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300206 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200207 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300208 { "largepages", VM_STAT(lpages) },
David Matlackf3414bc2016-12-20 15:25:57 -0800209 { "max_mmu_page_hash_collisions",
210 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500211 { NULL }
212};
213
Dexuan Cui2acf9232010-06-10 11:27:12 +0800214u64 __read_mostly host_xcr0;
215
Jan Kiszkab6785de2012-09-20 07:43:17 +0200216static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300217
Gleb Natapovaf585b92010-10-14 11:22:46 +0200218static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
219{
220 int i;
221 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
222 vcpu->arch.apf.gfns[i] = ~0;
223}
224
Avi Kivity18863bd2009-09-07 11:12:18 +0300225static void kvm_on_user_return(struct user_return_notifier *urn)
226{
227 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300228 struct kvm_shared_msrs *locals
229 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800230 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700231 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300232
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700233 /*
234 * Disabling irqs at this point since the following code could be
235 * interrupted and executed through kvm_arch_hardware_disable()
236 */
237 local_irq_save(flags);
238 if (locals->registered) {
239 locals->registered = false;
240 user_return_notifier_unregister(urn);
241 }
242 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300243 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800244 values = &locals->values[slot];
245 if (values->host != values->curr) {
246 wrmsrl(shared_msrs_global.msrs[slot], values->host);
247 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300248 }
249 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300250}
251
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800252static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300253{
Avi Kivity18863bd2009-09-07 11:12:18 +0300254 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200255 unsigned int cpu = smp_processor_id();
256 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300257
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800258 /* only read, and nobody should modify it at this time,
259 * so don't need lock */
260 if (slot >= shared_msrs_global.nr) {
261 printk(KERN_ERR "kvm: invalid MSR slot!");
262 return;
263 }
264 rdmsrl_safe(msr, &value);
265 smsr->values[slot].host = value;
266 smsr->values[slot].curr = value;
267}
268
269void kvm_define_shared_msr(unsigned slot, u32 msr)
270{
Nadav Amit0123be42014-07-24 15:06:56 +0300271 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200272 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300273 if (slot >= shared_msrs_global.nr)
274 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300275}
276EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
277
278static void kvm_shared_msr_cpu_online(void)
279{
280 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300281
282 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800283 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300284}
285
Andy Honig8b3c3102014-08-27 11:16:44 -0700286int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300287{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200288 unsigned int cpu = smp_processor_id();
289 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700290 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300291
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800292 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Andy Honig8b3c3102014-08-27 11:16:44 -0700293 return 0;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800294 smsr->values[slot].curr = value;
Andy Honig8b3c3102014-08-27 11:16:44 -0700295 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
296 if (err)
297 return 1;
298
Avi Kivity18863bd2009-09-07 11:12:18 +0300299 if (!smsr->registered) {
300 smsr->urn.on_user_return = kvm_on_user_return;
301 user_return_notifier_register(&smsr->urn);
302 smsr->registered = true;
303 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700304 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300305}
306EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
307
Radim Krčmář13a34e02014-08-28 15:13:03 +0200308static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200309{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200310 unsigned int cpu = smp_processor_id();
311 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200312
313 if (smsr->registered)
314 kvm_on_user_return(&smsr->urn);
315}
316
Carsten Otte6866b832007-10-29 16:09:10 +0100317u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
318{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300319 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100320}
321EXPORT_SYMBOL_GPL(kvm_get_apic_base);
322
Jim Mattson58871642018-05-09 16:56:04 -0400323enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
324{
325 return kvm_apic_mode(kvm_get_apic_base(vcpu));
326}
327EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
328
Jan Kiszka58cb6282014-01-24 16:48:44 +0100329int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100330{
Jim Mattson58871642018-05-09 16:56:04 -0400331 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
332 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200333 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
334 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100335
Jim Mattson58871642018-05-09 16:56:04 -0400336 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700337 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400338 if (!msr_info->host_initiated) {
339 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
340 return 1;
341 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
342 return 1;
343 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100344
345 kvm_lapic_set_base(vcpu, msr_info->data);
346 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100347}
348EXPORT_SYMBOL_GPL(kvm_set_apic_base);
349
Andi Kleen2605fc22014-05-02 00:44:37 +0200350asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000351{
352 /* Fault while not rebooting. We want the trace. */
353 BUG();
354}
355EXPORT_SYMBOL_GPL(kvm_spurious_fault);
356
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200357#define EXCPT_BENIGN 0
358#define EXCPT_CONTRIBUTORY 1
359#define EXCPT_PF 2
360
361static int exception_class(int vector)
362{
363 switch (vector) {
364 case PF_VECTOR:
365 return EXCPT_PF;
366 case DE_VECTOR:
367 case TS_VECTOR:
368 case NP_VECTOR:
369 case SS_VECTOR:
370 case GP_VECTOR:
371 return EXCPT_CONTRIBUTORY;
372 default:
373 break;
374 }
375 return EXCPT_BENIGN;
376}
377
Nadav Amitd6e8c852014-07-24 14:51:24 +0300378#define EXCPT_FAULT 0
379#define EXCPT_TRAP 1
380#define EXCPT_ABORT 2
381#define EXCPT_INTERRUPT 3
382
383static int exception_type(int vector)
384{
385 unsigned int mask;
386
387 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
388 return EXCPT_INTERRUPT;
389
390 mask = 1 << vector;
391
392 /* #DB is trap, as instruction watchpoints are handled elsewhere */
393 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
394 return EXCPT_TRAP;
395
396 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
397 return EXCPT_ABORT;
398
399 /* Reserved exceptions will result in fault */
400 return EXCPT_FAULT;
401}
402
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200403static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200404 unsigned nr, bool has_error, u32 error_code,
405 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200406{
407 u32 prev_nr;
408 int class1, class2;
409
Avi Kivity3842d132010-07-27 12:30:24 +0300410 kvm_make_request(KVM_REQ_EVENT, vcpu);
411
Wanpeng Li664f8e22017-08-24 03:35:09 -0700412 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200413 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200414 if (has_error && !is_protmode(vcpu))
415 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700416 if (reinject) {
417 /*
418 * On vmentry, vcpu->arch.exception.pending is only
419 * true if an event injection was blocked by
420 * nested_run_pending. In that case, however,
421 * vcpu_enter_guest requests an immediate exit,
422 * and the guest shouldn't proceed far enough to
423 * need reinjection.
424 */
425 WARN_ON_ONCE(vcpu->arch.exception.pending);
426 vcpu->arch.exception.injected = true;
427 } else {
428 vcpu->arch.exception.pending = true;
429 vcpu->arch.exception.injected = false;
430 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200431 vcpu->arch.exception.has_error_code = has_error;
432 vcpu->arch.exception.nr = nr;
433 vcpu->arch.exception.error_code = error_code;
434 return;
435 }
436
437 /* to check exception */
438 prev_nr = vcpu->arch.exception.nr;
439 if (prev_nr == DF_VECTOR) {
440 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300441 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200442 return;
443 }
444 class1 = exception_class(prev_nr);
445 class2 = exception_class(nr);
446 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
447 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700448 /*
449 * Generate double fault per SDM Table 5-5. Set
450 * exception.pending = true so that the double fault
451 * can trigger a nested vmexit.
452 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200453 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700454 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200455 vcpu->arch.exception.has_error_code = true;
456 vcpu->arch.exception.nr = DF_VECTOR;
457 vcpu->arch.exception.error_code = 0;
458 } else
459 /* replace previous exception with a new one in a hope
460 that instruction re-execution will regenerate lost
461 exception */
462 goto queue;
463}
464
Avi Kivity298101d2007-11-25 13:41:11 +0200465void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
466{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200467 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200468}
469EXPORT_SYMBOL_GPL(kvm_queue_exception);
470
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200471void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
472{
473 kvm_multiple_exception(vcpu, nr, false, 0, true);
474}
475EXPORT_SYMBOL_GPL(kvm_requeue_exception);
476
Kyle Huey6affcbe2016-11-29 12:40:40 -0800477int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200478{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100479 if (err)
480 kvm_inject_gp(vcpu, 0);
481 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800482 return kvm_skip_emulated_instruction(vcpu);
483
484 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100485}
486EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200487
Avi Kivity6389ee92010-11-29 16:12:30 +0200488void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200489{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200490 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700491 vcpu->arch.exception.nested_apf =
492 is_guest_mode(vcpu) && fault->async_page_fault;
493 if (vcpu->arch.exception.nested_apf)
494 vcpu->arch.apf.nested_apf_token = fault->address;
495 else
496 vcpu->arch.cr2 = fault->address;
Avi Kivity6389ee92010-11-29 16:12:30 +0200497 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200498}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300499EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200500
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200501static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200502{
Avi Kivity6389ee92010-11-29 16:12:30 +0200503 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
504 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200505 else
Avi Kivity6389ee92010-11-29 16:12:30 +0200506 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200507
508 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200509}
510
Sheng Yang3419ffc2008-05-15 09:52:48 +0800511void kvm_inject_nmi(struct kvm_vcpu *vcpu)
512{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300513 atomic_inc(&vcpu->arch.nmi_queued);
514 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800515}
516EXPORT_SYMBOL_GPL(kvm_inject_nmi);
517
Avi Kivity298101d2007-11-25 13:41:11 +0200518void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
519{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200520 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200521}
522EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
523
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200524void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
525{
526 kvm_multiple_exception(vcpu, nr, true, error_code, true);
527}
528EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
529
Carsten Ottea03490e2007-10-29 16:09:35 +0100530/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300531 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
532 * a #GP and return false.
533 */
534bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200535{
Avi Kivity0a79b002009-09-01 12:03:25 +0300536 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
537 return true;
538 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
539 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100540}
Avi Kivity0a79b002009-09-01 12:03:25 +0300541EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100542
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300543bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
544{
545 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
546 return true;
547
548 kvm_queue_exception(vcpu, UD_VECTOR);
549 return false;
550}
551EXPORT_SYMBOL_GPL(kvm_require_dr);
552
Carsten Ottea03490e2007-10-29 16:09:35 +0100553/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200554 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200555 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200556 * can read from guest physical or from the guest's guest physical memory.
557 */
558int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
559 gfn_t ngfn, void *data, int offset, int len,
560 u32 access)
561{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200562 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200563 gfn_t real_gfn;
564 gpa_t ngpa;
565
566 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200567 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200568 if (real_gfn == UNMAPPED_GVA)
569 return -EFAULT;
570
571 real_gfn = gpa_to_gfn(real_gfn);
572
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200573 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200574}
575EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
576
Fengguang Wu69b00492015-01-19 22:33:39 +0800577static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200578 void *data, int offset, int len, u32 access)
579{
580 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
581 data, offset, len, access);
582}
583
Joerg Roedelec92fe42010-09-10 17:30:51 +0200584/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100585 * Load the pae pdptrs. Return true is they are all valid.
586 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200587int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100588{
589 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
590 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
591 int i;
592 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200593 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100594
Joerg Roedelff03a072010-09-10 17:30:57 +0200595 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
596 offset * sizeof(u64), sizeof(pdpte),
597 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100598 if (ret < 0) {
599 ret = 0;
600 goto out;
601 }
602 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400603 if ((pdpte[i] & PT_PRESENT_MASK) &&
Xiao Guangronga0a64f52015-08-05 12:04:21 +0800604 (pdpte[i] &
605 vcpu->arch.mmu.guest_rsvd_check.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100606 ret = 0;
607 goto out;
608 }
609 }
610 ret = 1;
611
Joerg Roedelff03a072010-09-10 17:30:57 +0200612 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300613 __set_bit(VCPU_EXREG_PDPTR,
614 (unsigned long *)&vcpu->arch.regs_avail);
615 __set_bit(VCPU_EXREG_PDPTR,
616 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100617out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100618
619 return ret;
620}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100621EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100622
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100623bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200624{
Joerg Roedelff03a072010-09-10 17:30:57 +0200625 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200626 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200627 int offset;
628 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200629 int r;
630
Junaid Shahidd35b34a2018-08-08 17:45:24 -0700631 if (is_long_mode(vcpu) || !is_pae(vcpu) || !is_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200632 return false;
633
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300634 if (!test_bit(VCPU_EXREG_PDPTR,
635 (unsigned long *)&vcpu->arch.regs_avail))
636 return true;
637
Paolo Bonzinia5121772017-07-24 18:54:38 +0200638 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
639 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200640 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
641 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200642 if (r < 0)
643 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200644 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200645out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200646
647 return changed;
648}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100649EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200650
Avi Kivity49a9b072010-06-10 17:02:14 +0300651int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100652{
Sheng Yangaad82702010-05-12 16:40:42 +0800653 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800654 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800655
Avi Kivityf9a48e62010-01-06 19:10:22 +0200656 cr0 |= X86_CR0_ET;
657
Gleb Natapovab344822010-01-21 15:28:46 +0200658#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300659 if (cr0 & 0xffffffff00000000UL)
660 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200661#endif
662
663 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100664
Gleb Natapov0f122442010-04-28 19:15:31 +0300665 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
666 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100667
Gleb Natapov0f122442010-04-28 19:15:31 +0300668 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
669 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100670
671 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
672#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200673 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100674 int cs_db, cs_l;
675
Gleb Natapov0f122442010-04-28 19:15:31 +0300676 if (!is_pae(vcpu))
677 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100678 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300679 if (cs_l)
680 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100681 } else
682#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200683 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200684 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300685 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100686 }
687
Mao, Junjiead756a12012-07-02 01:18:48 +0000688 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
689 return 1;
690
Carsten Ottea03490e2007-10-29 16:09:35 +0100691 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100692
Lai Jiangshand170c412011-02-21 11:21:30 +0800693 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800694 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800695 kvm_async_pf_hash_reset(vcpu);
696 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800697
Sheng Yangaad82702010-05-12 16:40:42 +0800698 if ((cr0 ^ old_cr0) & update_bits)
699 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800700
Laszlo Ersek879ae182015-11-04 12:54:41 +0100701 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
702 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
703 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800704 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
705
Gleb Natapov0f122442010-04-28 19:15:31 +0300706 return 0;
707}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200708EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100709
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200710void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100711{
Avi Kivity49a9b072010-06-10 17:02:14 +0300712 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100713}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200714EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100715
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300716static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
717{
718 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
719 !vcpu->guest_xcr0_loaded) {
720 /* kvm_set_xcr() also depends on this */
Paolo Bonzini476b7ad2017-12-13 13:51:32 +0100721 if (vcpu->arch.xcr0 != host_xcr0)
722 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300723 vcpu->guest_xcr0_loaded = 1;
724 }
725}
726
727static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
728{
729 if (vcpu->guest_xcr0_loaded) {
730 if (vcpu->arch.xcr0 != host_xcr0)
731 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
732 vcpu->guest_xcr0_loaded = 0;
733 }
734}
735
Fengguang Wu69b00492015-01-19 22:33:39 +0800736static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800737{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000738 u64 xcr0 = xcr;
739 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200740 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800741
742 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
743 if (index != XCR_XFEATURE_ENABLED_MASK)
744 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700745 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800746 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700747 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800748 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200749
750 /*
751 * Do not allow the guest to set bits that we do not support
752 * saving. However, xcr0 bit 0 is always set, even if the
753 * emulated CPU does not support XSAVE (see fx_init).
754 */
Dave Hansend91cab72015-09-02 16:31:26 -0700755 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200756 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800757 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200758
Dave Hansend91cab72015-09-02 16:31:26 -0700759 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
760 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000761 return 1;
762
Dave Hansend91cab72015-09-02 16:31:26 -0700763 if (xcr0 & XFEATURE_MASK_AVX512) {
764 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800765 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700766 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800767 return 1;
768 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800769 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000770
Dave Hansend91cab72015-09-02 16:31:26 -0700771 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000772 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800773 return 0;
774}
775
776int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
777{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000778 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
779 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800780 kvm_inject_gp(vcpu, 0);
781 return 1;
782 }
783 return 0;
784}
785EXPORT_SYMBOL_GPL(kvm_set_xcr);
786
Avi Kivitya83b29c2010-06-10 17:02:15 +0300787int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100788{
Avi Kivityfc78f512009-12-07 12:16:48 +0200789 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800790 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800791 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800792
Gleb Natapov0f122442010-04-28 19:15:31 +0300793 if (cr4 & CR4_RESERVED_BITS)
794 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100795
Radim Krčmářd6321d42017-08-05 00:12:49 +0200796 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && (cr4 & X86_CR4_OSXSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800797 return 1;
798
Radim Krčmářd6321d42017-08-05 00:12:49 +0200799 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMEP) && (cr4 & X86_CR4_SMEP))
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800800 return 1;
801
Radim Krčmářd6321d42017-08-05 00:12:49 +0200802 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMAP) && (cr4 & X86_CR4_SMAP))
Feng Wu97ec8c02014-04-01 17:46:34 +0800803 return 1;
804
Radim Krčmářd6321d42017-08-05 00:12:49 +0200805 if (!guest_cpuid_has(vcpu, X86_FEATURE_FSGSBASE) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800806 return 1;
807
Radim Krčmářd6321d42017-08-05 00:12:49 +0200808 if (!guest_cpuid_has(vcpu, X86_FEATURE_PKU) && (cr4 & X86_CR4_PKE))
Huaitong Hanb9baba82016-03-22 16:51:21 +0800809 return 1;
810
Yu Zhangfd8cb432017-08-24 20:27:56 +0800811 if (!guest_cpuid_has(vcpu, X86_FEATURE_LA57) && (cr4 & X86_CR4_LA57))
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300812 return 1;
Gleb Natapov0f122442010-04-28 19:15:31 +0300813
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200814 if (!guest_cpuid_has(vcpu, X86_FEATURE_UMIP) && (cr4 & X86_CR4_UMIP))
815 return 1;
816
Carsten Ottea03490e2007-10-29 16:09:35 +0100817 if (is_long_mode(vcpu)) {
818 if (!(cr4 & X86_CR4_PAE))
Avi Kivitya2edf572009-05-24 22:19:00 +0300819 return 1;
820 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800821 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200822 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Carsten Ottea03490e2007-10-29 16:09:35 +0100823 kvm_read_cr3(vcpu)))
824 return 1;
825
Mao, Junjiead756a12012-07-02 01:18:48 +0000826 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200827 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000828 return 1;
829
830 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
831 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
832 return 1;
833 }
834
Gleb Natapov0f122442010-04-28 19:15:31 +0300835 if (kvm_x86_ops->set_cr4(vcpu, cr4))
836 return 1;
837
Mao, Junjiead756a12012-07-02 01:18:48 +0000838 if (((cr4 ^ old_cr4) & pdptr_bits) ||
839 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800840 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300841
Huaitong Hanb9baba82016-03-22 16:51:21 +0800842 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200843 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800844
Gleb Natapov0f122442010-04-28 19:15:31 +0300845 return 0;
846}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200847EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100848
Avi Kivity23902182010-06-10 17:02:16 +0300849int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100850{
Junaid Shahidade61e22018-06-27 14:59:15 -0700851 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100852#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700853 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
854
Junaid Shahidade61e22018-06-27 14:59:15 -0700855 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -0700856 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
857 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -0700858 }
Paolo Bonziniac146232014-11-10 13:53:25 +0100859#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200860
Avi Kivity9f8fe502010-12-05 17:30:00 +0200861 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -0700862 if (!skip_tlb_flush) {
863 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -0700864 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -0700865 }
Gleb Natapov0f122442010-04-28 19:15:31 +0300866 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200867 }
868
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800869 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -0700870 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800871 return 1;
872 else if (is_pae(vcpu) && is_paging(vcpu) &&
Jan Kiszkad9f89b82014-05-10 09:24:34 +0200873 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300874 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100875
Junaid Shahidade61e22018-06-27 14:59:15 -0700876 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +0300877 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200878 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Junaid Shahid7c390d32018-06-27 14:59:06 -0700879
Gleb Natapov0f122442010-04-28 19:15:31 +0300880 return 0;
881}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200882EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100883
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100884int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100885{
Gleb Natapov0f122442010-04-28 19:15:31 +0300886 if (cr8 & CR8_RESERVED_BITS)
887 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200888 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100889 kvm_lapic_set_tpr(vcpu, cr8);
890 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800891 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300892 return 0;
893}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200894EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100895
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200896unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100897{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200898 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100899 return kvm_lapic_get_cr8(vcpu);
900 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800901 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100902}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200903EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100904
Nadav Amitae561ed2015-04-02 03:10:37 +0300905static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
906{
907 int i;
908
909 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
910 for (i = 0; i < KVM_NR_DB_REGS; i++)
911 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
912 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
913 }
914}
915
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100916static void kvm_update_dr6(struct kvm_vcpu *vcpu)
917{
918 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
919 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
920}
921
Jan Kiszkac8639012012-09-21 05:42:55 +0200922static void kvm_update_dr7(struct kvm_vcpu *vcpu)
923{
924 unsigned long dr7;
925
926 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
927 dr7 = vcpu->arch.guest_debug_dr7;
928 else
929 dr7 = vcpu->arch.dr7;
930 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +0100931 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
932 if (dr7 & DR7_BP_EN_MASK)
933 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +0200934}
935
Nadav Amit6f43ed02014-07-15 17:37:46 +0300936static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
937{
938 u64 fixed = DR6_FIXED_1;
939
Radim Krčmářd6321d42017-08-05 00:12:49 +0200940 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +0300941 fixed |= DR6_RTM;
942 return fixed;
943}
944
Gleb Natapov338dbc92010-04-28 19:15:32 +0300945static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300946{
947 switch (dr) {
948 case 0 ... 3:
949 vcpu->arch.db[dr] = val;
950 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
951 vcpu->arch.eff_db[dr] = val;
952 break;
953 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300954 /* fall through */
955 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300956 if (val & 0xffffffff00000000ULL)
957 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +0300958 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100959 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300960 break;
961 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300962 /* fall through */
963 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300964 if (val & 0xffffffff00000000ULL)
965 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300966 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +0200967 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300968 break;
969 }
970
971 return 0;
972}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300973
974int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
975{
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300976 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +0300977 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300978 return 1;
979 }
980 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +0300981}
Gleb Natapov020df072010-04-13 10:05:23 +0300982EXPORT_SYMBOL_GPL(kvm_set_dr);
983
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300984int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300985{
986 switch (dr) {
987 case 0 ... 3:
988 *val = vcpu->arch.db[dr];
989 break;
990 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +0300991 /* fall through */
992 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +0100993 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
994 *val = vcpu->arch.dr6;
995 else
996 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +0300997 break;
998 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +0300999 /* fall through */
1000 default: /* 7 */
1001 *val = vcpu->arch.dr7;
1002 break;
1003 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001004 return 0;
1005}
Gleb Natapov020df072010-04-13 10:05:23 +03001006EXPORT_SYMBOL_GPL(kvm_get_dr);
1007
Avi Kivity022cd0e2011-11-10 14:57:23 +02001008bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1009{
1010 u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
1011 u64 data;
1012 int err;
1013
Wei Huangc6702c92015-06-19 13:44:45 +02001014 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001015 if (err)
1016 return err;
1017 kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
1018 kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
1019 return err;
1020}
1021EXPORT_SYMBOL_GPL(kvm_rdpmc);
1022
Carsten Otte043405e2007-10-10 17:16:19 +02001023/*
1024 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1025 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1026 *
1027 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001028 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001029 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
1030 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001031 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001032
Carsten Otte043405e2007-10-10 17:16:19 +02001033static u32 msrs_to_save[] = {
1034 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001035 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001036#ifdef CONFIG_X86_64
1037 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1038#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001039 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01001040 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001041 MSR_IA32_SPEC_CTRL, MSR_IA32_ARCH_CAPABILITIES
Carsten Otte043405e2007-10-10 17:16:19 +02001042};
1043
1044static unsigned num_msrs_to_save;
1045
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001046static u32 emulated_msrs[] = {
1047 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1048 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1049 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1050 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001051 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001052 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1053 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001054 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001055 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001056 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001057 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001058 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001059 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001060 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1061 HV_X64_MSR_TSC_EMULATION_STATUS,
1062
1063 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001064 MSR_KVM_PV_EOI_EN,
1065
Will Auldba904632012-11-29 12:42:50 -08001066 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001067 MSR_IA32_TSCDEADLINE,
Carsten Otte043405e2007-10-10 17:16:19 +02001068 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001069 MSR_IA32_MCG_STATUS,
1070 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001071 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001072 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001073 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001074 MSR_PLATFORM_INFO,
1075 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001076 MSR_AMD64_VIRT_SPEC_CTRL,
Carsten Otte043405e2007-10-10 17:16:19 +02001077};
1078
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001079static unsigned num_emulated_msrs;
1080
Tom Lendacky801e4592018-02-21 13:39:51 -06001081/*
1082 * List of msr numbers which are used to expose MSR-based features that
1083 * can be used by a hypervisor to validate requested CPU features.
1084 */
1085static u32 msr_based_features[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001086 MSR_IA32_VMX_BASIC,
1087 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1088 MSR_IA32_VMX_PINBASED_CTLS,
1089 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1090 MSR_IA32_VMX_PROCBASED_CTLS,
1091 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1092 MSR_IA32_VMX_EXIT_CTLS,
1093 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1094 MSR_IA32_VMX_ENTRY_CTLS,
1095 MSR_IA32_VMX_MISC,
1096 MSR_IA32_VMX_CR0_FIXED0,
1097 MSR_IA32_VMX_CR0_FIXED1,
1098 MSR_IA32_VMX_CR4_FIXED0,
1099 MSR_IA32_VMX_CR4_FIXED1,
1100 MSR_IA32_VMX_VMCS_ENUM,
1101 MSR_IA32_VMX_PROCBASED_CTLS2,
1102 MSR_IA32_VMX_EPT_VPID_CAP,
1103 MSR_IA32_VMX_VMFUNC,
1104
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001105 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001106 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001107 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001108};
1109
1110static unsigned int num_msr_based_features;
1111
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001112u64 kvm_get_arch_capabilities(void)
1113{
1114 u64 data;
1115
1116 rdmsrl_safe(MSR_IA32_ARCH_CAPABILITIES, &data);
1117
1118 /*
1119 * If we're doing cache flushes (either "always" or "cond")
1120 * we will do one whenever the guest does a vmlaunch/vmresume.
1121 * If an outer hypervisor is doing the cache flush for us
1122 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1123 * capability to the guest too, and if EPT is disabled we're not
1124 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1125 * require a nested hypervisor to do a flush of its own.
1126 */
1127 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1128 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1129
1130 return data;
1131}
1132EXPORT_SYMBOL_GPL(kvm_get_arch_capabilities);
1133
Wanpeng Li66421c12018-02-28 14:03:30 +08001134static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1135{
1136 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001137 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001138 msr->data = kvm_get_arch_capabilities();
1139 break;
1140 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001141 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001142 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001143 default:
1144 if (kvm_x86_ops->get_msr_feature(msr))
1145 return 1;
1146 }
1147 return 0;
1148}
1149
Tom Lendacky801e4592018-02-21 13:39:51 -06001150static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1151{
1152 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001153 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001154
1155 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001156 r = kvm_get_msr_feature(&msr);
1157 if (r)
1158 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001159
1160 *data = msr.data;
1161
1162 return 0;
1163}
1164
Jan Kiszka384bb782013-04-20 10:52:36 +02001165bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001166{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001167 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001168 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001169
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02001170 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
Jan Kiszka384bb782013-04-20 10:52:36 +02001171 return false;
Alexander Graf1b2fd702009-02-02 16:23:51 +01001172
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02001173 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
Jan Kiszka384bb782013-04-20 10:52:36 +02001174 return false;
Alexander Grafd8017472008-11-25 20:17:11 +01001175
Jan Kiszka384bb782013-04-20 10:52:36 +02001176 return true;
1177}
1178EXPORT_SYMBOL_GPL(kvm_valid_efer);
1179
1180static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
1181{
1182 u64 old_efer = vcpu->arch.efer;
1183
1184 if (!kvm_valid_efer(vcpu, efer))
1185 return 1;
1186
1187 if (is_paging(vcpu)
1188 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1189 return 1;
1190
Carsten Otte15c4a642007-10-30 18:44:17 +01001191 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001192 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001193
Sheng Yanga3d204e2010-05-12 16:40:40 +08001194 kvm_x86_ops->set_efer(vcpu, efer);
1195
Sheng Yangaad82702010-05-12 16:40:42 +08001196 /* Update reserved bits */
1197 if ((efer ^ old_efer) & EFER_NX)
1198 kvm_mmu_reset_context(vcpu);
1199
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001200 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001201}
1202
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001203void kvm_enable_efer_bits(u64 mask)
1204{
1205 efer_reserved_bits &= ~mask;
1206}
1207EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1208
Carsten Otte15c4a642007-10-30 18:44:17 +01001209/*
1210 * Writes msr value into into the appropriate "register".
1211 * Returns 0 on success, non-0 otherwise.
1212 * Assumes vcpu_load() was already called.
1213 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001214int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01001215{
Nadav Amit854e8bb2014-09-16 03:24:05 +03001216 switch (msr->index) {
1217 case MSR_FS_BASE:
1218 case MSR_GS_BASE:
1219 case MSR_KERNEL_GS_BASE:
1220 case MSR_CSTAR:
1221 case MSR_LSTAR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08001222 if (is_noncanonical_address(msr->data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001223 return 1;
1224 break;
1225 case MSR_IA32_SYSENTER_EIP:
1226 case MSR_IA32_SYSENTER_ESP:
1227 /*
1228 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1229 * non-canonical address is written on Intel but not on
1230 * AMD (which ignores the top 32-bits, because it does
1231 * not implement 64-bit SYSENTER).
1232 *
1233 * 64-bit code should hence be able to write a non-canonical
1234 * value on AMD. Making the address canonical ensures that
1235 * vmentry does not fail on Intel after writing a non-canonical
1236 * value, and that something deterministic happens if the guest
1237 * invokes 64-bit SYSENTER.
1238 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08001239 msr->data = get_canonical(msr->data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001240 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001241 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001242}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001243EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001244
Carsten Otte313a3dc2007-10-11 19:16:52 +02001245/*
1246 * Adapt set_msr() to msr_io()'s calling convention
1247 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001248static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1249{
1250 struct msr_data msr;
1251 int r;
1252
1253 msr.index = index;
1254 msr.host_initiated = true;
1255 r = kvm_get_msr(vcpu, &msr);
1256 if (r)
1257 return r;
1258
1259 *data = msr.data;
1260 return 0;
1261}
1262
Carsten Otte313a3dc2007-10-11 19:16:52 +02001263static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1264{
Will Auld8fe8ab42012-11-29 12:42:12 -08001265 struct msr_data msr;
1266
1267 msr.data = *data;
1268 msr.index = index;
1269 msr.host_initiated = true;
1270 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001271}
1272
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001273#ifdef CONFIG_X86_64
1274struct pvclock_gtod_data {
1275 seqcount_t seq;
1276
1277 struct { /* extract of a clocksource struct */
1278 int vclock_mode;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001279 u64 cycle_last;
1280 u64 mask;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001281 u32 mult;
1282 u32 shift;
1283 } clock;
1284
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001285 u64 boot_ns;
1286 u64 nsec_base;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001287 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001288};
1289
1290static struct pvclock_gtod_data pvclock_gtod_data;
1291
1292static void update_pvclock_gtod(struct timekeeper *tk)
1293{
1294 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001295 u64 boot_ns;
1296
Peter Zijlstra876e7882015-03-19 10:09:06 +01001297 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001298
1299 write_seqcount_begin(&vdata->seq);
1300
1301 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001302 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1303 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1304 vdata->clock.mask = tk->tkr_mono.mask;
1305 vdata->clock.mult = tk->tkr_mono.mult;
1306 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001307
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001308 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001309 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001310
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001311 vdata->wall_time_sec = tk->xtime_sec;
1312
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001313 write_seqcount_end(&vdata->seq);
1314}
1315#endif
1316
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001317void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1318{
1319 /*
1320 * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
1321 * vcpu_enter_guest. This function is only called from
1322 * the physical CPU that is running vcpu.
1323 */
1324 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1325}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001326
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001327static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1328{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001329 int version;
1330 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001331 struct pvclock_wall_clock wc;
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001332 struct timespec64 boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001333
1334 if (!wall_clock)
1335 return;
1336
Avi Kivity9ed3c442010-05-04 15:00:37 +03001337 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1338 if (r)
1339 return;
1340
1341 if (version & 1)
1342 ++version; /* first time write, random junk */
1343
1344 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001345
Nicholas Krause1dab1342015-12-30 13:08:46 -05001346 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1347 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001348
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001349 /*
1350 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001351 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001352 * wall clock specified here. guest system time equals host
1353 * system time for us, thus we must fill in host boot time here.
1354 */
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001355 getboottime64(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001356
Bruce Rogers4b648662012-07-20 10:44:24 -06001357 if (kvm->arch.kvmclock_offset) {
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001358 struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
1359 boot = timespec64_sub(boot, ts);
Bruce Rogers4b648662012-07-20 10:44:24 -06001360 }
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001361 wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001362 wc.nsec = boot.tv_nsec;
1363 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001364
1365 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1366
1367 version++;
1368 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001369}
1370
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001371static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1372{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001373 do_shl32_div32(dividend, divisor);
1374 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001375}
1376
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001377static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001378 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001379{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001380 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001381 int32_t shift = 0;
1382 uint64_t tps64;
1383 uint32_t tps32;
1384
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001385 tps64 = base_hz;
1386 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001387 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001388 tps64 >>= 1;
1389 shift--;
1390 }
1391
1392 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001393 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1394 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001395 scaled64 >>= 1;
1396 else
1397 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001398 shift++;
1399 }
1400
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001401 *pshift = shift;
1402 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001403
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001404 pr_debug("%s: base_hz %llu => %llu, shift %d, mul %u\n",
1405 __func__, base_hz, scaled_hz, shift, *pmultiplier);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001406}
1407
Marcelo Tosattid8281992012-11-27 23:29:01 -02001408#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001409static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001410#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001411
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001412static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001413static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001414
Zachary Amsdencc578282012-02-03 15:43:50 -02001415static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001416{
Zachary Amsdencc578282012-02-03 15:43:50 -02001417 u64 v = (u64)khz * (1000000 + ppm);
1418 do_div(v, 1000000);
1419 return v;
1420}
1421
Haozhong Zhang381d5852015-10-20 15:39:04 +08001422static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1423{
1424 u64 ratio;
1425
1426 /* Guest TSC same frequency as host TSC? */
1427 if (!scale) {
1428 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1429 return 0;
1430 }
1431
1432 /* TSC scaling supported? */
1433 if (!kvm_has_tsc_control) {
1434 if (user_tsc_khz > tsc_khz) {
1435 vcpu->arch.tsc_catchup = 1;
1436 vcpu->arch.tsc_always_catchup = 1;
1437 return 0;
1438 } else {
1439 WARN(1, "user requested TSC rate below hardware speed\n");
1440 return -1;
1441 }
1442 }
1443
1444 /* TSC scaling required - calculate ratio */
1445 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1446 user_tsc_khz, tsc_khz);
1447
1448 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
1449 WARN_ONCE(1, "Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1450 user_tsc_khz);
1451 return -1;
1452 }
1453
1454 vcpu->arch.tsc_scaling_ratio = ratio;
1455 return 0;
1456}
1457
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001458static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001459{
1460 u32 thresh_lo, thresh_hi;
1461 int use_scaling = 0;
1462
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001463 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001464 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001465 /* set tsc_scaling_ratio to a safe value */
1466 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001467 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001468 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001469
Zachary Amsdenc2855452010-09-18 14:38:15 -10001470 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001471 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001472 &vcpu->arch.virtual_tsc_shift,
1473 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001474 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001475
1476 /*
1477 * Compute the variation in TSC rate which is acceptable
1478 * within the range of tolerance and decide if the
1479 * rate being applied is within that bounds of the hardware
1480 * rate. If so, no scaling or compensation need be done.
1481 */
1482 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1483 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001484 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1485 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 -02001486 use_scaling = 1;
1487 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001488 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001489}
1490
1491static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1492{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001493 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001494 vcpu->arch.virtual_tsc_mult,
1495 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001496 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001497 return tsc;
1498}
1499
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001500static inline int gtod_is_based_on_tsc(int mode)
1501{
1502 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1503}
1504
Fengguang Wu69b00492015-01-19 22:33:39 +08001505static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001506{
1507#ifdef CONFIG_X86_64
1508 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001509 struct kvm_arch *ka = &vcpu->kvm->arch;
1510 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1511
1512 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1513 atomic_read(&vcpu->kvm->online_vcpus));
1514
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001515 /*
1516 * Once the masterclock is enabled, always perform request in
1517 * order to update it.
1518 *
1519 * In order to enable masterclock, the host clocksource must be TSC
1520 * and the vcpus need to have matched TSCs. When that happens,
1521 * perform request to enable masterclock.
1522 */
1523 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001524 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001525 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1526
1527 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1528 atomic_read(&vcpu->kvm->online_vcpus),
1529 ka->use_master_clock, gtod->clock.vclock_mode);
1530#endif
1531}
1532
Will Auldba904632012-11-29 12:42:50 -08001533static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1534{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001535 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001536 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1537}
1538
Haozhong Zhang35181e82015-10-20 15:39:03 +08001539/*
1540 * Multiply tsc by a fixed point number represented by ratio.
1541 *
1542 * The most significant 64-N bits (mult) of ratio represent the
1543 * integral part of the fixed point number; the remaining N bits
1544 * (frac) represent the fractional part, ie. ratio represents a fixed
1545 * point number (mult + frac * 2^(-N)).
1546 *
1547 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1548 */
1549static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1550{
1551 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1552}
1553
1554u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1555{
1556 u64 _tsc = tsc;
1557 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1558
1559 if (ratio != kvm_default_tsc_scaling_ratio)
1560 _tsc = __scale_tsc(ratio, tsc);
1561
1562 return _tsc;
1563}
1564EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1565
Haozhong Zhang07c14192015-10-20 15:39:05 +08001566static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1567{
1568 u64 tsc;
1569
1570 tsc = kvm_scale_tsc(vcpu, rdtsc());
1571
1572 return target_tsc - tsc;
1573}
1574
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001575u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1576{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001577 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1578
1579 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001580}
1581EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1582
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001583static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1584{
1585 kvm_x86_ops->write_tsc_offset(vcpu, offset);
1586 vcpu->arch.tsc_offset = offset;
1587}
1588
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001589static inline bool kvm_check_tsc_unstable(void)
1590{
1591#ifdef CONFIG_X86_64
1592 /*
1593 * TSC is marked unstable when we're running on Hyper-V,
1594 * 'TSC page' clocksource is good.
1595 */
1596 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1597 return false;
1598#endif
1599 return check_tsc_unstable();
1600}
1601
Will Auld8fe8ab42012-11-29 12:42:12 -08001602void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001603{
1604 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001605 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001606 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001607 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001608 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001609 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001610 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001611
Jan Kiszka038f8c12011-02-04 10:49:11 +01001612 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001613 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini108b2492016-09-01 14:21:03 +02001614 ns = ktime_get_boot_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001615 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001616
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001617 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001618 if (data == 0 && msr->host_initiated) {
1619 /*
1620 * detection of vcpu initialization -- need to sync
1621 * with other vCPUs. This particularly helps to keep
1622 * kvm_clock stable after CPU hotplug
1623 */
1624 synchronizing = true;
1625 } else {
1626 u64 tsc_exp = kvm->arch.last_tsc_write +
1627 nsec_to_cycles(vcpu, elapsed);
1628 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1629 /*
1630 * Special case: TSC write with a small delta (1 second)
1631 * of virtual cycle time against real time is
1632 * interpreted as an attempt to synchronize the CPU.
1633 */
1634 synchronizing = data < tsc_exp + tsc_hz &&
1635 data + tsc_hz > tsc_exp;
1636 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001637 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001638
1639 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001640 * For a reliable TSC, we can match TSC offsets, and for an unstable
1641 * TSC, we add elapsed time in this computation. We could let the
1642 * compensation code attempt to catch up if we fall behind, but
1643 * it's better to try to match offsets from the beginning.
1644 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001645 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001646 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001647 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001648 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001649 pr_debug("kvm: matched tsc offset for %llu\n", data);
1650 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001651 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001652 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08001653 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsden759379d2010-08-19 22:07:25 -10001654 pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001655 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001656 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001657 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001658 } else {
1659 /*
1660 * We split periods of matched TSC writes into generations.
1661 * For each generation, we track the original measured
1662 * nanosecond time, offset, and write, so if TSCs are in
1663 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001664 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001665 *
1666 * These values are tracked in kvm->arch.cur_xxx variables.
1667 */
1668 kvm->arch.cur_tsc_generation++;
1669 kvm->arch.cur_tsc_nsec = ns;
1670 kvm->arch.cur_tsc_write = data;
1671 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001672 matched = false;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001673 pr_debug("kvm: new tsc generation %llu, clock %llu\n",
Zachary Amsdene26101b2012-02-03 15:43:57 -02001674 kvm->arch.cur_tsc_generation, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001675 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001676
1677 /*
1678 * We also track th most recent recorded KHZ, write and time to
1679 * allow the matching interval to be extended at each write.
1680 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001681 kvm->arch.last_tsc_nsec = ns;
1682 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001683 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001684
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001685 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001686
1687 /* Keep track of which generation this VCPU has synchronized to */
1688 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1689 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1690 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1691
Radim Krčmářd6321d42017-08-05 00:12:49 +02001692 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08001693 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02001694
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001695 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001696 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001697
1698 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001699 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001700 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001701 } else if (!already_matched) {
1702 kvm->arch.nr_vcpus_matched_tsc++;
1703 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001704
1705 kvm_track_tsc_matching(vcpu);
1706 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001707}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001708
Zachary Amsden99e3e302010-08-19 22:07:17 -10001709EXPORT_SYMBOL_GPL(kvm_write_tsc);
1710
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001711static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1712 s64 adjustment)
1713{
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01001714 kvm_vcpu_write_tsc_offset(vcpu, vcpu->arch.tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001715}
1716
1717static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1718{
1719 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1720 WARN_ON(adjustment < 0);
1721 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01001722 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001723}
1724
Marcelo Tosattid8281992012-11-27 23:29:01 -02001725#ifdef CONFIG_X86_64
1726
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001727static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001728{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001729 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02001730 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001731
1732 if (likely(ret >= last))
1733 return ret;
1734
1735 /*
1736 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001737 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02001738 * very likely) and there's a data dependence, so force GCC
1739 * to generate a branch instead. I don't barrier() because
1740 * we don't actually need a barrier, and if this function
1741 * ever gets inlined it will generate worse code.
1742 */
1743 asm volatile ("");
1744 return last;
1745}
1746
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001747static inline u64 vgettsc(u64 *tsc_timestamp, int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001748{
1749 long v;
1750 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001751 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001752
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001753 switch (gtod->clock.vclock_mode) {
1754 case VCLOCK_HVCLOCK:
1755 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
1756 tsc_timestamp);
1757 if (tsc_pg_val != U64_MAX) {
1758 /* TSC page valid */
1759 *mode = VCLOCK_HVCLOCK;
1760 v = (tsc_pg_val - gtod->clock.cycle_last) &
1761 gtod->clock.mask;
1762 } else {
1763 /* TSC page invalid */
1764 *mode = VCLOCK_NONE;
1765 }
1766 break;
1767 case VCLOCK_TSC:
1768 *mode = VCLOCK_TSC;
1769 *tsc_timestamp = read_tsc();
1770 v = (*tsc_timestamp - gtod->clock.cycle_last) &
1771 gtod->clock.mask;
1772 break;
1773 default:
1774 *mode = VCLOCK_NONE;
1775 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001776
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001777 if (*mode == VCLOCK_NONE)
1778 *tsc_timestamp = v = 0;
1779
Marcelo Tosattid8281992012-11-27 23:29:01 -02001780 return v * gtod->clock.mult;
1781}
1782
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001783static int do_monotonic_boot(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001784{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001785 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001786 unsigned long seq;
1787 int mode;
1788 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001789
Marcelo Tosattid8281992012-11-27 23:29:01 -02001790 do {
1791 seq = read_seqcount_begin(&gtod->seq);
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001792 ns = gtod->nsec_base;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001793 ns += vgettsc(tsc_timestamp, &mode);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001794 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001795 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001796 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001797 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001798
1799 return mode;
1800}
1801
Arnd Bergmann899a31f2018-04-23 10:04:26 +02001802static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001803{
1804 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1805 unsigned long seq;
1806 int mode;
1807 u64 ns;
1808
1809 do {
1810 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001811 ts->tv_sec = gtod->wall_time_sec;
1812 ns = gtod->nsec_base;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001813 ns += vgettsc(tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001814 ns >>= gtod->clock.shift;
1815 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
1816
1817 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
1818 ts->tv_nsec = ns;
1819
1820 return mode;
1821}
1822
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001823/* returns true if host is using TSC based clocksource */
1824static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001825{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001826 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001827 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02001828 return false;
1829
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001830 return gtod_is_based_on_tsc(do_monotonic_boot(kernel_ns,
1831 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001832}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001833
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001834/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02001835static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001836 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001837{
1838 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001839 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001840 return false;
1841
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001842 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001843}
Marcelo Tosattid8281992012-11-27 23:29:01 -02001844#endif
1845
1846/*
1847 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001848 * Assuming a stable TSC across physical CPUS, and a stable TSC
1849 * across virtual CPUs, the following condition is possible.
1850 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001851 * CPUs at the next numbered event.
1852 *
1853 * "timespecX" represents host monotonic time. "tscX" represents
1854 * RDTSC value.
1855 *
1856 * VCPU0 on CPU0 | VCPU1 on CPU1
1857 *
1858 * 1. read timespec0,tsc0
1859 * 2. | timespec1 = timespec0 + N
1860 * | tsc1 = tsc0 + M
1861 * 3. transition to guest | transition to guest
1862 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
1863 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
1864 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
1865 *
1866 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
1867 *
1868 * - ret0 < ret1
1869 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
1870 * ...
1871 * - 0 < N - M => M < N
1872 *
1873 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
1874 * always the case (the difference between two distinct xtime instances
1875 * might be smaller then the difference between corresponding TSC reads,
1876 * when updating guest vcpus pvclock areas).
1877 *
1878 * To avoid that problem, do not allow visibility of distinct
1879 * system_timestamp/tsc_timestamp values simultaneously: use a master
1880 * copy of host monotonic time values. Update that master copy
1881 * in lockstep.
1882 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001883 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02001884 *
1885 */
1886
1887static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
1888{
1889#ifdef CONFIG_X86_64
1890 struct kvm_arch *ka = &kvm->arch;
1891 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001892 bool host_tsc_clocksource, vcpus_matched;
1893
1894 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1895 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001896
1897 /*
1898 * If the host uses TSC clock, then passthrough TSC as stable
1899 * to the guest.
1900 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001901 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02001902 &ka->master_kernel_ns,
1903 &ka->master_cycle_now);
1904
Marcelo Tosatti16a96022014-05-14 12:43:24 -03001905 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02001906 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02001907 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001908
Marcelo Tosattid8281992012-11-27 23:29:01 -02001909 if (ka->use_master_clock)
1910 atomic_set(&kvm_guest_has_master_clock, 1);
1911
1912 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001913 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
1914 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001915#endif
1916}
1917
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01001918void kvm_make_mclock_inprogress_request(struct kvm *kvm)
1919{
1920 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
1921}
1922
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001923static void kvm_gen_update_masterclock(struct kvm *kvm)
1924{
1925#ifdef CONFIG_X86_64
1926 int i;
1927 struct kvm_vcpu *vcpu;
1928 struct kvm_arch *ka = &kvm->arch;
1929
1930 spin_lock(&ka->pvclock_gtod_sync_lock);
1931 kvm_make_mclock_inprogress_request(kvm);
1932 /* no guest entries from this point */
1933 pvclock_update_vm_gtod_copy(kvm);
1934
1935 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08001936 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001937
1938 /* guest entries allowed */
1939 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02001940 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03001941
1942 spin_unlock(&ka->pvclock_gtod_sync_lock);
1943#endif
1944}
1945
Marcelo Tosattie891a322017-04-17 12:51:37 -03001946u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02001947{
Paolo Bonzini108b2492016-09-01 14:21:03 +02001948 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01001949 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07001950 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02001951
Paolo Bonzini8b953442016-11-16 18:31:30 +01001952 spin_lock(&ka->pvclock_gtod_sync_lock);
1953 if (!ka->use_master_clock) {
1954 spin_unlock(&ka->pvclock_gtod_sync_lock);
1955 return ktime_get_boot_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02001956 }
1957
Paolo Bonzini8b953442016-11-16 18:31:30 +01001958 hv_clock.tsc_timestamp = ka->master_cycle_now;
1959 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
1960 spin_unlock(&ka->pvclock_gtod_sync_lock);
1961
Wanpeng Lie2c22062017-05-11 18:12:05 -07001962 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
1963 get_cpu();
1964
Wanpeng Lie70b57a2017-11-20 14:55:05 -08001965 if (__this_cpu_read(cpu_tsc_khz)) {
1966 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
1967 &hv_clock.tsc_shift,
1968 &hv_clock.tsc_to_system_mul);
1969 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
1970 } else
1971 ret = ktime_get_boot_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07001972
1973 put_cpu();
1974
1975 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02001976}
1977
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02001978static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
1979{
1980 struct kvm_vcpu_arch *vcpu = &v->arch;
1981 struct pvclock_vcpu_time_info guest_hv_clock;
1982
Paolo Bonzini4e335d92017-05-02 16:20:18 +02001983 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02001984 &guest_hv_clock, sizeof(guest_hv_clock))))
1985 return;
1986
1987 /* This VCPU is paused, but it's legal for a guest to read another
1988 * VCPU's kvmclock, so we really have to follow the specification where
1989 * it says that version is odd if data is being modified, and even after
1990 * it is consistent.
1991 *
1992 * Version field updates must be kept separate. This is because
1993 * kvm_write_guest_cached might use a "rep movs" instruction, and
1994 * writes within a string instruction are weakly ordered. So there
1995 * are three writes overall.
1996 *
1997 * As a small optimization, only write the version field in the first
1998 * and third write. The vcpu->pv_time cache is still valid, because the
1999 * version field is the first in the struct.
2000 */
2001 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2002
Liran Alon51c4b8b2017-11-05 16:11:30 +02002003 if (guest_hv_clock.version & 1)
2004 ++guest_hv_clock.version; /* first time write, random junk */
2005
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002006 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002007 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2008 &vcpu->hv_clock,
2009 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002010
2011 smp_wmb();
2012
2013 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2014 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2015
2016 if (vcpu->pvclock_set_guest_stopped_request) {
2017 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2018 vcpu->pvclock_set_guest_stopped_request = false;
2019 }
2020
2021 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2022
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002023 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2024 &vcpu->hv_clock,
2025 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002026
2027 smp_wmb();
2028
2029 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002030 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2031 &vcpu->hv_clock,
2032 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002033}
2034
Zachary Amsden34c238a2010-09-18 14:38:14 -10002035static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002036{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002037 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002038 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002039 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002040 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002041 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002042 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002043 bool use_master_clock;
2044
2045 kernel_ns = 0;
2046 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002047
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002048 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002049 * If the host uses TSC clock, then passthrough TSC as stable
2050 * to the guest.
2051 */
2052 spin_lock(&ka->pvclock_gtod_sync_lock);
2053 use_master_clock = ka->use_master_clock;
2054 if (use_master_clock) {
2055 host_tsc = ka->master_cycle_now;
2056 kernel_ns = ka->master_kernel_ns;
2057 }
2058 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002059
2060 /* Keep irq disabled to prevent changes to the clock */
2061 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002062 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2063 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002064 local_irq_restore(flags);
2065 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2066 return 1;
2067 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002068 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002069 host_tsc = rdtsc();
Paolo Bonzini108b2492016-09-01 14:21:03 +02002070 kernel_ns = ktime_get_boot_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002071 }
2072
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002073 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002074
2075 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002076 * We may have to catch up the TSC to match elapsed wall clock
2077 * time for two reasons, even if kvmclock is used.
2078 * 1) CPU could have been running below the maximum TSC rate
2079 * 2) Broken TSC compensation resets the base at each VCPU
2080 * entry to avoid unknown leaps of TSC even when running
2081 * again on the same CPU. This may cause apparent elapsed
2082 * time to disappear, and the guest to stand still or run
2083 * very slowly.
2084 */
2085 if (vcpu->tsc_catchup) {
2086 u64 tsc = compute_guest_tsc(v, kernel_ns);
2087 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002088 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002089 tsc_timestamp = tsc;
2090 }
2091 }
2092
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002093 local_irq_restore(flags);
2094
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002095 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002096
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002097 if (kvm_has_tsc_control)
2098 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2099
2100 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002101 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002102 &vcpu->hv_clock.tsc_shift,
2103 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002104 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002105 }
2106
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002107 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002108 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002109 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002110
Marcelo Tosattid8281992012-11-27 23:29:01 -02002111 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002112 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002113 if (use_master_clock)
2114 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2115
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002116 vcpu->hv_clock.flags = pvclock_flags;
2117
Paolo Bonzini095cf552016-02-08 12:54:12 +01002118 if (vcpu->pv_time_enabled)
2119 kvm_setup_pvclock_page(v);
2120 if (v == kvm_get_vcpu(v->kvm, 0))
2121 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002122 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002123}
2124
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002125/*
2126 * kvmclock updates which are isolated to a given vcpu, such as
2127 * vcpu->cpu migration, should not allow system_timestamp from
2128 * the rest of the vcpus to remain static. Otherwise ntp frequency
2129 * correction applies to one vcpu's system_timestamp but not
2130 * the others.
2131 *
2132 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002133 * We need to rate-limit these requests though, as they can
2134 * considerably slow guests that have a large number of vcpus.
2135 * The time for a remote vcpu to update its kvmclock is bound
2136 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002137 */
2138
Andrew Jones7e44e442014-02-28 12:52:54 +01002139#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2140
2141static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002142{
2143 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002144 struct delayed_work *dwork = to_delayed_work(work);
2145 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2146 kvmclock_update_work);
2147 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002148 struct kvm_vcpu *vcpu;
2149
2150 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002151 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002152 kvm_vcpu_kick(vcpu);
2153 }
2154}
2155
Andrew Jones7e44e442014-02-28 12:52:54 +01002156static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2157{
2158 struct kvm *kvm = v->kvm;
2159
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002160 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002161 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2162 KVMCLOCK_UPDATE_DELAY);
2163}
2164
Andrew Jones332967a2014-02-28 12:52:55 +01002165#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2166
2167static void kvmclock_sync_fn(struct work_struct *work)
2168{
2169 struct delayed_work *dwork = to_delayed_work(work);
2170 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2171 kvmclock_sync_work);
2172 struct kvm *kvm = container_of(ka, struct kvm, arch);
2173
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002174 if (!kvmclock_periodic_sync)
2175 return;
2176
Andrew Jones332967a2014-02-28 12:52:55 +01002177 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2178 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2179 KVMCLOCK_SYNC_PERIOD);
2180}
2181
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002182static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002183{
2184 u64 mcg_cap = vcpu->arch.mcg_cap;
2185 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002186 u32 msr = msr_info->index;
2187 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002188
2189 switch (msr) {
2190 case MSR_IA32_MCG_STATUS:
2191 vcpu->arch.mcg_status = data;
2192 break;
2193 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002194 if (!(mcg_cap & MCG_CTL_P) &&
2195 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002196 return 1;
2197 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002198 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002199 vcpu->arch.mcg_ctl = data;
2200 break;
2201 default:
2202 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002203 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002204 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01002205 /* only 0 or all 1s can be written to IA32_MCi_CTL
2206 * some Linux kernels though clear bit 10 in bank 4 to
2207 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2208 * this to avoid an uncatched #GP in the guest
2209 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002210 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002211 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002212 return -1;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002213 if (!msr_info->host_initiated &&
2214 (offset & 0x3) == 1 && data != 0)
2215 return -1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002216 vcpu->arch.mce_banks[offset] = data;
2217 break;
2218 }
2219 return 1;
2220 }
2221 return 0;
2222}
2223
Ed Swierkffde22a2009-10-15 15:21:43 -07002224static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2225{
2226 struct kvm *kvm = vcpu->kvm;
2227 int lm = is_long_mode(vcpu);
2228 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2229 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2230 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2231 : kvm->arch.xen_hvm_config.blob_size_32;
2232 u32 page_num = data & ~PAGE_MASK;
2233 u64 page_addr = data & PAGE_MASK;
2234 u8 *page;
2235 int r;
2236
2237 r = -E2BIG;
2238 if (page_num >= blob_size)
2239 goto out;
2240 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002241 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2242 if (IS_ERR(page)) {
2243 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002244 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002245 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002246 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002247 goto out_free;
2248 r = 0;
2249out_free:
2250 kfree(page);
2251out:
2252 return r;
2253}
2254
Gleb Natapov344d9582010-10-14 11:22:50 +02002255static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2256{
2257 gpa_t gpa = data & ~0x3f;
2258
Wanpeng Li52a5c152017-07-13 18:30:42 -07002259 /* Bits 3:5 are reserved, Should be zero */
2260 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002261 return 1;
2262
2263 vcpu->arch.apf.msr_val = data;
2264
2265 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2266 kvm_clear_async_pf_completion_queue(vcpu);
2267 kvm_async_pf_hash_reset(vcpu);
2268 return 0;
2269 }
2270
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002271 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002272 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002273 return 1;
2274
Gleb Natapov6adba522010-10-14 11:22:55 +02002275 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002276 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002277 kvm_async_pf_wakeup_all(vcpu);
2278 return 0;
2279}
2280
Glauber Costa12f9a482011-02-01 14:16:40 -05002281static void kvmclock_reset(struct kvm_vcpu *vcpu)
2282{
Andy Honig0b794592013-02-20 14:48:10 -08002283 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05002284}
2285
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002286static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2287{
2288 ++vcpu->stat.tlb_flush;
2289 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2290}
2291
Glauber Costac9aaa892011-07-11 15:28:14 -04002292static void record_steal_time(struct kvm_vcpu *vcpu)
2293{
2294 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2295 return;
2296
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002297 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002298 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2299 return;
2300
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002301 /*
2302 * Doing a TLB flush here, on the guest's behalf, can avoid
2303 * expensive IPIs.
2304 */
2305 if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB)
2306 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002307
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002308 if (vcpu->arch.st.steal.version & 1)
2309 vcpu->arch.st.steal.version += 1; /* first time write, random junk */
2310
2311 vcpu->arch.st.steal.version += 1;
2312
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002313 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002314 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2315
2316 smp_wmb();
2317
Liang Chenc54cdf12016-03-16 19:33:16 +08002318 vcpu->arch.st.steal.steal += current->sched_info.run_delay -
2319 vcpu->arch.st.last_steal;
2320 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002321
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002322 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002323 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2324
2325 smp_wmb();
2326
2327 vcpu->arch.st.steal.version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002328
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002329 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002330 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2331}
2332
Will Auld8fe8ab42012-11-29 12:42:12 -08002333int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002334{
Gleb Natapov57537852012-01-15 14:17:22 +02002335 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002336 u32 msr = msr_info->index;
2337 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002338
Carsten Otte15c4a642007-10-30 18:44:17 +01002339 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002340 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002341 case MSR_IA32_UCODE_WRITE:
2342 case MSR_VM_HSAVE_PA:
2343 case MSR_AMD64_PATCH_LOADER:
2344 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002345 case MSR_AMD64_DC_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002346 break;
2347
Wanpeng Li518e7b92018-02-28 14:03:31 +08002348 case MSR_IA32_UCODE_REV:
2349 if (msr_info->host_initiated)
2350 vcpu->arch.microcode_version = data;
2351 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002352 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02002353 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002354 case MSR_K7_HWCR:
2355 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002356 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002357 data &= ~(u64)0x8; /* ignore TLB cache disable */
Matthias Lange22d48b2d2014-06-26 13:50:15 +02002358 data &= ~(u64)0x40000; /* ignore Mc status write enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02002359 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002360 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2361 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002362 return 1;
2363 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002364 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002365 case MSR_FAM10H_MMIO_CONF_BASE:
2366 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002367 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2368 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002369 return 1;
2370 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002371 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002372 case MSR_IA32_DEBUGCTLMSR:
2373 if (!data) {
2374 /* We support the non-activated case already */
2375 break;
2376 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2377 /* Values other than LBR and BTF are vendor-specific,
2378 thus reserved and should throw a #GP */
2379 return 1;
2380 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002381 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2382 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002383 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002384 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002385 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002386 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002387 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002388 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2389 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002390 case MSR_IA32_TSCDEADLINE:
2391 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2392 break;
Will Auldba904632012-11-29 12:42:50 -08002393 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002394 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002395 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002396 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002397 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002398 }
2399 vcpu->arch.ia32_tsc_adjust_msr = data;
2400 }
2401 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002402 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002403 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01002404 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002405 case MSR_IA32_SMBASE:
2406 if (!msr_info->host_initiated)
2407 return 1;
2408 vcpu->arch.smbase = data;
2409 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002410 case MSR_IA32_TSC:
2411 kvm_write_tsc(vcpu, msr_info);
2412 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002413 case MSR_SMI_COUNT:
2414 if (!msr_info->host_initiated)
2415 return 1;
2416 vcpu->arch.smi_count = data;
2417 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002418 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002419 case MSR_KVM_WALL_CLOCK:
2420 vcpu->kvm->arch.wall_clock = data;
2421 kvm_write_wall_clock(vcpu->kvm, data);
2422 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002423 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002424 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002425 struct kvm_arch *ka = &vcpu->kvm->arch;
2426
Glauber Costa12f9a482011-02-01 14:16:40 -05002427 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002428
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002429 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2430 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2431
2432 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002433 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002434
2435 ka->boot_vcpu_runs_old_kvmclock = tmp;
2436 }
2437
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002438 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002439 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002440
2441 /* we verify if the enable bit is set... */
2442 if (!(data & 1))
2443 break;
2444
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002445 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002446 &vcpu->arch.pv_time, data & ~1ULL,
2447 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002448 vcpu->arch.pv_time_enabled = false;
2449 else
2450 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002451
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002452 break;
2453 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002454 case MSR_KVM_ASYNC_PF_EN:
2455 if (kvm_pv_enable_async_pf(vcpu, data))
2456 return 1;
2457 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002458 case MSR_KVM_STEAL_TIME:
2459
2460 if (unlikely(!sched_info_on()))
2461 return 1;
2462
2463 if (data & KVM_STEAL_RESERVED_MASK)
2464 return 1;
2465
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002466 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002467 data & KVM_STEAL_VALID_BITS,
2468 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002469 return 1;
2470
2471 vcpu->arch.st.msr_val = data;
2472
2473 if (!(data & KVM_MSR_ENABLED))
2474 break;
2475
Glauber Costac9aaa892011-07-11 15:28:14 -04002476 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2477
2478 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002479 case MSR_KVM_PV_EOI_EN:
2480 if (kvm_lapic_enable_pv_eoi(vcpu, data))
2481 return 1;
2482 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002483
Huang Ying890ca9a2009-05-11 16:48:15 +08002484 case MSR_IA32_MCG_CTL:
2485 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002486 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002487 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002488
Wei Huang6912ac32015-06-12 01:34:56 -04002489 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2490 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2491 pr = true; /* fall through */
2492 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2493 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002494 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002495 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002496
2497 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002498 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2499 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002500 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002501 case MSR_K7_CLK_CTL:
2502 /*
2503 * Ignore all writes to this no longer documented MSR.
2504 * Writes are only relevant for old K7 processors,
2505 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002506 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002507 * affected processor models on the command line, hence
2508 * the need to ignore the workaround.
2509 */
2510 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002511 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002512 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2513 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002514 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002515 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2516 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2517 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002518 return kvm_hv_set_msr_common(vcpu, msr, data,
2519 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002520 case MSR_IA32_BBL_CR_CTL3:
2521 /* Drop writes to this legacy MSR -- see rdmsr
2522 * counterpart for further detail.
2523 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002524 if (report_ignored_msrs)
2525 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2526 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002527 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002528 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002529 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002530 return 1;
2531 vcpu->arch.osvw.length = data;
2532 break;
2533 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002534 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002535 return 1;
2536 vcpu->arch.osvw.status = data;
2537 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002538 case MSR_PLATFORM_INFO:
2539 if (!msr_info->host_initiated ||
2540 data & ~MSR_PLATFORM_INFO_CPUID_FAULT ||
2541 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2542 cpuid_fault_enabled(vcpu)))
2543 return 1;
2544 vcpu->arch.msr_platform_info = data;
2545 break;
2546 case MSR_MISC_FEATURES_ENABLES:
2547 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2548 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2549 !supports_cpuid_fault(vcpu)))
2550 return 1;
2551 vcpu->arch.msr_misc_features_enables = data;
2552 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002553 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002554 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2555 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002556 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002557 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002558 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002559 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002560 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002561 return 1;
2562 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002563 if (report_ignored_msrs)
2564 vcpu_unimpl(vcpu,
2565 "ignored wrmsr: 0x%x data 0x%llx\n",
2566 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002567 break;
2568 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002569 }
2570 return 0;
2571}
2572EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2573
2574
2575/*
2576 * Reads an msr value (of 'msr_index') into 'pdata'.
2577 * Returns 0 on success, non-0 otherwise.
2578 * Assumes vcpu_load() was already called.
2579 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002580int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01002581{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002582 return kvm_x86_ops->get_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002583}
Wincy Vanff651cb2014-12-11 08:52:58 +03002584EXPORT_SYMBOL_GPL(kvm_get_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002585
Paolo Bonzini44883f02018-07-26 13:01:52 +02002586static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002587{
2588 u64 data;
2589 u64 mcg_cap = vcpu->arch.mcg_cap;
2590 unsigned bank_num = mcg_cap & 0xff;
2591
2592 switch (msr) {
2593 case MSR_IA32_P5_MC_ADDR:
2594 case MSR_IA32_P5_MC_TYPE:
2595 data = 0;
2596 break;
2597 case MSR_IA32_MCG_CAP:
2598 data = vcpu->arch.mcg_cap;
2599 break;
2600 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002601 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002602 return 1;
2603 data = vcpu->arch.mcg_ctl;
2604 break;
2605 case MSR_IA32_MCG_STATUS:
2606 data = vcpu->arch.mcg_status;
2607 break;
2608 default:
2609 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002610 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002611 u32 offset = msr - MSR_IA32_MC0_CTL;
2612 data = vcpu->arch.mce_banks[offset];
2613 break;
2614 }
2615 return 1;
2616 }
2617 *pdata = data;
2618 return 0;
2619}
2620
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002621int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002622{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002623 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002624 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002625 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002626 case MSR_IA32_DEBUGCTLMSR:
2627 case MSR_IA32_LASTBRANCHFROMIP:
2628 case MSR_IA32_LASTBRANCHTOIP:
2629 case MSR_IA32_LASTINTFROMIP:
2630 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302631 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002632 case MSR_K8_TSEG_ADDR:
2633 case MSR_K8_TSEG_MASK:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302634 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002635 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002636 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002637 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002638 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002639 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03002640 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02002641 case MSR_AMD64_DC_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002642 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002643 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06002644 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04002645 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2646 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2647 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2648 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002649 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002650 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2651 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002652 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002653 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08002654 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002655 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002656 case MSR_IA32_TSC:
2657 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
2658 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002659 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002660 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002661 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002662 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002663 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002664 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002665 /*
2666 * MSR_EBC_FREQUENCY_ID
2667 * Conservative value valid for even the basic CPU models.
2668 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2669 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2670 * and 266MHz for model 3, or 4. Set Core Clock
2671 * Frequency to System Bus Frequency Ratio to 1 (bits
2672 * 31:24) even though these are only valid for CPU
2673 * models > 2, however guests may end up dividing or
2674 * multiplying by zero otherwise.
2675 */
2676 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002677 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002678 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002679 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002680 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002681 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002682 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002683 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002684 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002685 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002686 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002687 break;
Will Auldba904632012-11-29 12:42:50 -08002688 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002689 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002690 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002691 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002692 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002693 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002694 case MSR_IA32_SMBASE:
2695 if (!msr_info->host_initiated)
2696 return 1;
2697 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002698 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002699 case MSR_SMI_COUNT:
2700 msr_info->data = vcpu->arch.smi_count;
2701 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002702 case MSR_IA32_PERF_STATUS:
2703 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002704 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002705 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002706 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002707 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002708 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002709 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002710 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002711 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002712 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002713 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002714 break;
2715 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002716 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002717 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002718 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002719 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002720 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02002721 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002722 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002723 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04002724 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002725 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002726 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002727 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002728 case MSR_IA32_P5_MC_ADDR:
2729 case MSR_IA32_P5_MC_TYPE:
2730 case MSR_IA32_MCG_CAP:
2731 case MSR_IA32_MCG_CTL:
2732 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002733 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002734 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
2735 msr_info->host_initiated);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002736 case MSR_K7_CLK_CTL:
2737 /*
2738 * Provide expected ramp-up count for K7. All other
2739 * are set to zero, indicating minimum divisors for
2740 * every field.
2741 *
2742 * This prevents guest kernels on AMD host with CPU
2743 * type 6, model 8 and higher from exploding due to
2744 * the rdmsr failing.
2745 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002746 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002747 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002748 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002749 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2750 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002751 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002752 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2753 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2754 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03002755 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02002756 msr_info->index, &msr_info->data,
2757 msr_info->host_initiated);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002758 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002759 case MSR_IA32_BBL_CR_CTL3:
2760 /* This legacy MSR exists but isn't fully documented in current
2761 * silicon. It is however accessed by winxp in very narrow
2762 * scenarios where it sets bit #19, itself documented as
2763 * a "reserved" bit. Best effort attempt to source coherent
2764 * read data here should the balance of the register be
2765 * interpreted by the guest:
2766 *
2767 * L2 cache control register 3: 64GB range, 256KB size,
2768 * enabled, latency 0x1, configured
2769 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002770 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05002771 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002772 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002773 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002774 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002775 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002776 break;
2777 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002778 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002779 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002780 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002781 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002782 case MSR_PLATFORM_INFO:
2783 msr_info->data = vcpu->arch.msr_platform_info;
2784 break;
2785 case MSR_MISC_FEATURES_ENABLES:
2786 msr_info->data = vcpu->arch.msr_misc_features_enables;
2787 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002788 default:
Wei Huangc6702c92015-06-19 13:44:45 +02002789 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002790 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002791 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002792 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
2793 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02002794 return 1;
2795 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002796 if (report_ignored_msrs)
2797 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
2798 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002799 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02002800 }
2801 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002802 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002803 return 0;
2804}
2805EXPORT_SYMBOL_GPL(kvm_get_msr_common);
2806
Carsten Otte313a3dc2007-10-11 19:16:52 +02002807/*
2808 * Read or write a bunch of msrs. All parameters are kernel addresses.
2809 *
2810 * @return number of msrs set successfully.
2811 */
2812static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2813 struct kvm_msr_entry *entries,
2814 int (*do_msr)(struct kvm_vcpu *vcpu,
2815 unsigned index, u64 *data))
2816{
Tom Lendacky801e4592018-02-21 13:39:51 -06002817 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002818
Carsten Otte313a3dc2007-10-11 19:16:52 +02002819 for (i = 0; i < msrs->nmsrs; ++i)
2820 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2821 break;
2822
Carsten Otte313a3dc2007-10-11 19:16:52 +02002823 return i;
2824}
2825
2826/*
2827 * Read or write a bunch of msrs. Parameters are user addresses.
2828 *
2829 * @return number of msrs set successfully.
2830 */
2831static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2832 int (*do_msr)(struct kvm_vcpu *vcpu,
2833 unsigned index, u64 *data),
2834 int writeback)
2835{
2836 struct kvm_msrs msrs;
2837 struct kvm_msr_entry *entries;
2838 int r, n;
2839 unsigned size;
2840
2841 r = -EFAULT;
2842 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2843 goto out;
2844
2845 r = -E2BIG;
2846 if (msrs.nmsrs >= MAX_IO_MSRS)
2847 goto out;
2848
Carsten Otte313a3dc2007-10-11 19:16:52 +02002849 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002850 entries = memdup_user(user_msrs->entries, size);
2851 if (IS_ERR(entries)) {
2852 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002853 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002854 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002855
2856 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2857 if (r < 0)
2858 goto out_free;
2859
2860 r = -EFAULT;
2861 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2862 goto out_free;
2863
2864 r = n;
2865
2866out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03002867 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002868out:
2869 return r;
2870}
2871
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002872static inline bool kvm_can_mwait_in_guest(void)
2873{
2874 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02002875 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
2876 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002877}
2878
Alexander Graf784aa3d2014-07-14 18:27:35 +02002879int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002880{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002881 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002882
2883 switch (ext) {
2884 case KVM_CAP_IRQCHIP:
2885 case KVM_CAP_HLT:
2886 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002887 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02002888 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02002889 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002890 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08002891 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05002892 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002893 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03002894 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08002895 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002896 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02002897 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04002898 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03002899 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002900 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04002901 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08002902 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07002903 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002904 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002905 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02002906 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02002907 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03002908 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03002909 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03002910 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03002911 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02002912 case KVM_CAP_HYPERV_TLBFLUSH:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08002913 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002914 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01002915 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002916 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02002917 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01002918 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05002919 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002920 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01002921 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05002922 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01002923 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03002924 case KVM_CAP_ENABLE_CAP_VM:
2925 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02002926 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07002927 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01002928 case KVM_CAP_IMMEDIATE_EXIT:
Tom Lendacky801e4592018-02-21 13:39:51 -06002929 case KVM_CAP_GET_MSR_FEATURES:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002930 r = 1;
2931 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08002932 case KVM_CAP_SYNC_REGS:
2933 r = KVM_SYNC_X86_VALID_FIELDS;
2934 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01002935 case KVM_CAP_ADJUST_CLOCK:
2936 r = KVM_CLOCK_TSC_STABLE;
2937 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002938 case KVM_CAP_X86_DISABLE_EXITS:
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03002939 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002940 if(kvm_can_mwait_in_guest())
2941 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02002942 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02002943 case KVM_CAP_X86_SMM:
2944 /* SMBASE is usually relocated above 1M on modern chipsets,
2945 * and SMM handlers might indeed rely on 4G segment limits,
2946 * so do not report SMM to be available if real mode is
2947 * emulated via vm86 mode. Still, do not go to great lengths
2948 * to avoid userspace's usage of the feature, because it is a
2949 * fringe case that is not enabled except via specific settings
2950 * of the module parameters.
2951 */
Tom Lendackybc226f02018-05-10 22:06:39 +02002952 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02002953 break;
Avi Kivity774ead32007-12-26 13:57:04 +02002954 case KVM_CAP_VAPIC:
2955 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
2956 break;
Avi Kivityf7252302008-02-20 11:53:16 +02002957 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03002958 r = KVM_SOFT_MAX_VCPUS;
2959 break;
2960 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02002961 r = KVM_MAX_VCPUS;
2962 break;
Avi Kivitya988b912008-02-20 11:59:20 +02002963 case KVM_CAP_NR_MEMSLOTS:
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002964 r = KVM_USER_MEM_SLOTS;
Avi Kivitya988b912008-02-20 11:59:20 +02002965 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03002966 case KVM_CAP_PV_MMU: /* obsolete */
2967 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002968 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002969 case KVM_CAP_MCE:
2970 r = KVM_MAX_MCE_BANKS;
2971 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002972 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02002973 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002974 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01002975 case KVM_CAP_TSC_CONTROL:
2976 r = kvm_has_tsc_control;
2977 break;
Radim Krčmář371313132016-07-12 22:09:27 +02002978 case KVM_CAP_X2APIC_API:
2979 r = KVM_X2APIC_API_VALID_FLAGS;
2980 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02002981 case KVM_CAP_NESTED_STATE:
2982 r = kvm_x86_ops->get_nested_state ?
2983 kvm_x86_ops->get_nested_state(NULL, 0, 0) : 0;
2984 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002985 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08002986 break;
2987 }
2988 return r;
2989
2990}
2991
Carsten Otte043405e2007-10-10 17:16:19 +02002992long kvm_arch_dev_ioctl(struct file *filp,
2993 unsigned int ioctl, unsigned long arg)
2994{
2995 void __user *argp = (void __user *)arg;
2996 long r;
2997
2998 switch (ioctl) {
2999 case KVM_GET_MSR_INDEX_LIST: {
3000 struct kvm_msr_list __user *user_msr_list = argp;
3001 struct kvm_msr_list msr_list;
3002 unsigned n;
3003
3004 r = -EFAULT;
3005 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3006 goto out;
3007 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003008 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Carsten Otte043405e2007-10-10 17:16:19 +02003009 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3010 goto out;
3011 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003012 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003013 goto out;
3014 r = -EFAULT;
3015 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3016 num_msrs_to_save * sizeof(u32)))
3017 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003018 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003019 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003020 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003021 goto out;
3022 r = 0;
3023 break;
3024 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003025 case KVM_GET_SUPPORTED_CPUID:
3026 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003027 struct kvm_cpuid2 __user *cpuid_arg = argp;
3028 struct kvm_cpuid2 cpuid;
3029
3030 r = -EFAULT;
3031 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3032 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003033
3034 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3035 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003036 if (r)
3037 goto out;
3038
3039 r = -EFAULT;
3040 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3041 goto out;
3042 r = 0;
3043 break;
3044 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003045 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003046 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003047 if (copy_to_user(argp, &kvm_mce_cap_supported,
3048 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003049 goto out;
3050 r = 0;
3051 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003052 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3053 struct kvm_msr_list __user *user_msr_list = argp;
3054 struct kvm_msr_list msr_list;
3055 unsigned int n;
3056
3057 r = -EFAULT;
3058 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3059 goto out;
3060 n = msr_list.nmsrs;
3061 msr_list.nmsrs = num_msr_based_features;
3062 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3063 goto out;
3064 r = -E2BIG;
3065 if (n < msr_list.nmsrs)
3066 goto out;
3067 r = -EFAULT;
3068 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3069 num_msr_based_features * sizeof(u32)))
3070 goto out;
3071 r = 0;
3072 break;
3073 }
3074 case KVM_GET_MSRS:
3075 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3076 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003077 }
Carsten Otte043405e2007-10-10 17:16:19 +02003078 default:
3079 r = -EINVAL;
3080 }
3081out:
3082 return r;
3083}
3084
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003085static void wbinvd_ipi(void *garbage)
3086{
3087 wbinvd();
3088}
3089
3090static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3091{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003092 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003093}
3094
Carsten Otte313a3dc2007-10-11 19:16:52 +02003095void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3096{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003097 /* Address WBINVD may be executed by guest */
3098 if (need_emulate_wbinvd(vcpu)) {
3099 if (kvm_x86_ops->has_wbinvd_exit())
3100 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3101 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3102 smp_call_function_single(vcpu->cpu,
3103 wbinvd_ipi, NULL, 1);
3104 }
3105
Carsten Otte313a3dc2007-10-11 19:16:52 +02003106 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003107
3108 /* Apply any externally detected TSC adjustments (due to suspend) */
3109 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3110 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3111 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003112 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003113 }
3114
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003115 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003116 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003117 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003118 if (tsc_delta < 0)
3119 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003120
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003121 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003122 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003123 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003124 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003125 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003126 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003127
3128 if (kvm_lapic_hv_timer_in_use(vcpu))
3129 kvm_lapic_restart_hv_timer(vcpu);
3130
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003131 /*
3132 * On a host with synchronized TSC, there is no need to update
3133 * kvmclock on vcpu->cpu migration
3134 */
3135 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03003136 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003137 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003138 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003139 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003140 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003141
Glauber Costac9aaa892011-07-11 15:28:14 -04003142 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003143}
3144
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003145static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3146{
3147 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3148 return;
3149
Wanpeng Lifa55eed2017-12-12 17:33:01 -08003150 vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003151
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003152 kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003153 &vcpu->arch.st.steal.preempted,
3154 offsetof(struct kvm_steal_time, preempted),
3155 sizeof(vcpu->arch.st.steal.preempted));
3156}
3157
Carsten Otte313a3dc2007-10-11 19:16:52 +02003158void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3159{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003160 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003161
3162 if (vcpu->preempted)
3163 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3164
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003165 /*
3166 * Disable page faults because we're in atomic context here.
3167 * kvm_write_guest_offset_cached() would call might_fault()
3168 * that relies on pagefault_disable() to tell if there's a
3169 * bug. NOTE: the write to guest memory may not go through if
3170 * during postcopy live migration or if there's heavy guest
3171 * paging.
3172 */
3173 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003174 /*
3175 * kvm_memslots() will be called by
3176 * kvm_write_guest_offset_cached() so take the srcu lock.
3177 */
3178 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003179 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003180 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003181 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003182 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003183 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003184 /*
3185 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3186 * on every vmexit, but if not, we might have a stale dr6 from the
3187 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
3188 */
3189 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003190}
3191
Carsten Otte313a3dc2007-10-11 19:16:52 +02003192static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3193 struct kvm_lapic_state *s)
3194{
Liran Alonfa59cc02017-12-24 18:12:53 +02003195 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003196 kvm_x86_ops->sync_pir_to_irr(vcpu);
3197
Radim Krčmářa92e2542016-07-12 22:09:22 +02003198 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003199}
3200
3201static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3202 struct kvm_lapic_state *s)
3203{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003204 int r;
3205
3206 r = kvm_apic_set_state(vcpu, s);
3207 if (r)
3208 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003209 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003210
3211 return 0;
3212}
3213
Matt Gingell127a4572015-11-17 17:32:05 +01003214static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3215{
3216 return (!lapic_in_kernel(vcpu) ||
3217 kvm_apic_accept_pic_intr(vcpu));
3218}
3219
Matt Gingell782d4222015-11-16 15:26:00 -08003220/*
3221 * if userspace requested an interrupt window, check that the
3222 * interrupt window is open.
3223 *
3224 * No need to exit to userspace if we already have an interrupt queued.
3225 */
3226static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3227{
3228 return kvm_arch_interrupt_allowed(vcpu) &&
3229 !kvm_cpu_has_interrupt(vcpu) &&
3230 !kvm_event_needs_reinjection(vcpu) &&
3231 kvm_cpu_accept_dm_intr(vcpu);
3232}
3233
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003234static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3235 struct kvm_interrupt *irq)
3236{
Chen Gang02cdb502013-02-27 11:33:25 +08003237 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003238 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003239
3240 if (!irqchip_in_kernel(vcpu->kvm)) {
3241 kvm_queue_interrupt(vcpu, irq->irq, false);
3242 kvm_make_request(KVM_REQ_EVENT, vcpu);
3243 return 0;
3244 }
3245
3246 /*
3247 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3248 * fail for in-kernel 8259.
3249 */
3250 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003251 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003252
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003253 if (vcpu->arch.pending_external_vector != -1)
3254 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003255
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003256 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003257 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003258 return 0;
3259}
3260
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003261static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3262{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003263 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003264
3265 return 0;
3266}
3267
Paolo Bonzinif0778252015-04-01 15:06:40 +02003268static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3269{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003270 kvm_make_request(KVM_REQ_SMI, vcpu);
3271
Paolo Bonzinif0778252015-04-01 15:06:40 +02003272 return 0;
3273}
3274
Avi Kivityb209749f2007-10-22 16:50:39 +02003275static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3276 struct kvm_tpr_access_ctl *tac)
3277{
3278 if (tac->flags)
3279 return -EINVAL;
3280 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3281 return 0;
3282}
3283
Huang Ying890ca9a2009-05-11 16:48:15 +08003284static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3285 u64 mcg_cap)
3286{
3287 int r;
3288 unsigned bank_num = mcg_cap & 0xff, bank;
3289
3290 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003291 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003292 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003293 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003294 goto out;
3295 r = 0;
3296 vcpu->arch.mcg_cap = mcg_cap;
3297 /* Init IA32_MCG_CTL to all 1s */
3298 if (mcg_cap & MCG_CTL_P)
3299 vcpu->arch.mcg_ctl = ~(u64)0;
3300 /* Init IA32_MCi_CTL to all 1s */
3301 for (bank = 0; bank < bank_num; bank++)
3302 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003303
3304 if (kvm_x86_ops->setup_mce)
3305 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003306out:
3307 return r;
3308}
3309
3310static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3311 struct kvm_x86_mce *mce)
3312{
3313 u64 mcg_cap = vcpu->arch.mcg_cap;
3314 unsigned bank_num = mcg_cap & 0xff;
3315 u64 *banks = vcpu->arch.mce_banks;
3316
3317 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3318 return -EINVAL;
3319 /*
3320 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3321 * reporting is disabled
3322 */
3323 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3324 vcpu->arch.mcg_ctl != ~(u64)0)
3325 return 0;
3326 banks += 4 * mce->bank;
3327 /*
3328 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3329 * reporting is disabled for the bank
3330 */
3331 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3332 return 0;
3333 if (mce->status & MCI_STATUS_UC) {
3334 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003335 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003336 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003337 return 0;
3338 }
3339 if (banks[1] & MCI_STATUS_VAL)
3340 mce->status |= MCI_STATUS_OVER;
3341 banks[2] = mce->addr;
3342 banks[3] = mce->misc;
3343 vcpu->arch.mcg_status = mce->mcg_status;
3344 banks[1] = mce->status;
3345 kvm_queue_exception(vcpu, MC_VECTOR);
3346 } else if (!(banks[1] & MCI_STATUS_VAL)
3347 || !(banks[1] & MCI_STATUS_UC)) {
3348 if (banks[1] & MCI_STATUS_VAL)
3349 mce->status |= MCI_STATUS_OVER;
3350 banks[2] = mce->addr;
3351 banks[3] = mce->misc;
3352 banks[1] = mce->status;
3353 } else
3354 banks[1] |= MCI_STATUS_OVER;
3355 return 0;
3356}
3357
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003358static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3359 struct kvm_vcpu_events *events)
3360{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003361 process_nmi(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07003362 /*
3363 * FIXME: pass injected and pending separately. This is only
3364 * needed for nested virtualization, whose state cannot be
3365 * migrated yet. For now we can combine them.
3366 */
Jan Kiszka03b82a32010-02-15 10:45:41 +01003367 events->exception.injected =
Wanpeng Li664f8e22017-08-24 03:35:09 -07003368 (vcpu->arch.exception.pending ||
3369 vcpu->arch.exception.injected) &&
Jan Kiszka03b82a32010-02-15 10:45:41 +01003370 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003371 events->exception.nr = vcpu->arch.exception.nr;
3372 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003373 events->exception.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003374 events->exception.error_code = vcpu->arch.exception.error_code;
3375
Jan Kiszka03b82a32010-02-15 10:45:41 +01003376 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003377 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003378 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003379 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003380 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003381
3382 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003383 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003384 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003385 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003386
Jan Kiszka66450a22013-03-13 12:42:34 +01003387 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003388
Paolo Bonzinif0778252015-04-01 15:06:40 +02003389 events->smi.smm = is_smm(vcpu);
3390 events->smi.pending = vcpu->arch.smi_pending;
3391 events->smi.smm_inside_nmi =
3392 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3393 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3394
Jan Kiszkadab4b912009-12-06 18:24:15 +01003395 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003396 | KVM_VCPUEVENT_VALID_SHADOW
3397 | KVM_VCPUEVENT_VALID_SMM);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003398 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003399}
3400
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003401static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags);
3402
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003403static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3404 struct kvm_vcpu_events *events)
3405{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003406 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003407 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003408 | KVM_VCPUEVENT_VALID_SHADOW
3409 | KVM_VCPUEVENT_VALID_SMM))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003410 return -EINVAL;
3411
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003412 if (events->exception.injected &&
Jim Mattson28d06352017-04-05 09:14:40 -07003413 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR ||
3414 is_guest_mode(vcpu)))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003415 return -EINVAL;
3416
David Hildenbrand28bf2882017-03-23 11:46:03 +01003417 /* INITs are latched while in SMM */
3418 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3419 (events->smi.smm || events->smi.pending) &&
3420 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3421 return -EINVAL;
3422
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003423 process_nmi(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07003424 vcpu->arch.exception.injected = false;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003425 vcpu->arch.exception.pending = events->exception.injected;
3426 vcpu->arch.exception.nr = events->exception.nr;
3427 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3428 vcpu->arch.exception.error_code = events->exception.error_code;
3429
Liran Alon04140b42018-03-23 03:01:31 +03003430 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003431 vcpu->arch.interrupt.nr = events->interrupt.nr;
3432 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003433 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3434 kvm_x86_ops->set_interrupt_shadow(vcpu,
3435 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003436
3437 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003438 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3439 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003440 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3441
Jan Kiszka66450a22013-03-13 12:42:34 +01003442 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003443 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003444 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003445
Paolo Bonzinif0778252015-04-01 15:06:40 +02003446 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003447 u32 hflags = vcpu->arch.hflags;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003448 if (events->smi.smm)
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003449 hflags |= HF_SMM_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003450 else
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003451 hflags &= ~HF_SMM_MASK;
3452 kvm_set_hflags(vcpu, hflags);
3453
Paolo Bonzinif0778252015-04-01 15:06:40 +02003454 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003455
3456 if (events->smi.smm) {
3457 if (events->smi.smm_inside_nmi)
3458 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003459 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003460 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
3461 if (lapic_in_kernel(vcpu)) {
3462 if (events->smi.latched_init)
3463 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3464 else
3465 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3466 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003467 }
3468 }
3469
Avi Kivity3842d132010-07-27 12:30:24 +03003470 kvm_make_request(KVM_REQ_EVENT, vcpu);
3471
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003472 return 0;
3473}
3474
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003475static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3476 struct kvm_debugregs *dbgregs)
3477{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003478 unsigned long val;
3479
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003480 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003481 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003482 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003483 dbgregs->dr7 = vcpu->arch.dr7;
3484 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003485 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003486}
3487
3488static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3489 struct kvm_debugregs *dbgregs)
3490{
3491 if (dbgregs->flags)
3492 return -EINVAL;
3493
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003494 if (dbgregs->dr6 & ~0xffffffffull)
3495 return -EINVAL;
3496 if (dbgregs->dr7 & ~0xffffffffull)
3497 return -EINVAL;
3498
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003499 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003500 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003501 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003502 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003503 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003504 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003505
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003506 return 0;
3507}
3508
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003509#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3510
3511static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3512{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003513 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003514 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003515 u64 valid;
3516
3517 /*
3518 * Copy legacy XSAVE area, to avoid complications with CPUID
3519 * leaves 0 and 1 in the loop below.
3520 */
3521 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3522
3523 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01003524 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003525 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3526
3527 /*
3528 * Copy each region from the possibly compacted offset to the
3529 * non-compacted offset.
3530 */
Dave Hansend91cab72015-09-02 16:31:26 -07003531 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003532 while (valid) {
3533 u64 feature = valid & -valid;
3534 int index = fls64(feature) - 1;
3535 void *src = get_xsave_addr(xsave, feature);
3536
3537 if (src) {
3538 u32 size, offset, ecx, edx;
3539 cpuid_count(XSTATE_CPUID, index,
3540 &size, &offset, &ecx, &edx);
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003541 if (feature == XFEATURE_MASK_PKRU)
3542 memcpy(dest + offset, &vcpu->arch.pkru,
3543 sizeof(vcpu->arch.pkru));
3544 else
3545 memcpy(dest + offset, src, size);
3546
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003547 }
3548
3549 valid -= feature;
3550 }
3551}
3552
3553static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3554{
Ingo Molnarc47ada32015-04-30 17:15:32 +02003555 struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003556 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3557 u64 valid;
3558
3559 /*
3560 * Copy legacy XSAVE area, to avoid complications with CPUID
3561 * leaves 0 and 1 in the loop below.
3562 */
3563 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3564
3565 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003566 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02003567 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02003568 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003569
3570 /*
3571 * Copy each region from the non-compacted offset to the
3572 * possibly compacted offset.
3573 */
Dave Hansend91cab72015-09-02 16:31:26 -07003574 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003575 while (valid) {
3576 u64 feature = valid & -valid;
3577 int index = fls64(feature) - 1;
3578 void *dest = get_xsave_addr(xsave, feature);
3579
3580 if (dest) {
3581 u32 size, offset, ecx, edx;
3582 cpuid_count(XSTATE_CPUID, index,
3583 &size, &offset, &ecx, &edx);
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003584 if (feature == XFEATURE_MASK_PKRU)
3585 memcpy(&vcpu->arch.pkru, src + offset,
3586 sizeof(vcpu->arch.pkru));
3587 else
3588 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08003589 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003590
3591 valid -= feature;
3592 }
3593}
3594
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003595static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3596 struct kvm_xsave *guest_xsave)
3597{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003598 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003599 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3600 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003601 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003602 memcpy(guest_xsave->region,
Ingo Molnar7366ed72015-04-27 04:19:39 +02003603 &vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003604 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003605 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07003606 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003607 }
3608}
3609
Wanpeng Lia5758132017-05-11 02:58:55 -07003610#define XSAVE_MXCSR_OFFSET 24
3611
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003612static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3613 struct kvm_xsave *guest_xsave)
3614{
3615 u64 xstate_bv =
3616 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07003617 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003618
Borislav Petkovd366bf72016-04-04 22:25:02 +02003619 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003620 /*
3621 * Here we allow setting states that are not present in
3622 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3623 * with old userspace.
3624 */
Wanpeng Lia5758132017-05-11 02:58:55 -07003625 if (xstate_bv & ~kvm_supported_xcr0() ||
3626 mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003627 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003628 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003629 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07003630 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
3631 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003632 return -EINVAL;
Ingo Molnar7366ed72015-04-27 04:19:39 +02003633 memcpy(&vcpu->arch.guest_fpu.state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003634 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003635 }
3636 return 0;
3637}
3638
3639static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3640 struct kvm_xcrs *guest_xcrs)
3641{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003642 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003643 guest_xcrs->nr_xcrs = 0;
3644 return;
3645 }
3646
3647 guest_xcrs->nr_xcrs = 1;
3648 guest_xcrs->flags = 0;
3649 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3650 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3651}
3652
3653static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3654 struct kvm_xcrs *guest_xcrs)
3655{
3656 int i, r = 0;
3657
Borislav Petkovd366bf72016-04-04 22:25:02 +02003658 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003659 return -EINVAL;
3660
3661 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3662 return -EINVAL;
3663
3664 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3665 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003666 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003667 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003668 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003669 break;
3670 }
3671 if (r)
3672 r = -EINVAL;
3673 return r;
3674}
3675
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003676/*
3677 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3678 * stopped by the hypervisor. This function will be called from the host only.
3679 * EINVAL is returned when the host attempts to set the flag for a guest that
3680 * does not support pv clocks.
3681 */
3682static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3683{
Andy Honig0b794592013-02-20 14:48:10 -08003684 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003685 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003686 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003687 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3688 return 0;
3689}
3690
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003691static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3692 struct kvm_enable_cap *cap)
3693{
3694 if (cap->flags)
3695 return -EINVAL;
3696
3697 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03003698 case KVM_CAP_HYPERV_SYNIC2:
3699 if (cap->args[0])
3700 return -EINVAL;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003701 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08003702 if (!irqchip_in_kernel(vcpu->kvm))
3703 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03003704 return kvm_hv_activate_synic(vcpu, cap->cap ==
3705 KVM_CAP_HYPERV_SYNIC2);
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003706 default:
3707 return -EINVAL;
3708 }
3709}
3710
Carsten Otte313a3dc2007-10-11 19:16:52 +02003711long kvm_arch_vcpu_ioctl(struct file *filp,
3712 unsigned int ioctl, unsigned long arg)
3713{
3714 struct kvm_vcpu *vcpu = filp->private_data;
3715 void __user *argp = (void __user *)arg;
3716 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003717 union {
3718 struct kvm_lapic_state *lapic;
3719 struct kvm_xsave *xsave;
3720 struct kvm_xcrs *xcrs;
3721 void *buffer;
3722 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003723
Christoffer Dall9b0624712017-12-04 21:35:36 +01003724 vcpu_load(vcpu);
3725
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003726 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003727 switch (ioctl) {
3728 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003729 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003730 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003731 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003732 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003733
Dave Hansenb772ff32008-08-11 10:01:47 -07003734 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003735 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003736 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003737 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003738 if (r)
3739 goto out;
3740 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003741 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003742 goto out;
3743 r = 0;
3744 break;
3745 }
3746 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003747 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003748 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003749 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003750 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01003751 if (IS_ERR(u.lapic)) {
3752 r = PTR_ERR(u.lapic);
3753 goto out_nofree;
3754 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02003755
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003756 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003757 break;
3758 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003759 case KVM_INTERRUPT: {
3760 struct kvm_interrupt irq;
3761
3762 r = -EFAULT;
3763 if (copy_from_user(&irq, argp, sizeof irq))
3764 goto out;
3765 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003766 break;
3767 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003768 case KVM_NMI: {
3769 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003770 break;
3771 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003772 case KVM_SMI: {
3773 r = kvm_vcpu_ioctl_smi(vcpu);
3774 break;
3775 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003776 case KVM_SET_CPUID: {
3777 struct kvm_cpuid __user *cpuid_arg = argp;
3778 struct kvm_cpuid cpuid;
3779
3780 r = -EFAULT;
3781 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3782 goto out;
3783 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003784 break;
3785 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02003786 case KVM_SET_CPUID2: {
3787 struct kvm_cpuid2 __user *cpuid_arg = argp;
3788 struct kvm_cpuid2 cpuid;
3789
3790 r = -EFAULT;
3791 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3792 goto out;
3793 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003794 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003795 break;
3796 }
3797 case KVM_GET_CPUID2: {
3798 struct kvm_cpuid2 __user *cpuid_arg = argp;
3799 struct kvm_cpuid2 cpuid;
3800
3801 r = -EFAULT;
3802 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3803 goto out;
3804 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00003805 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02003806 if (r)
3807 goto out;
3808 r = -EFAULT;
3809 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
3810 goto out;
3811 r = 0;
3812 break;
3813 }
Tom Lendacky801e4592018-02-21 13:39:51 -06003814 case KVM_GET_MSRS: {
3815 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003816 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06003817 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003818 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003819 }
3820 case KVM_SET_MSRS: {
3821 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003822 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06003823 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003824 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003825 }
Avi Kivityb209749f2007-10-22 16:50:39 +02003826 case KVM_TPR_ACCESS_REPORTING: {
3827 struct kvm_tpr_access_ctl tac;
3828
3829 r = -EFAULT;
3830 if (copy_from_user(&tac, argp, sizeof tac))
3831 goto out;
3832 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
3833 if (r)
3834 goto out;
3835 r = -EFAULT;
3836 if (copy_to_user(argp, &tac, sizeof tac))
3837 goto out;
3838 r = 0;
3839 break;
3840 };
Avi Kivityb93463a2007-10-25 16:52:32 +02003841 case KVM_SET_VAPIC_ADDR: {
3842 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01003843 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02003844
3845 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02003846 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02003847 goto out;
3848 r = -EFAULT;
3849 if (copy_from_user(&va, argp, sizeof va))
3850 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01003851 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08003852 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01003853 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02003854 break;
3855 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003856 case KVM_X86_SETUP_MCE: {
3857 u64 mcg_cap;
3858
3859 r = -EFAULT;
3860 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
3861 goto out;
3862 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
3863 break;
3864 }
3865 case KVM_X86_SET_MCE: {
3866 struct kvm_x86_mce mce;
3867
3868 r = -EFAULT;
3869 if (copy_from_user(&mce, argp, sizeof mce))
3870 goto out;
3871 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
3872 break;
3873 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003874 case KVM_GET_VCPU_EVENTS: {
3875 struct kvm_vcpu_events events;
3876
3877 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
3878
3879 r = -EFAULT;
3880 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
3881 break;
3882 r = 0;
3883 break;
3884 }
3885 case KVM_SET_VCPU_EVENTS: {
3886 struct kvm_vcpu_events events;
3887
3888 r = -EFAULT;
3889 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
3890 break;
3891
3892 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
3893 break;
3894 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003895 case KVM_GET_DEBUGREGS: {
3896 struct kvm_debugregs dbgregs;
3897
3898 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
3899
3900 r = -EFAULT;
3901 if (copy_to_user(argp, &dbgregs,
3902 sizeof(struct kvm_debugregs)))
3903 break;
3904 r = 0;
3905 break;
3906 }
3907 case KVM_SET_DEBUGREGS: {
3908 struct kvm_debugregs dbgregs;
3909
3910 r = -EFAULT;
3911 if (copy_from_user(&dbgregs, argp,
3912 sizeof(struct kvm_debugregs)))
3913 break;
3914
3915 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
3916 break;
3917 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003918 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003919 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003920 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003921 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003922 break;
3923
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003924 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003925
3926 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003927 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003928 break;
3929 r = 0;
3930 break;
3931 }
3932 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003933 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01003934 if (IS_ERR(u.xsave)) {
3935 r = PTR_ERR(u.xsave);
3936 goto out_nofree;
3937 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003938
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003939 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003940 break;
3941 }
3942 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003943 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003944 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003945 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003946 break;
3947
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003948 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003949
3950 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003951 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003952 sizeof(struct kvm_xcrs)))
3953 break;
3954 r = 0;
3955 break;
3956 }
3957 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003958 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01003959 if (IS_ERR(u.xcrs)) {
3960 r = PTR_ERR(u.xcrs);
3961 goto out_nofree;
3962 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003963
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003964 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003965 break;
3966 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01003967 case KVM_SET_TSC_KHZ: {
3968 u32 user_tsc_khz;
3969
3970 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003971 user_tsc_khz = (u32)arg;
3972
3973 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
3974 goto out;
3975
Zachary Amsdencc578282012-02-03 15:43:50 -02003976 if (user_tsc_khz == 0)
3977 user_tsc_khz = tsc_khz;
3978
Haozhong Zhang381d5852015-10-20 15:39:04 +08003979 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
3980 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003981
Joerg Roedel92a1f122011-03-25 09:44:51 +01003982 goto out;
3983 }
3984 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02003985 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003986 goto out;
3987 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003988 case KVM_KVMCLOCK_CTRL: {
3989 r = kvm_set_guest_paused(vcpu);
3990 goto out;
3991 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003992 case KVM_ENABLE_CAP: {
3993 struct kvm_enable_cap cap;
3994
3995 r = -EFAULT;
3996 if (copy_from_user(&cap, argp, sizeof(cap)))
3997 goto out;
3998 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3999 break;
4000 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004001 case KVM_GET_NESTED_STATE: {
4002 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4003 u32 user_data_size;
4004
4005 r = -EINVAL;
4006 if (!kvm_x86_ops->get_nested_state)
4007 break;
4008
4009 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
4010 if (get_user(user_data_size, &user_kvm_nested_state->size))
4011 return -EFAULT;
4012
4013 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4014 user_data_size);
4015 if (r < 0)
4016 return r;
4017
4018 if (r > user_data_size) {
4019 if (put_user(r, &user_kvm_nested_state->size))
4020 return -EFAULT;
4021 return -E2BIG;
4022 }
4023 r = 0;
4024 break;
4025 }
4026 case KVM_SET_NESTED_STATE: {
4027 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4028 struct kvm_nested_state kvm_state;
4029
4030 r = -EINVAL;
4031 if (!kvm_x86_ops->set_nested_state)
4032 break;
4033
4034 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
4035 return -EFAULT;
4036
4037 if (kvm_state.size < sizeof(kvm_state))
4038 return -EINVAL;
4039
4040 if (kvm_state.flags &
4041 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE))
4042 return -EINVAL;
4043
4044 /* nested_run_pending implies guest_mode. */
4045 if (kvm_state.flags == KVM_STATE_NESTED_RUN_PENDING)
4046 return -EINVAL;
4047
4048 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
4049 break;
4050 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004051 default:
4052 r = -EINVAL;
4053 }
4054out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004055 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004056out_nofree:
4057 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004058 return r;
4059}
4060
Souptick Joarder1499fa82018-04-19 00:49:58 +05304061vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004062{
4063 return VM_FAULT_SIGBUS;
4064}
4065
Carsten Otte1fe779f2007-10-29 16:08:35 +01004066static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4067{
4068 int ret;
4069
4070 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004071 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004072 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4073 return ret;
4074}
4075
Sheng Yangb927a3c2009-07-21 10:42:48 +08004076static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4077 u64 ident_addr)
4078{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004079 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004080}
4081
Carsten Otte1fe779f2007-10-29 16:08:35 +01004082static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
4083 u32 kvm_nr_mmu_pages)
4084{
4085 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4086 return -EINVAL;
4087
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004088 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004089
4090 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004091 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004092
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004093 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004094 return 0;
4095}
4096
4097static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
4098{
Dave Hansen39de71e2010-08-19 18:11:14 -07004099 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004100}
4101
Carsten Otte1fe779f2007-10-29 16:08:35 +01004102static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4103{
David Hildenbrand90bca052017-04-07 10:50:23 +02004104 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004105 int r;
4106
4107 r = 0;
4108 switch (chip->chip_id) {
4109 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004110 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004111 sizeof(struct kvm_pic_state));
4112 break;
4113 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004114 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004115 sizeof(struct kvm_pic_state));
4116 break;
4117 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004118 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004119 break;
4120 default:
4121 r = -EINVAL;
4122 break;
4123 }
4124 return r;
4125}
4126
4127static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4128{
David Hildenbrand90bca052017-04-07 10:50:23 +02004129 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004130 int r;
4131
4132 r = 0;
4133 switch (chip->chip_id) {
4134 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004135 spin_lock(&pic->lock);
4136 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004137 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004138 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004139 break;
4140 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004141 spin_lock(&pic->lock);
4142 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004143 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004144 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004145 break;
4146 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004147 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004148 break;
4149 default:
4150 r = -EINVAL;
4151 break;
4152 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004153 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004154 return r;
4155}
4156
Sheng Yange0f63cb2008-03-04 00:50:59 +08004157static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4158{
Radim Krčmář34f39412016-03-02 22:56:50 +01004159 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4160
4161 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4162
4163 mutex_lock(&kps->lock);
4164 memcpy(ps, &kps->channels, sizeof(*ps));
4165 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304166 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004167}
4168
4169static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4170{
Andrew Honig01856042015-11-18 14:50:23 -08004171 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004172 struct kvm_pit *pit = kvm->arch.vpit;
4173
4174 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004175 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004176 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004177 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4178 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304179 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004180}
4181
4182static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4183{
Beth Kone9f42752009-07-07 11:50:38 -04004184 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4185 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4186 sizeof(ps->channels));
4187 ps->flags = kvm->arch.vpit->pit_state.flags;
4188 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004189 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304190 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004191}
4192
4193static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4194{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304195 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004196 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004197 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004198 struct kvm_pit *pit = kvm->arch.vpit;
4199
4200 mutex_lock(&pit->pit_state.lock);
4201 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004202 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4203 if (!prev_legacy && cur_legacy)
4204 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004205 memcpy(&pit->pit_state.channels, &ps->channels,
4206 sizeof(pit->pit_state.channels));
4207 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004208 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004209 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004210 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004211 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304212 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004213}
4214
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004215static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4216 struct kvm_reinject_control *control)
4217{
Radim Krčmář71474e22016-03-02 22:56:45 +01004218 struct kvm_pit *pit = kvm->arch.vpit;
4219
4220 if (!pit)
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004221 return -ENXIO;
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004222
Radim Krčmář71474e22016-03-02 22:56:45 +01004223 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4224 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4225 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4226 */
4227 mutex_lock(&pit->pit_state.lock);
4228 kvm_pit_set_reinject(pit, control->pit_reinject);
4229 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004230
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004231 return 0;
4232}
4233
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004234/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004235 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
4236 * @kvm: kvm instance
4237 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004238 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004239 * Steps 1-4 below provide general overview of dirty page logging. See
4240 * kvm_get_dirty_log_protect() function description for additional details.
4241 *
4242 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
4243 * always flush the TLB (step 4) even if previous step failed and the dirty
4244 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
4245 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
4246 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004247 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004248 * 1. Take a snapshot of the bit and clear it if needed.
4249 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004250 * 3. Copy the snapshot to the userspace.
4251 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004252 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004253int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004254{
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004255 bool is_dirty = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004256 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004257
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004258 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004259
Kai Huang88178fd2015-01-28 10:54:27 +08004260 /*
4261 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4262 */
4263 if (kvm_x86_ops->flush_log_dirty)
4264 kvm_x86_ops->flush_log_dirty(kvm);
4265
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004266 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004267
4268 /*
4269 * All the TLBs can be flushed out of mmu lock, see the comments in
4270 * kvm_mmu_slot_remove_write_access().
4271 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004272 lockdep_assert_held(&kvm->slots_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004273 if (is_dirty)
4274 kvm_flush_remote_tlbs(kvm);
4275
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004276 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004277 return r;
4278}
4279
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004280int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4281 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004282{
4283 if (!irqchip_in_kernel(kvm))
4284 return -ENXIO;
4285
4286 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004287 irq_event->irq, irq_event->level,
4288 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004289 return 0;
4290}
4291
Nadav Amit90de4a12015-04-13 01:53:41 +03004292static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4293 struct kvm_enable_cap *cap)
4294{
4295 int r;
4296
4297 if (cap->flags)
4298 return -EINVAL;
4299
4300 switch (cap->cap) {
4301 case KVM_CAP_DISABLE_QUIRKS:
4302 kvm->arch.disabled_quirks = cap->args[0];
4303 r = 0;
4304 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004305 case KVM_CAP_SPLIT_IRQCHIP: {
4306 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004307 r = -EINVAL;
4308 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4309 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004310 r = -EEXIST;
4311 if (irqchip_in_kernel(kvm))
4312 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004313 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004314 goto split_irqchip_unlock;
4315 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004316 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004317 goto split_irqchip_unlock;
4318 /* Pairs with irqchip_in_kernel. */
4319 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004320 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004321 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004322 r = 0;
4323split_irqchip_unlock:
4324 mutex_unlock(&kvm->lock);
4325 break;
4326 }
Radim Krčmář371313132016-07-12 22:09:27 +02004327 case KVM_CAP_X2APIC_API:
4328 r = -EINVAL;
4329 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4330 break;
4331
4332 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4333 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004334 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4335 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004336
4337 r = 0;
4338 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004339 case KVM_CAP_X86_DISABLE_EXITS:
4340 r = -EINVAL;
4341 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4342 break;
4343
4344 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4345 kvm_can_mwait_in_guest())
4346 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004347 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004348 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004349 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4350 kvm->arch.pause_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004351 r = 0;
4352 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004353 default:
4354 r = -EINVAL;
4355 break;
4356 }
4357 return r;
4358}
4359
Carsten Otte1fe779f2007-10-29 16:08:35 +01004360long kvm_arch_vm_ioctl(struct file *filp,
4361 unsigned int ioctl, unsigned long arg)
4362{
4363 struct kvm *kvm = filp->private_data;
4364 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004365 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004366 /*
4367 * This union makes it completely explicit to gcc-3.x
4368 * that these two variables' stack usage should be
4369 * combined, not added together.
4370 */
4371 union {
4372 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004373 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004374 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004375 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004376
4377 switch (ioctl) {
4378 case KVM_SET_TSS_ADDR:
4379 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004380 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004381 case KVM_SET_IDENTITY_MAP_ADDR: {
4382 u64 ident_addr;
4383
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004384 mutex_lock(&kvm->lock);
4385 r = -EINVAL;
4386 if (kvm->created_vcpus)
4387 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004388 r = -EFAULT;
4389 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004390 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004391 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004392set_identity_unlock:
4393 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004394 break;
4395 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004396 case KVM_SET_NR_MMU_PAGES:
4397 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004398 break;
4399 case KVM_GET_NR_MMU_PAGES:
4400 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4401 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004402 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004403 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004404
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004405 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004406 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004407 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004408
Avi Kivity3e515702012-03-05 14:23:29 +02004409 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004410 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004411 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004412
4413 r = kvm_pic_init(kvm);
4414 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004415 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004416
4417 r = kvm_ioapic_init(kvm);
4418 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004419 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004420 goto create_irqchip_unlock;
4421 }
4422
Avi Kivity399ec802008-11-19 13:58:46 +02004423 r = kvm_setup_default_irq_routing(kvm);
4424 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004425 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004426 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004427 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004428 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004429 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004430 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004431 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004432 create_irqchip_unlock:
4433 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004434 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004435 }
Sheng Yang78376992008-01-28 05:10:22 +08004436 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004437 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4438 goto create_pit;
4439 case KVM_CREATE_PIT2:
4440 r = -EFAULT;
4441 if (copy_from_user(&u.pit_config, argp,
4442 sizeof(struct kvm_pit_config)))
4443 goto out;
4444 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004445 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004446 r = -EEXIST;
4447 if (kvm->arch.vpit)
4448 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004449 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004450 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004451 if (kvm->arch.vpit)
4452 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004453 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004454 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004455 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004456 case KVM_GET_IRQCHIP: {
4457 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004458 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004459
Sasha Levinff5c2c02011-12-04 19:36:29 +02004460 chip = memdup_user(argp, sizeof(*chip));
4461 if (IS_ERR(chip)) {
4462 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004463 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004464 }
4465
Carsten Otte1fe779f2007-10-29 16:08:35 +01004466 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004467 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004468 goto get_irqchip_out;
4469 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4470 if (r)
4471 goto get_irqchip_out;
4472 r = -EFAULT;
4473 if (copy_to_user(argp, chip, sizeof *chip))
4474 goto get_irqchip_out;
4475 r = 0;
4476 get_irqchip_out:
4477 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004478 break;
4479 }
4480 case KVM_SET_IRQCHIP: {
4481 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004482 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004483
Sasha Levinff5c2c02011-12-04 19:36:29 +02004484 chip = memdup_user(argp, sizeof(*chip));
4485 if (IS_ERR(chip)) {
4486 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004487 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004488 }
4489
Carsten Otte1fe779f2007-10-29 16:08:35 +01004490 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004491 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004492 goto set_irqchip_out;
4493 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
4494 if (r)
4495 goto set_irqchip_out;
4496 r = 0;
4497 set_irqchip_out:
4498 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004499 break;
4500 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08004501 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004502 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004503 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004504 goto out;
4505 r = -ENXIO;
4506 if (!kvm->arch.vpit)
4507 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004508 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004509 if (r)
4510 goto out;
4511 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004512 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004513 goto out;
4514 r = 0;
4515 break;
4516 }
4517 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004518 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004519 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004520 goto out;
4521 r = -ENXIO;
4522 if (!kvm->arch.vpit)
4523 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004524 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004525 break;
4526 }
Beth Kone9f42752009-07-07 11:50:38 -04004527 case KVM_GET_PIT2: {
4528 r = -ENXIO;
4529 if (!kvm->arch.vpit)
4530 goto out;
4531 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
4532 if (r)
4533 goto out;
4534 r = -EFAULT;
4535 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
4536 goto out;
4537 r = 0;
4538 break;
4539 }
4540 case KVM_SET_PIT2: {
4541 r = -EFAULT;
4542 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
4543 goto out;
4544 r = -ENXIO;
4545 if (!kvm->arch.vpit)
4546 goto out;
4547 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04004548 break;
4549 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004550 case KVM_REINJECT_CONTROL: {
4551 struct kvm_reinject_control control;
4552 r = -EFAULT;
4553 if (copy_from_user(&control, argp, sizeof(control)))
4554 goto out;
4555 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004556 break;
4557 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004558 case KVM_SET_BOOT_CPU_ID:
4559 r = 0;
4560 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02004561 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004562 r = -EBUSY;
4563 else
4564 kvm->arch.bsp_vcpu_id = arg;
4565 mutex_unlock(&kvm->lock);
4566 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07004567 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02004568 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07004569 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02004570 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07004571 goto out;
4572 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02004573 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07004574 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02004575 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07004576 r = 0;
4577 break;
4578 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004579 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004580 struct kvm_clock_data user_ns;
4581 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004582
4583 r = -EFAULT;
4584 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
4585 goto out;
4586
4587 r = -EINVAL;
4588 if (user_ns.flags)
4589 goto out;
4590
4591 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02004592 /*
4593 * TODO: userspace has to take care of races with VCPU_RUN, so
4594 * kvm_gen_update_masterclock() can be cut down to locked
4595 * pvclock_update_vm_gtod_copy().
4596 */
4597 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03004598 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02004599 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02004600 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004601 break;
4602 }
4603 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004604 struct kvm_clock_data user_ns;
4605 u64 now_ns;
4606
Marcelo Tosattie891a322017-04-17 12:51:37 -03004607 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02004608 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01004609 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004610 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004611
4612 r = -EFAULT;
4613 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
4614 goto out;
4615 r = 0;
4616 break;
4617 }
Nadav Amit90de4a12015-04-13 01:53:41 +03004618 case KVM_ENABLE_CAP: {
4619 struct kvm_enable_cap cap;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004620
Nadav Amit90de4a12015-04-13 01:53:41 +03004621 r = -EFAULT;
4622 if (copy_from_user(&cap, argp, sizeof(cap)))
4623 goto out;
4624 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
4625 break;
4626 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06004627 case KVM_MEMORY_ENCRYPT_OP: {
4628 r = -ENOTTY;
4629 if (kvm_x86_ops->mem_enc_op)
4630 r = kvm_x86_ops->mem_enc_op(kvm, argp);
4631 break;
4632 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06004633 case KVM_MEMORY_ENCRYPT_REG_REGION: {
4634 struct kvm_enc_region region;
4635
4636 r = -EFAULT;
4637 if (copy_from_user(&region, argp, sizeof(region)))
4638 goto out;
4639
4640 r = -ENOTTY;
4641 if (kvm_x86_ops->mem_enc_reg_region)
4642 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
4643 break;
4644 }
4645 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
4646 struct kvm_enc_region region;
4647
4648 r = -EFAULT;
4649 if (copy_from_user(&region, argp, sizeof(region)))
4650 goto out;
4651
4652 r = -ENOTTY;
4653 if (kvm_x86_ops->mem_enc_unreg_region)
4654 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
4655 break;
4656 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03004657 case KVM_HYPERV_EVENTFD: {
4658 struct kvm_hyperv_eventfd hvevfd;
4659
4660 r = -EFAULT;
4661 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
4662 goto out;
4663 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
4664 break;
4665 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004666 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02004667 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004668 }
4669out:
4670 return r;
4671}
4672
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08004673static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02004674{
4675 u32 dummy[2];
4676 unsigned i, j;
4677
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004678 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02004679 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
4680 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004681
4682 /*
4683 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01004684 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004685 */
4686 switch (msrs_to_save[i]) {
4687 case MSR_IA32_BNDCFGS:
4688 if (!kvm_x86_ops->mpx_supported())
4689 continue;
4690 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01004691 case MSR_TSC_AUX:
4692 if (!kvm_x86_ops->rdtscp_supported())
4693 continue;
4694 break;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004695 default:
4696 break;
4697 }
4698
Carsten Otte043405e2007-10-10 17:16:19 +02004699 if (j < i)
4700 msrs_to_save[j] = msrs_to_save[i];
4701 j++;
4702 }
4703 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004704
4705 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
Tom Lendackybc226f02018-05-10 22:06:39 +02004706 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs[i]))
4707 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004708
4709 if (j < i)
4710 emulated_msrs[j] = emulated_msrs[i];
4711 j++;
4712 }
4713 num_emulated_msrs = j;
Tom Lendacky801e4592018-02-21 13:39:51 -06004714
4715 for (i = j = 0; i < ARRAY_SIZE(msr_based_features); i++) {
4716 struct kvm_msr_entry msr;
4717
4718 msr.index = msr_based_features[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08004719 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06004720 continue;
4721
4722 if (j < i)
4723 msr_based_features[j] = msr_based_features[i];
4724 j++;
4725 }
4726 num_msr_based_features = j;
Carsten Otte043405e2007-10-10 17:16:19 +02004727}
4728
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004729static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
4730 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004731{
Avi Kivity70252a12010-01-19 12:51:22 +02004732 int handled = 0;
4733 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004734
Avi Kivity70252a12010-01-19 12:51:22 +02004735 do {
4736 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004737 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004738 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
4739 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004740 break;
4741 handled += n;
4742 addr += n;
4743 len -= n;
4744 v += n;
4745 } while (len);
4746
4747 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004748}
4749
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004750static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004751{
Avi Kivity70252a12010-01-19 12:51:22 +02004752 int handled = 0;
4753 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004754
Avi Kivity70252a12010-01-19 12:51:22 +02004755 do {
4756 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004757 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004758 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
4759 addr, n, v))
4760 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02004761 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08004762 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02004763 handled += n;
4764 addr += n;
4765 len -= n;
4766 v += n;
4767 } while (len);
4768
4769 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004770}
4771
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02004772static void kvm_set_segment(struct kvm_vcpu *vcpu,
4773 struct kvm_segment *var, int seg)
4774{
4775 kvm_x86_ops->set_segment(vcpu, var, seg);
4776}
4777
4778void kvm_get_segment(struct kvm_vcpu *vcpu,
4779 struct kvm_segment *var, int seg)
4780{
4781 kvm_x86_ops->get_segment(vcpu, var, seg);
4782}
4783
Paolo Bonzini54987b72014-09-02 13:23:06 +02004784gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
4785 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004786{
4787 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004788
4789 BUG_ON(!mmu_is_nested(vcpu));
4790
4791 /* NPT walks are always user-walks */
4792 access |= PFERR_USER_MASK;
Paolo Bonzini54987b72014-09-02 13:23:06 +02004793 t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004794
4795 return t_gpa;
4796}
4797
Avi Kivityab9ae312010-11-22 17:53:26 +02004798gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
4799 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004800{
4801 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02004802 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004803}
4804
Avi Kivityab9ae312010-11-22 17:53:26 +02004805 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
4806 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004807{
4808 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4809 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004810 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004811}
4812
Avi Kivityab9ae312010-11-22 17:53:26 +02004813gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
4814 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004815{
4816 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
4817 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02004818 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004819}
4820
4821/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02004822gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
4823 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004824{
Avi Kivityab9ae312010-11-22 17:53:26 +02004825 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004826}
4827
4828static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4829 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004830 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01004831{
4832 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004833 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004834
4835 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004836 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004837 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004838 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02004839 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004840 int ret;
4841
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004842 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004843 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004844 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
4845 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004846 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004847 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004848 goto out;
4849 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01004850
Izik Eidus77c20022008-12-29 01:42:19 +02004851 bytes -= toread;
4852 data += toread;
4853 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004854 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004855out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004856 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004857}
Izik Eidus77c20022008-12-29 01:42:19 +02004858
Gleb Natapov1871c602010-02-10 14:21:32 +02004859/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03004860static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
4861 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004862 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004863{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004864 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004865 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004866 unsigned offset;
4867 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004868
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004869 /* Inline kvm_read_guest_virt_helper for speed. */
4870 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
4871 exception);
4872 if (unlikely(gpa == UNMAPPED_GVA))
4873 return X86EMUL_PROPAGATE_FAULT;
4874
4875 offset = addr & (PAGE_SIZE-1);
4876 if (WARN_ON(offset + bytes > PAGE_SIZE))
4877 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004878 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
4879 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02004880 if (unlikely(ret < 0))
4881 return X86EMUL_IO_NEEDED;
4882
4883 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02004884}
4885
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004886int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03004887 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004888 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02004889{
4890 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03004891
Gleb Natapov1871c602010-02-10 14:21:32 +02004892 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004893 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004894}
Nadav Har'El064aea72011-05-25 23:04:56 +03004895EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02004896
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004897static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
4898 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02004899 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02004900{
Avi Kivity0f65dd72011-04-20 13:37:53 +03004901 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02004902 u32 access = 0;
4903
4904 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
4905 access |= PFERR_USER_MASK;
4906
4907 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02004908}
4909
Radim Krčmář7a036a62015-10-30 16:36:24 +01004910static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
4911 unsigned long addr, void *val, unsigned int bytes)
4912{
4913 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4914 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
4915
4916 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
4917}
4918
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004919static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
4920 struct kvm_vcpu *vcpu, u32 access,
4921 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02004922{
4923 void *data = val;
4924 int r = X86EMUL_CONTINUE;
4925
4926 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02004927 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004928 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02004929 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02004930 unsigned offset = addr & (PAGE_SIZE-1);
4931 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
4932 int ret;
4933
Avi Kivitybcc55cb2010-11-22 17:53:22 +02004934 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02004935 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004936 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02004937 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004938 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02004939 goto out;
4940 }
4941
4942 bytes -= towrite;
4943 data += towrite;
4944 addr += towrite;
4945 }
4946out:
4947 return r;
4948}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004949
4950static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02004951 unsigned int bytes, struct x86_exception *exception,
4952 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004953{
4954 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02004955 u32 access = PFERR_WRITE_MASK;
4956
4957 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
4958 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004959
4960 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02004961 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004962}
4963
4964int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
4965 unsigned int bytes, struct x86_exception *exception)
4966{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02004967 /* kvm_write_guest_virt_system can pull in tons of pages. */
4968 vcpu->arch.l1tf_flush_l1d = true;
4969
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02004970 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
4971 PFERR_WRITE_MASK, exception);
4972}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03004973EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02004974
Wanpeng Li082d06e2018-04-03 16:28:48 -07004975int handle_ud(struct kvm_vcpu *vcpu)
4976{
Wanpeng Li6c86eed2018-04-03 16:28:49 -07004977 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li082d06e2018-04-03 16:28:48 -07004978 enum emulation_result er;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07004979 char sig[5]; /* ud2; .ascii "kvm" */
4980 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07004981
Wanpeng Li6c86eed2018-04-03 16:28:49 -07004982 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02004983 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
4984 sig, sizeof(sig), &e) == 0 &&
Wanpeng Li6c86eed2018-04-03 16:28:49 -07004985 memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0) {
4986 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
4987 emul_type = 0;
4988 }
4989
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004990 er = kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07004991 if (er == EMULATE_USER_EXIT)
4992 return 0;
4993 if (er != EMULATE_DONE)
4994 kvm_queue_exception(vcpu, UD_VECTOR);
4995 return 1;
4996}
4997EXPORT_SYMBOL_GPL(handle_ud);
4998
Tom Lendacky0f89b202016-12-14 14:59:23 -05004999static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5000 gpa_t gpa, bool write)
5001{
5002 /* For APIC access vmexit */
5003 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5004 return 1;
5005
5006 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5007 trace_vcpu_match_mmio(gva, gpa, write, true);
5008 return 1;
5009 }
5010
5011 return 0;
5012}
5013
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005014static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5015 gpa_t *gpa, struct x86_exception *exception,
5016 bool write)
5017{
Avi Kivity97d64b72012-09-12 14:52:00 +03005018 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5019 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005020
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005021 /*
5022 * currently PKRU is only applied to ept enabled guest so
5023 * there is no pkey in EPT page table for L1 guest or EPT
5024 * shadow page table for L2 guest.
5025 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005026 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005027 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005028 vcpu->arch.access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005029 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5030 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005031 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005032 return 1;
5033 }
5034
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005035 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5036
5037 if (*gpa == UNMAPPED_GVA)
5038 return -1;
5039
Tom Lendacky0f89b202016-12-14 14:59:23 -05005040 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005041}
5042
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005043int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005044 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005045{
5046 int ret;
5047
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005048 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005049 if (ret < 0)
5050 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005051 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005052 return 1;
5053}
5054
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005055struct read_write_emulator_ops {
5056 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5057 int bytes);
5058 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5059 void *val, int bytes);
5060 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5061 int bytes, void *val);
5062 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5063 void *val, int bytes);
5064 bool write;
5065};
5066
5067static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5068{
5069 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005070 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005071 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005072 vcpu->mmio_read_completed = 0;
5073 return 1;
5074 }
5075
5076 return 0;
5077}
5078
5079static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5080 void *val, int bytes)
5081{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005082 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005083}
5084
5085static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5086 void *val, int bytes)
5087{
5088 return emulator_write_phys(vcpu, gpa, val, bytes);
5089}
5090
5091static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5092{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005093 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005094 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5095}
5096
5097static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5098 void *val, int bytes)
5099{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005100 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005101 return X86EMUL_IO_NEEDED;
5102}
5103
5104static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5105 void *val, int bytes)
5106{
Avi Kivityf78146b2012-04-18 19:22:47 +03005107 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5108
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005109 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005110 return X86EMUL_CONTINUE;
5111}
5112
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005113static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005114 .read_write_prepare = read_prepare,
5115 .read_write_emulate = read_emulate,
5116 .read_write_mmio = vcpu_mmio_read,
5117 .read_write_exit_mmio = read_exit_mmio,
5118};
5119
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005120static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005121 .read_write_emulate = write_emulate,
5122 .read_write_mmio = write_mmio,
5123 .read_write_exit_mmio = write_exit_mmio,
5124 .write = true,
5125};
5126
Xiao Guangrong22388a32011-07-13 14:32:31 +08005127static int emulator_read_write_onepage(unsigned long addr, void *val,
5128 unsigned int bytes,
5129 struct x86_exception *exception,
5130 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005131 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005132{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005133 gpa_t gpa;
5134 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005135 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005136 struct kvm_mmio_fragment *frag;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005137 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
5138
5139 /*
5140 * If the exit was due to a NPF we may already have a GPA.
5141 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5142 * Note, this cannot be used on string operations since string
5143 * operation using rep will only have the initial GPA from the NPF
5144 * occurred.
5145 */
5146 if (vcpu->arch.gpa_available &&
5147 emulator_can_use_gpa(ctxt) &&
Brijesh Singh618232e2017-08-17 18:36:57 +02005148 (addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
5149 gpa = vcpu->arch.gpa_val;
5150 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5151 } else {
5152 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5153 if (ret < 0)
5154 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005155 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005156
Brijesh Singh618232e2017-08-17 18:36:57 +02005157 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005158 return X86EMUL_CONTINUE;
5159
Carsten Ottebbd9b642007-10-30 18:44:21 +01005160 /*
5161 * Is this MMIO handled locally?
5162 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005163 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005164 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005165 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005166
Avi Kivity70252a12010-01-19 12:51:22 +02005167 gpa += handled;
5168 bytes -= handled;
5169 val += handled;
5170
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005171 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5172 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5173 frag->gpa = gpa;
5174 frag->data = val;
5175 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005176 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005177}
5178
Xiubo Li52eb5a62015-03-13 17:39:45 +08005179static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5180 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005181 void *val, unsigned int bytes,
5182 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005183 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005184{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005185 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005186 gpa_t gpa;
5187 int rc;
5188
5189 if (ops->read_write_prepare &&
5190 ops->read_write_prepare(vcpu, val, bytes))
5191 return X86EMUL_CONTINUE;
5192
5193 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005194
Carsten Ottebbd9b642007-10-30 18:44:21 +01005195 /* Crossing a page boundary? */
5196 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005197 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005198
5199 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005200 rc = emulator_read_write_onepage(addr, val, now, exception,
5201 vcpu, ops);
5202
Carsten Ottebbd9b642007-10-30 18:44:21 +01005203 if (rc != X86EMUL_CONTINUE)
5204 return rc;
5205 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005206 if (ctxt->mode != X86EMUL_MODE_PROT64)
5207 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005208 val += now;
5209 bytes -= now;
5210 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005211
Avi Kivityf78146b2012-04-18 19:22:47 +03005212 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5213 vcpu, ops);
5214 if (rc != X86EMUL_CONTINUE)
5215 return rc;
5216
5217 if (!vcpu->mmio_nr_fragments)
5218 return rc;
5219
5220 gpa = vcpu->mmio_fragments[0].gpa;
5221
5222 vcpu->mmio_needed = 1;
5223 vcpu->mmio_cur_fragment = 0;
5224
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005225 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005226 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5227 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5228 vcpu->run->mmio.phys_addr = gpa;
5229
5230 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005231}
5232
5233static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5234 unsigned long addr,
5235 void *val,
5236 unsigned int bytes,
5237 struct x86_exception *exception)
5238{
5239 return emulator_read_write(ctxt, addr, val, bytes,
5240 exception, &read_emultor);
5241}
5242
Xiubo Li52eb5a62015-03-13 17:39:45 +08005243static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005244 unsigned long addr,
5245 const void *val,
5246 unsigned int bytes,
5247 struct x86_exception *exception)
5248{
5249 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5250 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005251}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005252
Avi Kivitydaea3e72010-03-15 13:59:54 +02005253#define CMPXCHG_TYPE(t, ptr, old, new) \
5254 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5255
5256#ifdef CONFIG_X86_64
5257# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5258#else
5259# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005260 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005261#endif
5262
Avi Kivity0f65dd72011-04-20 13:37:53 +03005263static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5264 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005265 const void *old,
5266 const void *new,
5267 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005268 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005269{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005270 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005271 gpa_t gpa;
5272 struct page *page;
5273 char *kaddr;
5274 bool exchanged;
5275
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005276 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005277 if (bytes > 8 || (bytes & (bytes - 1)))
5278 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005279
Avi Kivitydaea3e72010-03-15 13:59:54 +02005280 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005281
Avi Kivitydaea3e72010-03-15 13:59:54 +02005282 if (gpa == UNMAPPED_GVA ||
5283 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5284 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005285
Avi Kivitydaea3e72010-03-15 13:59:54 +02005286 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5287 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005288
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005289 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08005290 if (is_error_page(page))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005291 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005292
Cong Wang8fd75e12011-11-25 23:14:17 +08005293 kaddr = kmap_atomic(page);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005294 kaddr += offset_in_page(gpa);
5295 switch (bytes) {
5296 case 1:
5297 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5298 break;
5299 case 2:
5300 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5301 break;
5302 case 4:
5303 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5304 break;
5305 case 8:
5306 exchanged = CMPXCHG64(kaddr, old, new);
5307 break;
5308 default:
5309 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005310 }
Cong Wang8fd75e12011-11-25 23:14:17 +08005311 kunmap_atomic(kaddr);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005312 kvm_release_page_dirty(page);
5313
5314 if (!exchanged)
5315 return X86EMUL_CMPXCHG_FAILED;
5316
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005317 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005318 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005319
5320 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005321
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005322emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005323 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005324
Avi Kivity0f65dd72011-04-20 13:37:53 +03005325 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005326}
5327
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005328static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5329{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005330 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005331
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005332 for (i = 0; i < vcpu->arch.pio.count; i++) {
5333 if (vcpu->arch.pio.in)
5334 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5335 vcpu->arch.pio.size, pd);
5336 else
5337 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5338 vcpu->arch.pio.port, vcpu->arch.pio.size,
5339 pd);
5340 if (r)
5341 break;
5342 pd += vcpu->arch.pio.size;
5343 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005344 return r;
5345}
5346
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005347static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5348 unsigned short port, void *val,
5349 unsigned int count, bool in)
5350{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005351 vcpu->arch.pio.port = port;
5352 vcpu->arch.pio.in = in;
5353 vcpu->arch.pio.count = count;
5354 vcpu->arch.pio.size = size;
5355
5356 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5357 vcpu->arch.pio.count = 0;
5358 return 1;
5359 }
5360
5361 vcpu->run->exit_reason = KVM_EXIT_IO;
5362 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5363 vcpu->run->io.size = size;
5364 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5365 vcpu->run->io.count = count;
5366 vcpu->run->io.port = port;
5367
5368 return 0;
5369}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005370
Avi Kivityca1d4a92011-04-20 13:37:53 +03005371static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5372 int size, unsigned short port, void *val,
5373 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005374{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005375 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005376 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005377
Gleb Natapov79729952010-03-18 15:20:24 +02005378 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005379 goto data_avail;
5380
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005381 memset(vcpu->arch.pio_data, 0, size * count);
5382
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005383 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5384 if (ret) {
5385data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005386 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005387 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005388 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005389 return 1;
5390 }
5391
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005392 return 0;
5393}
5394
Avi Kivityca1d4a92011-04-20 13:37:53 +03005395static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5396 int size, unsigned short port,
5397 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005398{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005399 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5400
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005401 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005402 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005403 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005404}
5405
Carsten Ottebbd9b642007-10-30 18:44:21 +01005406static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5407{
5408 return kvm_x86_ops->get_segment_base(vcpu, seg);
5409}
5410
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005411static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005412{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005413 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005414}
5415
Jiang Biaoae6a2372016-11-07 08:54:51 +08005416static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005417{
5418 if (!need_emulate_wbinvd(vcpu))
5419 return X86EMUL_CONTINUE;
5420
5421 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005422 int cpu = get_cpu();
5423
5424 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005425 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5426 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005427 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005428 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005429 } else
5430 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005431 return X86EMUL_CONTINUE;
5432}
Joel Schopp5cb56052015-03-02 13:43:31 -06005433
5434int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
5435{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005436 kvm_emulate_wbinvd_noskip(vcpu);
5437 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06005438}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005439EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
5440
Joel Schopp5cb56052015-03-02 13:43:31 -06005441
5442
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005443static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
5444{
Joel Schopp5cb56052015-03-02 13:43:31 -06005445 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005446}
5447
Xiubo Li52eb5a62015-03-13 17:39:45 +08005448static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
5449 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005450{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005451 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005452}
5453
Xiubo Li52eb5a62015-03-13 17:39:45 +08005454static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
5455 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005456{
Gleb Natapov338dbc92010-04-28 19:15:32 +03005457
Avi Kivity717746e2011-04-20 13:37:53 +03005458 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005459}
5460
Gleb Natapov52a46612010-03-18 15:20:03 +02005461static u64 mk_cr_64(u64 curr_cr, u32 new_val)
5462{
5463 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
5464}
5465
Avi Kivity717746e2011-04-20 13:37:53 +03005466static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02005467{
Avi Kivity717746e2011-04-20 13:37:53 +03005468 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02005469 unsigned long value;
5470
5471 switch (cr) {
5472 case 0:
5473 value = kvm_read_cr0(vcpu);
5474 break;
5475 case 2:
5476 value = vcpu->arch.cr2;
5477 break;
5478 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005479 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02005480 break;
5481 case 4:
5482 value = kvm_read_cr4(vcpu);
5483 break;
5484 case 8:
5485 value = kvm_get_cr8(vcpu);
5486 break;
5487 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03005488 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02005489 return 0;
5490 }
5491
5492 return value;
5493}
5494
Avi Kivity717746e2011-04-20 13:37:53 +03005495static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02005496{
Avi Kivity717746e2011-04-20 13:37:53 +03005497 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03005498 int res = 0;
5499
Gleb Natapov52a46612010-03-18 15:20:03 +02005500 switch (cr) {
5501 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03005502 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02005503 break;
5504 case 2:
5505 vcpu->arch.cr2 = val;
5506 break;
5507 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005508 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02005509 break;
5510 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03005511 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02005512 break;
5513 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005514 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02005515 break;
5516 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03005517 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03005518 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02005519 }
Gleb Natapov0f122442010-04-28 19:15:31 +03005520
5521 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02005522}
5523
Avi Kivity717746e2011-04-20 13:37:53 +03005524static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02005525{
Avi Kivity717746e2011-04-20 13:37:53 +03005526 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02005527}
5528
Avi Kivity4bff1e862011-04-20 13:37:53 +03005529static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005530{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005531 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005532}
5533
Avi Kivity4bff1e862011-04-20 13:37:53 +03005534static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005535{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005536 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005537}
5538
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03005539static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
5540{
5541 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
5542}
5543
5544static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
5545{
5546 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
5547}
5548
Avi Kivity4bff1e862011-04-20 13:37:53 +03005549static unsigned long emulator_get_cached_segment_base(
5550 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03005551{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005552 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03005553}
5554
Avi Kivity1aa36612011-04-27 13:20:30 +03005555static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
5556 struct desc_struct *desc, u32 *base3,
5557 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005558{
5559 struct kvm_segment var;
5560
Avi Kivity4bff1e862011-04-20 13:37:53 +03005561 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03005562 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005563
Gleb Natapov378a8b02013-01-21 15:36:48 +02005564 if (var.unusable) {
5565 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02005566 if (base3)
5567 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005568 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02005569 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005570
5571 if (var.g)
5572 var.limit >>= 12;
5573 set_desc_limit(desc, var.limit);
5574 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02005575#ifdef CONFIG_X86_64
5576 if (base3)
5577 *base3 = var.base >> 32;
5578#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005579 desc->type = var.type;
5580 desc->s = var.s;
5581 desc->dpl = var.dpl;
5582 desc->p = var.present;
5583 desc->avl = var.avl;
5584 desc->l = var.l;
5585 desc->d = var.db;
5586 desc->g = var.g;
5587
5588 return true;
5589}
5590
Avi Kivity1aa36612011-04-27 13:20:30 +03005591static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
5592 struct desc_struct *desc, u32 base3,
5593 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005594{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005595 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005596 struct kvm_segment var;
5597
Avi Kivity1aa36612011-04-27 13:20:30 +03005598 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005599 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02005600#ifdef CONFIG_X86_64
5601 var.base |= ((u64)base3) << 32;
5602#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005603 var.limit = get_desc_limit(desc);
5604 if (desc->g)
5605 var.limit = (var.limit << 12) | 0xfff;
5606 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005607 var.dpl = desc->dpl;
5608 var.db = desc->d;
5609 var.s = desc->s;
5610 var.l = desc->l;
5611 var.g = desc->g;
5612 var.avl = desc->avl;
5613 var.present = desc->p;
5614 var.unusable = !var.present;
5615 var.padding = 0;
5616
5617 kvm_set_segment(vcpu, &var, seg);
5618 return;
5619}
5620
Avi Kivity717746e2011-04-20 13:37:53 +03005621static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
5622 u32 msr_index, u64 *pdata)
5623{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005624 struct msr_data msr;
5625 int r;
5626
5627 msr.index = msr_index;
5628 msr.host_initiated = false;
5629 r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
5630 if (r)
5631 return r;
5632
5633 *pdata = msr.data;
5634 return 0;
Avi Kivity717746e2011-04-20 13:37:53 +03005635}
5636
5637static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
5638 u32 msr_index, u64 data)
5639{
Will Auld8fe8ab42012-11-29 12:42:12 -08005640 struct msr_data msr;
5641
5642 msr.data = data;
5643 msr.index = msr_index;
5644 msr.host_initiated = false;
5645 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03005646}
5647
Paolo Bonzini64d60672015-05-07 11:36:11 +02005648static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
5649{
5650 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5651
5652 return vcpu->arch.smbase;
5653}
5654
5655static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
5656{
5657 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5658
5659 vcpu->arch.smbase = smbase;
5660}
5661
Nadav Amit67f4d422014-06-02 18:34:09 +03005662static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
5663 u32 pmc)
5664{
Wei Huangc6702c92015-06-19 13:44:45 +02005665 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03005666}
5667
Avi Kivity222d21a2011-11-10 14:57:30 +02005668static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
5669 u32 pmc, u64 *pdata)
5670{
Wei Huangc6702c92015-06-19 13:44:45 +02005671 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02005672}
5673
Avi Kivity6c3287f2011-04-20 15:43:05 +03005674static void emulator_halt(struct x86_emulate_ctxt *ctxt)
5675{
5676 emul_to_vcpu(ctxt)->arch.halt_request = 1;
5677}
5678
Avi Kivity29535382011-04-20 13:37:53 +03005679static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005680 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02005681 enum x86_intercept_stage stage)
5682{
Avi Kivity29535382011-04-20 13:37:53 +03005683 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005684}
5685
Yu Zhange911eb32017-08-24 20:27:52 +08005686static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
5687 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005688{
Yu Zhange911eb32017-08-24 20:27:52 +08005689 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005690}
5691
Avi Kivitydd856ef2012-08-27 23:46:17 +03005692static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
5693{
5694 return kvm_register_read(emul_to_vcpu(ctxt), reg);
5695}
5696
5697static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
5698{
5699 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
5700}
5701
Nadav Amit801806d2015-01-26 09:32:23 +02005702static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
5703{
5704 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
5705}
5706
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005707static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
5708{
5709 return emul_to_vcpu(ctxt)->arch.hflags;
5710}
5711
5712static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
5713{
5714 kvm_set_hflags(emul_to_vcpu(ctxt), emul_flags);
5715}
5716
Ladi Prosek0234bf82017-10-11 16:54:40 +02005717static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt, u64 smbase)
5718{
5719 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smbase);
5720}
5721
Mathias Krause0225fb52012-08-30 01:30:16 +02005722static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03005723 .read_gpr = emulator_read_gpr,
5724 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005725 .read_std = emulator_read_std,
5726 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01005727 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02005728 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005729 .read_emulated = emulator_read_emulated,
5730 .write_emulated = emulator_write_emulated,
5731 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005732 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005733 .pio_in_emulated = emulator_pio_in_emulated,
5734 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03005735 .get_segment = emulator_get_segment,
5736 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03005737 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005738 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005739 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03005740 .set_gdt = emulator_set_gdt,
5741 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02005742 .get_cr = emulator_get_cr,
5743 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02005744 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03005745 .get_dr = emulator_get_dr,
5746 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02005747 .get_smbase = emulator_get_smbase,
5748 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03005749 .set_msr = emulator_set_msr,
5750 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03005751 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02005752 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03005753 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005754 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03005755 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02005756 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01005757 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02005758 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02005759 .get_hflags = emulator_get_hflags,
5760 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02005761 .pre_leave_smm = emulator_pre_leave_smm,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005762};
5763
Gleb Natapov95cb2292010-04-28 19:15:43 +03005764static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
5765{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02005766 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03005767 /*
5768 * an sti; sti; sequence only disable interrupts for the first
5769 * instruction. So, if the last instruction, be it emulated or
5770 * not, left the system with the INT_STI flag enabled, it
5771 * means that the last instruction is an sti. We should not
5772 * leave the flag on in this case. The same goes for mov ss
5773 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02005774 if (int_shadow & mask)
5775 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005776 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03005777 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01005778 if (!mask)
5779 kvm_make_request(KVM_REQ_EVENT, vcpu);
5780 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03005781}
5782
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005783static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03005784{
5785 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02005786 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005787 return kvm_propagate_fault(vcpu, &ctxt->exception);
5788
5789 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02005790 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
5791 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03005792 else
Avi Kivityda9cb572010-11-22 17:53:21 +02005793 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02005794 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03005795}
5796
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005797static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
5798{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005799 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005800 int cs_db, cs_l;
5801
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005802 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
5803
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005804 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02005805 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
5806
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005807 ctxt->eip = kvm_rip_read(vcpu);
5808 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
5809 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03005810 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005811 cs_db ? X86EMUL_MODE_PROT32 :
5812 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02005813 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005814 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
5815 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09005816
Avi Kivitydd856ef2012-08-27 23:46:17 +03005817 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02005818 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03005819}
5820
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005821int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02005822{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005823 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02005824 int ret;
5825
5826 init_emulate_ctxt(vcpu);
5827
Avi Kivity9dac77f2011-06-01 15:34:25 +03005828 ctxt->op_bytes = 2;
5829 ctxt->ad_bytes = 2;
5830 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005831 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02005832
5833 if (ret != X86EMUL_CONTINUE)
5834 return EMULATE_FAIL;
5835
Avi Kivity9dac77f2011-06-01 15:34:25 +03005836 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09005837 kvm_rip_write(vcpu, ctxt->eip);
5838 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02005839
Mohammed Gamal63995652010-09-19 14:34:06 +02005840 return EMULATE_DONE;
5841}
5842EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
5843
Liran Alone2366172018-03-12 13:12:49 +02005844static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005845{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005846 int r = EMULATE_DONE;
5847
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005848 ++vcpu->stat.insn_emulation_fail;
5849 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02005850
5851 if (emulation_type & EMULTYPE_NO_UD_ON_FAIL)
5852 return EMULATE_FAIL;
5853
Nadav Amita2b9e6c2014-09-17 02:50:50 +03005854 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005855 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5856 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5857 vcpu->run->internal.ndata = 0;
Liran Alon1f4dcb32017-11-05 16:56:33 +02005858 r = EMULATE_USER_EXIT;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005859 }
Liran Alone2366172018-03-12 13:12:49 +02005860
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005861 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01005862
5863 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005864}
5865
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005866static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03005867 bool write_fault_to_shadow_pgtable,
5868 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03005869{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005870 gpa_t gpa = cr2;
Dan Williamsba049e92016-01-15 16:56:11 -08005871 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005872
Sean Christopherson384bf222018-08-23 13:56:49 -07005873 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Gleb Natapov991eebf2013-04-11 12:10:51 +03005874 return false;
5875
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07005876 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
5877 return false;
5878
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005879 if (!vcpu->arch.mmu.direct_map) {
5880 /*
5881 * Write permission should be allowed since only
5882 * write access need to be emulated.
5883 */
5884 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03005885
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005886 /*
5887 * If the mapping is invalid in guest, let cpu retry
5888 * it to generate fault.
5889 */
5890 if (gpa == UNMAPPED_GVA)
5891 return true;
5892 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005893
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005894 /*
5895 * Do not retry the unhandleable instruction if it faults on the
5896 * readonly host memory, otherwise it will goto a infinite loop:
5897 * retry instruction -> write #PF -> emulation fail -> retry
5898 * instruction -> ...
5899 */
5900 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005901
5902 /*
5903 * If the instruction failed on the error pfn, it can not be fixed,
5904 * report the error to userspace.
5905 */
5906 if (is_error_noslot_pfn(pfn))
5907 return false;
5908
5909 kvm_release_pfn_clean(pfn);
5910
5911 /* The instructions are well-emulated on direct mmu. */
5912 if (vcpu->arch.mmu.direct_map) {
5913 unsigned int indirect_shadow_pages;
5914
5915 spin_lock(&vcpu->kvm->mmu_lock);
5916 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
5917 spin_unlock(&vcpu->kvm->mmu_lock);
5918
5919 if (indirect_shadow_pages)
5920 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
5921
Gleb Natapova6f177e2010-07-08 12:41:12 +03005922 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08005923 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03005924
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08005925 /*
5926 * if emulation was due to access to shadowed page table
5927 * and it failed try to unshadow page and re-enter the
5928 * guest to let CPU execute the instruction.
5929 */
5930 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08005931
5932 /*
5933 * If the access faults on its page table, it can not
5934 * be fixed by unprotecting shadow page and it should
5935 * be reported to userspace.
5936 */
5937 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03005938}
5939
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005940static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
5941 unsigned long cr2, int emulation_type)
5942{
5943 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5944 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
5945
5946 last_retry_eip = vcpu->arch.last_retry_eip;
5947 last_retry_addr = vcpu->arch.last_retry_addr;
5948
5949 /*
5950 * If the emulation is caused by #PF and it is non-page_table
5951 * writing instruction, it means the VM-EXIT is caused by shadow
5952 * page protected, we can zap the shadow page and retry this
5953 * instruction directly.
5954 *
5955 * Note: if the guest uses a non-page-table modifying instruction
5956 * on the PDE that points to the instruction, then we will unmap
5957 * the instruction and go to an infinite loop. So, we cache the
5958 * last retried eip and the last fault address, if we meet the eip
5959 * and the address again, we can break out of the potential infinite
5960 * loop.
5961 */
5962 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
5963
Sean Christopherson384bf222018-08-23 13:56:49 -07005964 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005965 return false;
5966
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07005967 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
5968 return false;
5969
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005970 if (x86_page_table_writing_insn(ctxt))
5971 return false;
5972
5973 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
5974 return false;
5975
5976 vcpu->arch.last_retry_eip = ctxt->eip;
5977 vcpu->arch.last_retry_addr = cr2;
5978
5979 if (!vcpu->arch.mmu.direct_map)
5980 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
5981
Xiao Guangrong22368022013-01-13 23:44:12 +08005982 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005983
5984 return true;
5985}
5986
Gleb Natapov716d51a2012-09-03 15:24:26 +03005987static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
5988static int complete_emulated_pio(struct kvm_vcpu *vcpu);
5989
Paolo Bonzini64d60672015-05-07 11:36:11 +02005990static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02005991{
Paolo Bonzini64d60672015-05-07 11:36:11 +02005992 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02005993 /* This is a good place to trace that we are exiting SMM. */
5994 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
5995
Paolo Bonzinic43203c2016-06-01 22:26:00 +02005996 /* Process a latched INIT or SMI, if any. */
5997 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02005998 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02005999
6000 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006001}
6002
6003static void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
6004{
6005 unsigned changed = vcpu->arch.hflags ^ emul_flags;
6006
Paolo Bonzinia5845392015-04-01 18:18:53 +02006007 vcpu->arch.hflags = emul_flags;
Paolo Bonzini64d60672015-05-07 11:36:11 +02006008
6009 if (changed & HF_SMM_MASK)
6010 kvm_smm_changed(vcpu);
Paolo Bonzinia5845392015-04-01 18:18:53 +02006011}
6012
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006013static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6014 unsigned long *db)
6015{
6016 u32 dr6 = 0;
6017 int i;
6018 u32 enable, rwlen;
6019
6020 enable = dr7;
6021 rwlen = dr7 >> 16;
6022 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6023 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6024 dr6 |= (1 << i);
6025 return dr6;
6026}
6027
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006028static void kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006029{
6030 struct kvm_run *kvm_run = vcpu->run;
6031
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006032 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6033 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6034 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6035 kvm_run->debug.arch.exception = DB_VECTOR;
6036 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6037 *r = EMULATE_USER_EXIT;
6038 } else {
6039 /*
6040 * "Certain debug exceptions may clear bit 0-3. The
6041 * remaining contents of the DR6 register are never
6042 * cleared by the processor".
6043 */
6044 vcpu->arch.dr6 &= ~15;
6045 vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
6046 kvm_queue_exception(vcpu, DB_VECTOR);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006047 }
6048}
6049
Kyle Huey6affcbe2016-11-29 12:40:40 -08006050int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6051{
6052 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
6053 int r = EMULATE_DONE;
6054
6055 kvm_x86_ops->skip_emulated_instruction(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006056
6057 /*
6058 * rflags is the old, "raw" value of the flags. The new value has
6059 * not been saved yet.
6060 *
6061 * This is correct even for TF set by the guest, because "the
6062 * processor will not generate this exception after the instruction
6063 * that sets the TF flag".
6064 */
6065 if (unlikely(rflags & X86_EFLAGS_TF))
6066 kvm_vcpu_do_singlestep(vcpu, &r);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006067 return r == EMULATE_DONE;
6068}
6069EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6070
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006071static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6072{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006073 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6074 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006075 struct kvm_run *kvm_run = vcpu->run;
6076 unsigned long eip = kvm_get_linear_rip(vcpu);
6077 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006078 vcpu->arch.guest_debug_dr7,
6079 vcpu->arch.eff_db);
6080
6081 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006082 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006083 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006084 kvm_run->debug.arch.exception = DB_VECTOR;
6085 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6086 *r = EMULATE_USER_EXIT;
6087 return true;
6088 }
6089 }
6090
Nadav Amit4161a562014-07-17 01:19:31 +03006091 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6092 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006093 unsigned long eip = kvm_get_linear_rip(vcpu);
6094 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006095 vcpu->arch.dr7,
6096 vcpu->arch.db);
6097
6098 if (dr6 != 0) {
6099 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006100 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006101 kvm_queue_exception(vcpu, DB_VECTOR);
6102 *r = EMULATE_DONE;
6103 return true;
6104 }
6105 }
6106
6107 return false;
6108}
6109
Liran Alon04789b62018-03-12 13:12:50 +02006110static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6111{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006112 switch (ctxt->opcode_len) {
6113 case 1:
6114 switch (ctxt->b) {
6115 case 0xe4: /* IN */
6116 case 0xe5:
6117 case 0xec:
6118 case 0xed:
6119 case 0xe6: /* OUT */
6120 case 0xe7:
6121 case 0xee:
6122 case 0xef:
6123 case 0x6c: /* INS */
6124 case 0x6d:
6125 case 0x6e: /* OUTS */
6126 case 0x6f:
6127 return true;
6128 }
6129 break;
6130 case 2:
6131 switch (ctxt->b) {
6132 case 0x33: /* RDPMC */
6133 return true;
6134 }
6135 break;
Liran Alon04789b62018-03-12 13:12:50 +02006136 }
6137
6138 return false;
6139}
6140
Andre Przywara51d8b662010-12-21 11:12:02 +01006141int x86_emulate_instruction(struct kvm_vcpu *vcpu,
6142 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01006143 int emulation_type,
6144 void *insn,
6145 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006146{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006147 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006148 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006149 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006150 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006151
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006152 vcpu->arch.l1tf_flush_l1d = true;
6153
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006154 /*
6155 * Clear write_fault_to_shadow_pgtable here to ensure it is
6156 * never reused.
6157 */
6158 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006159 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006160
Sheng Yang571008d2008-01-02 14:49:22 +08006161 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006162 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006163
6164 /*
6165 * We will reenter on the same instruction since
6166 * we do not set complete_userspace_io. This does not
6167 * handle watchpoints yet, those would be handled in
6168 * the emulate_ops.
6169 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006170 if (!(emulation_type & EMULTYPE_SKIP) &&
6171 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006172 return r;
6173
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006174 ctxt->interruptibility = 0;
6175 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006176 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006177 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006178
Borislav Petkovb51e9742013-09-22 16:44:52 +02006179 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006180
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006181 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006182
Avi Kivitye46479f2010-04-11 13:05:16 +03006183 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006184 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006185 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02006186 if (emulation_type & EMULTYPE_TRAP_UD)
6187 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03006188 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6189 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006190 return EMULATE_DONE;
Paolo Bonzini6ea6e842017-11-10 10:49:38 +01006191 if (ctxt->have_exception && inject_emulated_exception(vcpu))
6192 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006193 if (emulation_type & EMULTYPE_SKIP)
6194 return EMULATE_FAIL;
Liran Alone2366172018-03-12 13:12:49 +02006195 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006196 }
6197 }
6198
Liran Alon04789b62018-03-12 13:12:50 +02006199 if ((emulation_type & EMULTYPE_VMWARE) &&
6200 !is_vmware_backdoor_opcode(ctxt))
6201 return EMULATE_FAIL;
6202
Gleb Natapovba8afb62009-04-12 13:36:57 +03006203 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006204 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006205 if (ctxt->eflags & X86_EFLAGS_RF)
6206 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Gleb Natapovba8afb62009-04-12 13:36:57 +03006207 return EMULATE_DONE;
6208 }
6209
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006210 if (retry_instruction(ctxt, cr2, emulation_type))
6211 return EMULATE_DONE;
6212
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006213 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006214 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006215 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6216 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006217 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006218 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006219
Gleb Natapov5cd21912010-03-18 15:20:26 +02006220restart:
Tom Lendacky0f89b202016-12-14 14:59:23 -05006221 /* Save the faulting GPA (cr2) in the address field */
6222 ctxt->exception.address = cr2;
6223
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006224 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006225
Joerg Roedel775fde82011-04-04 12:39:24 +02006226 if (r == EMULATION_INTERCEPTED)
6227 return EMULATE_DONE;
6228
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006229 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03006230 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6231 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006232 return EMULATE_DONE;
6233
Liran Alone2366172018-03-12 13:12:49 +02006234 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006235 }
6236
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006237 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006238 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006239 if (inject_emulated_exception(vcpu))
6240 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006241 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006242 if (!vcpu->arch.pio.in) {
6243 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006244 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006245 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006246 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006247 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6248 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006249 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006250 } else if (vcpu->mmio_needed) {
6251 if (!vcpu->mmio_is_write)
6252 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006253 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006254 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006255 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006256 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006257 else
6258 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006259
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006260 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006261 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006262 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006263 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006264 kvm_rip_write(vcpu, ctxt->eip);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006265 if (r == EMULATE_DONE &&
6266 (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
6267 kvm_vcpu_do_singlestep(vcpu, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02006268 if (!ctxt->have_exception ||
6269 exception_type(ctxt->exception.vector) == EXCPT_TRAP)
6270 __kvm_set_rflags(vcpu, ctxt->eflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006271
6272 /*
6273 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6274 * do nothing, and it will be requested again as soon as
6275 * the shadow expires. But we still need to check here,
6276 * because POPF has no interrupt shadow.
6277 */
6278 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6279 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006280 } else
6281 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006282
Gleb Natapove85d28f2010-07-29 15:11:52 +03006283 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006284}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006285
6286int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6287{
6288 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6289}
6290EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6291
6292int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6293 void *insn, int insn_len)
6294{
6295 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6296}
6297EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006298
Sean Christophersondca7f122018-03-08 08:57:27 -08006299static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6300 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006301{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006302 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006303 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
6304 size, port, &val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006305 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02006306 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02006307 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01006308}
Carsten Ottede7d7892007-10-30 18:44:25 +01006309
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006310static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6311{
6312 unsigned long val;
6313
6314 /* We should only ever be called with arch.pio.count equal to 1 */
6315 BUG_ON(vcpu->arch.pio.count != 1);
6316
6317 /* For size less than 4 we merge, else we zero extend */
6318 val = (vcpu->arch.pio.size < 4) ? kvm_register_read(vcpu, VCPU_REGS_RAX)
6319 : 0;
6320
6321 /*
6322 * Since vcpu->arch.pio.count == 1 let emulator_pio_in_emulated perform
6323 * the copy and tracing
6324 */
6325 emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, vcpu->arch.pio.size,
6326 vcpu->arch.pio.port, &val, 1);
6327 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
6328
6329 return 1;
6330}
6331
Sean Christophersondca7f122018-03-08 08:57:27 -08006332static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6333 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006334{
6335 unsigned long val;
6336 int ret;
6337
6338 /* For size less than 4 we merge, else we zero extend */
6339 val = (size < 4) ? kvm_register_read(vcpu, VCPU_REGS_RAX) : 0;
6340
6341 ret = emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, size, port,
6342 &val, 1);
6343 if (ret) {
6344 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
6345 return ret;
6346 }
6347
6348 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
6349
6350 return 0;
6351}
Sean Christophersondca7f122018-03-08 08:57:27 -08006352
6353int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
6354{
6355 int ret = kvm_skip_emulated_instruction(vcpu);
6356
6357 /*
6358 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6359 * KVM_EXIT_DEBUG here.
6360 */
6361 if (in)
6362 return kvm_fast_pio_in(vcpu, size, port) && ret;
6363 else
6364 return kvm_fast_pio_out(vcpu, size, port) && ret;
6365}
6366EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006367
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006368static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006369{
Tejun Heo0a3aee02010-12-18 16:28:55 +01006370 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006371 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006372}
6373
6374static void tsc_khz_changed(void *data)
6375{
6376 struct cpufreq_freqs *freq = data;
6377 unsigned long khz = 0;
6378
6379 if (data)
6380 khz = freq->new;
6381 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
6382 khz = cpufreq_quick_get(raw_smp_processor_id());
6383 if (!khz)
6384 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01006385 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006386}
6387
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006388#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006389static void kvm_hyperv_tsc_notifier(void)
6390{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006391 struct kvm *kvm;
6392 struct kvm_vcpu *vcpu;
6393 int cpu;
6394
6395 spin_lock(&kvm_lock);
6396 list_for_each_entry(kvm, &vm_list, vm_list)
6397 kvm_make_mclock_inprogress_request(kvm);
6398
6399 hyperv_stop_tsc_emulation();
6400
6401 /* TSC frequency always matches when on Hyper-V */
6402 for_each_present_cpu(cpu)
6403 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
6404 kvm_max_guest_tsc_khz = tsc_khz;
6405
6406 list_for_each_entry(kvm, &vm_list, vm_list) {
6407 struct kvm_arch *ka = &kvm->arch;
6408
6409 spin_lock(&ka->pvclock_gtod_sync_lock);
6410
6411 pvclock_update_vm_gtod_copy(kvm);
6412
6413 kvm_for_each_vcpu(cpu, vcpu, kvm)
6414 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
6415
6416 kvm_for_each_vcpu(cpu, vcpu, kvm)
6417 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
6418
6419 spin_unlock(&ka->pvclock_gtod_sync_lock);
6420 }
6421 spin_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006422}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006423#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006424
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006425static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
6426 void *data)
6427{
6428 struct cpufreq_freqs *freq = data;
6429 struct kvm *kvm;
6430 struct kvm_vcpu *vcpu;
6431 int i, send_ipi = 0;
6432
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006433 /*
6434 * We allow guests to temporarily run on slowing clocks,
6435 * provided we notify them after, or to run on accelerating
6436 * clocks, provided we notify them before. Thus time never
6437 * goes backwards.
6438 *
6439 * However, we have a problem. We can't atomically update
6440 * the frequency of a given CPU from this function; it is
6441 * merely a notifier, which can be called from any CPU.
6442 * Changing the TSC frequency at arbitrary points in time
6443 * requires a recomputation of local variables related to
6444 * the TSC for each VCPU. We must flag these local variables
6445 * to be updated and be sure the update takes place with the
6446 * new frequency before any guests proceed.
6447 *
6448 * Unfortunately, the combination of hotplug CPU and frequency
6449 * change creates an intractable locking scenario; the order
6450 * of when these callouts happen is undefined with respect to
6451 * CPU hotplug, and they can race with each other. As such,
6452 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
6453 * undefined; you can actually have a CPU frequency change take
6454 * place in between the computation of X and the setting of the
6455 * variable. To protect against this problem, all updates of
6456 * the per_cpu tsc_khz variable are done in an interrupt
6457 * protected IPI, and all callers wishing to update the value
6458 * must wait for a synchronous IPI to complete (which is trivial
6459 * if the caller is on the CPU already). This establishes the
6460 * necessary total order on variable updates.
6461 *
6462 * Note that because a guest time update may take place
6463 * anytime after the setting of the VCPU's request bit, the
6464 * correct TSC value must be set before the request. However,
6465 * to ensure the update actually makes it to any guest which
6466 * starts running in hardware virtualization between the set
6467 * and the acquisition of the spinlock, we must also ping the
6468 * CPU after setting the request bit.
6469 *
6470 */
6471
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006472 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
6473 return 0;
6474 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
6475 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006476
6477 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006478
Paolo Bonzini2f303b72013-09-25 13:53:07 +02006479 spin_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006480 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03006481 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006482 if (vcpu->cpu != freq->cpu)
6483 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10006484 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006485 if (vcpu->cpu != smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006486 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006487 }
6488 }
Paolo Bonzini2f303b72013-09-25 13:53:07 +02006489 spin_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006490
6491 if (freq->old < freq->new && send_ipi) {
6492 /*
6493 * We upscale the frequency. Must make the guest
6494 * doesn't see old kvmclock values while running with
6495 * the new frequency, otherwise we risk the guest sees
6496 * time go backwards.
6497 *
6498 * In case we update the frequency for another cpu
6499 * (which might be in guest context) send an interrupt
6500 * to kick the cpu out of guest context. Next time
6501 * guest context is entered kvmclock will be updated,
6502 * so the guest will not see stale values.
6503 */
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006504 smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006505 }
6506 return 0;
6507}
6508
6509static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006510 .notifier_call = kvmclock_cpufreq_notifier
6511};
6512
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006513static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006514{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006515 tsc_khz_changed(NULL);
6516 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006517}
6518
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006519static void kvm_timer_init(void)
6520{
Zachary Amsdenc2855452010-09-18 14:38:15 -10006521 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05306522
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006523 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10006524#ifdef CONFIG_CPU_FREQ
6525 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02006526 int cpu;
6527
Zachary Amsdenc2855452010-09-18 14:38:15 -10006528 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02006529 cpu = get_cpu();
6530 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10006531 if (policy.cpuinfo.max_freq)
6532 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02006533 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10006534#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006535 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
6536 CPUFREQ_TRANSITION_NOTIFIER);
6537 }
Zachary Amsdenc2855452010-09-18 14:38:15 -10006538 pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05306539
Thomas Gleixner73c1b412016-12-21 20:19:54 +01006540 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006541 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006542}
6543
Andi Kleendd60d212017-07-25 17:20:32 -07006544DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
6545EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006546
Gleb Natapovf5132b02011-11-10 14:57:22 +02006547int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006548{
Alex,Shi086c9852011-10-20 15:34:01 +08006549 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006550}
6551
6552static int kvm_is_user_mode(void)
6553{
6554 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006555
Alex,Shi086c9852011-10-20 15:34:01 +08006556 if (__this_cpu_read(current_vcpu))
6557 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006558
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006559 return user_mode != 0;
6560}
6561
6562static unsigned long kvm_get_guest_ip(void)
6563{
6564 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006565
Alex,Shi086c9852011-10-20 15:34:01 +08006566 if (__this_cpu_read(current_vcpu))
6567 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006568
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006569 return ip;
6570}
6571
6572static struct perf_guest_info_callbacks kvm_guest_cbs = {
6573 .is_in_guest = kvm_is_in_guest,
6574 .is_user_mode = kvm_is_user_mode,
6575 .get_guest_ip = kvm_get_guest_ip,
6576};
6577
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006578static void kvm_set_mmio_spte_mask(void)
6579{
6580 u64 mask;
6581 int maxphyaddr = boot_cpu_data.x86_phys_bits;
6582
6583 /*
6584 * Set the reserved bits and the present bit of an paging-structure
6585 * entry to generate page fault with PFER.RSV = 1.
6586 */
Junaid Shahid28a1f3a2018-08-14 10:15:34 -07006587
6588 /*
6589 * Mask the uppermost physical address bit, which would be reserved as
6590 * long as the supported physical address width is less than 52.
6591 */
6592 mask = 1ull << 51;
Xiao Guangrong885032b2013-06-07 16:51:23 +08006593
Xiao Guangrong885032b2013-06-07 16:51:23 +08006594 /* Set the present bit. */
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006595 mask |= 1ull;
6596
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006597 /*
6598 * If reserved bit is not supported, clear the present bit to disable
6599 * mmio page fault.
6600 */
Arnd Bergmann7288bde2018-08-20 23:37:50 +02006601 if (IS_ENABLED(CONFIG_X86_64) && maxphyaddr == 52)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006602 mask &= ~1ull;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006603
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006604 kvm_mmu_set_mmio_spte_mask(mask, mask);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006605}
6606
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006607#ifdef CONFIG_X86_64
6608static void pvclock_gtod_update_fn(struct work_struct *work)
6609{
Marcelo Tosattid8281992012-11-27 23:29:01 -02006610 struct kvm *kvm;
6611
6612 struct kvm_vcpu *vcpu;
6613 int i;
6614
Paolo Bonzini2f303b72013-09-25 13:53:07 +02006615 spin_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006616 list_for_each_entry(kvm, &vm_list, vm_list)
6617 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08006618 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006619 atomic_set(&kvm_guest_has_master_clock, 0);
Paolo Bonzini2f303b72013-09-25 13:53:07 +02006620 spin_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006621}
6622
6623static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
6624
6625/*
6626 * Notification about pvclock gtod data update.
6627 */
6628static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
6629 void *priv)
6630{
6631 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
6632 struct timekeeper *tk = priv;
6633
6634 update_pvclock_gtod(tk);
6635
6636 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01006637 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006638 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01006639 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006640 atomic_read(&kvm_guest_has_master_clock) != 0)
6641 queue_work(system_long_wq, &pvclock_gtod_work);
6642
6643 return 0;
6644}
6645
6646static struct notifier_block pvclock_gtod_notifier = {
6647 .notifier_call = pvclock_gtod_notify,
6648};
6649#endif
6650
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006651int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02006652{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006653 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02006654 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006655
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006656 if (kvm_x86_ops) {
6657 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006658 r = -EEXIST;
6659 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006660 }
6661
6662 if (!ops->cpu_has_kvm_support()) {
6663 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006664 r = -EOPNOTSUPP;
6665 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006666 }
6667 if (ops->disabled_by_bios()) {
6668 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006669 r = -EOPNOTSUPP;
6670 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006671 }
6672
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02006673 r = -ENOMEM;
6674 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
6675 if (!shared_msrs) {
6676 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
6677 goto out;
6678 }
6679
Avi Kivity97db56c2008-01-13 13:23:56 +02006680 r = kvm_mmu_module_init();
6681 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02006682 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02006683
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006684 kvm_set_mmio_spte_mask();
Avi Kivity97db56c2008-01-13 13:23:56 +02006685
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006686 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01006687
Sheng Yang7b523452008-04-25 21:13:50 +08006688 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04006689 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006690 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006691 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006692
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006693 perf_register_guest_info_callbacks(&kvm_guest_cbs);
6694
Borislav Petkovd366bf72016-04-04 22:25:02 +02006695 if (boot_cpu_has(X86_FEATURE_XSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +08006696 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
6697
Gleb Natapovc5cc4212012-08-05 15:58:30 +03006698 kvm_lapic_init();
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006699#ifdef CONFIG_X86_64
6700 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006701
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006702 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006703 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006704#endif
6705
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006706 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006707
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02006708out_free_percpu:
6709 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006710out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006711 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02006712}
Hollis Blanchard8776e512007-10-31 17:24:24 -05006713
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006714void kvm_arch_exit(void)
6715{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006716#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006717 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006718 clear_hv_tscchange_cb();
6719#endif
David Matlackcef84c32016-12-16 14:30:36 -08006720 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006721 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
6722
Jan Kiszka888d2562009-04-17 19:24:58 +02006723 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
6724 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
6725 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006726 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006727#ifdef CONFIG_X86_64
6728 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
6729#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006730 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006731 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02006732 free_percpu(shared_msrs);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08006733}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08006734
Joel Schopp5cb56052015-03-02 13:43:31 -06006735int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05006736{
6737 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02006738 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03006739 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05006740 return 1;
6741 } else {
6742 vcpu->run->exit_reason = KVM_EXIT_HLT;
6743 return 0;
6744 }
6745}
Joel Schopp5cb56052015-03-02 13:43:31 -06006746EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
6747
6748int kvm_emulate_halt(struct kvm_vcpu *vcpu)
6749{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006750 int ret = kvm_skip_emulated_instruction(vcpu);
6751 /*
6752 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
6753 * KVM_EXIT_DEBUG here.
6754 */
6755 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06006756}
Hollis Blanchard8776e512007-10-31 17:24:24 -05006757EXPORT_SYMBOL_GPL(kvm_emulate_halt);
6758
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01006759#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02006760static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
6761 unsigned long clock_type)
6762{
6763 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02006764 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01006765 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02006766 int ret;
6767
6768 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
6769 return -KVM_EOPNOTSUPP;
6770
6771 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
6772 return -KVM_EOPNOTSUPP;
6773
6774 clock_pairing.sec = ts.tv_sec;
6775 clock_pairing.nsec = ts.tv_nsec;
6776 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
6777 clock_pairing.flags = 0;
6778
6779 ret = 0;
6780 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
6781 sizeof(struct kvm_clock_pairing)))
6782 ret = -KVM_EFAULT;
6783
6784 return ret;
6785}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01006786#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02006787
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306788/*
6789 * kvm_pv_kick_cpu_op: Kick a vcpu.
6790 *
6791 * @apicid - apicid of vcpu to be kicked.
6792 */
6793static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
6794{
Raghavendra K T24d21662013-08-26 14:18:35 +05306795 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306796
Raghavendra K T24d21662013-08-26 14:18:35 +05306797 lapic_irq.shorthand = 0;
6798 lapic_irq.dest_mode = 0;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08006799 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05306800 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06006801 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306802
Raghavendra K T24d21662013-08-26 14:18:35 +05306803 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08006804 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306805}
6806
Andrey Smetanind62caab2015-11-10 15:36:33 +03006807void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
6808{
6809 vcpu->arch.apicv_active = false;
6810 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
6811}
6812
Hollis Blanchard8776e512007-10-31 17:24:24 -05006813int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
6814{
6815 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03006816 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05006817
Radim Krčmář696ca772018-05-24 17:50:56 +02006818 if (kvm_hv_hypercall_enabled(vcpu->kvm))
6819 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02006820
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006821 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
6822 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
6823 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
6824 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
6825 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006826
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006827 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006828
Nadav Amita449c7a2014-06-18 17:19:24 +03006829 op_64_bit = is_64_bit_mode(vcpu);
6830 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05006831 nr &= 0xFFFFFFFF;
6832 a0 &= 0xFFFFFFFF;
6833 a1 &= 0xFFFFFFFF;
6834 a2 &= 0xFFFFFFFF;
6835 a3 &= 0xFFFFFFFF;
6836 }
6837
Jan Kiszka07708c42009-08-03 18:43:28 +02006838 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
6839 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02006840 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02006841 }
6842
Hollis Blanchard8776e512007-10-31 17:24:24 -05006843 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02006844 case KVM_HC_VAPIC_POLL_IRQ:
6845 ret = 0;
6846 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05306847 case KVM_HC_KICK_CPU:
6848 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
6849 ret = 0;
6850 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01006851#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02006852 case KVM_HC_CLOCK_PAIRING:
6853 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
6854 break;
Wanpeng Li4180bf12018-07-23 14:39:54 +08006855 case KVM_HC_SEND_IPI:
6856 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
6857 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01006858#endif
Hollis Blanchard8776e512007-10-31 17:24:24 -05006859 default:
6860 ret = -KVM_ENOSYS;
6861 break;
6862 }
Radim Krčmář696ca772018-05-24 17:50:56 +02006863out:
Nadav Amita449c7a2014-06-18 17:19:24 +03006864 if (!op_64_bit)
6865 ret = (u32)ret;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006866 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03006867
Amit Shahf11c3a82008-02-21 01:00:30 +05306868 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03006869 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006870}
6871EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
6872
Jan Kiszkab6785de2012-09-20 07:43:17 +02006873static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05006874{
Avi Kivityd6aa1002011-04-20 15:47:13 +03006875 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006876 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006877 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006878
Hollis Blanchard8776e512007-10-31 17:24:24 -05006879 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006880
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01006881 return emulator_write_emulated(ctxt, rip, instruction, 3,
6882 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05006883}
6884
Avi Kivity851ba692009-08-24 11:10:17 +03006885static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006886{
Matt Gingell782d4222015-11-16 15:26:00 -08006887 return vcpu->run->request_interrupt_window &&
6888 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006889}
6890
Avi Kivity851ba692009-08-24 11:10:17 +03006891static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006892{
Avi Kivity851ba692009-08-24 11:10:17 +03006893 struct kvm_run *kvm_run = vcpu->run;
6894
Jan Kiszka91586a32009-10-05 13:07:21 +02006895 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02006896 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02006897 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006898 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01006899 kvm_run->ready_for_interrupt_injection =
6900 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08006901 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05006902}
6903
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006904static void update_cr8_intercept(struct kvm_vcpu *vcpu)
6905{
6906 int max_irr, tpr;
6907
6908 if (!kvm_x86_ops->update_cr8_intercept)
6909 return;
6910
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01006911 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03006912 return;
6913
Andrey Smetanind62caab2015-11-10 15:36:33 +03006914 if (vcpu->arch.apicv_active)
6915 return;
6916
Gleb Natapov8db3baa2009-05-11 13:35:54 +03006917 if (!vcpu->arch.apic->vapic_addr)
6918 max_irr = kvm_lapic_find_highest_irr(vcpu);
6919 else
6920 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006921
6922 if (max_irr != -1)
6923 max_irr >>= 4;
6924
6925 tpr = kvm_lapic_get_cr8(vcpu);
6926
6927 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
6928}
6929
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006930static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006931{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006932 int r;
6933
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006934 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07006935
Liran Alon1a680e32018-03-23 03:01:33 +03006936 if (vcpu->arch.exception.injected)
6937 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07006938 /*
Liran Alona042c262018-03-23 03:01:32 +03006939 * Do not inject an NMI or interrupt if there is a pending
6940 * exception. Exceptions and interrupts are recognized at
6941 * instruction boundaries, i.e. the start of an instruction.
6942 * Trap-like exceptions, e.g. #DB, have higher priority than
6943 * NMIs and interrupts, i.e. traps are recognized before an
6944 * NMI/interrupt that's pending on the same instruction.
6945 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
6946 * priority, but are only generated (pended) during instruction
6947 * execution, i.e. a pending fault-like exception means the
6948 * fault occurred on the *previous* instruction and must be
6949 * serviced prior to recognizing any new events in order to
6950 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07006951 */
Liran Alon1a680e32018-03-23 03:01:33 +03006952 else if (!vcpu->arch.exception.pending) {
6953 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07006954 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03006955 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07006956 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07006957 }
6958
Liran Alon1a680e32018-03-23 03:01:33 +03006959 /*
6960 * Call check_nested_events() even if we reinjected a previous event
6961 * in order for caller to determine if it should require immediate-exit
6962 * from L2 to L1 due to pending L1 events which require exit
6963 * from L2 to L1.
6964 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07006965 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
6966 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
6967 if (r != 0)
6968 return r;
6969 }
6970
6971 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03006972 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02006973 trace_kvm_inj_exception(vcpu->arch.exception.nr,
6974 vcpu->arch.exception.has_error_code,
6975 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03006976
Liran Alon1a680e32018-03-23 03:01:33 +03006977 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07006978 vcpu->arch.exception.pending = false;
6979 vcpu->arch.exception.injected = true;
6980
Nadav Amitd6e8c852014-07-24 14:51:24 +03006981 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
6982 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
6983 X86_EFLAGS_RF);
6984
Nadav Amit6bdf0662014-09-30 20:49:14 +03006985 if (vcpu->arch.exception.nr == DB_VECTOR &&
6986 (vcpu->arch.dr7 & DR7_GD)) {
6987 vcpu->arch.dr7 &= ~DR7_GD;
6988 kvm_update_dr7(vcpu);
6989 }
6990
Wanpeng Licfcd20e2017-07-13 18:30:39 -07006991 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03006992 }
6993
6994 /* Don't consider new event if we re-injected an event */
6995 if (kvm_event_needs_reinjection(vcpu))
6996 return 0;
6997
6998 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
6999 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007000 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007001 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007002 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007003 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007004 --vcpu->arch.nmi_pending;
7005 vcpu->arch.nmi_injected = true;
7006 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007007 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007008 /*
7009 * Because interrupts can be injected asynchronously, we are
7010 * calling check_nested_events again here to avoid a race condition.
7011 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7012 * proposal and current concerns. Perhaps we should be setting
7013 * KVM_REQ_EVENT only on certain events and not unconditionally?
7014 */
7015 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7016 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7017 if (r != 0)
7018 return r;
7019 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007020 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007021 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7022 false);
7023 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007024 }
7025 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007026
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007027 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007028}
7029
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007030static void process_nmi(struct kvm_vcpu *vcpu)
7031{
7032 unsigned limit = 2;
7033
7034 /*
7035 * x86 is limited to one NMI running, and one NMI pending after it.
7036 * If an NMI is already in progress, limit further NMIs to just one.
7037 * Otherwise, allow two (and we'll inject the first one immediately).
7038 */
7039 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7040 limit = 1;
7041
7042 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7043 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7044 kvm_make_request(KVM_REQ_EVENT, vcpu);
7045}
7046
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007047static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007048{
7049 u32 flags = 0;
7050 flags |= seg->g << 23;
7051 flags |= seg->db << 22;
7052 flags |= seg->l << 21;
7053 flags |= seg->avl << 20;
7054 flags |= seg->present << 15;
7055 flags |= seg->dpl << 13;
7056 flags |= seg->s << 12;
7057 flags |= seg->type << 8;
7058 return flags;
7059}
7060
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007061static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007062{
7063 struct kvm_segment seg;
7064 int offset;
7065
7066 kvm_get_segment(vcpu, &seg, n);
7067 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7068
7069 if (n < 3)
7070 offset = 0x7f84 + n * 12;
7071 else
7072 offset = 0x7f2c + (n - 3) * 12;
7073
7074 put_smstate(u32, buf, offset + 8, seg.base);
7075 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007076 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007077}
7078
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007079#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007080static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007081{
7082 struct kvm_segment seg;
7083 int offset;
7084 u16 flags;
7085
7086 kvm_get_segment(vcpu, &seg, n);
7087 offset = 0x7e00 + n * 16;
7088
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007089 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007090 put_smstate(u16, buf, offset, seg.selector);
7091 put_smstate(u16, buf, offset + 2, flags);
7092 put_smstate(u32, buf, offset + 4, seg.limit);
7093 put_smstate(u64, buf, offset + 8, seg.base);
7094}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007095#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007096
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007097static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007098{
7099 struct desc_ptr dt;
7100 struct kvm_segment seg;
7101 unsigned long val;
7102 int i;
7103
7104 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7105 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7106 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7107 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7108
7109 for (i = 0; i < 8; i++)
7110 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7111
7112 kvm_get_dr(vcpu, 6, &val);
7113 put_smstate(u32, buf, 0x7fcc, (u32)val);
7114 kvm_get_dr(vcpu, 7, &val);
7115 put_smstate(u32, buf, 0x7fc8, (u32)val);
7116
7117 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7118 put_smstate(u32, buf, 0x7fc4, seg.selector);
7119 put_smstate(u32, buf, 0x7f64, seg.base);
7120 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007121 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007122
7123 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7124 put_smstate(u32, buf, 0x7fc0, seg.selector);
7125 put_smstate(u32, buf, 0x7f80, seg.base);
7126 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007127 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007128
7129 kvm_x86_ops->get_gdt(vcpu, &dt);
7130 put_smstate(u32, buf, 0x7f74, dt.address);
7131 put_smstate(u32, buf, 0x7f70, dt.size);
7132
7133 kvm_x86_ops->get_idt(vcpu, &dt);
7134 put_smstate(u32, buf, 0x7f58, dt.address);
7135 put_smstate(u32, buf, 0x7f54, dt.size);
7136
7137 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007138 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007139
7140 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7141
7142 /* revision id */
7143 put_smstate(u32, buf, 0x7efc, 0x00020000);
7144 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7145}
7146
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007147static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007148{
7149#ifdef CONFIG_X86_64
7150 struct desc_ptr dt;
7151 struct kvm_segment seg;
7152 unsigned long val;
7153 int i;
7154
7155 for (i = 0; i < 16; i++)
7156 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7157
7158 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7159 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7160
7161 kvm_get_dr(vcpu, 6, &val);
7162 put_smstate(u64, buf, 0x7f68, val);
7163 kvm_get_dr(vcpu, 7, &val);
7164 put_smstate(u64, buf, 0x7f60, val);
7165
7166 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7167 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7168 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7169
7170 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7171
7172 /* revision id */
7173 put_smstate(u32, buf, 0x7efc, 0x00020064);
7174
7175 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7176
7177 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7178 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007179 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007180 put_smstate(u32, buf, 0x7e94, seg.limit);
7181 put_smstate(u64, buf, 0x7e98, seg.base);
7182
7183 kvm_x86_ops->get_idt(vcpu, &dt);
7184 put_smstate(u32, buf, 0x7e84, dt.size);
7185 put_smstate(u64, buf, 0x7e88, dt.address);
7186
7187 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7188 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007189 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007190 put_smstate(u32, buf, 0x7e74, seg.limit);
7191 put_smstate(u64, buf, 0x7e78, seg.base);
7192
7193 kvm_x86_ops->get_gdt(vcpu, &dt);
7194 put_smstate(u32, buf, 0x7e64, dt.size);
7195 put_smstate(u64, buf, 0x7e68, dt.address);
7196
7197 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007198 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007199#else
7200 WARN_ON_ONCE(1);
7201#endif
7202}
7203
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007204static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007205{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007206 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007207 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007208 char buf[512];
7209 u32 cr0;
7210
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007211 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007212 memset(buf, 0, 512);
Radim Krčmářd6321d42017-08-05 00:12:49 +02007213 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007214 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007215 else
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007216 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007217
Ladi Prosek0234bf82017-10-11 16:54:40 +02007218 /*
7219 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7220 * vCPU state (e.g. leave guest mode) after we've saved the state into
7221 * the SMM state-save area.
7222 */
7223 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7224
7225 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007226 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007227
7228 if (kvm_x86_ops->get_nmi_mask(vcpu))
7229 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7230 else
7231 kvm_x86_ops->set_nmi_mask(vcpu, true);
7232
7233 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7234 kvm_rip_write(vcpu, 0x8000);
7235
7236 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7237 kvm_x86_ops->set_cr0(vcpu, cr0);
7238 vcpu->arch.cr0 = cr0;
7239
7240 kvm_x86_ops->set_cr4(vcpu, 0);
7241
Paolo Bonzini18c36262015-08-07 12:27:54 +02007242 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7243 dt.address = dt.size = 0;
7244 kvm_x86_ops->set_idt(vcpu, &dt);
7245
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007246 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7247
7248 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7249 cs.base = vcpu->arch.smbase;
7250
7251 ds.selector = 0;
7252 ds.base = 0;
7253
7254 cs.limit = ds.limit = 0xffffffff;
7255 cs.type = ds.type = 0x3;
7256 cs.dpl = ds.dpl = 0;
7257 cs.db = ds.db = 0;
7258 cs.s = ds.s = 1;
7259 cs.l = ds.l = 0;
7260 cs.g = ds.g = 1;
7261 cs.avl = ds.avl = 0;
7262 cs.present = ds.present = 1;
7263 cs.unusable = ds.unusable = 0;
7264 cs.padding = ds.padding = 0;
7265
7266 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7267 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7268 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7269 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7270 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7271 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7272
Radim Krčmářd6321d42017-08-05 00:12:49 +02007273 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007274 kvm_x86_ops->set_efer(vcpu, 0);
7275
7276 kvm_update_cpuid(vcpu);
7277 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007278}
7279
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007280static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007281{
7282 vcpu->arch.smi_pending = true;
7283 kvm_make_request(KVM_REQ_EVENT, vcpu);
7284}
7285
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01007286void kvm_make_scan_ioapic_request(struct kvm *kvm)
7287{
7288 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
7289}
7290
Yang Zhang3d81bc72013-04-11 19:25:13 +08007291static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08007292{
Yang Zhang3d81bc72013-04-11 19:25:13 +08007293 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
7294 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08007295
Andrey Smetanin63086302015-11-10 15:36:32 +03007296 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007297
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07007298 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03007299 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007300 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02007301 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03007302 kvm_x86_ops->sync_pir_to_irr(vcpu);
Andrey Smetanin63086302015-11-10 15:36:32 +03007303 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007304 }
Liran Alone40ff1d2018-03-21 02:50:31 +02007305
7306 if (is_guest_mode(vcpu))
7307 vcpu->arch.load_eoi_exitmap_pending = true;
7308 else
7309 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
7310}
7311
7312static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
7313{
7314 u64 eoi_exit_bitmap[4];
7315
7316 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
7317 return;
7318
Andrey Smetanin5c9194122015-11-10 15:36:34 +03007319 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
7320 vcpu_to_synic(vcpu)->vec_bitmap, 256);
7321 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007322}
7323
Michal Hocko93065ac2018-08-21 21:52:33 -07007324int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
7325 unsigned long start, unsigned long end,
7326 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01007327{
7328 unsigned long apic_address;
7329
7330 /*
7331 * The physical address of apic access page is stored in the VMCS.
7332 * Update it when it becomes invalid.
7333 */
7334 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
7335 if (start <= apic_address && apic_address < end)
7336 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07007337
7338 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01007339}
7340
Tang Chen4256f432014-09-24 15:57:54 +08007341void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
7342{
Tang Chenc24ae0d2014-09-24 15:57:58 +08007343 struct page *page = NULL;
7344
Paolo Bonzini35754c92015-07-29 12:05:37 +02007345 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02007346 return;
7347
Tang Chen4256f432014-09-24 15:57:54 +08007348 if (!kvm_x86_ops->set_apic_access_page_addr)
7349 return;
7350
Tang Chenc24ae0d2014-09-24 15:57:58 +08007351 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02007352 if (is_error_page(page))
7353 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08007354 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
7355
7356 /*
7357 * Do not pin apic access page in memory, the MMU notifier
7358 * will call us again if it is migrated or swapped out.
7359 */
7360 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08007361}
7362EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
7363
Sean Christophersond264ee02018-08-27 15:21:12 -07007364void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
7365{
7366 smp_send_reschedule(vcpu->cpu);
7367}
7368EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
7369
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007370/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01007371 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007372 * exiting to the userspace. Otherwise, the value will be returned to the
7373 * userspace.
7374 */
Avi Kivity851ba692009-08-24 11:10:17 +03007375static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007376{
7377 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08007378 bool req_int_win =
7379 dm_request_for_irq_injection(vcpu) &&
7380 kvm_cpu_accept_dm_intr(vcpu);
7381
Jan Kiszka730dca42013-04-28 10:50:52 +02007382 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007383
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02007384 if (kvm_request_pending(vcpu)) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +02007385 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu))
7386 kvm_x86_ops->get_vmcs12_pages(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007387 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05007388 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007389 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03007390 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007391 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
7392 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03007393 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
7394 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10007395 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
7396 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007397 if (unlikely(r))
7398 goto out;
7399 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03007400 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03007401 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07007402 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
7403 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007404 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08007405 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007406 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007407 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02007408 r = 0;
7409 goto out;
7410 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03007411 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007412 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007413 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01007414 r = 0;
7415 goto out;
7416 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02007417 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
7418 /* Page is swapped out. Do synthetic halt */
7419 vcpu->arch.apf.halted = true;
7420 r = 1;
7421 goto out;
7422 }
Glauber Costac9aaa892011-07-11 15:28:14 -04007423 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
7424 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007425 if (kvm_check_request(KVM_REQ_SMI, vcpu))
7426 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007427 if (kvm_check_request(KVM_REQ_NMI, vcpu))
7428 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007429 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02007430 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007431 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02007432 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07007433 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
7434 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
7435 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03007436 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07007437 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
7438 vcpu->run->eoi.vector =
7439 vcpu->arch.pending_ioapic_eoi;
7440 r = 0;
7441 goto out;
7442 }
7443 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08007444 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
7445 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02007446 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
7447 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08007448 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
7449 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03007450 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
7451 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7452 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
7453 r = 0;
7454 goto out;
7455 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03007456 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
7457 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7458 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
7459 r = 0;
7460 goto out;
7461 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03007462 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
7463 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
7464 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
7465 r = 0;
7466 goto out;
7467 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03007468
7469 /*
7470 * KVM_REQ_HV_STIMER has to be processed after
7471 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
7472 * depend on the guest clock being up-to-date
7473 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03007474 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
7475 kvm_hv_process_stimers(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02007476 }
Avi Kivityb93463a2007-10-25 16:52:32 +02007477
Avi Kivityb463a6f2010-07-20 15:06:17 +03007478 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01007479 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01007480 kvm_apic_accept_events(vcpu);
7481 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
7482 r = 1;
7483 goto out;
7484 }
7485
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007486 if (inject_pending_event(vcpu, req_int_win) != 0)
7487 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00007488 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007489 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007490 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007491 * SMIs have three cases:
7492 * 1) They can be nested, and then there is nothing to
7493 * do here because RSM will cause a vmexit anyway.
7494 * 2) There is an ISA-specific reason why SMI cannot be
7495 * injected, and the moment when this changes can be
7496 * intercepted.
7497 * 3) Or the SMI can be pending because
7498 * inject_pending_event has completed the injection
7499 * of an IRQ or NMI from the previous vmexit, and
7500 * then we request an immediate exit to inject the
7501 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007502 */
7503 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007504 if (!kvm_x86_ops->enable_smi_window(vcpu))
7505 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00007506 if (vcpu->arch.nmi_pending)
7507 kvm_x86_ops->enable_nmi_window(vcpu);
7508 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
7509 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007510 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00007511 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03007512
Avi Kivity3842d132010-07-27 12:30:24 +03007513 if (kvm_lapic_enabled(vcpu)) {
7514 update_cr8_intercept(vcpu);
7515 kvm_lapic_sync_to_vapic(vcpu);
7516 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007517 }
Avi Kivityb93463a2007-10-25 16:52:32 +02007518
Avi Kivityd8368af2012-05-14 18:07:56 +03007519 r = kvm_mmu_reload(vcpu);
7520 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03007521 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03007522 }
7523
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007524 preempt_disable();
7525
7526 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007527
7528 /*
7529 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
7530 * IPI are then delayed after guest entry, which ensures that they
7531 * result in virtual interrupt delivery.
7532 */
7533 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007534 vcpu->mode = IN_GUEST_MODE;
7535
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02007536 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
7537
Lan Tianyu0f127d12016-03-13 11:10:29 +08007538 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007539 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02007540 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007541 *
7542 * 2) For APICv, we should set ->mode before checking PIR.ON. This
7543 * pairs with the memory barrier implicit in pi_test_and_set_on
7544 * (see vmx_deliver_posted_interrupt).
7545 *
7546 * 3) This also orders the write to mode from any reads to the page
7547 * tables done while the VCPU is running. Please see the comment
7548 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007549 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02007550 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007551
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007552 /*
7553 * This handles the case where a posted interrupt was
7554 * notified with kvm_vcpu_kick.
7555 */
Liran Alonfa59cc02017-12-24 18:12:53 +02007556 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
7557 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007558
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02007559 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007560 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007561 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007562 smp_wmb();
7563 local_irq_enable();
7564 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02007565 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007566 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03007567 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007568 }
7569
David Matlackfc5b7f32016-03-30 12:24:47 -07007570 kvm_load_guest_xcr0(vcpu);
7571
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007572 if (req_immediate_exit) {
7573 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07007574 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007575 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03007576
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01007577 trace_kvm_entry(vcpu->vcpu_id);
Wanpeng Li9c48d512017-12-01 00:15:10 -08007578 if (lapic_timer_advance_ns)
7579 wait_lapic_expire(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02007580 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007581
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007582 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007583 set_debugreg(0, 7);
7584 set_debugreg(vcpu->arch.eff_db[0], 0);
7585 set_debugreg(vcpu->arch.eff_db[1], 1);
7586 set_debugreg(vcpu->arch.eff_db[2], 2);
7587 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01007588 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03007589 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007590 }
7591
Avi Kivity851ba692009-08-24 11:10:17 +03007592 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007593
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007594 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01007595 * Do this here before restoring debug registers on the host. And
7596 * since we do this before handling the vmexit, a DR access vmexit
7597 * can (a) read the correct value of the debug registers, (b) set
7598 * KVM_DEBUGREG_WONT_EXIT again.
7599 */
7600 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01007601 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
7602 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01007603 kvm_update_dr0123(vcpu);
7604 kvm_update_dr6(vcpu);
7605 kvm_update_dr7(vcpu);
7606 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01007607 }
7608
7609 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007610 * If the guest has used debug registers, at least dr7
7611 * will be disabled while returning to the host.
7612 * If we don't have active breakpoints in the host, we don't
7613 * care about the messed up debug address registers. But if
7614 * we have some of them active, restore the old state.
7615 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01007616 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007617 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007618
Haozhong Zhang4ba76532015-10-20 15:39:07 +08007619 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10007620
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007621 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03007622 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08007623
David Matlackfc5b7f32016-03-30 12:24:47 -07007624 kvm_put_guest_xcr0(vcpu);
7625
Andi Kleendd60d212017-07-25 17:20:32 -07007626 kvm_before_interrupt(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08007627 kvm_x86_ops->handle_external_intr(vcpu);
Andi Kleendd60d212017-07-25 17:20:32 -07007628 kvm_after_interrupt(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007629
7630 ++vcpu->stat.exits;
7631
Paolo Bonzinif2485b32016-06-15 15:23:11 +02007632 guest_exit_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007633
Paolo Bonzinif2485b32016-06-15 15:23:11 +02007634 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007635 preempt_enable();
7636
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007637 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03007638
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007639 /*
7640 * Profile KVM exit RIPs:
7641 */
7642 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007643 unsigned long rip = kvm_rip_read(vcpu);
7644 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007645 }
7646
Zachary Amsdencc578282012-02-03 15:43:50 -02007647 if (unlikely(vcpu->arch.tsc_always_catchup))
7648 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02007649
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03007650 if (vcpu->arch.apic_attention)
7651 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02007652
Brijesh Singh618232e2017-08-17 18:36:57 +02007653 vcpu->arch.gpa_available = false;
Avi Kivity851ba692009-08-24 11:10:17 +03007654 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03007655 return r;
7656
7657cancel_injection:
7658 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03007659 if (unlikely(vcpu->arch.apic_attention))
7660 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007661out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03007662 return r;
7663}
7664
Paolo Bonzini362c6982015-02-06 12:48:04 +01007665static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
7666{
Feng Wubf9f6ac2015-09-18 22:29:55 +08007667 if (!kvm_arch_vcpu_runnable(vcpu) &&
7668 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01007669 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
7670 kvm_vcpu_block(vcpu);
7671 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007672
7673 if (kvm_x86_ops->post_block)
7674 kvm_x86_ops->post_block(vcpu);
7675
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01007676 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
7677 return 1;
7678 }
Gleb Natapov09cec752009-03-23 15:11:44 +02007679
Paolo Bonzini362c6982015-02-06 12:48:04 +01007680 kvm_apic_accept_events(vcpu);
7681 switch(vcpu->arch.mp_state) {
7682 case KVM_MP_STATE_HALTED:
7683 vcpu->arch.pv.pv_unhalted = false;
7684 vcpu->arch.mp_state =
7685 KVM_MP_STATE_RUNNABLE;
7686 case KVM_MP_STATE_RUNNABLE:
7687 vcpu->arch.apf.halted = false;
7688 break;
7689 case KVM_MP_STATE_INIT_RECEIVED:
7690 break;
7691 default:
7692 return -EINTR;
7693 break;
7694 }
7695 return 1;
7696}
7697
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02007698static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
7699{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01007700 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
7701 kvm_x86_ops->check_nested_events(vcpu, false);
7702
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02007703 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
7704 !vcpu->arch.apf.halted);
7705}
7706
Paolo Bonzini362c6982015-02-06 12:48:04 +01007707static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03007708{
7709 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007710 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03007711
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007712 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02007713 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03007714
Paolo Bonzini362c6982015-02-06 12:48:04 +01007715 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02007716 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007717 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007718 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01007719 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007720 }
7721
Gleb Natapov09cec752009-03-23 15:11:44 +02007722 if (r <= 0)
7723 break;
7724
Radim Krčmář72875d82017-04-26 22:32:19 +02007725 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02007726 if (kvm_cpu_has_pending_timer(vcpu))
7727 kvm_inject_pending_timer_irqs(vcpu);
7728
Matt Gingell782d4222015-11-16 15:26:00 -08007729 if (dm_request_for_irq_injection(vcpu) &&
7730 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02007731 r = 0;
7732 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02007733 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01007734 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02007735 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02007736
7737 kvm_check_async_pf_completion(vcpu);
7738
Gleb Natapov09cec752009-03-23 15:11:44 +02007739 if (signal_pending(current)) {
7740 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03007741 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02007742 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01007743 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02007744 }
7745 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007746 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09007747 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007748 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03007749 }
7750 }
7751
Marcelo Tosattif656ce02009-12-23 14:35:25 -02007752 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007753
7754 return r;
7755}
7756
Gleb Natapov716d51a2012-09-03 15:24:26 +03007757static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
7758{
7759 int r;
7760 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007761 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03007762 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
7763 if (r != EMULATE_DONE)
7764 return 0;
7765 return 1;
7766}
7767
7768static int complete_emulated_pio(struct kvm_vcpu *vcpu)
7769{
7770 BUG_ON(!vcpu->arch.pio.count);
7771
7772 return complete_emulated_io(vcpu);
7773}
7774
Avi Kivityf78146b2012-04-18 19:22:47 +03007775/*
7776 * Implements the following, as a state machine:
7777 *
7778 * read:
7779 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007780 * for each mmio piece in the fragment
7781 * write gpa, len
7782 * exit
7783 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03007784 * execute insn
7785 *
7786 * write:
7787 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007788 * for each mmio piece in the fragment
7789 * write gpa, len
7790 * copy data
7791 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03007792 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03007793static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02007794{
7795 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03007796 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007797 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02007798
Gleb Natapov716d51a2012-09-03 15:24:26 +03007799 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02007800
Gleb Natapov716d51a2012-09-03 15:24:26 +03007801 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007802 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
7803 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03007804 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007805 memcpy(frag->data, run->mmio.data, len);
7806
7807 if (frag->len <= 8) {
7808 /* Switch to the next fragment. */
7809 frag++;
7810 vcpu->mmio_cur_fragment++;
7811 } else {
7812 /* Go forward to the next mmio piece. */
7813 frag->data += len;
7814 frag->gpa += len;
7815 frag->len -= len;
7816 }
7817
Andrew Honiga08d3b32014-02-27 19:35:14 +01007818 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03007819 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02007820
7821 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02007822 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03007823 return 1;
7824 vcpu->mmio_read_completed = 1;
7825 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02007826 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007827
Gleb Natapov716d51a2012-09-03 15:24:26 +03007828 run->exit_reason = KVM_EXIT_MMIO;
7829 run->mmio.phys_addr = frag->gpa;
7830 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08007831 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
7832 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03007833 run->mmio.is_write = vcpu->mmio_is_write;
7834 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
7835 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02007836}
7837
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007838int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
7839{
7840 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007841
Christoffer Dallaccb7572017-12-04 21:35:25 +01007842 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01007843 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01007844 kvm_load_guest_fpu(vcpu);
7845
Avi Kivityac9f6dc2008-07-06 15:48:31 +03007846 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02007847 if (kvm_run->immediate_exit) {
7848 r = -EINTR;
7849 goto out;
7850 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03007851 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01007852 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02007853 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03007854 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02007855 if (signal_pending(current)) {
7856 r = -EINTR;
7857 vcpu->run->exit_reason = KVM_EXIT_INTR;
7858 ++vcpu->stat.signal_exits;
7859 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03007860 goto out;
7861 }
7862
Ken Hofsass01643c52018-01-31 16:03:36 -08007863 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
7864 r = -EINVAL;
7865 goto out;
7866 }
7867
7868 if (vcpu->run->kvm_dirty_regs) {
7869 r = sync_regs(vcpu);
7870 if (r != 0)
7871 goto out;
7872 }
7873
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007874 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02007875 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007876 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
7877 r = -EINVAL;
7878 goto out;
7879 }
7880 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007881
Gleb Natapov716d51a2012-09-03 15:24:26 +03007882 if (unlikely(vcpu->arch.complete_userspace_io)) {
7883 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
7884 vcpu->arch.complete_userspace_io = NULL;
7885 r = cui(vcpu);
7886 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01007887 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03007888 } else
7889 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02007890
Paolo Bonzini460df4c2017-02-08 11:50:15 +01007891 if (kvm_run->immediate_exit)
7892 r = -EINTR;
7893 else
7894 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007895
7896out:
Peter Xu5663d8f2017-12-12 17:15:02 +01007897 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08007898 if (vcpu->run->kvm_valid_regs)
7899 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03007900 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01007901 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007902
Christoffer Dallaccb7572017-12-04 21:35:25 +01007903 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007904 return r;
7905}
7906
Ken Hofsass01643c52018-01-31 16:03:36 -08007907static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007908{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007909 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
7910 /*
7911 * We are here if userspace calls get_regs() in the middle of
7912 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08007913 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007914 * that usually, but some bad designed PV devices (vmware
7915 * backdoor interface) need this to work
7916 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03007917 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007918 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
7919 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007920 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
7921 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
7922 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
7923 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
7924 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
7925 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
7926 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
7927 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007928#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007929 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
7930 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
7931 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
7932 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
7933 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
7934 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
7935 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
7936 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007937#endif
7938
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007939 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02007940 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08007941}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007942
Ken Hofsass01643c52018-01-31 16:03:36 -08007943int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
7944{
7945 vcpu_load(vcpu);
7946 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01007947 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007948 return 0;
7949}
7950
Ken Hofsass01643c52018-01-31 16:03:36 -08007951static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007952{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02007953 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
7954 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
7955
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007956 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
7957 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
7958 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
7959 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
7960 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
7961 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
7962 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
7963 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007964#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007965 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
7966 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
7967 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
7968 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
7969 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
7970 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
7971 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
7972 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007973#endif
7974
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007975 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08007976 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007977
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02007978 vcpu->arch.exception.pending = false;
7979
Avi Kivity3842d132010-07-27 12:30:24 +03007980 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08007981}
Avi Kivity3842d132010-07-27 12:30:24 +03007982
Ken Hofsass01643c52018-01-31 16:03:36 -08007983int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
7984{
7985 vcpu_load(vcpu);
7986 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01007987 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007988 return 0;
7989}
7990
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007991void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
7992{
7993 struct kvm_segment cs;
7994
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02007995 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007996 *db = cs.db;
7997 *l = cs.l;
7998}
7999EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8000
Ken Hofsass01643c52018-01-31 16:03:36 -08008001static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008002{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008003 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008004
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008005 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8006 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8007 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8008 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8009 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8010 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008011
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008012 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8013 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008014
8015 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008016 sregs->idt.limit = dt.size;
8017 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008018 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008019 sregs->gdt.limit = dt.size;
8020 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008021
Avi Kivity4d4ec082009-12-29 18:07:30 +02008022 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008023 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008024 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008025 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008026 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008027 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008028 sregs->apic_base = kvm_get_apic_base(vcpu);
8029
Gleb Natapov923c61b2009-05-11 13:35:48 +03008030 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008031
Liran Alon04140b42018-03-23 03:01:31 +03008032 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008033 set_bit(vcpu->arch.interrupt.nr,
8034 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008035}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008036
Ken Hofsass01643c52018-01-31 16:03:36 -08008037int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8038 struct kvm_sregs *sregs)
8039{
8040 vcpu_load(vcpu);
8041 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008042 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008043 return 0;
8044}
8045
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008046int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8047 struct kvm_mp_state *mp_state)
8048{
Christoffer Dallfd232562017-12-04 21:35:30 +01008049 vcpu_load(vcpu);
8050
Jan Kiszka66450a22013-03-13 12:42:34 +01008051 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308052 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8053 vcpu->arch.pv.pv_unhalted)
8054 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8055 else
8056 mp_state->mp_state = vcpu->arch.mp_state;
8057
Christoffer Dallfd232562017-12-04 21:35:30 +01008058 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008059 return 0;
8060}
8061
8062int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8063 struct kvm_mp_state *mp_state)
8064{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008065 int ret = -EINVAL;
8066
8067 vcpu_load(vcpu);
8068
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008069 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008070 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008071 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008072
David Hildenbrand28bf2882017-03-23 11:46:03 +01008073 /* INITs are latched while in SMM */
8074 if ((is_smm(vcpu) || vcpu->arch.smi_pending) &&
8075 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8076 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008077 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008078
Jan Kiszka66450a22013-03-13 12:42:34 +01008079 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8080 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8081 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8082 } else
8083 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008084 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008085
8086 ret = 0;
8087out:
8088 vcpu_put(vcpu);
8089 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008090}
8091
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008092int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8093 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008094{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008095 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008096 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008097
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008098 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008099
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008100 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008101 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02008102
Gleb Natapovc6975182010-02-18 12:15:01 +02008103 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03008104 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02008105
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008106 kvm_rip_write(vcpu, ctxt->eip);
8107 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008108 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03008109 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02008110}
8111EXPORT_SYMBOL_GPL(kvm_task_switch);
8112
Peng Hao3140c152018-04-02 09:15:32 +08008113static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008114{
Tianyu Lan74fec5b2018-07-23 12:31:21 +00008115 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
8116 (sregs->cr4 & X86_CR4_OSXSAVE))
8117 return -EINVAL;
8118
Tianyu Lan37b95952018-01-16 17:34:07 +08008119 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008120 /*
8121 * When EFER.LME and CR0.PG are set, the processor is in
8122 * 64-bit mode (though maybe in a 32-bit code segment).
8123 * CR4.PAE and EFER.LMA must be set.
8124 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008125 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008126 || !(sregs->efer & EFER_LMA))
8127 return -EINVAL;
8128 } else {
8129 /*
8130 * Not in 64-bit mode: EFER.LMA is clear and the code
8131 * segment cannot be 64-bit.
8132 */
8133 if (sregs->efer & EFER_LMA || sregs->cs.l)
8134 return -EINVAL;
8135 }
8136
8137 return 0;
8138}
8139
Ken Hofsass01643c52018-01-31 16:03:36 -08008140static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008141{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008142 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008143 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008144 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008145 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008146 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008147 int ret = -EINVAL;
8148
Lan Tianyuf2981032017-12-14 03:01:52 -05008149 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08008150 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05008151
Jim Mattsond3802282017-08-10 10:14:13 -07008152 apic_base_msr.data = sregs->apic_base;
8153 apic_base_msr.host_initiated = true;
8154 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008155 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01008156
Gleb Natapov89a27f42010-02-16 10:51:48 +02008157 dt.size = sregs->idt.limit;
8158 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008159 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008160 dt.size = sregs->gdt.limit;
8161 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008162 kvm_x86_ops->set_gdt(vcpu, &dt);
8163
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008164 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008165 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02008166 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02008167 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008168
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008169 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008170
Avi Kivityf6801df2010-01-21 15:31:50 +02008171 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008172 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008173
Avi Kivity4d4ec082009-12-29 18:07:30 +02008174 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008175 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00008176 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008177
Avi Kivityfc78f512009-12-07 12:16:48 +02008178 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05008179 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
8180 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008181 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05008182 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02008183 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008184
8185 idx = srcu_read_lock(&vcpu->kvm->srcu);
Junaid Shahidd35b34a2018-08-08 17:45:24 -07008186 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02008187 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02008188 mmu_reset_needed = 1;
8189 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008190 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008191
8192 if (mmu_reset_needed)
8193 kvm_mmu_reset_context(vcpu);
8194
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03008195 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03008196 pending_vec = find_first_bit(
8197 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
8198 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008199 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03008200 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008201 }
8202
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008203 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8204 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8205 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8206 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8207 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8208 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008209
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008210 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8211 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008212
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03008213 update_cr8_intercept(vcpu);
8214
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008215 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03008216 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008217 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02008218 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008219 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
8220
Avi Kivity3842d132010-07-27 12:30:24 +03008221 kvm_make_request(KVM_REQ_EVENT, vcpu);
8222
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008223 ret = 0;
8224out:
Ken Hofsass01643c52018-01-31 16:03:36 -08008225 return ret;
8226}
8227
8228int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
8229 struct kvm_sregs *sregs)
8230{
8231 int ret;
8232
8233 vcpu_load(vcpu);
8234 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008235 vcpu_put(vcpu);
8236 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008237}
8238
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008239int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
8240 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008241{
Jan Kiszka355be0b2009-10-03 00:31:21 +02008242 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008243 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008244
Christoffer Dall66b56562017-12-04 21:35:33 +01008245 vcpu_load(vcpu);
8246
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008247 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
8248 r = -EBUSY;
8249 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03008250 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008251 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
8252 kvm_queue_exception(vcpu, DB_VECTOR);
8253 else
8254 kvm_queue_exception(vcpu, BP_VECTOR);
8255 }
8256
Jan Kiszka91586a32009-10-05 13:07:21 +02008257 /*
8258 * Read rflags as long as potentially injected trace flags are still
8259 * filtered out.
8260 */
8261 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008262
8263 vcpu->guest_debug = dbg->control;
8264 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
8265 vcpu->guest_debug = 0;
8266
8267 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008268 for (i = 0; i < KVM_NR_DB_REGS; ++i)
8269 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02008270 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008271 } else {
8272 for (i = 0; i < KVM_NR_DB_REGS; i++)
8273 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008274 }
Jan Kiszkac8639012012-09-21 05:42:55 +02008275 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008276
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008277 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8278 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
8279 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008280
Jan Kiszka91586a32009-10-05 13:07:21 +02008281 /*
8282 * Trigger an rflags update that will inject or remove the trace
8283 * flags.
8284 */
8285 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008286
Paolo Bonzinia96036b2015-11-10 11:55:36 +01008287 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008288
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008289 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008290
Avi Kivity2122ff52010-05-13 11:25:04 +03008291out:
Christoffer Dall66b56562017-12-04 21:35:33 +01008292 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008293 return r;
8294}
8295
8296/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08008297 * Translate a guest virtual address to a guest physical address.
8298 */
8299int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
8300 struct kvm_translation *tr)
8301{
8302 unsigned long vaddr = tr->linear_address;
8303 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008304 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008305
Christoffer Dall1da5b612017-12-04 21:35:32 +01008306 vcpu_load(vcpu);
8307
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008308 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02008309 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008310 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008311 tr->physical_address = gpa;
8312 tr->valid = gpa != UNMAPPED_GVA;
8313 tr->writeable = 1;
8314 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008315
Christoffer Dall1da5b612017-12-04 21:35:32 +01008316 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008317 return 0;
8318}
8319
Hollis Blanchardd0752062007-10-31 17:24:25 -05008320int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8321{
Christoffer Dall13931232017-12-04 21:35:34 +01008322 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008323
Christoffer Dall13931232017-12-04 21:35:34 +01008324 vcpu_load(vcpu);
8325
8326 fxsave = &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008327 memcpy(fpu->fpr, fxsave->st_space, 128);
8328 fpu->fcw = fxsave->cwd;
8329 fpu->fsw = fxsave->swd;
8330 fpu->ftwx = fxsave->twd;
8331 fpu->last_opcode = fxsave->fop;
8332 fpu->last_ip = fxsave->rip;
8333 fpu->last_dp = fxsave->rdp;
8334 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
8335
Christoffer Dall13931232017-12-04 21:35:34 +01008336 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008337 return 0;
8338}
8339
8340int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8341{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01008342 struct fxregs_state *fxsave;
8343
8344 vcpu_load(vcpu);
8345
8346 fxsave = &vcpu->arch.guest_fpu.state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008347
Hollis Blanchardd0752062007-10-31 17:24:25 -05008348 memcpy(fxsave->st_space, fpu->fpr, 128);
8349 fxsave->cwd = fpu->fcw;
8350 fxsave->swd = fpu->fsw;
8351 fxsave->twd = fpu->ftwx;
8352 fxsave->fop = fpu->last_opcode;
8353 fxsave->rip = fpu->last_ip;
8354 fxsave->rdp = fpu->last_dp;
8355 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
8356
Christoffer Dall6a96bc72017-12-04 21:35:35 +01008357 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008358 return 0;
8359}
8360
Ken Hofsass01643c52018-01-31 16:03:36 -08008361static void store_regs(struct kvm_vcpu *vcpu)
8362{
8363 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
8364
8365 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
8366 __get_regs(vcpu, &vcpu->run->s.regs.regs);
8367
8368 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
8369 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
8370
8371 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
8372 kvm_vcpu_ioctl_x86_get_vcpu_events(
8373 vcpu, &vcpu->run->s.regs.events);
8374}
8375
8376static int sync_regs(struct kvm_vcpu *vcpu)
8377{
8378 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
8379 return -EINVAL;
8380
8381 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
8382 __set_regs(vcpu, &vcpu->run->s.regs.regs);
8383 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
8384 }
8385 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
8386 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
8387 return -EINVAL;
8388 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
8389 }
8390 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
8391 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
8392 vcpu, &vcpu->run->s.regs.events))
8393 return -EINVAL;
8394 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
8395 }
8396
8397 return 0;
8398}
8399
Ingo Molnar0ee6a512015-04-27 06:58:22 +02008400static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05008401{
Ingo Molnarbf935b02015-04-30 10:23:42 +02008402 fpstate_init(&vcpu->arch.guest_fpu.state);
Borislav Petkov782511b2016-04-04 22:25:03 +02008403 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar7366ed72015-04-27 04:19:39 +02008404 vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01008405 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008406
Dexuan Cui2acf9232010-06-10 11:27:12 +08008407 /*
8408 * Ensure guest xcr0 is valid for loading
8409 */
Dave Hansend91cab72015-09-02 16:31:26 -07008410 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08008411
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008412 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008413}
Hollis Blanchardd0752062007-10-31 17:24:25 -05008414
Rik van Rielf775b132017-11-14 16:54:23 -05008415/* Swap (qemu) user FPU context for the guest FPU context. */
Hollis Blanchardd0752062007-10-31 17:24:25 -05008416void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8417{
Rik van Rielf775b132017-11-14 16:54:23 -05008418 preempt_disable();
8419 copy_fpregs_to_fpstate(&vcpu->arch.user_fpu);
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02008420 /* PKRU is separately restored in kvm_x86_ops->run. */
8421 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state,
8422 ~XFEATURE_MASK_PKRU);
Rik van Rielf775b132017-11-14 16:54:23 -05008423 preempt_enable();
Avi Kivity0c048512010-01-21 15:31:52 +02008424 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008425}
Hollis Blanchardd0752062007-10-31 17:24:25 -05008426
Rik van Rielf775b132017-11-14 16:54:23 -05008427/* When vcpu_run ends, restore user space FPU context. */
Hollis Blanchardd0752062007-10-31 17:24:25 -05008428void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8429{
Rik van Rielf775b132017-11-14 16:54:23 -05008430 preempt_disable();
Ingo Molnar4f836342015-04-27 02:53:16 +02008431 copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
Rik van Rielf775b132017-11-14 16:54:23 -05008432 copy_kernel_to_fpregs(&vcpu->arch.user_fpu.state);
8433 preempt_enable();
Avi Kivityf096ed82007-11-18 13:54:33 +02008434 ++vcpu->stat.fpu_reload;
Avi Kivity0c048512010-01-21 15:31:52 +02008435 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008436}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008437
8438void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
8439{
Ido Yarivbd768e12016-10-21 12:39:57 -04008440 void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask;
8441
Glauber Costa12f9a482011-02-01 14:16:40 -05008442 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01008443
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008444 kvm_x86_ops->vcpu_free(vcpu);
Ido Yarivbd768e12016-10-21 12:39:57 -04008445 free_cpumask_var(wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008446}
8447
8448struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
8449 unsigned int id)
8450{
Liang Lic447e762015-05-21 04:41:25 +08008451 struct kvm_vcpu *vcpu;
8452
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008453 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Zachary Amsden6755bae2010-08-19 22:07:22 -10008454 printk_once(KERN_WARNING
8455 "kvm: SMP vm created on host with unstable TSC; "
8456 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08008457
8458 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
8459
Liang Lic447e762015-05-21 04:41:25 +08008460 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02008461}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008462
Avi Kivity26e52152007-11-20 15:30:24 +02008463int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
8464{
Xiao Guangrong19efffa2015-06-15 16:55:31 +08008465 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01008466 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03008467 kvm_vcpu_reset(vcpu, false);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02008468 kvm_mmu_setup(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008469 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01008470 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008471}
8472
Dominik Dingel31928aa2014-12-04 15:47:07 +01008473void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008474{
Will Auld8fe8ab42012-11-29 12:42:12 -08008475 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01008476 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008477
Roman Kagand3457c82017-07-14 17:13:20 +03008478 kvm_hv_vcpu_postcreate(vcpu);
8479
Christoffer Dallec7660c2017-12-04 21:35:23 +01008480 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01008481 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01008482 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08008483 msr.data = 0x0;
8484 msr.index = MSR_IA32_TSC;
8485 msr.host_initiated = true;
8486 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008487 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01008488 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008489
Marcelo Tosatti630994b2015-05-12 22:42:04 -03008490 if (!kvmclock_periodic_sync)
8491 return;
8492
Andrew Jones332967a2014-02-28 12:52:55 +01008493 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
8494 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008495}
8496
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06008497void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008498{
Gleb Natapov344d9582010-10-14 11:22:50 +02008499 vcpu->arch.apf.msr_val = 0;
8500
Christoffer Dallec7660c2017-12-04 21:35:23 +01008501 vcpu_load(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008502 kvm_mmu_unload(vcpu);
8503 vcpu_put(vcpu);
8504
8505 kvm_x86_ops->vcpu_free(vcpu);
8506}
8507
Nadav Amitd28bc9d2015-04-13 14:34:08 +03008508void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008509{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01008510 kvm_lapic_reset(vcpu, init_event);
8511
Paolo Bonzinie69fab52015-06-04 10:44:44 +02008512 vcpu->arch.hflags = 0;
8513
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008514 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02008515 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008516 atomic_set(&vcpu->arch.nmi_queued, 0);
8517 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02008518 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03008519 kvm_clear_interrupt_queue(vcpu);
8520 kvm_clear_exception_queue(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008521 vcpu->arch.exception.pending = false;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02008522
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008523 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03008524 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03008525 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01008526 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008527 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02008528 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008529
Nadav Amit11190222015-04-02 03:10:38 +03008530 vcpu->arch.cr2 = 0;
8531
Avi Kivity3842d132010-07-27 12:30:24 +03008532 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02008533 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04008534 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03008535
Glauber Costa12f9a482011-02-01 14:16:40 -05008536 kvmclock_reset(vcpu);
8537
Gleb Natapovaf585b92010-10-14 11:22:46 +02008538 kvm_clear_async_pf_completion_queue(vcpu);
8539 kvm_async_pf_hash_reset(vcpu);
8540 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008541
Wanpeng Lia554d202017-10-11 05:10:19 -07008542 if (kvm_mpx_supported()) {
8543 void *mpx_state_buffer;
8544
8545 /*
8546 * To avoid have the INIT path from kvm_apic_has_events() that be
8547 * called with loaded FPU and does not let userspace fix the state.
8548 */
Rik van Rielf775b132017-11-14 16:54:23 -05008549 if (init_event)
8550 kvm_put_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07008551 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu.state.xsave,
8552 XFEATURE_MASK_BNDREGS);
8553 if (mpx_state_buffer)
8554 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
8555 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu.state.xsave,
8556 XFEATURE_MASK_BNDCSR);
8557 if (mpx_state_buffer)
8558 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05008559 if (init_event)
8560 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07008561 }
8562
Paolo Bonzini64d60672015-05-07 11:36:11 +02008563 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03008564 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008565 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07008566
8567 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
8568 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07008569
8570 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02008571 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02008572
Julian Stecklina66f7b722012-12-05 15:26:19 +01008573 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
8574 vcpu->arch.regs_avail = ~0;
8575 vcpu->arch.regs_dirty = ~0;
8576
Wanpeng Lia554d202017-10-11 05:10:19 -07008577 vcpu->arch.ia32_xss = 0;
8578
Nadav Amitd28bc9d2015-04-13 14:34:08 +03008579 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008580}
8581
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01008582void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01008583{
8584 struct kvm_segment cs;
8585
8586 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
8587 cs.selector = vector << 8;
8588 cs.base = vector << 12;
8589 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8590 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008591}
8592
Radim Krčmář13a34e02014-08-28 15:13:03 +02008593int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008594{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10008595 struct kvm *kvm;
8596 struct kvm_vcpu *vcpu;
8597 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008598 int ret;
8599 u64 local_tsc;
8600 u64 max_tsc = 0;
8601 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03008602
8603 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02008604 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008605 if (ret != 0)
8606 return ret;
8607
Andy Lutomirski4ea16362015-06-25 18:44:07 +02008608 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008609 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008610 list_for_each_entry(kvm, &vm_list, vm_list) {
8611 kvm_for_each_vcpu(i, vcpu, kvm) {
8612 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08008613 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008614 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
8615 backwards_tsc = true;
8616 if (vcpu->arch.last_host_tsc > max_tsc)
8617 max_tsc = vcpu->arch.last_host_tsc;
8618 }
8619 }
8620 }
8621
8622 /*
8623 * Sometimes, even reliable TSCs go backwards. This happens on
8624 * platforms that reset TSC during suspend or hibernate actions, but
8625 * maintain synchronization. We must compensate. Fortunately, we can
8626 * detect that condition here, which happens early in CPU bringup,
8627 * before any KVM threads can be running. Unfortunately, we can't
8628 * bring the TSCs fully up to date with real time, as we aren't yet far
8629 * enough into CPU bringup that we know how much real time has actually
Paolo Bonzini108b2492016-09-01 14:21:03 +02008630 * elapsed; our helper function, ktime_get_boot_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008631 * variables that haven't been updated yet.
8632 *
8633 * So we simply find the maximum observed TSC above, then record the
8634 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
8635 * the adjustment will be applied. Note that we accumulate
8636 * adjustments, in case multiple suspend cycles happen before some VCPU
8637 * gets a chance to run again. In the event that no KVM threads get a
8638 * chance to run, we will miss the entire elapsed period, as we'll have
8639 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
8640 * loose cycle time. This isn't too big a deal, since the loss will be
8641 * uniform across all VCPUs (not to mention the scenario is extremely
8642 * unlikely). It is possible that a second hibernate recovery happens
8643 * much faster than a first, causing the observed TSC here to be
8644 * smaller; this would require additional padding adjustment, which is
8645 * why we set last_host_tsc to the local tsc observed here.
8646 *
8647 * N.B. - this code below runs only on platforms with reliable TSC,
8648 * as that is the only way backwards_tsc is set above. Also note
8649 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
8650 * have the same delta_cyc adjustment applied if backwards_tsc
8651 * is detected. Note further, this adjustment is only done once,
8652 * as we reset last_host_tsc on all VCPUs to stop this from being
8653 * called multiple times (one for each physical CPU bringup).
8654 *
Guo Chao4a969982012-06-28 15:17:27 +08008655 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008656 * will be compensated by the logic in vcpu_load, which sets the TSC to
8657 * catchup mode. This will catchup all VCPUs to real time, but cannot
8658 * guarantee that they stay in perfect synchronization.
8659 */
8660 if (backwards_tsc) {
8661 u64 delta_cyc = max_tsc - local_tsc;
8662 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02008663 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008664 kvm_for_each_vcpu(i, vcpu, kvm) {
8665 vcpu->arch.tsc_offset_adjustment += delta_cyc;
8666 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08008667 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02008668 }
8669
8670 /*
8671 * We have to disable TSC offset matching.. if you were
8672 * booting a VM while issuing an S4 host suspend....
8673 * you may have some problem. Solving this issue is
8674 * left as an exercise to the reader.
8675 */
8676 kvm->arch.last_tsc_nsec = 0;
8677 kvm->arch.last_tsc_write = 0;
8678 }
8679
8680 }
8681 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008682}
8683
Radim Krčmář13a34e02014-08-28 15:13:03 +02008684void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008685{
Radim Krčmář13a34e02014-08-28 15:13:03 +02008686 kvm_x86_ops->hardware_disable();
8687 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008688}
8689
8690int kvm_arch_hardware_setup(void)
8691{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03008692 int r;
8693
8694 r = kvm_x86_ops->hardware_setup();
8695 if (r != 0)
8696 return r;
8697
Haozhong Zhang35181e82015-10-20 15:39:03 +08008698 if (kvm_has_tsc_control) {
8699 /*
8700 * Make sure the user can only configure tsc_khz values that
8701 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03008702 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08008703 * be 1 on all machines.
8704 */
8705 u64 max = min(0x7fffffffULL,
8706 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
8707 kvm_max_guest_tsc_khz = max;
8708
Haozhong Zhangad7218832015-10-20 15:39:02 +08008709 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08008710 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08008711
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03008712 kvm_init_msr_list();
8713 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008714}
8715
8716void kvm_arch_hardware_unsetup(void)
8717{
8718 kvm_x86_ops->hardware_unsetup();
8719}
8720
8721void kvm_arch_check_processor_compat(void *rtn)
8722{
8723 kvm_x86_ops->check_processor_compatibility(rtn);
8724}
8725
Paolo Bonzinid71ba782015-07-29 11:56:48 +02008726bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
8727{
8728 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
8729}
8730EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
8731
8732bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
8733{
8734 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
8735}
8736
Gleb Natapov54e98182012-08-05 15:58:32 +03008737struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008738EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03008739
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008740int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
8741{
8742 struct page *page;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008743 int r;
8744
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05008745 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu);
Avi Kivity9aabc88f2010-07-29 15:11:50 +03008746 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
David Hildenbrand26de7982017-08-24 20:51:25 +02008747 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03008748 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008749 else
Avi Kivitya4535292008-04-13 17:54:35 +03008750 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008751
8752 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
8753 if (!page) {
8754 r = -ENOMEM;
8755 goto fail;
8756 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008757 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008758
Zachary Amsdencc578282012-02-03 15:43:50 -02008759 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10008760
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008761 r = kvm_mmu_create(vcpu);
8762 if (r < 0)
8763 goto fail_free_pio_data;
8764
David Hildenbrand26de7982017-08-24 20:51:25 +02008765 if (irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008766 r = kvm_create_lapic(vcpu);
8767 if (r < 0)
8768 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03008769 } else
8770 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008771
Huang Ying890ca9a2009-05-11 16:48:15 +08008772 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
8773 GFP_KERNEL);
8774 if (!vcpu->arch.mce_banks) {
8775 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08008776 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08008777 }
8778 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
8779
Wei Yongjunf1797352013-04-18 07:41:00 +08008780 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) {
8781 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008782 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08008783 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008784
Ingo Molnar0ee6a512015-04-27 06:58:22 +02008785 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01008786
Paolo Bonzini4344ee92013-10-02 16:06:16 +02008787 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02008788
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03008789 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
8790
Radim Krčmář74545702015-04-27 15:11:25 +02008791 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
8792
Gleb Natapovaf585b92010-10-14 11:22:46 +02008793 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008794 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008795
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02008796 vcpu->arch.pending_external_vector = -1;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008797 vcpu->arch.preempted_in_kernel = false;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02008798
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008799 kvm_hv_vcpu_init(vcpu);
8800
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008801 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02008802
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008803fail_free_mce_banks:
8804 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08008805fail_free_lapic:
8806 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008807fail_mmu_destroy:
8808 kvm_mmu_destroy(vcpu);
8809fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008810 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008811fail:
8812 return r;
8813}
8814
8815void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
8816{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008817 int idx;
8818
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008819 kvm_hv_vcpu_uninit(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008820 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08008821 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008822 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008823 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008824 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008825 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008826 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02008827 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03008828 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008829}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008830
Radim Krčmáře790d9e2014-08-21 18:08:05 +02008831void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
8832{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008833 vcpu->arch.l1tf_flush_l1d = true;
Radim Krčmářae97a3b2014-08-21 18:08:06 +02008834 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02008835}
8836
Carsten Ottee08b9632012-01-04 10:25:20 +01008837int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008838{
Carsten Ottee08b9632012-01-04 10:25:20 +01008839 if (type)
8840 return -EINVAL;
8841
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01008842 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08008843 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Xiao Guangrong365c8862013-05-31 08:36:29 +08008844 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03008845 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06008846 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008847
Sheng Yang5550af42008-10-15 20:15:06 +08008848 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
8849 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06008850 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
8851 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
8852 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08008853
Jan Kiszka038f8c12011-02-04 10:49:11 +01008854 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03008855 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008856 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
8857
Paolo Bonzini108b2492016-09-01 14:21:03 +02008858 kvm->arch.kvmclock_offset = -ktime_get_boot_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02008859 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01008860
Andrew Jones7e44e442014-02-28 12:52:54 +01008861 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01008862 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01008863
Roman Kagancbc02362018-02-01 16:48:31 +03008864 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08008865 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08008866 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08008867
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05008868 if (kvm_x86_ops->vm_init)
8869 return kvm_x86_ops->vm_init(kvm);
8870
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01008871 return 0;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008872}
8873
8874static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
8875{
Christoffer Dallec7660c2017-12-04 21:35:23 +01008876 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008877 kvm_mmu_unload(vcpu);
8878 vcpu_put(vcpu);
8879}
8880
8881static void kvm_free_vcpus(struct kvm *kvm)
8882{
8883 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03008884 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008885
8886 /*
8887 * Unpin any mmu pages first.
8888 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02008889 kvm_for_each_vcpu(i, vcpu, kvm) {
8890 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03008891 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02008892 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03008893 kvm_for_each_vcpu(i, vcpu, kvm)
8894 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008895
Gleb Natapov988a2ca2009-06-09 15:56:29 +03008896 mutex_lock(&kvm->lock);
8897 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
8898 kvm->vcpus[i] = NULL;
8899
8900 atomic_set(&kvm->online_vcpus, 0);
8901 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008902}
8903
Sheng Yangad8ba2c2009-01-06 10:03:02 +08008904void kvm_arch_sync_events(struct kvm *kvm)
8905{
Andrew Jones332967a2014-02-28 12:52:55 +01008906 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01008907 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08008908 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08008909}
8910
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008911int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008912{
8913 int i, r;
Paolo Bonzini25188b92015-10-14 15:51:08 +02008914 unsigned long hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02008915 struct kvm_memslots *slots = kvm_memslots(kvm);
8916 struct kvm_memory_slot *slot, old;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008917
8918 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008919 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
8920 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008921
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02008922 slot = id_to_memslot(slots, id);
8923 if (size) {
Paolo Bonzinib21629d2016-06-01 14:09:18 +02008924 if (slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02008925 return -EEXIST;
8926
8927 /*
8928 * MAP_SHARED to prevent internal slot pages from being moved
8929 * by fork()/COW.
8930 */
8931 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
8932 MAP_SHARED | MAP_ANONYMOUS, 0);
8933 if (IS_ERR((void *)hva))
8934 return PTR_ERR((void *)hva);
8935 } else {
8936 if (!slot->npages)
8937 return 0;
8938
8939 hva = 0;
8940 }
8941
8942 old = *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008943 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008944 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008945
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008946 m.slot = id | (i << 16);
8947 m.flags = 0;
8948 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02008949 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008950 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008951 r = __kvm_set_memory_region(kvm, &m);
8952 if (r < 0)
8953 return r;
8954 }
8955
Eric Biggers103c7632018-01-31 17:30:21 -08008956 if (!size)
8957 vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02008958
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008959 return 0;
8960}
8961EXPORT_SYMBOL_GPL(__x86_set_memory_region);
8962
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008963int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008964{
8965 int r;
8966
8967 mutex_lock(&kvm->slots_lock);
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008968 r = __x86_set_memory_region(kvm, id, gpa, size);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02008969 mutex_unlock(&kvm->slots_lock);
8970
8971 return r;
8972}
8973EXPORT_SYMBOL_GPL(x86_set_memory_region);
8974
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008975void kvm_arch_destroy_vm(struct kvm *kvm)
8976{
Andrew Honig27469d22013-04-18 09:38:14 -07008977 if (current->mm == kvm->mm) {
8978 /*
8979 * Free memory regions allocated on behalf of userspace,
8980 * unless the the memory map has changed due to process exit
8981 * or fd copying.
8982 */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02008983 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
8984 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
8985 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
Andrew Honig27469d22013-04-18 09:38:14 -07008986 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05008987 if (kvm_x86_ops->vm_destroy)
8988 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08008989 kvm_pic_destroy(kvm);
8990 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008991 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02008992 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08008993 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02008994 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03008995 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08008996}
Zhang Xiantao0de10342007-11-20 16:25:04 +08008997
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05308998void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09008999 struct kvm_memory_slot *dont)
9000{
9001 int i;
9002
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009003 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
9004 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009005 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009006 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009007 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009008 if (i == 0)
9009 continue;
9010
9011 if (!dont || free->arch.lpage_info[i - 1] !=
9012 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009013 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009014 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009015 }
9016 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009017
9018 kvm_page_track_free_memslot(free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009019}
9020
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309021int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
9022 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009023{
9024 int i;
9025
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009026 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009027 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009028 unsigned long ugfn;
9029 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009030 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009031
9032 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9033 slot->base_gfn, level) + 1;
9034
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009035 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009036 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
9037 GFP_KERNEL);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009038 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009039 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009040 if (i == 0)
9041 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009042
Kees Cook778e1cd2018-06-12 14:04:48 -07009043 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009044 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009045 goto out_free;
9046
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009047 slot->arch.lpage_info[i - 1] = linfo;
9048
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009049 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009050 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009051 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009052 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009053 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9054 /*
9055 * If the gfn and userspace address are not aligned wrt each
9056 * other, or if explicitly asked to, disable large page
9057 * support for this slot
9058 */
9059 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
9060 !kvm_largepages_enabled()) {
9061 unsigned long j;
9062
9063 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009064 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009065 }
9066 }
9067
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009068 if (kvm_page_track_create_memslot(slot, npages))
9069 goto out_free;
9070
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009071 return 0;
9072
9073out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009074 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009075 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009076 slot->arch.rmap[i] = NULL;
9077 if (i == 0)
9078 continue;
9079
Thomas Huth548ef282015-02-24 21:29:25 +01009080 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009081 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009082 }
9083 return -ENOMEM;
9084}
9085
Paolo Bonzini15f46012015-05-17 21:26:08 +02009086void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009087{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009088 /*
9089 * memslots->generation has been incremented.
9090 * mmio generation may have reached its maximum value.
9091 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009092 kvm_mmu_invalidate_mmio_sptes(kvm, slots);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009093}
9094
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009095int kvm_arch_prepare_memory_region(struct kvm *kvm,
9096 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009097 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009098 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009099{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009100 return 0;
9101}
9102
Kai Huang88178fd2015-01-28 10:54:27 +08009103static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9104 struct kvm_memory_slot *new)
9105{
9106 /* Still write protect RO slot */
9107 if (new->flags & KVM_MEM_READONLY) {
9108 kvm_mmu_slot_remove_write_access(kvm, new);
9109 return;
9110 }
9111
9112 /*
9113 * Call kvm_x86_ops dirty logging hooks when they are valid.
9114 *
9115 * kvm_x86_ops->slot_disable_log_dirty is called when:
9116 *
9117 * - KVM_MR_CREATE with dirty logging is disabled
9118 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9119 *
9120 * The reason is, in case of PML, we need to set D-bit for any slots
9121 * with dirty logging disabled in order to eliminate unnecessary GPA
9122 * logging in PML buffer (and potential PML buffer full VMEXT). This
9123 * guarantees leaving PML enabled during guest's lifetime won't have
9124 * any additonal overhead from PML when guest is running with dirty
9125 * logging disabled for memory slots.
9126 *
9127 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
9128 * to dirty logging mode.
9129 *
9130 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
9131 *
9132 * In case of write protect:
9133 *
9134 * Write protect all pages for dirty logging.
9135 *
9136 * All the sptes including the large sptes which point to this
9137 * slot are set to readonly. We can not create any new large
9138 * spte on this slot until the end of the logging.
9139 *
9140 * See the comments in fast_page_fault().
9141 */
9142 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
9143 if (kvm_x86_ops->slot_enable_log_dirty)
9144 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
9145 else
9146 kvm_mmu_slot_remove_write_access(kvm, new);
9147 } else {
9148 if (kvm_x86_ops->slot_disable_log_dirty)
9149 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
9150 }
9151}
9152
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009153void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009154 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009155 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009156 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009157 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009158{
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009159 int nr_mmu_pages = 0;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009160
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08009161 if (!kvm->arch.n_requested_mmu_pages)
9162 nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
Zhang Xiantao0de10342007-11-20 16:25:04 +08009163
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08009164 if (nr_mmu_pages)
9165 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
Kai Huang1c91cad42015-01-28 10:54:26 +08009166
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009167 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08009168 * Dirty logging tracks sptes in 4k granularity, meaning that large
9169 * sptes have to be split. If live migration is successful, the guest
9170 * in the source machine will be destroyed and large sptes will be
9171 * created in the destination. However, if the guest continues to run
9172 * in the source machine (for example if live migration fails), small
9173 * sptes will remain around and cause bad performance.
9174 *
9175 * Scan sptes if dirty logging has been stopped, dropping those
9176 * which can be collapsed into a single large-page spte. Later
9177 * page faults will create the large-page sptes.
9178 */
9179 if ((change != KVM_MR_DELETE) &&
9180 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
9181 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
9182 kvm_mmu_zap_collapsible_sptes(kvm, new);
9183
9184 /*
Kai Huang88178fd2015-01-28 10:54:27 +08009185 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08009186 *
Kai Huang88178fd2015-01-28 10:54:27 +08009187 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
9188 * been zapped so no dirty logging staff is needed for old slot. For
9189 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
9190 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009191 *
9192 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009193 */
Kai Huang88178fd2015-01-28 10:54:27 +08009194 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009195 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08009196}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009197
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009198void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009199{
Xiao Guangrong6ca18b692013-05-31 08:36:23 +08009200 kvm_mmu_invalidate_zap_all_pages(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009201}
9202
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009203void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
9204 struct kvm_memory_slot *slot)
9205{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +08009206 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009207}
9208
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009209static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
9210{
9211 if (!list_empty_careful(&vcpu->async_pf.done))
9212 return true;
9213
9214 if (kvm_apic_has_events(vcpu))
9215 return true;
9216
9217 if (vcpu->arch.pv.pv_unhalted)
9218 return true;
9219
Wanpeng Lia5f01f82017-09-13 04:04:01 -07009220 if (vcpu->arch.exception.pending)
9221 return true;
9222
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009223 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9224 (vcpu->arch.nmi_pending &&
9225 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009226 return true;
9227
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009228 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
9229 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +02009230 return true;
9231
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009232 if (kvm_arch_interrupt_allowed(vcpu) &&
9233 kvm_cpu_has_interrupt(vcpu))
9234 return true;
9235
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009236 if (kvm_hv_has_stimer_pending(vcpu))
9237 return true;
9238
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009239 return false;
9240}
9241
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009242int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
9243{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009244 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009245}
Zhang Xiantao57361992007-12-17 14:21:40 +08009246
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009247bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
9248{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009249 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009250}
9251
Christoffer Dallb6d33832012-03-08 16:44:24 -05009252int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08009253{
Christoffer Dallb6d33832012-03-08 16:44:24 -05009254 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08009255}
Gleb Natapov78646122009-03-23 12:12:11 +02009256
9257int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
9258{
9259 return kvm_x86_ops->interrupt_allowed(vcpu);
9260}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009261
Nadav Amit82b32772014-11-02 11:54:45 +02009262unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
9263{
9264 if (is_64_bit_mode(vcpu))
9265 return kvm_rip_read(vcpu);
9266 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
9267 kvm_rip_read(vcpu));
9268}
9269EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
9270
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009271bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
9272{
Nadav Amit82b32772014-11-02 11:54:45 +02009273 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009274}
9275EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
9276
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009277unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
9278{
9279 unsigned long rflags;
9280
9281 rflags = kvm_x86_ops->get_rflags(vcpu);
9282 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01009283 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009284 return rflags;
9285}
9286EXPORT_SYMBOL_GPL(kvm_get_rflags);
9287
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009288static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009289{
9290 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009291 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01009292 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009293 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009294}
9295
9296void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
9297{
9298 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03009299 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009300}
9301EXPORT_SYMBOL_GPL(kvm_set_rflags);
9302
Gleb Natapov56028d02010-10-17 18:13:42 +02009303void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
9304{
9305 int r;
9306
Xiao Guangrongfb67e142010-12-07 10:35:25 +08009307 if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08009308 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02009309 return;
9310
9311 r = kvm_mmu_reload(vcpu);
9312 if (unlikely(r))
9313 return;
9314
Xiao Guangrongfb67e142010-12-07 10:35:25 +08009315 if (!vcpu->arch.mmu.direct_map &&
9316 work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
9317 return;
9318
Gleb Natapov56028d02010-10-17 18:13:42 +02009319 vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
9320}
9321
Gleb Natapovaf585b92010-10-14 11:22:46 +02009322static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
9323{
9324 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
9325}
9326
9327static inline u32 kvm_async_pf_next_probe(u32 key)
9328{
9329 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
9330}
9331
9332static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9333{
9334 u32 key = kvm_async_pf_hash_fn(gfn);
9335
9336 while (vcpu->arch.apf.gfns[key] != ~0)
9337 key = kvm_async_pf_next_probe(key);
9338
9339 vcpu->arch.apf.gfns[key] = gfn;
9340}
9341
9342static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
9343{
9344 int i;
9345 u32 key = kvm_async_pf_hash_fn(gfn);
9346
9347 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08009348 (vcpu->arch.apf.gfns[key] != gfn &&
9349 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02009350 key = kvm_async_pf_next_probe(key);
9351
9352 return key;
9353}
9354
9355bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9356{
9357 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
9358}
9359
9360static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9361{
9362 u32 i, j, k;
9363
9364 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
9365 while (true) {
9366 vcpu->arch.apf.gfns[i] = ~0;
9367 do {
9368 j = kvm_async_pf_next_probe(j);
9369 if (vcpu->arch.apf.gfns[j] == ~0)
9370 return;
9371 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
9372 /*
9373 * k lies cyclically in ]i,j]
9374 * | i.k.j |
9375 * |....j i.k.| or |.k..j i...|
9376 */
9377 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
9378 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
9379 i = j;
9380 }
9381}
9382
Gleb Natapov7c907052010-10-14 11:22:53 +02009383static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
9384{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02009385
9386 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
9387 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +02009388}
9389
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009390static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
9391{
9392
9393 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
9394 sizeof(u32));
9395}
9396
Gleb Natapovaf585b92010-10-14 11:22:46 +02009397void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
9398 struct kvm_async_pf *work)
9399{
Avi Kivity6389ee92010-11-29 16:12:30 +02009400 struct x86_exception fault;
9401
Gleb Natapov7c907052010-10-14 11:22:53 +02009402 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009403 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02009404
9405 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
Gleb Natapovfc5f06f2010-10-14 11:22:56 +02009406 (vcpu->arch.apf.send_user_only &&
9407 kvm_x86_ops->get_cpl(vcpu) == 0))
Gleb Natapov7c907052010-10-14 11:22:53 +02009408 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
9409 else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02009410 fault.vector = PF_VECTOR;
9411 fault.error_code_valid = true;
9412 fault.error_code = 0;
9413 fault.nested_page_fault = false;
9414 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07009415 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +02009416 kvm_inject_page_fault(vcpu, &fault);
Gleb Natapov7c907052010-10-14 11:22:53 +02009417 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009418}
9419
9420void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
9421 struct kvm_async_pf *work)
9422{
Avi Kivity6389ee92010-11-29 16:12:30 +02009423 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009424 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +02009425
chai wenf2e10662013-10-14 22:22:33 +08009426 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02009427 work->arch.token = ~0; /* broadcast wakeup */
9428 else
9429 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Wanpeng Li24dccf82017-03-20 21:18:55 -07009430 trace_kvm_async_pf_ready(work->arch.token, work->gva);
Gleb Natapov7c907052010-10-14 11:22:53 +02009431
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009432 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
9433 !apf_get_user(vcpu, &val)) {
9434 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
9435 vcpu->arch.exception.pending &&
9436 vcpu->arch.exception.nr == PF_VECTOR &&
9437 !apf_put_user(vcpu, 0)) {
9438 vcpu->arch.exception.injected = false;
9439 vcpu->arch.exception.pending = false;
9440 vcpu->arch.exception.nr = 0;
9441 vcpu->arch.exception.has_error_code = false;
9442 vcpu->arch.exception.error_code = 0;
9443 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
9444 fault.vector = PF_VECTOR;
9445 fault.error_code_valid = true;
9446 fault.error_code = 0;
9447 fault.nested_page_fault = false;
9448 fault.address = work->arch.token;
9449 fault.async_page_fault = true;
9450 kvm_inject_page_fault(vcpu, &fault);
9451 }
Gleb Natapov7c907052010-10-14 11:22:53 +02009452 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08009453 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03009454 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02009455}
9456
9457bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
9458{
9459 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
9460 return true;
9461 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -07009462 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009463}
9464
Paolo Bonzini5544eb92015-07-07 15:41:58 +02009465void kvm_arch_start_assignment(struct kvm *kvm)
9466{
9467 atomic_inc(&kvm->arch.assigned_device_count);
9468}
9469EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
9470
9471void kvm_arch_end_assignment(struct kvm *kvm)
9472{
9473 atomic_dec(&kvm->arch.assigned_device_count);
9474}
9475EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
9476
9477bool kvm_arch_has_assigned_device(struct kvm *kvm)
9478{
9479 return atomic_read(&kvm->arch.assigned_device_count);
9480}
9481EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
9482
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009483void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
9484{
9485 atomic_inc(&kvm->arch.noncoherent_dma_count);
9486}
9487EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
9488
9489void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
9490{
9491 atomic_dec(&kvm->arch.noncoherent_dma_count);
9492}
9493EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
9494
9495bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
9496{
9497 return atomic_read(&kvm->arch.noncoherent_dma_count);
9498}
9499EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
9500
Alex Williamson14717e22016-05-05 11:58:35 -06009501bool kvm_arch_has_irq_bypass(void)
9502{
9503 return kvm_x86_ops->update_pi_irte != NULL;
9504}
9505
Feng Wu87276882015-09-18 22:29:40 +08009506int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
9507 struct irq_bypass_producer *prod)
9508{
9509 struct kvm_kernel_irqfd *irqfd =
9510 container_of(cons, struct kvm_kernel_irqfd, consumer);
9511
Alex Williamson14717e22016-05-05 11:58:35 -06009512 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +08009513
Alex Williamson14717e22016-05-05 11:58:35 -06009514 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
9515 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +08009516}
9517
9518void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
9519 struct irq_bypass_producer *prod)
9520{
9521 int ret;
9522 struct kvm_kernel_irqfd *irqfd =
9523 container_of(cons, struct kvm_kernel_irqfd, consumer);
9524
Feng Wu87276882015-09-18 22:29:40 +08009525 WARN_ON(irqfd->producer != prod);
9526 irqfd->producer = NULL;
9527
9528 /*
9529 * When producer of consumer is unregistered, we change back to
9530 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +02009531 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +08009532 * int this case doesn't want to receive the interrupts.
9533 */
9534 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
9535 if (ret)
9536 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
9537 " fails: %d\n", irqfd->consumer.token, ret);
9538}
9539
9540int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
9541 uint32_t guest_irq, bool set)
9542{
9543 if (!kvm_x86_ops->update_pi_irte)
9544 return -EINVAL;
9545
9546 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
9547}
9548
Feng Wu520040142016-01-25 16:53:33 +08009549bool kvm_vector_hashing_enabled(void)
9550{
9551 return vector_hashing;
9552}
9553EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
9554
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009555EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +08009556EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009557EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
9558EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
9559EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
9560EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02009561EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02009562EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02009563EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02009564EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02009565EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02009566EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01009567EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09009568EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +02009569EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
Kai Huang843e4332015-01-28 10:54:28 +08009570EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +08009571EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05009572EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
9573EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);