blob: 23b6c2e38d9e06563491d0e9792b49986cef7f81 [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
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200100#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
101#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
Hollis Blanchard417bc302007-10-31 17:24:23 -0500102
Radim Krčmářc5192652016-07-12 22:09:28 +0200103#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
104 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200105
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300106static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300107static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200108static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100109static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800110static void store_regs(struct kvm_vcpu *vcpu);
111static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200112
Paolo Bonzini893590c2015-11-06 11:46:24 +0100113struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300114EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800115
Paolo Bonzini893590c2015-11-06 11:46:24 +0100116static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030117module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200118
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200119static bool __read_mostly report_ignored_msrs = true;
120module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
121
Wanpeng Li4c276252018-05-05 04:02:32 -0700122unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200123module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
124
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300125static bool __read_mostly kvmclock_periodic_sync = true;
126module_param(kvmclock_periodic_sync, bool, S_IRUGO);
127
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100129EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100130u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100131EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800132u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
133EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
134u64 __read_mostly kvm_max_tsc_scaling_ratio;
135EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700136u64 __read_mostly kvm_default_tsc_scaling_ratio;
137EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100138
Zachary Amsdencc578282012-02-03 15:43:50 -0200139/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100140static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200141module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
142
Sean Christophersonc3941d92019-04-17 10:15:33 -0700143/*
144 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
145 * adaptive tuning starting from default advancment of 1000ns. '0' disables
146 * advancement entirely. Any other value is used as-is and disables adaptive
147 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
148 */
149static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800150module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500151
Feng Wu520040142016-01-25 16:53:33 +0800152static bool __read_mostly vector_hashing = true;
153module_param(vector_hashing, bool, S_IRUGO);
154
Liran Alonc4ae60e2018-03-12 13:12:47 +0200155bool __read_mostly enable_vmware_backdoor = false;
156module_param(enable_vmware_backdoor, bool, S_IRUGO);
157EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
158
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700159static bool __read_mostly force_emulation_prefix = false;
160module_param(force_emulation_prefix, bool, S_IRUGO);
161
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800162int __read_mostly pi_inject_timer = -1;
163module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
164
Avi Kivity18863bd2009-09-07 11:12:18 +0300165#define KVM_NR_SHARED_MSRS 16
166
167struct kvm_shared_msrs_global {
168 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800169 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300170};
171
172struct kvm_shared_msrs {
173 struct user_return_notifier urn;
174 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800175 struct kvm_shared_msr_values {
176 u64 host;
177 u64 curr;
178 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300179};
180
181static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200182static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300183
Sean Christophersoncfc48182020-03-02 15:56:23 -0800184#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
185 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
186 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
187 | XFEATURE_MASK_PKRU)
188
Sean Christopherson91661982020-03-02 15:57:06 -0800189u64 __read_mostly host_efer;
190EXPORT_SYMBOL_GPL(host_efer);
191
Aaron Lewis139a12c2019-10-21 16:30:25 -0700192static u64 __read_mostly host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100193u64 __read_mostly supported_xss;
194EXPORT_SYMBOL_GPL(supported_xss);
Aaron Lewis139a12c2019-10-21 16:30:25 -0700195
Hollis Blanchard417bc302007-10-31 17:24:23 -0500196struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200197 { "pf_fixed", VCPU_STAT(pf_fixed) },
198 { "pf_guest", VCPU_STAT(pf_guest) },
199 { "tlb_flush", VCPU_STAT(tlb_flush) },
200 { "invlpg", VCPU_STAT(invlpg) },
201 { "exits", VCPU_STAT(exits) },
202 { "io_exits", VCPU_STAT(io_exits) },
203 { "mmio_exits", VCPU_STAT(mmio_exits) },
204 { "signal_exits", VCPU_STAT(signal_exits) },
205 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800206 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200207 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100208 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200209 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200210 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200211 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530212 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200213 { "request_irq", VCPU_STAT(request_irq_exits) },
214 { "irq_exits", VCPU_STAT(irq_exits) },
215 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200216 { "fpu_reload", VCPU_STAT(fpu_reload) },
217 { "insn_emulation", VCPU_STAT(insn_emulation) },
218 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300219 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200220 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100221 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200222 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200223 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
224 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
225 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
226 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
227 { "mmu_flooded", VM_STAT(mmu_flooded) },
228 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200229 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300230 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200231 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200232 { "largepages", VM_STAT(lpages, .mode = 0444) },
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100233 { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
David Matlackf3414bc2016-12-20 15:25:57 -0800234 { "max_mmu_page_hash_collisions",
235 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500236 { NULL }
237};
238
Dexuan Cui2acf9232010-06-10 11:27:12 +0800239u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800240u64 __read_mostly supported_xcr0;
241EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800242
Marc Orrb666a4b2018-11-06 14:53:56 -0800243struct kmem_cache *x86_fpu_cache;
244EXPORT_SYMBOL_GPL(x86_fpu_cache);
245
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800246static struct kmem_cache *x86_emulator_cache;
247
248static struct kmem_cache *kvm_alloc_emulator_cache(void)
249{
Sean Christopherson06add252020-02-18 15:29:50 -0800250 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
251 unsigned int size = sizeof(struct x86_emulate_ctxt);
252
253 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800254 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800255 SLAB_ACCOUNT, useroffset,
256 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800257}
258
Jan Kiszkab6785de2012-09-20 07:43:17 +0200259static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300260
Gleb Natapovaf585b92010-10-14 11:22:46 +0200261static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
262{
263 int i;
264 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
265 vcpu->arch.apf.gfns[i] = ~0;
266}
267
Avi Kivity18863bd2009-09-07 11:12:18 +0300268static void kvm_on_user_return(struct user_return_notifier *urn)
269{
270 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300271 struct kvm_shared_msrs *locals
272 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800273 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700274 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300275
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700276 /*
277 * Disabling irqs at this point since the following code could be
278 * interrupted and executed through kvm_arch_hardware_disable()
279 */
280 local_irq_save(flags);
281 if (locals->registered) {
282 locals->registered = false;
283 user_return_notifier_unregister(urn);
284 }
285 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300286 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800287 values = &locals->values[slot];
288 if (values->host != values->curr) {
289 wrmsrl(shared_msrs_global.msrs[slot], values->host);
290 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300291 }
292 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300293}
294
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800295void kvm_define_shared_msr(unsigned slot, u32 msr)
296{
Nadav Amit0123be42014-07-24 15:06:56 +0300297 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200298 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300299 if (slot >= shared_msrs_global.nr)
300 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300301}
302EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
303
304static void kvm_shared_msr_cpu_online(void)
305{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800306 unsigned int cpu = smp_processor_id();
307 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
308 u64 value;
309 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300310
Sean Christopherson05c19c22019-11-22 12:04:50 -0800311 for (i = 0; i < shared_msrs_global.nr; ++i) {
312 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
313 smsr->values[i].host = value;
314 smsr->values[i].curr = value;
315 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300316}
317
Andy Honig8b3c3102014-08-27 11:16:44 -0700318int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300319{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200320 unsigned int cpu = smp_processor_id();
321 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700322 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300323
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500324 value = (value & mask) | (smsr->values[slot].host & ~mask);
325 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700326 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700327 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
328 if (err)
329 return 1;
330
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500331 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300332 if (!smsr->registered) {
333 smsr->urn.on_user_return = kvm_on_user_return;
334 user_return_notifier_register(&smsr->urn);
335 smsr->registered = true;
336 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700337 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300338}
339EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
340
Radim Krčmář13a34e02014-08-28 15:13:03 +0200341static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200342{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200343 unsigned int cpu = smp_processor_id();
344 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200345
346 if (smsr->registered)
347 kvm_on_user_return(&smsr->urn);
348}
349
Carsten Otte6866b832007-10-29 16:09:10 +0100350u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
351{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300352 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100353}
354EXPORT_SYMBOL_GPL(kvm_get_apic_base);
355
Jim Mattson58871642018-05-09 16:56:04 -0400356enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
357{
358 return kvm_apic_mode(kvm_get_apic_base(vcpu));
359}
360EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
361
Jan Kiszka58cb6282014-01-24 16:48:44 +0100362int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100363{
Jim Mattson58871642018-05-09 16:56:04 -0400364 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
365 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200366 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
367 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100368
Jim Mattson58871642018-05-09 16:56:04 -0400369 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700370 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400371 if (!msr_info->host_initiated) {
372 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
373 return 1;
374 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
375 return 1;
376 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100377
378 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800379 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100380 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100381}
382EXPORT_SYMBOL_GPL(kvm_set_apic_base);
383
Andi Kleen2605fc22014-05-02 00:44:37 +0200384asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000385{
386 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200387 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000388}
389EXPORT_SYMBOL_GPL(kvm_spurious_fault);
390
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200391#define EXCPT_BENIGN 0
392#define EXCPT_CONTRIBUTORY 1
393#define EXCPT_PF 2
394
395static int exception_class(int vector)
396{
397 switch (vector) {
398 case PF_VECTOR:
399 return EXCPT_PF;
400 case DE_VECTOR:
401 case TS_VECTOR:
402 case NP_VECTOR:
403 case SS_VECTOR:
404 case GP_VECTOR:
405 return EXCPT_CONTRIBUTORY;
406 default:
407 break;
408 }
409 return EXCPT_BENIGN;
410}
411
Nadav Amitd6e8c852014-07-24 14:51:24 +0300412#define EXCPT_FAULT 0
413#define EXCPT_TRAP 1
414#define EXCPT_ABORT 2
415#define EXCPT_INTERRUPT 3
416
417static int exception_type(int vector)
418{
419 unsigned int mask;
420
421 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
422 return EXCPT_INTERRUPT;
423
424 mask = 1 << vector;
425
426 /* #DB is trap, as instruction watchpoints are handled elsewhere */
427 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
428 return EXCPT_TRAP;
429
430 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
431 return EXCPT_ABORT;
432
433 /* Reserved exceptions will result in fault */
434 return EXCPT_FAULT;
435}
436
Jim Mattsonda998b42018-10-16 14:29:22 -0700437void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
438{
439 unsigned nr = vcpu->arch.exception.nr;
440 bool has_payload = vcpu->arch.exception.has_payload;
441 unsigned long payload = vcpu->arch.exception.payload;
442
443 if (!has_payload)
444 return;
445
446 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700447 case DB_VECTOR:
448 /*
449 * "Certain debug exceptions may clear bit 0-3. The
450 * remaining contents of the DR6 register are never
451 * cleared by the processor".
452 */
453 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
454 /*
455 * DR6.RTM is set by all #DB exceptions that don't clear it.
456 */
457 vcpu->arch.dr6 |= DR6_RTM;
458 vcpu->arch.dr6 |= payload;
459 /*
460 * Bit 16 should be set in the payload whenever the #DB
461 * exception should clear DR6.RTM. This makes the payload
462 * compatible with the pending debug exceptions under VMX.
463 * Though not currently documented in the SDM, this also
464 * makes the payload compatible with the exit qualification
465 * for #DB exceptions under VMX.
466 */
467 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800468
469 /*
470 * The #DB payload is defined as compatible with the 'pending
471 * debug exceptions' field under VMX, not DR6. While bit 12 is
472 * defined in the 'pending debug exceptions' field (enabled
473 * breakpoint), it is reserved and must be zero in DR6.
474 */
475 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700476 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700477 case PF_VECTOR:
478 vcpu->arch.cr2 = payload;
479 break;
480 }
481
482 vcpu->arch.exception.has_payload = false;
483 vcpu->arch.exception.payload = 0;
484}
485EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
486
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200487static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200488 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700489 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200490{
491 u32 prev_nr;
492 int class1, class2;
493
Avi Kivity3842d132010-07-27 12:30:24 +0300494 kvm_make_request(KVM_REQ_EVENT, vcpu);
495
Wanpeng Li664f8e22017-08-24 03:35:09 -0700496 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200497 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200498 if (has_error && !is_protmode(vcpu))
499 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700500 if (reinject) {
501 /*
502 * On vmentry, vcpu->arch.exception.pending is only
503 * true if an event injection was blocked by
504 * nested_run_pending. In that case, however,
505 * vcpu_enter_guest requests an immediate exit,
506 * and the guest shouldn't proceed far enough to
507 * need reinjection.
508 */
509 WARN_ON_ONCE(vcpu->arch.exception.pending);
510 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700511 if (WARN_ON_ONCE(has_payload)) {
512 /*
513 * A reinjected event has already
514 * delivered its payload.
515 */
516 has_payload = false;
517 payload = 0;
518 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700519 } else {
520 vcpu->arch.exception.pending = true;
521 vcpu->arch.exception.injected = false;
522 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200523 vcpu->arch.exception.has_error_code = has_error;
524 vcpu->arch.exception.nr = nr;
525 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700526 vcpu->arch.exception.has_payload = has_payload;
527 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800528 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700529 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200530 return;
531 }
532
533 /* to check exception */
534 prev_nr = vcpu->arch.exception.nr;
535 if (prev_nr == DF_VECTOR) {
536 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300537 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200538 return;
539 }
540 class1 = exception_class(prev_nr);
541 class2 = exception_class(nr);
542 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
543 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700544 /*
545 * Generate double fault per SDM Table 5-5. Set
546 * exception.pending = true so that the double fault
547 * can trigger a nested vmexit.
548 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200549 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700550 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200551 vcpu->arch.exception.has_error_code = true;
552 vcpu->arch.exception.nr = DF_VECTOR;
553 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700554 vcpu->arch.exception.has_payload = false;
555 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200556 } else
557 /* replace previous exception with a new one in a hope
558 that instruction re-execution will regenerate lost
559 exception */
560 goto queue;
561}
562
Avi Kivity298101d2007-11-25 13:41:11 +0200563void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
564{
Jim Mattson91e86d22018-10-16 14:29:21 -0700565 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200566}
567EXPORT_SYMBOL_GPL(kvm_queue_exception);
568
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200569void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
570{
Jim Mattson91e86d22018-10-16 14:29:21 -0700571 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200572}
573EXPORT_SYMBOL_GPL(kvm_requeue_exception);
574
Jim Mattsonf10c7292018-10-16 14:29:23 -0700575static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
576 unsigned long payload)
577{
578 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
579}
580
Jim Mattsonda998b42018-10-16 14:29:22 -0700581static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
582 u32 error_code, unsigned long payload)
583{
584 kvm_multiple_exception(vcpu, nr, true, error_code,
585 true, payload, false);
586}
587
Kyle Huey6affcbe2016-11-29 12:40:40 -0800588int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200589{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100590 if (err)
591 kvm_inject_gp(vcpu, 0);
592 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800593 return kvm_skip_emulated_instruction(vcpu);
594
595 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100596}
597EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200598
Avi Kivity6389ee92010-11-29 16:12:30 +0200599void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200600{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200601 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700602 vcpu->arch.exception.nested_apf =
603 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700604 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700605 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700606 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
607 } else {
608 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
609 fault->address);
610 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200611}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300612EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200613
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200614static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200615{
Avi Kivity6389ee92010-11-29 16:12:30 +0200616 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
617 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200618 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200619 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200620
621 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200622}
623
Sheng Yang3419ffc2008-05-15 09:52:48 +0800624void kvm_inject_nmi(struct kvm_vcpu *vcpu)
625{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300626 atomic_inc(&vcpu->arch.nmi_queued);
627 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800628}
629EXPORT_SYMBOL_GPL(kvm_inject_nmi);
630
Avi Kivity298101d2007-11-25 13:41:11 +0200631void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
632{
Jim Mattson91e86d22018-10-16 14:29:21 -0700633 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200634}
635EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
636
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200637void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
638{
Jim Mattson91e86d22018-10-16 14:29:21 -0700639 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200640}
641EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
642
Carsten Ottea03490e2007-10-29 16:09:35 +0100643/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300644 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
645 * a #GP and return false.
646 */
647bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200648{
Avi Kivity0a79b002009-09-01 12:03:25 +0300649 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
650 return true;
651 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
652 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100653}
Avi Kivity0a79b002009-09-01 12:03:25 +0300654EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100655
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300656bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
657{
658 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
659 return true;
660
661 kvm_queue_exception(vcpu, UD_VECTOR);
662 return false;
663}
664EXPORT_SYMBOL_GPL(kvm_require_dr);
665
Carsten Ottea03490e2007-10-29 16:09:35 +0100666/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200667 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200668 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200669 * can read from guest physical or from the guest's guest physical memory.
670 */
671int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
672 gfn_t ngfn, void *data, int offset, int len,
673 u32 access)
674{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200675 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200676 gfn_t real_gfn;
677 gpa_t ngpa;
678
679 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200680 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200681 if (real_gfn == UNMAPPED_GVA)
682 return -EFAULT;
683
684 real_gfn = gpa_to_gfn(real_gfn);
685
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200686 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200687}
688EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
689
Fengguang Wu69b00492015-01-19 22:33:39 +0800690static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200691 void *data, int offset, int len, u32 access)
692{
693 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
694 data, offset, len, access);
695}
696
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700697static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
698{
699 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
700 rsvd_bits(1, 2);
701}
702
Joerg Roedelec92fe42010-09-10 17:30:51 +0200703/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700704 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100705 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200706int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100707{
708 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
709 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
710 int i;
711 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200712 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100713
Joerg Roedelff03a072010-09-10 17:30:57 +0200714 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
715 offset * sizeof(u64), sizeof(pdpte),
716 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100717 if (ret < 0) {
718 ret = 0;
719 goto out;
720 }
721 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400722 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700723 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100724 ret = 0;
725 goto out;
726 }
727 }
728 ret = 1;
729
Joerg Roedelff03a072010-09-10 17:30:57 +0200730 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700731 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
732
Carsten Ottea03490e2007-10-29 16:09:35 +0100733out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100734
735 return ret;
736}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100737EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100738
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100739bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200740{
Joerg Roedelff03a072010-09-10 17:30:57 +0200741 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200742 int offset;
743 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200744 int r;
745
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200746 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200747 return false;
748
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700749 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300750 return true;
751
Paolo Bonzinia5121772017-07-24 18:54:38 +0200752 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
753 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200754 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
755 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200756 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800757 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200758
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800759 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200760}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100761EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200762
Avi Kivity49a9b072010-06-10 17:02:14 +0300763int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100764{
Sheng Yangaad82702010-05-12 16:40:42 +0800765 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800766 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800767
Avi Kivityf9a48e62010-01-06 19:10:22 +0200768 cr0 |= X86_CR0_ET;
769
Gleb Natapovab344822010-01-21 15:28:46 +0200770#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300771 if (cr0 & 0xffffffff00000000UL)
772 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200773#endif
774
775 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100776
Gleb Natapov0f122442010-04-28 19:15:31 +0300777 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
778 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100779
Gleb Natapov0f122442010-04-28 19:15:31 +0300780 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
781 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100782
783 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
784#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200785 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100786 int cs_db, cs_l;
787
Gleb Natapov0f122442010-04-28 19:15:31 +0300788 if (!is_pae(vcpu))
789 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100790 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300791 if (cs_l)
792 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100793 } else
794#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200795 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200796 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300797 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100798 }
799
Mao, Junjiead756a12012-07-02 01:18:48 +0000800 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
801 return 1;
802
Carsten Ottea03490e2007-10-29 16:09:35 +0100803 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100804
Lai Jiangshand170c412011-02-21 11:21:30 +0800805 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800806 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800807 kvm_async_pf_hash_reset(vcpu);
808 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800809
Sheng Yangaad82702010-05-12 16:40:42 +0800810 if ((cr0 ^ old_cr0) & update_bits)
811 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800812
Laszlo Ersek879ae182015-11-04 12:54:41 +0100813 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
814 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
815 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800816 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
817
Gleb Natapov0f122442010-04-28 19:15:31 +0300818 return 0;
819}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200820EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100821
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200822void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100823{
Avi Kivity49a9b072010-06-10 17:02:14 +0300824 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100825}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200826EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100827
Aaron Lewis139a12c2019-10-21 16:30:25 -0700828void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300829{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700830 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300831
Aaron Lewis139a12c2019-10-21 16:30:25 -0700832 if (vcpu->arch.xcr0 != host_xcr0)
833 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
834
835 if (vcpu->arch.xsaves_enabled &&
836 vcpu->arch.ia32_xss != host_xss)
837 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
838 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300839}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700840EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
841
842void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
843{
844 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
845
846 if (vcpu->arch.xcr0 != host_xcr0)
847 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
848
849 if (vcpu->arch.xsaves_enabled &&
850 vcpu->arch.ia32_xss != host_xss)
851 wrmsrl(MSR_IA32_XSS, host_xss);
852 }
853
854}
855EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300856
Fengguang Wu69b00492015-01-19 22:33:39 +0800857static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800858{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000859 u64 xcr0 = xcr;
860 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200861 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800862
863 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
864 if (index != XCR_XFEATURE_ENABLED_MASK)
865 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700866 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800867 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700868 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800869 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200870
871 /*
872 * Do not allow the guest to set bits that we do not support
873 * saving. However, xcr0 bit 0 is always set, even if the
874 * emulated CPU does not support XSAVE (see fx_init).
875 */
Dave Hansend91cab72015-09-02 16:31:26 -0700876 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200877 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800878 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200879
Dave Hansend91cab72015-09-02 16:31:26 -0700880 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
881 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000882 return 1;
883
Dave Hansend91cab72015-09-02 16:31:26 -0700884 if (xcr0 & XFEATURE_MASK_AVX512) {
885 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800886 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700887 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800888 return 1;
889 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800890 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000891
Dave Hansend91cab72015-09-02 16:31:26 -0700892 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000893 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800894 return 0;
895}
896
897int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
898{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000899 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
900 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800901 kvm_inject_gp(vcpu, 0);
902 return 1;
903 }
904 return 0;
905}
906EXPORT_SYMBOL_GPL(kvm_set_xcr);
907
Sean Christopherson345599f2019-12-10 14:44:16 -0800908#define __cr4_reserved_bits(__cpu_has, __c) \
909({ \
910 u64 __reserved_bits = CR4_RESERVED_BITS; \
911 \
912 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
913 __reserved_bits |= X86_CR4_OSXSAVE; \
914 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
915 __reserved_bits |= X86_CR4_SMEP; \
916 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
917 __reserved_bits |= X86_CR4_SMAP; \
918 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
919 __reserved_bits |= X86_CR4_FSGSBASE; \
920 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
921 __reserved_bits |= X86_CR4_PKE; \
922 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
923 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800924 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
925 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800926 __reserved_bits; \
927})
928
Sean Christophersonb11306b2019-12-10 14:44:13 -0800929static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)
930{
Sean Christopherson345599f2019-12-10 14:44:16 -0800931 u64 reserved_bits = __cr4_reserved_bits(cpu_has, c);
Sean Christophersonb11306b2019-12-10 14:44:13 -0800932
Sean Christophersonc10398b2020-03-02 15:56:46 -0800933 if (kvm_cpu_cap_has(X86_FEATURE_LA57))
Sean Christopherson345599f2019-12-10 14:44:16 -0800934 reserved_bits &= ~X86_CR4_LA57;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800935
Sean Christopherson90d2f602020-03-02 15:56:47 -0800936 if (kvm_cpu_cap_has(X86_FEATURE_UMIP))
Sean Christopherson345599f2019-12-10 14:44:16 -0800937 reserved_bits &= ~X86_CR4_UMIP;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800938
939 return reserved_bits;
940}
941
Wanpeng Li3ca94192019-09-18 17:50:10 +0800942static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
943{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800944 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800945 return -EINVAL;
946
Sean Christopherson345599f2019-12-10 14:44:16 -0800947 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800948 return -EINVAL;
949
950 return 0;
951}
952
Avi Kivitya83b29c2010-06-10 17:02:15 +0300953int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100954{
Avi Kivityfc78f512009-12-07 12:16:48 +0200955 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800956 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800957 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800958
Wanpeng Li3ca94192019-09-18 17:50:10 +0800959 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200960 return 1;
961
Carsten Ottea03490e2007-10-29 16:09:35 +0100962 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300963 if (!(cr4 & X86_CR4_PAE))
964 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300965 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
966 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200967 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
968 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300969 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100970
Mao, Junjiead756a12012-07-02 01:18:48 +0000971 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200972 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000973 return 1;
974
975 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
976 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
977 return 1;
978 }
979
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300980 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300981 return 1;
982
Mao, Junjiead756a12012-07-02 01:18:48 +0000983 if (((cr4 ^ old_cr4) & pdptr_bits) ||
984 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800985 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300986
Huaitong Hanb9baba82016-03-22 16:51:21 +0800987 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200988 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800989
Gleb Natapov0f122442010-04-28 19:15:31 +0300990 return 0;
991}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200992EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100993
Avi Kivity23902182010-06-10 17:02:16 +0300994int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100995{
Junaid Shahidade61e22018-06-27 14:59:15 -0700996 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100997#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700998 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
999
Junaid Shahidade61e22018-06-27 14:59:15 -07001000 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001001 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1002 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001003 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001004#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001005
Avi Kivity9f8fe502010-12-05 17:30:00 +02001006 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001007 if (!skip_tlb_flush) {
1008 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -07001009 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001010 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001011 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001012 }
1013
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001014 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001015 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001016 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001017 else if (is_pae_paging(vcpu) &&
1018 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001019 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001020
Junaid Shahidade61e22018-06-27 14:59:15 -07001021 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001022 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001023 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001024
Gleb Natapov0f122442010-04-28 19:15:31 +03001025 return 0;
1026}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001027EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001028
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001029int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001030{
Gleb Natapov0f122442010-04-28 19:15:31 +03001031 if (cr8 & CR8_RESERVED_BITS)
1032 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001033 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001034 kvm_lapic_set_tpr(vcpu, cr8);
1035 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001036 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001037 return 0;
1038}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001039EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001040
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001041unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001042{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001043 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001044 return kvm_lapic_get_cr8(vcpu);
1045 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001046 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001047}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001048EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001049
Nadav Amitae561ed2015-04-02 03:10:37 +03001050static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1051{
1052 int i;
1053
1054 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1055 for (i = 0; i < KVM_NR_DB_REGS; i++)
1056 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1057 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1058 }
1059}
1060
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001061static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1062{
1063 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1064 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1065}
1066
Jan Kiszkac8639012012-09-21 05:42:55 +02001067static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1068{
1069 unsigned long dr7;
1070
1071 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1072 dr7 = vcpu->arch.guest_debug_dr7;
1073 else
1074 dr7 = vcpu->arch.dr7;
1075 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001076 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1077 if (dr7 & DR7_BP_EN_MASK)
1078 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001079}
1080
Nadav Amit6f43ed02014-07-15 17:37:46 +03001081static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1082{
1083 u64 fixed = DR6_FIXED_1;
1084
Radim Krčmářd6321d42017-08-05 00:12:49 +02001085 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001086 fixed |= DR6_RTM;
1087 return fixed;
1088}
1089
Gleb Natapov338dbc92010-04-28 19:15:32 +03001090static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001091{
Marios Pomonisea740052019-12-11 12:47:52 -08001092 size_t size = ARRAY_SIZE(vcpu->arch.db);
1093
Gleb Natapov020df072010-04-13 10:05:23 +03001094 switch (dr) {
1095 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001096 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001097 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1098 vcpu->arch.eff_db[dr] = val;
1099 break;
1100 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001101 /* fall through */
1102 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001103 if (val & 0xffffffff00000000ULL)
1104 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001105 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001106 kvm_update_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001107 break;
1108 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001109 /* fall through */
1110 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001111 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001112 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001113 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001114 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001115 break;
1116 }
1117
1118 return 0;
1119}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001120
1121int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1122{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001123 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001124 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001125 return 1;
1126 }
1127 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001128}
Gleb Natapov020df072010-04-13 10:05:23 +03001129EXPORT_SYMBOL_GPL(kvm_set_dr);
1130
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001131int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001132{
Marios Pomonisea740052019-12-11 12:47:52 -08001133 size_t size = ARRAY_SIZE(vcpu->arch.db);
1134
Gleb Natapov020df072010-04-13 10:05:23 +03001135 switch (dr) {
1136 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001137 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001138 break;
1139 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001140 /* fall through */
1141 case 6:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001142 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1143 *val = vcpu->arch.dr6;
1144 else
1145 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001146 break;
1147 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001148 /* fall through */
1149 default: /* 7 */
1150 *val = vcpu->arch.dr7;
1151 break;
1152 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001153 return 0;
1154}
Gleb Natapov020df072010-04-13 10:05:23 +03001155EXPORT_SYMBOL_GPL(kvm_get_dr);
1156
Avi Kivity022cd0e2011-11-10 14:57:23 +02001157bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1158{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001159 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001160 u64 data;
1161 int err;
1162
Wei Huangc6702c92015-06-19 13:44:45 +02001163 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001164 if (err)
1165 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001166 kvm_rax_write(vcpu, (u32)data);
1167 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001168 return err;
1169}
1170EXPORT_SYMBOL_GPL(kvm_rdpmc);
1171
Carsten Otte043405e2007-10-10 17:16:19 +02001172/*
1173 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1174 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1175 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001176 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1177 * extract the supported MSRs from the related const lists.
1178 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001179 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001180 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001181 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001182 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001183
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001184static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001185 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001186 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001187#ifdef CONFIG_X86_64
1188 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1189#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001190 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001191 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001192 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001193 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1194 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1195 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1196 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1197 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1198 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001199 MSR_IA32_UMWAIT_CONTROL,
1200
Jim Mattsone2ada662019-08-21 11:20:04 -07001201 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1202 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1203 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1204 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1205 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1206 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1207 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1208 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1209 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1210 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1211 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1212 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1213 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001214 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1215 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1216 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1217 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1218 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1219 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1220 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1221 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1222 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001223};
1224
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001225static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001226static unsigned num_msrs_to_save;
1227
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001228static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001229 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1230 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1231 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1232 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001233 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001234 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1235 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001236 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001237 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001238 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001239 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001240 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001241 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001242 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1243 HV_X64_MSR_TSC_EMULATION_STATUS,
1244
1245 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001246 MSR_KVM_PV_EOI_EN,
1247
Will Auldba904632012-11-29 12:42:50 -08001248 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001249 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001250 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001251 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001252 MSR_IA32_MCG_STATUS,
1253 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001254 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001255 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001256 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001257 MSR_PLATFORM_INFO,
1258 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001259 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001260 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001261 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001262
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001263 /*
1264 * The following list leaves out MSRs whose values are determined
1265 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1266 * We always support the "true" VMX control MSRs, even if the host
1267 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001268 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001269 */
1270 MSR_IA32_VMX_BASIC,
1271 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1272 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1273 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1274 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1275 MSR_IA32_VMX_MISC,
1276 MSR_IA32_VMX_CR0_FIXED0,
1277 MSR_IA32_VMX_CR4_FIXED0,
1278 MSR_IA32_VMX_VMCS_ENUM,
1279 MSR_IA32_VMX_PROCBASED_CTLS2,
1280 MSR_IA32_VMX_EPT_VPID_CAP,
1281 MSR_IA32_VMX_VMFUNC,
1282
Borislav Petkov191c8132019-04-18 18:32:50 +02001283 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001284 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001285};
1286
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001287static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001288static unsigned num_emulated_msrs;
1289
Tom Lendacky801e4592018-02-21 13:39:51 -06001290/*
1291 * List of msr numbers which are used to expose MSR-based features that
1292 * can be used by a hypervisor to validate requested CPU features.
1293 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001294static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001295 MSR_IA32_VMX_BASIC,
1296 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1297 MSR_IA32_VMX_PINBASED_CTLS,
1298 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1299 MSR_IA32_VMX_PROCBASED_CTLS,
1300 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1301 MSR_IA32_VMX_EXIT_CTLS,
1302 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1303 MSR_IA32_VMX_ENTRY_CTLS,
1304 MSR_IA32_VMX_MISC,
1305 MSR_IA32_VMX_CR0_FIXED0,
1306 MSR_IA32_VMX_CR0_FIXED1,
1307 MSR_IA32_VMX_CR4_FIXED0,
1308 MSR_IA32_VMX_CR4_FIXED1,
1309 MSR_IA32_VMX_VMCS_ENUM,
1310 MSR_IA32_VMX_PROCBASED_CTLS2,
1311 MSR_IA32_VMX_EPT_VPID_CAP,
1312 MSR_IA32_VMX_VMFUNC,
1313
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001314 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001315 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001316 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001317};
1318
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001319static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001320static unsigned int num_msr_based_features;
1321
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001322static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001323{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001324 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001325
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001326 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1327 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001328
1329 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001330 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1331 * the nested hypervisor runs with NX huge pages. If it is not,
1332 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1333 * L1 guests, so it need not worry about its own (L2) guests.
1334 */
1335 data |= ARCH_CAP_PSCHANGE_MC_NO;
1336
1337 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001338 * If we're doing cache flushes (either "always" or "cond")
1339 * we will do one whenever the guest does a vmlaunch/vmresume.
1340 * If an outer hypervisor is doing the cache flush for us
1341 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1342 * capability to the guest too, and if EPT is disabled we're not
1343 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1344 * require a nested hypervisor to do a flush of its own.
1345 */
1346 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1347 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1348
Paolo Bonzini0c549142019-08-19 17:24:07 +02001349 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1350 data |= ARCH_CAP_RDCL_NO;
1351 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1352 data |= ARCH_CAP_SSB_NO;
1353 if (!boot_cpu_has_bug(X86_BUG_MDS))
1354 data |= ARCH_CAP_MDS_NO;
1355
Pawan Guptae1d38b62019-10-23 12:23:33 +02001356 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001357 * On TAA affected systems:
1358 * - nothing to do if TSX is disabled on the host.
1359 * - we emulate TSX_CTRL if present on the host.
1360 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001361 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001362 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001363 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001364 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1365 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001366
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001367 return data;
1368}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001369
Wanpeng Li66421c12018-02-28 14:03:30 +08001370static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1371{
1372 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001373 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001374 msr->data = kvm_get_arch_capabilities();
1375 break;
1376 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001377 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001378 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001379 default:
1380 if (kvm_x86_ops->get_msr_feature(msr))
1381 return 1;
1382 }
1383 return 0;
1384}
1385
Tom Lendacky801e4592018-02-21 13:39:51 -06001386static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1387{
1388 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001389 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001390
1391 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001392 r = kvm_get_msr_feature(&msr);
1393 if (r)
1394 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001395
1396 *data = msr.data;
1397
1398 return 0;
1399}
1400
Sean Christopherson11988492019-04-02 08:19:15 -07001401static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1402{
1403 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1404 return false;
1405
1406 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1407 return false;
1408
Sean Christopherson0a629562019-04-02 08:19:16 -07001409 if (efer & (EFER_LME | EFER_LMA) &&
1410 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1411 return false;
1412
1413 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1414 return false;
1415
Sean Christopherson11988492019-04-02 08:19:15 -07001416 return true;
1417
1418}
Jan Kiszka384bb782013-04-20 10:52:36 +02001419bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001420{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001421 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001422 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001423
Sean Christopherson11988492019-04-02 08:19:15 -07001424 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001425}
1426EXPORT_SYMBOL_GPL(kvm_valid_efer);
1427
Sean Christopherson11988492019-04-02 08:19:15 -07001428static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001429{
1430 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001431 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001432
Sean Christopherson11988492019-04-02 08:19:15 -07001433 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001434 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001435
Sean Christopherson11988492019-04-02 08:19:15 -07001436 if (!msr_info->host_initiated) {
1437 if (!__kvm_valid_efer(vcpu, efer))
1438 return 1;
1439
1440 if (is_paging(vcpu) &&
1441 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1442 return 1;
1443 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001444
Carsten Otte15c4a642007-10-30 18:44:17 +01001445 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001446 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001447
Sheng Yanga3d204e2010-05-12 16:40:40 +08001448 kvm_x86_ops->set_efer(vcpu, efer);
1449
Sheng Yangaad82702010-05-12 16:40:42 +08001450 /* Update reserved bits */
1451 if ((efer ^ old_efer) & EFER_NX)
1452 kvm_mmu_reset_context(vcpu);
1453
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001454 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001455}
1456
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001457void kvm_enable_efer_bits(u64 mask)
1458{
1459 efer_reserved_bits &= ~mask;
1460}
1461EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1462
Carsten Otte15c4a642007-10-30 18:44:17 +01001463/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001464 * Write @data into the MSR specified by @index. Select MSR specific fault
1465 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001466 * Returns 0 on success, non-0 otherwise.
1467 * Assumes vcpu_load() was already called.
1468 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001469static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1470 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001471{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001472 struct msr_data msr;
1473
1474 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001475 case MSR_FS_BASE:
1476 case MSR_GS_BASE:
1477 case MSR_KERNEL_GS_BASE:
1478 case MSR_CSTAR:
1479 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001480 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001481 return 1;
1482 break;
1483 case MSR_IA32_SYSENTER_EIP:
1484 case MSR_IA32_SYSENTER_ESP:
1485 /*
1486 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1487 * non-canonical address is written on Intel but not on
1488 * AMD (which ignores the top 32-bits, because it does
1489 * not implement 64-bit SYSENTER).
1490 *
1491 * 64-bit code should hence be able to write a non-canonical
1492 * value on AMD. Making the address canonical ensures that
1493 * vmentry does not fail on Intel after writing a non-canonical
1494 * value, and that something deterministic happens if the guest
1495 * invokes 64-bit SYSENTER.
1496 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001497 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001498 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001499
1500 msr.data = data;
1501 msr.index = index;
1502 msr.host_initiated = host_initiated;
1503
1504 return kvm_x86_ops->set_msr(vcpu, &msr);
1505}
1506
1507/*
1508 * Read the MSR specified by @index into @data. Select MSR specific fault
1509 * checks are bypassed if @host_initiated is %true.
1510 * Returns 0 on success, non-0 otherwise.
1511 * Assumes vcpu_load() was already called.
1512 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001513int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1514 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001515{
1516 struct msr_data msr;
1517 int ret;
1518
1519 msr.index = index;
1520 msr.host_initiated = host_initiated;
1521
1522 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1523 if (!ret)
1524 *data = msr.data;
1525 return ret;
1526}
1527
1528int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1529{
1530 return __kvm_get_msr(vcpu, index, data, false);
1531}
1532EXPORT_SYMBOL_GPL(kvm_get_msr);
1533
1534int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1535{
1536 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001537}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001538EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001539
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001540int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1541{
1542 u32 ecx = kvm_rcx_read(vcpu);
1543 u64 data;
1544
1545 if (kvm_get_msr(vcpu, ecx, &data)) {
1546 trace_kvm_msr_read_ex(ecx);
1547 kvm_inject_gp(vcpu, 0);
1548 return 1;
1549 }
1550
1551 trace_kvm_msr_read(ecx, data);
1552
1553 kvm_rax_write(vcpu, data & -1u);
1554 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1555 return kvm_skip_emulated_instruction(vcpu);
1556}
1557EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1558
1559int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1560{
1561 u32 ecx = kvm_rcx_read(vcpu);
1562 u64 data = kvm_read_edx_eax(vcpu);
1563
1564 if (kvm_set_msr(vcpu, ecx, data)) {
1565 trace_kvm_msr_write_ex(ecx, data);
1566 kvm_inject_gp(vcpu, 0);
1567 return 1;
1568 }
1569
1570 trace_kvm_msr_write(ecx, data);
1571 return kvm_skip_emulated_instruction(vcpu);
1572}
1573EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1574
Carsten Otte313a3dc2007-10-11 19:16:52 +02001575/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001576 * The fast path for frequent and performance sensitive wrmsr emulation,
1577 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1578 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1579 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1580 * other cases which must be called after interrupts are enabled on the host.
1581 */
1582static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1583{
1584 if (lapic_in_kernel(vcpu) && apic_x2apic_mode(vcpu->arch.apic) &&
1585 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
1586 ((data & APIC_MODE_MASK) == APIC_DM_FIXED)) {
1587
Wanpeng Lid5361672020-03-26 10:20:02 +08001588 data &= ~(1 << 12);
1589 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001590 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001591 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1592 trace_kvm_apic_write(APIC_ICR, (u32)data);
1593 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001594 }
1595
1596 return 1;
1597}
1598
1599enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1600{
1601 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001602 u64 data;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001603 int ret = 0;
1604
1605 switch (msr) {
1606 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001607 data = kvm_read_edx_eax(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001608 ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1609 break;
1610 default:
1611 return EXIT_FASTPATH_NONE;
1612 }
1613
1614 if (!ret) {
1615 trace_kvm_msr_write(msr, data);
1616 return EXIT_FASTPATH_SKIP_EMUL_INS;
1617 }
1618
1619 return EXIT_FASTPATH_NONE;
1620}
1621EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1622
1623/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001624 * Adapt set_msr() to msr_io()'s calling convention
1625 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001626static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1627{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001628 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001629}
1630
Carsten Otte313a3dc2007-10-11 19:16:52 +02001631static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1632{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001633 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001634}
1635
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001636#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001637struct pvclock_clock {
1638 int vclock_mode;
1639 u64 cycle_last;
1640 u64 mask;
1641 u32 mult;
1642 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001643 u64 base_cycles;
1644 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001645};
1646
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001647struct pvclock_gtod_data {
1648 seqcount_t seq;
1649
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001650 struct pvclock_clock clock; /* extract of a clocksource struct */
1651 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001652
Paolo Bonzini917f9472020-01-22 14:32:20 +01001653 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001654 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001655};
1656
1657static struct pvclock_gtod_data pvclock_gtod_data;
1658
1659static void update_pvclock_gtod(struct timekeeper *tk)
1660{
1661 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1662
1663 write_seqcount_begin(&vdata->seq);
1664
1665 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001666 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1667 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1668 vdata->clock.mask = tk->tkr_mono.mask;
1669 vdata->clock.mult = tk->tkr_mono.mult;
1670 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001671 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1672 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001673
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001674 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode;
1675 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1676 vdata->raw_clock.mask = tk->tkr_raw.mask;
1677 vdata->raw_clock.mult = tk->tkr_raw.mult;
1678 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001679 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1680 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001681
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001682 vdata->wall_time_sec = tk->xtime_sec;
1683
Paolo Bonzini917f9472020-01-22 14:32:20 +01001684 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001685
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001686 write_seqcount_end(&vdata->seq);
1687}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001688
1689static s64 get_kvmclock_base_ns(void)
1690{
1691 /* Count up from boot time, but with the frequency of the raw clock. */
1692 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1693}
1694#else
1695static s64 get_kvmclock_base_ns(void)
1696{
1697 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1698 return ktime_get_boottime_ns();
1699}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001700#endif
1701
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001702void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1703{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001704 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001705 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001706}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001707
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001708static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1709{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001710 int version;
1711 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001712 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001713 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001714
1715 if (!wall_clock)
1716 return;
1717
Avi Kivity9ed3c442010-05-04 15:00:37 +03001718 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1719 if (r)
1720 return;
1721
1722 if (version & 1)
1723 ++version; /* first time write, random junk */
1724
1725 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001726
Nicholas Krause1dab1342015-12-30 13:08:46 -05001727 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1728 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001729
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001730 /*
1731 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001732 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001733 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001734 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001735 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001736
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001737 wc.nsec = do_div(wall_nsec, 1000000000);
1738 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001739 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001740
1741 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1742
1743 version++;
1744 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001745}
1746
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001747static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1748{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001749 do_shl32_div32(dividend, divisor);
1750 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001751}
1752
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001753static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001754 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001755{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001756 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001757 int32_t shift = 0;
1758 uint64_t tps64;
1759 uint32_t tps32;
1760
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001761 tps64 = base_hz;
1762 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001763 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001764 tps64 >>= 1;
1765 shift--;
1766 }
1767
1768 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001769 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1770 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001771 scaled64 >>= 1;
1772 else
1773 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001774 shift++;
1775 }
1776
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001777 *pshift = shift;
1778 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001779}
1780
Marcelo Tosattid8281992012-11-27 23:29:01 -02001781#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001782static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001783#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001784
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001785static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001786static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001787
Zachary Amsdencc578282012-02-03 15:43:50 -02001788static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001789{
Zachary Amsdencc578282012-02-03 15:43:50 -02001790 u64 v = (u64)khz * (1000000 + ppm);
1791 do_div(v, 1000000);
1792 return v;
1793}
1794
Haozhong Zhang381d5852015-10-20 15:39:04 +08001795static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1796{
1797 u64 ratio;
1798
1799 /* Guest TSC same frequency as host TSC? */
1800 if (!scale) {
1801 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1802 return 0;
1803 }
1804
1805 /* TSC scaling supported? */
1806 if (!kvm_has_tsc_control) {
1807 if (user_tsc_khz > tsc_khz) {
1808 vcpu->arch.tsc_catchup = 1;
1809 vcpu->arch.tsc_always_catchup = 1;
1810 return 0;
1811 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001812 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001813 return -1;
1814 }
1815 }
1816
1817 /* TSC scaling required - calculate ratio */
1818 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1819 user_tsc_khz, tsc_khz);
1820
1821 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001822 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1823 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001824 return -1;
1825 }
1826
1827 vcpu->arch.tsc_scaling_ratio = ratio;
1828 return 0;
1829}
1830
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001831static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001832{
1833 u32 thresh_lo, thresh_hi;
1834 int use_scaling = 0;
1835
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001836 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001837 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001838 /* set tsc_scaling_ratio to a safe value */
1839 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001840 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001841 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001842
Zachary Amsdenc2855452010-09-18 14:38:15 -10001843 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001844 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001845 &vcpu->arch.virtual_tsc_shift,
1846 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001847 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001848
1849 /*
1850 * Compute the variation in TSC rate which is acceptable
1851 * within the range of tolerance and decide if the
1852 * rate being applied is within that bounds of the hardware
1853 * rate. If so, no scaling or compensation need be done.
1854 */
1855 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1856 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001857 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1858 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 -02001859 use_scaling = 1;
1860 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001861 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001862}
1863
1864static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1865{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001866 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001867 vcpu->arch.virtual_tsc_mult,
1868 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001869 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001870 return tsc;
1871}
1872
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001873static inline int gtod_is_based_on_tsc(int mode)
1874{
1875 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1876}
1877
Fengguang Wu69b00492015-01-19 22:33:39 +08001878static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001879{
1880#ifdef CONFIG_X86_64
1881 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001882 struct kvm_arch *ka = &vcpu->kvm->arch;
1883 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1884
1885 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1886 atomic_read(&vcpu->kvm->online_vcpus));
1887
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001888 /*
1889 * Once the masterclock is enabled, always perform request in
1890 * order to update it.
1891 *
1892 * In order to enable masterclock, the host clocksource must be TSC
1893 * and the vcpus need to have matched TSCs. When that happens,
1894 * perform request to enable masterclock.
1895 */
1896 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001897 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001898 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1899
1900 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1901 atomic_read(&vcpu->kvm->online_vcpus),
1902 ka->use_master_clock, gtod->clock.vclock_mode);
1903#endif
1904}
1905
Will Auldba904632012-11-29 12:42:50 -08001906static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1907{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001908 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001909 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1910}
1911
Haozhong Zhang35181e82015-10-20 15:39:03 +08001912/*
1913 * Multiply tsc by a fixed point number represented by ratio.
1914 *
1915 * The most significant 64-N bits (mult) of ratio represent the
1916 * integral part of the fixed point number; the remaining N bits
1917 * (frac) represent the fractional part, ie. ratio represents a fixed
1918 * point number (mult + frac * 2^(-N)).
1919 *
1920 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1921 */
1922static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1923{
1924 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1925}
1926
1927u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1928{
1929 u64 _tsc = tsc;
1930 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1931
1932 if (ratio != kvm_default_tsc_scaling_ratio)
1933 _tsc = __scale_tsc(ratio, tsc);
1934
1935 return _tsc;
1936}
1937EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1938
Haozhong Zhang07c14192015-10-20 15:39:05 +08001939static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1940{
1941 u64 tsc;
1942
1943 tsc = kvm_scale_tsc(vcpu, rdtsc());
1944
1945 return target_tsc - tsc;
1946}
1947
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001948u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1949{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001950 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1951
1952 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001953}
1954EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1955
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001956static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1957{
Leonid Shatz326e7422018-11-06 12:14:25 +02001958 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001959}
1960
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001961static inline bool kvm_check_tsc_unstable(void)
1962{
1963#ifdef CONFIG_X86_64
1964 /*
1965 * TSC is marked unstable when we're running on Hyper-V,
1966 * 'TSC page' clocksource is good.
1967 */
1968 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1969 return false;
1970#endif
1971 return check_tsc_unstable();
1972}
1973
Will Auld8fe8ab42012-11-29 12:42:12 -08001974void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001975{
1976 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001977 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001978 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001979 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001980 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001981 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001982 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001983
Jan Kiszka038f8c12011-02-04 10:49:11 +01001984 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001985 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001986 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001987 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001988
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001989 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001990 if (data == 0 && msr->host_initiated) {
1991 /*
1992 * detection of vcpu initialization -- need to sync
1993 * with other vCPUs. This particularly helps to keep
1994 * kvm_clock stable after CPU hotplug
1995 */
1996 synchronizing = true;
1997 } else {
1998 u64 tsc_exp = kvm->arch.last_tsc_write +
1999 nsec_to_cycles(vcpu, elapsed);
2000 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
2001 /*
2002 * Special case: TSC write with a small delta (1 second)
2003 * of virtual cycle time against real time is
2004 * interpreted as an attempt to synchronize the CPU.
2005 */
2006 synchronizing = data < tsc_exp + tsc_hz &&
2007 data + tsc_hz > tsc_exp;
2008 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002009 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002010
2011 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002012 * For a reliable TSC, we can match TSC offsets, and for an unstable
2013 * TSC, we add elapsed time in this computation. We could let the
2014 * compensation code attempt to catch up if we fall behind, but
2015 * it's better to try to match offsets from the beginning.
2016 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002017 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002018 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002019 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002020 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002021 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002022 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002023 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002024 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002025 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002026 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002027 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002028 } else {
2029 /*
2030 * We split periods of matched TSC writes into generations.
2031 * For each generation, we track the original measured
2032 * nanosecond time, offset, and write, so if TSCs are in
2033 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002034 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002035 *
2036 * These values are tracked in kvm->arch.cur_xxx variables.
2037 */
2038 kvm->arch.cur_tsc_generation++;
2039 kvm->arch.cur_tsc_nsec = ns;
2040 kvm->arch.cur_tsc_write = data;
2041 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002042 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002043 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002044
2045 /*
2046 * We also track th most recent recorded KHZ, write and time to
2047 * allow the matching interval to be extended at each write.
2048 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002049 kvm->arch.last_tsc_nsec = ns;
2050 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002051 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002052
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002053 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002054
2055 /* Keep track of which generation this VCPU has synchronized to */
2056 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2057 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2058 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2059
Radim Krčmářd6321d42017-08-05 00:12:49 +02002060 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002061 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002062
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002063 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002064 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002065
2066 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002067 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002068 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002069 } else if (!already_matched) {
2070 kvm->arch.nr_vcpus_matched_tsc++;
2071 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002072
2073 kvm_track_tsc_matching(vcpu);
2074 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002075}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002076
Zachary Amsden99e3e302010-08-19 22:07:17 -10002077EXPORT_SYMBOL_GPL(kvm_write_tsc);
2078
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002079static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2080 s64 adjustment)
2081{
Leonid Shatz326e7422018-11-06 12:14:25 +02002082 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
2083 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002084}
2085
2086static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2087{
2088 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2089 WARN_ON(adjustment < 0);
2090 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002091 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002092}
2093
Marcelo Tosattid8281992012-11-27 23:29:01 -02002094#ifdef CONFIG_X86_64
2095
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002096static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002097{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002098 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002099 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002100
2101 if (likely(ret >= last))
2102 return ret;
2103
2104 /*
2105 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002106 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002107 * very likely) and there's a data dependence, so force GCC
2108 * to generate a branch instead. I don't barrier() because
2109 * we don't actually need a barrier, and if this function
2110 * ever gets inlined it will generate worse code.
2111 */
2112 asm volatile ("");
2113 return last;
2114}
2115
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002116static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2117 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002118{
2119 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002120 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002121
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002122 switch (clock->vclock_mode) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002123 case VCLOCK_HVCLOCK:
2124 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2125 tsc_timestamp);
2126 if (tsc_pg_val != U64_MAX) {
2127 /* TSC page valid */
2128 *mode = VCLOCK_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002129 v = (tsc_pg_val - clock->cycle_last) &
2130 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002131 } else {
2132 /* TSC page invalid */
2133 *mode = VCLOCK_NONE;
2134 }
2135 break;
2136 case VCLOCK_TSC:
2137 *mode = VCLOCK_TSC;
2138 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002139 v = (*tsc_timestamp - clock->cycle_last) &
2140 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002141 break;
2142 default:
2143 *mode = VCLOCK_NONE;
2144 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002145
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002146 if (*mode == VCLOCK_NONE)
2147 *tsc_timestamp = v = 0;
2148
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002149 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002150}
2151
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002152static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002153{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002154 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002155 unsigned long seq;
2156 int mode;
2157 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002158
Marcelo Tosattid8281992012-11-27 23:29:01 -02002159 do {
2160 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002161 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002162 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002163 ns >>= gtod->raw_clock.shift;
2164 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002165 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002166 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002167
2168 return mode;
2169}
2170
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002171static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002172{
2173 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2174 unsigned long seq;
2175 int mode;
2176 u64 ns;
2177
2178 do {
2179 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002180 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002181 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002182 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002183 ns >>= gtod->clock.shift;
2184 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2185
2186 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2187 ts->tv_nsec = ns;
2188
2189 return mode;
2190}
2191
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002192/* returns true if host is using TSC based clocksource */
2193static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002194{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002195 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002196 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002197 return false;
2198
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002199 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002200 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002201}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002202
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002203/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002204static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002205 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002206{
2207 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002208 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002209 return false;
2210
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002211 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002212}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002213#endif
2214
2215/*
2216 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002217 * Assuming a stable TSC across physical CPUS, and a stable TSC
2218 * across virtual CPUs, the following condition is possible.
2219 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002220 * CPUs at the next numbered event.
2221 *
2222 * "timespecX" represents host monotonic time. "tscX" represents
2223 * RDTSC value.
2224 *
2225 * VCPU0 on CPU0 | VCPU1 on CPU1
2226 *
2227 * 1. read timespec0,tsc0
2228 * 2. | timespec1 = timespec0 + N
2229 * | tsc1 = tsc0 + M
2230 * 3. transition to guest | transition to guest
2231 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2232 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2233 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2234 *
2235 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2236 *
2237 * - ret0 < ret1
2238 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2239 * ...
2240 * - 0 < N - M => M < N
2241 *
2242 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2243 * always the case (the difference between two distinct xtime instances
2244 * might be smaller then the difference between corresponding TSC reads,
2245 * when updating guest vcpus pvclock areas).
2246 *
2247 * To avoid that problem, do not allow visibility of distinct
2248 * system_timestamp/tsc_timestamp values simultaneously: use a master
2249 * copy of host monotonic time values. Update that master copy
2250 * in lockstep.
2251 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002252 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002253 *
2254 */
2255
2256static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2257{
2258#ifdef CONFIG_X86_64
2259 struct kvm_arch *ka = &kvm->arch;
2260 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002261 bool host_tsc_clocksource, vcpus_matched;
2262
2263 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2264 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002265
2266 /*
2267 * If the host uses TSC clock, then passthrough TSC as stable
2268 * to the guest.
2269 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002270 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002271 &ka->master_kernel_ns,
2272 &ka->master_cycle_now);
2273
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002274 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002275 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002276 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002277
Marcelo Tosattid8281992012-11-27 23:29:01 -02002278 if (ka->use_master_clock)
2279 atomic_set(&kvm_guest_has_master_clock, 1);
2280
2281 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002282 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2283 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002284#endif
2285}
2286
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002287void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2288{
2289 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2290}
2291
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002292static void kvm_gen_update_masterclock(struct kvm *kvm)
2293{
2294#ifdef CONFIG_X86_64
2295 int i;
2296 struct kvm_vcpu *vcpu;
2297 struct kvm_arch *ka = &kvm->arch;
2298
2299 spin_lock(&ka->pvclock_gtod_sync_lock);
2300 kvm_make_mclock_inprogress_request(kvm);
2301 /* no guest entries from this point */
2302 pvclock_update_vm_gtod_copy(kvm);
2303
2304 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002305 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002306
2307 /* guest entries allowed */
2308 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002309 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002310
2311 spin_unlock(&ka->pvclock_gtod_sync_lock);
2312#endif
2313}
2314
Marcelo Tosattie891a322017-04-17 12:51:37 -03002315u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002316{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002317 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002318 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002319 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002320
Paolo Bonzini8b953442016-11-16 18:31:30 +01002321 spin_lock(&ka->pvclock_gtod_sync_lock);
2322 if (!ka->use_master_clock) {
2323 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002324 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002325 }
2326
Paolo Bonzini8b953442016-11-16 18:31:30 +01002327 hv_clock.tsc_timestamp = ka->master_cycle_now;
2328 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2329 spin_unlock(&ka->pvclock_gtod_sync_lock);
2330
Wanpeng Lie2c22062017-05-11 18:12:05 -07002331 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2332 get_cpu();
2333
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002334 if (__this_cpu_read(cpu_tsc_khz)) {
2335 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2336 &hv_clock.tsc_shift,
2337 &hv_clock.tsc_to_system_mul);
2338 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2339 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002340 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002341
2342 put_cpu();
2343
2344 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002345}
2346
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002347static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2348{
2349 struct kvm_vcpu_arch *vcpu = &v->arch;
2350 struct pvclock_vcpu_time_info guest_hv_clock;
2351
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002352 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002353 &guest_hv_clock, sizeof(guest_hv_clock))))
2354 return;
2355
2356 /* This VCPU is paused, but it's legal for a guest to read another
2357 * VCPU's kvmclock, so we really have to follow the specification where
2358 * it says that version is odd if data is being modified, and even after
2359 * it is consistent.
2360 *
2361 * Version field updates must be kept separate. This is because
2362 * kvm_write_guest_cached might use a "rep movs" instruction, and
2363 * writes within a string instruction are weakly ordered. So there
2364 * are three writes overall.
2365 *
2366 * As a small optimization, only write the version field in the first
2367 * and third write. The vcpu->pv_time cache is still valid, because the
2368 * version field is the first in the struct.
2369 */
2370 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2371
Liran Alon51c4b8b2017-11-05 16:11:30 +02002372 if (guest_hv_clock.version & 1)
2373 ++guest_hv_clock.version; /* first time write, random junk */
2374
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002375 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002376 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2377 &vcpu->hv_clock,
2378 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002379
2380 smp_wmb();
2381
2382 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2383 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2384
2385 if (vcpu->pvclock_set_guest_stopped_request) {
2386 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2387 vcpu->pvclock_set_guest_stopped_request = false;
2388 }
2389
2390 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2391
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002392 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2393 &vcpu->hv_clock,
2394 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002395
2396 smp_wmb();
2397
2398 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002399 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2400 &vcpu->hv_clock,
2401 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002402}
2403
Zachary Amsden34c238a2010-09-18 14:38:14 -10002404static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002405{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002406 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002407 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002408 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002409 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002410 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002411 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002412 bool use_master_clock;
2413
2414 kernel_ns = 0;
2415 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002416
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002417 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002418 * If the host uses TSC clock, then passthrough TSC as stable
2419 * to the guest.
2420 */
2421 spin_lock(&ka->pvclock_gtod_sync_lock);
2422 use_master_clock = ka->use_master_clock;
2423 if (use_master_clock) {
2424 host_tsc = ka->master_cycle_now;
2425 kernel_ns = ka->master_kernel_ns;
2426 }
2427 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002428
2429 /* Keep irq disabled to prevent changes to the clock */
2430 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002431 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2432 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002433 local_irq_restore(flags);
2434 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2435 return 1;
2436 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002437 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002438 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002439 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002440 }
2441
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002442 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002443
2444 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002445 * We may have to catch up the TSC to match elapsed wall clock
2446 * time for two reasons, even if kvmclock is used.
2447 * 1) CPU could have been running below the maximum TSC rate
2448 * 2) Broken TSC compensation resets the base at each VCPU
2449 * entry to avoid unknown leaps of TSC even when running
2450 * again on the same CPU. This may cause apparent elapsed
2451 * time to disappear, and the guest to stand still or run
2452 * very slowly.
2453 */
2454 if (vcpu->tsc_catchup) {
2455 u64 tsc = compute_guest_tsc(v, kernel_ns);
2456 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002457 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002458 tsc_timestamp = tsc;
2459 }
2460 }
2461
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002462 local_irq_restore(flags);
2463
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002464 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002465
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002466 if (kvm_has_tsc_control)
2467 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2468
2469 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002470 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002471 &vcpu->hv_clock.tsc_shift,
2472 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002473 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002474 }
2475
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002476 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002477 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002478 vcpu->last_guest_tsc = tsc_timestamp;
Paolo Bonzini9446e6f2020-02-12 13:27:10 +01002479 WARN_ON((s64)vcpu->hv_clock.system_time < 0);
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002480
Marcelo Tosattid8281992012-11-27 23:29:01 -02002481 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002482 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002483 if (use_master_clock)
2484 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2485
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002486 vcpu->hv_clock.flags = pvclock_flags;
2487
Paolo Bonzini095cf552016-02-08 12:54:12 +01002488 if (vcpu->pv_time_enabled)
2489 kvm_setup_pvclock_page(v);
2490 if (v == kvm_get_vcpu(v->kvm, 0))
2491 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002492 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002493}
2494
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002495/*
2496 * kvmclock updates which are isolated to a given vcpu, such as
2497 * vcpu->cpu migration, should not allow system_timestamp from
2498 * the rest of the vcpus to remain static. Otherwise ntp frequency
2499 * correction applies to one vcpu's system_timestamp but not
2500 * the others.
2501 *
2502 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002503 * We need to rate-limit these requests though, as they can
2504 * considerably slow guests that have a large number of vcpus.
2505 * The time for a remote vcpu to update its kvmclock is bound
2506 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002507 */
2508
Andrew Jones7e44e442014-02-28 12:52:54 +01002509#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2510
2511static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002512{
2513 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002514 struct delayed_work *dwork = to_delayed_work(work);
2515 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2516 kvmclock_update_work);
2517 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002518 struct kvm_vcpu *vcpu;
2519
2520 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002521 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002522 kvm_vcpu_kick(vcpu);
2523 }
2524}
2525
Andrew Jones7e44e442014-02-28 12:52:54 +01002526static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2527{
2528 struct kvm *kvm = v->kvm;
2529
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002530 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002531 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2532 KVMCLOCK_UPDATE_DELAY);
2533}
2534
Andrew Jones332967a2014-02-28 12:52:55 +01002535#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2536
2537static void kvmclock_sync_fn(struct work_struct *work)
2538{
2539 struct delayed_work *dwork = to_delayed_work(work);
2540 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2541 kvmclock_sync_work);
2542 struct kvm *kvm = container_of(ka, struct kvm, arch);
2543
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002544 if (!kvmclock_periodic_sync)
2545 return;
2546
Andrew Jones332967a2014-02-28 12:52:55 +01002547 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2548 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2549 KVMCLOCK_SYNC_PERIOD);
2550}
2551
Borislav Petkov191c8132019-04-18 18:32:50 +02002552/*
2553 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2554 */
2555static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2556{
2557 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002558 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002559 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2560
2561 return false;
2562}
2563
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002564static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002565{
2566 u64 mcg_cap = vcpu->arch.mcg_cap;
2567 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002568 u32 msr = msr_info->index;
2569 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002570
2571 switch (msr) {
2572 case MSR_IA32_MCG_STATUS:
2573 vcpu->arch.mcg_status = data;
2574 break;
2575 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002576 if (!(mcg_cap & MCG_CTL_P) &&
2577 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002578 return 1;
2579 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002580 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002581 vcpu->arch.mcg_ctl = data;
2582 break;
2583 default:
2584 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002585 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002586 u32 offset = array_index_nospec(
2587 msr - MSR_IA32_MC0_CTL,
2588 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2589
Andre Przywara114be422010-03-24 17:46:42 +01002590 /* only 0 or all 1s can be written to IA32_MCi_CTL
2591 * some Linux kernels though clear bit 10 in bank 4 to
2592 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2593 * this to avoid an uncatched #GP in the guest
2594 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002595 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002596 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002597 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002598
2599 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002600 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002601 (offset & 0x3) == 1 && data != 0) {
2602 if (!can_set_mci_status(vcpu))
2603 return -1;
2604 }
2605
Huang Ying890ca9a2009-05-11 16:48:15 +08002606 vcpu->arch.mce_banks[offset] = data;
2607 break;
2608 }
2609 return 1;
2610 }
2611 return 0;
2612}
2613
Ed Swierkffde22a2009-10-15 15:21:43 -07002614static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2615{
2616 struct kvm *kvm = vcpu->kvm;
2617 int lm = is_long_mode(vcpu);
2618 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2619 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2620 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2621 : kvm->arch.xen_hvm_config.blob_size_32;
2622 u32 page_num = data & ~PAGE_MASK;
2623 u64 page_addr = data & PAGE_MASK;
2624 u8 *page;
2625 int r;
2626
2627 r = -E2BIG;
2628 if (page_num >= blob_size)
2629 goto out;
2630 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002631 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2632 if (IS_ERR(page)) {
2633 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002634 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002635 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002636 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002637 goto out_free;
2638 r = 0;
2639out_free:
2640 kfree(page);
2641out:
2642 return r;
2643}
2644
Gleb Natapov344d9582010-10-14 11:22:50 +02002645static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2646{
2647 gpa_t gpa = data & ~0x3f;
2648
Wanpeng Li52a5c152017-07-13 18:30:42 -07002649 /* Bits 3:5 are reserved, Should be zero */
2650 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002651 return 1;
2652
2653 vcpu->arch.apf.msr_val = data;
2654
2655 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2656 kvm_clear_async_pf_completion_queue(vcpu);
2657 kvm_async_pf_hash_reset(vcpu);
2658 return 0;
2659 }
2660
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002661 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002662 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002663 return 1;
2664
Gleb Natapov6adba522010-10-14 11:22:55 +02002665 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002666 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002667 kvm_async_pf_wakeup_all(vcpu);
2668 return 0;
2669}
2670
Glauber Costa12f9a482011-02-01 14:16:40 -05002671static void kvmclock_reset(struct kvm_vcpu *vcpu)
2672{
Andy Honig0b794592013-02-20 14:48:10 -08002673 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002674 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002675}
2676
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002677static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2678{
2679 ++vcpu->stat.tlb_flush;
2680 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2681}
2682
Glauber Costac9aaa892011-07-11 15:28:14 -04002683static void record_steal_time(struct kvm_vcpu *vcpu)
2684{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002685 struct kvm_host_map map;
2686 struct kvm_steal_time *st;
2687
Glauber Costac9aaa892011-07-11 15:28:14 -04002688 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2689 return;
2690
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002691 /* -EAGAIN is returned in atomic context so we can just return. */
2692 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2693 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002694 return;
2695
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002696 st = map.hva +
2697 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2698
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002699 /*
2700 * Doing a TLB flush here, on the guest's behalf, can avoid
2701 * expensive IPIs.
2702 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002703 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002704 st->preempted & KVM_VCPU_FLUSH_TLB);
2705 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002706 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002707
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002708 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002709
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002710 if (st->version & 1)
2711 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002712
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002713 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002714
2715 smp_wmb();
2716
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002717 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002718 vcpu->arch.st.last_steal;
2719 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002720
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002721 smp_wmb();
2722
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002723 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002724
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002725 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002726}
2727
Will Auld8fe8ab42012-11-29 12:42:12 -08002728int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002729{
Gleb Natapov57537852012-01-15 14:17:22 +02002730 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002731 u32 msr = msr_info->index;
2732 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002733
Carsten Otte15c4a642007-10-30 18:44:17 +01002734 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002735 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002736 case MSR_IA32_UCODE_WRITE:
2737 case MSR_VM_HSAVE_PA:
2738 case MSR_AMD64_PATCH_LOADER:
2739 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002740 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002741 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002742 break;
2743
Wanpeng Li518e7b92018-02-28 14:03:31 +08002744 case MSR_IA32_UCODE_REV:
2745 if (msr_info->host_initiated)
2746 vcpu->arch.microcode_version = data;
2747 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002748 case MSR_IA32_ARCH_CAPABILITIES:
2749 if (!msr_info->host_initiated)
2750 return 1;
2751 vcpu->arch.arch_capabilities = data;
2752 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002753 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002754 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002755 case MSR_K7_HWCR:
2756 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002757 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002758 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002759
2760 /* Handle McStatusWrEn */
2761 if (data == BIT_ULL(18)) {
2762 vcpu->arch.msr_hwcr = data;
2763 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002764 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2765 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002766 return 1;
2767 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002768 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002769 case MSR_FAM10H_MMIO_CONF_BASE:
2770 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002771 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2772 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002773 return 1;
2774 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002775 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002776 case MSR_IA32_DEBUGCTLMSR:
2777 if (!data) {
2778 /* We support the non-activated case already */
2779 break;
2780 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2781 /* Values other than LBR and BTF are vendor-specific,
2782 thus reserved and should throw a #GP */
2783 return 1;
2784 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002785 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2786 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002787 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002788 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002789 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002790 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002791 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002792 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2793 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002794 case MSR_IA32_TSCDEADLINE:
2795 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2796 break;
Will Auldba904632012-11-29 12:42:50 -08002797 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002798 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002799 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002800 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002801 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002802 }
2803 vcpu->arch.ia32_tsc_adjust_msr = data;
2804 }
2805 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002806 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002807 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2808 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2809 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2810 return 1;
2811 vcpu->arch.ia32_misc_enable_msr = data;
2812 kvm_update_cpuid(vcpu);
2813 } else {
2814 vcpu->arch.ia32_misc_enable_msr = data;
2815 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002816 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002817 case MSR_IA32_SMBASE:
2818 if (!msr_info->host_initiated)
2819 return 1;
2820 vcpu->arch.smbase = data;
2821 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002822 case MSR_IA32_POWER_CTL:
2823 vcpu->arch.msr_ia32_power_ctl = data;
2824 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002825 case MSR_IA32_TSC:
2826 kvm_write_tsc(vcpu, msr_info);
2827 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002828 case MSR_IA32_XSS:
2829 if (!msr_info->host_initiated &&
2830 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2831 return 1;
2832 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002833 * KVM supports exposing PT to the guest, but does not support
2834 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2835 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002836 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002837 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002838 return 1;
2839 vcpu->arch.ia32_xss = data;
2840 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002841 case MSR_SMI_COUNT:
2842 if (!msr_info->host_initiated)
2843 return 1;
2844 vcpu->arch.smi_count = data;
2845 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002846 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002847 case MSR_KVM_WALL_CLOCK:
2848 vcpu->kvm->arch.wall_clock = data;
2849 kvm_write_wall_clock(vcpu->kvm, data);
2850 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002851 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002852 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002853 struct kvm_arch *ka = &vcpu->kvm->arch;
2854
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002855 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2856 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2857
2858 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002859 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002860
2861 ka->boot_vcpu_runs_old_kvmclock = tmp;
2862 }
2863
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002864 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002865 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002866
2867 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002868 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002869 if (!(data & 1))
2870 break;
2871
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002872 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002873 &vcpu->arch.pv_time, data & ~1ULL,
2874 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002875 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002876
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002877 break;
2878 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002879 case MSR_KVM_ASYNC_PF_EN:
2880 if (kvm_pv_enable_async_pf(vcpu, data))
2881 return 1;
2882 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002883 case MSR_KVM_STEAL_TIME:
2884
2885 if (unlikely(!sched_info_on()))
2886 return 1;
2887
2888 if (data & KVM_STEAL_RESERVED_MASK)
2889 return 1;
2890
Glauber Costac9aaa892011-07-11 15:28:14 -04002891 vcpu->arch.st.msr_val = data;
2892
2893 if (!(data & KVM_MSR_ENABLED))
2894 break;
2895
Glauber Costac9aaa892011-07-11 15:28:14 -04002896 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2897
2898 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002899 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002900 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002901 return 1;
2902 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002903
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002904 case MSR_KVM_POLL_CONTROL:
2905 /* only enable bit supported */
2906 if (data & (-1ULL << 1))
2907 return 1;
2908
2909 vcpu->arch.msr_kvm_poll_control = data;
2910 break;
2911
Huang Ying890ca9a2009-05-11 16:48:15 +08002912 case MSR_IA32_MCG_CTL:
2913 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002914 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002915 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002916
Wei Huang6912ac32015-06-12 01:34:56 -04002917 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2918 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2919 pr = true; /* fall through */
2920 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2921 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002922 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002923 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002924
2925 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002926 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2927 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002928 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002929 case MSR_K7_CLK_CTL:
2930 /*
2931 * Ignore all writes to this no longer documented MSR.
2932 * Writes are only relevant for old K7 processors,
2933 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002934 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002935 * affected processor models on the command line, hence
2936 * the need to ignore the workaround.
2937 */
2938 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002939 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002940 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2941 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002942 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002943 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2944 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2945 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002946 return kvm_hv_set_msr_common(vcpu, msr, data,
2947 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002948 case MSR_IA32_BBL_CR_CTL3:
2949 /* Drop writes to this legacy MSR -- see rdmsr
2950 * counterpart for further detail.
2951 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002952 if (report_ignored_msrs)
2953 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2954 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002955 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002956 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002957 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002958 return 1;
2959 vcpu->arch.osvw.length = data;
2960 break;
2961 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002962 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002963 return 1;
2964 vcpu->arch.osvw.status = data;
2965 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002966 case MSR_PLATFORM_INFO:
2967 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002968 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2969 cpuid_fault_enabled(vcpu)))
2970 return 1;
2971 vcpu->arch.msr_platform_info = data;
2972 break;
2973 case MSR_MISC_FEATURES_ENABLES:
2974 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2975 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2976 !supports_cpuid_fault(vcpu)))
2977 return 1;
2978 vcpu->arch.msr_misc_features_enables = data;
2979 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002980 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002981 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2982 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002983 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002984 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002985 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002986 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002987 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002988 return 1;
2989 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002990 if (report_ignored_msrs)
2991 vcpu_unimpl(vcpu,
2992 "ignored wrmsr: 0x%x data 0x%llx\n",
2993 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002994 break;
2995 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002996 }
2997 return 0;
2998}
2999EXPORT_SYMBOL_GPL(kvm_set_msr_common);
3000
Paolo Bonzini44883f02018-07-26 13:01:52 +02003001static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003002{
3003 u64 data;
3004 u64 mcg_cap = vcpu->arch.mcg_cap;
3005 unsigned bank_num = mcg_cap & 0xff;
3006
3007 switch (msr) {
3008 case MSR_IA32_P5_MC_ADDR:
3009 case MSR_IA32_P5_MC_TYPE:
3010 data = 0;
3011 break;
3012 case MSR_IA32_MCG_CAP:
3013 data = vcpu->arch.mcg_cap;
3014 break;
3015 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003016 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003017 return 1;
3018 data = vcpu->arch.mcg_ctl;
3019 break;
3020 case MSR_IA32_MCG_STATUS:
3021 data = vcpu->arch.mcg_status;
3022 break;
3023 default:
3024 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003025 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003026 u32 offset = array_index_nospec(
3027 msr - MSR_IA32_MC0_CTL,
3028 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3029
Huang Ying890ca9a2009-05-11 16:48:15 +08003030 data = vcpu->arch.mce_banks[offset];
3031 break;
3032 }
3033 return 1;
3034 }
3035 *pdata = data;
3036 return 0;
3037}
3038
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003039int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003040{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003041 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003042 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003043 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003044 case MSR_IA32_DEBUGCTLMSR:
3045 case MSR_IA32_LASTBRANCHFROMIP:
3046 case MSR_IA32_LASTBRANCHTOIP:
3047 case MSR_IA32_LASTINTFROMIP:
3048 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303049 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003050 case MSR_K8_TSEG_ADDR:
3051 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003052 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003053 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003054 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003055 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003056 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003057 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003058 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003059 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003060 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003061 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003062 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003063 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3064 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3065 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3066 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003067 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003068 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3069 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003070 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003071 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003072 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003073 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003074 case MSR_IA32_ARCH_CAPABILITIES:
3075 if (!msr_info->host_initiated &&
3076 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3077 return 1;
3078 msr_info->data = vcpu->arch.arch_capabilities;
3079 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003080 case MSR_IA32_POWER_CTL:
3081 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3082 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003083 case MSR_IA32_TSC:
3084 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3085 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003086 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003087 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003088 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003089 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003090 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003091 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003092 /*
3093 * MSR_EBC_FREQUENCY_ID
3094 * Conservative value valid for even the basic CPU models.
3095 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3096 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3097 * and 266MHz for model 3, or 4. Set Core Clock
3098 * Frequency to System Bus Frequency Ratio to 1 (bits
3099 * 31:24) even though these are only valid for CPU
3100 * models > 2, however guests may end up dividing or
3101 * multiplying by zero otherwise.
3102 */
3103 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003104 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003105 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003106 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003107 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003108 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003109 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003110 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003111 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003112 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003113 break;
Will Auldba904632012-11-29 12:42:50 -08003114 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003115 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003116 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003117 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003118 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003119 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003120 case MSR_IA32_SMBASE:
3121 if (!msr_info->host_initiated)
3122 return 1;
3123 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003124 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003125 case MSR_SMI_COUNT:
3126 msr_info->data = vcpu->arch.smi_count;
3127 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003128 case MSR_IA32_PERF_STATUS:
3129 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003130 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003131 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003132 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003133 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003134 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003135 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003136 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003137 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003138 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003139 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003140 break;
3141 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003142 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003143 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003144 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003145 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003146 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003147 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003148 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003149 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003150 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003151 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003152 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003153 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003154 case MSR_KVM_POLL_CONTROL:
3155 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3156 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003157 case MSR_IA32_P5_MC_ADDR:
3158 case MSR_IA32_P5_MC_TYPE:
3159 case MSR_IA32_MCG_CAP:
3160 case MSR_IA32_MCG_CTL:
3161 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003162 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003163 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3164 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003165 case MSR_IA32_XSS:
3166 if (!msr_info->host_initiated &&
3167 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3168 return 1;
3169 msr_info->data = vcpu->arch.ia32_xss;
3170 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003171 case MSR_K7_CLK_CTL:
3172 /*
3173 * Provide expected ramp-up count for K7. All other
3174 * are set to zero, indicating minimum divisors for
3175 * every field.
3176 *
3177 * This prevents guest kernels on AMD host with CPU
3178 * type 6, model 8 and higher from exploding due to
3179 * the rdmsr failing.
3180 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003181 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003182 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003183 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003184 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3185 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003186 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003187 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3188 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3189 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003190 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003191 msr_info->index, &msr_info->data,
3192 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003193 case MSR_IA32_BBL_CR_CTL3:
3194 /* This legacy MSR exists but isn't fully documented in current
3195 * silicon. It is however accessed by winxp in very narrow
3196 * scenarios where it sets bit #19, itself documented as
3197 * a "reserved" bit. Best effort attempt to source coherent
3198 * read data here should the balance of the register be
3199 * interpreted by the guest:
3200 *
3201 * L2 cache control register 3: 64GB range, 256KB size,
3202 * enabled, latency 0x1, configured
3203 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003204 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003205 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003206 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003207 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003208 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003209 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003210 break;
3211 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003212 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003213 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003214 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003215 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003216 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003217 if (!msr_info->host_initiated &&
3218 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3219 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003220 msr_info->data = vcpu->arch.msr_platform_info;
3221 break;
3222 case MSR_MISC_FEATURES_ENABLES:
3223 msr_info->data = vcpu->arch.msr_misc_features_enables;
3224 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003225 case MSR_K7_HWCR:
3226 msr_info->data = vcpu->arch.msr_hwcr;
3227 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003228 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003229 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003230 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003231 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003232 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3233 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003234 return 1;
3235 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003236 if (report_ignored_msrs)
3237 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3238 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003239 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003240 }
3241 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003242 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003243 return 0;
3244}
3245EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3246
Carsten Otte313a3dc2007-10-11 19:16:52 +02003247/*
3248 * Read or write a bunch of msrs. All parameters are kernel addresses.
3249 *
3250 * @return number of msrs set successfully.
3251 */
3252static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3253 struct kvm_msr_entry *entries,
3254 int (*do_msr)(struct kvm_vcpu *vcpu,
3255 unsigned index, u64 *data))
3256{
Tom Lendacky801e4592018-02-21 13:39:51 -06003257 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003258
Carsten Otte313a3dc2007-10-11 19:16:52 +02003259 for (i = 0; i < msrs->nmsrs; ++i)
3260 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3261 break;
3262
Carsten Otte313a3dc2007-10-11 19:16:52 +02003263 return i;
3264}
3265
3266/*
3267 * Read or write a bunch of msrs. Parameters are user addresses.
3268 *
3269 * @return number of msrs set successfully.
3270 */
3271static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3272 int (*do_msr)(struct kvm_vcpu *vcpu,
3273 unsigned index, u64 *data),
3274 int writeback)
3275{
3276 struct kvm_msrs msrs;
3277 struct kvm_msr_entry *entries;
3278 int r, n;
3279 unsigned size;
3280
3281 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003282 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003283 goto out;
3284
3285 r = -E2BIG;
3286 if (msrs.nmsrs >= MAX_IO_MSRS)
3287 goto out;
3288
Carsten Otte313a3dc2007-10-11 19:16:52 +02003289 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003290 entries = memdup_user(user_msrs->entries, size);
3291 if (IS_ERR(entries)) {
3292 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003293 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003294 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003295
3296 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3297 if (r < 0)
3298 goto out_free;
3299
3300 r = -EFAULT;
3301 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3302 goto out_free;
3303
3304 r = n;
3305
3306out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003307 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003308out:
3309 return r;
3310}
3311
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003312static inline bool kvm_can_mwait_in_guest(void)
3313{
3314 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003315 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3316 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003317}
3318
Alexander Graf784aa3d2014-07-14 18:27:35 +02003319int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003320{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003321 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003322
3323 switch (ext) {
3324 case KVM_CAP_IRQCHIP:
3325 case KVM_CAP_HLT:
3326 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003327 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003328 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003329 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003330 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003331 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003332 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003333 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003334 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003335 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003336 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003337 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003338 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003339 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003340 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003341 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003342 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003343 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003344 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003345 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003346 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003347 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003348 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003349 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003350 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003351 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003352 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003353 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003354 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003355 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003356 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003357 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003358 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003359 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003360 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003361 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003362 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003363 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003364 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003365 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003366 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003367 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003368 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003369 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003370 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003371 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003372 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003373 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003374 r = 1;
3375 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003376 case KVM_CAP_SYNC_REGS:
3377 r = KVM_SYNC_X86_VALID_FIELDS;
3378 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003379 case KVM_CAP_ADJUST_CLOCK:
3380 r = KVM_CLOCK_TSC_STABLE;
3381 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003382 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003383 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3384 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003385 if(kvm_can_mwait_in_guest())
3386 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003387 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003388 case KVM_CAP_X86_SMM:
3389 /* SMBASE is usually relocated above 1M on modern chipsets,
3390 * and SMM handlers might indeed rely on 4G segment limits,
3391 * so do not report SMM to be available if real mode is
3392 * emulated via vm86 mode. Still, do not go to great lengths
3393 * to avoid userspace's usage of the feature, because it is a
3394 * fringe case that is not enabled except via specific settings
3395 * of the module parameters.
3396 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003397 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003398 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003399 case KVM_CAP_VAPIC:
3400 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3401 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003402 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003403 r = KVM_SOFT_MAX_VCPUS;
3404 break;
3405 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003406 r = KVM_MAX_VCPUS;
3407 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003408 case KVM_CAP_MAX_VCPU_ID:
3409 r = KVM_MAX_VCPU_ID;
3410 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003411 case KVM_CAP_PV_MMU: /* obsolete */
3412 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003413 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003414 case KVM_CAP_MCE:
3415 r = KVM_MAX_MCE_BANKS;
3416 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003417 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003418 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003419 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003420 case KVM_CAP_TSC_CONTROL:
3421 r = kvm_has_tsc_control;
3422 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003423 case KVM_CAP_X2APIC_API:
3424 r = KVM_X2APIC_API_VALID_FLAGS;
3425 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003426 case KVM_CAP_NESTED_STATE:
3427 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303428 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003429 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003430 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003431 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3432 break;
3433 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3434 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003435 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003436 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003437 break;
3438 }
3439 return r;
3440
3441}
3442
Carsten Otte043405e2007-10-10 17:16:19 +02003443long kvm_arch_dev_ioctl(struct file *filp,
3444 unsigned int ioctl, unsigned long arg)
3445{
3446 void __user *argp = (void __user *)arg;
3447 long r;
3448
3449 switch (ioctl) {
3450 case KVM_GET_MSR_INDEX_LIST: {
3451 struct kvm_msr_list __user *user_msr_list = argp;
3452 struct kvm_msr_list msr_list;
3453 unsigned n;
3454
3455 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003456 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003457 goto out;
3458 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003459 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003460 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003461 goto out;
3462 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003463 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003464 goto out;
3465 r = -EFAULT;
3466 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3467 num_msrs_to_save * sizeof(u32)))
3468 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003469 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003470 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003471 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003472 goto out;
3473 r = 0;
3474 break;
3475 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003476 case KVM_GET_SUPPORTED_CPUID:
3477 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003478 struct kvm_cpuid2 __user *cpuid_arg = argp;
3479 struct kvm_cpuid2 cpuid;
3480
3481 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003482 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003483 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003484
3485 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3486 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003487 if (r)
3488 goto out;
3489
3490 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003491 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003492 goto out;
3493 r = 0;
3494 break;
3495 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003496 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003497 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003498 if (copy_to_user(argp, &kvm_mce_cap_supported,
3499 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003500 goto out;
3501 r = 0;
3502 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003503 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3504 struct kvm_msr_list __user *user_msr_list = argp;
3505 struct kvm_msr_list msr_list;
3506 unsigned int n;
3507
3508 r = -EFAULT;
3509 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3510 goto out;
3511 n = msr_list.nmsrs;
3512 msr_list.nmsrs = num_msr_based_features;
3513 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3514 goto out;
3515 r = -E2BIG;
3516 if (n < msr_list.nmsrs)
3517 goto out;
3518 r = -EFAULT;
3519 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3520 num_msr_based_features * sizeof(u32)))
3521 goto out;
3522 r = 0;
3523 break;
3524 }
3525 case KVM_GET_MSRS:
3526 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3527 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003528 default:
3529 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003530 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003531 }
3532out:
3533 return r;
3534}
3535
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003536static void wbinvd_ipi(void *garbage)
3537{
3538 wbinvd();
3539}
3540
3541static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3542{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003543 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003544}
3545
Carsten Otte313a3dc2007-10-11 19:16:52 +02003546void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3547{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003548 /* Address WBINVD may be executed by guest */
3549 if (need_emulate_wbinvd(vcpu)) {
3550 if (kvm_x86_ops->has_wbinvd_exit())
3551 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3552 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3553 smp_call_function_single(vcpu->cpu,
3554 wbinvd_ipi, NULL, 1);
3555 }
3556
Carsten Otte313a3dc2007-10-11 19:16:52 +02003557 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003558
3559 /* Apply any externally detected TSC adjustments (due to suspend) */
3560 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3561 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3562 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003563 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003564 }
3565
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003566 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003567 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003568 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003569 if (tsc_delta < 0)
3570 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003571
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003572 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003573 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003574 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003575 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003576 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003577 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003578
3579 if (kvm_lapic_hv_timer_in_use(vcpu))
3580 kvm_lapic_restart_hv_timer(vcpu);
3581
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003582 /*
3583 * On a host with synchronized TSC, there is no need to update
3584 * kvmclock on vcpu->cpu migration
3585 */
3586 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003587 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003588 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003589 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003590 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003591 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003592
Glauber Costac9aaa892011-07-11 15:28:14 -04003593 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003594}
3595
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003596static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3597{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003598 struct kvm_host_map map;
3599 struct kvm_steal_time *st;
3600
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003601 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3602 return;
3603
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003604 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003605 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003606
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003607 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3608 &vcpu->arch.st.cache, true))
3609 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003610
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003611 st = map.hva +
3612 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3613
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003614 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003615
3616 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003617}
3618
Carsten Otte313a3dc2007-10-11 19:16:52 +02003619void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3620{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003621 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003622
3623 if (vcpu->preempted)
3624 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3625
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003626 /*
3627 * Disable page faults because we're in atomic context here.
3628 * kvm_write_guest_offset_cached() would call might_fault()
3629 * that relies on pagefault_disable() to tell if there's a
3630 * bug. NOTE: the write to guest memory may not go through if
3631 * during postcopy live migration or if there's heavy guest
3632 * paging.
3633 */
3634 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003635 /*
3636 * kvm_memslots() will be called by
3637 * kvm_write_guest_offset_cached() so take the srcu lock.
3638 */
3639 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003640 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003641 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003642 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003643 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003644 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003645 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003646 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3647 * on every vmexit, but if not, we might have a stale dr6 from the
3648 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003649 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003650 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003651}
3652
Carsten Otte313a3dc2007-10-11 19:16:52 +02003653static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3654 struct kvm_lapic_state *s)
3655{
Liran Alonfa59cc02017-12-24 18:12:53 +02003656 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003657 kvm_x86_ops->sync_pir_to_irr(vcpu);
3658
Radim Krčmářa92e2542016-07-12 22:09:22 +02003659 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003660}
3661
3662static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3663 struct kvm_lapic_state *s)
3664{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003665 int r;
3666
3667 r = kvm_apic_set_state(vcpu, s);
3668 if (r)
3669 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003670 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003671
3672 return 0;
3673}
3674
Matt Gingell127a4572015-11-17 17:32:05 +01003675static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3676{
3677 return (!lapic_in_kernel(vcpu) ||
3678 kvm_apic_accept_pic_intr(vcpu));
3679}
3680
Matt Gingell782d4222015-11-16 15:26:00 -08003681/*
3682 * if userspace requested an interrupt window, check that the
3683 * interrupt window is open.
3684 *
3685 * No need to exit to userspace if we already have an interrupt queued.
3686 */
3687static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3688{
3689 return kvm_arch_interrupt_allowed(vcpu) &&
3690 !kvm_cpu_has_interrupt(vcpu) &&
3691 !kvm_event_needs_reinjection(vcpu) &&
3692 kvm_cpu_accept_dm_intr(vcpu);
3693}
3694
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003695static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3696 struct kvm_interrupt *irq)
3697{
Chen Gang02cdb502013-02-27 11:33:25 +08003698 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003699 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003700
3701 if (!irqchip_in_kernel(vcpu->kvm)) {
3702 kvm_queue_interrupt(vcpu, irq->irq, false);
3703 kvm_make_request(KVM_REQ_EVENT, vcpu);
3704 return 0;
3705 }
3706
3707 /*
3708 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3709 * fail for in-kernel 8259.
3710 */
3711 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003712 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003713
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003714 if (vcpu->arch.pending_external_vector != -1)
3715 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003716
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003717 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003718 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003719 return 0;
3720}
3721
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003722static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3723{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003724 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003725
3726 return 0;
3727}
3728
Paolo Bonzinif0778252015-04-01 15:06:40 +02003729static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3730{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003731 kvm_make_request(KVM_REQ_SMI, vcpu);
3732
Paolo Bonzinif0778252015-04-01 15:06:40 +02003733 return 0;
3734}
3735
Avi Kivityb209749f2007-10-22 16:50:39 +02003736static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3737 struct kvm_tpr_access_ctl *tac)
3738{
3739 if (tac->flags)
3740 return -EINVAL;
3741 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3742 return 0;
3743}
3744
Huang Ying890ca9a2009-05-11 16:48:15 +08003745static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3746 u64 mcg_cap)
3747{
3748 int r;
3749 unsigned bank_num = mcg_cap & 0xff, bank;
3750
3751 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003752 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003753 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003754 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003755 goto out;
3756 r = 0;
3757 vcpu->arch.mcg_cap = mcg_cap;
3758 /* Init IA32_MCG_CTL to all 1s */
3759 if (mcg_cap & MCG_CTL_P)
3760 vcpu->arch.mcg_ctl = ~(u64)0;
3761 /* Init IA32_MCi_CTL to all 1s */
3762 for (bank = 0; bank < bank_num; bank++)
3763 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003764
Sean Christopherson92735b12019-08-02 15:06:17 -07003765 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003766out:
3767 return r;
3768}
3769
3770static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3771 struct kvm_x86_mce *mce)
3772{
3773 u64 mcg_cap = vcpu->arch.mcg_cap;
3774 unsigned bank_num = mcg_cap & 0xff;
3775 u64 *banks = vcpu->arch.mce_banks;
3776
3777 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3778 return -EINVAL;
3779 /*
3780 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3781 * reporting is disabled
3782 */
3783 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3784 vcpu->arch.mcg_ctl != ~(u64)0)
3785 return 0;
3786 banks += 4 * mce->bank;
3787 /*
3788 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3789 * reporting is disabled for the bank
3790 */
3791 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3792 return 0;
3793 if (mce->status & MCI_STATUS_UC) {
3794 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003795 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003796 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003797 return 0;
3798 }
3799 if (banks[1] & MCI_STATUS_VAL)
3800 mce->status |= MCI_STATUS_OVER;
3801 banks[2] = mce->addr;
3802 banks[3] = mce->misc;
3803 vcpu->arch.mcg_status = mce->mcg_status;
3804 banks[1] = mce->status;
3805 kvm_queue_exception(vcpu, MC_VECTOR);
3806 } else if (!(banks[1] & MCI_STATUS_VAL)
3807 || !(banks[1] & MCI_STATUS_UC)) {
3808 if (banks[1] & MCI_STATUS_VAL)
3809 mce->status |= MCI_STATUS_OVER;
3810 banks[2] = mce->addr;
3811 banks[3] = mce->misc;
3812 banks[1] = mce->status;
3813 } else
3814 banks[1] |= MCI_STATUS_OVER;
3815 return 0;
3816}
3817
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003818static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3819 struct kvm_vcpu_events *events)
3820{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003821 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003822
Wanpeng Li664f8e22017-08-24 03:35:09 -07003823 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003824 * In guest mode, payload delivery should be deferred,
3825 * so that the L1 hypervisor can intercept #PF before
3826 * CR2 is modified (or intercept #DB before DR6 is
3827 * modified under nVMX). Unless the per-VM capability,
3828 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3829 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3830 * opportunistically defer the exception payload, deliver it if the
3831 * capability hasn't been requested before processing a
3832 * KVM_GET_VCPU_EVENTS.
3833 */
3834 if (!vcpu->kvm->arch.exception_payload_enabled &&
3835 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3836 kvm_deliver_exception_payload(vcpu);
3837
3838 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003839 * The API doesn't provide the instruction length for software
3840 * exceptions, so don't report them. As long as the guest RIP
3841 * isn't advanced, we should expect to encounter the exception
3842 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003843 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003844 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3845 events->exception.injected = 0;
3846 events->exception.pending = 0;
3847 } else {
3848 events->exception.injected = vcpu->arch.exception.injected;
3849 events->exception.pending = vcpu->arch.exception.pending;
3850 /*
3851 * For ABI compatibility, deliberately conflate
3852 * pending and injected exceptions when
3853 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3854 */
3855 if (!vcpu->kvm->arch.exception_payload_enabled)
3856 events->exception.injected |=
3857 vcpu->arch.exception.pending;
3858 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003859 events->exception.nr = vcpu->arch.exception.nr;
3860 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3861 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003862 events->exception_has_payload = vcpu->arch.exception.has_payload;
3863 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003864
Jan Kiszka03b82a32010-02-15 10:45:41 +01003865 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003866 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003867 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003868 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003869 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003870
3871 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003872 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003873 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003874 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003875
Jan Kiszka66450a22013-03-13 12:42:34 +01003876 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003877
Paolo Bonzinif0778252015-04-01 15:06:40 +02003878 events->smi.smm = is_smm(vcpu);
3879 events->smi.pending = vcpu->arch.smi_pending;
3880 events->smi.smm_inside_nmi =
3881 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3882 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3883
Jan Kiszkadab4b912009-12-06 18:24:15 +01003884 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003885 | KVM_VCPUEVENT_VALID_SHADOW
3886 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003887 if (vcpu->kvm->arch.exception_payload_enabled)
3888 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3889
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003890 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003891}
3892
Sean Christophersonc5833c72019-04-02 08:03:10 -07003893static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003894
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003895static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3896 struct kvm_vcpu_events *events)
3897{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003898 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003899 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003900 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003901 | KVM_VCPUEVENT_VALID_SMM
3902 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003903 return -EINVAL;
3904
Jim Mattson59073aa2018-10-16 14:29:20 -07003905 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3906 if (!vcpu->kvm->arch.exception_payload_enabled)
3907 return -EINVAL;
3908 if (events->exception.pending)
3909 events->exception.injected = 0;
3910 else
3911 events->exception_has_payload = 0;
3912 } else {
3913 events->exception.pending = 0;
3914 events->exception_has_payload = 0;
3915 }
3916
3917 if ((events->exception.injected || events->exception.pending) &&
3918 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003919 return -EINVAL;
3920
David Hildenbrand28bf2882017-03-23 11:46:03 +01003921 /* INITs are latched while in SMM */
3922 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3923 (events->smi.smm || events->smi.pending) &&
3924 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3925 return -EINVAL;
3926
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003927 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003928 vcpu->arch.exception.injected = events->exception.injected;
3929 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003930 vcpu->arch.exception.nr = events->exception.nr;
3931 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3932 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003933 vcpu->arch.exception.has_payload = events->exception_has_payload;
3934 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003935
Liran Alon04140b42018-03-23 03:01:31 +03003936 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003937 vcpu->arch.interrupt.nr = events->interrupt.nr;
3938 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003939 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3940 kvm_x86_ops->set_interrupt_shadow(vcpu,
3941 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003942
3943 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003944 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3945 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003946 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3947
Jan Kiszka66450a22013-03-13 12:42:34 +01003948 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003949 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003950 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003951
Paolo Bonzinif0778252015-04-01 15:06:40 +02003952 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003953 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3954 if (events->smi.smm)
3955 vcpu->arch.hflags |= HF_SMM_MASK;
3956 else
3957 vcpu->arch.hflags &= ~HF_SMM_MASK;
3958 kvm_smm_changed(vcpu);
3959 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003960
Paolo Bonzinif0778252015-04-01 15:06:40 +02003961 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003962
3963 if (events->smi.smm) {
3964 if (events->smi.smm_inside_nmi)
3965 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003966 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003967 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003968 }
3969
3970 if (lapic_in_kernel(vcpu)) {
3971 if (events->smi.latched_init)
3972 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3973 else
3974 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003975 }
3976 }
3977
Avi Kivity3842d132010-07-27 12:30:24 +03003978 kvm_make_request(KVM_REQ_EVENT, vcpu);
3979
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003980 return 0;
3981}
3982
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003983static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3984 struct kvm_debugregs *dbgregs)
3985{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003986 unsigned long val;
3987
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003988 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003989 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003990 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003991 dbgregs->dr7 = vcpu->arch.dr7;
3992 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003993 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003994}
3995
3996static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3997 struct kvm_debugregs *dbgregs)
3998{
3999 if (dbgregs->flags)
4000 return -EINVAL;
4001
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004002 if (dbgregs->dr6 & ~0xffffffffull)
4003 return -EINVAL;
4004 if (dbgregs->dr7 & ~0xffffffffull)
4005 return -EINVAL;
4006
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004007 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004008 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004009 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004010 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004011 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004012 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004013
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004014 return 0;
4015}
4016
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004017#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4018
4019static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4020{
Marc Orrb666a4b2018-11-06 14:53:56 -08004021 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004022 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004023 u64 valid;
4024
4025 /*
4026 * Copy legacy XSAVE area, to avoid complications with CPUID
4027 * leaves 0 and 1 in the loop below.
4028 */
4029 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4030
4031 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004032 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004033 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4034
4035 /*
4036 * Copy each region from the possibly compacted offset to the
4037 * non-compacted offset.
4038 */
Dave Hansend91cab72015-09-02 16:31:26 -07004039 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004040 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004041 u64 xfeature_mask = valid & -valid;
4042 int xfeature_nr = fls64(xfeature_mask) - 1;
4043 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004044
4045 if (src) {
4046 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004047 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004048 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004049 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004050 memcpy(dest + offset, &vcpu->arch.pkru,
4051 sizeof(vcpu->arch.pkru));
4052 else
4053 memcpy(dest + offset, src, size);
4054
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004055 }
4056
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004057 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004058 }
4059}
4060
4061static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4062{
Marc Orrb666a4b2018-11-06 14:53:56 -08004063 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004064 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4065 u64 valid;
4066
4067 /*
4068 * Copy legacy XSAVE area, to avoid complications with CPUID
4069 * leaves 0 and 1 in the loop below.
4070 */
4071 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4072
4073 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004074 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004075 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004076 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004077
4078 /*
4079 * Copy each region from the non-compacted offset to the
4080 * possibly compacted offset.
4081 */
Dave Hansend91cab72015-09-02 16:31:26 -07004082 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004083 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004084 u64 xfeature_mask = valid & -valid;
4085 int xfeature_nr = fls64(xfeature_mask) - 1;
4086 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004087
4088 if (dest) {
4089 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004090 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004091 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004092 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004093 memcpy(&vcpu->arch.pkru, src + offset,
4094 sizeof(vcpu->arch.pkru));
4095 else
4096 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004097 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004098
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004099 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004100 }
4101}
4102
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004103static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4104 struct kvm_xsave *guest_xsave)
4105{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004106 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004107 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4108 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004109 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004110 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004111 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004112 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004113 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004114 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004115 }
4116}
4117
Wanpeng Lia5758132017-05-11 02:58:55 -07004118#define XSAVE_MXCSR_OFFSET 24
4119
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004120static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4121 struct kvm_xsave *guest_xsave)
4122{
4123 u64 xstate_bv =
4124 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004125 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004126
Borislav Petkovd366bf72016-04-04 22:25:02 +02004127 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004128 /*
4129 * Here we allow setting states that are not present in
4130 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4131 * with old userspace.
4132 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004133 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004134 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004135 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004136 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004137 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4138 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004139 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004140 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004141 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004142 }
4143 return 0;
4144}
4145
4146static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4147 struct kvm_xcrs *guest_xcrs)
4148{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004149 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004150 guest_xcrs->nr_xcrs = 0;
4151 return;
4152 }
4153
4154 guest_xcrs->nr_xcrs = 1;
4155 guest_xcrs->flags = 0;
4156 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4157 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4158}
4159
4160static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4161 struct kvm_xcrs *guest_xcrs)
4162{
4163 int i, r = 0;
4164
Borislav Petkovd366bf72016-04-04 22:25:02 +02004165 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004166 return -EINVAL;
4167
4168 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4169 return -EINVAL;
4170
4171 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4172 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004173 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004174 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004175 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004176 break;
4177 }
4178 if (r)
4179 r = -EINVAL;
4180 return r;
4181}
4182
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004183/*
4184 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4185 * stopped by the hypervisor. This function will be called from the host only.
4186 * EINVAL is returned when the host attempts to set the flag for a guest that
4187 * does not support pv clocks.
4188 */
4189static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4190{
Andy Honig0b794592013-02-20 14:48:10 -08004191 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004192 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004193 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004194 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4195 return 0;
4196}
4197
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004198static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4199 struct kvm_enable_cap *cap)
4200{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004201 int r;
4202 uint16_t vmcs_version;
4203 void __user *user_ptr;
4204
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004205 if (cap->flags)
4206 return -EINVAL;
4207
4208 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004209 case KVM_CAP_HYPERV_SYNIC2:
4210 if (cap->args[0])
4211 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004212 /* fall through */
4213
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004214 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004215 if (!irqchip_in_kernel(vcpu->kvm))
4216 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004217 return kvm_hv_activate_synic(vcpu, cap->cap ==
4218 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004219 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004220 if (!kvm_x86_ops->nested_enable_evmcs)
4221 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004222 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4223 if (!r) {
4224 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4225 if (copy_to_user(user_ptr, &vmcs_version,
4226 sizeof(vmcs_version)))
4227 r = -EFAULT;
4228 }
4229 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004230 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4231 if (!kvm_x86_ops->enable_direct_tlbflush)
4232 return -ENOTTY;
4233
4234 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004235
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004236 default:
4237 return -EINVAL;
4238 }
4239}
4240
Carsten Otte313a3dc2007-10-11 19:16:52 +02004241long kvm_arch_vcpu_ioctl(struct file *filp,
4242 unsigned int ioctl, unsigned long arg)
4243{
4244 struct kvm_vcpu *vcpu = filp->private_data;
4245 void __user *argp = (void __user *)arg;
4246 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004247 union {
4248 struct kvm_lapic_state *lapic;
4249 struct kvm_xsave *xsave;
4250 struct kvm_xcrs *xcrs;
4251 void *buffer;
4252 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004253
Christoffer Dall9b0624712017-12-04 21:35:36 +01004254 vcpu_load(vcpu);
4255
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004256 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004257 switch (ioctl) {
4258 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004259 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004260 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004261 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004262 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4263 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004264
Dave Hansenb772ff32008-08-11 10:01:47 -07004265 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004266 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004267 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004268 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004269 if (r)
4270 goto out;
4271 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004272 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004273 goto out;
4274 r = 0;
4275 break;
4276 }
4277 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004278 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004279 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004280 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004281 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004282 if (IS_ERR(u.lapic)) {
4283 r = PTR_ERR(u.lapic);
4284 goto out_nofree;
4285 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004286
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004287 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004288 break;
4289 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004290 case KVM_INTERRUPT: {
4291 struct kvm_interrupt irq;
4292
4293 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004294 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004295 goto out;
4296 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004297 break;
4298 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004299 case KVM_NMI: {
4300 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004301 break;
4302 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004303 case KVM_SMI: {
4304 r = kvm_vcpu_ioctl_smi(vcpu);
4305 break;
4306 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004307 case KVM_SET_CPUID: {
4308 struct kvm_cpuid __user *cpuid_arg = argp;
4309 struct kvm_cpuid cpuid;
4310
4311 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004312 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004313 goto out;
4314 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004315 break;
4316 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004317 case KVM_SET_CPUID2: {
4318 struct kvm_cpuid2 __user *cpuid_arg = argp;
4319 struct kvm_cpuid2 cpuid;
4320
4321 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004322 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004323 goto out;
4324 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004325 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004326 break;
4327 }
4328 case KVM_GET_CPUID2: {
4329 struct kvm_cpuid2 __user *cpuid_arg = argp;
4330 struct kvm_cpuid2 cpuid;
4331
4332 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004333 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004334 goto out;
4335 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004336 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004337 if (r)
4338 goto out;
4339 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004340 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004341 goto out;
4342 r = 0;
4343 break;
4344 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004345 case KVM_GET_MSRS: {
4346 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004347 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004348 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004349 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004350 }
4351 case KVM_SET_MSRS: {
4352 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004353 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004354 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004355 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004356 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004357 case KVM_TPR_ACCESS_REPORTING: {
4358 struct kvm_tpr_access_ctl tac;
4359
4360 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004361 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004362 goto out;
4363 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4364 if (r)
4365 goto out;
4366 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004367 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004368 goto out;
4369 r = 0;
4370 break;
4371 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004372 case KVM_SET_VAPIC_ADDR: {
4373 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004374 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004375
4376 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004377 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004378 goto out;
4379 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004380 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004381 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004382 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004383 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004384 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004385 break;
4386 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004387 case KVM_X86_SETUP_MCE: {
4388 u64 mcg_cap;
4389
4390 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004391 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004392 goto out;
4393 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4394 break;
4395 }
4396 case KVM_X86_SET_MCE: {
4397 struct kvm_x86_mce mce;
4398
4399 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004400 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004401 goto out;
4402 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4403 break;
4404 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004405 case KVM_GET_VCPU_EVENTS: {
4406 struct kvm_vcpu_events events;
4407
4408 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4409
4410 r = -EFAULT;
4411 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4412 break;
4413 r = 0;
4414 break;
4415 }
4416 case KVM_SET_VCPU_EVENTS: {
4417 struct kvm_vcpu_events events;
4418
4419 r = -EFAULT;
4420 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4421 break;
4422
4423 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4424 break;
4425 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004426 case KVM_GET_DEBUGREGS: {
4427 struct kvm_debugregs dbgregs;
4428
4429 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4430
4431 r = -EFAULT;
4432 if (copy_to_user(argp, &dbgregs,
4433 sizeof(struct kvm_debugregs)))
4434 break;
4435 r = 0;
4436 break;
4437 }
4438 case KVM_SET_DEBUGREGS: {
4439 struct kvm_debugregs dbgregs;
4440
4441 r = -EFAULT;
4442 if (copy_from_user(&dbgregs, argp,
4443 sizeof(struct kvm_debugregs)))
4444 break;
4445
4446 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4447 break;
4448 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004449 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004450 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004451 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004452 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004453 break;
4454
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004455 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004456
4457 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004458 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004459 break;
4460 r = 0;
4461 break;
4462 }
4463 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004464 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004465 if (IS_ERR(u.xsave)) {
4466 r = PTR_ERR(u.xsave);
4467 goto out_nofree;
4468 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004469
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004470 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004471 break;
4472 }
4473 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004474 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004475 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004476 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004477 break;
4478
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004479 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004480
4481 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004482 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004483 sizeof(struct kvm_xcrs)))
4484 break;
4485 r = 0;
4486 break;
4487 }
4488 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004489 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004490 if (IS_ERR(u.xcrs)) {
4491 r = PTR_ERR(u.xcrs);
4492 goto out_nofree;
4493 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004494
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004495 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004496 break;
4497 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004498 case KVM_SET_TSC_KHZ: {
4499 u32 user_tsc_khz;
4500
4501 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004502 user_tsc_khz = (u32)arg;
4503
4504 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4505 goto out;
4506
Zachary Amsdencc578282012-02-03 15:43:50 -02004507 if (user_tsc_khz == 0)
4508 user_tsc_khz = tsc_khz;
4509
Haozhong Zhang381d5852015-10-20 15:39:04 +08004510 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4511 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004512
Joerg Roedel92a1f122011-03-25 09:44:51 +01004513 goto out;
4514 }
4515 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004516 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004517 goto out;
4518 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004519 case KVM_KVMCLOCK_CTRL: {
4520 r = kvm_set_guest_paused(vcpu);
4521 goto out;
4522 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004523 case KVM_ENABLE_CAP: {
4524 struct kvm_enable_cap cap;
4525
4526 r = -EFAULT;
4527 if (copy_from_user(&cap, argp, sizeof(cap)))
4528 goto out;
4529 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4530 break;
4531 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004532 case KVM_GET_NESTED_STATE: {
4533 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4534 u32 user_data_size;
4535
4536 r = -EINVAL;
4537 if (!kvm_x86_ops->get_nested_state)
4538 break;
4539
4540 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004541 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004542 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004543 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004544
4545 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4546 user_data_size);
4547 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004548 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004549
4550 if (r > user_data_size) {
4551 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004552 r = -EFAULT;
4553 else
4554 r = -E2BIG;
4555 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004556 }
Liran Alon26b471c2018-09-16 14:28:20 +03004557
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004558 r = 0;
4559 break;
4560 }
4561 case KVM_SET_NESTED_STATE: {
4562 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4563 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004564 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004565
4566 r = -EINVAL;
4567 if (!kvm_x86_ops->set_nested_state)
4568 break;
4569
Liran Alon26b471c2018-09-16 14:28:20 +03004570 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004571 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004572 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004573
Liran Alon26b471c2018-09-16 14:28:20 +03004574 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004575 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004576 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004577
4578 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004579 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4580 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004581 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004582
4583 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004584 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4585 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004586 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004587
Sean Christophersonad5996d2019-11-22 08:58:18 -08004588 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004589 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004590 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004591 break;
4592 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004593 case KVM_GET_SUPPORTED_HV_CPUID: {
4594 struct kvm_cpuid2 __user *cpuid_arg = argp;
4595 struct kvm_cpuid2 cpuid;
4596
4597 r = -EFAULT;
4598 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4599 goto out;
4600
4601 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4602 cpuid_arg->entries);
4603 if (r)
4604 goto out;
4605
4606 r = -EFAULT;
4607 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4608 goto out;
4609 r = 0;
4610 break;
4611 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004612 default:
4613 r = -EINVAL;
4614 }
4615out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004616 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004617out_nofree:
4618 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004619 return r;
4620}
4621
Souptick Joarder1499fa82018-04-19 00:49:58 +05304622vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004623{
4624 return VM_FAULT_SIGBUS;
4625}
4626
Carsten Otte1fe779f2007-10-29 16:08:35 +01004627static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4628{
4629 int ret;
4630
4631 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004632 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004633 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4634 return ret;
4635}
4636
Sheng Yangb927a3c2009-07-21 10:42:48 +08004637static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4638 u64 ident_addr)
4639{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004640 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004641}
4642
Carsten Otte1fe779f2007-10-29 16:08:35 +01004643static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004644 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004645{
4646 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4647 return -EINVAL;
4648
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004649 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004650
4651 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004652 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004653
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004654 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004655 return 0;
4656}
4657
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004658static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004659{
Dave Hansen39de71e2010-08-19 18:11:14 -07004660 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004661}
4662
Carsten Otte1fe779f2007-10-29 16:08:35 +01004663static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4664{
David Hildenbrand90bca052017-04-07 10:50:23 +02004665 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004666 int r;
4667
4668 r = 0;
4669 switch (chip->chip_id) {
4670 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004671 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004672 sizeof(struct kvm_pic_state));
4673 break;
4674 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004675 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004676 sizeof(struct kvm_pic_state));
4677 break;
4678 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004679 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004680 break;
4681 default:
4682 r = -EINVAL;
4683 break;
4684 }
4685 return r;
4686}
4687
4688static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4689{
David Hildenbrand90bca052017-04-07 10:50:23 +02004690 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004691 int r;
4692
4693 r = 0;
4694 switch (chip->chip_id) {
4695 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004696 spin_lock(&pic->lock);
4697 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004698 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004699 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004700 break;
4701 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004702 spin_lock(&pic->lock);
4703 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004704 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004705 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004706 break;
4707 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004708 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004709 break;
4710 default:
4711 r = -EINVAL;
4712 break;
4713 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004714 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004715 return r;
4716}
4717
Sheng Yange0f63cb2008-03-04 00:50:59 +08004718static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4719{
Radim Krčmář34f39412016-03-02 22:56:50 +01004720 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4721
4722 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4723
4724 mutex_lock(&kps->lock);
4725 memcpy(ps, &kps->channels, sizeof(*ps));
4726 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304727 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004728}
4729
4730static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4731{
Andrew Honig01856042015-11-18 14:50:23 -08004732 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004733 struct kvm_pit *pit = kvm->arch.vpit;
4734
4735 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004736 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004737 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004738 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4739 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304740 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004741}
4742
4743static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4744{
Beth Kone9f42752009-07-07 11:50:38 -04004745 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4746 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4747 sizeof(ps->channels));
4748 ps->flags = kvm->arch.vpit->pit_state.flags;
4749 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004750 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304751 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004752}
4753
4754static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4755{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304756 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004757 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004758 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004759 struct kvm_pit *pit = kvm->arch.vpit;
4760
4761 mutex_lock(&pit->pit_state.lock);
4762 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004763 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4764 if (!prev_legacy && cur_legacy)
4765 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004766 memcpy(&pit->pit_state.channels, &ps->channels,
4767 sizeof(pit->pit_state.channels));
4768 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004769 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004770 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004771 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004772 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304773 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004774}
4775
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004776static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4777 struct kvm_reinject_control *control)
4778{
Radim Krčmář71474e22016-03-02 22:56:45 +01004779 struct kvm_pit *pit = kvm->arch.vpit;
4780
Radim Krčmář71474e22016-03-02 22:56:45 +01004781 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4782 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4783 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4784 */
4785 mutex_lock(&pit->pit_state.lock);
4786 kvm_pit_set_reinject(pit, control->pit_reinject);
4787 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004788
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004789 return 0;
4790}
4791
Sean Christopherson0dff0842020-02-18 13:07:29 -08004792void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004793{
Kai Huang88178fd2015-01-28 10:54:27 +08004794 /*
4795 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4796 */
4797 if (kvm_x86_ops->flush_log_dirty)
4798 kvm_x86_ops->flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004799}
4800
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004801int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4802 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004803{
4804 if (!irqchip_in_kernel(kvm))
4805 return -ENXIO;
4806
4807 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004808 irq_event->irq, irq_event->level,
4809 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004810 return 0;
4811}
4812
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004813int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4814 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004815{
4816 int r;
4817
4818 if (cap->flags)
4819 return -EINVAL;
4820
4821 switch (cap->cap) {
4822 case KVM_CAP_DISABLE_QUIRKS:
4823 kvm->arch.disabled_quirks = cap->args[0];
4824 r = 0;
4825 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004826 case KVM_CAP_SPLIT_IRQCHIP: {
4827 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004828 r = -EINVAL;
4829 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4830 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004831 r = -EEXIST;
4832 if (irqchip_in_kernel(kvm))
4833 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004834 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004835 goto split_irqchip_unlock;
4836 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004837 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004838 goto split_irqchip_unlock;
4839 /* Pairs with irqchip_in_kernel. */
4840 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004841 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004842 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004843 r = 0;
4844split_irqchip_unlock:
4845 mutex_unlock(&kvm->lock);
4846 break;
4847 }
Radim Krčmář371313132016-07-12 22:09:27 +02004848 case KVM_CAP_X2APIC_API:
4849 r = -EINVAL;
4850 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4851 break;
4852
4853 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4854 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004855 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4856 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004857
4858 r = 0;
4859 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004860 case KVM_CAP_X86_DISABLE_EXITS:
4861 r = -EINVAL;
4862 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4863 break;
4864
4865 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4866 kvm_can_mwait_in_guest())
4867 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004868 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004869 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004870 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4871 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004872 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4873 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004874 r = 0;
4875 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004876 case KVM_CAP_MSR_PLATFORM_INFO:
4877 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4878 r = 0;
4879 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004880 case KVM_CAP_EXCEPTION_PAYLOAD:
4881 kvm->arch.exception_payload_enabled = cap->args[0];
4882 r = 0;
4883 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004884 default:
4885 r = -EINVAL;
4886 break;
4887 }
4888 return r;
4889}
4890
Carsten Otte1fe779f2007-10-29 16:08:35 +01004891long kvm_arch_vm_ioctl(struct file *filp,
4892 unsigned int ioctl, unsigned long arg)
4893{
4894 struct kvm *kvm = filp->private_data;
4895 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004896 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004897 /*
4898 * This union makes it completely explicit to gcc-3.x
4899 * that these two variables' stack usage should be
4900 * combined, not added together.
4901 */
4902 union {
4903 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004904 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004905 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004906 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004907
4908 switch (ioctl) {
4909 case KVM_SET_TSS_ADDR:
4910 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004911 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004912 case KVM_SET_IDENTITY_MAP_ADDR: {
4913 u64 ident_addr;
4914
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004915 mutex_lock(&kvm->lock);
4916 r = -EINVAL;
4917 if (kvm->created_vcpus)
4918 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004919 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004920 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004921 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004922 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004923set_identity_unlock:
4924 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004925 break;
4926 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004927 case KVM_SET_NR_MMU_PAGES:
4928 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004929 break;
4930 case KVM_GET_NR_MMU_PAGES:
4931 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4932 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004933 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004934 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004935
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004936 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004937 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004938 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004939
Avi Kivity3e515702012-03-05 14:23:29 +02004940 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004941 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004942 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004943
4944 r = kvm_pic_init(kvm);
4945 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004946 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004947
4948 r = kvm_ioapic_init(kvm);
4949 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004950 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004951 goto create_irqchip_unlock;
4952 }
4953
Avi Kivity399ec802008-11-19 13:58:46 +02004954 r = kvm_setup_default_irq_routing(kvm);
4955 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004956 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004957 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004958 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004959 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004960 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004961 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004962 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004963 create_irqchip_unlock:
4964 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004965 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004966 }
Sheng Yang78376992008-01-28 05:10:22 +08004967 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004968 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4969 goto create_pit;
4970 case KVM_CREATE_PIT2:
4971 r = -EFAULT;
4972 if (copy_from_user(&u.pit_config, argp,
4973 sizeof(struct kvm_pit_config)))
4974 goto out;
4975 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004976 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004977 r = -EEXIST;
4978 if (kvm->arch.vpit)
4979 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004980 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004981 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004982 if (kvm->arch.vpit)
4983 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004984 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004985 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004986 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004987 case KVM_GET_IRQCHIP: {
4988 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004989 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004990
Sasha Levinff5c2c02011-12-04 19:36:29 +02004991 chip = memdup_user(argp, sizeof(*chip));
4992 if (IS_ERR(chip)) {
4993 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004994 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004995 }
4996
Carsten Otte1fe779f2007-10-29 16:08:35 +01004997 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004998 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004999 goto get_irqchip_out;
5000 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5001 if (r)
5002 goto get_irqchip_out;
5003 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005004 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005005 goto get_irqchip_out;
5006 r = 0;
5007 get_irqchip_out:
5008 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005009 break;
5010 }
5011 case KVM_SET_IRQCHIP: {
5012 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005013 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005014
Sasha Levinff5c2c02011-12-04 19:36:29 +02005015 chip = memdup_user(argp, sizeof(*chip));
5016 if (IS_ERR(chip)) {
5017 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005018 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005019 }
5020
Carsten Otte1fe779f2007-10-29 16:08:35 +01005021 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005022 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005023 goto set_irqchip_out;
5024 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005025 set_irqchip_out:
5026 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005027 break;
5028 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005029 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005030 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005031 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005032 goto out;
5033 r = -ENXIO;
5034 if (!kvm->arch.vpit)
5035 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005036 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005037 if (r)
5038 goto out;
5039 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005040 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005041 goto out;
5042 r = 0;
5043 break;
5044 }
5045 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005046 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005047 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005048 goto out;
5049 r = -ENXIO;
5050 if (!kvm->arch.vpit)
5051 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005052 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005053 break;
5054 }
Beth Kone9f42752009-07-07 11:50:38 -04005055 case KVM_GET_PIT2: {
5056 r = -ENXIO;
5057 if (!kvm->arch.vpit)
5058 goto out;
5059 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5060 if (r)
5061 goto out;
5062 r = -EFAULT;
5063 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5064 goto out;
5065 r = 0;
5066 break;
5067 }
5068 case KVM_SET_PIT2: {
5069 r = -EFAULT;
5070 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5071 goto out;
5072 r = -ENXIO;
5073 if (!kvm->arch.vpit)
5074 goto out;
5075 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005076 break;
5077 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005078 case KVM_REINJECT_CONTROL: {
5079 struct kvm_reinject_control control;
5080 r = -EFAULT;
5081 if (copy_from_user(&control, argp, sizeof(control)))
5082 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005083 r = -ENXIO;
5084 if (!kvm->arch.vpit)
5085 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005086 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005087 break;
5088 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005089 case KVM_SET_BOOT_CPU_ID:
5090 r = 0;
5091 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005092 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005093 r = -EBUSY;
5094 else
5095 kvm->arch.bsp_vcpu_id = arg;
5096 mutex_unlock(&kvm->lock);
5097 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005098 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005099 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005100 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005101 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005102 goto out;
5103 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005104 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005105 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005106 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005107 r = 0;
5108 break;
5109 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005110 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005111 struct kvm_clock_data user_ns;
5112 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005113
5114 r = -EFAULT;
5115 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5116 goto out;
5117
5118 r = -EINVAL;
5119 if (user_ns.flags)
5120 goto out;
5121
5122 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005123 /*
5124 * TODO: userspace has to take care of races with VCPU_RUN, so
5125 * kvm_gen_update_masterclock() can be cut down to locked
5126 * pvclock_update_vm_gtod_copy().
5127 */
5128 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005129 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005130 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005131 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005132 break;
5133 }
5134 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005135 struct kvm_clock_data user_ns;
5136 u64 now_ns;
5137
Marcelo Tosattie891a322017-04-17 12:51:37 -03005138 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005139 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005140 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005141 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005142
5143 r = -EFAULT;
5144 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5145 goto out;
5146 r = 0;
5147 break;
5148 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005149 case KVM_MEMORY_ENCRYPT_OP: {
5150 r = -ENOTTY;
5151 if (kvm_x86_ops->mem_enc_op)
5152 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5153 break;
5154 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005155 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5156 struct kvm_enc_region region;
5157
5158 r = -EFAULT;
5159 if (copy_from_user(&region, argp, sizeof(region)))
5160 goto out;
5161
5162 r = -ENOTTY;
5163 if (kvm_x86_ops->mem_enc_reg_region)
5164 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5165 break;
5166 }
5167 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5168 struct kvm_enc_region region;
5169
5170 r = -EFAULT;
5171 if (copy_from_user(&region, argp, sizeof(region)))
5172 goto out;
5173
5174 r = -ENOTTY;
5175 if (kvm_x86_ops->mem_enc_unreg_region)
5176 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5177 break;
5178 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005179 case KVM_HYPERV_EVENTFD: {
5180 struct kvm_hyperv_eventfd hvevfd;
5181
5182 r = -EFAULT;
5183 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5184 goto out;
5185 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5186 break;
5187 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005188 case KVM_SET_PMU_EVENT_FILTER:
5189 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5190 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005191 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005192 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005193 }
5194out:
5195 return r;
5196}
5197
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005198static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005199{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005200 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005201 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005202 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005203
Jim Mattsone2ada662019-08-21 11:20:04 -07005204 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005205 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005206
5207 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005208
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005209 num_msrs_to_save = 0;
5210 num_emulated_msrs = 0;
5211 num_msr_based_features = 0;
5212
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005213 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5214 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005215 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005216
5217 /*
5218 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005219 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005220 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005221 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005222 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005223 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005224 continue;
5225 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005226 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005227 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005228 continue;
5229 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005230 case MSR_IA32_RTIT_CTL:
5231 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005232 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005233 continue;
5234 break;
5235 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005236 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005237 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5238 continue;
5239 break;
5240 case MSR_IA32_RTIT_OUTPUT_BASE:
5241 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005242 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005243 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5244 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5245 continue;
5246 break;
5247 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
Sean Christopherson7b874c22020-03-02 15:56:59 -08005248 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005249 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005250 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5251 continue;
5252 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005253 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005254 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005255 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5256 continue;
5257 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005258 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005259 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005260 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5261 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005262 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005263 default:
5264 break;
5265 }
5266
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005267 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005268 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005269
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005270 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5271 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005272 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005273
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005274 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005275 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005276
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005277 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005278 struct kvm_msr_entry msr;
5279
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005280 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005281 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005282 continue;
5283
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005284 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005285 }
Carsten Otte043405e2007-10-10 17:16:19 +02005286}
5287
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005288static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5289 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005290{
Avi Kivity70252a12010-01-19 12:51:22 +02005291 int handled = 0;
5292 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005293
Avi Kivity70252a12010-01-19 12:51:22 +02005294 do {
5295 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005296 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005297 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5298 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005299 break;
5300 handled += n;
5301 addr += n;
5302 len -= n;
5303 v += n;
5304 } while (len);
5305
5306 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005307}
5308
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005309static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005310{
Avi Kivity70252a12010-01-19 12:51:22 +02005311 int handled = 0;
5312 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005313
Avi Kivity70252a12010-01-19 12:51:22 +02005314 do {
5315 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005316 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005317 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5318 addr, n, v))
5319 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005320 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005321 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005322 handled += n;
5323 addr += n;
5324 len -= n;
5325 v += n;
5326 } while (len);
5327
5328 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005329}
5330
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005331static void kvm_set_segment(struct kvm_vcpu *vcpu,
5332 struct kvm_segment *var, int seg)
5333{
5334 kvm_x86_ops->set_segment(vcpu, var, seg);
5335}
5336
5337void kvm_get_segment(struct kvm_vcpu *vcpu,
5338 struct kvm_segment *var, int seg)
5339{
5340 kvm_x86_ops->get_segment(vcpu, var, seg);
5341}
5342
Paolo Bonzini54987b72014-09-02 13:23:06 +02005343gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5344 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005345{
5346 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005347
5348 BUG_ON(!mmu_is_nested(vcpu));
5349
5350 /* NPT walks are always user-walks */
5351 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005352 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005353
5354 return t_gpa;
5355}
5356
Avi Kivityab9ae312010-11-22 17:53:26 +02005357gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5358 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005359{
5360 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005361 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005362}
5363
Avi Kivityab9ae312010-11-22 17:53:26 +02005364 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5365 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005366{
5367 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5368 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005369 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005370}
5371
Avi Kivityab9ae312010-11-22 17:53:26 +02005372gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5373 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005374{
5375 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5376 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005377 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005378}
5379
5380/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005381gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5382 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005383{
Avi Kivityab9ae312010-11-22 17:53:26 +02005384 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005385}
5386
5387static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5388 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005389 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005390{
5391 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005392 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005393
5394 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005395 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005396 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005397 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005398 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005399 int ret;
5400
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005401 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005402 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005403 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5404 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005405 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005406 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005407 goto out;
5408 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005409
Izik Eidus77c20022008-12-29 01:42:19 +02005410 bytes -= toread;
5411 data += toread;
5412 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005413 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005414out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005415 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005416}
Izik Eidus77c20022008-12-29 01:42:19 +02005417
Gleb Natapov1871c602010-02-10 14:21:32 +02005418/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005419static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5420 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005421 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005422{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005423 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005424 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005425 unsigned offset;
5426 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005427
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005428 /* Inline kvm_read_guest_virt_helper for speed. */
5429 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5430 exception);
5431 if (unlikely(gpa == UNMAPPED_GVA))
5432 return X86EMUL_PROPAGATE_FAULT;
5433
5434 offset = addr & (PAGE_SIZE-1);
5435 if (WARN_ON(offset + bytes > PAGE_SIZE))
5436 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005437 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5438 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005439 if (unlikely(ret < 0))
5440 return X86EMUL_IO_NEEDED;
5441
5442 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005443}
5444
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005445int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005446 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005447 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005448{
5449 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005450
Paolo Bonzini353c0952019-01-29 18:41:16 +01005451 /*
5452 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5453 * is returned, but our callers are not ready for that and they blindly
5454 * call kvm_inject_page_fault. Ensure that they at least do not leak
5455 * uninitialized kernel stack memory into cr2 and error code.
5456 */
5457 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005458 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005459 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005460}
Nadav Har'El064aea72011-05-25 23:04:56 +03005461EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005462
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005463static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5464 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005465 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005466{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005467 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005468 u32 access = 0;
5469
5470 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5471 access |= PFERR_USER_MASK;
5472
5473 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005474}
5475
Radim Krčmář7a036a62015-10-30 16:36:24 +01005476static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5477 unsigned long addr, void *val, unsigned int bytes)
5478{
5479 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5480 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5481
5482 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5483}
5484
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005485static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5486 struct kvm_vcpu *vcpu, u32 access,
5487 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005488{
5489 void *data = val;
5490 int r = X86EMUL_CONTINUE;
5491
5492 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005493 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005494 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005495 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005496 unsigned offset = addr & (PAGE_SIZE-1);
5497 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5498 int ret;
5499
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005500 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005501 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005502 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005503 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005504 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005505 goto out;
5506 }
5507
5508 bytes -= towrite;
5509 data += towrite;
5510 addr += towrite;
5511 }
5512out:
5513 return r;
5514}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005515
5516static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005517 unsigned int bytes, struct x86_exception *exception,
5518 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005519{
5520 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005521 u32 access = PFERR_WRITE_MASK;
5522
5523 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5524 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005525
5526 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005527 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005528}
5529
5530int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5531 unsigned int bytes, struct x86_exception *exception)
5532{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005533 /* kvm_write_guest_virt_system can pull in tons of pages. */
5534 vcpu->arch.l1tf_flush_l1d = true;
5535
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005536 /*
5537 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5538 * is returned, but our callers are not ready for that and they blindly
5539 * call kvm_inject_page_fault. Ensure that they at least do not leak
5540 * uninitialized kernel stack memory into cr2 and error code.
5541 */
5542 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005543 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5544 PFERR_WRITE_MASK, exception);
5545}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005546EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005547
Wanpeng Li082d06e2018-04-03 16:28:48 -07005548int handle_ud(struct kvm_vcpu *vcpu)
5549{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005550 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005551 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005552 char sig[5]; /* ud2; .ascii "kvm" */
5553 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005554
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005555 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005556 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5557 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005558 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005559 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005560 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005561 }
5562
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005563 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005564}
5565EXPORT_SYMBOL_GPL(handle_ud);
5566
Tom Lendacky0f89b202016-12-14 14:59:23 -05005567static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5568 gpa_t gpa, bool write)
5569{
5570 /* For APIC access vmexit */
5571 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5572 return 1;
5573
5574 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5575 trace_vcpu_match_mmio(gva, gpa, write, true);
5576 return 1;
5577 }
5578
5579 return 0;
5580}
5581
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005582static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5583 gpa_t *gpa, struct x86_exception *exception,
5584 bool write)
5585{
Avi Kivity97d64b72012-09-12 14:52:00 +03005586 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5587 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005588
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005589 /*
5590 * currently PKRU is only applied to ept enabled guest so
5591 * there is no pkey in EPT page table for L1 guest or EPT
5592 * shadow page table for L2 guest.
5593 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005594 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005595 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005596 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005597 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5598 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005599 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005600 return 1;
5601 }
5602
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005603 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5604
5605 if (*gpa == UNMAPPED_GVA)
5606 return -1;
5607
Tom Lendacky0f89b202016-12-14 14:59:23 -05005608 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005609}
5610
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005611int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005612 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005613{
5614 int ret;
5615
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005616 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005617 if (ret < 0)
5618 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005619 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005620 return 1;
5621}
5622
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005623struct read_write_emulator_ops {
5624 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5625 int bytes);
5626 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5627 void *val, int bytes);
5628 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5629 int bytes, void *val);
5630 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5631 void *val, int bytes);
5632 bool write;
5633};
5634
5635static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5636{
5637 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005638 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005639 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005640 vcpu->mmio_read_completed = 0;
5641 return 1;
5642 }
5643
5644 return 0;
5645}
5646
5647static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5648 void *val, int bytes)
5649{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005650 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005651}
5652
5653static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5654 void *val, int bytes)
5655{
5656 return emulator_write_phys(vcpu, gpa, val, bytes);
5657}
5658
5659static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5660{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005661 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005662 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5663}
5664
5665static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5666 void *val, int bytes)
5667{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005668 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005669 return X86EMUL_IO_NEEDED;
5670}
5671
5672static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5673 void *val, int bytes)
5674{
Avi Kivityf78146b2012-04-18 19:22:47 +03005675 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5676
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005677 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005678 return X86EMUL_CONTINUE;
5679}
5680
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005681static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005682 .read_write_prepare = read_prepare,
5683 .read_write_emulate = read_emulate,
5684 .read_write_mmio = vcpu_mmio_read,
5685 .read_write_exit_mmio = read_exit_mmio,
5686};
5687
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005688static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005689 .read_write_emulate = write_emulate,
5690 .read_write_mmio = write_mmio,
5691 .read_write_exit_mmio = write_exit_mmio,
5692 .write = true,
5693};
5694
Xiao Guangrong22388a32011-07-13 14:32:31 +08005695static int emulator_read_write_onepage(unsigned long addr, void *val,
5696 unsigned int bytes,
5697 struct x86_exception *exception,
5698 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005699 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005700{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005701 gpa_t gpa;
5702 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005703 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005704 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005705 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005706
5707 /*
5708 * If the exit was due to a NPF we may already have a GPA.
5709 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5710 * Note, this cannot be used on string operations since string
5711 * operation using rep will only have the initial GPA from the NPF
5712 * occurred.
5713 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005714 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5715 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5716 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005717 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5718 } else {
5719 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5720 if (ret < 0)
5721 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005722 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005723
Brijesh Singh618232e2017-08-17 18:36:57 +02005724 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005725 return X86EMUL_CONTINUE;
5726
Carsten Ottebbd9b642007-10-30 18:44:21 +01005727 /*
5728 * Is this MMIO handled locally?
5729 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005730 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005731 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005732 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005733
Avi Kivity70252a12010-01-19 12:51:22 +02005734 gpa += handled;
5735 bytes -= handled;
5736 val += handled;
5737
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005738 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5739 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5740 frag->gpa = gpa;
5741 frag->data = val;
5742 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005743 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005744}
5745
Xiubo Li52eb5a62015-03-13 17:39:45 +08005746static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5747 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005748 void *val, unsigned int bytes,
5749 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005750 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005751{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005752 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005753 gpa_t gpa;
5754 int rc;
5755
5756 if (ops->read_write_prepare &&
5757 ops->read_write_prepare(vcpu, val, bytes))
5758 return X86EMUL_CONTINUE;
5759
5760 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005761
Carsten Ottebbd9b642007-10-30 18:44:21 +01005762 /* Crossing a page boundary? */
5763 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005764 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005765
5766 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005767 rc = emulator_read_write_onepage(addr, val, now, exception,
5768 vcpu, ops);
5769
Carsten Ottebbd9b642007-10-30 18:44:21 +01005770 if (rc != X86EMUL_CONTINUE)
5771 return rc;
5772 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005773 if (ctxt->mode != X86EMUL_MODE_PROT64)
5774 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005775 val += now;
5776 bytes -= now;
5777 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005778
Avi Kivityf78146b2012-04-18 19:22:47 +03005779 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5780 vcpu, ops);
5781 if (rc != X86EMUL_CONTINUE)
5782 return rc;
5783
5784 if (!vcpu->mmio_nr_fragments)
5785 return rc;
5786
5787 gpa = vcpu->mmio_fragments[0].gpa;
5788
5789 vcpu->mmio_needed = 1;
5790 vcpu->mmio_cur_fragment = 0;
5791
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005792 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005793 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5794 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5795 vcpu->run->mmio.phys_addr = gpa;
5796
5797 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005798}
5799
5800static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5801 unsigned long addr,
5802 void *val,
5803 unsigned int bytes,
5804 struct x86_exception *exception)
5805{
5806 return emulator_read_write(ctxt, addr, val, bytes,
5807 exception, &read_emultor);
5808}
5809
Xiubo Li52eb5a62015-03-13 17:39:45 +08005810static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005811 unsigned long addr,
5812 const void *val,
5813 unsigned int bytes,
5814 struct x86_exception *exception)
5815{
5816 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5817 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005818}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005819
Avi Kivitydaea3e72010-03-15 13:59:54 +02005820#define CMPXCHG_TYPE(t, ptr, old, new) \
5821 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5822
5823#ifdef CONFIG_X86_64
5824# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5825#else
5826# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005827 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005828#endif
5829
Avi Kivity0f65dd72011-04-20 13:37:53 +03005830static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5831 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005832 const void *old,
5833 const void *new,
5834 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005835 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005836{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005837 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005838 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005839 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005840 char *kaddr;
5841 bool exchanged;
5842
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005843 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005844 if (bytes > 8 || (bytes & (bytes - 1)))
5845 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005846
Avi Kivitydaea3e72010-03-15 13:59:54 +02005847 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005848
Avi Kivitydaea3e72010-03-15 13:59:54 +02005849 if (gpa == UNMAPPED_GVA ||
5850 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5851 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005852
Avi Kivitydaea3e72010-03-15 13:59:54 +02005853 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5854 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005855
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005856 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005857 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005858
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005859 kaddr = map.hva + offset_in_page(gpa);
5860
Avi Kivitydaea3e72010-03-15 13:59:54 +02005861 switch (bytes) {
5862 case 1:
5863 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5864 break;
5865 case 2:
5866 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5867 break;
5868 case 4:
5869 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5870 break;
5871 case 8:
5872 exchanged = CMPXCHG64(kaddr, old, new);
5873 break;
5874 default:
5875 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005876 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005877
5878 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005879
5880 if (!exchanged)
5881 return X86EMUL_CMPXCHG_FAILED;
5882
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005883 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005884
5885 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005886
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005887emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005888 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005889
Avi Kivity0f65dd72011-04-20 13:37:53 +03005890 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005891}
5892
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005893static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5894{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005895 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005896
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005897 for (i = 0; i < vcpu->arch.pio.count; i++) {
5898 if (vcpu->arch.pio.in)
5899 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5900 vcpu->arch.pio.size, pd);
5901 else
5902 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5903 vcpu->arch.pio.port, vcpu->arch.pio.size,
5904 pd);
5905 if (r)
5906 break;
5907 pd += vcpu->arch.pio.size;
5908 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005909 return r;
5910}
5911
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005912static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5913 unsigned short port, void *val,
5914 unsigned int count, bool in)
5915{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005916 vcpu->arch.pio.port = port;
5917 vcpu->arch.pio.in = in;
5918 vcpu->arch.pio.count = count;
5919 vcpu->arch.pio.size = size;
5920
5921 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5922 vcpu->arch.pio.count = 0;
5923 return 1;
5924 }
5925
5926 vcpu->run->exit_reason = KVM_EXIT_IO;
5927 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5928 vcpu->run->io.size = size;
5929 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5930 vcpu->run->io.count = count;
5931 vcpu->run->io.port = port;
5932
5933 return 0;
5934}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005935
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005936static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5937 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005938{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005939 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005940
Gleb Natapov79729952010-03-18 15:20:24 +02005941 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005942 goto data_avail;
5943
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005944 memset(vcpu->arch.pio_data, 0, size * count);
5945
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005946 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5947 if (ret) {
5948data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005949 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005950 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005951 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005952 return 1;
5953 }
5954
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005955 return 0;
5956}
5957
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005958static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5959 int size, unsigned short port, void *val,
5960 unsigned int count)
5961{
5962 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
5963
5964}
5965
5966static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5967 unsigned short port, const void *val,
5968 unsigned int count)
5969{
5970 memcpy(vcpu->arch.pio_data, val, size * count);
5971 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
5972 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
5973}
5974
Avi Kivityca1d4a92011-04-20 13:37:53 +03005975static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5976 int size, unsigned short port,
5977 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005978{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005979 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005980}
5981
Carsten Ottebbd9b642007-10-30 18:44:21 +01005982static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5983{
5984 return kvm_x86_ops->get_segment_base(vcpu, seg);
5985}
5986
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005987static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005988{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005989 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005990}
5991
Jiang Biaoae6a2372016-11-07 08:54:51 +08005992static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005993{
5994 if (!need_emulate_wbinvd(vcpu))
5995 return X86EMUL_CONTINUE;
5996
5997 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005998 int cpu = get_cpu();
5999
6000 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006001 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6002 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006003 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006004 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006005 } else
6006 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006007 return X86EMUL_CONTINUE;
6008}
Joel Schopp5cb56052015-03-02 13:43:31 -06006009
6010int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6011{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006012 kvm_emulate_wbinvd_noskip(vcpu);
6013 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006014}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006015EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6016
Joel Schopp5cb56052015-03-02 13:43:31 -06006017
6018
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006019static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6020{
Joel Schopp5cb56052015-03-02 13:43:31 -06006021 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006022}
6023
Xiubo Li52eb5a62015-03-13 17:39:45 +08006024static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6025 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006026{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006027 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006028}
6029
Xiubo Li52eb5a62015-03-13 17:39:45 +08006030static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6031 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006032{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006033
Avi Kivity717746e2011-04-20 13:37:53 +03006034 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006035}
6036
Gleb Natapov52a46612010-03-18 15:20:03 +02006037static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6038{
6039 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6040}
6041
Avi Kivity717746e2011-04-20 13:37:53 +03006042static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006043{
Avi Kivity717746e2011-04-20 13:37:53 +03006044 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006045 unsigned long value;
6046
6047 switch (cr) {
6048 case 0:
6049 value = kvm_read_cr0(vcpu);
6050 break;
6051 case 2:
6052 value = vcpu->arch.cr2;
6053 break;
6054 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006055 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006056 break;
6057 case 4:
6058 value = kvm_read_cr4(vcpu);
6059 break;
6060 case 8:
6061 value = kvm_get_cr8(vcpu);
6062 break;
6063 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006064 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006065 return 0;
6066 }
6067
6068 return value;
6069}
6070
Avi Kivity717746e2011-04-20 13:37:53 +03006071static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006072{
Avi Kivity717746e2011-04-20 13:37:53 +03006073 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006074 int res = 0;
6075
Gleb Natapov52a46612010-03-18 15:20:03 +02006076 switch (cr) {
6077 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006078 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006079 break;
6080 case 2:
6081 vcpu->arch.cr2 = val;
6082 break;
6083 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006084 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006085 break;
6086 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006087 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006088 break;
6089 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006090 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006091 break;
6092 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006093 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006094 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006095 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006096
6097 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006098}
6099
Avi Kivity717746e2011-04-20 13:37:53 +03006100static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006101{
Avi Kivity717746e2011-04-20 13:37:53 +03006102 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006103}
6104
Avi Kivity4bff1e862011-04-20 13:37:53 +03006105static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006106{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006107 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006108}
6109
Avi Kivity4bff1e862011-04-20 13:37:53 +03006110static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006111{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006112 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006113}
6114
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006115static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6116{
6117 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6118}
6119
6120static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6121{
6122 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6123}
6124
Avi Kivity4bff1e862011-04-20 13:37:53 +03006125static unsigned long emulator_get_cached_segment_base(
6126 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006127{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006128 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006129}
6130
Avi Kivity1aa36612011-04-27 13:20:30 +03006131static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6132 struct desc_struct *desc, u32 *base3,
6133 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006134{
6135 struct kvm_segment var;
6136
Avi Kivity4bff1e862011-04-20 13:37:53 +03006137 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006138 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006139
Gleb Natapov378a8b02013-01-21 15:36:48 +02006140 if (var.unusable) {
6141 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006142 if (base3)
6143 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006144 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006145 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006146
6147 if (var.g)
6148 var.limit >>= 12;
6149 set_desc_limit(desc, var.limit);
6150 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006151#ifdef CONFIG_X86_64
6152 if (base3)
6153 *base3 = var.base >> 32;
6154#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006155 desc->type = var.type;
6156 desc->s = var.s;
6157 desc->dpl = var.dpl;
6158 desc->p = var.present;
6159 desc->avl = var.avl;
6160 desc->l = var.l;
6161 desc->d = var.db;
6162 desc->g = var.g;
6163
6164 return true;
6165}
6166
Avi Kivity1aa36612011-04-27 13:20:30 +03006167static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6168 struct desc_struct *desc, u32 base3,
6169 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006170{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006171 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006172 struct kvm_segment var;
6173
Avi Kivity1aa36612011-04-27 13:20:30 +03006174 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006175 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006176#ifdef CONFIG_X86_64
6177 var.base |= ((u64)base3) << 32;
6178#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006179 var.limit = get_desc_limit(desc);
6180 if (desc->g)
6181 var.limit = (var.limit << 12) | 0xfff;
6182 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006183 var.dpl = desc->dpl;
6184 var.db = desc->d;
6185 var.s = desc->s;
6186 var.l = desc->l;
6187 var.g = desc->g;
6188 var.avl = desc->avl;
6189 var.present = desc->p;
6190 var.unusable = !var.present;
6191 var.padding = 0;
6192
6193 kvm_set_segment(vcpu, &var, seg);
6194 return;
6195}
6196
Avi Kivity717746e2011-04-20 13:37:53 +03006197static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6198 u32 msr_index, u64 *pdata)
6199{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006200 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006201}
6202
6203static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6204 u32 msr_index, u64 data)
6205{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006206 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006207}
6208
Paolo Bonzini64d60672015-05-07 11:36:11 +02006209static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6210{
6211 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6212
6213 return vcpu->arch.smbase;
6214}
6215
6216static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6217{
6218 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6219
6220 vcpu->arch.smbase = smbase;
6221}
6222
Nadav Amit67f4d422014-06-02 18:34:09 +03006223static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6224 u32 pmc)
6225{
Like Xu98ff80f2019-10-27 18:52:40 +08006226 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006227}
6228
Avi Kivity222d21a2011-11-10 14:57:30 +02006229static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6230 u32 pmc, u64 *pdata)
6231{
Wei Huangc6702c92015-06-19 13:44:45 +02006232 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006233}
6234
Avi Kivity6c3287f2011-04-20 15:43:05 +03006235static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6236{
6237 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6238}
6239
Avi Kivity29535382011-04-20 13:37:53 +03006240static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006241 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006242 enum x86_intercept_stage stage)
6243{
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006244 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage,
6245 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006246}
6247
Yu Zhange911eb32017-08-24 20:27:52 +08006248static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006249 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6250 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006251{
Sean Christophersonf91af512020-03-04 17:34:37 -08006252 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006253}
6254
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006255static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6256{
6257 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6258}
6259
6260static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6261{
6262 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6263}
6264
6265static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6266{
6267 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6268}
6269
Avi Kivitydd856ef2012-08-27 23:46:17 +03006270static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6271{
6272 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6273}
6274
6275static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6276{
6277 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6278}
6279
Nadav Amit801806d2015-01-26 09:32:23 +02006280static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6281{
6282 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6283}
6284
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006285static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6286{
6287 return emul_to_vcpu(ctxt)->arch.hflags;
6288}
6289
6290static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6291{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006292 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006293}
6294
Sean Christophersoned193212019-04-02 08:03:09 -07006295static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6296 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006297{
Sean Christophersoned193212019-04-02 08:03:09 -07006298 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006299}
6300
Sean Christophersonc5833c72019-04-02 08:03:10 -07006301static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6302{
6303 kvm_smm_changed(emul_to_vcpu(ctxt));
6304}
6305
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006306static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6307{
6308 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6309}
6310
Mathias Krause0225fb52012-08-30 01:30:16 +02006311static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006312 .read_gpr = emulator_read_gpr,
6313 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006314 .read_std = emulator_read_std,
6315 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006316 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006317 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006318 .read_emulated = emulator_read_emulated,
6319 .write_emulated = emulator_write_emulated,
6320 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006321 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006322 .pio_in_emulated = emulator_pio_in_emulated,
6323 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006324 .get_segment = emulator_get_segment,
6325 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006326 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006327 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006328 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006329 .set_gdt = emulator_set_gdt,
6330 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006331 .get_cr = emulator_get_cr,
6332 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006333 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006334 .get_dr = emulator_get_dr,
6335 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006336 .get_smbase = emulator_get_smbase,
6337 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006338 .set_msr = emulator_set_msr,
6339 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006340 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006341 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006342 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006343 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006344 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006345 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006346 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006347 .guest_has_long_mode = emulator_guest_has_long_mode,
6348 .guest_has_movbe = emulator_guest_has_movbe,
6349 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006350 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006351 .get_hflags = emulator_get_hflags,
6352 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006353 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006354 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006355 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006356};
6357
Gleb Natapov95cb2292010-04-28 19:15:43 +03006358static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6359{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006360 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006361 /*
6362 * an sti; sti; sequence only disable interrupts for the first
6363 * instruction. So, if the last instruction, be it emulated or
6364 * not, left the system with the INT_STI flag enabled, it
6365 * means that the last instruction is an sti. We should not
6366 * leave the flag on in this case. The same goes for mov ss
6367 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006368 if (int_shadow & mask)
6369 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006370 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006371 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006372 if (!mask)
6373 kvm_make_request(KVM_REQ_EVENT, vcpu);
6374 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006375}
6376
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006377static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006378{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006379 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006380 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006381 return kvm_propagate_fault(vcpu, &ctxt->exception);
6382
6383 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006384 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6385 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006386 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006387 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006388 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006389}
6390
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006391static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6392{
6393 struct x86_emulate_ctxt *ctxt;
6394
6395 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6396 if (!ctxt) {
6397 pr_err("kvm: failed to allocate vcpu's emulator\n");
6398 return NULL;
6399 }
6400
6401 ctxt->vcpu = vcpu;
6402 ctxt->ops = &emulate_ops;
6403 vcpu->arch.emulate_ctxt = ctxt;
6404
6405 return ctxt;
6406}
6407
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006408static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6409{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006410 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006411 int cs_db, cs_l;
6412
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006413 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6414
Sean Christopherson744e6992020-02-18 15:03:09 -08006415 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006416 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006417 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6418
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006419 ctxt->eip = kvm_rip_read(vcpu);
6420 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6421 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006422 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006423 cs_db ? X86EMUL_MODE_PROT32 :
6424 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006425 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006426 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6427 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006428
Avi Kivitydd856ef2012-08-27 23:46:17 +03006429 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006430 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006431}
6432
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006433void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006434{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006435 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006436 int ret;
6437
6438 init_emulate_ctxt(vcpu);
6439
Avi Kivity9dac77f2011-06-01 15:34:25 +03006440 ctxt->op_bytes = 2;
6441 ctxt->ad_bytes = 2;
6442 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006443 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006444
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006445 if (ret != X86EMUL_CONTINUE) {
6446 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6447 } else {
6448 ctxt->eip = ctxt->_eip;
6449 kvm_rip_write(vcpu, ctxt->eip);
6450 kvm_set_rflags(vcpu, ctxt->eflags);
6451 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006452}
6453EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6454
Liran Alone2366172018-03-12 13:12:49 +02006455static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006456{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006457 ++vcpu->stat.insn_emulation_fail;
6458 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006459
Sean Christopherson42cbf062019-08-27 14:40:31 -07006460 if (emulation_type & EMULTYPE_VMWARE_GP) {
6461 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006462 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006463 }
Liran Alone2366172018-03-12 13:12:49 +02006464
Sean Christopherson738fece2019-08-27 14:40:34 -07006465 if (emulation_type & EMULTYPE_SKIP) {
6466 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6467 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6468 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006469 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006470 }
6471
Sean Christopherson22da61c2019-08-27 14:40:28 -07006472 kvm_queue_exception(vcpu, UD_VECTOR);
6473
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006474 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006475 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6476 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6477 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006478 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006479 }
Liran Alone2366172018-03-12 13:12:49 +02006480
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006481 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006482}
6483
Sean Christopherson736c2912019-12-06 15:57:14 -08006484static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006485 bool write_fault_to_shadow_pgtable,
6486 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006487{
Sean Christopherson736c2912019-12-06 15:57:14 -08006488 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006489 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006490
Sean Christopherson92daa482020-02-18 15:03:08 -08006491 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006492 return false;
6493
Sean Christopherson92daa482020-02-18 15:03:08 -08006494 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6495 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006496 return false;
6497
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006498 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006499 /*
6500 * Write permission should be allowed since only
6501 * write access need to be emulated.
6502 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006503 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006504
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006505 /*
6506 * If the mapping is invalid in guest, let cpu retry
6507 * it to generate fault.
6508 */
6509 if (gpa == UNMAPPED_GVA)
6510 return true;
6511 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006512
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006513 /*
6514 * Do not retry the unhandleable instruction if it faults on the
6515 * readonly host memory, otherwise it will goto a infinite loop:
6516 * retry instruction -> write #PF -> emulation fail -> retry
6517 * instruction -> ...
6518 */
6519 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006520
6521 /*
6522 * If the instruction failed on the error pfn, it can not be fixed,
6523 * report the error to userspace.
6524 */
6525 if (is_error_noslot_pfn(pfn))
6526 return false;
6527
6528 kvm_release_pfn_clean(pfn);
6529
6530 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006531 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006532 unsigned int indirect_shadow_pages;
6533
6534 spin_lock(&vcpu->kvm->mmu_lock);
6535 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6536 spin_unlock(&vcpu->kvm->mmu_lock);
6537
6538 if (indirect_shadow_pages)
6539 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6540
Gleb Natapova6f177e2010-07-08 12:41:12 +03006541 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006542 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006543
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006544 /*
6545 * if emulation was due to access to shadowed page table
6546 * and it failed try to unshadow page and re-enter the
6547 * guest to let CPU execute the instruction.
6548 */
6549 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006550
6551 /*
6552 * If the access faults on its page table, it can not
6553 * be fixed by unprotecting shadow page and it should
6554 * be reported to userspace.
6555 */
6556 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006557}
6558
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006559static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006560 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006561{
6562 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006563 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006564
6565 last_retry_eip = vcpu->arch.last_retry_eip;
6566 last_retry_addr = vcpu->arch.last_retry_addr;
6567
6568 /*
6569 * If the emulation is caused by #PF and it is non-page_table
6570 * writing instruction, it means the VM-EXIT is caused by shadow
6571 * page protected, we can zap the shadow page and retry this
6572 * instruction directly.
6573 *
6574 * Note: if the guest uses a non-page-table modifying instruction
6575 * on the PDE that points to the instruction, then we will unmap
6576 * the instruction and go to an infinite loop. So, we cache the
6577 * last retried eip and the last fault address, if we meet the eip
6578 * and the address again, we can break out of the potential infinite
6579 * loop.
6580 */
6581 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6582
Sean Christopherson92daa482020-02-18 15:03:08 -08006583 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006584 return false;
6585
Sean Christopherson92daa482020-02-18 15:03:08 -08006586 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6587 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006588 return false;
6589
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006590 if (x86_page_table_writing_insn(ctxt))
6591 return false;
6592
Sean Christopherson736c2912019-12-06 15:57:14 -08006593 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006594 return false;
6595
6596 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006597 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006598
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006599 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006600 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006601
Xiao Guangrong22368022013-01-13 23:44:12 +08006602 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006603
6604 return true;
6605}
6606
Gleb Natapov716d51a2012-09-03 15:24:26 +03006607static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6608static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6609
Paolo Bonzini64d60672015-05-07 11:36:11 +02006610static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006611{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006612 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006613 /* This is a good place to trace that we are exiting SMM. */
6614 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6615
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006616 /* Process a latched INIT or SMI, if any. */
6617 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006618 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006619
6620 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006621}
6622
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006623static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6624 unsigned long *db)
6625{
6626 u32 dr6 = 0;
6627 int i;
6628 u32 enable, rwlen;
6629
6630 enable = dr7;
6631 rwlen = dr7 >> 16;
6632 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6633 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6634 dr6 |= (1 << i);
6635 return dr6;
6636}
6637
Sean Christopherson120c2c42019-08-27 14:40:29 -07006638static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006639{
6640 struct kvm_run *kvm_run = vcpu->run;
6641
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006642 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6643 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6644 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6645 kvm_run->debug.arch.exception = DB_VECTOR;
6646 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006647 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006648 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006649 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006650 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006651}
6652
Kyle Huey6affcbe2016-11-29 12:40:40 -08006653int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6654{
6655 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006656 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006657
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006658 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006659 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006660 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006661
6662 /*
6663 * rflags is the old, "raw" value of the flags. The new value has
6664 * not been saved yet.
6665 *
6666 * This is correct even for TF set by the guest, because "the
6667 * processor will not generate this exception after the instruction
6668 * that sets the TF flag".
6669 */
6670 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006671 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006672 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006673}
6674EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6675
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006676static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6677{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006678 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6679 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006680 struct kvm_run *kvm_run = vcpu->run;
6681 unsigned long eip = kvm_get_linear_rip(vcpu);
6682 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006683 vcpu->arch.guest_debug_dr7,
6684 vcpu->arch.eff_db);
6685
6686 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006687 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006688 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006689 kvm_run->debug.arch.exception = DB_VECTOR;
6690 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006691 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006692 return true;
6693 }
6694 }
6695
Nadav Amit4161a562014-07-17 01:19:31 +03006696 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6697 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006698 unsigned long eip = kvm_get_linear_rip(vcpu);
6699 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006700 vcpu->arch.dr7,
6701 vcpu->arch.db);
6702
6703 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006704 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006705 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006706 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006707 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006708 return true;
6709 }
6710 }
6711
6712 return false;
6713}
6714
Liran Alon04789b62018-03-12 13:12:50 +02006715static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6716{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006717 switch (ctxt->opcode_len) {
6718 case 1:
6719 switch (ctxt->b) {
6720 case 0xe4: /* IN */
6721 case 0xe5:
6722 case 0xec:
6723 case 0xed:
6724 case 0xe6: /* OUT */
6725 case 0xe7:
6726 case 0xee:
6727 case 0xef:
6728 case 0x6c: /* INS */
6729 case 0x6d:
6730 case 0x6e: /* OUTS */
6731 case 0x6f:
6732 return true;
6733 }
6734 break;
6735 case 2:
6736 switch (ctxt->b) {
6737 case 0x33: /* RDPMC */
6738 return true;
6739 }
6740 break;
Liran Alon04789b62018-03-12 13:12:50 +02006741 }
6742
6743 return false;
6744}
6745
Sean Christopherson736c2912019-12-06 15:57:14 -08006746int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6747 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006748{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006749 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006750 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006751 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006752 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006753
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006754 vcpu->arch.l1tf_flush_l1d = true;
6755
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006756 /*
6757 * Clear write_fault_to_shadow_pgtable here to ensure it is
6758 * never reused.
6759 */
6760 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006761 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006762
Sheng Yang571008d2008-01-02 14:49:22 +08006763 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006764 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006765
6766 /*
6767 * We will reenter on the same instruction since
6768 * we do not set complete_userspace_io. This does not
6769 * handle watchpoints yet, those would be handled in
6770 * the emulate_ops.
6771 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006772 if (!(emulation_type & EMULTYPE_SKIP) &&
6773 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006774 return r;
6775
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006776 ctxt->interruptibility = 0;
6777 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006778 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006779 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006780
Borislav Petkovb51e9742013-09-22 16:44:52 +02006781 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006782
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006783 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006784
Avi Kivitye46479f2010-04-11 13:05:16 +03006785 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006786 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006787 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006788 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006789 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6790 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006791 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006792 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006793 if (reexecute_instruction(vcpu, cr2_or_gpa,
6794 write_fault_to_spt,
6795 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006796 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006797 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006798 /*
6799 * #UD should result in just EMULATION_FAILED, and trap-like
6800 * exception should not be encountered during decode.
6801 */
6802 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6803 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006804 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006805 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006806 }
Liran Alone2366172018-03-12 13:12:49 +02006807 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006808 }
6809 }
6810
Sean Christopherson42cbf062019-08-27 14:40:31 -07006811 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6812 !is_vmware_backdoor_opcode(ctxt)) {
6813 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006814 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006815 }
Liran Alon04789b62018-03-12 13:12:50 +02006816
Sean Christopherson1957aa62019-08-27 14:40:39 -07006817 /*
6818 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6819 * for kvm_skip_emulated_instruction(). The caller is responsible for
6820 * updating interruptibility state and injecting single-step #DBs.
6821 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006822 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006823 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006824 if (ctxt->eflags & X86_EFLAGS_RF)
6825 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006826 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006827 }
6828
Sean Christopherson736c2912019-12-06 15:57:14 -08006829 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006830 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006831
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006832 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006833 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006834 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6835 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006836 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006837 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006838
Gleb Natapov5cd21912010-03-18 15:20:26 +02006839restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006840 if (emulation_type & EMULTYPE_PF) {
6841 /* Save the faulting GPA (cr2) in the address field */
6842 ctxt->exception.address = cr2_or_gpa;
6843
6844 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6845 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006846 ctxt->gpa_available = true;
6847 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006848 }
6849 } else {
6850 /* Sanitize the address out of an abundance of paranoia. */
6851 ctxt->exception.address = 0;
6852 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006853
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006854 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006855
Joerg Roedel775fde82011-04-04 12:39:24 +02006856 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006857 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006858
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006859 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006860 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006861 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006862 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006863
Liran Alone2366172018-03-12 13:12:49 +02006864 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006865 }
6866
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006867 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006868 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006869 if (inject_emulated_exception(vcpu))
6870 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006871 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006872 if (!vcpu->arch.pio.in) {
6873 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006874 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006875 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006876 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006877 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6878 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006879 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006880 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006881 ++vcpu->stat.mmio_exits;
6882
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006883 if (!vcpu->mmio_is_write)
6884 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006885 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006886 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006887 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006888 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006889 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006890 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006891
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006892 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006893 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006894 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006895 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006896 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006897 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6898 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006899 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006900 r = kvm_vcpu_do_singlestep(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006901 if (kvm_x86_ops->update_emulated_instruction)
6902 kvm_x86_ops->update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006903 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006904 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006905
6906 /*
6907 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6908 * do nothing, and it will be requested again as soon as
6909 * the shadow expires. But we still need to check here,
6910 * because POPF has no interrupt shadow.
6911 */
6912 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6913 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006914 } else
6915 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006916
Gleb Natapove85d28f2010-07-29 15:11:52 +03006917 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006918}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006919
6920int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6921{
6922 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6923}
6924EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6925
6926int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6927 void *insn, int insn_len)
6928{
6929 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6930}
6931EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006932
Sean Christopherson8764ed52019-04-29 07:04:15 -07006933static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6934{
6935 vcpu->arch.pio.count = 0;
6936 return 1;
6937}
6938
Sean Christopherson45def772019-03-11 20:01:05 -07006939static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6940{
6941 vcpu->arch.pio.count = 0;
6942
6943 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6944 return 1;
6945
6946 return kvm_skip_emulated_instruction(vcpu);
6947}
6948
Sean Christophersondca7f122018-03-08 08:57:27 -08006949static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6950 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006951{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006952 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006953 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6954
Sean Christopherson8764ed52019-04-29 07:04:15 -07006955 if (ret)
6956 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006957
Sean Christopherson8764ed52019-04-29 07:04:15 -07006958 /*
6959 * Workaround userspace that relies on old KVM behavior of %rip being
6960 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6961 */
6962 if (port == 0x7e &&
6963 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6964 vcpu->arch.complete_userspace_io =
6965 complete_fast_pio_out_port_0x7e;
6966 kvm_skip_emulated_instruction(vcpu);
6967 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006968 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6969 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6970 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006971 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006972}
Carsten Ottede7d7892007-10-30 18:44:25 +01006973
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006974static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6975{
6976 unsigned long val;
6977
6978 /* We should only ever be called with arch.pio.count equal to 1 */
6979 BUG_ON(vcpu->arch.pio.count != 1);
6980
Sean Christopherson45def772019-03-11 20:01:05 -07006981 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6982 vcpu->arch.pio.count = 0;
6983 return 1;
6984 }
6985
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006986 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006987 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006988
6989 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006990 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006991 * the copy and tracing
6992 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006993 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006994 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006995
Sean Christopherson45def772019-03-11 20:01:05 -07006996 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006997}
6998
Sean Christophersondca7f122018-03-08 08:57:27 -08006999static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7000 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007001{
7002 unsigned long val;
7003 int ret;
7004
7005 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007006 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007007
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007008 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007009 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007010 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007011 return ret;
7012 }
7013
Sean Christopherson45def772019-03-11 20:01:05 -07007014 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007015 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7016
7017 return 0;
7018}
Sean Christophersondca7f122018-03-08 08:57:27 -08007019
7020int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7021{
Sean Christopherson45def772019-03-11 20:01:05 -07007022 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007023
Sean Christophersondca7f122018-03-08 08:57:27 -08007024 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007025 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007026 else
Sean Christopherson45def772019-03-11 20:01:05 -07007027 ret = kvm_fast_pio_out(vcpu, size, port);
7028 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007029}
7030EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007031
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007032static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007033{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007034 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007035 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007036}
7037
7038static void tsc_khz_changed(void *data)
7039{
7040 struct cpufreq_freqs *freq = data;
7041 unsigned long khz = 0;
7042
7043 if (data)
7044 khz = freq->new;
7045 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7046 khz = cpufreq_quick_get(raw_smp_processor_id());
7047 if (!khz)
7048 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007049 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007050}
7051
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007052#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007053static void kvm_hyperv_tsc_notifier(void)
7054{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007055 struct kvm *kvm;
7056 struct kvm_vcpu *vcpu;
7057 int cpu;
7058
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007059 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007060 list_for_each_entry(kvm, &vm_list, vm_list)
7061 kvm_make_mclock_inprogress_request(kvm);
7062
7063 hyperv_stop_tsc_emulation();
7064
7065 /* TSC frequency always matches when on Hyper-V */
7066 for_each_present_cpu(cpu)
7067 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7068 kvm_max_guest_tsc_khz = tsc_khz;
7069
7070 list_for_each_entry(kvm, &vm_list, vm_list) {
7071 struct kvm_arch *ka = &kvm->arch;
7072
7073 spin_lock(&ka->pvclock_gtod_sync_lock);
7074
7075 pvclock_update_vm_gtod_copy(kvm);
7076
7077 kvm_for_each_vcpu(cpu, vcpu, kvm)
7078 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7079
7080 kvm_for_each_vcpu(cpu, vcpu, kvm)
7081 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7082
7083 spin_unlock(&ka->pvclock_gtod_sync_lock);
7084 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007085 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007086}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007087#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007088
Viresh Kumardf240142019-04-29 15:03:58 +05307089static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007090{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007091 struct kvm *kvm;
7092 struct kvm_vcpu *vcpu;
7093 int i, send_ipi = 0;
7094
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007095 /*
7096 * We allow guests to temporarily run on slowing clocks,
7097 * provided we notify them after, or to run on accelerating
7098 * clocks, provided we notify them before. Thus time never
7099 * goes backwards.
7100 *
7101 * However, we have a problem. We can't atomically update
7102 * the frequency of a given CPU from this function; it is
7103 * merely a notifier, which can be called from any CPU.
7104 * Changing the TSC frequency at arbitrary points in time
7105 * requires a recomputation of local variables related to
7106 * the TSC for each VCPU. We must flag these local variables
7107 * to be updated and be sure the update takes place with the
7108 * new frequency before any guests proceed.
7109 *
7110 * Unfortunately, the combination of hotplug CPU and frequency
7111 * change creates an intractable locking scenario; the order
7112 * of when these callouts happen is undefined with respect to
7113 * CPU hotplug, and they can race with each other. As such,
7114 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7115 * undefined; you can actually have a CPU frequency change take
7116 * place in between the computation of X and the setting of the
7117 * variable. To protect against this problem, all updates of
7118 * the per_cpu tsc_khz variable are done in an interrupt
7119 * protected IPI, and all callers wishing to update the value
7120 * must wait for a synchronous IPI to complete (which is trivial
7121 * if the caller is on the CPU already). This establishes the
7122 * necessary total order on variable updates.
7123 *
7124 * Note that because a guest time update may take place
7125 * anytime after the setting of the VCPU's request bit, the
7126 * correct TSC value must be set before the request. However,
7127 * to ensure the update actually makes it to any guest which
7128 * starts running in hardware virtualization between the set
7129 * and the acquisition of the spinlock, we must also ping the
7130 * CPU after setting the request bit.
7131 *
7132 */
7133
Viresh Kumardf240142019-04-29 15:03:58 +05307134 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007135
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007136 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007137 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007138 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307139 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007140 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007141 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007142 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007143 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007144 }
7145 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007146 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007147
7148 if (freq->old < freq->new && send_ipi) {
7149 /*
7150 * We upscale the frequency. Must make the guest
7151 * doesn't see old kvmclock values while running with
7152 * the new frequency, otherwise we risk the guest sees
7153 * time go backwards.
7154 *
7155 * In case we update the frequency for another cpu
7156 * (which might be in guest context) send an interrupt
7157 * to kick the cpu out of guest context. Next time
7158 * guest context is entered kvmclock will be updated,
7159 * so the guest will not see stale values.
7160 */
Viresh Kumardf240142019-04-29 15:03:58 +05307161 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007162 }
Viresh Kumardf240142019-04-29 15:03:58 +05307163}
7164
7165static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7166 void *data)
7167{
7168 struct cpufreq_freqs *freq = data;
7169 int cpu;
7170
7171 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7172 return 0;
7173 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7174 return 0;
7175
7176 for_each_cpu(cpu, freq->policy->cpus)
7177 __kvmclock_cpufreq_notifier(freq, cpu);
7178
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007179 return 0;
7180}
7181
7182static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007183 .notifier_call = kvmclock_cpufreq_notifier
7184};
7185
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007186static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007187{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007188 tsc_khz_changed(NULL);
7189 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007190}
7191
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007192static void kvm_timer_init(void)
7193{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007194 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307195
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007196 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007197#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007198 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007199 int cpu;
7200
Avi Kivity3e26f232010-12-16 12:16:34 +02007201 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007202 policy = cpufreq_cpu_get(cpu);
7203 if (policy && policy->cpuinfo.max_freq)
7204 max_tsc_khz = policy->cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007205 put_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007206 cpufreq_cpu_put(policy);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007207#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007208 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7209 CPUFREQ_TRANSITION_NOTIFIER);
7210 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307211
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007212 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007213 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007214}
7215
Andi Kleendd60d212017-07-25 17:20:32 -07007216DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7217EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007218
Gleb Natapovf5132b02011-11-10 14:57:22 +02007219int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007220{
Alex,Shi086c9852011-10-20 15:34:01 +08007221 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007222}
7223
7224static int kvm_is_user_mode(void)
7225{
7226 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007227
Alex,Shi086c9852011-10-20 15:34:01 +08007228 if (__this_cpu_read(current_vcpu))
7229 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007230
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007231 return user_mode != 0;
7232}
7233
7234static unsigned long kvm_get_guest_ip(void)
7235{
7236 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007237
Alex,Shi086c9852011-10-20 15:34:01 +08007238 if (__this_cpu_read(current_vcpu))
7239 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007240
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007241 return ip;
7242}
7243
Luwei Kang8479e042019-02-18 19:26:07 -05007244static void kvm_handle_intel_pt_intr(void)
7245{
7246 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7247
7248 kvm_make_request(KVM_REQ_PMI, vcpu);
7249 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7250 (unsigned long *)&vcpu->arch.pmu.global_status);
7251}
7252
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007253static struct perf_guest_info_callbacks kvm_guest_cbs = {
7254 .is_in_guest = kvm_is_in_guest,
7255 .is_user_mode = kvm_is_user_mode,
7256 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007257 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007258};
7259
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007260#ifdef CONFIG_X86_64
7261static void pvclock_gtod_update_fn(struct work_struct *work)
7262{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007263 struct kvm *kvm;
7264
7265 struct kvm_vcpu *vcpu;
7266 int i;
7267
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007268 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007269 list_for_each_entry(kvm, &vm_list, vm_list)
7270 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007271 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007272 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007273 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007274}
7275
7276static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7277
7278/*
7279 * Notification about pvclock gtod data update.
7280 */
7281static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7282 void *priv)
7283{
7284 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7285 struct timekeeper *tk = priv;
7286
7287 update_pvclock_gtod(tk);
7288
7289 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007290 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007291 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007292 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007293 atomic_read(&kvm_guest_has_master_clock) != 0)
7294 queue_work(system_long_wq, &pvclock_gtod_work);
7295
7296 return 0;
7297}
7298
7299static struct notifier_block pvclock_gtod_notifier = {
7300 .notifier_call = pvclock_gtod_notify,
7301};
7302#endif
7303
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007304int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007305{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007306 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007307 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007308
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007309 if (kvm_x86_ops) {
7310 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007311 r = -EEXIST;
7312 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007313 }
7314
7315 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007316 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007317 r = -EOPNOTSUPP;
7318 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007319 }
7320 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007321 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007322 r = -EOPNOTSUPP;
7323 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007324 }
7325
Marc Orrb666a4b2018-11-06 14:53:56 -08007326 /*
7327 * KVM explicitly assumes that the guest has an FPU and
7328 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7329 * vCPU's FPU state as a fxregs_state struct.
7330 */
7331 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7332 printk(KERN_ERR "kvm: inadequate fpu\n");
7333 r = -EOPNOTSUPP;
7334 goto out;
7335 }
7336
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007337 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007338 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007339 __alignof__(struct fpu), SLAB_ACCOUNT,
7340 NULL);
7341 if (!x86_fpu_cache) {
7342 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7343 goto out;
7344 }
7345
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007346 x86_emulator_cache = kvm_alloc_emulator_cache();
7347 if (!x86_emulator_cache) {
7348 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7349 goto out_free_x86_fpu_cache;
7350 }
7351
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007352 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7353 if (!shared_msrs) {
7354 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007355 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007356 }
7357
Avi Kivity97db56c2008-01-13 13:23:56 +02007358 r = kvm_mmu_module_init();
7359 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007360 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007361
Sheng Yang7b523452008-04-25 21:13:50 +08007362 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007363 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007364 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007365 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007366
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007367 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7368
Sean Christophersoncfc48182020-03-02 15:56:23 -08007369 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007370 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007371 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7372 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007373
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007374 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007375 if (pi_inject_timer == -1)
7376 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007377#ifdef CONFIG_X86_64
7378 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007379
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007380 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007381 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007382#endif
7383
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007384 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007385
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007386out_free_percpu:
7387 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007388out_free_x86_emulator_cache:
7389 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007390out_free_x86_fpu_cache:
7391 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007392out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007393 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007394}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007395
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007396void kvm_arch_exit(void)
7397{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007398#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007399 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007400 clear_hv_tscchange_cb();
7401#endif
David Matlackcef84c32016-12-16 14:30:36 -08007402 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007403 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7404
Jan Kiszka888d2562009-04-17 19:24:58 +02007405 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7406 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7407 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007408 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007409#ifdef CONFIG_X86_64
7410 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7411#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007412 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007413 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007414 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007415 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007416}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007417
Joel Schopp5cb56052015-03-02 13:43:31 -06007418int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007419{
7420 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007421 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007422 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007423 return 1;
7424 } else {
7425 vcpu->run->exit_reason = KVM_EXIT_HLT;
7426 return 0;
7427 }
7428}
Joel Schopp5cb56052015-03-02 13:43:31 -06007429EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7430
7431int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7432{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007433 int ret = kvm_skip_emulated_instruction(vcpu);
7434 /*
7435 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7436 * KVM_EXIT_DEBUG here.
7437 */
7438 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007439}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007440EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7441
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007442#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007443static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7444 unsigned long clock_type)
7445{
7446 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007447 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007448 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007449 int ret;
7450
7451 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7452 return -KVM_EOPNOTSUPP;
7453
7454 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7455 return -KVM_EOPNOTSUPP;
7456
7457 clock_pairing.sec = ts.tv_sec;
7458 clock_pairing.nsec = ts.tv_nsec;
7459 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7460 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007461 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007462
7463 ret = 0;
7464 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7465 sizeof(struct kvm_clock_pairing)))
7466 ret = -KVM_EFAULT;
7467
7468 return ret;
7469}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007470#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007471
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307472/*
7473 * kvm_pv_kick_cpu_op: Kick a vcpu.
7474 *
7475 * @apicid - apicid of vcpu to be kicked.
7476 */
7477static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7478{
Raghavendra K T24d21662013-08-26 14:18:35 +05307479 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307480
Peter Xu150a84f2019-12-04 20:07:21 +01007481 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007482 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007483 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307484 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007485 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307486
Raghavendra K T24d21662013-08-26 14:18:35 +05307487 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007488 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307489}
7490
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007491bool kvm_apicv_activated(struct kvm *kvm)
7492{
7493 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7494}
7495EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7496
7497void kvm_apicv_init(struct kvm *kvm, bool enable)
7498{
7499 if (enable)
7500 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7501 &kvm->arch.apicv_inhibit_reasons);
7502 else
7503 set_bit(APICV_INHIBIT_REASON_DISABLE,
7504 &kvm->arch.apicv_inhibit_reasons);
7505}
7506EXPORT_SYMBOL_GPL(kvm_apicv_init);
7507
Wanpeng Li71506292019-06-11 20:23:49 +08007508static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7509{
7510 struct kvm_vcpu *target = NULL;
7511 struct kvm_apic_map *map;
7512
7513 rcu_read_lock();
7514 map = rcu_dereference(kvm->arch.apic_map);
7515
7516 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7517 target = map->phys_map[dest_id]->vcpu;
7518
7519 rcu_read_unlock();
7520
Wanpeng Li266e85a2019-07-24 17:43:13 +08007521 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007522 kvm_vcpu_yield_to(target);
7523}
7524
Hollis Blanchard8776e512007-10-31 17:24:24 -05007525int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7526{
7527 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007528 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007529
Radim Krčmář696ca772018-05-24 17:50:56 +02007530 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7531 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007532
Sean Christophersonde3cd112019-04-30 10:36:17 -07007533 nr = kvm_rax_read(vcpu);
7534 a0 = kvm_rbx_read(vcpu);
7535 a1 = kvm_rcx_read(vcpu);
7536 a2 = kvm_rdx_read(vcpu);
7537 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007538
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007539 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007540
Nadav Amita449c7a2014-06-18 17:19:24 +03007541 op_64_bit = is_64_bit_mode(vcpu);
7542 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007543 nr &= 0xFFFFFFFF;
7544 a0 &= 0xFFFFFFFF;
7545 a1 &= 0xFFFFFFFF;
7546 a2 &= 0xFFFFFFFF;
7547 a3 &= 0xFFFFFFFF;
7548 }
7549
Jan Kiszka07708c42009-08-03 18:43:28 +02007550 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7551 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007552 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007553 }
7554
Hollis Blanchard8776e512007-10-31 17:24:24 -05007555 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007556 case KVM_HC_VAPIC_POLL_IRQ:
7557 ret = 0;
7558 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307559 case KVM_HC_KICK_CPU:
7560 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007561 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307562 ret = 0;
7563 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007564#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007565 case KVM_HC_CLOCK_PAIRING:
7566 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7567 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007568#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007569 case KVM_HC_SEND_IPI:
7570 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7571 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007572 case KVM_HC_SCHED_YIELD:
7573 kvm_sched_yield(vcpu->kvm, a0);
7574 ret = 0;
7575 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007576 default:
7577 ret = -KVM_ENOSYS;
7578 break;
7579 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007580out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007581 if (!op_64_bit)
7582 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007583 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007584
Amit Shahf11c3a82008-02-21 01:00:30 +05307585 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007586 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007587}
7588EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7589
Jan Kiszkab6785de2012-09-20 07:43:17 +02007590static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007591{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007592 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007593 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007594 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007595
Hollis Blanchard8776e512007-10-31 17:24:24 -05007596 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007597
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007598 return emulator_write_emulated(ctxt, rip, instruction, 3,
7599 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007600}
7601
Avi Kivity851ba692009-08-24 11:10:17 +03007602static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007603{
Matt Gingell782d4222015-11-16 15:26:00 -08007604 return vcpu->run->request_interrupt_window &&
7605 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007606}
7607
Avi Kivity851ba692009-08-24 11:10:17 +03007608static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007609{
Avi Kivity851ba692009-08-24 11:10:17 +03007610 struct kvm_run *kvm_run = vcpu->run;
7611
Jan Kiszka91586a32009-10-05 13:07:21 +02007612 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007613 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007614 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007615 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007616 kvm_run->ready_for_interrupt_injection =
7617 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007618 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007619}
7620
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007621static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7622{
7623 int max_irr, tpr;
7624
7625 if (!kvm_x86_ops->update_cr8_intercept)
7626 return;
7627
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007628 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007629 return;
7630
Andrey Smetanind62caab2015-11-10 15:36:33 +03007631 if (vcpu->arch.apicv_active)
7632 return;
7633
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007634 if (!vcpu->arch.apic->vapic_addr)
7635 max_irr = kvm_lapic_find_highest_irr(vcpu);
7636 else
7637 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007638
7639 if (max_irr != -1)
7640 max_irr >>= 4;
7641
7642 tpr = kvm_lapic_get_cr8(vcpu);
7643
7644 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7645}
7646
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007647static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007648{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007649 int r;
7650
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007651 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007652
Liran Alon1a680e32018-03-23 03:01:33 +03007653 if (vcpu->arch.exception.injected)
7654 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007655 /*
Liran Alona042c262018-03-23 03:01:32 +03007656 * Do not inject an NMI or interrupt if there is a pending
7657 * exception. Exceptions and interrupts are recognized at
7658 * instruction boundaries, i.e. the start of an instruction.
7659 * Trap-like exceptions, e.g. #DB, have higher priority than
7660 * NMIs and interrupts, i.e. traps are recognized before an
7661 * NMI/interrupt that's pending on the same instruction.
7662 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7663 * priority, but are only generated (pended) during instruction
7664 * execution, i.e. a pending fault-like exception means the
7665 * fault occurred on the *previous* instruction and must be
7666 * serviced prior to recognizing any new events in order to
7667 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007668 */
Liran Alon1a680e32018-03-23 03:01:33 +03007669 else if (!vcpu->arch.exception.pending) {
7670 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007671 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007672 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007673 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007674 }
7675
Liran Alon1a680e32018-03-23 03:01:33 +03007676 /*
7677 * Call check_nested_events() even if we reinjected a previous event
7678 * in order for caller to determine if it should require immediate-exit
7679 * from L2 to L1 due to pending L1 events which require exit
7680 * from L2 to L1.
7681 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007682 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007683 r = kvm_x86_ops->check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007684 if (r != 0)
7685 return r;
7686 }
7687
7688 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007689 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007690 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7691 vcpu->arch.exception.has_error_code,
7692 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007693
Liran Alon1a680e32018-03-23 03:01:33 +03007694 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007695 vcpu->arch.exception.pending = false;
7696 vcpu->arch.exception.injected = true;
7697
Nadav Amitd6e8c852014-07-24 14:51:24 +03007698 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7699 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7700 X86_EFLAGS_RF);
7701
Jim Mattsonf10c7292018-10-16 14:29:23 -07007702 if (vcpu->arch.exception.nr == DB_VECTOR) {
7703 /*
7704 * This code assumes that nSVM doesn't use
7705 * check_nested_events(). If it does, the
7706 * DR6/DR7 changes should happen before L1
7707 * gets a #VMEXIT for an intercepted #DB in
7708 * L2. (Under VMX, on the other hand, the
7709 * DR6/DR7 changes should not happen in the
7710 * event of a VM-exit to L1 for an intercepted
7711 * #DB in L2.)
7712 */
7713 kvm_deliver_exception_payload(vcpu);
7714 if (vcpu->arch.dr7 & DR7_GD) {
7715 vcpu->arch.dr7 &= ~DR7_GD;
7716 kvm_update_dr7(vcpu);
7717 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007718 }
7719
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007720 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007721 }
7722
7723 /* Don't consider new event if we re-injected an event */
7724 if (kvm_event_needs_reinjection(vcpu))
7725 return 0;
7726
7727 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7728 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007729 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007730 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007731 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007732 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007733 --vcpu->arch.nmi_pending;
7734 vcpu->arch.nmi_injected = true;
7735 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007736 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007737 /*
7738 * Because interrupts can be injected asynchronously, we are
7739 * calling check_nested_events again here to avoid a race condition.
7740 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7741 * proposal and current concerns. Perhaps we should be setting
7742 * KVM_REQ_EVENT only on certain events and not unconditionally?
7743 */
7744 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007745 r = kvm_x86_ops->check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007746 if (r != 0)
7747 return r;
7748 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007749 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007750 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7751 false);
7752 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007753 }
7754 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007755
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007756 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007757}
7758
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007759static void process_nmi(struct kvm_vcpu *vcpu)
7760{
7761 unsigned limit = 2;
7762
7763 /*
7764 * x86 is limited to one NMI running, and one NMI pending after it.
7765 * If an NMI is already in progress, limit further NMIs to just one.
7766 * Otherwise, allow two (and we'll inject the first one immediately).
7767 */
7768 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7769 limit = 1;
7770
7771 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7772 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7773 kvm_make_request(KVM_REQ_EVENT, vcpu);
7774}
7775
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007776static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007777{
7778 u32 flags = 0;
7779 flags |= seg->g << 23;
7780 flags |= seg->db << 22;
7781 flags |= seg->l << 21;
7782 flags |= seg->avl << 20;
7783 flags |= seg->present << 15;
7784 flags |= seg->dpl << 13;
7785 flags |= seg->s << 12;
7786 flags |= seg->type << 8;
7787 return flags;
7788}
7789
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007790static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007791{
7792 struct kvm_segment seg;
7793 int offset;
7794
7795 kvm_get_segment(vcpu, &seg, n);
7796 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7797
7798 if (n < 3)
7799 offset = 0x7f84 + n * 12;
7800 else
7801 offset = 0x7f2c + (n - 3) * 12;
7802
7803 put_smstate(u32, buf, offset + 8, seg.base);
7804 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007805 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007806}
7807
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007808#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007809static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007810{
7811 struct kvm_segment seg;
7812 int offset;
7813 u16 flags;
7814
7815 kvm_get_segment(vcpu, &seg, n);
7816 offset = 0x7e00 + n * 16;
7817
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007818 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007819 put_smstate(u16, buf, offset, seg.selector);
7820 put_smstate(u16, buf, offset + 2, flags);
7821 put_smstate(u32, buf, offset + 4, seg.limit);
7822 put_smstate(u64, buf, offset + 8, seg.base);
7823}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007824#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007825
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007826static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007827{
7828 struct desc_ptr dt;
7829 struct kvm_segment seg;
7830 unsigned long val;
7831 int i;
7832
7833 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7834 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7835 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7836 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7837
7838 for (i = 0; i < 8; i++)
7839 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7840
7841 kvm_get_dr(vcpu, 6, &val);
7842 put_smstate(u32, buf, 0x7fcc, (u32)val);
7843 kvm_get_dr(vcpu, 7, &val);
7844 put_smstate(u32, buf, 0x7fc8, (u32)val);
7845
7846 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7847 put_smstate(u32, buf, 0x7fc4, seg.selector);
7848 put_smstate(u32, buf, 0x7f64, seg.base);
7849 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007850 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007851
7852 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7853 put_smstate(u32, buf, 0x7fc0, seg.selector);
7854 put_smstate(u32, buf, 0x7f80, seg.base);
7855 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007856 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007857
7858 kvm_x86_ops->get_gdt(vcpu, &dt);
7859 put_smstate(u32, buf, 0x7f74, dt.address);
7860 put_smstate(u32, buf, 0x7f70, dt.size);
7861
7862 kvm_x86_ops->get_idt(vcpu, &dt);
7863 put_smstate(u32, buf, 0x7f58, dt.address);
7864 put_smstate(u32, buf, 0x7f54, dt.size);
7865
7866 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007867 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007868
7869 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7870
7871 /* revision id */
7872 put_smstate(u32, buf, 0x7efc, 0x00020000);
7873 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7874}
7875
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007876#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007877static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007878{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007879 struct desc_ptr dt;
7880 struct kvm_segment seg;
7881 unsigned long val;
7882 int i;
7883
7884 for (i = 0; i < 16; i++)
7885 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7886
7887 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7888 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7889
7890 kvm_get_dr(vcpu, 6, &val);
7891 put_smstate(u64, buf, 0x7f68, val);
7892 kvm_get_dr(vcpu, 7, &val);
7893 put_smstate(u64, buf, 0x7f60, val);
7894
7895 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7896 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7897 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7898
7899 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7900
7901 /* revision id */
7902 put_smstate(u32, buf, 0x7efc, 0x00020064);
7903
7904 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7905
7906 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7907 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007908 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007909 put_smstate(u32, buf, 0x7e94, seg.limit);
7910 put_smstate(u64, buf, 0x7e98, seg.base);
7911
7912 kvm_x86_ops->get_idt(vcpu, &dt);
7913 put_smstate(u32, buf, 0x7e84, dt.size);
7914 put_smstate(u64, buf, 0x7e88, dt.address);
7915
7916 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7917 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007918 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007919 put_smstate(u32, buf, 0x7e74, seg.limit);
7920 put_smstate(u64, buf, 0x7e78, seg.base);
7921
7922 kvm_x86_ops->get_gdt(vcpu, &dt);
7923 put_smstate(u32, buf, 0x7e64, dt.size);
7924 put_smstate(u64, buf, 0x7e68, dt.address);
7925
7926 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007927 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007928}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007929#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007930
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007931static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007932{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007933 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007934 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007935 char buf[512];
7936 u32 cr0;
7937
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007938 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007939 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007940#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007941 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007942 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007943 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007944#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007945 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007946
Ladi Prosek0234bf82017-10-11 16:54:40 +02007947 /*
7948 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7949 * vCPU state (e.g. leave guest mode) after we've saved the state into
7950 * the SMM state-save area.
7951 */
7952 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7953
7954 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007955 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007956
7957 if (kvm_x86_ops->get_nmi_mask(vcpu))
7958 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7959 else
7960 kvm_x86_ops->set_nmi_mask(vcpu, true);
7961
7962 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7963 kvm_rip_write(vcpu, 0x8000);
7964
7965 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7966 kvm_x86_ops->set_cr0(vcpu, cr0);
7967 vcpu->arch.cr0 = cr0;
7968
7969 kvm_x86_ops->set_cr4(vcpu, 0);
7970
Paolo Bonzini18c36262015-08-07 12:27:54 +02007971 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7972 dt.address = dt.size = 0;
7973 kvm_x86_ops->set_idt(vcpu, &dt);
7974
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007975 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7976
7977 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7978 cs.base = vcpu->arch.smbase;
7979
7980 ds.selector = 0;
7981 ds.base = 0;
7982
7983 cs.limit = ds.limit = 0xffffffff;
7984 cs.type = ds.type = 0x3;
7985 cs.dpl = ds.dpl = 0;
7986 cs.db = ds.db = 0;
7987 cs.s = ds.s = 1;
7988 cs.l = ds.l = 0;
7989 cs.g = ds.g = 1;
7990 cs.avl = ds.avl = 0;
7991 cs.present = ds.present = 1;
7992 cs.unusable = ds.unusable = 0;
7993 cs.padding = ds.padding = 0;
7994
7995 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7996 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7997 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7998 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7999 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8000 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8001
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008002#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008003 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008004 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008005#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008006
8007 kvm_update_cpuid(vcpu);
8008 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008009}
8010
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008011static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008012{
8013 vcpu->arch.smi_pending = true;
8014 kvm_make_request(KVM_REQ_EVENT, vcpu);
8015}
8016
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008017void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8018 unsigned long *vcpu_bitmap)
8019{
8020 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008021
8022 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8023
Mao Wenandb5a95e2019-11-19 11:06:40 +08008024 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
8025 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008026
8027 free_cpumask_var(cpus);
8028}
8029
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008030void kvm_make_scan_ioapic_request(struct kvm *kvm)
8031{
8032 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8033}
8034
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008035void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8036{
8037 if (!lapic_in_kernel(vcpu))
8038 return;
8039
8040 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8041 kvm_apic_update_apicv(vcpu);
8042 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
8043}
8044EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8045
8046/*
8047 * NOTE: Do not hold any lock prior to calling this.
8048 *
8049 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8050 * locked, because it calls __x86_set_memory_region() which does
8051 * synchronize_srcu(&kvm->srcu).
8052 */
8053void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8054{
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008055 unsigned long old, new, expected;
8056
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008057 if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
8058 !kvm_x86_ops->check_apicv_inhibit_reasons(bit))
8059 return;
8060
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008061 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8062 do {
8063 expected = new = old;
8064 if (activate)
8065 __clear_bit(bit, &new);
8066 else
8067 __set_bit(bit, &new);
8068 if (new == old)
8069 break;
8070 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8071 } while (old != expected);
8072
8073 if (!!old == !!new)
8074 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008075
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008076 trace_kvm_apicv_update_request(activate, bit);
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06008077 if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
8078 kvm_x86_ops->pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008079 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
8080}
8081EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8082
Yang Zhang3d81bc72013-04-11 19:25:13 +08008083static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008084{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008085 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008086 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008087
Andrey Smetanin63086302015-11-10 15:36:32 +03008088 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008089
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008090 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008091 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008092 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008093 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03008094 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008095 if (ioapic_in_kernel(vcpu->kvm))
8096 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008097 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008098
8099 if (is_guest_mode(vcpu))
8100 vcpu->arch.load_eoi_exitmap_pending = true;
8101 else
8102 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8103}
8104
8105static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8106{
8107 u64 eoi_exit_bitmap[4];
8108
8109 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8110 return;
8111
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008112 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8113 vcpu_to_synic(vcpu)->vec_bitmap, 256);
8114 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008115}
8116
Michal Hocko93065ac2018-08-21 21:52:33 -07008117int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8118 unsigned long start, unsigned long end,
8119 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008120{
8121 unsigned long apic_address;
8122
8123 /*
8124 * The physical address of apic access page is stored in the VMCS.
8125 * Update it when it becomes invalid.
8126 */
8127 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8128 if (start <= apic_address && apic_address < end)
8129 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008130
8131 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008132}
8133
Tang Chen4256f432014-09-24 15:57:54 +08008134void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8135{
Tang Chenc24ae0d2014-09-24 15:57:58 +08008136 struct page *page = NULL;
8137
Paolo Bonzini35754c92015-07-29 12:05:37 +02008138 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008139 return;
8140
Tang Chen4256f432014-09-24 15:57:54 +08008141 if (!kvm_x86_ops->set_apic_access_page_addr)
8142 return;
8143
Tang Chenc24ae0d2014-09-24 15:57:58 +08008144 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02008145 if (is_error_page(page))
8146 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08008147 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
8148
8149 /*
8150 * Do not pin apic access page in memory, the MMU notifier
8151 * will call us again if it is migrated or swapped out.
8152 */
8153 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08008154}
Tang Chen4256f432014-09-24 15:57:54 +08008155
Sean Christophersond264ee02018-08-27 15:21:12 -07008156void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8157{
8158 smp_send_reschedule(vcpu->cpu);
8159}
8160EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8161
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008162/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008163 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008164 * exiting to the userspace. Otherwise, the value will be returned to the
8165 * userspace.
8166 */
Avi Kivity851ba692009-08-24 11:10:17 +03008167static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008168{
8169 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008170 bool req_int_win =
8171 dm_request_for_irq_injection(vcpu) &&
8172 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008173 enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE;
Matt Gingell62a193e2015-11-16 15:26:07 -08008174
Jan Kiszka730dca42013-04-28 10:50:52 +02008175 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008176
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008177 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008178 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8179 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8180 r = 0;
8181 goto out;
8182 }
8183 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008184 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008185 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008186 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008187 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008188 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8189 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008190 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8191 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008192 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8193 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008194 if (unlikely(r))
8195 goto out;
8196 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008197 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008198 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008199 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8200 kvm_mmu_load_pgd(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008201 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008202 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008203 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008204 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008205 r = 0;
8206 goto out;
8207 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008208 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008209 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008210 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008211 r = 0;
8212 goto out;
8213 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008214 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8215 /* Page is swapped out. Do synthetic halt */
8216 vcpu->arch.apf.halted = true;
8217 r = 1;
8218 goto out;
8219 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008220 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8221 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008222 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8223 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008224 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8225 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008226 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008227 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008228 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008229 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008230 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8231 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8232 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008233 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008234 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8235 vcpu->run->eoi.vector =
8236 vcpu->arch.pending_ioapic_eoi;
8237 r = 0;
8238 goto out;
8239 }
8240 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008241 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8242 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008243 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8244 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008245 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8246 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008247 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8248 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8249 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8250 r = 0;
8251 goto out;
8252 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008253 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8254 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8255 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8256 r = 0;
8257 goto out;
8258 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008259 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8260 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8261 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8262 r = 0;
8263 goto out;
8264 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008265
8266 /*
8267 * KVM_REQ_HV_STIMER has to be processed after
8268 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8269 * depend on the guest clock being up-to-date
8270 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008271 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8272 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008273 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8274 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008275 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008276
Avi Kivityb463a6f2010-07-20 15:06:17 +03008277 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008278 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008279 kvm_apic_accept_events(vcpu);
8280 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8281 r = 1;
8282 goto out;
8283 }
8284
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008285 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008286 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008287 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008288 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008289 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008290 * SMIs have three cases:
8291 * 1) They can be nested, and then there is nothing to
8292 * do here because RSM will cause a vmexit anyway.
8293 * 2) There is an ISA-specific reason why SMI cannot be
8294 * injected, and the moment when this changes can be
8295 * intercepted.
8296 * 3) Or the SMI can be pending because
8297 * inject_pending_event has completed the injection
8298 * of an IRQ or NMI from the previous vmexit, and
8299 * then we request an immediate exit to inject the
8300 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008301 */
8302 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008303 if (!kvm_x86_ops->enable_smi_window(vcpu))
8304 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008305 if (vcpu->arch.nmi_pending)
8306 kvm_x86_ops->enable_nmi_window(vcpu);
8307 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8308 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008309 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008310 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008311
Avi Kivity3842d132010-07-27 12:30:24 +03008312 if (kvm_lapic_enabled(vcpu)) {
8313 update_cr8_intercept(vcpu);
8314 kvm_lapic_sync_to_vapic(vcpu);
8315 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008316 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008317
Avi Kivityd8368af2012-05-14 18:07:56 +03008318 r = kvm_mmu_reload(vcpu);
8319 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008320 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008321 }
8322
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008323 preempt_disable();
8324
8325 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008326
8327 /*
8328 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8329 * IPI are then delayed after guest entry, which ensures that they
8330 * result in virtual interrupt delivery.
8331 */
8332 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008333 vcpu->mode = IN_GUEST_MODE;
8334
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008335 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8336
Lan Tianyu0f127d12016-03-13 11:10:29 +08008337 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008338 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008339 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008340 *
Luwei Kang81b01662019-01-31 16:52:02 +08008341 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008342 * pairs with the memory barrier implicit in pi_test_and_set_on
8343 * (see vmx_deliver_posted_interrupt).
8344 *
8345 * 3) This also orders the write to mode from any reads to the page
8346 * tables done while the VCPU is running. Please see the comment
8347 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008348 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008349 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008350
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008351 /*
8352 * This handles the case where a posted interrupt was
8353 * notified with kvm_vcpu_kick.
8354 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008355 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8356 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008357
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008358 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008359 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008360 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008361 smp_wmb();
8362 local_irq_enable();
8363 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008364 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008365 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008366 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008367 }
8368
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008369 if (req_immediate_exit) {
8370 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008371 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008372 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008373
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008374 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008375 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008376
Sean Christopherson2620fe22020-01-17 11:30:51 -08008377 fpregs_assert_state_consistent();
8378 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8379 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008380
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008381 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008382 set_debugreg(0, 7);
8383 set_debugreg(vcpu->arch.eff_db[0], 0);
8384 set_debugreg(vcpu->arch.eff_db[1], 1);
8385 set_debugreg(vcpu->arch.eff_db[2], 2);
8386 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008387 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008388 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008389 }
8390
Avi Kivity851ba692009-08-24 11:10:17 +03008391 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008392
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008393 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008394 * Do this here before restoring debug registers on the host. And
8395 * since we do this before handling the vmexit, a DR access vmexit
8396 * can (a) read the correct value of the debug registers, (b) set
8397 * KVM_DEBUGREG_WONT_EXIT again.
8398 */
8399 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008400 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8401 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008402 kvm_update_dr0123(vcpu);
8403 kvm_update_dr6(vcpu);
8404 kvm_update_dr7(vcpu);
8405 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008406 }
8407
8408 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008409 * If the guest has used debug registers, at least dr7
8410 * will be disabled while returning to the host.
8411 * If we don't have active breakpoints in the host, we don't
8412 * care about the messed up debug address registers. But if
8413 * we have some of them active, restore the old state.
8414 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008415 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008416 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008417
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008418 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008419
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008420 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008421 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008422
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008423 kvm_x86_ops->handle_exit_irqoff(vcpu, &exit_fastpath);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008424
Sean Christophersond7a08882019-07-10 09:07:34 -07008425 /*
8426 * Consume any pending interrupts, including the possible source of
8427 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8428 * An instruction is required after local_irq_enable() to fully unblock
8429 * interrupts on processors that implement an interrupt shadow, the
8430 * stat.exits increment will do nicely.
8431 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008432 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008433 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008434 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008435 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008436 kvm_after_interrupt(vcpu);
8437
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008438 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008439 if (lapic_in_kernel(vcpu)) {
8440 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8441 if (delta != S64_MIN) {
8442 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8443 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8444 }
8445 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008446
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008447 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008448 preempt_enable();
8449
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008450 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008451
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008452 /*
8453 * Profile KVM exit RIPs:
8454 */
8455 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008456 unsigned long rip = kvm_rip_read(vcpu);
8457 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008458 }
8459
Zachary Amsdencc578282012-02-03 15:43:50 -02008460 if (unlikely(vcpu->arch.tsc_always_catchup))
8461 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008462
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008463 if (vcpu->arch.apic_attention)
8464 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008465
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008466 r = kvm_x86_ops->handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008467 return r;
8468
8469cancel_injection:
8470 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008471 if (unlikely(vcpu->arch.apic_attention))
8472 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008473out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008474 return r;
8475}
8476
Paolo Bonzini362c6982015-02-06 12:48:04 +01008477static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8478{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008479 if (!kvm_arch_vcpu_runnable(vcpu) &&
8480 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008481 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8482 kvm_vcpu_block(vcpu);
8483 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008484
8485 if (kvm_x86_ops->post_block)
8486 kvm_x86_ops->post_block(vcpu);
8487
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008488 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8489 return 1;
8490 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008491
Paolo Bonzini362c6982015-02-06 12:48:04 +01008492 kvm_apic_accept_events(vcpu);
8493 switch(vcpu->arch.mp_state) {
8494 case KVM_MP_STATE_HALTED:
8495 vcpu->arch.pv.pv_unhalted = false;
8496 vcpu->arch.mp_state =
8497 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008498 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008499 case KVM_MP_STATE_RUNNABLE:
8500 vcpu->arch.apf.halted = false;
8501 break;
8502 case KVM_MP_STATE_INIT_RECEIVED:
8503 break;
8504 default:
8505 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008506 }
8507 return 1;
8508}
8509
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008510static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8511{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008512 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008513 kvm_x86_ops->check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008514
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008515 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8516 !vcpu->arch.apf.halted);
8517}
8518
Paolo Bonzini362c6982015-02-06 12:48:04 +01008519static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008520{
8521 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008522 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008523
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008524 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008525 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008526
Paolo Bonzini362c6982015-02-06 12:48:04 +01008527 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008528 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008529 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008530 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008531 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008532 }
8533
Gleb Natapov09cec752009-03-23 15:11:44 +02008534 if (r <= 0)
8535 break;
8536
Radim Krčmář72875d82017-04-26 22:32:19 +02008537 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008538 if (kvm_cpu_has_pending_timer(vcpu))
8539 kvm_inject_pending_timer_irqs(vcpu);
8540
Matt Gingell782d4222015-11-16 15:26:00 -08008541 if (dm_request_for_irq_injection(vcpu) &&
8542 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008543 r = 0;
8544 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008545 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008546 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008547 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008548
8549 kvm_check_async_pf_completion(vcpu);
8550
Gleb Natapov09cec752009-03-23 15:11:44 +02008551 if (signal_pending(current)) {
8552 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008553 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008554 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008555 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008556 }
8557 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008558 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008559 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008560 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008561 }
8562 }
8563
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008564 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008565
8566 return r;
8567}
8568
Gleb Natapov716d51a2012-09-03 15:24:26 +03008569static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8570{
8571 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008572
Gleb Natapov716d51a2012-09-03 15:24:26 +03008573 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008574 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008575 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008576 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008577}
8578
8579static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8580{
8581 BUG_ON(!vcpu->arch.pio.count);
8582
8583 return complete_emulated_io(vcpu);
8584}
8585
Avi Kivityf78146b2012-04-18 19:22:47 +03008586/*
8587 * Implements the following, as a state machine:
8588 *
8589 * read:
8590 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008591 * for each mmio piece in the fragment
8592 * write gpa, len
8593 * exit
8594 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008595 * execute insn
8596 *
8597 * write:
8598 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008599 * for each mmio piece in the fragment
8600 * write gpa, len
8601 * copy data
8602 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008603 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008604static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008605{
8606 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008607 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008608 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008609
Gleb Natapov716d51a2012-09-03 15:24:26 +03008610 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008611
Gleb Natapov716d51a2012-09-03 15:24:26 +03008612 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008613 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8614 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008615 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008616 memcpy(frag->data, run->mmio.data, len);
8617
8618 if (frag->len <= 8) {
8619 /* Switch to the next fragment. */
8620 frag++;
8621 vcpu->mmio_cur_fragment++;
8622 } else {
8623 /* Go forward to the next mmio piece. */
8624 frag->data += len;
8625 frag->gpa += len;
8626 frag->len -= len;
8627 }
8628
Andrew Honiga08d3b32014-02-27 19:35:14 +01008629 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008630 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008631
8632 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008633 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008634 return 1;
8635 vcpu->mmio_read_completed = 1;
8636 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008637 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008638
Gleb Natapov716d51a2012-09-03 15:24:26 +03008639 run->exit_reason = KVM_EXIT_MMIO;
8640 run->mmio.phys_addr = frag->gpa;
8641 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008642 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8643 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008644 run->mmio.is_write = vcpu->mmio_is_write;
8645 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8646 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008647}
8648
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008649static void kvm_save_current_fpu(struct fpu *fpu)
8650{
8651 /*
8652 * If the target FPU state is not resident in the CPU registers, just
8653 * memcpy() from current, else save CPU state directly to the target.
8654 */
8655 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8656 memcpy(&fpu->state, &current->thread.fpu.state,
8657 fpu_kernel_xstate_size);
8658 else
8659 copy_fpregs_to_fpstate(fpu);
8660}
8661
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008662/* Swap (qemu) user FPU context for the guest FPU context. */
8663static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8664{
Rik van Riel5f409e22019-04-03 18:41:52 +02008665 fpregs_lock();
8666
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008667 kvm_save_current_fpu(vcpu->arch.user_fpu);
8668
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008669 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008670 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008671 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008672
8673 fpregs_mark_activate();
8674 fpregs_unlock();
8675
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008676 trace_kvm_fpu(1);
8677}
8678
8679/* When vcpu_run ends, restore user space FPU context. */
8680static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8681{
Rik van Riel5f409e22019-04-03 18:41:52 +02008682 fpregs_lock();
8683
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008684 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8685
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008686 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008687
8688 fpregs_mark_activate();
8689 fpregs_unlock();
8690
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008691 ++vcpu->stat.fpu_reload;
8692 trace_kvm_fpu(0);
8693}
8694
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008695int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8696{
8697 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008698
Christoffer Dallaccb7572017-12-04 21:35:25 +01008699 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008700 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008701 kvm_load_guest_fpu(vcpu);
8702
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008703 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008704 if (kvm_run->immediate_exit) {
8705 r = -EINTR;
8706 goto out;
8707 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008708 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008709 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008710 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008711 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008712 if (signal_pending(current)) {
8713 r = -EINTR;
8714 vcpu->run->exit_reason = KVM_EXIT_INTR;
8715 ++vcpu->stat.signal_exits;
8716 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008717 goto out;
8718 }
8719
Ken Hofsass01643c52018-01-31 16:03:36 -08008720 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8721 r = -EINVAL;
8722 goto out;
8723 }
8724
8725 if (vcpu->run->kvm_dirty_regs) {
8726 r = sync_regs(vcpu);
8727 if (r != 0)
8728 goto out;
8729 }
8730
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008731 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008732 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008733 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8734 r = -EINVAL;
8735 goto out;
8736 }
8737 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008738
Gleb Natapov716d51a2012-09-03 15:24:26 +03008739 if (unlikely(vcpu->arch.complete_userspace_io)) {
8740 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8741 vcpu->arch.complete_userspace_io = NULL;
8742 r = cui(vcpu);
8743 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008744 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008745 } else
8746 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008747
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008748 if (kvm_run->immediate_exit)
8749 r = -EINTR;
8750 else
8751 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008752
8753out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008754 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008755 if (vcpu->run->kvm_valid_regs)
8756 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008757 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008758 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008759
Christoffer Dallaccb7572017-12-04 21:35:25 +01008760 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008761 return r;
8762}
8763
Ken Hofsass01643c52018-01-31 16:03:36 -08008764static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008765{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008766 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8767 /*
8768 * We are here if userspace calls get_regs() in the middle of
8769 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008770 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008771 * that usually, but some bad designed PV devices (vmware
8772 * backdoor interface) need this to work
8773 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008774 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008775 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8776 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008777 regs->rax = kvm_rax_read(vcpu);
8778 regs->rbx = kvm_rbx_read(vcpu);
8779 regs->rcx = kvm_rcx_read(vcpu);
8780 regs->rdx = kvm_rdx_read(vcpu);
8781 regs->rsi = kvm_rsi_read(vcpu);
8782 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008783 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008784 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008785#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008786 regs->r8 = kvm_r8_read(vcpu);
8787 regs->r9 = kvm_r9_read(vcpu);
8788 regs->r10 = kvm_r10_read(vcpu);
8789 regs->r11 = kvm_r11_read(vcpu);
8790 regs->r12 = kvm_r12_read(vcpu);
8791 regs->r13 = kvm_r13_read(vcpu);
8792 regs->r14 = kvm_r14_read(vcpu);
8793 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008794#endif
8795
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008796 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008797 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008798}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008799
Ken Hofsass01643c52018-01-31 16:03:36 -08008800int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8801{
8802 vcpu_load(vcpu);
8803 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008804 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008805 return 0;
8806}
8807
Ken Hofsass01643c52018-01-31 16:03:36 -08008808static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008809{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008810 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8811 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8812
Sean Christophersonde3cd112019-04-30 10:36:17 -07008813 kvm_rax_write(vcpu, regs->rax);
8814 kvm_rbx_write(vcpu, regs->rbx);
8815 kvm_rcx_write(vcpu, regs->rcx);
8816 kvm_rdx_write(vcpu, regs->rdx);
8817 kvm_rsi_write(vcpu, regs->rsi);
8818 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008819 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008820 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008821#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008822 kvm_r8_write(vcpu, regs->r8);
8823 kvm_r9_write(vcpu, regs->r9);
8824 kvm_r10_write(vcpu, regs->r10);
8825 kvm_r11_write(vcpu, regs->r11);
8826 kvm_r12_write(vcpu, regs->r12);
8827 kvm_r13_write(vcpu, regs->r13);
8828 kvm_r14_write(vcpu, regs->r14);
8829 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008830#endif
8831
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008832 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008833 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008834
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008835 vcpu->arch.exception.pending = false;
8836
Avi Kivity3842d132010-07-27 12:30:24 +03008837 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008838}
Avi Kivity3842d132010-07-27 12:30:24 +03008839
Ken Hofsass01643c52018-01-31 16:03:36 -08008840int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8841{
8842 vcpu_load(vcpu);
8843 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008844 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008845 return 0;
8846}
8847
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008848void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8849{
8850 struct kvm_segment cs;
8851
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008852 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008853 *db = cs.db;
8854 *l = cs.l;
8855}
8856EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8857
Ken Hofsass01643c52018-01-31 16:03:36 -08008858static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008859{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008860 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008861
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008862 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8863 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8864 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8865 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8866 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8867 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008868
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008869 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8870 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008871
8872 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008873 sregs->idt.limit = dt.size;
8874 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008875 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008876 sregs->gdt.limit = dt.size;
8877 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008878
Avi Kivity4d4ec082009-12-29 18:07:30 +02008879 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008880 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008881 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008882 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008883 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008884 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008885 sregs->apic_base = kvm_get_apic_base(vcpu);
8886
Jordan Borgner0e96f312018-10-28 12:58:28 +00008887 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008888
Liran Alon04140b42018-03-23 03:01:31 +03008889 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008890 set_bit(vcpu->arch.interrupt.nr,
8891 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008892}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008893
Ken Hofsass01643c52018-01-31 16:03:36 -08008894int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8895 struct kvm_sregs *sregs)
8896{
8897 vcpu_load(vcpu);
8898 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008899 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008900 return 0;
8901}
8902
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008903int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8904 struct kvm_mp_state *mp_state)
8905{
Christoffer Dallfd232562017-12-04 21:35:30 +01008906 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008907 if (kvm_mpx_supported())
8908 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008909
Jan Kiszka66450a22013-03-13 12:42:34 +01008910 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308911 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8912 vcpu->arch.pv.pv_unhalted)
8913 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8914 else
8915 mp_state->mp_state = vcpu->arch.mp_state;
8916
Sean Christophersonf958bd22019-12-09 12:19:31 -08008917 if (kvm_mpx_supported())
8918 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008919 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008920 return 0;
8921}
8922
8923int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8924 struct kvm_mp_state *mp_state)
8925{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008926 int ret = -EINVAL;
8927
8928 vcpu_load(vcpu);
8929
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008930 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008931 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008932 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008933
Liran Alon27cbe7d2019-11-11 11:16:40 +02008934 /*
8935 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8936 * INIT state; latched init should be reported using
8937 * KVM_SET_VCPU_EVENTS, so reject it here.
8938 */
8939 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008940 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8941 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008942 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008943
Jan Kiszka66450a22013-03-13 12:42:34 +01008944 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8945 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8946 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8947 } else
8948 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008949 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008950
8951 ret = 0;
8952out:
8953 vcpu_put(vcpu);
8954 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008955}
8956
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008957int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8958 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008959{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008960 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008961 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008962
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008963 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008964
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008965 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008966 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008967 if (ret) {
8968 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8969 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8970 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008971 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008972 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008973
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008974 kvm_rip_write(vcpu, ctxt->eip);
8975 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008976 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008977}
8978EXPORT_SYMBOL_GPL(kvm_task_switch);
8979
Peng Hao3140c152018-04-02 09:15:32 +08008980static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008981{
Tianyu Lan37b95952018-01-16 17:34:07 +08008982 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008983 /*
8984 * When EFER.LME and CR0.PG are set, the processor is in
8985 * 64-bit mode (though maybe in a 32-bit code segment).
8986 * CR4.PAE and EFER.LMA must be set.
8987 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008988 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008989 || !(sregs->efer & EFER_LMA))
8990 return -EINVAL;
8991 } else {
8992 /*
8993 * Not in 64-bit mode: EFER.LMA is clear and the code
8994 * segment cannot be 64-bit.
8995 */
8996 if (sregs->efer & EFER_LMA || sregs->cs.l)
8997 return -EINVAL;
8998 }
8999
Wanpeng Li3ca94192019-09-18 17:50:10 +08009000 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009001}
9002
Ken Hofsass01643c52018-01-31 16:03:36 -08009003static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009004{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009005 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009006 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009007 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009008 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009009 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009010 int ret = -EINVAL;
9011
Lan Tianyuf2981032017-12-14 03:01:52 -05009012 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009013 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009014
Jim Mattsond3802282017-08-10 10:14:13 -07009015 apic_base_msr.data = sregs->apic_base;
9016 apic_base_msr.host_initiated = true;
9017 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009018 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009019
Gleb Natapov89a27f42010-02-16 10:51:48 +02009020 dt.size = sregs->idt.limit;
9021 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009022 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009023 dt.size = sregs->gdt.limit;
9024 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009025 kvm_x86_ops->set_gdt(vcpu, &dt);
9026
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009027 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009028 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009029 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009030 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009031
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009032 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009033
Avi Kivityf6801df2010-01-21 15:31:50 +02009034 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009035 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009036
Avi Kivity4d4ec082009-12-29 18:07:30 +02009037 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009038 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009039 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009040
Avi Kivityfc78f512009-12-07 12:16:48 +02009041 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009042 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9043 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009044 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009045 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009046 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009047
9048 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009049 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009050 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009051 mmu_reset_needed = 1;
9052 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009053 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009054
9055 if (mmu_reset_needed)
9056 kvm_mmu_reset_context(vcpu);
9057
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009058 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009059 pending_vec = find_first_bit(
9060 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9061 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009062 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009063 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009064 }
9065
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009066 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9067 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9068 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9069 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9070 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9071 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009072
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009073 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9074 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009075
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009076 update_cr8_intercept(vcpu);
9077
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009078 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009079 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009080 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009081 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009082 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9083
Avi Kivity3842d132010-07-27 12:30:24 +03009084 kvm_make_request(KVM_REQ_EVENT, vcpu);
9085
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009086 ret = 0;
9087out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009088 return ret;
9089}
9090
9091int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9092 struct kvm_sregs *sregs)
9093{
9094 int ret;
9095
9096 vcpu_load(vcpu);
9097 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009098 vcpu_put(vcpu);
9099 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009100}
9101
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009102int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9103 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009104{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009105 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009106 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009107
Christoffer Dall66b56562017-12-04 21:35:33 +01009108 vcpu_load(vcpu);
9109
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009110 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9111 r = -EBUSY;
9112 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009113 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009114 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9115 kvm_queue_exception(vcpu, DB_VECTOR);
9116 else
9117 kvm_queue_exception(vcpu, BP_VECTOR);
9118 }
9119
Jan Kiszka91586a32009-10-05 13:07:21 +02009120 /*
9121 * Read rflags as long as potentially injected trace flags are still
9122 * filtered out.
9123 */
9124 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009125
9126 vcpu->guest_debug = dbg->control;
9127 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9128 vcpu->guest_debug = 0;
9129
9130 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009131 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9132 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009133 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009134 } else {
9135 for (i = 0; i < KVM_NR_DB_REGS; i++)
9136 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009137 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009138 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009139
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009140 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9141 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9142 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009143
Jan Kiszka91586a32009-10-05 13:07:21 +02009144 /*
9145 * Trigger an rflags update that will inject or remove the trace
9146 * flags.
9147 */
9148 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009149
Paolo Bonzinia96036b2015-11-10 11:55:36 +01009150 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009151
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009152 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009153
Avi Kivity2122ff52010-05-13 11:25:04 +03009154out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009155 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009156 return r;
9157}
9158
9159/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009160 * Translate a guest virtual address to a guest physical address.
9161 */
9162int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9163 struct kvm_translation *tr)
9164{
9165 unsigned long vaddr = tr->linear_address;
9166 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009167 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009168
Christoffer Dall1da5b612017-12-04 21:35:32 +01009169 vcpu_load(vcpu);
9170
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009171 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009172 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009173 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009174 tr->physical_address = gpa;
9175 tr->valid = gpa != UNMAPPED_GVA;
9176 tr->writeable = 1;
9177 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009178
Christoffer Dall1da5b612017-12-04 21:35:32 +01009179 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009180 return 0;
9181}
9182
Hollis Blanchardd0752062007-10-31 17:24:25 -05009183int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9184{
Christoffer Dall13931232017-12-04 21:35:34 +01009185 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009186
Christoffer Dall13931232017-12-04 21:35:34 +01009187 vcpu_load(vcpu);
9188
Marc Orrb666a4b2018-11-06 14:53:56 -08009189 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009190 memcpy(fpu->fpr, fxsave->st_space, 128);
9191 fpu->fcw = fxsave->cwd;
9192 fpu->fsw = fxsave->swd;
9193 fpu->ftwx = fxsave->twd;
9194 fpu->last_opcode = fxsave->fop;
9195 fpu->last_ip = fxsave->rip;
9196 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009197 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009198
Christoffer Dall13931232017-12-04 21:35:34 +01009199 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009200 return 0;
9201}
9202
9203int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9204{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009205 struct fxregs_state *fxsave;
9206
9207 vcpu_load(vcpu);
9208
Marc Orrb666a4b2018-11-06 14:53:56 -08009209 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009210
Hollis Blanchardd0752062007-10-31 17:24:25 -05009211 memcpy(fxsave->st_space, fpu->fpr, 128);
9212 fxsave->cwd = fpu->fcw;
9213 fxsave->swd = fpu->fsw;
9214 fxsave->twd = fpu->ftwx;
9215 fxsave->fop = fpu->last_opcode;
9216 fxsave->rip = fpu->last_ip;
9217 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009218 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009219
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009220 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009221 return 0;
9222}
9223
Ken Hofsass01643c52018-01-31 16:03:36 -08009224static void store_regs(struct kvm_vcpu *vcpu)
9225{
9226 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9227
9228 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9229 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9230
9231 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9232 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9233
9234 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9235 kvm_vcpu_ioctl_x86_get_vcpu_events(
9236 vcpu, &vcpu->run->s.regs.events);
9237}
9238
9239static int sync_regs(struct kvm_vcpu *vcpu)
9240{
9241 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9242 return -EINVAL;
9243
9244 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9245 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9246 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9247 }
9248 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9249 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9250 return -EINVAL;
9251 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9252 }
9253 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9254 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9255 vcpu, &vcpu->run->s.regs.events))
9256 return -EINVAL;
9257 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9258 }
9259
9260 return 0;
9261}
9262
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009263static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009264{
Marc Orrb666a4b2018-11-06 14:53:56 -08009265 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009266 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009267 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009268 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009269
Dexuan Cui2acf9232010-06-10 11:27:12 +08009270 /*
9271 * Ensure guest xcr0 is valid for loading
9272 */
Dave Hansend91cab72015-09-02 16:31:26 -07009273 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009275 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009276}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009277
Sean Christopherson897cc382019-12-18 13:55:09 -08009278int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009279{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009280 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009281 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9282 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009283
Sean Christopherson897cc382019-12-18 13:55:09 -08009284 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009285}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009286
Sean Christophersone529ef62019-12-18 13:55:15 -08009287int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009288{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009289 struct page *page;
9290 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009291
Sean Christopherson95a0d012019-12-18 13:55:23 -08009292 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9293 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9294 else
9295 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9296
9297 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9298
9299 r = kvm_mmu_create(vcpu);
9300 if (r < 0)
9301 return r;
9302
9303 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009304 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9305 if (r < 0)
9306 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009307 if (kvm_apicv_activated(vcpu->kvm))
9308 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009309 } else
9310 static_key_slow_inc(&kvm_no_apic_vcpu);
9311
9312 r = -ENOMEM;
9313
9314 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9315 if (!page)
9316 goto fail_free_lapic;
9317 vcpu->arch.pio_data = page_address(page);
9318
9319 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9320 GFP_KERNEL_ACCOUNT);
9321 if (!vcpu->arch.mce_banks)
9322 goto fail_free_pio_data;
9323 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9324
9325 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9326 GFP_KERNEL_ACCOUNT))
9327 goto fail_free_mce_banks;
9328
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009329 if (!alloc_emulate_ctxt(vcpu))
9330 goto free_wbinvd_dirty_mask;
9331
Sean Christopherson95a0d012019-12-18 13:55:23 -08009332 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9333 GFP_KERNEL_ACCOUNT);
9334 if (!vcpu->arch.user_fpu) {
9335 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009336 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009337 }
9338
9339 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9340 GFP_KERNEL_ACCOUNT);
9341 if (!vcpu->arch.guest_fpu) {
9342 pr_err("kvm: failed to allocate vcpu's fpu\n");
9343 goto free_user_fpu;
9344 }
9345 fx_init(vcpu);
9346
9347 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9348
9349 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9350
9351 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9352
9353 kvm_async_pf_hash_reset(vcpu);
9354 kvm_pmu_init(vcpu);
9355
9356 vcpu->arch.pending_external_vector = -1;
9357 vcpu->arch.preempted_in_kernel = false;
9358
9359 kvm_hv_vcpu_init(vcpu);
9360
9361 r = kvm_x86_ops->vcpu_create(vcpu);
9362 if (r)
9363 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009364
Sean Christopherson0cf91352019-03-07 15:43:02 -08009365 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009366 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009367 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009368 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009369 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009370 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009371 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009372 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009373
9374free_guest_fpu:
9375 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9376free_user_fpu:
9377 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009378free_emulate_ctxt:
9379 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009380free_wbinvd_dirty_mask:
9381 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9382fail_free_mce_banks:
9383 kfree(vcpu->arch.mce_banks);
9384fail_free_pio_data:
9385 free_page((unsigned long)vcpu->arch.pio_data);
9386fail_free_lapic:
9387 kvm_free_lapic(vcpu);
9388fail_mmu_destroy:
9389 kvm_mmu_destroy(vcpu);
9390 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009391}
9392
Dominik Dingel31928aa2014-12-04 15:47:07 +01009393void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009394{
Will Auld8fe8ab42012-11-29 12:42:12 -08009395 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009396 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009397
Roman Kagand3457c82017-07-14 17:13:20 +03009398 kvm_hv_vcpu_postcreate(vcpu);
9399
Christoffer Dallec7660c2017-12-04 21:35:23 +01009400 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009401 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009402 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009403 msr.data = 0x0;
9404 msr.index = MSR_IA32_TSC;
9405 msr.host_initiated = true;
9406 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009407 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009408
9409 /* poll control enabled by default */
9410 vcpu->arch.msr_kvm_poll_control = 1;
9411
Christoffer Dallec7660c2017-12-04 21:35:23 +01009412 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009413
Wanpeng Lib34de572020-02-28 11:18:41 +08009414 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9415 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9416 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009417}
9418
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009419void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009420{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009421 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009422 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009423
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009424 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9425
Sean Christopherson50b143e2019-12-18 13:55:07 -08009426 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009427
9428 kvm_x86_ops->vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009429
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009430 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009431 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9432 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9433 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009434
9435 kvm_hv_vcpu_uninit(vcpu);
9436 kvm_pmu_destroy(vcpu);
9437 kfree(vcpu->arch.mce_banks);
9438 kvm_free_lapic(vcpu);
9439 idx = srcu_read_lock(&vcpu->kvm->srcu);
9440 kvm_mmu_destroy(vcpu);
9441 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9442 free_page((unsigned long)vcpu->arch.pio_data);
9443 if (!lapic_in_kernel(vcpu))
9444 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009445}
9446
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009447void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009448{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009449 kvm_lapic_reset(vcpu, init_event);
9450
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009451 vcpu->arch.hflags = 0;
9452
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009453 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009454 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009455 atomic_set(&vcpu->arch.nmi_queued, 0);
9456 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009457 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009458 kvm_clear_interrupt_queue(vcpu);
9459 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009460
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009461 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009462 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009463 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009464 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009465 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009466 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009467
Nadav Amit11190222015-04-02 03:10:38 +03009468 vcpu->arch.cr2 = 0;
9469
Avi Kivity3842d132010-07-27 12:30:24 +03009470 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009471 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009472 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009473
Glauber Costa12f9a482011-02-01 14:16:40 -05009474 kvmclock_reset(vcpu);
9475
Gleb Natapovaf585b92010-10-14 11:22:46 +02009476 kvm_clear_async_pf_completion_queue(vcpu);
9477 kvm_async_pf_hash_reset(vcpu);
9478 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009479
Wanpeng Lia554d202017-10-11 05:10:19 -07009480 if (kvm_mpx_supported()) {
9481 void *mpx_state_buffer;
9482
9483 /*
9484 * To avoid have the INIT path from kvm_apic_has_events() that be
9485 * called with loaded FPU and does not let userspace fix the state.
9486 */
Rik van Rielf775b132017-11-14 16:54:23 -05009487 if (init_event)
9488 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009489 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009490 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009491 if (mpx_state_buffer)
9492 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009493 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009494 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009495 if (mpx_state_buffer)
9496 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009497 if (init_event)
9498 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009499 }
9500
Paolo Bonzini64d60672015-05-07 11:36:11 +02009501 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009502 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009503 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009504
Kyle Hueydb2336a2017-03-20 01:16:28 -07009505 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009506
9507 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009508 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009509
Julian Stecklina66f7b722012-12-05 15:26:19 +01009510 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9511 vcpu->arch.regs_avail = ~0;
9512 vcpu->arch.regs_dirty = ~0;
9513
Wanpeng Lia554d202017-10-11 05:10:19 -07009514 vcpu->arch.ia32_xss = 0;
9515
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009516 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009517}
9518
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009519void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009520{
9521 struct kvm_segment cs;
9522
9523 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9524 cs.selector = vector << 8;
9525 cs.base = vector << 12;
9526 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9527 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009528}
9529
Radim Krčmář13a34e02014-08-28 15:13:03 +02009530int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009531{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009532 struct kvm *kvm;
9533 struct kvm_vcpu *vcpu;
9534 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009535 int ret;
9536 u64 local_tsc;
9537 u64 max_tsc = 0;
9538 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009539
9540 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009541 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009542 if (ret != 0)
9543 return ret;
9544
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009545 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009546 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009547 list_for_each_entry(kvm, &vm_list, vm_list) {
9548 kvm_for_each_vcpu(i, vcpu, kvm) {
9549 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009550 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009551 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9552 backwards_tsc = true;
9553 if (vcpu->arch.last_host_tsc > max_tsc)
9554 max_tsc = vcpu->arch.last_host_tsc;
9555 }
9556 }
9557 }
9558
9559 /*
9560 * Sometimes, even reliable TSCs go backwards. This happens on
9561 * platforms that reset TSC during suspend or hibernate actions, but
9562 * maintain synchronization. We must compensate. Fortunately, we can
9563 * detect that condition here, which happens early in CPU bringup,
9564 * before any KVM threads can be running. Unfortunately, we can't
9565 * bring the TSCs fully up to date with real time, as we aren't yet far
9566 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009567 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009568 * variables that haven't been updated yet.
9569 *
9570 * So we simply find the maximum observed TSC above, then record the
9571 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9572 * the adjustment will be applied. Note that we accumulate
9573 * adjustments, in case multiple suspend cycles happen before some VCPU
9574 * gets a chance to run again. In the event that no KVM threads get a
9575 * chance to run, we will miss the entire elapsed period, as we'll have
9576 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9577 * loose cycle time. This isn't too big a deal, since the loss will be
9578 * uniform across all VCPUs (not to mention the scenario is extremely
9579 * unlikely). It is possible that a second hibernate recovery happens
9580 * much faster than a first, causing the observed TSC here to be
9581 * smaller; this would require additional padding adjustment, which is
9582 * why we set last_host_tsc to the local tsc observed here.
9583 *
9584 * N.B. - this code below runs only on platforms with reliable TSC,
9585 * as that is the only way backwards_tsc is set above. Also note
9586 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9587 * have the same delta_cyc adjustment applied if backwards_tsc
9588 * is detected. Note further, this adjustment is only done once,
9589 * as we reset last_host_tsc on all VCPUs to stop this from being
9590 * called multiple times (one for each physical CPU bringup).
9591 *
Guo Chao4a969982012-06-28 15:17:27 +08009592 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009593 * will be compensated by the logic in vcpu_load, which sets the TSC to
9594 * catchup mode. This will catchup all VCPUs to real time, but cannot
9595 * guarantee that they stay in perfect synchronization.
9596 */
9597 if (backwards_tsc) {
9598 u64 delta_cyc = max_tsc - local_tsc;
9599 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009600 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009601 kvm_for_each_vcpu(i, vcpu, kvm) {
9602 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9603 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009604 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009605 }
9606
9607 /*
9608 * We have to disable TSC offset matching.. if you were
9609 * booting a VM while issuing an S4 host suspend....
9610 * you may have some problem. Solving this issue is
9611 * left as an exercise to the reader.
9612 */
9613 kvm->arch.last_tsc_nsec = 0;
9614 kvm->arch.last_tsc_write = 0;
9615 }
9616
9617 }
9618 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009619}
9620
Radim Krčmář13a34e02014-08-28 15:13:03 +02009621void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009622{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009623 kvm_x86_ops->hardware_disable();
9624 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009625}
9626
Sean Christophersonb9904082020-03-21 13:25:55 -07009627int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009628{
Sean Christophersond008dfd2020-03-21 13:25:56 -07009629 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009630 int r;
9631
Sean Christopherson91661982020-03-02 15:57:06 -08009632 rdmsrl_safe(MSR_EFER, &host_efer);
9633
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009634 if (boot_cpu_has(X86_FEATURE_XSAVES))
9635 rdmsrl(MSR_IA32_XSS, host_xss);
9636
Sean Christophersond008dfd2020-03-21 13:25:56 -07009637 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009638 if (r != 0)
9639 return r;
9640
Sean Christopherson69c6f692020-03-21 13:25:59 -07009641 kvm_x86_ops = ops->runtime_ops;
9642
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009643 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9644 supported_xss = 0;
9645
Sean Christophersonb11306b2019-12-10 14:44:13 -08009646 cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data);
9647
Haozhong Zhang35181e82015-10-20 15:39:03 +08009648 if (kvm_has_tsc_control) {
9649 /*
9650 * Make sure the user can only configure tsc_khz values that
9651 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009652 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009653 * be 1 on all machines.
9654 */
9655 u64 max = min(0x7fffffffULL,
9656 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9657 kvm_max_guest_tsc_khz = max;
9658
Haozhong Zhangad7218832015-10-20 15:39:02 +08009659 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009660 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009661
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009662 kvm_init_msr_list();
9663 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009664}
9665
9666void kvm_arch_hardware_unsetup(void)
9667{
9668 kvm_x86_ops->hardware_unsetup();
9669}
9670
Sean Christophersonb9904082020-03-21 13:25:55 -07009671int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009672{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009673 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -07009674 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009675
9676 WARN_ON(!irqs_disabled());
9677
9678 if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits)
9679 return -EIO;
9680
Sean Christophersond008dfd2020-03-21 13:25:56 -07009681 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009682}
9683
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009684bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9685{
9686 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9687}
9688EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9689
9690bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9691{
9692 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9693}
9694
Gleb Natapov54e98182012-08-05 15:58:32 +03009695struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009696EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009697
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009698void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9699{
Like Xub35e5542019-10-27 18:52:43 +08009700 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9701
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009702 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009703 if (pmu->version && unlikely(pmu->event_count)) {
9704 pmu->need_cleanup = true;
9705 kvm_make_request(KVM_REQ_PMU, vcpu);
9706 }
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009707 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009708}
9709
Sean Christopherson562b6b02020-01-26 16:41:13 -08009710void kvm_arch_free_vm(struct kvm *kvm)
9711{
9712 kfree(kvm->arch.hyperv.hv_pa_pg);
9713 vfree(kvm);
9714}
9715
9716
Carsten Ottee08b9632012-01-04 10:25:20 +01009717int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009718{
Carsten Ottee08b9632012-01-04 10:25:20 +01009719 if (type)
9720 return -EINVAL;
9721
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009722 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009723 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009724 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009725 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009726 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009727 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009728
Sheng Yang5550af42008-10-15 20:15:06 +08009729 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9730 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009731 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9732 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9733 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009734
Jan Kiszka038f8c12011-02-04 10:49:11 +01009735 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009736 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009737 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9738
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009739 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009740 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009741
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009742 kvm->arch.guest_can_read_msr_platform_info = true;
9743
Andrew Jones7e44e442014-02-28 12:52:54 +01009744 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009745 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009746
Roman Kagancbc02362018-02-01 16:48:31 +03009747 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009748 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009749 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009750
Sean Christopherson92735b12019-08-02 15:06:17 -07009751 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009752}
9753
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009754int kvm_arch_post_init_vm(struct kvm *kvm)
9755{
9756 return kvm_mmu_post_init_vm(kvm);
9757}
9758
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009759static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9760{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009761 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009762 kvm_mmu_unload(vcpu);
9763 vcpu_put(vcpu);
9764}
9765
9766static void kvm_free_vcpus(struct kvm *kvm)
9767{
9768 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009769 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009770
9771 /*
9772 * Unpin any mmu pages first.
9773 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009774 kvm_for_each_vcpu(i, vcpu, kvm) {
9775 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009776 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009777 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009778 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009779 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009780
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009781 mutex_lock(&kvm->lock);
9782 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9783 kvm->vcpus[i] = NULL;
9784
9785 atomic_set(&kvm->online_vcpus, 0);
9786 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009787}
9788
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009789void kvm_arch_sync_events(struct kvm *kvm)
9790{
Andrew Jones332967a2014-02-28 12:52:55 +01009791 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009792 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009793 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009794}
9795
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009796int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009797{
9798 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009799 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009800 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009801 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009802
9803 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009804 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9805 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009806
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009807 slot = id_to_memslot(slots, id);
9808 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009809 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009810 return -EEXIST;
9811
9812 /*
9813 * MAP_SHARED to prevent internal slot pages from being moved
9814 * by fork()/COW.
9815 */
9816 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9817 MAP_SHARED | MAP_ANONYMOUS, 0);
9818 if (IS_ERR((void *)hva))
9819 return PTR_ERR((void *)hva);
9820 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009821 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009822 return 0;
9823
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009824 /*
9825 * Stuff a non-canonical value to catch use-after-delete. This
9826 * ends up being 0 on 32-bit KVM, but there's no better
9827 * alternative.
9828 */
9829 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009830 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009831 }
9832
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009833 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009834 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009835
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009836 m.slot = id | (i << 16);
9837 m.flags = 0;
9838 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009839 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009840 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009841 r = __kvm_set_memory_region(kvm, &m);
9842 if (r < 0)
9843 return r;
9844 }
9845
Eric Biggers103c7632018-01-31 17:30:21 -08009846 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009847 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009848
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009849 return 0;
9850}
9851EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9852
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009853void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9854{
9855 kvm_mmu_pre_destroy_vm(kvm);
9856}
9857
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009858void kvm_arch_destroy_vm(struct kvm *kvm)
9859{
Andrew Honig27469d22013-04-18 09:38:14 -07009860 if (current->mm == kvm->mm) {
9861 /*
9862 * Free memory regions allocated on behalf of userspace,
9863 * unless the the memory map has changed due to process exit
9864 * or fd copying.
9865 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009866 mutex_lock(&kvm->slots_lock);
9867 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9868 0, 0);
9869 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9870 0, 0);
9871 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9872 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009873 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009874 if (kvm_x86_ops->vm_destroy)
9875 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009876 kvm_pic_destroy(kvm);
9877 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009878 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009879 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009880 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009881 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009882 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009883 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009884}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009885
Sean Christophersone96c81e2020-02-18 13:07:27 -08009886void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009887{
9888 int i;
9889
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009890 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009891 kvfree(slot->arch.rmap[i]);
9892 slot->arch.rmap[i] = NULL;
9893
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009894 if (i == 0)
9895 continue;
9896
Sean Christophersone96c81e2020-02-18 13:07:27 -08009897 kvfree(slot->arch.lpage_info[i - 1]);
9898 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009899 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009900
Sean Christophersone96c81e2020-02-18 13:07:27 -08009901 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009902}
9903
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009904static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9905 unsigned long npages)
9906{
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009907 int i;
9908
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009909 /*
9910 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9911 * old arrays will be freed by __kvm_set_memory_region() if installing
9912 * the new memslot is successful.
9913 */
9914 memset(&slot->arch, 0, sizeof(slot->arch));
9915
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009916 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009917 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009918 unsigned long ugfn;
9919 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009920 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009921
9922 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9923 slot->base_gfn, level) + 1;
9924
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009925 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009926 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009927 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009928 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009929 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009930 if (i == 0)
9931 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009932
Ben Gardon254272c2019-02-11 11:02:50 -08009933 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009934 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009935 goto out_free;
9936
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009937 slot->arch.lpage_info[i - 1] = linfo;
9938
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009939 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009940 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009941 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009942 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009943 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9944 /*
9945 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -08009946 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009947 */
Sean Christopherson600087b2020-03-02 15:57:05 -08009948 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009949 unsigned long j;
9950
9951 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009952 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009953 }
9954 }
9955
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009956 if (kvm_page_track_create_memslot(slot, npages))
9957 goto out_free;
9958
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009959 return 0;
9960
9961out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009962 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009963 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009964 slot->arch.rmap[i] = NULL;
9965 if (i == 0)
9966 continue;
9967
Thomas Huth548ef282015-02-24 21:29:25 +01009968 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009969 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009970 }
9971 return -ENOMEM;
9972}
9973
Sean Christopherson15248252019-02-05 12:54:17 -08009974void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009975{
Boris Ostrovsky91724812019-12-05 01:30:51 +00009976 struct kvm_vcpu *vcpu;
9977 int i;
9978
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009979 /*
9980 * memslots->generation has been incremented.
9981 * mmio generation may have reached its maximum value.
9982 */
Sean Christopherson15248252019-02-05 12:54:17 -08009983 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +00009984
9985 /* Force re-initialization of steal_time cache */
9986 kvm_for_each_vcpu(i, vcpu, kvm)
9987 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009988}
9989
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009990int kvm_arch_prepare_memory_region(struct kvm *kvm,
9991 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009992 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009993 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009994{
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009995 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
9996 return kvm_alloc_memslot_metadata(memslot,
9997 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009998 return 0;
9999}
10000
Kai Huang88178fd2015-01-28 10:54:27 +080010001static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
10002 struct kvm_memory_slot *new)
10003{
10004 /* Still write protect RO slot */
10005 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010006 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +080010007 return;
10008 }
10009
10010 /*
10011 * Call kvm_x86_ops dirty logging hooks when they are valid.
10012 *
10013 * kvm_x86_ops->slot_disable_log_dirty is called when:
10014 *
10015 * - KVM_MR_CREATE with dirty logging is disabled
10016 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
10017 *
10018 * The reason is, in case of PML, we need to set D-bit for any slots
10019 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010020 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010021 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010022 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010023 * logging disabled for memory slots.
10024 *
10025 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
10026 * to dirty logging mode.
10027 *
10028 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10029 *
10030 * In case of write protect:
10031 *
10032 * Write protect all pages for dirty logging.
10033 *
10034 * All the sptes including the large sptes which point to this
10035 * slot are set to readonly. We can not create any new large
10036 * spte on this slot until the end of the logging.
10037 *
10038 * See the comments in fast_page_fault().
10039 */
10040 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010041 if (kvm_x86_ops->slot_enable_log_dirty) {
Kai Huang88178fd2015-01-28 10:54:27 +080010042 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010043 } else {
10044 int level =
10045 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10046 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10047
10048 /*
10049 * If we're with initial-all-set, we don't need
10050 * to write protect any small page because
10051 * they're reported as dirty already. However
10052 * we still need to write-protect huge pages
10053 * so that the page split can happen lazily on
10054 * the first write to the huge page.
10055 */
10056 kvm_mmu_slot_remove_write_access(kvm, new, level);
10057 }
Kai Huang88178fd2015-01-28 10:54:27 +080010058 } else {
10059 if (kvm_x86_ops->slot_disable_log_dirty)
10060 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
10061 }
10062}
10063
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010064void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010065 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010066 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010067 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010068 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010069{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010070 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010071 kvm_mmu_change_mmu_pages(kvm,
10072 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010073
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010074 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010075 * Dirty logging tracks sptes in 4k granularity, meaning that large
10076 * sptes have to be split. If live migration is successful, the guest
10077 * in the source machine will be destroyed and large sptes will be
10078 * created in the destination. However, if the guest continues to run
10079 * in the source machine (for example if live migration fails), small
10080 * sptes will remain around and cause bad performance.
10081 *
10082 * Scan sptes if dirty logging has been stopped, dropping those
10083 * which can be collapsed into a single large-page spte. Later
10084 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010085 *
10086 * There is no need to do this in any of the following cases:
10087 * CREATE: No dirty mappings will already exist.
10088 * MOVE/DELETE: The old mappings will already have been cleaned up by
10089 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010090 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010091 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010092 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10093 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10094 kvm_mmu_zap_collapsible_sptes(kvm, new);
10095
10096 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010097 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010098 *
Kai Huang88178fd2015-01-28 10:54:27 +080010099 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10100 * been zapped so no dirty logging staff is needed for old slot. For
10101 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10102 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010103 *
10104 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010105 */
Kai Huang88178fd2015-01-28 10:54:27 +080010106 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010107 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010108
10109 /* Free the arrays associated with the old memslot. */
10110 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010111 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010112}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010113
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010114void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010115{
Sean Christopherson7390de12019-02-05 13:01:31 -080010116 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010117}
10118
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010119void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10120 struct kvm_memory_slot *slot)
10121{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010122 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010123}
10124
Liran Alone6c67d82018-09-04 10:56:52 +030010125static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10126{
10127 return (is_guest_mode(vcpu) &&
10128 kvm_x86_ops->guest_apic_has_interrupt &&
10129 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
10130}
10131
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010132static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10133{
10134 if (!list_empty_careful(&vcpu->async_pf.done))
10135 return true;
10136
10137 if (kvm_apic_has_events(vcpu))
10138 return true;
10139
10140 if (vcpu->arch.pv.pv_unhalted)
10141 return true;
10142
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010143 if (vcpu->arch.exception.pending)
10144 return true;
10145
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010146 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10147 (vcpu->arch.nmi_pending &&
10148 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010149 return true;
10150
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010151 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10152 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010153 return true;
10154
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010155 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010156 (kvm_cpu_has_interrupt(vcpu) ||
10157 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010158 return true;
10159
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010160 if (kvm_hv_has_stimer_pending(vcpu))
10161 return true;
10162
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010163 return false;
10164}
10165
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010166int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10167{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010168 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010169}
Zhang Xiantao57361992007-12-17 14:21:40 +080010170
Wanpeng Li17e433b2019-08-05 10:03:19 +080010171bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10172{
10173 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10174 return true;
10175
10176 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10177 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10178 kvm_test_request(KVM_REQ_EVENT, vcpu))
10179 return true;
10180
10181 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
10182 return true;
10183
10184 return false;
10185}
10186
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010187bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10188{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010189 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010190}
10191
Christoffer Dallb6d33832012-03-08 16:44:24 -050010192int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010193{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010194 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010195}
Gleb Natapov78646122009-03-23 12:12:11 +020010196
10197int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10198{
10199 return kvm_x86_ops->interrupt_allowed(vcpu);
10200}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010201
Nadav Amit82b32772014-11-02 11:54:45 +020010202unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10203{
10204 if (is_64_bit_mode(vcpu))
10205 return kvm_rip_read(vcpu);
10206 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10207 kvm_rip_read(vcpu));
10208}
10209EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10210
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010211bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10212{
Nadav Amit82b32772014-11-02 11:54:45 +020010213 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010214}
10215EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10216
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010217unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10218{
10219 unsigned long rflags;
10220
10221 rflags = kvm_x86_ops->get_rflags(vcpu);
10222 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010223 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010224 return rflags;
10225}
10226EXPORT_SYMBOL_GPL(kvm_get_rflags);
10227
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010228static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010229{
10230 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010231 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010232 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010233 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010234}
10235
10236void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10237{
10238 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010239 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010240}
10241EXPORT_SYMBOL_GPL(kvm_set_rflags);
10242
Gleb Natapov56028d02010-10-17 18:13:42 +020010243void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10244{
10245 int r;
10246
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010247 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010248 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010249 return;
10250
10251 r = kvm_mmu_reload(vcpu);
10252 if (unlikely(r))
10253 return;
10254
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010255 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010256 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010257 return;
10258
Sean Christopherson7a026742020-02-06 14:14:34 -080010259 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010260}
10261
Gleb Natapovaf585b92010-10-14 11:22:46 +020010262static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10263{
10264 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10265}
10266
10267static inline u32 kvm_async_pf_next_probe(u32 key)
10268{
10269 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10270}
10271
10272static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10273{
10274 u32 key = kvm_async_pf_hash_fn(gfn);
10275
10276 while (vcpu->arch.apf.gfns[key] != ~0)
10277 key = kvm_async_pf_next_probe(key);
10278
10279 vcpu->arch.apf.gfns[key] = gfn;
10280}
10281
10282static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10283{
10284 int i;
10285 u32 key = kvm_async_pf_hash_fn(gfn);
10286
10287 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010288 (vcpu->arch.apf.gfns[key] != gfn &&
10289 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010290 key = kvm_async_pf_next_probe(key);
10291
10292 return key;
10293}
10294
10295bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10296{
10297 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10298}
10299
10300static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10301{
10302 u32 i, j, k;
10303
10304 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10305 while (true) {
10306 vcpu->arch.apf.gfns[i] = ~0;
10307 do {
10308 j = kvm_async_pf_next_probe(j);
10309 if (vcpu->arch.apf.gfns[j] == ~0)
10310 return;
10311 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10312 /*
10313 * k lies cyclically in ]i,j]
10314 * | i.k.j |
10315 * |....j i.k.| or |.k..j i...|
10316 */
10317 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10318 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10319 i = j;
10320 }
10321}
10322
Gleb Natapov7c907052010-10-14 11:22:53 +020010323static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10324{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010325
10326 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10327 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010328}
10329
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010330static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10331{
10332
10333 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10334 sizeof(u32));
10335}
10336
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010337static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10338{
10339 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10340 return false;
10341
10342 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10343 (vcpu->arch.apf.send_user_only &&
10344 kvm_x86_ops->get_cpl(vcpu) == 0))
10345 return false;
10346
10347 return true;
10348}
10349
10350bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10351{
10352 if (unlikely(!lapic_in_kernel(vcpu) ||
10353 kvm_event_needs_reinjection(vcpu) ||
10354 vcpu->arch.exception.pending))
10355 return false;
10356
10357 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10358 return false;
10359
10360 /*
10361 * If interrupts are off we cannot even use an artificial
10362 * halt state.
10363 */
10364 return kvm_x86_ops->interrupt_allowed(vcpu);
10365}
10366
Gleb Natapovaf585b92010-10-14 11:22:46 +020010367void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10368 struct kvm_async_pf *work)
10369{
Avi Kivity6389ee92010-11-29 16:12:30 +020010370 struct x86_exception fault;
10371
Sean Christopherson736c2912019-12-06 15:57:14 -080010372 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010373 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010374
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010375 if (kvm_can_deliver_async_pf(vcpu) &&
10376 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010377 fault.vector = PF_VECTOR;
10378 fault.error_code_valid = true;
10379 fault.error_code = 0;
10380 fault.nested_page_fault = false;
10381 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010382 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010383 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010384 } else {
10385 /*
10386 * It is not possible to deliver a paravirtualized asynchronous
10387 * page fault, but putting the guest in an artificial halt state
10388 * can be beneficial nevertheless: if an interrupt arrives, we
10389 * can deliver it timely and perhaps the guest will schedule
10390 * another process. When the instruction that triggered a page
10391 * fault is retried, hopefully the page will be ready in the host.
10392 */
10393 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010394 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010395}
10396
10397void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10398 struct kvm_async_pf *work)
10399{
Avi Kivity6389ee92010-11-29 16:12:30 +020010400 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010401 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010402
chai wenf2e10662013-10-14 22:22:33 +080010403 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010404 work->arch.token = ~0; /* broadcast wakeup */
10405 else
10406 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010407 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010408
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010409 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10410 !apf_get_user(vcpu, &val)) {
10411 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10412 vcpu->arch.exception.pending &&
10413 vcpu->arch.exception.nr == PF_VECTOR &&
10414 !apf_put_user(vcpu, 0)) {
10415 vcpu->arch.exception.injected = false;
10416 vcpu->arch.exception.pending = false;
10417 vcpu->arch.exception.nr = 0;
10418 vcpu->arch.exception.has_error_code = false;
10419 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010420 vcpu->arch.exception.has_payload = false;
10421 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010422 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10423 fault.vector = PF_VECTOR;
10424 fault.error_code_valid = true;
10425 fault.error_code = 0;
10426 fault.nested_page_fault = false;
10427 fault.address = work->arch.token;
10428 fault.async_page_fault = true;
10429 kvm_inject_page_fault(vcpu, &fault);
10430 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010431 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010432 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010433 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010434}
10435
10436bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10437{
10438 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10439 return true;
10440 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010441 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010442}
10443
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010444void kvm_arch_start_assignment(struct kvm *kvm)
10445{
10446 atomic_inc(&kvm->arch.assigned_device_count);
10447}
10448EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10449
10450void kvm_arch_end_assignment(struct kvm *kvm)
10451{
10452 atomic_dec(&kvm->arch.assigned_device_count);
10453}
10454EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10455
10456bool kvm_arch_has_assigned_device(struct kvm *kvm)
10457{
10458 return atomic_read(&kvm->arch.assigned_device_count);
10459}
10460EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10461
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010462void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10463{
10464 atomic_inc(&kvm->arch.noncoherent_dma_count);
10465}
10466EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10467
10468void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10469{
10470 atomic_dec(&kvm->arch.noncoherent_dma_count);
10471}
10472EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10473
10474bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10475{
10476 return atomic_read(&kvm->arch.noncoherent_dma_count);
10477}
10478EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10479
Alex Williamson14717e22016-05-05 11:58:35 -060010480bool kvm_arch_has_irq_bypass(void)
10481{
Sean Christopherson92735b12019-08-02 15:06:17 -070010482 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010483}
10484
Feng Wu87276882015-09-18 22:29:40 +080010485int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10486 struct irq_bypass_producer *prod)
10487{
10488 struct kvm_kernel_irqfd *irqfd =
10489 container_of(cons, struct kvm_kernel_irqfd, consumer);
10490
Alex Williamson14717e22016-05-05 11:58:35 -060010491 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010492
Alex Williamson14717e22016-05-05 11:58:35 -060010493 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10494 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010495}
10496
10497void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10498 struct irq_bypass_producer *prod)
10499{
10500 int ret;
10501 struct kvm_kernel_irqfd *irqfd =
10502 container_of(cons, struct kvm_kernel_irqfd, consumer);
10503
Feng Wu87276882015-09-18 22:29:40 +080010504 WARN_ON(irqfd->producer != prod);
10505 irqfd->producer = NULL;
10506
10507 /*
10508 * When producer of consumer is unregistered, we change back to
10509 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010510 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010511 * int this case doesn't want to receive the interrupts.
10512 */
10513 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10514 if (ret)
10515 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10516 " fails: %d\n", irqfd->consumer.token, ret);
10517}
10518
10519int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10520 uint32_t guest_irq, bool set)
10521{
Feng Wu87276882015-09-18 22:29:40 +080010522 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10523}
10524
Feng Wu520040142016-01-25 16:53:33 +080010525bool kvm_vector_hashing_enabled(void)
10526{
10527 return vector_hashing;
10528}
Feng Wu520040142016-01-25 16:53:33 +080010529
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010530bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10531{
10532 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10533}
10534EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10535
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010536u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10537{
10538 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10539
10540 /* The STIBP bit doesn't fault even if it's not advertised */
10541 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10542 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10543 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10544 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10545 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10546 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10547
10548 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10549 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10550 bits &= ~SPEC_CTRL_SSBD;
10551 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10552 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10553 bits &= ~SPEC_CTRL_SSBD;
10554
10555 return bits;
10556}
10557EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010558
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010559EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010560EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010561EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10562EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10563EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10564EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010565EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010566EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010567EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010568EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010569EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010570EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010571EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010572EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010573EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010574EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010575EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010576EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010577EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10578EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010579EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010580EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);