blob: 67af887f0b9b15d941928a84208aac9c614f0e51 [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
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700677static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
678{
679 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
680 rsvd_bits(1, 2);
681}
682
Joerg Roedelec92fe42010-09-10 17:30:51 +0200683/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700684 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100685 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200686int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100687{
688 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
689 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
690 int i;
691 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200692 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100693
Joerg Roedelff03a072010-09-10 17:30:57 +0200694 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
695 offset * sizeof(u64), sizeof(pdpte),
696 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100697 if (ret < 0) {
698 ret = 0;
699 goto out;
700 }
701 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400702 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700703 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100704 ret = 0;
705 goto out;
706 }
707 }
708 ret = 1;
709
Joerg Roedelff03a072010-09-10 17:30:57 +0200710 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300711 __set_bit(VCPU_EXREG_PDPTR,
712 (unsigned long *)&vcpu->arch.regs_avail);
713 __set_bit(VCPU_EXREG_PDPTR,
714 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100715out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100716
717 return ret;
718}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100719EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100720
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100721bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200722{
Joerg Roedelff03a072010-09-10 17:30:57 +0200723 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200724 bool changed = true;
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200725 int offset;
726 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200727 int r;
728
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200729 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200730 return false;
731
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300732 if (!test_bit(VCPU_EXREG_PDPTR,
733 (unsigned long *)&vcpu->arch.regs_avail))
734 return true;
735
Paolo Bonzinia5121772017-07-24 18:54:38 +0200736 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
737 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200738 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
739 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200740 if (r < 0)
741 goto out;
Joerg Roedelff03a072010-09-10 17:30:57 +0200742 changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200743out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200744
745 return changed;
746}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100747EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200748
Avi Kivity49a9b072010-06-10 17:02:14 +0300749int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100750{
Sheng Yangaad82702010-05-12 16:40:42 +0800751 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800752 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800753
Avi Kivityf9a48e62010-01-06 19:10:22 +0200754 cr0 |= X86_CR0_ET;
755
Gleb Natapovab344822010-01-21 15:28:46 +0200756#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300757 if (cr0 & 0xffffffff00000000UL)
758 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200759#endif
760
761 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100762
Gleb Natapov0f122442010-04-28 19:15:31 +0300763 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
764 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100765
Gleb Natapov0f122442010-04-28 19:15:31 +0300766 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
767 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100768
769 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
770#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200771 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100772 int cs_db, cs_l;
773
Gleb Natapov0f122442010-04-28 19:15:31 +0300774 if (!is_pae(vcpu))
775 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100776 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300777 if (cs_l)
778 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100779 } else
780#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200781 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200782 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300783 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100784 }
785
Mao, Junjiead756a12012-07-02 01:18:48 +0000786 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
787 return 1;
788
Carsten Ottea03490e2007-10-29 16:09:35 +0100789 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100790
Lai Jiangshand170c412011-02-21 11:21:30 +0800791 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800792 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800793 kvm_async_pf_hash_reset(vcpu);
794 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800795
Sheng Yangaad82702010-05-12 16:40:42 +0800796 if ((cr0 ^ old_cr0) & update_bits)
797 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800798
Laszlo Ersek879ae182015-11-04 12:54:41 +0100799 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
800 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
801 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800802 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
803
Gleb Natapov0f122442010-04-28 19:15:31 +0300804 return 0;
805}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200806EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100807
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200808void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100809{
Avi Kivity49a9b072010-06-10 17:02:14 +0300810 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100811}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200812EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100813
WANG Chao1811d972019-04-12 15:55:39 +0800814void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300815{
816 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
817 !vcpu->guest_xcr0_loaded) {
818 /* kvm_set_xcr() also depends on this */
Paolo Bonzini476b7ad2017-12-13 13:51:32 +0100819 if (vcpu->arch.xcr0 != host_xcr0)
820 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300821 vcpu->guest_xcr0_loaded = 1;
822 }
823}
WANG Chao1811d972019-04-12 15:55:39 +0800824EXPORT_SYMBOL_GPL(kvm_load_guest_xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300825
WANG Chao1811d972019-04-12 15:55:39 +0800826void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300827{
828 if (vcpu->guest_xcr0_loaded) {
829 if (vcpu->arch.xcr0 != host_xcr0)
830 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
831 vcpu->guest_xcr0_loaded = 0;
832 }
833}
WANG Chao1811d972019-04-12 15:55:39 +0800834EXPORT_SYMBOL_GPL(kvm_put_guest_xcr0);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300835
Fengguang Wu69b00492015-01-19 22:33:39 +0800836static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800837{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000838 u64 xcr0 = xcr;
839 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200840 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800841
842 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
843 if (index != XCR_XFEATURE_ENABLED_MASK)
844 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700845 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800846 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700847 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800848 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200849
850 /*
851 * Do not allow the guest to set bits that we do not support
852 * saving. However, xcr0 bit 0 is always set, even if the
853 * emulated CPU does not support XSAVE (see fx_init).
854 */
Dave Hansend91cab72015-09-02 16:31:26 -0700855 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200856 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800857 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200858
Dave Hansend91cab72015-09-02 16:31:26 -0700859 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
860 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000861 return 1;
862
Dave Hansend91cab72015-09-02 16:31:26 -0700863 if (xcr0 & XFEATURE_MASK_AVX512) {
864 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800865 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700866 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800867 return 1;
868 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800869 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000870
Dave Hansend91cab72015-09-02 16:31:26 -0700871 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000872 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800873 return 0;
874}
875
876int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
877{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000878 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
879 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800880 kvm_inject_gp(vcpu, 0);
881 return 1;
882 }
883 return 0;
884}
885EXPORT_SYMBOL_GPL(kvm_set_xcr);
886
Avi Kivitya83b29c2010-06-10 17:02:15 +0300887int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100888{
Avi Kivityfc78f512009-12-07 12:16:48 +0200889 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800890 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800891 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800892
Gleb Natapov0f122442010-04-28 19:15:31 +0300893 if (cr4 & CR4_RESERVED_BITS)
894 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100895
Radim Krčmářd6321d42017-08-05 00:12:49 +0200896 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && (cr4 & X86_CR4_OSXSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800897 return 1;
898
Radim Krčmářd6321d42017-08-05 00:12:49 +0200899 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMEP) && (cr4 & X86_CR4_SMEP))
Yang, Wei Yc68b7342011-06-03 11:13:42 +0800900 return 1;
901
Radim Krčmářd6321d42017-08-05 00:12:49 +0200902 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMAP) && (cr4 & X86_CR4_SMAP))
Feng Wu97ec8c02014-04-01 17:46:34 +0800903 return 1;
904
Radim Krčmářd6321d42017-08-05 00:12:49 +0200905 if (!guest_cpuid_has(vcpu, X86_FEATURE_FSGSBASE) && (cr4 & X86_CR4_FSGSBASE))
Yang, Wei74dc2b42011-06-14 20:10:18 +0800906 return 1;
907
Radim Krčmářd6321d42017-08-05 00:12:49 +0200908 if (!guest_cpuid_has(vcpu, X86_FEATURE_PKU) && (cr4 & X86_CR4_PKE))
Huaitong Hanb9baba82016-03-22 16:51:21 +0800909 return 1;
910
Yu Zhangfd8cb432017-08-24 20:27:56 +0800911 if (!guest_cpuid_has(vcpu, X86_FEATURE_LA57) && (cr4 & X86_CR4_LA57))
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300912 return 1;
Gleb Natapov0f122442010-04-28 19:15:31 +0300913
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200914 if (!guest_cpuid_has(vcpu, X86_FEATURE_UMIP) && (cr4 & X86_CR4_UMIP))
915 return 1;
916
Carsten Ottea03490e2007-10-29 16:09:35 +0100917 if (is_long_mode(vcpu)) {
918 if (!(cr4 & X86_CR4_PAE))
Avi Kivitya2edf572009-05-24 22:19:00 +0300919 return 1;
920 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800921 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200922 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Carsten Ottea03490e2007-10-29 16:09:35 +0100923 kvm_read_cr3(vcpu)))
924 return 1;
925
Mao, Junjiead756a12012-07-02 01:18:48 +0000926 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200927 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000928 return 1;
929
930 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
931 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
932 return 1;
933 }
934
Gleb Natapov0f122442010-04-28 19:15:31 +0300935 if (kvm_x86_ops->set_cr4(vcpu, cr4))
936 return 1;
937
Mao, Junjiead756a12012-07-02 01:18:48 +0000938 if (((cr4 ^ old_cr4) & pdptr_bits) ||
939 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800940 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300941
Huaitong Hanb9baba82016-03-22 16:51:21 +0800942 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200943 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800944
Gleb Natapov0f122442010-04-28 19:15:31 +0300945 return 0;
946}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200947EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100948
Avi Kivity23902182010-06-10 17:02:16 +0300949int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100950{
Junaid Shahidade61e22018-06-27 14:59:15 -0700951 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100952#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700953 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
954
Junaid Shahidade61e22018-06-27 14:59:15 -0700955 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -0700956 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
957 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -0700958 }
Paolo Bonziniac146232014-11-10 13:53:25 +0100959#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +0200960
Avi Kivity9f8fe502010-12-05 17:30:00 +0200961 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -0700962 if (!skip_tlb_flush) {
963 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -0700964 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -0700965 }
Gleb Natapov0f122442010-04-28 19:15:31 +0300966 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200967 }
968
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800969 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -0700970 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +0800971 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200972 else if (is_pae_paging(vcpu) &&
973 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +0300974 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100975
Junaid Shahidade61e22018-06-27 14:59:15 -0700976 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +0300977 vcpu->arch.cr3 = cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +0200978 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Junaid Shahid7c390d32018-06-27 14:59:06 -0700979
Gleb Natapov0f122442010-04-28 19:15:31 +0300980 return 0;
981}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200982EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100983
Andre Przywaraeea1cff2010-12-21 11:12:00 +0100984int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100985{
Gleb Natapov0f122442010-04-28 19:15:31 +0300986 if (cr8 & CR8_RESERVED_BITS)
987 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +0200988 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100989 kvm_lapic_set_tpr(vcpu, cr8);
990 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800991 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300992 return 0;
993}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200994EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100995
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200996unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100997{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200998 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +0100999 return kvm_lapic_get_cr8(vcpu);
1000 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001001 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001002}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001003EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001004
Nadav Amitae561ed2015-04-02 03:10:37 +03001005static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1006{
1007 int i;
1008
1009 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1010 for (i = 0; i < KVM_NR_DB_REGS; i++)
1011 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1012 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1013 }
1014}
1015
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001016static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1017{
1018 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1019 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1020}
1021
Jan Kiszkac8639012012-09-21 05:42:55 +02001022static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1023{
1024 unsigned long dr7;
1025
1026 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1027 dr7 = vcpu->arch.guest_debug_dr7;
1028 else
1029 dr7 = vcpu->arch.dr7;
1030 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001031 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1032 if (dr7 & DR7_BP_EN_MASK)
1033 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001034}
1035
Nadav Amit6f43ed02014-07-15 17:37:46 +03001036static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1037{
1038 u64 fixed = DR6_FIXED_1;
1039
Radim Krčmářd6321d42017-08-05 00:12:49 +02001040 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001041 fixed |= DR6_RTM;
1042 return fixed;
1043}
1044
Gleb Natapov338dbc92010-04-28 19:15:32 +03001045static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001046{
1047 switch (dr) {
1048 case 0 ... 3:
1049 vcpu->arch.db[dr] = val;
1050 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1051 vcpu->arch.eff_db[dr] = val;
1052 break;
1053 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001054 /* fall through */
1055 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001056 if (val & 0xffffffff00000000ULL)
1057 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001058 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001059 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001060 break;
1061 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001062 /* fall through */
1063 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +03001064 if (val & 0xffffffff00000000ULL)
1065 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001066 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001067 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001068 break;
1069 }
1070
1071 return 0;
1072}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001073
1074int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1075{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001076 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001077 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001078 return 1;
1079 }
1080 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001081}
Gleb Natapov020df072010-04-13 10:05:23 +03001082EXPORT_SYMBOL_GPL(kvm_set_dr);
1083
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001084int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001085{
1086 switch (dr) {
1087 case 0 ... 3:
1088 *val = vcpu->arch.db[dr];
1089 break;
1090 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001091 /* fall through */
1092 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001093 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1094 *val = vcpu->arch.dr6;
1095 else
1096 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001097 break;
1098 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001099 /* fall through */
1100 default: /* 7 */
1101 *val = vcpu->arch.dr7;
1102 break;
1103 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001104 return 0;
1105}
Gleb Natapov020df072010-04-13 10:05:23 +03001106EXPORT_SYMBOL_GPL(kvm_get_dr);
1107
Avi Kivity022cd0e2011-11-10 14:57:23 +02001108bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1109{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001110 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001111 u64 data;
1112 int err;
1113
Wei Huangc6702c92015-06-19 13:44:45 +02001114 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001115 if (err)
1116 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001117 kvm_rax_write(vcpu, (u32)data);
1118 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001119 return err;
1120}
1121EXPORT_SYMBOL_GPL(kvm_rdpmc);
1122
Carsten Otte043405e2007-10-10 17:16:19 +02001123/*
1124 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1125 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1126 *
1127 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001128 * capabilities of the host cpu. This capabilities test skips MSRs that are
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001129 * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
1130 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001131 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001132
Carsten Otte043405e2007-10-10 17:16:19 +02001133static u32 msrs_to_save[] = {
1134 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001135 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001136#ifdef CONFIG_X86_64
1137 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1138#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001139 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01001140 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001141 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001142 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1143 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1144 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1145 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1146 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1147 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Jim Mattsone2ada662019-08-21 11:20:04 -07001148 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1149 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1150 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1151 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1152 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1153 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1154 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1155 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1156 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1157 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1158 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1159 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1160 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
1161 MSR_ARCH_PERFMON_PERFCTR0 + 18, MSR_ARCH_PERFMON_PERFCTR0 + 19,
1162 MSR_ARCH_PERFMON_PERFCTR0 + 20, MSR_ARCH_PERFMON_PERFCTR0 + 21,
1163 MSR_ARCH_PERFMON_PERFCTR0 + 22, MSR_ARCH_PERFMON_PERFCTR0 + 23,
1164 MSR_ARCH_PERFMON_PERFCTR0 + 24, MSR_ARCH_PERFMON_PERFCTR0 + 25,
1165 MSR_ARCH_PERFMON_PERFCTR0 + 26, MSR_ARCH_PERFMON_PERFCTR0 + 27,
1166 MSR_ARCH_PERFMON_PERFCTR0 + 28, MSR_ARCH_PERFMON_PERFCTR0 + 29,
1167 MSR_ARCH_PERFMON_PERFCTR0 + 30, MSR_ARCH_PERFMON_PERFCTR0 + 31,
1168 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1169 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1170 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1171 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1172 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1173 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1174 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1175 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1176 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
1177 MSR_ARCH_PERFMON_EVENTSEL0 + 18, MSR_ARCH_PERFMON_EVENTSEL0 + 19,
1178 MSR_ARCH_PERFMON_EVENTSEL0 + 20, MSR_ARCH_PERFMON_EVENTSEL0 + 21,
1179 MSR_ARCH_PERFMON_EVENTSEL0 + 22, MSR_ARCH_PERFMON_EVENTSEL0 + 23,
1180 MSR_ARCH_PERFMON_EVENTSEL0 + 24, MSR_ARCH_PERFMON_EVENTSEL0 + 25,
1181 MSR_ARCH_PERFMON_EVENTSEL0 + 26, MSR_ARCH_PERFMON_EVENTSEL0 + 27,
1182 MSR_ARCH_PERFMON_EVENTSEL0 + 28, MSR_ARCH_PERFMON_EVENTSEL0 + 29,
1183 MSR_ARCH_PERFMON_EVENTSEL0 + 30, MSR_ARCH_PERFMON_EVENTSEL0 + 31,
Carsten Otte043405e2007-10-10 17:16:19 +02001184};
1185
1186static unsigned num_msrs_to_save;
1187
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001188static u32 emulated_msrs[] = {
1189 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1190 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1191 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1192 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001193 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001194 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1195 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001196 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001197 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001198 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001199 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001200 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001201 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001202 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1203 HV_X64_MSR_TSC_EMULATION_STATUS,
1204
1205 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001206 MSR_KVM_PV_EOI_EN,
1207
Will Auldba904632012-11-29 12:42:50 -08001208 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001209 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001210 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001211 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001212 MSR_IA32_MCG_STATUS,
1213 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001214 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001215 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001216 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001217 MSR_PLATFORM_INFO,
1218 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001219 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001220 MSR_IA32_POWER_CTL,
Borislav Petkov191c8132019-04-18 18:32:50 +02001221
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001222 /*
1223 * The following list leaves out MSRs whose values are determined
1224 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1225 * We always support the "true" VMX control MSRs, even if the host
1226 * processor does not, so I am putting these registers here rather
1227 * than in msrs_to_save.
1228 */
1229 MSR_IA32_VMX_BASIC,
1230 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1231 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1232 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1233 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1234 MSR_IA32_VMX_MISC,
1235 MSR_IA32_VMX_CR0_FIXED0,
1236 MSR_IA32_VMX_CR4_FIXED0,
1237 MSR_IA32_VMX_VMCS_ENUM,
1238 MSR_IA32_VMX_PROCBASED_CTLS2,
1239 MSR_IA32_VMX_EPT_VPID_CAP,
1240 MSR_IA32_VMX_VMFUNC,
1241
Borislav Petkov191c8132019-04-18 18:32:50 +02001242 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001243 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001244};
1245
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001246static unsigned num_emulated_msrs;
1247
Tom Lendacky801e4592018-02-21 13:39:51 -06001248/*
1249 * List of msr numbers which are used to expose MSR-based features that
1250 * can be used by a hypervisor to validate requested CPU features.
1251 */
1252static u32 msr_based_features[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001253 MSR_IA32_VMX_BASIC,
1254 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1255 MSR_IA32_VMX_PINBASED_CTLS,
1256 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1257 MSR_IA32_VMX_PROCBASED_CTLS,
1258 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1259 MSR_IA32_VMX_EXIT_CTLS,
1260 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1261 MSR_IA32_VMX_ENTRY_CTLS,
1262 MSR_IA32_VMX_MISC,
1263 MSR_IA32_VMX_CR0_FIXED0,
1264 MSR_IA32_VMX_CR0_FIXED1,
1265 MSR_IA32_VMX_CR4_FIXED0,
1266 MSR_IA32_VMX_CR4_FIXED1,
1267 MSR_IA32_VMX_VMCS_ENUM,
1268 MSR_IA32_VMX_PROCBASED_CTLS2,
1269 MSR_IA32_VMX_EPT_VPID_CAP,
1270 MSR_IA32_VMX_VMFUNC,
1271
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001272 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001273 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001274 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001275};
1276
1277static unsigned int num_msr_based_features;
1278
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001279static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001280{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001281 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001282
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001283 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1284 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001285
1286 /*
1287 * If we're doing cache flushes (either "always" or "cond")
1288 * we will do one whenever the guest does a vmlaunch/vmresume.
1289 * If an outer hypervisor is doing the cache flush for us
1290 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1291 * capability to the guest too, and if EPT is disabled we're not
1292 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1293 * require a nested hypervisor to do a flush of its own.
1294 */
1295 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1296 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1297
Paolo Bonzini0c549142019-08-19 17:24:07 +02001298 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1299 data |= ARCH_CAP_RDCL_NO;
1300 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1301 data |= ARCH_CAP_SSB_NO;
1302 if (!boot_cpu_has_bug(X86_BUG_MDS))
1303 data |= ARCH_CAP_MDS_NO;
1304
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001305 return data;
1306}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001307
Wanpeng Li66421c12018-02-28 14:03:30 +08001308static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1309{
1310 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001311 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001312 msr->data = kvm_get_arch_capabilities();
1313 break;
1314 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001315 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001316 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001317 default:
1318 if (kvm_x86_ops->get_msr_feature(msr))
1319 return 1;
1320 }
1321 return 0;
1322}
1323
Tom Lendacky801e4592018-02-21 13:39:51 -06001324static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1325{
1326 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001327 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001328
1329 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001330 r = kvm_get_msr_feature(&msr);
1331 if (r)
1332 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001333
1334 *data = msr.data;
1335
1336 return 0;
1337}
1338
Sean Christopherson11988492019-04-02 08:19:15 -07001339static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1340{
1341 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1342 return false;
1343
1344 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1345 return false;
1346
Sean Christopherson0a629562019-04-02 08:19:16 -07001347 if (efer & (EFER_LME | EFER_LMA) &&
1348 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1349 return false;
1350
1351 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1352 return false;
1353
Sean Christopherson11988492019-04-02 08:19:15 -07001354 return true;
1355
1356}
Jan Kiszka384bb782013-04-20 10:52:36 +02001357bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001358{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001359 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001360 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001361
Sean Christopherson11988492019-04-02 08:19:15 -07001362 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001363}
1364EXPORT_SYMBOL_GPL(kvm_valid_efer);
1365
Sean Christopherson11988492019-04-02 08:19:15 -07001366static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001367{
1368 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001369 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001370
Sean Christopherson11988492019-04-02 08:19:15 -07001371 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001372 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001373
Sean Christopherson11988492019-04-02 08:19:15 -07001374 if (!msr_info->host_initiated) {
1375 if (!__kvm_valid_efer(vcpu, efer))
1376 return 1;
1377
1378 if (is_paging(vcpu) &&
1379 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1380 return 1;
1381 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001382
Carsten Otte15c4a642007-10-30 18:44:17 +01001383 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001384 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001385
Sheng Yanga3d204e2010-05-12 16:40:40 +08001386 kvm_x86_ops->set_efer(vcpu, efer);
1387
Sheng Yangaad82702010-05-12 16:40:42 +08001388 /* Update reserved bits */
1389 if ((efer ^ old_efer) & EFER_NX)
1390 kvm_mmu_reset_context(vcpu);
1391
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001392 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001393}
1394
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001395void kvm_enable_efer_bits(u64 mask)
1396{
1397 efer_reserved_bits &= ~mask;
1398}
1399EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1400
Carsten Otte15c4a642007-10-30 18:44:17 +01001401/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001402 * Write @data into the MSR specified by @index. Select MSR specific fault
1403 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001404 * Returns 0 on success, non-0 otherwise.
1405 * Assumes vcpu_load() was already called.
1406 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001407static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1408 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001409{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001410 struct msr_data msr;
1411
1412 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001413 case MSR_FS_BASE:
1414 case MSR_GS_BASE:
1415 case MSR_KERNEL_GS_BASE:
1416 case MSR_CSTAR:
1417 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001418 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001419 return 1;
1420 break;
1421 case MSR_IA32_SYSENTER_EIP:
1422 case MSR_IA32_SYSENTER_ESP:
1423 /*
1424 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1425 * non-canonical address is written on Intel but not on
1426 * AMD (which ignores the top 32-bits, because it does
1427 * not implement 64-bit SYSENTER).
1428 *
1429 * 64-bit code should hence be able to write a non-canonical
1430 * value on AMD. Making the address canonical ensures that
1431 * vmentry does not fail on Intel after writing a non-canonical
1432 * value, and that something deterministic happens if the guest
1433 * invokes 64-bit SYSENTER.
1434 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001435 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001436 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001437
1438 msr.data = data;
1439 msr.index = index;
1440 msr.host_initiated = host_initiated;
1441
1442 return kvm_x86_ops->set_msr(vcpu, &msr);
1443}
1444
1445/*
1446 * Read the MSR specified by @index into @data. Select MSR specific fault
1447 * checks are bypassed if @host_initiated is %true.
1448 * Returns 0 on success, non-0 otherwise.
1449 * Assumes vcpu_load() was already called.
1450 */
1451static int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1452 bool host_initiated)
1453{
1454 struct msr_data msr;
1455 int ret;
1456
1457 msr.index = index;
1458 msr.host_initiated = host_initiated;
1459
1460 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1461 if (!ret)
1462 *data = msr.data;
1463 return ret;
1464}
1465
1466int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1467{
1468 return __kvm_get_msr(vcpu, index, data, false);
1469}
1470EXPORT_SYMBOL_GPL(kvm_get_msr);
1471
1472int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1473{
1474 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001475}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001476EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001477
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001478int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1479{
1480 u32 ecx = kvm_rcx_read(vcpu);
1481 u64 data;
1482
1483 if (kvm_get_msr(vcpu, ecx, &data)) {
1484 trace_kvm_msr_read_ex(ecx);
1485 kvm_inject_gp(vcpu, 0);
1486 return 1;
1487 }
1488
1489 trace_kvm_msr_read(ecx, data);
1490
1491 kvm_rax_write(vcpu, data & -1u);
1492 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1493 return kvm_skip_emulated_instruction(vcpu);
1494}
1495EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1496
1497int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1498{
1499 u32 ecx = kvm_rcx_read(vcpu);
1500 u64 data = kvm_read_edx_eax(vcpu);
1501
1502 if (kvm_set_msr(vcpu, ecx, data)) {
1503 trace_kvm_msr_write_ex(ecx, data);
1504 kvm_inject_gp(vcpu, 0);
1505 return 1;
1506 }
1507
1508 trace_kvm_msr_write(ecx, data);
1509 return kvm_skip_emulated_instruction(vcpu);
1510}
1511EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1512
Carsten Otte313a3dc2007-10-11 19:16:52 +02001513/*
1514 * Adapt set_msr() to msr_io()'s calling convention
1515 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001516static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1517{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001518 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001519}
1520
Carsten Otte313a3dc2007-10-11 19:16:52 +02001521static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1522{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001523 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001524}
1525
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001526#ifdef CONFIG_X86_64
1527struct pvclock_gtod_data {
1528 seqcount_t seq;
1529
1530 struct { /* extract of a clocksource struct */
1531 int vclock_mode;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001532 u64 cycle_last;
1533 u64 mask;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001534 u32 mult;
1535 u32 shift;
1536 } clock;
1537
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001538 u64 boot_ns;
1539 u64 nsec_base;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001540 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001541};
1542
1543static struct pvclock_gtod_data pvclock_gtod_data;
1544
1545static void update_pvclock_gtod(struct timekeeper *tk)
1546{
1547 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001548 u64 boot_ns;
1549
Peter Zijlstra876e7882015-03-19 10:09:06 +01001550 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001551
1552 write_seqcount_begin(&vdata->seq);
1553
1554 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001555 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1556 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1557 vdata->clock.mask = tk->tkr_mono.mask;
1558 vdata->clock.mult = tk->tkr_mono.mult;
1559 vdata->clock.shift = tk->tkr_mono.shift;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001560
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00001561 vdata->boot_ns = boot_ns;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001562 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001563
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001564 vdata->wall_time_sec = tk->xtime_sec;
1565
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001566 write_seqcount_end(&vdata->seq);
1567}
1568#endif
1569
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001570void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1571{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001572 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001573 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001574}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001575
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001576static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1577{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001578 int version;
1579 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001580 struct pvclock_wall_clock wc;
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001581 struct timespec64 boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001582
1583 if (!wall_clock)
1584 return;
1585
Avi Kivity9ed3c442010-05-04 15:00:37 +03001586 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1587 if (r)
1588 return;
1589
1590 if (version & 1)
1591 ++version; /* first time write, random junk */
1592
1593 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001594
Nicholas Krause1dab1342015-12-30 13:08:46 -05001595 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1596 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001597
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001598 /*
1599 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001600 * system time (updated by kvm_guest_time_update below) to the
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001601 * wall clock specified here. guest system time equals host
1602 * system time for us, thus we must fill in host boot time here.
1603 */
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001604 getboottime64(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001605
Bruce Rogers4b648662012-07-20 10:44:24 -06001606 if (kvm->arch.kvmclock_offset) {
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001607 struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
1608 boot = timespec64_sub(boot, ts);
Bruce Rogers4b648662012-07-20 10:44:24 -06001609 }
Arnd Bergmann87aeb542016-06-17 17:48:56 +02001610 wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001611 wc.nsec = boot.tv_nsec;
1612 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001613
1614 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1615
1616 version++;
1617 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001618}
1619
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001620static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1621{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001622 do_shl32_div32(dividend, divisor);
1623 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001624}
1625
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001626static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001627 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001628{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001629 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001630 int32_t shift = 0;
1631 uint64_t tps64;
1632 uint32_t tps32;
1633
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001634 tps64 = base_hz;
1635 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001636 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001637 tps64 >>= 1;
1638 shift--;
1639 }
1640
1641 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001642 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1643 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001644 scaled64 >>= 1;
1645 else
1646 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001647 shift++;
1648 }
1649
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001650 *pshift = shift;
1651 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001652}
1653
Marcelo Tosattid8281992012-11-27 23:29:01 -02001654#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001655static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001656#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001657
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001658static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001659static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001660
Zachary Amsdencc578282012-02-03 15:43:50 -02001661static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001662{
Zachary Amsdencc578282012-02-03 15:43:50 -02001663 u64 v = (u64)khz * (1000000 + ppm);
1664 do_div(v, 1000000);
1665 return v;
1666}
1667
Haozhong Zhang381d5852015-10-20 15:39:04 +08001668static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1669{
1670 u64 ratio;
1671
1672 /* Guest TSC same frequency as host TSC? */
1673 if (!scale) {
1674 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1675 return 0;
1676 }
1677
1678 /* TSC scaling supported? */
1679 if (!kvm_has_tsc_control) {
1680 if (user_tsc_khz > tsc_khz) {
1681 vcpu->arch.tsc_catchup = 1;
1682 vcpu->arch.tsc_always_catchup = 1;
1683 return 0;
1684 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001685 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001686 return -1;
1687 }
1688 }
1689
1690 /* TSC scaling required - calculate ratio */
1691 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1692 user_tsc_khz, tsc_khz);
1693
1694 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001695 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1696 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001697 return -1;
1698 }
1699
1700 vcpu->arch.tsc_scaling_ratio = ratio;
1701 return 0;
1702}
1703
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001704static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001705{
1706 u32 thresh_lo, thresh_hi;
1707 int use_scaling = 0;
1708
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001709 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001710 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001711 /* set tsc_scaling_ratio to a safe value */
1712 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001713 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001714 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001715
Zachary Amsdenc2855452010-09-18 14:38:15 -10001716 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001717 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001718 &vcpu->arch.virtual_tsc_shift,
1719 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001720 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001721
1722 /*
1723 * Compute the variation in TSC rate which is acceptable
1724 * within the range of tolerance and decide if the
1725 * rate being applied is within that bounds of the hardware
1726 * rate. If so, no scaling or compensation need be done.
1727 */
1728 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1729 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001730 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1731 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 -02001732 use_scaling = 1;
1733 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001734 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001735}
1736
1737static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1738{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001739 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001740 vcpu->arch.virtual_tsc_mult,
1741 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001742 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001743 return tsc;
1744}
1745
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001746static inline int gtod_is_based_on_tsc(int mode)
1747{
1748 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1749}
1750
Fengguang Wu69b00492015-01-19 22:33:39 +08001751static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001752{
1753#ifdef CONFIG_X86_64
1754 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001755 struct kvm_arch *ka = &vcpu->kvm->arch;
1756 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1757
1758 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1759 atomic_read(&vcpu->kvm->online_vcpus));
1760
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001761 /*
1762 * Once the masterclock is enabled, always perform request in
1763 * order to update it.
1764 *
1765 * In order to enable masterclock, the host clocksource must be TSC
1766 * and the vcpus need to have matched TSCs. When that happens,
1767 * perform request to enable masterclock.
1768 */
1769 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001770 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001771 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1772
1773 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1774 atomic_read(&vcpu->kvm->online_vcpus),
1775 ka->use_master_clock, gtod->clock.vclock_mode);
1776#endif
1777}
1778
Will Auldba904632012-11-29 12:42:50 -08001779static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1780{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001781 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001782 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1783}
1784
Haozhong Zhang35181e82015-10-20 15:39:03 +08001785/*
1786 * Multiply tsc by a fixed point number represented by ratio.
1787 *
1788 * The most significant 64-N bits (mult) of ratio represent the
1789 * integral part of the fixed point number; the remaining N bits
1790 * (frac) represent the fractional part, ie. ratio represents a fixed
1791 * point number (mult + frac * 2^(-N)).
1792 *
1793 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1794 */
1795static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1796{
1797 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1798}
1799
1800u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1801{
1802 u64 _tsc = tsc;
1803 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1804
1805 if (ratio != kvm_default_tsc_scaling_ratio)
1806 _tsc = __scale_tsc(ratio, tsc);
1807
1808 return _tsc;
1809}
1810EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1811
Haozhong Zhang07c14192015-10-20 15:39:05 +08001812static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1813{
1814 u64 tsc;
1815
1816 tsc = kvm_scale_tsc(vcpu, rdtsc());
1817
1818 return target_tsc - tsc;
1819}
1820
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001821u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1822{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001823 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1824
1825 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001826}
1827EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1828
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001829static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1830{
Leonid Shatz326e7422018-11-06 12:14:25 +02001831 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001832}
1833
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001834static inline bool kvm_check_tsc_unstable(void)
1835{
1836#ifdef CONFIG_X86_64
1837 /*
1838 * TSC is marked unstable when we're running on Hyper-V,
1839 * 'TSC page' clocksource is good.
1840 */
1841 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1842 return false;
1843#endif
1844 return check_tsc_unstable();
1845}
1846
Will Auld8fe8ab42012-11-29 12:42:12 -08001847void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001848{
1849 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001850 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001851 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001852 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001853 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001854 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001855 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001856
Jan Kiszka038f8c12011-02-04 10:49:11 +01001857 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001858 offset = kvm_compute_tsc_offset(vcpu, data);
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02001859 ns = ktime_get_boottime_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001860 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001861
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001862 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001863 if (data == 0 && msr->host_initiated) {
1864 /*
1865 * detection of vcpu initialization -- need to sync
1866 * with other vCPUs. This particularly helps to keep
1867 * kvm_clock stable after CPU hotplug
1868 */
1869 synchronizing = true;
1870 } else {
1871 u64 tsc_exp = kvm->arch.last_tsc_write +
1872 nsec_to_cycles(vcpu, elapsed);
1873 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1874 /*
1875 * Special case: TSC write with a small delta (1 second)
1876 * of virtual cycle time against real time is
1877 * interpreted as an attempt to synchronize the CPU.
1878 */
1879 synchronizing = data < tsc_exp + tsc_hz &&
1880 data + tsc_hz > tsc_exp;
1881 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001882 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001883
1884 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001885 * For a reliable TSC, we can match TSC offsets, and for an unstable
1886 * TSC, we add elapsed time in this computation. We could let the
1887 * compensation code attempt to catch up if we fall behind, but
1888 * it's better to try to match offsets from the beginning.
1889 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001890 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001891 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001892 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02001893 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001894 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01001895 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001896 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08001897 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001898 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001899 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001900 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001901 } else {
1902 /*
1903 * We split periods of matched TSC writes into generations.
1904 * For each generation, we track the original measured
1905 * nanosecond time, offset, and write, so if TSCs are in
1906 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08001907 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02001908 *
1909 * These values are tracked in kvm->arch.cur_xxx variables.
1910 */
1911 kvm->arch.cur_tsc_generation++;
1912 kvm->arch.cur_tsc_nsec = ns;
1913 kvm->arch.cur_tsc_write = data;
1914 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001915 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001916 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02001917
1918 /*
1919 * We also track th most recent recorded KHZ, write and time to
1920 * allow the matching interval to be extended at each write.
1921 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001922 kvm->arch.last_tsc_nsec = ns;
1923 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001924 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001925
Zachary Amsdenb183aa52012-02-03 15:43:53 -02001926 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02001927
1928 /* Keep track of which generation this VCPU has synchronized to */
1929 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1930 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1931 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1932
Radim Krčmářd6321d42017-08-05 00:12:49 +02001933 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08001934 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02001935
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001936 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001937 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001938
1939 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001940 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001941 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001942 } else if (!already_matched) {
1943 kvm->arch.nr_vcpus_matched_tsc++;
1944 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001945
1946 kvm_track_tsc_matching(vcpu);
1947 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001948}
Zachary Amsdene26101b2012-02-03 15:43:57 -02001949
Zachary Amsden99e3e302010-08-19 22:07:17 -10001950EXPORT_SYMBOL_GPL(kvm_write_tsc);
1951
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001952static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1953 s64 adjustment)
1954{
Leonid Shatz326e7422018-11-06 12:14:25 +02001955 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1956 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001957}
1958
1959static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1960{
1961 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1962 WARN_ON(adjustment < 0);
1963 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01001964 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001965}
1966
Marcelo Tosattid8281992012-11-27 23:29:01 -02001967#ifdef CONFIG_X86_64
1968
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001969static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001970{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001971 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02001972 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001973
1974 if (likely(ret >= last))
1975 return ret;
1976
1977 /*
1978 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001979 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02001980 * very likely) and there's a data dependence, so force GCC
1981 * to generate a branch instead. I don't barrier() because
1982 * we don't actually need a barrier, and if this function
1983 * ever gets inlined it will generate worse code.
1984 */
1985 asm volatile ("");
1986 return last;
1987}
1988
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001989static inline u64 vgettsc(u64 *tsc_timestamp, int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02001990{
1991 long v;
1992 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001993 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02001994
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001995 switch (gtod->clock.vclock_mode) {
1996 case VCLOCK_HVCLOCK:
1997 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
1998 tsc_timestamp);
1999 if (tsc_pg_val != U64_MAX) {
2000 /* TSC page valid */
2001 *mode = VCLOCK_HVCLOCK;
2002 v = (tsc_pg_val - gtod->clock.cycle_last) &
2003 gtod->clock.mask;
2004 } else {
2005 /* TSC page invalid */
2006 *mode = VCLOCK_NONE;
2007 }
2008 break;
2009 case VCLOCK_TSC:
2010 *mode = VCLOCK_TSC;
2011 *tsc_timestamp = read_tsc();
2012 v = (*tsc_timestamp - gtod->clock.cycle_last) &
2013 gtod->clock.mask;
2014 break;
2015 default:
2016 *mode = VCLOCK_NONE;
2017 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002018
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002019 if (*mode == VCLOCK_NONE)
2020 *tsc_timestamp = v = 0;
2021
Marcelo Tosattid8281992012-11-27 23:29:01 -02002022 return v * gtod->clock.mult;
2023}
2024
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002025static int do_monotonic_boot(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002026{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002027 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002028 unsigned long seq;
2029 int mode;
2030 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002031
Marcelo Tosattid8281992012-11-27 23:29:01 -02002032 do {
2033 seq = read_seqcount_begin(&gtod->seq);
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002034 ns = gtod->nsec_base;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002035 ns += vgettsc(tsc_timestamp, &mode);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002036 ns >>= gtod->clock.shift;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002037 ns += gtod->boot_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002038 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002039 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002040
2041 return mode;
2042}
2043
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002044static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002045{
2046 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2047 unsigned long seq;
2048 int mode;
2049 u64 ns;
2050
2051 do {
2052 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002053 ts->tv_sec = gtod->wall_time_sec;
2054 ns = gtod->nsec_base;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002055 ns += vgettsc(tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002056 ns >>= gtod->clock.shift;
2057 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2058
2059 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2060 ts->tv_nsec = ns;
2061
2062 return mode;
2063}
2064
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002065/* returns true if host is using TSC based clocksource */
2066static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002067{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002068 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002069 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002070 return false;
2071
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002072 return gtod_is_based_on_tsc(do_monotonic_boot(kernel_ns,
2073 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002074}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002075
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002076/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002077static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002078 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002079{
2080 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002081 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002082 return false;
2083
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002084 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002085}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002086#endif
2087
2088/*
2089 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002090 * Assuming a stable TSC across physical CPUS, and a stable TSC
2091 * across virtual CPUs, the following condition is possible.
2092 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002093 * CPUs at the next numbered event.
2094 *
2095 * "timespecX" represents host monotonic time. "tscX" represents
2096 * RDTSC value.
2097 *
2098 * VCPU0 on CPU0 | VCPU1 on CPU1
2099 *
2100 * 1. read timespec0,tsc0
2101 * 2. | timespec1 = timespec0 + N
2102 * | tsc1 = tsc0 + M
2103 * 3. transition to guest | transition to guest
2104 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2105 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2106 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2107 *
2108 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2109 *
2110 * - ret0 < ret1
2111 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2112 * ...
2113 * - 0 < N - M => M < N
2114 *
2115 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2116 * always the case (the difference between two distinct xtime instances
2117 * might be smaller then the difference between corresponding TSC reads,
2118 * when updating guest vcpus pvclock areas).
2119 *
2120 * To avoid that problem, do not allow visibility of distinct
2121 * system_timestamp/tsc_timestamp values simultaneously: use a master
2122 * copy of host monotonic time values. Update that master copy
2123 * in lockstep.
2124 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002125 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002126 *
2127 */
2128
2129static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2130{
2131#ifdef CONFIG_X86_64
2132 struct kvm_arch *ka = &kvm->arch;
2133 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002134 bool host_tsc_clocksource, vcpus_matched;
2135
2136 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2137 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002138
2139 /*
2140 * If the host uses TSC clock, then passthrough TSC as stable
2141 * to the guest.
2142 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002143 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002144 &ka->master_kernel_ns,
2145 &ka->master_cycle_now);
2146
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002147 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002148 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002149 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002150
Marcelo Tosattid8281992012-11-27 23:29:01 -02002151 if (ka->use_master_clock)
2152 atomic_set(&kvm_guest_has_master_clock, 1);
2153
2154 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002155 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2156 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002157#endif
2158}
2159
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002160void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2161{
2162 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2163}
2164
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002165static void kvm_gen_update_masterclock(struct kvm *kvm)
2166{
2167#ifdef CONFIG_X86_64
2168 int i;
2169 struct kvm_vcpu *vcpu;
2170 struct kvm_arch *ka = &kvm->arch;
2171
2172 spin_lock(&ka->pvclock_gtod_sync_lock);
2173 kvm_make_mclock_inprogress_request(kvm);
2174 /* no guest entries from this point */
2175 pvclock_update_vm_gtod_copy(kvm);
2176
2177 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002178 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002179
2180 /* guest entries allowed */
2181 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002182 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002183
2184 spin_unlock(&ka->pvclock_gtod_sync_lock);
2185#endif
2186}
2187
Marcelo Tosattie891a322017-04-17 12:51:37 -03002188u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002189{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002190 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002191 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002192 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002193
Paolo Bonzini8b953442016-11-16 18:31:30 +01002194 spin_lock(&ka->pvclock_gtod_sync_lock);
2195 if (!ka->use_master_clock) {
2196 spin_unlock(&ka->pvclock_gtod_sync_lock);
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002197 return ktime_get_boottime_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002198 }
2199
Paolo Bonzini8b953442016-11-16 18:31:30 +01002200 hv_clock.tsc_timestamp = ka->master_cycle_now;
2201 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2202 spin_unlock(&ka->pvclock_gtod_sync_lock);
2203
Wanpeng Lie2c22062017-05-11 18:12:05 -07002204 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2205 get_cpu();
2206
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002207 if (__this_cpu_read(cpu_tsc_khz)) {
2208 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2209 &hv_clock.tsc_shift,
2210 &hv_clock.tsc_to_system_mul);
2211 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2212 } else
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002213 ret = ktime_get_boottime_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002214
2215 put_cpu();
2216
2217 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002218}
2219
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002220static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2221{
2222 struct kvm_vcpu_arch *vcpu = &v->arch;
2223 struct pvclock_vcpu_time_info guest_hv_clock;
2224
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002225 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002226 &guest_hv_clock, sizeof(guest_hv_clock))))
2227 return;
2228
2229 /* This VCPU is paused, but it's legal for a guest to read another
2230 * VCPU's kvmclock, so we really have to follow the specification where
2231 * it says that version is odd if data is being modified, and even after
2232 * it is consistent.
2233 *
2234 * Version field updates must be kept separate. This is because
2235 * kvm_write_guest_cached might use a "rep movs" instruction, and
2236 * writes within a string instruction are weakly ordered. So there
2237 * are three writes overall.
2238 *
2239 * As a small optimization, only write the version field in the first
2240 * and third write. The vcpu->pv_time cache is still valid, because the
2241 * version field is the first in the struct.
2242 */
2243 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2244
Liran Alon51c4b8b2017-11-05 16:11:30 +02002245 if (guest_hv_clock.version & 1)
2246 ++guest_hv_clock.version; /* first time write, random junk */
2247
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002248 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002249 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2250 &vcpu->hv_clock,
2251 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002252
2253 smp_wmb();
2254
2255 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2256 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2257
2258 if (vcpu->pvclock_set_guest_stopped_request) {
2259 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2260 vcpu->pvclock_set_guest_stopped_request = false;
2261 }
2262
2263 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2264
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002265 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2266 &vcpu->hv_clock,
2267 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002268
2269 smp_wmb();
2270
2271 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002272 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2273 &vcpu->hv_clock,
2274 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002275}
2276
Zachary Amsden34c238a2010-09-18 14:38:14 -10002277static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002278{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002279 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002280 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002281 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002282 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002283 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002284 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002285 bool use_master_clock;
2286
2287 kernel_ns = 0;
2288 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002289
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002290 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002291 * If the host uses TSC clock, then passthrough TSC as stable
2292 * to the guest.
2293 */
2294 spin_lock(&ka->pvclock_gtod_sync_lock);
2295 use_master_clock = ka->use_master_clock;
2296 if (use_master_clock) {
2297 host_tsc = ka->master_cycle_now;
2298 kernel_ns = ka->master_kernel_ns;
2299 }
2300 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002301
2302 /* Keep irq disabled to prevent changes to the clock */
2303 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002304 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2305 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002306 local_irq_restore(flags);
2307 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2308 return 1;
2309 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002310 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002311 host_tsc = rdtsc();
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002312 kernel_ns = ktime_get_boottime_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002313 }
2314
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002315 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002316
2317 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002318 * We may have to catch up the TSC to match elapsed wall clock
2319 * time for two reasons, even if kvmclock is used.
2320 * 1) CPU could have been running below the maximum TSC rate
2321 * 2) Broken TSC compensation resets the base at each VCPU
2322 * entry to avoid unknown leaps of TSC even when running
2323 * again on the same CPU. This may cause apparent elapsed
2324 * time to disappear, and the guest to stand still or run
2325 * very slowly.
2326 */
2327 if (vcpu->tsc_catchup) {
2328 u64 tsc = compute_guest_tsc(v, kernel_ns);
2329 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002330 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002331 tsc_timestamp = tsc;
2332 }
2333 }
2334
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002335 local_irq_restore(flags);
2336
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002337 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002338
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002339 if (kvm_has_tsc_control)
2340 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2341
2342 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002343 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002344 &vcpu->hv_clock.tsc_shift,
2345 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002346 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002347 }
2348
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002349 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002350 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002351 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002352
Marcelo Tosattid8281992012-11-27 23:29:01 -02002353 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002354 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002355 if (use_master_clock)
2356 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2357
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002358 vcpu->hv_clock.flags = pvclock_flags;
2359
Paolo Bonzini095cf552016-02-08 12:54:12 +01002360 if (vcpu->pv_time_enabled)
2361 kvm_setup_pvclock_page(v);
2362 if (v == kvm_get_vcpu(v->kvm, 0))
2363 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002364 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002365}
2366
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002367/*
2368 * kvmclock updates which are isolated to a given vcpu, such as
2369 * vcpu->cpu migration, should not allow system_timestamp from
2370 * the rest of the vcpus to remain static. Otherwise ntp frequency
2371 * correction applies to one vcpu's system_timestamp but not
2372 * the others.
2373 *
2374 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002375 * We need to rate-limit these requests though, as they can
2376 * considerably slow guests that have a large number of vcpus.
2377 * The time for a remote vcpu to update its kvmclock is bound
2378 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002379 */
2380
Andrew Jones7e44e442014-02-28 12:52:54 +01002381#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2382
2383static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002384{
2385 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002386 struct delayed_work *dwork = to_delayed_work(work);
2387 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2388 kvmclock_update_work);
2389 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002390 struct kvm_vcpu *vcpu;
2391
2392 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002393 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002394 kvm_vcpu_kick(vcpu);
2395 }
2396}
2397
Andrew Jones7e44e442014-02-28 12:52:54 +01002398static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2399{
2400 struct kvm *kvm = v->kvm;
2401
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002402 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002403 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2404 KVMCLOCK_UPDATE_DELAY);
2405}
2406
Andrew Jones332967a2014-02-28 12:52:55 +01002407#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2408
2409static void kvmclock_sync_fn(struct work_struct *work)
2410{
2411 struct delayed_work *dwork = to_delayed_work(work);
2412 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2413 kvmclock_sync_work);
2414 struct kvm *kvm = container_of(ka, struct kvm, arch);
2415
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002416 if (!kvmclock_periodic_sync)
2417 return;
2418
Andrew Jones332967a2014-02-28 12:52:55 +01002419 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2420 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2421 KVMCLOCK_SYNC_PERIOD);
2422}
2423
Borislav Petkov191c8132019-04-18 18:32:50 +02002424/*
2425 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2426 */
2427static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2428{
2429 /* McStatusWrEn enabled? */
2430 if (guest_cpuid_is_amd(vcpu))
2431 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2432
2433 return false;
2434}
2435
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002436static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002437{
2438 u64 mcg_cap = vcpu->arch.mcg_cap;
2439 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002440 u32 msr = msr_info->index;
2441 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002442
2443 switch (msr) {
2444 case MSR_IA32_MCG_STATUS:
2445 vcpu->arch.mcg_status = data;
2446 break;
2447 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002448 if (!(mcg_cap & MCG_CTL_P) &&
2449 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002450 return 1;
2451 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002452 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002453 vcpu->arch.mcg_ctl = data;
2454 break;
2455 default:
2456 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002457 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002458 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01002459 /* only 0 or all 1s can be written to IA32_MCi_CTL
2460 * some Linux kernels though clear bit 10 in bank 4 to
2461 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2462 * this to avoid an uncatched #GP in the guest
2463 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002464 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002465 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002466 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002467
2468 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002469 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002470 (offset & 0x3) == 1 && data != 0) {
2471 if (!can_set_mci_status(vcpu))
2472 return -1;
2473 }
2474
Huang Ying890ca9a2009-05-11 16:48:15 +08002475 vcpu->arch.mce_banks[offset] = data;
2476 break;
2477 }
2478 return 1;
2479 }
2480 return 0;
2481}
2482
Ed Swierkffde22a2009-10-15 15:21:43 -07002483static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2484{
2485 struct kvm *kvm = vcpu->kvm;
2486 int lm = is_long_mode(vcpu);
2487 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2488 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2489 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2490 : kvm->arch.xen_hvm_config.blob_size_32;
2491 u32 page_num = data & ~PAGE_MASK;
2492 u64 page_addr = data & PAGE_MASK;
2493 u8 *page;
2494 int r;
2495
2496 r = -E2BIG;
2497 if (page_num >= blob_size)
2498 goto out;
2499 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002500 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2501 if (IS_ERR(page)) {
2502 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002503 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002504 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002505 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002506 goto out_free;
2507 r = 0;
2508out_free:
2509 kfree(page);
2510out:
2511 return r;
2512}
2513
Gleb Natapov344d9582010-10-14 11:22:50 +02002514static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2515{
2516 gpa_t gpa = data & ~0x3f;
2517
Wanpeng Li52a5c152017-07-13 18:30:42 -07002518 /* Bits 3:5 are reserved, Should be zero */
2519 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002520 return 1;
2521
2522 vcpu->arch.apf.msr_val = data;
2523
2524 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2525 kvm_clear_async_pf_completion_queue(vcpu);
2526 kvm_async_pf_hash_reset(vcpu);
2527 return 0;
2528 }
2529
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002530 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002531 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002532 return 1;
2533
Gleb Natapov6adba522010-10-14 11:22:55 +02002534 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002535 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002536 kvm_async_pf_wakeup_all(vcpu);
2537 return 0;
2538}
2539
Glauber Costa12f9a482011-02-01 14:16:40 -05002540static void kvmclock_reset(struct kvm_vcpu *vcpu)
2541{
Andy Honig0b794592013-02-20 14:48:10 -08002542 vcpu->arch.pv_time_enabled = false;
Glauber Costa12f9a482011-02-01 14:16:40 -05002543}
2544
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002545static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2546{
2547 ++vcpu->stat.tlb_flush;
2548 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2549}
2550
Glauber Costac9aaa892011-07-11 15:28:14 -04002551static void record_steal_time(struct kvm_vcpu *vcpu)
2552{
2553 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2554 return;
2555
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002556 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002557 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2558 return;
2559
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002560 /*
2561 * Doing a TLB flush here, on the guest's behalf, can avoid
2562 * expensive IPIs.
2563 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002564 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
2565 vcpu->arch.st.steal.preempted & KVM_VCPU_FLUSH_TLB);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002566 if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB)
2567 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002568
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002569 if (vcpu->arch.st.steal.version & 1)
2570 vcpu->arch.st.steal.version += 1; /* first time write, random junk */
2571
2572 vcpu->arch.st.steal.version += 1;
2573
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002574 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002575 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2576
2577 smp_wmb();
2578
Liang Chenc54cdf12016-03-16 19:33:16 +08002579 vcpu->arch.st.steal.steal += current->sched_info.run_delay -
2580 vcpu->arch.st.last_steal;
2581 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002582
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002583 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002584 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2585
2586 smp_wmb();
2587
2588 vcpu->arch.st.steal.version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002589
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002590 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
Glauber Costac9aaa892011-07-11 15:28:14 -04002591 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2592}
2593
Will Auld8fe8ab42012-11-29 12:42:12 -08002594int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002595{
Gleb Natapov57537852012-01-15 14:17:22 +02002596 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002597 u32 msr = msr_info->index;
2598 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002599
Carsten Otte15c4a642007-10-30 18:44:17 +01002600 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002601 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002602 case MSR_IA32_UCODE_WRITE:
2603 case MSR_VM_HSAVE_PA:
2604 case MSR_AMD64_PATCH_LOADER:
2605 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002606 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002607 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002608 break;
2609
Wanpeng Li518e7b92018-02-28 14:03:31 +08002610 case MSR_IA32_UCODE_REV:
2611 if (msr_info->host_initiated)
2612 vcpu->arch.microcode_version = data;
2613 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002614 case MSR_IA32_ARCH_CAPABILITIES:
2615 if (!msr_info->host_initiated)
2616 return 1;
2617 vcpu->arch.arch_capabilities = data;
2618 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002619 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002620 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002621 case MSR_K7_HWCR:
2622 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002623 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002624 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002625
2626 /* Handle McStatusWrEn */
2627 if (data == BIT_ULL(18)) {
2628 vcpu->arch.msr_hwcr = data;
2629 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002630 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2631 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002632 return 1;
2633 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002634 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002635 case MSR_FAM10H_MMIO_CONF_BASE:
2636 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002637 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2638 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002639 return 1;
2640 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002641 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002642 case MSR_IA32_DEBUGCTLMSR:
2643 if (!data) {
2644 /* We support the non-activated case already */
2645 break;
2646 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2647 /* Values other than LBR and BTF are vendor-specific,
2648 thus reserved and should throw a #GP */
2649 return 1;
2650 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002651 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2652 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002653 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002654 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002655 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002656 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002657 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002658 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2659 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002660 case MSR_IA32_TSCDEADLINE:
2661 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2662 break;
Will Auldba904632012-11-29 12:42:50 -08002663 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002664 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002665 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002666 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002667 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002668 }
2669 vcpu->arch.ia32_tsc_adjust_msr = data;
2670 }
2671 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002672 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002673 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2674 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2675 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2676 return 1;
2677 vcpu->arch.ia32_misc_enable_msr = data;
2678 kvm_update_cpuid(vcpu);
2679 } else {
2680 vcpu->arch.ia32_misc_enable_msr = data;
2681 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002682 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002683 case MSR_IA32_SMBASE:
2684 if (!msr_info->host_initiated)
2685 return 1;
2686 vcpu->arch.smbase = data;
2687 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002688 case MSR_IA32_POWER_CTL:
2689 vcpu->arch.msr_ia32_power_ctl = data;
2690 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002691 case MSR_IA32_TSC:
2692 kvm_write_tsc(vcpu, msr_info);
2693 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002694 case MSR_SMI_COUNT:
2695 if (!msr_info->host_initiated)
2696 return 1;
2697 vcpu->arch.smi_count = data;
2698 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002699 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002700 case MSR_KVM_WALL_CLOCK:
2701 vcpu->kvm->arch.wall_clock = data;
2702 kvm_write_wall_clock(vcpu->kvm, data);
2703 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002704 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002705 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002706 struct kvm_arch *ka = &vcpu->kvm->arch;
2707
Glauber Costa12f9a482011-02-01 14:16:40 -05002708 kvmclock_reset(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002709
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002710 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2711 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2712
2713 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002714 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002715
2716 ka->boot_vcpu_runs_old_kvmclock = tmp;
2717 }
2718
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002719 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002720 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002721
2722 /* we verify if the enable bit is set... */
2723 if (!(data & 1))
2724 break;
2725
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002726 if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002727 &vcpu->arch.pv_time, data & ~1ULL,
2728 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002729 vcpu->arch.pv_time_enabled = false;
2730 else
2731 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002732
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002733 break;
2734 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002735 case MSR_KVM_ASYNC_PF_EN:
2736 if (kvm_pv_enable_async_pf(vcpu, data))
2737 return 1;
2738 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002739 case MSR_KVM_STEAL_TIME:
2740
2741 if (unlikely(!sched_info_on()))
2742 return 1;
2743
2744 if (data & KVM_STEAL_RESERVED_MASK)
2745 return 1;
2746
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002747 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
Andrew Honig8f964522013-03-29 09:35:21 -07002748 data & KVM_STEAL_VALID_BITS,
2749 sizeof(struct kvm_steal_time)))
Glauber Costac9aaa892011-07-11 15:28:14 -04002750 return 1;
2751
2752 vcpu->arch.st.msr_val = data;
2753
2754 if (!(data & KVM_MSR_ENABLED))
2755 break;
2756
Glauber Costac9aaa892011-07-11 15:28:14 -04002757 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2758
2759 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002760 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002761 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002762 return 1;
2763 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002764
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002765 case MSR_KVM_POLL_CONTROL:
2766 /* only enable bit supported */
2767 if (data & (-1ULL << 1))
2768 return 1;
2769
2770 vcpu->arch.msr_kvm_poll_control = data;
2771 break;
2772
Huang Ying890ca9a2009-05-11 16:48:15 +08002773 case MSR_IA32_MCG_CTL:
2774 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002775 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002776 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002777
Wei Huang6912ac32015-06-12 01:34:56 -04002778 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2779 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2780 pr = true; /* fall through */
2781 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2782 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002783 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002784 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002785
2786 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002787 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2788 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002789 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002790 case MSR_K7_CLK_CTL:
2791 /*
2792 * Ignore all writes to this no longer documented MSR.
2793 * Writes are only relevant for old K7 processors,
2794 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002795 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002796 * affected processor models on the command line, hence
2797 * the need to ignore the workaround.
2798 */
2799 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002800 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002801 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2802 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002803 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002804 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2805 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2806 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002807 return kvm_hv_set_msr_common(vcpu, msr, data,
2808 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002809 case MSR_IA32_BBL_CR_CTL3:
2810 /* Drop writes to this legacy MSR -- see rdmsr
2811 * counterpart for further detail.
2812 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002813 if (report_ignored_msrs)
2814 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2815 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002816 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002817 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002818 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002819 return 1;
2820 vcpu->arch.osvw.length = data;
2821 break;
2822 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002823 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002824 return 1;
2825 vcpu->arch.osvw.status = data;
2826 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002827 case MSR_PLATFORM_INFO:
2828 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002829 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2830 cpuid_fault_enabled(vcpu)))
2831 return 1;
2832 vcpu->arch.msr_platform_info = data;
2833 break;
2834 case MSR_MISC_FEATURES_ENABLES:
2835 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2836 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2837 !supports_cpuid_fault(vcpu)))
2838 return 1;
2839 vcpu->arch.msr_misc_features_enables = data;
2840 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002841 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002842 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2843 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002844 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002845 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002846 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002847 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002848 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002849 return 1;
2850 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002851 if (report_ignored_msrs)
2852 vcpu_unimpl(vcpu,
2853 "ignored wrmsr: 0x%x data 0x%llx\n",
2854 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002855 break;
2856 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002857 }
2858 return 0;
2859}
2860EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2861
Paolo Bonzini44883f02018-07-26 13:01:52 +02002862static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002863{
2864 u64 data;
2865 u64 mcg_cap = vcpu->arch.mcg_cap;
2866 unsigned bank_num = mcg_cap & 0xff;
2867
2868 switch (msr) {
2869 case MSR_IA32_P5_MC_ADDR:
2870 case MSR_IA32_P5_MC_TYPE:
2871 data = 0;
2872 break;
2873 case MSR_IA32_MCG_CAP:
2874 data = vcpu->arch.mcg_cap;
2875 break;
2876 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002877 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002878 return 1;
2879 data = vcpu->arch.mcg_ctl;
2880 break;
2881 case MSR_IA32_MCG_STATUS:
2882 data = vcpu->arch.mcg_status;
2883 break;
2884 default:
2885 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002886 msr < MSR_IA32_MCx_CTL(bank_num)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002887 u32 offset = msr - MSR_IA32_MC0_CTL;
2888 data = vcpu->arch.mce_banks[offset];
2889 break;
2890 }
2891 return 1;
2892 }
2893 *pdata = data;
2894 return 0;
2895}
2896
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002897int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002898{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002899 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01002900 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01002901 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002902 case MSR_IA32_DEBUGCTLMSR:
2903 case MSR_IA32_LASTBRANCHFROMIP:
2904 case MSR_IA32_LASTBRANCHTOIP:
2905 case MSR_IA32_LASTINTFROMIP:
2906 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05302907 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02002908 case MSR_K8_TSEG_ADDR:
2909 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02002910 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02002911 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02002912 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002913 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002914 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03002915 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02002916 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002917 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002918 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01002919 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06002920 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04002921 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2922 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2923 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2924 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002925 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002926 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2927 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02002928 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002929 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08002930 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03002931 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002932 case MSR_IA32_ARCH_CAPABILITIES:
2933 if (!msr_info->host_initiated &&
2934 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
2935 return 1;
2936 msr_info->data = vcpu->arch.arch_capabilities;
2937 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002938 case MSR_IA32_POWER_CTL:
2939 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
2940 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002941 case MSR_IA32_TSC:
2942 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
2943 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002944 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03002945 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002946 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002947 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002948 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01002949 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02002950 /*
2951 * MSR_EBC_FREQUENCY_ID
2952 * Conservative value valid for even the basic CPU models.
2953 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2954 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2955 * and 266MHz for model 3, or 4. Set Core Clock
2956 * Frequency to System Bus Frequency Ratio to 1 (bits
2957 * 31:24) even though these are only valid for CPU
2958 * models > 2, however guests may end up dividing or
2959 * multiplying by zero otherwise.
2960 */
2961 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002962 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02002963 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002964 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002965 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01002966 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002967 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002968 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002969 break;
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002970 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002971 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002972 break;
Will Auldba904632012-11-29 12:42:50 -08002973 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002974 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08002975 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002976 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002977 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01002978 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002979 case MSR_IA32_SMBASE:
2980 if (!msr_info->host_initiated)
2981 return 1;
2982 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01002983 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002984 case MSR_SMI_COUNT:
2985 msr_info->data = vcpu->arch.smi_count;
2986 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002987 case MSR_IA32_PERF_STATUS:
2988 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002989 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01002990 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08002991 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01002992 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002993 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002994 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01002995 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002996 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04002997 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002998 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002999 break;
3000 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003001 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003002 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003003 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003004 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003005 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003006 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003007 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003008 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003009 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003010 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003011 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003012 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003013 case MSR_KVM_POLL_CONTROL:
3014 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3015 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003016 case MSR_IA32_P5_MC_ADDR:
3017 case MSR_IA32_P5_MC_TYPE:
3018 case MSR_IA32_MCG_CAP:
3019 case MSR_IA32_MCG_CTL:
3020 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003021 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003022 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3023 msr_info->host_initiated);
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003024 case MSR_K7_CLK_CTL:
3025 /*
3026 * Provide expected ramp-up count for K7. All other
3027 * are set to zero, indicating minimum divisors for
3028 * every field.
3029 *
3030 * This prevents guest kernels on AMD host with CPU
3031 * type 6, model 8 and higher from exploding due to
3032 * the rdmsr failing.
3033 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003034 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003035 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003036 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003037 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3038 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003039 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003040 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3041 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3042 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003043 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003044 msr_info->index, &msr_info->data,
3045 msr_info->host_initiated);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003046 break;
john cooper91c9c3e2011-01-21 00:21:00 -05003047 case MSR_IA32_BBL_CR_CTL3:
3048 /* This legacy MSR exists but isn't fully documented in current
3049 * silicon. It is however accessed by winxp in very narrow
3050 * scenarios where it sets bit #19, itself documented as
3051 * a "reserved" bit. Best effort attempt to source coherent
3052 * read data here should the balance of the register be
3053 * interpreted by the guest:
3054 *
3055 * L2 cache control register 3: 64GB range, 256KB size,
3056 * enabled, latency 0x1, configured
3057 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003058 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003059 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003060 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003061 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003062 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003063 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003064 break;
3065 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003066 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003067 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003068 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003069 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003070 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003071 if (!msr_info->host_initiated &&
3072 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3073 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003074 msr_info->data = vcpu->arch.msr_platform_info;
3075 break;
3076 case MSR_MISC_FEATURES_ENABLES:
3077 msr_info->data = vcpu->arch.msr_misc_features_enables;
3078 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003079 case MSR_K7_HWCR:
3080 msr_info->data = vcpu->arch.msr_hwcr;
3081 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003082 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003083 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003084 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003085 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003086 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3087 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003088 return 1;
3089 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003090 if (report_ignored_msrs)
3091 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3092 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003093 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003094 }
3095 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003096 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003097 return 0;
3098}
3099EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3100
Carsten Otte313a3dc2007-10-11 19:16:52 +02003101/*
3102 * Read or write a bunch of msrs. All parameters are kernel addresses.
3103 *
3104 * @return number of msrs set successfully.
3105 */
3106static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3107 struct kvm_msr_entry *entries,
3108 int (*do_msr)(struct kvm_vcpu *vcpu,
3109 unsigned index, u64 *data))
3110{
Tom Lendacky801e4592018-02-21 13:39:51 -06003111 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003112
Carsten Otte313a3dc2007-10-11 19:16:52 +02003113 for (i = 0; i < msrs->nmsrs; ++i)
3114 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3115 break;
3116
Carsten Otte313a3dc2007-10-11 19:16:52 +02003117 return i;
3118}
3119
3120/*
3121 * Read or write a bunch of msrs. Parameters are user addresses.
3122 *
3123 * @return number of msrs set successfully.
3124 */
3125static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3126 int (*do_msr)(struct kvm_vcpu *vcpu,
3127 unsigned index, u64 *data),
3128 int writeback)
3129{
3130 struct kvm_msrs msrs;
3131 struct kvm_msr_entry *entries;
3132 int r, n;
3133 unsigned size;
3134
3135 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003136 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003137 goto out;
3138
3139 r = -E2BIG;
3140 if (msrs.nmsrs >= MAX_IO_MSRS)
3141 goto out;
3142
Carsten Otte313a3dc2007-10-11 19:16:52 +02003143 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003144 entries = memdup_user(user_msrs->entries, size);
3145 if (IS_ERR(entries)) {
3146 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003147 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003148 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003149
3150 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3151 if (r < 0)
3152 goto out_free;
3153
3154 r = -EFAULT;
3155 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3156 goto out_free;
3157
3158 r = n;
3159
3160out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003161 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003162out:
3163 return r;
3164}
3165
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003166static inline bool kvm_can_mwait_in_guest(void)
3167{
3168 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003169 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3170 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003171}
3172
Alexander Graf784aa3d2014-07-14 18:27:35 +02003173int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003174{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003175 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003176
3177 switch (ext) {
3178 case KVM_CAP_IRQCHIP:
3179 case KVM_CAP_HLT:
3180 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003181 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003182 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003183 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003184 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003185 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003186 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003187 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003188 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003189 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003190 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003191 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003192 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003193 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003194 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003195 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003196 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003197 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003198 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003199 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003200 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003201 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003202 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003203 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003204 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003205 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003206 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003207 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003208 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003209 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003210 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003211 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003212 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003213 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003214 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003215 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003216 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003217 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003218 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003219 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003220 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003221 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003222 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003223 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003224 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003225 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003226 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003227 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003228 r = 1;
3229 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003230 case KVM_CAP_SYNC_REGS:
3231 r = KVM_SYNC_X86_VALID_FIELDS;
3232 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003233 case KVM_CAP_ADJUST_CLOCK:
3234 r = KVM_CLOCK_TSC_STABLE;
3235 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003236 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003237 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3238 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003239 if(kvm_can_mwait_in_guest())
3240 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003241 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003242 case KVM_CAP_X86_SMM:
3243 /* SMBASE is usually relocated above 1M on modern chipsets,
3244 * and SMM handlers might indeed rely on 4G segment limits,
3245 * so do not report SMM to be available if real mode is
3246 * emulated via vm86 mode. Still, do not go to great lengths
3247 * to avoid userspace's usage of the feature, because it is a
3248 * fringe case that is not enabled except via specific settings
3249 * of the module parameters.
3250 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003251 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003252 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003253 case KVM_CAP_VAPIC:
3254 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3255 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003256 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003257 r = KVM_SOFT_MAX_VCPUS;
3258 break;
3259 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003260 r = KVM_MAX_VCPUS;
3261 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003262 case KVM_CAP_MAX_VCPU_ID:
3263 r = KVM_MAX_VCPU_ID;
3264 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003265 case KVM_CAP_PV_MMU: /* obsolete */
3266 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003267 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003268 case KVM_CAP_MCE:
3269 r = KVM_MAX_MCE_BANKS;
3270 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003271 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003272 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003273 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003274 case KVM_CAP_TSC_CONTROL:
3275 r = kvm_has_tsc_control;
3276 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003277 case KVM_CAP_X2APIC_API:
3278 r = KVM_X2APIC_API_VALID_FLAGS;
3279 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003280 case KVM_CAP_NESTED_STATE:
3281 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303282 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003283 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003284 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003285 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3286 break;
3287 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3288 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003289 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003290 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003291 break;
3292 }
3293 return r;
3294
3295}
3296
Carsten Otte043405e2007-10-10 17:16:19 +02003297long kvm_arch_dev_ioctl(struct file *filp,
3298 unsigned int ioctl, unsigned long arg)
3299{
3300 void __user *argp = (void __user *)arg;
3301 long r;
3302
3303 switch (ioctl) {
3304 case KVM_GET_MSR_INDEX_LIST: {
3305 struct kvm_msr_list __user *user_msr_list = argp;
3306 struct kvm_msr_list msr_list;
3307 unsigned n;
3308
3309 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003310 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003311 goto out;
3312 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003313 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003314 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003315 goto out;
3316 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003317 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003318 goto out;
3319 r = -EFAULT;
3320 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3321 num_msrs_to_save * sizeof(u32)))
3322 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003323 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003324 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003325 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003326 goto out;
3327 r = 0;
3328 break;
3329 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003330 case KVM_GET_SUPPORTED_CPUID:
3331 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003332 struct kvm_cpuid2 __user *cpuid_arg = argp;
3333 struct kvm_cpuid2 cpuid;
3334
3335 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003336 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003337 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003338
3339 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3340 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003341 if (r)
3342 goto out;
3343
3344 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003345 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003346 goto out;
3347 r = 0;
3348 break;
3349 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003350 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003351 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003352 if (copy_to_user(argp, &kvm_mce_cap_supported,
3353 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003354 goto out;
3355 r = 0;
3356 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003357 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3358 struct kvm_msr_list __user *user_msr_list = argp;
3359 struct kvm_msr_list msr_list;
3360 unsigned int n;
3361
3362 r = -EFAULT;
3363 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3364 goto out;
3365 n = msr_list.nmsrs;
3366 msr_list.nmsrs = num_msr_based_features;
3367 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3368 goto out;
3369 r = -E2BIG;
3370 if (n < msr_list.nmsrs)
3371 goto out;
3372 r = -EFAULT;
3373 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3374 num_msr_based_features * sizeof(u32)))
3375 goto out;
3376 r = 0;
3377 break;
3378 }
3379 case KVM_GET_MSRS:
3380 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3381 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003382 }
Carsten Otte043405e2007-10-10 17:16:19 +02003383 default:
3384 r = -EINVAL;
3385 }
3386out:
3387 return r;
3388}
3389
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003390static void wbinvd_ipi(void *garbage)
3391{
3392 wbinvd();
3393}
3394
3395static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3396{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003397 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003398}
3399
Carsten Otte313a3dc2007-10-11 19:16:52 +02003400void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3401{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003402 /* Address WBINVD may be executed by guest */
3403 if (need_emulate_wbinvd(vcpu)) {
3404 if (kvm_x86_ops->has_wbinvd_exit())
3405 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3406 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3407 smp_call_function_single(vcpu->cpu,
3408 wbinvd_ipi, NULL, 1);
3409 }
3410
Carsten Otte313a3dc2007-10-11 19:16:52 +02003411 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003412
Wanpeng Lie7517322019-07-22 12:26:20 +08003413 fpregs_assert_state_consistent();
3414 if (test_thread_flag(TIF_NEED_FPU_LOAD))
3415 switch_fpu_return();
3416
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003417 /* Apply any externally detected TSC adjustments (due to suspend) */
3418 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3419 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3420 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003421 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003422 }
3423
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003424 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003425 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003426 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003427 if (tsc_delta < 0)
3428 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003429
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003430 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003431 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003432 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003433 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003434 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003435 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003436
3437 if (kvm_lapic_hv_timer_in_use(vcpu))
3438 kvm_lapic_restart_hv_timer(vcpu);
3439
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003440 /*
3441 * On a host with synchronized TSC, there is no need to update
3442 * kvmclock on vcpu->cpu migration
3443 */
3444 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003445 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003446 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003447 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003448 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003449 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003450
Glauber Costac9aaa892011-07-11 15:28:14 -04003451 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003452}
3453
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003454static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3455{
3456 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3457 return;
3458
Wanpeng Lifa55eed2017-12-12 17:33:01 -08003459 vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003460
Paolo Bonzini4e335d92017-05-02 16:20:18 +02003461 kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003462 &vcpu->arch.st.steal.preempted,
3463 offsetof(struct kvm_steal_time, preempted),
3464 sizeof(vcpu->arch.st.steal.preempted));
3465}
3466
Carsten Otte313a3dc2007-10-11 19:16:52 +02003467void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3468{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003469 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003470
3471 if (vcpu->preempted)
3472 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3473
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003474 /*
3475 * Disable page faults because we're in atomic context here.
3476 * kvm_write_guest_offset_cached() would call might_fault()
3477 * that relies on pagefault_disable() to tell if there's a
3478 * bug. NOTE: the write to guest memory may not go through if
3479 * during postcopy live migration or if there's heavy guest
3480 * paging.
3481 */
3482 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003483 /*
3484 * kvm_memslots() will be called by
3485 * kvm_write_guest_offset_cached() so take the srcu lock.
3486 */
3487 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003488 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003489 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003490 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003491 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003492 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003493 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003494 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3495 * on every vmexit, but if not, we might have a stale dr6 from the
3496 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003497 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003498 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003499}
3500
Carsten Otte313a3dc2007-10-11 19:16:52 +02003501static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3502 struct kvm_lapic_state *s)
3503{
Liran Alonfa59cc02017-12-24 18:12:53 +02003504 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003505 kvm_x86_ops->sync_pir_to_irr(vcpu);
3506
Radim Krčmářa92e2542016-07-12 22:09:22 +02003507 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003508}
3509
3510static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3511 struct kvm_lapic_state *s)
3512{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003513 int r;
3514
3515 r = kvm_apic_set_state(vcpu, s);
3516 if (r)
3517 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003518 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003519
3520 return 0;
3521}
3522
Matt Gingell127a4572015-11-17 17:32:05 +01003523static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3524{
3525 return (!lapic_in_kernel(vcpu) ||
3526 kvm_apic_accept_pic_intr(vcpu));
3527}
3528
Matt Gingell782d4222015-11-16 15:26:00 -08003529/*
3530 * if userspace requested an interrupt window, check that the
3531 * interrupt window is open.
3532 *
3533 * No need to exit to userspace if we already have an interrupt queued.
3534 */
3535static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3536{
3537 return kvm_arch_interrupt_allowed(vcpu) &&
3538 !kvm_cpu_has_interrupt(vcpu) &&
3539 !kvm_event_needs_reinjection(vcpu) &&
3540 kvm_cpu_accept_dm_intr(vcpu);
3541}
3542
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003543static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3544 struct kvm_interrupt *irq)
3545{
Chen Gang02cdb502013-02-27 11:33:25 +08003546 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003547 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003548
3549 if (!irqchip_in_kernel(vcpu->kvm)) {
3550 kvm_queue_interrupt(vcpu, irq->irq, false);
3551 kvm_make_request(KVM_REQ_EVENT, vcpu);
3552 return 0;
3553 }
3554
3555 /*
3556 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3557 * fail for in-kernel 8259.
3558 */
3559 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003560 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003561
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003562 if (vcpu->arch.pending_external_vector != -1)
3563 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003564
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003565 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003566 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003567 return 0;
3568}
3569
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003570static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3571{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003572 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003573
3574 return 0;
3575}
3576
Paolo Bonzinif0778252015-04-01 15:06:40 +02003577static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3578{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003579 kvm_make_request(KVM_REQ_SMI, vcpu);
3580
Paolo Bonzinif0778252015-04-01 15:06:40 +02003581 return 0;
3582}
3583
Avi Kivityb209749f2007-10-22 16:50:39 +02003584static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3585 struct kvm_tpr_access_ctl *tac)
3586{
3587 if (tac->flags)
3588 return -EINVAL;
3589 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3590 return 0;
3591}
3592
Huang Ying890ca9a2009-05-11 16:48:15 +08003593static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3594 u64 mcg_cap)
3595{
3596 int r;
3597 unsigned bank_num = mcg_cap & 0xff, bank;
3598
3599 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003600 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003601 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003602 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003603 goto out;
3604 r = 0;
3605 vcpu->arch.mcg_cap = mcg_cap;
3606 /* Init IA32_MCG_CTL to all 1s */
3607 if (mcg_cap & MCG_CTL_P)
3608 vcpu->arch.mcg_ctl = ~(u64)0;
3609 /* Init IA32_MCi_CTL to all 1s */
3610 for (bank = 0; bank < bank_num; bank++)
3611 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003612
Sean Christopherson92735b12019-08-02 15:06:17 -07003613 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003614out:
3615 return r;
3616}
3617
3618static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3619 struct kvm_x86_mce *mce)
3620{
3621 u64 mcg_cap = vcpu->arch.mcg_cap;
3622 unsigned bank_num = mcg_cap & 0xff;
3623 u64 *banks = vcpu->arch.mce_banks;
3624
3625 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3626 return -EINVAL;
3627 /*
3628 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3629 * reporting is disabled
3630 */
3631 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3632 vcpu->arch.mcg_ctl != ~(u64)0)
3633 return 0;
3634 banks += 4 * mce->bank;
3635 /*
3636 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3637 * reporting is disabled for the bank
3638 */
3639 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3640 return 0;
3641 if (mce->status & MCI_STATUS_UC) {
3642 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003643 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003644 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003645 return 0;
3646 }
3647 if (banks[1] & MCI_STATUS_VAL)
3648 mce->status |= MCI_STATUS_OVER;
3649 banks[2] = mce->addr;
3650 banks[3] = mce->misc;
3651 vcpu->arch.mcg_status = mce->mcg_status;
3652 banks[1] = mce->status;
3653 kvm_queue_exception(vcpu, MC_VECTOR);
3654 } else if (!(banks[1] & MCI_STATUS_VAL)
3655 || !(banks[1] & MCI_STATUS_UC)) {
3656 if (banks[1] & MCI_STATUS_VAL)
3657 mce->status |= MCI_STATUS_OVER;
3658 banks[2] = mce->addr;
3659 banks[3] = mce->misc;
3660 banks[1] = mce->status;
3661 } else
3662 banks[1] |= MCI_STATUS_OVER;
3663 return 0;
3664}
3665
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003666static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3667 struct kvm_vcpu_events *events)
3668{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003669 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003670
Wanpeng Li664f8e22017-08-24 03:35:09 -07003671 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003672 * The API doesn't provide the instruction length for software
3673 * exceptions, so don't report them. As long as the guest RIP
3674 * isn't advanced, we should expect to encounter the exception
3675 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003676 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003677 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3678 events->exception.injected = 0;
3679 events->exception.pending = 0;
3680 } else {
3681 events->exception.injected = vcpu->arch.exception.injected;
3682 events->exception.pending = vcpu->arch.exception.pending;
3683 /*
3684 * For ABI compatibility, deliberately conflate
3685 * pending and injected exceptions when
3686 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3687 */
3688 if (!vcpu->kvm->arch.exception_payload_enabled)
3689 events->exception.injected |=
3690 vcpu->arch.exception.pending;
3691 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003692 events->exception.nr = vcpu->arch.exception.nr;
3693 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3694 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003695 events->exception_has_payload = vcpu->arch.exception.has_payload;
3696 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003697
Jan Kiszka03b82a32010-02-15 10:45:41 +01003698 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003699 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003700 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003701 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003702 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003703
3704 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003705 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003706 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003707 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003708
Jan Kiszka66450a22013-03-13 12:42:34 +01003709 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003710
Paolo Bonzinif0778252015-04-01 15:06:40 +02003711 events->smi.smm = is_smm(vcpu);
3712 events->smi.pending = vcpu->arch.smi_pending;
3713 events->smi.smm_inside_nmi =
3714 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3715 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3716
Jan Kiszkadab4b912009-12-06 18:24:15 +01003717 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003718 | KVM_VCPUEVENT_VALID_SHADOW
3719 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003720 if (vcpu->kvm->arch.exception_payload_enabled)
3721 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3722
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003723 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003724}
3725
Sean Christophersonc5833c72019-04-02 08:03:10 -07003726static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003727
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003728static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3729 struct kvm_vcpu_events *events)
3730{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003731 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003732 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003733 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003734 | KVM_VCPUEVENT_VALID_SMM
3735 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003736 return -EINVAL;
3737
Jim Mattson59073aa2018-10-16 14:29:20 -07003738 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3739 if (!vcpu->kvm->arch.exception_payload_enabled)
3740 return -EINVAL;
3741 if (events->exception.pending)
3742 events->exception.injected = 0;
3743 else
3744 events->exception_has_payload = 0;
3745 } else {
3746 events->exception.pending = 0;
3747 events->exception_has_payload = 0;
3748 }
3749
3750 if ((events->exception.injected || events->exception.pending) &&
3751 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003752 return -EINVAL;
3753
David Hildenbrand28bf2882017-03-23 11:46:03 +01003754 /* INITs are latched while in SMM */
3755 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3756 (events->smi.smm || events->smi.pending) &&
3757 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3758 return -EINVAL;
3759
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003760 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003761 vcpu->arch.exception.injected = events->exception.injected;
3762 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003763 vcpu->arch.exception.nr = events->exception.nr;
3764 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3765 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003766 vcpu->arch.exception.has_payload = events->exception_has_payload;
3767 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003768
Liran Alon04140b42018-03-23 03:01:31 +03003769 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003770 vcpu->arch.interrupt.nr = events->interrupt.nr;
3771 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003772 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3773 kvm_x86_ops->set_interrupt_shadow(vcpu,
3774 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003775
3776 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003777 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3778 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003779 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3780
Jan Kiszka66450a22013-03-13 12:42:34 +01003781 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003782 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003783 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003784
Paolo Bonzinif0778252015-04-01 15:06:40 +02003785 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003786 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3787 if (events->smi.smm)
3788 vcpu->arch.hflags |= HF_SMM_MASK;
3789 else
3790 vcpu->arch.hflags &= ~HF_SMM_MASK;
3791 kvm_smm_changed(vcpu);
3792 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003793
Paolo Bonzinif0778252015-04-01 15:06:40 +02003794 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003795
3796 if (events->smi.smm) {
3797 if (events->smi.smm_inside_nmi)
3798 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003799 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003800 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
3801 if (lapic_in_kernel(vcpu)) {
3802 if (events->smi.latched_init)
3803 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3804 else
3805 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3806 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02003807 }
3808 }
3809
Avi Kivity3842d132010-07-27 12:30:24 +03003810 kvm_make_request(KVM_REQ_EVENT, vcpu);
3811
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003812 return 0;
3813}
3814
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003815static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3816 struct kvm_debugregs *dbgregs)
3817{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003818 unsigned long val;
3819
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003820 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003821 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003822 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003823 dbgregs->dr7 = vcpu->arch.dr7;
3824 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003825 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003826}
3827
3828static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3829 struct kvm_debugregs *dbgregs)
3830{
3831 if (dbgregs->flags)
3832 return -EINVAL;
3833
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003834 if (dbgregs->dr6 & ~0xffffffffull)
3835 return -EINVAL;
3836 if (dbgregs->dr7 & ~0xffffffffull)
3837 return -EINVAL;
3838
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003839 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003840 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003841 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003842 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003843 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01003844 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003845
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003846 return 0;
3847}
3848
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003849#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3850
3851static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3852{
Marc Orrb666a4b2018-11-06 14:53:56 -08003853 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02003854 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003855 u64 valid;
3856
3857 /*
3858 * Copy legacy XSAVE area, to avoid complications with CPUID
3859 * leaves 0 and 1 in the loop below.
3860 */
3861 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3862
3863 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01003864 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003865 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3866
3867 /*
3868 * Copy each region from the possibly compacted offset to the
3869 * non-compacted offset.
3870 */
Dave Hansend91cab72015-09-02 16:31:26 -07003871 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003872 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003873 u64 xfeature_mask = valid & -valid;
3874 int xfeature_nr = fls64(xfeature_mask) - 1;
3875 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003876
3877 if (src) {
3878 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003879 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003880 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003881 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003882 memcpy(dest + offset, &vcpu->arch.pkru,
3883 sizeof(vcpu->arch.pkru));
3884 else
3885 memcpy(dest + offset, src, size);
3886
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003887 }
3888
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003889 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003890 }
3891}
3892
3893static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3894{
Marc Orrb666a4b2018-11-06 14:53:56 -08003895 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003896 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3897 u64 valid;
3898
3899 /*
3900 * Copy legacy XSAVE area, to avoid complications with CPUID
3901 * leaves 0 and 1 in the loop below.
3902 */
3903 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3904
3905 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02003906 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02003907 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02003908 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003909
3910 /*
3911 * Copy each region from the non-compacted offset to the
3912 * possibly compacted offset.
3913 */
Dave Hansend91cab72015-09-02 16:31:26 -07003914 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003915 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003916 u64 xfeature_mask = valid & -valid;
3917 int xfeature_nr = fls64(xfeature_mask) - 1;
3918 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003919
3920 if (dest) {
3921 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003922 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003923 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003924 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02003925 memcpy(&vcpu->arch.pkru, src + offset,
3926 sizeof(vcpu->arch.pkru));
3927 else
3928 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08003929 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003930
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02003931 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003932 }
3933}
3934
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003935static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3936 struct kvm_xsave *guest_xsave)
3937{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003938 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003939 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3940 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02003941 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003942 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08003943 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003944 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003945 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07003946 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003947 }
3948}
3949
Wanpeng Lia5758132017-05-11 02:58:55 -07003950#define XSAVE_MXCSR_OFFSET 24
3951
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003952static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
3953 struct kvm_xsave *guest_xsave)
3954{
3955 u64 xstate_bv =
3956 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07003957 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003958
Borislav Petkovd366bf72016-04-04 22:25:02 +02003959 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003960 /*
3961 * Here we allow setting states that are not present in
3962 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
3963 * with old userspace.
3964 */
Wanpeng Lia5758132017-05-11 02:58:55 -07003965 if (xstate_bv & ~kvm_supported_xcr0() ||
3966 mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003967 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01003968 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02003969 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07003970 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
3971 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003972 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08003973 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02003974 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003975 }
3976 return 0;
3977}
3978
3979static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
3980 struct kvm_xcrs *guest_xcrs)
3981{
Borislav Petkovd366bf72016-04-04 22:25:02 +02003982 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003983 guest_xcrs->nr_xcrs = 0;
3984 return;
3985 }
3986
3987 guest_xcrs->nr_xcrs = 1;
3988 guest_xcrs->flags = 0;
3989 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
3990 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
3991}
3992
3993static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
3994 struct kvm_xcrs *guest_xcrs)
3995{
3996 int i, r = 0;
3997
Borislav Petkovd366bf72016-04-04 22:25:02 +02003998 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003999 return -EINVAL;
4000
4001 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4002 return -EINVAL;
4003
4004 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4005 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004006 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004007 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004008 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004009 break;
4010 }
4011 if (r)
4012 r = -EINVAL;
4013 return r;
4014}
4015
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004016/*
4017 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4018 * stopped by the hypervisor. This function will be called from the host only.
4019 * EINVAL is returned when the host attempts to set the flag for a guest that
4020 * does not support pv clocks.
4021 */
4022static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4023{
Andy Honig0b794592013-02-20 14:48:10 -08004024 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004025 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004026 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004027 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4028 return 0;
4029}
4030
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004031static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4032 struct kvm_enable_cap *cap)
4033{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004034 int r;
4035 uint16_t vmcs_version;
4036 void __user *user_ptr;
4037
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004038 if (cap->flags)
4039 return -EINVAL;
4040
4041 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004042 case KVM_CAP_HYPERV_SYNIC2:
4043 if (cap->args[0])
4044 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004045 /* fall through */
4046
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004047 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004048 if (!irqchip_in_kernel(vcpu->kvm))
4049 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004050 return kvm_hv_activate_synic(vcpu, cap->cap ==
4051 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004052 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004053 if (!kvm_x86_ops->nested_enable_evmcs)
4054 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004055 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4056 if (!r) {
4057 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4058 if (copy_to_user(user_ptr, &vmcs_version,
4059 sizeof(vmcs_version)))
4060 r = -EFAULT;
4061 }
4062 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004063 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4064 if (!kvm_x86_ops->enable_direct_tlbflush)
4065 return -ENOTTY;
4066
4067 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004068
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004069 default:
4070 return -EINVAL;
4071 }
4072}
4073
Carsten Otte313a3dc2007-10-11 19:16:52 +02004074long kvm_arch_vcpu_ioctl(struct file *filp,
4075 unsigned int ioctl, unsigned long arg)
4076{
4077 struct kvm_vcpu *vcpu = filp->private_data;
4078 void __user *argp = (void __user *)arg;
4079 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004080 union {
4081 struct kvm_lapic_state *lapic;
4082 struct kvm_xsave *xsave;
4083 struct kvm_xcrs *xcrs;
4084 void *buffer;
4085 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004086
Christoffer Dall9b0624712017-12-04 21:35:36 +01004087 vcpu_load(vcpu);
4088
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004089 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004090 switch (ioctl) {
4091 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004092 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004093 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004094 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004095 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4096 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004097
Dave Hansenb772ff32008-08-11 10:01:47 -07004098 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004099 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004100 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004101 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004102 if (r)
4103 goto out;
4104 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004105 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004106 goto out;
4107 r = 0;
4108 break;
4109 }
4110 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004111 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004112 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004113 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004114 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004115 if (IS_ERR(u.lapic)) {
4116 r = PTR_ERR(u.lapic);
4117 goto out_nofree;
4118 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004119
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004120 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004121 break;
4122 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004123 case KVM_INTERRUPT: {
4124 struct kvm_interrupt irq;
4125
4126 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004127 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004128 goto out;
4129 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004130 break;
4131 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004132 case KVM_NMI: {
4133 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004134 break;
4135 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004136 case KVM_SMI: {
4137 r = kvm_vcpu_ioctl_smi(vcpu);
4138 break;
4139 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004140 case KVM_SET_CPUID: {
4141 struct kvm_cpuid __user *cpuid_arg = argp;
4142 struct kvm_cpuid cpuid;
4143
4144 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004145 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004146 goto out;
4147 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004148 break;
4149 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004150 case KVM_SET_CPUID2: {
4151 struct kvm_cpuid2 __user *cpuid_arg = argp;
4152 struct kvm_cpuid2 cpuid;
4153
4154 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004155 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004156 goto out;
4157 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004158 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004159 break;
4160 }
4161 case KVM_GET_CPUID2: {
4162 struct kvm_cpuid2 __user *cpuid_arg = argp;
4163 struct kvm_cpuid2 cpuid;
4164
4165 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004166 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004167 goto out;
4168 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004169 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004170 if (r)
4171 goto out;
4172 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004173 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004174 goto out;
4175 r = 0;
4176 break;
4177 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004178 case KVM_GET_MSRS: {
4179 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004180 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004181 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004182 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004183 }
4184 case KVM_SET_MSRS: {
4185 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004186 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004187 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004188 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004189 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004190 case KVM_TPR_ACCESS_REPORTING: {
4191 struct kvm_tpr_access_ctl tac;
4192
4193 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004194 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004195 goto out;
4196 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4197 if (r)
4198 goto out;
4199 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004200 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004201 goto out;
4202 r = 0;
4203 break;
4204 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004205 case KVM_SET_VAPIC_ADDR: {
4206 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004207 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004208
4209 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004210 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004211 goto out;
4212 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004213 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004214 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004215 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004216 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004217 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004218 break;
4219 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004220 case KVM_X86_SETUP_MCE: {
4221 u64 mcg_cap;
4222
4223 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004224 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004225 goto out;
4226 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4227 break;
4228 }
4229 case KVM_X86_SET_MCE: {
4230 struct kvm_x86_mce mce;
4231
4232 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004233 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004234 goto out;
4235 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4236 break;
4237 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004238 case KVM_GET_VCPU_EVENTS: {
4239 struct kvm_vcpu_events events;
4240
4241 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4242
4243 r = -EFAULT;
4244 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4245 break;
4246 r = 0;
4247 break;
4248 }
4249 case KVM_SET_VCPU_EVENTS: {
4250 struct kvm_vcpu_events events;
4251
4252 r = -EFAULT;
4253 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4254 break;
4255
4256 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4257 break;
4258 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004259 case KVM_GET_DEBUGREGS: {
4260 struct kvm_debugregs dbgregs;
4261
4262 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4263
4264 r = -EFAULT;
4265 if (copy_to_user(argp, &dbgregs,
4266 sizeof(struct kvm_debugregs)))
4267 break;
4268 r = 0;
4269 break;
4270 }
4271 case KVM_SET_DEBUGREGS: {
4272 struct kvm_debugregs dbgregs;
4273
4274 r = -EFAULT;
4275 if (copy_from_user(&dbgregs, argp,
4276 sizeof(struct kvm_debugregs)))
4277 break;
4278
4279 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4280 break;
4281 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004282 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004283 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004284 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004285 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004286 break;
4287
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004288 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004289
4290 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004291 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004292 break;
4293 r = 0;
4294 break;
4295 }
4296 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004297 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004298 if (IS_ERR(u.xsave)) {
4299 r = PTR_ERR(u.xsave);
4300 goto out_nofree;
4301 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004302
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004303 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004304 break;
4305 }
4306 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004307 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004308 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004309 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004310 break;
4311
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004312 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004313
4314 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004315 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004316 sizeof(struct kvm_xcrs)))
4317 break;
4318 r = 0;
4319 break;
4320 }
4321 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004322 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004323 if (IS_ERR(u.xcrs)) {
4324 r = PTR_ERR(u.xcrs);
4325 goto out_nofree;
4326 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004327
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004328 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004329 break;
4330 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004331 case KVM_SET_TSC_KHZ: {
4332 u32 user_tsc_khz;
4333
4334 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004335 user_tsc_khz = (u32)arg;
4336
4337 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4338 goto out;
4339
Zachary Amsdencc578282012-02-03 15:43:50 -02004340 if (user_tsc_khz == 0)
4341 user_tsc_khz = tsc_khz;
4342
Haozhong Zhang381d5852015-10-20 15:39:04 +08004343 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4344 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004345
Joerg Roedel92a1f122011-03-25 09:44:51 +01004346 goto out;
4347 }
4348 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004349 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004350 goto out;
4351 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004352 case KVM_KVMCLOCK_CTRL: {
4353 r = kvm_set_guest_paused(vcpu);
4354 goto out;
4355 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004356 case KVM_ENABLE_CAP: {
4357 struct kvm_enable_cap cap;
4358
4359 r = -EFAULT;
4360 if (copy_from_user(&cap, argp, sizeof(cap)))
4361 goto out;
4362 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4363 break;
4364 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004365 case KVM_GET_NESTED_STATE: {
4366 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4367 u32 user_data_size;
4368
4369 r = -EINVAL;
4370 if (!kvm_x86_ops->get_nested_state)
4371 break;
4372
4373 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004374 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004375 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004376 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004377
4378 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4379 user_data_size);
4380 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004381 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004382
4383 if (r > user_data_size) {
4384 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004385 r = -EFAULT;
4386 else
4387 r = -E2BIG;
4388 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004389 }
Liran Alon26b471c2018-09-16 14:28:20 +03004390
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004391 r = 0;
4392 break;
4393 }
4394 case KVM_SET_NESTED_STATE: {
4395 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4396 struct kvm_nested_state kvm_state;
4397
4398 r = -EINVAL;
4399 if (!kvm_x86_ops->set_nested_state)
4400 break;
4401
Liran Alon26b471c2018-09-16 14:28:20 +03004402 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004403 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004404 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004405
Liran Alon26b471c2018-09-16 14:28:20 +03004406 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004407 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004408 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004409
4410 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004411 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4412 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004413 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004414
4415 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004416 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4417 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004418 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004419
4420 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
4421 break;
4422 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004423 case KVM_GET_SUPPORTED_HV_CPUID: {
4424 struct kvm_cpuid2 __user *cpuid_arg = argp;
4425 struct kvm_cpuid2 cpuid;
4426
4427 r = -EFAULT;
4428 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4429 goto out;
4430
4431 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4432 cpuid_arg->entries);
4433 if (r)
4434 goto out;
4435
4436 r = -EFAULT;
4437 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4438 goto out;
4439 r = 0;
4440 break;
4441 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004442 default:
4443 r = -EINVAL;
4444 }
4445out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004446 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004447out_nofree:
4448 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004449 return r;
4450}
4451
Souptick Joarder1499fa82018-04-19 00:49:58 +05304452vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004453{
4454 return VM_FAULT_SIGBUS;
4455}
4456
Carsten Otte1fe779f2007-10-29 16:08:35 +01004457static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4458{
4459 int ret;
4460
4461 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004462 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004463 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4464 return ret;
4465}
4466
Sheng Yangb927a3c2009-07-21 10:42:48 +08004467static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4468 u64 ident_addr)
4469{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004470 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004471}
4472
Carsten Otte1fe779f2007-10-29 16:08:35 +01004473static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004474 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004475{
4476 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4477 return -EINVAL;
4478
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004479 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004480
4481 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004482 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004483
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004484 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004485 return 0;
4486}
4487
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004488static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004489{
Dave Hansen39de71e2010-08-19 18:11:14 -07004490 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004491}
4492
Carsten Otte1fe779f2007-10-29 16:08:35 +01004493static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4494{
David Hildenbrand90bca052017-04-07 10:50:23 +02004495 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004496 int r;
4497
4498 r = 0;
4499 switch (chip->chip_id) {
4500 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004501 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004502 sizeof(struct kvm_pic_state));
4503 break;
4504 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004505 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004506 sizeof(struct kvm_pic_state));
4507 break;
4508 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004509 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004510 break;
4511 default:
4512 r = -EINVAL;
4513 break;
4514 }
4515 return r;
4516}
4517
4518static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4519{
David Hildenbrand90bca052017-04-07 10:50:23 +02004520 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004521 int r;
4522
4523 r = 0;
4524 switch (chip->chip_id) {
4525 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004526 spin_lock(&pic->lock);
4527 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004528 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004529 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004530 break;
4531 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004532 spin_lock(&pic->lock);
4533 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004534 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004535 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004536 break;
4537 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004538 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004539 break;
4540 default:
4541 r = -EINVAL;
4542 break;
4543 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004544 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004545 return r;
4546}
4547
Sheng Yange0f63cb2008-03-04 00:50:59 +08004548static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4549{
Radim Krčmář34f39412016-03-02 22:56:50 +01004550 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4551
4552 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4553
4554 mutex_lock(&kps->lock);
4555 memcpy(ps, &kps->channels, sizeof(*ps));
4556 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304557 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004558}
4559
4560static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4561{
Andrew Honig01856042015-11-18 14:50:23 -08004562 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004563 struct kvm_pit *pit = kvm->arch.vpit;
4564
4565 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004566 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004567 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004568 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4569 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304570 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004571}
4572
4573static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4574{
Beth Kone9f42752009-07-07 11:50:38 -04004575 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4576 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4577 sizeof(ps->channels));
4578 ps->flags = kvm->arch.vpit->pit_state.flags;
4579 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004580 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304581 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004582}
4583
4584static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4585{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304586 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004587 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004588 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004589 struct kvm_pit *pit = kvm->arch.vpit;
4590
4591 mutex_lock(&pit->pit_state.lock);
4592 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004593 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4594 if (!prev_legacy && cur_legacy)
4595 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004596 memcpy(&pit->pit_state.channels, &ps->channels,
4597 sizeof(pit->pit_state.channels));
4598 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004599 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004600 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004601 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004602 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304603 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004604}
4605
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004606static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4607 struct kvm_reinject_control *control)
4608{
Radim Krčmář71474e22016-03-02 22:56:45 +01004609 struct kvm_pit *pit = kvm->arch.vpit;
4610
4611 if (!pit)
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004612 return -ENXIO;
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004613
Radim Krčmář71474e22016-03-02 22:56:45 +01004614 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4615 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4616 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4617 */
4618 mutex_lock(&pit->pit_state.lock);
4619 kvm_pit_set_reinject(pit, control->pit_reinject);
4620 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004621
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004622 return 0;
4623}
4624
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004625/**
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004626 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
4627 * @kvm: kvm instance
4628 * @log: slot id and address to which we copy the log
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004629 *
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004630 * Steps 1-4 below provide general overview of dirty page logging. See
4631 * kvm_get_dirty_log_protect() function description for additional details.
4632 *
4633 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
4634 * always flush the TLB (step 4) even if previous step failed and the dirty
4635 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
4636 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
4637 * writes will be marked dirty for next log read.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004638 *
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004639 * 1. Take a snapshot of the bit and clear it if needed.
4640 * 2. Write protect the corresponding page.
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004641 * 3. Copy the snapshot to the userspace.
4642 * 4. Flush TLB's if needed.
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09004643 */
Takuya Yoshikawa60c34612012-03-03 14:21:48 +09004644int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004645{
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004646 bool flush = false;
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004647 int r;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004648
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004649 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004650
Kai Huang88178fd2015-01-28 10:54:27 +08004651 /*
4652 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4653 */
4654 if (kvm_x86_ops->flush_log_dirty)
4655 kvm_x86_ops->flush_log_dirty(kvm);
4656
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004657 r = kvm_get_dirty_log_protect(kvm, log, &flush);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004658
4659 /*
4660 * All the TLBs can be flushed out of mmu lock, see the comments in
4661 * kvm_mmu_slot_remove_write_access().
4662 */
Paolo Bonzinie108ff22015-01-15 15:58:54 -08004663 lockdep_assert_held(&kvm->slots_lock);
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02004664 if (flush)
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004665 kvm_flush_remote_tlbs(kvm);
4666
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004667 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004668 return r;
4669}
4670
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02004671int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
4672{
4673 bool flush = false;
4674 int r;
4675
4676 mutex_lock(&kvm->slots_lock);
4677
4678 /*
4679 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4680 */
4681 if (kvm_x86_ops->flush_log_dirty)
4682 kvm_x86_ops->flush_log_dirty(kvm);
4683
4684 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
4685
4686 /*
4687 * All the TLBs can be flushed out of mmu lock, see the comments in
4688 * kvm_mmu_slot_remove_write_access().
4689 */
4690 lockdep_assert_held(&kvm->slots_lock);
4691 if (flush)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004692 kvm_flush_remote_tlbs(kvm);
4693
4694 mutex_unlock(&kvm->slots_lock);
4695 return r;
Avi Kivity367e1312009-08-26 14:57:07 +03004696}
Dave Hansenf0d66272008-08-11 10:01:45 -07004697
4698int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4699 bool line_status)
4700{
4701 if (!irqchip_in_kernel(kvm))
4702 return -ENXIO;
4703
Beth Kone9f42752009-07-07 11:50:38 -04004704 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Dave Hansenf0d66272008-08-11 10:01:45 -07004705 irq_event->irq, irq_event->level,
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004706 line_status);
Dave Hansenf0d66272008-08-11 10:01:45 -07004707 return 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004708}
4709
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004710int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4711 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004712{
4713 int r;
4714
4715 if (cap->flags)
4716 return -EINVAL;
4717
4718 switch (cap->cap) {
4719 case KVM_CAP_DISABLE_QUIRKS:
4720 kvm->arch.disabled_quirks = cap->args[0];
4721 r = 0;
4722 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004723 case KVM_CAP_SPLIT_IRQCHIP: {
4724 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004725 r = -EINVAL;
4726 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4727 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004728 r = -EEXIST;
4729 if (irqchip_in_kernel(kvm))
4730 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004731 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004732 goto split_irqchip_unlock;
4733 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004734 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004735 goto split_irqchip_unlock;
4736 /* Pairs with irqchip_in_kernel. */
4737 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004738 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004739 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004740 r = 0;
4741split_irqchip_unlock:
4742 mutex_unlock(&kvm->lock);
4743 break;
4744 }
Radim Krčmář371313132016-07-12 22:09:27 +02004745 case KVM_CAP_X2APIC_API:
4746 r = -EINVAL;
4747 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4748 break;
4749
4750 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4751 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004752 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4753 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004754
4755 r = 0;
4756 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004757 case KVM_CAP_X86_DISABLE_EXITS:
4758 r = -EINVAL;
4759 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4760 break;
4761
4762 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4763 kvm_can_mwait_in_guest())
4764 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004765 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004766 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004767 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4768 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004769 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4770 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004771 r = 0;
4772 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004773 case KVM_CAP_MSR_PLATFORM_INFO:
4774 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4775 r = 0;
4776 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004777 case KVM_CAP_EXCEPTION_PAYLOAD:
4778 kvm->arch.exception_payload_enabled = cap->args[0];
4779 r = 0;
4780 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004781 default:
4782 r = -EINVAL;
4783 break;
4784 }
4785 return r;
4786}
4787
Carsten Otte1fe779f2007-10-29 16:08:35 +01004788long kvm_arch_vm_ioctl(struct file *filp,
4789 unsigned int ioctl, unsigned long arg)
4790{
4791 struct kvm *kvm = filp->private_data;
4792 void __user *argp = (void __user *)arg;
4793 int r = -ENOTTY;
4794 /*
4795 * This union makes it completely explicit to gcc-3.x
4796 * that these two variables' stack usage should be
4797 * combined, not added together.
4798 */
4799 union {
4800 struct kvm_pit_state ps;
4801 struct kvm_pit_state2 ps2;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004802 struct kvm_pit_config pit_config;
4803 } u;
4804
4805 switch (ioctl) {
4806 case KVM_SET_TSS_ADDR:
4807 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004808 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004809 case KVM_SET_IDENTITY_MAP_ADDR: {
4810 u64 ident_addr;
4811
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004812 mutex_lock(&kvm->lock);
4813 r = -EINVAL;
4814 if (kvm->created_vcpus)
4815 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004816 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004817 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004818 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004819 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004820set_identity_unlock:
4821 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004822 break;
4823 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004824 case KVM_SET_NR_MMU_PAGES:
4825 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004826 break;
4827 case KVM_GET_NR_MMU_PAGES:
4828 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4829 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004830 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004831 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004832
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004833 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004834 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004835 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004836
Avi Kivity3e515702012-03-05 14:23:29 +02004837 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004838 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004839 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004840
4841 r = kvm_pic_init(kvm);
4842 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004843 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004844
4845 r = kvm_ioapic_init(kvm);
4846 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004847 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004848 goto create_irqchip_unlock;
4849 }
4850
Avi Kivity399ec802008-11-19 13:58:46 +02004851 r = kvm_setup_default_irq_routing(kvm);
4852 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004853 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004854 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004855 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004856 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004857 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004858 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004859 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004860 create_irqchip_unlock:
4861 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004862 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004863 }
Sheng Yang78376992008-01-28 05:10:22 +08004864 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004865 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4866 goto create_pit;
4867 case KVM_CREATE_PIT2:
4868 r = -EFAULT;
4869 if (copy_from_user(&u.pit_config, argp,
4870 sizeof(struct kvm_pit_config)))
4871 goto out;
4872 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004873 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004874 r = -EEXIST;
4875 if (kvm->arch.vpit)
4876 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004877 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004878 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004879 if (kvm->arch.vpit)
4880 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004881 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004882 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004883 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004884 case KVM_GET_IRQCHIP: {
4885 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004886 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004887
Sasha Levinff5c2c02011-12-04 19:36:29 +02004888 chip = memdup_user(argp, sizeof(*chip));
4889 if (IS_ERR(chip)) {
4890 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004891 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004892 }
4893
Carsten Otte1fe779f2007-10-29 16:08:35 +01004894 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004895 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004896 goto get_irqchip_out;
4897 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4898 if (r)
4899 goto get_irqchip_out;
4900 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004901 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07004902 goto get_irqchip_out;
4903 r = 0;
4904 get_irqchip_out:
4905 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004906 break;
4907 }
4908 case KVM_SET_IRQCHIP: {
4909 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004910 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004911
Sasha Levinff5c2c02011-12-04 19:36:29 +02004912 chip = memdup_user(argp, sizeof(*chip));
4913 if (IS_ERR(chip)) {
4914 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004915 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004916 }
4917
Carsten Otte1fe779f2007-10-29 16:08:35 +01004918 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004919 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004920 goto set_irqchip_out;
4921 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
4922 if (r)
4923 goto set_irqchip_out;
4924 r = 0;
4925 set_irqchip_out:
4926 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004927 break;
4928 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08004929 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004930 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004931 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004932 goto out;
4933 r = -ENXIO;
4934 if (!kvm->arch.vpit)
4935 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004936 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004937 if (r)
4938 goto out;
4939 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07004940 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004941 goto out;
4942 r = 0;
4943 break;
4944 }
4945 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08004946 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004947 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08004948 goto out;
4949 r = -ENXIO;
4950 if (!kvm->arch.vpit)
4951 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07004952 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08004953 break;
4954 }
Beth Kone9f42752009-07-07 11:50:38 -04004955 case KVM_GET_PIT2: {
4956 r = -ENXIO;
4957 if (!kvm->arch.vpit)
4958 goto out;
4959 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
4960 if (r)
4961 goto out;
4962 r = -EFAULT;
4963 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
4964 goto out;
4965 r = 0;
4966 break;
4967 }
4968 case KVM_SET_PIT2: {
4969 r = -EFAULT;
4970 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
4971 goto out;
4972 r = -ENXIO;
4973 if (!kvm->arch.vpit)
4974 goto out;
4975 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04004976 break;
4977 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004978 case KVM_REINJECT_CONTROL: {
4979 struct kvm_reinject_control control;
4980 r = -EFAULT;
4981 if (copy_from_user(&control, argp, sizeof(control)))
4982 goto out;
4983 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004984 break;
4985 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004986 case KVM_SET_BOOT_CPU_ID:
4987 r = 0;
4988 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02004989 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02004990 r = -EBUSY;
4991 else
4992 kvm->arch.bsp_vcpu_id = arg;
4993 mutex_unlock(&kvm->lock);
4994 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07004995 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02004996 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07004997 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02004998 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07004999 goto out;
5000 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005001 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005002 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005003 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005004 r = 0;
5005 break;
5006 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005007 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005008 struct kvm_clock_data user_ns;
5009 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005010
5011 r = -EFAULT;
5012 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5013 goto out;
5014
5015 r = -EINVAL;
5016 if (user_ns.flags)
5017 goto out;
5018
5019 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005020 /*
5021 * TODO: userspace has to take care of races with VCPU_RUN, so
5022 * kvm_gen_update_masterclock() can be cut down to locked
5023 * pvclock_update_vm_gtod_copy().
5024 */
5025 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005026 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005027 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005028 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005029 break;
5030 }
5031 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005032 struct kvm_clock_data user_ns;
5033 u64 now_ns;
5034
Marcelo Tosattie891a322017-04-17 12:51:37 -03005035 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005036 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005037 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005038 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005039
5040 r = -EFAULT;
5041 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5042 goto out;
5043 r = 0;
5044 break;
5045 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005046 case KVM_MEMORY_ENCRYPT_OP: {
5047 r = -ENOTTY;
5048 if (kvm_x86_ops->mem_enc_op)
5049 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5050 break;
5051 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005052 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5053 struct kvm_enc_region region;
5054
5055 r = -EFAULT;
5056 if (copy_from_user(&region, argp, sizeof(region)))
5057 goto out;
5058
5059 r = -ENOTTY;
5060 if (kvm_x86_ops->mem_enc_reg_region)
5061 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5062 break;
5063 }
5064 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5065 struct kvm_enc_region region;
5066
5067 r = -EFAULT;
5068 if (copy_from_user(&region, argp, sizeof(region)))
5069 goto out;
5070
5071 r = -ENOTTY;
5072 if (kvm_x86_ops->mem_enc_unreg_region)
5073 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5074 break;
5075 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005076 case KVM_HYPERV_EVENTFD: {
5077 struct kvm_hyperv_eventfd hvevfd;
5078
5079 r = -EFAULT;
5080 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5081 goto out;
5082 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5083 break;
5084 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005085 case KVM_SET_PMU_EVENT_FILTER:
5086 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5087 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005088 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005089 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005090 }
5091out:
5092 return r;
5093}
5094
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005095static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005096{
5097 u32 dummy[2];
5098 unsigned i, j;
5099
Jim Mattsone2ada662019-08-21 11:20:04 -07005100 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
5101 "Please update the fixed PMCs in msrs_to_save[]");
5102 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_GENERIC != 32,
5103 "Please update the generic perfctr/eventsel MSRs in msrs_to_save[]");
5104
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005105 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02005106 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
5107 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005108
5109 /*
5110 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005111 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005112 */
5113 switch (msrs_to_save[i]) {
5114 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005115 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005116 continue;
5117 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005118 case MSR_TSC_AUX:
5119 if (!kvm_x86_ops->rdtscp_supported())
5120 continue;
5121 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005122 case MSR_IA32_RTIT_CTL:
5123 case MSR_IA32_RTIT_STATUS:
5124 if (!kvm_x86_ops->pt_supported())
5125 continue;
5126 break;
5127 case MSR_IA32_RTIT_CR3_MATCH:
5128 if (!kvm_x86_ops->pt_supported() ||
5129 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5130 continue;
5131 break;
5132 case MSR_IA32_RTIT_OUTPUT_BASE:
5133 case MSR_IA32_RTIT_OUTPUT_MASK:
5134 if (!kvm_x86_ops->pt_supported() ||
5135 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5136 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5137 continue;
5138 break;
5139 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
5140 if (!kvm_x86_ops->pt_supported() ||
5141 msrs_to_save[i] - MSR_IA32_RTIT_ADDR0_A >=
5142 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5143 continue;
5144 break;
5145 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005146 default:
5147 break;
5148 }
5149
Carsten Otte043405e2007-10-10 17:16:19 +02005150 if (j < i)
5151 msrs_to_save[j] = msrs_to_save[i];
5152 j++;
5153 }
5154 num_msrs_to_save = j;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005155
5156 for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
Tom Lendackybc226f02018-05-10 22:06:39 +02005157 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs[i]))
5158 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005159
5160 if (j < i)
5161 emulated_msrs[j] = emulated_msrs[i];
5162 j++;
5163 }
5164 num_emulated_msrs = j;
Tom Lendacky801e4592018-02-21 13:39:51 -06005165
5166 for (i = j = 0; i < ARRAY_SIZE(msr_based_features); i++) {
5167 struct kvm_msr_entry msr;
5168
5169 msr.index = msr_based_features[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005170 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005171 continue;
5172
5173 if (j < i)
5174 msr_based_features[j] = msr_based_features[i];
5175 j++;
5176 }
5177 num_msr_based_features = j;
Carsten Otte043405e2007-10-10 17:16:19 +02005178}
5179
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005180static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5181 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005182{
Avi Kivity70252a12010-01-19 12:51:22 +02005183 int handled = 0;
5184 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005185
Avi Kivity70252a12010-01-19 12:51:22 +02005186 do {
5187 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005188 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005189 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5190 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005191 break;
5192 handled += n;
5193 addr += n;
5194 len -= n;
5195 v += n;
5196 } while (len);
5197
5198 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005199}
5200
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005201static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005202{
Avi Kivity70252a12010-01-19 12:51:22 +02005203 int handled = 0;
5204 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005205
Avi Kivity70252a12010-01-19 12:51:22 +02005206 do {
5207 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005208 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005209 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5210 addr, n, v))
5211 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005212 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005213 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005214 handled += n;
5215 addr += n;
5216 len -= n;
5217 v += n;
5218 } while (len);
5219
5220 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005221}
5222
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005223static void kvm_set_segment(struct kvm_vcpu *vcpu,
5224 struct kvm_segment *var, int seg)
5225{
5226 kvm_x86_ops->set_segment(vcpu, var, seg);
5227}
5228
5229void kvm_get_segment(struct kvm_vcpu *vcpu,
5230 struct kvm_segment *var, int seg)
5231{
5232 kvm_x86_ops->get_segment(vcpu, var, seg);
5233}
5234
Paolo Bonzini54987b72014-09-02 13:23:06 +02005235gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5236 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005237{
5238 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005239
5240 BUG_ON(!mmu_is_nested(vcpu));
5241
5242 /* NPT walks are always user-walks */
5243 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005244 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005245
5246 return t_gpa;
5247}
5248
Avi Kivityab9ae312010-11-22 17:53:26 +02005249gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5250 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005251{
5252 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005253 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005254}
5255
Avi Kivityab9ae312010-11-22 17:53:26 +02005256 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5257 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005258{
5259 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5260 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005261 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005262}
5263
Avi Kivityab9ae312010-11-22 17:53:26 +02005264gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5265 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005266{
5267 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5268 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005269 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005270}
5271
5272/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005273gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5274 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005275{
Avi Kivityab9ae312010-11-22 17:53:26 +02005276 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005277}
5278
5279static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5280 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005281 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005282{
5283 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005284 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005285
5286 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005287 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005288 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005289 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005290 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005291 int ret;
5292
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005293 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005294 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005295 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5296 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005297 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005298 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005299 goto out;
5300 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005301
Izik Eidus77c20022008-12-29 01:42:19 +02005302 bytes -= toread;
5303 data += toread;
5304 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005305 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005306out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005307 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005308}
Izik Eidus77c20022008-12-29 01:42:19 +02005309
Gleb Natapov1871c602010-02-10 14:21:32 +02005310/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005311static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5312 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005313 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005314{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005315 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005316 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005317 unsigned offset;
5318 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005319
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005320 /* Inline kvm_read_guest_virt_helper for speed. */
5321 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5322 exception);
5323 if (unlikely(gpa == UNMAPPED_GVA))
5324 return X86EMUL_PROPAGATE_FAULT;
5325
5326 offset = addr & (PAGE_SIZE-1);
5327 if (WARN_ON(offset + bytes > PAGE_SIZE))
5328 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005329 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5330 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005331 if (unlikely(ret < 0))
5332 return X86EMUL_IO_NEEDED;
5333
5334 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005335}
5336
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005337int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005338 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005339 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005340{
5341 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005342
Paolo Bonzini353c0952019-01-29 18:41:16 +01005343 /*
5344 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5345 * is returned, but our callers are not ready for that and they blindly
5346 * call kvm_inject_page_fault. Ensure that they at least do not leak
5347 * uninitialized kernel stack memory into cr2 and error code.
5348 */
5349 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005350 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005351 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005352}
Nadav Har'El064aea72011-05-25 23:04:56 +03005353EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005354
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005355static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5356 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005357 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005358{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005359 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005360 u32 access = 0;
5361
5362 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5363 access |= PFERR_USER_MASK;
5364
5365 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005366}
5367
Radim Krčmář7a036a62015-10-30 16:36:24 +01005368static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5369 unsigned long addr, void *val, unsigned int bytes)
5370{
5371 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5372 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5373
5374 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5375}
5376
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005377static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5378 struct kvm_vcpu *vcpu, u32 access,
5379 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005380{
5381 void *data = val;
5382 int r = X86EMUL_CONTINUE;
5383
5384 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005385 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005386 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005387 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005388 unsigned offset = addr & (PAGE_SIZE-1);
5389 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5390 int ret;
5391
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005392 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005393 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005394 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005395 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005396 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005397 goto out;
5398 }
5399
5400 bytes -= towrite;
5401 data += towrite;
5402 addr += towrite;
5403 }
5404out:
5405 return r;
5406}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005407
5408static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005409 unsigned int bytes, struct x86_exception *exception,
5410 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005411{
5412 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005413 u32 access = PFERR_WRITE_MASK;
5414
5415 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5416 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005417
5418 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005419 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005420}
5421
5422int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5423 unsigned int bytes, struct x86_exception *exception)
5424{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005425 /* kvm_write_guest_virt_system can pull in tons of pages. */
5426 vcpu->arch.l1tf_flush_l1d = true;
5427
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005428 /*
5429 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5430 * is returned, but our callers are not ready for that and they blindly
5431 * call kvm_inject_page_fault. Ensure that they at least do not leak
5432 * uninitialized kernel stack memory into cr2 and error code.
5433 */
5434 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005435 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5436 PFERR_WRITE_MASK, exception);
5437}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005438EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005439
Wanpeng Li082d06e2018-04-03 16:28:48 -07005440int handle_ud(struct kvm_vcpu *vcpu)
5441{
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005442 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005443 enum emulation_result er;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005444 char sig[5]; /* ud2; .ascii "kvm" */
5445 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005446
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005447 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005448 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5449 sig, sizeof(sig), &e) == 0 &&
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005450 memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0) {
5451 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
5452 emul_type = 0;
5453 }
5454
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005455 er = kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005456 if (er == EMULATE_USER_EXIT)
5457 return 0;
5458 if (er != EMULATE_DONE)
5459 kvm_queue_exception(vcpu, UD_VECTOR);
5460 return 1;
5461}
5462EXPORT_SYMBOL_GPL(handle_ud);
5463
Tom Lendacky0f89b202016-12-14 14:59:23 -05005464static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5465 gpa_t gpa, bool write)
5466{
5467 /* For APIC access vmexit */
5468 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5469 return 1;
5470
5471 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5472 trace_vcpu_match_mmio(gva, gpa, write, true);
5473 return 1;
5474 }
5475
5476 return 0;
5477}
5478
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005479static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5480 gpa_t *gpa, struct x86_exception *exception,
5481 bool write)
5482{
Avi Kivity97d64b72012-09-12 14:52:00 +03005483 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5484 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005485
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005486 /*
5487 * currently PKRU is only applied to ept enabled guest so
5488 * there is no pkey in EPT page table for L1 guest or EPT
5489 * shadow page table for L2 guest.
5490 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005491 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005492 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005493 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005494 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5495 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005496 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005497 return 1;
5498 }
5499
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005500 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5501
5502 if (*gpa == UNMAPPED_GVA)
5503 return -1;
5504
Tom Lendacky0f89b202016-12-14 14:59:23 -05005505 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005506}
5507
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005508int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005509 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005510{
5511 int ret;
5512
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005513 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005514 if (ret < 0)
5515 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005516 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005517 return 1;
5518}
5519
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005520struct read_write_emulator_ops {
5521 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5522 int bytes);
5523 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5524 void *val, int bytes);
5525 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5526 int bytes, void *val);
5527 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5528 void *val, int bytes);
5529 bool write;
5530};
5531
5532static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5533{
5534 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005535 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005536 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005537 vcpu->mmio_read_completed = 0;
5538 return 1;
5539 }
5540
5541 return 0;
5542}
5543
5544static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5545 void *val, int bytes)
5546{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005547 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005548}
5549
5550static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5551 void *val, int bytes)
5552{
5553 return emulator_write_phys(vcpu, gpa, val, bytes);
5554}
5555
5556static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5557{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005558 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005559 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5560}
5561
5562static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5563 void *val, int bytes)
5564{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005565 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005566 return X86EMUL_IO_NEEDED;
5567}
5568
5569static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5570 void *val, int bytes)
5571{
Avi Kivityf78146b2012-04-18 19:22:47 +03005572 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5573
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005574 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005575 return X86EMUL_CONTINUE;
5576}
5577
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005578static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005579 .read_write_prepare = read_prepare,
5580 .read_write_emulate = read_emulate,
5581 .read_write_mmio = vcpu_mmio_read,
5582 .read_write_exit_mmio = read_exit_mmio,
5583};
5584
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005585static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005586 .read_write_emulate = write_emulate,
5587 .read_write_mmio = write_mmio,
5588 .read_write_exit_mmio = write_exit_mmio,
5589 .write = true,
5590};
5591
Xiao Guangrong22388a32011-07-13 14:32:31 +08005592static int emulator_read_write_onepage(unsigned long addr, void *val,
5593 unsigned int bytes,
5594 struct x86_exception *exception,
5595 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005596 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005597{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005598 gpa_t gpa;
5599 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005600 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005601 struct kvm_mmio_fragment *frag;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005602 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
5603
5604 /*
5605 * If the exit was due to a NPF we may already have a GPA.
5606 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5607 * Note, this cannot be used on string operations since string
5608 * operation using rep will only have the initial GPA from the NPF
5609 * occurred.
5610 */
5611 if (vcpu->arch.gpa_available &&
5612 emulator_can_use_gpa(ctxt) &&
Brijesh Singh618232e2017-08-17 18:36:57 +02005613 (addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
5614 gpa = vcpu->arch.gpa_val;
5615 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5616 } else {
5617 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5618 if (ret < 0)
5619 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005620 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005621
Brijesh Singh618232e2017-08-17 18:36:57 +02005622 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005623 return X86EMUL_CONTINUE;
5624
Carsten Ottebbd9b642007-10-30 18:44:21 +01005625 /*
5626 * Is this MMIO handled locally?
5627 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005628 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005629 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005630 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005631
Avi Kivity70252a12010-01-19 12:51:22 +02005632 gpa += handled;
5633 bytes -= handled;
5634 val += handled;
5635
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005636 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5637 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5638 frag->gpa = gpa;
5639 frag->data = val;
5640 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005641 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005642}
5643
Xiubo Li52eb5a62015-03-13 17:39:45 +08005644static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5645 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005646 void *val, unsigned int bytes,
5647 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005648 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005649{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005650 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005651 gpa_t gpa;
5652 int rc;
5653
5654 if (ops->read_write_prepare &&
5655 ops->read_write_prepare(vcpu, val, bytes))
5656 return X86EMUL_CONTINUE;
5657
5658 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005659
Carsten Ottebbd9b642007-10-30 18:44:21 +01005660 /* Crossing a page boundary? */
5661 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005662 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005663
5664 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005665 rc = emulator_read_write_onepage(addr, val, now, exception,
5666 vcpu, ops);
5667
Carsten Ottebbd9b642007-10-30 18:44:21 +01005668 if (rc != X86EMUL_CONTINUE)
5669 return rc;
5670 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005671 if (ctxt->mode != X86EMUL_MODE_PROT64)
5672 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005673 val += now;
5674 bytes -= now;
5675 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005676
Avi Kivityf78146b2012-04-18 19:22:47 +03005677 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5678 vcpu, ops);
5679 if (rc != X86EMUL_CONTINUE)
5680 return rc;
5681
5682 if (!vcpu->mmio_nr_fragments)
5683 return rc;
5684
5685 gpa = vcpu->mmio_fragments[0].gpa;
5686
5687 vcpu->mmio_needed = 1;
5688 vcpu->mmio_cur_fragment = 0;
5689
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005690 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005691 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5692 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5693 vcpu->run->mmio.phys_addr = gpa;
5694
5695 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005696}
5697
5698static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5699 unsigned long addr,
5700 void *val,
5701 unsigned int bytes,
5702 struct x86_exception *exception)
5703{
5704 return emulator_read_write(ctxt, addr, val, bytes,
5705 exception, &read_emultor);
5706}
5707
Xiubo Li52eb5a62015-03-13 17:39:45 +08005708static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005709 unsigned long addr,
5710 const void *val,
5711 unsigned int bytes,
5712 struct x86_exception *exception)
5713{
5714 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5715 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005716}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005717
Avi Kivitydaea3e72010-03-15 13:59:54 +02005718#define CMPXCHG_TYPE(t, ptr, old, new) \
5719 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5720
5721#ifdef CONFIG_X86_64
5722# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5723#else
5724# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005725 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005726#endif
5727
Avi Kivity0f65dd72011-04-20 13:37:53 +03005728static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5729 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005730 const void *old,
5731 const void *new,
5732 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005733 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005734{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005735 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005736 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005737 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005738 char *kaddr;
5739 bool exchanged;
5740
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005741 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005742 if (bytes > 8 || (bytes & (bytes - 1)))
5743 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005744
Avi Kivitydaea3e72010-03-15 13:59:54 +02005745 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005746
Avi Kivitydaea3e72010-03-15 13:59:54 +02005747 if (gpa == UNMAPPED_GVA ||
5748 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5749 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005750
Avi Kivitydaea3e72010-03-15 13:59:54 +02005751 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5752 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005753
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005754 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005755 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005756
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005757 kaddr = map.hva + offset_in_page(gpa);
5758
Avi Kivitydaea3e72010-03-15 13:59:54 +02005759 switch (bytes) {
5760 case 1:
5761 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5762 break;
5763 case 2:
5764 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5765 break;
5766 case 4:
5767 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5768 break;
5769 case 8:
5770 exchanged = CMPXCHG64(kaddr, old, new);
5771 break;
5772 default:
5773 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005774 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005775
5776 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005777
5778 if (!exchanged)
5779 return X86EMUL_CMPXCHG_FAILED;
5780
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005781 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005782
5783 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005784
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005785emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005786 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005787
Avi Kivity0f65dd72011-04-20 13:37:53 +03005788 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005789}
5790
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005791static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5792{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005793 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005794
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005795 for (i = 0; i < vcpu->arch.pio.count; i++) {
5796 if (vcpu->arch.pio.in)
5797 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5798 vcpu->arch.pio.size, pd);
5799 else
5800 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5801 vcpu->arch.pio.port, vcpu->arch.pio.size,
5802 pd);
5803 if (r)
5804 break;
5805 pd += vcpu->arch.pio.size;
5806 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005807 return r;
5808}
5809
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005810static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5811 unsigned short port, void *val,
5812 unsigned int count, bool in)
5813{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005814 vcpu->arch.pio.port = port;
5815 vcpu->arch.pio.in = in;
5816 vcpu->arch.pio.count = count;
5817 vcpu->arch.pio.size = size;
5818
5819 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5820 vcpu->arch.pio.count = 0;
5821 return 1;
5822 }
5823
5824 vcpu->run->exit_reason = KVM_EXIT_IO;
5825 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5826 vcpu->run->io.size = size;
5827 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5828 vcpu->run->io.count = count;
5829 vcpu->run->io.port = port;
5830
5831 return 0;
5832}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005833
Avi Kivityca1d4a92011-04-20 13:37:53 +03005834static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5835 int size, unsigned short port, void *val,
5836 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005837{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005838 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005839 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005840
Gleb Natapov79729952010-03-18 15:20:24 +02005841 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005842 goto data_avail;
5843
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005844 memset(vcpu->arch.pio_data, 0, size * count);
5845
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005846 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5847 if (ret) {
5848data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005849 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005850 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005851 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005852 return 1;
5853 }
5854
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005855 return 0;
5856}
5857
Avi Kivityca1d4a92011-04-20 13:37:53 +03005858static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5859 int size, unsigned short port,
5860 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005861{
Avi Kivityca1d4a92011-04-20 13:37:53 +03005862 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5863
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005864 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005865 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005866 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005867}
5868
Carsten Ottebbd9b642007-10-30 18:44:21 +01005869static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5870{
5871 return kvm_x86_ops->get_segment_base(vcpu, seg);
5872}
5873
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005874static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005875{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005876 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005877}
5878
Jiang Biaoae6a2372016-11-07 08:54:51 +08005879static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005880{
5881 if (!need_emulate_wbinvd(vcpu))
5882 return X86EMUL_CONTINUE;
5883
5884 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005885 int cpu = get_cpu();
5886
5887 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005888 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5889 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005890 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005891 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005892 } else
5893 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005894 return X86EMUL_CONTINUE;
5895}
Joel Schopp5cb56052015-03-02 13:43:31 -06005896
5897int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
5898{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005899 kvm_emulate_wbinvd_noskip(vcpu);
5900 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06005901}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005902EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
5903
Joel Schopp5cb56052015-03-02 13:43:31 -06005904
5905
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005906static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
5907{
Joel Schopp5cb56052015-03-02 13:43:31 -06005908 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03005909}
5910
Xiubo Li52eb5a62015-03-13 17:39:45 +08005911static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
5912 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005913{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005914 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005915}
5916
Xiubo Li52eb5a62015-03-13 17:39:45 +08005917static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
5918 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005919{
Gleb Natapov338dbc92010-04-28 19:15:32 +03005920
Avi Kivity717746e2011-04-20 13:37:53 +03005921 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005922}
5923
Gleb Natapov52a46612010-03-18 15:20:03 +02005924static u64 mk_cr_64(u64 curr_cr, u32 new_val)
5925{
5926 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
5927}
5928
Avi Kivity717746e2011-04-20 13:37:53 +03005929static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02005930{
Avi Kivity717746e2011-04-20 13:37:53 +03005931 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02005932 unsigned long value;
5933
5934 switch (cr) {
5935 case 0:
5936 value = kvm_read_cr0(vcpu);
5937 break;
5938 case 2:
5939 value = vcpu->arch.cr2;
5940 break;
5941 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005942 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02005943 break;
5944 case 4:
5945 value = kvm_read_cr4(vcpu);
5946 break;
5947 case 8:
5948 value = kvm_get_cr8(vcpu);
5949 break;
5950 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03005951 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02005952 return 0;
5953 }
5954
5955 return value;
5956}
5957
Avi Kivity717746e2011-04-20 13:37:53 +03005958static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02005959{
Avi Kivity717746e2011-04-20 13:37:53 +03005960 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03005961 int res = 0;
5962
Gleb Natapov52a46612010-03-18 15:20:03 +02005963 switch (cr) {
5964 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03005965 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02005966 break;
5967 case 2:
5968 vcpu->arch.cr2 = val;
5969 break;
5970 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005971 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02005972 break;
5973 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03005974 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02005975 break;
5976 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005977 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02005978 break;
5979 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03005980 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03005981 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02005982 }
Gleb Natapov0f122442010-04-28 19:15:31 +03005983
5984 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02005985}
5986
Avi Kivity717746e2011-04-20 13:37:53 +03005987static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02005988{
Avi Kivity717746e2011-04-20 13:37:53 +03005989 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02005990}
5991
Avi Kivity4bff1e862011-04-20 13:37:53 +03005992static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005993{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005994 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005995}
5996
Avi Kivity4bff1e862011-04-20 13:37:53 +03005997static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03005998{
Avi Kivity4bff1e862011-04-20 13:37:53 +03005999 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006000}
6001
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006002static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6003{
6004 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6005}
6006
6007static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6008{
6009 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6010}
6011
Avi Kivity4bff1e862011-04-20 13:37:53 +03006012static unsigned long emulator_get_cached_segment_base(
6013 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006014{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006015 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006016}
6017
Avi Kivity1aa36612011-04-27 13:20:30 +03006018static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6019 struct desc_struct *desc, u32 *base3,
6020 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006021{
6022 struct kvm_segment var;
6023
Avi Kivity4bff1e862011-04-20 13:37:53 +03006024 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006025 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006026
Gleb Natapov378a8b02013-01-21 15:36:48 +02006027 if (var.unusable) {
6028 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006029 if (base3)
6030 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006031 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006032 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006033
6034 if (var.g)
6035 var.limit >>= 12;
6036 set_desc_limit(desc, var.limit);
6037 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006038#ifdef CONFIG_X86_64
6039 if (base3)
6040 *base3 = var.base >> 32;
6041#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006042 desc->type = var.type;
6043 desc->s = var.s;
6044 desc->dpl = var.dpl;
6045 desc->p = var.present;
6046 desc->avl = var.avl;
6047 desc->l = var.l;
6048 desc->d = var.db;
6049 desc->g = var.g;
6050
6051 return true;
6052}
6053
Avi Kivity1aa36612011-04-27 13:20:30 +03006054static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6055 struct desc_struct *desc, u32 base3,
6056 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006057{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006058 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006059 struct kvm_segment var;
6060
Avi Kivity1aa36612011-04-27 13:20:30 +03006061 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006062 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006063#ifdef CONFIG_X86_64
6064 var.base |= ((u64)base3) << 32;
6065#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006066 var.limit = get_desc_limit(desc);
6067 if (desc->g)
6068 var.limit = (var.limit << 12) | 0xfff;
6069 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006070 var.dpl = desc->dpl;
6071 var.db = desc->d;
6072 var.s = desc->s;
6073 var.l = desc->l;
6074 var.g = desc->g;
6075 var.avl = desc->avl;
6076 var.present = desc->p;
6077 var.unusable = !var.present;
6078 var.padding = 0;
6079
6080 kvm_set_segment(vcpu, &var, seg);
6081 return;
6082}
6083
Avi Kivity717746e2011-04-20 13:37:53 +03006084static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6085 u32 msr_index, u64 *pdata)
6086{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006087 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006088}
6089
6090static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6091 u32 msr_index, u64 data)
6092{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006093 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006094}
6095
Paolo Bonzini64d60672015-05-07 11:36:11 +02006096static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6097{
6098 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6099
6100 return vcpu->arch.smbase;
6101}
6102
6103static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6104{
6105 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6106
6107 vcpu->arch.smbase = smbase;
6108}
6109
Nadav Amit67f4d422014-06-02 18:34:09 +03006110static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6111 u32 pmc)
6112{
Wei Huangc6702c92015-06-19 13:44:45 +02006113 return kvm_pmu_is_valid_msr_idx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006114}
6115
Avi Kivity222d21a2011-11-10 14:57:30 +02006116static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6117 u32 pmc, u64 *pdata)
6118{
Wei Huangc6702c92015-06-19 13:44:45 +02006119 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006120}
6121
Avi Kivity6c3287f2011-04-20 15:43:05 +03006122static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6123{
6124 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6125}
6126
Avi Kivity29535382011-04-20 13:37:53 +03006127static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006128 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006129 enum x86_intercept_stage stage)
6130{
Avi Kivity29535382011-04-20 13:37:53 +03006131 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006132}
6133
Yu Zhange911eb32017-08-24 20:27:52 +08006134static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6135 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006136{
Yu Zhange911eb32017-08-24 20:27:52 +08006137 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006138}
6139
Avi Kivitydd856ef2012-08-27 23:46:17 +03006140static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6141{
6142 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6143}
6144
6145static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6146{
6147 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6148}
6149
Nadav Amit801806d2015-01-26 09:32:23 +02006150static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6151{
6152 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6153}
6154
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006155static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6156{
6157 return emul_to_vcpu(ctxt)->arch.hflags;
6158}
6159
6160static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6161{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006162 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006163}
6164
Sean Christophersoned193212019-04-02 08:03:09 -07006165static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6166 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006167{
Sean Christophersoned193212019-04-02 08:03:09 -07006168 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006169}
6170
Sean Christophersonc5833c72019-04-02 08:03:10 -07006171static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6172{
6173 kvm_smm_changed(emul_to_vcpu(ctxt));
6174}
6175
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006176static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6177{
6178 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6179}
6180
Mathias Krause0225fb52012-08-30 01:30:16 +02006181static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006182 .read_gpr = emulator_read_gpr,
6183 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006184 .read_std = emulator_read_std,
6185 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006186 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006187 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006188 .read_emulated = emulator_read_emulated,
6189 .write_emulated = emulator_write_emulated,
6190 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006191 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006192 .pio_in_emulated = emulator_pio_in_emulated,
6193 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006194 .get_segment = emulator_get_segment,
6195 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006196 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006197 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006198 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006199 .set_gdt = emulator_set_gdt,
6200 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006201 .get_cr = emulator_get_cr,
6202 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006203 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006204 .get_dr = emulator_get_dr,
6205 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006206 .get_smbase = emulator_get_smbase,
6207 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006208 .set_msr = emulator_set_msr,
6209 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006210 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006211 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006212 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006213 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006214 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006215 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006216 .get_cpuid = emulator_get_cpuid,
Nadav Amit801806d2015-01-26 09:32:23 +02006217 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006218 .get_hflags = emulator_get_hflags,
6219 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006220 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006221 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006222 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006223};
6224
Gleb Natapov95cb2292010-04-28 19:15:43 +03006225static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6226{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006227 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006228 /*
6229 * an sti; sti; sequence only disable interrupts for the first
6230 * instruction. So, if the last instruction, be it emulated or
6231 * not, left the system with the INT_STI flag enabled, it
6232 * means that the last instruction is an sti. We should not
6233 * leave the flag on in this case. The same goes for mov ss
6234 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006235 if (int_shadow & mask)
6236 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006237 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006238 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006239 if (!mask)
6240 kvm_make_request(KVM_REQ_EVENT, vcpu);
6241 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006242}
6243
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006244static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006245{
6246 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006247 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006248 return kvm_propagate_fault(vcpu, &ctxt->exception);
6249
6250 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006251 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6252 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006253 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006254 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006255 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006256}
6257
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006258static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6259{
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006260 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006261 int cs_db, cs_l;
6262
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006263 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6264
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006265 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006266 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6267
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006268 ctxt->eip = kvm_rip_read(vcpu);
6269 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6270 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006271 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006272 cs_db ? X86EMUL_MODE_PROT32 :
6273 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006274 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006275 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6276 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006277
Avi Kivitydd856ef2012-08-27 23:46:17 +03006278 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006279 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006280}
6281
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006282int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006283{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006284 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006285 int ret;
6286
6287 init_emulate_ctxt(vcpu);
6288
Avi Kivity9dac77f2011-06-01 15:34:25 +03006289 ctxt->op_bytes = 2;
6290 ctxt->ad_bytes = 2;
6291 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006292 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006293
6294 if (ret != X86EMUL_CONTINUE)
6295 return EMULATE_FAIL;
6296
Avi Kivity9dac77f2011-06-01 15:34:25 +03006297 ctxt->eip = ctxt->_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006298 kvm_rip_write(vcpu, ctxt->eip);
6299 kvm_set_rflags(vcpu, ctxt->eflags);
Mohammed Gamal63995652010-09-19 14:34:06 +02006300
Mohammed Gamal63995652010-09-19 14:34:06 +02006301 return EMULATE_DONE;
6302}
6303EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6304
Liran Alone2366172018-03-12 13:12:49 +02006305static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006306{
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006307 int r = EMULATE_DONE;
6308
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006309 ++vcpu->stat.insn_emulation_fail;
6310 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006311
6312 if (emulation_type & EMULTYPE_NO_UD_ON_FAIL)
6313 return EMULATE_FAIL;
6314
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006315 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006316 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6317 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6318 vcpu->run->internal.ndata = 0;
Liran Alon1f4dcb32017-11-05 16:56:33 +02006319 r = EMULATE_USER_EXIT;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006320 }
Liran Alone2366172018-03-12 13:12:49 +02006321
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006322 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006323
6324 return r;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006325}
6326
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006327static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006328 bool write_fault_to_shadow_pgtable,
6329 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006330{
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006331 gpa_t gpa = cr2;
Dan Williamsba049e92016-01-15 16:56:11 -08006332 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006333
Sean Christopherson384bf222018-08-23 13:56:49 -07006334 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006335 return false;
6336
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006337 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6338 return false;
6339
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006340 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006341 /*
6342 * Write permission should be allowed since only
6343 * write access need to be emulated.
6344 */
6345 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006346
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006347 /*
6348 * If the mapping is invalid in guest, let cpu retry
6349 * it to generate fault.
6350 */
6351 if (gpa == UNMAPPED_GVA)
6352 return true;
6353 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006354
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006355 /*
6356 * Do not retry the unhandleable instruction if it faults on the
6357 * readonly host memory, otherwise it will goto a infinite loop:
6358 * retry instruction -> write #PF -> emulation fail -> retry
6359 * instruction -> ...
6360 */
6361 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006362
6363 /*
6364 * If the instruction failed on the error pfn, it can not be fixed,
6365 * report the error to userspace.
6366 */
6367 if (is_error_noslot_pfn(pfn))
6368 return false;
6369
6370 kvm_release_pfn_clean(pfn);
6371
6372 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006373 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006374 unsigned int indirect_shadow_pages;
6375
6376 spin_lock(&vcpu->kvm->mmu_lock);
6377 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6378 spin_unlock(&vcpu->kvm->mmu_lock);
6379
6380 if (indirect_shadow_pages)
6381 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6382
Gleb Natapova6f177e2010-07-08 12:41:12 +03006383 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006384 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006385
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006386 /*
6387 * if emulation was due to access to shadowed page table
6388 * and it failed try to unshadow page and re-enter the
6389 * guest to let CPU execute the instruction.
6390 */
6391 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006392
6393 /*
6394 * If the access faults on its page table, it can not
6395 * be fixed by unprotecting shadow page and it should
6396 * be reported to userspace.
6397 */
6398 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006399}
6400
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006401static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
6402 unsigned long cr2, int emulation_type)
6403{
6404 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6405 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
6406
6407 last_retry_eip = vcpu->arch.last_retry_eip;
6408 last_retry_addr = vcpu->arch.last_retry_addr;
6409
6410 /*
6411 * If the emulation is caused by #PF and it is non-page_table
6412 * writing instruction, it means the VM-EXIT is caused by shadow
6413 * page protected, we can zap the shadow page and retry this
6414 * instruction directly.
6415 *
6416 * Note: if the guest uses a non-page-table modifying instruction
6417 * on the PDE that points to the instruction, then we will unmap
6418 * the instruction and go to an infinite loop. So, we cache the
6419 * last retried eip and the last fault address, if we meet the eip
6420 * and the address again, we can break out of the potential infinite
6421 * loop.
6422 */
6423 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6424
Sean Christopherson384bf222018-08-23 13:56:49 -07006425 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006426 return false;
6427
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006428 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6429 return false;
6430
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006431 if (x86_page_table_writing_insn(ctxt))
6432 return false;
6433
6434 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
6435 return false;
6436
6437 vcpu->arch.last_retry_eip = ctxt->eip;
6438 vcpu->arch.last_retry_addr = cr2;
6439
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006440 if (!vcpu->arch.mmu->direct_map)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006441 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
6442
Xiao Guangrong22368022013-01-13 23:44:12 +08006443 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006444
6445 return true;
6446}
6447
Gleb Natapov716d51a2012-09-03 15:24:26 +03006448static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6449static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6450
Paolo Bonzini64d60672015-05-07 11:36:11 +02006451static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006452{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006453 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006454 /* This is a good place to trace that we are exiting SMM. */
6455 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6456
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006457 /* Process a latched INIT or SMI, if any. */
6458 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006459 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006460
6461 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006462}
6463
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006464static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6465 unsigned long *db)
6466{
6467 u32 dr6 = 0;
6468 int i;
6469 u32 enable, rwlen;
6470
6471 enable = dr7;
6472 rwlen = dr7 >> 16;
6473 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6474 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6475 dr6 |= (1 << i);
6476 return dr6;
6477}
6478
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006479static void kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu, int *r)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006480{
6481 struct kvm_run *kvm_run = vcpu->run;
6482
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006483 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6484 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6485 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6486 kvm_run->debug.arch.exception = DB_VECTOR;
6487 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6488 *r = EMULATE_USER_EXIT;
6489 } else {
Jim Mattsonf10c7292018-10-16 14:29:23 -07006490 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006491 }
6492}
6493
Kyle Huey6affcbe2016-11-29 12:40:40 -08006494int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6495{
6496 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006497 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006498
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006499 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
6500 if (unlikely(r != EMULATE_DONE))
6501 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006502
6503 /*
6504 * rflags is the old, "raw" value of the flags. The new value has
6505 * not been saved yet.
6506 *
6507 * This is correct even for TF set by the guest, because "the
6508 * processor will not generate this exception after the instruction
6509 * that sets the TF flag".
6510 */
6511 if (unlikely(rflags & X86_EFLAGS_TF))
6512 kvm_vcpu_do_singlestep(vcpu, &r);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006513 return r == EMULATE_DONE;
6514}
6515EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6516
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006517static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6518{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006519 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6520 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006521 struct kvm_run *kvm_run = vcpu->run;
6522 unsigned long eip = kvm_get_linear_rip(vcpu);
6523 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006524 vcpu->arch.guest_debug_dr7,
6525 vcpu->arch.eff_db);
6526
6527 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006528 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006529 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006530 kvm_run->debug.arch.exception = DB_VECTOR;
6531 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6532 *r = EMULATE_USER_EXIT;
6533 return true;
6534 }
6535 }
6536
Nadav Amit4161a562014-07-17 01:19:31 +03006537 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6538 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006539 unsigned long eip = kvm_get_linear_rip(vcpu);
6540 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006541 vcpu->arch.dr7,
6542 vcpu->arch.db);
6543
6544 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006545 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006546 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006547 kvm_queue_exception(vcpu, DB_VECTOR);
6548 *r = EMULATE_DONE;
6549 return true;
6550 }
6551 }
6552
6553 return false;
6554}
6555
Liran Alon04789b62018-03-12 13:12:50 +02006556static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6557{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006558 switch (ctxt->opcode_len) {
6559 case 1:
6560 switch (ctxt->b) {
6561 case 0xe4: /* IN */
6562 case 0xe5:
6563 case 0xec:
6564 case 0xed:
6565 case 0xe6: /* OUT */
6566 case 0xe7:
6567 case 0xee:
6568 case 0xef:
6569 case 0x6c: /* INS */
6570 case 0x6d:
6571 case 0x6e: /* OUTS */
6572 case 0x6f:
6573 return true;
6574 }
6575 break;
6576 case 2:
6577 switch (ctxt->b) {
6578 case 0x33: /* RDPMC */
6579 return true;
6580 }
6581 break;
Liran Alon04789b62018-03-12 13:12:50 +02006582 }
6583
6584 return false;
6585}
6586
Andre Przywara51d8b662010-12-21 11:12:02 +01006587int x86_emulate_instruction(struct kvm_vcpu *vcpu,
6588 unsigned long cr2,
Andre Przywaradc25e892010-12-21 11:12:07 +01006589 int emulation_type,
6590 void *insn,
6591 int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006592{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006593 int r;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006594 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006595 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006596 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006597
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006598 vcpu->arch.l1tf_flush_l1d = true;
6599
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006600 /*
6601 * Clear write_fault_to_shadow_pgtable here to ensure it is
6602 * never reused.
6603 */
6604 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006605 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006606
Sheng Yang571008d2008-01-02 14:49:22 +08006607 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006608 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006609
6610 /*
6611 * We will reenter on the same instruction since
6612 * we do not set complete_userspace_io. This does not
6613 * handle watchpoints yet, those would be handled in
6614 * the emulate_ops.
6615 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006616 if (!(emulation_type & EMULTYPE_SKIP) &&
6617 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006618 return r;
6619
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006620 ctxt->interruptibility = 0;
6621 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006622 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006623 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006624
Borislav Petkovb51e9742013-09-22 16:44:52 +02006625 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006626
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006627 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006628
Avi Kivitye46479f2010-04-11 13:05:16 +03006629 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006630 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006631 if (r != EMULATION_OK) {
Avi Kivity40059962011-02-01 16:32:04 +02006632 if (emulation_type & EMULTYPE_TRAP_UD)
6633 return EMULATE_FAIL;
Gleb Natapov991eebf2013-04-11 12:10:51 +03006634 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6635 emulation_type))
Carsten Ottebbd9b642007-10-30 18:44:21 +01006636 return EMULATE_DONE;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006637 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006638 /*
6639 * #UD should result in just EMULATION_FAILED, and trap-like
6640 * exception should not be encountered during decode.
6641 */
6642 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6643 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006644 inject_emulated_exception(vcpu);
Paolo Bonzini6ea6e842017-11-10 10:49:38 +01006645 return EMULATE_DONE;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006646 }
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006647 if (emulation_type & EMULTYPE_SKIP)
6648 return EMULATE_FAIL;
Liran Alone2366172018-03-12 13:12:49 +02006649 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006650 }
6651 }
6652
Liran Alon04789b62018-03-12 13:12:50 +02006653 if ((emulation_type & EMULTYPE_VMWARE) &&
6654 !is_vmware_backdoor_opcode(ctxt))
6655 return EMULATE_FAIL;
6656
Gleb Natapovba8afb62009-04-12 13:36:57 +03006657 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006658 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006659 if (ctxt->eflags & X86_EFLAGS_RF)
6660 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Vitaly Kuznetsov97413d22019-08-13 15:53:31 +02006661 kvm_x86_ops->set_interrupt_shadow(vcpu, 0);
Gleb Natapovba8afb62009-04-12 13:36:57 +03006662 return EMULATE_DONE;
6663 }
6664
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006665 if (retry_instruction(ctxt, cr2, emulation_type))
6666 return EMULATE_DONE;
6667
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006668 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006669 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006670 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6671 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006672 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006673 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006674
Gleb Natapov5cd21912010-03-18 15:20:26 +02006675restart:
Tom Lendacky0f89b202016-12-14 14:59:23 -05006676 /* Save the faulting GPA (cr2) in the address field */
6677 ctxt->exception.address = cr2;
6678
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006679 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006680
Joerg Roedel775fde82011-04-04 12:39:24 +02006681 if (r == EMULATION_INTERCEPTED)
6682 return EMULATE_DONE;
6683
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006684 if (r == EMULATION_FAILED) {
Gleb Natapov991eebf2013-04-11 12:10:51 +03006685 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6686 emulation_type))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006687 return EMULATE_DONE;
6688
Liran Alone2366172018-03-12 13:12:49 +02006689 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006690 }
6691
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006692 if (ctxt->have_exception) {
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006693 r = EMULATE_DONE;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006694 if (inject_emulated_exception(vcpu))
6695 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006696 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006697 if (!vcpu->arch.pio.in) {
6698 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006699 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006700 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006701 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006702 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6703 }
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006704 r = EMULATE_USER_EXIT;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006705 } else if (vcpu->mmio_needed) {
6706 if (!vcpu->mmio_is_write)
6707 writeback = false;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006708 r = EMULATE_USER_EXIT;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006709 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006710 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006711 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006712 else
6713 r = EMULATE_DONE;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006714
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006715 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006716 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006717 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006718 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006719 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006720 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6721 kvm_rip_write(vcpu, ctxt->eip);
6722 if (r == EMULATE_DONE && ctxt->tf)
6723 kvm_vcpu_do_singlestep(vcpu, &r);
Nadav Amit38827db2014-11-02 11:54:53 +02006724 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006725 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006726
6727 /*
6728 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6729 * do nothing, and it will be requested again as soon as
6730 * the shadow expires. But we still need to check here,
6731 * because POPF has no interrupt shadow.
6732 */
6733 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6734 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006735 } else
6736 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006737
Gleb Natapove85d28f2010-07-29 15:11:52 +03006738 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006739}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006740
6741int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6742{
6743 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6744}
6745EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6746
6747int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6748 void *insn, int insn_len)
6749{
6750 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6751}
6752EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006753
Sean Christopherson8764ed52019-04-29 07:04:15 -07006754static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6755{
6756 vcpu->arch.pio.count = 0;
6757 return 1;
6758}
6759
Sean Christopherson45def772019-03-11 20:01:05 -07006760static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6761{
6762 vcpu->arch.pio.count = 0;
6763
6764 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6765 return 1;
6766
6767 return kvm_skip_emulated_instruction(vcpu);
6768}
6769
Sean Christophersondca7f122018-03-08 08:57:27 -08006770static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6771 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006772{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006773 unsigned long val = kvm_rax_read(vcpu);
Avi Kivityca1d4a92011-04-20 13:37:53 +03006774 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
6775 size, port, &val, 1);
Sean Christopherson8764ed52019-04-29 07:04:15 -07006776 if (ret)
6777 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006778
Sean Christopherson8764ed52019-04-29 07:04:15 -07006779 /*
6780 * Workaround userspace that relies on old KVM behavior of %rip being
6781 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6782 */
6783 if (port == 0x7e &&
6784 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6785 vcpu->arch.complete_userspace_io =
6786 complete_fast_pio_out_port_0x7e;
6787 kvm_skip_emulated_instruction(vcpu);
6788 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006789 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6790 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6791 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006792 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006793}
Carsten Ottede7d7892007-10-30 18:44:25 +01006794
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006795static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6796{
6797 unsigned long val;
6798
6799 /* We should only ever be called with arch.pio.count equal to 1 */
6800 BUG_ON(vcpu->arch.pio.count != 1);
6801
Sean Christopherson45def772019-03-11 20:01:05 -07006802 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6803 vcpu->arch.pio.count = 0;
6804 return 1;
6805 }
6806
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006807 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006808 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006809
6810 /*
6811 * Since vcpu->arch.pio.count == 1 let emulator_pio_in_emulated perform
6812 * the copy and tracing
6813 */
6814 emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, vcpu->arch.pio.size,
6815 vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006816 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006817
Sean Christopherson45def772019-03-11 20:01:05 -07006818 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006819}
6820
Sean Christophersondca7f122018-03-08 08:57:27 -08006821static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6822 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006823{
6824 unsigned long val;
6825 int ret;
6826
6827 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006828 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006829
6830 ret = emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, size, port,
6831 &val, 1);
6832 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07006833 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006834 return ret;
6835 }
6836
Sean Christopherson45def772019-03-11 20:01:05 -07006837 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006838 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
6839
6840 return 0;
6841}
Sean Christophersondca7f122018-03-08 08:57:27 -08006842
6843int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
6844{
Sean Christopherson45def772019-03-11 20:01:05 -07006845 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08006846
Sean Christophersondca7f122018-03-08 08:57:27 -08006847 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07006848 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08006849 else
Sean Christopherson45def772019-03-11 20:01:05 -07006850 ret = kvm_fast_pio_out(vcpu, size, port);
6851 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08006852}
6853EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006854
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006855static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006856{
Tejun Heo0a3aee02010-12-18 16:28:55 +01006857 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00006858 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006859}
6860
6861static void tsc_khz_changed(void *data)
6862{
6863 struct cpufreq_freqs *freq = data;
6864 unsigned long khz = 0;
6865
6866 if (data)
6867 khz = freq->new;
6868 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
6869 khz = cpufreq_quick_get(raw_smp_processor_id());
6870 if (!khz)
6871 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01006872 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006873}
6874
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006875#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006876static void kvm_hyperv_tsc_notifier(void)
6877{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006878 struct kvm *kvm;
6879 struct kvm_vcpu *vcpu;
6880 int cpu;
6881
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006882 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006883 list_for_each_entry(kvm, &vm_list, vm_list)
6884 kvm_make_mclock_inprogress_request(kvm);
6885
6886 hyperv_stop_tsc_emulation();
6887
6888 /* TSC frequency always matches when on Hyper-V */
6889 for_each_present_cpu(cpu)
6890 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
6891 kvm_max_guest_tsc_khz = tsc_khz;
6892
6893 list_for_each_entry(kvm, &vm_list, vm_list) {
6894 struct kvm_arch *ka = &kvm->arch;
6895
6896 spin_lock(&ka->pvclock_gtod_sync_lock);
6897
6898 pvclock_update_vm_gtod_copy(kvm);
6899
6900 kvm_for_each_vcpu(cpu, vcpu, kvm)
6901 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
6902
6903 kvm_for_each_vcpu(cpu, vcpu, kvm)
6904 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
6905
6906 spin_unlock(&ka->pvclock_gtod_sync_lock);
6907 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006908 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006909}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01006910#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01006911
Viresh Kumardf240142019-04-29 15:03:58 +05306912static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006913{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006914 struct kvm *kvm;
6915 struct kvm_vcpu *vcpu;
6916 int i, send_ipi = 0;
6917
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006918 /*
6919 * We allow guests to temporarily run on slowing clocks,
6920 * provided we notify them after, or to run on accelerating
6921 * clocks, provided we notify them before. Thus time never
6922 * goes backwards.
6923 *
6924 * However, we have a problem. We can't atomically update
6925 * the frequency of a given CPU from this function; it is
6926 * merely a notifier, which can be called from any CPU.
6927 * Changing the TSC frequency at arbitrary points in time
6928 * requires a recomputation of local variables related to
6929 * the TSC for each VCPU. We must flag these local variables
6930 * to be updated and be sure the update takes place with the
6931 * new frequency before any guests proceed.
6932 *
6933 * Unfortunately, the combination of hotplug CPU and frequency
6934 * change creates an intractable locking scenario; the order
6935 * of when these callouts happen is undefined with respect to
6936 * CPU hotplug, and they can race with each other. As such,
6937 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
6938 * undefined; you can actually have a CPU frequency change take
6939 * place in between the computation of X and the setting of the
6940 * variable. To protect against this problem, all updates of
6941 * the per_cpu tsc_khz variable are done in an interrupt
6942 * protected IPI, and all callers wishing to update the value
6943 * must wait for a synchronous IPI to complete (which is trivial
6944 * if the caller is on the CPU already). This establishes the
6945 * necessary total order on variable updates.
6946 *
6947 * Note that because a guest time update may take place
6948 * anytime after the setting of the VCPU's request bit, the
6949 * correct TSC value must be set before the request. However,
6950 * to ensure the update actually makes it to any guest which
6951 * starts running in hardware virtualization between the set
6952 * and the acquisition of the spinlock, we must also ping the
6953 * CPU after setting the request bit.
6954 *
6955 */
6956
Viresh Kumardf240142019-04-29 15:03:58 +05306957 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006958
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006959 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006960 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03006961 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05306962 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006963 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10006964 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006965 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10006966 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006967 }
6968 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08006969 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006970
6971 if (freq->old < freq->new && send_ipi) {
6972 /*
6973 * We upscale the frequency. Must make the guest
6974 * doesn't see old kvmclock values while running with
6975 * the new frequency, otherwise we risk the guest sees
6976 * time go backwards.
6977 *
6978 * In case we update the frequency for another cpu
6979 * (which might be in guest context) send an interrupt
6980 * to kick the cpu out of guest context. Next time
6981 * guest context is entered kvmclock will be updated,
6982 * so the guest will not see stale values.
6983 */
Viresh Kumardf240142019-04-29 15:03:58 +05306984 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01006985 }
Viresh Kumardf240142019-04-29 15:03:58 +05306986}
6987
6988static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
6989 void *data)
6990{
6991 struct cpufreq_freqs *freq = data;
6992 int cpu;
6993
6994 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
6995 return 0;
6996 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
6997 return 0;
6998
6999 for_each_cpu(cpu, freq->policy->cpus)
7000 __kvmclock_cpufreq_notifier(freq, cpu);
7001
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007002 return 0;
7003}
7004
7005static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007006 .notifier_call = kvmclock_cpufreq_notifier
7007};
7008
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007009static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007010{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007011 tsc_khz_changed(NULL);
7012 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007013}
7014
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007015static void kvm_timer_init(void)
7016{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007017 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307018
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007019 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007020#ifdef CONFIG_CPU_FREQ
7021 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007022 int cpu;
7023
Zachary Amsdenc2855452010-09-18 14:38:15 -10007024 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02007025 cpu = get_cpu();
7026 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007027 if (policy.cpuinfo.max_freq)
7028 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007029 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007030#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007031 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7032 CPUFREQ_TRANSITION_NOTIFIER);
7033 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307034
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007035 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007036 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007037}
7038
Andi Kleendd60d212017-07-25 17:20:32 -07007039DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7040EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007041
Gleb Natapovf5132b02011-11-10 14:57:22 +02007042int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007043{
Alex,Shi086c9852011-10-20 15:34:01 +08007044 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007045}
7046
7047static int kvm_is_user_mode(void)
7048{
7049 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007050
Alex,Shi086c9852011-10-20 15:34:01 +08007051 if (__this_cpu_read(current_vcpu))
7052 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007053
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007054 return user_mode != 0;
7055}
7056
7057static unsigned long kvm_get_guest_ip(void)
7058{
7059 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007060
Alex,Shi086c9852011-10-20 15:34:01 +08007061 if (__this_cpu_read(current_vcpu))
7062 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007063
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007064 return ip;
7065}
7066
Luwei Kang8479e042019-02-18 19:26:07 -05007067static void kvm_handle_intel_pt_intr(void)
7068{
7069 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7070
7071 kvm_make_request(KVM_REQ_PMI, vcpu);
7072 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7073 (unsigned long *)&vcpu->arch.pmu.global_status);
7074}
7075
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007076static struct perf_guest_info_callbacks kvm_guest_cbs = {
7077 .is_in_guest = kvm_is_in_guest,
7078 .is_user_mode = kvm_is_user_mode,
7079 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007080 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007081};
7082
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007083#ifdef CONFIG_X86_64
7084static void pvclock_gtod_update_fn(struct work_struct *work)
7085{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007086 struct kvm *kvm;
7087
7088 struct kvm_vcpu *vcpu;
7089 int i;
7090
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007091 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007092 list_for_each_entry(kvm, &vm_list, vm_list)
7093 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007094 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007095 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007096 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007097}
7098
7099static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7100
7101/*
7102 * Notification about pvclock gtod data update.
7103 */
7104static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7105 void *priv)
7106{
7107 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7108 struct timekeeper *tk = priv;
7109
7110 update_pvclock_gtod(tk);
7111
7112 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007113 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007114 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007115 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007116 atomic_read(&kvm_guest_has_master_clock) != 0)
7117 queue_work(system_long_wq, &pvclock_gtod_work);
7118
7119 return 0;
7120}
7121
7122static struct notifier_block pvclock_gtod_notifier = {
7123 .notifier_call = pvclock_gtod_notify,
7124};
7125#endif
7126
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007127int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007128{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007129 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007130 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007131
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007132 if (kvm_x86_ops) {
7133 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007134 r = -EEXIST;
7135 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007136 }
7137
7138 if (!ops->cpu_has_kvm_support()) {
7139 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007140 r = -EOPNOTSUPP;
7141 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007142 }
7143 if (ops->disabled_by_bios()) {
7144 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007145 r = -EOPNOTSUPP;
7146 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007147 }
7148
Marc Orrb666a4b2018-11-06 14:53:56 -08007149 /*
7150 * KVM explicitly assumes that the guest has an FPU and
7151 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7152 * vCPU's FPU state as a fxregs_state struct.
7153 */
7154 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7155 printk(KERN_ERR "kvm: inadequate fpu\n");
7156 r = -EOPNOTSUPP;
7157 goto out;
7158 }
7159
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007160 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007161 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007162 __alignof__(struct fpu), SLAB_ACCOUNT,
7163 NULL);
7164 if (!x86_fpu_cache) {
7165 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7166 goto out;
7167 }
7168
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007169 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7170 if (!shared_msrs) {
7171 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Marc Orrb666a4b2018-11-06 14:53:56 -08007172 goto out_free_x86_fpu_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007173 }
7174
Avi Kivity97db56c2008-01-13 13:23:56 +02007175 r = kvm_mmu_module_init();
7176 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007177 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007178
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007179 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007180
Sheng Yang7b523452008-04-25 21:13:50 +08007181 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007182 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007183 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007184 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007185
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007186 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7187
Borislav Petkovd366bf72016-04-04 22:25:02 +02007188 if (boot_cpu_has(X86_FEATURE_XSAVE))
Dexuan Cui2acf9232010-06-10 11:27:12 +08007189 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
7190
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007191 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007192 if (pi_inject_timer == -1)
7193 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007194#ifdef CONFIG_X86_64
7195 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007196
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007197 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007198 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007199#endif
7200
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007201 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007202
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007203out_free_percpu:
7204 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007205out_free_x86_fpu_cache:
7206 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007207out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007208 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007209}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007210
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007211void kvm_arch_exit(void)
7212{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007213#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007214 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007215 clear_hv_tscchange_cb();
7216#endif
David Matlackcef84c32016-12-16 14:30:36 -08007217 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007218 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7219
Jan Kiszka888d2562009-04-17 19:24:58 +02007220 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7221 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7222 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007223 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007224#ifdef CONFIG_X86_64
7225 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7226#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007227 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007228 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007229 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007230 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007231}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007232
Joel Schopp5cb56052015-03-02 13:43:31 -06007233int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007234{
7235 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007236 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007237 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007238 return 1;
7239 } else {
7240 vcpu->run->exit_reason = KVM_EXIT_HLT;
7241 return 0;
7242 }
7243}
Joel Schopp5cb56052015-03-02 13:43:31 -06007244EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7245
7246int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7247{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007248 int ret = kvm_skip_emulated_instruction(vcpu);
7249 /*
7250 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7251 * KVM_EXIT_DEBUG here.
7252 */
7253 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007254}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007255EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7256
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007257#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007258static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7259 unsigned long clock_type)
7260{
7261 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007262 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007263 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007264 int ret;
7265
7266 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7267 return -KVM_EOPNOTSUPP;
7268
7269 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7270 return -KVM_EOPNOTSUPP;
7271
7272 clock_pairing.sec = ts.tv_sec;
7273 clock_pairing.nsec = ts.tv_nsec;
7274 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7275 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007276 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007277
7278 ret = 0;
7279 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7280 sizeof(struct kvm_clock_pairing)))
7281 ret = -KVM_EFAULT;
7282
7283 return ret;
7284}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007285#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007286
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307287/*
7288 * kvm_pv_kick_cpu_op: Kick a vcpu.
7289 *
7290 * @apicid - apicid of vcpu to be kicked.
7291 */
7292static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7293{
Raghavendra K T24d21662013-08-26 14:18:35 +05307294 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307295
Raghavendra K T24d21662013-08-26 14:18:35 +05307296 lapic_irq.shorthand = 0;
7297 lapic_irq.dest_mode = 0;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007298 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307299 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007300 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307301
Raghavendra K T24d21662013-08-26 14:18:35 +05307302 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007303 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307304}
7305
Andrey Smetanind62caab2015-11-10 15:36:33 +03007306void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
7307{
Paolo Bonzinif7589cc2019-01-30 17:18:59 +01007308 if (!lapic_in_kernel(vcpu)) {
7309 WARN_ON_ONCE(vcpu->arch.apicv_active);
7310 return;
7311 }
7312 if (!vcpu->arch.apicv_active)
7313 return;
7314
Andrey Smetanind62caab2015-11-10 15:36:33 +03007315 vcpu->arch.apicv_active = false;
7316 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
7317}
7318
Wanpeng Li71506292019-06-11 20:23:49 +08007319static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7320{
7321 struct kvm_vcpu *target = NULL;
7322 struct kvm_apic_map *map;
7323
7324 rcu_read_lock();
7325 map = rcu_dereference(kvm->arch.apic_map);
7326
7327 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7328 target = map->phys_map[dest_id]->vcpu;
7329
7330 rcu_read_unlock();
7331
Wanpeng Li266e85a2019-07-24 17:43:13 +08007332 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007333 kvm_vcpu_yield_to(target);
7334}
7335
Hollis Blanchard8776e512007-10-31 17:24:24 -05007336int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7337{
7338 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007339 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007340
Radim Krčmář696ca772018-05-24 17:50:56 +02007341 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7342 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007343
Sean Christophersonde3cd112019-04-30 10:36:17 -07007344 nr = kvm_rax_read(vcpu);
7345 a0 = kvm_rbx_read(vcpu);
7346 a1 = kvm_rcx_read(vcpu);
7347 a2 = kvm_rdx_read(vcpu);
7348 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007349
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007350 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007351
Nadav Amita449c7a2014-06-18 17:19:24 +03007352 op_64_bit = is_64_bit_mode(vcpu);
7353 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007354 nr &= 0xFFFFFFFF;
7355 a0 &= 0xFFFFFFFF;
7356 a1 &= 0xFFFFFFFF;
7357 a2 &= 0xFFFFFFFF;
7358 a3 &= 0xFFFFFFFF;
7359 }
7360
Jan Kiszka07708c42009-08-03 18:43:28 +02007361 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7362 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007363 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007364 }
7365
Hollis Blanchard8776e512007-10-31 17:24:24 -05007366 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007367 case KVM_HC_VAPIC_POLL_IRQ:
7368 ret = 0;
7369 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307370 case KVM_HC_KICK_CPU:
7371 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007372 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307373 ret = 0;
7374 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007375#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007376 case KVM_HC_CLOCK_PAIRING:
7377 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7378 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007379#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007380 case KVM_HC_SEND_IPI:
7381 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7382 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007383 case KVM_HC_SCHED_YIELD:
7384 kvm_sched_yield(vcpu->kvm, a0);
7385 ret = 0;
7386 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007387 default:
7388 ret = -KVM_ENOSYS;
7389 break;
7390 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007391out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007392 if (!op_64_bit)
7393 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007394 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007395
Amit Shahf11c3a82008-02-21 01:00:30 +05307396 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007397 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007398}
7399EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7400
Jan Kiszkab6785de2012-09-20 07:43:17 +02007401static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007402{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007403 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007404 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007405 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007406
Hollis Blanchard8776e512007-10-31 17:24:24 -05007407 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007408
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007409 return emulator_write_emulated(ctxt, rip, instruction, 3,
7410 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007411}
7412
Avi Kivity851ba692009-08-24 11:10:17 +03007413static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007414{
Matt Gingell782d4222015-11-16 15:26:00 -08007415 return vcpu->run->request_interrupt_window &&
7416 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007417}
7418
Avi Kivity851ba692009-08-24 11:10:17 +03007419static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007420{
Avi Kivity851ba692009-08-24 11:10:17 +03007421 struct kvm_run *kvm_run = vcpu->run;
7422
Jan Kiszka91586a32009-10-05 13:07:21 +02007423 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007424 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007425 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007426 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007427 kvm_run->ready_for_interrupt_injection =
7428 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007429 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007430}
7431
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007432static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7433{
7434 int max_irr, tpr;
7435
7436 if (!kvm_x86_ops->update_cr8_intercept)
7437 return;
7438
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007439 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007440 return;
7441
Andrey Smetanind62caab2015-11-10 15:36:33 +03007442 if (vcpu->arch.apicv_active)
7443 return;
7444
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007445 if (!vcpu->arch.apic->vapic_addr)
7446 max_irr = kvm_lapic_find_highest_irr(vcpu);
7447 else
7448 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007449
7450 if (max_irr != -1)
7451 max_irr >>= 4;
7452
7453 tpr = kvm_lapic_get_cr8(vcpu);
7454
7455 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7456}
7457
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007458static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007459{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007460 int r;
7461
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007462 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007463
Liran Alon1a680e32018-03-23 03:01:33 +03007464 if (vcpu->arch.exception.injected)
7465 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007466 /*
Liran Alona042c262018-03-23 03:01:32 +03007467 * Do not inject an NMI or interrupt if there is a pending
7468 * exception. Exceptions and interrupts are recognized at
7469 * instruction boundaries, i.e. the start of an instruction.
7470 * Trap-like exceptions, e.g. #DB, have higher priority than
7471 * NMIs and interrupts, i.e. traps are recognized before an
7472 * NMI/interrupt that's pending on the same instruction.
7473 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7474 * priority, but are only generated (pended) during instruction
7475 * execution, i.e. a pending fault-like exception means the
7476 * fault occurred on the *previous* instruction and must be
7477 * serviced prior to recognizing any new events in order to
7478 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007479 */
Liran Alon1a680e32018-03-23 03:01:33 +03007480 else if (!vcpu->arch.exception.pending) {
7481 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007482 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007483 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007484 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007485 }
7486
Liran Alon1a680e32018-03-23 03:01:33 +03007487 /*
7488 * Call check_nested_events() even if we reinjected a previous event
7489 * in order for caller to determine if it should require immediate-exit
7490 * from L2 to L1 due to pending L1 events which require exit
7491 * from L2 to L1.
7492 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007493 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7494 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7495 if (r != 0)
7496 return r;
7497 }
7498
7499 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007500 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007501 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7502 vcpu->arch.exception.has_error_code,
7503 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007504
Liran Alon1a680e32018-03-23 03:01:33 +03007505 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007506 vcpu->arch.exception.pending = false;
7507 vcpu->arch.exception.injected = true;
7508
Nadav Amitd6e8c852014-07-24 14:51:24 +03007509 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7510 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7511 X86_EFLAGS_RF);
7512
Jim Mattsonf10c7292018-10-16 14:29:23 -07007513 if (vcpu->arch.exception.nr == DB_VECTOR) {
7514 /*
7515 * This code assumes that nSVM doesn't use
7516 * check_nested_events(). If it does, the
7517 * DR6/DR7 changes should happen before L1
7518 * gets a #VMEXIT for an intercepted #DB in
7519 * L2. (Under VMX, on the other hand, the
7520 * DR6/DR7 changes should not happen in the
7521 * event of a VM-exit to L1 for an intercepted
7522 * #DB in L2.)
7523 */
7524 kvm_deliver_exception_payload(vcpu);
7525 if (vcpu->arch.dr7 & DR7_GD) {
7526 vcpu->arch.dr7 &= ~DR7_GD;
7527 kvm_update_dr7(vcpu);
7528 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007529 }
7530
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007531 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007532 }
7533
7534 /* Don't consider new event if we re-injected an event */
7535 if (kvm_event_needs_reinjection(vcpu))
7536 return 0;
7537
7538 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7539 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007540 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007541 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007542 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007543 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007544 --vcpu->arch.nmi_pending;
7545 vcpu->arch.nmi_injected = true;
7546 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007547 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007548 /*
7549 * Because interrupts can be injected asynchronously, we are
7550 * calling check_nested_events again here to avoid a race condition.
7551 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7552 * proposal and current concerns. Perhaps we should be setting
7553 * KVM_REQ_EVENT only on certain events and not unconditionally?
7554 */
7555 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7556 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7557 if (r != 0)
7558 return r;
7559 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007560 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007561 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7562 false);
7563 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007564 }
7565 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007566
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007567 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007568}
7569
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007570static void process_nmi(struct kvm_vcpu *vcpu)
7571{
7572 unsigned limit = 2;
7573
7574 /*
7575 * x86 is limited to one NMI running, and one NMI pending after it.
7576 * If an NMI is already in progress, limit further NMIs to just one.
7577 * Otherwise, allow two (and we'll inject the first one immediately).
7578 */
7579 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7580 limit = 1;
7581
7582 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7583 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7584 kvm_make_request(KVM_REQ_EVENT, vcpu);
7585}
7586
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007587static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007588{
7589 u32 flags = 0;
7590 flags |= seg->g << 23;
7591 flags |= seg->db << 22;
7592 flags |= seg->l << 21;
7593 flags |= seg->avl << 20;
7594 flags |= seg->present << 15;
7595 flags |= seg->dpl << 13;
7596 flags |= seg->s << 12;
7597 flags |= seg->type << 8;
7598 return flags;
7599}
7600
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007601static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007602{
7603 struct kvm_segment seg;
7604 int offset;
7605
7606 kvm_get_segment(vcpu, &seg, n);
7607 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7608
7609 if (n < 3)
7610 offset = 0x7f84 + n * 12;
7611 else
7612 offset = 0x7f2c + (n - 3) * 12;
7613
7614 put_smstate(u32, buf, offset + 8, seg.base);
7615 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007616 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007617}
7618
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007619#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007620static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007621{
7622 struct kvm_segment seg;
7623 int offset;
7624 u16 flags;
7625
7626 kvm_get_segment(vcpu, &seg, n);
7627 offset = 0x7e00 + n * 16;
7628
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007629 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007630 put_smstate(u16, buf, offset, seg.selector);
7631 put_smstate(u16, buf, offset + 2, flags);
7632 put_smstate(u32, buf, offset + 4, seg.limit);
7633 put_smstate(u64, buf, offset + 8, seg.base);
7634}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007635#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007636
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007637static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007638{
7639 struct desc_ptr dt;
7640 struct kvm_segment seg;
7641 unsigned long val;
7642 int i;
7643
7644 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7645 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7646 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7647 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7648
7649 for (i = 0; i < 8; i++)
7650 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7651
7652 kvm_get_dr(vcpu, 6, &val);
7653 put_smstate(u32, buf, 0x7fcc, (u32)val);
7654 kvm_get_dr(vcpu, 7, &val);
7655 put_smstate(u32, buf, 0x7fc8, (u32)val);
7656
7657 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7658 put_smstate(u32, buf, 0x7fc4, seg.selector);
7659 put_smstate(u32, buf, 0x7f64, seg.base);
7660 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007661 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007662
7663 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7664 put_smstate(u32, buf, 0x7fc0, seg.selector);
7665 put_smstate(u32, buf, 0x7f80, seg.base);
7666 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007667 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007668
7669 kvm_x86_ops->get_gdt(vcpu, &dt);
7670 put_smstate(u32, buf, 0x7f74, dt.address);
7671 put_smstate(u32, buf, 0x7f70, dt.size);
7672
7673 kvm_x86_ops->get_idt(vcpu, &dt);
7674 put_smstate(u32, buf, 0x7f58, dt.address);
7675 put_smstate(u32, buf, 0x7f54, dt.size);
7676
7677 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007678 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007679
7680 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7681
7682 /* revision id */
7683 put_smstate(u32, buf, 0x7efc, 0x00020000);
7684 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7685}
7686
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007687#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007688static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007689{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007690 struct desc_ptr dt;
7691 struct kvm_segment seg;
7692 unsigned long val;
7693 int i;
7694
7695 for (i = 0; i < 16; i++)
7696 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7697
7698 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7699 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7700
7701 kvm_get_dr(vcpu, 6, &val);
7702 put_smstate(u64, buf, 0x7f68, val);
7703 kvm_get_dr(vcpu, 7, &val);
7704 put_smstate(u64, buf, 0x7f60, val);
7705
7706 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7707 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7708 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7709
7710 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7711
7712 /* revision id */
7713 put_smstate(u32, buf, 0x7efc, 0x00020064);
7714
7715 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7716
7717 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7718 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007719 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007720 put_smstate(u32, buf, 0x7e94, seg.limit);
7721 put_smstate(u64, buf, 0x7e98, seg.base);
7722
7723 kvm_x86_ops->get_idt(vcpu, &dt);
7724 put_smstate(u32, buf, 0x7e84, dt.size);
7725 put_smstate(u64, buf, 0x7e88, dt.address);
7726
7727 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7728 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007729 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007730 put_smstate(u32, buf, 0x7e74, seg.limit);
7731 put_smstate(u64, buf, 0x7e78, seg.base);
7732
7733 kvm_x86_ops->get_gdt(vcpu, &dt);
7734 put_smstate(u32, buf, 0x7e64, dt.size);
7735 put_smstate(u64, buf, 0x7e68, dt.address);
7736
7737 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007738 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007739}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007740#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007741
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007742static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007743{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007744 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007745 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007746 char buf[512];
7747 u32 cr0;
7748
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007749 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007750 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007751#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007752 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007753 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007754 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007755#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007756 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007757
Ladi Prosek0234bf82017-10-11 16:54:40 +02007758 /*
7759 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7760 * vCPU state (e.g. leave guest mode) after we've saved the state into
7761 * the SMM state-save area.
7762 */
7763 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7764
7765 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007766 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007767
7768 if (kvm_x86_ops->get_nmi_mask(vcpu))
7769 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7770 else
7771 kvm_x86_ops->set_nmi_mask(vcpu, true);
7772
7773 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7774 kvm_rip_write(vcpu, 0x8000);
7775
7776 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7777 kvm_x86_ops->set_cr0(vcpu, cr0);
7778 vcpu->arch.cr0 = cr0;
7779
7780 kvm_x86_ops->set_cr4(vcpu, 0);
7781
Paolo Bonzini18c36262015-08-07 12:27:54 +02007782 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7783 dt.address = dt.size = 0;
7784 kvm_x86_ops->set_idt(vcpu, &dt);
7785
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007786 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7787
7788 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7789 cs.base = vcpu->arch.smbase;
7790
7791 ds.selector = 0;
7792 ds.base = 0;
7793
7794 cs.limit = ds.limit = 0xffffffff;
7795 cs.type = ds.type = 0x3;
7796 cs.dpl = ds.dpl = 0;
7797 cs.db = ds.db = 0;
7798 cs.s = ds.s = 1;
7799 cs.l = ds.l = 0;
7800 cs.g = ds.g = 1;
7801 cs.avl = ds.avl = 0;
7802 cs.present = ds.present = 1;
7803 cs.unusable = ds.unusable = 0;
7804 cs.padding = ds.padding = 0;
7805
7806 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7807 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7808 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7809 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7810 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7811 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7812
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007813#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007814 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007815 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007816#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007817
7818 kvm_update_cpuid(vcpu);
7819 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007820}
7821
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007822static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007823{
7824 vcpu->arch.smi_pending = true;
7825 kvm_make_request(KVM_REQ_EVENT, vcpu);
7826}
7827
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01007828void kvm_make_scan_ioapic_request(struct kvm *kvm)
7829{
7830 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
7831}
7832
Yang Zhang3d81bc72013-04-11 19:25:13 +08007833static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08007834{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08007835 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08007836 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08007837
Andrey Smetanin63086302015-11-10 15:36:32 +03007838 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007839
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07007840 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03007841 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007842 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02007843 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03007844 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08007845 if (ioapic_in_kernel(vcpu->kvm))
7846 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02007847 }
Liran Alone40ff1d2018-03-21 02:50:31 +02007848
7849 if (is_guest_mode(vcpu))
7850 vcpu->arch.load_eoi_exitmap_pending = true;
7851 else
7852 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
7853}
7854
7855static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
7856{
7857 u64 eoi_exit_bitmap[4];
7858
7859 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
7860 return;
7861
Andrey Smetanin5c9194122015-11-10 15:36:34 +03007862 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
7863 vcpu_to_synic(vcpu)->vec_bitmap, 256);
7864 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007865}
7866
Michal Hocko93065ac2018-08-21 21:52:33 -07007867int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
7868 unsigned long start, unsigned long end,
7869 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01007870{
7871 unsigned long apic_address;
7872
7873 /*
7874 * The physical address of apic access page is stored in the VMCS.
7875 * Update it when it becomes invalid.
7876 */
7877 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
7878 if (start <= apic_address && apic_address < end)
7879 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07007880
7881 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01007882}
7883
Tang Chen4256f432014-09-24 15:57:54 +08007884void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
7885{
Tang Chenc24ae0d2014-09-24 15:57:58 +08007886 struct page *page = NULL;
7887
Paolo Bonzini35754c92015-07-29 12:05:37 +02007888 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02007889 return;
7890
Tang Chen4256f432014-09-24 15:57:54 +08007891 if (!kvm_x86_ops->set_apic_access_page_addr)
7892 return;
7893
Tang Chenc24ae0d2014-09-24 15:57:58 +08007894 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02007895 if (is_error_page(page))
7896 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08007897 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
7898
7899 /*
7900 * Do not pin apic access page in memory, the MMU notifier
7901 * will call us again if it is migrated or swapped out.
7902 */
7903 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08007904}
7905EXPORT_SYMBOL_GPL(kvm_vcpu_reload_apic_access_page);
7906
Sean Christophersond264ee02018-08-27 15:21:12 -07007907void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
7908{
7909 smp_send_reschedule(vcpu->cpu);
7910}
7911EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
7912
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007913/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01007914 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09007915 * exiting to the userspace. Otherwise, the value will be returned to the
7916 * userspace.
7917 */
Avi Kivity851ba692009-08-24 11:10:17 +03007918static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007919{
7920 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08007921 bool req_int_win =
7922 dm_request_for_irq_injection(vcpu) &&
7923 kvm_cpu_accept_dm_intr(vcpu);
7924
Jan Kiszka730dca42013-04-28 10:50:52 +02007925 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007926
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02007927 if (kvm_request_pending(vcpu)) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +02007928 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu))
7929 kvm_x86_ops->get_vmcs12_pages(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007930 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05007931 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007932 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03007933 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007934 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
7935 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03007936 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
7937 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10007938 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
7939 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007940 if (unlikely(r))
7941 goto out;
7942 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03007943 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03007944 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07007945 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
7946 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007947 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08007948 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03007949 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007950 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02007951 r = 0;
7952 goto out;
7953 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03007954 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007955 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007956 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01007957 r = 0;
7958 goto out;
7959 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02007960 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
7961 /* Page is swapped out. Do synthetic halt */
7962 vcpu->arch.apf.halted = true;
7963 r = 1;
7964 goto out;
7965 }
Glauber Costac9aaa892011-07-11 15:28:14 -04007966 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
7967 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02007968 if (kvm_check_request(KVM_REQ_SMI, vcpu))
7969 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007970 if (kvm_check_request(KVM_REQ_NMI, vcpu))
7971 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007972 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02007973 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02007974 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02007975 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07007976 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
7977 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
7978 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03007979 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07007980 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
7981 vcpu->run->eoi.vector =
7982 vcpu->arch.pending_ioapic_eoi;
7983 r = 0;
7984 goto out;
7985 }
7986 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08007987 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
7988 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02007989 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
7990 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08007991 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
7992 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03007993 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
7994 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7995 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
7996 r = 0;
7997 goto out;
7998 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03007999 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8000 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8001 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8002 r = 0;
8003 goto out;
8004 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008005 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8006 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8007 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8008 r = 0;
8009 goto out;
8010 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008011
8012 /*
8013 * KVM_REQ_HV_STIMER has to be processed after
8014 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8015 * depend on the guest clock being up-to-date
8016 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008017 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8018 kvm_hv_process_stimers(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008019 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008020
Avi Kivityb463a6f2010-07-20 15:06:17 +03008021 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008022 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008023 kvm_apic_accept_events(vcpu);
8024 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8025 r = 1;
8026 goto out;
8027 }
8028
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008029 if (inject_pending_event(vcpu, req_int_win) != 0)
8030 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008031 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008032 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008033 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008034 * SMIs have three cases:
8035 * 1) They can be nested, and then there is nothing to
8036 * do here because RSM will cause a vmexit anyway.
8037 * 2) There is an ISA-specific reason why SMI cannot be
8038 * injected, and the moment when this changes can be
8039 * intercepted.
8040 * 3) Or the SMI can be pending because
8041 * inject_pending_event has completed the injection
8042 * of an IRQ or NMI from the previous vmexit, and
8043 * then we request an immediate exit to inject the
8044 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008045 */
8046 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008047 if (!kvm_x86_ops->enable_smi_window(vcpu))
8048 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008049 if (vcpu->arch.nmi_pending)
8050 kvm_x86_ops->enable_nmi_window(vcpu);
8051 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8052 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008053 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008054 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008055
Avi Kivity3842d132010-07-27 12:30:24 +03008056 if (kvm_lapic_enabled(vcpu)) {
8057 update_cr8_intercept(vcpu);
8058 kvm_lapic_sync_to_vapic(vcpu);
8059 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008060 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008061
Avi Kivityd8368af2012-05-14 18:07:56 +03008062 r = kvm_mmu_reload(vcpu);
8063 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008064 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008065 }
8066
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008067 preempt_disable();
8068
8069 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008070
8071 /*
8072 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8073 * IPI are then delayed after guest entry, which ensures that they
8074 * result in virtual interrupt delivery.
8075 */
8076 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008077 vcpu->mode = IN_GUEST_MODE;
8078
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008079 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8080
Lan Tianyu0f127d12016-03-13 11:10:29 +08008081 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008082 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008083 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008084 *
Luwei Kang81b01662019-01-31 16:52:02 +08008085 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008086 * pairs with the memory barrier implicit in pi_test_and_set_on
8087 * (see vmx_deliver_posted_interrupt).
8088 *
8089 * 3) This also orders the write to mode from any reads to the page
8090 * tables done while the VCPU is running. Please see the comment
8091 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008092 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008093 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008094
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008095 /*
8096 * This handles the case where a posted interrupt was
8097 * notified with kvm_vcpu_kick.
8098 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008099 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8100 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008101
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008102 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008103 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008104 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008105 smp_wmb();
8106 local_irq_enable();
8107 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008108 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008109 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008110 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008111 }
8112
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008113 if (req_immediate_exit) {
8114 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008115 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008116 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008117
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008118 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008119 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008120
Wanpeng Lie7517322019-07-22 12:26:20 +08008121 /* The preempt notifier should have taken care of the FPU already. */
8122 WARN_ON_ONCE(test_thread_flag(TIF_NEED_FPU_LOAD));
Rik van Riel5f409e22019-04-03 18:41:52 +02008123
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008124 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008125 set_debugreg(0, 7);
8126 set_debugreg(vcpu->arch.eff_db[0], 0);
8127 set_debugreg(vcpu->arch.eff_db[1], 1);
8128 set_debugreg(vcpu->arch.eff_db[2], 2);
8129 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008130 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008131 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008132 }
8133
Avi Kivity851ba692009-08-24 11:10:17 +03008134 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008135
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008136 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008137 * Do this here before restoring debug registers on the host. And
8138 * since we do this before handling the vmexit, a DR access vmexit
8139 * can (a) read the correct value of the debug registers, (b) set
8140 * KVM_DEBUGREG_WONT_EXIT again.
8141 */
8142 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008143 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8144 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008145 kvm_update_dr0123(vcpu);
8146 kvm_update_dr6(vcpu);
8147 kvm_update_dr7(vcpu);
8148 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008149 }
8150
8151 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008152 * If the guest has used debug registers, at least dr7
8153 * will be disabled while returning to the host.
8154 * If we don't have active breakpoints in the host, we don't
8155 * care about the messed up debug address registers. But if
8156 * we have some of them active, restore the old state.
8157 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008158 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008159 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008160
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008161 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008162
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008163 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008164 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008165
Sean Christopherson95b5a482019-04-19 22:50:59 -07008166 kvm_x86_ops->handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008167
Sean Christophersond7a08882019-07-10 09:07:34 -07008168 /*
8169 * Consume any pending interrupts, including the possible source of
8170 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8171 * An instruction is required after local_irq_enable() to fully unblock
8172 * interrupts on processors that implement an interrupt shadow, the
8173 * stat.exits increment will do nicely.
8174 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008175 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008176 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008177 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008178 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008179 kvm_after_interrupt(vcpu);
8180
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008181 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008182 if (lapic_in_kernel(vcpu)) {
8183 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8184 if (delta != S64_MIN) {
8185 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8186 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8187 }
8188 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008189
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008190 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008191 preempt_enable();
8192
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008193 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008194
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008195 /*
8196 * Profile KVM exit RIPs:
8197 */
8198 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008199 unsigned long rip = kvm_rip_read(vcpu);
8200 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008201 }
8202
Zachary Amsdencc578282012-02-03 15:43:50 -02008203 if (unlikely(vcpu->arch.tsc_always_catchup))
8204 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008205
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008206 if (vcpu->arch.apic_attention)
8207 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008208
Brijesh Singh618232e2017-08-17 18:36:57 +02008209 vcpu->arch.gpa_available = false;
Avi Kivity851ba692009-08-24 11:10:17 +03008210 r = kvm_x86_ops->handle_exit(vcpu);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008211 return r;
8212
8213cancel_injection:
8214 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008215 if (unlikely(vcpu->arch.apic_attention))
8216 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008217out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008218 return r;
8219}
8220
Paolo Bonzini362c6982015-02-06 12:48:04 +01008221static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8222{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008223 if (!kvm_arch_vcpu_runnable(vcpu) &&
8224 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008225 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8226 kvm_vcpu_block(vcpu);
8227 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008228
8229 if (kvm_x86_ops->post_block)
8230 kvm_x86_ops->post_block(vcpu);
8231
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008232 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8233 return 1;
8234 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008235
Paolo Bonzini362c6982015-02-06 12:48:04 +01008236 kvm_apic_accept_events(vcpu);
8237 switch(vcpu->arch.mp_state) {
8238 case KVM_MP_STATE_HALTED:
8239 vcpu->arch.pv.pv_unhalted = false;
8240 vcpu->arch.mp_state =
8241 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008242 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008243 case KVM_MP_STATE_RUNNABLE:
8244 vcpu->arch.apf.halted = false;
8245 break;
8246 case KVM_MP_STATE_INIT_RECEIVED:
8247 break;
8248 default:
8249 return -EINTR;
8250 break;
8251 }
8252 return 1;
8253}
8254
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008255static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8256{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008257 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
8258 kvm_x86_ops->check_nested_events(vcpu, false);
8259
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008260 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8261 !vcpu->arch.apf.halted);
8262}
8263
Paolo Bonzini362c6982015-02-06 12:48:04 +01008264static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008265{
8266 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008267 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008268
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008269 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008270 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008271
Paolo Bonzini362c6982015-02-06 12:48:04 +01008272 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008273 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008274 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008275 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008276 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008277 }
8278
Gleb Natapov09cec752009-03-23 15:11:44 +02008279 if (r <= 0)
8280 break;
8281
Radim Krčmář72875d82017-04-26 22:32:19 +02008282 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008283 if (kvm_cpu_has_pending_timer(vcpu))
8284 kvm_inject_pending_timer_irqs(vcpu);
8285
Matt Gingell782d4222015-11-16 15:26:00 -08008286 if (dm_request_for_irq_injection(vcpu) &&
8287 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008288 r = 0;
8289 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008290 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008291 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008292 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008293
8294 kvm_check_async_pf_completion(vcpu);
8295
Gleb Natapov09cec752009-03-23 15:11:44 +02008296 if (signal_pending(current)) {
8297 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008298 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008299 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008300 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008301 }
8302 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008303 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008304 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008305 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008306 }
8307 }
8308
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008309 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008310
8311 return r;
8312}
8313
Gleb Natapov716d51a2012-09-03 15:24:26 +03008314static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8315{
8316 int r;
8317 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008318 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008319 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8320 if (r != EMULATE_DONE)
8321 return 0;
8322 return 1;
8323}
8324
8325static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8326{
8327 BUG_ON(!vcpu->arch.pio.count);
8328
8329 return complete_emulated_io(vcpu);
8330}
8331
Avi Kivityf78146b2012-04-18 19:22:47 +03008332/*
8333 * Implements the following, as a state machine:
8334 *
8335 * read:
8336 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008337 * for each mmio piece in the fragment
8338 * write gpa, len
8339 * exit
8340 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008341 * execute insn
8342 *
8343 * write:
8344 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008345 * for each mmio piece in the fragment
8346 * write gpa, len
8347 * copy data
8348 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008349 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008350static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008351{
8352 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008353 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008354 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008355
Gleb Natapov716d51a2012-09-03 15:24:26 +03008356 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008357
Gleb Natapov716d51a2012-09-03 15:24:26 +03008358 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008359 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8360 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008361 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008362 memcpy(frag->data, run->mmio.data, len);
8363
8364 if (frag->len <= 8) {
8365 /* Switch to the next fragment. */
8366 frag++;
8367 vcpu->mmio_cur_fragment++;
8368 } else {
8369 /* Go forward to the next mmio piece. */
8370 frag->data += len;
8371 frag->gpa += len;
8372 frag->len -= len;
8373 }
8374
Andrew Honiga08d3b32014-02-27 19:35:14 +01008375 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008376 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008377
8378 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008379 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008380 return 1;
8381 vcpu->mmio_read_completed = 1;
8382 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008383 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008384
Gleb Natapov716d51a2012-09-03 15:24:26 +03008385 run->exit_reason = KVM_EXIT_MMIO;
8386 run->mmio.phys_addr = frag->gpa;
8387 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008388 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8389 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008390 run->mmio.is_write = vcpu->mmio_is_write;
8391 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8392 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008393}
8394
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008395/* Swap (qemu) user FPU context for the guest FPU context. */
8396static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8397{
Rik van Riel5f409e22019-04-03 18:41:52 +02008398 fpregs_lock();
8399
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008400 copy_fpregs_to_fpstate(vcpu->arch.user_fpu);
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008401 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008402 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008403 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008404
8405 fpregs_mark_activate();
8406 fpregs_unlock();
8407
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008408 trace_kvm_fpu(1);
8409}
8410
8411/* When vcpu_run ends, restore user space FPU context. */
8412static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8413{
Rik van Riel5f409e22019-04-03 18:41:52 +02008414 fpregs_lock();
8415
Marc Orrb666a4b2018-11-06 14:53:56 -08008416 copy_fpregs_to_fpstate(vcpu->arch.guest_fpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008417 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008418
8419 fpregs_mark_activate();
8420 fpregs_unlock();
8421
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008422 ++vcpu->stat.fpu_reload;
8423 trace_kvm_fpu(0);
8424}
8425
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008426int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8427{
8428 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008429
Christoffer Dallaccb7572017-12-04 21:35:25 +01008430 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008431 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008432 kvm_load_guest_fpu(vcpu);
8433
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008434 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008435 if (kvm_run->immediate_exit) {
8436 r = -EINTR;
8437 goto out;
8438 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008439 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008440 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008441 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008442 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008443 if (signal_pending(current)) {
8444 r = -EINTR;
8445 vcpu->run->exit_reason = KVM_EXIT_INTR;
8446 ++vcpu->stat.signal_exits;
8447 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008448 goto out;
8449 }
8450
Ken Hofsass01643c52018-01-31 16:03:36 -08008451 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8452 r = -EINVAL;
8453 goto out;
8454 }
8455
8456 if (vcpu->run->kvm_dirty_regs) {
8457 r = sync_regs(vcpu);
8458 if (r != 0)
8459 goto out;
8460 }
8461
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008462 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008463 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008464 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8465 r = -EINVAL;
8466 goto out;
8467 }
8468 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008469
Gleb Natapov716d51a2012-09-03 15:24:26 +03008470 if (unlikely(vcpu->arch.complete_userspace_io)) {
8471 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8472 vcpu->arch.complete_userspace_io = NULL;
8473 r = cui(vcpu);
8474 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008475 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008476 } else
8477 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008478
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008479 if (kvm_run->immediate_exit)
8480 r = -EINTR;
8481 else
8482 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008483
8484out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008485 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008486 if (vcpu->run->kvm_valid_regs)
8487 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008488 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008489 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008490
Christoffer Dallaccb7572017-12-04 21:35:25 +01008491 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008492 return r;
8493}
8494
Ken Hofsass01643c52018-01-31 16:03:36 -08008495static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008496{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008497 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8498 /*
8499 * We are here if userspace calls get_regs() in the middle of
8500 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008501 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008502 * that usually, but some bad designed PV devices (vmware
8503 * backdoor interface) need this to work
8504 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03008505 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008506 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8507 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008508 regs->rax = kvm_rax_read(vcpu);
8509 regs->rbx = kvm_rbx_read(vcpu);
8510 regs->rcx = kvm_rcx_read(vcpu);
8511 regs->rdx = kvm_rdx_read(vcpu);
8512 regs->rsi = kvm_rsi_read(vcpu);
8513 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008514 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008515 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008516#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008517 regs->r8 = kvm_r8_read(vcpu);
8518 regs->r9 = kvm_r9_read(vcpu);
8519 regs->r10 = kvm_r10_read(vcpu);
8520 regs->r11 = kvm_r11_read(vcpu);
8521 regs->r12 = kvm_r12_read(vcpu);
8522 regs->r13 = kvm_r13_read(vcpu);
8523 regs->r14 = kvm_r14_read(vcpu);
8524 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008525#endif
8526
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008527 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008528 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008529}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008530
Ken Hofsass01643c52018-01-31 16:03:36 -08008531int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8532{
8533 vcpu_load(vcpu);
8534 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008535 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008536 return 0;
8537}
8538
Ken Hofsass01643c52018-01-31 16:03:36 -08008539static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008540{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008541 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8542 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8543
Sean Christophersonde3cd112019-04-30 10:36:17 -07008544 kvm_rax_write(vcpu, regs->rax);
8545 kvm_rbx_write(vcpu, regs->rbx);
8546 kvm_rcx_write(vcpu, regs->rcx);
8547 kvm_rdx_write(vcpu, regs->rdx);
8548 kvm_rsi_write(vcpu, regs->rsi);
8549 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008550 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008551 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008552#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008553 kvm_r8_write(vcpu, regs->r8);
8554 kvm_r9_write(vcpu, regs->r9);
8555 kvm_r10_write(vcpu, regs->r10);
8556 kvm_r11_write(vcpu, regs->r11);
8557 kvm_r12_write(vcpu, regs->r12);
8558 kvm_r13_write(vcpu, regs->r13);
8559 kvm_r14_write(vcpu, regs->r14);
8560 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008561#endif
8562
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008563 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008564 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008565
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008566 vcpu->arch.exception.pending = false;
8567
Avi Kivity3842d132010-07-27 12:30:24 +03008568 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008569}
Avi Kivity3842d132010-07-27 12:30:24 +03008570
Ken Hofsass01643c52018-01-31 16:03:36 -08008571int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8572{
8573 vcpu_load(vcpu);
8574 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008575 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008576 return 0;
8577}
8578
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008579void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8580{
8581 struct kvm_segment cs;
8582
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008583 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008584 *db = cs.db;
8585 *l = cs.l;
8586}
8587EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8588
Ken Hofsass01643c52018-01-31 16:03:36 -08008589static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008590{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008591 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008592
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008593 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8594 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8595 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8596 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8597 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8598 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008599
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008600 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8601 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008602
8603 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008604 sregs->idt.limit = dt.size;
8605 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008606 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008607 sregs->gdt.limit = dt.size;
8608 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008609
Avi Kivity4d4ec082009-12-29 18:07:30 +02008610 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008611 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008612 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008613 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008614 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008615 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008616 sregs->apic_base = kvm_get_apic_base(vcpu);
8617
Jordan Borgner0e96f312018-10-28 12:58:28 +00008618 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008619
Liran Alon04140b42018-03-23 03:01:31 +03008620 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008621 set_bit(vcpu->arch.interrupt.nr,
8622 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008623}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008624
Ken Hofsass01643c52018-01-31 16:03:36 -08008625int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8626 struct kvm_sregs *sregs)
8627{
8628 vcpu_load(vcpu);
8629 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008630 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008631 return 0;
8632}
8633
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008634int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8635 struct kvm_mp_state *mp_state)
8636{
Christoffer Dallfd232562017-12-04 21:35:30 +01008637 vcpu_load(vcpu);
8638
Jan Kiszka66450a22013-03-13 12:42:34 +01008639 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308640 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8641 vcpu->arch.pv.pv_unhalted)
8642 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8643 else
8644 mp_state->mp_state = vcpu->arch.mp_state;
8645
Christoffer Dallfd232562017-12-04 21:35:30 +01008646 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008647 return 0;
8648}
8649
8650int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8651 struct kvm_mp_state *mp_state)
8652{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008653 int ret = -EINVAL;
8654
8655 vcpu_load(vcpu);
8656
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008657 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008658 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008659 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008660
David Hildenbrand28bf2882017-03-23 11:46:03 +01008661 /* INITs are latched while in SMM */
8662 if ((is_smm(vcpu) || vcpu->arch.smi_pending) &&
8663 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8664 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008665 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008666
Jan Kiszka66450a22013-03-13 12:42:34 +01008667 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8668 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8669 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8670 } else
8671 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008672 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008673
8674 ret = 0;
8675out:
8676 vcpu_put(vcpu);
8677 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008678}
8679
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008680int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8681 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008682{
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008683 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008684 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008685
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008686 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008687
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008688 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008689 has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02008690
Gleb Natapovc6975182010-02-18 12:15:01 +02008691 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03008692 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02008693
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008694 kvm_rip_write(vcpu, ctxt->eip);
8695 kvm_set_rflags(vcpu, ctxt->eflags);
Avi Kivity3842d132010-07-27 12:30:24 +03008696 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov19d04432010-04-15 12:29:50 +03008697 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02008698}
8699EXPORT_SYMBOL_GPL(kvm_task_switch);
8700
Peng Hao3140c152018-04-02 09:15:32 +08008701static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008702{
Tianyu Lan74fec5b2018-07-23 12:31:21 +00008703 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
8704 (sregs->cr4 & X86_CR4_OSXSAVE))
8705 return -EINVAL;
8706
Tianyu Lan37b95952018-01-16 17:34:07 +08008707 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008708 /*
8709 * When EFER.LME and CR0.PG are set, the processor is in
8710 * 64-bit mode (though maybe in a 32-bit code segment).
8711 * CR4.PAE and EFER.LMA must be set.
8712 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008713 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008714 || !(sregs->efer & EFER_LMA))
8715 return -EINVAL;
8716 } else {
8717 /*
8718 * Not in 64-bit mode: EFER.LMA is clear and the code
8719 * segment cannot be 64-bit.
8720 */
8721 if (sregs->efer & EFER_LMA || sregs->cs.l)
8722 return -EINVAL;
8723 }
8724
8725 return 0;
8726}
8727
Ken Hofsass01643c52018-01-31 16:03:36 -08008728static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008729{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008730 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008731 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008732 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008733 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008734 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008735 int ret = -EINVAL;
8736
Lan Tianyuf2981032017-12-14 03:01:52 -05008737 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08008738 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05008739
Jim Mattsond3802282017-08-10 10:14:13 -07008740 apic_base_msr.data = sregs->apic_base;
8741 apic_base_msr.host_initiated = true;
8742 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008743 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01008744
Gleb Natapov89a27f42010-02-16 10:51:48 +02008745 dt.size = sregs->idt.limit;
8746 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008747 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008748 dt.size = sregs->gdt.limit;
8749 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008750 kvm_x86_ops->set_gdt(vcpu, &dt);
8751
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008752 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008753 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02008754 vcpu->arch.cr3 = sregs->cr3;
Avi Kivityaff48ba2010-12-05 18:56:11 +02008755 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008756
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008757 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008758
Avi Kivityf6801df2010-01-21 15:31:50 +02008759 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008760 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008761
Avi Kivity4d4ec082009-12-29 18:07:30 +02008762 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008763 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00008764 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008765
Avi Kivityfc78f512009-12-07 12:16:48 +02008766 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05008767 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
8768 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008769 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05008770 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02008771 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008772
8773 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02008774 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02008775 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02008776 mmu_reset_needed = 1;
8777 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008778 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008779
8780 if (mmu_reset_needed)
8781 kvm_mmu_reset_context(vcpu);
8782
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03008783 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03008784 pending_vec = find_first_bit(
8785 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
8786 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008787 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03008788 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008789 }
8790
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008791 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8792 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8793 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8794 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8795 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8796 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008797
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008798 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8799 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008800
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03008801 update_cr8_intercept(vcpu);
8802
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008803 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03008804 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008805 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02008806 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03008807 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
8808
Avi Kivity3842d132010-07-27 12:30:24 +03008809 kvm_make_request(KVM_REQ_EVENT, vcpu);
8810
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008811 ret = 0;
8812out:
Ken Hofsass01643c52018-01-31 16:03:36 -08008813 return ret;
8814}
8815
8816int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
8817 struct kvm_sregs *sregs)
8818{
8819 int ret;
8820
8821 vcpu_load(vcpu);
8822 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008823 vcpu_put(vcpu);
8824 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008825}
8826
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008827int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
8828 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008829{
Jan Kiszka355be0b2009-10-03 00:31:21 +02008830 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008831 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008832
Christoffer Dall66b56562017-12-04 21:35:33 +01008833 vcpu_load(vcpu);
8834
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008835 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
8836 r = -EBUSY;
8837 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03008838 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008839 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
8840 kvm_queue_exception(vcpu, DB_VECTOR);
8841 else
8842 kvm_queue_exception(vcpu, BP_VECTOR);
8843 }
8844
Jan Kiszka91586a32009-10-05 13:07:21 +02008845 /*
8846 * Read rflags as long as potentially injected trace flags are still
8847 * filtered out.
8848 */
8849 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008850
8851 vcpu->guest_debug = dbg->control;
8852 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
8853 vcpu->guest_debug = 0;
8854
8855 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008856 for (i = 0; i < KVM_NR_DB_REGS; ++i)
8857 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02008858 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008859 } else {
8860 for (i = 0; i < KVM_NR_DB_REGS; i++)
8861 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008862 }
Jan Kiszkac8639012012-09-21 05:42:55 +02008863 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01008864
Jan Kiszkaf92653e2010-02-23 17:47:55 +01008865 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8866 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
8867 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008868
Jan Kiszka91586a32009-10-05 13:07:21 +02008869 /*
8870 * Trigger an rflags update that will inject or remove the trace
8871 * flags.
8872 */
8873 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01008874
Paolo Bonzinia96036b2015-11-10 11:55:36 +01008875 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02008876
Jan Kiszka4f926bf22009-10-30 12:46:59 +01008877 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008878
Avi Kivity2122ff52010-05-13 11:25:04 +03008879out:
Christoffer Dall66b56562017-12-04 21:35:33 +01008880 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008881 return r;
8882}
8883
8884/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08008885 * Translate a guest virtual address to a guest physical address.
8886 */
8887int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
8888 struct kvm_translation *tr)
8889{
8890 unsigned long vaddr = tr->linear_address;
8891 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008892 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008893
Christoffer Dall1da5b612017-12-04 21:35:32 +01008894 vcpu_load(vcpu);
8895
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008896 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02008897 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008898 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008899 tr->physical_address = gpa;
8900 tr->valid = gpa != UNMAPPED_GVA;
8901 tr->writeable = 1;
8902 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08008903
Christoffer Dall1da5b612017-12-04 21:35:32 +01008904 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08008905 return 0;
8906}
8907
Hollis Blanchardd0752062007-10-31 17:24:25 -05008908int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8909{
Christoffer Dall13931232017-12-04 21:35:34 +01008910 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008911
Christoffer Dall13931232017-12-04 21:35:34 +01008912 vcpu_load(vcpu);
8913
Marc Orrb666a4b2018-11-06 14:53:56 -08008914 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008915 memcpy(fpu->fpr, fxsave->st_space, 128);
8916 fpu->fcw = fxsave->cwd;
8917 fpu->fsw = fxsave->swd;
8918 fpu->ftwx = fxsave->twd;
8919 fpu->last_opcode = fxsave->fop;
8920 fpu->last_ip = fxsave->rip;
8921 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00008922 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05008923
Christoffer Dall13931232017-12-04 21:35:34 +01008924 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008925 return 0;
8926}
8927
8928int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8929{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01008930 struct fxregs_state *fxsave;
8931
8932 vcpu_load(vcpu);
8933
Marc Orrb666a4b2018-11-06 14:53:56 -08008934 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008935
Hollis Blanchardd0752062007-10-31 17:24:25 -05008936 memcpy(fxsave->st_space, fpu->fpr, 128);
8937 fxsave->cwd = fpu->fcw;
8938 fxsave->swd = fpu->fsw;
8939 fxsave->twd = fpu->ftwx;
8940 fxsave->fop = fpu->last_opcode;
8941 fxsave->rip = fpu->last_ip;
8942 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00008943 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05008944
Christoffer Dall6a96bc72017-12-04 21:35:35 +01008945 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05008946 return 0;
8947}
8948
Ken Hofsass01643c52018-01-31 16:03:36 -08008949static void store_regs(struct kvm_vcpu *vcpu)
8950{
8951 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
8952
8953 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
8954 __get_regs(vcpu, &vcpu->run->s.regs.regs);
8955
8956 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
8957 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
8958
8959 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
8960 kvm_vcpu_ioctl_x86_get_vcpu_events(
8961 vcpu, &vcpu->run->s.regs.events);
8962}
8963
8964static int sync_regs(struct kvm_vcpu *vcpu)
8965{
8966 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
8967 return -EINVAL;
8968
8969 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
8970 __set_regs(vcpu, &vcpu->run->s.regs.regs);
8971 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
8972 }
8973 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
8974 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
8975 return -EINVAL;
8976 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
8977 }
8978 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
8979 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
8980 vcpu, &vcpu->run->s.regs.events))
8981 return -EINVAL;
8982 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
8983 }
8984
8985 return 0;
8986}
8987
Ingo Molnar0ee6a512015-04-27 06:58:22 +02008988static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05008989{
Marc Orrb666a4b2018-11-06 14:53:56 -08008990 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02008991 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08008992 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01008993 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05008994
Dexuan Cui2acf9232010-06-10 11:27:12 +08008995 /*
8996 * Ensure guest xcr0 is valid for loading
8997 */
Dave Hansend91cab72015-09-02 16:31:26 -07008998 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08008999
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009000 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009001}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009002
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009003void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
9004{
Ido Yarivbd768e12016-10-21 12:39:57 -04009005 void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask;
9006
Glauber Costa12f9a482011-02-01 14:16:40 -05009007 kvmclock_reset(vcpu);
Joerg Roedel7f1ea202009-02-25 16:08:31 +01009008
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009009 kvm_x86_ops->vcpu_free(vcpu);
Ido Yarivbd768e12016-10-21 12:39:57 -04009010 free_cpumask_var(wbinvd_dirty_mask);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009011}
9012
9013struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
9014 unsigned int id)
9015{
Liang Lic447e762015-05-21 04:41:25 +08009016 struct kvm_vcpu *vcpu;
9017
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009018 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Zachary Amsden6755bae2010-08-19 22:07:22 -10009019 printk_once(KERN_WARNING
9020 "kvm: SMP vm created on host with unstable TSC; "
9021 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009022
9023 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
9024
Liang Lic447e762015-05-21 04:41:25 +08009025 return vcpu;
Avi Kivity26e52152007-11-20 15:30:24 +02009026}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009027
Avi Kivity26e52152007-11-20 15:30:24 +02009028int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
9029{
Sean Christopherson0cf91352019-03-07 15:43:02 -08009030 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009031 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009032 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009033 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009034 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009035 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009036 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009037 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009038}
9039
Dominik Dingel31928aa2014-12-04 15:47:07 +01009040void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009041{
Will Auld8fe8ab42012-11-29 12:42:12 -08009042 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009043 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009044
Roman Kagand3457c82017-07-14 17:13:20 +03009045 kvm_hv_vcpu_postcreate(vcpu);
9046
Christoffer Dallec7660c2017-12-04 21:35:23 +01009047 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009048 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009049 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009050 msr.data = 0x0;
9051 msr.index = MSR_IA32_TSC;
9052 msr.host_initiated = true;
9053 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009054 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009055
9056 /* poll control enabled by default */
9057 vcpu->arch.msr_kvm_poll_control = 1;
9058
Christoffer Dallec7660c2017-12-04 21:35:23 +01009059 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009060
Marcelo Tosatti630994b2015-05-12 22:42:04 -03009061 if (!kvmclock_periodic_sync)
9062 return;
9063
Andrew Jones332967a2014-02-28 12:52:55 +01009064 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9065 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009066}
9067
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009068void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009069{
Gleb Natapov344d9582010-10-14 11:22:50 +02009070 vcpu->arch.apf.msr_val = 0;
9071
Christoffer Dallec7660c2017-12-04 21:35:23 +01009072 vcpu_load(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009073 kvm_mmu_unload(vcpu);
9074 vcpu_put(vcpu);
9075
9076 kvm_x86_ops->vcpu_free(vcpu);
9077}
9078
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009079void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009080{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009081 kvm_lapic_reset(vcpu, init_event);
9082
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009083 vcpu->arch.hflags = 0;
9084
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009085 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009086 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009087 atomic_set(&vcpu->arch.nmi_queued, 0);
9088 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009089 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009090 kvm_clear_interrupt_queue(vcpu);
9091 kvm_clear_exception_queue(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07009092 vcpu->arch.exception.pending = false;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009093
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009094 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009095 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009096 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009097 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009098 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009099 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009100
Nadav Amit11190222015-04-02 03:10:38 +03009101 vcpu->arch.cr2 = 0;
9102
Avi Kivity3842d132010-07-27 12:30:24 +03009103 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009104 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009105 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009106
Glauber Costa12f9a482011-02-01 14:16:40 -05009107 kvmclock_reset(vcpu);
9108
Gleb Natapovaf585b92010-10-14 11:22:46 +02009109 kvm_clear_async_pf_completion_queue(vcpu);
9110 kvm_async_pf_hash_reset(vcpu);
9111 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009112
Wanpeng Lia554d202017-10-11 05:10:19 -07009113 if (kvm_mpx_supported()) {
9114 void *mpx_state_buffer;
9115
9116 /*
9117 * To avoid have the INIT path from kvm_apic_has_events() that be
9118 * called with loaded FPU and does not let userspace fix the state.
9119 */
Rik van Rielf775b132017-11-14 16:54:23 -05009120 if (init_event)
9121 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009122 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009123 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009124 if (mpx_state_buffer)
9125 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009126 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009127 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009128 if (mpx_state_buffer)
9129 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009130 if (init_event)
9131 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009132 }
9133
Paolo Bonzini64d60672015-05-07 11:36:11 +02009134 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009135 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009136 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009137
Kyle Hueydb2336a2017-03-20 01:16:28 -07009138 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009139
9140 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009141 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009142
Julian Stecklina66f7b722012-12-05 15:26:19 +01009143 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9144 vcpu->arch.regs_avail = ~0;
9145 vcpu->arch.regs_dirty = ~0;
9146
Wanpeng Lia554d202017-10-11 05:10:19 -07009147 vcpu->arch.ia32_xss = 0;
9148
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009149 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009150}
9151
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009152void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009153{
9154 struct kvm_segment cs;
9155
9156 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9157 cs.selector = vector << 8;
9158 cs.base = vector << 12;
9159 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9160 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009161}
9162
Radim Krčmář13a34e02014-08-28 15:13:03 +02009163int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009164{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009165 struct kvm *kvm;
9166 struct kvm_vcpu *vcpu;
9167 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009168 int ret;
9169 u64 local_tsc;
9170 u64 max_tsc = 0;
9171 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009172
9173 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009174 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009175 if (ret != 0)
9176 return ret;
9177
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009178 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009179 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009180 list_for_each_entry(kvm, &vm_list, vm_list) {
9181 kvm_for_each_vcpu(i, vcpu, kvm) {
9182 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009183 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009184 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9185 backwards_tsc = true;
9186 if (vcpu->arch.last_host_tsc > max_tsc)
9187 max_tsc = vcpu->arch.last_host_tsc;
9188 }
9189 }
9190 }
9191
9192 /*
9193 * Sometimes, even reliable TSCs go backwards. This happens on
9194 * platforms that reset TSC during suspend or hibernate actions, but
9195 * maintain synchronization. We must compensate. Fortunately, we can
9196 * detect that condition here, which happens early in CPU bringup,
9197 * before any KVM threads can be running. Unfortunately, we can't
9198 * bring the TSCs fully up to date with real time, as we aren't yet far
9199 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009200 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009201 * variables that haven't been updated yet.
9202 *
9203 * So we simply find the maximum observed TSC above, then record the
9204 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9205 * the adjustment will be applied. Note that we accumulate
9206 * adjustments, in case multiple suspend cycles happen before some VCPU
9207 * gets a chance to run again. In the event that no KVM threads get a
9208 * chance to run, we will miss the entire elapsed period, as we'll have
9209 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9210 * loose cycle time. This isn't too big a deal, since the loss will be
9211 * uniform across all VCPUs (not to mention the scenario is extremely
9212 * unlikely). It is possible that a second hibernate recovery happens
9213 * much faster than a first, causing the observed TSC here to be
9214 * smaller; this would require additional padding adjustment, which is
9215 * why we set last_host_tsc to the local tsc observed here.
9216 *
9217 * N.B. - this code below runs only on platforms with reliable TSC,
9218 * as that is the only way backwards_tsc is set above. Also note
9219 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9220 * have the same delta_cyc adjustment applied if backwards_tsc
9221 * is detected. Note further, this adjustment is only done once,
9222 * as we reset last_host_tsc on all VCPUs to stop this from being
9223 * called multiple times (one for each physical CPU bringup).
9224 *
Guo Chao4a969982012-06-28 15:17:27 +08009225 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009226 * will be compensated by the logic in vcpu_load, which sets the TSC to
9227 * catchup mode. This will catchup all VCPUs to real time, but cannot
9228 * guarantee that they stay in perfect synchronization.
9229 */
9230 if (backwards_tsc) {
9231 u64 delta_cyc = max_tsc - local_tsc;
9232 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009233 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009234 kvm_for_each_vcpu(i, vcpu, kvm) {
9235 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9236 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009237 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009238 }
9239
9240 /*
9241 * We have to disable TSC offset matching.. if you were
9242 * booting a VM while issuing an S4 host suspend....
9243 * you may have some problem. Solving this issue is
9244 * left as an exercise to the reader.
9245 */
9246 kvm->arch.last_tsc_nsec = 0;
9247 kvm->arch.last_tsc_write = 0;
9248 }
9249
9250 }
9251 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009252}
9253
Radim Krčmář13a34e02014-08-28 15:13:03 +02009254void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009255{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009256 kvm_x86_ops->hardware_disable();
9257 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009258}
9259
9260int kvm_arch_hardware_setup(void)
9261{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009262 int r;
9263
9264 r = kvm_x86_ops->hardware_setup();
9265 if (r != 0)
9266 return r;
9267
Haozhong Zhang35181e82015-10-20 15:39:03 +08009268 if (kvm_has_tsc_control) {
9269 /*
9270 * Make sure the user can only configure tsc_khz values that
9271 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009272 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009273 * be 1 on all machines.
9274 */
9275 u64 max = min(0x7fffffffULL,
9276 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9277 kvm_max_guest_tsc_khz = max;
9278
Haozhong Zhangad7218832015-10-20 15:39:02 +08009279 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009280 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009281
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009282 kvm_init_msr_list();
9283 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009284}
9285
9286void kvm_arch_hardware_unsetup(void)
9287{
9288 kvm_x86_ops->hardware_unsetup();
9289}
9290
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009291int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009292{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009293 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009294}
9295
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009296bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9297{
9298 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9299}
9300EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9301
9302bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9303{
9304 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9305}
9306
Gleb Natapov54e98182012-08-05 15:58:32 +03009307struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009308EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009309
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009310int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
9311{
9312 struct page *page;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009313 int r;
9314
Avi Kivity9aabc88f2010-07-29 15:11:50 +03009315 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
David Hildenbrand26de7982017-08-24 20:51:25 +02009316 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03009317 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009318 else
Avi Kivitya4535292008-04-13 17:54:35 +03009319 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009320
9321 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9322 if (!page) {
9323 r = -ENOMEM;
9324 goto fail;
9325 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009326 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009327
Zachary Amsdencc578282012-02-03 15:43:50 -02009328 kvm_set_tsc_khz(vcpu, max_tsc_khz);
Zachary Amsdenc2855452010-09-18 14:38:15 -10009329
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009330 r = kvm_mmu_create(vcpu);
9331 if (r < 0)
9332 goto fail_free_pio_data;
9333
David Hildenbrand26de7982017-08-24 20:51:25 +02009334 if (irqchip_in_kernel(vcpu->kvm)) {
Paolo Bonzinif7589cc2019-01-30 17:18:59 +01009335 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu);
Sean Christopherson39497d72019-04-17 10:15:32 -07009336 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009337 if (r < 0)
9338 goto fail_mmu_destroy;
Gleb Natapov54e98182012-08-05 15:58:32 +03009339 } else
9340 static_key_slow_inc(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009341
Huang Ying890ca9a2009-05-11 16:48:15 +08009342 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
Ben Gardon254272c2019-02-11 11:02:50 -08009343 GFP_KERNEL_ACCOUNT);
Huang Ying890ca9a2009-05-11 16:48:15 +08009344 if (!vcpu->arch.mce_banks) {
9345 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08009346 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08009347 }
9348 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9349
Ben Gardon254272c2019-02-11 11:02:50 -08009350 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9351 GFP_KERNEL_ACCOUNT)) {
Wei Yongjunf1797352013-04-18 07:41:00 +08009352 r = -ENOMEM;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009353 goto fail_free_mce_banks;
Wei Yongjunf1797352013-04-18 07:41:00 +08009354 }
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009355
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009356 fx_init(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01009357
Paolo Bonzini4344ee92013-10-02 16:06:16 +02009358 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
Paolo Bonzinid7876f12013-10-02 16:06:15 +02009359
Eugene Korenevsky5a4f55c2015-03-29 23:56:12 +03009360 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9361
Radim Krčmář74545702015-04-27 15:11:25 +02009362 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9363
Gleb Natapovaf585b92010-10-14 11:22:46 +02009364 kvm_async_pf_hash_reset(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009365 kvm_pmu_init(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009366
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02009367 vcpu->arch.pending_external_vector = -1;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009368 vcpu->arch.preempted_in_kernel = false;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02009369
Andrey Smetanin5c9194122015-11-10 15:36:34 +03009370 kvm_hv_vcpu_init(vcpu);
9371
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009372 return 0;
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009373
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009374fail_free_mce_banks:
9375 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08009376fail_free_lapic:
9377 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009378fail_mmu_destroy:
9379 kvm_mmu_destroy(vcpu);
9380fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009381 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009382fail:
9383 return r;
9384}
9385
9386void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
9387{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009388 int idx;
9389
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009390 kvm_hv_vcpu_uninit(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02009391 kvm_pmu_destroy(vcpu);
Wei Yongjun36cb93f2010-01-22 14:18:47 +08009392 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009393 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009394 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009395 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009396 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009397 free_page((unsigned long)vcpu->arch.pio_data);
Paolo Bonzini35754c92015-07-29 12:05:37 +02009398 if (!lapic_in_kernel(vcpu))
Gleb Natapov54e98182012-08-05 15:58:32 +03009399 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009400}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009401
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009402void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9403{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009404 vcpu->arch.l1tf_flush_l1d = true;
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009405 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009406}
9407
Carsten Ottee08b9632012-01-04 10:25:20 +01009408int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009409{
Carsten Ottee08b9632012-01-04 10:25:20 +01009410 if (type)
9411 return -EINVAL;
9412
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009413 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009414 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009415 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009416 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009417
Sheng Yang5550af42008-10-15 20:15:06 +08009418 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9419 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009420 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9421 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9422 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009423
Jan Kiszka038f8c12011-02-04 10:49:11 +01009424 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009425 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009426 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9427
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009428 kvm->arch.kvmclock_offset = -ktime_get_boottime_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009429 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009430
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009431 kvm->arch.guest_can_read_msr_platform_info = true;
9432
Andrew Jones7e44e442014-02-28 12:52:54 +01009433 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009434 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009435
Roman Kagancbc02362018-02-01 16:48:31 +03009436 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009437 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009438 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009439
Sean Christopherson92735b12019-08-02 15:06:17 -07009440 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009441}
9442
9443static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9444{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009445 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009446 kvm_mmu_unload(vcpu);
9447 vcpu_put(vcpu);
9448}
9449
9450static void kvm_free_vcpus(struct kvm *kvm)
9451{
9452 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009453 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009454
9455 /*
9456 * Unpin any mmu pages first.
9457 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009458 kvm_for_each_vcpu(i, vcpu, kvm) {
9459 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009460 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009461 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009462 kvm_for_each_vcpu(i, vcpu, kvm)
9463 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009464
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009465 mutex_lock(&kvm->lock);
9466 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9467 kvm->vcpus[i] = NULL;
9468
9469 atomic_set(&kvm->online_vcpus, 0);
9470 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009471}
9472
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009473void kvm_arch_sync_events(struct kvm *kvm)
9474{
Andrew Jones332967a2014-02-28 12:52:55 +01009475 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009476 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009477 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009478}
9479
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009480int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009481{
9482 int i, r;
Paolo Bonzini25188b92015-10-14 15:51:08 +02009483 unsigned long hva;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009484 struct kvm_memslots *slots = kvm_memslots(kvm);
9485 struct kvm_memory_slot *slot, old;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009486
9487 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009488 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9489 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009490
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009491 slot = id_to_memslot(slots, id);
9492 if (size) {
Paolo Bonzinib21629d2016-06-01 14:09:18 +02009493 if (slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009494 return -EEXIST;
9495
9496 /*
9497 * MAP_SHARED to prevent internal slot pages from being moved
9498 * by fork()/COW.
9499 */
9500 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9501 MAP_SHARED | MAP_ANONYMOUS, 0);
9502 if (IS_ERR((void *)hva))
9503 return PTR_ERR((void *)hva);
9504 } else {
9505 if (!slot->npages)
9506 return 0;
9507
9508 hva = 0;
9509 }
9510
9511 old = *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009512 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009513 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009514
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009515 m.slot = id | (i << 16);
9516 m.flags = 0;
9517 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009518 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009519 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009520 r = __kvm_set_memory_region(kvm, &m);
9521 if (r < 0)
9522 return r;
9523 }
9524
Eric Biggers103c7632018-01-31 17:30:21 -08009525 if (!size)
9526 vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009527
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009528 return 0;
9529}
9530EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9531
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009532int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009533{
9534 int r;
9535
9536 mutex_lock(&kvm->slots_lock);
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009537 r = __x86_set_memory_region(kvm, id, gpa, size);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009538 mutex_unlock(&kvm->slots_lock);
9539
9540 return r;
9541}
9542EXPORT_SYMBOL_GPL(x86_set_memory_region);
9543
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009544void kvm_arch_destroy_vm(struct kvm *kvm)
9545{
Andrew Honig27469d22013-04-18 09:38:14 -07009546 if (current->mm == kvm->mm) {
9547 /*
9548 * Free memory regions allocated on behalf of userspace,
9549 * unless the the memory map has changed due to process exit
9550 * or fd copying.
9551 */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009552 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
9553 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
9554 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
Andrew Honig27469d22013-04-18 09:38:14 -07009555 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009556 if (kvm_x86_ops->vm_destroy)
9557 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009558 kvm_pic_destroy(kvm);
9559 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009560 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009561 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009562 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009563 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009564 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009565 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009566}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009567
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309568void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009569 struct kvm_memory_slot *dont)
9570{
9571 int i;
9572
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009573 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
9574 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009575 kvfree(free->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009576 free->arch.rmap[i] = NULL;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009577 }
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009578 if (i == 0)
9579 continue;
9580
9581 if (!dont || free->arch.lpage_info[i - 1] !=
9582 dont->arch.lpage_info[i - 1]) {
Thomas Huth548ef282015-02-24 21:29:25 +01009583 kvfree(free->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009584 free->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009585 }
9586 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009587
9588 kvm_page_track_free_memslot(free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009589}
9590
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05309591int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
9592 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009593{
9594 int i;
9595
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009596 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009597 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009598 unsigned long ugfn;
9599 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009600 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009601
9602 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9603 slot->base_gfn, level) + 1;
9604
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009605 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009606 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009607 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009608 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009609 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009610 if (i == 0)
9611 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009612
Ben Gardon254272c2019-02-11 11:02:50 -08009613 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009614 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009615 goto out_free;
9616
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009617 slot->arch.lpage_info[i - 1] = linfo;
9618
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009619 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009620 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009621 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009622 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009623 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9624 /*
9625 * If the gfn and userspace address are not aligned wrt each
9626 * other, or if explicitly asked to, disable large page
9627 * support for this slot
9628 */
9629 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
9630 !kvm_largepages_enabled()) {
9631 unsigned long j;
9632
9633 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009634 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009635 }
9636 }
9637
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009638 if (kvm_page_track_create_memslot(slot, npages))
9639 goto out_free;
9640
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009641 return 0;
9642
9643out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009644 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009645 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009646 slot->arch.rmap[i] = NULL;
9647 if (i == 0)
9648 continue;
9649
Thomas Huth548ef282015-02-24 21:29:25 +01009650 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009651 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009652 }
9653 return -ENOMEM;
9654}
9655
Sean Christopherson15248252019-02-05 12:54:17 -08009656void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009657{
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009658 /*
9659 * memslots->generation has been incremented.
9660 * mmio generation may have reached its maximum value.
9661 */
Sean Christopherson15248252019-02-05 12:54:17 -08009662 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009663}
9664
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009665int kvm_arch_prepare_memory_region(struct kvm *kvm,
9666 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009667 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009668 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009669{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009670 return 0;
9671}
9672
Kai Huang88178fd2015-01-28 10:54:27 +08009673static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9674 struct kvm_memory_slot *new)
9675{
9676 /* Still write protect RO slot */
9677 if (new->flags & KVM_MEM_READONLY) {
9678 kvm_mmu_slot_remove_write_access(kvm, new);
9679 return;
9680 }
9681
9682 /*
9683 * Call kvm_x86_ops dirty logging hooks when they are valid.
9684 *
9685 * kvm_x86_ops->slot_disable_log_dirty is called when:
9686 *
9687 * - KVM_MR_CREATE with dirty logging is disabled
9688 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9689 *
9690 * The reason is, in case of PML, we need to set D-bit for any slots
9691 * with dirty logging disabled in order to eliminate unnecessary GPA
9692 * logging in PML buffer (and potential PML buffer full VMEXT). This
9693 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +08009694 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +08009695 * logging disabled for memory slots.
9696 *
9697 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
9698 * to dirty logging mode.
9699 *
9700 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
9701 *
9702 * In case of write protect:
9703 *
9704 * Write protect all pages for dirty logging.
9705 *
9706 * All the sptes including the large sptes which point to this
9707 * slot are set to readonly. We can not create any new large
9708 * spte on this slot until the end of the logging.
9709 *
9710 * See the comments in fast_page_fault().
9711 */
9712 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
9713 if (kvm_x86_ops->slot_enable_log_dirty)
9714 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
9715 else
9716 kvm_mmu_slot_remove_write_access(kvm, new);
9717 } else {
9718 if (kvm_x86_ops->slot_disable_log_dirty)
9719 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
9720 }
9721}
9722
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009723void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009724 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009725 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009726 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09009727 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009728{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +08009729 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +08009730 kvm_mmu_change_mmu_pages(kvm,
9731 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +08009732
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009733 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08009734 * Dirty logging tracks sptes in 4k granularity, meaning that large
9735 * sptes have to be split. If live migration is successful, the guest
9736 * in the source machine will be destroyed and large sptes will be
9737 * created in the destination. However, if the guest continues to run
9738 * in the source machine (for example if live migration fails), small
9739 * sptes will remain around and cause bad performance.
9740 *
9741 * Scan sptes if dirty logging has been stopped, dropping those
9742 * which can be collapsed into a single large-page spte. Later
9743 * page faults will create the large-page sptes.
9744 */
9745 if ((change != KVM_MR_DELETE) &&
9746 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
9747 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
9748 kvm_mmu_zap_collapsible_sptes(kvm, new);
9749
9750 /*
Kai Huang88178fd2015-01-28 10:54:27 +08009751 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +08009752 *
Kai Huang88178fd2015-01-28 10:54:27 +08009753 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
9754 * been zapped so no dirty logging staff is needed for old slot. For
9755 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
9756 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009757 *
9758 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +09009759 */
Kai Huang88178fd2015-01-28 10:54:27 +08009760 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02009761 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Zhang Xiantao0de10342007-11-20 16:25:04 +08009762}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009763
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009764void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009765{
Sean Christopherson7390de12019-02-05 13:01:31 -08009766 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03009767}
9768
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009769void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
9770 struct kvm_memory_slot *slot)
9771{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +08009772 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03009773}
9774
Liran Alone6c67d82018-09-04 10:56:52 +03009775static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
9776{
9777 return (is_guest_mode(vcpu) &&
9778 kvm_x86_ops->guest_apic_has_interrupt &&
9779 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
9780}
9781
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009782static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
9783{
9784 if (!list_empty_careful(&vcpu->async_pf.done))
9785 return true;
9786
9787 if (kvm_apic_has_events(vcpu))
9788 return true;
9789
9790 if (vcpu->arch.pv.pv_unhalted)
9791 return true;
9792
Wanpeng Lia5f01f82017-09-13 04:04:01 -07009793 if (vcpu->arch.exception.pending)
9794 return true;
9795
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009796 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9797 (vcpu->arch.nmi_pending &&
9798 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009799 return true;
9800
ZhuangYanying47a66ee2017-05-26 13:16:48 +08009801 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
9802 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +02009803 return true;
9804
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009805 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +03009806 (kvm_cpu_has_interrupt(vcpu) ||
9807 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009808 return true;
9809
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03009810 if (kvm_hv_has_stimer_pending(vcpu))
9811 return true;
9812
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009813 return false;
9814}
9815
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009816int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
9817{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02009818 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +08009819}
Zhang Xiantao57361992007-12-17 14:21:40 +08009820
Wanpeng Li17e433b2019-08-05 10:03:19 +08009821bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
9822{
9823 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
9824 return true;
9825
9826 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9827 kvm_test_request(KVM_REQ_SMI, vcpu) ||
9828 kvm_test_request(KVM_REQ_EVENT, vcpu))
9829 return true;
9830
9831 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
9832 return true;
9833
9834 return false;
9835}
9836
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009837bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
9838{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08009839 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +08009840}
9841
Christoffer Dallb6d33832012-03-08 16:44:24 -05009842int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +08009843{
Christoffer Dallb6d33832012-03-08 16:44:24 -05009844 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +08009845}
Gleb Natapov78646122009-03-23 12:12:11 +02009846
9847int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
9848{
9849 return kvm_x86_ops->interrupt_allowed(vcpu);
9850}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009851
Nadav Amit82b32772014-11-02 11:54:45 +02009852unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
9853{
9854 if (is_64_bit_mode(vcpu))
9855 return kvm_rip_read(vcpu);
9856 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
9857 kvm_rip_read(vcpu));
9858}
9859EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
9860
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009861bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
9862{
Nadav Amit82b32772014-11-02 11:54:45 +02009863 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009864}
9865EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
9866
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009867unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
9868{
9869 unsigned long rflags;
9870
9871 rflags = kvm_x86_ops->get_rflags(vcpu);
9872 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01009873 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009874 return rflags;
9875}
9876EXPORT_SYMBOL_GPL(kvm_get_rflags);
9877
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009878static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009879{
9880 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009881 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01009882 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009883 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01009884}
9885
9886void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
9887{
9888 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +03009889 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +02009890}
9891EXPORT_SYMBOL_GPL(kvm_set_rflags);
9892
Gleb Natapov56028d02010-10-17 18:13:42 +02009893void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
9894{
9895 int r;
9896
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009897 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +08009898 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +02009899 return;
9900
9901 r = kvm_mmu_reload(vcpu);
9902 if (unlikely(r))
9903 return;
9904
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009905 if (!vcpu->arch.mmu->direct_map &&
9906 work->arch.cr3 != vcpu->arch.mmu->get_cr3(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +08009907 return;
9908
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009909 vcpu->arch.mmu->page_fault(vcpu, work->gva, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +02009910}
9911
Gleb Natapovaf585b92010-10-14 11:22:46 +02009912static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
9913{
9914 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
9915}
9916
9917static inline u32 kvm_async_pf_next_probe(u32 key)
9918{
9919 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
9920}
9921
9922static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9923{
9924 u32 key = kvm_async_pf_hash_fn(gfn);
9925
9926 while (vcpu->arch.apf.gfns[key] != ~0)
9927 key = kvm_async_pf_next_probe(key);
9928
9929 vcpu->arch.apf.gfns[key] = gfn;
9930}
9931
9932static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
9933{
9934 int i;
9935 u32 key = kvm_async_pf_hash_fn(gfn);
9936
9937 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +08009938 (vcpu->arch.apf.gfns[key] != gfn &&
9939 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +02009940 key = kvm_async_pf_next_probe(key);
9941
9942 return key;
9943}
9944
9945bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9946{
9947 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
9948}
9949
9950static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
9951{
9952 u32 i, j, k;
9953
9954 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
9955 while (true) {
9956 vcpu->arch.apf.gfns[i] = ~0;
9957 do {
9958 j = kvm_async_pf_next_probe(j);
9959 if (vcpu->arch.apf.gfns[j] == ~0)
9960 return;
9961 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
9962 /*
9963 * k lies cyclically in ]i,j]
9964 * | i.k.j |
9965 * |....j i.k.| or |.k..j i...|
9966 */
9967 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
9968 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
9969 i = j;
9970 }
9971}
9972
Gleb Natapov7c907052010-10-14 11:22:53 +02009973static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
9974{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02009975
9976 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
9977 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +02009978}
9979
Wanpeng Li9a6e7c32017-09-14 03:54:16 -07009980static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
9981{
9982
9983 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
9984 sizeof(u32));
9985}
9986
Paolo Bonzini1dfdb452019-06-05 16:46:44 +02009987static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
9988{
9989 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
9990 return false;
9991
9992 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
9993 (vcpu->arch.apf.send_user_only &&
9994 kvm_x86_ops->get_cpl(vcpu) == 0))
9995 return false;
9996
9997 return true;
9998}
9999
10000bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10001{
10002 if (unlikely(!lapic_in_kernel(vcpu) ||
10003 kvm_event_needs_reinjection(vcpu) ||
10004 vcpu->arch.exception.pending))
10005 return false;
10006
10007 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10008 return false;
10009
10010 /*
10011 * If interrupts are off we cannot even use an artificial
10012 * halt state.
10013 */
10014 return kvm_x86_ops->interrupt_allowed(vcpu);
10015}
10016
Gleb Natapovaf585b92010-10-14 11:22:46 +020010017void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10018 struct kvm_async_pf *work)
10019{
Avi Kivity6389ee92010-11-29 16:12:30 +020010020 struct x86_exception fault;
10021
Gleb Natapov7c907052010-10-14 11:22:53 +020010022 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010023 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010024
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010025 if (kvm_can_deliver_async_pf(vcpu) &&
10026 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010027 fault.vector = PF_VECTOR;
10028 fault.error_code_valid = true;
10029 fault.error_code = 0;
10030 fault.nested_page_fault = false;
10031 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010032 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010033 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010034 } else {
10035 /*
10036 * It is not possible to deliver a paravirtualized asynchronous
10037 * page fault, but putting the guest in an artificial halt state
10038 * can be beneficial nevertheless: if an interrupt arrives, we
10039 * can deliver it timely and perhaps the guest will schedule
10040 * another process. When the instruction that triggered a page
10041 * fault is retried, hopefully the page will be ready in the host.
10042 */
10043 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010044 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010045}
10046
10047void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10048 struct kvm_async_pf *work)
10049{
Avi Kivity6389ee92010-11-29 16:12:30 +020010050 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010051 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010052
chai wenf2e10662013-10-14 22:22:33 +080010053 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010054 work->arch.token = ~0; /* broadcast wakeup */
10055 else
10056 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Wanpeng Li24dccf82017-03-20 21:18:55 -070010057 trace_kvm_async_pf_ready(work->arch.token, work->gva);
Gleb Natapov7c907052010-10-14 11:22:53 +020010058
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010059 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10060 !apf_get_user(vcpu, &val)) {
10061 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10062 vcpu->arch.exception.pending &&
10063 vcpu->arch.exception.nr == PF_VECTOR &&
10064 !apf_put_user(vcpu, 0)) {
10065 vcpu->arch.exception.injected = false;
10066 vcpu->arch.exception.pending = false;
10067 vcpu->arch.exception.nr = 0;
10068 vcpu->arch.exception.has_error_code = false;
10069 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010070 vcpu->arch.exception.has_payload = false;
10071 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010072 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10073 fault.vector = PF_VECTOR;
10074 fault.error_code_valid = true;
10075 fault.error_code = 0;
10076 fault.nested_page_fault = false;
10077 fault.address = work->arch.token;
10078 fault.async_page_fault = true;
10079 kvm_inject_page_fault(vcpu, &fault);
10080 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010081 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010082 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010083 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010084}
10085
10086bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10087{
10088 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10089 return true;
10090 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010091 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010092}
10093
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010094void kvm_arch_start_assignment(struct kvm *kvm)
10095{
10096 atomic_inc(&kvm->arch.assigned_device_count);
10097}
10098EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10099
10100void kvm_arch_end_assignment(struct kvm *kvm)
10101{
10102 atomic_dec(&kvm->arch.assigned_device_count);
10103}
10104EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10105
10106bool kvm_arch_has_assigned_device(struct kvm *kvm)
10107{
10108 return atomic_read(&kvm->arch.assigned_device_count);
10109}
10110EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10111
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010112void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10113{
10114 atomic_inc(&kvm->arch.noncoherent_dma_count);
10115}
10116EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10117
10118void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10119{
10120 atomic_dec(&kvm->arch.noncoherent_dma_count);
10121}
10122EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10123
10124bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10125{
10126 return atomic_read(&kvm->arch.noncoherent_dma_count);
10127}
10128EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10129
Alex Williamson14717e22016-05-05 11:58:35 -060010130bool kvm_arch_has_irq_bypass(void)
10131{
Sean Christopherson92735b12019-08-02 15:06:17 -070010132 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010133}
10134
Feng Wu87276882015-09-18 22:29:40 +080010135int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10136 struct irq_bypass_producer *prod)
10137{
10138 struct kvm_kernel_irqfd *irqfd =
10139 container_of(cons, struct kvm_kernel_irqfd, consumer);
10140
Alex Williamson14717e22016-05-05 11:58:35 -060010141 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010142
Alex Williamson14717e22016-05-05 11:58:35 -060010143 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10144 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010145}
10146
10147void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10148 struct irq_bypass_producer *prod)
10149{
10150 int ret;
10151 struct kvm_kernel_irqfd *irqfd =
10152 container_of(cons, struct kvm_kernel_irqfd, consumer);
10153
Feng Wu87276882015-09-18 22:29:40 +080010154 WARN_ON(irqfd->producer != prod);
10155 irqfd->producer = NULL;
10156
10157 /*
10158 * When producer of consumer is unregistered, we change back to
10159 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010160 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010161 * int this case doesn't want to receive the interrupts.
10162 */
10163 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10164 if (ret)
10165 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10166 " fails: %d\n", irqfd->consumer.token, ret);
10167}
10168
10169int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10170 uint32_t guest_irq, bool set)
10171{
Feng Wu87276882015-09-18 22:29:40 +080010172 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10173}
10174
Feng Wu520040142016-01-25 16:53:33 +080010175bool kvm_vector_hashing_enabled(void)
10176{
10177 return vector_hashing;
10178}
10179EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
10180
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010181bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10182{
10183 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10184}
10185EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10186
10187
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010188EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010189EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010190EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10191EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10192EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10193EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010194EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010195EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010196EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010197EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010198EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010199EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010200EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010201EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010202EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010203EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010204EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010205EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010206EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10207EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);