blob: fd0889f2f37fae1053e619a66ea08f7e1888f8d1 [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 Tosatti0061d532013-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 Tosatti0061d532013-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 Tosatti0061d532013-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 Tosatti0061d532013-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 Tosatti0061d532013-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 /*
2823 * We do support PT if kvm_x86_ops->pt_supported(), but we do
2824 * not support IA32_XSS[bit 8]. Guests will have to use
2825 * RDMSR/WRMSR rather than XSAVES/XRSTORS to save/restore PT
2826 * MSRs.
2827 */
2828 if (data != 0)
2829 return 1;
2830 vcpu->arch.ia32_xss = data;
2831 break;
Liran Alon52797bf2017-11-15 13:43:14 +02002832 case MSR_SMI_COUNT:
2833 if (!msr_info->host_initiated)
2834 return 1;
2835 vcpu->arch.smi_count = data;
2836 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002837 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002838 case MSR_KVM_WALL_CLOCK:
2839 vcpu->kvm->arch.wall_clock = data;
2840 kvm_write_wall_clock(vcpu->kvm, data);
2841 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04002842 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002843 case MSR_KVM_SYSTEM_TIME: {
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002844 struct kvm_arch *ka = &vcpu->kvm->arch;
2845
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002846 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2847 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2848
2849 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
Radim Krčmář1bd20092017-04-26 22:32:20 +02002850 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosatti54750f22015-01-20 15:54:52 -02002851
2852 ka->boot_vcpu_runs_old_kvmclock = tmp;
2853 }
2854
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002855 vcpu->arch.time = data;
Marcelo Tosatti0061d532013-05-09 20:21:41 -03002856 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002857
2858 /* we verify if the enable bit is set... */
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002859 vcpu->arch.pv_time_enabled = false;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002860 if (!(data & 1))
2861 break;
2862
Paolo Bonzini49dedf02019-10-10 12:49:22 +02002863 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
Andrew Honig8f964522013-03-29 09:35:21 -07002864 &vcpu->arch.pv_time, data & ~1ULL,
2865 sizeof(struct pvclock_vcpu_time_info)))
Andy Honig0b794592013-02-20 14:48:10 -08002866 vcpu->arch.pv_time_enabled = true;
Xiao Guangrong32cad842012-08-03 15:42:52 +08002867
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02002868 break;
2869 }
Gleb Natapov344d9582010-10-14 11:22:50 +02002870 case MSR_KVM_ASYNC_PF_EN:
2871 if (kvm_pv_enable_async_pf(vcpu, data))
2872 return 1;
2873 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002874 case MSR_KVM_STEAL_TIME:
2875
2876 if (unlikely(!sched_info_on()))
2877 return 1;
2878
2879 if (data & KVM_STEAL_RESERVED_MASK)
2880 return 1;
2881
Glauber Costac9aaa892011-07-11 15:28:14 -04002882 vcpu->arch.st.msr_val = data;
2883
2884 if (!(data & KVM_MSR_ENABLED))
2885 break;
2886
Glauber Costac9aaa892011-07-11 15:28:14 -04002887 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2888
2889 break;
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002890 case MSR_KVM_PV_EOI_EN:
Ladi Prosek72bbf932018-10-16 18:49:59 +02002891 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03002892 return 1;
2893 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04002894
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03002895 case MSR_KVM_POLL_CONTROL:
2896 /* only enable bit supported */
2897 if (data & (-1ULL << 1))
2898 return 1;
2899
2900 vcpu->arch.msr_kvm_poll_control = data;
2901 break;
2902
Huang Ying890ca9a2009-05-11 16:48:15 +08002903 case MSR_IA32_MCG_CTL:
2904 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08002905 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Wanpeng Li9ffd9862017-10-19 06:47:56 -07002906 return set_msr_mce(vcpu, msr_info);
Andre Przywara71db6022009-06-12 22:01:29 +02002907
Wei Huang6912ac32015-06-12 01:34:56 -04002908 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2909 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2910 pr = true; /* fall through */
2911 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2912 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02002913 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002914 return kvm_pmu_set_msr(vcpu, msr_info);
Gleb Natapov57537852012-01-15 14:17:22 +02002915
2916 if (pr || data != 0)
Christoffer Dalla737f252012-06-03 21:17:48 +03002917 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2918 "0x%x data 0x%llx\n", msr, data);
Gleb Natapov57537852012-01-15 14:17:22 +02002919 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002920 case MSR_K7_CLK_CTL:
2921 /*
2922 * Ignore all writes to this no longer documented MSR.
2923 * Writes are only relevant for old K7 processors,
2924 * all pre-dating SVM, but a recommended workaround from
Guo Chao4a969982012-06-28 15:17:27 +08002925 * AMD for these chips. It is possible to specify the
Jes Sorensen84e0cef2010-09-01 11:42:04 +02002926 * affected processor models on the command line, hence
2927 * the need to ignore the workaround.
2928 */
2929 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02002930 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002931 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2932 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03002933 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01002934 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2935 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2936 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine7d95132015-07-03 15:01:37 +03002937 return kvm_hv_set_msr_common(vcpu, msr, data,
2938 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05002939 case MSR_IA32_BBL_CR_CTL3:
2940 /* Drop writes to this legacy MSR -- see rdmsr
2941 * counterpart for further detail.
2942 */
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002943 if (report_ignored_msrs)
2944 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2945 msr, data);
john cooper91c9c3e2011-01-21 00:21:00 -05002946 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002947 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002948 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002949 return 1;
2950 vcpu->arch.osvw.length = data;
2951 break;
2952 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002953 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002954 return 1;
2955 vcpu->arch.osvw.status = data;
2956 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07002957 case MSR_PLATFORM_INFO:
2958 if (!msr_info->host_initiated ||
Kyle Hueydb2336a2017-03-20 01:16:28 -07002959 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2960 cpuid_fault_enabled(vcpu)))
2961 return 1;
2962 vcpu->arch.msr_platform_info = data;
2963 break;
2964 case MSR_MISC_FEATURES_ENABLES:
2965 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2966 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2967 !supports_cpuid_fault(vcpu)))
2968 return 1;
2969 vcpu->arch.msr_misc_features_enables = data;
2970 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01002971 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07002972 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2973 return xen_hvm_config(vcpu, data);
Wei Huangc6702c92015-06-19 13:44:45 +02002974 if (kvm_pmu_is_valid_msr(vcpu, msr))
Paolo Bonziniafd80d82013-03-28 17:18:35 +01002975 return kvm_pmu_set_msr(vcpu, msr_info);
Andre Przywaraed85c062009-06-25 12:36:49 +02002976 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05002977 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
Christoffer Dalla737f252012-06-03 21:17:48 +03002978 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002979 return 1;
2980 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02002981 if (report_ignored_msrs)
2982 vcpu_unimpl(vcpu,
2983 "ignored wrmsr: 0x%x data 0x%llx\n",
2984 msr, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02002985 break;
2986 }
Carsten Otte15c4a642007-10-30 18:44:17 +01002987 }
2988 return 0;
2989}
2990EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2991
Paolo Bonzini44883f02018-07-26 13:01:52 +02002992static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Huang Ying890ca9a2009-05-11 16:48:15 +08002993{
2994 u64 data;
2995 u64 mcg_cap = vcpu->arch.mcg_cap;
2996 unsigned bank_num = mcg_cap & 0xff;
2997
2998 switch (msr) {
2999 case MSR_IA32_P5_MC_ADDR:
3000 case MSR_IA32_P5_MC_TYPE:
3001 data = 0;
3002 break;
3003 case MSR_IA32_MCG_CAP:
3004 data = vcpu->arch.mcg_cap;
3005 break;
3006 case MSR_IA32_MCG_CTL:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003007 if (!(mcg_cap & MCG_CTL_P) && !host)
Huang Ying890ca9a2009-05-11 16:48:15 +08003008 return 1;
3009 data = vcpu->arch.mcg_ctl;
3010 break;
3011 case MSR_IA32_MCG_STATUS:
3012 data = vcpu->arch.mcg_status;
3013 break;
3014 default:
3015 if (msr >= MSR_IA32_MC0_CTL &&
Chen Yucong81760dc2014-09-23 10:44:35 +08003016 msr < MSR_IA32_MCx_CTL(bank_num)) {
Marios Pomonis6ec4c5e2019-12-11 12:47:49 -08003017 u32 offset = array_index_nospec(
3018 msr - MSR_IA32_MC0_CTL,
3019 MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
3020
Huang Ying890ca9a2009-05-11 16:48:15 +08003021 data = vcpu->arch.mce_banks[offset];
3022 break;
3023 }
3024 return 1;
3025 }
3026 *pdata = data;
3027 return 0;
3028}
3029
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003030int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Carsten Otte15c4a642007-10-30 18:44:17 +01003031{
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003032 switch (msr_info->index) {
Carsten Otte15c4a642007-10-30 18:44:17 +01003033 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01003034 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02003035 case MSR_IA32_DEBUGCTLMSR:
3036 case MSR_IA32_LASTBRANCHFROMIP:
3037 case MSR_IA32_LASTBRANCHTOIP:
3038 case MSR_IA32_LASTINTFROMIP:
3039 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05303040 case MSR_K8_SYSCFG:
Paolo Bonzini3afb1122015-09-18 17:33:04 +02003041 case MSR_K8_TSEG_ADDR:
3042 case MSR_K8_TSEG_MASK:
Avi Kivity61a6bd62008-12-29 17:32:28 +02003043 case MSR_VM_HSAVE_PA:
Andre Przywara1fdbd482009-06-24 12:44:34 +02003044 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02003045 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02003046 case MSR_FAM10H_MMIO_CONF_BASE:
Borislav Petkov2e32b712013-02-19 19:33:13 +01003047 case MSR_AMD64_BU_CFG2:
Dmitry Bilunov0c2df2a2016-05-31 17:38:24 +03003048 case MSR_IA32_PERF_CTL:
Ladi Prosek405a3532017-04-06 15:22:20 +02003049 case MSR_AMD64_DC_CFG:
Eduardo Habkost0e1b8692018-12-17 22:34:18 -02003050 case MSR_F15H_EX_CFG:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003051 msr_info->data = 0;
Carsten Otte15c4a642007-10-30 18:44:17 +01003052 break;
Janakarajan Natarajanc51eb522018-02-05 13:24:52 -06003053 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
Wei Huang6912ac32015-06-12 01:34:56 -04003054 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
3055 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
3056 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
3057 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
Wei Huangc6702c92015-06-19 13:44:45 +02003058 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003059 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
3060 msr_info->data = 0;
Gleb Natapov57537852012-01-15 14:17:22 +02003061 break;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003062 case MSR_IA32_UCODE_REV:
Wanpeng Li518e7b92018-02-28 14:03:31 +08003063 msr_info->data = vcpu->arch.microcode_version;
Marcelo Tosatti742bc672011-07-29 19:44:21 -03003064 break;
Sean Christopherson0cf91352019-03-07 15:43:02 -08003065 case MSR_IA32_ARCH_CAPABILITIES:
3066 if (!msr_info->host_initiated &&
3067 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3068 return 1;
3069 msr_info->data = vcpu->arch.arch_capabilities;
3070 break;
Paolo Bonzini73f624f2019-06-06 14:32:59 +02003071 case MSR_IA32_POWER_CTL:
3072 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
3073 break;
Paolo Bonzinidd259932018-04-13 11:38:35 +02003074 case MSR_IA32_TSC:
3075 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
3076 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03003077 case MSR_MTRRcap:
Avi Kivity9ba075a2008-05-26 20:06:35 +03003078 case 0x200 ... 0x2ff:
Xiao Guangrongff536042015-06-15 16:55:22 +08003079 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
Carsten Otte15c4a642007-10-30 18:44:17 +01003080 case 0xcd: /* fsb frequency */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003081 msr_info->data = 3;
Carsten Otte15c4a642007-10-30 18:44:17 +01003082 break;
Jes Sorensen7b914092010-09-09 12:06:46 +02003083 /*
3084 * MSR_EBC_FREQUENCY_ID
3085 * Conservative value valid for even the basic CPU models.
3086 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
3087 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
3088 * and 266MHz for model 3, or 4. Set Core Clock
3089 * Frequency to System Bus Frequency Ratio to 1 (bits
3090 * 31:24) even though these are only valid for CPU
3091 * models > 2, however guests may end up dividing or
3092 * multiplying by zero otherwise.
3093 */
3094 case MSR_EBC_FREQUENCY_ID:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003095 msr_info->data = 1 << 24;
Jes Sorensen7b914092010-09-09 12:06:46 +02003096 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003097 case MSR_IA32_APICBASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003098 msr_info->data = kvm_get_apic_base(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +01003099 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03003100 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003101 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003102 case MSR_IA32_TSCDEADLINE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003103 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
Liu, Jinsonga3e06bb2011-09-22 16:55:52 +08003104 break;
Will Auldba904632012-11-29 12:42:50 -08003105 case MSR_IA32_TSC_ADJUST:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003106 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
Will Auldba904632012-11-29 12:42:50 -08003107 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003108 case MSR_IA32_MISC_ENABLE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003109 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01003110 break;
Paolo Bonzini64d60672015-05-07 11:36:11 +02003111 case MSR_IA32_SMBASE:
3112 if (!msr_info->host_initiated)
3113 return 1;
3114 msr_info->data = vcpu->arch.smbase;
Carsten Otte15c4a642007-10-30 18:44:17 +01003115 break;
Liran Alon52797bf2017-11-15 13:43:14 +02003116 case MSR_SMI_COUNT:
3117 msr_info->data = vcpu->arch.smi_count;
3118 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003119 case MSR_IA32_PERF_STATUS:
3120 /* TSC increment by tick */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003121 msr_info->data = 1000ULL;
Alexander Graf847f0ad2008-02-21 12:11:01 +01003122 /* CPU multiplier */
Nicolas Ioossb0996ae2015-06-29 18:39:23 +08003123 msr_info->data |= (((uint64_t)4ULL) << 40);
Alexander Graf847f0ad2008-02-21 12:11:01 +01003124 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003125 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003126 msr_info->data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01003127 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003128 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003129 case MSR_KVM_WALL_CLOCK_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003130 msr_info->data = vcpu->kvm->arch.wall_clock;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003131 break;
3132 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04003133 case MSR_KVM_SYSTEM_TIME_NEW:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003134 msr_info->data = vcpu->arch.time;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02003135 break;
Gleb Natapov344d9582010-10-14 11:22:50 +02003136 case MSR_KVM_ASYNC_PF_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003137 msr_info->data = vcpu->arch.apf.msr_val;
Gleb Natapov344d9582010-10-14 11:22:50 +02003138 break;
Glauber Costac9aaa892011-07-11 15:28:14 -04003139 case MSR_KVM_STEAL_TIME:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003140 msr_info->data = vcpu->arch.st.msr_val;
Glauber Costac9aaa892011-07-11 15:28:14 -04003141 break;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003142 case MSR_KVM_PV_EOI_EN:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003143 msr_info->data = vcpu->arch.pv_eoi.msr_val;
Michael S. Tsirkin1d921282012-08-26 18:00:29 +03003144 break;
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03003145 case MSR_KVM_POLL_CONTROL:
3146 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3147 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003148 case MSR_IA32_P5_MC_ADDR:
3149 case MSR_IA32_P5_MC_TYPE:
3150 case MSR_IA32_MCG_CAP:
3151 case MSR_IA32_MCG_CTL:
3152 case MSR_IA32_MCG_STATUS:
Chen Yucong81760dc2014-09-23 10:44:35 +08003153 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
Paolo Bonzini44883f02018-07-26 13:01:52 +02003154 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3155 msr_info->host_initiated);
Aaron Lewis864e2ab2019-10-21 16:30:26 -07003156 case MSR_IA32_XSS:
3157 if (!msr_info->host_initiated &&
3158 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3159 return 1;
3160 msr_info->data = vcpu->arch.ia32_xss;
3161 break;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003162 case MSR_K7_CLK_CTL:
3163 /*
3164 * Provide expected ramp-up count for K7. All other
3165 * are set to zero, indicating minimum divisors for
3166 * every field.
3167 *
3168 * This prevents guest kernels on AMD host with CPU
3169 * type 6, model 8 and higher from exploding due to
3170 * the rdmsr failing.
3171 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003172 msr_info->data = 0x20000000;
Jes Sorensen84e0cef2010-09-01 11:42:04 +02003173 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003174 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Andrey Smetanine7d95132015-07-03 15:01:37 +03003175 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3176 case HV_X64_MSR_CRASH_CTL:
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03003177 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01003178 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3179 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3180 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +03003181 return kvm_hv_get_msr_common(vcpu,
Paolo Bonzini44883f02018-07-26 13:01:52 +02003182 msr_info->index, &msr_info->data,
3183 msr_info->host_initiated);
john cooper91c9c3e2011-01-21 00:21:00 -05003184 case MSR_IA32_BBL_CR_CTL3:
3185 /* This legacy MSR exists but isn't fully documented in current
3186 * silicon. It is however accessed by winxp in very narrow
3187 * scenarios where it sets bit #19, itself documented as
3188 * a "reserved" bit. Best effort attempt to source coherent
3189 * read data here should the balance of the register be
3190 * interpreted by the guest:
3191 *
3192 * L2 cache control register 3: 64GB range, 256KB size,
3193 * enabled, latency 0x1, configured
3194 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003195 msr_info->data = 0xbe702111;
john cooper91c9c3e2011-01-21 00:21:00 -05003196 break;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003197 case MSR_AMD64_OSVW_ID_LENGTH:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003198 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003199 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003200 msr_info->data = vcpu->arch.osvw.length;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003201 break;
3202 case MSR_AMD64_OSVW_STATUS:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003203 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003204 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003205 msr_info->data = vcpu->arch.osvw.status;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05003206 break;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003207 case MSR_PLATFORM_INFO:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003208 if (!msr_info->host_initiated &&
3209 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3210 return 1;
Kyle Hueydb2336a2017-03-20 01:16:28 -07003211 msr_info->data = vcpu->arch.msr_platform_info;
3212 break;
3213 case MSR_MISC_FEATURES_ENABLES:
3214 msr_info->data = vcpu->arch.msr_misc_features_enables;
3215 break;
Borislav Petkov191c8132019-04-18 18:32:50 +02003216 case MSR_K7_HWCR:
3217 msr_info->data = vcpu->arch.msr_hwcr;
3218 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003219 default:
Wei Huangc6702c92015-06-19 13:44:45 +02003220 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003221 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
Andre Przywaraed85c062009-06-25 12:36:49 +02003222 if (!ignore_msrs) {
Bandan Dasae0f5492016-11-15 01:36:18 -05003223 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3224 msr_info->index);
Andre Przywaraed85c062009-06-25 12:36:49 +02003225 return 1;
3226 } else {
Eyal Moscovicifab0aa32017-11-08 14:32:08 +02003227 if (report_ignored_msrs)
3228 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3229 msr_info->index);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003230 msr_info->data = 0;
Andre Przywaraed85c062009-06-25 12:36:49 +02003231 }
3232 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01003233 }
Carsten Otte15c4a642007-10-30 18:44:17 +01003234 return 0;
3235}
3236EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3237
Carsten Otte313a3dc2007-10-11 19:16:52 +02003238/*
3239 * Read or write a bunch of msrs. All parameters are kernel addresses.
3240 *
3241 * @return number of msrs set successfully.
3242 */
3243static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3244 struct kvm_msr_entry *entries,
3245 int (*do_msr)(struct kvm_vcpu *vcpu,
3246 unsigned index, u64 *data))
3247{
Tom Lendacky801e4592018-02-21 13:39:51 -06003248 int i;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003249
Carsten Otte313a3dc2007-10-11 19:16:52 +02003250 for (i = 0; i < msrs->nmsrs; ++i)
3251 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3252 break;
3253
Carsten Otte313a3dc2007-10-11 19:16:52 +02003254 return i;
3255}
3256
3257/*
3258 * Read or write a bunch of msrs. Parameters are user addresses.
3259 *
3260 * @return number of msrs set successfully.
3261 */
3262static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3263 int (*do_msr)(struct kvm_vcpu *vcpu,
3264 unsigned index, u64 *data),
3265 int writeback)
3266{
3267 struct kvm_msrs msrs;
3268 struct kvm_msr_entry *entries;
3269 int r, n;
3270 unsigned size;
3271
3272 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003273 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02003274 goto out;
3275
3276 r = -E2BIG;
3277 if (msrs.nmsrs >= MAX_IO_MSRS)
3278 goto out;
3279
Carsten Otte313a3dc2007-10-11 19:16:52 +02003280 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003281 entries = memdup_user(user_msrs->entries, size);
3282 if (IS_ERR(entries)) {
3283 r = PTR_ERR(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003284 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003285 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02003286
3287 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3288 if (r < 0)
3289 goto out_free;
3290
3291 r = -EFAULT;
3292 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3293 goto out_free;
3294
3295 r = n;
3296
3297out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03003298 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003299out:
3300 return r;
3301}
3302
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003303static inline bool kvm_can_mwait_in_guest(void)
3304{
3305 return boot_cpu_has(X86_FEATURE_MWAIT) &&
KarimAllah Ahmed8e9b29b2018-04-11 11:16:03 +02003306 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3307 boot_cpu_has(X86_FEATURE_ARAT);
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003308}
3309
Alexander Graf784aa3d2014-07-14 18:27:35 +02003310int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003311{
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003312 int r = 0;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003313
3314 switch (ext) {
3315 case KVM_CAP_IRQCHIP:
3316 case KVM_CAP_HLT:
3317 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003318 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02003319 case KVM_CAP_EXT_CPUID:
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003320 case KVM_CAP_EXT_EMUL_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003321 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08003322 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05003323 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003324 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03003325 case KVM_CAP_SYNC_MMU:
Lai Jiangshana355c852010-12-14 17:57:47 +08003326 case KVM_CAP_USER_NMI:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003327 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02003328 case KVM_CAP_IRQ_INJECT_STATUS:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003329 case KVM_CAP_IOEVENTFD:
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +03003330 case KVM_CAP_IOEVENTFD_NO_LENGTH:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003331 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04003332 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08003333 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07003334 case KVM_CAP_XEN_HVM:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003335 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003336 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02003337 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02003338 case KVM_CAP_HYPERV_SPIN:
Andrey Smetanin5c9194122015-11-10 15:36:34 +03003339 case KVM_CAP_HYPERV_SYNIC:
Roman Kaganefc479e2017-06-22 16:51:01 +03003340 case KVM_CAP_HYPERV_SYNIC2:
Roman Kagand3457c82017-07-14 17:13:20 +03003341 case KVM_CAP_HYPERV_VP_INDEX:
Roman Kaganfaeb7832018-02-01 16:48:32 +03003342 case KVM_CAP_HYPERV_EVENTFD:
Vitaly Kuznetsovc1aea912018-05-16 17:21:31 +02003343 case KVM_CAP_HYPERV_TLBFLUSH:
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02003344 case KVM_CAP_HYPERV_SEND_IPI:
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01003345 case KVM_CAP_HYPERV_CPUID:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08003346 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003347 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01003348 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003349 case KVM_CAP_XSAVE:
Gleb Natapov344d9582010-10-14 11:22:50 +02003350 case KVM_CAP_ASYNC_PF:
Joerg Roedel92a1f122011-03-25 09:44:51 +01003351 case KVM_CAP_GET_TSC_KHZ:
Eric B Munson1c0b28c2012-03-10 14:37:27 -05003352 case KVM_CAP_KVMCLOCK_CTRL:
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003353 case KVM_CAP_READONLY_MEM:
Paolo Bonzini5f66b622014-01-29 18:10:45 +01003354 case KVM_CAP_HYPERV_TIME:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -05003355 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Radim Krčmářdefcf512015-01-08 15:59:30 +01003356 case KVM_CAP_TSC_DEADLINE_TIMER:
Nadav Amit90de4a12015-04-13 01:53:41 +03003357 case KVM_CAP_DISABLE_QUIRKS:
Paolo Bonzinid71ba782015-07-29 11:56:48 +02003358 case KVM_CAP_SET_BOOT_CPU_ID:
Steve Rutherford49df6392015-07-29 23:21:40 -07003359 case KVM_CAP_SPLIT_IRQCHIP:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01003360 case KVM_CAP_IMMEDIATE_EXIT:
Eric Hankland66bb8a02019-07-10 18:25:15 -07003361 case KVM_CAP_PMU_EVENT_FILTER:
Tom Lendacky801e4592018-02-21 13:39:51 -06003362 case KVM_CAP_GET_MSR_FEATURES:
Drew Schmitt6fbbde92018-08-20 10:32:15 -07003363 case KVM_CAP_MSR_PLATFORM_INFO:
Jim Mattsonc4f55192018-10-16 14:29:24 -07003364 case KVM_CAP_EXCEPTION_PAYLOAD:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003365 r = 1;
3366 break;
Ken Hofsass01643c52018-01-31 16:03:36 -08003367 case KVM_CAP_SYNC_REGS:
3368 r = KVM_SYNC_X86_VALID_FIELDS;
3369 break;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01003370 case KVM_CAP_ADJUST_CLOCK:
3371 r = KVM_CLOCK_TSC_STABLE;
3372 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003373 case KVM_CAP_X86_DISABLE_EXITS:
Wanpeng Lib5170062019-05-21 14:06:53 +08003374 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3375 KVM_X86_DISABLE_EXITS_CSTATE;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003376 if(kvm_can_mwait_in_guest())
3377 r |= KVM_X86_DISABLE_EXITS_MWAIT;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003378 break;
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003379 case KVM_CAP_X86_SMM:
3380 /* SMBASE is usually relocated above 1M on modern chipsets,
3381 * and SMM handlers might indeed rely on 4G segment limits,
3382 * so do not report SMM to be available if real mode is
3383 * emulated via vm86 mode. Still, do not go to great lengths
3384 * to avoid userspace's usage of the feature, because it is a
3385 * fringe case that is not enabled except via specific settings
3386 * of the module parameters.
3387 */
Tom Lendackybc226f02018-05-10 22:06:39 +02003388 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003389 break;
Avi Kivity774ead32007-12-26 13:57:04 +02003390 case KVM_CAP_VAPIC:
3391 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3392 break;
Avi Kivityf7252302008-02-20 11:53:16 +02003393 case KVM_CAP_NR_VCPUS:
Sasha Levin8c3ba332011-07-18 17:17:15 +03003394 r = KVM_SOFT_MAX_VCPUS;
3395 break;
3396 case KVM_CAP_MAX_VCPUS:
Avi Kivityf7252302008-02-20 11:53:16 +02003397 r = KVM_MAX_VCPUS;
3398 break;
Thomas Hutha86cb412019-05-23 18:43:08 +02003399 case KVM_CAP_MAX_VCPU_ID:
3400 r = KVM_MAX_VCPU_ID;
3401 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03003402 case KVM_CAP_PV_MMU: /* obsolete */
3403 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003404 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003405 case KVM_CAP_MCE:
3406 r = KVM_MAX_MCE_BANKS;
3407 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003408 case KVM_CAP_XCRS:
Borislav Petkovd366bf72016-04-04 22:25:02 +02003409 r = boot_cpu_has(X86_FEATURE_XSAVE);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08003410 break;
Joerg Roedel92a1f122011-03-25 09:44:51 +01003411 case KVM_CAP_TSC_CONTROL:
3412 r = kvm_has_tsc_control;
3413 break;
Radim Krčmář371313132016-07-12 22:09:27 +02003414 case KVM_CAP_X2APIC_API:
3415 r = KVM_X2APIC_API_VALID_FLAGS;
3416 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003417 case KVM_CAP_NESTED_STATE:
3418 r = kvm_x86_ops->get_nested_state ?
Hariprasad Kelambe43c442019-04-06 15:06:58 +05303419 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02003420 break;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003421 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
Vitaly Kuznetsov5a0165f2019-08-28 09:59:05 +02003422 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3423 break;
3424 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3425 r = kvm_x86_ops->nested_enable_evmcs != NULL;
Tianyu Lan344c6c82019-08-22 22:30:20 +08003426 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003427 default:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003428 break;
3429 }
3430 return r;
3431
3432}
3433
Carsten Otte043405e2007-10-10 17:16:19 +02003434long kvm_arch_dev_ioctl(struct file *filp,
3435 unsigned int ioctl, unsigned long arg)
3436{
3437 void __user *argp = (void __user *)arg;
3438 long r;
3439
3440 switch (ioctl) {
3441 case KVM_GET_MSR_INDEX_LIST: {
3442 struct kvm_msr_list __user *user_msr_list = argp;
3443 struct kvm_msr_list msr_list;
3444 unsigned n;
3445
3446 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003447 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003448 goto out;
3449 n = msr_list.nmsrs;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003450 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003451 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
Carsten Otte043405e2007-10-10 17:16:19 +02003452 goto out;
3453 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003454 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02003455 goto out;
3456 r = -EFAULT;
3457 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3458 num_msrs_to_save * sizeof(u32)))
3459 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02003460 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02003461 &emulated_msrs,
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02003462 num_emulated_msrs * sizeof(u32)))
Carsten Otte043405e2007-10-10 17:16:19 +02003463 goto out;
3464 r = 0;
3465 break;
3466 }
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003467 case KVM_GET_SUPPORTED_CPUID:
3468 case KVM_GET_EMULATED_CPUID: {
Avi Kivity674eea02008-02-11 18:37:23 +02003469 struct kvm_cpuid2 __user *cpuid_arg = argp;
3470 struct kvm_cpuid2 cpuid;
3471
3472 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003473 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003474 goto out;
Borislav Petkov9c15bb12013-09-22 16:44:50 +02003475
3476 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3477 ioctl);
Avi Kivity674eea02008-02-11 18:37:23 +02003478 if (r)
3479 goto out;
3480
3481 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00003482 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Avi Kivity674eea02008-02-11 18:37:23 +02003483 goto out;
3484 r = 0;
3485 break;
3486 }
Huang Ying890ca9a2009-05-11 16:48:15 +08003487 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
Huang Ying890ca9a2009-05-11 16:48:15 +08003488 r = -EFAULT;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003489 if (copy_to_user(argp, &kvm_mce_cap_supported,
3490 sizeof(kvm_mce_cap_supported)))
Huang Ying890ca9a2009-05-11 16:48:15 +08003491 goto out;
3492 r = 0;
3493 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06003494 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3495 struct kvm_msr_list __user *user_msr_list = argp;
3496 struct kvm_msr_list msr_list;
3497 unsigned int n;
3498
3499 r = -EFAULT;
3500 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3501 goto out;
3502 n = msr_list.nmsrs;
3503 msr_list.nmsrs = num_msr_based_features;
3504 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3505 goto out;
3506 r = -E2BIG;
3507 if (n < msr_list.nmsrs)
3508 goto out;
3509 r = -EFAULT;
3510 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3511 num_msr_based_features * sizeof(u32)))
3512 goto out;
3513 r = 0;
3514 break;
3515 }
3516 case KVM_GET_MSRS:
3517 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3518 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08003519 }
Carsten Otte043405e2007-10-10 17:16:19 +02003520 default:
3521 r = -EINVAL;
3522 }
3523out:
3524 return r;
3525}
3526
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003527static void wbinvd_ipi(void *garbage)
3528{
3529 wbinvd();
3530}
3531
3532static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3533{
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06003534 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003535}
3536
Carsten Otte313a3dc2007-10-11 19:16:52 +02003537void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3538{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003539 /* Address WBINVD may be executed by guest */
3540 if (need_emulate_wbinvd(vcpu)) {
3541 if (kvm_x86_ops->has_wbinvd_exit())
3542 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3543 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3544 smp_call_function_single(vcpu->cpu,
3545 wbinvd_ipi, NULL, 1);
3546 }
3547
Carsten Otte313a3dc2007-10-11 19:16:52 +02003548 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003549
3550 /* Apply any externally detected TSC adjustments (due to suspend) */
3551 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3552 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3553 vcpu->arch.tsc_offset_adjustment = 0;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08003554 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02003555 }
3556
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003557 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
Zachary Amsden6f526ec2012-02-03 15:43:54 -02003558 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003559 rdtsc() - vcpu->arch.last_host_tsc;
Zachary Amsdene48672f2010-08-19 22:07:23 -10003560 if (tsc_delta < 0)
3561 mark_tsc_unstable("KVM discovered backwards TSC");
Yunhong Jiangce7a0582016-06-13 14:20:01 -07003562
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01003563 if (kvm_check_tsc_unstable()) {
Haozhong Zhang07c14192015-10-20 15:39:05 +08003564 u64 offset = kvm_compute_tsc_offset(vcpu,
Zachary Amsdenb183aa52012-02-03 15:43:53 -02003565 vcpu->arch.last_guest_tsc);
Luiz Capitulinoa545ab62016-09-07 14:47:19 -04003566 kvm_vcpu_write_tsc_offset(vcpu, offset);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003567 vcpu->arch.tsc_catchup = 1;
Zachary Amsdenc2855452010-09-18 14:38:15 -10003568 }
Paolo Bonzinia749e242017-06-29 17:14:50 +02003569
3570 if (kvm_lapic_hv_timer_in_use(vcpu))
3571 kvm_lapic_restart_hv_timer(vcpu);
3572
Marcelo Tosattid98d07c2012-11-27 23:29:04 -02003573 /*
3574 * On a host with synchronized TSC, there is no need to update
3575 * kvmclock on vcpu->cpu migration
3576 */
3577 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
Marcelo Tosatti0061d532013-05-09 20:21:41 -03003578 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10003579 if (vcpu->cpu != cpu)
Radim Krčmář1bd20092017-04-26 22:32:20 +02003580 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10003581 vcpu->cpu = cpu;
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003582 }
Glauber Costac9aaa892011-07-11 15:28:14 -04003583
Glauber Costac9aaa892011-07-11 15:28:14 -04003584 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003585}
3586
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003587static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3588{
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003589 struct kvm_host_map map;
3590 struct kvm_steal_time *st;
3591
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003592 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3593 return;
3594
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003595 if (vcpu->arch.st.preempted)
Boris Ostrovsky8c6de562019-10-30 19:01:31 +00003596 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003597
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003598 if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map,
3599 &vcpu->arch.st.cache, true))
3600 return;
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003601
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003602 st = map.hva +
3603 offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS);
3604
Boris Ostrovskya6bd8112019-12-06 15:36:12 +00003605 st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED;
Boris Ostrovskyb0431382019-12-05 03:45:32 +00003606
3607 kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003608}
3609
Carsten Otte313a3dc2007-10-11 19:16:52 +02003610void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3611{
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003612 int idx;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003613
3614 if (vcpu->preempted)
3615 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3616
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003617 /*
3618 * Disable page faults because we're in atomic context here.
3619 * kvm_write_guest_offset_cached() would call might_fault()
3620 * that relies on pagefault_disable() to tell if there's a
3621 * bug. NOTE: the write to guest memory may not go through if
3622 * during postcopy live migration or if there's heavy guest
3623 * paging.
3624 */
3625 pagefault_disable();
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003626 /*
3627 * kvm_memslots() will be called by
3628 * kvm_write_guest_offset_cached() so take the srcu lock.
3629 */
3630 idx = srcu_read_lock(&vcpu->kvm->srcu);
Pan Xinhui0b9f6c42016-11-02 05:08:35 -04003631 kvm_steal_time_set_preempted(vcpu);
Andrea Arcangelicc0d9072016-12-17 19:13:32 +01003632 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Andrea Arcangeli931f2612016-12-17 18:43:52 +01003633 pagefault_enable();
Avi Kivity02daab22009-12-30 12:40:26 +02003634 kvm_x86_ops->vcpu_put(vcpu);
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003635 vcpu->arch.last_host_tsc = rdtsc();
Wanpeng Liefdab992017-12-13 10:46:40 +01003636 /*
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003637 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3638 * on every vmexit, but if not, we might have a stale dr6 from the
3639 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
Wanpeng Liefdab992017-12-13 10:46:40 +01003640 */
Radim Krčmářf9dcf082018-10-23 16:31:38 +02003641 set_debugreg(0, 6);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003642}
3643
Carsten Otte313a3dc2007-10-11 19:16:52 +02003644static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3645 struct kvm_lapic_state *s)
3646{
Liran Alonfa59cc02017-12-24 18:12:53 +02003647 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03003648 kvm_x86_ops->sync_pir_to_irr(vcpu);
3649
Radim Krčmářa92e2542016-07-12 22:09:22 +02003650 return kvm_apic_get_state(vcpu, s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003651}
3652
3653static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3654 struct kvm_lapic_state *s)
3655{
Radim Krčmářa92e2542016-07-12 22:09:22 +02003656 int r;
3657
3658 r = kvm_apic_set_state(vcpu, s);
3659 if (r)
3660 return r;
Gleb Natapovcb142eb2009-08-09 15:17:40 +03003661 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02003662
3663 return 0;
3664}
3665
Matt Gingell127a4572015-11-17 17:32:05 +01003666static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3667{
3668 return (!lapic_in_kernel(vcpu) ||
3669 kvm_apic_accept_pic_intr(vcpu));
3670}
3671
Matt Gingell782d4222015-11-16 15:26:00 -08003672/*
3673 * if userspace requested an interrupt window, check that the
3674 * interrupt window is open.
3675 *
3676 * No need to exit to userspace if we already have an interrupt queued.
3677 */
3678static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3679{
3680 return kvm_arch_interrupt_allowed(vcpu) &&
3681 !kvm_cpu_has_interrupt(vcpu) &&
3682 !kvm_event_needs_reinjection(vcpu) &&
3683 kvm_cpu_accept_dm_intr(vcpu);
3684}
3685
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003686static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3687 struct kvm_interrupt *irq)
3688{
Chen Gang02cdb502013-02-27 11:33:25 +08003689 if (irq->irq >= KVM_NR_INTERRUPTS)
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003690 return -EINVAL;
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003691
3692 if (!irqchip_in_kernel(vcpu->kvm)) {
3693 kvm_queue_interrupt(vcpu, irq->irq, false);
3694 kvm_make_request(KVM_REQ_EVENT, vcpu);
3695 return 0;
3696 }
3697
3698 /*
3699 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3700 * fail for in-kernel 8259.
3701 */
3702 if (pic_in_kernel(vcpu->kvm))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003703 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003704
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003705 if (vcpu->arch.pending_external_vector != -1)
3706 return -EEXIST;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003707
Steve Rutherford1c1a9ce2015-07-30 11:27:16 +02003708 vcpu->arch.pending_external_vector = irq->irq;
Matt Gingell934bf652015-11-16 15:26:05 -08003709 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08003710 return 0;
3711}
3712
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003713static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3714{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003715 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003716
3717 return 0;
3718}
3719
Paolo Bonzinif0778252015-04-01 15:06:40 +02003720static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3721{
Paolo Bonzini64d60672015-05-07 11:36:11 +02003722 kvm_make_request(KVM_REQ_SMI, vcpu);
3723
Paolo Bonzinif0778252015-04-01 15:06:40 +02003724 return 0;
3725}
3726
Avi Kivityb209749f2007-10-22 16:50:39 +02003727static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3728 struct kvm_tpr_access_ctl *tac)
3729{
3730 if (tac->flags)
3731 return -EINVAL;
3732 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3733 return 0;
3734}
3735
Huang Ying890ca9a2009-05-11 16:48:15 +08003736static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3737 u64 mcg_cap)
3738{
3739 int r;
3740 unsigned bank_num = mcg_cap & 0xff, bank;
3741
3742 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02003743 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08003744 goto out;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003745 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
Huang Ying890ca9a2009-05-11 16:48:15 +08003746 goto out;
3747 r = 0;
3748 vcpu->arch.mcg_cap = mcg_cap;
3749 /* Init IA32_MCG_CTL to all 1s */
3750 if (mcg_cap & MCG_CTL_P)
3751 vcpu->arch.mcg_ctl = ~(u64)0;
3752 /* Init IA32_MCi_CTL to all 1s */
3753 for (bank = 0; bank < bank_num; bank++)
3754 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003755
Sean Christopherson92735b12019-08-02 15:06:17 -07003756 kvm_x86_ops->setup_mce(vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003757out:
3758 return r;
3759}
3760
3761static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3762 struct kvm_x86_mce *mce)
3763{
3764 u64 mcg_cap = vcpu->arch.mcg_cap;
3765 unsigned bank_num = mcg_cap & 0xff;
3766 u64 *banks = vcpu->arch.mce_banks;
3767
3768 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3769 return -EINVAL;
3770 /*
3771 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3772 * reporting is disabled
3773 */
3774 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3775 vcpu->arch.mcg_ctl != ~(u64)0)
3776 return 0;
3777 banks += 4 * mce->bank;
3778 /*
3779 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3780 * reporting is disabled for the bank
3781 */
3782 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3783 return 0;
3784 if (mce->status & MCI_STATUS_UC) {
3785 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02003786 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003787 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08003788 return 0;
3789 }
3790 if (banks[1] & MCI_STATUS_VAL)
3791 mce->status |= MCI_STATUS_OVER;
3792 banks[2] = mce->addr;
3793 banks[3] = mce->misc;
3794 vcpu->arch.mcg_status = mce->mcg_status;
3795 banks[1] = mce->status;
3796 kvm_queue_exception(vcpu, MC_VECTOR);
3797 } else if (!(banks[1] & MCI_STATUS_VAL)
3798 || !(banks[1] & MCI_STATUS_UC)) {
3799 if (banks[1] & MCI_STATUS_VAL)
3800 mce->status |= MCI_STATUS_OVER;
3801 banks[2] = mce->addr;
3802 banks[3] = mce->misc;
3803 banks[1] = mce->status;
3804 } else
3805 banks[1] |= MCI_STATUS_OVER;
3806 return 0;
3807}
3808
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003809static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3810 struct kvm_vcpu_events *events)
3811{
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003812 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003813
Wanpeng Li664f8e22017-08-24 03:35:09 -07003814 /*
Oliver Uptona06230b2020-02-07 02:36:06 -08003815 * In guest mode, payload delivery should be deferred,
3816 * so that the L1 hypervisor can intercept #PF before
3817 * CR2 is modified (or intercept #DB before DR6 is
3818 * modified under nVMX). Unless the per-VM capability,
3819 * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of
3820 * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we
3821 * opportunistically defer the exception payload, deliver it if the
3822 * capability hasn't been requested before processing a
3823 * KVM_GET_VCPU_EVENTS.
3824 */
3825 if (!vcpu->kvm->arch.exception_payload_enabled &&
3826 vcpu->arch.exception.pending && vcpu->arch.exception.has_payload)
3827 kvm_deliver_exception_payload(vcpu);
3828
3829 /*
Jim Mattson59073aa2018-10-16 14:29:20 -07003830 * The API doesn't provide the instruction length for software
3831 * exceptions, so don't report them. As long as the guest RIP
3832 * isn't advanced, we should expect to encounter the exception
3833 * again.
Wanpeng Li664f8e22017-08-24 03:35:09 -07003834 */
Jim Mattson59073aa2018-10-16 14:29:20 -07003835 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3836 events->exception.injected = 0;
3837 events->exception.pending = 0;
3838 } else {
3839 events->exception.injected = vcpu->arch.exception.injected;
3840 events->exception.pending = vcpu->arch.exception.pending;
3841 /*
3842 * For ABI compatibility, deliberately conflate
3843 * pending and injected exceptions when
3844 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3845 */
3846 if (!vcpu->kvm->arch.exception_payload_enabled)
3847 events->exception.injected |=
3848 vcpu->arch.exception.pending;
3849 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003850 events->exception.nr = vcpu->arch.exception.nr;
3851 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3852 events->exception.error_code = vcpu->arch.exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003853 events->exception_has_payload = vcpu->arch.exception.has_payload;
3854 events->exception_payload = vcpu->arch.exception.payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003855
Jan Kiszka03b82a32010-02-15 10:45:41 +01003856 events->interrupt.injected =
Liran Alon04140b42018-03-23 03:01:31 +03003857 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003858 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01003859 events->interrupt.soft = 0;
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003860 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003861
3862 events->nmi.injected = vcpu->arch.nmi_injected;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003863 events->nmi.pending = vcpu->arch.nmi_pending != 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003864 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003865 events->nmi.pad = 0;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003866
Jan Kiszka66450a22013-03-13 12:42:34 +01003867 events->sipi_vector = 0; /* never valid when reporting to user space */
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003868
Paolo Bonzinif0778252015-04-01 15:06:40 +02003869 events->smi.smm = is_smm(vcpu);
3870 events->smi.pending = vcpu->arch.smi_pending;
3871 events->smi.smm_inside_nmi =
3872 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3873 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3874
Jan Kiszkadab4b912009-12-06 18:24:15 +01003875 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Paolo Bonzinif0778252015-04-01 15:06:40 +02003876 | KVM_VCPUEVENT_VALID_SHADOW
3877 | KVM_VCPUEVENT_VALID_SMM);
Jim Mattson59073aa2018-10-16 14:29:20 -07003878 if (vcpu->kvm->arch.exception_payload_enabled)
3879 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3880
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003881 memset(&events->reserved, 0, sizeof(events->reserved));
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003882}
3883
Sean Christophersonc5833c72019-04-02 08:03:10 -07003884static void kvm_smm_changed(struct kvm_vcpu *vcpu);
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003885
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003886static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3887 struct kvm_vcpu_events *events)
3888{
Jan Kiszkadab4b912009-12-06 18:24:15 +01003889 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01003890 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
Paolo Bonzinif0778252015-04-01 15:06:40 +02003891 | KVM_VCPUEVENT_VALID_SHADOW
Jim Mattson59073aa2018-10-16 14:29:20 -07003892 | KVM_VCPUEVENT_VALID_SMM
3893 | KVM_VCPUEVENT_VALID_PAYLOAD))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003894 return -EINVAL;
3895
Jim Mattson59073aa2018-10-16 14:29:20 -07003896 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3897 if (!vcpu->kvm->arch.exception_payload_enabled)
3898 return -EINVAL;
3899 if (events->exception.pending)
3900 events->exception.injected = 0;
3901 else
3902 events->exception_has_payload = 0;
3903 } else {
3904 events->exception.pending = 0;
3905 events->exception_has_payload = 0;
3906 }
3907
3908 if ((events->exception.injected || events->exception.pending) &&
3909 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
Paolo Bonzini78e546c2016-06-01 14:09:20 +02003910 return -EINVAL;
3911
David Hildenbrand28bf2882017-03-23 11:46:03 +01003912 /* INITs are latched while in SMM */
3913 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3914 (events->smi.smm || events->smi.pending) &&
3915 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3916 return -EINVAL;
3917
Avi Kivity7460fb4a2011-09-20 13:43:14 +03003918 process_nmi(vcpu);
Jim Mattson59073aa2018-10-16 14:29:20 -07003919 vcpu->arch.exception.injected = events->exception.injected;
3920 vcpu->arch.exception.pending = events->exception.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003921 vcpu->arch.exception.nr = events->exception.nr;
3922 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3923 vcpu->arch.exception.error_code = events->exception.error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -07003924 vcpu->arch.exception.has_payload = events->exception_has_payload;
3925 vcpu->arch.exception.payload = events->exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003926
Liran Alon04140b42018-03-23 03:01:31 +03003927 vcpu->arch.interrupt.injected = events->interrupt.injected;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003928 vcpu->arch.interrupt.nr = events->interrupt.nr;
3929 vcpu->arch.interrupt.soft = events->interrupt.soft;
Jan Kiszka48005f62010-02-19 19:38:07 +01003930 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3931 kvm_x86_ops->set_interrupt_shadow(vcpu,
3932 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003933
3934 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01003935 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3936 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003937 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3938
Jan Kiszka66450a22013-03-13 12:42:34 +01003939 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01003940 lapic_in_kernel(vcpu))
Jan Kiszka66450a22013-03-13 12:42:34 +01003941 vcpu->arch.apic->sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003942
Paolo Bonzinif0778252015-04-01 15:06:40 +02003943 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
Sean Christophersonc5833c72019-04-02 08:03:10 -07003944 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3945 if (events->smi.smm)
3946 vcpu->arch.hflags |= HF_SMM_MASK;
3947 else
3948 vcpu->arch.hflags &= ~HF_SMM_MASK;
3949 kvm_smm_changed(vcpu);
3950 }
Xiao Guangrong6ef4e072016-12-24 10:00:42 +01003951
Paolo Bonzinif0778252015-04-01 15:06:40 +02003952 vcpu->arch.smi_pending = events->smi.pending;
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003953
3954 if (events->smi.smm) {
3955 if (events->smi.smm_inside_nmi)
3956 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
Paolo Bonzinif0778252015-04-01 15:06:40 +02003957 else
Wanpeng Lif4ef1912017-08-01 16:05:25 -07003958 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
Liran Alonff90afa2019-11-11 11:16:39 +02003959 }
3960
3961 if (lapic_in_kernel(vcpu)) {
3962 if (events->smi.latched_init)
3963 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3964 else
3965 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
Paolo Bonzinif0778252015-04-01 15:06:40 +02003966 }
3967 }
3968
Avi Kivity3842d132010-07-27 12:30:24 +03003969 kvm_make_request(KVM_REQ_EVENT, vcpu);
3970
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003971 return 0;
3972}
3973
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003974static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3975 struct kvm_debugregs *dbgregs)
3976{
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003977 unsigned long val;
3978
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003979 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003980 kvm_get_dr(vcpu, 6, &val);
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01003981 dbgregs->dr6 = val;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003982 dbgregs->dr7 = vcpu->arch.dr7;
3983 dbgregs->flags = 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04003984 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003985}
3986
3987static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3988 struct kvm_debugregs *dbgregs)
3989{
3990 if (dbgregs->flags)
3991 return -EINVAL;
3992
Paolo Bonzinid14bdb52016-06-01 14:09:23 +02003993 if (dbgregs->dr6 & ~0xffffffffull)
3994 return -EINVAL;
3995 if (dbgregs->dr7 & ~0xffffffffull)
3996 return -EINVAL;
3997
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01003998 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03003999 kvm_update_dr0123(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004000 vcpu->arch.dr6 = dbgregs->dr6;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004001 kvm_update_dr6(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004002 vcpu->arch.dr7 = dbgregs->dr7;
Jan Kiszka9926c9f2014-01-04 18:47:15 +01004003 kvm_update_dr7(vcpu);
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004004
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004005 return 0;
4006}
4007
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004008#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
4009
4010static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
4011{
Marc Orrb666a4b2018-11-06 14:53:56 -08004012 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Ingo Molnar400e4b22015-04-24 10:19:47 +02004013 u64 xstate_bv = xsave->header.xfeatures;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004014 u64 valid;
4015
4016 /*
4017 * Copy legacy XSAVE area, to avoid complications with CPUID
4018 * leaves 0 and 1 in the loop below.
4019 */
4020 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
4021
4022 /* Set XSTATE_BV */
Radim Krčmář00c87e92017-02-01 14:19:53 +01004023 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004024 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
4025
4026 /*
4027 * Copy each region from the possibly compacted offset to the
4028 * non-compacted offset.
4029 */
Dave Hansend91cab72015-09-02 16:31:26 -07004030 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004031 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004032 u64 xfeature_mask = valid & -valid;
4033 int xfeature_nr = fls64(xfeature_mask) - 1;
4034 void *src = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004035
4036 if (src) {
4037 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004038 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004039 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004040 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004041 memcpy(dest + offset, &vcpu->arch.pkru,
4042 sizeof(vcpu->arch.pkru));
4043 else
4044 memcpy(dest + offset, src, size);
4045
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004046 }
4047
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004048 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004049 }
4050}
4051
4052static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
4053{
Marc Orrb666a4b2018-11-06 14:53:56 -08004054 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004055 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
4056 u64 valid;
4057
4058 /*
4059 * Copy legacy XSAVE area, to avoid complications with CPUID
4060 * leaves 0 and 1 in the loop below.
4061 */
4062 memcpy(xsave, src, XSAVE_HDR_OFFSET);
4063
4064 /* Set XSTATE_BV and possibly XCOMP_BV. */
Ingo Molnar400e4b22015-04-24 10:19:47 +02004065 xsave->header.xfeatures = xstate_bv;
Borislav Petkov782511b2016-04-04 22:25:03 +02004066 if (boot_cpu_has(X86_FEATURE_XSAVES))
Ingo Molnar3a544502015-04-24 10:14:36 +02004067 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004068
4069 /*
4070 * Copy each region from the non-compacted offset to the
4071 * possibly compacted offset.
4072 */
Dave Hansend91cab72015-09-02 16:31:26 -07004073 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004074 while (valid) {
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004075 u64 xfeature_mask = valid & -valid;
4076 int xfeature_nr = fls64(xfeature_mask) - 1;
4077 void *dest = get_xsave_addr(xsave, xfeature_nr);
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004078
4079 if (dest) {
4080 u32 size, offset, ecx, edx;
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004081 cpuid_count(XSTATE_CPUID, xfeature_nr,
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004082 &size, &offset, &ecx, &edx);
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004083 if (xfeature_nr == XFEATURE_PKRU)
Paolo Bonzini38cfd5e2017-08-23 23:16:29 +02004084 memcpy(&vcpu->arch.pkru, src + offset,
4085 sizeof(vcpu->arch.pkru));
4086 else
4087 memcpy(dest, src + offset, size);
Wanpeng Liee4100d2015-07-09 15:44:52 +08004088 }
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004089
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02004090 valid -= xfeature_mask;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004091 }
4092}
4093
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004094static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
4095 struct kvm_xsave *guest_xsave)
4096{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004097 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004098 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
4099 fill_xsave((u8 *) guest_xsave->region, vcpu);
Paolo Bonzini4344ee92013-10-02 16:06:16 +02004100 } else {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004101 memcpy(guest_xsave->region,
Marc Orrb666a4b2018-11-06 14:53:56 -08004102 &vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004103 sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004104 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
Dave Hansend91cab72015-09-02 16:31:26 -07004105 XFEATURE_MASK_FPSSE;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004106 }
4107}
4108
Wanpeng Lia5758132017-05-11 02:58:55 -07004109#define XSAVE_MXCSR_OFFSET 24
4110
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004111static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4112 struct kvm_xsave *guest_xsave)
4113{
4114 u64 xstate_bv =
4115 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
Wanpeng Lia5758132017-05-11 02:58:55 -07004116 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004117
Borislav Petkovd366bf72016-04-04 22:25:02 +02004118 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004119 /*
4120 * Here we allow setting states that are not present in
4121 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4122 * with old userspace.
4123 */
Sean Christophersoncfc48182020-03-02 15:56:23 -08004124 if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask)
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004125 return -EINVAL;
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01004126 load_xsave(vcpu, (u8 *)guest_xsave->region);
Paolo Bonzinid7876f12013-10-02 16:06:15 +02004127 } else {
Wanpeng Lia5758132017-05-11 02:58:55 -07004128 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4129 mxcsr & ~mxcsr_feature_mask)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004130 return -EINVAL;
Marc Orrb666a4b2018-11-06 14:53:56 -08004131 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
Ingo Molnarc47ada32015-04-30 17:15:32 +02004132 guest_xsave->region, sizeof(struct fxregs_state));
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004133 }
4134 return 0;
4135}
4136
4137static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4138 struct kvm_xcrs *guest_xcrs)
4139{
Borislav Petkovd366bf72016-04-04 22:25:02 +02004140 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004141 guest_xcrs->nr_xcrs = 0;
4142 return;
4143 }
4144
4145 guest_xcrs->nr_xcrs = 1;
4146 guest_xcrs->flags = 0;
4147 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4148 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4149}
4150
4151static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4152 struct kvm_xcrs *guest_xcrs)
4153{
4154 int i, r = 0;
4155
Borislav Petkovd366bf72016-04-04 22:25:02 +02004156 if (!boot_cpu_has(X86_FEATURE_XSAVE))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004157 return -EINVAL;
4158
4159 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4160 return -EINVAL;
4161
4162 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4163 /* Only support XCR0 currently */
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004164 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004165 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
Paolo Bonzinic67a04c2013-10-17 16:50:47 +02004166 guest_xcrs->xcrs[i].value);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004167 break;
4168 }
4169 if (r)
4170 r = -EINVAL;
4171 return r;
4172}
4173
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004174/*
4175 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4176 * stopped by the hypervisor. This function will be called from the host only.
4177 * EINVAL is returned when the host attempts to set the flag for a guest that
4178 * does not support pv clocks.
4179 */
4180static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4181{
Andy Honig0b794592013-02-20 14:48:10 -08004182 if (!vcpu->arch.pv_time_enabled)
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004183 return -EINVAL;
Marcelo Tosatti51d59c62012-08-03 15:57:49 -03004184 vcpu->arch.pvclock_set_guest_stopped_request = true;
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004185 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4186 return 0;
4187}
4188
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004189static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4190 struct kvm_enable_cap *cap)
4191{
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004192 int r;
4193 uint16_t vmcs_version;
4194 void __user *user_ptr;
4195
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004196 if (cap->flags)
4197 return -EINVAL;
4198
4199 switch (cap->cap) {
Roman Kaganefc479e2017-06-22 16:51:01 +03004200 case KVM_CAP_HYPERV_SYNIC2:
4201 if (cap->args[0])
4202 return -EINVAL;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004203 /* fall through */
4204
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004205 case KVM_CAP_HYPERV_SYNIC:
Wanpeng Li546d87e2017-01-03 18:56:19 -08004206 if (!irqchip_in_kernel(vcpu->kvm))
4207 return -EINVAL;
Roman Kaganefc479e2017-06-22 16:51:01 +03004208 return kvm_hv_activate_synic(vcpu, cap->cap ==
4209 KVM_CAP_HYPERV_SYNIC2);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004210 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
Sean Christopherson51589172018-12-03 13:53:10 -08004211 if (!kvm_x86_ops->nested_enable_evmcs)
4212 return -ENOTTY;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004213 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4214 if (!r) {
4215 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4216 if (copy_to_user(user_ptr, &vmcs_version,
4217 sizeof(vmcs_version)))
4218 r = -EFAULT;
4219 }
4220 return r;
Tianyu Lan344c6c82019-08-22 22:30:20 +08004221 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4222 if (!kvm_x86_ops->enable_direct_tlbflush)
4223 return -ENOTTY;
4224
4225 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004226
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004227 default:
4228 return -EINVAL;
4229 }
4230}
4231
Carsten Otte313a3dc2007-10-11 19:16:52 +02004232long kvm_arch_vcpu_ioctl(struct file *filp,
4233 unsigned int ioctl, unsigned long arg)
4234{
4235 struct kvm_vcpu *vcpu = filp->private_data;
4236 void __user *argp = (void __user *)arg;
4237 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004238 union {
4239 struct kvm_lapic_state *lapic;
4240 struct kvm_xsave *xsave;
4241 struct kvm_xcrs *xcrs;
4242 void *buffer;
4243 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004244
Christoffer Dall9b0624712017-12-04 21:35:36 +01004245 vcpu_load(vcpu);
4246
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004247 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02004248 switch (ioctl) {
4249 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004250 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004251 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004252 goto out;
Ben Gardon254272c2019-02-11 11:02:50 -08004253 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4254 GFP_KERNEL_ACCOUNT);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004255
Dave Hansenb772ff32008-08-11 10:01:47 -07004256 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004257 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07004258 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004259 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004260 if (r)
4261 goto out;
4262 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004263 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004264 goto out;
4265 r = 0;
4266 break;
4267 }
4268 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004269 r = -EINVAL;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01004270 if (!lapic_in_kernel(vcpu))
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02004271 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004272 u.lapic = memdup_user(argp, sizeof(*u.lapic));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004273 if (IS_ERR(u.lapic)) {
4274 r = PTR_ERR(u.lapic);
4275 goto out_nofree;
4276 }
Sasha Levinff5c2c02011-12-04 19:36:29 +02004277
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004278 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004279 break;
4280 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004281 case KVM_INTERRUPT: {
4282 struct kvm_interrupt irq;
4283
4284 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004285 if (copy_from_user(&irq, argp, sizeof(irq)))
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004286 goto out;
4287 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08004288 break;
4289 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004290 case KVM_NMI: {
4291 r = kvm_vcpu_ioctl_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02004292 break;
4293 }
Paolo Bonzinif0778252015-04-01 15:06:40 +02004294 case KVM_SMI: {
4295 r = kvm_vcpu_ioctl_smi(vcpu);
4296 break;
4297 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004298 case KVM_SET_CPUID: {
4299 struct kvm_cpuid __user *cpuid_arg = argp;
4300 struct kvm_cpuid cpuid;
4301
4302 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004303 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02004304 goto out;
4305 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004306 break;
4307 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02004308 case KVM_SET_CPUID2: {
4309 struct kvm_cpuid2 __user *cpuid_arg = argp;
4310 struct kvm_cpuid2 cpuid;
4311
4312 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004313 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004314 goto out;
4315 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004316 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004317 break;
4318 }
4319 case KVM_GET_CPUID2: {
4320 struct kvm_cpuid2 __user *cpuid_arg = argp;
4321 struct kvm_cpuid2 cpuid;
4322
4323 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004324 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004325 goto out;
4326 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00004327 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02004328 if (r)
4329 goto out;
4330 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004331 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004332 goto out;
4333 r = 0;
4334 break;
4335 }
Tom Lendacky801e4592018-02-21 13:39:51 -06004336 case KVM_GET_MSRS: {
4337 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004338 r = msr_io(vcpu, argp, do_get_msr, 1);
Tom Lendacky801e4592018-02-21 13:39:51 -06004339 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004340 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004341 }
4342 case KVM_SET_MSRS: {
4343 int idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004344 r = msr_io(vcpu, argp, do_set_msr, 0);
Tom Lendacky801e4592018-02-21 13:39:51 -06004345 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004346 break;
Tom Lendacky801e4592018-02-21 13:39:51 -06004347 }
Avi Kivityb209749f2007-10-22 16:50:39 +02004348 case KVM_TPR_ACCESS_REPORTING: {
4349 struct kvm_tpr_access_ctl tac;
4350
4351 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004352 if (copy_from_user(&tac, argp, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004353 goto out;
4354 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4355 if (r)
4356 goto out;
4357 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004358 if (copy_to_user(argp, &tac, sizeof(tac)))
Avi Kivityb209749f2007-10-22 16:50:39 +02004359 goto out;
4360 r = 0;
4361 break;
4362 };
Avi Kivityb93463a2007-10-25 16:52:32 +02004363 case KVM_SET_VAPIC_ADDR: {
4364 struct kvm_vapic_addr va;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004365 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004366
4367 r = -EINVAL;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004368 if (!lapic_in_kernel(vcpu))
Avi Kivityb93463a2007-10-25 16:52:32 +02004369 goto out;
4370 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004371 if (copy_from_user(&va, argp, sizeof(va)))
Avi Kivityb93463a2007-10-25 16:52:32 +02004372 goto out;
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004373 idx = srcu_read_lock(&vcpu->kvm->srcu);
Andy Honigfda4e2e2013-11-20 10:23:22 -08004374 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
Paolo Bonzini7301d6a2016-11-17 15:55:46 +01004375 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004376 break;
4377 }
Huang Ying890ca9a2009-05-11 16:48:15 +08004378 case KVM_X86_SETUP_MCE: {
4379 u64 mcg_cap;
4380
4381 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004382 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004383 goto out;
4384 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4385 break;
4386 }
4387 case KVM_X86_SET_MCE: {
4388 struct kvm_x86_mce mce;
4389
4390 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004391 if (copy_from_user(&mce, argp, sizeof(mce)))
Huang Ying890ca9a2009-05-11 16:48:15 +08004392 goto out;
4393 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4394 break;
4395 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004396 case KVM_GET_VCPU_EVENTS: {
4397 struct kvm_vcpu_events events;
4398
4399 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4400
4401 r = -EFAULT;
4402 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4403 break;
4404 r = 0;
4405 break;
4406 }
4407 case KVM_SET_VCPU_EVENTS: {
4408 struct kvm_vcpu_events events;
4409
4410 r = -EFAULT;
4411 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4412 break;
4413
4414 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4415 break;
4416 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01004417 case KVM_GET_DEBUGREGS: {
4418 struct kvm_debugregs dbgregs;
4419
4420 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4421
4422 r = -EFAULT;
4423 if (copy_to_user(argp, &dbgregs,
4424 sizeof(struct kvm_debugregs)))
4425 break;
4426 r = 0;
4427 break;
4428 }
4429 case KVM_SET_DEBUGREGS: {
4430 struct kvm_debugregs dbgregs;
4431
4432 r = -EFAULT;
4433 if (copy_from_user(&dbgregs, argp,
4434 sizeof(struct kvm_debugregs)))
4435 break;
4436
4437 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4438 break;
4439 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004440 case KVM_GET_XSAVE: {
Ben Gardon254272c2019-02-11 11:02:50 -08004441 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004442 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004443 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004444 break;
4445
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004446 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004447
4448 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004449 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004450 break;
4451 r = 0;
4452 break;
4453 }
4454 case KVM_SET_XSAVE: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004455 u.xsave = memdup_user(argp, sizeof(*u.xsave));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004456 if (IS_ERR(u.xsave)) {
4457 r = PTR_ERR(u.xsave);
4458 goto out_nofree;
4459 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004460
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004461 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004462 break;
4463 }
4464 case KVM_GET_XCRS: {
Ben Gardon254272c2019-02-11 11:02:50 -08004465 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004466 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004467 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004468 break;
4469
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004470 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004471
4472 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004473 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004474 sizeof(struct kvm_xcrs)))
4475 break;
4476 r = 0;
4477 break;
4478 }
4479 case KVM_SET_XCRS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02004480 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
Christoffer Dall9b0624712017-12-04 21:35:36 +01004481 if (IS_ERR(u.xcrs)) {
4482 r = PTR_ERR(u.xcrs);
4483 goto out_nofree;
4484 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004485
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004486 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08004487 break;
4488 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01004489 case KVM_SET_TSC_KHZ: {
4490 u32 user_tsc_khz;
4491
4492 r = -EINVAL;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004493 user_tsc_khz = (u32)arg;
4494
4495 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4496 goto out;
4497
Zachary Amsdencc578282012-02-03 15:43:50 -02004498 if (user_tsc_khz == 0)
4499 user_tsc_khz = tsc_khz;
4500
Haozhong Zhang381d5852015-10-20 15:39:04 +08004501 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4502 r = 0;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004503
Joerg Roedel92a1f122011-03-25 09:44:51 +01004504 goto out;
4505 }
4506 case KVM_GET_TSC_KHZ: {
Zachary Amsdencc578282012-02-03 15:43:50 -02004507 r = vcpu->arch.virtual_tsc_khz;
Joerg Roedel92a1f122011-03-25 09:44:51 +01004508 goto out;
4509 }
Eric B Munson1c0b28c2012-03-10 14:37:27 -05004510 case KVM_KVMCLOCK_CTRL: {
4511 r = kvm_set_guest_paused(vcpu);
4512 goto out;
4513 }
Andrey Smetanin5c9194122015-11-10 15:36:34 +03004514 case KVM_ENABLE_CAP: {
4515 struct kvm_enable_cap cap;
4516
4517 r = -EFAULT;
4518 if (copy_from_user(&cap, argp, sizeof(cap)))
4519 goto out;
4520 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4521 break;
4522 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004523 case KVM_GET_NESTED_STATE: {
4524 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4525 u32 user_data_size;
4526
4527 r = -EINVAL;
4528 if (!kvm_x86_ops->get_nested_state)
4529 break;
4530
4531 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
Liran Alon26b471c2018-09-16 14:28:20 +03004532 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004533 if (get_user(user_data_size, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004534 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004535
4536 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4537 user_data_size);
4538 if (r < 0)
Liran Alon26b471c2018-09-16 14:28:20 +03004539 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004540
4541 if (r > user_data_size) {
4542 if (put_user(r, &user_kvm_nested_state->size))
Liran Alon26b471c2018-09-16 14:28:20 +03004543 r = -EFAULT;
4544 else
4545 r = -E2BIG;
4546 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004547 }
Liran Alon26b471c2018-09-16 14:28:20 +03004548
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004549 r = 0;
4550 break;
4551 }
4552 case KVM_SET_NESTED_STATE: {
4553 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4554 struct kvm_nested_state kvm_state;
Sean Christophersonad5996d2019-11-22 08:58:18 -08004555 int idx;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004556
4557 r = -EINVAL;
4558 if (!kvm_x86_ops->set_nested_state)
4559 break;
4560
Liran Alon26b471c2018-09-16 14:28:20 +03004561 r = -EFAULT;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004562 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
Liran Alon26b471c2018-09-16 14:28:20 +03004563 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004564
Liran Alon26b471c2018-09-16 14:28:20 +03004565 r = -EINVAL;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004566 if (kvm_state.size < sizeof(kvm_state))
Liran Alon26b471c2018-09-16 14:28:20 +03004567 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004568
4569 if (kvm_state.flags &
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004570 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4571 | KVM_STATE_NESTED_EVMCS))
Liran Alon26b471c2018-09-16 14:28:20 +03004572 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004573
4574 /* nested_run_pending implies guest_mode. */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02004575 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4576 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
Liran Alon26b471c2018-09-16 14:28:20 +03004577 break;
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004578
Sean Christophersonad5996d2019-11-22 08:58:18 -08004579 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004580 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
Sean Christophersonad5996d2019-11-22 08:58:18 -08004581 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Jim Mattson8fcc4b52018-07-10 11:27:20 +02004582 break;
4583 }
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01004584 case KVM_GET_SUPPORTED_HV_CPUID: {
4585 struct kvm_cpuid2 __user *cpuid_arg = argp;
4586 struct kvm_cpuid2 cpuid;
4587
4588 r = -EFAULT;
4589 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4590 goto out;
4591
4592 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4593 cpuid_arg->entries);
4594 if (r)
4595 goto out;
4596
4597 r = -EFAULT;
4598 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4599 goto out;
4600 r = 0;
4601 break;
4602 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02004603 default:
4604 r = -EINVAL;
4605 }
4606out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03004607 kfree(u.buffer);
Christoffer Dall9b0624712017-12-04 21:35:36 +01004608out_nofree:
4609 vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02004610 return r;
4611}
4612
Souptick Joarder1499fa82018-04-19 00:49:58 +05304613vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Carsten Otte5b1c1492012-01-04 10:25:23 +01004614{
4615 return VM_FAULT_SIGBUS;
4616}
4617
Carsten Otte1fe779f2007-10-29 16:08:35 +01004618static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4619{
4620 int ret;
4621
4622 if (addr > (unsigned int)(-3 * PAGE_SIZE))
Guo Chao951179c2012-11-02 18:33:22 +08004623 return -EINVAL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004624 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4625 return ret;
4626}
4627
Sheng Yangb927a3c2009-07-21 10:42:48 +08004628static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4629 u64 ident_addr)
4630{
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004631 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004632}
4633
Carsten Otte1fe779f2007-10-29 16:08:35 +01004634static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004635 unsigned long kvm_nr_mmu_pages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004636{
4637 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4638 return -EINVAL;
4639
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004640 mutex_lock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004641
4642 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004643 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004644
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004645 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004646 return 0;
4647}
4648
Ben Gardonbc8a3d82019-04-08 11:07:30 -07004649static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
Carsten Otte1fe779f2007-10-29 16:08:35 +01004650{
Dave Hansen39de71e2010-08-19 18:11:14 -07004651 return kvm->arch.n_max_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004652}
4653
Carsten Otte1fe779f2007-10-29 16:08:35 +01004654static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4655{
David Hildenbrand90bca052017-04-07 10:50:23 +02004656 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004657 int r;
4658
4659 r = 0;
4660 switch (chip->chip_id) {
4661 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004662 memcpy(&chip->chip.pic, &pic->pics[0],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004663 sizeof(struct kvm_pic_state));
4664 break;
4665 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004666 memcpy(&chip->chip.pic, &pic->pics[1],
Carsten Otte1fe779f2007-10-29 16:08:35 +01004667 sizeof(struct kvm_pic_state));
4668 break;
4669 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004670 kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004671 break;
4672 default:
4673 r = -EINVAL;
4674 break;
4675 }
4676 return r;
4677}
4678
4679static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4680{
David Hildenbrand90bca052017-04-07 10:50:23 +02004681 struct kvm_pic *pic = kvm->arch.vpic;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004682 int r;
4683
4684 r = 0;
4685 switch (chip->chip_id) {
4686 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand90bca052017-04-07 10:50:23 +02004687 spin_lock(&pic->lock);
4688 memcpy(&pic->pics[0], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004689 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004690 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004691 break;
4692 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand90bca052017-04-07 10:50:23 +02004693 spin_lock(&pic->lock);
4694 memcpy(&pic->pics[1], &chip->chip.pic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01004695 sizeof(struct kvm_pic_state));
David Hildenbrand90bca052017-04-07 10:50:23 +02004696 spin_unlock(&pic->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004697 break;
4698 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand33392b42017-04-07 10:50:27 +02004699 kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004700 break;
4701 default:
4702 r = -EINVAL;
4703 break;
4704 }
David Hildenbrand90bca052017-04-07 10:50:23 +02004705 kvm_pic_update_irq(pic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004706 return r;
4707}
4708
Sheng Yange0f63cb2008-03-04 00:50:59 +08004709static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4710{
Radim Krčmář34f39412016-03-02 22:56:50 +01004711 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4712
4713 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4714
4715 mutex_lock(&kps->lock);
4716 memcpy(ps, &kps->channels, sizeof(*ps));
4717 mutex_unlock(&kps->lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304718 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004719}
4720
4721static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4722{
Andrew Honig01856042015-11-18 14:50:23 -08004723 int i;
Radim Krčmář09edea72016-03-02 22:56:43 +01004724 struct kvm_pit *pit = kvm->arch.vpit;
4725
4726 mutex_lock(&pit->pit_state.lock);
Radim Krčmář34f39412016-03-02 22:56:50 +01004727 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
Andrew Honig01856042015-11-18 14:50:23 -08004728 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004729 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4730 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304731 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004732}
4733
4734static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4735{
Beth Kone9f42752009-07-07 11:50:38 -04004736 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4737 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4738 sizeof(ps->channels));
4739 ps->flags = kvm->arch.vpit->pit_state.flags;
4740 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04004741 memset(&ps->reserved, 0, sizeof(ps->reserved));
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304742 return 0;
Beth Kone9f42752009-07-07 11:50:38 -04004743}
4744
4745static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4746{
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304747 int start = 0;
Andrew Honig01856042015-11-18 14:50:23 -08004748 int i;
Beth Kone9f42752009-07-07 11:50:38 -04004749 u32 prev_legacy, cur_legacy;
Radim Krčmář09edea72016-03-02 22:56:43 +01004750 struct kvm_pit *pit = kvm->arch.vpit;
4751
4752 mutex_lock(&pit->pit_state.lock);
4753 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
Beth Kone9f42752009-07-07 11:50:38 -04004754 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4755 if (!prev_legacy && cur_legacy)
4756 start = 1;
Radim Krčmář09edea72016-03-02 22:56:43 +01004757 memcpy(&pit->pit_state.channels, &ps->channels,
4758 sizeof(pit->pit_state.channels));
4759 pit->pit_state.flags = ps->flags;
Andrew Honig01856042015-11-18 14:50:23 -08004760 for (i = 0; i < 3; i++)
Radim Krčmář09edea72016-03-02 22:56:43 +01004761 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
Paolo Bonzinie5e57e72016-01-07 13:50:38 +01004762 start && i == 0);
Radim Krčmář09edea72016-03-02 22:56:43 +01004763 mutex_unlock(&pit->pit_state.lock);
Saurabh Sengar2da29bc2015-10-30 12:56:11 +05304764 return 0;
Sheng Yange0f63cb2008-03-04 00:50:59 +08004765}
4766
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004767static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4768 struct kvm_reinject_control *control)
4769{
Radim Krčmář71474e22016-03-02 22:56:45 +01004770 struct kvm_pit *pit = kvm->arch.vpit;
4771
Radim Krčmář71474e22016-03-02 22:56:45 +01004772 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4773 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4774 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4775 */
4776 mutex_lock(&pit->pit_state.lock);
4777 kvm_pit_set_reinject(pit, control->pit_reinject);
4778 mutex_unlock(&pit->pit_state.lock);
Radim Krčmářb39c90b2016-03-02 22:56:44 +01004779
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02004780 return 0;
4781}
4782
Sean Christopherson0dff0842020-02-18 13:07:29 -08004783void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004784{
Kai Huang88178fd2015-01-28 10:54:27 +08004785 /*
4786 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4787 */
4788 if (kvm_x86_ops->flush_log_dirty)
4789 kvm_x86_ops->flush_log_dirty(kvm);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08004790}
4791
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004792int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4793 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004794{
4795 if (!irqchip_in_kernel(kvm))
4796 return -ENXIO;
4797
4798 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +08004799 irq_event->irq, irq_event->level,
4800 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04004801 return 0;
4802}
4803
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01004804int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4805 struct kvm_enable_cap *cap)
Nadav Amit90de4a12015-04-13 01:53:41 +03004806{
4807 int r;
4808
4809 if (cap->flags)
4810 return -EINVAL;
4811
4812 switch (cap->cap) {
4813 case KVM_CAP_DISABLE_QUIRKS:
4814 kvm->arch.disabled_quirks = cap->args[0];
4815 r = 0;
4816 break;
Steve Rutherford49df6392015-07-29 23:21:40 -07004817 case KVM_CAP_SPLIT_IRQCHIP: {
4818 mutex_lock(&kvm->lock);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004819 r = -EINVAL;
4820 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4821 goto split_irqchip_unlock;
Steve Rutherford49df6392015-07-29 23:21:40 -07004822 r = -EEXIST;
4823 if (irqchip_in_kernel(kvm))
4824 goto split_irqchip_unlock;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004825 if (kvm->created_vcpus)
Steve Rutherford49df6392015-07-29 23:21:40 -07004826 goto split_irqchip_unlock;
4827 r = kvm_setup_empty_irq_routing(kvm);
David Hildenbrand5c0aea02017-04-28 17:06:20 +02004828 if (r)
Steve Rutherford49df6392015-07-29 23:21:40 -07004829 goto split_irqchip_unlock;
4830 /* Pairs with irqchip_in_kernel. */
4831 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004832 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07004833 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
Steve Rutherford49df6392015-07-29 23:21:40 -07004834 r = 0;
4835split_irqchip_unlock:
4836 mutex_unlock(&kvm->lock);
4837 break;
4838 }
Radim Krčmář371313132016-07-12 22:09:27 +02004839 case KVM_CAP_X2APIC_API:
4840 r = -EINVAL;
4841 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4842 break;
4843
4844 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4845 kvm->arch.x2apic_format = true;
Radim Krčmářc5192652016-07-12 22:09:28 +02004846 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4847 kvm->arch.x2apic_broadcast_quirk_disabled = true;
Radim Krčmář371313132016-07-12 22:09:27 +02004848
4849 r = 0;
4850 break;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004851 case KVM_CAP_X86_DISABLE_EXITS:
4852 r = -EINVAL;
4853 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4854 break;
4855
4856 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4857 kvm_can_mwait_in_guest())
4858 kvm->arch.mwait_in_guest = true;
Michael S. Tsirkin766d3572018-06-08 02:19:53 +03004859 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
Wanpeng Licaa057a2018-03-12 04:53:03 -07004860 kvm->arch.hlt_in_guest = true;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004861 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4862 kvm->arch.pause_in_guest = true;
Wanpeng Lib5170062019-05-21 14:06:53 +08004863 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4864 kvm->arch.cstate_in_guest = true;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004865 r = 0;
4866 break;
Drew Schmitt6fbbde92018-08-20 10:32:15 -07004867 case KVM_CAP_MSR_PLATFORM_INFO:
4868 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4869 r = 0;
4870 break;
Jim Mattsonc4f55192018-10-16 14:29:24 -07004871 case KVM_CAP_EXCEPTION_PAYLOAD:
4872 kvm->arch.exception_payload_enabled = cap->args[0];
4873 r = 0;
4874 break;
Nadav Amit90de4a12015-04-13 01:53:41 +03004875 default:
4876 r = -EINVAL;
4877 break;
4878 }
4879 return r;
4880}
4881
Carsten Otte1fe779f2007-10-29 16:08:35 +01004882long kvm_arch_vm_ioctl(struct file *filp,
4883 unsigned int ioctl, unsigned long arg)
4884{
4885 struct kvm *kvm = filp->private_data;
4886 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03004887 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07004888 /*
4889 * This union makes it completely explicit to gcc-3.x
4890 * that these two variables' stack usage should be
4891 * combined, not added together.
4892 */
4893 union {
4894 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04004895 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004896 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07004897 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004898
4899 switch (ioctl) {
4900 case KVM_SET_TSS_ADDR:
4901 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004902 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004903 case KVM_SET_IDENTITY_MAP_ADDR: {
4904 u64 ident_addr;
4905
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004906 mutex_lock(&kvm->lock);
4907 r = -EINVAL;
4908 if (kvm->created_vcpus)
4909 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004910 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004911 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004912 goto set_identity_unlock;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004913 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
David Hildenbrand1af1ac92017-08-24 20:51:36 +02004914set_identity_unlock:
4915 mutex_unlock(&kvm->lock);
Sheng Yangb927a3c2009-07-21 10:42:48 +08004916 break;
4917 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01004918 case KVM_SET_NR_MMU_PAGES:
4919 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004920 break;
4921 case KVM_GET_NR_MMU_PAGES:
4922 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4923 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004924 case KVM_CREATE_IRQCHIP: {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004925 mutex_lock(&kvm->lock);
Radim Krčmář09941362016-12-16 16:10:03 +01004926
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004927 r = -EEXIST;
Radim Krčmář35e6eaa2016-12-16 16:10:01 +01004928 if (irqchip_in_kernel(kvm))
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004929 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004930
Avi Kivity3e515702012-03-05 14:23:29 +02004931 r = -EINVAL;
Paolo Bonzini557abc42016-06-13 14:50:04 +02004932 if (kvm->created_vcpus)
Avi Kivity3e515702012-03-05 14:23:29 +02004933 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004934
4935 r = kvm_pic_init(kvm);
4936 if (r)
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004937 goto create_irqchip_unlock;
Radim Krčmář09941362016-12-16 16:10:03 +01004938
4939 r = kvm_ioapic_init(kvm);
4940 if (r) {
Radim Krčmář09941362016-12-16 16:10:03 +01004941 kvm_pic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004942 goto create_irqchip_unlock;
4943 }
4944
Avi Kivity399ec802008-11-19 13:58:46 +02004945 r = kvm_setup_default_irq_routing(kvm);
4946 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08004947 kvm_ioapic_destroy(kvm);
Radim Krčmář09941362016-12-16 16:10:03 +01004948 kvm_pic_destroy(kvm);
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004949 goto create_irqchip_unlock;
Avi Kivity399ec802008-11-19 13:58:46 +02004950 }
Radim Krčmář49776fa2016-12-16 16:10:02 +01004951 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Paolo Bonzini71ba9942015-07-29 12:31:15 +02004952 smp_wmb();
Radim Krčmář49776fa2016-12-16 16:10:02 +01004953 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004954 create_irqchip_unlock:
4955 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004956 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02004957 }
Sheng Yang78376992008-01-28 05:10:22 +08004958 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004959 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4960 goto create_pit;
4961 case KVM_CREATE_PIT2:
4962 r = -EFAULT;
4963 if (copy_from_user(&u.pit_config, argp,
4964 sizeof(struct kvm_pit_config)))
4965 goto out;
4966 create_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004967 mutex_lock(&kvm->lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02004968 r = -EEXIST;
4969 if (kvm->arch.vpit)
4970 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08004971 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02004972 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08004973 if (kvm->arch.vpit)
4974 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02004975 create_pit_unlock:
Paolo Bonzini250715a2016-06-01 14:09:24 +02004976 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08004977 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004978 case KVM_GET_IRQCHIP: {
4979 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02004980 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01004981
Sasha Levinff5c2c02011-12-04 19:36:29 +02004982 chip = memdup_user(argp, sizeof(*chip));
4983 if (IS_ERR(chip)) {
4984 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01004985 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02004986 }
4987
Carsten Otte1fe779f2007-10-29 16:08:35 +01004988 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01004989 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07004990 goto get_irqchip_out;
4991 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
4992 if (r)
4993 goto get_irqchip_out;
4994 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00004995 if (copy_to_user(argp, chip, sizeof(*chip)))
Dave Hansenf0d66272008-08-11 10:01:45 -07004996 goto get_irqchip_out;
4997 r = 0;
4998 get_irqchip_out:
4999 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005000 break;
5001 }
5002 case KVM_SET_IRQCHIP: {
5003 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Sasha Levinff5c2c02011-12-04 19:36:29 +02005004 struct kvm_irqchip *chip;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005005
Sasha Levinff5c2c02011-12-04 19:36:29 +02005006 chip = memdup_user(argp, sizeof(*chip));
5007 if (IS_ERR(chip)) {
5008 r = PTR_ERR(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005009 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02005010 }
5011
Carsten Otte1fe779f2007-10-29 16:08:35 +01005012 r = -ENXIO;
Radim Krčmář826da322016-12-16 16:10:06 +01005013 if (!irqchip_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07005014 goto set_irqchip_out;
5015 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
Dave Hansenf0d66272008-08-11 10:01:45 -07005016 set_irqchip_out:
5017 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01005018 break;
5019 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08005020 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005021 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005022 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005023 goto out;
5024 r = -ENXIO;
5025 if (!kvm->arch.vpit)
5026 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005027 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005028 if (r)
5029 goto out;
5030 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07005031 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005032 goto out;
5033 r = 0;
5034 break;
5035 }
5036 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08005037 r = -EFAULT;
Jordan Borgner0e96f312018-10-28 12:58:28 +00005038 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08005039 goto out;
5040 r = -ENXIO;
5041 if (!kvm->arch.vpit)
5042 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07005043 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08005044 break;
5045 }
Beth Kone9f42752009-07-07 11:50:38 -04005046 case KVM_GET_PIT2: {
5047 r = -ENXIO;
5048 if (!kvm->arch.vpit)
5049 goto out;
5050 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5051 if (r)
5052 goto out;
5053 r = -EFAULT;
5054 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5055 goto out;
5056 r = 0;
5057 break;
5058 }
5059 case KVM_SET_PIT2: {
5060 r = -EFAULT;
5061 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5062 goto out;
5063 r = -ENXIO;
5064 if (!kvm->arch.vpit)
5065 goto out;
5066 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
Beth Kone9f42752009-07-07 11:50:38 -04005067 break;
5068 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005069 case KVM_REINJECT_CONTROL: {
5070 struct kvm_reinject_control control;
5071 r = -EFAULT;
5072 if (copy_from_user(&control, argp, sizeof(control)))
5073 goto out;
Miaohe Lincad23e72019-12-07 17:25:22 +08005074 r = -ENXIO;
5075 if (!kvm->arch.vpit)
5076 goto out;
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005077 r = kvm_vm_ioctl_reinject(kvm, &control);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02005078 break;
5079 }
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005080 case KVM_SET_BOOT_CPU_ID:
5081 r = 0;
5082 mutex_lock(&kvm->lock);
Paolo Bonzini557abc42016-06-13 14:50:04 +02005083 if (kvm->created_vcpus)
Paolo Bonzinid71ba782015-07-29 11:56:48 +02005084 r = -EBUSY;
5085 else
5086 kvm->arch.bsp_vcpu_id = arg;
5087 mutex_unlock(&kvm->lock);
5088 break;
Ed Swierkffde22a2009-10-15 15:21:43 -07005089 case KVM_XEN_HVM_CONFIG: {
Paolo Bonzini51776042017-10-26 15:45:47 +02005090 struct kvm_xen_hvm_config xhc;
Ed Swierkffde22a2009-10-15 15:21:43 -07005091 r = -EFAULT;
Paolo Bonzini51776042017-10-26 15:45:47 +02005092 if (copy_from_user(&xhc, argp, sizeof(xhc)))
Ed Swierkffde22a2009-10-15 15:21:43 -07005093 goto out;
5094 r = -EINVAL;
Paolo Bonzini51776042017-10-26 15:45:47 +02005095 if (xhc.flags)
Ed Swierkffde22a2009-10-15 15:21:43 -07005096 goto out;
Paolo Bonzini51776042017-10-26 15:45:47 +02005097 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
Ed Swierkffde22a2009-10-15 15:21:43 -07005098 r = 0;
5099 break;
5100 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005101 case KVM_SET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005102 struct kvm_clock_data user_ns;
5103 u64 now_ns;
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005104
5105 r = -EFAULT;
5106 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5107 goto out;
5108
5109 r = -EINVAL;
5110 if (user_ns.flags)
5111 goto out;
5112
5113 r = 0;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005114 /*
5115 * TODO: userspace has to take care of races with VCPU_RUN, so
5116 * kvm_gen_update_masterclock() can be cut down to locked
5117 * pvclock_update_vm_gtod_copy().
5118 */
5119 kvm_gen_update_masterclock(kvm);
Marcelo Tosattie891a322017-04-17 12:51:37 -03005120 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005121 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
Radim Krčmář0bc48be2017-05-16 22:50:00 +02005122 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005123 break;
5124 }
5125 case KVM_GET_CLOCK: {
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005126 struct kvm_clock_data user_ns;
5127 u64 now_ns;
5128
Marcelo Tosattie891a322017-04-17 12:51:37 -03005129 now_ns = get_kvmclock_ns(kvm);
Paolo Bonzini108b2492016-09-01 14:21:03 +02005130 user_ns.clock = now_ns;
Paolo Bonzinie3fd9a92016-11-09 17:48:15 +01005131 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
Vasiliy Kulikov97e69aa2010-10-30 22:54:47 +04005132 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005133
5134 r = -EFAULT;
5135 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5136 goto out;
5137 r = 0;
5138 break;
5139 }
Brijesh Singh5acc5c02017-12-04 10:57:26 -06005140 case KVM_MEMORY_ENCRYPT_OP: {
5141 r = -ENOTTY;
5142 if (kvm_x86_ops->mem_enc_op)
5143 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5144 break;
5145 }
Brijesh Singh69eaede2017-12-04 10:57:26 -06005146 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5147 struct kvm_enc_region region;
5148
5149 r = -EFAULT;
5150 if (copy_from_user(&region, argp, sizeof(region)))
5151 goto out;
5152
5153 r = -ENOTTY;
5154 if (kvm_x86_ops->mem_enc_reg_region)
5155 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5156 break;
5157 }
5158 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5159 struct kvm_enc_region region;
5160
5161 r = -EFAULT;
5162 if (copy_from_user(&region, argp, sizeof(region)))
5163 goto out;
5164
5165 r = -ENOTTY;
5166 if (kvm_x86_ops->mem_enc_unreg_region)
5167 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5168 break;
5169 }
Roman Kaganfaeb7832018-02-01 16:48:32 +03005170 case KVM_HYPERV_EVENTFD: {
5171 struct kvm_hyperv_eventfd hvevfd;
5172
5173 r = -EFAULT;
5174 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5175 goto out;
5176 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5177 break;
5178 }
Eric Hankland66bb8a02019-07-10 18:25:15 -07005179 case KVM_SET_PMU_EVENT_FILTER:
5180 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5181 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005182 default:
Paolo Bonziniad6260d2017-03-27 14:30:40 +02005183 r = -ENOTTY;
Carsten Otte1fe779f2007-10-29 16:08:35 +01005184 }
5185out:
5186 return r;
5187}
5188
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08005189static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02005190{
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005191 struct x86_pmu_capability x86_pmu;
Carsten Otte043405e2007-10-10 17:16:19 +02005192 u32 dummy[2];
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005193 unsigned i;
Carsten Otte043405e2007-10-10 17:16:19 +02005194
Jim Mattsone2ada662019-08-21 11:20:04 -07005195 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005196 "Please update the fixed PMCs in msrs_to_saved_all[]");
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005197
5198 perf_get_x86_pmu_capability(&x86_pmu);
Jim Mattsone2ada662019-08-21 11:20:04 -07005199
Xiaoyao Li6cbee2b2019-11-13 09:15:21 +08005200 num_msrs_to_save = 0;
5201 num_emulated_msrs = 0;
5202 num_msr_based_features = 0;
5203
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005204 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5205 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
Carsten Otte043405e2007-10-10 17:16:19 +02005206 continue;
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005207
5208 /*
5209 * Even MSRs that are valid in the host may not be exposed
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005210 * to the guests in some cases.
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005211 */
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005212 switch (msrs_to_save_all[i]) {
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005213 case MSR_IA32_BNDCFGS:
Liran Alon503234b2018-09-14 03:25:53 +03005214 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005215 continue;
5216 break;
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005217 case MSR_TSC_AUX:
Sean Christopherson13908512020-03-02 15:56:57 -08005218 if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
Paolo Bonzini9dbe6cf2015-11-12 14:49:17 +01005219 continue;
5220 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005221 case MSR_IA32_RTIT_CTL:
5222 case MSR_IA32_RTIT_STATUS:
5223 if (!kvm_x86_ops->pt_supported())
5224 continue;
5225 break;
5226 case MSR_IA32_RTIT_CR3_MATCH:
5227 if (!kvm_x86_ops->pt_supported() ||
5228 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5229 continue;
5230 break;
5231 case MSR_IA32_RTIT_OUTPUT_BASE:
5232 case MSR_IA32_RTIT_OUTPUT_MASK:
5233 if (!kvm_x86_ops->pt_supported() ||
5234 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5235 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5236 continue;
5237 break;
5238 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
5239 if (!kvm_x86_ops->pt_supported() ||
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005240 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
Chao Pengbf8c55d2018-10-24 16:05:14 +08005241 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5242 continue;
5243 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005244 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005245 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005246 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5247 continue;
5248 break;
Paolo Bonzinicf05a672019-10-01 15:33:07 +02005249 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005250 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
Paolo Bonzini24c29b72019-10-01 15:18:26 +02005251 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5252 continue;
Chao Pengbf8c55d2018-10-24 16:05:14 +08005253 }
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005254 default:
5255 break;
5256 }
5257
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005258 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
Carsten Otte043405e2007-10-10 17:16:19 +02005259 }
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005260
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005261 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5262 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
Tom Lendackybc226f02018-05-10 22:06:39 +02005263 continue;
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005264
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005265 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Paolo Bonzini62ef68b2015-05-05 12:08:55 +02005266 }
Tom Lendacky801e4592018-02-21 13:39:51 -06005267
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005268 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
Tom Lendacky801e4592018-02-21 13:39:51 -06005269 struct kvm_msr_entry msr;
5270
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005271 msr.index = msr_based_features_all[i];
Wanpeng Li66421c12018-02-28 14:03:30 +08005272 if (kvm_get_msr_feature(&msr))
Tom Lendacky801e4592018-02-21 13:39:51 -06005273 continue;
5274
Chenyi Qiang7a5ee6e2019-11-06 14:35:20 +08005275 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
Tom Lendacky801e4592018-02-21 13:39:51 -06005276 }
Carsten Otte043405e2007-10-10 17:16:19 +02005277}
5278
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005279static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5280 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005281{
Avi Kivity70252a12010-01-19 12:51:22 +02005282 int handled = 0;
5283 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005284
Avi Kivity70252a12010-01-19 12:51:22 +02005285 do {
5286 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005287 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005288 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5289 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005290 break;
5291 handled += n;
5292 addr += n;
5293 len -= n;
5294 v += n;
5295 } while (len);
5296
5297 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005298}
5299
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03005300static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005301{
Avi Kivity70252a12010-01-19 12:51:22 +02005302 int handled = 0;
5303 int n;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005304
Avi Kivity70252a12010-01-19 12:51:22 +02005305 do {
5306 n = min(len, 8);
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01005307 if (!(lapic_in_kernel(vcpu) &&
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005308 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5309 addr, n, v))
5310 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
Avi Kivity70252a12010-01-19 12:51:22 +02005311 break;
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005312 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
Avi Kivity70252a12010-01-19 12:51:22 +02005313 handled += n;
5314 addr += n;
5315 len -= n;
5316 v += n;
5317 } while (len);
5318
5319 return handled;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005320}
5321
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02005322static void kvm_set_segment(struct kvm_vcpu *vcpu,
5323 struct kvm_segment *var, int seg)
5324{
5325 kvm_x86_ops->set_segment(vcpu, var, seg);
5326}
5327
5328void kvm_get_segment(struct kvm_vcpu *vcpu,
5329 struct kvm_segment *var, int seg)
5330{
5331 kvm_x86_ops->get_segment(vcpu, var, seg);
5332}
5333
Paolo Bonzini54987b72014-09-02 13:23:06 +02005334gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5335 struct x86_exception *exception)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005336{
5337 gpa_t t_gpa;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005338
5339 BUG_ON(!mmu_is_nested(vcpu));
5340
5341 /* NPT walks are always user-walks */
5342 access |= PFERR_USER_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005343 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005344
5345 return t_gpa;
5346}
5347
Avi Kivityab9ae312010-11-22 17:53:26 +02005348gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5349 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005350{
5351 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivityab9ae312010-11-22 17:53:26 +02005352 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005353}
5354
Avi Kivityab9ae312010-11-22 17:53:26 +02005355 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5356 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005357{
5358 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5359 access |= PFERR_FETCH_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005360 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005361}
5362
Avi Kivityab9ae312010-11-22 17:53:26 +02005363gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5364 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005365{
5366 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5367 access |= PFERR_WRITE_MASK;
Avi Kivityab9ae312010-11-22 17:53:26 +02005368 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005369}
5370
5371/* uses this to access any guest's mapped memory without checking CPL */
Avi Kivityab9ae312010-11-22 17:53:26 +02005372gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5373 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005374{
Avi Kivityab9ae312010-11-22 17:53:26 +02005375 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005376}
5377
5378static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5379 struct kvm_vcpu *vcpu, u32 access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005380 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005381{
5382 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005383 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005384
5385 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005386 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005387 exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005388 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02005389 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005390 int ret;
5391
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005392 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005393 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005394 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5395 offset, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005396 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005397 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005398 goto out;
5399 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01005400
Izik Eidus77c20022008-12-29 01:42:19 +02005401 bytes -= toread;
5402 data += toread;
5403 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005404 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005405out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005406 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005407}
Izik Eidus77c20022008-12-29 01:42:19 +02005408
Gleb Natapov1871c602010-02-10 14:21:32 +02005409/* used for instruction fetching */
Avi Kivity0f65dd72011-04-20 13:37:53 +03005410static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5411 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005412 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005413{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005414 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005415 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005416 unsigned offset;
5417 int ret;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005418
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005419 /* Inline kvm_read_guest_virt_helper for speed. */
5420 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5421 exception);
5422 if (unlikely(gpa == UNMAPPED_GVA))
5423 return X86EMUL_PROPAGATE_FAULT;
5424
5425 offset = addr & (PAGE_SIZE-1);
5426 if (WARN_ON(offset + bytes > PAGE_SIZE))
5427 bytes = (unsigned)PAGE_SIZE - offset;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005428 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5429 offset, bytes);
Paolo Bonzini44583cb2014-05-13 14:02:13 +02005430 if (unlikely(ret < 0))
5431 return X86EMUL_IO_NEEDED;
5432
5433 return X86EMUL_CONTINUE;
Gleb Natapov1871c602010-02-10 14:21:32 +02005434}
5435
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005436int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005437 gva_t addr, void *val, unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005438 struct x86_exception *exception)
Gleb Natapov1871c602010-02-10 14:21:32 +02005439{
5440 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005441
Paolo Bonzini353c0952019-01-29 18:41:16 +01005442 /*
5443 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5444 * is returned, but our callers are not ready for that and they blindly
5445 * call kvm_inject_page_fault. Ensure that they at least do not leak
5446 * uninitialized kernel stack memory into cr2 and error code.
5447 */
5448 memset(exception, 0, sizeof(*exception));
Gleb Natapov1871c602010-02-10 14:21:32 +02005449 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005450 exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005451}
Nadav Har'El064aea72011-05-25 23:04:56 +03005452EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
Gleb Natapov1871c602010-02-10 14:21:32 +02005453
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005454static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5455 gva_t addr, void *val, unsigned int bytes,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005456 struct x86_exception *exception, bool system)
Gleb Natapov1871c602010-02-10 14:21:32 +02005457{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005458 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005459 u32 access = 0;
5460
5461 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5462 access |= PFERR_USER_MASK;
5463
5464 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
Gleb Natapov1871c602010-02-10 14:21:32 +02005465}
5466
Radim Krčmář7a036a62015-10-30 16:36:24 +01005467static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5468 unsigned long addr, void *val, unsigned int bytes)
5469{
5470 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5471 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5472
5473 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5474}
5475
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005476static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5477 struct kvm_vcpu *vcpu, u32 access,
5478 struct x86_exception *exception)
Izik Eidus77c20022008-12-29 01:42:19 +02005479{
5480 void *data = val;
5481 int r = X86EMUL_CONTINUE;
5482
5483 while (bytes) {
Joerg Roedel14dfe852010-09-10 17:30:49 +02005484 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005485 access,
Avi Kivityab9ae312010-11-22 17:53:26 +02005486 exception);
Izik Eidus77c20022008-12-29 01:42:19 +02005487 unsigned offset = addr & (PAGE_SIZE-1);
5488 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5489 int ret;
5490
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005491 if (gpa == UNMAPPED_GVA)
Avi Kivityab9ae312010-11-22 17:53:26 +02005492 return X86EMUL_PROPAGATE_FAULT;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005493 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
Izik Eidus77c20022008-12-29 01:42:19 +02005494 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03005495 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02005496 goto out;
5497 }
5498
5499 bytes -= towrite;
5500 data += towrite;
5501 addr += towrite;
5502 }
5503out:
5504 return r;
5505}
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005506
5507static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005508 unsigned int bytes, struct x86_exception *exception,
5509 bool system)
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005510{
5511 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005512 u32 access = PFERR_WRITE_MASK;
5513
5514 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5515 access |= PFERR_USER_MASK;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005516
5517 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005518 access, exception);
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005519}
5520
5521int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5522 unsigned int bytes, struct x86_exception *exception)
5523{
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005524 /* kvm_write_guest_virt_system can pull in tons of pages. */
5525 vcpu->arch.l1tf_flush_l1d = true;
5526
Fuqian Huang541ab2a2019-09-12 12:18:17 +08005527 /*
5528 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5529 * is returned, but our callers are not ready for that and they blindly
5530 * call kvm_inject_page_fault. Ensure that they at least do not leak
5531 * uninitialized kernel stack memory into cr2 and error code.
5532 */
5533 memset(exception, 0, sizeof(*exception));
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02005534 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5535 PFERR_WRITE_MASK, exception);
5536}
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005537EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
Izik Eidus77c20022008-12-29 01:42:19 +02005538
Wanpeng Li082d06e2018-04-03 16:28:48 -07005539int handle_ud(struct kvm_vcpu *vcpu)
5540{
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005541 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005542 int emul_type = EMULTYPE_TRAP_UD;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005543 char sig[5]; /* ud2; .ascii "kvm" */
5544 struct x86_exception e;
Wanpeng Li082d06e2018-04-03 16:28:48 -07005545
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005546 if (force_emulation_prefix &&
Paolo Bonzini3c9fa242018-06-06 17:38:09 +02005547 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5548 sig, sizeof(sig), &e) == 0 &&
Masami Hiramatsub3dc0692019-09-06 22:13:59 +09005549 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005550 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
Sean Christophersonb4000602019-08-27 14:40:32 -07005551 emul_type = EMULTYPE_TRAP_UD_FORCED;
Wanpeng Li6c86eed2018-04-03 16:28:49 -07005552 }
5553
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005554 return kvm_emulate_instruction(vcpu, emul_type);
Wanpeng Li082d06e2018-04-03 16:28:48 -07005555}
5556EXPORT_SYMBOL_GPL(handle_ud);
5557
Tom Lendacky0f89b202016-12-14 14:59:23 -05005558static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5559 gpa_t gpa, bool write)
5560{
5561 /* For APIC access vmexit */
5562 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5563 return 1;
5564
5565 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5566 trace_vcpu_match_mmio(gva, gpa, write, true);
5567 return 1;
5568 }
5569
5570 return 0;
5571}
5572
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005573static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5574 gpa_t *gpa, struct x86_exception *exception,
5575 bool write)
5576{
Avi Kivity97d64b72012-09-12 14:52:00 +03005577 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5578 | (write ? PFERR_WRITE_MASK : 0);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005579
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005580 /*
5581 * currently PKRU is only applied to ept enabled guest so
5582 * there is no pkey in EPT page table for L1 guest or EPT
5583 * shadow page table for L2 guest.
5584 */
Avi Kivity97d64b72012-09-12 14:52:00 +03005585 if (vcpu_match_mmio_gva(vcpu, gva)
Feng Wu97ec8c02014-04-01 17:46:34 +08005586 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
Sean Christopherson871bd032019-08-01 13:35:21 -07005587 vcpu->arch.mmio_access, 0, access)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005588 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5589 (gva & (PAGE_SIZE - 1));
Xiao Guangrong4f022642011-07-12 03:34:24 +08005590 trace_vcpu_match_mmio(gva, *gpa, write, false);
Xiao Guangrongbebb1062011-07-12 03:23:20 +08005591 return 1;
5592 }
5593
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005594 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5595
5596 if (*gpa == UNMAPPED_GVA)
5597 return -1;
5598
Tom Lendacky0f89b202016-12-14 14:59:23 -05005599 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005600}
5601
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005602int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02005603 const void *val, int bytes)
Avi Kivity9f811282008-03-02 14:06:05 +02005604{
5605 int ret;
5606
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005607 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005608 if (ret < 0)
5609 return 0;
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005610 kvm_page_track_write(vcpu, gpa, val, bytes);
Avi Kivity9f811282008-03-02 14:06:05 +02005611 return 1;
5612}
5613
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005614struct read_write_emulator_ops {
5615 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5616 int bytes);
5617 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5618 void *val, int bytes);
5619 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5620 int bytes, void *val);
5621 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5622 void *val, int bytes);
5623 bool write;
5624};
5625
5626static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5627{
5628 if (vcpu->mmio_read_completed) {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005629 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005630 vcpu->mmio_fragments[0].gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005631 vcpu->mmio_read_completed = 0;
5632 return 1;
5633 }
5634
5635 return 0;
5636}
5637
5638static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5639 void *val, int bytes)
5640{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005641 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005642}
5643
5644static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5645 void *val, int bytes)
5646{
5647 return emulator_write_phys(vcpu, gpa, val, bytes);
5648}
5649
5650static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5651{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005652 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005653 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5654}
5655
5656static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5657 void *val, int bytes)
5658{
Wanpeng Lie39d200f2017-12-14 17:40:50 -08005659 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005660 return X86EMUL_IO_NEEDED;
5661}
5662
5663static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5664 void *val, int bytes)
5665{
Avi Kivityf78146b2012-04-18 19:22:47 +03005666 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5667
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005668 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005669 return X86EMUL_CONTINUE;
5670}
5671
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005672static const struct read_write_emulator_ops read_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005673 .read_write_prepare = read_prepare,
5674 .read_write_emulate = read_emulate,
5675 .read_write_mmio = vcpu_mmio_read,
5676 .read_write_exit_mmio = read_exit_mmio,
5677};
5678
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005679static const struct read_write_emulator_ops write_emultor = {
Xiao Guangrong77d197b2011-07-13 14:31:50 +08005680 .read_write_emulate = write_emulate,
5681 .read_write_mmio = write_mmio,
5682 .read_write_exit_mmio = write_exit_mmio,
5683 .write = true,
5684};
5685
Xiao Guangrong22388a32011-07-13 14:32:31 +08005686static int emulator_read_write_onepage(unsigned long addr, void *val,
5687 unsigned int bytes,
5688 struct x86_exception *exception,
5689 struct kvm_vcpu *vcpu,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005690 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005691{
Xiao Guangrongaf7cc7d2011-07-12 03:22:46 +08005692 gpa_t gpa;
5693 int handled, ret;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005694 bool write = ops->write;
Avi Kivityf78146b2012-04-18 19:22:47 +03005695 struct kvm_mmio_fragment *frag;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08005696 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005697
5698 /*
5699 * If the exit was due to a NPF we may already have a GPA.
5700 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5701 * Note, this cannot be used on string operations since string
5702 * operation using rep will only have the initial GPA from the NPF
5703 * occurred.
5704 */
Sean Christopherson744e6992020-02-18 15:03:09 -08005705 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) &&
5706 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
5707 gpa = ctxt->gpa_val;
Brijesh Singh618232e2017-08-17 18:36:57 +02005708 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5709 } else {
5710 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5711 if (ret < 0)
5712 return X86EMUL_PROPAGATE_FAULT;
Tom Lendacky0f89b202016-12-14 14:59:23 -05005713 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005714
Brijesh Singh618232e2017-08-17 18:36:57 +02005715 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
Carsten Ottebbd9b642007-10-30 18:44:21 +01005716 return X86EMUL_CONTINUE;
5717
Carsten Ottebbd9b642007-10-30 18:44:21 +01005718 /*
5719 * Is this MMIO handled locally?
5720 */
Xiao Guangrong22388a32011-07-13 14:32:31 +08005721 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
Avi Kivity70252a12010-01-19 12:51:22 +02005722 if (handled == bytes)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005723 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005724
Avi Kivity70252a12010-01-19 12:51:22 +02005725 gpa += handled;
5726 bytes -= handled;
5727 val += handled;
5728
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005729 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5730 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5731 frag->gpa = gpa;
5732 frag->data = val;
5733 frag->len = bytes;
Avi Kivityf78146b2012-04-18 19:22:47 +03005734 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005735}
5736
Xiubo Li52eb5a62015-03-13 17:39:45 +08005737static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5738 unsigned long addr,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005739 void *val, unsigned int bytes,
5740 struct x86_exception *exception,
Mathias Krause0fbe9b02012-08-30 01:30:17 +02005741 const struct read_write_emulator_ops *ops)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005742{
Avi Kivity0f65dd72011-04-20 13:37:53 +03005743 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivityf78146b2012-04-18 19:22:47 +03005744 gpa_t gpa;
5745 int rc;
5746
5747 if (ops->read_write_prepare &&
5748 ops->read_write_prepare(vcpu, val, bytes))
5749 return X86EMUL_CONTINUE;
5750
5751 vcpu->mmio_nr_fragments = 0;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005752
Carsten Ottebbd9b642007-10-30 18:44:21 +01005753 /* Crossing a page boundary? */
5754 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
Avi Kivityf78146b2012-04-18 19:22:47 +03005755 int now;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005756
5757 now = -addr & ~PAGE_MASK;
Xiao Guangrong22388a32011-07-13 14:32:31 +08005758 rc = emulator_read_write_onepage(addr, val, now, exception,
5759 vcpu, ops);
5760
Carsten Ottebbd9b642007-10-30 18:44:21 +01005761 if (rc != X86EMUL_CONTINUE)
5762 return rc;
5763 addr += now;
Nadav Amitbac155312015-01-26 09:32:26 +02005764 if (ctxt->mode != X86EMUL_MODE_PROT64)
5765 addr = (u32)addr;
Carsten Ottebbd9b642007-10-30 18:44:21 +01005766 val += now;
5767 bytes -= now;
5768 }
Xiao Guangrong22388a32011-07-13 14:32:31 +08005769
Avi Kivityf78146b2012-04-18 19:22:47 +03005770 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5771 vcpu, ops);
5772 if (rc != X86EMUL_CONTINUE)
5773 return rc;
5774
5775 if (!vcpu->mmio_nr_fragments)
5776 return rc;
5777
5778 gpa = vcpu->mmio_fragments[0].gpa;
5779
5780 vcpu->mmio_needed = 1;
5781 vcpu->mmio_cur_fragment = 0;
5782
Xiao Guangrong87da7e62012-10-24 14:07:59 +08005783 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
Avi Kivityf78146b2012-04-18 19:22:47 +03005784 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5785 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5786 vcpu->run->mmio.phys_addr = gpa;
5787
5788 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
Xiao Guangrong22388a32011-07-13 14:32:31 +08005789}
5790
5791static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5792 unsigned long addr,
5793 void *val,
5794 unsigned int bytes,
5795 struct x86_exception *exception)
5796{
5797 return emulator_read_write(ctxt, addr, val, bytes,
5798 exception, &read_emultor);
5799}
5800
Xiubo Li52eb5a62015-03-13 17:39:45 +08005801static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
Xiao Guangrong22388a32011-07-13 14:32:31 +08005802 unsigned long addr,
5803 const void *val,
5804 unsigned int bytes,
5805 struct x86_exception *exception)
5806{
5807 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5808 exception, &write_emultor);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005809}
Carsten Ottebbd9b642007-10-30 18:44:21 +01005810
Avi Kivitydaea3e72010-03-15 13:59:54 +02005811#define CMPXCHG_TYPE(t, ptr, old, new) \
5812 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5813
5814#ifdef CONFIG_X86_64
5815# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5816#else
5817# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01005818 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02005819#endif
5820
Avi Kivity0f65dd72011-04-20 13:37:53 +03005821static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5822 unsigned long addr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01005823 const void *old,
5824 const void *new,
5825 unsigned int bytes,
Avi Kivity0f65dd72011-04-20 13:37:53 +03005826 struct x86_exception *exception)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005827{
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005828 struct kvm_host_map map;
Avi Kivity0f65dd72011-04-20 13:37:53 +03005829 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005830 gpa_t gpa;
Avi Kivitydaea3e72010-03-15 13:59:54 +02005831 char *kaddr;
5832 bool exchanged;
5833
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005834 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02005835 if (bytes > 8 || (bytes & (bytes - 1)))
5836 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005837
Avi Kivitydaea3e72010-03-15 13:59:54 +02005838 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005839
Avi Kivitydaea3e72010-03-15 13:59:54 +02005840 if (gpa == UNMAPPED_GVA ||
5841 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5842 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005843
Avi Kivitydaea3e72010-03-15 13:59:54 +02005844 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5845 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005846
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005847 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08005848 goto emul_write;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005849
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005850 kaddr = map.hva + offset_in_page(gpa);
5851
Avi Kivitydaea3e72010-03-15 13:59:54 +02005852 switch (bytes) {
5853 case 1:
5854 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5855 break;
5856 case 2:
5857 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5858 break;
5859 case 4:
5860 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5861 break;
5862 case 8:
5863 exchanged = CMPXCHG64(kaddr, old, new);
5864 break;
5865 default:
5866 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005867 }
KarimAllah Ahmed42e35f82019-01-31 21:24:39 +01005868
5869 kvm_vcpu_unmap(vcpu, &map, true);
Avi Kivitydaea3e72010-03-15 13:59:54 +02005870
5871 if (!exchanged)
5872 return X86EMUL_CMPXCHG_FAILED;
5873
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08005874 kvm_page_track_write(vcpu, gpa, new, bytes);
Gleb Natapov8f6abd02010-04-13 10:21:56 +03005875
5876 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02005877
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005878emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02005879 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05005880
Avi Kivity0f65dd72011-04-20 13:37:53 +03005881 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005882}
5883
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005884static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5885{
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005886 int r = 0, i;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005887
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005888 for (i = 0; i < vcpu->arch.pio.count; i++) {
5889 if (vcpu->arch.pio.in)
5890 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5891 vcpu->arch.pio.size, pd);
5892 else
5893 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5894 vcpu->arch.pio.port, vcpu->arch.pio.size,
5895 pd);
5896 if (r)
5897 break;
5898 pd += vcpu->arch.pio.size;
5899 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005900 return r;
5901}
5902
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005903static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5904 unsigned short port, void *val,
5905 unsigned int count, bool in)
5906{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005907 vcpu->arch.pio.port = port;
5908 vcpu->arch.pio.in = in;
5909 vcpu->arch.pio.count = count;
5910 vcpu->arch.pio.size = size;
5911
5912 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
5913 vcpu->arch.pio.count = 0;
5914 return 1;
5915 }
5916
5917 vcpu->run->exit_reason = KVM_EXIT_IO;
5918 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
5919 vcpu->run->io.size = size;
5920 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5921 vcpu->run->io.count = count;
5922 vcpu->run->io.port = port;
5923
5924 return 0;
5925}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005926
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005927static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
5928 unsigned short port, void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005929{
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005930 int ret;
Avi Kivityca1d4a92011-04-20 13:37:53 +03005931
Gleb Natapov79729952010-03-18 15:20:24 +02005932 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005933 goto data_avail;
5934
Wanpeng Licbfc6c92017-05-19 02:46:56 -07005935 memset(vcpu->arch.pio_data, 0, size * count);
5936
Xiao Guangrong6f6fbe92011-09-22 16:55:10 +08005937 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5938 if (ret) {
5939data_avail:
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005940 memcpy(val, vcpu->arch.pio_data, size * count);
Ulrich Obergfell11719032014-05-02 17:57:47 +02005941 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
Gleb Natapov79729952010-03-18 15:20:24 +02005942 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005943 return 1;
5944 }
5945
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005946 return 0;
5947}
5948
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005949static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5950 int size, unsigned short port, void *val,
5951 unsigned int count)
5952{
5953 return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count);
5954
5955}
5956
5957static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
5958 unsigned short port, const void *val,
5959 unsigned int count)
5960{
5961 memcpy(vcpu->arch.pio_data, val, size * count);
5962 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
5963 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
5964}
5965
Avi Kivityca1d4a92011-04-20 13:37:53 +03005966static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5967 int size, unsigned short port,
5968 const void *val, unsigned int count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005969{
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08005970 return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005971}
5972
Carsten Ottebbd9b642007-10-30 18:44:21 +01005973static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5974{
5975 return kvm_x86_ops->get_segment_base(vcpu, seg);
5976}
5977
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005978static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
Carsten Ottebbd9b642007-10-30 18:44:21 +01005979{
Avi Kivity3cb16fe2011-04-20 15:38:44 +03005980 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01005981}
5982
Jiang Biaoae6a2372016-11-07 08:54:51 +08005983static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005984{
5985 if (!need_emulate_wbinvd(vcpu))
5986 return X86EMUL_CONTINUE;
5987
5988 if (kvm_x86_ops->has_wbinvd_exit()) {
Jan Kiszka2eec7342010-11-01 14:01:29 +01005989 int cpu = get_cpu();
5990
5991 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005992 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5993 wbinvd_ipi, NULL, 1);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005994 put_cpu();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005995 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
Jan Kiszka2eec7342010-11-01 14:01:29 +01005996 } else
5997 wbinvd();
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005998 return X86EMUL_CONTINUE;
5999}
Joel Schopp5cb56052015-03-02 13:43:31 -06006000
6001int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
6002{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006003 kvm_emulate_wbinvd_noskip(vcpu);
6004 return kvm_skip_emulated_instruction(vcpu);
Joel Schopp5cb56052015-03-02 13:43:31 -06006005}
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006006EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
6007
Joel Schopp5cb56052015-03-02 13:43:31 -06006008
6009
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006010static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
6011{
Joel Schopp5cb56052015-03-02 13:43:31 -06006012 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006013}
6014
Xiubo Li52eb5a62015-03-13 17:39:45 +08006015static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
6016 unsigned long *dest)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006017{
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006018 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006019}
6020
Xiubo Li52eb5a62015-03-13 17:39:45 +08006021static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
6022 unsigned long value)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006023{
Gleb Natapov338dbc92010-04-28 19:15:32 +03006024
Avi Kivity717746e2011-04-20 13:37:53 +03006025 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006026}
6027
Gleb Natapov52a46612010-03-18 15:20:03 +02006028static u64 mk_cr_64(u64 curr_cr, u32 new_val)
6029{
6030 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
6031}
6032
Avi Kivity717746e2011-04-20 13:37:53 +03006033static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
Gleb Natapov52a46612010-03-18 15:20:03 +02006034{
Avi Kivity717746e2011-04-20 13:37:53 +03006035 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov52a46612010-03-18 15:20:03 +02006036 unsigned long value;
6037
6038 switch (cr) {
6039 case 0:
6040 value = kvm_read_cr0(vcpu);
6041 break;
6042 case 2:
6043 value = vcpu->arch.cr2;
6044 break;
6045 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006046 value = kvm_read_cr3(vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +02006047 break;
6048 case 4:
6049 value = kvm_read_cr4(vcpu);
6050 break;
6051 case 8:
6052 value = kvm_get_cr8(vcpu);
6053 break;
6054 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006055 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov52a46612010-03-18 15:20:03 +02006056 return 0;
6057 }
6058
6059 return value;
6060}
6061
Avi Kivity717746e2011-04-20 13:37:53 +03006062static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
Gleb Natapov52a46612010-03-18 15:20:03 +02006063{
Avi Kivity717746e2011-04-20 13:37:53 +03006064 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov0f122442010-04-28 19:15:31 +03006065 int res = 0;
6066
Gleb Natapov52a46612010-03-18 15:20:03 +02006067 switch (cr) {
6068 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03006069 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006070 break;
6071 case 2:
6072 vcpu->arch.cr2 = val;
6073 break;
6074 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006075 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006076 break;
6077 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03006078 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02006079 break;
6080 case 8:
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006081 res = kvm_set_cr8(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02006082 break;
6083 default:
Christoffer Dalla737f252012-06-03 21:17:48 +03006084 kvm_err("%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03006085 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02006086 }
Gleb Natapov0f122442010-04-28 19:15:31 +03006087
6088 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02006089}
6090
Avi Kivity717746e2011-04-20 13:37:53 +03006091static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
Gleb Natapov9c537242010-03-18 15:20:05 +02006092{
Avi Kivity717746e2011-04-20 13:37:53 +03006093 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
Gleb Natapov9c537242010-03-18 15:20:05 +02006094}
6095
Avi Kivity4bff1e862011-04-20 13:37:53 +03006096static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006097{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006098 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006099}
6100
Avi Kivity4bff1e862011-04-20 13:37:53 +03006101static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006102{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006103 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006104}
6105
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006106static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6107{
6108 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6109}
6110
6111static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6112{
6113 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6114}
6115
Avi Kivity4bff1e862011-04-20 13:37:53 +03006116static unsigned long emulator_get_cached_segment_base(
6117 struct x86_emulate_ctxt *ctxt, int seg)
Gleb Natapov5951c442010-04-28 19:15:29 +03006118{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006119 return get_segment_base(emul_to_vcpu(ctxt), seg);
Gleb Natapov5951c442010-04-28 19:15:29 +03006120}
6121
Avi Kivity1aa36612011-04-27 13:20:30 +03006122static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6123 struct desc_struct *desc, u32 *base3,
6124 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006125{
6126 struct kvm_segment var;
6127
Avi Kivity4bff1e862011-04-20 13:37:53 +03006128 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
Avi Kivity1aa36612011-04-27 13:20:30 +03006129 *selector = var.selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006130
Gleb Natapov378a8b02013-01-21 15:36:48 +02006131 if (var.unusable) {
6132 memset(desc, 0, sizeof(*desc));
Radim Krčmářf0367ee2017-05-18 19:37:30 +02006133 if (base3)
6134 *base3 = 0;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006135 return false;
Gleb Natapov378a8b02013-01-21 15:36:48 +02006136 }
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006137
6138 if (var.g)
6139 var.limit >>= 12;
6140 set_desc_limit(desc, var.limit);
6141 set_desc_base(desc, (unsigned long)var.base);
Gleb Natapov5601d052011-03-07 14:55:06 +02006142#ifdef CONFIG_X86_64
6143 if (base3)
6144 *base3 = var.base >> 32;
6145#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006146 desc->type = var.type;
6147 desc->s = var.s;
6148 desc->dpl = var.dpl;
6149 desc->p = var.present;
6150 desc->avl = var.avl;
6151 desc->l = var.l;
6152 desc->d = var.db;
6153 desc->g = var.g;
6154
6155 return true;
6156}
6157
Avi Kivity1aa36612011-04-27 13:20:30 +03006158static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6159 struct desc_struct *desc, u32 base3,
6160 int seg)
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006161{
Avi Kivity4bff1e862011-04-20 13:37:53 +03006162 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006163 struct kvm_segment var;
6164
Avi Kivity1aa36612011-04-27 13:20:30 +03006165 var.selector = selector;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006166 var.base = get_desc_base(desc);
Gleb Natapov5601d052011-03-07 14:55:06 +02006167#ifdef CONFIG_X86_64
6168 var.base |= ((u64)base3) << 32;
6169#endif
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006170 var.limit = get_desc_limit(desc);
6171 if (desc->g)
6172 var.limit = (var.limit << 12) | 0xfff;
6173 var.type = desc->type;
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006174 var.dpl = desc->dpl;
6175 var.db = desc->d;
6176 var.s = desc->s;
6177 var.l = desc->l;
6178 var.g = desc->g;
6179 var.avl = desc->avl;
6180 var.present = desc->p;
6181 var.unusable = !var.present;
6182 var.padding = 0;
6183
6184 kvm_set_segment(vcpu, &var, seg);
6185 return;
6186}
6187
Avi Kivity717746e2011-04-20 13:37:53 +03006188static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6189 u32 msr_index, u64 *pdata)
6190{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006191 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
Avi Kivity717746e2011-04-20 13:37:53 +03006192}
6193
6194static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6195 u32 msr_index, u64 data)
6196{
Sean Christophersonf20935d2019-09-05 14:22:54 -07006197 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
Avi Kivity717746e2011-04-20 13:37:53 +03006198}
6199
Paolo Bonzini64d60672015-05-07 11:36:11 +02006200static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6201{
6202 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6203
6204 return vcpu->arch.smbase;
6205}
6206
6207static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6208{
6209 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6210
6211 vcpu->arch.smbase = smbase;
6212}
6213
Nadav Amit67f4d422014-06-02 18:34:09 +03006214static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6215 u32 pmc)
6216{
Like Xu98ff80f2019-10-27 18:52:40 +08006217 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
Nadav Amit67f4d422014-06-02 18:34:09 +03006218}
6219
Avi Kivity222d21a2011-11-10 14:57:30 +02006220static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6221 u32 pmc, u64 *pdata)
6222{
Wei Huangc6702c92015-06-19 13:44:45 +02006223 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
Avi Kivity222d21a2011-11-10 14:57:30 +02006224}
6225
Avi Kivity6c3287f2011-04-20 15:43:05 +03006226static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6227{
6228 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6229}
6230
Avi Kivity29535382011-04-20 13:37:53 +03006231static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006232 struct x86_instruction_info *info,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006233 enum x86_intercept_stage stage)
6234{
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08006235 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage,
6236 &ctxt->exception);
Avi Kivityc4f035c2011-04-04 12:39:22 +02006237}
6238
Yu Zhange911eb32017-08-24 20:27:52 +08006239static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6240 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006241{
Yu Zhange911eb32017-08-24 20:27:52 +08006242 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006243}
6244
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006245static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt)
6246{
6247 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM);
6248}
6249
6250static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt)
6251{
6252 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE);
6253}
6254
6255static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
6256{
6257 return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
6258}
6259
Avi Kivitydd856ef2012-08-27 23:46:17 +03006260static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6261{
6262 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6263}
6264
6265static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6266{
6267 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6268}
6269
Nadav Amit801806d2015-01-26 09:32:23 +02006270static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6271{
6272 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6273}
6274
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006275static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6276{
6277 return emul_to_vcpu(ctxt)->arch.hflags;
6278}
6279
6280static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6281{
Sean Christophersonc5833c72019-04-02 08:03:10 -07006282 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006283}
6284
Sean Christophersoned193212019-04-02 08:03:09 -07006285static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6286 const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006287{
Sean Christophersoned193212019-04-02 08:03:09 -07006288 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
Ladi Prosek0234bf82017-10-11 16:54:40 +02006289}
6290
Sean Christophersonc5833c72019-04-02 08:03:10 -07006291static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6292{
6293 kvm_smm_changed(emul_to_vcpu(ctxt));
6294}
6295
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006296static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6297{
6298 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6299}
6300
Mathias Krause0225fb52012-08-30 01:30:16 +02006301static const struct x86_emulate_ops emulate_ops = {
Avi Kivitydd856ef2012-08-27 23:46:17 +03006302 .read_gpr = emulator_read_gpr,
6303 .write_gpr = emulator_write_gpr,
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006304 .read_std = emulator_read_std,
6305 .write_std = emulator_write_std,
Radim Krčmář7a036a62015-10-30 16:36:24 +01006306 .read_phys = kvm_read_guest_phys_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02006307 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006308 .read_emulated = emulator_read_emulated,
6309 .write_emulated = emulator_write_emulated,
6310 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity3cb16fe2011-04-20 15:38:44 +03006311 .invlpg = emulator_invlpg,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006312 .pio_in_emulated = emulator_pio_in_emulated,
6313 .pio_out_emulated = emulator_pio_out_emulated,
Avi Kivity1aa36612011-04-27 13:20:30 +03006314 .get_segment = emulator_get_segment,
6315 .set_segment = emulator_set_segment,
Gleb Natapov5951c442010-04-28 19:15:29 +03006316 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02006317 .get_gdt = emulator_get_gdt,
Mohammed Gamal160ce1f2010-08-04 05:44:24 +03006318 .get_idt = emulator_get_idt,
Avi Kivity1ac9d0c2011-04-20 15:12:00 +03006319 .set_gdt = emulator_set_gdt,
6320 .set_idt = emulator_set_idt,
Gleb Natapov52a46612010-03-18 15:20:03 +02006321 .get_cr = emulator_get_cr,
6322 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02006323 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03006324 .get_dr = emulator_get_dr,
6325 .set_dr = emulator_set_dr,
Paolo Bonzini64d60672015-05-07 11:36:11 +02006326 .get_smbase = emulator_get_smbase,
6327 .set_smbase = emulator_set_smbase,
Avi Kivity717746e2011-04-20 13:37:53 +03006328 .set_msr = emulator_set_msr,
6329 .get_msr = emulator_get_msr,
Nadav Amit67f4d422014-06-02 18:34:09 +03006330 .check_pmc = emulator_check_pmc,
Avi Kivity222d21a2011-11-10 14:57:30 +02006331 .read_pmc = emulator_read_pmc,
Avi Kivity6c3287f2011-04-20 15:43:05 +03006332 .halt = emulator_halt,
Avi Kivitybcaf5cc2011-04-20 15:53:23 +03006333 .wbinvd = emulator_wbinvd,
Avi Kivityd6aa1002011-04-20 15:47:13 +03006334 .fix_hypercall = emulator_fix_hypercall,
Avi Kivityc4f035c2011-04-04 12:39:22 +02006335 .intercept = emulator_intercept,
Stephan Bärwolfbdb42f52012-01-12 16:43:03 +01006336 .get_cpuid = emulator_get_cpuid,
Sean Christopherson5ae78e92019-12-17 13:32:38 -08006337 .guest_has_long_mode = emulator_guest_has_long_mode,
6338 .guest_has_movbe = emulator_guest_has_movbe,
6339 .guest_has_fxsr = emulator_guest_has_fxsr,
Nadav Amit801806d2015-01-26 09:32:23 +02006340 .set_nmi_mask = emulator_set_nmi_mask,
Ladi Prosek6ed071f2017-04-25 16:42:44 +02006341 .get_hflags = emulator_get_hflags,
6342 .set_hflags = emulator_set_hflags,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006343 .pre_leave_smm = emulator_pre_leave_smm,
Sean Christophersonc5833c72019-04-02 08:03:10 -07006344 .post_leave_smm = emulator_post_leave_smm,
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006345 .set_xcr = emulator_set_xcr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01006346};
6347
Gleb Natapov95cb2292010-04-28 19:15:43 +03006348static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6349{
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006350 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
Gleb Natapov95cb2292010-04-28 19:15:43 +03006351 /*
6352 * an sti; sti; sequence only disable interrupts for the first
6353 * instruction. So, if the last instruction, be it emulated or
6354 * not, left the system with the INT_STI flag enabled, it
6355 * means that the last instruction is an sti. We should not
6356 * leave the flag on in this case. The same goes for mov ss
6357 */
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02006358 if (int_shadow & mask)
6359 mask = 0;
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006360 if (unlikely(int_shadow || mask)) {
Gleb Natapov95cb2292010-04-28 19:15:43 +03006361 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006362 if (!mask)
6363 kvm_make_request(KVM_REQ_EVENT, vcpu);
6364 }
Gleb Natapov95cb2292010-04-28 19:15:43 +03006365}
6366
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006367static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
Gleb Natapov54b84862010-04-28 19:15:44 +03006368{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006369 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Avi Kivityda9cb572010-11-22 17:53:21 +02006370 if (ctxt->exception.vector == PF_VECTOR)
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006371 return kvm_propagate_fault(vcpu, &ctxt->exception);
6372
6373 if (ctxt->exception.error_code_valid)
Avi Kivityda9cb572010-11-22 17:53:21 +02006374 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6375 ctxt->exception.error_code);
Gleb Natapov54b84862010-04-28 19:15:44 +03006376 else
Avi Kivityda9cb572010-11-22 17:53:21 +02006377 kvm_queue_exception(vcpu, ctxt->exception.vector);
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006378 return false;
Gleb Natapov54b84862010-04-28 19:15:44 +03006379}
6380
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006381static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
6382{
6383 struct x86_emulate_ctxt *ctxt;
6384
6385 ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);
6386 if (!ctxt) {
6387 pr_err("kvm: failed to allocate vcpu's emulator\n");
6388 return NULL;
6389 }
6390
6391 ctxt->vcpu = vcpu;
6392 ctxt->ops = &emulate_ops;
6393 vcpu->arch.emulate_ctxt = ctxt;
6394
6395 return ctxt;
6396}
6397
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006398static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6399{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006400 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006401 int cs_db, cs_l;
6402
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006403 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6404
Sean Christopherson744e6992020-02-18 15:03:09 -08006405 ctxt->gpa_available = false;
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006406 ctxt->eflags = kvm_get_rflags(vcpu);
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006407 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6408
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006409 ctxt->eip = kvm_rip_read(vcpu);
6410 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6411 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
Nadav Amit42bf5492014-04-18 07:11:34 +03006412 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006413 cs_db ? X86EMUL_MODE_PROT32 :
6414 X86EMUL_MODE_PROT16;
Paolo Bonzinia5845392015-04-01 18:18:53 +02006415 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006416 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6417 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
Takuya Yoshikawaadf52232011-05-25 11:06:16 +09006418
Avi Kivitydd856ef2012-08-27 23:46:17 +03006419 init_decode_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006420 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006421}
6422
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006423void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
Mohammed Gamal63995652010-09-19 14:34:06 +02006424{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006425 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal63995652010-09-19 14:34:06 +02006426 int ret;
6427
6428 init_emulate_ctxt(vcpu);
6429
Avi Kivity9dac77f2011-06-01 15:34:25 +03006430 ctxt->op_bytes = 2;
6431 ctxt->ad_bytes = 2;
6432 ctxt->_eip = ctxt->eip + inc_eip;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006433 ret = emulate_int_real(ctxt, irq);
Mohammed Gamal63995652010-09-19 14:34:06 +02006434
Sean Christopherson9497e1f2019-08-27 14:40:36 -07006435 if (ret != X86EMUL_CONTINUE) {
6436 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6437 } else {
6438 ctxt->eip = ctxt->_eip;
6439 kvm_rip_write(vcpu, ctxt->eip);
6440 kvm_set_rflags(vcpu, ctxt->eflags);
6441 }
Mohammed Gamal63995652010-09-19 14:34:06 +02006442}
6443EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6444
Liran Alone2366172018-03-12 13:12:49 +02006445static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006446{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006447 ++vcpu->stat.insn_emulation_fail;
6448 trace_kvm_emulate_insn_failed(vcpu);
Liran Alone2366172018-03-12 13:12:49 +02006449
Sean Christopherson42cbf062019-08-27 14:40:31 -07006450 if (emulation_type & EMULTYPE_VMWARE_GP) {
6451 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006452 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006453 }
Liran Alone2366172018-03-12 13:12:49 +02006454
Sean Christopherson738fece2019-08-27 14:40:34 -07006455 if (emulation_type & EMULTYPE_SKIP) {
6456 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6457 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6458 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006459 return 0;
Sean Christopherson738fece2019-08-27 14:40:34 -07006460 }
6461
Sean Christopherson22da61c2019-08-27 14:40:28 -07006462 kvm_queue_exception(vcpu, UD_VECTOR);
6463
Nadav Amita2b9e6c2014-09-17 02:50:50 +03006464 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006465 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6466 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6467 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006468 return 0;
Joerg Roedelfc3a9152010-11-29 17:51:49 +01006469 }
Liran Alone2366172018-03-12 13:12:49 +02006470
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006471 return 1;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006472}
6473
Sean Christopherson736c2912019-12-06 15:57:14 -08006474static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006475 bool write_fault_to_shadow_pgtable,
6476 int emulation_type)
Gleb Natapova6f177e2010-07-08 12:41:12 +03006477{
Sean Christopherson736c2912019-12-06 15:57:14 -08006478 gpa_t gpa = cr2_or_gpa;
Dan Williamsba049e92016-01-15 16:56:11 -08006479 kvm_pfn_t pfn;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006480
Sean Christopherson92daa482020-02-18 15:03:08 -08006481 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Gleb Natapov991eebf2013-04-11 12:10:51 +03006482 return false;
6483
Sean Christopherson92daa482020-02-18 15:03:08 -08006484 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6485 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006486 return false;
6487
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006488 if (!vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006489 /*
6490 * Write permission should be allowed since only
6491 * write access need to be emulated.
6492 */
Sean Christopherson736c2912019-12-06 15:57:14 -08006493 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Gleb Natapov68be0802010-07-14 19:05:45 +03006494
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006495 /*
6496 * If the mapping is invalid in guest, let cpu retry
6497 * it to generate fault.
6498 */
6499 if (gpa == UNMAPPED_GVA)
6500 return true;
6501 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006502
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006503 /*
6504 * Do not retry the unhandleable instruction if it faults on the
6505 * readonly host memory, otherwise it will goto a infinite loop:
6506 * retry instruction -> write #PF -> emulation fail -> retry
6507 * instruction -> ...
6508 */
6509 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006510
6511 /*
6512 * If the instruction failed on the error pfn, it can not be fixed,
6513 * report the error to userspace.
6514 */
6515 if (is_error_noslot_pfn(pfn))
6516 return false;
6517
6518 kvm_release_pfn_clean(pfn);
6519
6520 /* The instructions are well-emulated on direct mmu. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006521 if (vcpu->arch.mmu->direct_map) {
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006522 unsigned int indirect_shadow_pages;
6523
6524 spin_lock(&vcpu->kvm->mmu_lock);
6525 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6526 spin_unlock(&vcpu->kvm->mmu_lock);
6527
6528 if (indirect_shadow_pages)
6529 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6530
Gleb Natapova6f177e2010-07-08 12:41:12 +03006531 return true;
Xiao Guangrong8e3d9d02012-08-21 10:57:42 +08006532 }
Gleb Natapova6f177e2010-07-08 12:41:12 +03006533
Xiao Guangrong95b3cf62013-01-13 23:46:52 +08006534 /*
6535 * if emulation was due to access to shadowed page table
6536 * and it failed try to unshadow page and re-enter the
6537 * guest to let CPU execute the instruction.
6538 */
6539 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006540
6541 /*
6542 * If the access faults on its page table, it can not
6543 * be fixed by unprotecting shadow page and it should
6544 * be reported to userspace.
6545 */
6546 return !write_fault_to_shadow_pgtable;
Gleb Natapova6f177e2010-07-08 12:41:12 +03006547}
6548
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006549static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
Sean Christopherson736c2912019-12-06 15:57:14 -08006550 gpa_t cr2_or_gpa, int emulation_type)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006551{
6552 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Sean Christopherson736c2912019-12-06 15:57:14 -08006553 unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006554
6555 last_retry_eip = vcpu->arch.last_retry_eip;
6556 last_retry_addr = vcpu->arch.last_retry_addr;
6557
6558 /*
6559 * If the emulation is caused by #PF and it is non-page_table
6560 * writing instruction, it means the VM-EXIT is caused by shadow
6561 * page protected, we can zap the shadow page and retry this
6562 * instruction directly.
6563 *
6564 * Note: if the guest uses a non-page-table modifying instruction
6565 * on the PDE that points to the instruction, then we will unmap
6566 * the instruction and go to an infinite loop. So, we cache the
6567 * last retried eip and the last fault address, if we meet the eip
6568 * and the address again, we can break out of the potential infinite
6569 * loop.
6570 */
6571 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6572
Sean Christopherson92daa482020-02-18 15:03:08 -08006573 if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF))
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006574 return false;
6575
Sean Christopherson92daa482020-02-18 15:03:08 -08006576 if (WARN_ON_ONCE(is_guest_mode(vcpu)) ||
6577 WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF)))
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07006578 return false;
6579
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006580 if (x86_page_table_writing_insn(ctxt))
6581 return false;
6582
Sean Christopherson736c2912019-12-06 15:57:14 -08006583 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa)
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006584 return false;
6585
6586 vcpu->arch.last_retry_eip = ctxt->eip;
Sean Christopherson736c2912019-12-06 15:57:14 -08006587 vcpu->arch.last_retry_addr = cr2_or_gpa;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006588
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02006589 if (!vcpu->arch.mmu->direct_map)
Sean Christopherson736c2912019-12-06 15:57:14 -08006590 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006591
Xiao Guangrong22368022013-01-13 23:44:12 +08006592 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006593
6594 return true;
6595}
6596
Gleb Natapov716d51a2012-09-03 15:24:26 +03006597static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6598static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6599
Paolo Bonzini64d60672015-05-07 11:36:11 +02006600static void kvm_smm_changed(struct kvm_vcpu *vcpu)
Paolo Bonzinia5845392015-04-01 18:18:53 +02006601{
Paolo Bonzini64d60672015-05-07 11:36:11 +02006602 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
Paolo Bonzini660a5d52015-05-05 11:50:23 +02006603 /* This is a good place to trace that we are exiting SMM. */
6604 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6605
Paolo Bonzinic43203c2016-06-01 22:26:00 +02006606 /* Process a latched INIT or SMI, if any. */
6607 kvm_make_request(KVM_REQ_EVENT, vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006608 }
Paolo Bonzini699023e2015-05-18 15:03:39 +02006609
6610 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02006611}
6612
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006613static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6614 unsigned long *db)
6615{
6616 u32 dr6 = 0;
6617 int i;
6618 u32 enable, rwlen;
6619
6620 enable = dr7;
6621 rwlen = dr7 >> 16;
6622 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6623 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6624 dr6 |= (1 << i);
6625 return dr6;
6626}
6627
Sean Christopherson120c2c42019-08-27 14:40:29 -07006628static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006629{
6630 struct kvm_run *kvm_run = vcpu->run;
6631
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006632 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6633 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6634 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6635 kvm_run->debug.arch.exception = DB_VECTOR;
6636 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006637 return 0;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006638 }
Sean Christopherson120c2c42019-08-27 14:40:29 -07006639 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006640 return 1;
Paolo Bonzini663f4c62013-06-25 18:32:07 +02006641}
6642
Kyle Huey6affcbe2016-11-29 12:40:40 -08006643int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6644{
6645 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006646 int r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006647
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006648 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006649 if (unlikely(!r))
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02006650 return 0;
Paolo Bonzinic8401dd2017-06-07 15:13:14 +02006651
6652 /*
6653 * rflags is the old, "raw" value of the flags. The new value has
6654 * not been saved yet.
6655 *
6656 * This is correct even for TF set by the guest, because "the
6657 * processor will not generate this exception after the instruction
6658 * that sets the TF flag".
6659 */
6660 if (unlikely(rflags & X86_EFLAGS_TF))
Sean Christopherson120c2c42019-08-27 14:40:29 -07006661 r = kvm_vcpu_do_singlestep(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006662 return r;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006663}
6664EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6665
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006666static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6667{
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006668 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6669 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006670 struct kvm_run *kvm_run = vcpu->run;
6671 unsigned long eip = kvm_get_linear_rip(vcpu);
6672 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006673 vcpu->arch.guest_debug_dr7,
6674 vcpu->arch.eff_db);
6675
6676 if (dr6 != 0) {
Nadav Amit6f43ed02014-07-15 17:37:46 +03006677 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Nadav Amit82b32772014-11-02 11:54:45 +02006678 kvm_run->debug.arch.pc = eip;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006679 kvm_run->debug.arch.exception = DB_VECTOR;
6680 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006681 *r = 0;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006682 return true;
6683 }
6684 }
6685
Nadav Amit4161a562014-07-17 01:19:31 +03006686 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6687 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
Nadav Amit82b32772014-11-02 11:54:45 +02006688 unsigned long eip = kvm_get_linear_rip(vcpu);
6689 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006690 vcpu->arch.dr7,
6691 vcpu->arch.db);
6692
6693 if (dr6 != 0) {
Liran Alon1fc5d1942019-06-06 01:54:47 +03006694 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006695 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006696 kvm_queue_exception(vcpu, DB_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006697 *r = 1;
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006698 return true;
6699 }
6700 }
6701
6702 return false;
6703}
6704
Liran Alon04789b62018-03-12 13:12:50 +02006705static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6706{
Arbel Moshe2d7921c2018-03-12 13:12:53 +02006707 switch (ctxt->opcode_len) {
6708 case 1:
6709 switch (ctxt->b) {
6710 case 0xe4: /* IN */
6711 case 0xe5:
6712 case 0xec:
6713 case 0xed:
6714 case 0xe6: /* OUT */
6715 case 0xe7:
6716 case 0xee:
6717 case 0xef:
6718 case 0x6c: /* INS */
6719 case 0x6d:
6720 case 0x6e: /* OUTS */
6721 case 0x6f:
6722 return true;
6723 }
6724 break;
6725 case 2:
6726 switch (ctxt->b) {
6727 case 0x33: /* RDPMC */
6728 return true;
6729 }
6730 break;
Liran Alon04789b62018-03-12 13:12:50 +02006731 }
6732
6733 return false;
6734}
6735
Sean Christopherson736c2912019-12-06 15:57:14 -08006736int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
6737 int emulation_type, void *insn, int insn_len)
Carsten Ottebbd9b642007-10-30 18:44:21 +01006738{
Gleb Natapov95cb2292010-04-28 19:15:43 +03006739 int r;
Sean Christophersonc9b8b072020-02-18 15:29:48 -08006740 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006741 bool writeback = true;
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006742 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006743
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006744 vcpu->arch.l1tf_flush_l1d = true;
6745
Xiao Guangrong93c05d32013-01-13 23:49:07 +08006746 /*
6747 * Clear write_fault_to_shadow_pgtable here to ensure it is
6748 * never reused.
6749 */
6750 vcpu->arch.write_fault_to_shadow_pgtable = false;
Avi Kivity26eef702008-07-03 14:59:22 +03006751 kvm_clear_exception_queue(vcpu);
Gleb Natapov8d7d81022011-04-12 12:36:21 +03006752
Sheng Yang571008d2008-01-02 14:49:22 +08006753 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Mohammed Gamal8ec47222010-08-16 00:47:01 +03006754 init_emulate_ctxt(vcpu);
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006755
6756 /*
6757 * We will reenter on the same instruction since
6758 * we do not set complete_userspace_io. This does not
6759 * handle watchpoints yet, those would be handled in
6760 * the emulate_ops.
6761 */
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006762 if (!(emulation_type & EMULTYPE_SKIP) &&
6763 kvm_vcpu_check_breakpoint(vcpu, &r))
Paolo Bonzini4a1e10d2013-05-30 11:48:30 +02006764 return r;
6765
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006766 ctxt->interruptibility = 0;
6767 ctxt->have_exception = false;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02006768 ctxt->exception.vector = -1;
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006769 ctxt->perm_ok = false;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006770
Borislav Petkovb51e9742013-09-22 16:44:52 +02006771 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
Avi Kivity40059962011-02-01 16:32:04 +02006772
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006773 r = x86_decode_insn(ctxt, insn, insn_len);
Joerg Roedeld47f00a2010-09-10 17:30:56 +02006774
Avi Kivitye46479f2010-04-11 13:05:16 +03006775 trace_kvm_emulate_insn_start(vcpu);
Avi Kivityf2b57562007-11-18 15:17:51 +02006776 ++vcpu->stat.insn_emulation;
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09006777 if (r != EMULATION_OK) {
Sean Christophersonb4000602019-08-27 14:40:32 -07006778 if ((emulation_type & EMULTYPE_TRAP_UD) ||
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006779 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6780 kvm_queue_exception(vcpu, UD_VECTOR);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006781 return 1;
Sean Christophersonc83fad65e2019-08-27 14:40:33 -07006782 }
Sean Christopherson736c2912019-12-06 15:57:14 -08006783 if (reexecute_instruction(vcpu, cr2_or_gpa,
6784 write_fault_to_spt,
6785 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006786 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006787 if (ctxt->have_exception) {
Jan Dakinevichc8848ce2019-08-27 13:07:08 +00006788 /*
6789 * #UD should result in just EMULATION_FAILED, and trap-like
6790 * exception should not be encountered during decode.
6791 */
6792 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6793 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
Jan Dakinevich8530a792019-08-27 13:07:09 +00006794 inject_emulated_exception(vcpu);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006795 return 1;
Jan Dakinevich8530a792019-08-27 13:07:09 +00006796 }
Liran Alone2366172018-03-12 13:12:49 +02006797 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006798 }
6799 }
6800
Sean Christopherson42cbf062019-08-27 14:40:31 -07006801 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6802 !is_vmware_backdoor_opcode(ctxt)) {
6803 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006804 return 1;
Sean Christopherson42cbf062019-08-27 14:40:31 -07006805 }
Liran Alon04789b62018-03-12 13:12:50 +02006806
Sean Christopherson1957aa62019-08-27 14:40:39 -07006807 /*
6808 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6809 * for kvm_skip_emulated_instruction(). The caller is responsible for
6810 * updating interruptibility state and injecting single-step #DBs.
6811 */
Gleb Natapovba8afb62009-04-12 13:36:57 +03006812 if (emulation_type & EMULTYPE_SKIP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03006813 kvm_rip_write(vcpu, ctxt->_eip);
Nadav Amitbb663c72014-07-21 14:37:26 +03006814 if (ctxt->eflags & X86_EFLAGS_RF)
6815 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006816 return 1;
Gleb Natapovba8afb62009-04-12 13:36:57 +03006817 }
6818
Sean Christopherson736c2912019-12-06 15:57:14 -08006819 if (retry_instruction(ctxt, cr2_or_gpa, emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006820 return 1;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08006821
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006822 /* this is needed for vmware backdoor interface to work since it
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006823 changes registers values during IO operation */
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006824 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6825 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
Avi Kivitydd856ef2012-08-27 23:46:17 +03006826 emulator_invalidate_register_cache(ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006827 }
Gleb Natapov4d2179e2010-04-28 19:15:42 +03006828
Gleb Natapov5cd21912010-03-18 15:20:26 +02006829restart:
Sean Christopherson92daa482020-02-18 15:03:08 -08006830 if (emulation_type & EMULTYPE_PF) {
6831 /* Save the faulting GPA (cr2) in the address field */
6832 ctxt->exception.address = cr2_or_gpa;
6833
6834 /* With shadow page tables, cr2 contains a GVA or nGPA. */
6835 if (vcpu->arch.mmu->direct_map) {
Sean Christopherson744e6992020-02-18 15:03:09 -08006836 ctxt->gpa_available = true;
6837 ctxt->gpa_val = cr2_or_gpa;
Sean Christopherson92daa482020-02-18 15:03:08 -08006838 }
6839 } else {
6840 /* Sanitize the address out of an abundance of paranoia. */
6841 ctxt->exception.address = 0;
6842 }
Tom Lendacky0f89b202016-12-14 14:59:23 -05006843
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006844 r = x86_emulate_insn(ctxt);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006845
Joerg Roedel775fde82011-04-04 12:39:24 +02006846 if (r == EMULATION_INTERCEPTED)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006847 return 1;
Joerg Roedel775fde82011-04-04 12:39:24 +02006848
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006849 if (r == EMULATION_FAILED) {
Sean Christopherson736c2912019-12-06 15:57:14 -08006850 if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt,
Gleb Natapov991eebf2013-04-11 12:10:51 +03006851 emulation_type))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006852 return 1;
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03006853
Liran Alone2366172018-03-12 13:12:49 +02006854 return handle_emulation_failure(vcpu, emulation_type);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006855 }
6856
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006857 if (ctxt->have_exception) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006858 r = 1;
Paolo Bonzinief54bcf2014-09-04 19:46:15 +02006859 if (inject_emulated_exception(vcpu))
6860 return r;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006861 } else if (vcpu->arch.pio.count) {
Paolo Bonzini0912c972013-08-27 15:41:43 +02006862 if (!vcpu->arch.pio.in) {
6863 /* FIXME: return into emulator if single-stepping. */
Gleb Natapove85d28f2010-07-29 15:11:52 +03006864 vcpu->arch.pio.count = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02006865 } else {
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006866 writeback = false;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006867 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6868 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006869 r = 0;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006870 } else if (vcpu->mmio_needed) {
Sean Christophersonbc8a0aa2019-08-27 14:40:27 -07006871 ++vcpu->stat.mmio_exits;
6872
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006873 if (!vcpu->mmio_is_write)
6874 writeback = false;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006875 r = 0;
Gleb Natapov716d51a2012-09-03 15:24:26 +03006876 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006877 } else if (r == EMULATION_RESTART)
Gleb Natapove85d28f2010-07-29 15:11:52 +03006878 goto restart;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03006879 else
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006880 r = 1;
Gleb Natapove85d28f2010-07-29 15:11:52 +03006881
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006882 if (writeback) {
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006883 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09006884 toggle_interruptibility(vcpu, ctxt->interruptibility);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006885 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
Nadav Amit38827db2014-11-02 11:54:53 +02006886 if (!ctxt->have_exception ||
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006887 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6888 kvm_rip_write(vcpu, ctxt->eip);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07006889 if (r && ctxt->tf)
Sean Christopherson120c2c42019-08-27 14:40:29 -07006890 r = kvm_vcpu_do_singlestep(vcpu);
Oliver Upton5ef8acb2020-02-07 02:36:07 -08006891 if (kvm_x86_ops->update_emulated_instruction)
6892 kvm_x86_ops->update_emulated_instruction(vcpu);
Nadav Amit38827db2014-11-02 11:54:53 +02006893 __kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson75ee23b2019-08-23 13:55:44 -07006894 }
Paolo Bonzini6addfc42014-03-27 11:29:28 +01006895
6896 /*
6897 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6898 * do nothing, and it will be requested again as soon as
6899 * the shadow expires. But we still need to check here,
6900 * because POPF has no interrupt shadow.
6901 */
6902 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6903 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02006904 } else
6905 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Gleb Natapov3457e412010-04-28 19:15:38 +03006906
Gleb Natapove85d28f2010-07-29 15:11:52 +03006907 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01006908}
Sean Christophersonc60658d2018-08-23 13:56:53 -07006909
6910int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6911{
6912 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6913}
6914EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6915
6916int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6917 void *insn, int insn_len)
6918{
6919 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6920}
6921EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
Carsten Ottebbd9b642007-10-30 18:44:21 +01006922
Sean Christopherson8764ed52019-04-29 07:04:15 -07006923static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6924{
6925 vcpu->arch.pio.count = 0;
6926 return 1;
6927}
6928
Sean Christopherson45def772019-03-11 20:01:05 -07006929static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6930{
6931 vcpu->arch.pio.count = 0;
6932
6933 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6934 return 1;
6935
6936 return kvm_skip_emulated_instruction(vcpu);
6937}
6938
Sean Christophersondca7f122018-03-08 08:57:27 -08006939static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6940 unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01006941{
Sean Christophersonde3cd112019-04-30 10:36:17 -07006942 unsigned long val = kvm_rax_read(vcpu);
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006943 int ret = emulator_pio_out(vcpu, size, port, &val, 1);
6944
Sean Christopherson8764ed52019-04-29 07:04:15 -07006945 if (ret)
6946 return ret;
Sean Christopherson45def772019-03-11 20:01:05 -07006947
Sean Christopherson8764ed52019-04-29 07:04:15 -07006948 /*
6949 * Workaround userspace that relies on old KVM behavior of %rip being
6950 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6951 */
6952 if (port == 0x7e &&
6953 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6954 vcpu->arch.complete_userspace_io =
6955 complete_fast_pio_out_port_0x7e;
6956 kvm_skip_emulated_instruction(vcpu);
6957 } else {
Sean Christopherson45def772019-03-11 20:01:05 -07006958 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6959 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6960 }
Sean Christopherson8764ed52019-04-29 07:04:15 -07006961 return 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01006962}
Carsten Ottede7d7892007-10-30 18:44:25 +01006963
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006964static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6965{
6966 unsigned long val;
6967
6968 /* We should only ever be called with arch.pio.count equal to 1 */
6969 BUG_ON(vcpu->arch.pio.count != 1);
6970
Sean Christopherson45def772019-03-11 20:01:05 -07006971 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6972 vcpu->arch.pio.count = 0;
6973 return 1;
6974 }
6975
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006976 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006977 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006978
6979 /*
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006980 * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006981 * the copy and tracing
6982 */
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006983 emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1);
Sean Christophersonde3cd112019-04-30 10:36:17 -07006984 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006985
Sean Christopherson45def772019-03-11 20:01:05 -07006986 return kvm_skip_emulated_instruction(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006987}
6988
Sean Christophersondca7f122018-03-08 08:57:27 -08006989static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6990 unsigned short port)
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006991{
6992 unsigned long val;
6993 int ret;
6994
6995 /* For size less than 4 we merge, else we zero extend */
Sean Christophersonde3cd112019-04-30 10:36:17 -07006996 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006997
Sean Christopherson2e3bb4d2020-02-18 15:29:41 -08006998 ret = emulator_pio_in(vcpu, size, port, &val, 1);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05006999 if (ret) {
Sean Christophersonde3cd112019-04-30 10:36:17 -07007000 kvm_rax_write(vcpu, val);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007001 return ret;
7002 }
7003
Sean Christopherson45def772019-03-11 20:01:05 -07007004 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007005 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
7006
7007 return 0;
7008}
Sean Christophersondca7f122018-03-08 08:57:27 -08007009
7010int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
7011{
Sean Christopherson45def772019-03-11 20:01:05 -07007012 int ret;
Sean Christophersondca7f122018-03-08 08:57:27 -08007013
Sean Christophersondca7f122018-03-08 08:57:27 -08007014 if (in)
Sean Christopherson45def772019-03-11 20:01:05 -07007015 ret = kvm_fast_pio_in(vcpu, size, port);
Sean Christophersondca7f122018-03-08 08:57:27 -08007016 else
Sean Christopherson45def772019-03-11 20:01:05 -07007017 ret = kvm_fast_pio_out(vcpu, size, port);
7018 return ret && kvm_skip_emulated_instruction(vcpu);
Sean Christophersondca7f122018-03-08 08:57:27 -08007019}
7020EXPORT_SYMBOL_GPL(kvm_fast_pio);
Tom Lendacky8370c3d2016-11-23 12:01:50 -05007021
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007022static int kvmclock_cpu_down_prep(unsigned int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007023{
Tejun Heo0a3aee02010-12-18 16:28:55 +01007024 __this_cpu_write(cpu_tsc_khz, 0);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007025 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007026}
7027
7028static void tsc_khz_changed(void *data)
7029{
7030 struct cpufreq_freqs *freq = data;
7031 unsigned long khz = 0;
7032
7033 if (data)
7034 khz = freq->new;
7035 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7036 khz = cpufreq_quick_get(raw_smp_processor_id());
7037 if (!khz)
7038 khz = tsc_khz;
Tejun Heo0a3aee02010-12-18 16:28:55 +01007039 __this_cpu_write(cpu_tsc_khz, khz);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007040}
7041
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007042#ifdef CONFIG_X86_64
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007043static void kvm_hyperv_tsc_notifier(void)
7044{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007045 struct kvm *kvm;
7046 struct kvm_vcpu *vcpu;
7047 int cpu;
7048
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007049 mutex_lock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007050 list_for_each_entry(kvm, &vm_list, vm_list)
7051 kvm_make_mclock_inprogress_request(kvm);
7052
7053 hyperv_stop_tsc_emulation();
7054
7055 /* TSC frequency always matches when on Hyper-V */
7056 for_each_present_cpu(cpu)
7057 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
7058 kvm_max_guest_tsc_khz = tsc_khz;
7059
7060 list_for_each_entry(kvm, &vm_list, vm_list) {
7061 struct kvm_arch *ka = &kvm->arch;
7062
7063 spin_lock(&ka->pvclock_gtod_sync_lock);
7064
7065 pvclock_update_vm_gtod_copy(kvm);
7066
7067 kvm_for_each_vcpu(cpu, vcpu, kvm)
7068 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
7069
7070 kvm_for_each_vcpu(cpu, vcpu, kvm)
7071 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
7072
7073 spin_unlock(&ka->pvclock_gtod_sync_lock);
7074 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007075 mutex_unlock(&kvm_lock);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007076}
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007077#endif
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007078
Viresh Kumardf240142019-04-29 15:03:58 +05307079static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007080{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007081 struct kvm *kvm;
7082 struct kvm_vcpu *vcpu;
7083 int i, send_ipi = 0;
7084
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007085 /*
7086 * We allow guests to temporarily run on slowing clocks,
7087 * provided we notify them after, or to run on accelerating
7088 * clocks, provided we notify them before. Thus time never
7089 * goes backwards.
7090 *
7091 * However, we have a problem. We can't atomically update
7092 * the frequency of a given CPU from this function; it is
7093 * merely a notifier, which can be called from any CPU.
7094 * Changing the TSC frequency at arbitrary points in time
7095 * requires a recomputation of local variables related to
7096 * the TSC for each VCPU. We must flag these local variables
7097 * to be updated and be sure the update takes place with the
7098 * new frequency before any guests proceed.
7099 *
7100 * Unfortunately, the combination of hotplug CPU and frequency
7101 * change creates an intractable locking scenario; the order
7102 * of when these callouts happen is undefined with respect to
7103 * CPU hotplug, and they can race with each other. As such,
7104 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7105 * undefined; you can actually have a CPU frequency change take
7106 * place in between the computation of X and the setting of the
7107 * variable. To protect against this problem, all updates of
7108 * the per_cpu tsc_khz variable are done in an interrupt
7109 * protected IPI, and all callers wishing to update the value
7110 * must wait for a synchronous IPI to complete (which is trivial
7111 * if the caller is on the CPU already). This establishes the
7112 * necessary total order on variable updates.
7113 *
7114 * Note that because a guest time update may take place
7115 * anytime after the setting of the VCPU's request bit, the
7116 * correct TSC value must be set before the request. However,
7117 * to ensure the update actually makes it to any guest which
7118 * starts running in hardware virtualization between the set
7119 * and the acquisition of the spinlock, we must also ping the
7120 * CPU after setting the request bit.
7121 *
7122 */
7123
Viresh Kumardf240142019-04-29 15:03:58 +05307124 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007125
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007126 mutex_lock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007127 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03007128 kvm_for_each_vcpu(i, vcpu, kvm) {
Viresh Kumardf240142019-04-29 15:03:58 +05307129 if (vcpu->cpu != cpu)
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007130 continue;
Zachary Amsdenc2855452010-09-18 14:38:15 -10007131 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007132 if (vcpu->cpu != raw_smp_processor_id())
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007133 send_ipi = 1;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007134 }
7135 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007136 mutex_unlock(&kvm_lock);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007137
7138 if (freq->old < freq->new && send_ipi) {
7139 /*
7140 * We upscale the frequency. Must make the guest
7141 * doesn't see old kvmclock values while running with
7142 * the new frequency, otherwise we risk the guest sees
7143 * time go backwards.
7144 *
7145 * In case we update the frequency for another cpu
7146 * (which might be in guest context) send an interrupt
7147 * to kick the cpu out of guest context. Next time
7148 * guest context is entered kvmclock will be updated,
7149 * so the guest will not see stale values.
7150 */
Viresh Kumardf240142019-04-29 15:03:58 +05307151 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007152 }
Viresh Kumardf240142019-04-29 15:03:58 +05307153}
7154
7155static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7156 void *data)
7157{
7158 struct cpufreq_freqs *freq = data;
7159 int cpu;
7160
7161 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7162 return 0;
7163 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7164 return 0;
7165
7166 for_each_cpu(cpu, freq->policy->cpus)
7167 __kvmclock_cpufreq_notifier(freq, cpu);
7168
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007169 return 0;
7170}
7171
7172static struct notifier_block kvmclock_cpufreq_notifier_block = {
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007173 .notifier_call = kvmclock_cpufreq_notifier
7174};
7175
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007176static int kvmclock_cpu_online(unsigned int cpu)
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007177{
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007178 tsc_khz_changed(NULL);
7179 return 0;
Zachary Amsden8cfdc002010-08-19 22:07:21 -10007180}
7181
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007182static void kvm_timer_init(void)
7183{
Zachary Amsdenc2855452010-09-18 14:38:15 -10007184 max_tsc_khz = tsc_khz;
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307185
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007186 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenc2855452010-09-18 14:38:15 -10007187#ifdef CONFIG_CPU_FREQ
7188 struct cpufreq_policy policy;
Borislav Petkov758f5882016-09-04 19:13:57 +02007189 int cpu;
7190
Zachary Amsdenc2855452010-09-18 14:38:15 -10007191 memset(&policy, 0, sizeof(policy));
Avi Kivity3e26f232010-12-16 12:16:34 +02007192 cpu = get_cpu();
7193 cpufreq_get_policy(&policy, cpu);
Zachary Amsdenc2855452010-09-18 14:38:15 -10007194 if (policy.cpuinfo.max_freq)
7195 max_tsc_khz = policy.cpuinfo.max_freq;
Avi Kivity3e26f232010-12-16 12:16:34 +02007196 put_cpu();
Zachary Amsdenc2855452010-09-18 14:38:15 -10007197#endif
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007198 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7199 CPUFREQ_TRANSITION_NOTIFIER);
7200 }
Srivatsa S. Bhat460dd422014-03-11 02:09:01 +05307201
Thomas Gleixner73c1b412016-12-21 20:19:54 +01007202 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007203 kvmclock_cpu_online, kvmclock_cpu_down_prep);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007204}
7205
Andi Kleendd60d212017-07-25 17:20:32 -07007206DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7207EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007208
Gleb Natapovf5132b02011-11-10 14:57:22 +02007209int kvm_is_in_guest(void)
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007210{
Alex,Shi086c9852011-10-20 15:34:01 +08007211 return __this_cpu_read(current_vcpu) != NULL;
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007212}
7213
7214static int kvm_is_user_mode(void)
7215{
7216 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007217
Alex,Shi086c9852011-10-20 15:34:01 +08007218 if (__this_cpu_read(current_vcpu))
7219 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007220
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007221 return user_mode != 0;
7222}
7223
7224static unsigned long kvm_get_guest_ip(void)
7225{
7226 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007227
Alex,Shi086c9852011-10-20 15:34:01 +08007228 if (__this_cpu_read(current_vcpu))
7229 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08007230
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007231 return ip;
7232}
7233
Luwei Kang8479e042019-02-18 19:26:07 -05007234static void kvm_handle_intel_pt_intr(void)
7235{
7236 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7237
7238 kvm_make_request(KVM_REQ_PMI, vcpu);
7239 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7240 (unsigned long *)&vcpu->arch.pmu.global_status);
7241}
7242
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007243static struct perf_guest_info_callbacks kvm_guest_cbs = {
7244 .is_in_guest = kvm_is_in_guest,
7245 .is_user_mode = kvm_is_user_mode,
7246 .get_guest_ip = kvm_get_guest_ip,
Luwei Kang8479e042019-02-18 19:26:07 -05007247 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007248};
7249
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007250#ifdef CONFIG_X86_64
7251static void pvclock_gtod_update_fn(struct work_struct *work)
7252{
Marcelo Tosattid8281992012-11-27 23:29:01 -02007253 struct kvm *kvm;
7254
7255 struct kvm_vcpu *vcpu;
7256 int i;
7257
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007258 mutex_lock(&kvm_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007259 list_for_each_entry(kvm, &vm_list, vm_list)
7260 kvm_for_each_vcpu(i, vcpu, kvm)
Guo Hui Liu105b21b2014-09-12 13:43:19 +08007261 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02007262 atomic_set(&kvm_guest_has_master_clock, 0);
Junaid Shahid0d9ce162019-01-03 17:14:28 -08007263 mutex_unlock(&kvm_lock);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007264}
7265
7266static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7267
7268/*
7269 * Notification about pvclock gtod data update.
7270 */
7271static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7272 void *priv)
7273{
7274 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7275 struct timekeeper *tk = priv;
7276
7277 update_pvclock_gtod(tk);
7278
7279 /* disable master clock if host does not trust, or does not
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007280 * use, TSC based clocksource.
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007281 */
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01007282 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007283 atomic_read(&kvm_guest_has_master_clock) != 0)
7284 queue_work(system_long_wq, &pvclock_gtod_work);
7285
7286 return 0;
7287}
7288
7289static struct notifier_block pvclock_gtod_notifier = {
7290 .notifier_call = pvclock_gtod_notify,
7291};
7292#endif
7293
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007294int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02007295{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007296 int r;
Mathias Krause6b61edf2013-06-26 20:36:23 +02007297 struct kvm_x86_ops *ops = opaque;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007298
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007299 if (kvm_x86_ops) {
7300 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007301 r = -EEXIST;
7302 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007303 }
7304
7305 if (!ops->cpu_has_kvm_support()) {
7306 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007307 r = -EOPNOTSUPP;
7308 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007309 }
7310 if (ops->disabled_by_bios()) {
7311 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007312 r = -EOPNOTSUPP;
7313 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007314 }
7315
Marc Orrb666a4b2018-11-06 14:53:56 -08007316 /*
7317 * KVM explicitly assumes that the guest has an FPU and
7318 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7319 * vCPU's FPU state as a fxregs_state struct.
7320 */
7321 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7322 printk(KERN_ERR "kvm: inadequate fpu\n");
7323 r = -EOPNOTSUPP;
7324 goto out;
7325 }
7326
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007327 r = -ENOMEM;
Paolo Bonzinied8e4812018-12-21 11:25:59 +01007328 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
Marc Orrb666a4b2018-11-06 14:53:56 -08007329 __alignof__(struct fpu), SLAB_ACCOUNT,
7330 NULL);
7331 if (!x86_fpu_cache) {
7332 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7333 goto out;
7334 }
7335
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007336 x86_emulator_cache = kvm_alloc_emulator_cache();
7337 if (!x86_emulator_cache) {
7338 pr_err("kvm: failed to allocate cache for x86 emulator\n");
7339 goto out_free_x86_fpu_cache;
7340 }
7341
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007342 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7343 if (!shared_msrs) {
7344 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007345 goto out_free_x86_emulator_cache;
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007346 }
7347
Avi Kivity97db56c2008-01-13 13:23:56 +02007348 r = kvm_mmu_module_init();
7349 if (r)
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007350 goto out_free_percpu;
Avi Kivity97db56c2008-01-13 13:23:56 +02007351
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007352 kvm_x86_ops = ops;
Paolo Bonzini920c8372014-03-26 15:54:00 +01007353
Sheng Yang7b523452008-04-25 21:13:50 +08007354 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Bandan Dasffb128c2016-07-12 18:18:49 -04007355 PT_DIRTY_MASK, PT64_NX_MASK, 0,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007356 PT_PRESENT_MASK, 0, sme_me_mask);
Zachary Amsdenb820cc02009-09-29 11:38:34 -10007357 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01007358
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007359 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7360
Sean Christophersoncfc48182020-03-02 15:56:23 -08007361 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
Dexuan Cui2acf9232010-06-10 11:27:12 +08007362 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
Sean Christophersoncfc48182020-03-02 15:56:23 -08007363 supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0;
7364 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08007365
Gleb Natapovc5cc4212012-08-05 15:58:30 +03007366 kvm_lapic_init();
Wanpeng Li0c5f81d2019-07-06 09:26:51 +08007367 if (pi_inject_timer == -1)
7368 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007369#ifdef CONFIG_X86_64
7370 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007371
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007372 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007373 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007374#endif
7375
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007376 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007377
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007378out_free_percpu:
7379 free_percpu(shared_msrs);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08007380out_free_x86_emulator_cache:
7381 kmem_cache_destroy(x86_emulator_cache);
Marc Orrb666a4b2018-11-06 14:53:56 -08007382out_free_x86_fpu_cache:
7383 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007384out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007385 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02007386}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007387
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007388void kvm_arch_exit(void)
7389{
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007390#ifdef CONFIG_X86_64
Thomas Gleixner5fa4ec92018-01-31 09:41:40 +01007391 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
Vitaly Kuznetsov0092e432018-01-24 14:23:37 +01007392 clear_hv_tscchange_cb();
7393#endif
David Matlackcef84c32016-12-16 14:30:36 -08007394 kvm_lapic_exit();
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007395 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7396
Jan Kiszka888d2562009-04-17 19:24:58 +02007397 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7398 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7399 CPUFREQ_TRANSITION_NOTIFIER);
Sebastian Andrzej Siewior251a5fd2016-07-13 17:16:33 +00007400 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
Marcelo Tosatti16e8d742012-11-27 23:29:00 -02007401#ifdef CONFIG_X86_64
7402 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7403#endif
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007404 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007405 kvm_mmu_module_exit();
Marcelo Tosatti013f6a52013-01-03 11:41:39 -02007406 free_percpu(shared_msrs);
Marc Orrb666a4b2018-11-06 14:53:56 -08007407 kmem_cache_destroy(x86_fpu_cache);
Zhang Xiantao56c6d282007-11-18 20:43:21 +08007408}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08007409
Joel Schopp5cb56052015-03-02 13:43:31 -06007410int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007411{
7412 ++vcpu->stat.halt_exits;
Paolo Bonzini35754c92015-07-29 12:05:37 +02007413 if (lapic_in_kernel(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03007414 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007415 return 1;
7416 } else {
7417 vcpu->run->exit_reason = KVM_EXIT_HLT;
7418 return 0;
7419 }
7420}
Joel Schopp5cb56052015-03-02 13:43:31 -06007421EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7422
7423int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7424{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007425 int ret = kvm_skip_emulated_instruction(vcpu);
7426 /*
7427 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7428 * KVM_EXIT_DEBUG here.
7429 */
7430 return kvm_vcpu_halt(vcpu) && ret;
Joel Schopp5cb56052015-03-02 13:43:31 -06007431}
Hollis Blanchard8776e512007-10-31 17:24:24 -05007432EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7433
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007434#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007435static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7436 unsigned long clock_type)
7437{
7438 struct kvm_clock_pairing clock_pairing;
Arnd Bergmann899a31f2018-04-23 10:04:26 +02007439 struct timespec64 ts;
Paolo Bonzini80fbd892017-02-08 10:57:24 +01007440 u64 cycle;
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007441 int ret;
7442
7443 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7444 return -KVM_EOPNOTSUPP;
7445
7446 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7447 return -KVM_EOPNOTSUPP;
7448
7449 clock_pairing.sec = ts.tv_sec;
7450 clock_pairing.nsec = ts.tv_nsec;
7451 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7452 clock_pairing.flags = 0;
Liran Alonbcbfbd82018-11-08 00:43:06 +02007453 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007454
7455 ret = 0;
7456 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7457 sizeof(struct kvm_clock_pairing)))
7458 ret = -KVM_EFAULT;
7459
7460 return ret;
7461}
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007462#endif
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007463
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307464/*
7465 * kvm_pv_kick_cpu_op: Kick a vcpu.
7466 *
7467 * @apicid - apicid of vcpu to be kicked.
7468 */
7469static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7470{
Raghavendra K T24d21662013-08-26 14:18:35 +05307471 struct kvm_lapic_irq lapic_irq;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307472
Peter Xu150a84f2019-12-04 20:07:21 +01007473 lapic_irq.shorthand = APIC_DEST_NOSHORT;
Peter Xuc96001c2019-12-04 20:07:18 +01007474 lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
Longpeng(Mike)ebd28fc2017-08-02 11:20:51 +08007475 lapic_irq.level = 0;
Raghavendra K T24d21662013-08-26 14:18:35 +05307476 lapic_irq.dest_id = apicid;
James Sullivan93bbf0b2015-03-18 19:26:03 -06007477 lapic_irq.msi_redir_hint = false;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307478
Raghavendra K T24d21662013-08-26 14:18:35 +05307479 lapic_irq.delivery_mode = APIC_DM_REMRD;
Xiubo Li795a1492015-03-13 17:39:44 +08007480 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307481}
7482
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007483bool kvm_apicv_activated(struct kvm *kvm)
7484{
7485 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0);
7486}
7487EXPORT_SYMBOL_GPL(kvm_apicv_activated);
7488
7489void kvm_apicv_init(struct kvm *kvm, bool enable)
7490{
7491 if (enable)
7492 clear_bit(APICV_INHIBIT_REASON_DISABLE,
7493 &kvm->arch.apicv_inhibit_reasons);
7494 else
7495 set_bit(APICV_INHIBIT_REASON_DISABLE,
7496 &kvm->arch.apicv_inhibit_reasons);
7497}
7498EXPORT_SYMBOL_GPL(kvm_apicv_init);
7499
Wanpeng Li71506292019-06-11 20:23:49 +08007500static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7501{
7502 struct kvm_vcpu *target = NULL;
7503 struct kvm_apic_map *map;
7504
7505 rcu_read_lock();
7506 map = rcu_dereference(kvm->arch.apic_map);
7507
7508 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7509 target = map->phys_map[dest_id]->vcpu;
7510
7511 rcu_read_unlock();
7512
Wanpeng Li266e85a2019-07-24 17:43:13 +08007513 if (target && READ_ONCE(target->ready))
Wanpeng Li71506292019-06-11 20:23:49 +08007514 kvm_vcpu_yield_to(target);
7515}
7516
Hollis Blanchard8776e512007-10-31 17:24:24 -05007517int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7518{
7519 unsigned long nr, a0, a1, a2, a3, ret;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007520 int op_64_bit;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007521
Radim Krčmář696ca772018-05-24 17:50:56 +02007522 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7523 return kvm_hv_hypercall(vcpu);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02007524
Sean Christophersonde3cd112019-04-30 10:36:17 -07007525 nr = kvm_rax_read(vcpu);
7526 a0 = kvm_rbx_read(vcpu);
7527 a1 = kvm_rcx_read(vcpu);
7528 a2 = kvm_rdx_read(vcpu);
7529 a3 = kvm_rsi_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007530
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007531 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007532
Nadav Amita449c7a2014-06-18 17:19:24 +03007533 op_64_bit = is_64_bit_mode(vcpu);
7534 if (!op_64_bit) {
Hollis Blanchard8776e512007-10-31 17:24:24 -05007535 nr &= 0xFFFFFFFF;
7536 a0 &= 0xFFFFFFFF;
7537 a1 &= 0xFFFFFFFF;
7538 a2 &= 0xFFFFFFFF;
7539 a3 &= 0xFFFFFFFF;
7540 }
7541
Jan Kiszka07708c42009-08-03 18:43:28 +02007542 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7543 ret = -KVM_EPERM;
Radim Krčmář696ca772018-05-24 17:50:56 +02007544 goto out;
Jan Kiszka07708c42009-08-03 18:43:28 +02007545 }
7546
Hollis Blanchard8776e512007-10-31 17:24:24 -05007547 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02007548 case KVM_HC_VAPIC_POLL_IRQ:
7549 ret = 0;
7550 break;
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307551 case KVM_HC_KICK_CPU:
7552 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
Wanpeng Li266e85a2019-07-24 17:43:13 +08007553 kvm_sched_yield(vcpu->kvm, a1);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05307554 ret = 0;
7555 break;
Arnd Bergmann8ef81a92017-02-09 16:10:42 +01007556#ifdef CONFIG_X86_64
Marcelo Tosatti55dd00a2017-01-24 15:09:39 -02007557 case KVM_HC_CLOCK_PAIRING:
7558 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7559 break;
Sean Christopherson1ed199a2019-01-23 09:22:39 -08007560#endif
Wanpeng Li4180bf12018-07-23 14:39:54 +08007561 case KVM_HC_SEND_IPI:
7562 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7563 break;
Wanpeng Li71506292019-06-11 20:23:49 +08007564 case KVM_HC_SCHED_YIELD:
7565 kvm_sched_yield(vcpu->kvm, a0);
7566 ret = 0;
7567 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05007568 default:
7569 ret = -KVM_ENOSYS;
7570 break;
7571 }
Radim Krčmář696ca772018-05-24 17:50:56 +02007572out:
Nadav Amita449c7a2014-06-18 17:19:24 +03007573 if (!op_64_bit)
7574 ret = (u32)ret;
Sean Christophersonde3cd112019-04-30 10:36:17 -07007575 kvm_rax_write(vcpu, ret);
Marian Rotariu6356ee02018-04-30 12:23:01 +03007576
Amit Shahf11c3a82008-02-21 01:00:30 +05307577 ++vcpu->stat.hypercalls;
Marian Rotariu6356ee02018-04-30 12:23:01 +03007578 return kvm_skip_emulated_instruction(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007579}
7580EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7581
Jan Kiszkab6785de2012-09-20 07:43:17 +02007582static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
Hollis Blanchard8776e512007-10-31 17:24:24 -05007583{
Avi Kivityd6aa1002011-04-20 15:47:13 +03007584 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007585 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007586 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007587
Hollis Blanchard8776e512007-10-31 17:24:24 -05007588 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007589
Dmitry Vyukovce2e8522017-01-17 14:51:04 +01007590 return emulator_write_emulated(ctxt, rip, instruction, 3,
7591 &ctxt->exception);
Hollis Blanchard8776e512007-10-31 17:24:24 -05007592}
7593
Avi Kivity851ba692009-08-24 11:10:17 +03007594static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007595{
Matt Gingell782d4222015-11-16 15:26:00 -08007596 return vcpu->run->request_interrupt_window &&
7597 likely(!pic_in_kernel(vcpu->kvm));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007598}
7599
Avi Kivity851ba692009-08-24 11:10:17 +03007600static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007601{
Avi Kivity851ba692009-08-24 11:10:17 +03007602 struct kvm_run *kvm_run = vcpu->run;
7603
Jan Kiszka91586a32009-10-05 13:07:21 +02007604 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Paolo Bonzinif0778252015-04-01 15:06:40 +02007605 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02007606 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007607 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Matt Gingell127a4572015-11-17 17:32:05 +01007608 kvm_run->ready_for_interrupt_injection =
7609 pic_in_kernel(vcpu->kvm) ||
Matt Gingell782d4222015-11-16 15:26:00 -08007610 kvm_vcpu_ready_for_interrupt_injection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05007611}
7612
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007613static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7614{
7615 int max_irr, tpr;
7616
7617 if (!kvm_x86_ops->update_cr8_intercept)
7618 return;
7619
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01007620 if (!lapic_in_kernel(vcpu))
Avi Kivity88c808f2009-08-17 22:49:40 +03007621 return;
7622
Andrey Smetanind62caab2015-11-10 15:36:33 +03007623 if (vcpu->arch.apicv_active)
7624 return;
7625
Gleb Natapov8db3baa2009-05-11 13:35:54 +03007626 if (!vcpu->arch.apic->vapic_addr)
7627 max_irr = kvm_lapic_find_highest_irr(vcpu);
7628 else
7629 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007630
7631 if (max_irr != -1)
7632 max_irr >>= 4;
7633
7634 tpr = kvm_lapic_get_cr8(vcpu);
7635
7636 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7637}
7638
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007639static int inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007640{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007641 int r;
7642
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007643 /* try to reinject previous events if any */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007644
Liran Alon1a680e32018-03-23 03:01:33 +03007645 if (vcpu->arch.exception.injected)
7646 kvm_x86_ops->queue_exception(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007647 /*
Liran Alona042c262018-03-23 03:01:32 +03007648 * Do not inject an NMI or interrupt if there is a pending
7649 * exception. Exceptions and interrupts are recognized at
7650 * instruction boundaries, i.e. the start of an instruction.
7651 * Trap-like exceptions, e.g. #DB, have higher priority than
7652 * NMIs and interrupts, i.e. traps are recognized before an
7653 * NMI/interrupt that's pending on the same instruction.
7654 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7655 * priority, but are only generated (pended) during instruction
7656 * execution, i.e. a pending fault-like exception means the
7657 * fault occurred on the *previous* instruction and must be
7658 * serviced prior to recognizing any new events in order to
7659 * fully complete the previous instruction.
Wanpeng Li664f8e22017-08-24 03:35:09 -07007660 */
Liran Alon1a680e32018-03-23 03:01:33 +03007661 else if (!vcpu->arch.exception.pending) {
7662 if (vcpu->arch.nmi_injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007663 kvm_x86_ops->set_nmi(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007664 else if (vcpu->arch.interrupt.injected)
Wanpeng Li664f8e22017-08-24 03:35:09 -07007665 kvm_x86_ops->set_irq(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007666 }
7667
Liran Alon1a680e32018-03-23 03:01:33 +03007668 /*
7669 * Call check_nested_events() even if we reinjected a previous event
7670 * in order for caller to determine if it should require immediate-exit
7671 * from L2 to L1 due to pending L1 events which require exit
7672 * from L2 to L1.
7673 */
Wanpeng Li664f8e22017-08-24 03:35:09 -07007674 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007675 r = kvm_x86_ops->check_nested_events(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007676 if (r != 0)
7677 return r;
7678 }
7679
7680 /* try to inject new event if pending */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03007681 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02007682 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7683 vcpu->arch.exception.has_error_code,
7684 vcpu->arch.exception.error_code);
Nadav Amitd6e8c852014-07-24 14:51:24 +03007685
Liran Alon1a680e32018-03-23 03:01:33 +03007686 WARN_ON_ONCE(vcpu->arch.exception.injected);
Wanpeng Li664f8e22017-08-24 03:35:09 -07007687 vcpu->arch.exception.pending = false;
7688 vcpu->arch.exception.injected = true;
7689
Nadav Amitd6e8c852014-07-24 14:51:24 +03007690 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7691 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7692 X86_EFLAGS_RF);
7693
Jim Mattsonf10c7292018-10-16 14:29:23 -07007694 if (vcpu->arch.exception.nr == DB_VECTOR) {
7695 /*
7696 * This code assumes that nSVM doesn't use
7697 * check_nested_events(). If it does, the
7698 * DR6/DR7 changes should happen before L1
7699 * gets a #VMEXIT for an intercepted #DB in
7700 * L2. (Under VMX, on the other hand, the
7701 * DR6/DR7 changes should not happen in the
7702 * event of a VM-exit to L1 for an intercepted
7703 * #DB in L2.)
7704 */
7705 kvm_deliver_exception_payload(vcpu);
7706 if (vcpu->arch.dr7 & DR7_GD) {
7707 vcpu->arch.dr7 &= ~DR7_GD;
7708 kvm_update_dr7(vcpu);
7709 }
Nadav Amit6bdf0662014-09-30 20:49:14 +03007710 }
7711
Wanpeng Licfcd20e2017-07-13 18:30:39 -07007712 kvm_x86_ops->queue_exception(vcpu);
Liran Alon1a680e32018-03-23 03:01:33 +03007713 }
7714
7715 /* Don't consider new event if we re-injected an event */
7716 if (kvm_event_needs_reinjection(vcpu))
7717 return 0;
7718
7719 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7720 kvm_x86_ops->smi_allowed(vcpu)) {
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007721 vcpu->arch.smi_pending = false;
Liran Alon52797bf2017-11-15 13:43:14 +02007722 ++vcpu->arch.smi_count;
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007723 enter_smm(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02007724 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
Yuki Shibuya321c5652016-03-24 05:17:03 +00007725 --vcpu->arch.nmi_pending;
7726 vcpu->arch.nmi_injected = true;
7727 kvm_x86_ops->set_nmi(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007728 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
Bandan Das9242b5b2014-07-08 00:30:23 -04007729 /*
7730 * Because interrupts can be injected asynchronously, we are
7731 * calling check_nested_events again here to avoid a race condition.
7732 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7733 * proposal and current concerns. Perhaps we should be setting
7734 * KVM_REQ_EVENT only on certain events and not unconditionally?
7735 */
7736 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
Sean Christophersona1c77ab2020-03-02 22:27:35 -08007737 r = kvm_x86_ops->check_nested_events(vcpu);
Bandan Das9242b5b2014-07-08 00:30:23 -04007738 if (r != 0)
7739 return r;
7740 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007741 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007742 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7743 false);
7744 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007745 }
7746 }
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007747
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007748 return 0;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007749}
7750
Avi Kivity7460fb4a2011-09-20 13:43:14 +03007751static void process_nmi(struct kvm_vcpu *vcpu)
7752{
7753 unsigned limit = 2;
7754
7755 /*
7756 * x86 is limited to one NMI running, and one NMI pending after it.
7757 * If an NMI is already in progress, limit further NMIs to just one.
7758 * Otherwise, allow two (and we'll inject the first one immediately).
7759 */
7760 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7761 limit = 1;
7762
7763 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7764 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7765 kvm_make_request(KVM_REQ_EVENT, vcpu);
7766}
7767
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007768static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007769{
7770 u32 flags = 0;
7771 flags |= seg->g << 23;
7772 flags |= seg->db << 22;
7773 flags |= seg->l << 21;
7774 flags |= seg->avl << 20;
7775 flags |= seg->present << 15;
7776 flags |= seg->dpl << 13;
7777 flags |= seg->s << 12;
7778 flags |= seg->type << 8;
7779 return flags;
7780}
7781
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007782static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007783{
7784 struct kvm_segment seg;
7785 int offset;
7786
7787 kvm_get_segment(vcpu, &seg, n);
7788 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7789
7790 if (n < 3)
7791 offset = 0x7f84 + n * 12;
7792 else
7793 offset = 0x7f2c + (n - 3) * 12;
7794
7795 put_smstate(u32, buf, offset + 8, seg.base);
7796 put_smstate(u32, buf, offset + 4, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007797 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007798}
7799
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007800#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007801static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007802{
7803 struct kvm_segment seg;
7804 int offset;
7805 u16 flags;
7806
7807 kvm_get_segment(vcpu, &seg, n);
7808 offset = 0x7e00 + n * 16;
7809
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007810 flags = enter_smm_get_segment_flags(&seg) >> 8;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007811 put_smstate(u16, buf, offset, seg.selector);
7812 put_smstate(u16, buf, offset + 2, flags);
7813 put_smstate(u32, buf, offset + 4, seg.limit);
7814 put_smstate(u64, buf, offset + 8, seg.base);
7815}
Alexander Kuleshovefbb2882015-09-06 19:35:41 +06007816#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007817
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007818static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007819{
7820 struct desc_ptr dt;
7821 struct kvm_segment seg;
7822 unsigned long val;
7823 int i;
7824
7825 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7826 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7827 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7828 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7829
7830 for (i = 0; i < 8; i++)
7831 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7832
7833 kvm_get_dr(vcpu, 6, &val);
7834 put_smstate(u32, buf, 0x7fcc, (u32)val);
7835 kvm_get_dr(vcpu, 7, &val);
7836 put_smstate(u32, buf, 0x7fc8, (u32)val);
7837
7838 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7839 put_smstate(u32, buf, 0x7fc4, seg.selector);
7840 put_smstate(u32, buf, 0x7f64, seg.base);
7841 put_smstate(u32, buf, 0x7f60, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007842 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007843
7844 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7845 put_smstate(u32, buf, 0x7fc0, seg.selector);
7846 put_smstate(u32, buf, 0x7f80, seg.base);
7847 put_smstate(u32, buf, 0x7f7c, seg.limit);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007848 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007849
7850 kvm_x86_ops->get_gdt(vcpu, &dt);
7851 put_smstate(u32, buf, 0x7f74, dt.address);
7852 put_smstate(u32, buf, 0x7f70, dt.size);
7853
7854 kvm_x86_ops->get_idt(vcpu, &dt);
7855 put_smstate(u32, buf, 0x7f58, dt.address);
7856 put_smstate(u32, buf, 0x7f54, dt.size);
7857
7858 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007859 enter_smm_save_seg_32(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007860
7861 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7862
7863 /* revision id */
7864 put_smstate(u32, buf, 0x7efc, 0x00020000);
7865 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7866}
7867
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007868#ifdef CONFIG_X86_64
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007869static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007870{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007871 struct desc_ptr dt;
7872 struct kvm_segment seg;
7873 unsigned long val;
7874 int i;
7875
7876 for (i = 0; i < 16; i++)
7877 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7878
7879 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7880 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7881
7882 kvm_get_dr(vcpu, 6, &val);
7883 put_smstate(u64, buf, 0x7f68, val);
7884 kvm_get_dr(vcpu, 7, &val);
7885 put_smstate(u64, buf, 0x7f60, val);
7886
7887 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7888 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7889 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7890
7891 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7892
7893 /* revision id */
7894 put_smstate(u32, buf, 0x7efc, 0x00020064);
7895
7896 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7897
7898 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7899 put_smstate(u16, buf, 0x7e90, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007900 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007901 put_smstate(u32, buf, 0x7e94, seg.limit);
7902 put_smstate(u64, buf, 0x7e98, seg.base);
7903
7904 kvm_x86_ops->get_idt(vcpu, &dt);
7905 put_smstate(u32, buf, 0x7e84, dt.size);
7906 put_smstate(u64, buf, 0x7e88, dt.address);
7907
7908 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7909 put_smstate(u16, buf, 0x7e70, seg.selector);
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007910 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007911 put_smstate(u32, buf, 0x7e74, seg.limit);
7912 put_smstate(u64, buf, 0x7e78, seg.base);
7913
7914 kvm_x86_ops->get_gdt(vcpu, &dt);
7915 put_smstate(u32, buf, 0x7e64, dt.size);
7916 put_smstate(u64, buf, 0x7e68, dt.address);
7917
7918 for (i = 0; i < 6; i++)
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007919 enter_smm_save_seg_64(vcpu, buf, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007920}
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007921#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007922
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007923static void enter_smm(struct kvm_vcpu *vcpu)
Paolo Bonzini64d60672015-05-07 11:36:11 +02007924{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007925 struct kvm_segment cs, ds;
Paolo Bonzini18c36262015-08-07 12:27:54 +02007926 struct desc_ptr dt;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007927 char buf[512];
7928 u32 cr0;
7929
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007930 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007931 memset(buf, 0, 512);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007932#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007933 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007934 enter_smm_save_state_64(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007935 else
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007936#endif
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02007937 enter_smm_save_state_32(vcpu, buf);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007938
Ladi Prosek0234bf82017-10-11 16:54:40 +02007939 /*
7940 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7941 * vCPU state (e.g. leave guest mode) after we've saved the state into
7942 * the SMM state-save area.
7943 */
7944 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7945
7946 vcpu->arch.hflags |= HF_SMM_MASK;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007947 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007948
7949 if (kvm_x86_ops->get_nmi_mask(vcpu))
7950 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7951 else
7952 kvm_x86_ops->set_nmi_mask(vcpu, true);
7953
7954 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7955 kvm_rip_write(vcpu, 0x8000);
7956
7957 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7958 kvm_x86_ops->set_cr0(vcpu, cr0);
7959 vcpu->arch.cr0 = cr0;
7960
7961 kvm_x86_ops->set_cr4(vcpu, 0);
7962
Paolo Bonzini18c36262015-08-07 12:27:54 +02007963 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7964 dt.address = dt.size = 0;
7965 kvm_x86_ops->set_idt(vcpu, &dt);
7966
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007967 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7968
7969 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7970 cs.base = vcpu->arch.smbase;
7971
7972 ds.selector = 0;
7973 ds.base = 0;
7974
7975 cs.limit = ds.limit = 0xffffffff;
7976 cs.type = ds.type = 0x3;
7977 cs.dpl = ds.dpl = 0;
7978 cs.db = ds.db = 0;
7979 cs.s = ds.s = 1;
7980 cs.l = ds.l = 0;
7981 cs.g = ds.g = 1;
7982 cs.avl = ds.avl = 0;
7983 cs.present = ds.present = 1;
7984 cs.unusable = ds.unusable = 0;
7985 cs.padding = ds.padding = 0;
7986
7987 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7988 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7989 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7990 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7991 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7992 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7993
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007994#ifdef CONFIG_X86_64
Radim Krčmářd6321d42017-08-05 00:12:49 +02007995 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007996 kvm_x86_ops->set_efer(vcpu, 0);
Sean Christophersonb68f3cc2019-04-02 08:10:48 -07007997#endif
Paolo Bonzini660a5d52015-05-05 11:50:23 +02007998
7999 kvm_update_cpuid(vcpu);
8000 kvm_mmu_reset_context(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008001}
8002
Paolo Bonziniee2cd4b2016-06-01 22:26:01 +02008003static void process_smi(struct kvm_vcpu *vcpu)
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008004{
8005 vcpu->arch.smi_pending = true;
8006 kvm_make_request(KVM_REQ_EVENT, vcpu);
8007}
8008
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008009void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
8010 unsigned long *vcpu_bitmap)
8011{
8012 cpumask_var_t cpus;
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008013
8014 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
8015
Mao Wenandb5a95e2019-11-19 11:06:40 +08008016 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
8017 vcpu_bitmap, cpus);
Nitesh Narayan Lal7ee30bc2019-11-07 07:53:43 -05008018
8019 free_cpumask_var(cpus);
8020}
8021
Paolo Bonzini2860c4b2016-01-07 15:05:10 +01008022void kvm_make_scan_ioapic_request(struct kvm *kvm)
8023{
8024 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
8025}
8026
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008027void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
8028{
8029 if (!lapic_in_kernel(vcpu))
8030 return;
8031
8032 vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm);
8033 kvm_apic_update_apicv(vcpu);
8034 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
8035}
8036EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
8037
8038/*
8039 * NOTE: Do not hold any lock prior to calling this.
8040 *
8041 * In particular, kvm_request_apicv_update() expects kvm->srcu not to be
8042 * locked, because it calls __x86_set_memory_region() which does
8043 * synchronize_srcu(&kvm->srcu).
8044 */
8045void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
8046{
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06008047 if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
8048 !kvm_x86_ops->check_apicv_inhibit_reasons(bit))
8049 return;
8050
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008051 if (activate) {
8052 if (!test_and_clear_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
8053 !kvm_apicv_activated(kvm))
8054 return;
8055 } else {
8056 if (test_and_set_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
8057 kvm_apicv_activated(kvm))
8058 return;
8059 }
8060
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -06008061 trace_kvm_apicv_update_request(activate, bit);
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06008062 if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
8063 kvm_x86_ops->pre_update_apicv_exec_ctrl(kvm, activate);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008064 kvm_make_all_cpus_request(kvm, KVM_REQ_APICV_UPDATE);
8065}
8066EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
8067
Yang Zhang3d81bc72013-04-11 19:25:13 +08008068static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008069{
Wanpeng Lidcbd3e42018-12-17 10:43:23 +08008070 if (!kvm_apic_present(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008071 return;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008072
Andrey Smetanin63086302015-11-10 15:36:32 +03008073 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008074
Steve Rutherfordb053b2a2015-07-29 23:32:35 -07008075 if (irqchip_split(vcpu->kvm))
Andrey Smetanin63086302015-11-10 15:36:32 +03008076 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008077 else {
Liran Alonfa59cc02017-12-24 18:12:53 +02008078 if (vcpu->arch.apicv_active)
Andrey Smetanind62caab2015-11-10 15:36:33 +03008079 kvm_x86_ops->sync_pir_to_irr(vcpu);
Wanpeng Lie97f8522018-11-20 16:34:18 +08008080 if (ioapic_in_kernel(vcpu->kvm))
8081 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
Radim Krčmářdb2bdcb2015-10-08 20:23:34 +02008082 }
Liran Alone40ff1d2018-03-21 02:50:31 +02008083
8084 if (is_guest_mode(vcpu))
8085 vcpu->arch.load_eoi_exitmap_pending = true;
8086 else
8087 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
8088}
8089
8090static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
8091{
8092 u64 eoi_exit_bitmap[4];
8093
8094 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
8095 return;
8096
Andrey Smetanin5c9194122015-11-10 15:36:34 +03008097 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
8098 vcpu_to_synic(vcpu)->vec_bitmap, 256);
8099 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008100}
8101
Michal Hocko93065ac2018-08-21 21:52:33 -07008102int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
8103 unsigned long start, unsigned long end,
8104 bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +01008105{
8106 unsigned long apic_address;
8107
8108 /*
8109 * The physical address of apic access page is stored in the VMCS.
8110 * Update it when it becomes invalid.
8111 */
8112 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
8113 if (start <= apic_address && apic_address < end)
8114 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
Michal Hocko93065ac2018-08-21 21:52:33 -07008115
8116 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +01008117}
8118
Tang Chen4256f432014-09-24 15:57:54 +08008119void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
8120{
Tang Chenc24ae0d2014-09-24 15:57:58 +08008121 struct page *page = NULL;
8122
Paolo Bonzini35754c92015-07-29 12:05:37 +02008123 if (!lapic_in_kernel(vcpu))
Paolo Bonzinif439ed22014-10-02 13:53:24 +02008124 return;
8125
Tang Chen4256f432014-09-24 15:57:54 +08008126 if (!kvm_x86_ops->set_apic_access_page_addr)
8127 return;
8128
Tang Chenc24ae0d2014-09-24 15:57:58 +08008129 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Andrea Arcangelie8fd5e92015-05-08 14:32:56 +02008130 if (is_error_page(page))
8131 return;
Tang Chenc24ae0d2014-09-24 15:57:58 +08008132 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
8133
8134 /*
8135 * Do not pin apic access page in memory, the MMU notifier
8136 * will call us again if it is migrated or swapped out.
8137 */
8138 put_page(page);
Tang Chen4256f432014-09-24 15:57:54 +08008139}
Tang Chen4256f432014-09-24 15:57:54 +08008140
Sean Christophersond264ee02018-08-27 15:21:12 -07008141void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
8142{
8143 smp_send_reschedule(vcpu->cpu);
8144}
8145EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
8146
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008147/*
Paolo Bonzini362c6982015-02-06 12:48:04 +01008148 * Returns 1 to let vcpu_run() continue the guest execution loop without
Takuya Yoshikawa9357d932013-12-13 15:08:38 +09008149 * exiting to the userspace. Otherwise, the value will be returned to the
8150 * userspace.
8151 */
Avi Kivity851ba692009-08-24 11:10:17 +03008152static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008153{
8154 int r;
Matt Gingell62a193e2015-11-16 15:26:07 -08008155 bool req_int_win =
8156 dm_request_for_irq_injection(vcpu) &&
8157 kvm_cpu_accept_dm_intr(vcpu);
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008158 enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE;
Matt Gingell62a193e2015-11-16 15:26:07 -08008159
Jan Kiszka730dca42013-04-28 10:50:52 +02008160 bool req_immediate_exit = false;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008161
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008162 if (kvm_request_pending(vcpu)) {
Jim Mattson671ddc72019-10-15 10:44:05 -07008163 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8164 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8165 r = 0;
8166 goto out;
8167 }
8168 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008169 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05008170 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008171 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03008172 __kvm_migrate_timers(vcpu);
Marcelo Tosattid8281992012-11-27 23:29:01 -02008173 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8174 kvm_gen_update_masterclock(vcpu->kvm);
Marcelo Tosatti0061d532013-05-09 20:21:41 -03008175 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8176 kvm_gen_kvmclock_update(vcpu);
Zachary Amsden34c238a2010-09-18 14:38:14 -10008177 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8178 r = kvm_guest_time_update(vcpu);
Zachary Amsden8cfdc002010-08-19 22:07:21 -10008179 if (unlikely(r))
8180 goto out;
8181 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008182 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03008183 kvm_mmu_sync_roots(vcpu);
Junaid Shahid6e427822018-06-27 14:59:08 -07008184 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
8185 kvm_mmu_load_cr3(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008186 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008187 kvm_vcpu_flush_tlb(vcpu, true);
Avi Kivitya8eeb042010-05-10 12:34:53 +03008188 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008189 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02008190 r = 0;
8191 goto out;
8192 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03008193 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008194 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07008195 vcpu->mmio_needed = 0;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01008196 r = 0;
8197 goto out;
8198 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008199 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8200 /* Page is swapped out. Do synthetic halt */
8201 vcpu->arch.apf.halted = true;
8202 r = 1;
8203 goto out;
8204 }
Glauber Costac9aaa892011-07-11 15:28:14 -04008205 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8206 record_steal_time(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02008207 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8208 process_smi(vcpu);
Avi Kivity7460fb4a2011-09-20 13:43:14 +03008209 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8210 process_nmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008211 if (kvm_check_request(KVM_REQ_PMU, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008212 kvm_pmu_handle_event(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +02008213 if (kvm_check_request(KVM_REQ_PMI, vcpu))
Wei Huangc6702c92015-06-19 13:44:45 +02008214 kvm_pmu_deliver_pmi(vcpu);
Steve Rutherford7543a632015-07-29 23:21:41 -07008215 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8216 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8217 if (test_bit(vcpu->arch.pending_ioapic_eoi,
Andrey Smetanin63086302015-11-10 15:36:32 +03008218 vcpu->arch.ioapic_handled_vectors)) {
Steve Rutherford7543a632015-07-29 23:21:41 -07008219 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8220 vcpu->run->eoi.vector =
8221 vcpu->arch.pending_ioapic_eoi;
8222 r = 0;
8223 goto out;
8224 }
8225 }
Yang Zhang3d81bc72013-04-11 19:25:13 +08008226 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8227 vcpu_scan_ioapic(vcpu);
Liran Alone40ff1d2018-03-21 02:50:31 +02008228 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8229 vcpu_load_eoi_exitmap(vcpu);
Tang Chen4256f432014-09-24 15:57:54 +08008230 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8231 kvm_vcpu_reload_apic_access_page(vcpu);
Andrey Smetanin2ce79182015-07-03 15:01:41 +03008232 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8233 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8234 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8235 r = 0;
8236 goto out;
8237 }
Andrey Smetanine516ceb2015-09-16 12:29:48 +03008238 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8239 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8240 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8241 r = 0;
8242 goto out;
8243 }
Andrey Smetanindb3975712015-11-10 15:36:35 +03008244 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8245 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8246 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8247 r = 0;
8248 goto out;
8249 }
Andrey Smetaninf3b138c2015-12-28 18:27:24 +03008250
8251 /*
8252 * KVM_REQ_HV_STIMER has to be processed after
8253 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8254 * depend on the guest clock being up-to-date
8255 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03008256 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8257 kvm_hv_process_stimers(vcpu);
Suravee Suthikulpanit8df14af2019-11-14 14:15:06 -06008258 if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
8259 kvm_vcpu_update_apicv(vcpu);
Avi Kivity2f52d582008-01-16 12:49:30 +02008260 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008261
Avi Kivityb463a6f2010-07-20 15:06:17 +03008262 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
Paolo Bonzini0f1e2612016-12-17 16:05:19 +01008263 ++vcpu->stat.req_event;
Jan Kiszka66450a22013-03-13 12:42:34 +01008264 kvm_apic_accept_events(vcpu);
8265 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8266 r = 1;
8267 goto out;
8268 }
8269
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008270 if (inject_pending_event(vcpu) != 0)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008271 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008272 else {
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008273 /* Enable SMI/NMI/IRQ window open exits if needed.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008274 *
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008275 * SMIs have three cases:
8276 * 1) They can be nested, and then there is nothing to
8277 * do here because RSM will cause a vmexit anyway.
8278 * 2) There is an ISA-specific reason why SMI cannot be
8279 * injected, and the moment when this changes can be
8280 * intercepted.
8281 * 3) Or the SMI can be pending because
8282 * inject_pending_event has completed the injection
8283 * of an IRQ or NMI from the previous vmexit, and
8284 * then we request an immediate exit to inject the
8285 * SMI.
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008286 */
8287 if (vcpu->arch.smi_pending && !is_smm(vcpu))
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008288 if (!kvm_x86_ops->enable_smi_window(vcpu))
8289 req_immediate_exit = true;
Yuki Shibuya321c5652016-03-24 05:17:03 +00008290 if (vcpu->arch.nmi_pending)
8291 kvm_x86_ops->enable_nmi_window(vcpu);
8292 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8293 kvm_x86_ops->enable_irq_window(vcpu);
Wanpeng Li664f8e22017-08-24 03:35:09 -07008294 WARN_ON(vcpu->arch.exception.pending);
Yuki Shibuya321c5652016-03-24 05:17:03 +00008295 }
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03008296
Avi Kivity3842d132010-07-27 12:30:24 +03008297 if (kvm_lapic_enabled(vcpu)) {
8298 update_cr8_intercept(vcpu);
8299 kvm_lapic_sync_to_vapic(vcpu);
8300 }
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008301 }
Avi Kivityb93463a2007-10-25 16:52:32 +02008302
Avi Kivityd8368af2012-05-14 18:07:56 +03008303 r = kvm_mmu_reload(vcpu);
8304 if (unlikely(r)) {
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008305 goto cancel_injection;
Avi Kivityd8368af2012-05-14 18:07:56 +03008306 }
8307
Jan Kiszka42dbaa52008-12-15 13:52:10 +01008308 preempt_disable();
8309
8310 kvm_x86_ops->prepare_guest_switch(vcpu);
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008311
8312 /*
8313 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8314 * IPI are then delayed after guest entry, which ensures that they
8315 * result in virtual interrupt delivery.
8316 */
8317 local_irq_disable();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008318 vcpu->mode = IN_GUEST_MODE;
8319
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008320 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8321
Lan Tianyu0f127d12016-03-13 11:10:29 +08008322 /*
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008323 * 1) We should set ->mode before checking ->requests. Please see
Andrew Jonescde9af62017-04-26 22:32:24 +02008324 * the comment in kvm_vcpu_exiting_guest_mode().
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008325 *
Luwei Kang81b01662019-01-31 16:52:02 +08008326 * 2) For APICv, we should set ->mode before checking PID.ON. This
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008327 * pairs with the memory barrier implicit in pi_test_and_set_on
8328 * (see vmx_deliver_posted_interrupt).
8329 *
8330 * 3) This also orders the write to mode from any reads to the page
8331 * tables done while the VCPU is running. Please see the comment
8332 * in kvm_flush_remote_tlbs.
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008333 */
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008334 smp_mb__after_srcu_read_unlock();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008335
Paolo Bonzinib95234c2016-12-19 13:57:33 +01008336 /*
8337 * This handles the case where a posted interrupt was
8338 * notified with kvm_vcpu_kick.
8339 */
Liran Alonfa59cc02017-12-24 18:12:53 +02008340 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8341 kvm_x86_ops->sync_pir_to_irr(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008342
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02008343 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008344 || need_resched() || signal_pending(current)) {
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008345 vcpu->mode = OUTSIDE_GUEST_MODE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008346 smp_wmb();
8347 local_irq_enable();
8348 preempt_enable();
Michael S. Tsirkin01b71912013-11-04 22:36:25 +02008349 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008350 r = 1;
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008351 goto cancel_injection;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008352 }
8353
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008354 if (req_immediate_exit) {
8355 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sean Christophersond264ee02018-08-27 15:21:12 -07008356 kvm_x86_ops->request_immediate_exit(vcpu);
Paolo Bonzinic43203c2016-06-01 22:26:00 +02008357 }
Nadav Har'Eld6185f22011-09-22 13:52:56 +03008358
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008359 trace_kvm_entry(vcpu->vcpu_id);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02008360 guest_enter_irqoff();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008361
Sean Christopherson2620fe22020-01-17 11:30:51 -08008362 fpregs_assert_state_consistent();
8363 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8364 switch_fpu_return();
Rik van Riel5f409e22019-04-03 18:41:52 +02008365
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008366 if (unlikely(vcpu->arch.switch_db_regs)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008367 set_debugreg(0, 7);
8368 set_debugreg(vcpu->arch.eff_db[0], 0);
8369 set_debugreg(vcpu->arch.eff_db[1], 1);
8370 set_debugreg(vcpu->arch.eff_db[2], 2);
8371 set_debugreg(vcpu->arch.eff_db[3], 3);
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008372 set_debugreg(vcpu->arch.dr6, 6);
Nadav Amitae561ed2015-04-02 03:10:37 +03008373 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008374 }
8375
Avi Kivity851ba692009-08-24 11:10:17 +03008376 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008377
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008378 /*
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008379 * Do this here before restoring debug registers on the host. And
8380 * since we do this before handling the vmexit, a DR access vmexit
8381 * can (a) read the correct value of the debug registers, (b) set
8382 * KVM_DEBUGREG_WONT_EXIT again.
8383 */
8384 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008385 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8386 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
Paolo Bonzini70e4da72016-02-26 12:28:40 +01008387 kvm_update_dr0123(vcpu);
8388 kvm_update_dr6(vcpu);
8389 kvm_update_dr7(vcpu);
8390 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
Paolo Bonzinic77fb5f2014-02-21 10:17:24 +01008391 }
8392
8393 /*
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008394 * If the guest has used debug registers, at least dr7
8395 * will be disabled while returning to the host.
8396 * If we don't have active breakpoints in the host, we don't
8397 * care about the messed up debug address registers. But if
8398 * we have some of them active, restore the old state.
8399 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01008400 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02008401 hw_breakpoint_restore();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008402
Haozhong Zhang4ba76532015-10-20 15:39:07 +08008403 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
Zachary Amsden1d5f0662010-08-19 22:07:30 -10008404
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +08008405 vcpu->mode = OUTSIDE_GUEST_MODE;
Avi Kivityd94e1dc2010-05-03 16:54:48 +03008406 smp_wmb();
Yang Zhanga547c6d2013-04-11 19:25:10 +08008407
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008408 kvm_x86_ops->handle_exit_irqoff(vcpu, &exit_fastpath);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008409
Sean Christophersond7a08882019-07-10 09:07:34 -07008410 /*
8411 * Consume any pending interrupts, including the possible source of
8412 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8413 * An instruction is required after local_irq_enable() to fully unblock
8414 * interrupts on processors that implement an interrupt shadow, the
8415 * stat.exits increment will do nicely.
8416 */
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008417 kvm_before_interrupt(vcpu);
Sean Christophersond7a08882019-07-10 09:07:34 -07008418 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008419 ++vcpu->stat.exits;
Sean Christophersond7a08882019-07-10 09:07:34 -07008420 local_irq_disable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008421 kvm_after_interrupt(vcpu);
8422
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008423 guest_exit_irqoff();
Wanpeng Liec0671d2019-05-20 16:18:08 +08008424 if (lapic_in_kernel(vcpu)) {
8425 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8426 if (delta != S64_MIN) {
8427 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8428 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8429 }
8430 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008431
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008432 local_irq_enable();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008433 preempt_enable();
8434
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008435 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03008436
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008437 /*
8438 * Profile KVM exit RIPs:
8439 */
8440 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008441 unsigned long rip = kvm_rip_read(vcpu);
8442 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008443 }
8444
Zachary Amsdencc578282012-02-03 15:43:50 -02008445 if (unlikely(vcpu->arch.tsc_always_catchup))
8446 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02008447
Michael S. Tsirkin5cfb1d52012-06-24 19:24:54 +03008448 if (vcpu->arch.apic_attention)
8449 kvm_lapic_sync_from_vapic(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02008450
Wanpeng Li1e9e2622019-11-21 11:17:11 +08008451 r = kvm_x86_ops->handle_exit(vcpu, exit_fastpath);
Michael S. Tsirkind905c062012-06-24 19:25:00 +03008452 return r;
8453
8454cancel_injection:
8455 kvm_x86_ops->cancel_injection(vcpu);
Michael S. Tsirkinae7a2a32012-06-24 19:25:07 +03008456 if (unlikely(vcpu->arch.apic_attention))
8457 kvm_lapic_sync_from_vapic(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008458out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03008459 return r;
8460}
8461
Paolo Bonzini362c6982015-02-06 12:48:04 +01008462static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8463{
Feng Wubf9f6ac2015-09-18 22:29:55 +08008464 if (!kvm_arch_vcpu_runnable(vcpu) &&
8465 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008466 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8467 kvm_vcpu_block(vcpu);
8468 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008469
8470 if (kvm_x86_ops->post_block)
8471 kvm_x86_ops->post_block(vcpu);
8472
Paolo Bonzini9c8fd1b2015-02-06 12:58:42 +01008473 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8474 return 1;
8475 }
Gleb Natapov09cec752009-03-23 15:11:44 +02008476
Paolo Bonzini362c6982015-02-06 12:48:04 +01008477 kvm_apic_accept_events(vcpu);
8478 switch(vcpu->arch.mp_state) {
8479 case KVM_MP_STATE_HALTED:
8480 vcpu->arch.pv.pv_unhalted = false;
8481 vcpu->arch.mp_state =
8482 KVM_MP_STATE_RUNNABLE;
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06008483 /* fall through */
Paolo Bonzini362c6982015-02-06 12:48:04 +01008484 case KVM_MP_STATE_RUNNABLE:
8485 vcpu->arch.apf.halted = false;
8486 break;
8487 case KVM_MP_STATE_INIT_RECEIVED:
8488 break;
8489 default:
8490 return -EINTR;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008491 }
8492 return 1;
8493}
8494
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008495static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8496{
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008497 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
Sean Christophersona1c77ab2020-03-02 22:27:35 -08008498 kvm_x86_ops->check_nested_events(vcpu);
Paolo Bonzini0ad3bed2016-12-19 15:23:54 +01008499
Paolo Bonzini5d9bc642015-10-13 10:18:53 +02008500 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8501 !vcpu->arch.apf.halted);
8502}
8503
Paolo Bonzini362c6982015-02-06 12:48:04 +01008504static int vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03008505{
8506 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008507 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008508
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008509 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008510 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosattid7690172008-09-08 15:23:48 -03008511
Paolo Bonzini362c6982015-02-06 12:48:04 +01008512 for (;;) {
Paolo Bonzini58f800d2015-10-13 21:32:50 +02008513 if (kvm_vcpu_running(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008514 r = vcpu_enter_guest(vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008515 } else {
Paolo Bonzini362c6982015-02-06 12:48:04 +01008516 r = vcpu_block(kvm, vcpu);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008517 }
8518
Gleb Natapov09cec752009-03-23 15:11:44 +02008519 if (r <= 0)
8520 break;
8521
Radim Krčmář72875d82017-04-26 22:32:19 +02008522 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
Gleb Natapov09cec752009-03-23 15:11:44 +02008523 if (kvm_cpu_has_pending_timer(vcpu))
8524 kvm_inject_pending_timer_irqs(vcpu);
8525
Matt Gingell782d4222015-11-16 15:26:00 -08008526 if (dm_request_for_irq_injection(vcpu) &&
8527 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
Paolo Bonzini4ca7dd82015-07-30 10:32:16 +02008528 r = 0;
8529 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Gleb Natapov09cec752009-03-23 15:11:44 +02008530 ++vcpu->stat.request_irq_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008531 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008532 }
Gleb Natapovaf585b92010-10-14 11:22:46 +02008533
8534 kvm_check_async_pf_completion(vcpu);
8535
Gleb Natapov09cec752009-03-23 15:11:44 +02008536 if (signal_pending(current)) {
8537 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03008538 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02008539 ++vcpu->stat.signal_exits;
Paolo Bonzini362c6982015-02-06 12:48:04 +01008540 break;
Gleb Natapov09cec752009-03-23 15:11:44 +02008541 }
8542 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008543 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09008544 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008545 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03008546 }
8547 }
8548
Marcelo Tosattif656ce02009-12-23 14:35:25 -02008549 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008550
8551 return r;
8552}
8553
Gleb Natapov716d51a2012-09-03 15:24:26 +03008554static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8555{
8556 int r;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008557
Gleb Natapov716d51a2012-09-03 15:24:26 +03008558 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07008559 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008560 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008561 return r;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008562}
8563
8564static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8565{
8566 BUG_ON(!vcpu->arch.pio.count);
8567
8568 return complete_emulated_io(vcpu);
8569}
8570
Avi Kivityf78146b2012-04-18 19:22:47 +03008571/*
8572 * Implements the following, as a state machine:
8573 *
8574 * read:
8575 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008576 * for each mmio piece in the fragment
8577 * write gpa, len
8578 * exit
8579 * copy data
Avi Kivityf78146b2012-04-18 19:22:47 +03008580 * execute insn
8581 *
8582 * write:
8583 * for each fragment
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008584 * for each mmio piece in the fragment
8585 * write gpa, len
8586 * copy data
8587 * exit
Avi Kivityf78146b2012-04-18 19:22:47 +03008588 */
Gleb Natapov716d51a2012-09-03 15:24:26 +03008589static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
Avi Kivity5287f192010-01-19 14:20:10 +02008590{
8591 struct kvm_run *run = vcpu->run;
Avi Kivityf78146b2012-04-18 19:22:47 +03008592 struct kvm_mmio_fragment *frag;
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008593 unsigned len;
Avi Kivity5287f192010-01-19 14:20:10 +02008594
Gleb Natapov716d51a2012-09-03 15:24:26 +03008595 BUG_ON(!vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008596
Gleb Natapov716d51a2012-09-03 15:24:26 +03008597 /* Complete previous fragment */
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008598 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8599 len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008600 if (!vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008601 memcpy(frag->data, run->mmio.data, len);
8602
8603 if (frag->len <= 8) {
8604 /* Switch to the next fragment. */
8605 frag++;
8606 vcpu->mmio_cur_fragment++;
8607 } else {
8608 /* Go forward to the next mmio piece. */
8609 frag->data += len;
8610 frag->gpa += len;
8611 frag->len -= len;
8612 }
8613
Andrew Honiga08d3b32014-02-27 19:35:14 +01008614 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
Gleb Natapov716d51a2012-09-03 15:24:26 +03008615 vcpu->mmio_needed = 0;
Paolo Bonzini0912c972013-08-27 15:41:43 +02008616
8617 /* FIXME: return into emulator if single-stepping. */
Avi Kivitycef4dea2010-01-20 12:01:20 +02008618 if (vcpu->mmio_is_write)
Gleb Natapov716d51a2012-09-03 15:24:26 +03008619 return 1;
8620 vcpu->mmio_read_completed = 1;
8621 return complete_emulated_io(vcpu);
Avi Kivity5287f192010-01-19 14:20:10 +02008622 }
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008623
Gleb Natapov716d51a2012-09-03 15:24:26 +03008624 run->exit_reason = KVM_EXIT_MMIO;
8625 run->mmio.phys_addr = frag->gpa;
8626 if (vcpu->mmio_is_write)
Xiao Guangrong87da7e62012-10-24 14:07:59 +08008627 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8628 run->mmio.len = min(8u, frag->len);
Gleb Natapov716d51a2012-09-03 15:24:26 +03008629 run->mmio.is_write = vcpu->mmio_is_write;
8630 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8631 return 0;
Avi Kivity5287f192010-01-19 14:20:10 +02008632}
8633
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008634static void kvm_save_current_fpu(struct fpu *fpu)
8635{
8636 /*
8637 * If the target FPU state is not resident in the CPU registers, just
8638 * memcpy() from current, else save CPU state directly to the target.
8639 */
8640 if (test_thread_flag(TIF_NEED_FPU_LOAD))
8641 memcpy(&fpu->state, &current->thread.fpu.state,
8642 fpu_kernel_xstate_size);
8643 else
8644 copy_fpregs_to_fpstate(fpu);
8645}
8646
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008647/* Swap (qemu) user FPU context for the guest FPU context. */
8648static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8649{
Rik van Riel5f409e22019-04-03 18:41:52 +02008650 fpregs_lock();
8651
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008652 kvm_save_current_fpu(vcpu->arch.user_fpu);
8653
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008654 /* PKRU is separately restored in kvm_x86_ops->run. */
Marc Orrb666a4b2018-11-06 14:53:56 -08008655 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008656 ~XFEATURE_MASK_PKRU);
Rik van Riel5f409e22019-04-03 18:41:52 +02008657
8658 fpregs_mark_activate();
8659 fpregs_unlock();
8660
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008661 trace_kvm_fpu(1);
8662}
8663
8664/* When vcpu_run ends, restore user space FPU context. */
8665static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8666{
Rik van Riel5f409e22019-04-03 18:41:52 +02008667 fpregs_lock();
8668
Sean Christophersonc9aef3b2020-01-17 11:30:49 -08008669 kvm_save_current_fpu(vcpu->arch.guest_fpu);
8670
Wanpeng Lid9a710e2019-07-22 12:26:21 +08008671 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
Rik van Riel5f409e22019-04-03 18:41:52 +02008672
8673 fpregs_mark_activate();
8674 fpregs_unlock();
8675
Sebastian Andrzej Siewior822f3122018-09-12 15:33:45 +02008676 ++vcpu->stat.fpu_reload;
8677 trace_kvm_fpu(0);
8678}
8679
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008680int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8681{
8682 int r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008683
Christoffer Dallaccb7572017-12-04 21:35:25 +01008684 vcpu_load(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008685 kvm_sigset_activate(vcpu);
Peter Xu5663d8f2017-12-12 17:15:02 +01008686 kvm_load_guest_fpu(vcpu);
8687
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008688 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Jan H. Schönherr2f173d22017-09-06 18:34:06 +02008689 if (kvm_run->immediate_exit) {
8690 r = -EINTR;
8691 goto out;
8692 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008693 kvm_vcpu_block(vcpu);
Jan Kiszka66450a22013-03-13 12:42:34 +01008694 kvm_apic_accept_events(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +02008695 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008696 r = -EAGAIN;
Jan H. Schönherra0595002017-09-06 00:27:19 +02008697 if (signal_pending(current)) {
8698 r = -EINTR;
8699 vcpu->run->exit_reason = KVM_EXIT_INTR;
8700 ++vcpu->stat.signal_exits;
8701 }
Avi Kivityac9f6dc2008-07-06 15:48:31 +03008702 goto out;
8703 }
8704
Ken Hofsass01643c52018-01-31 16:03:36 -08008705 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8706 r = -EINVAL;
8707 goto out;
8708 }
8709
8710 if (vcpu->run->kvm_dirty_regs) {
8711 r = sync_regs(vcpu);
8712 if (r != 0)
8713 goto out;
8714 }
8715
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008716 /* re-sync apic's tpr */
Paolo Bonzini35754c92015-07-29 12:05:37 +02008717 if (!lapic_in_kernel(vcpu)) {
Andre Przywaraeea1cff2010-12-21 11:12:00 +01008718 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8719 r = -EINVAL;
8720 goto out;
8721 }
8722 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008723
Gleb Natapov716d51a2012-09-03 15:24:26 +03008724 if (unlikely(vcpu->arch.complete_userspace_io)) {
8725 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8726 vcpu->arch.complete_userspace_io = NULL;
8727 r = cui(vcpu);
8728 if (r <= 0)
Peter Xu5663d8f2017-12-12 17:15:02 +01008729 goto out;
Gleb Natapov716d51a2012-09-03 15:24:26 +03008730 } else
8731 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
Avi Kivity5287f192010-01-19 14:20:10 +02008732
Paolo Bonzini460df4c2017-02-08 11:50:15 +01008733 if (kvm_run->immediate_exit)
8734 r = -EINTR;
8735 else
8736 r = vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008737
8738out:
Peter Xu5663d8f2017-12-12 17:15:02 +01008739 kvm_put_guest_fpu(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008740 if (vcpu->run->kvm_valid_regs)
8741 store_regs(vcpu);
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03008742 post_kvm_run_save(vcpu);
Jan H. Schönherr20b70352017-11-24 22:39:01 +01008743 kvm_sigset_deactivate(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008744
Christoffer Dallaccb7572017-12-04 21:35:25 +01008745 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008746 return r;
8747}
8748
Ken Hofsass01643c52018-01-31 16:03:36 -08008749static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008750{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008751 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8752 /*
8753 * We are here if userspace calls get_regs() in the middle of
8754 * instruction emulation. Registers state needs to be copied
Guo Chao4a969982012-06-28 15:17:27 +08008755 * back from emulation context to vcpu. Userspace shouldn't do
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008756 * that usually, but some bad designed PV devices (vmware
8757 * backdoor interface) need this to work
8758 */
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008759 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008760 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8761 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07008762 regs->rax = kvm_rax_read(vcpu);
8763 regs->rbx = kvm_rbx_read(vcpu);
8764 regs->rcx = kvm_rcx_read(vcpu);
8765 regs->rdx = kvm_rdx_read(vcpu);
8766 regs->rsi = kvm_rsi_read(vcpu);
8767 regs->rdi = kvm_rdi_read(vcpu);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008768 regs->rsp = kvm_rsp_read(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008769 regs->rbp = kvm_rbp_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008770#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008771 regs->r8 = kvm_r8_read(vcpu);
8772 regs->r9 = kvm_r9_read(vcpu);
8773 regs->r10 = kvm_r10_read(vcpu);
8774 regs->r11 = kvm_r11_read(vcpu);
8775 regs->r12 = kvm_r12_read(vcpu);
8776 regs->r13 = kvm_r13_read(vcpu);
8777 regs->r14 = kvm_r14_read(vcpu);
8778 regs->r15 = kvm_r15_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008779#endif
8780
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008781 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02008782 regs->rflags = kvm_get_rflags(vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008783}
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008784
Ken Hofsass01643c52018-01-31 16:03:36 -08008785int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8786{
8787 vcpu_load(vcpu);
8788 __get_regs(vcpu, regs);
Christoffer Dall1fc9b762017-12-04 21:35:26 +01008789 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008790 return 0;
8791}
8792
Ken Hofsass01643c52018-01-31 16:03:36 -08008793static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008794{
Gleb Natapov7ae441e2011-03-31 12:06:41 +02008795 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8796 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8797
Sean Christophersonde3cd112019-04-30 10:36:17 -07008798 kvm_rax_write(vcpu, regs->rax);
8799 kvm_rbx_write(vcpu, regs->rbx);
8800 kvm_rcx_write(vcpu, regs->rcx);
8801 kvm_rdx_write(vcpu, regs->rdx);
8802 kvm_rsi_write(vcpu, regs->rsi);
8803 kvm_rdi_write(vcpu, regs->rdi);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02008804 kvm_rsp_write(vcpu, regs->rsp);
Sean Christophersonde3cd112019-04-30 10:36:17 -07008805 kvm_rbp_write(vcpu, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008806#ifdef CONFIG_X86_64
Sean Christophersonde3cd112019-04-30 10:36:17 -07008807 kvm_r8_write(vcpu, regs->r8);
8808 kvm_r9_write(vcpu, regs->r9);
8809 kvm_r10_write(vcpu, regs->r10);
8810 kvm_r11_write(vcpu, regs->r11);
8811 kvm_r12_write(vcpu, regs->r12);
8812 kvm_r13_write(vcpu, regs->r13);
8813 kvm_r14_write(vcpu, regs->r14);
8814 kvm_r15_write(vcpu, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008815#endif
8816
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008817 kvm_rip_write(vcpu, regs->rip);
Wanpeng Lid73235d2017-12-07 00:30:08 -08008818 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008819
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02008820 vcpu->arch.exception.pending = false;
8821
Avi Kivity3842d132010-07-27 12:30:24 +03008822 kvm_make_request(KVM_REQ_EVENT, vcpu);
Ken Hofsass01643c52018-01-31 16:03:36 -08008823}
Avi Kivity3842d132010-07-27 12:30:24 +03008824
Ken Hofsass01643c52018-01-31 16:03:36 -08008825int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8826{
8827 vcpu_load(vcpu);
8828 __set_regs(vcpu, regs);
Christoffer Dall875656f2017-12-04 21:35:27 +01008829 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008830 return 0;
8831}
8832
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008833void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8834{
8835 struct kvm_segment cs;
8836
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008837 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008838 *db = cs.db;
8839 *l = cs.l;
8840}
8841EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8842
Ken Hofsass01643c52018-01-31 16:03:36 -08008843static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008844{
Gleb Natapov89a27f42010-02-16 10:51:48 +02008845 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008846
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008847 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8848 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8849 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8850 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8851 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8852 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008853
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02008854 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8855 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008856
8857 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008858 sregs->idt.limit = dt.size;
8859 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008860 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02008861 sregs->gdt.limit = dt.size;
8862 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008863
Avi Kivity4d4ec082009-12-29 18:07:30 +02008864 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008865 sregs->cr2 = vcpu->arch.cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02008866 sregs->cr3 = kvm_read_cr3(vcpu);
Avi Kivityfc78f512009-12-07 12:16:48 +02008867 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02008868 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02008869 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008870 sregs->apic_base = kvm_get_apic_base(vcpu);
8871
Jordan Borgner0e96f312018-10-28 12:58:28 +00008872 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008873
Liran Alon04140b42018-03-23 03:01:31 +03008874 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03008875 set_bit(vcpu->arch.interrupt.nr,
8876 (unsigned long *)sregs->interrupt_bitmap);
Ken Hofsass01643c52018-01-31 16:03:36 -08008877}
Gleb Natapov16d7a192009-04-21 17:45:10 +03008878
Ken Hofsass01643c52018-01-31 16:03:36 -08008879int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8880 struct kvm_sregs *sregs)
8881{
8882 vcpu_load(vcpu);
8883 __get_sregs(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01008884 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008885 return 0;
8886}
8887
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008888int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8889 struct kvm_mp_state *mp_state)
8890{
Christoffer Dallfd232562017-12-04 21:35:30 +01008891 vcpu_load(vcpu);
Sean Christophersonf958bd22019-12-09 12:19:31 -08008892 if (kvm_mpx_supported())
8893 kvm_load_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008894
Jan Kiszka66450a22013-03-13 12:42:34 +01008895 kvm_apic_accept_events(vcpu);
Srivatsa Vaddagiri6aef2662013-08-26 14:18:34 +05308896 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8897 vcpu->arch.pv.pv_unhalted)
8898 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8899 else
8900 mp_state->mp_state = vcpu->arch.mp_state;
8901
Sean Christophersonf958bd22019-12-09 12:19:31 -08008902 if (kvm_mpx_supported())
8903 kvm_put_guest_fpu(vcpu);
Christoffer Dallfd232562017-12-04 21:35:30 +01008904 vcpu_put(vcpu);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008905 return 0;
8906}
8907
8908int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8909 struct kvm_mp_state *mp_state)
8910{
Christoffer Dalle83dff52017-12-04 21:35:31 +01008911 int ret = -EINVAL;
8912
8913 vcpu_load(vcpu);
8914
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01008915 if (!lapic_in_kernel(vcpu) &&
Jan Kiszka66450a22013-03-13 12:42:34 +01008916 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
Christoffer Dalle83dff52017-12-04 21:35:31 +01008917 goto out;
Jan Kiszka66450a22013-03-13 12:42:34 +01008918
Liran Alon27cbe7d2019-11-11 11:16:40 +02008919 /*
8920 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8921 * INIT state; latched init should be reported using
8922 * KVM_SET_VCPU_EVENTS, so reject it here.
8923 */
8924 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
David Hildenbrand28bf2882017-03-23 11:46:03 +01008925 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8926 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
Christoffer Dalle83dff52017-12-04 21:35:31 +01008927 goto out;
David Hildenbrand28bf2882017-03-23 11:46:03 +01008928
Jan Kiszka66450a22013-03-13 12:42:34 +01008929 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8930 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8931 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8932 } else
8933 vcpu->arch.mp_state = mp_state->mp_state;
Avi Kivity3842d132010-07-27 12:30:24 +03008934 kvm_make_request(KVM_REQ_EVENT, vcpu);
Christoffer Dalle83dff52017-12-04 21:35:31 +01008935
8936 ret = 0;
8937out:
8938 vcpu_put(vcpu);
8939 return ret;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03008940}
8941
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008942int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8943 int reason, bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008944{
Sean Christophersonc9b8b072020-02-18 15:29:48 -08008945 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008946 int ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008947
Mohammed Gamal8ec47222010-08-16 00:47:01 +03008948 init_emulate_ctxt(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02008949
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01008950 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008951 has_error_code, error_code);
Sean Christopherson10517782019-08-27 14:40:35 -07008952 if (ret) {
8953 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8954 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8955 vcpu->run->internal.ndata = 0;
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008956 return 0;
Sean Christopherson10517782019-08-27 14:40:35 -07008957 }
Gleb Natapovc6975182010-02-18 12:15:01 +02008958
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09008959 kvm_rip_write(vcpu, ctxt->eip);
8960 kvm_set_rflags(vcpu, ctxt->eflags);
Sean Christopherson60fc3d02019-08-27 14:40:38 -07008961 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02008962}
8963EXPORT_SYMBOL_GPL(kvm_task_switch);
8964
Peng Hao3140c152018-04-02 09:15:32 +08008965static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Lan Tianyuf2981032017-12-14 03:01:52 -05008966{
Tianyu Lan37b95952018-01-16 17:34:07 +08008967 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
Lan Tianyuf2981032017-12-14 03:01:52 -05008968 /*
8969 * When EFER.LME and CR0.PG are set, the processor is in
8970 * 64-bit mode (though maybe in a 32-bit code segment).
8971 * CR4.PAE and EFER.LMA must be set.
8972 */
Tianyu Lan37b95952018-01-16 17:34:07 +08008973 if (!(sregs->cr4 & X86_CR4_PAE)
Lan Tianyuf2981032017-12-14 03:01:52 -05008974 || !(sregs->efer & EFER_LMA))
8975 return -EINVAL;
8976 } else {
8977 /*
8978 * Not in 64-bit mode: EFER.LMA is clear and the code
8979 * segment cannot be 64-bit.
8980 */
8981 if (sregs->efer & EFER_LMA || sregs->cs.l)
8982 return -EINVAL;
8983 }
8984
Wanpeng Li3ca94192019-09-18 17:50:10 +08008985 return kvm_valid_cr4(vcpu, sregs->cr4);
Lan Tianyuf2981032017-12-14 03:01:52 -05008986}
8987
Ken Hofsass01643c52018-01-31 16:03:36 -08008988static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008989{
Jan Kiszka58cb6282014-01-24 16:48:44 +01008990 struct msr_data apic_base_msr;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05008991 int mmu_reset_needed = 0;
Wei Huangc4d21882018-05-01 09:49:54 -05008992 int cpuid_update_needed = 0;
Xiao Guangrong63f42e02011-01-12 15:39:18 +08008993 int pending_vec, max_bits, idx;
Gleb Natapov89a27f42010-02-16 10:51:48 +02008994 struct desc_ptr dt;
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01008995 int ret = -EINVAL;
8996
Lan Tianyuf2981032017-12-14 03:01:52 -05008997 if (kvm_valid_sregs(vcpu, sregs))
Eric Biggers8dbfb2b2017-12-20 16:24:27 -08008998 goto out;
Lan Tianyuf2981032017-12-14 03:01:52 -05008999
Jim Mattsond3802282017-08-10 10:14:13 -07009000 apic_base_msr.data = sregs->apic_base;
9001 apic_base_msr.host_initiated = true;
9002 if (kvm_set_apic_base(vcpu, &apic_base_msr))
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009003 goto out;
Petr Matousek6d1068b2012-11-06 19:24:07 +01009004
Gleb Natapov89a27f42010-02-16 10:51:48 +02009005 dt.size = sregs->idt.limit;
9006 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009007 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02009008 dt.size = sregs->gdt.limit;
9009 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009010 kvm_x86_ops->set_gdt(vcpu, &dt);
9011
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009012 vcpu->arch.cr2 = sregs->cr2;
Avi Kivity9f8fe502010-12-05 17:30:00 +02009013 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02009014 vcpu->arch.cr3 = sregs->cr3;
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07009015 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009016
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02009017 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009018
Avi Kivityf6801df2010-01-21 15:31:50 +02009019 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009020 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009021
Avi Kivity4d4ec082009-12-29 18:07:30 +02009022 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009023 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00009024 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009025
Avi Kivityfc78f512009-12-07 12:16:48 +02009026 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Wei Huangc4d21882018-05-01 09:49:54 -05009027 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
9028 (X86_CR4_OSXSAVE | X86_CR4_PKE));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009029 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Wei Huangc4d21882018-05-01 09:49:54 -05009030 if (cpuid_update_needed)
Avi Kivity00b27a32011-11-23 16:30:32 +02009031 kvm_update_cpuid(vcpu);
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009032
9033 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02009034 if (is_pae_paging(vcpu)) {
Avi Kivity9f8fe502010-12-05 17:30:00 +02009035 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02009036 mmu_reset_needed = 1;
9037 }
Xiao Guangrong63f42e02011-01-12 15:39:18 +08009038 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009039
9040 if (mmu_reset_needed)
9041 kvm_mmu_reset_context(vcpu);
9042
Michael S. Tsirkina50abc32012-09-05 20:00:52 +03009043 max_bits = KVM_NR_INTERRUPTS;
Gleb Natapov923c61b2009-05-11 13:35:48 +03009044 pending_vec = find_first_bit(
9045 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
9046 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009047 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03009048 pr_debug("Set back pending irq %d\n", pending_vec);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009049 }
9050
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009051 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
9052 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
9053 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
9054 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
9055 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
9056 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009057
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02009058 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
9059 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009060
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03009061 update_cr8_intercept(vcpu);
9062
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009063 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03009064 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009065 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02009066 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03009067 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9068
Avi Kivity3842d132010-07-27 12:30:24 +03009069 kvm_make_request(KVM_REQ_EVENT, vcpu);
9070
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009071 ret = 0;
9072out:
Ken Hofsass01643c52018-01-31 16:03:36 -08009073 return ret;
9074}
9075
9076int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
9077 struct kvm_sregs *sregs)
9078{
9079 int ret;
9080
9081 vcpu_load(vcpu);
9082 ret = __set_sregs(vcpu, sregs);
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01009083 vcpu_put(vcpu);
9084 return ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009085}
9086
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009087int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
9088 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009089{
Jan Kiszka355be0b2009-10-03 00:31:21 +02009090 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009091 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009092
Christoffer Dall66b56562017-12-04 21:35:33 +01009093 vcpu_load(vcpu);
9094
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009095 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
9096 r = -EBUSY;
9097 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03009098 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009099 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
9100 kvm_queue_exception(vcpu, DB_VECTOR);
9101 else
9102 kvm_queue_exception(vcpu, BP_VECTOR);
9103 }
9104
Jan Kiszka91586a32009-10-05 13:07:21 +02009105 /*
9106 * Read rflags as long as potentially injected trace flags are still
9107 * filtered out.
9108 */
9109 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009110
9111 vcpu->guest_debug = dbg->control;
9112 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
9113 vcpu->guest_debug = 0;
9114
9115 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009116 for (i = 0; i < KVM_NR_DB_REGS; ++i)
9117 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
Jan Kiszkac8639012012-09-21 05:42:55 +02009118 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009119 } else {
9120 for (i = 0; i < KVM_NR_DB_REGS; i++)
9121 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009122 }
Jan Kiszkac8639012012-09-21 05:42:55 +02009123 kvm_update_dr7(vcpu);
Jan Kiszkaae675ef2008-12-15 13:52:10 +01009124
Jan Kiszkaf92653e2010-02-23 17:47:55 +01009125 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9126 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
9127 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009128
Jan Kiszka91586a32009-10-05 13:07:21 +02009129 /*
9130 * Trigger an rflags update that will inject or remove the trace
9131 * flags.
9132 */
9133 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01009134
Paolo Bonzinia96036b2015-11-10 11:55:36 +01009135 kvm_x86_ops->update_bp_intercept(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02009136
Jan Kiszka4f926bf22009-10-30 12:46:59 +01009137 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009138
Avi Kivity2122ff52010-05-13 11:25:04 +03009139out:
Christoffer Dall66b56562017-12-04 21:35:33 +01009140 vcpu_put(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05009141 return r;
9142}
9143
9144/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08009145 * Translate a guest virtual address to a guest physical address.
9146 */
9147int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
9148 struct kvm_translation *tr)
9149{
9150 unsigned long vaddr = tr->linear_address;
9151 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009152 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009153
Christoffer Dall1da5b612017-12-04 21:35:32 +01009154 vcpu_load(vcpu);
9155
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009156 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02009157 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02009158 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009159 tr->physical_address = gpa;
9160 tr->valid = gpa != UNMAPPED_GVA;
9161 tr->writeable = 1;
9162 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08009163
Christoffer Dall1da5b612017-12-04 21:35:32 +01009164 vcpu_put(vcpu);
Zhang Xiantao8b006792007-11-16 13:05:55 +08009165 return 0;
9166}
9167
Hollis Blanchardd0752062007-10-31 17:24:25 -05009168int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9169{
Christoffer Dall13931232017-12-04 21:35:34 +01009170 struct fxregs_state *fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009171
Christoffer Dall13931232017-12-04 21:35:34 +01009172 vcpu_load(vcpu);
9173
Marc Orrb666a4b2018-11-06 14:53:56 -08009174 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009175 memcpy(fpu->fpr, fxsave->st_space, 128);
9176 fpu->fcw = fxsave->cwd;
9177 fpu->fsw = fxsave->swd;
9178 fpu->ftwx = fxsave->twd;
9179 fpu->last_opcode = fxsave->fop;
9180 fpu->last_ip = fxsave->rip;
9181 fpu->last_dp = fxsave->rdp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009182 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009183
Christoffer Dall13931232017-12-04 21:35:34 +01009184 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009185 return 0;
9186}
9187
9188int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9189{
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009190 struct fxregs_state *fxsave;
9191
9192 vcpu_load(vcpu);
9193
Marc Orrb666a4b2018-11-06 14:53:56 -08009194 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009195
Hollis Blanchardd0752062007-10-31 17:24:25 -05009196 memcpy(fxsave->st_space, fpu->fpr, 128);
9197 fxsave->cwd = fpu->fcw;
9198 fxsave->swd = fpu->fsw;
9199 fxsave->twd = fpu->ftwx;
9200 fxsave->fop = fpu->last_opcode;
9201 fxsave->rip = fpu->last_ip;
9202 fxsave->rdp = fpu->last_dp;
Jordan Borgner0e96f312018-10-28 12:58:28 +00009203 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
Hollis Blanchardd0752062007-10-31 17:24:25 -05009204
Christoffer Dall6a96bc72017-12-04 21:35:35 +01009205 vcpu_put(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05009206 return 0;
9207}
9208
Ken Hofsass01643c52018-01-31 16:03:36 -08009209static void store_regs(struct kvm_vcpu *vcpu)
9210{
9211 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9212
9213 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9214 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9215
9216 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9217 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9218
9219 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9220 kvm_vcpu_ioctl_x86_get_vcpu_events(
9221 vcpu, &vcpu->run->s.regs.events);
9222}
9223
9224static int sync_regs(struct kvm_vcpu *vcpu)
9225{
9226 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9227 return -EINVAL;
9228
9229 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9230 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9231 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9232 }
9233 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9234 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9235 return -EINVAL;
9236 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9237 }
9238 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9239 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9240 vcpu, &vcpu->run->s.regs.events))
9241 return -EINVAL;
9242 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9243 }
9244
9245 return 0;
9246}
9247
Ingo Molnar0ee6a512015-04-27 06:58:22 +02009248static void fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05009249{
Marc Orrb666a4b2018-11-06 14:53:56 -08009250 fpstate_init(&vcpu->arch.guest_fpu->state);
Borislav Petkov782511b2016-04-04 22:25:03 +02009251 if (boot_cpu_has(X86_FEATURE_XSAVES))
Marc Orrb666a4b2018-11-06 14:53:56 -08009252 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
Paolo Bonzinidf1daba2014-11-21 19:05:07 +01009253 host_xcr0 | XSTATE_COMPACTION_ENABLED;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009254
Dexuan Cui2acf9232010-06-10 11:27:12 +08009255 /*
9256 * Ensure guest xcr0 is valid for loading
9257 */
Dave Hansend91cab72015-09-02 16:31:26 -07009258 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Dexuan Cui2acf9232010-06-10 11:27:12 +08009259
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009260 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05009261}
Hollis Blanchardd0752062007-10-31 17:24:25 -05009262
Sean Christopherson897cc382019-12-18 13:55:09 -08009263int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009264{
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009265 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
Sean Christopherson897cc382019-12-18 13:55:09 -08009266 pr_warn_once("kvm: SMP vm created on host with unstable TSC; "
9267 "guest TSC will not be reliable\n");
Liang Lic447e762015-05-21 04:41:25 +08009268
Sean Christopherson897cc382019-12-18 13:55:09 -08009269 return 0;
Avi Kivity26e52152007-11-20 15:30:24 +02009270}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009271
Sean Christophersone529ef62019-12-18 13:55:15 -08009272int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Avi Kivity26e52152007-11-20 15:30:24 +02009273{
Sean Christopherson95a0d012019-12-18 13:55:23 -08009274 struct page *page;
9275 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009276
Sean Christopherson95a0d012019-12-18 13:55:23 -08009277 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
9278 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
9279 else
9280 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
9281
9282 kvm_set_tsc_khz(vcpu, max_tsc_khz);
9283
9284 r = kvm_mmu_create(vcpu);
9285 if (r < 0)
9286 return r;
9287
9288 if (irqchip_in_kernel(vcpu->kvm)) {
Sean Christopherson95a0d012019-12-18 13:55:23 -08009289 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
9290 if (r < 0)
9291 goto fail_mmu_destroy;
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06009292 if (kvm_apicv_activated(vcpu->kvm))
9293 vcpu->arch.apicv_active = true;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009294 } else
9295 static_key_slow_inc(&kvm_no_apic_vcpu);
9296
9297 r = -ENOMEM;
9298
9299 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9300 if (!page)
9301 goto fail_free_lapic;
9302 vcpu->arch.pio_data = page_address(page);
9303
9304 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
9305 GFP_KERNEL_ACCOUNT);
9306 if (!vcpu->arch.mce_banks)
9307 goto fail_free_pio_data;
9308 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9309
9310 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9311 GFP_KERNEL_ACCOUNT))
9312 goto fail_free_mce_banks;
9313
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009314 if (!alloc_emulate_ctxt(vcpu))
9315 goto free_wbinvd_dirty_mask;
9316
Sean Christopherson95a0d012019-12-18 13:55:23 -08009317 vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
9318 GFP_KERNEL_ACCOUNT);
9319 if (!vcpu->arch.user_fpu) {
9320 pr_err("kvm: failed to allocate userspace's fpu\n");
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009321 goto free_emulate_ctxt;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009322 }
9323
9324 vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
9325 GFP_KERNEL_ACCOUNT);
9326 if (!vcpu->arch.guest_fpu) {
9327 pr_err("kvm: failed to allocate vcpu's fpu\n");
9328 goto free_user_fpu;
9329 }
9330 fx_init(vcpu);
9331
9332 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
9333
9334 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9335
9336 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9337
9338 kvm_async_pf_hash_reset(vcpu);
9339 kvm_pmu_init(vcpu);
9340
9341 vcpu->arch.pending_external_vector = -1;
9342 vcpu->arch.preempted_in_kernel = false;
9343
9344 kvm_hv_vcpu_init(vcpu);
9345
9346 r = kvm_x86_ops->vcpu_create(vcpu);
9347 if (r)
9348 goto free_guest_fpu;
Sean Christopherson5f73db12019-12-18 13:55:18 -08009349
Sean Christopherson0cf91352019-03-07 15:43:02 -08009350 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
Jim Mattsone53d88af2018-10-30 12:20:21 -07009351 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
Xiao Guangrong19efffa2015-06-15 16:55:31 +08009352 kvm_vcpu_mtrr_init(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009353 vcpu_load(vcpu);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009354 kvm_vcpu_reset(vcpu, false);
Paolo Bonzinie1732992018-10-08 21:28:09 +02009355 kvm_init_mmu(vcpu, false);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009356 vcpu_put(vcpu);
Christoffer Dallec7660c2017-12-04 21:35:23 +01009357 return 0;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009358
9359free_guest_fpu:
9360 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9361free_user_fpu:
9362 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009363free_emulate_ctxt:
9364 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009365free_wbinvd_dirty_mask:
9366 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9367fail_free_mce_banks:
9368 kfree(vcpu->arch.mce_banks);
9369fail_free_pio_data:
9370 free_page((unsigned long)vcpu->arch.pio_data);
9371fail_free_lapic:
9372 kvm_free_lapic(vcpu);
9373fail_mmu_destroy:
9374 kvm_mmu_destroy(vcpu);
9375 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009376}
9377
Dominik Dingel31928aa2014-12-04 15:47:07 +01009378void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009379{
Will Auld8fe8ab42012-11-29 12:42:12 -08009380 struct msr_data msr;
Andrew Jones332967a2014-02-28 12:52:55 +01009381 struct kvm *kvm = vcpu->kvm;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009382
Roman Kagand3457c82017-07-14 17:13:20 +03009383 kvm_hv_vcpu_postcreate(vcpu);
9384
Christoffer Dallec7660c2017-12-04 21:35:23 +01009385 if (mutex_lock_killable(&vcpu->mutex))
Dominik Dingel31928aa2014-12-04 15:47:07 +01009386 return;
Christoffer Dallec7660c2017-12-04 21:35:23 +01009387 vcpu_load(vcpu);
Will Auld8fe8ab42012-11-29 12:42:12 -08009388 msr.data = 0x0;
9389 msr.index = MSR_IA32_TSC;
9390 msr.host_initiated = true;
9391 kvm_write_tsc(vcpu, &msr);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009392 vcpu_put(vcpu);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -03009393
9394 /* poll control enabled by default */
9395 vcpu->arch.msr_kvm_poll_control = 1;
9396
Christoffer Dallec7660c2017-12-04 21:35:23 +01009397 mutex_unlock(&vcpu->mutex);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009398
Wanpeng Lib34de572020-02-28 11:18:41 +08009399 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0)
9400 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9401 KVMCLOCK_SYNC_PERIOD);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02009402}
9403
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06009404void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009405{
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009406 struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache;
Sean Christopherson95a0d012019-12-18 13:55:23 -08009407 int idx;
Gleb Natapov344d9582010-10-14 11:22:50 +02009408
Paolo Bonzini4cbc4182020-01-30 18:47:38 +01009409 kvm_release_pfn(cache->pfn, cache->dirty, cache);
9410
Sean Christopherson50b143e2019-12-18 13:55:07 -08009411 kvmclock_reset(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009412
9413 kvm_x86_ops->vcpu_free(vcpu);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009414
Sean Christophersonc9b8b072020-02-18 15:29:48 -08009415 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
Sean Christopherson50b143e2019-12-18 13:55:07 -08009416 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
9417 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
9418 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
Sean Christopherson95a0d012019-12-18 13:55:23 -08009419
9420 kvm_hv_vcpu_uninit(vcpu);
9421 kvm_pmu_destroy(vcpu);
9422 kfree(vcpu->arch.mce_banks);
9423 kvm_free_lapic(vcpu);
9424 idx = srcu_read_lock(&vcpu->kvm->srcu);
9425 kvm_mmu_destroy(vcpu);
9426 srcu_read_unlock(&vcpu->kvm->srcu, idx);
9427 free_page((unsigned long)vcpu->arch.pio_data);
9428 if (!lapic_in_kernel(vcpu))
9429 static_key_slow_dec(&kvm_no_apic_vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009430}
9431
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009432void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009433{
Radim Krčmářb7e31be2018-03-01 15:24:25 +01009434 kvm_lapic_reset(vcpu, init_event);
9435
Paolo Bonzinie69fab52015-06-04 10:44:44 +02009436 vcpu->arch.hflags = 0;
9437
Paolo Bonzinic43203c2016-06-01 22:26:00 +02009438 vcpu->arch.smi_pending = 0;
Liran Alon52797bf2017-11-15 13:43:14 +02009439 vcpu->arch.smi_count = 0;
Avi Kivity7460fb4a2011-09-20 13:43:14 +03009440 atomic_set(&vcpu->arch.nmi_queued, 0);
9441 vcpu->arch.nmi_pending = 0;
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009442 vcpu->arch.nmi_injected = false;
Nadav Amit5f7552d2014-06-30 12:03:02 +03009443 kvm_clear_interrupt_queue(vcpu);
9444 kvm_clear_exception_queue(vcpu);
Jan Kiszka448fa4a2008-09-26 09:30:48 +02009445
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009446 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
Nadav Amitae561ed2015-04-02 03:10:37 +03009447 kvm_update_dr0123(vcpu);
Nadav Amit6f43ed02014-07-15 17:37:46 +03009448 vcpu->arch.dr6 = DR6_INIT;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009449 kvm_update_dr6(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009450 vcpu->arch.dr7 = DR7_FIXED_1;
Jan Kiszkac8639012012-09-21 05:42:55 +02009451 kvm_update_dr7(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01009452
Nadav Amit11190222015-04-02 03:10:38 +03009453 vcpu->arch.cr2 = 0;
9454
Avi Kivity3842d132010-07-27 12:30:24 +03009455 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov344d9582010-10-14 11:22:50 +02009456 vcpu->arch.apf.msr_val = 0;
Glauber Costac9aaa892011-07-11 15:28:14 -04009457 vcpu->arch.st.msr_val = 0;
Avi Kivity3842d132010-07-27 12:30:24 +03009458
Glauber Costa12f9a482011-02-01 14:16:40 -05009459 kvmclock_reset(vcpu);
9460
Gleb Natapovaf585b92010-10-14 11:22:46 +02009461 kvm_clear_async_pf_completion_queue(vcpu);
9462 kvm_async_pf_hash_reset(vcpu);
9463 vcpu->arch.apf.halted = false;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009464
Wanpeng Lia554d202017-10-11 05:10:19 -07009465 if (kvm_mpx_supported()) {
9466 void *mpx_state_buffer;
9467
9468 /*
9469 * To avoid have the INIT path from kvm_apic_has_events() that be
9470 * called with loaded FPU and does not let userspace fix the state.
9471 */
Rik van Rielf775b132017-11-14 16:54:23 -05009472 if (init_event)
9473 kvm_put_guest_fpu(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08009474 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009475 XFEATURE_BNDREGS);
Wanpeng Lia554d202017-10-11 05:10:19 -07009476 if (mpx_state_buffer)
9477 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
Marc Orrb666a4b2018-11-06 14:53:56 -08009478 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
Sebastian Andrzej Siewiorabd16d62019-04-03 18:41:40 +02009479 XFEATURE_BNDCSR);
Wanpeng Lia554d202017-10-11 05:10:19 -07009480 if (mpx_state_buffer)
9481 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
Rik van Rielf775b132017-11-14 16:54:23 -05009482 if (init_event)
9483 kvm_load_guest_fpu(vcpu);
Wanpeng Lia554d202017-10-11 05:10:19 -07009484 }
9485
Paolo Bonzini64d60672015-05-07 11:36:11 +02009486 if (!init_event) {
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009487 kvm_pmu_reset(vcpu);
Paolo Bonzini64d60672015-05-07 11:36:11 +02009488 vcpu->arch.smbase = 0x30000;
Kyle Hueydb2336a2017-03-20 01:16:28 -07009489
Kyle Hueydb2336a2017-03-20 01:16:28 -07009490 vcpu->arch.msr_misc_features_enables = 0;
Wanpeng Lia554d202017-10-11 05:10:19 -07009491
9492 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
Paolo Bonzini64d60672015-05-07 11:36:11 +02009493 }
Gleb Natapovf5132b02011-11-10 14:57:22 +02009494
Julian Stecklina66f7b722012-12-05 15:26:19 +01009495 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9496 vcpu->arch.regs_avail = ~0;
9497 vcpu->arch.regs_dirty = ~0;
9498
Wanpeng Lia554d202017-10-11 05:10:19 -07009499 vcpu->arch.ia32_xss = 0;
9500
Nadav Amitd28bc9d2015-04-13 14:34:08 +03009501 kvm_x86_ops->vcpu_reset(vcpu, init_event);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009502}
9503
Paolo Bonzini2b4a2732014-11-24 14:35:24 +01009504void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
Jan Kiszka66450a22013-03-13 12:42:34 +01009505{
9506 struct kvm_segment cs;
9507
9508 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9509 cs.selector = vector << 8;
9510 cs.base = vector << 12;
9511 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9512 kvm_rip_write(vcpu, 0);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009513}
9514
Radim Krčmář13a34e02014-08-28 15:13:03 +02009515int kvm_arch_hardware_enable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009516{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10009517 struct kvm *kvm;
9518 struct kvm_vcpu *vcpu;
9519 int i;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009520 int ret;
9521 u64 local_tsc;
9522 u64 max_tsc = 0;
9523 bool stable, backwards_tsc = false;
Avi Kivity18863bd2009-09-07 11:12:18 +03009524
9525 kvm_shared_msr_cpu_online();
Radim Krčmář13a34e02014-08-28 15:13:03 +02009526 ret = kvm_x86_ops->hardware_enable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009527 if (ret != 0)
9528 return ret;
9529
Andy Lutomirski4ea16362015-06-25 18:44:07 +02009530 local_tsc = rdtsc();
Vitaly Kuznetsovb0c39dc2018-01-24 14:23:36 +01009531 stable = !kvm_check_tsc_unstable();
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009532 list_for_each_entry(kvm, &vm_list, vm_list) {
9533 kvm_for_each_vcpu(i, vcpu, kvm) {
9534 if (!stable && vcpu->cpu == smp_processor_id())
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009535 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009536 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9537 backwards_tsc = true;
9538 if (vcpu->arch.last_host_tsc > max_tsc)
9539 max_tsc = vcpu->arch.last_host_tsc;
9540 }
9541 }
9542 }
9543
9544 /*
9545 * Sometimes, even reliable TSCs go backwards. This happens on
9546 * platforms that reset TSC during suspend or hibernate actions, but
9547 * maintain synchronization. We must compensate. Fortunately, we can
9548 * detect that condition here, which happens early in CPU bringup,
9549 * before any KVM threads can be running. Unfortunately, we can't
9550 * bring the TSCs fully up to date with real time, as we aren't yet far
9551 * enough into CPU bringup that we know how much real time has actually
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02009552 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009553 * variables that haven't been updated yet.
9554 *
9555 * So we simply find the maximum observed TSC above, then record the
9556 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9557 * the adjustment will be applied. Note that we accumulate
9558 * adjustments, in case multiple suspend cycles happen before some VCPU
9559 * gets a chance to run again. In the event that no KVM threads get a
9560 * chance to run, we will miss the entire elapsed period, as we'll have
9561 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9562 * loose cycle time. This isn't too big a deal, since the loss will be
9563 * uniform across all VCPUs (not to mention the scenario is extremely
9564 * unlikely). It is possible that a second hibernate recovery happens
9565 * much faster than a first, causing the observed TSC here to be
9566 * smaller; this would require additional padding adjustment, which is
9567 * why we set last_host_tsc to the local tsc observed here.
9568 *
9569 * N.B. - this code below runs only on platforms with reliable TSC,
9570 * as that is the only way backwards_tsc is set above. Also note
9571 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9572 * have the same delta_cyc adjustment applied if backwards_tsc
9573 * is detected. Note further, this adjustment is only done once,
9574 * as we reset last_host_tsc on all VCPUs to stop this from being
9575 * called multiple times (one for each physical CPU bringup).
9576 *
Guo Chao4a969982012-06-28 15:17:27 +08009577 * Platforms with unreliable TSCs don't have to deal with this, they
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009578 * will be compensated by the logic in vcpu_load, which sets the TSC to
9579 * catchup mode. This will catchup all VCPUs to real time, but cannot
9580 * guarantee that they stay in perfect synchronization.
9581 */
9582 if (backwards_tsc) {
9583 u64 delta_cyc = max_tsc - local_tsc;
9584 list_for_each_entry(kvm, &vm_list, vm_list) {
Ladi Proseka826faf2017-06-26 09:56:43 +02009585 kvm->arch.backwards_tsc_observed = true;
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009586 kvm_for_each_vcpu(i, vcpu, kvm) {
9587 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9588 vcpu->arch.last_host_tsc = local_tsc;
Guo Hui Liu105b21b2014-09-12 13:43:19 +08009589 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Zachary Amsden0dd6a6e2012-02-03 15:43:56 -02009590 }
9591
9592 /*
9593 * We have to disable TSC offset matching.. if you were
9594 * booting a VM while issuing an S4 host suspend....
9595 * you may have some problem. Solving this issue is
9596 * left as an exercise to the reader.
9597 */
9598 kvm->arch.last_tsc_nsec = 0;
9599 kvm->arch.last_tsc_write = 0;
9600 }
9601
9602 }
9603 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009604}
9605
Radim Krčmář13a34e02014-08-28 15:13:03 +02009606void kvm_arch_hardware_disable(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009607{
Radim Krčmář13a34e02014-08-28 15:13:03 +02009608 kvm_x86_ops->hardware_disable();
9609 drop_user_return_notifiers();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009610}
9611
9612int kvm_arch_hardware_setup(void)
9613{
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009614 int r;
9615
9616 r = kvm_x86_ops->hardware_setup();
9617 if (r != 0)
9618 return r;
9619
Sean Christophersonb11306b2019-12-10 14:44:13 -08009620 cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data);
9621
Haozhong Zhang35181e82015-10-20 15:39:03 +08009622 if (kvm_has_tsc_control) {
9623 /*
9624 * Make sure the user can only configure tsc_khz values that
9625 * fit into a signed integer.
Marcelo Tosatti273ba452018-06-11 14:12:10 -03009626 * A min value is not calculated because it will always
Haozhong Zhang35181e82015-10-20 15:39:03 +08009627 * be 1 on all machines.
9628 */
9629 u64 max = min(0x7fffffffULL,
9630 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9631 kvm_max_guest_tsc_khz = max;
9632
Haozhong Zhangad7218832015-10-20 15:39:02 +08009633 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
Haozhong Zhang35181e82015-10-20 15:39:03 +08009634 }
Haozhong Zhangad7218832015-10-20 15:39:02 +08009635
Aaron Lewis139a12c2019-10-21 16:30:25 -07009636 if (boot_cpu_has(X86_FEATURE_XSAVES))
9637 rdmsrl(MSR_IA32_XSS, host_xss);
9638
Nadav Amit9e9c3fe2015-04-12 21:47:15 +03009639 kvm_init_msr_list();
9640 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009641}
9642
9643void kvm_arch_hardware_unsetup(void)
9644{
9645 kvm_x86_ops->hardware_unsetup();
9646}
9647
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009648int kvm_arch_check_processor_compat(void)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009649{
Sean Christophersonf1cdecf2019-12-10 14:44:14 -08009650 struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
9651
9652 WARN_ON(!irqs_disabled());
9653
9654 if (kvm_host_cr4_reserved_bits(c) != cr4_reserved_bits)
9655 return -EIO;
9656
Sean Christophersonf257d6d2019-04-19 22:18:17 -07009657 return kvm_x86_ops->check_processor_compatibility();
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08009658}
9659
Paolo Bonzinid71ba782015-07-29 11:56:48 +02009660bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9661{
9662 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9663}
9664EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9665
9666bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9667{
9668 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
9669}
9670
Gleb Natapov54e98182012-08-05 15:58:32 +03009671struct static_key kvm_no_apic_vcpu __read_mostly;
Paolo Bonzinibce87cc2016-01-08 13:48:51 +01009672EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
Gleb Natapov54e98182012-08-05 15:58:32 +03009673
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009674void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9675{
Like Xub35e5542019-10-27 18:52:43 +08009676 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9677
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009678 vcpu->arch.l1tf_flush_l1d = true;
Like Xub35e5542019-10-27 18:52:43 +08009679 if (pmu->version && unlikely(pmu->event_count)) {
9680 pmu->need_cleanup = true;
9681 kvm_make_request(KVM_REQ_PMU, vcpu);
9682 }
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009683 kvm_x86_ops->sched_in(vcpu, cpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02009684}
9685
Sean Christopherson562b6b02020-01-26 16:41:13 -08009686void kvm_arch_free_vm(struct kvm *kvm)
9687{
9688 kfree(kvm->arch.hyperv.hv_pa_pg);
9689 vfree(kvm);
9690}
9691
9692
Carsten Ottee08b9632012-01-04 10:25:20 +01009693int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009694{
Carsten Ottee08b9632012-01-04 10:25:20 +01009695 if (type)
9696 return -EINVAL;
9697
Paolo Bonzini6ef768f2014-11-20 13:45:31 +01009698 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08009699 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Sean Christopherson10605202019-09-12 19:46:10 -07009700 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009701 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03009702 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06009703 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009704
Sheng Yang5550af42008-10-15 20:15:06 +08009705 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9706 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Alex Williamson7a844282012-09-21 11:58:03 -06009707 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9708 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9709 &kvm->arch.irq_sources_bitmap);
Sheng Yang5550af42008-10-15 20:15:06 +08009710
Jan Kiszka038f8c12011-02-04 10:49:11 +01009711 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
Gleb Natapov1e08ec42012-09-13 17:19:24 +03009712 mutex_init(&kvm->arch.apic_map_lock);
Marcelo Tosattid8281992012-11-27 23:29:01 -02009713 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9714
Paolo Bonzini8171cd62020-01-22 14:36:09 +01009715 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
Marcelo Tosattid8281992012-11-27 23:29:01 -02009716 pvclock_update_vm_gtod_copy(kvm);
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01009717
Drew Schmitt6fbbde92018-08-20 10:32:15 -07009718 kvm->arch.guest_can_read_msr_platform_info = true;
9719
Andrew Jones7e44e442014-02-28 12:52:54 +01009720 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
Andrew Jones332967a2014-02-28 12:52:55 +01009721 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
Andrew Jones7e44e442014-02-28 12:52:54 +01009722
Roman Kagancbc02362018-02-01 16:48:31 +03009723 kvm_hv_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009724 kvm_page_track_init(kvm);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009725 kvm_mmu_init_vm(kvm);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +08009726
Sean Christopherson92735b12019-08-02 15:06:17 -07009727 return kvm_x86_ops->vm_init(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009728}
9729
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009730int kvm_arch_post_init_vm(struct kvm *kvm)
9731{
9732 return kvm_mmu_post_init_vm(kvm);
9733}
9734
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009735static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9736{
Christoffer Dallec7660c2017-12-04 21:35:23 +01009737 vcpu_load(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009738 kvm_mmu_unload(vcpu);
9739 vcpu_put(vcpu);
9740}
9741
9742static void kvm_free_vcpus(struct kvm *kvm)
9743{
9744 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009745 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009746
9747 /*
9748 * Unpin any mmu pages first.
9749 */
Gleb Natapovaf585b92010-10-14 11:22:46 +02009750 kvm_for_each_vcpu(i, vcpu, kvm) {
9751 kvm_clear_async_pf_completion_queue(vcpu);
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009752 kvm_unload_vcpu_mmu(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02009753 }
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009754 kvm_for_each_vcpu(i, vcpu, kvm)
Sean Christopherson4543bdc2019-12-18 13:55:14 -08009755 kvm_vcpu_destroy(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009756
Gleb Natapov988a2ca2009-06-09 15:56:29 +03009757 mutex_lock(&kvm->lock);
9758 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9759 kvm->vcpus[i] = NULL;
9760
9761 atomic_set(&kvm->online_vcpus, 0);
9762 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009763}
9764
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009765void kvm_arch_sync_events(struct kvm *kvm)
9766{
Andrew Jones332967a2014-02-28 12:52:55 +01009767 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
Andrew Jones7e44e442014-02-28 12:52:54 +01009768 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08009769 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08009770}
9771
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009772int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009773{
9774 int i, r;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009775 unsigned long hva, uninitialized_var(old_npages);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009776 struct kvm_memslots *slots = kvm_memslots(kvm);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009777 struct kvm_memory_slot *slot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009778
9779 /* Called with kvm->slots_lock held. */
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009780 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9781 return -EINVAL;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009782
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009783 slot = id_to_memslot(slots, id);
9784 if (size) {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009785 if (slot && slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009786 return -EEXIST;
9787
9788 /*
9789 * MAP_SHARED to prevent internal slot pages from being moved
9790 * by fork()/COW.
9791 */
9792 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9793 MAP_SHARED | MAP_ANONYMOUS, 0);
9794 if (IS_ERR((void *)hva))
9795 return PTR_ERR((void *)hva);
9796 } else {
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009797 if (!slot || !slot->npages)
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009798 return 0;
9799
Sean Christophersonabbed4f2020-03-04 16:24:22 -08009800 /*
9801 * Stuff a non-canonical value to catch use-after-delete. This
9802 * ends up being 0 on 32-bit KVM, but there's no better
9803 * alternative.
9804 */
9805 hva = (unsigned long)(0xdeadull << 48);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009806 old_npages = slot->npages;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009807 }
9808
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009809 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009810 struct kvm_userspace_memory_region m;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009811
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009812 m.slot = id | (i << 16);
9813 m.flags = 0;
9814 m.guest_phys_addr = gpa;
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009815 m.userspace_addr = hva;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02009816 m.memory_size = size;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009817 r = __kvm_set_memory_region(kvm, &m);
9818 if (r < 0)
9819 return r;
9820 }
9821
Eric Biggers103c7632018-01-31 17:30:21 -08009822 if (!size)
Sean Christopherson0577d1a2020-02-18 13:07:31 -08009823 vm_munmap(hva, old_npages * PAGE_SIZE);
Paolo Bonzinif0d648b2015-10-12 13:56:27 +02009824
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02009825 return 0;
9826}
9827EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9828
Junaid Shahid1aa9b952019-11-04 20:26:00 +01009829void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9830{
9831 kvm_mmu_pre_destroy_vm(kvm);
9832}
9833
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009834void kvm_arch_destroy_vm(struct kvm *kvm)
9835{
Andrew Honig27469d22013-04-18 09:38:14 -07009836 if (current->mm == kvm->mm) {
9837 /*
9838 * Free memory regions allocated on behalf of userspace,
9839 * unless the the memory map has changed due to process exit
9840 * or fd copying.
9841 */
Peter Xu6a3c6232020-01-09 09:57:16 -05009842 mutex_lock(&kvm->slots_lock);
9843 __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
9844 0, 0);
9845 __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
9846 0, 0);
9847 __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
9848 mutex_unlock(&kvm->slots_lock);
Andrew Honig27469d22013-04-18 09:38:14 -07009849 }
Suravee Suthikulpanit03543132016-05-04 14:09:42 -05009850 if (kvm_x86_ops->vm_destroy)
9851 kvm_x86_ops->vm_destroy(kvm);
Peter Xuc7611592017-03-15 16:01:19 +08009852 kvm_pic_destroy(kvm);
9853 kvm_ioapic_destroy(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009854 kvm_free_vcpus(kvm);
Radim Krčmářaf1bae52016-07-12 22:09:30 +02009855 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
Eric Hankland66bb8a02019-07-10 18:25:15 -07009856 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
Xiao Guangrong13d268c2016-02-24 17:51:16 +08009857 kvm_mmu_uninit_vm(kvm);
Paolo Bonzini2beb6dad2017-03-27 17:53:50 +02009858 kvm_page_track_cleanup(kvm);
Roman Kagancbc02362018-02-01 16:48:31 +03009859 kvm_hv_destroy_vm(kvm);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08009860}
Zhang Xiantao0de10342007-11-20 16:25:04 +08009861
Sean Christophersone96c81e2020-02-18 13:07:27 -08009862void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009863{
9864 int i;
9865
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009866 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Sean Christophersone96c81e2020-02-18 13:07:27 -08009867 kvfree(slot->arch.rmap[i]);
9868 slot->arch.rmap[i] = NULL;
9869
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009870 if (i == 0)
9871 continue;
9872
Sean Christophersone96c81e2020-02-18 13:07:27 -08009873 kvfree(slot->arch.lpage_info[i - 1]);
9874 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009875 }
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009876
Sean Christophersone96c81e2020-02-18 13:07:27 -08009877 kvm_page_track_free_memslot(slot);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009878}
9879
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009880static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
9881 unsigned long npages)
9882{
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009883 int i;
9884
Sean Christophersonedd4fa32020-02-18 13:07:15 -08009885 /*
9886 * Clear out the previous array pointers for the KVM_MR_MOVE case. The
9887 * old arrays will be freed by __kvm_set_memory_region() if installing
9888 * the new memslot is successful.
9889 */
9890 memset(&slot->arch, 0, sizeof(slot->arch));
9891
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009892 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009893 struct kvm_lpage_info *linfo;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009894 unsigned long ugfn;
9895 int lpages;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009896 int level = i + 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009897
9898 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9899 slot->base_gfn, level) + 1;
9900
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009901 slot->arch.rmap[i] =
Kees Cook778e1cd2018-06-12 14:04:48 -07009902 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
Ben Gardon254272c2019-02-11 11:02:50 -08009903 GFP_KERNEL_ACCOUNT);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009904 if (!slot->arch.rmap[i])
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009905 goto out_free;
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009906 if (i == 0)
9907 continue;
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09009908
Ben Gardon254272c2019-02-11 11:02:50 -08009909 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009910 if (!linfo)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009911 goto out_free;
9912
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009913 slot->arch.lpage_info[i - 1] = linfo;
9914
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009915 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009916 linfo[0].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009917 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009918 linfo[lpages - 1].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009919 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9920 /*
9921 * If the gfn and userspace address are not aligned wrt each
9922 * other, or if explicitly asked to, disable large page
9923 * support for this slot
9924 */
9925 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
9926 !kvm_largepages_enabled()) {
9927 unsigned long j;
9928
9929 for (j = 0; j < lpages; ++j)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08009930 linfo[j].disallow_lpage = 1;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009931 }
9932 }
9933
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08009934 if (kvm_page_track_create_memslot(slot, npages))
9935 goto out_free;
9936
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009937 return 0;
9938
9939out_free:
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009940 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
Thomas Huth548ef282015-02-24 21:29:25 +01009941 kvfree(slot->arch.rmap[i]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009942 slot->arch.rmap[i] = NULL;
9943 if (i == 0)
9944 continue;
9945
Thomas Huth548ef282015-02-24 21:29:25 +01009946 kvfree(slot->arch.lpage_info[i - 1]);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09009947 slot->arch.lpage_info[i - 1] = NULL;
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09009948 }
9949 return -ENOMEM;
9950}
9951
Sean Christopherson15248252019-02-05 12:54:17 -08009952void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009953{
Boris Ostrovsky91724812019-12-05 01:30:51 +00009954 struct kvm_vcpu *vcpu;
9955 int i;
9956
Takuya Yoshikawae6dff7d2013-07-04 13:41:26 +09009957 /*
9958 * memslots->generation has been incremented.
9959 * mmio generation may have reached its maximum value.
9960 */
Sean Christopherson15248252019-02-05 12:54:17 -08009961 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
Boris Ostrovsky91724812019-12-05 01:30:51 +00009962
9963 /* Force re-initialization of steal_time cache */
9964 kvm_for_each_vcpu(i, vcpu, kvm)
9965 kvm_vcpu_kick(vcpu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09009966}
9967
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009968int kvm_arch_prepare_memory_region(struct kvm *kvm,
9969 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02009970 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09009971 enum kvm_mr_change change)
Zhang Xiantao0de10342007-11-20 16:25:04 +08009972{
Sean Christopherson0dab98b2020-02-18 13:07:19 -08009973 if (change == KVM_MR_CREATE || change == KVM_MR_MOVE)
9974 return kvm_alloc_memslot_metadata(memslot,
9975 mem->memory_size >> PAGE_SHIFT);
Marcelo Tosattif7784b82009-12-23 14:35:18 -02009976 return 0;
9977}
9978
Kai Huang88178fd2015-01-28 10:54:27 +08009979static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9980 struct kvm_memory_slot *new)
9981{
9982 /* Still write protect RO slot */
9983 if (new->flags & KVM_MEM_READONLY) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08009984 kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
Kai Huang88178fd2015-01-28 10:54:27 +08009985 return;
9986 }
9987
9988 /*
9989 * Call kvm_x86_ops dirty logging hooks when they are valid.
9990 *
9991 * kvm_x86_ops->slot_disable_log_dirty is called when:
9992 *
9993 * - KVM_MR_CREATE with dirty logging is disabled
9994 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9995 *
9996 * The reason is, in case of PML, we need to set D-bit for any slots
9997 * with dirty logging disabled in order to eliminate unnecessary GPA
Miaohe Lin0a03cbd2019-12-06 16:20:18 +08009998 * logging in PML buffer (and potential PML buffer full VMEXIT). This
Kai Huang88178fd2015-01-28 10:54:27 +08009999 * guarantees leaving PML enabled during guest's lifetime won't have
Wei Yangbdd303c2018-11-05 14:45:03 +080010000 * any additional overhead from PML when guest is running with dirty
Kai Huang88178fd2015-01-28 10:54:27 +080010001 * logging disabled for memory slots.
10002 *
10003 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
10004 * to dirty logging mode.
10005 *
10006 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
10007 *
10008 * In case of write protect:
10009 *
10010 * Write protect all pages for dirty logging.
10011 *
10012 * All the sptes including the large sptes which point to this
10013 * slot are set to readonly. We can not create any new large
10014 * spte on this slot until the end of the logging.
10015 *
10016 * See the comments in fast_page_fault().
10017 */
10018 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
Jay Zhou3c9bd402020-02-27 09:32:27 +080010019 if (kvm_x86_ops->slot_enable_log_dirty) {
Kai Huang88178fd2015-01-28 10:54:27 +080010020 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
Jay Zhou3c9bd402020-02-27 09:32:27 +080010021 } else {
10022 int level =
10023 kvm_dirty_log_manual_protect_and_init_set(kvm) ?
10024 PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
10025
10026 /*
10027 * If we're with initial-all-set, we don't need
10028 * to write protect any small page because
10029 * they're reported as dirty already. However
10030 * we still need to write-protect huge pages
10031 * so that the page split can happen lazily on
10032 * the first write to the huge page.
10033 */
10034 kvm_mmu_slot_remove_write_access(kvm, new, level);
10035 }
Kai Huang88178fd2015-01-28 10:54:27 +080010036 } else {
10037 if (kvm_x86_ops->slot_disable_log_dirty)
10038 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
10039 }
10040}
10041
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010042void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +020010043 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -080010044 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010045 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +090010046 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -020010047{
Xiao Guangrong48c0e4e2011-03-04 18:59:21 +080010048 if (!kvm->arch.n_requested_mmu_pages)
Wei Yang4d666232018-09-27 08:31:26 +080010049 kvm_mmu_change_mmu_pages(kvm,
10050 kvm_mmu_calculate_default_mmu_pages(kvm));
Kai Huang1c91cad42015-01-28 10:54:26 +080010051
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010052 /*
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010053 * Dirty logging tracks sptes in 4k granularity, meaning that large
10054 * sptes have to be split. If live migration is successful, the guest
10055 * in the source machine will be destroyed and large sptes will be
10056 * created in the destination. However, if the guest continues to run
10057 * in the source machine (for example if live migration fails), small
10058 * sptes will remain around and cause bad performance.
10059 *
10060 * Scan sptes if dirty logging has been stopped, dropping those
10061 * which can be collapsed into a single large-page spte. Later
10062 * page faults will create the large-page sptes.
Sean Christopherson319109a2019-09-11 12:19:52 -070010063 *
10064 * There is no need to do this in any of the following cases:
10065 * CREATE: No dirty mappings will already exist.
10066 * MOVE/DELETE: The old mappings will already have been cleaned up by
10067 * kvm_arch_flush_shadow_memslot()
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010068 */
Sean Christopherson319109a2019-09-11 12:19:52 -070010069 if (change == KVM_MR_FLAGS_ONLY &&
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +080010070 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
10071 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
10072 kvm_mmu_zap_collapsible_sptes(kvm, new);
10073
10074 /*
Kai Huang88178fd2015-01-28 10:54:27 +080010075 * Set up write protection and/or dirty logging for the new slot.
Xiao Guangrongc126d942014-04-17 17:06:14 +080010076 *
Kai Huang88178fd2015-01-28 10:54:27 +080010077 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
10078 * been zapped so no dirty logging staff is needed for old slot. For
10079 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
10080 * new and it's also covered when dealing with the new slot.
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010081 *
10082 * FIXME: const-ify all uses of struct kvm_memory_slot.
Takuya Yoshikawac972f3b2013-01-08 19:43:28 +090010083 */
Kai Huang88178fd2015-01-28 10:54:27 +080010084 if (change != KVM_MR_DELETE)
Paolo Bonzinif36f3f22015-05-18 13:20:23 +020010085 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
Sean Christopherson21198842020-02-18 13:07:25 -080010086
10087 /* Free the arrays associated with the old memslot. */
10088 if (change == KVM_MR_MOVE)
Sean Christophersone96c81e2020-02-18 13:07:27 -080010089 kvm_arch_free_memslot(kvm, old);
Zhang Xiantao0de10342007-11-20 16:25:04 +080010090}
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010091
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010092void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010093{
Sean Christopherson7390de12019-02-05 13:01:31 -080010094 kvm_mmu_zap_all(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -030010095}
10096
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010097void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
10098 struct kvm_memory_slot *slot)
10099{
Xiaoguang Chenae7cd872016-10-09 15:41:44 +080010100 kvm_page_track_flush_slot(kvm, slot);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -030010101}
10102
Liran Alone6c67d82018-09-04 10:56:52 +030010103static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
10104{
10105 return (is_guest_mode(vcpu) &&
10106 kvm_x86_ops->guest_apic_has_interrupt &&
10107 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
10108}
10109
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010110static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
10111{
10112 if (!list_empty_careful(&vcpu->async_pf.done))
10113 return true;
10114
10115 if (kvm_apic_has_events(vcpu))
10116 return true;
10117
10118 if (vcpu->arch.pv.pv_unhalted)
10119 return true;
10120
Wanpeng Lia5f01f82017-09-13 04:04:01 -070010121 if (vcpu->arch.exception.pending)
10122 return true;
10123
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010124 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10125 (vcpu->arch.nmi_pending &&
10126 kvm_x86_ops->nmi_allowed(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010127 return true;
10128
ZhuangYanying47a66ee2017-05-26 13:16:48 +080010129 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
10130 (vcpu->arch.smi_pending && !is_smm(vcpu)))
Paolo Bonzini73917732015-10-13 10:19:35 +020010131 return true;
10132
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010133 if (kvm_arch_interrupt_allowed(vcpu) &&
Liran Alone6c67d82018-09-04 10:56:52 +030010134 (kvm_cpu_has_interrupt(vcpu) ||
10135 kvm_guest_apic_has_interrupt(vcpu)))
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010136 return true;
10137
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +030010138 if (kvm_hv_has_stimer_pending(vcpu))
10139 return true;
10140
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010141 return false;
10142}
10143
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010144int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
10145{
Paolo Bonzini5d9bc642015-10-13 10:18:53 +020010146 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
Zhang Xiantao1d737c82007-12-14 09:35:10 +080010147}
Zhang Xiantao57361992007-12-17 14:21:40 +080010148
Wanpeng Li17e433b2019-08-05 10:03:19 +080010149bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
10150{
10151 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
10152 return true;
10153
10154 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
10155 kvm_test_request(KVM_REQ_SMI, vcpu) ||
10156 kvm_test_request(KVM_REQ_EVENT, vcpu))
10157 return true;
10158
10159 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
10160 return true;
10161
10162 return false;
10163}
10164
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010165bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
10166{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +080010167 return vcpu->arch.preempted_in_kernel;
Longpeng(Mike)199b5762017-08-08 12:05:32 +080010168}
10169
Christoffer Dallb6d33832012-03-08 16:44:24 -050010170int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
Zhang Xiantao57361992007-12-17 14:21:40 +080010171{
Christoffer Dallb6d33832012-03-08 16:44:24 -050010172 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
Zhang Xiantao57361992007-12-17 14:21:40 +080010173}
Gleb Natapov78646122009-03-23 12:12:11 +020010174
10175int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
10176{
10177 return kvm_x86_ops->interrupt_allowed(vcpu);
10178}
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010179
Nadav Amit82b32772014-11-02 11:54:45 +020010180unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
10181{
10182 if (is_64_bit_mode(vcpu))
10183 return kvm_rip_read(vcpu);
10184 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
10185 kvm_rip_read(vcpu));
10186}
10187EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
10188
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010189bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
10190{
Nadav Amit82b32772014-11-02 11:54:45 +020010191 return kvm_get_linear_rip(vcpu) == linear_rip;
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010192}
10193EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
10194
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010195unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
10196{
10197 unsigned long rflags;
10198
10199 rflags = kvm_x86_ops->get_rflags(vcpu);
10200 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +010010201 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010202 return rflags;
10203}
10204EXPORT_SYMBOL_GPL(kvm_get_rflags);
10205
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010206static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010207{
10208 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +010010209 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +010010210 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010211 kvm_x86_ops->set_rflags(vcpu, rflags);
Paolo Bonzini6addfc42014-03-27 11:29:28 +010010212}
10213
10214void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
10215{
10216 __kvm_set_rflags(vcpu, rflags);
Avi Kivity3842d132010-07-27 12:30:24 +030010217 kvm_make_request(KVM_REQ_EVENT, vcpu);
Jan Kiszka94fe45d2009-10-18 13:24:44 +020010218}
10219EXPORT_SYMBOL_GPL(kvm_set_rflags);
10220
Gleb Natapov56028d02010-10-17 18:13:42 +020010221void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10222{
10223 int r;
10224
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010225 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
chai wenf2e10662013-10-14 22:22:33 +080010226 work->wakeup_all)
Gleb Natapov56028d02010-10-17 18:13:42 +020010227 return;
10228
10229 r = kvm_mmu_reload(vcpu);
10230 if (unlikely(r))
10231 return;
10232
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020010233 if (!vcpu->arch.mmu->direct_map &&
Sean Christophersond8dd54e2020-03-02 18:02:39 -080010234 work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu))
Xiao Guangrongfb67e142010-12-07 10:35:25 +080010235 return;
10236
Sean Christopherson7a026742020-02-06 14:14:34 -080010237 kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true);
Gleb Natapov56028d02010-10-17 18:13:42 +020010238}
10239
Gleb Natapovaf585b92010-10-14 11:22:46 +020010240static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10241{
10242 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10243}
10244
10245static inline u32 kvm_async_pf_next_probe(u32 key)
10246{
10247 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10248}
10249
10250static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10251{
10252 u32 key = kvm_async_pf_hash_fn(gfn);
10253
10254 while (vcpu->arch.apf.gfns[key] != ~0)
10255 key = kvm_async_pf_next_probe(key);
10256
10257 vcpu->arch.apf.gfns[key] = gfn;
10258}
10259
10260static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10261{
10262 int i;
10263 u32 key = kvm_async_pf_hash_fn(gfn);
10264
10265 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
Xiao Guangrongc7d28c22010-11-01 17:00:30 +080010266 (vcpu->arch.apf.gfns[key] != gfn &&
10267 vcpu->arch.apf.gfns[key] != ~0); i++)
Gleb Natapovaf585b92010-10-14 11:22:46 +020010268 key = kvm_async_pf_next_probe(key);
10269
10270 return key;
10271}
10272
10273bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10274{
10275 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10276}
10277
10278static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10279{
10280 u32 i, j, k;
10281
10282 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10283 while (true) {
10284 vcpu->arch.apf.gfns[i] = ~0;
10285 do {
10286 j = kvm_async_pf_next_probe(j);
10287 if (vcpu->arch.apf.gfns[j] == ~0)
10288 return;
10289 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10290 /*
10291 * k lies cyclically in ]i,j]
10292 * | i.k.j |
10293 * |....j i.k.| or |.k..j i...|
10294 */
10295 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10296 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10297 i = j;
10298 }
10299}
10300
Gleb Natapov7c907052010-10-14 11:22:53 +020010301static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10302{
Paolo Bonzini4e335d92017-05-02 16:20:18 +020010303
10304 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10305 sizeof(val));
Gleb Natapov7c907052010-10-14 11:22:53 +020010306}
10307
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010308static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10309{
10310
10311 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10312 sizeof(u32));
10313}
10314
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010315static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10316{
10317 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10318 return false;
10319
10320 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10321 (vcpu->arch.apf.send_user_only &&
10322 kvm_x86_ops->get_cpl(vcpu) == 0))
10323 return false;
10324
10325 return true;
10326}
10327
10328bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10329{
10330 if (unlikely(!lapic_in_kernel(vcpu) ||
10331 kvm_event_needs_reinjection(vcpu) ||
10332 vcpu->arch.exception.pending))
10333 return false;
10334
10335 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10336 return false;
10337
10338 /*
10339 * If interrupts are off we cannot even use an artificial
10340 * halt state.
10341 */
10342 return kvm_x86_ops->interrupt_allowed(vcpu);
10343}
10344
Gleb Natapovaf585b92010-10-14 11:22:46 +020010345void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10346 struct kvm_async_pf *work)
10347{
Avi Kivity6389ee92010-11-29 16:12:30 +020010348 struct x86_exception fault;
10349
Sean Christopherson736c2912019-12-06 15:57:14 -080010350 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010351 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
Gleb Natapov7c907052010-10-14 11:22:53 +020010352
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010353 if (kvm_can_deliver_async_pf(vcpu) &&
10354 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
Avi Kivity6389ee92010-11-29 16:12:30 +020010355 fault.vector = PF_VECTOR;
10356 fault.error_code_valid = true;
10357 fault.error_code = 0;
10358 fault.nested_page_fault = false;
10359 fault.address = work->arch.token;
Wanpeng Liadfe20f2017-07-13 18:30:41 -070010360 fault.async_page_fault = true;
Avi Kivity6389ee92010-11-29 16:12:30 +020010361 kvm_inject_page_fault(vcpu, &fault);
Paolo Bonzini1dfdb452019-06-05 16:46:44 +020010362 } else {
10363 /*
10364 * It is not possible to deliver a paravirtualized asynchronous
10365 * page fault, but putting the guest in an artificial halt state
10366 * can be beneficial nevertheless: if an interrupt arrives, we
10367 * can deliver it timely and perhaps the guest will schedule
10368 * another process. When the instruction that triggered a page
10369 * fault is retried, hopefully the page will be ready in the host.
10370 */
10371 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
Gleb Natapov7c907052010-10-14 11:22:53 +020010372 }
Gleb Natapovaf585b92010-10-14 11:22:46 +020010373}
10374
10375void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10376 struct kvm_async_pf *work)
10377{
Avi Kivity6389ee92010-11-29 16:12:30 +020010378 struct x86_exception fault;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010379 u32 val;
Avi Kivity6389ee92010-11-29 16:12:30 +020010380
chai wenf2e10662013-10-14 22:22:33 +080010381 if (work->wakeup_all)
Gleb Natapov7c907052010-10-14 11:22:53 +020010382 work->arch.token = ~0; /* broadcast wakeup */
10383 else
10384 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
Sean Christopherson736c2912019-12-06 15:57:14 -080010385 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
Gleb Natapov7c907052010-10-14 11:22:53 +020010386
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010387 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10388 !apf_get_user(vcpu, &val)) {
10389 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10390 vcpu->arch.exception.pending &&
10391 vcpu->arch.exception.nr == PF_VECTOR &&
10392 !apf_put_user(vcpu, 0)) {
10393 vcpu->arch.exception.injected = false;
10394 vcpu->arch.exception.pending = false;
10395 vcpu->arch.exception.nr = 0;
10396 vcpu->arch.exception.has_error_code = false;
10397 vcpu->arch.exception.error_code = 0;
Jim Mattsonc8514362018-10-16 14:29:19 -070010398 vcpu->arch.exception.has_payload = false;
10399 vcpu->arch.exception.payload = 0;
Wanpeng Li9a6e7c32017-09-14 03:54:16 -070010400 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10401 fault.vector = PF_VECTOR;
10402 fault.error_code_valid = true;
10403 fault.error_code = 0;
10404 fault.nested_page_fault = false;
10405 fault.address = work->arch.token;
10406 fault.async_page_fault = true;
10407 kvm_inject_page_fault(vcpu, &fault);
10408 }
Gleb Natapov7c907052010-10-14 11:22:53 +020010409 }
Xiao Guangronge6d53e32010-11-01 17:01:28 +080010410 vcpu->arch.apf.halted = false;
Gleb Natapova4fa1632012-05-03 11:36:39 +030010411 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Gleb Natapov7c907052010-10-14 11:22:53 +020010412}
10413
10414bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10415{
10416 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10417 return true;
10418 else
Wanpeng Li9bc1f092017-06-08 20:13:40 -070010419 return kvm_can_do_async_pf(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020010420}
10421
Paolo Bonzini5544eb92015-07-07 15:41:58 +020010422void kvm_arch_start_assignment(struct kvm *kvm)
10423{
10424 atomic_inc(&kvm->arch.assigned_device_count);
10425}
10426EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10427
10428void kvm_arch_end_assignment(struct kvm *kvm)
10429{
10430 atomic_dec(&kvm->arch.assigned_device_count);
10431}
10432EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10433
10434bool kvm_arch_has_assigned_device(struct kvm *kvm)
10435{
10436 return atomic_read(&kvm->arch.assigned_device_count);
10437}
10438EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10439
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060010440void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10441{
10442 atomic_inc(&kvm->arch.noncoherent_dma_count);
10443}
10444EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10445
10446void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10447{
10448 atomic_dec(&kvm->arch.noncoherent_dma_count);
10449}
10450EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10451
10452bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10453{
10454 return atomic_read(&kvm->arch.noncoherent_dma_count);
10455}
10456EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10457
Alex Williamson14717e22016-05-05 11:58:35 -060010458bool kvm_arch_has_irq_bypass(void)
10459{
Sean Christopherson92735b12019-08-02 15:06:17 -070010460 return true;
Alex Williamson14717e22016-05-05 11:58:35 -060010461}
10462
Feng Wu87276882015-09-18 22:29:40 +080010463int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10464 struct irq_bypass_producer *prod)
10465{
10466 struct kvm_kernel_irqfd *irqfd =
10467 container_of(cons, struct kvm_kernel_irqfd, consumer);
10468
Alex Williamson14717e22016-05-05 11:58:35 -060010469 irqfd->producer = prod;
Feng Wu87276882015-09-18 22:29:40 +080010470
Alex Williamson14717e22016-05-05 11:58:35 -060010471 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10472 prod->irq, irqfd->gsi, 1);
Feng Wu87276882015-09-18 22:29:40 +080010473}
10474
10475void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10476 struct irq_bypass_producer *prod)
10477{
10478 int ret;
10479 struct kvm_kernel_irqfd *irqfd =
10480 container_of(cons, struct kvm_kernel_irqfd, consumer);
10481
Feng Wu87276882015-09-18 22:29:40 +080010482 WARN_ON(irqfd->producer != prod);
10483 irqfd->producer = NULL;
10484
10485 /*
10486 * When producer of consumer is unregistered, we change back to
10487 * remapped mode, so we can re-use the current implementation
Andrea Gelminibb3541f2016-05-21 14:14:44 +020010488 * when the irq is masked/disabled or the consumer side (KVM
Feng Wu87276882015-09-18 22:29:40 +080010489 * int this case doesn't want to receive the interrupts.
10490 */
10491 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10492 if (ret)
10493 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10494 " fails: %d\n", irqfd->consumer.token, ret);
10495}
10496
10497int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10498 uint32_t guest_irq, bool set)
10499{
Feng Wu87276882015-09-18 22:29:40 +080010500 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10501}
10502
Feng Wu520040142016-01-25 16:53:33 +080010503bool kvm_vector_hashing_enabled(void)
10504{
10505 return vector_hashing;
10506}
Feng Wu520040142016-01-25 16:53:33 +080010507
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010508bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10509{
10510 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10511}
10512EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10513
Paolo Bonzini6441fa62020-01-20 16:33:06 +010010514u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu)
10515{
10516 uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD;
10517
10518 /* The STIBP bit doesn't fault even if it's not advertised */
10519 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
10520 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
10521 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10522 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
10523 !boot_cpu_has(X86_FEATURE_AMD_IBRS))
10524 bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP);
10525
10526 if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) &&
10527 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
10528 bits &= ~SPEC_CTRL_SSBD;
10529 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
10530 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
10531 bits &= ~SPEC_CTRL_SSBD;
10532
10533 return bits;
10534}
10535EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits);
Marcelo Tosatti2d5ba192019-06-03 19:52:44 -030010536
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010537EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
Jason Wang931c33b2015-09-15 14:41:58 +080010538EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010539EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10540EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10541EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10542EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +020010543EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +020010544EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +020010545EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +020010546EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Sean Christopherson5497b952019-07-11 08:58:29 -070010547EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
Joerg Roedelec1ff792009-10-09 16:08:31 +020010548EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +020010549EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +010010550EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +090010551EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
Peter Xu4f75bcc2019-09-06 10:17:22 +080010552EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
Kai Huang843e4332015-01-28 10:54:28 +080010553EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
Feng Wuefc64402015-09-18 22:29:51 +080010554EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050010555EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10556EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);
Suravee Suthikulpanit24bbf742019-11-14 14:15:07 -060010557EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request);