blob: 935cd40cbae26109b060a0a6297ed2d9ba5474a4 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Carsten Otte043405e2007-10-10 17:16:19 +02002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * derived from drivers/kvm/kvm_main.c
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03008 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Carsten Otte043405e2007-10-10 17:16:19 +020011 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030015 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020017 */
18
Avi Kivityedf88412007-12-16 11:02:48 +020019#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020020#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080022#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020023#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -080025#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030026#include "x86.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020027#include "cpuid.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020028#include "pmu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030029#include "hyperv.h"
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -060030#include "lapic.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020031
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020032#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030033#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020034#include <linux/kvm.h>
35#include <linux/fs.h>
36#include <linux/vmalloc.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040037#include <linux/export.h>
38#include <linux/moduleparam.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080039#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050040#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010041#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030042#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010043#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030044#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020045#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080047#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080048#include <linux/uaccess.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020049#include <linux/hash.h>
Joerg Roedela1b60c12011-09-06 18:46:34 +020050#include <linux/pci.h>
Marcelo Tosatti16e8d742012-11-27 23:29:00 -020051#include <linux/timekeeper_internal.h>
52#include <linux/pvclock_gtod.h>
Feng Wu87276882015-09-18 22:29:40 +080053#include <linux/kvm_irqfd.h>
54#include <linux/irqbypass.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010055#include <linux/sched/stat.h>
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080056#include <linux/sched/isolation.h>
Tom Lendackyd0ec49d2017-07-17 16:10:27 -050057#include <linux/mem_encrypt.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010058
Avi Kivityaec51dc2009-07-01 16:01:02 +030059#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080060
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020061#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080062#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020063#include <asm/desc.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080064#include <asm/mce.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020065#include <linux/kernel_stat.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020066#include <asm/fpu/internal.h> /* Ugh! */
Zachary Amsden1d5f0662010-08-19 22:07:30 -100067#include <asm/pvclock.h>
Avi Kivity217fc9c2010-08-26 13:38:03 +030068#include <asm/div64.h>
Feng Wuefc64402015-09-18 22:29:51 +080069#include <asm/irq_remapping.h>
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +010070#include <asm/mshyperv.h>
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +010071#include <asm/hypervisor.h>
Chao Pengbf8c55d2018-10-24 16:05:14 +080072#include <asm/intel_pt.h>
Masami Hiramatsub3dc0692019-09-06 22:13:59 +090073#include <asm/emulate_prefix.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000074#include <clocksource/hyperv_timer.h>
Carsten Otte043405e2007-10-10 17:16:19 +020075
Dave Hansend1898b72016-06-01 10:42:20 -070076#define CREATE_TRACE_POINTS
77#include "trace.h"
78
Carsten Otte313a3dc2007-10-11 19:16:52 +020079#define MAX_IO_MSRS 256
Huang Ying890ca9a2009-05-11 16:48:15 +080080#define KVM_MAX_MCE_BANKS 32
Ashok Rajc45dcc72016-06-22 14:59:56 +080081u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
82EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
Huang Ying890ca9a2009-05-11 16:48:15 +080083
Avi Kivity0f65dd72011-04-20 13:37:53 +030084#define emul_to_vcpu(ctxt) \
Sean Christophersonc9b8b072020-02-18 15:29:48 -080085 ((struct kvm_vcpu *)(ctxt)->vcpu)
Avi Kivity0f65dd72011-04-20 13:37:53 +030086
Joerg Roedel50a37eb2008-01-31 14:57:38 +010087/* EFER defaults:
88 * - enable syscall per default because its emulated by KVM
89 * - enable LME and LMA per default on 64 bit KVM
90 */
91#ifdef CONFIG_X86_64
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080092static
93u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
Joerg Roedel50a37eb2008-01-31 14:57:38 +010094#else
Lai Jiangshan1260edbe2011-02-21 11:51:35 +080095static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
Joerg Roedel50a37eb2008-01-31 14:57:38 +010096#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020097
Sean Christophersonb11306b2019-12-10 14:44:13 -080098static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
99
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200100#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
101#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
Hollis Blanchard417bc302007-10-31 17:24:23 -0500102
Radim Krčmářc5192652016-07-12 22:09:28 +0200103#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
104 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
Radim Krčmář371313132016-07-12 22:09:27 +0200105
Gleb Natapovcb142eb2009-08-09 15:17:40 +0300106static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300107static void process_nmi(struct kvm_vcpu *vcpu);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +0200108static void enter_smm(struct kvm_vcpu *vcpu);
Paolo Bonzini6addfc42014-03-27 11:29:28 +0100109static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
Ken Hofsass01643c52018-01-31 16:03:36 -0800110static void store_regs(struct kvm_vcpu *vcpu);
111static int sync_regs(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +0200112
Paolo Bonzini893590c2015-11-06 11:46:24 +0100113struct kvm_x86_ops *kvm_x86_ops __read_mostly;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300114EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +0800115
Paolo Bonzini893590c2015-11-06 11:46:24 +0100116static bool __read_mostly ignore_msrs = 0;
Rusty Russell476bc002012-01-13 09:32:18 +1030117module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
Andre Przywaraed85c062009-06-25 12:36:49 +0200118
Eyal Moscovicifab0aa32017-11-08 14:32:08 +0200119static bool __read_mostly report_ignored_msrs = true;
120module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
121
Wanpeng Li4c276252018-05-05 04:02:32 -0700122unsigned int min_timer_period_us = 200;
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200123module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
124
Marcelo Tosatti630994b2015-05-12 22:42:04 -0300125static bool __read_mostly kvmclock_periodic_sync = true;
126module_param(kvmclock_periodic_sync, bool, S_IRUGO);
127
Paolo Bonzini893590c2015-11-06 11:46:24 +0100128bool __read_mostly kvm_has_tsc_control;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100129EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
Paolo Bonzini893590c2015-11-06 11:46:24 +0100130u32 __read_mostly kvm_max_guest_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100131EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800132u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
133EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
134u64 __read_mostly kvm_max_tsc_scaling_ratio;
135EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
Yunhong Jiang64672c92016-06-13 14:19:59 -0700136u64 __read_mostly kvm_default_tsc_scaling_ratio;
137EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100138
Zachary Amsdencc578282012-02-03 15:43:50 -0200139/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
Paolo Bonzini893590c2015-11-06 11:46:24 +0100140static u32 __read_mostly tsc_tolerance_ppm = 250;
Zachary Amsdencc578282012-02-03 15:43:50 -0200141module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
142
Sean Christophersonc3941d92019-04-17 10:15:33 -0700143/*
144 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
145 * adaptive tuning starting from default advancment of 1000ns. '0' disables
146 * advancement entirely. Any other value is used as-is and disables adaptive
147 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
148 */
149static int __read_mostly lapic_timer_advance_ns = -1;
Wanpeng Li0e6edce2019-05-20 16:18:06 +0800150module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
Marcelo Tosattid0659d92014-12-16 09:08:15 -0500151
Feng Wu520040142016-01-25 16:53:33 +0800152static bool __read_mostly vector_hashing = true;
153module_param(vector_hashing, bool, S_IRUGO);
154
Liran Alonc4ae60e2018-03-12 13:12:47 +0200155bool __read_mostly enable_vmware_backdoor = false;
156module_param(enable_vmware_backdoor, bool, S_IRUGO);
157EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
158
Wanpeng Li6c86eed2018-04-03 16:28:49 -0700159static bool __read_mostly force_emulation_prefix = false;
160module_param(force_emulation_prefix, bool, S_IRUGO);
161
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800162int __read_mostly pi_inject_timer = -1;
163module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
164
Avi Kivity18863bd2009-09-07 11:12:18 +0300165#define KVM_NR_SHARED_MSRS 16
166
167struct kvm_shared_msrs_global {
168 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800169 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300170};
171
172struct kvm_shared_msrs {
173 struct user_return_notifier urn;
174 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800175 struct kvm_shared_msr_values {
176 u64 host;
177 u64 curr;
178 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300179};
180
181static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200182static struct kvm_shared_msrs __percpu *shared_msrs;
Avi Kivity18863bd2009-09-07 11:12:18 +0300183
Sean Christophersoncfc48182020-03-02 15:56:23 -0800184#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
185 | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
186 | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
187 | XFEATURE_MASK_PKRU)
188
Aaron Lewis139a12c2019-10-21 16:30:25 -0700189static u64 __read_mostly host_xss;
190
Hollis Blanchard417bc302007-10-31 17:24:23 -0500191struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200192 { "pf_fixed", VCPU_STAT(pf_fixed) },
193 { "pf_guest", VCPU_STAT(pf_guest) },
194 { "tlb_flush", VCPU_STAT(tlb_flush) },
195 { "invlpg", VCPU_STAT(invlpg) },
196 { "exits", VCPU_STAT(exits) },
197 { "io_exits", VCPU_STAT(io_exits) },
198 { "mmio_exits", VCPU_STAT(mmio_exits) },
199 { "signal_exits", VCPU_STAT(signal_exits) },
200 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800201 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200202 { "halt_exits", VCPU_STAT(halt_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +0100203 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +0200204 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +0200205 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200206 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530207 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200208 { "request_irq", VCPU_STAT(request_irq_exits) },
209 { "irq_exits", VCPU_STAT(irq_exits) },
210 { "host_state_reload", VCPU_STAT(host_state_reload) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200211 { "fpu_reload", VCPU_STAT(fpu_reload) },
212 { "insn_emulation", VCPU_STAT(insn_emulation) },
213 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300214 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200215 { "nmi_injections", VCPU_STAT(nmi_injections) },
Paolo Bonzini0f1e2612016-12-17 16:05:19 +0100216 { "req_event", VCPU_STAT(req_event) },
Paolo Bonzinic595cee2018-07-02 13:07:14 +0200217 { "l1d_flush", VCPU_STAT(l1d_flush) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200218 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
219 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
220 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
221 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
222 { "mmu_flooded", VM_STAT(mmu_flooded) },
223 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200224 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300225 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200226 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Paolo Bonzini833b45d2019-09-30 18:48:44 +0200227 { "largepages", VM_STAT(lpages, .mode = 0444) },
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100228 { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
David Matlackf3414bc2016-12-20 15:25:57 -0800229 { "max_mmu_page_hash_collisions",
230 VM_STAT(max_mmu_page_hash_collisions) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500231 { NULL }
232};
233
Dexuan Cui2acf9232010-06-10 11:27:12 +0800234u64 __read_mostly host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800235u64 __read_mostly supported_xcr0;
236EXPORT_SYMBOL_GPL(supported_xcr0);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800237
Marc Orrb666a4b2018-11-06 14:53:56 -0800238struct kmem_cache *x86_fpu_cache;
239EXPORT_SYMBOL_GPL(x86_fpu_cache);
240
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800241static struct kmem_cache *x86_emulator_cache;
242
243static struct kmem_cache *kvm_alloc_emulator_cache(void)
244{
Sean Christopherson06add252020-02-18 15:29:50 -0800245 unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src);
246 unsigned int size = sizeof(struct x86_emulate_ctxt);
247
248 return kmem_cache_create_usercopy("x86_emulator", size,
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800249 __alignof__(struct x86_emulate_ctxt),
Sean Christopherson06add252020-02-18 15:29:50 -0800250 SLAB_ACCOUNT, useroffset,
251 size - useroffset, NULL);
Sean Christophersonc9b8b072020-02-18 15:29:48 -0800252}
253
Jan Kiszkab6785de2012-09-20 07:43:17 +0200254static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
Avi Kivityd6aa1002011-04-20 15:47:13 +0300255
Gleb Natapovaf585b92010-10-14 11:22:46 +0200256static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
257{
258 int i;
259 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
260 vcpu->arch.apf.gfns[i] = ~0;
261}
262
Avi Kivity18863bd2009-09-07 11:12:18 +0300263static void kvm_on_user_return(struct user_return_notifier *urn)
264{
265 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300266 struct kvm_shared_msrs *locals
267 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800268 struct kvm_shared_msr_values *values;
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700269 unsigned long flags;
Avi Kivity18863bd2009-09-07 11:12:18 +0300270
Ignacio Alvarado1650b4e2016-11-04 12:15:55 -0700271 /*
272 * Disabling irqs at this point since the following code could be
273 * interrupted and executed through kvm_arch_hardware_disable()
274 */
275 local_irq_save(flags);
276 if (locals->registered) {
277 locals->registered = false;
278 user_return_notifier_unregister(urn);
279 }
280 local_irq_restore(flags);
Avi Kivity18863bd2009-09-07 11:12:18 +0300281 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800282 values = &locals->values[slot];
283 if (values->host != values->curr) {
284 wrmsrl(shared_msrs_global.msrs[slot], values->host);
285 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300286 }
287 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300288}
289
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800290void kvm_define_shared_msr(unsigned slot, u32 msr)
291{
Nadav Amit0123be42014-07-24 15:06:56 +0300292 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
Paolo Bonzinic847fe82015-07-29 11:06:34 +0200293 shared_msrs_global.msrs[slot] = msr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300294 if (slot >= shared_msrs_global.nr)
295 shared_msrs_global.nr = slot + 1;
Avi Kivity18863bd2009-09-07 11:12:18 +0300296}
297EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
298
299static void kvm_shared_msr_cpu_online(void)
300{
Sean Christopherson05c19c22019-11-22 12:04:50 -0800301 unsigned int cpu = smp_processor_id();
302 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
303 u64 value;
304 int i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300305
Sean Christopherson05c19c22019-11-22 12:04:50 -0800306 for (i = 0; i < shared_msrs_global.nr; ++i) {
307 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
308 smsr->values[i].host = value;
309 smsr->values[i].curr = value;
310 }
Avi Kivity18863bd2009-09-07 11:12:18 +0300311}
312
Andy Honig8b3c3102014-08-27 11:16:44 -0700313int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300314{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200315 unsigned int cpu = smp_processor_id();
316 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Andy Honig8b3c3102014-08-27 11:16:44 -0700317 int err;
Avi Kivity18863bd2009-09-07 11:12:18 +0300318
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500319 value = (value & mask) | (smsr->values[slot].host & ~mask);
320 if (value == smsr->values[slot].curr)
Andy Honig8b3c3102014-08-27 11:16:44 -0700321 return 0;
Andy Honig8b3c3102014-08-27 11:16:44 -0700322 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
323 if (err)
324 return 1;
325
Paolo Bonzinide1fca52019-11-18 12:23:00 -0500326 smsr->values[slot].curr = value;
Avi Kivity18863bd2009-09-07 11:12:18 +0300327 if (!smsr->registered) {
328 smsr->urn.on_user_return = kvm_on_user_return;
329 user_return_notifier_register(&smsr->urn);
330 smsr->registered = true;
331 }
Andy Honig8b3c3102014-08-27 11:16:44 -0700332 return 0;
Avi Kivity18863bd2009-09-07 11:12:18 +0300333}
334EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
335
Radim Krčmář13a34e02014-08-28 15:13:03 +0200336static void drop_user_return_notifiers(void)
Avi Kivity3548bab2009-11-28 14:18:47 +0200337{
Marcelo Tosatti013f6a52013-01-03 11:41:39 -0200338 unsigned int cpu = smp_processor_id();
339 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
Avi Kivity3548bab2009-11-28 14:18:47 +0200340
341 if (smsr->registered)
342 kvm_on_user_return(&smsr->urn);
343}
344
Carsten Otte6866b832007-10-29 16:09:10 +0100345u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
346{
Gleb Natapov8a5a87d2012-08-05 15:58:26 +0300347 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100348}
349EXPORT_SYMBOL_GPL(kvm_get_apic_base);
350
Jim Mattson58871642018-05-09 16:56:04 -0400351enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
352{
353 return kvm_apic_mode(kvm_get_apic_base(vcpu));
354}
355EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
356
Jan Kiszka58cb6282014-01-24 16:48:44 +0100357int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte6866b832007-10-29 16:09:10 +0100358{
Jim Mattson58871642018-05-09 16:56:04 -0400359 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
360 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
Radim Krčmářd6321d42017-08-05 00:12:49 +0200361 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
362 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100363
Jim Mattson58871642018-05-09 16:56:04 -0400364 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
Jim Mattsond3802282017-08-10 10:14:13 -0700365 return 1;
Jim Mattson58871642018-05-09 16:56:04 -0400366 if (!msr_info->host_initiated) {
367 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
368 return 1;
369 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
370 return 1;
371 }
Jan Kiszka58cb6282014-01-24 16:48:44 +0100372
373 kvm_lapic_set_base(vcpu, msr_info->data);
Wanpeng Li4abaffc2020-02-26 10:41:02 +0800374 kvm_recalculate_apic_map(vcpu->kvm);
Jan Kiszka58cb6282014-01-24 16:48:44 +0100375 return 0;
Carsten Otte6866b832007-10-29 16:09:10 +0100376}
377EXPORT_SYMBOL_GPL(kvm_set_apic_base);
378
Andi Kleen2605fc22014-05-02 00:44:37 +0200379asmlinkage __visible void kvm_spurious_fault(void)
Geoff Levande3ba45b2013-04-05 19:20:30 +0000380{
381 /* Fault while not rebooting. We want the trace. */
kbuild test robotb4fdcf62019-09-29 18:43:28 +0200382 BUG_ON(!kvm_rebooting);
Geoff Levande3ba45b2013-04-05 19:20:30 +0000383}
384EXPORT_SYMBOL_GPL(kvm_spurious_fault);
385
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200386#define EXCPT_BENIGN 0
387#define EXCPT_CONTRIBUTORY 1
388#define EXCPT_PF 2
389
390static int exception_class(int vector)
391{
392 switch (vector) {
393 case PF_VECTOR:
394 return EXCPT_PF;
395 case DE_VECTOR:
396 case TS_VECTOR:
397 case NP_VECTOR:
398 case SS_VECTOR:
399 case GP_VECTOR:
400 return EXCPT_CONTRIBUTORY;
401 default:
402 break;
403 }
404 return EXCPT_BENIGN;
405}
406
Nadav Amitd6e8c852014-07-24 14:51:24 +0300407#define EXCPT_FAULT 0
408#define EXCPT_TRAP 1
409#define EXCPT_ABORT 2
410#define EXCPT_INTERRUPT 3
411
412static int exception_type(int vector)
413{
414 unsigned int mask;
415
416 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
417 return EXCPT_INTERRUPT;
418
419 mask = 1 << vector;
420
421 /* #DB is trap, as instruction watchpoints are handled elsewhere */
422 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
423 return EXCPT_TRAP;
424
425 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
426 return EXCPT_ABORT;
427
428 /* Reserved exceptions will result in fault */
429 return EXCPT_FAULT;
430}
431
Jim Mattsonda998b42018-10-16 14:29:22 -0700432void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
433{
434 unsigned nr = vcpu->arch.exception.nr;
435 bool has_payload = vcpu->arch.exception.has_payload;
436 unsigned long payload = vcpu->arch.exception.payload;
437
438 if (!has_payload)
439 return;
440
441 switch (nr) {
Jim Mattsonf10c7292018-10-16 14:29:23 -0700442 case DB_VECTOR:
443 /*
444 * "Certain debug exceptions may clear bit 0-3. The
445 * remaining contents of the DR6 register are never
446 * cleared by the processor".
447 */
448 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
449 /*
450 * DR6.RTM is set by all #DB exceptions that don't clear it.
451 */
452 vcpu->arch.dr6 |= DR6_RTM;
453 vcpu->arch.dr6 |= payload;
454 /*
455 * Bit 16 should be set in the payload whenever the #DB
456 * exception should clear DR6.RTM. This makes the payload
457 * compatible with the pending debug exceptions under VMX.
458 * Though not currently documented in the SDM, this also
459 * makes the payload compatible with the exit qualification
460 * for #DB exceptions under VMX.
461 */
462 vcpu->arch.dr6 ^= payload & DR6_RTM;
Oliver Upton307f1cf2020-02-07 02:36:04 -0800463
464 /*
465 * The #DB payload is defined as compatible with the 'pending
466 * debug exceptions' field under VMX, not DR6. While bit 12 is
467 * defined in the 'pending debug exceptions' field (enabled
468 * breakpoint), it is reserved and must be zero in DR6.
469 */
470 vcpu->arch.dr6 &= ~BIT(12);
Jim Mattsonf10c7292018-10-16 14:29:23 -0700471 break;
Jim Mattsonda998b42018-10-16 14:29:22 -0700472 case PF_VECTOR:
473 vcpu->arch.cr2 = payload;
474 break;
475 }
476
477 vcpu->arch.exception.has_payload = false;
478 vcpu->arch.exception.payload = 0;
479}
480EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
481
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200482static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200483 unsigned nr, bool has_error, u32 error_code,
Jim Mattson91e86d22018-10-16 14:29:21 -0700484 bool has_payload, unsigned long payload, bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200485{
486 u32 prev_nr;
487 int class1, class2;
488
Avi Kivity3842d132010-07-27 12:30:24 +0300489 kvm_make_request(KVM_REQ_EVENT, vcpu);
490
Wanpeng Li664f8e22017-08-24 03:35:09 -0700491 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200492 queue:
Nadav Amit3ffb2462014-11-02 11:54:42 +0200493 if (has_error && !is_protmode(vcpu))
494 has_error = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700495 if (reinject) {
496 /*
497 * On vmentry, vcpu->arch.exception.pending is only
498 * true if an event injection was blocked by
499 * nested_run_pending. In that case, however,
500 * vcpu_enter_guest requests an immediate exit,
501 * and the guest shouldn't proceed far enough to
502 * need reinjection.
503 */
504 WARN_ON_ONCE(vcpu->arch.exception.pending);
505 vcpu->arch.exception.injected = true;
Jim Mattson91e86d22018-10-16 14:29:21 -0700506 if (WARN_ON_ONCE(has_payload)) {
507 /*
508 * A reinjected event has already
509 * delivered its payload.
510 */
511 has_payload = false;
512 payload = 0;
513 }
Wanpeng Li664f8e22017-08-24 03:35:09 -0700514 } else {
515 vcpu->arch.exception.pending = true;
516 vcpu->arch.exception.injected = false;
517 }
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200518 vcpu->arch.exception.has_error_code = has_error;
519 vcpu->arch.exception.nr = nr;
520 vcpu->arch.exception.error_code = error_code;
Jim Mattson91e86d22018-10-16 14:29:21 -0700521 vcpu->arch.exception.has_payload = has_payload;
522 vcpu->arch.exception.payload = payload;
Oliver Uptona06230b2020-02-07 02:36:06 -0800523 if (!is_guest_mode(vcpu))
Jim Mattsonda998b42018-10-16 14:29:22 -0700524 kvm_deliver_exception_payload(vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200525 return;
526 }
527
528 /* to check exception */
529 prev_nr = vcpu->arch.exception.nr;
530 if (prev_nr == DF_VECTOR) {
531 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300532 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200533 return;
534 }
535 class1 = exception_class(prev_nr);
536 class2 = exception_class(nr);
537 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
538 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
Wanpeng Li664f8e22017-08-24 03:35:09 -0700539 /*
540 * Generate double fault per SDM Table 5-5. Set
541 * exception.pending = true so that the double fault
542 * can trigger a nested vmexit.
543 */
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200544 vcpu->arch.exception.pending = true;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700545 vcpu->arch.exception.injected = false;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200546 vcpu->arch.exception.has_error_code = true;
547 vcpu->arch.exception.nr = DF_VECTOR;
548 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -0700549 vcpu->arch.exception.has_payload = false;
550 vcpu->arch.exception.payload = 0;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200551 } else
552 /* replace previous exception with a new one in a hope
553 that instruction re-execution will regenerate lost
554 exception */
555 goto queue;
556}
557
Avi Kivity298101d2007-11-25 13:41:11 +0200558void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
559{
Jim Mattson91e86d22018-10-16 14:29:21 -0700560 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200561}
562EXPORT_SYMBOL_GPL(kvm_queue_exception);
563
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200564void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
565{
Jim Mattson91e86d22018-10-16 14:29:21 -0700566 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200567}
568EXPORT_SYMBOL_GPL(kvm_requeue_exception);
569
Jim Mattsonf10c7292018-10-16 14:29:23 -0700570static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
571 unsigned long payload)
572{
573 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
574}
575
Jim Mattsonda998b42018-10-16 14:29:22 -0700576static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
577 u32 error_code, unsigned long payload)
578{
579 kvm_multiple_exception(vcpu, nr, true, error_code,
580 true, payload, false);
581}
582
Kyle Huey6affcbe2016-11-29 12:40:40 -0800583int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200584{
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100585 if (err)
586 kvm_inject_gp(vcpu, 0);
587 else
Kyle Huey6affcbe2016-11-29 12:40:40 -0800588 return kvm_skip_emulated_instruction(vcpu);
589
590 return 1;
Andre Przywaradb8fcef2010-12-21 11:12:01 +0100591}
592EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
Joerg Roedel8df25a32010-09-10 17:30:46 +0200593
Avi Kivity6389ee92010-11-29 16:12:30 +0200594void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200595{
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200596 ++vcpu->stat.pf_guest;
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700597 vcpu->arch.exception.nested_apf =
598 is_guest_mode(vcpu) && fault->async_page_fault;
Jim Mattsonda998b42018-10-16 14:29:22 -0700599 if (vcpu->arch.exception.nested_apf) {
Wanpeng Liadfe20f2017-07-13 18:30:41 -0700600 vcpu->arch.apf.nested_apf_token = fault->address;
Jim Mattsonda998b42018-10-16 14:29:22 -0700601 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
602 } else {
603 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
604 fault->address);
605 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200606}
Nadav Har'El27d6c862011-05-25 23:06:59 +0300607EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200608
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200609static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200610{
Avi Kivity6389ee92010-11-29 16:12:30 +0200611 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
612 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200613 else
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +0200614 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +0200615
616 return fault->nested_page_fault;
Joerg Roedeld4f8cf62010-09-10 17:30:55 +0200617}
618
Sheng Yang3419ffc2008-05-15 09:52:48 +0800619void kvm_inject_nmi(struct kvm_vcpu *vcpu)
620{
Avi Kivity7460fb4a2011-09-20 13:43:14 +0300621 atomic_inc(&vcpu->arch.nmi_queued);
622 kvm_make_request(KVM_REQ_NMI, vcpu);
Sheng Yang3419ffc2008-05-15 09:52:48 +0800623}
624EXPORT_SYMBOL_GPL(kvm_inject_nmi);
625
Avi Kivity298101d2007-11-25 13:41:11 +0200626void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
627{
Jim Mattson91e86d22018-10-16 14:29:21 -0700628 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200629}
630EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
631
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200632void kvm_requeue_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, true);
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200635}
636EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
637
Carsten Ottea03490e2007-10-29 16:09:35 +0100638/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300639 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
640 * a #GP and return false.
641 */
642bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200643{
Avi Kivity0a79b002009-09-01 12:03:25 +0300644 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
645 return true;
646 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
647 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100648}
Avi Kivity0a79b002009-09-01 12:03:25 +0300649EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100650
Nadav Amit16f8a6f2014-10-03 01:10:05 +0300651bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
652{
653 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
654 return true;
655
656 kvm_queue_exception(vcpu, UD_VECTOR);
657 return false;
658}
659EXPORT_SYMBOL_GPL(kvm_require_dr);
660
Carsten Ottea03490e2007-10-29 16:09:35 +0100661/*
Joerg Roedelec92fe42010-09-10 17:30:51 +0200662 * This function will be used to read from the physical memory of the currently
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200663 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
Joerg Roedelec92fe42010-09-10 17:30:51 +0200664 * can read from guest physical or from the guest's guest physical memory.
665 */
666int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
667 gfn_t ngfn, void *data, int offset, int len,
668 u32 access)
669{
Paolo Bonzini54987b72014-09-02 13:23:06 +0200670 struct x86_exception exception;
Joerg Roedelec92fe42010-09-10 17:30:51 +0200671 gfn_t real_gfn;
672 gpa_t ngpa;
673
674 ngpa = gfn_to_gpa(ngfn);
Paolo Bonzini54987b72014-09-02 13:23:06 +0200675 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200676 if (real_gfn == UNMAPPED_GVA)
677 return -EFAULT;
678
679 real_gfn = gpa_to_gfn(real_gfn);
680
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200681 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
Joerg Roedelec92fe42010-09-10 17:30:51 +0200682}
683EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
684
Fengguang Wu69b00492015-01-19 22:33:39 +0800685static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200686 void *data, int offset, int len, u32 access)
687{
688 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
689 data, offset, len, access);
690}
691
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700692static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
693{
694 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
695 rsvd_bits(1, 2);
696}
697
Joerg Roedelec92fe42010-09-10 17:30:51 +0200698/*
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700699 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
Carsten Ottea03490e2007-10-29 16:09:35 +0100700 */
Joerg Roedelff03a072010-09-10 17:30:57 +0200701int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100702{
703 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
704 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
705 int i;
706 int ret;
Joerg Roedelff03a072010-09-10 17:30:57 +0200707 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100708
Joerg Roedelff03a072010-09-10 17:30:57 +0200709 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
710 offset * sizeof(u64), sizeof(pdpte),
711 PFERR_USER_MASK|PFERR_WRITE_MASK);
Carsten Ottea03490e2007-10-29 16:09:35 +0100712 if (ret < 0) {
713 ret = 0;
714 goto out;
715 }
716 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Bandan Das812f30b2016-07-12 18:18:50 -0400717 if ((pdpte[i] & PT_PRESENT_MASK) &&
Sean Christopherson16cfacc2019-09-03 16:36:45 -0700718 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100719 ret = 0;
720 goto out;
721 }
722 }
723 ret = 1;
724
Joerg Roedelff03a072010-09-10 17:30:57 +0200725 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700726 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
727
Carsten Ottea03490e2007-10-29 16:09:35 +0100728out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100729
730 return ret;
731}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100732EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100733
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100734bool pdptrs_changed(struct kvm_vcpu *vcpu)
Avi Kivityd835dfe2007-11-21 02:57:59 +0200735{
Joerg Roedelff03a072010-09-10 17:30:57 +0200736 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200737 int offset;
738 gfn_t gfn;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200739 int r;
740
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200741 if (!is_pae_paging(vcpu))
Avi Kivityd835dfe2007-11-21 02:57:59 +0200742 return false;
743
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700744 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300745 return true;
746
Paolo Bonzinia5121772017-07-24 18:54:38 +0200747 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
748 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
Joerg Roedel3d06b8b2010-09-10 17:30:53 +0200749 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
750 PFERR_USER_MASK | PFERR_WRITE_MASK);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200751 if (r < 0)
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800752 return true;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200753
Miaohe Lin7f7f0d92019-10-25 18:54:34 +0800754 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200755}
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +0100756EXPORT_SYMBOL_GPL(pdptrs_changed);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200757
Avi Kivity49a9b072010-06-10 17:02:14 +0300758int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100759{
Sheng Yangaad82702010-05-12 16:40:42 +0800760 unsigned long old_cr0 = kvm_read_cr0(vcpu);
Xiao Guangrongd81135a2015-05-13 14:42:28 +0800761 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
Sheng Yangaad82702010-05-12 16:40:42 +0800762
Avi Kivityf9a48e62010-01-06 19:10:22 +0200763 cr0 |= X86_CR0_ET;
764
Gleb Natapovab344822010-01-21 15:28:46 +0200765#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300766 if (cr0 & 0xffffffff00000000UL)
767 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200768#endif
769
770 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100771
Gleb Natapov0f122442010-04-28 19:15:31 +0300772 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
773 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100774
Gleb Natapov0f122442010-04-28 19:15:31 +0300775 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
776 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100777
778 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
779#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200780 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100781 int cs_db, cs_l;
782
Gleb Natapov0f122442010-04-28 19:15:31 +0300783 if (!is_pae(vcpu))
784 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100785 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300786 if (cs_l)
787 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100788 } else
789#endif
Joerg Roedelff03a072010-09-10 17:30:57 +0200790 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
Avi Kivity9f8fe502010-12-05 17:30:00 +0200791 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300792 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100793 }
794
Mao, Junjiead756a12012-07-02 01:18:48 +0000795 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
796 return 1;
797
Carsten Ottea03490e2007-10-29 16:09:35 +0100798 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100799
Lai Jiangshand170c412011-02-21 11:21:30 +0800800 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800801 kvm_clear_async_pf_completion_queue(vcpu);
Lai Jiangshand170c412011-02-21 11:21:30 +0800802 kvm_async_pf_hash_reset(vcpu);
803 }
Xiao Guangronge5f3f022010-11-12 14:47:01 +0800804
Sheng Yangaad82702010-05-12 16:40:42 +0800805 if ((cr0 ^ old_cr0) & update_bits)
806 kvm_mmu_reset_context(vcpu);
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800807
Laszlo Ersek879ae182015-11-04 12:54:41 +0100808 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
809 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
810 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongb18d5432015-06-15 16:55:21 +0800811 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
812
Gleb Natapov0f122442010-04-28 19:15:31 +0300813 return 0;
814}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200815EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100816
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200817void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100818{
Avi Kivity49a9b072010-06-10 17:02:14 +0300819 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100820}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200821EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100822
Aaron Lewis139a12c2019-10-21 16:30:25 -0700823void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300824{
Aaron Lewis139a12c2019-10-21 16:30:25 -0700825 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300826
Aaron Lewis139a12c2019-10-21 16:30:25 -0700827 if (vcpu->arch.xcr0 != host_xcr0)
828 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
829
830 if (vcpu->arch.xsaves_enabled &&
831 vcpu->arch.ia32_xss != host_xss)
832 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
833 }
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300834}
Aaron Lewis139a12c2019-10-21 16:30:25 -0700835EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
836
837void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
838{
839 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
840
841 if (vcpu->arch.xcr0 != host_xcr0)
842 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
843
844 if (vcpu->arch.xsaves_enabled &&
845 vcpu->arch.ia32_xss != host_xss)
846 wrmsrl(MSR_IA32_XSS, host_xss);
847 }
848
849}
850EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
Marcelo Tosatti42bdf992013-04-15 23:30:13 -0300851
Fengguang Wu69b00492015-01-19 22:33:39 +0800852static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800853{
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000854 u64 xcr0 = xcr;
855 u64 old_xcr0 = vcpu->arch.xcr0;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200856 u64 valid_bits;
Dexuan Cui2acf9232010-06-10 11:27:12 +0800857
858 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
859 if (index != XCR_XFEATURE_ENABLED_MASK)
860 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700861 if (!(xcr0 & XFEATURE_MASK_FP))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800862 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700863 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
Dexuan Cui2acf9232010-06-10 11:27:12 +0800864 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200865
866 /*
867 * Do not allow the guest to set bits that we do not support
868 * saving. However, xcr0 bit 0 is always set, even if the
869 * emulated CPU does not support XSAVE (see fx_init).
870 */
Dave Hansend91cab72015-09-02 16:31:26 -0700871 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200872 if (xcr0 & ~valid_bits)
Dexuan Cui2acf9232010-06-10 11:27:12 +0800873 return 1;
Paolo Bonzini46c34cb2013-10-17 16:50:46 +0200874
Dave Hansend91cab72015-09-02 16:31:26 -0700875 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
876 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
Liu, Jinsong390bd522014-02-24 10:58:09 +0000877 return 1;
878
Dave Hansend91cab72015-09-02 16:31:26 -0700879 if (xcr0 & XFEATURE_MASK_AVX512) {
880 if (!(xcr0 & XFEATURE_MASK_YMM))
Chao Peng612263b2014-10-22 17:35:24 +0800881 return 1;
Dave Hansend91cab72015-09-02 16:31:26 -0700882 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
Chao Peng612263b2014-10-22 17:35:24 +0800883 return 1;
884 }
Dexuan Cui2acf9232010-06-10 11:27:12 +0800885 vcpu->arch.xcr0 = xcr0;
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000886
Dave Hansend91cab72015-09-02 16:31:26 -0700887 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Liu, Jinsong56c103e2014-02-21 17:39:02 +0000888 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800889 return 0;
890}
891
892int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
893{
Zhanghaoyu (A)764bcbc2013-06-14 07:36:13 +0000894 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
895 __kvm_set_xcr(vcpu, index, xcr)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +0800896 kvm_inject_gp(vcpu, 0);
897 return 1;
898 }
899 return 0;
900}
901EXPORT_SYMBOL_GPL(kvm_set_xcr);
902
Sean Christopherson345599f2019-12-10 14:44:16 -0800903#define __cr4_reserved_bits(__cpu_has, __c) \
904({ \
905 u64 __reserved_bits = CR4_RESERVED_BITS; \
906 \
907 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
908 __reserved_bits |= X86_CR4_OSXSAVE; \
909 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
910 __reserved_bits |= X86_CR4_SMEP; \
911 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
912 __reserved_bits |= X86_CR4_SMAP; \
913 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
914 __reserved_bits |= X86_CR4_FSGSBASE; \
915 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
916 __reserved_bits |= X86_CR4_PKE; \
917 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
918 __reserved_bits |= X86_CR4_LA57; \
Sean Christophersond76c7fb2020-01-28 15:53:44 -0800919 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
920 __reserved_bits |= X86_CR4_UMIP; \
Sean Christopherson345599f2019-12-10 14:44:16 -0800921 __reserved_bits; \
922})
923
Sean Christophersonb11306b2019-12-10 14:44:13 -0800924static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)
925{
Sean Christopherson345599f2019-12-10 14:44:16 -0800926 u64 reserved_bits = __cr4_reserved_bits(cpu_has, c);
Sean Christophersonb11306b2019-12-10 14:44:13 -0800927
Sean Christophersonc10398b2020-03-02 15:56:46 -0800928 if (kvm_cpu_cap_has(X86_FEATURE_LA57))
Sean Christopherson345599f2019-12-10 14:44:16 -0800929 reserved_bits &= ~X86_CR4_LA57;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800930
Sean Christopherson90d2f602020-03-02 15:56:47 -0800931 if (kvm_cpu_cap_has(X86_FEATURE_UMIP))
Sean Christopherson345599f2019-12-10 14:44:16 -0800932 reserved_bits &= ~X86_CR4_UMIP;
Sean Christophersonb11306b2019-12-10 14:44:13 -0800933
934 return reserved_bits;
935}
936
Wanpeng Li3ca94192019-09-18 17:50:10 +0800937static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
938{
Sean Christophersonb11306b2019-12-10 14:44:13 -0800939 if (cr4 & cr4_reserved_bits)
Wanpeng Li3ca94192019-09-18 17:50:10 +0800940 return -EINVAL;
941
Sean Christopherson345599f2019-12-10 14:44:16 -0800942 if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
Wanpeng Li3ca94192019-09-18 17:50:10 +0800943 return -EINVAL;
944
945 return 0;
946}
947
Avi Kivitya83b29c2010-06-10 17:02:15 +0300948int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100949{
Avi Kivityfc78f512009-12-07 12:16:48 +0200950 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Xiao Guangrong0be02262015-05-11 22:55:21 +0800951 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
Huaitong Hanb9baba82016-03-22 16:51:21 +0800952 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
Xiao Guangrong0be02262015-05-11 22:55:21 +0800953
Wanpeng Li3ca94192019-09-18 17:50:10 +0800954 if (kvm_valid_cr4(vcpu, cr4))
Paolo Bonziniae3e61e2016-07-12 10:36:41 +0200955 return 1;
956
Carsten Ottea03490e2007-10-29 16:09:35 +0100957 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300958 if (!(cr4 & X86_CR4_PAE))
959 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300960 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
961 && ((cr4 ^ old_cr4) & pdptr_bits)
Avi Kivity9f8fe502010-12-05 17:30:00 +0200962 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
963 kvm_read_cr3(vcpu)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300964 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100965
Mao, Junjiead756a12012-07-02 01:18:48 +0000966 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
Radim Krčmářd6321d42017-08-05 00:12:49 +0200967 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
Mao, Junjiead756a12012-07-02 01:18:48 +0000968 return 1;
969
970 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
971 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
972 return 1;
973 }
974
Nadav Har'El5e1746d2011-05-25 23:03:24 +0300975 if (kvm_x86_ops->set_cr4(vcpu, cr4))
Gleb Natapov0f122442010-04-28 19:15:31 +0300976 return 1;
977
Mao, Junjiead756a12012-07-02 01:18:48 +0000978 if (((cr4 ^ old_cr4) & pdptr_bits) ||
979 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
Sheng Yangaad82702010-05-12 16:40:42 +0800980 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300981
Huaitong Hanb9baba82016-03-22 16:51:21 +0800982 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
Avi Kivity00b27a32011-11-23 16:30:32 +0200983 kvm_update_cpuid(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +0800984
Gleb Natapov0f122442010-04-28 19:15:31 +0300985 return 0;
986}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200987EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100988
Avi Kivity23902182010-06-10 17:02:16 +0300989int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100990{
Junaid Shahidade61e22018-06-27 14:59:15 -0700991 bool skip_tlb_flush = false;
Paolo Bonziniac146232014-11-10 13:53:25 +0100992#ifdef CONFIG_X86_64
Junaid Shahidc19986f2018-05-04 11:37:13 -0700993 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
994
Junaid Shahidade61e22018-06-27 14:59:15 -0700995 if (pcid_enabled) {
Junaid Shahid208320b2018-06-27 14:59:21 -0700996 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
997 cr3 &= ~X86_CR3_PCID_NOFLUSH;
Junaid Shahidade61e22018-06-27 14:59:15 -0700998 }
Paolo Bonziniac146232014-11-10 13:53:25 +0100999#endif
Nadav Amit9d88fca2014-11-02 11:54:52 +02001000
Avi Kivity9f8fe502010-12-05 17:30:00 +02001001 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
Junaid Shahid956bf352018-06-27 14:59:18 -07001002 if (!skip_tlb_flush) {
1003 kvm_mmu_sync_roots(vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -07001004 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07001005 }
Gleb Natapov0f122442010-04-28 19:15:31 +03001006 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +02001007 }
1008
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001009 if (is_long_mode(vcpu) &&
Wanpeng Lia780a3e2018-05-13 02:24:47 -07001010 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
Yu Zhangd1cd3ce2017-08-24 20:27:53 +08001011 return 1;
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02001012 else if (is_pae_paging(vcpu) &&
1013 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
Nadav Amit346874c2014-04-18 03:35:09 +03001014 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +01001015
Junaid Shahidade61e22018-06-27 14:59:15 -07001016 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
Gleb Natapov0f122442010-04-28 19:15:31 +03001017 vcpu->arch.cr3 = cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001018 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Junaid Shahid7c390d32018-06-27 14:59:06 -07001019
Gleb Natapov0f122442010-04-28 19:15:31 +03001020 return 0;
1021}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001022EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +01001023
Andre Przywaraeea1cff2010-12-21 11:12:00 +01001024int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +01001025{
Gleb Natapov0f122442010-04-28 19:15:31 +03001026 if (cr8 & CR8_RESERVED_BITS)
1027 return 1;
Paolo Bonzini35754c92015-07-29 12:05:37 +02001028 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001029 kvm_lapic_set_tpr(vcpu, cr8);
1030 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001031 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +03001032 return 0;
1033}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001034EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001035
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001036unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +01001037{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001038 if (lapic_in_kernel(vcpu))
Carsten Ottea03490e2007-10-29 16:09:35 +01001039 return kvm_lapic_get_cr8(vcpu);
1040 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001041 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +01001042}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001043EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +01001044
Nadav Amitae561ed2015-04-02 03:10:37 +03001045static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1046{
1047 int i;
1048
1049 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1050 for (i = 0; i < KVM_NR_DB_REGS; i++)
1051 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1052 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1053 }
1054}
1055
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001056static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1057{
1058 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1059 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1060}
1061
Jan Kiszkac8639012012-09-21 05:42:55 +02001062static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1063{
1064 unsigned long dr7;
1065
1066 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1067 dr7 = vcpu->arch.guest_debug_dr7;
1068 else
1069 dr7 = vcpu->arch.dr7;
1070 kvm_x86_ops->set_dr7(vcpu, dr7);
Paolo Bonzini360b9482014-02-21 09:55:56 +01001071 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1072 if (dr7 & DR7_BP_EN_MASK)
1073 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
Jan Kiszkac8639012012-09-21 05:42:55 +02001074}
1075
Nadav Amit6f43ed02014-07-15 17:37:46 +03001076static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1077{
1078 u64 fixed = DR6_FIXED_1;
1079
Radim Krčmářd6321d42017-08-05 00:12:49 +02001080 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
Nadav Amit6f43ed02014-07-15 17:37:46 +03001081 fixed |= DR6_RTM;
1082 return fixed;
1083}
1084
Gleb Natapov338dbc92010-04-28 19:15:32 +03001085static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +03001086{
Marios Pomonisea740052019-12-11 12:47:52 -08001087 size_t size = ARRAY_SIZE(vcpu->arch.db);
1088
Gleb Natapov020df072010-04-13 10:05:23 +03001089 switch (dr) {
1090 case 0 ... 3:
Marios Pomonisea740052019-12-11 12:47:52 -08001091 vcpu->arch.db[array_index_nospec(dr, size)] = val;
Gleb Natapov020df072010-04-13 10:05:23 +03001092 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1093 vcpu->arch.eff_db[dr] = val;
1094 break;
1095 case 4:
Gleb Natapov020df072010-04-13 10:05:23 +03001096 /* fall through */
1097 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +03001098 if (val & 0xffffffff00000000ULL)
1099 return -1; /* #GP */
Nadav Amit6f43ed02014-07-15 17:37:46 +03001100 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001101 kvm_update_dr6(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:
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001137 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1138 *val = vcpu->arch.dr6;
1139 else
1140 *val = kvm_x86_ops->get_dr6(vcpu);
Gleb Natapov020df072010-04-13 10:05:23 +03001141 break;
1142 case 5:
Gleb Natapov020df072010-04-13 10:05:23 +03001143 /* fall through */
1144 default: /* 7 */
1145 *val = vcpu->arch.dr7;
1146 break;
1147 }
Gleb Natapov338dbc92010-04-28 19:15:32 +03001148 return 0;
1149}
Gleb Natapov020df072010-04-13 10:05:23 +03001150EXPORT_SYMBOL_GPL(kvm_get_dr);
1151
Avi Kivity022cd0e2011-11-10 14:57:23 +02001152bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1153{
Sean Christophersonde3cd112019-04-30 10:36:17 -07001154 u32 ecx = kvm_rcx_read(vcpu);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001155 u64 data;
1156 int err;
1157
Wei Huangc6702c92015-06-19 13:44:45 +02001158 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001159 if (err)
1160 return err;
Sean Christophersonde3cd112019-04-30 10:36:17 -07001161 kvm_rax_write(vcpu, (u32)data);
1162 kvm_rdx_write(vcpu, data >> 32);
Avi Kivity022cd0e2011-11-10 14:57:23 +02001163 return err;
1164}
1165EXPORT_SYMBOL_GPL(kvm_rdpmc);
1166
Carsten Otte043405e2007-10-10 17:16:19 +02001167/*
1168 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1169 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1170 *
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001171 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1172 * extract the supported MSRs from the related const lists.
1173 * msrs_to_save is selected from the msrs_to_save_all to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -04001174 * capabilities of the host cpu. This capabilities test skips MSRs that are
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001175 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001176 * may depend on host virtualization features rather than host cpu features.
Carsten Otte043405e2007-10-10 17:16:19 +02001177 */
Glauber Costae3267cb2009-10-06 13:24:50 -04001178
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001179static const u32 msrs_to_save_all[] = {
Carsten Otte043405e2007-10-10 17:16:19 +02001180 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -04001181 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +02001182#ifdef CONFIG_X86_64
1183 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1184#endif
Nadav Har'Elb3897a42013-07-08 19:12:35 +08001185 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001186 MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001187 MSR_IA32_SPEC_CTRL,
Chao Pengbf8c55d2018-10-24 16:05:14 +08001188 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1189 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1190 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1191 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1192 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1193 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001194 MSR_IA32_UMWAIT_CONTROL,
1195
Jim Mattsone2ada662019-08-21 11:20:04 -07001196 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1197 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1198 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1199 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1200 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1201 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1202 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1203 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1204 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1205 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1206 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1207 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1208 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
Jim Mattsone2ada662019-08-21 11:20:04 -07001209 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1210 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1211 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1212 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1213 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1214 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1215 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1216 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1217 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
Carsten Otte043405e2007-10-10 17:16:19 +02001218};
1219
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001220static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
Carsten Otte043405e2007-10-10 17:16:19 +02001221static unsigned num_msrs_to_save;
1222
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001223static const u32 emulated_msrs_all[] = {
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001224 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1225 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1226 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1227 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
Ladi Prosek72c139b2017-07-26 13:32:59 +02001228 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001229 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1230 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001231 HV_X64_MSR_RESET,
Andrey Smetanin11c4b1c2015-09-16 12:29:49 +03001232 HV_X64_MSR_VP_INDEX,
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001233 HV_X64_MSR_VP_RUNTIME,
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001234 HV_X64_MSR_SCONTROL,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001235 HV_X64_MSR_STIMER0_CONFIG,
Ladi Prosekd4abc572018-03-20 15:02:07 +01001236 HV_X64_MSR_VP_ASSIST_PAGE,
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001237 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1238 HV_X64_MSR_TSC_EMULATION_STATUS,
1239
1240 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001241 MSR_KVM_PV_EOI_EN,
1242
Will Auldba904632012-11-29 12:42:50 -08001243 MSR_IA32_TSC_ADJUST,
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08001244 MSR_IA32_TSCDEADLINE,
Xiaoyao Li2bdb76c2019-03-08 15:57:20 +08001245 MSR_IA32_ARCH_CAPABILITIES,
Carsten Otte043405e2007-10-10 17:16:19 +02001246 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +03001247 MSR_IA32_MCG_STATUS,
1248 MSR_IA32_MCG_CTL,
Ashok Rajc45dcc72016-06-22 14:59:56 +08001249 MSR_IA32_MCG_EXT_CTL,
Paolo Bonzini64d60672015-05-07 11:36:11 +02001250 MSR_IA32_SMBASE,
Liran Alon52797bf2017-11-15 13:43:14 +02001251 MSR_SMI_COUNT,
Kyle Hueydb2336a2017-03-20 01:16:28 -07001252 MSR_PLATFORM_INFO,
1253 MSR_MISC_FEATURES_ENABLES,
Tom Lendackybc226f02018-05-10 22:06:39 +02001254 MSR_AMD64_VIRT_SPEC_CTRL,
Liran Alon6c6a2ab2019-04-15 18:45:26 +03001255 MSR_IA32_POWER_CTL,
Paolo Bonzini99634e32020-01-20 14:22:55 +01001256 MSR_IA32_UCODE_REV,
Borislav Petkov191c8132019-04-18 18:32:50 +02001257
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001258 /*
1259 * The following list leaves out MSRs whose values are determined
1260 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1261 * We always support the "true" VMX control MSRs, even if the host
1262 * processor does not, so I am putting these registers here rather
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001263 * than in msrs_to_save_all.
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02001264 */
1265 MSR_IA32_VMX_BASIC,
1266 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1267 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1268 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1269 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1270 MSR_IA32_VMX_MISC,
1271 MSR_IA32_VMX_CR0_FIXED0,
1272 MSR_IA32_VMX_CR4_FIXED0,
1273 MSR_IA32_VMX_VMCS_ENUM,
1274 MSR_IA32_VMX_PROCBASED_CTLS2,
1275 MSR_IA32_VMX_EPT_VPID_CAP,
1276 MSR_IA32_VMX_VMFUNC,
1277
Borislav Petkov191c8132019-04-18 18:32:50 +02001278 MSR_K7_HWCR,
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03001279 MSR_KVM_POLL_CONTROL,
Carsten Otte043405e2007-10-10 17:16:19 +02001280};
1281
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001282static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02001283static unsigned num_emulated_msrs;
1284
Tom Lendacky801e4592018-02-21 13:39:51 -06001285/*
1286 * List of msr numbers which are used to expose MSR-based features that
1287 * can be used by a hypervisor to validate requested CPU features.
1288 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001289static const u32 msr_based_features_all[] = {
Paolo Bonzini13893092018-02-26 13:40:09 +01001290 MSR_IA32_VMX_BASIC,
1291 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1292 MSR_IA32_VMX_PINBASED_CTLS,
1293 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1294 MSR_IA32_VMX_PROCBASED_CTLS,
1295 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1296 MSR_IA32_VMX_EXIT_CTLS,
1297 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1298 MSR_IA32_VMX_ENTRY_CTLS,
1299 MSR_IA32_VMX_MISC,
1300 MSR_IA32_VMX_CR0_FIXED0,
1301 MSR_IA32_VMX_CR0_FIXED1,
1302 MSR_IA32_VMX_CR4_FIXED0,
1303 MSR_IA32_VMX_CR4_FIXED1,
1304 MSR_IA32_VMX_VMCS_ENUM,
1305 MSR_IA32_VMX_PROCBASED_CTLS2,
1306 MSR_IA32_VMX_EPT_VPID_CAP,
1307 MSR_IA32_VMX_VMFUNC,
1308
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01001309 MSR_F10H_DECFG,
Wanpeng Li518e7b92018-02-28 14:03:31 +08001310 MSR_IA32_UCODE_REV,
Paolo Bonzinicd283252018-06-25 14:04:37 +02001311 MSR_IA32_ARCH_CAPABILITIES,
Tom Lendacky801e4592018-02-21 13:39:51 -06001312};
1313
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08001314static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
Tom Lendacky801e4592018-02-21 13:39:51 -06001315static unsigned int num_msr_based_features;
1316
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001317static u64 kvm_get_arch_capabilities(void)
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001318{
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001319 u64 data = 0;
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001320
Xiaoyao Li4d22c172019-04-19 10:16:24 +08001321 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1322 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001323
1324 /*
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01001325 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1326 * the nested hypervisor runs with NX huge pages. If it is not,
1327 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1328 * L1 guests, so it need not worry about its own (L2) guests.
1329 */
1330 data |= ARCH_CAP_PSCHANGE_MC_NO;
1331
1332 /*
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001333 * If we're doing cache flushes (either "always" or "cond")
1334 * we will do one whenever the guest does a vmlaunch/vmresume.
1335 * If an outer hypervisor is doing the cache flush for us
1336 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1337 * capability to the guest too, and if EPT is disabled we're not
1338 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1339 * require a nested hypervisor to do a flush of its own.
1340 */
1341 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1342 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1343
Paolo Bonzini0c549142019-08-19 17:24:07 +02001344 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1345 data |= ARCH_CAP_RDCL_NO;
1346 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1347 data |= ARCH_CAP_SSB_NO;
1348 if (!boot_cpu_has_bug(X86_BUG_MDS))
1349 data |= ARCH_CAP_MDS_NO;
1350
Pawan Guptae1d38b62019-10-23 12:23:33 +02001351 /*
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001352 * On TAA affected systems:
1353 * - nothing to do if TSX is disabled on the host.
1354 * - we emulate TSX_CTRL if present on the host.
1355 * This lets the guest use VERW to clear CPU buffers.
Pawan Guptae1d38b62019-10-23 12:23:33 +02001356 */
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001357 if (!boot_cpu_has(X86_FEATURE_RTM))
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001358 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
Paolo Bonzinicbbaa272019-11-18 18:58:26 +01001359 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1360 data |= ARCH_CAP_TAA_NO;
Pawan Guptae1d38b62019-10-23 12:23:33 +02001361
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001362 return data;
1363}
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001364
Wanpeng Li66421c12018-02-28 14:03:30 +08001365static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1366{
1367 switch (msr->index) {
Paolo Bonzinicd283252018-06-25 14:04:37 +02001368 case MSR_IA32_ARCH_CAPABILITIES:
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02001369 msr->data = kvm_get_arch_capabilities();
1370 break;
1371 case MSR_IA32_UCODE_REV:
Paolo Bonzinicd283252018-06-25 14:04:37 +02001372 rdmsrl_safe(msr->index, &msr->data);
Wanpeng Li518e7b92018-02-28 14:03:31 +08001373 break;
Wanpeng Li66421c12018-02-28 14:03:30 +08001374 default:
1375 if (kvm_x86_ops->get_msr_feature(msr))
1376 return 1;
1377 }
1378 return 0;
1379}
1380
Tom Lendacky801e4592018-02-21 13:39:51 -06001381static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1382{
1383 struct kvm_msr_entry msr;
Wanpeng Li66421c12018-02-28 14:03:30 +08001384 int r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001385
1386 msr.index = index;
Wanpeng Li66421c12018-02-28 14:03:30 +08001387 r = kvm_get_msr_feature(&msr);
1388 if (r)
1389 return r;
Tom Lendacky801e4592018-02-21 13:39:51 -06001390
1391 *data = msr.data;
1392
1393 return 0;
1394}
1395
Sean Christopherson11988492019-04-02 08:19:15 -07001396static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1397{
1398 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
1399 return false;
1400
1401 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
1402 return false;
1403
Sean Christopherson0a629562019-04-02 08:19:16 -07001404 if (efer & (EFER_LME | EFER_LMA) &&
1405 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1406 return false;
1407
1408 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1409 return false;
1410
Sean Christopherson11988492019-04-02 08:19:15 -07001411 return true;
1412
1413}
Jan Kiszka384bb782013-04-20 10:52:36 +02001414bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +01001415{
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001416 if (efer & efer_reserved_bits)
Jan Kiszka384bb782013-04-20 10:52:36 +02001417 return false;
Carsten Otte15c4a642007-10-30 18:44:17 +01001418
Sean Christopherson11988492019-04-02 08:19:15 -07001419 return __kvm_valid_efer(vcpu, efer);
Jan Kiszka384bb782013-04-20 10:52:36 +02001420}
1421EXPORT_SYMBOL_GPL(kvm_valid_efer);
1422
Sean Christopherson11988492019-04-02 08:19:15 -07001423static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Jan Kiszka384bb782013-04-20 10:52:36 +02001424{
1425 u64 old_efer = vcpu->arch.efer;
Sean Christopherson11988492019-04-02 08:19:15 -07001426 u64 efer = msr_info->data;
Jan Kiszka384bb782013-04-20 10:52:36 +02001427
Sean Christopherson11988492019-04-02 08:19:15 -07001428 if (efer & efer_reserved_bits)
Paolo Bonzini66f61c92019-05-24 21:52:46 +02001429 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +02001430
Sean Christopherson11988492019-04-02 08:19:15 -07001431 if (!msr_info->host_initiated) {
1432 if (!__kvm_valid_efer(vcpu, efer))
1433 return 1;
1434
1435 if (is_paging(vcpu) &&
1436 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1437 return 1;
1438 }
Jan Kiszka384bb782013-04-20 10:52:36 +02001439
Carsten Otte15c4a642007-10-30 18:44:17 +01001440 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +02001441 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +01001442
Sheng Yanga3d204e2010-05-12 16:40:40 +08001443 kvm_x86_ops->set_efer(vcpu, efer);
1444
Sheng Yangaad82702010-05-12 16:40:42 +08001445 /* Update reserved bits */
1446 if ((efer ^ old_efer) & EFER_NX)
1447 kvm_mmu_reset_context(vcpu);
1448
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001449 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01001450}
1451
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +01001452void kvm_enable_efer_bits(u64 mask)
1453{
1454 efer_reserved_bits &= ~mask;
1455}
1456EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1457
Carsten Otte15c4a642007-10-30 18:44:17 +01001458/*
Sean Christophersonf20935d2019-09-05 14:22:54 -07001459 * Write @data into the MSR specified by @index. Select MSR specific fault
1460 * checks are bypassed if @host_initiated is %true.
Carsten Otte15c4a642007-10-30 18:44:17 +01001461 * Returns 0 on success, non-0 otherwise.
1462 * Assumes vcpu_load() was already called.
1463 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001464static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1465 bool host_initiated)
Carsten Otte15c4a642007-10-30 18:44:17 +01001466{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001467 struct msr_data msr;
1468
1469 switch (index) {
Nadav Amit854e8bb2014-09-16 03:24:05 +03001470 case MSR_FS_BASE:
1471 case MSR_GS_BASE:
1472 case MSR_KERNEL_GS_BASE:
1473 case MSR_CSTAR:
1474 case MSR_LSTAR:
Sean Christophersonf20935d2019-09-05 14:22:54 -07001475 if (is_noncanonical_address(data, vcpu))
Nadav Amit854e8bb2014-09-16 03:24:05 +03001476 return 1;
1477 break;
1478 case MSR_IA32_SYSENTER_EIP:
1479 case MSR_IA32_SYSENTER_ESP:
1480 /*
1481 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1482 * non-canonical address is written on Intel but not on
1483 * AMD (which ignores the top 32-bits, because it does
1484 * not implement 64-bit SYSENTER).
1485 *
1486 * 64-bit code should hence be able to write a non-canonical
1487 * value on AMD. Making the address canonical ensures that
1488 * vmentry does not fail on Intel after writing a non-canonical
1489 * value, and that something deterministic happens if the guest
1490 * invokes 64-bit SYSENTER.
1491 */
Sean Christophersonf20935d2019-09-05 14:22:54 -07001492 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
Nadav Amit854e8bb2014-09-16 03:24:05 +03001493 }
Sean Christophersonf20935d2019-09-05 14:22:54 -07001494
1495 msr.data = data;
1496 msr.index = index;
1497 msr.host_initiated = host_initiated;
1498
1499 return kvm_x86_ops->set_msr(vcpu, &msr);
1500}
1501
1502/*
1503 * Read the MSR specified by @index into @data. Select MSR specific fault
1504 * checks are bypassed if @host_initiated is %true.
1505 * Returns 0 on success, non-0 otherwise.
1506 * Assumes vcpu_load() was already called.
1507 */
Paolo Bonziniedef5c32019-11-18 12:23:00 -05001508int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1509 bool host_initiated)
Sean Christophersonf20935d2019-09-05 14:22:54 -07001510{
1511 struct msr_data msr;
1512 int ret;
1513
1514 msr.index = index;
1515 msr.host_initiated = host_initiated;
1516
1517 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1518 if (!ret)
1519 *data = msr.data;
1520 return ret;
1521}
1522
1523int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
1524{
1525 return __kvm_get_msr(vcpu, index, data, false);
1526}
1527EXPORT_SYMBOL_GPL(kvm_get_msr);
1528
1529int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1530{
1531 return __kvm_set_msr(vcpu, index, data, false);
Carsten Otte15c4a642007-10-30 18:44:17 +01001532}
Nadav Amit854e8bb2014-09-16 03:24:05 +03001533EXPORT_SYMBOL_GPL(kvm_set_msr);
Carsten Otte15c4a642007-10-30 18:44:17 +01001534
Sean Christopherson1edce0a2019-09-05 14:22:55 -07001535int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1536{
1537 u32 ecx = kvm_rcx_read(vcpu);
1538 u64 data;
1539
1540 if (kvm_get_msr(vcpu, ecx, &data)) {
1541 trace_kvm_msr_read_ex(ecx);
1542 kvm_inject_gp(vcpu, 0);
1543 return 1;
1544 }
1545
1546 trace_kvm_msr_read(ecx, data);
1547
1548 kvm_rax_write(vcpu, data & -1u);
1549 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1550 return kvm_skip_emulated_instruction(vcpu);
1551}
1552EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1553
1554int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1555{
1556 u32 ecx = kvm_rcx_read(vcpu);
1557 u64 data = kvm_read_edx_eax(vcpu);
1558
1559 if (kvm_set_msr(vcpu, ecx, data)) {
1560 trace_kvm_msr_write_ex(ecx, data);
1561 kvm_inject_gp(vcpu, 0);
1562 return 1;
1563 }
1564
1565 trace_kvm_msr_write(ecx, data);
1566 return kvm_skip_emulated_instruction(vcpu);
1567}
1568EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1569
Carsten Otte313a3dc2007-10-11 19:16:52 +02001570/*
Wanpeng Li1e9e2622019-11-21 11:17:11 +08001571 * The fast path for frequent and performance sensitive wrmsr emulation,
1572 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
1573 * the latency of virtual IPI by avoiding the expensive bits of transitioning
1574 * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the
1575 * other cases which must be called after interrupts are enabled on the host.
1576 */
1577static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
1578{
1579 if (lapic_in_kernel(vcpu) && apic_x2apic_mode(vcpu->arch.apic) &&
1580 ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
1581 ((data & APIC_MODE_MASK) == APIC_DM_FIXED)) {
1582
1583 kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
1584 return kvm_lapic_reg_write(vcpu->arch.apic, APIC_ICR, (u32)data);
1585 }
1586
1587 return 1;
1588}
1589
1590enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1591{
1592 u32 msr = kvm_rcx_read(vcpu);
1593 u64 data = kvm_read_edx_eax(vcpu);
1594 int ret = 0;
1595
1596 switch (msr) {
1597 case APIC_BASE_MSR + (APIC_ICR >> 4):
1598 ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1599 break;
1600 default:
1601 return EXIT_FASTPATH_NONE;
1602 }
1603
1604 if (!ret) {
1605 trace_kvm_msr_write(msr, data);
1606 return EXIT_FASTPATH_SKIP_EMUL_INS;
1607 }
1608
1609 return EXIT_FASTPATH_NONE;
1610}
1611EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
1612
1613/*
Carsten Otte313a3dc2007-10-11 19:16:52 +02001614 * Adapt set_msr() to msr_io()'s calling convention
1615 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001616static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1617{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001618 return __kvm_get_msr(vcpu, index, data, true);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001619}
1620
Carsten Otte313a3dc2007-10-11 19:16:52 +02001621static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1622{
Sean Christophersonf20935d2019-09-05 14:22:54 -07001623 return __kvm_set_msr(vcpu, index, *data, true);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001624}
1625
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001626#ifdef CONFIG_X86_64
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001627struct pvclock_clock {
1628 int vclock_mode;
1629 u64 cycle_last;
1630 u64 mask;
1631 u32 mult;
1632 u32 shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001633 u64 base_cycles;
1634 u64 offset;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001635};
1636
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001637struct pvclock_gtod_data {
1638 seqcount_t seq;
1639
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001640 struct pvclock_clock clock; /* extract of a clocksource struct */
1641 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001642
Paolo Bonzini917f9472020-01-22 14:32:20 +01001643 ktime_t offs_boot;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001644 u64 wall_time_sec;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001645};
1646
1647static struct pvclock_gtod_data pvclock_gtod_data;
1648
1649static void update_pvclock_gtod(struct timekeeper *tk)
1650{
1651 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
1652
1653 write_seqcount_begin(&vdata->seq);
1654
1655 /* copy pvclock gtod data */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001656 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1657 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1658 vdata->clock.mask = tk->tkr_mono.mask;
1659 vdata->clock.mult = tk->tkr_mono.mult;
1660 vdata->clock.shift = tk->tkr_mono.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001661 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec;
1662 vdata->clock.offset = tk->tkr_mono.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001663
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001664 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode;
1665 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1666 vdata->raw_clock.mask = tk->tkr_raw.mask;
1667 vdata->raw_clock.mult = tk->tkr_raw.mult;
1668 vdata->raw_clock.shift = tk->tkr_raw.shift;
Paolo Bonzini917f9472020-01-22 14:32:20 +01001669 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec;
1670 vdata->raw_clock.offset = tk->tkr_raw.base;
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001671
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02001672 vdata->wall_time_sec = tk->xtime_sec;
1673
Paolo Bonzini917f9472020-01-22 14:32:20 +01001674 vdata->offs_boot = tk->offs_boot;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02001675
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001676 write_seqcount_end(&vdata->seq);
1677}
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001678
1679static s64 get_kvmclock_base_ns(void)
1680{
1681 /* Count up from boot time, but with the frequency of the raw clock. */
1682 return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
1683}
1684#else
1685static s64 get_kvmclock_base_ns(void)
1686{
1687 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */
1688 return ktime_get_boottime_ns();
1689}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001690#endif
1691
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001692void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1693{
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001694 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
Wanpeng Li4d151bf2019-07-06 09:26:50 +08001695 kvm_vcpu_kick(vcpu);
Nicholas Krausebab5bb32015-01-01 22:05:18 -05001696}
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001697
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001698static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1699{
Avi Kivity9ed3c442010-05-04 15:00:37 +03001700 int version;
1701 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001702 struct pvclock_wall_clock wc;
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001703 u64 wall_nsec;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001704
1705 if (!wall_clock)
1706 return;
1707
Avi Kivity9ed3c442010-05-04 15:00:37 +03001708 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1709 if (r)
1710 return;
1711
1712 if (version & 1)
1713 ++version; /* first time write, random junk */
1714
1715 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001716
Nicholas Krause1dab1342015-12-30 13:08:46 -05001717 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1718 return;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001719
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001720 /*
1721 * The guest calculates current wall clock time by adding
Zachary Amsden34c238a2010-09-18 14:38:14 -10001722 * system time (updated by kvm_guest_time_update below) to the
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001723 * wall clock specified here. We do the reverse here.
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001724 */
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001725 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001726
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001727 wc.nsec = do_div(wall_nsec, 1000000000);
1728 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001729 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001730
1731 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1732
1733 version++;
1734 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001735}
1736
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001737static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1738{
Paolo Bonzinib51012d2016-01-22 11:39:22 +01001739 do_shl32_div32(dividend, divisor);
1740 return dividend;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001741}
1742
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001743static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001744 s8 *pshift, u32 *pmultiplier)
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001745{
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001746 uint64_t scaled64;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001747 int32_t shift = 0;
1748 uint64_t tps64;
1749 uint32_t tps32;
1750
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001751 tps64 = base_hz;
1752 scaled64 = scaled_hz;
Jan Kiszka50933622010-09-26 13:00:53 +02001753 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001754 tps64 >>= 1;
1755 shift--;
1756 }
1757
1758 tps32 = (uint32_t)tps64;
Jan Kiszka50933622010-09-26 13:00:53 +02001759 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1760 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001761 scaled64 >>= 1;
1762 else
1763 tps32 <<= 1;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001764 shift++;
1765 }
1766
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10001767 *pshift = shift;
1768 *pmultiplier = div_frac(scaled64, tps32);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02001769}
1770
Marcelo Tosattid8281992012-11-27 23:29:01 -02001771#ifdef CONFIG_X86_64
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001772static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
Marcelo Tosattid8281992012-11-27 23:29:01 -02001773#endif
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02001774
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001775static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
Fengguang Wu69b00492015-01-19 22:33:39 +08001776static unsigned long max_tsc_khz;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001777
Zachary Amsdencc578282012-02-03 15:43:50 -02001778static u32 adjust_tsc_khz(u32 khz, s32 ppm)
Zachary Amsdenc2855452010-09-18 14:38:15 -10001779{
Zachary Amsdencc578282012-02-03 15:43:50 -02001780 u64 v = (u64)khz * (1000000 + ppm);
1781 do_div(v, 1000000);
1782 return v;
1783}
1784
Haozhong Zhang381d5852015-10-20 15:39:04 +08001785static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1786{
1787 u64 ratio;
1788
1789 /* Guest TSC same frequency as host TSC? */
1790 if (!scale) {
1791 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1792 return 0;
1793 }
1794
1795 /* TSC scaling supported? */
1796 if (!kvm_has_tsc_control) {
1797 if (user_tsc_khz > tsc_khz) {
1798 vcpu->arch.tsc_catchup = 1;
1799 vcpu->arch.tsc_always_catchup = 1;
1800 return 0;
1801 } else {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001802 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
Haozhong Zhang381d5852015-10-20 15:39:04 +08001803 return -1;
1804 }
1805 }
1806
1807 /* TSC scaling required - calculate ratio */
1808 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1809 user_tsc_khz, tsc_khz);
1810
1811 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
Paolo Bonzini3f16a5c2019-06-26 14:16:13 +02001812 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1813 user_tsc_khz);
Haozhong Zhang381d5852015-10-20 15:39:04 +08001814 return -1;
1815 }
1816
1817 vcpu->arch.tsc_scaling_ratio = ratio;
1818 return 0;
1819}
1820
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001821static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
Zachary Amsdencc578282012-02-03 15:43:50 -02001822{
1823 u32 thresh_lo, thresh_hi;
1824 int use_scaling = 0;
1825
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001826 /* tsc_khz can be zero if TSC calibration fails */
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001827 if (user_tsc_khz == 0) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001828 /* set tsc_scaling_ratio to a safe value */
1829 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
Haozhong Zhang381d5852015-10-20 15:39:04 +08001830 return -1;
Haozhong Zhangad7218832015-10-20 15:39:02 +08001831 }
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001832
Zachary Amsdenc2855452010-09-18 14:38:15 -10001833 /* Compute a scale to convert nanoseconds in TSC cycles */
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01001834 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
Zachary Amsdencc578282012-02-03 15:43:50 -02001835 &vcpu->arch.virtual_tsc_shift,
1836 &vcpu->arch.virtual_tsc_mult);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001837 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
Zachary Amsdencc578282012-02-03 15:43:50 -02001838
1839 /*
1840 * Compute the variation in TSC rate which is acceptable
1841 * within the range of tolerance and decide if the
1842 * rate being applied is within that bounds of the hardware
1843 * rate. If so, no scaling or compensation need be done.
1844 */
1845 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1846 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001847 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1848 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 -02001849 use_scaling = 1;
1850 }
Paolo Bonzini4941b8c2016-02-08 14:51:12 +01001851 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
Zachary Amsdenc2855452010-09-18 14:38:15 -10001852}
1853
1854static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1855{
Zachary Amsdene26101b2012-02-03 15:43:57 -02001856 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
Zachary Amsdencc578282012-02-03 15:43:50 -02001857 vcpu->arch.virtual_tsc_mult,
1858 vcpu->arch.virtual_tsc_shift);
Zachary Amsdene26101b2012-02-03 15:43:57 -02001859 tsc += vcpu->arch.this_tsc_write;
Zachary Amsdenc2855452010-09-18 14:38:15 -10001860 return tsc;
1861}
1862
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001863static inline int gtod_is_based_on_tsc(int mode)
1864{
1865 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1866}
1867
Fengguang Wu69b00492015-01-19 22:33:39 +08001868static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001869{
1870#ifdef CONFIG_X86_64
1871 bool vcpus_matched;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001872 struct kvm_arch *ka = &vcpu->kvm->arch;
1873 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1874
1875 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1876 atomic_read(&vcpu->kvm->online_vcpus));
1877
Marcelo Tosatti7f187922014-11-04 21:30:44 -02001878 /*
1879 * Once the masterclock is enabled, always perform request in
1880 * order to update it.
1881 *
1882 * In order to enable masterclock, the host clocksource must be TSC
1883 * and the vcpus need to have matched TSCs. When that happens,
1884 * perform request to enable masterclock.
1885 */
1886 if (ka->use_master_clock ||
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001887 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001888 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1889
1890 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1891 atomic_read(&vcpu->kvm->online_vcpus),
1892 ka->use_master_clock, gtod->clock.vclock_mode);
1893#endif
1894}
1895
Will Auldba904632012-11-29 12:42:50 -08001896static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1897{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001898 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
Will Auldba904632012-11-29 12:42:50 -08001899 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1900}
1901
Haozhong Zhang35181e82015-10-20 15:39:03 +08001902/*
1903 * Multiply tsc by a fixed point number represented by ratio.
1904 *
1905 * The most significant 64-N bits (mult) of ratio represent the
1906 * integral part of the fixed point number; the remaining N bits
1907 * (frac) represent the fractional part, ie. ratio represents a fixed
1908 * point number (mult + frac * 2^(-N)).
1909 *
1910 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1911 */
1912static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1913{
1914 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1915}
1916
1917u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1918{
1919 u64 _tsc = tsc;
1920 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1921
1922 if (ratio != kvm_default_tsc_scaling_ratio)
1923 _tsc = __scale_tsc(ratio, tsc);
1924
1925 return _tsc;
1926}
1927EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1928
Haozhong Zhang07c14192015-10-20 15:39:05 +08001929static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1930{
1931 u64 tsc;
1932
1933 tsc = kvm_scale_tsc(vcpu, rdtsc());
1934
1935 return target_tsc - tsc;
1936}
1937
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001938u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1939{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001940 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1941
1942 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
Haozhong Zhang4ba76532015-10-20 15:39:07 +08001943}
1944EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1945
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001946static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1947{
Leonid Shatz326e7422018-11-06 12:14:25 +02001948 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04001949}
1950
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01001951static inline bool kvm_check_tsc_unstable(void)
1952{
1953#ifdef CONFIG_X86_64
1954 /*
1955 * TSC is marked unstable when we're running on Hyper-V,
1956 * 'TSC page' clocksource is good.
1957 */
1958 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1959 return false;
1960#endif
1961 return check_tsc_unstable();
1962}
1963
Will Auld8fe8ab42012-11-29 12:42:12 -08001964void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
Zachary Amsden99e3e302010-08-19 22:07:17 -10001965{
1966 struct kvm *kvm = vcpu->kvm;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001967 u64 offset, ns, elapsed;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001968 unsigned long flags;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02001969 bool matched;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02001970 bool already_matched;
Will Auld8fe8ab42012-11-29 12:42:12 -08001971 u64 data = msr->data;
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001972 bool synchronizing = false;
Zachary Amsden99e3e302010-08-19 22:07:17 -10001973
Jan Kiszka038f8c12011-02-04 10:49:11 +01001974 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
Haozhong Zhang07c14192015-10-20 15:39:05 +08001975 offset = kvm_compute_tsc_offset(vcpu, data);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01001976 ns = get_kvmclock_base_ns();
Zachary Amsdenf38e0982010-08-19 22:07:20 -10001977 elapsed = ns - kvm->arch.last_tsc_nsec;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02001978
Marcelo Tosatti03ba32c2013-03-11 23:10:24 -03001979 if (vcpu->arch.virtual_tsc_khz) {
Denis Plotnikovbd8fab32017-04-07 12:09:53 +03001980 if (data == 0 && msr->host_initiated) {
1981 /*
1982 * detection of vcpu initialization -- need to sync
1983 * with other vCPUs. This particularly helps to keep
1984 * kvm_clock stable after CPU hotplug
1985 */
1986 synchronizing = true;
1987 } else {
1988 u64 tsc_exp = kvm->arch.last_tsc_write +
1989 nsec_to_cycles(vcpu, elapsed);
1990 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1991 /*
1992 * Special case: TSC write with a small delta (1 second)
1993 * of virtual cycle time against real time is
1994 * interpreted as an attempt to synchronize the CPU.
1995 */
1996 synchronizing = data < tsc_exp + tsc_hz &&
1997 data + tsc_hz > tsc_exp;
1998 }
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03001999 }
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002000
2001 /*
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002002 * For a reliable TSC, we can match TSC offsets, and for an unstable
2003 * TSC, we add elapsed time in this computation. We could let the
2004 * compensation code attempt to catch up if we fall behind, but
2005 * it's better to try to match offsets from the beginning.
2006 */
Denis Plotnikovc5e8ec82017-04-07 12:09:52 +03002007 if (synchronizing &&
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002008 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002009 if (!kvm_check_tsc_unstable()) {
Zachary Amsdene26101b2012-02-03 15:43:57 -02002010 offset = kvm->arch.cur_tsc_offset;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002011 } else {
Joerg Roedel857e4092011-03-25 09:44:50 +01002012 u64 delta = nsec_to_cycles(vcpu, elapsed);
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002013 data += delta;
Haozhong Zhang07c14192015-10-20 15:39:05 +08002014 offset = kvm_compute_tsc_offset(vcpu, data);
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002015 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002016 matched = true;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002017 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002018 } else {
2019 /*
2020 * We split periods of matched TSC writes into generations.
2021 * For each generation, we track the original measured
2022 * nanosecond time, offset, and write, so if TSCs are in
2023 * sync, we can match exact offset, and if not, we can match
Guo Chao4a969982012-06-28 15:17:27 +08002024 * exact software computation in compute_guest_tsc()
Zachary Amsdene26101b2012-02-03 15:43:57 -02002025 *
2026 * These values are tracked in kvm->arch.cur_xxx variables.
2027 */
2028 kvm->arch.cur_tsc_generation++;
2029 kvm->arch.cur_tsc_nsec = ns;
2030 kvm->arch.cur_tsc_write = data;
2031 kvm->arch.cur_tsc_offset = offset;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002032 matched = false;
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002033 }
Zachary Amsdene26101b2012-02-03 15:43:57 -02002034
2035 /*
2036 * We also track th most recent recorded KHZ, write and time to
2037 * allow the matching interval to be extended at each write.
2038 */
Zachary Amsdenf38e0982010-08-19 22:07:20 -10002039 kvm->arch.last_tsc_nsec = ns;
2040 kvm->arch.last_tsc_write = data;
Zachary Amsden5d3cb0f62012-02-03 15:43:51 -02002041 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
Zachary Amsden99e3e302010-08-19 22:07:17 -10002042
Zachary Amsdenb183aa52012-02-03 15:43:53 -02002043 vcpu->arch.last_guest_tsc = data;
Zachary Amsdene26101b2012-02-03 15:43:57 -02002044
2045 /* Keep track of which generation this VCPU has synchronized to */
2046 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
2047 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
2048 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
2049
Radim Krčmářd6321d42017-08-05 00:12:49 +02002050 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
Will Auldba904632012-11-29 12:42:50 -08002051 update_ia32_tsc_adjust_msr(vcpu, offset);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002052
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04002053 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdene26101b2012-02-03 15:43:57 -02002054 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002055
2056 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002057 if (!matched) {
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002058 kvm->arch.nr_vcpus_matched_tsc = 0;
Tomasz Grabiec0d3da0d2014-06-24 09:42:43 +02002059 } else if (!already_matched) {
2060 kvm->arch.nr_vcpus_matched_tsc++;
2061 }
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002062
2063 kvm_track_tsc_matching(vcpu);
2064 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
Zachary Amsden99e3e302010-08-19 22:07:17 -10002065}
Zachary Amsdene26101b2012-02-03 15:43:57 -02002066
Zachary Amsden99e3e302010-08-19 22:07:17 -10002067EXPORT_SYMBOL_GPL(kvm_write_tsc);
2068
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002069static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
2070 s64 adjustment)
2071{
Leonid Shatz326e7422018-11-06 12:14:25 +02002072 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
2073 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002074}
2075
2076static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
2077{
2078 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
2079 WARN_ON(adjustment < 0);
2080 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +01002081 adjust_tsc_offset_guest(vcpu, adjustment);
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002082}
2083
Marcelo Tosattid8281992012-11-27 23:29:01 -02002084#ifdef CONFIG_X86_64
2085
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002086static u64 read_tsc(void)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002087{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002088 u64 ret = (u64)rdtsc_ordered();
Andy Lutomirski03b97302015-06-25 18:44:08 +02002089 u64 last = pvclock_gtod_data.clock.cycle_last;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002090
2091 if (likely(ret >= last))
2092 return ret;
2093
2094 /*
2095 * GCC likes to generate cmov here, but this branch is extremely
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08002096 * predictable (it's just a function of time and the likely is
Marcelo Tosattid8281992012-11-27 23:29:01 -02002097 * very likely) and there's a data dependence, so force GCC
2098 * to generate a branch instead. I don't barrier() because
2099 * we don't actually need a barrier, and if this function
2100 * ever gets inlined it will generate worse code.
2101 */
2102 asm volatile ("");
2103 return last;
2104}
2105
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002106static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2107 int *mode)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002108{
2109 long v;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002110 u64 tsc_pg_val;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002111
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002112 switch (clock->vclock_mode) {
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002113 case VCLOCK_HVCLOCK:
2114 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2115 tsc_timestamp);
2116 if (tsc_pg_val != U64_MAX) {
2117 /* TSC page valid */
2118 *mode = VCLOCK_HVCLOCK;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002119 v = (tsc_pg_val - clock->cycle_last) &
2120 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002121 } else {
2122 /* TSC page invalid */
2123 *mode = VCLOCK_NONE;
2124 }
2125 break;
2126 case VCLOCK_TSC:
2127 *mode = VCLOCK_TSC;
2128 *tsc_timestamp = read_tsc();
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002129 v = (*tsc_timestamp - clock->cycle_last) &
2130 clock->mask;
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002131 break;
2132 default:
2133 *mode = VCLOCK_NONE;
2134 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002135
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002136 if (*mode == VCLOCK_NONE)
2137 *tsc_timestamp = v = 0;
2138
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002139 return v * clock->mult;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002140}
2141
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002142static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002143{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002144 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002145 unsigned long seq;
2146 int mode;
2147 u64 ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002148
Marcelo Tosattid8281992012-11-27 23:29:01 -02002149 do {
2150 seq = read_seqcount_begin(&gtod->seq);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002151 ns = gtod->raw_clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002152 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
Paolo Bonzini917f9472020-01-22 14:32:20 +01002153 ns >>= gtod->raw_clock.shift;
2154 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002155 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Thomas Gleixnercbcf2dd2014-07-16 21:04:54 +00002156 *t = ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002157
2158 return mode;
2159}
2160
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002161static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002162{
2163 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2164 unsigned long seq;
2165 int mode;
2166 u64 ns;
2167
2168 do {
2169 seq = read_seqcount_begin(&gtod->seq);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002170 ts->tv_sec = gtod->wall_time_sec;
Paolo Bonzini917f9472020-01-22 14:32:20 +01002171 ns = gtod->clock.base_cycles;
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002172 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002173 ns >>= gtod->clock.shift;
2174 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2175
2176 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2177 ts->tv_nsec = ns;
2178
2179 return mode;
2180}
2181
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002182/* returns true if host is using TSC based clocksource */
2183static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
Marcelo Tosattid8281992012-11-27 23:29:01 -02002184{
Marcelo Tosattid8281992012-11-27 23:29:01 -02002185 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002186 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosattid8281992012-11-27 23:29:01 -02002187 return false;
2188
Marcelo Tosatti53fafdb2019-10-28 12:36:22 -02002189 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002190 tsc_timestamp));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002191}
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002192
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002193/* returns true if host is using TSC based clocksource */
Arnd Bergmann899a31f2018-04-23 10:04:26 +02002194static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002195 u64 *tsc_timestamp)
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002196{
2197 /* checked again under seqlock below */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002198 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002199 return false;
2200
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01002201 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02002202}
Marcelo Tosattid8281992012-11-27 23:29:01 -02002203#endif
2204
2205/*
2206 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002207 * Assuming a stable TSC across physical CPUS, and a stable TSC
2208 * across virtual CPUs, the following condition is possible.
2209 * Each numbered line represents an event visible to both
Marcelo Tosattid8281992012-11-27 23:29:01 -02002210 * CPUs at the next numbered event.
2211 *
2212 * "timespecX" represents host monotonic time. "tscX" represents
2213 * RDTSC value.
2214 *
2215 * VCPU0 on CPU0 | VCPU1 on CPU1
2216 *
2217 * 1. read timespec0,tsc0
2218 * 2. | timespec1 = timespec0 + N
2219 * | tsc1 = tsc0 + M
2220 * 3. transition to guest | transition to guest
2221 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2222 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2223 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2224 *
2225 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2226 *
2227 * - ret0 < ret1
2228 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2229 * ...
2230 * - 0 < N - M => M < N
2231 *
2232 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2233 * always the case (the difference between two distinct xtime instances
2234 * might be smaller then the difference between corresponding TSC reads,
2235 * when updating guest vcpus pvclock areas).
2236 *
2237 * To avoid that problem, do not allow visibility of distinct
2238 * system_timestamp/tsc_timestamp values simultaneously: use a master
2239 * copy of host monotonic time values. Update that master copy
2240 * in lockstep.
2241 *
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002242 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
Marcelo Tosattid8281992012-11-27 23:29:01 -02002243 *
2244 */
2245
2246static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2247{
2248#ifdef CONFIG_X86_64
2249 struct kvm_arch *ka = &kvm->arch;
2250 int vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002251 bool host_tsc_clocksource, vcpus_matched;
2252
2253 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2254 atomic_read(&kvm->online_vcpus));
Marcelo Tosattid8281992012-11-27 23:29:01 -02002255
2256 /*
2257 * If the host uses TSC clock, then passthrough TSC as stable
2258 * to the guest.
2259 */
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002260 host_tsc_clocksource = kvm_get_time_and_clockread(
Marcelo Tosattid8281992012-11-27 23:29:01 -02002261 &ka->master_kernel_ns,
2262 &ka->master_cycle_now);
2263
Marcelo Tosatti16a96022014-05-14 12:43:24 -03002264 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
Ladi Proseka826faf2017-06-26 09:56:43 +02002265 && !ka->backwards_tsc_observed
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002266 && !ka->boot_vcpu_runs_old_kvmclock;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002267
Marcelo Tosattid8281992012-11-27 23:29:01 -02002268 if (ka->use_master_clock)
2269 atomic_set(&kvm_guest_has_master_clock, 1);
2270
2271 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
Marcelo Tosattib48aa972012-11-27 23:29:03 -02002272 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2273 vcpus_matched);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002274#endif
2275}
2276
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01002277void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2278{
2279 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2280}
2281
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002282static void kvm_gen_update_masterclock(struct kvm *kvm)
2283{
2284#ifdef CONFIG_X86_64
2285 int i;
2286 struct kvm_vcpu *vcpu;
2287 struct kvm_arch *ka = &kvm->arch;
2288
2289 spin_lock(&ka->pvclock_gtod_sync_lock);
2290 kvm_make_mclock_inprogress_request(kvm);
2291 /* no guest entries from this point */
2292 pvclock_update_vm_gtod_copy(kvm);
2293
2294 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002295 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002296
2297 /* guest entries allowed */
2298 kvm_for_each_vcpu(i, vcpu, kvm)
Radim Krčmář72875d82017-04-26 22:32:19 +02002299 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
Marcelo Tosatti2e762ff2013-08-27 23:55:29 -03002300
2301 spin_unlock(&ka->pvclock_gtod_sync_lock);
2302#endif
2303}
2304
Marcelo Tosattie891a322017-04-17 12:51:37 -03002305u64 get_kvmclock_ns(struct kvm *kvm)
Paolo Bonzini108b2492016-09-01 14:21:03 +02002306{
Paolo Bonzini108b2492016-09-01 14:21:03 +02002307 struct kvm_arch *ka = &kvm->arch;
Paolo Bonzini8b953442016-11-16 18:31:30 +01002308 struct pvclock_vcpu_time_info hv_clock;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002309 u64 ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002310
Paolo Bonzini8b953442016-11-16 18:31:30 +01002311 spin_lock(&ka->pvclock_gtod_sync_lock);
2312 if (!ka->use_master_clock) {
2313 spin_unlock(&ka->pvclock_gtod_sync_lock);
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002314 return get_kvmclock_base_ns() + ka->kvmclock_offset;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002315 }
2316
Paolo Bonzini8b953442016-11-16 18:31:30 +01002317 hv_clock.tsc_timestamp = ka->master_cycle_now;
2318 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2319 spin_unlock(&ka->pvclock_gtod_sync_lock);
2320
Wanpeng Lie2c22062017-05-11 18:12:05 -07002321 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2322 get_cpu();
2323
Wanpeng Lie70b57a2017-11-20 14:55:05 -08002324 if (__this_cpu_read(cpu_tsc_khz)) {
2325 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2326 &hv_clock.tsc_shift,
2327 &hv_clock.tsc_to_system_mul);
2328 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2329 } else
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002330 ret = get_kvmclock_base_ns() + ka->kvmclock_offset;
Wanpeng Lie2c22062017-05-11 18:12:05 -07002331
2332 put_cpu();
2333
2334 return ret;
Paolo Bonzini108b2492016-09-01 14:21:03 +02002335}
2336
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002337static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2338{
2339 struct kvm_vcpu_arch *vcpu = &v->arch;
2340 struct pvclock_vcpu_time_info guest_hv_clock;
2341
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002342 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002343 &guest_hv_clock, sizeof(guest_hv_clock))))
2344 return;
2345
2346 /* This VCPU is paused, but it's legal for a guest to read another
2347 * VCPU's kvmclock, so we really have to follow the specification where
2348 * it says that version is odd if data is being modified, and even after
2349 * it is consistent.
2350 *
2351 * Version field updates must be kept separate. This is because
2352 * kvm_write_guest_cached might use a "rep movs" instruction, and
2353 * writes within a string instruction are weakly ordered. So there
2354 * are three writes overall.
2355 *
2356 * As a small optimization, only write the version field in the first
2357 * and third write. The vcpu->pv_time cache is still valid, because the
2358 * version field is the first in the struct.
2359 */
2360 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2361
Liran Alon51c4b8b2017-11-05 16:11:30 +02002362 if (guest_hv_clock.version & 1)
2363 ++guest_hv_clock.version; /* first time write, random junk */
2364
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002365 vcpu->hv_clock.version = guest_hv_clock.version + 1;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002366 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2367 &vcpu->hv_clock,
2368 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002369
2370 smp_wmb();
2371
2372 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2373 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2374
2375 if (vcpu->pvclock_set_guest_stopped_request) {
2376 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2377 vcpu->pvclock_set_guest_stopped_request = false;
2378 }
2379
2380 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2381
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002382 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2383 &vcpu->hv_clock,
2384 sizeof(vcpu->hv_clock));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002385
2386 smp_wmb();
2387
2388 vcpu->hv_clock.version++;
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002389 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2390 &vcpu->hv_clock,
2391 sizeof(vcpu->hv_clock.version));
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002392}
2393
Zachary Amsden34c238a2010-09-18 14:38:14 -10002394static int kvm_guest_time_update(struct kvm_vcpu *v)
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002395{
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002396 unsigned long flags, tgt_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002397 struct kvm_vcpu_arch *vcpu = &v->arch;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002398 struct kvm_arch *ka = &v->kvm->arch;
Marcelo Tosattif25e6562014-01-06 12:18:59 -02002399 s64 kernel_ns;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002400 u64 tsc_timestamp, host_tsc;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002401 u8 pvclock_flags;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002402 bool use_master_clock;
2403
2404 kernel_ns = 0;
2405 host_tsc = 0;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +02002406
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002407 /*
Marcelo Tosattid8281992012-11-27 23:29:01 -02002408 * If the host uses TSC clock, then passthrough TSC as stable
2409 * to the guest.
2410 */
2411 spin_lock(&ka->pvclock_gtod_sync_lock);
2412 use_master_clock = ka->use_master_clock;
2413 if (use_master_clock) {
2414 host_tsc = ka->master_cycle_now;
2415 kernel_ns = ka->master_kernel_ns;
2416 }
2417 spin_unlock(&ka->pvclock_gtod_sync_lock);
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002418
2419 /* Keep irq disabled to prevent changes to the clock */
2420 local_irq_save(flags);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002421 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2422 if (unlikely(tgt_tsc_khz == 0)) {
Marcelo Tosattic09664b2013-03-18 13:54:32 -03002423 local_irq_restore(flags);
2424 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2425 return 1;
2426 }
Marcelo Tosattid8281992012-11-27 23:29:01 -02002427 if (!use_master_clock) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002428 host_tsc = rdtsc();
Paolo Bonzini8171cd62020-01-22 14:36:09 +01002429 kernel_ns = get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02002430 }
2431
Haozhong Zhang4ba76532015-10-20 15:39:07 +08002432 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
Marcelo Tosattid8281992012-11-27 23:29:01 -02002433
2434 /*
Zachary Amsdenc2855452010-09-18 14:38:15 -10002435 * We may have to catch up the TSC to match elapsed wall clock
2436 * time for two reasons, even if kvmclock is used.
2437 * 1) CPU could have been running below the maximum TSC rate
2438 * 2) Broken TSC compensation resets the base at each VCPU
2439 * entry to avoid unknown leaps of TSC even when running
2440 * again on the same CPU. This may cause apparent elapsed
2441 * time to disappear, and the guest to stand still or run
2442 * very slowly.
2443 */
2444 if (vcpu->tsc_catchup) {
2445 u64 tsc = compute_guest_tsc(v, kernel_ns);
2446 if (tsc > tsc_timestamp) {
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002447 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
Zachary Amsdenc2855452010-09-18 14:38:15 -10002448 tsc_timestamp = tsc;
2449 }
2450 }
2451
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002452 local_irq_restore(flags);
2453
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002454 /* With all the info we got, fill in the values */
Zachary Amsdenc2855452010-09-18 14:38:15 -10002455
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002456 if (kvm_has_tsc_control)
2457 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2458
2459 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
Paolo Bonzini3ae13fa2016-02-08 15:11:15 +01002460 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
Zachary Amsden5f4e3f82010-09-18 14:38:13 -10002461 &vcpu->hv_clock.tsc_shift,
2462 &vcpu->hv_clock.tsc_to_system_mul);
Paolo Bonzini78db6a52016-02-08 14:51:40 +01002463 vcpu->hw_tsc_khz = tgt_tsc_khz;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002464 }
2465
Zachary Amsden1d5f0662010-08-19 22:07:30 -10002466 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
Zachary Amsden759379d2010-08-19 22:07:25 -10002467 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
Zachary Amsden28e46392010-09-18 14:38:12 -10002468 vcpu->last_guest_tsc = tsc_timestamp;
Paolo Bonzini9446e6f2020-02-12 13:27:10 +01002469 WARN_ON((s64)vcpu->hv_clock.system_time < 0);
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03002470
Marcelo Tosattid8281992012-11-27 23:29:01 -02002471 /* If the host uses TSC clocksource, then it is stable */
Paolo Bonzini0d6dd2f2016-09-01 14:20:09 +02002472 pvclock_flags = 0;
Marcelo Tosattid8281992012-11-27 23:29:01 -02002473 if (use_master_clock)
2474 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2475
Marcelo Tosatti78c03372012-11-27 23:28:47 -02002476 vcpu->hv_clock.flags = pvclock_flags;
2477
Paolo Bonzini095cf552016-02-08 12:54:12 +01002478 if (vcpu->pv_time_enabled)
2479 kvm_setup_pvclock_page(v);
2480 if (v == kvm_get_vcpu(v->kvm, 0))
2481 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10002482 return 0;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002483}
2484
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002485/*
2486 * kvmclock updates which are isolated to a given vcpu, such as
2487 * vcpu->cpu migration, should not allow system_timestamp from
2488 * the rest of the vcpus to remain static. Otherwise ntp frequency
2489 * correction applies to one vcpu's system_timestamp but not
2490 * the others.
2491 *
2492 * So in those cases, request a kvmclock update for all vcpus.
Andrew Jones7e44e442014-02-28 12:52:54 +01002493 * We need to rate-limit these requests though, as they can
2494 * considerably slow guests that have a large number of vcpus.
2495 * The time for a remote vcpu to update its kvmclock is bound
2496 * by the delay we use to rate-limit the updates.
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002497 */
2498
Andrew Jones7e44e442014-02-28 12:52:54 +01002499#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2500
2501static void kvmclock_update_fn(struct work_struct *work)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002502{
2503 int i;
Andrew Jones7e44e442014-02-28 12:52:54 +01002504 struct delayed_work *dwork = to_delayed_work(work);
2505 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2506 kvmclock_update_work);
2507 struct kvm *kvm = container_of(ka, struct kvm, arch);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002508 struct kvm_vcpu *vcpu;
2509
2510 kvm_for_each_vcpu(i, vcpu, kvm) {
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002511 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002512 kvm_vcpu_kick(vcpu);
2513 }
2514}
2515
Andrew Jones7e44e442014-02-28 12:52:54 +01002516static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2517{
2518 struct kvm *kvm = v->kvm;
2519
Guo Hui Liu105b21b2014-09-12 13:43:19 +08002520 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
Andrew Jones7e44e442014-02-28 12:52:54 +01002521 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2522 KVMCLOCK_UPDATE_DELAY);
2523}
2524
Andrew Jones332967a2014-02-28 12:52:55 +01002525#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2526
2527static void kvmclock_sync_fn(struct work_struct *work)
2528{
2529 struct delayed_work *dwork = to_delayed_work(work);
2530 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2531 kvmclock_sync_work);
2532 struct kvm *kvm = container_of(ka, struct kvm, arch);
2533
Marcelo Tosatti630994b2015-05-12 22:42:04 -03002534 if (!kvmclock_periodic_sync)
2535 return;
2536
Andrew Jones332967a2014-02-28 12:52:55 +01002537 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2538 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2539 KVMCLOCK_SYNC_PERIOD);
2540}
2541
Borislav Petkov191c8132019-04-18 18:32:50 +02002542/*
2543 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2544 */
2545static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2546{
2547 /* McStatusWrEn enabled? */
2548 if (guest_cpuid_is_amd(vcpu))
2549 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2550
2551 return false;
2552}
2553
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002554static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Huang Ying890ca9a2009-05-11 16:48:15 +08002555{
2556 u64 mcg_cap = vcpu->arch.mcg_cap;
2557 unsigned bank_num = mcg_cap & 0xff;
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002558 u32 msr = msr_info->index;
2559 u64 data = msr_info->data;
Huang Ying890ca9a2009-05-11 16:48:15 +08002560
2561 switch (msr) {
2562 case MSR_IA32_MCG_STATUS:
2563 vcpu->arch.mcg_status = data;
2564 break;
2565 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02002566 if (!(mcg_cap & MCG_CTL_P) &&
2567 (data || !msr_info->host_initiated))
Huang Ying890ca9a2009-05-11 16:48:15 +08002568 return 1;
2569 if (data != 0 && data != ~(u64)0)
Paolo Bonzini44883f02018-07-26 13:01:52 +02002570 return 1;
Huang Ying890ca9a2009-05-11 16:48:15 +08002571 vcpu->arch.mcg_ctl = data;
2572 break;
2573 default:
2574 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08002575 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08002576 u32 offset = array_index_nospec(
2577 msr - MSR_IA32_MC0_CTL,
2578 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
2579
Andre Przywara114be422010-03-24 17:46:42 +01002580 /* only 0 or all 1s can be written to IA32_MCi_CTL
2581 * some Linux kernels though clear bit 10 in bank 4 to
2582 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2583 * this to avoid an uncatched #GP in the guest
2584 */
Huang Ying890ca9a2009-05-11 16:48:15 +08002585 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01002586 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08002587 return -1;
Borislav Petkov191c8132019-04-18 18:32:50 +02002588
2589 /* MCi_STATUS */
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002590 if (!msr_info->host_initiated &&
Borislav Petkov191c8132019-04-18 18:32:50 +02002591 (offset & 0x3) == 1 && data != 0) {
2592 if (!can_set_mci_status(vcpu))
2593 return -1;
2594 }
2595
Huang Ying890ca9a2009-05-11 16:48:15 +08002596 vcpu->arch.mce_banks[offset] = data;
2597 break;
2598 }
2599 return 1;
2600 }
2601 return 0;
2602}
2603
Ed Swierkffde22a2009-10-15 15:21:43 -07002604static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2605{
2606 struct kvm *kvm = vcpu->kvm;
2607 int lm = is_long_mode(vcpu);
2608 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2609 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2610 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2611 : kvm->arch.xen_hvm_config.blob_size_32;
2612 u32 page_num = data & ~PAGE_MASK;
2613 u64 page_addr = data & PAGE_MASK;
2614 u8 *page;
2615 int r;
2616
2617 r = -E2BIG;
2618 if (page_num >= blob_size)
2619 goto out;
2620 r = -ENOMEM;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002621 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2622 if (IS_ERR(page)) {
2623 r = PTR_ERR(page);
Ed Swierkffde22a2009-10-15 15:21:43 -07002624 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02002625 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002626 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
Ed Swierkffde22a2009-10-15 15:21:43 -07002627 goto out_free;
2628 r = 0;
2629out_free:
2630 kfree(page);
2631out:
2632 return r;
2633}
2634
Gleb Natapov344d9582010-10-14 11:22:50 +02002635static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2636{
2637 gpa_t gpa = data & ~0x3f;
2638
Wanpeng Li52a5c152017-07-13 18:30:42 -07002639 /* Bits 3:5 are reserved, Should be zero */
2640 if (data & 0x38)
Gleb Natapov344d9582010-10-14 11:22:50 +02002641 return 1;
2642
2643 vcpu->arch.apf.msr_val = data;
2644
2645 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2646 kvm_clear_async_pf_completion_queue(vcpu);
2647 kvm_async_pf_hash_reset(vcpu);
2648 return 0;
2649 }
2650
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002651 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
Andrew Honig8f964522013-03-29 09:35:21 -07002652 sizeof(u32)))
Gleb Natapov344d9582010-10-14 11:22:50 +02002653 return 1;
2654
Gleb Natapov6adba522010-10-14 11:22:55 +02002655 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
Wanpeng Li52a5c152017-07-13 18:30:42 -07002656 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
Gleb Natapov344d9582010-10-14 11:22:50 +02002657 kvm_async_pf_wakeup_all(vcpu);
2658 return 0;
2659}
2660
Glauber Costa12f9a482011-02-01 14:16:40 -05002661static void kvmclock_reset(struct kvm_vcpu *vcpu)
2662{
Andy Honig0b794592013-02-20 14:48:10 -08002663 vcpu->arch.pv_time_enabled = false;
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002664 vcpu->arch.time = 0;
Glauber Costa12f9a482011-02-01 14:16:40 -05002665}
2666
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002667static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2668{
2669 ++vcpu->stat.tlb_flush;
2670 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2671}
2672
Glauber Costac9aaa892011-07-11 15:28:14 -04002673static void record_steal_time(struct kvm_vcpu *vcpu)
2674{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002675 struct kvm_host_map map;
2676 struct kvm_steal_time *st;
2677
Glauber Costac9aaa892011-07-11 15:28:14 -04002678 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2679 return;
2680
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002681 /* -EAGAIN is returned in atomic context so we can just return. */
2682 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT,
2683 &map, &vcpu->arch.st.cache, false))
Glauber Costac9aaa892011-07-11 15:28:14 -04002684 return;
2685
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002686 st = map.hva +
2687 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
2688
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002689 /*
2690 * Doing a TLB flush here, on the guest's behalf, can avoid
2691 * expensive IPIs.
2692 */
Wanpeng Lib382f442019-08-05 10:03:24 +08002693 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002694 st->preempted & KVM_VCPU_FLUSH_TLB);
2695 if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
Wanpeng Lif38a7b72017-12-12 17:33:04 -08002696 kvm_vcpu_flush_tlb(vcpu, false);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04002697
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00002698 vcpu->arch.st.preempted = 0;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002699
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002700 if (st->version & 1)
2701 st->version += 1; /* first time write, random junk */
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002702
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002703 st->version += 1;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002704
2705 smp_wmb();
2706
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002707 st->steal += current->sched_info.run_delay -
Liang Chenc54cdf12016-03-16 19:33:16 +08002708 vcpu->arch.st.last_steal;
2709 vcpu->arch.st.last_steal = current->sched_info.run_delay;
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002710
Wanpeng Li35f3fae12016-05-03 11:43:10 +08002711 smp_wmb();
2712
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002713 st->version += 1;
Glauber Costac9aaa892011-07-11 15:28:14 -04002714
Boris Ostrovskyb0431382019-12-05 03:45:32 +00002715 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false);
Glauber Costac9aaa892011-07-11 15:28:14 -04002716}
2717
Will Auld8fe8ab42012-11-29 12:42:12 -08002718int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01002719{
Gleb Natapov57537852012-01-15 14:17:22 +02002720 bool pr = false;
Will Auld8fe8ab42012-11-29 12:42:12 -08002721 u32 msr = msr_info->index;
2722 u64 data = msr_info->data;
Gleb Natapov57537852012-01-15 14:17:22 +02002723
Carsten Otte15c4a642007-10-30 18:44:17 +01002724 switch (msr) {
Borislav Petkov2e32b712013-02-19 19:33:13 +01002725 case MSR_AMD64_NB_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002726 case MSR_IA32_UCODE_WRITE:
2727 case MSR_VM_HSAVE_PA:
2728 case MSR_AMD64_PATCH_LOADER:
2729 case MSR_AMD64_BU_CFG2:
Ladi Prosek405a3532017-04-06 15:22:20 +02002730 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02002731 case MSR_F15H_EX_CFG:
Borislav Petkov2e32b712013-02-19 19:33:13 +01002732 break;
2733
Wanpeng Li518e7b92018-02-28 14:03:31 +08002734 case MSR_IA32_UCODE_REV:
2735 if (msr_info->host_initiated)
2736 vcpu->arch.microcode_version = data;
2737 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08002738 case MSR_IA32_ARCH_CAPABILITIES:
2739 if (!msr_info->host_initiated)
2740 return 1;
2741 vcpu->arch.arch_capabilities = data;
2742 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002743 case MSR_EFER:
Sean Christopherson11988492019-04-02 08:19:15 -07002744 return set_efer(vcpu, msr_info);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002745 case MSR_K7_HWCR:
2746 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01002747 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Nicolae Mogoreanua223c312012-02-21 13:44:21 -08002748 data &= ~(u64)0x8; /* ignore TLB cache disable */
Borislav Petkov191c8132019-04-18 18:32:50 +02002749
2750 /* Handle McStatusWrEn */
2751 if (data == BIT_ULL(18)) {
2752 vcpu->arch.msr_hwcr = data;
2753 } else if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002754 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2755 data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02002756 return 1;
2757 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002758 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002759 case MSR_FAM10H_MMIO_CONF_BASE:
2760 if (data != 0) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002761 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2762 "0x%llx\n", data);
Andre Przywaraf7c6d142009-07-02 15:04:14 +02002763 return 1;
2764 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002765 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002766 case MSR_IA32_DEBUGCTLMSR:
2767 if (!data) {
2768 /* We support the non-activated case already */
2769 break;
2770 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2771 /* Values other than LBR and BTF are vendor-specific,
2772 thus reserved and should throw a #GP */
2773 return 1;
2774 }
Christoffer Dalla737f252012-06-03 21:17:48 +03002775 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2776 __func__, data);
Alexander Grafb5e2fec2008-07-22 08:00:45 +02002777 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03002778 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08002779 return kvm_mtrr_set_msr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01002780 case MSR_IA32_APICBASE:
Jan Kiszka58cb6282014-01-24 16:48:44 +01002781 return kvm_set_apic_base(vcpu, msr_info);
Gleb Natapov0105d1a2009-07-05 17:39:36 +03002782 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2783 return kvm_x2apic_msr_write(vcpu, msr, data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08002784 case MSR_IA32_TSCDEADLINE:
2785 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2786 break;
Will Auldba904632012-11-29 12:42:50 -08002787 case MSR_IA32_TSC_ADJUST:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002788 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
Will Auldba904632012-11-29 12:42:50 -08002789 if (!msr_info->host_initiated) {
Chris J Argesd913b902014-11-12 21:00:39 -06002790 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
Haozhong Zhangd7add052015-08-07 11:24:32 +08002791 adjust_tsc_offset_guest(vcpu, adj);
Will Auldba904632012-11-29 12:42:50 -08002792 }
2793 vcpu->arch.ia32_tsc_adjust_msr = data;
2794 }
2795 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002796 case MSR_IA32_MISC_ENABLE:
Wanpeng Li511a85562019-05-21 14:06:54 +08002797 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2798 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2799 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2800 return 1;
2801 vcpu->arch.ia32_misc_enable_msr = data;
2802 kvm_update_cpuid(vcpu);
2803 } else {
2804 vcpu->arch.ia32_misc_enable_msr = data;
2805 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002806 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002807 case MSR_IA32_SMBASE:
2808 if (!msr_info->host_initiated)
2809 return 1;
2810 vcpu->arch.smbase = data;
2811 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02002812 case MSR_IA32_POWER_CTL:
2813 vcpu->arch.msr_ia32_power_ctl = data;
2814 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02002815 case MSR_IA32_TSC:
2816 kvm_write_tsc(vcpu, msr_info);
2817 break;
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002818 case MSR_IA32_XSS:
2819 if (!msr_info->host_initiated &&
2820 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2821 return 1;
2822 /*
Sean Christophersona1bead22020-03-02 15:57:00 -08002823 * KVM supports exposing PT to the guest, but does not support
2824 * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than
2825 * XSAVES/XRSTORS to save/restore PT MSRs.
Aaron Lewis864e2ab2019-10-21 16:30:26 -07002826 */
2827 if (data != 0)
2828 return 1;
2829 vcpu->arch.ia32_xss = data;
2830 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002831 case MSR_SMI_COUNT:
2832 if (!msr_info->host_initiated)
2833 return 1;
2834 vcpu->arch.smi_count = data;
2835 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002836 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002837 case MSR_KVM_WALL_CLOCK:
2838 vcpu->kvm->arch.wall_clock = data;
2839 kvm_write_wall_clock(vcpu->kvm, data);
2840 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002841 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002842 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002843 struct kvm_arch *ka = &vcpu->kvm->arch;
2844
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002845 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2846 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2847
2848 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002849 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002850
2851 ka->boot_vcpu_runs_old_kvmclock = tmp;
2852 }
2853
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002854 vcpu->arch.time = data;
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03002855 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002856
2857 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002858 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002859 if (!(data & 1))
2860 break;
2861
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002862 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002863 &vcpu->arch.pv_time, data & ~1ULL,
2864 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002865 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002866
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002867 break;
2868 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002869 case MSR_KVM_ASYNC_PF_EN:
2870 if (kvm_pv_enable_async_pf(vcpu, data))
2871 return 1;
2872 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002873 case MSR_KVM_STEAL_TIME:
2874
2875 if (unlikely(!sched_info_on()))
2876 return 1;
2877
2878 if (data & KVM_STEAL_RESERVED_MASK)
2879 return 1;
2880
Glauber Costac9aaa892011-07-11 15:28:14 -04002881 vcpu->arch.st.msr_val = data;
2882
2883 if (!(data & KVM_MSR_ENABLED))
2884 break;
2885
Glauber Costac9aaa892011-07-11 15:28:14 -04002886 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2887
2888 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002889 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002890 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002891 return 1;
2892 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002893
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002894 case MSR_KVM_POLL_CONTROL:
2895 /* only enable bit supported */
2896 if (data & (-1ULL << 1))
2897 return 1;
2898
2899 vcpu->arch.msr_kvm_poll_control = data;
2900 break;
2901
Huang Ying890ca9a2009-05-11 16:48:15 +08002902 case MSR_IA32_MCG_CTL:
2903 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002904 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002905 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002906
Wei Huang6912ac32015-06-12 01:34:56 -04002907 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2908 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2909 pr = true; /* fall through */
2910 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2911 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002912 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002913 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002914
2915 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002916 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2917 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002918 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002919 case MSR_K7_CLK_CTL:
2920 /*
2921 * Ignore all writes to this no longer documented MSR.
2922 * Writes are only relevant for old K7 processors,
2923 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002924 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002925 * affected processor models on the command line, hence
2926 * the need to ignore the workaround.
2927 */
2928 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002929 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002930 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2931 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002932 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002933 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2934 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2935 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002936 return kvm_hv_set_msr_common(vcpu, msr, data,
2937 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002938 case MSR_IA32_BBL_CR_CTL3:
2939 /* Drop writes to this legacy MSR -- see rdmsr
2940 * counterpart for further detail.
2941 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002942 if (report_ignored_msrs)
2943 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2944 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002945 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002946 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002947 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002948 return 1;
2949 vcpu->arch.osvw.length = data;
2950 break;
2951 case MSR_AMD64_OSVW_STATUS:
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.status = data;
2955 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002956 case MSR_PLATFORM_INFO:
2957 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002958 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2959 cpuid_fault_enabled(vcpu)))
2960 return 1;
2961 vcpu->arch.msr_platform_info = data;
2962 break;
2963 case MSR_MISC_FEATURES_ENABLES:
2964 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2965 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2966 !supports_cpuid_fault(vcpu)))
2967 return 1;
2968 vcpu->arch.msr_misc_features_enables = data;
2969 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002970 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002971 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2972 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002973 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002974 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002975 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002976 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002977 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002978 return 1;
2979 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002980 if (report_ignored_msrs)
2981 vcpu_unimpl(vcpu,
2982 "ignored wrmsr: 0x%x data 0x%llx\n",
2983 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002984 break;
2985 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002986 }
2987 return 0;
2988}
2989EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2990
Paolo Bonzini44883f02018-07-26 13:01:52 +02002991static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002992{
2993 u64 data;
2994 u64 mcg_cap = vcpu->arch.mcg_cap;
2995 unsigned bank_num = mcg_cap & 0xff;
2996
2997 switch (msr) {
2998 case MSR_IA32_P5_MC_ADDR:
2999 case MSR_IA32_P5_MC_TYPE:
3000 data = 0;
3001 break;
3002 case MSR_IA32_MCG_CAP:
3003 data = vcpu->arch.mcg_cap;
3004 break;
3005 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003006 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003007 return 1;
3008 data = vcpu->arch.mcg_ctl;
3009 break;
3010 case MSR_IA32_MCG_STATUS:
3011 data = vcpu->arch.mcg_status;
3012 break;
3013 default:
3014 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003015 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003016 u32 offset = array_index_nospec(
3017 msr - MSR_IA32_MC0_CTL,
3018 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3019
Huang Ying890ca9a2009-05-11 16:48:15 +08003020 data = vcpu->arch.mce_banks[offset];
3021 break;
3022 }
3023 return 1;
3024 }
3025 *pdata = data;
3026 return 0;
3027}
3028
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003029int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003030{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003031 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003032 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003033 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003034 case MSR_IA32_DEBUGCTLMSR:
3035 case MSR_IA32_LASTBRANCHFROMIP:
3036 case MSR_IA32_LASTBRANCHTOIP:
3037 case MSR_IA32_LASTINTFROMIP:
3038 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303039 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003040 case MSR_K8_TSEG_ADDR:
3041 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003042 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003043 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003044 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003045 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003046 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003047 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003048 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003049 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003050 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003051 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003052 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003053 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3054 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3055 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3056 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003057 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003058 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3059 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003060 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003061 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003062 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003063 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003064 case MSR_IA32_ARCH_CAPABILITIES:
3065 if (!msr_info->host_initiated &&
3066 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3067 return 1;
3068 msr_info->data = vcpu->arch.arch_capabilities;
3069 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003070 case MSR_IA32_POWER_CTL:
3071 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3072 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003073 case MSR_IA32_TSC:
3074 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3075 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003076 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003077 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003078 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003079 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003080 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003081 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003082 /*
3083 * MSR_EBC_FREQUENCY_ID
3084 * Conservative value valid for even the basic CPU models.
3085 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3086 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3087 * and 266MHz for model 3, or 4. Set Core Clock
3088 * Frequency to System Bus Frequency Ratio to 1 (bits
3089 * 31:24) even though these are only valid for CPU
3090 * models > 2, however guests may end up dividing or
3091 * multiplying by zero otherwise.
3092 */
3093 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003094 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003095 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003096 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003097 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003098 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003099 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003100 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003101 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003102 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003103 break;
Will Auldba904632012-11-29 12:42:50 -08003104 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003105 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003106 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003107 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003108 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003109 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003110 case MSR_IA32_SMBASE:
3111 if (!msr_info->host_initiated)
3112 return 1;
3113 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003114 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003115 case MSR_SMI_COUNT:
3116 msr_info->data = vcpu->arch.smi_count;
3117 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003118 case MSR_IA32_PERF_STATUS:
3119 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003120 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003121 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003122 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003123 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003124 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003125 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003126 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003127 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003128 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003129 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003130 break;
3131 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003132 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003133 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003134 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003135 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003136 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003137 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003138 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003139 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003140 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003141 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003142 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003143 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003144 case MSR_KVM_POLL_CONTROL:
3145 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3146 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003147 case MSR_IA32_P5_MC_ADDR:
3148 case MSR_IA32_P5_MC_TYPE:
3149 case MSR_IA32_MCG_CAP:
3150 case MSR_IA32_MCG_CTL:
3151 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003152 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003153 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3154 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003155 case MSR_IA32_XSS:
3156 if (!msr_info->host_initiated &&
3157 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3158 return 1;
3159 msr_info->data = vcpu->arch.ia32_xss;
3160 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003161 case MSR_K7_CLK_CTL:
3162 /*
3163 * Provide expected ramp-up count for K7. All other
3164 * are set to zero, indicating minimum divisors for
3165 * every field.
3166 *
3167 * This prevents guest kernels on AMD host with CPU
3168 * type 6, model 8 and higher from exploding due to
3169 * the rdmsr failing.
3170 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003171 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003172 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003173 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003174 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3175 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003176 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003177 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3178 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3179 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003180 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003181 msr_info->index, &msr_info->data,
3182 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003183 case MSR_IA32_BBL_CR_CTL3:
3184 /* This legacy MSR exists but isn't fully documented in current
3185 * silicon. It is however accessed by winxp in very narrow
3186 * scenarios where it sets bit #19, itself documented as
3187 * a "reserved" bit. Best effort attempt to source coherent
3188 * read data here should the balance of the register be
3189 * interpreted by the guest:
3190 *
3191 * L2 cache control register 3: 64GB range, 256KB size,
3192 * enabled, latency 0x1, configured
3193 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003194 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003195 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003196 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003197 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003198 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003199 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003200 break;
3201 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003202 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003203 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003204 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003205 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003206 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003207 if (!msr_info->host_initiated &&
3208 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3209 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003210 msr_info->data = vcpu->arch.msr_platform_info;
3211 break;
3212 case MSR_MISC_FEATURES_ENABLES:
3213 msr_info->data = vcpu->arch.msr_misc_features_enables;
3214 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003215 case MSR_K7_HWCR:
3216 msr_info->data = vcpu->arch.msr_hwcr;
3217 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003218 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003219 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003220 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003221 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003222 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3223 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003224 return 1;
3225 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003226 if (report_ignored_msrs)
3227 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3228 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003229 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003230 }
3231 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003232 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003233 return 0;
3234}
3235EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3236
Carsten Otte313a3dc2007-10-11 19:16:52 +02003237/*
3238 * Read or write a bunch of msrs. All parameters are kernel addresses.
3239 *
3240 * @return number of msrs set successfully.
3241 */
3242static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3243 struct kvm_msr_entry *entries,
3244 int (*do_msr)(struct kvm_vcpu *vcpu,
3245 unsigned index, u64 *data))
3246{
Tom Lendacky801e4592018-02-21 13:39:51 -06003247 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003248
Carsten Otte313a3dc2007-10-11 19:16:52 +02003249 for (i = 0; i < msrs->nmsrs; ++i)
3250 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3251 break;
3252
Carsten Otte313a3dc2007-10-11 19:16:52 +02003253 return i;
3254}
3255
3256/*
3257 * Read or write a bunch of msrs. Parameters are user addresses.
3258 *
3259 * @return number of msrs set successfully.
3260 */
3261static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3262 int (*do_msr)(struct kvm_vcpu *vcpu,
3263 unsigned index, u64 *data),
3264 int writeback)
3265{
3266 struct kvm_msrs msrs;
3267 struct kvm_msr_entry *entries;
3268 int r, n;
3269 unsigned size;
3270
3271 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003272 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003273 goto out;
3274
3275 r = -E2BIG;
3276 if (msrs.nmsrs >= MAX_IO_MSRS)
3277 goto out;
3278
Carsten Otte313a3dc2007-10-11 19:16:52 +02003279 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003280 entries = memdup_user(user_msrs->entries, size);
3281 if (IS_ERR(entries)) {
3282 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003283 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003284 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003285
3286 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3287 if (r < 0)
3288 goto out_free;
3289
3290 r = -EFAULT;
3291 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3292 goto out_free;
3293
3294 r = n;
3295
3296out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003297 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003298out:
3299 return r;
3300}
3301
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003302static inline bool kvm_can_mwait_in_guest(void)
3303{
3304 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003305 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3306 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003307}
3308
Alexander Graf784aa3d2014-07-14 18:27:35 +02003309int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003310{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003311 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003312
3313 switch (ext) {
3314 case KVM_CAP_IRQCHIP:
3315 case KVM_CAP_HLT:
3316 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003317 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003318 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003319 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003320 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003321 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003322 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003323 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003324 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003325 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003326 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003327 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003328 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003329 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003330 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003331 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003332 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003333 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003334 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003335 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003336 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003337 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003338 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003339 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003340 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003341 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003342 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003343 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003344 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003345 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003346 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003347 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003348 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003349 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003350 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003351 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003352 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003353 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003354 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003355 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003356 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003357 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003358 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003359 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003360 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003361 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003362 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003363 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003364 r = 1;
3365 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003366 case KVM_CAP_SYNC_REGS:
3367 r = KVM_SYNC_X86_VALID_FIELDS;
3368 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003369 case KVM_CAP_ADJUST_CLOCK:
3370 r = KVM_CLOCK_TSC_STABLE;
3371 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003372 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003373 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3374 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003375 if(kvm_can_mwait_in_guest())
3376 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003377 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003378 case KVM_CAP_X86_SMM:
3379 /* SMBASE is usually relocated above 1M on modern chipsets,
3380 * and SMM handlers might indeed rely on 4G segment limits,
3381 * so do not report SMM to be available if real mode is
3382 * emulated via vm86 mode. Still, do not go to great lengths
3383 * to avoid userspace's usage of the feature, because it is a
3384 * fringe case that is not enabled except via specific settings
3385 * of the module parameters.
3386 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003387 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003388 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003389 case KVM_CAP_VAPIC:
3390 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3391 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003392 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003393 r = KVM_SOFT_MAX_VCPUS;
3394 break;
3395 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003396 r = KVM_MAX_VCPUS;
3397 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003398 case KVM_CAP_MAX_VCPU_ID:
3399 r = KVM_MAX_VCPU_ID;
3400 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003401 case KVM_CAP_PV_MMU: /* obsolete */
3402 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003403 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003404 case KVM_CAP_MCE:
3405 r = KVM_MAX_MCE_BANKS;
3406 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003407 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003408 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003409 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003410 case KVM_CAP_TSC_CONTROL:
3411 r = kvm_has_tsc_control;
3412 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003413 case KVM_CAP_X2APIC_API:
3414 r = KVM_X2APIC_API_VALID_FLAGS;
3415 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003416 case KVM_CAP_NESTED_STATE:
3417 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303418 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003419 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003420 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003421 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3422 break;
3423 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3424 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003425 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003426 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003427 break;
3428 }
3429 return r;
3430
3431}
3432
Carsten Otte043405e2007-10-10 17:16:19 +02003433long kvm_arch_dev_ioctl(struct file *filp,
3434 unsigned int ioctl, unsigned long arg)
3435{
3436 void __user *argp = (void __user *)arg;
3437 long r;
3438
3439 switch (ioctl) {
3440 case KVM_GET_MSR_INDEX_LIST: {
3441 struct kvm_msr_list __user *user_msr_list = argp;
3442 struct kvm_msr_list msr_list;
3443 unsigned n;
3444
3445 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003446 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003447 goto out;
3448 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003449 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003450 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003451 goto out;
3452 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003453 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003454 goto out;
3455 r = -EFAULT;
3456 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3457 num_msrs_to_save * sizeof(u32)))
3458 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003459 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003460 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003461 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003462 goto out;
3463 r = 0;
3464 break;
3465 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003466 case KVM_GET_SUPPORTED_CPUID:
3467 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003468 struct kvm_cpuid2 __user *cpuid_arg = argp;
3469 struct kvm_cpuid2 cpuid;
3470
3471 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003472 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003473 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003474
3475 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3476 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003477 if (r)
3478 goto out;
3479
3480 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003481 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003482 goto out;
3483 r = 0;
3484 break;
3485 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003486 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003487 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003488 if (copy_to_user(argp, &kvm_mce_cap_supported,
3489 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003490 goto out;
3491 r = 0;
3492 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003493 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3494 struct kvm_msr_list __user *user_msr_list = argp;
3495 struct kvm_msr_list msr_list;
3496 unsigned int n;
3497
3498 r = -EFAULT;
3499 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3500 goto out;
3501 n = msr_list.nmsrs;
3502 msr_list.nmsrs = num_msr_based_features;
3503 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3504 goto out;
3505 r = -E2BIG;
3506 if (n < msr_list.nmsrs)
3507 goto out;
3508 r = -EFAULT;
3509 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3510 num_msr_based_features * sizeof(u32)))
3511 goto out;
3512 r = 0;
3513 break;
3514 }
3515 case KVM_GET_MSRS:
3516 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3517 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003518 }
Carsten Otte043405e2007-10-10 17:16:19 +02003519 default:
3520 r = -EINVAL;
3521 }
3522out:
3523 return r;
3524}
3525
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003526static void wbinvd_ipi(void *garbage)
3527{
3528 wbinvd();
3529}
3530
3531static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3532{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003533 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003534}
3535
Carsten Otte313a3dc2007-10-11 19:16:52 +02003536void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3537{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003538 /* Address WBINVD may be executed by guest */
3539 if (need_emulate_wbinvd(vcpu)) {
3540 if (kvm_x86_ops->has_wbinvd_exit())
3541 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3542 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3543 smp_call_function_single(vcpu->cpu,
3544 wbinvd_ipi, NULL, 1);
3545 }
3546
Carsten Otte313a3dc2007-10-11 19:16:52 +02003547 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003548
3549 /* Apply any externally detected TSC adjustments (due to suspend) */
3550 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3551 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3552 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003553 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003554 }
3555
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003556 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003557 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003558 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003559 if (tsc_delta < 0)
3560 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003561
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003562 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003563 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003564 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003565 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003566 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003567 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003568
3569 if (kvm_lapic_hv_timer_in_use(vcpu))
3570 kvm_lapic_restart_hv_timer(vcpu);
3571
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003572 /*
3573 * On a host with synchronized TSC, there is no need to update
3574 * kvmclock on vcpu->cpu migration
3575 */
3576 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03003577 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003578 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003579 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003580 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003581 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003582
Glauber Costac9aaa892011-07-11 15:28:14 -04003583 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003584}
3585
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003586static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3587{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003588 struct kvm_host_map map;
3589 struct kvm_steal_time *st;
3590
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003591 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3592 return;
3593
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003594 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003595 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003596
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003597 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3598 &vcpu->arch.st.cache, true))
3599 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003600
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003601 st = map.hva +
3602 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3603
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003604 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003605
3606 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003607}
3608
Carsten Otte313a3dc2007-10-11 19:16:52 +02003609void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3610{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003611 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003612
3613 if (vcpu->preempted)
3614 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3615
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003616 /*
3617 * Disable page faults because we're in atomic context here.
3618 * kvm_write_guest_offset_cached() would call might_fault()
3619 * that relies on pagefault_disable() to tell if there's a
3620 * bug. NOTE: the write to guest memory may not go through if
3621 * during postcopy live migration or if there's heavy guest
3622 * paging.
3623 */
3624 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003625 /*
3626 * kvm_memslots() will be called by
3627 * kvm_write_guest_offset_cached() so take the srcu lock.
3628 */
3629 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003630 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003631 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003632 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003633 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003634 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003635 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003636 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3637 * on every vmexit, but if not, we might have a stale dr6 from the
3638 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003639 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003640 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003641}
3642
Carsten Otte313a3dc2007-10-11 19:16:52 +02003643static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3644 struct kvm_lapic_state *s)
3645{
Liran Alonfa59cc02017-12-24 18:12:53 +02003646 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003647 kvm_x86_ops->sync_pir_to_irr(vcpu);
3648
Radim Krčmářa92e2542016-07-12 22:09:22 +02003649 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003650}
3651
3652static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3653 struct kvm_lapic_state *s)
3654{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003655 int r;
3656
3657 r = kvm_apic_set_state(vcpu, s);
3658 if (r)
3659 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003660 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003661
3662 return 0;
3663}
3664
Matt Gingell127a4572015-11-17 17:32:05 +01003665static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3666{
3667 return (!lapic_in_kernel(vcpu) ||
3668 kvm_apic_accept_pic_intr(vcpu));
3669}
3670
Matt Gingell782d4222015-11-16 15:26:00 -08003671/*
3672 * if userspace requested an interrupt window, check that the
3673 * interrupt window is open.
3674 *
3675 * No need to exit to userspace if we already have an interrupt queued.
3676 */
3677static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3678{
3679 return kvm_arch_interrupt_allowed(vcpu) &&
3680 !kvm_cpu_has_interrupt(vcpu) &&
3681 !kvm_event_needs_reinjection(vcpu) &&
3682 kvm_cpu_accept_dm_intr(vcpu);
3683}
3684
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003685static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3686 struct kvm_interrupt *irq)
3687{
Chen Gang02cdb502013-02-27 11:33:25 +08003688 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003689 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003690
3691 if (!irqchip_in_kernel(vcpu->kvm)) {
3692 kvm_queue_interrupt(vcpu, irq->irq, false);
3693 kvm_make_request(KVM_REQ_EVENT, vcpu);
3694 return 0;
3695 }
3696
3697 /*
3698 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3699 * fail for in-kernel 8259.
3700 */
3701 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003702 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003703
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003704 if (vcpu->arch.pending_external_vector != -1)
3705 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003706
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003707 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003708 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003709 return 0;
3710}
3711
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003712static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3713{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003714 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003715
3716 return 0;
3717}
3718
Paolo Bonzinif0778252015-04-01 15:06:40 +02003719static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3720{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003721 kvm_make_request(KVM_REQ_SMI, vcpu);
3722
Paolo Bonzinif0778252015-04-01 15:06:40 +02003723 return 0;
3724}
3725
Avi Kivityb209749f2007-10-22 16:50:39 +02003726static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3727 struct kvm_tpr_access_ctl *tac)
3728{
3729 if (tac->flags)
3730 return -EINVAL;
3731 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3732 return 0;
3733}
3734
Huang Ying890ca9a2009-05-11 16:48:15 +08003735static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3736 u64 mcg_cap)
3737{
3738 int r;
3739 unsigned bank_num = mcg_cap & 0xff, bank;
3740
3741 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003742 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003743 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003744 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003745 goto out;
3746 r = 0;
3747 vcpu->arch.mcg_cap = mcg_cap;
3748 /* Init IA32_MCG_CTL to all 1s */
3749 if (mcg_cap & MCG_CTL_P)
3750 vcpu->arch.mcg_ctl = ~(u64)0;
3751 /* Init IA32_MCi_CTL to all 1s */
3752 for (bank = 0; bank < bank_num; bank++)
3753 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003754
Sean Christopherson92735b12019-08-02 15:06:17 -07003755 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003756out:
3757 return r;
3758}
3759
3760static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3761 struct kvm_x86_mce *mce)
3762{
3763 u64 mcg_cap = vcpu->arch.mcg_cap;
3764 unsigned bank_num = mcg_cap & 0xff;
3765 u64 *banks = vcpu->arch.mce_banks;
3766
3767 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3768 return -EINVAL;
3769 /*
3770 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3771 * reporting is disabled
3772 */
3773 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3774 vcpu->arch.mcg_ctl != ~(u64)0)
3775 return 0;
3776 banks += 4 * mce->bank;
3777 /*
3778 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3779 * reporting is disabled for the bank
3780 */
3781 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3782 return 0;
3783 if (mce->status & MCI_STATUS_UC) {
3784 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003785 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003786 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003787 return 0;
3788 }
3789 if (banks[1] & MCI_STATUS_VAL)
3790 mce->status |= MCI_STATUS_OVER;
3791 banks[2] = mce->addr;
3792 banks[3] = mce->misc;
3793 vcpu->arch.mcg_status = mce->mcg_status;
3794 banks[1] = mce->status;
3795 kvm_queue_exception(vcpu, MC_VECTOR);
3796 } else if (!(banks[1] & MCI_STATUS_VAL)
3797 || !(banks[1] & MCI_STATUS_UC)) {
3798 if (banks[1] & MCI_STATUS_VAL)
3799 mce->status |= MCI_STATUS_OVER;
3800 banks[2] = mce->addr;
3801 banks[3] = mce->misc;
3802 banks[1] = mce->status;
3803 } else
3804 banks[1] |= MCI_STATUS_OVER;
3805 return 0;
3806}
3807
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003808static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3809 struct kvm_vcpu_events *events)
3810{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003811 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003812
Wanpeng Li664f8e22017-08-24 03:35:09 -07003813 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003814 * In guest mode, payload delivery should be deferred,
3815 * so that the L1 hypervisor can intercept #PF before
3816 * CR2 is modified (or intercept #DB before DR6 is
3817 * modified under nVMX). Unless the per-VM capability,
3818 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3819 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3820 * opportunistically defer the exception payload, deliver it if the
3821 * capability hasn't been requested before processing a
3822 * KVM_GET_VCPU_EVENTS.
3823 */
3824 if (!vcpu->kvm->arch.exception_payload_enabled &&
3825 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3826 kvm_deliver_exception_payload(vcpu);
3827
3828 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003829 * The API doesn't provide the instruction length for software
3830 * exceptions, so don't report them. As long as the guest RIP
3831 * isn't advanced, we should expect to encounter the exception
3832 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003833 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003834 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3835 events->exception.injected = 0;
3836 events->exception.pending = 0;
3837 } else {
3838 events->exception.injected = vcpu->arch.exception.injected;
3839 events->exception.pending = vcpu->arch.exception.pending;
3840 /*
3841 * For ABI compatibility, deliberately conflate
3842 * pending and injected exceptions when
3843 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3844 */
3845 if (!vcpu->kvm->arch.exception_payload_enabled)
3846 events->exception.injected |=
3847 vcpu->arch.exception.pending;
3848 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003849 events->exception.nr = vcpu->arch.exception.nr;
3850 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3851 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003852 events->exception_has_payload = vcpu->arch.exception.has_payload;
3853 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003854
Jan Kiszka03b82a32010-02-15 10:45:41 +01003855 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003856 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003857 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003858 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003859 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003860
3861 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003862 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003863 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003864 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003865
Jan Kiszka66450a22013-03-13 12:42:34 +01003866 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003867
Paolo Bonzinif0778252015-04-01 15:06:40 +02003868 events->smi.smm = is_smm(vcpu);
3869 events->smi.pending = vcpu->arch.smi_pending;
3870 events->smi.smm_inside_nmi =
3871 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3872 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3873
Jan Kiszkadab4b912009-12-06 18:24:15 +01003874 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003875 | KVM_VCPUEVENT_VALID_SHADOW
3876 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003877 if (vcpu->kvm->arch.exception_payload_enabled)
3878 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3879
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003880 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003881}
3882
Sean Christophersonc5833c72019-04-02 08:03:10 -07003883static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003884
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003885static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3886 struct kvm_vcpu_events *events)
3887{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003888 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003889 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003890 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003891 | KVM_VCPUEVENT_VALID_SMM
3892 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003893 return -EINVAL;
3894
Jim Mattson59073aa2018-10-16 14:29:20 -07003895 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3896 if (!vcpu->kvm->arch.exception_payload_enabled)
3897 return -EINVAL;
3898 if (events->exception.pending)
3899 events->exception.injected = 0;
3900 else
3901 events->exception_has_payload = 0;
3902 } else {
3903 events->exception.pending = 0;
3904 events->exception_has_payload = 0;
3905 }
3906
3907 if ((events->exception.injected || events->exception.pending) &&
3908 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003909 return -EINVAL;
3910
David Hildenbrand28bf2882017-03-23 11:46:03 +01003911 /* INITs are latched while in SMM */
3912 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3913 (events->smi.smm || events->smi.pending) &&
3914 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3915 return -EINVAL;
3916
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003917 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003918 vcpu->arch.exception.injected = events->exception.injected;
3919 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003920 vcpu->arch.exception.nr = events->exception.nr;
3921 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3922 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003923 vcpu->arch.exception.has_payload = events->exception_has_payload;
3924 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003925
Liran Alon04140b42018-03-23 03:01:31 +03003926 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003927 vcpu->arch.interrupt.nr = events->interrupt.nr;
3928 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003929 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3930 kvm_x86_ops->set_interrupt_shadow(vcpu,
3931 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003932
3933 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003934 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3935 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003936 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3937
Jan Kiszka66450a22013-03-13 12:42:34 +01003938 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003939 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003940 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003941
Paolo Bonzinif0778252015-04-01 15:06:40 +02003942 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003943 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3944 if (events->smi.smm)
3945 vcpu->arch.hflags |= HF_SMM_MASK;
3946 else
3947 vcpu->arch.hflags &= ~HF_SMM_MASK;
3948 kvm_smm_changed(vcpu);
3949 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003950
Paolo Bonzinif0778252015-04-01 15:06:40 +02003951 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003952
3953 if (events->smi.smm) {
3954 if (events->smi.smm_inside_nmi)
3955 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003956 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003957 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003958 }
3959
3960 if (lapic_in_kernel(vcpu)) {
3961 if (events->smi.latched_init)
3962 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3963 else
3964 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003965 }
3966 }
3967
Avi Kivity3842d132010-07-27 12:30:24 +03003968 kvm_make_request(KVM_REQ_EVENT, vcpu);
3969
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003970 return 0;
3971}
3972
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003973static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3974 struct kvm_debugregs *dbgregs)
3975{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003976 unsigned long val;
3977
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003978 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003979 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003980 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003981 dbgregs->dr7 = vcpu->arch.dr7;
3982 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003983 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003984}
3985
3986static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3987 struct kvm_debugregs *dbgregs)
3988{
3989 if (dbgregs->flags)
3990 return -EINVAL;
3991
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003992 if (dbgregs->dr6 & ~0xffffffffull)
3993 return -EINVAL;
3994 if (dbgregs->dr7 & ~0xffffffffull)
3995 return -EINVAL;
3996
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003997 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003998 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003999 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004000 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004001 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004002 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004003
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004004 return 0;
4005}
4006
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004007#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4008
4009static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4010{
Marc Orrb666a4b2018-11-06 14:53:56 -08004011 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004012 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004013 u64 valid;
4014
4015 /*
4016 * Copy legacy XSAVE area, to avoid complications with CPUID
4017 * leaves 0 and 1 in the loop below.
4018 */
4019 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4020
4021 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004022 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004023 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4024
4025 /*
4026 * Copy each region from the possibly compacted offset to the
4027 * non-compacted offset.
4028 */
Dave Hansend91cab72015-09-02 16:31:26 -07004029 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004030 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004031 u64 xfeature_mask = valid & -valid;
4032 int xfeature_nr = fls64(xfeature_mask) - 1;
4033 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004034
4035 if (src) {
4036 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004037 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004038 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004039 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004040 memcpy(dest + offset, &vcpu->arch.pkru,
4041 sizeof(vcpu->arch.pkru));
4042 else
4043 memcpy(dest + offset, src, size);
4044
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004045 }
4046
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004047 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004048 }
4049}
4050
4051static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4052{
Marc Orrb666a4b2018-11-06 14:53:56 -08004053 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004054 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4055 u64 valid;
4056
4057 /*
4058 * Copy legacy XSAVE area, to avoid complications with CPUID
4059 * leaves 0 and 1 in the loop below.
4060 */
4061 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4062
4063 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004064 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004065 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004066 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004067
4068 /*
4069 * Copy each region from the non-compacted offset to the
4070 * possibly compacted offset.
4071 */
Dave Hansend91cab72015-09-02 16:31:26 -07004072 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004073 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004074 u64 xfeature_mask = valid & -valid;
4075 int xfeature_nr = fls64(xfeature_mask) - 1;
4076 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004077
4078 if (dest) {
4079 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004080 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004081 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004082 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004083 memcpy(&vcpu->arch.pkru, src + offset,
4084 sizeof(vcpu->arch.pkru));
4085 else
4086 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004087 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004088
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004089 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004090 }
4091}
4092
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004093static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4094 struct kvm_xsave *guest_xsave)
4095{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004096 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004097 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4098 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004099 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004100 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004101 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004102 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004103 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004104 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004105 }
4106}
4107
Wanpeng Lia5758132017-05-11 02:58:55 -07004108#define XSAVE_MXCSR_OFFSET 24
4109
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004110static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4111 struct kvm_xsave *guest_xsave)
4112{
4113 u64 xstate_bv =
4114 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004115 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004116
Borislav Petkovd366bf72016-04-04 22:25:02 +02004117 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004118 /*
4119 * Here we allow setting states that are not present in
4120 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4121 * with old userspace.
4122 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004123 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004124 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004125 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004126 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004127 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4128 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004129 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004130 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004131 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004132 }
4133 return 0;
4134}
4135
4136static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4137 struct kvm_xcrs *guest_xcrs)
4138{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004139 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004140 guest_xcrs->nr_xcrs = 0;
4141 return;
4142 }
4143
4144 guest_xcrs->nr_xcrs = 1;
4145 guest_xcrs->flags = 0;
4146 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4147 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4148}
4149
4150static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4151 struct kvm_xcrs *guest_xcrs)
4152{
4153 int i, r = 0;
4154
Borislav Petkovd366bf72016-04-04 22:25:02 +02004155 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004156 return -EINVAL;
4157
4158 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4159 return -EINVAL;
4160
4161 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4162 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004163 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004164 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004165 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004166 break;
4167 }
4168 if (r)
4169 r = -EINVAL;
4170 return r;
4171}
4172
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004173/*
4174 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4175 * stopped by the hypervisor. This function will be called from the host only.
4176 * EINVAL is returned when the host attempts to set the flag for a guest that
4177 * does not support pv clocks.
4178 */
4179static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4180{
Andy Honig0b794592013-02-20 14:48:10 -08004181 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004182 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004183 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004184 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4185 return 0;
4186}
4187
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004188static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4189 struct kvm_enable_cap *cap)
4190{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004191 int r;
4192 uint16_t vmcs_version;
4193 void __user *user_ptr;
4194
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004195 if (cap->flags)
4196 return -EINVAL;
4197
4198 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004199 case KVM_CAP_HYPERV_SYNIC2:
4200 if (cap->args[0])
4201 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004202 /* fall through */
4203
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004204 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004205 if (!irqchip_in_kernel(vcpu->kvm))
4206 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004207 return kvm_hv_activate_synic(vcpu, cap->cap ==
4208 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004209 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004210 if (!kvm_x86_ops->nested_enable_evmcs)
4211 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004212 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4213 if (!r) {
4214 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4215 if (copy_to_user(user_ptr, &vmcs_version,
4216 sizeof(vmcs_version)))
4217 r = -EFAULT;
4218 }
4219 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004220 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4221 if (!kvm_x86_ops->enable_direct_tlbflush)
4222 return -ENOTTY;
4223
4224 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004225
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004226 default:
4227 return -EINVAL;
4228 }
4229}
4230
Carsten Otte313a3dc2007-10-11 19:16:52 +02004231long kvm_arch_vcpu_ioctl(struct file *filp,
4232 unsigned int ioctl, unsigned long arg)
4233{
4234 struct kvm_vcpu *vcpu = filp->private_data;
4235 void __user *argp = (void __user *)arg;
4236 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004237 union {
4238 struct kvm_lapic_state *lapic;
4239 struct kvm_xsave *xsave;
4240 struct kvm_xcrs *xcrs;
4241 void *buffer;
4242 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004243
Christoffer Dall9b0624712017-12-04 21:35:36 +01004244 vcpu_load(vcpu);
4245
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004246 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004247 switch (ioctl) {
4248 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004249 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004250 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004251 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004252 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4253 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004254
Dave Hansenb772ff32008-08-11 10:01:47 -07004255 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004256 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004257 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004258 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004259 if (r)
4260 goto out;
4261 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004262 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004263 goto out;
4264 r = 0;
4265 break;
4266 }
4267 case KVM_SET_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;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004271 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004272 if (IS_ERR(u.lapic)) {
4273 r = PTR_ERR(u.lapic);
4274 goto out_nofree;
4275 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004276
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004277 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004278 break;
4279 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004280 case KVM_INTERRUPT: {
4281 struct kvm_interrupt irq;
4282
4283 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004284 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004285 goto out;
4286 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004287 break;
4288 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004289 case KVM_NMI: {
4290 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004291 break;
4292 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004293 case KVM_SMI: {
4294 r = kvm_vcpu_ioctl_smi(vcpu);
4295 break;
4296 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004297 case KVM_SET_CPUID: {
4298 struct kvm_cpuid __user *cpuid_arg = argp;
4299 struct kvm_cpuid cpuid;
4300
4301 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004302 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004303 goto out;
4304 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004305 break;
4306 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004307 case KVM_SET_CPUID2: {
4308 struct kvm_cpuid2 __user *cpuid_arg = argp;
4309 struct kvm_cpuid2 cpuid;
4310
4311 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004312 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004313 goto out;
4314 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004315 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004316 break;
4317 }
4318 case KVM_GET_CPUID2: {
4319 struct kvm_cpuid2 __user *cpuid_arg = argp;
4320 struct kvm_cpuid2 cpuid;
4321
4322 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004323 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004324 goto out;
4325 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004326 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004327 if (r)
4328 goto out;
4329 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004330 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004331 goto out;
4332 r = 0;
4333 break;
4334 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004335 case KVM_GET_MSRS: {
4336 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004337 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004338 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004339 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004340 }
4341 case KVM_SET_MSRS: {
4342 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004343 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004344 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004345 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004346 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004347 case KVM_TPR_ACCESS_REPORTING: {
4348 struct kvm_tpr_access_ctl tac;
4349
4350 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004351 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004352 goto out;
4353 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4354 if (r)
4355 goto out;
4356 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004357 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004358 goto out;
4359 r = 0;
4360 break;
4361 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004362 case KVM_SET_VAPIC_ADDR: {
4363 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004364 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004365
4366 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004367 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004368 goto out;
4369 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004370 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004371 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004372 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004373 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004374 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004375 break;
4376 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004377 case KVM_X86_SETUP_MCE: {
4378 u64 mcg_cap;
4379
4380 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004381 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004382 goto out;
4383 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4384 break;
4385 }
4386 case KVM_X86_SET_MCE: {
4387 struct kvm_x86_mce mce;
4388
4389 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004390 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004391 goto out;
4392 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4393 break;
4394 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004395 case KVM_GET_VCPU_EVENTS: {
4396 struct kvm_vcpu_events events;
4397
4398 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4399
4400 r = -EFAULT;
4401 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4402 break;
4403 r = 0;
4404 break;
4405 }
4406 case KVM_SET_VCPU_EVENTS: {
4407 struct kvm_vcpu_events events;
4408
4409 r = -EFAULT;
4410 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4411 break;
4412
4413 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4414 break;
4415 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004416 case KVM_GET_DEBUGREGS: {
4417 struct kvm_debugregs dbgregs;
4418
4419 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4420
4421 r = -EFAULT;
4422 if (copy_to_user(argp, &dbgregs,
4423 sizeof(struct kvm_debugregs)))
4424 break;
4425 r = 0;
4426 break;
4427 }
4428 case KVM_SET_DEBUGREGS: {
4429 struct kvm_debugregs dbgregs;
4430
4431 r = -EFAULT;
4432 if (copy_from_user(&dbgregs, argp,
4433 sizeof(struct kvm_debugregs)))
4434 break;
4435
4436 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4437 break;
4438 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004439 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004440 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004441 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004442 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004443 break;
4444
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004445 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004446
4447 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004448 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004449 break;
4450 r = 0;
4451 break;
4452 }
4453 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004454 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004455 if (IS_ERR(u.xsave)) {
4456 r = PTR_ERR(u.xsave);
4457 goto out_nofree;
4458 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004459
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004460 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004461 break;
4462 }
4463 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004464 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004465 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004466 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004467 break;
4468
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004469 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004470
4471 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004472 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004473 sizeof(struct kvm_xcrs)))
4474 break;
4475 r = 0;
4476 break;
4477 }
4478 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004479 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004480 if (IS_ERR(u.xcrs)) {
4481 r = PTR_ERR(u.xcrs);
4482 goto out_nofree;
4483 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004484
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004485 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004486 break;
4487 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004488 case KVM_SET_TSC_KHZ: {
4489 u32 user_tsc_khz;
4490
4491 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004492 user_tsc_khz = (u32)arg;
4493
4494 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4495 goto out;
4496
Zachary Amsdencc578282012-02-03 15:43:50 -02004497 if (user_tsc_khz == 0)
4498 user_tsc_khz = tsc_khz;
4499
Haozhong Zhang381d5852015-10-20 15:39:04 +08004500 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4501 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004502
Joerg Roedel92a1f122011-03-25 09:44:51 +01004503 goto out;
4504 }
4505 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004506 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004507 goto out;
4508 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004509 case KVM_KVMCLOCK_CTRL: {
4510 r = kvm_set_guest_paused(vcpu);
4511 goto out;
4512 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004513 case KVM_ENABLE_CAP: {
4514 struct kvm_enable_cap cap;
4515
4516 r = -EFAULT;
4517 if (copy_from_user(&cap, argp, sizeof(cap)))
4518 goto out;
4519 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4520 break;
4521 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004522 case KVM_GET_NESTED_STATE: {
4523 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4524 u32 user_data_size;
4525
4526 r = -EINVAL;
4527 if (!kvm_x86_ops->get_nested_state)
4528 break;
4529
4530 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004531 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004532 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004533 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004534
4535 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4536 user_data_size);
4537 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004538 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004539
4540 if (r > user_data_size) {
4541 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004542 r = -EFAULT;
4543 else
4544 r = -E2BIG;
4545 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004546 }
Liran Alon26b471c2018-09-16 14:28:20 +03004547
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004548 r = 0;
4549 break;
4550 }
4551 case KVM_SET_NESTED_STATE: {
4552 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4553 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004554 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004555
4556 r = -EINVAL;
4557 if (!kvm_x86_ops->set_nested_state)
4558 break;
4559
Liran Alon26b471c2018-09-16 14:28:20 +03004560 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004561 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004562 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004563
Liran Alon26b471c2018-09-16 14:28:20 +03004564 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004565 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004566 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004567
4568 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004569 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4570 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004571 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004572
4573 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004574 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4575 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004576 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004577
Sean Christophersonad5996d2019-11-22 08:58:18 -08004578 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004579 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004580 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004581 break;
4582 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004583 case KVM_GET_SUPPORTED_HV_CPUID: {
4584 struct kvm_cpuid2 __user *cpuid_arg = argp;
4585 struct kvm_cpuid2 cpuid;
4586
4587 r = -EFAULT;
4588 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4589 goto out;
4590
4591 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4592 cpuid_arg->entries);
4593 if (r)
4594 goto out;
4595
4596 r = -EFAULT;
4597 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4598 goto out;
4599 r = 0;
4600 break;
4601 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004602 default:
4603 r = -EINVAL;
4604 }
4605out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004606 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004607out_nofree:
4608 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004609 return r;
4610}
4611
Souptick Joarder1499fa82018-04-19 00:49:58 +05304612vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004613{
4614 return VM_FAULT_SIGBUS;
4615}
4616
Carsten Otte1fe779f2007-10-29 16:08:35 +01004617static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4618{
4619 int ret;
4620
4621 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004622 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004623 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4624 return ret;
4625}
4626
Sheng Yangb927a3c2009-07-21 10:42:48 +08004627static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4628 u64 ident_addr)
4629{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004630 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004631}
4632
Carsten Otte1fe779f2007-10-29 16:08:35 +01004633static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004634 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004635{
4636 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4637 return -EINVAL;
4638
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004639 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004640
4641 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004642 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004643
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004644 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004645 return 0;
4646}
4647
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004648static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004649{
Dave Hansen39de71e2010-08-19 18:11:14 -07004650 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004651}
4652
Carsten Otte1fe779f2007-10-29 16:08:35 +01004653static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4654{
David Hildenbrand90bca052017-04-07 10:50:23 +02004655 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004656 int r;
4657
4658 r = 0;
4659 switch (chip->chip_id) {
4660 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004661 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004662 sizeof(struct kvm_pic_state));
4663 break;
4664 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004665 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004666 sizeof(struct kvm_pic_state));
4667 break;
4668 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004669 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004670 break;
4671 default:
4672 r = -EINVAL;
4673 break;
4674 }
4675 return r;
4676}
4677
4678static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4679{
David Hildenbrand90bca052017-04-07 10:50:23 +02004680 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004681 int r;
4682
4683 r = 0;
4684 switch (chip->chip_id) {
4685 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004686 spin_lock(&pic->lock);
4687 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004688 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004689 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004690 break;
4691 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004692 spin_lock(&pic->lock);
4693 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004694 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004695 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004696 break;
4697 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004698 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004699 break;
4700 default:
4701 r = -EINVAL;
4702 break;
4703 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004704 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004705 return r;
4706}
4707
Sheng Yange0f63cb2008-03-04 00:50:59 +08004708static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4709{
Radim Krčmář34f39412016-03-02 22:56:50 +01004710 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4711
4712 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4713
4714 mutex_lock(&kps->lock);
4715 memcpy(ps, &kps->channels, sizeof(*ps));
4716 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304717 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004718}
4719
4720static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4721{
Andrew Honig01856042015-11-18 14:50:23 -08004722 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004723 struct kvm_pit *pit = kvm->arch.vpit;
4724
4725 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004726 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004727 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004728 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4729 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304730 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004731}
4732
4733static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4734{
Beth Kone9f42752009-07-07 11:50:38 -04004735 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4736 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4737 sizeof(ps->channels));
4738 ps->flags = kvm->arch.vpit->pit_state.flags;
4739 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004740 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304741 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004742}
4743
4744static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4745{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304746 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004747 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004748 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004749 struct kvm_pit *pit = kvm->arch.vpit;
4750
4751 mutex_lock(&pit->pit_state.lock);
4752 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004753 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4754 if (!prev_legacy && cur_legacy)
4755 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004756 memcpy(&pit->pit_state.channels, &ps->channels,
4757 sizeof(pit->pit_state.channels));
4758 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004759 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004760 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004761 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004762 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304763 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004764}
4765
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004766static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4767 struct kvm_reinject_control *control)
4768{
Radim Krčmář71474e22016-03-02 22:56:45 +01004769 struct kvm_pit *pit = kvm->arch.vpit;
4770
Radim Krčmář71474e22016-03-02 22:56:45 +01004771 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4772 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4773 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4774 */
4775 mutex_lock(&pit->pit_state.lock);
4776 kvm_pit_set_reinject(pit, control->pit_reinject);
4777 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004778
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004779 return 0;
4780}
4781
Sean Christopherson0dff0842020-02-18 13:07:29 -08004782void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004783{
Kai Huang88178fd2015-01-28 10:54:27 +08004784 /*
4785 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4786 */
4787 if (kvm_x86_ops->flush_log_dirty)
4788 kvm_x86_ops->flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004789}
4790
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004791int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4792 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004793{
4794 if (!irqchip_in_kernel(kvm))
4795 return -ENXIO;
4796
4797 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004798 irq_event->irq, irq_event->level,
4799 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004800 return 0;
4801}
4802
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004803int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4804 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004805{
4806 int r;
4807
4808 if (cap->flags)
4809 return -EINVAL;
4810
4811 switch (cap->cap) {
4812 case KVM_CAP_DISABLE_QUIRKS:
4813 kvm->arch.disabled_quirks = cap->args[0];
4814 r = 0;
4815 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004816 case KVM_CAP_SPLIT_IRQCHIP: {
4817 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004818 r = -EINVAL;
4819 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4820 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004821 r = -EEXIST;
4822 if (irqchip_in_kernel(kvm))
4823 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004824 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004825 goto split_irqchip_unlock;
4826 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004827 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004828 goto split_irqchip_unlock;
4829 /* Pairs with irqchip_in_kernel. */
4830 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004831 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004832 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004833 r = 0;
4834split_irqchip_unlock:
4835 mutex_unlock(&kvm->lock);
4836 break;
4837 }
Radim Krčmář371313132016-07-12 22:09:27 +02004838 case KVM_CAP_X2APIC_API:
4839 r = -EINVAL;
4840 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4841 break;
4842
4843 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4844 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004845 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4846 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004847
4848 r = 0;
4849 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004850 case KVM_CAP_X86_DISABLE_EXITS:
4851 r = -EINVAL;
4852 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4853 break;
4854
4855 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4856 kvm_can_mwait_in_guest())
4857 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004858 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004859 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004860 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4861 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004862 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4863 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004864 r = 0;
4865 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004866 case KVM_CAP_MSR_PLATFORM_INFO:
4867 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4868 r = 0;
4869 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004870 case KVM_CAP_EXCEPTION_PAYLOAD:
4871 kvm->arch.exception_payload_enabled = cap->args[0];
4872 r = 0;
4873 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004874 default:
4875 r = -EINVAL;
4876 break;
4877 }
4878 return r;
4879}
4880
Carsten Otte1fe779f2007-10-29 16:08:35 +01004881long kvm_arch_vm_ioctl(struct file *filp,
4882 unsigned int ioctl, unsigned long arg)
4883{
4884 struct kvm *kvm = filp->private_data;
4885 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004886 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004887 /*
4888 * This union makes it completely explicit to gcc-3.x
4889 * that these two variables' stack usage should be
4890 * combined, not added together.
4891 */
4892 union {
4893 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004894 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004895 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004896 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004897
4898 switch (ioctl) {
4899 case KVM_SET_TSS_ADDR:
4900 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004901 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004902 case KVM_SET_IDENTITY_MAP_ADDR: {
4903 u64 ident_addr;
4904
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004905 mutex_lock(&kvm->lock);
4906 r = -EINVAL;
4907 if (kvm->created_vcpus)
4908 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004909 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004910 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004911 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004912 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004913set_identity_unlock:
4914 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004915 break;
4916 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004917 case KVM_SET_NR_MMU_PAGES:
4918 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004919 break;
4920 case KVM_GET_NR_MMU_PAGES:
4921 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4922 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004923 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004924 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004925
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004926 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004927 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004928 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004929
Avi Kivity3e515702012-03-05 14:23:29 +02004930 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004931 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004932 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004933
4934 r = kvm_pic_init(kvm);
4935 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004936 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004937
4938 r = kvm_ioapic_init(kvm);
4939 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004940 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004941 goto create_irqchip_unlock;
4942 }
4943
Avi Kivity399ec802008-11-19 13:58:46 +02004944 r = kvm_setup_default_irq_routing(kvm);
4945 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004946 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004947 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004948 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004949 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004950 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004951 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004952 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004953 create_irqchip_unlock:
4954 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004955 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004956 }
Sheng Yang78376992008-01-28 05:10:22 +08004957 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004958 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4959 goto create_pit;
4960 case KVM_CREATE_PIT2:
4961 r = -EFAULT;
4962 if (copy_from_user(&u.pit_config, argp,
4963 sizeof(struct kvm_pit_config)))
4964 goto out;
4965 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004966 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004967 r = -EEXIST;
4968 if (kvm->arch.vpit)
4969 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004970 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004971 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004972 if (kvm->arch.vpit)
4973 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004974 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004975 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004976 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004977 case KVM_GET_IRQCHIP: {
4978 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004979 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004980
Sasha Levinff5c2c02011-12-04 19:36:29 +02004981 chip = memdup_user(argp, sizeof(*chip));
4982 if (IS_ERR(chip)) {
4983 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004984 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004985 }
4986
Carsten Otte1fe779f2007-10-29 16:08:35 +01004987 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004988 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004989 goto get_irqchip_out;
4990 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4991 if (r)
4992 goto get_irqchip_out;
4993 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004994 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07004995 goto get_irqchip_out;
4996 r = 0;
4997 get_irqchip_out:
4998 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004999 break;
5000 }
5001 case KVM_SET_IRQCHIP: {
5002 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005003 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005004
Sasha Levinff5c2c02011-12-04 19:36:29 +02005005 chip = memdup_user(argp, sizeof(*chip));
5006 if (IS_ERR(chip)) {
5007 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005008 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005009 }
5010
Carsten Otte1fe779f2007-10-29 16:08:35 +01005011 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005012 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005013 goto set_irqchip_out;
5014 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005015 set_irqchip_out:
5016 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005017 break;
5018 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005019 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005020 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005021 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005022 goto out;
5023 r = -ENXIO;
5024 if (!kvm->arch.vpit)
5025 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005026 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005027 if (r)
5028 goto out;
5029 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005030 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005031 goto out;
5032 r = 0;
5033 break;
5034 }
5035 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005036 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005037 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005038 goto out;
5039 r = -ENXIO;
5040 if (!kvm->arch.vpit)
5041 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005042 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005043 break;
5044 }
Beth Kone9f42752009-07-07 11:50:38 -04005045 case KVM_GET_PIT2: {
5046 r = -ENXIO;
5047 if (!kvm->arch.vpit)
5048 goto out;
5049 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5050 if (r)
5051 goto out;
5052 r = -EFAULT;
5053 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5054 goto out;
5055 r = 0;
5056 break;
5057 }
5058 case KVM_SET_PIT2: {
5059 r = -EFAULT;
5060 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5061 goto out;
5062 r = -ENXIO;
5063 if (!kvm->arch.vpit)
5064 goto out;
5065 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005066 break;
5067 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005068 case KVM_REINJECT_CONTROL: {
5069 struct kvm_reinject_control control;
5070 r = -EFAULT;
5071 if (copy_from_user(&control, argp, sizeof(control)))
5072 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005073 r = -ENXIO;
5074 if (!kvm->arch.vpit)
5075 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005076 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005077 break;
5078 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005079 case KVM_SET_BOOT_CPU_ID:
5080 r = 0;
5081 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005082 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005083 r = -EBUSY;
5084 else
5085 kvm->arch.bsp_vcpu_id = arg;
5086 mutex_unlock(&kvm->lock);
5087 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005088 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005089 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005090 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005091 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005092 goto out;
5093 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005094 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005095 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005096 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005097 r = 0;
5098 break;
5099 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005100 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005101 struct kvm_clock_data user_ns;
5102 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005103
5104 r = -EFAULT;
5105 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5106 goto out;
5107
5108 r = -EINVAL;
5109 if (user_ns.flags)
5110 goto out;
5111
5112 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005113 /*
5114 * TODO: userspace has to take care of races with VCPU_RUN, so
5115 * kvm_gen_update_masterclock() can be cut down to locked
5116 * pvclock_update_vm_gtod_copy().
5117 */
5118 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005119 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005120 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005121 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005122 break;
5123 }
5124 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005125 struct kvm_clock_data user_ns;
5126 u64 now_ns;
5127
Marcelo Tosattie891a322017-04-17 12:51:37 -03005128 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005129 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005130 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005131 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005132
5133 r = -EFAULT;
5134 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5135 goto out;
5136 r = 0;
5137 break;
5138 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005139 case KVM_MEMORY_ENCRYPT_OP: {
5140 r = -ENOTTY;
5141 if (kvm_x86_ops->mem_enc_op)
5142 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5143 break;
5144 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005145 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5146 struct kvm_enc_region region;
5147
5148 r = -EFAULT;
5149 if (copy_from_user(&region, argp, sizeof(region)))
5150 goto out;
5151
5152 r = -ENOTTY;
5153 if (kvm_x86_ops->mem_enc_reg_region)
5154 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5155 break;
5156 }
5157 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5158 struct kvm_enc_region region;
5159
5160 r = -EFAULT;
5161 if (copy_from_user(&region, argp, sizeof(region)))
5162 goto out;
5163
5164 r = -ENOTTY;
5165 if (kvm_x86_ops->mem_enc_unreg_region)
5166 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5167 break;
5168 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005169 case KVM_HYPERV_EVENTFD: {
5170 struct kvm_hyperv_eventfd hvevfd;
5171
5172 r = -EFAULT;
5173 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5174 goto out;
5175 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5176 break;
5177 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005178 case KVM_SET_PMU_EVENT_FILTER:
5179 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5180 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005181 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005182 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005183 }
5184out:
5185 return r;
5186}
5187
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005188static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005189{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005190 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005191 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005192 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005193
Jim Mattsone2ada662019-08-21 11:20:04 -07005194 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005195 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005196
5197 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005198
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005199 num_msrs_to_save = 0;
5200 num_emulated_msrs = 0;
5201 num_msr_based_features = 0;
5202
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005203 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5204 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005205 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005206
5207 /*
5208 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005209 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005210 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005211 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005212 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005213 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005214 continue;
5215 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005216 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005217 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005218 continue;
5219 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005220 case MSR_IA32_RTIT_CTL:
5221 case MSR_IA32_RTIT_STATUS:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005222 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Chao Pengbf8c55d2018-10-24 16:05:14 +08005223 continue;
5224 break;
5225 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005226 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005227 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5228 continue;
5229 break;
5230 case MSR_IA32_RTIT_OUTPUT_BASE:
5231 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson7b874c22020-03-02 15:56:59 -08005232 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08005233 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5234 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5235 continue;
5236 break;
5237 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
Sean Christopherson7b874c22020-03-02 15:56:59 -08005238 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005239 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005240 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5241 continue;
5242 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005243 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005244 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005245 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5246 continue;
5247 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005248 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005249 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005250 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5251 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005252 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005253 default:
5254 break;
5255 }
5256
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005257 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005258 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005259
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005260 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5261 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005262 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005263
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005264 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005265 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005266
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005267 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005268 struct kvm_msr_entry msr;
5269
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005270 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005271 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005272 continue;
5273
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005274 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005275 }
Carsten Otte043405e2007-10-10 17:16:19 +02005276}
5277
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005278static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5279 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005280{
Avi Kivity70252a12010-01-19 12:51:22 +02005281 int handled = 0;
5282 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005283
Avi Kivity70252a12010-01-19 12:51:22 +02005284 do {
5285 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005286 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005287 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5288 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005289 break;
5290 handled += n;
5291 addr += n;
5292 len -= n;
5293 v += n;
5294 } while (len);
5295
5296 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005297}
5298
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005299static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005300{
Avi Kivity70252a12010-01-19 12:51:22 +02005301 int handled = 0;
5302 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005303
Avi Kivity70252a12010-01-19 12:51:22 +02005304 do {
5305 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005306 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005307 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5308 addr, n, v))
5309 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005310 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005311 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005312 handled += n;
5313 addr += n;
5314 len -= n;
5315 v += n;
5316 } while (len);
5317
5318 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005319}
5320
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005321static void kvm_set_segment(struct kvm_vcpu *vcpu,
5322 struct kvm_segment *var, int seg)
5323{
5324 kvm_x86_ops->set_segment(vcpu, var, seg);
5325}
5326
5327void kvm_get_segment(struct kvm_vcpu *vcpu,
5328 struct kvm_segment *var, int seg)
5329{
5330 kvm_x86_ops->get_segment(vcpu, var, seg);
5331}
5332
Paolo Bonzini54987b72014-09-02 13:23:06 +02005333gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5334 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005335{
5336 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005337
5338 BUG_ON(!mmu_is_nested(vcpu));
5339
5340 /* NPT walks are always user-walks */
5341 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005342 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005343
5344 return t_gpa;
5345}
5346
Avi Kivityab9ae312010-11-22 17:53:26 +02005347gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5348 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005349{
5350 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005351 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005352}
5353
Avi Kivityab9ae312010-11-22 17:53:26 +02005354 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5355 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005356{
5357 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5358 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005359 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005360}
5361
Avi Kivityab9ae312010-11-22 17:53:26 +02005362gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5363 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005364{
5365 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5366 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005367 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005368}
5369
5370/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005371gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5372 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005373{
Avi Kivityab9ae312010-11-22 17:53:26 +02005374 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005375}
5376
5377static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5378 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005379 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005380{
5381 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005382 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005383
5384 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005385 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005386 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005387 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005388 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005389 int ret;
5390
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005391 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005392 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005393 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5394 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005395 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005396 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005397 goto out;
5398 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005399
Izik Eidus77c20022008-12-29 01:42:19 +02005400 bytes -= toread;
5401 data += toread;
5402 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005403 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005404out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005405 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005406}
Izik Eidus77c20022008-12-29 01:42:19 +02005407
Gleb Natapov1871c602010-02-10 14:21:32 +02005408/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005409static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5410 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005411 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005412{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005413 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005414 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005415 unsigned offset;
5416 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005417
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005418 /* Inline kvm_read_guest_virt_helper for speed. */
5419 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5420 exception);
5421 if (unlikely(gpa == UNMAPPED_GVA))
5422 return X86EMUL_PROPAGATE_FAULT;
5423
5424 offset = addr & (PAGE_SIZE-1);
5425 if (WARN_ON(offset + bytes > PAGE_SIZE))
5426 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005427 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5428 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005429 if (unlikely(ret < 0))
5430 return X86EMUL_IO_NEEDED;
5431
5432 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005433}
5434
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005435int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005436 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005437 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005438{
5439 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005440
Paolo Bonzini353c0952019-01-29 18:41:16 +01005441 /*
5442 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5443 * is returned, but our callers are not ready for that and they blindly
5444 * call kvm_inject_page_fault. Ensure that they at least do not leak
5445 * uninitialized kernel stack memory into cr2 and error code.
5446 */
5447 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005448 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005449 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005450}
Nadav Har'El064aea72011-05-25 23:04:56 +03005451EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005452
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005453static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5454 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005455 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005456{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005457 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005458 u32 access = 0;
5459
5460 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5461 access |= PFERR_USER_MASK;
5462
5463 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005464}
5465
Radim Krčmář7a036a62015-10-30 16:36:24 +01005466static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5467 unsigned long addr, void *val, unsigned int bytes)
5468{
5469 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5470 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5471
5472 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5473}
5474
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005475static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5476 struct kvm_vcpu *vcpu, u32 access,
5477 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005478{
5479 void *data = val;
5480 int r = X86EMUL_CONTINUE;
5481
5482 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005483 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005484 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005485 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005486 unsigned offset = addr & (PAGE_SIZE-1);
5487 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5488 int ret;
5489
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005490 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005491 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005492 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005493 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005494 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005495 goto out;
5496 }
5497
5498 bytes -= towrite;
5499 data += towrite;
5500 addr += towrite;
5501 }
5502out:
5503 return r;
5504}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005505
5506static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005507 unsigned int bytes, struct x86_exception *exception,
5508 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005509{
5510 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005511 u32 access = PFERR_WRITE_MASK;
5512
5513 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5514 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005515
5516 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005517 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005518}
5519
5520int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5521 unsigned int bytes, struct x86_exception *exception)
5522{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005523 /* kvm_write_guest_virt_system can pull in tons of pages. */
5524 vcpu->arch.l1tf_flush_l1d = true;
5525
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005526 /*
5527 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5528 * is returned, but our callers are not ready for that and they blindly
5529 * call kvm_inject_page_fault. Ensure that they at least do not leak
5530 * uninitialized kernel stack memory into cr2 and error code.
5531 */
5532 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005533 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5534 PFERR_WRITE_MASK, exception);
5535}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005536EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005537
Wanpeng Li082d06e2018-04-03 16:28:48 -07005538int handle_ud(struct kvm_vcpu *vcpu)
5539{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005540 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005541 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005542 char sig[5]; /* ud2; .ascii "kvm" */
5543 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005544
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005545 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005546 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5547 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005548 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005549 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005550 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005551 }
5552
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005553 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005554}
5555EXPORT_SYMBOL_GPL(handle_ud);
5556
Tom Lendacky0f89b202016-12-14 14:59:23 -05005557static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5558 gpa_t gpa, bool write)
5559{
5560 /* For APIC access vmexit */
5561 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5562 return 1;
5563
5564 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5565 trace_vcpu_match_mmio(gva, gpa, write, true);
5566 return 1;
5567 }
5568
5569 return 0;
5570}
5571
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005572static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5573 gpa_t *gpa, struct x86_exception *exception,
5574 bool write)
5575{
Avi Kivity97d64b72012-09-12 14:52:00 +03005576 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5577 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005578
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005579 /*
5580 * currently PKRU is only applied to ept enabled guest so
5581 * there is no pkey in EPT page table for L1 guest or EPT
5582 * shadow page table for L2 guest.
5583 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005584 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005585 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005586 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005587 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5588 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005589 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005590 return 1;
5591 }
5592
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005593 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5594
5595 if (*gpa == UNMAPPED_GVA)
5596 return -1;
5597
Tom Lendacky0f89b202016-12-14 14:59:23 -05005598 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005599}
5600
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005601int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005602 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005603{
5604 int ret;
5605
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005606 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005607 if (ret < 0)
5608 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005609 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005610 return 1;
5611}
5612
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005613struct read_write_emulator_ops {
5614 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5615 int bytes);
5616 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5617 void *val, int bytes);
5618 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5619 int bytes, void *val);
5620 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5621 void *val, int bytes);
5622 bool write;
5623};
5624
5625static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5626{
5627 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005628 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005629 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005630 vcpu->mmio_read_completed = 0;
5631 return 1;
5632 }
5633
5634 return 0;
5635}
5636
5637static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5638 void *val, int bytes)
5639{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005640 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005641}
5642
5643static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5644 void *val, int bytes)
5645{
5646 return emulator_write_phys(vcpu, gpa, val, bytes);
5647}
5648
5649static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5650{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005651 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005652 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5653}
5654
5655static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5656 void *val, int bytes)
5657{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005658 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005659 return X86EMUL_IO_NEEDED;
5660}
5661
5662static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5663 void *val, int bytes)
5664{
Avi Kivityf78146b2012-04-18 19:22:47 +03005665 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5666
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005667 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005668 return X86EMUL_CONTINUE;
5669}
5670
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005671static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005672 .read_write_prepare = read_prepare,
5673 .read_write_emulate = read_emulate,
5674 .read_write_mmio = vcpu_mmio_read,
5675 .read_write_exit_mmio = read_exit_mmio,
5676};
5677
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005678static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005679 .read_write_emulate = write_emulate,
5680 .read_write_mmio = write_mmio,
5681 .read_write_exit_mmio = write_exit_mmio,
5682 .write = true,
5683};
5684
Xiao Guangrong22388a32011-07-13 14:32:31 +08005685static int emulator_read_write_onepage(unsigned long addr, void *val,
5686 unsigned int bytes,
5687 struct x86_exception *exception,
5688 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005689 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005690{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005691 gpa_t gpa;
5692 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005693 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005694 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005695 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005696
5697 /*
5698 * If the exit was due to a NPF we may already have a GPA.
5699 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5700 * Note, this cannot be used on string operations since string
5701 * operation using rep will only have the initial GPA from the NPF
5702 * occurred.
5703 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005704 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5705 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5706 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005707 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5708 } else {
5709 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5710 if (ret < 0)
5711 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005712 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005713
Brijesh Singh618232e2017-08-17 18:36:57 +02005714 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005715 return X86EMUL_CONTINUE;
5716
Carsten Ottebbd9b642007-10-30 18:44:21 +01005717 /*
5718 * Is this MMIO handled locally?
5719 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005720 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005721 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005722 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005723
Avi Kivity70252a12010-01-19 12:51:22 +02005724 gpa += handled;
5725 bytes -= handled;
5726 val += handled;
5727
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005728 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5729 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5730 frag->gpa = gpa;
5731 frag->data = val;
5732 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005733 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005734}
5735
Xiubo Li52eb5a62015-03-13 17:39:45 +08005736static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5737 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005738 void *val, unsigned int bytes,
5739 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005740 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005741{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005742 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005743 gpa_t gpa;
5744 int rc;
5745
5746 if (ops->read_write_prepare &&
5747 ops->read_write_prepare(vcpu, val, bytes))
5748 return X86EMUL_CONTINUE;
5749
5750 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005751
Carsten Ottebbd9b642007-10-30 18:44:21 +01005752 /* Crossing a page boundary? */
5753 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005754 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005755
5756 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005757 rc = emulator_read_write_onepage(addr, val, now, exception,
5758 vcpu, ops);
5759
Carsten Ottebbd9b642007-10-30 18:44:21 +01005760 if (rc != X86EMUL_CONTINUE)
5761 return rc;
5762 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005763 if (ctxt->mode != X86EMUL_MODE_PROT64)
5764 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005765 val += now;
5766 bytes -= now;
5767 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005768
Avi Kivityf78146b2012-04-18 19:22:47 +03005769 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5770 vcpu, ops);
5771 if (rc != X86EMUL_CONTINUE)
5772 return rc;
5773
5774 if (!vcpu->mmio_nr_fragments)
5775 return rc;
5776
5777 gpa = vcpu->mmio_fragments[0].gpa;
5778
5779 vcpu->mmio_needed = 1;
5780 vcpu->mmio_cur_fragment = 0;
5781
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005782 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005783 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5784 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5785 vcpu->run->mmio.phys_addr = gpa;
5786
5787 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005788}
5789
5790static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5791 unsigned long addr,
5792 void *val,
5793 unsigned int bytes,
5794 struct x86_exception *exception)
5795{
5796 return emulator_read_write(ctxt, addr, val, bytes,
5797 exception, &read_emultor);
5798}
5799
Xiubo Li52eb5a62015-03-13 17:39:45 +08005800static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005801 unsigned long addr,
5802 const void *val,
5803 unsigned int bytes,
5804 struct x86_exception *exception)
5805{
5806 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5807 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005808}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005809
Avi Kivitydaea3e72010-03-15 13:59:54 +02005810#define CMPXCHG_TYPE(t, ptr, old, new) \
5811 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5812
5813#ifdef CONFIG_X86_64
5814# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5815#else
5816# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005817 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005818#endif
5819
Avi Kivity0f65dd72011-04-20 13:37:53 +03005820static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5821 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005822 const void *old,
5823 const void *new,
5824 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005825 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005826{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005827 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005828 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005829 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005830 char *kaddr;
5831 bool exchanged;
5832
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005833 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005834 if (bytes > 8 || (bytes & (bytes - 1)))
5835 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005836
Avi Kivitydaea3e72010-03-15 13:59:54 +02005837 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005838
Avi Kivitydaea3e72010-03-15 13:59:54 +02005839 if (gpa == UNMAPPED_GVA ||
5840 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5841 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005842
Avi Kivitydaea3e72010-03-15 13:59:54 +02005843 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5844 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005845
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005846 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005847 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005848
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005849 kaddr = map.hva + offset_in_page(gpa);
5850
Avi Kivitydaea3e72010-03-15 13:59:54 +02005851 switch (bytes) {
5852 case 1:
5853 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5854 break;
5855 case 2:
5856 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5857 break;
5858 case 4:
5859 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5860 break;
5861 case 8:
5862 exchanged = CMPXCHG64(kaddr, old, new);
5863 break;
5864 default:
5865 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005866 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005867
5868 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005869
5870 if (!exchanged)
5871 return X86EMUL_CMPXCHG_FAILED;
5872
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005873 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005874
5875 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005876
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005877emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005878 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005879
Avi Kivity0f65dd72011-04-20 13:37:53 +03005880 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005881}
5882
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005883static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5884{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005885 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005886
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005887 for (i = 0; i < vcpu->arch.pio.count; i++) {
5888 if (vcpu->arch.pio.in)
5889 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5890 vcpu->arch.pio.size, pd);
5891 else
5892 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5893 vcpu->arch.pio.port, vcpu->arch.pio.size,
5894 pd);
5895 if (r)
5896 break;
5897 pd += vcpu->arch.pio.size;
5898 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005899 return r;
5900}
5901
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005902static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5903 unsigned short port, void *val,
5904 unsigned int count, bool in)
5905{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005906 vcpu->arch.pio.port = port;
5907 vcpu->arch.pio.in = in;
5908 vcpu->arch.pio.count = count;
5909 vcpu->arch.pio.size = size;
5910
5911 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5912 vcpu->arch.pio.count = 0;
5913 return 1;
5914 }
5915
5916 vcpu->run->exit_reason = KVM_EXIT_IO;
5917 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5918 vcpu->run->io.size = size;
5919 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5920 vcpu->run->io.count = count;
5921 vcpu->run->io.port = port;
5922
5923 return 0;
5924}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005925
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005926static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5927 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005928{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005929 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005930
Gleb Natapov79729952010-03-18 15:20:24 +02005931 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005932 goto data_avail;
5933
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005934 memset(vcpu->arch.pio_data, 0, size * count);
5935
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005936 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5937 if (ret) {
5938data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005939 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005940 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005941 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005942 return 1;
5943 }
5944
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005945 return 0;
5946}
5947
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005948static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5949 int size, unsigned short port, void *val,
5950 unsigned int count)
5951{
5952 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
5953
5954}
5955
5956static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5957 unsigned short port, const void *val,
5958 unsigned int count)
5959{
5960 memcpy(vcpu->arch.pio_data, val, size * count);
5961 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
5962 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
5963}
5964
Avi Kivityca1d4a92011-04-20 13:37:53 +03005965static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5966 int size, unsigned short port,
5967 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005968{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005969 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005970}
5971
Carsten Ottebbd9b642007-10-30 18:44:21 +01005972static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5973{
5974 return kvm_x86_ops->get_segment_base(vcpu, seg);
5975}
5976
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005977static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005978{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005979 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005980}
5981
Jiang Biaoae6a2372016-11-07 08:54:51 +08005982static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005983{
5984 if (!need_emulate_wbinvd(vcpu))
5985 return X86EMUL_CONTINUE;
5986
5987 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005988 int cpu = get_cpu();
5989
5990 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005991 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5992 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005993 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005994 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005995 } else
5996 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005997 return X86EMUL_CONTINUE;
5998}
Joel Schopp5cb56052015-03-02 13:43:31 -06005999
6000int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6001{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006002 kvm_emulate_wbinvd_noskip(vcpu);
6003 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006004}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006005EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6006
Joel Schopp5cb56052015-03-02 13:43:31 -06006007
6008
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006009static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6010{
Joel Schopp5cb56052015-03-02 13:43:31 -06006011 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006012}
6013
Xiubo Li52eb5a62015-03-13 17:39:45 +08006014static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6015 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006016{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006017 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006018}
6019
Xiubo Li52eb5a62015-03-13 17:39:45 +08006020static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6021 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006022{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006023
Avi Kivity717746e2011-04-20 13:37:53 +03006024 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006025}
6026
Gleb Natapov52a46612010-03-18 15:20:03 +02006027static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6028{
6029 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6030}
6031
Avi Kivity717746e2011-04-20 13:37:53 +03006032static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006033{
Avi Kivity717746e2011-04-20 13:37:53 +03006034 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006035 unsigned long value;
6036
6037 switch (cr) {
6038 case 0:
6039 value = kvm_read_cr0(vcpu);
6040 break;
6041 case 2:
6042 value = vcpu->arch.cr2;
6043 break;
6044 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006045 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006046 break;
6047 case 4:
6048 value = kvm_read_cr4(vcpu);
6049 break;
6050 case 8:
6051 value = kvm_get_cr8(vcpu);
6052 break;
6053 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006054 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006055 return 0;
6056 }
6057
6058 return value;
6059}
6060
Avi Kivity717746e2011-04-20 13:37:53 +03006061static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006062{
Avi Kivity717746e2011-04-20 13:37:53 +03006063 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006064 int res = 0;
6065
Gleb Natapov52a46612010-03-18 15:20:03 +02006066 switch (cr) {
6067 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006068 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006069 break;
6070 case 2:
6071 vcpu->arch.cr2 = val;
6072 break;
6073 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006074 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006075 break;
6076 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006077 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006078 break;
6079 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006080 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006081 break;
6082 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006083 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006084 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006085 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006086
6087 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006088}
6089
Avi Kivity717746e2011-04-20 13:37:53 +03006090static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006091{
Avi Kivity717746e2011-04-20 13:37:53 +03006092 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006093}
6094
Avi Kivity4bff1e862011-04-20 13:37:53 +03006095static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006096{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006097 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006098}
6099
Avi Kivity4bff1e862011-04-20 13:37:53 +03006100static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006101{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006102 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006103}
6104
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006105static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6106{
6107 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6108}
6109
6110static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6111{
6112 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6113}
6114
Avi Kivity4bff1e862011-04-20 13:37:53 +03006115static unsigned long emulator_get_cached_segment_base(
6116 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006117{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006118 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006119}
6120
Avi Kivity1aa36612011-04-27 13:20:30 +03006121static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6122 struct desc_struct *desc, u32 *base3,
6123 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006124{
6125 struct kvm_segment var;
6126
Avi Kivity4bff1e862011-04-20 13:37:53 +03006127 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006128 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006129
Gleb Natapov378a8b02013-01-21 15:36:48 +02006130 if (var.unusable) {
6131 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006132 if (base3)
6133 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006134 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006135 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006136
6137 if (var.g)
6138 var.limit >>= 12;
6139 set_desc_limit(desc, var.limit);
6140 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006141#ifdef CONFIG_X86_64
6142 if (base3)
6143 *base3 = var.base >> 32;
6144#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006145 desc->type = var.type;
6146 desc->s = var.s;
6147 desc->dpl = var.dpl;
6148 desc->p = var.present;
6149 desc->avl = var.avl;
6150 desc->l = var.l;
6151 desc->d = var.db;
6152 desc->g = var.g;
6153
6154 return true;
6155}
6156
Avi Kivity1aa36612011-04-27 13:20:30 +03006157static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6158 struct desc_struct *desc, u32 base3,
6159 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006160{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006161 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006162 struct kvm_segment var;
6163
Avi Kivity1aa36612011-04-27 13:20:30 +03006164 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006165 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006166#ifdef CONFIG_X86_64
6167 var.base |= ((u64)base3) << 32;
6168#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006169 var.limit = get_desc_limit(desc);
6170 if (desc->g)
6171 var.limit = (var.limit << 12) | 0xfff;
6172 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006173 var.dpl = desc->dpl;
6174 var.db = desc->d;
6175 var.s = desc->s;
6176 var.l = desc->l;
6177 var.g = desc->g;
6178 var.avl = desc->avl;
6179 var.present = desc->p;
6180 var.unusable = !var.present;
6181 var.padding = 0;
6182
6183 kvm_set_segment(vcpu, &var, seg);
6184 return;
6185}
6186
Avi Kivity717746e2011-04-20 13:37:53 +03006187static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6188 u32 msr_index, u64 *pdata)
6189{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006190 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006191}
6192
6193static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6194 u32 msr_index, u64 data)
6195{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006196 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006197}
6198
Paolo Bonzini64d60672015-05-07 11:36:11 +02006199static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6200{
6201 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6202
6203 return vcpu->arch.smbase;
6204}
6205
6206static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6207{
6208 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6209
6210 vcpu->arch.smbase = smbase;
6211}
6212
Nadav Amit67f4d422014-06-02 18:34:09 +03006213static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6214 u32 pmc)
6215{
Like Xu98ff80f2019-10-27 18:52:40 +08006216 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006217}
6218
Avi Kivity222d21a2011-11-10 14:57:30 +02006219static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6220 u32 pmc, u64 *pdata)
6221{
Wei Huangc6702c92015-06-19 13:44:45 +02006222 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006223}
6224
Avi Kivity6c3287f2011-04-20 15:43:05 +03006225static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6226{
6227 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6228}
6229
Avi Kivity29535382011-04-20 13:37:53 +03006230static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006231 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006232 enum x86_intercept_stage stage)
6233{
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006234 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage,
6235 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006236}
6237
Yu Zhange911eb32017-08-24 20:27:52 +08006238static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6239 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006240{
Yu Zhange911eb32017-08-24 20:27:52 +08006241 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006242}
6243
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006244static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6245{
6246 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6247}
6248
6249static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6250{
6251 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6252}
6253
6254static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6255{
6256 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6257}
6258
Avi Kivitydd856ef2012-08-27 23:46:17 +03006259static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6260{
6261 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6262}
6263
6264static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6265{
6266 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6267}
6268
Nadav Amit801806d2015-01-26 09:32:23 +02006269static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6270{
6271 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6272}
6273
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006274static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6275{
6276 return emul_to_vcpu(ctxt)->arch.hflags;
6277}
6278
6279static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6280{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006281 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006282}
6283
Sean Christophersoned193212019-04-02 08:03:09 -07006284static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6285 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006286{
Sean Christophersoned193212019-04-02 08:03:09 -07006287 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006288}
6289
Sean Christophersonc5833c72019-04-02 08:03:10 -07006290static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6291{
6292 kvm_smm_changed(emul_to_vcpu(ctxt));
6293}
6294
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006295static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6296{
6297 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6298}
6299
Mathias Krause0225fb52012-08-30 01:30:16 +02006300static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006301 .read_gpr = emulator_read_gpr,
6302 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006303 .read_std = emulator_read_std,
6304 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006305 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006306 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006307 .read_emulated = emulator_read_emulated,
6308 .write_emulated = emulator_write_emulated,
6309 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006310 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006311 .pio_in_emulated = emulator_pio_in_emulated,
6312 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006313 .get_segment = emulator_get_segment,
6314 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006315 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006316 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006317 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006318 .set_gdt = emulator_set_gdt,
6319 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006320 .get_cr = emulator_get_cr,
6321 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006322 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006323 .get_dr = emulator_get_dr,
6324 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006325 .get_smbase = emulator_get_smbase,
6326 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006327 .set_msr = emulator_set_msr,
6328 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006329 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006330 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006331 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006332 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006333 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006334 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006335 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006336 .guest_has_long_mode = emulator_guest_has_long_mode,
6337 .guest_has_movbe = emulator_guest_has_movbe,
6338 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006339 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006340 .get_hflags = emulator_get_hflags,
6341 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006342 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006343 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006344 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006345};
6346
Gleb Natapov95cb2292010-04-28 19:15:43 +03006347static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6348{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006349 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006350 /*
6351 * an sti; sti; sequence only disable interrupts for the first
6352 * instruction. So, if the last instruction, be it emulated or
6353 * not, left the system with the INT_STI flag enabled, it
6354 * means that the last instruction is an sti. We should not
6355 * leave the flag on in this case. The same goes for mov ss
6356 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006357 if (int_shadow & mask)
6358 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006359 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006360 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006361 if (!mask)
6362 kvm_make_request(KVM_REQ_EVENT, vcpu);
6363 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006364}
6365
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006366static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006367{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006368 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006369 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006370 return kvm_propagate_fault(vcpu, &ctxt->exception);
6371
6372 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006373 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6374 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006375 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006376 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006377 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006378}
6379
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006380static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6381{
6382 struct x86_emulate_ctxt *ctxt;
6383
6384 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6385 if (!ctxt) {
6386 pr_err("kvm: failed to allocate vcpu's emulator\n");
6387 return NULL;
6388 }
6389
6390 ctxt->vcpu = vcpu;
6391 ctxt->ops = &emulate_ops;
6392 vcpu->arch.emulate_ctxt = ctxt;
6393
6394 return ctxt;
6395}
6396
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006397static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6398{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006399 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006400 int cs_db, cs_l;
6401
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006402 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6403
Sean Christopherson744e6992020-02-18 15:03:09 -08006404 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006405 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006406 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6407
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006408 ctxt->eip = kvm_rip_read(vcpu);
6409 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6410 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006411 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006412 cs_db ? X86EMUL_MODE_PROT32 :
6413 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006414 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006415 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6416 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006417
Avi Kivitydd856ef2012-08-27 23:46:17 +03006418 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006419 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006420}
6421
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006422void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006423{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006424 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006425 int ret;
6426
6427 init_emulate_ctxt(vcpu);
6428
Avi Kivity9dac77f2011-06-01 15:34:25 +03006429 ctxt->op_bytes = 2;
6430 ctxt->ad_bytes = 2;
6431 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006432 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006433
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006434 if (ret != X86EMUL_CONTINUE) {
6435 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6436 } else {
6437 ctxt->eip = ctxt->_eip;
6438 kvm_rip_write(vcpu, ctxt->eip);
6439 kvm_set_rflags(vcpu, ctxt->eflags);
6440 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006441}
6442EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6443
Liran Alone2366172018-03-12 13:12:49 +02006444static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006445{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006446 ++vcpu->stat.insn_emulation_fail;
6447 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006448
Sean Christopherson42cbf062019-08-27 14:40:31 -07006449 if (emulation_type & EMULTYPE_VMWARE_GP) {
6450 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006451 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006452 }
Liran Alone2366172018-03-12 13:12:49 +02006453
Sean Christopherson738fece2019-08-27 14:40:34 -07006454 if (emulation_type & EMULTYPE_SKIP) {
6455 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6456 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6457 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006458 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006459 }
6460
Sean Christopherson22da61c2019-08-27 14:40:28 -07006461 kvm_queue_exception(vcpu, UD_VECTOR);
6462
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006463 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006464 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6465 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6466 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006467 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006468 }
Liran Alone2366172018-03-12 13:12:49 +02006469
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006470 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006471}
6472
Sean Christopherson736c2912019-12-06 15:57:14 -08006473static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006474 bool write_fault_to_shadow_pgtable,
6475 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006476{
Sean Christopherson736c2912019-12-06 15:57:14 -08006477 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006478 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006479
Sean Christopherson92daa482020-02-18 15:03:08 -08006480 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006481 return false;
6482
Sean Christopherson92daa482020-02-18 15:03:08 -08006483 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6484 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006485 return false;
6486
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006487 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006488 /*
6489 * Write permission should be allowed since only
6490 * write access need to be emulated.
6491 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006492 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006493
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006494 /*
6495 * If the mapping is invalid in guest, let cpu retry
6496 * it to generate fault.
6497 */
6498 if (gpa == UNMAPPED_GVA)
6499 return true;
6500 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006501
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006502 /*
6503 * Do not retry the unhandleable instruction if it faults on the
6504 * readonly host memory, otherwise it will goto a infinite loop:
6505 * retry instruction -> write #PF -> emulation fail -> retry
6506 * instruction -> ...
6507 */
6508 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006509
6510 /*
6511 * If the instruction failed on the error pfn, it can not be fixed,
6512 * report the error to userspace.
6513 */
6514 if (is_error_noslot_pfn(pfn))
6515 return false;
6516
6517 kvm_release_pfn_clean(pfn);
6518
6519 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006520 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006521 unsigned int indirect_shadow_pages;
6522
6523 spin_lock(&vcpu->kvm->mmu_lock);
6524 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6525 spin_unlock(&vcpu->kvm->mmu_lock);
6526
6527 if (indirect_shadow_pages)
6528 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6529
Gleb Natapova6f177e2010-07-08 12:41:12 +03006530 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006531 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006532
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006533 /*
6534 * if emulation was due to access to shadowed page table
6535 * and it failed try to unshadow page and re-enter the
6536 * guest to let CPU execute the instruction.
6537 */
6538 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006539
6540 /*
6541 * If the access faults on its page table, it can not
6542 * be fixed by unprotecting shadow page and it should
6543 * be reported to userspace.
6544 */
6545 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006546}
6547
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006548static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006549 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006550{
6551 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006552 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006553
6554 last_retry_eip = vcpu->arch.last_retry_eip;
6555 last_retry_addr = vcpu->arch.last_retry_addr;
6556
6557 /*
6558 * If the emulation is caused by #PF and it is non-page_table
6559 * writing instruction, it means the VM-EXIT is caused by shadow
6560 * page protected, we can zap the shadow page and retry this
6561 * instruction directly.
6562 *
6563 * Note: if the guest uses a non-page-table modifying instruction
6564 * on the PDE that points to the instruction, then we will unmap
6565 * the instruction and go to an infinite loop. So, we cache the
6566 * last retried eip and the last fault address, if we meet the eip
6567 * and the address again, we can break out of the potential infinite
6568 * loop.
6569 */
6570 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6571
Sean Christopherson92daa482020-02-18 15:03:08 -08006572 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006573 return false;
6574
Sean Christopherson92daa482020-02-18 15:03:08 -08006575 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6576 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006577 return false;
6578
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006579 if (x86_page_table_writing_insn(ctxt))
6580 return false;
6581
Sean Christopherson736c2912019-12-06 15:57:14 -08006582 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006583 return false;
6584
6585 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006586 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006587
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006588 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006589 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006590
Xiao Guangrong22368022013-01-13 23:44:12 +08006591 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006592
6593 return true;
6594}
6595
Gleb Natapov716d51a2012-09-03 15:24:26 +03006596static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6597static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6598
Paolo Bonzini64d60672015-05-07 11:36:11 +02006599static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006600{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006601 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006602 /* This is a good place to trace that we are exiting SMM. */
6603 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6604
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006605 /* Process a latched INIT or SMI, if any. */
6606 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006607 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006608
6609 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006610}
6611
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006612static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6613 unsigned long *db)
6614{
6615 u32 dr6 = 0;
6616 int i;
6617 u32 enable, rwlen;
6618
6619 enable = dr7;
6620 rwlen = dr7 >> 16;
6621 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6622 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6623 dr6 |= (1 << i);
6624 return dr6;
6625}
6626
Sean Christopherson120c2c42019-08-27 14:40:29 -07006627static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006628{
6629 struct kvm_run *kvm_run = vcpu->run;
6630
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006631 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6632 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6633 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6634 kvm_run->debug.arch.exception = DB_VECTOR;
6635 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006636 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006637 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006638 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006639 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006640}
6641
Kyle Huey6affcbe2016-11-29 12:40:40 -08006642int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6643{
6644 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006645 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006646
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006647 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006648 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006649 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006650
6651 /*
6652 * rflags is the old, "raw" value of the flags. The new value has
6653 * not been saved yet.
6654 *
6655 * This is correct even for TF set by the guest, because "the
6656 * processor will not generate this exception after the instruction
6657 * that sets the TF flag".
6658 */
6659 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006660 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006661 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006662}
6663EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6664
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006665static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6666{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006667 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6668 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006669 struct kvm_run *kvm_run = vcpu->run;
6670 unsigned long eip = kvm_get_linear_rip(vcpu);
6671 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006672 vcpu->arch.guest_debug_dr7,
6673 vcpu->arch.eff_db);
6674
6675 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006676 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006677 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006678 kvm_run->debug.arch.exception = DB_VECTOR;
6679 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006680 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006681 return true;
6682 }
6683 }
6684
Nadav Amit4161a562014-07-17 01:19:31 +03006685 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6686 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006687 unsigned long eip = kvm_get_linear_rip(vcpu);
6688 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006689 vcpu->arch.dr7,
6690 vcpu->arch.db);
6691
6692 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006693 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006694 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006695 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006696 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006697 return true;
6698 }
6699 }
6700
6701 return false;
6702}
6703
Liran Alon04789b62018-03-12 13:12:50 +02006704static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6705{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006706 switch (ctxt->opcode_len) {
6707 case 1:
6708 switch (ctxt->b) {
6709 case 0xe4: /* IN */
6710 case 0xe5:
6711 case 0xec:
6712 case 0xed:
6713 case 0xe6: /* OUT */
6714 case 0xe7:
6715 case 0xee:
6716 case 0xef:
6717 case 0x6c: /* INS */
6718 case 0x6d:
6719 case 0x6e: /* OUTS */
6720 case 0x6f:
6721 return true;
6722 }
6723 break;
6724 case 2:
6725 switch (ctxt->b) {
6726 case 0x33: /* RDPMC */
6727 return true;
6728 }
6729 break;
Liran Alon04789b62018-03-12 13:12:50 +02006730 }
6731
6732 return false;
6733}
6734
Sean Christopherson736c2912019-12-06 15:57:14 -08006735int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6736 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006737{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006738 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006739 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006740 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006741 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006742
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006743 vcpu->arch.l1tf_flush_l1d = true;
6744
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006745 /*
6746 * Clear write_fault_to_shadow_pgtable here to ensure it is
6747 * never reused.
6748 */
6749 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006750 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006751
Sheng Yang571008d2008-01-02 14:49:22 +08006752 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006753 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006754
6755 /*
6756 * We will reenter on the same instruction since
6757 * we do not set complete_userspace_io. This does not
6758 * handle watchpoints yet, those would be handled in
6759 * the emulate_ops.
6760 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006761 if (!(emulation_type & EMULTYPE_SKIP) &&
6762 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006763 return r;
6764
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006765 ctxt->interruptibility = 0;
6766 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006767 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006768 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006769
Borislav Petkovb51e9742013-09-22 16:44:52 +02006770 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006771
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006772 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006773
Avi Kivitye46479f2010-04-11 13:05:16 +03006774 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006775 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006776 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006777 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006778 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6779 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006780 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006781 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006782 if (reexecute_instruction(vcpu, cr2_or_gpa,
6783 write_fault_to_spt,
6784 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006785 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006786 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006787 /*
6788 * #UD should result in just EMULATION_FAILED, and trap-like
6789 * exception should not be encountered during decode.
6790 */
6791 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6792 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006793 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006794 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006795 }
Liran Alone2366172018-03-12 13:12:49 +02006796 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006797 }
6798 }
6799
Sean Christopherson42cbf062019-08-27 14:40:31 -07006800 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6801 !is_vmware_backdoor_opcode(ctxt)) {
6802 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006803 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006804 }
Liran Alon04789b62018-03-12 13:12:50 +02006805
Sean Christopherson1957aa62019-08-27 14:40:39 -07006806 /*
6807 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6808 * for kvm_skip_emulated_instruction(). The caller is responsible for
6809 * updating interruptibility state and injecting single-step #DBs.
6810 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006811 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006812 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006813 if (ctxt->eflags & X86_EFLAGS_RF)
6814 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006815 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006816 }
6817
Sean Christopherson736c2912019-12-06 15:57:14 -08006818 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006819 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006820
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006821 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006822 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006823 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6824 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006825 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006826 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006827
Gleb Natapov5cd21912010-03-18 15:20:26 +02006828restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006829 if (emulation_type & EMULTYPE_PF) {
6830 /* Save the faulting GPA (cr2) in the address field */
6831 ctxt->exception.address = cr2_or_gpa;
6832
6833 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6834 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006835 ctxt->gpa_available = true;
6836 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006837 }
6838 } else {
6839 /* Sanitize the address out of an abundance of paranoia. */
6840 ctxt->exception.address = 0;
6841 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006842
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006843 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006844
Joerg Roedel775fde82011-04-04 12:39:24 +02006845 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006846 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006847
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006848 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006849 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006850 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006851 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006852
Liran Alone2366172018-03-12 13:12:49 +02006853 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006854 }
6855
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006856 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006857 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006858 if (inject_emulated_exception(vcpu))
6859 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006860 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006861 if (!vcpu->arch.pio.in) {
6862 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006863 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006864 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006865 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006866 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6867 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006868 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006869 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006870 ++vcpu->stat.mmio_exits;
6871
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006872 if (!vcpu->mmio_is_write)
6873 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006874 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006875 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006876 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006877 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006878 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006879 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006880
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006881 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006882 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006883 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006884 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006885 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006886 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6887 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006888 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006889 r = kvm_vcpu_do_singlestep(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006890 if (kvm_x86_ops->update_emulated_instruction)
6891 kvm_x86_ops->update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006892 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006893 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006894
6895 /*
6896 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6897 * do nothing, and it will be requested again as soon as
6898 * the shadow expires. But we still need to check here,
6899 * because POPF has no interrupt shadow.
6900 */
6901 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6902 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006903 } else
6904 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006905
Gleb Natapove85d28f2010-07-29 15:11:52 +03006906 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006907}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006908
6909int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6910{
6911 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6912}
6913EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6914
6915int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6916 void *insn, int insn_len)
6917{
6918 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6919}
6920EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006921
Sean Christopherson8764ed52019-04-29 07:04:15 -07006922static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6923{
6924 vcpu->arch.pio.count = 0;
6925 return 1;
6926}
6927
Sean Christopherson45def772019-03-11 20:01:05 -07006928static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6929{
6930 vcpu->arch.pio.count = 0;
6931
6932 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6933 return 1;
6934
6935 return kvm_skip_emulated_instruction(vcpu);
6936}
6937
Sean Christophersondca7f122018-03-08 08:57:27 -08006938static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6939 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006940{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006941 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006942 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6943
Sean Christopherson8764ed52019-04-29 07:04:15 -07006944 if (ret)
6945 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006946
Sean Christopherson8764ed52019-04-29 07:04:15 -07006947 /*
6948 * Workaround userspace that relies on old KVM behavior of %rip being
6949 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6950 */
6951 if (port == 0x7e &&
6952 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6953 vcpu->arch.complete_userspace_io =
6954 complete_fast_pio_out_port_0x7e;
6955 kvm_skip_emulated_instruction(vcpu);
6956 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006957 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6958 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6959 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006960 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006961}
Carsten Ottede7d7892007-10-30 18:44:25 +01006962
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006963static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6964{
6965 unsigned long val;
6966
6967 /* We should only ever be called with arch.pio.count equal to 1 */
6968 BUG_ON(vcpu->arch.pio.count != 1);
6969
Sean Christopherson45def772019-03-11 20:01:05 -07006970 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6971 vcpu->arch.pio.count = 0;
6972 return 1;
6973 }
6974
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006975 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006976 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006977
6978 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006979 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006980 * the copy and tracing
6981 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006982 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006983 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006984
Sean Christopherson45def772019-03-11 20:01:05 -07006985 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006986}
6987
Sean Christophersondca7f122018-03-08 08:57:27 -08006988static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6989 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006990{
6991 unsigned long val;
6992 int ret;
6993
6994 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006995 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006996
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006997 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006998 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07006999 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007000 return ret;
7001 }
7002
Sean Christopherson45def772019-03-11 20:01:05 -07007003 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007004 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7005
7006 return 0;
7007}
Sean Christophersondca7f122018-03-08 08:57:27 -08007008
7009int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7010{
Sean Christopherson45def772019-03-11 20:01:05 -07007011 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007012
Sean Christophersondca7f122018-03-08 08:57:27 -08007013 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007014 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007015 else
Sean Christopherson45def772019-03-11 20:01:05 -07007016 ret = kvm_fast_pio_out(vcpu, size, port);
7017 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007018}
7019EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007020
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007021static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007022{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007023 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007024 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007025}
7026
7027static void tsc_khz_changed(void *data)
7028{
7029 struct cpufreq_freqs *freq = data;
7030 unsigned long khz = 0;
7031
7032 if (data)
7033 khz = freq->new;
7034 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7035 khz = cpufreq_quick_get(raw_smp_processor_id());
7036 if (!khz)
7037 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007038 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007039}
7040
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007041#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007042static void kvm_hyperv_tsc_notifier(void)
7043{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007044 struct kvm *kvm;
7045 struct kvm_vcpu *vcpu;
7046 int cpu;
7047
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007048 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007049 list_for_each_entry(kvm, &vm_list, vm_list)
7050 kvm_make_mclock_inprogress_request(kvm);
7051
7052 hyperv_stop_tsc_emulation();
7053
7054 /* TSC frequency always matches when on Hyper-V */
7055 for_each_present_cpu(cpu)
7056 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7057 kvm_max_guest_tsc_khz = tsc_khz;
7058
7059 list_for_each_entry(kvm, &vm_list, vm_list) {
7060 struct kvm_arch *ka = &kvm->arch;
7061
7062 spin_lock(&ka->pvclock_gtod_sync_lock);
7063
7064 pvclock_update_vm_gtod_copy(kvm);
7065
7066 kvm_for_each_vcpu(cpu, vcpu, kvm)
7067 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7068
7069 kvm_for_each_vcpu(cpu, vcpu, kvm)
7070 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7071
7072 spin_unlock(&ka->pvclock_gtod_sync_lock);
7073 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007074 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007075}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007076#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007077
Viresh Kumardf240142019-04-29 15:03:58 +05307078static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007079{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007080 struct kvm *kvm;
7081 struct kvm_vcpu *vcpu;
7082 int i, send_ipi = 0;
7083
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007084 /*
7085 * We allow guests to temporarily run on slowing clocks,
7086 * provided we notify them after, or to run on accelerating
7087 * clocks, provided we notify them before. Thus time never
7088 * goes backwards.
7089 *
7090 * However, we have a problem. We can't atomically update
7091 * the frequency of a given CPU from this function; it is
7092 * merely a notifier, which can be called from any CPU.
7093 * Changing the TSC frequency at arbitrary points in time
7094 * requires a recomputation of local variables related to
7095 * the TSC for each VCPU. We must flag these local variables
7096 * to be updated and be sure the update takes place with the
7097 * new frequency before any guests proceed.
7098 *
7099 * Unfortunately, the combination of hotplug CPU and frequency
7100 * change creates an intractable locking scenario; the order
7101 * of when these callouts happen is undefined with respect to
7102 * CPU hotplug, and they can race with each other. As such,
7103 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7104 * undefined; you can actually have a CPU frequency change take
7105 * place in between the computation of X and the setting of the
7106 * variable. To protect against this problem, all updates of
7107 * the per_cpu tsc_khz variable are done in an interrupt
7108 * protected IPI, and all callers wishing to update the value
7109 * must wait for a synchronous IPI to complete (which is trivial
7110 * if the caller is on the CPU already). This establishes the
7111 * necessary total order on variable updates.
7112 *
7113 * Note that because a guest time update may take place
7114 * anytime after the setting of the VCPU's request bit, the
7115 * correct TSC value must be set before the request. However,
7116 * to ensure the update actually makes it to any guest which
7117 * starts running in hardware virtualization between the set
7118 * and the acquisition of the spinlock, we must also ping the
7119 * CPU after setting the request bit.
7120 *
7121 */
7122
Viresh Kumardf240142019-04-29 15:03:58 +05307123 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007124
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007125 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007126 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007127 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307128 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007129 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007130 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007131 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007132 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007133 }
7134 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007135 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007136
7137 if (freq->old < freq->new && send_ipi) {
7138 /*
7139 * We upscale the frequency. Must make the guest
7140 * doesn't see old kvmclock values while running with
7141 * the new frequency, otherwise we risk the guest sees
7142 * time go backwards.
7143 *
7144 * In case we update the frequency for another cpu
7145 * (which might be in guest context) send an interrupt
7146 * to kick the cpu out of guest context. Next time
7147 * guest context is entered kvmclock will be updated,
7148 * so the guest will not see stale values.
7149 */
Viresh Kumardf240142019-04-29 15:03:58 +05307150 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007151 }
Viresh Kumardf240142019-04-29 15:03:58 +05307152}
7153
7154static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7155 void *data)
7156{
7157 struct cpufreq_freqs *freq = data;
7158 int cpu;
7159
7160 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7161 return 0;
7162 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7163 return 0;
7164
7165 for_each_cpu(cpu, freq->policy->cpus)
7166 __kvmclock_cpufreq_notifier(freq, cpu);
7167
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007168 return 0;
7169}
7170
7171static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007172 .notifier_call = kvmclock_cpufreq_notifier
7173};
7174
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007175static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007176{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007177 tsc_khz_changed(NULL);
7178 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007179}
7180
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007181static void kvm_timer_init(void)
7182{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007183 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307184
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007185 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007186#ifdef CONFIG_CPU_FREQ
7187 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007188 int cpu;
7189
Zachary Amsdenc2855452010-09-18 14:38:15 -10007190 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02007191 cpu = get_cpu();
7192 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007193 if (policy.cpuinfo.max_freq)
7194 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007195 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007196#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007197 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7198 CPUFREQ_TRANSITION_NOTIFIER);
7199 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307200
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007201 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007202 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007203}
7204
Andi Kleendd60d212017-07-25 17:20:32 -07007205DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7206EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007207
Gleb Natapovf5132b02011-11-10 14:57:22 +02007208int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007209{
Alex,Shi086c9852011-10-20 15:34:01 +08007210 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007211}
7212
7213static int kvm_is_user_mode(void)
7214{
7215 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007216
Alex,Shi086c9852011-10-20 15:34:01 +08007217 if (__this_cpu_read(current_vcpu))
7218 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007219
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007220 return user_mode != 0;
7221}
7222
7223static unsigned long kvm_get_guest_ip(void)
7224{
7225 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007226
Alex,Shi086c9852011-10-20 15:34:01 +08007227 if (__this_cpu_read(current_vcpu))
7228 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007229
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007230 return ip;
7231}
7232
Luwei Kang8479e042019-02-18 19:26:07 -05007233static void kvm_handle_intel_pt_intr(void)
7234{
7235 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7236
7237 kvm_make_request(KVM_REQ_PMI, vcpu);
7238 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7239 (unsigned long *)&vcpu->arch.pmu.global_status);
7240}
7241
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007242static struct perf_guest_info_callbacks kvm_guest_cbs = {
7243 .is_in_guest = kvm_is_in_guest,
7244 .is_user_mode = kvm_is_user_mode,
7245 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007246 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007247};
7248
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007249#ifdef CONFIG_X86_64
7250static void pvclock_gtod_update_fn(struct work_struct *work)
7251{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007252 struct kvm *kvm;
7253
7254 struct kvm_vcpu *vcpu;
7255 int i;
7256
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007257 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007258 list_for_each_entry(kvm, &vm_list, vm_list)
7259 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007260 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007261 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007262 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007263}
7264
7265static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7266
7267/*
7268 * Notification about pvclock gtod data update.
7269 */
7270static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7271 void *priv)
7272{
7273 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7274 struct timekeeper *tk = priv;
7275
7276 update_pvclock_gtod(tk);
7277
7278 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007279 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007280 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007281 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007282 atomic_read(&kvm_guest_has_master_clock) != 0)
7283 queue_work(system_long_wq, &pvclock_gtod_work);
7284
7285 return 0;
7286}
7287
7288static struct notifier_block pvclock_gtod_notifier = {
7289 .notifier_call = pvclock_gtod_notify,
7290};
7291#endif
7292
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007293int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007294{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007295 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007296 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007297
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007298 if (kvm_x86_ops) {
7299 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007300 r = -EEXIST;
7301 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007302 }
7303
7304 if (!ops->cpu_has_kvm_support()) {
7305 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007306 r = -EOPNOTSUPP;
7307 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007308 }
7309 if (ops->disabled_by_bios()) {
7310 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007311 r = -EOPNOTSUPP;
7312 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007313 }
7314
Marc Orrb666a4b2018-11-06 14:53:56 -08007315 /*
7316 * KVM explicitly assumes that the guest has an FPU and
7317 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7318 * vCPU's FPU state as a fxregs_state struct.
7319 */
7320 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7321 printk(KERN_ERR "kvm: inadequate fpu\n");
7322 r = -EOPNOTSUPP;
7323 goto out;
7324 }
7325
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007326 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007327 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007328 __alignof__(struct fpu), SLAB_ACCOUNT,
7329 NULL);
7330 if (!x86_fpu_cache) {
7331 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7332 goto out;
7333 }
7334
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007335 x86_emulator_cache = kvm_alloc_emulator_cache();
7336 if (!x86_emulator_cache) {
7337 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7338 goto out_free_x86_fpu_cache;
7339 }
7340
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007341 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7342 if (!shared_msrs) {
7343 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007344 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007345 }
7346
Avi Kivity97db56c2008-01-13 13:23:56 +02007347 r = kvm_mmu_module_init();
7348 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007349 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007350
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007351 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007352
Sheng Yang7b523452008-04-25 21:13:50 +08007353 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007354 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007355 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007356 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007357
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007358 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7359
Sean Christophersoncfc48182020-03-02 15:56:23 -08007360 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007361 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007362 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7363 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007364
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007365 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007366 if (pi_inject_timer == -1)
7367 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007368#ifdef CONFIG_X86_64
7369 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007370
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007371 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007372 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007373#endif
7374
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007375 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007376
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007377out_free_percpu:
7378 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007379out_free_x86_emulator_cache:
7380 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007381out_free_x86_fpu_cache:
7382 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007383out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007384 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007385}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007386
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007387void kvm_arch_exit(void)
7388{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007389#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007390 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007391 clear_hv_tscchange_cb();
7392#endif
David Matlackcef84c32016-12-16 14:30:36 -08007393 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007394 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7395
Jan Kiszka888d2562009-04-17 19:24:58 +02007396 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7397 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7398 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007399 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007400#ifdef CONFIG_X86_64
7401 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7402#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007403 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007404 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007405 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007406 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007407}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007408
Joel Schopp5cb56052015-03-02 13:43:31 -06007409int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007410{
7411 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007412 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007413 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007414 return 1;
7415 } else {
7416 vcpu->run->exit_reason = KVM_EXIT_HLT;
7417 return 0;
7418 }
7419}
Joel Schopp5cb56052015-03-02 13:43:31 -06007420EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7421
7422int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7423{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007424 int ret = kvm_skip_emulated_instruction(vcpu);
7425 /*
7426 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7427 * KVM_EXIT_DEBUG here.
7428 */
7429 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007430}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007431EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7432
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007433#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007434static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7435 unsigned long clock_type)
7436{
7437 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007438 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007439 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007440 int ret;
7441
7442 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7443 return -KVM_EOPNOTSUPP;
7444
7445 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7446 return -KVM_EOPNOTSUPP;
7447
7448 clock_pairing.sec = ts.tv_sec;
7449 clock_pairing.nsec = ts.tv_nsec;
7450 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7451 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007452 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007453
7454 ret = 0;
7455 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7456 sizeof(struct kvm_clock_pairing)))
7457 ret = -KVM_EFAULT;
7458
7459 return ret;
7460}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007461#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007462
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307463/*
7464 * kvm_pv_kick_cpu_op: Kick a vcpu.
7465 *
7466 * @apicid - apicid of vcpu to be kicked.
7467 */
7468static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7469{
Raghavendra K T24d21662013-08-26 14:18:35 +05307470 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307471
Peter Xu150a84f2019-12-04 20:07:21 +01007472 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007473 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007474 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307475 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007476 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307477
Raghavendra K T24d21662013-08-26 14:18:35 +05307478 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007479 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307480}
7481
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007482bool kvm_apicv_activated(struct kvm *kvm)
7483{
7484 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7485}
7486EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7487
7488void kvm_apicv_init(struct kvm *kvm, bool enable)
7489{
7490 if (enable)
7491 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7492 &kvm->arch.apicv_inhibit_reasons);
7493 else
7494 set_bit(APICV_INHIBIT_REASON_DISABLE,
7495 &kvm->arch.apicv_inhibit_reasons);
7496}
7497EXPORT_SYMBOL_GPL(kvm_apicv_init);
7498
Wanpeng Li71506292019-06-11 20:23:49 +08007499static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7500{
7501 struct kvm_vcpu *target = NULL;
7502 struct kvm_apic_map *map;
7503
7504 rcu_read_lock();
7505 map = rcu_dereference(kvm->arch.apic_map);
7506
7507 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7508 target = map->phys_map[dest_id]->vcpu;
7509
7510 rcu_read_unlock();
7511
Wanpeng Li266e85a2019-07-24 17:43:13 +08007512 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007513 kvm_vcpu_yield_to(target);
7514}
7515
Hollis Blanchard8776e512007-10-31 17:24:24 -05007516int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7517{
7518 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007519 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007520
Radim Krčmář696ca772018-05-24 17:50:56 +02007521 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7522 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007523
Sean Christophersonde3cd112019-04-30 10:36:17 -07007524 nr = kvm_rax_read(vcpu);
7525 a0 = kvm_rbx_read(vcpu);
7526 a1 = kvm_rcx_read(vcpu);
7527 a2 = kvm_rdx_read(vcpu);
7528 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007529
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007530 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007531
Nadav Amita449c7a2014-06-18 17:19:24 +03007532 op_64_bit = is_64_bit_mode(vcpu);
7533 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007534 nr &= 0xFFFFFFFF;
7535 a0 &= 0xFFFFFFFF;
7536 a1 &= 0xFFFFFFFF;
7537 a2 &= 0xFFFFFFFF;
7538 a3 &= 0xFFFFFFFF;
7539 }
7540
Jan Kiszka07708c42009-08-03 18:43:28 +02007541 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7542 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007543 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007544 }
7545
Hollis Blanchard8776e512007-10-31 17:24:24 -05007546 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007547 case KVM_HC_VAPIC_POLL_IRQ:
7548 ret = 0;
7549 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307550 case KVM_HC_KICK_CPU:
7551 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007552 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307553 ret = 0;
7554 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007555#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007556 case KVM_HC_CLOCK_PAIRING:
7557 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7558 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007559#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007560 case KVM_HC_SEND_IPI:
7561 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7562 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007563 case KVM_HC_SCHED_YIELD:
7564 kvm_sched_yield(vcpu->kvm, a0);
7565 ret = 0;
7566 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007567 default:
7568 ret = -KVM_ENOSYS;
7569 break;
7570 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007571out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007572 if (!op_64_bit)
7573 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007574 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007575
Amit Shahf11c3a82008-02-21 01:00:30 +05307576 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007577 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007578}
7579EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7580
Jan Kiszkab6785de2012-09-20 07:43:17 +02007581static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007582{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007583 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007584 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007585 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007586
Hollis Blanchard8776e512007-10-31 17:24:24 -05007587 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007588
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007589 return emulator_write_emulated(ctxt, rip, instruction, 3,
7590 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007591}
7592
Avi Kivity851ba692009-08-24 11:10:17 +03007593static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007594{
Matt Gingell782d4222015-11-16 15:26:00 -08007595 return vcpu->run->request_interrupt_window &&
7596 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007597}
7598
Avi Kivity851ba692009-08-24 11:10:17 +03007599static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007600{
Avi Kivity851ba692009-08-24 11:10:17 +03007601 struct kvm_run *kvm_run = vcpu->run;
7602
Jan Kiszka91586a32009-10-05 13:07:21 +02007603 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007604 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007605 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007606 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007607 kvm_run->ready_for_interrupt_injection =
7608 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007609 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007610}
7611
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007612static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7613{
7614 int max_irr, tpr;
7615
7616 if (!kvm_x86_ops->update_cr8_intercept)
7617 return;
7618
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007619 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007620 return;
7621
Andrey Smetanind62caab2015-11-10 15:36:33 +03007622 if (vcpu->arch.apicv_active)
7623 return;
7624
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007625 if (!vcpu->arch.apic->vapic_addr)
7626 max_irr = kvm_lapic_find_highest_irr(vcpu);
7627 else
7628 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007629
7630 if (max_irr != -1)
7631 max_irr >>= 4;
7632
7633 tpr = kvm_lapic_get_cr8(vcpu);
7634
7635 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7636}
7637
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007638static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007639{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007640 int r;
7641
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007642 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007643
Liran Alon1a680e32018-03-23 03:01:33 +03007644 if (vcpu->arch.exception.injected)
7645 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007646 /*
Liran Alona042c262018-03-23 03:01:32 +03007647 * Do not inject an NMI or interrupt if there is a pending
7648 * exception. Exceptions and interrupts are recognized at
7649 * instruction boundaries, i.e. the start of an instruction.
7650 * Trap-like exceptions, e.g. #DB, have higher priority than
7651 * NMIs and interrupts, i.e. traps are recognized before an
7652 * NMI/interrupt that's pending on the same instruction.
7653 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7654 * priority, but are only generated (pended) during instruction
7655 * execution, i.e. a pending fault-like exception means the
7656 * fault occurred on the *previous* instruction and must be
7657 * serviced prior to recognizing any new events in order to
7658 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007659 */
Liran Alon1a680e32018-03-23 03:01:33 +03007660 else if (!vcpu->arch.exception.pending) {
7661 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007662 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007663 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007664 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007665 }
7666
Liran Alon1a680e32018-03-23 03:01:33 +03007667 /*
7668 * Call check_nested_events() even if we reinjected a previous event
7669 * in order for caller to determine if it should require immediate-exit
7670 * from L2 to L1 due to pending L1 events which require exit
7671 * from L2 to L1.
7672 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007673 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007674 r = kvm_x86_ops->check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007675 if (r != 0)
7676 return r;
7677 }
7678
7679 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007680 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007681 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7682 vcpu->arch.exception.has_error_code,
7683 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007684
Liran Alon1a680e32018-03-23 03:01:33 +03007685 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007686 vcpu->arch.exception.pending = false;
7687 vcpu->arch.exception.injected = true;
7688
Nadav Amitd6e8c852014-07-24 14:51:24 +03007689 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7690 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7691 X86_EFLAGS_RF);
7692
Jim Mattsonf10c7292018-10-16 14:29:23 -07007693 if (vcpu->arch.exception.nr == DB_VECTOR) {
7694 /*
7695 * This code assumes that nSVM doesn't use
7696 * check_nested_events(). If it does, the
7697 * DR6/DR7 changes should happen before L1
7698 * gets a #VMEXIT for an intercepted #DB in
7699 * L2. (Under VMX, on the other hand, the
7700 * DR6/DR7 changes should not happen in the
7701 * event of a VM-exit to L1 for an intercepted
7702 * #DB in L2.)
7703 */
7704 kvm_deliver_exception_payload(vcpu);
7705 if (vcpu->arch.dr7 & DR7_GD) {
7706 vcpu->arch.dr7 &= ~DR7_GD;
7707 kvm_update_dr7(vcpu);
7708 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007709 }
7710
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007711 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007712 }
7713
7714 /* Don't consider new event if we re-injected an event */
7715 if (kvm_event_needs_reinjection(vcpu))
7716 return 0;
7717
7718 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7719 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007720 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007721 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007722 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007723 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007724 --vcpu->arch.nmi_pending;
7725 vcpu->arch.nmi_injected = true;
7726 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007727 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007728 /*
7729 * Because interrupts can be injected asynchronously, we are
7730 * calling check_nested_events again here to avoid a race condition.
7731 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7732 * proposal and current concerns. Perhaps we should be setting
7733 * KVM_REQ_EVENT only on certain events and not unconditionally?
7734 */
7735 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007736 r = kvm_x86_ops->check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007737 if (r != 0)
7738 return r;
7739 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007740 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007741 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7742 false);
7743 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007744 }
7745 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007746
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007747 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007748}
7749
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007750static void process_nmi(struct kvm_vcpu *vcpu)
7751{
7752 unsigned limit = 2;
7753
7754 /*
7755 * x86 is limited to one NMI running, and one NMI pending after it.
7756 * If an NMI is already in progress, limit further NMIs to just one.
7757 * Otherwise, allow two (and we'll inject the first one immediately).
7758 */
7759 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7760 limit = 1;
7761
7762 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7763 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7764 kvm_make_request(KVM_REQ_EVENT, vcpu);
7765}
7766
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007767static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007768{
7769 u32 flags = 0;
7770 flags |= seg->g << 23;
7771 flags |= seg->db << 22;
7772 flags |= seg->l << 21;
7773 flags |= seg->avl << 20;
7774 flags |= seg->present << 15;
7775 flags |= seg->dpl << 13;
7776 flags |= seg->s << 12;
7777 flags |= seg->type << 8;
7778 return flags;
7779}
7780
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007781static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007782{
7783 struct kvm_segment seg;
7784 int offset;
7785
7786 kvm_get_segment(vcpu, &seg, n);
7787 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7788
7789 if (n < 3)
7790 offset = 0x7f84 + n * 12;
7791 else
7792 offset = 0x7f2c + (n - 3) * 12;
7793
7794 put_smstate(u32, buf, offset + 8, seg.base);
7795 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007796 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007797}
7798
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007799#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007800static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007801{
7802 struct kvm_segment seg;
7803 int offset;
7804 u16 flags;
7805
7806 kvm_get_segment(vcpu, &seg, n);
7807 offset = 0x7e00 + n * 16;
7808
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007809 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007810 put_smstate(u16, buf, offset, seg.selector);
7811 put_smstate(u16, buf, offset + 2, flags);
7812 put_smstate(u32, buf, offset + 4, seg.limit);
7813 put_smstate(u64, buf, offset + 8, seg.base);
7814}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007815#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007816
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007817static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007818{
7819 struct desc_ptr dt;
7820 struct kvm_segment seg;
7821 unsigned long val;
7822 int i;
7823
7824 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7825 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7826 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7827 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7828
7829 for (i = 0; i < 8; i++)
7830 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7831
7832 kvm_get_dr(vcpu, 6, &val);
7833 put_smstate(u32, buf, 0x7fcc, (u32)val);
7834 kvm_get_dr(vcpu, 7, &val);
7835 put_smstate(u32, buf, 0x7fc8, (u32)val);
7836
7837 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7838 put_smstate(u32, buf, 0x7fc4, seg.selector);
7839 put_smstate(u32, buf, 0x7f64, seg.base);
7840 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007841 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007842
7843 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7844 put_smstate(u32, buf, 0x7fc0, seg.selector);
7845 put_smstate(u32, buf, 0x7f80, seg.base);
7846 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007847 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007848
7849 kvm_x86_ops->get_gdt(vcpu, &dt);
7850 put_smstate(u32, buf, 0x7f74, dt.address);
7851 put_smstate(u32, buf, 0x7f70, dt.size);
7852
7853 kvm_x86_ops->get_idt(vcpu, &dt);
7854 put_smstate(u32, buf, 0x7f58, dt.address);
7855 put_smstate(u32, buf, 0x7f54, dt.size);
7856
7857 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007858 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007859
7860 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7861
7862 /* revision id */
7863 put_smstate(u32, buf, 0x7efc, 0x00020000);
7864 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7865}
7866
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007867#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007868static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007869{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007870 struct desc_ptr dt;
7871 struct kvm_segment seg;
7872 unsigned long val;
7873 int i;
7874
7875 for (i = 0; i < 16; i++)
7876 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7877
7878 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7879 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7880
7881 kvm_get_dr(vcpu, 6, &val);
7882 put_smstate(u64, buf, 0x7f68, val);
7883 kvm_get_dr(vcpu, 7, &val);
7884 put_smstate(u64, buf, 0x7f60, val);
7885
7886 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7887 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7888 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7889
7890 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7891
7892 /* revision id */
7893 put_smstate(u32, buf, 0x7efc, 0x00020064);
7894
7895 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7896
7897 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7898 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007899 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007900 put_smstate(u32, buf, 0x7e94, seg.limit);
7901 put_smstate(u64, buf, 0x7e98, seg.base);
7902
7903 kvm_x86_ops->get_idt(vcpu, &dt);
7904 put_smstate(u32, buf, 0x7e84, dt.size);
7905 put_smstate(u64, buf, 0x7e88, dt.address);
7906
7907 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7908 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007909 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007910 put_smstate(u32, buf, 0x7e74, seg.limit);
7911 put_smstate(u64, buf, 0x7e78, seg.base);
7912
7913 kvm_x86_ops->get_gdt(vcpu, &dt);
7914 put_smstate(u32, buf, 0x7e64, dt.size);
7915 put_smstate(u64, buf, 0x7e68, dt.address);
7916
7917 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007918 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007919}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007920#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007921
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007922static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007923{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007924 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007925 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007926 char buf[512];
7927 u32 cr0;
7928
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007929 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007930 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007931#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007932 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007933 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007934 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007935#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007936 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007937
Ladi Prosek0234bf82017-10-11 16:54:40 +02007938 /*
7939 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7940 * vCPU state (e.g. leave guest mode) after we've saved the state into
7941 * the SMM state-save area.
7942 */
7943 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7944
7945 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007946 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007947
7948 if (kvm_x86_ops->get_nmi_mask(vcpu))
7949 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7950 else
7951 kvm_x86_ops->set_nmi_mask(vcpu, true);
7952
7953 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7954 kvm_rip_write(vcpu, 0x8000);
7955
7956 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7957 kvm_x86_ops->set_cr0(vcpu, cr0);
7958 vcpu->arch.cr0 = cr0;
7959
7960 kvm_x86_ops->set_cr4(vcpu, 0);
7961
Paolo Bonzini18c36262015-08-07 12:27:54 +02007962 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7963 dt.address = dt.size = 0;
7964 kvm_x86_ops->set_idt(vcpu, &dt);
7965
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007966 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7967
7968 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7969 cs.base = vcpu->arch.smbase;
7970
7971 ds.selector = 0;
7972 ds.base = 0;
7973
7974 cs.limit = ds.limit = 0xffffffff;
7975 cs.type = ds.type = 0x3;
7976 cs.dpl = ds.dpl = 0;
7977 cs.db = ds.db = 0;
7978 cs.s = ds.s = 1;
7979 cs.l = ds.l = 0;
7980 cs.g = ds.g = 1;
7981 cs.avl = ds.avl = 0;
7982 cs.present = ds.present = 1;
7983 cs.unusable = ds.unusable = 0;
7984 cs.padding = ds.padding = 0;
7985
7986 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7987 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7988 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7989 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7990 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7991 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7992
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007993#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007994 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007995 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007996#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007997
7998 kvm_update_cpuid(vcpu);
7999 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008000}
8001
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008002static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008003{
8004 vcpu->arch.smi_pending = true;
8005 kvm_make_request(KVM_REQ_EVENT, vcpu);
8006}
8007
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008008void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8009 unsigned long *vcpu_bitmap)
8010{
8011 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008012
8013 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8014
Mao Wenandb5a95e2019-11-19 11:06:40 +08008015 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
8016 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008017
8018 free_cpumask_var(cpus);
8019}
8020
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008021void kvm_make_scan_ioapic_request(struct kvm *kvm)
8022{
8023 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8024}
8025
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008026void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8027{
8028 if (!lapic_in_kernel(vcpu))
8029 return;
8030
8031 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8032 kvm_apic_update_apicv(vcpu);
8033 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
8034}
8035EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8036
8037/*
8038 * NOTE: Do not hold any lock prior to calling this.
8039 *
8040 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8041 * locked, because it calls __x86_set_memory_region() which does
8042 * synchronize_srcu(&kvm->srcu).
8043 */
8044void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8045{
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008046 if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
8047 !kvm_x86_ops->check_apicv_inhibit_reasons(bit))
8048 return;
8049
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008050 if (activate) {
8051 if (!test_and_clear_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
8052 !kvm_apicv_activated(kvm))
8053 return;
8054 } else {
8055 if (test_and_set_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
8056 kvm_apicv_activated(kvm))
8057 return;
8058 }
8059
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008060 trace_kvm_apicv_update_request(activate, bit);
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06008061 if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
8062 kvm_x86_ops->pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008063 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
8064}
8065EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8066
Yang Zhang3d81bc72013-04-11 19:25:13 +08008067static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008068{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008069 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008070 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008071
Andrey Smetanin63086302015-11-10 15:36:32 +03008072 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008073
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008074 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008075 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008076 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008077 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03008078 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008079 if (ioapic_in_kernel(vcpu->kvm))
8080 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008081 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008082
8083 if (is_guest_mode(vcpu))
8084 vcpu->arch.load_eoi_exitmap_pending = true;
8085 else
8086 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8087}
8088
8089static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8090{
8091 u64 eoi_exit_bitmap[4];
8092
8093 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8094 return;
8095
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008096 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8097 vcpu_to_synic(vcpu)->vec_bitmap, 256);
8098 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008099}
8100
Michal Hocko93065ac2018-08-21 21:52:33 -07008101int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8102 unsigned long start, unsigned long end,
8103 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008104{
8105 unsigned long apic_address;
8106
8107 /*
8108 * The physical address of apic access page is stored in the VMCS.
8109 * Update it when it becomes invalid.
8110 */
8111 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8112 if (start <= apic_address && apic_address < end)
8113 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008114
8115 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008116}
8117
Tang Chen4256f432014-09-24 15:57:54 +08008118void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8119{
Tang Chenc24ae0d2014-09-24 15:57:58 +08008120 struct page *page = NULL;
8121
Paolo Bonzini35754c92015-07-29 12:05:37 +02008122 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008123 return;
8124
Tang Chen4256f432014-09-24 15:57:54 +08008125 if (!kvm_x86_ops->set_apic_access_page_addr)
8126 return;
8127
Tang Chenc24ae0d2014-09-24 15:57:58 +08008128 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02008129 if (is_error_page(page))
8130 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08008131 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
8132
8133 /*
8134 * Do not pin apic access page in memory, the MMU notifier
8135 * will call us again if it is migrated or swapped out.
8136 */
8137 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08008138}
Tang Chen4256f432014-09-24 15:57:54 +08008139
Sean Christophersond264ee02018-08-27 15:21:12 -07008140void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8141{
8142 smp_send_reschedule(vcpu->cpu);
8143}
8144EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8145
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008146/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008147 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008148 * exiting to the userspace. Otherwise, the value will be returned to the
8149 * userspace.
8150 */
Avi Kivity851ba692009-08-24 11:10:17 +03008151static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008152{
8153 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008154 bool req_int_win =
8155 dm_request_for_irq_injection(vcpu) &&
8156 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008157 enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE;
Matt Gingell62a193e2015-11-16 15:26:07 -08008158
Jan Kiszka730dca42013-04-28 10:50:52 +02008159 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008160
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008161 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008162 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8163 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8164 r = 0;
8165 goto out;
8166 }
8167 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008168 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008169 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008170 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008171 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008172 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8173 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d53d2013-05-09 20:21:41 -03008174 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8175 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008176 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8177 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008178 if (unlikely(r))
8179 goto out;
8180 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008181 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008182 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07008183 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
8184 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008185 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008186 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008187 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008188 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008189 r = 0;
8190 goto out;
8191 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008192 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008193 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008194 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008195 r = 0;
8196 goto out;
8197 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008198 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8199 /* Page is swapped out. Do synthetic halt */
8200 vcpu->arch.apf.halted = true;
8201 r = 1;
8202 goto out;
8203 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008204 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8205 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008206 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8207 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008208 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8209 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008210 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008211 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008212 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008213 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008214 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8215 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8216 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008217 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008218 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8219 vcpu->run->eoi.vector =
8220 vcpu->arch.pending_ioapic_eoi;
8221 r = 0;
8222 goto out;
8223 }
8224 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008225 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8226 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008227 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8228 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008229 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8230 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008231 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8232 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8233 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8234 r = 0;
8235 goto out;
8236 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008237 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8238 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8239 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8240 r = 0;
8241 goto out;
8242 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008243 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8244 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8245 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8246 r = 0;
8247 goto out;
8248 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008249
8250 /*
8251 * KVM_REQ_HV_STIMER has to be processed after
8252 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8253 * depend on the guest clock being up-to-date
8254 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008255 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8256 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008257 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8258 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008259 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008260
Avi Kivityb463a6f2010-07-20 15:06:17 +03008261 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008262 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008263 kvm_apic_accept_events(vcpu);
8264 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8265 r = 1;
8266 goto out;
8267 }
8268
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008269 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008270 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008271 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008272 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008273 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008274 * SMIs have three cases:
8275 * 1) They can be nested, and then there is nothing to
8276 * do here because RSM will cause a vmexit anyway.
8277 * 2) There is an ISA-specific reason why SMI cannot be
8278 * injected, and the moment when this changes can be
8279 * intercepted.
8280 * 3) Or the SMI can be pending because
8281 * inject_pending_event has completed the injection
8282 * of an IRQ or NMI from the previous vmexit, and
8283 * then we request an immediate exit to inject the
8284 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008285 */
8286 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008287 if (!kvm_x86_ops->enable_smi_window(vcpu))
8288 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008289 if (vcpu->arch.nmi_pending)
8290 kvm_x86_ops->enable_nmi_window(vcpu);
8291 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8292 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008293 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008294 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008295
Avi Kivity3842d132010-07-27 12:30:24 +03008296 if (kvm_lapic_enabled(vcpu)) {
8297 update_cr8_intercept(vcpu);
8298 kvm_lapic_sync_to_vapic(vcpu);
8299 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008300 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008301
Avi Kivityd8368af2012-05-14 18:07:56 +03008302 r = kvm_mmu_reload(vcpu);
8303 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008304 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008305 }
8306
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008307 preempt_disable();
8308
8309 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008310
8311 /*
8312 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8313 * IPI are then delayed after guest entry, which ensures that they
8314 * result in virtual interrupt delivery.
8315 */
8316 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008317 vcpu->mode = IN_GUEST_MODE;
8318
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008319 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8320
Lan Tianyu0f127d12016-03-13 11:10:29 +08008321 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008322 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008323 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008324 *
Luwei Kang81b01662019-01-31 16:52:02 +08008325 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008326 * pairs with the memory barrier implicit in pi_test_and_set_on
8327 * (see vmx_deliver_posted_interrupt).
8328 *
8329 * 3) This also orders the write to mode from any reads to the page
8330 * tables done while the VCPU is running. Please see the comment
8331 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008332 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008333 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008334
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008335 /*
8336 * This handles the case where a posted interrupt was
8337 * notified with kvm_vcpu_kick.
8338 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008339 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8340 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008341
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008342 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008343 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008344 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008345 smp_wmb();
8346 local_irq_enable();
8347 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008348 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008349 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008350 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008351 }
8352
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008353 if (req_immediate_exit) {
8354 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008355 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008356 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008357
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008358 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008359 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008360
Sean Christopherson2620fe22020-01-17 11:30:51 -08008361 fpregs_assert_state_consistent();
8362 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8363 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008364
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008365 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008366 set_debugreg(0, 7);
8367 set_debugreg(vcpu->arch.eff_db[0], 0);
8368 set_debugreg(vcpu->arch.eff_db[1], 1);
8369 set_debugreg(vcpu->arch.eff_db[2], 2);
8370 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008371 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008372 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008373 }
8374
Avi Kivity851ba692009-08-24 11:10:17 +03008375 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008376
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008377 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008378 * Do this here before restoring debug registers on the host. And
8379 * since we do this before handling the vmexit, a DR access vmexit
8380 * can (a) read the correct value of the debug registers, (b) set
8381 * KVM_DEBUGREG_WONT_EXIT again.
8382 */
8383 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008384 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8385 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008386 kvm_update_dr0123(vcpu);
8387 kvm_update_dr6(vcpu);
8388 kvm_update_dr7(vcpu);
8389 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008390 }
8391
8392 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008393 * If the guest has used debug registers, at least dr7
8394 * will be disabled while returning to the host.
8395 * If we don't have active breakpoints in the host, we don't
8396 * care about the messed up debug address registers. But if
8397 * we have some of them active, restore the old state.
8398 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008399 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008400 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008401
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008402 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008403
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008404 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008405 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008406
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008407 kvm_x86_ops->handle_exit_irqoff(vcpu, &exit_fastpath);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008408
Sean Christophersond7a08882019-07-10 09:07:34 -07008409 /*
8410 * Consume any pending interrupts, including the possible source of
8411 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8412 * An instruction is required after local_irq_enable() to fully unblock
8413 * interrupts on processors that implement an interrupt shadow, the
8414 * stat.exits increment will do nicely.
8415 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008416 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008417 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008418 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008419 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008420 kvm_after_interrupt(vcpu);
8421
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008422 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008423 if (lapic_in_kernel(vcpu)) {
8424 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8425 if (delta != S64_MIN) {
8426 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8427 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8428 }
8429 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008430
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008431 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008432 preempt_enable();
8433
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008434 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008435
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008436 /*
8437 * Profile KVM exit RIPs:
8438 */
8439 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008440 unsigned long rip = kvm_rip_read(vcpu);
8441 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008442 }
8443
Zachary Amsdencc578282012-02-03 15:43:50 -02008444 if (unlikely(vcpu->arch.tsc_always_catchup))
8445 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008446
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008447 if (vcpu->arch.apic_attention)
8448 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008449
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008450 r = kvm_x86_ops->handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008451 return r;
8452
8453cancel_injection:
8454 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008455 if (unlikely(vcpu->arch.apic_attention))
8456 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008457out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008458 return r;
8459}
8460
Paolo Bonzini362c6982015-02-06 12:48:04 +01008461static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8462{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008463 if (!kvm_arch_vcpu_runnable(vcpu) &&
8464 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008465 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8466 kvm_vcpu_block(vcpu);
8467 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008468
8469 if (kvm_x86_ops->post_block)
8470 kvm_x86_ops->post_block(vcpu);
8471
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008472 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8473 return 1;
8474 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008475
Paolo Bonzini362c6982015-02-06 12:48:04 +01008476 kvm_apic_accept_events(vcpu);
8477 switch(vcpu->arch.mp_state) {
8478 case KVM_MP_STATE_HALTED:
8479 vcpu->arch.pv.pv_unhalted = false;
8480 vcpu->arch.mp_state =
8481 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008482 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008483 case KVM_MP_STATE_RUNNABLE:
8484 vcpu->arch.apf.halted = false;
8485 break;
8486 case KVM_MP_STATE_INIT_RECEIVED:
8487 break;
8488 default:
8489 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008490 }
8491 return 1;
8492}
8493
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008494static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8495{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008496 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008497 kvm_x86_ops->check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008498
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008499 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8500 !vcpu->arch.apf.halted);
8501}
8502
Paolo Bonzini362c6982015-02-06 12:48:04 +01008503static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008504{
8505 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008506 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008507
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008508 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008509 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008510
Paolo Bonzini362c6982015-02-06 12:48:04 +01008511 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008512 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008513 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008514 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008515 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008516 }
8517
Gleb Natapov09cec752009-03-23 15:11:44 +02008518 if (r <= 0)
8519 break;
8520
Radim Krčmář72875d82017-04-26 22:32:19 +02008521 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008522 if (kvm_cpu_has_pending_timer(vcpu))
8523 kvm_inject_pending_timer_irqs(vcpu);
8524
Matt Gingell782d4222015-11-16 15:26:00 -08008525 if (dm_request_for_irq_injection(vcpu) &&
8526 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008527 r = 0;
8528 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008529 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008530 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008531 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008532
8533 kvm_check_async_pf_completion(vcpu);
8534
Gleb Natapov09cec752009-03-23 15:11:44 +02008535 if (signal_pending(current)) {
8536 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008537 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008538 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008539 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008540 }
8541 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008542 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008543 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008544 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008545 }
8546 }
8547
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008548 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008549
8550 return r;
8551}
8552
Gleb Natapov716d51a2012-09-03 15:24:26 +03008553static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8554{
8555 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008556
Gleb Natapov716d51a2012-09-03 15:24:26 +03008557 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008558 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008559 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008560 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008561}
8562
8563static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8564{
8565 BUG_ON(!vcpu->arch.pio.count);
8566
8567 return complete_emulated_io(vcpu);
8568}
8569
Avi Kivityf78146b2012-04-18 19:22:47 +03008570/*
8571 * Implements the following, as a state machine:
8572 *
8573 * read:
8574 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008575 * for each mmio piece in the fragment
8576 * write gpa, len
8577 * exit
8578 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008579 * execute insn
8580 *
8581 * write:
8582 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008583 * for each mmio piece in the fragment
8584 * write gpa, len
8585 * copy data
8586 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008587 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008588static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008589{
8590 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008591 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008592 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008593
Gleb Natapov716d51a2012-09-03 15:24:26 +03008594 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008595
Gleb Natapov716d51a2012-09-03 15:24:26 +03008596 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008597 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8598 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008599 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008600 memcpy(frag->data, run->mmio.data, len);
8601
8602 if (frag->len <= 8) {
8603 /* Switch to the next fragment. */
8604 frag++;
8605 vcpu->mmio_cur_fragment++;
8606 } else {
8607 /* Go forward to the next mmio piece. */
8608 frag->data += len;
8609 frag->gpa += len;
8610 frag->len -= len;
8611 }
8612
Andrew Honiga08d3b32014-02-27 19:35:14 +01008613 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008614 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008615
8616 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008617 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008618 return 1;
8619 vcpu->mmio_read_completed = 1;
8620 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008621 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008622
Gleb Natapov716d51a2012-09-03 15:24:26 +03008623 run->exit_reason = KVM_EXIT_MMIO;
8624 run->mmio.phys_addr = frag->gpa;
8625 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008626 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8627 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008628 run->mmio.is_write = vcpu->mmio_is_write;
8629 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8630 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008631}
8632
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008633static void kvm_save_current_fpu(struct fpu *fpu)
8634{
8635 /*
8636 * If the target FPU state is not resident in the CPU registers, just
8637 * memcpy() from current, else save CPU state directly to the target.
8638 */
8639 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8640 memcpy(&fpu->state, &current->thread.fpu.state,
8641 fpu_kernel_xstate_size);
8642 else
8643 copy_fpregs_to_fpstate(fpu);
8644}
8645
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008646/* Swap (qemu) user FPU context for the guest FPU context. */
8647static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8648{
Rik van Riel5f409e22019-04-03 18:41:52 +02008649 fpregs_lock();
8650
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008651 kvm_save_current_fpu(vcpu->arch.user_fpu);
8652
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008653 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008654 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008655 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008656
8657 fpregs_mark_activate();
8658 fpregs_unlock();
8659
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008660 trace_kvm_fpu(1);
8661}
8662
8663/* When vcpu_run ends, restore user space FPU context. */
8664static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8665{
Rik van Riel5f409e22019-04-03 18:41:52 +02008666 fpregs_lock();
8667
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008668 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8669
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008670 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008671
8672 fpregs_mark_activate();
8673 fpregs_unlock();
8674
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008675 ++vcpu->stat.fpu_reload;
8676 trace_kvm_fpu(0);
8677}
8678
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008679int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8680{
8681 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008682
Christoffer Dallaccb7572017-12-04 21:35:25 +01008683 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008684 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008685 kvm_load_guest_fpu(vcpu);
8686
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008687 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008688 if (kvm_run->immediate_exit) {
8689 r = -EINTR;
8690 goto out;
8691 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008692 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008693 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008694 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008695 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008696 if (signal_pending(current)) {
8697 r = -EINTR;
8698 vcpu->run->exit_reason = KVM_EXIT_INTR;
8699 ++vcpu->stat.signal_exits;
8700 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008701 goto out;
8702 }
8703
Ken Hofsass01643c52018-01-31 16:03:36 -08008704 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8705 r = -EINVAL;
8706 goto out;
8707 }
8708
8709 if (vcpu->run->kvm_dirty_regs) {
8710 r = sync_regs(vcpu);
8711 if (r != 0)
8712 goto out;
8713 }
8714
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008715 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008716 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008717 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8718 r = -EINVAL;
8719 goto out;
8720 }
8721 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008722
Gleb Natapov716d51a2012-09-03 15:24:26 +03008723 if (unlikely(vcpu->arch.complete_userspace_io)) {
8724 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8725 vcpu->arch.complete_userspace_io = NULL;
8726 r = cui(vcpu);
8727 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008728 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008729 } else
8730 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008731
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008732 if (kvm_run->immediate_exit)
8733 r = -EINTR;
8734 else
8735 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008736
8737out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008738 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008739 if (vcpu->run->kvm_valid_regs)
8740 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008741 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008742 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008743
Christoffer Dallaccb7572017-12-04 21:35:25 +01008744 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008745 return r;
8746}
8747
Ken Hofsass01643c52018-01-31 16:03:36 -08008748static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008749{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008750 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8751 /*
8752 * We are here if userspace calls get_regs() in the middle of
8753 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008754 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008755 * that usually, but some bad designed PV devices (vmware
8756 * backdoor interface) need this to work
8757 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008758 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008759 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8760 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008761 regs->rax = kvm_rax_read(vcpu);
8762 regs->rbx = kvm_rbx_read(vcpu);
8763 regs->rcx = kvm_rcx_read(vcpu);
8764 regs->rdx = kvm_rdx_read(vcpu);
8765 regs->rsi = kvm_rsi_read(vcpu);
8766 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008767 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008768 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008769#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008770 regs->r8 = kvm_r8_read(vcpu);
8771 regs->r9 = kvm_r9_read(vcpu);
8772 regs->r10 = kvm_r10_read(vcpu);
8773 regs->r11 = kvm_r11_read(vcpu);
8774 regs->r12 = kvm_r12_read(vcpu);
8775 regs->r13 = kvm_r13_read(vcpu);
8776 regs->r14 = kvm_r14_read(vcpu);
8777 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008778#endif
8779
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008780 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008781 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008782}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008783
Ken Hofsass01643c52018-01-31 16:03:36 -08008784int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8785{
8786 vcpu_load(vcpu);
8787 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008788 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008789 return 0;
8790}
8791
Ken Hofsass01643c52018-01-31 16:03:36 -08008792static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008793{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008794 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8795 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8796
Sean Christophersonde3cd112019-04-30 10:36:17 -07008797 kvm_rax_write(vcpu, regs->rax);
8798 kvm_rbx_write(vcpu, regs->rbx);
8799 kvm_rcx_write(vcpu, regs->rcx);
8800 kvm_rdx_write(vcpu, regs->rdx);
8801 kvm_rsi_write(vcpu, regs->rsi);
8802 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008803 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008804 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008805#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008806 kvm_r8_write(vcpu, regs->r8);
8807 kvm_r9_write(vcpu, regs->r9);
8808 kvm_r10_write(vcpu, regs->r10);
8809 kvm_r11_write(vcpu, regs->r11);
8810 kvm_r12_write(vcpu, regs->r12);
8811 kvm_r13_write(vcpu, regs->r13);
8812 kvm_r14_write(vcpu, regs->r14);
8813 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008814#endif
8815
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008816 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008817 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008818
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008819 vcpu->arch.exception.pending = false;
8820
Avi Kivity3842d132010-07-27 12:30:24 +03008821 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008822}
Avi Kivity3842d132010-07-27 12:30:24 +03008823
Ken Hofsass01643c52018-01-31 16:03:36 -08008824int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8825{
8826 vcpu_load(vcpu);
8827 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008828 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008829 return 0;
8830}
8831
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008832void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8833{
8834 struct kvm_segment cs;
8835
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008836 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008837 *db = cs.db;
8838 *l = cs.l;
8839}
8840EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8841
Ken Hofsass01643c52018-01-31 16:03:36 -08008842static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008843{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008844 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008845
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008846 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8847 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8848 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8849 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8850 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8851 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008852
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008853 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8854 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008855
8856 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008857 sregs->idt.limit = dt.size;
8858 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008859 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008860 sregs->gdt.limit = dt.size;
8861 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008862
Avi Kivity4d4ec082009-12-29 18:07:30 +02008863 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008864 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008865 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008866 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008867 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008868 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008869 sregs->apic_base = kvm_get_apic_base(vcpu);
8870
Jordan Borgner0e96f312018-10-28 12:58:28 +00008871 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008872
Liran Alon04140b42018-03-23 03:01:31 +03008873 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008874 set_bit(vcpu->arch.interrupt.nr,
8875 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008876}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008877
Ken Hofsass01643c52018-01-31 16:03:36 -08008878int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8879 struct kvm_sregs *sregs)
8880{
8881 vcpu_load(vcpu);
8882 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008883 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008884 return 0;
8885}
8886
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008887int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8888 struct kvm_mp_state *mp_state)
8889{
Christoffer Dallfd232562017-12-04 21:35:30 +01008890 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008891 if (kvm_mpx_supported())
8892 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008893
Jan Kiszka66450a22013-03-13 12:42:34 +01008894 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308895 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8896 vcpu->arch.pv.pv_unhalted)
8897 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8898 else
8899 mp_state->mp_state = vcpu->arch.mp_state;
8900
Sean Christophersonf958bd22019-12-09 12:19:31 -08008901 if (kvm_mpx_supported())
8902 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008903 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008904 return 0;
8905}
8906
8907int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8908 struct kvm_mp_state *mp_state)
8909{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008910 int ret = -EINVAL;
8911
8912 vcpu_load(vcpu);
8913
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008914 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008915 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008916 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008917
Liran Alon27cbe7d2019-11-11 11:16:40 +02008918 /*
8919 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8920 * INIT state; latched init should be reported using
8921 * KVM_SET_VCPU_EVENTS, so reject it here.
8922 */
8923 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008924 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8925 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008926 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008927
Jan Kiszka66450a22013-03-13 12:42:34 +01008928 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8929 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8930 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8931 } else
8932 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008933 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008934
8935 ret = 0;
8936out:
8937 vcpu_put(vcpu);
8938 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008939}
8940
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008941int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8942 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008943{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008944 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008945 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008946
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008947 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008948
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008949 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008950 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008951 if (ret) {
8952 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8953 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8954 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008955 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008956 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008957
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008958 kvm_rip_write(vcpu, ctxt->eip);
8959 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008960 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008961}
8962EXPORT_SYMBOL_GPL(kvm_task_switch);
8963
Peng Hao3140c152018-04-02 09:15:32 +08008964static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008965{
Tianyu Lan37b95952018-01-16 17:34:07 +08008966 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008967 /*
8968 * When EFER.LME and CR0.PG are set, the processor is in
8969 * 64-bit mode (though maybe in a 32-bit code segment).
8970 * CR4.PAE and EFER.LMA must be set.
8971 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008972 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008973 || !(sregs->efer & EFER_LMA))
8974 return -EINVAL;
8975 } else {
8976 /*
8977 * Not in 64-bit mode: EFER.LMA is clear and the code
8978 * segment cannot be 64-bit.
8979 */
8980 if (sregs->efer & EFER_LMA || sregs->cs.l)
8981 return -EINVAL;
8982 }
8983
Wanpeng Li3ca94192019-09-18 17:50:10 +08008984 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05008985}
8986
Ken Hofsass01643c52018-01-31 16:03:36 -08008987static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008988{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008989 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008990 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008991 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008992 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008993 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008994 int ret = -EINVAL;
8995
Lan Tianyuf2981032017-12-14 03:01:52 -05008996 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08008997 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05008998
Jim Mattsond3802282017-08-10 10:14:13 -07008999 apic_base_msr.data = sregs->apic_base;
9000 apic_base_msr.host_initiated = true;
9001 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009002 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009003
Gleb Natapov89a27f42010-02-16 10:51:48 +02009004 dt.size = sregs->idt.limit;
9005 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009006 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009007 dt.size = sregs->gdt.limit;
9008 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009009 kvm_x86_ops->set_gdt(vcpu, &dt);
9010
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009011 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009012 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009013 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009014 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009015
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009016 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009017
Avi Kivityf6801df2010-01-21 15:31:50 +02009018 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009019 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009020
Avi Kivity4d4ec082009-12-29 18:07:30 +02009021 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009022 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009023 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009024
Avi Kivityfc78f512009-12-07 12:16:48 +02009025 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009026 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9027 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009028 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009029 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009030 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009031
9032 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009033 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009034 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009035 mmu_reset_needed = 1;
9036 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009037 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009038
9039 if (mmu_reset_needed)
9040 kvm_mmu_reset_context(vcpu);
9041
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009042 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009043 pending_vec = find_first_bit(
9044 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9045 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009046 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009047 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009048 }
9049
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009050 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9051 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9052 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9053 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9054 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9055 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009056
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009057 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9058 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009059
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009060 update_cr8_intercept(vcpu);
9061
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009062 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009063 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009064 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009065 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009066 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9067
Avi Kivity3842d132010-07-27 12:30:24 +03009068 kvm_make_request(KVM_REQ_EVENT, vcpu);
9069
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009070 ret = 0;
9071out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009072 return ret;
9073}
9074
9075int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9076 struct kvm_sregs *sregs)
9077{
9078 int ret;
9079
9080 vcpu_load(vcpu);
9081 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009082 vcpu_put(vcpu);
9083 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009084}
9085
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009086int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9087 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009088{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009089 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009090 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009091
Christoffer Dall66b56562017-12-04 21:35:33 +01009092 vcpu_load(vcpu);
9093
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009094 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9095 r = -EBUSY;
9096 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009097 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009098 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9099 kvm_queue_exception(vcpu, DB_VECTOR);
9100 else
9101 kvm_queue_exception(vcpu, BP_VECTOR);
9102 }
9103
Jan Kiszka91586a32009-10-05 13:07:21 +02009104 /*
9105 * Read rflags as long as potentially injected trace flags are still
9106 * filtered out.
9107 */
9108 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009109
9110 vcpu->guest_debug = dbg->control;
9111 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9112 vcpu->guest_debug = 0;
9113
9114 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009115 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9116 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009117 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009118 } else {
9119 for (i = 0; i < KVM_NR_DB_REGS; i++)
9120 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009121 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009122 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009123
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009124 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9125 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9126 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009127
Jan Kiszka91586a32009-10-05 13:07:21 +02009128 /*
9129 * Trigger an rflags update that will inject or remove the trace
9130 * flags.
9131 */
9132 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009133
Paolo Bonzinia96036b2015-11-10 11:55:36 +01009134 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009135
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009136 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009137
Avi Kivity2122ff52010-05-13 11:25:04 +03009138out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009139 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009140 return r;
9141}
9142
9143/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009144 * Translate a guest virtual address to a guest physical address.
9145 */
9146int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9147 struct kvm_translation *tr)
9148{
9149 unsigned long vaddr = tr->linear_address;
9150 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009151 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009152
Christoffer Dall1da5b612017-12-04 21:35:32 +01009153 vcpu_load(vcpu);
9154
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009155 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009156 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009157 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009158 tr->physical_address = gpa;
9159 tr->valid = gpa != UNMAPPED_GVA;
9160 tr->writeable = 1;
9161 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009162
Christoffer Dall1da5b612017-12-04 21:35:32 +01009163 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009164 return 0;
9165}
9166
Hollis Blanchardd0752062007-10-31 17:24:25 -05009167int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9168{
Christoffer Dall13931232017-12-04 21:35:34 +01009169 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009170
Christoffer Dall13931232017-12-04 21:35:34 +01009171 vcpu_load(vcpu);
9172
Marc Orrb666a4b2018-11-06 14:53:56 -08009173 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009174 memcpy(fpu->fpr, fxsave->st_space, 128);
9175 fpu->fcw = fxsave->cwd;
9176 fpu->fsw = fxsave->swd;
9177 fpu->ftwx = fxsave->twd;
9178 fpu->last_opcode = fxsave->fop;
9179 fpu->last_ip = fxsave->rip;
9180 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009181 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009182
Christoffer Dall13931232017-12-04 21:35:34 +01009183 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009184 return 0;
9185}
9186
9187int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9188{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009189 struct fxregs_state *fxsave;
9190
9191 vcpu_load(vcpu);
9192
Marc Orrb666a4b2018-11-06 14:53:56 -08009193 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009194
Hollis Blanchardd0752062007-10-31 17:24:25 -05009195 memcpy(fxsave->st_space, fpu->fpr, 128);
9196 fxsave->cwd = fpu->fcw;
9197 fxsave->swd = fpu->fsw;
9198 fxsave->twd = fpu->ftwx;
9199 fxsave->fop = fpu->last_opcode;
9200 fxsave->rip = fpu->last_ip;
9201 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009202 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009203
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009204 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009205 return 0;
9206}
9207
Ken Hofsass01643c52018-01-31 16:03:36 -08009208static void store_regs(struct kvm_vcpu *vcpu)
9209{
9210 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9211
9212 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9213 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9214
9215 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9216 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9217
9218 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9219 kvm_vcpu_ioctl_x86_get_vcpu_events(
9220 vcpu, &vcpu->run->s.regs.events);
9221}
9222
9223static int sync_regs(struct kvm_vcpu *vcpu)
9224{
9225 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9226 return -EINVAL;
9227
9228 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9229 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9230 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9231 }
9232 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9233 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9234 return -EINVAL;
9235 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9236 }
9237 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9238 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9239 vcpu, &vcpu->run->s.regs.events))
9240 return -EINVAL;
9241 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9242 }
9243
9244 return 0;
9245}
9246
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009247static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009248{
Marc Orrb666a4b2018-11-06 14:53:56 -08009249 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009250 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009251 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009252 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009253
Dexuan Cui2acf9232010-06-10 11:27:12 +08009254 /*
9255 * Ensure guest xcr0 is valid for loading
9256 */
Dave Hansend91cab72015-09-02 16:31:26 -07009257 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009258
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009259 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009260}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009261
Sean Christopherson897cc382019-12-18 13:55:09 -08009262int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009263{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009264 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009265 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9266 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009267
Sean Christopherson897cc382019-12-18 13:55:09 -08009268 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009269}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009270
Sean Christophersone529ef62019-12-18 13:55:15 -08009271int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009272{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009273 struct page *page;
9274 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009275
Sean Christopherson95a0d012019-12-18 13:55:23 -08009276 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9277 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9278 else
9279 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9280
9281 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9282
9283 r = kvm_mmu_create(vcpu);
9284 if (r < 0)
9285 return r;
9286
9287 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009288 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9289 if (r < 0)
9290 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009291 if (kvm_apicv_activated(vcpu->kvm))
9292 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009293 } else
9294 static_key_slow_inc(&kvm_no_apic_vcpu);
9295
9296 r = -ENOMEM;
9297
9298 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9299 if (!page)
9300 goto fail_free_lapic;
9301 vcpu->arch.pio_data = page_address(page);
9302
9303 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9304 GFP_KERNEL_ACCOUNT);
9305 if (!vcpu->arch.mce_banks)
9306 goto fail_free_pio_data;
9307 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9308
9309 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9310 GFP_KERNEL_ACCOUNT))
9311 goto fail_free_mce_banks;
9312
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009313 if (!alloc_emulate_ctxt(vcpu))
9314 goto free_wbinvd_dirty_mask;
9315
Sean Christopherson95a0d012019-12-18 13:55:23 -08009316 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9317 GFP_KERNEL_ACCOUNT);
9318 if (!vcpu->arch.user_fpu) {
9319 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009320 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009321 }
9322
9323 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9324 GFP_KERNEL_ACCOUNT);
9325 if (!vcpu->arch.guest_fpu) {
9326 pr_err("kvm: failed to allocate vcpu's fpu\n");
9327 goto free_user_fpu;
9328 }
9329 fx_init(vcpu);
9330
9331 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9332
9333 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9334
9335 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9336
9337 kvm_async_pf_hash_reset(vcpu);
9338 kvm_pmu_init(vcpu);
9339
9340 vcpu->arch.pending_external_vector = -1;
9341 vcpu->arch.preempted_in_kernel = false;
9342
9343 kvm_hv_vcpu_init(vcpu);
9344
9345 r = kvm_x86_ops->vcpu_create(vcpu);
9346 if (r)
9347 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009348
Sean Christopherson0cf91352019-03-07 15:43:02 -08009349 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009350 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009351 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009352 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009353 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009354 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009355 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009356 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009357
9358free_guest_fpu:
9359 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9360free_user_fpu:
9361 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009362free_emulate_ctxt:
9363 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009364free_wbinvd_dirty_mask:
9365 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9366fail_free_mce_banks:
9367 kfree(vcpu->arch.mce_banks);
9368fail_free_pio_data:
9369 free_page((unsigned long)vcpu->arch.pio_data);
9370fail_free_lapic:
9371 kvm_free_lapic(vcpu);
9372fail_mmu_destroy:
9373 kvm_mmu_destroy(vcpu);
9374 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009375}
9376
Dominik Dingel31928aa2014-12-04 15:47:07 +01009377void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009378{
Will Auld8fe8ab42012-11-29 12:42:12 -08009379 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009380 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009381
Roman Kagand3457c82017-07-14 17:13:20 +03009382 kvm_hv_vcpu_postcreate(vcpu);
9383
Christoffer Dallec7660c2017-12-04 21:35:23 +01009384 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009385 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009386 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009387 msr.data = 0x0;
9388 msr.index = MSR_IA32_TSC;
9389 msr.host_initiated = true;
9390 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009391 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009392
9393 /* poll control enabled by default */
9394 vcpu->arch.msr_kvm_poll_control = 1;
9395
Christoffer Dallec7660c2017-12-04 21:35:23 +01009396 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009397
Wanpeng Lib34de572020-02-28 11:18:41 +08009398 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9399 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9400 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009401}
9402
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009403void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009404{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009405 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009406 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009407
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009408 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9409
Sean Christopherson50b143e2019-12-18 13:55:07 -08009410 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009411
9412 kvm_x86_ops->vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009413
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009414 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009415 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9416 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9417 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009418
9419 kvm_hv_vcpu_uninit(vcpu);
9420 kvm_pmu_destroy(vcpu);
9421 kfree(vcpu->arch.mce_banks);
9422 kvm_free_lapic(vcpu);
9423 idx = srcu_read_lock(&vcpu->kvm->srcu);
9424 kvm_mmu_destroy(vcpu);
9425 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9426 free_page((unsigned long)vcpu->arch.pio_data);
9427 if (!lapic_in_kernel(vcpu))
9428 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009429}
9430
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009431void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009432{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009433 kvm_lapic_reset(vcpu, init_event);
9434
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009435 vcpu->arch.hflags = 0;
9436
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009437 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009438 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009439 atomic_set(&vcpu->arch.nmi_queued, 0);
9440 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009441 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009442 kvm_clear_interrupt_queue(vcpu);
9443 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009444
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009445 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009446 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009447 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009448 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009449 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009450 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009451
Nadav Amit11190222015-04-02 03:10:38 +03009452 vcpu->arch.cr2 = 0;
9453
Avi Kivity3842d132010-07-27 12:30:24 +03009454 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009455 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009456 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009457
Glauber Costa12f9a482011-02-01 14:16:40 -05009458 kvmclock_reset(vcpu);
9459
Gleb Natapovaf585b92010-10-14 11:22:46 +02009460 kvm_clear_async_pf_completion_queue(vcpu);
9461 kvm_async_pf_hash_reset(vcpu);
9462 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009463
Wanpeng Lia554d202017-10-11 05:10:19 -07009464 if (kvm_mpx_supported()) {
9465 void *mpx_state_buffer;
9466
9467 /*
9468 * To avoid have the INIT path from kvm_apic_has_events() that be
9469 * called with loaded FPU and does not let userspace fix the state.
9470 */
Rik van Rielf775b132017-11-14 16:54:23 -05009471 if (init_event)
9472 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009473 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009474 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009475 if (mpx_state_buffer)
9476 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009477 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009478 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009479 if (mpx_state_buffer)
9480 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009481 if (init_event)
9482 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009483 }
9484
Paolo Bonzini64d60672015-05-07 11:36:11 +02009485 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009486 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009487 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009488
Kyle Hueydb2336a2017-03-20 01:16:28 -07009489 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009490
9491 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009492 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009493
Julian Stecklina66f7b722012-12-05 15:26:19 +01009494 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9495 vcpu->arch.regs_avail = ~0;
9496 vcpu->arch.regs_dirty = ~0;
9497
Wanpeng Lia554d202017-10-11 05:10:19 -07009498 vcpu->arch.ia32_xss = 0;
9499
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009500 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009501}
9502
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009503void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009504{
9505 struct kvm_segment cs;
9506
9507 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9508 cs.selector = vector << 8;
9509 cs.base = vector << 12;
9510 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9511 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009512}
9513
Radim Krčmář13a34e02014-08-28 15:13:03 +02009514int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009515{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009516 struct kvm *kvm;
9517 struct kvm_vcpu *vcpu;
9518 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009519 int ret;
9520 u64 local_tsc;
9521 u64 max_tsc = 0;
9522 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009523
9524 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009525 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009526 if (ret != 0)
9527 return ret;
9528
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009529 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009530 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009531 list_for_each_entry(kvm, &vm_list, vm_list) {
9532 kvm_for_each_vcpu(i, vcpu, kvm) {
9533 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009534 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009535 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9536 backwards_tsc = true;
9537 if (vcpu->arch.last_host_tsc > max_tsc)
9538 max_tsc = vcpu->arch.last_host_tsc;
9539 }
9540 }
9541 }
9542
9543 /*
9544 * Sometimes, even reliable TSCs go backwards. This happens on
9545 * platforms that reset TSC during suspend or hibernate actions, but
9546 * maintain synchronization. We must compensate. Fortunately, we can
9547 * detect that condition here, which happens early in CPU bringup,
9548 * before any KVM threads can be running. Unfortunately, we can't
9549 * bring the TSCs fully up to date with real time, as we aren't yet far
9550 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009551 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009552 * variables that haven't been updated yet.
9553 *
9554 * So we simply find the maximum observed TSC above, then record the
9555 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9556 * the adjustment will be applied. Note that we accumulate
9557 * adjustments, in case multiple suspend cycles happen before some VCPU
9558 * gets a chance to run again. In the event that no KVM threads get a
9559 * chance to run, we will miss the entire elapsed period, as we'll have
9560 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9561 * loose cycle time. This isn't too big a deal, since the loss will be
9562 * uniform across all VCPUs (not to mention the scenario is extremely
9563 * unlikely). It is possible that a second hibernate recovery happens
9564 * much faster than a first, causing the observed TSC here to be
9565 * smaller; this would require additional padding adjustment, which is
9566 * why we set last_host_tsc to the local tsc observed here.
9567 *
9568 * N.B. - this code below runs only on platforms with reliable TSC,
9569 * as that is the only way backwards_tsc is set above. Also note
9570 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9571 * have the same delta_cyc adjustment applied if backwards_tsc
9572 * is detected. Note further, this adjustment is only done once,
9573 * as we reset last_host_tsc on all VCPUs to stop this from being
9574 * called multiple times (one for each physical CPU bringup).
9575 *
Guo Chao4a969982012-06-28 15:17:27 +08009576 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009577 * will be compensated by the logic in vcpu_load, which sets the TSC to
9578 * catchup mode. This will catchup all VCPUs to real time, but cannot
9579 * guarantee that they stay in perfect synchronization.
9580 */
9581 if (backwards_tsc) {
9582 u64 delta_cyc = max_tsc - local_tsc;
9583 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009584 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009585 kvm_for_each_vcpu(i, vcpu, kvm) {
9586 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9587 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009588 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009589 }
9590
9591 /*
9592 * We have to disable TSC offset matching.. if you were
9593 * booting a VM while issuing an S4 host suspend....
9594 * you may have some problem. Solving this issue is
9595 * left as an exercise to the reader.
9596 */
9597 kvm->arch.last_tsc_nsec = 0;
9598 kvm->arch.last_tsc_write = 0;
9599 }
9600
9601 }
9602 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009603}
9604
Radim Krčmář13a34e02014-08-28 15:13:03 +02009605void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009606{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009607 kvm_x86_ops->hardware_disable();
9608 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009609}
9610
9611int kvm_arch_hardware_setup(void)
9612{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009613 int r;
9614
9615 r = kvm_x86_ops->hardware_setup();
9616 if (r != 0)
9617 return r;
9618
Sean Christophersonb11306b2019-12-10 14:44:13 -08009619 cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data);
9620
Haozhong Zhang35181e82015-10-20 15:39:03 +08009621 if (kvm_has_tsc_control) {
9622 /*
9623 * Make sure the user can only configure tsc_khz values that
9624 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009625 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009626 * be 1 on all machines.
9627 */
9628 u64 max = min(0x7fffffffULL,
9629 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9630 kvm_max_guest_tsc_khz = max;
9631
Haozhong Zhangad7218832015-10-20 15:39:02 +08009632 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009633 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009634
Aaron Lewis139a12c2019-10-21 16:30:25 -07009635 if (boot_cpu_has(X86_FEATURE_XSAVES))
9636 rdmsrl(MSR_IA32_XSS, host_xss);
9637
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009638 kvm_init_msr_list();
9639 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009640}
9641
9642void kvm_arch_hardware_unsetup(void)
9643{
9644 kvm_x86_ops->hardware_unsetup();
9645}
9646
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009647int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009648{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009649 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
9650
9651 WARN_ON(!irqs_disabled());
9652
9653 if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits)
9654 return -EIO;
9655
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009656 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009657}
9658
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009659bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9660{
9661 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9662}
9663EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9664
9665bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9666{
9667 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9668}
9669
Gleb Natapov54e98182012-08-05 15:58:32 +03009670struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009671EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009672
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009673void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9674{
Like Xub35e5542019-10-27 18:52:43 +08009675 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9676
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009677 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009678 if (pmu->version && unlikely(pmu->event_count)) {
9679 pmu->need_cleanup = true;
9680 kvm_make_request(KVM_REQ_PMU, vcpu);
9681 }
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009682 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009683}
9684
Sean Christopherson562b6b02020-01-26 16:41:13 -08009685void kvm_arch_free_vm(struct kvm *kvm)
9686{
9687 kfree(kvm->arch.hyperv.hv_pa_pg);
9688 vfree(kvm);
9689}
9690
9691
Carsten Ottee08b9632012-01-04 10:25:20 +01009692int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009693{
Carsten Ottee08b9632012-01-04 10:25:20 +01009694 if (type)
9695 return -EINVAL;
9696
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009697 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009698 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009699 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009700 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009701 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009702 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009703
Sheng Yang5550af42008-10-15 20:15:06 +08009704 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9705 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009706 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9707 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9708 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009709
Jan Kiszka038f8c12011-02-04 10:49:11 +01009710 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009711 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009712 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9713
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009714 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009715 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009716
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009717 kvm->arch.guest_can_read_msr_platform_info = true;
9718
Andrew Jones7e44e442014-02-28 12:52:54 +01009719 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009720 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009721
Roman Kagancbc02362018-02-01 16:48:31 +03009722 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009723 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009724 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009725
Sean Christopherson92735b12019-08-02 15:06:17 -07009726 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009727}
9728
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009729int kvm_arch_post_init_vm(struct kvm *kvm)
9730{
9731 return kvm_mmu_post_init_vm(kvm);
9732}
9733
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009734static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9735{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009736 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009737 kvm_mmu_unload(vcpu);
9738 vcpu_put(vcpu);
9739}
9740
9741static void kvm_free_vcpus(struct kvm *kvm)
9742{
9743 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009744 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009745
9746 /*
9747 * Unpin any mmu pages first.
9748 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009749 kvm_for_each_vcpu(i, vcpu, kvm) {
9750 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009751 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009752 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009753 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009754 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009755
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009756 mutex_lock(&kvm->lock);
9757 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9758 kvm->vcpus[i] = NULL;
9759
9760 atomic_set(&kvm->online_vcpus, 0);
9761 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009762}
9763
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009764void kvm_arch_sync_events(struct kvm *kvm)
9765{
Andrew Jones332967a2014-02-28 12:52:55 +01009766 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009767 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009768 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009769}
9770
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009771int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009772{
9773 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009774 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009775 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009776 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009777
9778 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009779 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9780 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009781
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009782 slot = id_to_memslot(slots, id);
9783 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009784 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009785 return -EEXIST;
9786
9787 /*
9788 * MAP_SHARED to prevent internal slot pages from being moved
9789 * by fork()/COW.
9790 */
9791 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9792 MAP_SHARED | MAP_ANONYMOUS, 0);
9793 if (IS_ERR((void *)hva))
9794 return PTR_ERR((void *)hva);
9795 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009796 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009797 return 0;
9798
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009799 /*
9800 * Stuff a non-canonical value to catch use-after-delete. This
9801 * ends up being 0 on 32-bit KVM, but there's no better
9802 * alternative.
9803 */
9804 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009805 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009806 }
9807
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009808 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009809 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009810
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009811 m.slot = id | (i << 16);
9812 m.flags = 0;
9813 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009814 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009815 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009816 r = __kvm_set_memory_region(kvm, &m);
9817 if (r < 0)
9818 return r;
9819 }
9820
Eric Biggers103c7632018-01-31 17:30:21 -08009821 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009822 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009823
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009824 return 0;
9825}
9826EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9827
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009828void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9829{
9830 kvm_mmu_pre_destroy_vm(kvm);
9831}
9832
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009833void kvm_arch_destroy_vm(struct kvm *kvm)
9834{
Andrew Honig27469d22013-04-18 09:38:14 -07009835 if (current->mm == kvm->mm) {
9836 /*
9837 * Free memory regions allocated on behalf of userspace,
9838 * unless the the memory map has changed due to process exit
9839 * or fd copying.
9840 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009841 mutex_lock(&kvm->slots_lock);
9842 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9843 0, 0);
9844 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9845 0, 0);
9846 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9847 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009848 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009849 if (kvm_x86_ops->vm_destroy)
9850 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009851 kvm_pic_destroy(kvm);
9852 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009853 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009854 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009855 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009856 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009857 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009858 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009859}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009860
Sean Christophersone96c81e2020-02-18 13:07:27 -08009861void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009862{
9863 int i;
9864
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009865 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009866 kvfree(slot->arch.rmap[i]);
9867 slot->arch.rmap[i] = NULL;
9868
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009869 if (i == 0)
9870 continue;
9871
Sean Christophersone96c81e2020-02-18 13:07:27 -08009872 kvfree(slot->arch.lpage_info[i - 1]);
9873 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009874 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009875
Sean Christophersone96c81e2020-02-18 13:07:27 -08009876 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009877}
9878
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009879static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9880 unsigned long npages)
9881{
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009882 int i;
9883
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009884 /*
9885 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9886 * old arrays will be freed by __kvm_set_memory_region() if installing
9887 * the new memslot is successful.
9888 */
9889 memset(&slot->arch, 0, sizeof(slot->arch));
9890
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009891 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009892 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009893 unsigned long ugfn;
9894 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009895 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009896
9897 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9898 slot->base_gfn, level) + 1;
9899
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009900 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009901 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009902 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009903 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009904 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009905 if (i == 0)
9906 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009907
Ben Gardon254272c2019-02-11 11:02:50 -08009908 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009909 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009910 goto out_free;
9911
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009912 slot->arch.lpage_info[i - 1] = linfo;
9913
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009914 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009915 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009916 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009917 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009918 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9919 /*
9920 * If the gfn and userspace address are not aligned wrt each
Sean Christopherson600087b2020-03-02 15:57:05 -08009921 * other, disable large page support for this slot.
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009922 */
Sean Christopherson600087b2020-03-02 15:57:05 -08009923 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) {
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009924 unsigned long j;
9925
9926 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009927 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009928 }
9929 }
9930
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009931 if (kvm_page_track_create_memslot(slot, npages))
9932 goto out_free;
9933
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009934 return 0;
9935
9936out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009937 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009938 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009939 slot->arch.rmap[i] = NULL;
9940 if (i == 0)
9941 continue;
9942
Thomas Huth548ef282015-02-24 21:29:25 +01009943 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009944 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009945 }
9946 return -ENOMEM;
9947}
9948
Sean Christopherson15248252019-02-05 12:54:17 -08009949void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009950{
Boris Ostrovsky91724812019-12-05 01:30:51 +00009951 struct kvm_vcpu *vcpu;
9952 int i;
9953
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009954 /*
9955 * memslots->generation has been incremented.
9956 * mmio generation may have reached its maximum value.
9957 */
Sean Christopherson15248252019-02-05 12:54:17 -08009958 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +00009959
9960 /* Force re-initialization of steal_time cache */
9961 kvm_for_each_vcpu(i, vcpu, kvm)
9962 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009963}
9964
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009965int kvm_arch_prepare_memory_region(struct kvm *kvm,
9966 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009967 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009968 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009969{
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009970 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
9971 return kvm_alloc_memslot_metadata(memslot,
9972 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009973 return 0;
9974}
9975
Kai Huang88178fd2015-01-28 10:54:27 +08009976static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9977 struct kvm_memory_slot *new)
9978{
9979 /* Still write protect RO slot */
9980 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08009981 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +08009982 return;
9983 }
9984
9985 /*
9986 * Call kvm_x86_ops dirty logging hooks when they are valid.
9987 *
9988 * kvm_x86_ops->slot_disable_log_dirty is called when:
9989 *
9990 * - KVM_MR_CREATE with dirty logging is disabled
9991 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9992 *
9993 * The reason is, in case of PML, we need to set D-bit for any slots
9994 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +08009995 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +08009996 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +08009997 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +08009998 * logging disabled for memory slots.
9999 *
10000 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
10001 * to dirty logging mode.
10002 *
10003 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10004 *
10005 * In case of write protect:
10006 *
10007 * Write protect all pages for dirty logging.
10008 *
10009 * All the sptes including the large sptes which point to this
10010 * slot are set to readonly. We can not create any new large
10011 * spte on this slot until the end of the logging.
10012 *
10013 * See the comments in fast_page_fault().
10014 */
10015 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010016 if (kvm_x86_ops->slot_enable_log_dirty) {
Kai Huang88178fd2015-01-28 10:54:27 +080010017 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010018 } else {
10019 int level =
10020 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10021 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10022
10023 /*
10024 * If we're with initial-all-set, we don't need
10025 * to write protect any small page because
10026 * they're reported as dirty already. However
10027 * we still need to write-protect huge pages
10028 * so that the page split can happen lazily on
10029 * the first write to the huge page.
10030 */
10031 kvm_mmu_slot_remove_write_access(kvm, new, level);
10032 }
Kai Huang88178fd2015-01-28 10:54:27 +080010033 } else {
10034 if (kvm_x86_ops->slot_disable_log_dirty)
10035 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
10036 }
10037}
10038
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010039void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010040 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010041 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010042 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010043 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010044{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010045 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010046 kvm_mmu_change_mmu_pages(kvm,
10047 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010048
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010049 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010050 * Dirty logging tracks sptes in 4k granularity, meaning that large
10051 * sptes have to be split. If live migration is successful, the guest
10052 * in the source machine will be destroyed and large sptes will be
10053 * created in the destination. However, if the guest continues to run
10054 * in the source machine (for example if live migration fails), small
10055 * sptes will remain around and cause bad performance.
10056 *
10057 * Scan sptes if dirty logging has been stopped, dropping those
10058 * which can be collapsed into a single large-page spte. Later
10059 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010060 *
10061 * There is no need to do this in any of the following cases:
10062 * CREATE: No dirty mappings will already exist.
10063 * MOVE/DELETE: The old mappings will already have been cleaned up by
10064 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010065 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010066 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010067 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10068 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10069 kvm_mmu_zap_collapsible_sptes(kvm, new);
10070
10071 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010072 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010073 *
Kai Huang88178fd2015-01-28 10:54:27 +080010074 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10075 * been zapped so no dirty logging staff is needed for old slot. For
10076 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10077 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010078 *
10079 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010080 */
Kai Huang88178fd2015-01-28 10:54:27 +080010081 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010082 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010083
10084 /* Free the arrays associated with the old memslot. */
10085 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010086 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010087}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010088
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010089void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010090{
Sean Christopherson7390de12019-02-05 13:01:31 -080010091 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010092}
10093
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010094void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10095 struct kvm_memory_slot *slot)
10096{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010097 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010098}
10099
Liran Alone6c67d82018-09-04 10:56:52 +030010100static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10101{
10102 return (is_guest_mode(vcpu) &&
10103 kvm_x86_ops->guest_apic_has_interrupt &&
10104 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
10105}
10106
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010107static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10108{
10109 if (!list_empty_careful(&vcpu->async_pf.done))
10110 return true;
10111
10112 if (kvm_apic_has_events(vcpu))
10113 return true;
10114
10115 if (vcpu->arch.pv.pv_unhalted)
10116 return true;
10117
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010118 if (vcpu->arch.exception.pending)
10119 return true;
10120
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010121 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10122 (vcpu->arch.nmi_pending &&
10123 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010124 return true;
10125
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010126 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10127 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010128 return true;
10129
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010130 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010131 (kvm_cpu_has_interrupt(vcpu) ||
10132 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010133 return true;
10134
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010135 if (kvm_hv_has_stimer_pending(vcpu))
10136 return true;
10137
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010138 return false;
10139}
10140
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010141int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10142{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010143 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010144}
Zhang Xiantao57361992007-12-17 14:21:40 +080010145
Wanpeng Li17e433b2019-08-05 10:03:19 +080010146bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10147{
10148 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10149 return true;
10150
10151 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10152 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10153 kvm_test_request(KVM_REQ_EVENT, vcpu))
10154 return true;
10155
10156 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
10157 return true;
10158
10159 return false;
10160}
10161
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010162bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10163{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010164 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010165}
10166
Christoffer Dallb6d33832012-03-08 16:44:24 -050010167int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010168{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010169 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010170}
Gleb Natapov78646122009-03-23 12:12:11 +020010171
10172int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10173{
10174 return kvm_x86_ops->interrupt_allowed(vcpu);
10175}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010176
Nadav Amit82b32772014-11-02 11:54:45 +020010177unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10178{
10179 if (is_64_bit_mode(vcpu))
10180 return kvm_rip_read(vcpu);
10181 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10182 kvm_rip_read(vcpu));
10183}
10184EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10185
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010186bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10187{
Nadav Amit82b32772014-11-02 11:54:45 +020010188 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010189}
10190EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10191
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010192unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10193{
10194 unsigned long rflags;
10195
10196 rflags = kvm_x86_ops->get_rflags(vcpu);
10197 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010198 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010199 return rflags;
10200}
10201EXPORT_SYMBOL_GPL(kvm_get_rflags);
10202
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010203static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010204{
10205 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010206 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010207 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010208 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010209}
10210
10211void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10212{
10213 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010214 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010215}
10216EXPORT_SYMBOL_GPL(kvm_set_rflags);
10217
Gleb Natapov56028d02010-10-17 18:13:42 +020010218void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10219{
10220 int r;
10221
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010222 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010223 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010224 return;
10225
10226 r = kvm_mmu_reload(vcpu);
10227 if (unlikely(r))
10228 return;
10229
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010230 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010231 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010232 return;
10233
Sean Christopherson7a026742020-02-06 14:14:34 -080010234 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010235}
10236
Gleb Natapovaf585b92010-10-14 11:22:46 +020010237static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10238{
10239 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10240}
10241
10242static inline u32 kvm_async_pf_next_probe(u32 key)
10243{
10244 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10245}
10246
10247static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10248{
10249 u32 key = kvm_async_pf_hash_fn(gfn);
10250
10251 while (vcpu->arch.apf.gfns[key] != ~0)
10252 key = kvm_async_pf_next_probe(key);
10253
10254 vcpu->arch.apf.gfns[key] = gfn;
10255}
10256
10257static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10258{
10259 int i;
10260 u32 key = kvm_async_pf_hash_fn(gfn);
10261
10262 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010263 (vcpu->arch.apf.gfns[key] != gfn &&
10264 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010265 key = kvm_async_pf_next_probe(key);
10266
10267 return key;
10268}
10269
10270bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10271{
10272 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10273}
10274
10275static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10276{
10277 u32 i, j, k;
10278
10279 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10280 while (true) {
10281 vcpu->arch.apf.gfns[i] = ~0;
10282 do {
10283 j = kvm_async_pf_next_probe(j);
10284 if (vcpu->arch.apf.gfns[j] == ~0)
10285 return;
10286 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10287 /*
10288 * k lies cyclically in ]i,j]
10289 * | i.k.j |
10290 * |....j i.k.| or |.k..j i...|
10291 */
10292 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10293 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10294 i = j;
10295 }
10296}
10297
Gleb Natapov7c907052010-10-14 11:22:53 +020010298static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10299{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010300
10301 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10302 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010303}
10304
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010305static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10306{
10307
10308 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10309 sizeof(u32));
10310}
10311
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010312static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10313{
10314 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10315 return false;
10316
10317 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10318 (vcpu->arch.apf.send_user_only &&
10319 kvm_x86_ops->get_cpl(vcpu) == 0))
10320 return false;
10321
10322 return true;
10323}
10324
10325bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10326{
10327 if (unlikely(!lapic_in_kernel(vcpu) ||
10328 kvm_event_needs_reinjection(vcpu) ||
10329 vcpu->arch.exception.pending))
10330 return false;
10331
10332 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10333 return false;
10334
10335 /*
10336 * If interrupts are off we cannot even use an artificial
10337 * halt state.
10338 */
10339 return kvm_x86_ops->interrupt_allowed(vcpu);
10340}
10341
Gleb Natapovaf585b92010-10-14 11:22:46 +020010342void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10343 struct kvm_async_pf *work)
10344{
Avi Kivity6389ee92010-11-29 16:12:30 +020010345 struct x86_exception fault;
10346
Sean Christopherson736c2912019-12-06 15:57:14 -080010347 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010348 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010349
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010350 if (kvm_can_deliver_async_pf(vcpu) &&
10351 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010352 fault.vector = PF_VECTOR;
10353 fault.error_code_valid = true;
10354 fault.error_code = 0;
10355 fault.nested_page_fault = false;
10356 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010357 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010358 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010359 } else {
10360 /*
10361 * It is not possible to deliver a paravirtualized asynchronous
10362 * page fault, but putting the guest in an artificial halt state
10363 * can be beneficial nevertheless: if an interrupt arrives, we
10364 * can deliver it timely and perhaps the guest will schedule
10365 * another process. When the instruction that triggered a page
10366 * fault is retried, hopefully the page will be ready in the host.
10367 */
10368 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010369 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010370}
10371
10372void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10373 struct kvm_async_pf *work)
10374{
Avi Kivity6389ee92010-11-29 16:12:30 +020010375 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010376 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010377
chai wenf2e10662013-10-14 22:22:33 +080010378 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010379 work->arch.token = ~0; /* broadcast wakeup */
10380 else
10381 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010382 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010383
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010384 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10385 !apf_get_user(vcpu, &val)) {
10386 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10387 vcpu->arch.exception.pending &&
10388 vcpu->arch.exception.nr == PF_VECTOR &&
10389 !apf_put_user(vcpu, 0)) {
10390 vcpu->arch.exception.injected = false;
10391 vcpu->arch.exception.pending = false;
10392 vcpu->arch.exception.nr = 0;
10393 vcpu->arch.exception.has_error_code = false;
10394 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010395 vcpu->arch.exception.has_payload = false;
10396 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010397 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10398 fault.vector = PF_VECTOR;
10399 fault.error_code_valid = true;
10400 fault.error_code = 0;
10401 fault.nested_page_fault = false;
10402 fault.address = work->arch.token;
10403 fault.async_page_fault = true;
10404 kvm_inject_page_fault(vcpu, &fault);
10405 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010406 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010407 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010408 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010409}
10410
10411bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10412{
10413 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10414 return true;
10415 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010416 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010417}
10418
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010419void kvm_arch_start_assignment(struct kvm *kvm)
10420{
10421 atomic_inc(&kvm->arch.assigned_device_count);
10422}
10423EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10424
10425void kvm_arch_end_assignment(struct kvm *kvm)
10426{
10427 atomic_dec(&kvm->arch.assigned_device_count);
10428}
10429EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10430
10431bool kvm_arch_has_assigned_device(struct kvm *kvm)
10432{
10433 return atomic_read(&kvm->arch.assigned_device_count);
10434}
10435EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10436
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010437void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10438{
10439 atomic_inc(&kvm->arch.noncoherent_dma_count);
10440}
10441EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10442
10443void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10444{
10445 atomic_dec(&kvm->arch.noncoherent_dma_count);
10446}
10447EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10448
10449bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10450{
10451 return atomic_read(&kvm->arch.noncoherent_dma_count);
10452}
10453EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10454
Alex Williamson14717e22016-05-05 11:58:35 -060010455bool kvm_arch_has_irq_bypass(void)
10456{
Sean Christopherson92735b12019-08-02 15:06:17 -070010457 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010458}
10459
Feng Wu87276882015-09-18 22:29:40 +080010460int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10461 struct irq_bypass_producer *prod)
10462{
10463 struct kvm_kernel_irqfd *irqfd =
10464 container_of(cons, struct kvm_kernel_irqfd, consumer);
10465
Alex Williamson14717e22016-05-05 11:58:35 -060010466 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010467
Alex Williamson14717e22016-05-05 11:58:35 -060010468 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10469 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010470}
10471
10472void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10473 struct irq_bypass_producer *prod)
10474{
10475 int ret;
10476 struct kvm_kernel_irqfd *irqfd =
10477 container_of(cons, struct kvm_kernel_irqfd, consumer);
10478
Feng Wu87276882015-09-18 22:29:40 +080010479 WARN_ON(irqfd->producer != prod);
10480 irqfd->producer = NULL;
10481
10482 /*
10483 * When producer of consumer is unregistered, we change back to
10484 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010485 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010486 * int this case doesn't want to receive the interrupts.
10487 */
10488 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10489 if (ret)
10490 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10491 " fails: %d\n", irqfd->consumer.token, ret);
10492}
10493
10494int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10495 uint32_t guest_irq, bool set)
10496{
Feng Wu87276882015-09-18 22:29:40 +080010497 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10498}
10499
Feng Wu520040142016-01-25 16:53:33 +080010500bool kvm_vector_hashing_enabled(void)
10501{
10502 return vector_hashing;
10503}
Feng Wu520040142016-01-25 16:53:33 +080010504
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010505bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10506{
10507 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10508}
10509EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10510
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010511u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10512{
10513 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10514
10515 /* The STIBP bit doesn't fault even if it's not advertised */
10516 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10517 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10518 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10519 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10520 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10521 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10522
10523 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10524 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10525 bits &= ~SPEC_CTRL_SSBD;
10526 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10527 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10528 bits &= ~SPEC_CTRL_SSBD;
10529
10530 return bits;
10531}
10532EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010533
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010534EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010535EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010536EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10537EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10538EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10539EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010540EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010541EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010542EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010543EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010544EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010545EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010546EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010547EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010548EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010549EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010550EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010551EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010552EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10553EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010554EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);