blob: cc1e92da8f3c7b29707c8bd929b2aa412caa4269 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Carsten Otte043405e2007-10-10 17:16:19 +02002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * derived from drivers/kvm/kvm_main.c
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03008 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020011 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030015 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020017 */
18
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020020#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080022#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020023#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030025#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020026#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020027#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030028#include "hyperv.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020029
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020030#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030031#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020032#include <linux/kvm.h>
33#include <linux/fs.h>
34#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040035#include <linux/export.h>
36#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080037#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050038#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010039#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030040#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010041#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030042#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020043#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080045#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080046#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020047#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020048#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020049#include <linux/timekeeper_internal.h>
50#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080051#include <linux/kvm_irqfd.h>
52#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010053#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080054#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050055#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010056
Avi Kivityaec51dc2009-07-01 16:01:02 +030057#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080058
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020059#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080060#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020061#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080062#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020063#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020064#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100065#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030066#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080067#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010068#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010069#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080070#include <asm/intel_pt.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000071#include <clocksource/hyperv_timer.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
Sean Christophersonc3941d92019-04-17 10:15:33 -0700138/*
139 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
140 * adaptive tuning starting from default advancment of 1000ns. '0' disables
141 * advancement entirely. Any other value is used as-is and disables adaptive
142 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
143 */
144static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800145module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500146
Feng Wu520040142016-01-25 16:53:33 +0800147static bool __read_mostly vector_hashing = true;
148module_param(vector_hashing, bool, S_IRUGO);
149
Liran Alonc4ae60e2018-03-12 13:12:47 +0200150bool __read_mostly enable_vmware_backdoor = false;
151module_param(enable_vmware_backdoor, bool, S_IRUGO);
152EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
153
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700154static bool __read_mostly force_emulation_prefix = false;
155module_param(force_emulation_prefix, bool, S_IRUGO);
156
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800157int __read_mostly pi_inject_timer = -1;
158module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
159
Avi Kivity18863bd2009-09-07 11:12:18 +0300160#define KVM_NR_SHARED_MSRS 16
161
162struct kvm_shared_msrs_global {
163 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800164 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300165};
166
167struct kvm_shared_msrs {
168 struct user_return_notifier urn;
169 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800170 struct kvm_shared_msr_values {
171 u64 host;
172 u64 curr;
173 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300174};
175
176static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200177static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300178
Hollis Blanchard417bc302007-10-31 17:24:23 -0500179struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200180 { "pf_fixed", VCPU_STAT(pf_fixed) },
181 { "pf_guest", VCPU_STAT(pf_guest) },
182 { "tlb_flush", VCPU_STAT(tlb_flush) },
183 { "invlpg", VCPU_STAT(invlpg) },
184 { "exits", VCPU_STAT(exits) },
185 { "io_exits", VCPU_STAT(io_exits) },
186 { "mmio_exits", VCPU_STAT(mmio_exits) },
187 { "signal_exits", VCPU_STAT(signal_exits) },
188 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800189 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200190 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100191 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200192 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200193 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200194 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530195 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200196 { "request_irq", VCPU_STAT(request_irq_exits) },
197 { "irq_exits", VCPU_STAT(irq_exits) },
198 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200199 { "fpu_reload", VCPU_STAT(fpu_reload) },
200 { "insn_emulation", VCPU_STAT(insn_emulation) },
201 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300202 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200203 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100204 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200205 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200206 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
207 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
208 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
209 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
210 { "mmu_flooded", VM_STAT(mmu_flooded) },
211 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200212 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300213 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200214 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300215 { "largepages", VM_STAT(lpages) },
David Matlackf3414bc2016-12-20 15:25:57 -0800216 { "max_mmu_page_hash_collisions",
217 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500218 { NULL }
219};
220
Dexuan Cui2acf9232010-06-10 11:27:12 +0800221u64 __read_mostly host_xcr0;
222
Marc Orrb666a4b2018-11-06 14:53:56 -0800223struct kmem_cache *x86_fpu_cache;
224EXPORT_SYMBOL_GPL(x86_fpu_cache);
225
Jan Kiszkab6785de2012-09-20 07:43:17 +0200226static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300227
Gleb Natapovaf585b92010-10-14 11:22:46 +0200228static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
229{
230 int i;
231 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
232 vcpu->arch.apf.gfns[i] = ~0;
233}
234
Avi Kivity18863bd2009-09-07 11:12:18 +0300235static void kvm_on_user_return(struct user_return_notifier *urn)
236{
237 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300238 struct kvm_shared_msrs *locals
239 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800240 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700241 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300242
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700243 /*
244 * Disabling irqs at this point since the following code could be
245 * interrupted and executed through kvm_arch_hardware_disable()
246 */
247 local_irq_save(flags);
248 if (locals->registered) {
249 locals->registered = false;
250 user_return_notifier_unregister(urn);
251 }
252 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300253 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800254 values = &locals->values[slot];
255 if (values->host != values->curr) {
256 wrmsrl(shared_msrs_global.msrs[slot], values->host);
257 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300258 }
259 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300260}
261
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800262static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300263{
Avi Kivity18863bd2009-09-07 11:12:18 +0300264 u64 value;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200265 unsigned int cpu = smp_processor_id();
266 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity18863bd2009-09-07 11:12:18 +0300267
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800268 /* only read, and nobody should modify it at this time,
269 * so don't need lock */
270 if (slot >= shared_msrs_global.nr) {
271 printk(KERN_ERR "kvm: invalid MSR slot!");
272 return;
273 }
274 rdmsrl_safe(msr, &value);
275 smsr->values[slot].host = value;
276 smsr->values[slot].curr = value;
277}
278
279void kvm_define_shared_msr(unsigned slot, u32 msr)
280{
Nadav Amit0123be42014-07-24 15:06:56 +0300281 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200282 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300283 if (slot >= shared_msrs_global.nr)
284 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300285}
286EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
287
288static void kvm_shared_msr_cpu_online(void)
289{
290 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300291
292 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800293 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300294}
295
Andy Honig8b3c3102014-08-27 11:16:44 -0700296int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300297{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200298 unsigned int cpu = smp_processor_id();
299 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700300 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300301
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800302 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Andy Honig8b3c3102014-08-27 11:16:44 -0700303 return 0;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800304 smsr->values[slot].curr = value;
Andy Honig8b3c3102014-08-27 11:16:44 -0700305 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
306 if (err)
307 return 1;
308
Avi Kivity18863bd2009-09-07 11:12:18 +0300309 if (!smsr->registered) {
310 smsr->urn.on_user_return = kvm_on_user_return;
311 user_return_notifier_register(&smsr->urn);
312 smsr->registered = true;
313 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700314 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300315}
316EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
317
Radim Krčmář13a34e02014-08-28 15:13:03 +0200318static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200319{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200320 unsigned int cpu = smp_processor_id();
321 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200322
323 if (smsr->registered)
324 kvm_on_user_return(&smsr->urn);
325}
326
Carsten Otte6866b832007-10-29 16:09:10 +0100327u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
328{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300329 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100330}
331EXPORT_SYMBOL_GPL(kvm_get_apic_base);
332
Jim Mattson58871642018-05-09 16:56:04 -0400333enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
334{
335 return kvm_apic_mode(kvm_get_apic_base(vcpu));
336}
337EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
338
Jan Kiszka58cb6282014-01-24 16:48:44 +0100339int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100340{
Jim Mattson58871642018-05-09 16:56:04 -0400341 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
342 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200343 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
344 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100345
Jim Mattson58871642018-05-09 16:56:04 -0400346 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700347 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400348 if (!msr_info->host_initiated) {
349 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
350 return 1;
351 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
352 return 1;
353 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100354
355 kvm_lapic_set_base(vcpu, msr_info->data);
356 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100357}
358EXPORT_SYMBOL_GPL(kvm_set_apic_base);
359
Andi Kleen2605fc22014-05-02 00:44:37 +0200360asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000361{
362 /* Fault while not rebooting. We want the trace. */
363 BUG();
364}
365EXPORT_SYMBOL_GPL(kvm_spurious_fault);
366
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200367#define EXCPT_BENIGN 0
368#define EXCPT_CONTRIBUTORY 1
369#define EXCPT_PF 2
370
371static int exception_class(int vector)
372{
373 switch (vector) {
374 case PF_VECTOR:
375 return EXCPT_PF;
376 case DE_VECTOR:
377 case TS_VECTOR:
378 case NP_VECTOR:
379 case SS_VECTOR:
380 case GP_VECTOR:
381 return EXCPT_CONTRIBUTORY;
382 default:
383 break;
384 }
385 return EXCPT_BENIGN;
386}
387
Nadav Amitd6e8c852014-07-24 14:51:24 +0300388#define EXCPT_FAULT 0
389#define EXCPT_TRAP 1
390#define EXCPT_ABORT 2
391#define EXCPT_INTERRUPT 3
392
393static int exception_type(int vector)
394{
395 unsigned int mask;
396
397 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
398 return EXCPT_INTERRUPT;
399
400 mask = 1 << vector;
401
402 /* #DB is trap, as instruction watchpoints are handled elsewhere */
403 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
404 return EXCPT_TRAP;
405
406 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
407 return EXCPT_ABORT;
408
409 /* Reserved exceptions will result in fault */
410 return EXCPT_FAULT;
411}
412
Jim Mattsonda998b42018-10-16 14:29:22 -0700413void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
414{
415 unsigned nr = vcpu->arch.exception.nr;
416 bool has_payload = vcpu->arch.exception.has_payload;
417 unsigned long payload = vcpu->arch.exception.payload;
418
419 if (!has_payload)
420 return;
421
422 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700423 case DB_VECTOR:
424 /*
425 * "Certain debug exceptions may clear bit 0-3. The
426 * remaining contents of the DR6 register are never
427 * cleared by the processor".
428 */
429 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
430 /*
431 * DR6.RTM is set by all #DB exceptions that don't clear it.
432 */
433 vcpu->arch.dr6 |= DR6_RTM;
434 vcpu->arch.dr6 |= payload;
435 /*
436 * Bit 16 should be set in the payload whenever the #DB
437 * exception should clear DR6.RTM. This makes the payload
438 * compatible with the pending debug exceptions under VMX.
439 * Though not currently documented in the SDM, this also
440 * makes the payload compatible with the exit qualification
441 * for #DB exceptions under VMX.
442 */
443 vcpu->arch.dr6 ^= payload & DR6_RTM;
444 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700445 case PF_VECTOR:
446 vcpu->arch.cr2 = payload;
447 break;
448 }
449
450 vcpu->arch.exception.has_payload = false;
451 vcpu->arch.exception.payload = 0;
452}
453EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
454
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200455static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200456 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700457 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200458{
459 u32 prev_nr;
460 int class1, class2;
461
Avi Kivity3842d132010-07-27 12:30:24 +0300462 kvm_make_request(KVM_REQ_EVENT, vcpu);
463
Wanpeng Li664f8e22017-08-24 03:35:09 -0700464 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200465 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200466 if (has_error && !is_protmode(vcpu))
467 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700468 if (reinject) {
469 /*
470 * On vmentry, vcpu->arch.exception.pending is only
471 * true if an event injection was blocked by
472 * nested_run_pending. In that case, however,
473 * vcpu_enter_guest requests an immediate exit,
474 * and the guest shouldn't proceed far enough to
475 * need reinjection.
476 */
477 WARN_ON_ONCE(vcpu->arch.exception.pending);
478 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700479 if (WARN_ON_ONCE(has_payload)) {
480 /*
481 * A reinjected event has already
482 * delivered its payload.
483 */
484 has_payload = false;
485 payload = 0;
486 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700487 } else {
488 vcpu->arch.exception.pending = true;
489 vcpu->arch.exception.injected = false;
490 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200491 vcpu->arch.exception.has_error_code = has_error;
492 vcpu->arch.exception.nr = nr;
493 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700494 vcpu->arch.exception.has_payload = has_payload;
495 vcpu->arch.exception.payload = payload;
Jim Mattsonda998b42018-10-16 14:29:22 -0700496 /*
497 * In guest mode, payload delivery should be deferred,
498 * so that the L1 hypervisor can intercept #PF before
Jim Mattsonf10c7292018-10-16 14:29:23 -0700499 * CR2 is modified (or intercept #DB before DR6 is
500 * modified under nVMX). However, for ABI
501 * compatibility with KVM_GET_VCPU_EVENTS and
502 * KVM_SET_VCPU_EVENTS, we can't delay payload
503 * delivery unless userspace has enabled this
504 * functionality via the per-VM capability,
505 * KVM_CAP_EXCEPTION_PAYLOAD.
Jim Mattsonda998b42018-10-16 14:29:22 -0700506 */
507 if (!vcpu->kvm->arch.exception_payload_enabled ||
508 !is_guest_mode(vcpu))
509 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200510 return;
511 }
512
513 /* to check exception */
514 prev_nr = vcpu->arch.exception.nr;
515 if (prev_nr == DF_VECTOR) {
516 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300517 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200518 return;
519 }
520 class1 = exception_class(prev_nr);
521 class2 = exception_class(nr);
522 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
523 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700524 /*
525 * Generate double fault per SDM Table 5-5. Set
526 * exception.pending = true so that the double fault
527 * can trigger a nested vmexit.
528 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200529 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700530 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200531 vcpu->arch.exception.has_error_code = true;
532 vcpu->arch.exception.nr = DF_VECTOR;
533 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700534 vcpu->arch.exception.has_payload = false;
535 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200536 } else
537 /* replace previous exception with a new one in a hope
538 that instruction re-execution will regenerate lost
539 exception */
540 goto queue;
541}
542
Avi Kivity298101d2007-11-25 13:41:11 +0200543void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
544{
Jim Mattson91e86d22018-10-16 14:29:21 -0700545 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200546}
547EXPORT_SYMBOL_GPL(kvm_queue_exception);
548
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200549void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
550{
Jim Mattson91e86d22018-10-16 14:29:21 -0700551 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200552}
553EXPORT_SYMBOL_GPL(kvm_requeue_exception);
554
Jim Mattsonf10c7292018-10-16 14:29:23 -0700555static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
556 unsigned long payload)
557{
558 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
559}
560
Jim Mattsonda998b42018-10-16 14:29:22 -0700561static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
562 u32 error_code, unsigned long payload)
563{
564 kvm_multiple_exception(vcpu, nr, true, error_code,
565 true, payload, false);
566}
567
Kyle Huey6affcbe2016-11-29 12:40:40 -0800568int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200569{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100570 if (err)
571 kvm_inject_gp(vcpu, 0);
572 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800573 return kvm_skip_emulated_instruction(vcpu);
574
575 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100576}
577EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200578
Avi Kivity6389ee92010-11-29 16:12:30 +0200579void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200580{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200581 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700582 vcpu->arch.exception.nested_apf =
583 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700584 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700585 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700586 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
587 } else {
588 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
589 fault->address);
590 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200591}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300592EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200593
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200594static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200595{
Avi Kivity6389ee92010-11-29 16:12:30 +0200596 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
597 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200598 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200599 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200600
601 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200602}
603
Sheng Yang3419ffc2008-05-15 09:52:48 +0800604void kvm_inject_nmi(struct kvm_vcpu *vcpu)
605{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300606 atomic_inc(&vcpu->arch.nmi_queued);
607 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800608}
609EXPORT_SYMBOL_GPL(kvm_inject_nmi);
610
Avi Kivity298101d2007-11-25 13:41:11 +0200611void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
612{
Jim Mattson91e86d22018-10-16 14:29:21 -0700613 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200614}
615EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
616
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200617void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
618{
Jim Mattson91e86d22018-10-16 14:29:21 -0700619 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200620}
621EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
622
Carsten Ottea03490e2007-10-29 16:09:35 +0100623/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300624 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
625 * a #GP and return false.
626 */
627bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200628{
Avi Kivity0a79b002009-09-01 12:03:25 +0300629 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
630 return true;
631 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
632 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100633}
Avi Kivity0a79b002009-09-01 12:03:25 +0300634EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100635
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300636bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
637{
638 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
639 return true;
640
641 kvm_queue_exception(vcpu, UD_VECTOR);
642 return false;
643}
644EXPORT_SYMBOL_GPL(kvm_require_dr);
645
Carsten Ottea03490e2007-10-29 16:09:35 +0100646/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200647 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200648 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200649 * can read from guest physical or from the guest's guest physical memory.
650 */
651int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
652 gfn_t ngfn, void *data, int offset, int len,
653 u32 access)
654{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200655 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200656 gfn_t real_gfn;
657 gpa_t ngpa;
658
659 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200660 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200661 if (real_gfn == UNMAPPED_GVA)
662 return -EFAULT;
663
664 real_gfn = gpa_to_gfn(real_gfn);
665
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200666 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200667}
668EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
669
Fengguang Wu69b00492015-01-19 22:33:39 +0800670static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200671 void *data, int offset, int len, u32 access)
672{
673 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
674 data, offset, len, access);
675}
676
Joerg Roedelec92fe42010-09-10 17:30:51 +0200677/*
Carsten Ottea03490e2007-10-29 16:09:35 +0100678 * Load the pae pdptrs. Return true is they are all valid.
679 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200680int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100681{
682 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
683 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
684 int i;
685 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200686 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100687
Joerg Roedelff03a072010-09-10 17:30:57 +0200688 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
689 offset * sizeof(u64), sizeof(pdpte),
690 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100691 if (ret < 0) {
692 ret = 0;
693 goto out;
694 }
695 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400696 if ((pdpte[i] & PT_PRESENT_MASK) &&
Xiao Guangronga0a64f52015-08-05 12:04:21 +0800697 (pdpte[i] &
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200698 vcpu->arch.mmu->guest_rsvd_check.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100699 ret = 0;
700 goto out;
701 }
702 }
703 ret = 1;
704
Joerg Roedelff03a072010-09-10 17:30:57 +0200705 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300706 __set_bit(VCPU_EXREG_PDPTR,
707 (unsigned long *)&vcpu->arch.regs_avail);
708 __set_bit(VCPU_EXREG_PDPTR,
709 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100710out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100711
712 return ret;
713}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100714EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100715
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100716bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200717{
Joerg Roedelff03a072010-09-10 17:30:57 +0200718 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200719 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200720 int offset;
721 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200722 int r;
723
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200724 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200725 return false;
726
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300727 if (!test_bit(VCPU_EXREG_PDPTR,
728 (unsigned long *)&vcpu->arch.regs_avail))
729 return true;
730
Paolo Bonzinia5121772017-07-24 18:54:38 +0200731 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
732 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200733 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
734 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200735 if (r < 0)
736 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200737 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200738out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200739
740 return changed;
741}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100742EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200743
Avi Kivity49a9b072010-06-10 17:02:14 +0300744int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100745{
Sheng Yangaad82702010-05-12 16:40:42 +0800746 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800747 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800748
Avi Kivityf9a48e62010-01-06 19:10:22 +0200749 cr0 |= X86_CR0_ET;
750
Gleb Natapovab344822010-01-21 15:28:46 +0200751#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300752 if (cr0 & 0xffffffff00000000UL)
753 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200754#endif
755
756 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100757
Gleb Natapov0f122442010-04-28 19:15:31 +0300758 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
759 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100760
Gleb Natapov0f122442010-04-28 19:15:31 +0300761 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
762 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100763
764 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
765#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200766 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100767 int cs_db, cs_l;
768
Gleb Natapov0f122442010-04-28 19:15:31 +0300769 if (!is_pae(vcpu))
770 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100771 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300772 if (cs_l)
773 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100774 } else
775#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200776 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200777 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300778 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100779 }
780
Mao, Junjiead756a12012-07-02 01:18:48 +0000781 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
782 return 1;
783
Carsten Ottea03490e2007-10-29 16:09:35 +0100784 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100785
Lai Jiangshand170c412011-02-21 11:21:30 +0800786 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800787 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800788 kvm_async_pf_hash_reset(vcpu);
789 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800790
Sheng Yangaad82702010-05-12 16:40:42 +0800791 if ((cr0 ^ old_cr0) & update_bits)
792 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800793
Laszlo Ersek879ae182015-11-04 12:54:41 +0100794 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
795 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
796 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800797 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
798
Gleb Natapov0f122442010-04-28 19:15:31 +0300799 return 0;
800}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200801EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100802
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200803void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100804{
Avi Kivity49a9b072010-06-10 17:02:14 +0300805 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100806}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200807EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100808
WANG Chao1811d972019-04-12 15:55:39 +0800809void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300810{
811 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
812 !vcpu->guest_xcr0_loaded) {
813 /* kvm_set_xcr() also depends on this */
Paolo Bonzini476b7ad2017-12-13 13:51:32 +0100814 if (vcpu->arch.xcr0 != host_xcr0)
815 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300816 vcpu->guest_xcr0_loaded = 1;
817 }
818}
WANG Chao1811d972019-04-12 15:55:39 +0800819EXPORT_SYMBOL_GPL(kvm_load_guest_xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300820
WANG Chao1811d972019-04-12 15:55:39 +0800821void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300822{
823 if (vcpu->guest_xcr0_loaded) {
824 if (vcpu->arch.xcr0 != host_xcr0)
825 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
826 vcpu->guest_xcr0_loaded = 0;
827 }
828}
WANG Chao1811d972019-04-12 15:55:39 +0800829EXPORT_SYMBOL_GPL(kvm_put_guest_xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300830
Fengguang Wu69b00492015-01-19 22:33:39 +0800831static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800832{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000833 u64 xcr0 = xcr;
834 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200835 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800836
837 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
838 if (index != XCR_XFEATURE_ENABLED_MASK)
839 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700840 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800841 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700842 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800843 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200844
845 /*
846 * Do not allow the guest to set bits that we do not support
847 * saving. However, xcr0 bit 0 is always set, even if the
848 * emulated CPU does not support XSAVE (see fx_init).
849 */
Dave Hansend91cab72015-09-02 16:31:26 -0700850 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200851 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800852 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200853
Dave Hansend91cab72015-09-02 16:31:26 -0700854 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
855 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000856 return 1;
857
Dave Hansend91cab72015-09-02 16:31:26 -0700858 if (xcr0 & XFEATURE_MASK_AVX512) {
859 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800860 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700861 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800862 return 1;
863 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800864 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000865
Dave Hansend91cab72015-09-02 16:31:26 -0700866 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000867 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800868 return 0;
869}
870
871int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
872{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000873 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
874 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800875 kvm_inject_gp(vcpu, 0);
876 return 1;
877 }
878 return 0;
879}
880EXPORT_SYMBOL_GPL(kvm_set_xcr);
881
Avi Kivitya83b29c2010-06-10 17:02:15 +0300882int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100883{
Avi Kivityfc78f512009-12-07 12:16:48 +0200884 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800885 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800886 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800887
Gleb Natapov0f122442010-04-28 19:15:31 +0300888 if (cr4 & CR4_RESERVED_BITS)
889 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100890
Radim Krčmářd6321d42017-08-05 00:12:49 +0200891 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && (cr4 & X86_CR4_OSXSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800892 return 1;
893
Radim Krčmářd6321d42017-08-05 00:12:49 +0200894 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMEP) && (cr4 & X86_CR4_SMEP))
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800895 return 1;
896
Radim Krčmářd6321d42017-08-05 00:12:49 +0200897 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMAP) && (cr4 & X86_CR4_SMAP))
Feng Wu97ec8c02014-04-01 17:46:34 +0800898 return 1;
899
Radim Krčmářd6321d42017-08-05 00:12:49 +0200900 if (!guest_cpuid_has(vcpu, X86_FEATURE_FSGSBASE) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800901 return 1;
902
Radim Krčmářd6321d42017-08-05 00:12:49 +0200903 if (!guest_cpuid_has(vcpu, X86_FEATURE_PKU) && (cr4 & X86_CR4_PKE))
Huaitong Hanb9baba82016-03-22 16:51:21 +0800904 return 1;
905
Yu Zhangfd8cb432017-08-24 20:27:56 +0800906 if (!guest_cpuid_has(vcpu, X86_FEATURE_LA57) && (cr4 & X86_CR4_LA57))
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300907 return 1;
Gleb Natapov0f122442010-04-28 19:15:31 +0300908
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200909 if (!guest_cpuid_has(vcpu, X86_FEATURE_UMIP) && (cr4 & X86_CR4_UMIP))
910 return 1;
911
Carsten Ottea03490e2007-10-29 16:09:35 +0100912 if (is_long_mode(vcpu)) {
913 if (!(cr4 & X86_CR4_PAE))
Avi Kivitya2edf572009-05-24 22:19:00 +0300914 return 1;
915 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800916 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200917 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Carsten Ottea03490e2007-10-29 16:09:35 +0100918 kvm_read_cr3(vcpu)))
919 return 1;
920
Mao, Junjiead756a12012-07-02 01:18:48 +0000921 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200922 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000923 return 1;
924
925 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
926 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
927 return 1;
928 }
929
Gleb Natapov0f122442010-04-28 19:15:31 +0300930 if (kvm_x86_ops->set_cr4(vcpu, cr4))
931 return 1;
932
Mao, Junjiead756a12012-07-02 01:18:48 +0000933 if (((cr4 ^ old_cr4) & pdptr_bits) ||
934 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800935 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300936
Huaitong Hanb9baba82016-03-22 16:51:21 +0800937 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200938 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800939
Gleb Natapov0f122442010-04-28 19:15:31 +0300940 return 0;
941}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200942EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100943
Avi Kivity23902182010-06-10 17:02:16 +0300944int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100945{
Junaid Shahidade61e22018-06-27 14:59:15 -0700946 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100947#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700948 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
949
Junaid Shahidade61e22018-06-27 14:59:15 -0700950 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -0700951 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
952 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -0700953 }
Paolo Bonziniac146232014-11-10 13:53:25 +0100954#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200955
Avi Kivity9f8fe502010-12-05 17:30:00 +0200956 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -0700957 if (!skip_tlb_flush) {
958 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -0700959 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -0700960 }
Gleb Natapov0f122442010-04-28 19:15:31 +0300961 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200962 }
963
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800964 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -0700965 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800966 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200967 else if (is_pae_paging(vcpu) &&
968 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300969 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100970
Junaid Shahidade61e22018-06-27 14:59:15 -0700971 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +0300972 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200973 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Junaid Shahid7c390d32018-06-27 14:59:06 -0700974
Gleb Natapov0f122442010-04-28 19:15:31 +0300975 return 0;
976}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200977EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100978
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100979int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100980{
Gleb Natapov0f122442010-04-28 19:15:31 +0300981 if (cr8 & CR8_RESERVED_BITS)
982 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200983 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100984 kvm_lapic_set_tpr(vcpu, cr8);
985 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800986 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300987 return 0;
988}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200989EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100990
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200991unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100992{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200993 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100994 return kvm_lapic_get_cr8(vcpu);
995 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800996 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100997}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200998EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100999
Nadav Amitae561ed2015-04-02 03:10:37 +03001000static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1001{
1002 int i;
1003
1004 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1005 for (i = 0; i < KVM_NR_DB_REGS; i++)
1006 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1007 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1008 }
1009}
1010
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001011static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1012{
1013 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1014 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1015}
1016
Jan Kiszkac8639012012-09-21 05:42:55 +02001017static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1018{
1019 unsigned long dr7;
1020
1021 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1022 dr7 = vcpu->arch.guest_debug_dr7;
1023 else
1024 dr7 = vcpu->arch.dr7;
1025 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001026 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1027 if (dr7 & DR7_BP_EN_MASK)
1028 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001029}
1030
Nadav Amit6f43ed02014-07-15 17:37:46 +03001031static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1032{
1033 u64 fixed = DR6_FIXED_1;
1034
Radim Krčmářd6321d42017-08-05 00:12:49 +02001035 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001036 fixed |= DR6_RTM;
1037 return fixed;
1038}
1039
Gleb Natapov338dbc92010-04-28 19:15:32 +03001040static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001041{
1042 switch (dr) {
1043 case 0 ... 3:
1044 vcpu->arch.db[dr] = val;
1045 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1046 vcpu->arch.eff_db[dr] = val;
1047 break;
1048 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001049 /* fall through */
1050 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001051 if (val & 0xffffffff00000000ULL)
1052 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001053 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001054 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001055 break;
1056 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001057 /* fall through */
1058 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +03001059 if (val & 0xffffffff00000000ULL)
1060 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001061 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001062 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001063 break;
1064 }
1065
1066 return 0;
1067}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001068
1069int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1070{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001071 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001072 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001073 return 1;
1074 }
1075 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001076}
Gleb Natapov020df072010-04-13 10:05:23 +03001077EXPORT_SYMBOL_GPL(kvm_set_dr);
1078
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001079int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001080{
1081 switch (dr) {
1082 case 0 ... 3:
1083 *val = vcpu->arch.db[dr];
1084 break;
1085 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001086 /* fall through */
1087 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001088 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1089 *val = vcpu->arch.dr6;
1090 else
1091 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001092 break;
1093 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001094 /* fall through */
1095 default: /* 7 */
1096 *val = vcpu->arch.dr7;
1097 break;
1098 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001099 return 0;
1100}
Gleb Natapov020df072010-04-13 10:05:23 +03001101EXPORT_SYMBOL_GPL(kvm_get_dr);
1102
Avi Kivity022cd0e2011-11-10 14:57:23 +02001103bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1104{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001105 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001106 u64 data;
1107 int err;
1108
Wei Huangc6702c92015-06-19 13:44:45 +02001109 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001110 if (err)
1111 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001112 kvm_rax_write(vcpu, (u32)data);
1113 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001114 return err;
1115}
1116EXPORT_SYMBOL_GPL(kvm_rdpmc);
1117
Carsten Otte043405e2007-10-10 17:16:19 +02001118/*
1119 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1120 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1121 *
1122 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001123 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001124 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
1125 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001126 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001127
Carsten Otte043405e2007-10-10 17:16:19 +02001128static u32 msrs_to_save[] = {
1129 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001130 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001131#ifdef CONFIG_X86_64
1132 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1133#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001134 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01001135 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001136 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001137 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1138 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1139 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1140 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1141 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1142 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Carsten Otte043405e2007-10-10 17:16:19 +02001143};
1144
1145static unsigned num_msrs_to_save;
1146
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001147static u32 emulated_msrs[] = {
1148 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1149 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1150 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1151 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001152 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001153 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1154 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001155 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001156 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001157 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001158 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001159 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001160 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001161 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1162 HV_X64_MSR_TSC_EMULATION_STATUS,
1163
1164 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001165 MSR_KVM_PV_EOI_EN,
1166
Will Auldba904632012-11-29 12:42:50 -08001167 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001168 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001169 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001170 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001171 MSR_IA32_MCG_STATUS,
1172 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001173 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001174 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001175 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001176 MSR_PLATFORM_INFO,
1177 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001178 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001179 MSR_IA32_POWER_CTL,
Borislav Petkov191c8132019-04-18 18:32:50 +02001180
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001181 /*
1182 * The following list leaves out MSRs whose values are determined
1183 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1184 * We always support the "true" VMX control MSRs, even if the host
1185 * processor does not, so I am putting these registers here rather
1186 * than in msrs_to_save.
1187 */
1188 MSR_IA32_VMX_BASIC,
1189 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1190 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1191 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1192 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1193 MSR_IA32_VMX_MISC,
1194 MSR_IA32_VMX_CR0_FIXED0,
1195 MSR_IA32_VMX_CR4_FIXED0,
1196 MSR_IA32_VMX_VMCS_ENUM,
1197 MSR_IA32_VMX_PROCBASED_CTLS2,
1198 MSR_IA32_VMX_EPT_VPID_CAP,
1199 MSR_IA32_VMX_VMFUNC,
1200
Borislav Petkov191c8132019-04-18 18:32:50 +02001201 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001202 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001203};
1204
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001205static unsigned num_emulated_msrs;
1206
Tom Lendacky801e4592018-02-21 13:39:51 -06001207/*
1208 * List of msr numbers which are used to expose MSR-based features that
1209 * can be used by a hypervisor to validate requested CPU features.
1210 */
1211static u32 msr_based_features[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001212 MSR_IA32_VMX_BASIC,
1213 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1214 MSR_IA32_VMX_PINBASED_CTLS,
1215 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1216 MSR_IA32_VMX_PROCBASED_CTLS,
1217 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1218 MSR_IA32_VMX_EXIT_CTLS,
1219 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1220 MSR_IA32_VMX_ENTRY_CTLS,
1221 MSR_IA32_VMX_MISC,
1222 MSR_IA32_VMX_CR0_FIXED0,
1223 MSR_IA32_VMX_CR0_FIXED1,
1224 MSR_IA32_VMX_CR4_FIXED0,
1225 MSR_IA32_VMX_CR4_FIXED1,
1226 MSR_IA32_VMX_VMCS_ENUM,
1227 MSR_IA32_VMX_PROCBASED_CTLS2,
1228 MSR_IA32_VMX_EPT_VPID_CAP,
1229 MSR_IA32_VMX_VMFUNC,
1230
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001231 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001232 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001233 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001234};
1235
1236static unsigned int num_msr_based_features;
1237
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001238static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001239{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001240 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001241
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001242 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1243 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001244
1245 /*
1246 * If we're doing cache flushes (either "always" or "cond")
1247 * we will do one whenever the guest does a vmlaunch/vmresume.
1248 * If an outer hypervisor is doing the cache flush for us
1249 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1250 * capability to the guest too, and if EPT is disabled we're not
1251 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1252 * require a nested hypervisor to do a flush of its own.
1253 */
1254 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1255 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1256
Paolo Bonzini0c549142019-08-19 17:24:07 +02001257 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1258 data |= ARCH_CAP_RDCL_NO;
1259 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1260 data |= ARCH_CAP_SSB_NO;
1261 if (!boot_cpu_has_bug(X86_BUG_MDS))
1262 data |= ARCH_CAP_MDS_NO;
1263
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001264 return data;
1265}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001266
Wanpeng Li66421c12018-02-28 14:03:30 +08001267static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1268{
1269 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001270 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001271 msr->data = kvm_get_arch_capabilities();
1272 break;
1273 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001274 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001275 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001276 default:
1277 if (kvm_x86_ops->get_msr_feature(msr))
1278 return 1;
1279 }
1280 return 0;
1281}
1282
Tom Lendacky801e4592018-02-21 13:39:51 -06001283static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1284{
1285 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001286 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001287
1288 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001289 r = kvm_get_msr_feature(&msr);
1290 if (r)
1291 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001292
1293 *data = msr.data;
1294
1295 return 0;
1296}
1297
Sean Christopherson11988492019-04-02 08:19:15 -07001298static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1299{
1300 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1301 return false;
1302
1303 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1304 return false;
1305
Sean Christopherson0a629562019-04-02 08:19:16 -07001306 if (efer & (EFER_LME | EFER_LMA) &&
1307 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1308 return false;
1309
1310 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1311 return false;
1312
Sean Christopherson11988492019-04-02 08:19:15 -07001313 return true;
1314
1315}
Jan Kiszka384bb782013-04-20 10:52:36 +02001316bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001317{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001318 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001319 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001320
Sean Christopherson11988492019-04-02 08:19:15 -07001321 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001322}
1323EXPORT_SYMBOL_GPL(kvm_valid_efer);
1324
Sean Christopherson11988492019-04-02 08:19:15 -07001325static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001326{
1327 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001328 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001329
Sean Christopherson11988492019-04-02 08:19:15 -07001330 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001331 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001332
Sean Christopherson11988492019-04-02 08:19:15 -07001333 if (!msr_info->host_initiated) {
1334 if (!__kvm_valid_efer(vcpu, efer))
1335 return 1;
1336
1337 if (is_paging(vcpu) &&
1338 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1339 return 1;
1340 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001341
Carsten Otte15c4a642007-10-30 18:44:17 +01001342 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001343 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001344
Sheng Yanga3d204e2010-05-12 16:40:40 +08001345 kvm_x86_ops->set_efer(vcpu, efer);
1346
Sheng Yangaad82702010-05-12 16:40:42 +08001347 /* Update reserved bits */
1348 if ((efer ^ old_efer) & EFER_NX)
1349 kvm_mmu_reset_context(vcpu);
1350
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001351 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001352}
1353
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001354void kvm_enable_efer_bits(u64 mask)
1355{
1356 efer_reserved_bits &= ~mask;
1357}
1358EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1359
Carsten Otte15c4a642007-10-30 18:44:17 +01001360/*
1361 * Writes msr value into into the appropriate "register".
1362 * Returns 0 on success, non-0 otherwise.
1363 * Assumes vcpu_load() was already called.
1364 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001365int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01001366{
Nadav Amit854e8bb2014-09-16 03:24:05 +03001367 switch (msr->index) {
1368 case MSR_FS_BASE:
1369 case MSR_GS_BASE:
1370 case MSR_KERNEL_GS_BASE:
1371 case MSR_CSTAR:
1372 case MSR_LSTAR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08001373 if (is_noncanonical_address(msr->data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001374 return 1;
1375 break;
1376 case MSR_IA32_SYSENTER_EIP:
1377 case MSR_IA32_SYSENTER_ESP:
1378 /*
1379 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1380 * non-canonical address is written on Intel but not on
1381 * AMD (which ignores the top 32-bits, because it does
1382 * not implement 64-bit SYSENTER).
1383 *
1384 * 64-bit code should hence be able to write a non-canonical
1385 * value on AMD. Making the address canonical ensures that
1386 * vmentry does not fail on Intel after writing a non-canonical
1387 * value, and that something deterministic happens if the guest
1388 * invokes 64-bit SYSENTER.
1389 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08001390 msr->data = get_canonical(msr->data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001391 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001392 return kvm_x86_ops->set_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001393}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001394EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001395
Carsten Otte313a3dc2007-10-11 19:16:52 +02001396/*
1397 * Adapt set_msr() to msr_io()'s calling convention
1398 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001399static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1400{
1401 struct msr_data msr;
1402 int r;
1403
1404 msr.index = index;
1405 msr.host_initiated = true;
1406 r = kvm_get_msr(vcpu, &msr);
1407 if (r)
1408 return r;
1409
1410 *data = msr.data;
1411 return 0;
1412}
1413
Carsten Otte313a3dc2007-10-11 19:16:52 +02001414static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1415{
Will Auld8fe8ab42012-11-29 12:42:12 -08001416 struct msr_data msr;
1417
1418 msr.data = *data;
1419 msr.index = index;
1420 msr.host_initiated = true;
1421 return kvm_set_msr(vcpu, &msr);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001422}
1423
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001424#ifdef CONFIG_X86_64
1425struct pvclock_gtod_data {
1426 seqcount_t seq;
1427
1428 struct { /* extract of a clocksource struct */
1429 int vclock_mode;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001430 u64 cycle_last;
1431 u64 mask;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001432 u32 mult;
1433 u32 shift;
1434 } clock;
1435
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001436 u64 boot_ns;
1437 u64 nsec_base;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001438 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001439};
1440
1441static struct pvclock_gtod_data pvclock_gtod_data;
1442
1443static void update_pvclock_gtod(struct timekeeper *tk)
1444{
1445 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001446 u64 boot_ns;
1447
Peter Zijlstra876e7882015-03-19 10:09:06 +01001448 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001449
1450 write_seqcount_begin(&vdata->seq);
1451
1452 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001453 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1454 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1455 vdata->clock.mask = tk->tkr_mono.mask;
1456 vdata->clock.mult = tk->tkr_mono.mult;
1457 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001458
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001459 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001460 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001461
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001462 vdata->wall_time_sec = tk->xtime_sec;
1463
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001464 write_seqcount_end(&vdata->seq);
1465}
1466#endif
1467
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001468void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1469{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001470 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001471 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001472}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001473
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001474static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1475{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001476 int version;
1477 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001478 struct pvclock_wall_clock wc;
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001479 struct timespec64 boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001480
1481 if (!wall_clock)
1482 return;
1483
Avi Kivity9ed3c442010-05-04 15:00:37 +03001484 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1485 if (r)
1486 return;
1487
1488 if (version & 1)
1489 ++version; /* first time write, random junk */
1490
1491 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001492
Nicholas Krause1dab1342015-12-30 13:08:46 -05001493 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1494 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001495
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001496 /*
1497 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001498 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001499 * wall clock specified here. guest system time equals host
1500 * system time for us, thus we must fill in host boot time here.
1501 */
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001502 getboottime64(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001503
Bruce Rogers4b648662012-07-20 10:44:24 -06001504 if (kvm->arch.kvmclock_offset) {
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001505 struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
1506 boot = timespec64_sub(boot, ts);
Bruce Rogers4b648662012-07-20 10:44:24 -06001507 }
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001508 wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001509 wc.nsec = boot.tv_nsec;
1510 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001511
1512 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1513
1514 version++;
1515 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001516}
1517
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001518static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1519{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001520 do_shl32_div32(dividend, divisor);
1521 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001522}
1523
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001524static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001525 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001526{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001527 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001528 int32_t shift = 0;
1529 uint64_t tps64;
1530 uint32_t tps32;
1531
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001532 tps64 = base_hz;
1533 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001534 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001535 tps64 >>= 1;
1536 shift--;
1537 }
1538
1539 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001540 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1541 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001542 scaled64 >>= 1;
1543 else
1544 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001545 shift++;
1546 }
1547
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001548 *pshift = shift;
1549 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001550}
1551
Marcelo Tosattid8281992012-11-27 23:29:01 -02001552#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001553static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001554#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001555
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001556static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001557static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001558
Zachary Amsdencc578282012-02-03 15:43:50 -02001559static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001560{
Zachary Amsdencc578282012-02-03 15:43:50 -02001561 u64 v = (u64)khz * (1000000 + ppm);
1562 do_div(v, 1000000);
1563 return v;
1564}
1565
Haozhong Zhang381d5852015-10-20 15:39:04 +08001566static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1567{
1568 u64 ratio;
1569
1570 /* Guest TSC same frequency as host TSC? */
1571 if (!scale) {
1572 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1573 return 0;
1574 }
1575
1576 /* TSC scaling supported? */
1577 if (!kvm_has_tsc_control) {
1578 if (user_tsc_khz > tsc_khz) {
1579 vcpu->arch.tsc_catchup = 1;
1580 vcpu->arch.tsc_always_catchup = 1;
1581 return 0;
1582 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001583 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001584 return -1;
1585 }
1586 }
1587
1588 /* TSC scaling required - calculate ratio */
1589 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1590 user_tsc_khz, tsc_khz);
1591
1592 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001593 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1594 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001595 return -1;
1596 }
1597
1598 vcpu->arch.tsc_scaling_ratio = ratio;
1599 return 0;
1600}
1601
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001602static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001603{
1604 u32 thresh_lo, thresh_hi;
1605 int use_scaling = 0;
1606
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001607 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001608 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001609 /* set tsc_scaling_ratio to a safe value */
1610 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001611 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001612 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001613
Zachary Amsdenc2855452010-09-18 14:38:15 -10001614 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001615 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001616 &vcpu->arch.virtual_tsc_shift,
1617 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001618 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001619
1620 /*
1621 * Compute the variation in TSC rate which is acceptable
1622 * within the range of tolerance and decide if the
1623 * rate being applied is within that bounds of the hardware
1624 * rate. If so, no scaling or compensation need be done.
1625 */
1626 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1627 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001628 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1629 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 -02001630 use_scaling = 1;
1631 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001632 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001633}
1634
1635static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1636{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001637 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001638 vcpu->arch.virtual_tsc_mult,
1639 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001640 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001641 return tsc;
1642}
1643
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001644static inline int gtod_is_based_on_tsc(int mode)
1645{
1646 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1647}
1648
Fengguang Wu69b00492015-01-19 22:33:39 +08001649static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001650{
1651#ifdef CONFIG_X86_64
1652 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001653 struct kvm_arch *ka = &vcpu->kvm->arch;
1654 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1655
1656 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1657 atomic_read(&vcpu->kvm->online_vcpus));
1658
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001659 /*
1660 * Once the masterclock is enabled, always perform request in
1661 * order to update it.
1662 *
1663 * In order to enable masterclock, the host clocksource must be TSC
1664 * and the vcpus need to have matched TSCs. When that happens,
1665 * perform request to enable masterclock.
1666 */
1667 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001668 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001669 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1670
1671 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1672 atomic_read(&vcpu->kvm->online_vcpus),
1673 ka->use_master_clock, gtod->clock.vclock_mode);
1674#endif
1675}
1676
Will Auldba904632012-11-29 12:42:50 -08001677static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1678{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001679 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001680 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1681}
1682
Haozhong Zhang35181e82015-10-20 15:39:03 +08001683/*
1684 * Multiply tsc by a fixed point number represented by ratio.
1685 *
1686 * The most significant 64-N bits (mult) of ratio represent the
1687 * integral part of the fixed point number; the remaining N bits
1688 * (frac) represent the fractional part, ie. ratio represents a fixed
1689 * point number (mult + frac * 2^(-N)).
1690 *
1691 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1692 */
1693static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1694{
1695 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1696}
1697
1698u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1699{
1700 u64 _tsc = tsc;
1701 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1702
1703 if (ratio != kvm_default_tsc_scaling_ratio)
1704 _tsc = __scale_tsc(ratio, tsc);
1705
1706 return _tsc;
1707}
1708EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1709
Haozhong Zhang07c14192015-10-20 15:39:05 +08001710static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1711{
1712 u64 tsc;
1713
1714 tsc = kvm_scale_tsc(vcpu, rdtsc());
1715
1716 return target_tsc - tsc;
1717}
1718
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001719u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1720{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001721 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1722
1723 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001724}
1725EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1726
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001727static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1728{
Leonid Shatz326e7422018-11-06 12:14:25 +02001729 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001730}
1731
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001732static inline bool kvm_check_tsc_unstable(void)
1733{
1734#ifdef CONFIG_X86_64
1735 /*
1736 * TSC is marked unstable when we're running on Hyper-V,
1737 * 'TSC page' clocksource is good.
1738 */
1739 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1740 return false;
1741#endif
1742 return check_tsc_unstable();
1743}
1744
Will Auld8fe8ab42012-11-29 12:42:12 -08001745void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001746{
1747 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001748 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001749 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001750 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001751 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001752 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001753 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001754
Jan Kiszka038f8c12011-02-04 10:49:11 +01001755 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001756 offset = kvm_compute_tsc_offset(vcpu, data);
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02001757 ns = ktime_get_boottime_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001758 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001759
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001760 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001761 if (data == 0 && msr->host_initiated) {
1762 /*
1763 * detection of vcpu initialization -- need to sync
1764 * with other vCPUs. This particularly helps to keep
1765 * kvm_clock stable after CPU hotplug
1766 */
1767 synchronizing = true;
1768 } else {
1769 u64 tsc_exp = kvm->arch.last_tsc_write +
1770 nsec_to_cycles(vcpu, elapsed);
1771 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1772 /*
1773 * Special case: TSC write with a small delta (1 second)
1774 * of virtual cycle time against real time is
1775 * interpreted as an attempt to synchronize the CPU.
1776 */
1777 synchronizing = data < tsc_exp + tsc_hz &&
1778 data + tsc_hz > tsc_exp;
1779 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001780 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001781
1782 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001783 * For a reliable TSC, we can match TSC offsets, and for an unstable
1784 * TSC, we add elapsed time in this computation. We could let the
1785 * compensation code attempt to catch up if we fall behind, but
1786 * it's better to try to match offsets from the beginning.
1787 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001788 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001789 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001790 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001791 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001792 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001793 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001794 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08001795 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001796 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001797 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001798 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001799 } else {
1800 /*
1801 * We split periods of matched TSC writes into generations.
1802 * For each generation, we track the original measured
1803 * nanosecond time, offset, and write, so if TSCs are in
1804 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001805 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001806 *
1807 * These values are tracked in kvm->arch.cur_xxx variables.
1808 */
1809 kvm->arch.cur_tsc_generation++;
1810 kvm->arch.cur_tsc_nsec = ns;
1811 kvm->arch.cur_tsc_write = data;
1812 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001813 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001814 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001815
1816 /*
1817 * We also track th most recent recorded KHZ, write and time to
1818 * allow the matching interval to be extended at each write.
1819 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001820 kvm->arch.last_tsc_nsec = ns;
1821 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001822 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001823
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001824 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001825
1826 /* Keep track of which generation this VCPU has synchronized to */
1827 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1828 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1829 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1830
Radim Krčmářd6321d42017-08-05 00:12:49 +02001831 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08001832 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02001833
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001834 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001835 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001836
1837 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001838 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001839 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001840 } else if (!already_matched) {
1841 kvm->arch.nr_vcpus_matched_tsc++;
1842 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001843
1844 kvm_track_tsc_matching(vcpu);
1845 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001846}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001847
Zachary Amsden99e3e302010-08-19 22:07:17 -10001848EXPORT_SYMBOL_GPL(kvm_write_tsc);
1849
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001850static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1851 s64 adjustment)
1852{
Leonid Shatz326e7422018-11-06 12:14:25 +02001853 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1854 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001855}
1856
1857static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1858{
1859 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1860 WARN_ON(adjustment < 0);
1861 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01001862 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001863}
1864
Marcelo Tosattid8281992012-11-27 23:29:01 -02001865#ifdef CONFIG_X86_64
1866
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001867static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001868{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001869 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02001870 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001871
1872 if (likely(ret >= last))
1873 return ret;
1874
1875 /*
1876 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001877 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02001878 * very likely) and there's a data dependence, so force GCC
1879 * to generate a branch instead. I don't barrier() because
1880 * we don't actually need a barrier, and if this function
1881 * ever gets inlined it will generate worse code.
1882 */
1883 asm volatile ("");
1884 return last;
1885}
1886
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001887static inline u64 vgettsc(u64 *tsc_timestamp, int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001888{
1889 long v;
1890 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001891 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001892
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001893 switch (gtod->clock.vclock_mode) {
1894 case VCLOCK_HVCLOCK:
1895 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
1896 tsc_timestamp);
1897 if (tsc_pg_val != U64_MAX) {
1898 /* TSC page valid */
1899 *mode = VCLOCK_HVCLOCK;
1900 v = (tsc_pg_val - gtod->clock.cycle_last) &
1901 gtod->clock.mask;
1902 } else {
1903 /* TSC page invalid */
1904 *mode = VCLOCK_NONE;
1905 }
1906 break;
1907 case VCLOCK_TSC:
1908 *mode = VCLOCK_TSC;
1909 *tsc_timestamp = read_tsc();
1910 v = (*tsc_timestamp - gtod->clock.cycle_last) &
1911 gtod->clock.mask;
1912 break;
1913 default:
1914 *mode = VCLOCK_NONE;
1915 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02001916
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001917 if (*mode == VCLOCK_NONE)
1918 *tsc_timestamp = v = 0;
1919
Marcelo Tosattid8281992012-11-27 23:29:01 -02001920 return v * gtod->clock.mult;
1921}
1922
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001923static int do_monotonic_boot(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001924{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001925 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001926 unsigned long seq;
1927 int mode;
1928 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001929
Marcelo Tosattid8281992012-11-27 23:29:01 -02001930 do {
1931 seq = read_seqcount_begin(&gtod->seq);
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001932 ns = gtod->nsec_base;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001933 ns += vgettsc(tsc_timestamp, &mode);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001934 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001935 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001936 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001937 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001938
1939 return mode;
1940}
1941
Arnd Bergmann899a31f2018-04-23 10:04:26 +02001942static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001943{
1944 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1945 unsigned long seq;
1946 int mode;
1947 u64 ns;
1948
1949 do {
1950 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001951 ts->tv_sec = gtod->wall_time_sec;
1952 ns = gtod->nsec_base;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001953 ns += vgettsc(tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001954 ns >>= gtod->clock.shift;
1955 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
1956
1957 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
1958 ts->tv_nsec = ns;
1959
1960 return mode;
1961}
1962
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001963/* returns true if host is using TSC based clocksource */
1964static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001965{
Marcelo Tosattid8281992012-11-27 23:29:01 -02001966 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001967 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02001968 return false;
1969
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001970 return gtod_is_based_on_tsc(do_monotonic_boot(kernel_ns,
1971 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02001972}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001973
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001974/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02001975static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001976 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001977{
1978 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001979 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001980 return false;
1981
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001982 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001983}
Marcelo Tosattid8281992012-11-27 23:29:01 -02001984#endif
1985
1986/*
1987 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001988 * Assuming a stable TSC across physical CPUS, and a stable TSC
1989 * across virtual CPUs, the following condition is possible.
1990 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02001991 * CPUs at the next numbered event.
1992 *
1993 * "timespecX" represents host monotonic time. "tscX" represents
1994 * RDTSC value.
1995 *
1996 * VCPU0 on CPU0 | VCPU1 on CPU1
1997 *
1998 * 1. read timespec0,tsc0
1999 * 2. | timespec1 = timespec0 + N
2000 * | tsc1 = tsc0 + M
2001 * 3. transition to guest | transition to guest
2002 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2003 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2004 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2005 *
2006 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2007 *
2008 * - ret0 < ret1
2009 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2010 * ...
2011 * - 0 < N - M => M < N
2012 *
2013 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2014 * always the case (the difference between two distinct xtime instances
2015 * might be smaller then the difference between corresponding TSC reads,
2016 * when updating guest vcpus pvclock areas).
2017 *
2018 * To avoid that problem, do not allow visibility of distinct
2019 * system_timestamp/tsc_timestamp values simultaneously: use a master
2020 * copy of host monotonic time values. Update that master copy
2021 * in lockstep.
2022 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002023 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002024 *
2025 */
2026
2027static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2028{
2029#ifdef CONFIG_X86_64
2030 struct kvm_arch *ka = &kvm->arch;
2031 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002032 bool host_tsc_clocksource, vcpus_matched;
2033
2034 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2035 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002036
2037 /*
2038 * If the host uses TSC clock, then passthrough TSC as stable
2039 * to the guest.
2040 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002041 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002042 &ka->master_kernel_ns,
2043 &ka->master_cycle_now);
2044
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002045 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002046 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002047 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002048
Marcelo Tosattid8281992012-11-27 23:29:01 -02002049 if (ka->use_master_clock)
2050 atomic_set(&kvm_guest_has_master_clock, 1);
2051
2052 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002053 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2054 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002055#endif
2056}
2057
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002058void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2059{
2060 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2061}
2062
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002063static void kvm_gen_update_masterclock(struct kvm *kvm)
2064{
2065#ifdef CONFIG_X86_64
2066 int i;
2067 struct kvm_vcpu *vcpu;
2068 struct kvm_arch *ka = &kvm->arch;
2069
2070 spin_lock(&ka->pvclock_gtod_sync_lock);
2071 kvm_make_mclock_inprogress_request(kvm);
2072 /* no guest entries from this point */
2073 pvclock_update_vm_gtod_copy(kvm);
2074
2075 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002076 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002077
2078 /* guest entries allowed */
2079 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002080 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002081
2082 spin_unlock(&ka->pvclock_gtod_sync_lock);
2083#endif
2084}
2085
Marcelo Tosattie891a322017-04-17 12:51:37 -03002086u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002087{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002088 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002089 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002090 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002091
Paolo Bonzini8b953442016-11-16 18:31:30 +01002092 spin_lock(&ka->pvclock_gtod_sync_lock);
2093 if (!ka->use_master_clock) {
2094 spin_unlock(&ka->pvclock_gtod_sync_lock);
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002095 return ktime_get_boottime_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002096 }
2097
Paolo Bonzini8b953442016-11-16 18:31:30 +01002098 hv_clock.tsc_timestamp = ka->master_cycle_now;
2099 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2100 spin_unlock(&ka->pvclock_gtod_sync_lock);
2101
Wanpeng Lie2c22062017-05-11 18:12:05 -07002102 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2103 get_cpu();
2104
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002105 if (__this_cpu_read(cpu_tsc_khz)) {
2106 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2107 &hv_clock.tsc_shift,
2108 &hv_clock.tsc_to_system_mul);
2109 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2110 } else
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002111 ret = ktime_get_boottime_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002112
2113 put_cpu();
2114
2115 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002116}
2117
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002118static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2119{
2120 struct kvm_vcpu_arch *vcpu = &v->arch;
2121 struct pvclock_vcpu_time_info guest_hv_clock;
2122
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002123 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002124 &guest_hv_clock, sizeof(guest_hv_clock))))
2125 return;
2126
2127 /* This VCPU is paused, but it's legal for a guest to read another
2128 * VCPU's kvmclock, so we really have to follow the specification where
2129 * it says that version is odd if data is being modified, and even after
2130 * it is consistent.
2131 *
2132 * Version field updates must be kept separate. This is because
2133 * kvm_write_guest_cached might use a "rep movs" instruction, and
2134 * writes within a string instruction are weakly ordered. So there
2135 * are three writes overall.
2136 *
2137 * As a small optimization, only write the version field in the first
2138 * and third write. The vcpu->pv_time cache is still valid, because the
2139 * version field is the first in the struct.
2140 */
2141 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2142
Liran Alon51c4b8b2017-11-05 16:11:30 +02002143 if (guest_hv_clock.version & 1)
2144 ++guest_hv_clock.version; /* first time write, random junk */
2145
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002146 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002147 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2148 &vcpu->hv_clock,
2149 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002150
2151 smp_wmb();
2152
2153 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2154 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2155
2156 if (vcpu->pvclock_set_guest_stopped_request) {
2157 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2158 vcpu->pvclock_set_guest_stopped_request = false;
2159 }
2160
2161 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2162
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002163 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2164 &vcpu->hv_clock,
2165 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002166
2167 smp_wmb();
2168
2169 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002170 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2171 &vcpu->hv_clock,
2172 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002173}
2174
Zachary Amsden34c238a2010-09-18 14:38:14 -10002175static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002176{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002177 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002178 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002179 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002180 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002181 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002182 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002183 bool use_master_clock;
2184
2185 kernel_ns = 0;
2186 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002187
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002188 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002189 * If the host uses TSC clock, then passthrough TSC as stable
2190 * to the guest.
2191 */
2192 spin_lock(&ka->pvclock_gtod_sync_lock);
2193 use_master_clock = ka->use_master_clock;
2194 if (use_master_clock) {
2195 host_tsc = ka->master_cycle_now;
2196 kernel_ns = ka->master_kernel_ns;
2197 }
2198 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002199
2200 /* Keep irq disabled to prevent changes to the clock */
2201 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002202 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2203 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002204 local_irq_restore(flags);
2205 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2206 return 1;
2207 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002208 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002209 host_tsc = rdtsc();
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002210 kernel_ns = ktime_get_boottime_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002211 }
2212
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002213 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002214
2215 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002216 * We may have to catch up the TSC to match elapsed wall clock
2217 * time for two reasons, even if kvmclock is used.
2218 * 1) CPU could have been running below the maximum TSC rate
2219 * 2) Broken TSC compensation resets the base at each VCPU
2220 * entry to avoid unknown leaps of TSC even when running
2221 * again on the same CPU. This may cause apparent elapsed
2222 * time to disappear, and the guest to stand still or run
2223 * very slowly.
2224 */
2225 if (vcpu->tsc_catchup) {
2226 u64 tsc = compute_guest_tsc(v, kernel_ns);
2227 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002228 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002229 tsc_timestamp = tsc;
2230 }
2231 }
2232
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002233 local_irq_restore(flags);
2234
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002235 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002236
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002237 if (kvm_has_tsc_control)
2238 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2239
2240 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002241 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002242 &vcpu->hv_clock.tsc_shift,
2243 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002244 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002245 }
2246
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002247 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002248 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002249 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002250
Marcelo Tosattid8281992012-11-27 23:29:01 -02002251 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002252 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002253 if (use_master_clock)
2254 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2255
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002256 vcpu->hv_clock.flags = pvclock_flags;
2257
Paolo Bonzini095cf552016-02-08 12:54:12 +01002258 if (vcpu->pv_time_enabled)
2259 kvm_setup_pvclock_page(v);
2260 if (v == kvm_get_vcpu(v->kvm, 0))
2261 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002262 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002263}
2264
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002265/*
2266 * kvmclock updates which are isolated to a given vcpu, such as
2267 * vcpu->cpu migration, should not allow system_timestamp from
2268 * the rest of the vcpus to remain static. Otherwise ntp frequency
2269 * correction applies to one vcpu's system_timestamp but not
2270 * the others.
2271 *
2272 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002273 * We need to rate-limit these requests though, as they can
2274 * considerably slow guests that have a large number of vcpus.
2275 * The time for a remote vcpu to update its kvmclock is bound
2276 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002277 */
2278
Andrew Jones7e44e442014-02-28 12:52:54 +01002279#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2280
2281static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002282{
2283 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002284 struct delayed_work *dwork = to_delayed_work(work);
2285 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2286 kvmclock_update_work);
2287 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002288 struct kvm_vcpu *vcpu;
2289
2290 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002291 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002292 kvm_vcpu_kick(vcpu);
2293 }
2294}
2295
Andrew Jones7e44e442014-02-28 12:52:54 +01002296static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2297{
2298 struct kvm *kvm = v->kvm;
2299
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002300 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002301 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2302 KVMCLOCK_UPDATE_DELAY);
2303}
2304
Andrew Jones332967a2014-02-28 12:52:55 +01002305#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2306
2307static void kvmclock_sync_fn(struct work_struct *work)
2308{
2309 struct delayed_work *dwork = to_delayed_work(work);
2310 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2311 kvmclock_sync_work);
2312 struct kvm *kvm = container_of(ka, struct kvm, arch);
2313
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002314 if (!kvmclock_periodic_sync)
2315 return;
2316
Andrew Jones332967a2014-02-28 12:52:55 +01002317 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2318 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2319 KVMCLOCK_SYNC_PERIOD);
2320}
2321
Borislav Petkov191c8132019-04-18 18:32:50 +02002322/*
2323 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2324 */
2325static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2326{
2327 /* McStatusWrEn enabled? */
2328 if (guest_cpuid_is_amd(vcpu))
2329 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2330
2331 return false;
2332}
2333
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002334static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002335{
2336 u64 mcg_cap = vcpu->arch.mcg_cap;
2337 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002338 u32 msr = msr_info->index;
2339 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002340
2341 switch (msr) {
2342 case MSR_IA32_MCG_STATUS:
2343 vcpu->arch.mcg_status = data;
2344 break;
2345 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002346 if (!(mcg_cap & MCG_CTL_P) &&
2347 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002348 return 1;
2349 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002350 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002351 vcpu->arch.mcg_ctl = data;
2352 break;
2353 default:
2354 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002355 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002356 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01002357 /* only 0 or all 1s can be written to IA32_MCi_CTL
2358 * some Linux kernels though clear bit 10 in bank 4 to
2359 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2360 * this to avoid an uncatched #GP in the guest
2361 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002362 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002363 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002364 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002365
2366 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002367 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002368 (offset & 0x3) == 1 && data != 0) {
2369 if (!can_set_mci_status(vcpu))
2370 return -1;
2371 }
2372
Huang Ying890ca9a2009-05-11 16:48:15 +08002373 vcpu->arch.mce_banks[offset] = data;
2374 break;
2375 }
2376 return 1;
2377 }
2378 return 0;
2379}
2380
Ed Swierkffde22a2009-10-15 15:21:43 -07002381static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2382{
2383 struct kvm *kvm = vcpu->kvm;
2384 int lm = is_long_mode(vcpu);
2385 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2386 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2387 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2388 : kvm->arch.xen_hvm_config.blob_size_32;
2389 u32 page_num = data & ~PAGE_MASK;
2390 u64 page_addr = data & PAGE_MASK;
2391 u8 *page;
2392 int r;
2393
2394 r = -E2BIG;
2395 if (page_num >= blob_size)
2396 goto out;
2397 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002398 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2399 if (IS_ERR(page)) {
2400 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002401 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002402 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002403 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002404 goto out_free;
2405 r = 0;
2406out_free:
2407 kfree(page);
2408out:
2409 return r;
2410}
2411
Gleb Natapov344d9582010-10-14 11:22:50 +02002412static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2413{
2414 gpa_t gpa = data & ~0x3f;
2415
Wanpeng Li52a5c152017-07-13 18:30:42 -07002416 /* Bits 3:5 are reserved, Should be zero */
2417 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002418 return 1;
2419
2420 vcpu->arch.apf.msr_val = data;
2421
2422 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2423 kvm_clear_async_pf_completion_queue(vcpu);
2424 kvm_async_pf_hash_reset(vcpu);
2425 return 0;
2426 }
2427
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002428 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002429 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002430 return 1;
2431
Gleb Natapov6adba522010-10-14 11:22:55 +02002432 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002433 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002434 kvm_async_pf_wakeup_all(vcpu);
2435 return 0;
2436}
2437
Glauber Costa12f9a482011-02-01 14:16:40 -05002438static void kvmclock_reset(struct kvm_vcpu *vcpu)
2439{
Andy Honig0b794592013-02-20 14:48:10 -08002440 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05002441}
2442
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002443static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2444{
2445 ++vcpu->stat.tlb_flush;
2446 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2447}
2448
Glauber Costac9aaa892011-07-11 15:28:14 -04002449static void record_steal_time(struct kvm_vcpu *vcpu)
2450{
2451 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2452 return;
2453
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002454 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002455 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2456 return;
2457
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002458 /*
2459 * Doing a TLB flush here, on the guest's behalf, can avoid
2460 * expensive IPIs.
2461 */
2462 if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB)
2463 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002464
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002465 if (vcpu->arch.st.steal.version & 1)
2466 vcpu->arch.st.steal.version += 1; /* first time write, random junk */
2467
2468 vcpu->arch.st.steal.version += 1;
2469
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002470 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002471 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2472
2473 smp_wmb();
2474
Liang Chenc54cdf12016-03-16 19:33:16 +08002475 vcpu->arch.st.steal.steal += current->sched_info.run_delay -
2476 vcpu->arch.st.last_steal;
2477 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002478
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002479 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002480 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2481
2482 smp_wmb();
2483
2484 vcpu->arch.st.steal.version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002485
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002486 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002487 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2488}
2489
Will Auld8fe8ab42012-11-29 12:42:12 -08002490int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002491{
Gleb Natapov57537852012-01-15 14:17:22 +02002492 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002493 u32 msr = msr_info->index;
2494 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002495
Carsten Otte15c4a642007-10-30 18:44:17 +01002496 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002497 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002498 case MSR_IA32_UCODE_WRITE:
2499 case MSR_VM_HSAVE_PA:
2500 case MSR_AMD64_PATCH_LOADER:
2501 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002502 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002503 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002504 break;
2505
Wanpeng Li518e7b92018-02-28 14:03:31 +08002506 case MSR_IA32_UCODE_REV:
2507 if (msr_info->host_initiated)
2508 vcpu->arch.microcode_version = data;
2509 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002510 case MSR_IA32_ARCH_CAPABILITIES:
2511 if (!msr_info->host_initiated)
2512 return 1;
2513 vcpu->arch.arch_capabilities = data;
2514 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002515 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002516 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002517 case MSR_K7_HWCR:
2518 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002519 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002520 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002521
2522 /* Handle McStatusWrEn */
2523 if (data == BIT_ULL(18)) {
2524 vcpu->arch.msr_hwcr = data;
2525 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002526 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2527 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002528 return 1;
2529 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002530 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002531 case MSR_FAM10H_MMIO_CONF_BASE:
2532 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002533 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2534 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002535 return 1;
2536 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002537 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002538 case MSR_IA32_DEBUGCTLMSR:
2539 if (!data) {
2540 /* We support the non-activated case already */
2541 break;
2542 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2543 /* Values other than LBR and BTF are vendor-specific,
2544 thus reserved and should throw a #GP */
2545 return 1;
2546 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002547 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2548 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002549 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002550 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002551 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002552 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002553 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002554 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2555 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002556 case MSR_IA32_TSCDEADLINE:
2557 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2558 break;
Will Auldba904632012-11-29 12:42:50 -08002559 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002560 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002561 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002562 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002563 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002564 }
2565 vcpu->arch.ia32_tsc_adjust_msr = data;
2566 }
2567 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002568 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002569 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2570 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2571 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2572 return 1;
2573 vcpu->arch.ia32_misc_enable_msr = data;
2574 kvm_update_cpuid(vcpu);
2575 } else {
2576 vcpu->arch.ia32_misc_enable_msr = data;
2577 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002578 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002579 case MSR_IA32_SMBASE:
2580 if (!msr_info->host_initiated)
2581 return 1;
2582 vcpu->arch.smbase = data;
2583 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002584 case MSR_IA32_POWER_CTL:
2585 vcpu->arch.msr_ia32_power_ctl = data;
2586 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002587 case MSR_IA32_TSC:
2588 kvm_write_tsc(vcpu, msr_info);
2589 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002590 case MSR_SMI_COUNT:
2591 if (!msr_info->host_initiated)
2592 return 1;
2593 vcpu->arch.smi_count = data;
2594 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002595 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002596 case MSR_KVM_WALL_CLOCK:
2597 vcpu->kvm->arch.wall_clock = data;
2598 kvm_write_wall_clock(vcpu->kvm, data);
2599 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002600 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002601 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002602 struct kvm_arch *ka = &vcpu->kvm->arch;
2603
Glauber Costa12f9a482011-02-01 14:16:40 -05002604 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002605
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002606 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2607 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2608
2609 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002610 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002611
2612 ka->boot_vcpu_runs_old_kvmclock = tmp;
2613 }
2614
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002615 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002616 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002617
2618 /* we verify if the enable bit is set... */
2619 if (!(data & 1))
2620 break;
2621
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002622 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002623 &vcpu->arch.pv_time, data & ~1ULL,
2624 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002625 vcpu->arch.pv_time_enabled = false;
2626 else
2627 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002628
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002629 break;
2630 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002631 case MSR_KVM_ASYNC_PF_EN:
2632 if (kvm_pv_enable_async_pf(vcpu, data))
2633 return 1;
2634 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002635 case MSR_KVM_STEAL_TIME:
2636
2637 if (unlikely(!sched_info_on()))
2638 return 1;
2639
2640 if (data & KVM_STEAL_RESERVED_MASK)
2641 return 1;
2642
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002643 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002644 data & KVM_STEAL_VALID_BITS,
2645 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002646 return 1;
2647
2648 vcpu->arch.st.msr_val = data;
2649
2650 if (!(data & KVM_MSR_ENABLED))
2651 break;
2652
Glauber Costac9aaa892011-07-11 15:28:14 -04002653 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2654
2655 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002656 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002657 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002658 return 1;
2659 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002660
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002661 case MSR_KVM_POLL_CONTROL:
2662 /* only enable bit supported */
2663 if (data & (-1ULL << 1))
2664 return 1;
2665
2666 vcpu->arch.msr_kvm_poll_control = data;
2667 break;
2668
Huang Ying890ca9a2009-05-11 16:48:15 +08002669 case MSR_IA32_MCG_CTL:
2670 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002671 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002672 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002673
Wei Huang6912ac32015-06-12 01:34:56 -04002674 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2675 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2676 pr = true; /* fall through */
2677 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2678 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002679 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002680 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002681
2682 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002683 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2684 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002685 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002686 case MSR_K7_CLK_CTL:
2687 /*
2688 * Ignore all writes to this no longer documented MSR.
2689 * Writes are only relevant for old K7 processors,
2690 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002691 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002692 * affected processor models on the command line, hence
2693 * the need to ignore the workaround.
2694 */
2695 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002696 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002697 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2698 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002699 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002700 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2701 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2702 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002703 return kvm_hv_set_msr_common(vcpu, msr, data,
2704 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002705 case MSR_IA32_BBL_CR_CTL3:
2706 /* Drop writes to this legacy MSR -- see rdmsr
2707 * counterpart for further detail.
2708 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002709 if (report_ignored_msrs)
2710 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2711 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002712 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002713 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002714 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002715 return 1;
2716 vcpu->arch.osvw.length = data;
2717 break;
2718 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002719 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002720 return 1;
2721 vcpu->arch.osvw.status = data;
2722 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002723 case MSR_PLATFORM_INFO:
2724 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002725 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2726 cpuid_fault_enabled(vcpu)))
2727 return 1;
2728 vcpu->arch.msr_platform_info = data;
2729 break;
2730 case MSR_MISC_FEATURES_ENABLES:
2731 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2732 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2733 !supports_cpuid_fault(vcpu)))
2734 return 1;
2735 vcpu->arch.msr_misc_features_enables = data;
2736 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002737 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002738 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2739 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002740 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002741 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002742 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002743 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002744 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002745 return 1;
2746 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002747 if (report_ignored_msrs)
2748 vcpu_unimpl(vcpu,
2749 "ignored wrmsr: 0x%x data 0x%llx\n",
2750 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002751 break;
2752 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002753 }
2754 return 0;
2755}
2756EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2757
2758
2759/*
2760 * Reads an msr value (of 'msr_index') into 'pdata'.
2761 * Returns 0 on success, non-0 otherwise.
2762 * Assumes vcpu_load() was already called.
2763 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002764int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Carsten Otte15c4a642007-10-30 18:44:17 +01002765{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002766 return kvm_x86_ops->get_msr(vcpu, msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002767}
Wincy Vanff651cb2014-12-11 08:52:58 +03002768EXPORT_SYMBOL_GPL(kvm_get_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01002769
Paolo Bonzini44883f02018-07-26 13:01:52 +02002770static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002771{
2772 u64 data;
2773 u64 mcg_cap = vcpu->arch.mcg_cap;
2774 unsigned bank_num = mcg_cap & 0xff;
2775
2776 switch (msr) {
2777 case MSR_IA32_P5_MC_ADDR:
2778 case MSR_IA32_P5_MC_TYPE:
2779 data = 0;
2780 break;
2781 case MSR_IA32_MCG_CAP:
2782 data = vcpu->arch.mcg_cap;
2783 break;
2784 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002785 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002786 return 1;
2787 data = vcpu->arch.mcg_ctl;
2788 break;
2789 case MSR_IA32_MCG_STATUS:
2790 data = vcpu->arch.mcg_status;
2791 break;
2792 default:
2793 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002794 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002795 u32 offset = msr - MSR_IA32_MC0_CTL;
2796 data = vcpu->arch.mce_banks[offset];
2797 break;
2798 }
2799 return 1;
2800 }
2801 *pdata = data;
2802 return 0;
2803}
2804
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002805int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002806{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002807 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002808 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002809 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002810 case MSR_IA32_DEBUGCTLMSR:
2811 case MSR_IA32_LASTBRANCHFROMIP:
2812 case MSR_IA32_LASTBRANCHTOIP:
2813 case MSR_IA32_LASTINTFROMIP:
2814 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302815 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002816 case MSR_K8_TSEG_ADDR:
2817 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002818 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002819 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002820 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002821 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002822 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03002823 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02002824 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002825 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002826 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002827 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06002828 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04002829 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2830 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2831 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2832 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002833 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002834 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2835 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002836 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002837 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08002838 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002839 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002840 case MSR_IA32_ARCH_CAPABILITIES:
2841 if (!msr_info->host_initiated &&
2842 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
2843 return 1;
2844 msr_info->data = vcpu->arch.arch_capabilities;
2845 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002846 case MSR_IA32_POWER_CTL:
2847 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
2848 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002849 case MSR_IA32_TSC:
2850 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
2851 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002852 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002853 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002854 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002855 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002856 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002857 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002858 /*
2859 * MSR_EBC_FREQUENCY_ID
2860 * Conservative value valid for even the basic CPU models.
2861 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2862 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2863 * and 266MHz for model 3, or 4. Set Core Clock
2864 * Frequency to System Bus Frequency Ratio to 1 (bits
2865 * 31:24) even though these are only valid for CPU
2866 * models > 2, however guests may end up dividing or
2867 * multiplying by zero otherwise.
2868 */
2869 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002870 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002871 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002872 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002873 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002874 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002875 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002876 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002877 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002878 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002879 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002880 break;
Will Auldba904632012-11-29 12:42:50 -08002881 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002882 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002883 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002884 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002885 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002886 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002887 case MSR_IA32_SMBASE:
2888 if (!msr_info->host_initiated)
2889 return 1;
2890 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002891 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002892 case MSR_SMI_COUNT:
2893 msr_info->data = vcpu->arch.smi_count;
2894 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002895 case MSR_IA32_PERF_STATUS:
2896 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002897 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002898 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002899 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002900 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002901 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002902 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002903 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002904 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002905 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002906 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002907 break;
2908 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002909 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002910 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002911 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02002912 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002913 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02002914 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002915 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002916 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04002917 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002918 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002919 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03002920 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002921 case MSR_KVM_POLL_CONTROL:
2922 msr_info->data = vcpu->arch.msr_kvm_poll_control;
2923 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08002924 case MSR_IA32_P5_MC_ADDR:
2925 case MSR_IA32_P5_MC_TYPE:
2926 case MSR_IA32_MCG_CAP:
2927 case MSR_IA32_MCG_CTL:
2928 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002929 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002930 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
2931 msr_info->host_initiated);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002932 case MSR_K7_CLK_CTL:
2933 /*
2934 * Provide expected ramp-up count for K7. All other
2935 * are set to zero, indicating minimum divisors for
2936 * every field.
2937 *
2938 * This prevents guest kernels on AMD host with CPU
2939 * type 6, model 8 and higher from exploding due to
2940 * the rdmsr failing.
2941 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002942 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002943 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002944 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002945 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2946 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002947 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002948 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2949 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2950 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03002951 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02002952 msr_info->index, &msr_info->data,
2953 msr_info->host_initiated);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002954 break;
john cooper91c9c3e2011-01-21 00:21:00 -05002955 case MSR_IA32_BBL_CR_CTL3:
2956 /* This legacy MSR exists but isn't fully documented in current
2957 * silicon. It is however accessed by winxp in very narrow
2958 * scenarios where it sets bit #19, itself documented as
2959 * a "reserved" bit. Best effort attempt to source coherent
2960 * read data here should the balance of the register be
2961 * interpreted by the guest:
2962 *
2963 * L2 cache control register 3: 64GB range, 256KB size,
2964 * enabled, latency 0x1, configured
2965 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002966 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05002967 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002968 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002969 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002970 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002971 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002972 break;
2973 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002974 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002975 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002976 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002977 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002978 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07002979 if (!msr_info->host_initiated &&
2980 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
2981 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002982 msr_info->data = vcpu->arch.msr_platform_info;
2983 break;
2984 case MSR_MISC_FEATURES_ENABLES:
2985 msr_info->data = vcpu->arch.msr_misc_features_enables;
2986 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02002987 case MSR_K7_HWCR:
2988 msr_info->data = vcpu->arch.msr_hwcr;
2989 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002990 default:
Wei Huangc6702c92015-06-19 13:44:45 +02002991 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002992 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002993 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002994 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
2995 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02002996 return 1;
2997 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002998 if (report_ignored_msrs)
2999 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3000 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003001 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003002 }
3003 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003004 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003005 return 0;
3006}
3007EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3008
Carsten Otte313a3dc2007-10-11 19:16:52 +02003009/*
3010 * Read or write a bunch of msrs. All parameters are kernel addresses.
3011 *
3012 * @return number of msrs set successfully.
3013 */
3014static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3015 struct kvm_msr_entry *entries,
3016 int (*do_msr)(struct kvm_vcpu *vcpu,
3017 unsigned index, u64 *data))
3018{
Tom Lendacky801e4592018-02-21 13:39:51 -06003019 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003020
Carsten Otte313a3dc2007-10-11 19:16:52 +02003021 for (i = 0; i < msrs->nmsrs; ++i)
3022 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3023 break;
3024
Carsten Otte313a3dc2007-10-11 19:16:52 +02003025 return i;
3026}
3027
3028/*
3029 * Read or write a bunch of msrs. Parameters are user addresses.
3030 *
3031 * @return number of msrs set successfully.
3032 */
3033static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3034 int (*do_msr)(struct kvm_vcpu *vcpu,
3035 unsigned index, u64 *data),
3036 int writeback)
3037{
3038 struct kvm_msrs msrs;
3039 struct kvm_msr_entry *entries;
3040 int r, n;
3041 unsigned size;
3042
3043 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003044 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003045 goto out;
3046
3047 r = -E2BIG;
3048 if (msrs.nmsrs >= MAX_IO_MSRS)
3049 goto out;
3050
Carsten Otte313a3dc2007-10-11 19:16:52 +02003051 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003052 entries = memdup_user(user_msrs->entries, size);
3053 if (IS_ERR(entries)) {
3054 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003055 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003056 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003057
3058 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3059 if (r < 0)
3060 goto out_free;
3061
3062 r = -EFAULT;
3063 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3064 goto out_free;
3065
3066 r = n;
3067
3068out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003069 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003070out:
3071 return r;
3072}
3073
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003074static inline bool kvm_can_mwait_in_guest(void)
3075{
3076 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003077 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3078 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003079}
3080
Alexander Graf784aa3d2014-07-14 18:27:35 +02003081int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003082{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003083 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003084
3085 switch (ext) {
3086 case KVM_CAP_IRQCHIP:
3087 case KVM_CAP_HLT:
3088 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003089 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003090 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003091 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003092 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003093 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003094 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003095 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003096 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003097 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003098 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003099 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003100 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003101 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003102 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003103 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003104 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003105 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003106 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003107 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003108 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003109 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003110 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003111 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003112 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003113 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003114 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003115 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02003116 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003117 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003118 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003119 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003120 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003121 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003122 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003123 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003124 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003125 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003126 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003127 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003128 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003129 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003130 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003131 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003132 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003133 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003134 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003135 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003136 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003137 r = 1;
3138 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003139 case KVM_CAP_SYNC_REGS:
3140 r = KVM_SYNC_X86_VALID_FIELDS;
3141 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003142 case KVM_CAP_ADJUST_CLOCK:
3143 r = KVM_CLOCK_TSC_STABLE;
3144 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003145 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003146 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3147 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003148 if(kvm_can_mwait_in_guest())
3149 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003150 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003151 case KVM_CAP_X86_SMM:
3152 /* SMBASE is usually relocated above 1M on modern chipsets,
3153 * and SMM handlers might indeed rely on 4G segment limits,
3154 * so do not report SMM to be available if real mode is
3155 * emulated via vm86 mode. Still, do not go to great lengths
3156 * to avoid userspace's usage of the feature, because it is a
3157 * fringe case that is not enabled except via specific settings
3158 * of the module parameters.
3159 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003160 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003161 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003162 case KVM_CAP_VAPIC:
3163 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3164 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003165 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003166 r = KVM_SOFT_MAX_VCPUS;
3167 break;
3168 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003169 r = KVM_MAX_VCPUS;
3170 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003171 case KVM_CAP_MAX_VCPU_ID:
3172 r = KVM_MAX_VCPU_ID;
3173 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003174 case KVM_CAP_PV_MMU: /* obsolete */
3175 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003176 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003177 case KVM_CAP_MCE:
3178 r = KVM_MAX_MCE_BANKS;
3179 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003180 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003181 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003182 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003183 case KVM_CAP_TSC_CONTROL:
3184 r = kvm_has_tsc_control;
3185 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003186 case KVM_CAP_X2APIC_API:
3187 r = KVM_X2APIC_API_VALID_FLAGS;
3188 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003189 case KVM_CAP_NESTED_STATE:
3190 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303191 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003192 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003193 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003194 break;
3195 }
3196 return r;
3197
3198}
3199
Carsten Otte043405e2007-10-10 17:16:19 +02003200long kvm_arch_dev_ioctl(struct file *filp,
3201 unsigned int ioctl, unsigned long arg)
3202{
3203 void __user *argp = (void __user *)arg;
3204 long r;
3205
3206 switch (ioctl) {
3207 case KVM_GET_MSR_INDEX_LIST: {
3208 struct kvm_msr_list __user *user_msr_list = argp;
3209 struct kvm_msr_list msr_list;
3210 unsigned n;
3211
3212 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003213 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003214 goto out;
3215 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003216 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003217 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003218 goto out;
3219 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003220 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003221 goto out;
3222 r = -EFAULT;
3223 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3224 num_msrs_to_save * sizeof(u32)))
3225 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003226 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003227 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003228 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003229 goto out;
3230 r = 0;
3231 break;
3232 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003233 case KVM_GET_SUPPORTED_CPUID:
3234 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003235 struct kvm_cpuid2 __user *cpuid_arg = argp;
3236 struct kvm_cpuid2 cpuid;
3237
3238 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003239 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003240 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003241
3242 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3243 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003244 if (r)
3245 goto out;
3246
3247 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003248 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003249 goto out;
3250 r = 0;
3251 break;
3252 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003253 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003254 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003255 if (copy_to_user(argp, &kvm_mce_cap_supported,
3256 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003257 goto out;
3258 r = 0;
3259 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003260 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3261 struct kvm_msr_list __user *user_msr_list = argp;
3262 struct kvm_msr_list msr_list;
3263 unsigned int n;
3264
3265 r = -EFAULT;
3266 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3267 goto out;
3268 n = msr_list.nmsrs;
3269 msr_list.nmsrs = num_msr_based_features;
3270 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3271 goto out;
3272 r = -E2BIG;
3273 if (n < msr_list.nmsrs)
3274 goto out;
3275 r = -EFAULT;
3276 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3277 num_msr_based_features * sizeof(u32)))
3278 goto out;
3279 r = 0;
3280 break;
3281 }
3282 case KVM_GET_MSRS:
3283 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3284 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003285 }
Carsten Otte043405e2007-10-10 17:16:19 +02003286 default:
3287 r = -EINVAL;
3288 }
3289out:
3290 return r;
3291}
3292
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003293static void wbinvd_ipi(void *garbage)
3294{
3295 wbinvd();
3296}
3297
3298static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3299{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003300 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003301}
3302
Carsten Otte313a3dc2007-10-11 19:16:52 +02003303void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3304{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003305 /* Address WBINVD may be executed by guest */
3306 if (need_emulate_wbinvd(vcpu)) {
3307 if (kvm_x86_ops->has_wbinvd_exit())
3308 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3309 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3310 smp_call_function_single(vcpu->cpu,
3311 wbinvd_ipi, NULL, 1);
3312 }
3313
Carsten Otte313a3dc2007-10-11 19:16:52 +02003314 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003315
Wanpeng Lie7517322019-07-22 12:26:20 +08003316 fpregs_assert_state_consistent();
3317 if (test_thread_flag(TIF_NEED_FPU_LOAD))
3318 switch_fpu_return();
3319
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003320 /* Apply any externally detected TSC adjustments (due to suspend) */
3321 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3322 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3323 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003324 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003325 }
3326
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003327 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003328 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003329 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003330 if (tsc_delta < 0)
3331 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003332
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003333 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003334 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003335 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003336 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003337 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003338 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003339
3340 if (kvm_lapic_hv_timer_in_use(vcpu))
3341 kvm_lapic_restart_hv_timer(vcpu);
3342
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003343 /*
3344 * On a host with synchronized TSC, there is no need to update
3345 * kvmclock on vcpu->cpu migration
3346 */
3347 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03003348 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003349 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003350 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003351 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003352 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003353
Glauber Costac9aaa892011-07-11 15:28:14 -04003354 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003355}
3356
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003357static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3358{
3359 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3360 return;
3361
Wanpeng Lifa55eed2017-12-12 17:33:01 -08003362 vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003363
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003364 kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003365 &vcpu->arch.st.steal.preempted,
3366 offsetof(struct kvm_steal_time, preempted),
3367 sizeof(vcpu->arch.st.steal.preempted));
3368}
3369
Carsten Otte313a3dc2007-10-11 19:16:52 +02003370void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3371{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003372 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003373
3374 if (vcpu->preempted)
3375 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3376
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003377 /*
3378 * Disable page faults because we're in atomic context here.
3379 * kvm_write_guest_offset_cached() would call might_fault()
3380 * that relies on pagefault_disable() to tell if there's a
3381 * bug. NOTE: the write to guest memory may not go through if
3382 * during postcopy live migration or if there's heavy guest
3383 * paging.
3384 */
3385 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003386 /*
3387 * kvm_memslots() will be called by
3388 * kvm_write_guest_offset_cached() so take the srcu lock.
3389 */
3390 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003391 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003392 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003393 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003394 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003395 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003396 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003397 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3398 * on every vmexit, but if not, we might have a stale dr6 from the
3399 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003400 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003401 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003402}
3403
Carsten Otte313a3dc2007-10-11 19:16:52 +02003404static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3405 struct kvm_lapic_state *s)
3406{
Liran Alonfa59cc02017-12-24 18:12:53 +02003407 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003408 kvm_x86_ops->sync_pir_to_irr(vcpu);
3409
Radim Krčmářa92e2542016-07-12 22:09:22 +02003410 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003411}
3412
3413static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3414 struct kvm_lapic_state *s)
3415{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003416 int r;
3417
3418 r = kvm_apic_set_state(vcpu, s);
3419 if (r)
3420 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003421 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003422
3423 return 0;
3424}
3425
Matt Gingell127a4572015-11-17 17:32:05 +01003426static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3427{
3428 return (!lapic_in_kernel(vcpu) ||
3429 kvm_apic_accept_pic_intr(vcpu));
3430}
3431
Matt Gingell782d4222015-11-16 15:26:00 -08003432/*
3433 * if userspace requested an interrupt window, check that the
3434 * interrupt window is open.
3435 *
3436 * No need to exit to userspace if we already have an interrupt queued.
3437 */
3438static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3439{
3440 return kvm_arch_interrupt_allowed(vcpu) &&
3441 !kvm_cpu_has_interrupt(vcpu) &&
3442 !kvm_event_needs_reinjection(vcpu) &&
3443 kvm_cpu_accept_dm_intr(vcpu);
3444}
3445
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003446static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3447 struct kvm_interrupt *irq)
3448{
Chen Gang02cdb502013-02-27 11:33:25 +08003449 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003450 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003451
3452 if (!irqchip_in_kernel(vcpu->kvm)) {
3453 kvm_queue_interrupt(vcpu, irq->irq, false);
3454 kvm_make_request(KVM_REQ_EVENT, vcpu);
3455 return 0;
3456 }
3457
3458 /*
3459 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3460 * fail for in-kernel 8259.
3461 */
3462 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003463 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003464
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003465 if (vcpu->arch.pending_external_vector != -1)
3466 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003467
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003468 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003469 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003470 return 0;
3471}
3472
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003473static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3474{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003475 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003476
3477 return 0;
3478}
3479
Paolo Bonzinif0778252015-04-01 15:06:40 +02003480static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3481{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003482 kvm_make_request(KVM_REQ_SMI, vcpu);
3483
Paolo Bonzinif0778252015-04-01 15:06:40 +02003484 return 0;
3485}
3486
Avi Kivityb209749f2007-10-22 16:50:39 +02003487static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3488 struct kvm_tpr_access_ctl *tac)
3489{
3490 if (tac->flags)
3491 return -EINVAL;
3492 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3493 return 0;
3494}
3495
Huang Ying890ca9a2009-05-11 16:48:15 +08003496static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3497 u64 mcg_cap)
3498{
3499 int r;
3500 unsigned bank_num = mcg_cap & 0xff, bank;
3501
3502 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003503 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003504 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003505 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003506 goto out;
3507 r = 0;
3508 vcpu->arch.mcg_cap = mcg_cap;
3509 /* Init IA32_MCG_CTL to all 1s */
3510 if (mcg_cap & MCG_CTL_P)
3511 vcpu->arch.mcg_ctl = ~(u64)0;
3512 /* Init IA32_MCi_CTL to all 1s */
3513 for (bank = 0; bank < bank_num; bank++)
3514 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003515
Sean Christopherson92735b12019-08-02 15:06:17 -07003516 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003517out:
3518 return r;
3519}
3520
3521static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3522 struct kvm_x86_mce *mce)
3523{
3524 u64 mcg_cap = vcpu->arch.mcg_cap;
3525 unsigned bank_num = mcg_cap & 0xff;
3526 u64 *banks = vcpu->arch.mce_banks;
3527
3528 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3529 return -EINVAL;
3530 /*
3531 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3532 * reporting is disabled
3533 */
3534 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3535 vcpu->arch.mcg_ctl != ~(u64)0)
3536 return 0;
3537 banks += 4 * mce->bank;
3538 /*
3539 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3540 * reporting is disabled for the bank
3541 */
3542 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3543 return 0;
3544 if (mce->status & MCI_STATUS_UC) {
3545 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003546 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003547 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003548 return 0;
3549 }
3550 if (banks[1] & MCI_STATUS_VAL)
3551 mce->status |= MCI_STATUS_OVER;
3552 banks[2] = mce->addr;
3553 banks[3] = mce->misc;
3554 vcpu->arch.mcg_status = mce->mcg_status;
3555 banks[1] = mce->status;
3556 kvm_queue_exception(vcpu, MC_VECTOR);
3557 } else if (!(banks[1] & MCI_STATUS_VAL)
3558 || !(banks[1] & MCI_STATUS_UC)) {
3559 if (banks[1] & MCI_STATUS_VAL)
3560 mce->status |= MCI_STATUS_OVER;
3561 banks[2] = mce->addr;
3562 banks[3] = mce->misc;
3563 banks[1] = mce->status;
3564 } else
3565 banks[1] |= MCI_STATUS_OVER;
3566 return 0;
3567}
3568
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003569static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3570 struct kvm_vcpu_events *events)
3571{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003572 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003573
Wanpeng Li664f8e22017-08-24 03:35:09 -07003574 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003575 * The API doesn't provide the instruction length for software
3576 * exceptions, so don't report them. As long as the guest RIP
3577 * isn't advanced, we should expect to encounter the exception
3578 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003579 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003580 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3581 events->exception.injected = 0;
3582 events->exception.pending = 0;
3583 } else {
3584 events->exception.injected = vcpu->arch.exception.injected;
3585 events->exception.pending = vcpu->arch.exception.pending;
3586 /*
3587 * For ABI compatibility, deliberately conflate
3588 * pending and injected exceptions when
3589 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3590 */
3591 if (!vcpu->kvm->arch.exception_payload_enabled)
3592 events->exception.injected |=
3593 vcpu->arch.exception.pending;
3594 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003595 events->exception.nr = vcpu->arch.exception.nr;
3596 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3597 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003598 events->exception_has_payload = vcpu->arch.exception.has_payload;
3599 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003600
Jan Kiszka03b82a32010-02-15 10:45:41 +01003601 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003602 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003603 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003604 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003605 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003606
3607 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003608 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003609 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003610 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003611
Jan Kiszka66450a22013-03-13 12:42:34 +01003612 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003613
Paolo Bonzinif0778252015-04-01 15:06:40 +02003614 events->smi.smm = is_smm(vcpu);
3615 events->smi.pending = vcpu->arch.smi_pending;
3616 events->smi.smm_inside_nmi =
3617 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3618 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3619
Jan Kiszkadab4b912009-12-06 18:24:15 +01003620 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003621 | KVM_VCPUEVENT_VALID_SHADOW
3622 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003623 if (vcpu->kvm->arch.exception_payload_enabled)
3624 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3625
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003626 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003627}
3628
Sean Christophersonc5833c72019-04-02 08:03:10 -07003629static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003630
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003631static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3632 struct kvm_vcpu_events *events)
3633{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003634 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003635 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003636 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003637 | KVM_VCPUEVENT_VALID_SMM
3638 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003639 return -EINVAL;
3640
Jim Mattson59073aa2018-10-16 14:29:20 -07003641 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3642 if (!vcpu->kvm->arch.exception_payload_enabled)
3643 return -EINVAL;
3644 if (events->exception.pending)
3645 events->exception.injected = 0;
3646 else
3647 events->exception_has_payload = 0;
3648 } else {
3649 events->exception.pending = 0;
3650 events->exception_has_payload = 0;
3651 }
3652
3653 if ((events->exception.injected || events->exception.pending) &&
3654 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003655 return -EINVAL;
3656
David Hildenbrand28bf2882017-03-23 11:46:03 +01003657 /* INITs are latched while in SMM */
3658 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3659 (events->smi.smm || events->smi.pending) &&
3660 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3661 return -EINVAL;
3662
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003663 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003664 vcpu->arch.exception.injected = events->exception.injected;
3665 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003666 vcpu->arch.exception.nr = events->exception.nr;
3667 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3668 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003669 vcpu->arch.exception.has_payload = events->exception_has_payload;
3670 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003671
Liran Alon04140b42018-03-23 03:01:31 +03003672 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003673 vcpu->arch.interrupt.nr = events->interrupt.nr;
3674 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003675 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3676 kvm_x86_ops->set_interrupt_shadow(vcpu,
3677 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003678
3679 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003680 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3681 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003682 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3683
Jan Kiszka66450a22013-03-13 12:42:34 +01003684 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003685 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003686 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003687
Paolo Bonzinif0778252015-04-01 15:06:40 +02003688 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003689 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3690 if (events->smi.smm)
3691 vcpu->arch.hflags |= HF_SMM_MASK;
3692 else
3693 vcpu->arch.hflags &= ~HF_SMM_MASK;
3694 kvm_smm_changed(vcpu);
3695 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003696
Paolo Bonzinif0778252015-04-01 15:06:40 +02003697 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003698
3699 if (events->smi.smm) {
3700 if (events->smi.smm_inside_nmi)
3701 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003702 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003703 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
3704 if (lapic_in_kernel(vcpu)) {
3705 if (events->smi.latched_init)
3706 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3707 else
3708 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3709 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003710 }
3711 }
3712
Avi Kivity3842d132010-07-27 12:30:24 +03003713 kvm_make_request(KVM_REQ_EVENT, vcpu);
3714
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003715 return 0;
3716}
3717
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003718static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3719 struct kvm_debugregs *dbgregs)
3720{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003721 unsigned long val;
3722
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003723 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003724 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003725 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003726 dbgregs->dr7 = vcpu->arch.dr7;
3727 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003728 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003729}
3730
3731static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3732 struct kvm_debugregs *dbgregs)
3733{
3734 if (dbgregs->flags)
3735 return -EINVAL;
3736
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003737 if (dbgregs->dr6 & ~0xffffffffull)
3738 return -EINVAL;
3739 if (dbgregs->dr7 & ~0xffffffffull)
3740 return -EINVAL;
3741
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003742 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003743 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003744 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003745 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003746 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003747 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003748
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003749 return 0;
3750}
3751
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003752#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3753
3754static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3755{
Marc Orrb666a4b2018-11-06 14:53:56 -08003756 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003757 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003758 u64 valid;
3759
3760 /*
3761 * Copy legacy XSAVE area, to avoid complications with CPUID
3762 * leaves 0 and 1 in the loop below.
3763 */
3764 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3765
3766 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01003767 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003768 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3769
3770 /*
3771 * Copy each region from the possibly compacted offset to the
3772 * non-compacted offset.
3773 */
Dave Hansend91cab72015-09-02 16:31:26 -07003774 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003775 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003776 u64 xfeature_mask = valid & -valid;
3777 int xfeature_nr = fls64(xfeature_mask) - 1;
3778 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003779
3780 if (src) {
3781 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003782 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003783 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003784 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003785 memcpy(dest + offset, &vcpu->arch.pkru,
3786 sizeof(vcpu->arch.pkru));
3787 else
3788 memcpy(dest + offset, src, size);
3789
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003790 }
3791
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003792 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003793 }
3794}
3795
3796static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3797{
Marc Orrb666a4b2018-11-06 14:53:56 -08003798 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003799 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3800 u64 valid;
3801
3802 /*
3803 * Copy legacy XSAVE area, to avoid complications with CPUID
3804 * leaves 0 and 1 in the loop below.
3805 */
3806 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3807
3808 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003809 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02003810 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02003811 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003812
3813 /*
3814 * Copy each region from the non-compacted offset to the
3815 * possibly compacted offset.
3816 */
Dave Hansend91cab72015-09-02 16:31:26 -07003817 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003818 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003819 u64 xfeature_mask = valid & -valid;
3820 int xfeature_nr = fls64(xfeature_mask) - 1;
3821 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003822
3823 if (dest) {
3824 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003825 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003826 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003827 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003828 memcpy(&vcpu->arch.pkru, src + offset,
3829 sizeof(vcpu->arch.pkru));
3830 else
3831 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08003832 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003833
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003834 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003835 }
3836}
3837
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003838static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3839 struct kvm_xsave *guest_xsave)
3840{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003841 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003842 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3843 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003844 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003845 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08003846 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003847 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003848 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07003849 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003850 }
3851}
3852
Wanpeng Lia5758132017-05-11 02:58:55 -07003853#define XSAVE_MXCSR_OFFSET 24
3854
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003855static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3856 struct kvm_xsave *guest_xsave)
3857{
3858 u64 xstate_bv =
3859 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07003860 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003861
Borislav Petkovd366bf72016-04-04 22:25:02 +02003862 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003863 /*
3864 * Here we allow setting states that are not present in
3865 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3866 * with old userspace.
3867 */
Wanpeng Lia5758132017-05-11 02:58:55 -07003868 if (xstate_bv & ~kvm_supported_xcr0() ||
3869 mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003870 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003871 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003872 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07003873 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
3874 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003875 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08003876 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003877 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003878 }
3879 return 0;
3880}
3881
3882static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3883 struct kvm_xcrs *guest_xcrs)
3884{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003885 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003886 guest_xcrs->nr_xcrs = 0;
3887 return;
3888 }
3889
3890 guest_xcrs->nr_xcrs = 1;
3891 guest_xcrs->flags = 0;
3892 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3893 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3894}
3895
3896static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3897 struct kvm_xcrs *guest_xcrs)
3898{
3899 int i, r = 0;
3900
Borislav Petkovd366bf72016-04-04 22:25:02 +02003901 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003902 return -EINVAL;
3903
3904 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
3905 return -EINVAL;
3906
3907 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
3908 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003909 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003910 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02003911 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003912 break;
3913 }
3914 if (r)
3915 r = -EINVAL;
3916 return r;
3917}
3918
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003919/*
3920 * kvm_set_guest_paused() indicates to the guest kernel that it has been
3921 * stopped by the hypervisor. This function will be called from the host only.
3922 * EINVAL is returned when the host attempts to set the flag for a guest that
3923 * does not support pv clocks.
3924 */
3925static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
3926{
Andy Honig0b794592013-02-20 14:48:10 -08003927 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003928 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03003929 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003930 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
3931 return 0;
3932}
3933
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003934static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3935 struct kvm_enable_cap *cap)
3936{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02003937 int r;
3938 uint16_t vmcs_version;
3939 void __user *user_ptr;
3940
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003941 if (cap->flags)
3942 return -EINVAL;
3943
3944 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03003945 case KVM_CAP_HYPERV_SYNIC2:
3946 if (cap->args[0])
3947 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06003948 /* fall through */
3949
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003950 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08003951 if (!irqchip_in_kernel(vcpu->kvm))
3952 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03003953 return kvm_hv_activate_synic(vcpu, cap->cap ==
3954 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02003955 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08003956 if (!kvm_x86_ops->nested_enable_evmcs)
3957 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02003958 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
3959 if (!r) {
3960 user_ptr = (void __user *)(uintptr_t)cap->args[0];
3961 if (copy_to_user(user_ptr, &vmcs_version,
3962 sizeof(vmcs_version)))
3963 r = -EFAULT;
3964 }
3965 return r;
3966
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003967 default:
3968 return -EINVAL;
3969 }
3970}
3971
Carsten Otte313a3dc2007-10-11 19:16:52 +02003972long kvm_arch_vcpu_ioctl(struct file *filp,
3973 unsigned int ioctl, unsigned long arg)
3974{
3975 struct kvm_vcpu *vcpu = filp->private_data;
3976 void __user *argp = (void __user *)arg;
3977 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003978 union {
3979 struct kvm_lapic_state *lapic;
3980 struct kvm_xsave *xsave;
3981 struct kvm_xcrs *xcrs;
3982 void *buffer;
3983 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003984
Christoffer Dall9b0624712017-12-04 21:35:36 +01003985 vcpu_load(vcpu);
3986
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003987 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003988 switch (ioctl) {
3989 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003990 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003991 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02003992 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08003993 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
3994 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003995
Dave Hansenb772ff32008-08-11 10:01:47 -07003996 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003997 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07003998 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03003999 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004000 if (r)
4001 goto out;
4002 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004003 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004004 goto out;
4005 r = 0;
4006 break;
4007 }
4008 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004009 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004010 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004011 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004012 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004013 if (IS_ERR(u.lapic)) {
4014 r = PTR_ERR(u.lapic);
4015 goto out_nofree;
4016 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004017
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004018 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004019 break;
4020 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004021 case KVM_INTERRUPT: {
4022 struct kvm_interrupt irq;
4023
4024 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004025 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004026 goto out;
4027 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004028 break;
4029 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004030 case KVM_NMI: {
4031 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004032 break;
4033 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004034 case KVM_SMI: {
4035 r = kvm_vcpu_ioctl_smi(vcpu);
4036 break;
4037 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004038 case KVM_SET_CPUID: {
4039 struct kvm_cpuid __user *cpuid_arg = argp;
4040 struct kvm_cpuid cpuid;
4041
4042 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004043 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004044 goto out;
4045 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004046 break;
4047 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004048 case KVM_SET_CPUID2: {
4049 struct kvm_cpuid2 __user *cpuid_arg = argp;
4050 struct kvm_cpuid2 cpuid;
4051
4052 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004053 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004054 goto out;
4055 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004056 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004057 break;
4058 }
4059 case KVM_GET_CPUID2: {
4060 struct kvm_cpuid2 __user *cpuid_arg = argp;
4061 struct kvm_cpuid2 cpuid;
4062
4063 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004064 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004065 goto out;
4066 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004067 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004068 if (r)
4069 goto out;
4070 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004071 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004072 goto out;
4073 r = 0;
4074 break;
4075 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004076 case KVM_GET_MSRS: {
4077 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004078 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004079 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004080 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004081 }
4082 case KVM_SET_MSRS: {
4083 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004084 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004085 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004086 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004087 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004088 case KVM_TPR_ACCESS_REPORTING: {
4089 struct kvm_tpr_access_ctl tac;
4090
4091 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004092 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004093 goto out;
4094 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4095 if (r)
4096 goto out;
4097 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004098 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004099 goto out;
4100 r = 0;
4101 break;
4102 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004103 case KVM_SET_VAPIC_ADDR: {
4104 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004105 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004106
4107 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004108 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004109 goto out;
4110 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004111 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004112 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004113 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004114 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004115 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004116 break;
4117 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004118 case KVM_X86_SETUP_MCE: {
4119 u64 mcg_cap;
4120
4121 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004122 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004123 goto out;
4124 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4125 break;
4126 }
4127 case KVM_X86_SET_MCE: {
4128 struct kvm_x86_mce mce;
4129
4130 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004131 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004132 goto out;
4133 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4134 break;
4135 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004136 case KVM_GET_VCPU_EVENTS: {
4137 struct kvm_vcpu_events events;
4138
4139 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4140
4141 r = -EFAULT;
4142 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4143 break;
4144 r = 0;
4145 break;
4146 }
4147 case KVM_SET_VCPU_EVENTS: {
4148 struct kvm_vcpu_events events;
4149
4150 r = -EFAULT;
4151 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4152 break;
4153
4154 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4155 break;
4156 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004157 case KVM_GET_DEBUGREGS: {
4158 struct kvm_debugregs dbgregs;
4159
4160 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4161
4162 r = -EFAULT;
4163 if (copy_to_user(argp, &dbgregs,
4164 sizeof(struct kvm_debugregs)))
4165 break;
4166 r = 0;
4167 break;
4168 }
4169 case KVM_SET_DEBUGREGS: {
4170 struct kvm_debugregs dbgregs;
4171
4172 r = -EFAULT;
4173 if (copy_from_user(&dbgregs, argp,
4174 sizeof(struct kvm_debugregs)))
4175 break;
4176
4177 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4178 break;
4179 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004180 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004181 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004182 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004183 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004184 break;
4185
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004186 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004187
4188 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004189 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004190 break;
4191 r = 0;
4192 break;
4193 }
4194 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004195 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004196 if (IS_ERR(u.xsave)) {
4197 r = PTR_ERR(u.xsave);
4198 goto out_nofree;
4199 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004200
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004201 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004202 break;
4203 }
4204 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004205 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004206 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004207 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004208 break;
4209
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004210 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004211
4212 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004213 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004214 sizeof(struct kvm_xcrs)))
4215 break;
4216 r = 0;
4217 break;
4218 }
4219 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004220 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004221 if (IS_ERR(u.xcrs)) {
4222 r = PTR_ERR(u.xcrs);
4223 goto out_nofree;
4224 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004225
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004226 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004227 break;
4228 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004229 case KVM_SET_TSC_KHZ: {
4230 u32 user_tsc_khz;
4231
4232 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004233 user_tsc_khz = (u32)arg;
4234
4235 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4236 goto out;
4237
Zachary Amsdencc578282012-02-03 15:43:50 -02004238 if (user_tsc_khz == 0)
4239 user_tsc_khz = tsc_khz;
4240
Haozhong Zhang381d5852015-10-20 15:39:04 +08004241 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4242 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004243
Joerg Roedel92a1f122011-03-25 09:44:51 +01004244 goto out;
4245 }
4246 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004247 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004248 goto out;
4249 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004250 case KVM_KVMCLOCK_CTRL: {
4251 r = kvm_set_guest_paused(vcpu);
4252 goto out;
4253 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004254 case KVM_ENABLE_CAP: {
4255 struct kvm_enable_cap cap;
4256
4257 r = -EFAULT;
4258 if (copy_from_user(&cap, argp, sizeof(cap)))
4259 goto out;
4260 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4261 break;
4262 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004263 case KVM_GET_NESTED_STATE: {
4264 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4265 u32 user_data_size;
4266
4267 r = -EINVAL;
4268 if (!kvm_x86_ops->get_nested_state)
4269 break;
4270
4271 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004272 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004273 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004274 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004275
4276 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4277 user_data_size);
4278 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004279 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004280
4281 if (r > user_data_size) {
4282 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004283 r = -EFAULT;
4284 else
4285 r = -E2BIG;
4286 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004287 }
Liran Alon26b471c2018-09-16 14:28:20 +03004288
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004289 r = 0;
4290 break;
4291 }
4292 case KVM_SET_NESTED_STATE: {
4293 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4294 struct kvm_nested_state kvm_state;
4295
4296 r = -EINVAL;
4297 if (!kvm_x86_ops->set_nested_state)
4298 break;
4299
Liran Alon26b471c2018-09-16 14:28:20 +03004300 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004301 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004302 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004303
Liran Alon26b471c2018-09-16 14:28:20 +03004304 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004305 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004306 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004307
4308 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004309 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4310 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004311 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004312
4313 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004314 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4315 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004316 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004317
4318 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
4319 break;
4320 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004321 case KVM_GET_SUPPORTED_HV_CPUID: {
4322 struct kvm_cpuid2 __user *cpuid_arg = argp;
4323 struct kvm_cpuid2 cpuid;
4324
4325 r = -EFAULT;
4326 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4327 goto out;
4328
4329 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4330 cpuid_arg->entries);
4331 if (r)
4332 goto out;
4333
4334 r = -EFAULT;
4335 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4336 goto out;
4337 r = 0;
4338 break;
4339 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004340 default:
4341 r = -EINVAL;
4342 }
4343out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004344 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004345out_nofree:
4346 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004347 return r;
4348}
4349
Souptick Joarder1499fa82018-04-19 00:49:58 +05304350vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004351{
4352 return VM_FAULT_SIGBUS;
4353}
4354
Carsten Otte1fe779f2007-10-29 16:08:35 +01004355static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4356{
4357 int ret;
4358
4359 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004360 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004361 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4362 return ret;
4363}
4364
Sheng Yangb927a3c2009-07-21 10:42:48 +08004365static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4366 u64 ident_addr)
4367{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004368 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004369}
4370
Carsten Otte1fe779f2007-10-29 16:08:35 +01004371static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004372 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004373{
4374 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4375 return -EINVAL;
4376
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004377 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004378
4379 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004380 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004381
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004382 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004383 return 0;
4384}
4385
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004386static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004387{
Dave Hansen39de71e2010-08-19 18:11:14 -07004388 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004389}
4390
Carsten Otte1fe779f2007-10-29 16:08:35 +01004391static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4392{
David Hildenbrand90bca052017-04-07 10:50:23 +02004393 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004394 int r;
4395
4396 r = 0;
4397 switch (chip->chip_id) {
4398 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004399 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004400 sizeof(struct kvm_pic_state));
4401 break;
4402 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004403 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004404 sizeof(struct kvm_pic_state));
4405 break;
4406 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004407 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004408 break;
4409 default:
4410 r = -EINVAL;
4411 break;
4412 }
4413 return r;
4414}
4415
4416static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4417{
David Hildenbrand90bca052017-04-07 10:50:23 +02004418 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004419 int r;
4420
4421 r = 0;
4422 switch (chip->chip_id) {
4423 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004424 spin_lock(&pic->lock);
4425 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004426 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004427 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004428 break;
4429 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004430 spin_lock(&pic->lock);
4431 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004432 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004433 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004434 break;
4435 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004436 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004437 break;
4438 default:
4439 r = -EINVAL;
4440 break;
4441 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004442 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004443 return r;
4444}
4445
Sheng Yange0f63cb2008-03-04 00:50:59 +08004446static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4447{
Radim Krčmář34f39412016-03-02 22:56:50 +01004448 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4449
4450 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4451
4452 mutex_lock(&kps->lock);
4453 memcpy(ps, &kps->channels, sizeof(*ps));
4454 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304455 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004456}
4457
4458static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4459{
Andrew Honig01856042015-11-18 14:50:23 -08004460 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004461 struct kvm_pit *pit = kvm->arch.vpit;
4462
4463 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004464 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004465 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004466 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4467 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304468 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004469}
4470
4471static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4472{
Beth Kone9f42752009-07-07 11:50:38 -04004473 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4474 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4475 sizeof(ps->channels));
4476 ps->flags = kvm->arch.vpit->pit_state.flags;
4477 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004478 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304479 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004480}
4481
4482static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4483{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304484 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004485 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004486 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004487 struct kvm_pit *pit = kvm->arch.vpit;
4488
4489 mutex_lock(&pit->pit_state.lock);
4490 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004491 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4492 if (!prev_legacy && cur_legacy)
4493 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004494 memcpy(&pit->pit_state.channels, &ps->channels,
4495 sizeof(pit->pit_state.channels));
4496 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004497 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004498 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004499 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004500 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304501 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004502}
4503
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004504static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4505 struct kvm_reinject_control *control)
4506{
Radim Krčmář71474e22016-03-02 22:56:45 +01004507 struct kvm_pit *pit = kvm->arch.vpit;
4508
4509 if (!pit)
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004510 return -ENXIO;
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004511
Radim Krčmář71474e22016-03-02 22:56:45 +01004512 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4513 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4514 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4515 */
4516 mutex_lock(&pit->pit_state.lock);
4517 kvm_pit_set_reinject(pit, control->pit_reinject);
4518 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004519
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004520 return 0;
4521}
4522
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004523/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004524 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
4525 * @kvm: kvm instance
4526 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004527 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004528 * Steps 1-4 below provide general overview of dirty page logging. See
4529 * kvm_get_dirty_log_protect() function description for additional details.
4530 *
4531 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
4532 * always flush the TLB (step 4) even if previous step failed and the dirty
4533 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
4534 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
4535 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004536 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004537 * 1. Take a snapshot of the bit and clear it if needed.
4538 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004539 * 3. Copy the snapshot to the userspace.
4540 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004541 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004542int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004543{
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004544 bool flush = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004545 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004546
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004547 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004548
Kai Huang88178fd2015-01-28 10:54:27 +08004549 /*
4550 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4551 */
4552 if (kvm_x86_ops->flush_log_dirty)
4553 kvm_x86_ops->flush_log_dirty(kvm);
4554
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004555 r = kvm_get_dirty_log_protect(kvm, log, &flush);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004556
4557 /*
4558 * All the TLBs can be flushed out of mmu lock, see the comments in
4559 * kvm_mmu_slot_remove_write_access().
4560 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004561 lockdep_assert_held(&kvm->slots_lock);
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004562 if (flush)
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004563 kvm_flush_remote_tlbs(kvm);
4564
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004565 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004566 return r;
4567}
4568
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02004569int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
4570{
4571 bool flush = false;
4572 int r;
4573
4574 mutex_lock(&kvm->slots_lock);
4575
4576 /*
4577 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4578 */
4579 if (kvm_x86_ops->flush_log_dirty)
4580 kvm_x86_ops->flush_log_dirty(kvm);
4581
4582 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
4583
4584 /*
4585 * All the TLBs can be flushed out of mmu lock, see the comments in
4586 * kvm_mmu_slot_remove_write_access().
4587 */
4588 lockdep_assert_held(&kvm->slots_lock);
4589 if (flush)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004590 kvm_flush_remote_tlbs(kvm);
4591
4592 mutex_unlock(&kvm->slots_lock);
4593 return r;
Avi Kivity367e1312009-08-26 14:57:07 +03004594}
Dave Hansenf0d66272008-08-11 10:01:45 -07004595
4596int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4597 bool line_status)
4598{
4599 if (!irqchip_in_kernel(kvm))
4600 return -ENXIO;
4601
Beth Kone9f42752009-07-07 11:50:38 -04004602 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Dave Hansenf0d66272008-08-11 10:01:45 -07004603 irq_event->irq, irq_event->level,
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004604 line_status);
Dave Hansenf0d66272008-08-11 10:01:45 -07004605 return 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004606}
4607
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004608int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4609 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004610{
4611 int r;
4612
4613 if (cap->flags)
4614 return -EINVAL;
4615
4616 switch (cap->cap) {
4617 case KVM_CAP_DISABLE_QUIRKS:
4618 kvm->arch.disabled_quirks = cap->args[0];
4619 r = 0;
4620 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004621 case KVM_CAP_SPLIT_IRQCHIP: {
4622 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004623 r = -EINVAL;
4624 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4625 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004626 r = -EEXIST;
4627 if (irqchip_in_kernel(kvm))
4628 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004629 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004630 goto split_irqchip_unlock;
4631 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004632 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004633 goto split_irqchip_unlock;
4634 /* Pairs with irqchip_in_kernel. */
4635 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004636 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004637 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004638 r = 0;
4639split_irqchip_unlock:
4640 mutex_unlock(&kvm->lock);
4641 break;
4642 }
Radim Krčmář371313132016-07-12 22:09:27 +02004643 case KVM_CAP_X2APIC_API:
4644 r = -EINVAL;
4645 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4646 break;
4647
4648 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4649 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004650 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4651 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004652
4653 r = 0;
4654 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004655 case KVM_CAP_X86_DISABLE_EXITS:
4656 r = -EINVAL;
4657 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4658 break;
4659
4660 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4661 kvm_can_mwait_in_guest())
4662 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004663 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004664 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004665 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4666 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004667 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4668 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004669 r = 0;
4670 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004671 case KVM_CAP_MSR_PLATFORM_INFO:
4672 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4673 r = 0;
4674 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004675 case KVM_CAP_EXCEPTION_PAYLOAD:
4676 kvm->arch.exception_payload_enabled = cap->args[0];
4677 r = 0;
4678 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004679 default:
4680 r = -EINVAL;
4681 break;
4682 }
4683 return r;
4684}
4685
Carsten Otte1fe779f2007-10-29 16:08:35 +01004686long kvm_arch_vm_ioctl(struct file *filp,
4687 unsigned int ioctl, unsigned long arg)
4688{
4689 struct kvm *kvm = filp->private_data;
4690 void __user *argp = (void __user *)arg;
4691 int r = -ENOTTY;
4692 /*
4693 * This union makes it completely explicit to gcc-3.x
4694 * that these two variables' stack usage should be
4695 * combined, not added together.
4696 */
4697 union {
4698 struct kvm_pit_state ps;
4699 struct kvm_pit_state2 ps2;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004700 struct kvm_pit_config pit_config;
4701 } u;
4702
4703 switch (ioctl) {
4704 case KVM_SET_TSS_ADDR:
4705 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004706 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004707 case KVM_SET_IDENTITY_MAP_ADDR: {
4708 u64 ident_addr;
4709
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004710 mutex_lock(&kvm->lock);
4711 r = -EINVAL;
4712 if (kvm->created_vcpus)
4713 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004714 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004715 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004716 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004717 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004718set_identity_unlock:
4719 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004720 break;
4721 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004722 case KVM_SET_NR_MMU_PAGES:
4723 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004724 break;
4725 case KVM_GET_NR_MMU_PAGES:
4726 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4727 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004728 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004729 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004730
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004731 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004732 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004733 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004734
Avi Kivity3e515702012-03-05 14:23:29 +02004735 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004736 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004737 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004738
4739 r = kvm_pic_init(kvm);
4740 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004741 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004742
4743 r = kvm_ioapic_init(kvm);
4744 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004745 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004746 goto create_irqchip_unlock;
4747 }
4748
Avi Kivity399ec802008-11-19 13:58:46 +02004749 r = kvm_setup_default_irq_routing(kvm);
4750 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004751 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004752 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004753 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004754 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004755 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004756 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004757 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004758 create_irqchip_unlock:
4759 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004760 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004761 }
Sheng Yang78376992008-01-28 05:10:22 +08004762 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004763 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4764 goto create_pit;
4765 case KVM_CREATE_PIT2:
4766 r = -EFAULT;
4767 if (copy_from_user(&u.pit_config, argp,
4768 sizeof(struct kvm_pit_config)))
4769 goto out;
4770 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004771 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004772 r = -EEXIST;
4773 if (kvm->arch.vpit)
4774 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004775 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004776 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004777 if (kvm->arch.vpit)
4778 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004779 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004780 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004781 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004782 case KVM_GET_IRQCHIP: {
4783 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004784 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004785
Sasha Levinff5c2c02011-12-04 19:36:29 +02004786 chip = memdup_user(argp, sizeof(*chip));
4787 if (IS_ERR(chip)) {
4788 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004789 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004790 }
4791
Carsten Otte1fe779f2007-10-29 16:08:35 +01004792 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004793 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004794 goto get_irqchip_out;
4795 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4796 if (r)
4797 goto get_irqchip_out;
4798 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004799 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07004800 goto get_irqchip_out;
4801 r = 0;
4802 get_irqchip_out:
4803 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004804 break;
4805 }
4806 case KVM_SET_IRQCHIP: {
4807 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004808 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004809
Sasha Levinff5c2c02011-12-04 19:36:29 +02004810 chip = memdup_user(argp, sizeof(*chip));
4811 if (IS_ERR(chip)) {
4812 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004813 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004814 }
4815
Carsten Otte1fe779f2007-10-29 16:08:35 +01004816 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004817 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004818 goto set_irqchip_out;
4819 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
4820 if (r)
4821 goto set_irqchip_out;
4822 r = 0;
4823 set_irqchip_out:
4824 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004825 break;
4826 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08004827 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004828 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004829 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004830 goto out;
4831 r = -ENXIO;
4832 if (!kvm->arch.vpit)
4833 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004834 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004835 if (r)
4836 goto out;
4837 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004838 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004839 goto out;
4840 r = 0;
4841 break;
4842 }
4843 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004844 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004845 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004846 goto out;
4847 r = -ENXIO;
4848 if (!kvm->arch.vpit)
4849 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004850 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004851 break;
4852 }
Beth Kone9f42752009-07-07 11:50:38 -04004853 case KVM_GET_PIT2: {
4854 r = -ENXIO;
4855 if (!kvm->arch.vpit)
4856 goto out;
4857 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
4858 if (r)
4859 goto out;
4860 r = -EFAULT;
4861 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
4862 goto out;
4863 r = 0;
4864 break;
4865 }
4866 case KVM_SET_PIT2: {
4867 r = -EFAULT;
4868 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
4869 goto out;
4870 r = -ENXIO;
4871 if (!kvm->arch.vpit)
4872 goto out;
4873 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04004874 break;
4875 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004876 case KVM_REINJECT_CONTROL: {
4877 struct kvm_reinject_control control;
4878 r = -EFAULT;
4879 if (copy_from_user(&control, argp, sizeof(control)))
4880 goto out;
4881 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004882 break;
4883 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004884 case KVM_SET_BOOT_CPU_ID:
4885 r = 0;
4886 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02004887 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004888 r = -EBUSY;
4889 else
4890 kvm->arch.bsp_vcpu_id = arg;
4891 mutex_unlock(&kvm->lock);
4892 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07004893 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02004894 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07004895 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02004896 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07004897 goto out;
4898 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02004899 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07004900 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02004901 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07004902 r = 0;
4903 break;
4904 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004905 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004906 struct kvm_clock_data user_ns;
4907 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004908
4909 r = -EFAULT;
4910 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
4911 goto out;
4912
4913 r = -EINVAL;
4914 if (user_ns.flags)
4915 goto out;
4916
4917 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02004918 /*
4919 * TODO: userspace has to take care of races with VCPU_RUN, so
4920 * kvm_gen_update_masterclock() can be cut down to locked
4921 * pvclock_update_vm_gtod_copy().
4922 */
4923 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03004924 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02004925 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02004926 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004927 break;
4928 }
4929 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004930 struct kvm_clock_data user_ns;
4931 u64 now_ns;
4932
Marcelo Tosattie891a322017-04-17 12:51:37 -03004933 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02004934 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01004935 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004936 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04004937
4938 r = -EFAULT;
4939 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
4940 goto out;
4941 r = 0;
4942 break;
4943 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06004944 case KVM_MEMORY_ENCRYPT_OP: {
4945 r = -ENOTTY;
4946 if (kvm_x86_ops->mem_enc_op)
4947 r = kvm_x86_ops->mem_enc_op(kvm, argp);
4948 break;
4949 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06004950 case KVM_MEMORY_ENCRYPT_REG_REGION: {
4951 struct kvm_enc_region region;
4952
4953 r = -EFAULT;
4954 if (copy_from_user(&region, argp, sizeof(region)))
4955 goto out;
4956
4957 r = -ENOTTY;
4958 if (kvm_x86_ops->mem_enc_reg_region)
4959 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
4960 break;
4961 }
4962 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
4963 struct kvm_enc_region region;
4964
4965 r = -EFAULT;
4966 if (copy_from_user(&region, argp, sizeof(region)))
4967 goto out;
4968
4969 r = -ENOTTY;
4970 if (kvm_x86_ops->mem_enc_unreg_region)
4971 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
4972 break;
4973 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03004974 case KVM_HYPERV_EVENTFD: {
4975 struct kvm_hyperv_eventfd hvevfd;
4976
4977 r = -EFAULT;
4978 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
4979 goto out;
4980 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
4981 break;
4982 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07004983 case KVM_SET_PMU_EVENT_FILTER:
4984 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
4985 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004986 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02004987 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004988 }
4989out:
4990 return r;
4991}
4992
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08004993static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02004994{
4995 u32 dummy[2];
4996 unsigned i, j;
4997
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02004998 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02004999 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
5000 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005001
5002 /*
5003 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005004 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005005 */
5006 switch (msrs_to_save[i]) {
5007 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005008 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005009 continue;
5010 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005011 case MSR_TSC_AUX:
5012 if (!kvm_x86_ops->rdtscp_supported())
5013 continue;
5014 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005015 case MSR_IA32_RTIT_CTL:
5016 case MSR_IA32_RTIT_STATUS:
5017 if (!kvm_x86_ops->pt_supported())
5018 continue;
5019 break;
5020 case MSR_IA32_RTIT_CR3_MATCH:
5021 if (!kvm_x86_ops->pt_supported() ||
5022 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5023 continue;
5024 break;
5025 case MSR_IA32_RTIT_OUTPUT_BASE:
5026 case MSR_IA32_RTIT_OUTPUT_MASK:
5027 if (!kvm_x86_ops->pt_supported() ||
5028 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5029 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5030 continue;
5031 break;
5032 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
5033 if (!kvm_x86_ops->pt_supported() ||
5034 msrs_to_save[i] - MSR_IA32_RTIT_ADDR0_A >=
5035 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5036 continue;
5037 break;
5038 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005039 default:
5040 break;
5041 }
5042
Carsten Otte043405e2007-10-10 17:16:19 +02005043 if (j < i)
5044 msrs_to_save[j] = msrs_to_save[i];
5045 j++;
5046 }
5047 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005048
5049 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
Tom Lendackybc226f02018-05-10 22:06:39 +02005050 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs[i]))
5051 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005052
5053 if (j < i)
5054 emulated_msrs[j] = emulated_msrs[i];
5055 j++;
5056 }
5057 num_emulated_msrs = j;
Tom Lendacky801e4592018-02-21 13:39:51 -06005058
5059 for (i = j = 0; i < ARRAY_SIZE(msr_based_features); i++) {
5060 struct kvm_msr_entry msr;
5061
5062 msr.index = msr_based_features[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005063 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005064 continue;
5065
5066 if (j < i)
5067 msr_based_features[j] = msr_based_features[i];
5068 j++;
5069 }
5070 num_msr_based_features = j;
Carsten Otte043405e2007-10-10 17:16:19 +02005071}
5072
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005073static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5074 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005075{
Avi Kivity70252a12010-01-19 12:51:22 +02005076 int handled = 0;
5077 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005078
Avi Kivity70252a12010-01-19 12:51:22 +02005079 do {
5080 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005081 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005082 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5083 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005084 break;
5085 handled += n;
5086 addr += n;
5087 len -= n;
5088 v += n;
5089 } while (len);
5090
5091 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005092}
5093
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005094static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005095{
Avi Kivity70252a12010-01-19 12:51:22 +02005096 int handled = 0;
5097 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005098
Avi Kivity70252a12010-01-19 12:51:22 +02005099 do {
5100 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005101 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005102 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5103 addr, n, v))
5104 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005105 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005106 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005107 handled += n;
5108 addr += n;
5109 len -= n;
5110 v += n;
5111 } while (len);
5112
5113 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005114}
5115
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005116static void kvm_set_segment(struct kvm_vcpu *vcpu,
5117 struct kvm_segment *var, int seg)
5118{
5119 kvm_x86_ops->set_segment(vcpu, var, seg);
5120}
5121
5122void kvm_get_segment(struct kvm_vcpu *vcpu,
5123 struct kvm_segment *var, int seg)
5124{
5125 kvm_x86_ops->get_segment(vcpu, var, seg);
5126}
5127
Paolo Bonzini54987b72014-09-02 13:23:06 +02005128gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5129 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005130{
5131 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005132
5133 BUG_ON(!mmu_is_nested(vcpu));
5134
5135 /* NPT walks are always user-walks */
5136 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005137 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005138
5139 return t_gpa;
5140}
5141
Avi Kivityab9ae312010-11-22 17:53:26 +02005142gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5143 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005144{
5145 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005146 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005147}
5148
Avi Kivityab9ae312010-11-22 17:53:26 +02005149 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5150 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005151{
5152 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5153 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005154 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005155}
5156
Avi Kivityab9ae312010-11-22 17:53:26 +02005157gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5158 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005159{
5160 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5161 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005162 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005163}
5164
5165/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005166gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5167 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005168{
Avi Kivityab9ae312010-11-22 17:53:26 +02005169 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005170}
5171
5172static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5173 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005174 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005175{
5176 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005177 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005178
5179 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005180 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005181 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005182 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005183 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005184 int ret;
5185
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005186 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005187 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005188 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5189 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005190 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005191 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005192 goto out;
5193 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005194
Izik Eidus77c20022008-12-29 01:42:19 +02005195 bytes -= toread;
5196 data += toread;
5197 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005198 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005199out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005200 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005201}
Izik Eidus77c20022008-12-29 01:42:19 +02005202
Gleb Natapov1871c602010-02-10 14:21:32 +02005203/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005204static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5205 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005206 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005207{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005208 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005209 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005210 unsigned offset;
5211 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005212
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005213 /* Inline kvm_read_guest_virt_helper for speed. */
5214 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5215 exception);
5216 if (unlikely(gpa == UNMAPPED_GVA))
5217 return X86EMUL_PROPAGATE_FAULT;
5218
5219 offset = addr & (PAGE_SIZE-1);
5220 if (WARN_ON(offset + bytes > PAGE_SIZE))
5221 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005222 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5223 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005224 if (unlikely(ret < 0))
5225 return X86EMUL_IO_NEEDED;
5226
5227 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005228}
5229
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005230int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005231 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005232 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005233{
5234 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005235
Paolo Bonzini353c0952019-01-29 18:41:16 +01005236 /*
5237 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5238 * is returned, but our callers are not ready for that and they blindly
5239 * call kvm_inject_page_fault. Ensure that they at least do not leak
5240 * uninitialized kernel stack memory into cr2 and error code.
5241 */
5242 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005243 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005244 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005245}
Nadav Har'El064aea72011-05-25 23:04:56 +03005246EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005247
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005248static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5249 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005250 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005251{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005252 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005253 u32 access = 0;
5254
5255 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5256 access |= PFERR_USER_MASK;
5257
5258 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005259}
5260
Radim Krčmář7a036a62015-10-30 16:36:24 +01005261static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5262 unsigned long addr, void *val, unsigned int bytes)
5263{
5264 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5265 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5266
5267 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5268}
5269
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005270static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5271 struct kvm_vcpu *vcpu, u32 access,
5272 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005273{
5274 void *data = val;
5275 int r = X86EMUL_CONTINUE;
5276
5277 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005278 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005279 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005280 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005281 unsigned offset = addr & (PAGE_SIZE-1);
5282 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5283 int ret;
5284
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005285 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005286 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005287 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005288 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005289 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005290 goto out;
5291 }
5292
5293 bytes -= towrite;
5294 data += towrite;
5295 addr += towrite;
5296 }
5297out:
5298 return r;
5299}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005300
5301static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005302 unsigned int bytes, struct x86_exception *exception,
5303 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005304{
5305 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005306 u32 access = PFERR_WRITE_MASK;
5307
5308 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5309 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005310
5311 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005312 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005313}
5314
5315int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5316 unsigned int bytes, struct x86_exception *exception)
5317{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005318 /* kvm_write_guest_virt_system can pull in tons of pages. */
5319 vcpu->arch.l1tf_flush_l1d = true;
5320
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005321 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5322 PFERR_WRITE_MASK, exception);
5323}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005324EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005325
Wanpeng Li082d06e2018-04-03 16:28:48 -07005326int handle_ud(struct kvm_vcpu *vcpu)
5327{
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005328 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005329 enum emulation_result er;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005330 char sig[5]; /* ud2; .ascii "kvm" */
5331 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005332
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005333 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005334 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5335 sig, sizeof(sig), &e) == 0 &&
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005336 memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0) {
5337 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
5338 emul_type = 0;
5339 }
5340
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005341 er = kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005342 if (er == EMULATE_USER_EXIT)
5343 return 0;
5344 if (er != EMULATE_DONE)
5345 kvm_queue_exception(vcpu, UD_VECTOR);
5346 return 1;
5347}
5348EXPORT_SYMBOL_GPL(handle_ud);
5349
Tom Lendacky0f89b202016-12-14 14:59:23 -05005350static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5351 gpa_t gpa, bool write)
5352{
5353 /* For APIC access vmexit */
5354 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5355 return 1;
5356
5357 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5358 trace_vcpu_match_mmio(gva, gpa, write, true);
5359 return 1;
5360 }
5361
5362 return 0;
5363}
5364
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005365static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5366 gpa_t *gpa, struct x86_exception *exception,
5367 bool write)
5368{
Avi Kivity97d64b72012-09-12 14:52:00 +03005369 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5370 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005371
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005372 /*
5373 * currently PKRU is only applied to ept enabled guest so
5374 * there is no pkey in EPT page table for L1 guest or EPT
5375 * shadow page table for L2 guest.
5376 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005377 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005378 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005379 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005380 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5381 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005382 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005383 return 1;
5384 }
5385
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005386 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5387
5388 if (*gpa == UNMAPPED_GVA)
5389 return -1;
5390
Tom Lendacky0f89b202016-12-14 14:59:23 -05005391 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005392}
5393
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005394int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005395 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005396{
5397 int ret;
5398
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005399 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005400 if (ret < 0)
5401 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005402 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005403 return 1;
5404}
5405
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005406struct read_write_emulator_ops {
5407 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5408 int bytes);
5409 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5410 void *val, int bytes);
5411 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5412 int bytes, void *val);
5413 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5414 void *val, int bytes);
5415 bool write;
5416};
5417
5418static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5419{
5420 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005421 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005422 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005423 vcpu->mmio_read_completed = 0;
5424 return 1;
5425 }
5426
5427 return 0;
5428}
5429
5430static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5431 void *val, int bytes)
5432{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005433 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005434}
5435
5436static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5437 void *val, int bytes)
5438{
5439 return emulator_write_phys(vcpu, gpa, val, bytes);
5440}
5441
5442static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5443{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005444 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005445 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5446}
5447
5448static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5449 void *val, int bytes)
5450{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005451 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005452 return X86EMUL_IO_NEEDED;
5453}
5454
5455static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5456 void *val, int bytes)
5457{
Avi Kivityf78146b2012-04-18 19:22:47 +03005458 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5459
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005460 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005461 return X86EMUL_CONTINUE;
5462}
5463
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005464static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005465 .read_write_prepare = read_prepare,
5466 .read_write_emulate = read_emulate,
5467 .read_write_mmio = vcpu_mmio_read,
5468 .read_write_exit_mmio = read_exit_mmio,
5469};
5470
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005471static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005472 .read_write_emulate = write_emulate,
5473 .read_write_mmio = write_mmio,
5474 .read_write_exit_mmio = write_exit_mmio,
5475 .write = true,
5476};
5477
Xiao Guangrong22388a32011-07-13 14:32:31 +08005478static int emulator_read_write_onepage(unsigned long addr, void *val,
5479 unsigned int bytes,
5480 struct x86_exception *exception,
5481 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005482 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005483{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005484 gpa_t gpa;
5485 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005486 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005487 struct kvm_mmio_fragment *frag;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005488 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
5489
5490 /*
5491 * If the exit was due to a NPF we may already have a GPA.
5492 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5493 * Note, this cannot be used on string operations since string
5494 * operation using rep will only have the initial GPA from the NPF
5495 * occurred.
5496 */
5497 if (vcpu->arch.gpa_available &&
5498 emulator_can_use_gpa(ctxt) &&
Brijesh Singh618232e2017-08-17 18:36:57 +02005499 (addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
5500 gpa = vcpu->arch.gpa_val;
5501 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5502 } else {
5503 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5504 if (ret < 0)
5505 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005506 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005507
Brijesh Singh618232e2017-08-17 18:36:57 +02005508 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005509 return X86EMUL_CONTINUE;
5510
Carsten Ottebbd9b642007-10-30 18:44:21 +01005511 /*
5512 * Is this MMIO handled locally?
5513 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005514 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005515 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005516 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005517
Avi Kivity70252a12010-01-19 12:51:22 +02005518 gpa += handled;
5519 bytes -= handled;
5520 val += handled;
5521
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005522 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5523 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5524 frag->gpa = gpa;
5525 frag->data = val;
5526 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005527 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005528}
5529
Xiubo Li52eb5a62015-03-13 17:39:45 +08005530static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5531 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005532 void *val, unsigned int bytes,
5533 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005534 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005535{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005536 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005537 gpa_t gpa;
5538 int rc;
5539
5540 if (ops->read_write_prepare &&
5541 ops->read_write_prepare(vcpu, val, bytes))
5542 return X86EMUL_CONTINUE;
5543
5544 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005545
Carsten Ottebbd9b642007-10-30 18:44:21 +01005546 /* Crossing a page boundary? */
5547 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005548 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005549
5550 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005551 rc = emulator_read_write_onepage(addr, val, now, exception,
5552 vcpu, ops);
5553
Carsten Ottebbd9b642007-10-30 18:44:21 +01005554 if (rc != X86EMUL_CONTINUE)
5555 return rc;
5556 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005557 if (ctxt->mode != X86EMUL_MODE_PROT64)
5558 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005559 val += now;
5560 bytes -= now;
5561 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005562
Avi Kivityf78146b2012-04-18 19:22:47 +03005563 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5564 vcpu, ops);
5565 if (rc != X86EMUL_CONTINUE)
5566 return rc;
5567
5568 if (!vcpu->mmio_nr_fragments)
5569 return rc;
5570
5571 gpa = vcpu->mmio_fragments[0].gpa;
5572
5573 vcpu->mmio_needed = 1;
5574 vcpu->mmio_cur_fragment = 0;
5575
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005576 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005577 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5578 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5579 vcpu->run->mmio.phys_addr = gpa;
5580
5581 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005582}
5583
5584static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5585 unsigned long addr,
5586 void *val,
5587 unsigned int bytes,
5588 struct x86_exception *exception)
5589{
5590 return emulator_read_write(ctxt, addr, val, bytes,
5591 exception, &read_emultor);
5592}
5593
Xiubo Li52eb5a62015-03-13 17:39:45 +08005594static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005595 unsigned long addr,
5596 const void *val,
5597 unsigned int bytes,
5598 struct x86_exception *exception)
5599{
5600 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5601 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005602}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005603
Avi Kivitydaea3e72010-03-15 13:59:54 +02005604#define CMPXCHG_TYPE(t, ptr, old, new) \
5605 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5606
5607#ifdef CONFIG_X86_64
5608# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5609#else
5610# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005611 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005612#endif
5613
Avi Kivity0f65dd72011-04-20 13:37:53 +03005614static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5615 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005616 const void *old,
5617 const void *new,
5618 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005619 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005620{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005621 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005622 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005623 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005624 char *kaddr;
5625 bool exchanged;
5626
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005627 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005628 if (bytes > 8 || (bytes & (bytes - 1)))
5629 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005630
Avi Kivitydaea3e72010-03-15 13:59:54 +02005631 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005632
Avi Kivitydaea3e72010-03-15 13:59:54 +02005633 if (gpa == UNMAPPED_GVA ||
5634 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5635 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005636
Avi Kivitydaea3e72010-03-15 13:59:54 +02005637 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5638 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005639
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005640 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005641 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005642
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005643 kaddr = map.hva + offset_in_page(gpa);
5644
Avi Kivitydaea3e72010-03-15 13:59:54 +02005645 switch (bytes) {
5646 case 1:
5647 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5648 break;
5649 case 2:
5650 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5651 break;
5652 case 4:
5653 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5654 break;
5655 case 8:
5656 exchanged = CMPXCHG64(kaddr, old, new);
5657 break;
5658 default:
5659 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005660 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005661
5662 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005663
5664 if (!exchanged)
5665 return X86EMUL_CMPXCHG_FAILED;
5666
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005667 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005668
5669 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005670
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005671emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005672 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005673
Avi Kivity0f65dd72011-04-20 13:37:53 +03005674 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005675}
5676
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005677static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5678{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005679 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005680
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005681 for (i = 0; i < vcpu->arch.pio.count; i++) {
5682 if (vcpu->arch.pio.in)
5683 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5684 vcpu->arch.pio.size, pd);
5685 else
5686 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5687 vcpu->arch.pio.port, vcpu->arch.pio.size,
5688 pd);
5689 if (r)
5690 break;
5691 pd += vcpu->arch.pio.size;
5692 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005693 return r;
5694}
5695
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005696static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5697 unsigned short port, void *val,
5698 unsigned int count, bool in)
5699{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005700 vcpu->arch.pio.port = port;
5701 vcpu->arch.pio.in = in;
5702 vcpu->arch.pio.count = count;
5703 vcpu->arch.pio.size = size;
5704
5705 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5706 vcpu->arch.pio.count = 0;
5707 return 1;
5708 }
5709
5710 vcpu->run->exit_reason = KVM_EXIT_IO;
5711 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5712 vcpu->run->io.size = size;
5713 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5714 vcpu->run->io.count = count;
5715 vcpu->run->io.port = port;
5716
5717 return 0;
5718}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005719
Avi Kivityca1d4a92011-04-20 13:37:53 +03005720static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5721 int size, unsigned short port, void *val,
5722 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005723{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005724 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005725 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005726
Gleb Natapov79729952010-03-18 15:20:24 +02005727 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005728 goto data_avail;
5729
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005730 memset(vcpu->arch.pio_data, 0, size * count);
5731
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005732 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5733 if (ret) {
5734data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005735 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005736 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005737 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005738 return 1;
5739 }
5740
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005741 return 0;
5742}
5743
Avi Kivityca1d4a92011-04-20 13:37:53 +03005744static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5745 int size, unsigned short port,
5746 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005747{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005748 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5749
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005750 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005751 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005752 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005753}
5754
Carsten Ottebbd9b642007-10-30 18:44:21 +01005755static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5756{
5757 return kvm_x86_ops->get_segment_base(vcpu, seg);
5758}
5759
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005760static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005761{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005762 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005763}
5764
Jiang Biaoae6a2372016-11-07 08:54:51 +08005765static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005766{
5767 if (!need_emulate_wbinvd(vcpu))
5768 return X86EMUL_CONTINUE;
5769
5770 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005771 int cpu = get_cpu();
5772
5773 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005774 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5775 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005776 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005777 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005778 } else
5779 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005780 return X86EMUL_CONTINUE;
5781}
Joel Schopp5cb56052015-03-02 13:43:31 -06005782
5783int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
5784{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005785 kvm_emulate_wbinvd_noskip(vcpu);
5786 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06005787}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005788EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
5789
Joel Schopp5cb56052015-03-02 13:43:31 -06005790
5791
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005792static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
5793{
Joel Schopp5cb56052015-03-02 13:43:31 -06005794 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005795}
5796
Xiubo Li52eb5a62015-03-13 17:39:45 +08005797static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
5798 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005799{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005800 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005801}
5802
Xiubo Li52eb5a62015-03-13 17:39:45 +08005803static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
5804 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005805{
Gleb Natapov338dbc92010-04-28 19:15:32 +03005806
Avi Kivity717746e2011-04-20 13:37:53 +03005807 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005808}
5809
Gleb Natapov52a46612010-03-18 15:20:03 +02005810static u64 mk_cr_64(u64 curr_cr, u32 new_val)
5811{
5812 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
5813}
5814
Avi Kivity717746e2011-04-20 13:37:53 +03005815static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02005816{
Avi Kivity717746e2011-04-20 13:37:53 +03005817 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02005818 unsigned long value;
5819
5820 switch (cr) {
5821 case 0:
5822 value = kvm_read_cr0(vcpu);
5823 break;
5824 case 2:
5825 value = vcpu->arch.cr2;
5826 break;
5827 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005828 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02005829 break;
5830 case 4:
5831 value = kvm_read_cr4(vcpu);
5832 break;
5833 case 8:
5834 value = kvm_get_cr8(vcpu);
5835 break;
5836 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03005837 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02005838 return 0;
5839 }
5840
5841 return value;
5842}
5843
Avi Kivity717746e2011-04-20 13:37:53 +03005844static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02005845{
Avi Kivity717746e2011-04-20 13:37:53 +03005846 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03005847 int res = 0;
5848
Gleb Natapov52a46612010-03-18 15:20:03 +02005849 switch (cr) {
5850 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03005851 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02005852 break;
5853 case 2:
5854 vcpu->arch.cr2 = val;
5855 break;
5856 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005857 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02005858 break;
5859 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03005860 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02005861 break;
5862 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005863 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02005864 break;
5865 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03005866 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03005867 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02005868 }
Gleb Natapov0f122442010-04-28 19:15:31 +03005869
5870 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02005871}
5872
Avi Kivity717746e2011-04-20 13:37:53 +03005873static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02005874{
Avi Kivity717746e2011-04-20 13:37:53 +03005875 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02005876}
5877
Avi Kivity4bff1e862011-04-20 13:37:53 +03005878static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005879{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005880 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005881}
5882
Avi Kivity4bff1e862011-04-20 13:37:53 +03005883static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005884{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005885 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005886}
5887
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03005888static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
5889{
5890 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
5891}
5892
5893static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
5894{
5895 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
5896}
5897
Avi Kivity4bff1e862011-04-20 13:37:53 +03005898static unsigned long emulator_get_cached_segment_base(
5899 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03005900{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005901 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03005902}
5903
Avi Kivity1aa36612011-04-27 13:20:30 +03005904static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
5905 struct desc_struct *desc, u32 *base3,
5906 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005907{
5908 struct kvm_segment var;
5909
Avi Kivity4bff1e862011-04-20 13:37:53 +03005910 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03005911 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005912
Gleb Natapov378a8b02013-01-21 15:36:48 +02005913 if (var.unusable) {
5914 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02005915 if (base3)
5916 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005917 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02005918 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005919
5920 if (var.g)
5921 var.limit >>= 12;
5922 set_desc_limit(desc, var.limit);
5923 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02005924#ifdef CONFIG_X86_64
5925 if (base3)
5926 *base3 = var.base >> 32;
5927#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005928 desc->type = var.type;
5929 desc->s = var.s;
5930 desc->dpl = var.dpl;
5931 desc->p = var.present;
5932 desc->avl = var.avl;
5933 desc->l = var.l;
5934 desc->d = var.db;
5935 desc->g = var.g;
5936
5937 return true;
5938}
5939
Avi Kivity1aa36612011-04-27 13:20:30 +03005940static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
5941 struct desc_struct *desc, u32 base3,
5942 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005943{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005944 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005945 struct kvm_segment var;
5946
Avi Kivity1aa36612011-04-27 13:20:30 +03005947 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005948 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02005949#ifdef CONFIG_X86_64
5950 var.base |= ((u64)base3) << 32;
5951#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005952 var.limit = get_desc_limit(desc);
5953 if (desc->g)
5954 var.limit = (var.limit << 12) | 0xfff;
5955 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005956 var.dpl = desc->dpl;
5957 var.db = desc->d;
5958 var.s = desc->s;
5959 var.l = desc->l;
5960 var.g = desc->g;
5961 var.avl = desc->avl;
5962 var.present = desc->p;
5963 var.unusable = !var.present;
5964 var.padding = 0;
5965
5966 kvm_set_segment(vcpu, &var, seg);
5967 return;
5968}
5969
Avi Kivity717746e2011-04-20 13:37:53 +03005970static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
5971 u32 msr_index, u64 *pdata)
5972{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005973 struct msr_data msr;
5974 int r;
5975
5976 msr.index = msr_index;
5977 msr.host_initiated = false;
5978 r = kvm_get_msr(emul_to_vcpu(ctxt), &msr);
5979 if (r)
5980 return r;
5981
5982 *pdata = msr.data;
5983 return 0;
Avi Kivity717746e2011-04-20 13:37:53 +03005984}
5985
5986static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
5987 u32 msr_index, u64 data)
5988{
Will Auld8fe8ab42012-11-29 12:42:12 -08005989 struct msr_data msr;
5990
5991 msr.data = data;
5992 msr.index = msr_index;
5993 msr.host_initiated = false;
5994 return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
Avi Kivity717746e2011-04-20 13:37:53 +03005995}
5996
Paolo Bonzini64d60672015-05-07 11:36:11 +02005997static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
5998{
5999 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6000
6001 return vcpu->arch.smbase;
6002}
6003
6004static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6005{
6006 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6007
6008 vcpu->arch.smbase = smbase;
6009}
6010
Nadav Amit67f4d422014-06-02 18:34:09 +03006011static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6012 u32 pmc)
6013{
Wei Huangc6702c92015-06-19 13:44:45 +02006014 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006015}
6016
Avi Kivity222d21a2011-11-10 14:57:30 +02006017static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6018 u32 pmc, u64 *pdata)
6019{
Wei Huangc6702c92015-06-19 13:44:45 +02006020 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006021}
6022
Avi Kivity6c3287f2011-04-20 15:43:05 +03006023static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6024{
6025 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6026}
6027
Avi Kivity29535382011-04-20 13:37:53 +03006028static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006029 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006030 enum x86_intercept_stage stage)
6031{
Avi Kivity29535382011-04-20 13:37:53 +03006032 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006033}
6034
Yu Zhange911eb32017-08-24 20:27:52 +08006035static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6036 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006037{
Yu Zhange911eb32017-08-24 20:27:52 +08006038 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006039}
6040
Avi Kivitydd856ef2012-08-27 23:46:17 +03006041static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6042{
6043 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6044}
6045
6046static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6047{
6048 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6049}
6050
Nadav Amit801806d2015-01-26 09:32:23 +02006051static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6052{
6053 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6054}
6055
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006056static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6057{
6058 return emul_to_vcpu(ctxt)->arch.hflags;
6059}
6060
6061static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6062{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006063 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006064}
6065
Sean Christophersoned193212019-04-02 08:03:09 -07006066static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6067 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006068{
Sean Christophersoned193212019-04-02 08:03:09 -07006069 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006070}
6071
Sean Christophersonc5833c72019-04-02 08:03:10 -07006072static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6073{
6074 kvm_smm_changed(emul_to_vcpu(ctxt));
6075}
6076
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006077static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6078{
6079 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6080}
6081
Mathias Krause0225fb52012-08-30 01:30:16 +02006082static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006083 .read_gpr = emulator_read_gpr,
6084 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006085 .read_std = emulator_read_std,
6086 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006087 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006088 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006089 .read_emulated = emulator_read_emulated,
6090 .write_emulated = emulator_write_emulated,
6091 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006092 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006093 .pio_in_emulated = emulator_pio_in_emulated,
6094 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006095 .get_segment = emulator_get_segment,
6096 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006097 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006098 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006099 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006100 .set_gdt = emulator_set_gdt,
6101 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006102 .get_cr = emulator_get_cr,
6103 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006104 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006105 .get_dr = emulator_get_dr,
6106 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006107 .get_smbase = emulator_get_smbase,
6108 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006109 .set_msr = emulator_set_msr,
6110 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006111 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006112 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006113 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006114 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006115 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006116 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006117 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02006118 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006119 .get_hflags = emulator_get_hflags,
6120 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006121 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006122 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006123 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006124};
6125
Gleb Natapov95cb2292010-04-28 19:15:43 +03006126static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6127{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006128 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006129 /*
6130 * an sti; sti; sequence only disable interrupts for the first
6131 * instruction. So, if the last instruction, be it emulated or
6132 * not, left the system with the INT_STI flag enabled, it
6133 * means that the last instruction is an sti. We should not
6134 * leave the flag on in this case. The same goes for mov ss
6135 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006136 if (int_shadow & mask)
6137 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006138 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006139 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006140 if (!mask)
6141 kvm_make_request(KVM_REQ_EVENT, vcpu);
6142 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006143}
6144
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006145static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006146{
6147 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006148 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006149 return kvm_propagate_fault(vcpu, &ctxt->exception);
6150
6151 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006152 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6153 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006154 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006155 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006156 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006157}
6158
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006159static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6160{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006161 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006162 int cs_db, cs_l;
6163
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006164 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6165
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006166 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006167 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6168
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006169 ctxt->eip = kvm_rip_read(vcpu);
6170 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6171 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006172 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006173 cs_db ? X86EMUL_MODE_PROT32 :
6174 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006175 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006176 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6177 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006178
Avi Kivitydd856ef2012-08-27 23:46:17 +03006179 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006180 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006181}
6182
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006183int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006184{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006185 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006186 int ret;
6187
6188 init_emulate_ctxt(vcpu);
6189
Avi Kivity9dac77f2011-06-01 15:34:25 +03006190 ctxt->op_bytes = 2;
6191 ctxt->ad_bytes = 2;
6192 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006193 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006194
6195 if (ret != X86EMUL_CONTINUE)
6196 return EMULATE_FAIL;
6197
Avi Kivity9dac77f2011-06-01 15:34:25 +03006198 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006199 kvm_rip_write(vcpu, ctxt->eip);
6200 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02006201
Mohammed Gamal63995652010-09-19 14:34:06 +02006202 return EMULATE_DONE;
6203}
6204EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6205
Liran Alone2366172018-03-12 13:12:49 +02006206static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006207{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006208 int r = EMULATE_DONE;
6209
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006210 ++vcpu->stat.insn_emulation_fail;
6211 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006212
6213 if (emulation_type & EMULTYPE_NO_UD_ON_FAIL)
6214 return EMULATE_FAIL;
6215
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006216 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006217 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6218 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6219 vcpu->run->internal.ndata = 0;
Liran Alon1f4dcb32017-11-05 16:56:33 +02006220 r = EMULATE_USER_EXIT;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006221 }
Liran Alone2366172018-03-12 13:12:49 +02006222
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006223 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006224
6225 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006226}
6227
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006228static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006229 bool write_fault_to_shadow_pgtable,
6230 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006231{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006232 gpa_t gpa = cr2;
Dan Williamsba049e92016-01-15 16:56:11 -08006233 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006234
Sean Christopherson384bf222018-08-23 13:56:49 -07006235 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006236 return false;
6237
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006238 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6239 return false;
6240
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006241 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006242 /*
6243 * Write permission should be allowed since only
6244 * write access need to be emulated.
6245 */
6246 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006247
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006248 /*
6249 * If the mapping is invalid in guest, let cpu retry
6250 * it to generate fault.
6251 */
6252 if (gpa == UNMAPPED_GVA)
6253 return true;
6254 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006255
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006256 /*
6257 * Do not retry the unhandleable instruction if it faults on the
6258 * readonly host memory, otherwise it will goto a infinite loop:
6259 * retry instruction -> write #PF -> emulation fail -> retry
6260 * instruction -> ...
6261 */
6262 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006263
6264 /*
6265 * If the instruction failed on the error pfn, it can not be fixed,
6266 * report the error to userspace.
6267 */
6268 if (is_error_noslot_pfn(pfn))
6269 return false;
6270
6271 kvm_release_pfn_clean(pfn);
6272
6273 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006274 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006275 unsigned int indirect_shadow_pages;
6276
6277 spin_lock(&vcpu->kvm->mmu_lock);
6278 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6279 spin_unlock(&vcpu->kvm->mmu_lock);
6280
6281 if (indirect_shadow_pages)
6282 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6283
Gleb Natapova6f177e2010-07-08 12:41:12 +03006284 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006285 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006286
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006287 /*
6288 * if emulation was due to access to shadowed page table
6289 * and it failed try to unshadow page and re-enter the
6290 * guest to let CPU execute the instruction.
6291 */
6292 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006293
6294 /*
6295 * If the access faults on its page table, it can not
6296 * be fixed by unprotecting shadow page and it should
6297 * be reported to userspace.
6298 */
6299 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006300}
6301
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006302static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
6303 unsigned long cr2, int emulation_type)
6304{
6305 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6306 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
6307
6308 last_retry_eip = vcpu->arch.last_retry_eip;
6309 last_retry_addr = vcpu->arch.last_retry_addr;
6310
6311 /*
6312 * If the emulation is caused by #PF and it is non-page_table
6313 * writing instruction, it means the VM-EXIT is caused by shadow
6314 * page protected, we can zap the shadow page and retry this
6315 * instruction directly.
6316 *
6317 * Note: if the guest uses a non-page-table modifying instruction
6318 * on the PDE that points to the instruction, then we will unmap
6319 * the instruction and go to an infinite loop. So, we cache the
6320 * last retried eip and the last fault address, if we meet the eip
6321 * and the address again, we can break out of the potential infinite
6322 * loop.
6323 */
6324 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6325
Sean Christopherson384bf222018-08-23 13:56:49 -07006326 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006327 return false;
6328
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006329 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6330 return false;
6331
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006332 if (x86_page_table_writing_insn(ctxt))
6333 return false;
6334
6335 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
6336 return false;
6337
6338 vcpu->arch.last_retry_eip = ctxt->eip;
6339 vcpu->arch.last_retry_addr = cr2;
6340
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006341 if (!vcpu->arch.mmu->direct_map)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006342 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
6343
Xiao Guangrong22368022013-01-13 23:44:12 +08006344 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006345
6346 return true;
6347}
6348
Gleb Natapov716d51a2012-09-03 15:24:26 +03006349static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6350static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6351
Paolo Bonzini64d60672015-05-07 11:36:11 +02006352static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006353{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006354 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006355 /* This is a good place to trace that we are exiting SMM. */
6356 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6357
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006358 /* Process a latched INIT or SMI, if any. */
6359 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006360 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006361
6362 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006363}
6364
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006365static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6366 unsigned long *db)
6367{
6368 u32 dr6 = 0;
6369 int i;
6370 u32 enable, rwlen;
6371
6372 enable = dr7;
6373 rwlen = dr7 >> 16;
6374 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6375 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6376 dr6 |= (1 << i);
6377 return dr6;
6378}
6379
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006380static void kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006381{
6382 struct kvm_run *kvm_run = vcpu->run;
6383
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006384 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6385 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6386 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6387 kvm_run->debug.arch.exception = DB_VECTOR;
6388 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6389 *r = EMULATE_USER_EXIT;
6390 } else {
Jim Mattsonf10c7292018-10-16 14:29:23 -07006391 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006392 }
6393}
6394
Kyle Huey6affcbe2016-11-29 12:40:40 -08006395int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6396{
6397 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006398 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006399
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006400 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
6401 if (unlikely(r != EMULATE_DONE))
6402 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006403
6404 /*
6405 * rflags is the old, "raw" value of the flags. The new value has
6406 * not been saved yet.
6407 *
6408 * This is correct even for TF set by the guest, because "the
6409 * processor will not generate this exception after the instruction
6410 * that sets the TF flag".
6411 */
6412 if (unlikely(rflags & X86_EFLAGS_TF))
6413 kvm_vcpu_do_singlestep(vcpu, &r);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006414 return r == EMULATE_DONE;
6415}
6416EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6417
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006418static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6419{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006420 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6421 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006422 struct kvm_run *kvm_run = vcpu->run;
6423 unsigned long eip = kvm_get_linear_rip(vcpu);
6424 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006425 vcpu->arch.guest_debug_dr7,
6426 vcpu->arch.eff_db);
6427
6428 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006429 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006430 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006431 kvm_run->debug.arch.exception = DB_VECTOR;
6432 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6433 *r = EMULATE_USER_EXIT;
6434 return true;
6435 }
6436 }
6437
Nadav Amit4161a562014-07-17 01:19:31 +03006438 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6439 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006440 unsigned long eip = kvm_get_linear_rip(vcpu);
6441 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006442 vcpu->arch.dr7,
6443 vcpu->arch.db);
6444
6445 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006446 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006447 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006448 kvm_queue_exception(vcpu, DB_VECTOR);
6449 *r = EMULATE_DONE;
6450 return true;
6451 }
6452 }
6453
6454 return false;
6455}
6456
Liran Alon04789b62018-03-12 13:12:50 +02006457static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6458{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006459 switch (ctxt->opcode_len) {
6460 case 1:
6461 switch (ctxt->b) {
6462 case 0xe4: /* IN */
6463 case 0xe5:
6464 case 0xec:
6465 case 0xed:
6466 case 0xe6: /* OUT */
6467 case 0xe7:
6468 case 0xee:
6469 case 0xef:
6470 case 0x6c: /* INS */
6471 case 0x6d:
6472 case 0x6e: /* OUTS */
6473 case 0x6f:
6474 return true;
6475 }
6476 break;
6477 case 2:
6478 switch (ctxt->b) {
6479 case 0x33: /* RDPMC */
6480 return true;
6481 }
6482 break;
Liran Alon04789b62018-03-12 13:12:50 +02006483 }
6484
6485 return false;
6486}
6487
Andre Przywara51d8b662010-12-21 11:12:02 +01006488int x86_emulate_instruction(struct kvm_vcpu *vcpu,
6489 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01006490 int emulation_type,
6491 void *insn,
6492 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006493{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006494 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006495 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006496 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006497 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006498
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006499 vcpu->arch.l1tf_flush_l1d = true;
6500
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006501 /*
6502 * Clear write_fault_to_shadow_pgtable here to ensure it is
6503 * never reused.
6504 */
6505 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006506 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006507
Sheng Yang571008d2008-01-02 14:49:22 +08006508 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006509 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006510
6511 /*
6512 * We will reenter on the same instruction since
6513 * we do not set complete_userspace_io. This does not
6514 * handle watchpoints yet, those would be handled in
6515 * the emulate_ops.
6516 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006517 if (!(emulation_type & EMULTYPE_SKIP) &&
6518 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006519 return r;
6520
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006521 ctxt->interruptibility = 0;
6522 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006523 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006524 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006525
Borislav Petkovb51e9742013-09-22 16:44:52 +02006526 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006527
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006528 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006529
Avi Kivitye46479f2010-04-11 13:05:16 +03006530 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006531 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006532 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02006533 if (emulation_type & EMULTYPE_TRAP_UD)
6534 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03006535 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6536 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006537 return EMULATE_DONE;
Paolo Bonzini6ea6e842017-11-10 10:49:38 +01006538 if (ctxt->have_exception && inject_emulated_exception(vcpu))
6539 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006540 if (emulation_type & EMULTYPE_SKIP)
6541 return EMULATE_FAIL;
Liran Alone2366172018-03-12 13:12:49 +02006542 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006543 }
6544 }
6545
Liran Alon04789b62018-03-12 13:12:50 +02006546 if ((emulation_type & EMULTYPE_VMWARE) &&
6547 !is_vmware_backdoor_opcode(ctxt))
6548 return EMULATE_FAIL;
6549
Gleb Natapovba8afb62009-04-12 13:36:57 +03006550 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006551 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006552 if (ctxt->eflags & X86_EFLAGS_RF)
6553 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Vitaly Kuznetsov97413d22019-08-13 15:53:31 +02006554 kvm_x86_ops->set_interrupt_shadow(vcpu, 0);
Gleb Natapovba8afb62009-04-12 13:36:57 +03006555 return EMULATE_DONE;
6556 }
6557
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006558 if (retry_instruction(ctxt, cr2, emulation_type))
6559 return EMULATE_DONE;
6560
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006561 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006562 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006563 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6564 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006565 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006566 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006567
Gleb Natapov5cd21912010-03-18 15:20:26 +02006568restart:
Tom Lendacky0f89b202016-12-14 14:59:23 -05006569 /* Save the faulting GPA (cr2) in the address field */
6570 ctxt->exception.address = cr2;
6571
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006572 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006573
Joerg Roedel775fde82011-04-04 12:39:24 +02006574 if (r == EMULATION_INTERCEPTED)
6575 return EMULATE_DONE;
6576
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006577 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03006578 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6579 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006580 return EMULATE_DONE;
6581
Liran Alone2366172018-03-12 13:12:49 +02006582 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006583 }
6584
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006585 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006586 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006587 if (inject_emulated_exception(vcpu))
6588 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006589 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006590 if (!vcpu->arch.pio.in) {
6591 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006592 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006593 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006594 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006595 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6596 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006597 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006598 } else if (vcpu->mmio_needed) {
6599 if (!vcpu->mmio_is_write)
6600 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006601 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006602 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006603 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006604 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006605 else
6606 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006607
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006608 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006609 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006610 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006611 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006612 kvm_rip_write(vcpu, ctxt->eip);
Alexander Popov5cc244a2019-01-21 15:48:40 +03006613 if (r == EMULATE_DONE && ctxt->tf)
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006614 kvm_vcpu_do_singlestep(vcpu, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02006615 if (!ctxt->have_exception ||
6616 exception_type(ctxt->exception.vector) == EXCPT_TRAP)
6617 __kvm_set_rflags(vcpu, ctxt->eflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006618
6619 /*
6620 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6621 * do nothing, and it will be requested again as soon as
6622 * the shadow expires. But we still need to check here,
6623 * because POPF has no interrupt shadow.
6624 */
6625 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6626 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006627 } else
6628 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006629
Gleb Natapove85d28f2010-07-29 15:11:52 +03006630 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006631}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006632
6633int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6634{
6635 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6636}
6637EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6638
6639int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6640 void *insn, int insn_len)
6641{
6642 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6643}
6644EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006645
Sean Christopherson8764ed52019-04-29 07:04:15 -07006646static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6647{
6648 vcpu->arch.pio.count = 0;
6649 return 1;
6650}
6651
Sean Christopherson45def772019-03-11 20:01:05 -07006652static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6653{
6654 vcpu->arch.pio.count = 0;
6655
6656 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6657 return 1;
6658
6659 return kvm_skip_emulated_instruction(vcpu);
6660}
6661
Sean Christophersondca7f122018-03-08 08:57:27 -08006662static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6663 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006664{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006665 unsigned long val = kvm_rax_read(vcpu);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006666 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
6667 size, port, &val, 1);
Sean Christopherson8764ed52019-04-29 07:04:15 -07006668 if (ret)
6669 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006670
Sean Christopherson8764ed52019-04-29 07:04:15 -07006671 /*
6672 * Workaround userspace that relies on old KVM behavior of %rip being
6673 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6674 */
6675 if (port == 0x7e &&
6676 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6677 vcpu->arch.complete_userspace_io =
6678 complete_fast_pio_out_port_0x7e;
6679 kvm_skip_emulated_instruction(vcpu);
6680 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006681 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6682 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6683 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006684 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006685}
Carsten Ottede7d7892007-10-30 18:44:25 +01006686
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006687static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6688{
6689 unsigned long val;
6690
6691 /* We should only ever be called with arch.pio.count equal to 1 */
6692 BUG_ON(vcpu->arch.pio.count != 1);
6693
Sean Christopherson45def772019-03-11 20:01:05 -07006694 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6695 vcpu->arch.pio.count = 0;
6696 return 1;
6697 }
6698
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006699 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006700 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006701
6702 /*
6703 * Since vcpu->arch.pio.count == 1 let emulator_pio_in_emulated perform
6704 * the copy and tracing
6705 */
6706 emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, vcpu->arch.pio.size,
6707 vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006708 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006709
Sean Christopherson45def772019-03-11 20:01:05 -07006710 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006711}
6712
Sean Christophersondca7f122018-03-08 08:57:27 -08006713static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6714 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006715{
6716 unsigned long val;
6717 int ret;
6718
6719 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006720 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006721
6722 ret = emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, size, port,
6723 &val, 1);
6724 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07006725 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006726 return ret;
6727 }
6728
Sean Christopherson45def772019-03-11 20:01:05 -07006729 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006730 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
6731
6732 return 0;
6733}
Sean Christophersondca7f122018-03-08 08:57:27 -08006734
6735int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
6736{
Sean Christopherson45def772019-03-11 20:01:05 -07006737 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08006738
Sean Christophersondca7f122018-03-08 08:57:27 -08006739 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07006740 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08006741 else
Sean Christopherson45def772019-03-11 20:01:05 -07006742 ret = kvm_fast_pio_out(vcpu, size, port);
6743 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08006744}
6745EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006746
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006747static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006748{
Tejun Heo0a3aee02010-12-18 16:28:55 +01006749 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006750 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006751}
6752
6753static void tsc_khz_changed(void *data)
6754{
6755 struct cpufreq_freqs *freq = data;
6756 unsigned long khz = 0;
6757
6758 if (data)
6759 khz = freq->new;
6760 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
6761 khz = cpufreq_quick_get(raw_smp_processor_id());
6762 if (!khz)
6763 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01006764 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006765}
6766
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006767#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006768static void kvm_hyperv_tsc_notifier(void)
6769{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006770 struct kvm *kvm;
6771 struct kvm_vcpu *vcpu;
6772 int cpu;
6773
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006774 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006775 list_for_each_entry(kvm, &vm_list, vm_list)
6776 kvm_make_mclock_inprogress_request(kvm);
6777
6778 hyperv_stop_tsc_emulation();
6779
6780 /* TSC frequency always matches when on Hyper-V */
6781 for_each_present_cpu(cpu)
6782 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
6783 kvm_max_guest_tsc_khz = tsc_khz;
6784
6785 list_for_each_entry(kvm, &vm_list, vm_list) {
6786 struct kvm_arch *ka = &kvm->arch;
6787
6788 spin_lock(&ka->pvclock_gtod_sync_lock);
6789
6790 pvclock_update_vm_gtod_copy(kvm);
6791
6792 kvm_for_each_vcpu(cpu, vcpu, kvm)
6793 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
6794
6795 kvm_for_each_vcpu(cpu, vcpu, kvm)
6796 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
6797
6798 spin_unlock(&ka->pvclock_gtod_sync_lock);
6799 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006800 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006801}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006802#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006803
Viresh Kumardf240142019-04-29 15:03:58 +05306804static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006805{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006806 struct kvm *kvm;
6807 struct kvm_vcpu *vcpu;
6808 int i, send_ipi = 0;
6809
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006810 /*
6811 * We allow guests to temporarily run on slowing clocks,
6812 * provided we notify them after, or to run on accelerating
6813 * clocks, provided we notify them before. Thus time never
6814 * goes backwards.
6815 *
6816 * However, we have a problem. We can't atomically update
6817 * the frequency of a given CPU from this function; it is
6818 * merely a notifier, which can be called from any CPU.
6819 * Changing the TSC frequency at arbitrary points in time
6820 * requires a recomputation of local variables related to
6821 * the TSC for each VCPU. We must flag these local variables
6822 * to be updated and be sure the update takes place with the
6823 * new frequency before any guests proceed.
6824 *
6825 * Unfortunately, the combination of hotplug CPU and frequency
6826 * change creates an intractable locking scenario; the order
6827 * of when these callouts happen is undefined with respect to
6828 * CPU hotplug, and they can race with each other. As such,
6829 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
6830 * undefined; you can actually have a CPU frequency change take
6831 * place in between the computation of X and the setting of the
6832 * variable. To protect against this problem, all updates of
6833 * the per_cpu tsc_khz variable are done in an interrupt
6834 * protected IPI, and all callers wishing to update the value
6835 * must wait for a synchronous IPI to complete (which is trivial
6836 * if the caller is on the CPU already). This establishes the
6837 * necessary total order on variable updates.
6838 *
6839 * Note that because a guest time update may take place
6840 * anytime after the setting of the VCPU's request bit, the
6841 * correct TSC value must be set before the request. However,
6842 * to ensure the update actually makes it to any guest which
6843 * starts running in hardware virtualization between the set
6844 * and the acquisition of the spinlock, we must also ping the
6845 * CPU after setting the request bit.
6846 *
6847 */
6848
Viresh Kumardf240142019-04-29 15:03:58 +05306849 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006850
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006851 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006852 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03006853 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05306854 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006855 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10006856 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006857 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006858 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006859 }
6860 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006861 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006862
6863 if (freq->old < freq->new && send_ipi) {
6864 /*
6865 * We upscale the frequency. Must make the guest
6866 * doesn't see old kvmclock values while running with
6867 * the new frequency, otherwise we risk the guest sees
6868 * time go backwards.
6869 *
6870 * In case we update the frequency for another cpu
6871 * (which might be in guest context) send an interrupt
6872 * to kick the cpu out of guest context. Next time
6873 * guest context is entered kvmclock will be updated,
6874 * so the guest will not see stale values.
6875 */
Viresh Kumardf240142019-04-29 15:03:58 +05306876 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006877 }
Viresh Kumardf240142019-04-29 15:03:58 +05306878}
6879
6880static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
6881 void *data)
6882{
6883 struct cpufreq_freqs *freq = data;
6884 int cpu;
6885
6886 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
6887 return 0;
6888 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
6889 return 0;
6890
6891 for_each_cpu(cpu, freq->policy->cpus)
6892 __kvmclock_cpufreq_notifier(freq, cpu);
6893
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006894 return 0;
6895}
6896
6897static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006898 .notifier_call = kvmclock_cpufreq_notifier
6899};
6900
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006901static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006902{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006903 tsc_khz_changed(NULL);
6904 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006905}
6906
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006907static void kvm_timer_init(void)
6908{
Zachary Amsdenc2855452010-09-18 14:38:15 -10006909 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05306910
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006911 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10006912#ifdef CONFIG_CPU_FREQ
6913 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02006914 int cpu;
6915
Zachary Amsdenc2855452010-09-18 14:38:15 -10006916 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02006917 cpu = get_cpu();
6918 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10006919 if (policy.cpuinfo.max_freq)
6920 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02006921 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10006922#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006923 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
6924 CPUFREQ_TRANSITION_NOTIFIER);
6925 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05306926
Thomas Gleixner73c1b412016-12-21 20:19:54 +01006927 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006928 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10006929}
6930
Andi Kleendd60d212017-07-25 17:20:32 -07006931DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
6932EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006933
Gleb Natapovf5132b02011-11-10 14:57:22 +02006934int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006935{
Alex,Shi086c9852011-10-20 15:34:01 +08006936 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006937}
6938
6939static int kvm_is_user_mode(void)
6940{
6941 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006942
Alex,Shi086c9852011-10-20 15:34:01 +08006943 if (__this_cpu_read(current_vcpu))
6944 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006945
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006946 return user_mode != 0;
6947}
6948
6949static unsigned long kvm_get_guest_ip(void)
6950{
6951 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006952
Alex,Shi086c9852011-10-20 15:34:01 +08006953 if (__this_cpu_read(current_vcpu))
6954 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08006955
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006956 return ip;
6957}
6958
Luwei Kang8479e042019-02-18 19:26:07 -05006959static void kvm_handle_intel_pt_intr(void)
6960{
6961 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
6962
6963 kvm_make_request(KVM_REQ_PMI, vcpu);
6964 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
6965 (unsigned long *)&vcpu->arch.pmu.global_status);
6966}
6967
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006968static struct perf_guest_info_callbacks kvm_guest_cbs = {
6969 .is_in_guest = kvm_is_in_guest,
6970 .is_user_mode = kvm_is_user_mode,
6971 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05006972 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006973};
6974
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006975#ifdef CONFIG_X86_64
6976static void pvclock_gtod_update_fn(struct work_struct *work)
6977{
Marcelo Tosattid8281992012-11-27 23:29:01 -02006978 struct kvm *kvm;
6979
6980 struct kvm_vcpu *vcpu;
6981 int i;
6982
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006983 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006984 list_for_each_entry(kvm, &vm_list, vm_list)
6985 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08006986 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02006987 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006988 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02006989}
6990
6991static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
6992
6993/*
6994 * Notification about pvclock gtod data update.
6995 */
6996static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
6997 void *priv)
6998{
6999 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7000 struct timekeeper *tk = priv;
7001
7002 update_pvclock_gtod(tk);
7003
7004 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007005 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007006 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007007 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007008 atomic_read(&kvm_guest_has_master_clock) != 0)
7009 queue_work(system_long_wq, &pvclock_gtod_work);
7010
7011 return 0;
7012}
7013
7014static struct notifier_block pvclock_gtod_notifier = {
7015 .notifier_call = pvclock_gtod_notify,
7016};
7017#endif
7018
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007019int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007020{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007021 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007022 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007023
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007024 if (kvm_x86_ops) {
7025 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007026 r = -EEXIST;
7027 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007028 }
7029
7030 if (!ops->cpu_has_kvm_support()) {
7031 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007032 r = -EOPNOTSUPP;
7033 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007034 }
7035 if (ops->disabled_by_bios()) {
7036 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007037 r = -EOPNOTSUPP;
7038 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007039 }
7040
Marc Orrb666a4b2018-11-06 14:53:56 -08007041 /*
7042 * KVM explicitly assumes that the guest has an FPU and
7043 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7044 * vCPU's FPU state as a fxregs_state struct.
7045 */
7046 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7047 printk(KERN_ERR "kvm: inadequate fpu\n");
7048 r = -EOPNOTSUPP;
7049 goto out;
7050 }
7051
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007052 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007053 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007054 __alignof__(struct fpu), SLAB_ACCOUNT,
7055 NULL);
7056 if (!x86_fpu_cache) {
7057 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7058 goto out;
7059 }
7060
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007061 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7062 if (!shared_msrs) {
7063 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Marc Orrb666a4b2018-11-06 14:53:56 -08007064 goto out_free_x86_fpu_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007065 }
7066
Avi Kivity97db56c2008-01-13 13:23:56 +02007067 r = kvm_mmu_module_init();
7068 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007069 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007070
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007071 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007072
Sheng Yang7b523452008-04-25 21:13:50 +08007073 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007074 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007075 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007076 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007077
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007078 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7079
Borislav Petkovd366bf72016-04-04 22:25:02 +02007080 if (boot_cpu_has(X86_FEATURE_XSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +08007081 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
7082
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007083 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007084 if (pi_inject_timer == -1)
7085 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007086#ifdef CONFIG_X86_64
7087 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007088
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007089 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007090 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007091#endif
7092
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007093 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007094
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007095out_free_percpu:
7096 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007097out_free_x86_fpu_cache:
7098 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007099out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007100 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007101}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007102
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007103void kvm_arch_exit(void)
7104{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007105#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007106 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007107 clear_hv_tscchange_cb();
7108#endif
David Matlackcef84c32016-12-16 14:30:36 -08007109 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007110 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7111
Jan Kiszka888d2562009-04-17 19:24:58 +02007112 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7113 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7114 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007115 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007116#ifdef CONFIG_X86_64
7117 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7118#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007119 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007120 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007121 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007122 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007123}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007124
Joel Schopp5cb56052015-03-02 13:43:31 -06007125int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007126{
7127 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007128 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007129 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007130 return 1;
7131 } else {
7132 vcpu->run->exit_reason = KVM_EXIT_HLT;
7133 return 0;
7134 }
7135}
Joel Schopp5cb56052015-03-02 13:43:31 -06007136EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7137
7138int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7139{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007140 int ret = kvm_skip_emulated_instruction(vcpu);
7141 /*
7142 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7143 * KVM_EXIT_DEBUG here.
7144 */
7145 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007146}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007147EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7148
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007149#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007150static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7151 unsigned long clock_type)
7152{
7153 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007154 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007155 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007156 int ret;
7157
7158 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7159 return -KVM_EOPNOTSUPP;
7160
7161 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7162 return -KVM_EOPNOTSUPP;
7163
7164 clock_pairing.sec = ts.tv_sec;
7165 clock_pairing.nsec = ts.tv_nsec;
7166 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7167 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007168 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007169
7170 ret = 0;
7171 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7172 sizeof(struct kvm_clock_pairing)))
7173 ret = -KVM_EFAULT;
7174
7175 return ret;
7176}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007177#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007178
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307179/*
7180 * kvm_pv_kick_cpu_op: Kick a vcpu.
7181 *
7182 * @apicid - apicid of vcpu to be kicked.
7183 */
7184static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7185{
Raghavendra K T24d21662013-08-26 14:18:35 +05307186 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307187
Raghavendra K T24d21662013-08-26 14:18:35 +05307188 lapic_irq.shorthand = 0;
7189 lapic_irq.dest_mode = 0;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007190 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307191 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007192 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307193
Raghavendra K T24d21662013-08-26 14:18:35 +05307194 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007195 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307196}
7197
Andrey Smetanind62caab2015-11-10 15:36:33 +03007198void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
7199{
Paolo Bonzinif7589cc2019-01-30 17:18:59 +01007200 if (!lapic_in_kernel(vcpu)) {
7201 WARN_ON_ONCE(vcpu->arch.apicv_active);
7202 return;
7203 }
7204 if (!vcpu->arch.apicv_active)
7205 return;
7206
Andrey Smetanind62caab2015-11-10 15:36:33 +03007207 vcpu->arch.apicv_active = false;
7208 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
7209}
7210
Wanpeng Li71506292019-06-11 20:23:49 +08007211static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7212{
7213 struct kvm_vcpu *target = NULL;
7214 struct kvm_apic_map *map;
7215
7216 rcu_read_lock();
7217 map = rcu_dereference(kvm->arch.apic_map);
7218
7219 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7220 target = map->phys_map[dest_id]->vcpu;
7221
7222 rcu_read_unlock();
7223
Wanpeng Li266e85a2019-07-24 17:43:13 +08007224 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007225 kvm_vcpu_yield_to(target);
7226}
7227
Hollis Blanchard8776e512007-10-31 17:24:24 -05007228int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7229{
7230 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007231 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007232
Radim Krčmář696ca772018-05-24 17:50:56 +02007233 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7234 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007235
Sean Christophersonde3cd112019-04-30 10:36:17 -07007236 nr = kvm_rax_read(vcpu);
7237 a0 = kvm_rbx_read(vcpu);
7238 a1 = kvm_rcx_read(vcpu);
7239 a2 = kvm_rdx_read(vcpu);
7240 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007241
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007242 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007243
Nadav Amita449c7a2014-06-18 17:19:24 +03007244 op_64_bit = is_64_bit_mode(vcpu);
7245 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007246 nr &= 0xFFFFFFFF;
7247 a0 &= 0xFFFFFFFF;
7248 a1 &= 0xFFFFFFFF;
7249 a2 &= 0xFFFFFFFF;
7250 a3 &= 0xFFFFFFFF;
7251 }
7252
Jan Kiszka07708c42009-08-03 18:43:28 +02007253 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7254 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007255 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007256 }
7257
Hollis Blanchard8776e512007-10-31 17:24:24 -05007258 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007259 case KVM_HC_VAPIC_POLL_IRQ:
7260 ret = 0;
7261 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307262 case KVM_HC_KICK_CPU:
7263 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007264 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307265 ret = 0;
7266 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007267#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007268 case KVM_HC_CLOCK_PAIRING:
7269 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7270 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007271#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007272 case KVM_HC_SEND_IPI:
7273 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7274 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007275 case KVM_HC_SCHED_YIELD:
7276 kvm_sched_yield(vcpu->kvm, a0);
7277 ret = 0;
7278 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007279 default:
7280 ret = -KVM_ENOSYS;
7281 break;
7282 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007283out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007284 if (!op_64_bit)
7285 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007286 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007287
Amit Shahf11c3a82008-02-21 01:00:30 +05307288 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007289 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007290}
7291EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7292
Jan Kiszkab6785de2012-09-20 07:43:17 +02007293static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007294{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007295 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007296 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007297 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007298
Hollis Blanchard8776e512007-10-31 17:24:24 -05007299 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007300
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007301 return emulator_write_emulated(ctxt, rip, instruction, 3,
7302 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007303}
7304
Avi Kivity851ba692009-08-24 11:10:17 +03007305static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007306{
Matt Gingell782d4222015-11-16 15:26:00 -08007307 return vcpu->run->request_interrupt_window &&
7308 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007309}
7310
Avi Kivity851ba692009-08-24 11:10:17 +03007311static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007312{
Avi Kivity851ba692009-08-24 11:10:17 +03007313 struct kvm_run *kvm_run = vcpu->run;
7314
Jan Kiszka91586a32009-10-05 13:07:21 +02007315 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007316 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007317 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007318 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007319 kvm_run->ready_for_interrupt_injection =
7320 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007321 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007322}
7323
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007324static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7325{
7326 int max_irr, tpr;
7327
7328 if (!kvm_x86_ops->update_cr8_intercept)
7329 return;
7330
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007331 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007332 return;
7333
Andrey Smetanind62caab2015-11-10 15:36:33 +03007334 if (vcpu->arch.apicv_active)
7335 return;
7336
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007337 if (!vcpu->arch.apic->vapic_addr)
7338 max_irr = kvm_lapic_find_highest_irr(vcpu);
7339 else
7340 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007341
7342 if (max_irr != -1)
7343 max_irr >>= 4;
7344
7345 tpr = kvm_lapic_get_cr8(vcpu);
7346
7347 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7348}
7349
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007350static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007351{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007352 int r;
7353
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007354 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007355
Liran Alon1a680e32018-03-23 03:01:33 +03007356 if (vcpu->arch.exception.injected)
7357 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007358 /*
Liran Alona042c262018-03-23 03:01:32 +03007359 * Do not inject an NMI or interrupt if there is a pending
7360 * exception. Exceptions and interrupts are recognized at
7361 * instruction boundaries, i.e. the start of an instruction.
7362 * Trap-like exceptions, e.g. #DB, have higher priority than
7363 * NMIs and interrupts, i.e. traps are recognized before an
7364 * NMI/interrupt that's pending on the same instruction.
7365 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7366 * priority, but are only generated (pended) during instruction
7367 * execution, i.e. a pending fault-like exception means the
7368 * fault occurred on the *previous* instruction and must be
7369 * serviced prior to recognizing any new events in order to
7370 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007371 */
Liran Alon1a680e32018-03-23 03:01:33 +03007372 else if (!vcpu->arch.exception.pending) {
7373 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007374 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007375 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007376 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007377 }
7378
Liran Alon1a680e32018-03-23 03:01:33 +03007379 /*
7380 * Call check_nested_events() even if we reinjected a previous event
7381 * in order for caller to determine if it should require immediate-exit
7382 * from L2 to L1 due to pending L1 events which require exit
7383 * from L2 to L1.
7384 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007385 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7386 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7387 if (r != 0)
7388 return r;
7389 }
7390
7391 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007392 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007393 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7394 vcpu->arch.exception.has_error_code,
7395 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007396
Liran Alon1a680e32018-03-23 03:01:33 +03007397 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007398 vcpu->arch.exception.pending = false;
7399 vcpu->arch.exception.injected = true;
7400
Nadav Amitd6e8c852014-07-24 14:51:24 +03007401 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7402 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7403 X86_EFLAGS_RF);
7404
Jim Mattsonf10c7292018-10-16 14:29:23 -07007405 if (vcpu->arch.exception.nr == DB_VECTOR) {
7406 /*
7407 * This code assumes that nSVM doesn't use
7408 * check_nested_events(). If it does, the
7409 * DR6/DR7 changes should happen before L1
7410 * gets a #VMEXIT for an intercepted #DB in
7411 * L2. (Under VMX, on the other hand, the
7412 * DR6/DR7 changes should not happen in the
7413 * event of a VM-exit to L1 for an intercepted
7414 * #DB in L2.)
7415 */
7416 kvm_deliver_exception_payload(vcpu);
7417 if (vcpu->arch.dr7 & DR7_GD) {
7418 vcpu->arch.dr7 &= ~DR7_GD;
7419 kvm_update_dr7(vcpu);
7420 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007421 }
7422
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007423 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007424 }
7425
7426 /* Don't consider new event if we re-injected an event */
7427 if (kvm_event_needs_reinjection(vcpu))
7428 return 0;
7429
7430 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7431 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007432 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007433 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007434 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007435 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007436 --vcpu->arch.nmi_pending;
7437 vcpu->arch.nmi_injected = true;
7438 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007439 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007440 /*
7441 * Because interrupts can be injected asynchronously, we are
7442 * calling check_nested_events again here to avoid a race condition.
7443 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7444 * proposal and current concerns. Perhaps we should be setting
7445 * KVM_REQ_EVENT only on certain events and not unconditionally?
7446 */
7447 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7448 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7449 if (r != 0)
7450 return r;
7451 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007452 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007453 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7454 false);
7455 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007456 }
7457 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007458
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007459 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007460}
7461
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007462static void process_nmi(struct kvm_vcpu *vcpu)
7463{
7464 unsigned limit = 2;
7465
7466 /*
7467 * x86 is limited to one NMI running, and one NMI pending after it.
7468 * If an NMI is already in progress, limit further NMIs to just one.
7469 * Otherwise, allow two (and we'll inject the first one immediately).
7470 */
7471 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7472 limit = 1;
7473
7474 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7475 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7476 kvm_make_request(KVM_REQ_EVENT, vcpu);
7477}
7478
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007479static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007480{
7481 u32 flags = 0;
7482 flags |= seg->g << 23;
7483 flags |= seg->db << 22;
7484 flags |= seg->l << 21;
7485 flags |= seg->avl << 20;
7486 flags |= seg->present << 15;
7487 flags |= seg->dpl << 13;
7488 flags |= seg->s << 12;
7489 flags |= seg->type << 8;
7490 return flags;
7491}
7492
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007493static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007494{
7495 struct kvm_segment seg;
7496 int offset;
7497
7498 kvm_get_segment(vcpu, &seg, n);
7499 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7500
7501 if (n < 3)
7502 offset = 0x7f84 + n * 12;
7503 else
7504 offset = 0x7f2c + (n - 3) * 12;
7505
7506 put_smstate(u32, buf, offset + 8, seg.base);
7507 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007508 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007509}
7510
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007511#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007512static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007513{
7514 struct kvm_segment seg;
7515 int offset;
7516 u16 flags;
7517
7518 kvm_get_segment(vcpu, &seg, n);
7519 offset = 0x7e00 + n * 16;
7520
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007521 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007522 put_smstate(u16, buf, offset, seg.selector);
7523 put_smstate(u16, buf, offset + 2, flags);
7524 put_smstate(u32, buf, offset + 4, seg.limit);
7525 put_smstate(u64, buf, offset + 8, seg.base);
7526}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007527#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007528
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007529static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007530{
7531 struct desc_ptr dt;
7532 struct kvm_segment seg;
7533 unsigned long val;
7534 int i;
7535
7536 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7537 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7538 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7539 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7540
7541 for (i = 0; i < 8; i++)
7542 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7543
7544 kvm_get_dr(vcpu, 6, &val);
7545 put_smstate(u32, buf, 0x7fcc, (u32)val);
7546 kvm_get_dr(vcpu, 7, &val);
7547 put_smstate(u32, buf, 0x7fc8, (u32)val);
7548
7549 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7550 put_smstate(u32, buf, 0x7fc4, seg.selector);
7551 put_smstate(u32, buf, 0x7f64, seg.base);
7552 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007553 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007554
7555 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7556 put_smstate(u32, buf, 0x7fc0, seg.selector);
7557 put_smstate(u32, buf, 0x7f80, seg.base);
7558 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007559 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007560
7561 kvm_x86_ops->get_gdt(vcpu, &dt);
7562 put_smstate(u32, buf, 0x7f74, dt.address);
7563 put_smstate(u32, buf, 0x7f70, dt.size);
7564
7565 kvm_x86_ops->get_idt(vcpu, &dt);
7566 put_smstate(u32, buf, 0x7f58, dt.address);
7567 put_smstate(u32, buf, 0x7f54, dt.size);
7568
7569 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007570 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007571
7572 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7573
7574 /* revision id */
7575 put_smstate(u32, buf, 0x7efc, 0x00020000);
7576 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7577}
7578
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007579#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007580static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007581{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007582 struct desc_ptr dt;
7583 struct kvm_segment seg;
7584 unsigned long val;
7585 int i;
7586
7587 for (i = 0; i < 16; i++)
7588 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7589
7590 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7591 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7592
7593 kvm_get_dr(vcpu, 6, &val);
7594 put_smstate(u64, buf, 0x7f68, val);
7595 kvm_get_dr(vcpu, 7, &val);
7596 put_smstate(u64, buf, 0x7f60, val);
7597
7598 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7599 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7600 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7601
7602 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7603
7604 /* revision id */
7605 put_smstate(u32, buf, 0x7efc, 0x00020064);
7606
7607 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7608
7609 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7610 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007611 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007612 put_smstate(u32, buf, 0x7e94, seg.limit);
7613 put_smstate(u64, buf, 0x7e98, seg.base);
7614
7615 kvm_x86_ops->get_idt(vcpu, &dt);
7616 put_smstate(u32, buf, 0x7e84, dt.size);
7617 put_smstate(u64, buf, 0x7e88, dt.address);
7618
7619 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7620 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007621 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007622 put_smstate(u32, buf, 0x7e74, seg.limit);
7623 put_smstate(u64, buf, 0x7e78, seg.base);
7624
7625 kvm_x86_ops->get_gdt(vcpu, &dt);
7626 put_smstate(u32, buf, 0x7e64, dt.size);
7627 put_smstate(u64, buf, 0x7e68, dt.address);
7628
7629 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007630 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007631}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007632#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007633
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007634static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007635{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007636 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007637 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007638 char buf[512];
7639 u32 cr0;
7640
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007641 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007642 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007643#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007644 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007645 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007646 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007647#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007648 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007649
Ladi Prosek0234bf82017-10-11 16:54:40 +02007650 /*
7651 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7652 * vCPU state (e.g. leave guest mode) after we've saved the state into
7653 * the SMM state-save area.
7654 */
7655 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7656
7657 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007658 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007659
7660 if (kvm_x86_ops->get_nmi_mask(vcpu))
7661 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7662 else
7663 kvm_x86_ops->set_nmi_mask(vcpu, true);
7664
7665 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7666 kvm_rip_write(vcpu, 0x8000);
7667
7668 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7669 kvm_x86_ops->set_cr0(vcpu, cr0);
7670 vcpu->arch.cr0 = cr0;
7671
7672 kvm_x86_ops->set_cr4(vcpu, 0);
7673
Paolo Bonzini18c36262015-08-07 12:27:54 +02007674 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7675 dt.address = dt.size = 0;
7676 kvm_x86_ops->set_idt(vcpu, &dt);
7677
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007678 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7679
7680 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7681 cs.base = vcpu->arch.smbase;
7682
7683 ds.selector = 0;
7684 ds.base = 0;
7685
7686 cs.limit = ds.limit = 0xffffffff;
7687 cs.type = ds.type = 0x3;
7688 cs.dpl = ds.dpl = 0;
7689 cs.db = ds.db = 0;
7690 cs.s = ds.s = 1;
7691 cs.l = ds.l = 0;
7692 cs.g = ds.g = 1;
7693 cs.avl = ds.avl = 0;
7694 cs.present = ds.present = 1;
7695 cs.unusable = ds.unusable = 0;
7696 cs.padding = ds.padding = 0;
7697
7698 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7699 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7700 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7701 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7702 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7703 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7704
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007705#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007706 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007707 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007708#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007709
7710 kvm_update_cpuid(vcpu);
7711 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007712}
7713
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007714static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007715{
7716 vcpu->arch.smi_pending = true;
7717 kvm_make_request(KVM_REQ_EVENT, vcpu);
7718}
7719
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01007720void kvm_make_scan_ioapic_request(struct kvm *kvm)
7721{
7722 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
7723}
7724
Yang Zhang3d81bc72013-04-11 19:25:13 +08007725static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08007726{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08007727 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08007728 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08007729
Andrey Smetanin63086302015-11-10 15:36:32 +03007730 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007731
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07007732 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03007733 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007734 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02007735 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03007736 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08007737 if (ioapic_in_kernel(vcpu->kvm))
7738 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007739 }
Liran Alone40ff1d2018-03-21 02:50:31 +02007740
7741 if (is_guest_mode(vcpu))
7742 vcpu->arch.load_eoi_exitmap_pending = true;
7743 else
7744 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
7745}
7746
7747static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
7748{
7749 u64 eoi_exit_bitmap[4];
7750
7751 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
7752 return;
7753
Andrey Smetanin5c9194122015-11-10 15:36:34 +03007754 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
7755 vcpu_to_synic(vcpu)->vec_bitmap, 256);
7756 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007757}
7758
Michal Hocko93065ac2018-08-21 21:52:33 -07007759int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
7760 unsigned long start, unsigned long end,
7761 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01007762{
7763 unsigned long apic_address;
7764
7765 /*
7766 * The physical address of apic access page is stored in the VMCS.
7767 * Update it when it becomes invalid.
7768 */
7769 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
7770 if (start <= apic_address && apic_address < end)
7771 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07007772
7773 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01007774}
7775
Tang Chen4256f432014-09-24 15:57:54 +08007776void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
7777{
Tang Chenc24ae0d2014-09-24 15:57:58 +08007778 struct page *page = NULL;
7779
Paolo Bonzini35754c92015-07-29 12:05:37 +02007780 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02007781 return;
7782
Tang Chen4256f432014-09-24 15:57:54 +08007783 if (!kvm_x86_ops->set_apic_access_page_addr)
7784 return;
7785
Tang Chenc24ae0d2014-09-24 15:57:58 +08007786 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02007787 if (is_error_page(page))
7788 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08007789 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
7790
7791 /*
7792 * Do not pin apic access page in memory, the MMU notifier
7793 * will call us again if it is migrated or swapped out.
7794 */
7795 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08007796}
7797EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
7798
Sean Christophersond264ee02018-08-27 15:21:12 -07007799void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
7800{
7801 smp_send_reschedule(vcpu->cpu);
7802}
7803EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
7804
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007805/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01007806 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007807 * exiting to the userspace. Otherwise, the value will be returned to the
7808 * userspace.
7809 */
Avi Kivity851ba692009-08-24 11:10:17 +03007810static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007811{
7812 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08007813 bool req_int_win =
7814 dm_request_for_irq_injection(vcpu) &&
7815 kvm_cpu_accept_dm_intr(vcpu);
7816
Jan Kiszka730dca42013-04-28 10:50:52 +02007817 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007818
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02007819 if (kvm_request_pending(vcpu)) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +02007820 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu))
7821 kvm_x86_ops->get_vmcs12_pages(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007822 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05007823 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007824 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03007825 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007826 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
7827 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03007828 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
7829 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10007830 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
7831 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007832 if (unlikely(r))
7833 goto out;
7834 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03007835 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03007836 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07007837 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
7838 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007839 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08007840 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007841 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007842 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02007843 r = 0;
7844 goto out;
7845 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03007846 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007847 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007848 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01007849 r = 0;
7850 goto out;
7851 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02007852 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
7853 /* Page is swapped out. Do synthetic halt */
7854 vcpu->arch.apf.halted = true;
7855 r = 1;
7856 goto out;
7857 }
Glauber Costac9aaa892011-07-11 15:28:14 -04007858 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
7859 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007860 if (kvm_check_request(KVM_REQ_SMI, vcpu))
7861 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007862 if (kvm_check_request(KVM_REQ_NMI, vcpu))
7863 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007864 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02007865 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007866 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02007867 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07007868 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
7869 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
7870 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03007871 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07007872 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
7873 vcpu->run->eoi.vector =
7874 vcpu->arch.pending_ioapic_eoi;
7875 r = 0;
7876 goto out;
7877 }
7878 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08007879 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
7880 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02007881 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
7882 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08007883 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
7884 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03007885 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
7886 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7887 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
7888 r = 0;
7889 goto out;
7890 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03007891 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
7892 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7893 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
7894 r = 0;
7895 goto out;
7896 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03007897 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
7898 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
7899 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
7900 r = 0;
7901 goto out;
7902 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03007903
7904 /*
7905 * KVM_REQ_HV_STIMER has to be processed after
7906 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
7907 * depend on the guest clock being up-to-date
7908 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03007909 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
7910 kvm_hv_process_stimers(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02007911 }
Avi Kivityb93463a2007-10-25 16:52:32 +02007912
Avi Kivityb463a6f2010-07-20 15:06:17 +03007913 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01007914 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01007915 kvm_apic_accept_events(vcpu);
7916 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
7917 r = 1;
7918 goto out;
7919 }
7920
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007921 if (inject_pending_event(vcpu, req_int_win) != 0)
7922 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00007923 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007924 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007925 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007926 * SMIs have three cases:
7927 * 1) They can be nested, and then there is nothing to
7928 * do here because RSM will cause a vmexit anyway.
7929 * 2) There is an ISA-specific reason why SMI cannot be
7930 * injected, and the moment when this changes can be
7931 * intercepted.
7932 * 3) Or the SMI can be pending because
7933 * inject_pending_event has completed the injection
7934 * of an IRQ or NMI from the previous vmexit, and
7935 * then we request an immediate exit to inject the
7936 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007937 */
7938 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007939 if (!kvm_x86_ops->enable_smi_window(vcpu))
7940 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00007941 if (vcpu->arch.nmi_pending)
7942 kvm_x86_ops->enable_nmi_window(vcpu);
7943 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
7944 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007945 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00007946 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03007947
Avi Kivity3842d132010-07-27 12:30:24 +03007948 if (kvm_lapic_enabled(vcpu)) {
7949 update_cr8_intercept(vcpu);
7950 kvm_lapic_sync_to_vapic(vcpu);
7951 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007952 }
Avi Kivityb93463a2007-10-25 16:52:32 +02007953
Avi Kivityd8368af2012-05-14 18:07:56 +03007954 r = kvm_mmu_reload(vcpu);
7955 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03007956 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03007957 }
7958
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007959 preempt_disable();
7960
7961 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007962
7963 /*
7964 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
7965 * IPI are then delayed after guest entry, which ensures that they
7966 * result in virtual interrupt delivery.
7967 */
7968 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007969 vcpu->mode = IN_GUEST_MODE;
7970
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02007971 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
7972
Lan Tianyu0f127d12016-03-13 11:10:29 +08007973 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007974 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02007975 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007976 *
Luwei Kang81b01662019-01-31 16:52:02 +08007977 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007978 * pairs with the memory barrier implicit in pi_test_and_set_on
7979 * (see vmx_deliver_posted_interrupt).
7980 *
7981 * 3) This also orders the write to mode from any reads to the page
7982 * tables done while the VCPU is running. Please see the comment
7983 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007984 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02007985 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007986
Paolo Bonzinib95234c2016-12-19 13:57:33 +01007987 /*
7988 * This handles the case where a posted interrupt was
7989 * notified with kvm_vcpu_kick.
7990 */
Liran Alonfa59cc02017-12-24 18:12:53 +02007991 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
7992 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007993
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02007994 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007995 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08007996 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007997 smp_wmb();
7998 local_irq_enable();
7999 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008000 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008001 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008002 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008003 }
8004
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008005 if (req_immediate_exit) {
8006 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008007 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008008 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008009
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008010 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008011 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008012
Wanpeng Lie7517322019-07-22 12:26:20 +08008013 /* The preempt notifier should have taken care of the FPU already. */
8014 WARN_ON_ONCE(test_thread_flag(TIF_NEED_FPU_LOAD));
Rik van Riel5f409e22019-04-03 18:41:52 +02008015
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008016 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008017 set_debugreg(0, 7);
8018 set_debugreg(vcpu->arch.eff_db[0], 0);
8019 set_debugreg(vcpu->arch.eff_db[1], 1);
8020 set_debugreg(vcpu->arch.eff_db[2], 2);
8021 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008022 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008023 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008024 }
8025
Avi Kivity851ba692009-08-24 11:10:17 +03008026 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008027
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008028 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008029 * Do this here before restoring debug registers on the host. And
8030 * since we do this before handling the vmexit, a DR access vmexit
8031 * can (a) read the correct value of the debug registers, (b) set
8032 * KVM_DEBUGREG_WONT_EXIT again.
8033 */
8034 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008035 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8036 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008037 kvm_update_dr0123(vcpu);
8038 kvm_update_dr6(vcpu);
8039 kvm_update_dr7(vcpu);
8040 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008041 }
8042
8043 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008044 * If the guest has used debug registers, at least dr7
8045 * will be disabled while returning to the host.
8046 * If we don't have active breakpoints in the host, we don't
8047 * care about the messed up debug address registers. But if
8048 * we have some of them active, restore the old state.
8049 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008050 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008051 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008052
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008053 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008054
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008055 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008056 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008057
Sean Christopherson95b5a482019-04-19 22:50:59 -07008058 kvm_x86_ops->handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008059
Sean Christophersond7a08882019-07-10 09:07:34 -07008060 /*
8061 * Consume any pending interrupts, including the possible source of
8062 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8063 * An instruction is required after local_irq_enable() to fully unblock
8064 * interrupts on processors that implement an interrupt shadow, the
8065 * stat.exits increment will do nicely.
8066 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008067 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008068 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008069 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008070 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008071 kvm_after_interrupt(vcpu);
8072
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008073 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008074 if (lapic_in_kernel(vcpu)) {
8075 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8076 if (delta != S64_MIN) {
8077 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8078 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8079 }
8080 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008081
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008082 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008083 preempt_enable();
8084
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008085 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008086
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008087 /*
8088 * Profile KVM exit RIPs:
8089 */
8090 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008091 unsigned long rip = kvm_rip_read(vcpu);
8092 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008093 }
8094
Zachary Amsdencc578282012-02-03 15:43:50 -02008095 if (unlikely(vcpu->arch.tsc_always_catchup))
8096 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008097
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008098 if (vcpu->arch.apic_attention)
8099 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008100
Brijesh Singh618232e2017-08-17 18:36:57 +02008101 vcpu->arch.gpa_available = false;
Avi Kivity851ba692009-08-24 11:10:17 +03008102 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008103 return r;
8104
8105cancel_injection:
8106 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008107 if (unlikely(vcpu->arch.apic_attention))
8108 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008109out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008110 return r;
8111}
8112
Paolo Bonzini362c6982015-02-06 12:48:04 +01008113static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8114{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008115 if (!kvm_arch_vcpu_runnable(vcpu) &&
8116 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008117 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8118 kvm_vcpu_block(vcpu);
8119 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008120
8121 if (kvm_x86_ops->post_block)
8122 kvm_x86_ops->post_block(vcpu);
8123
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008124 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8125 return 1;
8126 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008127
Paolo Bonzini362c6982015-02-06 12:48:04 +01008128 kvm_apic_accept_events(vcpu);
8129 switch(vcpu->arch.mp_state) {
8130 case KVM_MP_STATE_HALTED:
8131 vcpu->arch.pv.pv_unhalted = false;
8132 vcpu->arch.mp_state =
8133 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008134 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008135 case KVM_MP_STATE_RUNNABLE:
8136 vcpu->arch.apf.halted = false;
8137 break;
8138 case KVM_MP_STATE_INIT_RECEIVED:
8139 break;
8140 default:
8141 return -EINTR;
8142 break;
8143 }
8144 return 1;
8145}
8146
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008147static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8148{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008149 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
8150 kvm_x86_ops->check_nested_events(vcpu, false);
8151
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008152 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8153 !vcpu->arch.apf.halted);
8154}
8155
Paolo Bonzini362c6982015-02-06 12:48:04 +01008156static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008157{
8158 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008159 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008160
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008161 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008162 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008163
Paolo Bonzini362c6982015-02-06 12:48:04 +01008164 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008165 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008166 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008167 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008168 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008169 }
8170
Gleb Natapov09cec752009-03-23 15:11:44 +02008171 if (r <= 0)
8172 break;
8173
Radim Krčmář72875d82017-04-26 22:32:19 +02008174 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008175 if (kvm_cpu_has_pending_timer(vcpu))
8176 kvm_inject_pending_timer_irqs(vcpu);
8177
Matt Gingell782d4222015-11-16 15:26:00 -08008178 if (dm_request_for_irq_injection(vcpu) &&
8179 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008180 r = 0;
8181 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008182 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008183 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008184 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008185
8186 kvm_check_async_pf_completion(vcpu);
8187
Gleb Natapov09cec752009-03-23 15:11:44 +02008188 if (signal_pending(current)) {
8189 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008190 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008191 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008192 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008193 }
8194 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008195 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008196 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008197 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008198 }
8199 }
8200
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008201 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008202
8203 return r;
8204}
8205
Gleb Natapov716d51a2012-09-03 15:24:26 +03008206static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8207{
8208 int r;
8209 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008210 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008211 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8212 if (r != EMULATE_DONE)
8213 return 0;
8214 return 1;
8215}
8216
8217static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8218{
8219 BUG_ON(!vcpu->arch.pio.count);
8220
8221 return complete_emulated_io(vcpu);
8222}
8223
Avi Kivityf78146b2012-04-18 19:22:47 +03008224/*
8225 * Implements the following, as a state machine:
8226 *
8227 * read:
8228 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008229 * for each mmio piece in the fragment
8230 * write gpa, len
8231 * exit
8232 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008233 * execute insn
8234 *
8235 * write:
8236 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008237 * for each mmio piece in the fragment
8238 * write gpa, len
8239 * copy data
8240 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008241 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008242static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008243{
8244 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008245 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008246 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008247
Gleb Natapov716d51a2012-09-03 15:24:26 +03008248 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008249
Gleb Natapov716d51a2012-09-03 15:24:26 +03008250 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008251 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8252 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008253 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008254 memcpy(frag->data, run->mmio.data, len);
8255
8256 if (frag->len <= 8) {
8257 /* Switch to the next fragment. */
8258 frag++;
8259 vcpu->mmio_cur_fragment++;
8260 } else {
8261 /* Go forward to the next mmio piece. */
8262 frag->data += len;
8263 frag->gpa += len;
8264 frag->len -= len;
8265 }
8266
Andrew Honiga08d3b32014-02-27 19:35:14 +01008267 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008268 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008269
8270 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008271 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008272 return 1;
8273 vcpu->mmio_read_completed = 1;
8274 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008275 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008276
Gleb Natapov716d51a2012-09-03 15:24:26 +03008277 run->exit_reason = KVM_EXIT_MMIO;
8278 run->mmio.phys_addr = frag->gpa;
8279 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008280 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8281 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008282 run->mmio.is_write = vcpu->mmio_is_write;
8283 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8284 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008285}
8286
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008287/* Swap (qemu) user FPU context for the guest FPU context. */
8288static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8289{
Rik van Riel5f409e22019-04-03 18:41:52 +02008290 fpregs_lock();
8291
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008292 copy_fpregs_to_fpstate(vcpu->arch.user_fpu);
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008293 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008294 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008295 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008296
8297 fpregs_mark_activate();
8298 fpregs_unlock();
8299
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008300 trace_kvm_fpu(1);
8301}
8302
8303/* When vcpu_run ends, restore user space FPU context. */
8304static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8305{
Rik van Riel5f409e22019-04-03 18:41:52 +02008306 fpregs_lock();
8307
Marc Orrb666a4b2018-11-06 14:53:56 -08008308 copy_fpregs_to_fpstate(vcpu->arch.guest_fpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008309 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008310
8311 fpregs_mark_activate();
8312 fpregs_unlock();
8313
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008314 ++vcpu->stat.fpu_reload;
8315 trace_kvm_fpu(0);
8316}
8317
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008318int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8319{
8320 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008321
Christoffer Dallaccb7572017-12-04 21:35:25 +01008322 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008323 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008324 kvm_load_guest_fpu(vcpu);
8325
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008326 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008327 if (kvm_run->immediate_exit) {
8328 r = -EINTR;
8329 goto out;
8330 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008331 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008332 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008333 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008334 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008335 if (signal_pending(current)) {
8336 r = -EINTR;
8337 vcpu->run->exit_reason = KVM_EXIT_INTR;
8338 ++vcpu->stat.signal_exits;
8339 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008340 goto out;
8341 }
8342
Ken Hofsass01643c52018-01-31 16:03:36 -08008343 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8344 r = -EINVAL;
8345 goto out;
8346 }
8347
8348 if (vcpu->run->kvm_dirty_regs) {
8349 r = sync_regs(vcpu);
8350 if (r != 0)
8351 goto out;
8352 }
8353
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008354 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008355 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008356 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8357 r = -EINVAL;
8358 goto out;
8359 }
8360 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008361
Gleb Natapov716d51a2012-09-03 15:24:26 +03008362 if (unlikely(vcpu->arch.complete_userspace_io)) {
8363 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8364 vcpu->arch.complete_userspace_io = NULL;
8365 r = cui(vcpu);
8366 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008367 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008368 } else
8369 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008370
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008371 if (kvm_run->immediate_exit)
8372 r = -EINTR;
8373 else
8374 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008375
8376out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008377 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008378 if (vcpu->run->kvm_valid_regs)
8379 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008380 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008381 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008382
Christoffer Dallaccb7572017-12-04 21:35:25 +01008383 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008384 return r;
8385}
8386
Ken Hofsass01643c52018-01-31 16:03:36 -08008387static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008388{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008389 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8390 /*
8391 * We are here if userspace calls get_regs() in the middle of
8392 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008393 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008394 * that usually, but some bad designed PV devices (vmware
8395 * backdoor interface) need this to work
8396 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03008397 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008398 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8399 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008400 regs->rax = kvm_rax_read(vcpu);
8401 regs->rbx = kvm_rbx_read(vcpu);
8402 regs->rcx = kvm_rcx_read(vcpu);
8403 regs->rdx = kvm_rdx_read(vcpu);
8404 regs->rsi = kvm_rsi_read(vcpu);
8405 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008406 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008407 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008408#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008409 regs->r8 = kvm_r8_read(vcpu);
8410 regs->r9 = kvm_r9_read(vcpu);
8411 regs->r10 = kvm_r10_read(vcpu);
8412 regs->r11 = kvm_r11_read(vcpu);
8413 regs->r12 = kvm_r12_read(vcpu);
8414 regs->r13 = kvm_r13_read(vcpu);
8415 regs->r14 = kvm_r14_read(vcpu);
8416 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008417#endif
8418
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008419 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008420 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008421}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008422
Ken Hofsass01643c52018-01-31 16:03:36 -08008423int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8424{
8425 vcpu_load(vcpu);
8426 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008427 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008428 return 0;
8429}
8430
Ken Hofsass01643c52018-01-31 16:03:36 -08008431static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008432{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008433 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8434 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8435
Sean Christophersonde3cd112019-04-30 10:36:17 -07008436 kvm_rax_write(vcpu, regs->rax);
8437 kvm_rbx_write(vcpu, regs->rbx);
8438 kvm_rcx_write(vcpu, regs->rcx);
8439 kvm_rdx_write(vcpu, regs->rdx);
8440 kvm_rsi_write(vcpu, regs->rsi);
8441 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008442 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008443 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008444#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008445 kvm_r8_write(vcpu, regs->r8);
8446 kvm_r9_write(vcpu, regs->r9);
8447 kvm_r10_write(vcpu, regs->r10);
8448 kvm_r11_write(vcpu, regs->r11);
8449 kvm_r12_write(vcpu, regs->r12);
8450 kvm_r13_write(vcpu, regs->r13);
8451 kvm_r14_write(vcpu, regs->r14);
8452 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008453#endif
8454
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008455 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008456 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008457
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008458 vcpu->arch.exception.pending = false;
8459
Avi Kivity3842d132010-07-27 12:30:24 +03008460 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008461}
Avi Kivity3842d132010-07-27 12:30:24 +03008462
Ken Hofsass01643c52018-01-31 16:03:36 -08008463int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8464{
8465 vcpu_load(vcpu);
8466 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008467 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008468 return 0;
8469}
8470
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008471void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8472{
8473 struct kvm_segment cs;
8474
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008475 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008476 *db = cs.db;
8477 *l = cs.l;
8478}
8479EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8480
Ken Hofsass01643c52018-01-31 16:03:36 -08008481static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008482{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008483 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008484
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008485 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8486 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8487 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8488 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8489 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8490 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008491
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008492 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8493 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008494
8495 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008496 sregs->idt.limit = dt.size;
8497 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008498 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008499 sregs->gdt.limit = dt.size;
8500 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008501
Avi Kivity4d4ec082009-12-29 18:07:30 +02008502 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008503 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008504 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008505 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008506 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008507 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008508 sregs->apic_base = kvm_get_apic_base(vcpu);
8509
Jordan Borgner0e96f312018-10-28 12:58:28 +00008510 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008511
Liran Alon04140b42018-03-23 03:01:31 +03008512 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008513 set_bit(vcpu->arch.interrupt.nr,
8514 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008515}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008516
Ken Hofsass01643c52018-01-31 16:03:36 -08008517int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8518 struct kvm_sregs *sregs)
8519{
8520 vcpu_load(vcpu);
8521 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008522 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008523 return 0;
8524}
8525
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008526int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8527 struct kvm_mp_state *mp_state)
8528{
Christoffer Dallfd232562017-12-04 21:35:30 +01008529 vcpu_load(vcpu);
8530
Jan Kiszka66450a22013-03-13 12:42:34 +01008531 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308532 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8533 vcpu->arch.pv.pv_unhalted)
8534 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8535 else
8536 mp_state->mp_state = vcpu->arch.mp_state;
8537
Christoffer Dallfd232562017-12-04 21:35:30 +01008538 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008539 return 0;
8540}
8541
8542int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8543 struct kvm_mp_state *mp_state)
8544{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008545 int ret = -EINVAL;
8546
8547 vcpu_load(vcpu);
8548
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008549 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008550 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008551 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008552
David Hildenbrand28bf2882017-03-23 11:46:03 +01008553 /* INITs are latched while in SMM */
8554 if ((is_smm(vcpu) || vcpu->arch.smi_pending) &&
8555 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8556 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008557 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008558
Jan Kiszka66450a22013-03-13 12:42:34 +01008559 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8560 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8561 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8562 } else
8563 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008564 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008565
8566 ret = 0;
8567out:
8568 vcpu_put(vcpu);
8569 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008570}
8571
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008572int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8573 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008574{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008575 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008576 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008577
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008578 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008579
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008580 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008581 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02008582
Gleb Natapovc6975182010-02-18 12:15:01 +02008583 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03008584 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02008585
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008586 kvm_rip_write(vcpu, ctxt->eip);
8587 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008588 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03008589 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02008590}
8591EXPORT_SYMBOL_GPL(kvm_task_switch);
8592
Peng Hao3140c152018-04-02 09:15:32 +08008593static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008594{
Tianyu Lan74fec5b2018-07-23 12:31:21 +00008595 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
8596 (sregs->cr4 & X86_CR4_OSXSAVE))
8597 return -EINVAL;
8598
Tianyu Lan37b95952018-01-16 17:34:07 +08008599 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008600 /*
8601 * When EFER.LME and CR0.PG are set, the processor is in
8602 * 64-bit mode (though maybe in a 32-bit code segment).
8603 * CR4.PAE and EFER.LMA must be set.
8604 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008605 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008606 || !(sregs->efer & EFER_LMA))
8607 return -EINVAL;
8608 } else {
8609 /*
8610 * Not in 64-bit mode: EFER.LMA is clear and the code
8611 * segment cannot be 64-bit.
8612 */
8613 if (sregs->efer & EFER_LMA || sregs->cs.l)
8614 return -EINVAL;
8615 }
8616
8617 return 0;
8618}
8619
Ken Hofsass01643c52018-01-31 16:03:36 -08008620static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008621{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008622 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008623 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008624 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008625 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008626 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008627 int ret = -EINVAL;
8628
Lan Tianyuf2981032017-12-14 03:01:52 -05008629 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08008630 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05008631
Jim Mattsond3802282017-08-10 10:14:13 -07008632 apic_base_msr.data = sregs->apic_base;
8633 apic_base_msr.host_initiated = true;
8634 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008635 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01008636
Gleb Natapov89a27f42010-02-16 10:51:48 +02008637 dt.size = sregs->idt.limit;
8638 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008639 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008640 dt.size = sregs->gdt.limit;
8641 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008642 kvm_x86_ops->set_gdt(vcpu, &dt);
8643
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008644 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008645 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02008646 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02008647 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008648
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008649 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008650
Avi Kivityf6801df2010-01-21 15:31:50 +02008651 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008652 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008653
Avi Kivity4d4ec082009-12-29 18:07:30 +02008654 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008655 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00008656 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008657
Avi Kivityfc78f512009-12-07 12:16:48 +02008658 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05008659 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
8660 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008661 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05008662 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02008663 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008664
8665 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02008666 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02008667 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02008668 mmu_reset_needed = 1;
8669 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008670 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008671
8672 if (mmu_reset_needed)
8673 kvm_mmu_reset_context(vcpu);
8674
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03008675 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03008676 pending_vec = find_first_bit(
8677 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
8678 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008679 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03008680 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008681 }
8682
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008683 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8684 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8685 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8686 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8687 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8688 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008689
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008690 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8691 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008692
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03008693 update_cr8_intercept(vcpu);
8694
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008695 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03008696 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008697 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02008698 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008699 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
8700
Avi Kivity3842d132010-07-27 12:30:24 +03008701 kvm_make_request(KVM_REQ_EVENT, vcpu);
8702
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008703 ret = 0;
8704out:
Ken Hofsass01643c52018-01-31 16:03:36 -08008705 return ret;
8706}
8707
8708int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
8709 struct kvm_sregs *sregs)
8710{
8711 int ret;
8712
8713 vcpu_load(vcpu);
8714 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008715 vcpu_put(vcpu);
8716 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008717}
8718
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008719int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
8720 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008721{
Jan Kiszka355be0b2009-10-03 00:31:21 +02008722 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008723 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008724
Christoffer Dall66b56562017-12-04 21:35:33 +01008725 vcpu_load(vcpu);
8726
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008727 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
8728 r = -EBUSY;
8729 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03008730 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008731 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
8732 kvm_queue_exception(vcpu, DB_VECTOR);
8733 else
8734 kvm_queue_exception(vcpu, BP_VECTOR);
8735 }
8736
Jan Kiszka91586a32009-10-05 13:07:21 +02008737 /*
8738 * Read rflags as long as potentially injected trace flags are still
8739 * filtered out.
8740 */
8741 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008742
8743 vcpu->guest_debug = dbg->control;
8744 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
8745 vcpu->guest_debug = 0;
8746
8747 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008748 for (i = 0; i < KVM_NR_DB_REGS; ++i)
8749 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02008750 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008751 } else {
8752 for (i = 0; i < KVM_NR_DB_REGS; i++)
8753 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008754 }
Jan Kiszkac8639012012-09-21 05:42:55 +02008755 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008756
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008757 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8758 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
8759 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008760
Jan Kiszka91586a32009-10-05 13:07:21 +02008761 /*
8762 * Trigger an rflags update that will inject or remove the trace
8763 * flags.
8764 */
8765 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008766
Paolo Bonzinia96036b2015-11-10 11:55:36 +01008767 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008768
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008769 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008770
Avi Kivity2122ff52010-05-13 11:25:04 +03008771out:
Christoffer Dall66b56562017-12-04 21:35:33 +01008772 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008773 return r;
8774}
8775
8776/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08008777 * Translate a guest virtual address to a guest physical address.
8778 */
8779int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
8780 struct kvm_translation *tr)
8781{
8782 unsigned long vaddr = tr->linear_address;
8783 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008784 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008785
Christoffer Dall1da5b612017-12-04 21:35:32 +01008786 vcpu_load(vcpu);
8787
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008788 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02008789 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008790 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008791 tr->physical_address = gpa;
8792 tr->valid = gpa != UNMAPPED_GVA;
8793 tr->writeable = 1;
8794 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008795
Christoffer Dall1da5b612017-12-04 21:35:32 +01008796 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008797 return 0;
8798}
8799
Hollis Blanchardd0752062007-10-31 17:24:25 -05008800int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8801{
Christoffer Dall13931232017-12-04 21:35:34 +01008802 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008803
Christoffer Dall13931232017-12-04 21:35:34 +01008804 vcpu_load(vcpu);
8805
Marc Orrb666a4b2018-11-06 14:53:56 -08008806 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008807 memcpy(fpu->fpr, fxsave->st_space, 128);
8808 fpu->fcw = fxsave->cwd;
8809 fpu->fsw = fxsave->swd;
8810 fpu->ftwx = fxsave->twd;
8811 fpu->last_opcode = fxsave->fop;
8812 fpu->last_ip = fxsave->rip;
8813 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00008814 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05008815
Christoffer Dall13931232017-12-04 21:35:34 +01008816 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008817 return 0;
8818}
8819
8820int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8821{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01008822 struct fxregs_state *fxsave;
8823
8824 vcpu_load(vcpu);
8825
Marc Orrb666a4b2018-11-06 14:53:56 -08008826 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008827
Hollis Blanchardd0752062007-10-31 17:24:25 -05008828 memcpy(fxsave->st_space, fpu->fpr, 128);
8829 fxsave->cwd = fpu->fcw;
8830 fxsave->swd = fpu->fsw;
8831 fxsave->twd = fpu->ftwx;
8832 fxsave->fop = fpu->last_opcode;
8833 fxsave->rip = fpu->last_ip;
8834 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00008835 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05008836
Christoffer Dall6a96bc72017-12-04 21:35:35 +01008837 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008838 return 0;
8839}
8840
Ken Hofsass01643c52018-01-31 16:03:36 -08008841static void store_regs(struct kvm_vcpu *vcpu)
8842{
8843 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
8844
8845 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
8846 __get_regs(vcpu, &vcpu->run->s.regs.regs);
8847
8848 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
8849 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
8850
8851 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
8852 kvm_vcpu_ioctl_x86_get_vcpu_events(
8853 vcpu, &vcpu->run->s.regs.events);
8854}
8855
8856static int sync_regs(struct kvm_vcpu *vcpu)
8857{
8858 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
8859 return -EINVAL;
8860
8861 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
8862 __set_regs(vcpu, &vcpu->run->s.regs.regs);
8863 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
8864 }
8865 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
8866 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
8867 return -EINVAL;
8868 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
8869 }
8870 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
8871 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
8872 vcpu, &vcpu->run->s.regs.events))
8873 return -EINVAL;
8874 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
8875 }
8876
8877 return 0;
8878}
8879
Ingo Molnar0ee6a512015-04-27 06:58:22 +02008880static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05008881{
Marc Orrb666a4b2018-11-06 14:53:56 -08008882 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02008883 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08008884 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01008885 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008886
Dexuan Cui2acf9232010-06-10 11:27:12 +08008887 /*
8888 * Ensure guest xcr0 is valid for loading
8889 */
Dave Hansend91cab72015-09-02 16:31:26 -07008890 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08008891
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008892 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008893}
Hollis Blanchardd0752062007-10-31 17:24:25 -05008894
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008895void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
8896{
Ido Yarivbd768e12016-10-21 12:39:57 -04008897 void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask;
8898
Glauber Costa12f9a482011-02-01 14:16:40 -05008899 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01008900
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008901 kvm_x86_ops->vcpu_free(vcpu);
Ido Yarivbd768e12016-10-21 12:39:57 -04008902 free_cpumask_var(wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008903}
8904
8905struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
8906 unsigned int id)
8907{
Liang Lic447e762015-05-21 04:41:25 +08008908 struct kvm_vcpu *vcpu;
8909
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01008910 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Zachary Amsden6755bae2010-08-19 22:07:22 -10008911 printk_once(KERN_WARNING
8912 "kvm: SMP vm created on host with unstable TSC; "
8913 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08008914
8915 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
8916
Liang Lic447e762015-05-21 04:41:25 +08008917 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02008918}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008919
Avi Kivity26e52152007-11-20 15:30:24 +02008920int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
8921{
Sean Christopherson0cf91352019-03-07 15:43:02 -08008922 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07008923 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08008924 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01008925 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03008926 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02008927 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008928 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01008929 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008930}
8931
Dominik Dingel31928aa2014-12-04 15:47:07 +01008932void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008933{
Will Auld8fe8ab42012-11-29 12:42:12 -08008934 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01008935 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008936
Roman Kagand3457c82017-07-14 17:13:20 +03008937 kvm_hv_vcpu_postcreate(vcpu);
8938
Christoffer Dallec7660c2017-12-04 21:35:23 +01008939 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01008940 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01008941 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08008942 msr.data = 0x0;
8943 msr.index = MSR_IA32_TSC;
8944 msr.host_initiated = true;
8945 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008946 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03008947
8948 /* poll control enabled by default */
8949 vcpu->arch.msr_kvm_poll_control = 1;
8950
Christoffer Dallec7660c2017-12-04 21:35:23 +01008951 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008952
Marcelo Tosatti630994b2015-05-12 22:42:04 -03008953 if (!kvmclock_periodic_sync)
8954 return;
8955
Andrew Jones332967a2014-02-28 12:52:55 +01008956 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
8957 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02008958}
8959
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06008960void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008961{
Gleb Natapov344d9582010-10-14 11:22:50 +02008962 vcpu->arch.apf.msr_val = 0;
8963
Christoffer Dallec7660c2017-12-04 21:35:23 +01008964 vcpu_load(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008965 kvm_mmu_unload(vcpu);
8966 vcpu_put(vcpu);
8967
8968 kvm_x86_ops->vcpu_free(vcpu);
8969}
8970
Nadav Amitd28bc9d2015-04-13 14:34:08 +03008971void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08008972{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01008973 kvm_lapic_reset(vcpu, init_event);
8974
Paolo Bonzinie69fab52015-06-04 10:44:44 +02008975 vcpu->arch.hflags = 0;
8976
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008977 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02008978 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008979 atomic_set(&vcpu->arch.nmi_queued, 0);
8980 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02008981 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03008982 kvm_clear_interrupt_queue(vcpu);
8983 kvm_clear_exception_queue(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008984 vcpu->arch.exception.pending = false;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02008985
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008986 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03008987 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03008988 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01008989 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008990 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02008991 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008992
Nadav Amit11190222015-04-02 03:10:38 +03008993 vcpu->arch.cr2 = 0;
8994
Avi Kivity3842d132010-07-27 12:30:24 +03008995 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02008996 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04008997 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03008998
Glauber Costa12f9a482011-02-01 14:16:40 -05008999 kvmclock_reset(vcpu);
9000
Gleb Natapovaf585b92010-10-14 11:22:46 +02009001 kvm_clear_async_pf_completion_queue(vcpu);
9002 kvm_async_pf_hash_reset(vcpu);
9003 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009004
Wanpeng Lia554d202017-10-11 05:10:19 -07009005 if (kvm_mpx_supported()) {
9006 void *mpx_state_buffer;
9007
9008 /*
9009 * To avoid have the INIT path from kvm_apic_has_events() that be
9010 * called with loaded FPU and does not let userspace fix the state.
9011 */
Rik van Rielf775b132017-11-14 16:54:23 -05009012 if (init_event)
9013 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009014 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009015 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009016 if (mpx_state_buffer)
9017 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009018 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009019 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009020 if (mpx_state_buffer)
9021 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009022 if (init_event)
9023 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009024 }
9025
Paolo Bonzini64d60672015-05-07 11:36:11 +02009026 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009027 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009028 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009029
Kyle Hueydb2336a2017-03-20 01:16:28 -07009030 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009031
9032 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009033 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009034
Julian Stecklina66f7b722012-12-05 15:26:19 +01009035 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9036 vcpu->arch.regs_avail = ~0;
9037 vcpu->arch.regs_dirty = ~0;
9038
Wanpeng Lia554d202017-10-11 05:10:19 -07009039 vcpu->arch.ia32_xss = 0;
9040
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009041 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009042}
9043
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009044void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009045{
9046 struct kvm_segment cs;
9047
9048 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9049 cs.selector = vector << 8;
9050 cs.base = vector << 12;
9051 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9052 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009053}
9054
Radim Krčmář13a34e02014-08-28 15:13:03 +02009055int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009056{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009057 struct kvm *kvm;
9058 struct kvm_vcpu *vcpu;
9059 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009060 int ret;
9061 u64 local_tsc;
9062 u64 max_tsc = 0;
9063 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009064
9065 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009066 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009067 if (ret != 0)
9068 return ret;
9069
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009070 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009071 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009072 list_for_each_entry(kvm, &vm_list, vm_list) {
9073 kvm_for_each_vcpu(i, vcpu, kvm) {
9074 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009075 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009076 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9077 backwards_tsc = true;
9078 if (vcpu->arch.last_host_tsc > max_tsc)
9079 max_tsc = vcpu->arch.last_host_tsc;
9080 }
9081 }
9082 }
9083
9084 /*
9085 * Sometimes, even reliable TSCs go backwards. This happens on
9086 * platforms that reset TSC during suspend or hibernate actions, but
9087 * maintain synchronization. We must compensate. Fortunately, we can
9088 * detect that condition here, which happens early in CPU bringup,
9089 * before any KVM threads can be running. Unfortunately, we can't
9090 * bring the TSCs fully up to date with real time, as we aren't yet far
9091 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009092 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009093 * variables that haven't been updated yet.
9094 *
9095 * So we simply find the maximum observed TSC above, then record the
9096 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9097 * the adjustment will be applied. Note that we accumulate
9098 * adjustments, in case multiple suspend cycles happen before some VCPU
9099 * gets a chance to run again. In the event that no KVM threads get a
9100 * chance to run, we will miss the entire elapsed period, as we'll have
9101 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9102 * loose cycle time. This isn't too big a deal, since the loss will be
9103 * uniform across all VCPUs (not to mention the scenario is extremely
9104 * unlikely). It is possible that a second hibernate recovery happens
9105 * much faster than a first, causing the observed TSC here to be
9106 * smaller; this would require additional padding adjustment, which is
9107 * why we set last_host_tsc to the local tsc observed here.
9108 *
9109 * N.B. - this code below runs only on platforms with reliable TSC,
9110 * as that is the only way backwards_tsc is set above. Also note
9111 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9112 * have the same delta_cyc adjustment applied if backwards_tsc
9113 * is detected. Note further, this adjustment is only done once,
9114 * as we reset last_host_tsc on all VCPUs to stop this from being
9115 * called multiple times (one for each physical CPU bringup).
9116 *
Guo Chao4a969982012-06-28 15:17:27 +08009117 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009118 * will be compensated by the logic in vcpu_load, which sets the TSC to
9119 * catchup mode. This will catchup all VCPUs to real time, but cannot
9120 * guarantee that they stay in perfect synchronization.
9121 */
9122 if (backwards_tsc) {
9123 u64 delta_cyc = max_tsc - local_tsc;
9124 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009125 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009126 kvm_for_each_vcpu(i, vcpu, kvm) {
9127 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9128 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009129 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009130 }
9131
9132 /*
9133 * We have to disable TSC offset matching.. if you were
9134 * booting a VM while issuing an S4 host suspend....
9135 * you may have some problem. Solving this issue is
9136 * left as an exercise to the reader.
9137 */
9138 kvm->arch.last_tsc_nsec = 0;
9139 kvm->arch.last_tsc_write = 0;
9140 }
9141
9142 }
9143 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009144}
9145
Radim Krčmář13a34e02014-08-28 15:13:03 +02009146void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009147{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009148 kvm_x86_ops->hardware_disable();
9149 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009150}
9151
9152int kvm_arch_hardware_setup(void)
9153{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009154 int r;
9155
9156 r = kvm_x86_ops->hardware_setup();
9157 if (r != 0)
9158 return r;
9159
Haozhong Zhang35181e82015-10-20 15:39:03 +08009160 if (kvm_has_tsc_control) {
9161 /*
9162 * Make sure the user can only configure tsc_khz values that
9163 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009164 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009165 * be 1 on all machines.
9166 */
9167 u64 max = min(0x7fffffffULL,
9168 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9169 kvm_max_guest_tsc_khz = max;
9170
Haozhong Zhangad7218832015-10-20 15:39:02 +08009171 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009172 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009173
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009174 kvm_init_msr_list();
9175 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009176}
9177
9178void kvm_arch_hardware_unsetup(void)
9179{
9180 kvm_x86_ops->hardware_unsetup();
9181}
9182
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009183int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009184{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009185 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009186}
9187
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009188bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9189{
9190 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9191}
9192EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9193
9194bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9195{
9196 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9197}
9198
Gleb Natapov54e98182012-08-05 15:58:32 +03009199struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009200EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009201
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009202int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
9203{
9204 struct page *page;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009205 int r;
9206
Avi Kivity9aabc88f2010-07-29 15:11:50 +03009207 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
David Hildenbrand26de7982017-08-24 20:51:25 +02009208 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03009209 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009210 else
Avi Kivitya4535292008-04-13 17:54:35 +03009211 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009212
9213 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9214 if (!page) {
9215 r = -ENOMEM;
9216 goto fail;
9217 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009218 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009219
Zachary Amsdencc578282012-02-03 15:43:50 -02009220 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10009221
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009222 r = kvm_mmu_create(vcpu);
9223 if (r < 0)
9224 goto fail_free_pio_data;
9225
David Hildenbrand26de7982017-08-24 20:51:25 +02009226 if (irqchip_in_kernel(vcpu->kvm)) {
Paolo Bonzinif7589cc2019-01-30 17:18:59 +01009227 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu);
Sean Christopherson39497d72019-04-17 10:15:32 -07009228 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009229 if (r < 0)
9230 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03009231 } else
9232 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009233
Huang Ying890ca9a2009-05-11 16:48:15 +08009234 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
Ben Gardon254272c2019-02-11 11:02:50 -08009235 GFP_KERNEL_ACCOUNT);
Huang Ying890ca9a2009-05-11 16:48:15 +08009236 if (!vcpu->arch.mce_banks) {
9237 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08009238 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08009239 }
9240 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9241
Ben Gardon254272c2019-02-11 11:02:50 -08009242 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9243 GFP_KERNEL_ACCOUNT)) {
Wei Yongjunf1797352013-04-18 07:41:00 +08009244 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009245 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08009246 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009247
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009248 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01009249
Paolo Bonzini4344ee92013-10-02 16:06:16 +02009250 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02009251
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03009252 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9253
Radim Krčmář74545702015-04-27 15:11:25 +02009254 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9255
Gleb Natapovaf585b92010-10-14 11:22:46 +02009256 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009257 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009258
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02009259 vcpu->arch.pending_external_vector = -1;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009260 vcpu->arch.preempted_in_kernel = false;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02009261
Andrey Smetanin5c9194122015-11-10 15:36:34 +03009262 kvm_hv_vcpu_init(vcpu);
9263
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009264 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009265
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009266fail_free_mce_banks:
9267 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08009268fail_free_lapic:
9269 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009270fail_mmu_destroy:
9271 kvm_mmu_destroy(vcpu);
9272fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009273 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009274fail:
9275 return r;
9276}
9277
9278void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
9279{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009280 int idx;
9281
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009282 kvm_hv_vcpu_uninit(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009283 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08009284 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009285 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009286 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009287 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009288 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009289 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02009290 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03009291 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009292}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009293
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009294void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9295{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009296 vcpu->arch.l1tf_flush_l1d = true;
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009297 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009298}
9299
Carsten Ottee08b9632012-01-04 10:25:20 +01009300int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009301{
Carsten Ottee08b9632012-01-04 10:25:20 +01009302 if (type)
9303 return -EINVAL;
9304
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009305 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009306 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009307 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009308 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009309
Sheng Yang5550af42008-10-15 20:15:06 +08009310 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9311 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009312 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9313 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9314 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009315
Jan Kiszka038f8c12011-02-04 10:49:11 +01009316 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009317 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009318 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9319
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009320 kvm->arch.kvmclock_offset = -ktime_get_boottime_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009321 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009322
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009323 kvm->arch.guest_can_read_msr_platform_info = true;
9324
Andrew Jones7e44e442014-02-28 12:52:54 +01009325 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009326 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009327
Roman Kagancbc02362018-02-01 16:48:31 +03009328 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009329 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009330 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009331
Sean Christopherson92735b12019-08-02 15:06:17 -07009332 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009333}
9334
9335static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9336{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009337 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009338 kvm_mmu_unload(vcpu);
9339 vcpu_put(vcpu);
9340}
9341
9342static void kvm_free_vcpus(struct kvm *kvm)
9343{
9344 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009345 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009346
9347 /*
9348 * Unpin any mmu pages first.
9349 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009350 kvm_for_each_vcpu(i, vcpu, kvm) {
9351 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009352 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009353 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009354 kvm_for_each_vcpu(i, vcpu, kvm)
9355 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009356
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009357 mutex_lock(&kvm->lock);
9358 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9359 kvm->vcpus[i] = NULL;
9360
9361 atomic_set(&kvm->online_vcpus, 0);
9362 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009363}
9364
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009365void kvm_arch_sync_events(struct kvm *kvm)
9366{
Andrew Jones332967a2014-02-28 12:52:55 +01009367 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009368 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009369 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009370}
9371
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009372int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009373{
9374 int i, r;
Paolo Bonzini25188b92015-10-14 15:51:08 +02009375 unsigned long hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009376 struct kvm_memslots *slots = kvm_memslots(kvm);
9377 struct kvm_memory_slot *slot, old;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009378
9379 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009380 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9381 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009382
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009383 slot = id_to_memslot(slots, id);
9384 if (size) {
Paolo Bonzinib21629d2016-06-01 14:09:18 +02009385 if (slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009386 return -EEXIST;
9387
9388 /*
9389 * MAP_SHARED to prevent internal slot pages from being moved
9390 * by fork()/COW.
9391 */
9392 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9393 MAP_SHARED | MAP_ANONYMOUS, 0);
9394 if (IS_ERR((void *)hva))
9395 return PTR_ERR((void *)hva);
9396 } else {
9397 if (!slot->npages)
9398 return 0;
9399
9400 hva = 0;
9401 }
9402
9403 old = *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009404 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009405 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009406
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009407 m.slot = id | (i << 16);
9408 m.flags = 0;
9409 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009410 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009411 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009412 r = __kvm_set_memory_region(kvm, &m);
9413 if (r < 0)
9414 return r;
9415 }
9416
Eric Biggers103c7632018-01-31 17:30:21 -08009417 if (!size)
9418 vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009419
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009420 return 0;
9421}
9422EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9423
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009424int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009425{
9426 int r;
9427
9428 mutex_lock(&kvm->slots_lock);
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009429 r = __x86_set_memory_region(kvm, id, gpa, size);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009430 mutex_unlock(&kvm->slots_lock);
9431
9432 return r;
9433}
9434EXPORT_SYMBOL_GPL(x86_set_memory_region);
9435
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009436void kvm_arch_destroy_vm(struct kvm *kvm)
9437{
Andrew Honig27469d22013-04-18 09:38:14 -07009438 if (current->mm == kvm->mm) {
9439 /*
9440 * Free memory regions allocated on behalf of userspace,
9441 * unless the the memory map has changed due to process exit
9442 * or fd copying.
9443 */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009444 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
9445 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
9446 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
Andrew Honig27469d22013-04-18 09:38:14 -07009447 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009448 if (kvm_x86_ops->vm_destroy)
9449 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009450 kvm_pic_destroy(kvm);
9451 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009452 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009453 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009454 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009455 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009456 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009457 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009458}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009459
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309460void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009461 struct kvm_memory_slot *dont)
9462{
9463 int i;
9464
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009465 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
9466 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009467 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009468 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009469 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009470 if (i == 0)
9471 continue;
9472
9473 if (!dont || free->arch.lpage_info[i - 1] !=
9474 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009475 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009476 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009477 }
9478 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009479
9480 kvm_page_track_free_memslot(free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009481}
9482
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309483int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
9484 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009485{
9486 int i;
9487
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009488 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009489 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009490 unsigned long ugfn;
9491 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009492 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009493
9494 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9495 slot->base_gfn, level) + 1;
9496
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009497 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009498 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009499 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009500 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009501 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009502 if (i == 0)
9503 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009504
Ben Gardon254272c2019-02-11 11:02:50 -08009505 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009506 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009507 goto out_free;
9508
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009509 slot->arch.lpage_info[i - 1] = linfo;
9510
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009511 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009512 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009513 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009514 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009515 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9516 /*
9517 * If the gfn and userspace address are not aligned wrt each
9518 * other, or if explicitly asked to, disable large page
9519 * support for this slot
9520 */
9521 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
9522 !kvm_largepages_enabled()) {
9523 unsigned long j;
9524
9525 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009526 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009527 }
9528 }
9529
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009530 if (kvm_page_track_create_memslot(slot, npages))
9531 goto out_free;
9532
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009533 return 0;
9534
9535out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009536 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009537 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009538 slot->arch.rmap[i] = NULL;
9539 if (i == 0)
9540 continue;
9541
Thomas Huth548ef282015-02-24 21:29:25 +01009542 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009543 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009544 }
9545 return -ENOMEM;
9546}
9547
Sean Christopherson15248252019-02-05 12:54:17 -08009548void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009549{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009550 /*
9551 * memslots->generation has been incremented.
9552 * mmio generation may have reached its maximum value.
9553 */
Sean Christopherson15248252019-02-05 12:54:17 -08009554 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009555}
9556
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009557int kvm_arch_prepare_memory_region(struct kvm *kvm,
9558 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009559 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009560 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009561{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009562 return 0;
9563}
9564
Kai Huang88178fd2015-01-28 10:54:27 +08009565static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9566 struct kvm_memory_slot *new)
9567{
9568 /* Still write protect RO slot */
9569 if (new->flags & KVM_MEM_READONLY) {
9570 kvm_mmu_slot_remove_write_access(kvm, new);
9571 return;
9572 }
9573
9574 /*
9575 * Call kvm_x86_ops dirty logging hooks when they are valid.
9576 *
9577 * kvm_x86_ops->slot_disable_log_dirty is called when:
9578 *
9579 * - KVM_MR_CREATE with dirty logging is disabled
9580 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9581 *
9582 * The reason is, in case of PML, we need to set D-bit for any slots
9583 * with dirty logging disabled in order to eliminate unnecessary GPA
9584 * logging in PML buffer (and potential PML buffer full VMEXT). This
9585 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +08009586 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +08009587 * logging disabled for memory slots.
9588 *
9589 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
9590 * to dirty logging mode.
9591 *
9592 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
9593 *
9594 * In case of write protect:
9595 *
9596 * Write protect all pages for dirty logging.
9597 *
9598 * All the sptes including the large sptes which point to this
9599 * slot are set to readonly. We can not create any new large
9600 * spte on this slot until the end of the logging.
9601 *
9602 * See the comments in fast_page_fault().
9603 */
9604 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
9605 if (kvm_x86_ops->slot_enable_log_dirty)
9606 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
9607 else
9608 kvm_mmu_slot_remove_write_access(kvm, new);
9609 } else {
9610 if (kvm_x86_ops->slot_disable_log_dirty)
9611 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
9612 }
9613}
9614
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009615void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009616 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009617 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009618 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009619 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009620{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08009621 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +08009622 kvm_mmu_change_mmu_pages(kvm,
9623 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +08009624
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009625 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08009626 * Dirty logging tracks sptes in 4k granularity, meaning that large
9627 * sptes have to be split. If live migration is successful, the guest
9628 * in the source machine will be destroyed and large sptes will be
9629 * created in the destination. However, if the guest continues to run
9630 * in the source machine (for example if live migration fails), small
9631 * sptes will remain around and cause bad performance.
9632 *
9633 * Scan sptes if dirty logging has been stopped, dropping those
9634 * which can be collapsed into a single large-page spte. Later
9635 * page faults will create the large-page sptes.
9636 */
9637 if ((change != KVM_MR_DELETE) &&
9638 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
9639 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
9640 kvm_mmu_zap_collapsible_sptes(kvm, new);
9641
9642 /*
Kai Huang88178fd2015-01-28 10:54:27 +08009643 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08009644 *
Kai Huang88178fd2015-01-28 10:54:27 +08009645 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
9646 * been zapped so no dirty logging staff is needed for old slot. For
9647 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
9648 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009649 *
9650 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009651 */
Kai Huang88178fd2015-01-28 10:54:27 +08009652 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009653 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08009654}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009655
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009656void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009657{
Sean Christopherson7390de12019-02-05 13:01:31 -08009658 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009659}
9660
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009661void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
9662 struct kvm_memory_slot *slot)
9663{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +08009664 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009665}
9666
Liran Alone6c67d82018-09-04 10:56:52 +03009667static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
9668{
9669 return (is_guest_mode(vcpu) &&
9670 kvm_x86_ops->guest_apic_has_interrupt &&
9671 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
9672}
9673
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009674static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
9675{
9676 if (!list_empty_careful(&vcpu->async_pf.done))
9677 return true;
9678
9679 if (kvm_apic_has_events(vcpu))
9680 return true;
9681
9682 if (vcpu->arch.pv.pv_unhalted)
9683 return true;
9684
Wanpeng Lia5f01f82017-09-13 04:04:01 -07009685 if (vcpu->arch.exception.pending)
9686 return true;
9687
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009688 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9689 (vcpu->arch.nmi_pending &&
9690 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009691 return true;
9692
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009693 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
9694 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +02009695 return true;
9696
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009697 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +03009698 (kvm_cpu_has_interrupt(vcpu) ||
9699 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009700 return true;
9701
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009702 if (kvm_hv_has_stimer_pending(vcpu))
9703 return true;
9704
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009705 return false;
9706}
9707
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009708int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
9709{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009710 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009711}
Zhang Xiantao57361992007-12-17 14:21:40 +08009712
Wanpeng Li17e433b2019-08-05 10:03:19 +08009713bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
9714{
9715 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
9716 return true;
9717
9718 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9719 kvm_test_request(KVM_REQ_SMI, vcpu) ||
9720 kvm_test_request(KVM_REQ_EVENT, vcpu))
9721 return true;
9722
9723 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
9724 return true;
9725
9726 return false;
9727}
9728
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009729bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
9730{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009731 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009732}
9733
Christoffer Dallb6d33832012-03-08 16:44:24 -05009734int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08009735{
Christoffer Dallb6d33832012-03-08 16:44:24 -05009736 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08009737}
Gleb Natapov78646122009-03-23 12:12:11 +02009738
9739int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
9740{
9741 return kvm_x86_ops->interrupt_allowed(vcpu);
9742}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009743
Nadav Amit82b32772014-11-02 11:54:45 +02009744unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
9745{
9746 if (is_64_bit_mode(vcpu))
9747 return kvm_rip_read(vcpu);
9748 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
9749 kvm_rip_read(vcpu));
9750}
9751EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
9752
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009753bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
9754{
Nadav Amit82b32772014-11-02 11:54:45 +02009755 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009756}
9757EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
9758
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009759unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
9760{
9761 unsigned long rflags;
9762
9763 rflags = kvm_x86_ops->get_rflags(vcpu);
9764 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01009765 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009766 return rflags;
9767}
9768EXPORT_SYMBOL_GPL(kvm_get_rflags);
9769
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009770static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009771{
9772 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009773 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01009774 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009775 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009776}
9777
9778void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
9779{
9780 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03009781 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009782}
9783EXPORT_SYMBOL_GPL(kvm_set_rflags);
9784
Gleb Natapov56028d02010-10-17 18:13:42 +02009785void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
9786{
9787 int r;
9788
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009789 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08009790 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02009791 return;
9792
9793 r = kvm_mmu_reload(vcpu);
9794 if (unlikely(r))
9795 return;
9796
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009797 if (!vcpu->arch.mmu->direct_map &&
9798 work->arch.cr3 != vcpu->arch.mmu->get_cr3(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +08009799 return;
9800
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009801 vcpu->arch.mmu->page_fault(vcpu, work->gva, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +02009802}
9803
Gleb Natapovaf585b92010-10-14 11:22:46 +02009804static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
9805{
9806 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
9807}
9808
9809static inline u32 kvm_async_pf_next_probe(u32 key)
9810{
9811 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
9812}
9813
9814static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9815{
9816 u32 key = kvm_async_pf_hash_fn(gfn);
9817
9818 while (vcpu->arch.apf.gfns[key] != ~0)
9819 key = kvm_async_pf_next_probe(key);
9820
9821 vcpu->arch.apf.gfns[key] = gfn;
9822}
9823
9824static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
9825{
9826 int i;
9827 u32 key = kvm_async_pf_hash_fn(gfn);
9828
9829 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08009830 (vcpu->arch.apf.gfns[key] != gfn &&
9831 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02009832 key = kvm_async_pf_next_probe(key);
9833
9834 return key;
9835}
9836
9837bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9838{
9839 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
9840}
9841
9842static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9843{
9844 u32 i, j, k;
9845
9846 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
9847 while (true) {
9848 vcpu->arch.apf.gfns[i] = ~0;
9849 do {
9850 j = kvm_async_pf_next_probe(j);
9851 if (vcpu->arch.apf.gfns[j] == ~0)
9852 return;
9853 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
9854 /*
9855 * k lies cyclically in ]i,j]
9856 * | i.k.j |
9857 * |....j i.k.| or |.k..j i...|
9858 */
9859 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
9860 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
9861 i = j;
9862 }
9863}
9864
Gleb Natapov7c907052010-10-14 11:22:53 +02009865static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
9866{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02009867
9868 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
9869 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +02009870}
9871
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009872static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
9873{
9874
9875 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
9876 sizeof(u32));
9877}
9878
Paolo Bonzini1dfdb452019-06-05 16:46:44 +02009879static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
9880{
9881 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
9882 return false;
9883
9884 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
9885 (vcpu->arch.apf.send_user_only &&
9886 kvm_x86_ops->get_cpl(vcpu) == 0))
9887 return false;
9888
9889 return true;
9890}
9891
9892bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
9893{
9894 if (unlikely(!lapic_in_kernel(vcpu) ||
9895 kvm_event_needs_reinjection(vcpu) ||
9896 vcpu->arch.exception.pending))
9897 return false;
9898
9899 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
9900 return false;
9901
9902 /*
9903 * If interrupts are off we cannot even use an artificial
9904 * halt state.
9905 */
9906 return kvm_x86_ops->interrupt_allowed(vcpu);
9907}
9908
Gleb Natapovaf585b92010-10-14 11:22:46 +02009909void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
9910 struct kvm_async_pf *work)
9911{
Avi Kivity6389ee92010-11-29 16:12:30 +02009912 struct x86_exception fault;
9913
Gleb Natapov7c907052010-10-14 11:22:53 +02009914 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009915 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +02009916
Paolo Bonzini1dfdb452019-06-05 16:46:44 +02009917 if (kvm_can_deliver_async_pf(vcpu) &&
9918 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +02009919 fault.vector = PF_VECTOR;
9920 fault.error_code_valid = true;
9921 fault.error_code = 0;
9922 fault.nested_page_fault = false;
9923 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07009924 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +02009925 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +02009926 } else {
9927 /*
9928 * It is not possible to deliver a paravirtualized asynchronous
9929 * page fault, but putting the guest in an artificial halt state
9930 * can be beneficial nevertheless: if an interrupt arrives, we
9931 * can deliver it timely and perhaps the guest will schedule
9932 * another process. When the instruction that triggered a page
9933 * fault is retried, hopefully the page will be ready in the host.
9934 */
9935 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +02009936 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02009937}
9938
9939void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
9940 struct kvm_async_pf *work)
9941{
Avi Kivity6389ee92010-11-29 16:12:30 +02009942 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009943 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +02009944
chai wenf2e10662013-10-14 22:22:33 +08009945 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +02009946 work->arch.token = ~0; /* broadcast wakeup */
9947 else
9948 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Wanpeng Li24dccf82017-03-20 21:18:55 -07009949 trace_kvm_async_pf_ready(work->arch.token, work->gva);
Gleb Natapov7c907052010-10-14 11:22:53 +02009950
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009951 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
9952 !apf_get_user(vcpu, &val)) {
9953 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
9954 vcpu->arch.exception.pending &&
9955 vcpu->arch.exception.nr == PF_VECTOR &&
9956 !apf_put_user(vcpu, 0)) {
9957 vcpu->arch.exception.injected = false;
9958 vcpu->arch.exception.pending = false;
9959 vcpu->arch.exception.nr = 0;
9960 vcpu->arch.exception.has_error_code = false;
9961 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -07009962 vcpu->arch.exception.has_payload = false;
9963 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009964 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
9965 fault.vector = PF_VECTOR;
9966 fault.error_code_valid = true;
9967 fault.error_code = 0;
9968 fault.nested_page_fault = false;
9969 fault.address = work->arch.token;
9970 fault.async_page_fault = true;
9971 kvm_inject_page_fault(vcpu, &fault);
9972 }
Gleb Natapov7c907052010-10-14 11:22:53 +02009973 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +08009974 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +03009975 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +02009976}
9977
9978bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
9979{
9980 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
9981 return true;
9982 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -07009983 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009984}
9985
Paolo Bonzini5544eb92015-07-07 15:41:58 +02009986void kvm_arch_start_assignment(struct kvm *kvm)
9987{
9988 atomic_inc(&kvm->arch.assigned_device_count);
9989}
9990EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
9991
9992void kvm_arch_end_assignment(struct kvm *kvm)
9993{
9994 atomic_dec(&kvm->arch.assigned_device_count);
9995}
9996EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
9997
9998bool kvm_arch_has_assigned_device(struct kvm *kvm)
9999{
10000 return atomic_read(&kvm->arch.assigned_device_count);
10001}
10002EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10003
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010004void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10005{
10006 atomic_inc(&kvm->arch.noncoherent_dma_count);
10007}
10008EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10009
10010void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10011{
10012 atomic_dec(&kvm->arch.noncoherent_dma_count);
10013}
10014EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10015
10016bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10017{
10018 return atomic_read(&kvm->arch.noncoherent_dma_count);
10019}
10020EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10021
Alex Williamson14717e22016-05-05 11:58:35 -060010022bool kvm_arch_has_irq_bypass(void)
10023{
Sean Christopherson92735b12019-08-02 15:06:17 -070010024 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010025}
10026
Feng Wu87276882015-09-18 22:29:40 +080010027int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10028 struct irq_bypass_producer *prod)
10029{
10030 struct kvm_kernel_irqfd *irqfd =
10031 container_of(cons, struct kvm_kernel_irqfd, consumer);
10032
Alex Williamson14717e22016-05-05 11:58:35 -060010033 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010034
Alex Williamson14717e22016-05-05 11:58:35 -060010035 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10036 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010037}
10038
10039void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10040 struct irq_bypass_producer *prod)
10041{
10042 int ret;
10043 struct kvm_kernel_irqfd *irqfd =
10044 container_of(cons, struct kvm_kernel_irqfd, consumer);
10045
Feng Wu87276882015-09-18 22:29:40 +080010046 WARN_ON(irqfd->producer != prod);
10047 irqfd->producer = NULL;
10048
10049 /*
10050 * When producer of consumer is unregistered, we change back to
10051 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010052 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010053 * int this case doesn't want to receive the interrupts.
10054 */
10055 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10056 if (ret)
10057 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10058 " fails: %d\n", irqfd->consumer.token, ret);
10059}
10060
10061int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10062 uint32_t guest_irq, bool set)
10063{
Feng Wu87276882015-09-18 22:29:40 +080010064 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10065}
10066
Feng Wu520040142016-01-25 16:53:33 +080010067bool kvm_vector_hashing_enabled(void)
10068{
10069 return vector_hashing;
10070}
10071EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
10072
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010073bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10074{
10075 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10076}
10077EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10078
10079
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010080EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010081EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010082EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10083EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10084EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10085EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010086EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010087EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010088EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010089EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010090EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010091EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010092EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010093EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Radim Krčmář7b462682014-08-21 18:08:09 +020010094EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
Kai Huang843e4332015-01-28 10:54:28 +080010095EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010096EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010097EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10098EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);