blob: c17e6eb9ad43d84fece0aeb875bfd903e69a252c [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
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700113struct 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
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400575void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
576 unsigned long payload)
Jim Mattsonf10c7292018-10-16 14:29:23 -0700577{
578 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
579}
Paolo Bonzini4d5523c2020-05-05 07:33:20 -0400580EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700581
Jim Mattsonda998b42018-10-16 14:29:22 -0700582static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
583 u32 error_code, unsigned long payload)
584{
585 kvm_multiple_exception(vcpu, nr, true, error_code,
586 true, payload, false);
587}
588
Kyle Huey6affcbe2016-11-29 12:40:40 -0800589int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200590{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100591 if (err)
592 kvm_inject_gp(vcpu, 0);
593 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800594 return kvm_skip_emulated_instruction(vcpu);
595
596 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100597}
598EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200599
Avi Kivity6389ee92010-11-29 16:12:30 +0200600void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200601{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200602 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700603 vcpu->arch.exception.nested_apf =
604 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700605 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700606 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700607 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
608 } else {
609 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
610 fault->address);
611 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200612}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300613EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200614
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200615static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200616{
Avi Kivity6389ee92010-11-29 16:12:30 +0200617 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
618 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200619 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200620 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200621
622 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200623}
624
Sheng Yang3419ffc2008-05-15 09:52:48 +0800625void kvm_inject_nmi(struct kvm_vcpu *vcpu)
626{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300627 atomic_inc(&vcpu->arch.nmi_queued);
628 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800629}
630EXPORT_SYMBOL_GPL(kvm_inject_nmi);
631
Avi Kivity298101d2007-11-25 13:41:11 +0200632void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
633{
Jim Mattson91e86d22018-10-16 14:29:21 -0700634 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200635}
636EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
637
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200638void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
639{
Jim Mattson91e86d22018-10-16 14:29:21 -0700640 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200641}
642EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
643
Carsten Ottea03490e2007-10-29 16:09:35 +0100644/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300645 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
646 * a #GP and return false.
647 */
648bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200649{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700650 if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl)
Avi Kivity0a79b002009-09-01 12:03:25 +0300651 return true;
652 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
653 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100654}
Avi Kivity0a79b002009-09-01 12:03:25 +0300655EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100656
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300657bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
658{
659 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
660 return true;
661
662 kvm_queue_exception(vcpu, UD_VECTOR);
663 return false;
664}
665EXPORT_SYMBOL_GPL(kvm_require_dr);
666
Carsten Ottea03490e2007-10-29 16:09:35 +0100667/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200668 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200669 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200670 * can read from guest physical or from the guest's guest physical memory.
671 */
672int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
673 gfn_t ngfn, void *data, int offset, int len,
674 u32 access)
675{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200676 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200677 gfn_t real_gfn;
678 gpa_t ngpa;
679
680 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200681 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200682 if (real_gfn == UNMAPPED_GVA)
683 return -EFAULT;
684
685 real_gfn = gpa_to_gfn(real_gfn);
686
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200687 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200688}
689EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
690
Fengguang Wu69b00492015-01-19 22:33:39 +0800691static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200692 void *data, int offset, int len, u32 access)
693{
694 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
695 data, offset, len, access);
696}
697
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700698static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
699{
700 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
701 rsvd_bits(1, 2);
702}
703
Joerg Roedelec92fe42010-09-10 17:30:51 +0200704/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700705 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100706 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200707int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100708{
709 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
710 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
711 int i;
712 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200713 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100714
Joerg Roedelff03a072010-09-10 17:30:57 +0200715 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
716 offset * sizeof(u64), sizeof(pdpte),
717 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100718 if (ret < 0) {
719 ret = 0;
720 goto out;
721 }
722 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400723 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700724 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100725 ret = 0;
726 goto out;
727 }
728 }
729 ret = 1;
730
Joerg Roedelff03a072010-09-10 17:30:57 +0200731 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700732 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
733
Carsten Ottea03490e2007-10-29 16:09:35 +0100734out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100735
736 return ret;
737}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100738EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100739
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100740bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200741{
Joerg Roedelff03a072010-09-10 17:30:57 +0200742 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200743 int offset;
744 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200745 int r;
746
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200747 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200748 return false;
749
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700750 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300751 return true;
752
Paolo Bonzinia5121772017-07-24 18:54:38 +0200753 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
754 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200755 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
756 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200757 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800758 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200759
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800760 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200761}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100762EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200763
Avi Kivity49a9b072010-06-10 17:02:14 +0300764int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100765{
Sheng Yangaad82702010-05-12 16:40:42 +0800766 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800767 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800768
Avi Kivityf9a48e62010-01-06 19:10:22 +0200769 cr0 |= X86_CR0_ET;
770
Gleb Natapovab344822010-01-21 15:28:46 +0200771#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300772 if (cr0 & 0xffffffff00000000UL)
773 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200774#endif
775
776 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100777
Gleb Natapov0f122442010-04-28 19:15:31 +0300778 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
779 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100780
Gleb Natapov0f122442010-04-28 19:15:31 +0300781 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
782 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100783
784 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
785#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200786 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100787 int cs_db, cs_l;
788
Gleb Natapov0f122442010-04-28 19:15:31 +0300789 if (!is_pae(vcpu))
790 return 1;
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700791 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300792 if (cs_l)
793 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100794 } else
795#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200796 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200797 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300798 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100799 }
800
Mao, Junjiead756a12012-07-02 01:18:48 +0000801 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
802 return 1;
803
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700804 kvm_x86_ops.set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100805
Lai Jiangshand170c412011-02-21 11:21:30 +0800806 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800807 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800808 kvm_async_pf_hash_reset(vcpu);
809 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800810
Sheng Yangaad82702010-05-12 16:40:42 +0800811 if ((cr0 ^ old_cr0) & update_bits)
812 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800813
Laszlo Ersek879ae182015-11-04 12:54:41 +0100814 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
815 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
816 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800817 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
818
Gleb Natapov0f122442010-04-28 19:15:31 +0300819 return 0;
820}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200821EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100822
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200823void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100824{
Avi Kivity49a9b072010-06-10 17:02:14 +0300825 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100826}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200827EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100828
Aaron Lewis139a12c2019-10-21 16:30:25 -0700829void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300830{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700831 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300832
Aaron Lewis139a12c2019-10-21 16:30:25 -0700833 if (vcpu->arch.xcr0 != host_xcr0)
834 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
835
836 if (vcpu->arch.xsaves_enabled &&
837 vcpu->arch.ia32_xss != host_xss)
838 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
839 }
Babu Moger37486132020-05-12 18:59:06 -0500840
841 if (static_cpu_has(X86_FEATURE_PKU) &&
842 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
843 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
844 vcpu->arch.pkru != vcpu->arch.host_pkru)
845 __write_pkru(vcpu->arch.pkru);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300846}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700847EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
848
849void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
850{
Babu Moger37486132020-05-12 18:59:06 -0500851 if (static_cpu_has(X86_FEATURE_PKU) &&
852 (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
853 (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
854 vcpu->arch.pkru = rdpkru();
855 if (vcpu->arch.pkru != vcpu->arch.host_pkru)
856 __write_pkru(vcpu->arch.host_pkru);
857 }
858
Aaron Lewis139a12c2019-10-21 16:30:25 -0700859 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
860
861 if (vcpu->arch.xcr0 != host_xcr0)
862 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
863
864 if (vcpu->arch.xsaves_enabled &&
865 vcpu->arch.ia32_xss != host_xss)
866 wrmsrl(MSR_IA32_XSS, host_xss);
867 }
868
869}
870EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300871
Fengguang Wu69b00492015-01-19 22:33:39 +0800872static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800873{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000874 u64 xcr0 = xcr;
875 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200876 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800877
878 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
879 if (index != XCR_XFEATURE_ENABLED_MASK)
880 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700881 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800882 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700883 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800884 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200885
886 /*
887 * Do not allow the guest to set bits that we do not support
888 * saving. However, xcr0 bit 0 is always set, even if the
889 * emulated CPU does not support XSAVE (see fx_init).
890 */
Dave Hansend91cab72015-09-02 16:31:26 -0700891 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200892 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800893 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200894
Dave Hansend91cab72015-09-02 16:31:26 -0700895 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
896 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000897 return 1;
898
Dave Hansend91cab72015-09-02 16:31:26 -0700899 if (xcr0 & XFEATURE_MASK_AVX512) {
900 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800901 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700902 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800903 return 1;
904 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800905 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000906
Dave Hansend91cab72015-09-02 16:31:26 -0700907 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000908 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800909 return 0;
910}
911
912int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
913{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700914 if (kvm_x86_ops.get_cpl(vcpu) != 0 ||
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000915 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800916 kvm_inject_gp(vcpu, 0);
917 return 1;
918 }
919 return 0;
920}
921EXPORT_SYMBOL_GPL(kvm_set_xcr);
922
Sean Christopherson345599f2019-12-10 14:44:16 -0800923#define __cr4_reserved_bits(__cpu_has, __c) \
924({ \
925 u64 __reserved_bits = CR4_RESERVED_BITS; \
926 \
927 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
928 __reserved_bits |= X86_CR4_OSXSAVE; \
929 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
930 __reserved_bits |= X86_CR4_SMEP; \
931 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
932 __reserved_bits |= X86_CR4_SMAP; \
933 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
934 __reserved_bits |= X86_CR4_FSGSBASE; \
935 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
936 __reserved_bits |= X86_CR4_PKE; \
937 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
938 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800939 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
940 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800941 __reserved_bits; \
942})
943
Wanpeng Li3ca94192019-09-18 17:50:10 +0800944static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
945{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800946 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800947 return -EINVAL;
948
Sean Christopherson345599f2019-12-10 14:44:16 -0800949 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800950 return -EINVAL;
951
952 return 0;
953}
954
Avi Kivitya83b29c2010-06-10 17:02:15 +0300955int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100956{
Avi Kivityfc78f512009-12-07 12:16:48 +0200957 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800958 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800959 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800960
Wanpeng Li3ca94192019-09-18 17:50:10 +0800961 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200962 return 1;
963
Carsten Ottea03490e2007-10-29 16:09:35 +0100964 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300965 if (!(cr4 & X86_CR4_PAE))
966 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300967 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
968 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200969 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
970 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300971 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100972
Mao, Junjiead756a12012-07-02 01:18:48 +0000973 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200974 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000975 return 1;
976
977 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
978 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
979 return 1;
980 }
981
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700982 if (kvm_x86_ops.set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300983 return 1;
984
Mao, Junjiead756a12012-07-02 01:18:48 +0000985 if (((cr4 ^ old_cr4) & pdptr_bits) ||
986 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800987 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300988
Huaitong Hanb9baba82016-03-22 16:51:21 +0800989 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200990 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800991
Gleb Natapov0f122442010-04-28 19:15:31 +0300992 return 0;
993}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200994EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100995
Avi Kivity23902182010-06-10 17:02:16 +0300996int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100997{
Junaid Shahidade61e22018-06-27 14:59:15 -0700998 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100999#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -07001000 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
1001
Junaid Shahidade61e22018-06-27 14:59:15 -07001002 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -07001003 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
1004 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -07001005 }
Paolo Bonziniac146232014-11-10 13:53:25 +01001006#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001007
Avi Kivity9f8fe502010-12-05 17:30:00 +02001008 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001009 if (!skip_tlb_flush) {
1010 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -07001011 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001012 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001013 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001014 }
1015
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001016 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001017 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001018 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001019 else if (is_pae_paging(vcpu) &&
1020 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001021 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001022
Junaid Shahidade61e22018-06-27 14:59:15 -07001023 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001024 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001025 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001026
Gleb Natapov0f122442010-04-28 19:15:31 +03001027 return 0;
1028}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001029EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001030
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001031int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001032{
Gleb Natapov0f122442010-04-28 19:15:31 +03001033 if (cr8 & CR8_RESERVED_BITS)
1034 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001035 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001036 kvm_lapic_set_tpr(vcpu, cr8);
1037 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001038 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001039 return 0;
1040}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001041EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001042
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001043unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001044{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001045 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001046 return kvm_lapic_get_cr8(vcpu);
1047 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001048 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001049}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001050EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001051
Nadav Amitae561ed2015-04-02 03:10:37 +03001052static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1053{
1054 int i;
1055
1056 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1057 for (i = 0; i < KVM_NR_DB_REGS; i++)
1058 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1059 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1060 }
1061}
1062
Jan Kiszkac8639012012-09-21 05:42:55 +02001063static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1064{
1065 unsigned long dr7;
1066
1067 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1068 dr7 = vcpu->arch.guest_debug_dr7;
1069 else
1070 dr7 = vcpu->arch.dr7;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001071 kvm_x86_ops.set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001072 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1073 if (dr7 & DR7_BP_EN_MASK)
1074 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001075}
1076
Nadav Amit6f43ed02014-07-15 17:37:46 +03001077static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1078{
1079 u64 fixed = DR6_FIXED_1;
1080
Radim Krčmářd6321d42017-08-05 00:12:49 +02001081 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001082 fixed |= DR6_RTM;
1083 return fixed;
1084}
1085
Gleb Natapov338dbc92010-04-28 19:15:32 +03001086static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001087{
Marios Pomonisea740052019-12-11 12:47:52 -08001088 size_t size = ARRAY_SIZE(vcpu->arch.db);
1089
Gleb Natapov020df072010-04-13 10:05:23 +03001090 switch (dr) {
1091 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001092 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001093 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1094 vcpu->arch.eff_db[dr] = val;
1095 break;
1096 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001097 /* fall through */
1098 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001099 if (val & 0xffffffff00000000ULL)
1100 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001101 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001102 break;
1103 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001104 /* fall through */
1105 default: /* 7 */
Krish Sadhukhanb91991b2020-01-15 19:54:32 -05001106 if (!kvm_dr7_valid(val))
Gleb Natapov338dbc92010-04-28 19:15:32 +03001107 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +03001108 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02001109 kvm_update_dr7(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001110 break;
1111 }
1112
1113 return 0;
1114}
Gleb Natapov338dbc92010-04-28 19:15:32 +03001115
1116int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1117{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001118 if (__kvm_set_dr(vcpu, dr, val)) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03001119 kvm_inject_gp(vcpu, 0);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001120 return 1;
1121 }
1122 return 0;
Gleb Natapov338dbc92010-04-28 19:15:32 +03001123}
Gleb Natapov020df072010-04-13 10:05:23 +03001124EXPORT_SYMBOL_GPL(kvm_set_dr);
1125
Nadav Amit16f8a6f2014-10-03 01:10:05 +03001126int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +03001127{
Marios Pomonisea740052019-12-11 12:47:52 -08001128 size_t size = ARRAY_SIZE(vcpu->arch.db);
1129
Gleb Natapov020df072010-04-13 10:05:23 +03001130 switch (dr) {
1131 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001132 *val = vcpu->arch.db[array_index_nospec(dr, size)];
Gleb Natapov020df072010-04-13 10:05:23 +03001133 break;
1134 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001135 /* fall through */
1136 case 6:
Paolo Bonzini5679b802020-05-04 11:28:25 -04001137 *val = vcpu->arch.dr6;
Gleb Natapov020df072010-04-13 10:05:23 +03001138 break;
1139 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001140 /* fall through */
1141 default: /* 7 */
1142 *val = vcpu->arch.dr7;
1143 break;
1144 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001145 return 0;
1146}
Gleb Natapov020df072010-04-13 10:05:23 +03001147EXPORT_SYMBOL_GPL(kvm_get_dr);
1148
Avi Kivity022cd0e2011-11-10 14:57:23 +02001149bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1150{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001151 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001152 u64 data;
1153 int err;
1154
Wei Huangc6702c92015-06-19 13:44:45 +02001155 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001156 if (err)
1157 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001158 kvm_rax_write(vcpu, (u32)data);
1159 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001160 return err;
1161}
1162EXPORT_SYMBOL_GPL(kvm_rdpmc);
1163
Carsten Otte043405e2007-10-10 17:16:19 +02001164/*
1165 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1166 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1167 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001168 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1169 * extract the supported MSRs from the related const lists.
1170 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001171 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001172 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001173 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001174 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001175
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001176static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001177 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001178 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001179#ifdef CONFIG_X86_64
1180 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1181#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001182 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001183 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001184 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001185 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1186 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1187 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1188 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1189 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1190 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001191 MSR_IA32_UMWAIT_CONTROL,
1192
Jim Mattsone2ada662019-08-21 11:20:04 -07001193 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1194 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1195 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1196 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1197 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1198 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1199 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1200 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1201 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1202 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1203 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1204 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1205 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001206 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1207 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1208 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1209 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1210 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1211 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1212 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1213 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1214 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001215};
1216
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001217static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001218static unsigned num_msrs_to_save;
1219
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001220static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001221 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1222 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1223 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1224 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001225 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001226 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1227 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001228 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001229 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001230 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001231 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001232 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001233 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001234 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1235 HV_X64_MSR_TSC_EMULATION_STATUS,
1236
1237 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001238 MSR_KVM_PV_EOI_EN,
1239
Will Auldba904632012-11-29 12:42:50 -08001240 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001241 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001242 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001243 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001244 MSR_IA32_MCG_STATUS,
1245 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001246 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001247 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001248 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001249 MSR_PLATFORM_INFO,
1250 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001251 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001252 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001253 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001254
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001255 /*
1256 * The following list leaves out MSRs whose values are determined
1257 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1258 * We always support the "true" VMX control MSRs, even if the host
1259 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001260 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001261 */
1262 MSR_IA32_VMX_BASIC,
1263 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1264 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1265 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1266 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1267 MSR_IA32_VMX_MISC,
1268 MSR_IA32_VMX_CR0_FIXED0,
1269 MSR_IA32_VMX_CR4_FIXED0,
1270 MSR_IA32_VMX_VMCS_ENUM,
1271 MSR_IA32_VMX_PROCBASED_CTLS2,
1272 MSR_IA32_VMX_EPT_VPID_CAP,
1273 MSR_IA32_VMX_VMFUNC,
1274
Borislav Petkov191c8132019-04-18 18:32:50 +02001275 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001276 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001277};
1278
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001279static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001280static unsigned num_emulated_msrs;
1281
Tom Lendacky801e4592018-02-21 13:39:51 -06001282/*
1283 * List of msr numbers which are used to expose MSR-based features that
1284 * can be used by a hypervisor to validate requested CPU features.
1285 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001286static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001287 MSR_IA32_VMX_BASIC,
1288 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1289 MSR_IA32_VMX_PINBASED_CTLS,
1290 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1291 MSR_IA32_VMX_PROCBASED_CTLS,
1292 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1293 MSR_IA32_VMX_EXIT_CTLS,
1294 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1295 MSR_IA32_VMX_ENTRY_CTLS,
1296 MSR_IA32_VMX_MISC,
1297 MSR_IA32_VMX_CR0_FIXED0,
1298 MSR_IA32_VMX_CR0_FIXED1,
1299 MSR_IA32_VMX_CR4_FIXED0,
1300 MSR_IA32_VMX_CR4_FIXED1,
1301 MSR_IA32_VMX_VMCS_ENUM,
1302 MSR_IA32_VMX_PROCBASED_CTLS2,
1303 MSR_IA32_VMX_EPT_VPID_CAP,
1304 MSR_IA32_VMX_VMFUNC,
1305
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001306 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001307 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001308 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001309};
1310
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001311static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001312static unsigned int num_msr_based_features;
1313
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001314static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001315{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001316 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001317
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001318 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1319 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001320
1321 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001322 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1323 * the nested hypervisor runs with NX huge pages. If it is not,
1324 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1325 * L1 guests, so it need not worry about its own (L2) guests.
1326 */
1327 data |= ARCH_CAP_PSCHANGE_MC_NO;
1328
1329 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001330 * If we're doing cache flushes (either "always" or "cond")
1331 * we will do one whenever the guest does a vmlaunch/vmresume.
1332 * If an outer hypervisor is doing the cache flush for us
1333 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1334 * capability to the guest too, and if EPT is disabled we're not
1335 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1336 * require a nested hypervisor to do a flush of its own.
1337 */
1338 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1339 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1340
Paolo Bonzini0c549142019-08-19 17:24:07 +02001341 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1342 data |= ARCH_CAP_RDCL_NO;
1343 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1344 data |= ARCH_CAP_SSB_NO;
1345 if (!boot_cpu_has_bug(X86_BUG_MDS))
1346 data |= ARCH_CAP_MDS_NO;
1347
Pawan Guptae1d38b62019-10-23 12:23:33 +02001348 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001349 * On TAA affected systems:
1350 * - nothing to do if TSX is disabled on the host.
1351 * - we emulate TSX_CTRL if present on the host.
1352 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001353 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001354 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001355 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001356 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1357 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001358
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001359 return data;
1360}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001361
Wanpeng Li66421c12018-02-28 14:03:30 +08001362static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1363{
1364 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001365 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001366 msr->data = kvm_get_arch_capabilities();
1367 break;
1368 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001369 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001370 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001371 default:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001372 if (kvm_x86_ops.get_msr_feature(msr))
Wanpeng Li66421c12018-02-28 14:03:30 +08001373 return 1;
1374 }
1375 return 0;
1376}
1377
Tom Lendacky801e4592018-02-21 13:39:51 -06001378static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1379{
1380 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001381 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001382
1383 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001384 r = kvm_get_msr_feature(&msr);
1385 if (r)
1386 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001387
1388 *data = msr.data;
1389
1390 return 0;
1391}
1392
Sean Christopherson11988492019-04-02 08:19:15 -07001393static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1394{
1395 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1396 return false;
1397
1398 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1399 return false;
1400
Sean Christopherson0a629562019-04-02 08:19:16 -07001401 if (efer & (EFER_LME | EFER_LMA) &&
1402 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1403 return false;
1404
1405 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1406 return false;
1407
Sean Christopherson11988492019-04-02 08:19:15 -07001408 return true;
1409
1410}
Jan Kiszka384bb782013-04-20 10:52:36 +02001411bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001412{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001413 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001414 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001415
Sean Christopherson11988492019-04-02 08:19:15 -07001416 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001417}
1418EXPORT_SYMBOL_GPL(kvm_valid_efer);
1419
Sean Christopherson11988492019-04-02 08:19:15 -07001420static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001421{
1422 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001423 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001424
Sean Christopherson11988492019-04-02 08:19:15 -07001425 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001426 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001427
Sean Christopherson11988492019-04-02 08:19:15 -07001428 if (!msr_info->host_initiated) {
1429 if (!__kvm_valid_efer(vcpu, efer))
1430 return 1;
1431
1432 if (is_paging(vcpu) &&
1433 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1434 return 1;
1435 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001436
Carsten Otte15c4a642007-10-30 18:44:17 +01001437 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001438 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001439
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001440 kvm_x86_ops.set_efer(vcpu, efer);
Sheng Yanga3d204e2010-05-12 16:40:40 +08001441
Sheng Yangaad82702010-05-12 16:40:42 +08001442 /* Update reserved bits */
1443 if ((efer ^ old_efer) & EFER_NX)
1444 kvm_mmu_reset_context(vcpu);
1445
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001446 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001447}
1448
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001449void kvm_enable_efer_bits(u64 mask)
1450{
1451 efer_reserved_bits &= ~mask;
1452}
1453EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1454
Carsten Otte15c4a642007-10-30 18:44:17 +01001455/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001456 * Write @data into the MSR specified by @index. Select MSR specific fault
1457 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001458 * Returns 0 on success, non-0 otherwise.
1459 * Assumes vcpu_load() was already called.
1460 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001461static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1462 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001463{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001464 struct msr_data msr;
1465
1466 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001467 case MSR_FS_BASE:
1468 case MSR_GS_BASE:
1469 case MSR_KERNEL_GS_BASE:
1470 case MSR_CSTAR:
1471 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001472 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001473 return 1;
1474 break;
1475 case MSR_IA32_SYSENTER_EIP:
1476 case MSR_IA32_SYSENTER_ESP:
1477 /*
1478 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1479 * non-canonical address is written on Intel but not on
1480 * AMD (which ignores the top 32-bits, because it does
1481 * not implement 64-bit SYSENTER).
1482 *
1483 * 64-bit code should hence be able to write a non-canonical
1484 * value on AMD. Making the address canonical ensures that
1485 * vmentry does not fail on Intel after writing a non-canonical
1486 * value, and that something deterministic happens if the guest
1487 * invokes 64-bit SYSENTER.
1488 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001489 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001490 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001491
1492 msr.data = data;
1493 msr.index = index;
1494 msr.host_initiated = host_initiated;
1495
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001496 return kvm_x86_ops.set_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001497}
1498
1499/*
1500 * Read the MSR specified by @index into @data. Select MSR specific fault
1501 * checks are bypassed if @host_initiated is %true.
1502 * Returns 0 on success, non-0 otherwise.
1503 * Assumes vcpu_load() was already called.
1504 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001505int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1506 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001507{
1508 struct msr_data msr;
1509 int ret;
1510
1511 msr.index = index;
1512 msr.host_initiated = host_initiated;
1513
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001514 ret = kvm_x86_ops.get_msr(vcpu, &msr);
Sean Christophersonf20935d2019-09-05 14:22:54 -07001515 if (!ret)
1516 *data = msr.data;
1517 return ret;
1518}
1519
1520int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1521{
1522 return __kvm_get_msr(vcpu, index, data, false);
1523}
1524EXPORT_SYMBOL_GPL(kvm_get_msr);
1525
1526int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1527{
1528 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001529}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001530EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001531
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001532int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1533{
1534 u32 ecx = kvm_rcx_read(vcpu);
1535 u64 data;
1536
1537 if (kvm_get_msr(vcpu, ecx, &data)) {
1538 trace_kvm_msr_read_ex(ecx);
1539 kvm_inject_gp(vcpu, 0);
1540 return 1;
1541 }
1542
1543 trace_kvm_msr_read(ecx, data);
1544
1545 kvm_rax_write(vcpu, data & -1u);
1546 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1547 return kvm_skip_emulated_instruction(vcpu);
1548}
1549EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1550
1551int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1552{
1553 u32 ecx = kvm_rcx_read(vcpu);
1554 u64 data = kvm_read_edx_eax(vcpu);
1555
1556 if (kvm_set_msr(vcpu, ecx, data)) {
1557 trace_kvm_msr_write_ex(ecx, data);
1558 kvm_inject_gp(vcpu, 0);
1559 return 1;
1560 }
1561
1562 trace_kvm_msr_write(ecx, data);
1563 return kvm_skip_emulated_instruction(vcpu);
1564}
1565EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1566
Carsten Otte313a3dc2007-10-11 19:16:52 +02001567/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001568 * The fast path for frequent and performance sensitive wrmsr emulation,
1569 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1570 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1571 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1572 * other cases which must be called after interrupts are enabled on the host.
1573 */
1574static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1575{
Wanpeng Lie1be9ac2020-03-26 10:20:01 +08001576 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1577 return 1;
1578
1579 if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001580 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
Wanpeng Li4064a4c2020-04-02 16:20:26 +08001581 ((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
1582 ((u32)(data >> 32) != X2APIC_BROADCAST)) {
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001583
Wanpeng Lid5361672020-03-26 10:20:02 +08001584 data &= ~(1 << 12);
1585 kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001586 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
Wanpeng Lid5361672020-03-26 10:20:02 +08001587 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
1588 trace_kvm_apic_write(APIC_ICR, (u32)data);
1589 return 0;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001590 }
1591
1592 return 1;
1593}
1594
1595enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1596{
1597 u32 msr = kvm_rcx_read(vcpu);
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001598 u64 data;
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001599 int ret = 0;
1600
1601 switch (msr) {
1602 case APIC_BASE_MSR + (APIC_ICR >> 4):
Wanpeng Li8a1038d2020-03-26 10:20:00 +08001603 data = kvm_read_edx_eax(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001604 ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1605 break;
1606 default:
1607 return EXIT_FASTPATH_NONE;
1608 }
1609
1610 if (!ret) {
1611 trace_kvm_msr_write(msr, data);
1612 return EXIT_FASTPATH_SKIP_EMUL_INS;
1613 }
1614
1615 return EXIT_FASTPATH_NONE;
1616}
1617EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1618
1619/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001620 * Adapt set_msr() to msr_io()'s calling convention
1621 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001622static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1623{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001624 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001625}
1626
Carsten Otte313a3dc2007-10-11 19:16:52 +02001627static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1628{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001629 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001630}
1631
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001632#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001633struct pvclock_clock {
1634 int vclock_mode;
1635 u64 cycle_last;
1636 u64 mask;
1637 u32 mult;
1638 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001639 u64 base_cycles;
1640 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001641};
1642
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001643struct pvclock_gtod_data {
1644 seqcount_t seq;
1645
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001646 struct pvclock_clock clock; /* extract of a clocksource struct */
1647 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001648
Paolo Bonzini917f9472020-01-22 14:32:20 +01001649 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001650 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001651};
1652
1653static struct pvclock_gtod_data pvclock_gtod_data;
1654
1655static void update_pvclock_gtod(struct timekeeper *tk)
1656{
1657 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1658
1659 write_seqcount_begin(&vdata->seq);
1660
1661 /* copy pvclock gtod data */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001662 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001663 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1664 vdata->clock.mask = tk->tkr_mono.mask;
1665 vdata->clock.mult = tk->tkr_mono.mult;
1666 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001667 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1668 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001669
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001670 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001671 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1672 vdata->raw_clock.mask = tk->tkr_raw.mask;
1673 vdata->raw_clock.mult = tk->tkr_raw.mult;
1674 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001675 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1676 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001677
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001678 vdata->wall_time_sec = tk->xtime_sec;
1679
Paolo Bonzini917f9472020-01-22 14:32:20 +01001680 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001681
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001682 write_seqcount_end(&vdata->seq);
1683}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001684
1685static s64 get_kvmclock_base_ns(void)
1686{
1687 /* Count up from boot time, but with the frequency of the raw clock. */
1688 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1689}
1690#else
1691static s64 get_kvmclock_base_ns(void)
1692{
1693 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1694 return ktime_get_boottime_ns();
1695}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001696#endif
1697
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001698void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1699{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001700 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001701 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001702}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001703
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001704static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1705{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001706 int version;
1707 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001708 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001709 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001710
1711 if (!wall_clock)
1712 return;
1713
Avi Kivity9ed3c442010-05-04 15:00:37 +03001714 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1715 if (r)
1716 return;
1717
1718 if (version & 1)
1719 ++version; /* first time write, random junk */
1720
1721 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001722
Nicholas Krause1dab1342015-12-30 13:08:46 -05001723 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1724 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001725
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001726 /*
1727 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001728 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001729 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001730 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001731 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001732
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001733 wc.nsec = do_div(wall_nsec, 1000000000);
1734 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001735 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001736
1737 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1738
1739 version++;
1740 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001741}
1742
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001743static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1744{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001745 do_shl32_div32(dividend, divisor);
1746 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001747}
1748
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001749static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001750 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001751{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001752 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001753 int32_t shift = 0;
1754 uint64_t tps64;
1755 uint32_t tps32;
1756
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001757 tps64 = base_hz;
1758 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001759 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001760 tps64 >>= 1;
1761 shift--;
1762 }
1763
1764 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001765 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1766 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001767 scaled64 >>= 1;
1768 else
1769 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001770 shift++;
1771 }
1772
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001773 *pshift = shift;
1774 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001775}
1776
Marcelo Tosattid8281992012-11-27 23:29:01 -02001777#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001778static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001779#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001780
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001781static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001782static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001783
Zachary Amsdencc578282012-02-03 15:43:50 -02001784static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001785{
Zachary Amsdencc578282012-02-03 15:43:50 -02001786 u64 v = (u64)khz * (1000000 + ppm);
1787 do_div(v, 1000000);
1788 return v;
1789}
1790
Haozhong Zhang381d5852015-10-20 15:39:04 +08001791static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1792{
1793 u64 ratio;
1794
1795 /* Guest TSC same frequency as host TSC? */
1796 if (!scale) {
1797 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1798 return 0;
1799 }
1800
1801 /* TSC scaling supported? */
1802 if (!kvm_has_tsc_control) {
1803 if (user_tsc_khz > tsc_khz) {
1804 vcpu->arch.tsc_catchup = 1;
1805 vcpu->arch.tsc_always_catchup = 1;
1806 return 0;
1807 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001808 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001809 return -1;
1810 }
1811 }
1812
1813 /* TSC scaling required - calculate ratio */
1814 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1815 user_tsc_khz, tsc_khz);
1816
1817 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001818 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1819 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001820 return -1;
1821 }
1822
1823 vcpu->arch.tsc_scaling_ratio = ratio;
1824 return 0;
1825}
1826
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001827static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001828{
1829 u32 thresh_lo, thresh_hi;
1830 int use_scaling = 0;
1831
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001832 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001833 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001834 /* set tsc_scaling_ratio to a safe value */
1835 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001836 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001837 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001838
Zachary Amsdenc2855452010-09-18 14:38:15 -10001839 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001840 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001841 &vcpu->arch.virtual_tsc_shift,
1842 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001843 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001844
1845 /*
1846 * Compute the variation in TSC rate which is acceptable
1847 * within the range of tolerance and decide if the
1848 * rate being applied is within that bounds of the hardware
1849 * rate. If so, no scaling or compensation need be done.
1850 */
1851 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1852 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001853 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1854 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 -02001855 use_scaling = 1;
1856 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001857 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001858}
1859
1860static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1861{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001862 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001863 vcpu->arch.virtual_tsc_mult,
1864 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001865 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001866 return tsc;
1867}
1868
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001869static inline int gtod_is_based_on_tsc(int mode)
1870{
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001871 return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001872}
1873
Fengguang Wu69b00492015-01-19 22:33:39 +08001874static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001875{
1876#ifdef CONFIG_X86_64
1877 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001878 struct kvm_arch *ka = &vcpu->kvm->arch;
1879 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1880
1881 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1882 atomic_read(&vcpu->kvm->online_vcpus));
1883
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001884 /*
1885 * Once the masterclock is enabled, always perform request in
1886 * order to update it.
1887 *
1888 * In order to enable masterclock, the host clocksource must be TSC
1889 * and the vcpus need to have matched TSCs. When that happens,
1890 * perform request to enable masterclock.
1891 */
1892 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001893 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001894 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1895
1896 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1897 atomic_read(&vcpu->kvm->online_vcpus),
1898 ka->use_master_clock, gtod->clock.vclock_mode);
1899#endif
1900}
1901
Will Auldba904632012-11-29 12:42:50 -08001902static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1903{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001904 u64 curr_offset = kvm_x86_ops.read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001905 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1906}
1907
Haozhong Zhang35181e82015-10-20 15:39:03 +08001908/*
1909 * Multiply tsc by a fixed point number represented by ratio.
1910 *
1911 * The most significant 64-N bits (mult) of ratio represent the
1912 * integral part of the fixed point number; the remaining N bits
1913 * (frac) represent the fractional part, ie. ratio represents a fixed
1914 * point number (mult + frac * 2^(-N)).
1915 *
1916 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1917 */
1918static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1919{
1920 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1921}
1922
1923u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1924{
1925 u64 _tsc = tsc;
1926 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1927
1928 if (ratio != kvm_default_tsc_scaling_ratio)
1929 _tsc = __scale_tsc(ratio, tsc);
1930
1931 return _tsc;
1932}
1933EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1934
Haozhong Zhang07c14192015-10-20 15:39:05 +08001935static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1936{
1937 u64 tsc;
1938
1939 tsc = kvm_scale_tsc(vcpu, rdtsc());
1940
1941 return target_tsc - tsc;
1942}
1943
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001944u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1945{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001946 u64 tsc_offset = kvm_x86_ops.read_l1_tsc_offset(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001947
1948 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001949}
1950EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1951
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001952static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1953{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07001954 vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001955}
1956
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001957static inline bool kvm_check_tsc_unstable(void)
1958{
1959#ifdef CONFIG_X86_64
1960 /*
1961 * TSC is marked unstable when we're running on Hyper-V,
1962 * 'TSC page' clocksource is good.
1963 */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01001964 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001965 return false;
1966#endif
1967 return check_tsc_unstable();
1968}
1969
Will Auld8fe8ab42012-11-29 12:42:12 -08001970void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001971{
1972 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001973 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001974 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001975 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001976 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001977 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001978 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001979
Jan Kiszka038f8c12011-02-04 10:49:11 +01001980 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001981 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001982 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001983 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001984
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001985 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001986 if (data == 0 && msr->host_initiated) {
1987 /*
1988 * detection of vcpu initialization -- need to sync
1989 * with other vCPUs. This particularly helps to keep
1990 * kvm_clock stable after CPU hotplug
1991 */
1992 synchronizing = true;
1993 } else {
1994 u64 tsc_exp = kvm->arch.last_tsc_write +
1995 nsec_to_cycles(vcpu, elapsed);
1996 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1997 /*
1998 * Special case: TSC write with a small delta (1 second)
1999 * of virtual cycle time against real time is
2000 * interpreted as an attempt to synchronize the CPU.
2001 */
2002 synchronizing = data < tsc_exp + tsc_hz &&
2003 data + tsc_hz > tsc_exp;
2004 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002005 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002006
2007 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002008 * For a reliable TSC, we can match TSC offsets, and for an unstable
2009 * TSC, we add elapsed time in this computation. We could let the
2010 * compensation code attempt to catch up if we fall behind, but
2011 * it's better to try to match offsets from the beginning.
2012 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002013 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002014 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002015 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002016 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002017 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002018 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002019 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002020 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002021 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002022 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002023 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002024 } else {
2025 /*
2026 * We split periods of matched TSC writes into generations.
2027 * For each generation, we track the original measured
2028 * nanosecond time, offset, and write, so if TSCs are in
2029 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002030 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002031 *
2032 * These values are tracked in kvm->arch.cur_xxx variables.
2033 */
2034 kvm->arch.cur_tsc_generation++;
2035 kvm->arch.cur_tsc_nsec = ns;
2036 kvm->arch.cur_tsc_write = data;
2037 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002038 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002039 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002040
2041 /*
2042 * We also track th most recent recorded KHZ, write and time to
2043 * allow the matching interval to be extended at each write.
2044 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002045 kvm->arch.last_tsc_nsec = ns;
2046 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002047 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002048
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002049 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002050
2051 /* Keep track of which generation this VCPU has synchronized to */
2052 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2053 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2054 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2055
Radim Krčmářd6321d42017-08-05 00:12:49 +02002056 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002057 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002058
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002059 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002060 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002061
2062 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002063 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002064 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002065 } else if (!already_matched) {
2066 kvm->arch.nr_vcpus_matched_tsc++;
2067 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002068
2069 kvm_track_tsc_matching(vcpu);
2070 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002071}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002072
Zachary Amsden99e3e302010-08-19 22:07:17 -10002073EXPORT_SYMBOL_GPL(kvm_write_tsc);
2074
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002075static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2076 s64 adjustment)
2077{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07002078 u64 tsc_offset = kvm_x86_ops.read_l1_tsc_offset(vcpu);
Leonid Shatz326e7422018-11-06 12:14:25 +02002079 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002080}
2081
2082static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2083{
2084 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2085 WARN_ON(adjustment < 0);
2086 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002087 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002088}
2089
Marcelo Tosattid8281992012-11-27 23:29:01 -02002090#ifdef CONFIG_X86_64
2091
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002092static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002093{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002094 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002095 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002096
2097 if (likely(ret >= last))
2098 return ret;
2099
2100 /*
2101 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002102 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002103 * very likely) and there's a data dependence, so force GCC
2104 * to generate a branch instead. I don't barrier() because
2105 * we don't actually need a barrier, and if this function
2106 * ever gets inlined it will generate worse code.
2107 */
2108 asm volatile ("");
2109 return last;
2110}
2111
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002112static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2113 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002114{
2115 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002116 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002117
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002118 switch (clock->vclock_mode) {
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002119 case VDSO_CLOCKMODE_HVCLOCK:
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002120 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2121 tsc_timestamp);
2122 if (tsc_pg_val != U64_MAX) {
2123 /* TSC page valid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002124 *mode = VDSO_CLOCKMODE_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002125 v = (tsc_pg_val - clock->cycle_last) &
2126 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002127 } else {
2128 /* TSC page invalid */
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002129 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002130 }
2131 break;
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002132 case VDSO_CLOCKMODE_TSC:
2133 *mode = VDSO_CLOCKMODE_TSC;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002134 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002135 v = (*tsc_timestamp - clock->cycle_last) &
2136 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002137 break;
2138 default:
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002139 *mode = VDSO_CLOCKMODE_NONE;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002140 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002141
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +01002142 if (*mode == VDSO_CLOCKMODE_NONE)
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002143 *tsc_timestamp = v = 0;
2144
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002145 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002146}
2147
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002148static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002149{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002150 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002151 unsigned long seq;
2152 int mode;
2153 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002154
Marcelo Tosattid8281992012-11-27 23:29:01 -02002155 do {
2156 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002157 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002158 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002159 ns >>= gtod->raw_clock.shift;
2160 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002161 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002162 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002163
2164 return mode;
2165}
2166
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002167static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002168{
2169 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2170 unsigned long seq;
2171 int mode;
2172 u64 ns;
2173
2174 do {
2175 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002176 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002177 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002178 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002179 ns >>= gtod->clock.shift;
2180 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2181
2182 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2183 ts->tv_nsec = ns;
2184
2185 return mode;
2186}
2187
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002188/* returns true if host is using TSC based clocksource */
2189static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002190{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002191 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002192 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002193 return false;
2194
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002195 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002196 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002197}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002198
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002199/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002200static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002201 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002202{
2203 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002204 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002205 return false;
2206
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002207 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002208}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002209#endif
2210
2211/*
2212 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002213 * Assuming a stable TSC across physical CPUS, and a stable TSC
2214 * across virtual CPUs, the following condition is possible.
2215 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002216 * CPUs at the next numbered event.
2217 *
2218 * "timespecX" represents host monotonic time. "tscX" represents
2219 * RDTSC value.
2220 *
2221 * VCPU0 on CPU0 | VCPU1 on CPU1
2222 *
2223 * 1. read timespec0,tsc0
2224 * 2. | timespec1 = timespec0 + N
2225 * | tsc1 = tsc0 + M
2226 * 3. transition to guest | transition to guest
2227 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2228 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2229 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2230 *
2231 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2232 *
2233 * - ret0 < ret1
2234 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2235 * ...
2236 * - 0 < N - M => M < N
2237 *
2238 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2239 * always the case (the difference between two distinct xtime instances
2240 * might be smaller then the difference between corresponding TSC reads,
2241 * when updating guest vcpus pvclock areas).
2242 *
2243 * To avoid that problem, do not allow visibility of distinct
2244 * system_timestamp/tsc_timestamp values simultaneously: use a master
2245 * copy of host monotonic time values. Update that master copy
2246 * in lockstep.
2247 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002248 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002249 *
2250 */
2251
2252static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2253{
2254#ifdef CONFIG_X86_64
2255 struct kvm_arch *ka = &kvm->arch;
2256 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002257 bool host_tsc_clocksource, vcpus_matched;
2258
2259 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2260 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002261
2262 /*
2263 * If the host uses TSC clock, then passthrough TSC as stable
2264 * to the guest.
2265 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002266 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002267 &ka->master_kernel_ns,
2268 &ka->master_cycle_now);
2269
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002270 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002271 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002272 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002273
Marcelo Tosattid8281992012-11-27 23:29:01 -02002274 if (ka->use_master_clock)
2275 atomic_set(&kvm_guest_has_master_clock, 1);
2276
2277 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002278 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2279 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002280#endif
2281}
2282
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002283void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2284{
2285 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2286}
2287
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002288static void kvm_gen_update_masterclock(struct kvm *kvm)
2289{
2290#ifdef CONFIG_X86_64
2291 int i;
2292 struct kvm_vcpu *vcpu;
2293 struct kvm_arch *ka = &kvm->arch;
2294
2295 spin_lock(&ka->pvclock_gtod_sync_lock);
2296 kvm_make_mclock_inprogress_request(kvm);
2297 /* no guest entries from this point */
2298 pvclock_update_vm_gtod_copy(kvm);
2299
2300 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002301 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002302
2303 /* guest entries allowed */
2304 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002305 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002306
2307 spin_unlock(&ka->pvclock_gtod_sync_lock);
2308#endif
2309}
2310
Marcelo Tosattie891a322017-04-17 12:51:37 -03002311u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002312{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002313 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002314 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002315 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002316
Paolo Bonzini8b953442016-11-16 18:31:30 +01002317 spin_lock(&ka->pvclock_gtod_sync_lock);
2318 if (!ka->use_master_clock) {
2319 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002320 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002321 }
2322
Paolo Bonzini8b953442016-11-16 18:31:30 +01002323 hv_clock.tsc_timestamp = ka->master_cycle_now;
2324 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2325 spin_unlock(&ka->pvclock_gtod_sync_lock);
2326
Wanpeng Lie2c22062017-05-11 18:12:05 -07002327 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2328 get_cpu();
2329
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002330 if (__this_cpu_read(cpu_tsc_khz)) {
2331 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2332 &hv_clock.tsc_shift,
2333 &hv_clock.tsc_to_system_mul);
2334 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2335 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002336 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002337
2338 put_cpu();
2339
2340 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002341}
2342
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002343static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2344{
2345 struct kvm_vcpu_arch *vcpu = &v->arch;
2346 struct pvclock_vcpu_time_info guest_hv_clock;
2347
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002348 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002349 &guest_hv_clock, sizeof(guest_hv_clock))))
2350 return;
2351
2352 /* This VCPU is paused, but it's legal for a guest to read another
2353 * VCPU's kvmclock, so we really have to follow the specification where
2354 * it says that version is odd if data is being modified, and even after
2355 * it is consistent.
2356 *
2357 * Version field updates must be kept separate. This is because
2358 * kvm_write_guest_cached might use a "rep movs" instruction, and
2359 * writes within a string instruction are weakly ordered. So there
2360 * are three writes overall.
2361 *
2362 * As a small optimization, only write the version field in the first
2363 * and third write. The vcpu->pv_time cache is still valid, because the
2364 * version field is the first in the struct.
2365 */
2366 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2367
Liran Alon51c4b8b2017-11-05 16:11:30 +02002368 if (guest_hv_clock.version & 1)
2369 ++guest_hv_clock.version; /* first time write, random junk */
2370
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002371 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002372 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2373 &vcpu->hv_clock,
2374 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002375
2376 smp_wmb();
2377
2378 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2379 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2380
2381 if (vcpu->pvclock_set_guest_stopped_request) {
2382 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2383 vcpu->pvclock_set_guest_stopped_request = false;
2384 }
2385
2386 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2387
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002388 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2389 &vcpu->hv_clock,
2390 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002391
2392 smp_wmb();
2393
2394 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002395 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2396 &vcpu->hv_clock,
2397 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002398}
2399
Zachary Amsden34c238a2010-09-18 14:38:14 -10002400static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002401{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002402 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002403 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002404 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002405 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002406 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002407 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002408 bool use_master_clock;
2409
2410 kernel_ns = 0;
2411 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002412
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002413 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002414 * If the host uses TSC clock, then passthrough TSC as stable
2415 * to the guest.
2416 */
2417 spin_lock(&ka->pvclock_gtod_sync_lock);
2418 use_master_clock = ka->use_master_clock;
2419 if (use_master_clock) {
2420 host_tsc = ka->master_cycle_now;
2421 kernel_ns = ka->master_kernel_ns;
2422 }
2423 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002424
2425 /* Keep irq disabled to prevent changes to the clock */
2426 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002427 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2428 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002429 local_irq_restore(flags);
2430 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2431 return 1;
2432 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002433 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002434 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002435 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002436 }
2437
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002438 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002439
2440 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002441 * We may have to catch up the TSC to match elapsed wall clock
2442 * time for two reasons, even if kvmclock is used.
2443 * 1) CPU could have been running below the maximum TSC rate
2444 * 2) Broken TSC compensation resets the base at each VCPU
2445 * entry to avoid unknown leaps of TSC even when running
2446 * again on the same CPU. This may cause apparent elapsed
2447 * time to disappear, and the guest to stand still or run
2448 * very slowly.
2449 */
2450 if (vcpu->tsc_catchup) {
2451 u64 tsc = compute_guest_tsc(v, kernel_ns);
2452 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002453 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002454 tsc_timestamp = tsc;
2455 }
2456 }
2457
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002458 local_irq_restore(flags);
2459
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002460 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002461
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002462 if (kvm_has_tsc_control)
2463 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2464
2465 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002466 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002467 &vcpu->hv_clock.tsc_shift,
2468 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002469 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002470 }
2471
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002472 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002473 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002474 vcpu->last_guest_tsc = tsc_timestamp;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002475
Marcelo Tosattid8281992012-11-27 23:29:01 -02002476 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002477 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002478 if (use_master_clock)
2479 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2480
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002481 vcpu->hv_clock.flags = pvclock_flags;
2482
Paolo Bonzini095cf552016-02-08 12:54:12 +01002483 if (vcpu->pv_time_enabled)
2484 kvm_setup_pvclock_page(v);
2485 if (v == kvm_get_vcpu(v->kvm, 0))
2486 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002487 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002488}
2489
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002490/*
2491 * kvmclock updates which are isolated to a given vcpu, such as
2492 * vcpu->cpu migration, should not allow system_timestamp from
2493 * the rest of the vcpus to remain static. Otherwise ntp frequency
2494 * correction applies to one vcpu's system_timestamp but not
2495 * the others.
2496 *
2497 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002498 * We need to rate-limit these requests though, as they can
2499 * considerably slow guests that have a large number of vcpus.
2500 * The time for a remote vcpu to update its kvmclock is bound
2501 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002502 */
2503
Andrew Jones7e44e442014-02-28 12:52:54 +01002504#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2505
2506static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002507{
2508 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002509 struct delayed_work *dwork = to_delayed_work(work);
2510 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2511 kvmclock_update_work);
2512 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002513 struct kvm_vcpu *vcpu;
2514
2515 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002516 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002517 kvm_vcpu_kick(vcpu);
2518 }
2519}
2520
Andrew Jones7e44e442014-02-28 12:52:54 +01002521static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2522{
2523 struct kvm *kvm = v->kvm;
2524
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002525 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002526 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2527 KVMCLOCK_UPDATE_DELAY);
2528}
2529
Andrew Jones332967a2014-02-28 12:52:55 +01002530#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2531
2532static void kvmclock_sync_fn(struct work_struct *work)
2533{
2534 struct delayed_work *dwork = to_delayed_work(work);
2535 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2536 kvmclock_sync_work);
2537 struct kvm *kvm = container_of(ka, struct kvm, arch);
2538
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002539 if (!kvmclock_periodic_sync)
2540 return;
2541
Andrew Jones332967a2014-02-28 12:52:55 +01002542 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2543 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2544 KVMCLOCK_SYNC_PERIOD);
2545}
2546
Borislav Petkov191c8132019-04-18 18:32:50 +02002547/*
2548 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2549 */
2550static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2551{
2552 /* McStatusWrEn enabled? */
Sean Christopherson23493d02020-03-04 17:34:33 -08002553 if (guest_cpuid_is_amd_or_hygon(vcpu))
Borislav Petkov191c8132019-04-18 18:32:50 +02002554 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2555
2556 return false;
2557}
2558
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002559static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002560{
2561 u64 mcg_cap = vcpu->arch.mcg_cap;
2562 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002563 u32 msr = msr_info->index;
2564 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002565
2566 switch (msr) {
2567 case MSR_IA32_MCG_STATUS:
2568 vcpu->arch.mcg_status = data;
2569 break;
2570 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002571 if (!(mcg_cap & MCG_CTL_P) &&
2572 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002573 return 1;
2574 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002575 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002576 vcpu->arch.mcg_ctl = data;
2577 break;
2578 default:
2579 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002580 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002581 u32 offset = array_index_nospec(
2582 msr - MSR_IA32_MC0_CTL,
2583 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2584
Andre Przywara114be422010-03-24 17:46:42 +01002585 /* only 0 or all 1s can be written to IA32_MCi_CTL
2586 * some Linux kernels though clear bit 10 in bank 4 to
2587 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2588 * this to avoid an uncatched #GP in the guest
2589 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002590 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002591 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002592 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002593
2594 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002595 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002596 (offset & 0x3) == 1 && data != 0) {
2597 if (!can_set_mci_status(vcpu))
2598 return -1;
2599 }
2600
Huang Ying890ca9a2009-05-11 16:48:15 +08002601 vcpu->arch.mce_banks[offset] = data;
2602 break;
2603 }
2604 return 1;
2605 }
2606 return 0;
2607}
2608
Ed Swierkffde22a2009-10-15 15:21:43 -07002609static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2610{
2611 struct kvm *kvm = vcpu->kvm;
2612 int lm = is_long_mode(vcpu);
2613 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2614 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2615 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2616 : kvm->arch.xen_hvm_config.blob_size_32;
2617 u32 page_num = data & ~PAGE_MASK;
2618 u64 page_addr = data & PAGE_MASK;
2619 u8 *page;
2620 int r;
2621
2622 r = -E2BIG;
2623 if (page_num >= blob_size)
2624 goto out;
2625 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002626 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2627 if (IS_ERR(page)) {
2628 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002629 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002630 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002631 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002632 goto out_free;
2633 r = 0;
2634out_free:
2635 kfree(page);
2636out:
2637 return r;
2638}
2639
Gleb Natapov344d9582010-10-14 11:22:50 +02002640static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2641{
2642 gpa_t gpa = data & ~0x3f;
2643
Wanpeng Li52a5c152017-07-13 18:30:42 -07002644 /* Bits 3:5 are reserved, Should be zero */
2645 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002646 return 1;
2647
2648 vcpu->arch.apf.msr_val = data;
2649
2650 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2651 kvm_clear_async_pf_completion_queue(vcpu);
2652 kvm_async_pf_hash_reset(vcpu);
2653 return 0;
2654 }
2655
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002656 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002657 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002658 return 1;
2659
Gleb Natapov6adba522010-10-14 11:22:55 +02002660 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002661 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002662 kvm_async_pf_wakeup_all(vcpu);
2663 return 0;
2664}
2665
Glauber Costa12f9a482011-02-01 14:16:40 -05002666static void kvmclock_reset(struct kvm_vcpu *vcpu)
2667{
Andy Honig0b794592013-02-20 14:48:10 -08002668 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002669 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002670}
2671
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002672static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2673{
2674 ++vcpu->stat.tlb_flush;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07002675 kvm_x86_ops.tlb_flush(vcpu, invalidate_gpa);
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002676}
2677
Glauber Costac9aaa892011-07-11 15:28:14 -04002678static void record_steal_time(struct kvm_vcpu *vcpu)
2679{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002680 struct kvm_host_map map;
2681 struct kvm_steal_time *st;
2682
Glauber Costac9aaa892011-07-11 15:28:14 -04002683 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2684 return;
2685
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002686 /* -EAGAIN is returned in atomic context so we can just return. */
2687 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2688 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002689 return;
2690
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002691 st = map.hva +
2692 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2693
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002694 /*
2695 * Doing a TLB flush here, on the guest's behalf, can avoid
2696 * expensive IPIs.
2697 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002698 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002699 st->preempted & KVM_VCPU_FLUSH_TLB);
2700 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002701 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002702
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002703 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002704
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002705 if (st->version & 1)
2706 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002707
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002708 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002709
2710 smp_wmb();
2711
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002712 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002713 vcpu->arch.st.last_steal;
2714 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002715
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002716 smp_wmb();
2717
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002718 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002719
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002720 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002721}
2722
Will Auld8fe8ab42012-11-29 12:42:12 -08002723int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002724{
Gleb Natapov57537852012-01-15 14:17:22 +02002725 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002726 u32 msr = msr_info->index;
2727 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002728
Carsten Otte15c4a642007-10-30 18:44:17 +01002729 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002730 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002731 case MSR_IA32_UCODE_WRITE:
2732 case MSR_VM_HSAVE_PA:
2733 case MSR_AMD64_PATCH_LOADER:
2734 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002735 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002736 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002737 break;
2738
Wanpeng Li518e7b92018-02-28 14:03:31 +08002739 case MSR_IA32_UCODE_REV:
2740 if (msr_info->host_initiated)
2741 vcpu->arch.microcode_version = data;
2742 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002743 case MSR_IA32_ARCH_CAPABILITIES:
2744 if (!msr_info->host_initiated)
2745 return 1;
2746 vcpu->arch.arch_capabilities = data;
2747 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002748 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002749 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002750 case MSR_K7_HWCR:
2751 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002752 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002753 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002754
2755 /* Handle McStatusWrEn */
2756 if (data == BIT_ULL(18)) {
2757 vcpu->arch.msr_hwcr = data;
2758 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002759 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2760 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002761 return 1;
2762 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002763 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002764 case MSR_FAM10H_MMIO_CONF_BASE:
2765 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002766 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2767 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002768 return 1;
2769 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002770 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002771 case MSR_IA32_DEBUGCTLMSR:
2772 if (!data) {
2773 /* We support the non-activated case already */
2774 break;
2775 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2776 /* Values other than LBR and BTF are vendor-specific,
2777 thus reserved and should throw a #GP */
2778 return 1;
2779 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002780 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2781 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002782 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002783 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002784 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002785 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002786 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002787 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2788 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002789 case MSR_IA32_TSCDEADLINE:
2790 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2791 break;
Will Auldba904632012-11-29 12:42:50 -08002792 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002793 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002794 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002795 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002796 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002797 }
2798 vcpu->arch.ia32_tsc_adjust_msr = data;
2799 }
2800 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002801 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002802 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2803 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2804 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2805 return 1;
2806 vcpu->arch.ia32_misc_enable_msr = data;
2807 kvm_update_cpuid(vcpu);
2808 } else {
2809 vcpu->arch.ia32_misc_enable_msr = data;
2810 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002811 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002812 case MSR_IA32_SMBASE:
2813 if (!msr_info->host_initiated)
2814 return 1;
2815 vcpu->arch.smbase = data;
2816 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002817 case MSR_IA32_POWER_CTL:
2818 vcpu->arch.msr_ia32_power_ctl = data;
2819 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002820 case MSR_IA32_TSC:
2821 kvm_write_tsc(vcpu, msr_info);
2822 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002823 case MSR_IA32_XSS:
2824 if (!msr_info->host_initiated &&
2825 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2826 return 1;
2827 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002828 * KVM supports exposing PT to the guest, but does not support
2829 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2830 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002831 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01002832 if (data & ~supported_xss)
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002833 return 1;
2834 vcpu->arch.ia32_xss = data;
2835 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002836 case MSR_SMI_COUNT:
2837 if (!msr_info->host_initiated)
2838 return 1;
2839 vcpu->arch.smi_count = data;
2840 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002841 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002842 case MSR_KVM_WALL_CLOCK:
2843 vcpu->kvm->arch.wall_clock = data;
2844 kvm_write_wall_clock(vcpu->kvm, data);
2845 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002846 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002847 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002848 struct kvm_arch *ka = &vcpu->kvm->arch;
2849
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002850 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2851 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2852
2853 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002854 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002855
2856 ka->boot_vcpu_runs_old_kvmclock = tmp;
2857 }
2858
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002859 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002860 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002861
2862 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002863 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002864 if (!(data & 1))
2865 break;
2866
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002867 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002868 &vcpu->arch.pv_time, data & ~1ULL,
2869 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002870 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002871
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002872 break;
2873 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002874 case MSR_KVM_ASYNC_PF_EN:
2875 if (kvm_pv_enable_async_pf(vcpu, data))
2876 return 1;
2877 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002878 case MSR_KVM_STEAL_TIME:
2879
2880 if (unlikely(!sched_info_on()))
2881 return 1;
2882
2883 if (data & KVM_STEAL_RESERVED_MASK)
2884 return 1;
2885
Glauber Costac9aaa892011-07-11 15:28:14 -04002886 vcpu->arch.st.msr_val = data;
2887
2888 if (!(data & KVM_MSR_ENABLED))
2889 break;
2890
Glauber Costac9aaa892011-07-11 15:28:14 -04002891 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2892
2893 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002894 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002895 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002896 return 1;
2897 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002898
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002899 case MSR_KVM_POLL_CONTROL:
2900 /* only enable bit supported */
2901 if (data & (-1ULL << 1))
2902 return 1;
2903
2904 vcpu->arch.msr_kvm_poll_control = data;
2905 break;
2906
Huang Ying890ca9a2009-05-11 16:48:15 +08002907 case MSR_IA32_MCG_CTL:
2908 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002909 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002910 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002911
Wei Huang6912ac32015-06-12 01:34:56 -04002912 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2913 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2914 pr = true; /* fall through */
2915 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2916 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002917 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002918 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002919
2920 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002921 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2922 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002923 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002924 case MSR_K7_CLK_CTL:
2925 /*
2926 * Ignore all writes to this no longer documented MSR.
2927 * Writes are only relevant for old K7 processors,
2928 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002929 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002930 * affected processor models on the command line, hence
2931 * the need to ignore the workaround.
2932 */
2933 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002934 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002935 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2936 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002937 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002938 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2939 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2940 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002941 return kvm_hv_set_msr_common(vcpu, msr, data,
2942 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002943 case MSR_IA32_BBL_CR_CTL3:
2944 /* Drop writes to this legacy MSR -- see rdmsr
2945 * counterpart for further detail.
2946 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002947 if (report_ignored_msrs)
2948 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2949 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002950 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002951 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002952 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002953 return 1;
2954 vcpu->arch.osvw.length = data;
2955 break;
2956 case MSR_AMD64_OSVW_STATUS:
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.status = data;
2960 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002961 case MSR_PLATFORM_INFO:
2962 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002963 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2964 cpuid_fault_enabled(vcpu)))
2965 return 1;
2966 vcpu->arch.msr_platform_info = data;
2967 break;
2968 case MSR_MISC_FEATURES_ENABLES:
2969 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2970 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2971 !supports_cpuid_fault(vcpu)))
2972 return 1;
2973 vcpu->arch.msr_misc_features_enables = data;
2974 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002975 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002976 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2977 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002978 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002979 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002980 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002981 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002982 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002983 return 1;
2984 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002985 if (report_ignored_msrs)
2986 vcpu_unimpl(vcpu,
2987 "ignored wrmsr: 0x%x data 0x%llx\n",
2988 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002989 break;
2990 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002991 }
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2995
Paolo Bonzini44883f02018-07-26 13:01:52 +02002996static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002997{
2998 u64 data;
2999 u64 mcg_cap = vcpu->arch.mcg_cap;
3000 unsigned bank_num = mcg_cap & 0xff;
3001
3002 switch (msr) {
3003 case MSR_IA32_P5_MC_ADDR:
3004 case MSR_IA32_P5_MC_TYPE:
3005 data = 0;
3006 break;
3007 case MSR_IA32_MCG_CAP:
3008 data = vcpu->arch.mcg_cap;
3009 break;
3010 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003011 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003012 return 1;
3013 data = vcpu->arch.mcg_ctl;
3014 break;
3015 case MSR_IA32_MCG_STATUS:
3016 data = vcpu->arch.mcg_status;
3017 break;
3018 default:
3019 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003020 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003021 u32 offset = array_index_nospec(
3022 msr - MSR_IA32_MC0_CTL,
3023 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3024
Huang Ying890ca9a2009-05-11 16:48:15 +08003025 data = vcpu->arch.mce_banks[offset];
3026 break;
3027 }
3028 return 1;
3029 }
3030 *pdata = data;
3031 return 0;
3032}
3033
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003034int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003035{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003036 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003037 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003038 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003039 case MSR_IA32_DEBUGCTLMSR:
3040 case MSR_IA32_LASTBRANCHFROMIP:
3041 case MSR_IA32_LASTBRANCHTOIP:
3042 case MSR_IA32_LASTINTFROMIP:
3043 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303044 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003045 case MSR_K8_TSEG_ADDR:
3046 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003047 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003048 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003049 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003050 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003051 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003052 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003053 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003054 case MSR_F15H_EX_CFG:
Venkatesh Srinivas2ca1a062020-04-16 11:42:54 -07003055 /*
3056 * Intel Sandy Bridge CPUs must support the RAPL (running average power
3057 * limit) MSRs. Just return 0, as we do not want to expose the host
3058 * data here. Do not conditionalize this on CPUID, as KVM does not do
3059 * so for existing CPU-specific MSRs.
3060 */
3061 case MSR_RAPL_POWER_UNIT:
3062 case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */
3063 case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */
3064 case MSR_PKG_ENERGY_STATUS: /* Total package */
3065 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003066 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003067 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003068 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003069 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3070 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3071 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3072 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003073 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003074 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3075 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003076 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003077 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003078 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003079 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003080 case MSR_IA32_ARCH_CAPABILITIES:
3081 if (!msr_info->host_initiated &&
3082 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3083 return 1;
3084 msr_info->data = vcpu->arch.arch_capabilities;
3085 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003086 case MSR_IA32_POWER_CTL:
3087 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3088 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003089 case MSR_IA32_TSC:
3090 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3091 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003092 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003093 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003094 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003095 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003096 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003097 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003098 /*
3099 * MSR_EBC_FREQUENCY_ID
3100 * Conservative value valid for even the basic CPU models.
3101 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3102 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3103 * and 266MHz for model 3, or 4. Set Core Clock
3104 * Frequency to System Bus Frequency Ratio to 1 (bits
3105 * 31:24) even though these are only valid for CPU
3106 * models > 2, however guests may end up dividing or
3107 * multiplying by zero otherwise.
3108 */
3109 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003110 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003111 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003112 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003113 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003114 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003115 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003116 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003117 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003118 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003119 break;
Will Auldba904632012-11-29 12:42:50 -08003120 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003121 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003122 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003123 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003124 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003125 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003126 case MSR_IA32_SMBASE:
3127 if (!msr_info->host_initiated)
3128 return 1;
3129 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003130 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003131 case MSR_SMI_COUNT:
3132 msr_info->data = vcpu->arch.smi_count;
3133 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003134 case MSR_IA32_PERF_STATUS:
3135 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003136 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003137 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003138 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003139 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003140 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003141 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003142 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003143 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003144 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003145 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003146 break;
3147 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003148 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003149 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003150 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003151 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003152 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003153 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003154 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003155 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003156 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003157 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003158 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003159 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003160 case MSR_KVM_POLL_CONTROL:
3161 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3162 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003163 case MSR_IA32_P5_MC_ADDR:
3164 case MSR_IA32_P5_MC_TYPE:
3165 case MSR_IA32_MCG_CAP:
3166 case MSR_IA32_MCG_CTL:
3167 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003168 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003169 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3170 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003171 case MSR_IA32_XSS:
3172 if (!msr_info->host_initiated &&
3173 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3174 return 1;
3175 msr_info->data = vcpu->arch.ia32_xss;
3176 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003177 case MSR_K7_CLK_CTL:
3178 /*
3179 * Provide expected ramp-up count for K7. All other
3180 * are set to zero, indicating minimum divisors for
3181 * every field.
3182 *
3183 * This prevents guest kernels on AMD host with CPU
3184 * type 6, model 8 and higher from exploding due to
3185 * the rdmsr failing.
3186 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003187 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003188 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003189 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003190 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3191 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003192 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003193 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3194 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3195 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003196 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003197 msr_info->index, &msr_info->data,
3198 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003199 case MSR_IA32_BBL_CR_CTL3:
3200 /* This legacy MSR exists but isn't fully documented in current
3201 * silicon. It is however accessed by winxp in very narrow
3202 * scenarios where it sets bit #19, itself documented as
3203 * a "reserved" bit. Best effort attempt to source coherent
3204 * read data here should the balance of the register be
3205 * interpreted by the guest:
3206 *
3207 * L2 cache control register 3: 64GB range, 256KB size,
3208 * enabled, latency 0x1, configured
3209 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003210 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003211 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003212 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003213 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003214 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003215 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003216 break;
3217 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003218 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003219 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003220 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003221 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003222 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003223 if (!msr_info->host_initiated &&
3224 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3225 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003226 msr_info->data = vcpu->arch.msr_platform_info;
3227 break;
3228 case MSR_MISC_FEATURES_ENABLES:
3229 msr_info->data = vcpu->arch.msr_misc_features_enables;
3230 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003231 case MSR_K7_HWCR:
3232 msr_info->data = vcpu->arch.msr_hwcr;
3233 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003234 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003235 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003236 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003237 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003238 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3239 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003240 return 1;
3241 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003242 if (report_ignored_msrs)
3243 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3244 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003245 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003246 }
3247 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003248 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003249 return 0;
3250}
3251EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3252
Carsten Otte313a3dc2007-10-11 19:16:52 +02003253/*
3254 * Read or write a bunch of msrs. All parameters are kernel addresses.
3255 *
3256 * @return number of msrs set successfully.
3257 */
3258static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3259 struct kvm_msr_entry *entries,
3260 int (*do_msr)(struct kvm_vcpu *vcpu,
3261 unsigned index, u64 *data))
3262{
Tom Lendacky801e4592018-02-21 13:39:51 -06003263 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003264
Carsten Otte313a3dc2007-10-11 19:16:52 +02003265 for (i = 0; i < msrs->nmsrs; ++i)
3266 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3267 break;
3268
Carsten Otte313a3dc2007-10-11 19:16:52 +02003269 return i;
3270}
3271
3272/*
3273 * Read or write a bunch of msrs. Parameters are user addresses.
3274 *
3275 * @return number of msrs set successfully.
3276 */
3277static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3278 int (*do_msr)(struct kvm_vcpu *vcpu,
3279 unsigned index, u64 *data),
3280 int writeback)
3281{
3282 struct kvm_msrs msrs;
3283 struct kvm_msr_entry *entries;
3284 int r, n;
3285 unsigned size;
3286
3287 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003288 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003289 goto out;
3290
3291 r = -E2BIG;
3292 if (msrs.nmsrs >= MAX_IO_MSRS)
3293 goto out;
3294
Carsten Otte313a3dc2007-10-11 19:16:52 +02003295 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003296 entries = memdup_user(user_msrs->entries, size);
3297 if (IS_ERR(entries)) {
3298 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003299 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003300 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003301
3302 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3303 if (r < 0)
3304 goto out_free;
3305
3306 r = -EFAULT;
3307 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3308 goto out_free;
3309
3310 r = n;
3311
3312out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003313 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003314out:
3315 return r;
3316}
3317
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003318static inline bool kvm_can_mwait_in_guest(void)
3319{
3320 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003321 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3322 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003323}
3324
Alexander Graf784aa3d2014-07-14 18:27:35 +02003325int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003326{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003327 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003328
3329 switch (ext) {
3330 case KVM_CAP_IRQCHIP:
3331 case KVM_CAP_HLT:
3332 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003333 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003334 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003335 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003336 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003337 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003338 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003339 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003340 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003341 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003342 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003343 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003344 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003345 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003346 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003347 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003348 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003349 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003350 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003351 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003352 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003353 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003354 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003355 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003356 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003357 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003358 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003359 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003360 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003361 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003362 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003363 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003364 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003365 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003366 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003367 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003368 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003369 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003370 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003371 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003372 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003373 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003374 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003375 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003376 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003377 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003378 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003379 case KVM_CAP_EXCEPTION_PAYLOAD:
Peter Xub9b27822020-05-05 11:47:50 -04003380 case KVM_CAP_SET_GUEST_DEBUG:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003381 r = 1;
3382 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003383 case KVM_CAP_SYNC_REGS:
3384 r = KVM_SYNC_X86_VALID_FIELDS;
3385 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003386 case KVM_CAP_ADJUST_CLOCK:
3387 r = KVM_CLOCK_TSC_STABLE;
3388 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003389 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003390 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3391 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003392 if(kvm_can_mwait_in_guest())
3393 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003394 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003395 case KVM_CAP_X86_SMM:
3396 /* SMBASE is usually relocated above 1M on modern chipsets,
3397 * and SMM handlers might indeed rely on 4G segment limits,
3398 * so do not report SMM to be available if real mode is
3399 * emulated via vm86 mode. Still, do not go to great lengths
3400 * to avoid userspace's usage of the feature, because it is a
3401 * fringe case that is not enabled except via specific settings
3402 * of the module parameters.
3403 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003404 r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003405 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003406 case KVM_CAP_VAPIC:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003407 r = !kvm_x86_ops.cpu_has_accelerated_tpr();
Avi Kivity774ead32007-12-26 13:57:04 +02003408 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003409 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003410 r = KVM_SOFT_MAX_VCPUS;
3411 break;
3412 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003413 r = KVM_MAX_VCPUS;
3414 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003415 case KVM_CAP_MAX_VCPU_ID:
3416 r = KVM_MAX_VCPU_ID;
3417 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003418 case KVM_CAP_PV_MMU: /* obsolete */
3419 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003420 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003421 case KVM_CAP_MCE:
3422 r = KVM_MAX_MCE_BANKS;
3423 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003424 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003425 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003426 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003427 case KVM_CAP_TSC_CONTROL:
3428 r = kvm_has_tsc_control;
3429 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003430 case KVM_CAP_X2APIC_API:
3431 r = KVM_X2APIC_API_VALID_FLAGS;
3432 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003433 case KVM_CAP_NESTED_STATE:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003434 r = kvm_x86_ops.get_nested_state ?
3435 kvm_x86_ops.get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003436 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003437 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003438 r = kvm_x86_ops.enable_direct_tlbflush != NULL;
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003439 break;
3440 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003441 r = kvm_x86_ops.nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003442 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003443 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003444 break;
3445 }
3446 return r;
3447
3448}
3449
Carsten Otte043405e2007-10-10 17:16:19 +02003450long kvm_arch_dev_ioctl(struct file *filp,
3451 unsigned int ioctl, unsigned long arg)
3452{
3453 void __user *argp = (void __user *)arg;
3454 long r;
3455
3456 switch (ioctl) {
3457 case KVM_GET_MSR_INDEX_LIST: {
3458 struct kvm_msr_list __user *user_msr_list = argp;
3459 struct kvm_msr_list msr_list;
3460 unsigned n;
3461
3462 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003463 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003464 goto out;
3465 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003466 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003467 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003468 goto out;
3469 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003470 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003471 goto out;
3472 r = -EFAULT;
3473 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3474 num_msrs_to_save * sizeof(u32)))
3475 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003476 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003477 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003478 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003479 goto out;
3480 r = 0;
3481 break;
3482 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003483 case KVM_GET_SUPPORTED_CPUID:
3484 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003485 struct kvm_cpuid2 __user *cpuid_arg = argp;
3486 struct kvm_cpuid2 cpuid;
3487
3488 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003489 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003490 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003491
3492 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3493 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003494 if (r)
3495 goto out;
3496
3497 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003498 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003499 goto out;
3500 r = 0;
3501 break;
3502 }
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003503 case KVM_X86_GET_MCE_CAP_SUPPORTED:
Huang Ying890ca9a2009-05-11 16:48:15 +08003504 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003505 if (copy_to_user(argp, &kvm_mce_cap_supported,
3506 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003507 goto out;
3508 r = 0;
3509 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003510 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3511 struct kvm_msr_list __user *user_msr_list = argp;
3512 struct kvm_msr_list msr_list;
3513 unsigned int n;
3514
3515 r = -EFAULT;
3516 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3517 goto out;
3518 n = msr_list.nmsrs;
3519 msr_list.nmsrs = num_msr_based_features;
3520 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3521 goto out;
3522 r = -E2BIG;
3523 if (n < msr_list.nmsrs)
3524 goto out;
3525 r = -EFAULT;
3526 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3527 num_msr_based_features * sizeof(u32)))
3528 goto out;
3529 r = 0;
3530 break;
3531 }
3532 case KVM_GET_MSRS:
3533 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3534 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003535 default:
3536 r = -EINVAL;
Xiaoyao Licf6c26e2020-02-29 10:52:12 +08003537 break;
Carsten Otte043405e2007-10-10 17:16:19 +02003538 }
3539out:
3540 return r;
3541}
3542
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003543static void wbinvd_ipi(void *garbage)
3544{
3545 wbinvd();
3546}
3547
3548static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3549{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003550 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003551}
3552
Carsten Otte313a3dc2007-10-11 19:16:52 +02003553void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3554{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003555 /* Address WBINVD may be executed by guest */
3556 if (need_emulate_wbinvd(vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003557 if (kvm_x86_ops.has_wbinvd_exit())
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003558 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3559 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3560 smp_call_function_single(vcpu->cpu,
3561 wbinvd_ipi, NULL, 1);
3562 }
3563
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003564 kvm_x86_ops.vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003565
Babu Moger37486132020-05-12 18:59:06 -05003566 /* Save host pkru register if supported */
3567 vcpu->arch.host_pkru = read_pkru();
3568
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003569 /* Apply any externally detected TSC adjustments (due to suspend) */
3570 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3571 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3572 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003573 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003574 }
3575
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003576 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003577 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003578 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003579 if (tsc_delta < 0)
3580 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003581
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003582 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003583 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003584 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003585 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003586 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003587 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003588
3589 if (kvm_lapic_hv_timer_in_use(vcpu))
3590 kvm_lapic_restart_hv_timer(vcpu);
3591
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003592 /*
3593 * On a host with synchronized TSC, there is no need to update
3594 * kvmclock on vcpu->cpu migration
3595 */
3596 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03003597 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003598 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003599 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003600 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003601 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003602
Glauber Costac9aaa892011-07-11 15:28:14 -04003603 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003604}
3605
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003606static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3607{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003608 struct kvm_host_map map;
3609 struct kvm_steal_time *st;
3610
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003611 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3612 return;
3613
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003614 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003615 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003616
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003617 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3618 &vcpu->arch.st.cache, true))
3619 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003620
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003621 st = map.hva +
3622 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3623
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003624 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003625
3626 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003627}
3628
Carsten Otte313a3dc2007-10-11 19:16:52 +02003629void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3630{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003631 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003632
3633 if (vcpu->preempted)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003634 vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu);
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003635
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003636 /*
3637 * Disable page faults because we're in atomic context here.
3638 * kvm_write_guest_offset_cached() would call might_fault()
3639 * that relies on pagefault_disable() to tell if there's a
3640 * bug. NOTE: the write to guest memory may not go through if
3641 * during postcopy live migration or if there's heavy guest
3642 * paging.
3643 */
3644 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003645 /*
3646 * kvm_memslots() will be called by
3647 * kvm_write_guest_offset_cached() so take the srcu lock.
3648 */
3649 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003650 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003651 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003652 pagefault_enable();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003653 kvm_x86_ops.vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003654 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003655 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003656 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3657 * on every vmexit, but if not, we might have a stale dr6 from the
3658 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003659 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003660 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003661}
3662
Carsten Otte313a3dc2007-10-11 19:16:52 +02003663static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3664 struct kvm_lapic_state *s)
3665{
Liran Alonfa59cc02017-12-24 18:12:53 +02003666 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003667 kvm_x86_ops.sync_pir_to_irr(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003668
Radim Krčmářa92e2542016-07-12 22:09:22 +02003669 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003670}
3671
3672static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3673 struct kvm_lapic_state *s)
3674{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003675 int r;
3676
3677 r = kvm_apic_set_state(vcpu, s);
3678 if (r)
3679 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003680 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003681
3682 return 0;
3683}
3684
Matt Gingell127a4572015-11-17 17:32:05 +01003685static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3686{
3687 return (!lapic_in_kernel(vcpu) ||
3688 kvm_apic_accept_pic_intr(vcpu));
3689}
3690
Matt Gingell782d4222015-11-16 15:26:00 -08003691/*
3692 * if userspace requested an interrupt window, check that the
3693 * interrupt window is open.
3694 *
3695 * No need to exit to userspace if we already have an interrupt queued.
3696 */
3697static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3698{
3699 return kvm_arch_interrupt_allowed(vcpu) &&
3700 !kvm_cpu_has_interrupt(vcpu) &&
3701 !kvm_event_needs_reinjection(vcpu) &&
3702 kvm_cpu_accept_dm_intr(vcpu);
3703}
3704
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003705static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3706 struct kvm_interrupt *irq)
3707{
Chen Gang02cdb502013-02-27 11:33:25 +08003708 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003709 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003710
3711 if (!irqchip_in_kernel(vcpu->kvm)) {
3712 kvm_queue_interrupt(vcpu, irq->irq, false);
3713 kvm_make_request(KVM_REQ_EVENT, vcpu);
3714 return 0;
3715 }
3716
3717 /*
3718 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3719 * fail for in-kernel 8259.
3720 */
3721 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003722 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003723
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003724 if (vcpu->arch.pending_external_vector != -1)
3725 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003726
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003727 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003728 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003729 return 0;
3730}
3731
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003732static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3733{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003734 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003735
3736 return 0;
3737}
3738
Paolo Bonzinif0778252015-04-01 15:06:40 +02003739static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3740{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003741 kvm_make_request(KVM_REQ_SMI, vcpu);
3742
Paolo Bonzinif0778252015-04-01 15:06:40 +02003743 return 0;
3744}
3745
Avi Kivityb209749f2007-10-22 16:50:39 +02003746static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3747 struct kvm_tpr_access_ctl *tac)
3748{
3749 if (tac->flags)
3750 return -EINVAL;
3751 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3752 return 0;
3753}
3754
Huang Ying890ca9a2009-05-11 16:48:15 +08003755static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3756 u64 mcg_cap)
3757{
3758 int r;
3759 unsigned bank_num = mcg_cap & 0xff, bank;
3760
3761 r = -EINVAL;
Jim Mattsonc4e0e4ab2020-05-11 15:56:16 -07003762 if (!bank_num || bank_num > KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003763 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003764 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003765 goto out;
3766 r = 0;
3767 vcpu->arch.mcg_cap = mcg_cap;
3768 /* Init IA32_MCG_CTL to all 1s */
3769 if (mcg_cap & MCG_CTL_P)
3770 vcpu->arch.mcg_ctl = ~(u64)0;
3771 /* Init IA32_MCi_CTL to all 1s */
3772 for (bank = 0; bank < bank_num; bank++)
3773 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003774
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003775 kvm_x86_ops.setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003776out:
3777 return r;
3778}
3779
3780static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3781 struct kvm_x86_mce *mce)
3782{
3783 u64 mcg_cap = vcpu->arch.mcg_cap;
3784 unsigned bank_num = mcg_cap & 0xff;
3785 u64 *banks = vcpu->arch.mce_banks;
3786
3787 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3788 return -EINVAL;
3789 /*
3790 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3791 * reporting is disabled
3792 */
3793 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3794 vcpu->arch.mcg_ctl != ~(u64)0)
3795 return 0;
3796 banks += 4 * mce->bank;
3797 /*
3798 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3799 * reporting is disabled for the bank
3800 */
3801 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3802 return 0;
3803 if (mce->status & MCI_STATUS_UC) {
3804 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003805 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003806 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003807 return 0;
3808 }
3809 if (banks[1] & MCI_STATUS_VAL)
3810 mce->status |= MCI_STATUS_OVER;
3811 banks[2] = mce->addr;
3812 banks[3] = mce->misc;
3813 vcpu->arch.mcg_status = mce->mcg_status;
3814 banks[1] = mce->status;
3815 kvm_queue_exception(vcpu, MC_VECTOR);
3816 } else if (!(banks[1] & MCI_STATUS_VAL)
3817 || !(banks[1] & MCI_STATUS_UC)) {
3818 if (banks[1] & MCI_STATUS_VAL)
3819 mce->status |= MCI_STATUS_OVER;
3820 banks[2] = mce->addr;
3821 banks[3] = mce->misc;
3822 banks[1] = mce->status;
3823 } else
3824 banks[1] |= MCI_STATUS_OVER;
3825 return 0;
3826}
3827
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003828static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3829 struct kvm_vcpu_events *events)
3830{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003831 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003832
Wanpeng Li664f8e22017-08-24 03:35:09 -07003833 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003834 * In guest mode, payload delivery should be deferred,
3835 * so that the L1 hypervisor can intercept #PF before
3836 * CR2 is modified (or intercept #DB before DR6 is
3837 * modified under nVMX). Unless the per-VM capability,
3838 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3839 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3840 * opportunistically defer the exception payload, deliver it if the
3841 * capability hasn't been requested before processing a
3842 * KVM_GET_VCPU_EVENTS.
3843 */
3844 if (!vcpu->kvm->arch.exception_payload_enabled &&
3845 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3846 kvm_deliver_exception_payload(vcpu);
3847
3848 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003849 * The API doesn't provide the instruction length for software
3850 * exceptions, so don't report them. As long as the guest RIP
3851 * isn't advanced, we should expect to encounter the exception
3852 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003853 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003854 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3855 events->exception.injected = 0;
3856 events->exception.pending = 0;
3857 } else {
3858 events->exception.injected = vcpu->arch.exception.injected;
3859 events->exception.pending = vcpu->arch.exception.pending;
3860 /*
3861 * For ABI compatibility, deliberately conflate
3862 * pending and injected exceptions when
3863 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3864 */
3865 if (!vcpu->kvm->arch.exception_payload_enabled)
3866 events->exception.injected |=
3867 vcpu->arch.exception.pending;
3868 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003869 events->exception.nr = vcpu->arch.exception.nr;
3870 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3871 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003872 events->exception_has_payload = vcpu->arch.exception.has_payload;
3873 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003874
Jan Kiszka03b82a32010-02-15 10:45:41 +01003875 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003876 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003877 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003878 events->interrupt.soft = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003879 events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003880
3881 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003882 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003883 events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003884 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003885
Jan Kiszka66450a22013-03-13 12:42:34 +01003886 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003887
Paolo Bonzinif0778252015-04-01 15:06:40 +02003888 events->smi.smm = is_smm(vcpu);
3889 events->smi.pending = vcpu->arch.smi_pending;
3890 events->smi.smm_inside_nmi =
3891 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3892 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3893
Jan Kiszkadab4b912009-12-06 18:24:15 +01003894 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003895 | KVM_VCPUEVENT_VALID_SHADOW
3896 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003897 if (vcpu->kvm->arch.exception_payload_enabled)
3898 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3899
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003900 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003901}
3902
Sean Christophersonc5833c72019-04-02 08:03:10 -07003903static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003904
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003905static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3906 struct kvm_vcpu_events *events)
3907{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003908 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003909 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003910 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003911 | KVM_VCPUEVENT_VALID_SMM
3912 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003913 return -EINVAL;
3914
Jim Mattson59073aa2018-10-16 14:29:20 -07003915 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3916 if (!vcpu->kvm->arch.exception_payload_enabled)
3917 return -EINVAL;
3918 if (events->exception.pending)
3919 events->exception.injected = 0;
3920 else
3921 events->exception_has_payload = 0;
3922 } else {
3923 events->exception.pending = 0;
3924 events->exception_has_payload = 0;
3925 }
3926
3927 if ((events->exception.injected || events->exception.pending) &&
3928 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003929 return -EINVAL;
3930
David Hildenbrand28bf2882017-03-23 11:46:03 +01003931 /* INITs are latched while in SMM */
3932 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3933 (events->smi.smm || events->smi.pending) &&
3934 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3935 return -EINVAL;
3936
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003937 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003938 vcpu->arch.exception.injected = events->exception.injected;
3939 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003940 vcpu->arch.exception.nr = events->exception.nr;
3941 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3942 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003943 vcpu->arch.exception.has_payload = events->exception_has_payload;
3944 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003945
Liran Alon04140b42018-03-23 03:01:31 +03003946 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003947 vcpu->arch.interrupt.nr = events->interrupt.nr;
3948 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003949 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003950 kvm_x86_ops.set_interrupt_shadow(vcpu,
Jan Kiszka48005f62010-02-19 19:38:07 +01003951 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003952
3953 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003954 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3955 vcpu->arch.nmi_pending = events->nmi.pending;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003956 kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003957
Jan Kiszka66450a22013-03-13 12:42:34 +01003958 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003959 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003960 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003961
Paolo Bonzinif0778252015-04-01 15:06:40 +02003962 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003963 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3964 if (events->smi.smm)
3965 vcpu->arch.hflags |= HF_SMM_MASK;
3966 else
3967 vcpu->arch.hflags &= ~HF_SMM_MASK;
3968 kvm_smm_changed(vcpu);
3969 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003970
Paolo Bonzinif0778252015-04-01 15:06:40 +02003971 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003972
3973 if (events->smi.smm) {
3974 if (events->smi.smm_inside_nmi)
3975 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003976 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003977 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003978 }
3979
3980 if (lapic_in_kernel(vcpu)) {
3981 if (events->smi.latched_init)
3982 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3983 else
3984 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003985 }
3986 }
3987
Avi Kivity3842d132010-07-27 12:30:24 +03003988 kvm_make_request(KVM_REQ_EVENT, vcpu);
3989
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003990 return 0;
3991}
3992
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003993static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3994 struct kvm_debugregs *dbgregs)
3995{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003996 unsigned long val;
3997
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003998 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003999 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004000 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004001 dbgregs->dr7 = vcpu->arch.dr7;
4002 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004003 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004004}
4005
4006static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
4007 struct kvm_debugregs *dbgregs)
4008{
4009 if (dbgregs->flags)
4010 return -EINVAL;
4011
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02004012 if (dbgregs->dr6 & ~0xffffffffull)
4013 return -EINVAL;
4014 if (dbgregs->dr7 & ~0xffffffffull)
4015 return -EINVAL;
4016
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004017 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03004018 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004019 vcpu->arch.dr6 = dbgregs->dr6;
4020 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004021 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004022
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004023 return 0;
4024}
4025
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004026#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4027
4028static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4029{
Marc Orrb666a4b2018-11-06 14:53:56 -08004030 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004031 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004032 u64 valid;
4033
4034 /*
4035 * Copy legacy XSAVE area, to avoid complications with CPUID
4036 * leaves 0 and 1 in the loop below.
4037 */
4038 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4039
4040 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004041 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004042 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4043
4044 /*
4045 * Copy each region from the possibly compacted offset to the
4046 * non-compacted offset.
4047 */
Dave Hansend91cab72015-09-02 16:31:26 -07004048 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004049 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004050 u64 xfeature_mask = valid & -valid;
4051 int xfeature_nr = fls64(xfeature_mask) - 1;
4052 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004053
4054 if (src) {
4055 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004056 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004057 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004058 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004059 memcpy(dest + offset, &vcpu->arch.pkru,
4060 sizeof(vcpu->arch.pkru));
4061 else
4062 memcpy(dest + offset, src, size);
4063
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004064 }
4065
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004066 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004067 }
4068}
4069
4070static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4071{
Marc Orrb666a4b2018-11-06 14:53:56 -08004072 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004073 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4074 u64 valid;
4075
4076 /*
4077 * Copy legacy XSAVE area, to avoid complications with CPUID
4078 * leaves 0 and 1 in the loop below.
4079 */
4080 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4081
4082 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004083 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004084 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004085 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004086
4087 /*
4088 * Copy each region from the non-compacted offset to the
4089 * possibly compacted offset.
4090 */
Dave Hansend91cab72015-09-02 16:31:26 -07004091 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004092 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004093 u64 xfeature_mask = valid & -valid;
4094 int xfeature_nr = fls64(xfeature_mask) - 1;
4095 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004096
4097 if (dest) {
4098 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004099 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004100 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004101 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004102 memcpy(&vcpu->arch.pkru, src + offset,
4103 sizeof(vcpu->arch.pkru));
4104 else
4105 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004106 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004107
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004108 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004109 }
4110}
4111
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004112static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4113 struct kvm_xsave *guest_xsave)
4114{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004115 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004116 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4117 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004118 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004119 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004120 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004121 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004122 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004123 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004124 }
4125}
4126
Wanpeng Lia5758132017-05-11 02:58:55 -07004127#define XSAVE_MXCSR_OFFSET 24
4128
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004129static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4130 struct kvm_xsave *guest_xsave)
4131{
4132 u64 xstate_bv =
4133 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004134 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004135
Borislav Petkovd366bf72016-04-04 22:25:02 +02004136 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004137 /*
4138 * Here we allow setting states that are not present in
4139 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4140 * with old userspace.
4141 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004142 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004143 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004144 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004145 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004146 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4147 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004148 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004149 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004150 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004151 }
4152 return 0;
4153}
4154
4155static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4156 struct kvm_xcrs *guest_xcrs)
4157{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004158 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004159 guest_xcrs->nr_xcrs = 0;
4160 return;
4161 }
4162
4163 guest_xcrs->nr_xcrs = 1;
4164 guest_xcrs->flags = 0;
4165 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4166 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4167}
4168
4169static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4170 struct kvm_xcrs *guest_xcrs)
4171{
4172 int i, r = 0;
4173
Borislav Petkovd366bf72016-04-04 22:25:02 +02004174 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004175 return -EINVAL;
4176
4177 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4178 return -EINVAL;
4179
4180 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4181 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004182 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004183 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004184 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004185 break;
4186 }
4187 if (r)
4188 r = -EINVAL;
4189 return r;
4190}
4191
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004192/*
4193 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4194 * stopped by the hypervisor. This function will be called from the host only.
4195 * EINVAL is returned when the host attempts to set the flag for a guest that
4196 * does not support pv clocks.
4197 */
4198static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4199{
Andy Honig0b794592013-02-20 14:48:10 -08004200 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004201 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004202 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004203 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4204 return 0;
4205}
4206
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004207static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4208 struct kvm_enable_cap *cap)
4209{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004210 int r;
4211 uint16_t vmcs_version;
4212 void __user *user_ptr;
4213
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004214 if (cap->flags)
4215 return -EINVAL;
4216
4217 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004218 case KVM_CAP_HYPERV_SYNIC2:
4219 if (cap->args[0])
4220 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004221 /* fall through */
4222
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004223 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004224 if (!irqchip_in_kernel(vcpu->kvm))
4225 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004226 return kvm_hv_activate_synic(vcpu, cap->cap ==
4227 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004228 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004229 if (!kvm_x86_ops.nested_enable_evmcs)
Sean Christopherson51589172018-12-03 13:53:10 -08004230 return -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004231 r = kvm_x86_ops.nested_enable_evmcs(vcpu, &vmcs_version);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004232 if (!r) {
4233 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4234 if (copy_to_user(user_ptr, &vmcs_version,
4235 sizeof(vmcs_version)))
4236 r = -EFAULT;
4237 }
4238 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004239 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004240 if (!kvm_x86_ops.enable_direct_tlbflush)
Tianyu Lan344c6c82019-08-22 22:30:20 +08004241 return -ENOTTY;
4242
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004243 return kvm_x86_ops.enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004244
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004245 default:
4246 return -EINVAL;
4247 }
4248}
4249
Carsten Otte313a3dc2007-10-11 19:16:52 +02004250long kvm_arch_vcpu_ioctl(struct file *filp,
4251 unsigned int ioctl, unsigned long arg)
4252{
4253 struct kvm_vcpu *vcpu = filp->private_data;
4254 void __user *argp = (void __user *)arg;
4255 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004256 union {
4257 struct kvm_lapic_state *lapic;
4258 struct kvm_xsave *xsave;
4259 struct kvm_xcrs *xcrs;
4260 void *buffer;
4261 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004262
Christoffer Dall9b0624712017-12-04 21:35:36 +01004263 vcpu_load(vcpu);
4264
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004265 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004266 switch (ioctl) {
4267 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004268 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004269 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004270 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004271 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4272 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004273
Dave Hansenb772ff32008-08-11 10:01:47 -07004274 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004275 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004276 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004277 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004278 if (r)
4279 goto out;
4280 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004281 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004282 goto out;
4283 r = 0;
4284 break;
4285 }
4286 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004287 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004288 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004289 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004290 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004291 if (IS_ERR(u.lapic)) {
4292 r = PTR_ERR(u.lapic);
4293 goto out_nofree;
4294 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004295
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004296 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004297 break;
4298 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004299 case KVM_INTERRUPT: {
4300 struct kvm_interrupt irq;
4301
4302 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004303 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004304 goto out;
4305 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004306 break;
4307 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004308 case KVM_NMI: {
4309 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004310 break;
4311 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004312 case KVM_SMI: {
4313 r = kvm_vcpu_ioctl_smi(vcpu);
4314 break;
4315 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004316 case KVM_SET_CPUID: {
4317 struct kvm_cpuid __user *cpuid_arg = argp;
4318 struct kvm_cpuid cpuid;
4319
4320 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004321 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004322 goto out;
4323 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004324 break;
4325 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004326 case KVM_SET_CPUID2: {
4327 struct kvm_cpuid2 __user *cpuid_arg = argp;
4328 struct kvm_cpuid2 cpuid;
4329
4330 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004331 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004332 goto out;
4333 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004334 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004335 break;
4336 }
4337 case KVM_GET_CPUID2: {
4338 struct kvm_cpuid2 __user *cpuid_arg = argp;
4339 struct kvm_cpuid2 cpuid;
4340
4341 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004342 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004343 goto out;
4344 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004345 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004346 if (r)
4347 goto out;
4348 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004349 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004350 goto out;
4351 r = 0;
4352 break;
4353 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004354 case KVM_GET_MSRS: {
4355 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004356 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004357 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004358 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004359 }
4360 case KVM_SET_MSRS: {
4361 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004362 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004363 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004364 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004365 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004366 case KVM_TPR_ACCESS_REPORTING: {
4367 struct kvm_tpr_access_ctl tac;
4368
4369 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004370 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004371 goto out;
4372 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4373 if (r)
4374 goto out;
4375 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004376 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004377 goto out;
4378 r = 0;
4379 break;
4380 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004381 case KVM_SET_VAPIC_ADDR: {
4382 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004383 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004384
4385 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004386 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004387 goto out;
4388 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004389 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004390 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004391 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004392 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004393 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004394 break;
4395 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004396 case KVM_X86_SETUP_MCE: {
4397 u64 mcg_cap;
4398
4399 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004400 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004401 goto out;
4402 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4403 break;
4404 }
4405 case KVM_X86_SET_MCE: {
4406 struct kvm_x86_mce mce;
4407
4408 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004409 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004410 goto out;
4411 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4412 break;
4413 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004414 case KVM_GET_VCPU_EVENTS: {
4415 struct kvm_vcpu_events events;
4416
4417 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4418
4419 r = -EFAULT;
4420 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4421 break;
4422 r = 0;
4423 break;
4424 }
4425 case KVM_SET_VCPU_EVENTS: {
4426 struct kvm_vcpu_events events;
4427
4428 r = -EFAULT;
4429 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4430 break;
4431
4432 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4433 break;
4434 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004435 case KVM_GET_DEBUGREGS: {
4436 struct kvm_debugregs dbgregs;
4437
4438 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4439
4440 r = -EFAULT;
4441 if (copy_to_user(argp, &dbgregs,
4442 sizeof(struct kvm_debugregs)))
4443 break;
4444 r = 0;
4445 break;
4446 }
4447 case KVM_SET_DEBUGREGS: {
4448 struct kvm_debugregs dbgregs;
4449
4450 r = -EFAULT;
4451 if (copy_from_user(&dbgregs, argp,
4452 sizeof(struct kvm_debugregs)))
4453 break;
4454
4455 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4456 break;
4457 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004458 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004459 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004460 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004461 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004462 break;
4463
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004464 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004465
4466 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004467 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004468 break;
4469 r = 0;
4470 break;
4471 }
4472 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004473 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004474 if (IS_ERR(u.xsave)) {
4475 r = PTR_ERR(u.xsave);
4476 goto out_nofree;
4477 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004478
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004479 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004480 break;
4481 }
4482 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004483 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004484 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004485 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004486 break;
4487
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004488 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004489
4490 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004491 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004492 sizeof(struct kvm_xcrs)))
4493 break;
4494 r = 0;
4495 break;
4496 }
4497 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004498 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004499 if (IS_ERR(u.xcrs)) {
4500 r = PTR_ERR(u.xcrs);
4501 goto out_nofree;
4502 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004503
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004504 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004505 break;
4506 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004507 case KVM_SET_TSC_KHZ: {
4508 u32 user_tsc_khz;
4509
4510 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004511 user_tsc_khz = (u32)arg;
4512
4513 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4514 goto out;
4515
Zachary Amsdencc578282012-02-03 15:43:50 -02004516 if (user_tsc_khz == 0)
4517 user_tsc_khz = tsc_khz;
4518
Haozhong Zhang381d5852015-10-20 15:39:04 +08004519 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4520 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004521
Joerg Roedel92a1f122011-03-25 09:44:51 +01004522 goto out;
4523 }
4524 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004525 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004526 goto out;
4527 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004528 case KVM_KVMCLOCK_CTRL: {
4529 r = kvm_set_guest_paused(vcpu);
4530 goto out;
4531 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004532 case KVM_ENABLE_CAP: {
4533 struct kvm_enable_cap cap;
4534
4535 r = -EFAULT;
4536 if (copy_from_user(&cap, argp, sizeof(cap)))
4537 goto out;
4538 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4539 break;
4540 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004541 case KVM_GET_NESTED_STATE: {
4542 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4543 u32 user_data_size;
4544
4545 r = -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004546 if (!kvm_x86_ops.get_nested_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004547 break;
4548
4549 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004550 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004551 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004552 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004553
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004554 r = kvm_x86_ops.get_nested_state(vcpu, user_kvm_nested_state,
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004555 user_data_size);
4556 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004557 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004558
4559 if (r > user_data_size) {
4560 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004561 r = -EFAULT;
4562 else
4563 r = -E2BIG;
4564 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004565 }
Liran Alon26b471c2018-09-16 14:28:20 +03004566
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004567 r = 0;
4568 break;
4569 }
4570 case KVM_SET_NESTED_STATE: {
4571 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4572 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004573 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004574
4575 r = -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004576 if (!kvm_x86_ops.set_nested_state)
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004577 break;
4578
Liran Alon26b471c2018-09-16 14:28:20 +03004579 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004580 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004581 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004582
Liran Alon26b471c2018-09-16 14:28:20 +03004583 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004584 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004585 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004586
4587 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004588 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4589 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004590 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004591
4592 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004593 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4594 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004595 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004596
Sean Christophersonad5996d2019-11-22 08:58:18 -08004597 idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004598 r = kvm_x86_ops.set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004599 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004600 break;
4601 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004602 case KVM_GET_SUPPORTED_HV_CPUID: {
4603 struct kvm_cpuid2 __user *cpuid_arg = argp;
4604 struct kvm_cpuid2 cpuid;
4605
4606 r = -EFAULT;
4607 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4608 goto out;
4609
4610 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4611 cpuid_arg->entries);
4612 if (r)
4613 goto out;
4614
4615 r = -EFAULT;
4616 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4617 goto out;
4618 r = 0;
4619 break;
4620 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004621 default:
4622 r = -EINVAL;
4623 }
4624out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004625 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004626out_nofree:
4627 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004628 return r;
4629}
4630
Souptick Joarder1499fa82018-04-19 00:49:58 +05304631vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004632{
4633 return VM_FAULT_SIGBUS;
4634}
4635
Carsten Otte1fe779f2007-10-29 16:08:35 +01004636static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4637{
4638 int ret;
4639
4640 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004641 return -EINVAL;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004642 ret = kvm_x86_ops.set_tss_addr(kvm, addr);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004643 return ret;
4644}
4645
Sheng Yangb927a3c2009-07-21 10:42:48 +08004646static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4647 u64 ident_addr)
4648{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004649 return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004650}
4651
Carsten Otte1fe779f2007-10-29 16:08:35 +01004652static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004653 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004654{
4655 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4656 return -EINVAL;
4657
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004658 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004659
4660 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004661 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004662
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004663 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004664 return 0;
4665}
4666
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004667static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004668{
Dave Hansen39de71e2010-08-19 18:11:14 -07004669 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004670}
4671
Carsten Otte1fe779f2007-10-29 16:08:35 +01004672static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4673{
David Hildenbrand90bca052017-04-07 10:50:23 +02004674 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004675 int r;
4676
4677 r = 0;
4678 switch (chip->chip_id) {
4679 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004680 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004681 sizeof(struct kvm_pic_state));
4682 break;
4683 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004684 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004685 sizeof(struct kvm_pic_state));
4686 break;
4687 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004688 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004689 break;
4690 default:
4691 r = -EINVAL;
4692 break;
4693 }
4694 return r;
4695}
4696
4697static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4698{
David Hildenbrand90bca052017-04-07 10:50:23 +02004699 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004700 int r;
4701
4702 r = 0;
4703 switch (chip->chip_id) {
4704 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004705 spin_lock(&pic->lock);
4706 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004707 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004708 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004709 break;
4710 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004711 spin_lock(&pic->lock);
4712 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004713 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004714 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004715 break;
4716 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004717 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004718 break;
4719 default:
4720 r = -EINVAL;
4721 break;
4722 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004723 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004724 return r;
4725}
4726
Sheng Yange0f63cb2008-03-04 00:50:59 +08004727static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4728{
Radim Krčmář34f39412016-03-02 22:56:50 +01004729 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4730
4731 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4732
4733 mutex_lock(&kps->lock);
4734 memcpy(ps, &kps->channels, sizeof(*ps));
4735 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304736 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004737}
4738
4739static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4740{
Andrew Honig01856042015-11-18 14:50:23 -08004741 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004742 struct kvm_pit *pit = kvm->arch.vpit;
4743
4744 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004745 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004746 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004747 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4748 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304749 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004750}
4751
4752static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4753{
Beth Kone9f42752009-07-07 11:50:38 -04004754 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4755 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4756 sizeof(ps->channels));
4757 ps->flags = kvm->arch.vpit->pit_state.flags;
4758 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004759 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304760 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004761}
4762
4763static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4764{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304765 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004766 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004767 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004768 struct kvm_pit *pit = kvm->arch.vpit;
4769
4770 mutex_lock(&pit->pit_state.lock);
4771 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004772 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4773 if (!prev_legacy && cur_legacy)
4774 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004775 memcpy(&pit->pit_state.channels, &ps->channels,
4776 sizeof(pit->pit_state.channels));
4777 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004778 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004779 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004780 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004781 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304782 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004783}
4784
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004785static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4786 struct kvm_reinject_control *control)
4787{
Radim Krčmář71474e22016-03-02 22:56:45 +01004788 struct kvm_pit *pit = kvm->arch.vpit;
4789
Radim Krčmář71474e22016-03-02 22:56:45 +01004790 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4791 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4792 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4793 */
4794 mutex_lock(&pit->pit_state.lock);
4795 kvm_pit_set_reinject(pit, control->pit_reinject);
4796 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004797
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004798 return 0;
4799}
4800
Sean Christopherson0dff0842020-02-18 13:07:29 -08004801void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004802{
Kai Huang88178fd2015-01-28 10:54:27 +08004803 /*
4804 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4805 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07004806 if (kvm_x86_ops.flush_log_dirty)
4807 kvm_x86_ops.flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004808}
4809
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004810int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4811 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004812{
4813 if (!irqchip_in_kernel(kvm))
4814 return -ENXIO;
4815
4816 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004817 irq_event->irq, irq_event->level,
4818 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004819 return 0;
4820}
4821
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004822int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4823 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004824{
4825 int r;
4826
4827 if (cap->flags)
4828 return -EINVAL;
4829
4830 switch (cap->cap) {
4831 case KVM_CAP_DISABLE_QUIRKS:
4832 kvm->arch.disabled_quirks = cap->args[0];
4833 r = 0;
4834 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004835 case KVM_CAP_SPLIT_IRQCHIP: {
4836 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004837 r = -EINVAL;
4838 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4839 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004840 r = -EEXIST;
4841 if (irqchip_in_kernel(kvm))
4842 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004843 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004844 goto split_irqchip_unlock;
4845 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004846 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004847 goto split_irqchip_unlock;
4848 /* Pairs with irqchip_in_kernel. */
4849 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004850 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004851 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004852 r = 0;
4853split_irqchip_unlock:
4854 mutex_unlock(&kvm->lock);
4855 break;
4856 }
Radim Krčmář371313132016-07-12 22:09:27 +02004857 case KVM_CAP_X2APIC_API:
4858 r = -EINVAL;
4859 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4860 break;
4861
4862 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4863 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004864 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4865 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004866
4867 r = 0;
4868 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004869 case KVM_CAP_X86_DISABLE_EXITS:
4870 r = -EINVAL;
4871 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4872 break;
4873
4874 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4875 kvm_can_mwait_in_guest())
4876 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004877 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004878 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004879 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4880 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004881 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4882 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004883 r = 0;
4884 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004885 case KVM_CAP_MSR_PLATFORM_INFO:
4886 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4887 r = 0;
4888 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004889 case KVM_CAP_EXCEPTION_PAYLOAD:
4890 kvm->arch.exception_payload_enabled = cap->args[0];
4891 r = 0;
4892 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004893 default:
4894 r = -EINVAL;
4895 break;
4896 }
4897 return r;
4898}
4899
Carsten Otte1fe779f2007-10-29 16:08:35 +01004900long kvm_arch_vm_ioctl(struct file *filp,
4901 unsigned int ioctl, unsigned long arg)
4902{
4903 struct kvm *kvm = filp->private_data;
4904 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004905 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004906 /*
4907 * This union makes it completely explicit to gcc-3.x
4908 * that these two variables' stack usage should be
4909 * combined, not added together.
4910 */
4911 union {
4912 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004913 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004914 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004915 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004916
4917 switch (ioctl) {
4918 case KVM_SET_TSS_ADDR:
4919 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004920 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004921 case KVM_SET_IDENTITY_MAP_ADDR: {
4922 u64 ident_addr;
4923
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004924 mutex_lock(&kvm->lock);
4925 r = -EINVAL;
4926 if (kvm->created_vcpus)
4927 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004928 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004929 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004930 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004931 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004932set_identity_unlock:
4933 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004934 break;
4935 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004936 case KVM_SET_NR_MMU_PAGES:
4937 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004938 break;
4939 case KVM_GET_NR_MMU_PAGES:
4940 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4941 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004942 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004943 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004944
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004945 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004946 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004947 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004948
Avi Kivity3e515702012-03-05 14:23:29 +02004949 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004950 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004951 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004952
4953 r = kvm_pic_init(kvm);
4954 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004955 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004956
4957 r = kvm_ioapic_init(kvm);
4958 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004959 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004960 goto create_irqchip_unlock;
4961 }
4962
Avi Kivity399ec802008-11-19 13:58:46 +02004963 r = kvm_setup_default_irq_routing(kvm);
4964 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004965 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004966 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004967 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004968 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004969 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004970 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004971 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004972 create_irqchip_unlock:
4973 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004974 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004975 }
Sheng Yang78376992008-01-28 05:10:22 +08004976 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004977 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4978 goto create_pit;
4979 case KVM_CREATE_PIT2:
4980 r = -EFAULT;
4981 if (copy_from_user(&u.pit_config, argp,
4982 sizeof(struct kvm_pit_config)))
4983 goto out;
4984 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004985 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004986 r = -EEXIST;
4987 if (kvm->arch.vpit)
4988 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004989 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004990 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004991 if (kvm->arch.vpit)
4992 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004993 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004994 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004995 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004996 case KVM_GET_IRQCHIP: {
4997 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004998 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004999
Sasha Levinff5c2c02011-12-04 19:36:29 +02005000 chip = memdup_user(argp, sizeof(*chip));
5001 if (IS_ERR(chip)) {
5002 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005003 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005004 }
5005
Carsten Otte1fe779f2007-10-29 16:08:35 +01005006 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005007 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005008 goto get_irqchip_out;
5009 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
5010 if (r)
5011 goto get_irqchip_out;
5012 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005013 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07005014 goto get_irqchip_out;
5015 r = 0;
5016 get_irqchip_out:
5017 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005018 break;
5019 }
5020 case KVM_SET_IRQCHIP: {
5021 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005022 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005023
Sasha Levinff5c2c02011-12-04 19:36:29 +02005024 chip = memdup_user(argp, sizeof(*chip));
5025 if (IS_ERR(chip)) {
5026 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005027 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005028 }
5029
Carsten Otte1fe779f2007-10-29 16:08:35 +01005030 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005031 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005032 goto set_irqchip_out;
5033 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005034 set_irqchip_out:
5035 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005036 break;
5037 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005038 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005039 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005040 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005041 goto out;
5042 r = -ENXIO;
5043 if (!kvm->arch.vpit)
5044 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005045 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005046 if (r)
5047 goto out;
5048 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005049 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005050 goto out;
5051 r = 0;
5052 break;
5053 }
5054 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005055 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005056 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005057 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005058 mutex_lock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005059 r = -ENXIO;
5060 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005061 goto set_pit_out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005062 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005063set_pit_out:
5064 mutex_unlock(&kvm->lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005065 break;
5066 }
Beth Kone9f42752009-07-07 11:50:38 -04005067 case KVM_GET_PIT2: {
5068 r = -ENXIO;
5069 if (!kvm->arch.vpit)
5070 goto out;
5071 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5072 if (r)
5073 goto out;
5074 r = -EFAULT;
5075 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5076 goto out;
5077 r = 0;
5078 break;
5079 }
5080 case KVM_SET_PIT2: {
5081 r = -EFAULT;
5082 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5083 goto out;
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005084 mutex_lock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005085 r = -ENXIO;
5086 if (!kvm->arch.vpit)
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005087 goto set_pit2_out;
Beth Kone9f42752009-07-07 11:50:38 -04005088 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Steve Rutherford7289fdb2020-04-16 12:11:52 -07005089set_pit2_out:
5090 mutex_unlock(&kvm->lock);
Beth Kone9f42752009-07-07 11:50:38 -04005091 break;
5092 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005093 case KVM_REINJECT_CONTROL: {
5094 struct kvm_reinject_control control;
5095 r = -EFAULT;
5096 if (copy_from_user(&control, argp, sizeof(control)))
5097 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005098 r = -ENXIO;
5099 if (!kvm->arch.vpit)
5100 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005101 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005102 break;
5103 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005104 case KVM_SET_BOOT_CPU_ID:
5105 r = 0;
5106 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005107 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005108 r = -EBUSY;
5109 else
5110 kvm->arch.bsp_vcpu_id = arg;
5111 mutex_unlock(&kvm->lock);
5112 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005113 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005114 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005115 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005116 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005117 goto out;
5118 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005119 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005120 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005121 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005122 r = 0;
5123 break;
5124 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005125 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005126 struct kvm_clock_data user_ns;
5127 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005128
5129 r = -EFAULT;
5130 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5131 goto out;
5132
5133 r = -EINVAL;
5134 if (user_ns.flags)
5135 goto out;
5136
5137 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005138 /*
5139 * TODO: userspace has to take care of races with VCPU_RUN, so
5140 * kvm_gen_update_masterclock() can be cut down to locked
5141 * pvclock_update_vm_gtod_copy().
5142 */
5143 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005144 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005145 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005146 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005147 break;
5148 }
5149 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005150 struct kvm_clock_data user_ns;
5151 u64 now_ns;
5152
Marcelo Tosattie891a322017-04-17 12:51:37 -03005153 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005154 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005155 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005156 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005157
5158 r = -EFAULT;
5159 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5160 goto out;
5161 r = 0;
5162 break;
5163 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005164 case KVM_MEMORY_ENCRYPT_OP: {
5165 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005166 if (kvm_x86_ops.mem_enc_op)
5167 r = kvm_x86_ops.mem_enc_op(kvm, argp);
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005168 break;
5169 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005170 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5171 struct kvm_enc_region region;
5172
5173 r = -EFAULT;
5174 if (copy_from_user(&region, argp, sizeof(region)))
5175 goto out;
5176
5177 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005178 if (kvm_x86_ops.mem_enc_reg_region)
5179 r = kvm_x86_ops.mem_enc_reg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005180 break;
5181 }
5182 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5183 struct kvm_enc_region region;
5184
5185 r = -EFAULT;
5186 if (copy_from_user(&region, argp, sizeof(region)))
5187 goto out;
5188
5189 r = -ENOTTY;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005190 if (kvm_x86_ops.mem_enc_unreg_region)
5191 r = kvm_x86_ops.mem_enc_unreg_region(kvm, &region);
Brijesh Singh69eaede2017-12-04 10:57:26 -06005192 break;
5193 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005194 case KVM_HYPERV_EVENTFD: {
5195 struct kvm_hyperv_eventfd hvevfd;
5196
5197 r = -EFAULT;
5198 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5199 goto out;
5200 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5201 break;
5202 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005203 case KVM_SET_PMU_EVENT_FILTER:
5204 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5205 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005206 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005207 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005208 }
5209out:
5210 return r;
5211}
5212
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005213static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005214{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005215 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005216 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005217 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005218
Jim Mattsone2ada662019-08-21 11:20:04 -07005219 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005220 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005221
5222 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005223
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005224 num_msrs_to_save = 0;
5225 num_emulated_msrs = 0;
5226 num_msr_based_features = 0;
5227
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005228 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5229 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005230 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005231
5232 /*
5233 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005234 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005235 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005236 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005237 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005238 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005239 continue;
5240 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005241 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005242 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005243 continue;
5244 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005245 case MSR_IA32_RTIT_CTL:
5246 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005247 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005248 continue;
5249 break;
5250 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005251 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005252 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5253 continue;
5254 break;
5255 case MSR_IA32_RTIT_OUTPUT_BASE:
5256 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005257 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005258 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5259 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5260 continue;
5261 break;
5262 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
Sean Christopherson7b874c22020-03-02 15:56:59 -08005263 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005264 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005265 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5266 continue;
5267 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005268 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005269 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005270 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5271 continue;
5272 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005273 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005274 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005275 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5276 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005277 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005278 default:
5279 break;
5280 }
5281
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005282 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005283 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005284
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005285 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005286 if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005287 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005288
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005289 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005290 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005291
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005292 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005293 struct kvm_msr_entry msr;
5294
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005295 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005296 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005297 continue;
5298
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005299 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005300 }
Carsten Otte043405e2007-10-10 17:16:19 +02005301}
5302
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005303static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5304 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005305{
Avi Kivity70252a12010-01-19 12:51:22 +02005306 int handled = 0;
5307 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005308
Avi Kivity70252a12010-01-19 12:51:22 +02005309 do {
5310 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005311 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005312 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5313 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005314 break;
5315 handled += n;
5316 addr += n;
5317 len -= n;
5318 v += n;
5319 } while (len);
5320
5321 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005322}
5323
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005324static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005325{
Avi Kivity70252a12010-01-19 12:51:22 +02005326 int handled = 0;
5327 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005328
Avi Kivity70252a12010-01-19 12:51:22 +02005329 do {
5330 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005331 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005332 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5333 addr, n, v))
5334 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005335 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005336 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005337 handled += n;
5338 addr += n;
5339 len -= n;
5340 v += n;
5341 } while (len);
5342
5343 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005344}
5345
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005346static void kvm_set_segment(struct kvm_vcpu *vcpu,
5347 struct kvm_segment *var, int seg)
5348{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005349 kvm_x86_ops.set_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005350}
5351
5352void kvm_get_segment(struct kvm_vcpu *vcpu,
5353 struct kvm_segment *var, int seg)
5354{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005355 kvm_x86_ops.get_segment(vcpu, var, seg);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005356}
5357
Paolo Bonzini54987b72014-09-02 13:23:06 +02005358gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5359 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005360{
5361 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005362
5363 BUG_ON(!mmu_is_nested(vcpu));
5364
5365 /* NPT walks are always user-walks */
5366 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005367 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005368
5369 return t_gpa;
5370}
5371
Avi Kivityab9ae312010-11-22 17:53:26 +02005372gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5373 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005374{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005375 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005376 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005377}
5378
Avi Kivityab9ae312010-11-22 17:53:26 +02005379 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5380 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005381{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005382 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005383 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005384 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005385}
5386
Avi Kivityab9ae312010-11-22 17:53:26 +02005387gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5388 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005389{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005390 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Gleb Natapov1871c602010-02-10 14:21:32 +02005391 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005392 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005393}
5394
5395/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005396gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5397 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005398{
Avi Kivityab9ae312010-11-22 17:53:26 +02005399 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005400}
5401
5402static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5403 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005404 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005405{
5406 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005407 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005408
5409 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005410 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005411 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005412 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005413 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005414 int ret;
5415
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005416 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005417 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005418 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5419 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005420 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005421 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005422 goto out;
5423 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005424
Izik Eidus77c20022008-12-29 01:42:19 +02005425 bytes -= toread;
5426 data += toread;
5427 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005428 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005429out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005430 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005431}
Izik Eidus77c20022008-12-29 01:42:19 +02005432
Gleb Natapov1871c602010-02-10 14:21:32 +02005433/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005434static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5435 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005436 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005437{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005438 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005439 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005440 unsigned offset;
5441 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005442
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005443 /* Inline kvm_read_guest_virt_helper for speed. */
5444 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5445 exception);
5446 if (unlikely(gpa == UNMAPPED_GVA))
5447 return X86EMUL_PROPAGATE_FAULT;
5448
5449 offset = addr & (PAGE_SIZE-1);
5450 if (WARN_ON(offset + bytes > PAGE_SIZE))
5451 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005452 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5453 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005454 if (unlikely(ret < 0))
5455 return X86EMUL_IO_NEEDED;
5456
5457 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005458}
5459
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005460int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005461 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005462 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005463{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005464 u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005465
Paolo Bonzini353c0952019-01-29 18:41:16 +01005466 /*
5467 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5468 * is returned, but our callers are not ready for that and they blindly
5469 * call kvm_inject_page_fault. Ensure that they at least do not leak
5470 * uninitialized kernel stack memory into cr2 and error code.
5471 */
5472 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005473 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005474 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005475}
Nadav Har'El064aea72011-05-25 23:04:56 +03005476EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005477
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005478static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5479 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005480 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005481{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005482 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005483 u32 access = 0;
5484
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005485 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005486 access |= PFERR_USER_MASK;
5487
5488 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005489}
5490
Radim Krčmář7a036a62015-10-30 16:36:24 +01005491static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5492 unsigned long addr, void *val, unsigned int bytes)
5493{
5494 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5495 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5496
5497 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5498}
5499
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005500static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5501 struct kvm_vcpu *vcpu, u32 access,
5502 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005503{
5504 void *data = val;
5505 int r = X86EMUL_CONTINUE;
5506
5507 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005508 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005509 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005510 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005511 unsigned offset = addr & (PAGE_SIZE-1);
5512 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5513 int ret;
5514
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005515 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005516 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005517 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005518 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005519 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005520 goto out;
5521 }
5522
5523 bytes -= towrite;
5524 data += towrite;
5525 addr += towrite;
5526 }
5527out:
5528 return r;
5529}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005530
5531static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005532 unsigned int bytes, struct x86_exception *exception,
5533 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005534{
5535 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005536 u32 access = PFERR_WRITE_MASK;
5537
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005538 if (!system && kvm_x86_ops.get_cpl(vcpu) == 3)
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005539 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005540
5541 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005542 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005543}
5544
5545int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5546 unsigned int bytes, struct x86_exception *exception)
5547{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005548 /* kvm_write_guest_virt_system can pull in tons of pages. */
5549 vcpu->arch.l1tf_flush_l1d = true;
5550
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005551 /*
5552 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5553 * is returned, but our callers are not ready for that and they blindly
5554 * call kvm_inject_page_fault. Ensure that they at least do not leak
5555 * uninitialized kernel stack memory into cr2 and error code.
5556 */
5557 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005558 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5559 PFERR_WRITE_MASK, exception);
5560}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005561EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005562
Wanpeng Li082d06e2018-04-03 16:28:48 -07005563int handle_ud(struct kvm_vcpu *vcpu)
5564{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005565 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005566 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005567 char sig[5]; /* ud2; .ascii "kvm" */
5568 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005569
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005570 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005571 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5572 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005573 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005574 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005575 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005576 }
5577
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005578 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005579}
5580EXPORT_SYMBOL_GPL(handle_ud);
5581
Tom Lendacky0f89b202016-12-14 14:59:23 -05005582static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5583 gpa_t gpa, bool write)
5584{
5585 /* For APIC access vmexit */
5586 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5587 return 1;
5588
5589 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5590 trace_vcpu_match_mmio(gva, gpa, write, true);
5591 return 1;
5592 }
5593
5594 return 0;
5595}
5596
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005597static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5598 gpa_t *gpa, struct x86_exception *exception,
5599 bool write)
5600{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005601 u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
Avi Kivity97d64b72012-09-12 14:52:00 +03005602 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005603
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005604 /*
5605 * currently PKRU is only applied to ept enabled guest so
5606 * there is no pkey in EPT page table for L1 guest or EPT
5607 * shadow page table for L2 guest.
5608 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005609 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005610 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005611 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005612 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5613 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005614 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005615 return 1;
5616 }
5617
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005618 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5619
5620 if (*gpa == UNMAPPED_GVA)
5621 return -1;
5622
Tom Lendacky0f89b202016-12-14 14:59:23 -05005623 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005624}
5625
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005626int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005627 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005628{
5629 int ret;
5630
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005631 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005632 if (ret < 0)
5633 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005634 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005635 return 1;
5636}
5637
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005638struct read_write_emulator_ops {
5639 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5640 int bytes);
5641 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5642 void *val, int bytes);
5643 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5644 int bytes, void *val);
5645 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5646 void *val, int bytes);
5647 bool write;
5648};
5649
5650static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5651{
5652 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005653 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005654 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005655 vcpu->mmio_read_completed = 0;
5656 return 1;
5657 }
5658
5659 return 0;
5660}
5661
5662static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5663 void *val, int bytes)
5664{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005665 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005666}
5667
5668static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5669 void *val, int bytes)
5670{
5671 return emulator_write_phys(vcpu, gpa, val, bytes);
5672}
5673
5674static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5675{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005676 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005677 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5678}
5679
5680static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5681 void *val, int bytes)
5682{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005683 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005684 return X86EMUL_IO_NEEDED;
5685}
5686
5687static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5688 void *val, int bytes)
5689{
Avi Kivityf78146b2012-04-18 19:22:47 +03005690 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5691
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005692 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005693 return X86EMUL_CONTINUE;
5694}
5695
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005696static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005697 .read_write_prepare = read_prepare,
5698 .read_write_emulate = read_emulate,
5699 .read_write_mmio = vcpu_mmio_read,
5700 .read_write_exit_mmio = read_exit_mmio,
5701};
5702
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005703static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005704 .read_write_emulate = write_emulate,
5705 .read_write_mmio = write_mmio,
5706 .read_write_exit_mmio = write_exit_mmio,
5707 .write = true,
5708};
5709
Xiao Guangrong22388a32011-07-13 14:32:31 +08005710static int emulator_read_write_onepage(unsigned long addr, void *val,
5711 unsigned int bytes,
5712 struct x86_exception *exception,
5713 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005714 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005715{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005716 gpa_t gpa;
5717 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005718 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005719 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005720 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005721
5722 /*
5723 * If the exit was due to a NPF we may already have a GPA.
5724 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5725 * Note, this cannot be used on string operations since string
5726 * operation using rep will only have the initial GPA from the NPF
5727 * occurred.
5728 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005729 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5730 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5731 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005732 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5733 } else {
5734 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5735 if (ret < 0)
5736 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005737 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005738
Brijesh Singh618232e2017-08-17 18:36:57 +02005739 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005740 return X86EMUL_CONTINUE;
5741
Carsten Ottebbd9b642007-10-30 18:44:21 +01005742 /*
5743 * Is this MMIO handled locally?
5744 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005745 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005746 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005747 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005748
Avi Kivity70252a12010-01-19 12:51:22 +02005749 gpa += handled;
5750 bytes -= handled;
5751 val += handled;
5752
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005753 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5754 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5755 frag->gpa = gpa;
5756 frag->data = val;
5757 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005758 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005759}
5760
Xiubo Li52eb5a62015-03-13 17:39:45 +08005761static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5762 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005763 void *val, unsigned int bytes,
5764 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005765 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005766{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005767 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005768 gpa_t gpa;
5769 int rc;
5770
5771 if (ops->read_write_prepare &&
5772 ops->read_write_prepare(vcpu, val, bytes))
5773 return X86EMUL_CONTINUE;
5774
5775 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005776
Carsten Ottebbd9b642007-10-30 18:44:21 +01005777 /* Crossing a page boundary? */
5778 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005779 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005780
5781 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005782 rc = emulator_read_write_onepage(addr, val, now, exception,
5783 vcpu, ops);
5784
Carsten Ottebbd9b642007-10-30 18:44:21 +01005785 if (rc != X86EMUL_CONTINUE)
5786 return rc;
5787 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005788 if (ctxt->mode != X86EMUL_MODE_PROT64)
5789 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005790 val += now;
5791 bytes -= now;
5792 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005793
Avi Kivityf78146b2012-04-18 19:22:47 +03005794 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5795 vcpu, ops);
5796 if (rc != X86EMUL_CONTINUE)
5797 return rc;
5798
5799 if (!vcpu->mmio_nr_fragments)
5800 return rc;
5801
5802 gpa = vcpu->mmio_fragments[0].gpa;
5803
5804 vcpu->mmio_needed = 1;
5805 vcpu->mmio_cur_fragment = 0;
5806
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005807 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005808 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5809 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5810 vcpu->run->mmio.phys_addr = gpa;
5811
5812 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005813}
5814
5815static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5816 unsigned long addr,
5817 void *val,
5818 unsigned int bytes,
5819 struct x86_exception *exception)
5820{
5821 return emulator_read_write(ctxt, addr, val, bytes,
5822 exception, &read_emultor);
5823}
5824
Xiubo Li52eb5a62015-03-13 17:39:45 +08005825static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005826 unsigned long addr,
5827 const void *val,
5828 unsigned int bytes,
5829 struct x86_exception *exception)
5830{
5831 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5832 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005833}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005834
Avi Kivitydaea3e72010-03-15 13:59:54 +02005835#define CMPXCHG_TYPE(t, ptr, old, new) \
5836 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5837
5838#ifdef CONFIG_X86_64
5839# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5840#else
5841# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005842 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005843#endif
5844
Avi Kivity0f65dd72011-04-20 13:37:53 +03005845static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5846 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005847 const void *old,
5848 const void *new,
5849 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005850 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005851{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005852 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005853 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005854 u64 page_line_mask;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005855 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005856 char *kaddr;
5857 bool exchanged;
5858
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005859 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005860 if (bytes > 8 || (bytes & (bytes - 1)))
5861 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005862
Avi Kivitydaea3e72010-03-15 13:59:54 +02005863 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005864
Avi Kivitydaea3e72010-03-15 13:59:54 +02005865 if (gpa == UNMAPPED_GVA ||
5866 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5867 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005868
Xiaoyao Li9de6fe32020-04-10 13:54:01 +02005869 /*
5870 * Emulate the atomic as a straight write to avoid #AC if SLD is
5871 * enabled in the host and the access splits a cache line.
5872 */
5873 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
5874 page_line_mask = ~(cache_line_size() - 1);
5875 else
5876 page_line_mask = PAGE_MASK;
5877
5878 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005879 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005880
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005881 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005882 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005883
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005884 kaddr = map.hva + offset_in_page(gpa);
5885
Avi Kivitydaea3e72010-03-15 13:59:54 +02005886 switch (bytes) {
5887 case 1:
5888 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5889 break;
5890 case 2:
5891 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5892 break;
5893 case 4:
5894 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5895 break;
5896 case 8:
5897 exchanged = CMPXCHG64(kaddr, old, new);
5898 break;
5899 default:
5900 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005901 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005902
5903 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005904
5905 if (!exchanged)
5906 return X86EMUL_CMPXCHG_FAILED;
5907
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005908 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005909
5910 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005911
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005912emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005913 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005914
Avi Kivity0f65dd72011-04-20 13:37:53 +03005915 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005916}
5917
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005918static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5919{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005920 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005921
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005922 for (i = 0; i < vcpu->arch.pio.count; i++) {
5923 if (vcpu->arch.pio.in)
5924 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5925 vcpu->arch.pio.size, pd);
5926 else
5927 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5928 vcpu->arch.pio.port, vcpu->arch.pio.size,
5929 pd);
5930 if (r)
5931 break;
5932 pd += vcpu->arch.pio.size;
5933 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005934 return r;
5935}
5936
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005937static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5938 unsigned short port, void *val,
5939 unsigned int count, bool in)
5940{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005941 vcpu->arch.pio.port = port;
5942 vcpu->arch.pio.in = in;
5943 vcpu->arch.pio.count = count;
5944 vcpu->arch.pio.size = size;
5945
5946 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5947 vcpu->arch.pio.count = 0;
5948 return 1;
5949 }
5950
5951 vcpu->run->exit_reason = KVM_EXIT_IO;
5952 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5953 vcpu->run->io.size = size;
5954 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5955 vcpu->run->io.count = count;
5956 vcpu->run->io.port = port;
5957
5958 return 0;
5959}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005960
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005961static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5962 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005963{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005964 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005965
Gleb Natapov79729952010-03-18 15:20:24 +02005966 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005967 goto data_avail;
5968
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005969 memset(vcpu->arch.pio_data, 0, size * count);
5970
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005971 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5972 if (ret) {
5973data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005974 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005975 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005976 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005977 return 1;
5978 }
5979
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005980 return 0;
5981}
5982
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005983static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5984 int size, unsigned short port, void *val,
5985 unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005986{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005987 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
Avi Kivityca1d4a92011-04-20 13:37:53 +03005988
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005989}
5990
5991static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5992 unsigned short port, const void *val,
5993 unsigned int count)
5994{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005995 memcpy(vcpu->arch.pio_data, val, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005996 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005997 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005998}
5999
Carsten Ottebbd9b642007-10-30 18:44:21 +01006000static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
6001 int size, unsigned short port,
6002 const void *val, unsigned int count)
6003{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006004 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006005}
6006
6007static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
6008{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006009 return kvm_x86_ops.get_segment_base(vcpu, seg);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006010}
6011
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006012static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006013{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006014 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006015}
6016
Jiang Biaoae6a2372016-11-07 08:54:51 +08006017static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006018{
6019 if (!need_emulate_wbinvd(vcpu))
6020 return X86EMUL_CONTINUE;
6021
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006022 if (kvm_x86_ops.has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01006023 int cpu = get_cpu();
6024
6025 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006026 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
6027 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006028 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006029 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01006030 } else
6031 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006032 return X86EMUL_CONTINUE;
6033}
Joel Schopp5cb56052015-03-02 13:43:31 -06006034
6035int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6036{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006037 kvm_emulate_wbinvd_noskip(vcpu);
6038 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006039}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006040EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6041
Joel Schopp5cb56052015-03-02 13:43:31 -06006042
6043
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006044static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6045{
Joel Schopp5cb56052015-03-02 13:43:31 -06006046 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006047}
6048
Xiubo Li52eb5a62015-03-13 17:39:45 +08006049static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6050 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006051{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006052 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006053}
6054
Xiubo Li52eb5a62015-03-13 17:39:45 +08006055static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6056 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006057{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006058
Avi Kivity717746e2011-04-20 13:37:53 +03006059 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006060}
6061
Gleb Natapov52a46612010-03-18 15:20:03 +02006062static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6063{
6064 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6065}
6066
Avi Kivity717746e2011-04-20 13:37:53 +03006067static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006068{
Avi Kivity717746e2011-04-20 13:37:53 +03006069 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006070 unsigned long value;
6071
6072 switch (cr) {
6073 case 0:
6074 value = kvm_read_cr0(vcpu);
6075 break;
6076 case 2:
6077 value = vcpu->arch.cr2;
6078 break;
6079 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006080 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006081 break;
6082 case 4:
6083 value = kvm_read_cr4(vcpu);
6084 break;
6085 case 8:
6086 value = kvm_get_cr8(vcpu);
6087 break;
6088 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006089 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006090 return 0;
6091 }
6092
6093 return value;
6094}
6095
Avi Kivity717746e2011-04-20 13:37:53 +03006096static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006097{
Avi Kivity717746e2011-04-20 13:37:53 +03006098 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006099 int res = 0;
6100
Gleb Natapov52a46612010-03-18 15:20:03 +02006101 switch (cr) {
6102 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006103 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006104 break;
6105 case 2:
6106 vcpu->arch.cr2 = val;
6107 break;
6108 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006109 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006110 break;
6111 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006112 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006113 break;
6114 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006115 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006116 break;
6117 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006118 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006119 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006120 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006121
6122 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006123}
6124
Avi Kivity717746e2011-04-20 13:37:53 +03006125static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006126{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006127 return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006128}
6129
Avi Kivity4bff1e862011-04-20 13:37:53 +03006130static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006131{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006132 kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006133}
6134
Avi Kivity4bff1e862011-04-20 13:37:53 +03006135static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006136{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006137 kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006138}
6139
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006140static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6141{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006142 kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006143}
6144
6145static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6146{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006147 kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt);
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006148}
6149
Avi Kivity4bff1e862011-04-20 13:37:53 +03006150static unsigned long emulator_get_cached_segment_base(
6151 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006152{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006153 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006154}
6155
Avi Kivity1aa36612011-04-27 13:20:30 +03006156static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6157 struct desc_struct *desc, u32 *base3,
6158 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006159{
6160 struct kvm_segment var;
6161
Avi Kivity4bff1e862011-04-20 13:37:53 +03006162 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006163 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006164
Gleb Natapov378a8b02013-01-21 15:36:48 +02006165 if (var.unusable) {
6166 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006167 if (base3)
6168 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006169 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006170 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006171
6172 if (var.g)
6173 var.limit >>= 12;
6174 set_desc_limit(desc, var.limit);
6175 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006176#ifdef CONFIG_X86_64
6177 if (base3)
6178 *base3 = var.base >> 32;
6179#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006180 desc->type = var.type;
6181 desc->s = var.s;
6182 desc->dpl = var.dpl;
6183 desc->p = var.present;
6184 desc->avl = var.avl;
6185 desc->l = var.l;
6186 desc->d = var.db;
6187 desc->g = var.g;
6188
6189 return true;
6190}
6191
Avi Kivity1aa36612011-04-27 13:20:30 +03006192static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6193 struct desc_struct *desc, u32 base3,
6194 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006195{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006196 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006197 struct kvm_segment var;
6198
Avi Kivity1aa36612011-04-27 13:20:30 +03006199 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006200 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006201#ifdef CONFIG_X86_64
6202 var.base |= ((u64)base3) << 32;
6203#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006204 var.limit = get_desc_limit(desc);
6205 if (desc->g)
6206 var.limit = (var.limit << 12) | 0xfff;
6207 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006208 var.dpl = desc->dpl;
6209 var.db = desc->d;
6210 var.s = desc->s;
6211 var.l = desc->l;
6212 var.g = desc->g;
6213 var.avl = desc->avl;
6214 var.present = desc->p;
6215 var.unusable = !var.present;
6216 var.padding = 0;
6217
6218 kvm_set_segment(vcpu, &var, seg);
6219 return;
6220}
6221
Avi Kivity717746e2011-04-20 13:37:53 +03006222static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6223 u32 msr_index, u64 *pdata)
6224{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006225 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006226}
6227
6228static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6229 u32 msr_index, u64 data)
6230{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006231 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006232}
6233
Paolo Bonzini64d60672015-05-07 11:36:11 +02006234static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6235{
6236 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6237
6238 return vcpu->arch.smbase;
6239}
6240
6241static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6242{
6243 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6244
6245 vcpu->arch.smbase = smbase;
6246}
6247
Nadav Amit67f4d422014-06-02 18:34:09 +03006248static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6249 u32 pmc)
6250{
Like Xu98ff80f2019-10-27 18:52:40 +08006251 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006252}
6253
Avi Kivity222d21a2011-11-10 14:57:30 +02006254static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6255 u32 pmc, u64 *pdata)
6256{
Wei Huangc6702c92015-06-19 13:44:45 +02006257 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006258}
6259
Avi Kivity6c3287f2011-04-20 15:43:05 +03006260static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6261{
6262 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6263}
6264
Avi Kivity29535382011-04-20 13:37:53 +03006265static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006266 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006267 enum x86_intercept_stage stage)
6268{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006269 return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006270 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006271}
6272
Yu Zhange911eb32017-08-24 20:27:52 +08006273static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Sean Christophersonf91af512020-03-04 17:34:37 -08006274 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx,
6275 bool exact_only)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006276{
Sean Christophersonf91af512020-03-04 17:34:37 -08006277 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006278}
6279
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006280static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6281{
6282 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6283}
6284
6285static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6286{
6287 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6288}
6289
6290static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6291{
6292 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6293}
6294
Avi Kivitydd856ef2012-08-27 23:46:17 +03006295static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6296{
6297 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6298}
6299
6300static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6301{
6302 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6303}
6304
Nadav Amit801806d2015-01-26 09:32:23 +02006305static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6306{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006307 kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked);
Nadav Amit801806d2015-01-26 09:32:23 +02006308}
6309
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006310static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6311{
6312 return emul_to_vcpu(ctxt)->arch.hflags;
6313}
6314
6315static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6316{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006317 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006318}
6319
Sean Christophersoned193212019-04-02 08:03:09 -07006320static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6321 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006322{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006323 return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006324}
6325
Sean Christophersonc5833c72019-04-02 08:03:10 -07006326static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6327{
6328 kvm_smm_changed(emul_to_vcpu(ctxt));
6329}
6330
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006331static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6332{
6333 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6334}
6335
Mathias Krause0225fb52012-08-30 01:30:16 +02006336static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006337 .read_gpr = emulator_read_gpr,
6338 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006339 .read_std = emulator_read_std,
6340 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006341 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006342 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006343 .read_emulated = emulator_read_emulated,
6344 .write_emulated = emulator_write_emulated,
6345 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006346 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006347 .pio_in_emulated = emulator_pio_in_emulated,
6348 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006349 .get_segment = emulator_get_segment,
6350 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006351 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006352 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006353 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006354 .set_gdt = emulator_set_gdt,
6355 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006356 .get_cr = emulator_get_cr,
6357 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006358 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006359 .get_dr = emulator_get_dr,
6360 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006361 .get_smbase = emulator_get_smbase,
6362 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006363 .set_msr = emulator_set_msr,
6364 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006365 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006366 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006367 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006368 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006369 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006370 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006371 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006372 .guest_has_long_mode = emulator_guest_has_long_mode,
6373 .guest_has_movbe = emulator_guest_has_movbe,
6374 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006375 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006376 .get_hflags = emulator_get_hflags,
6377 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006378 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006379 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006380 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006381};
6382
Gleb Natapov95cb2292010-04-28 19:15:43 +03006383static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6384{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006385 u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006386 /*
6387 * an sti; sti; sequence only disable interrupts for the first
6388 * instruction. So, if the last instruction, be it emulated or
6389 * not, left the system with the INT_STI flag enabled, it
6390 * means that the last instruction is an sti. We should not
6391 * leave the flag on in this case. The same goes for mov ss
6392 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006393 if (int_shadow & mask)
6394 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006395 if (unlikely(int_shadow || mask)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006396 kvm_x86_ops.set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006397 if (!mask)
6398 kvm_make_request(KVM_REQ_EVENT, vcpu);
6399 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006400}
6401
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006402static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006403{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006404 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006405 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006406 return kvm_propagate_fault(vcpu, &ctxt->exception);
6407
6408 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006409 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6410 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006411 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006412 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006413 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006414}
6415
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006416static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6417{
6418 struct x86_emulate_ctxt *ctxt;
6419
6420 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6421 if (!ctxt) {
6422 pr_err("kvm: failed to allocate vcpu's emulator\n");
6423 return NULL;
6424 }
6425
6426 ctxt->vcpu = vcpu;
6427 ctxt->ops = &emulate_ops;
6428 vcpu->arch.emulate_ctxt = ctxt;
6429
6430 return ctxt;
6431}
6432
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006433static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6434{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006435 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006436 int cs_db, cs_l;
6437
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006438 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006439
Sean Christopherson744e6992020-02-18 15:03:09 -08006440 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006441 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006442 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6443
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006444 ctxt->eip = kvm_rip_read(vcpu);
6445 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6446 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006447 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006448 cs_db ? X86EMUL_MODE_PROT32 :
6449 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006450 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006451 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6452 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006453
Avi Kivitydd856ef2012-08-27 23:46:17 +03006454 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006455 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006456}
6457
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006458void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006459{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006460 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006461 int ret;
6462
6463 init_emulate_ctxt(vcpu);
6464
Avi Kivity9dac77f2011-06-01 15:34:25 +03006465 ctxt->op_bytes = 2;
6466 ctxt->ad_bytes = 2;
6467 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006468 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006469
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006470 if (ret != X86EMUL_CONTINUE) {
6471 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6472 } else {
6473 ctxt->eip = ctxt->_eip;
6474 kvm_rip_write(vcpu, ctxt->eip);
6475 kvm_set_rflags(vcpu, ctxt->eflags);
6476 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006477}
6478EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6479
Liran Alone2366172018-03-12 13:12:49 +02006480static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006481{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006482 ++vcpu->stat.insn_emulation_fail;
6483 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006484
Sean Christopherson42cbf062019-08-27 14:40:31 -07006485 if (emulation_type & EMULTYPE_VMWARE_GP) {
6486 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006487 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006488 }
Liran Alone2366172018-03-12 13:12:49 +02006489
Sean Christopherson738fece2019-08-27 14:40:34 -07006490 if (emulation_type & EMULTYPE_SKIP) {
6491 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6492 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6493 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006494 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006495 }
6496
Sean Christopherson22da61c2019-08-27 14:40:28 -07006497 kvm_queue_exception(vcpu, UD_VECTOR);
6498
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006499 if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006500 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6501 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6502 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006503 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006504 }
Liran Alone2366172018-03-12 13:12:49 +02006505
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006506 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006507}
6508
Sean Christopherson736c2912019-12-06 15:57:14 -08006509static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006510 bool write_fault_to_shadow_pgtable,
6511 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006512{
Sean Christopherson736c2912019-12-06 15:57:14 -08006513 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006514 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006515
Sean Christopherson92daa482020-02-18 15:03:08 -08006516 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006517 return false;
6518
Sean Christopherson92daa482020-02-18 15:03:08 -08006519 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6520 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006521 return false;
6522
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006523 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006524 /*
6525 * Write permission should be allowed since only
6526 * write access need to be emulated.
6527 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006528 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006529
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006530 /*
6531 * If the mapping is invalid in guest, let cpu retry
6532 * it to generate fault.
6533 */
6534 if (gpa == UNMAPPED_GVA)
6535 return true;
6536 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006537
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006538 /*
6539 * Do not retry the unhandleable instruction if it faults on the
6540 * readonly host memory, otherwise it will goto a infinite loop:
6541 * retry instruction -> write #PF -> emulation fail -> retry
6542 * instruction -> ...
6543 */
6544 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006545
6546 /*
6547 * If the instruction failed on the error pfn, it can not be fixed,
6548 * report the error to userspace.
6549 */
6550 if (is_error_noslot_pfn(pfn))
6551 return false;
6552
6553 kvm_release_pfn_clean(pfn);
6554
6555 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006556 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006557 unsigned int indirect_shadow_pages;
6558
6559 spin_lock(&vcpu->kvm->mmu_lock);
6560 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6561 spin_unlock(&vcpu->kvm->mmu_lock);
6562
6563 if (indirect_shadow_pages)
6564 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6565
Gleb Natapova6f177e2010-07-08 12:41:12 +03006566 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006567 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006568
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006569 /*
6570 * if emulation was due to access to shadowed page table
6571 * and it failed try to unshadow page and re-enter the
6572 * guest to let CPU execute the instruction.
6573 */
6574 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006575
6576 /*
6577 * If the access faults on its page table, it can not
6578 * be fixed by unprotecting shadow page and it should
6579 * be reported to userspace.
6580 */
6581 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006582}
6583
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006584static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006585 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006586{
6587 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006588 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006589
6590 last_retry_eip = vcpu->arch.last_retry_eip;
6591 last_retry_addr = vcpu->arch.last_retry_addr;
6592
6593 /*
6594 * If the emulation is caused by #PF and it is non-page_table
6595 * writing instruction, it means the VM-EXIT is caused by shadow
6596 * page protected, we can zap the shadow page and retry this
6597 * instruction directly.
6598 *
6599 * Note: if the guest uses a non-page-table modifying instruction
6600 * on the PDE that points to the instruction, then we will unmap
6601 * the instruction and go to an infinite loop. So, we cache the
6602 * last retried eip and the last fault address, if we meet the eip
6603 * and the address again, we can break out of the potential infinite
6604 * loop.
6605 */
6606 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6607
Sean Christopherson92daa482020-02-18 15:03:08 -08006608 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006609 return false;
6610
Sean Christopherson92daa482020-02-18 15:03:08 -08006611 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6612 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006613 return false;
6614
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006615 if (x86_page_table_writing_insn(ctxt))
6616 return false;
6617
Sean Christopherson736c2912019-12-06 15:57:14 -08006618 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006619 return false;
6620
6621 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006622 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006623
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006624 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006625 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006626
Xiao Guangrong22368022013-01-13 23:44:12 +08006627 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006628
6629 return true;
6630}
6631
Gleb Natapov716d51a2012-09-03 15:24:26 +03006632static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6633static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6634
Paolo Bonzini64d60672015-05-07 11:36:11 +02006635static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006636{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006637 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006638 /* This is a good place to trace that we are exiting SMM. */
6639 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6640
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006641 /* Process a latched INIT or SMI, if any. */
6642 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006643 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006644
6645 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006646}
6647
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006648static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6649 unsigned long *db)
6650{
6651 u32 dr6 = 0;
6652 int i;
6653 u32 enable, rwlen;
6654
6655 enable = dr7;
6656 rwlen = dr7 >> 16;
6657 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6658 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6659 dr6 |= (1 << i);
6660 return dr6;
6661}
6662
Sean Christopherson120c2c42019-08-27 14:40:29 -07006663static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006664{
6665 struct kvm_run *kvm_run = vcpu->run;
6666
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006667 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6668 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
Peter Xud5d260c2020-05-05 16:49:59 -04006669 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006670 kvm_run->debug.arch.exception = DB_VECTOR;
6671 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006672 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006673 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006674 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006675 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006676}
6677
Kyle Huey6affcbe2016-11-29 12:40:40 -08006678int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6679{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006680 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006681 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006682
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006683 r = kvm_x86_ops.skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006684 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006685 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006686
6687 /*
6688 * rflags is the old, "raw" value of the flags. The new value has
6689 * not been saved yet.
6690 *
6691 * This is correct even for TF set by the guest, because "the
6692 * processor will not generate this exception after the instruction
6693 * that sets the TF flag".
6694 */
6695 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006696 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006697 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006698}
6699EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6700
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006701static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6702{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006703 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6704 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006705 struct kvm_run *kvm_run = vcpu->run;
6706 unsigned long eip = kvm_get_linear_rip(vcpu);
6707 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006708 vcpu->arch.guest_debug_dr7,
6709 vcpu->arch.eff_db);
6710
6711 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006712 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006713 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006714 kvm_run->debug.arch.exception = DB_VECTOR;
6715 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006716 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006717 return true;
6718 }
6719 }
6720
Nadav Amit4161a562014-07-17 01:19:31 +03006721 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6722 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006723 unsigned long eip = kvm_get_linear_rip(vcpu);
6724 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006725 vcpu->arch.dr7,
6726 vcpu->arch.db);
6727
6728 if (dr6 != 0) {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04006729 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006730 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006731 return true;
6732 }
6733 }
6734
6735 return false;
6736}
6737
Liran Alon04789b62018-03-12 13:12:50 +02006738static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6739{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006740 switch (ctxt->opcode_len) {
6741 case 1:
6742 switch (ctxt->b) {
6743 case 0xe4: /* IN */
6744 case 0xe5:
6745 case 0xec:
6746 case 0xed:
6747 case 0xe6: /* OUT */
6748 case 0xe7:
6749 case 0xee:
6750 case 0xef:
6751 case 0x6c: /* INS */
6752 case 0x6d:
6753 case 0x6e: /* OUTS */
6754 case 0x6f:
6755 return true;
6756 }
6757 break;
6758 case 2:
6759 switch (ctxt->b) {
6760 case 0x33: /* RDPMC */
6761 return true;
6762 }
6763 break;
Liran Alon04789b62018-03-12 13:12:50 +02006764 }
6765
6766 return false;
6767}
6768
Sean Christopherson736c2912019-12-06 15:57:14 -08006769int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6770 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006771{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006772 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006773 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006774 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006775 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006776
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006777 vcpu->arch.l1tf_flush_l1d = true;
6778
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006779 /*
6780 * Clear write_fault_to_shadow_pgtable here to ensure it is
6781 * never reused.
6782 */
6783 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006784 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006785
Sheng Yang571008d2008-01-02 14:49:22 +08006786 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006787 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006788
6789 /*
6790 * We will reenter on the same instruction since
6791 * we do not set complete_userspace_io. This does not
6792 * handle watchpoints yet, those would be handled in
6793 * the emulate_ops.
6794 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006795 if (!(emulation_type & EMULTYPE_SKIP) &&
6796 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006797 return r;
6798
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006799 ctxt->interruptibility = 0;
6800 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006801 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006802 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006803
Borislav Petkovb51e9742013-09-22 16:44:52 +02006804 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006805
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006806 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006807
Avi Kivitye46479f2010-04-11 13:05:16 +03006808 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006809 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006810 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006811 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006812 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6813 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006814 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006815 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006816 if (reexecute_instruction(vcpu, cr2_or_gpa,
6817 write_fault_to_spt,
6818 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006819 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006820 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006821 /*
6822 * #UD should result in just EMULATION_FAILED, and trap-like
6823 * exception should not be encountered during decode.
6824 */
6825 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6826 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006827 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006828 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006829 }
Liran Alone2366172018-03-12 13:12:49 +02006830 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006831 }
6832 }
6833
Sean Christopherson42cbf062019-08-27 14:40:31 -07006834 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6835 !is_vmware_backdoor_opcode(ctxt)) {
6836 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006837 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006838 }
Liran Alon04789b62018-03-12 13:12:50 +02006839
Sean Christopherson1957aa62019-08-27 14:40:39 -07006840 /*
6841 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6842 * for kvm_skip_emulated_instruction(). The caller is responsible for
6843 * updating interruptibility state and injecting single-step #DBs.
6844 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006845 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006846 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006847 if (ctxt->eflags & X86_EFLAGS_RF)
6848 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006849 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006850 }
6851
Sean Christopherson736c2912019-12-06 15:57:14 -08006852 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006853 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006854
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006855 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006856 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006857 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6858 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006859 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006860 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006861
Gleb Natapov5cd21912010-03-18 15:20:26 +02006862restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006863 if (emulation_type & EMULTYPE_PF) {
6864 /* Save the faulting GPA (cr2) in the address field */
6865 ctxt->exception.address = cr2_or_gpa;
6866
6867 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6868 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006869 ctxt->gpa_available = true;
6870 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006871 }
6872 } else {
6873 /* Sanitize the address out of an abundance of paranoia. */
6874 ctxt->exception.address = 0;
6875 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006876
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006877 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006878
Joerg Roedel775fde82011-04-04 12:39:24 +02006879 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006880 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006881
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006882 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006883 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006884 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006885 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006886
Liran Alone2366172018-03-12 13:12:49 +02006887 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006888 }
6889
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006890 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006891 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006892 if (inject_emulated_exception(vcpu))
6893 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006894 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006895 if (!vcpu->arch.pio.in) {
6896 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006897 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006898 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006899 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006900 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6901 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006902 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006903 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006904 ++vcpu->stat.mmio_exits;
6905
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006906 if (!vcpu->mmio_is_write)
6907 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006908 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006909 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006910 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006911 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006912 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006913 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006914
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006915 if (writeback) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006916 unsigned long rflags = kvm_x86_ops.get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006917 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006918 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006919 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006920 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6921 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006922 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006923 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07006924 if (kvm_x86_ops.update_emulated_instruction)
6925 kvm_x86_ops.update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006926 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006927 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006928
6929 /*
6930 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6931 * do nothing, and it will be requested again as soon as
6932 * the shadow expires. But we still need to check here,
6933 * because POPF has no interrupt shadow.
6934 */
6935 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6936 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006937 } else
6938 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006939
Gleb Natapove85d28f2010-07-29 15:11:52 +03006940 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006941}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006942
6943int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6944{
6945 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6946}
6947EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6948
6949int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6950 void *insn, int insn_len)
6951{
6952 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6953}
6954EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006955
Sean Christopherson8764ed52019-04-29 07:04:15 -07006956static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6957{
6958 vcpu->arch.pio.count = 0;
6959 return 1;
6960}
6961
Sean Christopherson45def772019-03-11 20:01:05 -07006962static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6963{
6964 vcpu->arch.pio.count = 0;
6965
6966 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6967 return 1;
6968
6969 return kvm_skip_emulated_instruction(vcpu);
6970}
6971
Sean Christophersondca7f122018-03-08 08:57:27 -08006972static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6973 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006974{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006975 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006976 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6977
Sean Christopherson8764ed52019-04-29 07:04:15 -07006978 if (ret)
6979 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006980
Sean Christopherson8764ed52019-04-29 07:04:15 -07006981 /*
6982 * Workaround userspace that relies on old KVM behavior of %rip being
6983 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6984 */
6985 if (port == 0x7e &&
6986 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6987 vcpu->arch.complete_userspace_io =
6988 complete_fast_pio_out_port_0x7e;
6989 kvm_skip_emulated_instruction(vcpu);
6990 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006991 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6992 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6993 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006994 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006995}
Carsten Ottede7d7892007-10-30 18:44:25 +01006996
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006997static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6998{
6999 unsigned long val;
7000
7001 /* We should only ever be called with arch.pio.count equal to 1 */
7002 BUG_ON(vcpu->arch.pio.count != 1);
7003
Sean Christopherson45def772019-03-11 20:01:05 -07007004 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
7005 vcpu->arch.pio.count = 0;
7006 return 1;
7007 }
7008
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007009 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007010 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007011
7012 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007013 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007014 * the copy and tracing
7015 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007016 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07007017 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007018
Sean Christopherson45def772019-03-11 20:01:05 -07007019 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007020}
7021
Sean Christophersondca7f122018-03-08 08:57:27 -08007022static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
7023 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007024{
7025 unsigned long val;
7026 int ret;
7027
7028 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07007029 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007030
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08007031 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007032 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007033 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007034 return ret;
7035 }
7036
Sean Christopherson45def772019-03-11 20:01:05 -07007037 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007038 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7039
7040 return 0;
7041}
Sean Christophersondca7f122018-03-08 08:57:27 -08007042
7043int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7044{
Sean Christopherson45def772019-03-11 20:01:05 -07007045 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007046
Sean Christophersondca7f122018-03-08 08:57:27 -08007047 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007048 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007049 else
Sean Christopherson45def772019-03-11 20:01:05 -07007050 ret = kvm_fast_pio_out(vcpu, size, port);
7051 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007052}
7053EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007054
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007055static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007056{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007057 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007058 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007059}
7060
7061static void tsc_khz_changed(void *data)
7062{
7063 struct cpufreq_freqs *freq = data;
7064 unsigned long khz = 0;
7065
7066 if (data)
7067 khz = freq->new;
7068 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7069 khz = cpufreq_quick_get(raw_smp_processor_id());
7070 if (!khz)
7071 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007072 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007073}
7074
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007075#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007076static void kvm_hyperv_tsc_notifier(void)
7077{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007078 struct kvm *kvm;
7079 struct kvm_vcpu *vcpu;
7080 int cpu;
7081
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007082 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007083 list_for_each_entry(kvm, &vm_list, vm_list)
7084 kvm_make_mclock_inprogress_request(kvm);
7085
7086 hyperv_stop_tsc_emulation();
7087
7088 /* TSC frequency always matches when on Hyper-V */
7089 for_each_present_cpu(cpu)
7090 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7091 kvm_max_guest_tsc_khz = tsc_khz;
7092
7093 list_for_each_entry(kvm, &vm_list, vm_list) {
7094 struct kvm_arch *ka = &kvm->arch;
7095
7096 spin_lock(&ka->pvclock_gtod_sync_lock);
7097
7098 pvclock_update_vm_gtod_copy(kvm);
7099
7100 kvm_for_each_vcpu(cpu, vcpu, kvm)
7101 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7102
7103 kvm_for_each_vcpu(cpu, vcpu, kvm)
7104 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7105
7106 spin_unlock(&ka->pvclock_gtod_sync_lock);
7107 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007108 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007109}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007110#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007111
Viresh Kumardf240142019-04-29 15:03:58 +05307112static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007113{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007114 struct kvm *kvm;
7115 struct kvm_vcpu *vcpu;
7116 int i, send_ipi = 0;
7117
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007118 /*
7119 * We allow guests to temporarily run on slowing clocks,
7120 * provided we notify them after, or to run on accelerating
7121 * clocks, provided we notify them before. Thus time never
7122 * goes backwards.
7123 *
7124 * However, we have a problem. We can't atomically update
7125 * the frequency of a given CPU from this function; it is
7126 * merely a notifier, which can be called from any CPU.
7127 * Changing the TSC frequency at arbitrary points in time
7128 * requires a recomputation of local variables related to
7129 * the TSC for each VCPU. We must flag these local variables
7130 * to be updated and be sure the update takes place with the
7131 * new frequency before any guests proceed.
7132 *
7133 * Unfortunately, the combination of hotplug CPU and frequency
7134 * change creates an intractable locking scenario; the order
7135 * of when these callouts happen is undefined with respect to
7136 * CPU hotplug, and they can race with each other. As such,
7137 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7138 * undefined; you can actually have a CPU frequency change take
7139 * place in between the computation of X and the setting of the
7140 * variable. To protect against this problem, all updates of
7141 * the per_cpu tsc_khz variable are done in an interrupt
7142 * protected IPI, and all callers wishing to update the value
7143 * must wait for a synchronous IPI to complete (which is trivial
7144 * if the caller is on the CPU already). This establishes the
7145 * necessary total order on variable updates.
7146 *
7147 * Note that because a guest time update may take place
7148 * anytime after the setting of the VCPU's request bit, the
7149 * correct TSC value must be set before the request. However,
7150 * to ensure the update actually makes it to any guest which
7151 * starts running in hardware virtualization between the set
7152 * and the acquisition of the spinlock, we must also ping the
7153 * CPU after setting the request bit.
7154 *
7155 */
7156
Viresh Kumardf240142019-04-29 15:03:58 +05307157 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007158
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007159 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007160 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007161 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307162 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007163 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007164 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007165 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007166 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007167 }
7168 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007169 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007170
7171 if (freq->old < freq->new && send_ipi) {
7172 /*
7173 * We upscale the frequency. Must make the guest
7174 * doesn't see old kvmclock values while running with
7175 * the new frequency, otherwise we risk the guest sees
7176 * time go backwards.
7177 *
7178 * In case we update the frequency for another cpu
7179 * (which might be in guest context) send an interrupt
7180 * to kick the cpu out of guest context. Next time
7181 * guest context is entered kvmclock will be updated,
7182 * so the guest will not see stale values.
7183 */
Viresh Kumardf240142019-04-29 15:03:58 +05307184 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007185 }
Viresh Kumardf240142019-04-29 15:03:58 +05307186}
7187
7188static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7189 void *data)
7190{
7191 struct cpufreq_freqs *freq = data;
7192 int cpu;
7193
7194 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7195 return 0;
7196 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7197 return 0;
7198
7199 for_each_cpu(cpu, freq->policy->cpus)
7200 __kvmclock_cpufreq_notifier(freq, cpu);
7201
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007202 return 0;
7203}
7204
7205static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007206 .notifier_call = kvmclock_cpufreq_notifier
7207};
7208
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007209static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007210{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007211 tsc_khz_changed(NULL);
7212 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007213}
7214
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007215static void kvm_timer_init(void)
7216{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007217 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307218
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007219 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007220#ifdef CONFIG_CPU_FREQ
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007221 struct cpufreq_policy *policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007222 int cpu;
7223
Avi Kivity3e26f232010-12-16 12:16:34 +02007224 cpu = get_cpu();
Paolo Bonziniaaec7c02020-02-28 10:49:10 +01007225 policy = cpufreq_cpu_get(cpu);
Wanpeng Li9a119972020-03-02 15:15:36 +08007226 if (policy) {
7227 if (policy->cpuinfo.max_freq)
7228 max_tsc_khz = policy->cpuinfo.max_freq;
7229 cpufreq_cpu_put(policy);
7230 }
Avi Kivity3e26f232010-12-16 12:16:34 +02007231 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007232#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007233 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7234 CPUFREQ_TRANSITION_NOTIFIER);
7235 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307236
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007237 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007238 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007239}
7240
Andi Kleendd60d212017-07-25 17:20:32 -07007241DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7242EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007243
Gleb Natapovf5132b02011-11-10 14:57:22 +02007244int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007245{
Alex,Shi086c9852011-10-20 15:34:01 +08007246 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007247}
7248
7249static int kvm_is_user_mode(void)
7250{
7251 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007252
Alex,Shi086c9852011-10-20 15:34:01 +08007253 if (__this_cpu_read(current_vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007254 user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007255
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007256 return user_mode != 0;
7257}
7258
7259static unsigned long kvm_get_guest_ip(void)
7260{
7261 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007262
Alex,Shi086c9852011-10-20 15:34:01 +08007263 if (__this_cpu_read(current_vcpu))
7264 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007265
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007266 return ip;
7267}
7268
Luwei Kang8479e042019-02-18 19:26:07 -05007269static void kvm_handle_intel_pt_intr(void)
7270{
7271 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7272
7273 kvm_make_request(KVM_REQ_PMI, vcpu);
7274 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7275 (unsigned long *)&vcpu->arch.pmu.global_status);
7276}
7277
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007278static struct perf_guest_info_callbacks kvm_guest_cbs = {
7279 .is_in_guest = kvm_is_in_guest,
7280 .is_user_mode = kvm_is_user_mode,
7281 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007282 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007283};
7284
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007285#ifdef CONFIG_X86_64
7286static void pvclock_gtod_update_fn(struct work_struct *work)
7287{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007288 struct kvm *kvm;
7289
7290 struct kvm_vcpu *vcpu;
7291 int i;
7292
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007293 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007294 list_for_each_entry(kvm, &vm_list, vm_list)
7295 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007296 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007297 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007298 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007299}
7300
7301static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7302
7303/*
7304 * Notification about pvclock gtod data update.
7305 */
7306static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7307 void *priv)
7308{
7309 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7310 struct timekeeper *tk = priv;
7311
7312 update_pvclock_gtod(tk);
7313
7314 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007315 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007316 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007317 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007318 atomic_read(&kvm_guest_has_master_clock) != 0)
7319 queue_work(system_long_wq, &pvclock_gtod_work);
7320
7321 return 0;
7322}
7323
7324static struct notifier_block pvclock_gtod_notifier = {
7325 .notifier_call = pvclock_gtod_notify,
7326};
7327#endif
7328
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007329int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007330{
Sean Christophersond008dfd2020-03-21 13:25:56 -07007331 struct kvm_x86_init_ops *ops = opaque;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007332 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007333
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007334 if (kvm_x86_ops.hardware_enable) {
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007335 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007336 r = -EEXIST;
7337 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007338 }
7339
7340 if (!ops->cpu_has_kvm_support()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007341 pr_err_ratelimited("kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007342 r = -EOPNOTSUPP;
7343 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007344 }
7345 if (ops->disabled_by_bios()) {
Erwan Veluef935c252020-02-27 19:00:46 +01007346 pr_err_ratelimited("kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007347 r = -EOPNOTSUPP;
7348 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007349 }
7350
Marc Orrb666a4b2018-11-06 14:53:56 -08007351 /*
7352 * KVM explicitly assumes that the guest has an FPU and
7353 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7354 * vCPU's FPU state as a fxregs_state struct.
7355 */
7356 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7357 printk(KERN_ERR "kvm: inadequate fpu\n");
7358 r = -EOPNOTSUPP;
7359 goto out;
7360 }
7361
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007362 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007363 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007364 __alignof__(struct fpu), SLAB_ACCOUNT,
7365 NULL);
7366 if (!x86_fpu_cache) {
7367 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7368 goto out;
7369 }
7370
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007371 x86_emulator_cache = kvm_alloc_emulator_cache();
7372 if (!x86_emulator_cache) {
7373 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7374 goto out_free_x86_fpu_cache;
7375 }
7376
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007377 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7378 if (!shared_msrs) {
7379 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007380 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007381 }
7382
Avi Kivity97db56c2008-01-13 13:23:56 +02007383 r = kvm_mmu_module_init();
7384 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007385 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007386
Sheng Yang7b523452008-04-25 21:13:50 +08007387 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007388 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007389 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007390 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007391
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007392 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7393
Sean Christophersoncfc48182020-03-02 15:56:23 -08007394 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007395 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007396 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7397 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007398
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007399 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007400 if (pi_inject_timer == -1)
7401 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007402#ifdef CONFIG_X86_64
7403 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007404
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007405 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007406 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007407#endif
7408
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007409 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007410
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007411out_free_percpu:
7412 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007413out_free_x86_emulator_cache:
7414 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007415out_free_x86_fpu_cache:
7416 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007417out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007418 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007419}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007420
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007421void kvm_arch_exit(void)
7422{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007423#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007424 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007425 clear_hv_tscchange_cb();
7426#endif
David Matlackcef84c32016-12-16 14:30:36 -08007427 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007428 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7429
Jan Kiszka888d2562009-04-17 19:24:58 +02007430 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7431 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7432 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007433 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007434#ifdef CONFIG_X86_64
7435 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7436#endif
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007437 kvm_x86_ops.hardware_enable = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007438 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007439 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007440 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007441}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007442
Joel Schopp5cb56052015-03-02 13:43:31 -06007443int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007444{
7445 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007446 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007447 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007448 return 1;
7449 } else {
7450 vcpu->run->exit_reason = KVM_EXIT_HLT;
7451 return 0;
7452 }
7453}
Joel Schopp5cb56052015-03-02 13:43:31 -06007454EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7455
7456int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7457{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007458 int ret = kvm_skip_emulated_instruction(vcpu);
7459 /*
7460 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7461 * KVM_EXIT_DEBUG here.
7462 */
7463 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007464}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007465EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7466
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007467#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007468static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7469 unsigned long clock_type)
7470{
7471 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007472 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007473 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007474 int ret;
7475
7476 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7477 return -KVM_EOPNOTSUPP;
7478
7479 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7480 return -KVM_EOPNOTSUPP;
7481
7482 clock_pairing.sec = ts.tv_sec;
7483 clock_pairing.nsec = ts.tv_nsec;
7484 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7485 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007486 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007487
7488 ret = 0;
7489 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7490 sizeof(struct kvm_clock_pairing)))
7491 ret = -KVM_EFAULT;
7492
7493 return ret;
7494}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007495#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007496
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307497/*
7498 * kvm_pv_kick_cpu_op: Kick a vcpu.
7499 *
7500 * @apicid - apicid of vcpu to be kicked.
7501 */
7502static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7503{
Raghavendra K T24d21662013-08-26 14:18:35 +05307504 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307505
Peter Xu150a84f2019-12-04 20:07:21 +01007506 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007507 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007508 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307509 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007510 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307511
Raghavendra K T24d21662013-08-26 14:18:35 +05307512 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007513 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307514}
7515
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007516bool kvm_apicv_activated(struct kvm *kvm)
7517{
7518 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7519}
7520EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7521
7522void kvm_apicv_init(struct kvm *kvm, bool enable)
7523{
7524 if (enable)
7525 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7526 &kvm->arch.apicv_inhibit_reasons);
7527 else
7528 set_bit(APICV_INHIBIT_REASON_DISABLE,
7529 &kvm->arch.apicv_inhibit_reasons);
7530}
7531EXPORT_SYMBOL_GPL(kvm_apicv_init);
7532
Wanpeng Li71506292019-06-11 20:23:49 +08007533static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7534{
7535 struct kvm_vcpu *target = NULL;
7536 struct kvm_apic_map *map;
7537
7538 rcu_read_lock();
7539 map = rcu_dereference(kvm->arch.apic_map);
7540
7541 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7542 target = map->phys_map[dest_id]->vcpu;
7543
7544 rcu_read_unlock();
7545
Wanpeng Li266e85a2019-07-24 17:43:13 +08007546 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007547 kvm_vcpu_yield_to(target);
7548}
7549
Hollis Blanchard8776e512007-10-31 17:24:24 -05007550int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7551{
7552 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007553 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007554
Radim Krčmář696ca772018-05-24 17:50:56 +02007555 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7556 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007557
Sean Christophersonde3cd112019-04-30 10:36:17 -07007558 nr = kvm_rax_read(vcpu);
7559 a0 = kvm_rbx_read(vcpu);
7560 a1 = kvm_rcx_read(vcpu);
7561 a2 = kvm_rdx_read(vcpu);
7562 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007563
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007564 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007565
Nadav Amita449c7a2014-06-18 17:19:24 +03007566 op_64_bit = is_64_bit_mode(vcpu);
7567 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007568 nr &= 0xFFFFFFFF;
7569 a0 &= 0xFFFFFFFF;
7570 a1 &= 0xFFFFFFFF;
7571 a2 &= 0xFFFFFFFF;
7572 a3 &= 0xFFFFFFFF;
7573 }
7574
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007575 if (kvm_x86_ops.get_cpl(vcpu) != 0) {
Jan Kiszka07708c42009-08-03 18:43:28 +02007576 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007577 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007578 }
7579
Hollis Blanchard8776e512007-10-31 17:24:24 -05007580 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007581 case KVM_HC_VAPIC_POLL_IRQ:
7582 ret = 0;
7583 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307584 case KVM_HC_KICK_CPU:
7585 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007586 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307587 ret = 0;
7588 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007589#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007590 case KVM_HC_CLOCK_PAIRING:
7591 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7592 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007593#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007594 case KVM_HC_SEND_IPI:
7595 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7596 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007597 case KVM_HC_SCHED_YIELD:
7598 kvm_sched_yield(vcpu->kvm, a0);
7599 ret = 0;
7600 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007601 default:
7602 ret = -KVM_ENOSYS;
7603 break;
7604 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007605out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007606 if (!op_64_bit)
7607 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007608 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007609
Amit Shahf11c3a82008-02-21 01:00:30 +05307610 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007611 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007612}
7613EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7614
Jan Kiszkab6785de2012-09-20 07:43:17 +02007615static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007616{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007617 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007618 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007619 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007620
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007621 kvm_x86_ops.patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007622
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007623 return emulator_write_emulated(ctxt, rip, instruction, 3,
7624 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007625}
7626
Avi Kivity851ba692009-08-24 11:10:17 +03007627static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007628{
Matt Gingell782d4222015-11-16 15:26:00 -08007629 return vcpu->run->request_interrupt_window &&
7630 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007631}
7632
Avi Kivity851ba692009-08-24 11:10:17 +03007633static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007634{
Avi Kivity851ba692009-08-24 11:10:17 +03007635 struct kvm_run *kvm_run = vcpu->run;
7636
Jan Kiszka91586a32009-10-05 13:07:21 +02007637 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007638 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007639 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007640 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007641 kvm_run->ready_for_interrupt_injection =
7642 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007643 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007644}
7645
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007646static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7647{
7648 int max_irr, tpr;
7649
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007650 if (!kvm_x86_ops.update_cr8_intercept)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007651 return;
7652
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007653 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007654 return;
7655
Andrey Smetanind62caab2015-11-10 15:36:33 +03007656 if (vcpu->arch.apicv_active)
7657 return;
7658
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007659 if (!vcpu->arch.apic->vapic_addr)
7660 max_irr = kvm_lapic_find_highest_irr(vcpu);
7661 else
7662 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007663
7664 if (max_irr != -1)
7665 max_irr >>= 4;
7666
7667 tpr = kvm_lapic_get_cr8(vcpu);
7668
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007669 kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007670}
7671
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007672static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007673{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007674 int r;
7675
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007676 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007677
Liran Alon1a680e32018-03-23 03:01:33 +03007678 if (vcpu->arch.exception.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007679 kvm_x86_ops.queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007680 /*
Liran Alona042c262018-03-23 03:01:32 +03007681 * Do not inject an NMI or interrupt if there is a pending
7682 * exception. Exceptions and interrupts are recognized at
7683 * instruction boundaries, i.e. the start of an instruction.
7684 * Trap-like exceptions, e.g. #DB, have higher priority than
7685 * NMIs and interrupts, i.e. traps are recognized before an
7686 * NMI/interrupt that's pending on the same instruction.
7687 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7688 * priority, but are only generated (pended) during instruction
7689 * execution, i.e. a pending fault-like exception means the
7690 * fault occurred on the *previous* instruction and must be
7691 * serviced prior to recognizing any new events in order to
7692 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007693 */
Liran Alon1a680e32018-03-23 03:01:33 +03007694 else if (!vcpu->arch.exception.pending) {
7695 if (vcpu->arch.nmi_injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007696 kvm_x86_ops.set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007697 else if (vcpu->arch.interrupt.injected)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007698 kvm_x86_ops.set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007699 }
7700
Liran Alon1a680e32018-03-23 03:01:33 +03007701 /*
7702 * Call check_nested_events() even if we reinjected a previous event
7703 * in order for caller to determine if it should require immediate-exit
7704 * from L2 to L1 due to pending L1 events which require exit
7705 * from L2 to L1.
7706 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007707 if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events) {
7708 r = kvm_x86_ops.check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007709 if (r != 0)
7710 return r;
7711 }
7712
7713 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007714 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007715 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7716 vcpu->arch.exception.has_error_code,
7717 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007718
Liran Alon1a680e32018-03-23 03:01:33 +03007719 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007720 vcpu->arch.exception.pending = false;
7721 vcpu->arch.exception.injected = true;
7722
Nadav Amitd6e8c852014-07-24 14:51:24 +03007723 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7724 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7725 X86_EFLAGS_RF);
7726
Jim Mattsonf10c7292018-10-16 14:29:23 -07007727 if (vcpu->arch.exception.nr == DB_VECTOR) {
7728 /*
7729 * This code assumes that nSVM doesn't use
7730 * check_nested_events(). If it does, the
7731 * DR6/DR7 changes should happen before L1
7732 * gets a #VMEXIT for an intercepted #DB in
7733 * L2. (Under VMX, on the other hand, the
7734 * DR6/DR7 changes should not happen in the
7735 * event of a VM-exit to L1 for an intercepted
7736 * #DB in L2.)
7737 */
7738 kvm_deliver_exception_payload(vcpu);
7739 if (vcpu->arch.dr7 & DR7_GD) {
7740 vcpu->arch.dr7 &= ~DR7_GD;
7741 kvm_update_dr7(vcpu);
7742 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007743 }
7744
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007745 kvm_x86_ops.queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007746 }
7747
7748 /* Don't consider new event if we re-injected an event */
7749 if (kvm_event_needs_reinjection(vcpu))
7750 return 0;
7751
7752 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007753 kvm_x86_ops.smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007754 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007755 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007756 enter_smm(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007757 } else if (vcpu->arch.nmi_pending && kvm_x86_ops.nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007758 --vcpu->arch.nmi_pending;
7759 vcpu->arch.nmi_injected = true;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007760 kvm_x86_ops.set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007761 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007762 /*
7763 * Because interrupts can be injected asynchronously, we are
7764 * calling check_nested_events again here to avoid a race condition.
7765 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7766 * proposal and current concerns. Perhaps we should be setting
7767 * KVM_REQ_EVENT only on certain events and not unconditionally?
7768 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007769 if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events) {
7770 r = kvm_x86_ops.check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007771 if (r != 0)
7772 return r;
7773 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007774 if (kvm_x86_ops.interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007775 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7776 false);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007777 kvm_x86_ops.set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007778 }
7779 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007780
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007781 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007782}
7783
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007784static void process_nmi(struct kvm_vcpu *vcpu)
7785{
7786 unsigned limit = 2;
7787
7788 /*
7789 * x86 is limited to one NMI running, and one NMI pending after it.
7790 * If an NMI is already in progress, limit further NMIs to just one.
7791 * Otherwise, allow two (and we'll inject the first one immediately).
7792 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007793 if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007794 limit = 1;
7795
7796 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7797 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7798 kvm_make_request(KVM_REQ_EVENT, vcpu);
7799}
7800
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007801static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007802{
7803 u32 flags = 0;
7804 flags |= seg->g << 23;
7805 flags |= seg->db << 22;
7806 flags |= seg->l << 21;
7807 flags |= seg->avl << 20;
7808 flags |= seg->present << 15;
7809 flags |= seg->dpl << 13;
7810 flags |= seg->s << 12;
7811 flags |= seg->type << 8;
7812 return flags;
7813}
7814
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007815static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007816{
7817 struct kvm_segment seg;
7818 int offset;
7819
7820 kvm_get_segment(vcpu, &seg, n);
7821 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7822
7823 if (n < 3)
7824 offset = 0x7f84 + n * 12;
7825 else
7826 offset = 0x7f2c + (n - 3) * 12;
7827
7828 put_smstate(u32, buf, offset + 8, seg.base);
7829 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007830 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007831}
7832
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007833#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007834static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007835{
7836 struct kvm_segment seg;
7837 int offset;
7838 u16 flags;
7839
7840 kvm_get_segment(vcpu, &seg, n);
7841 offset = 0x7e00 + n * 16;
7842
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007843 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007844 put_smstate(u16, buf, offset, seg.selector);
7845 put_smstate(u16, buf, offset + 2, flags);
7846 put_smstate(u32, buf, offset + 4, seg.limit);
7847 put_smstate(u64, buf, offset + 8, seg.base);
7848}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007849#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007850
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007851static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007852{
7853 struct desc_ptr dt;
7854 struct kvm_segment seg;
7855 unsigned long val;
7856 int i;
7857
7858 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7859 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7860 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7861 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7862
7863 for (i = 0; i < 8; i++)
7864 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7865
7866 kvm_get_dr(vcpu, 6, &val);
7867 put_smstate(u32, buf, 0x7fcc, (u32)val);
7868 kvm_get_dr(vcpu, 7, &val);
7869 put_smstate(u32, buf, 0x7fc8, (u32)val);
7870
7871 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7872 put_smstate(u32, buf, 0x7fc4, seg.selector);
7873 put_smstate(u32, buf, 0x7f64, seg.base);
7874 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007875 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007876
7877 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7878 put_smstate(u32, buf, 0x7fc0, seg.selector);
7879 put_smstate(u32, buf, 0x7f80, seg.base);
7880 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007881 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007882
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007883 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007884 put_smstate(u32, buf, 0x7f74, dt.address);
7885 put_smstate(u32, buf, 0x7f70, dt.size);
7886
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007887 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007888 put_smstate(u32, buf, 0x7f58, dt.address);
7889 put_smstate(u32, buf, 0x7f54, dt.size);
7890
7891 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007892 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007893
7894 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7895
7896 /* revision id */
7897 put_smstate(u32, buf, 0x7efc, 0x00020000);
7898 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7899}
7900
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007901#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007902static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007903{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007904 struct desc_ptr dt;
7905 struct kvm_segment seg;
7906 unsigned long val;
7907 int i;
7908
7909 for (i = 0; i < 16; i++)
7910 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7911
7912 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7913 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7914
7915 kvm_get_dr(vcpu, 6, &val);
7916 put_smstate(u64, buf, 0x7f68, val);
7917 kvm_get_dr(vcpu, 7, &val);
7918 put_smstate(u64, buf, 0x7f60, val);
7919
7920 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7921 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7922 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7923
7924 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7925
7926 /* revision id */
7927 put_smstate(u32, buf, 0x7efc, 0x00020064);
7928
7929 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7930
7931 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7932 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007933 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007934 put_smstate(u32, buf, 0x7e94, seg.limit);
7935 put_smstate(u64, buf, 0x7e98, seg.base);
7936
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007937 kvm_x86_ops.get_idt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007938 put_smstate(u32, buf, 0x7e84, dt.size);
7939 put_smstate(u64, buf, 0x7e88, dt.address);
7940
7941 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7942 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007943 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007944 put_smstate(u32, buf, 0x7e74, seg.limit);
7945 put_smstate(u64, buf, 0x7e78, seg.base);
7946
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007947 kvm_x86_ops.get_gdt(vcpu, &dt);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007948 put_smstate(u32, buf, 0x7e64, dt.size);
7949 put_smstate(u64, buf, 0x7e68, dt.address);
7950
7951 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007952 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007953}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007954#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007955
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007956static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007957{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007958 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007959 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007960 char buf[512];
7961 u32 cr0;
7962
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007963 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007964 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007965#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007966 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007967 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007968 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007969#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007970 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007971
Ladi Prosek0234bf82017-10-11 16:54:40 +02007972 /*
7973 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7974 * vCPU state (e.g. leave guest mode) after we've saved the state into
7975 * the SMM state-save area.
7976 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007977 kvm_x86_ops.pre_enter_smm(vcpu, buf);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007978
7979 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007980 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007981
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007982 if (kvm_x86_ops.get_nmi_mask(vcpu))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007983 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7984 else
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007985 kvm_x86_ops.set_nmi_mask(vcpu, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007986
7987 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7988 kvm_rip_write(vcpu, 0x8000);
7989
7990 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007991 kvm_x86_ops.set_cr0(vcpu, cr0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007992 vcpu->arch.cr0 = cr0;
7993
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007994 kvm_x86_ops.set_cr4(vcpu, 0);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007995
Paolo Bonzini18c36262015-08-07 12:27:54 +02007996 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7997 dt.address = dt.size = 0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007998 kvm_x86_ops.set_idt(vcpu, &dt);
Paolo Bonzini18c36262015-08-07 12:27:54 +02007999
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008000 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
8001
8002 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
8003 cs.base = vcpu->arch.smbase;
8004
8005 ds.selector = 0;
8006 ds.base = 0;
8007
8008 cs.limit = ds.limit = 0xffffffff;
8009 cs.type = ds.type = 0x3;
8010 cs.dpl = ds.dpl = 0;
8011 cs.db = ds.db = 0;
8012 cs.s = ds.s = 1;
8013 cs.l = ds.l = 0;
8014 cs.g = ds.g = 1;
8015 cs.avl = ds.avl = 0;
8016 cs.present = ds.present = 1;
8017 cs.unusable = ds.unusable = 0;
8018 cs.padding = ds.padding = 0;
8019
8020 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
8021 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
8022 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
8023 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
8024 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
8025 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
8026
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008027#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02008028 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008029 kvm_x86_ops.set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07008030#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02008031
8032 kvm_update_cpuid(vcpu);
8033 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008034}
8035
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008036static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008037{
8038 vcpu->arch.smi_pending = true;
8039 kvm_make_request(KVM_REQ_EVENT, vcpu);
8040}
8041
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008042void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8043 unsigned long *vcpu_bitmap)
8044{
8045 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008046
8047 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8048
Mao Wenandb5a95e2019-11-19 11:06:40 +08008049 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05008050 NULL, vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008051
8052 free_cpumask_var(cpus);
8053}
8054
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008055void kvm_make_scan_ioapic_request(struct kvm *kvm)
8056{
8057 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8058}
8059
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008060void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8061{
8062 if (!lapic_in_kernel(vcpu))
8063 return;
8064
8065 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8066 kvm_apic_update_apicv(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008067 kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008068}
8069EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8070
8071/*
8072 * NOTE: Do not hold any lock prior to calling this.
8073 *
8074 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8075 * locked, because it calls __x86_set_memory_region() which does
8076 * synchronize_srcu(&kvm->srcu).
8077 */
8078void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8079{
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008080 struct kvm_vcpu *except;
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008081 unsigned long old, new, expected;
8082
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008083 if (!kvm_x86_ops.check_apicv_inhibit_reasons ||
8084 !kvm_x86_ops.check_apicv_inhibit_reasons(bit))
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008085 return;
8086
Paolo Bonzini8e205a62020-03-14 12:29:23 +01008087 old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
8088 do {
8089 expected = new = old;
8090 if (activate)
8091 __clear_bit(bit, &new);
8092 else
8093 __set_bit(bit, &new);
8094 if (new == old)
8095 break;
8096 old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
8097 } while (old != expected);
8098
8099 if (!!old == !!new)
8100 return;
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008101
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008102 trace_kvm_apicv_update_request(activate, bit);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008103 if (kvm_x86_ops.pre_update_apicv_exec_ctrl)
8104 kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit7d611232020-05-06 21:35:39 -05008105
8106 /*
8107 * Sending request to update APICV for all other vcpus,
8108 * while update the calling vcpu immediately instead of
8109 * waiting for another #VMEXIT to handle the request.
8110 */
8111 except = kvm_get_running_vcpu();
8112 kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE,
8113 except);
8114 if (except)
8115 kvm_vcpu_update_apicv(except);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008116}
8117EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8118
Yang Zhang3d81bc72013-04-11 19:25:13 +08008119static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008120{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008121 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008122 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008123
Andrey Smetanin63086302015-11-10 15:36:32 +03008124 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008125
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008126 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008127 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008128 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008129 if (vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008130 kvm_x86_ops.sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008131 if (ioapic_in_kernel(vcpu->kvm))
8132 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008133 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008134
8135 if (is_guest_mode(vcpu))
8136 vcpu->arch.load_eoi_exitmap_pending = true;
8137 else
8138 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8139}
8140
8141static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8142{
8143 u64 eoi_exit_bitmap[4];
8144
8145 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8146 return;
8147
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008148 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8149 vcpu_to_synic(vcpu)->vec_bitmap, 256);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008150 kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008151}
8152
Michal Hocko93065ac2018-08-21 21:52:33 -07008153int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8154 unsigned long start, unsigned long end,
8155 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008156{
8157 unsigned long apic_address;
8158
8159 /*
8160 * The physical address of apic access page is stored in the VMCS.
8161 * Update it when it becomes invalid.
8162 */
8163 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8164 if (start <= apic_address && apic_address < end)
8165 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008166
8167 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008168}
8169
Tang Chen4256f432014-09-24 15:57:54 +08008170void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8171{
Tang Chenc24ae0d2014-09-24 15:57:58 +08008172 struct page *page = NULL;
8173
Paolo Bonzini35754c92015-07-29 12:05:37 +02008174 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008175 return;
8176
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008177 if (!kvm_x86_ops.set_apic_access_page_addr)
Tang Chen4256f432014-09-24 15:57:54 +08008178 return;
8179
Tang Chenc24ae0d2014-09-24 15:57:58 +08008180 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02008181 if (is_error_page(page))
8182 return;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008183 kvm_x86_ops.set_apic_access_page_addr(vcpu, page_to_phys(page));
Tang Chenc24ae0d2014-09-24 15:57:58 +08008184
8185 /*
8186 * Do not pin apic access page in memory, the MMU notifier
8187 * will call us again if it is migrated or swapped out.
8188 */
8189 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08008190}
Tang Chen4256f432014-09-24 15:57:54 +08008191
Sean Christophersond264ee02018-08-27 15:21:12 -07008192void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8193{
8194 smp_send_reschedule(vcpu->cpu);
8195}
8196EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8197
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008198/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008199 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008200 * exiting to the userspace. Otherwise, the value will be returned to the
8201 * userspace.
8202 */
Avi Kivity851ba692009-08-24 11:10:17 +03008203static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008204{
8205 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008206 bool req_int_win =
8207 dm_request_for_irq_injection(vcpu) &&
8208 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008209 enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE;
Matt Gingell62a193e2015-11-16 15:26:07 -08008210
Jan Kiszka730dca42013-04-28 10:50:52 +02008211 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008212
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008213 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008214 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008215 if (unlikely(!kvm_x86_ops.get_vmcs12_pages(vcpu))) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008216 r = 0;
8217 goto out;
8218 }
8219 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008220 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008221 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008222 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008223 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008224 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8225 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03008226 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8227 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008228 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8229 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008230 if (unlikely(r))
8231 goto out;
8232 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008233 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008234 kvm_mmu_sync_roots(vcpu);
Paolo Bonzini727a7e22020-03-05 03:52:50 -05008235 if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
8236 kvm_mmu_load_pgd(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008237 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008238 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008239 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008240 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008241 r = 0;
8242 goto out;
8243 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008244 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008245 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008246 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008247 r = 0;
8248 goto out;
8249 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008250 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8251 /* Page is swapped out. Do synthetic halt */
8252 vcpu->arch.apf.halted = true;
8253 r = 1;
8254 goto out;
8255 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008256 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8257 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008258 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8259 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008260 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8261 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008262 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008263 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008264 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008265 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008266 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8267 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8268 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008269 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008270 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8271 vcpu->run->eoi.vector =
8272 vcpu->arch.pending_ioapic_eoi;
8273 r = 0;
8274 goto out;
8275 }
8276 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008277 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8278 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008279 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8280 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008281 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8282 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008283 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8284 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8285 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8286 r = 0;
8287 goto out;
8288 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008289 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8290 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8291 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8292 r = 0;
8293 goto out;
8294 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008295 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8296 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8297 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8298 r = 0;
8299 goto out;
8300 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008301
8302 /*
8303 * KVM_REQ_HV_STIMER has to be processed after
8304 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8305 * depend on the guest clock being up-to-date
8306 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008307 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8308 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008309 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8310 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008311 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008312
Avi Kivityb463a6f2010-07-20 15:06:17 +03008313 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008314 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008315 kvm_apic_accept_events(vcpu);
8316 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8317 r = 1;
8318 goto out;
8319 }
8320
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008321 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008322 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008323 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008324 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008325 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008326 * SMIs have three cases:
8327 * 1) They can be nested, and then there is nothing to
8328 * do here because RSM will cause a vmexit anyway.
8329 * 2) There is an ISA-specific reason why SMI cannot be
8330 * injected, and the moment when this changes can be
8331 * intercepted.
8332 * 3) Or the SMI can be pending because
8333 * inject_pending_event has completed the injection
8334 * of an IRQ or NMI from the previous vmexit, and
8335 * then we request an immediate exit to inject the
8336 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008337 */
8338 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008339 if (!kvm_x86_ops.enable_smi_window(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008340 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008341 if (vcpu->arch.nmi_pending)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008342 kvm_x86_ops.enable_nmi_window(vcpu);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008343 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008344 kvm_x86_ops.enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008345 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008346 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008347
Avi Kivity3842d132010-07-27 12:30:24 +03008348 if (kvm_lapic_enabled(vcpu)) {
8349 update_cr8_intercept(vcpu);
8350 kvm_lapic_sync_to_vapic(vcpu);
8351 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008352 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008353
Avi Kivityd8368af2012-05-14 18:07:56 +03008354 r = kvm_mmu_reload(vcpu);
8355 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008356 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008357 }
8358
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008359 preempt_disable();
8360
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008361 kvm_x86_ops.prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008362
8363 /*
8364 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8365 * IPI are then delayed after guest entry, which ensures that they
8366 * result in virtual interrupt delivery.
8367 */
8368 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008369 vcpu->mode = IN_GUEST_MODE;
8370
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008371 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8372
Lan Tianyu0f127d12016-03-13 11:10:29 +08008373 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008374 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008375 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008376 *
Luwei Kang81b01662019-01-31 16:52:02 +08008377 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008378 * pairs with the memory barrier implicit in pi_test_and_set_on
8379 * (see vmx_deliver_posted_interrupt).
8380 *
8381 * 3) This also orders the write to mode from any reads to the page
8382 * tables done while the VCPU is running. Please see the comment
8383 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008384 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008385 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008386
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008387 /*
8388 * This handles the case where a posted interrupt was
8389 * notified with kvm_vcpu_kick.
8390 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008391 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008392 kvm_x86_ops.sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008393
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008394 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008395 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008396 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008397 smp_wmb();
8398 local_irq_enable();
8399 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008400 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008401 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008402 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008403 }
8404
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008405 if (req_immediate_exit) {
8406 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008407 kvm_x86_ops.request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008408 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008409
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008410 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008411 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008412
Sean Christopherson2620fe22020-01-17 11:30:51 -08008413 fpregs_assert_state_consistent();
8414 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8415 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008416
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008417 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008418 set_debugreg(0, 7);
8419 set_debugreg(vcpu->arch.eff_db[0], 0);
8420 set_debugreg(vcpu->arch.eff_db[1], 1);
8421 set_debugreg(vcpu->arch.eff_db[2], 2);
8422 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008423 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008424 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008425 }
8426
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008427 kvm_x86_ops.run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008428
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008429 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008430 * Do this here before restoring debug registers on the host. And
8431 * since we do this before handling the vmexit, a DR access vmexit
8432 * can (a) read the correct value of the debug registers, (b) set
8433 * KVM_DEBUGREG_WONT_EXIT again.
8434 */
8435 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008436 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008437 kvm_x86_ops.sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008438 kvm_update_dr0123(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008439 kvm_update_dr7(vcpu);
8440 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008441 }
8442
8443 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008444 * If the guest has used debug registers, at least dr7
8445 * will be disabled while returning to the host.
8446 * If we don't have active breakpoints in the host, we don't
8447 * care about the messed up debug address registers. But if
8448 * we have some of them active, restore the old state.
8449 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008450 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008451 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008452
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008453 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008454
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008455 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008456 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008457
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008458 kvm_x86_ops.handle_exit_irqoff(vcpu, &exit_fastpath);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008459
Sean Christophersond7a08882019-07-10 09:07:34 -07008460 /*
8461 * Consume any pending interrupts, including the possible source of
8462 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8463 * An instruction is required after local_irq_enable() to fully unblock
8464 * interrupts on processors that implement an interrupt shadow, the
8465 * stat.exits increment will do nicely.
8466 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008467 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008468 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008469 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008470 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008471 kvm_after_interrupt(vcpu);
8472
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008473 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008474 if (lapic_in_kernel(vcpu)) {
8475 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8476 if (delta != S64_MIN) {
8477 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8478 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8479 }
8480 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008481
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008482 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008483 preempt_enable();
8484
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008485 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008486
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008487 /*
8488 * Profile KVM exit RIPs:
8489 */
8490 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008491 unsigned long rip = kvm_rip_read(vcpu);
8492 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008493 }
8494
Zachary Amsdencc578282012-02-03 15:43:50 -02008495 if (unlikely(vcpu->arch.tsc_always_catchup))
8496 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008497
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008498 if (vcpu->arch.apic_attention)
8499 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008500
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008501 r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008502 return r;
8503
8504cancel_injection:
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008505 kvm_x86_ops.cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008506 if (unlikely(vcpu->arch.apic_attention))
8507 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008508out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008509 return r;
8510}
8511
Paolo Bonzini362c6982015-02-06 12:48:04 +01008512static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8513{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008514 if (!kvm_arch_vcpu_runnable(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008515 (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008516 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8517 kvm_vcpu_block(vcpu);
8518 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008519
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008520 if (kvm_x86_ops.post_block)
8521 kvm_x86_ops.post_block(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008522
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008523 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8524 return 1;
8525 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008526
Paolo Bonzini362c6982015-02-06 12:48:04 +01008527 kvm_apic_accept_events(vcpu);
8528 switch(vcpu->arch.mp_state) {
8529 case KVM_MP_STATE_HALTED:
8530 vcpu->arch.pv.pv_unhalted = false;
8531 vcpu->arch.mp_state =
8532 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008533 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008534 case KVM_MP_STATE_RUNNABLE:
8535 vcpu->arch.apf.halted = false;
8536 break;
8537 case KVM_MP_STATE_INIT_RECEIVED:
8538 break;
8539 default:
8540 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008541 }
8542 return 1;
8543}
8544
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008545static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8546{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008547 if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events)
8548 kvm_x86_ops.check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008549
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008550 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8551 !vcpu->arch.apf.halted);
8552}
8553
Paolo Bonzini362c6982015-02-06 12:48:04 +01008554static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008555{
8556 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008557 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008558
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008559 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008560 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008561
Paolo Bonzini362c6982015-02-06 12:48:04 +01008562 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008563 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008564 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008565 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008566 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008567 }
8568
Gleb Natapov09cec752009-03-23 15:11:44 +02008569 if (r <= 0)
8570 break;
8571
Radim Krčmář72875d82017-04-26 22:32:19 +02008572 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008573 if (kvm_cpu_has_pending_timer(vcpu))
8574 kvm_inject_pending_timer_irqs(vcpu);
8575
Matt Gingell782d4222015-11-16 15:26:00 -08008576 if (dm_request_for_irq_injection(vcpu) &&
8577 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008578 r = 0;
8579 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008580 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008581 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008582 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008583
8584 kvm_check_async_pf_completion(vcpu);
8585
Gleb Natapov09cec752009-03-23 15:11:44 +02008586 if (signal_pending(current)) {
8587 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008588 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008589 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008590 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008591 }
8592 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008593 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008594 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008595 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008596 }
8597 }
8598
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008599 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008600
8601 return r;
8602}
8603
Gleb Natapov716d51a2012-09-03 15:24:26 +03008604static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8605{
8606 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008607
Gleb Natapov716d51a2012-09-03 15:24:26 +03008608 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008609 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008610 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008611 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008612}
8613
8614static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8615{
8616 BUG_ON(!vcpu->arch.pio.count);
8617
8618 return complete_emulated_io(vcpu);
8619}
8620
Avi Kivityf78146b2012-04-18 19:22:47 +03008621/*
8622 * Implements the following, as a state machine:
8623 *
8624 * read:
8625 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008626 * for each mmio piece in the fragment
8627 * write gpa, len
8628 * exit
8629 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008630 * execute insn
8631 *
8632 * write:
8633 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008634 * for each mmio piece in the fragment
8635 * write gpa, len
8636 * copy data
8637 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008638 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008639static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008640{
8641 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008642 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008643 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008644
Gleb Natapov716d51a2012-09-03 15:24:26 +03008645 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008646
Gleb Natapov716d51a2012-09-03 15:24:26 +03008647 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008648 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8649 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008650 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008651 memcpy(frag->data, run->mmio.data, len);
8652
8653 if (frag->len <= 8) {
8654 /* Switch to the next fragment. */
8655 frag++;
8656 vcpu->mmio_cur_fragment++;
8657 } else {
8658 /* Go forward to the next mmio piece. */
8659 frag->data += len;
8660 frag->gpa += len;
8661 frag->len -= len;
8662 }
8663
Andrew Honiga08d3b32014-02-27 19:35:14 +01008664 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008665 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008666
8667 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008668 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008669 return 1;
8670 vcpu->mmio_read_completed = 1;
8671 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008672 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008673
Gleb Natapov716d51a2012-09-03 15:24:26 +03008674 run->exit_reason = KVM_EXIT_MMIO;
8675 run->mmio.phys_addr = frag->gpa;
8676 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008677 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8678 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008679 run->mmio.is_write = vcpu->mmio_is_write;
8680 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8681 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008682}
8683
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008684static void kvm_save_current_fpu(struct fpu *fpu)
8685{
8686 /*
8687 * If the target FPU state is not resident in the CPU registers, just
8688 * memcpy() from current, else save CPU state directly to the target.
8689 */
8690 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8691 memcpy(&fpu->state, &current->thread.fpu.state,
8692 fpu_kernel_xstate_size);
8693 else
8694 copy_fpregs_to_fpstate(fpu);
8695}
8696
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008697/* Swap (qemu) user FPU context for the guest FPU context. */
8698static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8699{
Rik van Riel5f409e22019-04-03 18:41:52 +02008700 fpregs_lock();
8701
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008702 kvm_save_current_fpu(vcpu->arch.user_fpu);
8703
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008704 /* PKRU is separately restored in kvm_x86_ops.run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008705 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008706 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008707
8708 fpregs_mark_activate();
8709 fpregs_unlock();
8710
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008711 trace_kvm_fpu(1);
8712}
8713
8714/* When vcpu_run ends, restore user space FPU context. */
8715static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8716{
Rik van Riel5f409e22019-04-03 18:41:52 +02008717 fpregs_lock();
8718
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008719 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8720
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008721 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008722
8723 fpregs_mark_activate();
8724 fpregs_unlock();
8725
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008726 ++vcpu->stat.fpu_reload;
8727 trace_kvm_fpu(0);
8728}
8729
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008730int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8731{
8732 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008733
Christoffer Dallaccb7572017-12-04 21:35:25 +01008734 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008735 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008736 kvm_load_guest_fpu(vcpu);
8737
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008738 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008739 if (kvm_run->immediate_exit) {
8740 r = -EINTR;
8741 goto out;
8742 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008743 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008744 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008745 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008746 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008747 if (signal_pending(current)) {
8748 r = -EINTR;
8749 vcpu->run->exit_reason = KVM_EXIT_INTR;
8750 ++vcpu->stat.signal_exits;
8751 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008752 goto out;
8753 }
8754
Ken Hofsass01643c52018-01-31 16:03:36 -08008755 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8756 r = -EINVAL;
8757 goto out;
8758 }
8759
8760 if (vcpu->run->kvm_dirty_regs) {
8761 r = sync_regs(vcpu);
8762 if (r != 0)
8763 goto out;
8764 }
8765
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008766 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008767 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008768 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8769 r = -EINVAL;
8770 goto out;
8771 }
8772 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008773
Gleb Natapov716d51a2012-09-03 15:24:26 +03008774 if (unlikely(vcpu->arch.complete_userspace_io)) {
8775 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8776 vcpu->arch.complete_userspace_io = NULL;
8777 r = cui(vcpu);
8778 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008779 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008780 } else
8781 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008782
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008783 if (kvm_run->immediate_exit)
8784 r = -EINTR;
8785 else
8786 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008787
8788out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008789 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008790 if (vcpu->run->kvm_valid_regs)
8791 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008792 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008793 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008794
Christoffer Dallaccb7572017-12-04 21:35:25 +01008795 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008796 return r;
8797}
8798
Ken Hofsass01643c52018-01-31 16:03:36 -08008799static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008800{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008801 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8802 /*
8803 * We are here if userspace calls get_regs() in the middle of
8804 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008805 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008806 * that usually, but some bad designed PV devices (vmware
8807 * backdoor interface) need this to work
8808 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008809 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008810 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8811 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008812 regs->rax = kvm_rax_read(vcpu);
8813 regs->rbx = kvm_rbx_read(vcpu);
8814 regs->rcx = kvm_rcx_read(vcpu);
8815 regs->rdx = kvm_rdx_read(vcpu);
8816 regs->rsi = kvm_rsi_read(vcpu);
8817 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008818 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008819 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008820#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008821 regs->r8 = kvm_r8_read(vcpu);
8822 regs->r9 = kvm_r9_read(vcpu);
8823 regs->r10 = kvm_r10_read(vcpu);
8824 regs->r11 = kvm_r11_read(vcpu);
8825 regs->r12 = kvm_r12_read(vcpu);
8826 regs->r13 = kvm_r13_read(vcpu);
8827 regs->r14 = kvm_r14_read(vcpu);
8828 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008829#endif
8830
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008831 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008832 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008833}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008834
Ken Hofsass01643c52018-01-31 16:03:36 -08008835int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8836{
8837 vcpu_load(vcpu);
8838 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008839 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008840 return 0;
8841}
8842
Ken Hofsass01643c52018-01-31 16:03:36 -08008843static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008844{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008845 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8846 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8847
Sean Christophersonde3cd112019-04-30 10:36:17 -07008848 kvm_rax_write(vcpu, regs->rax);
8849 kvm_rbx_write(vcpu, regs->rbx);
8850 kvm_rcx_write(vcpu, regs->rcx);
8851 kvm_rdx_write(vcpu, regs->rdx);
8852 kvm_rsi_write(vcpu, regs->rsi);
8853 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008854 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008855 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008856#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008857 kvm_r8_write(vcpu, regs->r8);
8858 kvm_r9_write(vcpu, regs->r9);
8859 kvm_r10_write(vcpu, regs->r10);
8860 kvm_r11_write(vcpu, regs->r11);
8861 kvm_r12_write(vcpu, regs->r12);
8862 kvm_r13_write(vcpu, regs->r13);
8863 kvm_r14_write(vcpu, regs->r14);
8864 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008865#endif
8866
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008867 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008868 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008869
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008870 vcpu->arch.exception.pending = false;
8871
Avi Kivity3842d132010-07-27 12:30:24 +03008872 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008873}
Avi Kivity3842d132010-07-27 12:30:24 +03008874
Ken Hofsass01643c52018-01-31 16:03:36 -08008875int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8876{
8877 vcpu_load(vcpu);
8878 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008879 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008880 return 0;
8881}
8882
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008883void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8884{
8885 struct kvm_segment cs;
8886
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008887 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008888 *db = cs.db;
8889 *l = cs.l;
8890}
8891EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8892
Ken Hofsass01643c52018-01-31 16:03:36 -08008893static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008894{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008895 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008896
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008897 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8898 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8899 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8900 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8901 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8902 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008903
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008904 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8905 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008906
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008907 kvm_x86_ops.get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008908 sregs->idt.limit = dt.size;
8909 sregs->idt.base = dt.address;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07008910 kvm_x86_ops.get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008911 sregs->gdt.limit = dt.size;
8912 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008913
Avi Kivity4d4ec082009-12-29 18:07:30 +02008914 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008915 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008916 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008917 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008918 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008919 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008920 sregs->apic_base = kvm_get_apic_base(vcpu);
8921
Jordan Borgner0e96f312018-10-28 12:58:28 +00008922 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008923
Liran Alon04140b42018-03-23 03:01:31 +03008924 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008925 set_bit(vcpu->arch.interrupt.nr,
8926 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008927}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008928
Ken Hofsass01643c52018-01-31 16:03:36 -08008929int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8930 struct kvm_sregs *sregs)
8931{
8932 vcpu_load(vcpu);
8933 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008934 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008935 return 0;
8936}
8937
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008938int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8939 struct kvm_mp_state *mp_state)
8940{
Christoffer Dallfd232562017-12-04 21:35:30 +01008941 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008942 if (kvm_mpx_supported())
8943 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008944
Jan Kiszka66450a22013-03-13 12:42:34 +01008945 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308946 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8947 vcpu->arch.pv.pv_unhalted)
8948 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8949 else
8950 mp_state->mp_state = vcpu->arch.mp_state;
8951
Sean Christophersonf958bd22019-12-09 12:19:31 -08008952 if (kvm_mpx_supported())
8953 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008954 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008955 return 0;
8956}
8957
8958int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8959 struct kvm_mp_state *mp_state)
8960{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008961 int ret = -EINVAL;
8962
8963 vcpu_load(vcpu);
8964
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008965 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008966 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008967 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008968
Liran Alon27cbe7d2019-11-11 11:16:40 +02008969 /*
8970 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8971 * INIT state; latched init should be reported using
8972 * KVM_SET_VCPU_EVENTS, so reject it here.
8973 */
8974 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008975 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8976 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008977 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008978
Jan Kiszka66450a22013-03-13 12:42:34 +01008979 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8980 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8981 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8982 } else
8983 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008984 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008985
8986 ret = 0;
8987out:
8988 vcpu_put(vcpu);
8989 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008990}
8991
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008992int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8993 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008994{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008995 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008996 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008997
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008998 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008999
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01009000 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009001 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07009002 if (ret) {
9003 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9004 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
9005 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009006 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07009007 }
Gleb Natapovc6975182010-02-18 12:15:01 +02009008
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09009009 kvm_rip_write(vcpu, ctxt->eip);
9010 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07009011 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02009012}
9013EXPORT_SYMBOL_GPL(kvm_task_switch);
9014
Peng Hao3140c152018-04-02 09:15:32 +08009015static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05009016{
Tianyu Lan37b95952018-01-16 17:34:07 +08009017 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05009018 /*
9019 * When EFER.LME and CR0.PG are set, the processor is in
9020 * 64-bit mode (though maybe in a 32-bit code segment).
9021 * CR4.PAE and EFER.LMA must be set.
9022 */
Tianyu Lan37b95952018-01-16 17:34:07 +08009023 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05009024 || !(sregs->efer & EFER_LMA))
9025 return -EINVAL;
9026 } else {
9027 /*
9028 * Not in 64-bit mode: EFER.LMA is clear and the code
9029 * segment cannot be 64-bit.
9030 */
9031 if (sregs->efer & EFER_LMA || sregs->cs.l)
9032 return -EINVAL;
9033 }
9034
Wanpeng Li3ca94192019-09-18 17:50:10 +08009035 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05009036}
9037
Ken Hofsass01643c52018-01-31 16:03:36 -08009038static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009039{
Jan Kiszka58cb6282014-01-24 16:48:44 +01009040 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009041 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05009042 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009043 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02009044 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009045 int ret = -EINVAL;
9046
Lan Tianyuf2981032017-12-14 03:01:52 -05009047 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08009048 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05009049
Jim Mattsond3802282017-08-10 10:14:13 -07009050 apic_base_msr.data = sregs->apic_base;
9051 apic_base_msr.host_initiated = true;
9052 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009053 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009054
Gleb Natapov89a27f42010-02-16 10:51:48 +02009055 dt.size = sregs->idt.limit;
9056 dt.address = sregs->idt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009057 kvm_x86_ops.set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009058 dt.size = sregs->gdt.limit;
9059 dt.address = sregs->gdt.base;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009060 kvm_x86_ops.set_gdt(vcpu, &dt);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009061
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009062 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009063 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009064 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009065 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009066
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009067 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009068
Avi Kivityf6801df2010-01-21 15:31:50 +02009069 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009070 kvm_x86_ops.set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009071
Avi Kivity4d4ec082009-12-29 18:07:30 +02009072 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009073 kvm_x86_ops.set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009074 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009075
Avi Kivityfc78f512009-12-07 12:16:48 +02009076 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009077 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9078 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009079 kvm_x86_ops.set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009080 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009081 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009082
9083 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009084 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009085 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009086 mmu_reset_needed = 1;
9087 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009088 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009089
9090 if (mmu_reset_needed)
9091 kvm_mmu_reset_context(vcpu);
9092
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009093 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009094 pending_vec = find_first_bit(
9095 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9096 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009097 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009098 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009099 }
9100
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009101 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9102 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9103 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9104 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9105 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9106 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009107
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009108 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9109 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009110
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009111 update_cr8_intercept(vcpu);
9112
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009113 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009114 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009115 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009116 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009117 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9118
Avi Kivity3842d132010-07-27 12:30:24 +03009119 kvm_make_request(KVM_REQ_EVENT, vcpu);
9120
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009121 ret = 0;
9122out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009123 return ret;
9124}
9125
9126int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9127 struct kvm_sregs *sregs)
9128{
9129 int ret;
9130
9131 vcpu_load(vcpu);
9132 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009133 vcpu_put(vcpu);
9134 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009135}
9136
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009137int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9138 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009139{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009140 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009141 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009142
Christoffer Dall66b56562017-12-04 21:35:33 +01009143 vcpu_load(vcpu);
9144
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009145 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9146 r = -EBUSY;
9147 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009148 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009149 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9150 kvm_queue_exception(vcpu, DB_VECTOR);
9151 else
9152 kvm_queue_exception(vcpu, BP_VECTOR);
9153 }
9154
Jan Kiszka91586a32009-10-05 13:07:21 +02009155 /*
9156 * Read rflags as long as potentially injected trace flags are still
9157 * filtered out.
9158 */
9159 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009160
9161 vcpu->guest_debug = dbg->control;
9162 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9163 vcpu->guest_debug = 0;
9164
9165 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009166 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9167 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009168 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009169 } else {
9170 for (i = 0; i < KVM_NR_DB_REGS; i++)
9171 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009172 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009173 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009174
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009175 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9176 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9177 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009178
Jan Kiszka91586a32009-10-05 13:07:21 +02009179 /*
9180 * Trigger an rflags update that will inject or remove the trace
9181 * flags.
9182 */
9183 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009184
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009185 kvm_x86_ops.update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009186
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009187 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009188
Avi Kivity2122ff52010-05-13 11:25:04 +03009189out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009190 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009191 return r;
9192}
9193
9194/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009195 * Translate a guest virtual address to a guest physical address.
9196 */
9197int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9198 struct kvm_translation *tr)
9199{
9200 unsigned long vaddr = tr->linear_address;
9201 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009202 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009203
Christoffer Dall1da5b612017-12-04 21:35:32 +01009204 vcpu_load(vcpu);
9205
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009206 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009207 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009208 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009209 tr->physical_address = gpa;
9210 tr->valid = gpa != UNMAPPED_GVA;
9211 tr->writeable = 1;
9212 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009213
Christoffer Dall1da5b612017-12-04 21:35:32 +01009214 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009215 return 0;
9216}
9217
Hollis Blanchardd0752062007-10-31 17:24:25 -05009218int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9219{
Christoffer Dall13931232017-12-04 21:35:34 +01009220 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009221
Christoffer Dall13931232017-12-04 21:35:34 +01009222 vcpu_load(vcpu);
9223
Marc Orrb666a4b2018-11-06 14:53:56 -08009224 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009225 memcpy(fpu->fpr, fxsave->st_space, 128);
9226 fpu->fcw = fxsave->cwd;
9227 fpu->fsw = fxsave->swd;
9228 fpu->ftwx = fxsave->twd;
9229 fpu->last_opcode = fxsave->fop;
9230 fpu->last_ip = fxsave->rip;
9231 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009232 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009233
Christoffer Dall13931232017-12-04 21:35:34 +01009234 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009235 return 0;
9236}
9237
9238int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9239{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009240 struct fxregs_state *fxsave;
9241
9242 vcpu_load(vcpu);
9243
Marc Orrb666a4b2018-11-06 14:53:56 -08009244 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009245
Hollis Blanchardd0752062007-10-31 17:24:25 -05009246 memcpy(fxsave->st_space, fpu->fpr, 128);
9247 fxsave->cwd = fpu->fcw;
9248 fxsave->swd = fpu->fsw;
9249 fxsave->twd = fpu->ftwx;
9250 fxsave->fop = fpu->last_opcode;
9251 fxsave->rip = fpu->last_ip;
9252 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009253 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009254
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009255 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009256 return 0;
9257}
9258
Ken Hofsass01643c52018-01-31 16:03:36 -08009259static void store_regs(struct kvm_vcpu *vcpu)
9260{
9261 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9262
9263 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9264 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9265
9266 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9267 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9268
9269 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9270 kvm_vcpu_ioctl_x86_get_vcpu_events(
9271 vcpu, &vcpu->run->s.regs.events);
9272}
9273
9274static int sync_regs(struct kvm_vcpu *vcpu)
9275{
9276 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9277 return -EINVAL;
9278
9279 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9280 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9281 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9282 }
9283 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9284 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9285 return -EINVAL;
9286 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9287 }
9288 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9289 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9290 vcpu, &vcpu->run->s.regs.events))
9291 return -EINVAL;
9292 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9293 }
9294
9295 return 0;
9296}
9297
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009298static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009299{
Marc Orrb666a4b2018-11-06 14:53:56 -08009300 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009301 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009302 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009303 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009304
Dexuan Cui2acf9232010-06-10 11:27:12 +08009305 /*
9306 * Ensure guest xcr0 is valid for loading
9307 */
Dave Hansend91cab72015-09-02 16:31:26 -07009308 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009309
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009310 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009311}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009312
Sean Christopherson897cc382019-12-18 13:55:09 -08009313int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009314{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009315 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009316 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9317 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009318
Sean Christopherson897cc382019-12-18 13:55:09 -08009319 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009320}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009321
Sean Christophersone529ef62019-12-18 13:55:15 -08009322int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009323{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009324 struct page *page;
9325 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009326
Sean Christopherson95a0d012019-12-18 13:55:23 -08009327 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9328 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9329 else
9330 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9331
9332 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9333
9334 r = kvm_mmu_create(vcpu);
9335 if (r < 0)
9336 return r;
9337
9338 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009339 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9340 if (r < 0)
9341 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009342 if (kvm_apicv_activated(vcpu->kvm))
9343 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009344 } else
9345 static_key_slow_inc(&kvm_no_apic_vcpu);
9346
9347 r = -ENOMEM;
9348
9349 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9350 if (!page)
9351 goto fail_free_lapic;
9352 vcpu->arch.pio_data = page_address(page);
9353
9354 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9355 GFP_KERNEL_ACCOUNT);
9356 if (!vcpu->arch.mce_banks)
9357 goto fail_free_pio_data;
9358 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9359
9360 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9361 GFP_KERNEL_ACCOUNT))
9362 goto fail_free_mce_banks;
9363
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009364 if (!alloc_emulate_ctxt(vcpu))
9365 goto free_wbinvd_dirty_mask;
9366
Sean Christopherson95a0d012019-12-18 13:55:23 -08009367 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9368 GFP_KERNEL_ACCOUNT);
9369 if (!vcpu->arch.user_fpu) {
9370 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009371 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009372 }
9373
9374 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9375 GFP_KERNEL_ACCOUNT);
9376 if (!vcpu->arch.guest_fpu) {
9377 pr_err("kvm: failed to allocate vcpu's fpu\n");
9378 goto free_user_fpu;
9379 }
9380 fx_init(vcpu);
9381
9382 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9383
9384 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9385
9386 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9387
9388 kvm_async_pf_hash_reset(vcpu);
9389 kvm_pmu_init(vcpu);
9390
9391 vcpu->arch.pending_external_vector = -1;
9392 vcpu->arch.preempted_in_kernel = false;
9393
9394 kvm_hv_vcpu_init(vcpu);
9395
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009396 r = kvm_x86_ops.vcpu_create(vcpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009397 if (r)
9398 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009399
Sean Christopherson0cf91352019-03-07 15:43:02 -08009400 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009401 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009402 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009403 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009404 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009405 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009406 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009407 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009408
9409free_guest_fpu:
9410 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9411free_user_fpu:
9412 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009413free_emulate_ctxt:
9414 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009415free_wbinvd_dirty_mask:
9416 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9417fail_free_mce_banks:
9418 kfree(vcpu->arch.mce_banks);
9419fail_free_pio_data:
9420 free_page((unsigned long)vcpu->arch.pio_data);
9421fail_free_lapic:
9422 kvm_free_lapic(vcpu);
9423fail_mmu_destroy:
9424 kvm_mmu_destroy(vcpu);
9425 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009426}
9427
Dominik Dingel31928aa2014-12-04 15:47:07 +01009428void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009429{
Will Auld8fe8ab42012-11-29 12:42:12 -08009430 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009431 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009432
Roman Kagand3457c82017-07-14 17:13:20 +03009433 kvm_hv_vcpu_postcreate(vcpu);
9434
Christoffer Dallec7660c2017-12-04 21:35:23 +01009435 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009436 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009437 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009438 msr.data = 0x0;
9439 msr.index = MSR_IA32_TSC;
9440 msr.host_initiated = true;
9441 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009442 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009443
9444 /* poll control enabled by default */
9445 vcpu->arch.msr_kvm_poll_control = 1;
9446
Christoffer Dallec7660c2017-12-04 21:35:23 +01009447 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009448
Wanpeng Lib34de572020-02-28 11:18:41 +08009449 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9450 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9451 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009452}
9453
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009454void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009455{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009456 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009457 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009458
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009459 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9460
Sean Christopherson50b143e2019-12-18 13:55:07 -08009461 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009462
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009463 kvm_x86_ops.vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009464
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009465 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009466 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9467 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9468 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009469
9470 kvm_hv_vcpu_uninit(vcpu);
9471 kvm_pmu_destroy(vcpu);
9472 kfree(vcpu->arch.mce_banks);
9473 kvm_free_lapic(vcpu);
9474 idx = srcu_read_lock(&vcpu->kvm->srcu);
9475 kvm_mmu_destroy(vcpu);
9476 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9477 free_page((unsigned long)vcpu->arch.pio_data);
9478 if (!lapic_in_kernel(vcpu))
9479 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009480}
9481
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009482void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009483{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009484 kvm_lapic_reset(vcpu, init_event);
9485
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009486 vcpu->arch.hflags = 0;
9487
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009488 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009489 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009490 atomic_set(&vcpu->arch.nmi_queued, 0);
9491 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009492 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009493 kvm_clear_interrupt_queue(vcpu);
9494 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009495
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009496 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009497 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009498 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009499 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009500 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009501
Nadav Amit11190222015-04-02 03:10:38 +03009502 vcpu->arch.cr2 = 0;
9503
Avi Kivity3842d132010-07-27 12:30:24 +03009504 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009505 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009506 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009507
Glauber Costa12f9a482011-02-01 14:16:40 -05009508 kvmclock_reset(vcpu);
9509
Gleb Natapovaf585b92010-10-14 11:22:46 +02009510 kvm_clear_async_pf_completion_queue(vcpu);
9511 kvm_async_pf_hash_reset(vcpu);
9512 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009513
Wanpeng Lia554d202017-10-11 05:10:19 -07009514 if (kvm_mpx_supported()) {
9515 void *mpx_state_buffer;
9516
9517 /*
9518 * To avoid have the INIT path from kvm_apic_has_events() that be
9519 * called with loaded FPU and does not let userspace fix the state.
9520 */
Rik van Rielf775b132017-11-14 16:54:23 -05009521 if (init_event)
9522 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009523 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009524 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009525 if (mpx_state_buffer)
9526 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009527 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009528 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009529 if (mpx_state_buffer)
9530 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009531 if (init_event)
9532 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009533 }
9534
Paolo Bonzini64d60672015-05-07 11:36:11 +02009535 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009536 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009537 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009538
Kyle Hueydb2336a2017-03-20 01:16:28 -07009539 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009540
9541 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009542 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009543
Julian Stecklina66f7b722012-12-05 15:26:19 +01009544 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9545 vcpu->arch.regs_avail = ~0;
9546 vcpu->arch.regs_dirty = ~0;
9547
Wanpeng Lia554d202017-10-11 05:10:19 -07009548 vcpu->arch.ia32_xss = 0;
9549
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009550 kvm_x86_ops.vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009551}
9552
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009553void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009554{
9555 struct kvm_segment cs;
9556
9557 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9558 cs.selector = vector << 8;
9559 cs.base = vector << 12;
9560 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9561 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009562}
9563
Radim Krčmář13a34e02014-08-28 15:13:03 +02009564int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009565{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009566 struct kvm *kvm;
9567 struct kvm_vcpu *vcpu;
9568 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009569 int ret;
9570 u64 local_tsc;
9571 u64 max_tsc = 0;
9572 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009573
9574 kvm_shared_msr_cpu_online();
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009575 ret = kvm_x86_ops.hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009576 if (ret != 0)
9577 return ret;
9578
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009579 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009580 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009581 list_for_each_entry(kvm, &vm_list, vm_list) {
9582 kvm_for_each_vcpu(i, vcpu, kvm) {
9583 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009584 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009585 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9586 backwards_tsc = true;
9587 if (vcpu->arch.last_host_tsc > max_tsc)
9588 max_tsc = vcpu->arch.last_host_tsc;
9589 }
9590 }
9591 }
9592
9593 /*
9594 * Sometimes, even reliable TSCs go backwards. This happens on
9595 * platforms that reset TSC during suspend or hibernate actions, but
9596 * maintain synchronization. We must compensate. Fortunately, we can
9597 * detect that condition here, which happens early in CPU bringup,
9598 * before any KVM threads can be running. Unfortunately, we can't
9599 * bring the TSCs fully up to date with real time, as we aren't yet far
9600 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009601 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009602 * variables that haven't been updated yet.
9603 *
9604 * So we simply find the maximum observed TSC above, then record the
9605 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9606 * the adjustment will be applied. Note that we accumulate
9607 * adjustments, in case multiple suspend cycles happen before some VCPU
9608 * gets a chance to run again. In the event that no KVM threads get a
9609 * chance to run, we will miss the entire elapsed period, as we'll have
9610 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9611 * loose cycle time. This isn't too big a deal, since the loss will be
9612 * uniform across all VCPUs (not to mention the scenario is extremely
9613 * unlikely). It is possible that a second hibernate recovery happens
9614 * much faster than a first, causing the observed TSC here to be
9615 * smaller; this would require additional padding adjustment, which is
9616 * why we set last_host_tsc to the local tsc observed here.
9617 *
9618 * N.B. - this code below runs only on platforms with reliable TSC,
9619 * as that is the only way backwards_tsc is set above. Also note
9620 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9621 * have the same delta_cyc adjustment applied if backwards_tsc
9622 * is detected. Note further, this adjustment is only done once,
9623 * as we reset last_host_tsc on all VCPUs to stop this from being
9624 * called multiple times (one for each physical CPU bringup).
9625 *
Guo Chao4a969982012-06-28 15:17:27 +08009626 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009627 * will be compensated by the logic in vcpu_load, which sets the TSC to
9628 * catchup mode. This will catchup all VCPUs to real time, but cannot
9629 * guarantee that they stay in perfect synchronization.
9630 */
9631 if (backwards_tsc) {
9632 u64 delta_cyc = max_tsc - local_tsc;
9633 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009634 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009635 kvm_for_each_vcpu(i, vcpu, kvm) {
9636 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9637 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009638 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009639 }
9640
9641 /*
9642 * We have to disable TSC offset matching.. if you were
9643 * booting a VM while issuing an S4 host suspend....
9644 * you may have some problem. Solving this issue is
9645 * left as an exercise to the reader.
9646 */
9647 kvm->arch.last_tsc_nsec = 0;
9648 kvm->arch.last_tsc_write = 0;
9649 }
9650
9651 }
9652 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009653}
9654
Radim Krčmář13a34e02014-08-28 15:13:03 +02009655void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009656{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009657 kvm_x86_ops.hardware_disable();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009658 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009659}
9660
Sean Christophersonb9904082020-03-21 13:25:55 -07009661int kvm_arch_hardware_setup(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009662{
Sean Christophersond008dfd2020-03-21 13:25:56 -07009663 struct kvm_x86_init_ops *ops = opaque;
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009664 int r;
9665
Sean Christopherson91661982020-03-02 15:57:06 -08009666 rdmsrl_safe(MSR_EFER, &host_efer);
9667
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009668 if (boot_cpu_has(X86_FEATURE_XSAVES))
9669 rdmsrl(MSR_IA32_XSS, host_xss);
9670
Sean Christophersond008dfd2020-03-21 13:25:56 -07009671 r = ops->hardware_setup();
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009672 if (r != 0)
9673 return r;
9674
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009675 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
Sean Christopherson69c6f692020-03-21 13:25:59 -07009676
Paolo Bonzini408e9a32020-03-05 16:11:56 +01009677 if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
9678 supported_xss = 0;
9679
Paolo Bonzini139f7422020-05-05 09:40:46 -04009680#define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f)
9681 cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_);
9682#undef __kvm_cpu_cap_has
Sean Christophersonb11306b2019-12-10 14:44:13 -08009683
Haozhong Zhang35181e82015-10-20 15:39:03 +08009684 if (kvm_has_tsc_control) {
9685 /*
9686 * Make sure the user can only configure tsc_khz values that
9687 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009688 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009689 * be 1 on all machines.
9690 */
9691 u64 max = min(0x7fffffffULL,
9692 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9693 kvm_max_guest_tsc_khz = max;
9694
Haozhong Zhangad7218832015-10-20 15:39:02 +08009695 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009696 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009697
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009698 kvm_init_msr_list();
9699 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009700}
9701
9702void kvm_arch_hardware_unsetup(void)
9703{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009704 kvm_x86_ops.hardware_unsetup();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009705}
9706
Sean Christophersonb9904082020-03-21 13:25:55 -07009707int kvm_arch_check_processor_compat(void *opaque)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009708{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009709 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
Sean Christophersond008dfd2020-03-21 13:25:56 -07009710 struct kvm_x86_init_ops *ops = opaque;
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009711
9712 WARN_ON(!irqs_disabled());
9713
Paolo Bonzini139f7422020-05-05 09:40:46 -04009714 if (__cr4_reserved_bits(cpu_has, c) !=
9715 __cr4_reserved_bits(cpu_has, &boot_cpu_data))
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009716 return -EIO;
9717
Sean Christophersond008dfd2020-03-21 13:25:56 -07009718 return ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009719}
9720
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009721bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9722{
9723 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9724}
9725EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9726
9727bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9728{
9729 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9730}
9731
Gleb Natapov54e98182012-08-05 15:58:32 +03009732struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009733EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009734
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009735void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9736{
Like Xub35e5542019-10-27 18:52:43 +08009737 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9738
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009739 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009740 if (pmu->version && unlikely(pmu->event_count)) {
9741 pmu->need_cleanup = true;
9742 kvm_make_request(KVM_REQ_PMU, vcpu);
9743 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009744 kvm_x86_ops.sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009745}
9746
Sean Christopherson562b6b02020-01-26 16:41:13 -08009747void kvm_arch_free_vm(struct kvm *kvm)
9748{
9749 kfree(kvm->arch.hyperv.hv_pa_pg);
9750 vfree(kvm);
9751}
9752
9753
Carsten Ottee08b9632012-01-04 10:25:20 +01009754int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009755{
Carsten Ottee08b9632012-01-04 10:25:20 +01009756 if (type)
9757 return -EINVAL;
9758
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009759 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009760 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009761 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009762 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009763 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009764 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009765
Sheng Yang5550af42008-10-15 20:15:06 +08009766 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9767 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009768 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9769 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9770 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009771
Jan Kiszka038f8c12011-02-04 10:49:11 +01009772 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009773 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009774 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9775
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009776 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009777 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009778
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009779 kvm->arch.guest_can_read_msr_platform_info = true;
9780
Andrew Jones7e44e442014-02-28 12:52:54 +01009781 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009782 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009783
Roman Kagancbc02362018-02-01 16:48:31 +03009784 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009785 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009786 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009787
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009788 return kvm_x86_ops.vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009789}
9790
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009791int kvm_arch_post_init_vm(struct kvm *kvm)
9792{
9793 return kvm_mmu_post_init_vm(kvm);
9794}
9795
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009796static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9797{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009798 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009799 kvm_mmu_unload(vcpu);
9800 vcpu_put(vcpu);
9801}
9802
9803static void kvm_free_vcpus(struct kvm *kvm)
9804{
9805 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009806 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009807
9808 /*
9809 * Unpin any mmu pages first.
9810 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009811 kvm_for_each_vcpu(i, vcpu, kvm) {
9812 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009813 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009814 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009815 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009816 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009817
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009818 mutex_lock(&kvm->lock);
9819 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9820 kvm->vcpus[i] = NULL;
9821
9822 atomic_set(&kvm->online_vcpus, 0);
9823 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009824}
9825
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009826void kvm_arch_sync_events(struct kvm *kvm)
9827{
Andrew Jones332967a2014-02-28 12:52:55 +01009828 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009829 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009830 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009831}
9832
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009833int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009834{
9835 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009836 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009837 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009838 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009839
9840 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009841 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9842 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009843
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009844 slot = id_to_memslot(slots, id);
9845 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009846 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009847 return -EEXIST;
9848
9849 /*
9850 * MAP_SHARED to prevent internal slot pages from being moved
9851 * by fork()/COW.
9852 */
9853 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9854 MAP_SHARED | MAP_ANONYMOUS, 0);
9855 if (IS_ERR((void *)hva))
9856 return PTR_ERR((void *)hva);
9857 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009858 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009859 return 0;
9860
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009861 /*
9862 * Stuff a non-canonical value to catch use-after-delete. This
9863 * ends up being 0 on 32-bit KVM, but there's no better
9864 * alternative.
9865 */
9866 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009867 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009868 }
9869
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009870 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009871 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009872
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009873 m.slot = id | (i << 16);
9874 m.flags = 0;
9875 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009876 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009877 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009878 r = __kvm_set_memory_region(kvm, &m);
9879 if (r < 0)
9880 return r;
9881 }
9882
Eric Biggers103c7632018-01-31 17:30:21 -08009883 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009884 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009885
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009886 return 0;
9887}
9888EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9889
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009890void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9891{
9892 kvm_mmu_pre_destroy_vm(kvm);
9893}
9894
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009895void kvm_arch_destroy_vm(struct kvm *kvm)
9896{
Andrew Honig27469d22013-04-18 09:38:14 -07009897 if (current->mm == kvm->mm) {
9898 /*
9899 * Free memory regions allocated on behalf of userspace,
9900 * unless the the memory map has changed due to process exit
9901 * or fd copying.
9902 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009903 mutex_lock(&kvm->slots_lock);
9904 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9905 0, 0);
9906 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9907 0, 0);
9908 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9909 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009910 }
Sean Christophersonafaf0b22020-03-21 13:26:00 -07009911 if (kvm_x86_ops.vm_destroy)
9912 kvm_x86_ops.vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009913 kvm_pic_destroy(kvm);
9914 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009915 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009916 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009917 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009918 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009919 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009920 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009921}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009922
Sean Christophersone96c81e2020-02-18 13:07:27 -08009923void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009924{
9925 int i;
9926
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009927 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009928 kvfree(slot->arch.rmap[i]);
9929 slot->arch.rmap[i] = NULL;
9930
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009931 if (i == 0)
9932 continue;
9933
Sean Christophersone96c81e2020-02-18 13:07:27 -08009934 kvfree(slot->arch.lpage_info[i - 1]);
9935 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009936 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009937
Sean Christophersone96c81e2020-02-18 13:07:27 -08009938 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009939}
9940
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009941static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9942 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009943{
9944 int i;
9945
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009946 /*
9947 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9948 * old arrays will be freed by __kvm_set_memory_region() if installing
9949 * the new memslot is successful.
9950 */
9951 memset(&slot->arch, 0, sizeof(slot->arch));
9952
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009953 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009954 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009955 unsigned long ugfn;
9956 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009957 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009958
9959 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9960 slot->base_gfn, level) + 1;
9961
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009962 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009963 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009964 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009965 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009966 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009967 if (i == 0)
9968 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009969
Ben Gardon254272c2019-02-11 11:02:50 -08009970 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009971 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009972 goto out_free;
9973
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009974 slot->arch.lpage_info[i - 1] = linfo;
9975
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009976 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009977 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009978 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009979 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009980 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9981 /*
9982 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -08009983 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009984 */
Sean Christopherson600087b2020-03-02 15:57:05 -08009985 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009986 unsigned long j;
9987
9988 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009989 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009990 }
9991 }
9992
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009993 if (kvm_page_track_create_memslot(slot, npages))
9994 goto out_free;
9995
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009996 return 0;
9997
9998out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009999 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +010010000 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010001 slot->arch.rmap[i] = NULL;
10002 if (i == 0)
10003 continue;
10004
Thomas Huth548ef282015-02-24 21:29:25 +010010005 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +090010006 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +090010007 }
10008 return -ENOMEM;
10009}
10010
Sean Christopherson15248252019-02-05 12:54:17 -080010011void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010012{
Boris Ostrovsky91724812019-12-05 01:30:51 +000010013 struct kvm_vcpu *vcpu;
10014 int i;
10015
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +090010016 /*
10017 * memslots->generation has been incremented.
10018 * mmio generation may have reached its maximum value.
10019 */
Sean Christopherson15248252019-02-05 12:54:17 -080010020 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +000010021
10022 /* Force re-initialization of steal_time cache */
10023 kvm_for_each_vcpu(i, vcpu, kvm)
10024 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +090010025}
10026
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010027int kvm_arch_prepare_memory_region(struct kvm *kvm,
10028 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010029 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +090010030 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +080010031{
Sean Christopherson0dab98b2020-02-18 13:07:19 -080010032 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
10033 return kvm_alloc_memslot_metadata(memslot,
10034 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010035 return 0;
10036}
10037
Kai Huang88178fd2015-01-28 10:54:27 +080010038static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
10039 struct kvm_memory_slot *new)
10040{
10041 /* Still write protect RO slot */
10042 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010043 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +080010044 return;
10045 }
10046
10047 /*
10048 * Call kvm_x86_ops dirty logging hooks when they are valid.
10049 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010050 * kvm_x86_ops.slot_disable_log_dirty is called when:
Kai Huang88178fd2015-01-28 10:54:27 +080010051 *
10052 * - KVM_MR_CREATE with dirty logging is disabled
10053 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
10054 *
10055 * The reason is, in case of PML, we need to set D-bit for any slots
10056 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +080010057 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +080010058 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010059 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010060 * logging disabled for memory slots.
10061 *
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010062 * kvm_x86_ops.slot_enable_log_dirty is called when switching new slot
Kai Huang88178fd2015-01-28 10:54:27 +080010063 * to dirty logging mode.
10064 *
10065 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10066 *
10067 * In case of write protect:
10068 *
10069 * Write protect all pages for dirty logging.
10070 *
10071 * All the sptes including the large sptes which point to this
10072 * slot are set to readonly. We can not create any new large
10073 * spte on this slot until the end of the logging.
10074 *
10075 * See the comments in fast_page_fault().
10076 */
10077 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010078 if (kvm_x86_ops.slot_enable_log_dirty) {
10079 kvm_x86_ops.slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010080 } else {
10081 int level =
10082 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10083 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10084
10085 /*
10086 * If we're with initial-all-set, we don't need
10087 * to write protect any small page because
10088 * they're reported as dirty already. However
10089 * we still need to write-protect huge pages
10090 * so that the page split can happen lazily on
10091 * the first write to the huge page.
10092 */
10093 kvm_mmu_slot_remove_write_access(kvm, new, level);
10094 }
Kai Huang88178fd2015-01-28 10:54:27 +080010095 } else {
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010096 if (kvm_x86_ops.slot_disable_log_dirty)
10097 kvm_x86_ops.slot_disable_log_dirty(kvm, new);
Kai Huang88178fd2015-01-28 10:54:27 +080010098 }
10099}
10100
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010101void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010102 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010103 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010104 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010105 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010106{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010107 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010108 kvm_mmu_change_mmu_pages(kvm,
10109 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010110
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010111 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010112 * Dirty logging tracks sptes in 4k granularity, meaning that large
10113 * sptes have to be split. If live migration is successful, the guest
10114 * in the source machine will be destroyed and large sptes will be
10115 * created in the destination. However, if the guest continues to run
10116 * in the source machine (for example if live migration fails), small
10117 * sptes will remain around and cause bad performance.
10118 *
10119 * Scan sptes if dirty logging has been stopped, dropping those
10120 * which can be collapsed into a single large-page spte. Later
10121 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010122 *
10123 * There is no need to do this in any of the following cases:
10124 * CREATE: No dirty mappings will already exist.
10125 * MOVE/DELETE: The old mappings will already have been cleaned up by
10126 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010127 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010128 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010129 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10130 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10131 kvm_mmu_zap_collapsible_sptes(kvm, new);
10132
10133 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010134 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010135 *
Kai Huang88178fd2015-01-28 10:54:27 +080010136 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10137 * been zapped so no dirty logging staff is needed for old slot. For
10138 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10139 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010140 *
10141 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010142 */
Kai Huang88178fd2015-01-28 10:54:27 +080010143 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010144 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010145
10146 /* Free the arrays associated with the old memslot. */
10147 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010148 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010149}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010150
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010151void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010152{
Sean Christopherson7390de12019-02-05 13:01:31 -080010153 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010154}
10155
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010156void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10157 struct kvm_memory_slot *slot)
10158{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010159 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010160}
10161
Liran Alone6c67d82018-09-04 10:56:52 +030010162static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10163{
10164 return (is_guest_mode(vcpu) &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010165 kvm_x86_ops.guest_apic_has_interrupt &&
10166 kvm_x86_ops.guest_apic_has_interrupt(vcpu));
Liran Alone6c67d82018-09-04 10:56:52 +030010167}
10168
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010169static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10170{
10171 if (!list_empty_careful(&vcpu->async_pf.done))
10172 return true;
10173
10174 if (kvm_apic_has_events(vcpu))
10175 return true;
10176
10177 if (vcpu->arch.pv.pv_unhalted)
10178 return true;
10179
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010180 if (vcpu->arch.exception.pending)
10181 return true;
10182
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010183 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10184 (vcpu->arch.nmi_pending &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010185 kvm_x86_ops.nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010186 return true;
10187
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010188 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10189 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010190 return true;
10191
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010192 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010193 (kvm_cpu_has_interrupt(vcpu) ||
10194 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010195 return true;
10196
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010197 if (kvm_hv_has_stimer_pending(vcpu))
10198 return true;
10199
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010200 return false;
10201}
10202
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010203int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10204{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010205 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010206}
Zhang Xiantao57361992007-12-17 14:21:40 +080010207
Wanpeng Li17e433b2019-08-05 10:03:19 +080010208bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10209{
10210 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10211 return true;
10212
10213 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10214 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10215 kvm_test_request(KVM_REQ_EVENT, vcpu))
10216 return true;
10217
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010218 if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu))
Wanpeng Li17e433b2019-08-05 10:03:19 +080010219 return true;
10220
10221 return false;
10222}
10223
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010224bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10225{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010226 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010227}
10228
Christoffer Dallb6d33832012-03-08 16:44:24 -050010229int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010230{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010231 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010232}
Gleb Natapov78646122009-03-23 12:12:11 +020010233
10234int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10235{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010236 return kvm_x86_ops.interrupt_allowed(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +020010237}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010238
Nadav Amit82b32772014-11-02 11:54:45 +020010239unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10240{
10241 if (is_64_bit_mode(vcpu))
10242 return kvm_rip_read(vcpu);
10243 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10244 kvm_rip_read(vcpu));
10245}
10246EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10247
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010248bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10249{
Nadav Amit82b32772014-11-02 11:54:45 +020010250 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010251}
10252EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10253
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010254unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10255{
10256 unsigned long rflags;
10257
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010258 rflags = kvm_x86_ops.get_rflags(vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010259 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010260 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010261 return rflags;
10262}
10263EXPORT_SYMBOL_GPL(kvm_get_rflags);
10264
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010265static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010266{
10267 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010268 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010269 rflags |= X86_EFLAGS_TF;
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010270 kvm_x86_ops.set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010271}
10272
10273void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10274{
10275 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010276 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010277}
10278EXPORT_SYMBOL_GPL(kvm_set_rflags);
10279
Gleb Natapov56028d02010-10-17 18:13:42 +020010280void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10281{
10282 int r;
10283
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010284 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010285 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010286 return;
10287
10288 r = kvm_mmu_reload(vcpu);
10289 if (unlikely(r))
10290 return;
10291
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010292 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010293 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010294 return;
10295
Sean Christopherson7a026742020-02-06 14:14:34 -080010296 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010297}
10298
Gleb Natapovaf585b92010-10-14 11:22:46 +020010299static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10300{
10301 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10302}
10303
10304static inline u32 kvm_async_pf_next_probe(u32 key)
10305{
10306 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10307}
10308
10309static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10310{
10311 u32 key = kvm_async_pf_hash_fn(gfn);
10312
10313 while (vcpu->arch.apf.gfns[key] != ~0)
10314 key = kvm_async_pf_next_probe(key);
10315
10316 vcpu->arch.apf.gfns[key] = gfn;
10317}
10318
10319static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10320{
10321 int i;
10322 u32 key = kvm_async_pf_hash_fn(gfn);
10323
10324 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010325 (vcpu->arch.apf.gfns[key] != gfn &&
10326 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010327 key = kvm_async_pf_next_probe(key);
10328
10329 return key;
10330}
10331
10332bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10333{
10334 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10335}
10336
10337static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10338{
10339 u32 i, j, k;
10340
10341 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10342 while (true) {
10343 vcpu->arch.apf.gfns[i] = ~0;
10344 do {
10345 j = kvm_async_pf_next_probe(j);
10346 if (vcpu->arch.apf.gfns[j] == ~0)
10347 return;
10348 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10349 /*
10350 * k lies cyclically in ]i,j]
10351 * | i.k.j |
10352 * |....j i.k.| or |.k..j i...|
10353 */
10354 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10355 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10356 i = j;
10357 }
10358}
10359
Gleb Natapov7c907052010-10-14 11:22:53 +020010360static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10361{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010362
10363 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10364 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010365}
10366
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010367static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10368{
10369
10370 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10371 sizeof(u32));
10372}
10373
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010374static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10375{
10376 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10377 return false;
10378
10379 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10380 (vcpu->arch.apf.send_user_only &&
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010381 kvm_x86_ops.get_cpl(vcpu) == 0))
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010382 return false;
10383
10384 return true;
10385}
10386
10387bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10388{
10389 if (unlikely(!lapic_in_kernel(vcpu) ||
10390 kvm_event_needs_reinjection(vcpu) ||
10391 vcpu->arch.exception.pending))
10392 return false;
10393
10394 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10395 return false;
10396
10397 /*
10398 * If interrupts are off we cannot even use an artificial
10399 * halt state.
10400 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010401 return kvm_x86_ops.interrupt_allowed(vcpu);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010402}
10403
Gleb Natapovaf585b92010-10-14 11:22:46 +020010404void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10405 struct kvm_async_pf *work)
10406{
Avi Kivity6389ee92010-11-29 16:12:30 +020010407 struct x86_exception fault;
10408
Sean Christopherson736c2912019-12-06 15:57:14 -080010409 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010410 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010411
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010412 if (kvm_can_deliver_async_pf(vcpu) &&
10413 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010414 fault.vector = PF_VECTOR;
10415 fault.error_code_valid = true;
10416 fault.error_code = 0;
10417 fault.nested_page_fault = false;
10418 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010419 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010420 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010421 } else {
10422 /*
10423 * It is not possible to deliver a paravirtualized asynchronous
10424 * page fault, but putting the guest in an artificial halt state
10425 * can be beneficial nevertheless: if an interrupt arrives, we
10426 * can deliver it timely and perhaps the guest will schedule
10427 * another process. When the instruction that triggered a page
10428 * fault is retried, hopefully the page will be ready in the host.
10429 */
10430 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010431 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010432}
10433
10434void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10435 struct kvm_async_pf *work)
10436{
Avi Kivity6389ee92010-11-29 16:12:30 +020010437 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010438 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010439
chai wenf2e10662013-10-14 22:22:33 +080010440 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010441 work->arch.token = ~0; /* broadcast wakeup */
10442 else
10443 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010444 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010445
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010446 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10447 !apf_get_user(vcpu, &val)) {
10448 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10449 vcpu->arch.exception.pending &&
10450 vcpu->arch.exception.nr == PF_VECTOR &&
10451 !apf_put_user(vcpu, 0)) {
10452 vcpu->arch.exception.injected = false;
10453 vcpu->arch.exception.pending = false;
10454 vcpu->arch.exception.nr = 0;
10455 vcpu->arch.exception.has_error_code = false;
10456 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010457 vcpu->arch.exception.has_payload = false;
10458 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010459 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10460 fault.vector = PF_VECTOR;
10461 fault.error_code_valid = true;
10462 fault.error_code = 0;
10463 fault.nested_page_fault = false;
10464 fault.address = work->arch.token;
10465 fault.async_page_fault = true;
10466 kvm_inject_page_fault(vcpu, &fault);
10467 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010468 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010469 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010470 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010471}
10472
10473bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10474{
10475 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10476 return true;
10477 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010478 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010479}
10480
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010481void kvm_arch_start_assignment(struct kvm *kvm)
10482{
10483 atomic_inc(&kvm->arch.assigned_device_count);
10484}
10485EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10486
10487void kvm_arch_end_assignment(struct kvm *kvm)
10488{
10489 atomic_dec(&kvm->arch.assigned_device_count);
10490}
10491EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10492
10493bool kvm_arch_has_assigned_device(struct kvm *kvm)
10494{
10495 return atomic_read(&kvm->arch.assigned_device_count);
10496}
10497EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10498
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010499void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10500{
10501 atomic_inc(&kvm->arch.noncoherent_dma_count);
10502}
10503EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10504
10505void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10506{
10507 atomic_dec(&kvm->arch.noncoherent_dma_count);
10508}
10509EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10510
10511bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10512{
10513 return atomic_read(&kvm->arch.noncoherent_dma_count);
10514}
10515EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10516
Alex Williamson14717e22016-05-05 11:58:35 -060010517bool kvm_arch_has_irq_bypass(void)
10518{
Sean Christopherson92735b12019-08-02 15:06:17 -070010519 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010520}
10521
Feng Wu87276882015-09-18 22:29:40 +080010522int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10523 struct irq_bypass_producer *prod)
10524{
10525 struct kvm_kernel_irqfd *irqfd =
10526 container_of(cons, struct kvm_kernel_irqfd, consumer);
10527
Alex Williamson14717e22016-05-05 11:58:35 -060010528 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010529
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010530 return kvm_x86_ops.update_pi_irte(irqfd->kvm,
Alex Williamson14717e22016-05-05 11:58:35 -060010531 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010532}
10533
10534void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10535 struct irq_bypass_producer *prod)
10536{
10537 int ret;
10538 struct kvm_kernel_irqfd *irqfd =
10539 container_of(cons, struct kvm_kernel_irqfd, consumer);
10540
Feng Wu87276882015-09-18 22:29:40 +080010541 WARN_ON(irqfd->producer != prod);
10542 irqfd->producer = NULL;
10543
10544 /*
10545 * When producer of consumer is unregistered, we change back to
10546 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010547 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010548 * int this case doesn't want to receive the interrupts.
10549 */
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010550 ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
Feng Wu87276882015-09-18 22:29:40 +080010551 if (ret)
10552 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10553 " fails: %d\n", irqfd->consumer.token, ret);
10554}
10555
10556int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10557 uint32_t guest_irq, bool set)
10558{
Sean Christophersonafaf0b22020-03-21 13:26:00 -070010559 return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set);
Feng Wu87276882015-09-18 22:29:40 +080010560}
10561
Feng Wu520040142016-01-25 16:53:33 +080010562bool kvm_vector_hashing_enabled(void)
10563{
10564 return vector_hashing;
10565}
Feng Wu520040142016-01-25 16:53:33 +080010566
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010567bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10568{
10569 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10570}
10571EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10572
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010573u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10574{
10575 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10576
10577 /* The STIBP bit doesn't fault even if it's not advertised */
10578 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10579 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10580 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10581 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10582 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10583 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10584
10585 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10586 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10587 bits &= ~SPEC_CTRL_SSBD;
10588 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10589 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10590 bits &= ~SPEC_CTRL_SSBD;
10591
10592 return bits;
10593}
10594EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010595
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010596EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010597EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010598EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10599EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10600EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10601EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010602EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010603EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010604EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010605EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010606EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010607EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010608EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010609EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010610EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010611EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010612EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010613EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010614EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10615EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanitab56f8e2020-03-12 05:39:28 -050010616EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010617EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);