blob: 59958ce2b6816de16e9d13fa0e2241de509d6fcd [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"
Sean Christopherson2f728d62020-02-18 15:29:49 -080025#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030026#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020027#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020028#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030029#include "hyperv.h"
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -060030#include "lapic.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020031
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020032#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030033#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020034#include <linux/kvm.h>
35#include <linux/fs.h>
36#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040037#include <linux/export.h>
38#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080039#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050040#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010041#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030042#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010043#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030044#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020045#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080047#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080048#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020049#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020050#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020051#include <linux/timekeeper_internal.h>
52#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080053#include <linux/kvm_irqfd.h>
54#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010055#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080056#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050057#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010058
Avi Kivityaec51dc2009-07-01 16:01:02 +030059#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080060
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020061#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080062#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020063#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080064#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020065#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020066#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100067#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030068#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080069#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010070#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010071#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080072#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090073#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000074#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020075
Dave Hansend1898b72016-06-01 10:42:20 -070076#define CREATE_TRACE_POINTS
77#include "trace.h"
78
Carsten Otte313a3dc2007-10-11 19:16:52 +020079#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080080#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080081u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
82EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080083
Avi Kivity0f65dd72011-04-20 13:37:53 +030084#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080085 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030086
Joerg Roedel50a37eb2008-01-31 14:57:38 +010087/* EFER defaults:
88 * - enable syscall per default because its emulated by KVM
89 * - enable LME and LMA per default on 64 bit KVM
90 */
91#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080092static
93u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010094#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080095static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010096#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020097
Sean Christophersonb11306b2019-12-10 14:44:13 -080098static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
99
Radim Krčmářc5192652016-07-12 22:09:28 +0200100#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
101 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200102
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300103static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300104static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200105static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100106static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800107static void store_regs(struct kvm_vcpu *vcpu);
108static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200109
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700110struct kvm_x86_ops kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300111EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800112
Paolo Bonzini893590c2015-11-06 11:46:24 +0100113static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030114module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200115
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200116static bool __read_mostly report_ignored_msrs = true;
117module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
118
Wanpeng Li4c276252018-05-05 04:02:32 -0700119unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200120module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
121
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300122static bool __read_mostly kvmclock_periodic_sync = true;
123module_param(kvmclock_periodic_sync, bool, S_IRUGO);
124
Paolo Bonzini893590c2015-11-06 11:46:24 +0100125bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100126EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100127u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100128EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800129u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
130EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
131u64 __read_mostly kvm_max_tsc_scaling_ratio;
132EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700133u64 __read_mostly kvm_default_tsc_scaling_ratio;
134EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100135
Zachary Amsdencc578282012-02-03 15:43:50 -0200136/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100137static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200138module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
139
Sean Christophersonc3941d92019-04-17 10:15:33 -0700140/*
141 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
142 * adaptive tuning starting from default advancment of 1000ns. '0' disables
143 * advancement entirely. Any other value is used as-is and disables adaptive
144 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
145 */
146static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800147module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500148
Feng Wu520040142016-01-25 16:53:33 +0800149static bool __read_mostly vector_hashing = true;
150module_param(vector_hashing, bool, S_IRUGO);
151
Liran Alonc4ae60e2018-03-12 13:12:47 +0200152bool __read_mostly enable_vmware_backdoor = false;
153module_param(enable_vmware_backdoor, bool, S_IRUGO);
154EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
155
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700156static bool __read_mostly force_emulation_prefix = false;
157module_param(force_emulation_prefix, bool, S_IRUGO);
158
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800159int __read_mostly pi_inject_timer = -1;
160module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
161
Avi Kivity18863bd2009-09-07 11:12:18 +0300162#define KVM_NR_SHARED_MSRS 16
163
164struct kvm_shared_msrs_global {
165 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800166 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300167};
168
169struct kvm_shared_msrs {
170 struct user_return_notifier urn;
171 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800172 struct kvm_shared_msr_values {
173 u64 host;
174 u64 curr;
175 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300176};
177
178static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200179static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300180
Sean Christophersoncfc48182020-03-02 15:56:23 -0800181#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
182 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
183 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
184 | XFEATURE_MASK_PKRU)
185
Sean Christopherson91661982020-03-02 15:57:06 -0800186u64 __read_mostly host_efer;
187EXPORT_SYMBOL_GPL(host_efer);
188
Aaron Lewis139a12c2019-10-21 16:30:25 -0700189static u64 __read_mostly host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100190u64 __read_mostly supported_xss;
191EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700192
Hollis Blanchard417bc302007-10-31 17:24:23 -0500193struct kvm_stats_debugfs_item debugfs_entries[] = {
Emanuele Giuseppe Esposito812756a2020-04-14 17:56:25 +0200194 VCPU_STAT("pf_fixed", pf_fixed),
195 VCPU_STAT("pf_guest", pf_guest),
196 VCPU_STAT("tlb_flush", tlb_flush),
197 VCPU_STAT("invlpg", invlpg),
198 VCPU_STAT("exits", exits),
199 VCPU_STAT("io_exits", io_exits),
200 VCPU_STAT("mmio_exits", mmio_exits),
201 VCPU_STAT("signal_exits", signal_exits),
202 VCPU_STAT("irq_window", irq_window_exits),
203 VCPU_STAT("nmi_window", nmi_window_exits),
204 VCPU_STAT("halt_exits", halt_exits),
205 VCPU_STAT("halt_successful_poll", halt_successful_poll),
206 VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
207 VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
208 VCPU_STAT("halt_wakeup", halt_wakeup),
209 VCPU_STAT("hypercalls", hypercalls),
210 VCPU_STAT("request_irq", request_irq_exits),
211 VCPU_STAT("irq_exits", irq_exits),
212 VCPU_STAT("host_state_reload", host_state_reload),
213 VCPU_STAT("fpu_reload", fpu_reload),
214 VCPU_STAT("insn_emulation", insn_emulation),
215 VCPU_STAT("insn_emulation_fail", insn_emulation_fail),
216 VCPU_STAT("irq_injections", irq_injections),
217 VCPU_STAT("nmi_injections", nmi_injections),
218 VCPU_STAT("req_event", req_event),
219 VCPU_STAT("l1d_flush", l1d_flush),
220 VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped),
221 VM_STAT("mmu_pte_write", mmu_pte_write),
222 VM_STAT("mmu_pte_updated", mmu_pte_updated),
223 VM_STAT("mmu_pde_zapped", mmu_pde_zapped),
224 VM_STAT("mmu_flooded", mmu_flooded),
225 VM_STAT("mmu_recycled", mmu_recycled),
226 VM_STAT("mmu_cache_miss", mmu_cache_miss),
227 VM_STAT("mmu_unsync", mmu_unsync),
228 VM_STAT("remote_tlb_flush", remote_tlb_flush),
229 VM_STAT("largepages", lpages, .mode = 0444),
230 VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444),
231 VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
Hollis Blanchard417bc302007-10-31 17:24:23 -0500232 { NULL }
233};
234
Dexuan Cui2acf9232010-06-10 11:27:12 +0800235u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800236u64 __read_mostly supported_xcr0;
237EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800238
Marc Orrb666a4b2018-11-06 14:53:56 -0800239struct kmem_cache *x86_fpu_cache;
240EXPORT_SYMBOL_GPL(x86_fpu_cache);
241
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800242static struct kmem_cache *x86_emulator_cache;
243
244static struct kmem_cache *kvm_alloc_emulator_cache(void)
245{
Sean Christopherson06add252020-02-18 15:29:50 -0800246 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
247 unsigned int size = sizeof(struct x86_emulate_ctxt);
248
249 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800250 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800251 SLAB_ACCOUNT, useroffset,
252 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800253}
254
Jan Kiszkab6785de2012-09-20 07:43:17 +0200255static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300256
Gleb Natapovaf585b92010-10-14 11:22:46 +0200257static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
258{
259 int i;
260 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
261 vcpu->arch.apf.gfns[i] = ~0;
262}
263
Avi Kivity18863bd2009-09-07 11:12:18 +0300264static void kvm_on_user_return(struct user_return_notifier *urn)
265{
266 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300267 struct kvm_shared_msrs *locals
268 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800269 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700270 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300271
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700272 /*
273 * Disabling irqs at this point since the following code could be
274 * interrupted and executed through kvm_arch_hardware_disable()
275 */
276 local_irq_save(flags);
277 if (locals->registered) {
278 locals->registered = false;
279 user_return_notifier_unregister(urn);
280 }
281 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300282 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800283 values = &locals->values[slot];
284 if (values->host != values->curr) {
285 wrmsrl(shared_msrs_global.msrs[slot], values->host);
286 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300287 }
288 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300289}
290
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800291void kvm_define_shared_msr(unsigned slot, u32 msr)
292{
Nadav Amit0123be42014-07-24 15:06:56 +0300293 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200294 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300295 if (slot >= shared_msrs_global.nr)
296 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300297}
298EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
299
300static void kvm_shared_msr_cpu_online(void)
301{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800302 unsigned int cpu = smp_processor_id();
303 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
304 u64 value;
305 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300306
Sean Christopherson05c19c22019-11-22 12:04:50 -0800307 for (i = 0; i < shared_msrs_global.nr; ++i) {
308 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
309 smsr->values[i].host = value;
310 smsr->values[i].curr = value;
311 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300312}
313
Andy Honig8b3c3102014-08-27 11:16:44 -0700314int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300315{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200316 unsigned int cpu = smp_processor_id();
317 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700318 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300319
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500320 value = (value & mask) | (smsr->values[slot].host & ~mask);
321 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700322 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700323 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
324 if (err)
325 return 1;
326
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500327 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300328 if (!smsr->registered) {
329 smsr->urn.on_user_return = kvm_on_user_return;
330 user_return_notifier_register(&smsr->urn);
331 smsr->registered = true;
332 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700333 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300334}
335EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
336
Radim Krčmář13a34e02014-08-28 15:13:03 +0200337static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200338{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200339 unsigned int cpu = smp_processor_id();
340 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200341
342 if (smsr->registered)
343 kvm_on_user_return(&smsr->urn);
344}
345
Carsten Otte6866b832007-10-29 16:09:10 +0100346u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
347{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300348 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100349}
350EXPORT_SYMBOL_GPL(kvm_get_apic_base);
351
Jim Mattson58871642018-05-09 16:56:04 -0400352enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
353{
354 return kvm_apic_mode(kvm_get_apic_base(vcpu));
355}
356EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
357
Jan Kiszka58cb6282014-01-24 16:48:44 +0100358int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100359{
Jim Mattson58871642018-05-09 16:56:04 -0400360 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
361 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200362 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
363 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100364
Jim Mattson58871642018-05-09 16:56:04 -0400365 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700366 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400367 if (!msr_info->host_initiated) {
368 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
369 return 1;
370 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
371 return 1;
372 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100373
374 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800375 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100376 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100377}
378EXPORT_SYMBOL_GPL(kvm_set_apic_base);
379
Andi Kleen2605fc22014-05-02 00:44:37 +0200380asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000381{
382 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200383 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000384}
385EXPORT_SYMBOL_GPL(kvm_spurious_fault);
386
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200387#define EXCPT_BENIGN 0
388#define EXCPT_CONTRIBUTORY 1
389#define EXCPT_PF 2
390
391static int exception_class(int vector)
392{
393 switch (vector) {
394 case PF_VECTOR:
395 return EXCPT_PF;
396 case DE_VECTOR:
397 case TS_VECTOR:
398 case NP_VECTOR:
399 case SS_VECTOR:
400 case GP_VECTOR:
401 return EXCPT_CONTRIBUTORY;
402 default:
403 break;
404 }
405 return EXCPT_BENIGN;
406}
407
Nadav Amitd6e8c852014-07-24 14:51:24 +0300408#define EXCPT_FAULT 0
409#define EXCPT_TRAP 1
410#define EXCPT_ABORT 2
411#define EXCPT_INTERRUPT 3
412
413static int exception_type(int vector)
414{
415 unsigned int mask;
416
417 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
418 return EXCPT_INTERRUPT;
419
420 mask = 1 << vector;
421
422 /* #DB is trap, as instruction watchpoints are handled elsewhere */
423 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
424 return EXCPT_TRAP;
425
426 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
427 return EXCPT_ABORT;
428
429 /* Reserved exceptions will result in fault */
430 return EXCPT_FAULT;
431}
432
Jim Mattsonda998b42018-10-16 14:29:22 -0700433void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
434{
435 unsigned nr = vcpu->arch.exception.nr;
436 bool has_payload = vcpu->arch.exception.has_payload;
437 unsigned long payload = vcpu->arch.exception.payload;
438
439 if (!has_payload)
440 return;
441
442 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700443 case DB_VECTOR:
444 /*
445 * "Certain debug exceptions may clear bit 0-3. The
446 * remaining contents of the DR6 register are never
447 * cleared by the processor".
448 */
449 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
450 /*
451 * DR6.RTM is set by all #DB exceptions that don't clear it.
452 */
453 vcpu->arch.dr6 |= DR6_RTM;
454 vcpu->arch.dr6 |= payload;
455 /*
456 * Bit 16 should be set in the payload whenever the #DB
457 * exception should clear DR6.RTM. This makes the payload
458 * compatible with the pending debug exceptions under VMX.
459 * Though not currently documented in the SDM, this also
460 * makes the payload compatible with the exit qualification
461 * for #DB exceptions under VMX.
462 */
463 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800464
465 /*
466 * The #DB payload is defined as compatible with the 'pending
467 * debug exceptions' field under VMX, not DR6. While bit 12 is
468 * defined in the 'pending debug exceptions' field (enabled
469 * breakpoint), it is reserved and must be zero in DR6.
470 */
471 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700472 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700473 case PF_VECTOR:
474 vcpu->arch.cr2 = payload;
475 break;
476 }
477
478 vcpu->arch.exception.has_payload = false;
479 vcpu->arch.exception.payload = 0;
480}
481EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
482
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200483static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200484 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700485 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200486{
487 u32 prev_nr;
488 int class1, class2;
489
Avi Kivity3842d132010-07-27 12:30:24 +0300490 kvm_make_request(KVM_REQ_EVENT, vcpu);
491
Wanpeng Li664f8e22017-08-24 03:35:09 -0700492 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200493 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200494 if (has_error && !is_protmode(vcpu))
495 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700496 if (reinject) {
497 /*
498 * On vmentry, vcpu->arch.exception.pending is only
499 * true if an event injection was blocked by
500 * nested_run_pending. In that case, however,
501 * vcpu_enter_guest requests an immediate exit,
502 * and the guest shouldn't proceed far enough to
503 * need reinjection.
504 */
505 WARN_ON_ONCE(vcpu->arch.exception.pending);
506 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700507 if (WARN_ON_ONCE(has_payload)) {
508 /*
509 * A reinjected event has already
510 * delivered its payload.
511 */
512 has_payload = false;
513 payload = 0;
514 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700515 } else {
516 vcpu->arch.exception.pending = true;
517 vcpu->arch.exception.injected = false;
518 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200519 vcpu->arch.exception.has_error_code = has_error;
520 vcpu->arch.exception.nr = nr;
521 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700522 vcpu->arch.exception.has_payload = has_payload;
523 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800524 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700525 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200526 return;
527 }
528
529 /* to check exception */
530 prev_nr = vcpu->arch.exception.nr;
531 if (prev_nr == DF_VECTOR) {
532 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300533 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200534 return;
535 }
536 class1 = exception_class(prev_nr);
537 class2 = exception_class(nr);
538 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
539 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700540 /*
541 * Generate double fault per SDM Table 5-5. Set
542 * exception.pending = true so that the double fault
543 * can trigger a nested vmexit.
544 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200545 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700546 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200547 vcpu->arch.exception.has_error_code = true;
548 vcpu->arch.exception.nr = DF_VECTOR;
549 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700550 vcpu->arch.exception.has_payload = false;
551 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200552 } else
553 /* replace previous exception with a new one in a hope
554 that instruction re-execution will regenerate lost
555 exception */
556 goto queue;
557}
558
Avi Kivity298101d2007-11-25 13:41:11 +0200559void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
560{
Jim Mattson91e86d22018-10-16 14:29:21 -0700561 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200562}
563EXPORT_SYMBOL_GPL(kvm_queue_exception);
564
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200565void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
566{
Jim Mattson91e86d22018-10-16 14:29:21 -0700567 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200568}
569EXPORT_SYMBOL_GPL(kvm_requeue_exception);
570
Jim Mattsonf10c7292018-10-16 14:29:23 -0700571static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
572 unsigned long payload)
573{
574 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
575}
576
Jim Mattsonda998b42018-10-16 14:29:22 -0700577static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
578 u32 error_code, unsigned long payload)
579{
580 kvm_multiple_exception(vcpu, nr, true, error_code,
581 true, payload, false);
582}
583
Kyle Huey6affcbe2016-11-29 12:40:40 -0800584int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200585{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100586 if (err)
587 kvm_inject_gp(vcpu, 0);
588 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800589 return kvm_skip_emulated_instruction(vcpu);
590
591 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100592}
593EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200594
Avi Kivity6389ee92010-11-29 16:12:30 +0200595void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200596{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200597 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700598 vcpu->arch.exception.nested_apf =
599 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700600 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700601 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700602 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
603 } else {
604 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
605 fault->address);
606 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200607}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300608EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200609
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700610bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
611 struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200612{
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400613 struct kvm_mmu *fault_mmu;
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700614 WARN_ON_ONCE(fault->vector != PF_VECTOR);
615
Paolo Bonzini0cd665b2020-03-25 12:50:03 -0400616 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu :
617 vcpu->arch.walk_mmu;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200618
Junaid Shahidee1fa202020-03-20 14:28:03 -0700619 /*
620 * Invalidate the TLB entry for the faulting address, if it exists,
621 * else the access will fault indefinitely (and to emulate hardware).
622 */
623 if ((fault->error_code & PFERR_PRESENT_MASK) &&
624 !(fault->error_code & PFERR_RSVD_MASK))
625 kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address,
626 fault_mmu->root_hpa);
627
628 fault_mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200629 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200630}
Sean Christopherson53b3d8e2020-03-20 14:28:01 -0700631EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200632
Sheng Yang3419ffc2008-05-15 09:52:48 +0800633void kvm_inject_nmi(struct kvm_vcpu *vcpu)
634{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300635 atomic_inc(&vcpu->arch.nmi_queued);
636 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800637}
638EXPORT_SYMBOL_GPL(kvm_inject_nmi);
639
Avi Kivity298101d2007-11-25 13:41:11 +0200640void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
641{
Jim Mattson91e86d22018-10-16 14:29:21 -0700642 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200643}
644EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
645
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200646void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
647{
Jim Mattson91e86d22018-10-16 14:29:21 -0700648 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200649}
650EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
651
Carsten Ottea03490e2007-10-29 16:09:35 +0100652/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300653 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
654 * a #GP and return false.
655 */
656bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200657{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700658 if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300659 return true;
660 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
661 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100662}
Avi Kivity0a79b002009-09-01 12:03:25 +0300663EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100664
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300665bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
666{
667 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
668 return true;
669
670 kvm_queue_exception(vcpu, UD_VECTOR);
671 return false;
672}
673EXPORT_SYMBOL_GPL(kvm_require_dr);
674
Carsten Ottea03490e2007-10-29 16:09:35 +0100675/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200676 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200677 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200678 * can read from guest physical or from the guest's guest physical memory.
679 */
680int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
681 gfn_t ngfn, void *data, int offset, int len,
682 u32 access)
683{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200684 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200685 gfn_t real_gfn;
686 gpa_t ngpa;
687
688 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200689 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200690 if (real_gfn == UNMAPPED_GVA)
691 return -EFAULT;
692
693 real_gfn = gpa_to_gfn(real_gfn);
694
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200695 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200696}
697EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
698
Fengguang Wu69b00492015-01-19 22:33:39 +0800699static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200700 void *data, int offset, int len, u32 access)
701{
702 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
703 data, offset, len, access);
704}
705
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700706static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
707{
708 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
709 rsvd_bits(1, 2);
710}
711
Joerg Roedelec92fe42010-09-10 17:30:51 +0200712/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700713 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100714 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200715int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100716{
717 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
718 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
719 int i;
720 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200721 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100722
Joerg Roedelff03a072010-09-10 17:30:57 +0200723 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
724 offset * sizeof(u64), sizeof(pdpte),
725 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100726 if (ret < 0) {
727 ret = 0;
728 goto out;
729 }
730 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400731 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700732 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100733 ret = 0;
734 goto out;
735 }
736 }
737 ret = 1;
738
Joerg Roedelff03a072010-09-10 17:30:57 +0200739 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700740 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
741
Carsten Ottea03490e2007-10-29 16:09:35 +0100742out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100743
744 return ret;
745}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100746EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100747
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100748bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200749{
Joerg Roedelff03a072010-09-10 17:30:57 +0200750 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200751 int offset;
752 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200753 int r;
754
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200755 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200756 return false;
757
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700758 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300759 return true;
760
Paolo Bonzinia5121772017-07-24 18:54:38 +0200761 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
762 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200763 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
764 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200765 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800766 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200767
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800768 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200769}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100770EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200771
Avi Kivity49a9b072010-06-10 17:02:14 +0300772int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100773{
Sheng Yangaad82702010-05-12 16:40:42 +0800774 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800775 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800776
Avi Kivityf9a48e62010-01-06 19:10:22 +0200777 cr0 |= X86_CR0_ET;
778
Gleb Natapovab344822010-01-21 15:28:46 +0200779#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300780 if (cr0 & 0xffffffff00000000UL)
781 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200782#endif
783
784 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100785
Gleb Natapov0f122442010-04-28 19:15:31 +0300786 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
787 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100788
Gleb Natapov0f122442010-04-28 19:15:31 +0300789 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
790 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100791
792 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
793#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200794 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100795 int cs_db, cs_l;
796
Gleb Natapov0f122442010-04-28 19:15:31 +0300797 if (!is_pae(vcpu))
798 return 1;
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700799 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300800 if (cs_l)
801 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100802 } else
803#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200804 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200805 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300806 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100807 }
808
Mao, Junjiead756a12012-07-02 01:18:48 +0000809 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
810 return 1;
811
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700812 kvm_x86_ops.set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100813
Lai Jiangshand170c412011-02-21 11:21:30 +0800814 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800815 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800816 kvm_async_pf_hash_reset(vcpu);
817 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800818
Sheng Yangaad82702010-05-12 16:40:42 +0800819 if ((cr0 ^ old_cr0) & update_bits)
820 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800821
Laszlo Ersek879ae182015-11-04 12:54:41 +0100822 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
823 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
824 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800825 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
826
Gleb Natapov0f122442010-04-28 19:15:31 +0300827 return 0;
828}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200829EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100830
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200831void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100832{
Avi Kivity49a9b072010-06-10 17:02:14 +0300833 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100834}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200835EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100836
Aaron Lewis139a12c2019-10-21 16:30:25 -0700837void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300838{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700839 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300840
Aaron Lewis139a12c2019-10-21 16:30:25 -0700841 if (vcpu->arch.xcr0 != host_xcr0)
842 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
843
844 if (vcpu->arch.xsaves_enabled &&
845 vcpu->arch.ia32_xss != host_xss)
846 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
847 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300848}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700849EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
850
851void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
852{
853 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
854
855 if (vcpu->arch.xcr0 != host_xcr0)
856 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
857
858 if (vcpu->arch.xsaves_enabled &&
859 vcpu->arch.ia32_xss != host_xss)
860 wrmsrl(MSR_IA32_XSS, host_xss);
861 }
862
863}
864EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300865
Fengguang Wu69b00492015-01-19 22:33:39 +0800866static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800867{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000868 u64 xcr0 = xcr;
869 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200870 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800871
872 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
873 if (index != XCR_XFEATURE_ENABLED_MASK)
874 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700875 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800876 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700877 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800878 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200879
880 /*
881 * Do not allow the guest to set bits that we do not support
882 * saving. However, xcr0 bit 0 is always set, even if the
883 * emulated CPU does not support XSAVE (see fx_init).
884 */
Dave Hansend91cab72015-09-02 16:31:26 -0700885 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200886 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800887 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200888
Dave Hansend91cab72015-09-02 16:31:26 -0700889 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
890 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000891 return 1;
892
Dave Hansend91cab72015-09-02 16:31:26 -0700893 if (xcr0 & XFEATURE_MASK_AVX512) {
894 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800895 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700896 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800897 return 1;
898 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800899 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000900
Dave Hansend91cab72015-09-02 16:31:26 -0700901 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000902 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800903 return 0;
904}
905
906int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
907{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700908 if (kvm_x86_ops.get_cpl(vcpu) != 0 ||
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000909 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800910 kvm_inject_gp(vcpu, 0);
911 return 1;
912 }
913 return 0;
914}
915EXPORT_SYMBOL_GPL(kvm_set_xcr);
916
Sean Christopherson345599f2019-12-10 14:44:16 -0800917#define __cr4_reserved_bits(__cpu_has, __c) \
918({ \
919 u64 __reserved_bits = CR4_RESERVED_BITS; \
920 \
921 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
922 __reserved_bits |= X86_CR4_OSXSAVE; \
923 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
924 __reserved_bits |= X86_CR4_SMEP; \
925 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
926 __reserved_bits |= X86_CR4_SMAP; \
927 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
928 __reserved_bits |= X86_CR4_FSGSBASE; \
929 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
930 __reserved_bits |= X86_CR4_PKE; \
931 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
932 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800933 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
934 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800935 __reserved_bits; \
936})
937
Sean Christophersonb11306b2019-12-10 14:44:13 -0800938static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)
939{
Sean Christopherson345599f2019-12-10 14:44:16 -0800940 u64 reserved_bits = __cr4_reserved_bits(cpu_has, c);
Sean Christophersonb11306b2019-12-10 14:44:13 -0800941
Sean Christophersonc10398b2020-03-02 15:56:46 -0800942 if (kvm_cpu_cap_has(X86_FEATURE_LA57))
Sean Christopherson345599f2019-12-10 14:44:16 -0800943 reserved_bits &= ~X86_CR4_LA57;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800944
Sean Christopherson90d2f602020-03-02 15:56:47 -0800945 if (kvm_cpu_cap_has(X86_FEATURE_UMIP))
Sean Christopherson345599f2019-12-10 14:44:16 -0800946 reserved_bits &= ~X86_CR4_UMIP;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800947
948 return reserved_bits;
949}
950
Wanpeng Li3ca94192019-09-18 17:50:10 +0800951static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
952{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800953 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800954 return -EINVAL;
955
Sean Christopherson345599f2019-12-10 14:44:16 -0800956 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800957 return -EINVAL;
958
959 return 0;
960}
961
Avi Kivitya83b29c2010-06-10 17:02:15 +0300962int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100963{
Avi Kivityfc78f512009-12-07 12:16:48 +0200964 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800965 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800966 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800967
Wanpeng Li3ca94192019-09-18 17:50:10 +0800968 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200969 return 1;
970
Carsten Ottea03490e2007-10-29 16:09:35 +0100971 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300972 if (!(cr4 & X86_CR4_PAE))
973 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300974 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
975 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200976 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
977 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300978 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100979
Mao, Junjiead756a12012-07-02 01:18:48 +0000980 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200981 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000982 return 1;
983
984 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
985 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
986 return 1;
987 }
988
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700989 if (kvm_x86_ops.set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300990 return 1;
991
Mao, Junjiead756a12012-07-02 01:18:48 +0000992 if (((cr4 ^ old_cr4) & pdptr_bits) ||
993 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800994 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300995
Huaitong Hanb9baba82016-03-22 16:51:21 +0800996 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200997 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800998
Gleb Natapov0f122442010-04-28 19:15:31 +0300999 return 0;
1000}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001001EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +01001002
Avi Kivity23902182010-06-10 17:02:16 +03001003int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +01001004{
Junaid Shahidade61e22018-06-27 14:59:15 -07001005 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +01001006#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001007 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1008
Junaid Shahidade61e22018-06-27 14:59:15 -07001009 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001010 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1011 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001012 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001013#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001014
Avi Kivity9f8fe502010-12-05 17:30:00 +02001015 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001016 if (!skip_tlb_flush) {
1017 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001018 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001019 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001020 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001021 }
1022
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001023 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001024 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001025 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001026 else if (is_pae_paging(vcpu) &&
1027 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001028 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001029
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07001030 kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001031 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001032 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001033
Gleb Natapov0f122442010-04-28 19:15:31 +03001034 return 0;
1035}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001036EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001037
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001038int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001039{
Gleb Natapov0f122442010-04-28 19:15:31 +03001040 if (cr8 & CR8_RESERVED_BITS)
1041 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001042 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001043 kvm_lapic_set_tpr(vcpu, cr8);
1044 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001045 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001046 return 0;
1047}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001048EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001049
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001050unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001051{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001052 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001053 return kvm_lapic_get_cr8(vcpu);
1054 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001055 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001056}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001057EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001058
Nadav Amitae561ed2015-04-02 03:10:37 +03001059static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1060{
1061 int i;
1062
1063 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1064 for (i = 0; i < KVM_NR_DB_REGS; i++)
1065 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1066 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1067 }
1068}
1069
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001070static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1071{
1072 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001073 kvm_x86_ops.set_dr6(vcpu, vcpu->arch.dr6);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001074}
1075
Jan Kiszkac8639012012-09-21 05:42:55 +02001076static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1077{
1078 unsigned long dr7;
1079
1080 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1081 dr7 = vcpu->arch.guest_debug_dr7;
1082 else
1083 dr7 = vcpu->arch.dr7;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001084 kvm_x86_ops.set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001085 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1086 if (dr7 & DR7_BP_EN_MASK)
1087 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001088}
1089
Nadav Amit6f43ed02014-07-15 17:37:46 +03001090static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1091{
1092 u64 fixed = DR6_FIXED_1;
1093
Radim Krčmářd6321d42017-08-05 00:12:49 +02001094 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001095 fixed |= DR6_RTM;
1096 return fixed;
1097}
1098
Gleb Natapov338dbc92010-04-28 19:15:32 +03001099static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001100{
Marios Pomonisea740052019-12-11 12:47:52 -08001101 size_t size = ARRAY_SIZE(vcpu->arch.db);
1102
Gleb Natapov020df072010-04-13 10:05:23 +03001103 switch (dr) {
1104 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001105 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001106 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1107 vcpu->arch.eff_db[dr] = val;
1108 break;
1109 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001110 /* fall through */
1111 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001112 if (val & 0xffffffff00000000ULL)
1113 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001114 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001115 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001116 break;
1117 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001118 /* fall through */
1119 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001120 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001121 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001122 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001123 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001124 break;
1125 }
1126
1127 return 0;
1128}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001129
1130int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1131{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001132 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001133 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001134 return 1;
1135 }
1136 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001137}
Gleb Natapov020df072010-04-13 10:05:23 +03001138EXPORT_SYMBOL_GPL(kvm_set_dr);
1139
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001140int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001141{
Marios Pomonisea740052019-12-11 12:47:52 -08001142 size_t size = ARRAY_SIZE(vcpu->arch.db);
1143
Gleb Natapov020df072010-04-13 10:05:23 +03001144 switch (dr) {
1145 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001146 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001147 break;
1148 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001149 /* fall through */
1150 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001151 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1152 *val = vcpu->arch.dr6;
1153 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001154 *val = kvm_x86_ops.get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001155 break;
1156 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001157 /* fall through */
1158 default: /* 7 */
1159 *val = vcpu->arch.dr7;
1160 break;
1161 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001162 return 0;
1163}
Gleb Natapov020df072010-04-13 10:05:23 +03001164EXPORT_SYMBOL_GPL(kvm_get_dr);
1165
Avi Kivity022cd0e2011-11-10 14:57:23 +02001166bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1167{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001168 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001169 u64 data;
1170 int err;
1171
Wei Huangc6702c92015-06-19 13:44:45 +02001172 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001173 if (err)
1174 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001175 kvm_rax_write(vcpu, (u32)data);
1176 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001177 return err;
1178}
1179EXPORT_SYMBOL_GPL(kvm_rdpmc);
1180
Carsten Otte043405e2007-10-10 17:16:19 +02001181/*
1182 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1183 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1184 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001185 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1186 * extract the supported MSRs from the related const lists.
1187 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001188 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001189 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001190 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001191 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001192
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001193static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001194 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001195 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001196#ifdef CONFIG_X86_64
1197 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1198#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001199 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001200 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001201 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001202 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1203 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1204 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1205 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1206 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1207 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001208 MSR_IA32_UMWAIT_CONTROL,
1209
Jim Mattsone2ada662019-08-21 11:20:04 -07001210 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1211 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1212 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1213 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1214 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1215 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1216 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1217 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1218 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1219 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1220 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1221 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1222 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001223 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1224 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1225 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1226 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1227 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1228 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1229 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1230 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1231 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001232};
1233
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001234static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001235static unsigned num_msrs_to_save;
1236
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001237static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001238 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1239 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1240 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1241 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001242 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001243 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1244 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001245 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001246 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001247 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001248 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001249 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001250 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001251 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1252 HV_X64_MSR_TSC_EMULATION_STATUS,
1253
1254 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001255 MSR_KVM_PV_EOI_EN,
1256
Will Auldba904632012-11-29 12:42:50 -08001257 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001258 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001259 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001260 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001261 MSR_IA32_MCG_STATUS,
1262 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001263 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001264 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001265 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001266 MSR_PLATFORM_INFO,
1267 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001268 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001269 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001270 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001271
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001272 /*
1273 * The following list leaves out MSRs whose values are determined
1274 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1275 * We always support the "true" VMX control MSRs, even if the host
1276 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001277 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001278 */
1279 MSR_IA32_VMX_BASIC,
1280 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1281 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1282 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1283 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1284 MSR_IA32_VMX_MISC,
1285 MSR_IA32_VMX_CR0_FIXED0,
1286 MSR_IA32_VMX_CR4_FIXED0,
1287 MSR_IA32_VMX_VMCS_ENUM,
1288 MSR_IA32_VMX_PROCBASED_CTLS2,
1289 MSR_IA32_VMX_EPT_VPID_CAP,
1290 MSR_IA32_VMX_VMFUNC,
1291
Borislav Petkov191c8132019-04-18 18:32:50 +02001292 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001293 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001294};
1295
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001296static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001297static unsigned num_emulated_msrs;
1298
Tom Lendacky801e4592018-02-21 13:39:51 -06001299/*
1300 * List of msr numbers which are used to expose MSR-based features that
1301 * can be used by a hypervisor to validate requested CPU features.
1302 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001303static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001304 MSR_IA32_VMX_BASIC,
1305 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1306 MSR_IA32_VMX_PINBASED_CTLS,
1307 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1308 MSR_IA32_VMX_PROCBASED_CTLS,
1309 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1310 MSR_IA32_VMX_EXIT_CTLS,
1311 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1312 MSR_IA32_VMX_ENTRY_CTLS,
1313 MSR_IA32_VMX_MISC,
1314 MSR_IA32_VMX_CR0_FIXED0,
1315 MSR_IA32_VMX_CR0_FIXED1,
1316 MSR_IA32_VMX_CR4_FIXED0,
1317 MSR_IA32_VMX_CR4_FIXED1,
1318 MSR_IA32_VMX_VMCS_ENUM,
1319 MSR_IA32_VMX_PROCBASED_CTLS2,
1320 MSR_IA32_VMX_EPT_VPID_CAP,
1321 MSR_IA32_VMX_VMFUNC,
1322
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001323 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001324 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001325 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001326};
1327
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001328static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001329static unsigned int num_msr_based_features;
1330
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001331static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001332{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001333 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001334
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001335 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1336 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001337
1338 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001339 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1340 * the nested hypervisor runs with NX huge pages. If it is not,
1341 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1342 * L1 guests, so it need not worry about its own (L2) guests.
1343 */
1344 data |= ARCH_CAP_PSCHANGE_MC_NO;
1345
1346 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001347 * If we're doing cache flushes (either "always" or "cond")
1348 * we will do one whenever the guest does a vmlaunch/vmresume.
1349 * If an outer hypervisor is doing the cache flush for us
1350 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1351 * capability to the guest too, and if EPT is disabled we're not
1352 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1353 * require a nested hypervisor to do a flush of its own.
1354 */
1355 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1356 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1357
Paolo Bonzini0c549142019-08-19 17:24:07 +02001358 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1359 data |= ARCH_CAP_RDCL_NO;
1360 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1361 data |= ARCH_CAP_SSB_NO;
1362 if (!boot_cpu_has_bug(X86_BUG_MDS))
1363 data |= ARCH_CAP_MDS_NO;
1364
Pawan Guptae1d38b62019-10-23 12:23:33 +02001365 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001366 * On TAA affected systems:
1367 * - nothing to do if TSX is disabled on the host.
1368 * - we emulate TSX_CTRL if present on the host.
1369 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001370 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001371 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001372 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001373 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1374 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001375
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001376 return data;
1377}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001378
Wanpeng Li66421c12018-02-28 14:03:30 +08001379static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1380{
1381 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001382 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001383 msr->data = kvm_get_arch_capabilities();
1384 break;
1385 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001386 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001387 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001388 default:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001389 if (kvm_x86_ops.get_msr_feature(msr))
Wanpeng Li66421c12018-02-28 14:03:30 +08001390 return 1;
1391 }
1392 return 0;
1393}
1394
Tom Lendacky801e4592018-02-21 13:39:51 -06001395static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1396{
1397 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001398 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001399
1400 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001401 r = kvm_get_msr_feature(&msr);
1402 if (r)
1403 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001404
1405 *data = msr.data;
1406
1407 return 0;
1408}
1409
Sean Christopherson11988492019-04-02 08:19:15 -07001410static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1411{
1412 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1413 return false;
1414
1415 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1416 return false;
1417
Sean Christopherson0a629562019-04-02 08:19:16 -07001418 if (efer & (EFER_LME | EFER_LMA) &&
1419 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1420 return false;
1421
1422 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1423 return false;
1424
Sean Christopherson11988492019-04-02 08:19:15 -07001425 return true;
1426
1427}
Jan Kiszka384bb782013-04-20 10:52:36 +02001428bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001429{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001430 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001431 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001432
Sean Christopherson11988492019-04-02 08:19:15 -07001433 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001434}
1435EXPORT_SYMBOL_GPL(kvm_valid_efer);
1436
Sean Christopherson11988492019-04-02 08:19:15 -07001437static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001438{
1439 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001440 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001441
Sean Christopherson11988492019-04-02 08:19:15 -07001442 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001443 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001444
Sean Christopherson11988492019-04-02 08:19:15 -07001445 if (!msr_info->host_initiated) {
1446 if (!__kvm_valid_efer(vcpu, efer))
1447 return 1;
1448
1449 if (is_paging(vcpu) &&
1450 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1451 return 1;
1452 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001453
Carsten Otte15c4a642007-10-30 18:44:17 +01001454 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001455 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001456
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001457 kvm_x86_ops.set_efer(vcpu, efer);
Sheng Yanga3d204e2010-05-12 16:40:40 +08001458
Sheng Yangaad82702010-05-12 16:40:42 +08001459 /* Update reserved bits */
1460 if ((efer ^ old_efer) & EFER_NX)
1461 kvm_mmu_reset_context(vcpu);
1462
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001463 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001464}
1465
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001466void kvm_enable_efer_bits(u64 mask)
1467{
1468 efer_reserved_bits &= ~mask;
1469}
1470EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1471
Carsten Otte15c4a642007-10-30 18:44:17 +01001472/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001473 * Write @data into the MSR specified by @index. Select MSR specific fault
1474 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001475 * Returns 0 on success, non-0 otherwise.
1476 * Assumes vcpu_load() was already called.
1477 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001478static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1479 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001480{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001481 struct msr_data msr;
1482
1483 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001484 case MSR_FS_BASE:
1485 case MSR_GS_BASE:
1486 case MSR_KERNEL_GS_BASE:
1487 case MSR_CSTAR:
1488 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001489 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001490 return 1;
1491 break;
1492 case MSR_IA32_SYSENTER_EIP:
1493 case MSR_IA32_SYSENTER_ESP:
1494 /*
1495 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1496 * non-canonical address is written on Intel but not on
1497 * AMD (which ignores the top 32-bits, because it does
1498 * not implement 64-bit SYSENTER).
1499 *
1500 * 64-bit code should hence be able to write a non-canonical
1501 * value on AMD. Making the address canonical ensures that
1502 * vmentry does not fail on Intel after writing a non-canonical
1503 * value, and that something deterministic happens if the guest
1504 * invokes 64-bit SYSENTER.
1505 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001506 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001507 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001508
1509 msr.data = data;
1510 msr.index = index;
1511 msr.host_initiated = host_initiated;
1512
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001513 return kvm_x86_ops.set_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001514}
1515
1516/*
1517 * Read the MSR specified by @index into @data. Select MSR specific fault
1518 * checks are bypassed if @host_initiated is %true.
1519 * Returns 0 on success, non-0 otherwise.
1520 * Assumes vcpu_load() was already called.
1521 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001522int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1523 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001524{
1525 struct msr_data msr;
1526 int ret;
1527
1528 msr.index = index;
1529 msr.host_initiated = host_initiated;
1530
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001531 ret = kvm_x86_ops.get_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001532 if (!ret)
1533 *data = msr.data;
1534 return ret;
1535}
1536
1537int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1538{
1539 return __kvm_get_msr(vcpu, index, data, false);
1540}
1541EXPORT_SYMBOL_GPL(kvm_get_msr);
1542
1543int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1544{
1545 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001546}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001547EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001548
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001549int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1550{
1551 u32 ecx = kvm_rcx_read(vcpu);
1552 u64 data;
1553
1554 if (kvm_get_msr(vcpu, ecx, &data)) {
1555 trace_kvm_msr_read_ex(ecx);
1556 kvm_inject_gp(vcpu, 0);
1557 return 1;
1558 }
1559
1560 trace_kvm_msr_read(ecx, data);
1561
1562 kvm_rax_write(vcpu, data & -1u);
1563 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1564 return kvm_skip_emulated_instruction(vcpu);
1565}
1566EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1567
1568int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1569{
1570 u32 ecx = kvm_rcx_read(vcpu);
1571 u64 data = kvm_read_edx_eax(vcpu);
1572
1573 if (kvm_set_msr(vcpu, ecx, data)) {
1574 trace_kvm_msr_write_ex(ecx, data);
1575 kvm_inject_gp(vcpu, 0);
1576 return 1;
1577 }
1578
1579 trace_kvm_msr_write(ecx, data);
1580 return kvm_skip_emulated_instruction(vcpu);
1581}
1582EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1583
Carsten Otte313a3dc2007-10-11 19:16:52 +02001584/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001585 * The fast path for frequent and performance sensitive wrmsr emulation,
1586 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1587 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1588 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1589 * other cases which must be called after interrupts are enabled on the host.
1590 */
1591static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1592{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001593 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1594 return 1;
1595
1596 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001597 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001598 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1599 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001600
Wanpeng Lid5361672020-03-26 10:20:02 +08001601 data &= ~(1 << 12);
1602 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001603 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001604 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1605 trace_kvm_apic_write(APIC_ICR, (u32)data);
1606 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001607 }
1608
1609 return 1;
1610}
1611
1612enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1613{
1614 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001615 u64 data;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001616 int ret = 0;
1617
1618 switch (msr) {
1619 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001620 data = kvm_read_edx_eax(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001621 ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1622 break;
1623 default:
1624 return EXIT_FASTPATH_NONE;
1625 }
1626
1627 if (!ret) {
1628 trace_kvm_msr_write(msr, data);
1629 return EXIT_FASTPATH_SKIP_EMUL_INS;
1630 }
1631
1632 return EXIT_FASTPATH_NONE;
1633}
1634EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1635
1636/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001637 * Adapt set_msr() to msr_io()'s calling convention
1638 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001639static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1640{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001641 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001642}
1643
Carsten Otte313a3dc2007-10-11 19:16:52 +02001644static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1645{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001646 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001647}
1648
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001649#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001650struct pvclock_clock {
1651 int vclock_mode;
1652 u64 cycle_last;
1653 u64 mask;
1654 u32 mult;
1655 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001656 u64 base_cycles;
1657 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001658};
1659
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001660struct pvclock_gtod_data {
1661 seqcount_t seq;
1662
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001663 struct pvclock_clock clock; /* extract of a clocksource struct */
1664 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001665
Paolo Bonzini917f9472020-01-22 14:32:20 +01001666 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001667 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001668};
1669
1670static struct pvclock_gtod_data pvclock_gtod_data;
1671
1672static void update_pvclock_gtod(struct timekeeper *tk)
1673{
1674 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1675
1676 write_seqcount_begin(&vdata->seq);
1677
1678 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001679 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001680 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1681 vdata->clock.mask = tk->tkr_mono.mask;
1682 vdata->clock.mult = tk->tkr_mono.mult;
1683 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001684 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1685 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001686
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001687 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001688 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1689 vdata->raw_clock.mask = tk->tkr_raw.mask;
1690 vdata->raw_clock.mult = tk->tkr_raw.mult;
1691 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001692 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1693 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001694
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001695 vdata->wall_time_sec = tk->xtime_sec;
1696
Paolo Bonzini917f9472020-01-22 14:32:20 +01001697 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001698
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001699 write_seqcount_end(&vdata->seq);
1700}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001701
1702static s64 get_kvmclock_base_ns(void)
1703{
1704 /* Count up from boot time, but with the frequency of the raw clock. */
1705 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1706}
1707#else
1708static s64 get_kvmclock_base_ns(void)
1709{
1710 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1711 return ktime_get_boottime_ns();
1712}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001713#endif
1714
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001715void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1716{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001717 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001718 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001719}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001720
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001721static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1722{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001723 int version;
1724 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001725 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001726 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001727
1728 if (!wall_clock)
1729 return;
1730
Avi Kivity9ed3c442010-05-04 15:00:37 +03001731 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1732 if (r)
1733 return;
1734
1735 if (version & 1)
1736 ++version; /* first time write, random junk */
1737
1738 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001739
Nicholas Krause1dab1342015-12-30 13:08:46 -05001740 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1741 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001742
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001743 /*
1744 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001745 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001746 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001747 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001748 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001749
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001750 wc.nsec = do_div(wall_nsec, 1000000000);
1751 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001752 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001753
1754 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1755
1756 version++;
1757 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001758}
1759
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001760static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1761{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001762 do_shl32_div32(dividend, divisor);
1763 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001764}
1765
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001766static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001767 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001768{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001769 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001770 int32_t shift = 0;
1771 uint64_t tps64;
1772 uint32_t tps32;
1773
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001774 tps64 = base_hz;
1775 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001776 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001777 tps64 >>= 1;
1778 shift--;
1779 }
1780
1781 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001782 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1783 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001784 scaled64 >>= 1;
1785 else
1786 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001787 shift++;
1788 }
1789
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001790 *pshift = shift;
1791 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001792}
1793
Marcelo Tosattid8281992012-11-27 23:29:01 -02001794#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001795static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001796#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001797
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001798static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001799static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001800
Zachary Amsdencc578282012-02-03 15:43:50 -02001801static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001802{
Zachary Amsdencc578282012-02-03 15:43:50 -02001803 u64 v = (u64)khz * (1000000 + ppm);
1804 do_div(v, 1000000);
1805 return v;
1806}
1807
Haozhong Zhang381d5852015-10-20 15:39:04 +08001808static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1809{
1810 u64 ratio;
1811
1812 /* Guest TSC same frequency as host TSC? */
1813 if (!scale) {
1814 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1815 return 0;
1816 }
1817
1818 /* TSC scaling supported? */
1819 if (!kvm_has_tsc_control) {
1820 if (user_tsc_khz > tsc_khz) {
1821 vcpu->arch.tsc_catchup = 1;
1822 vcpu->arch.tsc_always_catchup = 1;
1823 return 0;
1824 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001825 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001826 return -1;
1827 }
1828 }
1829
1830 /* TSC scaling required - calculate ratio */
1831 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1832 user_tsc_khz, tsc_khz);
1833
1834 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001835 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1836 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001837 return -1;
1838 }
1839
1840 vcpu->arch.tsc_scaling_ratio = ratio;
1841 return 0;
1842}
1843
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001844static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001845{
1846 u32 thresh_lo, thresh_hi;
1847 int use_scaling = 0;
1848
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001849 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001850 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001851 /* set tsc_scaling_ratio to a safe value */
1852 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001853 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001854 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001855
Zachary Amsdenc2855452010-09-18 14:38:15 -10001856 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001857 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001858 &vcpu->arch.virtual_tsc_shift,
1859 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001860 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001861
1862 /*
1863 * Compute the variation in TSC rate which is acceptable
1864 * within the range of tolerance and decide if the
1865 * rate being applied is within that bounds of the hardware
1866 * rate. If so, no scaling or compensation need be done.
1867 */
1868 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1869 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001870 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1871 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 -02001872 use_scaling = 1;
1873 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001874 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001875}
1876
1877static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1878{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001879 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001880 vcpu->arch.virtual_tsc_mult,
1881 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001882 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001883 return tsc;
1884}
1885
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001886static inline int gtod_is_based_on_tsc(int mode)
1887{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001888 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001889}
1890
Fengguang Wu69b00492015-01-19 22:33:39 +08001891static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001892{
1893#ifdef CONFIG_X86_64
1894 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001895 struct kvm_arch *ka = &vcpu->kvm->arch;
1896 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1897
1898 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1899 atomic_read(&vcpu->kvm->online_vcpus));
1900
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001901 /*
1902 * Once the masterclock is enabled, always perform request in
1903 * order to update it.
1904 *
1905 * In order to enable masterclock, the host clocksource must be TSC
1906 * and the vcpus need to have matched TSCs. When that happens,
1907 * perform request to enable masterclock.
1908 */
1909 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001910 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001911 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1912
1913 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1914 atomic_read(&vcpu->kvm->online_vcpus),
1915 ka->use_master_clock, gtod->clock.vclock_mode);
1916#endif
1917}
1918
Will Auldba904632012-11-29 12:42:50 -08001919static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1920{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001921 u64 curr_offset = kvm_x86_ops.read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001922 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1923}
1924
Haozhong Zhang35181e82015-10-20 15:39:03 +08001925/*
1926 * Multiply tsc by a fixed point number represented by ratio.
1927 *
1928 * The most significant 64-N bits (mult) of ratio represent the
1929 * integral part of the fixed point number; the remaining N bits
1930 * (frac) represent the fractional part, ie. ratio represents a fixed
1931 * point number (mult + frac * 2^(-N)).
1932 *
1933 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1934 */
1935static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1936{
1937 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1938}
1939
1940u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1941{
1942 u64 _tsc = tsc;
1943 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1944
1945 if (ratio != kvm_default_tsc_scaling_ratio)
1946 _tsc = __scale_tsc(ratio, tsc);
1947
1948 return _tsc;
1949}
1950EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1951
Haozhong Zhang07c14192015-10-20 15:39:05 +08001952static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1953{
1954 u64 tsc;
1955
1956 tsc = kvm_scale_tsc(vcpu, rdtsc());
1957
1958 return target_tsc - tsc;
1959}
1960
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001961u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1962{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001963 u64 tsc_offset = kvm_x86_ops.read_l1_tsc_offset(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001964
1965 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001966}
1967EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1968
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001969static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1970{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001971 vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001972}
1973
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001974static inline bool kvm_check_tsc_unstable(void)
1975{
1976#ifdef CONFIG_X86_64
1977 /*
1978 * TSC is marked unstable when we're running on Hyper-V,
1979 * 'TSC page' clocksource is good.
1980 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001981 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001982 return false;
1983#endif
1984 return check_tsc_unstable();
1985}
1986
Will Auld8fe8ab42012-11-29 12:42:12 -08001987void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001988{
1989 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001990 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001991 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001992 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001993 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001994 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001995 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001996
Jan Kiszka038f8c12011-02-04 10:49:11 +01001997 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001998 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001999 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002000 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002001
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03002002 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03002003 if (data == 0 && msr->host_initiated) {
2004 /*
2005 * detection of vcpu initialization -- need to sync
2006 * with other vCPUs. This particularly helps to keep
2007 * kvm_clock stable after CPU hotplug
2008 */
2009 synchronizing = true;
2010 } else {
2011 u64 tsc_exp = kvm->arch.last_tsc_write +
2012 nsec_to_cycles(vcpu, elapsed);
2013 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2014 /*
2015 * Special case: TSC write with a small delta (1 second)
2016 * of virtual cycle time against real time is
2017 * interpreted as an attempt to synchronize the CPU.
2018 */
2019 synchronizing = data < tsc_exp + tsc_hz &&
2020 data + tsc_hz > tsc_exp;
2021 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002022 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002023
2024 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002025 * For a reliable TSC, we can match TSC offsets, and for an unstable
2026 * TSC, we add elapsed time in this computation. We could let the
2027 * compensation code attempt to catch up if we fall behind, but
2028 * it's better to try to match offsets from the beginning.
2029 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002030 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002031 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002032 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002033 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002034 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002035 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002036 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002037 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002038 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002039 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002040 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002041 } else {
2042 /*
2043 * We split periods of matched TSC writes into generations.
2044 * For each generation, we track the original measured
2045 * nanosecond time, offset, and write, so if TSCs are in
2046 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002047 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002048 *
2049 * These values are tracked in kvm->arch.cur_xxx variables.
2050 */
2051 kvm->arch.cur_tsc_generation++;
2052 kvm->arch.cur_tsc_nsec = ns;
2053 kvm->arch.cur_tsc_write = data;
2054 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002055 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002056 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002057
2058 /*
2059 * We also track th most recent recorded KHZ, write and time to
2060 * allow the matching interval to be extended at each write.
2061 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002062 kvm->arch.last_tsc_nsec = ns;
2063 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002064 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002065
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002066 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002067
2068 /* Keep track of which generation this VCPU has synchronized to */
2069 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2070 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2071 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2072
Radim Krčmářd6321d42017-08-05 00:12:49 +02002073 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002074 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002075
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002076 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002077 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002078
2079 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002080 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002081 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002082 } else if (!already_matched) {
2083 kvm->arch.nr_vcpus_matched_tsc++;
2084 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002085
2086 kvm_track_tsc_matching(vcpu);
2087 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002088}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002089
Zachary Amsden99e3e302010-08-19 22:07:17 -10002090EXPORT_SYMBOL_GPL(kvm_write_tsc);
2091
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002092static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2093 s64 adjustment)
2094{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07002095 u64 tsc_offset = kvm_x86_ops.read_l1_tsc_offset(vcpu);
Leonid Shatz326e7422018-11-06 12:14:25 +02002096 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002097}
2098
2099static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2100{
2101 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2102 WARN_ON(adjustment < 0);
2103 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002104 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002105}
2106
Marcelo Tosattid8281992012-11-27 23:29:01 -02002107#ifdef CONFIG_X86_64
2108
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002109static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002110{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002111 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002112 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002113
2114 if (likely(ret >= last))
2115 return ret;
2116
2117 /*
2118 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002119 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002120 * very likely) and there's a data dependence, so force GCC
2121 * to generate a branch instead. I don't barrier() because
2122 * we don't actually need a barrier, and if this function
2123 * ever gets inlined it will generate worse code.
2124 */
2125 asm volatile ("");
2126 return last;
2127}
2128
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002129static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2130 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002131{
2132 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002133 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002134
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002135 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002136 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002137 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2138 tsc_timestamp);
2139 if (tsc_pg_val != U64_MAX) {
2140 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002141 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002142 v = (tsc_pg_val - clock->cycle_last) &
2143 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002144 } else {
2145 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002146 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002147 }
2148 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002149 case VDSO_CLOCKMODE_TSC:
2150 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002151 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002152 v = (*tsc_timestamp - clock->cycle_last) &
2153 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002154 break;
2155 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002156 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002157 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002158
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002159 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002160 *tsc_timestamp = v = 0;
2161
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002162 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002163}
2164
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002165static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002166{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002167 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002168 unsigned long seq;
2169 int mode;
2170 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002171
Marcelo Tosattid8281992012-11-27 23:29:01 -02002172 do {
2173 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002174 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002175 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002176 ns >>= gtod->raw_clock.shift;
2177 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002178 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002179 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002180
2181 return mode;
2182}
2183
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002184static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002185{
2186 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2187 unsigned long seq;
2188 int mode;
2189 u64 ns;
2190
2191 do {
2192 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002193 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002194 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002195 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002196 ns >>= gtod->clock.shift;
2197 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2198
2199 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2200 ts->tv_nsec = ns;
2201
2202 return mode;
2203}
2204
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002205/* returns true if host is using TSC based clocksource */
2206static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002207{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002208 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002209 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002210 return false;
2211
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002212 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002213 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002214}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002215
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002216/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002217static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002218 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002219{
2220 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002221 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002222 return false;
2223
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002224 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002225}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002226#endif
2227
2228/*
2229 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002230 * Assuming a stable TSC across physical CPUS, and a stable TSC
2231 * across virtual CPUs, the following condition is possible.
2232 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002233 * CPUs at the next numbered event.
2234 *
2235 * "timespecX" represents host monotonic time. "tscX" represents
2236 * RDTSC value.
2237 *
2238 * VCPU0 on CPU0 | VCPU1 on CPU1
2239 *
2240 * 1. read timespec0,tsc0
2241 * 2. | timespec1 = timespec0 + N
2242 * | tsc1 = tsc0 + M
2243 * 3. transition to guest | transition to guest
2244 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2245 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2246 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2247 *
2248 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2249 *
2250 * - ret0 < ret1
2251 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2252 * ...
2253 * - 0 < N - M => M < N
2254 *
2255 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2256 * always the case (the difference between two distinct xtime instances
2257 * might be smaller then the difference between corresponding TSC reads,
2258 * when updating guest vcpus pvclock areas).
2259 *
2260 * To avoid that problem, do not allow visibility of distinct
2261 * system_timestamp/tsc_timestamp values simultaneously: use a master
2262 * copy of host monotonic time values. Update that master copy
2263 * in lockstep.
2264 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002265 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002266 *
2267 */
2268
2269static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2270{
2271#ifdef CONFIG_X86_64
2272 struct kvm_arch *ka = &kvm->arch;
2273 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002274 bool host_tsc_clocksource, vcpus_matched;
2275
2276 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2277 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002278
2279 /*
2280 * If the host uses TSC clock, then passthrough TSC as stable
2281 * to the guest.
2282 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002283 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002284 &ka->master_kernel_ns,
2285 &ka->master_cycle_now);
2286
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002287 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002288 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002289 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002290
Marcelo Tosattid8281992012-11-27 23:29:01 -02002291 if (ka->use_master_clock)
2292 atomic_set(&kvm_guest_has_master_clock, 1);
2293
2294 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002295 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2296 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002297#endif
2298}
2299
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002300void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2301{
2302 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2303}
2304
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002305static void kvm_gen_update_masterclock(struct kvm *kvm)
2306{
2307#ifdef CONFIG_X86_64
2308 int i;
2309 struct kvm_vcpu *vcpu;
2310 struct kvm_arch *ka = &kvm->arch;
2311
2312 spin_lock(&ka->pvclock_gtod_sync_lock);
2313 kvm_make_mclock_inprogress_request(kvm);
2314 /* no guest entries from this point */
2315 pvclock_update_vm_gtod_copy(kvm);
2316
2317 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002318 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002319
2320 /* guest entries allowed */
2321 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002322 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002323
2324 spin_unlock(&ka->pvclock_gtod_sync_lock);
2325#endif
2326}
2327
Marcelo Tosattie891a322017-04-17 12:51:37 -03002328u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002329{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002330 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002331 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002332 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002333
Paolo Bonzini8b953442016-11-16 18:31:30 +01002334 spin_lock(&ka->pvclock_gtod_sync_lock);
2335 if (!ka->use_master_clock) {
2336 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002337 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002338 }
2339
Paolo Bonzini8b953442016-11-16 18:31:30 +01002340 hv_clock.tsc_timestamp = ka->master_cycle_now;
2341 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2342 spin_unlock(&ka->pvclock_gtod_sync_lock);
2343
Wanpeng Lie2c22062017-05-11 18:12:05 -07002344 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2345 get_cpu();
2346
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002347 if (__this_cpu_read(cpu_tsc_khz)) {
2348 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2349 &hv_clock.tsc_shift,
2350 &hv_clock.tsc_to_system_mul);
2351 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2352 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002353 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002354
2355 put_cpu();
2356
2357 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002358}
2359
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002360static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2361{
2362 struct kvm_vcpu_arch *vcpu = &v->arch;
2363 struct pvclock_vcpu_time_info guest_hv_clock;
2364
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002365 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002366 &guest_hv_clock, sizeof(guest_hv_clock))))
2367 return;
2368
2369 /* This VCPU is paused, but it's legal for a guest to read another
2370 * VCPU's kvmclock, so we really have to follow the specification where
2371 * it says that version is odd if data is being modified, and even after
2372 * it is consistent.
2373 *
2374 * Version field updates must be kept separate. This is because
2375 * kvm_write_guest_cached might use a "rep movs" instruction, and
2376 * writes within a string instruction are weakly ordered. So there
2377 * are three writes overall.
2378 *
2379 * As a small optimization, only write the version field in the first
2380 * and third write. The vcpu->pv_time cache is still valid, because the
2381 * version field is the first in the struct.
2382 */
2383 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2384
Liran Alon51c4b8b2017-11-05 16:11:30 +02002385 if (guest_hv_clock.version & 1)
2386 ++guest_hv_clock.version; /* first time write, random junk */
2387
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002388 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002389 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2390 &vcpu->hv_clock,
2391 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002392
2393 smp_wmb();
2394
2395 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2396 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2397
2398 if (vcpu->pvclock_set_guest_stopped_request) {
2399 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2400 vcpu->pvclock_set_guest_stopped_request = false;
2401 }
2402
2403 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2404
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002405 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2406 &vcpu->hv_clock,
2407 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002408
2409 smp_wmb();
2410
2411 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002412 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2413 &vcpu->hv_clock,
2414 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002415}
2416
Zachary Amsden34c238a2010-09-18 14:38:14 -10002417static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002418{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002419 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002420 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002421 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002422 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002423 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002424 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002425 bool use_master_clock;
2426
2427 kernel_ns = 0;
2428 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002429
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002430 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002431 * If the host uses TSC clock, then passthrough TSC as stable
2432 * to the guest.
2433 */
2434 spin_lock(&ka->pvclock_gtod_sync_lock);
2435 use_master_clock = ka->use_master_clock;
2436 if (use_master_clock) {
2437 host_tsc = ka->master_cycle_now;
2438 kernel_ns = ka->master_kernel_ns;
2439 }
2440 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002441
2442 /* Keep irq disabled to prevent changes to the clock */
2443 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002444 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2445 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002446 local_irq_restore(flags);
2447 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2448 return 1;
2449 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002450 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002451 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002452 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002453 }
2454
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002455 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002456
2457 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002458 * We may have to catch up the TSC to match elapsed wall clock
2459 * time for two reasons, even if kvmclock is used.
2460 * 1) CPU could have been running below the maximum TSC rate
2461 * 2) Broken TSC compensation resets the base at each VCPU
2462 * entry to avoid unknown leaps of TSC even when running
2463 * again on the same CPU. This may cause apparent elapsed
2464 * time to disappear, and the guest to stand still or run
2465 * very slowly.
2466 */
2467 if (vcpu->tsc_catchup) {
2468 u64 tsc = compute_guest_tsc(v, kernel_ns);
2469 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002470 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002471 tsc_timestamp = tsc;
2472 }
2473 }
2474
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002475 local_irq_restore(flags);
2476
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002477 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002478
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002479 if (kvm_has_tsc_control)
2480 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2481
2482 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002483 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002484 &vcpu->hv_clock.tsc_shift,
2485 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002486 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002487 }
2488
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002489 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002490 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002491 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002492
Marcelo Tosattid8281992012-11-27 23:29:01 -02002493 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002494 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002495 if (use_master_clock)
2496 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2497
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002498 vcpu->hv_clock.flags = pvclock_flags;
2499
Paolo Bonzini095cf552016-02-08 12:54:12 +01002500 if (vcpu->pv_time_enabled)
2501 kvm_setup_pvclock_page(v);
2502 if (v == kvm_get_vcpu(v->kvm, 0))
2503 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002504 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002505}
2506
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002507/*
2508 * kvmclock updates which are isolated to a given vcpu, such as
2509 * vcpu->cpu migration, should not allow system_timestamp from
2510 * the rest of the vcpus to remain static. Otherwise ntp frequency
2511 * correction applies to one vcpu's system_timestamp but not
2512 * the others.
2513 *
2514 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002515 * We need to rate-limit these requests though, as they can
2516 * considerably slow guests that have a large number of vcpus.
2517 * The time for a remote vcpu to update its kvmclock is bound
2518 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002519 */
2520
Andrew Jones7e44e442014-02-28 12:52:54 +01002521#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2522
2523static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002524{
2525 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002526 struct delayed_work *dwork = to_delayed_work(work);
2527 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2528 kvmclock_update_work);
2529 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002530 struct kvm_vcpu *vcpu;
2531
2532 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002533 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002534 kvm_vcpu_kick(vcpu);
2535 }
2536}
2537
Andrew Jones7e44e442014-02-28 12:52:54 +01002538static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2539{
2540 struct kvm *kvm = v->kvm;
2541
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002542 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002543 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2544 KVMCLOCK_UPDATE_DELAY);
2545}
2546
Andrew Jones332967a2014-02-28 12:52:55 +01002547#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2548
2549static void kvmclock_sync_fn(struct work_struct *work)
2550{
2551 struct delayed_work *dwork = to_delayed_work(work);
2552 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2553 kvmclock_sync_work);
2554 struct kvm *kvm = container_of(ka, struct kvm, arch);
2555
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002556 if (!kvmclock_periodic_sync)
2557 return;
2558
Andrew Jones332967a2014-02-28 12:52:55 +01002559 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2560 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2561 KVMCLOCK_SYNC_PERIOD);
2562}
2563
Borislav Petkov191c8132019-04-18 18:32:50 +02002564/*
2565 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2566 */
2567static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2568{
2569 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002570 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002571 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2572
2573 return false;
2574}
2575
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002576static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002577{
2578 u64 mcg_cap = vcpu->arch.mcg_cap;
2579 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002580 u32 msr = msr_info->index;
2581 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002582
2583 switch (msr) {
2584 case MSR_IA32_MCG_STATUS:
2585 vcpu->arch.mcg_status = data;
2586 break;
2587 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002588 if (!(mcg_cap & MCG_CTL_P) &&
2589 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002590 return 1;
2591 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002592 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002593 vcpu->arch.mcg_ctl = data;
2594 break;
2595 default:
2596 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002597 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002598 u32 offset = array_index_nospec(
2599 msr - MSR_IA32_MC0_CTL,
2600 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2601
Andre Przywara114be422010-03-24 17:46:42 +01002602 /* only 0 or all 1s can be written to IA32_MCi_CTL
2603 * some Linux kernels though clear bit 10 in bank 4 to
2604 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2605 * this to avoid an uncatched #GP in the guest
2606 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002607 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002608 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002609 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002610
2611 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002612 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002613 (offset & 0x3) == 1 && data != 0) {
2614 if (!can_set_mci_status(vcpu))
2615 return -1;
2616 }
2617
Huang Ying890ca9a2009-05-11 16:48:15 +08002618 vcpu->arch.mce_banks[offset] = data;
2619 break;
2620 }
2621 return 1;
2622 }
2623 return 0;
2624}
2625
Ed Swierkffde22a2009-10-15 15:21:43 -07002626static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2627{
2628 struct kvm *kvm = vcpu->kvm;
2629 int lm = is_long_mode(vcpu);
2630 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2631 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2632 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2633 : kvm->arch.xen_hvm_config.blob_size_32;
2634 u32 page_num = data & ~PAGE_MASK;
2635 u64 page_addr = data & PAGE_MASK;
2636 u8 *page;
2637 int r;
2638
2639 r = -E2BIG;
2640 if (page_num >= blob_size)
2641 goto out;
2642 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002643 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2644 if (IS_ERR(page)) {
2645 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002646 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002647 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002648 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002649 goto out_free;
2650 r = 0;
2651out_free:
2652 kfree(page);
2653out:
2654 return r;
2655}
2656
Gleb Natapov344d9582010-10-14 11:22:50 +02002657static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2658{
2659 gpa_t gpa = data & ~0x3f;
2660
Wanpeng Li52a5c152017-07-13 18:30:42 -07002661 /* Bits 3:5 are reserved, Should be zero */
2662 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002663 return 1;
2664
2665 vcpu->arch.apf.msr_val = data;
2666
2667 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2668 kvm_clear_async_pf_completion_queue(vcpu);
2669 kvm_async_pf_hash_reset(vcpu);
2670 return 0;
2671 }
2672
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002673 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002674 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002675 return 1;
2676
Gleb Natapov6adba522010-10-14 11:22:55 +02002677 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002678 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002679 kvm_async_pf_wakeup_all(vcpu);
2680 return 0;
2681}
2682
Glauber Costa12f9a482011-02-01 14:16:40 -05002683static void kvmclock_reset(struct kvm_vcpu *vcpu)
2684{
Andy Honig0b794592013-02-20 14:48:10 -08002685 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002686 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002687}
2688
Sean Christopherson77809382020-03-20 14:28:18 -07002689static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002690{
2691 ++vcpu->stat.tlb_flush;
Sean Christopherson77809382020-03-20 14:28:18 -07002692 kvm_x86_ops.tlb_flush_all(vcpu);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002693}
2694
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002695static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
2696{
2697 ++vcpu->stat.tlb_flush;
2698 kvm_x86_ops.tlb_flush_guest(vcpu);
2699}
2700
Glauber Costac9aaa892011-07-11 15:28:14 -04002701static void record_steal_time(struct kvm_vcpu *vcpu)
2702{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002703 struct kvm_host_map map;
2704 struct kvm_steal_time *st;
2705
Glauber Costac9aaa892011-07-11 15:28:14 -04002706 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2707 return;
2708
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002709 /* -EAGAIN is returned in atomic context so we can just return. */
2710 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2711 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002712 return;
2713
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002714 st = map.hva +
2715 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2716
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002717 /*
2718 * Doing a TLB flush here, on the guest's behalf, can avoid
2719 * expensive IPIs.
2720 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002721 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002722 st->preempted & KVM_VCPU_FLUSH_TLB);
2723 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04002724 kvm_vcpu_flush_tlb_guest(vcpu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002725
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002726 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002727
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002728 if (st->version & 1)
2729 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002730
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002731 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002732
2733 smp_wmb();
2734
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002735 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002736 vcpu->arch.st.last_steal;
2737 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002738
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002739 smp_wmb();
2740
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002741 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002742
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002743 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002744}
2745
Will Auld8fe8ab42012-11-29 12:42:12 -08002746int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002747{
Gleb Natapov57537852012-01-15 14:17:22 +02002748 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002749 u32 msr = msr_info->index;
2750 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002751
Carsten Otte15c4a642007-10-30 18:44:17 +01002752 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002753 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002754 case MSR_IA32_UCODE_WRITE:
2755 case MSR_VM_HSAVE_PA:
2756 case MSR_AMD64_PATCH_LOADER:
2757 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002758 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002759 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002760 break;
2761
Wanpeng Li518e7b92018-02-28 14:03:31 +08002762 case MSR_IA32_UCODE_REV:
2763 if (msr_info->host_initiated)
2764 vcpu->arch.microcode_version = data;
2765 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002766 case MSR_IA32_ARCH_CAPABILITIES:
2767 if (!msr_info->host_initiated)
2768 return 1;
2769 vcpu->arch.arch_capabilities = data;
2770 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002771 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002772 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002773 case MSR_K7_HWCR:
2774 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002775 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002776 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002777
2778 /* Handle McStatusWrEn */
2779 if (data == BIT_ULL(18)) {
2780 vcpu->arch.msr_hwcr = data;
2781 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002782 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2783 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002784 return 1;
2785 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002786 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002787 case MSR_FAM10H_MMIO_CONF_BASE:
2788 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002789 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2790 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002791 return 1;
2792 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002793 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002794 case MSR_IA32_DEBUGCTLMSR:
2795 if (!data) {
2796 /* We support the non-activated case already */
2797 break;
2798 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2799 /* Values other than LBR and BTF are vendor-specific,
2800 thus reserved and should throw a #GP */
2801 return 1;
2802 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002803 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2804 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002805 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002806 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002807 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002808 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002809 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002810 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2811 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002812 case MSR_IA32_TSCDEADLINE:
2813 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2814 break;
Will Auldba904632012-11-29 12:42:50 -08002815 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002816 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002817 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002818 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002819 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002820 }
2821 vcpu->arch.ia32_tsc_adjust_msr = data;
2822 }
2823 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002824 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002825 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2826 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2827 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2828 return 1;
2829 vcpu->arch.ia32_misc_enable_msr = data;
2830 kvm_update_cpuid(vcpu);
2831 } else {
2832 vcpu->arch.ia32_misc_enable_msr = data;
2833 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002834 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002835 case MSR_IA32_SMBASE:
2836 if (!msr_info->host_initiated)
2837 return 1;
2838 vcpu->arch.smbase = data;
2839 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002840 case MSR_IA32_POWER_CTL:
2841 vcpu->arch.msr_ia32_power_ctl = data;
2842 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002843 case MSR_IA32_TSC:
2844 kvm_write_tsc(vcpu, msr_info);
2845 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002846 case MSR_IA32_XSS:
2847 if (!msr_info->host_initiated &&
2848 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2849 return 1;
2850 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002851 * KVM supports exposing PT to the guest, but does not support
2852 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2853 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002854 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002855 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002856 return 1;
2857 vcpu->arch.ia32_xss = data;
2858 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002859 case MSR_SMI_COUNT:
2860 if (!msr_info->host_initiated)
2861 return 1;
2862 vcpu->arch.smi_count = data;
2863 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002864 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002865 case MSR_KVM_WALL_CLOCK:
2866 vcpu->kvm->arch.wall_clock = data;
2867 kvm_write_wall_clock(vcpu->kvm, data);
2868 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002869 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002870 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002871 struct kvm_arch *ka = &vcpu->kvm->arch;
2872
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002873 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2874 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2875
2876 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002877 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002878
2879 ka->boot_vcpu_runs_old_kvmclock = tmp;
2880 }
2881
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002882 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002883 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002884
2885 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002886 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002887 if (!(data & 1))
2888 break;
2889
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002890 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002891 &vcpu->arch.pv_time, data & ~1ULL,
2892 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002893 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002894
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002895 break;
2896 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002897 case MSR_KVM_ASYNC_PF_EN:
2898 if (kvm_pv_enable_async_pf(vcpu, data))
2899 return 1;
2900 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002901 case MSR_KVM_STEAL_TIME:
2902
2903 if (unlikely(!sched_info_on()))
2904 return 1;
2905
2906 if (data & KVM_STEAL_RESERVED_MASK)
2907 return 1;
2908
Glauber Costac9aaa892011-07-11 15:28:14 -04002909 vcpu->arch.st.msr_val = data;
2910
2911 if (!(data & KVM_MSR_ENABLED))
2912 break;
2913
Glauber Costac9aaa892011-07-11 15:28:14 -04002914 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2915
2916 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002917 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002918 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002919 return 1;
2920 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002921
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002922 case MSR_KVM_POLL_CONTROL:
2923 /* only enable bit supported */
2924 if (data & (-1ULL << 1))
2925 return 1;
2926
2927 vcpu->arch.msr_kvm_poll_control = data;
2928 break;
2929
Huang Ying890ca9a2009-05-11 16:48:15 +08002930 case MSR_IA32_MCG_CTL:
2931 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002932 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002933 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002934
Wei Huang6912ac32015-06-12 01:34:56 -04002935 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2936 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2937 pr = true; /* fall through */
2938 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2939 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002940 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002941 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002942
2943 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002944 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2945 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002946 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002947 case MSR_K7_CLK_CTL:
2948 /*
2949 * Ignore all writes to this no longer documented MSR.
2950 * Writes are only relevant for old K7 processors,
2951 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002952 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002953 * affected processor models on the command line, hence
2954 * the need to ignore the workaround.
2955 */
2956 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002957 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002958 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2959 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002960 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002961 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2962 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2963 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002964 return kvm_hv_set_msr_common(vcpu, msr, data,
2965 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002966 case MSR_IA32_BBL_CR_CTL3:
2967 /* Drop writes to this legacy MSR -- see rdmsr
2968 * counterpart for further detail.
2969 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002970 if (report_ignored_msrs)
2971 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2972 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002973 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002974 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002975 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002976 return 1;
2977 vcpu->arch.osvw.length = data;
2978 break;
2979 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002980 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002981 return 1;
2982 vcpu->arch.osvw.status = data;
2983 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002984 case MSR_PLATFORM_INFO:
2985 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002986 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2987 cpuid_fault_enabled(vcpu)))
2988 return 1;
2989 vcpu->arch.msr_platform_info = data;
2990 break;
2991 case MSR_MISC_FEATURES_ENABLES:
2992 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2993 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2994 !supports_cpuid_fault(vcpu)))
2995 return 1;
2996 vcpu->arch.msr_misc_features_enables = data;
2997 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002998 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002999 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
3000 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02003001 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01003002 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02003003 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003004 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03003005 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003006 return 1;
3007 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003008 if (report_ignored_msrs)
3009 vcpu_unimpl(vcpu,
3010 "ignored wrmsr: 0x%x data 0x%llx\n",
3011 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003012 break;
3013 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003014 }
3015 return 0;
3016}
3017EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3018
Paolo Bonzini44883f02018-07-26 13:01:52 +02003019static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003020{
3021 u64 data;
3022 u64 mcg_cap = vcpu->arch.mcg_cap;
3023 unsigned bank_num = mcg_cap & 0xff;
3024
3025 switch (msr) {
3026 case MSR_IA32_P5_MC_ADDR:
3027 case MSR_IA32_P5_MC_TYPE:
3028 data = 0;
3029 break;
3030 case MSR_IA32_MCG_CAP:
3031 data = vcpu->arch.mcg_cap;
3032 break;
3033 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003034 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003035 return 1;
3036 data = vcpu->arch.mcg_ctl;
3037 break;
3038 case MSR_IA32_MCG_STATUS:
3039 data = vcpu->arch.mcg_status;
3040 break;
3041 default:
3042 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003043 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003044 u32 offset = array_index_nospec(
3045 msr - MSR_IA32_MC0_CTL,
3046 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3047
Huang Ying890ca9a2009-05-11 16:48:15 +08003048 data = vcpu->arch.mce_banks[offset];
3049 break;
3050 }
3051 return 1;
3052 }
3053 *pdata = data;
3054 return 0;
3055}
3056
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003057int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003058{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003059 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003060 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003061 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003062 case MSR_IA32_DEBUGCTLMSR:
3063 case MSR_IA32_LASTBRANCHFROMIP:
3064 case MSR_IA32_LASTBRANCHTOIP:
3065 case MSR_IA32_LASTINTFROMIP:
3066 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303067 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003068 case MSR_K8_TSEG_ADDR:
3069 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003070 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003071 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003072 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003073 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003074 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003075 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003076 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003077 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003078 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003079 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003080 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003081 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3082 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3083 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3084 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003085 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003086 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3087 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003088 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003089 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003090 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003091 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003092 case MSR_IA32_ARCH_CAPABILITIES:
3093 if (!msr_info->host_initiated &&
3094 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3095 return 1;
3096 msr_info->data = vcpu->arch.arch_capabilities;
3097 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003098 case MSR_IA32_POWER_CTL:
3099 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3100 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003101 case MSR_IA32_TSC:
3102 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3103 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003104 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003105 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003106 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003107 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003108 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003109 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003110 /*
3111 * MSR_EBC_FREQUENCY_ID
3112 * Conservative value valid for even the basic CPU models.
3113 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3114 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3115 * and 266MHz for model 3, or 4. Set Core Clock
3116 * Frequency to System Bus Frequency Ratio to 1 (bits
3117 * 31:24) even though these are only valid for CPU
3118 * models > 2, however guests may end up dividing or
3119 * multiplying by zero otherwise.
3120 */
3121 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003122 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003123 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003124 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003125 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003126 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003127 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003128 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003129 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003130 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003131 break;
Will Auldba904632012-11-29 12:42:50 -08003132 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003133 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003134 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003135 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003136 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003137 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003138 case MSR_IA32_SMBASE:
3139 if (!msr_info->host_initiated)
3140 return 1;
3141 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003142 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003143 case MSR_SMI_COUNT:
3144 msr_info->data = vcpu->arch.smi_count;
3145 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003146 case MSR_IA32_PERF_STATUS:
3147 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003148 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003149 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003150 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003151 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003152 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003153 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003154 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003155 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003156 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003157 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003158 break;
3159 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003160 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003161 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003162 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003163 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003164 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003165 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003166 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003167 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003168 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003169 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003170 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003171 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003172 case MSR_KVM_POLL_CONTROL:
3173 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3174 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003175 case MSR_IA32_P5_MC_ADDR:
3176 case MSR_IA32_P5_MC_TYPE:
3177 case MSR_IA32_MCG_CAP:
3178 case MSR_IA32_MCG_CTL:
3179 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003180 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003181 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3182 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003183 case MSR_IA32_XSS:
3184 if (!msr_info->host_initiated &&
3185 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3186 return 1;
3187 msr_info->data = vcpu->arch.ia32_xss;
3188 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003189 case MSR_K7_CLK_CTL:
3190 /*
3191 * Provide expected ramp-up count for K7. All other
3192 * are set to zero, indicating minimum divisors for
3193 * every field.
3194 *
3195 * This prevents guest kernels on AMD host with CPU
3196 * type 6, model 8 and higher from exploding due to
3197 * the rdmsr failing.
3198 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003199 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003200 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003201 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003202 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3203 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003204 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003205 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3206 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3207 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003208 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003209 msr_info->index, &msr_info->data,
3210 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003211 case MSR_IA32_BBL_CR_CTL3:
3212 /* This legacy MSR exists but isn't fully documented in current
3213 * silicon. It is however accessed by winxp in very narrow
3214 * scenarios where it sets bit #19, itself documented as
3215 * a "reserved" bit. Best effort attempt to source coherent
3216 * read data here should the balance of the register be
3217 * interpreted by the guest:
3218 *
3219 * L2 cache control register 3: 64GB range, 256KB size,
3220 * enabled, latency 0x1, configured
3221 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003222 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003223 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003224 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003225 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003226 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003227 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003228 break;
3229 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003230 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003231 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003232 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003233 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003234 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003235 if (!msr_info->host_initiated &&
3236 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3237 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003238 msr_info->data = vcpu->arch.msr_platform_info;
3239 break;
3240 case MSR_MISC_FEATURES_ENABLES:
3241 msr_info->data = vcpu->arch.msr_misc_features_enables;
3242 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003243 case MSR_K7_HWCR:
3244 msr_info->data = vcpu->arch.msr_hwcr;
3245 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003246 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003247 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003248 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003249 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003250 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3251 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003252 return 1;
3253 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003254 if (report_ignored_msrs)
3255 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3256 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003257 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003258 }
3259 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003260 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003261 return 0;
3262}
3263EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3264
Carsten Otte313a3dc2007-10-11 19:16:52 +02003265/*
3266 * Read or write a bunch of msrs. All parameters are kernel addresses.
3267 *
3268 * @return number of msrs set successfully.
3269 */
3270static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3271 struct kvm_msr_entry *entries,
3272 int (*do_msr)(struct kvm_vcpu *vcpu,
3273 unsigned index, u64 *data))
3274{
Tom Lendacky801e4592018-02-21 13:39:51 -06003275 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003276
Carsten Otte313a3dc2007-10-11 19:16:52 +02003277 for (i = 0; i < msrs->nmsrs; ++i)
3278 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3279 break;
3280
Carsten Otte313a3dc2007-10-11 19:16:52 +02003281 return i;
3282}
3283
3284/*
3285 * Read or write a bunch of msrs. Parameters are user addresses.
3286 *
3287 * @return number of msrs set successfully.
3288 */
3289static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3290 int (*do_msr)(struct kvm_vcpu *vcpu,
3291 unsigned index, u64 *data),
3292 int writeback)
3293{
3294 struct kvm_msrs msrs;
3295 struct kvm_msr_entry *entries;
3296 int r, n;
3297 unsigned size;
3298
3299 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003300 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003301 goto out;
3302
3303 r = -E2BIG;
3304 if (msrs.nmsrs >= MAX_IO_MSRS)
3305 goto out;
3306
Carsten Otte313a3dc2007-10-11 19:16:52 +02003307 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003308 entries = memdup_user(user_msrs->entries, size);
3309 if (IS_ERR(entries)) {
3310 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003311 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003312 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003313
3314 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3315 if (r < 0)
3316 goto out_free;
3317
3318 r = -EFAULT;
3319 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3320 goto out_free;
3321
3322 r = n;
3323
3324out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003325 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003326out:
3327 return r;
3328}
3329
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003330static inline bool kvm_can_mwait_in_guest(void)
3331{
3332 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003333 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3334 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003335}
3336
Alexander Graf784aa3d2014-07-14 18:27:35 +02003337int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003338{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003339 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003340
3341 switch (ext) {
3342 case KVM_CAP_IRQCHIP:
3343 case KVM_CAP_HLT:
3344 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003345 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003346 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003347 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003348 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003349 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003350 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003351 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003352 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003353 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003354 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003355 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003356 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003357 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003358 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003359 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003360 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003361 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003362 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003363 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003364 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003365 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003366 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003367 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003368 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003369 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003370 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003371 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003372 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003373 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003374 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003375 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003376 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003377 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003378 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003379 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003380 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003381 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003382 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003383 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003384 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003385 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003386 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003387 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003388 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003389 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003390 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003391 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003392 r = 1;
3393 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003394 case KVM_CAP_SYNC_REGS:
3395 r = KVM_SYNC_X86_VALID_FIELDS;
3396 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003397 case KVM_CAP_ADJUST_CLOCK:
3398 r = KVM_CLOCK_TSC_STABLE;
3399 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003400 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003401 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3402 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003403 if(kvm_can_mwait_in_guest())
3404 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003405 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003406 case KVM_CAP_X86_SMM:
3407 /* SMBASE is usually relocated above 1M on modern chipsets,
3408 * and SMM handlers might indeed rely on 4G segment limits,
3409 * so do not report SMM to be available if real mode is
3410 * emulated via vm86 mode. Still, do not go to great lengths
3411 * to avoid userspace's usage of the feature, because it is a
3412 * fringe case that is not enabled except via specific settings
3413 * of the module parameters.
3414 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003415 r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003416 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003417 case KVM_CAP_VAPIC:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003418 r = !kvm_x86_ops.cpu_has_accelerated_tpr();
Avi Kivity774ead32007-12-26 13:57:04 +02003419 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003420 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003421 r = KVM_SOFT_MAX_VCPUS;
3422 break;
3423 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003424 r = KVM_MAX_VCPUS;
3425 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003426 case KVM_CAP_MAX_VCPU_ID:
3427 r = KVM_MAX_VCPU_ID;
3428 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003429 case KVM_CAP_PV_MMU: /* obsolete */
3430 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003431 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003432 case KVM_CAP_MCE:
3433 r = KVM_MAX_MCE_BANKS;
3434 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003435 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003436 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003437 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003438 case KVM_CAP_TSC_CONTROL:
3439 r = kvm_has_tsc_control;
3440 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003441 case KVM_CAP_X2APIC_API:
3442 r = KVM_X2APIC_API_VALID_FLAGS;
3443 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003444 case KVM_CAP_NESTED_STATE:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003445 r = kvm_x86_ops.get_nested_state ?
3446 kvm_x86_ops.get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003447 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003448 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003449 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003450 break;
3451 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003452 r = kvm_x86_ops.nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003453 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003454 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003455 break;
3456 }
3457 return r;
3458
3459}
3460
Carsten Otte043405e2007-10-10 17:16:19 +02003461long kvm_arch_dev_ioctl(struct file *filp,
3462 unsigned int ioctl, unsigned long arg)
3463{
3464 void __user *argp = (void __user *)arg;
3465 long r;
3466
3467 switch (ioctl) {
3468 case KVM_GET_MSR_INDEX_LIST: {
3469 struct kvm_msr_list __user *user_msr_list = argp;
3470 struct kvm_msr_list msr_list;
3471 unsigned n;
3472
3473 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003474 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003475 goto out;
3476 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003477 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003478 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003479 goto out;
3480 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003481 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003482 goto out;
3483 r = -EFAULT;
3484 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3485 num_msrs_to_save * sizeof(u32)))
3486 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003487 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003488 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003489 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003490 goto out;
3491 r = 0;
3492 break;
3493 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003494 case KVM_GET_SUPPORTED_CPUID:
3495 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003496 struct kvm_cpuid2 __user *cpuid_arg = argp;
3497 struct kvm_cpuid2 cpuid;
3498
3499 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003500 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003501 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003502
3503 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3504 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003505 if (r)
3506 goto out;
3507
3508 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003509 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003510 goto out;
3511 r = 0;
3512 break;
3513 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003514 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003515 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003516 if (copy_to_user(argp, &kvm_mce_cap_supported,
3517 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003518 goto out;
3519 r = 0;
3520 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003521 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3522 struct kvm_msr_list __user *user_msr_list = argp;
3523 struct kvm_msr_list msr_list;
3524 unsigned int n;
3525
3526 r = -EFAULT;
3527 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3528 goto out;
3529 n = msr_list.nmsrs;
3530 msr_list.nmsrs = num_msr_based_features;
3531 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3532 goto out;
3533 r = -E2BIG;
3534 if (n < msr_list.nmsrs)
3535 goto out;
3536 r = -EFAULT;
3537 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3538 num_msr_based_features * sizeof(u32)))
3539 goto out;
3540 r = 0;
3541 break;
3542 }
3543 case KVM_GET_MSRS:
3544 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3545 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003546 default:
3547 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003548 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003549 }
3550out:
3551 return r;
3552}
3553
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003554static void wbinvd_ipi(void *garbage)
3555{
3556 wbinvd();
3557}
3558
3559static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3560{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003561 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003562}
3563
Carsten Otte313a3dc2007-10-11 19:16:52 +02003564void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3565{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003566 /* Address WBINVD may be executed by guest */
3567 if (need_emulate_wbinvd(vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003568 if (kvm_x86_ops.has_wbinvd_exit())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003569 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3570 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3571 smp_call_function_single(vcpu->cpu,
3572 wbinvd_ipi, NULL, 1);
3573 }
3574
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003575 kvm_x86_ops.vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003576
3577 /* Apply any externally detected TSC adjustments (due to suspend) */
3578 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3579 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3580 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003581 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003582 }
3583
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003584 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003585 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003586 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003587 if (tsc_delta < 0)
3588 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003589
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003590 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003591 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003592 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003593 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003594 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003595 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003596
3597 if (kvm_lapic_hv_timer_in_use(vcpu))
3598 kvm_lapic_restart_hv_timer(vcpu);
3599
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003600 /*
3601 * On a host with synchronized TSC, there is no need to update
3602 * kvmclock on vcpu->cpu migration
3603 */
3604 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003605 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003606 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003607 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003608 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003609 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003610
Glauber Costac9aaa892011-07-11 15:28:14 -04003611 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003612}
3613
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003614static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3615{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003616 struct kvm_host_map map;
3617 struct kvm_steal_time *st;
3618
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003619 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3620 return;
3621
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003622 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003623 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003624
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003625 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3626 &vcpu->arch.st.cache, true))
3627 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003628
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003629 st = map.hva +
3630 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3631
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003632 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003633
3634 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003635}
3636
Carsten Otte313a3dc2007-10-11 19:16:52 +02003637void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3638{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003639 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003640
3641 if (vcpu->preempted)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003642 vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003643
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003644 /*
3645 * Disable page faults because we're in atomic context here.
3646 * kvm_write_guest_offset_cached() would call might_fault()
3647 * that relies on pagefault_disable() to tell if there's a
3648 * bug. NOTE: the write to guest memory may not go through if
3649 * during postcopy live migration or if there's heavy guest
3650 * paging.
3651 */
3652 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003653 /*
3654 * kvm_memslots() will be called by
3655 * kvm_write_guest_offset_cached() so take the srcu lock.
3656 */
3657 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003658 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003659 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003660 pagefault_enable();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003661 kvm_x86_ops.vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003662 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003663 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003664 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3665 * on every vmexit, but if not, we might have a stale dr6 from the
3666 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003667 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003668 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003669}
3670
Carsten Otte313a3dc2007-10-11 19:16:52 +02003671static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3672 struct kvm_lapic_state *s)
3673{
Liran Alonfa59cc02017-12-24 18:12:53 +02003674 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003675 kvm_x86_ops.sync_pir_to_irr(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003676
Radim Krčmářa92e2542016-07-12 22:09:22 +02003677 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003678}
3679
3680static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3681 struct kvm_lapic_state *s)
3682{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003683 int r;
3684
3685 r = kvm_apic_set_state(vcpu, s);
3686 if (r)
3687 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003688 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003689
3690 return 0;
3691}
3692
Matt Gingell127a4572015-11-17 17:32:05 +01003693static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3694{
3695 return (!lapic_in_kernel(vcpu) ||
3696 kvm_apic_accept_pic_intr(vcpu));
3697}
3698
Matt Gingell782d4222015-11-16 15:26:00 -08003699/*
3700 * if userspace requested an interrupt window, check that the
3701 * interrupt window is open.
3702 *
3703 * No need to exit to userspace if we already have an interrupt queued.
3704 */
3705static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3706{
3707 return kvm_arch_interrupt_allowed(vcpu) &&
3708 !kvm_cpu_has_interrupt(vcpu) &&
3709 !kvm_event_needs_reinjection(vcpu) &&
3710 kvm_cpu_accept_dm_intr(vcpu);
3711}
3712
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003713static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3714 struct kvm_interrupt *irq)
3715{
Chen Gang02cdb502013-02-27 11:33:25 +08003716 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003717 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003718
3719 if (!irqchip_in_kernel(vcpu->kvm)) {
3720 kvm_queue_interrupt(vcpu, irq->irq, false);
3721 kvm_make_request(KVM_REQ_EVENT, vcpu);
3722 return 0;
3723 }
3724
3725 /*
3726 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3727 * fail for in-kernel 8259.
3728 */
3729 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003730 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003731
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003732 if (vcpu->arch.pending_external_vector != -1)
3733 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003734
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003735 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003736 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003737 return 0;
3738}
3739
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003740static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3741{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003742 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003743
3744 return 0;
3745}
3746
Paolo Bonzinif0778252015-04-01 15:06:40 +02003747static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3748{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003749 kvm_make_request(KVM_REQ_SMI, vcpu);
3750
Paolo Bonzinif0778252015-04-01 15:06:40 +02003751 return 0;
3752}
3753
Avi Kivityb209749f2007-10-22 16:50:39 +02003754static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3755 struct kvm_tpr_access_ctl *tac)
3756{
3757 if (tac->flags)
3758 return -EINVAL;
3759 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3760 return 0;
3761}
3762
Huang Ying890ca9a2009-05-11 16:48:15 +08003763static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3764 u64 mcg_cap)
3765{
3766 int r;
3767 unsigned bank_num = mcg_cap & 0xff, bank;
3768
3769 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003770 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003771 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003772 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003773 goto out;
3774 r = 0;
3775 vcpu->arch.mcg_cap = mcg_cap;
3776 /* Init IA32_MCG_CTL to all 1s */
3777 if (mcg_cap & MCG_CTL_P)
3778 vcpu->arch.mcg_ctl = ~(u64)0;
3779 /* Init IA32_MCi_CTL to all 1s */
3780 for (bank = 0; bank < bank_num; bank++)
3781 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003782
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003783 kvm_x86_ops.setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003784out:
3785 return r;
3786}
3787
3788static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3789 struct kvm_x86_mce *mce)
3790{
3791 u64 mcg_cap = vcpu->arch.mcg_cap;
3792 unsigned bank_num = mcg_cap & 0xff;
3793 u64 *banks = vcpu->arch.mce_banks;
3794
3795 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3796 return -EINVAL;
3797 /*
3798 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3799 * reporting is disabled
3800 */
3801 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3802 vcpu->arch.mcg_ctl != ~(u64)0)
3803 return 0;
3804 banks += 4 * mce->bank;
3805 /*
3806 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3807 * reporting is disabled for the bank
3808 */
3809 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3810 return 0;
3811 if (mce->status & MCI_STATUS_UC) {
3812 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003813 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003814 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003815 return 0;
3816 }
3817 if (banks[1] & MCI_STATUS_VAL)
3818 mce->status |= MCI_STATUS_OVER;
3819 banks[2] = mce->addr;
3820 banks[3] = mce->misc;
3821 vcpu->arch.mcg_status = mce->mcg_status;
3822 banks[1] = mce->status;
3823 kvm_queue_exception(vcpu, MC_VECTOR);
3824 } else if (!(banks[1] & MCI_STATUS_VAL)
3825 || !(banks[1] & MCI_STATUS_UC)) {
3826 if (banks[1] & MCI_STATUS_VAL)
3827 mce->status |= MCI_STATUS_OVER;
3828 banks[2] = mce->addr;
3829 banks[3] = mce->misc;
3830 banks[1] = mce->status;
3831 } else
3832 banks[1] |= MCI_STATUS_OVER;
3833 return 0;
3834}
3835
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003836static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3837 struct kvm_vcpu_events *events)
3838{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003839 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003840
Wanpeng Li664f8e22017-08-24 03:35:09 -07003841 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003842 * In guest mode, payload delivery should be deferred,
3843 * so that the L1 hypervisor can intercept #PF before
3844 * CR2 is modified (or intercept #DB before DR6 is
3845 * modified under nVMX). Unless the per-VM capability,
3846 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3847 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3848 * opportunistically defer the exception payload, deliver it if the
3849 * capability hasn't been requested before processing a
3850 * KVM_GET_VCPU_EVENTS.
3851 */
3852 if (!vcpu->kvm->arch.exception_payload_enabled &&
3853 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3854 kvm_deliver_exception_payload(vcpu);
3855
3856 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003857 * The API doesn't provide the instruction length for software
3858 * exceptions, so don't report them. As long as the guest RIP
3859 * isn't advanced, we should expect to encounter the exception
3860 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003861 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003862 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3863 events->exception.injected = 0;
3864 events->exception.pending = 0;
3865 } else {
3866 events->exception.injected = vcpu->arch.exception.injected;
3867 events->exception.pending = vcpu->arch.exception.pending;
3868 /*
3869 * For ABI compatibility, deliberately conflate
3870 * pending and injected exceptions when
3871 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3872 */
3873 if (!vcpu->kvm->arch.exception_payload_enabled)
3874 events->exception.injected |=
3875 vcpu->arch.exception.pending;
3876 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003877 events->exception.nr = vcpu->arch.exception.nr;
3878 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3879 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003880 events->exception_has_payload = vcpu->arch.exception.has_payload;
3881 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003882
Jan Kiszka03b82a32010-02-15 10:45:41 +01003883 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003884 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003885 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003886 events->interrupt.soft = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003887 events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003888
3889 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003890 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003891 events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003892 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003893
Jan Kiszka66450a22013-03-13 12:42:34 +01003894 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003895
Paolo Bonzinif0778252015-04-01 15:06:40 +02003896 events->smi.smm = is_smm(vcpu);
3897 events->smi.pending = vcpu->arch.smi_pending;
3898 events->smi.smm_inside_nmi =
3899 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3900 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3901
Jan Kiszkadab4b912009-12-06 18:24:15 +01003902 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003903 | KVM_VCPUEVENT_VALID_SHADOW
3904 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003905 if (vcpu->kvm->arch.exception_payload_enabled)
3906 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3907
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003908 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003909}
3910
Sean Christophersonc5833c72019-04-02 08:03:10 -07003911static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003912
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003913static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3914 struct kvm_vcpu_events *events)
3915{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003916 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003917 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003918 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003919 | KVM_VCPUEVENT_VALID_SMM
3920 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003921 return -EINVAL;
3922
Jim Mattson59073aa2018-10-16 14:29:20 -07003923 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3924 if (!vcpu->kvm->arch.exception_payload_enabled)
3925 return -EINVAL;
3926 if (events->exception.pending)
3927 events->exception.injected = 0;
3928 else
3929 events->exception_has_payload = 0;
3930 } else {
3931 events->exception.pending = 0;
3932 events->exception_has_payload = 0;
3933 }
3934
3935 if ((events->exception.injected || events->exception.pending) &&
3936 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003937 return -EINVAL;
3938
David Hildenbrand28bf2882017-03-23 11:46:03 +01003939 /* INITs are latched while in SMM */
3940 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3941 (events->smi.smm || events->smi.pending) &&
3942 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3943 return -EINVAL;
3944
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003945 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003946 vcpu->arch.exception.injected = events->exception.injected;
3947 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003948 vcpu->arch.exception.nr = events->exception.nr;
3949 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3950 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003951 vcpu->arch.exception.has_payload = events->exception_has_payload;
3952 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003953
Liran Alon04140b42018-03-23 03:01:31 +03003954 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003955 vcpu->arch.interrupt.nr = events->interrupt.nr;
3956 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003957 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003958 kvm_x86_ops.set_interrupt_shadow(vcpu,
Jan Kiszka48005f62010-02-19 19:38:07 +01003959 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003960
3961 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003962 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3963 vcpu->arch.nmi_pending = events->nmi.pending;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003964 kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003965
Jan Kiszka66450a22013-03-13 12:42:34 +01003966 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003967 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003968 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003969
Paolo Bonzinif0778252015-04-01 15:06:40 +02003970 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003971 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3972 if (events->smi.smm)
3973 vcpu->arch.hflags |= HF_SMM_MASK;
3974 else
3975 vcpu->arch.hflags &= ~HF_SMM_MASK;
3976 kvm_smm_changed(vcpu);
3977 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003978
Paolo Bonzinif0778252015-04-01 15:06:40 +02003979 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003980
3981 if (events->smi.smm) {
3982 if (events->smi.smm_inside_nmi)
3983 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003984 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003985 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003986 }
3987
3988 if (lapic_in_kernel(vcpu)) {
3989 if (events->smi.latched_init)
3990 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3991 else
3992 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003993 }
3994 }
3995
Avi Kivity3842d132010-07-27 12:30:24 +03003996 kvm_make_request(KVM_REQ_EVENT, vcpu);
3997
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003998 return 0;
3999}
4000
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004001static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
4002 struct kvm_debugregs *dbgregs)
4003{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004004 unsigned long val;
4005
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004006 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004007 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004008 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004009 dbgregs->dr7 = vcpu->arch.dr7;
4010 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004011 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004012}
4013
4014static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4015 struct kvm_debugregs *dbgregs)
4016{
4017 if (dbgregs->flags)
4018 return -EINVAL;
4019
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004020 if (dbgregs->dr6 & ~0xffffffffull)
4021 return -EINVAL;
4022 if (dbgregs->dr7 & ~0xffffffffull)
4023 return -EINVAL;
4024
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004025 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004026 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004027 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004028 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004029 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004030 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004031
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004032 return 0;
4033}
4034
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004035#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4036
4037static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4038{
Marc Orrb666a4b2018-11-06 14:53:56 -08004039 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004040 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004041 u64 valid;
4042
4043 /*
4044 * Copy legacy XSAVE area, to avoid complications with CPUID
4045 * leaves 0 and 1 in the loop below.
4046 */
4047 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4048
4049 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004050 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004051 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4052
4053 /*
4054 * Copy each region from the possibly compacted offset to the
4055 * non-compacted offset.
4056 */
Dave Hansend91cab72015-09-02 16:31:26 -07004057 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004058 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004059 u64 xfeature_mask = valid & -valid;
4060 int xfeature_nr = fls64(xfeature_mask) - 1;
4061 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004062
4063 if (src) {
4064 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004065 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004066 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004067 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004068 memcpy(dest + offset, &vcpu->arch.pkru,
4069 sizeof(vcpu->arch.pkru));
4070 else
4071 memcpy(dest + offset, src, size);
4072
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004073 }
4074
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004075 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004076 }
4077}
4078
4079static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4080{
Marc Orrb666a4b2018-11-06 14:53:56 -08004081 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004082 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4083 u64 valid;
4084
4085 /*
4086 * Copy legacy XSAVE area, to avoid complications with CPUID
4087 * leaves 0 and 1 in the loop below.
4088 */
4089 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4090
4091 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004092 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004093 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004094 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004095
4096 /*
4097 * Copy each region from the non-compacted offset to the
4098 * possibly compacted offset.
4099 */
Dave Hansend91cab72015-09-02 16:31:26 -07004100 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004101 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004102 u64 xfeature_mask = valid & -valid;
4103 int xfeature_nr = fls64(xfeature_mask) - 1;
4104 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004105
4106 if (dest) {
4107 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004108 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004109 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004110 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004111 memcpy(&vcpu->arch.pkru, src + offset,
4112 sizeof(vcpu->arch.pkru));
4113 else
4114 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004115 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004116
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004117 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004118 }
4119}
4120
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004121static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4122 struct kvm_xsave *guest_xsave)
4123{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004124 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004125 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4126 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004127 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004128 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004129 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004130 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004131 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004132 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004133 }
4134}
4135
Wanpeng Lia5758132017-05-11 02:58:55 -07004136#define XSAVE_MXCSR_OFFSET 24
4137
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004138static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4139 struct kvm_xsave *guest_xsave)
4140{
4141 u64 xstate_bv =
4142 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004143 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004144
Borislav Petkovd366bf72016-04-04 22:25:02 +02004145 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004146 /*
4147 * Here we allow setting states that are not present in
4148 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4149 * with old userspace.
4150 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004151 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004152 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004153 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004154 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004155 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4156 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004157 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004158 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004159 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004160 }
4161 return 0;
4162}
4163
4164static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4165 struct kvm_xcrs *guest_xcrs)
4166{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004167 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004168 guest_xcrs->nr_xcrs = 0;
4169 return;
4170 }
4171
4172 guest_xcrs->nr_xcrs = 1;
4173 guest_xcrs->flags = 0;
4174 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4175 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4176}
4177
4178static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4179 struct kvm_xcrs *guest_xcrs)
4180{
4181 int i, r = 0;
4182
Borislav Petkovd366bf72016-04-04 22:25:02 +02004183 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004184 return -EINVAL;
4185
4186 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4187 return -EINVAL;
4188
4189 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4190 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004191 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004192 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004193 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004194 break;
4195 }
4196 if (r)
4197 r = -EINVAL;
4198 return r;
4199}
4200
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004201/*
4202 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4203 * stopped by the hypervisor. This function will be called from the host only.
4204 * EINVAL is returned when the host attempts to set the flag for a guest that
4205 * does not support pv clocks.
4206 */
4207static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4208{
Andy Honig0b794592013-02-20 14:48:10 -08004209 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004210 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004211 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004212 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4213 return 0;
4214}
4215
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004216static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4217 struct kvm_enable_cap *cap)
4218{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004219 int r;
4220 uint16_t vmcs_version;
4221 void __user *user_ptr;
4222
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004223 if (cap->flags)
4224 return -EINVAL;
4225
4226 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004227 case KVM_CAP_HYPERV_SYNIC2:
4228 if (cap->args[0])
4229 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004230 /* fall through */
4231
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004232 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004233 if (!irqchip_in_kernel(vcpu->kvm))
4234 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004235 return kvm_hv_activate_synic(vcpu, cap->cap ==
4236 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004237 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004238 if (!kvm_x86_ops.nested_enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004239 return -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004240 r = kvm_x86_ops.nested_enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004241 if (!r) {
4242 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4243 if (copy_to_user(user_ptr, &vmcs_version,
4244 sizeof(vmcs_version)))
4245 r = -EFAULT;
4246 }
4247 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004248 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004249 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004250 return -ENOTTY;
4251
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004252 return kvm_x86_ops.enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004253
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004254 default:
4255 return -EINVAL;
4256 }
4257}
4258
Carsten Otte313a3dc2007-10-11 19:16:52 +02004259long kvm_arch_vcpu_ioctl(struct file *filp,
4260 unsigned int ioctl, unsigned long arg)
4261{
4262 struct kvm_vcpu *vcpu = filp->private_data;
4263 void __user *argp = (void __user *)arg;
4264 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004265 union {
4266 struct kvm_lapic_state *lapic;
4267 struct kvm_xsave *xsave;
4268 struct kvm_xcrs *xcrs;
4269 void *buffer;
4270 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004271
Christoffer Dall9b0624712017-12-04 21:35:36 +01004272 vcpu_load(vcpu);
4273
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004274 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004275 switch (ioctl) {
4276 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004277 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004278 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004279 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004280 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4281 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004282
Dave Hansenb772ff32008-08-11 10:01:47 -07004283 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004284 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004285 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004286 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004287 if (r)
4288 goto out;
4289 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004290 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004291 goto out;
4292 r = 0;
4293 break;
4294 }
4295 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004296 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004297 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004298 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004299 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004300 if (IS_ERR(u.lapic)) {
4301 r = PTR_ERR(u.lapic);
4302 goto out_nofree;
4303 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004304
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004305 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004306 break;
4307 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004308 case KVM_INTERRUPT: {
4309 struct kvm_interrupt irq;
4310
4311 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004312 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004313 goto out;
4314 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004315 break;
4316 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004317 case KVM_NMI: {
4318 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004319 break;
4320 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004321 case KVM_SMI: {
4322 r = kvm_vcpu_ioctl_smi(vcpu);
4323 break;
4324 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004325 case KVM_SET_CPUID: {
4326 struct kvm_cpuid __user *cpuid_arg = argp;
4327 struct kvm_cpuid cpuid;
4328
4329 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004330 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004331 goto out;
4332 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004333 break;
4334 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004335 case KVM_SET_CPUID2: {
4336 struct kvm_cpuid2 __user *cpuid_arg = argp;
4337 struct kvm_cpuid2 cpuid;
4338
4339 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004340 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004341 goto out;
4342 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004343 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004344 break;
4345 }
4346 case KVM_GET_CPUID2: {
4347 struct kvm_cpuid2 __user *cpuid_arg = argp;
4348 struct kvm_cpuid2 cpuid;
4349
4350 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004351 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004352 goto out;
4353 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004354 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004355 if (r)
4356 goto out;
4357 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004358 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004359 goto out;
4360 r = 0;
4361 break;
4362 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004363 case KVM_GET_MSRS: {
4364 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004365 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004366 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004367 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004368 }
4369 case KVM_SET_MSRS: {
4370 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004371 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004372 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004373 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004374 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004375 case KVM_TPR_ACCESS_REPORTING: {
4376 struct kvm_tpr_access_ctl tac;
4377
4378 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004379 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004380 goto out;
4381 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4382 if (r)
4383 goto out;
4384 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004385 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004386 goto out;
4387 r = 0;
4388 break;
4389 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004390 case KVM_SET_VAPIC_ADDR: {
4391 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004392 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004393
4394 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004395 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004396 goto out;
4397 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004398 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004399 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004400 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004401 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004402 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004403 break;
4404 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004405 case KVM_X86_SETUP_MCE: {
4406 u64 mcg_cap;
4407
4408 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004409 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004410 goto out;
4411 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4412 break;
4413 }
4414 case KVM_X86_SET_MCE: {
4415 struct kvm_x86_mce mce;
4416
4417 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004418 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004419 goto out;
4420 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4421 break;
4422 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004423 case KVM_GET_VCPU_EVENTS: {
4424 struct kvm_vcpu_events events;
4425
4426 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4427
4428 r = -EFAULT;
4429 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4430 break;
4431 r = 0;
4432 break;
4433 }
4434 case KVM_SET_VCPU_EVENTS: {
4435 struct kvm_vcpu_events events;
4436
4437 r = -EFAULT;
4438 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4439 break;
4440
4441 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4442 break;
4443 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004444 case KVM_GET_DEBUGREGS: {
4445 struct kvm_debugregs dbgregs;
4446
4447 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4448
4449 r = -EFAULT;
4450 if (copy_to_user(argp, &dbgregs,
4451 sizeof(struct kvm_debugregs)))
4452 break;
4453 r = 0;
4454 break;
4455 }
4456 case KVM_SET_DEBUGREGS: {
4457 struct kvm_debugregs dbgregs;
4458
4459 r = -EFAULT;
4460 if (copy_from_user(&dbgregs, argp,
4461 sizeof(struct kvm_debugregs)))
4462 break;
4463
4464 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4465 break;
4466 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004467 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004468 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004469 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004470 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004471 break;
4472
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004473 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004474
4475 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004476 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004477 break;
4478 r = 0;
4479 break;
4480 }
4481 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004482 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004483 if (IS_ERR(u.xsave)) {
4484 r = PTR_ERR(u.xsave);
4485 goto out_nofree;
4486 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004487
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004488 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004489 break;
4490 }
4491 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004492 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004493 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004494 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004495 break;
4496
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004497 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004498
4499 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004500 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004501 sizeof(struct kvm_xcrs)))
4502 break;
4503 r = 0;
4504 break;
4505 }
4506 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004507 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004508 if (IS_ERR(u.xcrs)) {
4509 r = PTR_ERR(u.xcrs);
4510 goto out_nofree;
4511 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004512
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004513 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004514 break;
4515 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004516 case KVM_SET_TSC_KHZ: {
4517 u32 user_tsc_khz;
4518
4519 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004520 user_tsc_khz = (u32)arg;
4521
4522 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4523 goto out;
4524
Zachary Amsdencc578282012-02-03 15:43:50 -02004525 if (user_tsc_khz == 0)
4526 user_tsc_khz = tsc_khz;
4527
Haozhong Zhang381d5852015-10-20 15:39:04 +08004528 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4529 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004530
Joerg Roedel92a1f122011-03-25 09:44:51 +01004531 goto out;
4532 }
4533 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004534 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004535 goto out;
4536 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004537 case KVM_KVMCLOCK_CTRL: {
4538 r = kvm_set_guest_paused(vcpu);
4539 goto out;
4540 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004541 case KVM_ENABLE_CAP: {
4542 struct kvm_enable_cap cap;
4543
4544 r = -EFAULT;
4545 if (copy_from_user(&cap, argp, sizeof(cap)))
4546 goto out;
4547 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4548 break;
4549 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004550 case KVM_GET_NESTED_STATE: {
4551 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4552 u32 user_data_size;
4553
4554 r = -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004555 if (!kvm_x86_ops.get_nested_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004556 break;
4557
4558 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004559 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004560 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004561 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004562
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004563 r = kvm_x86_ops.get_nested_state(vcpu, user_kvm_nested_state,
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004564 user_data_size);
4565 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004566 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004567
4568 if (r > user_data_size) {
4569 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004570 r = -EFAULT;
4571 else
4572 r = -E2BIG;
4573 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004574 }
Liran Alon26b471c2018-09-16 14:28:20 +03004575
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004576 r = 0;
4577 break;
4578 }
4579 case KVM_SET_NESTED_STATE: {
4580 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4581 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004582 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004583
4584 r = -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004585 if (!kvm_x86_ops.set_nested_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004586 break;
4587
Liran Alon26b471c2018-09-16 14:28:20 +03004588 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004589 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004590 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004591
Liran Alon26b471c2018-09-16 14:28:20 +03004592 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004593 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004594 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004595
4596 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004597 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4598 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004599 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004600
4601 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004602 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4603 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004604 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004605
Sean Christophersonad5996d2019-11-22 08:58:18 -08004606 idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004607 r = kvm_x86_ops.set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004608 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004609 break;
4610 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004611 case KVM_GET_SUPPORTED_HV_CPUID: {
4612 struct kvm_cpuid2 __user *cpuid_arg = argp;
4613 struct kvm_cpuid2 cpuid;
4614
4615 r = -EFAULT;
4616 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4617 goto out;
4618
4619 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4620 cpuid_arg->entries);
4621 if (r)
4622 goto out;
4623
4624 r = -EFAULT;
4625 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4626 goto out;
4627 r = 0;
4628 break;
4629 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004630 default:
4631 r = -EINVAL;
4632 }
4633out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004634 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004635out_nofree:
4636 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004637 return r;
4638}
4639
Souptick Joarder1499fa82018-04-19 00:49:58 +05304640vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004641{
4642 return VM_FAULT_SIGBUS;
4643}
4644
Carsten Otte1fe779f2007-10-29 16:08:35 +01004645static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4646{
4647 int ret;
4648
4649 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004650 return -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004651 ret = kvm_x86_ops.set_tss_addr(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004652 return ret;
4653}
4654
Sheng Yangb927a3c2009-07-21 10:42:48 +08004655static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4656 u64 ident_addr)
4657{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004658 return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004659}
4660
Carsten Otte1fe779f2007-10-29 16:08:35 +01004661static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004662 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004663{
4664 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4665 return -EINVAL;
4666
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004667 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004668
4669 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004670 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004671
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004672 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004673 return 0;
4674}
4675
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004676static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004677{
Dave Hansen39de71e2010-08-19 18:11:14 -07004678 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004679}
4680
Carsten Otte1fe779f2007-10-29 16:08:35 +01004681static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4682{
David Hildenbrand90bca052017-04-07 10:50:23 +02004683 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004684 int r;
4685
4686 r = 0;
4687 switch (chip->chip_id) {
4688 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004689 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004690 sizeof(struct kvm_pic_state));
4691 break;
4692 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004693 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004694 sizeof(struct kvm_pic_state));
4695 break;
4696 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004697 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004698 break;
4699 default:
4700 r = -EINVAL;
4701 break;
4702 }
4703 return r;
4704}
4705
4706static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4707{
David Hildenbrand90bca052017-04-07 10:50:23 +02004708 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004709 int r;
4710
4711 r = 0;
4712 switch (chip->chip_id) {
4713 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004714 spin_lock(&pic->lock);
4715 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004716 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004717 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004718 break;
4719 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004720 spin_lock(&pic->lock);
4721 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004722 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004723 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004724 break;
4725 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004726 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004727 break;
4728 default:
4729 r = -EINVAL;
4730 break;
4731 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004732 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004733 return r;
4734}
4735
Sheng Yange0f63cb2008-03-04 00:50:59 +08004736static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4737{
Radim Krčmář34f39412016-03-02 22:56:50 +01004738 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4739
4740 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4741
4742 mutex_lock(&kps->lock);
4743 memcpy(ps, &kps->channels, sizeof(*ps));
4744 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304745 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004746}
4747
4748static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4749{
Andrew Honig01856042015-11-18 14:50:23 -08004750 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004751 struct kvm_pit *pit = kvm->arch.vpit;
4752
4753 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004754 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004755 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004756 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4757 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304758 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004759}
4760
4761static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4762{
Beth Kone9f42752009-07-07 11:50:38 -04004763 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4764 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4765 sizeof(ps->channels));
4766 ps->flags = kvm->arch.vpit->pit_state.flags;
4767 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004768 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304769 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004770}
4771
4772static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4773{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304774 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004775 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004776 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004777 struct kvm_pit *pit = kvm->arch.vpit;
4778
4779 mutex_lock(&pit->pit_state.lock);
4780 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004781 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4782 if (!prev_legacy && cur_legacy)
4783 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004784 memcpy(&pit->pit_state.channels, &ps->channels,
4785 sizeof(pit->pit_state.channels));
4786 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004787 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004788 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004789 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004790 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304791 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004792}
4793
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004794static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4795 struct kvm_reinject_control *control)
4796{
Radim Krčmář71474e22016-03-02 22:56:45 +01004797 struct kvm_pit *pit = kvm->arch.vpit;
4798
Radim Krčmář71474e22016-03-02 22:56:45 +01004799 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4800 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4801 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4802 */
4803 mutex_lock(&pit->pit_state.lock);
4804 kvm_pit_set_reinject(pit, control->pit_reinject);
4805 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004806
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004807 return 0;
4808}
4809
Sean Christopherson0dff0842020-02-18 13:07:29 -08004810void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004811{
Kai Huang88178fd2015-01-28 10:54:27 +08004812 /*
4813 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4814 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004815 if (kvm_x86_ops.flush_log_dirty)
4816 kvm_x86_ops.flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004817}
4818
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004819int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4820 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004821{
4822 if (!irqchip_in_kernel(kvm))
4823 return -ENXIO;
4824
4825 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004826 irq_event->irq, irq_event->level,
4827 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004828 return 0;
4829}
4830
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004831int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4832 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004833{
4834 int r;
4835
4836 if (cap->flags)
4837 return -EINVAL;
4838
4839 switch (cap->cap) {
4840 case KVM_CAP_DISABLE_QUIRKS:
4841 kvm->arch.disabled_quirks = cap->args[0];
4842 r = 0;
4843 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004844 case KVM_CAP_SPLIT_IRQCHIP: {
4845 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004846 r = -EINVAL;
4847 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4848 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004849 r = -EEXIST;
4850 if (irqchip_in_kernel(kvm))
4851 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004852 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004853 goto split_irqchip_unlock;
4854 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004855 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004856 goto split_irqchip_unlock;
4857 /* Pairs with irqchip_in_kernel. */
4858 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004859 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004860 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004861 r = 0;
4862split_irqchip_unlock:
4863 mutex_unlock(&kvm->lock);
4864 break;
4865 }
Radim Krčmář371313132016-07-12 22:09:27 +02004866 case KVM_CAP_X2APIC_API:
4867 r = -EINVAL;
4868 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4869 break;
4870
4871 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4872 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004873 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4874 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004875
4876 r = 0;
4877 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004878 case KVM_CAP_X86_DISABLE_EXITS:
4879 r = -EINVAL;
4880 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4881 break;
4882
4883 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4884 kvm_can_mwait_in_guest())
4885 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004886 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004887 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004888 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4889 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004890 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4891 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004892 r = 0;
4893 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004894 case KVM_CAP_MSR_PLATFORM_INFO:
4895 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4896 r = 0;
4897 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004898 case KVM_CAP_EXCEPTION_PAYLOAD:
4899 kvm->arch.exception_payload_enabled = cap->args[0];
4900 r = 0;
4901 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004902 default:
4903 r = -EINVAL;
4904 break;
4905 }
4906 return r;
4907}
4908
Carsten Otte1fe779f2007-10-29 16:08:35 +01004909long kvm_arch_vm_ioctl(struct file *filp,
4910 unsigned int ioctl, unsigned long arg)
4911{
4912 struct kvm *kvm = filp->private_data;
4913 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004914 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004915 /*
4916 * This union makes it completely explicit to gcc-3.x
4917 * that these two variables' stack usage should be
4918 * combined, not added together.
4919 */
4920 union {
4921 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004922 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004923 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004924 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004925
4926 switch (ioctl) {
4927 case KVM_SET_TSS_ADDR:
4928 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004929 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004930 case KVM_SET_IDENTITY_MAP_ADDR: {
4931 u64 ident_addr;
4932
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004933 mutex_lock(&kvm->lock);
4934 r = -EINVAL;
4935 if (kvm->created_vcpus)
4936 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004937 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004938 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004939 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004940 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004941set_identity_unlock:
4942 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004943 break;
4944 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004945 case KVM_SET_NR_MMU_PAGES:
4946 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004947 break;
4948 case KVM_GET_NR_MMU_PAGES:
4949 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4950 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004951 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004952 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004953
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004954 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004955 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004956 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004957
Avi Kivity3e515702012-03-05 14:23:29 +02004958 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004959 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004960 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004961
4962 r = kvm_pic_init(kvm);
4963 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004964 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004965
4966 r = kvm_ioapic_init(kvm);
4967 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004968 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004969 goto create_irqchip_unlock;
4970 }
4971
Avi Kivity399ec802008-11-19 13:58:46 +02004972 r = kvm_setup_default_irq_routing(kvm);
4973 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004974 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004975 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004976 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004977 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004978 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004979 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004980 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004981 create_irqchip_unlock:
4982 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004983 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004984 }
Sheng Yang78376992008-01-28 05:10:22 +08004985 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004986 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4987 goto create_pit;
4988 case KVM_CREATE_PIT2:
4989 r = -EFAULT;
4990 if (copy_from_user(&u.pit_config, argp,
4991 sizeof(struct kvm_pit_config)))
4992 goto out;
4993 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004994 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004995 r = -EEXIST;
4996 if (kvm->arch.vpit)
4997 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004998 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004999 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08005000 if (kvm->arch.vpit)
5001 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02005002 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02005003 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08005004 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005005 case KVM_GET_IRQCHIP: {
5006 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005007 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005008
Sasha Levinff5c2c02011-12-04 19:36:29 +02005009 chip = memdup_user(argp, sizeof(*chip));
5010 if (IS_ERR(chip)) {
5011 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005012 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005013 }
5014
Carsten Otte1fe779f2007-10-29 16:08:35 +01005015 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005016 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005017 goto get_irqchip_out;
5018 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5019 if (r)
5020 goto get_irqchip_out;
5021 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005022 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005023 goto get_irqchip_out;
5024 r = 0;
5025 get_irqchip_out:
5026 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005027 break;
5028 }
5029 case KVM_SET_IRQCHIP: {
5030 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005031 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005032
Sasha Levinff5c2c02011-12-04 19:36:29 +02005033 chip = memdup_user(argp, sizeof(*chip));
5034 if (IS_ERR(chip)) {
5035 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005036 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005037 }
5038
Carsten Otte1fe779f2007-10-29 16:08:35 +01005039 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005040 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005041 goto set_irqchip_out;
5042 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005043 set_irqchip_out:
5044 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005045 break;
5046 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005047 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005048 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005049 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005050 goto out;
5051 r = -ENXIO;
5052 if (!kvm->arch.vpit)
5053 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005054 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005055 if (r)
5056 goto out;
5057 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005058 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005059 goto out;
5060 r = 0;
5061 break;
5062 }
5063 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005064 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005065 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005066 goto out;
5067 r = -ENXIO;
5068 if (!kvm->arch.vpit)
5069 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005070 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005071 break;
5072 }
Beth Kone9f42752009-07-07 11:50:38 -04005073 case KVM_GET_PIT2: {
5074 r = -ENXIO;
5075 if (!kvm->arch.vpit)
5076 goto out;
5077 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5078 if (r)
5079 goto out;
5080 r = -EFAULT;
5081 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5082 goto out;
5083 r = 0;
5084 break;
5085 }
5086 case KVM_SET_PIT2: {
5087 r = -EFAULT;
5088 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5089 goto out;
5090 r = -ENXIO;
5091 if (!kvm->arch.vpit)
5092 goto out;
5093 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005094 break;
5095 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005096 case KVM_REINJECT_CONTROL: {
5097 struct kvm_reinject_control control;
5098 r = -EFAULT;
5099 if (copy_from_user(&control, argp, sizeof(control)))
5100 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005101 r = -ENXIO;
5102 if (!kvm->arch.vpit)
5103 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005104 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005105 break;
5106 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005107 case KVM_SET_BOOT_CPU_ID:
5108 r = 0;
5109 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005110 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005111 r = -EBUSY;
5112 else
5113 kvm->arch.bsp_vcpu_id = arg;
5114 mutex_unlock(&kvm->lock);
5115 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005116 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005117 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005118 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005119 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005120 goto out;
5121 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005122 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005123 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005124 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005125 r = 0;
5126 break;
5127 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005128 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005129 struct kvm_clock_data user_ns;
5130 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005131
5132 r = -EFAULT;
5133 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5134 goto out;
5135
5136 r = -EINVAL;
5137 if (user_ns.flags)
5138 goto out;
5139
5140 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005141 /*
5142 * TODO: userspace has to take care of races with VCPU_RUN, so
5143 * kvm_gen_update_masterclock() can be cut down to locked
5144 * pvclock_update_vm_gtod_copy().
5145 */
5146 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005147 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005148 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005149 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005150 break;
5151 }
5152 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005153 struct kvm_clock_data user_ns;
5154 u64 now_ns;
5155
Marcelo Tosattie891a322017-04-17 12:51:37 -03005156 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005157 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005158 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005159 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005160
5161 r = -EFAULT;
5162 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5163 goto out;
5164 r = 0;
5165 break;
5166 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005167 case KVM_MEMORY_ENCRYPT_OP: {
5168 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005169 if (kvm_x86_ops.mem_enc_op)
5170 r = kvm_x86_ops.mem_enc_op(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005171 break;
5172 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005173 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5174 struct kvm_enc_region region;
5175
5176 r = -EFAULT;
5177 if (copy_from_user(&region, argp, sizeof(region)))
5178 goto out;
5179
5180 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005181 if (kvm_x86_ops.mem_enc_reg_region)
5182 r = kvm_x86_ops.mem_enc_reg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005183 break;
5184 }
5185 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5186 struct kvm_enc_region region;
5187
5188 r = -EFAULT;
5189 if (copy_from_user(&region, argp, sizeof(region)))
5190 goto out;
5191
5192 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005193 if (kvm_x86_ops.mem_enc_unreg_region)
5194 r = kvm_x86_ops.mem_enc_unreg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005195 break;
5196 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005197 case KVM_HYPERV_EVENTFD: {
5198 struct kvm_hyperv_eventfd hvevfd;
5199
5200 r = -EFAULT;
5201 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5202 goto out;
5203 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5204 break;
5205 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005206 case KVM_SET_PMU_EVENT_FILTER:
5207 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5208 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005209 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005210 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005211 }
5212out:
5213 return r;
5214}
5215
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005216static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005217{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005218 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005219 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005220 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005221
Jim Mattsone2ada662019-08-21 11:20:04 -07005222 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005223 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005224
5225 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005226
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005227 num_msrs_to_save = 0;
5228 num_emulated_msrs = 0;
5229 num_msr_based_features = 0;
5230
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005231 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5232 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005233 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005234
5235 /*
5236 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005237 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005238 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005239 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005240 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005241 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005242 continue;
5243 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005244 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005245 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005246 continue;
5247 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005248 case MSR_IA32_RTIT_CTL:
5249 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005250 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005251 continue;
5252 break;
5253 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005254 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005255 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5256 continue;
5257 break;
5258 case MSR_IA32_RTIT_OUTPUT_BASE:
5259 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005260 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005261 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5262 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5263 continue;
5264 break;
5265 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
Sean Christopherson7b874c22020-03-02 15:56:59 -08005266 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005267 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005268 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5269 continue;
5270 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005271 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005272 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005273 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5274 continue;
5275 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005276 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005277 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005278 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5279 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005280 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005281 default:
5282 break;
5283 }
5284
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005285 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005286 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005287
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005288 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005289 if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005290 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005291
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005292 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005293 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005294
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005295 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005296 struct kvm_msr_entry msr;
5297
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005298 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005299 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005300 continue;
5301
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005302 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005303 }
Carsten Otte043405e2007-10-10 17:16:19 +02005304}
5305
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005306static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5307 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005308{
Avi Kivity70252a12010-01-19 12:51:22 +02005309 int handled = 0;
5310 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005311
Avi Kivity70252a12010-01-19 12:51:22 +02005312 do {
5313 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005314 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005315 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5316 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005317 break;
5318 handled += n;
5319 addr += n;
5320 len -= n;
5321 v += n;
5322 } while (len);
5323
5324 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005325}
5326
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005327static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005328{
Avi Kivity70252a12010-01-19 12:51:22 +02005329 int handled = 0;
5330 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005331
Avi Kivity70252a12010-01-19 12:51:22 +02005332 do {
5333 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005334 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005335 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5336 addr, n, v))
5337 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005338 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005339 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005340 handled += n;
5341 addr += n;
5342 len -= n;
5343 v += n;
5344 } while (len);
5345
5346 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005347}
5348
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005349static void kvm_set_segment(struct kvm_vcpu *vcpu,
5350 struct kvm_segment *var, int seg)
5351{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005352 kvm_x86_ops.set_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005353}
5354
5355void kvm_get_segment(struct kvm_vcpu *vcpu,
5356 struct kvm_segment *var, int seg)
5357{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005358 kvm_x86_ops.get_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005359}
5360
Paolo Bonzini54987b72014-09-02 13:23:06 +02005361gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5362 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005363{
5364 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005365
5366 BUG_ON(!mmu_is_nested(vcpu));
5367
5368 /* NPT walks are always user-walks */
5369 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005370 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005371
5372 return t_gpa;
5373}
5374
Avi Kivityab9ae312010-11-22 17:53:26 +02005375gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5376 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005377{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005378 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005379 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005380}
5381
Avi Kivityab9ae312010-11-22 17:53:26 +02005382 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5383 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005384{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005385 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005386 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005387 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005388}
5389
Avi Kivityab9ae312010-11-22 17:53:26 +02005390gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5391 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005392{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005393 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005394 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005395 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005396}
5397
5398/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005399gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5400 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005401{
Avi Kivityab9ae312010-11-22 17:53:26 +02005402 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005403}
5404
5405static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5406 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005407 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005408{
5409 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005410 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005411
5412 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005413 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005414 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005415 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005416 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005417 int ret;
5418
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005419 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005420 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005421 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5422 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005423 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005424 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005425 goto out;
5426 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005427
Izik Eidus77c20022008-12-29 01:42:19 +02005428 bytes -= toread;
5429 data += toread;
5430 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005431 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005432out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005433 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005434}
Izik Eidus77c20022008-12-29 01:42:19 +02005435
Gleb Natapov1871c602010-02-10 14:21:32 +02005436/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005437static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5438 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005439 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005440{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005441 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005442 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005443 unsigned offset;
5444 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005445
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005446 /* Inline kvm_read_guest_virt_helper for speed. */
5447 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5448 exception);
5449 if (unlikely(gpa == UNMAPPED_GVA))
5450 return X86EMUL_PROPAGATE_FAULT;
5451
5452 offset = addr & (PAGE_SIZE-1);
5453 if (WARN_ON(offset + bytes > PAGE_SIZE))
5454 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005455 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5456 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005457 if (unlikely(ret < 0))
5458 return X86EMUL_IO_NEEDED;
5459
5460 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005461}
5462
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005463int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005464 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005465 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005466{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005467 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005468
Paolo Bonzini353c0952019-01-29 18:41:16 +01005469 /*
5470 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5471 * is returned, but our callers are not ready for that and they blindly
5472 * call kvm_inject_page_fault. Ensure that they at least do not leak
5473 * uninitialized kernel stack memory into cr2 and error code.
5474 */
5475 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005476 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005477 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005478}
Nadav Har'El064aea72011-05-25 23:04:56 +03005479EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005480
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005481static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5482 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005483 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005484{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005485 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005486 u32 access = 0;
5487
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005488 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005489 access |= PFERR_USER_MASK;
5490
5491 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005492}
5493
Radim Krčmář7a036a62015-10-30 16:36:24 +01005494static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5495 unsigned long addr, void *val, unsigned int bytes)
5496{
5497 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5498 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5499
5500 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5501}
5502
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005503static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5504 struct kvm_vcpu *vcpu, u32 access,
5505 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005506{
5507 void *data = val;
5508 int r = X86EMUL_CONTINUE;
5509
5510 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005511 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005512 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005513 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005514 unsigned offset = addr & (PAGE_SIZE-1);
5515 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5516 int ret;
5517
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005518 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005519 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005520 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005521 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005522 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005523 goto out;
5524 }
5525
5526 bytes -= towrite;
5527 data += towrite;
5528 addr += towrite;
5529 }
5530out:
5531 return r;
5532}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005533
5534static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005535 unsigned int bytes, struct x86_exception *exception,
5536 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005537{
5538 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005539 u32 access = PFERR_WRITE_MASK;
5540
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005541 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005542 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005543
5544 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005545 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005546}
5547
5548int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5549 unsigned int bytes, struct x86_exception *exception)
5550{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005551 /* kvm_write_guest_virt_system can pull in tons of pages. */
5552 vcpu->arch.l1tf_flush_l1d = true;
5553
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005554 /*
5555 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5556 * is returned, but our callers are not ready for that and they blindly
5557 * call kvm_inject_page_fault. Ensure that they at least do not leak
5558 * uninitialized kernel stack memory into cr2 and error code.
5559 */
5560 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005561 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5562 PFERR_WRITE_MASK, exception);
5563}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005564EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005565
Wanpeng Li082d06e2018-04-03 16:28:48 -07005566int handle_ud(struct kvm_vcpu *vcpu)
5567{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005568 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005569 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005570 char sig[5]; /* ud2; .ascii "kvm" */
5571 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005572
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005573 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005574 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5575 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005576 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005577 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005578 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005579 }
5580
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005581 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005582}
5583EXPORT_SYMBOL_GPL(handle_ud);
5584
Tom Lendacky0f89b202016-12-14 14:59:23 -05005585static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5586 gpa_t gpa, bool write)
5587{
5588 /* For APIC access vmexit */
5589 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5590 return 1;
5591
5592 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5593 trace_vcpu_match_mmio(gva, gpa, write, true);
5594 return 1;
5595 }
5596
5597 return 0;
5598}
5599
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005600static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5601 gpa_t *gpa, struct x86_exception *exception,
5602 bool write)
5603{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005604 u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03005605 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005606
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005607 /*
5608 * currently PKRU is only applied to ept enabled guest so
5609 * there is no pkey in EPT page table for L1 guest or EPT
5610 * shadow page table for L2 guest.
5611 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005612 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005613 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005614 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005615 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5616 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005617 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005618 return 1;
5619 }
5620
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005621 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5622
5623 if (*gpa == UNMAPPED_GVA)
5624 return -1;
5625
Tom Lendacky0f89b202016-12-14 14:59:23 -05005626 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005627}
5628
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005629int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005630 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005631{
5632 int ret;
5633
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005634 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005635 if (ret < 0)
5636 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005637 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005638 return 1;
5639}
5640
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005641struct read_write_emulator_ops {
5642 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5643 int bytes);
5644 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5645 void *val, int bytes);
5646 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5647 int bytes, void *val);
5648 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5649 void *val, int bytes);
5650 bool write;
5651};
5652
5653static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5654{
5655 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005656 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005657 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005658 vcpu->mmio_read_completed = 0;
5659 return 1;
5660 }
5661
5662 return 0;
5663}
5664
5665static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5666 void *val, int bytes)
5667{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005668 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005669}
5670
5671static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5672 void *val, int bytes)
5673{
5674 return emulator_write_phys(vcpu, gpa, val, bytes);
5675}
5676
5677static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5678{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005679 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005680 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5681}
5682
5683static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5684 void *val, int bytes)
5685{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005686 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005687 return X86EMUL_IO_NEEDED;
5688}
5689
5690static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5691 void *val, int bytes)
5692{
Avi Kivityf78146b2012-04-18 19:22:47 +03005693 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5694
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005695 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005696 return X86EMUL_CONTINUE;
5697}
5698
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005699static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005700 .read_write_prepare = read_prepare,
5701 .read_write_emulate = read_emulate,
5702 .read_write_mmio = vcpu_mmio_read,
5703 .read_write_exit_mmio = read_exit_mmio,
5704};
5705
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005706static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005707 .read_write_emulate = write_emulate,
5708 .read_write_mmio = write_mmio,
5709 .read_write_exit_mmio = write_exit_mmio,
5710 .write = true,
5711};
5712
Xiao Guangrong22388a32011-07-13 14:32:31 +08005713static int emulator_read_write_onepage(unsigned long addr, void *val,
5714 unsigned int bytes,
5715 struct x86_exception *exception,
5716 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005717 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005718{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005719 gpa_t gpa;
5720 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005721 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005722 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005723 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005724
5725 /*
5726 * If the exit was due to a NPF we may already have a GPA.
5727 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5728 * Note, this cannot be used on string operations since string
5729 * operation using rep will only have the initial GPA from the NPF
5730 * occurred.
5731 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005732 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5733 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5734 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005735 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5736 } else {
5737 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5738 if (ret < 0)
5739 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005740 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005741
Brijesh Singh618232e2017-08-17 18:36:57 +02005742 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005743 return X86EMUL_CONTINUE;
5744
Carsten Ottebbd9b642007-10-30 18:44:21 +01005745 /*
5746 * Is this MMIO handled locally?
5747 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005748 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005749 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005750 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005751
Avi Kivity70252a12010-01-19 12:51:22 +02005752 gpa += handled;
5753 bytes -= handled;
5754 val += handled;
5755
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005756 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5757 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5758 frag->gpa = gpa;
5759 frag->data = val;
5760 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005761 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005762}
5763
Xiubo Li52eb5a62015-03-13 17:39:45 +08005764static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5765 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005766 void *val, unsigned int bytes,
5767 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005768 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005769{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005770 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005771 gpa_t gpa;
5772 int rc;
5773
5774 if (ops->read_write_prepare &&
5775 ops->read_write_prepare(vcpu, val, bytes))
5776 return X86EMUL_CONTINUE;
5777
5778 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005779
Carsten Ottebbd9b642007-10-30 18:44:21 +01005780 /* Crossing a page boundary? */
5781 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005782 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005783
5784 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005785 rc = emulator_read_write_onepage(addr, val, now, exception,
5786 vcpu, ops);
5787
Carsten Ottebbd9b642007-10-30 18:44:21 +01005788 if (rc != X86EMUL_CONTINUE)
5789 return rc;
5790 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005791 if (ctxt->mode != X86EMUL_MODE_PROT64)
5792 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005793 val += now;
5794 bytes -= now;
5795 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005796
Avi Kivityf78146b2012-04-18 19:22:47 +03005797 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5798 vcpu, ops);
5799 if (rc != X86EMUL_CONTINUE)
5800 return rc;
5801
5802 if (!vcpu->mmio_nr_fragments)
5803 return rc;
5804
5805 gpa = vcpu->mmio_fragments[0].gpa;
5806
5807 vcpu->mmio_needed = 1;
5808 vcpu->mmio_cur_fragment = 0;
5809
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005810 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005811 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5812 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5813 vcpu->run->mmio.phys_addr = gpa;
5814
5815 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005816}
5817
5818static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5819 unsigned long addr,
5820 void *val,
5821 unsigned int bytes,
5822 struct x86_exception *exception)
5823{
5824 return emulator_read_write(ctxt, addr, val, bytes,
5825 exception, &read_emultor);
5826}
5827
Xiubo Li52eb5a62015-03-13 17:39:45 +08005828static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005829 unsigned long addr,
5830 const void *val,
5831 unsigned int bytes,
5832 struct x86_exception *exception)
5833{
5834 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5835 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005836}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005837
Avi Kivitydaea3e72010-03-15 13:59:54 +02005838#define CMPXCHG_TYPE(t, ptr, old, new) \
5839 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5840
5841#ifdef CONFIG_X86_64
5842# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5843#else
5844# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005845 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005846#endif
5847
Avi Kivity0f65dd72011-04-20 13:37:53 +03005848static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5849 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005850 const void *old,
5851 const void *new,
5852 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005853 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005854{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005855 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005856 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005857 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005858 char *kaddr;
5859 bool exchanged;
5860
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005861 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005862 if (bytes > 8 || (bytes & (bytes - 1)))
5863 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005864
Avi Kivitydaea3e72010-03-15 13:59:54 +02005865 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005866
Avi Kivitydaea3e72010-03-15 13:59:54 +02005867 if (gpa == UNMAPPED_GVA ||
5868 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5869 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005870
Avi Kivitydaea3e72010-03-15 13:59:54 +02005871 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5872 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005873
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005874 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005875 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005876
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005877 kaddr = map.hva + offset_in_page(gpa);
5878
Avi Kivitydaea3e72010-03-15 13:59:54 +02005879 switch (bytes) {
5880 case 1:
5881 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5882 break;
5883 case 2:
5884 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5885 break;
5886 case 4:
5887 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5888 break;
5889 case 8:
5890 exchanged = CMPXCHG64(kaddr, old, new);
5891 break;
5892 default:
5893 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005894 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005895
5896 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005897
5898 if (!exchanged)
5899 return X86EMUL_CMPXCHG_FAILED;
5900
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005901 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005902
5903 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005904
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005905emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005906 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005907
Avi Kivity0f65dd72011-04-20 13:37:53 +03005908 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005909}
5910
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005911static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5912{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005913 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005914
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005915 for (i = 0; i < vcpu->arch.pio.count; i++) {
5916 if (vcpu->arch.pio.in)
5917 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5918 vcpu->arch.pio.size, pd);
5919 else
5920 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5921 vcpu->arch.pio.port, vcpu->arch.pio.size,
5922 pd);
5923 if (r)
5924 break;
5925 pd += vcpu->arch.pio.size;
5926 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005927 return r;
5928}
5929
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005930static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5931 unsigned short port, void *val,
5932 unsigned int count, bool in)
5933{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005934 vcpu->arch.pio.port = port;
5935 vcpu->arch.pio.in = in;
5936 vcpu->arch.pio.count = count;
5937 vcpu->arch.pio.size = size;
5938
5939 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5940 vcpu->arch.pio.count = 0;
5941 return 1;
5942 }
5943
5944 vcpu->run->exit_reason = KVM_EXIT_IO;
5945 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5946 vcpu->run->io.size = size;
5947 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5948 vcpu->run->io.count = count;
5949 vcpu->run->io.port = port;
5950
5951 return 0;
5952}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005953
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005954static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5955 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005956{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005957 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005958
Gleb Natapov79729952010-03-18 15:20:24 +02005959 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005960 goto data_avail;
5961
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005962 memset(vcpu->arch.pio_data, 0, size * count);
5963
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005964 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5965 if (ret) {
5966data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005967 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005968 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005969 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005970 return 1;
5971 }
5972
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005973 return 0;
5974}
5975
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005976static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5977 int size, unsigned short port, void *val,
5978 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005979{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005980 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005981
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005982}
5983
5984static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5985 unsigned short port, const void *val,
5986 unsigned int count)
5987{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005988 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005989 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005990 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005991}
5992
Carsten Ottebbd9b642007-10-30 18:44:21 +01005993static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5994 int size, unsigned short port,
5995 const void *val, unsigned int count)
5996{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005997 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005998}
5999
6000static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6001{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006002 return kvm_x86_ops.get_segment_base(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006003}
6004
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006005static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006006{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006007 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006008}
6009
Jiang Biaoae6a2372016-11-07 08:54:51 +08006010static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006011{
6012 if (!need_emulate_wbinvd(vcpu))
6013 return X86EMUL_CONTINUE;
6014
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006015 if (kvm_x86_ops.has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006016 int cpu = get_cpu();
6017
6018 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006019 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6020 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006021 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006022 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006023 } else
6024 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006025 return X86EMUL_CONTINUE;
6026}
Joel Schopp5cb56052015-03-02 13:43:31 -06006027
6028int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6029{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006030 kvm_emulate_wbinvd_noskip(vcpu);
6031 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006032}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006033EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6034
Joel Schopp5cb56052015-03-02 13:43:31 -06006035
6036
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006037static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6038{
Joel Schopp5cb56052015-03-02 13:43:31 -06006039 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006040}
6041
Xiubo Li52eb5a62015-03-13 17:39:45 +08006042static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6043 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006044{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006045 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006046}
6047
Xiubo Li52eb5a62015-03-13 17:39:45 +08006048static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6049 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006050{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006051
Avi Kivity717746e2011-04-20 13:37:53 +03006052 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006053}
6054
Gleb Natapov52a46612010-03-18 15:20:03 +02006055static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6056{
6057 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6058}
6059
Avi Kivity717746e2011-04-20 13:37:53 +03006060static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006061{
Avi Kivity717746e2011-04-20 13:37:53 +03006062 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006063 unsigned long value;
6064
6065 switch (cr) {
6066 case 0:
6067 value = kvm_read_cr0(vcpu);
6068 break;
6069 case 2:
6070 value = vcpu->arch.cr2;
6071 break;
6072 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006073 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006074 break;
6075 case 4:
6076 value = kvm_read_cr4(vcpu);
6077 break;
6078 case 8:
6079 value = kvm_get_cr8(vcpu);
6080 break;
6081 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006082 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006083 return 0;
6084 }
6085
6086 return value;
6087}
6088
Avi Kivity717746e2011-04-20 13:37:53 +03006089static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006090{
Avi Kivity717746e2011-04-20 13:37:53 +03006091 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006092 int res = 0;
6093
Gleb Natapov52a46612010-03-18 15:20:03 +02006094 switch (cr) {
6095 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006096 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006097 break;
6098 case 2:
6099 vcpu->arch.cr2 = val;
6100 break;
6101 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006102 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006103 break;
6104 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006105 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006106 break;
6107 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006108 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006109 break;
6110 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006111 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006112 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006113 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006114
6115 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006116}
6117
Avi Kivity717746e2011-04-20 13:37:53 +03006118static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006119{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006120 return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006121}
6122
Avi Kivity4bff1e862011-04-20 13:37:53 +03006123static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006124{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006125 kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006126}
6127
Avi Kivity4bff1e862011-04-20 13:37:53 +03006128static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006129{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006130 kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006131}
6132
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006133static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6134{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006135 kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006136}
6137
6138static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6139{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006140 kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006141}
6142
Avi Kivity4bff1e862011-04-20 13:37:53 +03006143static unsigned long emulator_get_cached_segment_base(
6144 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006145{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006146 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006147}
6148
Avi Kivity1aa36612011-04-27 13:20:30 +03006149static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6150 struct desc_struct *desc, u32 *base3,
6151 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006152{
6153 struct kvm_segment var;
6154
Avi Kivity4bff1e862011-04-20 13:37:53 +03006155 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006156 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006157
Gleb Natapov378a8b02013-01-21 15:36:48 +02006158 if (var.unusable) {
6159 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006160 if (base3)
6161 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006162 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006163 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006164
6165 if (var.g)
6166 var.limit >>= 12;
6167 set_desc_limit(desc, var.limit);
6168 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006169#ifdef CONFIG_X86_64
6170 if (base3)
6171 *base3 = var.base >> 32;
6172#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006173 desc->type = var.type;
6174 desc->s = var.s;
6175 desc->dpl = var.dpl;
6176 desc->p = var.present;
6177 desc->avl = var.avl;
6178 desc->l = var.l;
6179 desc->d = var.db;
6180 desc->g = var.g;
6181
6182 return true;
6183}
6184
Avi Kivity1aa36612011-04-27 13:20:30 +03006185static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6186 struct desc_struct *desc, u32 base3,
6187 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006188{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006189 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006190 struct kvm_segment var;
6191
Avi Kivity1aa36612011-04-27 13:20:30 +03006192 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006193 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006194#ifdef CONFIG_X86_64
6195 var.base |= ((u64)base3) << 32;
6196#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006197 var.limit = get_desc_limit(desc);
6198 if (desc->g)
6199 var.limit = (var.limit << 12) | 0xfff;
6200 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006201 var.dpl = desc->dpl;
6202 var.db = desc->d;
6203 var.s = desc->s;
6204 var.l = desc->l;
6205 var.g = desc->g;
6206 var.avl = desc->avl;
6207 var.present = desc->p;
6208 var.unusable = !var.present;
6209 var.padding = 0;
6210
6211 kvm_set_segment(vcpu, &var, seg);
6212 return;
6213}
6214
Avi Kivity717746e2011-04-20 13:37:53 +03006215static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6216 u32 msr_index, u64 *pdata)
6217{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006218 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006219}
6220
6221static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6222 u32 msr_index, u64 data)
6223{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006224 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006225}
6226
Paolo Bonzini64d60672015-05-07 11:36:11 +02006227static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6228{
6229 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6230
6231 return vcpu->arch.smbase;
6232}
6233
6234static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6235{
6236 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6237
6238 vcpu->arch.smbase = smbase;
6239}
6240
Nadav Amit67f4d422014-06-02 18:34:09 +03006241static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6242 u32 pmc)
6243{
Like Xu98ff80f2019-10-27 18:52:40 +08006244 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006245}
6246
Avi Kivity222d21a2011-11-10 14:57:30 +02006247static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6248 u32 pmc, u64 *pdata)
6249{
Wei Huangc6702c92015-06-19 13:44:45 +02006250 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006251}
6252
Avi Kivity6c3287f2011-04-20 15:43:05 +03006253static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6254{
6255 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6256}
6257
Avi Kivity29535382011-04-20 13:37:53 +03006258static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006259 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006260 enum x86_intercept_stage stage)
6261{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006262 return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006263 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006264}
6265
Yu Zhange911eb32017-08-24 20:27:52 +08006266static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006267 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6268 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006269{
Sean Christophersonf91af512020-03-04 17:34:37 -08006270 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006271}
6272
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006273static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6274{
6275 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6276}
6277
6278static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6279{
6280 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6281}
6282
6283static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6284{
6285 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6286}
6287
Avi Kivitydd856ef2012-08-27 23:46:17 +03006288static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6289{
6290 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6291}
6292
6293static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6294{
6295 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6296}
6297
Nadav Amit801806d2015-01-26 09:32:23 +02006298static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6299{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006300 kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02006301}
6302
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006303static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6304{
6305 return emul_to_vcpu(ctxt)->arch.hflags;
6306}
6307
6308static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6309{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006310 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006311}
6312
Sean Christophersoned193212019-04-02 08:03:09 -07006313static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6314 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006315{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006316 return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006317}
6318
Sean Christophersonc5833c72019-04-02 08:03:10 -07006319static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6320{
6321 kvm_smm_changed(emul_to_vcpu(ctxt));
6322}
6323
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006324static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6325{
6326 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6327}
6328
Mathias Krause0225fb52012-08-30 01:30:16 +02006329static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006330 .read_gpr = emulator_read_gpr,
6331 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006332 .read_std = emulator_read_std,
6333 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006334 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006335 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006336 .read_emulated = emulator_read_emulated,
6337 .write_emulated = emulator_write_emulated,
6338 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006339 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006340 .pio_in_emulated = emulator_pio_in_emulated,
6341 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006342 .get_segment = emulator_get_segment,
6343 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006344 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006345 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006346 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006347 .set_gdt = emulator_set_gdt,
6348 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006349 .get_cr = emulator_get_cr,
6350 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006351 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006352 .get_dr = emulator_get_dr,
6353 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006354 .get_smbase = emulator_get_smbase,
6355 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006356 .set_msr = emulator_set_msr,
6357 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006358 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006359 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006360 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006361 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006362 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006363 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006364 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006365 .guest_has_long_mode = emulator_guest_has_long_mode,
6366 .guest_has_movbe = emulator_guest_has_movbe,
6367 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006368 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006369 .get_hflags = emulator_get_hflags,
6370 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006371 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006372 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006373 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006374};
6375
Gleb Natapov95cb2292010-04-28 19:15:43 +03006376static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6377{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006378 u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006379 /*
6380 * an sti; sti; sequence only disable interrupts for the first
6381 * instruction. So, if the last instruction, be it emulated or
6382 * not, left the system with the INT_STI flag enabled, it
6383 * means that the last instruction is an sti. We should not
6384 * leave the flag on in this case. The same goes for mov ss
6385 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006386 if (int_shadow & mask)
6387 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006388 if (unlikely(int_shadow || mask)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006389 kvm_x86_ops.set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006390 if (!mask)
6391 kvm_make_request(KVM_REQ_EVENT, vcpu);
6392 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006393}
6394
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006395static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006396{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006397 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006398 if (ctxt->exception.vector == PF_VECTOR)
Sean Christopherson53b3d8e2020-03-20 14:28:01 -07006399 return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006400
6401 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006402 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6403 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006404 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006405 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006406 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006407}
6408
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006409static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6410{
6411 struct x86_emulate_ctxt *ctxt;
6412
6413 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6414 if (!ctxt) {
6415 pr_err("kvm: failed to allocate vcpu's emulator\n");
6416 return NULL;
6417 }
6418
6419 ctxt->vcpu = vcpu;
6420 ctxt->ops = &emulate_ops;
6421 vcpu->arch.emulate_ctxt = ctxt;
6422
6423 return ctxt;
6424}
6425
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006426static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6427{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006428 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006429 int cs_db, cs_l;
6430
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006431 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006432
Sean Christopherson744e6992020-02-18 15:03:09 -08006433 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006434 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006435 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6436
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006437 ctxt->eip = kvm_rip_read(vcpu);
6438 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6439 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006440 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006441 cs_db ? X86EMUL_MODE_PROT32 :
6442 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006443 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006444 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6445 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006446
Avi Kivitydd856ef2012-08-27 23:46:17 +03006447 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006448 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006449}
6450
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006451void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006452{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006453 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006454 int ret;
6455
6456 init_emulate_ctxt(vcpu);
6457
Avi Kivity9dac77f2011-06-01 15:34:25 +03006458 ctxt->op_bytes = 2;
6459 ctxt->ad_bytes = 2;
6460 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006461 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006462
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006463 if (ret != X86EMUL_CONTINUE) {
6464 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6465 } else {
6466 ctxt->eip = ctxt->_eip;
6467 kvm_rip_write(vcpu, ctxt->eip);
6468 kvm_set_rflags(vcpu, ctxt->eflags);
6469 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006470}
6471EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6472
Liran Alone2366172018-03-12 13:12:49 +02006473static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006474{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006475 ++vcpu->stat.insn_emulation_fail;
6476 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006477
Sean Christopherson42cbf062019-08-27 14:40:31 -07006478 if (emulation_type & EMULTYPE_VMWARE_GP) {
6479 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006480 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006481 }
Liran Alone2366172018-03-12 13:12:49 +02006482
Sean Christopherson738fece2019-08-27 14:40:34 -07006483 if (emulation_type & EMULTYPE_SKIP) {
6484 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6485 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6486 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006487 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006488 }
6489
Sean Christopherson22da61c2019-08-27 14:40:28 -07006490 kvm_queue_exception(vcpu, UD_VECTOR);
6491
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006492 if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006493 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6494 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6495 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006496 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006497 }
Liran Alone2366172018-03-12 13:12:49 +02006498
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006499 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006500}
6501
Sean Christopherson736c2912019-12-06 15:57:14 -08006502static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006503 bool write_fault_to_shadow_pgtable,
6504 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006505{
Sean Christopherson736c2912019-12-06 15:57:14 -08006506 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006507 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006508
Sean Christopherson92daa482020-02-18 15:03:08 -08006509 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006510 return false;
6511
Sean Christopherson92daa482020-02-18 15:03:08 -08006512 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6513 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006514 return false;
6515
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006516 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006517 /*
6518 * Write permission should be allowed since only
6519 * write access need to be emulated.
6520 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006521 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006522
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006523 /*
6524 * If the mapping is invalid in guest, let cpu retry
6525 * it to generate fault.
6526 */
6527 if (gpa == UNMAPPED_GVA)
6528 return true;
6529 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006530
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006531 /*
6532 * Do not retry the unhandleable instruction if it faults on the
6533 * readonly host memory, otherwise it will goto a infinite loop:
6534 * retry instruction -> write #PF -> emulation fail -> retry
6535 * instruction -> ...
6536 */
6537 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006538
6539 /*
6540 * If the instruction failed on the error pfn, it can not be fixed,
6541 * report the error to userspace.
6542 */
6543 if (is_error_noslot_pfn(pfn))
6544 return false;
6545
6546 kvm_release_pfn_clean(pfn);
6547
6548 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006549 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006550 unsigned int indirect_shadow_pages;
6551
6552 spin_lock(&vcpu->kvm->mmu_lock);
6553 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6554 spin_unlock(&vcpu->kvm->mmu_lock);
6555
6556 if (indirect_shadow_pages)
6557 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6558
Gleb Natapova6f177e2010-07-08 12:41:12 +03006559 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006560 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006561
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006562 /*
6563 * if emulation was due to access to shadowed page table
6564 * and it failed try to unshadow page and re-enter the
6565 * guest to let CPU execute the instruction.
6566 */
6567 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006568
6569 /*
6570 * If the access faults on its page table, it can not
6571 * be fixed by unprotecting shadow page and it should
6572 * be reported to userspace.
6573 */
6574 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006575}
6576
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006577static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006578 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006579{
6580 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006581 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006582
6583 last_retry_eip = vcpu->arch.last_retry_eip;
6584 last_retry_addr = vcpu->arch.last_retry_addr;
6585
6586 /*
6587 * If the emulation is caused by #PF and it is non-page_table
6588 * writing instruction, it means the VM-EXIT is caused by shadow
6589 * page protected, we can zap the shadow page and retry this
6590 * instruction directly.
6591 *
6592 * Note: if the guest uses a non-page-table modifying instruction
6593 * on the PDE that points to the instruction, then we will unmap
6594 * the instruction and go to an infinite loop. So, we cache the
6595 * last retried eip and the last fault address, if we meet the eip
6596 * and the address again, we can break out of the potential infinite
6597 * loop.
6598 */
6599 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6600
Sean Christopherson92daa482020-02-18 15:03:08 -08006601 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006602 return false;
6603
Sean Christopherson92daa482020-02-18 15:03:08 -08006604 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6605 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006606 return false;
6607
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006608 if (x86_page_table_writing_insn(ctxt))
6609 return false;
6610
Sean Christopherson736c2912019-12-06 15:57:14 -08006611 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006612 return false;
6613
6614 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006615 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006616
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006617 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006618 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006619
Xiao Guangrong22368022013-01-13 23:44:12 +08006620 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006621
6622 return true;
6623}
6624
Gleb Natapov716d51a2012-09-03 15:24:26 +03006625static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6626static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6627
Paolo Bonzini64d60672015-05-07 11:36:11 +02006628static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006629{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006630 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006631 /* This is a good place to trace that we are exiting SMM. */
6632 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6633
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006634 /* Process a latched INIT or SMI, if any. */
6635 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006636 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006637
6638 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006639}
6640
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006641static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6642 unsigned long *db)
6643{
6644 u32 dr6 = 0;
6645 int i;
6646 u32 enable, rwlen;
6647
6648 enable = dr7;
6649 rwlen = dr7 >> 16;
6650 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6651 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6652 dr6 |= (1 << i);
6653 return dr6;
6654}
6655
Sean Christopherson120c2c42019-08-27 14:40:29 -07006656static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006657{
6658 struct kvm_run *kvm_run = vcpu->run;
6659
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006660 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6661 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6662 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6663 kvm_run->debug.arch.exception = DB_VECTOR;
6664 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006665 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006666 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006667 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006668 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006669}
6670
Kyle Huey6affcbe2016-11-29 12:40:40 -08006671int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6672{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006673 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006674 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006675
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006676 r = kvm_x86_ops.skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006677 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006678 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006679
6680 /*
6681 * rflags is the old, "raw" value of the flags. The new value has
6682 * not been saved yet.
6683 *
6684 * This is correct even for TF set by the guest, because "the
6685 * processor will not generate this exception after the instruction
6686 * that sets the TF flag".
6687 */
6688 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006689 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006690 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006691}
6692EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6693
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006694static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6695{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006696 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6697 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006698 struct kvm_run *kvm_run = vcpu->run;
6699 unsigned long eip = kvm_get_linear_rip(vcpu);
6700 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006701 vcpu->arch.guest_debug_dr7,
6702 vcpu->arch.eff_db);
6703
6704 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006705 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006706 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006707 kvm_run->debug.arch.exception = DB_VECTOR;
6708 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006709 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006710 return true;
6711 }
6712 }
6713
Nadav Amit4161a562014-07-17 01:19:31 +03006714 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6715 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006716 unsigned long eip = kvm_get_linear_rip(vcpu);
6717 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006718 vcpu->arch.dr7,
6719 vcpu->arch.db);
6720
6721 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006722 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006723 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006724 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006725 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006726 return true;
6727 }
6728 }
6729
6730 return false;
6731}
6732
Liran Alon04789b62018-03-12 13:12:50 +02006733static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6734{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006735 switch (ctxt->opcode_len) {
6736 case 1:
6737 switch (ctxt->b) {
6738 case 0xe4: /* IN */
6739 case 0xe5:
6740 case 0xec:
6741 case 0xed:
6742 case 0xe6: /* OUT */
6743 case 0xe7:
6744 case 0xee:
6745 case 0xef:
6746 case 0x6c: /* INS */
6747 case 0x6d:
6748 case 0x6e: /* OUTS */
6749 case 0x6f:
6750 return true;
6751 }
6752 break;
6753 case 2:
6754 switch (ctxt->b) {
6755 case 0x33: /* RDPMC */
6756 return true;
6757 }
6758 break;
Liran Alon04789b62018-03-12 13:12:50 +02006759 }
6760
6761 return false;
6762}
6763
Sean Christopherson736c2912019-12-06 15:57:14 -08006764int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6765 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006766{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006767 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006768 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006769 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006770 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006771
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006772 vcpu->arch.l1tf_flush_l1d = true;
6773
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006774 /*
6775 * Clear write_fault_to_shadow_pgtable here to ensure it is
6776 * never reused.
6777 */
6778 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006779 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006780
Sheng Yang571008d2008-01-02 14:49:22 +08006781 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006782 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006783
6784 /*
6785 * We will reenter on the same instruction since
6786 * we do not set complete_userspace_io. This does not
6787 * handle watchpoints yet, those would be handled in
6788 * the emulate_ops.
6789 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006790 if (!(emulation_type & EMULTYPE_SKIP) &&
6791 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006792 return r;
6793
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006794 ctxt->interruptibility = 0;
6795 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006796 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006797 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006798
Borislav Petkovb51e9742013-09-22 16:44:52 +02006799 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006800
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006801 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006802
Avi Kivitye46479f2010-04-11 13:05:16 +03006803 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006804 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006805 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006806 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006807 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6808 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006809 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006810 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006811 if (reexecute_instruction(vcpu, cr2_or_gpa,
6812 write_fault_to_spt,
6813 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006814 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006815 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006816 /*
6817 * #UD should result in just EMULATION_FAILED, and trap-like
6818 * exception should not be encountered during decode.
6819 */
6820 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6821 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006822 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006823 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006824 }
Liran Alone2366172018-03-12 13:12:49 +02006825 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006826 }
6827 }
6828
Sean Christopherson42cbf062019-08-27 14:40:31 -07006829 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6830 !is_vmware_backdoor_opcode(ctxt)) {
6831 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006832 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006833 }
Liran Alon04789b62018-03-12 13:12:50 +02006834
Sean Christopherson1957aa62019-08-27 14:40:39 -07006835 /*
6836 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6837 * for kvm_skip_emulated_instruction(). The caller is responsible for
6838 * updating interruptibility state and injecting single-step #DBs.
6839 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006840 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006841 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006842 if (ctxt->eflags & X86_EFLAGS_RF)
6843 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006844 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006845 }
6846
Sean Christopherson736c2912019-12-06 15:57:14 -08006847 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006848 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006849
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006850 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006851 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006852 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6853 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006854 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006855 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006856
Gleb Natapov5cd21912010-03-18 15:20:26 +02006857restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006858 if (emulation_type & EMULTYPE_PF) {
6859 /* Save the faulting GPA (cr2) in the address field */
6860 ctxt->exception.address = cr2_or_gpa;
6861
6862 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6863 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006864 ctxt->gpa_available = true;
6865 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006866 }
6867 } else {
6868 /* Sanitize the address out of an abundance of paranoia. */
6869 ctxt->exception.address = 0;
6870 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006871
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006872 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006873
Joerg Roedel775fde82011-04-04 12:39:24 +02006874 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006875 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006876
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006877 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006878 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006879 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006880 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006881
Liran Alone2366172018-03-12 13:12:49 +02006882 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006883 }
6884
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006885 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006886 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006887 if (inject_emulated_exception(vcpu))
6888 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006889 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006890 if (!vcpu->arch.pio.in) {
6891 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006892 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006893 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006894 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006895 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6896 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006897 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006898 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006899 ++vcpu->stat.mmio_exits;
6900
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006901 if (!vcpu->mmio_is_write)
6902 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006903 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006904 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006905 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006906 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006907 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006908 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006909
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006910 if (writeback) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006911 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006912 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006913 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006914 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006915 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6916 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006917 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006918 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006919 if (kvm_x86_ops.update_emulated_instruction)
6920 kvm_x86_ops.update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006921 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006922 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006923
6924 /*
6925 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6926 * do nothing, and it will be requested again as soon as
6927 * the shadow expires. But we still need to check here,
6928 * because POPF has no interrupt shadow.
6929 */
6930 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6931 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006932 } else
6933 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006934
Gleb Natapove85d28f2010-07-29 15:11:52 +03006935 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006936}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006937
6938int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6939{
6940 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6941}
6942EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6943
6944int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6945 void *insn, int insn_len)
6946{
6947 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6948}
6949EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006950
Sean Christopherson8764ed52019-04-29 07:04:15 -07006951static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6952{
6953 vcpu->arch.pio.count = 0;
6954 return 1;
6955}
6956
Sean Christopherson45def772019-03-11 20:01:05 -07006957static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6958{
6959 vcpu->arch.pio.count = 0;
6960
6961 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6962 return 1;
6963
6964 return kvm_skip_emulated_instruction(vcpu);
6965}
6966
Sean Christophersondca7f122018-03-08 08:57:27 -08006967static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6968 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006969{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006970 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006971 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6972
Sean Christopherson8764ed52019-04-29 07:04:15 -07006973 if (ret)
6974 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006975
Sean Christopherson8764ed52019-04-29 07:04:15 -07006976 /*
6977 * Workaround userspace that relies on old KVM behavior of %rip being
6978 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6979 */
6980 if (port == 0x7e &&
6981 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6982 vcpu->arch.complete_userspace_io =
6983 complete_fast_pio_out_port_0x7e;
6984 kvm_skip_emulated_instruction(vcpu);
6985 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006986 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6987 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6988 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006989 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006990}
Carsten Ottede7d7892007-10-30 18:44:25 +01006991
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006992static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6993{
6994 unsigned long val;
6995
6996 /* We should only ever be called with arch.pio.count equal to 1 */
6997 BUG_ON(vcpu->arch.pio.count != 1);
6998
Sean Christopherson45def772019-03-11 20:01:05 -07006999 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7000 vcpu->arch.pio.count = 0;
7001 return 1;
7002 }
7003
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007004 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007005 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007006
7007 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007008 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007009 * the copy and tracing
7010 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007011 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007012 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007013
Sean Christopherson45def772019-03-11 20:01:05 -07007014 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007015}
7016
Sean Christophersondca7f122018-03-08 08:57:27 -08007017static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7018 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007019{
7020 unsigned long val;
7021 int ret;
7022
7023 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007024 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007025
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007026 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007027 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007028 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007029 return ret;
7030 }
7031
Sean Christopherson45def772019-03-11 20:01:05 -07007032 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007033 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7034
7035 return 0;
7036}
Sean Christophersondca7f122018-03-08 08:57:27 -08007037
7038int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7039{
Sean Christopherson45def772019-03-11 20:01:05 -07007040 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007041
Sean Christophersondca7f122018-03-08 08:57:27 -08007042 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007043 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007044 else
Sean Christopherson45def772019-03-11 20:01:05 -07007045 ret = kvm_fast_pio_out(vcpu, size, port);
7046 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007047}
7048EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007049
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007050static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007051{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007052 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007053 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007054}
7055
7056static void tsc_khz_changed(void *data)
7057{
7058 struct cpufreq_freqs *freq = data;
7059 unsigned long khz = 0;
7060
7061 if (data)
7062 khz = freq->new;
7063 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7064 khz = cpufreq_quick_get(raw_smp_processor_id());
7065 if (!khz)
7066 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007067 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007068}
7069
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007070#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007071static void kvm_hyperv_tsc_notifier(void)
7072{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007073 struct kvm *kvm;
7074 struct kvm_vcpu *vcpu;
7075 int cpu;
7076
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007077 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007078 list_for_each_entry(kvm, &vm_list, vm_list)
7079 kvm_make_mclock_inprogress_request(kvm);
7080
7081 hyperv_stop_tsc_emulation();
7082
7083 /* TSC frequency always matches when on Hyper-V */
7084 for_each_present_cpu(cpu)
7085 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7086 kvm_max_guest_tsc_khz = tsc_khz;
7087
7088 list_for_each_entry(kvm, &vm_list, vm_list) {
7089 struct kvm_arch *ka = &kvm->arch;
7090
7091 spin_lock(&ka->pvclock_gtod_sync_lock);
7092
7093 pvclock_update_vm_gtod_copy(kvm);
7094
7095 kvm_for_each_vcpu(cpu, vcpu, kvm)
7096 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7097
7098 kvm_for_each_vcpu(cpu, vcpu, kvm)
7099 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7100
7101 spin_unlock(&ka->pvclock_gtod_sync_lock);
7102 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007103 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007104}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007105#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007106
Viresh Kumardf240142019-04-29 15:03:58 +05307107static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007108{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007109 struct kvm *kvm;
7110 struct kvm_vcpu *vcpu;
7111 int i, send_ipi = 0;
7112
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007113 /*
7114 * We allow guests to temporarily run on slowing clocks,
7115 * provided we notify them after, or to run on accelerating
7116 * clocks, provided we notify them before. Thus time never
7117 * goes backwards.
7118 *
7119 * However, we have a problem. We can't atomically update
7120 * the frequency of a given CPU from this function; it is
7121 * merely a notifier, which can be called from any CPU.
7122 * Changing the TSC frequency at arbitrary points in time
7123 * requires a recomputation of local variables related to
7124 * the TSC for each VCPU. We must flag these local variables
7125 * to be updated and be sure the update takes place with the
7126 * new frequency before any guests proceed.
7127 *
7128 * Unfortunately, the combination of hotplug CPU and frequency
7129 * change creates an intractable locking scenario; the order
7130 * of when these callouts happen is undefined with respect to
7131 * CPU hotplug, and they can race with each other. As such,
7132 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7133 * undefined; you can actually have a CPU frequency change take
7134 * place in between the computation of X and the setting of the
7135 * variable. To protect against this problem, all updates of
7136 * the per_cpu tsc_khz variable are done in an interrupt
7137 * protected IPI, and all callers wishing to update the value
7138 * must wait for a synchronous IPI to complete (which is trivial
7139 * if the caller is on the CPU already). This establishes the
7140 * necessary total order on variable updates.
7141 *
7142 * Note that because a guest time update may take place
7143 * anytime after the setting of the VCPU's request bit, the
7144 * correct TSC value must be set before the request. However,
7145 * to ensure the update actually makes it to any guest which
7146 * starts running in hardware virtualization between the set
7147 * and the acquisition of the spinlock, we must also ping the
7148 * CPU after setting the request bit.
7149 *
7150 */
7151
Viresh Kumardf240142019-04-29 15:03:58 +05307152 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007153
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007154 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007155 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007156 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307157 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007158 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007159 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007160 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007161 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007162 }
7163 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007164 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007165
7166 if (freq->old < freq->new && send_ipi) {
7167 /*
7168 * We upscale the frequency. Must make the guest
7169 * doesn't see old kvmclock values while running with
7170 * the new frequency, otherwise we risk the guest sees
7171 * time go backwards.
7172 *
7173 * In case we update the frequency for another cpu
7174 * (which might be in guest context) send an interrupt
7175 * to kick the cpu out of guest context. Next time
7176 * guest context is entered kvmclock will be updated,
7177 * so the guest will not see stale values.
7178 */
Viresh Kumardf240142019-04-29 15:03:58 +05307179 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007180 }
Viresh Kumardf240142019-04-29 15:03:58 +05307181}
7182
7183static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7184 void *data)
7185{
7186 struct cpufreq_freqs *freq = data;
7187 int cpu;
7188
7189 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7190 return 0;
7191 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7192 return 0;
7193
7194 for_each_cpu(cpu, freq->policy->cpus)
7195 __kvmclock_cpufreq_notifier(freq, cpu);
7196
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007197 return 0;
7198}
7199
7200static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007201 .notifier_call = kvmclock_cpufreq_notifier
7202};
7203
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007204static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007205{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007206 tsc_khz_changed(NULL);
7207 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007208}
7209
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007210static void kvm_timer_init(void)
7211{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007212 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307213
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007214 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007215#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007216 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007217 int cpu;
7218
Avi Kivity3e26f232010-12-16 12:16:34 +02007219 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007220 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08007221 if (policy) {
7222 if (policy->cpuinfo.max_freq)
7223 max_tsc_khz = policy->cpuinfo.max_freq;
7224 cpufreq_cpu_put(policy);
7225 }
Avi Kivity3e26f232010-12-16 12:16:34 +02007226 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007227#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007228 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7229 CPUFREQ_TRANSITION_NOTIFIER);
7230 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307231
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007232 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007233 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007234}
7235
Andi Kleendd60d212017-07-25 17:20:32 -07007236DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7237EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007238
Gleb Natapovf5132b02011-11-10 14:57:22 +02007239int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007240{
Alex,Shi086c9852011-10-20 15:34:01 +08007241 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007242}
7243
7244static int kvm_is_user_mode(void)
7245{
7246 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007247
Alex,Shi086c9852011-10-20 15:34:01 +08007248 if (__this_cpu_read(current_vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007249 user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007250
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007251 return user_mode != 0;
7252}
7253
7254static unsigned long kvm_get_guest_ip(void)
7255{
7256 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007257
Alex,Shi086c9852011-10-20 15:34:01 +08007258 if (__this_cpu_read(current_vcpu))
7259 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007260
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007261 return ip;
7262}
7263
Luwei Kang8479e042019-02-18 19:26:07 -05007264static void kvm_handle_intel_pt_intr(void)
7265{
7266 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7267
7268 kvm_make_request(KVM_REQ_PMI, vcpu);
7269 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7270 (unsigned long *)&vcpu->arch.pmu.global_status);
7271}
7272
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007273static struct perf_guest_info_callbacks kvm_guest_cbs = {
7274 .is_in_guest = kvm_is_in_guest,
7275 .is_user_mode = kvm_is_user_mode,
7276 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007277 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007278};
7279
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007280#ifdef CONFIG_X86_64
7281static void pvclock_gtod_update_fn(struct work_struct *work)
7282{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007283 struct kvm *kvm;
7284
7285 struct kvm_vcpu *vcpu;
7286 int i;
7287
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007288 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007289 list_for_each_entry(kvm, &vm_list, vm_list)
7290 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007291 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007292 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007293 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007294}
7295
7296static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7297
7298/*
7299 * Notification about pvclock gtod data update.
7300 */
7301static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7302 void *priv)
7303{
7304 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7305 struct timekeeper *tk = priv;
7306
7307 update_pvclock_gtod(tk);
7308
7309 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007310 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007311 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007312 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007313 atomic_read(&kvm_guest_has_master_clock) != 0)
7314 queue_work(system_long_wq, &pvclock_gtod_work);
7315
7316 return 0;
7317}
7318
7319static struct notifier_block pvclock_gtod_notifier = {
7320 .notifier_call = pvclock_gtod_notify,
7321};
7322#endif
7323
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007324int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007325{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007326 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007327 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007328
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007329 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007330 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007331 r = -EEXIST;
7332 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007333 }
7334
7335 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007336 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007337 r = -EOPNOTSUPP;
7338 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007339 }
7340 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007341 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007342 r = -EOPNOTSUPP;
7343 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007344 }
7345
Marc Orrb666a4b2018-11-06 14:53:56 -08007346 /*
7347 * KVM explicitly assumes that the guest has an FPU and
7348 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7349 * vCPU's FPU state as a fxregs_state struct.
7350 */
7351 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7352 printk(KERN_ERR "kvm: inadequate fpu\n");
7353 r = -EOPNOTSUPP;
7354 goto out;
7355 }
7356
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007357 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007358 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007359 __alignof__(struct fpu), SLAB_ACCOUNT,
7360 NULL);
7361 if (!x86_fpu_cache) {
7362 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7363 goto out;
7364 }
7365
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007366 x86_emulator_cache = kvm_alloc_emulator_cache();
7367 if (!x86_emulator_cache) {
7368 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7369 goto out_free_x86_fpu_cache;
7370 }
7371
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007372 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7373 if (!shared_msrs) {
7374 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007375 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007376 }
7377
Avi Kivity97db56c2008-01-13 13:23:56 +02007378 r = kvm_mmu_module_init();
7379 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007380 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007381
Sheng Yang7b523452008-04-25 21:13:50 +08007382 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007383 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007384 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007385 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007386
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007387 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7388
Sean Christophersoncfc48182020-03-02 15:56:23 -08007389 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007390 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007391 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7392 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007393
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007394 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007395 if (pi_inject_timer == -1)
7396 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007397#ifdef CONFIG_X86_64
7398 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007399
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007400 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007401 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007402#endif
7403
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007404 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007405
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007406out_free_percpu:
7407 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007408out_free_x86_emulator_cache:
7409 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007410out_free_x86_fpu_cache:
7411 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007412out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007413 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007414}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007415
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007416void kvm_arch_exit(void)
7417{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007418#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007419 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007420 clear_hv_tscchange_cb();
7421#endif
David Matlackcef84c32016-12-16 14:30:36 -08007422 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007423 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7424
Jan Kiszka888d2562009-04-17 19:24:58 +02007425 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7426 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7427 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007428 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007429#ifdef CONFIG_X86_64
7430 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7431#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007432 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007433 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007434 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007435 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007436}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007437
Joel Schopp5cb56052015-03-02 13:43:31 -06007438int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007439{
7440 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007441 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007442 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007443 return 1;
7444 } else {
7445 vcpu->run->exit_reason = KVM_EXIT_HLT;
7446 return 0;
7447 }
7448}
Joel Schopp5cb56052015-03-02 13:43:31 -06007449EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7450
7451int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7452{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007453 int ret = kvm_skip_emulated_instruction(vcpu);
7454 /*
7455 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7456 * KVM_EXIT_DEBUG here.
7457 */
7458 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007459}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007460EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7461
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007462#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007463static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7464 unsigned long clock_type)
7465{
7466 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007467 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007468 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007469 int ret;
7470
7471 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7472 return -KVM_EOPNOTSUPP;
7473
7474 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7475 return -KVM_EOPNOTSUPP;
7476
7477 clock_pairing.sec = ts.tv_sec;
7478 clock_pairing.nsec = ts.tv_nsec;
7479 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7480 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007481 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007482
7483 ret = 0;
7484 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7485 sizeof(struct kvm_clock_pairing)))
7486 ret = -KVM_EFAULT;
7487
7488 return ret;
7489}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007490#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007491
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307492/*
7493 * kvm_pv_kick_cpu_op: Kick a vcpu.
7494 *
7495 * @apicid - apicid of vcpu to be kicked.
7496 */
7497static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7498{
Raghavendra K T24d21662013-08-26 14:18:35 +05307499 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307500
Peter Xu150a84f2019-12-04 20:07:21 +01007501 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007502 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007503 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307504 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007505 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307506
Raghavendra K T24d21662013-08-26 14:18:35 +05307507 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007508 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307509}
7510
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007511bool kvm_apicv_activated(struct kvm *kvm)
7512{
7513 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7514}
7515EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7516
7517void kvm_apicv_init(struct kvm *kvm, bool enable)
7518{
7519 if (enable)
7520 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7521 &kvm->arch.apicv_inhibit_reasons);
7522 else
7523 set_bit(APICV_INHIBIT_REASON_DISABLE,
7524 &kvm->arch.apicv_inhibit_reasons);
7525}
7526EXPORT_SYMBOL_GPL(kvm_apicv_init);
7527
Wanpeng Li71506292019-06-11 20:23:49 +08007528static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7529{
7530 struct kvm_vcpu *target = NULL;
7531 struct kvm_apic_map *map;
7532
7533 rcu_read_lock();
7534 map = rcu_dereference(kvm->arch.apic_map);
7535
7536 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7537 target = map->phys_map[dest_id]->vcpu;
7538
7539 rcu_read_unlock();
7540
Wanpeng Li266e85a2019-07-24 17:43:13 +08007541 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007542 kvm_vcpu_yield_to(target);
7543}
7544
Hollis Blanchard8776e512007-10-31 17:24:24 -05007545int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7546{
7547 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007548 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007549
Radim Krčmář696ca772018-05-24 17:50:56 +02007550 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7551 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007552
Sean Christophersonde3cd112019-04-30 10:36:17 -07007553 nr = kvm_rax_read(vcpu);
7554 a0 = kvm_rbx_read(vcpu);
7555 a1 = kvm_rcx_read(vcpu);
7556 a2 = kvm_rdx_read(vcpu);
7557 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007558
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007559 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007560
Nadav Amita449c7a2014-06-18 17:19:24 +03007561 op_64_bit = is_64_bit_mode(vcpu);
7562 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007563 nr &= 0xFFFFFFFF;
7564 a0 &= 0xFFFFFFFF;
7565 a1 &= 0xFFFFFFFF;
7566 a2 &= 0xFFFFFFFF;
7567 a3 &= 0xFFFFFFFF;
7568 }
7569
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007570 if (kvm_x86_ops.get_cpl(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02007571 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007572 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007573 }
7574
Hollis Blanchard8776e512007-10-31 17:24:24 -05007575 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007576 case KVM_HC_VAPIC_POLL_IRQ:
7577 ret = 0;
7578 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307579 case KVM_HC_KICK_CPU:
7580 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007581 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307582 ret = 0;
7583 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007584#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007585 case KVM_HC_CLOCK_PAIRING:
7586 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7587 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007588#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007589 case KVM_HC_SEND_IPI:
7590 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7591 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007592 case KVM_HC_SCHED_YIELD:
7593 kvm_sched_yield(vcpu->kvm, a0);
7594 ret = 0;
7595 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007596 default:
7597 ret = -KVM_ENOSYS;
7598 break;
7599 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007600out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007601 if (!op_64_bit)
7602 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007603 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007604
Amit Shahf11c3a82008-02-21 01:00:30 +05307605 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007606 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007607}
7608EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7609
Jan Kiszkab6785de2012-09-20 07:43:17 +02007610static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007611{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007612 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007613 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007614 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007615
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007616 kvm_x86_ops.patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007617
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007618 return emulator_write_emulated(ctxt, rip, instruction, 3,
7619 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007620}
7621
Avi Kivity851ba692009-08-24 11:10:17 +03007622static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007623{
Matt Gingell782d4222015-11-16 15:26:00 -08007624 return vcpu->run->request_interrupt_window &&
7625 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007626}
7627
Avi Kivity851ba692009-08-24 11:10:17 +03007628static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007629{
Avi Kivity851ba692009-08-24 11:10:17 +03007630 struct kvm_run *kvm_run = vcpu->run;
7631
Jan Kiszka91586a32009-10-05 13:07:21 +02007632 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007633 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007634 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007635 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007636 kvm_run->ready_for_interrupt_injection =
7637 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007638 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007639}
7640
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007641static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7642{
7643 int max_irr, tpr;
7644
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007645 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007646 return;
7647
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007648 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007649 return;
7650
Andrey Smetanind62caab2015-11-10 15:36:33 +03007651 if (vcpu->arch.apicv_active)
7652 return;
7653
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007654 if (!vcpu->arch.apic->vapic_addr)
7655 max_irr = kvm_lapic_find_highest_irr(vcpu);
7656 else
7657 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007658
7659 if (max_irr != -1)
7660 max_irr >>= 4;
7661
7662 tpr = kvm_lapic_get_cr8(vcpu);
7663
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007664 kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007665}
7666
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007667static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007668{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007669 int r;
7670
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007671 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007672
Liran Alon1a680e32018-03-23 03:01:33 +03007673 if (vcpu->arch.exception.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007674 kvm_x86_ops.queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007675 /*
Liran Alona042c262018-03-23 03:01:32 +03007676 * Do not inject an NMI or interrupt if there is a pending
7677 * exception. Exceptions and interrupts are recognized at
7678 * instruction boundaries, i.e. the start of an instruction.
7679 * Trap-like exceptions, e.g. #DB, have higher priority than
7680 * NMIs and interrupts, i.e. traps are recognized before an
7681 * NMI/interrupt that's pending on the same instruction.
7682 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7683 * priority, but are only generated (pended) during instruction
7684 * execution, i.e. a pending fault-like exception means the
7685 * fault occurred on the *previous* instruction and must be
7686 * serviced prior to recognizing any new events in order to
7687 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007688 */
Liran Alon1a680e32018-03-23 03:01:33 +03007689 else if (!vcpu->arch.exception.pending) {
7690 if (vcpu->arch.nmi_injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007691 kvm_x86_ops.set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007692 else if (vcpu->arch.interrupt.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007693 kvm_x86_ops.set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007694 }
7695
Liran Alon1a680e32018-03-23 03:01:33 +03007696 /*
7697 * Call check_nested_events() even if we reinjected a previous event
7698 * in order for caller to determine if it should require immediate-exit
7699 * from L2 to L1 due to pending L1 events which require exit
7700 * from L2 to L1.
7701 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007702 if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events) {
7703 r = kvm_x86_ops.check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007704 if (r != 0)
7705 return r;
7706 }
7707
7708 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007709 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007710 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7711 vcpu->arch.exception.has_error_code,
7712 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007713
Liran Alon1a680e32018-03-23 03:01:33 +03007714 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007715 vcpu->arch.exception.pending = false;
7716 vcpu->arch.exception.injected = true;
7717
Nadav Amitd6e8c852014-07-24 14:51:24 +03007718 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7719 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7720 X86_EFLAGS_RF);
7721
Jim Mattsonf10c7292018-10-16 14:29:23 -07007722 if (vcpu->arch.exception.nr == DB_VECTOR) {
7723 /*
7724 * This code assumes that nSVM doesn't use
7725 * check_nested_events(). If it does, the
7726 * DR6/DR7 changes should happen before L1
7727 * gets a #VMEXIT for an intercepted #DB in
7728 * L2. (Under VMX, on the other hand, the
7729 * DR6/DR7 changes should not happen in the
7730 * event of a VM-exit to L1 for an intercepted
7731 * #DB in L2.)
7732 */
7733 kvm_deliver_exception_payload(vcpu);
7734 if (vcpu->arch.dr7 & DR7_GD) {
7735 vcpu->arch.dr7 &= ~DR7_GD;
7736 kvm_update_dr7(vcpu);
7737 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007738 }
7739
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007740 kvm_x86_ops.queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007741 }
7742
7743 /* Don't consider new event if we re-injected an event */
7744 if (kvm_event_needs_reinjection(vcpu))
7745 return 0;
7746
7747 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007748 kvm_x86_ops.smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007749 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007750 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007751 enter_smm(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007752 } else if (vcpu->arch.nmi_pending && kvm_x86_ops.nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007753 --vcpu->arch.nmi_pending;
7754 vcpu->arch.nmi_injected = true;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007755 kvm_x86_ops.set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007756 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007757 /*
7758 * Because interrupts can be injected asynchronously, we are
7759 * calling check_nested_events again here to avoid a race condition.
7760 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7761 * proposal and current concerns. Perhaps we should be setting
7762 * KVM_REQ_EVENT only on certain events and not unconditionally?
7763 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007764 if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events) {
7765 r = kvm_x86_ops.check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007766 if (r != 0)
7767 return r;
7768 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007769 if (kvm_x86_ops.interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007770 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7771 false);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007772 kvm_x86_ops.set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007773 }
7774 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007775
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007776 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007777}
7778
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007779static void process_nmi(struct kvm_vcpu *vcpu)
7780{
7781 unsigned limit = 2;
7782
7783 /*
7784 * x86 is limited to one NMI running, and one NMI pending after it.
7785 * If an NMI is already in progress, limit further NMIs to just one.
7786 * Otherwise, allow two (and we'll inject the first one immediately).
7787 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007788 if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007789 limit = 1;
7790
7791 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7792 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7793 kvm_make_request(KVM_REQ_EVENT, vcpu);
7794}
7795
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007796static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007797{
7798 u32 flags = 0;
7799 flags |= seg->g << 23;
7800 flags |= seg->db << 22;
7801 flags |= seg->l << 21;
7802 flags |= seg->avl << 20;
7803 flags |= seg->present << 15;
7804 flags |= seg->dpl << 13;
7805 flags |= seg->s << 12;
7806 flags |= seg->type << 8;
7807 return flags;
7808}
7809
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007810static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007811{
7812 struct kvm_segment seg;
7813 int offset;
7814
7815 kvm_get_segment(vcpu, &seg, n);
7816 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7817
7818 if (n < 3)
7819 offset = 0x7f84 + n * 12;
7820 else
7821 offset = 0x7f2c + (n - 3) * 12;
7822
7823 put_smstate(u32, buf, offset + 8, seg.base);
7824 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007825 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007826}
7827
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007828#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007829static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007830{
7831 struct kvm_segment seg;
7832 int offset;
7833 u16 flags;
7834
7835 kvm_get_segment(vcpu, &seg, n);
7836 offset = 0x7e00 + n * 16;
7837
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007838 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007839 put_smstate(u16, buf, offset, seg.selector);
7840 put_smstate(u16, buf, offset + 2, flags);
7841 put_smstate(u32, buf, offset + 4, seg.limit);
7842 put_smstate(u64, buf, offset + 8, seg.base);
7843}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007844#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007845
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007846static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007847{
7848 struct desc_ptr dt;
7849 struct kvm_segment seg;
7850 unsigned long val;
7851 int i;
7852
7853 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7854 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7855 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7856 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7857
7858 for (i = 0; i < 8; i++)
7859 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7860
7861 kvm_get_dr(vcpu, 6, &val);
7862 put_smstate(u32, buf, 0x7fcc, (u32)val);
7863 kvm_get_dr(vcpu, 7, &val);
7864 put_smstate(u32, buf, 0x7fc8, (u32)val);
7865
7866 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7867 put_smstate(u32, buf, 0x7fc4, seg.selector);
7868 put_smstate(u32, buf, 0x7f64, seg.base);
7869 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007870 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007871
7872 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7873 put_smstate(u32, buf, 0x7fc0, seg.selector);
7874 put_smstate(u32, buf, 0x7f80, seg.base);
7875 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007876 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007877
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007878 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007879 put_smstate(u32, buf, 0x7f74, dt.address);
7880 put_smstate(u32, buf, 0x7f70, dt.size);
7881
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007882 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007883 put_smstate(u32, buf, 0x7f58, dt.address);
7884 put_smstate(u32, buf, 0x7f54, dt.size);
7885
7886 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007887 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007888
7889 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7890
7891 /* revision id */
7892 put_smstate(u32, buf, 0x7efc, 0x00020000);
7893 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7894}
7895
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007896#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007897static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007898{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007899 struct desc_ptr dt;
7900 struct kvm_segment seg;
7901 unsigned long val;
7902 int i;
7903
7904 for (i = 0; i < 16; i++)
7905 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7906
7907 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7908 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7909
7910 kvm_get_dr(vcpu, 6, &val);
7911 put_smstate(u64, buf, 0x7f68, val);
7912 kvm_get_dr(vcpu, 7, &val);
7913 put_smstate(u64, buf, 0x7f60, val);
7914
7915 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7916 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7917 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7918
7919 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7920
7921 /* revision id */
7922 put_smstate(u32, buf, 0x7efc, 0x00020064);
7923
7924 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7925
7926 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7927 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007928 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007929 put_smstate(u32, buf, 0x7e94, seg.limit);
7930 put_smstate(u64, buf, 0x7e98, seg.base);
7931
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007932 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007933 put_smstate(u32, buf, 0x7e84, dt.size);
7934 put_smstate(u64, buf, 0x7e88, dt.address);
7935
7936 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7937 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007938 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007939 put_smstate(u32, buf, 0x7e74, seg.limit);
7940 put_smstate(u64, buf, 0x7e78, seg.base);
7941
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007942 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007943 put_smstate(u32, buf, 0x7e64, dt.size);
7944 put_smstate(u64, buf, 0x7e68, dt.address);
7945
7946 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007947 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007948}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007949#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007950
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007951static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007952{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007953 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007954 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007955 char buf[512];
7956 u32 cr0;
7957
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007958 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007959 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007960#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007961 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007962 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007963 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007964#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007965 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007966
Ladi Prosek0234bf82017-10-11 16:54:40 +02007967 /*
7968 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7969 * vCPU state (e.g. leave guest mode) after we've saved the state into
7970 * the SMM state-save area.
7971 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007972 kvm_x86_ops.pre_enter_smm(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007973
7974 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007975 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007976
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007977 if (kvm_x86_ops.get_nmi_mask(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007978 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7979 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007980 kvm_x86_ops.set_nmi_mask(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007981
7982 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7983 kvm_rip_write(vcpu, 0x8000);
7984
7985 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007986 kvm_x86_ops.set_cr0(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007987 vcpu->arch.cr0 = cr0;
7988
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007989 kvm_x86_ops.set_cr4(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007990
Paolo Bonzini18c36262015-08-07 12:27:54 +02007991 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7992 dt.address = dt.size = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007993 kvm_x86_ops.set_idt(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02007994
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007995 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7996
7997 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7998 cs.base = vcpu->arch.smbase;
7999
8000 ds.selector = 0;
8001 ds.base = 0;
8002
8003 cs.limit = ds.limit = 0xffffffff;
8004 cs.type = ds.type = 0x3;
8005 cs.dpl = ds.dpl = 0;
8006 cs.db = ds.db = 0;
8007 cs.s = ds.s = 1;
8008 cs.l = ds.l = 0;
8009 cs.g = ds.g = 1;
8010 cs.avl = ds.avl = 0;
8011 cs.present = ds.present = 1;
8012 cs.unusable = ds.unusable = 0;
8013 cs.padding = ds.padding = 0;
8014
8015 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8016 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8017 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8018 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8019 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8020 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8021
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008022#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008023 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008024 kvm_x86_ops.set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008025#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008026
8027 kvm_update_cpuid(vcpu);
8028 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008029}
8030
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008031static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008032{
8033 vcpu->arch.smi_pending = true;
8034 kvm_make_request(KVM_REQ_EVENT, vcpu);
8035}
8036
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008037void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8038 unsigned long *vcpu_bitmap)
8039{
8040 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008041
8042 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8043
Mao Wenandb5a95e2019-11-19 11:06:40 +08008044 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
8045 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008046
8047 free_cpumask_var(cpus);
8048}
8049
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008050void kvm_make_scan_ioapic_request(struct kvm *kvm)
8051{
8052 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8053}
8054
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008055void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8056{
8057 if (!lapic_in_kernel(vcpu))
8058 return;
8059
8060 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8061 kvm_apic_update_apicv(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008062 kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008063}
8064EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8065
8066/*
8067 * NOTE: Do not hold any lock prior to calling this.
8068 *
8069 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8070 * locked, because it calls __x86_set_memory_region() which does
8071 * synchronize_srcu(&kvm->srcu).
8072 */
8073void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8074{
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008075 unsigned long old, new, expected;
8076
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008077 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
8078 !kvm_x86_ops.check_apicv_inhibit_reasons(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008079 return;
8080
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008081 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8082 do {
8083 expected = new = old;
8084 if (activate)
8085 __clear_bit(bit, &new);
8086 else
8087 __set_bit(bit, &new);
8088 if (new == old)
8089 break;
8090 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8091 } while (old != expected);
8092
8093 if (!!old == !!new)
8094 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008095
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008096 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008097 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
8098 kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008099 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
8100}
8101EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8102
Yang Zhang3d81bc72013-04-11 19:25:13 +08008103static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008104{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008105 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008106 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008107
Andrey Smetanin63086302015-11-10 15:36:32 +03008108 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008109
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008110 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008111 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008112 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008113 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008114 kvm_x86_ops.sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008115 if (ioapic_in_kernel(vcpu->kvm))
8116 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008117 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008118
8119 if (is_guest_mode(vcpu))
8120 vcpu->arch.load_eoi_exitmap_pending = true;
8121 else
8122 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8123}
8124
8125static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8126{
8127 u64 eoi_exit_bitmap[4];
8128
8129 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8130 return;
8131
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008132 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8133 vcpu_to_synic(vcpu)->vec_bitmap, 256);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008134 kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008135}
8136
Michal Hocko93065ac2018-08-21 21:52:33 -07008137int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8138 unsigned long start, unsigned long end,
8139 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008140{
8141 unsigned long apic_address;
8142
8143 /*
8144 * The physical address of apic access page is stored in the VMCS.
8145 * Update it when it becomes invalid.
8146 */
8147 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8148 if (start <= apic_address && apic_address < end)
8149 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008150
8151 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008152}
8153
Tang Chen4256f432014-09-24 15:57:54 +08008154void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8155{
Paolo Bonzini35754c92015-07-29 12:05:37 +02008156 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008157 return;
8158
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008159 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08008160 return;
8161
Sean Christophersona4148b72020-03-20 14:28:24 -07008162 kvm_x86_ops.set_apic_access_page_addr(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008163}
Tang Chen4256f432014-09-24 15:57:54 +08008164
Sean Christophersond264ee02018-08-27 15:21:12 -07008165void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8166{
8167 smp_send_reschedule(vcpu->cpu);
8168}
8169EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8170
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008171/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008172 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008173 * exiting to the userspace. Otherwise, the value will be returned to the
8174 * userspace.
8175 */
Avi Kivity851ba692009-08-24 11:10:17 +03008176static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008177{
8178 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008179 bool req_int_win =
8180 dm_request_for_irq_injection(vcpu) &&
8181 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008182 enum exit_fastpath_completion exit_fastpath;
Matt Gingell62a193e2015-11-16 15:26:07 -08008183
Jan Kiszka730dca42013-04-28 10:50:52 +02008184 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008185
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008186 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008187 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008188 if (unlikely(!kvm_x86_ops.get_vmcs12_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008189 r = 0;
8190 goto out;
8191 }
8192 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008193 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008194 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008195 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008196 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008197 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8198 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008199 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8200 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008201 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8202 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008203 if (unlikely(r))
8204 goto out;
8205 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008206 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008207 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008208 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8209 kvm_mmu_load_pgd(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008210 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
Sean Christopherson77809382020-03-20 14:28:18 -07008211 kvm_vcpu_flush_tlb_all(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008212
8213 /* Flushing all ASIDs flushes the current ASID... */
8214 kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
8215 }
8216 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
8217 kvm_vcpu_flush_tlb_current(vcpu);
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04008218 if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
8219 kvm_vcpu_flush_tlb_guest(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07008220
Avi Kivitya8eeb042010-05-10 12:34:53 +03008221 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008222 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008223 r = 0;
8224 goto out;
8225 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008226 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008227 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008228 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008229 r = 0;
8230 goto out;
8231 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008232 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8233 /* Page is swapped out. Do synthetic halt */
8234 vcpu->arch.apf.halted = true;
8235 r = 1;
8236 goto out;
8237 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008238 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8239 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008240 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8241 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008242 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8243 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008244 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008245 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008246 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008247 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008248 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8249 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8250 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008251 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008252 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8253 vcpu->run->eoi.vector =
8254 vcpu->arch.pending_ioapic_eoi;
8255 r = 0;
8256 goto out;
8257 }
8258 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008259 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8260 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008261 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8262 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008263 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8264 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008265 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8266 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8267 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8268 r = 0;
8269 goto out;
8270 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008271 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8272 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8273 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8274 r = 0;
8275 goto out;
8276 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008277 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8278 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8279 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8280 r = 0;
8281 goto out;
8282 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008283
8284 /*
8285 * KVM_REQ_HV_STIMER has to be processed after
8286 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8287 * depend on the guest clock being up-to-date
8288 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008289 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8290 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008291 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8292 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008293 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008294
Avi Kivityb463a6f2010-07-20 15:06:17 +03008295 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008296 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008297 kvm_apic_accept_events(vcpu);
8298 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8299 r = 1;
8300 goto out;
8301 }
8302
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008303 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008304 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008305 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008306 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008307 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008308 * SMIs have three cases:
8309 * 1) They can be nested, and then there is nothing to
8310 * do here because RSM will cause a vmexit anyway.
8311 * 2) There is an ISA-specific reason why SMI cannot be
8312 * injected, and the moment when this changes can be
8313 * intercepted.
8314 * 3) Or the SMI can be pending because
8315 * inject_pending_event has completed the injection
8316 * of an IRQ or NMI from the previous vmexit, and
8317 * then we request an immediate exit to inject the
8318 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008319 */
8320 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008321 if (!kvm_x86_ops.enable_smi_window(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008322 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008323 if (vcpu->arch.nmi_pending)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008324 kvm_x86_ops.enable_nmi_window(vcpu);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008325 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008326 kvm_x86_ops.enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008327 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008328 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008329
Avi Kivity3842d132010-07-27 12:30:24 +03008330 if (kvm_lapic_enabled(vcpu)) {
8331 update_cr8_intercept(vcpu);
8332 kvm_lapic_sync_to_vapic(vcpu);
8333 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008334 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008335
Avi Kivityd8368af2012-05-14 18:07:56 +03008336 r = kvm_mmu_reload(vcpu);
8337 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008338 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008339 }
8340
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008341 preempt_disable();
8342
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008343 kvm_x86_ops.prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008344
8345 /*
8346 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8347 * IPI are then delayed after guest entry, which ensures that they
8348 * result in virtual interrupt delivery.
8349 */
8350 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008351 vcpu->mode = IN_GUEST_MODE;
8352
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008353 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8354
Lan Tianyu0f127d12016-03-13 11:10:29 +08008355 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008356 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008357 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008358 *
Luwei Kang81b01662019-01-31 16:52:02 +08008359 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008360 * pairs with the memory barrier implicit in pi_test_and_set_on
8361 * (see vmx_deliver_posted_interrupt).
8362 *
8363 * 3) This also orders the write to mode from any reads to the page
8364 * tables done while the VCPU is running. Please see the comment
8365 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008366 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008367 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008368
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008369 /*
8370 * This handles the case where a posted interrupt was
8371 * notified with kvm_vcpu_kick.
8372 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008373 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008374 kvm_x86_ops.sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008375
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008376 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008377 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008378 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008379 smp_wmb();
8380 local_irq_enable();
8381 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008382 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008383 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008384 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008385 }
8386
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008387 if (req_immediate_exit) {
8388 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008389 kvm_x86_ops.request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008390 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008391
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008392 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008393 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008394
Sean Christopherson2620fe22020-01-17 11:30:51 -08008395 fpregs_assert_state_consistent();
8396 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8397 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008398
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008399 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008400 set_debugreg(0, 7);
8401 set_debugreg(vcpu->arch.eff_db[0], 0);
8402 set_debugreg(vcpu->arch.eff_db[1], 1);
8403 set_debugreg(vcpu->arch.eff_db[2], 2);
8404 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008405 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008406 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008407 }
8408
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008409 exit_fastpath = kvm_x86_ops.run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008410
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008411 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008412 * Do this here before restoring debug registers on the host. And
8413 * since we do this before handling the vmexit, a DR access vmexit
8414 * can (a) read the correct value of the debug registers, (b) set
8415 * KVM_DEBUGREG_WONT_EXIT again.
8416 */
8417 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008418 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008419 kvm_x86_ops.sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008420 kvm_update_dr0123(vcpu);
8421 kvm_update_dr6(vcpu);
8422 kvm_update_dr7(vcpu);
8423 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008424 }
8425
8426 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008427 * If the guest has used debug registers, at least dr7
8428 * will be disabled while returning to the host.
8429 * If we don't have active breakpoints in the host, we don't
8430 * care about the messed up debug address registers. But if
8431 * we have some of them active, restore the old state.
8432 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008433 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008434 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008435
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008436 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008437
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008438 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008439 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008440
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07008441 kvm_x86_ops.handle_exit_irqoff(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008442
Sean Christophersond7a08882019-07-10 09:07:34 -07008443 /*
8444 * Consume any pending interrupts, including the possible source of
8445 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8446 * An instruction is required after local_irq_enable() to fully unblock
8447 * interrupts on processors that implement an interrupt shadow, the
8448 * stat.exits increment will do nicely.
8449 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008450 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008451 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008452 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008453 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008454 kvm_after_interrupt(vcpu);
8455
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008456 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008457 if (lapic_in_kernel(vcpu)) {
8458 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8459 if (delta != S64_MIN) {
8460 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8461 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8462 }
8463 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008464
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008465 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008466 preempt_enable();
8467
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008468 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008469
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008470 /*
8471 * Profile KVM exit RIPs:
8472 */
8473 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008474 unsigned long rip = kvm_rip_read(vcpu);
8475 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008476 }
8477
Zachary Amsdencc578282012-02-03 15:43:50 -02008478 if (unlikely(vcpu->arch.tsc_always_catchup))
8479 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008480
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008481 if (vcpu->arch.apic_attention)
8482 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008483
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008484 r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008485 return r;
8486
8487cancel_injection:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008488 kvm_x86_ops.cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008489 if (unlikely(vcpu->arch.apic_attention))
8490 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008491out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008492 return r;
8493}
8494
Paolo Bonzini362c6982015-02-06 12:48:04 +01008495static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8496{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008497 if (!kvm_arch_vcpu_runnable(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008498 (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008499 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8500 kvm_vcpu_block(vcpu);
8501 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008502
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008503 if (kvm_x86_ops.post_block)
8504 kvm_x86_ops.post_block(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008505
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008506 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8507 return 1;
8508 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008509
Paolo Bonzini362c6982015-02-06 12:48:04 +01008510 kvm_apic_accept_events(vcpu);
8511 switch(vcpu->arch.mp_state) {
8512 case KVM_MP_STATE_HALTED:
8513 vcpu->arch.pv.pv_unhalted = false;
8514 vcpu->arch.mp_state =
8515 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008516 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008517 case KVM_MP_STATE_RUNNABLE:
8518 vcpu->arch.apf.halted = false;
8519 break;
8520 case KVM_MP_STATE_INIT_RECEIVED:
8521 break;
8522 default:
8523 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008524 }
8525 return 1;
8526}
8527
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008528static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8529{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008530 if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events)
8531 kvm_x86_ops.check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008532
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008533 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8534 !vcpu->arch.apf.halted);
8535}
8536
Paolo Bonzini362c6982015-02-06 12:48:04 +01008537static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008538{
8539 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008540 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008541
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008542 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008543 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008544
Paolo Bonzini362c6982015-02-06 12:48:04 +01008545 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008546 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008547 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008548 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008549 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008550 }
8551
Gleb Natapov09cec752009-03-23 15:11:44 +02008552 if (r <= 0)
8553 break;
8554
Radim Krčmář72875d82017-04-26 22:32:19 +02008555 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008556 if (kvm_cpu_has_pending_timer(vcpu))
8557 kvm_inject_pending_timer_irqs(vcpu);
8558
Matt Gingell782d4222015-11-16 15:26:00 -08008559 if (dm_request_for_irq_injection(vcpu) &&
8560 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008561 r = 0;
8562 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008563 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008564 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008565 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008566
8567 kvm_check_async_pf_completion(vcpu);
8568
Gleb Natapov09cec752009-03-23 15:11:44 +02008569 if (signal_pending(current)) {
8570 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008571 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008572 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008573 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008574 }
8575 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008576 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008577 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008578 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008579 }
8580 }
8581
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008582 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008583
8584 return r;
8585}
8586
Gleb Natapov716d51a2012-09-03 15:24:26 +03008587static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8588{
8589 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008590
Gleb Natapov716d51a2012-09-03 15:24:26 +03008591 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008592 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008593 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008594 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008595}
8596
8597static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8598{
8599 BUG_ON(!vcpu->arch.pio.count);
8600
8601 return complete_emulated_io(vcpu);
8602}
8603
Avi Kivityf78146b2012-04-18 19:22:47 +03008604/*
8605 * Implements the following, as a state machine:
8606 *
8607 * read:
8608 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008609 * for each mmio piece in the fragment
8610 * write gpa, len
8611 * exit
8612 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008613 * execute insn
8614 *
8615 * write:
8616 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008617 * for each mmio piece in the fragment
8618 * write gpa, len
8619 * copy data
8620 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008621 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008622static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008623{
8624 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008625 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008626 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008627
Gleb Natapov716d51a2012-09-03 15:24:26 +03008628 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008629
Gleb Natapov716d51a2012-09-03 15:24:26 +03008630 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008631 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8632 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008633 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008634 memcpy(frag->data, run->mmio.data, len);
8635
8636 if (frag->len <= 8) {
8637 /* Switch to the next fragment. */
8638 frag++;
8639 vcpu->mmio_cur_fragment++;
8640 } else {
8641 /* Go forward to the next mmio piece. */
8642 frag->data += len;
8643 frag->gpa += len;
8644 frag->len -= len;
8645 }
8646
Andrew Honiga08d3b32014-02-27 19:35:14 +01008647 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008648 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008649
8650 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008651 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008652 return 1;
8653 vcpu->mmio_read_completed = 1;
8654 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008655 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008656
Gleb Natapov716d51a2012-09-03 15:24:26 +03008657 run->exit_reason = KVM_EXIT_MMIO;
8658 run->mmio.phys_addr = frag->gpa;
8659 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008660 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8661 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008662 run->mmio.is_write = vcpu->mmio_is_write;
8663 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8664 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008665}
8666
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008667static void kvm_save_current_fpu(struct fpu *fpu)
8668{
8669 /*
8670 * If the target FPU state is not resident in the CPU registers, just
8671 * memcpy() from current, else save CPU state directly to the target.
8672 */
8673 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8674 memcpy(&fpu->state, &current->thread.fpu.state,
8675 fpu_kernel_xstate_size);
8676 else
8677 copy_fpregs_to_fpstate(fpu);
8678}
8679
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008680/* Swap (qemu) user FPU context for the guest FPU context. */
8681static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8682{
Rik van Riel5f409e22019-04-03 18:41:52 +02008683 fpregs_lock();
8684
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008685 kvm_save_current_fpu(vcpu->arch.user_fpu);
8686
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008687 /* PKRU is separately restored in kvm_x86_ops.run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008688 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008689 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008690
8691 fpregs_mark_activate();
8692 fpregs_unlock();
8693
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008694 trace_kvm_fpu(1);
8695}
8696
8697/* When vcpu_run ends, restore user space FPU context. */
8698static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8699{
Rik van Riel5f409e22019-04-03 18:41:52 +02008700 fpregs_lock();
8701
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008702 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8703
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008704 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008705
8706 fpregs_mark_activate();
8707 fpregs_unlock();
8708
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008709 ++vcpu->stat.fpu_reload;
8710 trace_kvm_fpu(0);
8711}
8712
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008713int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008714{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008715 struct kvm_run *kvm_run = vcpu->run;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008716 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008717
Christoffer Dallaccb7572017-12-04 21:35:25 +01008718 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008719 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008720 kvm_load_guest_fpu(vcpu);
8721
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008722 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008723 if (kvm_run->immediate_exit) {
8724 r = -EINTR;
8725 goto out;
8726 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008727 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008728 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008729 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008730 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008731 if (signal_pending(current)) {
8732 r = -EINTR;
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008733 kvm_run->exit_reason = KVM_EXIT_INTR;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008734 ++vcpu->stat.signal_exits;
8735 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008736 goto out;
8737 }
8738
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008739 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008740 r = -EINVAL;
8741 goto out;
8742 }
8743
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008744 if (kvm_run->kvm_dirty_regs) {
Ken Hofsass01643c52018-01-31 16:03:36 -08008745 r = sync_regs(vcpu);
8746 if (r != 0)
8747 goto out;
8748 }
8749
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008750 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008751 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008752 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8753 r = -EINVAL;
8754 goto out;
8755 }
8756 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008757
Gleb Natapov716d51a2012-09-03 15:24:26 +03008758 if (unlikely(vcpu->arch.complete_userspace_io)) {
8759 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8760 vcpu->arch.complete_userspace_io = NULL;
8761 r = cui(vcpu);
8762 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008763 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008764 } else
8765 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008766
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008767 if (kvm_run->immediate_exit)
8768 r = -EINTR;
8769 else
8770 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008771
8772out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008773 kvm_put_guest_fpu(vcpu);
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08008774 if (kvm_run->kvm_valid_regs)
Ken Hofsass01643c52018-01-31 16:03:36 -08008775 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008776 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008777 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008778
Christoffer Dallaccb7572017-12-04 21:35:25 +01008779 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008780 return r;
8781}
8782
Ken Hofsass01643c52018-01-31 16:03:36 -08008783static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008784{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008785 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8786 /*
8787 * We are here if userspace calls get_regs() in the middle of
8788 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008789 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008790 * that usually, but some bad designed PV devices (vmware
8791 * backdoor interface) need this to work
8792 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008793 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008794 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8795 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008796 regs->rax = kvm_rax_read(vcpu);
8797 regs->rbx = kvm_rbx_read(vcpu);
8798 regs->rcx = kvm_rcx_read(vcpu);
8799 regs->rdx = kvm_rdx_read(vcpu);
8800 regs->rsi = kvm_rsi_read(vcpu);
8801 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008802 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008803 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008804#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008805 regs->r8 = kvm_r8_read(vcpu);
8806 regs->r9 = kvm_r9_read(vcpu);
8807 regs->r10 = kvm_r10_read(vcpu);
8808 regs->r11 = kvm_r11_read(vcpu);
8809 regs->r12 = kvm_r12_read(vcpu);
8810 regs->r13 = kvm_r13_read(vcpu);
8811 regs->r14 = kvm_r14_read(vcpu);
8812 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008813#endif
8814
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008815 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008816 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008817}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008818
Ken Hofsass01643c52018-01-31 16:03:36 -08008819int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8820{
8821 vcpu_load(vcpu);
8822 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008823 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008824 return 0;
8825}
8826
Ken Hofsass01643c52018-01-31 16:03:36 -08008827static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008828{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008829 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8830 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8831
Sean Christophersonde3cd112019-04-30 10:36:17 -07008832 kvm_rax_write(vcpu, regs->rax);
8833 kvm_rbx_write(vcpu, regs->rbx);
8834 kvm_rcx_write(vcpu, regs->rcx);
8835 kvm_rdx_write(vcpu, regs->rdx);
8836 kvm_rsi_write(vcpu, regs->rsi);
8837 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008838 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008839 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008840#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008841 kvm_r8_write(vcpu, regs->r8);
8842 kvm_r9_write(vcpu, regs->r9);
8843 kvm_r10_write(vcpu, regs->r10);
8844 kvm_r11_write(vcpu, regs->r11);
8845 kvm_r12_write(vcpu, regs->r12);
8846 kvm_r13_write(vcpu, regs->r13);
8847 kvm_r14_write(vcpu, regs->r14);
8848 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008849#endif
8850
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008851 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008852 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008853
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008854 vcpu->arch.exception.pending = false;
8855
Avi Kivity3842d132010-07-27 12:30:24 +03008856 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008857}
Avi Kivity3842d132010-07-27 12:30:24 +03008858
Ken Hofsass01643c52018-01-31 16:03:36 -08008859int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8860{
8861 vcpu_load(vcpu);
8862 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008863 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008864 return 0;
8865}
8866
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008867void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8868{
8869 struct kvm_segment cs;
8870
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008871 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008872 *db = cs.db;
8873 *l = cs.l;
8874}
8875EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8876
Ken Hofsass01643c52018-01-31 16:03:36 -08008877static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008878{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008879 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008880
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008881 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8882 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8883 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8884 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8885 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8886 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008887
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008888 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8889 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008890
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008891 kvm_x86_ops.get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008892 sregs->idt.limit = dt.size;
8893 sregs->idt.base = dt.address;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008894 kvm_x86_ops.get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008895 sregs->gdt.limit = dt.size;
8896 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008897
Avi Kivity4d4ec082009-12-29 18:07:30 +02008898 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008899 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008900 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008901 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008902 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008903 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008904 sregs->apic_base = kvm_get_apic_base(vcpu);
8905
Jordan Borgner0e96f312018-10-28 12:58:28 +00008906 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008907
Liran Alon04140b42018-03-23 03:01:31 +03008908 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008909 set_bit(vcpu->arch.interrupt.nr,
8910 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008911}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008912
Ken Hofsass01643c52018-01-31 16:03:36 -08008913int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8914 struct kvm_sregs *sregs)
8915{
8916 vcpu_load(vcpu);
8917 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008918 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008919 return 0;
8920}
8921
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008922int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8923 struct kvm_mp_state *mp_state)
8924{
Christoffer Dallfd232562017-12-04 21:35:30 +01008925 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008926 if (kvm_mpx_supported())
8927 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008928
Jan Kiszka66450a22013-03-13 12:42:34 +01008929 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308930 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8931 vcpu->arch.pv.pv_unhalted)
8932 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8933 else
8934 mp_state->mp_state = vcpu->arch.mp_state;
8935
Sean Christophersonf958bd22019-12-09 12:19:31 -08008936 if (kvm_mpx_supported())
8937 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008938 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008939 return 0;
8940}
8941
8942int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8943 struct kvm_mp_state *mp_state)
8944{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008945 int ret = -EINVAL;
8946
8947 vcpu_load(vcpu);
8948
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008949 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008950 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008951 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008952
Liran Alon27cbe7d2019-11-11 11:16:40 +02008953 /*
8954 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8955 * INIT state; latched init should be reported using
8956 * KVM_SET_VCPU_EVENTS, so reject it here.
8957 */
8958 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008959 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8960 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008961 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008962
Jan Kiszka66450a22013-03-13 12:42:34 +01008963 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8964 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8965 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8966 } else
8967 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008968 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008969
8970 ret = 0;
8971out:
8972 vcpu_put(vcpu);
8973 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008974}
8975
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008976int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8977 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008978{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008979 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008980 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008981
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008982 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008983
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008984 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008985 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008986 if (ret) {
8987 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8988 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8989 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008990 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008991 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008992
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008993 kvm_rip_write(vcpu, ctxt->eip);
8994 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008995 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008996}
8997EXPORT_SYMBOL_GPL(kvm_task_switch);
8998
Peng Hao3140c152018-04-02 09:15:32 +08008999static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009000{
Tianyu Lan37b95952018-01-16 17:34:07 +08009001 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009002 /*
9003 * When EFER.LME and CR0.PG are set, the processor is in
9004 * 64-bit mode (though maybe in a 32-bit code segment).
9005 * CR4.PAE and EFER.LMA must be set.
9006 */
Tianyu Lan37b95952018-01-16 17:34:07 +08009007 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05009008 || !(sregs->efer & EFER_LMA))
9009 return -EINVAL;
9010 } else {
9011 /*
9012 * Not in 64-bit mode: EFER.LMA is clear and the code
9013 * segment cannot be 64-bit.
9014 */
9015 if (sregs->efer & EFER_LMA || sregs->cs.l)
9016 return -EINVAL;
9017 }
9018
Wanpeng Li3ca94192019-09-18 17:50:10 +08009019 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009020}
9021
Ken Hofsass01643c52018-01-31 16:03:36 -08009022static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009023{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009024 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009025 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009026 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009027 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009028 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009029 int ret = -EINVAL;
9030
Lan Tianyuf2981032017-12-14 03:01:52 -05009031 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009032 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009033
Jim Mattsond3802282017-08-10 10:14:13 -07009034 apic_base_msr.data = sregs->apic_base;
9035 apic_base_msr.host_initiated = true;
9036 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009037 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009038
Gleb Natapov89a27f42010-02-16 10:51:48 +02009039 dt.size = sregs->idt.limit;
9040 dt.address = sregs->idt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009041 kvm_x86_ops.set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009042 dt.size = sregs->gdt.limit;
9043 dt.address = sregs->gdt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009044 kvm_x86_ops.set_gdt(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009045
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009046 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009047 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009048 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009049 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009050
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009051 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009052
Avi Kivityf6801df2010-01-21 15:31:50 +02009053 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009054 kvm_x86_ops.set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009055
Avi Kivity4d4ec082009-12-29 18:07:30 +02009056 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009057 kvm_x86_ops.set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009058 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009059
Avi Kivityfc78f512009-12-07 12:16:48 +02009060 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009061 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9062 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009063 kvm_x86_ops.set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009064 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009065 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009066
9067 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009068 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009069 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009070 mmu_reset_needed = 1;
9071 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009072 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009073
9074 if (mmu_reset_needed)
9075 kvm_mmu_reset_context(vcpu);
9076
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009077 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009078 pending_vec = find_first_bit(
9079 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9080 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009081 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009082 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009083 }
9084
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009085 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9086 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9087 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9088 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9089 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9090 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009091
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009092 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9093 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009094
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009095 update_cr8_intercept(vcpu);
9096
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009097 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009098 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009099 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009100 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009101 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9102
Avi Kivity3842d132010-07-27 12:30:24 +03009103 kvm_make_request(KVM_REQ_EVENT, vcpu);
9104
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009105 ret = 0;
9106out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009107 return ret;
9108}
9109
9110int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9111 struct kvm_sregs *sregs)
9112{
9113 int ret;
9114
9115 vcpu_load(vcpu);
9116 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009117 vcpu_put(vcpu);
9118 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009119}
9120
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009121int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9122 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009123{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009124 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009125 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009126
Christoffer Dall66b56562017-12-04 21:35:33 +01009127 vcpu_load(vcpu);
9128
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009129 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9130 r = -EBUSY;
9131 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009132 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009133 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9134 kvm_queue_exception(vcpu, DB_VECTOR);
9135 else
9136 kvm_queue_exception(vcpu, BP_VECTOR);
9137 }
9138
Jan Kiszka91586a32009-10-05 13:07:21 +02009139 /*
9140 * Read rflags as long as potentially injected trace flags are still
9141 * filtered out.
9142 */
9143 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009144
9145 vcpu->guest_debug = dbg->control;
9146 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9147 vcpu->guest_debug = 0;
9148
9149 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009150 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9151 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009152 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009153 } else {
9154 for (i = 0; i < KVM_NR_DB_REGS; i++)
9155 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009156 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009157 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009158
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009159 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9160 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9161 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009162
Jan Kiszka91586a32009-10-05 13:07:21 +02009163 /*
9164 * Trigger an rflags update that will inject or remove the trace
9165 * flags.
9166 */
9167 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009168
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009169 kvm_x86_ops.update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009170
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009171 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009172
Avi Kivity2122ff52010-05-13 11:25:04 +03009173out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009174 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009175 return r;
9176}
9177
9178/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009179 * Translate a guest virtual address to a guest physical address.
9180 */
9181int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9182 struct kvm_translation *tr)
9183{
9184 unsigned long vaddr = tr->linear_address;
9185 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009186 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009187
Christoffer Dall1da5b612017-12-04 21:35:32 +01009188 vcpu_load(vcpu);
9189
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009190 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009191 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009192 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009193 tr->physical_address = gpa;
9194 tr->valid = gpa != UNMAPPED_GVA;
9195 tr->writeable = 1;
9196 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009197
Christoffer Dall1da5b612017-12-04 21:35:32 +01009198 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009199 return 0;
9200}
9201
Hollis Blanchardd0752062007-10-31 17:24:25 -05009202int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9203{
Christoffer Dall13931232017-12-04 21:35:34 +01009204 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009205
Christoffer Dall13931232017-12-04 21:35:34 +01009206 vcpu_load(vcpu);
9207
Marc Orrb666a4b2018-11-06 14:53:56 -08009208 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009209 memcpy(fpu->fpr, fxsave->st_space, 128);
9210 fpu->fcw = fxsave->cwd;
9211 fpu->fsw = fxsave->swd;
9212 fpu->ftwx = fxsave->twd;
9213 fpu->last_opcode = fxsave->fop;
9214 fpu->last_ip = fxsave->rip;
9215 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009216 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009217
Christoffer Dall13931232017-12-04 21:35:34 +01009218 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009219 return 0;
9220}
9221
9222int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9223{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009224 struct fxregs_state *fxsave;
9225
9226 vcpu_load(vcpu);
9227
Marc Orrb666a4b2018-11-06 14:53:56 -08009228 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009229
Hollis Blanchardd0752062007-10-31 17:24:25 -05009230 memcpy(fxsave->st_space, fpu->fpr, 128);
9231 fxsave->cwd = fpu->fcw;
9232 fxsave->swd = fpu->fsw;
9233 fxsave->twd = fpu->ftwx;
9234 fxsave->fop = fpu->last_opcode;
9235 fxsave->rip = fpu->last_ip;
9236 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009237 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009238
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009239 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009240 return 0;
9241}
9242
Ken Hofsass01643c52018-01-31 16:03:36 -08009243static void store_regs(struct kvm_vcpu *vcpu)
9244{
9245 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9246
9247 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9248 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9249
9250 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9251 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9252
9253 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9254 kvm_vcpu_ioctl_x86_get_vcpu_events(
9255 vcpu, &vcpu->run->s.regs.events);
9256}
9257
9258static int sync_regs(struct kvm_vcpu *vcpu)
9259{
9260 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9261 return -EINVAL;
9262
9263 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9264 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9265 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9266 }
9267 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9268 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9269 return -EINVAL;
9270 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9271 }
9272 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9273 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9274 vcpu, &vcpu->run->s.regs.events))
9275 return -EINVAL;
9276 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9277 }
9278
9279 return 0;
9280}
9281
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009282static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009283{
Marc Orrb666a4b2018-11-06 14:53:56 -08009284 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009285 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009286 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009287 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009288
Dexuan Cui2acf9232010-06-10 11:27:12 +08009289 /*
9290 * Ensure guest xcr0 is valid for loading
9291 */
Dave Hansend91cab72015-09-02 16:31:26 -07009292 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009293
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009294 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009295}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009296
Sean Christopherson897cc382019-12-18 13:55:09 -08009297int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009298{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009299 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009300 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9301 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009302
Sean Christopherson897cc382019-12-18 13:55:09 -08009303 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009304}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009305
Sean Christophersone529ef62019-12-18 13:55:15 -08009306int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009307{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009308 struct page *page;
9309 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009310
Sean Christopherson95a0d012019-12-18 13:55:23 -08009311 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9312 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9313 else
9314 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9315
9316 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9317
9318 r = kvm_mmu_create(vcpu);
9319 if (r < 0)
9320 return r;
9321
9322 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009323 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9324 if (r < 0)
9325 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009326 if (kvm_apicv_activated(vcpu->kvm))
9327 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009328 } else
9329 static_key_slow_inc(&kvm_no_apic_vcpu);
9330
9331 r = -ENOMEM;
9332
9333 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9334 if (!page)
9335 goto fail_free_lapic;
9336 vcpu->arch.pio_data = page_address(page);
9337
9338 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9339 GFP_KERNEL_ACCOUNT);
9340 if (!vcpu->arch.mce_banks)
9341 goto fail_free_pio_data;
9342 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9343
9344 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9345 GFP_KERNEL_ACCOUNT))
9346 goto fail_free_mce_banks;
9347
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009348 if (!alloc_emulate_ctxt(vcpu))
9349 goto free_wbinvd_dirty_mask;
9350
Sean Christopherson95a0d012019-12-18 13:55:23 -08009351 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9352 GFP_KERNEL_ACCOUNT);
9353 if (!vcpu->arch.user_fpu) {
9354 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009355 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009356 }
9357
9358 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9359 GFP_KERNEL_ACCOUNT);
9360 if (!vcpu->arch.guest_fpu) {
9361 pr_err("kvm: failed to allocate vcpu's fpu\n");
9362 goto free_user_fpu;
9363 }
9364 fx_init(vcpu);
9365
9366 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9367
9368 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9369
9370 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9371
9372 kvm_async_pf_hash_reset(vcpu);
9373 kvm_pmu_init(vcpu);
9374
9375 vcpu->arch.pending_external_vector = -1;
9376 vcpu->arch.preempted_in_kernel = false;
9377
9378 kvm_hv_vcpu_init(vcpu);
9379
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009380 r = kvm_x86_ops.vcpu_create(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009381 if (r)
9382 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009383
Sean Christopherson0cf91352019-03-07 15:43:02 -08009384 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009385 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009386 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009387 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009388 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009389 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009390 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009391 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009392
9393free_guest_fpu:
9394 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9395free_user_fpu:
9396 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009397free_emulate_ctxt:
9398 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009399free_wbinvd_dirty_mask:
9400 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9401fail_free_mce_banks:
9402 kfree(vcpu->arch.mce_banks);
9403fail_free_pio_data:
9404 free_page((unsigned long)vcpu->arch.pio_data);
9405fail_free_lapic:
9406 kvm_free_lapic(vcpu);
9407fail_mmu_destroy:
9408 kvm_mmu_destroy(vcpu);
9409 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009410}
9411
Dominik Dingel31928aa2014-12-04 15:47:07 +01009412void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009413{
Will Auld8fe8ab42012-11-29 12:42:12 -08009414 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009415 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009416
Roman Kagand3457c82017-07-14 17:13:20 +03009417 kvm_hv_vcpu_postcreate(vcpu);
9418
Christoffer Dallec7660c2017-12-04 21:35:23 +01009419 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009420 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009421 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009422 msr.data = 0x0;
9423 msr.index = MSR_IA32_TSC;
9424 msr.host_initiated = true;
9425 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009426 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009427
9428 /* poll control enabled by default */
9429 vcpu->arch.msr_kvm_poll_control = 1;
9430
Christoffer Dallec7660c2017-12-04 21:35:23 +01009431 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009432
Wanpeng Lib34de572020-02-28 11:18:41 +08009433 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9434 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9435 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009436}
9437
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009438void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009439{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009440 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009441 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009442
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009443 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9444
Sean Christopherson50b143e2019-12-18 13:55:07 -08009445 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009446
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009447 kvm_x86_ops.vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009448
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009449 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009450 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9451 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9452 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009453
9454 kvm_hv_vcpu_uninit(vcpu);
9455 kvm_pmu_destroy(vcpu);
9456 kfree(vcpu->arch.mce_banks);
9457 kvm_free_lapic(vcpu);
9458 idx = srcu_read_lock(&vcpu->kvm->srcu);
9459 kvm_mmu_destroy(vcpu);
9460 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9461 free_page((unsigned long)vcpu->arch.pio_data);
9462 if (!lapic_in_kernel(vcpu))
9463 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009464}
9465
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009466void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009467{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009468 kvm_lapic_reset(vcpu, init_event);
9469
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009470 vcpu->arch.hflags = 0;
9471
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009472 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009473 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009474 atomic_set(&vcpu->arch.nmi_queued, 0);
9475 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009476 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009477 kvm_clear_interrupt_queue(vcpu);
9478 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009479
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009480 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009481 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009482 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009483 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009484 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009485 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009486
Nadav Amit11190222015-04-02 03:10:38 +03009487 vcpu->arch.cr2 = 0;
9488
Avi Kivity3842d132010-07-27 12:30:24 +03009489 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009490 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009491 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009492
Glauber Costa12f9a482011-02-01 14:16:40 -05009493 kvmclock_reset(vcpu);
9494
Gleb Natapovaf585b92010-10-14 11:22:46 +02009495 kvm_clear_async_pf_completion_queue(vcpu);
9496 kvm_async_pf_hash_reset(vcpu);
9497 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009498
Wanpeng Lia554d202017-10-11 05:10:19 -07009499 if (kvm_mpx_supported()) {
9500 void *mpx_state_buffer;
9501
9502 /*
9503 * To avoid have the INIT path from kvm_apic_has_events() that be
9504 * called with loaded FPU and does not let userspace fix the state.
9505 */
Rik van Rielf775b132017-11-14 16:54:23 -05009506 if (init_event)
9507 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009508 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009509 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009510 if (mpx_state_buffer)
9511 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009512 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009513 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009514 if (mpx_state_buffer)
9515 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009516 if (init_event)
9517 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009518 }
9519
Paolo Bonzini64d60672015-05-07 11:36:11 +02009520 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009521 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009522 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009523
Kyle Hueydb2336a2017-03-20 01:16:28 -07009524 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009525
9526 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009527 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009528
Julian Stecklina66f7b722012-12-05 15:26:19 +01009529 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9530 vcpu->arch.regs_avail = ~0;
9531 vcpu->arch.regs_dirty = ~0;
9532
Wanpeng Lia554d202017-10-11 05:10:19 -07009533 vcpu->arch.ia32_xss = 0;
9534
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009535 kvm_x86_ops.vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009536}
9537
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009538void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009539{
9540 struct kvm_segment cs;
9541
9542 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9543 cs.selector = vector << 8;
9544 cs.base = vector << 12;
9545 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9546 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009547}
9548
Radim Krčmář13a34e02014-08-28 15:13:03 +02009549int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009550{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009551 struct kvm *kvm;
9552 struct kvm_vcpu *vcpu;
9553 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009554 int ret;
9555 u64 local_tsc;
9556 u64 max_tsc = 0;
9557 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009558
9559 kvm_shared_msr_cpu_online();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009560 ret = kvm_x86_ops.hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009561 if (ret != 0)
9562 return ret;
9563
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009564 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009565 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009566 list_for_each_entry(kvm, &vm_list, vm_list) {
9567 kvm_for_each_vcpu(i, vcpu, kvm) {
9568 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009569 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009570 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9571 backwards_tsc = true;
9572 if (vcpu->arch.last_host_tsc > max_tsc)
9573 max_tsc = vcpu->arch.last_host_tsc;
9574 }
9575 }
9576 }
9577
9578 /*
9579 * Sometimes, even reliable TSCs go backwards. This happens on
9580 * platforms that reset TSC during suspend or hibernate actions, but
9581 * maintain synchronization. We must compensate. Fortunately, we can
9582 * detect that condition here, which happens early in CPU bringup,
9583 * before any KVM threads can be running. Unfortunately, we can't
9584 * bring the TSCs fully up to date with real time, as we aren't yet far
9585 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009586 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009587 * variables that haven't been updated yet.
9588 *
9589 * So we simply find the maximum observed TSC above, then record the
9590 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9591 * the adjustment will be applied. Note that we accumulate
9592 * adjustments, in case multiple suspend cycles happen before some VCPU
9593 * gets a chance to run again. In the event that no KVM threads get a
9594 * chance to run, we will miss the entire elapsed period, as we'll have
9595 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9596 * loose cycle time. This isn't too big a deal, since the loss will be
9597 * uniform across all VCPUs (not to mention the scenario is extremely
9598 * unlikely). It is possible that a second hibernate recovery happens
9599 * much faster than a first, causing the observed TSC here to be
9600 * smaller; this would require additional padding adjustment, which is
9601 * why we set last_host_tsc to the local tsc observed here.
9602 *
9603 * N.B. - this code below runs only on platforms with reliable TSC,
9604 * as that is the only way backwards_tsc is set above. Also note
9605 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9606 * have the same delta_cyc adjustment applied if backwards_tsc
9607 * is detected. Note further, this adjustment is only done once,
9608 * as we reset last_host_tsc on all VCPUs to stop this from being
9609 * called multiple times (one for each physical CPU bringup).
9610 *
Guo Chao4a969982012-06-28 15:17:27 +08009611 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009612 * will be compensated by the logic in vcpu_load, which sets the TSC to
9613 * catchup mode. This will catchup all VCPUs to real time, but cannot
9614 * guarantee that they stay in perfect synchronization.
9615 */
9616 if (backwards_tsc) {
9617 u64 delta_cyc = max_tsc - local_tsc;
9618 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009619 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009620 kvm_for_each_vcpu(i, vcpu, kvm) {
9621 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9622 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009623 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009624 }
9625
9626 /*
9627 * We have to disable TSC offset matching.. if you were
9628 * booting a VM while issuing an S4 host suspend....
9629 * you may have some problem. Solving this issue is
9630 * left as an exercise to the reader.
9631 */
9632 kvm->arch.last_tsc_nsec = 0;
9633 kvm->arch.last_tsc_write = 0;
9634 }
9635
9636 }
9637 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009638}
9639
Radim Krčmář13a34e02014-08-28 15:13:03 +02009640void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009641{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009642 kvm_x86_ops.hardware_disable();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009643 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009644}
9645
Sean Christophersonb9904082020-03-21 13:25:55 -07009646int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009647{
Sean Christophersond008dfd2020-03-21 13:25:56 -07009648 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009649 int r;
9650
Sean Christopherson91661982020-03-02 15:57:06 -08009651 rdmsrl_safe(MSR_EFER, &host_efer);
9652
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009653 if (boot_cpu_has(X86_FEATURE_XSAVES))
9654 rdmsrl(MSR_IA32_XSS, host_xss);
9655
Sean Christophersond008dfd2020-03-21 13:25:56 -07009656 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009657 if (r != 0)
9658 return r;
9659
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009660 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Sean Christopherson69c6f692020-03-21 13:25:59 -07009661
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009662 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9663 supported_xss = 0;
9664
Sean Christophersonb11306b2019-12-10 14:44:13 -08009665 cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data);
9666
Haozhong Zhang35181e82015-10-20 15:39:03 +08009667 if (kvm_has_tsc_control) {
9668 /*
9669 * Make sure the user can only configure tsc_khz values that
9670 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009671 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009672 * be 1 on all machines.
9673 */
9674 u64 max = min(0x7fffffffULL,
9675 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9676 kvm_max_guest_tsc_khz = max;
9677
Haozhong Zhangad7218832015-10-20 15:39:02 +08009678 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009679 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009680
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009681 kvm_init_msr_list();
9682 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009683}
9684
9685void kvm_arch_hardware_unsetup(void)
9686{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009687 kvm_x86_ops.hardware_unsetup();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009688}
9689
Sean Christophersonb9904082020-03-21 13:25:55 -07009690int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009691{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009692 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -07009693 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009694
9695 WARN_ON(!irqs_disabled());
9696
9697 if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits)
9698 return -EIO;
9699
Sean Christophersond008dfd2020-03-21 13:25:56 -07009700 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009701}
9702
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009703bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9704{
9705 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9706}
9707EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9708
9709bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9710{
9711 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9712}
9713
Gleb Natapov54e98182012-08-05 15:58:32 +03009714struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009715EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009716
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009717void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9718{
Like Xub35e5542019-10-27 18:52:43 +08009719 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9720
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009721 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009722 if (pmu->version && unlikely(pmu->event_count)) {
9723 pmu->need_cleanup = true;
9724 kvm_make_request(KVM_REQ_PMU, vcpu);
9725 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009726 kvm_x86_ops.sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009727}
9728
Sean Christopherson562b6b02020-01-26 16:41:13 -08009729void kvm_arch_free_vm(struct kvm *kvm)
9730{
9731 kfree(kvm->arch.hyperv.hv_pa_pg);
9732 vfree(kvm);
9733}
9734
9735
Carsten Ottee08b9632012-01-04 10:25:20 +01009736int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009737{
Carsten Ottee08b9632012-01-04 10:25:20 +01009738 if (type)
9739 return -EINVAL;
9740
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009741 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009742 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009743 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009744 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009745 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009746 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009747
Sheng Yang5550af42008-10-15 20:15:06 +08009748 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9749 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009750 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9751 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9752 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009753
Jan Kiszka038f8c12011-02-04 10:49:11 +01009754 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009755 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009756 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9757
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009758 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009759 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009760
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009761 kvm->arch.guest_can_read_msr_platform_info = true;
9762
Andrew Jones7e44e442014-02-28 12:52:54 +01009763 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009764 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009765
Roman Kagancbc02362018-02-01 16:48:31 +03009766 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009767 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009768 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009769
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009770 return kvm_x86_ops.vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009771}
9772
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009773int kvm_arch_post_init_vm(struct kvm *kvm)
9774{
9775 return kvm_mmu_post_init_vm(kvm);
9776}
9777
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009778static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9779{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009780 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009781 kvm_mmu_unload(vcpu);
9782 vcpu_put(vcpu);
9783}
9784
9785static void kvm_free_vcpus(struct kvm *kvm)
9786{
9787 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009788 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009789
9790 /*
9791 * Unpin any mmu pages first.
9792 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009793 kvm_for_each_vcpu(i, vcpu, kvm) {
9794 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009795 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009796 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009797 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009798 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009799
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009800 mutex_lock(&kvm->lock);
9801 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9802 kvm->vcpus[i] = NULL;
9803
9804 atomic_set(&kvm->online_vcpus, 0);
9805 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009806}
9807
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009808void kvm_arch_sync_events(struct kvm *kvm)
9809{
Andrew Jones332967a2014-02-28 12:52:55 +01009810 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009811 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009812 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009813}
9814
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009815int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009816{
9817 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009818 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009819 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009820 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009821
9822 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009823 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9824 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009825
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009826 slot = id_to_memslot(slots, id);
9827 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009828 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009829 return -EEXIST;
9830
9831 /*
9832 * MAP_SHARED to prevent internal slot pages from being moved
9833 * by fork()/COW.
9834 */
9835 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9836 MAP_SHARED | MAP_ANONYMOUS, 0);
9837 if (IS_ERR((void *)hva))
9838 return PTR_ERR((void *)hva);
9839 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009840 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009841 return 0;
9842
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009843 /*
9844 * Stuff a non-canonical value to catch use-after-delete. This
9845 * ends up being 0 on 32-bit KVM, but there's no better
9846 * alternative.
9847 */
9848 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009849 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009850 }
9851
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009852 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009853 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009854
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009855 m.slot = id | (i << 16);
9856 m.flags = 0;
9857 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009858 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009859 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009860 r = __kvm_set_memory_region(kvm, &m);
9861 if (r < 0)
9862 return r;
9863 }
9864
Eric Biggers103c7632018-01-31 17:30:21 -08009865 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009866 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009867
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009868 return 0;
9869}
9870EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9871
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009872void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9873{
9874 kvm_mmu_pre_destroy_vm(kvm);
9875}
9876
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009877void kvm_arch_destroy_vm(struct kvm *kvm)
9878{
Andrew Honig27469d22013-04-18 09:38:14 -07009879 if (current->mm == kvm->mm) {
9880 /*
9881 * Free memory regions allocated on behalf of userspace,
9882 * unless the the memory map has changed due to process exit
9883 * or fd copying.
9884 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009885 mutex_lock(&kvm->slots_lock);
9886 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9887 0, 0);
9888 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9889 0, 0);
9890 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9891 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009892 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009893 if (kvm_x86_ops.vm_destroy)
9894 kvm_x86_ops.vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009895 kvm_pic_destroy(kvm);
9896 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009897 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009898 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009899 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009900 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009901 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009902 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009903}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009904
Sean Christophersone96c81e2020-02-18 13:07:27 -08009905void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009906{
9907 int i;
9908
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009909 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009910 kvfree(slot->arch.rmap[i]);
9911 slot->arch.rmap[i] = NULL;
9912
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009913 if (i == 0)
9914 continue;
9915
Sean Christophersone96c81e2020-02-18 13:07:27 -08009916 kvfree(slot->arch.lpage_info[i - 1]);
9917 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009918 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009919
Sean Christophersone96c81e2020-02-18 13:07:27 -08009920 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009921}
9922
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009923static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9924 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009925{
9926 int i;
9927
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009928 /*
9929 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9930 * old arrays will be freed by __kvm_set_memory_region() if installing
9931 * the new memslot is successful.
9932 */
9933 memset(&slot->arch, 0, sizeof(slot->arch));
9934
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009935 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009936 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009937 unsigned long ugfn;
9938 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009939 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009940
9941 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9942 slot->base_gfn, level) + 1;
9943
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009944 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009945 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009946 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009947 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009948 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009949 if (i == 0)
9950 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009951
Ben Gardon254272c2019-02-11 11:02:50 -08009952 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009953 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009954 goto out_free;
9955
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009956 slot->arch.lpage_info[i - 1] = linfo;
9957
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009958 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009959 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009960 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009961 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009962 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9963 /*
9964 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -08009965 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009966 */
Sean Christopherson600087b2020-03-02 15:57:05 -08009967 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009968 unsigned long j;
9969
9970 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009971 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009972 }
9973 }
9974
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009975 if (kvm_page_track_create_memslot(slot, npages))
9976 goto out_free;
9977
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009978 return 0;
9979
9980out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009981 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009982 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009983 slot->arch.rmap[i] = NULL;
9984 if (i == 0)
9985 continue;
9986
Thomas Huth548ef282015-02-24 21:29:25 +01009987 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009988 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009989 }
9990 return -ENOMEM;
9991}
9992
Sean Christopherson15248252019-02-05 12:54:17 -08009993void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009994{
Boris Ostrovsky91724812019-12-05 01:30:51 +00009995 struct kvm_vcpu *vcpu;
9996 int i;
9997
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009998 /*
9999 * memslots->generation has been incremented.
10000 * mmio generation may have reached its maximum value.
10001 */
Sean Christopherson15248252019-02-05 12:54:17 -080010002 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010003
10004 /* Force re-initialization of steal_time cache */
10005 kvm_for_each_vcpu(i, vcpu, kvm)
10006 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010007}
10008
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010009int kvm_arch_prepare_memory_region(struct kvm *kvm,
10010 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010011 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010012 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010013{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010014 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10015 return kvm_alloc_memslot_metadata(memslot,
10016 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010017 return 0;
10018}
10019
Kai Huang88178fd2015-01-28 10:54:27 +080010020static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
10021 struct kvm_memory_slot *new)
10022{
10023 /* Still write protect RO slot */
10024 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010025 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +080010026 return;
10027 }
10028
10029 /*
10030 * Call kvm_x86_ops dirty logging hooks when they are valid.
10031 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010032 * kvm_x86_ops.slot_disable_log_dirty is called when:
Kai Huang88178fd2015-01-28 10:54:27 +080010033 *
10034 * - KVM_MR_CREATE with dirty logging is disabled
10035 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
10036 *
10037 * The reason is, in case of PML, we need to set D-bit for any slots
10038 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010039 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010040 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010041 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010042 * logging disabled for memory slots.
10043 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010044 * kvm_x86_ops.slot_enable_log_dirty is called when switching new slot
Kai Huang88178fd2015-01-28 10:54:27 +080010045 * to dirty logging mode.
10046 *
10047 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10048 *
10049 * In case of write protect:
10050 *
10051 * Write protect all pages for dirty logging.
10052 *
10053 * All the sptes including the large sptes which point to this
10054 * slot are set to readonly. We can not create any new large
10055 * spte on this slot until the end of the logging.
10056 *
10057 * See the comments in fast_page_fault().
10058 */
10059 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010060 if (kvm_x86_ops.slot_enable_log_dirty) {
10061 kvm_x86_ops.slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010062 } else {
10063 int level =
10064 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10065 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10066
10067 /*
10068 * If we're with initial-all-set, we don't need
10069 * to write protect any small page because
10070 * they're reported as dirty already. However
10071 * we still need to write-protect huge pages
10072 * so that the page split can happen lazily on
10073 * the first write to the huge page.
10074 */
10075 kvm_mmu_slot_remove_write_access(kvm, new, level);
10076 }
Kai Huang88178fd2015-01-28 10:54:27 +080010077 } else {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010078 if (kvm_x86_ops.slot_disable_log_dirty)
10079 kvm_x86_ops.slot_disable_log_dirty(kvm, new);
Kai Huang88178fd2015-01-28 10:54:27 +080010080 }
10081}
10082
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010083void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010084 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010085 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010086 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010087 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010088{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010089 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010090 kvm_mmu_change_mmu_pages(kvm,
10091 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010092
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010093 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010094 * Dirty logging tracks sptes in 4k granularity, meaning that large
10095 * sptes have to be split. If live migration is successful, the guest
10096 * in the source machine will be destroyed and large sptes will be
10097 * created in the destination. However, if the guest continues to run
10098 * in the source machine (for example if live migration fails), small
10099 * sptes will remain around and cause bad performance.
10100 *
10101 * Scan sptes if dirty logging has been stopped, dropping those
10102 * which can be collapsed into a single large-page spte. Later
10103 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010104 *
10105 * There is no need to do this in any of the following cases:
10106 * CREATE: No dirty mappings will already exist.
10107 * MOVE/DELETE: The old mappings will already have been cleaned up by
10108 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010109 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010110 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010111 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10112 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10113 kvm_mmu_zap_collapsible_sptes(kvm, new);
10114
10115 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010116 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010117 *
Kai Huang88178fd2015-01-28 10:54:27 +080010118 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10119 * been zapped so no dirty logging staff is needed for old slot. For
10120 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10121 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010122 *
10123 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010124 */
Kai Huang88178fd2015-01-28 10:54:27 +080010125 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010126 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010127
10128 /* Free the arrays associated with the old memslot. */
10129 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010130 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010131}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010132
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010133void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010134{
Sean Christopherson7390de12019-02-05 13:01:31 -080010135 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010136}
10137
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010138void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10139 struct kvm_memory_slot *slot)
10140{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010141 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010142}
10143
Liran Alone6c67d82018-09-04 10:56:52 +030010144static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10145{
10146 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010147 kvm_x86_ops.guest_apic_has_interrupt &&
10148 kvm_x86_ops.guest_apic_has_interrupt(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030010149}
10150
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010151static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10152{
10153 if (!list_empty_careful(&vcpu->async_pf.done))
10154 return true;
10155
10156 if (kvm_apic_has_events(vcpu))
10157 return true;
10158
10159 if (vcpu->arch.pv.pv_unhalted)
10160 return true;
10161
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010162 if (vcpu->arch.exception.pending)
10163 return true;
10164
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010165 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10166 (vcpu->arch.nmi_pending &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010167 kvm_x86_ops.nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010168 return true;
10169
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010170 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10171 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010172 return true;
10173
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010174 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010175 (kvm_cpu_has_interrupt(vcpu) ||
10176 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010177 return true;
10178
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010179 if (kvm_hv_has_stimer_pending(vcpu))
10180 return true;
10181
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010182 return false;
10183}
10184
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010185int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10186{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010187 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010188}
Zhang Xiantao57361992007-12-17 14:21:40 +080010189
Wanpeng Li17e433b2019-08-05 10:03:19 +080010190bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10191{
10192 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10193 return true;
10194
10195 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10196 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10197 kvm_test_request(KVM_REQ_EVENT, vcpu))
10198 return true;
10199
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010200 if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu))
Wanpeng Li17e433b2019-08-05 10:03:19 +080010201 return true;
10202
10203 return false;
10204}
10205
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010206bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10207{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010208 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010209}
10210
Christoffer Dallb6d33832012-03-08 16:44:24 -050010211int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010212{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010213 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010214}
Gleb Natapov78646122009-03-23 12:12:11 +020010215
10216int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10217{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010218 return kvm_x86_ops.interrupt_allowed(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +020010219}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010220
Nadav Amit82b32772014-11-02 11:54:45 +020010221unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10222{
10223 if (is_64_bit_mode(vcpu))
10224 return kvm_rip_read(vcpu);
10225 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10226 kvm_rip_read(vcpu));
10227}
10228EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10229
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010230bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10231{
Nadav Amit82b32772014-11-02 11:54:45 +020010232 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010233}
10234EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10235
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010236unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10237{
10238 unsigned long rflags;
10239
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010240 rflags = kvm_x86_ops.get_rflags(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010241 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010242 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010243 return rflags;
10244}
10245EXPORT_SYMBOL_GPL(kvm_get_rflags);
10246
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010247static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010248{
10249 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010250 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010251 rflags |= X86_EFLAGS_TF;
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010252 kvm_x86_ops.set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010253}
10254
10255void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10256{
10257 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010258 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010259}
10260EXPORT_SYMBOL_GPL(kvm_set_rflags);
10261
Gleb Natapov56028d02010-10-17 18:13:42 +020010262void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10263{
10264 int r;
10265
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010266 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010267 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010268 return;
10269
10270 r = kvm_mmu_reload(vcpu);
10271 if (unlikely(r))
10272 return;
10273
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010274 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010275 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010276 return;
10277
Sean Christopherson7a026742020-02-06 14:14:34 -080010278 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010279}
10280
Gleb Natapovaf585b92010-10-14 11:22:46 +020010281static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10282{
10283 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10284}
10285
10286static inline u32 kvm_async_pf_next_probe(u32 key)
10287{
10288 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10289}
10290
10291static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10292{
10293 u32 key = kvm_async_pf_hash_fn(gfn);
10294
10295 while (vcpu->arch.apf.gfns[key] != ~0)
10296 key = kvm_async_pf_next_probe(key);
10297
10298 vcpu->arch.apf.gfns[key] = gfn;
10299}
10300
10301static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10302{
10303 int i;
10304 u32 key = kvm_async_pf_hash_fn(gfn);
10305
10306 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010307 (vcpu->arch.apf.gfns[key] != gfn &&
10308 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010309 key = kvm_async_pf_next_probe(key);
10310
10311 return key;
10312}
10313
10314bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10315{
10316 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10317}
10318
10319static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10320{
10321 u32 i, j, k;
10322
10323 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10324 while (true) {
10325 vcpu->arch.apf.gfns[i] = ~0;
10326 do {
10327 j = kvm_async_pf_next_probe(j);
10328 if (vcpu->arch.apf.gfns[j] == ~0)
10329 return;
10330 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10331 /*
10332 * k lies cyclically in ]i,j]
10333 * | i.k.j |
10334 * |....j i.k.| or |.k..j i...|
10335 */
10336 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10337 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10338 i = j;
10339 }
10340}
10341
Gleb Natapov7c907052010-10-14 11:22:53 +020010342static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10343{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010344
10345 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10346 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010347}
10348
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010349static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10350{
10351
10352 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10353 sizeof(u32));
10354}
10355
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010356static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10357{
10358 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10359 return false;
10360
10361 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10362 (vcpu->arch.apf.send_user_only &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010363 kvm_x86_ops.get_cpl(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010364 return false;
10365
10366 return true;
10367}
10368
10369bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10370{
10371 if (unlikely(!lapic_in_kernel(vcpu) ||
10372 kvm_event_needs_reinjection(vcpu) ||
10373 vcpu->arch.exception.pending))
10374 return false;
10375
10376 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10377 return false;
10378
10379 /*
10380 * If interrupts are off we cannot even use an artificial
10381 * halt state.
10382 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010383 return kvm_x86_ops.interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010384}
10385
Gleb Natapovaf585b92010-10-14 11:22:46 +020010386void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10387 struct kvm_async_pf *work)
10388{
Avi Kivity6389ee92010-11-29 16:12:30 +020010389 struct x86_exception fault;
10390
Sean Christopherson736c2912019-12-06 15:57:14 -080010391 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010392 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010393
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010394 if (kvm_can_deliver_async_pf(vcpu) &&
10395 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010396 fault.vector = PF_VECTOR;
10397 fault.error_code_valid = true;
10398 fault.error_code = 0;
10399 fault.nested_page_fault = false;
10400 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010401 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010402 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010403 } else {
10404 /*
10405 * It is not possible to deliver a paravirtualized asynchronous
10406 * page fault, but putting the guest in an artificial halt state
10407 * can be beneficial nevertheless: if an interrupt arrives, we
10408 * can deliver it timely and perhaps the guest will schedule
10409 * another process. When the instruction that triggered a page
10410 * fault is retried, hopefully the page will be ready in the host.
10411 */
10412 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010413 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010414}
10415
10416void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10417 struct kvm_async_pf *work)
10418{
Avi Kivity6389ee92010-11-29 16:12:30 +020010419 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010420 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010421
chai wenf2e10662013-10-14 22:22:33 +080010422 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010423 work->arch.token = ~0; /* broadcast wakeup */
10424 else
10425 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010426 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010427
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010428 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10429 !apf_get_user(vcpu, &val)) {
10430 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10431 vcpu->arch.exception.pending &&
10432 vcpu->arch.exception.nr == PF_VECTOR &&
10433 !apf_put_user(vcpu, 0)) {
10434 vcpu->arch.exception.injected = false;
10435 vcpu->arch.exception.pending = false;
10436 vcpu->arch.exception.nr = 0;
10437 vcpu->arch.exception.has_error_code = false;
10438 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010439 vcpu->arch.exception.has_payload = false;
10440 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010441 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10442 fault.vector = PF_VECTOR;
10443 fault.error_code_valid = true;
10444 fault.error_code = 0;
10445 fault.nested_page_fault = false;
10446 fault.address = work->arch.token;
10447 fault.async_page_fault = true;
10448 kvm_inject_page_fault(vcpu, &fault);
10449 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010450 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010451 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010452 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010453}
10454
10455bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10456{
10457 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10458 return true;
10459 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010460 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010461}
10462
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010463void kvm_arch_start_assignment(struct kvm *kvm)
10464{
10465 atomic_inc(&kvm->arch.assigned_device_count);
10466}
10467EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10468
10469void kvm_arch_end_assignment(struct kvm *kvm)
10470{
10471 atomic_dec(&kvm->arch.assigned_device_count);
10472}
10473EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10474
10475bool kvm_arch_has_assigned_device(struct kvm *kvm)
10476{
10477 return atomic_read(&kvm->arch.assigned_device_count);
10478}
10479EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10480
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010481void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10482{
10483 atomic_inc(&kvm->arch.noncoherent_dma_count);
10484}
10485EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10486
10487void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10488{
10489 atomic_dec(&kvm->arch.noncoherent_dma_count);
10490}
10491EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10492
10493bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10494{
10495 return atomic_read(&kvm->arch.noncoherent_dma_count);
10496}
10497EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10498
Alex Williamson14717e22016-05-05 11:58:35 -060010499bool kvm_arch_has_irq_bypass(void)
10500{
Sean Christopherson92735b12019-08-02 15:06:17 -070010501 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010502}
10503
Feng Wu87276882015-09-18 22:29:40 +080010504int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10505 struct irq_bypass_producer *prod)
10506{
10507 struct kvm_kernel_irqfd *irqfd =
10508 container_of(cons, struct kvm_kernel_irqfd, consumer);
10509
Alex Williamson14717e22016-05-05 11:58:35 -060010510 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010511
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010512 return kvm_x86_ops.update_pi_irte(irqfd->kvm,
Alex Williamson14717e22016-05-05 11:58:35 -060010513 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010514}
10515
10516void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10517 struct irq_bypass_producer *prod)
10518{
10519 int ret;
10520 struct kvm_kernel_irqfd *irqfd =
10521 container_of(cons, struct kvm_kernel_irqfd, consumer);
10522
Feng Wu87276882015-09-18 22:29:40 +080010523 WARN_ON(irqfd->producer != prod);
10524 irqfd->producer = NULL;
10525
10526 /*
10527 * When producer of consumer is unregistered, we change back to
10528 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010529 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010530 * int this case doesn't want to receive the interrupts.
10531 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010532 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080010533 if (ret)
10534 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10535 " fails: %d\n", irqfd->consumer.token, ret);
10536}
10537
10538int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10539 uint32_t guest_irq, bool set)
10540{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010541 return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080010542}
10543
Feng Wu520040142016-01-25 16:53:33 +080010544bool kvm_vector_hashing_enabled(void)
10545{
10546 return vector_hashing;
10547}
Feng Wu520040142016-01-25 16:53:33 +080010548
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010549bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10550{
10551 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10552}
10553EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10554
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010555u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10556{
10557 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10558
10559 /* The STIBP bit doesn't fault even if it's not advertised */
10560 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10561 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10562 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10563 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10564 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10565 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10566
10567 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10568 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10569 bits &= ~SPEC_CTRL_SSBD;
10570 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10571 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10572 bits &= ~SPEC_CTRL_SSBD;
10573
10574 return bits;
10575}
10576EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010577
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010578EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010579EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010580EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10581EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10582EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10583EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010584EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010585EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010586EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010587EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010588EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010589EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010590EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010591EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010592EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010593EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010594EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010595EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010596EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10597EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010598EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010599EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);